Improve WebRTC track enabled support
[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 // FIXME: We should use tracks
17
18 function isVideoBlack()
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(0, 0, canvas.width, canvas.height);
25     data = imageData.data;
26     for (var cptr = 0; cptr < canvas.width * canvas.height; ++cptr) {
27         // Approximatively black pixels.
28         if (data[4 * cptr] > 10 || data[4 * cptr + 1] > 10 || data[4 * cptr + 2] > 10)
29             return false;
30     }
31     return true;
32 }
33
34 var track;
35 promise_test((test) => {
36     if (window.testRunner)
37         testRunner.setUserMediaPermission(true);
38
39     return navigator.mediaDevices.getUserMedia({ video: true}).then((localStream) => {
40         return new Promise((resolve, reject) => {
41             if (window.internals)
42                 internals.useMockRTCPeerConnectionFactory("TwoRealPeerConnections");
43
44             track = localStream.getVideoTracks()[0];
45
46             createConnections((firstConnection) => {
47                 firstConnection.addStream(localStream);
48             }, (secondConnection) => {
49                 secondConnection.onaddstream = (streamEvent) => { resolve(streamEvent.stream); };
50             });
51             setTimeout(() => reject("Test timed out"), 5000);
52         });
53     }).then((remoteStream) => {
54         video.srcObject = remoteStream;
55         return video.play();
56     }).then(() => {
57          assert_false(isVideoBlack(), "track is enabled, video is not black");
58     }).then(() => {
59         track.enabled = false;
60         return waitFor(500);
61     }).then(() => {
62         assert_true(isVideoBlack(), "track is disabled, video is black");
63         track.enabled = true;
64         return waitFor(500);
65     }).then(() => {
66         assert_false(isVideoBlack(), "track is reenabled, video is not black");
67     });
68 }, "Outgoing muted/unmuted video track");
69         </script>
70     </body>
71 </html>