71739aaa0db7d8e3a787b066e19d065ebee15f42
[WebKit-https.git] / LayoutTests / webrtc / video-replace-track.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         <script src="routines.js"></script>
9     </head>
10     <body>
11 <div id="log"></div>
12         <video id="video" autoplay=""></video>
13         <canvas id="canvas" width="640" height="480"></canvas>
14         <script src ="routines.js"></script>
15         <script>
16 video = document.getElementById("video");
17 canvas = document.getElementById("canvas");
18
19 function grabImagePixels()
20 {
21     canvas.width = video.videoWidth;
22     canvas.height = video.videoHeight;
23     canvas.getContext('2d').drawImage(video, 0, 0, canvas.width, canvas.height);
24
25     imageData = canvas.getContext('2d').getImageData(20, 20, 2, 2);
26     return imageData.data;
27  }
28
29 async function testFrontCameraImage(counter)
30 {
31     if (!counter)
32         counter = 0;
33
34     if (isVideoBlack(canvas, video, 20, 20, 2 ,2))
35         return;
36
37     if (counter >= 20)
38         return Promise.reject("testFrontCameraImage timed out");
39
40     await waitFor(50);
41     return testFrontCameraImage(++counter);
42 }
43
44 function isBetween100And200(value)
45 {
46      return data[0] > 100 && data[0] < 200;
47 }
48
49 async function testBackCameraImage(counter)
50 {
51     if (!counter)
52         counter = 0;
53
54     data = grabImagePixels();
55
56     if(isBetween100And200(data[0]) && isBetween100And200(data[1]) && isBetween100And200(data[2]))
57         return;
58
59     if (counter >= 20)
60         return Promise.reject("testFrontCameraImage timed out");
61
62     await waitFor(50);
63     return testBackCameraImage(++counter);
64 }
65
66 promise_test((test) => {
67     if (window.testRunner)
68         testRunner.setUserMediaPermission(true);
69
70     var sender;
71     var frontStream;
72     var backStream;
73     return navigator.mediaDevices.getUserMedia({ video: { facingMode: { exact: ["user"] } } }).then((stream) => {
74         frontStream = stream;
75         return new Promise((resolve, reject) => {
76             createConnections((firstConnection) => {
77                 sender = firstConnection.addTrack(frontStream.getVideoTracks()[0], frontStream);
78            }, (secondConnection) => {
79                 secondConnection.ontrack = (trackEvent) => {
80                     resolve(trackEvent.streams[0]);
81                 };
82             });
83             setTimeout(() => reject("Test timed out"), 5000);
84         });
85     }).then((remoteStream) => {
86         video.srcObject = remoteStream;
87         return video.play();
88     }).then(() => {
89         return testFrontCameraImage();
90     }).then(() => {
91         return navigator.mediaDevices.getUserMedia({ video: { facingMode: { exact: ["environment"] } } });
92     }).then((stream) => {
93         backStream = stream;
94         var currentTrack = sender.track;
95         promise = sender.replaceTrack(backStream.getVideoTracks()[0]);
96         assert_true(currentTrack === sender.track);
97         return promise;
98     }).then(() => {
99         assert_true(sender.track === backStream.getVideoTracks()[0]);
100         return testBackCameraImage();
101     });
102 }, "Switching from front to back camera");
103
104 promise_test((test) => {
105     if (window.testRunner)
106         testRunner.setUserMediaPermission(true);
107
108     var sender;
109     var frontStream;
110     var backStream;
111
112     return navigator.mediaDevices.getUserMedia({ video: { width: 640, height: 480, facingMode: { exact: ["user"] } } }).then((stream) => {
113         frontStream = stream;
114         assert_true(frontStream.getVideoTracks()[0].getSettings().height === 480, "frontStream should be big");
115         return new Promise((resolve, reject) => {
116             createConnections((firstConnection) => {
117                 sender = firstConnection.addTrack(frontStream.getVideoTracks()[0], frontStream);
118             }, (secondConnection) => {
119                 secondConnection.ontrack = (trackEvent) => {
120                     resolve(trackEvent.streams[0]);
121                 };
122             });
123             setTimeout(() => reject("Test timed out"), 5000);
124         });
125     }).then((remoteStream) => {
126         video.srcObject = remoteStream;
127         return video.play();
128     }).then(() => {
129         return testFrontCameraImage();
130     }).then(() => {
131         return navigator.mediaDevices.getUserMedia({ video: { width: 320, height: 240, facingMode: { exact: ["environment"] } } });
132     }).then((stream) => {
133         backStream = stream;
134         assert_true(backStream.getVideoTracks()[0].getSettings().height === 240, "backStream should be small");
135         return sender.replaceTrack(backStream.getVideoTracks()[0]);
136     }).then(() => {
137         return testBackCameraImage();
138     });
139 }, "Switching from front to back camera, with lower resolution");
140
141 promise_test((test) => {
142     if (window.testRunner)
143         testRunner.setUserMediaPermission(true);
144
145     var sender;
146     var frontStream;
147     var backStream;
148
149     return navigator.mediaDevices.getUserMedia({ video: { width: 320, height: 240, facingMode: { exact: ["user"] } } }).then((stream) => {
150         frontStream = stream;
151         assert_true(frontStream.getVideoTracks()[0].getSettings().height === 240, "front stream should be small");
152     }).then(() => {
153         return new Promise((resolve, reject) => {
154             createConnections((firstConnection) => {
155                 sender = firstConnection.addTrack(frontStream.getVideoTracks()[0], frontStream);
156             }, (secondConnection) => {
157                 secondConnection.ontrack = (trackEvent) => {
158                     resolve(trackEvent.streams[0]);
159                 };
160             });
161             setTimeout(() => reject("Test timed out"), 5000);
162         });
163     }).then((remoteStream) => {
164         video.srcObject = remoteStream;
165         return video.play();
166     }).then(() => {
167         return testFrontCameraImage();
168     }).then(() => {
169         return navigator.mediaDevices.getUserMedia({ video: { width: 640, height: 480 , facingMode: { exact: ["environment"] } } });
170     }).then((stream) => {
171         backStream = stream;
172         assert_true(backStream.getVideoTracks()[0].getSettings().height === 480, "back stream should be big");
173         return sender.replaceTrack(backStream.getVideoTracks()[0]);
174     }).then(() => {
175         return testBackCameraImage();
176     });
177
178 }, "Switching from front to back camera, with higher resolution");
179         </script>
180     </body>
181 </html>