summaryrefslogtreecommitdiffstats
path: root/dom/media/webrtc/tests/mochitests/test_getUserMedia_basicAudio_loopback.html
blob: 10bf669c00cce528cc5d8bb0308766513625b9d2 (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
<!DOCTYPE HTML>
<html>
<head>
  <script type="application/javascript" src="mediaStreamPlayback.js"></script>
</head>
<body>
<pre id="test">

<script>
  createHTML({
    title: "getUserMedia Basic Audio Test Loopback",
    bug: "1406350",
    visible: true
  });

  /**
   * Run a test to verify the use of LoopbackTone as audio input.
   */
  runTest(async () => {
    if (!SpecialPowers.getCharPref("media.audio_loopback_dev", "")) {
      todo(false, "No loopback device set by framework. Try --use-test-media-devices");
      return;
    }

    // At this point DefaultLoopbackTone has been instantiated
    // automatically on frequency TEST_AUDIO_FREQ (440 Hz). Verify
    // that a tone is detected on that frequency.
    info("Capturing at default frequency");
    const stream = await getUserMedia({audio: true});

    try {
      const audioContext = new AudioContext();
      const analyser = new AudioStreamAnalyser(audioContext, stream);
      analyser.enableDebugCanvas();
      await analyser.waitForAnalysisSuccess(array => {
        // High energy on 1000 Hz low energy around that
        const freg_50Hz   = array[analyser.binIndexForFrequency(50)];
        const freq        = array[analyser.binIndexForFrequency(TEST_AUDIO_FREQ)];
        const freq_2000Hz = array[analyser.binIndexForFrequency(2000)];

        info("Analysing audio frequency - low:target:high = "
                + freg_50Hz + ':' + freq + ':' + freq_2000Hz);
        return freg_50Hz < 50 && freq > 200 && freq_2000Hz < 50;
      });

      // Use the LoopbackTone API to change the frequency of the default tone.
      // Verify that a tone is detected on the new frequency (800 Hz).
      info("Change loopback tone frequency");
      DefaultLoopbackTone.changeFrequency(800);
      await analyser.waitForAnalysisSuccess(array => {
        const freg_50Hz   = array[analyser.binIndexForFrequency(50)];
        const freq        = array[analyser.binIndexForFrequency(800)];
        const freq_2000Hz = array[analyser.binIndexForFrequency(2000)];

        info("Analysing audio frequency - low:target:high = "
                + freg_50Hz + ':' + freq + ':' + freq_2000Hz);
        return freg_50Hz < 50 && freq > 200 && freq_2000Hz < 50;
      });

      // Create a second tone at a different frequency.
      // Verify that both tones are detected.
      info("Multiple loopback tones");
      DefaultLoopbackTone.changeFrequency(TEST_AUDIO_FREQ);
      const second_tone = new LoopbackTone(audioContext, 2000);
      second_tone.start();
      await analyser.waitForAnalysisSuccess(array => {
        const freg_50Hz   = array[analyser.binIndexForFrequency(50)];
        const freq        = array[analyser.binIndexForFrequency(TEST_AUDIO_FREQ)];
        const freq_2000Hz = array[analyser.binIndexForFrequency(2000)];
        const freq_4000Hz = array[analyser.binIndexForFrequency(4000)];

        info("Analysing audio frequency - low:target1:target2:high = "
                + freg_50Hz + ':' + freq + ':' + freq_2000Hz + ':' + freq_4000Hz);
        return freg_50Hz < 50 && freq > 200 && freq_2000Hz > 200 && freq_4000Hz < 50;
      });

      // Stop all tones and verify that there is no audio on the given frequencies.
      info("Stop all loopback tones");
      DefaultLoopbackTone.stop();
      second_tone.stop()
      await analyser.waitForAnalysisSuccess(array => {
        const freg_50Hz   = array[analyser.binIndexForFrequency(50)];
        const freq        = array[analyser.binIndexForFrequency(TEST_AUDIO_FREQ)];
        const freq_2000Hz = array[analyser.binIndexForFrequency(2000)];

        info("Analysing audio frequency - low:target:high = "
                + freg_50Hz + ':' + freq + ':' + freq_2000Hz);
        return freg_50Hz < 50 && freq < 50 && freq_2000Hz < 50;
      });
    } finally {
      for (let t of stream.getTracks()) {
        t.stop();
      }
    }
  });
</script>
</pre>
</body>
</html>