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.

137 lines
5.5 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. #include "rtc_base/timestamp_aligner.h"
  11. #include <cstdlib>
  12. #include <limits>
  13. #include "rtc_base/checks.h"
  14. #include "rtc_base/logging.h"
  15. #include "rtc_base/time_utils.h"
  16. namespace rtc {
  17. TimestampAligner::TimestampAligner()
  18. : frames_seen_(0),
  19. offset_us_(0),
  20. clip_bias_us_(0),
  21. prev_translated_time_us_(std::numeric_limits<int64_t>::min()) {}
  22. TimestampAligner::~TimestampAligner() {}
  23. int64_t TimestampAligner::TranslateTimestamp(int64_t camera_time_us,
  24. int64_t system_time_us) {
  25. return ClipTimestamp(
  26. camera_time_us + UpdateOffset(camera_time_us, system_time_us),
  27. system_time_us);
  28. }
  29. int64_t TimestampAligner::UpdateOffset(int64_t camera_time_us,
  30. int64_t system_time_us) {
  31. // Estimate the offset between system monotonic time and the capture
  32. // time from the camera. The camera is assumed to provide more
  33. // accurate timestamps than we get from the system time. But the
  34. // camera may use its own free-running clock with a large offset and
  35. // a small drift compared to the system clock. So the model is
  36. // basically
  37. //
  38. // y_k = c_0 + c_1 * x_k + v_k
  39. //
  40. // where x_k is the camera timestamp, believed to be accurate in its
  41. // own scale. y_k is our reading of the system clock. v_k is the
  42. // measurement noise, i.e., the delay from frame capture until the
  43. // system clock was read.
  44. //
  45. // It's possible to do (weighted) least-squares estimation of both
  46. // c_0 and c_1. Then we get the constants as c_1 = Cov(x,y) /
  47. // Var(x), and c_0 = mean(y) - c_1 * mean(x). Substituting this c_0,
  48. // we can rearrange the model as
  49. //
  50. // y_k = mean(y) + (x_k - mean(x)) + (c_1 - 1) * (x_k - mean(x)) + v_k
  51. //
  52. // Now if we use a weighted average which gradually forgets old
  53. // values, x_k - mean(x) is bounded, of the same order as the time
  54. // constant (and close to constant for a steady frame rate). In
  55. // addition, the frequency error |c_1 - 1| should be small. Cameras
  56. // with a frequency error up to 3000 ppm (3 ms drift per second)
  57. // have been observed, but frequency errors below 100 ppm could be
  58. // expected of any cheap crystal.
  59. //
  60. // Bottom line is that we ignore the c_1 term, and use only the estimator
  61. //
  62. // x_k + mean(y-x)
  63. //
  64. // where mean is plain averaging for initial samples, followed by
  65. // exponential averaging.
  66. // The input for averaging, y_k - x_k in the above notation.
  67. int64_t diff_us = system_time_us - camera_time_us;
  68. // The deviation from the current average.
  69. int64_t error_us = diff_us - offset_us_;
  70. // If the current difference is far from the currently estimated
  71. // offset, the filter is reset. This could happen, e.g., if the
  72. // camera clock is reset, or cameras are plugged in and out, or if
  73. // the application process is temporarily suspended. Expected to
  74. // happen for the very first timestamp (|frames_seen_| = 0). The
  75. // threshold of 300 ms should make this unlikely in normal
  76. // operation, and at the same time, converging gradually rather than
  77. // resetting the filter should be tolerable for jumps in camera time
  78. // below this threshold.
  79. static const int64_t kResetThresholdUs = 300000;
  80. if (std::abs(error_us) > kResetThresholdUs) {
  81. RTC_LOG(LS_INFO) << "Resetting timestamp translation after averaging "
  82. << frames_seen_ << " frames. Old offset: " << offset_us_
  83. << ", new offset: " << diff_us;
  84. frames_seen_ = 0;
  85. clip_bias_us_ = 0;
  86. }
  87. static const int kWindowSize = 100;
  88. if (frames_seen_ < kWindowSize) {
  89. ++frames_seen_;
  90. }
  91. offset_us_ += error_us / frames_seen_;
  92. return offset_us_;
  93. }
  94. int64_t TimestampAligner::ClipTimestamp(int64_t filtered_time_us,
  95. int64_t system_time_us) {
  96. const int64_t kMinFrameIntervalUs = rtc::kNumMicrosecsPerMillisec;
  97. // Clip to make sure we don't produce timestamps in the future.
  98. int64_t time_us = filtered_time_us - clip_bias_us_;
  99. if (time_us > system_time_us) {
  100. clip_bias_us_ += time_us - system_time_us;
  101. time_us = system_time_us;
  102. }
  103. // Make timestamps monotonic, with a minimum inter-frame interval of 1 ms.
  104. else if (time_us < prev_translated_time_us_ + kMinFrameIntervalUs) {
  105. time_us = prev_translated_time_us_ + kMinFrameIntervalUs;
  106. if (time_us > system_time_us) {
  107. // In the anomalous case that this function is called with values of
  108. // |system_time_us| less than |kMinFrameIntervalUs| apart, we may output
  109. // timestamps with with too short inter-frame interval. We may even return
  110. // duplicate timestamps in case this function is called several times with
  111. // exactly the same |system_time_us|.
  112. RTC_LOG(LS_WARNING) << "too short translated timestamp interval: "
  113. << "system time (us) = " << system_time_us
  114. << ", interval (us) = "
  115. << system_time_us - prev_translated_time_us_;
  116. time_us = system_time_us;
  117. }
  118. }
  119. RTC_DCHECK_GE(time_us, prev_translated_time_us_);
  120. RTC_DCHECK_LE(time_us, system_time_us);
  121. prev_translated_time_us_ = time_us;
  122. return time_us;
  123. }
  124. } // namespace rtc