LayoutTest webrtc/video-mute.html is a flaky failure
[WebKit-https.git] / LayoutTests / webrtc / video-mute.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         <video id="video" autoplay width="640" height="480"></video>
11         <canvas id="canvas" width="640" height="480"></canvas>
12         <script src ="routines.js"></script>
13         <script>
14 video = document.getElementById("video");
15 canvas = document.getElementById("canvas");
16
17 function isVideoBlack()
18 {
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] > 10 || data[4 * cptr + 1] > 10 || data[4 * cptr + 2] > 10)
28             return false;
29     }
30     return true;
31 }
32
33 function pollVideoBlackCheck(expected, resolve)
34 {
35     if (isVideoBlack() === expected)
36          resolve();
37
38     setTimeout(() => pollVideoBlackCheck(expected, resolve), 50);
39 }
40
41 function checkVideoBlack(expected, message)
42 {
43     return new Promise((resolve, reject) => {
44        pollVideoBlackCheck(expected, resolve);
45         setTimeout(() => reject(message), 5000);
46     });
47 }
48
49 var track;
50 promise_test((test) => {
51     if (window.testRunner)
52         testRunner.setUserMediaPermission(true);
53
54     return navigator.mediaDevices.getUserMedia({ video: {width: 640, height: 480}}).then((localStream) => {
55         return new Promise((resolve, reject) => {
56             track = localStream.getVideoTracks()[0];
57
58             createConnections((firstConnection) => {
59                 firstConnection.addTrack(track, localStream);
60             }, (secondConnection) => {
61                 secondConnection.ontrack = (trackEvent) => { resolve(trackEvent.streams[0]); };
62             });
63             setTimeout(() => reject("Test timed out"), 5000);
64         });
65     }).then((remoteStream) => {
66         video.srcObject = remoteStream;
67         return video.play();
68     }).then(() => {
69         return checkVideoBlack(false, "track is enabled, video should not be black");
70     }).then(() => {
71         track.enabled = false;
72         return checkVideoBlack(true, "track is disabled, video should be black");
73     }).then(() => {
74         track.enabled = true;
75         return checkVideoBlack(false, "track is reenabled, video should not be black");
76     });
77 }, "Outgoing muted/unmuted video track");
78         </script>
79     </body>
80 </html>