/
pssh.js
208 lines (179 loc) · 5.91 KB
/
pssh.js
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*! @license
* Shaka Player
* Copyright 2016 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
goog.provide('shaka.util.Pssh');
goog.require('goog.asserts');
goog.require('shaka.log');
goog.require('shaka.util.BufferUtils');
goog.require('shaka.util.Mp4Parser');
goog.require('shaka.util.Uint8ArrayUtils');
/**
* @summary
* Parse a PSSH box and extract the system IDs.
*/
shaka.util.Pssh = class {
/**
* @param {!Uint8Array} psshBox
*/
constructor(psshBox) {
/**
* In hex.
* @type {!Array.<string>}
*/
this.systemIds = [];
/**
* In hex.
* @type {!Array.<string>}
*/
this.cencKeyIds = [];
/**
* Array with the pssh boxes found.
* @type {!Array.<!Uint8Array>}
*/
this.data = [];
new shaka.util.Mp4Parser()
.box('moov', shaka.util.Mp4Parser.children)
.fullBox('pssh', (box) => this.parsePsshBox_(box))
.parse(psshBox);
if (this.data.length == 0) {
shaka.log.warning('No pssh box found!');
}
}
/**
* @param {!shaka.extern.ParsedBox} box
* @private
*/
parsePsshBox_(box) {
goog.asserts.assert(
box.version != null,
'PSSH boxes are full boxes and must have a valid version');
goog.asserts.assert(
box.flags != null,
'PSSH boxes are full boxes and must have a valid flag');
if (box.version > 1) {
shaka.log.warning('Unrecognized PSSH version found!');
return;
}
// The "reader" gives us a view on the payload of the box. Create a new
// view that contains the whole box.
const dataView = box.reader.getDataView();
goog.asserts.assert(
dataView.byteOffset >= 12, 'DataView at incorrect position');
const pssh = shaka.util.BufferUtils.toUint8(dataView, -12, box.size);
this.data.push(pssh);
this.systemIds.push(
shaka.util.Uint8ArrayUtils.toHex(box.reader.readBytes(16)));
if (box.version > 0) {
const numKeyIds = box.reader.readUint32();
for (let i = 0; i < numKeyIds; i++) {
const keyId =
shaka.util.Uint8ArrayUtils.toHex(box.reader.readBytes(16));
this.cencKeyIds.push(keyId);
}
}
}
/**
* Creates a pssh blob from the given system ID and data.
*
* @param {!Uint8Array} data
* @param {!Uint8Array} systemId
* @return {!Uint8Array}
*/
static createPssh(data, systemId) {
goog.asserts.assert(systemId.byteLength == 16, 'Invalid system ID length');
const dataLength = data.length;
const psshSize = 0x4 + 0x4 + 0x4 + systemId.length + 0x4 + dataLength;
/** @type {!Uint8Array} */
const psshBox = new Uint8Array(psshSize);
/** @type {!DataView} */
const psshData = shaka.util.BufferUtils.toDataView(psshBox);
let byteCursor = 0;
psshData.setUint32(byteCursor, psshSize);
byteCursor += 0x4;
psshData.setUint32(byteCursor, 0x70737368); // 'pssh'
byteCursor += 0x4;
psshData.setUint32(byteCursor, 0); // flags
byteCursor += 0x4;
psshBox.set(systemId, byteCursor);
byteCursor += systemId.length;
psshData.setUint32(byteCursor, dataLength);
byteCursor += 0x4;
psshBox.set(data, byteCursor);
byteCursor += dataLength;
goog.asserts.assert(byteCursor === psshSize, 'PSSH invalid length.');
return psshBox;
}
/**
* Creates a ClearKey cenc pssh blob from the given system ID and data.
* Data will contain one key id.
* Based on https://www.w3.org/TR/eme-initdata-cenc/
* @param {!Uint8Array} data
* @param {!Uint8Array} systemId
* @return {!Uint8Array}
*/
static createPsshClearKey(data, systemId) {
goog.asserts.assert(systemId.byteLength == 16, 'Invalid system ID length');
const dataLength = data.length;
const psshSize = 0x4 + // pssh size (4 bytes)
0x4 + // 'pssh' (4 bytes)
0x4 + // version (1 byte) + flags (3 bytes)
systemId.length + // system id (16 bytes)
0x4 + // KID count (4 bytes)
dataLength + // KID value (16 bytes)
0x4; // size of data (4 bytes)
/** @type {!Uint8Array} */
const psshBox = new Uint8Array(psshSize);
/** @type {!DataView} */
const psshData = shaka.util.BufferUtils.toDataView(psshBox);
let byteCursor = 0;
psshData.setUint32(byteCursor, psshSize);
byteCursor += 0x4;
psshData.setUint32(byteCursor, 0x70737368); // 'pssh'
byteCursor += 0x4;
psshData.setUint32(byteCursor, 0x01000000); // version = 1
byteCursor += 0x4;
psshBox.set(systemId, byteCursor);
byteCursor += systemId.length;
psshData.setUint32(byteCursor, 0x00000001); // KID_count
byteCursor += 0x4;
psshBox.set(data, byteCursor); // add kid
byteCursor += dataLength;
psshData.setUint32(byteCursor, 0x00000000);
byteCursor += 0x4;
goog.asserts.assert(byteCursor === psshSize, 'PSSH invalid length.');
return psshBox;
}
/**
* Normalise the initData array. This is to apply browser specific
* work-arounds, e.g. removing duplicates which appears to occur
* intermittently when the native msneedkey event fires (i.e. event.initData
* contains dupes).
*
* @param {!Uint8Array} initData
* @return {!Uint8Array}
*/
static normaliseInitData(initData) {
if (!initData) {
return initData;
}
const pssh = new shaka.util.Pssh(initData);
// If there is only a single pssh, return the original array.
if (pssh.data.length <= 1) {
return initData;
}
// Dedupe psshData.
/** @type {!Array.<!Uint8Array>} */
const dedupedInitDatas = [];
for (const initData of pssh.data) {
const found = dedupedInitDatas.some((x) => {
return shaka.util.BufferUtils.equal(x, initData);
});
if (!found) {
dedupedInitDatas.push(initData);
}
}
return shaka.util.Uint8ArrayUtils.concat(...dedupedInitDatas);
}
};