22085eb729751ae463b310a707ad6a44ae5a194d
[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     </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, 2, 2);
25     return imageData.data;
26  }
27
28 function testFrontCameraImage()
29 {
30     data = grabImagePixels();
31
32     assert_true(data[0] < 20);
33     assert_true(data[1] < 20);
34     assert_true(data[2] < 20);
35 }
36
37 function testBackCameraImage()
38 {
39     data = grabImagePixels();
40
41     assert_true(data[0] > 100);
42     assert_true(data[1] > 100);
43     assert_true(data[2] > 100);
44
45     assert_true(data[0] < 200);
46     assert_true(data[1] < 200);
47     assert_true(data[2] < 200);
48 }
49
50 promise_test((test) => {
51     if (window.testRunner)
52         testRunner.setUserMediaPermission(true);
53
54     var sender;
55     var frontStream;
56     var backStream;
57     return navigator.mediaDevices.getUserMedia({ video: { facingMode: { exact: ["user"] } } }).then((stream) => {
58         frontStream = stream;
59         return navigator.mediaDevices.getUserMedia({ video: { facingMode: { exact: ["environment"] } } });
60     }).then((stream) => {
61         backStream = stream;
62     }).then(() => {
63         return new Promise((resolve, reject) => {
64             createConnections((firstConnection) => {
65                 sender = firstConnection.addTrack(frontStream.getVideoTracks()[0], frontStream);
66             }, (secondConnection) => {
67                 secondConnection.ontrack = (trackEvent) => {
68                     resolve(trackEvent.streams[0]);
69                 };
70             });
71             setTimeout(() => reject("Test timed out"), 5000);
72         });
73     }).then((remoteStream) => {
74         video.srcObject = remoteStream;
75         return video.play();
76     }).then(() => {
77         testFrontCameraImage();
78     }).then(() => {
79         var currentTrack = sender.track;
80         promise = sender.replaceTrack(backStream.getVideoTracks()[0]);
81         assert_true(currentTrack === sender.track);
82         return promise;
83     }).then(() => {
84         assert_true(sender.track === backStream.getVideoTracks()[0]);
85         return waitFor(500);
86     }).then(() => {
87         testBackCameraImage();
88     });
89 }, "Switching from front to back camera");
90
91 promise_test((test) => {
92     if (window.testRunner)
93         testRunner.setUserMediaPermission(true);
94
95     var sender;
96     var frontStream;
97     var backStream;
98
99     return navigator.mediaDevices.getUserMedia({ video: { height: { min: 400 }, facingMode: { exact: ["user"] } } }).then((stream) => {
100         frontStream = stream;
101         return navigator.mediaDevices.getUserMedia({ video: { height: { max: 400 }, facingMode: { exact: ["environment"] } } });
102     }).then((stream) => {
103         backStream = stream;
104         assert_true(frontStream.getVideoTracks()[0].getSettings().height >= 400, "frontStream should be bigger");
105         assert_true(backStream.getVideoTracks()[0].getSettings().height < 400, "backStream should be smaller");
106     }).then(() => {
107         return new Promise((resolve, reject) => {
108             createConnections((firstConnection) => {
109                 sender = firstConnection.addTrack(frontStream.getVideoTracks()[0], frontStream);
110             }, (secondConnection) => {
111                 secondConnection.ontrack = (trackEvent) => {
112                     resolve(trackEvent.streams[0]);
113                 };
114             });
115             setTimeout(() => reject("Test timed out"), 5000);
116         });
117     }).then((remoteStream) => {
118         video.srcObject = remoteStream;
119         return video.play();
120     }).then(() => {
121         testFrontCameraImage();
122     }).then(() => {
123         return sender.replaceTrack(backStream.getVideoTracks()[0]);
124     }).then(() => {
125         return waitFor(500);
126     }).then(() => {
127         testBackCameraImage();
128     });
129 }, "Switching from front to back camera, with lower resolution");
130
131 promise_test((test) => {
132     if (window.testRunner)
133         testRunner.setUserMediaPermission(true);
134
135     var sender;
136     var frontStream;
137     var backStream;
138
139     return navigator.mediaDevices.getUserMedia({ video: { height: { max: 400 }, facingMode: { exact: ["user"] } } }).then((stream) => {
140         frontStream = stream;
141         return navigator.mediaDevices.getUserMedia({ video: { height: { min: 400 }, facingMode: { exact: ["environment"] } } });
142     }).then((stream) => {
143         backStream = stream;
144         assert_true(frontStream.getVideoTracks()[0].getSettings().height < 400, "front stream should be smaller");
145         assert_true(backStream.getVideoTracks()[0].getSettings().height >= 400, "back stream should be bigger");
146     }).then(() => {
147         return new Promise((resolve, reject) => {
148             createConnections((firstConnection) => {
149                 sender = firstConnection.addTrack(frontStream.getVideoTracks()[0], frontStream);
150             }, (secondConnection) => {
151                 secondConnection.ontrack = (trackEvent) => {
152                     resolve(trackEvent.streams[0]);
153                 };
154             });
155             setTimeout(() => reject("Test timed out"), 5000);
156         });
157     }).then((remoteStream) => {
158         video.srcObject = remoteStream;
159         return video.play();
160     }).then(() => {
161         testFrontCameraImage();
162     }).then(() => {
163         return promise_rejects(test, "InvalidModificationError", sender.replaceTrack(backStream.getVideoTracks()[0]));
164     }).then(() => {
165         return waitFor(500);
166     }).then(() => {
167         testBackCameraImage();
168     });
169 }, "Switching from front to back camera, with higher resolution");
170         </script>
171     </body>
172 </html>