Merge pull request #2746 from connortechnology/fix_2745

Fix 2745
This commit is contained in:
Isaac Connor 2019-10-30 16:59:20 -04:00 committed by GitHub
commit 8f327f23bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 156 additions and 152 deletions

View File

@ -87,7 +87,7 @@ Event::Event(
char sql[ZM_SQL_MED_BUFSIZ]; char sql[ZM_SQL_MED_BUFSIZ];
struct tm *stime = localtime(&start_time.tv_sec); struct tm *stime = localtime(&start_time.tv_sec);
snprintf(sql, sizeof(sql), "INSERT INTO Events ( MonitorId, StorageId, Name, StartTime, Width, Height, Cause, Notes, StateId, Orientation, Videoed, DefaultVideo, SaveJPEGs, Scheme ) values ( %d, %d, 'New Event', from_unixtime( %ld ), %d, %d, '%s', '%s', %d, %d, %d, '', %d, '%s' )", snprintf(sql, sizeof(sql), "INSERT INTO Events ( MonitorId, StorageId, Name, StartTime, Width, Height, Cause, Notes, StateId, Orientation, Videoed, DefaultVideo, SaveJPEGs, Scheme ) VALUES ( %d, %d, 'New Event', from_unixtime( %ld ), %d, %d, '%s', '%s', %d, %d, %d, '', %d, '%s' )",
monitor->Id(), monitor->Id(),
storage->Id(), storage->Id(),
start_time.tv_sec, start_time.tv_sec,
@ -288,27 +288,40 @@ void Event::createNotes(std::string &notes) {
notes += *setIter; notes += *setIter;
} }
} }
} } // void Event::createNotes(std::string &notes)
bool Event::WriteFrameImage(Image *image, struct timeval timestamp, const char *event_file, bool alarm_frame) { bool Event::WriteFrameImage(
Image *image,
struct timeval timestamp,
const char *event_file,
bool alarm_frame) {
int thisquality =
(alarm_frame && (config.jpeg_alarm_file_quality > config.jpeg_file_quality)) ?
config.jpeg_alarm_file_quality : 0 ; // quality to use, zero is default
int thisquality = ( alarm_frame && (config.jpeg_alarm_file_quality > config.jpeg_file_quality) ) ? config.jpeg_alarm_file_quality : 0 ; // quality to use, zero is default
bool rc; bool rc;
if ( !config.timestamp_on_capture ) { if ( !config.timestamp_on_capture ) {
// stash the image we plan to use in another pointer regardless if timestamped. // stash the image we plan to use in another pointer regardless if timestamped.
// exif is only timestamp at present this switches on or off for write
Image *ts_image = new Image(*image); Image *ts_image = new Image(*image);
monitor->TimestampImage(ts_image, &timestamp); monitor->TimestampImage(ts_image, &timestamp);
rc = ts_image->WriteJpeg(event_file, thisquality, (monitor->Exif() ? timestamp : (timeval){0,0})); // exif is only timestamp at present this switches on or off for write rc = ts_image->WriteJpeg(event_file, thisquality,
(monitor->Exif() ? timestamp : (timeval){0,0}));
delete(ts_image); delete(ts_image);
} else { } else {
rc = image->WriteJpeg(event_file, thisquality, (monitor->Exif() ? timestamp : (timeval){0,0})); // exif is only timestamp at present this switches on or off for write rc = image->WriteJpeg(event_file, thisquality,
(monitor->Exif() ? timestamp : (timeval){0,0}));
} }
return rc; return rc;
} }
bool Event::WriteFrameVideo( const Image *image, const struct timeval timestamp, VideoWriter* videow ) { bool Event::WriteFrameVideo(
const Image *image,
const struct timeval timestamp,
VideoWriter* videow) {
const Image* frameimg = image; const Image* frameimg = image;
Image ts_image; Image ts_image;
@ -336,7 +349,7 @@ bool Event::WriteFrameVideo( const Image *image, const struct timeval timestamp,
} }
return true; return true;
} } // bool Event::WriteFrameVideo
void Event::updateNotes(const StringSetMap &newNoteSetMap) { void Event::updateNotes(const StringSetMap &newNoteSetMap) {
bool update = false; bool update = false;
@ -347,7 +360,9 @@ void Event::updateNotes( const StringSetMap &newNoteSetMap ) {
noteSetMap = newNoteSetMap; noteSetMap = newNoteSetMap;
update = true; update = true;
} else { } else {
for ( StringSetMap::const_iterator newNoteSetMapIter = newNoteSetMap.begin(); newNoteSetMapIter != newNoteSetMap.end(); ++newNoteSetMapIter ) { for ( StringSetMap::const_iterator newNoteSetMapIter = newNoteSetMap.begin();
newNoteSetMapIter != newNoteSetMap.end();
++newNoteSetMapIter ) {
const std::string &newNoteGroup = newNoteSetMapIter->first; const std::string &newNoteGroup = newNoteSetMapIter->first;
const StringSet &newNoteSet = newNoteSetMapIter->second; const StringSet &newNoteSet = newNoteSetMapIter->second;
//Info( "Got %d new strings", newNoteSet.size() ); //Info( "Got %d new strings", newNoteSet.size() );
@ -360,7 +375,9 @@ void Event::updateNotes( const StringSetMap &newNoteSetMap ) {
} else { } else {
StringSet &noteSet = noteSetMapIter->second; StringSet &noteSet = noteSetMapIter->second;
//Info( "Found note group %s, got %d strings", newNoteGroup.c_str(), newNoteSet.size() ); //Info( "Found note group %s, got %d strings", newNoteGroup.c_str(), newNoteSet.size() );
for ( StringSet::const_iterator newNoteSetIter = newNoteSet.begin(); newNoteSetIter != newNoteSet.end(); ++newNoteSetIter ) { for ( StringSet::const_iterator newNoteSetIter = newNoteSet.begin();
newNoteSetIter != newNoteSet.end();
++newNoteSetIter ) {
const std::string &newNote = *newNoteSetIter; const std::string &newNote = *newNoteSetIter;
StringSet::iterator noteSetIter = noteSet.find(newNote); StringSet::iterator noteSetIter = noteSet.find(newNote);
if ( noteSetIter == noteSet.end() ) { if ( noteSetIter == noteSet.end() ) {
@ -387,7 +404,7 @@ void Event::updateNotes( const StringSetMap &newNoteSetMap ) {
unsigned long notesLen = 0; unsigned long notesLen = 0;
if ( !stmt ) { if ( !stmt ) {
const char *sql = "update Events set Notes = ? where Id = ?"; const char *sql = "UPDATE `Events` SET `Notes` = ? WHERE `Id` = ?";
stmt = mysql_stmt_init(&dbconn); stmt = mysql_stmt_init(&dbconn);
if ( mysql_stmt_prepare(stmt, sql, strlen(sql)) ) { if ( mysql_stmt_prepare(stmt, sql, strlen(sql)) ) {
@ -396,7 +413,7 @@ void Event::updateNotes( const StringSetMap &newNoteSetMap ) {
/* Get the parameter count from the statement */ /* Get the parameter count from the statement */
if ( mysql_stmt_param_count(stmt) != 2 ) { if ( mysql_stmt_param_count(stmt) != 2 ) {
Fatal( "Unexpected parameter count %ld in sql '%s'", mysql_stmt_param_count( stmt ), sql ); Error("Unexpected parameter count %ld in sql '%s'", mysql_stmt_param_count(stmt), sql);
} }
MYSQL_BIND bind[2]; MYSQL_BIND bind[2];
@ -416,21 +433,21 @@ void Event::updateNotes( const StringSetMap &newNoteSetMap ) {
/* Bind the buffers */ /* Bind the buffers */
if ( mysql_stmt_bind_param(stmt, bind) ) { if ( mysql_stmt_bind_param(stmt, bind) ) {
Fatal( "Unable to bind sql '%s': %s", sql, mysql_stmt_error(stmt) ); Error("Unable to bind sql '%s': %s", sql, mysql_stmt_error(stmt));
} }
} }
strncpy(notesStr, notes.c_str(), sizeof(notesStr)); strncpy(notesStr, notes.c_str(), sizeof(notesStr));
if ( mysql_stmt_execute(stmt) ) { if ( mysql_stmt_execute(stmt) ) {
Fatal( "Unable to execute sql '%s': %s", sql, mysql_stmt_error(stmt) ); Error("Unable to execute sql '%s': %s", sql, mysql_stmt_error(stmt));
} }
#else #else
static char escapedNotes[ZM_SQL_MED_BUFSIZ]; static char escapedNotes[ZM_SQL_MED_BUFSIZ];
mysql_real_escape_string(&dbconn, escapedNotes, notes.c_str(), notes.length()); mysql_real_escape_string(&dbconn, escapedNotes, notes.c_str(), notes.length());
snprintf(sql, sizeof(sql), "UPDATE Events SET Notes = '%s' WHERE Id = %" PRIu64, escapedNotes, id); snprintf(sql, sizeof(sql), "UPDATE `Events` SET `Notes` = '%s' WHERE `Id` = %" PRIu64, escapedNotes, id);
db_mutex.lock(); db_mutex.lock();
if ( mysql_query(&dbconn, sql) ) { if ( mysql_query(&dbconn, sql) ) {
Error("Can't insert event: %s", mysql_error(&dbconn)); Error("Can't insert event: %s", mysql_error(&dbconn));
@ -438,7 +455,7 @@ void Event::updateNotes( const StringSetMap &newNoteSetMap ) {
db_mutex.unlock(); db_mutex.unlock();
#endif #endif
} // end if update } // end if update
} } // void Event::updateNotes(const StringSetMap &newNoteSetMap)
void Event::AddFrames(int n_frames, Image **images, struct timeval **timestamps) { void Event::AddFrames(int n_frames, Image **images, struct timeval **timestamps) {
for (int i = 0; i < n_frames; i += ZM_SQL_BATCH_SIZE) { for (int i = 0; i < n_frames; i += ZM_SQL_BATCH_SIZE) {
@ -448,7 +465,7 @@ void Event::AddFrames( int n_frames, Image **images, struct timeval **timestamps
void Event::AddFramesInternal(int n_frames, int start_frame, Image **images, struct timeval **timestamps) { void Event::AddFramesInternal(int n_frames, int start_frame, Image **images, struct timeval **timestamps) {
static char sql[ZM_SQL_LGE_BUFSIZ]; static char sql[ZM_SQL_LGE_BUFSIZ];
strncpy(sql, "INSERT INTO Frames ( EventId, FrameId, TimeStamp, Delta ) VALUES ", sizeof(sql)); strncpy(sql, "INSERT INTO `Frames` (`EventId`, `FrameId`, `TimeStamp`, `Delta`) VALUES ", sizeof(sql));
int frameCount = 0; int frameCount = 0;
for ( int i = start_frame; i < n_frames && i - start_frame < ZM_SQL_BATCH_SIZE; i++ ) { for ( int i = start_frame; i < n_frames && i - start_frame < ZM_SQL_BATCH_SIZE; i++ ) {
if ( timestamps[i]->tv_sec <= 0 ) { if ( timestamps[i]->tv_sec <= 0 ) {
@ -463,7 +480,7 @@ void Event::AddFramesInternal( int n_frames, int start_frame, Image **images, st
snprintf(event_file, sizeof(event_file), staticConfig.capture_file_format, path, frames); snprintf(event_file, sizeof(event_file), staticConfig.capture_file_format, path, frames);
Debug(1, "Writing pre-capture frame %d", frames); Debug(1, "Writing pre-capture frame %d", frames);
WriteFrameImage(images[i], *(timestamps[i]), event_file); WriteFrameImage(images[i], *(timestamps[i]), event_file);
} else { }
//If this is the first frame, we should add a thumbnail to the event directory //If this is the first frame, we should add a thumbnail to the event directory
// ICON: We are working through the pre-event frames so this snapshot won't // ICON: We are working through the pre-event frames so this snapshot won't
// neccessarily be of the motion. But some events are less than 10 frames, // neccessarily be of the motion. But some events are less than 10 frames,
@ -471,7 +488,7 @@ void Event::AddFramesInternal( int n_frames, int start_frame, Image **images, st
if ( frames == 1 ) { if ( frames == 1 ) {
WriteFrameImage(images[i], *(timestamps[i]), snapshot_file); WriteFrameImage(images[i], *(timestamps[i]), snapshot_file);
} }
}
if ( videowriter != NULL ) { if ( videowriter != NULL ) {
WriteFrameVideo(images[i], *(timestamps[i]), videowriter); WriteFrameVideo(images[i], *(timestamps[i]), videowriter);
} }
@ -481,7 +498,10 @@ void Event::AddFramesInternal( int n_frames, int start_frame, Image **images, st
// Delta is Decimal(8,2) so 6 integer digits and 2 decimal digits // Delta is Decimal(8,2) so 6 integer digits and 2 decimal digits
if ( delta_time.sec > 999999 ) { if ( delta_time.sec > 999999 ) {
Warning("Invalid delta_time from_unixtime(%ld), %s%ld.%02ld", Warning("Invalid delta_time from_unixtime(%ld), %s%ld.%02ld",
timestamps[i]->tv_sec, delta_time.positive?"":"-", delta_time.sec, delta_time.fsec ); timestamps[i]->tv_sec,
(delta_time.positive?"":"-"),
delta_time.sec,
delta_time.fsec);
delta_time.sec = 0; delta_time.sec = 0;
} }
@ -504,7 +524,7 @@ void Event::AddFramesInternal( int n_frames, int start_frame, Image **images, st
} else { } else {
Debug(1, "No valid pre-capture frames to add"); Debug(1, "No valid pre-capture frames to add");
} }
} } // void Event::AddFramesInternal(int n_frames, int start_frame, Image **images, struct timeval **timestamps)
void Event::WriteDbFrames() { void Event::WriteDbFrames() {
static char sql[ZM_SQL_LGE_BUFSIZ]; static char sql[ZM_SQL_LGE_BUFSIZ];
@ -542,29 +562,52 @@ void Event::AddFrame(Image *image, struct timeval timestamp, int score, Image *a
frames++; frames++;
bool write_to_db = false; bool write_to_db = false;
FrameType frame_type = score>0?ALARM:(score<0?BULK:NORMAL);
// < 0 means no motion detection is being done.
if ( score < 0 )
score = 0;
if ( monitor->GetOptSaveJPEGs() & 1 ) { if ( monitor->GetOptSaveJPEGs() & 1 ) {
if ( frames == 1 )
write_to_db = true; // web ui might show this as thumbnail, so db needs to know about it.
static char event_file[PATH_MAX]; static char event_file[PATH_MAX];
snprintf(event_file, sizeof(event_file), staticConfig.capture_file_format, path, frames); snprintf(event_file, sizeof(event_file), staticConfig.capture_file_format, path, frames);
Debug(1, "Writing capture frame %d to %s", frames, event_file); Debug(1, "Writing capture frame %d to %s", frames, event_file);
if ( !WriteFrameImage(image, timestamp, event_file) ) { if ( !WriteFrameImage(image, timestamp, event_file) ) {
Error("Failed to write frame image"); Error("Failed to write frame image");
} }
} else { }
// If this is the first frame, we should add a thumbnail to the event directory // If this is the first frame, we should add a thumbnail to the event directory
if ( (frames == 1) || (score > (int)max_score) ) { if ( (frames == 1) || (score > (int)max_score) ) {
write_to_db = true; // web ui might show this as thumbnail, so db needs to know about it. write_to_db = true; // web ui might show this as thumbnail, so db needs to know about it.
WriteFrameImage(image, timestamp, snapshot_file); WriteFrameImage(image, timestamp, snapshot_file);
} }
// We are writing an Alarm frame
if ( frame_type == ALARM ) {
// The first frame with a score will be the frame that alarmed the event // The first frame with a score will be the frame that alarmed the event
if ( (!alarm_frame_written) && (score > 0) ) { if ( !alarm_frame_written ) {
write_to_db = true; // OD processing will need it, so the db needs to know about it write_to_db = true; // OD processing will need it, so the db needs to know about it
alarm_frame_written = true; alarm_frame_written = true;
WriteFrameImage(image, timestamp, alarm_file); WriteFrameImage(image, timestamp, alarm_file);
} }
alarm_frames++;
tot_score += score;
if ( score > (int)max_score )
max_score = score;
if ( alarm_image ) {
if ( monitor->GetOptSaveJPEGs() & 2 ) {
static char event_file[PATH_MAX];
snprintf(event_file, sizeof(event_file), staticConfig.analyse_file_format, path, frames);
Debug(1, "Writing analysis frame %d", frames);
if ( ! WriteFrameImage(alarm_image, timestamp, event_file, true) ) {
Error("Failed to write analysis frame image");
} }
}
}
} // end if frame_type == ALARM
if ( videowriter != NULL ) { if ( videowriter != NULL ) {
WriteFrameVideo(image, timestamp, videowriter); WriteFrameVideo(image, timestamp, videowriter);
} }
@ -572,11 +615,6 @@ void Event::AddFrame(Image *image, struct timeval timestamp, int score, Image *a
struct DeltaTimeval delta_time; struct DeltaTimeval delta_time;
DELTA_TIMEVAL(delta_time, timestamp, start_time, DT_PREC_2); DELTA_TIMEVAL(delta_time, timestamp, start_time, DT_PREC_2);
FrameType frame_type = score>0?ALARM:(score<0?BULK:NORMAL);
// < 0 means no motion detection is being done.
if ( score < 0 )
score = 0;
bool db_frame = ( frame_type != BULK ) || (frames==1) || ((frames%config.bulk_frame_interval)==0) ; bool db_frame = ( frame_type != BULK ) || (frames==1) || ((frames%config.bulk_frame_interval)==0) ;
if ( db_frame ) { if ( db_frame ) {
static char sql[ZM_SQL_MED_BUFSIZ]; static char sql[ZM_SQL_MED_BUFSIZ];
@ -613,57 +651,4 @@ void Event::AddFrame(Image *image, struct timeval timestamp, int score, Image *a
} // end if db_frame } // end if db_frame
end_time = timestamp; end_time = timestamp;
} // end void Event::AddFrame(Image *image, struct timeval timestamp, int score, Image *alarm_image)
// We are writing an Alarm frame
if ( frame_type == ALARM ) {
alarm_frames++;
tot_score += score;
if ( score > (int)max_score )
max_score = score;
if ( alarm_image ) {
if ( monitor->GetOptSaveJPEGs() & 2 ) {
static char event_file[PATH_MAX];
snprintf(event_file, sizeof(event_file), staticConfig.analyse_file_format, path, frames);
Debug(1, "Writing analysis frame %d", frames);
if ( ! WriteFrameImage(alarm_image, timestamp, event_file, true) ) {
Error("Failed to write analysis frame image");
}
}
}
} // end if frame_type == ALARM
/* This makes viewing the diagnostic images impossible because it keeps deleting them
if ( config.record_diag_images ) {
char diag_glob[PATH_MAX] = "";
snprintf( diag_glob, sizeof(diag_glob), "%s/%d/diag-*.jpg", staticConfig.DIR_EVENTS.c_str(), monitor->Id() );
glob_t pglob;
int glob_status = glob( diag_glob, 0, 0, &pglob );
if ( glob_status != 0 ) {
if ( glob_status < 0 ) {
Error( "Can't glob '%s': %s", diag_glob, strerror(errno) );
} else {
Debug( 1, "Can't glob '%s': %d", diag_glob, glob_status );
}
} else {
char new_diag_path[PATH_MAX] = "";
for ( int i = 0; i < pglob.gl_pathc; i++ ) {
char *diag_path = pglob.gl_pathv[i];
char *diag_file = strstr( diag_path, "diag-" );
if ( diag_file ) {
snprintf( new_diag_path, sizeof(new_diag_path), general_file_format, path, frames, diag_file );
if ( rename( diag_path, new_diag_path ) < 0 ) {
Error( "Can't rename '%s' to '%s': %s", diag_path, new_diag_path, strerror(errno) );
}
}
}
}
globfree( &pglob );
}
*/
}

View File

@ -85,6 +85,9 @@ if ( empty($_REQUEST['path']) ) {
$Frame = new ZM\Frame(); $Frame = new ZM\Frame();
$Frame->Id('objdetect'); $Frame->Id('objdetect');
} else if ( $_REQUEST['fid'] == 'alarm' ) { } else if ( $_REQUEST['fid'] == 'alarm' ) {
$path = $Event->Path().'/alarm.jpg';
if ( !file_exists($path) ) {
# legacy support
# look for first alarmed frame # look for first alarmed frame
$Frame = ZM\Frame::find_one( $Frame = ZM\Frame::find_one(
array('EventId'=>$_REQUEST['eid'], 'Type'=>'Alarm'), array('EventId'=>$_REQUEST['eid'], 'Type'=>'Alarm'),
@ -103,9 +106,18 @@ if ( empty($_REQUEST['path']) ) {
# If we store Frames as jpgs, then we don't store an alarmed snapshot # If we store Frames as jpgs, then we don't store an alarmed snapshot
$path = $Event->Path().'/'.sprintf('%0'.ZM_EVENT_IMAGE_DIGITS.'d', $Frame->FrameId()).'-'.$show.'.jpg'; $path = $Event->Path().'/'.sprintf('%0'.ZM_EVENT_IMAGE_DIGITS.'d', $Frame->FrameId()).'-'.$show.'.jpg';
} else { } else {
$path = $Event->Path().'/alarm.jpg'; header('HTTP/1.0 404 Not Found');
ZM\Fatal('No alarm jpg found for event '.$_REQUEST['eid']);
return;
} }
} else {
$Frame = new ZM\Frame();
$Frame->Delta(1);
$Frame->FrameId('alarm');
} # alarm.jpg found
} else if ( $_REQUEST['fid'] == 'snapshot' ) { } else if ( $_REQUEST['fid'] == 'snapshot' ) {
$path = $Event->Path().'/snapshot.jpg';
if ( !file_exists($path) ) {
$Frame = ZM\Frame::find_one(array('EventId'=>$_REQUEST['eid'], 'Score'=>$Event->MaxScore())); $Frame = ZM\Frame::find_one(array('EventId'=>$_REQUEST['eid'], 'Score'=>$Event->MaxScore()));
if ( !$Frame ) if ( !$Frame )
$Frame = ZM\Frame::find_one(array('EventId'=>$_REQUEST['eid'])); $Frame = ZM\Frame::find_one(array('EventId'=>$_REQUEST['eid']));
@ -124,8 +136,15 @@ if ( empty($_REQUEST['path']) ) {
# If we store Frames as jpgs, then we don't store a snapshot # If we store Frames as jpgs, then we don't store a snapshot
$path = $Event->Path().'/'.sprintf('%0'.ZM_EVENT_IMAGE_DIGITS.'d', $Frame->FrameId()).'-'.$show.'.jpg'; $path = $Event->Path().'/'.sprintf('%0'.ZM_EVENT_IMAGE_DIGITS.'d', $Frame->FrameId()).'-'.$show.'.jpg';
} else { } else {
$path = $Event->Path().'/snapshot.jpg'; header('HTTP/1.0 404 Not Found');
} ZM\Fatal('No alarm jpg found for event '.$_REQUEST['eid']);
return;
} # end if stored jpgs
} else {
$Frame = new ZM\Frame();
$Frame->Delta(1);
$Frame->FrameId('snapshot');
} # end if found snapshot.jpg
} else { } else {
$Frame = ZM\Frame::find_one(array('EventId'=>$_REQUEST['eid'], 'FrameId'=>$_REQUEST['fid'])); $Frame = ZM\Frame::find_one(array('EventId'=>$_REQUEST['eid'], 'FrameId'=>$_REQUEST['fid']));