UNPKG

casparcg-state

Version:

Node.js Javascript/Typescript library for keeping and resolving a given state of CasparCG into commands for casparcg-connection.

166 lines 8.51 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.resolveBackgroundState = exports.diffBackground = void 0; const api_1 = require("../api"); const util_1 = require("../util"); const casparcg_connection_1 = require("casparcg-connection"); function diffBackground(oldState, newState, channel, layer) { const oldLayer = (0, util_1.getLayer)(oldState, channel, layer); const newLayer = (0, util_1.getLayer)(newState, channel, layer); let bgDiff = (0, util_1.compareAttrs)(newLayer.nextUp, oldLayer.nextUp, ['content']); let noClear = false; if (!bgDiff && newLayer.nextUp) { if (newLayer.nextUp.content === api_1.LayerContentType.MEDIA || newLayer.nextUp.content === api_1.LayerContentType.INPUT || newLayer.nextUp.content === api_1.LayerContentType.HTMLPAGE || newLayer.nextUp.content === api_1.LayerContentType.ROUTE) { const nl = newLayer.nextUp; const ol = oldLayer.nextUp; (0, util_1.setDefaultValue)([nl, ol], ['auto'], false); bgDiff = (0, util_1.compareAttrs)(nl, ol, ['auto', 'channelLayout']); } if (!bgDiff && newLayer.nextUp.content === api_1.LayerContentType.MEDIA) { const nl = newLayer.nextUp; const ol = oldLayer.nextUp; (0, util_1.setDefaultValue)([nl, ol], ['seek', 'length', 'inPoint'], 0); bgDiff = (0, util_1.compareAttrs)(nl, ol, ['media', 'seek', 'length', 'inPoint', 'afilter', 'vfilter']); } if (!bgDiff && newLayer.nextUp.content === api_1.LayerContentType.ROUTE) { const nl = newLayer.nextUp; const ol = oldLayer.nextUp; bgDiff = (0, util_1.compareAttrs)(nl, ol, ['delay', 'mode', 'afilter', 'vfilter']); } if (!bgDiff && newLayer.nextUp.content === api_1.LayerContentType.INPUT) { const nl = newLayer.nextUp; const ol = oldLayer.nextUp; bgDiff = (0, util_1.compareAttrs)(nl, ol, ['afilter', 'vfilter']); } if (!bgDiff && newLayer.nextUp && oldLayer.nextUp && (typeof newLayer.nextUp.media !== 'string' || typeof oldLayer.nextUp.media !== 'string')) { const nMedia = newLayer.nextUp.media; const oMedia = oldLayer.nextUp.media; bgDiff = (0, util_1.compareAttrs)(nMedia, oMedia, ['inTransition', 'outTransition', 'changeTransition']); } if (!bgDiff && newLayer.nextUp && 'route' in newLayer.nextUp && oldLayer.nextUp && 'route' in oldLayer.nextUp) { const nRoute = newLayer.nextUp.route; const oRoute = oldLayer.nextUp.route; bgDiff = (0, util_1.compareAttrs)(nRoute, oRoute, ['channel', 'layer']); if (bgDiff) noClear = true; } } return { bgDiff, noClear }; } exports.diffBackground = diffBackground; function resolveBackgroundState(oldState, newState, channel, layer, forceDiff = false) { const oldChannel = (0, util_1.getChannel)(oldState, channel); const newChannel = (0, util_1.getChannel)(newState, channel); const oldLayer = (0, util_1.getLayer)(oldState, channel, layer); const newLayer = (0, util_1.getLayer)(newState, channel, layer); const diffCmds = { cmds: [], }; const diff = diffBackground(oldState, newState, channel, layer); let bgDiff = diff.bgDiff; const noClear = diff.noClear; if (forceDiff) bgDiff = 'Forced diff by foreground'; if (bgDiff) { const options = { channel: newChannel.channelNo, layer: newLayer.layerNo, }; if (newLayer.nextUp) { // make sure the layer is empty before trying to load something new // this prevents weird behaviour when files don't load correctly if (oldLayer.nextUp && !oldLayer.nextUp.clearOn404 && !noClear) { (0, util_1.addCommands)(diffCmds, (0, util_1.addContext)((0, util_1.literal)({ command: casparcg_connection_1.Commands.Loadbg, params: { channel: newChannel.channelNo, layer: newLayer.layerNo, clip: 'EMPTY', }, }), `Old nextUp was set, clear it first (${oldLayer.nextUp.media})`, newLayer)); } (0, util_1.setTransition)(options, newChannel, newLayer, newLayer.nextUp.media, false, true); if (newLayer.nextUp.content === api_1.LayerContentType.MEDIA) { const layer = newLayer.nextUp; const { inPointFrames, lengthFrames, seekFrames, looping, channelLayout } = (0, util_1.calculatePlayAttributes)(0, layer, newChannel, oldChannel); (0, util_1.addCommands)(diffCmds, (0, util_1.addContext)((0, util_1.literal)({ command: casparcg_connection_1.Commands.Loadbg, params: (0, util_1.fixPlayCommandInput)({ ...options, auto: layer.auto, clip: (newLayer.nextUp.media || '').toString(), in: inPointFrames, seek: seekFrames, length: lengthFrames || undefined, loop: !!looping, channelLayout: channelLayout, clearOn404: layer.clearOn404, aFilter: layer.afilter, vFilter: layer.vfilter, }), }), `Nextup media (${newLayer.nextUp.media})`, newLayer)); } else if (newLayer.nextUp.content === api_1.LayerContentType.HTMLPAGE) { // const layer = newLayer.nextUp as HtmlPageLayer & NextUp (0, util_1.addCommands)(diffCmds, (0, util_1.addContext)((0, util_1.literal)({ command: casparcg_connection_1.Commands.LoadbgHtml, params: { ...options, url: (newLayer.nextUp.media || '').toString(), }, }), `Nextup HTML (${newLayer.nextUp.media})`, newLayer)); } else if (newLayer.nextUp.content === api_1.LayerContentType.INPUT) { const layer = newLayer.nextUp; (0, util_1.addCommands)(diffCmds, (0, util_1.addContext)((0, util_1.literal)({ command: casparcg_connection_1.Commands.LoadbgDecklink, params: { ...options, device: layer.input.device, format: layer.input.format, // filter: layer.filter, channelLayout: layer.input.channelLayout, aFilter: layer.afilter, vFilter: layer.vfilter, }, }), `Nextup Decklink (${layer.input.device})`, newLayer)); } else if (newLayer.nextUp.content === api_1.LayerContentType.ROUTE) { const layer = newLayer.nextUp; (0, util_1.addCommands)(diffCmds, (0, util_1.addContext)((0, util_1.literal)({ command: casparcg_connection_1.Commands.LoadbgRoute, params: { ...options, route: layer.route, mode: layer.mode, channelLayout: layer.route ? layer.route.channelLayout : undefined, framesDelay: layer.delay ? Math.floor((0, util_1.time2FramesChannel)(layer.delay, newChannel, oldChannel)) : undefined, aFilter: layer.afilter, vFilter: layer.vfilter, }, }), `Nextup Route (${layer.route})`, newLayer)); } // } else if (compareAttrs(oldLayer.nextUp, newLayer, ['media'])) { // this.log('REMOVE BG') // console.log('REMOVE BG', oldLayer.nextUp, newLayer) // additionalCmds.push(new AMCP.LoadbgCommand({ // channel: newChannel.channelNo, // layer: newLayer.layerNo, // clip: 'EMPTY' // })) } } return { commands: diffCmds, }; } exports.resolveBackgroundState = resolveBackgroundState; //# sourceMappingURL=background.js.map