cache side panel icon for faster loading and lower load on client open

merge-requests/118/head
Moe 2019-10-02 22:04:52 -07:00
parent 2ad0dbcc4d
commit 3a81da567c
1 changed files with 106 additions and 85 deletions

View File

@ -105,6 +105,7 @@ module.exports = function(s,config,lang){
callback = options
var options = {flags: ''}
}
s.checkDetails(monitor)
var inputOptions = []
var outputOptions = []
var streamDir = s.dir.streams + monitor.ke + '/' + monitor.mid + '/'
@ -112,11 +113,24 @@ module.exports = function(s,config,lang){
var secondsInward = options.secondsInward || '0'
if(secondsInward.length === 1)secondsInward = '0' + secondsInward
if(options.flags)outputOptions.push(options.flags)
var runExtraction = function(){
const checkExists = function(streamDir,callback){
s.fileStats(streamDir,function(err){
var response = false
if(err){
// s.debugLog(err)
}else{
response = true
}
callback(response)
})
}
const noIconChecks = function(){
const runExtraction = function(){
try{
var snapBuffer = []
var temporaryImageFile = streamDir + s.gid(5) + '.jpg'
var ffmpegCmd = `-loglevel quiet -re -probesize 1000000 -analyzeduration 1000000 ${inputOptions.join(' ')} -i "${url}" ${outputOptions.join(' ')} -vframes 1 ${temporaryImageFile}`
var iconImageFile = streamDir + 'icon.jpg'
var ffmpegCmd = `-loglevel quiet -re -probesize 1000000 -analyzeduration 1000000 ${inputOptions.join(' ')} -i "${url}" ${outputOptions.join(' ')} -vframes 1 "${temporaryImageFile}"`
var snapProcess = spawn(config.ffmpegDir,s.splitForFFPMEG(ffmpegCmd),{detached: true})
snapProcess.stderr.on('data',function(data){
console.log(data.toString())
@ -125,9 +139,14 @@ module.exports = function(s,config,lang){
clearTimeout(snapProcessTimeout)
fs.readFile(temporaryImageFile,function(err,buffer){
if(buffer){
if(options.useIcon === true){
fs.writeFile(iconImageFile,buffer,function(){
callback(buffer,false)
})
}else{
callback(buffer,false)
}
}else{
s.debugLog(err,ffmpegCmd)
fs.readFile(config.defaultMjpeg,function(err,buffer){
callback(buffer,false)
})
@ -149,17 +168,6 @@ module.exports = function(s,config,lang){
if(url){
runExtraction()
}else{
var checkExists = function(streamDir,callback){
s.fileStats(streamDir,function(err){
var response = false
if(err){
// s.debugLog(err)
}else{
response = true
}
callback(response)
})
}
checkExists(streamDir + 's.jpg',function(success){
if(success === false){
checkExists(streamDir + 'detectorStream.m3u8',function(success){
@ -171,9 +179,9 @@ module.exports = function(s,config,lang){
switch(monitor.protocol){
case'rtsp':
if(
monitor.details.rtsp_transport
&& monitor.details.rtsp_transport !== ''
&& monitor.details.rtsp_transport !== 'no'
details.rtsp_transport
&& details.rtsp_transport !== ''
&& details.rtsp_transport !== 'no'
){
inputOptions.push('-rtsp_transport ' + monitor.details.rtsp_transport)
}
@ -202,6 +210,20 @@ module.exports = function(s,config,lang){
})
}
}
if(options.useIcon === true){
checkExists(streamDir + 's.jpg',function(success){
if(success === false){
noIconChecks()
}else{
s.readFile(streamDir + 'icon.jpg',function(err,snapBuffer){
callback(snapBuffer,true)
})
}
})
}else{
noIconChecks()
}
}
s.mergeDetectorBufferChunks = function(monitor,callback){
var pathDir = s.dir.streams+monitor.ke+'/'+monitor.id+'/'
var mergedFile = s.formattedTime()+'.mp4'
@ -632,16 +654,15 @@ module.exports = function(s,config,lang){
}
return options
}
s.cameraSendSnapshot = function(e){
s.cameraSendSnapshot = function(e,options){
if(!options)options = {}
s.checkDetails(e)
if(config.doSnapshot === true){
if(e.mon.mode !== 'stop'){
var pathDir = s.dir.streams+e.ke+'/'+e.mid+'/'
var url = s.buildMonitorUrl(e.mon)
s.getRawSnapshotFromMonitor(e.mon,{
flags: '-s 200x200',
url: url
},function(data,isStaticFile){
s.getRawSnapshotFromMonitor(e.mon,Object.assign({
flags: '-s 200x200'
},options),function(data,isStaticFile){
if(data && (data[data.length-2] === 0xFF && data[data.length-1] === 0xD9)){
s.tx({
f: 'monitor_snapshot',
@ -1285,7 +1306,7 @@ module.exports = function(s,config,lang){
s.resetStreamCheck(e)
})
}
s.cameraSendSnapshot({mid:e.id,ke:e.ke,mon:e})
s.cameraSendSnapshot({mid:e.id,ke:e.ke,mon:e},{useIcon: true})
//check host to see if has password and user in it
clearTimeout(s.group[e.ke].activeMonitors[e.id].recordingChecker)
if(s.group[e.ke].activeMonitors[e.id].isStarted === true){
@ -1628,7 +1649,7 @@ module.exports = function(s,config,lang){
s.group[e.ke].activeMonitors[e.id].isStarted = false
s.group[e.ke].activeMonitors[e.id].isRecording = false
s.tx({f:'monitor_stopping',mid:e.id,ke:e.ke,time:s.formattedTime()},'GRP_'+e.ke);
s.cameraSendSnapshot({mid:e.id,ke:e.ke,mon:e})
s.cameraSendSnapshot({mid:e.id,ke:e.ke,mon:e},{useIcon: true})
if(e.functionMode === 'stop'){
s.userLog(e,{type:lang['Monitor Stopped'],msg:lang.MonitorStoppedText});
clearTimeout(s.group[e.ke].activeMonitors[e.id].delete)