Time: Remove DELTA_TIMEVAL macro and replace usage with proper std::chrono::duration operations
This commit is contained in:
parent
154b17d5f3
commit
7474294ac3
|
@ -237,35 +237,37 @@ Event::~Event() {
|
||||||
Warning("Empty endtime for event. Should not happen. Setting to now.");
|
Warning("Empty endtime for event. Should not happen. Setting to now.");
|
||||||
gettimeofday(&end_time, nullptr);
|
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,
|
Debug(2, "start_time: %" PRIi64 ".% " PRIi64 " end_time: %" PRIi64 ".%" PRIi64,
|
||||||
static_cast<int64>(start_time.tv_sec),
|
static_cast<int64>(start_time.tv_sec),
|
||||||
static_cast<int64>(start_time.tv_usec),
|
static_cast<int64>(start_time.tv_usec),
|
||||||
static_cast<int64>(end_time.tv_sec),
|
static_cast<int64>(end_time.tv_sec),
|
||||||
static_cast<int64>(end_time.tv_usec));
|
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
|
std::string sql = stringtf(
|
||||||
char sql[ZM_SQL_LGE_BUFSIZ];
|
"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'",
|
||||||
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'",
|
|
||||||
monitor->EventPrefix(), id, end_time.tv_sec,
|
monitor->EventPrefix(), id, end_time.tv_sec,
|
||||||
delta_time.positive?"":"-", delta_time.sec, delta_time.fsec,
|
delta_time.count(),
|
||||||
frames, alarm_frames,
|
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);
|
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.
|
// Name might have been changed during recording, so just do the update without changing the name.
|
||||||
snprintf(sql, sizeof(sql),
|
sql = stringtf(
|
||||||
"UPDATE Events SET EndDateTime = from_unixtime(%ld), Length = %s%ld.%02ld, Frames = %d, AlarmFrames = %d, TotScore = %d, AvgScore = %d, MaxScore = %d WHERE Id = %" PRIu64,
|
"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,
|
end_time.tv_sec,
|
||||||
delta_time.positive?"":"-", delta_time.sec, delta_time.fsec,
|
delta_time.count(),
|
||||||
frames, alarm_frames,
|
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);
|
id);
|
||||||
zmDbDoUpdate(sql);
|
zmDbDoUpdate(sql.c_str());
|
||||||
} // end if no changed rows due to Name change during recording
|
} // end if no changed rows due to Name change during recording
|
||||||
} // Event::~Event()
|
} // Event::~Event()
|
||||||
|
|
||||||
|
@ -578,53 +580,56 @@ void Event::AddFrame(
|
||||||
or ( monitor_state == Monitor::PREALARM );
|
or ( monitor_state == Monitor::PREALARM );
|
||||||
|
|
||||||
if (db_frame) {
|
if (db_frame) {
|
||||||
|
std::chrono::duration<double> delta_time =
|
||||||
struct DeltaTimeval delta_time;
|
zm::chrono::duration_cast<Microseconds>(timestamp) - zm::chrono::duration_cast<Microseconds>(start_time);
|
||||||
DELTA_TIMEVAL(delta_time, timestamp, start_time, DT_PREC_2);
|
Debug(1, "Frame delta is %" PRIi64 ".%" PRIi64 " - %" PRIi64 ".%" PRIi64 " = %.2f, score %u zone_stats.size %zu",
|
||||||
Debug(1, "Frame delta is %" PRIi64 ".%" PRIi64 " - %" PRIi64 ".%" PRIi64 " = %lu.%lu, score %u zone_stats.size %zu",
|
|
||||||
static_cast<int64>(start_time.tv_sec), static_cast<int64>(start_time.tv_usec),
|
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),
|
static_cast<int64>(timestamp.tv_sec), static_cast<int64>(timestamp.tv_usec),
|
||||||
delta_time.sec, delta_time.fsec,
|
delta_time.count(),
|
||||||
score,
|
score,
|
||||||
zone_stats.size());
|
zone_stats.size());
|
||||||
|
|
||||||
|
Milliseconds delta_time_ms = std::chrono::duration_cast<Milliseconds>(delta_time);
|
||||||
// The idea is to write out 1/sec
|
// 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();
|
double fps = monitor->get_capture_fps();
|
||||||
if ( write_to_db
|
if (write_to_db
|
||||||
or
|
or
|
||||||
( frame_data.size() >= MAX_DB_FRAMES )
|
(frame_data.size() >= MAX_DB_FRAMES)
|
||||||
or
|
or
|
||||||
( frame_type == BULK )
|
(frame_type == BULK)
|
||||||
or
|
or
|
||||||
( fps and (frame_data.size() > fps) )
|
(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)",
|
||||||
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));
|
||||||
frame_data.size(), write_to_db, fps, (frame_type == BULK));
|
|
||||||
WriteDbFrames();
|
WriteDbFrames();
|
||||||
last_db_frame = frames;
|
last_db_frame = frames;
|
||||||
|
|
||||||
char sql[ZM_SQL_MED_BUFSIZ];
|
std::string sql = stringtf(
|
||||||
snprintf(sql, sizeof(sql),
|
"UPDATE Events SET Length = %.2f, Frames = %d, AlarmFrames = %d, TotScore = %d, AvgScore = %d, MaxScore = %d WHERE Id = %" PRIu64,
|
||||||
"UPDATE Events SET Length = %s%ld.%02ld, Frames = %d, AlarmFrames = %d, TotScore = %d, AvgScore = %d, MaxScore = %d WHERE Id = %" PRIu64,
|
delta_time.count(),
|
||||||
( delta_time.positive?"":"-" ),
|
frames,
|
||||||
delta_time.sec, delta_time.fsec,
|
|
||||||
frames,
|
|
||||||
alarm_frames,
|
alarm_frames,
|
||||||
tot_score,
|
tot_score,
|
||||||
(int)(alarm_frames?(tot_score/alarm_frames):0),
|
static_cast<uint32>(alarm_frames ? (tot_score / alarm_frames) : 0),
|
||||||
max_score,
|
max_score,
|
||||||
id
|
id);
|
||||||
);
|
|
||||||
dbQueue.push(std::move(sql));
|
dbQueue.push(std::move(sql));
|
||||||
} else {
|
} else {
|
||||||
Debug(1, "Not Adding %zu frames to DB because write_to_db:%d or frames > analysis fps %f or BULK",
|
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);
|
frame_data.size(), write_to_db, fps);
|
||||||
} // end if frame_type == BULK
|
} // end if frame_type == BULK
|
||||||
} // end if db_frame
|
} // end if db_frame
|
||||||
|
|
||||||
if (score > (int)max_score)
|
if (score > (int) max_score) {
|
||||||
max_score = score;
|
max_score = score;
|
||||||
|
}
|
||||||
end_time = timestamp;
|
end_time = timestamp;
|
||||||
} // end void Event::AddFrame(Image *image, struct timeval timestamp, int score, Image *alarm_image)
|
} // end void Event::AddFrame(Image *image, struct timeval timestamp, int score, Image *alarm_image)
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@ Frame::Frame(event_id_t p_event_id,
|
||||||
int p_frame_id,
|
int p_frame_id,
|
||||||
FrameType p_type,
|
FrameType p_type,
|
||||||
struct timeval p_timestamp,
|
struct timeval p_timestamp,
|
||||||
struct DeltaTimeval &p_delta,
|
const DeltaTimeval &p_delta,
|
||||||
int p_score,
|
int p_score,
|
||||||
std::vector<ZoneStats> p_stats)
|
std::vector<ZoneStats> p_stats)
|
||||||
: event_id(p_event_id),
|
: event_id(p_event_id),
|
||||||
|
|
|
@ -42,7 +42,7 @@ class Frame {
|
||||||
int p_frame_id,
|
int p_frame_id,
|
||||||
FrameType p_type,
|
FrameType p_type,
|
||||||
struct timeval p_timestamp,
|
struct timeval p_timestamp,
|
||||||
struct DeltaTimeval &p_delta,
|
const DeltaTimeval &p_delta,
|
||||||
int p_score,
|
int p_score,
|
||||||
std::vector<ZoneStats> p_stats
|
std::vector<ZoneStats> p_stats
|
||||||
);
|
);
|
||||||
|
|
|
@ -391,12 +391,16 @@ bool MonitorStream::sendFrame(Image *image, const timeval ×tamp) {
|
||||||
fprintf(stdout, "Content-type: %s\r\n\r\n", vid_stream->MimeType());
|
fprintf(stdout, "Content-type: %s\r\n\r\n", vid_stream->MimeType());
|
||||||
vid_stream->OpenStream();
|
vid_stream->OpenStream();
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct timeval base_time;
|
static struct timeval base_time;
|
||||||
struct DeltaTimeval delta_time;
|
Milliseconds delta_time =
|
||||||
if ( !frame_count )
|
zm::chrono::duration_cast<Milliseconds>(timestamp) - zm::chrono::duration_cast<Milliseconds>(base_time);
|
||||||
|
|
||||||
|
if (!frame_count) {
|
||||||
base_time = timestamp;
|
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 {
|
} else {
|
||||||
static unsigned char temp_img_buffer[ZM_MAX_IMAGE_SIZE];
|
static unsigned char temp_img_buffer[ZM_MAX_IMAGE_SIZE];
|
||||||
|
|
||||||
|
|
|
@ -51,28 +51,6 @@ struct DeltaTimeval
|
||||||
|
|
||||||
#define DT_MAXGRAN DT_GRAN_1000000
|
#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 USEC_PER_SEC 1000000
|
||||||
#define MSEC_PER_SEC 1000
|
#define MSEC_PER_SEC 1000
|
||||||
|
|
||||||
|
|
13
src/zmc.cpp
13
src/zmc.cpp
|
@ -286,8 +286,7 @@ int main(int argc, char *argv[]) {
|
||||||
capture_delays[i], alarm_capture_delays[i]);
|
capture_delays[i], alarm_capture_delays[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct timeval now;
|
timeval now;
|
||||||
struct DeltaTimeval delta_time;
|
|
||||||
int sleep_time = 0;
|
int sleep_time = 0;
|
||||||
|
|
||||||
while (!zm_terminate) {
|
while (!zm_terminate) {
|
||||||
|
@ -319,19 +318,19 @@ int main(int argc, char *argv[]) {
|
||||||
if (delay) {
|
if (delay) {
|
||||||
gettimeofday(&now, nullptr);
|
gettimeofday(&now, nullptr);
|
||||||
if (last_capture_times[i].tv_sec) {
|
if (last_capture_times[i].tv_sec) {
|
||||||
// DT_PREC_3 means that the value will be in thousands of a second
|
Microseconds delta_time = zm::chrono::duration_cast<Microseconds>(now)
|
||||||
DELTA_TIMEVAL(delta_time, now, last_capture_times[i], DT_PREC_6);
|
- zm::chrono::duration_cast<Microseconds>(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.delta - sleep_time);
|
sleep_time = delay - (delta_time.count() - sleep_time);
|
||||||
Debug(4,
|
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,
|
sleep_time,
|
||||||
static_cast<int64>(now.tv_sec),
|
static_cast<int64>(now.tv_sec),
|
||||||
static_cast<int64>(now.tv_usec),
|
static_cast<int64>(now.tv_usec),
|
||||||
static_cast<int64>(last_capture_times[i].tv_sec),
|
static_cast<int64>(last_capture_times[i].tv_sec),
|
||||||
static_cast<int64>(last_capture_times[i].tv_usec),
|
static_cast<int64>(last_capture_times[i].tv_usec),
|
||||||
delta_time.delta,
|
static_cast<int64>(delta_time.count()),
|
||||||
delay);
|
delay);
|
||||||
|
|
||||||
if (sleep_time > 0) {
|
if (sleep_time > 0) {
|
||||||
|
|
Loading…
Reference in New Issue