2020-11-02 01:47:05 +00:00
|
|
|
const fs = require('fs');
|
|
|
|
const spawn = require('child_process').spawn;
|
|
|
|
const execSync = require('child_process').execSync;
|
2020-11-17 02:08:24 +00:00
|
|
|
module.exports = async (s,config,lang,onFinish) => {
|
2020-11-02 01:47:05 +00:00
|
|
|
const {
|
2020-11-03 05:22:03 +00:00
|
|
|
sanitizedFfmpegCommand,
|
|
|
|
createPipeArray,
|
|
|
|
splitForFFPMEG,
|
2020-11-17 02:08:24 +00:00
|
|
|
checkForWindows,
|
|
|
|
checkForUnix,
|
|
|
|
checkStaticBuilds,
|
|
|
|
checkVersion,
|
|
|
|
checkHwAccelMethods,
|
2020-11-02 01:47:05 +00:00
|
|
|
} = require('./ffmpeg/utils.js')(s,config,lang)
|
2020-11-17 02:08:24 +00:00
|
|
|
const {
|
|
|
|
buildMainInput,
|
|
|
|
buildMainStream,
|
|
|
|
buildJpegApiOutput,
|
|
|
|
buildMainRecording,
|
|
|
|
buildAudioDetector,
|
|
|
|
buildMainDetector,
|
|
|
|
buildEventRecordingOutput,
|
|
|
|
buildTimelapseOutput,
|
|
|
|
} = require('./ffmpeg/builders.js')(s,config,lang)
|
2019-07-26 06:24:55 +00:00
|
|
|
if(config.ffmpegBinary)config.ffmpegDir = config.ffmpegBinary
|
2020-11-17 02:08:24 +00:00
|
|
|
|
2018-10-02 02:21:16 +00:00
|
|
|
s.ffmpeg = function(e){
|
2019-12-11 07:23:01 +00:00
|
|
|
try{
|
2021-12-10 00:08:26 +00:00
|
|
|
const activeMonitor = s.group[e.ke].activeMonitors[e.mid];
|
2021-11-17 20:10:25 +00:00
|
|
|
const dataPortToken = s.gid(10);
|
|
|
|
s.dataPortTokens[dataPortToken] = {
|
|
|
|
type: 'cameraThread',
|
|
|
|
ke: e.ke,
|
|
|
|
mid: e.mid,
|
|
|
|
}
|
2021-05-23 20:40:29 +00:00
|
|
|
const ffmpegCommand = [`-progress pipe:5`];
|
2021-12-06 23:45:25 +00:00
|
|
|
const allOutputs = [
|
2021-05-23 20:40:29 +00:00
|
|
|
buildMainStream(e),
|
|
|
|
buildJpegApiOutput(e),
|
|
|
|
buildMainRecording(e),
|
|
|
|
buildAudioDetector(e),
|
|
|
|
buildMainDetector(e),
|
|
|
|
buildEventRecordingOutput(e),
|
|
|
|
buildTimelapseOutput(e),
|
2021-12-06 23:45:25 +00:00
|
|
|
];
|
|
|
|
if(allOutputs.filter(output => !!output).length > 0){
|
|
|
|
([
|
|
|
|
buildMainInput(e),
|
|
|
|
]).concat(allOutputs).forEach(function(commandStringPart){
|
|
|
|
ffmpegCommand.push(commandStringPart)
|
|
|
|
})
|
|
|
|
s.onFfmpegCameraStringCreationExtensions.forEach(function(extender){
|
|
|
|
extender(e,ffmpegCommand)
|
|
|
|
})
|
|
|
|
const stdioPipes = createPipeArray(e)
|
|
|
|
const ffmpegCommandString = ffmpegCommand.join(' ')
|
|
|
|
//hold ffmpeg command for log stream
|
|
|
|
activeMonitor.ffmpeg = sanitizedFfmpegCommand(e,ffmpegCommandString)
|
|
|
|
//clean the string of spatial impurities and split for spawn()
|
|
|
|
const ffmpegCommandParsed = splitForFFPMEG(ffmpegCommandString)
|
|
|
|
try{
|
2022-11-16 21:10:26 +00:00
|
|
|
fs.rmSync(e.sdir + 'cmd.txt')
|
2021-12-06 23:45:25 +00:00
|
|
|
}catch(err){
|
2019-12-11 07:23:01 +00:00
|
|
|
|
2021-05-23 20:40:29 +00:00
|
|
|
}
|
2021-12-06 23:45:25 +00:00
|
|
|
fs.writeFileSync(e.sdir + 'cmd.txt',JSON.stringify({
|
2021-12-19 15:39:44 +00:00
|
|
|
dataPortToken: dataPortToken,
|
2021-12-06 23:45:25 +00:00
|
|
|
cmd: ffmpegCommandParsed,
|
|
|
|
pipes: stdioPipes.length,
|
|
|
|
rawMonitorConfig: s.group[e.ke].rawMonitorConfigurations[e.id],
|
|
|
|
globalInfo: {
|
|
|
|
config: config,
|
|
|
|
isAtleatOneDetectorPluginConnected: s.isAtleatOneDetectorPluginConnected
|
|
|
|
}
|
|
|
|
},null,3),'utf8')
|
|
|
|
var cameraCommandParams = [
|
|
|
|
config.monitorDaemonPath ? config.monitorDaemonPath : __dirname + '/cameraThread/singleCamera.js',
|
|
|
|
config.ffmpegDir,
|
|
|
|
e.sdir + 'cmd.txt'
|
|
|
|
]
|
|
|
|
const cameraProcess = spawn('node',cameraCommandParams,{detached: true,stdio: stdioPipes})
|
|
|
|
if(config.debugLog === true && config.debugLogMonitors === true){
|
|
|
|
cameraProcess.stderr.on('data',(data) => {
|
2022-09-18 21:38:47 +00:00
|
|
|
const string = data.toString()
|
|
|
|
var checkLog = function(x){return string.indexOf(x)>-1}
|
|
|
|
switch(true){
|
|
|
|
case checkLog('pkt->duration = 0'):
|
|
|
|
case checkLog('[hls @'):
|
|
|
|
case checkLog('Past duration'):
|
|
|
|
case checkLog('Last message repeated'):
|
|
|
|
case checkLog('Non-monotonous DTS'):
|
|
|
|
case checkLog('NULL @'):
|
|
|
|
case checkLog('RTP: missed'):
|
|
|
|
case checkLog('deprecated pixel format used'):
|
|
|
|
if(!config.debugLogMonitorsVerbose){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
console.log(`${e.ke} ${e.name} (${e.mid})`)
|
2021-12-06 23:45:25 +00:00
|
|
|
console.log(data.toString())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return cameraProcess
|
|
|
|
}else{
|
|
|
|
return null
|
2021-08-26 19:51:05 +00:00
|
|
|
}
|
2021-05-23 20:40:29 +00:00
|
|
|
}catch(err){
|
|
|
|
s.systemLog(err)
|
|
|
|
return null
|
|
|
|
}
|
2018-09-29 02:09:14 +00:00
|
|
|
}
|
2018-09-30 22:44:04 +00:00
|
|
|
if(!config.ffmpegDir){
|
2020-11-17 02:08:24 +00:00
|
|
|
if(s.isWin){
|
|
|
|
const windowsCheck = checkForWindows()
|
|
|
|
if(!windowsCheck.ok){
|
|
|
|
const staticBuildCheck = await checkStaticBuilds()
|
|
|
|
if(!staticBuildCheck.ok){
|
|
|
|
console.log(staticBuildCheck.msg)
|
|
|
|
console.log('No FFmpeg found.')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
const staticBuildCheck = await checkStaticBuilds()
|
|
|
|
if(!staticBuildCheck.ok){
|
|
|
|
const unixCheck = checkForUnix()
|
|
|
|
if(!unixCheck.ok){
|
|
|
|
console.log(staticBuildCheck.msg.join('\n'))
|
|
|
|
console.log(unixCheck.msg)
|
|
|
|
console.log('No FFmpeg found.')
|
|
|
|
}
|
|
|
|
}else if(staticBuildCheck.msg.length > 0){
|
|
|
|
console.log(staticBuildCheck.msg.join('\n'))
|
|
|
|
}
|
|
|
|
}
|
2018-09-29 20:09:24 +00:00
|
|
|
}
|
2020-11-17 02:08:24 +00:00
|
|
|
checkVersion()
|
|
|
|
checkHwAccelMethods()
|
|
|
|
s.onFFmpegLoadedExtensions.forEach(function(extender){
|
|
|
|
extender()
|
|
|
|
})
|
|
|
|
onFinish()
|
2018-09-29 02:09:14 +00:00
|
|
|
}
|