From a2badde7162789710154c9c0c497ea9e253c75f3 Mon Sep 17 00:00:00 2001 From: Michelle Zhuo Date: Mon, 26 Apr 2021 17:40:54 -0700 Subject: [PATCH 1/4] refactor: Call 'navigator.requestMediaKeySystemAccess' --- index.js | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 14c2c1f..86a8a8c 100644 --- a/index.js +++ b/index.js @@ -311,6 +311,13 @@ class McEncryptionSchemePolyfill { navigator.mediaCapabilities.decodingInfo = McEncryptionSchemePolyfill.polyfillDecodingInfo_; + if (!mediaKeySystemAccess) { + capabilities.keySystemAccess = + await McEncryptionSchemePolyfill.getMediaKeySystemAccess_( + requestedConfiguration); + return capabilities; + } + // The results we have may not be valid. Run the query again through our // polyfill. return McEncryptionSchemePolyfill.polyfillDecodingInfo_.call( @@ -378,16 +385,31 @@ 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. + capabilities.keySystemAccess = + await McEncryptionSchemePolyfill.getMediaKeySystemAccess_( + requestedConfiguration); + } + + return capabilities; + } + + /** + * Call navigator.requestMediaKeySystemAccess to get the MediaKeySystemAccess + * information. + * @param {!MediaDecodingConfiguration} requestedConfiguration The requested + * decoding configuration. + */ + static async getMediaKeySystemAccess_(requestedConfiguration) { + const mediaKeySystemConfig = McEncryptionSchemePolyfill.convertToMediaKeySystemConfig_( requestedConfiguration); - capabilities.keySystemAccess = + const keySystemAccess = await navigator.requestMediaKeySystemAccess( requestedConfiguration.keySystemConfiguration.keySystem, [mediaKeySystemConfig]); - } - - return capabilities; + return keySystemAccess; } /** From 39772a666ed92bd6dcf8578a6805890d1210b9dd Mon Sep 17 00:00:00 2001 From: Joey Parrish Date: Thu, 2 Jun 2022 14:41:08 -0700 Subject: [PATCH 2/4] Respond to feedback --- index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 86a8a8c..2df98a0 100644 --- a/index.js +++ b/index.js @@ -399,7 +399,8 @@ class McEncryptionSchemePolyfill { * Call navigator.requestMediaKeySystemAccess to get the MediaKeySystemAccess * information. * @param {!MediaDecodingConfiguration} requestedConfiguration The requested - * decoding configuration. + * decoding configuration. + * @private */ static async getMediaKeySystemAccess_(requestedConfiguration) { const mediaKeySystemConfig = From 69741eac3e213f7942857baa1d77954c85fa6888 Mon Sep 17 00:00:00 2001 From: Joey Parrish Date: Thu, 2 Jun 2022 14:42:48 -0700 Subject: [PATCH 3/4] Fix linter errors --- index.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/index.js b/index.js index 2df98a0..c139813 100644 --- a/index.js +++ b/index.js @@ -398,8 +398,11 @@ class McEncryptionSchemePolyfill { /** * 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) { From f63474232eafb8f02d9de26e48fec608226c9a71 Mon Sep 17 00:00:00 2001 From: Joey Parrish Date: Tue, 7 Jun 2022 13:34:19 -0700 Subject: [PATCH 4/4] Rework comments to clarify flow --- index.js | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index c139813..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,6 +314,8 @@ class McEncryptionSchemePolyfill { navigator.mediaCapabilities.decodingInfo = McEncryptionSchemePolyfill.polyfillDecodingInfo_; + // 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_( @@ -318,8 +323,9 @@ class McEncryptionSchemePolyfill { return capabilities; } - // The results we have may not be valid. Run the query again through our - // polyfill. + // 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); } @@ -386,7 +392,8 @@ class McEncryptionSchemePolyfill { capabilities.keySystemAccess, supportedScheme); } else if (requestedConfiguration.keySystemConfiguration) { // If the result is supported and the content is encrypted, we should have - // a MediaKeySystemAccess instance as part of the result. + // 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);