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

createHTML({
  title: "Test the ability of opening multiple microphones via gUM",
  bug: "1238038",
});

runTest(async () => {
  // Ensure we use the real microphones by disabling loopback devices and fake devices.
  await pushPrefs(["media.audio_loopback_dev", ""], ["media.navigator.streams.fake", false]);

  try {
    // Allow exposing microphone by calling gUM first
    const defaultStream = await navigator.mediaDevices.getUserMedia({ audio: true });
    for (const track of defaultStream.getTracks()) {
      track.stop();
    }

    const devices = await navigator.mediaDevices.enumerateDevices();
    // Create constraints
    let constraints = [];
    devices.forEach((device) => {
      if (device.kind === "audioinput") {
        constraints.push({
          audio: { deviceId: { exact: device.deviceId } },
        });
      }
    });
    if (constraints.length >= 2) {
      // Open microphones by the constraints
      let mediaStreams = [];
      for (const c of constraints) {
        let stream = await navigator.mediaDevices.getUserMedia(c);
        mediaStreams.push(stream);
      }
      // Close microphones
      for (const stream of mediaStreams) {
        for (const track of stream.getTracks()) {
          track.stop();
        }
      }
      mediaStreams = [];
    } else {
      dump("Skip test since we need at least two microphones\n");
    }
  } catch (e) {
    ok(false, e.name + ": " + e.message);
  }
});
</script>
</pre>
</body>
</html>