37 #ifndef RTPTIMEUTILITIES_H
39 #define RTPTIMEUTILITIES_H
41 #include "rtpconfig.h"
43 #ifndef RTP_HAVE_QUERYPERFORMANCECOUNTER
49 #define RTP_NTPTIMEOFFSET 2208988800UL
51 #ifdef RTP_HAVE_VSUINT64SUFFIX
52 #define C1000000 1000000ui64
53 #define CEPOCH 11644473600000000ui64
55 #define C1000000 1000000ULL
56 #define CEPOCH 11644473600000000ULL
73 uint32_t
GetMSW()
const {
return msw; }
76 uint32_t
GetLSW()
const {
return lsw; }
95 static void Wait(
const RTPTime &delay);
108 RTPTime(int64_t seconds, uint32_t microseconds);
111 int64_t GetSeconds()
const;
114 uint32_t GetMicroSeconds()
const;
124 bool operator<(
const RTPTime &t)
const;
125 bool operator>(
const RTPTime &t)
const;
126 bool operator<=(
const RTPTime &t)
const;
127 bool operator>=(
const RTPTime &t)
const;
129 bool IsZero()
const {
return m_t == 0; }
131 #ifdef RTP_HAVE_QUERYPERFORMANCECOUNTER
132 static inline uint64_t CalculateMicroseconds(uint64_t performancecount,uint64_t performancefrequency);
147 m_t = (double)seconds + 1e-6*(
double)microseconds;
151 int64_t possec = -seconds;
153 m_t = (double)possec + 1e-6*(
double)microseconds;
160 if (ntptime.
GetMSW() < RTP_NTPTIMEOFFSET)
166 uint32_t sec = ntptime.
GetMSW() - RTP_NTPTIMEOFFSET;
168 double x = (double)ntptime.
GetLSW();
169 x /= (65536.0*65536.0);
171 uint32_t microsec = (uint32_t)x;
173 m_t = (double)sec + 1e-6*(
double)microsec;
188 int64_t sec = (int64_t)m_t;
189 microsec = (uint32_t)(1e6*(m_t - (
double)sec) + 0.5);
193 int64_t sec = (int64_t)(-m_t);
194 microsec = (uint32_t)(1e6*((-m_t) - (double)sec) + 0.5);
197 if (microsec >= 1000000)
205 #ifdef RTP_HAVE_QUERYPERFORMANCECOUNTER
207 inline uint64_t RTPTime::CalculateMicroseconds(uint64_t performancecount,uint64_t performancefrequency)
209 uint64_t f = performancefrequency;
210 uint64_t a = performancecount;
214 return b*C1000000+(c*C1000000)/f;
219 static int inited = 0;
220 static uint64_t microseconds, initmicroseconds;
221 static LARGE_INTEGER performancefrequency;
223 uint64_t emulate_microseconds, microdiff;
224 SYSTEMTIME systemtime;
227 LARGE_INTEGER performancecount;
229 QueryPerformanceCounter(&performancecount);
233 QueryPerformanceFrequency(&performancefrequency);
234 GetSystemTime(&systemtime);
235 SystemTimeToFileTime(&systemtime,&filetime);
236 microseconds = ( ((uint64_t)(filetime.dwHighDateTime) << 32) + (uint64_t)(filetime.dwLowDateTime) ) / (uint64_t)10;
237 microseconds-= CEPOCH;
238 initmicroseconds = CalculateMicroseconds(performancecount.QuadPart, performancefrequency.QuadPart);
241 emulate_microseconds = CalculateMicroseconds(performancecount.QuadPart, performancefrequency.QuadPart);
243 microdiff = emulate_microseconds - initmicroseconds;
245 double t = 1e-6*(double)(microseconds + microdiff);
254 uint64_t sec = (uint64_t)delay.m_t;
255 uint32_t microsec = (uint32_t)(1e6*(delay.m_t-(double)sec));
256 DWORD t = ((DWORD)sec)*1000+(((DWORD)microsec)/1000);
262 #ifdef RTP_HAVE_CLOCK_GETTIME
263 inline double RTPTime_timespecToDouble(
struct timespec &ts)
265 return (
double)ts.tv_sec + 1e-9*(double)ts.tv_nsec;
270 static bool s_initialized =
false;
271 static double s_startOffet = 0;
275 s_initialized =
true;
278 struct timespec tpSys, tpMono;
280 clock_gettime(CLOCK_REALTIME, &tpSys);
281 clock_gettime(CLOCK_MONOTONIC, &tpMono);
283 double tSys = RTPTime_timespecToDouble(tpSys);
284 double tMono = RTPTime_timespecToDouble(tpMono);
286 s_startOffet = tSys - tMono;
290 struct timespec tpMono;
291 clock_gettime(CLOCK_MONOTONIC, &tpMono);
293 double tMono0 = RTPTime_timespecToDouble(tpMono);
294 return tMono0 + s_startOffet;
304 return RTPTime((uint64_t)tv.tv_sec,(uint32_t)tv.tv_usec);
313 uint64_t sec = (uint64_t)delay.m_t;
314 uint64_t nanosec = (uint32_t)(1e9*(delay.m_t-(double)sec));
316 struct timespec req,rem;
319 req.tv_sec = (time_t)sec;
320 req.tv_nsec = ((long)nanosec);
323 ret = nanosleep(&req,&rem);
325 }
while (ret == -1 && errno == EINTR);
336 inline RTPTime &RTPTime::operator+=(
const RTPTime &t)
344 uint32_t sec = (uint32_t)m_t;
345 uint32_t microsec = (uint32_t)((m_t - (
double)sec)*1e6);
347 uint32_t msw = sec+RTP_NTPTIMEOFFSET;
351 x = microsec/1000000.0;
352 x *= (65536.0*65536.0);
358 inline bool RTPTime::operator<(
const RTPTime &t)
const
363 inline bool RTPTime::operator>(
const RTPTime &t)
const
368 inline bool RTPTime::operator<=(
const RTPTime &t)
const
373 inline bool RTPTime::operator>=(
const RTPTime &t)
const
378 class JRTPLIB_IMPORTEXPORT RTPTimeInitializerObject
381 RTPTimeInitializerObject();
382 void Dummy() { dummy++; }
387 extern RTPTimeInitializerObject timeinit;
This is a simple wrapper for the most significant word (MSW) and least significant word (LSW) of an N...
Definition: rtptimeutilities.h:67
RTPNTPTime(uint32_t m, uint32_t l)
This constructor creates and instance with MSW m and LSW l.
Definition: rtptimeutilities.h:70
uint32_t GetMSW() const
Returns the most significant word.
Definition: rtptimeutilities.h:73
uint32_t GetLSW() const
Returns the least significant word.
Definition: rtptimeutilities.h:76
This class is used to specify wallclock time, delay intervals etc.
Definition: rtptimeutilities.h:86
int64_t GetSeconds() const
Returns the number of seconds stored in this instance.
Definition: rtptimeutilities.h:177
static void Wait(const RTPTime &delay)
This function waits the amount of time specified in delay.
Definition: rtptimeutilities.h:308
uint32_t GetMicroSeconds() const
Returns the number of microseconds stored in this instance.
Definition: rtptimeutilities.h:182
static RTPTime CurrentTime()
Returns an RTPTime instance representing the current wallclock time.
Definition: rtptimeutilities.h:299
double GetDouble() const
Returns the time stored in this instance, expressed in units of seconds.
Definition: rtptimeutilities.h:117
RTPNTPTime GetNTPTime() const
Returns the NTP time corresponding to the time stored in this instance.
Definition: rtptimeutilities.h:342
RTPTime(double t)
Creates an RTPTime instance representing t, which is expressed in units of seconds.
Definition: rtptimeutilities.h:138