-
Notifications
You must be signed in to change notification settings - Fork 23
/
index.html
259 lines (237 loc) · 7.64 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<html>
<head>
<title>WebRTC Streamer</title>
<link rel="icon" type="image/png" href="webrtc.png" />
<link rel="stylesheet" type="text/css" href="styles.css">
<script src="libs/adapter.min.js" ></script>
<script src="webrtcconfig.js" ></script>
<script src="webrtcstreamer.js" ></script>
<script>
// ------------------------------------------
// WebRTC connections
// ------------------------------------------
var webRtcServerList = {};
// ------------------------------------------
// decode URL arguments
// ------------------------------------------
var argurl = { video:location.search.slice(1) };
var argoptions = null;
var layout = null;
if (typeof URLSearchParams != 'undefined') {
var params = new URLSearchParams(location.search);
argurl = { video:params.get("video"), audio:params.get("audio") };
argoptions = params.get("options");
if (!argoptions) {
argoptions = webrtcConfig.options;
}
layout = params.get("layout");
} else {
console.log("URLSearchParams not supported then no argument could be used");
}
// ------------------------------------------
// get text from url object (video,audio)
// ------------------------------------------
function getText(url) {
var text;
if (url.video) {
text = url.video + " ";
}
if (url.audio && (url.audio != url.video)) {
text += url.audio + " ";
}
return text;
}
// ------------------------------------------
// get the div where to insert a video
// ------------------------------------------
function getContentDiv() {
var contentDiv = null;
if (document.getElementById("layout")) {
var divList = document.getElementsByTagName("div");
for (var i=0; i<divList.length; i++) {
if (divList[i].childNodes.length == 0) {
contentDiv = divList[i];
break;
}
}
} else {
contentDiv = document.getElementById("content");
}
return contentDiv;
}
// ------------------------------------------
// init device list
// ------------------------------------------
function onGetDeviceList(remoteDeviceList) {
var deviceList = [];
if (argurl.video || argurl.audio) {
deviceList.push( argurl );
}
if (remoteDeviceList) {
deviceList.push.apply( deviceList, remoteDeviceList );
}
// create navigation menu
var urllist = document.getElementById("menu");
for (var dev in deviceList) {
var url = deviceList[dev];
var option = document.createElement("a");
var videoTag = "video_" + JSON.stringify(url);
option.url = url;
option.text = getText(url);
option.id = "nav_" + videoTag;
option.onclick = function () {
if (this.className === "active") {
del(this.url);
} else {
add(this.url);
}
}
urllist.appendChild(option);
}
if (argurl.video || argurl.audio) {
add(argurl);
} else {
var nbVideos = 1;
if (layout) {
var splitLayout = layout.split("x");
var nbrow = parseInt(splitLayout[0]);
var nbcol = parseInt(splitLayout[1]);
nbVideos = nbrow*nbcol;
}
var random = deviceList.sort(() => .5 - Math.random()).slice(0,nbVideos);
random.forEach((stream) => {
add(stream);
});
}
}
// ------------------------------------------
// Fill version
// ------------------------------------------
function onVersion(version) {
document.getElementById("footer").innerHTML = "<p><a href='https://github.com/mpromonet/webrtc-streamer'>WebRTC-Streamer</a>"
+ " " + version.split(" ")[0] + "</p>";
}
// ------------------------------------------
// add a webrtc client connection
// ------------------------------------------
function del(url) {
var videoTag = "video_" + JSON.stringify(url);
// disconnect webrtc connection
var webrtcServer = webRtcServerList[videoTag];
if (webrtcServer) {
webrtcServer.disconnect();
webRtcServerList[videoTag] = undefined;
}
// remove the video element and its tile
var divElt = document.getElementById ("div_" + videoTag);
divElt.parentElement.removeChild(divElt);
// unhighlight the navigation
var navElt = document.getElementById ("nav_" + videoTag);
navElt.className = "";
}
// ------------------------------------------
// add a webrtc client connection
// ------------------------------------------
function add(url) {
var videoTag = "video_" + JSON.stringify(url);
// add a video element to display webrtc stream
if (document.getElementById (videoTag) === null) {
var contentDiv = getContentDiv();
if (contentDiv) {
let webstreamurl = webrtcConfig.url + "/webrtcstreamer.html?";
if (url.video) {
webstreamurl += "video=" + encodeURIComponent(url.video) + "&";
}
if (url.audio) {
webstreamurl += "audio=" + encodeURIComponent(url.audio) + "&";
}
if (argoptions) {
webstreamurl += "options=" + encodeURIComponent(argoptions) + "&";
}
var divelt = document.createElement("div");
divelt.id = "div_" + videoTag
var nameelt = document.createElement("h2");
nameelt.id = "title_" + videoTag
nameelt.innerHTML = "<a href='" + webstreamurl +"' >"+getText(url)+"</a>";
divelt.appendChild(nameelt);
var videoelt = document.createElement("video");
videoelt.id = videoTag;
videoelt.title = getText(url);
videoelt.muted = true;
videoelt.controls = true;
videoelt.playsinline = true;
if (layout) {
var splitLayout = layout.split("x");
var nbrow = parseInt(splitLayout[0]);
var nbcol = parseInt(splitLayout[1]);
videoelt.width = window.innerWidth / (nbcol+1)
videoelt.height = window.innerHeight / (nbrow+1)
}
divelt.appendChild(videoelt);
contentDiv.appendChild(divelt);
}
}
var videoelt = document.getElementById (videoTag);
if (videoelt) {
// connect video element to webrtc stream
var webRtcServer = new WebRtcStreamer(videoTag, webrtcConfig.url);
var options = argoptions;
if (layout) {
options += webrtcConfig.layoutextraoptions;
}
webRtcServer.connect(url.video, url.audio, options);
// highlight the navigation
var navElt = document.getElementById ("nav_" + videoTag);
navElt.className = "active";
// register webrtc streamer connection
webRtcServerList[videoTag] = webRtcServer;
}
}
// ------------------------------------------
// load/unload callbacks
// ------------------------------------------
window.onload = function() {
if (layout) {
var splitLayout = layout.split("x");
var nbrow = parseInt(splitLayout[0]);
var nbcol = parseInt(splitLayout[1]);
const layoutElement = document.createElement("div")
layoutElement.id = "layout"
layoutElement.style.display= "grid"
layoutElement.style.gridTemplateColumns= `repeat(${nbcol}, 1fr)`
for (var irow=0; irow<nbrow; irow++) {
for (var icol=0; icol<nbcol; icol++) {
const divElement = document.createElement("div")
divElement.id = "cell_" + irow + "_" + icol
divElement.style.width = "1fr"
divElement.style.height = "1fr"
layoutElement.appendChild(divElement);
}
}
let content = document.getElementById("content")
content.appendChild(layoutElement)
}
fetch(webrtcConfig.url + "/api/getMediaList")
.then( (response) => response.json() )
.then( (response) => onGetDeviceList( response ))
fetch(webrtcConfig.url + "/api/version")
.then( (response) => response.text() )
.then( (response) => onVersion( response ))
}
window.onbeforeunload = function() {
for (var url in webRtcServerList) {
webRtcServerList[url].disconnect()
}
};
</script>
</head>
<body>
<div id="container">
<header>
<nav id="menu"></nav>
</header>
<div id="content"></div>
<footer id="footer"></footer>
</div>
</body>
</html>