| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 |
| 11 | #ifndef RTC_BASE_TIMESTAMP_ALIGNER_H_ |
| 12 | #define RTC_BASE_TIMESTAMP_ALIGNER_H_ |
| 13 |
| 14 | #include <stdint.h> |
| 15 |
| 16 | #include "rtc_base/system/rtc_export.h" |
| 17 | #include "rtc_base/time_utils.h" |
| 18 |
| 19 | namespace rtc { |
| 20 |
| 21 | // The TimestampAligner class helps translating timestamps of a capture system |
| 22 | // into the same timescale as is used by rtc::TimeMicros(). Some capture systems |
| 23 | // provide timestamps, which comes from the capturing hardware (camera or sound |
| 24 | // card) or stamped close to the capturing hardware. Such timestamps are more |
| 25 | // accurate (less jittery) than reading the system clock, but may have a |
| 26 | // different epoch and unknown clock drift. Frame timestamps in webrtc should |
| 27 | // use rtc::TimeMicros (system monotonic time), and this class provides a filter |
| 28 | // which lets us use the rtc::TimeMicros timescale, and at the same time take |
| 29 | // advantage of higher accuracy of the capturer's clock. |
| 30 |
| 31 | // This class is not thread safe, so all calls to it must be synchronized |
| 32 | // externally. |
| 33 | class RTC_EXPORT TimestampAligner { |
| 34 | public: |
| 35 | TimestampAligner(); |
| 36 | ~TimestampAligner(); |
| 37 |
| 38 | TimestampAligner(const TimestampAligner&) = delete; |
| 39 | TimestampAligner& operator=(const TimestampAligner&) = delete; |
| 40 |
| 41 | public: |
| 42 | // Minimum difference of two timestamps generated by |
| 43 | // "TranslateTimestamp(int64_t capturer_time_us, int64_t system_time_us)" |
| 44 | // This avoids the caller from getting two timestamps with the same |
| 45 | // millisecond. |
| 46 | static constexpr int64_t kMinFrameIntervalUs = rtc::kNumMicrosecsPerMillisec; |
| 47 |
| 48 | // Translates timestamps of a capture system to the same timescale as is used |
| 49 | // by rtc::TimeMicros(). `capturer_time_us` is assumed to be accurate, but |
| 50 | // with an unknown epoch and clock drift. `system_time_us` is |
| 51 | // time according to rtc::TimeMicros(), preferably read as soon as |
| 52 | // possible when the frame is captured. It may have poor accuracy |
| 53 | // due to poor resolution or scheduling delays. Returns the |
| 54 | // translated timestamp. |
| 55 | int64_t TranslateTimestamp(int64_t capturer_time_us, int64_t system_time_us); |
| 56 |
| 57 | // Returns the translated timestamp without updating the states. This is to |
| 58 | // allow TimestampAligner to translate capturer time into system clock based |
| 59 | // on earlier observations. It won't guarantee monotonicity. |
| 60 | int64_t TranslateTimestamp(int64_t capturer_time_us) const; |
| 61 |
| 62 | protected: |
| 63 | // Update the estimated offset between capturer's time and system monotonic |
| 64 | // time. |
| 65 | int64_t UpdateOffset(int64_t capturer_time_us, int64_t system_time_us); |
| 66 |
| 67 | // Clip timestamp, return value is always |
| 68 | // <= `system_time_us`, and |
| 69 | // >= min(`prev_translated_time_us_` + `kMinFrameIntervalUs`, |
| 70 | // `system_time_us`). |
| 71 | int64_t ClipTimestamp(int64_t filtered_time_us, int64_t system_time_us); |
| 72 |
| 73 | private: |
| 74 | // State for the timestamp translation. |
| 75 | int frames_seen_; |
| 76 | // Estimated offset between capturer's time and system monotonic time. |
| 77 | int64_t offset_us_; |
| 78 |
| 79 | // State for the ClipTimestamp method, applied after the filter. |
| 80 | // A large negative clock drift of the capturer tends to push translated |
| 81 | // timestamps into the future. `clip_bias_us_` is subtracted from the |
| 82 | // translated timestamps, to get them back from the future. |
| 83 | int64_t clip_bias_us_; |
| 84 | // Used to ensure that translated timestamps are monotonous. |
| 85 | int64_t prev_translated_time_us_; |
| 86 | // Offset between `prev_translated_time_us_` and the corresponding capturer |
| 87 | // time. |
| 88 | int64_t prev_time_offset_us_; |
| 89 | }; |
| 90 |
| 91 | } // namespace rtc |
| 92 |
| 93 | #endif // RTC_BASE_TIMESTAMP_ALIGNER_H_ |
| 94 |