diff --git a/build/types/polyfill b/build/types/polyfill index eeacd0936..8afff665d 100644 --- a/build/types/polyfill +++ b/build/types/polyfill @@ -4,7 +4,6 @@ +../../lib/polyfill/indexed_db.js +../../lib/polyfill/input_event.js +../../lib/polyfill/mathround.js -+../../lib/polyfill/mediakeys.js +../../lib/polyfill/mediasource.js +../../lib/polyfill/patchedmediakeys_ms.js +../../lib/polyfill/patchedmediakeys_nop.js diff --git a/docs/tutorials/plugins.md b/docs/tutorials/plugins.md index fa55ef493..cd242973a 100644 --- a/docs/tutorials/plugins.md +++ b/docs/tutorials/plugins.md @@ -69,7 +69,10 @@ __Polyfills__ - prefixed video QoE metrics: {@linksource shaka.polyfill.VideoPlaybackQuality} - prefixed EME implementations for IE 11 and very old versions of embedded - Chrome/Chromium: {@linksource shaka.polyfill.MediaKeys} + Chrome/Chromium: + - {@linksource shaka.polyfill.PatchedMediaKeysMs} + - {@linksource shaka.polyfill.PatchedMediaKeysWebkit} + - {@linksource shaka.polyfill.PatchedMediaKeysNop} - variants of VTTCue and TextTrackCue constructors: {@linksource shaka.polyfill.VTTCue} diff --git a/lib/polyfill/all.js b/lib/polyfill/all.js index 28c76bc4d..62388280f 100644 --- a/lib/polyfill/all.js +++ b/lib/polyfill/all.js @@ -33,14 +33,14 @@ goog.provide('shaka.polyfill.register'); */ shaka.polyfill.installAll = function() { for (let i = 0; i < shaka.polyfill.polyfills_.length; ++i) { - shaka.polyfill.polyfills_[i](); + shaka.polyfill.polyfills_[i].callback(); } }; /** * Contains the polyfills that will be installed. - * @private {!Array.} + * @private {!Array.<{priority: number, callback: function()}>} */ shaka.polyfill.polyfills_ = []; @@ -49,8 +49,18 @@ shaka.polyfill.polyfills_ = []; * Registers a new polyfill to be installed. * * @param {function()} polyfill + * @param {number=} priority An optional number priority. Higher priorities + * will be executed before lower priority ones. Default is 0. * @export */ -shaka.polyfill.register = function(polyfill) { - shaka.polyfill.polyfills_.push(polyfill); +shaka.polyfill.register = function(polyfill, priority) { + priority = priority || 0; + const item = {priority: priority, callback: polyfill}; + for (let i = 0; i < shaka.polyfill.polyfills_.length; i++) { + if (shaka.polyfill.polyfills_[i].priority < priority) { + shaka.polyfill.polyfills_.splice(i, 0, item); + return; + } + } + shaka.polyfill.polyfills_.push(item); }; diff --git a/lib/polyfill/mediakeys.js b/lib/polyfill/mediakeys.js deleted file mode 100644 index be962d9d2..000000000 --- a/lib/polyfill/mediakeys.js +++ /dev/null @@ -1,69 +0,0 @@ -/** - * @license - * Copyright 2016 Google Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -goog.provide('shaka.polyfill.MediaKeys'); - -goog.require('shaka.log'); -goog.require('shaka.polyfill.PatchedMediaKeysMs'); -goog.require('shaka.polyfill.PatchedMediaKeysNop'); -goog.require('shaka.polyfill.PatchedMediaKeysWebkit'); -goog.require('shaka.polyfill.register'); - - -/** - * @namespace shaka.polyfill.MediaKeys - * - * @summary A polyfill to unify EME APIs across browser versions. - * - * The {@link https://w3c.github.io/encrypted-media/ EME spec} is still a - * work-in-progress. As such, we need to provide a consistent API to the Shaka - * Player. Until the spec is completely stable, the API provided by this - * polyfill may lag behind the latest spec developments. - */ - - -/** - * Install the polyfill if needed. - */ -shaka.polyfill.MediaKeys.install = function() { - shaka.log.debug('MediaKeys.install'); - - if (!window.HTMLVideoElement) { - // Avoid errors on very old browsers. - return; - } - - if (navigator.requestMediaKeySystemAccess && - MediaKeySystemAccess.prototype.getConfiguration) { - shaka.log.info('Using native EME as-is.'); - } else if (HTMLMediaElement.prototype.webkitGenerateKeyRequest) { - shaka.log.info('Using webkit-prefixed EME v0.1b'); - shaka.polyfill.PatchedMediaKeysWebkit.install('webkit'); - } else if (HTMLMediaElement.prototype.generateKeyRequest) { - shaka.log.info('Using nonprefixed EME v0.1b'); - shaka.polyfill.PatchedMediaKeysWebkit.install(''); - } else if (window.MSMediaKeys) { - shaka.log.info('Using ms-prefixed EME v20140218'); - shaka.polyfill.PatchedMediaKeysMs.install(); - } else { - shaka.log.info('EME not available.'); - shaka.polyfill.PatchedMediaKeysNop.install(); - } -}; - - -shaka.polyfill.register(shaka.polyfill.MediaKeys.install); diff --git a/lib/polyfill/patchedmediakeys_ms.js b/lib/polyfill/patchedmediakeys_ms.js index 54d6d4c64..96b7bd86e 100644 --- a/lib/polyfill/patchedmediakeys_ms.js +++ b/lib/polyfill/patchedmediakeys_ms.js @@ -19,6 +19,7 @@ goog.provide('shaka.polyfill.PatchedMediaKeysMs'); goog.require('goog.asserts'); goog.require('shaka.log'); +goog.require('shaka.polyfill.register'); goog.require('shaka.util.ArrayUtils'); goog.require('shaka.util.EventManager'); goog.require('shaka.util.FakeEvent'); @@ -29,12 +30,25 @@ goog.require('shaka.util.Uint8ArrayUtils'); /** - * Install a polyfill to implement {@link http://goo.gl/blgtZZ EME draft - * 12 March 2015} on top of ms-prefixed + * @namespace shaka.polyfill.PatchedMediaKeysMs + * + * @summary A polyfill to implement + * {@link http://goo.gl/blgtZZ EME draft 12 March 2015} + * on top of ms-prefixed * {@link http://www.w3.org/TR/2014/WD-encrypted-media-20140218/ EME v20140218}. */ + + +/** + * Installs the polyfill if needed. + */ shaka.polyfill.PatchedMediaKeysMs.install = function() { - shaka.log.debug('PatchedMediaKeysMs.install'); + if (!window.HTMLVideoElement || !window.MSMediaKeys || + (navigator.requestMediaKeySystemAccess && + MediaKeySystemAccess.prototype.getConfiguration)) { + return; + } + shaka.log.info('Using ms-prefixed EME v20140218'); // Alias const PatchedMediaKeysMs = shaka.polyfill.PatchedMediaKeysMs; @@ -826,3 +840,6 @@ shaka.polyfill.PatchedMediaKeysMs.MediaKeyStatusMap.prototype. values = function() { goog.asserts.assert(false, 'Not used! Provided only for compiler.'); }; + + +shaka.polyfill.register(shaka.polyfill.PatchedMediaKeysMs.install); diff --git a/lib/polyfill/patchedmediakeys_nop.js b/lib/polyfill/patchedmediakeys_nop.js index ffc56854f..db0c98331 100644 --- a/lib/polyfill/patchedmediakeys_nop.js +++ b/lib/polyfill/patchedmediakeys_nop.js @@ -19,14 +19,28 @@ goog.provide('shaka.polyfill.PatchedMediaKeysNop'); goog.require('goog.asserts'); goog.require('shaka.log'); +goog.require('shaka.polyfill.register'); /** - * Install a polyfill to stub out {@link http://goo.gl/blgtZZ EME draft - * 12 March 2015} on browsers without EME. All methods will fail. + * @namespace shaka.polyfill.PatchedMediaKeysNop + * + * @summary A polyfill to stub out + * {@link http://goo.gl/blgtZZ EME draft 12 March 2015} on browsers without EME. + * All methods will fail. + */ + + +/** + * Installs the polyfill if needed. */ shaka.polyfill.PatchedMediaKeysNop.install = function() { - shaka.log.debug('PatchedMediaKeysNop.install'); + if (!window.HTMLVideoElement || + (navigator.requestMediaKeySystemAccess && + MediaKeySystemAccess.prototype.getConfiguration)) { + return; + } + shaka.log.info('EME not available.'); // Alias. const PatchedMediaKeysNop = shaka.polyfill.PatchedMediaKeysNop; @@ -135,3 +149,6 @@ shaka.polyfill.PatchedMediaKeysNop.MediaKeySystemAccess.prototype. shaka.polyfill.PatchedMediaKeysNop.MediaKeySystemAccess.prototype. keySystem; + +// A low priority ensures this is the last and acts as a fallback. +shaka.polyfill.register(shaka.polyfill.PatchedMediaKeysNop.install, -10); diff --git a/lib/polyfill/patchedmediakeys_webkit.js b/lib/polyfill/patchedmediakeys_webkit.js index a5527ae74..65ae15367 100644 --- a/lib/polyfill/patchedmediakeys_webkit.js +++ b/lib/polyfill/patchedmediakeys_webkit.js @@ -19,6 +19,7 @@ goog.provide('shaka.polyfill.PatchedMediaKeysWebkit'); goog.require('goog.asserts'); goog.require('shaka.log'); +goog.require('shaka.polyfill.register'); goog.require('shaka.util.EventManager'); goog.require('shaka.util.FakeEvent'); goog.require('shaka.util.FakeEventTarget'); @@ -27,6 +28,15 @@ goog.require('shaka.util.StringUtils'); goog.require('shaka.util.Uint8ArrayUtils'); +/** + * @namespace shaka.polyfill.PatchedMediaKeysWebkit + * + * @summary A polyfill to implement + * {@link http://goo.gl/blgtZZ EME draft 12 March 2015} on top of + * webkit-prefixed {@link http://goo.gl/FSpoAo EME v0.1b}. + */ + + /** * Store api prefix. * @@ -36,19 +46,26 @@ shaka.polyfill.PatchedMediaKeysWebkit.prefix_ = ''; /** - * Install a polyfill to implement {@link http://goo.gl/blgtZZ EME draft - * 12 March 2015} on top of webkit-prefixed - * {@link http://goo.gl/FSpoAo EME v0.1b}. - * - * @param {string} prefix + * Installs the polyfill if needed. */ -shaka.polyfill.PatchedMediaKeysWebkit.install = function(prefix) { - shaka.log.debug('PatchedMediaKeysWebkit.install'); - +shaka.polyfill.PatchedMediaKeysWebkit.install = function() { // Alias. const PatchedMediaKeysWebkit = shaka.polyfill.PatchedMediaKeysWebkit; - PatchedMediaKeysWebkit.prefix_ = prefix; - let prefixApi = PatchedMediaKeysWebkit.prefixApi_; + const prefixApi = PatchedMediaKeysWebkit.prefixApi_; + + if (!window.HTMLVideoElement || + (navigator.requestMediaKeySystemAccess && + MediaKeySystemAccess.prototype.getConfiguration)) { + return; + } + if (HTMLMediaElement.prototype.webkitGenerateKeyRequest) { + shaka.log.info('Using webkit-prefixed EME v0.1b'); + PatchedMediaKeysWebkit.prefix_ = 'webkit'; + } else if (HTMLMediaElement.prototype.generateKeyRequest) { + shaka.log.info('Using nonprefixed EME v0.1b'); + } else { + return; + } goog.asserts.assert( HTMLMediaElement.prototype[prefixApi('generateKeyRequest')], @@ -1024,3 +1041,6 @@ shaka.polyfill.PatchedMediaKeysWebkit.MediaKeyStatusMap.prototype. values = function() { goog.asserts.assert(false, 'Not used! Provided only for compiler.'); }; + + +shaka.polyfill.register(shaka.polyfill.PatchedMediaKeysWebkit.install); diff --git a/shaka-player.uncompiled.js b/shaka-player.uncompiled.js index c9888cd6c..79d5dcdf9 100644 --- a/shaka-player.uncompiled.js +++ b/shaka-player.uncompiled.js @@ -44,8 +44,10 @@ goog.require('shaka.polyfill.Fullscreen'); goog.require('shaka.polyfill.IndexedDB'); goog.require('shaka.polyfill.InputEvent'); goog.require('shaka.polyfill.MathRound'); -goog.require('shaka.polyfill.MediaKeys'); goog.require('shaka.polyfill.MediaSource'); +goog.require('shaka.polyfill.PatchedMediaKeysMs'); +goog.require('shaka.polyfill.PatchedMediaKeysNop'); +goog.require('shaka.polyfill.PatchedMediaKeysWebkit'); goog.require('shaka.polyfill.VTTCue'); goog.require('shaka.polyfill.VideoPlayPromise'); goog.require('shaka.polyfill.VideoPlaybackQuality');