summaryrefslogtreecommitdiffstats
path: root/dom/media/webrtc/tests/mochitests/test_peerConnection_audioCodecs.html
blob: 41dc5f88603a447d6faf5b04200374cc4bf404a8 (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
<!DOCTYPE HTML>
<html>
<head>
  <script type="application/javascript" src="pc.js"></script>
</head>
<body>
<pre id="test">
<script type="application/javascript">
  createHTML({
    bug: "1395853",
    title: "Verify audio content over WebRTC for every audio codec",
  });

  // We match the format member against the sdp to figure out the payload type,
  // So all other present codecs can be removed.
  const codecs = [ "opus", "G722", "PCMU", "PCMA" ];
  const audioContext = new AudioContext();

  async function testAudioCodec(options = {}, codec) {
    // sdputils checks for opus as part of its sdp sanity test
    options.opus = codec == "opus";

    let test = new PeerConnectionTest(options);
    test.setMediaConstraints([{audio: true}], []);

    test.chain.insertBefore("PC_LOCAL_SET_LOCAL_DESCRIPTION", [
      function PC_LOCAL_FILTER_OUT_CODECS() {
        let otherCodec = codecs.find(c => c != codec);
        let otherId = sdputils.findCodecId(test.originalOffer.sdp, otherCodec);

        let id = sdputils.findCodecId(test.originalOffer.sdp, codec);
        test.originalOffer.sdp =
          sdputils.removeAllButPayloadType(test.originalOffer.sdp, id);

        ok(!test.originalOffer.sdp.match(new RegExp(`m=.*UDP/TLS/RTP/SAVPF.* ${otherId}[^0-9]`, "gi")),
          `Other codec ${otherId} should be removed after filtering`);
        ok(test.originalOffer.sdp.match(new RegExp(`m=.*UDP/TLS/RTP/SAVPF.* ${id}[^0-9]`, "gi")),
          `Tested codec ${id} should remain after filtering`);

        for (let c of codecs.filter(c => c != codec)) {
          // Remove rtpmaps for the other codecs so sdp sanity tests pass.
          let id = sdputils.findCodecId(test.originalOffer.sdp, c);
          test.originalOffer.sdp =
            sdputils.removeRtpMapForPayloadType(test.originalOffer.sdp, id);
        }

        ok(!test.originalOffer.sdp.match(new RegExp(`a=rtpmap:${otherId}.*\\r\\n`, "gi")),
           `Rtpmap of other codec ${otherId} should be removed after filtering`);
        ok(test.originalOffer.sdp.match(new RegExp(`a=rtpmap:${id}.*\\r\\n`, "gi")),
           `Rtpmap of tested codec should remain after filtering`);
      },
    ]);

    test.chain.append([
      async function CHECK_AUDIO_FLOW() {
        try {
          await test.pcRemote.checkReceivingToneFrom(audioContext, test.pcLocal);
          ok(true, "input and output audio data matches");
        } catch(e) {
          ok(false, `No audio flow: ${e}`);
        }
      },
    ]);

    await test.run();
  }

  runNetworkTest(async (options) => {
    // Start a tone so that the gUM call will record something even with
    // --use-test-media-devices.  TEST_AUDIO_FREQ matches the frequency of the
    // tone in fake microphone devices.
    const tone = new LoopbackTone(audioContext, TEST_AUDIO_FREQ);
    tone.start();

    for (let codec of codecs) {
      info(`Testing audio for codec ${codec}`);
      try {
        await testAudioCodec(options, codec);
      } catch(e) {
        ok(false, `Error in test for codec ${codec}: ${e}\n${e.stack}`);
      }
      info(`Tested audio for codec ${codec}`);
    }

    tone.stop();
  });
</script>
</pre>
</body>
</html>