forked from shaka-project/shaka-player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpssh_unit.js
264 lines (226 loc) · 9.95 KB
/
pssh_unit.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/**
* @license
* Copyright 2016 Google Inc.
*
* 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.
*/
describe('Pssh', function() {
var fromHex = shaka.util.Uint8ArrayUtils.fromHex;
var toHex = shaka.util.Uint8ArrayUtils.toHex;
/** @const {string} */
var WIDEVINE_SYSTEM_ID = 'edef8ba979d64acea3c827dcd51d21ed';
/** @const {string} */
var PLAYREADY_SYSTEM_ID = '9a04f07998404286ab92e65be0885f95';
/** @const {string} */
var GENERIC_SYSTEM_ID = '1077efecc0b24d02ace33c1e52e2fb4b';
/** @const {string} */
var WIDEVINE_PSSH =
'00000028' + // atom size
'70737368' + // atom type='pssh'
'00000000' + // v0, flags=0
'edef8ba979d64acea3c827dcd51d21ed' + // system id (Widevine)
'00000008' + // data size
'0102030405060708'; // data
/** @const {string} */
var PLAYREADY_PSSH =
'00000028' + // atom size
'70737368' + // atom type 'pssh'
'00000000' + // v0, flags=0
'9a04f07998404286ab92e65be0885f95' + // system id (PlayReady)
'00000008' + // data size
'0102030405060708'; // data
/** @const {string} */
var GENERIC_PSSH =
'00000044' + // atom size
'70737368' + // atom type 'pssh'
'01000000' + // v1, flags=0
'1077efecc0b24d02ace33c1e52e2fb4b' + // system id (generic CENC)
'00000002' + // key ID count
'30313233343536373839303132333435' + // key ID='0123456789012345'
'38393031323334354142434445464748' + // key ID='ABCDEFGHIJKLMNOP'
'00000000'; // data size=0
/** @const {string} */
var ZERO_SIZED_GENERIC_PSSH =
'00000000' + // atom size (whole buffer)
'70737368' + // atom type='pssh'
'01000000' + // v1, flags=0
'1077efecc0b24d02ace33c1e52e2fb4b' + // system id (generic CENC)
'00000002' + // key ID count
'30313233343536373839303132333435' + // key ID='0123456789012345'
'38393031323334354142434445464748' + // key ID='ABCDEFGHIJKLMNOP'
'00000000'; // data size=0
/** @const {string} */
var OTHER_BOX =
'00000018' + // atom size
'77686174' + // atom type 'what'
'deadbeefdeadbeefdeadbeefdeadbeef'; // garbage box data
/** @const {string} */
var TRUNCATED_WIDEVINE_PSSH =
WIDEVINE_PSSH.substr(0, WIDEVINE_PSSH.length - 6);
/** @const {string} */
var TRUNCATED_PLAYREADY_PSSH =
PLAYREADY_PSSH.substr(0, PLAYREADY_PSSH.length - 6);
/** @const {string} */
var TRUNCATED_GENERIC_PSSH =
GENERIC_PSSH.substr(0, GENERIC_PSSH.length - 6);
it('parses a Widevine PSSH', function() {
var pssh = new shaka.util.Pssh(fromHex(WIDEVINE_PSSH));
expect(pssh.systemIds.length).toBe(1);
expect(pssh.systemIds[0]).toBe(WIDEVINE_SYSTEM_ID);
expect(pssh.cencKeyIds.length).toBe(0);
});
it('parses a PlayReady PSSH', function() {
var pssh = new shaka.util.Pssh(fromHex(PLAYREADY_PSSH));
expect(pssh.systemIds.length).toBe(1);
expect(pssh.systemIds[0]).toBe(PLAYREADY_SYSTEM_ID);
expect(pssh.cencKeyIds.length).toBe(0);
});
it('parses a generic CENC PSSH', function() {
var pssh = new shaka.util.Pssh(fromHex(GENERIC_PSSH));
expect(pssh.systemIds.length).toBe(1);
expect(pssh.systemIds[0]).toBe(GENERIC_SYSTEM_ID);
expect(pssh.cencKeyIds.length).toBe(2);
});
it('throws on a truncated PSSH', function() {
var psshs = [
fromHex(TRUNCATED_WIDEVINE_PSSH),
fromHex(TRUNCATED_PLAYREADY_PSSH),
fromHex(TRUNCATED_GENERIC_PSSH),
fromHex(WIDEVINE_PSSH + TRUNCATED_PLAYREADY_PSSH),
fromHex(WIDEVINE_PSSH + TRUNCATED_GENERIC_PSSH),
fromHex(PLAYREADY_PSSH + TRUNCATED_WIDEVINE_PSSH),
fromHex(PLAYREADY_PSSH + TRUNCATED_GENERIC_PSSH),
fromHex(GENERIC_PSSH + TRUNCATED_WIDEVINE_PSSH),
fromHex(GENERIC_PSSH + TRUNCATED_PLAYREADY_PSSH)
];
for (var i = 0; i < psshs.length; ++i) {
try {
new shaka.util.Pssh(psshs[i]);
fail();
} catch (error) {
expect(error instanceof shaka.util.Error).toBe(true);
expect(error.code).toBe(
shaka.util.Error.Code.BUFFER_READ_OUT_OF_BOUNDS);
}
}
});
it('parses concatenated PSSHs in any order', function() {
var pssh = new shaka.util.Pssh(fromHex(
WIDEVINE_PSSH + PLAYREADY_PSSH));
expect(pssh.systemIds.length).toBe(2);
expect(pssh.systemIds[0]).toBe(WIDEVINE_SYSTEM_ID);
expect(pssh.systemIds[1]).toBe(PLAYREADY_SYSTEM_ID);
expect(pssh.cencKeyIds.length).toBe(0);
pssh = new shaka.util.Pssh(fromHex(
PLAYREADY_PSSH + WIDEVINE_PSSH));
expect(pssh.systemIds[0]).toBe(PLAYREADY_SYSTEM_ID);
expect(pssh.systemIds[1]).toBe(WIDEVINE_SYSTEM_ID);
expect(pssh.cencKeyIds.length).toBe(0);
pssh = new shaka.util.Pssh(fromHex(
WIDEVINE_PSSH + GENERIC_PSSH));
expect(pssh.systemIds[0]).toBe(WIDEVINE_SYSTEM_ID);
expect(pssh.systemIds[1]).toBe(GENERIC_SYSTEM_ID);
expect(pssh.cencKeyIds.length).toBe(2);
pssh = new shaka.util.Pssh(fromHex(
GENERIC_PSSH + WIDEVINE_PSSH));
expect(pssh.systemIds[0]).toBe(GENERIC_SYSTEM_ID);
expect(pssh.systemIds[1]).toBe(WIDEVINE_SYSTEM_ID);
expect(pssh.cencKeyIds.length).toBe(2);
pssh = new shaka.util.Pssh(fromHex(
WIDEVINE_PSSH + PLAYREADY_PSSH + GENERIC_PSSH));
expect(pssh.systemIds[0]).toBe(WIDEVINE_SYSTEM_ID);
expect(pssh.systemIds[1]).toBe(PLAYREADY_SYSTEM_ID);
expect(pssh.systemIds[2]).toBe(GENERIC_SYSTEM_ID);
expect(pssh.cencKeyIds.length).toBe(2);
pssh = new shaka.util.Pssh(fromHex(
PLAYREADY_PSSH + WIDEVINE_PSSH + GENERIC_PSSH));
expect(pssh.systemIds[0]).toBe(PLAYREADY_SYSTEM_ID);
expect(pssh.systemIds[1]).toBe(WIDEVINE_SYSTEM_ID);
expect(pssh.systemIds[2]).toBe(GENERIC_SYSTEM_ID);
expect(pssh.cencKeyIds.length).toBe(2);
pssh = new shaka.util.Pssh(fromHex(
WIDEVINE_PSSH + GENERIC_PSSH + PLAYREADY_PSSH));
expect(pssh.systemIds[0]).toBe(WIDEVINE_SYSTEM_ID);
expect(pssh.systemIds[1]).toBe(GENERIC_SYSTEM_ID);
expect(pssh.systemIds[2]).toBe(PLAYREADY_SYSTEM_ID);
expect(pssh.cencKeyIds.length).toBe(2);
pssh = new shaka.util.Pssh(fromHex(
PLAYREADY_PSSH + GENERIC_PSSH + WIDEVINE_PSSH));
expect(pssh.systemIds[0]).toBe(PLAYREADY_SYSTEM_ID);
expect(pssh.systemIds[1]).toBe(GENERIC_SYSTEM_ID);
expect(pssh.systemIds[2]).toBe(WIDEVINE_SYSTEM_ID);
expect(pssh.cencKeyIds.length).toBe(2);
pssh = new shaka.util.Pssh(fromHex(
GENERIC_PSSH + WIDEVINE_PSSH + PLAYREADY_PSSH));
expect(pssh.systemIds[0]).toBe(GENERIC_SYSTEM_ID);
expect(pssh.systemIds[1]).toBe(WIDEVINE_SYSTEM_ID);
expect(pssh.systemIds[2]).toBe(PLAYREADY_SYSTEM_ID);
expect(pssh.cencKeyIds.length).toBe(2);
pssh = new shaka.util.Pssh(fromHex(
GENERIC_PSSH + PLAYREADY_PSSH + WIDEVINE_PSSH));
expect(pssh.systemIds[0]).toBe(GENERIC_SYSTEM_ID);
expect(pssh.systemIds[1]).toBe(PLAYREADY_SYSTEM_ID);
expect(pssh.systemIds[2]).toBe(WIDEVINE_SYSTEM_ID);
expect(pssh.cencKeyIds.length).toBe(2);
});
it('ignores non-PSSH boxes and continues parsing', function() {
var pssh = new shaka.util.Pssh(fromHex(
OTHER_BOX + WIDEVINE_PSSH));
expect(pssh.systemIds.length).toBe(1);
expect(pssh.systemIds[0]).toBe(WIDEVINE_SYSTEM_ID);
pssh = new shaka.util.Pssh(fromHex(
WIDEVINE_PSSH + OTHER_BOX));
expect(pssh.systemIds.length).toBe(1);
expect(pssh.systemIds[0]).toBe(WIDEVINE_SYSTEM_ID);
expect(pssh.cencKeyIds.length).toBe(0);
pssh = new shaka.util.Pssh(fromHex(
PLAYREADY_PSSH + OTHER_BOX + WIDEVINE_PSSH));
expect(pssh.systemIds.length).toBe(2);
expect(pssh.systemIds[0]).toBe(PLAYREADY_SYSTEM_ID);
expect(pssh.systemIds[1]).toBe(WIDEVINE_SYSTEM_ID);
expect(pssh.cencKeyIds.length).toBe(0);
});
it('parses a zero-sized PSSH box', function() {
var pssh = new shaka.util.Pssh(fromHex(ZERO_SIZED_GENERIC_PSSH));
expect(pssh.systemIds.length).toBe(1);
expect(pssh.systemIds[0]).toBe(GENERIC_SYSTEM_ID);
expect(pssh.cencKeyIds.length).toBe(2);
pssh = new shaka.util.Pssh(fromHex(
WIDEVINE_PSSH + ZERO_SIZED_GENERIC_PSSH));
expect(pssh.systemIds.length).toBe(2);
expect(pssh.systemIds[0]).toBe(WIDEVINE_SYSTEM_ID);
expect(pssh.systemIds[1]).toBe(GENERIC_SYSTEM_ID);
expect(pssh.cencKeyIds.length).toBe(2);
});
it('extracts boundaries for concatenated boxes', function() {
var psshData = fromHex(
GENERIC_PSSH +
WIDEVINE_PSSH +
OTHER_BOX +
PLAYREADY_PSSH);
var pssh = new shaka.util.Pssh(psshData);
expect(pssh.dataBoundaries.length).toBe(3);
var data1 = psshData.subarray(
pssh.dataBoundaries[0].start,
pssh.dataBoundaries[0].end + 1);
var data2 = psshData.subarray(
pssh.dataBoundaries[1].start,
pssh.dataBoundaries[1].end + 1);
var data3 = psshData.subarray(
pssh.dataBoundaries[2].start,
pssh.dataBoundaries[2].end + 1);
expect(toHex(data1)).toEqual(GENERIC_PSSH);
expect(toHex(data2)).toEqual(WIDEVINE_PSSH);
expect(toHex(data3)).toEqual(PLAYREADY_PSSH);
});
});