-
Notifications
You must be signed in to change notification settings - Fork 0
/
control.html
538 lines (537 loc) · 24.4 KB
/
control.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<style>
html,body {
background-color:#000000;
height:100%;
width:100%;
padding:0;
margin:0;
color: #ffffff;
}
#videoProgress, #musicProgress {
width: 95%;
}
#leftEM, #rightEM{
background-color: black;
border: red 1px solid;
width: 50%;
position: fixed;
overflow: auto;
}
#leftEM {
height: 100%;
top: 0;
left: 0;
}
#rightEM {
top: 0;
right: 0;
height: 100%
}
button {
height: 25px;
background-color: black;
border: red 1px solid;
color: white;
}
#button {
margin: 2px;
}
button:not(:active) {
transition: background-color 10ms step-end;
}
button:active {
background: red;
}
#status {
float: right;
width: 33%;
right: 0;
top: 0;
border: red solid 1px;
}
br {
padding-top: 5px;
margin: 5px;
}
.controlSubDiv {
padding-bottom: 10px;
border: red 1px solid;
}
</style>
<title>drops-control v{{ .Version }}</title>
</head>
<body>
<div id="leftEM">
<div id="status">
<span id="connectionStatus"></span>
<span id="overlayStatus"></span>
</div>
<h1 style="margin: 0">Audio</h1>
<div id="audioControl" class="controlSubDiv">
<button onmousedown="sendControlMessage('audio', 'stop', 'type')">Stop</button>
<label for="audioVolume">Volume</label><input type="range" value=1.0 step=0.01 max=1 min=0 id="audioVolume" onchange="setVolume(document.getElementById('audioVolume').value, 'audioVolume')">
<br>
<label for="playbackRate">PlaybackRate</label><input type="range" value=1.0 step=0.1 max=2.0 min=0.4 id="playbackRate" onchange="let pvr = document.getElementById('playbackRate').value;setVolume(pvr, 'playbackRate');document.getElementById('playbackRateValue').innerText = pvr"> <span id="playbackRateValue">1.0</span> <button onmousedown="setVolume('1', 'playbackRate');document.getElementById('playbackRate').value = 1;document.getElementById('playbackRateValue').innerText = '1'">[1.0]</button>
<br>
<label for="echoChecked">Echo<input type="checkbox" id="echoChecked"></label>
<label for="echoDelay">Delay<input min="35" max="250" value="50" step="1" type="range" id="echoDelay" onchange="let edv = document.getElementById('echoDelay').value;setEchoDelay(edv);document.getElementById('echoDelayValue').innerText = edv"></label>
<span id="echoDelayValue"></span>
</div>
<div id="dropsAudio"></div>
<h1 style="margin: 0">VideoLoop</h1>
<div id="videoLoopControls" class="controlSubDiv">
<button onmousedown="videoLoopControl('start')">Start</button>
<button onmousedown="videoLoopControl('stop')">Stop</button>
<button style="float: right" onmousedown="videoLoopNext = true">Next</button>
</div>
<div id="videoLoopTitles"></div>
</div>
<div id="rightEM">
<div style="float: right">
<button onclick="testOverlay();">TestFrame</button>
<button onclick="openUpload();">Upload</button>
<button onclick="load();">Reload</button>
</div>
<h1 style="margin: 0">Video</h1>
<div id="videoControl" class="controlSubDiv">
<label for="vid">videoFile/Url:</label>
<input value="" type="text" id="vid">
<br>
<button onmousedown="sendControlMessage('video', 'stop', '');">Stop</button>
<button onmousedown="sendControlMessage('video', 'play', document.getElementById('vid').value)">Send</button>
<button onmousedown="sendControlMessage('video', 'pr', '');">Pause/Resume</button>
<span id="videoPlayerState" style="color: green">Ready</span>
<br>
<label for="videoPlaybackRate">PlaybackRate</label>
<input type="range" value=1.0 step=0.2 max=2 min=0.2 id="videoPlaybackRate" onchange="let vprv = document.getElementById('videoPlaybackRate').value;setVolume(vprv, 'videoPlaybackRate');document.getElementById('videoPlaybackRateValue').innerText = vprv;">
<span id="videoPlaybackRateValue">1</span>
<button onmousedown="setVolume('1', 'videoPlaybackRate');document.getElementById('videoPlaybackRate').value = 1;document.getElementById('videoPlaybackRateValue').innerText = '1'">[1.0]</button>
<br>
<label for="videoVolume">Volume</label>
<input type="range" value=1.0 step=0.1 max=1 min=0 id="videoVolume" onchange="setVolume(document.getElementById('videoVolume').value, 'videoVolume')">
<br>
<label for="videoRepeatEm">Repeat</label>
<input type="checkbox" id="videoRepeatEm" onchange="setVideoRepeat(document.getElementById('videoRepeatEm'))">
<br>
<label for="videoProgress">Progress<span id="videoProgressIntEm"></span></label><input type="range" min=0 step=0.1 max=10 onmouseover="videoProgressInteraction = true" onmouseout="videoProgressInteraction = false" id="videoProgress" onclick="sendControlMessage('video', 'seek', document.getElementById('videoProgress').value);">
</div>
<div id="dropsVideo"></div>
<h1 style="margin: 0">Music</h1>
<div id="musicControl" class="controlSubDiv">
<button onmousedown="sendControlMessage('music', 'stop', '');clearInterval(musicInterval);musicInterval = null">Stop</button>
<label for="musicVolume">Volume</label><input type="range" value=0.2 step=0.1 max=1 min=0 id="musicVolume" onchange="setVolume(document.getElementById('musicVolume').value, 'musicVolume')">
<button style="float: right" onmousedown="continuousPlay(0)">loop all</button>
<br>
<span id="musicCurrentFilename">No File playing</span>
<button style="float: right" onmousedown="next = true;">next</button>
<br>
<label style="float: right" for="musicAnnounce">Announce:</label><input style="float: right" type="checkbox" id="musicAnnounce">
<label for="musicProgress">Progress:
<span id="musicProgressIntEm"></span>
</label>
<input onmouseover="musicProgressInteraction = true" onmouseout="musicProgressInteraction = false" type="range" min=0 step=0.01 max=10 id="musicProgress" onclick="sendControlMessage('music', 'seek', document.getElementById('musicProgress').value);">
</div>
<div id="music"></div>
</div>
<script>
let testOverlay = () => {
if (!document.getElementById('test')) {
console.log("opening test frame");
document.body.insertAdjacentHTML('beforeend', `<div id="test" style="position: fixed ;z-index: max()-1; bottom: 0; left: 0; width: 25%;height: 25%; border: red solid 1px; background-color: black;color: white"><iframe style="z-index: max();height: 100%; width: 100%" src="/overlay"></iframe><button onmousedown="document.getElementById('test').remove()" style="position: fixed ;z-index: max(); bottom: 0; left: 0; border: green solid 1px; background-color: black;color: white;">close</button></div>`);
let testDiv = document.getElementById('test');
let initResize = () => {
window.addEventListener('mousemove', Resize, false);
window.addEventListener('mouseup', stopResize, false);
}
let Resize = (e) => {
testDiv.style.width = (e.clientX + testDiv.offsetLeft) + 'px';
let nh = (window.innerHeight - e.clientY) + 'px';
console.log("resize Height: " + nh)
testDiv.style.height = nh;
}
let stopResize = () => {
window.removeEventListener('mousemove', Resize, false);
window.removeEventListener('mouseup', stopResize, false);
}
let resizer = document.createElement('div');
resizer.className = 'resizer';
resizer.style.width = '10px';
resizer.style.height = '10px';
resizer.style.background = 'red';
resizer.style.position = 'absolute';
resizer.style.right = '0';
resizer.style.top = '0';
resizer.style.cursor = "ne-resize";
testDiv.appendChild(resizer);
resizer.addEventListener('mousedown', initResize, false);
} else {
console.log("testing frame already open");
}
};
console.log("Version: {{ .Version }}");
let ws;
let host = location.host;
let overlayStatus = false;
let connectionStatus = false;
let latestOverlayConnectionTime;
let listUrl = `http://${host}/list?type=`;
let wsAddress = `ws://${host}/controlWS`;
let connectionStatusEm = document.getElementById("connectionStatus");
let overlayStatusEm = document.getElementById("overlayStatus");
let videoProgressIntEm = document.getElementById("videoProgressIntEm");
let videoProgress = document.getElementById("videoProgress");
let videoProgressInteraction = false;
let videoPlayerState = document.getElementById("videoPlayerState");
let videoDropEm = document.getElementById("dropsVideo");
let videoProgressDuration = 0;
let videoProgressCurrent = 0;
let videoLoopNext = false;
let videoLoopTitlesEm = document.getElementById("videoLoopTitles");
let videoLoopTitles = [];
let musicCurrentFilename = document.getElementById("musicCurrentFilename");
let musicProgressIntEm = document.getElementById("musicProgressIntEm");
let musicProgress = document.getElementById("musicProgress");
let musicAnnounce = document.getElementById("musicAnnounce");
let musicProgressInteraction;
let musicEm = document.getElementById("music");
let musicProgressDuration;
let musicProgressCurrent;
let musicInterval = null;
let audioDropEm = document.getElementById("dropsAudio");
let audioFiles = [];
let next = false;
let openUpload = () => {
document.getElementById("rightEM").insertAdjacentHTML("beforeend",`<div id="uploadDiv" style="position: fixed;right: 0;top: 0;background-color: black;width: 50%;height: 85%">
<button onmousedown="document.getElementById('uploadDiv').remove()" style="float: right;position: fixed">close</button>
<iframe style="height: 100%;width: 100%" src="/uploadAsset"></iframe>
</div>`)
};
let timeFix = (seconds) => {
const date = new Date(0);
date.setSeconds(seconds);
let rn;
try {
rn = date.toISOString().substring(19, 11);
} catch (e) {
console.log(`error: Failed to parse seconds ${seconds}: ${e}`);
return;
}
return rn;
};
let continuousPlay = (index) => {
let position = index;
next = false;
musicCurrentFilename.innerText = `${audioFiles[position]}`;
sendControlMessage('music', 'play', `music/${audioFiles[position]}`);
if (musicAnnounce.checked) {
sendControlMessage('music', 'announce', `${audioFiles[position].substring(0, audioFiles[position].lastIndexOf('.'))}`);
}
musicInterval = setInterval(() => {
if (musicProgressCurrent+2 > musicProgressDuration || next) {
next = false;
clearInterval(musicInterval);
musicInterval = null;
position++;
if (position > audioFiles.length-1) {
position = 0;
}
continuousPlay(position);
}
}, 1000);
};
let processMessage = (msg) => {
let pj;
try {
pj = JSON.parse(msg.data);
}
catch (e) {
console.error("Failed to parse json", e);
return;
}
let action = pj["a"];
let key = pj["k"];
let value = pj["v"];
switch (action) {
case "status":
latestOverlayConnectionTime = Date.now();
overlayStatus = true;
setStatus();
break;
case "video":
switch (key) {
case "progress":
if (!videoProgressInteraction) {
videoProgress.value = value;
}
videoProgressCurrent = value;
break;
case "duration":
videoProgress.max = value;
videoProgressDuration = value;
break;
case "state":
videoPlayerState.innerText = value;
console.log(`videoPlayerState: ${value}`);
switch (value) {
case "playing":
videoPlayerState.style.color = "green";
break;
case "ready":
videoProgress.max = 1;
videoProgress.value = 0;
videoPlayerState.style.color = "green";
videoProgressCurrent = 0;
videoProgressDuration = 0;
break;
case "paused":
videoPlayerState.style.color = "red";
break;
}
}
videoProgressIntEm.innerText = `${timeFix(videoProgressCurrent)}/${timeFix(videoProgressDuration)}`;
break;
case "music":
switch (key) {
case "progress":
if (!musicProgressInteraction) {
musicProgress.value = value;
}
musicProgressCurrent = value;
break;
case "duration":
musicProgress.max = value;
musicProgressDuration = value;
break;
}
musicProgressIntEm.innerText = `${timeFix(musicProgressCurrent)}/${timeFix(musicProgressDuration)}`;
break;
case "error":
console.log(`ERROR:: action = ${action} : key = ${key} : value = : ${value}`);
alert("overlay returned an error, check console output");
}
};
let connect = () => {
console.log(`connecting to ${wsAddress}`);
ws = new WebSocket(wsAddress);
ws.onopen = function() {
connectionStatus = true;
setStatus();
console.log(`connected to ${wsAddress}`);
};
ws.onmessage = function(event) {processMessage(event)};
ws.onclose = function() {
connectionStatus = false;
setStatus();
console.log('Socket is closed. Reconnect will be attempted in 5 second(s).');
ws = null;
};
ws.onerror = function(err) {
console.error('Socket encountered error: ', err.message, 'Closing socket');
ws.close();
};
};
let sendControlMessage = (action, key, value) => {
ws.send(JSON.stringify({a: action, k: key, v: value}));
};
let setEchoDelay = (delay) => {
sendControlMessage("audio", "echoDelay", delay);
}
let setStatus = () => {
try {
if (connectionStatus) {
connectionStatusEm.innerText = "Server";
connectionStatusEm.style.color = "green";
} else {
connectionStatusEm.innerText = "Server";
connectionStatusEm.style.color = "red";
}
if (overlayStatus) {
overlayStatusEm.innerText = "Overlay";
overlayStatusEm.style.color = "green";
} else {
overlayStatusEm.innerText = "Overlay";
overlayStatusEm.style.color = "red";
}
}
catch (e) {
console.log("failed to get element");
}
};
let echoChecked = document.getElementById("echoChecked");
let sendAudio = (url) => {
if (echoChecked.checked) {
sendControlMessage("audio", "echo", url);
} else {
sendControlMessage("audio", "play", url);
}
};
let setVolume = (level, type) => {
sendControlMessage("volume", type, level);
};
let addDrop = (filename) => {
let filenameExt = filename.substring(filename.length - 4);
switch (filenameExt) {
case ".mp3":
audioDropEm.insertAdjacentHTML("beforeend", `<button id="button" onmousedown="sendAudio('drops/${filename}')">${filename.substring(0, filename.lastIndexOf('.'))}</button>`);
break;
case ".mp4":
videoDropEm.insertAdjacentHTML("beforeend", `<button id="button" onmousedown="sendControlMessage('video', 'play', 'drops/${filename}')">${filename.substring(0, filename.lastIndexOf('.'))}</button>`);
break;
case ".mkv":
videoDropEm.insertAdjacentHTML("beforeend", `<button id="button" onmousedown="sendControlMessage('video', 'play', 'drops/${filename}')">${filename.substring(0, filename.lastIndexOf('.'))}</button>`);
break;
}
};
let loadDrops = () => {
fetch(`${listUrl}drops`).then(response => {
return response.json();
}).then(data => {
data["files"].forEach((value) => {
addDrop(value);
})
}).catch(err => {
console.log("error", err);
});
};
let addMusic = (filename, n) => {
let filenameExt = filename.substring(filename.length - 4);
if (filenameExt === ".mp3") {
audioFiles.push(filename);
musicEm.insertAdjacentHTML("beforeend", `<button id="button" onmousedown="if (musicInterval !== null ) {
clearInterval(musicInterval);
musicInterval = null;
continuousPlay(${n});
} else {
sendControlMessage('music', 'play', 'music/${filename}');
musicCurrentFilename.innerText = '${filename}';
}
if (musicAnnounce.checked) {
sendControlMessage('music', 'announce', '${filename.substring(0, filename.lastIndexOf("."))}');
}">${filename.substring(0, filename.lastIndexOf("."))}</button>`);
}
};
let loadMusic = () => {
audioFiles = [];
fetch(`${listUrl}music`).then(response => {
return response.json();
}).then(data => {
let df = data["files"];
for (let i = 0; i < df.length; i++) {
addMusic(df[i], i);
}
}).catch(err => {
console.log("error", err);
});
};
let videoLoopInterval = null;
let videoLoopPosition = 0;
let playingColor = (filename) => {
const div = document.querySelectorAll('#videoLoopTitles *');
div.forEach(child => {
child.style.color = "white";
});
let rmExt = filename.substring(0, filename.lastIndexOf('.'));
let ce = document.getElementById(rmExt)
ce.style.color = "green";
}
let videoRepeat = false;
let setVideoRepeat = (em) => {
if (em.checked) {
videoRepeat = true;
ws.send(JSON.stringify({a: "video", k: "loop", v: "true"}));
} else {
videoRepeat = false;
ws.send(JSON.stringify({a: "video", k: "loop", v: "false"}));
}
}
let loopVideoSwitch = false;
let playLoopVideo = (index) => {
if (loopVideoSwitch) {
sendControlMessage('video', 'play', `videoLoop/${videoLoopTitles[index]}`)
playingColor(videoLoopTitles[index]);
setTimeout(() => { // wait for video to begin playing before starting loop interval
videoLoopInterval = setInterval(() => {
if ( videoProgressCurrent > 1 && (videoProgressDuration-videoProgressCurrent < 0.2 || videoLoopNext)) {
clearInterval(videoLoopInterval);
videoLoopNext = false;
if (index===videoLoopTitles.length-1 ) {
playLoopVideo(0);
} else {
playLoopVideo(index + 1);
}
}
}, 100)
},1000);
}
}
let videoLoopControl = (action) => {
switch (action) {
case "start":
loopVideoSwitch = true;
videoLoopPosition = 0;
playLoopVideo(0);
break;
case "stop":
loopVideoSwitch = false;
videoLoopPosition = 0;
break;
}
}
let addLoopVideo = (filename) => {
let filenameExt = filename.substring(filename.length - 4);
if (filenameExt === ".mkv" || filenameExt === ".mp4") {
videoLoopTitles.push(filename);
let rmExt = filename.substring(0, filename.lastIndexOf('.'));
videoLoopTitlesEm.insertAdjacentHTML("beforeend", `<button style="margin: 2px" id="${rmExt}">${rmExt}</button>`);
}
};
let loadLoopVideoTitles = () => {
videoLoopTitles = [];
fetch(`${listUrl}videoLoop`).then(response => {
return response.json();
}).then(data => {
data["files"].forEach((value) => {
addLoopVideo(value);
})
}).catch(err => {
console.log("error", err);
});
};
let checkConnectionStatus = () => {
let endTime = Date.now();
let elapsedTime = endTime - latestOverlayConnectionTime;
if (elapsedTime > 2000) {
console.log("More than 2000ms elapsed without overlay contact, assuming disconnected");
overlayStatus = false;
setStatus();
}
};
let load = () => {
musicEm.innerHTML = "";
audioDropEm.innerHTML = "";
videoDropEm.innerHTML = "";
videoLoopTitlesEm.innerHTML = "";
loadDrops();
loadMusic();
loadLoopVideoTitles();
};
window.addEventListener('load', () => {
console.log('All assets are loaded');
connect();
load();
setInterval(() => {
checkConnectionStatus();
if (!connectionStatus) {
console.log("Socket is closed, connecting...");
connect();
}
}, 3000);
})
</script>
</body>
</html>