1b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org/*
2b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *
4b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  Use of this source code is governed by a BSD-style license
5b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  that can be found in the LICENSE file in the root of the source
6b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  tree. An additional intellectual property rights grant can be found
7b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  in the file PATENTS.  All contributing project authors may
8b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  be found in the AUTHORS file in the root of the source tree.
9b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org */
10b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
11b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org// System independant wrapper for polling elapsed time in ms and us.
12b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org// The implementation works in the tick domain which can be mapped over to the
13b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org// time domain.
14b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#ifndef WEBRTC_SYSTEM_WRAPPERS_INTERFACE_TICK_UTIL_H_
15b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#define WEBRTC_SYSTEM_WRAPPERS_INTERFACE_TICK_UTIL_H_
16b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
17b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#if _WIN32
18c6d6fed3c0a82bb7a09095381b974e8e5eebcb35pbos@webrtc.org// Note: The Windows header must always be included before mmsystem.h
19b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#include <windows.h>
20b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#include <mmsystem.h>
21b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#elif WEBRTC_LINUX
223f45c2e0ac4cb280f941efa3a3476895795e3dd6pbos@webrtc.org#include <time.h>
23b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#elif WEBRTC_MAC
24b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#include <mach/mach_time.h>
25b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#include <string.h>
26b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#else
27b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#include <sys/time.h>
28b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#include <time.h>
29b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#endif
30b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
31c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org#include "webrtc/typedefs.h"
32b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
33b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.orgnamespace webrtc {
34c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org
35b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.orgclass TickInterval;
36b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
37c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org// Class representing the current time.
38c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.orgclass TickTime {
39c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org public:
40c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  TickTime();
41c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.org  explicit TickTime(int64_t ticks);
421755c2534d4378ecde390d4b7e9189d8e74c7d10phoglund@webrtc.org
43c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  // Current time in the tick domain.
44c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  static TickTime Now();
45b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
46c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  // Now in the time domain in ms.
47c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.org  static int64_t MillisecondTimestamp();
48b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
49c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  // Now in the time domain in us.
50c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.org  static int64_t MicrosecondTimestamp();
51b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
52c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  // Returns the number of ticks in the tick domain.
53c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.org  int64_t Ticks() const;
54b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
55c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.org  static int64_t MillisecondsToTicks(const int64_t ms);
56b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
57c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.org  static int64_t TicksToMilliseconds(const int64_t ticks);
58b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
59c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  // Returns a TickTime that is ticks later than the passed TickTime.
60c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.org  friend TickTime operator+(const TickTime lhs, const int64_t ticks);
61c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.org  TickTime& operator+=(const int64_t& ticks);
62b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
63c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  // Returns a TickInterval that is the difference in ticks beween rhs and lhs.
64c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  friend TickInterval operator-(const TickTime& lhs, const TickTime& rhs);
651755c2534d4378ecde390d4b7e9189d8e74c7d10phoglund@webrtc.org
66c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  // Call to engage the fake clock. This is useful for tests since relying on
67c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  // a real clock often makes the test flaky.
68c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.org  static void UseFakeClock(int64_t start_millisecond);
691755c2534d4378ecde390d4b7e9189d8e74c7d10phoglund@webrtc.org
70c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  // Advance the fake clock. Must be called after UseFakeClock.
71c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.org  static void AdvanceFakeClock(int64_t milliseconds);
721755c2534d4378ecde390d4b7e9189d8e74c7d10phoglund@webrtc.org
73c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org private:
74c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.org  static int64_t QueryOsForTicks();
751755c2534d4378ecde390d4b7e9189d8e74c7d10phoglund@webrtc.org
76c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  static bool use_fake_clock_;
77c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.org  static int64_t fake_ticks_;
781755c2534d4378ecde390d4b7e9189d8e74c7d10phoglund@webrtc.org
79c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.org  int64_t ticks_;
80b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org};
81b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
82c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org// Represents a time delta in ticks.
83c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.orgclass TickInterval {
84c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org public:
85c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  TickInterval();
86b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
87c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.org  int64_t Milliseconds() const;
88c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.org  int64_t Microseconds() const;
89b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
90c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  // Returns the sum of two TickIntervals as a TickInterval.
91c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  friend TickInterval operator+(const TickInterval& lhs,
92c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org                                const TickInterval& rhs);
93c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  TickInterval& operator+=(const TickInterval& rhs);
94b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
95c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  // Returns a TickInterval corresponding to rhs - lhs.
96c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  friend TickInterval operator-(const TickInterval& lhs,
97c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org                                const TickInterval& rhs);
98c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  TickInterval& operator-=(const TickInterval& rhs);
99b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
100c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  friend bool operator>(const TickInterval& lhs, const TickInterval& rhs);
101c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  friend bool operator<=(const TickInterval& lhs, const TickInterval& rhs);
102c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  friend bool operator<(const TickInterval& lhs, const TickInterval& rhs);
103c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  friend bool operator>=(const TickInterval& lhs, const TickInterval& rhs);
104b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
105c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org private:
106c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.org  explicit TickInterval(int64_t interval);
107b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
108c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  friend class TickTime;
109c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  friend TickInterval operator-(const TickTime& lhs, const TickTime& rhs);
110b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
111c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org private:
112c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.org  int64_t interval_;
113b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org};
114b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
115c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.orginline TickInterval operator+(const TickInterval& lhs,
116c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org                              const TickInterval& rhs) {
117c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  return TickInterval(lhs.interval_ + rhs.interval_);
118b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
119b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
120c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.orginline TickInterval operator-(const TickInterval& lhs,
121c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org                              const TickInterval& rhs) {
122c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  return TickInterval(lhs.interval_ - rhs.interval_);
123b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
124b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
125c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.orginline TickInterval operator-(const TickTime& lhs, const TickTime& rhs) {
126c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  return TickInterval(lhs.ticks_ - rhs.ticks_);
127b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
128b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
129c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.orginline TickTime operator+(const TickTime lhs, const int64_t ticks) {
130c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  TickTime time = lhs;
131c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  time.ticks_ += ticks;
132c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  return time;
133b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
1341755c2534d4378ecde390d4b7e9189d8e74c7d10phoglund@webrtc.org
135c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.orginline bool operator>(const TickInterval& lhs, const TickInterval& rhs) {
136c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  return lhs.interval_ > rhs.interval_;
137b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
1381755c2534d4378ecde390d4b7e9189d8e74c7d10phoglund@webrtc.org
139c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.orginline bool operator<=(const TickInterval& lhs, const TickInterval& rhs) {
140c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  return lhs.interval_ <= rhs.interval_;
141b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
1421755c2534d4378ecde390d4b7e9189d8e74c7d10phoglund@webrtc.org
143c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.orginline bool operator<(const TickInterval& lhs, const TickInterval& rhs) {
144c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  return lhs.interval_ <= rhs.interval_;
145b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
1461755c2534d4378ecde390d4b7e9189d8e74c7d10phoglund@webrtc.org
147c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.orginline bool operator>=(const TickInterval& lhs, const TickInterval& rhs) {
148c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  return lhs.interval_ >= rhs.interval_;
149b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
150b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
1511755c2534d4378ecde390d4b7e9189d8e74c7d10phoglund@webrtc.orginline TickTime::TickTime()
152c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org    : ticks_(0) {
1531755c2534d4378ecde390d4b7e9189d8e74c7d10phoglund@webrtc.org}
1541755c2534d4378ecde390d4b7e9189d8e74c7d10phoglund@webrtc.org
155c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.orginline TickTime::TickTime(int64_t ticks)
156c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org    : ticks_(ticks) {
1571755c2534d4378ecde390d4b7e9189d8e74c7d10phoglund@webrtc.org}
1581755c2534d4378ecde390d4b7e9189d8e74c7d10phoglund@webrtc.org
159c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.orginline TickTime TickTime::Now() {
160c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  if (use_fake_clock_)
161c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org    return TickTime(fake_ticks_);
1621755c2534d4378ecde390d4b7e9189d8e74c7d10phoglund@webrtc.org  else
1631755c2534d4378ecde390d4b7e9189d8e74c7d10phoglund@webrtc.org    return TickTime(QueryOsForTicks());
1641755c2534d4378ecde390d4b7e9189d8e74c7d10phoglund@webrtc.org}
1651755c2534d4378ecde390d4b7e9189d8e74c7d10phoglund@webrtc.org
166c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.orginline int64_t TickTime::MillisecondTimestamp() {
167c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.org  int64_t ticks = TickTime::Now().Ticks();
168b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#if _WIN32
169c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org#ifdef USE_QUERY_PERFORMANCE_COUNTER
170c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  LARGE_INTEGER qpfreq;
171c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  QueryPerformanceFrequency(&qpfreq);
172c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  return (ticks * 1000) / qpfreq.QuadPart;
173c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org#else
174c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  return ticks;
175c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org#endif
176b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#elif defined(WEBRTC_LINUX) || defined(WEBRTC_MAC)
177c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  return ticks / 1000000LL;
178b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#else
179c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  return ticks / 1000LL;
180b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#endif
181b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
182b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
183c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.orginline int64_t TickTime::MicrosecondTimestamp() {
184c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.org  int64_t ticks = TickTime::Now().Ticks();
185b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#if _WIN32
186c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org#ifdef USE_QUERY_PERFORMANCE_COUNTER
187c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  LARGE_INTEGER qpfreq;
188c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  QueryPerformanceFrequency(&qpfreq);
189c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  return (ticks * 1000) / (qpfreq.QuadPart / 1000);
190c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org#else
191c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  return ticks * 1000LL;
192c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org#endif
193b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#elif defined(WEBRTC_LINUX) || defined(WEBRTC_MAC)
194c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  return ticks / 1000LL;
195b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#else
196c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  return ticks;
197b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#endif
198b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
199b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
200c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.orginline int64_t TickTime::Ticks() const {
201c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  return ticks_;
202b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
203b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
204c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.orginline int64_t TickTime::MillisecondsToTicks(const int64_t ms) {
205b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#if _WIN32
206c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org#ifdef USE_QUERY_PERFORMANCE_COUNTER
207c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  LARGE_INTEGER qpfreq;
208c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  QueryPerformanceFrequency(&qpfreq);
209c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  return (qpfreq.QuadPart * ms) / 1000;
210c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org#else
211c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  return ms;
212c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org#endif
213b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#elif defined(WEBRTC_LINUX) || defined(WEBRTC_MAC)
214c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  return ms * 1000000LL;
215b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#else
216c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  return ms * 1000LL;
217b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#endif
218b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
219b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
220c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.orginline int64_t TickTime::TicksToMilliseconds(const int64_t ticks) {
221b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#if _WIN32
222c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org#ifdef USE_QUERY_PERFORMANCE_COUNTER
223c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  LARGE_INTEGER qpfreq;
224c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  QueryPerformanceFrequency(&qpfreq);
225c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  return (ticks * 1000) / qpfreq.QuadPart;
226c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org#else
227c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  return ticks;
228c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org#endif
229b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#elif defined(WEBRTC_LINUX) || defined(WEBRTC_MAC)
230c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  return ticks / 1000000LL;
231b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#else
232c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  return ticks / 1000LL;
233b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#endif
234b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
235b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
236c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.orginline TickTime& TickTime::operator+=(const int64_t& ticks) {
237c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  ticks_ += ticks;
238c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  return *this;
239b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
240b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
241c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.orginline TickInterval::TickInterval() : interval_(0) {
242b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
243b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
244c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.orginline TickInterval::TickInterval(const int64_t interval)
245c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  : interval_(interval) {
246b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
247b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
248c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.orginline int64_t TickInterval::Milliseconds() const {
249b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#if _WIN32
250c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org#ifdef USE_QUERY_PERFORMANCE_COUNTER
251c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  LARGE_INTEGER qpfreq;
252c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  QueryPerformanceFrequency(&qpfreq);
253c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  return (interval_ * 1000) / qpfreq.QuadPart;
254c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org#else
255c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  // interval_ is in ms
256c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  return interval_;
257c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org#endif
258b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#elif defined(WEBRTC_LINUX) || defined(WEBRTC_MAC)
259c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  // interval_ is in ns
260c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  return interval_ / 1000000;
261b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#else
262c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  // interval_ is usecs
263c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  return interval_ / 1000;
264b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#endif
265b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
266b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
267c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.orginline int64_t TickInterval::Microseconds() const {
268b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#if _WIN32
269c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org#ifdef USE_QUERY_PERFORMANCE_COUNTER
270c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  LARGE_INTEGER qpfreq;
271c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  QueryPerformanceFrequency(&qpfreq);
272c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  return (interval_ * 1000000) / qpfreq.QuadPart;
273c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org#else
274c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  // interval_ is in ms
275c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  return interval_ * 1000LL;
276c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org#endif
277b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#elif defined(WEBRTC_LINUX) || defined(WEBRTC_MAC)
278c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  // interval_ is in ns
279c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  return interval_ / 1000;
280b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#else
281c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  // interval_ is usecs
282c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  return interval_;
283b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#endif
284b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
285b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
286c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.orginline TickInterval& TickInterval::operator+=(const TickInterval& rhs) {
287c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  interval_ += rhs.interval_;
288c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  return *this;
289b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
290b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
291c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.orginline TickInterval& TickInterval::operator-=(const TickInterval& rhs) {
292c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  interval_ -= rhs.interval_;
293c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  return *this;
294b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
2951755c2534d4378ecde390d4b7e9189d8e74c7d10phoglund@webrtc.org
296c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org}  // namespace webrtc
297b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
298c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org#endif  // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_TICK_UTIL_H_
299