a03b219e0db2c6e6667da2603a25b53777d6045a
[WebKit-https.git] / LayoutTests / webrtc / video-mute.html
1 <!doctype html>
2 <html>
3     <head>
4         <meta charset="utf-8">
5         <title>Testing muting video</title>
6         <script src="../resources/testharness.js"></script>
7         <script src="../resources/testharnessreport.js"></script>
8     </head>
9     <body>
10         <div>Video should be running, go to black and running.</div>
11         <div>Following, should be a snapshot of the video, a black frame and a snapshot of the video.</div>
12         <video id="video" autoplay playsInline width="320" height="240"></video>
13         <canvas id="canvas1" width="320" height="240"></canvas>
14         <canvas id="canvas2" width="320" height="240"></canvas>
15         <canvas id="canvas3" width="320" height="240"></canvas>
16         <script src ="routines.js"></script>
17         <script>
18 function isVideoBlack(id)
19 {
20     var canvas = document.getElementById(id);
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(0, 0, canvas.width, canvas.height);
26     data = imageData.data;
27     for (var cptr = 0; cptr < canvas.width * canvas.height; ++cptr) {
28         // Approximatively black pixels.
29         if (data[4 * cptr] > 10 || data[4 * cptr + 1] > 10 || data[4 * cptr + 2] > 10)
30             return false;
31     }
32     return true;
33 }
34
35 function pollVideoBlackCheck(expected, id, resolve)
36 {
37     if (isVideoBlack(id) === expected) {
38         resolve();
39         return;
40     }
41
42     setTimeout(() => pollVideoBlackCheck(expected, id, resolve), 50);
43 }
44
45 function checkVideoBlack(expected, id)
46 {
47     return new Promise((resolve, reject) => {
48        pollVideoBlackCheck(expected, id, resolve);
49         setTimeout(() => reject("checkVideoBlack timed out for " + id + " expected " + expected), 5000);
50     });
51 }
52
53 var track;
54 var remoteTrack;
55 var receivingConnection;
56 promise_test((test) => {
57     if (window.testRunner)
58         testRunner.setUserMediaPermission(true);
59
60     return navigator.mediaDevices.getUserMedia({video: {width: 320, height: 240, facingMode: "environment"}}).then((localStream) => {
61         return new Promise((resolve, reject) => {
62             track = localStream.getVideoTracks()[0];
63
64             createConnections((firstConnection) => {
65                 firstConnection.addTrack(track, localStream);
66             }, (secondConnection) => {
67                 receivingConnection = secondConnection;
68                 secondConnection.ontrack = (trackEvent) => {
69                     remoteTrack = trackEvent.track;
70                     resolve(trackEvent.streams[0]);
71                 };
72             });
73             setTimeout(() => reject("Test timed out"), 5000);
74         });
75     }).then((remoteStream) => {
76         video.srcObject = remoteStream;
77         return video.play();
78     });
79 }, "Setting video exchange");
80
81 promise_test(() => {
82     if (receivingConnection.connectionState === "connected")
83         return Promise.resolve();
84     return new Promise((resolve, reject) => {
85         receivingConnection.onconnectionstatechange = () => {
86             if (receivingConnection.connectionState === "connected")
87                 resolve();
88         };
89         setTimeout(() => reject("Test timed out"), 5000);
90     });
91 }, "Ensuring connection state is connected");
92
93 promise_test((test) => {
94     return checkVideoBlack(false, "canvas1");
95 }, "Track is enabled, video should not be black");
96
97 promise_test((test) => {
98     track.enabled = false;
99     return checkVideoBlack(true, "canvas2");
100 }, "Track is disabled, video should be black");
101
102 promise_test((test) => {
103     if (!window.internals)
104         return Promise.reject("This test requires internals API");
105     internals.observeMediaStreamTrack(remoteTrack);
106     let counter = internals.trackVideoSampleCount;
107     return new Promise((resolve, reject) => {
108         setInterval(() => {
109             // Should get 1 frame per second.
110             if (internals.trackVideoSampleCount - counter >= 2)
111                 resolve();
112         }, 300);
113         setTimeout(() => reject("did not get frames"), 5000);
114     });
115 }, "If disabled, black frames should still be coming");
116
117 promise_test((test) => {
118     track.enabled = true;
119     return checkVideoBlack(true, "canvas2");
120 }, "Track is enabled, video should not be black");
121
122         </script>
123     </body>
124 </html>