How to use getSimpleConfigurationForInitDataType method in wpt

Best JavaScript code snippet using wpt

encrypted-media-utils.js

Source:encrypted-media-utils.js Github

copy

Full Screen

...14// or false if not.15function isInitDataTypeSupported(initDataType)16{17 return navigator.requestMediaKeySystemAccess(18 "org.w3.clearkey", getSimpleConfigurationForInitDataType(initDataType))19 .then(function() { return true; }, function() { return false; });20}21function getInitData(initDataType)22{23 if (initDataType == 'webm') {24 return new Uint8Array([25 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,26 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F27 ]);28 }29 if (initDataType == 'cenc') {30 return new Uint8Array([31 0x00, 0x00, 0x00, 0x00, // size = 032 0x70, 0x73, 0x73, 0x68, // 'pssh'33 0x01, // version = 134 0x00, 0x00, 0x00, // flags35 0x10, 0x77, 0xEF, 0xEC, 0xC0, 0xB2, 0x4D, 0x02, // Common SystemID36 0xAC, 0xE3, 0x3C, 0x1E, 0x52, 0xE2, 0xFB, 0x4B,37 0x00, 0x00, 0x00, 0x01, // key count38 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, // key39 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,40 0x00, 0x00, 0x00, 0x00 // datasize41 ]);42 }43 if (initDataType == 'keyids') {44 var keyId = new Uint8Array([45 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,46 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F47 ]);48 return stringToUint8Array(createKeyIDs(keyId));49 }50 throw 'initDataType ' + initDataType + ' not supported.';51}52// Returns an array of audioCapabilities that includes entries for a set of53// codecs that should cover all user agents.54function getPossibleAudioCapabilities()55{56 return [57 { contentType: 'audio/mp4; codecs="mp4a.40.2"' },58 { contentType: 'audio/webm; codecs="opus"' },59 ];60}61// Returns a trivial MediaKeySystemConfiguration that should be accepted,62// possibly as a subset of the specified capabilities, by all user agents.63function getSimpleConfiguration()64{65 return [ {66 initDataTypes : [ 'webm', 'cenc', 'keyids' ],67 audioCapabilities: getPossibleAudioCapabilities()68 } ];69}70// Returns a MediaKeySystemConfiguration for |initDataType| that should be71// accepted, possibly as a subset of the specified capabilities, by all72// user agents.73function getSimpleConfigurationForInitDataType(initDataType)74{75 return [ {76 initDataTypes: [ initDataType ],77 audioCapabilities: getPossibleAudioCapabilities()78 } ];79}80// Returns a MediaKeySystemConfiguration for |mediaFile| that specifies81// both audio and video capabilities for the specified file..82function getConfigurationForFile(mediaFile)83{84 if (mediaFile.toLowerCase().endsWith('webm')) {85 return [ {86 initDataTypes: [ 'webm' ],87 audioCapabilities: [ { contentType: 'audio/webm; codecs="opus"' } ],88 videoCapabilities: [ { contentType: 'video/webm; codecs="vp8"' } ]89 } ];90 }91 // NOTE: Supporting other mediaFormats is not currently implemented as92 // Chromium only tests with WebM files.93 throw 'mediaFile ' + mediaFile + ' not supported.';94}95function waitForEventAndRunStep(eventName, element, func, stepTest)96{97 var eventCallback = function(event) {98 if (func)99 func(event);100 }101 if (stepTest)102 eventCallback = stepTest.step_func(eventCallback);103 element.addEventListener(eventName, eventCallback, true);104}105// Copied from LayoutTests/resources/js-test.js.106// See it for details of why this is necessary.107function asyncGC(callback)108{109 GCController.collectAll();110 setTimeout(callback, 0);111}112function createGCPromise()113{114 // Run gc() as a promise.115 return new Promise(116 function(resolve, reject) {117 asyncGC(resolve);118 });119}120function delayToAllowEventProcessingPromise()121{122 return new Promise(123 function(resolve, reject) {124 setTimeout(resolve, 0);125 });126}127function stringToUint8Array(str)128{129 var result = new Uint8Array(str.length);130 for(var i = 0; i < str.length; i++) {131 result[i] = str.charCodeAt(i);132 }133 return result;134}135function arrayBufferAsString(buffer)136{137 // MediaKeySession.keyStatuses iterators return an ArrayBuffer,138 // so convert it into a printable string.139 return String.fromCharCode.apply(null, new Uint8Array(buffer));140}141function dumpKeyStatuses(keyStatuses)142{143 consoleWrite("for (var entry of keyStatuses)");144 for (var entry of keyStatuses) {145 consoleWrite(arrayBufferAsString(entry[0]) + ": " + entry[1]);146 }147 consoleWrite("for (var keyId of keyStatuses.keys())");148 for (var keyId of keyStatuses.keys()) {149 consoleWrite(arrayBufferAsString(keyId));150 }151 consoleWrite("for (var status of keyStatuses.values())");152 for (var status of keyStatuses.values()) {153 consoleWrite(status);154 }155 consoleWrite("for (var entry of keyStatuses.entries())");156 for (var entry of keyStatuses.entries()) {157 consoleWrite(arrayBufferAsString(entry[0]) + ": " + entry[1]);158 }159 consoleWrite("keyStatuses.forEach()");160 keyStatuses.forEach(function(status, keyId) {161 consoleWrite(arrayBufferAsString(keyId) + ": " + status);162 });163}164// Verify that |keyStatuses| contains just the keys in |keys.expected|165// and none of the keys in |keys.unexpected|. All keys should have status166// 'usable'. Example call: verifyKeyStatuses(mediaKeySession.keyStatuses,167// { expected: [key1], unexpected: [key2] });168function verifyKeyStatuses(keyStatuses, keys)169{170 var expected = keys.expected || [];171 var unexpected = keys.unexpected || [];172 // |keyStatuses| should have same size as number of |keys.expected|.173 assert_equals(keyStatuses.size, expected.length);174 // All |keys.expected| should be found.175 expected.map(function(key) {176 assert_true(keyStatuses.has(key));177 assert_equals(keyStatuses.get(key), 'usable');178 });179 // All |keys.unexpected| should not be found.180 unexpected.map(function(key) {181 assert_false(keyStatuses.has(key));182 assert_equals(keyStatuses.get(key), undefined);183 });184}185// Encodes |data| into base64url string. There is no '=' padding, and the186// characters '-' and '_' must be used instead of '+' and '/', respectively.187function base64urlEncode(data)188{189 var result = btoa(String.fromCharCode.apply(null, data));190 return result.replace(/=+$/g, '').replace(/\+/g, "-").replace(/\//g, "_");191}192// Decode |encoded| using base64url decoding.193function base64urlDecode(encoded)194{195 return atob(encoded.replace(/\-/g, "+").replace(/\_/g, "/"));196}197// For Clear Key, the License Format is a JSON Web Key (JWK) Set, which contains198// a set of cryptographic keys represented by JSON. These helper functions help199// wrap raw keys into a JWK set.200// See:201// https://w3c.github.io/encrypted-media/#clear-key-license-format202// http://tools.ietf.org/html/draft-ietf-jose-json-web-key203//204// Creates a JWK from raw key ID and key.205// |keyId| and |key| are expected to be ArrayBufferViews, not base64-encoded.206function createJWK(keyId, key)207{208 var jwk = '{"kty":"oct","alg":"A128KW","kid":"';209 jwk += base64urlEncode(keyId);210 jwk += '","k":"';211 jwk += base64urlEncode(key);212 jwk += '"}';213 return jwk;214}215// Creates a JWK Set from multiple JWKs.216function createJWKSet()217{218 var jwkSet = '{"keys":[';219 for (var i = 0; i < arguments.length; i++) {220 if (i != 0)221 jwkSet += ',';222 jwkSet += arguments[i];223 }224 jwkSet += ']}';225 return jwkSet;226}227// Clear Key can also support Key IDs Initialization Data.228// ref: http://w3c.github.io/encrypted-media/keyids-format.html229// Each parameter is expected to be a key id in an Uint8Array.230function createKeyIDs()231{232 var keyIds = '{"kids":["';233 for (var i = 0; i < arguments.length; i++) {234 if (i != 0)235 keyIds += '","';236 keyIds += base64urlEncode(arguments[i]);237 }238 keyIds += '"]}';239 return keyIds;240}241function forceTestFailureFromPromise(test, error, message)242{243 // Promises convert exceptions into rejected Promises. Since there is244 // currently no way to report a failed test in the test harness, errors245 // are reported using force_timeout().246 if (message)247 consoleWrite(message + ': ' + error.message);248 else if (error)249 consoleWrite(error);250 test.force_timeout();251 test.done();252}253function extractSingleKeyIdFromMessage(message)254{255 var json = JSON.parse(String.fromCharCode.apply(null, new Uint8Array(message)));256 // Decode the first element of 'kids'.257 assert_equals(1, json.kids.length);258 var decoded_key = base64urlDecode(json.kids[0]);259 // Convert to an Uint8Array and return it.260 return stringToUint8Array(decoded_key);261}262// Create a MediaKeys object for Clear Key with 1 session. KeyId and key263// required for the video are already known and provided. Returns a promise264// that resolves to the MediaKeys object created.265function createMediaKeys(keyId, key)266{267 var mediaKeys;268 var mediaKeySession;269 var request = stringToUint8Array(createKeyIDs(keyId));270 var jwkSet = stringToUint8Array(createJWKSet(createJWK(keyId, key)));271 return navigator.requestMediaKeySystemAccess('org.w3.clearkey', getSimpleConfigurationForInitDataType('keyids')).then(function(access) {272 return access.createMediaKeys();273 }).then(function(result) {274 mediaKeys = result;275 mediaKeySession = mediaKeys.createSession();276 return mediaKeySession.generateRequest('keyids', request);277 }).then(function() {278 return mediaKeySession.update(jwkSet);279 }).then(function() {280 return Promise.resolve(mediaKeys);281 });282}283// Play the specified |content| on |video|. Returns a promise that is resolved284// after the video plays for |duration| seconds.285function playVideoAndWaitForTimeupdate(video, content, duration)...

Full Screen

Full Screen

check-initdata-type.js

Source:check-initdata-type.js Github

copy

Full Screen

...5 return isInitDataTypeSupported(initDataType).then(function(result) {6 // If |initDataType| is not supported, simply succeed.7 if (!result)8 return Promise.resolve('Not supported');9 return navigator.requestMediaKeySystemAccess( config.keysystem, getSimpleConfigurationForInitDataType(initDataType))10 .then(function(access) {11 return access.createMediaKeys();12 }).then(function(mediaKeys) {13 var mediaKeySession = mediaKeys.createSession();14 var initData = getInitData(initDataType);15 return mediaKeySession.generateRequest(initDataType, initData);16 });17 });18 }19 promise_test(function()20 {21 return checkInitDataType('webm');22 }, testnamePrefix( qualifier, config.keysystem ) + ' support for "webm".');23 promise_test(function()...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run wpt automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful