@@ -17,21 +17,11 @@ import {
1717 getKeySystemsForConfig ,
1818 getSupportedMediaKeySystemConfigurations ,
1919 isPersistentSessionType ,
20+ keySystemDomainToKeySystemFormat ,
2021 keySystemFormatToKeySystemDomain ,
21- keySystemIdToKeySystemDomain ,
2222 KeySystems ,
23- keySystemDomainToKeySystemFormat as keySystemToKeySystemFormat ,
24- parsePlayReadyWRM ,
2523 requestMediaKeySystemAccess ,
2624} from '../utils/mediakeys-helper' ;
27- import {
28- bin2str ,
29- parseMultiPssh ,
30- parseSinf ,
31- type PsshData ,
32- type PsshInvalidResult ,
33- } from '../utils/mp4-tools' ;
34- import { base64Decode } from '../utils/numeric-encoding-utils' ;
3525import { stringify } from '../utils/safe-json-stringify' ;
3626import { strToUtf8array } from '../utils/utf8-utils' ;
3727import type { EMEControllerConfig , HlsConfig , LoadPolicy } from '../config' ;
@@ -119,7 +109,7 @@ class EMEController extends Logger implements ComponentAPI {
119109 // @ts -ignore
120110 this . hls = this . config = this . keyIdToKeySessionPromise = null ;
121111 // @ts -ignore
122- this . onMediaEncrypted = this . onWaitingForKey = null ;
112+ this . onWaitingForKey = null ;
123113 }
124114
125115 private registerListeners ( ) {
@@ -398,7 +388,7 @@ class EMEController extends Logger implements ComponentAPI {
398388 hasMediaKeys : this . keySystemAccessPromises [ keySystem ] . hasMediaKeys ,
399389 } ) )
400390 . filter ( ( { hasMediaKeys } ) => ! ! hasMediaKeys )
401- . map ( ( { keySystem } ) => keySystemToKeySystemFormat ( keySystem ) )
391+ . map ( ( { keySystem } ) => keySystemDomainToKeySystemFormat ( keySystem ) )
402392 . filter ( ( keySystem ) : keySystem is KeySystemFormats => ! ! keySystem ) ;
403393 }
404394
@@ -416,7 +406,7 @@ class EMEController extends Logger implements ComponentAPI {
416406 return new Promise ( ( resolve , reject ) => {
417407 return this . getKeySystemSelectionPromise ( keySystemsToAttempt )
418408 . then ( ( { keySystem } ) => {
419- const keySystemFormat = keySystemToKeySystemFormat ( keySystem ) ;
409+ const keySystemFormat = keySystemDomainToKeySystemFormat ( keySystem ) ;
420410 if ( keySystemFormat ) {
421411 resolve ( keySystemFormat ) ;
422412 } else {
@@ -562,197 +552,6 @@ class EMEController extends Logger implements ComponentAPI {
562552 return this . attemptKeySystemAccess ( keySystemsToAttempt ) ;
563553 }
564554
565- private onMediaEncrypted = ( event : MediaEncryptedEvent ) => {
566- const { initDataType, initData } = event ;
567- const logMessage = `"${ event . type } " event: init data type: "${ initDataType } "` ;
568- this . debug ( logMessage ) ;
569-
570- // Ignore event when initData is null
571- if ( initData === null ) {
572- return ;
573- }
574-
575- if ( ! this . keyFormatPromise ) {
576- let keySystems = Object . keys (
577- this . keySystemAccessPromises ,
578- ) as KeySystems [ ] ;
579- if ( ! keySystems . length ) {
580- keySystems = getKeySystemsForConfig ( this . config ) ;
581- }
582- const keyFormats = keySystems
583- . map ( keySystemToKeySystemFormat )
584- . filter ( ( k ) => ! ! k ) as KeySystemFormats [ ] ;
585- this . keyFormatPromise = this . getKeyFormatPromise ( keyFormats ) ;
586- }
587-
588- this . keyFormatPromise . then ( ( keySystemFormat ) => {
589- const keySystem = keySystemFormatToKeySystemDomain ( keySystemFormat ) ;
590-
591- let keyId : Uint8Array < ArrayBuffer > | null | undefined ;
592- let keySystemDomain : KeySystems | undefined ;
593-
594- if ( initDataType === 'sinf' ) {
595- if ( keySystem !== KeySystems . FAIRPLAY ) {
596- this . warn (
597- `Ignoring unexpected "${ event . type } " event with init data type: "${ initDataType } " for selected key-system ${ keySystem } ` ,
598- ) ;
599- return ;
600- }
601- // Match sinf keyId to playlist skd://keyId=
602- const json = bin2str ( new Uint8Array ( initData ) ) ;
603- try {
604- const sinf = base64Decode ( JSON . parse ( json ) . sinf ) ;
605- const tenc = parseSinf ( sinf ) ;
606- if ( ! tenc ) {
607- throw new Error (
608- `'schm' box missing or not cbcs/cenc with schi > tenc` ,
609- ) ;
610- }
611- keyId = new Uint8Array ( tenc . subarray ( 8 , 24 ) ) ;
612- keySystemDomain = KeySystems . FAIRPLAY ;
613- } catch ( error ) {
614- this . warn ( `${ logMessage } Failed to parse sinf: ${ error } ` ) ;
615- return ;
616- }
617- } else {
618- if (
619- keySystem !== KeySystems . WIDEVINE &&
620- keySystem !== KeySystems . PLAYREADY
621- ) {
622- this . warn (
623- `Ignoring unexpected "${ event . type } " event with init data type: "${ initDataType } " for selected key-system ${ keySystem } ` ,
624- ) ;
625- return ;
626- }
627- // Support Widevine/PlayReady clear-lead key-session creation (otherwise depend on playlist keys)
628- const psshResults = parseMultiPssh ( initData ) ;
629-
630- const psshInfos = psshResults . filter (
631- ( pssh ) : pssh is PsshData =>
632- ! ! pssh . systemId &&
633- keySystemIdToKeySystemDomain ( pssh . systemId ) === keySystem ,
634- ) ;
635-
636- if ( psshInfos . length > 1 ) {
637- this . warn (
638- `${ logMessage } Using first of ${ psshInfos . length } pssh found for selected key-system ${ keySystem } ` ,
639- ) ;
640- }
641-
642- const psshInfo = psshInfos [ 0 ] ;
643-
644- if ( ! psshInfo ) {
645- if (
646- psshResults . length === 0 ||
647- psshResults . some (
648- ( pssh ) : pssh is PsshInvalidResult => ! pssh . systemId ,
649- )
650- ) {
651- this . warn ( `${ logMessage } contains incomplete or invalid pssh data` ) ;
652- } else {
653- this . log (
654- `ignoring ${ logMessage } for ${ ( psshResults as PsshData [ ] )
655- . map ( ( pssh ) => keySystemIdToKeySystemDomain ( pssh . systemId ) )
656- . join ( ',' ) } pssh data in favor of playlist keys`,
657- ) ;
658- }
659- return ;
660- }
661-
662- keySystemDomain = keySystemIdToKeySystemDomain ( psshInfo . systemId ) ;
663- if ( psshInfo . version === 0 && psshInfo . data ) {
664- if ( keySystemDomain === KeySystems . WIDEVINE ) {
665- const offset = psshInfo . data . length - 22 ;
666- keyId = new Uint8Array ( psshInfo . data . subarray ( offset , offset + 16 ) ) ;
667- } else if ( keySystemDomain === KeySystems . PLAYREADY ) {
668- keyId = parsePlayReadyWRM ( psshInfo . data ) ;
669- }
670- }
671- }
672-
673- if ( ! keySystemDomain || ! keyId ) {
674- return ;
675- }
676-
677- const keyIdHex = Hex . hexDump ( keyId ) ;
678- const { keyIdToKeySessionPromise, mediaKeySessions } = this ;
679-
680- let keySessionContextPromise = keyIdToKeySessionPromise [ keyIdHex ] ;
681- for ( let i = 0 ; i < mediaKeySessions . length ; i ++ ) {
682- // Match playlist key
683- const keyContext = mediaKeySessions [ i ] ;
684- const decryptdata = keyContext . decryptdata ;
685- if ( ! decryptdata . keyId ) {
686- continue ;
687- }
688- const oldKeyIdHex = Hex . hexDump ( decryptdata . keyId ) ;
689- if (
690- keyIdHex === oldKeyIdHex ||
691- decryptdata . uri . replace ( / - / g, '' ) . indexOf ( keyIdHex ) !== - 1
692- ) {
693- keySessionContextPromise = keyIdToKeySessionPromise [ oldKeyIdHex ] ;
694- if ( decryptdata . pssh ) {
695- break ;
696- }
697- delete keyIdToKeySessionPromise [ oldKeyIdHex ] ;
698- decryptdata . pssh = new Uint8Array ( initData ) ;
699- decryptdata . keyId = keyId ;
700- keySessionContextPromise = keyIdToKeySessionPromise [ keyIdHex ] =
701- keySessionContextPromise . then ( ( ) => {
702- return this . generateRequestWithPreferredKeySession (
703- keyContext ,
704- initDataType ,
705- initData ,
706- 'encrypted-event-key-match' ,
707- ) ;
708- } ) ;
709- keySessionContextPromise . catch ( ( error ) => this . handleError ( error ) ) ;
710- break ;
711- }
712- }
713-
714- if ( ! keySessionContextPromise ) {
715- if ( keySystemDomain !== keySystem ) {
716- this . log (
717- `Ignoring "${ event . type } " event with ${ keySystemDomain } init data for selected key-system ${ keySystem } ` ,
718- ) ;
719- return ;
720- }
721- // "Clear-lead" (misc key not encountered in playlist)
722- keySessionContextPromise = keyIdToKeySessionPromise [ keyIdHex ] =
723- this . getKeySystemSelectionPromise ( [ keySystemDomain ] ) . then (
724- ( { keySystem, mediaKeys } ) => {
725- this . throwIfDestroyed ( ) ;
726-
727- const decryptdata = new LevelKey (
728- 'ISO-23001-7' ,
729- keyIdHex ,
730- keySystemToKeySystemFormat ( keySystem ) ?? '' ,
731- ) ;
732- decryptdata . pssh = new Uint8Array ( initData ) ;
733- decryptdata . keyId = keyId ;
734- return this . attemptSetMediaKeys ( keySystem , mediaKeys ) . then ( ( ) => {
735- this . throwIfDestroyed ( ) ;
736- const keySessionContext = this . createMediaKeySessionContext ( {
737- decryptdata,
738- keySystem,
739- mediaKeys,
740- } ) ;
741- return this . generateRequestWithPreferredKeySession (
742- keySessionContext ,
743- initDataType ,
744- initData ,
745- 'encrypted-event-no-match' ,
746- ) ;
747- } ) ;
748- } ,
749- ) ;
750-
751- keySessionContextPromise . catch ( ( error ) => this . handleError ( error ) ) ;
752- }
753- } ) ;
754- } ;
755-
756555 private onWaitingForKey = ( event : Event ) => {
757556 this . log ( `"${ event . type } " event` ) ;
758557 } ;
@@ -1331,15 +1130,13 @@ class EMEController extends Logger implements ComponentAPI {
13311130 // keep reference of media
13321131 this . media = media ;
13331132
1334- addEventListener ( media , 'encrypted' , this . onMediaEncrypted ) ;
13351133 addEventListener ( media , 'waitingforkey' , this . onWaitingForKey ) ;
13361134 }
13371135
13381136 private onMediaDetached ( ) {
13391137 const media = this . media ;
13401138
13411139 if ( media ) {
1342- removeEventListener ( media , 'encrypted' , this . onMediaEncrypted ) ;
13431140 removeEventListener ( media , 'waitingforkey' , this . onWaitingForKey ) ;
13441141 this . media = null ;
13451142 this . mediaKeys = null ;
0 commit comments