d72ce809b1954a13e9066c717d371f156b5c7b1a
[WebKit-https.git] / Tools / TestWebKitAPI / Tests / WebKit / getUserMedia.html
1 <!DOCTYPE html>
2 <html>
3     <head>
4         <script>
5
6             let stream = null;
7
8             async function promptForCapture()
9             {
10                 try {
11                     await navigator.mediaDevices.enumerateDevices();
12                     const stream = await navigator.mediaDevices.getUserMedia({ audio: false, video: true })
13                     video.srcObject = stream;
14                     console.log("Got user media");
15                 } catch(error) {
16                     console.log(`Failed with error: ${error}`);
17                 }
18             }
19
20             function stop(kind)
21             {
22                 let activeTracks = [];
23                 stream.getTracks().forEach(track => {
24                     if (!kind || track.kind == kind)
25                         track.stop();
26                     else
27                         activeTracks.push(track);
28                 });
29
30                 if (!activeTracks.length) {
31                     stream = null;
32                     video.srcObject = null;
33                 }
34             }
35
36             function haveStream()
37             {
38                 return stream !== null;
39             }
40         </script>
41     <head>
42
43     <body onload="promptForCapture()">
44         <video id="video" controls></video>
45         <p>
46         <button onclick="stop()">Stop</button>
47         </p>
48     </body>
49 </html>