11import { LoadError } from './fragment-loader' ;
22import { ErrorDetails , ErrorTypes } from '../errors' ;
3+ import { type Fragment , isMediaFragment } from '../loader/fragment' ;
34import {
45 getKeySystemsForConfig ,
56 keySystemFormatToKeySystemDomain ,
@@ -8,7 +9,6 @@ import type { LevelKey } from './level-key';
89import type { HlsConfig } from '../config' ;
910import type EMEController from '../controller/eme-controller' ;
1011import type { MediaKeySessionContext } from '../controller/eme-controller' ;
11- import type { Fragment } from '../loader/fragment' ;
1212import type { ComponentAPI } from '../types/component-api' ;
1313import type { KeyLoadedData } from '../types/events' ;
1414import type {
@@ -94,34 +94,36 @@ export default class KeyLoader implements ComponentAPI {
9494 loadClear (
9595 loadingFrag : Fragment ,
9696 encryptedFragments : Fragment [ ] ,
97+ startFragRequested : boolean ,
9798 ) : null | Promise < void > {
9899 if (
99100 this . emeController &&
100101 this . config . emeEnabled &&
101102 ! this . emeController . getSelectedKeySystemFormats ( ) . length
102103 ) {
103- // access key-system with nearest key on start (loading frag is unencrypted)
104+ // Access key-system with nearest key on start (loading frag is unencrypted)
104105 if ( encryptedFragments . length ) {
105- const { sn, cc } = loadingFrag ;
106- for ( let i = 0 ; i < encryptedFragments . length ; i ++ ) {
106+ for ( let i = 0 , l = encryptedFragments . length ; i < l ; i ++ ) {
107107 const frag = encryptedFragments [ i ] ;
108+ // Loading at or before segment with EXT-X-KEY, or first frag loading and last EXT-X-KEY
108109 if (
109- cc <= frag . cc &&
110- ( sn === 'initSegment' || frag . sn === 'initSegment' || sn < frag . sn )
110+ ( loadingFrag . cc <= frag . cc &&
111+ ( ! isMediaFragment ( loadingFrag ) ||
112+ ! isMediaFragment ( frag ) ||
113+ loadingFrag . sn < frag . sn ) ) ||
114+ ( ! startFragRequested && i == l - 1 )
111115 ) {
112116 return this . emeController
113117 . selectKeySystemFormat ( frag )
114118 . then ( ( keySystemFormat ) => {
119+ if ( ! this . emeController ) {
120+ return ;
121+ }
115122 frag . setKeyFormat ( keySystemFormat ) ;
116- if (
117- this . emeController &&
118- this . config . requireKeySystemAccessOnStart
119- ) {
120- const keySystem =
121- keySystemFormatToKeySystemDomain ( keySystemFormat ) ;
122- if ( keySystem ) {
123- return this . emeController . getKeySystemAccess ( [ keySystem ] ) ;
124- }
123+ const keySystem =
124+ keySystemFormatToKeySystemDomain ( keySystemFormat ) ;
125+ if ( keySystem ) {
126+ return this . emeController . getKeySystemAccess ( [ keySystem ] ) ;
125127 }
126128 } ) ;
127129 }
0 commit comments