nisse | 191b359 | 2016-06-22 08:36:53 -0700 | [diff] [blame] | 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 | #ifndef WEBRTC_BASE_TIMESTAMPALIGNER_H_ |
| 12 | #define WEBRTC_BASE_TIMESTAMPALIGNER_H_ |
| 13 | |
pbos | 7eb0e23 | 2017-01-02 07:32:25 -0800 | [diff] [blame^] | 14 | #include "webrtc/base/basictypes.h" |
nisse | 191b359 | 2016-06-22 08:36:53 -0700 | [diff] [blame] | 15 | #include "webrtc/base/constructormagic.h" |
nisse | 191b359 | 2016-06-22 08:36:53 -0700 | [diff] [blame] | 16 | |
| 17 | namespace rtc { |
| 18 | |
nisse | a075848 | 2016-09-14 00:37:00 -0700 | [diff] [blame] | 19 | // The TimestampAligner class helps translating camera timestamps into |
| 20 | // the same timescale as is used by rtc::TimeMicros(). Some cameras |
| 21 | // have built in timestamping which is more accurate than reading the |
| 22 | // system clock, but using a different epoch and unknown clock drift. |
| 23 | // Frame timestamps in webrtc should use rtc::TimeMicros (system monotonic |
| 24 | // time), and this class provides a filter which lets us use the |
| 25 | // rtc::TimeMicros timescale, and at the same time take advantage of |
| 26 | // higher accuracy of the camera clock. |
| 27 | |
Magnus Jedvert | 0bade0d | 2016-09-01 15:15:00 +0200 | [diff] [blame] | 28 | // This class is not thread safe, so all calls to it must be synchronized |
| 29 | // externally. |
nisse | 191b359 | 2016-06-22 08:36:53 -0700 | [diff] [blame] | 30 | class TimestampAligner { |
| 31 | public: |
| 32 | TimestampAligner(); |
| 33 | ~TimestampAligner(); |
| 34 | |
| 35 | public: |
nisse | a075848 | 2016-09-14 00:37:00 -0700 | [diff] [blame] | 36 | // Translates camera timestamps to the same timescale as is used by |
| 37 | // rtc::TimeMicros(). |camera_time_us| is assumed to be accurate, but |
| 38 | // with an unknown epoch and clock drift. |system_time_us| is |
| 39 | // time according to rtc::TimeMicros(), preferably read as soon as |
| 40 | // possible when the frame is captured. It may have poor accuracy |
| 41 | // due to poor resolution or scheduling delays. Returns the |
| 42 | // translated timestamp. |
| 43 | int64_t TranslateTimestamp(int64_t camera_time_us, int64_t system_time_us); |
| 44 | |
| 45 | protected: |
nisse | 191b359 | 2016-06-22 08:36:53 -0700 | [diff] [blame] | 46 | // Update the estimated offset between camera time and system monotonic time. |
| 47 | int64_t UpdateOffset(int64_t camera_time_us, int64_t system_time_us); |
| 48 | |
nisse | a075848 | 2016-09-14 00:37:00 -0700 | [diff] [blame] | 49 | // Clip timestamp, return value is always |
| 50 | // <= |system_time_us|, and |
| 51 | // >= min(|prev_translated_time_us_| + |kMinFrameIntervalUs|, |
| 52 | // |system_time_us|). |
nisse | 191b359 | 2016-06-22 08:36:53 -0700 | [diff] [blame] | 53 | int64_t ClipTimestamp(int64_t filtered_time_us, int64_t system_time_us); |
| 54 | |
| 55 | private: |
nisse | 191b359 | 2016-06-22 08:36:53 -0700 | [diff] [blame] | 56 | // State for the timestamp translation. |
| 57 | int frames_seen_; |
| 58 | // Estimated offset between camera time and system monotonic time. |
| 59 | int64_t offset_us_; |
| 60 | |
nisse | a075848 | 2016-09-14 00:37:00 -0700 | [diff] [blame] | 61 | // State for the ClipTimestamp method, applied after the filter. |
| 62 | // A large negative camera clock drift tends to push translated |
| 63 | // timestamps into the future. |clip_bias_us_| is subtracted from the |
| 64 | // translated timestamps, to get them back from the future. |
nisse | 191b359 | 2016-06-22 08:36:53 -0700 | [diff] [blame] | 65 | int64_t clip_bias_us_; |
nisse | a075848 | 2016-09-14 00:37:00 -0700 | [diff] [blame] | 66 | // Used to ensure that translated timestamps are monotonous. |
| 67 | int64_t prev_translated_time_us_; |
nisse | 191b359 | 2016-06-22 08:36:53 -0700 | [diff] [blame] | 68 | RTC_DISALLOW_COPY_AND_ASSIGN(TimestampAligner); |
| 69 | }; |
| 70 | |
| 71 | } // namespace rtc |
| 72 | |
| 73 | #endif // WEBRTC_BASE_TIMESTAMPALIGNER_H_ |