-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
215 lines (188 loc) · 7.13 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<title>WebRTSP</title>
<link rel="stylesheet" href="/3rd_party/spin-4.1.1.css">
<link rel="stylesheet" href="/css/WebRTSP.css">
<script type="text/javascript" src="/3rd_party/adapter-8.2.1.js"></script>
<script type="text/javascript" src="/Config.js"></script>
<script>
const LocalStorageLastStreamerKey = "last streamer";
const StreamerPrefix = "/"
let server = null;
if(window.location.protocol === 'http:')
server = `ws://${window.location.hostname}:${WebRTSPPort}/`;
else
server = `wss://${window.location.hostname}:${WebRTSPPort}/`;
function cleanStreamsContainer() {
const streamsContainer =
document.querySelector("#streamsContainer");
const childNodes = streamsContainer.childNodes;
for(let i = 0; i < childNodes.length;) {
const child = childNodes[i];
if(child.tagName != "SPAN") {
streamsContainer.removeChild(child);
} else {
++i;
}
}
}
function hideStreamsContainer() {
const streamsContainer =
document.querySelector("#streamsContainer");
streamsContainer.style.display = "none";
cleanStreamsContainer();
}
function showStreamsContainer() {
const streamsContainer =
document.querySelector("#streamsContainer");
streamsContainer.style.display = "";
}
function updateSources(list) {
const buttonsContainer =
document.querySelector("#buttonsContainer");
buttonsContainer.innerHTML = "";
if(list && list.size > 1) {
list.forEach((description, streamerName) => {
let button = document.createElement("button");
button.innerHTML = streamerName;
button.title = description;
button.dataset.streamerName = streamerName;
button.dataset.streamerDescription = description;
button.onclick = function() {
hideStreamsContainer();
document.title = this.dataset.streamerName;
localStorage.setItem(LocalStorageLastStreamerKey, this.dataset.streamerName)
connect(this.dataset.streamerName);
}
buttonsContainer.appendChild(button);
});
} else {
buttonsContainer.hidden = true;
}
}
function updateStreams(streamerName, list) {
const streamsContainer =
document.querySelector("#streamsContainer");
cleanStreamsContainer()
const timeFormat =
Intl.DateTimeFormat(undefined, { dateStyle: "medium", timeStyle: "medium"});
if(list) {
list.forEach((description, streamName) => {
let button = document.createElement("button");
let buttonText;
let buttonTitle = description;
try {
buttonText = timeFormat.format(new Date(description));
} catch(e) {
let substreamName = streamName;
if(substreamName.startsWith(`${streamerName}/`)) {
substreamName = substreamName.substring(streamerName.length + 1);
}
buttonText = substreamName;
}
button.innerHTML = buttonText;
button.title = buttonTitle;
button.dataset.streamerName = streamerName;
button.dataset.streamName = streamName;
button.dataset.streamDescription = description;
button.onclick = function() {
document.title = `${streamerName}, ${buttonText}`;
connect(this.dataset.streamName);
}
streamsContainer.appendChild(button);
});
showStreamsContainer();
} else {
hideStreamsContainer();
}
}
let webrtsp = null;
function connect(streamerName) {
webrtsp.connect(server, streamerName);
}
async function onLoad() {
let Spin = await import("/3rd_party/spin.min-4.1.1.js");
let WebRTSP = await import("/WebRTSP/WebRTSP.mjs");
const videoContainer =
document.querySelector("#videoContainer");
const videoElement =
document.querySelector("#video");
const spinner =
new Spin.Spinner({color:'#ffffff'});
const stunServer =
(typeof STUNServer === "string") ? STUNServer : "stun:stun.l.google.com:19302"
webrtsp = new WebRTSP.WebRTSP(
videoElement,
[{
urls: [stunServer]
}],
{ debug: true });
webrtsp.events.addEventListener("list", (event) => {
const uri = event.detail.uri;
if(uri == "*") {
const streamers = event.detail.list;
if(streamers.size > 0) {
const lastStreamerName = localStorage.getItem(LocalStorageLastStreamerKey);
let desiredStreamerName = null;
if(window.location.pathname.startsWith(StreamerPrefix)) {
try {
desiredStreamerName = decodeURI(window.location.pathname.substring(StreamerPrefix.length));
} catch {}
}
if(desiredStreamerName && streamers.has(desiredStreamerName)) {
updateSources(null);
webrtsp.streamerName = desiredStreamerName;
} else if(lastStreamerName && streamers.has(lastStreamerName)) {
updateSources(streamers);
webrtsp.streamerName = lastStreamerName;
} else {
updateSources(streamers);
const requiredItem = Math.floor(Math.random() * streamers.size);
let it = streamers.keys();
for(let i = 0; i < requiredItem; ++i)
it.next();
webrtsp.streamerName = it.next().value;
localStorage.setItem(LocalStorageLastStreamerKey, webrtsp.streamerName)
}
document.title = webrtsp.streamerName;
} else {
updateSources(null);
webrtsp.streamerName = "*";
document.title = "WebRTSP";
localStorage.removeItem(LocalStorageLastStreamerKey)
}
} else {
const streams = event.detail.list;
updateStreams(uri, streams);
spinner.stop()
event.preventDefault();
}
});
webrtsp.events.addEventListener("disconnected", (event) => {
spinner.spin(videoContainer);
});
video.addEventListener('playing', (event) => {
spinner.stop()
});
spinner.spin(videoContainer);
connect();
}
</script>
</head>
<body onload="onLoad()">
<div id="rootContainer">
<div id="buttonsContainer">
</div>
<div id="bodyContainer">
<div id="streamsContainer" style="display: none;">
<span> Substreams: </span>
</div>
<div id="videoContainer">
<video id="video" autoplay="true" muted="muted"> </video>
</div>
</div>
</div>
</body>
</html>