zmc: Convert internals to std::chrono
This commit is contained in:
parent
3e8b10d813
commit
f675afc3f2
|
@ -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());
|
Debug(1, "Have camera type %s", CameraType_Strings[type].c_str());
|
||||||
col++;
|
col++;
|
||||||
function = (Function)atoi(dbrow[col]); col++;
|
function = (Function)atoi(dbrow[col]); col++;
|
||||||
enabled = dbrow[col] ? atoi(dbrow[col]) : 0; col++;
|
enabled = dbrow[col] ? atoi(dbrow[col]) : false; col++;
|
||||||
decoding_enabled = dbrow[col] ? atoi(dbrow[col]) : 0; col++;
|
decoding_enabled = dbrow[col] ? atoi(dbrow[col]) : false; col++;
|
||||||
|
|
||||||
ReloadLinkedMonitors(dbrow[col]); col++;
|
ReloadLinkedMonitors(dbrow[col]); col++;
|
||||||
|
|
||||||
/* "AnalysisFPSLimit, AnalysisUpdateDelay, MaxFPS, AlarmMaxFPS," */
|
/* "AnalysisFPSLimit, AnalysisUpdateDelay, MaxFPS, AlarmMaxFPS," */
|
||||||
analysis_fps_limit = dbrow[col] ? strtod(dbrow[col], nullptr) : 0.0; col++;
|
analysis_fps_limit = dbrow[col] ? strtod(dbrow[col], nullptr) : 0.0; col++;
|
||||||
analysis_update_delay = strtoul(dbrow[col++], nullptr, 0);
|
analysis_update_delay = Seconds(strtoul(dbrow[col++], nullptr, 0));
|
||||||
capture_delay = (dbrow[col] && atof(dbrow[col]) > 0.0) ? int(Microseconds::period::den / atof(dbrow[col])) : 0; col++;
|
capture_delay =
|
||||||
alarm_capture_delay = (dbrow[col] && atof(dbrow[col]) > 0.0) ? int(Microseconds::period::den / atof(dbrow[col])) : 0; col++;
|
(dbrow[col] && atof(dbrow[col]) > 0.0) ? std::chrono::duration_cast<Microseconds>(FPSeconds(1 / atof(dbrow[col])))
|
||||||
|
: Microseconds(0);
|
||||||
|
col++;
|
||||||
|
alarm_capture_delay =
|
||||||
|
(dbrow[col] && atof(dbrow[col]) > 0.0) ? std::chrono::duration_cast<Microseconds>(FPSeconds(1 / atof(dbrow[col])))
|
||||||
|
: Microseconds(0);
|
||||||
|
col++;
|
||||||
|
|
||||||
/* "Device, Channel, Format, V4LMultiBuffer, V4LCapturesPerFrame, " // V4L Settings */
|
/* "Device, Channel, Format, V4LMultiBuffer, V4LCapturesPerFrame, " // V4L Settings */
|
||||||
device = dbrow[col] ? dbrow[col] : ""; col++;
|
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), "Alarm Frame Count : %d\n", alarm_frame_count );
|
||||||
sprintf(output+strlen(output), "Section Length : %" PRIi64 "\n", static_cast<int64>(Seconds(section_length).count()));
|
sprintf(output+strlen(output), "Section Length : %" PRIi64 "\n", static_cast<int64>(Seconds(section_length).count()));
|
||||||
sprintf(output+strlen(output), "Min Section Length : %" PRIi64 "\n", static_cast<int64>(Seconds(min_section_length).count()));
|
sprintf(output+strlen(output), "Min Section Length : %" PRIi64 "\n", static_cast<int64>(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), "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 ? (double) Microseconds::period::den / alarm_capture_delay : 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), "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), "Alarm Reference Blend %%ge : %d\n", alarm_ref_blend_perc);
|
||||||
sprintf(output+strlen(output), "Track Motion : %d\n", track_motion);
|
sprintf(output+strlen(output), "Track Motion : %d\n", track_motion);
|
||||||
|
|
|
@ -319,9 +319,9 @@ protected:
|
||||||
int frame_skip; // How many frames to skip in continuous modes
|
int frame_skip; // How many frames to skip in continuous modes
|
||||||
int motion_frame_skip; // How many frames to skip in motion detection
|
int motion_frame_skip; // How many frames to skip in motion detection
|
||||||
double analysis_fps_limit; // Target framerate for video analysis
|
double analysis_fps_limit; // Target framerate for video analysis
|
||||||
unsigned int analysis_update_delay; // How long we wait before updating analysis parameters
|
Microseconds analysis_update_delay; // How long we wait before updating analysis parameters
|
||||||
int capture_delay; // How long we wait between capture frames
|
Microseconds 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 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 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
|
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
|
// 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;
|
SystemTimePoint GetTimestamp(int index = -1) const;
|
||||||
void UpdateAdaptiveSkip();
|
void UpdateAdaptiveSkip();
|
||||||
useconds_t GetAnalysisRate();
|
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; }
|
unsigned int GetCaptureMaxFPS() const { return capture_max_fps; }
|
||||||
int GetCaptureDelay() const { return capture_delay; }
|
Microseconds GetCaptureDelay() const { return capture_delay; }
|
||||||
int GetAlarmCaptureDelay() const { return alarm_capture_delay; }
|
Microseconds GetAlarmCaptureDelay() const { return alarm_capture_delay; }
|
||||||
unsigned int GetLastReadIndex() const;
|
unsigned int GetLastReadIndex() const;
|
||||||
unsigned int GetLastWriteIndex() const;
|
unsigned int GetLastWriteIndex() const;
|
||||||
uint64_t GetLastEventId() const;
|
uint64_t GetLastEventId() const;
|
||||||
|
|
|
@ -24,7 +24,6 @@
|
||||||
#include "zm_ffmpeg.h"
|
#include "zm_ffmpeg.h"
|
||||||
#include "zm_packet.h"
|
#include "zm_packet.h"
|
||||||
#include "zm_signal.h"
|
#include "zm_signal.h"
|
||||||
#include <sys/time.h>
|
|
||||||
|
|
||||||
PacketQueue::PacketQueue():
|
PacketQueue::PacketQueue():
|
||||||
video_stream_id(-1),
|
video_stream_id(-1),
|
||||||
|
|
79
src/zmc.cpp
79
src/zmc.cpp
|
@ -64,8 +64,6 @@ possible, this should run at more or less constant speed.
|
||||||
#include "zm_time.h"
|
#include "zm_time.h"
|
||||||
#include "zm_utils.h"
|
#include "zm_utils.h"
|
||||||
#include <getopt.h>
|
#include <getopt.h>
|
||||||
#include <iostream>
|
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
void Usage() {
|
void Usage() {
|
||||||
fprintf(stderr, "zmc -d <device_path> or -r <proto> -H <host> -P <port> -p <path> or -f <file_path> or -m <monitor_id>\n");
|
fprintf(stderr, "zmc -d <device_path> or -r <proto> -H <host> -P <port> -p <path> or -f <file_path> or -m <monitor_id>\n");
|
||||||
|
@ -252,7 +250,7 @@ int main(int argc, char *argv[]) {
|
||||||
monitor->Id());
|
monitor->Id());
|
||||||
zmDbDo(sql);
|
zmDbDo(sql);
|
||||||
|
|
||||||
int sleep_time = 0;
|
Seconds sleep_time = Seconds(0);
|
||||||
while (monitor->PrimeCapture() <= 0) {
|
while (monitor->PrimeCapture() <= 0) {
|
||||||
if (prime_capture_log_count % 60) {
|
if (prime_capture_log_count % 60) {
|
||||||
logPrintf(Logger::ERROR + monitor->Importance(),
|
logPrintf(Logger::ERROR + monitor->Importance(),
|
||||||
|
@ -260,34 +258,33 @@ int main(int argc, char *argv[]) {
|
||||||
} else {
|
} else {
|
||||||
Debug(1, "Failed to prime capture of initial monitor");
|
Debug(1, "Failed to prime capture of initial monitor");
|
||||||
}
|
}
|
||||||
|
|
||||||
prime_capture_log_count++;
|
prime_capture_log_count++;
|
||||||
if (zm_terminate) break;
|
if (zm_terminate) {
|
||||||
if (sleep_time < 60) sleep_time++;
|
break;
|
||||||
sleep(sleep_time);
|
}
|
||||||
|
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),
|
snprintf(sql, sizeof(sql),
|
||||||
"INSERT INTO Monitor_Status (MonitorId,Status) VALUES (%u, 'Connected') ON DUPLICATE KEY UPDATE Status='Connected'",
|
"INSERT INTO Monitor_Status (MonitorId,Status) VALUES (%u, 'Connected') ON DUPLICATE KEY UPDATE Status='Connected'",
|
||||||
monitor->Id());
|
monitor->Id());
|
||||||
zmDbDo(sql);
|
zmDbDo(sql);
|
||||||
} // end foreach monitor
|
} // end foreach monitor
|
||||||
if (zm_terminate) break;
|
|
||||||
|
|
||||||
int *capture_delays = new int[monitors.size()];
|
if (zm_terminate){
|
||||||
int *alarm_capture_delays = new int[monitors.size()];
|
break;
|
||||||
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]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
timeval now;
|
std::vector<SystemTimePoint> last_capture_times = std::vector<SystemTimePoint>(monitors.size());
|
||||||
int sleep_time = 0;
|
Microseconds sleep_time = Microseconds(0);
|
||||||
|
|
||||||
while (!zm_terminate) {
|
while (!zm_terminate) {
|
||||||
//sigprocmask(SIG_BLOCK, &block_set, 0);
|
//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.
|
// 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];
|
Microseconds delay = (monitors[i]->GetState() == Monitor::ALARM) ? monitors[i]->GetAlarmCaptureDelay()
|
||||||
if (delay) {
|
: monitors[i]->GetCaptureDelay();
|
||||||
gettimeofday(&now, nullptr);
|
if (delay != Seconds(0)) {
|
||||||
if (last_capture_times[i].tv_sec) {
|
SystemTimePoint now = std::chrono::system_clock::now();
|
||||||
Microseconds delta_time = zm::chrono::duration_cast<Microseconds>(now)
|
if (last_capture_times[i].time_since_epoch() != Seconds(0)) {
|
||||||
- zm::chrono::duration_cast<Microseconds>(last_capture_times[i]);
|
Microseconds delta_time = std::chrono::duration_cast<Microseconds>(now - last_capture_times[i]);
|
||||||
|
|
||||||
// You have to add back in the previous sleep time
|
// 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,
|
Debug(4,
|
||||||
"Sleep time is %d from now: %" PRIi64 ".%" PRIi64" last: %" PRIi64 ".% " PRIi64 " delta % " PRIi64 " delay: %d",
|
"Sleep time is %" PRIi64 " from now: %.2f s last: %.2f s delta % " PRIi64 " us delay: %" PRIi64 " us",
|
||||||
sleep_time,
|
static_cast<int64>(Microseconds(sleep_time).count()),
|
||||||
static_cast<int64>(now.tv_sec),
|
FPSeconds(now.time_since_epoch()).count(),
|
||||||
static_cast<int64>(now.tv_usec),
|
FPSeconds(last_capture_times[i].time_since_epoch()).count(),
|
||||||
static_cast<int64>(last_capture_times[i].tv_sec),
|
|
||||||
static_cast<int64>(last_capture_times[i].tv_usec),
|
|
||||||
static_cast<int64>(delta_time.count()),
|
static_cast<int64>(delta_time.count()),
|
||||||
delay);
|
static_cast<int64>(Microseconds(delay).count()));
|
||||||
|
|
||||||
if (sleep_time > 0) {
|
if (sleep_time > Seconds(0)) {
|
||||||
Debug(4, "usleeping (%d)", sleep_time);
|
std::this_thread::sleep_for(sleep_time);
|
||||||
usleep(sleep_time);
|
|
||||||
}
|
}
|
||||||
} // end if has a last_capture time
|
} // end if has a last_capture time
|
||||||
|
|
||||||
last_capture_times[i] = now;
|
last_capture_times[i] = now;
|
||||||
} // end if delay
|
} // end if delay
|
||||||
} // end foreach n_monitors
|
} // end foreach n_monitors
|
||||||
|
@ -348,15 +343,11 @@ int main(int argc, char *argv[]) {
|
||||||
}
|
}
|
||||||
} // end while ! zm_terminate and connected
|
} // end while ! zm_terminate and connected
|
||||||
|
|
||||||
for (size_t i = 0; i < monitors.size(); i++) {
|
for (std::shared_ptr<Monitor> & monitor : monitors) {
|
||||||
monitors[i]->Close();
|
monitor->Close();
|
||||||
monitors[i]->disconnect();
|
monitor->disconnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
delete [] alarm_capture_delays;
|
|
||||||
delete [] capture_delays;
|
|
||||||
delete [] last_capture_times;
|
|
||||||
|
|
||||||
if (zm_reload) {
|
if (zm_reload) {
|
||||||
for (std::shared_ptr<Monitor> &monitor : monitors) {
|
for (std::shared_ptr<Monitor> &monitor : monitors) {
|
||||||
monitor->Reload();
|
monitor->Reload();
|
||||||
|
|
Loading…
Reference in New Issue