-
Notifications
You must be signed in to change notification settings - Fork 125
/
take_pssh_out.ts
101 lines (95 loc) · 3.18 KB
/
take_pssh_out.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/**
* Copyright 2015 CANAL+ Group
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import log from "../../../log";
import sliceUint8Array from "../../../utils/slice_uint8array";
import { bytesToHex } from "../../../utils/string_parsing";
import {
getBoxContent,
getBoxOffsets,
} from "./get_box";
/** Information related to a PSSH box. */
export interface IISOBMFFPSSHInfo {
/** Corresponding DRM's system ID, as an hexadecimal string. */
systemId : string;
/** Additional data contained in the PSSH Box. */
data : Uint8Array;
}
/**
* Replace every PSSH box from an ISOBMFF segment by FREE boxes and returns the
* removed PSSH in an array.
* Useful to manually manage encryption while avoiding the round-trip with the
* browser's encrypted event.
* @param {Uint8Array} data - the ISOBMFF segment
* @returns {Array.<Uint8Array>} - The extracted PSSH boxes. In the order they
* are encountered.
*/
export default function takePSSHOut(data : Uint8Array) : IISOBMFFPSSHInfo[] {
let i = 0;
const moov = getBoxContent(data, 0x6D6F6F76 /* moov */);
if (moov === null) {
return [];
}
const psshBoxes : IISOBMFFPSSHInfo[] = [];
while (i < moov.length) {
let psshOffsets;
try {
psshOffsets = getBoxOffsets(moov, 0x70737368 /* pssh */);
} catch (e) {
const err = e instanceof Error ? e : "";
log.warn("Error while removing PSSH from ISOBMFF", err);
return psshBoxes;
}
if (psshOffsets == null) {
return psshBoxes;
}
const pssh = sliceUint8Array(moov, psshOffsets[0], psshOffsets[2]);
const systemId = getPsshSystemID(pssh, psshOffsets[1] - psshOffsets[0]);
if (systemId !== undefined) {
psshBoxes.push({ systemId, data: pssh });
}
// replace by `free` box.
moov[psshOffsets[0] + 4] = 0x66;
moov[psshOffsets[0] + 5] = 0x72;
moov[psshOffsets[0] + 6] = 0x65;
moov[psshOffsets[0] + 7] = 0x65;
i = psshOffsets[2];
}
return psshBoxes;
}
/**
* Parse systemId from a "pssh" box into an hexadecimal string.
* `undefined` if we could not extract a systemId.
* @param {Uint8Array} buff - The pssh box
* @param {number} initialDataOffset - offset of the first byte after the size
* and name in this pssh box.
* @returns {string|undefined}
*/
export function getPsshSystemID(
buff : Uint8Array,
initialDataOffset : number
) : string | undefined {
if (buff[initialDataOffset] > 1) {
log.warn("ISOBMFF: un-handled PSSH version");
return undefined;
}
const offset = initialDataOffset +
4; /* version + flags */
if (offset + 16 > buff.length) {
return undefined;
}
const systemIDBytes = sliceUint8Array(buff, offset, offset + 16);
return bytesToHex(systemIDBytes);
}