blob: 0164288785fd5db067ee73a39ee4206bb371611e [file] [log] [blame]
stefan@webrtc.org20ed36d2013-01-17 14:01:201/*
2 * Copyright (c) 2013 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
Mirko Bonadei92ea95e2017-09-15 04:47:3111#ifndef SYSTEM_WRAPPERS_INCLUDE_CLOCK_H_
12#define SYSTEM_WRAPPERS_INCLUDE_CLOCK_H_
stefan@webrtc.org20ed36d2013-01-17 14:01:2013
kwibergbfefb032016-05-01 21:53:4614#include <memory>
15
Karl Wiberg2b857922018-03-23 13:53:5416#include "rtc_base/synchronization/rw_lock_wrapper.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3117#include "system_wrappers/include/ntp_time.h"
Mirko Bonadei71207422017-09-15 11:58:0918#include "typedefs.h" // NOLINT(build/include)
stefan@webrtc.org20ed36d2013-01-17 14:01:2019
20namespace webrtc {
21
22// January 1970, in NTP seconds.
23const uint32_t kNtpJan1970 = 2208988800UL;
24
25// Magic NTP fractional unit.
26const double kMagicNtpFractionalUnit = 4.294967296E+9;
27
28// A clock interface that allows reading of absolute and relative timestamps.
29class Clock {
30 public:
31 virtual ~Clock() {}
32
33 // Return a timestamp in milliseconds relative to some arbitrary source; the
34 // source is fixed for this clock.
henrik.lundin@webrtc.org190a32f2014-06-09 17:40:4935 virtual int64_t TimeInMilliseconds() const = 0;
stefan@webrtc.org20ed36d2013-01-17 14:01:2036
37 // Return a timestamp in microseconds relative to some arbitrary source; the
38 // source is fixed for this clock.
henrik.lundin@webrtc.org190a32f2014-06-09 17:40:4939 virtual int64_t TimeInMicroseconds() const = 0;
stefan@webrtc.org20ed36d2013-01-17 14:01:2040
danilchap21dc1892017-03-07 10:51:0941 // Retrieve an NTP absolute timestamp.
42 virtual NtpTime CurrentNtpTime() const = 0;
stefan@webrtc.org20ed36d2013-01-17 14:01:2043
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:2344 // Retrieve an NTP absolute timestamp in milliseconds.
henrik.lundin@webrtc.org190a32f2014-06-09 17:40:4945 virtual int64_t CurrentNtpInMilliseconds() const = 0;
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:2346
47 // Converts an NTP timestamp to a millisecond timestamp.
danilchap37953762017-02-09 19:15:2548 static int64_t NtpToMs(uint32_t seconds, uint32_t fractions) {
49 return NtpTime(seconds, fractions).ToMs();
50 }
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:2351
stefan@webrtc.org20ed36d2013-01-17 14:01:2052 // Returns an instance of the real-time system clock implementation.
53 static Clock* GetRealTimeClock();
54};
55
56class SimulatedClock : public Clock {
57 public:
stefan@webrtc.org20ed36d2013-01-17 14:01:2058 explicit SimulatedClock(int64_t initial_time_us);
59
kjellander@webrtc.org14665ff2015-03-04 12:58:3560 ~SimulatedClock() override;
stefan@webrtc.org20ed36d2013-01-17 14:01:2061
62 // Return a timestamp in milliseconds relative to some arbitrary source; the
63 // source is fixed for this clock.
kjellander@webrtc.org14665ff2015-03-04 12:58:3564 int64_t TimeInMilliseconds() const override;
stefan@webrtc.org20ed36d2013-01-17 14:01:2065
66 // Return a timestamp in microseconds relative to some arbitrary source; the
67 // source is fixed for this clock.
kjellander@webrtc.org14665ff2015-03-04 12:58:3568 int64_t TimeInMicroseconds() const override;
stefan@webrtc.org20ed36d2013-01-17 14:01:2069
danilchap21dc1892017-03-07 10:51:0970 // Retrieve an NTP absolute timestamp.
71 NtpTime CurrentNtpTime() const override;
stefan@webrtc.org20ed36d2013-01-17 14:01:2072
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:2373 // Converts an NTP timestamp to a millisecond timestamp.
kjellander@webrtc.org14665ff2015-03-04 12:58:3574 int64_t CurrentNtpInMilliseconds() const override;
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:2375
stefan@webrtc.org20ed36d2013-01-17 14:01:2076 // Advance the simulated clock with a given number of milliseconds or
77 // microseconds.
stefan@webrtc.orga678a3b2013-01-21 07:42:1178 void AdvanceTimeMilliseconds(int64_t milliseconds);
79 void AdvanceTimeMicroseconds(int64_t microseconds);
stefan@webrtc.org20ed36d2013-01-17 14:01:2080
81 private:
henrike@webrtc.org7ea71de2014-06-26 16:13:5882 int64_t time_us_;
kwibergbfefb032016-05-01 21:53:4683 std::unique_ptr<RWLockWrapper> lock_;
stefan@webrtc.org20ed36d2013-01-17 14:01:2084};
85
86}; // namespace webrtc
87
Mirko Bonadei92ea95e2017-09-15 04:47:3188#endif // SYSTEM_WRAPPERS_INCLUDE_CLOCK_H_