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