Skip to content

Commit 408bba3

Browse files
committed
Initial key-system access on first segment request with encrypted segments
Follow up to video-dev#7383
1 parent fc3b80a commit 408bba3

File tree

3 files changed

+19
-16
lines changed

3 files changed

+19
-16
lines changed

api-extractor/report/hls.js.api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2995,7 +2995,7 @@ export class KeyLoader implements ComponentAPI {
29952995
// (undocumented)
29962996
load(frag: Fragment): Promise<KeyLoadedData>;
29972997
// (undocumented)
2998-
loadClear(loadingFrag: Fragment, encryptedFragments: Fragment[]): null | Promise<void>;
2998+
loadClear(loadingFrag: Fragment, encryptedFragments: Fragment[], startFragRequested: boolean): null | Promise<void>;
29992999
// (undocumented)
30003000
loadInternal(frag: Fragment, keySystemFormat?: KeySystemFormats): Promise<KeyLoadedData>;
30013001
// (undocumented)

src/controller/base-stream-controller.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,7 @@ export default class BaseStreamController
817817
keyLoadingPromise = this.keyLoader.loadClear(
818818
frag,
819819
details.encryptedFragments,
820+
this.startFragRequested,
820821
);
821822
if (keyLoadingPromise) {
822823
this.log(`[eme] blocking frag load until media-keys acquired`);

src/loader/key-loader.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { LoadError } from './fragment-loader';
22
import { ErrorDetails, ErrorTypes } from '../errors';
3+
import { type Fragment, isMediaFragment } from '../loader/fragment';
34
import {
45
getKeySystemsForConfig,
56
keySystemFormatToKeySystemDomain,
@@ -8,7 +9,6 @@ import type { LevelKey } from './level-key';
89
import type { HlsConfig } from '../config';
910
import type EMEController from '../controller/eme-controller';
1011
import type { MediaKeySessionContext } from '../controller/eme-controller';
11-
import type { Fragment } from '../loader/fragment';
1212
import type { ComponentAPI } from '../types/component-api';
1313
import type { KeyLoadedData } from '../types/events';
1414
import 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

Comments
 (0)