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

feat: Caching and other efficiency improvements for mcap polyfill #4708

Merged
merged 9 commits into from
Nov 18, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Charter Communications Inc <*@charter.com>
Code It <*@code-it.fr>
Damien Deis <[email protected]>
Dany L'Hébreux <[email protected]>
Dave Nicholas <[email protected]>
Dl Dador <[email protected]>
Edgeware AB <*@edgeware.tv>
Enson Choy <[email protected]>
Expand Down
52 changes: 48 additions & 4 deletions lib/polyfill/media_capabilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ shaka.polyfill.MediaCapabilities = class {
* @export
*/
static install() {
shaka.polyfill.MediaCapabilities.memoizedMediaKeySystemAccessRequests_ = {};
joeyparrish marked this conversation as resolved.
Show resolved Hide resolved
// Since MediaCapabilities implementation is buggy on the Chromecast
// platform (see https://github.com/shaka-project/shaka-player/issues/4569),
// we should always install polyfills on all Chromecast models.
Expand Down Expand Up @@ -96,7 +97,7 @@ shaka.polyfill.MediaCapabilities = class {
return res;
}

if (mediaDecodingConfig.type == 'media-source') {
if (mediaDecodingConfig.type === 'media-source') {
joeyparrish marked this conversation as resolved.
Show resolved Hide resolved
if (!shaka.util.Platform.supportsMediaSource()) {
return res;
}
Expand All @@ -116,7 +117,7 @@ shaka.polyfill.MediaCapabilities = class {
return res;
}
}
} else if (mediaDecodingConfig.type == 'file') {
} else if (mediaDecodingConfig.type === 'file') {
joeyparrish marked this conversation as resolved.
Show resolved Hide resolved
if (mediaDecodingConfig['video']) {
const contentType = mediaDecodingConfig['video'].contentType;
const isSupported = shaka.util.Platform.supportsMediaType(contentType);
Expand Down Expand Up @@ -185,10 +186,25 @@ shaka.polyfill.MediaCapabilities = class {
mediaKeySystemConfig.videoCapabilities = videoCapabilities;
}

const cacheKey = shaka.polyfill.MediaCapabilities
.generateKeySystemCacheKey(
mediaDecodingConfig.video.contentType,
mediaDecodingConfig.audio.contentType,
mediaDecodingConfig.keySystemConfiguration.keySystem);

let keySystemAccess;
try {
keySystemAccess = await navigator.requestMediaKeySystemAccess(
mediaCapkeySystemConfig.keySystem, [mediaKeySystemConfig]);
if (cacheKey in shaka.polyfill.MediaCapabilities
.memoizedMediaKeySystemAccessRequests_) {
keySystemAccess = shaka.polyfill.MediaCapabilities
.memoizedMediaKeySystemAccessRequests_[cacheKey];
} else {
keySystemAccess = await navigator.requestMediaKeySystemAccess(
mediaCapkeySystemConfig.keySystem, [mediaKeySystemConfig]);
shaka.polyfill.MediaCapabilities
.memoizedMediaKeySystemAccessRequests_[cacheKey] =
keySystemAccess;
}
} catch (e) {
shaka.log.info('navigator.requestMediaKeySystemAccess failed.');
}
Expand All @@ -201,6 +217,34 @@ shaka.polyfill.MediaCapabilities = class {

return res;
}

/**
* @param {!string} videoCodec
* @param {!string} audioCodec
* @param {!string} keySystem
* @return {!string}
* @static
*/
static generateKeySystemCacheKey(videoCodec, audioCodec, keySystem) {
joeyparrish marked this conversation as resolved.
Show resolved Hide resolved
return `${videoCodec}#${audioCodec}#${keySystem}`;
}

/**
* @param {!string} videoCodec
* @param {!string} audioCodec
* @param {!string} keySystem
* @param {!MediaKeySystemAccess} result
* @static
*/
static appendKeySystemCache(videoCodec, audioCodec, keySystem, result) {
joeyparrish marked this conversation as resolved.
Show resolved Hide resolved
shaka.polyfill.MediaCapabilities.memoizedMediaKeySystemAccessRequests_[
shaka.polyfill.MediaCapabilities.generateKeySystemCacheKey(
videoCodec,
audioCodec,
keySystem,
)
] = result;
}
};

/**
Expand Down
5 changes: 2 additions & 3 deletions lib/util/stream_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,6 @@ shaka.util.StreamUtils = class {

const mediaCapabilities = navigator.mediaCapabilities;

const operations = [];
const getVariantDecodingInfos = (async (variant, decodingConfig) => {
try {
const result = await mediaCapabilities.decodingInfo(decodingConfig);
Expand All @@ -532,10 +531,10 @@ shaka.util.StreamUtils = class {
variant, usePersistentLicenses, srcEquals);

for (const config of decodingConfigs) {
operations.push(getVariantDecodingInfos(variant, config));
// eslint-disable-next-line no-await-in-loop
await getVariantDecodingInfos(variant, config);
joeyparrish marked this conversation as resolved.
Show resolved Hide resolved
}
}
await Promise.all(operations);
}


Expand Down
197 changes: 197 additions & 0 deletions test/polyfill/media_capabilities_unit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
/*! @license
* Shaka Player
* Copyright 2016 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/

describe('MediaCapabilities', () => {
const originalVendor = navigator.vendor;
const originalUserAgent = navigator.userAgent;
const originalMediaCapabilities = navigator.mediaCapabilities;
/** @type {MediaDecodingConfiguration} */
let mockDecodingConfig;

beforeAll(() => {
Object.defineProperty(window['navigator'],
'userAgent', {
value: 'unknown', configurable: true,
writable: true,
});
Object.defineProperty(window['navigator'],
'vendor', {
value: 'unknown', configurable: true,
writable: true,
});
Object.defineProperty(window['navigator'],
'mediaCapabilities', {
value: undefined, configurable: true,
writable: true,
});
});

beforeEach(() => {
mockDecodingConfig = {
audio: {
bitrate: 100891,
channels: 2,
contentType: 'audio/mp4; codecs="mp4a.40.2"',
samplerate: 48000,
spatialRendering: false,
},
keySystemConfiguration: {
audio: {robustness: 'SW_SECURE_CRYPTO'},
distinctiveIdentifier: 'optional',
initDataType: 'cenc',
keySystem: 'com.widevine.alpha',
persistentState: 'optional',
sessionTypes: ['temporary'],
video: {robustness: 'SW_SECURE_CRYPTO'},
},
type: 'media-source',
video: {
bitrate: 349265,
contentType: 'video/mp4; codecs="avc1.4D4015"',
framerate: 23.976023976023978,
height: 288,
width: 512,
},
};
});

afterAll(() => {
Object.defineProperty(window['navigator'],
'userAgent', {value: originalUserAgent});
Object.defineProperty(window['navigator'],
'vendor', {value: originalVendor});
Object.defineProperty(window['navigator'],
'mediaCapabilities', {value: originalMediaCapabilities});
});

describe('install', () => {
it('should define decoding info method', () => {
shaka.polyfill.MediaCapabilities.install();

expect(navigator.mediaCapabilities.decodingInfo).toBeDefined();
});
});

describe('decodingInfo', () => {
it('should check codec support when MediaDecodingConfiguration ' +
'when type is "media-source"', () => {
joeyparrish marked this conversation as resolved.
Show resolved Hide resolved
const isTypeSupportedSpy =
spyOn(window['MediaSource'], 'isTypeSupported').and.returnValue(true);
shaka.polyfill.MediaCapabilities.install();
navigator.mediaCapabilities.decodingInfo(mockDecodingConfig);

expect(isTypeSupportedSpy).toHaveBeenCalledTimes(2);
expect(isTypeSupportedSpy).toHaveBeenCalledWith(
mockDecodingConfig.video.contentType,
);
expect(isTypeSupportedSpy).toHaveBeenCalledWith(
mockDecodingConfig.audio.contentType,
);
});
it('should check codec support when MediaDecodingConfiguration ' +
joeyparrish marked this conversation as resolved.
Show resolved Hide resolved
'when type is "file"', () => {
const supportsMediaTypeSpy =
spyOn(shaka['util']['Platform'],
'supportsMediaType').and.returnValue(true);
mockDecodingConfig.type = 'file';
shaka.polyfill.MediaCapabilities.install();
navigator.mediaCapabilities.decodingInfo(mockDecodingConfig);

expect(supportsMediaTypeSpy).toHaveBeenCalledTimes(2);
expect(supportsMediaTypeSpy).toHaveBeenCalledWith(
mockDecodingConfig.video.contentType,
);
expect(supportsMediaTypeSpy).toHaveBeenCalledWith(
mockDecodingConfig.audio.contentType,
);
});
it('should check MediaKeySystem when keySystemConfiguration is present',
async () => {
const mockResult = {mockKeySystemAccess: 'mockKeySystemAccess'};
spyOn(window['MediaSource'], 'isTypeSupported').and.returnValue(true);
const requestKeySystemAccessSpy =
spyOn(window['navigator'],
'requestMediaKeySystemAccess').and.returnValue(mockResult);

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

expect(requestKeySystemAccessSpy).toHaveBeenCalledWith(
'com.widevine.alpha',
[{
audioCapabilities: [
{
robustness: 'SW_SECURE_CRYPTO',
contentType: 'audio/mp4; codecs="mp4a.40.2"',
},
],
distinctiveIdentifier: 'optional',
initDataTypes: ['cenc'],
persistentState: 'optional',
sessionTypes: ['temporary'],
videoCapabilities: [{
robustness: 'SW_SECURE_CRYPTO',
contentType: 'video/mp4; codecs="avc1.4D4015"',
}],
}],
);
expect(result.keySystemAccess).toEqual(mockResult);
});
});

describe('generateKeySystemCacheKey', () => {
it('should create key as expected', () => {
const videoCodec = 'video/mp4; codecs="avc1.4D4015"';
const audioCodec = 'audio/mp4; codecs="mp4a.40.2"';
const keySystem = 'com.widevine.alpha';

const key = shaka.polyfill.MediaCapabilities.generateKeySystemCacheKey(
videoCodec,
audioCodec,
keySystem,
);

expect(key).toBe('video/mp4; codecs="avc1.4D4015"' +
'#audio/mp4; codecs="mp4a.40.2"#com.widevine.alpha');
});
});

describe('appendKeySystemCache', () => {
it('should populate cache and not call requestMediaKeySystemAccess', () => {
const videoCodec = 'video/mp4; codecs="avc1.4D4015"';
const audioCodec = 'audio/mp4; codecs="mp4a.40.2"';
const keySystem = 'com.widevine.alpha';

const key = shaka.polyfill.MediaCapabilities.generateKeySystemCacheKey(
videoCodec,
audioCodec,
keySystem,
);

const keySystemAccessResult = /** @type {!MediaKeySystemAccess} */ ({
keySystem: 'com.widevine.alpha',
});

spyOn(window['MediaSource'], 'isTypeSupported').and.returnValue(true);
const requestKeySystemAccessSpy =
spyOn(window['navigator'], 'requestMediaKeySystemAccess');

shaka.polyfill.MediaCapabilities.install();
shaka.polyfill.MediaCapabilities.appendKeySystemCache(
videoCodec,
audioCodec,
keySystem, keySystemAccessResult);
navigator.mediaCapabilities.decodingInfo(mockDecodingConfig);

expect(requestKeySystemAccessSpy)
.not.toHaveBeenCalled();
expect(shaka.polyfill.MediaCapabilities
.memoizedMediaKeySystemAccessRequests_[key])
.toBe(keySystemAccessResult);
});
});
});