summaryrefslogtreecommitdiffstats
path: root/dom/media/webaudio/test/test_mediaStreamAudioSourceNodeNoGC.html
blob: 84d41df50e66a2ae72ce2b49076e4c9438ab4628 (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
<!DOCTYPE HTML>
<html>
<meta charset="utf-8">
<head>
  <title>Test that MediaStreamAudioSourceNode and its input MediaStream stays alive while there are active tracks</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<pre id="test">
<script class="testbody" type="text/javascript">
SimpleTest.waitForExplicitFinish();
SimpleTest.requestFlakyTimeout("gUM and WebAudio data is async to main thread. " +
                               "We need a timeout to see that something does " +
                               "NOT happen to data.");

let context = new AudioContext();
let analyser = context.createAnalyser();

function wait(millis, resolveWithThis) {
  return new Promise(resolve => setTimeout(() => resolve(resolveWithThis), millis));
}

function binIndexForFrequency(frequency) {
  return 1 + Math.round(frequency * analyser.fftSize / context.sampleRate);
}

function waitForAudio(analysisFunction, cancelPromise) {
  let data = new Uint8Array(analyser.frequencyBinCount);
  let cancelled = false;
  let cancelledMsg = "";
  cancelPromise.then(msg => {
    cancelled = true;
    cancelledMsg = msg;
  });
  return new Promise((resolve, reject) => {
    let loop = () => {
      analyser.getByteFrequencyData(data);
      if (cancelled) {
        reject(new Error("waitForAudio cancelled: " + cancelledMsg));
        return;
      }
      if (analysisFunction(data)) {
        resolve();
        return;
      }
      requestAnimationFrame(loop);
    };
    loop();
  });
}

async function test() {
  try {
    await analyser.connect(context.destination);

    ok(true, "Waiting for audio to pass through the analyser")
    await waitForAudio(arr => arr[binIndexForFrequency(1000)] > 200,
                       wait(60000, "Timeout waiting for audio"));

    ok(true, "Audio was detected by the analyser. Forcing CC.");
    SpecialPowers.forceCC();
    SpecialPowers.forceGC();
    SpecialPowers.forceCC();
    SpecialPowers.forceGC();

    info("Checking that GC didn't destroy the stream or source node");
    await waitForAudio(arr => arr[binIndexForFrequency(1000)] < 50,
                       wait(5000, "Timeout waiting for GC (timeout OK)"))
                  .then(() => Promise.reject("Audio stopped unexpectedly"),
                        () => Promise.resolve());

    ok(true, "Audio is still flowing");
  } catch(e) {
    ok(false, "Error executing test: " + e + (e.stack ? "\n" + e.stack : ""));
    SimpleTest.finish();
  }
}

(async function() {
  try {
    await SpecialPowers.pushPrefEnv({
      set: [
        // This test expects the fake audio device, specifically for the tones
        // it outputs. Explicitly disable the audio loopback device and enable
        // fake streams.
        ['media.audio_loopback_dev', ''],
        ['media.navigator.streams.fake', true],
        ['media.navigator.permission.disabled', true]
      ]
    });

    // Test stream source GC
    let stream = await navigator.mediaDevices.getUserMedia({audio: true});
    let source = context.createMediaStreamSource(stream);
    stream = null;
    source.connect(analyser);
    await test(source);

    // Test track source GC
    stream = await navigator.mediaDevices.getUserMedia({audio: true});
    source = context.createMediaStreamTrackSource(stream.getAudioTracks()[0]);
    stream = null;
    source.connect(analyser);
    await test(source);
  } catch(e) {
    ok(false, `Error executing test: ${e}${e.stack ? "\n" + e.stack : ""}`);
  } finally {
    context.close();
    SimpleTest.finish();
  }
})();
</script>
</pre>
</body>
</html>