playback-temporary-encrypted-clear-segmented-sources.js (4799B)
1 function runTest(configEncrypted,configClear,qualifier) { 2 3 var testname = testnamePrefix(qualifier, configEncrypted.keysystem) 4 + ', temporary, ' 5 + /video\/([^;]*)/.exec(configEncrypted.videoType)[1] 6 + ', playback, encrypted and clear sources in separate segments'; 7 8 var configuration = { initDataTypes: [ configEncrypted.initDataType ], 9 audioCapabilities: [ { contentType: configEncrypted.audioType } ], 10 videoCapabilities: [ { contentType: configEncrypted.videoType } ], 11 sessionTypes: [ 'temporary' ] }; 12 13 async_test(function(test) { 14 var didAppendEncrypted = false, 15 _video = configEncrypted.video, 16 _mediaKeys, 17 _mediaKeySession, 18 _mediaSource, 19 _sourceBuffer; 20 21 function onFailure(error) { 22 forceTestFailureFromPromise(test, error); 23 } 24 25 function onVideoError(event) { 26 var message = (_video.error || {}).message || 'Got unknown error from <video>'; 27 forceTestFailureFromPromise(test, new Error(message)); 28 } 29 30 function onMessage(event) { 31 assert_equals(event.target, _mediaKeySession); 32 assert_true(event instanceof window.MediaKeyMessageEvent); 33 assert_equals(event.type, 'message'); 34 assert_in_array(event.messageType, ['license-request', 'individualization-request']); 35 36 configEncrypted.messagehandler(event.messageType, event.message).then(function(response) { 37 return _mediaKeySession.update(response); 38 }).catch(onFailure); 39 } 40 41 function onEncrypted(event) { 42 assert_equals(event.target, _video); 43 assert_true(event instanceof window.MediaEncryptedEvent); 44 assert_equals(event.type, 'encrypted'); 45 46 var initDataType = configEncrypted.initDataType || event.initDataType; 47 var initData = configEncrypted.initData || event.initData; 48 49 _mediaKeySession = _mediaKeys.createSession('temporary'); 50 waitForEventAndRunStep('message', _mediaKeySession, onMessage, test); 51 _mediaKeySession.generateRequest(initDataType, initData).catch(onFailure); 52 } 53 54 function onPlaying(event) { 55 // Not using waitForEventAndRunStep() to avoid too many 56 // EVENT(onTimeUpdate) logs. 57 _video.addEventListener('timeupdate', onTimeupdate, true); 58 } 59 60 function onTimeupdate(event) { 61 if (_video.currentTime > (configEncrypted.duration || 1) + (configClear.duration || 1)) { 62 _video.pause(); 63 test.done(); 64 } 65 if (_video.currentTime > 1 && !didAppendEncrypted) { 66 didAppendEncrypted = true; 67 _sourceBuffer.timestampOffset = configClear.duration; 68 fetchAndAppend(configEncrypted.videoPath).then(function() { 69 _mediaSource.endOfStream(); 70 }).catch(onFailure); 71 } 72 } 73 74 function fetchAndAppend(path) { 75 return fetch(path).then(function(response) { 76 if (!response.ok) throw new Error('Resource fetch failed'); 77 return response.arrayBuffer(); 78 }).then(function(data) { 79 return new Promise(function(resolve, reject) { 80 _sourceBuffer.appendBuffer(data); 81 _sourceBuffer.addEventListener('updateend', resolve); 82 _sourceBuffer.addEventListener('error', reject); 83 }); 84 }); 85 } 86 87 _video.addEventListener('error', onVideoError); 88 navigator.requestMediaKeySystemAccess(configEncrypted.keysystem, [configuration]).then(function(access) { 89 return access.createMediaKeys(); 90 }).then(function(mediaKeys) { 91 _mediaKeys = mediaKeys; 92 return _video.setMediaKeys(_mediaKeys); 93 }).then(function(){ 94 waitForEventAndRunStep('encrypted', _video, onEncrypted, test); 95 waitForEventAndRunStep('playing', _video, onPlaying, test); 96 97 return new Promise(function(resolve, reject) { 98 _mediaSource = new MediaSource(); 99 _mediaSource.addEventListener('sourceopen', resolve); 100 _video.src = URL.createObjectURL(_mediaSource); 101 }); 102 }).then(function() { 103 _sourceBuffer = _mediaSource.addSourceBuffer(configEncrypted.videoType); 104 return fetchAndAppend(configClear.videoPath); 105 }).then(function() { 106 _video.play(); 107 }).catch(onFailure); 108 }, testname); 109 }