blob: 506684496cdf00763c2c49c7199a9b6e60cfe9dc [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
kjellanderd56d68c2015-11-02 02:12:41 -080011#ifndef WEBRTC_SYSTEM_WRAPPERS_INCLUDE_CLOCK_H_
12#define WEBRTC_SYSTEM_WRAPPERS_INCLUDE_CLOCK_H_
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000013
kwibergbfefb032016-05-01 14:53:46 -070014#include <memory>
15
danilchap37953762017-02-09 11:15:25 -080016#include "webrtc/system_wrappers/include/ntp_time.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010017#include "webrtc/system_wrappers/include/rw_lock_wrapper.h"
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000018#include "webrtc/typedefs.h"
19
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:49 +000035 virtual int64_t TimeInMilliseconds() const = 0;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000036
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:49 +000039 virtual int64_t TimeInMicroseconds() const = 0;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000040
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000041 // Retrieve an NTP absolute timestamp in seconds and fractions of a second.
henrik.lundin@webrtc.org190a32f2014-06-09 17:40:49 +000042 virtual void CurrentNtp(uint32_t& seconds, uint32_t& fractions) const = 0;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000043
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000044 // Retrieve an NTP absolute timestamp in milliseconds.
henrik.lundin@webrtc.org190a32f2014-06-09 17:40:49 +000045 virtual int64_t CurrentNtpInMilliseconds() const = 0;
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000046
danilchap37953762017-02-09 11:15:25 -080047 // TODO(danilchap): Make pure virtual once implemented in derived classed
48 // replacing CurrentNtp function.
49 virtual NtpTime CurrentNtpTime() const;
50
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000051 // Converts an NTP timestamp to a millisecond timestamp.
danilchap37953762017-02-09 11:15:25 -080052 static int64_t NtpToMs(uint32_t seconds, uint32_t fractions) {
53 return NtpTime(seconds, fractions).ToMs();
54 }
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000055
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000056 // Returns an instance of the real-time system clock implementation.
57 static Clock* GetRealTimeClock();
58};
59
60class SimulatedClock : public Clock {
61 public:
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000062 explicit SimulatedClock(int64_t initial_time_us);
63
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000064 ~SimulatedClock() override;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000065
66 // Return a timestamp in milliseconds relative to some arbitrary source; the
67 // source is fixed for this clock.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000068 int64_t TimeInMilliseconds() const override;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000069
70 // Return a timestamp in microseconds relative to some arbitrary source; the
71 // source is fixed for this clock.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000072 int64_t TimeInMicroseconds() const override;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000073
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000074 // Retrieve an NTP absolute timestamp in milliseconds.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000075 void CurrentNtp(uint32_t& seconds, uint32_t& fractions) const override;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000076
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000077 // Converts an NTP timestamp to a millisecond timestamp.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000078 int64_t CurrentNtpInMilliseconds() const override;
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000079
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000080 // Advance the simulated clock with a given number of milliseconds or
81 // microseconds.
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +000082 void AdvanceTimeMilliseconds(int64_t milliseconds);
83 void AdvanceTimeMicroseconds(int64_t microseconds);
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000084
85 private:
henrike@webrtc.org7ea71de2014-06-26 16:13:58 +000086 int64_t time_us_;
kwibergbfefb032016-05-01 14:53:46 -070087 std::unique_ptr<RWLockWrapper> lock_;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000088};
89
90}; // namespace webrtc
91
kjellanderd56d68c2015-11-02 02:12:41 -080092#endif // WEBRTC_SYSTEM_WRAPPERS_INCLUDE_CLOCK_H_