Started work on back-end for synced video playback.

This commit is contained in:
rainbow napkin 2025-01-12 17:50:54 -05:00
parent b443840c29
commit 0b68db1265
6 changed files with 131 additions and 10 deletions

View file

@ -0,0 +1,69 @@
/*Canopy - The next generation of stoner streaming software
Copyright (C) 2024-2025 Rainbownapkin and the TTN Community
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.*/
//Local imports
const queuedMedia = require('./queuedMedia');
module.exports = class{
constructor(server, channel){
//Set server
this.server = server
//Set channel
this.channel = channel;
//Create variable to hold sync delta
this.syncDelta = 1000;
//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
const mediaObj = queuedMedia.fromMedia(inputMedia, new Date().getTime());
}
play(mediaObj){
//reset current timestamp
this.timestamp = 0;
//Set current playing media
this.nowPlaying = mediaObj;
//Send play signal out to the channel
this.server.io.in(this.channel.name).emit("play", {media: this.nowPlaying});
//Kick off the sync timer
this.syncTimer = setTimeout(this.sync.bind(this), this.syncDelta);
}
sync(){
//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){
//Increment the time stamp
this.timestamp++;
//Call the sync function in another second
this.syncTimer = setTimeout(this.sync.bind(this), this.syncDelta);
}
}
}