Skip to content

Commit

Permalink
fix: Fix usage of Shaka without polyfills
Browse files Browse the repository at this point in the history
Revert "fix: Work around override of MediaCapabilities polyfill in Apple browsers (#3668)"

This reverts commit 31c0cd4.

Fixes #3843 (usage of Shaka without polyfills)
Re-opens #3530 (MediaCapabilities polyfill not working on Safari)

Change-Id: Ib5aff1b38759e1a39200332f3f07d3d6bd3bd2e1
  • Loading branch information
joeyparrish committed Jan 11, 2022
1 parent 15e2deb commit b330572
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 65 deletions.
16 changes: 0 additions & 16 deletions externs/media_capabilities.js

This file was deleted.

16 changes: 7 additions & 9 deletions lib/polyfill/media_capabilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,14 @@ shaka.polyfill.MediaCapabilities = class {
navigator.mediaCapabilities) {
shaka.log.debug(
'MediaCapabilities: Native mediaCapabilities support found.');
} else {
if (!navigator.mediaCapabilities) {
navigator.mediaCapabilities = /** @type {!MediaCapabilities} */ ({});
}
navigator.mediaCapabilities.decodingInfo =
shaka.polyfill.MediaCapabilities.decodingInfo_;
return;
}

if (!navigator.mediaCapabilities) {
navigator.mediaCapabilities = /** @type {!MediaCapabilities} */ ({});
}
// Workaround because in Apple browsers there is no way to overwrite
// MediaCapabilities.
window.shakaMediaCapabilities = navigator.mediaCapabilities;
navigator.mediaCapabilities.decodingInfo =
shaka.polyfill.MediaCapabilities.decodingInfo_;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions lib/util/stream_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ shaka.util.StreamUtils = class {
*/
static async filterManifestByMediaCapabilities(
manifest, usePersistentLicenses) {
goog.asserts.assert(window.shakaMediaCapabilities,
goog.asserts.assert(navigator.mediaCapabilities,
'MediaCapabilities should be valid.');

await shaka.util.StreamUtils.getDecodingInfosForVariants(
Expand Down Expand Up @@ -468,7 +468,7 @@ shaka.util.StreamUtils = class {
return;
}

const mediaCapabilities = window.shakaMediaCapabilities;
const mediaCapabilities = navigator.mediaCapabilities;

const operations = [];
const getVariantDecodingInfos = (async (variant, decodingConfig) => {
Expand Down
6 changes: 3 additions & 3 deletions test/media/drm_engine_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('DrmEngine', () => {
navigator.requestMediaKeySystemAccess;
const originalLogError = shaka.log.error;
const originalBatchTime = shaka.media.DrmEngine.KEY_STATUS_BATCH_TIME;
const originalDecodingInfo = window.shakaMediaCapabilities.decodingInfo;
const originalDecodingInfo = navigator.mediaCapabilities.decodingInfo;

/** @type {!jasmine.Spy} */
let decodingInfoSpy;
Expand Down Expand Up @@ -76,7 +76,7 @@ describe('DrmEngine', () => {

beforeEach(() => {
decodingInfoSpy = jasmine.createSpy('decodingInfo');
window.shakaMediaCapabilities.decodingInfo =
navigator.mediaCapabilities.decodingInfo =
shaka.test.Util.spyFunc(decodingInfoSpy);

logErrorSpy = jasmine.createSpy('shaka.log.error');
Expand Down Expand Up @@ -153,7 +153,7 @@ describe('DrmEngine', () => {

navigator.requestMediaKeySystemAccess =
originalRequestMediaKeySystemAccess;
window.shakaMediaCapabilities.decodingInfo = originalDecodingInfo;
navigator.mediaCapabilities.decodingInfo = originalDecodingInfo;
shaka.log.error = originalLogError;
});

Expand Down
10 changes: 5 additions & 5 deletions test/player_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('Player', () => {
const originalLogWarn = shaka.log.warning;
const originalLogAlwaysWarn = shaka.log.alwaysWarn;
const originalIsTypeSupported = window.MediaSource.isTypeSupported;
const originalDecodingInfo = window.shakaMediaCapabilities.decodingInfo;
const originalDecodingInfo = navigator.mediaCapabilities.decodingInfo;

const fakeManifestUri = 'fake-manifest-uri';
const fakeMimeType = 'application/test';
Expand Down Expand Up @@ -93,7 +93,7 @@ describe('Player', () => {
// Since this is not an integration test, we don't want MediaCapabilities to
// fail assertions based on browser support for types. Pretend that all
// video and audio types are supported.
window.shakaMediaCapabilities.decodingInfo = async (config) => {
navigator.mediaCapabilities.decodingInfo = async (config) => {
await Promise.resolve();
const videoType = config['video'] ?
config['video'].contentType.split('/')[0] : null;
Expand Down Expand Up @@ -177,7 +177,7 @@ describe('Player', () => {
shaka.log.alwaysWarn = originalLogAlwaysWarn;
window.MediaSource.isTypeSupported = originalIsTypeSupported;
shaka.media.ManifestParser.unregisterParserByMime(fakeMimeType);
window.shakaMediaCapabilities.decodingInfo = originalDecodingInfo;
navigator.mediaCapabilities.decodingInfo = originalDecodingInfo;
}
});

Expand Down Expand Up @@ -2463,7 +2463,7 @@ describe('Player', () => {
it('throws CONTENT_UNSUPPORTED_BY_BROWSER', async () => {
window.MediaSource.isTypeSupported = (mimeType) => false;

window.shakaMediaCapabilities.decodingInfo = async (config) => {
navigator.mediaCapabilities.decodingInfo = async (config) => {
await Promise.resolve();
return {supported: false};
};
Expand Down Expand Up @@ -2827,7 +2827,7 @@ describe('Player', () => {
});
});

window.shakaMediaCapabilities.decodingInfo = async (config) => {
navigator.mediaCapabilities.decodingInfo = async (config) => {
await Promise.resolve();
const videoType = config['video'] ? config['video'].contentType : '';
if (videoType.includes('video') &&
Expand Down
66 changes: 36 additions & 30 deletions test/util/stream_utils_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ describe('StreamUtils', () => {
/** @type {!jasmine.Spy} */
let decodingInfoSpy;

const originalDecodingInfo = window.shakaMediaCapabilities.decodingInfo;
const originalDecodingInfo = navigator.mediaCapabilities.decodingInfo;

beforeEach(() => {
decodingInfoSpy = jasmine.createSpy('decodingInfo');
});

afterEach(() => {
window.shakaMediaCapabilities.decodingInfo = originalDecodingInfo;
navigator.mediaCapabilities.decodingInfo = originalDecodingInfo;
});

describe('filterStreamsByLanguageAndRole', () => {
Expand Down Expand Up @@ -511,7 +511,7 @@ describe('StreamUtils', () => {
});

it('handles decodingInfo exception', async () => {
window.shakaMediaCapabilities.decodingInfo =
navigator.mediaCapabilities.decodingInfo =
shaka.test.Util.spyFunc(decodingInfoSpy);
// If decodingInfo() fails, setDecodingInfo should finish without throwing
// an exception, and the variant should have no decodingInfo result.
Expand Down Expand Up @@ -539,38 +539,44 @@ describe('StreamUtils', () => {
});

it('includes transferFunction in config when hdr', async () => {
window.shakaMediaCapabilities.decodingInfo =
shaka.test.Util.spyFunc(decodingInfoSpy);
const originalDecodingInfo = navigator.mediaCapabilities.decodingInfo;

manifest = shaka.test.ManifestGenerator.generate((manifest) => {
manifest.addVariant(0, (variant) => {
variant.addVideo(0, (stream) => {
stream.mime('video/mp4', 'avc1.640028');
stream.hdr = 'SDR';
try {
navigator.mediaCapabilities.decodingInfo =
shaka.test.Util.spyFunc(decodingInfoSpy);

manifest = shaka.test.ManifestGenerator.generate((manifest) => {
manifest.addVariant(0, (variant) => {
variant.addVideo(0, (stream) => {
stream.mime('video/mp4', 'avc1.640028');
stream.hdr = 'SDR';
});
});
});
manifest.addVariant(1, (variant) => {
variant.addVideo(1, (stream) => {
stream.mime('video/mp4', 'hvc1.2.4.L150.90');
stream.hdr = 'PQ';
manifest.addVariant(1, (variant) => {
variant.addVideo(1, (stream) => {
stream.mime('video/mp4', 'hvc1.2.4.L150.90');
stream.hdr = 'PQ';
});
});
});
manifest.addVariant(2, (variant) => {
variant.addVideo(2, (stream) => {
stream.mime('video/mp4', 'hvc1.2.4.L153.B0');
stream.hdr = 'HLG';
manifest.addVariant(2, (variant) => {
variant.addVideo(2, (stream) => {
stream.mime('video/mp4', 'hvc1.2.4.L153.B0');
stream.hdr = 'HLG';
});
});
});
});

await StreamUtils.getDecodingInfosForVariants(manifest.variants,
/* usePersistentLicenses= */ false, /* srcEquals= */ false);
expect(decodingInfoSpy.calls.argsFor(0)[0].video.transferFunction)
.toBe('srgb');
expect(decodingInfoSpy.calls.argsFor(1)[0].video.transferFunction)
.toBe('pq');
expect(decodingInfoSpy.calls.argsFor(2)[0].video.transferFunction)
.toBe('hlg');
await StreamUtils.getDecodingInfosForVariants(manifest.variants,
/* usePersistentLicenses= */ false, /* srcEquals= */ false);
expect(decodingInfoSpy.calls.argsFor(0)[0].video.transferFunction)
.toBe('srgb');
expect(decodingInfoSpy.calls.argsFor(1)[0].video.transferFunction)
.toBe('pq');
expect(decodingInfoSpy.calls.argsFor(2)[0].video.transferFunction)
.toBe('hlg');
} finally {
navigator.mediaCapabilities.decodingInfo = originalDecodingInfo;
}
});
});

Expand Down Expand Up @@ -861,7 +867,7 @@ describe('StreamUtils', () => {
});
});
});
window.shakaMediaCapabilities.decodingInfo =
navigator.mediaCapabilities.decodingInfo =
shaka.test.Util.spyFunc(decodingInfoSpy);
decodingInfoSpy.and.callFake((config) => {
const res = config.video.contentType.includes('notsmooth') ?
Expand Down

0 comments on commit b330572

Please sign in to comment.