-
Notifications
You must be signed in to change notification settings - Fork 133
Expand file tree
/
Copy pathsimple-watcher.html
More file actions
67 lines (56 loc) · 2.02 KB
/
simple-watcher.html
File metadata and controls
67 lines (56 loc) · 2.02 KB
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
<!--
This example demonstrates how to watch a stream without libraries or dependencies.
With this HTML you can add a 'Broadcast Box Player' to any site you want
-->
<html>
<head>
<title>simple-watcher</title>
</head>
<body>
<b> WHEP URL </b> <input type="text" value="https://b.siobud.com/api/whep" id="whepURL" /> <br />
<b> Stream Key </b> <input type="text" id="streamKey" /> <br />
<button onclick="window.watchStream()"> Watch Stream </button>
<h3> Video </h3>
<video id="videoPlayer" autoplay muted controls style="width: 500"> </video>
<h3> Connection State </h3>
<div id="connectionState"></div> <br />
</body>
<script>
window.watchStream = () => {
const whepURL = document.getElementById('whepURL').value
if (whepURL === '') {
return window.alert('WHEP URL must not be empty')
}
const streamKey = document.getElementById('streamKey').value
if (streamKey === '') {
return window.alert('Stream Key must not be empty')
}
let peerConnection = new RTCPeerConnection()
peerConnection.addTransceiver('audio', { direction: 'recvonly' })
peerConnection.addTransceiver('video', { direction: 'recvonly' })
peerConnection.ontrack = function (event) {
document.getElementById('videoPlayer').srcObject = event.streams[0]
}
peerConnection.oniceconnectionstatechange = () => {
document.getElementById('connectionState').innerText = peerConnection.iceConnectionState;
}
peerConnection.createOffer().then(offer => {
peerConnection.setLocalDescription(offer)
fetch(whepURL, {
method: 'POST',
body: offer.sdp,
headers: {
Authorization: `Bearer ${streamKey}`,
'Content-Type': 'application/sdp'
}
}).then(r => r.text())
.then(answer => {
peerConnection.setRemoteDescription({
sdp: answer,
type: 'answer'
})
})
})
}
</script>
</html>