2020-04-06 22:47:58 +00:00
|
|
|
import CasparConnection from 'casparcg-connection'
|
|
|
|
|
|
|
|
const CasparCG = CasparConnection.CasparCG
|
|
|
|
const AMCP = CasparConnection.AMCP
|
2018-06-26 18:35:12 +00:00
|
|
|
|
2020-04-05 05:12:55 +00:00
|
|
|
const timeoutDuration = 5000
|
2018-06-26 18:35:12 +00:00
|
|
|
|
|
|
|
let io
|
|
|
|
let logger
|
|
|
|
|
|
|
|
let connection
|
|
|
|
let casparIsPlaying
|
|
|
|
let casparIsConnected
|
|
|
|
let currentHost
|
|
|
|
|
2020-04-06 22:47:58 +00:00
|
|
|
export function initialise(log, db, socket) {
|
2018-06-26 18:35:12 +00:00
|
|
|
io = socket.socket
|
|
|
|
logger = log
|
2020-04-06 22:47:58 +00:00
|
|
|
db = db
|
2018-06-26 18:35:12 +00:00
|
|
|
|
2020-04-06 22:47:58 +00:00
|
|
|
connect(db)
|
2018-06-26 18:35:12 +00:00
|
|
|
}
|
|
|
|
|
2020-04-06 22:47:58 +00:00
|
|
|
export function connect(db) {
|
|
|
|
currentHost = db.get('settings').value().casparhost
|
2018-06-26 18:35:12 +00:00
|
|
|
casparIsPlaying = false
|
|
|
|
casparIsConnected = false
|
|
|
|
logger.info('CasparCG: Connectiong to', currentHost + ':' + 5250)
|
|
|
|
|
|
|
|
connection = new CasparCG({
|
|
|
|
host: currentHost,
|
|
|
|
port: 5250,
|
|
|
|
queueMode: 2,
|
|
|
|
autoReconnectInterval: timeoutDuration,
|
|
|
|
onError: err => {
|
2020-04-05 05:12:55 +00:00
|
|
|
logger.error(err, 'CasparCG: Error')
|
2018-06-26 18:35:12 +00:00
|
|
|
},
|
|
|
|
onConnectionStatus: data => {
|
2020-04-06 22:47:58 +00:00
|
|
|
if (casparIsPlaying) return
|
2018-06-26 18:35:12 +00:00
|
|
|
casparIsConnected = data.connected
|
|
|
|
|
2020-04-05 05:12:55 +00:00
|
|
|
if (!casparIsConnected) {
|
|
|
|
logger.warn(`CasparCG: connection down, retrying in ${timeoutDuration / 1000} seconds`)
|
2018-06-26 18:35:12 +00:00
|
|
|
io.emit('casparcg.status', currentStatus())
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onConnected: async connected => {
|
2020-04-06 22:47:58 +00:00
|
|
|
if (casparIsPlaying) return
|
2018-06-26 18:35:12 +00:00
|
|
|
logger.info('CasparCG: connected', connected)
|
2020-04-05 05:12:55 +00:00
|
|
|
if (!casparIsPlaying) {
|
2020-04-06 22:47:58 +00:00
|
|
|
startPlaying(db).then()
|
2020-04-05 05:12:55 +00:00
|
|
|
} else {
|
|
|
|
logger.warn('CasparCG: Stopped from starting play again.')
|
|
|
|
}
|
2018-06-26 18:35:12 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export function currentStatus(e) {
|
|
|
|
return {
|
|
|
|
connected: casparIsConnected,
|
|
|
|
playing: casparIsPlaying,
|
|
|
|
error: e,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-06 22:47:58 +00:00
|
|
|
export async function startPlaying(db) {
|
|
|
|
let ip = db.get('settings').value().casparplayhost
|
2018-06-26 18:35:12 +00:00
|
|
|
|
|
|
|
// Check if we lost connection while attempting to start playing
|
|
|
|
if (!connection.connected) {
|
|
|
|
logger.error('CasparCG: Attempted to play but connection was lost')
|
|
|
|
}
|
|
|
|
|
2020-04-05 05:12:55 +00:00
|
|
|
let success = false
|
2018-06-26 18:35:12 +00:00
|
|
|
|
2020-04-05 05:12:55 +00:00
|
|
|
try {
|
|
|
|
// Send a play command
|
2020-04-06 22:47:58 +00:00
|
|
|
let command = `PLAY 1-100 [HTML] "http://${ip}/client.html" CUT 1 LINEAR RIGHT`
|
2020-04-05 05:12:55 +00:00
|
|
|
logger.info(`CasparCG Command: ${command}`)
|
|
|
|
await connection.do(new AMCP.CustomCommand(command))
|
|
|
|
success = true
|
|
|
|
} catch (e) {
|
|
|
|
// Weird error where it throws an error despite a successful play command on reconnect
|
|
|
|
if (e && e.responseProtocol && e.responseProtocol.code >= 200 && e.responseProtocol.code < 300) {
|
|
|
|
success = true
|
|
|
|
} else {
|
|
|
|
logger.error(e, 'CasparCG: Error starting play on client')
|
2018-06-26 18:35:12 +00:00
|
|
|
}
|
2020-04-05 05:12:55 +00:00
|
|
|
}
|
2018-06-26 18:35:12 +00:00
|
|
|
|
2020-04-05 05:12:55 +00:00
|
|
|
if (success) {
|
2018-06-26 18:35:12 +00:00
|
|
|
casparIsPlaying = true
|
|
|
|
|
|
|
|
// We are playing, notify all clients
|
|
|
|
io.emit('casparcg.status', currentStatus())
|
2020-04-05 05:12:55 +00:00
|
|
|
logger.info('CasparCG: client is up and playing')
|
2020-04-06 22:47:58 +00:00
|
|
|
/* console.log(connection)
|
|
|
|
for (var key in connection) {
|
|
|
|
console.log(key, '=', typeof(connection[key]))
|
|
|
|
} */
|
|
|
|
connection.autoConnect = false
|
|
|
|
// connection.close()
|
2020-04-05 05:12:55 +00:00
|
|
|
} else {
|
2018-06-26 18:35:12 +00:00
|
|
|
// Unknown error occured
|
2020-04-05 05:12:55 +00:00
|
|
|
casparIsPlaying = false
|
2018-06-26 18:35:12 +00:00
|
|
|
io.emit('casparcg.status', currentStatus(e))
|
|
|
|
}
|
|
|
|
}
|