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

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

Powered by Google App Engine
This is Rietveld 408576698