Skip to content

Commit

Permalink
fix: try to re-request key if the session expired (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonocasey committed Sep 15, 2020
1 parent c15d1ca commit 20d6adc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
20 changes: 11 additions & 9 deletions src/eme.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,16 @@ export const getSupportedKeySystem = (keySystems) => {
return promise;
};

export const makeNewRequest = ({
mediaKeys,
initDataType,
initData,
options,
getLicense,
removeSession,
eventBus
}) => {
export const makeNewRequest = (requestOptions) => {
const {
mediaKeys,
initDataType,
initData,
options,
getLicense,
removeSession,
eventBus
} = requestOptions;
const keySession = mediaKeys.createSession();

return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -140,6 +141,7 @@ export const makeNewRequest = ({
// videojs.log.debug('Session expired, closing the session.');
keySession.close().then(() => {
removeSession(initData);
makeNewRequest(requestOptions);
});
}
}, false);
Expand Down
12 changes: 10 additions & 2 deletions test/eme.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ QUnit.test('keystatuseschange triggers keystatuschange on eventBus for each key'

});

QUnit.test('keystatuseschange with expired key closes session', function(assert) {
QUnit.test('keystatuseschange with expired key closes and recreates session', function(assert) {
const removeSessionCalls = [];
// once the eme module gets the removeSession function, the session argument is already
// bound to the function (note that it's a custom session maintained by the plugin, not
Expand All @@ -150,10 +150,15 @@ QUnit.test('keystatuseschange with expired key closes session', function(assert)
const eventBus = {
trigger: (name) => {}
};
let creates = 0;

makeNewRequest({
mediaKeys: {
createSession: () => mockSession
createSession: () => {
creates++;

return mockSession;
}
},
initDataType: '',
initData,
Expand All @@ -163,6 +168,7 @@ QUnit.test('keystatuseschange with expired key closes session', function(assert)
eventBus
});

assert.equal(creates, 1, 'created session');
assert.equal(mockSession.listeners.length, 2, 'added listeners');
assert.equal(mockSession.listeners[1].type,
'keystatuseschange',
Expand Down Expand Up @@ -190,6 +196,8 @@ QUnit.test('keystatuseschange with expired key closes session', function(assert)
// synchronously
assert.equal(removeSessionCalls.length, 1, 'called remove session');
assert.equal(removeSessionCalls[0], initData, 'called to remove session with initData');

assert.equal(creates, 2, 'created another session');
});

QUnit.test('keystatuseschange with internal-error logs a warning', function(assert) {
Expand Down

0 comments on commit 20d6adc

Please sign in to comment.