blob: 4b6eab8edcebe598a3b8dcad83081ab243b2f2c5 [file] [log] [blame]
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +00001/*
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 06:47:31 +020011#ifndef SYSTEM_WRAPPERS_INCLUDE_CLOCK_H_
12#define SYSTEM_WRAPPERS_INCLUDE_CLOCK_H_
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000013
kwibergbfefb032016-05-01 14:53:46 -070014#include <memory>
15
Karl Wiberg2b857922018-03-23 14:53:54 +010016#include "rtc_base/synchronization/rw_lock_wrapper.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "system_wrappers/include/ntp_time.h"
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000018
19namespace webrtc {
20
21// January 1970, in NTP seconds.
22const uint32_t kNtpJan1970 = 2208988800UL;
23
24// Magic NTP fractional unit.
25const double kMagicNtpFractionalUnit = 4.294967296E+9;
26
27// A clock interface that allows reading of absolute and relative timestamps.
28class Clock {
29 public:
30 virtual ~Clock() {}
31
32 // Return a timestamp in milliseconds relative to some arbitrary source; the
33 // source is fixed for this clock.
henrik.lundin@webrtc.org190a32f2014-06-09 17:40:49 +000034 virtual int64_t TimeInMilliseconds() const = 0;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000035
36 // Return a timestamp in microseconds relative to some arbitrary source; the
37 // source is fixed for this clock.
henrik.lundin@webrtc.org190a32f2014-06-09 17:40:49 +000038 virtual int64_t TimeInMicroseconds() const = 0;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000039
danilchap21dc1892017-03-07 02:51:09 -080040 // Retrieve an NTP absolute timestamp.
41 virtual NtpTime CurrentNtpTime() const = 0;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000042
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000043 // Retrieve an NTP absolute timestamp in milliseconds.
henrik.lundin@webrtc.org190a32f2014-06-09 17:40:49 +000044 virtual int64_t CurrentNtpInMilliseconds() const = 0;
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000045
46 // Converts an NTP timestamp to a millisecond timestamp.
danilchap37953762017-02-09 11:15:25 -080047 static int64_t NtpToMs(uint32_t seconds, uint32_t fractions) {
48 return NtpTime(seconds, fractions).ToMs();
49 }
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000050
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000051 // Returns an instance of the real-time system clock implementation.
52 static Clock* GetRealTimeClock();
53};
54
55class SimulatedClock : public Clock {
56 public:
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000057 explicit SimulatedClock(int64_t initial_time_us);
58
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000059 ~SimulatedClock() override;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000060
61 // Return a timestamp in milliseconds relative to some arbitrary source; the
62 // source is fixed for this clock.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000063 int64_t TimeInMilliseconds() const override;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000064
65 // Return a timestamp in microseconds relative to some arbitrary source; the
66 // source is fixed for this clock.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000067 int64_t TimeInMicroseconds() const override;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000068
danilchap21dc1892017-03-07 02:51:09 -080069 // Retrieve an NTP absolute timestamp.
70 NtpTime CurrentNtpTime() const override;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000071
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000072 // Converts an NTP timestamp to a millisecond timestamp.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000073 int64_t CurrentNtpInMilliseconds() const override;
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000074
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000075 // Advance the simulated clock with a given number of milliseconds or
76 // microseconds.
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +000077 void AdvanceTimeMilliseconds(int64_t milliseconds);
78 void AdvanceTimeMicroseconds(int64_t microseconds);
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000079
80 private:
henrike@webrtc.org7ea71de2014-06-26 16:13:58 +000081 int64_t time_us_;
kwibergbfefb032016-05-01 14:53:46 -070082 std::unique_ptr<RWLockWrapper> lock_;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000083};
84
85}; // namespace webrtc
86
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020087#endif // SYSTEM_WRAPPERS_INCLUDE_CLOCK_H_