| 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 |
2
| /**
* @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.Functional');
goog.require('shaka.util.MapUtils');
goog.require('shaka.util.Uint8ArrayUtils');
goog.require('shaka.util.XmlUtils');
/**
* @namespace shaka.dash.ContentProtection
* @summary A set of functions for parsing and interpreting ContentProtection
* elements.
*/
/**
* @typedef {{
* defaultKeyId: ?string,
* defaultInit: Array.<shakaExtern.InitDataOverride>,
* drmInfos: !Array.<shakaExtern.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.<shakaExtern.InitDataOverride>} defaultInit
* The default init data override. This can be null to indicate that there
* is no default.
* @property {!Array.<shakaExtern.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.<shakaExtern.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.<shakaExtern.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 {!Object.<string, string>}
* @private
*/
shaka.dash.ContentProtection.defaultKeySystems_ = {
'urn:uuid:1077efec-c0b2-4d02-ace3-3c1e52e2fb4b': 'org.w3.clearkey',
'urn:uuid:edef8ba9-79d6-4ace-a3c8-27dcd51d21ed': 'com.widevine.alpha',
'urn:uuid:9a04f079-9840-4286-ab92-e65be0885f95': 'com.microsoft.playready',
'urn:uuid:f239e769-efa3-4850-9c16-a903c6932efb': 'com.adobe.primetime'
};
/**
* @const {string}
* @private
*/
shaka.dash.ContentProtection.MP4Protection_ =
'urn:mpeg:dash:mp4protection:2011';
/**
* Parses info from the ContentProtection elements at the AdaptationSet level.
*
* @param {!Array.<!Element>} elems
* @param {shakaExtern.DashContentProtectionCallback} callback
* @return {shaka.dash.ContentProtection.Context}
*/
shaka.dash.ContentProtection.parseFromAdaptationSet = function(
elems, callback) {
var ContentProtection = shaka.dash.ContentProtection;
var Functional = shaka.util.Functional;
var MapUtils = shaka.util.MapUtils;
var parsed = ContentProtection.parseElements_(elems);
// Find the default key ID and init data. Create a new array of all the
// non-CENC elements.
/** @type {Array.<shakaExtern.InitDataOverride>} */
var defaultInit = null;
var parsedNonCenc = parsed.filter(function(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;
}
});
// Get the default key ID; if there are multiple, they must all match.
var keyIds = parsed.map(function(elem) { return elem.keyId; })
.filter(Functional.isNotNull);
/** @type {?string} */
var defaultKeyId = null;
if (keyIds.length > 0) {
defaultKeyId = keyIds[0];
if (keyIds.some(Functional.isNotEqualFunc(defaultKeyId))) {
throw new shaka.util.Error(
shaka.util.Error.Category.MANIFEST,
shaka.util.Error.Code.DASH_CONFLICTING_KEY_IDS);
}
}
/** @type {!Array.<shakaExtern.DrmInfo>} */
var drmInfos = [];
if (parsedNonCenc.length > 0) {
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 = [ContentProtection.createDrmInfo_('', defaultInit)];
}
} else if (parsed.length > 0) {
// If there are only CENC element(s), then assume all key-systems are
// supported.
var keySystems = ContentProtection.defaultKeySystems_;
drmInfos =
MapUtils.values(keySystems)
.map(function(keySystem) {
return ContentProtection.createDrmInfo_(keySystem, defaultInit);
});
}
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 {shakaExtern.DashContentProtectionCallback} callback
* @param {shaka.dash.ContentProtection.Context} context
* @return {?string} The parsed key ID
*/
shaka.dash.ContentProtection.parseFromRepresentation = function(
elems, callback, context) {
var ContentProtection = shaka.dash.ContentProtection;
var repContext = ContentProtection.parseFromAdaptationSet(elems, callback);
if (context.firstRepresentation) {
var asUnknown = context.drmInfos.length == 1 &&
!context.drmInfos[0].keySystem;
var asUnencrypted = context.drmInfos.length == 0;
var repUnencrypted = repContext.drmInfos.length == 0;
// There are two cases when we need to replace the |drmInfos| in the context
// with those in the Representation:
// * The AdaptationSet does not list any ContentProtection.
// * 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(function(asInfo) {
return repContext.drmInfos.some(function(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.Category.MANIFEST,
shaka.util.Error.Code.DASH_NO_COMMON_KEY_SYSTEM);
}
}
return repContext.defaultKeyId || context.defaultKeyId;
};
/**
* Creates a DrmInfo object from the given info.
*
* @param {string} keySystem
* @param {Array.<shakaExtern.InitDataOverride>} initData
* @return {shakaExtern.DrmInfo}
* @private
*/
shaka.dash.ContentProtection.createDrmInfo_ = function(keySystem, initData) {
return {
keySystem: keySystem,
licenseServerUri: '',
distinctiveIdentifierRequired: false,
persistentStateRequired: false,
audioRobustness: '',
videoRobustness: '',
serverCertificate: null,
initData: initData || [],
keyIds: []
};
};
/**
* Creates DrmInfo objects from the given element.
*
* @param {Array.<shakaExtern.InitDataOverride>} defaultInit
* @param {shakaExtern.DashContentProtectionCallback} callback
* @param {!Array.<shaka.dash.ContentProtection.Element>} elements
* @return {!Array.<shakaExtern.DrmInfo>}
* @private
*/
shaka.dash.ContentProtection.convertElements_ = function(
defaultInit, callback, elements) {
var Functional = shaka.util.Functional;
return elements.map(
/**
* @param {shaka.dash.ContentProtection.Element} element
* @return {!Array.<shakaExtern.DrmInfo>}
*/
function(element) {
var ContentProtection = shaka.dash.ContentProtection;
var keySystem = ContentProtection.defaultKeySystems_[element.schemeUri];
if (keySystem) {
goog.asserts.assert(!element.init || element.init.length,
'Init data must be null or non-empty.');
var initData = element.init || defaultInit;
return [ContentProtection.createDrmInfo_(keySystem, initData)];
} else {
goog.asserts.assert(
callback, 'ContentProtection callback is required');
return callback(element.node) || [];
}
}).reduce(Functional.collapseArrays, []);
};
/**
* 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
*/
shaka.dash.ContentProtection.parseElements_ = function(elems) {
var Functional = shaka.util.Functional;
return elems.map(
/**
* @param {!Element} elem
* @return {?shaka.dash.ContentProtection.Element}
*/
function(elem) {
/** @type {?string} */
var schemeUri = elem.getAttribute('schemeIdUri');
/** @type {?string} */
var keyId = elem.getAttribute('cenc:default_KID');
/** @type {!Array.<string>} */
var psshs = shaka.util.XmlUtils.findChildren(elem, 'cenc: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.indexOf(' ') >= 0) {
throw new shaka.util.Error(
shaka.util.Error.Category.MANIFEST,
shaka.util.Error.Code.DASH_MULTIPLE_KEY_IDS_NOT_SUPPORTED);
}
}
/** @type {!Array.<shakaExtern.InitDataOverride>} */
var init = [];
try {
init = psshs.map(function(pssh) {
/** @type {shakaExtern.InitDataOverride} */
var ret = {
initDataType: 'cenc',
initData: shaka.util.Uint8ArrayUtils.fromBase64(pssh)
};
return ret;
});
} catch (e) {
// Invalid PSSH data, ignore.
throw new shaka.util.Error(
shaka.util.Error.Category.MANIFEST,
shaka.util.Error.Code.DASH_PSSH_BAD_ENCODING);
}
/** @type {shaka.dash.ContentProtection.Element} */
var element = {
node: elem,
schemeUri: schemeUri,
keyId: keyId,
init: (init.length > 0 ? init : null)
};
return element;
}).filter(Functional.isNotNull);
};
|