timestamp_aligner.h 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. #ifndef RTC_BASE_TIMESTAMP_ALIGNER_H_
  11. #define RTC_BASE_TIMESTAMP_ALIGNER_H_
  12. #include <stdint.h>
  13. #include "rtc_base/constructor_magic.h"
  14. #include "rtc_base/system/rtc_export.h"
  15. namespace rtc {
  16. // The TimestampAligner class helps translating timestamps of a capture system
  17. // into the same timescale as is used by rtc::TimeMicros(). Some capture systems
  18. // provide timestamps, which comes from the capturing hardware (camera or sound
  19. // card) or stamped close to the capturing hardware. Such timestamps are more
  20. // accurate (less jittery) than reading the system clock, but may have a
  21. // different epoch and unknown clock drift. Frame timestamps in webrtc should
  22. // use rtc::TimeMicros (system monotonic time), and this class provides a filter
  23. // which lets us use the rtc::TimeMicros timescale, and at the same time take
  24. // advantage of higher accuracy of the capturer's clock.
  25. // This class is not thread safe, so all calls to it must be synchronized
  26. // externally.
  27. class RTC_EXPORT TimestampAligner {
  28. public:
  29. TimestampAligner();
  30. ~TimestampAligner();
  31. public:
  32. // Translates timestamps of a capture system to the same timescale as is used
  33. // by rtc::TimeMicros(). |capturer_time_us| is assumed to be accurate, but
  34. // with an unknown epoch and clock drift. |system_time_us| is
  35. // time according to rtc::TimeMicros(), preferably read as soon as
  36. // possible when the frame is captured. It may have poor accuracy
  37. // due to poor resolution or scheduling delays. Returns the
  38. // translated timestamp.
  39. int64_t TranslateTimestamp(int64_t capturer_time_us, int64_t system_time_us);
  40. // Returns the translated timestamp without updating the states. This is to
  41. // allow TimestampAligner to translate capturer time into system clock based
  42. // on earlier observations. It won't guarantee monotonicity.
  43. int64_t TranslateTimestamp(int64_t capturer_time_us) const;
  44. protected:
  45. // Update the estimated offset between capturer's time and system monotonic
  46. // time.
  47. int64_t UpdateOffset(int64_t capturer_time_us, int64_t system_time_us);
  48. // Clip timestamp, return value is always
  49. // <= |system_time_us|, and
  50. // >= min(|prev_translated_time_us_| + |kMinFrameIntervalUs|,
  51. // |system_time_us|).
  52. int64_t ClipTimestamp(int64_t filtered_time_us, int64_t system_time_us);
  53. private:
  54. // State for the timestamp translation.
  55. int frames_seen_;
  56. // Estimated offset between capturer's time and system monotonic time.
  57. int64_t offset_us_;
  58. // State for the ClipTimestamp method, applied after the filter.
  59. // A large negative clock drift of the capturer tends to push translated
  60. // timestamps into the future. |clip_bias_us_| is subtracted from the
  61. // translated timestamps, to get them back from the future.
  62. int64_t clip_bias_us_;
  63. // Used to ensure that translated timestamps are monotonous.
  64. int64_t prev_translated_time_us_;
  65. // Offset between |prev_translated_time_us_| and the corresponding capturer
  66. // time.
  67. int64_t prev_time_offset_us_;
  68. RTC_DISALLOW_COPY_AND_ASSIGN(TimestampAligner);
  69. };
  70. } // namespace rtc
  71. #endif // RTC_BASE_TIMESTAMP_ALIGNER_H_