summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/mediacapture-insertable-streams/MediaStreamTrackProcessor-backpressure.worker.js
blob: 9e03e650e0d7ecda53498632a8a1cff700c33bb7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// META: title=MediaStreamTrackProcessor backpressure tests.

importScripts("/resources/testharness.js");

const height = 240;
const width = 320;

const inputCanvas = new OffscreenCanvas(width, height);
const inputCtx = inputCanvas.getContext('2d', {alpha: false});
inputCtx.fillStyle = 'black';
inputCtx.fillRect(0, 0, width, height);

const frameDuration = 40;

function makeUniformVideoFrame(timestamp) {
  return new VideoFrame(inputCanvas, {timestamp, alpha: 'discard'});
}

promise_test(async t => {
  const generator = new VideoTrackGenerator();
  t.add_cleanup(() => generator.track.stop());

  // Write frames for the duration of the test.
  const writer = generator.writable.getWriter();
  let timestamp = 0;
  const intervalId = setInterval(
    t.step_func(async () => {
      if (generator.readyState === 'live') {
        timestamp++;
        await writer.write(makeUniformVideoFrame(timestamp));
      }
    }),
    frameDuration);
  t.add_cleanup(() => clearInterval(intervalId));
  t.step_timeout(function() {
    clearInterval(intervalId);
    generator.track.stop();
  }, 2000);
  const processor = new MediaStreamTrackProcessor(generator);
  let ts = 1;
  await processor.readable.pipeTo(new WritableStream({
    async write(frame) {
      if (ts === 1) {
        assert_equals(frame.timestamp, ts, "Timestamp mismatch");
      } else {
        assert_greater_than_equal(frame.timestamp, ts, "Backpressure should have resulted in skipping at least 3 frames");
      }
      frame.close();
      ts+=3;
      // Wait the equivalent of 3 frames
      return new Promise((res) => t.step_timeout(res, 3*frameDuration));
    }
  }));
}, "Tests that backpressure forces MediaStreamTrackProcess to skip frames");

done();