summaryrefslogtreecommitdiffstats
path: root/dom/media/webrtc/tests/mochitests/test_getUserMedia_GC_MediaStream.html
blob: ae16097d075bb586f5c507e94e59137578b2e236 (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
<!DOCTYPE HTML>
<html>
<head>
  <script type="application/javascript" src="mediaStreamPlayback.js"></script>
</head>
<body>
<pre id="test">
<script type="application/javascript">
  "use strict";

  createHTML({
    title: "MediaStreams can be garbage collected",
    bug: "1407542"
  });

  let SpecialStream = SpecialPowers.wrap(MediaStream);

  async function testGC(stream, numCopies, copy) {
    let startStreams = await SpecialStream.countUnderlyingStreams();

    let copies = new Array(numCopies).fill(0).map(() => copy(stream));
    ok(await SpecialStream.countUnderlyingStreams() > startStreams,
        "MediaStreamTrack constructor creates more underlying streams");

    copies = [];
    await new Promise(r => SpecialPowers.exactGC(r));
    is(await SpecialStream.countUnderlyingStreams(), startStreams,
       "MediaStreamTracks should have been collected");
  }

  runTest(async () => {
    let gUMStream = await getUserMedia({video: true});
    info("Testing GC of track-array constructor with cloned tracks");
    await testGC(gUMStream, 10, s => new MediaStream(s.getTracks().map(t => t.clone())));

    info("Testing GC of empty constructor plus addTrack with cloned tracks");
    await testGC(gUMStream, 10, s => {
      let s2 = new MediaStream();
      s.getTracks().forEach(t => s2.addTrack(t.clone()));
      return s2;
    });

    info("Testing GC of cloned stream");
    await testGC(gUMStream, 10, s => s.clone());

    info("Testing GC of gUM stream");
    gUMStream = null;
    await new Promise(r => SpecialPowers.exactGC(r));
    is(await SpecialStream.countUnderlyingStreams(), 0,
       "Original gUM stream should be collectable");
  });
</script>
</pre>
</body>
</html>