blob: 138e936af20bb2b09424ec2503d7be0ea7ca1c77 [file] [log] [blame]
nisse191b3592016-06-22 08:36:53 -07001/*
2 * Copyright (c) 2016 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
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
Steve Anton10542f22019-01-11 09:11:00 -080011#ifndef RTC_BASE_TIMESTAMP_ALIGNER_H_
12#define RTC_BASE_TIMESTAMP_ALIGNER_H_
nisse191b3592016-06-22 08:36:53 -070013
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020014#include <stdint.h>
pbosc7c26a02017-01-02 08:42:32 -080015
Mirko Bonadei35214fc2019-09-23 14:54:28 +020016#include "rtc_base/system/rtc_export.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020017
18namespace rtc {
19
Minyue Li37e388a2020-03-05 11:16:19 +010020// The TimestampAligner class helps translating timestamps of a capture system
21// into the same timescale as is used by rtc::TimeMicros(). Some capture systems
22// provide timestamps, which comes from the capturing hardware (camera or sound
23// card) or stamped close to the capturing hardware. Such timestamps are more
24// accurate (less jittery) than reading the system clock, but may have a
25// different epoch and unknown clock drift. Frame timestamps in webrtc should
26// use rtc::TimeMicros (system monotonic time), and this class provides a filter
27// which lets us use the rtc::TimeMicros timescale, and at the same time take
28// advantage of higher accuracy of the capturer's clock.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020029
30// This class is not thread safe, so all calls to it must be synchronized
31// externally.
Mirko Bonadei35214fc2019-09-23 14:54:28 +020032class RTC_EXPORT TimestampAligner {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020033 public:
34 TimestampAligner();
35 ~TimestampAligner();
36
Byoungchan Lee14af7622022-01-12 05:24:58 +090037 TimestampAligner(const TimestampAligner&) = delete;
38 TimestampAligner& operator=(const TimestampAligner&) = delete;
39
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020040 public:
Minyue Li37e388a2020-03-05 11:16:19 +010041 // Translates timestamps of a capture system to the same timescale as is used
Artem Titov96e3b992021-07-26 16:03:14 +020042 // by rtc::TimeMicros(). `capturer_time_us` is assumed to be accurate, but
43 // with an unknown epoch and clock drift. `system_time_us` is
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020044 // time according to rtc::TimeMicros(), preferably read as soon as
45 // possible when the frame is captured. It may have poor accuracy
46 // due to poor resolution or scheduling delays. Returns the
47 // translated timestamp.
Minyue Li37e388a2020-03-05 11:16:19 +010048 int64_t TranslateTimestamp(int64_t capturer_time_us, int64_t system_time_us);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020049
Minyue Lidd14a952020-03-10 15:56:42 +010050 // Returns the translated timestamp without updating the states. This is to
51 // allow TimestampAligner to translate capturer time into system clock based
52 // on earlier observations. It won't guarantee monotonicity.
53 int64_t TranslateTimestamp(int64_t capturer_time_us) const;
54
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020055 protected:
Minyue Li37e388a2020-03-05 11:16:19 +010056 // Update the estimated offset between capturer's time and system monotonic
57 // time.
58 int64_t UpdateOffset(int64_t capturer_time_us, int64_t system_time_us);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020059
60 // Clip timestamp, return value is always
Artem Titov96e3b992021-07-26 16:03:14 +020061 // <= `system_time_us`, and
62 // >= min(`prev_translated_time_us_` + `kMinFrameIntervalUs`,
63 // `system_time_us`).
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020064 int64_t ClipTimestamp(int64_t filtered_time_us, int64_t system_time_us);
65
66 private:
67 // State for the timestamp translation.
68 int frames_seen_;
Minyue Li37e388a2020-03-05 11:16:19 +010069 // Estimated offset between capturer's time and system monotonic time.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020070 int64_t offset_us_;
71
72 // State for the ClipTimestamp method, applied after the filter.
Minyue Li37e388a2020-03-05 11:16:19 +010073 // A large negative clock drift of the capturer tends to push translated
Artem Titov96e3b992021-07-26 16:03:14 +020074 // timestamps into the future. `clip_bias_us_` is subtracted from the
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020075 // translated timestamps, to get them back from the future.
76 int64_t clip_bias_us_;
77 // Used to ensure that translated timestamps are monotonous.
78 int64_t prev_translated_time_us_;
Artem Titov96e3b992021-07-26 16:03:14 +020079 // Offset between `prev_translated_time_us_` and the corresponding capturer
Minyue Lidd14a952020-03-10 15:56:42 +010080 // time.
81 int64_t prev_time_offset_us_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020082};
83
84} // namespace rtc
nisse191b3592016-06-22 08:36:53 -070085
Steve Anton10542f22019-01-11 09:11:00 -080086#endif // RTC_BASE_TIMESTAMP_ALIGNER_H_