Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: simplify setupEmeOptions and add tests #869

Merged
merged 2 commits into from
Jul 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 33 additions & 20 deletions src/videojs-http-streaming.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -990,5 +1002,6 @@ export {
VhsSourceHandler,
emeKeySystems,
simpleTypeFromSourceType,
expandDataUri
expandDataUri,
setupEmeOptions
};
146 changes: 145 additions & 1 deletion test/videojs-http-streaming.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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');
});