<!doctype html>
<html>
<head>
<meta charset=utf-8>
<meta name="timeout" content="long">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/gc.js"></script>
<script src="RTCPeerConnection-helper.js"></script>
</head>
<body>
<script>
'use strict';

// Check that RTCPeerConnection is not collected by GC while displaying video.

promise_test(async t => {
  const canvas = document.createElement('canvas');
  canvas.width = canvas.height = 160;
  const ctx = canvas.getContext("2d");
  ctx.fillStyle = "blue";
  const drawCanvas = () => {
    ctx.fillRect(0, 0, canvas.width, canvas.height);
  };

  let pc1 = new RTCPeerConnection();
  let pc2 = new RTCPeerConnection();

  // Attach video to pc1.
  const [inputTrack] = canvas.captureStream().getTracks();
  pc1.addTrack(inputTrack);

  const destVideo = document.createElement('video');
  destVideo.autoplay = true;
  const onVideoChange = async () => {
    const start = performance.now();
    const width = destVideo.videoWidth;
    const height = destVideo.videoHeight;
    const resizeEvent = new Promise(r => destVideo.onresize = r);
    while (destVideo.videoWidth == width && destVideo.videoHeight == height) {
      if (performance.now() - start > 5000) {
        throw new Error("Timeout waiting for video size change");
      }
      drawCanvas();
      await Promise.race([
        resizeEvent,
        new Promise(r => requestAnimationFrame(r)),
      ]);
    }
  };

  // Setup cleanup. We cannot keep references to pc1 or pc2 so do a best-effort with GC.
  t.add_cleanup(async () => {
    inputTrack.stop();
    destVideo.srcObject = null;
    await garbageCollect();
  });

  // Setup pc1->pc2.
  let haveTrackEvent = new Promise(r => pc2.ontrack = r);
  exchangeIceCandidates(pc1, pc2);
  await pc1.setLocalDescription();
  await pc2.setRemoteDescription(pc1.localDescription);
  await pc2.setLocalDescription();
  await pc1.setRemoteDescription(pc2.localDescription);

  // Display pc2 received track in video element.
  const loadedMetadata = new Promise(r => destVideo.onloadedmetadata = r);
  destVideo.srcObject = new MediaStream([(await haveTrackEvent).track]);

  // Wait for video on the other side.
  await onVideoChange();
  const color = getVideoSignal(destVideo);
  assert_not_equals(color, 0);

  // Remove RTCPeerConnection references and garbage collect.
  pc1 = null;
  pc2 = null;
  haveTrackEvent = null;
  await garbageCollect();

  // Check that a change to video input is reflected in the output, i.e., the
  // peer connections were not garbage collected.
  canvas.width = canvas.height = 240;
  ctx.fillStyle = "red";
  await onVideoChange();
  assert_not_equals(color, getVideoSignal(destVideo));
  }, "GC does not collect a peer connection pipe rendering to a video element");
</script>
</body>
</html>

