﻿<!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: "1225722",
  title: "Multistream: Two audio/video streams without BUNDLE"
});

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();

  options.bundle = false;
  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 that codec stats do not coalesce without BUNDLE.
  const testNonBundledStats = async pc => {
    // This is basically PC_*_TEST_*_STATS fleshed out, but uses
    // sender/receiver.getStats instead of pc.getStats, since the codec stats
    // code assumes at most one sender and at most one receiver.
    await waitForSyncedRtcp(pc);
    const senderPromises = pc.getSenders().map(obj => obj.getStats());
    const receiverPromises = pc.getReceivers().map(obj => obj.getStats());
    const senderStats = await Promise.all(senderPromises);
    const receiverStats = await Promise.all(receiverPromises);
    for (const stats of [...senderStats, ...receiverStats]) {
      checkExpectedFields(stats);
      pedanticChecks(stats);
    }
    for (const stats of senderStats) {
      checkSenderStats(stats, 1);
    }
  };

  test.chain.insertAfter("PC_LOCAL_WAIT_FOR_MEDIA_FLOW", [
    async function PC_LOCAL_TEST_LOCAL_NONBUNDLED_STATS(test) {
      await testNonBundledStats(test.pcLocal._pc);
    },
  ]);

  test.chain.insertAfter("PC_REMOTE_WAIT_FOR_MEDIA_FLOW", [
    async function PC_REMOTE_TEST_LOCAL_NONBUNDLED_STATS(test) {
      await testNonBundledStats(test.pcRemote._pc);
    },
  ]);

  const testNonCoalescedCodecStats = stats => {
    const codecs = [...stats.values()]
          .filter(({type}) => type == "codec");
    is([...stats.values()].filter(({type}) => type.endsWith("rtp")).length, 16,
      "Expected: 4 outbound, 4 remote-inbound, 4 inbound, 4 remote-inbound");
    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)");
    const transportIds = new Set(codecs.map(({transportId}) => transportId));
    is(transportIds.size, 4,
      "Should have registered four transports for two sendrecv streams");
    for (const transportId of transportIds) {
      is(codecs.filter(c => c.transportId == transportId).length, 1,
        "Should have registered one codec per transport without BUNDLE");
    }
    for (const prefix of ["audio", "video"]) {
      const prefixed = codecs.filter(c => c.mimeType.startsWith(prefix));
      is(prefixed.length, 2, `Should have registered two ${prefix} codecs`);
      if (prefixed.length == 2) {
        is(prefixed[0].payloadType, prefixed[1].payloadType,
          "same payloadType");
        isnot(prefixed[0].transportId, prefixed[1].transportId,
          "different transportIds");
        is(prefixed[0].mimeType, prefixed[1].mimeType, "same mimeType");
        is(prefixed[0].clockRate, prefixed[1].clockRate, "same clockRate");
        is(prefixed[0].channels, prefixed[1].channels, "same channels");
        is(prefixed[0].sdpFmtpLine, prefixed[1].sdpFmtpLine,
          "same sdpFmtpLine");
      }
    }
  };

  test.chain.append([
    async function PC_LOCAL_TEST_NON_COALESCED_CODEC_STATS() {
      testNonCoalescedCodecStats(await test.pcLocal._pc.getStats());
    },
    async function PC_REMOTE_TEST_NON_COALESCED_CODEC_STATS() {
      testNonCoalescedCodecStats(await test.pcRemote._pc.getStats());
    },
  ]);

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