summaryrefslogtreecommitdiffstats
path: root/dom/media/webaudio/test/test_audioContextSuspendResumeClose.html
blob: 62bdd3fd54c2f16692f75c200399294bd671790b (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
<!DOCTYPE HTML>
<html>
<head>
  <title>Test suspend, resume and close method of the AudioContext</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="webaudio.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<pre id="test">
<script class="testbody" type="text/javascript">

function tryToCreateNodeOnClosedContext(ctx) {
  is(ctx.state, "closed", "The context is in closed state");

  [ { name: "createBufferSource" },
    { name: "createMediaStreamDestination",
      onOfflineAudioContext: false},
    { name: "createScriptProcessor" },
    { name: "createStereoPanner" },
    { name: "createAnalyser" },
    { name: "createGain" },
    { name: "createDelay" },
    { name: "createBiquadFilter" },
    { name: "createWaveShaper" },
    { name: "createPanner" },
    { name: "createConvolver" },
    { name: "createChannelSplitter" },
    { name: "createChannelMerger" },
    { name: "createDynamicsCompressor" },
    { name: "createOscillator" },
    { name: "createMediaElementSource",
      args: [new Audio()],
      onOfflineAudioContext: false },
    { name: "createMediaStreamSource",
      args: [(new AudioContext()).createMediaStreamDestination().stream],
      onOfflineAudioContext: false } ].forEach(function(e) {

      if (e.onOfflineAudioContext == false &&
          ctx instanceof OfflineAudioContext) {
        return;
      }

      expectNoException(function() {
        ctx[e.name].apply(ctx, e.args);
      }, DOMException.INVALID_STATE_ERR);
    });
}

function loadFile(url, callback) {
  var xhr = new XMLHttpRequest();
  xhr.open("GET", url, true);
  xhr.responseType = "arraybuffer";
  xhr.onload = function() {
    callback(xhr.response);
  };
  xhr.send();
}

// createBuffer, createPeriodicWave and decodeAudioData should work on a context
// that has `state` == "closed"
function tryLegalOpeerationsOnClosedContext(ctx) {
  is(ctx.state, "closed", "The context is in closed state");

  [ { name: "createBuffer",
      args: [1, 44100, 44100] },
    { name: "createPeriodicWave",
      args: [new Float32Array(10), new Float32Array(10)] }
  ].forEach(function(e) {
    expectNoException(function() {
      ctx[e.name].apply(ctx, e.args);
    });
  });
  loadFile("ting-44.1k-1ch.ogg", function(buf) {
    ctx.decodeAudioData(buf).then(function() {
      ok(true, "decodeAudioData on a closed context should work, it did.")
      finish();
    }).catch(function(){
      ok(false, "decodeAudioData on a closed context should work, it did not");
      finish();
    });
  });
}

// Test that MediaStreams that are the output of a suspended AudioContext are
// producing silence
// ac1 produce a sine fed to a MediaStreamAudioDestinationNode
// ac2 is connected to ac1 with a MediaStreamAudioSourceNode, and check that
// there is silence when ac1 is suspended
function testMultiContextOutput() {
  var ac1 = new AudioContext(),
      ac2 = new AudioContext();

  ac1.onstatechange = function() {
    ac1.onstatechange = null;

    var osc1 = ac1.createOscillator(),
        mediaStreamDestination1 = ac1.createMediaStreamDestination();

    var mediaStreamAudioSourceNode2 =
      ac2.createMediaStreamSource(mediaStreamDestination1.stream),
      sp2 = ac2.createScriptProcessor(),
      silentBuffersInARow = 0;


    sp2.onaudioprocess = function() {
      ac1.suspend().then(function() {
        is(ac1.state, "suspended", "ac1 is suspended");
        sp2.onaudioprocess = checkSilence;
      });
      sp2.onaudioprocess = null;
    }

    function checkSilence(e) {
      var input = e.inputBuffer.getChannelData(0);
      var silent = true;
      for (var i = 0; i < input.length; i++) {
        if (input[i] != 0.0) {
          silent = false;
        }
      }

      if (silent) {
        silentBuffersInARow++;
        if (silentBuffersInARow == 10) {
          ok(true,
              "MediaStreams produce silence when their input is blocked.");
          sp2.onaudioprocess = null;
          ac1.close();
          ac2.close();
          finish();
        }
      } else {
        is(silentBuffersInARow, 0,
            "No non silent buffer inbetween silent buffers.");
      }
    }

    osc1.connect(mediaStreamDestination1);

    mediaStreamAudioSourceNode2.connect(sp2);
    osc1.start();
  }
}


// Test that there is no buffering between contexts when connecting a running
// AudioContext to a suspended AudioContext. Our ScriptProcessorNode does some
// buffering internally, so we ensure this by using a very very low frequency
// on a sine, and oberve that the phase has changed by a big enough margin.
function testMultiContextInput() {
  var ac1 = new AudioContext(),
      ac2 = new AudioContext();

  ac1.onstatechange = function() {
    ac1.onstatechange = null;

    var osc1 = ac1.createOscillator(),
        mediaStreamDestination1 = ac1.createMediaStreamDestination(),
        sp1 = ac1.createScriptProcessor();

    var mediaStreamAudioSourceNode2 =
      ac2.createMediaStreamSource(mediaStreamDestination1.stream),
      sp2 = ac2.createScriptProcessor(),
      eventReceived = 0;


    osc1.frequency.value = 0.0001;

    function checkDiscontinuity(e) {
      var inputBuffer = e.inputBuffer.getChannelData(0);
      if (eventReceived++ == 3) {
        var delta = Math.abs(inputBuffer[1] - sp2.value),
            theoreticalIncrement = 2048 * 3 * Math.PI * 2 * osc1.frequency.value / ac1.sampleRate;
        ok(delta >= theoreticalIncrement,
            "Buffering did not occur when the context was suspended (delta:" + delta + " increment: " + theoreticalIncrement+")");
        ac1.close();
        ac2.close();
        sp1.onaudioprocess = null;
        sp2.onaudioprocess = null;
        finish();
      }
    }

    sp2.onaudioprocess = function(e) {
      var inputBuffer = e.inputBuffer.getChannelData(0);
      sp2.value = inputBuffer[inputBuffer.length - 1];
      ac2.suspend().then(function() {
          ac2.resume().then(function() {
            sp2.onaudioprocess = checkDiscontinuity;
            });
          });
    }

    osc1.connect(mediaStreamDestination1);
    osc1.connect(sp1);

    mediaStreamAudioSourceNode2.connect(sp2);
    osc1.start();
  }
}

// Test that ScriptProcessorNode's onaudioprocess don't get called while the
// context is suspended/closed. It is possible that we get the handler called
// exactly once after suspend, because the event has already been sent to the
// event loop.
function testScriptProcessNodeSuspended() {
  var ac = new AudioContext();
  var sp = ac.createScriptProcessor();
  var remainingIterations = 30;
  var afterResume = false;
  ac.onstatechange = function() {
    ac.onstatechange = null;
    sp.onaudioprocess = function() {
      ok(ac.state == "running", "If onaudioprocess is called, the context" +
          " must be running (was " + ac.state + ", remainingIterations:" + remainingIterations +")");
      remainingIterations--;
      if (!afterResume) {
        if (remainingIterations == 0) {
          ac.suspend().then(function() {
            ac.resume().then(function() {
              remainingIterations = 30;
              afterResume = true;
            });
          });
        }
      } else {
        sp.onaudioprocess = null;
        finish();
      }
    }
  }
  sp.connect(ac.destination);
}

// Take an AudioContext, make sure it switches to running when the audio starts
// flowing, and then, call suspend, resume and close on it, tracking its state.
function testAudioContext() {
  var ac = new AudioContext();
  is(ac.state, "suspended", "AudioContext should start in suspended state.");
  var stateTracker = {
    previous: ac.state,
     // no promise for the initial suspended -> running
    initial: {  handler: false },
    suspend: { promise: false, handler: false },
    resume: { promise: false, handler: false },
    close: { promise: false, handler: false }
  };

  function initialSuspendToRunning() {
    ok(stateTracker.previous == "suspended" &&
       ac.state == "running",
       "AudioContext should switch to \"running\" when the audio hardware is" +
       " ready.");

    stateTracker.previous = ac.state;
    ac.onstatechange = afterSuspend;
    stateTracker.initial.handler = true;

    ac.suspend().then(function() {
      ok(!stateTracker.suspend.promise && !stateTracker.suspend.handler,
        "Promise should be resolved before the callback, and only once.")
      stateTracker.suspend.promise = true;
    });
  }

  function afterSuspend() {
    ok(stateTracker.previous == "running" &&
       ac.state == "suspended",
       "AudioContext should switch to \"suspend\" when the audio stream is" +
       "suspended.");
    ok(stateTracker.suspend.promise && !stateTracker.suspend.handler,
        "Handler should be called after the callback, and only once");

    stateTracker.suspend.handler = true;
    stateTracker.previous = ac.state;
    ac.onstatechange = afterResume;

    ac.resume().then(function() {
      ok(!stateTracker.resume.promise && !stateTracker.resume.handler,
        "Promise should be called before the callback, and only once");
      stateTracker.resume.promise = true;
    });
  }

  function afterResume() {
    ok(stateTracker.previous == "suspended" &&
       ac.state == "running",
   "AudioContext should switch to \"running\" when the audio stream resumes.");

    ok(stateTracker.resume.promise && !stateTracker.resume.handler,
       "Handler should be called after the callback, and only once");

    stateTracker.resume.handler = true;
    stateTracker.previous = ac.state;
    ac.onstatechange = afterClose;

    ac.close().then(function() {
      ok(!stateTracker.close.promise && !stateTracker.close.handler,
        "Promise should be called before the callback, and only once");
      stateTracker.close.promise = true;
      tryToCreateNodeOnClosedContext(ac);
      tryLegalOpeerationsOnClosedContext(ac);
    });
  }

  function afterClose() {
    ok(stateTracker.previous == "running" &&
       ac.state == "closed",
       "AudioContext should switch to \"closed\" when the audio stream is" +
       " closed.");
    ok(stateTracker.close.promise && !stateTracker.close.handler,
       "Handler should be called after the callback, and only once");
  }

  ac.onstatechange = initialSuspendToRunning;
}

function testOfflineAudioContext() {
  var o = new OfflineAudioContext(1, 44100, 44100);
  is(o.state, "suspended", "OfflineAudioContext should start in suspended state.");

  expectRejectedPromise(o, "resume", "NotSupportedError");

  var previousState = o.state,
      finishedRendering = false;
  function beforeStartRendering() {
    ok(previousState == "suspended" && o.state == "running", "onstatechanged" +
        "handler is called on state changed, and the new state is running");
    previousState = o.state;
    o.onstatechange = onRenderingFinished;
  }

  function onRenderingFinished() {
    ok(previousState == "running" && o.state == "closed",
        "onstatechanged handler is called when rendering finishes, " +
        "and the new state is closed");
    ok(finishedRendering, "The Promise that is resolved when the rendering is" +
                    "done should be resolved earlier than the state change.");
    previousState = o.state;
    o.onstatechange = afterRenderingFinished;

    tryToCreateNodeOnClosedContext(o);
    tryLegalOpeerationsOnClosedContext(o);
  }

  function afterRenderingFinished() {
    ok(false, "There should be no transition out of the closed state.");
  }

  o.onstatechange = beforeStartRendering;

  o.startRendering().then(function() {
    finishedRendering = true;
  });
}

function testSuspendResumeEventLoop() {
  var ac = new AudioContext();
  var source = ac.createBufferSource();
  source.buffer = ac.createBuffer(1, 44100, 44100);
  source.onended = function() {
    ok(true, "The AudioContext did resume.");
    finish();
  }
  ac.onstatechange = function() {
    ac.onstatechange = null;

    ok(ac.state == "running", "initial state is running");
    ac.suspend();
    source.start();
    ac.resume();
  }
}

function testResumeInStateChangeForResumeCallback() {
  // Regression test for bug 1468085.
  var ac = new AudioContext;
  ac.onstatechange = function() {
    ac.resume().then(() => {
        ok(true, "resume promise resolved as expected.");
        finish();
    });
  }
}

var remaining = 0;
function finish() {
  remaining--;
  if (remaining == 0) {
    SimpleTest.finish();
  }
}


SimpleTest.waitForExplicitFinish();
addLoadEvent(function() {
  var tests = [
    testOfflineAudioContext,
    testScriptProcessNodeSuspended,
    testMultiContextOutput,
    testMultiContextInput,
    testSuspendResumeEventLoop,
    testResumeInStateChangeForResumeCallback
  ];

  // See Bug 1305136, many intermittent failures on Linux
  if (!navigator.platform.startsWith("Linux")) {
    tests.push(testAudioContext);
  }

  remaining = tests.length;
  tests.forEach(function(f) { f() });
});

</script>
</pre>
</body>
</html>