Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cast): Use cast platform APIs in MediaCapabilties polyfill #4727

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
aa973af
Initial fix with temp debug logs.
JulianDomingo Nov 17, 2022
0ee3554
Removed console debug logs after verifying eotf param on a 4K HDR Rok…
JulianDomingo Nov 18, 2022
4873f27
Updated comment on smpte2048 check.
JulianDomingo Nov 18, 2022
2d56c99
Merge branch 'main' into 4726-account-for-extended-mime-type-paramete…
JulianDomingo Nov 19, 2022
ef09c30
Added existence check for transferFunction parameter.
JulianDomingo Nov 19, 2022
51a96ea
Handled review comments (iter #0) and added unit tests. Also correcte…
JulianDomingo Nov 19, 2022
9588678
Fixed unit test linting errors.
JulianDomingo Nov 19, 2022
b60d59a
Restored cast namespace after polyfill test executions.
JulianDomingo Nov 19, 2022
bf81981
Use window.cast instead of cast for namespace checking.
JulianDomingo Nov 19, 2022
279b029
Sanity check to confirm issue with CDT() support on Selenium lab test…
JulianDomingo Nov 22, 2022
1dd0722
Updated canDisplayType() checks to guard against instable implementat…
JulianDomingo Nov 22, 2022
6a8cf51
Fall back to MediaSource.isTypeSupported() if cast APIs aren't availa…
JulianDomingo Nov 22, 2022
d0701ea
Removed window.cast to trigger CAST_API_UNAVAILABLE error for lab chr…
JulianDomingo Nov 23, 2022
720fe11
Verifying lab tests fail without check for cast.__platform__ and cast…
JulianDomingo Nov 23, 2022
8465a99
Re-instated deleted code from previous commit, which prevented cast l…
JulianDomingo Nov 23, 2022
be1065a
Handling PR review comments.
JulianDomingo Nov 29, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions lib/polyfill/media_capabilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ 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 @@ -111,11 +112,11 @@ shaka.polyfill.MediaCapabilities = class {
const contentType = videoConfig.contentType;
let isSupported;
if (shaka.util.Platform.isChromecast()) {
isSupported = shaka.polyfill.MediaCapabilities.canDisplayType_({
isSupported = shaka.polyfill.MediaCapabilities.canCastDisplayType_({
contentType,
width: videoConfig.width,
height: videoConfig.height,
frameRate: videoConfig.frameRate,
frameRate: videoConfig.framerate,
transferFunction: videoConfig.transferFunction,
});
} else {
Expand Down Expand Up @@ -236,14 +237,14 @@ shaka.polyfill.MediaCapabilities = class {

/**
* Checks if the given media parameters of the video or audio streams are
* supported by the platform.
* supported by the Cast platform.
* @param {{
* contentType: string,
* width: (number|undefined),
* height: (number|undefined),
* frameRate: (number|undefined),
* transferFunction: (string|undefined)
* }} options canDisplayType() options.
* }} options canCastDisplayType() options.
* contentType: A valid MIME type and (optionally) a codecs parameter.
* width: Describes the stream horizontal resolution in pixels.
* height: Describes the stream vertical resolution in pixels.
Expand All @@ -253,15 +254,20 @@ shaka.polyfill.MediaCapabilities = class {
* @return {boolean} `true` when the stream can be displayed on a Cast device.
* @private
*/
static canDisplayType_({
static canCastDisplayType_({
contentType,
width = undefined,
height = undefined,
frameRate = undefined,
transferFunction = undefined,
}) {
if (!cast.__platform__) {
return true;
if (!(window.cast && cast.__platform__ &&
cast.__platform__.canDisplayType)) {
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_UNEXPECTED_PLATFORM);
}
let displayType = contentType;
if (width && height) {
Expand All @@ -270,7 +276,7 @@ shaka.polyfill.MediaCapabilities = class {
if (frameRate) {
displayType += `; framerate=${frameRate}`;
}
if (transferFunction && transferFunction.toLowerCase() === 'pq') {
if (transferFunction === 'pq') {
// A "PQ" transfer function indicates this is an HDR-capable stream;
// "smpte2084" is the published standard. We need to inform the platform
// this query is specifically for HDR.
Expand Down
7 changes: 7 additions & 0 deletions lib/util/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,13 @@ shaka.util.Error.Code = {
*/
'CAST_RECEIVER_APP_UNAVAILABLE': 8006,

/**
* The Cast platform namespace was expected to be available, but wasn't.
* <br> This may be caused by Shaka failing to identify the correct platform
* through the userAgent string.
*/
'CAST_UNEXPECTED_PLATFORM': 8007,


// RETIRED: CAST_RECEIVER_APP_ID_MISSING': 8007,

Expand Down
75 changes: 75 additions & 0 deletions test/polyfill/media_capabilities_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@
*/

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

/** @type {MediaDecodingConfiguration} */
let mockDecodingConfig;
/** @type {!jasmine.Spy} */
let mockCanDisplayType;

beforeAll(() => {
Object.defineProperty(window['navigator'],
Expand Down Expand Up @@ -64,9 +69,13 @@ describe('MediaCapabilities', () => {
},
};
shaka.polyfill.MediaCapabilities.memoizedMediaKeySystemAccessRequests_ = {};

mockCanDisplayType = jasmine.createSpy('canDisplayType');
mockCanDisplayType.and.returnValue(false);
});

afterAll(() => {
window['cast'] = originalCast;
Object.defineProperty(window['navigator'],
'userAgent', {value: originalUserAgent});
Object.defineProperty(window['navigator'],
Expand Down Expand Up @@ -172,5 +181,71 @@ describe('MediaCapabilities', () => {
expect(requestKeySystemAccessSpy)
.toHaveBeenCalledTimes(1);
});

it('fails when the cast namespace is not available', async () => {
// Mock Shaka throwing an error from identifying an unexpected, non-Cast
// platform when performing Cast logic.
const isChromecastSpy =
spyOn(shaka['util']['Platform'],
'isChromecast').and.returnValue(true);
JulianDomingo marked this conversation as resolved.
Show resolved Hide resolved
const expected = Util.jasmineError(new shaka.util.Error(
shaka.util.Error.Severity.CRITICAL,
shaka.util.Error.Category.CAST,
shaka.util.Error.Code.CAST_UNEXPECTED_PLATFORM));
const isTypeSupportedSpy =
spyOn(window['MediaSource'], 'isTypeSupported').and.returnValue(true);

shaka.polyfill.MediaCapabilities.install();
await expectAsync(
navigator.mediaCapabilities.decodingInfo(mockDecodingConfig))
.toBeRejectedWith(expected);

// 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();
});

it('should use cast.__platform__.canDisplayType for "supported" field ' +
'when platform is Cast', async () => {
// 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.
window['cast'] = {
__platform__: {canDisplayType: mockCanDisplayType},
};
const isChromecastSpy =
spyOn(shaka['util']['Platform'],
'isChromecast').and.returnValue(true);
const isTypeSupportedSpy =
spyOn(window['MediaSource'], 'isTypeSupported').and.returnValue(true);

// Tests an HDR stream's extended MIME type is correctly provided.
mockDecodingConfig.video.transferFunction = 'pq';
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; ' +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks brittle. I don't know if we can rely on this exact amount of detail in converting a number to a string on all devices. Can you make the framerate 24 for this test instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, forgot to consider precision here.

'eotf=smpte2084');
return true;
});

shaka.polyfill.MediaCapabilities.install();
await navigator.mediaCapabilities.decodingInfo(mockDecodingConfig);

// 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);
});
});
});