blob: 38ce0d587145abe52c3da2209863e8af38295100 [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
11#ifndef WEBRTC_SYSTEM_WRAPPERS_INTERFACE_CLOCK_H_
12#define WEBRTC_SYSTEM_WRAPPERS_INTERFACE_CLOCK_H_
13
henrik.lundin@webrtc.org59336e82014-05-27 09:34:58 +000014#include "webrtc/system_wrappers/interface/scoped_ptr.h"
15#include "webrtc/system_wrappers/interface/thread_annotations.h"
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000016#include "webrtc/typedefs.h"
17
18namespace webrtc {
19
henrik.lundin@webrtc.org59336e82014-05-27 09:34:58 +000020class RWLockWrapper;
21
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000022// 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.
35 virtual int64_t TimeInMilliseconds() = 0;
36
37 // Return a timestamp in microseconds relative to some arbitrary source; the
38 // source is fixed for this clock.
39 virtual int64_t TimeInMicroseconds() = 0;
40
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000041 // Retrieve an NTP absolute timestamp in seconds and fractions of a second.
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000042 virtual void CurrentNtp(uint32_t& seconds, uint32_t& fractions) = 0;
43
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000044 // Retrieve an NTP absolute timestamp in milliseconds.
45 virtual int64_t CurrentNtpInMilliseconds() = 0;
46
47 // Converts an NTP timestamp to a millisecond timestamp.
48 static int64_t NtpToMs(uint32_t seconds, uint32_t fractions);
49
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000050 // Returns an instance of the real-time system clock implementation.
51 static Clock* GetRealTimeClock();
52};
53
54class SimulatedClock : public Clock {
55 public:
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000056 explicit SimulatedClock(int64_t initial_time_us);
57
henrik.lundin@webrtc.org59336e82014-05-27 09:34:58 +000058 virtual ~SimulatedClock();
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000059
60 // Return a timestamp in milliseconds relative to some arbitrary source; the
61 // source is fixed for this clock.
pbos@webrtc.orga2a27182013-08-01 17:26:15 +000062 virtual int64_t TimeInMilliseconds() OVERRIDE;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000063
64 // Return a timestamp in microseconds relative to some arbitrary source; the
65 // source is fixed for this clock.
pbos@webrtc.orga2a27182013-08-01 17:26:15 +000066 virtual int64_t TimeInMicroseconds() OVERRIDE;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000067
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000068 // Retrieve an NTP absolute timestamp in milliseconds.
pbos@webrtc.orga2a27182013-08-01 17:26:15 +000069 virtual void CurrentNtp(uint32_t& seconds, uint32_t& fractions) OVERRIDE;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000070
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000071 // Converts an NTP timestamp to a millisecond timestamp.
pbos@webrtc.orga2a27182013-08-01 17:26:15 +000072 virtual int64_t CurrentNtpInMilliseconds() OVERRIDE;
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000073
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000074 // Advance the simulated clock with a given number of milliseconds or
75 // microseconds.
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +000076 void AdvanceTimeMilliseconds(int64_t milliseconds);
77 void AdvanceTimeMicroseconds(int64_t microseconds);
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000078
79 private:
henrik.lundin@webrtc.org59336e82014-05-27 09:34:58 +000080 int64_t time_us_ GUARDED_BY(lock_);
81 scoped_ptr<RWLockWrapper> lock_;
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000082};
83
84}; // namespace webrtc
85
86#endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_CLOCK_H_