summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webrtc/RTCPeerConnection-GC.https.html
blob: 282c362a2d4ae2836862502b23a09c32db424288 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<!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);
    t.step_timeout(drawCanvas, 50);
  };

  drawCanvas();

  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;
  destVideo.muted = true;
  const onVideoChange = async () => {
    const start = performance.now();
    const width = destVideo.videoWidth;
    const height = destVideo.videoHeight;
    while (destVideo.videoWidth == width && destVideo.videoHeight == height) {
      if (performance.now() - start > 5000) {
        throw new Error("Timeout waiting for video size change");
      }
      await new Promise(r => t.step_timeout(r, 50));
    }
  };

  // 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 destVideo.play();
  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");

promise_test(async t => {
  const pc1 = new RTCPeerConnection();
  t.add_cleanup(() => pc1.close());
  const pc2 = new RTCPeerConnection();
  t.add_cleanup(() => pc2.close());

  const [track, stream] = await createTrackAndStreamWithCleanup(t, "video");
  pc1.addTrack(track, stream);
  exchangeIceCandidates(pc1, pc2);

  const metadataToBeLoaded = [];
  pc2.ontrack = (e) => {
    const stream = e.streams[0];
    const v = document.createElement('video');
    v.autoplay = true;
    v.srcObject = stream;
    v.id = stream.id
    metadataToBeLoaded.push(new Promise((resolve) => {
        v.addEventListener('loadedmetadata', () => {
            resolve();
        });
    }));
  };
  await exchangeOfferAnswer(pc1, pc2);

  garbageCollect();

  await Promise.all(metadataToBeLoaded);
}, "GC does not collect an HTMLMediaElement playing a video track");
</script>
</body>
</html>