@@ -29,6 +29,7 @@ import type {
2929 MediaAttachedData ,
3030 KeyLoadedData ,
3131 ErrorData ,
32+ ManifestLoadedData ,
3233} from '../types/events' ;
3334import type { EMEControllerConfig } from '../config' ;
3435import type { Fragment } from '../loader/fragment' ;
@@ -105,11 +106,13 @@ class EMEController implements ComponentAPI {
105106 private registerListeners ( ) {
106107 this . hls . on ( Events . MEDIA_ATTACHED , this . onMediaAttached , this ) ;
107108 this . hls . on ( Events . MEDIA_DETACHED , this . onMediaDetached , this ) ;
109+ this . hls . on ( Events . MANIFEST_LOADED , this . onManifestLoaded , this ) ;
108110 }
109111
110112 private unregisterListeners ( ) {
111113 this . hls . off ( Events . MEDIA_ATTACHED , this . onMediaAttached , this ) ;
112114 this . hls . off ( Events . MEDIA_DETACHED , this . onMediaDetached , this ) ;
115+ this . hls . off ( Events . MANIFEST_LOADED , this . onManifestLoaded , this ) ;
113116 }
114117
115118 private getLicenseServerUrl ( keySystem : KeySystems ) : string | never {
@@ -384,27 +387,31 @@ class EMEController implements ComponentAPI {
384387 frag . level
385388 } ) key formats ${ keyFormats . join ( ', ' ) } `
386389 ) ;
387- this . keyFormatPromise = new Promise ( ( resolve , reject ) => {
388- const keySystemsToAttempt = keyFormats
389- . map ( keySystemFormatToKeySystemDomain )
390- . filter ( ( value ) => ! ! value ) as any as KeySystems [ ] ;
391- return this . getKeySystemSelectionPromise ( keySystemsToAttempt ) . then (
392- ( { keySystem } ) => {
393- const keySystemFormat = keySystemToKeySystemFormat ( keySystem ) ;
394- if ( keySystemFormat ) {
395- resolve ( keySystemFormat ) ;
396- } else {
397- reject (
398- new Error ( `Unable to find format for key-system "${ keySystem } "` )
399- ) ;
400- }
401- }
402- ) ;
403- } ) ;
390+ this . keyFormatPromise = this . getKeyFormatPromise ( keyFormats ) ;
404391 }
405392 return this . keyFormatPromise ;
406393 }
407394
395+ private getKeyFormatPromise ( keyFormats : string [ ] ) : Promise < KeySystemFormats > {
396+ return new Promise ( ( resolve , reject ) => {
397+ const keySystemsToAttempt = keyFormats
398+ . map ( keySystemFormatToKeySystemDomain )
399+ . filter ( ( value ) => ! ! value ) as any as KeySystems [ ] ;
400+ return this . getKeySystemSelectionPromise ( keySystemsToAttempt ) . then (
401+ ( { keySystem } ) => {
402+ const keySystemFormat = keySystemToKeySystemFormat ( keySystem ) ;
403+ if ( keySystemFormat ) {
404+ resolve ( keySystemFormat ) ;
405+ } else {
406+ reject (
407+ new Error ( `Unable to find format for key-system "${ keySystem } "` )
408+ ) ;
409+ }
410+ }
411+ ) ;
412+ } ) ;
413+ }
414+
408415 public loadKey ( data : KeyLoadedData ) : Promise < MediaKeySessionContext > {
409416 const decryptdata = data . keyInfo . decryptdata ;
410417
@@ -890,7 +897,11 @@ class EMEController implements ComponentAPI {
890897 licenseChallenge
891898 ) ;
892899 } )
893- . catch ( ( ) => {
900+ . catch ( ( error : Error ) => {
901+ if ( ! keysListItem . decryptdata ) {
902+ // Key session removed. Cancel license request.
903+ throw error ;
904+ }
894905 // let's try to open before running setup
895906 xhr . open ( 'POST' , url , true ) ;
896907
@@ -1102,7 +1113,31 @@ class EMEController implements ComponentAPI {
11021113 } ) ;
11031114 }
11041115
1105- removeSession (
1116+ private onManifestLoaded (
1117+ event : Events . MANIFEST_LOADED ,
1118+ { sessionKeys } : ManifestLoadedData
1119+ ) {
1120+ if ( ! sessionKeys || ! this . config . emeEnabled ) {
1121+ return ;
1122+ }
1123+ if ( ! this . keyFormatPromise ) {
1124+ const keyFormats = sessionKeys . reduce (
1125+ ( formats : string [ ] , sessionKey : LevelKey ) => {
1126+ if ( formats . indexOf ( sessionKey . keyFormat ) === - 1 ) {
1127+ formats . push ( sessionKey . keyFormat ) ;
1128+ }
1129+ return formats ;
1130+ } ,
1131+ [ ]
1132+ ) ;
1133+ this . log (
1134+ `Selecting key-system from session-keys ${ keyFormats . join ( ', ' ) } `
1135+ ) ;
1136+ this . keyFormatPromise = this . getKeyFormatPromise ( keyFormats ) ;
1137+ }
1138+ }
1139+
1140+ private removeSession (
11061141 mediaKeySessionContext : MediaKeySessionContext
11071142 ) : Promise < void > | void {
11081143 const { mediaKeysSession, licenseXhr } = mediaKeySessionContext ;
0 commit comments