summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webaudio/the-audio-api/the-audioworklet-interface/processors/array-check-processor.js
blob: d6eeff3d15515e7ad534be90c25c1a13b3be927f (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/**
 * @class ArrayFrozenProcessor
 * @extends AudioWorkletProcessor
 */
class ArrayFrozenProcessor extends AudioWorkletProcessor {
  constructor() {
    super();
    this._messageSent = false;
  }

  process(inputs, outputs, parameters) {
    const input = inputs[0];
    const output = outputs[0];

    if (!this._messageSent) {
      this.port.postMessage({
        inputLength: input.length,
        isInputFrozen: Object.isFrozen(inputs) && Object.isFrozen(input),
        outputLength: output.length,
        isOutputFrozen: Object.isFrozen(outputs) && Object.isFrozen(output)
      });
      this._messageSent = true;
    }

    return false;
  }
}

/**
 * @class ArrayTransferProcessor
 * @extends AudioWorkletProcessor
 */
class ArrayTransferProcessor extends AudioWorkletProcessor {
  constructor() {
    super();
    this._messageSent = false;
  }

  process(inputs, outputs, parameters) {
    const input = inputs[0];
    const output = outputs[0];

    if (!this._messageSent) {
      try {
        // Transferring Array objects should NOT work.
        this.port.postMessage({
          inputs, input, inputChannel: input[0],
          outputs, output, outputChannel: output[0]
        }, [inputs, input, inputs[0], outputs, output, output[0]]);
        // Hence, the following must NOT be reached.
        this.port.postMessage({
          type: 'assertion',
          success: false,
          message: 'Transferring inputs/outputs, an individual input/output ' +
              'array, or a channel Float32Array MUST fail, but succeeded.'
        });
      } catch (error) {
        this.port.postMessage({
          type: 'assertion',
          success: true,
          message: 'Transferring inputs/outputs, an individual input/output ' +
              'array, or a channel Float32Array is not allowed as expected.'
        });
      }

      try {
        // Transferring ArrayBuffers should work.
        this.port.postMessage(
          {inputChannel: input[0], outputChannel: output[0]},
          [input[0].buffer, output[0].buffer]);
        this.port.postMessage({
          type: 'assertion',
          success: true,
          message: 'Transferring ArrayBuffers was successful as expected.'
        });
      } catch (error) {
        // This must NOT be reached.
        this.port.postMessage({
          type: 'assertion',
          success: false,
          message: 'Transferring ArrayBuffers unexpectedly failed.'
        });
      }

      this.port.postMessage({done: true});
      this._messageSent = true;
    }

    return false;
  }
}

registerProcessor('array-frozen-processor', ArrayFrozenProcessor);
registerProcessor('array-transfer-processor', ArrayTransferProcessor);