blob: 66724399fe6daebdfc7fe118a29210921577bfa0 [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
11#include "webrtc/base/logging.h"
12#include "webrtc/base/timestampaligner.h"
13
14namespace rtc {
15
nisse76f91cd2016-08-24 01:58:42 -070016TimestampAligner::TimestampAligner() : frames_seen_(0), offset_us_(0) {
17 thread_checker_.DetachFromThread();
18}
19
nisse191b3592016-06-22 08:36:53 -070020TimestampAligner::~TimestampAligner() {}
21
22int64_t TimestampAligner::UpdateOffset(int64_t camera_time_us,
23 int64_t system_time_us) {
nisse76f91cd2016-08-24 01:58:42 -070024 RTC_DCHECK(thread_checker_.CalledOnValidThread());
25
nisse191b3592016-06-22 08:36:53 -070026 // Estimate the offset between system monotonic time and the capture
27 // time from the camera. The camera is assumed to provide more
28 // accurate timestamps than we get from the system time. But the
29 // camera may use its own free-running clock with a large offset and
30 // a small drift compared to the system clock. So the model is
31 // basically
32 //
33 // y_k = c_0 + c_1 * x_k + v_k
34 //
35 // where x_k is the camera timestamp, believed to be accurate in its
36 // own scale. y_k is our reading of the system clock. v_k is the
37 // measurement noise, i.e., the delay from frame capture until the
38 // system clock was read.
39 //
40 // It's possible to do (weighted) least-squares estimation of both
41 // c_0 and c_1. Then we get the constants as c_1 = Cov(x,y) /
42 // Var(x), and c_0 = mean(y) - c_1 * mean(x). Substituting this c_0,
43 // we can rearrange the model as
44 //
45 // y_k = mean(y) + (x_k - mean(x)) + (c_1 - 1) * (x_k - mean(x)) + v_k
46 //
47 // Now if we use a weighted average which gradually forgets old
48 // values, x_k - mean(x) is bounded, of the same order as the time
49 // constant (and close to constant for a steady frame rate). In
50 // addition, the frequency error |c_1 - 1| should be small. Cameras
51 // with a frequency error up to 3000 ppm (3 ms drift per second)
52 // have been observed, but frequency errors below 100 ppm could be
53 // expected of any cheap crystal.
54 //
55 // Bottom line is that we ignore the c_1 term, and use only the estimator
56 //
57 // x_k + mean(y-x)
58 //
59 // where mean is plain averaging for initial samples, followed by
60 // exponential averaging.
61
62 // The input for averaging, y_k - x_k in the above notation.
63 int64_t diff_us = system_time_us - camera_time_us;
64 // The deviation from the current average.
65 int64_t error_us = diff_us - offset_us_;
66
67 // If the current difference is far from the currently estimated
68 // offset, the filter is reset. This could happen, e.g., if the
69 // camera clock is reset, or cameras are plugged in and out, or if
70 // the application process is temporarily suspended. The limit of
71 // 300 ms should make this unlikely in normal operation, and at the
72 // same time, converging gradually rather than resetting the filter
73 // should be tolerable for jumps in camera time below this
74 // threshold.
75 static const int64_t kResetLimitUs = 300000;
76 if (std::abs(error_us) > kResetLimitUs) {
77 LOG(LS_INFO) << "Resetting timestamp translation after averaging "
78 << frames_seen_ << " frames. Old offset: " << offset_us_
79 << ", new offset: " << diff_us;
80 frames_seen_ = 0;
81 prev_translated_time_us_ = rtc::Optional<int64_t>();
82 }
83
84 static const int kWindowSize = 100;
85 if (frames_seen_ < kWindowSize) {
86 ++frames_seen_;
87 }
88 offset_us_ += error_us / frames_seen_;
89 return offset_us_;
90}
91
92int64_t TimestampAligner::ClipTimestamp(int64_t time_us,
93 int64_t system_time_us) {
nisse76f91cd2016-08-24 01:58:42 -070094 RTC_DCHECK(thread_checker_.CalledOnValidThread());
95
nisse191b3592016-06-22 08:36:53 -070096 // Make timestamps monotonic.
97 if (!prev_translated_time_us_) {
98 // Initialize.
99 clip_bias_us_ = 0;
100 } else if (time_us < *prev_translated_time_us_) {
101 time_us = *prev_translated_time_us_;
102 }
103
104 // Clip to make sure we don't produce time stamps in the future.
105 time_us -= clip_bias_us_;
106 if (time_us > system_time_us) {
107 clip_bias_us_ += time_us - system_time_us;
108 time_us = system_time_us;
109 }
110 prev_translated_time_us_ = rtc::Optional<int64_t>(time_us);
111 return time_us;
112}
113
114} // namespace rtc