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