Browse Source

refactor: simplify setupEmeOptions and add tests (#869)

Co-authored-by: Brandon Casey <2381475+brandonocasey@users.noreply.github.com>
pull/872/head
Garrett Singer 5 years ago
committed by GitHub
parent
commit
e3921ed7d2
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 53
      src/videojs-http-streaming.js
  2. 146
      test/videojs-http-streaming.test.js

53
src/videojs-http-streaming.js

@ -172,27 +172,31 @@ const emeKeySystems = (keySystemOptions, videoPlaylist, audioPlaylist) => {
return videojs.mergeOptions(keySystemOptions, keySystemContentTypes);
};
const setupEmeOptions = (vhsHandler) => {
const player = vhsHandler.player_;
if (player.eme) {
const audioPlaylistLoader = vhsHandler.masterPlaylistController_.mediaTypes_.AUDIO.activePlaylistLoader;
const sourceOptions = emeKeySystems(
vhsHandler.source_.keySystems,
vhsHandler.playlists.media(),
audioPlaylistLoader && audioPlaylistLoader.media()
);
const setupEmeOptions = ({
player,
sourceKeySystems,
media,
audioMedia
}) => {
if (!player.eme) {
return;
}
if (sourceOptions) {
player.currentSource().keySystems = sourceOptions;
const sourceOptions = emeKeySystems(sourceKeySystems, media, audioMedia);
// works around https://bugs.chromium.org/p/chromium/issues/detail?id=895449
// in non-IE11 browsers. In IE11 this is too early to initialize media keys
if (!(videojs.browser.IE_VERSION === 11) && player.eme.initializeMediaKeys) {
player.eme.initializeMediaKeys();
}
}
if (!sourceOptions) {
return;
}
player.currentSource().keySystems = sourceOptions;
// works around https://bugs.chromium.org/p/chromium/issues/detail?id=895449
// in non-IE11 browsers. In IE11 this is too early to initialize media keys
if (videojs.browser.IE_VERSION === 11 || !player.eme.initializeMediaKeys) {
return;
}
player.eme.initializeMediaKeys();
};
const getVhsLocalStorage = () => {
@ -727,7 +731,15 @@ class VhsHandler extends Component {
});
this.masterPlaylistController_.sourceUpdater_.on('ready', () => {
setupEmeOptions(this);
const audioPlaylistLoader =
this.masterPlaylistController_.mediaTypes_.AUDIO.activePlaylistLoader;
setupEmeOptions({
player: this.player_,
sourceKeySystems: this.source_.keySystems,
media: this.playlists.media(),
audioMedia: audioPlaylistLoader && audioPlaylistLoader.media()
});
});
// the bandwidth of the primary segment loader is our best
@ -990,5 +1002,6 @@ export {
VhsSourceHandler,
emeKeySystems,
simpleTypeFromSourceType,
expandDataUri
expandDataUri,
setupEmeOptions
};

146
test/videojs-http-streaming.test.js

@ -36,7 +36,8 @@ import {
Vhs,
emeKeySystems,
LOCAL_STORAGE_KEY,
expandDataUri
expandDataUri,
setupEmeOptions
} from '../src/videojs-http-streaming';
import window from 'global/window';
// we need this so the plugin registers itself
@ -5723,3 +5724,146 @@ QUnit.test('expandDataUri requires comma to parse', function(assert) {
'did not parse when no comma after data URI'
);
});
QUnit.module('setupEmeOptions', {
beforeEach() {
this.origBrowser = videojs.browser;
// IE11 is a special case and should be tested separately
videojs.browser = videojs.mergeOptions(videojs.browser, { IE_VERSION: null });
},
afterEach() {
videojs.browser = this.origBrowser;
}
});
QUnit.test('no error if no eme', function(assert) {
const player = {};
const sourceKeySystems = {};
const media = {};
const audioMedia = {};
setupEmeOptions({ player, sourceKeySystems, media, audioMedia });
assert.ok(true, 'no exception');
});
QUnit.test('no initialize calls if no source key systems', function(assert) {
let numInitializeCalls = 0;
const player = { eme: { initializeMediaKeys: () => numInitializeCalls++ } };
const sourceKeySystems = null;
const media = {
attributes: { CODECS: 'avc1.4d400d,mp4a.40.2' },
contentProtection: { 'com.widevine.alpha': { pssh: new Uint8Array() } }
};
const audioMedia = null;
setupEmeOptions({ player, sourceKeySystems, media, audioMedia });
assert.equal(numInitializeCalls, 0, 'no initialize calls');
});
QUnit.test('initializes for muxed playlist', function(assert) {
let numInitializeCalls = 0;
const player = {
eme: { initializeMediaKeys: () => numInitializeCalls++ },
currentSource: () => {
return {};
}
};
const sourceKeySystems = {
'com.widevine.alpha': {
url: 'license-url'
}
};
const media = {
attributes: { CODECS: 'avc1.4d400d,mp4a.40.2' },
contentProtection: { 'com.widevine.alpha': { pssh: new Uint8Array() } }
};
const audioMedia = null;
setupEmeOptions({ player, sourceKeySystems, media, audioMedia });
assert.equal(numInitializeCalls, 1, 'one initialize call');
});
QUnit.test('initializes for demuxed playlist', function(assert) {
let numInitializeCalls = 0;
const player = {
eme: { initializeMediaKeys: () => numInitializeCalls++ },
currentSource: () => {
return {};
}
};
const sourceKeySystems = {
'com.widevine.alpha': {
url: 'license-url'
}
};
const media = {
attributes: { CODECS: 'avc1.4d400d,mp4a.40.2' },
contentProtection: { 'com.widevine.alpha': { pssh: new Uint8Array() } }
};
const audioMedia = {
attributes: {},
contentProtection: { 'com.widevine.alpha': { pssh: new Uint8Array() } }
};
setupEmeOptions({ player, sourceKeySystems, media, audioMedia });
assert.equal(numInitializeCalls, 1, 'one initialize call');
});
QUnit.test('does not initialize if IE11', function(assert) {
videojs.browser.IE_VERSION = 11;
let numInitializeCalls = 0;
const player = {
eme: { initializeMediaKeys: () => numInitializeCalls++ },
currentSource: () => {
return {};
}
};
const sourceKeySystems = {
'com.widevine.alpha': {
url: 'license-url'
}
};
const media = {
attributes: { CODECS: 'avc1.4d400d,mp4a.40.2' },
contentProtection: { 'com.widevine.alpha': { pssh: new Uint8Array() } }
};
const audioMedia = {
attributes: {},
contentProtection: { 'com.widevine.alpha': { pssh: new Uint8Array() } }
};
setupEmeOptions({ player, sourceKeySystems, media, audioMedia });
assert.equal(numInitializeCalls, 0, 'no initialize calls');
});
QUnit.test('only initializes once even for different pssh values', function(assert) {
let numInitializeCalls = 0;
const player = {
eme: { initializeMediaKeys: () => numInitializeCalls++ },
currentSource: () => {
return {};
}
};
const sourceKeySystems = {
'com.widevine.alpha': {
url: 'license-url'
}
};
const media = {
attributes: { CODECS: 'avc1.4d400d,mp4a.40.2' },
contentProtection: { 'com.widevine.alpha': { pssh: new Uint8Array([0]) } }
};
const audioMedia = {
attributes: {},
contentProtection: { 'com.widevine.alpha': { pssh: new Uint8Array([1]) } }
};
setupEmeOptions({ player, sourceKeySystems, media, audioMedia });
assert.equal(numInitializeCalls, 1, 'one initialize call');
});
Loading…
Cancel
Save