Video Syncronization Prototyping Complete.

This commit is contained in:
rainbow napkin 2025-01-15 06:29:12 -05:00
parent 0b68db1265
commit 6dc9ad7b34
10 changed files with 286 additions and 20 deletions

View file

@ -23,22 +23,29 @@ module.exports = class{
this.server = server
//Set channel
this.channel = channel;
//Create variable to hold sync delta
//Create variable to hold sync delta in ms
this.syncDelta = 1000;
//Create variable to hold current timestamp within the video
this.timestamp = 0;
//Create variable to hold sync timer
this.syncTimer = null;
//Create variable to hold currently playing media object
this.nowPlaying = null;
//Create variable to hold current timestamp within the video
this.timestamp = 0;
}
queueMedia(inputMedia){
//Create new media object, start it now
//Create new media object, set start time to now
const mediaObj = queuedMedia.fromMedia(inputMedia, new Date().getTime());
//Start playback
this.play(mediaObj);
}
play(mediaObj){
//Silently end the media
this.end(true);
//reset current timestamp
this.timestamp = 0;
@ -46,7 +53,7 @@ module.exports = class{
this.nowPlaying = mediaObj;
//Send play signal out to the channel
this.server.io.in(this.channel.name).emit("play", {media: this.nowPlaying});
this.sendQueue();
//Kick off the sync timer
this.syncTimer = setTimeout(this.sync.bind(this), this.syncDelta);
@ -56,14 +63,58 @@ module.exports = class{
//Send sync signal out to the channel
this.server.io.in(this.channel.name).emit("sync", {timestamp: this.timestamp});
//If the media hasn't finished playing
if(this.timeStamp < this.nowPlaying.duration){
//If the media has over a second to go
if((this.timestamp + 1) < this.nowPlaying.duration){
//Increment the time stamp
this.timestamp++;
this.timestamp += (this.syncDelta / 1000);
//Call the sync function in another second
this.syncTimer = setTimeout(this.sync.bind(this), this.syncDelta);
}else{
//Get leftover video length in ms
const leftover = (this.nowPlaying.duration - this.timestamp) * 1000;
//Call the end function once the video is over
this.syncTimer = setTimeout(this.end.bind(this), leftover);
}
}
end(quiet = false){
//Call off any existing sync timer
clearTimeout(this.syncTimer);
//Clear out the sync timer
this.syncTimer = null;
//Clear now playing
this.nowPlaying = null;
//Clear timestamp
this.timestamp = 0;
//If we're not being quiet
if(!quiet){
//Tell everyone of the end-times
this.server.io.in(this.channel.name).emit('end', {});
}
}
sendQueue(socket){
//Create data object
const data = {
media: this.nowPlaying,
timestamp: this.timestamp
}
//If a socket is specified
if(socket != null){
//Send data out to specified socket
socket.emit("play", data);
//Otherwise
}else{
//Send that shit out to the entire channel
this.server.io.in(this.channel.name).emit("play", data);
}
}
}