chunk-serialization.any.js (2434B)
1 // META: global=window 2 // META: script=/common/media.js 3 // META: script=/webcodecs/utils.js 4 5 var defaultAudioInit = { 6 type: 'key', 7 timestamp: 1234, 8 duration: 9876, 9 data: new Uint8Array([5, 6, 7, 8]) 10 }; 11 12 var defaultVideoInit = { 13 type: 'key', 14 timestamp: 1234, 15 duration: 5678, 16 data: new Uint8Array([9, 10, 11, 12]) 17 }; 18 19 function createDefaultChunk(type, init) { 20 return type == 'audio' ? new EncodedAudioChunk(init) : 21 new EncodedVideoChunk(init); 22 } 23 24 function runTest(t, type) { 25 let defaultInit = type == 'audio' ? defaultAudioInit : defaultVideoInit; 26 let originalData = createDefaultChunk(type, defaultInit); 27 28 let channel = new MessageChannel(); 29 let localPort = channel.port1; 30 let externalPort = channel.port2; 31 32 externalPort.onmessage = t.step_func((e) => { 33 let newData = e.data; 34 35 // We should have a valid deserialized buffer. 36 assert_equals(newData.type, defaultInit.type, 'type'); 37 assert_equals(newData.duration, defaultInit.duration, 'duration'); 38 assert_equals(newData.timestamp, defaultInit.timestamp, 'timestamp'); 39 assert_equals( 40 newData.byteLength, defaultInit.data.byteLength, 'byteLength'); 41 42 const originalData_copyDest = new Uint8Array(defaultInit.data); 43 const newData_copyDest = new Uint8Array(defaultInit.data); 44 45 originalData.copyTo(originalData_copyDest); 46 newData.copyTo(newData_copyDest); 47 48 for (var i = 0; i < newData_copyDest.length; ++i) { 49 assert_equals( 50 newData_copyDest[i], originalData_copyDest[i], `data (i=${i})`); 51 } 52 53 externalPort.postMessage('Done'); 54 }) 55 56 localPort.onmessage = t.step_func_done((e) => { 57 assert_equals(originalData.type, defaultInit.type, 'type'); 58 assert_equals(originalData.duration, defaultInit.duration, 'duration'); 59 assert_equals(originalData.timestamp, defaultInit.timestamp, 'timestamp'); 60 assert_equals( 61 originalData.byteLength, defaultInit.data.byteLength, 'byteLength'); 62 }) 63 64 localPort.postMessage(originalData); 65 } 66 67 async_test(t => { 68 runTest(t, 'audio'); 69 }, 'Verify EncodedAudioChunk is serializable.'); 70 71 72 async_test(t => { 73 runTest(t, 'video'); 74 }, 'Verify EncodedVideoChunk is serializable.'); 75 76 test(() => { 77 const chunk = createDefaultChunk("video", defaultVideoInit); 78 if (window.history) 79 assert_throws_dom("DataCloneError", () => history.pushState({ chunk }, null)); 80 }, "Verify EncodedVideoChunk cannot be stored");