nx_server_plugin_sdk 1.0 Server Plugin SDK

timer.h
1 // Copyright 2018-present Network Optix, Inc. Licensed under MPL 2.0: www.mozilla.org/MPL/2.0/
2
3 #ifndef RPI_TIMER_H
4 #define RPI_TIMER_H
5
6 #include <chrono>
7 #include <mutex>
8
9 namespace rpi_cam
10 {
12 class TimeCorrection
13 {
14 public:
15 typedef std::chrono::high_resolution_clock Clock;
16
17 static constexpr uint64_t MAX_DRIFT_US() { return 500 * 1000; }
18
19 TimeCorrection()
20 : m_baseUsec(0),
21 m_basePTS(0)
22 {}
23
24 uint64_t fixTime(uint64_t& ptsOld, uint64_t pts)
25 {
26 if (pts <= ptsOld || (pts - ptsOld) > MAX_DRIFT_US())
27 {
28 std::lock_guard<std::mutex> lock(m_mutex);
29
30 m_baseUsec = usecNow();
31 m_basePTS = pts;
32 }
33
34 ptsOld = pts;
35 return m_baseUsec + (pts - m_basePTS);
36 }
37
38 private:
39 mutable std::mutex m_mutex;
40 uint64_t m_baseUsec;
41 uint64_t m_basePTS;
42
43 static uint64_t usecNow()
44 {
45 return std::chrono::duration_cast<std::chrono::microseconds>(Clock::now().time_since_epoch()).count();
46 }
47 };
48 }
49
50 #endif
rpi_cam
Raspberry Pi camera unit and configuration.
Definition: camera_manager.cpp:21
rpi_cam::TimeCorrection
PTS to timestamps converter.
Definition: timer.h:12