summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webaudio/the-audio-api/the-audiocontext-interface/processing-after-resume.https.html
blob: e000ab124fefa6f0eea4e5517d04436428c0cd8c (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
<!doctype html>
<title>Test consistency of processing after resume()</title>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script>
const get_node_and_reply = (context) => {
  const node = new AudioWorkletNode(context, 'port-processor');
  return new Promise((resolve) => {
    node.port.onmessage = (event) => resolve({node: node, reply: event.data});
  });
};
const ping_for_reply = (node) => {
  return new Promise((resolve) => {
    node.port.onmessage = (event) => resolve(event.data);
    node.port.postMessage('ping');
  });
};
const assert_consistent = (constructReply, pong, expectedPongTime, name) => {
  const blockSize = 128;
  assert_equals(pong.timeStamp, expectedPongTime, `${name} pong time`);
  assert_equals(pong.processCallCount * blockSize,
                pong.currentFrame - constructReply.currentFrame,
                `${name} processed frame count`);
};
const modulePath = '/webaudio/the-audio-api/' +
    'the-audioworklet-interface/processors/port-processor.js';

promise_test(async () => {
  const realtime = new AudioContext();
  await realtime.audioWorklet.addModule(modulePath);
  await realtime.suspend();
  const timeBeforeResume = realtime.currentTime;
  // Two AudioWorkletNodes are constructed.
  // node1 is constructed before and node2 after the resume() call.
  const construct1 = get_node_and_reply(realtime);
  const resume = realtime.resume();
  const construct2 = get_node_and_reply(realtime);
  const {node: node1, reply: constructReply1} = await construct1;
  assert_equals(constructReply1.timeStamp, timeBeforeResume,
                'construct time before resume');
  const {node: node2, reply: constructReply2} = await construct2;
  assert_greater_than_equal(constructReply2.timeStamp, timeBeforeResume,
                'construct time after resume');
  await resume;
  // Suspend the context to freeze time and check that the processing for each
  // node matches the elapsed time.
  await realtime.suspend();
  const timeAfterSuspend = realtime.currentTime;
  const pong1 = await ping_for_reply(node1);
  const pong2 = await ping_for_reply(node2);
  assert_consistent(constructReply1, pong1, timeAfterSuspend, 'node1');
  assert_consistent(constructReply2, pong2, timeAfterSuspend, 'node2');
  assert_equals(pong1.currentFrame, pong2.currentFrame, 'currentFrame matches');
});
</script>