Skip to content

Commit

Permalink
Use addEventListener for MediaKeySession events
Browse files Browse the repository at this point in the history
  • Loading branch information
robwalch committed Dec 11, 2023
1 parent e036913 commit 3fac248
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 61 deletions.
4 changes: 4 additions & 0 deletions api-extractor/report/hls.js.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -2804,6 +2804,10 @@ export interface MediaKeySessionContext {
mediaKeys: MediaKeys;
// (undocumented)
mediaKeysSession: MediaKeySession;
// (undocumented)
_onkeystatuseschange?: (this: MediaKeySession, ev: Event) => any;
// (undocumented)
_onmessage?: (this: MediaKeySession, ev: MediaKeyMessageEvent) => any;
}

// Warning: (ae-missing-release-tag) "MediaPlaylist" is part of the package's API, but it is missing a release tag (@alpha, @beta, @public, or @internal)
Expand Down
35 changes: 28 additions & 7 deletions src/controller/eme-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ export interface MediaKeySessionContext {
mediaKeysSession: MediaKeySession;
keyStatus: MediaKeyStatus;
licenseXhr?: XMLHttpRequest;
_onmessage?: (this: MediaKeySession, ev: MediaKeyMessageEvent) => any;
_onkeystatuseschange?: (this: MediaKeySession, ev: Event) => any;
}

