[iOS] Unset active media capture source when stopped capturing
[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(testName)
29 {
30     test(() => {
31         data = grabImagePixels();
32
33         assert_true(data[0] < 20, "1");
34         assert_true(data[1] < 20, "2");
35         assert_true(data[2] < 20, "3");
36     }, "testFrontCameraImage " + testName);
37 }
38
39 function testBackCameraImage(testName)
40 {
41     test(() => {
42         data = grabImagePixels();
43
44         assert_true(data[0] > 100, "1");
45         assert_true(data[1] > 100, "2");
46         assert_true(data[2] > 100, "3");
47
48         assert_true(data[0] < 200, "4");
49         assert_true(data[1] < 200, "5");
50         assert_true(data[2] < 200, "6");
51     }, "testBackCameraImage " + testName);
52 }
53
54 promise_test((test) => {
55     if (window.testRunner)
56         testRunner.setUserMediaPermission(true);
57
58     var sender;
59     var frontStream;
60     var backStream;
61     return navigator.mediaDevices.getUserMedia({ video: { facingMode: { exact: ["user"] } } }).then((stream) => {
62         frontStream = stream;
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("test1");
78         return navigator.mediaDevices.getUserMedia({ video: { facingMode: { exact: ["environment"] } } });
79     }).then((stream) => {
80         backStream = stream;
81         var currentTrack = sender.track;
82         promise = sender.replaceTrack(backStream.getVideoTracks()[0]);
83         assert_true(currentTrack === sender.track);
84         return promise;
85     }).then(() => {
86         assert_true(sender.track === backStream.getVideoTracks()[0]);
87         return waitFor(500);
88     }).then(() => {
89         testBackCameraImage("test1");
90     });
91 }, "Switching from front to back camera");
92
93 promise_test((test) => {
94     if (window.testRunner)
95         testRunner.setUserMediaPermission(true);
96
97     var sender;
98     var frontStream;
99     var backStream;
100
101     return navigator.mediaDevices.getUserMedia({ video: { width: 640, height: 480, facingMode: { exact: ["user"] } } }).then((stream) => {
102         frontStream = stream;
103         assert_true(frontStream.getVideoTracks()[0].getSettings().height === 480, "frontStream should be big");
104         return new Promise((resolve, reject) => {
105             createConnections((firstConnection) => {
106                 sender = firstConnection.addTrack(frontStream.getVideoTracks()[0], frontStream);
107             }, (secondConnection) => {
108                 secondConnection.ontrack = (trackEvent) => {
109                     resolve(trackEvent.streams[0]);
110                 };
111             });
112             setTimeout(() => reject("Test timed out"), 5000);
113         });
114     }).then((remoteStream) => {
115         video.srcObject = remoteStream;
116         return video.play();
117     }).then(() => {
118         testFrontCameraImage("test2");
119         return navigator.mediaDevices.getUserMedia({ video: { width: 320, height: 240, facingMode: { exact: ["environment"] } } });
120     }).then((stream) => {
121         backStream = stream;
122         assert_true(backStream.getVideoTracks()[0].getSettings().height === 240, "backStream should be small");
123         return sender.replaceTrack(backStream.getVideoTracks()[0]);
124     }).then(() => {
125         return waitFor(500);
126     }).then(() => {
127         testBackCameraImage("test2");
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: { width: 320, height: 240, facingMode: { exact: ["user"] } } }).then((stream) => {
140         frontStream = stream;
141         assert_true(frontStream.getVideoTracks()[0].getSettings().height === 240, "front stream should be small");
142     }).then(() => {
143         return new Promise((resolve, reject) => {
144             createConnections((firstConnection) => {
145                 sender = firstConnection.addTrack(frontStream.getVideoTracks()[0], frontStream);
146             }, (secondConnection) => {
147                 secondConnection.ontrack = (trackEvent) => {
148                     resolve(trackEvent.streams[0]);
149                 };
150             });
151             setTimeout(() => reject("Test timed out"), 5000);
152         });
153     }).then((remoteStream) => {
154         video.srcObject = remoteStream;
155         return video.play();
156     }).then(() => {
157         testFrontCameraImage("test3");
158         return navigator.mediaDevices.getUserMedia({ video: { width: 640, height: 480 , facingMode: { exact: ["environment"] } } });
159     }).then((stream) => {
160         backStream = stream;
161         assert_true(backStream.getVideoTracks()[0].getSettings().height === 480, "back stream should be big");
162         return promise_rejects(test, "InvalidModificationError", sender.replaceTrack(backStream.getVideoTracks()[0]));
163     });
164 }, "Switching from front to back camera, with higher resolution");
165         </script>
166     </body>
167 </html>