RTCRtpSender.replaceTrack(null) ends current track
[WebKit-https.git] / LayoutTests / webrtc / video-replace-track-to-null.html
1 <!doctype html>
2 <html>
3     <head>
4         <meta charset="utf-8">
5         <title>Testing basic video exchange from offerer to receiver</title>
6         <script src="../resources/testharness.js"></script>
7         <script src="../resources/testharnessreport.js"></script>
8     </head>
9     <body>
10 <div id="log"></div>
11         <video id="video" autoplay=""></video>
12         <canvas id="canvas" width="640" height="480"></canvas>
13         <script src ="routines.js"></script>
14         <script>
15 video = document.getElementById("video");
16 canvas = document.getElementById("canvas");
17
18 async function getOutboundRTPStatsNumberOfEncodedFrames(connection)
19 {
20     var report = await connection.getStats();
21     var framesEncoded;
22     report.forEach((statItem) => {
23         if (statItem.type === "outbound-rtp") {
24             framesEncoded = statItem.framesEncoded;
25         }
26     });
27     return framesEncoded;
28 }
29
30 async function testFrameEncodedStarted(connection, count, previousFramesNumber)
31 {
32     var framesEncodedNumber = await getOutboundRTPStatsNumberOfEncodedFrames(connection);
33     if (previousFramesNumber && framesEncodedNumber > previousFramesNumber)
34         return;
35
36     if (count === undefined)
37         count = 0;
38
39     if (count >= 20)
40         return Promise.reject("test increasing frame encoded timed out");
41
42     await waitFor(100);
43     return testFrameEncodedStarted(connection, ++count, framesEncodedNumber);
44 }
45
46 async function testFrameEncodedStopped(connection, count, previousFramesNumber)
47 {
48     var framesEncodedNumber = await getOutboundRTPStatsNumberOfEncodedFrames(connection);
49     if (previousFramesNumber && framesEncodedNumber === previousFramesNumber)
50         return;
51
52     if (count === undefined)
53         count = 0;
54
55     if (count === 20)
56         return Promise.reject("test that number of encoded frames stopped increasing timed out");
57
58     await waitFor(100);
59     return testFrameEncodedStopped(connection, ++count, framesEncodedNumber);
60 }
61
62 var sender;
63 var sendingTrack;
64 var connection;
65 promise_test(async (test) => {
66     if (window.testRunner)
67         testRunner.setUserMediaPermission(true);
68
69     const frontStream = await navigator.mediaDevices.getUserMedia({ video: true });
70
71     const remoteStream = await new Promise((resolve, reject) => {
72         createConnections((firstConnection) => {
73             connection = firstConnection;
74             sender = firstConnection.addTrack(frontStream.getVideoTracks()[0], frontStream);
75         }, (secondConnection) => {
76             secondConnection.ontrack = (trackEvent) => {
77                 resolve(trackEvent.streams[0]);
78             };
79         });
80         setTimeout(() => reject("Test timed out"), 5000);
81     });
82
83     video.srcObject = remoteStream;
84     await video.play();
85 }, "Set-up test");
86
87 promise_test(async (test) => {
88     await testFrameEncodedStarted(connection);
89
90     sendingTrack = sender.track;
91     assert_equals(sendingTrack.readyState, "live");
92 }, "Test that frame counter increases");
93
94 promise_test(async (test) => {
95     promise = sender.replaceTrack(null);
96     assert_true(!!sender.track);
97     await promise;
98     assert_equals(sender.track, null);
99 }, "Test replaceTrack with null track");
100
101 promise_test(async (test) => {
102     await testFrameEncodedStopped(connection);
103     assert_equals(sendingTrack.readyState, "live");
104 }, "Test that frame counter no longer increases");
105         </script>
106     </body>
107 </html>