From f675afc3f28c75373c6f3e8d2e68b009f9686ea8 Mon Sep 17 00:00:00 2001 From: Peter Keresztes Schmidt Date: Sun, 13 Jun 2021 18:00:31 +0200 Subject: [PATCH] zmc: Convert internals to std::chrono --- src/zm_monitor.cpp | 20 +++++---- src/zm_monitor.h | 12 +++--- src/zm_packetqueue.cpp | 1 - src/zmc.cpp | 95 +++++++++++++++++++----------------------- 4 files changed, 62 insertions(+), 66 deletions(-) diff --git a/src/zm_monitor.cpp b/src/zm_monitor.cpp index 47d9d8459..08f458438 100644 --- a/src/zm_monitor.cpp +++ b/src/zm_monitor.cpp @@ -472,16 +472,22 @@ void Monitor::Load(MYSQL_ROW dbrow, bool load_zones=true, Purpose p = QUERY) { Debug(1, "Have camera type %s", CameraType_Strings[type].c_str()); col++; function = (Function)atoi(dbrow[col]); col++; - enabled = dbrow[col] ? atoi(dbrow[col]) : 0; col++; - decoding_enabled = dbrow[col] ? atoi(dbrow[col]) : 0; col++; + enabled = dbrow[col] ? atoi(dbrow[col]) : false; col++; + decoding_enabled = dbrow[col] ? atoi(dbrow[col]) : false; col++; ReloadLinkedMonitors(dbrow[col]); col++; /* "AnalysisFPSLimit, AnalysisUpdateDelay, MaxFPS, AlarmMaxFPS," */ analysis_fps_limit = dbrow[col] ? strtod(dbrow[col], nullptr) : 0.0; col++; - analysis_update_delay = strtoul(dbrow[col++], nullptr, 0); - capture_delay = (dbrow[col] && atof(dbrow[col]) > 0.0) ? int(Microseconds::period::den / atof(dbrow[col])) : 0; col++; - alarm_capture_delay = (dbrow[col] && atof(dbrow[col]) > 0.0) ? int(Microseconds::period::den / atof(dbrow[col])) : 0; col++; + analysis_update_delay = Seconds(strtoul(dbrow[col++], nullptr, 0)); + capture_delay = + (dbrow[col] && atof(dbrow[col]) > 0.0) ? std::chrono::duration_cast(FPSeconds(1 / atof(dbrow[col]))) + : Microseconds(0); + col++; + alarm_capture_delay = + (dbrow[col] && atof(dbrow[col]) > 0.0) ? std::chrono::duration_cast(FPSeconds(1 / atof(dbrow[col]))) + : Microseconds(0); + col++; /* "Device, Channel, Format, V4LMultiBuffer, V4LCapturesPerFrame, " // V4L Settings */ device = dbrow[col] ? dbrow[col] : ""; col++; @@ -2955,8 +2961,8 @@ bool Monitor::DumpSettings(char *output, bool verbose) { sprintf(output+strlen(output), "Alarm Frame Count : %d\n", alarm_frame_count ); sprintf(output+strlen(output), "Section Length : %" PRIi64 "\n", static_cast(Seconds(section_length).count())); sprintf(output+strlen(output), "Min Section Length : %" PRIi64 "\n", static_cast(Seconds(min_section_length).count())); - sprintf(output+strlen(output), "Maximum FPS : %.2f\n", capture_delay ? (double) Microseconds::period::den / capture_delay : 0.0); - sprintf(output+strlen(output), "Alarm Maximum FPS : %.2f\n", alarm_capture_delay ? (double) Microseconds::period::den / alarm_capture_delay : 0.0); + sprintf(output+strlen(output), "Maximum FPS : %.2f\n", capture_delay != Seconds(0) ? 1 / FPSeconds(capture_delay).count() : 0.0); + sprintf(output+strlen(output), "Alarm Maximum FPS : %.2f\n", alarm_capture_delay != Seconds(0) ? 1 / FPSeconds(alarm_capture_delay).count() : 0.0); sprintf(output+strlen(output), "Reference Blend %%ge : %d\n", ref_blend_perc); sprintf(output+strlen(output), "Alarm Reference Blend %%ge : %d\n", alarm_ref_blend_perc); sprintf(output+strlen(output), "Track Motion : %d\n", track_motion); diff --git a/src/zm_monitor.h b/src/zm_monitor.h index 88f872ed0..64638de62 100644 --- a/src/zm_monitor.h +++ b/src/zm_monitor.h @@ -319,9 +319,9 @@ protected: int frame_skip; // How many frames to skip in continuous modes int motion_frame_skip; // How many frames to skip in motion detection double analysis_fps_limit; // Target framerate for video analysis - unsigned int analysis_update_delay; // How long we wait before updating analysis parameters - int capture_delay; // How long we wait between capture frames - int alarm_capture_delay; // How long we wait between capture frames when in alarm state + Microseconds analysis_update_delay; // How long we wait before updating analysis parameters + Microseconds capture_delay; // How long we wait between capture frames + Microseconds alarm_capture_delay; // How long we wait between capture frames when in alarm state int alarm_frame_count; // How many alarm frames are required before an event is triggered int alert_to_alarm_frame_count; // How many alarm frames (consecutive score frames) are required to return alarm from alert // value for now is the same number configured in alarm_frame_count, maybe getting his own parameter some day @@ -538,10 +538,10 @@ public: SystemTimePoint GetTimestamp(int index = -1) const; void UpdateAdaptiveSkip(); useconds_t GetAnalysisRate(); - unsigned int GetAnalysisUpdateDelay() const { return analysis_update_delay; } + Microseconds GetAnalysisUpdateDelay() const { return analysis_update_delay; } unsigned int GetCaptureMaxFPS() const { return capture_max_fps; } - int GetCaptureDelay() const { return capture_delay; } - int GetAlarmCaptureDelay() const { return alarm_capture_delay; } + Microseconds GetCaptureDelay() const { return capture_delay; } + Microseconds GetAlarmCaptureDelay() const { return alarm_capture_delay; } unsigned int GetLastReadIndex() const; unsigned int GetLastWriteIndex() const; uint64_t GetLastEventId() const; diff --git a/src/zm_packetqueue.cpp b/src/zm_packetqueue.cpp index 9662e4958..f9221a61c 100644 --- a/src/zm_packetqueue.cpp +++ b/src/zm_packetqueue.cpp @@ -24,7 +24,6 @@ #include "zm_ffmpeg.h" #include "zm_packet.h" #include "zm_signal.h" -#include PacketQueue::PacketQueue(): video_stream_id(-1), diff --git a/src/zmc.cpp b/src/zmc.cpp index cdcaa66ef..9cb23dfba 100644 --- a/src/zmc.cpp +++ b/src/zmc.cpp @@ -64,8 +64,6 @@ possible, this should run at more or less constant speed. #include "zm_time.h" #include "zm_utils.h" #include -#include -#include void Usage() { fprintf(stderr, "zmc -d or -r -H -P -p or -f or -m \n"); @@ -214,7 +212,7 @@ int main(int argc, char *argv[]) { Error("No monitors found"); exit(-1); } else { - Debug(2, "%zu monitors loaded", monitors.size()); + Debug(2, "%zu monitors loaded", monitors.size()); } Info("Starting Capture version %s", ZM_VERSION); @@ -246,48 +244,47 @@ int main(int argc, char *argv[]) { monitor->SetStartupTime(now); monitor->SetHeartbeatTime(now); - snprintf(sql, sizeof(sql), + snprintf(sql, sizeof(sql), "INSERT INTO Monitor_Status (MonitorId,Status,CaptureFPS,AnalysisFPS)" " VALUES (%u, 'Running',0,0) ON DUPLICATE KEY UPDATE Status='Running',CaptureFPS=0,AnalysisFPS=0", monitor->Id()); zmDbDo(sql); - int sleep_time = 0; + Seconds sleep_time = Seconds(0); while (monitor->PrimeCapture() <= 0) { if (prime_capture_log_count % 60) { - logPrintf(Logger::ERROR+monitor->Importance(), - "Failed to prime capture of initial monitor"); + logPrintf(Logger::ERROR + monitor->Importance(), + "Failed to prime capture of initial monitor"); } else { Debug(1, "Failed to prime capture of initial monitor"); } - prime_capture_log_count ++; - if (zm_terminate) break; - if (sleep_time < 60) sleep_time++; - sleep(sleep_time); + + prime_capture_log_count++; + if (zm_terminate) { + break; + } + if (sleep_time < Seconds(60)) { + sleep_time++; + } + + std::this_thread::sleep_for(sleep_time); + } + if (zm_terminate){ + break; } - if (zm_terminate) break; snprintf(sql, sizeof(sql), "INSERT INTO Monitor_Status (MonitorId,Status) VALUES (%u, 'Connected') ON DUPLICATE KEY UPDATE Status='Connected'", monitor->Id()); zmDbDo(sql); } // end foreach monitor - if (zm_terminate) break; - int *capture_delays = new int[monitors.size()]; - int *alarm_capture_delays = new int[monitors.size()]; - struct timeval * last_capture_times = new struct timeval[monitors.size()]; - - for (size_t i = 0; i < monitors.size(); i++) { - last_capture_times[i].tv_sec = last_capture_times[i].tv_usec = 0; - capture_delays[i] = monitors[i]->GetCaptureDelay(); - alarm_capture_delays[i] = monitors[i]->GetAlarmCaptureDelay(); - Debug(2, "capture delay(%u mSecs 1000/capture_fps) alarm delay(%u)", - capture_delays[i], alarm_capture_delays[i]); + if (zm_terminate){ + break; } - timeval now; - int sleep_time = 0; + std::vector last_capture_times = std::vector(monitors.size()); + Microseconds sleep_time = Microseconds(0); while (!zm_terminate) { //sigprocmask(SIG_BLOCK, &block_set, 0); @@ -314,30 +311,28 @@ int main(int argc, char *argv[]) { } // capture_delay is the amount of time we should sleep in useconds to achieve the desired framerate. - int delay = (monitors[i]->GetState() == Monitor::ALARM) ? alarm_capture_delays[i] : capture_delays[i]; - if (delay) { - gettimeofday(&now, nullptr); - if (last_capture_times[i].tv_sec) { - Microseconds delta_time = zm::chrono::duration_cast(now) - - zm::chrono::duration_cast(last_capture_times[i]); + Microseconds delay = (monitors[i]->GetState() == Monitor::ALARM) ? monitors[i]->GetAlarmCaptureDelay() + : monitors[i]->GetCaptureDelay(); + if (delay != Seconds(0)) { + SystemTimePoint now = std::chrono::system_clock::now(); + if (last_capture_times[i].time_since_epoch() != Seconds(0)) { + Microseconds delta_time = std::chrono::duration_cast(now - last_capture_times[i]); // You have to add back in the previous sleep time - sleep_time = delay - (delta_time.count() - sleep_time); + sleep_time = delay - (delta_time - sleep_time); Debug(4, - "Sleep time is %d from now: %" PRIi64 ".%" PRIi64" last: %" PRIi64 ".% " PRIi64 " delta % " PRIi64 " delay: %d", - sleep_time, - static_cast(now.tv_sec), - static_cast(now.tv_usec), - static_cast(last_capture_times[i].tv_sec), - static_cast(last_capture_times[i].tv_usec), + "Sleep time is %" PRIi64 " from now: %.2f s last: %.2f s delta % " PRIi64 " us delay: %" PRIi64 " us", + static_cast(Microseconds(sleep_time).count()), + FPSeconds(now.time_since_epoch()).count(), + FPSeconds(last_capture_times[i].time_since_epoch()).count(), static_cast(delta_time.count()), - delay); + static_cast(Microseconds(delay).count())); - if (sleep_time > 0) { - Debug(4, "usleeping (%d)", sleep_time); - usleep(sleep_time); + if (sleep_time > Seconds(0)) { + std::this_thread::sleep_for(sleep_time); } } // end if has a last_capture time + last_capture_times[i] = now; } // end if delay } // end foreach n_monitors @@ -348,22 +343,18 @@ int main(int argc, char *argv[]) { } } // end while ! zm_terminate and connected - for (size_t i = 0; i < monitors.size(); i++) { - monitors[i]->Close(); - monitors[i]->disconnect(); + for (std::shared_ptr & monitor : monitors) { + monitor->Close(); + monitor->disconnect(); } - delete [] alarm_capture_delays; - delete [] capture_delays; - delete [] last_capture_times; - if (zm_reload) { for (std::shared_ptr &monitor : monitors) { monitor->Reload(); } logTerm(); logInit(log_id_string); - + zm_reload = false; } // end if zm_reload } // end while ! zm_terminate outer connection loop @@ -371,7 +362,7 @@ int main(int argc, char *argv[]) { for (std::shared_ptr &monitor : monitors) { static char sql[ZM_SQL_SML_BUFSIZ]; snprintf(sql, sizeof(sql), - "INSERT INTO Monitor_Status (MonitorId,Status) VALUES (%u, 'NotRunning') ON DUPLICATE KEY UPDATE Status='NotRunning'", + "INSERT INTO Monitor_Status (MonitorId,Status) VALUES (%u, 'NotRunning') ON DUPLICATE KEY UPDATE Status='NotRunning'", monitor->Id()); zmDbDo(sql); } @@ -382,5 +373,5 @@ int main(int argc, char *argv[]) { dbQueue.stop(); zmDbClose(); - return zm_terminate ? 0 : result; + return zm_terminate ? 0 : result; }