blob: 7a946d90c4f176f9d6a54109ef11949989191bd4 [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
14#include "webrtc/typedefs.h"
15
16namespace webrtc {
17
18// January 1970, in NTP seconds.
19const uint32_t kNtpJan1970 = 2208988800UL;
20
21// Magic NTP fractional unit.
22const double kMagicNtpFractionalUnit = 4.294967296E+9;
23
24// A clock interface that allows reading of absolute and relative timestamps.
25class Clock {
26 public:
27 virtual ~Clock() {}
28
29 // Return a timestamp in milliseconds relative to some arbitrary source; the
30 // source is fixed for this clock.
31 virtual int64_t TimeInMilliseconds() = 0;
32
33 // Return a timestamp in microseconds relative to some arbitrary source; the
34 // source is fixed for this clock.
35 virtual int64_t TimeInMicroseconds() = 0;
36
stefan@webrtc.org7da34592013-04-09 14:56:29 +000037 // Retrieve an NTP absolute timestamp.
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000038 virtual void CurrentNtp(uint32_t& seconds, uint32_t& fractions) = 0;
39
40 // Returns an instance of the real-time system clock implementation.
41 static Clock* GetRealTimeClock();
42};
43
44class SimulatedClock : public Clock {
45 public:
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000046 explicit SimulatedClock(int64_t initial_time_us);
47
48 virtual ~SimulatedClock() {}
49
50 // Return a timestamp in milliseconds relative to some arbitrary source; the
51 // source is fixed for this clock.
52 virtual int64_t TimeInMilliseconds();
53
54 // Return a timestamp in microseconds relative to some arbitrary source; the
55 // source is fixed for this clock.
56 virtual int64_t TimeInMicroseconds();
57
stefan@webrtc.org7da34592013-04-09 14:56:29 +000058 // Retrieve an NTP absolute timestamp.
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000059 virtual void CurrentNtp(uint32_t& seconds, uint32_t& fractions);
60
61 // Advance the simulated clock with a given number of milliseconds or
62 // microseconds.
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +000063 void AdvanceTimeMilliseconds(int64_t milliseconds);
64 void AdvanceTimeMicroseconds(int64_t microseconds);
stefan@webrtc.org20ed36d2013-01-17 14:01:20 +000065
66 private:
67 int64_t time_us_;
68};
69
70}; // namespace webrtc
71
72#endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_CLOCK_H_