Skip to content

Commit

Permalink
Simplified new CDT() call logic conditional statements.
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianDomingo committed Nov 22, 2022
1 parent 1dd0722 commit 715061f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 31 deletions.
28 changes: 13 additions & 15 deletions lib/polyfill/media_capabilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,30 +105,28 @@ shaka.polyfill.MediaCapabilities = class {
return res;
}
// Use 'MediaSource.isTypeSupported' to check if the stream is supported.
// Cast platforms will additionally check canDisplayType(), which
// accepts extended MIME type parameters.
// See: https://github.com/shaka-project/shaka-player/issues/4726
if (videoConfig) {
const contentType = videoConfig.contentType;
let isSupported = MediaSource.isTypeSupported(contentType);
if (shaka.util.Platform.isChromecast()) {
// Cast platforms will additionally check canDisplayType(), which
// accepts extended MIME type parameters.
if (shaka.util.Platform.isChromecast() &&
shaka.polyfill.MediaCapabilities.canCastDisplayType_(
{contentType})) {
// There will be at most 2 calls:
// - The first call determines the stability of
// the API by validating a contentType input limited only to MIME type
// and codecs is identical to MediaSource.isTypeSupported().
// - If the same result is observed, a second call will be executed
// containing resolution, frame rate, and transfer function support.
// See: https://github.com/shaka-project/shaka-player/issues/4726
if (isSupported ===
shaka.polyfill.MediaCapabilities.canCastDisplayType_({
contentType})) {
isSupported = shaka.polyfill.MediaCapabilities.canCastDisplayType_({
contentType,
width: videoConfig.width,
height: videoConfig.height,
frameRate: videoConfig.framerate,
transferFunction: videoConfig.transferFunction,
});
}
isSupported = shaka.polyfill.MediaCapabilities.canCastDisplayType_({
contentType,
width: videoConfig.width,
height: videoConfig.height,
frameRate: videoConfig.framerate,
transferFunction: videoConfig.transferFunction,
});
}
if (!isSupported) {
return res;
Expand Down
37 changes: 21 additions & 16 deletions test/polyfill/media_capabilities_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

describe('MediaCapabilities', () => {
const Util = shaka.test.Util;
const originalCast = window['cast'];
const originalVendor = navigator.vendor;
const originalUserAgent = navigator.userAgent;
const originalRequestMediaKeySystemAccess =
navigator.requestMediaKeySystemAccess;
const originalMediaCapabilities = navigator.mediaCapabilities;
const originalCast = window['cast'];

/** @type {MediaDecodingConfiguration} */
let mockDecodingConfig;
Expand Down Expand Up @@ -202,13 +202,14 @@ describe('MediaCapabilities', () => {

// 1 (during install()) + 1 (for video config check).
expect(isChromecastSpy).toHaveBeenCalledTimes(2);
// Called for decodingConfig.audio. This is never reached because of the
// error throw.
expect(isTypeSupportedSpy).not.toHaveBeenCalled();
// Called for decodingConfig.video. decodingConfig.audio is never reached
// due to the error thrown.
expect(isTypeSupportedSpy).toHaveBeenCalledTimes(1);
});

it('should use cast.__platform__.canDisplayType for "supported" field ' +
'when platform is Cast', async () => {
let canDisplayTypeCnt = 0;
// We're using quotes to access window.cast because the compiler
// knows about lots of Cast-specific APIs we aren't mocking. We
// don't need this mock strictly type-checked.
Expand All @@ -226,13 +227,18 @@ describe('MediaCapabilities', () => {
mockDecodingConfig.video.contentType =
'video/mp4; codecs="hev1.2.4.L153.B0"';
mockCanDisplayType.and.callFake((type) => {
expect(type).toBe(
'video/mp4; ' +
'codecs="hev1.2.4.L153.B0"; ' +
'width=512; ' +
'height=288; ' +
'framerate=23.976023976023978; ' +
'eotf=smpte2084');
if (canDisplayTypeCnt === 0) {
expect(type).toBe('video/mp4; codecs="hev1.2.4.L153.B0"');
} else if (canDisplayTypeCnt === 1) {
expect(type).toBe(
'video/mp4; ' +
'codecs="hev1.2.4.L153.B0"; ' +
'width=512; ' +
'height=288; ' +
'framerate=23.976023976023978; ' +
'eotf=smpte2084');
}
canDisplayTypeCnt++;
return true;
});

Expand All @@ -241,11 +247,10 @@ describe('MediaCapabilities', () => {

// 1 (during install()) + 1 (for video config check).
expect(isChromecastSpy).toHaveBeenCalledTimes(2);
// Called once for mockDecodingConfig.audio. Resolution, frame rate, and
// EOTF aren't applicable for audio, so isTypeSupported() is sufficient.
expect(isTypeSupportedSpy).toHaveBeenCalledTimes(1);
// Called once for mockDecodingConfig.video.
expect(mockCanDisplayType).toHaveBeenCalledTimes(1);
// 1 (mockDecodingConfig.video) + 1 (mockDecodingConfig.audio).
expect(isTypeSupportedSpy).toHaveBeenCalledTimes(2);
// Called twice for mockDecodingConfig.video.
expect(mockCanDisplayType).toHaveBeenCalledTimes(2);
});
});
});

0 comments on commit 715061f

Please sign in to comment.