From dff5452f110ae2a8b6cc1803f5957e8dc0b7c0f2 Mon Sep 17 00:00:00 2001 From: Peter Keresztes Schmidt Date: Sun, 13 Jun 2021 16:13:10 +0200 Subject: [PATCH] Event: Convert API to std::chrono --- src/zm_event.cpp | 57 ++++++++++++++++++---------------------------- src/zm_event.h | 35 +++++++++++----------------- src/zm_monitor.cpp | 32 +++++++++++++++++++------- src/zm_monitor.h | 8 +++++-- 4 files changed, 65 insertions(+), 67 deletions(-) diff --git a/src/zm_event.cpp b/src/zm_event.cpp index fc0d18f1f..8589711bd 100644 --- a/src/zm_event.cpp +++ b/src/zm_event.cpp @@ -43,13 +43,13 @@ Event::PreAlarmData Event::pre_alarm_data[MAX_PRE_ALARM_FRAMES] = {}; Event::Event( Monitor *p_monitor, - struct timeval p_start_time, + SystemTimePoint p_start_time, const std::string &p_cause, const StringSetMap &p_noteSetMap ) : id(0), monitor(p_monitor), - start_time(SystemTimePoint(zm::chrono::duration_cast(p_start_time))), + start_time(p_start_time), end_time(), cause(p_cause), noteSetMap(p_noteSetMap), @@ -220,7 +220,7 @@ Event::Event( zmDbDo(sql.c_str()); } } // end if GetOptVideoWriter -} // Event::Event( Monitor *p_monitor, struct timeval p_start_time, const std::string &p_cause, const StringSetMap &p_noteSetMap, bool p_videoEvent ) +} Event::~Event() { // 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. @@ -282,26 +282,20 @@ void Event::createNotes(std::string ¬es) { } } // void Event::createNotes(std::string ¬es) -bool Event::WriteFrameImage( - Image *image, - timeval timestamp, - const char *event_file, - bool alarm_frame) const { - +bool Event::WriteFrameImage(Image *image, SystemTimePoint timestamp, const char *event_file, bool alarm_frame) const { 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; - SystemTimePoint jpeg_timestamp = - monitor->Exif() ? SystemTimePoint(zm::chrono::duration_cast(timestamp)) : SystemTimePoint(); + SystemTimePoint jpeg_timestamp = monitor->Exif() ? timestamp : SystemTimePoint(); if (!config.timestamp_on_capture) { // 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); - monitor->TimestampImage(ts_image, timestamp); + monitor->TimestampImage(ts_image, zm::chrono::duration_cast(timestamp.time_since_epoch())); rc = ts_image->WriteJpeg(event_file, thisquality, jpeg_timestamp); delete ts_image; } else { @@ -309,7 +303,7 @@ bool Event::WriteFrameImage( } return rc; -} // end Event::WriteFrameImage( Image *image, struct timeval timestamp, const char *event_file, bool alarm_frame ) +} bool Event::WritePacket(const std::shared_ptr&packet) { if (videoStore->writePacket(packet) < 0) @@ -427,6 +421,8 @@ void Event::AddPacket(const std::shared_ptr&packet) { have_video_keyframe, packet->codec_type, (packet->codec_type == AVMEDIA_TYPE_VIDEO), packet->keyframe); ZM_DUMP_PACKET(packet->packet, "Adding to event"); + SystemTimePoint packet_ts = SystemTimePoint(zm::chrono::duration_cast(packet->timestamp)); + if (videoStore) { if (have_video_keyframe) { videoStore->writePacket(packet); @@ -437,9 +433,9 @@ void Event::AddPacket(const std::shared_ptr&packet) { } if ((packet->codec_type == AVMEDIA_TYPE_VIDEO) or packet->image) { - AddFrame(packet->image, packet->timestamp, packet->zone_stats, packet->score, packet->analysis_image); + AddFrame(packet->image, packet_ts, packet->zone_stats, packet->score, packet->analysis_image); } - end_time = SystemTimePoint(zm::chrono::duration_cast(packet->timestamp)); + end_time = packet_ts; } void Event::WriteDbFrames() { @@ -493,13 +489,12 @@ void Event::WriteDbFrames() { } } // end void Event::WriteDbFrames() -void Event::AddFrame( - Image *image, - struct timeval timestamp, - const std::vector &zone_stats, - int score, - Image *alarm_image) { - if (!timestamp.tv_sec) { +void Event::AddFrame(Image *image, + SystemTimePoint timestamp, + const std::vector &zone_stats, + int score, + Image *alarm_image) { + if (timestamp.time_since_epoch() == Seconds(0)) { Warning("Not adding new frame, zero timestamp"); return; } @@ -576,25 +571,17 @@ void Event::AddFrame( or ( monitor_state == Monitor::ALARM ) or ( monitor_state == Monitor::PREALARM ); - SystemTimePoint timestamp_us = SystemTimePoint(zm::chrono::duration_cast(timestamp)); - if (db_frame) { - Microseconds delta_time = std::chrono::duration_cast(timestamp_us - start_time); + Microseconds delta_time = std::chrono::duration_cast(timestamp - start_time); Debug(1, "Frame delta is %.2f s - %.2f s = %.2f s, score %u zone_stats.size %zu", - FPSeconds(timestamp_us.time_since_epoch()).count(), + FPSeconds(timestamp.time_since_epoch()).count(), FPSeconds(start_time.time_since_epoch()).count(), FPSeconds(delta_time).count(), score, zone_stats.size()); // The idea is to write out 1/sec - frame_data.push(new Frame(id, - frames, - frame_type, - SystemTimePoint(zm::chrono::duration_cast(timestamp)), - delta_time, - score, - zone_stats)); + frame_data.push(new Frame(id, frames, frame_type, timestamp, delta_time, score, zone_stats)); double fps = monitor->get_capture_fps(); if (write_to_db or @@ -627,8 +614,8 @@ void Event::AddFrame( if (score > (int) max_score) { max_score = score; } - end_time = timestamp_us; -} // end void Event::AddFrame(Image *image, struct timeval timestamp, int score, Image *alarm_image) + end_time = timestamp; +} bool Event::SetPath(Storage *storage) { scheme = storage->Scheme(); diff --git a/src/zm_event.h b/src/zm_event.h index b89c4efc9..38dc0e290 100644 --- a/src/zm_event.h +++ b/src/zm_event.h @@ -96,12 +96,10 @@ class Event { static bool OpenFrameSocket(int); static bool ValidateFrameSocket(int); - Event( - Monitor *p_monitor, - struct timeval p_start_time, - const std::string &p_cause, - const StringSetMap &p_noteSetMap - ); + Event(Monitor *p_monitor, + SystemTimePoint p_start_time, + const std::string &p_cause, + const StringSetMap &p_noteSetMap); ~Event(); uint64_t Id() const { return id; } @@ -109,28 +107,21 @@ class Event { int Frames() const { return frames; } int AlarmFrames() const { return alarm_frames; } - timeval StartTime() const { return zm::chrono::duration_cast(start_time.time_since_epoch()); } - timeval EndTime() const { return zm::chrono::duration_cast(end_time.time_since_epoch()); } + SystemTimePoint StartTime() const { return start_time; } + SystemTimePoint EndTime() const { return end_time; } void AddPacket(const std::shared_ptr &p); bool WritePacket(const std::shared_ptr &p); bool SendFrameImage(const Image *image, bool alarm_frame=false); - bool WriteFrameImage( - Image *image, - struct timeval timestamp, - const char *event_file, - bool alarm_frame=false - ) const; + bool WriteFrameImage(Image *image, SystemTimePoint timestamp, const char *event_file, bool alarm_frame = false) const; void updateNotes(const StringSetMap &stringSetMap); - void AddFrame( - Image *image, - struct timeval timestamp, - const std::vector &stats, - int score=0, - Image *alarm_image=nullptr - ); + void AddFrame(Image *image, + SystemTimePoint timestamp, + const std::vector &stats, + int score = 0, + Image *alarm_image = nullptr); private: void WriteDbFrames(); @@ -174,7 +165,7 @@ class Event { } static void AddPreAlarmFrame( Image *image, - struct timeval timestamp, + SystemTimePoint timestamp, int score=0, Image *alarm_frame=nullptr ) { diff --git a/src/zm_monitor.cpp b/src/zm_monitor.cpp index 9eeacb095..e9572ac23 100644 --- a/src/zm_monitor.cpp +++ b/src/zm_monitor.cpp @@ -1968,7 +1968,10 @@ bool Monitor::Analyse() { starting_packet = snap; } - event = new Event(this, starting_packet->timestamp, "Continuous", noteSetMap); + event = new Event(this, + SystemTimePoint(zm::chrono::duration_cast(starting_packet->timestamp)), + "Continuous", + noteSetMap); // Write out starting packets, do not modify packetqueue it will garbage collect itself while (starting_packet and ((*start_it) != snap_it)) { event->AddPacket(starting_packet); @@ -1990,7 +1993,10 @@ bool Monitor::Analyse() { start_it = nullptr; } else { // Create event from current snap - event = new Event(this, *timestamp, "Continuous", noteSetMap); + event = new Event(this, + SystemTimePoint(zm::chrono::duration_cast(*timestamp)), + "Continuous", + noteSetMap); } shared_data->last_event_id = event->Id(); @@ -2004,7 +2010,8 @@ bool Monitor::Analyse() { } alarm_cause = cause+" Continuous "+alarm_cause; strncpy(shared_data->alarm_cause, alarm_cause.c_str(), sizeof(shared_data->alarm_cause)-1); - video_store_data->recording = event->StartTime(); + SetVideoWriterStartTime(event->StartTime()); + Info("%s: %03d - Opened new event %" PRIu64 ", section start", name.c_str(), analysis_image_count, event->Id()); /* To prevent cancelling out an existing alert\prealarm\alarm state */ @@ -2066,10 +2073,13 @@ bool Monitor::Analyse() { starting_packet = snap; } - event = new Event(this, starting_packet->timestamp, cause, noteSetMap); + event = new Event(this, + SystemTimePoint(zm::chrono::duration_cast(starting_packet->timestamp)), + cause, + noteSetMap); shared_data->last_event_id = event->Id(); snprintf(video_store_data->event_file, sizeof(video_store_data->event_file), "%s", event->getEventFile()); - video_store_data->recording = event->StartTime(); + SetVideoWriterStartTime(event->StartTime()); shared_data->state = state = ALARM; // Write out starting packets, do not modify packetqueue it will garbage collect itself @@ -2185,7 +2195,10 @@ bool Monitor::Analyse() { // incremement pre alarm image count //have_pre_alarmed_frames ++; - Event::AddPreAlarmFrame(snap->image, *timestamp, score, nullptr); + Event::AddPreAlarmFrame(snap->image, + SystemTimePoint(zm::chrono::duration_cast(*timestamp)), + score, + nullptr); } else if (state == ALARM) { for (const Zone &zone : zones) { if (zone.Alarmed()) { @@ -2208,11 +2221,14 @@ bool Monitor::Analyse() { static_cast(timestamp->tv_sec - video_store_data->recording.tv_sec), section_length); closeEvent(); - event = new Event(this, *timestamp, cause, noteSetMap); + event = new Event(this, + SystemTimePoint(zm::chrono::duration_cast(*timestamp)), + cause, + noteSetMap); shared_data->last_event_id = event->Id(); //set up video store data snprintf(video_store_data->event_file, sizeof(video_store_data->event_file), "%s", event->getEventFile()); - video_store_data->recording = event->StartTime(); + SetVideoWriterStartTime(event->StartTime()); } } else { Error("ALARM but no event"); diff --git a/src/zm_monitor.h b/src/zm_monitor.h index dbe9caefb..a7d17d453 100644 --- a/src/zm_monitor.h +++ b/src/zm_monitor.h @@ -513,8 +513,12 @@ public: uint64_t GetVideoWriterEventId() const { return video_store_data->current_event; } void SetVideoWriterEventId( uint64_t p_event_id ) { video_store_data->current_event = p_event_id; } - struct timeval GetVideoWriterStartTime() const { return video_store_data->recording; } - void SetVideoWriterStartTime(const struct timeval &t) { video_store_data->recording = t; } + SystemTimePoint GetVideoWriterStartTime() const { + return SystemTimePoint(zm::chrono::duration_cast(video_store_data->recording)); + } + void SetVideoWriterStartTime(SystemTimePoint t) { + video_store_data->recording = zm::chrono::duration_cast(t.time_since_epoch()); + } unsigned int GetPreEventCount() const { return pre_event_count; }; int32_t GetImageBufferCount() const { return image_buffer_count; };