/
sync.js
582 lines (526 loc) · 18.5 KB
/
sync.js
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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
// Calls the play video function on the server
function playVideo(roomnum) {
// dailyPlayer.play();
//vimeoPlayer.play()
socket.emit('play video', {
room: roomnum
});
// Doesn't work well unless called in server
//io.sockets.in("room-"+roomnum).emit('playVideoClient');
}
// Calls the sync function on the server
function syncVideo(roomnum) {
var currTime = 0
var state
var videoId = id
// var syncText = document.getElementById("syncbutton")
// console.log(syncText.innerHTML)
// syncText.innerHTML = "<i class=\"fas fa-sync fa-spin\"></i> Sync"
switch (currPlayer) {
case 0:
currTime = player.getCurrentTime();
state = playerStatus
console.log("I am host and my current time is " + currTime + state)
break;
case 1:
currTime = dailyPlayer.currentTime;
state = dailyPlayer.paused;
break;
case 2:
vimeoPlayer.getCurrentTime().then(function(seconds) {
// seconds = the current playback position
currTime = seconds
// Need to nest async functions
vimeoPlayer.getPaused().then(function(paused) {
// paused = whether or not the player is paused
state = paused
console.log("state=" + state)
socket.emit('sync video', {
room: roomnum,
time: currTime,
state: state,
videoId: videoId
});
}).catch(function(error) {
// an error occurred
console.log("Error: Could not retrieve Vimeo Player state")
});
}).catch(function(error) {
// an error occurred
console.log("Error: Could not retrieve Vimeo player current time")
});
break;
case 3:
currTime = media.currentTime;
state = media.paused;
break;
default:
console.log("Error invalid player id")
}
// Required due to vimeo asyncronous functionality
if (currPlayer != 2) {
socket.emit('sync video', {
room: roomnum,
time: currTime,
state: state,
videoId: videoId
});
}
}
// This return the current time
function getTime() {
switch (currPlayer) {
case 0:
return player.getCurrentTime();
break;
case 1:
return dailyPlayer.currentTime;
break;
case 2:
vimeoPlayer.getCurrentTime().then(function(seconds) {
// seconds = the current playback position
return seconds
}).catch(function(error) {
// an error occurred
console.log("Error: Could not retrieve Vimeo player current time")
return null
});
break;
case 3:
return media.currentTime;
break;
default:
console.log("Error invalid player id")
}
}
function seekTo(time) {
switch (currPlayer) {
case 0:
player.seekTo(time)
player.playVideo()
break;
case 1:
dailyPlayer.seek(currTime);
dailyPlayer.play();
break;
case 2:
vimeoPlayer.setCurrentTime(currTime).then(function(seconds) {
// seconds = the actual time that the player seeked to
}).catch(function(error) {
switch (error.name) {
case 'RangeError':
// the time was less than 0 or greater than the video’s duration
console.log("the time was less than 0 or greater than the video’s duration")
break;
default:
// some other error occurred
break;
}
});
break;
case 3:
media.currentTime = currTime
media.play()
break;
}
}
// This parses the ID out of the video link
function idParse(videoId) {
// If user enters a full link
if (videoId.includes("https://") || videoId.includes("http://") || videoId.includes(".com/")) {
// Do some string processing with regex
switch (currPlayer) {
case 0:
var myRegex = /.+watch\?v=([A-Za-z0-9\-_]+)/g
var match = myRegex.exec(videoId)
if (match != null) {
console.log("You entered a link, but you really meant " + match[1])
return match[1]
}
videoId = "invalid"
break;
case 1:
var myRegex = /.+\/(.+)/g
if (videoId.includes("playlist")) {
myRegex = /.+video=(.+)/g
}
var match = myRegex.exec(videoId)
if (match != null) {
console.log("You entered a link, but you really meant " + match[1])
return match[1]
}
videoId = "invalid"
break;
case 2:
var myRegex = /.+\/(.+)/g
var match = myRegex.exec(videoId)
if (match != null) {
console.log("You entered a link, but you really meant " + match[1])
return match[1]
}
videoId = "invalid"
break;
case 3:
return videoId
break;
default:
console.log("Error invalid videoId")
}
}
return videoId
}
// QueueVideo
function enqueueVideo(roomnum) {
//var videoId = 'sjk7DiH0JhQ';
var videoId = document.getElementById("inputVideoId").value;
videoId = idParse(videoId)
if (videoId != "invalid") {
// Actually change the video!
socket.emit('enqueue video', {
room: roomnum,
videoId: videoId,
user: username
})
} else {
console.log("User entered an invalid video url :(")
invalidURL()
}
}
// Empty Queue
function emptyQueue(roomnum) {
// Empty the queue
socket.emit('empty queue', {
room: roomnum
});
// Notify
socket.emit('notify alerts', {
alert: 2,
user: username
})
}
// Change playVideo
function changeVideo(roomnum) {
//var videoId = 'sjk7DiH0JhQ';
var videoId = document.getElementById("inputVideoId").value;
videoId = idParse(videoId)
if (videoId != "invalid") {
var time = getTime()
console.log("The time is this man: " + time)
// Actually change the video!
socket.emit('change video', {
room: roomnum,
videoId: videoId,
time: time
});
} else {
console.log("User entered an invalid video url :(")
invalidURL()
}
//player.loadVideoById(videoId);
}
// Does this even work?
function changeVideoId(roomnum, id) {
//var videoId = 'sjk7DiH0JhQ';
document.getElementById("inputVideoId").innerHTML = id;
socket.emit('change video', {
room: roomnum,
videoId: id
});
//player.loadVideoById(videoId);
}
// Change to previous video
function prevVideo(roomnum) {
// This gets the previous video
socket.emit('change previous video', {
room: roomnum
}, function(data) {
// Actually change the video!
var prevTime = data.time
var time = getTime()
socket.emit('change video', {
room: roomnum,
videoId: data.videoId,
time: time,
prev: true
}, function(data) {
// Set to the previous time
setTimeout(function() {
seekTo(prevTime)
}, 1200);
});
});
}
function loveLive(roomnum) {
var time = getTime()
// love live, love4eva, why, gee, what is love, stay, starlight, bad boy
// likey, spring love, palette, roller coaster, DNA, I, peekaboo, wee woo
// rookie, russian roulette, i want you back, TT, whistle, ddu du ddu du, turtle, 24/7
// something new, #cookie jar, lion heart, i will show you, bubble pop, girl front, love cherry motion, ice cream cake
// stay (taeyeon), ordinary love, 11:11, SObeR, I'm so sick, heaven, genie, dinosaur
// Travel
var video_roulette = [
'97uviVyw0_o', 'tIWpr3tHzII', 'WkdtmT8A2iY', 'U7mPqycQ0tQ',
'i0p1bmr0EmE', 'FzVR_fymZw4', 'eNmL4JiGxZQ', 'J_CFBjAyPWE',
'V2hlQkVJZhE', 'erErBFKPbMY', 'd9IxdwEFk1c', '900X9fDFLc4',
'MBdVXkSdhwU', '4OrCA1OInoo', '6uJf2IT2Zh8', 'wLfHuClrQdI',
'J0h8-OTC38I', 'QslJYDX3o8s', 'X3H-4crGD6k', 'ePpPVE-GGJw',
'dISNgvVpWlo', 'IHNzOHi8sJs', 'sErJjrLswEw', '-j6XPEUKzn0',
'im1UUY8dQIk', 'rRgTMs_bGuI', 'nVCubhQ454c', 'MCEcWcIww5k',
'bw9CALKOvAI', 'tyInv6RWL0Q', 'VBbeuXW8Nko', 'glXgSSOKlls',
'k9_XH1YibcY', 'xGav-z5yRiU', 'WLJyhhNCHi0', 'DgT4CPv_CCE',
'F4oHuML9U2A', 'L9ro1KjkJMg', '6SwiSpudKWI', '8Oz7DG76ibY',
'xRbPAVnqtcs'
]
// Random number between 0 and 40 inclusive
var random = Math.floor(Math.random() * (41))
// Only for YouTube testing
socket.emit('change video', {
room: roomnum,
videoId: video_roulette[random],
time: time
})
}
// Get time - DEPRECATED
// socket.on('getTime', function(data) {
// var caller = data.caller
// var time = player.getCurrentTime()
// console.log("Syncing new socket to time: " + time)
// socket.emit('change time', {
// time: time,
// id: caller
// });
// });
// This just calls the sync host function in the server
socket.on('getData', function(data) {
console.log("Hi im the host, you called?")
socket.emit('sync host', {});
//socket.emit('change video', { time: time });
});
function changePlayer(roomnum, playerId) {
if (playerId != currPlayer) {
console.log("I changed!")
socket.emit('change player', {
room: roomnum,
playerId: playerId
});
}
}
// Change a single player
function changeSinglePlayer(playerId) {
return new Promise((resolve, reject) => {
if (playerId != currPlayer) {
console.log("I changed!")
socket.emit('change single player', {
playerId: playerId
});
}
resolve("socket entered change single player function")
})
}
//------------------------------//
// Client Synchronization Stuff //
//------------------------------//
var socket = io.connect();
var roomnum = 1
var id = "M7lc1UVf-VE"
// Calls the play/pause function
socket.on('playVideoClient', function(data) {
// Calls the proper play function for the player
switch (currPlayer) {
case 0:
play()
break;
case 1:
dailyPlay()
break;
case 2:
vimeoPlay()
break;
case 3:
html5Play()
break;
default:
console.log("Error invalid player id")
}
});
socket.on('pauseVideoClient', function(data) {
switch (currPlayer) {
case 0:
player.pauseVideo();
break;
case 1:
dailyPlayer.pause();
break;
case 2:
vimeoPlayer.pause();
break;
case 3:
media.pause()
break;
default:
console.log("Error invalid player id")
}
});
// Syncs the video client
socket.on('syncVideoClient', function(data) {
var currTime = data.time
var state = data.state
var videoId = data.videoId
var playerId = data.playerId
console.log("current time is: " + currTime)
console.log("curr vid id: " + id + " " + videoId)
console.log("state" + state)
// There should no longer be any need to sync a video change
// Video should always be the same
// if (id != videoId){
// console.log(id == videoId)
// changeVideoId(roomnum, videoId)
// }
// This switchs you to the correct player
// Should only happen when a new socket joins late
// Current issue: changePlayer is called asynchronously when we need this function to wait for it to finish
// changeSinglePlayer(playerId)
// currPlayer = playerId
// Change the player if necessary
if (currPlayer != playerId) {
// This changes the player then recalls sync afterwards on the host
changeSinglePlayer(playerId)
} else {
// This syncs the time and state
switch (currPlayer) {
case 0:
var clientTime = player.getCurrentTime();
// Only seek if off by more than .1 seconds
// CURRENTLY ALL SET TO TRUE TO TO SYNCING ISSUES
if (true || clientTime < currTime - .1 || clientTime > currTime + .1) {
player.seekTo(currTime);
}
// Sync player state
// IF parent player was paused
// If state is -1 (unstarted) the video will still start as intended
if (state == 2) {
console.log("paused?")
player.pauseVideo();
}
// If not paused
else {
player.playVideo();
}
break;
case 1:
var clientTime = dailyPlayer.currentTime;
// Only seek if off by more than .1 seconds
if (true || clientTime < currTime - .1 || clientTime > currTime + .1) {
dailyPlayer.seek(currTime);
}
if (state) {
console.log("i pausing!")
dailyPlayer.pause()
} else {
dailyPlayer.play()
}
break;
case 2:
vimeoPlayer.getCurrentTime().then(function(seconds) {
// seconds = the current playback position
if (true || seconds < currTime - .1 || seconds > currTime + .1) {
vimeoPlayer.setCurrentTime(currTime).then(function(seconds) {
if (state) {
vimeoPlayer.pause()
} else {
vimeoPlayer.play()
}
}).catch(function(error) {
switch (error.name) {
case 'RangeError':
// the time was less than 0 or greater than the video’s duration
console.log("the time was less than 0 or greater than the video’s duration")
break;
default:
// some other error occurred
break;
}
});
}
}).catch(function(error) {
// an error occurred
console.log("Error: Could not retrieve Vimeo player current time")
});
break;
case 3:
media.currentTime = currTime
// Sync player state
// IF parent player was paused
if (state) {
media.pause()
} else {
media.play()
}
break;
default:
console.log("Error invalid player id")
}
}
});
// Change video
socket.on('changeVideoClient', function(data) {
var videoId = data.videoId;
console.log("video id is: " + videoId)
// Pause right before changing
// pauseOther(roomnum)
// This is getting the video id from the server
// The original change video call updates the value for the room
// This probably is more inefficient than just passing in the parameter but is safer?
socket.emit('get video', function(id) {
console.log("it really is " + id)
videoId = id
// This changes the video
id = videoId
switch (currPlayer) {
case 0:
player.loadVideoById(videoId);
break;
case 1:
dailyPlayer.load(videoId, {
autoplay: true
});
break;
case 2:
vimeoPlayer.loadVideo(videoId).then(function(id) {
// the video successfully loaded
}).catch(function(error) {
switch (error.name) {
case 'TypeError':
// the id was not a number
break;
case 'PasswordError':
// the video is password-protected and the viewer needs to enter the
// password first
break;
case 'PrivacyError':
// the video is password-protected or private
break;
default:
// some other error occurred
break;
}
});
break;
case 3:
htmlLoadVideo(videoId)
break;
default:
console.log("Error invalid player id")
}
})
// Auto sync with host after 1000ms of changing video
setTimeout(function() {
console.log("resyncing with host after video change")
socket.emit('sync host', {});
}, 1000);
});
// Change time
socket.on('changeTime', function(data) {
var time = data.time
player.seekTo(time);
});