Skip to content

Commit

Permalink
feat: event payloads
Browse files Browse the repository at this point in the history
  • Loading branch information
adrums86 committed Mar 26, 2024
1 parent 5820da7 commit 2dae82a
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 12 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -617,8 +617,10 @@ This event is triggered directly from the underlying `keystatuseschange` event,
When the key session is created, an event of type `keysessioncreated` will be triggered on the Video.js playback tech.

```
player.tech().on('keysessioncreated', function(event) {
// note that there is no event data for keysessioncreated
player.tech().on('keysessioncreated', function(keySession) {
// Event data:
// keySession: the mediaKeySession object
// https://www.w3.org/TR/encrypted-media/#mediakeysession-interface
});
```

Expand Down
38 changes: 31 additions & 7 deletions src/eme.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,17 @@ export const makeNewRequest = (player, requestOptions) => {
try {
const keySession = mediaKeys.createSession();

eventBus.trigger('keysessioncreated');
eventBus.trigger({
type: 'keysessioncreated',
keySession
});

player.on('dispose', () => {
keySession.close().then(() => {
eventBus.trigger('keysessionclosed');
eventBus.trigger({
type: 'keysessionclosed',
keySession
});
}).catch((error) => {
const metadata = {
errorType: videojs.Error.EMEFailedToCloseSession,
Expand All @@ -128,7 +134,7 @@ export const makeNewRequest = (player, requestOptions) => {
keySession.addEventListener('message', (event) => {
eventBus.trigger({
type: 'keymessage',
event
messageEvent: event
});
// all other types will be handled by keystatuseschange
if (event.messageType !== 'license-request' && event.messageType !== 'license-renewal') {
Expand All @@ -138,7 +144,10 @@ export const makeNewRequest = (player, requestOptions) => {
getLicense(options, event.message, contentId)
.then((license) => {
resolve(keySession.update(license).then(() => {
eventBus.trigger('keysessionupdated');
eventBus.trigger({
type: 'keysessionupdated',
keySession
});
}).catch((error) => {
const metadata = {
errorType: videojs.Error.EMEFailedToUpdateSessionWithReceivedLicenseKeys,
Expand All @@ -153,9 +162,18 @@ export const makeNewRequest = (player, requestOptions) => {
});
}, false);

keySession.addEventListener('keystatuseschange', (event) => {
const KEY_STATUSES_CHANGE = 'keystatuseschange';

keySession.addEventListener(KEY_STATUSES_CHANGE, (event) => {
let expired = false;

// Re-emit the keystatuseschange event with the entire keyStatusesMap
eventBus.trigger({
type: KEY_STATUSES_CHANGE,
keyStatuses: keySession.keyStatuses
});

// Keep 'keystatuschange' for backward compatibility.
// based on https://www.w3.org/TR/encrypted-media/#example-using-all-events
keySession.keyStatuses.forEach((status, keyId) => {
// Trigger an event so that outside listeners can take action if appropriate.
Expand Down Expand Up @@ -191,7 +209,10 @@ export const makeNewRequest = (player, requestOptions) => {
// session can be created.
videojs.log.debug('Session expired, closing the session.');
keySession.close().then(() => {
eventBus.trigger('keysessionclosed');
eventBus.trigger({
type: 'keysessionclosed',
keySession
});
removeSession(initData);
makeNewRequest(player, requestOptions);
}).catch((error) => {
Expand Down Expand Up @@ -504,7 +525,10 @@ export const standard5July2016 = ({
}).then(() => {
return keySystemAccess.createMediaKeys();
}).then((createdMediaKeys) => {
eventBus.trigger('keysystemaccesscomplete');
eventBus.trigger({
type: 'keysystemaccesscomplete',
mediaKeys: createdMediaKeys
});
return addPendingSessions({
player,
video,
Expand Down
14 changes: 13 additions & 1 deletion src/fairplay.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,18 @@ const addKey = ({video, contentId, initData, cert, options, getLicense, eventBus
return;
}

eventBus.trigger('keysessioncreated');
eventBus.trigger({
type: 'keysessioncreated',
keySession
});

keySession.contentId = contentId;

keySession.addEventListener('webkitkeymessage', (event) => {
eventBus.trigger({
type: 'keymessage',
messageEvent: event
});
getLicense(options, contentId, event.message, (err, license) => {
if (eventBus) {
eventBus.trigger('licenserequestattempted');
Expand All @@ -105,6 +112,11 @@ const addKey = ({video, contentId, initData, cert, options, getLicense, eventBus
}

keySession.update(new Uint8Array(license));

eventBus.trigger({
type: 'keysessionupdated',
keySession
});
});
});

Expand Down
14 changes: 13 additions & 1 deletion src/ms-prefixed.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ export const addKeyToSession = (options, session, event, eventBus, emeError) =>
}

session.update(key);

eventBus.trigger({
type: 'keysessionupdated',
keySession: session
});
});
return;
}
Expand Down Expand Up @@ -92,7 +97,10 @@ export const createSession = (video, initData, options, eventBus, emeError) => {
throw error;
}

eventBus.trigger('keysessioncreated');
eventBus.trigger({
type: 'keysessioncreated',
keySession: session
});

// Note that mskeymessage may not always be called for PlayReady:
//
Expand All @@ -104,6 +112,10 @@ export const createSession = (video, initData, options, eventBus, emeError) => {
// eslint-disable-next-line max-len
// @see [PlayReady License Acquisition]{@link https://msdn.microsoft.com/en-us/library/dn468979.aspx}
session.addEventListener('mskeymessage', (event) => {
eventBus.trigger({
type: 'keymessage',
messageEvent: event
});
addKeyToSession(options, session, event, eventBus, emeError);
});

Expand Down
3 changes: 2 additions & 1 deletion test/eme.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ QUnit.test('keystatuseschange triggers keystatuschange on eventBus for each key'
options: {},
getLicense() {},
removeSession() {},
eventBus
eventBus,
emeError() {}
});

assert.equal(mockSession.listeners.length, 2, 'added listeners');
Expand Down

0 comments on commit 2dae82a

Please sign in to comment.