-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
207 lines (180 loc) · 6.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
<html>
<head>
<meta charset="utf-8" />
<title>3D Imaging for Crop Analytics</title>
<link rel="stylesheet" type="text/css" href="styles.css" />
<!-- <link rel="icon" type="image/ico" href="open3d_logo.ico" /> -->
<script src="libs/adapter.min.js"></script>
<script src="webrtcstreamer.js"></script>
<script>
/**
* Global WebRTC configs.
*/
let webRtcOptions = "rtptransport=tcp&timeout=60";
/**
* WebRTC connections.
*/
let webRtcServerList = {};
/**
* Get the div where to insert a video.
*/
function getContentDiv() {
let contentDiv = document.getElementById("content");
return contentDiv;
}
function setDifference(setA, setB) {
let _difference = new Set(setA);
for (let elem of setB) {
_difference.delete(elem);
}
return _difference;
}
/**
* Init device list
* @param {object} mediaList A map of media. E.g.
* let mediaList = {
* 0: {video: "window_0"},
* 1: {video: "window_1"},
* }
*/
function updateMediaList(mediaList) {
// Create navigation menu.
let menu = document.getElementById("menu");
// The current video source that the server is serving.
let validWindowIds = new Set();
for (let key in mediaList) {
validWindowIds.add(mediaList[key].video);
}
// Collect stale and new URLs. Avoid "removing while iterating".
let existingWindowIds = new Set();
let staleWindowIds = new Array();
for (let i = 0; i < menu.children.length; i++) {
let navElt = menu.children[i];
let windowId = navElt.windowId;
existingWindowIds.add(windowId);
if (!validWindowIds.has(windowId)) {
staleWindowIds.push(windowId);
}
}
let newWindowIds = [
...setDifference(validWindowIds, existingWindowIds),
];
// Remove stale URLs.
staleWindowIds.forEach(function (windowId) {
let videoId = "video_" + windowId;
let id = "nav_" + videoId;
document.getElementById(id).remove();
});
// Append new URLs to the menu bar.
newWindowIds.forEach(function (windowId) {
let videoId = "video_" + windowId;
let navId = "nav_" + videoId;
let navElt = document.createElement("a");
// navElt.text = '3D Imaging viewer';
navElt.windowId = windowId;
navElt.id = navId;
navElt.onclick = function () {
if (this.className === "active") {
delConnection(this.windowId);
} else {
addConnection(this.windowId);
}
};
menu.appendChild(navElt);
});
// Connect to the new URLs, this will also highlight the menu entry.
newWindowIds.forEach((windowId) => {
addConnection(windowId);
});
}
/**
* Delete a WebRTC client connection.
*/
function delConnection(windowId) {
console.log("delConnection: ", windowId);
let videoId = "video_" + windowId;
// Disconnect WebRTC connection.
let webrtcServer = webRtcServerList[videoId];
if (webrtcServer) {
webrtcServer.disconnect();
webRtcServerList[videoId] = undefined;
}
}
/**
* Add a WebRTC client connection.
*/
function addConnection(windowId) {
let videoId = "video_" + windowId;
// Add a video element to display WebRTC stream.
if (document.getElementById(videoId) === null) {
let contentDiv = getContentDiv();
if (contentDiv) {
let divElt = document.createElement("div");
divElt.id = "div_" + videoId;
let nameElt = document.createElement("h2");
nameElt.id = "title_" + videoId;
nameElt.innerHTML = "<div>" + windowId + "</div>";
divElt.appendChild(nameElt);
let videoElt = document.createElement("video");
videoElt.id = videoId;
videoElt.title = windowId;
videoElt.muted = true;
videoElt.controls = false;
videoElt.playsinline = true;
videoElt.innerText = "Your browser does not support HTML5 video.";
divElt.appendChild(videoElt);
contentDiv.appendChild(divElt);
}
}
let videoElt = document.getElementById(videoId);
if (videoElt) {
let onClose = function () {
console.log("onClose() called for videoId:", videoId);
// Remove the video element and its tile.
let divElt = document.getElementById("div_" + videoId);
divElt.parentElement.removeChild(divElt);
// Un-highlight the navigation.
let navElt = document.getElementById("nav_" + videoId);
navElt.className = "";
WebRtcStreamer.getMediaList()
.then((response) => response.json())
.then((response) => updateMediaList(response));
};
// Connect video element to WebRTC stream.
let webRtcClient = new WebRtcStreamer(videoId, "", onClose, null);
console.log("[addConnection] videoId: " + videoId);
webRtcClient.connect(windowId, /*audio*/ null, webRtcOptions);
console.log("[addConnection] windowId: " + windowId);
console.log("[addConnection] options: " + webRtcOptions);
// Highlight the navigation.
let navElt = document.getElementById("nav_" + videoId);
navElt.className = "active";
// Register WebRTC streamer connection.
webRtcServerList[videoId] = webRtcClient;
}
document.getElementById('nav_video_window_0').innerHTML = '3D Imaging';
document.getElementById('nav_video_window_0').style.display = "none";
document.getElementById('title_video_window_0').style.padding = "10px 0 10px 0";
document.getElementById('title_video_window_0').innerHTML = '<div>3D Imaging Reconstruction viewer</div>';
}
/**
* Load/unload callbacks.
*/
window.onload = function () {
WebRtcStreamer.getMediaList()
.then((response) => response.json())
.then((response) => updateMediaList(response));
};
window.onbeforeunload = function () {
for (let key in webRtcServerList) {
webRtcServerList[key].disconnect();
}
};
</script>
</head>
<body>
<nav id="menu"></nav>
<div id="content"></div>
<footer id="footer"></footer>
</body>
</html>