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