nisse | 191b359 | 2016-06-22 08:36:53 -0700 | [diff] [blame] | 1 | /* |
| 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 Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #ifndef RTC_BASE_TIMESTAMP_ALIGNER_H_ |
| 12 | #define RTC_BASE_TIMESTAMP_ALIGNER_H_ |
nisse | 191b359 | 2016-06-22 08:36:53 -0700 | [diff] [blame] | 13 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 14 | #include <stdint.h> |
pbos | c7c26a0 | 2017-01-02 08:42:32 -0800 | [diff] [blame] | 15 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 16 | #include "rtc_base/constructor_magic.h" |
Mirko Bonadei | 35214fc | 2019-09-23 14:54:28 +0200 | [diff] [blame] | 17 | #include "rtc_base/system/rtc_export.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 18 | |
| 19 | namespace rtc { |
| 20 | |
Minyue Li | 37e388a | 2020-03-05 11:16:19 +0100 | [diff] [blame] | 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. |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 30 | |
| 31 | // This class is not thread safe, so all calls to it must be synchronized |
| 32 | // externally. |
Mirko Bonadei | 35214fc | 2019-09-23 14:54:28 +0200 | [diff] [blame] | 33 | class RTC_EXPORT TimestampAligner { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 34 | public: |
| 35 | TimestampAligner(); |
| 36 | ~TimestampAligner(); |
| 37 | |
| 38 | public: |
Minyue Li | 37e388a | 2020-03-05 11:16:19 +0100 | [diff] [blame] | 39 | // Translates timestamps of a capture system to the same timescale as is used |
| 40 | // by rtc::TimeMicros(). |capturer_time_us| is assumed to be accurate, but |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 41 | // with an unknown epoch and clock drift. |system_time_us| is |
| 42 | // time according to rtc::TimeMicros(), preferably read as soon as |
| 43 | // possible when the frame is captured. It may have poor accuracy |
| 44 | // due to poor resolution or scheduling delays. Returns the |
| 45 | // translated timestamp. |
Minyue Li | 37e388a | 2020-03-05 11:16:19 +0100 | [diff] [blame] | 46 | int64_t TranslateTimestamp(int64_t capturer_time_us, int64_t system_time_us); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 47 | |
Minyue Li | dd14a95 | 2020-03-10 15:56:42 +0100 | [diff] [blame^] | 48 | // Returns the translated timestamp without updating the states. This is to |
| 49 | // allow TimestampAligner to translate capturer time into system clock based |
| 50 | // on earlier observations. It won't guarantee monotonicity. |
| 51 | int64_t TranslateTimestamp(int64_t capturer_time_us) const; |
| 52 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 53 | protected: |
Minyue Li | 37e388a | 2020-03-05 11:16:19 +0100 | [diff] [blame] | 54 | // Update the estimated offset between capturer's time and system monotonic |
| 55 | // time. |
| 56 | int64_t UpdateOffset(int64_t capturer_time_us, int64_t system_time_us); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 57 | |
| 58 | // Clip timestamp, return value is always |
| 59 | // <= |system_time_us|, and |
| 60 | // >= min(|prev_translated_time_us_| + |kMinFrameIntervalUs|, |
| 61 | // |system_time_us|). |
| 62 | int64_t ClipTimestamp(int64_t filtered_time_us, int64_t system_time_us); |
| 63 | |
| 64 | private: |
| 65 | // State for the timestamp translation. |
| 66 | int frames_seen_; |
Minyue Li | 37e388a | 2020-03-05 11:16:19 +0100 | [diff] [blame] | 67 | // Estimated offset between capturer's time and system monotonic time. |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 68 | int64_t offset_us_; |
| 69 | |
| 70 | // State for the ClipTimestamp method, applied after the filter. |
Minyue Li | 37e388a | 2020-03-05 11:16:19 +0100 | [diff] [blame] | 71 | // A large negative clock drift of the capturer tends to push translated |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 72 | // timestamps into the future. |clip_bias_us_| is subtracted from the |
| 73 | // translated timestamps, to get them back from the future. |
| 74 | int64_t clip_bias_us_; |
| 75 | // Used to ensure that translated timestamps are monotonous. |
| 76 | int64_t prev_translated_time_us_; |
Minyue Li | dd14a95 | 2020-03-10 15:56:42 +0100 | [diff] [blame^] | 77 | // Offset between |prev_translated_time_us_| and the corresponding capturer |
| 78 | // time. |
| 79 | int64_t prev_time_offset_us_; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 80 | RTC_DISALLOW_COPY_AND_ASSIGN(TimestampAligner); |
| 81 | }; |
| 82 | |
| 83 | } // namespace rtc |
nisse | 191b359 | 2016-06-22 08:36:53 -0700 | [diff] [blame] | 84 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 85 | #endif // RTC_BASE_TIMESTAMP_ALIGNER_H_ |