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: Avoid duplicate calls to decodingInfo() #43

Merged
merged 4 commits into from
Jun 7, 2022
Merged
Changes from all 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
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);
Copy link

Choose a reason for hiding this comment

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

This case could use a comment. Something to explain that this is avoiding unnecessary calls to the original decodingInfo calls, by doing the only remaining thing the polyfill would need to do in this situation, perhaps?

Copy link
Member Author

Choose a reason for hiding this comment

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

I added comments here, and reworded others to try to clarify things. PTAL!

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