From 2dae82a9b05d2e0a8eb4b363c6875454c569c5ed Mon Sep 17 00:00:00 2001 From: Adam Waldron Date: Mon, 25 Mar 2024 19:11:30 -0700 Subject: [PATCH] feat: event payloads --- README.md | 6 ++++-- src/eme.js | 38 +++++++++++++++++++++++++++++++------- src/fairplay.js | 14 +++++++++++++- src/ms-prefixed.js | 14 +++++++++++++- test/eme.test.js | 3 ++- 5 files changed, 63 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 0d990dd..1cd6979 100644 --- a/README.md +++ b/README.md @@ -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 }); ``` diff --git a/src/eme.js b/src/eme.js index 0dee1d8..66c0a6c 100644 --- a/src/eme.js +++ b/src/eme.js @@ -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, @@ -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') { @@ -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, @@ -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. @@ -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) => { @@ -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, diff --git a/src/fairplay.js b/src/fairplay.js index d85788e..3b56644 100644 --- a/src/fairplay.js +++ b/src/fairplay.js @@ -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'); @@ -105,6 +112,11 @@ const addKey = ({video, contentId, initData, cert, options, getLicense, eventBus } keySession.update(new Uint8Array(license)); + + eventBus.trigger({ + type: 'keysessionupdated', + keySession + }); }); }); diff --git a/src/ms-prefixed.js b/src/ms-prefixed.js index 201f336..bcd7366 100644 --- a/src/ms-prefixed.js +++ b/src/ms-prefixed.js @@ -33,6 +33,11 @@ export const addKeyToSession = (options, session, event, eventBus, emeError) => } session.update(key); + + eventBus.trigger({ + type: 'keysessionupdated', + keySession: session + }); }); return; } @@ -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: // @@ -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); }); diff --git a/test/eme.test.js b/test/eme.test.js index 5c424ff..f60874a 100644 --- a/test/eme.test.js +++ b/test/eme.test.js @@ -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');