Skip to content

Commit f5a33d4

Browse files
committed
Modify key-system helpers so that it's easier to support additional key-system strings
1 parent 78dadb7 commit f5a33d4

File tree

4 files changed

+53
-46
lines changed

4 files changed

+53
-46
lines changed

src/controller/eme-controller.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -325,10 +325,11 @@ class EMEController implements ComponentAPI {
325325
mediaKeySessionContext
326326
);
327327
const keyId = this.getKeyIdString(decryptdata);
328+
const scheme = 'cenc';
328329
this.keyIdToKeySessionPromise[keyId] =
329330
this.generateRequestWithPreferredKeySession(
330331
keySessionContext,
331-
'cenc',
332+
scheme,
332333
decryptdata.pssh
333334
);
334335
this.removeSession(mediaKeySessionContext);
@@ -380,7 +381,7 @@ class EMEController implements ComponentAPI {
380381
}
381382

382383
public selectKeySystemFormat(frag: Fragment): Promise<KeySystemFormats> {
383-
const keyFormats = Object.keys(frag.levelkeys || {});
384+
const keyFormats = Object.keys(frag.levelkeys || {}) as KeySystemFormats[];
384385
if (!this.keyFormatPromise) {
385386
this.log(
386387
`Selecting key-system from fragment (sn: ${frag.sn} ${frag.type}: ${
@@ -392,7 +393,9 @@ class EMEController implements ComponentAPI {
392393
return this.keyFormatPromise;
393394
}
394395

395-
private getKeyFormatPromise(keyFormats: string[]): Promise<KeySystemFormats> {
396+
private getKeyFormatPromise(
397+
keyFormats: KeySystemFormats[]
398+
): Promise<KeySystemFormats> {
396399
return new Promise((resolve, reject) => {
397400
const keySystemsInConfig = getKeySystemsForConfig(this.config);
398401
const keySystemsToAttempt = keyFormats
@@ -440,9 +443,10 @@ class EMEController implements ComponentAPI {
440443
mediaKeys,
441444
decryptdata,
442445
});
446+
const scheme = 'cenc';
443447
return this.generateRequestWithPreferredKeySession(
444448
keySessionContext,
445-
'cenc',
449+
scheme,
446450
decryptdata.pssh
447451
);
448452
});
@@ -1104,10 +1108,12 @@ class EMEController implements ComponentAPI {
11041108
return;
11051109
}
11061110
if (!this.keyFormatPromise) {
1107-
const keyFormats = sessionKeys.reduce(
1108-
(formats: string[], sessionKey: LevelKey) => {
1109-
if (formats.indexOf(sessionKey.keyFormat) === -1) {
1110-
formats.push(sessionKey.keyFormat);
1111+
const keyFormats: KeySystemFormats[] = sessionKeys.reduce(
1112+
(formats: KeySystemFormats[], sessionKey: LevelKey) => {
1113+
if (
1114+
formats.indexOf(sessionKey.keyFormat as KeySystemFormats) === -1
1115+
) {
1116+
formats.push(sessionKey.keyFormat as KeySystemFormats);
11111117
}
11121118
return formats;
11131119
},

src/loader/key-loader.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ export default class KeyLoader implements ComponentAPI {
179179
case 'SAMPLE-AES-CENC':
180180
case 'SAMPLE-AES-CTR':
181181
if (decryptdata.keyFormat === 'identity') {
182-
// loadKeyHTTP handles data URLs
182+
// loadKeyHTTP handles http(s) and data URLs
183183
return this.loadKeyHTTP(keyInfo, frag);
184184
}
185185
return this.loadKeyEME(keyInfo, frag);

src/loader/level-key.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,16 @@ export class LevelKey implements DecryptData {
175175
}
176176
break;
177177
}
178+
default: {
179+
let keydata = keyBytes.subarray(0, 16);
180+
if (keydata.length !== 16) {
181+
const padded = new Uint8Array(16);
182+
padded.set(keydata, 16 - keydata.length);
183+
keydata = padded;
184+
}
185+
this.keyId = keydata;
186+
break;
187+
}
178188
}
179189
}
180190

@@ -257,15 +267,10 @@ function getFairPlayV3Pssh(
257267
const fpsk = mp4Box.apply(null, args as [ArrayLike<number>, Uint8Array]);
258268
return fpsk;
259269
};
260-
const args = [
261-
FpsBoxTypes.fpsd,
262-
makeFpsKeySystemInfoBox(scheme),
263-
makeFpsKeyRequestBox(keyId, keyFormatVersions),
264-
];
265-
const data = mp4Box.apply(null, args as [ArrayLike<number>, Uint8Array]);
266270
const kFairPlayStreamingKeySystemUUID = new Uint8Array([
267271
0x94, 0xce, 0x86, 0xfb, 0x07, 0xff, 0x4f, 0x43, 0xad, 0xb8, 0x93, 0xd2,
268272
0xfa, 0x96, 0x8c, 0xa2,
269273
]);
274+
const data = mp4Box(FpsBoxTypes.fpsd, makeFpsKeySystemInfoBox(scheme), makeFpsKeyRequestBox(keyId, keyFormatVersions));
270275
return mp4pssh(kFairPlayStreamingKeySystemUUID, null, data);
271276
}

src/utils/mediakeys-helper.ts

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@ export enum KeySystemFormats {
2121
export function keySystemFormatToKeySystemDomain(
2222
format: KeySystemFormats
2323
): KeySystems | undefined {
24-
if (format === KeySystemFormats.FAIRPLAY) {
25-
return KeySystems.FAIRPLAY;
26-
} else if (format === KeySystemFormats.PLAYREADY) {
27-
return KeySystems.PLAYREADY;
28-
} else if (format === KeySystemFormats.WIDEVINE) {
29-
return KeySystems.WIDEVINE;
30-
} else if (format === KeySystemFormats.CLEARKEY) {
31-
return KeySystems.CLEARKEY;
24+
switch (format) {
25+
case KeySystemFormats.FAIRPLAY:
26+
return KeySystems.FAIRPLAY;
27+
case KeySystemFormats.PLAYREADY:
28+
return KeySystems.PLAYREADY;
29+
case KeySystemFormats.WIDEVINE:
30+
return KeySystems.WIDEVINE;
31+
case KeySystemFormats.CLEARKEY:
32+
return KeySystems.CLEARKEY;
3233
}
3334
}
3435

@@ -56,37 +57,32 @@ export function keySystemIdToKeySystemDomain(
5657
export function keySystemDomainToKeySystemFormat(
5758
keySystem: KeySystems
5859
): KeySystemFormats | undefined {
59-
if (keySystem === KeySystems.FAIRPLAY) {
60-
return KeySystemFormats.FAIRPLAY;
61-
} else if (keySystem === KeySystems.PLAYREADY) {
62-
return KeySystemFormats.PLAYREADY;
63-
} else if (keySystem === KeySystems.WIDEVINE) {
64-
return KeySystemFormats.WIDEVINE;
65-
} else if (keySystem === KeySystems.CLEARKEY) {
66-
return KeySystemFormats.CLEARKEY;
60+
switch (keySystem) {
61+
case KeySystems.FAIRPLAY:
62+
return KeySystemFormats.FAIRPLAY;
63+
case KeySystems.PLAYREADY:
64+
return KeySystemFormats.PLAYREADY;
65+
case KeySystems.WIDEVINE:
66+
return KeySystemFormats.WIDEVINE;
67+
case KeySystems.CLEARKEY:
68+
return KeySystemFormats.CLEARKEY;
6769
}
6870
}
6971

7072
export function getKeySystemsForConfig(
7173
config: EMEControllerConfig
7274
): KeySystems[] {
7375
const { drmSystems, widevineLicenseUrl } = config;
74-
const keySystemsToAttempt: KeySystems[] = [];
75-
[KeySystems.FAIRPLAY, KeySystems.PLAYREADY, KeySystems.CLEARKEY].forEach(
76-
(keySystem) => {
77-
if (drmSystems?.[keySystem]) {
78-
keySystemsToAttempt.push(keySystem);
79-
}
80-
}
81-
);
82-
if (widevineLicenseUrl || drmSystems?.[KeySystems.WIDEVINE]) {
76+
const keySystemsToAttempt: KeySystems[] = drmSystems
77+
? [
78+
KeySystems.FAIRPLAY,
79+
KeySystems.WIDEVINE,
80+
KeySystems.PLAYREADY,
81+
KeySystems.CLEARKEY,
82+
].filter((keySystem) => !!drmSystems[keySystem])
83+
: [];
84+
if (!keySystemsToAttempt[KeySystems.WIDEVINE] && widevineLicenseUrl) {
8385
keySystemsToAttempt.push(KeySystems.WIDEVINE);
84-
} else if (keySystemsToAttempt.length === 0) {
85-
keySystemsToAttempt.push(
86-
KeySystems.WIDEVINE,
87-
KeySystems.FAIRPLAY,
88-
KeySystems.PLAYREADY
89-
);
9086
}
9187
return keySystemsToAttempt;
9288
}

0 commit comments

Comments
 (0)