3128f230d627268fb8896ad1ae619c54dc949298
[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=""></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 var track;
34 promise_test((test) => {
35     if (window.testRunner)
36         testRunner.setUserMediaPermission(true);
37
38     return navigator.mediaDevices.getUserMedia({ video: true}).then((localStream) => {
39         return new Promise((resolve, reject) => {
40             track = localStream.getVideoTracks()[0];
41
42             createConnections((firstConnection) => {
43                 firstConnection.addTrack(track, localStream);
44             }, (secondConnection) => {
45                 secondConnection.ontrack = (trackEvent) => { resolve(trackEvent.streams[0]); };
46             });
47             setTimeout(() => reject("Test timed out"), 5000);
48         });
49     }).then((remoteStream) => {
50         video.srcObject = remoteStream;
51         return video.play();
52     }).then(() => {
53         return waitFor(100);
54     }).then(() => {
55          assert_false(isVideoBlack(), "track is enabled, video is not black");
56     }).then(() => {
57         track.enabled = false;
58         return waitFor(500);
59     }).then(() => {
60         assert_true(isVideoBlack(), "track is disabled, video is black");
61         track.enabled = true;
62         return waitFor(500);
63     }).then(() => {
64         assert_false(isVideoBlack(), "track is reenabled, video is not black");
65     });
66 }, "Outgoing muted/unmuted video track");
67         </script>
68     </body>
69 </html>