-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
444 lines (397 loc) · 13.2 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/toastify-js/src/toastify.min.css">
<link rel=icon href='data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg" viewBox="0 0 100 100"><text y=".9em" font-size="90">🎥</text></svg>'>
<title>Filmbert</title>
<style>
html, body {
background-color: black;
}
.showonhover {
opacity: 0;
}
.showonhover:hover{
opacity: 1;
}
#video-container {
width: 100%
position: relative;
}
#video {
position: absolute;
top: 50%;
transform: translateY(-50%);
margin-left: -10px;
}
#video-controls:hover {
opacity: 1;
}
#video-controls {
position: fixed;
bottom: 0;
left: 0;
right: 0;
padding: 5px;
opacity: 0;
z-index: 10;
-webkit-transition: opacity .3s;
-moz-transition: opacity .3s;
-o-transition: opacity .3s;
-ms-transition: opacity .3s;
transition: opacity .3s;
background-image: linear-gradient(bottom, rgb(30,30,30) 13%, rgb(50,50,50) 100%);
background-image: -o-linear-gradient(bottom, rgb(30,30,30) 13%, rgb(50,50,50) 100%);
background-image: -moz-linear-gradient(bottom, rgb(30,30,30) 13%, rgb(50,50,50) 100%);
background-image: -webkit-linear-gradient(bottom, rgb(30,30,30) 13%, rgb(50,50,50) 100%);
background-image: -ms-linear-gradient(bottom, rgb(30,30,30) 13%, rgb(50,50,50) 100%);
background-image: -webkit-gradient(
linear,
left bottom,
left top,
color-stop(0.13, rgb(30,30,30)),
color-stop(1, rgb(50,50,50))
);
}
#video-container:hover #video-controls {
opacity: .9;
}
.ctrl-button {
position: relative;
top: -6px;
}
button {
background: rgba(0,0,0,.5);
border: 0;
color: #EEE;
-webkit-border-radius: 3px;
-moz-border-radius: 3px;
-o-border-radius: 3px;
border-radius: 3px;
}
button:hover {
cursor: pointer;
}
#seek-bar {
width: 100%;
}
#volume-bar {
width: 60px;
}
#time-display {
color: #aaa;
font-size: 10px;
font-family: monospace;
line-height: 10px;
}
.overlay {
position: fixed;
top: 0;
bottom: 0;
left: 0;
right: 0;
background-color: #333;
z-index: 100;
color: #ddd;
font-family: monospace;
text-align: center;
padding-top: 200px;
}
#unmute-indicator {
position: fixed;
bottom: 80px;
right: 10px;
background-color: #fff;
padding: 10px;
border-radius: 10px;
opacity: 0.8;
}
#unmute-indicator:after {
content: '';
position: absolute;
bottom: 0;
right: 150px;
width: 0;
height: 0;
border: 20px solid transparent;
border-top-color: #fff;
border-bottom: 0;
border-left: 0;
margin-left: -10px;
margin-bottom: -20px;
}
</style>
</head>
<body>
<div class="overlay loading-screen">
🎥 ⌛ Loading Filmbert...
</div>
<div class="overlay user-name-screen" style="display: none; opacity: 0.8;">
Please enter your name:<br>
<input type="text" id="userNameField" value="Dummbert"><br>
<button type="button" onclick="setUserName()">Ok</button>
</div>
<span style="display: none;">Your ID: <span id="ws-id"></span></span>
<div id="video-container">
<video autobuffer id="video" width="100%" muted> <!-- mute video to allow autoplay -->
<source src="[[VIDEO_URL]]" type="video/mp4" />
<track label="English" kind="subtitles" srclang="en" src="[[SUB_URL]]" default>
</video>
</div>
<!-- Video Controls -->
<div id="unmute-indicator">
Hover at the bottom of the screen to show the controls.<br>
<b>Click Unmute to hear something.</b>
</div>
<div id="video-controls">
<table width="100%">
<tr width="100%">
<td width="40">
<button type="button" id="play-pause" class="play ctrl-button">▶</button>
</td>
<td width="*">
<input type="range" id="seek-bar" value="0">
</td>
<td width="60">
<span id="time-display">0:00:00 / 0:00:00</span>
</td>
<td width="40">
<button type="button" id="mute" class="ctrl-button">Unmute</button>
</td>
<td width="40">
<input type="range" id="volume-bar" min="0" max="1" step="0.1" value="1">
</td>
<td width="40">
<button type="button" id="full-screen" class="ctrl-button">Full-Screen</button>
</td>
</tr>
</table>
</div>
<script>
let userName = '';
const setUserName = () => {
userName = document.getElementById('userNameField').value;
document.querySelector('.user-name-screen').style.display = 'none';
init();
}
var client_id = Date.now();
console.log(`local client id ${client_id}`);
let videoElem = null;
var ws = new WebSocket(`ws://${location.host}/ws/${client_id}`);
let initialStatusReceived = false;
const ACTION_PLAY = 'play';
const ACTION_SEEKTO = 'seekto';
const ACTION_PAUSE = 'pause';
const ACTION_REQUEST_STATUS = 'request_status';
const ACTION_SEND_STATUS = 'send_status';
const show = (selector) => {
document.querySelector(selector).style.display = 'block';
}
const hide = (selector) => {
document.querySelector(selector).style.display = 'none';
}
const prettyTime = (totalSeconds) => {
const hours = Math.floor((totalSeconds / 3600)) % 60;
const minutes = Math.floor((totalSeconds / 60)) % 60;
const seconds = Math.floor(totalSeconds) % 60;
return `${hours}:${minutes < 10 ? '0' + minutes : minutes}:${seconds < 10 ? '0' + seconds : seconds}`
}
const hideLoadingScreen = () => {
document.querySelector('.loading-screen').style.display = 'none';
document.querySelector('.user-name-screen').style.display = 'block';
}
const notifyAction = (userName, action) => {
Toastify({
text: `${userName} ${action}`,
duration: 3000,
gravity: "top", // `top` or `bottom`
position: 'left', // `left`, `center` or `right`
}).showToast();
}
ws.onmessage = function(event) {
message = JSON.parse(event.data);
console.log(event.data);
if (videoElem === null) {
alert('got message, but video player not initialized!');
return;
}
const action = message.data.action;
const userName = message.data.userName;
switch (action) {
case ACTION_PLAY:
notifyAction(userName, 'pressed play');
videoElem.currentTime = message.data.currentTime;
videoElem.play();
break;
case ACTION_SEEKTO:
notifyAction(userName, 'jumped to ' + prettyTime(message.data.currentTime));
const {pauseAfterSeek} = message.data;
videoElem.currentTime = message.data.currentTime;
if (pauseAfterSeek) {
videoElem.pause();
} else {
videoElem.play();
}
break;
case ACTION_PAUSE:
notifyAction(userName, 'pressed pause');
videoElem.pause();
break;
case ACTION_REQUEST_STATUS:
notifyAction(userName, 'joined');
// respond to status requests
if (initialStatusReceived) {
sendStatus(videoElem.currentTime, !videoElem.paused);
}
break;
case ACTION_SEND_STATUS:
if (!initialStatusReceived) {
initialStatusReceived = true;
videoElem.currentTime = message.data.currentTime
if (message.data.playing) {
videoElem.play();
}
}
break;
default:
console.error(`unknown message action ${action}`)
}
};
const preOpenMessageQueue = [];
const sendMsg = (action, data = {}) => {
let time = null;
try {
time = videoElem.currentTime;
} catch (e) {
// pass
}
const message = JSON.stringify({action, userName, currentTime: time, ...data});
if (ws.readyState === ws.OPEN) {
ws.send(message);
} else {
console.error(`Error sending message ${message} on websocket in state ${ws.readyState}, postponing sending to later`);
preOpenMessageQueue.push(message)
ws.onopen = () => {
for (const msg of preOpenMessageQueue) {
ws.send(msg);
}
}
}
}
const sendPause = () => sendMsg(ACTION_PAUSE);
const sendPlay = (position) => sendMsg(ACTION_PLAY, {currentTime: position});
const sendSeekTo = (position, pauseAfterSeek) => sendMsg(ACTION_SEEKTO, {currentTime: position, pauseAfterSeek});
const sendRequestStatus = () => sendMsg(ACTION_REQUEST_STATUS);
const sendStatus = (position, playing) => sendMsg(ACTION_SEND_STATUS, {position, playing});
let startedInit = false;
const init = () => {
if (!startedInit) {
startedInit = true;
// request play status from other members
sendRequestStatus();
window.setTimeout(() => {
// if nobody responds within a second, assume we're the only ones and mark
// this player as initialized
initialStatusReceived = true;
}, 1000);
}
}
window.onload = function() {
hideLoadingScreen();
videoElem = document.querySelector('#video');
// hook up video player controls
// we cannot use the internal controls, since we cannot hook up any click
// handlers to them, but we need them for the remote control
// Video
const video = document.getElementById("video");
// Buttons
const playButton = document.getElementById("play-pause");
const muteButton = document.getElementById("mute");
const muteButtonIndicator = document.getElementById("unmute-indicator");
const fullScreenButton = document.getElementById("full-screen");
// Sliders
const seekBar = document.getElementById("seek-bar");
const volumeBar = document.getElementById("volume-bar");
const timeDisplay = document.getElementById("time-display");
window.setInterval(() => {
if (video.paused === true) {
playButton.innerHTML = "▶";
} else {
playButton.innerHTML = "⏸";
}
}, 500);
const togglePlayPause = () => {
if (video.paused === true) {
sendPlay(video.currentTime);
} else {
sendPause();
}
}
// Event listener for the play/pause button
playButton.addEventListener("click", togglePlayPause);
video.addEventListener("click", togglePlayPause);
// Event listener for the mute button
muteButton.addEventListener("click", function() {
if (video.muted === false) {
// Mute the video
video.muted = true;
muteButtonIndicator.style.display = 'block';
// Update the button text
muteButton.innerHTML = "Unmute";
} else {
// Unmute the video
video.muted = false;
muteButtonIndicator.style.display = 'none';
// Update the button text
muteButton.innerHTML = "Mute";
}
});
// Event listener for the full-screen button
fullScreenButton.addEventListener("click", function() {
if (video.requestFullscreen) {
video.requestFullscreen();
} else if (video.mozRequestFullScreen) {
video.mozRequestFullScreen(); // Firefox
} else if (video.webkitRequestFullscreen) {
video.webkitRequestFullscreen(); // Chrome and Safari
}
});
// Event listener for the seek bar
seekBar.addEventListener("change", function() {
video.currentTime = video.duration * (seekBar.value / 100);
});
let wasPausedBeforeSeek = false;
let seeking = false;
// Update the seek bar as the video plays
video.addEventListener("timeupdate", function() {
if (!seeking) {
seekBar.value = (100 / video.duration) * video.currentTime;
timeDisplay.innerHTML = prettyTime(video.currentTime) + ' / ' + prettyTime(video.duration);
}
});
// Pause the video when the seek handle is being dragged
seekBar.addEventListener("mousedown", function() {
seeking = true;
wasPausedBeforeSeek = video.paused;
video.pause();
});
// Play the video when the seek handle is dropped
seekBar.addEventListener("mouseup", function() {
// if (!wasPausedBeforeSeek) {
// video.play();
// sendPlay();
// }
sendSeekTo(video.duration * (seekBar.value / 100), wasPausedBeforeSeek);
seeking = false;
});
// Event listener for the volume bar
volumeBar.addEventListener("change", function() {
// Update the video volume
video.volume = volumeBar.value;
});
}
</script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/toastify-js"></script>
</body>
</html>