<!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: "1219711",
  title: "Disabling locally should be reflected remotely, individually for clones"
});

runNetworkTest(async () => {
  var test = new PeerConnectionTest();

  await pushPrefs(
    ["media.getusermedia.camera.stop_on_disable.enabled", true],
    ["media.getusermedia.camera.stop_on_disable.delay_ms", 0],
    ["media.getusermedia.microphone.stop_on_disable.enabled", true],
    ["media.getusermedia.microphone.stop_on_disable.delay_ms", 0],
    // Always use fake tracks since we depend on audio to have a large 1000Hz
    // component.
    ['media.audio_loopback_dev', ''],
    ['media.navigator.streams.fake', true]);
    // [TODO] re-enable HW decoder after bug 1526207 is fixed.
    if (navigator.userAgent.includes("Android")) {
      await pushPrefs(["media.navigator.mediadatadecoder_vpx_enabled", false],
                      ["media.webrtc.hw.h264.enabled", false]);
    }

  var originalStream;
  var localVideoOriginal;

  test.setMediaConstraints([{audio: true, video: true}], []);
  test.chain.replace("PC_LOCAL_GUM", [
    function PC_LOCAL_GUM_CLONE() {
      return getUserMedia(test.pcLocal.constraints[0]).then(stream => {
        originalStream = stream;
        localVideoOriginal =
          createMediaElement("video", "local-original");
        localVideoOriginal.srcObject = stream;
        test.pcLocal.attachLocalStream(originalStream.clone());
      });
    }
  ]);
  test.chain.append([
    function CHECK_ASSUMPTIONS() {
      is(test.pcLocal.localMediaElements.length, 2,
         "pcLocal should have one media element");
      is(test.pcRemote.remoteMediaElements.length, 2,
         "pcRemote should have one media element");
      is(test.pcLocal._pc.getLocalStreams().length, 1,
         "pcLocal should have one stream");
      is(test.pcRemote._pc.getRemoteStreams().length, 1,
         "pcRemote should have one stream");
    },
    async function CHECK_VIDEO() {
      info("Checking video");
      var h = new CaptureStreamTestHelper2D();
      var localVideoClone = test.pcLocal.localMediaElements
        .find(e => e instanceof HTMLVideoElement);
      var remoteVideoClone = test.pcRemote.remoteMediaElements
        .find(e => e instanceof HTMLVideoElement);

      // We check a pixel somewhere away from the top left corner since
      // MediaEngineFake puts semi-transparent time indicators there.
      const offsetX = 50;
      const offsetY = 50;
      const threshold = 128;
      const remoteDisabledColor = h.black;

      // We're regarding black as disabled here, and we're setting the alpha
      // channel of the pixel to 255 to disregard alpha when testing.
      var checkVideoEnabled = video => h.waitForPixel(video,
        px => (px[3] = 255, h.isPixelNot(px, h.black, threshold)),
        { offsetX, offsetY }
      );
      var checkVideoDisabled = video => h.waitForPixel(video,
        px => (px[3] = 255, h.isPixel(px, h.black, threshold)),
        { offsetX, offsetY }
      );

      info("Checking local original enabled");
      await checkVideoEnabled(localVideoOriginal);
      info("Checking local clone enabled");
      await checkVideoEnabled(localVideoClone);
      info("Checking remote clone enabled");
      await checkVideoEnabled(remoteVideoClone);

      info("Disabling original");
      originalStream.getVideoTracks()[0].enabled = false;

      info("Checking local original disabled");
      await checkVideoDisabled(localVideoOriginal);
      info("Checking local clone enabled");
      await checkVideoEnabled(localVideoClone);
      info("Checking remote clone enabled");
      await checkVideoEnabled(remoteVideoClone);

      info("Re-enabling original; disabling clone");
      originalStream.getVideoTracks()[0].enabled = true;
      test.pcLocal._pc.getLocalStreams()[0].getVideoTracks()[0].enabled = false;

      info("Checking local original enabled");
      await checkVideoEnabled(localVideoOriginal);
      info("Checking local clone disabled");
      await checkVideoDisabled(localVideoClone);
      info("Checking remote clone disabled");
      await checkVideoDisabled(remoteVideoClone);
    },
    async function CHECK_AUDIO() {
      info("Checking audio");
      var ac = new AudioContext();
      var localAnalyserOriginal = new AudioStreamAnalyser(ac, originalStream);
      var localAnalyserClone =
        new AudioStreamAnalyser(ac, test.pcLocal._pc.getLocalStreams()[0]);
      var remoteAnalyserClone =
        new AudioStreamAnalyser(ac, test.pcRemote._pc.getRemoteStreams()[0]);

      var freq = localAnalyserOriginal.binIndexForFrequency(TEST_AUDIO_FREQ);
      var checkAudioEnabled = analyser =>
        analyser.waitForAnalysisSuccess(array => array[freq] > 200);
      var checkAudioDisabled = analyser =>
        analyser.waitForAnalysisSuccess(array => array[freq] < 50);

      info("Checking local original enabled");
      await checkAudioEnabled(localAnalyserOriginal);
      info("Checking local clone enabled");
      await checkAudioEnabled(localAnalyserClone);
      info("Checking remote clone enabled");
      await checkAudioEnabled(remoteAnalyserClone);

      info("Disabling original");
      originalStream.getAudioTracks()[0].enabled = false;

      info("Checking local original disabled");
      await checkAudioDisabled(localAnalyserOriginal);
      info("Checking local clone enabled");
      await checkAudioEnabled(localAnalyserClone);
      info("Checking remote clone enabled");
      await checkAudioEnabled(remoteAnalyserClone);

      info("Re-enabling original; disabling clone");
      originalStream.getAudioTracks()[0].enabled = true;
      test.pcLocal._pc.getLocalStreams()[0].getAudioTracks()[0].enabled = false;

      info("Checking local original enabled");
      await checkAudioEnabled(localAnalyserOriginal);
      info("Checking local clone disabled");
      await checkAudioDisabled(localAnalyserClone);
      info("Checking remote clone disabled");
      await checkAudioDisabled(remoteAnalyserClone);
    },
  ]);
  await test.run();
});
</script>
</pre>
</body>
</html>
