OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2005 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2005 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 #ifndef WEBRTC_BASE_TIMEUTILS_H_ | 11 #ifndef WEBRTC_BASE_TIMEUTILS_H_ |
12 #define WEBRTC_BASE_TIMEUTILS_H_ | 12 #define WEBRTC_BASE_TIMEUTILS_H_ |
13 | 13 |
14 #include <time.h> | 14 #include <time.h> |
15 | 15 |
16 #include "webrtc/base/basictypes.h" | 16 #include "webrtc/base/basictypes.h" |
17 | 17 |
18 namespace rtc { | 18 namespace rtc { |
19 | 19 |
20 static const int64 kNumMillisecsPerSec = INT64_C(1000); | 20 static const int64_t kNumMillisecsPerSec = INT64_C(1000); |
21 static const int64 kNumMicrosecsPerSec = INT64_C(1000000); | 21 static const int64_t kNumMicrosecsPerSec = INT64_C(1000000); |
22 static const int64 kNumNanosecsPerSec = INT64_C(1000000000); | 22 static const int64_t kNumNanosecsPerSec = INT64_C(1000000000); |
23 | 23 |
24 static const int64 kNumMicrosecsPerMillisec = kNumMicrosecsPerSec / | 24 static const int64_t kNumMicrosecsPerMillisec = |
25 kNumMillisecsPerSec; | 25 kNumMicrosecsPerSec / kNumMillisecsPerSec; |
26 static const int64 kNumNanosecsPerMillisec = kNumNanosecsPerSec / | 26 static const int64_t kNumNanosecsPerMillisec = |
27 kNumMillisecsPerSec; | 27 kNumNanosecsPerSec / kNumMillisecsPerSec; |
28 static const int64 kNumNanosecsPerMicrosec = kNumNanosecsPerSec / | 28 static const int64_t kNumNanosecsPerMicrosec = |
29 kNumMicrosecsPerSec; | 29 kNumNanosecsPerSec / kNumMicrosecsPerSec; |
30 | 30 |
31 // January 1970, in NTP milliseconds. | 31 // January 1970, in NTP milliseconds. |
32 static const int64 kJan1970AsNtpMillisecs = INT64_C(2208988800000); | 32 static const int64_t kJan1970AsNtpMillisecs = INT64_C(2208988800000); |
33 | 33 |
34 typedef uint32 TimeStamp; | 34 typedef uint32_t TimeStamp; |
35 | 35 |
36 // Returns the current time in milliseconds. | 36 // Returns the current time in milliseconds. |
37 uint32 Time(); | 37 uint32_t Time(); |
38 // Returns the current time in microseconds. | 38 // Returns the current time in microseconds. |
39 uint64 TimeMicros(); | 39 uint64_t TimeMicros(); |
40 // Returns the current time in nanoseconds. | 40 // Returns the current time in nanoseconds. |
41 uint64 TimeNanos(); | 41 uint64_t TimeNanos(); |
42 | 42 |
43 // Stores current time in *tm and microseconds in *microseconds. | 43 // Stores current time in *tm and microseconds in *microseconds. |
44 void CurrentTmTime(struct tm *tm, int *microseconds); | 44 void CurrentTmTime(struct tm *tm, int *microseconds); |
45 | 45 |
46 // Returns a future timestamp, 'elapsed' milliseconds from now. | 46 // Returns a future timestamp, 'elapsed' milliseconds from now. |
47 uint32 TimeAfter(int32 elapsed); | 47 uint32_t TimeAfter(int32_t elapsed); |
48 | 48 |
49 // Comparisons between time values, which can wrap around. | 49 // Comparisons between time values, which can wrap around. |
50 bool TimeIsBetween(uint32 earlier, uint32 middle, uint32 later); // Inclusive | 50 bool TimeIsBetween(uint32_t earlier, |
51 bool TimeIsLaterOrEqual(uint32 earlier, uint32 later); // Inclusive | 51 uint32_t middle, |
52 bool TimeIsLater(uint32 earlier, uint32 later); // Exclusive | 52 uint32_t later); // Inclusive |
| 53 bool TimeIsLaterOrEqual(uint32_t earlier, uint32_t later); // Inclusive |
| 54 bool TimeIsLater(uint32_t earlier, uint32_t later); // Exclusive |
53 | 55 |
54 // Returns the later of two timestamps. | 56 // Returns the later of two timestamps. |
55 inline uint32 TimeMax(uint32 ts1, uint32 ts2) { | 57 inline uint32_t TimeMax(uint32_t ts1, uint32_t ts2) { |
56 return TimeIsLaterOrEqual(ts1, ts2) ? ts2 : ts1; | 58 return TimeIsLaterOrEqual(ts1, ts2) ? ts2 : ts1; |
57 } | 59 } |
58 | 60 |
59 // Returns the earlier of two timestamps. | 61 // Returns the earlier of two timestamps. |
60 inline uint32 TimeMin(uint32 ts1, uint32 ts2) { | 62 inline uint32_t TimeMin(uint32_t ts1, uint32_t ts2) { |
61 return TimeIsLaterOrEqual(ts1, ts2) ? ts1 : ts2; | 63 return TimeIsLaterOrEqual(ts1, ts2) ? ts1 : ts2; |
62 } | 64 } |
63 | 65 |
64 // Number of milliseconds that would elapse between 'earlier' and 'later' | 66 // Number of milliseconds that would elapse between 'earlier' and 'later' |
65 // timestamps. The value is negative if 'later' occurs before 'earlier'. | 67 // timestamps. The value is negative if 'later' occurs before 'earlier'. |
66 int32 TimeDiff(uint32 later, uint32 earlier); | 68 int32_t TimeDiff(uint32_t later, uint32_t earlier); |
67 | 69 |
68 // The number of milliseconds that have elapsed since 'earlier'. | 70 // The number of milliseconds that have elapsed since 'earlier'. |
69 inline int32 TimeSince(uint32 earlier) { | 71 inline int32_t TimeSince(uint32_t earlier) { |
70 return TimeDiff(Time(), earlier); | 72 return TimeDiff(Time(), earlier); |
71 } | 73 } |
72 | 74 |
73 // The number of milliseconds that will elapse between now and 'later'. | 75 // The number of milliseconds that will elapse between now and 'later'. |
74 inline int32 TimeUntil(uint32 later) { | 76 inline int32_t TimeUntil(uint32_t later) { |
75 return TimeDiff(later, Time()); | 77 return TimeDiff(later, Time()); |
76 } | 78 } |
77 | 79 |
78 // Converts a unix timestamp in nanoseconds to an NTP timestamp in ms. | 80 // Converts a unix timestamp in nanoseconds to an NTP timestamp in ms. |
79 inline int64 UnixTimestampNanosecsToNtpMillisecs(int64 unix_ts_ns) { | 81 inline int64_t UnixTimestampNanosecsToNtpMillisecs(int64_t unix_ts_ns) { |
80 return unix_ts_ns / kNumNanosecsPerMillisec + kJan1970AsNtpMillisecs; | 82 return unix_ts_ns / kNumNanosecsPerMillisec + kJan1970AsNtpMillisecs; |
81 } | 83 } |
82 | 84 |
83 class TimestampWrapAroundHandler { | 85 class TimestampWrapAroundHandler { |
84 public: | 86 public: |
85 TimestampWrapAroundHandler(); | 87 TimestampWrapAroundHandler(); |
86 | 88 |
87 int64 Unwrap(uint32 ts); | 89 int64_t Unwrap(uint32_t ts); |
88 | 90 |
89 private: | 91 private: |
90 uint32 last_ts_; | 92 uint32_t last_ts_; |
91 int64 num_wrap_; | 93 int64_t num_wrap_; |
92 }; | 94 }; |
93 | 95 |
94 } // namespace rtc | 96 } // namespace rtc |
95 | 97 |
96 #endif // WEBRTC_BASE_TIMEUTILS_H_ | 98 #endif // WEBRTC_BASE_TIMEUTILS_H_ |
OLD | NEW |