OLD | NEW |
---|---|
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 | |
11 #include "webrtc/base/logging.h" | 13 #include "webrtc/base/logging.h" |
12 #include "webrtc/base/timestampaligner.h" | 14 #include "webrtc/base/timestampaligner.h" |
13 | 15 |
14 namespace rtc { | 16 namespace rtc { |
15 | 17 |
16 TimestampAligner::TimestampAligner() : frames_seen_(0), offset_us_(0) {} | 18 TimestampAligner::TimestampAligner() |
19 : frames_seen_(0), | |
20 offset_us_(0), | |
21 clip_bias_us_(0), | |
22 prev_translated_time_us_(std::numeric_limits<int64_t>::min()) {} | |
17 | 23 |
18 TimestampAligner::~TimestampAligner() {} | 24 TimestampAligner::~TimestampAligner() {} |
19 | 25 |
26 int64_t TimestampAligner::TranslateTimestamp(int64_t camera_time_us, | |
27 int64_t system_time_us) { | |
28 return ClipTimestamp( | |
29 camera_time_us + UpdateOffset(camera_time_us, system_time_us), | |
30 system_time_us); | |
31 } | |
32 | |
20 int64_t TimestampAligner::UpdateOffset(int64_t camera_time_us, | 33 int64_t TimestampAligner::UpdateOffset(int64_t camera_time_us, |
21 int64_t system_time_us) { | 34 int64_t system_time_us) { |
22 // Estimate the offset between system monotonic time and the capture | 35 // Estimate the offset between system monotonic time and the capture |
23 // time from the camera. The camera is assumed to provide more | 36 // time from the camera. The camera is assumed to provide more |
24 // accurate timestamps than we get from the system time. But the | 37 // 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 | 38 // 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 | 39 // a small drift compared to the system clock. So the model is |
27 // basically | 40 // basically |
28 // | 41 // |
29 // y_k = c_0 + c_1 * x_k + v_k | 42 // y_k = c_0 + c_1 * x_k + v_k |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
67 // 300 ms should make this unlikely in normal operation, and at the | 80 // 300 ms should make this unlikely in normal operation, and at the |
68 // same time, converging gradually rather than resetting the filter | 81 // same time, converging gradually rather than resetting the filter |
69 // should be tolerable for jumps in camera time below this | 82 // should be tolerable for jumps in camera time below this |
70 // threshold. | 83 // threshold. |
71 static const int64_t kResetLimitUs = 300000; | 84 static const int64_t kResetLimitUs = 300000; |
72 if (std::abs(error_us) > kResetLimitUs) { | 85 if (std::abs(error_us) > kResetLimitUs) { |
73 LOG(LS_INFO) << "Resetting timestamp translation after averaging " | 86 LOG(LS_INFO) << "Resetting timestamp translation after averaging " |
74 << frames_seen_ << " frames. Old offset: " << offset_us_ | 87 << frames_seen_ << " frames. Old offset: " << offset_us_ |
75 << ", new offset: " << diff_us; | 88 << ", new offset: " << diff_us; |
76 frames_seen_ = 0; | 89 frames_seen_ = 0; |
77 prev_translated_time_us_ = rtc::Optional<int64_t>(); | 90 // Reset the clip bias, but not prev_translated_time_us_, the |
91 // latter is needed to ensure monotonous timestamps over a reset. | |
92 clip_bias_us_ = 0; | |
perkj_webrtc
2016/09/08 13:41:05
can this change be tested?
nisse-webrtc
2016/09/09 13:32:43
I'm adding a test for the clipping logic, but it d
| |
78 } | 93 } |
79 | 94 |
80 static const int kWindowSize = 100; | 95 static const int kWindowSize = 100; |
81 if (frames_seen_ < kWindowSize) { | 96 if (frames_seen_ < kWindowSize) { |
82 ++frames_seen_; | 97 ++frames_seen_; |
83 } | 98 } |
84 offset_us_ += error_us / frames_seen_; | 99 offset_us_ += error_us / frames_seen_; |
85 return offset_us_; | 100 return offset_us_; |
86 } | 101 } |
87 | 102 |
88 int64_t TimestampAligner::ClipTimestamp(int64_t time_us, | 103 int64_t TimestampAligner::ClipTimestamp(int64_t time_us, |
89 int64_t system_time_us) { | 104 int64_t system_time_us) { |
90 // Make timestamps monotonic. | 105 // Make timestamps monotonic. |
91 if (!prev_translated_time_us_) { | 106 if (time_us <= prev_translated_time_us_) { |
92 // Initialize. | 107 time_us = prev_translated_time_us_ + 1; |
93 clip_bias_us_ = 0; | |
94 } else if (time_us < *prev_translated_time_us_) { | |
95 time_us = *prev_translated_time_us_; | |
96 } | 108 } |
97 | 109 |
98 // Clip to make sure we don't produce time stamps in the future. | 110 // Clip to make sure we don't produce timestamps in the future. In |
111 // the anomalous case that this function is called multiple times | |
112 // with exactly the same system_time_us, we may also output the same | |
113 // timestamp several times, violating strict monotonicity. | |
99 time_us -= clip_bias_us_; | 114 time_us -= clip_bias_us_; |
100 if (time_us > system_time_us) { | 115 if (time_us > system_time_us) { |
101 clip_bias_us_ += time_us - system_time_us; | 116 clip_bias_us_ += time_us - system_time_us; |
102 time_us = system_time_us; | 117 time_us = system_time_us; |
103 } | 118 } |
104 prev_translated_time_us_ = rtc::Optional<int64_t>(time_us); | 119 prev_translated_time_us_ = time_us; |
105 return time_us; | 120 return time_us; |
106 } | 121 } |
107 | 122 |
108 } // namespace rtc | 123 } // namespace rtc |
OLD | NEW |