From b330572a51159825187f770ab4513d92f86bbd62 Mon Sep 17 00:00:00 2001 From: Joey Parrish Date: Mon, 10 Jan 2022 16:45:56 -0800 Subject: [PATCH] fix: Fix usage of Shaka without polyfills Revert "fix: Work around override of MediaCapabilities polyfill in Apple browsers (#3668)" This reverts commit 31c0cd4b8ca0daec1cd7760bffe2bd5376a7f5ab. Fixes #3843 (usage of Shaka without polyfills) Re-opens #3530 (MediaCapabilities polyfill not working on Safari) Change-Id: Ib5aff1b38759e1a39200332f3f07d3d6bd3bd2e1 --- externs/media_capabilities.js | 16 -------- lib/polyfill/media_capabilities.js | 16 ++++---- lib/util/stream_utils.js | 4 +- test/media/drm_engine_unit.js | 6 +-- test/player_unit.js | 10 ++--- test/util/stream_utils_unit.js | 66 ++++++++++++++++-------------- 6 files changed, 53 insertions(+), 65 deletions(-) delete mode 100644 externs/media_capabilities.js diff --git a/externs/media_capabilities.js b/externs/media_capabilities.js deleted file mode 100644 index e450542dc9..0000000000 --- a/externs/media_capabilities.js +++ /dev/null @@ -1,16 +0,0 @@ -/*! @license - * Shaka Player - * Copyright 2016 Google LLC - * SPDX-License-Identifier: Apache-2.0 - */ - -/** - * @fileoverview Externs for Shaka MediaCapabilities which were missing in the - * Closure compiler. - * - * @externs - */ - - -/** @type {MediaCapabilities} */ -window.shakaMediaCapabilities; diff --git a/lib/polyfill/media_capabilities.js b/lib/polyfill/media_capabilities.js index 85dc1c06b6..91904f5d85 100644 --- a/lib/polyfill/media_capabilities.js +++ b/lib/polyfill/media_capabilities.js @@ -45,16 +45,14 @@ shaka.polyfill.MediaCapabilities = class { navigator.mediaCapabilities) { shaka.log.debug( 'MediaCapabilities: Native mediaCapabilities support found.'); - } else { - if (!navigator.mediaCapabilities) { - navigator.mediaCapabilities = /** @type {!MediaCapabilities} */ ({}); - } - navigator.mediaCapabilities.decodingInfo = - shaka.polyfill.MediaCapabilities.decodingInfo_; + return; + } + + if (!navigator.mediaCapabilities) { + navigator.mediaCapabilities = /** @type {!MediaCapabilities} */ ({}); } - // Workaround because in Apple browsers there is no way to overwrite - // MediaCapabilities. - window.shakaMediaCapabilities = navigator.mediaCapabilities; + navigator.mediaCapabilities.decodingInfo = + shaka.polyfill.MediaCapabilities.decodingInfo_; } /** diff --git a/lib/util/stream_utils.js b/lib/util/stream_utils.js index f8358d68ff..ca32535840 100644 --- a/lib/util/stream_utils.js +++ b/lib/util/stream_utils.js @@ -419,7 +419,7 @@ shaka.util.StreamUtils = class { */ static async filterManifestByMediaCapabilities( manifest, usePersistentLicenses) { - goog.asserts.assert(window.shakaMediaCapabilities, + goog.asserts.assert(navigator.mediaCapabilities, 'MediaCapabilities should be valid.'); await shaka.util.StreamUtils.getDecodingInfosForVariants( @@ -468,7 +468,7 @@ shaka.util.StreamUtils = class { return; } - const mediaCapabilities = window.shakaMediaCapabilities; + const mediaCapabilities = navigator.mediaCapabilities; const operations = []; const getVariantDecodingInfos = (async (variant, decodingConfig) => { diff --git a/test/media/drm_engine_unit.js b/test/media/drm_engine_unit.js index c6208b651d..9b98f8314c 100644 --- a/test/media/drm_engine_unit.js +++ b/test/media/drm_engine_unit.js @@ -31,7 +31,7 @@ describe('DrmEngine', () => { navigator.requestMediaKeySystemAccess; const originalLogError = shaka.log.error; const originalBatchTime = shaka.media.DrmEngine.KEY_STATUS_BATCH_TIME; - const originalDecodingInfo = window.shakaMediaCapabilities.decodingInfo; + const originalDecodingInfo = navigator.mediaCapabilities.decodingInfo; /** @type {!jasmine.Spy} */ let decodingInfoSpy; @@ -76,7 +76,7 @@ describe('DrmEngine', () => { beforeEach(() => { decodingInfoSpy = jasmine.createSpy('decodingInfo'); - window.shakaMediaCapabilities.decodingInfo = + navigator.mediaCapabilities.decodingInfo = shaka.test.Util.spyFunc(decodingInfoSpy); logErrorSpy = jasmine.createSpy('shaka.log.error'); @@ -153,7 +153,7 @@ describe('DrmEngine', () => { navigator.requestMediaKeySystemAccess = originalRequestMediaKeySystemAccess; - window.shakaMediaCapabilities.decodingInfo = originalDecodingInfo; + navigator.mediaCapabilities.decodingInfo = originalDecodingInfo; shaka.log.error = originalLogError; }); diff --git a/test/player_unit.js b/test/player_unit.js index 5d83ce3233..b4775be97d 100644 --- a/test/player_unit.js +++ b/test/player_unit.js @@ -36,7 +36,7 @@ describe('Player', () => { const originalLogWarn = shaka.log.warning; const originalLogAlwaysWarn = shaka.log.alwaysWarn; const originalIsTypeSupported = window.MediaSource.isTypeSupported; - const originalDecodingInfo = window.shakaMediaCapabilities.decodingInfo; + const originalDecodingInfo = navigator.mediaCapabilities.decodingInfo; const fakeManifestUri = 'fake-manifest-uri'; const fakeMimeType = 'application/test'; @@ -93,7 +93,7 @@ describe('Player', () => { // Since this is not an integration test, we don't want MediaCapabilities to // fail assertions based on browser support for types. Pretend that all // video and audio types are supported. - window.shakaMediaCapabilities.decodingInfo = async (config) => { + navigator.mediaCapabilities.decodingInfo = async (config) => { await Promise.resolve(); const videoType = config['video'] ? config['video'].contentType.split('/')[0] : null; @@ -177,7 +177,7 @@ describe('Player', () => { shaka.log.alwaysWarn = originalLogAlwaysWarn; window.MediaSource.isTypeSupported = originalIsTypeSupported; shaka.media.ManifestParser.unregisterParserByMime(fakeMimeType); - window.shakaMediaCapabilities.decodingInfo = originalDecodingInfo; + navigator.mediaCapabilities.decodingInfo = originalDecodingInfo; } }); @@ -2463,7 +2463,7 @@ describe('Player', () => { it('throws CONTENT_UNSUPPORTED_BY_BROWSER', async () => { window.MediaSource.isTypeSupported = (mimeType) => false; - window.shakaMediaCapabilities.decodingInfo = async (config) => { + navigator.mediaCapabilities.decodingInfo = async (config) => { await Promise.resolve(); return {supported: false}; }; @@ -2827,7 +2827,7 @@ describe('Player', () => { }); }); - window.shakaMediaCapabilities.decodingInfo = async (config) => { + navigator.mediaCapabilities.decodingInfo = async (config) => { await Promise.resolve(); const videoType = config['video'] ? config['video'].contentType : ''; if (videoType.includes('video') && diff --git a/test/util/stream_utils_unit.js b/test/util/stream_utils_unit.js index 28968ae26c..542d4dd82d 100644 --- a/test/util/stream_utils_unit.js +++ b/test/util/stream_utils_unit.js @@ -16,14 +16,14 @@ describe('StreamUtils', () => { /** @type {!jasmine.Spy} */ let decodingInfoSpy; - const originalDecodingInfo = window.shakaMediaCapabilities.decodingInfo; + const originalDecodingInfo = navigator.mediaCapabilities.decodingInfo; beforeEach(() => { decodingInfoSpy = jasmine.createSpy('decodingInfo'); }); afterEach(() => { - window.shakaMediaCapabilities.decodingInfo = originalDecodingInfo; + navigator.mediaCapabilities.decodingInfo = originalDecodingInfo; }); describe('filterStreamsByLanguageAndRole', () => { @@ -511,7 +511,7 @@ describe('StreamUtils', () => { }); it('handles decodingInfo exception', async () => { - window.shakaMediaCapabilities.decodingInfo = + navigator.mediaCapabilities.decodingInfo = shaka.test.Util.spyFunc(decodingInfoSpy); // If decodingInfo() fails, setDecodingInfo should finish without throwing // an exception, and the variant should have no decodingInfo result. @@ -539,38 +539,44 @@ describe('StreamUtils', () => { }); it('includes transferFunction in config when hdr', async () => { - window.shakaMediaCapabilities.decodingInfo = - shaka.test.Util.spyFunc(decodingInfoSpy); + const originalDecodingInfo = navigator.mediaCapabilities.decodingInfo; - manifest = shaka.test.ManifestGenerator.generate((manifest) => { - manifest.addVariant(0, (variant) => { - variant.addVideo(0, (stream) => { - stream.mime('video/mp4', 'avc1.640028'); - stream.hdr = 'SDR'; + try { + navigator.mediaCapabilities.decodingInfo = + shaka.test.Util.spyFunc(decodingInfoSpy); + + manifest = shaka.test.ManifestGenerator.generate((manifest) => { + manifest.addVariant(0, (variant) => { + variant.addVideo(0, (stream) => { + stream.mime('video/mp4', 'avc1.640028'); + stream.hdr = 'SDR'; + }); }); - }); - manifest.addVariant(1, (variant) => { - variant.addVideo(1, (stream) => { - stream.mime('video/mp4', 'hvc1.2.4.L150.90'); - stream.hdr = 'PQ'; + manifest.addVariant(1, (variant) => { + variant.addVideo(1, (stream) => { + stream.mime('video/mp4', 'hvc1.2.4.L150.90'); + stream.hdr = 'PQ'; + }); }); - }); - manifest.addVariant(2, (variant) => { - variant.addVideo(2, (stream) => { - stream.mime('video/mp4', 'hvc1.2.4.L153.B0'); - stream.hdr = 'HLG'; + manifest.addVariant(2, (variant) => { + variant.addVideo(2, (stream) => { + stream.mime('video/mp4', 'hvc1.2.4.L153.B0'); + stream.hdr = 'HLG'; + }); }); }); - }); - await StreamUtils.getDecodingInfosForVariants(manifest.variants, - /* usePersistentLicenses= */ false, /* srcEquals= */ false); - expect(decodingInfoSpy.calls.argsFor(0)[0].video.transferFunction) - .toBe('srgb'); - expect(decodingInfoSpy.calls.argsFor(1)[0].video.transferFunction) - .toBe('pq'); - expect(decodingInfoSpy.calls.argsFor(2)[0].video.transferFunction) - .toBe('hlg'); + await StreamUtils.getDecodingInfosForVariants(manifest.variants, + /* usePersistentLicenses= */ false, /* srcEquals= */ false); + expect(decodingInfoSpy.calls.argsFor(0)[0].video.transferFunction) + .toBe('srgb'); + expect(decodingInfoSpy.calls.argsFor(1)[0].video.transferFunction) + .toBe('pq'); + expect(decodingInfoSpy.calls.argsFor(2)[0].video.transferFunction) + .toBe('hlg'); + } finally { + navigator.mediaCapabilities.decodingInfo = originalDecodingInfo; + } }); }); @@ -861,7 +867,7 @@ describe('StreamUtils', () => { }); }); }); - window.shakaMediaCapabilities.decodingInfo = + navigator.mediaCapabilities.decodingInfo = shaka.test.Util.spyFunc(decodingInfoSpy); decodingInfoSpy.and.callFake((config) => { const res = config.video.contentType.includes('notsmooth') ?