-
Notifications
You must be signed in to change notification settings - Fork 256
/
Copy pathindex.html
68 lines (57 loc) · 1.76 KB
/
index.html
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
56
57
58
59
60
61
62
63
64
65
66
67
68
<html>
<!--
SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
SPDX-License-Identifier: MIT
-->
<head>
<title>snapshot</title>
</head>
<body>
<button onclick="window.publishVideo()"> Publish Video </button>
<button onclick="window.generateSnapshot()"> Generate Snapshot </button>
<br />
<h3> Snapshot </h3>
<img id="snapshot"> </div> <br />
<h3> Video Stream </h3>
<video id="localVideo" autoplay="true" controls="true"></video> <br />
<h3> Logs </h3>
<div id="logs"></div>
</body>
<script>
var log = msg => {
document.getElementById('logs').innerHTML += msg + '<br>'
}
window.publishVideo = () => {
let pc = new RTCPeerConnection()
navigator.mediaDevices.getUserMedia({video: true})
.then(stream => {
document.getElementById('localVideo').srcObject = stream
stream.getTracks().forEach(function(track) {
pc.addTrack(track, stream)
})
pc.createOffer()
.then(offer => {
pc.setLocalDescription(offer)
return fetch(`/signal`, {
method: 'post',
headers: {
'Accept': 'application/json, text/plain, */*',
'Content-Type': 'application/json'
},
body: JSON.stringify(offer)
})
})
.then(res => res.json())
.then(res => pc.setRemoteDescription(res))
.catch(log)
}).catch(log)
}
window.generateSnapshot = () => {
fetch(`/snapshot`)
.then(response => response.blob())
.then(image => {
document.getElementById('snapshot').src = URL.createObjectURL(image)
})
}
</script>
</html>