Improve debugging ability of some webrtc tests
[WebKit-https.git] / LayoutTests / webrtc / video-rotation.html
1 <!doctype html>
2 <html>
3     <head>
4         <meta charset="utf-8">
5         <title>Testing video rotation in basic video exchange </title>
6         <script src="../resources/testharness.js"></script>
7         <script src="../resources/testharnessreport.js"></script>
8     </head>
9     <body>
10         <video id="video" autoplay playsInline width="320" height="240"></video>
11         <canvas id="canvas1" width="320" height="240"></canvas>
12         <canvas id="canvas2" width="320" height="240"></canvas>
13         <canvas id="canvas3" width="320" height="240"></canvas>
14         <script src ="routines.js"></script>
15         <script>
16 function isVideoBlack(id)
17 {
18     var canvas = document.getElementById(id);
19     canvas.width = video.videoWidth;
20     canvas.height = video.videoHeight;
21     canvas.getContext('2d').drawImage(video, 0, 0, canvas.width, canvas.height);
22
23     imageData = canvas.getContext('2d').getImageData(0, 0, canvas.width, canvas.height);
24     data = imageData.data;
25     for (var cptr = 0; cptr < canvas.width * canvas.height; ++cptr) {
26         // Approximatively black pixels.
27         if (data[4 * cptr] > 30 || data[4 * cptr + 1] > 30 || data[4 * cptr + 2] > 30)
28             return false;
29     }
30     return true;
31 }
32
33 function pollVideoBlackCheck(expected, id, resolve)
34 {
35     if (isVideoBlack(id) === expected) {
36         resolve();
37         return;
38     }
39
40     setTimeout(() => pollVideoBlackCheck(expected, id, resolve), 100);
41 }
42
43 function checkVideoBlack(expected, id)
44 {
45     return new Promise((resolve, reject) => {
46         pollVideoBlackCheck(expected, id, resolve);
47         setTimeout(() => reject("checkVideoBlack timed out for '" + id + "', expected " + expected), 5000);
48     });
49 }
50
51 var track;
52 promise_test((test) => {
53     if (window.testRunner)
54         testRunner.setUserMediaPermission(true);
55
56     return navigator.mediaDevices.getUserMedia({video: {width: 320, height: 240, facingMode: "environment"}}).then((localStream) => {
57         return new Promise((resolve, reject) => {
58             track = localStream.getVideoTracks()[0];
59
60             createConnections((firstConnection) => {
61                 firstConnection.addTrack(track, localStream);
62                 if (window.internals)
63                     internals.applyRotationForOutgoingVideoSources(firstConnection);
64             }, (secondConnection) => {
65                 secondConnection.ontrack = (trackEvent) => {
66                     resolve(trackEvent.streams[0]);
67                 };
68             });
69             setTimeout(() => reject("Test timed out"), 5000);
70         });
71     }).then((remoteStream) => {
72         video.srcObject = remoteStream;
73         return video.play();
74     });
75 }, "Setting video exchange");
76
77 promise_test((test) => {
78    return checkVideoBlack(false, "canvas1");
79 }, "Track is enabled, video should not be black");
80
81 promise_test((test) => {
82     if (window.internals)
83         window.internals.setCameraMediaStreamTrackOrientation(track, 90);
84
85     return checkVideoBlack(false, "canvas1").then(() => {
86         return waitForVideoSize(video, 240, 320);
87     });
88 }, "Track is enabled and rotated, video should not be black and should change size");
89
90 promise_test((test) => {
91     track.enabled = false;
92     return waitFor(500).then(() => {
93         return checkVideoBlack(true, "canvas2");
94     })
95 }, "Track is disabled, video should be black");
96
97         </script>
98     </body>
99 </html>