/**
Expand Down Expand Up @@ -717,7 +719,7 @@ class EMEController implements ComponentAPI {

const licenseStatus = new EventEmitter();

context.mediaKeysSession.onmessage = (event: MediaKeyMessageEvent) => {
const onmessage = (context._onmessage = (event: MediaKeyMessageEvent) => {
const keySession = context.mediaKeysSession;
if (!keySession) {
licenseStatus.emit('error', new Error('invalid state'));
Expand All @@ -743,10 +745,10 @@ class EMEController implements ComponentAPI {
} else {
this.warn(`unhandled media key message type "${messageType}"`);
}
};
});

context.mediaKeysSession.onkeystatuseschange = (
event: MediaKeyMessageEvent,
const onkeystatuseschange = (context._onkeystatuseschange = (
event: Event,
) => {
const keySession = context.mediaKeysSession;
if (!keySession) {
Expand All @@ -760,7 +762,13 @@ class EMEController implements ComponentAPI {
this.warn(`${context.keySystem} expired for key ${keyId}`);
this.renewKeySession(context);
}
};
});

context.mediaKeysSession.addEventListener('message', onmessage);
context.mediaKeysSession.addEventListener(
'keystatuseschange',
onkeystatuseschange,
);

const keyUsablePromise = new Promise(
(resolve: (value?: void) => void, reject) => {
Expand Down Expand Up @@ -1269,8 +1277,21 @@ class EMEController implements ComponentAPI {
this.log(
`Remove licenses and keys and close session ${mediaKeysSession.sessionId}`,
);
mediaKeysSession.onmessage = null;
mediaKeysSession.onkeystatuseschange = null;
if (mediaKeySessionContext._onmessage) {
mediaKeysSession.removeEventListener(
'message',
mediaKeySessionContext._onmessage,
);
mediaKeySessionContext._onmessage = undefined;
}
if (mediaKeySessionContext._onkeystatuseschange) {
mediaKeysSession.removeEventListener(
'keystatuseschange',
mediaKeySessionContext._onkeystatuseschange,
);
mediaKeySessionContext._onkeystatuseschange = undefined;
}

if (licenseXhr && licenseXhr.readyState !== XMLHttpRequest.DONE) {
licenseXhr.abort();
}
Expand Down
89 changes: 35 additions & 54 deletions tests/unit/controller/eme-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,34 @@ class MediaMock extends EventEmitter {
}
}

class MediaKeySessionMock extends EventEmitter {
addEventListener: any;
removeEventListener: any;
keyStatuses: Map<Uint8Array, string>;
constructor() {
super();
this.keyStatuses = new Map();
this.addEventListener = this.addListener.bind(this);
this.removeEventListener = this.removeListener.bind(this);
}
generateRequest() {
return Promise.resolve().then(() => {
this.emit('message', {
messageType: 'license-request',
message: new Uint8Array(0),
});
this.keyStatuses.set(new Uint8Array(0), 'usable');
this.emit('keystatuseschange', {});
});
}
remove() {
return Promise.resolve();
}
update() {
return Promise.resolve();
}
}

let emeController: EMEControllerTestable;
let media: MediaMock;
let sinonFakeXMLHttpRequestStatic: sinon.SinonFakeXMLHttpRequestStatic;
Expand Down Expand Up @@ -82,24 +110,7 @@ describe('EMEController', function () {
createMediaKeys: sinon.spy(() =>
Promise.resolve({
setServerCertificate: () => Promise.resolve(),
createSession: (): Partial<MediaKeySession> => ({
addEventListener: () => {},
onmessage: null,
onkeystatuseschange: null,
generateRequest() {
return Promise.resolve().then(() => {
this.onmessage({
messageType: 'license-request',
message: new Uint8Array(0),
});
this.keyStatuses.set(new Uint8Array(0), 'usable');
this.onkeystatuseschange({});
});
},
remove: () => Promise.resolve(),
update: () => Promise.resolve(),
keyStatuses: new Map(),
}),
createSession: () => new MediaKeySessionMock(),
}),
),
});
Expand Down Expand Up @@ -163,24 +174,7 @@ describe('EMEController', function () {
createMediaKeys: sinon.spy(() =>
Promise.resolve({
setServerCertificate: () => Promise.resolve(),
createSession: (): Partial<MediaKeySession> => ({
addEventListener: () => {},
onmessage: null,
onkeystatuseschange: null,
generateRequest() {
return Promise.resolve().then(() => {
this.onmessage({
messageType: 'license-request',
message: new Uint8Array(0),
});
this.keyStatuses.set(new Uint8Array(0), 'usable');
this.onkeystatuseschange({});
});
},
remove: () => Promise.resolve(),
update: () => Promise.resolve(),
keyStatuses: new Map(),
}),
createSession: () => new MediaKeySessionMock(),
}),
),
});
Expand Down Expand Up @@ -260,6 +254,7 @@ describe('EMEController', function () {
setServerCertificate: () => Promise.resolve(),
createSession: () => ({
addEventListener: () => {},
removeEventListener: () => {},
generateRequest: () => Promise.reject(new Error('bad data')),
remove: () => Promise.resolve(),
update: () => Promise.resolve(),
Expand Down Expand Up @@ -324,24 +319,7 @@ describe('EMEController', function () {
createMediaKeys: sinon.spy(() =>
Promise.resolve({
setServerCertificate: mediaKeysSetServerCertificateSpy,
createSession: () => ({
addEventListener: () => {},
onmessage: null,
onkeystatuseschange: null,
generateRequest() {
return Promise.resolve().then(() => {
this.onmessage({
messageType: 'license-request',
message: new Uint8Array(0),
});
this.keyStatuses.set(new Uint8Array(0), 'usable');
this.onkeystatuseschange({});
});
},
remove: () => Promise.resolve(),
update: () => Promise.resolve(),
keyStatuses: new Map(),
}),
createSession: () => new MediaKeySessionMock(),
}),
),
});
Expand Down Expand Up @@ -421,6 +399,7 @@ describe('EMEController', function () {
setServerCertificate: mediaKeysSetServerCertificateSpy,
createSession: () => ({
addEventListener: () => {},
removeEventListener: () => {},
generateRequest: () => Promise.resolve(),
remove: () => Promise.resolve(),
update: () => Promise.resolve(),
Expand Down Expand Up @@ -505,6 +484,7 @@ describe('EMEController', function () {
Promise.resolve({
createSession: () => ({
addEventListener: () => {},
removeEventListener: () => {},
generateRequest: () => Promise.resolve(),
remove: () => Promise.resolve(),
update: () => Promise.resolve(),
Expand Down Expand Up @@ -583,6 +563,7 @@ describe('EMEController', function () {
setServerCertificate: () => Promise.resolve(),
createSession: () => ({
addEventListener: () => {},
removeEventListener: () => {},
generateRequest: () => Promise.resolve(),
remove: () => Promise.resolve(),
update: () => Promise.resolve(),
Expand Down

0 comments on commit 3fac248

Please sign in to comment.