blob: f443057bea364a3aa8f3f42e5c24cf2a41bbe6f6 [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
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000014#include "webrtc/base/scoped_ptr.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010015#include "webrtc/system_wrappers/include/rw_lock_wrapper.h"
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000016#include "webrtc/typedefs.h"
17
18namespace webrtc {
19
20// January 1970, in NTP seconds.
21const uint32_t kNtpJan1970 = 2208988800UL;
22
23// Magic NTP fractional unit.
24const double kMagicNtpFractionalUnit = 4.294967296E+9;
25
26// A clock interface that allows reading of absolute and relative timestamps.
27class Clock {
28 public:
29 virtual ~Clock() {}
30
31 // Return a timestamp in milliseconds relative to some arbitrary source; the
32 // source is fixed for this clock.
henrik.lundin@webrtc.org190a32f2014-06-09 17:40:49 +000033 virtual int64_t TimeInMilliseconds() const = 0;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000034
35 // Return a timestamp in microseconds relative to some arbitrary source; the
36 // source is fixed for this clock.
henrik.lundin@webrtc.org190a32f2014-06-09 17:40:49 +000037 virtual int64_t TimeInMicroseconds() const = 0;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000038
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000039 // Retrieve an NTP absolute timestamp in seconds and fractions of a second.
henrik.lundin@webrtc.org190a32f2014-06-09 17:40:49 +000040 virtual void CurrentNtp(uint32_t& seconds, uint32_t& fractions) const = 0;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000041
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000042 // Retrieve an NTP absolute timestamp in milliseconds.
henrik.lundin@webrtc.org190a32f2014-06-09 17:40:49 +000043 virtual int64_t CurrentNtpInMilliseconds() const = 0;
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000044
45 // Converts an NTP timestamp to a millisecond timestamp.
46 static int64_t NtpToMs(uint32_t seconds, uint32_t fractions);
47
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000048 // Returns an instance of the real-time system clock implementation.
49 static Clock* GetRealTimeClock();
50};
51
52class SimulatedClock : public Clock {
53 public:
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000054 explicit SimulatedClock(int64_t initial_time_us);
55
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000056 ~SimulatedClock() override;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000057
58 // Return a timestamp in milliseconds relative to some arbitrary source; the
59 // source is fixed for this clock.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000060 int64_t TimeInMilliseconds() const override;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000061
62 // Return a timestamp in microseconds relative to some arbitrary source; the
63 // source is fixed for this clock.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000064 int64_t TimeInMicroseconds() const override;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000065
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000066 // Retrieve an NTP absolute timestamp in milliseconds.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000067 void CurrentNtp(uint32_t& seconds, uint32_t& fractions) const override;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000068
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000069 // Converts an NTP timestamp to a millisecond timestamp.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000070 int64_t CurrentNtpInMilliseconds() const override;
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000071
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000072 // Advance the simulated clock with a given number of milliseconds or
73 // microseconds.
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +000074 void AdvanceTimeMilliseconds(int64_t milliseconds);
75 void AdvanceTimeMicroseconds(int64_t microseconds);
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000076
77 private:
henrike@webrtc.org7ea71de2014-06-26 16:13:58 +000078 int64_t time_us_;
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000079 rtc::scoped_ptr<RWLockWrapper> lock_;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000080};
81
82}; // namespace webrtc
83
kjellanderd56d68c2015-11-02 02:12:41 -080084#endif // WEBRTC_SYSTEM_WRAPPERS_INCLUDE_CLOCK_H_