You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
2.9 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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 camera timestamps into
  17. // the same timescale as is used by rtc::TimeMicros(). Some cameras
  18. // have built in timestamping which is more accurate than reading the
  19. // system clock, but using a different epoch and unknown clock drift.
  20. // Frame timestamps in webrtc should use rtc::TimeMicros (system monotonic
  21. // time), and this class provides a filter which lets us use the
  22. // rtc::TimeMicros timescale, and at the same time take advantage of
  23. // higher accuracy of the camera clock.
  24. // This class is not thread safe, so all calls to it must be synchronized
  25. // externally.
  26. class RTC_EXPORT TimestampAligner {
  27. public:
  28. TimestampAligner();
  29. ~TimestampAligner();
  30. public:
  31. // Translates camera timestamps to the same timescale as is used by
  32. // rtc::TimeMicros(). |camera_time_us| is assumed to be accurate, but
  33. // with an unknown epoch and clock drift. |system_time_us| is
  34. // time according to rtc::TimeMicros(), preferably read as soon as
  35. // possible when the frame is captured. It may have poor accuracy
  36. // due to poor resolution or scheduling delays. Returns the
  37. // translated timestamp.
  38. int64_t TranslateTimestamp(int64_t camera_time_us, int64_t system_time_us);
  39. protected:
  40. // Update the estimated offset between camera time and system monotonic time.
  41. int64_t UpdateOffset(int64_t camera_time_us, int64_t system_time_us);
  42. // Clip timestamp, return value is always
  43. // <= |system_time_us|, and
  44. // >= min(|prev_translated_time_us_| + |kMinFrameIntervalUs|,
  45. // |system_time_us|).
  46. int64_t ClipTimestamp(int64_t filtered_time_us, int64_t system_time_us);
  47. private:
  48. // State for the timestamp translation.
  49. int frames_seen_;
  50. // Estimated offset between camera time and system monotonic time.
  51. int64_t offset_us_;
  52. // State for the ClipTimestamp method, applied after the filter.
  53. // A large negative camera clock drift tends to push translated
  54. // timestamps into the future. |clip_bias_us_| is subtracted from the
  55. // translated timestamps, to get them back from the future.
  56. int64_t clip_bias_us_;
  57. // Used to ensure that translated timestamps are monotonous.
  58. int64_t prev_translated_time_us_;
  59. RTC_DISALLOW_COPY_AND_ASSIGN(TimestampAligner);
  60. };
  61. } // namespace rtc
  62. #endif // RTC_BASE_TIMESTAMP_ALIGNER_H_