generated from amattu2/project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
219 lines (194 loc) · 6.15 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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Blink Liveview Middleware - Demo</title>
<style>
body {
font-family: sans-serif;
margin: 0;
padding: 8px;
background: #757575;
color: #000;
}
h1 {
margin: 0
}
video {
width: 100%;
max-width: 640px;
}
</style>
</head>
<body>
<h1>Blink Liveview Middleware - Demo</h1>
<div>
<div>
<h2>Controls</h2>
<button id="open">Open</button>
<button id="close" disabled>Close</button>
<button id="toggle-liveview" disabled>Start Liveview</button>
</div>
<div>
<h2>Connection Credentials</h2>
<form>
<div>
<label for="account-region">Account Region</label>
<input type="text" id="account-region" placeholder="u011" />
</div>
<div>
<label for="api-token">API Token</label>
<input type="text" id="api-token" placeholder="" />
</div>
<div>
<label for="account-id">Account ID</label>
<input type="number" id="account-id" placeholder="57381" />
</div>
<div>
<label for="network-id">Network ID</label>
<input type="number" id="network-id" placeholder="128991" />
</div>
<div>
<label for="camera-id">Camera ID</label>
<input type="number" id="camera-id" placeholder="122" />
</div>
<div>
<label for="camera-type">Camera Type</label>
<input type="text" id="camera-type" placeholder="owl" />
</div>
</form>
</div>
<hr />
<div>
<h2>Video stream</h2>
<video id="video" alt="Video stream" controls />
</div>
<div>
<h2>Raw WebSocket Output</h2>
<div id="output" style="max-height: 400px; overflow-y: scroll"></div>
</div>
</div>
<script>
var ws;
var liveviewActive = false;
const mediaSource = new MediaSource();
let queue = [];
let sourceBuffer = null;
document.getElementById("video").src = URL.createObjectURL(mediaSource);
mediaSource.addEventListener('sourceopen', () => {
if (!mediaSource.sourceBuffers.length) {
sourceBuffer = mediaSource.addSourceBuffer('video/mp4; codecs="avc1.640029,mp4a.40.2"');
sourceBuffer.mode = "segments";
}
sourceBuffer.addEventListener('updateend', () => {
if (queue.length > 0 && !sourceBuffer.updating) {
sourceBuffer.appendBuffer(queue.shift());
}
});
});
const output = document.getElementById("output");
const print = (data) => {
var d = document.createElement("div");
d.textContent = data;
output.appendChild(d);
output.scroll(0, output.scrollHeight);
};
const toggleButtons = (state) => {
document.getElementById("open").disabled = !state;
document.getElementById("toggle-liveview").disabled = state;
document.getElementById("close").disabled = state;
toggleLiveview(false);
};
const toggleLiveview = (state) => {
liveviewActive = state;
document.getElementById("toggle-liveview").textContent = !state ? "Start Liveview" : "Stop Liveview";
};
document.getElementById("open").onclick = function (evt) {
if (ws) {
return false;
}
ws = new WebSocket("ws://localhost:8080/liveview");
ws.binaryType = "arraybuffer";
ws.onopen = function (evt) {
print("OPEN");
toggleButtons(false);
}
ws.onclose = function (evt) {
print("CLOSE");
toggleButtons(true);
ws = null;
}
ws.onmessage = function (evt) {
if (evt.data instanceof ArrayBuffer) {
queue.push(new Uint8Array(evt.data));
if (sourceBuffer && !sourceBuffer.updating) {
sourceBuffer.appendBuffer(queue.shift());
}
return;
}
if (JSON.parse(evt.data)?.command === "liveview:stop") {
toggleLiveview(false);
document.getElementById("video").pause();
} else if (JSON.parse(evt.data)?.command === "liveview:start") {
document.getElementById("video").play();
}
print("RESPONSE: " + evt.data);
}
ws.onerror = function (evt) {
print("ERROR: " + evt.data);
}
return false;
};
document.getElementById("toggle-liveview").onclick = function (evt) {
if (!ws) {
return false;
}
if (liveviewActive) {
const data = {
command: "liveview:stop",
data: {},
};
ws.send(JSON.stringify(data));
print("SENT: " + JSON.stringify(data));
toggleLiveview(false);
return false;
}
const form = document.querySelector("form");
const data = {
command: "liveview:start",
data: {
account_region: form.querySelector("#account-region").value,
api_token: form.querySelector("#api-token").value,
account_id: form.querySelector("#account-id").value,
network_id: form.querySelector("#network-id").value,
camera_id: form.querySelector("#camera-id").value,
camera_type: form.querySelector("#camera-type").value,
},
};
localStorage.setItem("details", JSON.stringify(data));
ws.send(JSON.stringify(data));
toggleLiveview(true);
print("SENT: " + JSON.stringify(data));
};
document.getElementById("close").onclick = function (evt) {
if (!ws) {
return false;
}
ws.close();
return false;
};
window.onload = function (evt) {
const details = JSON.parse(localStorage.getItem("details"));
if (details) {
const form = document.querySelector("form");
form.querySelector("#account-region").value = details.data.account_region;
form.querySelector("#api-token").value = details.data.api_token;
form.querySelector("#account-id").value = details.data.account_id;
form.querySelector("#network-id").value = details.data.network_id;
form.querySelector("#camera-id").value = details.data.camera_id;
form.querySelector("#camera-type").value = details.data.camera_type;
}
};
</script>
</body>
</html>