﻿<!DOCTYPE HTML>
<html>
<head>
  <script type="application/javascript" src="pc.js"></script>
  <script type="application/javascript" src="stats.js"></script>
</head>
<body>
<pre id="test">
<script type="application/javascript">

  createHTML({
    bug: "1091242",
    title: "Multistream: Two audio/video streams"
  });

  runNetworkTest(async (options) => {
    // Disable platform encodre for SW MFT encoder causes some stats
    // exceeding the test thresholds.
    // E.g. inbound-rtp.packetsDiscarded value=118 >= 100.
    await matchPlatformH264CodecPrefs();

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

    // Test stats, including coalescing of codec stats.
    test.chain.insertAfter("PC_LOCAL_WAIT_FOR_MEDIA_FLOW",
      [PC_LOCAL_TEST_LOCAL_STATS]);

    test.chain.insertAfter("PC_REMOTE_WAIT_FOR_MEDIA_FLOW",
      [PC_REMOTE_TEST_REMOTE_STATS]);

    const testCoalescedCodecStats = stats => {
      is([...stats.values()].filter(({type}) => type.endsWith("rtp")).length,
        16,
        "Expected: 4 outbound, 4 remote-inbound, 4 inbound, 4 remote-inbound");
      const codecs = [...stats.values()]
          .filter(({type}) => type == "codec")
          .sort((a, b) => a.mimeType > b.mimeType);
      is(codecs.length, 2, "Should have registered two codecs (coalesced)");
      is(new Set(codecs.map(({transportId}) => transportId)).size, 1,
        "Should have registered only one transport with BUNDLE");
      const codecTypes = new Set(codecs.map(({codecType}) => codecType));
      is(codecTypes.size, 1,
        "Should have identical encode and decode configurations (and stats)");
      is(codecTypes[0], undefined,
        "Should have identical encode and decode configurations (and stats)");
      is(codecs[0].mimeType.slice(0, 5), "audio",
        "Should have registered an audio codec");
      is(codecs[1].mimeType.slice(0, 5), "video",
        "Should have registered a video codec");
    };

    test.chain.append([
      async function PC_LOCAL_TEST_COALESCED_CODEC_STATS() {
        testCoalescedCodecStats(await test.pcLocal._pc.getStats());
      },
      async function PC_REMOTE_TEST_COALESCED_CODEC_STATS() {
        testCoalescedCodecStats(await test.pcRemote._pc.getStats());
      },
    ]);

    return test.run();
  });
</script>
</pre>
</body>
</html>
