Skip to content

Commit

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

Resolves #5010

---------

Co-authored-by: Dan Sparacio <[email protected]>
  • Loading branch information
2 people authored and joeyparrish committed Mar 1, 2023
1 parent 78c7abe commit 141f0e3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 30 deletions.
10 changes: 2 additions & 8 deletions lib/polyfill/media_capabilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');


Expand Down Expand Up @@ -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);
Expand Down
42 changes: 20 additions & 22 deletions test/polyfill/media_capabilities_unit.js
Original file line number Diff line number Diff line change
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 @@ -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 () => {
Expand Down

0 comments on commit 141f0e3

Please sign in to comment.