summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webaudio/the-audio-api/the-audioworklet-interface/audioworklet-postmessage-sharedarraybuffer.https.html
blob: a5dd004981157c8791c391fbb71e4aa2620559c8 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<!DOCTYPE html>
<html>
  <head>
    <title>
      Test passing SharedArrayBuffer to an AudioWorklet
    </title>
    <script src="/resources/testharness.js"></script>
    <script src="/resources/testharnessreport.js"></script>
    <script src="/webaudio/resources/audit.js"></script>
  </head>
  <body>
    <script id="layout-test-code">
      let audit = Audit.createTaskRunner();

      let context = new AudioContext();

      let filePath = 'processors/sharedarraybuffer-processor.js';

      audit.define(
          'Test postMessage from AudioWorkletProcessor to AudioWorkletNode',
          (task, should) => {
            let workletNode =
                new AudioWorkletNode(context, 'sharedarraybuffer-processor');

            // After it is created, the worklet will send a new
            // SharedArrayBuffer to the main thread.
            //
            // The worklet will then wait to receive a message from the main
            // thread.
            //
            // When it receives the message, it will check whether it is a
            // SharedArrayBuffer, and send this information back to the main
            // thread.

            workletNode.port.onmessage = (event) => {
              let data = event.data;
              switch (data.state) {
                case 'created':
                  should(
                      data.sab instanceof SharedArrayBuffer,
                      'event.data.sab from worklet is an instance of SharedArrayBuffer')
                      .beTrue();

                  // Send a SharedArrayBuffer back to the worklet.
                  let sab = new SharedArrayBuffer(8);
                  workletNode.port.postMessage(sab);
                  break;

                case 'received message':
                  should(data.isSab, 'event.data from main thread is an instance of SharedArrayBuffer')
                      .beTrue();
                  task.done();
                  break;

                default:
                  should(false,
                         `Got unexpected message from worklet: ${data.state}`)
                      .beTrue();
                  task.done();
                  break;
              }
            };

            workletNode.port.onmessageerror = (event) => {
              should(false, 'Got messageerror from worklet').beTrue();
              task.done();
            };
          });

      context.audioWorklet.addModule(filePath).then(() => {
        audit.run();
      });
    </script>
  </body>
</html>