Started work on back-end for synced video playback.
This commit is contained in:
parent
b443840c29
commit
0b68db1265
|
|
@ -16,6 +16,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.*/
|
|||
|
||||
//local imports
|
||||
const connectedUser = require('./connectedUser');
|
||||
const queue = require('./media/queue');
|
||||
const flairModel = require('../../schemas/flairSchema');
|
||||
const permissionModel = require('../../schemas/permissionSchema');
|
||||
|
||||
|
|
@ -26,6 +27,7 @@ module.exports = class{
|
|||
this.tokeCommands = chanDB.tokeCommands;
|
||||
//Keeping these in a map was originally a vestige but it's more preformant than an array or object so :P
|
||||
this.userList = new Map();
|
||||
this.queue = new queue(server, this);
|
||||
}
|
||||
|
||||
async handleConnection(userDB, chanDB, socket){
|
||||
|
|
@ -52,6 +54,7 @@ module.exports = class{
|
|||
//if everything looks good, admit the connection to the channel
|
||||
socket.join(socket.chan);
|
||||
|
||||
//Hand off the connection initiation to it's user object
|
||||
await userObj.handleConnection(userDB, chanDB, socket)
|
||||
|
||||
//Send out the userlist
|
||||
|
|
|
|||
|
|
@ -70,12 +70,12 @@ module.exports = class{
|
|||
return;
|
||||
}
|
||||
|
||||
//Define listeners
|
||||
//Define listeners for inter-channel classes
|
||||
this.defineListeners(socket);
|
||||
this.chatHandler.defineListeners(socket);
|
||||
this.mediaYanker.defineListeners(socket);
|
||||
|
||||
//Connect the socket to it's given channel
|
||||
//Hand off the connection to it's given active channel object
|
||||
//Lil' hacky to pass chanDB like that, but why double up on DB calls?
|
||||
activeChan.handleConnection(userDB, chanDB, socket);
|
||||
}else{
|
||||
|
|
|
|||
|
|
@ -24,13 +24,5 @@ module.exports = class{
|
|||
this.id = id;
|
||||
this.type = type;
|
||||
this.duration = duration;
|
||||
|
||||
//Generate initial UUID, this will most likely be replaced when media object is added to a playlist
|
||||
//If this ends up being entirely redudnant in the future we'll remove it for performance
|
||||
this.genUUID();
|
||||
}
|
||||
|
||||
genUUID(){
|
||||
this.uuid = crypto.randomUUID();
|
||||
}
|
||||
}
|
||||
69
src/app/channel/media/queue.js
Normal file
69
src/app/channel/media/queue.js
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
43
src/app/channel/media/queuedMedia.js
Normal file
43
src/app/channel/media/queuedMedia.js
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
/*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 media = require('./media');
|
||||
|
||||
module.exports = class extends media{
|
||||
constructor(title, fileName, id, type, duration, startTime){
|
||||
//Call derived constructor
|
||||
super(title, fileName, id, type, duration);
|
||||
//Set media start time
|
||||
this.startTime = startTime;
|
||||
|
||||
//Generate id unique to this specific entry of this specific file within this specific channel's queue
|
||||
//That way even if we have six copies of the same video queued, we can still uniquely idenitify each instance
|
||||
this.genUUID();
|
||||
}
|
||||
|
||||
//statics
|
||||
static fromMedia(media, startTime){
|
||||
//Create and return queuedMedia object from given media object and arguments
|
||||
return new this(media.title, media.fileName, media.id, media.type, media.duration, startTime);
|
||||
}
|
||||
|
||||
//methods
|
||||
genUUID(){
|
||||
this.uuid = crypto.randomUUID();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -29,6 +29,7 @@ module.exports = class{
|
|||
|
||||
defineListeners(socket){
|
||||
socket.on("yank", (data) => {this.testYank(socket, data)});
|
||||
socket.on("play", (data) => {this.testPlay(socket, data)});
|
||||
}
|
||||
|
||||
async testYank(socket, data){
|
||||
|
|
@ -39,6 +40,19 @@ module.exports = class{
|
|||
}
|
||||
}
|
||||
|
||||
async testPlay(socket, data){
|
||||
try{
|
||||
//Pull media list
|
||||
const mediaList = await this.yankMedia(data.url);
|
||||
//Get active channel from server/socket
|
||||
const chan = this.server.activeChannels.get(socket.chan)
|
||||
//Queue the first media object given
|
||||
chan.queue.queueMedia(mediaList[0]);
|
||||
}catch(err){
|
||||
return loggerUtils.socketExceptionHandler(socket, err);
|
||||
}
|
||||
}
|
||||
|
||||
async yankMedia(url){
|
||||
const pullType = await this.getMediaType(url)
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue