hls.js 1.6.3-0.canary.11254 → 1.6.3-0.canary.11257

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -52,6 +52,7 @@ interface KeySystemAccessPromises {
52
52
  keySystemAccess: Promise<MediaKeySystemAccess>;
53
53
  mediaKeys?: Promise<MediaKeys>;
54
54
  certificate?: Promise<BufferSource | void>;
55
+ hasMediaKeys?: boolean;
55
56
  }
56
57
 
57
58
  export interface MediaKeySessionContext {
@@ -285,6 +286,7 @@ class EMEController extends Logger implements ComponentAPI {
285
286
  .createMediaKeys()
286
287
  .then((mediaKeys) => {
287
288
  this.log(`Media-keys created for "${keySystem}"`);
289
+ keySystemAccessPromises.hasMediaKeys = true;
288
290
  return certificateRequest.then((certificate) => {
289
291
  if (certificate) {
290
292
  return this.setMediaKeysServerCertificate(
@@ -384,29 +386,29 @@ class EMEController extends Logger implements ComponentAPI {
384
386
  return keySession.update(data);
385
387
  }
386
388
 
387
- public selectKeySystemFormat(frag: Fragment): Promise<KeySystemFormats> {
388
- const keyFormats = Object.keys(frag.levelkeys || {}) as KeySystemFormats[];
389
- if (!this.keyFormatPromise) {
390
- this.log(
391
- `Selecting key-system from fragment (sn: ${frag.sn} ${frag.type}: ${
392
- frag.level
393
- }) key formats ${keyFormats.join(', ')}`,
394
- );
395
- this.keyFormatPromise = this.getKeyFormatPromise(keyFormats);
396
- }
397
- return this.keyFormatPromise;
389
+ public getSelectedKeySystemFormats(): KeySystemFormats[] {
390
+ return (Object.keys(this.keySystemAccessPromises) as KeySystems[])
391
+ .map((keySystem) => ({
392
+ keySystem,
393
+ hasMediaKeys: this.keySystemAccessPromises[keySystem].hasMediaKeys,
394
+ }))
395
+ .filter(({ hasMediaKeys }) => !!hasMediaKeys)
396
+ .map(({ keySystem }) => keySystemToKeySystemFormat(keySystem))
397
+ .filter((keySystem): keySystem is KeySystemFormats => !!keySystem);
398
398
  }
399
399
 
400
- private getKeyFormatPromise(
401
- keyFormats: KeySystemFormats[],
400
+ public getKeySystemAccess(keySystemsToAttempt: KeySystems[]): Promise<void> {
401
+ return this.getKeySystemSelectionPromise(keySystemsToAttempt).then(
402
+ ({ keySystem, mediaKeys }) => {
403
+ return this.attemptSetMediaKeys(keySystem, mediaKeys);
404
+ },
405
+ );
406
+ }
407
+
408
+ public selectKeySystem(
409
+ keySystemsToAttempt: KeySystems[],
402
410
  ): Promise<KeySystemFormats> {
403
411
  return new Promise((resolve, reject) => {
404
- const keySystemsInConfig = getKeySystemsForConfig(this.config);
405
- const keySystemsToAttempt = keyFormats
406
- .map(keySystemFormatToKeySystemDomain)
407
- .filter(
408
- (value) => !!value && keySystemsInConfig.indexOf(value) !== -1,
409
- ) as any as KeySystems[];
410
412
  return this.getKeySystemSelectionPromise(keySystemsToAttempt)
411
413
  .then(({ keySystem }) => {
412
414
  const keySystemFormat = keySystemToKeySystemFormat(keySystem);
@@ -422,6 +424,32 @@ class EMEController extends Logger implements ComponentAPI {
422
424
  });
423
425
  }
424
426
 
427
+ public selectKeySystemFormat(frag: Fragment): Promise<KeySystemFormats> {
428
+ const keyFormats = Object.keys(frag.levelkeys || {}) as KeySystemFormats[];
429
+ if (!this.keyFormatPromise) {
430
+ this.log(
431
+ `Selecting key-system from fragment (sn: ${frag.sn} ${frag.type}: ${
432
+ frag.level
433
+ }) key formats ${keyFormats.join(', ')}`,
434
+ );
435
+ this.keyFormatPromise = this.getKeyFormatPromise(keyFormats);
436
+ }
437
+ return this.keyFormatPromise;
438
+ }
439
+
440
+ private getKeyFormatPromise(
441
+ keyFormats: KeySystemFormats[],
442
+ ): Promise<KeySystemFormats> {
443
+ const keySystemsInConfig = getKeySystemsForConfig(this.config);
444
+ const keySystemsToAttempt = keyFormats
445
+ .map(keySystemFormatToKeySystemDomain)
446
+ .filter(
447
+ (value) => !!value && keySystemsInConfig.indexOf(value) !== -1,
448
+ ) as any as KeySystems[];
449
+
450
+ return this.selectKeySystem(keySystemsToAttempt);
451
+ }
452
+
425
453
  public loadKey(data: KeyLoadedData): Promise<MediaKeySessionContext> {
426
454
  const decryptdata = data.keyInfo.decryptdata;
427
455
 
@@ -1,7 +1,11 @@
1
1
  import { LoadError } from './fragment-loader';
2
2
  import { ErrorDetails, ErrorTypes } from '../errors';
3
- import type { HlsConfig } from '../config';
3
+ import {
4
+ getKeySystemsForConfig,
5
+ keySystemFormatToKeySystemDomain,
6
+ } from '../utils/mediakeys-helper';
4
7
  import type { LevelKey } from './level-key';
8
+ import type { HlsConfig } from '../config';
5
9
  import type EMEController from '../controller/eme-controller';
6
10
  import type { MediaKeySessionContext } from '../controller/eme-controller';
7
11
  import type { Fragment } from '../loader/fragment';
@@ -90,25 +94,46 @@ export default class KeyLoader implements ComponentAPI {
90
94
  loadClear(
91
95
  loadingFrag: Fragment,
92
96
  encryptedFragments: Fragment[],
93
- ): void | Promise<void> {
94
- if (this.emeController && this.config.emeEnabled) {
95
- // access key-system with nearest key on start (loaidng frag is unencrypted)
96
- const { sn, cc } = loadingFrag;
97
- for (let i = 0; i < encryptedFragments.length; i++) {
98
- const frag = encryptedFragments[i];
99
- if (
100
- cc <= frag.cc &&
101
- (sn === 'initSegment' || frag.sn === 'initSegment' || sn < frag.sn)
102
- ) {
103
- this.emeController
104
- .selectKeySystemFormat(frag)
105
- .then((keySystemFormat) => {
106
- frag.setKeyFormat(keySystemFormat);
107
- });
108
- break;
97
+ ): null | Promise<void> {
98
+ if (
99
+ this.emeController &&
100
+ this.config.emeEnabled &&
101
+ !this.emeController.getSelectedKeySystemFormats().length
102
+ ) {
103
+ // access key-system with nearest key on start (loading frag is unencrypted)
104
+ if (encryptedFragments.length) {
105
+ const { sn, cc } = loadingFrag;
106
+ for (let i = 0; i < encryptedFragments.length; i++) {
107
+ const frag = encryptedFragments[i];
108
+ if (
109
+ cc <= frag.cc &&
110
+ (sn === 'initSegment' || frag.sn === 'initSegment' || sn < frag.sn)
111
+ ) {
112
+ return this.emeController
113
+ .selectKeySystemFormat(frag)
114
+ .then((keySystemFormat) => {
115
+ 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
+ }
125
+ }
126
+ });
127
+ }
128
+ }
129
+ } else if (this.config.requireKeySystemAccessOnStart) {
130
+ const keySystemsInConfig = getKeySystemsForConfig(this.config);
131
+ if (keySystemsInConfig.length) {
132
+ return this.emeController.getKeySystemAccess(keySystemsInConfig);
109
133
  }
110
134
  }
111
135
  }
136
+ return null;
112
137
  }
113
138
 
114
139
  load(frag: Fragment): Promise<KeyLoadedData> {