Skip to content

Commit

Permalink
fix: Avoid duplicate calls to decodingInfo() (#43)
Browse files Browse the repository at this point in the history
* refactor: Call 'navigator.requestMediaKeySystemAccess'

* Respond to feedback

* Fix linter errors

* Rework comments to clarify flow
  • Loading branch information
joeyparrish authored Jun 7, 2022
1 parent 0b816a7 commit fafd1dd
Showing 1 changed file with 43 additions and 10 deletions.
53 changes: 43 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,18 +301,31 @@ class McEncryptionSchemePolyfill {
return capabilities;
}

// If we land here, the browser does _not_ support the mediaKeySystemAccess
// field or the encryptionScheme field. So we install another patch to
// check the mediaKeySystemAccess or encryptionScheme field in future calls.
// If we land here, either the browser does not support the
// encryptionScheme field, or the browser does not support EME-related
// fields in MCap _at all_.

// First, install a patch to check the mediaKeySystemAccess or
// encryptionScheme field in future calls.
console.debug('McEncryptionSchemePolyfill: ' +
'No native encryptionScheme support found. '+
'Patching encryptionScheme support.');
// eslint-disable-next-line require-atomic-updates
navigator.mediaCapabilities.decodingInfo =
McEncryptionSchemePolyfill.polyfillDecodingInfo_;

// The results we have may not be valid. Run the query again through our
// polyfill.
// Second, if _none_ of the EME-related fields of MCap are supported, fill
// them in now before returning the results.
if (!mediaKeySystemAccess) {
capabilities.keySystemAccess =
await McEncryptionSchemePolyfill.getMediaKeySystemAccess_(
requestedConfiguration);
return capabilities;
}

// If we land here, it's only the encryption scheme field that is missing.
// The results we have may not be valid, since they didn't account for
// encryption scheme. Run the query again through our polyfill.
return McEncryptionSchemePolyfill.polyfillDecodingInfo_.call(
this, requestedConfiguration);
}
Expand Down Expand Up @@ -378,16 +391,36 @@ class McEncryptionSchemePolyfill {
new EmeEncryptionSchemePolyfillMediaKeySystemAccess(
capabilities.keySystemAccess, supportedScheme);
} else if (requestedConfiguration.keySystemConfiguration) {
const mediaKeySystemConfig =
// If the result is supported and the content is encrypted, we should have
// a MediaKeySystemAccess instance as part of the result. If we land
// here, the browser doesn't support the EME-related fields of MCap.
capabilities.keySystemAccess =
await McEncryptionSchemePolyfill.getMediaKeySystemAccess_(
requestedConfiguration);
}

return capabilities;
}

/**
* Call navigator.requestMediaKeySystemAccess to get the MediaKeySystemAccess
* information.
*
* @param {!MediaDecodingConfiguration} requestedConfiguration The requested
* decoding configuration.
* @return {!Promise.<!MediaKeySystemAccess>} A Promise to a
* MediaKeySystemAccess instance.
* @private
*/
static async getMediaKeySystemAccess_(requestedConfiguration) {
const mediaKeySystemConfig =
McEncryptionSchemePolyfill.convertToMediaKeySystemConfig_(
requestedConfiguration);
capabilities.keySystemAccess =
const keySystemAccess =
await navigator.requestMediaKeySystemAccess(
requestedConfiguration.keySystemConfiguration.keySystem,
[mediaKeySystemConfig]);
}

return capabilities;
return keySystemAccess;
}

/**
Expand Down

0 comments on commit fafd1dd

Please sign in to comment.