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
|
function testnamePrefix( qualifier, keysystem ) {
return ( qualifier || '' ) + ( keysystem === 'org.w3.clearkey' ? keysystem : 'drm' );
}
function getInitData(initDataType) {
// FIXME: This is messed up, because here we are hard coding the key ids for the different content
// that we use for clearkey testing: webm and mp4. For keyids we return the mp4 one
//
// The content used with the DRM today servers has a different key id altogether
if (initDataType == 'webm') {
return new Uint8Array([
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
]);
}
if (initDataType == 'cenc') {
return new Uint8Array([
0x00, 0x00, 0x00, 0x34, // size
0x70, 0x73, 0x73, 0x68, // 'pssh'
0x01, // version = 1
0x00, 0x00, 0x00, // flags
0x10, 0x77, 0xEF, 0xEC, 0xC0, 0xB2, 0x4D, 0x02, // Common SystemID
0xAC, 0xE3, 0x3C, 0x1E, 0x52, 0xE2, 0xFB, 0x4B,
0x00, 0x00, 0x00, 0x01, // key count
0x00, 0x00, 0x00, 0x00, 0x03, 0xd2, 0xfc, 0x41, // key id
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00 // datasize
]);
}
if (initDataType == 'keyids') {
var keyId = new Uint8Array([
0x00, 0x00, 0x00, 0x00, 0x03, 0xd2, 0xfc, 0x41,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
]);
return stringToUint8Array(createKeyIDs(keyId));
}
throw 'initDataType ' + initDataType + ' not supported.';
}
function stringToUint8Array(str)
{
var result = new Uint8Array(str.length);
for(var i = 0; i < str.length; i++) {
result[i] = str.charCodeAt(i);
}
return result;
}
// Encodes |data| into base64url string. There is no '=' padding, and the
// characters '-' and '_' must be used instead of '+' and '/', respectively.
function base64urlEncode(data) {
var result = btoa(String.fromCharCode.apply(null, data));
return result.replace(/=+$/g, '').replace(/\+/g, "-").replace(/\//g, "_");
}
// Decode |encoded| using base64url decoding.
function base64urlDecode(encoded) {
return atob(encoded.replace(/\-/g, "+").replace(/\_/g, "/"));
}
// Decode |encoded| using base64 to a Uint8Array
function base64DecodeToUnit8Array(encoded) {
return new Uint8Array( atob( encoded ).split('').map( function(c){return c.charCodeAt(0);} ) );
}
// Clear Key can also support Key IDs Initialization Data.
// ref: http://w3c.github.io/encrypted-media/keyids-format.html
// Each parameter is expected to be a key id in an Uint8Array.
function createKeyIDs() {
var keyIds = '{"kids":["';
for (var i = 0; i < arguments.length; i++) {
if (i != 0) keyIds += '","';
keyIds += base64urlEncode(arguments[i]);
}
keyIds += '"]}';
return keyIds;
}
function getSupportedKeySystem() {
var userAgent = navigator.userAgent.toLowerCase();
var keysystem = undefined;
if (userAgent.indexOf('edge') > -1 ) {
keysystem = 'com.microsoft.playready';
} else if ( userAgent.indexOf('chrome') > -1 || userAgent.indexOf('firefox') > -1 ) {
keysystem = 'com.widevine.alpha';
}
return keysystem;
}
function waitForEventAndRunStep(eventName, element, func, stepTest)
{
var eventCallback = function(event) {
if (func)
func(event);
}
element.addEventListener(eventName, stepTest.step_func(eventCallback), true);
}
function waitForEvent(eventName, element) {
return new Promise(function(resolve) {
element.addEventListener(eventName, resolve, true);
})
}
var consoleDiv = null;
function consoleWrite(text)
{
if (!consoleDiv && document.body) {
consoleDiv = document.createElement('div');
document.body.appendChild(consoleDiv);
}
var span = document.createElement('span');
span.appendChild(document.createTextNode(text));
span.appendChild(document.createElement('br'));
consoleDiv.appendChild(span);
}
function forceTestFailureFromPromise(test, error, message)
{
// Promises convert exceptions into rejected Promises. Since there is
// currently no way to report a failed test in the test harness, errors
// are reported using force_timeout().
if (message)
consoleWrite(message + ': ' + error.message);
else if (error)
consoleWrite(error);
test.force_timeout();
test.done();
}
// Returns an array of audioCapabilities that includes entries for a set of
// codecs that should cover all user agents.
function getPossibleAudioCapabilities()
{
return [
{ contentType: 'audio/mp4; codecs="mp4a.40.2"' },
{ contentType: 'audio/webm; codecs="opus"' },
];
}
// Returns a trivial MediaKeySystemConfiguration that should be accepted,
// possibly as a subset of the specified capabilities, by all user agents.
function getSimpleConfiguration()
{
return [ {
initDataTypes : [ 'webm', 'cenc', 'keyids' ],
audioCapabilities: getPossibleAudioCapabilities()
} ];
}
// Returns a MediaKeySystemConfiguration for |initDataType| that should be
// accepted, possibly as a subset of the specified capabilities, by all
// user agents.
function getSimpleConfigurationForInitDataType(initDataType)
{
return [ {
initDataTypes: [ initDataType ],
audioCapabilities: getPossibleAudioCapabilities()
} ];
}
// Returns a promise that is fulfilled with true if |initDataType| is supported,
// by keysystem or false if not.
function isInitDataTypeSupported(keysystem,initDataType)
{
return navigator.requestMediaKeySystemAccess(
keysystem, getSimpleConfigurationForInitDataType(initDataType))
.then(function() { return true; }, function() { return false; });
}
function getSupportedInitDataTypes( keysystem )
{
return [ 'cenc', 'keyids', 'webm' ].filter( isInitDataTypeSupported.bind( null, keysystem ) );
}
function arrayBufferAsString(buffer)
{
var array = [];
Array.prototype.push.apply( array, new Uint8Array( buffer ) );
return '0x' + array.map( function( x ) { return x < 16 ? '0'+x.toString(16) : x.toString(16); } ).join('');
}
function dumpKeyStatuses(keyStatuses)
{
var userAgent = navigator.userAgent.toLowerCase();
if (userAgent.indexOf('edge') === -1) {
consoleWrite("for (var entry of keyStatuses)");
for (var entry of keyStatuses) {
consoleWrite(arrayBufferAsString(entry[0]) + ": " + entry[1]);
}
consoleWrite("for (var keyId of keyStatuses.keys())");
for (var keyId of keyStatuses.keys()) {
consoleWrite(arrayBufferAsString(keyId));
}
consoleWrite("for (var status of keyStatuses.values())");
for (var status of keyStatuses.values()) {
consoleWrite(status);
}
consoleWrite("for (var entry of keyStatuses.entries())");
for (var entry of keyStatuses.entries()) {
consoleWrite(arrayBufferAsString(entry[0]) + ": " + entry[1]);
}
consoleWrite("keyStatuses.forEach()");
keyStatuses.forEach(function(status, keyId) {
consoleWrite(arrayBufferAsString(keyId) + ": " + status);
});
} else {
consoleWrite("keyStatuses.forEach()");
keyStatuses.forEach(function(keyId, status) {
consoleWrite(arrayBufferAsString(keyId) + ": " + status);
});
}
}
// Verify that |keyStatuses| contains just the keys in |keys.expected|
// and none of the keys in |keys.unexpected|. All keys should have status
// 'usable'. Example call: verifyKeyStatuses(mediaKeySession.keyStatuses,
// { expected: [key1], unexpected: [key2] });
function verifyKeyStatuses(keyStatuses, keys)
{
var expected = keys.expected || [];
var unexpected = keys.unexpected || [];
// |keyStatuses| should have same size as number of |keys.expected|.
assert_equals(keyStatuses.size, expected.length, "keystatuses should have expected size");
// All |keys.expected| should be found.
expected.map(function(key) {
assert_true(keyStatuses.has(key), "keystatuses should have the expected keys");
assert_equals(keyStatuses.get(key), 'usable', "keystatus value should be 'usable'");
});
// All |keys.unexpected| should not be found.
unexpected.map(function(key) {
assert_false(keyStatuses.has(key), "keystatuses should not have unexpected keys");
assert_equals(keyStatuses.get(key), undefined, "keystatus for unexpected key should be undefined");
});
}
// This function checks that calling |testCase.func| returns a
// rejected Promise with the error.name equal to
// |testCase.exception|.
function test_exception(testCase /*...*/) {
var func = testCase.func;
var exception = testCase.exception;
var args = Array.prototype.slice.call(arguments, 1);
// Currently blink throws for TypeErrors rather than returning
// a rejected promise (http://crbug.com/359386).
// FIXME: Remove try/catch once they become failed promises.
try {
return func.apply(null, args).then(
function (result) {
assert_unreached(format_value(func));
},
function (error) {
assert_equals(error.name, exception, format_value(func));
assert_not_equals(error.message, "", format_value(func));
}
);
} catch (e) {
// Only allow 'TypeError' exceptions to be thrown.
// Everything else should be a failed promise.
assert_equals('TypeError', exception, format_value(func));
assert_equals(e.name, exception, format_value(func));
}
}
|