From 141f0e384ebe9d31b5df899ef2c4c5d4217a31c9 Mon Sep 17 00:00:00 2001 From: Casey Occhialini <1508707+littlespex@users.noreply.github.com> Date: Tue, 28 Feb 2023 13:50:53 -0800 Subject: [PATCH] fix: Fallback to isTypeSupported when cast namespace is undefined (#5012) Resolves #5010 --------- Co-authored-by: Dan Sparacio --- lib/polyfill/media_capabilities.js | 10 ++---- test/polyfill/media_capabilities_unit.js | 42 +++++++++++------------- 2 files changed, 22 insertions(+), 30 deletions(-) diff --git a/lib/polyfill/media_capabilities.js b/lib/polyfill/media_capabilities.js index 780df20806..b45cc63a85 100644 --- a/lib/polyfill/media_capabilities.js +++ b/lib/polyfill/media_capabilities.js @@ -8,7 +8,6 @@ goog.provide('shaka.polyfill.MediaCapabilities'); goog.require('shaka.log'); goog.require('shaka.polyfill'); -goog.require('shaka.util.Error'); goog.require('shaka.util.Platform'); @@ -212,13 +211,8 @@ shaka.polyfill.MediaCapabilities = class { * @private */ static canCastDisplayType_(videoConfig) { - if (!(window.cast)) { - shaka.log.error('Expected cast namespace to be available!'); - throw new shaka.util.Error( - shaka.util.Error.Severity.CRITICAL, - shaka.util.Error.Category.CAST, - shaka.util.Error.Code.CAST_API_UNAVAILABLE); - } else if (!(cast.__platform__ && cast.__platform__.canDisplayType)) { + if (!(window.cast && + cast.__platform__ && cast.__platform__.canDisplayType)) { shaka.log.warning('Expected cast APIs to be available! Falling back to ' + 'MediaSource.isTypeSupported() for type support.'); return MediaSource.isTypeSupported(videoConfig.contentType); diff --git a/test/polyfill/media_capabilities_unit.js b/test/polyfill/media_capabilities_unit.js index 01afd990ae..20419fe5aa 100644 --- a/test/polyfill/media_capabilities_unit.js +++ b/test/polyfill/media_capabilities_unit.js @@ -5,7 +5,6 @@ */ describe('MediaCapabilities', () => { - const Util = shaka.test.Util; const originalCast = window['cast']; const originalVendor = navigator.vendor; const originalUserAgent = navigator.userAgent; @@ -168,30 +167,29 @@ describe('MediaCapabilities', () => { expect(result.keySystemAccess).toEqual(mockResult); }); - it('throws when the cast namespace is not available', async () => { - // Temporarily remove window.cast to trigger error. It's restored after - // every test. - delete window['cast']; + it('falls back to isTypeSupported() when cast namespace is not available', + async () => { + // Temporarily remove window.cast to trigger error. It's restored + // after every test. + delete window['cast']; - const isChromecastSpy = - spyOn(shaka['util']['Platform'], - 'isChromecast').and.returnValue(true); - const expected = Util.jasmineError(new shaka.util.Error( - shaka.util.Error.Severity.CRITICAL, - shaka.util.Error.Category.CAST, - shaka.util.Error.Code.CAST_API_UNAVAILABLE)); - const isTypeSupportedSpy = - spyOn(window['MediaSource'], 'isTypeSupported').and.returnValue(true); + const isChromecastSpy = + spyOn(shaka['util']['Platform'], + 'isChromecast').and.returnValue(true); + const isTypeSupportedSpy = + spyOn(window['MediaSource'], 'isTypeSupported') + .and.returnValue(true); - shaka.polyfill.MediaCapabilities.install(); - await expectAsync( - navigator.mediaCapabilities.decodingInfo(mockDecodingConfig)) - .toBeRejectedWith(expected); + shaka.polyfill.MediaCapabilities.install(); + await navigator.mediaCapabilities.decodingInfo(mockDecodingConfig); - expect(isTypeSupportedSpy).not.toHaveBeenCalled(); - // 1 (during install()) + 1 (for video config check). - expect(isChromecastSpy).toHaveBeenCalledTimes(2); - }); + expect(mockCanDisplayType).not.toHaveBeenCalled(); + // 1 (during install()) + 1 (for video config check). + expect(isChromecastSpy).toHaveBeenCalledTimes(2); + // 1 (fallback in canCastDisplayType()) + + // 1 (mockDecodingConfig.audio). + expect(isTypeSupportedSpy).toHaveBeenCalledTimes(2); + }); it('falls back to isTypeSupported() when canDisplayType() missing', async () => {