-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
fairplay_utils.js
367 lines (336 loc) · 11.3 KB
/
fairplay_utils.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
/*! @license
* Shaka Player
* Copyright 2016 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
goog.provide('shaka.util.FairPlayUtils');
goog.require('goog.Uri');
goog.require('goog.asserts');
goog.require('shaka.net.NetworkingEngine');
goog.require('shaka.util.BufferUtils');
goog.require('shaka.util.Error');
goog.require('shaka.util.StringUtils');
goog.require('shaka.util.Uint8ArrayUtils');
/**
* @summary A set of FairPlay utility functions.
* @export
*/
shaka.util.FairPlayUtils = class {
/**
* Check if FairPlay is supported.
*
* @return {!Promise.<boolean>}
* @export
*/
static async isFairPlaySupported() {
const config = {
initDataTypes: ['cenc', 'sinf', 'skd'],
videoCapabilities: [
{
contentType: 'video/mp4; codecs="avc1.42E01E"',
},
],
};
try {
await navigator.requestMediaKeySystemAccess('com.apple.fps', [config]);
return true;
} catch (err) {
return false;
}
}
/**
* Using the default method, extract a content ID from the init data. This is
* based on the FairPlay example documentation.
*
* @param {!BufferSource} initData
* @return {string}
* @export
*/
static defaultGetContentId(initData) {
const uriString = shaka.util.StringUtils.fromBytesAutoDetect(initData);
// The domain of that URI is the content ID according to Apple's FPS
// sample.
const uri = new goog.Uri(uriString);
return uri.getDomain();
}
/**
* Transforms the init data buffer using the given data. The format is:
*
* <pre>
* [4 bytes] initDataSize
* [initDataSize bytes] initData
* [4 bytes] contentIdSize
* [contentIdSize bytes] contentId
* [4 bytes] certSize
* [certSize bytes] cert
* </pre>
*
* @param {!BufferSource} initData
* @param {!BufferSource|string} contentId
* @param {?BufferSource} cert The server certificate; this will throw if not
* provided.
* @return {!Uint8Array}
* @export
*/
static initDataTransform(initData, contentId, cert) {
if (!cert || !cert.byteLength) {
throw new shaka.util.Error(
shaka.util.Error.Severity.CRITICAL,
shaka.util.Error.Category.DRM,
shaka.util.Error.Code.SERVER_CERTIFICATE_REQUIRED);
}
// From that, we build a new init data to use in the session. This is
// composed of several parts. First, the init data as a UTF-16 sdk:// URL.
// Second, a 4-byte LE length followed by the content ID in UTF-16-LE.
// Third, a 4-byte LE length followed by the certificate.
/** @type {BufferSource} */
let contentIdArray;
if (typeof contentId == 'string') {
contentIdArray =
shaka.util.StringUtils.toUTF16(contentId, /* littleEndian= */ true);
} else {
contentIdArray = contentId;
}
// The init data we get is a UTF-8 string; convert that to a UTF-16 string.
const sdkUri = shaka.util.StringUtils.fromBytesAutoDetect(initData);
const utf16 =
shaka.util.StringUtils.toUTF16(sdkUri, /* littleEndian= */ true);
const rebuiltInitData = new Uint8Array(
12 + utf16.byteLength + contentIdArray.byteLength + cert.byteLength);
let offset = 0;
/** @param {BufferSource} array */
const append = (array) => {
rebuiltInitData.set(shaka.util.BufferUtils.toUint8(array), offset);
offset += array.byteLength;
};
/** @param {BufferSource} array */
const appendWithLength = (array) => {
const view = shaka.util.BufferUtils.toDataView(rebuiltInitData);
const value = array.byteLength;
view.setUint32(offset, value, /* littleEndian= */ true);
offset += 4;
append(array);
};
appendWithLength(utf16);
appendWithLength(contentIdArray);
appendWithLength(cert);
goog.asserts.assert(
offset == rebuiltInitData.length, 'Inconsistent init data length');
return rebuiltInitData;
}
/**
* Basic initDataTransform configuration.
*
* @param {!Uint8Array} initData
* @param {string} initDataType
* @param {?shaka.extern.DrmInfo} drmInfo
* @private
*/
static basicInitDataTransform_(initData, initDataType, drmInfo) {
if (initDataType !== 'skd') {
return initData;
}
const StringUtils = shaka.util.StringUtils;
const FairPlayUtils = shaka.util.FairPlayUtils;
const cert = drmInfo.serverCertificate;
const initDataAsString = StringUtils.fromBytesAutoDetect(initData);
const contentId = initDataAsString.split('skd://').pop();
return FairPlayUtils.initDataTransform(initData, contentId, cert);
}
/**
* Verimatrix initDataTransform configuration.
*
* @param {!Uint8Array} initData
* @param {string} initDataType
* @param {?shaka.extern.DrmInfo} drmInfo
* @export
*/
static verimatrixInitDataTransform(initData, initDataType, drmInfo) {
return shaka.util.FairPlayUtils.basicInitDataTransform_(
initData, initDataType, drmInfo);
}
/**
* EZDRM initDataTransform configuration.
*
* @param {!Uint8Array} initData
* @param {string} initDataType
* @param {?shaka.extern.DrmInfo} drmInfo
* @export
*/
static ezdrmInitDataTransform(initData, initDataType, drmInfo) {
if (initDataType !== 'skd') {
return initData;
}
const StringUtils = shaka.util.StringUtils;
const FairPlayUtils = shaka.util.FairPlayUtils;
const cert = drmInfo.serverCertificate;
const initDataAsString = StringUtils.fromBytesAutoDetect(initData);
const contentId = initDataAsString.split(';').pop();
return FairPlayUtils.initDataTransform(initData, contentId, cert);
}
/**
* Conax initDataTransform configuration.
*
* @param {!Uint8Array} initData
* @param {string} initDataType
* @param {?shaka.extern.DrmInfo} drmInfo
* @export
*/
static conaxInitDataTransform(initData, initDataType, drmInfo) {
if (initDataType !== 'skd') {
return initData;
}
const StringUtils = shaka.util.StringUtils;
const FairPlayUtils = shaka.util.FairPlayUtils;
const cert = drmInfo.serverCertificate;
const initDataAsString = StringUtils.fromBytesAutoDetect(initData);
const skdValue = initDataAsString.split('skd://').pop().split('?').shift();
const stringToArray = (string) => {
// 2 bytes for each char
const buffer = new ArrayBuffer(string.length * 2);
const array = shaka.util.BufferUtils.toUint16(buffer);
for (let i = 0, strLen = string.length; i < strLen; i++) {
array[i] = string.charCodeAt(i);
}
return array;
};
const contentId = stringToArray(window.atob(skdValue));
return FairPlayUtils.initDataTransform(initData, contentId, cert);
}
/**
* ExpressPlay initDataTransform configuration.
*
* @param {!Uint8Array} initData
* @param {string} initDataType
* @param {?shaka.extern.DrmInfo} drmInfo
* @export
*/
static expressplayInitDataTransform(initData, initDataType, drmInfo) {
return shaka.util.FairPlayUtils.basicInitDataTransform_(
initData, initDataType, drmInfo);
}
/**
* Verimatrix FairPlay request.
*
* @param {shaka.net.NetworkingEngine.RequestType} type
* @param {shaka.extern.Request} request
* @param {shaka.extern.RequestContext=} context
* @export
*/
static verimatrixFairPlayRequest(type, request, context) {
if (type !== shaka.net.NetworkingEngine.RequestType.LICENSE) {
return;
}
const body = /** @type {!(ArrayBuffer|ArrayBufferView)} */(request.body);
const originalPayload = shaka.util.BufferUtils.toUint8(body);
const base64Payload = shaka.util.Uint8ArrayUtils.toBase64(originalPayload);
request.headers['Content-Type'] = 'application/x-www-form-urlencoded';
request.body = shaka.util.StringUtils.toUTF8('spc=' + base64Payload);
}
/**
* Set content-type to application/octet-stream in a FairPlay request.
*
* @param {shaka.net.NetworkingEngine.RequestType} type
* @param {shaka.extern.Request} request
* @param {shaka.extern.RequestContext=} context
* @private
*/
static octetStreamFairPlayRequest_(type, request, context) {
if (type !== shaka.net.NetworkingEngine.RequestType.LICENSE) {
return;
}
request.headers['Content-Type'] = 'application/octet-stream';
}
/**
* EZDRM FairPlay request.
*
* @param {shaka.net.NetworkingEngine.RequestType} type
* @param {shaka.extern.Request} request
* @param {shaka.extern.RequestContext=} context
* @export
*/
static ezdrmFairPlayRequest(type, request, context) {
shaka.util.FairPlayUtils.octetStreamFairPlayRequest_(type, request);
}
/**
* Conax FairPlay request.
*
* @param {shaka.net.NetworkingEngine.RequestType} type
* @param {shaka.extern.Request} request
* @param {shaka.extern.RequestContext=} context
* @export
*/
static conaxFairPlayRequest(type, request, context) {
shaka.util.FairPlayUtils.octetStreamFairPlayRequest_(type, request);
}
/**
* ExpressPlay FairPlay request.
*
* @param {shaka.net.NetworkingEngine.RequestType} type
* @param {shaka.extern.Request} request
* @param {shaka.extern.RequestContext=} context
* @export
*/
static expressplayFairPlayRequest(type, request, context) {
shaka.util.FairPlayUtils.octetStreamFairPlayRequest_(type, request);
}
/**
* Common FairPlay response transform for some DRMs providers.
*
* @param {shaka.net.NetworkingEngine.RequestType} type
* @param {shaka.extern.Response} response
* @param {shaka.extern.RequestContext=} context
* @export
*/
static commonFairPlayResponse(type, response, context) {
if (type !== shaka.net.NetworkingEngine.RequestType.LICENSE) {
return;
}
// In Apple's docs, responses can be of the form:
// '\n<ckc>base64encoded</ckc>\n' or 'base64encoded'
// We have also seen responses in JSON format from some of our partners.
// In all of these text-based formats, the CKC data is base64-encoded.
let responseText;
try {
// Convert it to text for further processing.
responseText = shaka.util.StringUtils.fromUTF8(response.data);
} catch (error) {
// Assume it's not a text format of any kind and leave it alone.
return;
}
let licenseProcessing = false;
// Trim whitespace.
responseText = responseText.trim();
// Look for <ckc> wrapper and remove it.
if (responseText.substr(0, 5) === '<ckc>' &&
responseText.substr(-6) === '</ckc>') {
responseText = responseText.slice(5, -6);
licenseProcessing = true;
}
// Look for a JSON wrapper and remove it.
try {
const responseObject = /** @type {!Object} */(JSON.parse(responseText));
if (responseObject['ckc']) {
responseText = responseObject['ckc'];
licenseProcessing = true;
}
if (responseObject['CkcMessage']) {
responseText = responseObject['CkcMessage'];
licenseProcessing = true;
}
if (responseObject['License']) {
responseText = responseObject['License'];
licenseProcessing = true;
}
} catch (err) {
// It wasn't JSON. Fall through with other transformations.
}
if (licenseProcessing) {
// Decode the base64-encoded data into the format the browser expects.
// It's not clear why FairPlay license servers don't just serve this
// directly.
response.data = shaka.util.BufferUtils.toArrayBuffer(
shaka.util.Uint8ArrayUtils.fromBase64(responseText));
}
}
};