-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
74 lines (65 loc) · 2.65 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
69
70
71
72
73
74
<html>
<head>
<title>Mpeg TS / Rtmp to WebRTC</title>
</head>
<body>
<h1>Mpeg TS / Rtmp to WebRTC</h1>
<p>
<label for="streamUrl">Stream Url</label>
<input id="streamUrl" type="text" value=""/>
<button id="streamOpenBtn">Open</button>
</p>
<p><a href="" id="connectionUrlLink"></a></p>
<p>Offer url: <i id="connectionUrl"></i></p>
<div style="max-width: 100%;">
<video id="webrtcVideo" width="100%" style="max-width: 800px;" autoplay controls=""></video>
<audio id="webrtcAudio" autoplay controls=""></audio>
</div>
</body>
</html>
<script>
let pc = undefined;
document.getElementById("streamOpenBtn").addEventListener("click", () => playStream());
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));
const getPlayerUrl = (streamSource) =>
getRelativeUrl("player.html#" + 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)
const streamUrl = document.getElementById("streamUrl").value;
document.getElementById("connectionUrl").innerText = getConnectionUrl(streamUrl);
document.getElementById("connectionUrlLink").innerText = getPlayerUrl(streamUrl);
document.getElementById("connectionUrlLink").href = getPlayerUrl(streamUrl);
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));
}
</script>