forked from shaka-project/shaka-player
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpssh.js
213 lines (179 loc) · 5.53 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
208
209
210
211
212
/*! @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)
.box('moof', shaka.util.Mp4Parser.children)
.fullBox('pssh', (box) => this.parsePsshBox_(box))
.parse(psshBox);
if (this.data.length == 0) {
shaka.log.v2('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, data, keyIds and version.
*
* @param {!Uint8Array} data
* @param {!Uint8Array} systemId
* @param {!Set<string>} keyIds
* @param {number} version
* @return {!Uint8Array}
*/
static createPssh(data, systemId, keyIds, version) {
goog.asserts.assert(systemId.byteLength == 16, 'Invalid system ID length');
const dataLength = data.length;
let psshSize = 0x4 + 0x4 + 0x4 + systemId.length + 0x4 + dataLength;
if (version > 0) {
psshSize += 0x4 + (16 * keyIds.size);
}
/** @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;
(version < 1) ? psshData.setUint32(byteCursor, 0) :
psshData.setUint32(byteCursor, 0x01000000); // version + flags
byteCursor += 0x4;
psshBox.set(systemId, byteCursor);
byteCursor += systemId.length;
// if version > 0, add KID count and kid values.
if (version > 0) {
psshData.setUint32(byteCursor, keyIds.size); // KID_count
byteCursor += 0x4;
const Uint8ArrayUtils = shaka.util.Uint8ArrayUtils;
for (const keyId of keyIds) {
const KID = Uint8ArrayUtils.fromHex(keyId);
psshBox.set(KID, byteCursor);
byteCursor += KID.length;
}
}
psshData.setUint32(byteCursor, dataLength);
byteCursor += 0x4;
psshBox.set(data, byteCursor);
byteCursor += dataLength;
goog.asserts.assert(byteCursor === psshSize, 'PSSH invalid length.');
return psshBox;
}
/**
* Returns just the data portion of a single PSSH
*
* @param {!Uint8Array} pssh
* @return {!Uint8Array}
*/
static getPsshData(pssh) {
let offset = 8; // Box size and type fields
/** @type {!DataView} */
const view = shaka.util.BufferUtils.toDataView(pssh);
// Read version
const version = view.getUint8(offset);
// Version (1), flags (3), system ID (16)
offset += 20;
if (version > 0) {
// Key ID count (4) and All key IDs (16*count)
offset += 4 + (16 * view.getUint32(offset));
}
// Data size
offset += 4;
return shaka.util.BufferUtils.toUint8(view, offset);
}
/**
* Normalise the initData array. This is to apply browser specific
* workarounds, 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);
}
};