<!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"
});

runNetworkTest(async () => {
  const 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 video to be somewhat green and
    // audio to have a large 1000Hz component (or 440Hz if using fake devices).
    ['media.audio_loopback_dev', ''],
    ['media.video_loopback_dev', ''],
    ['media.navigator.streams.fake', true]);

  test.setMediaConstraints([{audio: true, video: true}], []);
  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() {
      const h = new CaptureStreamTestHelper2D();
      const localVideo = test.pcLocal.localMediaElements
        .find(e => e instanceof HTMLVideoElement);
      const remoteVideo = 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;

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

      info("Checking local video enabled");
      await checkVideoEnabled(localVideo);
      info("Checking remote video enabled");
      await checkVideoEnabled(remoteVideo);

      info("Disabling original");
      test.pcLocal._pc.getLocalStreams()[0].getVideoTracks()[0].enabled = false;

      info("Checking local video disabled");
      await checkVideoDisabled(localVideo);
      info("Checking remote video disabled");
      await checkVideoDisabled(remoteVideo);
    },
    async function CHECK_AUDIO() {
      const ac = new AudioContext();
      const localAnalyser = new AudioStreamAnalyser(ac, test.pcLocal._pc.getLocalStreams()[0]);
      const remoteAnalyser = new AudioStreamAnalyser(ac, test.pcRemote._pc.getRemoteStreams()[0]);

      const checkAudio = (analyser, fun) => analyser.waitForAnalysisSuccess(fun);

      const freq = localAnalyser.binIndexForFrequency(TEST_AUDIO_FREQ);
      const checkAudioEnabled = analyser =>
        checkAudio(analyser, array => array[freq] > 200);
      const checkAudioDisabled = analyser =>
        checkAudio(analyser, array => array[freq] < 50);

      info("Checking local audio enabled");
      await checkAudioEnabled(localAnalyser);
      info("Checking remote audio enabled");
      await checkAudioEnabled(remoteAnalyser);

      test.pcLocal._pc.getLocalStreams()[0].getAudioTracks()[0].enabled = false;

      info("Checking local audio disabled");
      await checkAudioDisabled(localAnalyser);
      info("Checking remote audio disabled");
      await checkAudioDisabled(remoteAnalyser);
    },
  ]);
  await test.run();
});
</script>
</pre>
</body>
</html>
