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

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

Issue 2325563002: New method TimestampAligner::TranslateTimestamp (Closed)
Patch Set: Minimum inter-frame interval 1ms. Fix clipping logic. Created 4 years, 3 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
1 /* 1 /*
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include <limits>
12
13 #include "webrtc/base/checks.h"
11 #include "webrtc/base/logging.h" 14 #include "webrtc/base/logging.h"
12 #include "webrtc/base/timestampaligner.h" 15 #include "webrtc/base/timestampaligner.h"
16 #include "webrtc/base/timeutils.h"
13 17
14 namespace rtc { 18 namespace rtc {
15 19
16 TimestampAligner::TimestampAligner() : frames_seen_(0), offset_us_(0) {} 20 TimestampAligner::TimestampAligner()
21 : frames_seen_(0),
22 offset_us_(0),
23 clip_bias_us_(0),
24 prev_translated_time_us_(std::numeric_limits<int64_t>::min()) {}
17 25
18 TimestampAligner::~TimestampAligner() {} 26 TimestampAligner::~TimestampAligner() {}
19 27
28 int64_t TimestampAligner::TranslateTimestamp(int64_t camera_time_us,
29 int64_t system_time_us) {
30 return ClipTimestamp(
31 camera_time_us + UpdateOffset(camera_time_us, system_time_us),
32 system_time_us);
33 }
34
20 int64_t TimestampAligner::UpdateOffset(int64_t camera_time_us, 35 int64_t TimestampAligner::UpdateOffset(int64_t camera_time_us,
21 int64_t system_time_us) { 36 int64_t system_time_us) {
22 // Estimate the offset between system monotonic time and the capture 37 // Estimate the offset between system monotonic time and the capture
23 // time from the camera. The camera is assumed to provide more 38 // time from the camera. The camera is assumed to provide more
24 // accurate timestamps than we get from the system time. But the 39 // 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 40 // 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 41 // a small drift compared to the system clock. So the model is
27 // basically 42 // basically
28 // 43 //
29 // y_k = c_0 + c_1 * x_k + v_k 44 // y_k = c_0 + c_1 * x_k + v_k
(...skipping 26 matching lines...) Expand all
56 // exponential averaging. 71 // exponential averaging.
57 72
58 // The input for averaging, y_k - x_k in the above notation. 73 // The input for averaging, y_k - x_k in the above notation.
59 int64_t diff_us = system_time_us - camera_time_us; 74 int64_t diff_us = system_time_us - camera_time_us;
60 // The deviation from the current average. 75 // The deviation from the current average.
61 int64_t error_us = diff_us - offset_us_; 76 int64_t error_us = diff_us - offset_us_;
62 77
63 // If the current difference is far from the currently estimated 78 // If the current difference is far from the currently estimated
64 // offset, the filter is reset. This could happen, e.g., if the 79 // 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 80 // camera clock is reset, or cameras are plugged in and out, or if
66 // the application process is temporarily suspended. The limit of 81 // the application process is temporarily suspended. Expected to
67 // 300 ms should make this unlikely in normal operation, and at the 82 // happen for the very first timestamp (|frames_seen_| = 0). The
68 // same time, converging gradually rather than resetting the filter 83 // threshold of 300 ms should make this unlikely in normal
69 // should be tolerable for jumps in camera time below this 84 // operation, and at the same time, converging gradually rather than
70 // threshold. 85 // resetting the filter should be tolerable for jumps in camera time
71 static const int64_t kResetLimitUs = 300000; 86 // below this threshold.
72 if (std::abs(error_us) > kResetLimitUs) { 87 static const int64_t kResetThresholdUs = 300000;
88 if (std::abs(error_us) > kResetThresholdUs) {
73 LOG(LS_INFO) << "Resetting timestamp translation after averaging " 89 LOG(LS_INFO) << "Resetting timestamp translation after averaging "
74 << frames_seen_ << " frames. Old offset: " << offset_us_ 90 << frames_seen_ << " frames. Old offset: " << offset_us_
75 << ", new offset: " << diff_us; 91 << ", new offset: " << diff_us;
76 frames_seen_ = 0; 92 frames_seen_ = 0;
77 prev_translated_time_us_ = rtc::Optional<int64_t>(); 93 clip_bias_us_ = 0;
78 } 94 }
79 95
80 static const int kWindowSize = 100; 96 static const int kWindowSize = 100;
81 if (frames_seen_ < kWindowSize) { 97 if (frames_seen_ < kWindowSize) {
82 ++frames_seen_; 98 ++frames_seen_;
83 } 99 }
84 offset_us_ += error_us / frames_seen_; 100 offset_us_ += error_us / frames_seen_;
85 return offset_us_; 101 return offset_us_;
86 } 102 }
87 103
88 int64_t TimestampAligner::ClipTimestamp(int64_t time_us, 104 int64_t TimestampAligner::ClipTimestamp(int64_t filtered_time_us,
89 int64_t system_time_us) { 105 int64_t system_time_us) {
90 // Make timestamps monotonic. 106 const int64_t kMinFrameIntervalUs = rtc::kNumMicrosecsPerMillisec;
91 if (!prev_translated_time_us_) { 107 // Clip to make sure we don't produce timestamps in the future.
92 // Initialize. 108 int64_t time_us = filtered_time_us - clip_bias_us_;
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) { 109 if (time_us > system_time_us) {
101 clip_bias_us_ += time_us - system_time_us; 110 clip_bias_us_ += time_us - system_time_us;
102 time_us = system_time_us; 111 time_us = system_time_us;
103 } 112 }
104 prev_translated_time_us_ = rtc::Optional<int64_t>(time_us); 113 // Make timestamps monotonic, with a minimum inter-frame interval of 1 ms.
114 else if (time_us < prev_translated_time_us_ + kMinFrameIntervalUs) {
115 time_us = prev_translated_time_us_ + kMinFrameIntervalUs;
116 if (time_us > system_time_us) {
117 // In the anomalous case that this function is called with values of
118 // |system_time_us| less than |kMinFrameIntervalUs| apart, we may output
119 // timestamps with with too short inter-frame interval. We may even return
120 // duplicate timestamps in case this function is called several times with
121 // exactly the same |system_time_us|.
122 LOG(LS_WARNING) << "too short translated timestamp interval: "
123 << "system time (us) = " << system_time_us
124 << ", interval (us) = "
125 << system_time_us - prev_translated_time_us_;
126 time_us = system_time_us;
127 }
128 }
129 RTC_DCHECK_GE(time_us, prev_translated_time_us_);
130 RTC_DCHECK_LE(time_us, system_time_us);
131 prev_translated_time_us_ = time_us;
105 return time_us; 132 return time_us;
106 } 133 }
107 134
108 } // namespace rtc 135 } // 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