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 "webrtc/base/logging.h" | 11 #include "webrtc/base/logging.h" |
12 #include "webrtc/base/timestampaligner.h" | 12 #include "webrtc/base/timestampaligner.h" |
13 | 13 |
14 namespace rtc { | 14 namespace rtc { |
15 | 15 |
16 TimestampAligner::TimestampAligner() : frames_seen_(0), offset_us_(0) { | 16 TimestampAligner::TimestampAligner() : frames_seen_(0), offset_us_(0) {} |
17 thread_checker_.DetachFromThread(); | |
18 } | |
19 | 17 |
20 TimestampAligner::~TimestampAligner() {} | 18 TimestampAligner::~TimestampAligner() {} |
21 | 19 |
22 int64_t TimestampAligner::UpdateOffset(int64_t camera_time_us, | 20 int64_t TimestampAligner::UpdateOffset(int64_t camera_time_us, |
23 int64_t system_time_us) { | 21 int64_t system_time_us) { |
24 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | |
25 | |
26 // Estimate the offset between system monotonic time and the capture | 22 // Estimate the offset between system monotonic time and the capture |
27 // time from the camera. The camera is assumed to provide more | 23 // time from the camera. The camera is assumed to provide more |
28 // accurate timestamps than we get from the system time. But the | 24 // accurate timestamps than we get from the system time. But the |
29 // camera may use its own free-running clock with a large offset and | 25 // camera may use its own free-running clock with a large offset and |
30 // a small drift compared to the system clock. So the model is | 26 // a small drift compared to the system clock. So the model is |
31 // basically | 27 // basically |
32 // | 28 // |
33 // y_k = c_0 + c_1 * x_k + v_k | 29 // y_k = c_0 + c_1 * x_k + v_k |
34 // | 30 // |
35 // where x_k is the camera timestamp, believed to be accurate in its | 31 // where x_k is the camera timestamp, believed to be accurate in its |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 static const int kWindowSize = 100; | 80 static const int kWindowSize = 100; |
85 if (frames_seen_ < kWindowSize) { | 81 if (frames_seen_ < kWindowSize) { |
86 ++frames_seen_; | 82 ++frames_seen_; |
87 } | 83 } |
88 offset_us_ += error_us / frames_seen_; | 84 offset_us_ += error_us / frames_seen_; |
89 return offset_us_; | 85 return offset_us_; |
90 } | 86 } |
91 | 87 |
92 int64_t TimestampAligner::ClipTimestamp(int64_t time_us, | 88 int64_t TimestampAligner::ClipTimestamp(int64_t time_us, |
93 int64_t system_time_us) { | 89 int64_t system_time_us) { |
94 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | |
95 | |
96 // Make timestamps monotonic. | 90 // Make timestamps monotonic. |
97 if (!prev_translated_time_us_) { | 91 if (!prev_translated_time_us_) { |
98 // Initialize. | 92 // Initialize. |
99 clip_bias_us_ = 0; | 93 clip_bias_us_ = 0; |
100 } else if (time_us < *prev_translated_time_us_) { | 94 } else if (time_us < *prev_translated_time_us_) { |
101 time_us = *prev_translated_time_us_; | 95 time_us = *prev_translated_time_us_; |
102 } | 96 } |
103 | 97 |
104 // Clip to make sure we don't produce time stamps in the future. | 98 // Clip to make sure we don't produce time stamps in the future. |
105 time_us -= clip_bias_us_; | 99 time_us -= clip_bias_us_; |
106 if (time_us > system_time_us) { | 100 if (time_us > system_time_us) { |
107 clip_bias_us_ += time_us - system_time_us; | 101 clip_bias_us_ += time_us - system_time_us; |
108 time_us = system_time_us; | 102 time_us = system_time_us; |
109 } | 103 } |
110 prev_translated_time_us_ = rtc::Optional<int64_t>(time_us); | 104 prev_translated_time_us_ = rtc::Optional<int64_t>(time_us); |
111 return time_us; | 105 return time_us; |
112 } | 106 } |
113 | 107 |
114 } // namespace rtc | 108 } // namespace rtc |
OLD | NEW |