Time: Remove DELTA_TIMEVAL macro and replace usage with proper std::chrono::duration operations

This commit is contained in:
Peter Keresztes Schmidt 2021-06-06 15:13:09 +02:00
parent 154b17d5f3
commit 7474294ac3
6 changed files with 65 additions and 79 deletions

View File

@ -237,35 +237,37 @@ Event::~Event() {
Warning("Empty endtime for event. Should not happen. Setting to now.");
gettimeofday(&end_time, nullptr);
}
struct DeltaTimeval delta_time;
DELTA_TIMEVAL(delta_time, end_time, start_time, DT_PREC_2);
std::chrono::duration<double> delta_time =
zm::chrono::duration_cast<Microseconds>(end_time) - zm::chrono::duration_cast<Microseconds>(start_time);
Debug(2, "start_time: %" PRIi64 ".% " PRIi64 " end_time: %" PRIi64 ".%" PRIi64,
static_cast<int64>(start_time.tv_sec),
static_cast<int64>(start_time.tv_usec),
static_cast<int64>(end_time.tv_sec),
static_cast<int64>(end_time.tv_usec));
if (frame_data.size()) WriteDbFrames();
if (frame_data.size()){
WriteDbFrames();
}
// Should not be static because we might be multi-threaded
char sql[ZM_SQL_LGE_BUFSIZ];
snprintf(sql, sizeof(sql),
"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'",
std::string sql = stringtf(
"UPDATE Events SET Name='%s%" PRIu64 "', EndDateTime = from_unixtime(%ld), Length = %.2f, Frames = %d, AlarmFrames = %d, TotScore = %d, AvgScore = %d, MaxScore = %d WHERE Id = %" PRIu64 " AND Name='New Event'",
monitor->EventPrefix(), id, end_time.tv_sec,
delta_time.positive?"":"-", delta_time.sec, delta_time.fsec,
delta_time.count(),
frames, alarm_frames,
tot_score, (int)(alarm_frames?(tot_score/alarm_frames):0), max_score,
tot_score, static_cast<uint32>(alarm_frames ? (tot_score / alarm_frames) : 0), max_score,
id);
if (!zmDbDoUpdate(sql)) {
if (!zmDbDoUpdate(sql.c_str())) {
// 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,
sql = stringtf(
"UPDATE Events SET EndDateTime = from_unixtime(%ld), Length = %.2f, 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,
delta_time.count(),
frames, alarm_frames,
tot_score, (int)(alarm_frames?(tot_score/alarm_frames):0), max_score,
tot_score, static_cast<uint32>(alarm_frames ? (tot_score / alarm_frames) : 0), max_score,
id);
zmDbDoUpdate(sql);
zmDbDoUpdate(sql.c_str());
} // end if no changed rows due to Name change during recording
} // Event::~Event()
@ -578,53 +580,56 @@ void Event::AddFrame(
or ( monitor_state == Monitor::PREALARM );
if (db_frame) {
struct DeltaTimeval delta_time;
DELTA_TIMEVAL(delta_time, timestamp, start_time, DT_PREC_2);
Debug(1, "Frame delta is %" PRIi64 ".%" PRIi64 " - %" PRIi64 ".%" PRIi64 " = %lu.%lu, score %u zone_stats.size %zu",
std::chrono::duration<double> delta_time =
zm::chrono::duration_cast<Microseconds>(timestamp) - zm::chrono::duration_cast<Microseconds>(start_time);
Debug(1, "Frame delta is %" PRIi64 ".%" PRIi64 " - %" PRIi64 ".%" PRIi64 " = %.2f, score %u zone_stats.size %zu",
static_cast<int64>(start_time.tv_sec), static_cast<int64>(start_time.tv_usec),
static_cast<int64>(timestamp.tv_sec), static_cast<int64>(timestamp.tv_usec),
delta_time.sec, delta_time.fsec,
delta_time.count(),
score,
zone_stats.size());
Milliseconds delta_time_ms = std::chrono::duration_cast<Milliseconds>(delta_time);
// The idea is to write out 1/sec
frame_data.push(new Frame(id, frames, frame_type, timestamp, delta_time, score, zone_stats));
frame_data.push(new Frame(id,
frames,
frame_type,
timestamp,
zm::chrono::duration_cast<DeltaTimeval, DT_PREC_3>(delta_time_ms),
score,
zone_stats));
double fps = monitor->get_capture_fps();
if ( write_to_db
or
( frame_data.size() >= MAX_DB_FRAMES )
or
( frame_type == BULK )
or
( fps and (frame_data.size() > fps) )
) {
Debug(1, "Adding %zu 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));
if (write_to_db
or
(frame_data.size() >= MAX_DB_FRAMES)
or
(frame_type == BULK)
or
(fps and (frame_data.size() > fps))) {
Debug(1, "Adding %zu 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));
WriteDbFrames();
last_db_frame = frames;
char sql[ZM_SQL_MED_BUFSIZ];
snprintf(sql, sizeof(sql),
"UPDATE Events SET Length = %s%ld.%02ld, Frames = %d, AlarmFrames = %d, TotScore = %d, AvgScore = %d, MaxScore = %d WHERE Id = %" PRIu64,
( delta_time.positive?"":"-" ),
delta_time.sec, delta_time.fsec,
frames,
std::string sql = stringtf(
"UPDATE Events SET Length = %.2f, Frames = %d, AlarmFrames = %d, TotScore = %d, AvgScore = %d, MaxScore = %d WHERE Id = %" PRIu64,
delta_time.count(),
frames,
alarm_frames,
tot_score,
(int)(alarm_frames?(tot_score/alarm_frames):0),
static_cast<uint32>(alarm_frames ? (tot_score / alarm_frames) : 0),
max_score,
id
);
id);
dbQueue.push(std::move(sql));
} else {
Debug(1, "Not Adding %zu frames to DB because write_to_db:%d or frames > analysis fps %f or BULK",
frame_data.size(), write_to_db, fps);
} else {
Debug(1, "Not Adding %zu frames to DB because write_to_db:%d or frames > analysis fps %f or BULK",
frame_data.size(), write_to_db, fps);
} // end if frame_type == BULK
} // end if db_frame
if (score > (int)max_score)
if (score > (int) max_score) {
max_score = score;
}
end_time = timestamp;
} // end void Event::AddFrame(Image *image, struct timeval timestamp, int score, Image *alarm_image)

View File

@ -4,7 +4,7 @@ Frame::Frame(event_id_t p_event_id,
int p_frame_id,
FrameType p_type,
struct timeval p_timestamp,
struct DeltaTimeval &p_delta,
const DeltaTimeval &p_delta,
int p_score,
std::vector<ZoneStats> p_stats)
: event_id(p_event_id),

View File

@ -42,7 +42,7 @@ class Frame {
int p_frame_id,
FrameType p_type,
struct timeval p_timestamp,
struct DeltaTimeval &p_delta,
const DeltaTimeval &p_delta,
int p_score,
std::vector<ZoneStats> p_stats
);

View File

@ -391,12 +391,16 @@ bool MonitorStream::sendFrame(Image *image, const timeval &timestamp) {
fprintf(stdout, "Content-type: %s\r\n\r\n", vid_stream->MimeType());
vid_stream->OpenStream();
}
static struct timeval base_time;
struct DeltaTimeval delta_time;
if ( !frame_count )
Milliseconds delta_time =
zm::chrono::duration_cast<Milliseconds>(timestamp) - zm::chrono::duration_cast<Milliseconds>(base_time);
if (!frame_count) {
base_time = timestamp;
DELTA_TIMEVAL(delta_time, timestamp, base_time, DT_PREC_3);
/* double pts = */ vid_stream->EncodeFrame(send_image->Buffer(), send_image->Size(), config.mpeg_timed_frames, delta_time.delta);
}
/* double pts = */ vid_stream->EncodeFrame(send_image->Buffer(), send_image->Size(), config.mpeg_timed_frames, delta_time.count());
} else {
static unsigned char temp_img_buffer[ZM_MAX_IMAGE_SIZE];

View File

@ -51,28 +51,6 @@ struct DeltaTimeval
#define DT_MAXGRAN DT_GRAN_1000000
// This obviously wouldn't work for massive deltas but as it's mostly
// for frames it will only usually be a fraction of a second or so
#define DELTA_TIMEVAL( result, time1, time2, precision ) \
{ \
int delta = (((time1).tv_sec-(time2).tv_sec)*(precision))+(((time1).tv_usec-(time2).tv_usec)/(DT_MAXGRAN/(precision))); \
result.positive = (delta>=0); \
result.delta = abs(delta); \
result.sec = result.delta/(precision); \
result.fsec = result.delta%(precision); \
result.prec = (precision); \
}
#define TIMEVAL_INTERVAL( result, time1, time2, precision ) \
{ \
int delta = (((time1).tv_sec-(time2).tv_sec)*(precision))+(((time1).tv_usec-(time2).tv_usec)/(DT_MAXGRAN/(precision))); \
result.positive = (delta>=0); \
result.delta = abs(delta); \
result.sec = result.delta/(precision); \
result.fsec = result.delta%(precision); \
result.prec = (precision); \
}
#define USEC_PER_SEC 1000000
#define MSEC_PER_SEC 1000

View File

@ -286,8 +286,7 @@ int main(int argc, char *argv[]) {
capture_delays[i], alarm_capture_delays[i]);
}
struct timeval now;
struct DeltaTimeval delta_time;
timeval now;
int sleep_time = 0;
while (!zm_terminate) {
@ -319,19 +318,19 @@ int main(int argc, char *argv[]) {
if (delay) {
gettimeofday(&now, nullptr);
if (last_capture_times[i].tv_sec) {
// DT_PREC_3 means that the value will be in thousands of a second
DELTA_TIMEVAL(delta_time, now, last_capture_times[i], DT_PREC_6);
Microseconds delta_time = zm::chrono::duration_cast<Microseconds>(now)
- zm::chrono::duration_cast<Microseconds>(last_capture_times[i]);
// You have to add back in the previous sleep time
sleep_time = delay - (delta_time.delta - sleep_time);
sleep_time = delay - (delta_time.count() - sleep_time);
Debug(4,
"Sleep time is %d from now: %" PRIi64 ".%" PRIi64" last: %" PRIi64 ".% " PRIi64 " delta %lu delay: %d",
"Sleep time is %d from now: %" PRIi64 ".%" PRIi64" last: %" PRIi64 ".% " PRIi64 " delta % " PRIi64 " delay: %d",
sleep_time,
static_cast<int64>(now.tv_sec),
static_cast<int64>(now.tv_usec),
static_cast<int64>(last_capture_times[i].tv_sec),
static_cast<int64>(last_capture_times[i].tv_usec),
delta_time.delta,
static_cast<int64>(delta_time.count()),
delay);
if (sleep_time > 0) {