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