Debian Codegraph @

Xpjproject/third_party/webrtc_aec3/src/rtc_base/time_utils.h

raw symbols SCIP
1 /*
2 * Copyright 2005 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 RTC_BASE_TIME_UTILS_H_
12 #define RTC_BASE_TIME_UTILS_H_
13
14 #include <stdint.h>
15 #include <time.h>
16
17 #include "rtc_base/checks.h"
18 #include "rtc_base/system/rtc_export.h"
19 #include "rtc_base/system_time.h"
20
21 namespace rtc {
22
23 static const int64_t kNumMillisecsPerSec = INT64_C(1000);
24 static const int64_t kNumMicrosecsPerSec = INT64_C(1000000);
25 static const int64_t kNumNanosecsPerSec = INT64_C(1000000000);
26
27 static const int64_t kNumMicrosecsPerMillisec =
28 kNumMicrosecsPerSec / kNumMillisecsPerSec;
29 static const int64_t kNumNanosecsPerMillisec =
30 kNumNanosecsPerSec / kNumMillisecsPerSec;
31 static const int64_t kNumNanosecsPerMicrosec =
32 kNumNanosecsPerSec / kNumMicrosecsPerSec;
33
34 // TODO(honghaiz): Define a type for the time value specifically.
35
36 class ClockInterface {
37 public:
38 virtual ~ClockInterface() {}
39 virtual int64_t TimeNanos() const = 0;
40 };
41
42 // Sets the global source of time. This is useful mainly for unit tests.
43 //
44 // Returns the previously set ClockInterface, or nullptr if none is set.
45 //
46 // Does not transfer ownership of the clock. SetClockForTesting(nullptr)
47 // should be called before the ClockInterface is deleted.
48 //
49 // This method is not thread-safe; it should only be used when no other thread
50 // is running (for example, at the start/end of a unit test, or start/end of
51 // main()).
52 //
53 // TODO(deadbeef): Instead of having functions that access this global
54 // ClockInterface, we may want to pass the ClockInterface into everything
55 // that uses it, eliminating the need for a global variable and this function.
56 RTC_EXPORT ClockInterface* SetClockForTesting(ClockInterface* clock);
57
58 // Returns previously set clock, or nullptr if no custom clock is being used.
59 RTC_EXPORT ClockInterface* GetClockForTesting();
60
61 #if defined(WINUWP)
62 // Synchronizes the current clock based upon an NTP server's epoch in
63 // milliseconds.
64 void SyncWithNtp(int64_t time_from_ntp_server_ms);
65
66 // Returns the current time in nanoseconds. The clock is synchonized with the
67 // system wall clock time upon instatiation. It may also be synchronized using
68 // the SyncWithNtp() function above. Please note that the clock will most likely
69 // drift away from the system wall clock time as time goes by.
70 int64_t WinUwpSystemTimeNanos();
71 #endif // defined(WINUWP)
72
73 // Returns the actual system time, even if a clock is set for testing.
74 // Useful for timeouts while using a test clock, or for logging.
75 int64_t SystemTimeMillis();
76
77 // Returns the current time in milliseconds in 32 bits.
78 uint32_t Time32();
79
80 // Returns the current time in milliseconds in 64 bits.
81 RTC_EXPORT int64_t TimeMillis();
82 // Deprecated. Do not use this in any new code.
83 inline int64_t Time() {
84 return TimeMillis();
85 }
86
87 // Returns the current time in microseconds.
88 RTC_EXPORT int64_t TimeMicros();
89
90 // Returns the current time in nanoseconds.
91 RTC_EXPORT int64_t TimeNanos();
92
93 // Returns a future timestamp, 'elapsed' milliseconds from now.
94 int64_t TimeAfter(int64_t elapsed);
95
96 // Number of milliseconds that would elapse between 'earlier' and 'later'
97 // timestamps. The value is negative if 'later' occurs before 'earlier'.
98 int64_t TimeDiff(int64_t later, int64_t earlier);
99 int32_t TimeDiff32(uint32_t later, uint32_t earlier);
100
101 // The number of milliseconds that have elapsed since 'earlier'.
102 inline int64_t TimeSince(int64_t earlier) {
103 return TimeMillis() - earlier;
104 }
105
106 // The number of milliseconds that will elapse between now and 'later'.
107 inline int64_t TimeUntil(int64_t later) {
108 return later - TimeMillis();
109 }
110
111 class TimestampWrapAroundHandler {
112 public:
113 TimestampWrapAroundHandler();
114
115 int64_t Unwrap(uint32_t ts);
116
117 private:
118 uint32_t last_ts_;
119 int64_t num_wrap_;
120 };
121
122 // Convert from tm, which is relative to 1900-01-01 00:00 to number of
123 // seconds from 1970-01-01 00:00 ("epoch"). Don't return time_t since that
124 // is still 32 bits on many systems.
125 int64_t TmToSeconds(const tm& tm);
126
127 // Return the number of microseconds since January 1, 1970, UTC.
128 // Useful mainly when producing logs to be correlated with other
129 // devices, and when the devices in question all have properly
130 // synchronized clocks.
131 //
132 // Note that this function obeys the system's idea about what the time
133 // is. It is not guaranteed to be monotonic; it will jump in case the
134 // system time is changed, e.g., by some other process calling
135 // settimeofday. Always use rtc::TimeMicros(), not this function, for
136 // measuring time intervals and timeouts.
137 int64_t TimeUTCMicros();
138
139 // Return the number of milliseconds since January 1, 1970, UTC.
140 // See above.
141 int64_t TimeUTCMillis();
142
143 } // namespace rtc
144
145 #endif // RTC_BASE_TIME_UTILS_H_