Skip to content

Commit

Permalink
Merge pull request ZoneMinder#3283 from Carbenium/time-2
Browse files Browse the repository at this point in the history
Time: Remove defunct timeval methods and DeltaTimeval
  • Loading branch information
Carbenium authored Jun 7, 2021
2 parents 4615594 + d8d27bc commit 48b1e8a
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 258 deletions.
1 change: 0 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ set(ZM_BIN_SRC_FILES
zm_signal.cpp
zm_stream.cpp
zm_swscale.cpp
zm_time.cpp
zm_user.cpp
zm_utils.cpp
zm_videostore.cpp
Expand Down
26 changes: 9 additions & 17 deletions src/zm_event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ Event::~Event() {
gettimeofday(&end_time, nullptr);
}

std::chrono::duration<double> delta_time =
FPSeconds 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),
Expand Down Expand Up @@ -454,14 +454,12 @@ void Event::WriteDbFrames() {
while (frame_data.size()) {
Frame *frame = frame_data.front();
frame_data.pop();
frame_insert_sql += stringtf("\n( %" PRIu64 ", %d, '%s', from_unixtime( %ld ), %s%ld.%02ld, %d ),",
id, frame->frame_id,
frame_type_names[frame->type],
frame->timestamp.tv_sec,
frame->delta.positive ? "" : "-",
frame->delta.sec,
frame->delta.fsec,
frame->score);
frame_insert_sql += stringtf("\n( %" PRIu64 ", %d, '%s', from_unixtime( %ld ), %.2f, %d ),",
id, frame->frame_id,
frame_type_names[frame->type],
frame->timestamp.tv_sec,
std::chrono::duration_cast<FPSeconds>(frame->delta).count(),
frame->score);
if (config.record_event_stats and frame->zone_stats.size()) {
for (ZoneStats &stats : frame->zone_stats) {
stats_insert_sql += stringtf("\n(%" PRIu64 ",%d,%u,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%u),",
Expand Down Expand Up @@ -580,7 +578,7 @@ void Event::AddFrame(
or ( monitor_state == Monitor::PREALARM );

if (db_frame) {
std::chrono::duration<double> delta_time =
FPSeconds 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),
Expand All @@ -591,13 +589,7 @@ void Event::AddFrame(

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,
zm::chrono::duration_cast<DeltaTimeval, DT_PREC_3>(delta_time_ms),
score,
zone_stats));
frame_data.push(new Frame(id, frames, frame_type, timestamp, delta_time_ms, score, zone_stats));
double fps = monitor->get_capture_fps();
if (write_to_db
or
Expand Down
2 changes: 1 addition & 1 deletion src/zm_frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Frame::Frame(event_id_t p_event_id,
int p_frame_id,
FrameType p_type,
struct timeval p_timestamp,
const DeltaTimeval &p_delta,
Milliseconds p_delta,
int p_score,
std::vector<ZoneStats> p_stats)
: event_id(p_event_id),
Expand Down
4 changes: 2 additions & 2 deletions src/zm_frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Frame {
int p_frame_id,
FrameType p_type,
struct timeval p_timestamp,
const DeltaTimeval &p_delta,
Milliseconds p_delta,
int p_score,
std::vector<ZoneStats> p_stats
);
Expand All @@ -51,7 +51,7 @@ class Frame {
int frame_id;
FrameType type;
struct timeval timestamp;
struct DeltaTimeval delta;
Milliseconds delta;
int score;
std::vector<ZoneStats> zone_stats;
};
Expand Down
8 changes: 4 additions & 4 deletions src/zm_monitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -484,8 +484,8 @@ void Monitor::Load(MYSQL_ROW dbrow, bool load_zones=true, Purpose p = QUERY) {
/* "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(DT_PREC_6/atof(dbrow[col])):0; col++;
alarm_capture_delay = (dbrow[col] && atof(dbrow[col])>0.0)?int(DT_PREC_6/atof(dbrow[col])):0; col++;
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++;

/* "Device, Channel, Format, V4LMultiBuffer, V4LCapturesPerFrame, " // V4L Settings */
device = dbrow[col] ? dbrow[col] : ""; col++;
Expand Down Expand Up @@ -2963,8 +2963,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 : %d\n", section_length);
sprintf(output+strlen(output), "Min Section Length : %d\n", min_section_length);
sprintf(output+strlen(output), "Maximum FPS : %.2f\n", capture_delay?(double)DT_PREC_3/capture_delay:0.0);
sprintf(output+strlen(output), "Alarm Maximum FPS : %.2f\n", alarm_capture_delay?(double)DT_PREC_3/alarm_capture_delay:0.0);
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), "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);
Expand Down
29 changes: 14 additions & 15 deletions src/zm_monitorstream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,7 @@ bool MonitorStream::sendFrame(const char *filepath, const timeval &timestamp) {
}

// Calculate how long it takes to actually send the frame
struct timeval frameStartTime;
gettimeofday(&frameStartTime, nullptr);
TimePoint send_start_time = std::chrono::steady_clock::now();

if (
(0 > fprintf(stdout, "Content-Length: %d\r\nX-Timestamp: %d.%06d\r\n\r\n",
Expand All @@ -363,13 +362,14 @@ bool MonitorStream::sendFrame(const char *filepath, const timeval &timestamp) {
fputs("\r\n", stdout);
fflush(stdout);

struct timeval frameEndTime;
gettimeofday(&frameEndTime, nullptr);
TimePoint send_end_time = std::chrono::steady_clock::now();
Milliseconds frame_send_time = std::chrono::duration_cast<Milliseconds>(send_end_time - send_start_time);

int frameSendTime = tvDiffMsec(frameStartTime, frameEndTime);
if ( frameSendTime > 1000/maxfps ) {
if (frame_send_time > Milliseconds(lround(Milliseconds::period::den / maxfps))) {
maxfps /= 2;
Info("Frame send time %d msec too slow, throttling maxfps to %.2f", frameSendTime, maxfps);
Info("Frame send time %" PRIi64 " msec too slow, throttling maxfps to %.2f",
static_cast<int64>(frame_send_time.count()),
maxfps);
}

last_frame_sent = TV_2_FLOAT(now);
Expand Down Expand Up @@ -408,8 +408,7 @@ bool MonitorStream::sendFrame(Image *image, const timeval &timestamp) {
unsigned char *img_buffer = temp_img_buffer;

// Calculate how long it takes to actually send the frame
struct timeval frameStartTime;
gettimeofday(&frameStartTime, nullptr);
TimePoint send_start_time = std::chrono::steady_clock::now();

switch ( type ) {
case STREAM_JPEG :
Expand Down Expand Up @@ -451,14 +450,14 @@ bool MonitorStream::sendFrame(Image *image, const timeval &timestamp) {
fputs("\r\n", stdout);
fflush(stdout);

struct timeval frameEndTime;
gettimeofday(&frameEndTime, nullptr);
TimePoint send_end_time = std::chrono::steady_clock::now();
Milliseconds frame_send_time = std::chrono::duration_cast<Milliseconds>(send_end_time - send_start_time);

int frameSendTime = tvDiffMsec(frameStartTime, frameEndTime);
if ( frameSendTime > 1000/maxfps ) {
if (frame_send_time > Milliseconds(lround(Milliseconds::period::den / maxfps))) {
maxfps /= 1.5;
Warning("Frame send time %d msec too slow, throttling maxfps to %.2f",
frameSendTime, maxfps);
Warning("Frame send time %" PRIi64 " msec too slow, throttling maxfps to %.2f",
static_cast<int64>(frame_send_time.count()),
maxfps);
}
} // Not mpeg
last_frame_sent = TV_2_FLOAT(now);
Expand Down
27 changes: 18 additions & 9 deletions src/zm_rtp_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ RtpSource::RtpSource(
mRtpFactor = mRtpClock;

mBaseTimeReal = tvNow();
mBaseTimeNtp = tvZero();
mBaseTimeNtp = {};
mBaseTimeRtp = rtpTime;

mLastSrTimeReal = tvZero();
mLastSrTimeNtp = tvZero();
mLastSrTimeReal = {};
mLastSrTimeNtp = {};
mLastSrTimeRtp = 0;

if ( mCodecId != AV_CODEC_ID_H264 && mCodecId != AV_CODEC_ID_MPEG4 )
Expand Down Expand Up @@ -160,12 +160,18 @@ bool RtpSource::updateSeq(uint16_t seq) {

void RtpSource::updateJitter( const RtpDataHeader *header ) {
if ( mRtpFactor > 0 ) {
uint32_t localTimeRtp = mBaseTimeRtp + uint32_t(tvDiffSec(mBaseTimeReal) * mRtpFactor);
timeval now = {};
gettimeofday(&now, nullptr);

FPSeconds time_diff =
zm::chrono::duration_cast<Microseconds>(now) - zm::chrono::duration_cast<Microseconds>(mBaseTimeReal);

uint32_t localTimeRtp = mBaseTimeRtp + static_cast<uint32>(time_diff.count() * mRtpFactor);
uint32_t packetTransit = localTimeRtp - ntohl(header->timestampN);

Debug(5,
"Delta rtp = %.6f\n Local RTP time = %x Packet RTP time = %x Packet transit RTP time = %x",
tvDiffSec(mBaseTimeReal),
time_diff.count(),
localTimeRtp,
ntohl(header->timestampN),
packetTransit);
Expand All @@ -190,7 +196,8 @@ void RtpSource::updateRtcpData(
uint32_t ntpTimeSecs,
uint32_t ntpTimeFrac,
uint32_t rtpTime) {
struct timeval ntpTime = tvMake(ntpTimeSecs, suseconds_t((USEC_PER_SEC*(ntpTimeFrac>>16))/(1<<16)));
timeval ntpTime = zm::chrono::duration_cast<timeval>(
Seconds(ntpTimeSecs) + Microseconds((Microseconds::period::den * (ntpTimeFrac >> 16)) / (1 << 16)));

Debug(5, "ntpTime: %ld.%06ld, rtpTime: %x", ntpTime.tv_sec, ntpTime.tv_usec, rtpTime);

Expand All @@ -204,12 +211,14 @@ void RtpSource::updateRtcpData(
mLastSrTimeNtp.tv_sec, mLastSrTimeNtp.tv_usec, rtpTime,
ntpTime.tv_sec, ntpTime.tv_usec, rtpTime);

double diffNtpTime = tvDiffSec( mBaseTimeNtp, ntpTime );
FPSeconds diffNtpTime =
zm::chrono::duration_cast<Microseconds>(ntpTime) - zm::chrono::duration_cast<Microseconds>(mBaseTimeNtp);

uint32_t diffRtpTime = rtpTime - mBaseTimeRtp;
mRtpFactor = (uint32_t)(diffRtpTime / diffNtpTime);
mRtpFactor = static_cast<uint32>(diffRtpTime / diffNtpTime.count());

Debug( 5, "NTP-diff: %.6f RTP-diff: %d RTPfactor: %d",
diffNtpTime, diffRtpTime, mRtpFactor);
diffNtpTime.count(), diffRtpTime, mRtpFactor);
}
mLastSrTimeNtpSecs = ntpTimeSecs;
mLastSrTimeNtpFrac = ntpTimeFrac;
Expand Down
22 changes: 0 additions & 22 deletions src/zm_time.cpp

This file was deleted.

Loading

0 comments on commit 48b1e8a

Please sign in to comment.