From b7071869d1830f9e8c6d069d8652c4ff43b51a00 Mon Sep 17 00:00:00 2001 From: Davide Gerhard Date: Sat, 11 May 2019 11:14:59 +0200 Subject: [PATCH] fix clock_gettime() con macOS <10.12 (not provided by base) --- custom/apple/apple_compat.c | 21 +--------------- custom/apple/apple_compat.h | 48 ++++++++++++++++++++++++++++++------- qrtplib/CMakeLists.txt | 2 -- qrtplib/rtptimeutilities.h | 4 ++++ 4 files changed, 45 insertions(+), 30 deletions(-) diff --git a/custom/apple/apple_compat.c b/custom/apple/apple_compat.c index c8775a1d7..642fb96e7 100644 --- a/custom/apple/apple_compat.c +++ b/custom/apple/apple_compat.c @@ -57,23 +57,4 @@ int pthread_barrier_wait(pthread_barrier_t *barrier) } } -#ifdef _DARWIN_FEATURE_CLOCK_GETTIME -/** - * Missing POSIX RealTime/Monotonic Clock - */ -#include - -int clock_gettime(int clk_id, struct timespec *t) { - mach_timebase_info_data_t timebase; - mach_timebase_info(&timebase); - uint64_t time; - time = mach_absolute_time(); - double nseconds = ((double)time * (double)timebase.numer)/((double)timebase.denom); - double seconds = ((double)time * (double)timebase.numer)/((double)timebase.denom * 1e9); - t->tv_sec = seconds; - t->tv_nsec = nseconds; - return 0; -} -#endif - -#endif // APPLE Compatibility +#endif // __APPLE_ diff --git a/custom/apple/apple_compat.h b/custom/apple/apple_compat.h index ea583d3fa..8ab0956b2 100644 --- a/custom/apple/apple_compat.h +++ b/custom/apple/apple_compat.h @@ -31,13 +31,45 @@ int pthread_barrier_wait(pthread_barrier_t *barrier); #endif // PTHREAD_BARRIER_H_ -// -#ifndef CLOCK_REALTIME -# define CLOCK_REALTIME 0 -#endif -#ifndef CLOCK_MONOTONIC -# define CLOCK_MONOTONIC 0 -#endif +// macOS < 10.12 doesn't have clock_gettime() +#include +#if !defined(CLOCK_REALTIME) && !defined(CLOCK_MONOTONIC) -#endif // APPLE Compatibility +#define CLOCK_REALTIME 0 +#define CLOCK_MONOTONIC 6 +typedef int clockid_t; + +#include +#include + +// here to avoid problem on linking of qrtplib +inline int clock_gettime( clockid_t clk_id, struct timespec *ts ) +{ + int ret = -1; + if ( ts ) + { + if ( CLOCK_REALTIME == clk_id ) + { + struct timeval tv; + ret = gettimeofday(&tv, NULL); + ts->tv_sec = tv.tv_sec; + ts->tv_nsec = tv.tv_usec * 1000; + } + else if ( CLOCK_MONOTONIC == clk_id ) + { + const uint64_t t = mach_absolute_time(); + mach_timebase_info_data_t timebase; + mach_timebase_info(&timebase); + const uint64_t tdiff = t * timebase.numer / timebase.denom; + ts->tv_sec = tdiff / 1000000000; + ts->tv_nsec = tdiff % 1000000000; + ret = 0; + } + } + return ret; +} + +#endif // CLOCK_REALTIME and CLOCK_MONOTONIC + +#endif // __APPLE__ diff --git a/qrtplib/CMakeLists.txt b/qrtplib/CMakeLists.txt index 072a61d55..0c75ee075 100644 --- a/qrtplib/CMakeLists.txt +++ b/qrtplib/CMakeLists.txt @@ -1,7 +1,6 @@ project(qrtplib) set (qrtplib_HEADERS - ../custom/apple/apple_compat.h rtcpapppacket.h rtcpbyepacket.h rtcpcompoundpacket.h @@ -44,7 +43,6 @@ set (qrtplib_HEADERS ) set(qrtplib_SOURCES - ../custom/apple/apple_compat.c rtcpapppacket.cpp rtcpbyepacket.cpp rtcpcompoundpacket.cpp diff --git a/qrtplib/rtptimeutilities.h b/qrtplib/rtptimeutilities.h index 526141777..8b460f659 100644 --- a/qrtplib/rtptimeutilities.h +++ b/qrtplib/rtptimeutilities.h @@ -60,6 +60,10 @@ #define CEPOCH 11644473600000000ULL #endif // RTP_HAVE_VSUINT64SUFFIX +#ifdef __APPLE__ +#include "../custom/apple/apple_compat.h" +#endif + namespace qrtplib {