-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.html
71 lines (63 loc) · 2.09 KB
/
player.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
69
70
71
<html>
<head>
<title>WebRTC player</title>
<style>
body {
background-color: rgba(0, 0, 0, 0);
margin: 0 auto;
overflow: hidden;
}
#webrtcVideo {
width: 100%;
}
#webrtcAudio {
opacity: 0;
}
</style>
</head>
<body>
<video id="webrtcVideo" width="100%" autoplay></video>
<audio id="webrtcAudio" autoplay style="opacity: 0"></audio>
</body>
</html>
<script>
const streamUrl = location.hash.length > 0 ? location.hash.slice(1) : "";
let pc = undefined;
const webrtcVideo = document.getElementById("webrtcVideo");
const webrtcAudio = document.getElementById("webrtcAudio");
const getRelativeUrl = (url) =>
new URL(url, `${location.protocol}//${location.host}${location.pathname === '/' ? '' : location.pathname}`).href;
const getConnectionUrl = (streamSource) =>
getRelativeUrl("c?source=" + encodeURIComponent(streamSource));
function playStream() {
pc?.close();
webrtcVideo.srcObject = null;
webrtcAudio.srcObject = null;
pc = new RTCPeerConnection();
pc.ontrack = function (event) {
if (event.track.kind === "video") {
webrtcVideo.srcObject = event.streams[0];
}
if (event.track.kind === "audio") {
webrtcAudio.srcObject = event.streams[event.streams.length - 1];
}
};
pc.addTransceiver("video");
pc.addTransceiver("audio");
pc.createOffer()
.then(offer => {
pc.setLocalDescription(offer)
return fetch(getConnectionUrl(streamUrl), {
method: "post",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(offer),
})
})
.then(res => res.json())
.then(res => pc.setRemoteDescription(res))
.catch((e) => console.error("Failed to fetch peer connection info", e));
}
playStream();
</script>