Skip to content

Commit

Permalink
feat: add keysessioncreated event (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
gesinger committed Nov 18, 2020
1 parent 4fd7f8f commit d114979
Show file tree
Hide file tree
Showing 10 changed files with 206 additions and 43 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Maintenance Status: Stable
- [Events](#events)
- [`licenserequestattempted`](#licenserequestattempted)
- [`keystatuschange`](#keystatuschange)
- [`keysessioncreated`](#keysessioncreated)
- [License](#license)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->
Expand Down Expand Up @@ -552,6 +553,16 @@ player.tech(true).on('keystatuschange', function(event) {

This event is triggered directly from the underlying `keystatuseschange` event, so the statuses should correspond to [those listed in the spec](https://www.w3.org/TR/encrypted-media/#dom-mediakeystatus).

### `keysessioncreated`

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
});
```

## License

Apache License, Version 2.0. [View the license file](LICENSE)
Expand Down
2 changes: 2 additions & 0 deletions src/eme.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ export const makeNewRequest = (requestOptions) => {
} = requestOptions;
const keySession = mediaKeys.createSession();

eventBus.trigger('keysessioncreated');

return new Promise((resolve, reject) => {

keySession.addEventListener('message', (event) => {
Expand Down
2 changes: 2 additions & 0 deletions src/fairplay.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ const addKey = ({video, contentId, initData, cert, options, getLicense, eventBus
return;
}

eventBus.trigger('keysessioncreated');

keySession.contentId = contentId;

keySession.addEventListener('webkitkeymessage', (event) => {
Expand Down
2 changes: 2 additions & 0 deletions src/ms-prefixed.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ export const createSession = (video, initData, options, eventBus) => {
throw new Error('Could not create key session.');
}

eventBus.trigger('keysessioncreated');

// Note that mskeymessage may not always be called for PlayReady:
//
// "If initData contains a PlayReady object that contains an OnDemand header, only a
Expand Down
60 changes: 48 additions & 12 deletions test/eme.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
addPendingSessions,
getSupportedConfigurations
} from '../src/eme';
import { getMockEventBus } from './utils';
import sinon from 'sinon';

// mock session to make testing easier (so we can trigger events)
Expand Down Expand Up @@ -52,6 +53,10 @@ QUnit.test('keystatuseschange triggers keystatuschange on eventBus for each key'
const mockSession = getMockSession();
const eventBus = {
trigger: (event) => {
if (typeof event === 'string') {
return;
}

if (!callCount[event.keyId][event.status]) {
callCount[event.keyId][event.status] = 0;
}
Expand Down Expand Up @@ -279,7 +284,8 @@ QUnit.test('accepts a license URL as an option', function(assert) {
keySystems: {
'com.widevine.alpha': 'some-url'
}
}
},
eventBus: getMockEventBus()
}).catch((e) => {});

setTimeout(() => {
Expand Down Expand Up @@ -337,7 +343,8 @@ QUnit.test('accepts a license URL as property', function(assert) {
url: 'some-url'
}
}
}
},
eventBus: getMockEventBus()
}).catch((e) => {});

setTimeout(() => {
Expand Down Expand Up @@ -534,7 +541,8 @@ QUnit.test('errors when neither url nor getLicense is given', function(assert) {
standard5July2016({
video: {},
keySystemAccess,
options
options,
eventBus: getMockEventBus()
}).catch((err) => {
assert.equal(
err,
Expand Down Expand Up @@ -565,7 +573,8 @@ QUnit.test('rejects promise when getCertificate throws error', function(assert)
standard5July2016({
video: {},
keySystemAccess,
options
options,
eventBus: getMockEventBus()
}).catch((err) => {
assert.equal(err, 'error fetching certificate', 'correct error message');
done();
Expand All @@ -589,7 +598,8 @@ QUnit.test('rejects promise when createMediaKeys rejects', function(assert) {
standard5July2016({
video: {},
keySystemAccess,
options
options,
eventBus: getMockEventBus()
}).catch((err) => {
assert.equal(err, 'Failed to create and initialize a MediaKeys object',
'uses generic message');
Expand All @@ -615,7 +625,8 @@ QUnit.test('rejects promise when createMediaKeys rejects', function(assert) {
standard5July2016({
video: {},
keySystemAccess,
options
options,
eventBus: getMockEventBus()
}).catch((err) => {
assert.equal(err, 'failed creating mediaKeys', 'uses specific error when given');
done();
Expand Down Expand Up @@ -663,7 +674,8 @@ QUnit.test('rejects promise when addPendingSessions rejects', function(assert) {
standard5July2016({
video,
keySystemAccess,
options
options,
eventBus: getMockEventBus()
}).catch((err) => {
assert.equal(err, errMessage, testDescription);
done();
Expand Down Expand Up @@ -729,7 +741,8 @@ QUnit.test('getLicense not called for messageType that isnt license-request or l
standard5July2016({
video,
keySystemAccess,
options
options,
eventBus: getMockEventBus()
});
});

Expand Down Expand Up @@ -771,7 +784,8 @@ QUnit.test('getLicense promise rejection', function(assert) {
standard5July2016({
video,
keySystemAccess,
options
options,
eventBus: getMockEventBus()
}).catch((err) => {
assert.equal(err, 'error getting license', 'correct error message');
done();
Expand Down Expand Up @@ -870,7 +884,8 @@ QUnit.test('keySession.update promise rejection', function(assert) {
standard5July2016({
video,
keySystemAccess,
options
options,
eventBus: getMockEventBus()
}).catch((err) => {
assert.equal(err, 'keySession update failed', 'correct error message');
done();
Expand Down Expand Up @@ -911,7 +926,8 @@ QUnit.test('emeHeaders option sets headers on default license xhr request', func
emeHeaders: {
'Some-Header': 'some-header-value'
}
}
},
eventBus: getMockEventBus()
}).catch((e) => {});

setTimeout(() => {
Expand Down Expand Up @@ -977,7 +993,8 @@ QUnit.test('licenseHeaders keySystems property overrides emeHeaders value', func
emeHeaders: {
'Some-Header': 'lower-priority-header-value'
}
}
},
eventBus: getMockEventBus()
}).catch((e) => {});

setTimeout(() => {
Expand Down Expand Up @@ -1210,3 +1227,22 @@ QUnit.test('uses supportedConfigurations directly if provided', function(assert)
'used supportedConfigurations directly'
);
});

QUnit.test('makeNewRequest triggers keysessioncreated', function(assert) {
const done = assert.async();
const mockSession = getMockSession();

makeNewRequest({
mediaKeys: {
createSession: () => mockSession
},
eventBus: {
trigger: (eventName) => {
if (eventName === 'keysessioncreated') {
assert.ok(true, 'got a keysessioncreated event');
done();
}
}
}
});
});
79 changes: 73 additions & 6 deletions test/fairplay.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from '../src/fairplay';
import videojs from 'video.js';
import window from 'global/window';
import { getMockEventBus } from './utils';

QUnit.module('videojs-contrib-eme fairplay');

Expand Down Expand Up @@ -150,7 +151,7 @@ QUnit.test('error in getCertificate rejects promise', function(assert) {
}
};

fairplay({options: {keySystems}}).catch((err) => {
fairplay({options: {keySystems}, eventBus: getMockEventBus() }).catch((err) => {
assert.equal(err, 'error in getCertificate', 'message is good');
done();
});
Expand All @@ -171,7 +172,12 @@ QUnit.test('error in WebKitMediaKeys rejects promise', function(assert) {

keySystems[FAIRPLAY_KEY_SYSTEM] = {};

fairplay({video, initData, options: {keySystems}}).catch(err => {
fairplay({
video,
initData,
options: {keySystems},
eventBus: getMockEventBus()
}).catch(err => {
assert.equal(err, 'Could not create MediaKeys', 'message is good');
done();
});
Expand All @@ -192,7 +198,12 @@ QUnit.test('error in webkitSetMediaKeys rejects promise', function(assert) {

keySystems[FAIRPLAY_KEY_SYSTEM] = {};

fairplay({video, initData, options: {keySystems}}).catch(err => {
fairplay({
video,
initData,
options: {keySystems},
eventBus: getMockEventBus()
}).catch(err => {
assert.equal(err, 'Could not create MediaKeys', 'message is good');
done();
});
Expand All @@ -217,7 +228,12 @@ QUnit.test('error in webkitKeys.createSession rejects promise', function(assert)

keySystems[FAIRPLAY_KEY_SYSTEM] = {};

fairplay({video, initData, options: {keySystems}}).catch(err => {
fairplay({
video,
initData,
options: {keySystems},
eventBus: getMockEventBus()
}).catch(err => {
assert.equal(err, 'Could not create key session',
'message is good');
done();
Expand Down Expand Up @@ -253,13 +269,59 @@ QUnit.test('error in getLicense rejects promise', function(assert) {
}
};

fairplay({video, initData, options: {keySystems}}).catch(err => {
fairplay({
video,
initData,
options: {keySystems},
eventBus: getMockEventBus()
}).catch(err => {
assert.equal(err, 'error in getLicense', 'message is good');
done();
});

});

QUnit.test('keysessioncreated fired on key session created', function(assert) {
const keySystems = {};
const done = assert.async();
const initData = new Uint8Array([1, 2, 3, 4]).buffer;
let sessionCreated = false;
const video = {
webkitSetMediaKeys: () => {
video.webkitKeys = {
createSession: () => {
sessionCreated = true;
return {
addEventListener: () => {}
};
}
};
}
};
const eventBus = {
trigger: (event) => {
if (event === 'keysessioncreated') {
assert.ok(sessionCreated, 'keysessioncreated fired after session created');
done();
}
}
};

window.WebKitMediaKeys = () => {};

keySystems[FAIRPLAY_KEY_SYSTEM] = {
licenseUri: 'some-url',
certificateUri: 'some-other-url'
};

fairplay({
video,
initData,
options: { keySystems },
eventBus
});
});

QUnit.test('a webkitkeyerror rejects promise', function(assert) {
let keySession;
const keySystems = {};
Expand Down Expand Up @@ -294,7 +356,12 @@ QUnit.test('a webkitkeyerror rejects promise', function(assert) {
}
};

fairplay({video, initData, options: {keySystems}}).catch(err => {
fairplay({
video,
initData,
options: {keySystems},
eventBus: getMockEventBus()
}).catch(err => {
assert.equal(err, 'KeySession error: code 0, systemCode 1', 'message is good');
done();
});
Expand Down
Loading

0 comments on commit d114979

Please sign in to comment.