From 3978d619eb03534d89651a0cb11be8a9afad3387 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Velad=20Galv=C3=A1n?= Date: Tue, 7 May 2024 18:13:24 +0200 Subject: [PATCH] fix: Fix CBCS support in some platforms (#63) Fixes #62 Co-authored-by: Joey Parrish --- index.js | 51 +++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 45 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 817c3f4..f2c4d6b 100644 --- a/index.js +++ b/index.js @@ -202,10 +202,8 @@ class EmeEncryptionSchemePolyfill { } return capabilities.filter((capability) => { - // No specific scheme always works. In addition, accept the specific - // scheme we guessed for this UA. - return !capability['encryptionScheme'] || - capability['encryptionScheme'] == supportedScheme; + return checkSupportedScheme( + capability['encryptionScheme'], supportedScheme); }); } } @@ -369,10 +367,10 @@ class McEncryptionSchemePolyfill { configuration: requestedConfiguration, }; - if (audioScheme && audioScheme != supportedScheme) { + if (!checkSupportedScheme(audioScheme, supportedScheme)) { return notSupportedResult; } - if (videoScheme && videoScheme != supportedScheme) { + if (!checkSupportedScheme(videoScheme, supportedScheme)) { return notSupportedResult; } } @@ -600,6 +598,35 @@ function hasEncryptionScheme(mediaKeySystemAccess) { return false; } +/** + * @param {(string|undefined)} scheme Encryption scheme to check + * @param {?string} supportedScheme A guess at the encryption scheme this + * supports. + * @return {boolean} True if the scheme is compatible. + */ +function checkSupportedScheme(scheme, supportedScheme) { + if (!scheme) { + // Not encrypted = always supported + return true; + } + + if (scheme == supportedScheme) { + // The assumed-supported legacy scheme for this platform. + return true; + } + + if (scheme == 'cbcs' || scheme == 'cbcs-1-9') { + if (EncryptionSchemePolyfills.isRecentFirefox || + EncryptionSchemePolyfills.isChromecast) { + // Firefox >= 100 supports CBCS, but doesn't support queries yet. + // Older Chromecast devices are assumed to support CBCS as well. + return true; + } + } + + return false; +} + /** * The original requestMediaKeySystemAccess, before we patched it. * @@ -642,6 +669,18 @@ class EncryptionSchemePolyfills { } } +/** + * @const {boolean} + */ +EncryptionSchemePolyfills.isChromecast = + navigator.userAgent.includes('CrKey'); + +/** + * @const {boolean} + */ +EncryptionSchemePolyfills.isRecentFirefox = + parseInt(navigator.userAgent.split('Firefox/').pop(), 10) >= 100; + // Support for CommonJS and AMD module formats. /** @suppress {undefinedVars} */ (() => {