Continued work on media scheduler

This commit is contained in:
rainbow napkin 2025-01-28 09:43:39 -05:00
parent 9d01b4c962
commit d5a2a51be2
14 changed files with 415 additions and 54 deletions

View file

@ -44,16 +44,38 @@ module.exports = class{
defineListeners(socket){
socket.on("queue", (data) => {this.queueURL(socket, data)});
socket.on("delete", (data => {this.deleteMedia(socket, data)}));
socket.on("move", (data => {this.moveMedia(socket, data)}));
}
async queueURL(socket, data){
try{
//pull URL and start time from data
let {url, start} = data;
let {url, start, title} = data;
//Pull media list
const mediaList = await yanker.yankMedia(url);
const mediaList = await yanker.yankMedia(url, title);
//If we didn't find any media
if(mediaList == null || mediaList.length <= 0){
//Bitch, moan, complain...
loggerUtils.socketErrorHandler(socket, "No media found!", "queue");
//and ignore it!
return;
}
//If we have an invalid time
if(start == null || start < (new Date).getTime()){
//Get last item from schedule
const lastItem = (Array.from(this.schedule)[this.schedule.size - 1]);
//if we have a last item
if(lastItem != null){
//Throw it on five ms after the last item
start = lastItem[1].startTime + (lastItem[1].duration * 1000) + 5;
}
}
//Queue the first media object given
this.queueMedia(mediaList[0], start, socket);
@ -62,6 +84,24 @@ module.exports = class{
}
}
deleteMedia(socket, data){
try{
//Remove media by UUID
this.removeMedia(data.uuid, socket);
}catch(err){
return loggerUtils.socketExceptionHandler(socket, err);
}
}
moveMedia(socket, data){
try{
//Move media by UUID
this.rescheduleMedia(data.uuid, data.start, socket);
}catch(err){
return loggerUtils.socketExceptionHandler(socket, err);
}
}
//Default start time to now + half a second to give everyone time to process shit
queueMedia(inputMedia, start = new Date().getTime() + 50, socket){
//Create a new media queued object, set start time to now
@ -69,9 +109,6 @@ module.exports = class{
//schedule the media
this.scheduleMedia(mediaObj, socket);
//Refresh the next timer to ensure whatever comes on next is right
this.refreshNextTimer();
}
refreshNextTimer(){
@ -93,6 +130,62 @@ module.exports = class{
this.nextTimer = setTimeout(()=>{this.start(nextItem)}, startsIn);
}
rescheduleMedia(uuid, start = new Date().getTime() + 50, socket){
//Find and remove media from the schedule by UUID
const media = this.removeMedia(uuid);
//If we got a bad request
if(media == null){
//If an originating socket was provided for this request
if(socket != null){
//Yell at the user for being an asshole
loggerUtils.socketErrorHandler(socket, "Cannot move non-existant item!", "queue");
}
//Ignore it
return;
}
//Set media time
media.startTime = start;
//Re-schedule the media for the given time
this.scheduleMedia(media, socket);
}
removeMedia(uuid, socket){
//Get requested media
const media = this.getItemByUUID(uuid);
//If we got a bad request
if(media == null){
//If an originating socket was provided for this request
if(socket != null){
//Yell at the user for being an asshole
loggerUtils.socketErrorHandler(socket, "Cannot delete non-existant item!", "queue");
}
//Ignore it
return;
}
//If we're currently playing the requested item.
if(this.nowPlaying != null && this.nowPlaying.uuid == uuid){
//End playback
this.end();
}
//Take the item out of the schedule
this.schedule.delete(media.startTime);
//Refresh next timer
this.refreshNextTimer();
//Broadcast the channel queue
this.broadcastQueue();
//return found media in-case our calling function needs it :P
return media;
}
scheduleMedia(mediaObj, socket){
/* This is a fun method and I think it deserves it's own little explination...
Since we're working with a time based schedule, using start epochs as keys for our iterable seemed the best option
@ -159,6 +252,9 @@ module.exports = class{
//Broadcast the channel queue
this.broadcastQueue();
//Refresh the next timer to ensure whatever comes on next is right
this.refreshNextTimer();
}
start(mediaObj){
@ -270,6 +366,17 @@ module.exports = class{
}
}
getItemByUUID(uuid){
//Iterate through the schedule
for(let item of this.schedule){
//If the uuid matches
if(item[1].uuid == uuid){
//return the found item
return item[1];
}
}
}
sendMedia(socket){
//Create data object
const data = {