tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

MediaStreamTrackProcessor-backpressure.https.html (2364B)


      1 <!doctype html>
      2 <html>
      3 <head>
      4  <title>MediaStreamTrackProcessor backpressure</title>
      5  <link rel="help" href="https://w3c.github.io/mediacapture-insertable-streams">
      6 </head>
      7 <body>
      8  <h1 class="instructions">Description</h1>
      9 <p class="instructions">This test checks that MediaStreamTrackProcessor handles backpressure from a WHATWG stream pipeline.</p>
     10 <script src=/resources/testharness.js></script>
     11 <script src=/resources/testharnessreport.js></script>
     12 <script src=/resources/testdriver.js></script>
     13 <script src=/resources/testdriver-vendor.js></script>
     14 <script>
     15 
     16  const height = 240;
     17  const width = 320;
     18 
     19  const inputCanvas = new OffscreenCanvas(width, height);
     20  const inputCtx = inputCanvas.getContext('2d', {alpha: false});
     21  inputCtx.fillStyle = 'black';
     22  inputCtx.fillRect(0, 0, width, height);
     23 
     24  const frameDuration = 40;
     25 
     26  function makeUniformVideoFrame(timestamp) {
     27    return new VideoFrame(inputCanvas, {timestamp, alpha: 'discard'});
     28  }
     29 
     30  promise_test(async t => {
     31    // TODO: use  "new VideoTrackGenerator"
     32    const generator = new MediaStreamTrackGenerator({kind: 'video'});
     33    t.add_cleanup(() => generator.stop());
     34 
     35    // Write frames for the duration of the test.
     36    const writer = generator.writable.getWriter();
     37    let timestamp = 0;
     38    const intervalId = setInterval(
     39      t.step_func(async () => {
     40        if (generator.readyState === 'live') {
     41          timestamp++;
     42          await writer.write(makeUniformVideoFrame(timestamp));
     43        }
     44      }),
     45      frameDuration);
     46    t.add_cleanup(() => clearInterval(intervalId));
     47    t.step_timeout(function() {
     48      clearInterval(intervalId);
     49      generator.stop();
     50    }, 2000);
     51    const processor = new MediaStreamTrackProcessor({track: generator});
     52    let ts = 1;
     53    await processor.readable.pipeTo(new WritableStream({
     54      async write(frame) {
     55        if (ts === 1) {
     56          assert_equals(frame.timestamp, ts, "Timestamp mismatch");
     57        } else {
     58          assert_greater_than_equal(frame.timestamp, ts, "Backpressure should have resulted in skipping at least 3 frames");
     59        }
     60        frame.close();
     61        ts+=3;
     62        // Wait the equivalent of 3 frames
     63        return new Promise((res) => t.step_timeout(res, 3*frameDuration));
     64      }
     65    }));
     66  }, "Tests that backpressure forces MediaStreamTrackProcess to skip frames");
     67 </script>
     68 </body>
     69 </html>