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