2013-03-17 07:45:21 +08:00
|
|
|
//
|
2020-12-01 22:53:51 +08:00
|
|
|
// ZoneMinder Event Class Implementation
|
2013-03-17 07:45:21 +08:00
|
|
|
// Copyright (C) 2001-2008 Philip Coombes
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License
|
|
|
|
// as published by the Free Software Foundation; either version 2
|
|
|
|
// of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
2016-12-26 23:23:16 +08:00
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2013-03-17 07:45:21 +08:00
|
|
|
//
|
|
|
|
|
2021-02-04 11:47:28 +08:00
|
|
|
#include "zm_event.h"
|
2013-03-17 07:45:21 +08:00
|
|
|
|
2021-02-04 11:47:28 +08:00
|
|
|
#include "zm_camera.h"
|
2013-03-17 07:45:21 +08:00
|
|
|
#include "zm_db.h"
|
2021-02-04 11:47:28 +08:00
|
|
|
#include "zm_frame.h"
|
|
|
|
#include "zm_logger.h"
|
2013-03-17 07:45:21 +08:00
|
|
|
#include "zm_monitor.h"
|
2021-02-04 11:47:28 +08:00
|
|
|
#include "zm_signal.h"
|
|
|
|
#include "zm_videostore.h"
|
|
|
|
#include <cstring>
|
|
|
|
#include <sys/stat.h>
|
2021-03-04 06:48:02 +08:00
|
|
|
#include <unistd.h>
|
2013-03-17 07:45:21 +08:00
|
|
|
|
|
|
|
//#define USE_PREPARED_SQL 1
|
|
|
|
|
2017-04-13 21:47:19 +08:00
|
|
|
const char * Event::frame_type_names[3] = { "Normal", "Bulk", "Alarm" };
|
2020-11-12 03:03:08 +08:00
|
|
|
#define MAX_DB_FRAMES 120
|
2020-10-05 21:11:16 +08:00
|
|
|
char frame_insert_sql[ZM_SQL_LGE_BUFSIZ] = "INSERT INTO `Frames` (`EventId`, `FrameId`, `Type`, `TimeStamp`, `Delta`, `Score`) VALUES ";
|
2013-03-17 07:45:21 +08:00
|
|
|
|
|
|
|
int Event::pre_alarm_count = 0;
|
2017-02-05 00:20:21 +08:00
|
|
|
|
2021-02-27 05:28:03 +08:00
|
|
|
Event::PreAlarmData Event::pre_alarm_data[MAX_PRE_ALARM_FRAMES] = {};
|
2013-03-17 07:45:21 +08:00
|
|
|
|
2018-01-27 01:21:12 +08:00
|
|
|
Event::Event(
|
|
|
|
Monitor *p_monitor,
|
|
|
|
struct timeval p_start_time,
|
|
|
|
const std::string &p_cause,
|
2018-01-30 00:52:17 +08:00
|
|
|
const StringSetMap &p_noteSetMap
|
2018-01-27 02:02:16 +08:00
|
|
|
) :
|
2020-11-30 05:25:33 +08:00
|
|
|
id(0),
|
2018-04-14 23:03:08 +08:00
|
|
|
monitor(p_monitor),
|
|
|
|
start_time(p_start_time),
|
2020-12-31 10:21:59 +08:00
|
|
|
end_time({0,0}),
|
2018-04-14 23:03:08 +08:00
|
|
|
cause(p_cause),
|
2020-12-31 10:21:59 +08:00
|
|
|
noteSetMap(p_noteSetMap),
|
|
|
|
frames(0),
|
|
|
|
alarm_frames(0),
|
|
|
|
alarm_frame_written(false),
|
|
|
|
tot_score(0),
|
|
|
|
max_score(0),
|
|
|
|
//path(""),
|
|
|
|
//snapshit_file(),
|
|
|
|
//alarm_file(""),
|
|
|
|
videoStore(nullptr),
|
|
|
|
//video_name(""),
|
|
|
|
//video_file(""),
|
|
|
|
last_db_frame(0),
|
|
|
|
have_video_keyframe(false),
|
|
|
|
//scheme
|
|
|
|
save_jpegs(0)
|
2013-03-17 07:45:21 +08:00
|
|
|
{
|
2016-04-04 22:11:48 +08:00
|
|
|
std::string notes;
|
2018-01-27 01:21:12 +08:00
|
|
|
createNotes(notes);
|
2016-04-04 22:11:48 +08:00
|
|
|
|
2021-03-07 23:35:05 +08:00
|
|
|
timeval now = {};
|
|
|
|
gettimeofday(&now, nullptr);
|
2018-02-01 03:33:20 +08:00
|
|
|
|
2016-06-22 01:48:32 +08:00
|
|
|
if ( !start_time.tv_sec ) {
|
2021-01-11 23:06:54 +08:00
|
|
|
Warning("Event has zero time, setting to now");
|
2018-02-01 03:33:20 +08:00
|
|
|
start_time = now;
|
|
|
|
} else if ( start_time.tv_sec > now.tv_sec ) {
|
2021-01-16 00:53:25 +08:00
|
|
|
char buffer[26];
|
|
|
|
char buffer_now[26];
|
2021-03-07 23:35:05 +08:00
|
|
|
tm tm_info = {};
|
2021-01-16 00:53:25 +08:00
|
|
|
|
2021-03-07 23:35:05 +08:00
|
|
|
localtime_r(&start_time.tv_sec, &tm_info);
|
|
|
|
strftime(buffer, 26, "%Y:%m:%d %H:%M:%S", &tm_info);
|
|
|
|
localtime_r(&now.tv_sec, &tm_info);
|
|
|
|
strftime(buffer_now, 26, "%Y:%m:%d %H:%M:%S", &tm_info);
|
2021-01-16 00:53:25 +08:00
|
|
|
|
2018-05-24 22:59:26 +08:00
|
|
|
Error(
|
2021-03-29 21:58:09 +08:00
|
|
|
"StartDateTime in the future starttime %ld.%06ld >? now %ld.%06ld difference %d\nstarttime: %s\nnow: %s",
|
2021-01-16 00:53:25 +08:00
|
|
|
start_time.tv_sec, start_time.tv_usec, now.tv_sec, now.tv_usec,
|
|
|
|
(now.tv_sec-start_time.tv_sec),
|
|
|
|
buffer, buffer_now
|
2018-04-30 22:12:34 +08:00
|
|
|
);
|
2018-02-01 03:33:20 +08:00
|
|
|
start_time = now;
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
|
|
|
|
2017-01-15 05:55:28 +08:00
|
|
|
unsigned int state_id = 0;
|
2021-04-20 22:55:39 +08:00
|
|
|
{
|
|
|
|
zmDbRow dbrow;
|
|
|
|
if (dbrow.fetch("SELECT Id FROM States WHERE IsActive=1")) {
|
|
|
|
state_id = atoi(dbrow[0]);
|
|
|
|
}
|
2017-01-15 05:55:28 +08:00
|
|
|
}
|
2016-04-29 21:11:14 +08:00
|
|
|
|
2018-05-20 22:39:14 +08:00
|
|
|
// Copy it in case opening the mp4 doesn't work we can set it to another value
|
|
|
|
save_jpegs = monitor->GetOptSaveJPEGs();
|
2020-11-28 02:28:38 +08:00
|
|
|
Storage * storage = monitor->getStorage();
|
2018-05-20 22:39:14 +08:00
|
|
|
|
2021-02-19 05:01:45 +08:00
|
|
|
std::string sql = stringtf(
|
2019-08-23 00:57:00 +08:00
|
|
|
"INSERT INTO `Events` "
|
2020-12-08 05:26:26 +08:00
|
|
|
"( `MonitorId`, `StorageId`, `Name`, `StartDateTime`, `Width`, `Height`, `Cause`, `Notes`, `StateId`, `Orientation`, `Videoed`, `DefaultVideo`, `SaveJPEGs`, `Scheme` )"
|
2018-08-11 22:08:30 +08:00
|
|
|
" VALUES "
|
2020-07-23 01:14:40 +08:00
|
|
|
"( %d, %d, 'New Event', from_unixtime( %ld ), %d, %d, '%s', '%s', %d, %d, %d, '%s', %d, '%s' )",
|
2017-01-15 06:07:20 +08:00
|
|
|
monitor->Id(),
|
|
|
|
storage->Id(),
|
|
|
|
start_time.tv_sec,
|
|
|
|
monitor->Width(),
|
|
|
|
monitor->Height(),
|
|
|
|
cause.c_str(),
|
|
|
|
notes.c_str(),
|
|
|
|
state_id,
|
|
|
|
monitor->getOrientation(),
|
2021-02-19 02:30:05 +08:00
|
|
|
0,
|
|
|
|
"",
|
|
|
|
save_jpegs,
|
2017-12-19 02:25:24 +08:00
|
|
|
storage->SchemeString().c_str()
|
2017-01-15 06:07:20 +08:00
|
|
|
);
|
2018-02-16 04:54:13 +08:00
|
|
|
|
2021-02-19 05:01:45 +08:00
|
|
|
id = zmDbDoInsert(sql.c_str());
|
2020-03-14 02:47:22 +08:00
|
|
|
|
2020-11-28 02:28:38 +08:00
|
|
|
if ( !SetPath(storage) ) {
|
|
|
|
// Try another
|
|
|
|
Warning("Failed creating event dir at %s", storage->Path());
|
|
|
|
|
2021-02-19 05:01:45 +08:00
|
|
|
sql = stringtf("SELECT `Id` FROM `Storage` WHERE `Id` != %u", storage->Id());
|
2020-11-28 02:28:38 +08:00
|
|
|
if ( monitor->ServerId() )
|
|
|
|
sql += stringtf(" AND ServerId=%u", monitor->ServerId());
|
|
|
|
|
|
|
|
Debug(1, "%s", sql.c_str());
|
|
|
|
storage = nullptr;
|
|
|
|
|
|
|
|
MYSQL_RES *result = zmDbFetch(sql.c_str());
|
|
|
|
if ( result ) {
|
|
|
|
for ( int i = 0; MYSQL_ROW dbrow = mysql_fetch_row(result); i++ ) {
|
|
|
|
storage = new Storage(atoi(dbrow[0]));
|
|
|
|
if ( SetPath(storage) )
|
|
|
|
break;
|
|
|
|
delete storage;
|
|
|
|
storage = nullptr;
|
|
|
|
} // end foreach row of Storage
|
|
|
|
mysql_free_result(result);
|
|
|
|
result = nullptr;
|
|
|
|
}
|
|
|
|
if ( !storage ) {
|
|
|
|
Info("No valid local storage area found. Trying all other areas.");
|
|
|
|
// Try remote
|
2020-11-30 05:25:33 +08:00
|
|
|
sql = "SELECT `Id` FROM `Storage` WHERE ServerId IS NULL";
|
2020-11-28 02:28:38 +08:00
|
|
|
if ( monitor->ServerId() )
|
|
|
|
sql += stringtf(" OR ServerId != %u", monitor->ServerId());
|
|
|
|
|
|
|
|
MYSQL_RES *result = zmDbFetch(sql.c_str());
|
|
|
|
if ( result ) {
|
|
|
|
for ( int i = 0; MYSQL_ROW dbrow = mysql_fetch_row(result); i++ ) {
|
|
|
|
storage = new Storage(atoi(dbrow[0]));
|
|
|
|
if ( SetPath(storage) )
|
|
|
|
break;
|
|
|
|
delete storage;
|
|
|
|
storage = nullptr;
|
|
|
|
} // end foreach row of Storage
|
|
|
|
mysql_free_result(result);
|
|
|
|
result = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( !storage ) {
|
|
|
|
storage = new Storage();
|
|
|
|
Warning("Failed to find a storage area to save events.");
|
|
|
|
}
|
2020-11-30 05:25:33 +08:00
|
|
|
sql = stringtf("UPDATE Events SET StorageId = '%d' WHERE Id=%" PRIu64, storage->Id(), id);
|
2021-02-19 05:01:45 +08:00
|
|
|
zmDbDo(sql.c_str());
|
|
|
|
} // end if ! setPath(Storage)
|
2020-11-30 05:25:33 +08:00
|
|
|
Debug(1, "Using storage area at %s", path.c_str());
|
2020-03-14 02:47:22 +08:00
|
|
|
|
2020-07-22 04:10:05 +08:00
|
|
|
video_name = "";
|
2013-12-20 00:38:07 +08:00
|
|
|
|
2019-04-15 22:47:26 +08:00
|
|
|
snapshot_file = path + "/snapshot.jpg";
|
|
|
|
alarm_file = path + "/alarm.jpg";
|
2019-04-05 00:28:08 +08:00
|
|
|
|
2016-04-29 21:11:14 +08:00
|
|
|
/* Save as video */
|
2015-01-12 18:42:17 +08:00
|
|
|
|
2016-04-29 21:11:14 +08:00
|
|
|
if ( monitor->GetOptVideoWriter() != 0 ) {
|
2017-11-14 14:59:15 +08:00
|
|
|
std::string container = monitor->OutputContainer();
|
|
|
|
if ( container == "auto" || container == "" ) {
|
2021-02-14 22:49:30 +08:00
|
|
|
container = "mp4";
|
2016-04-29 21:11:14 +08:00
|
|
|
}
|
2017-11-14 14:59:15 +08:00
|
|
|
|
2020-07-23 01:14:40 +08:00
|
|
|
video_name = stringtf("%" PRIu64 "-%s.%s", id, "video", container.c_str());
|
2020-07-22 04:34:46 +08:00
|
|
|
video_file = path + "/" + video_name;
|
2020-09-29 23:02:40 +08:00
|
|
|
Debug(1, "Writing video file to %s", video_file.c_str());
|
2017-11-13 23:17:46 +08:00
|
|
|
videoStore = new VideoStore(
|
2020-07-23 01:14:40 +08:00
|
|
|
video_file.c_str(),
|
2017-11-14 15:39:58 +08:00
|
|
|
container.c_str(),
|
2021-02-11 05:52:50 +08:00
|
|
|
monitor->GetVideoStream(),
|
|
|
|
monitor->GetVideoCodecContext(),
|
|
|
|
( monitor->RecordAudio() ? monitor->GetAudioStream() : nullptr ),
|
|
|
|
( monitor->RecordAudio() ? monitor->GetAudioCodecContext() : nullptr ),
|
2017-11-13 23:17:46 +08:00
|
|
|
monitor );
|
|
|
|
|
2020-07-23 01:14:40 +08:00
|
|
|
if ( !videoStore->open() ) {
|
2021-02-19 02:30:05 +08:00
|
|
|
Warning("Failed to open videostore, turning on jpegs");
|
2017-11-13 23:17:46 +08:00
|
|
|
delete videoStore;
|
2020-09-29 23:02:40 +08:00
|
|
|
videoStore = nullptr;
|
2021-02-19 02:30:05 +08:00
|
|
|
if ( ! ( save_jpegs & 1 ) ) {
|
|
|
|
save_jpegs |= 1; // Turn on jpeg storage
|
2021-02-19 05:01:45 +08:00
|
|
|
sql = stringtf("UPDATE Events SET SaveJpegs=%d WHERE Id=%" PRIu64, save_jpegs, id);
|
|
|
|
zmDbDo(sql.c_str());
|
2021-02-19 02:30:05 +08:00
|
|
|
}
|
|
|
|
} else {
|
2021-02-19 05:01:45 +08:00
|
|
|
sql = stringtf("UPDATE Events SET Videoed=1, DefaultVideo = '%s' WHERE Id=%" PRIu64, video_name.c_str(), id);
|
|
|
|
zmDbDo(sql.c_str());
|
2018-04-14 23:03:08 +08:00
|
|
|
}
|
2020-12-31 10:21:59 +08:00
|
|
|
} // end if GetOptVideoWriter
|
2017-01-17 10:11:28 +08:00
|
|
|
} // Event::Event( Monitor *p_monitor, struct timeval p_start_time, const std::string &p_cause, const StringSetMap &p_noteSetMap, bool p_videoEvent )
|
2013-03-17 07:45:21 +08:00
|
|
|
|
2016-06-22 01:48:32 +08:00
|
|
|
Event::~Event() {
|
2018-04-12 23:39:30 +08:00
|
|
|
// We close the videowriter first, because if we finish the event, we might try to view the file, but we aren't done writing it yet.
|
2016-04-04 22:11:48 +08:00
|
|
|
|
2016-04-29 21:11:14 +08:00
|
|
|
/* Close the video file */
|
2020-09-29 23:02:40 +08:00
|
|
|
if ( videoStore != nullptr ) {
|
2021-02-05 09:39:48 +08:00
|
|
|
Debug(4, "Deleting video store");
|
2018-04-14 23:03:08 +08:00
|
|
|
delete videoStore;
|
2020-09-29 23:02:40 +08:00
|
|
|
videoStore = nullptr;
|
2016-04-29 21:11:14 +08:00
|
|
|
}
|
2013-12-20 00:38:07 +08:00
|
|
|
|
2020-11-03 05:34:00 +08:00
|
|
|
// endtime is set in AddFrame, so SHOULD be set to the value of the last frame timestamp.
|
2020-11-12 01:43:41 +08:00
|
|
|
if ( !end_time.tv_sec ) {
|
2020-11-03 05:34:00 +08:00
|
|
|
Warning("Empty endtime for event. Should not happen. Setting to now.");
|
|
|
|
gettimeofday(&end_time, nullptr);
|
|
|
|
}
|
2016-09-27 21:47:19 +08:00
|
|
|
struct DeltaTimeval delta_time;
|
2018-01-27 01:21:12 +08:00
|
|
|
DELTA_TIMEVAL(delta_time, end_time, start_time, DT_PREC_2);
|
2018-04-12 23:39:30 +08:00
|
|
|
Debug(2, "start_time:%d.%d end_time%d.%d", start_time.tv_sec, start_time.tv_usec, end_time.tv_sec, end_time.tv_usec);
|
2016-09-27 21:47:19 +08:00
|
|
|
|
2021-03-12 02:48:42 +08:00
|
|
|
if (frame_data.size()) WriteDbFrames();
|
2016-04-04 22:11:48 +08:00
|
|
|
|
2019-02-20 01:00:17 +08:00
|
|
|
// Should not be static because we might be multi-threaded
|
2019-04-05 00:55:35 +08:00
|
|
|
char sql[ZM_SQL_LGE_BUFSIZ];
|
2020-09-26 03:29:31 +08:00
|
|
|
snprintf(sql, sizeof(sql),
|
2020-11-05 02:49:47 +08:00
|
|
|
"UPDATE Events SET Name='%s%" PRIu64 "', EndDateTime = from_unixtime(%ld), Length = %s%ld.%02ld, Frames = %d, AlarmFrames = %d, TotScore = %d, AvgScore = %d, MaxScore = %d WHERE Id = %" PRIu64 " AND Name='New Event'",
|
2018-12-06 02:18:21 +08:00
|
|
|
monitor->EventPrefix(), id, end_time.tv_sec,
|
|
|
|
delta_time.positive?"":"-", delta_time.sec, delta_time.fsec,
|
|
|
|
frames, alarm_frames,
|
|
|
|
tot_score, (int)(alarm_frames?(tot_score/alarm_frames):0), max_score,
|
2020-07-22 04:10:05 +08:00
|
|
|
id);
|
2021-03-12 02:48:42 +08:00
|
|
|
if (!zmDbDoUpdate(sql)) {
|
|
|
|
// Name might have been changed during recording, so just do the update without changing the name.
|
|
|
|
snprintf(sql, sizeof(sql),
|
|
|
|
"UPDATE Events SET EndDateTime = from_unixtime(%ld), Length = %s%ld.%02ld, Frames = %d, AlarmFrames = %d, TotScore = %d, AvgScore = %d, MaxScore = %d WHERE Id = %" PRIu64,
|
|
|
|
end_time.tv_sec,
|
|
|
|
delta_time.positive?"":"-", delta_time.sec, delta_time.fsec,
|
|
|
|
frames, alarm_frames,
|
|
|
|
tot_score, (int)(alarm_frames?(tot_score/alarm_frames):0), max_score,
|
|
|
|
id);
|
|
|
|
zmDbDoUpdate(sql);
|
|
|
|
} // end if no changed rows due to Name change during recording
|
2019-10-31 04:25:09 +08:00
|
|
|
} // Event::~Event()
|
2013-03-17 07:45:21 +08:00
|
|
|
|
2018-04-12 23:39:30 +08:00
|
|
|
void Event::createNotes(std::string ¬es) {
|
2021-03-05 00:12:27 +08:00
|
|
|
notes.clear();
|
|
|
|
for ( StringSetMap::const_iterator mapIter = noteSetMap.begin(); mapIter != noteSetMap.end(); ++mapIter ) {
|
|
|
|
notes += mapIter->first;
|
|
|
|
notes += ": ";
|
|
|
|
const StringSet &stringSet = mapIter->second;
|
|
|
|
for ( StringSet::const_iterator setIter = stringSet.begin(); setIter != stringSet.end(); ++setIter ) {
|
|
|
|
if ( setIter != stringSet.begin() )
|
|
|
|
notes += ", ";
|
|
|
|
notes += *setIter;
|
2013-03-17 07:45:21 +08:00
|
|
|
}
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
2019-10-31 04:25:09 +08:00
|
|
|
} // void Event::createNotes(std::string ¬es)
|
2013-03-17 07:45:21 +08:00
|
|
|
|
2019-10-31 04:25:09 +08:00
|
|
|
bool Event::WriteFrameImage(
|
|
|
|
Image *image,
|
|
|
|
struct timeval timestamp,
|
|
|
|
const char *event_file,
|
2020-11-02 07:48:13 +08:00
|
|
|
bool alarm_frame) const {
|
2013-03-17 07:45:21 +08:00
|
|
|
|
2019-10-31 04:25:09 +08:00
|
|
|
int thisquality =
|
|
|
|
(alarm_frame && (config.jpeg_alarm_file_quality > config.jpeg_file_quality)) ?
|
2020-07-22 04:10:05 +08:00
|
|
|
config.jpeg_alarm_file_quality : 0; // quality to use, zero is default
|
2016-04-04 22:11:48 +08:00
|
|
|
|
2017-09-26 04:22:41 +08:00
|
|
|
bool rc;
|
2017-01-17 01:56:09 +08:00
|
|
|
|
2017-09-26 04:22:41 +08:00
|
|
|
if ( !config.timestamp_on_capture ) {
|
|
|
|
// stash the image we plan to use in another pointer regardless if timestamped.
|
2019-10-31 04:25:09 +08:00
|
|
|
// exif is only timestamp at present this switches on or off for write
|
2017-09-26 04:22:41 +08:00
|
|
|
Image *ts_image = new Image(*image);
|
2018-05-04 01:54:05 +08:00
|
|
|
monitor->TimestampImage(ts_image, ×tamp);
|
2019-10-31 04:25:09 +08:00
|
|
|
rc = ts_image->WriteJpeg(event_file, thisquality,
|
|
|
|
(monitor->Exif() ? timestamp : (timeval){0,0}));
|
2017-09-26 04:22:41 +08:00
|
|
|
delete(ts_image);
|
|
|
|
} else {
|
2019-10-31 04:25:09 +08:00
|
|
|
rc = image->WriteJpeg(event_file, thisquality,
|
|
|
|
(monitor->Exif() ? timestamp : (timeval){0,0}));
|
2017-09-26 04:22:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
2019-12-01 03:59:39 +08:00
|
|
|
} // end Event::WriteFrameImage( Image *image, struct timeval timestamp, const char *event_file, bool alarm_frame )
|
2013-03-17 07:45:21 +08:00
|
|
|
|
2018-08-18 04:02:40 +08:00
|
|
|
bool Event::WritePacket(ZMPacket &packet) {
|
2017-11-13 23:17:46 +08:00
|
|
|
|
2018-08-18 04:02:40 +08:00
|
|
|
if ( videoStore->writePacket(&packet) < 0 )
|
2016-04-29 21:11:14 +08:00
|
|
|
return false;
|
2017-11-14 15:39:58 +08:00
|
|
|
return true;
|
2019-10-31 04:25:09 +08:00
|
|
|
} // bool Event::WriteFrameVideo
|
2013-12-20 00:38:07 +08:00
|
|
|
|
2018-08-18 04:02:40 +08:00
|
|
|
void Event::updateNotes(const StringSetMap &newNoteSetMap) {
|
2016-04-04 22:11:48 +08:00
|
|
|
bool update = false;
|
2013-03-17 07:45:21 +08:00
|
|
|
|
2016-04-04 22:11:48 +08:00
|
|
|
//Info( "Checking notes, %d <> %d", noteSetMap.size(), newNoteSetMap.size() );
|
2016-06-22 01:48:32 +08:00
|
|
|
if ( newNoteSetMap.size() > 0 ) {
|
|
|
|
if ( noteSetMap.size() == 0 ) {
|
2016-04-04 22:11:48 +08:00
|
|
|
noteSetMap = newNoteSetMap;
|
|
|
|
update = true;
|
2016-06-22 01:48:32 +08:00
|
|
|
} else {
|
2019-10-31 04:25:09 +08:00
|
|
|
for ( StringSetMap::const_iterator newNoteSetMapIter = newNoteSetMap.begin();
|
|
|
|
newNoteSetMapIter != newNoteSetMap.end();
|
|
|
|
++newNoteSetMapIter ) {
|
2016-04-04 22:11:48 +08:00
|
|
|
const std::string &newNoteGroup = newNoteSetMapIter->first;
|
|
|
|
const StringSet &newNoteSet = newNoteSetMapIter->second;
|
|
|
|
//Info( "Got %d new strings", newNoteSet.size() );
|
2016-06-22 01:48:32 +08:00
|
|
|
if ( newNoteSet.size() > 0 ) {
|
2018-08-18 04:02:40 +08:00
|
|
|
StringSetMap::iterator noteSetMapIter = noteSetMap.find(newNoteGroup);
|
2016-06-22 01:48:32 +08:00
|
|
|
if ( noteSetMapIter == noteSetMap.end() ) {
|
2016-04-04 22:11:48 +08:00
|
|
|
//Info( "Can't find note group %s, copying %d strings", newNoteGroup.c_str(), newNoteSet.size() );
|
2018-08-18 04:02:40 +08:00
|
|
|
noteSetMap.insert(StringSetMap::value_type(newNoteGroup, newNoteSet));
|
2013-03-17 07:45:21 +08:00
|
|
|
update = true;
|
2016-06-22 01:48:32 +08:00
|
|
|
} else {
|
2016-04-04 22:11:48 +08:00
|
|
|
StringSet ¬eSet = noteSetMapIter->second;
|
|
|
|
//Info( "Found note group %s, got %d strings", newNoteGroup.c_str(), newNoteSet.size() );
|
2019-10-31 04:25:09 +08:00
|
|
|
for ( StringSet::const_iterator newNoteSetIter = newNoteSet.begin();
|
|
|
|
newNoteSetIter != newNoteSet.end();
|
|
|
|
++newNoteSetIter ) {
|
2016-04-04 22:11:48 +08:00
|
|
|
const std::string &newNote = *newNoteSetIter;
|
2018-08-18 04:02:40 +08:00
|
|
|
StringSet::iterator noteSetIter = noteSet.find(newNote);
|
2016-06-22 01:48:32 +08:00
|
|
|
if ( noteSetIter == noteSet.end() ) {
|
2018-08-18 04:02:40 +08:00
|
|
|
noteSet.insert(newNote);
|
2016-04-04 22:11:48 +08:00
|
|
|
update = true;
|
|
|
|
}
|
2017-05-20 20:26:55 +08:00
|
|
|
} // end for
|
|
|
|
} // end if ( noteSetMap.size() == 0
|
|
|
|
} // end if newNoteSetupMap.size() > 0
|
|
|
|
} // end foreach newNoteSetMap
|
|
|
|
} // end if have old notes
|
|
|
|
} // end if have new notes
|
2013-03-17 07:45:21 +08:00
|
|
|
|
2016-06-22 01:48:32 +08:00
|
|
|
if ( update ) {
|
2016-04-04 22:11:48 +08:00
|
|
|
std::string notes;
|
2018-08-18 04:02:40 +08:00
|
|
|
createNotes(notes);
|
2013-03-17 07:45:21 +08:00
|
|
|
|
2018-08-18 04:02:40 +08:00
|
|
|
Debug(2, "Updating notes for event %d, '%s'", id, notes.c_str());
|
2021-04-10 07:45:55 +08:00
|
|
|
char sql[ZM_SQL_LGE_BUFSIZ];
|
2013-03-17 07:45:21 +08:00
|
|
|
#if USE_PREPARED_SQL
|
2016-04-04 22:11:48 +08:00
|
|
|
static MYSQL_STMT *stmt = 0;
|
2013-03-17 07:45:21 +08:00
|
|
|
|
2016-04-04 22:11:48 +08:00
|
|
|
char notesStr[ZM_SQL_MED_BUFSIZ] = "";
|
|
|
|
unsigned long notesLen = 0;
|
2013-03-17 07:45:21 +08:00
|
|
|
|
2016-06-22 01:48:32 +08:00
|
|
|
if ( !stmt ) {
|
2019-10-31 04:25:09 +08:00
|
|
|
const char *sql = "UPDATE `Events` SET `Notes` = ? WHERE `Id` = ?";
|
2016-04-04 22:11:48 +08:00
|
|
|
|
2019-10-31 04:25:09 +08:00
|
|
|
stmt = mysql_stmt_init(&dbconn);
|
|
|
|
if ( mysql_stmt_prepare(stmt, sql, strlen(sql)) ) {
|
|
|
|
Fatal("Unable to prepare sql '%s': %s", sql, mysql_stmt_error(stmt));
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the parameter count from the statement */
|
2019-10-31 04:25:09 +08:00
|
|
|
if ( mysql_stmt_param_count(stmt) != 2 ) {
|
|
|
|
Error("Unexpected parameter count %ld in sql '%s'", mysql_stmt_param_count(stmt), sql);
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
MYSQL_BIND bind[2];
|
|
|
|
memset(bind, 0, sizeof(bind));
|
|
|
|
|
|
|
|
/* STRING PARAM */
|
|
|
|
bind[0].buffer_type = MYSQL_TYPE_STRING;
|
|
|
|
bind[0].buffer = (char *)notesStr;
|
|
|
|
bind[0].buffer_length = sizeof(notesStr);
|
|
|
|
bind[0].is_null = 0;
|
|
|
|
bind[0].length = ¬esLen;
|
|
|
|
|
|
|
|
bind[1].buffer_type= MYSQL_TYPE_LONG;
|
|
|
|
bind[1].buffer= (char *)&id;
|
|
|
|
bind[1].is_null= 0;
|
|
|
|
bind[1].length= 0;
|
|
|
|
|
|
|
|
/* Bind the buffers */
|
2019-10-31 04:25:09 +08:00
|
|
|
if ( mysql_stmt_bind_param(stmt, bind) ) {
|
|
|
|
Error("Unable to bind sql '%s': %s", sql, mysql_stmt_error(stmt));
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
2018-02-16 04:54:13 +08:00
|
|
|
} // end if ! stmt
|
2013-03-17 07:45:21 +08:00
|
|
|
|
2019-10-31 04:25:09 +08:00
|
|
|
strncpy(notesStr, notes.c_str(), sizeof(notesStr));
|
2013-03-17 07:45:21 +08:00
|
|
|
|
2019-10-31 04:25:09 +08:00
|
|
|
if ( mysql_stmt_execute(stmt) ) {
|
|
|
|
Error("Unable to execute sql '%s': %s", sql, mysql_stmt_error(stmt));
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
2013-03-17 07:45:21 +08:00
|
|
|
#else
|
2016-04-04 22:11:48 +08:00
|
|
|
static char escapedNotes[ZM_SQL_MED_BUFSIZ];
|
2013-03-17 07:45:21 +08:00
|
|
|
|
2018-04-14 23:03:08 +08:00
|
|
|
mysql_real_escape_string(&dbconn, escapedNotes, notes.c_str(), notes.length());
|
2013-03-17 07:45:21 +08:00
|
|
|
|
2019-10-31 04:25:09 +08:00
|
|
|
snprintf(sql, sizeof(sql), "UPDATE `Events` SET `Notes` = '%s' WHERE `Id` = %" PRIu64, escapedNotes, id);
|
2021-04-10 07:45:55 +08:00
|
|
|
dbQueue.push(std::move(sql));
|
2016-04-04 22:11:48 +08:00
|
|
|
#endif
|
2019-10-31 04:25:09 +08:00
|
|
|
} // end if update
|
|
|
|
} // void Event::updateNotes(const StringSetMap &newNoteSetMap)
|
2013-03-17 07:45:21 +08:00
|
|
|
|
2018-04-14 23:03:08 +08:00
|
|
|
void Event::AddFrames(int n_frames, Image **images, struct timeval **timestamps) {
|
2020-11-12 01:43:41 +08:00
|
|
|
for ( int i = 0; i < n_frames; i += ZM_SQL_BATCH_SIZE ) {
|
2016-04-04 22:11:48 +08:00
|
|
|
AddFramesInternal(n_frames, i, images, timestamps);
|
|
|
|
}
|
2013-10-27 09:41:12 +08:00
|
|
|
}
|
|
|
|
|
2018-04-14 23:03:08 +08:00
|
|
|
void Event::AddFramesInternal(int n_frames, int start_frame, Image **images, struct timeval **timestamps) {
|
2020-10-05 21:11:16 +08:00
|
|
|
char *frame_insert_values = (char *)&frame_insert_sql + 90; // 90 == strlen(frame_insert_sql);
|
|
|
|
//static char sql[ZM_SQL_LGE_BUFSIZ];
|
|
|
|
//strncpy(sql, "INSERT INTO `Frames` (`EventId`, `FrameId`, `TimeStamp`, `Delta`) VALUES ", sizeof(sql));
|
2016-04-04 22:11:48 +08:00
|
|
|
int frameCount = 0;
|
2016-06-22 01:48:32 +08:00
|
|
|
for ( int i = start_frame; i < n_frames && i - start_frame < ZM_SQL_BATCH_SIZE; i++ ) {
|
2017-10-23 21:56:30 +08:00
|
|
|
if ( timestamps[i]->tv_sec <= 0 ) {
|
2018-05-04 01:54:05 +08:00
|
|
|
Debug(1, "Not adding pre-capture frame %d, zero or less than 0 timestamp", i);
|
2016-04-04 22:11:48 +08:00
|
|
|
continue;
|
2017-10-10 02:58:07 +08:00
|
|
|
} else {
|
|
|
|
Debug( 3, "Adding pre-capture frame %d, timestamp = (%d), start_time=(%d)", i, timestamps[i]->tv_sec, start_time.tv_sec );
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
2013-03-17 07:45:21 +08:00
|
|
|
|
2016-04-04 22:11:48 +08:00
|
|
|
frames++;
|
2013-03-17 07:45:21 +08:00
|
|
|
|
2018-05-20 22:39:14 +08:00
|
|
|
if ( save_jpegs & 1 ) {
|
2020-07-23 01:14:40 +08:00
|
|
|
std::string event_file = stringtf(staticConfig.capture_file_format, path.c_str(), frames);
|
2018-05-04 01:54:05 +08:00
|
|
|
Debug(1, "Writing pre-capture frame %d", frames);
|
2020-07-22 04:10:05 +08:00
|
|
|
WriteFrameImage(images[i], *(timestamps[i]), event_file.c_str());
|
2014-12-25 02:45:50 +08:00
|
|
|
}
|
2019-10-31 04:25:09 +08:00
|
|
|
//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
|
|
|
|
// neccessarily be of the motion. But some events are less than 10 frames,
|
|
|
|
// so I am changing this to 1, but we should overwrite it later with a better snapshot.
|
|
|
|
if ( frames == 1 ) {
|
2020-05-03 06:03:42 +08:00
|
|
|
WriteFrameImage(images[i], *(timestamps[i]), snapshot_file.c_str());
|
2014-12-25 02:45:50 +08:00
|
|
|
}
|
2013-03-17 07:45:21 +08:00
|
|
|
|
2016-04-04 22:11:48 +08:00
|
|
|
struct DeltaTimeval delta_time;
|
2019-10-31 04:25:09 +08:00
|
|
|
DELTA_TIMEVAL(delta_time, *(timestamps[i]), start_time, DT_PREC_2);
|
2018-01-31 00:23:09 +08:00
|
|
|
// Delta is Decimal(8,2) so 6 integer digits and 2 decimal digits
|
|
|
|
if ( delta_time.sec > 999999 ) {
|
2018-01-27 01:21:12 +08:00
|
|
|
Warning("Invalid delta_time from_unixtime(%ld), %s%ld.%02ld",
|
2019-10-31 04:25:09 +08:00
|
|
|
timestamps[i]->tv_sec,
|
|
|
|
(delta_time.positive?"":"-"),
|
|
|
|
delta_time.sec,
|
|
|
|
delta_time.fsec);
|
2018-01-27 01:21:12 +08:00
|
|
|
delta_time.sec = 0;
|
|
|
|
}
|
2013-03-17 07:45:21 +08:00
|
|
|
|
2020-10-05 21:11:16 +08:00
|
|
|
frame_insert_values += snprintf(frame_insert_values,
|
|
|
|
sizeof(frame_insert_sql)-(frame_insert_values-(char *)&frame_insert_sql),
|
|
|
|
"\n( %" PRIu64 ", %d, 'Normal', from_unixtime(%ld), %s%ld.%02ld, 0 ),",
|
2018-08-12 06:49:30 +08:00
|
|
|
id, frames, timestamps[i]->tv_sec, delta_time.positive?"":"-", delta_time.sec, delta_time.fsec);
|
2013-03-17 07:45:21 +08:00
|
|
|
|
2016-04-04 22:11:48 +08:00
|
|
|
frameCount++;
|
2018-04-28 04:20:38 +08:00
|
|
|
} // end foreach frame
|
2013-03-17 07:45:21 +08:00
|
|
|
|
2016-06-22 01:48:32 +08:00
|
|
|
if ( frameCount ) {
|
2020-10-05 21:11:16 +08:00
|
|
|
*(frame_insert_values-1) = '\0';
|
2021-02-26 01:26:26 +08:00
|
|
|
zmDbDo(frame_insert_sql);
|
2016-04-04 22:11:48 +08:00
|
|
|
last_db_frame = frames;
|
2016-06-22 01:48:32 +08:00
|
|
|
} else {
|
2018-05-04 01:54:05 +08:00
|
|
|
Debug(1, "No valid pre-capture frames to add");
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
2020-11-03 05:34:00 +08:00
|
|
|
end_time = *timestamps[n_frames-1];
|
2019-10-31 04:25:09 +08:00
|
|
|
} // void Event::AddFramesInternal(int n_frames, int start_frame, Image **images, struct timeval **timestamps)
|
2013-03-17 07:45:21 +08:00
|
|
|
|
2020-12-22 23:18:15 +08:00
|
|
|
void Event::AddPacket(ZMPacket *packet) {
|
2017-11-14 15:39:58 +08:00
|
|
|
|
2021-02-22 09:24:36 +08:00
|
|
|
have_video_keyframe = have_video_keyframe ||
|
|
|
|
( ( packet->codec_type == AVMEDIA_TYPE_VIDEO ) &&
|
|
|
|
( packet->keyframe || monitor->GetOptVideoWriter() == Monitor::ENCODE) );
|
2021-01-12 02:45:21 +08:00
|
|
|
Debug(2, "have_video_keyframe %d codec_type %d == video? %d packet keyframe %d",
|
|
|
|
have_video_keyframe, packet->codec_type, (packet->codec_type == AVMEDIA_TYPE_VIDEO), packet->keyframe);
|
2021-02-06 21:46:28 +08:00
|
|
|
ZM_DUMP_PACKET(packet->packet, "Adding to event");
|
2017-12-01 04:01:48 +08:00
|
|
|
if ( videoStore ) {
|
2017-12-01 03:37:36 +08:00
|
|
|
if ( have_video_keyframe ) {
|
2018-04-14 23:03:08 +08:00
|
|
|
videoStore->writePacket(packet);
|
2017-12-01 03:37:36 +08:00
|
|
|
} else {
|
|
|
|
Debug(2, "No video keyframe yet, not writing");
|
|
|
|
}
|
2017-11-14 15:39:58 +08:00
|
|
|
//FIXME if it fails, we should write a jpeg
|
|
|
|
}
|
2021-01-28 01:49:27 +08:00
|
|
|
if ( ( packet->codec_type == AVMEDIA_TYPE_VIDEO ) or packet->image )
|
2020-12-22 23:18:15 +08:00
|
|
|
AddFrame(packet->image, *(packet->timestamp), packet->score, packet->analysis_image);
|
2020-12-16 04:57:01 +08:00
|
|
|
end_time = *packet->timestamp;
|
2017-11-21 00:48:56 +08:00
|
|
|
return;
|
2017-11-14 15:39:58 +08:00
|
|
|
}
|
|
|
|
|
2018-12-06 02:18:21 +08:00
|
|
|
void Event::WriteDbFrames() {
|
2020-10-05 21:11:16 +08:00
|
|
|
char *frame_insert_values_ptr = (char *)&frame_insert_sql + 90; // 90 == strlen(frame_insert_sql);
|
2020-11-12 03:03:08 +08:00
|
|
|
|
|
|
|
/* Each frame needs about 63 chars. So if we buffer too many frames, we will exceed the size of frame_insert_sql;
|
|
|
|
*/
|
2020-11-02 05:11:19 +08:00
|
|
|
Debug(1, "Inserting %d frames", frame_data.size());
|
2018-12-06 02:18:21 +08:00
|
|
|
while ( frame_data.size() ) {
|
|
|
|
Frame *frame = frame_data.front();
|
|
|
|
frame_data.pop();
|
2020-10-05 21:11:16 +08:00
|
|
|
frame_insert_values_ptr += snprintf(frame_insert_values_ptr,
|
|
|
|
sizeof(frame_insert_sql)-(frame_insert_values_ptr-(char *)&frame_insert_sql),
|
|
|
|
"\n( %" PRIu64 ", %d, '%s', from_unixtime( %ld ), %s%ld.%02ld, %d ),",
|
|
|
|
id, frame->frame_id,
|
|
|
|
frame_type_names[frame->type],
|
2018-12-06 02:18:21 +08:00
|
|
|
frame->timestamp.tv_sec,
|
|
|
|
frame->delta.positive?"":"-",
|
|
|
|
frame->delta.sec,
|
|
|
|
frame->delta.fsec,
|
|
|
|
frame->score);
|
|
|
|
delete frame;
|
|
|
|
}
|
2020-11-02 05:11:19 +08:00
|
|
|
*(frame_insert_values_ptr-1) = '\0'; // The -1 is for the extra , added for values above
|
2021-03-12 02:48:42 +08:00
|
|
|
std::string sql(frame_insert_sql);
|
|
|
|
|
|
|
|
dbQueue.push(std::move(sql));
|
|
|
|
//zmDbDo(frame_insert_sql);
|
2019-02-20 01:00:17 +08:00
|
|
|
} // end void Event::WriteDbFrames()
|
2018-12-06 02:18:21 +08:00
|
|
|
|
2018-05-04 01:54:05 +08:00
|
|
|
void Event::AddFrame(Image *image, struct timeval timestamp, int score, Image *alarm_image) {
|
2021-02-10 03:22:02 +08:00
|
|
|
if (!timestamp.tv_sec) {
|
|
|
|
Warning("Not adding new frame, zero timestamp");
|
2016-04-04 22:11:48 +08:00
|
|
|
return;
|
|
|
|
}
|
2013-03-17 07:45:21 +08:00
|
|
|
|
2016-04-04 22:11:48 +08:00
|
|
|
frames++;
|
2021-02-10 03:22:02 +08:00
|
|
|
Monitor::State monitor_state = monitor->GetState();
|
2013-03-17 07:45:21 +08:00
|
|
|
|
2019-04-07 22:29:02 +08:00
|
|
|
bool write_to_db = false;
|
2021-02-04 05:56:34 +08:00
|
|
|
FrameType frame_type = ( ( score > 0 ) ? ALARM : (
|
|
|
|
(
|
2021-02-10 03:22:02 +08:00
|
|
|
( monitor_state == Monitor::TAPE )
|
2021-02-04 05:56:34 +08:00
|
|
|
and
|
|
|
|
( config.bulk_frame_interval > 1 )
|
|
|
|
and
|
|
|
|
( ! (frames % config.bulk_frame_interval) )
|
|
|
|
) ? BULK : NORMAL
|
|
|
|
) );
|
2021-02-19 02:30:05 +08:00
|
|
|
Debug(1, "Have frame type %s from score(%d) state %d frames %d bulk frame interval %d and mod%d",
|
|
|
|
frame_type_names[frame_type], score, monitor->GetState(), frames, config.bulk_frame_interval, (frames % config.bulk_frame_interval) );
|
2021-02-04 05:56:34 +08:00
|
|
|
|
2021-03-03 00:47:38 +08:00
|
|
|
if (score < 0) score = 0;
|
2020-12-28 00:55:53 +08:00
|
|
|
tot_score += score;
|
2020-05-03 06:03:42 +08:00
|
|
|
|
2021-02-10 03:22:02 +08:00
|
|
|
if (image) {
|
|
|
|
if (save_jpegs & 1) {
|
2020-12-28 00:55:53 +08:00
|
|
|
std::string event_file = stringtf(staticConfig.capture_file_format, path.c_str(), frames);
|
|
|
|
Debug(1, "Writing capture frame %d to %s", frames, event_file.c_str());
|
2021-02-10 03:22:02 +08:00
|
|
|
if (!WriteFrameImage(image, timestamp, event_file.c_str())) {
|
2020-12-28 00:55:53 +08:00
|
|
|
Error("Failed to write frame image");
|
|
|
|
}
|
2021-02-10 03:22:02 +08:00
|
|
|
} // end if save_jpegs
|
2020-12-28 00:55:53 +08:00
|
|
|
|
|
|
|
// If this is the first frame, we should add a thumbnail to the event directory
|
|
|
|
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.
|
|
|
|
WriteFrameImage(image, timestamp, snapshot_file.c_str());
|
2018-11-13 01:43:20 +08:00
|
|
|
}
|
2013-03-17 07:45:21 +08:00
|
|
|
|
2020-12-28 00:55:53 +08:00
|
|
|
// We are writing an Alarm frame
|
2021-02-10 03:22:02 +08:00
|
|
|
if (frame_type == ALARM) {
|
2020-12-28 00:55:53 +08:00
|
|
|
// The first frame with a score will be the frame that alarmed the event
|
2021-02-10 03:22:02 +08:00
|
|
|
if (!alarm_frame_written) {
|
2020-12-28 00:55:53 +08:00
|
|
|
write_to_db = true; // OD processing will need it, so the db needs to know about it
|
|
|
|
alarm_frame_written = true;
|
|
|
|
WriteFrameImage(image, timestamp, alarm_file.c_str());
|
|
|
|
}
|
|
|
|
alarm_frames++;
|
2019-10-31 04:25:09 +08:00
|
|
|
|
2021-02-10 03:22:02 +08:00
|
|
|
if (alarm_image and (save_jpegs & 2)) {
|
2020-07-23 01:14:40 +08:00
|
|
|
std::string event_file = stringtf(staticConfig.analyse_file_format, path.c_str(), frames);
|
2019-10-31 04:25:09 +08:00
|
|
|
Debug(1, "Writing analysis frame %d", frames);
|
2021-02-10 03:22:02 +08:00
|
|
|
if (!WriteFrameImage(alarm_image, timestamp, event_file.c_str(), true)) {
|
2019-10-31 04:25:09 +08:00
|
|
|
Error("Failed to write analysis frame image");
|
|
|
|
}
|
|
|
|
}
|
2020-12-28 00:55:53 +08:00
|
|
|
} // end if is an alarm frame
|
2021-02-19 02:30:05 +08:00
|
|
|
} else {
|
|
|
|
Debug(1, "No image");
|
2020-12-28 00:55:53 +08:00
|
|
|
} // end if has image
|
2013-03-17 07:45:21 +08:00
|
|
|
|
2021-03-03 00:47:38 +08:00
|
|
|
bool db_frame = ( frame_type == BULK )
|
|
|
|
or ( frame_type == ALARM )
|
|
|
|
or ( frames == 1 )
|
|
|
|
or ( score > (int)max_score )
|
|
|
|
or ( monitor_state == Monitor::ALERT )
|
|
|
|
or ( monitor_state == Monitor::ALARM )
|
|
|
|
or ( monitor_state == Monitor::PREALARM );
|
|
|
|
|
2021-02-10 03:22:02 +08:00
|
|
|
if (db_frame) {
|
2013-03-17 07:45:21 +08:00
|
|
|
|
2020-05-03 06:03:42 +08:00
|
|
|
struct DeltaTimeval delta_time;
|
|
|
|
DELTA_TIMEVAL(delta_time, timestamp, start_time, DT_PREC_2);
|
2020-10-21 04:20:29 +08:00
|
|
|
Debug(1, "Frame delta is %d.%d - %d.%d = %d.%d",
|
2021-02-04 05:56:34 +08:00
|
|
|
start_time.tv_sec, start_time.tv_usec,
|
|
|
|
timestamp.tv_sec, timestamp.tv_usec,
|
|
|
|
delta_time.sec, delta_time.fsec);
|
2018-12-06 02:18:21 +08:00
|
|
|
|
2018-12-17 05:18:50 +08:00
|
|
|
// The idea is to write out 1/sec
|
2019-02-20 01:00:17 +08:00
|
|
|
frame_data.push(new Frame(id, frames, frame_type, timestamp, delta_time, score));
|
2020-08-08 05:52:43 +08:00
|
|
|
double fps = monitor->get_capture_fps();
|
2021-02-03 02:56:11 +08:00
|
|
|
if ( write_to_db
|
|
|
|
or
|
2021-02-04 05:56:34 +08:00
|
|
|
( frame_data.size() >= MAX_DB_FRAMES )
|
2020-11-12 03:03:08 +08:00
|
|
|
or
|
2021-02-04 05:56:34 +08:00
|
|
|
( frame_type == BULK )
|
2020-11-12 03:03:08 +08:00
|
|
|
or
|
|
|
|
( fps and (frame_data.size() > fps) )
|
|
|
|
) {
|
2020-11-14 01:43:31 +08:00
|
|
|
Debug(1, "Adding %d frames to DB because write_to_db:%d or frames > analysis fps %f or BULK(%d)",
|
|
|
|
frame_data.size(), write_to_db, fps, (frame_type==BULK));
|
2018-12-06 02:18:21 +08:00
|
|
|
WriteDbFrames();
|
|
|
|
last_db_frame = frames;
|
2013-03-17 07:45:21 +08:00
|
|
|
|
2021-04-10 07:45:55 +08:00
|
|
|
char sql[ZM_SQL_MED_BUFSIZ];
|
2018-04-28 04:20:38 +08:00
|
|
|
snprintf(sql, sizeof(sql),
|
2019-06-11 01:59:01 +08:00
|
|
|
"UPDATE Events SET Length = %s%ld.%02ld, Frames = %d, AlarmFrames = %d, TotScore = %d, AvgScore = %d, MaxScore = %d WHERE Id = %" PRIu64,
|
2017-05-20 02:43:49 +08:00
|
|
|
( delta_time.positive?"":"-" ),
|
|
|
|
delta_time.sec, delta_time.fsec,
|
|
|
|
frames,
|
|
|
|
alarm_frames,
|
|
|
|
tot_score,
|
|
|
|
(int)(alarm_frames?(tot_score/alarm_frames):0),
|
|
|
|
max_score,
|
|
|
|
id
|
|
|
|
);
|
2021-04-10 07:45:55 +08:00
|
|
|
dbQueue.push(std::move(sql));
|
2020-11-12 01:43:41 +08:00
|
|
|
} else {
|
|
|
|
Debug(1, "Not Adding %d frames to DB because write_to_db:%d or frames > analysis fps %f or BULK",
|
2020-11-12 03:03:08 +08:00
|
|
|
frame_data.size(), write_to_db, fps);
|
2018-12-06 02:18:21 +08:00
|
|
|
} // end if frame_type == BULK
|
2017-09-26 04:22:41 +08:00
|
|
|
} // end if db_frame
|
2013-03-17 07:45:21 +08:00
|
|
|
|
2021-02-10 03:22:02 +08:00
|
|
|
if (score > (int)max_score)
|
2021-02-04 05:56:34 +08:00
|
|
|
max_score = score;
|
2016-04-04 22:11:48 +08:00
|
|
|
end_time = timestamp;
|
2019-10-31 04:25:09 +08:00
|
|
|
} // end void Event::AddFrame(Image *image, struct timeval timestamp, int score, Image *alarm_image)
|
2020-11-28 02:28:38 +08:00
|
|
|
|
|
|
|
bool Event::SetPath(Storage *storage) {
|
|
|
|
scheme = storage->Scheme();
|
|
|
|
|
|
|
|
path = stringtf("%s/%d", storage->Path(), monitor->Id());
|
|
|
|
// Try to make the Monitor Dir. Normally this would exist, but in odd cases might not.
|
|
|
|
if ( mkdir(path.c_str(), 0755) and ( errno != EEXIST ) ) {
|
|
|
|
Error("Can't mkdir %s: %s", path.c_str(), strerror(errno));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-03-07 23:35:05 +08:00
|
|
|
tm stime = {};
|
|
|
|
localtime_r(&start_time.tv_sec, &stime);
|
2020-11-28 02:28:38 +08:00
|
|
|
if ( scheme == Storage::DEEP ) {
|
|
|
|
|
|
|
|
int dt_parts[6];
|
2021-03-07 23:35:05 +08:00
|
|
|
dt_parts[0] = stime.tm_year-100;
|
|
|
|
dt_parts[1] = stime.tm_mon+1;
|
|
|
|
dt_parts[2] = stime.tm_mday;
|
|
|
|
dt_parts[3] = stime.tm_hour;
|
|
|
|
dt_parts[4] = stime.tm_min;
|
|
|
|
dt_parts[5] = stime.tm_sec;
|
2020-11-28 02:28:38 +08:00
|
|
|
|
|
|
|
std::string date_path;
|
|
|
|
std::string time_path;
|
|
|
|
|
|
|
|
for ( unsigned int i = 0; i < sizeof(dt_parts)/sizeof(*dt_parts); i++ ) {
|
|
|
|
path += stringtf("/%02d", dt_parts[i]);
|
|
|
|
|
|
|
|
if ( mkdir(path.c_str(), 0755) and ( errno != EEXIST ) ) {
|
|
|
|
Error("Can't mkdir %s: %s", path.c_str(), strerror(errno));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ( i == 2 )
|
|
|
|
date_path = path;
|
|
|
|
}
|
2021-03-07 23:35:05 +08:00
|
|
|
time_path = stringtf("%02d/%02d/%02d", stime.tm_hour, stime.tm_min, stime.tm_sec);
|
2020-11-28 02:28:38 +08:00
|
|
|
|
|
|
|
// Create event id symlink
|
|
|
|
std::string id_file = stringtf("%s/.%" PRIu64, date_path.c_str(), id);
|
|
|
|
if ( symlink(time_path.c_str(), id_file.c_str()) < 0 ) {
|
|
|
|
Error("Can't symlink %s -> %s: %s", id_file.c_str(), time_path.c_str(), strerror(errno));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if ( scheme == Storage::MEDIUM ) {
|
|
|
|
path += stringtf("/%04d-%02d-%02d",
|
2021-03-07 23:35:05 +08:00
|
|
|
stime.tm_year+1900, stime.tm_mon+1, stime.tm_mday
|
2020-11-28 02:28:38 +08:00
|
|
|
);
|
|
|
|
if ( mkdir(path.c_str(), 0755) and ( errno != EEXIST ) ) {
|
|
|
|
Error("Can't mkdir %s: %s", path.c_str(), strerror(errno));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
path += stringtf("/%" PRIu64, id);
|
|
|
|
if ( mkdir(path.c_str(), 0755) and ( errno != EEXIST ) ) {
|
|
|
|
Error("Can't mkdir %s: %s", path.c_str(), strerror(errno));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
path += stringtf("/%" PRIu64, id);
|
|
|
|
if ( mkdir(path.c_str(), 0755) and ( errno != EEXIST ) ) {
|
|
|
|
Error("Can't mkdir %s: %s", path.c_str(), strerror(errno));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create empty id tag file
|
|
|
|
std::string id_file = stringtf("%s/.%" PRIu64, path.c_str(), id);
|
|
|
|
if ( FILE *id_fp = fopen(id_file.c_str(), "w") ) {
|
|
|
|
fclose(id_fp);
|
|
|
|
} else {
|
|
|
|
Error("Can't fopen %s: %s", id_file.c_str(), strerror(errno));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} // deep storage or not
|
|
|
|
return true;
|
|
|
|
} // end bool Event::SetPath
|