﻿<!DOCTYPE HTML>
<html>
<head>
  <script type="application/javascript" src="pc.js"></script>
  <script type="application/javascript" src="/tests/dom/canvas/test/captureStream_common.js"></script>
</head>
<body>
<pre id="test">
<script type="application/javascript">
createHTML({
  bug: "1166832",
  title: "Canvas(2D)::Multiple CaptureStream as video-only input to peerconnection",
  visible: true
});

/**
 * Test to verify using multiple capture streams concurrently.
 */
runNetworkTest(async () => {
  // [TODO] re-enable HW decoder after bug 1526207 is fixed.
  if (navigator.userAgent.includes("Android")) {
    await pushPrefs(["media.navigator.mediadatadecoder_vpx_enabled", false]);
    await pushPrefs(["media.webrtc.hw.h264.enabled", false]);
  }

  var test = new PeerConnectionTest();
  var h = new CaptureStreamTestHelper2D(50, 50);

  var vremote1;
  var stream1;
  var canvas1 = h.createAndAppendElement('canvas', 'source_canvas1');

  var vremote2;
  var stream2;
  var canvas2 = h.createAndAppendElement('canvas', 'source_canvas2');

  const threshold = 128;

  test.setMediaConstraints([{video: true}, {video: true}], []);
  test.chain.replace("PC_LOCAL_GUM", [
    function PC_LOCAL_CANVAS_CAPTURESTREAM(test) {
      h.drawColor(canvas1, h.green);
      h.drawColor(canvas2, h.blue);
      stream1 = canvas1.captureStream(0); // fps = 0 to capture single frame
      test.pcLocal.attachLocalStream(stream1);
      stream2 = canvas2.captureStream(0); // fps = 0 to capture single frame
      test.pcLocal.attachLocalStream(stream2);
      var i = 0;
      return setInterval(function() {
        try {
          info("draw " + i ? "green" : "red/blue");
          h.drawColor(canvas1, i ? h.green : h.red);
          h.drawColor(canvas2, i ? h.green : h.blue);
          i = 1 - i;
          stream1.requestFrame();
          stream2.requestFrame();
        } catch (e) {
          // ignore; stream might have shut down, and we don't bother clearing
          // the setInterval.
        }
      }, 500);
    }
  ]);

  test.chain.append([
    function CHECK_REMOTE_VIDEO() {
      is(test.pcRemote.remoteMediaElements.length, 2, "pcRemote Should have 2 remote media elements");
      vremote1 = test.pcRemote.remoteMediaElements[0];
      vremote2 = test.pcRemote.remoteMediaElements[1];

      // since we don't know which remote video is created first, we don't know
      // which should be blue or red, but this will make sure that one is
      // green and one is blue
      return Promise.race([
               Promise.all([
                 h.pixelMustBecome(vremote1, h.red, {
                   threshold,
                   infoString: "pcRemote's remote1 should become red",
                 }),
                 h.pixelMustBecome(vremote2, h.blue, {
                   threshold,
                   infoString: "pcRemote's remote2 should become blue",
                 }),
               ]),
               Promise.all([
                 h.pixelMustBecome(vremote2, h.red, {
                   threshold,
                   infoString: "pcRemote's remote2 should become red",
                 }),
                 h.pixelMustBecome(vremote1, h.blue, {
                   threshold,
                   infoString: "pcRemote's remote1 should become blue",
                 }),
               ])
             ]);
    },
    function WAIT_FOR_REMOTE_BOTH_GREEN() {
      return Promise.all([
               h.pixelMustBecome(vremote1, h.green, {
                   threshold,
                   infoString: "pcRemote's remote1 should become green",
               }),
               h.pixelMustBecome(vremote2, h.green, {
                   threshold,
                   infoString: "pcRemote's remote2 should become green",
               }),
             ])
    },
  ]);
  await test.run();
});
</script>
</pre>
</body>
</html>
