Skip to content

Commit

Permalink
Modify key-system helpers so that it's easier to support additional k…
Browse files Browse the repository at this point in the history
…ey-system strings
  • Loading branch information
robwalch committed Dec 15, 2022
1 parent 78dadb7 commit d3eefb7
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 46 deletions.
22 changes: 14 additions & 8 deletions src/controller/eme-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,10 +325,11 @@ class EMEController implements ComponentAPI {
mediaKeySessionContext
);
const keyId = this.getKeyIdString(decryptdata);
const scheme = 'cenc';
this.keyIdToKeySessionPromise[keyId] =
this.generateRequestWithPreferredKeySession(
keySessionContext,
'cenc',
scheme,
decryptdata.pssh
);
this.removeSession(mediaKeySessionContext);
Expand Down Expand Up @@ -380,7 +381,7 @@ class EMEController implements ComponentAPI {
}

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

private getKeyFormatPromise(keyFormats: string[]): Promise<KeySystemFormats> {
private getKeyFormatPromise(
keyFormats: KeySystemFormats[]
): Promise<KeySystemFormats> {
return new Promise((resolve, reject) => {
const keySystemsInConfig = getKeySystemsForConfig(this.config);
const keySystemsToAttempt = keyFormats
Expand Down Expand Up @@ -440,9 +443,10 @@ class EMEController implements ComponentAPI {
mediaKeys,
decryptdata,
});
const scheme = 'cenc';
return this.generateRequestWithPreferredKeySession(
keySessionContext,
'cenc',
scheme,
decryptdata.pssh
);
});
Expand Down Expand Up @@ -1104,10 +1108,12 @@ class EMEController implements ComponentAPI {
return;
}
if (!this.keyFormatPromise) {
const keyFormats = sessionKeys.reduce(
(formats: string[], sessionKey: LevelKey) => {
if (formats.indexOf(sessionKey.keyFormat) === -1) {
formats.push(sessionKey.keyFormat);
const keyFormats: KeySystemFormats[] = sessionKeys.reduce(
(formats: KeySystemFormats[], sessionKey: LevelKey) => {
if (
formats.indexOf(sessionKey.keyFormat as KeySystemFormats) === -1
) {
formats.push(sessionKey.keyFormat as KeySystemFormats);
}
return formats;
},
Expand Down
2 changes: 1 addition & 1 deletion src/loader/key-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ export default class KeyLoader implements ComponentAPI {
case 'SAMPLE-AES-CENC':
case 'SAMPLE-AES-CTR':
if (decryptdata.keyFormat === 'identity') {
// loadKeyHTTP handles data URLs
// loadKeyHTTP handles http(s) and data URLs
return this.loadKeyHTTP(keyInfo, frag);
}
return this.loadKeyEME(keyInfo, frag);
Expand Down
21 changes: 15 additions & 6 deletions src/loader/level-key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,16 @@ export class LevelKey implements DecryptData {
}
break;
}
default: {
let keydata = keyBytes.subarray(0, 16);
if (keydata.length !== 16) {
const padded = new Uint8Array(16);
padded.set(keydata, 16 - keydata.length);
keydata = padded;
}
this.keyId = keydata;
break;
}
}
}

Expand Down Expand Up @@ -257,15 +267,14 @@ function getFairPlayV3Pssh(
const fpsk = mp4Box.apply(null, args as [ArrayLike<number>, Uint8Array]);
return fpsk;
};
const args = [
FpsBoxTypes.fpsd,
makeFpsKeySystemInfoBox(scheme),
makeFpsKeyRequestBox(keyId, keyFormatVersions),
];
const data = mp4Box.apply(null, args as [ArrayLike<number>, Uint8Array]);
const kFairPlayStreamingKeySystemUUID = new Uint8Array([
0x94, 0xce, 0x86, 0xfb, 0x07, 0xff, 0x4f, 0x43, 0xad, 0xb8, 0x93, 0xd2,
0xfa, 0x96, 0x8c, 0xa2,
]);
const data = mp4Box(
FpsBoxTypes.fpsd,
makeFpsKeySystemInfoBox(scheme),
makeFpsKeyRequestBox(keyId, keyFormatVersions)
);
return mp4pssh(kFairPlayStreamingKeySystemUUID, null, data);
}
58 changes: 27 additions & 31 deletions src/utils/mediakeys-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ export enum KeySystemFormats {
export function keySystemFormatToKeySystemDomain(
format: KeySystemFormats
): KeySystems | undefined {
if (format === KeySystemFormats.FAIRPLAY) {
return KeySystems.FAIRPLAY;
} else if (format === KeySystemFormats.PLAYREADY) {
return KeySystems.PLAYREADY;
} else if (format === KeySystemFormats.WIDEVINE) {
return KeySystems.WIDEVINE;
} else if (format === KeySystemFormats.CLEARKEY) {
return KeySystems.CLEARKEY;
switch (format) {
case KeySystemFormats.FAIRPLAY:
return KeySystems.FAIRPLAY;
case KeySystemFormats.PLAYREADY:
return KeySystems.PLAYREADY;
case KeySystemFormats.WIDEVINE:
return KeySystems.WIDEVINE;
case KeySystemFormats.CLEARKEY:
return KeySystems.CLEARKEY;
}
}

Expand Down Expand Up @@ -56,37 +57,32 @@ export function keySystemIdToKeySystemDomain(
export function keySystemDomainToKeySystemFormat(
keySystem: KeySystems
): KeySystemFormats | undefined {
if (keySystem === KeySystems.FAIRPLAY) {
return KeySystemFormats.FAIRPLAY;
} else if (keySystem === KeySystems.PLAYREADY) {
return KeySystemFormats.PLAYREADY;
} else if (keySystem === KeySystems.WIDEVINE) {
return KeySystemFormats.WIDEVINE;
} else if (keySystem === KeySystems.CLEARKEY) {
return KeySystemFormats.CLEARKEY;
switch (keySystem) {
case KeySystems.FAIRPLAY:
return KeySystemFormats.FAIRPLAY;
case KeySystems.PLAYREADY:
return KeySystemFormats.PLAYREADY;
case KeySystems.WIDEVINE:
return KeySystemFormats.WIDEVINE;
case KeySystems.CLEARKEY:
return KeySystemFormats.CLEARKEY;
}
}

export function getKeySystemsForConfig(
config: EMEControllerConfig
): KeySystems[] {
const { drmSystems, widevineLicenseUrl } = config;
const keySystemsToAttempt: KeySystems[] = [];
[KeySystems.FAIRPLAY, KeySystems.PLAYREADY, KeySystems.CLEARKEY].forEach(
(keySystem) => {
if (drmSystems?.[keySystem]) {
keySystemsToAttempt.push(keySystem);
}
}
);
if (widevineLicenseUrl || drmSystems?.[KeySystems.WIDEVINE]) {
const keySystemsToAttempt: KeySystems[] = drmSystems
? [
KeySystems.FAIRPLAY,
KeySystems.WIDEVINE,
KeySystems.PLAYREADY,
KeySystems.CLEARKEY,
].filter((keySystem) => !!drmSystems[keySystem])
: [];
if (!keySystemsToAttempt[KeySystems.WIDEVINE] && widevineLicenseUrl) {
keySystemsToAttempt.push(KeySystems.WIDEVINE);
} else if (keySystemsToAttempt.length === 0) {
keySystemsToAttempt.push(
KeySystems.WIDEVINE,
KeySystems.FAIRPLAY,
KeySystems.PLAYREADY
);
}
return keySystemsToAttempt;
}
Expand Down

0 comments on commit d3eefb7

Please sign in to comment.