Update device orientation & motion permission native SPI as per latest proposal
[WebKit-https.git] / LayoutTests / webrtc / video-setDirection.html
1 <!doctype html>
2 <html>
3     <head>
4         <meta charset="utf-8">
5         <title>Testing video exchange using setDirection with renegotiation 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 grabFrameData(x, y, w, h)
18 {
19     canvas.width = video.videoWidth;
20     canvas.height = video.videoHeight;
21
22     canvas.getContext('2d').drawImage(video, x, y, w, h, x, y, w, h);
23     return canvas.getContext('2d').getImageData(x, y, w, h).data;
24 }
25
26 function testImage()
27 {
28     const data = grabFrameData(10, 325, 250, 1);
29
30     var index = 20;
31     assert_true(data[index] < 100);
32     assert_true(data[index + 1] < 100);
33     assert_true(data[index + 2] < 100);
34
35     index = 80;
36     assert_true(data[index] > 200);
37     assert_true(data[index + 1] > 200);
38     assert_true(data[index + 2] > 200);
39
40     index += 80;
41     assert_true(data[index] > 200);
42     assert_true(data[index + 1] > 200);
43     assert_true(data[index + 2] < 100);
44 }
45
46 var pc1, pc2;
47 async function renegotiate()
48 {
49     let d = await pc1.createOffer();
50     await pc1.setLocalDescription(d);
51     await pc2.setRemoteDescription(d);
52     d = await pc2.createAnswer();
53     await pc1.setRemoteDescription(d);
54     await pc2.setLocalDescription(d);
55 }
56
57 promise_test(async (t) => {
58     if (window.testRunner)
59         testRunner.setUserMediaPermission(true);
60
61     const localStream = await navigator.mediaDevices.getUserMedia({video: true});
62     const stream = await new Promise((resolve, reject) => {
63         createConnections((firstConnection) => {
64             pc1 = firstConnection;
65             firstConnection.addTrack(localStream.getVideoTracks()[0], localStream);
66         }, (secondConnection) => {
67             pc2 = secondConnection;
68             secondConnection.ontrack = (trackEvent) => { resolve(trackEvent.streams[0]); };
69         });
70         setTimeout(() => reject("Test timed out"), 5000);
71     });
72
73     video.srcObject = stream;
74     await video.play();
75
76     testImage();
77
78     let promise = new Promise((resolve) => {
79         pc2.getReceivers()[0].track.onmute = resolve;
80     });
81
82     pc1.getTransceivers()[0].direction = "inactive";
83     await renegotiate();
84     await promise;
85
86     promise = new Promise((resolve) => {
87         pc2.getReceivers()[0].track.onunmute = resolve;
88     });
89
90     pc1.getTransceivers()[0].direction = "sendrecv";
91     const streamPromise = new Promise(resolve => {
92         pc2.ontrack = (trackEvent) => { resolve (trackEvent.streams[0]); };
93     });
94
95     await renegotiate();
96     video.srcObject = await streamPromise;
97     await promise;
98
99     test(() => {
100         assert_equals(stream, video.srcObject);
101     }, "The MediaStream should remain the same");
102     await video.play();
103
104     testImage();
105 }, "Going from sendrecv to inactive and back to sendrecv");
106         </script>
107     </body>
108 </html>