Skip to content

Commit

Permalink
fix: Fallback to isTypeSupported when cast namespace is undefined (sh…
Browse files Browse the repository at this point in the history
…aka-project#5012)

Resolves shaka-project#5010

---------

Co-authored-by: Dan Sparacio <daniel.sparacio@cbsinteractive.com>
  • Loading branch information
littlespex and dsparacio committed Feb 28, 2023
1 parent 7da65a1 commit 50d0645
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 30 deletions.
10 changes: 2 additions & 8 deletions lib/polyfill/media_capabilities.js
Expand Up @@ -9,7 +9,6 @@ goog.provide('shaka.polyfill.MediaCapabilities');
goog.require('shaka.log');
goog.require('shaka.media.Capabilities');
goog.require('shaka.polyfill');
goog.require('shaka.util.Error');
goog.require('shaka.util.Platform');


Expand Down Expand Up @@ -247,13 +246,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 ' +
'shaka.media.Capabilities.isTypeSupported() for type support.');
return shaka.media.Capabilities.isTypeSupported(videoConfig.contentType);
Expand Down
43 changes: 21 additions & 22 deletions test/polyfill/media_capabilities_unit.js
Expand Up @@ -5,7 +5,6 @@
*/

describe('MediaCapabilities', () => {
const Util = shaka.test.Util;
const originalCast = window['cast'];
const originalVendor = navigator.vendor;
const originalUserAgent = navigator.userAgent;
Expand Down Expand Up @@ -181,30 +180,30 @@ describe('MediaCapabilities', () => {
.toHaveBeenCalledTimes(1);
});

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);
expect(window['MediaSource']['isTypeSupported']).toBeDefined();

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(supportMap.has(mockDecodingConfig.video.contentType))
.toBe(true);
expect(supportMap.has(mockDecodingConfig.audio.contentType))
.toBe(true);
});

it('falls back to isTypeSupported() when canDisplayType() missing',
async () => {
Expand Down

0 comments on commit 50d0645

Please sign in to comment.