diff --git a/index.js b/index.js index 14c2c1f..ebf9b9b 100644 --- a/index.js +++ b/index.js @@ -301,9 +301,12 @@ 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.'); @@ -311,8 +314,18 @@ class McEncryptionSchemePolyfill { 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); } @@ -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.} 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; } /**