From ad1302aae418311701a2e6f153d8b145810c13be Mon Sep 17 00:00:00 2001 From: vexorian Date: Sun, 12 Nov 2023 11:00:22 -0400 Subject: [PATCH] Use the median to decide what filler to play. This has memory consequences unfortunately. --- Dockerfile | 2 +- package.json | 3 ++- src/helperFuncs.js | 34 +++++++++++++++++++++++++++++++--- src/video.js | 13 +++++++++++-- 4 files changed, 45 insertions(+), 7 deletions(-) diff --git a/Dockerfile b/Dockerfile index 23435cc..b2abb0a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,6 @@ FROM node:12.18-alpine3.12 WORKDIR /home/node/app -COPY package*.json ./ +COPY package.json ./ RUN npm install && npm install -g browserify nexe@3.3.7 COPY --from=vexorian/dizquetv:nexecache /var/nexe/linux-x64-12.16.2 /var/nexe/ COPY . . diff --git a/package.json b/package.json index 2bb8697..a944c5d 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,8 @@ "node-ssdp": "^4.0.0", "random-js": "2.1.0", "request": "^2.88.2", - "uuid": "^8.0.0", + "quickselect": "2.0.0", + "uuid" : "9.0.1", "xml-writer": "^1.7.0" }, "bin": "dist/index.js", diff --git a/src/helperFuncs.js b/src/helperFuncs.js index 98e7552..9ed0755 100644 --- a/src/helperFuncs.js +++ b/src/helperFuncs.js @@ -8,6 +8,7 @@ module.exports = { let channelCache = require('./channel-cache'); const SLACK = require('./constants').SLACK; const randomJS = require("random-js"); +const quickselect = require("quickselect"); const Random = randomJS.Random; const random = new Random( randomJS.MersenneTwister19937.autoSeed() ); @@ -194,7 +195,11 @@ function pickRandomWithMaxDuration(channel, fillers, maxDuration) { } let listM = 0; let fillerId = undefined; - for (let j = 0; j < fillers.length; j++) { + + let median = getMedian(channelCache, channel, fillers); + + for (let medianCheck = 1; medianCheck >= 0; medianCheck--) { + for (let j = 0; j < fillers.length; j++) { list = fillers[j].content; let pickedList = false; let n = 0; @@ -204,6 +209,9 @@ function pickRandomWithMaxDuration(channel, fillers, maxDuration) { // a few extra milliseconds won't hurt anyone, would it? dun dun dun if (clip.duration <= maxDuration + SLACK ) { let t1 = channelCache.getProgramLastPlayTime( channel.number, clip ); + if ( (medianCheck==1) && (t1 > median) ) { + continue; + } let timeSince = ( (t1 == 0) ? D : (t0 - t1) ); if (timeSince < channel.fillerRepeatCooldown - SLACK) { @@ -247,11 +255,13 @@ function pickRandomWithMaxDuration(channel, fillers, maxDuration) { } } } + } + if (pick1 != null) { + break; + } } let pick = pick1; - let pickTitle = "null"; if (pick != null) { - pickTitle = pick.title; pick = JSON.parse( JSON.stringify(pick) ); pick.fillerId = fillerId; } @@ -322,6 +332,24 @@ function getWatermark( ffmpegSettings, channel, type) { } +function getMedian(channelCache, channel, fillers) { + let times = []; + for (let j = 0; j < fillers.length; j++) { + list = fillers[j].content; + for (let i = 0; i < list.length; i++) { + let clip = list[i]; + let t = channelCache.getProgramLastPlayTime( channel.number, clip); + times.push(t); + } + } + if (times.length == 0) { + return null; + } + quickselect(times, times.length / 2) + return times[times.length / 2]; + +} + function generateChannelContext(channel) { let channelContext = {}; for (let i = 0; i < CHANNEL_CONTEXT_KEYS.length; i++) { diff --git a/src/video.js b/src/video.js index e4f6f96..9f1ff4d 100644 --- a/src/video.js +++ b/src/video.js @@ -291,8 +291,17 @@ function video( channelService, fillerDB, db, programmingService, activeChannelS throw "No video to play, this means there's a serious unexpected bug or the channel db is corrupted." } let fillers = await fillerDB.getFillersFromChannel(brandChannel); - let lineup = helperFuncs.createLineup(prog, brandChannel, fillers, isFirst) - lineupItem = lineup.shift(); + try { + let lineup = helperFuncs.createLineup(prog, brandChannel, fillers, isFirst) + lineupItem = lineup.shift(); + } catch (err) { + console.log("Error when attempting to pick video: " +err.stack); + lineupItem = { + isOffline: true, + err: err, + duration : 60000, + }; + } } if ( !isBetween && !isLoading && (lineupItem != null) ) {