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