Skip to content

Commit

Permalink
fix: Fix MediaCapabilities polyfill on Safari
Browse files Browse the repository at this point in the history
Fixes shaka-project#3696
Fixes shaka-project#3530

Change-Id: I2f2e248c9001e10013eb4b03af6b9ef49f28dc6c
  • Loading branch information
joeyparrish committed Jan 11, 2022
1 parent dfc44cb commit 0201f2b
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions lib/polyfill/media_capabilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ shaka.polyfill.MediaCapabilities = class {
* @export
*/
static install() {
shaka.log.debug('MediaCapabilities: install');

// Since MediaCapabilities is not fully supported on Chromecast yet, we
// should always install polyfill for Chromecast.
// TODO: re-evaluate MediaCapabilities in the future versions of Chromecast.
Expand All @@ -40,17 +38,26 @@ shaka.polyfill.MediaCapabilities = class {
// TODO: re-evaluate MediaCapabilities in the future versions of PS5
// Browsers.
if (!shaka.util.Platform.isChromecast() &&
!shaka.util.Platform.isApple() &&
!shaka.util.Platform.isPS5() &&
navigator.mediaCapabilities) {
shaka.log.debug(
!shaka.util.Platform.isApple() &&
!shaka.util.Platform.isPS5() &&
navigator.mediaCapabilities) {
shaka.log.info(
'MediaCapabilities: Native mediaCapabilities support found.');
return;
}

shaka.log.info('MediaCapabilities: install');

if (!navigator.mediaCapabilities) {
navigator.mediaCapabilities = /** @type {!MediaCapabilities} */ ({});
}

// Keep the patched MediaCapabilities object from being garbage-collected in
// Safari.
// See https://github.com/google/shaka-player/issues/3696#issuecomment-1009472718
shaka.polyfill.MediaCapabilities.originalMcap =
navigator.mediaCapabilities;

navigator.mediaCapabilities.decodingInfo =
shaka.polyfill.MediaCapabilities.decodingInfo_;
}
Expand Down Expand Up @@ -180,6 +187,16 @@ shaka.polyfill.MediaCapabilities = class {
}
};

/**
* A copy of the MediaCapabilities instance, to prevent Safari from
* garbage-collecting the polyfilled method on it. We make it public and export
* it to ensure that it is not stripped out by the compiler.
*
* @type {MediaCapabilities}
* @export
*/
shaka.polyfill.MediaCapabilities.originalMcap = null;

// Install at a lower priority than MediaSource polyfill, so that we have
// MediaSource available first.
shaka.polyfill.register(shaka.polyfill.MediaCapabilities.install, -1);

0 comments on commit 0201f2b

Please sign in to comment.