2019-12-05 07:25:13 +00:00
|
|
|
|
|
|
|
const fs = require('fs')
|
2020-01-20 20:39:17 +00:00
|
|
|
const exec = require('child_process').exec
|
2019-12-05 07:25:13 +00:00
|
|
|
const spawn = require('child_process').spawn
|
|
|
|
process.send = process.send || function () {};
|
|
|
|
|
|
|
|
if(!process.argv[2] || !process.argv[3]){
|
|
|
|
return writeToStderr('Missing FFMPEG Command String or no command operator')
|
|
|
|
}
|
|
|
|
var jsonData = JSON.parse(fs.readFileSync(process.argv[3],'utf8'))
|
|
|
|
const ffmpegAbsolutePath = process.argv[2].trim()
|
|
|
|
const ffmpegCommandString = jsonData.cmd
|
2019-12-06 03:45:24 +00:00
|
|
|
const rawMonitorConfig = jsonData.rawMonitorConfig
|
2019-12-05 07:25:13 +00:00
|
|
|
const stdioPipes = jsonData.pipes || []
|
|
|
|
var newPipes = []
|
|
|
|
var stdioWriters = [];
|
2019-12-05 12:41:36 +00:00
|
|
|
|
2019-12-08 05:53:17 +00:00
|
|
|
var writeToStderr = function(text){
|
2019-12-08 15:49:27 +00:00
|
|
|
try{
|
|
|
|
stdioWriters[2].write(Buffer.from(`${text}`, 'utf8' ))
|
2019-12-08 05:53:17 +00:00
|
|
|
// stdioWriters[2].write(Buffer.from(`${new Error('writeToStderr').stack}`, 'utf8' ))
|
2019-12-08 15:49:27 +00:00
|
|
|
}catch(err){
|
2019-12-11 09:39:40 +00:00
|
|
|
// fs.appendFileSync('/home/Shinobi/test.log',text + '\n','utf8')
|
2019-12-08 05:53:17 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-20 20:39:17 +00:00
|
|
|
|
|
|
|
// [CTRL] + [C] = exit
|
2019-12-08 05:53:17 +00:00
|
|
|
process.on('uncaughtException', function (err) {
|
|
|
|
writeToStderr('Uncaught Exception occured!');
|
|
|
|
writeToStderr(err.stack);
|
|
|
|
});
|
2020-01-20 20:39:17 +00:00
|
|
|
const exitAction = function(){
|
|
|
|
process.kill(-cameraProcess.pid)
|
|
|
|
}
|
|
|
|
process.on('SIGTERM', exitAction);
|
|
|
|
process.on('SIGINT', exitAction);
|
|
|
|
process.on('exit', exitAction);
|
2019-12-08 05:53:17 +00:00
|
|
|
|
2019-12-05 07:25:13 +00:00
|
|
|
for(var i=0; i < stdioPipes; i++){
|
|
|
|
switch(i){
|
2019-12-08 15:49:27 +00:00
|
|
|
case 0:
|
|
|
|
newPipes[i] = 0
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
newPipes[i] = 1
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
newPipes[i] = 2
|
|
|
|
break;
|
2019-12-05 07:25:13 +00:00
|
|
|
case 3:
|
2019-12-11 07:23:01 +00:00
|
|
|
stdioWriters[i] = fs.createWriteStream(null, {fd: i, end:false});
|
2019-12-08 05:53:17 +00:00
|
|
|
if(rawMonitorConfig.details.detector === '1' && rawMonitorConfig.details.detector_pam === '1'){
|
|
|
|
newPipes[i] = 'pipe'
|
2019-12-06 03:45:24 +00:00
|
|
|
}else{
|
2019-12-08 05:53:17 +00:00
|
|
|
newPipes[i] = stdioWriters[i]
|
2019-12-06 03:45:24 +00:00
|
|
|
}
|
2019-12-05 07:25:13 +00:00
|
|
|
break;
|
2019-12-11 07:23:01 +00:00
|
|
|
case 5:
|
|
|
|
stdioWriters[i] = fs.createWriteStream(null, {fd: i, end:false});
|
|
|
|
newPipes[i] = 'pipe'
|
|
|
|
break;
|
2019-12-05 07:25:13 +00:00
|
|
|
default:
|
2019-12-11 07:23:01 +00:00
|
|
|
stdioWriters[i] = fs.createWriteStream(null, {fd: i, end:false});
|
2019-12-08 05:53:17 +00:00
|
|
|
newPipes[i] = stdioWriters[i]
|
2019-12-05 07:25:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-12-08 15:49:27 +00:00
|
|
|
stdioWriters.forEach((writer)=>{
|
|
|
|
writer.on('error', (err) => {
|
|
|
|
writeToStderr(err.stack);
|
|
|
|
});
|
|
|
|
})
|
2019-12-11 07:23:01 +00:00
|
|
|
writeToStderr(JSON.stringify(ffmpegCommandString))
|
2019-12-05 07:25:13 +00:00
|
|
|
var cameraProcess = spawn(ffmpegAbsolutePath,ffmpegCommandString,{detached: true,stdio:newPipes})
|
2019-12-05 14:12:29 +00:00
|
|
|
cameraProcess.on('close',()=>{
|
2019-12-08 15:49:27 +00:00
|
|
|
writeToStderr('Process Closed')
|
|
|
|
stdioWriters.forEach((writer)=>{
|
|
|
|
writer.end()
|
|
|
|
})
|
2019-12-05 14:12:29 +00:00
|
|
|
process.exit();
|
|
|
|
})
|
2019-12-11 07:23:01 +00:00
|
|
|
cameraProcess.stdio[5].on('data',(data)=>{
|
|
|
|
stdioWriters[5].write(data)
|
|
|
|
})
|
2019-12-08 08:00:56 +00:00
|
|
|
writeToStderr('Thread Opening')
|
|
|
|
|
2019-12-08 05:53:17 +00:00
|
|
|
|
2019-12-11 07:23:01 +00:00
|
|
|
|
|
|
|
if(rawMonitorConfig.details.detector === '1' && rawMonitorConfig.details.detector_pam === '1'){
|
|
|
|
try{
|
|
|
|
const attachPamDetector = require(__dirname + '/detector.js')(jsonData,stdioWriters[3])
|
|
|
|
attachPamDetector(cameraProcess,(err)=>{
|
|
|
|
writeToStderr(err)
|
|
|
|
})
|
|
|
|
}catch(err){
|
|
|
|
writeToStderr(err.stack)
|
2019-12-08 05:53:17 +00:00
|
|
|
}
|
2019-12-11 07:23:01 +00:00
|
|
|
}
|