blob: 151bcdccb1767d38d4517666eb788fd464a52bb2 [file] [log] [blame]
nisse191b3592016-06-22 08:36:53 -07001/*
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
Steve Anton10542f22019-01-11 09:11:00 -080011#ifndef RTC_BASE_TIMESTAMP_ALIGNER_H_
12#define RTC_BASE_TIMESTAMP_ALIGNER_H_
nisse191b3592016-06-22 08:36:53 -070013
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020014#include <stdint.h>
pbosc7c26a02017-01-02 08:42:32 -080015
Steve Anton10542f22019-01-11 09:11:00 -080016#include "rtc_base/constructor_magic.h"
Mirko Bonadei35214fc2019-09-23 14:54:28 +020017#include "rtc_base/system/rtc_export.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020018
19namespace rtc {
20
21// The TimestampAligner class helps translating camera timestamps into
22// the same timescale as is used by rtc::TimeMicros(). Some cameras
23// have built in timestamping which is more accurate than reading the
24// system clock, but using a different epoch and unknown clock drift.
25// Frame timestamps in webrtc should use rtc::TimeMicros (system monotonic
26// time), and this class provides a filter which lets us use the
27// rtc::TimeMicros timescale, and at the same time take advantage of
28// higher accuracy of the camera clock.
29
30// This class is not thread safe, so all calls to it must be synchronized
31// externally.
Mirko Bonadei35214fc2019-09-23 14:54:28 +020032class RTC_EXPORT TimestampAligner {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020033 public:
34 TimestampAligner();
35 ~TimestampAligner();
36
37 public:
38 // Translates camera timestamps to the same timescale as is used by
39 // rtc::TimeMicros(). |camera_time_us| is assumed to be accurate, but
40 // with an unknown epoch and clock drift. |system_time_us| is
41 // time according to rtc::TimeMicros(), preferably read as soon as
42 // possible when the frame is captured. It may have poor accuracy
43 // due to poor resolution or scheduling delays. Returns the
44 // translated timestamp.
45 int64_t TranslateTimestamp(int64_t camera_time_us, int64_t system_time_us);
46
47 protected:
48 // Update the estimated offset between camera time and system monotonic time.
49 int64_t UpdateOffset(int64_t camera_time_us, int64_t system_time_us);
50
51 // Clip timestamp, return value is always
52 // <= |system_time_us|, and
53 // >= min(|prev_translated_time_us_| + |kMinFrameIntervalUs|,
54 // |system_time_us|).
55 int64_t ClipTimestamp(int64_t filtered_time_us, int64_t system_time_us);
56
57 private:
58 // State for the timestamp translation.
59 int frames_seen_;
60 // Estimated offset between camera time and system monotonic time.
61 int64_t offset_us_;
62
63 // State for the ClipTimestamp method, applied after the filter.
64 // A large negative camera clock drift tends to push translated
65 // timestamps into the future. |clip_bias_us_| is subtracted from the
66 // translated timestamps, to get them back from the future.
67 int64_t clip_bias_us_;
68 // Used to ensure that translated timestamps are monotonous.
69 int64_t prev_translated_time_us_;
70 RTC_DISALLOW_COPY_AND_ASSIGN(TimestampAligner);
71};
72
73} // namespace rtc
nisse191b3592016-06-22 08:36:53 -070074
Steve Anton10542f22019-01-11 09:11:00 -080075#endif // RTC_BASE_TIMESTAMP_ALIGNER_H_