15738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project/*
25738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
35738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *
45738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *  Use of this source code is governed by a BSD-style license
55738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *  that can be found in the LICENSE file in the root of the source
65738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *  tree. An additional intellectual property rights grant can be found
75738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *  in the file PATENTS.  All contributing project authors may
85738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *  be found in the AUTHORS file in the root of the source tree.
95738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project */
105738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
115738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project// System independant wrapper for polling elapsed time in ms and us.
125738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project// The implementation works in the tick domain which can be mapped over to the
135738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project// time domain.
145738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#ifndef WEBRTC_SYSTEM_WRAPPERS_INTERFACE_TICK_UTIL_H_
155738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#define WEBRTC_SYSTEM_WRAPPERS_INTERFACE_TICK_UTIL_H_
165738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
175738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#if _WIN32
185738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#include <windows.h>
195738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#include <mmsystem.h>
205738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#elif WEBRTC_LINUX
215738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#include <ctime>
225738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#elif WEBRTC_MAC
235738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#include <mach/mach_time.h>
245738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#include <string.h>
255738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#else
265738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#include <sys/time.h>
275738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#include <time.h>
285738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#endif
295738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
305738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#include "typedefs.h"
315738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
325738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Projectnamespace webrtc {
335738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Projectclass TickInterval;
345738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
355738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Projectclass TickTime
365738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project{
375738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Projectpublic:
385738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    // Current time in the tick domain.
395738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    static TickTime Now();
405738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
415738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    // Now in the time domain in ms.
425738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    static WebRtc_Word64 MillisecondTimestamp();
435738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
445738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    // Now in the time domain in us.
455738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    static WebRtc_Word64 MicrosecondTimestamp();
465738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
475738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    WebRtc_Word64 Ticks() const;
485738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
495738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    static WebRtc_Word64 MillisecondsToTicks(const WebRtc_Word64 ms);
505738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
515738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    static WebRtc_Word64 TicksToMilliseconds(const WebRtc_Word64 ticks);
525738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
535738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    // Returns a TickTime that is ticks later than the passed TickTime
545738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    friend TickTime operator+(const TickTime lhs, const WebRtc_Word64 ticks);
555738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    TickTime& operator+=(const WebRtc_Word64& rhs);
565738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
575738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
585738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    // Returns a TickInterval that is the difference in ticks beween rhs and lhs
595738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    friend TickInterval operator-(const TickTime& lhs, const TickTime& rhs);
605738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Projectprivate:
615738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    WebRtc_Word64 _ticks;
625738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project};
635738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
645738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Projectclass TickInterval
655738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project{
665738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Projectpublic:
675738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    TickInterval();
685738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
695738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    WebRtc_Word64 Milliseconds() const;
705738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    WebRtc_Word64 Microseconds() const;
715738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
725738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    // Returns the sum of two TickIntervals as a TickInterval
735738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    friend TickInterval operator+(const TickInterval& lhs,
745738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project                                  const TickInterval& rhs);
755738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    TickInterval& operator-=(const TickInterval& rhs);
765738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
775738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    // Returns a TickInterval corresponding to rhs - lhs
785738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    friend TickInterval operator-(const TickInterval& lhs,
795738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project                                  const TickInterval& rhs);
805738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    TickInterval& operator+=(const TickInterval& rhs);
815738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
825738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    friend bool operator>(const TickInterval& lhs, const TickInterval& rhs);
835738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    friend bool operator<=(const TickInterval& lhs, const TickInterval& rhs);
845738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    friend bool operator<(const TickInterval& lhs, const TickInterval& rhs);
855738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    friend bool operator>=(const TickInterval& lhs, const TickInterval& rhs);
865738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
875738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Projectprivate:
885738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    TickInterval(WebRtc_Word64 interval);
895738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
905738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    friend class TickTime;
915738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    friend TickInterval operator-(const TickTime& lhs, const TickTime& rhs);
925738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
935738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Projectprivate:
945738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    WebRtc_Word64 _interval;
955738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project};
965738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
975738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Projectinline TickInterval operator+(const TickInterval& lhs, const TickInterval& rhs)
985738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project{
995738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    return TickInterval(lhs._interval + rhs._interval);
1005738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project}
1015738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
1025738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Projectinline TickInterval operator-(const TickInterval& lhs, const TickInterval& rhs)
1035738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project{
1045738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    return TickInterval(lhs._interval - rhs._interval);
1055738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project}
1065738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
1075738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Projectinline TickInterval operator-(const TickTime& lhs,const TickTime& rhs)
1085738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project{
1095738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    return TickInterval(lhs._ticks - rhs._ticks);
1105738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project}
1115738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
1125738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Projectinline TickTime operator+(const TickTime lhs, const WebRtc_Word64 ticks)
1135738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project{
1145738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    TickTime time = lhs;
1155738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    time._ticks += ticks;
1165738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    return time;
1175738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project}
1185738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Projectinline bool operator>(const TickInterval& lhs, const TickInterval& rhs)
1195738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project{
1205738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    return lhs._interval > rhs._interval;
1215738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project}
1225738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Projectinline bool operator<=(const TickInterval& lhs, const TickInterval& rhs)
1235738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project{
1245738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    return lhs._interval <= rhs._interval;
1255738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project}
1265738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Projectinline bool operator<(const TickInterval& lhs, const TickInterval& rhs)
1275738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project{
1285738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    return lhs._interval <= rhs._interval;
1295738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project}
1305738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Projectinline bool operator>=(const TickInterval& lhs, const TickInterval& rhs)
1315738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project{
1325738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    return lhs._interval >= rhs._interval;
1335738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project}
1345738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
1355738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Projectinline TickTime TickTime::Now()
1365738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project{
1375738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    TickTime result;
1385738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#if _WIN32
1395738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    // TODO(wu): Remove QueryPerformanceCounter implementation.
1405738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    #ifdef USE_QUERY_PERFORMANCE_COUNTER
1415738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        // QueryPerformanceCounter returns the value from the TSC which is
1425738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        // incremented at the CPU frequency. The algorithm used requires
1435738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        // the CPU frequency to be constant. Technology like speed stepping
1445738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        // which has variable CPU frequency will therefore yield unpredictable,
1455738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        // incorrect time estimations.
1465738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        LARGE_INTEGER qpcnt;
1475738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        QueryPerformanceCounter(&qpcnt);
1485738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        result._ticks = qpcnt.QuadPart;
1495738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    #else
1505738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        static volatile LONG lastTimeGetTime = 0;
1515738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        static volatile WebRtc_Word64 numWrapTimeGetTime = 0;
1525738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        volatile LONG* lastTimeGetTimePtr = &lastTimeGetTime;
1535738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        DWORD now = timeGetTime();
1545738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        // Atomically update the last gotten time
1555738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        DWORD old = InterlockedExchange(lastTimeGetTimePtr, now);
1565738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        if(now < old)
1575738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        {
1585738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project            // If now is earlier than old, there may have been a race between
1595738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project            // threads.
1605738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project            // 0x0fffffff ~3.1 days, the code will not take that long to execute
1615738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project            // so it must have been a wrap around.
1625738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project            if(old > 0xf0000000 && now < 0x0fffffff)
1635738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project            {
1645738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project                numWrapTimeGetTime++;
1655738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project            }
1665738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        }
1675738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        result._ticks = now + (numWrapTimeGetTime<<32);
1685738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    #endif
1695738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#elif defined(WEBRTC_LINUX)
1705738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    struct timespec ts;
1715738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    // TODO(wu): Remove CLOCK_REALTIME implementation.
1725738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    #ifdef WEBRTC_CLOCK_TYPE_REALTIME
1735738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        clock_gettime(CLOCK_REALTIME, &ts);
1745738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    #else
1755738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        clock_gettime(CLOCK_MONOTONIC, &ts);
1765738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    #endif
1775738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    result._ticks = 1000000000LL * static_cast<WebRtc_Word64>(ts.tv_sec) + static_cast<WebRtc_Word64>(ts.tv_nsec);
1785738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#elif defined(WEBRTC_MAC)
1795738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    static mach_timebase_info_data_t timebase;
1805738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    if (timebase.denom == 0) {
1815738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project      // Get the timebase if this is the first time we run.
1825738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project      // Recommended by Apple's QA1398.
1835738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project      kern_return_t retval = mach_timebase_info(&timebase);
1845738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project      if (retval != KERN_SUCCESS) {
1855738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        // TODO(wu): Implement CHECK similar to chrome for all the platforms.
1865738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        // Then replace this with a CHECK(retval == KERN_SUCCESS);
1875738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        asm("int3");
1885738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project      }
1895738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    }
1905738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    // Use timebase to convert absolute time tick units into nanoseconds.
1915738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    result._ticks = mach_absolute_time() * timebase.numer / timebase.denom;
1925738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#else
1935738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    struct timeval tv;
1945738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    gettimeofday(&tv, NULL);
1955738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    result._ticks = 1000000LL * static_cast<WebRtc_Word64>(tv.tv_sec) + static_cast<WebRtc_Word64>(tv.tv_usec);
1965738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#endif
1975738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    return result;
1985738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project}
1995738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
2005738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Projectinline WebRtc_Word64 TickTime::MillisecondTimestamp()
2015738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project{
2025738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    TickTime now = TickTime::Now();
2035738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#if _WIN32
2045738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    #ifdef USE_QUERY_PERFORMANCE_COUNTER
2055738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        LARGE_INTEGER qpfreq;
2065738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        QueryPerformanceFrequency(&qpfreq);
2075738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        return (now._ticks * 1000) / qpfreq.QuadPart;
2085738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    #else
2095738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        return now._ticks;
2105738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    #endif
2115738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#elif defined(WEBRTC_LINUX) || defined(WEBRTC_MAC)
2125738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    return now._ticks / 1000000LL;
2135738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#else
2145738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    return now._ticks / 1000LL;
2155738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#endif
2165738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project}
2175738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
2185738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Projectinline WebRtc_Word64 TickTime::MicrosecondTimestamp()
2195738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project{
2205738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    TickTime now = TickTime::Now();
2215738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
2225738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#if _WIN32
2235738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    #ifdef USE_QUERY_PERFORMANCE_COUNTER
2245738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        LARGE_INTEGER qpfreq;
2255738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        QueryPerformanceFrequency(&qpfreq);
2265738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        return (now._ticks * 1000) / (qpfreq.QuadPart/1000);
2275738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    #else
2285738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        return now._ticks *1000LL;
2295738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    #endif
2305738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#elif defined(WEBRTC_LINUX) || defined(WEBRTC_MAC)
2315738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    return now._ticks / 1000LL;
2325738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#else
2335738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    return now._ticks;
2345738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#endif
2355738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project}
2365738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
2375738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Projectinline WebRtc_Word64 TickTime::Ticks() const
2385738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project{
2395738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    return _ticks;
2405738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project}
2415738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
2425738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Projectinline WebRtc_Word64 TickTime::MillisecondsToTicks(const WebRtc_Word64 ms)
2435738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project{
2445738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#if _WIN32
2455738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    #ifdef USE_QUERY_PERFORMANCE_COUNTER
2465738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        LARGE_INTEGER qpfreq;
2475738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        QueryPerformanceFrequency(&qpfreq);
2485738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        return (qpfreq.QuadPart * ms) / 1000;
2495738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    #else
2505738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        return ms;
2515738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    #endif
2525738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#elif defined(WEBRTC_LINUX) || defined(WEBRTC_MAC)
2535738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    return ms * 1000000LL;
2545738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#else
2555738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    return ms * 1000LL;
2565738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#endif
2575738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project}
2585738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
2595738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Projectinline WebRtc_Word64 TickTime::TicksToMilliseconds(const WebRtc_Word64 ticks)
2605738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project{
2615738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#if _WIN32
2625738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    #ifdef USE_QUERY_PERFORMANCE_COUNTER
2635738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        LARGE_INTEGER qpfreq;
2645738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        QueryPerformanceFrequency(&qpfreq);
2655738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        return (ticks * 1000) / qpfreq.QuadPart;
2665738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    #else
2675738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        return ticks;
2685738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    #endif
2695738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#elif defined(WEBRTC_LINUX) || defined(WEBRTC_MAC)
2705738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    return ticks / 1000000LL;
2715738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#else
2725738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    return ticks / 1000LL;
2735738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#endif
2745738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project}
2755738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
2765738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Projectinline TickTime& TickTime::operator+=(const WebRtc_Word64& ticks)
2775738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project{
2785738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    _ticks += ticks;
2795738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    return *this;
2805738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project}
2815738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
2825738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Projectinline TickInterval::TickInterval() : _interval(0)
2835738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project{
2845738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project}
2855738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
2865738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Projectinline TickInterval::TickInterval(const WebRtc_Word64 interval)
2875738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    : _interval(interval)
2885738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project{
2895738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project}
2905738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
2915738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Projectinline WebRtc_Word64 TickInterval::Milliseconds() const
2925738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project{
2935738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#if _WIN32
2945738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    #ifdef USE_QUERY_PERFORMANCE_COUNTER
2955738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        LARGE_INTEGER qpfreq;
2965738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        QueryPerformanceFrequency(&qpfreq);
2975738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        return (_interval * 1000) / qpfreq.QuadPart;
2985738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    #else
2995738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project	// _interval is in ms
3005738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        return _interval;
3015738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    #endif
3025738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#elif defined(WEBRTC_LINUX) || defined(WEBRTC_MAC)
3035738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    // _interval is in ns
3045738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    return _interval / 1000000;
3055738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#else
3065738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    // _interval is usecs
3075738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    return _interval / 1000;
3085738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#endif
3095738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project}
3105738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
3115738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Projectinline WebRtc_Word64 TickInterval::Microseconds() const
3125738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project{
3135738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#if _WIN32
3145738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    #ifdef USE_QUERY_PERFORMANCE_COUNTER
3155738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        LARGE_INTEGER qpfreq;
3165738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        QueryPerformanceFrequency(&qpfreq);
3175738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        return (_interval * 1000000) / qpfreq.QuadPart;
3185738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    #else
3195738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project	// _interval is in ms
3205738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project        return _interval *1000LL;
3215738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    #endif
3225738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#elif defined(WEBRTC_LINUX) || defined(WEBRTC_MAC)
3235738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    // _interval is in ns
3245738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    return _interval / 1000;
3255738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#else
3265738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    // _interval is usecs
3275738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    return _interval;
3285738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#endif
3295738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project}
3305738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
3315738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Projectinline TickInterval& TickInterval::operator+=(const TickInterval& rhs)
3325738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project{
3335738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    _interval += rhs._interval;
3345738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    return *this;
3355738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project}
3365738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
3375738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Projectinline TickInterval& TickInterval::operator-=(const TickInterval& rhs)
3385738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project{
3395738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    _interval -= rhs._interval;
3405738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    return *this;
3415738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project}
3425738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project} // namespace webrtc
3435738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
3445738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_TICK_UTIL_H_
3455738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project