-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathcontent_protection.js
571 lines (504 loc) · 17.5 KB
/
content_protection.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
/**
* @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.
*/
goog.provide('shaka.dash.ContentProtection');
goog.require('goog.asserts');
goog.require('shaka.log');
goog.require('shaka.util.Error');
goog.require('shaka.util.ManifestParserUtils');
goog.require('shaka.util.Uint8ArrayUtils');
goog.require('shaka.util.XmlUtils');
/**
* @summary A set of functions for parsing and interpreting ContentProtection
* elements.
*/
shaka.dash.ContentProtection = class {
/**
* Parses info from the ContentProtection elements at the AdaptationSet level.
*
* @param {!Array.<!Element>} elems
* @param {shaka.extern.DashContentProtectionCallback} callback
* @param {boolean} ignoreDrmInfo
* @return {shaka.dash.ContentProtection.Context}
*/
static parseFromAdaptationSet(elems, callback, ignoreDrmInfo) {
const ContentProtection = shaka.dash.ContentProtection;
const ManifestParserUtils = shaka.util.ManifestParserUtils;
const parsed = ContentProtection.parseElements_(elems);
/** @type {Array.<shaka.extern.InitDataOverride>} */
let defaultInit = null;
/** @type {!Array.<shaka.extern.DrmInfo>} */
let drmInfos = [];
let parsedNonCenc = [];
// Get the default key ID; if there are multiple, they must all match.
const keyIds = new Set(parsed.map((element) => element.keyId));
// Remove any possible null value (elements may have no key ids).
keyIds.delete(null);
if (keyIds.size > 1) {
throw new shaka.util.Error(
shaka.util.Error.Severity.CRITICAL,
shaka.util.Error.Category.MANIFEST,
shaka.util.Error.Code.DASH_CONFLICTING_KEY_IDS);
}
if (!ignoreDrmInfo) {
// Find the default key ID and init data. Create a new array of all the
// non-CENC elements.
parsedNonCenc = parsed.filter((elem) => {
if (elem.schemeUri == ContentProtection.MP4Protection_) {
goog.asserts.assert(!elem.init || elem.init.length,
'Init data must be null or non-empty.');
defaultInit = elem.init || defaultInit;
return false;
} else {
return true;
}
});
if (parsedNonCenc.length) {
drmInfos = ContentProtection.convertElements_(
defaultInit, callback, parsedNonCenc);
// If there are no drmInfos after parsing, then add a dummy entry.
// This may be removed in parseKeyIds.
if (drmInfos.length == 0) {
drmInfos = [ManifestParserUtils.createDrmInfo('', defaultInit)];
}
}
}
// If there are only CENC element(s) or ignoreDrmInfo flag is set, assume
// all key-systems are supported.
if (parsed.length && (ignoreDrmInfo || !parsedNonCenc.length)) {
drmInfos = [];
const keySystems = ContentProtection.defaultKeySystems_;
for (const keySystem of keySystems.values()) {
// If the manifest doesn't specify any key systems, we shouldn't
// put clearkey in this list. Otherwise, it may be triggered when
// a real key system should be used instead.
if (keySystem != 'org.w3.clearkey') {
const info =
ManifestParserUtils.createDrmInfo(keySystem, defaultInit);
drmInfos.push(info);
}
}
}
// If we have a default key id, apply it to every initData.
const defaultKeyId = Array.from(keyIds)[0] || null;
if (defaultKeyId) {
for (const info of drmInfos) {
for (const initData of info.initData) {
initData.keyId = defaultKeyId;
}
}
}
return {
defaultKeyId: defaultKeyId,
defaultInit: defaultInit,
drmInfos: drmInfos,
firstRepresentation: true,
};
}
/**
* Parses the given ContentProtection elements found at the Representation
* level. This may update the |context|.
*
* @param {!Array.<!Element>} elems
* @param {shaka.extern.DashContentProtectionCallback} callback
* @param {shaka.dash.ContentProtection.Context} context
* @param {boolean} ignoreDrmInfo
* @return {?string} The parsed key ID
*/
static parseFromRepresentation(elems, callback, context, ignoreDrmInfo) {
const ContentProtection = shaka.dash.ContentProtection;
const repContext = ContentProtection.parseFromAdaptationSet(
elems, callback, ignoreDrmInfo);
if (context.firstRepresentation) {
const asUnknown = context.drmInfos.length == 1 &&
!context.drmInfos[0].keySystem;
const asUnencrypted = context.drmInfos.length == 0;
const repUnencrypted = repContext.drmInfos.length == 0;
// There are two cases where we need to replace the |drmInfos| in the
// context with those in the Representation:
// 1. The AdaptationSet does not list any ContentProtection.
// 2. The AdaptationSet only lists unknown key-systems.
if (asUnencrypted || (asUnknown && !repUnencrypted)) {
context.drmInfos = repContext.drmInfos;
}
context.firstRepresentation = false;
} else if (repContext.drmInfos.length > 0) {
// If this is not the first Representation, then we need to remove entries
// from the context that do not appear in this Representation.
context.drmInfos = context.drmInfos.filter((asInfo) => {
return repContext.drmInfos.some((repInfo) => {
return repInfo.keySystem == asInfo.keySystem;
});
});
// If we have filtered out all key-systems, throw an error.
if (context.drmInfos.length == 0) {
throw new shaka.util.Error(
shaka.util.Error.Severity.CRITICAL,
shaka.util.Error.Category.MANIFEST,
shaka.util.Error.Code.DASH_NO_COMMON_KEY_SYSTEM);
}
}
return repContext.defaultKeyId || context.defaultKeyId;
}
/**
* Gets a Widevine license URL from a content protection element
* containing a custom `ms:laurl` element
*
* @param {shaka.dash.ContentProtection.Element} element
* @return {string}
*/
static getWidevineLicenseUrl(element) {
const mslaurlNode = shaka.util.XmlUtils.findChildNS(
element.node, 'urn:microsoft', 'laurl');
if (mslaurlNode) {
return mslaurlNode.getAttribute('licenseUrl') || '';
}
return '';
}
/**
* Parses an Array buffer starting at byteOffset for PlayReady Object Records.
* Each PRO Record is preceded by its PlayReady Record type and length in
* bytes.
*
* PlayReady Object Record format: https://goo.gl/FTcu46
*
* @param {!ArrayBuffer} recordData
* @param {number} byteOffset
* @return {!Array.<shaka.dash.ContentProtection.PlayReadyRecord>}
* @private
*/
static parseMsProRecords_(recordData, byteOffset) {
const records = [];
const view = new DataView(recordData);
while (byteOffset < recordData.byteLength - 1) {
const type = view.getUint16(byteOffset, true);
byteOffset += 2;
const byteLength = view.getUint16(byteOffset, true);
byteOffset += 2;
goog.asserts.assert(
(byteLength & 1) === 0,
'expected byteLength to be an even number');
const recordValue = new Uint8Array(recordData, byteOffset, byteLength);
records.push({
type: type,
value: recordValue,
});
byteOffset += byteLength;
}
return records;
}
/**
* Parses an ArrayBuffer for PlayReady Objects. The data
* should contain a 32-bit integer indicating the length of
* the PRO in bytes. Following that, a 16-bit integer for
* the number of PlayReady Object Records in the PRO. Lastly,
* a byte array of the PRO Records themselves.
*
* PlayReady Object format: https://goo.gl/W8yAN4
*
* @param {!ArrayBuffer} data
* @return {!Array.<shaka.dash.ContentProtection.PlayReadyRecord>}
* @private
*/
static parseMsPro_(data) {
let byteOffset = 0;
const view = new DataView(data);
// First 4 bytes is the PRO length (DWORD)
const byteLength = view.getUint32(byteOffset, true /* littleEndian */);
byteOffset += 4;
if (byteLength !== data.byteLength) {
// Malformed PRO
shaka.log.warning('PlayReady Object with invalid length encountered.');
return [];
}
// Skip PRO Record count (WORD)
byteOffset += 2;
// Rest of the data contains the PRO Records
const ContentProtection = shaka.dash.ContentProtection;
return ContentProtection.parseMsProRecords_(data, byteOffset);
}
/**
* PlayReady Header format: https://goo.gl/dBzxNA
*
* @param {!Element} xml
* @return {string}
* @private
*/
static getLaurl_(xml) {
// LA_URL element is optional and no more than one is
// allowed inside the DATA element. Only absolute URLs are allowed.
// If the LA_URL element exists, it must not be empty.
const laurlNode = xml.querySelector('DATA > LA_URL');
if (laurlNode) {
return laurlNode.textContent;
}
// Not found
return '';
}
/**
* Gets a PlayReady license URL from a content protection element
* containing a PlayReady Header Object
*
* @param {shaka.dash.ContentProtection.Element} element
* @return {string}
*/
static getPlayReadyLicenseUrl(element) {
const proNode = shaka.util.XmlUtils.findChildNS(
element.node, 'urn:microsoft:playready', 'pro');
if (!proNode) {
return '';
}
const ContentProtection = shaka.dash.ContentProtection;
const PLAYREADY_RECORD_TYPES = ContentProtection.PLAYREADY_RECORD_TYPES;
const bytes = shaka.util.Uint8ArrayUtils.fromBase64(proNode.textContent);
const records = ContentProtection.parseMsPro_(bytes.buffer);
const record = records.filter((record) => {
return record.type === PLAYREADY_RECORD_TYPES.RIGHTS_MANAGEMENT;
})[0];
if (!record) {
return '';
}
const xml = shaka.util.StringUtils.fromUTF16(record.value, true);
const rootElement = shaka.util.XmlUtils.parseXmlString(xml, 'WRMHEADER');
if (!rootElement) {
return '';
}
return ContentProtection.getLaurl_(rootElement);
}
/**
* Creates DrmInfo objects from the given element.
*
* @param {Array.<shaka.extern.InitDataOverride>} defaultInit
* @param {shaka.extern.DashContentProtectionCallback} callback
* @param {!Array.<shaka.dash.ContentProtection.Element>} elements
* @return {!Array.<shaka.extern.DrmInfo>}
* @private
*/
static convertElements_(defaultInit, callback, elements) {
const ContentProtection = shaka.dash.ContentProtection;
const ManifestParserUtils = shaka.util.ManifestParserUtils;
const defaultKeySystems = ContentProtection.defaultKeySystems_;
const licenseUrlParsers = ContentProtection.licenseUrlParsers_;
/** @type {!Array.<shaka.extern.DrmInfo>} */
const out = [];
for (const element of elements) {
const keySystem = defaultKeySystems.get(element.schemeUri);
if (keySystem) {
goog.asserts.assert(
!element.init || element.init.length,
'Init data must be null or non-empty.');
const initData = element.init || defaultInit;
const info = ManifestParserUtils.createDrmInfo(keySystem, initData);
const licenseParser = licenseUrlParsers.get(keySystem);
if (licenseParser) {
info.licenseServerUri = licenseParser(element);
}
out.push(info);
} else {
goog.asserts.assert(callback, 'ContentProtection callback is required');
const infos = callback(element.node) || [];
for (const info of infos) {
out.push(info);
}
}
}
return out;
}
/**
* Parses the given ContentProtection elements. If there is an error, it
* removes those elements.
*
* @param {!Array.<!Element>} elems
* @return {!Array.<shaka.dash.ContentProtection.Element>}
* @private
*/
static parseElements_(elems) {
/** @type {!Array.<shaka.dash.ContentProtection.Element>} */
const out = [];
for (const elem of elems) {
const parsed = shaka.dash.ContentProtection.parseElement_(elem);
if (parsed) {
out.push(parsed);
}
}
return out;
}
/**
* Parses the given ContentProtection element.
*
* @param {!Element} elem
* @return {?shaka.dash.ContentProtection.Element}
* @private
*/
static parseElement_(elem) {
const NS = shaka.dash.ContentProtection.CencNamespaceUri_;
/** @type {?string} */
let schemeUri = elem.getAttribute('schemeIdUri');
/** @type {?string} */
let keyId = shaka.util.XmlUtils.getAttributeNS(elem, NS, 'default_KID');
/** @type {!Array.<string>} */
const psshs = shaka.util.XmlUtils.findChildrenNS(elem, NS, 'pssh')
.map(shaka.util.XmlUtils.getContents);
if (!schemeUri) {
shaka.log.error('Missing required schemeIdUri attribute on',
'ContentProtection element', elem);
return null;
}
schemeUri = schemeUri.toLowerCase();
if (keyId) {
keyId = keyId.replace(/-/g, '').toLowerCase();
if (keyId.includes(' ')) {
throw new shaka.util.Error(
shaka.util.Error.Severity.CRITICAL,
shaka.util.Error.Category.MANIFEST,
shaka.util.Error.Code.DASH_MULTIPLE_KEY_IDS_NOT_SUPPORTED);
}
}
/** @type {!Array.<shaka.extern.InitDataOverride>} */
let init = [];
try {
// Try parsing PSSH data.
init = psshs.map((pssh) => {
return {
initDataType: 'cenc',
initData: shaka.util.Uint8ArrayUtils.fromBase64(pssh),
keyId: null,
};
});
} catch (e) {
throw new shaka.util.Error(
shaka.util.Error.Severity.CRITICAL,
shaka.util.Error.Category.MANIFEST,
shaka.util.Error.Code.DASH_PSSH_BAD_ENCODING);
}
return {
node: elem,
schemeUri: schemeUri,
keyId: keyId,
init: (init.length > 0 ? init : null),
};
}
};
/**
* @typedef {{
* type: number,
* value: !Uint8Array
* }}
*
* @description
* The parsed result of a PlayReady object record.
*
* @property {number} type
* Type of data stored in the record.
* @property {!Uint8Array} value
* Record content.
*/
shaka.dash.ContentProtection.PlayReadyRecord;
/**
* Enum for PlayReady record types.
* @enum {number}
*/
shaka.dash.ContentProtection.PLAYREADY_RECORD_TYPES = {
RIGHTS_MANAGEMENT: 0x001,
RESERVED: 0x002,
EMBEDDED_LICENSE: 0x003,
};
/**
* @typedef {{
* defaultKeyId: ?string,
* defaultInit: Array.<shaka.extern.InitDataOverride>,
* drmInfos: !Array.<shaka.extern.DrmInfo>,
* firstRepresentation: boolean
* }}
*
* @description
* Contains information about the ContentProtection elements found at the
* AdaptationSet level.
*
* @property {?string} defaultKeyId
* The default key ID to use. This is used by parseKeyIds as a default. This
* can be null to indicate that there is no default.
* @property {Array.<shaka.extern.InitDataOverride>} defaultInit
* The default init data override. This can be null to indicate that there
* is no default.
* @property {!Array.<shaka.extern.DrmInfo>} drmInfos
* The DrmInfo objects.
* @property {boolean} firstRepresentation
* True when first parsed; changed to false after the first call to
* parseKeyIds. This is used to determine if a dummy key-system should be
* overwritten; namely that the first representation can replace the dummy
* from the AdaptationSet.
*/
shaka.dash.ContentProtection.Context;
/**
* @typedef {{
* node: !Element,
* schemeUri: string,
* keyId: ?string,
* init: Array.<shaka.extern.InitDataOverride>
* }}
*
* @description
* The parsed result of a single ContentProtection element.
*
* @property {!Element} node
* The ContentProtection XML element.
* @property {string} schemeUri
* The scheme URI.
* @property {?string} keyId
* The default key ID, if present.
* @property {Array.<shaka.extern.InitDataOverride>} init
* The init data, if present. If there is no init data, it will be null. If
* this is non-null, there is at least one element.
*/
shaka.dash.ContentProtection.Element;
/**
* A map of scheme URI to key system name.
*
* @const {!Map.<string, string>}
* @private
*/
shaka.dash.ContentProtection.defaultKeySystems_ = new Map()
.set('urn:uuid:1077efec-c0b2-4d02-ace3-3c1e52e2fb4b',
'org.w3.clearkey')
.set('urn:uuid:edef8ba9-79d6-4ace-a3c8-27dcd51d21ed',
'com.widevine.alpha')
.set('urn:uuid:9a04f079-9840-4286-ab92-e65be0885f95',
'com.microsoft.playready')
.set('urn:uuid:f239e769-efa3-4850-9c16-a903c6932efb',
'com.adobe.primetime');
/**
* A map of key system name to license server url parser.
*
* @const {!Map.<string, function(shaka.dash.ContentProtection.Element)>}
* @private
*/
shaka.dash.ContentProtection.licenseUrlParsers_ = new Map()
.set('com.widevine.alpha',
shaka.dash.ContentProtection.getWidevineLicenseUrl)
.set('com.microsoft.playready',
shaka.dash.ContentProtection.getPlayReadyLicenseUrl);
/**
* @const {string}
* @private
*/
shaka.dash.ContentProtection.MP4Protection_ =
'urn:mpeg:dash:mp4protection:2011';
/**
* @const {string}
* @private
*/
shaka.dash.ContentProtection.CencNamespaceUri_ = 'urn:mpeg:cenc:2013';