summaryrefslogtreecommitdiffstats
path: root/tests/manual/widgets/webrtc/index.html
blob: ee813d306c101bf17605d0a9e9df29b0c039f1cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<!doctype html>
<html>
    <head>
        <style>
            body {
                display: flex;
                justify-content: center;
                align-items: center;
                flex-wrap: wrap;
                flex-flow: column;
            }
            buttons {
                justify-content: space-around;
            }
        </style>
    </head>
    <body>
        <div id="buttons" >
            <input value ="getDisplayMedia" onclick="getDisplayMedia(true, true);" type="button">
            <input value ="Stop" onclick="stop();" type="button">
        </div>
        <div id="content"></div>
    </body>
    <script>
        const content = document.getElementById("content");
        const video = document.createElement("video");
        video.setAttribute("width", 640);
        video.setAttribute("height", 640);
        video.setAttribute("style", "background-color: black;");
        content.appendChild(video);

        async function getDisplayMedia(v = true, a = true) {
            stop();
            navigator.mediaDevices.getDisplayMedia({ video: v, audio: a })
            .then(stream => {
                start(stream);
            }, error => {
            console.error(error);
            });
        }
        function stop() {
            if (video.srcObject)
                for (const track of video.srcObject.getTracks())
                    track.stop()
            video.srcObject = null;
            video.setAttribute("style", "background-color: black;");
        }

        function start(stream) {
            video.srcObject = stream;
            video.play();
        }

    </script>
</html>