From c21ec0d1b98d1fcb12d38b702c35e42564b54e13 Mon Sep 17 00:00:00 2001 From: Moe Alam Date: Thu, 29 Oct 2020 16:14:52 -0700 Subject: [PATCH] ONVIF Scanner show Snapshot --- libs/monitor/utils.js | 73 ++++++++++++++++++++++++++++++- libs/scanners.js | 14 +++++- web/libs/js/dash2.onvifscanner.js | 1 + 3 files changed, 86 insertions(+), 2 deletions(-) diff --git a/libs/monitor/utils.js b/libs/monitor/utils.js index e1eb0a5e..0b05727c 100644 --- a/libs/monitor/utils.js +++ b/libs/monitor/utils.js @@ -1,3 +1,6 @@ +const fs = require('fs'); +const treekill = require('tree-kill'); +const spawn = require('child_process').spawn; module.exports = (s,config,lang) => { const cameraDestroy = function(e,p){ if( @@ -82,7 +85,75 @@ module.exports = (s,config,lang) => { } } } + const createSnapshot = (options) => { + const url = options.url + const streamDir = options.streamDir || s.dir.streams + const inputOptions = options.input || [] + const outputOptions = options.output || [] + return new Promise((resolve,reject) => { + if(!url){ + resolve(null); + return + } + const temporaryImageFile = streamDir + s.gid(5) + '.jpg' + const ffmpegCmd = s.splitForFFPMEG(`-loglevel warning -re -probesize 100000 -analyzeduration 100000 ${inputOptions.join(' ')} -i "${url}" ${outputOptions.join(' ')} -f image2 -an -vf "fps=1" -vframes 1 "${temporaryImageFile}"`) + const snapProcess = spawn('ffmpeg',ffmpegCmd,{detached: true}) + snapProcess.stderr.on('data',function(data){ + // s.debugLog(data.toString()) + }) + snapProcess.on('close',async function(data){ + clearTimeout(snapProcessTimeout) + fs.readFile(temporaryImageFile,(err,imageBuffer) => { + try{ + s.file('delete',temporaryImageFile) + }catch(err){ + + } + if(err){ + s.debugLog(err) + } + resolve(imageBuffer) + }) + }) + var snapProcessTimeout = setTimeout(function(){ + var pid = snapProcess.pid + if(s.isWin){ + spawn("taskkill", ["/pid", pid, '/t']) + }else{ + process.kill(-pid, 'SIGTERM') + } + setTimeout(function(){ + if(s.isWin === false){ + treekill(pid) + }else{ + snapProcess.kill() + } + fs.readFile(temporaryImageFile,(err,imageBuffer) => { + try{ + s.file('delete',temporaryImageFile) + }catch(err){ + + } + if(err){ + s.debugLog(err) + } + resolve(imageBuffer) + }) + },10000) + },30000) + }) + } + const addCredentialsToStreamLink = (options) => { + const streamUrl = options.url + const username = options.username + const password = options.password + const urlParts = streamUrl.split('://') + urlParts[0] = 'http' + return ['rtsp','://',`${username}:${password}@`,urlParts[1]].join('') + } return { - cameraDestroy: cameraDestroy + cameraDestroy: cameraDestroy, + createSnapshot: createSnapshot, + addCredentialsToStreamLink: addCredentialsToStreamLink, } } diff --git a/libs/scanners.js b/libs/scanners.js index d99a0210..1c19316a 100644 --- a/libs/scanners.js +++ b/libs/scanners.js @@ -2,6 +2,10 @@ var os = require('os'); var exec = require('child_process').exec; var onvif = require("node-onvif"); module.exports = function(s,config,lang,app,io){ + const { + createSnapshot, + addCredentialsToStreamLink, + } = require('./monitor/utils.js')(s,config,lang) const activeProbes = {} const runFFprobe = (url,auth,callback) => { var endData = {ok: false} @@ -114,7 +118,15 @@ module.exports = function(s,config,lang,app,io){ uri: stream.data.GetStreamUriResponse.MediaUri.Uri } responseList.push(cameraResponse) - if(foundCameraCallback)foundCameraCallback(Object.assign(cameraResponse,{f: 'onvif'})) + const imageSnap = (await createSnapshot({ + output: ['-s 400x400'], + url: addCredentialsToStreamLink({ + username: onvifUsername, + password: onvifPassword, + url: stream.data.GetStreamUriResponse.MediaUri.Uri + }), + })).toString('base64'); + if(foundCameraCallback)foundCameraCallback(Object.assign(cameraResponse,{f: 'onvif', snapShot: imageSnap})) }catch(err){ const searchError = (find) => { return s.stringContains(find,err.message,true) diff --git a/web/libs/js/dash2.onvifscanner.js b/web/libs/js/dash2.onvifscanner.js index 04e6c25b..5c83362d 100644 --- a/web/libs/js/dash2.onvifscanner.js +++ b/web/libs/js/dash2.onvifscanner.js @@ -24,6 +24,7 @@ $(document).ready(function(e){ streamUrl = options.uri } $('#onvif_probe .output_data').append(` +    ${options.ip} ${options.port}