MediaStreamTrackProcessor-backpressure.worker.js (1735B)
1 // META: title=MediaStreamTrackProcessor backpressure tests. 2 3 importScripts("/resources/testharness.js"); 4 5 const height = 240; 6 const width = 320; 7 8 const inputCanvas = new OffscreenCanvas(width, height); 9 const inputCtx = inputCanvas.getContext('2d', {alpha: false}); 10 inputCtx.fillStyle = 'black'; 11 inputCtx.fillRect(0, 0, width, height); 12 13 const frameDuration = 40; 14 15 function makeUniformVideoFrame(timestamp) { 16 return new VideoFrame(inputCanvas, {timestamp, alpha: 'discard'}); 17 } 18 19 promise_test(async t => { 20 const generator = new VideoTrackGenerator(); 21 t.add_cleanup(() => generator.track.stop()); 22 23 // Write frames for the duration of the test. 24 const writer = generator.writable.getWriter(); 25 let timestamp = 0; 26 const intervalId = setInterval( 27 t.step_func(async () => { 28 if (generator.readyState === 'live') { 29 timestamp++; 30 await writer.write(makeUniformVideoFrame(timestamp)); 31 } 32 }), 33 frameDuration); 34 t.add_cleanup(() => clearInterval(intervalId)); 35 t.step_timeout(function() { 36 clearInterval(intervalId); 37 generator.track.stop(); 38 }, 2000); 39 const processor = new MediaStreamTrackProcessor(generator); 40 let ts = 1; 41 await processor.readable.pipeTo(new WritableStream({ 42 async write(frame) { 43 if (ts === 1) { 44 assert_equals(frame.timestamp, ts, "Timestamp mismatch"); 45 } else { 46 assert_greater_than_equal(frame.timestamp, ts, "Backpressure should have resulted in skipping at least 3 frames"); 47 } 48 frame.close(); 49 ts+=3; 50 // Wait the equivalent of 3 frames 51 return new Promise((res) => t.step_timeout(res, 3*frameDuration)); 52 } 53 })); 54 }, "Tests that backpressure forces MediaStreamTrackProcess to skip frames"); 55 56 done();