Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6)

Side by Side Diff: webrtc/base/timestampaligner.cc

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

Powered by Google App Engine
This is Rietveld 408576698