78787bff8846fff539162adbfce4bd1d60fbdc35
[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 function grabImagePixels()
19 {
20     canvas.width = video.videoWidth;
21     canvas.height = video.videoHeight;
22     canvas.getContext('2d').drawImage(video, 0, 0, canvas.width, canvas.height);
23
24     imageData = canvas.getContext('2d').getImageData(20, 20, 100, 100);
25     return imageData.data;
26  }
27
28 var firstFrameData;
29
30 function storeFrame()
31 {
32     firstFrameData = grabImagePixels();
33 }
34
35 function checkCameraImageIsDifferent()
36 {
37     data = grabImagePixels();
38
39     assert_true(data[0] < 20);
40     assert_true(data[1] < 20);
41     assert_true(data[2] < 20);
42
43     var same = true;
44     for (var cptr = 0; cptr < data.length; ++cptr) {
45         if (data[cptr] != firstFrameData[cptr]) {
46             same = false;
47             break;
48         }
49     }
50     return !same;
51 }
52
53 function testCameraImage(resolve, reject, counter)
54 {
55     if (!counter)
56         counter = 0;
57
58     if (checkCameraImageIsDifferent()) {
59         resolve();
60         return;
61     }
62     if (++counter === 40) {
63         reject("testCameraImage timed out");
64         return;
65     }
66     setTimeout(() => testCameraImage(resolve, reject, counter), 100);
67 }
68
69 function testStoppedImage()
70 {
71     assert_array_equals(grabImagePixels(), firstFrameData);
72 }
73
74 promise_test((test) => {
75     if (window.testRunner)
76         testRunner.setUserMediaPermission(true);
77
78     var sender;
79     var frontStream;
80     return navigator.mediaDevices.getUserMedia({ video: true }).then((stream) => {
81         frontStream = stream;
82     }).then(() => {
83         return new Promise((resolve, reject) => {
84             createConnections((firstConnection) => {
85                 sender = firstConnection.addTrack(frontStream.getVideoTracks()[0], frontStream);
86             }, (secondConnection) => {
87                 secondConnection.ontrack = (trackEvent) => {
88                     resolve(trackEvent.streams[0]);
89                 };
90             });
91             setTimeout(() => reject("Test timed out"), 5000);
92         });
93     }).then((remoteStream) => {
94         video.srcObject = remoteStream;
95         return video.play();
96     }).then(() => {
97         return waitFor(100);
98     }).then(() => {
99         storeFrame();
100         return waitFor(100);
101     }).then(() => {
102         return new Promise((resolve, reject) => {
103             testCameraImage(resolve, reject);
104         });
105     }).then(() => {
106         promise = sender.replaceTrack(null);
107         assert_true(!!sender.track);
108         return promise;
109     }).then(() => {
110         return waitFor(100);
111     }).then(() => {
112         storeFrame();
113         return waitFor(100);
114     }).then(() => {
115         testStoppedImage();
116     });
117 }, "Stopping sending video using replaceTrack");
118         </script>
119     </body>
120 </html>