Skip to content

Commit 32bfb2e

Browse files
committed
switch to monotonic clock
1 parent dc749cb commit 32bfb2e

File tree

6 files changed

+30
-30
lines changed

6 files changed

+30
-30
lines changed

src/IMPDeviceSource.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "IMPDeviceSource.hpp"
22
#include <iostream>
33
#include "GroupsockHelper.hh"
4+
#include "WorkerUtils.hpp"
45

56
// explicit instantiation
67
template class IMPDeviceSource<H264NALUnit, video_stream>;
@@ -75,10 +76,7 @@ void IMPDeviceSource<FrameType, Stream>::deliverFrame()
7576
fFrameSize = nal.data.size();
7677
}
7778

78-
/* timestamp fix, can be removed if solved
7979
fPresentationTime = nal.time;
80-
*/
81-
gettimeofday(&fPresentationTime, NULL);
8280

8381
memcpy(fTo, &nal.data[0], fFrameSize);
8482

src/JPEGWorker.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ void JPEGWorker::run()
9494
unsigned long long ms{0};
9595

9696
// Initialize timestamp for stats calculation (ensure it's set before first use)
97-
gettimeofday(&global_jpeg[jpgChn]->stream->stats.ts, NULL);
97+
WorkerUtils::getMonotonicTimeOfDay(&global_jpeg[jpgChn]->stream->stats.ts);
9898
global_jpeg[jpgChn]->stream->stats.ts.tv_sec -= 10;
9999

100100
while (global_jpeg[jpgChn]->running)
@@ -197,14 +197,14 @@ void JPEGWorker::run()
197197
&stream); // Release stream after saving
198198
}
199199

200-
ms = WorkerUtils::tDiffInMs(&global_jpeg[jpgChn]->stream->stats.ts);
200+
ms = WorkerUtils::getMonotonicTimeDiffInMs(&global_jpeg[jpgChn]->stream->stats.ts);
201201
if (ms > 1000)
202202
{
203203
global_jpeg[jpgChn]->stream->stats.fps = fps;
204204
global_jpeg[jpgChn]->stream->stats.bps = bps;
205205
fps = 0;
206206
bps = 0;
207-
gettimeofday(&global_jpeg[jpgChn]->stream->stats.ts, NULL);
207+
WorkerUtils::getMonotonicTimeOfDay(&global_jpeg[jpgChn]->stream->stats.ts);
208208

209209
LOG_DDEBUG("JPG " << jpgChn
210210
<< " fps: " << global_jpeg[jpgChn]->stream->stats.fps

src/VideoWorker.cpp

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,9 @@ void VideoWorker::run()
5454
continue;
5555
}
5656

57-
/* timestamp fix, can be removed if solved
58-
int64_t nal_ts = stream.pack[stream.packCount - 1].timestamp;
59-
struct timeval encoder_time;
60-
encoder_time.tv_sec = nal_ts / 1000000;
61-
encoder_time.tv_usec = nal_ts % 1000000;
62-
*/
57+
58+
struct timeval monotonic_time;
59+
WorkerUtils::getMonotonicTimeOfDay(&monotonic_time);
6360

6461
for (uint32_t i = 0; i < stream.packCount; ++i)
6562
{
@@ -76,12 +73,9 @@ void VideoWorker::run()
7673
uint8_t *start = (uint8_t *) stream.pack[i].virAddr;
7774
uint8_t *end = (uint8_t *) stream.pack[i].virAddr + stream.pack[i].length;
7875
#endif
79-
H264NALUnit nalu;
8076

81-
/* timestamp fix, can be removed if solved
82-
nalu.imp_ts = stream.pack[i].timestamp;
83-
nalu.time = encoder_time;
84-
*/
77+
H264NALUnit nalu;
78+
nalu.time = monotonic_time;
8579

8680
// We use start+4 because the encoder inserts 4-byte MPEG
8781
//'startcodes' at the beginning of each NAL. Live555 complains
@@ -155,7 +149,7 @@ void VideoWorker::run()
155149

156150
IMP_Encoder_ReleaseStream(encChn, &stream);
157151

158-
ms = WorkerUtils::tDiffInMs(&global_video[encChn]->stream->stats.ts);
152+
ms = WorkerUtils::getMonotonicTimeDiffInMs(&global_video[encChn]->stream->stats.ts);
159153
if (ms > 1000)
160154
{
161155
/* currently we write into osd and stream stats,
@@ -168,7 +162,7 @@ void VideoWorker::run()
168162

169163
fps = 0;
170164
bps = 0;
171-
gettimeofday(&global_video[encChn]->stream->stats.ts, NULL);
165+
WorkerUtils::getMonotonicTimeOfDay(&global_video[encChn]->stream->stats.ts);
172166
global_video[encChn]->stream->osd.stats.ts = global_video[encChn]
173167
->stream->stats.ts;
174168
/*

src/WorkerUtils.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
11
#include "WorkerUtils.hpp"
22

33
#include <cstddef>
4+
#include <time.h>
45

56
namespace WorkerUtils {
67

7-
unsigned long long tDiffInMs(struct timeval *startTime)
8+
void getMonotonicTimeOfDay(struct timeval *tv) {
9+
struct timespec ts;
10+
clock_gettime(CLOCK_MONOTONIC, &ts);
11+
tv->tv_sec = ts.tv_sec;
12+
tv->tv_usec = ts.tv_nsec / 1000;
13+
}
14+
15+
16+
unsigned long long getMonotonicTimeDiffInMs(struct timeval *startTime)
817
{
918
struct timeval currentTime;
10-
gettimeofday(&currentTime, NULL);
11-
12-
long seconds = currentTime.tv_sec - startTime->tv_sec;
13-
long microseconds = currentTime.tv_usec - startTime->tv_usec;
19+
getMonotonicTimeOfDay(&currentTime);
1420

15-
unsigned long long milliseconds = (seconds * 1000) + (microseconds / 1000);
21+
uint64_t start_us = (uint64_t)startTime->tv_sec * 1000000 + startTime->tv_usec;
22+
uint64_t current_us = (uint64_t)currentTime.tv_sec * 1000000 + currentTime.tv_usec;
1623

17-
return milliseconds;
24+
return (current_us - start_us) / 1000;
1825
}
1926

2027
} // namespace WorkerUtils

src/WorkerUtils.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@ struct StartHelper
1212
std::binary_semaphore has_started{0};
1313
};
1414

15+
// WorkerUtils provides utility functions that are shared across different worker classes.
16+
// This includes time-related functions that use a monotonic clock to avoid issues
17+
// with system time changes (e.g., from NTP).
1518
namespace WorkerUtils {
1619

17-
unsigned long long tDiffInMs(struct timeval *startTime);
20+
void getMonotonicTimeOfDay(struct timeval *tv);
21+
unsigned long long getMonotonicTimeDiffInMs(struct timeval *startTime);
1822

1923
} // namespace WorkerUtils
2024

src/globals.hpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,7 @@ struct AudioFrame
2929
struct H264NALUnit
3030
{
3131
std::vector<uint8_t> data;
32-
/* timestamp fix, can be removed if solved
3332
struct timeval time;
34-
int64_t imp_ts;
35-
*/
3633
};
3734

3835
struct BackchannelFrame

0 commit comments

Comments
 (0)