37 #ifndef RTPTIMEUTILITIES_H
39 #define RTPTIMEUTILITIES_H
41 #include "rtpconfig.h"
48 #include <sys/timeb.h>
49 #endif // _WIN32_WINCE
52 #define RTP_NTPTIMEOFFSET 2208988800UL
65 uint32_t
GetMSW()
const {
return msw; }
68 uint32_t
GetLSW()
const {
return lsw; }
100 RTPTime(uint32_t seconds,uint32_t microseconds) { sec = seconds; microsec = microseconds; }
109 double GetDouble()
const {
return (((
double)sec)+(((
double)microsec)/1000000.0)); }
116 bool operator<(
const RTPTime &t)
const;
117 bool operator>(
const RTPTime &t)
const;
118 bool operator<=(
const RTPTime &t)
const;
119 bool operator>=(
const RTPTime &t)
const;
121 #if (defined(WIN32) || defined(_WIN32_WCE))
122 static inline unsigned __int64 CalculateMicroseconds(
unsigned __int64 performancecount,
unsigned __int64 performancefrequency);
123 #endif // WIN32 || _WIN32_WCE
125 uint32_t sec,microsec;
132 double t2 = t-((double)sec);
134 microsec = (uint32_t)t2;
139 if (ntptime.
GetMSW() < RTP_NTPTIMEOFFSET)
146 sec = ntptime.
GetMSW() - RTP_NTPTIMEOFFSET;
148 double x = (double)ntptime.
GetLSW();
149 x /= (65536.0*65536.0);
151 microsec = (uint32_t)x;
155 #if (defined(WIN32) || defined(_WIN32_WCE))
157 inline unsigned __int64 RTPTime::CalculateMicroseconds(
unsigned __int64 performancecount,
unsigned __int64 performancefrequency)
159 unsigned __int64 f = performancefrequency;
160 unsigned __int64 a = performancecount;
161 unsigned __int64 b = a/f;
162 unsigned __int64 c = a%f;
164 return b*1000000ui64+(c*1000000ui64)/f;
169 static int inited = 0;
170 static unsigned __int64 microseconds, initmicroseconds;
171 static LARGE_INTEGER performancefrequency;
173 unsigned __int64 emulate_microseconds, microdiff;
174 SYSTEMTIME systemtime;
177 LARGE_INTEGER performancecount;
179 QueryPerformanceCounter(&performancecount);
183 QueryPerformanceFrequency(&performancefrequency);
184 GetSystemTime(&systemtime);
185 SystemTimeToFileTime(&systemtime,&filetime);
186 microseconds = ( ((
unsigned __int64)(filetime.dwHighDateTime) << 32) + (
unsigned __int64)(filetime.dwLowDateTime) ) / 10ui64;
187 microseconds-= 11644473600000000ui64;
188 initmicroseconds = CalculateMicroseconds(performancecount.QuadPart, performancefrequency.QuadPart);
191 emulate_microseconds = CalculateMicroseconds(performancecount.QuadPart, performancefrequency.QuadPart);
193 microdiff = emulate_microseconds - initmicroseconds;
195 return RTPTime((uint32_t)((microseconds + microdiff) / 1000000ui64),((uint32_t)((microseconds + microdiff) % 1000000ui64)));
206 class RTPTimeInitializer
209 RTPTimeInitializer();
210 void Dummy() { dummy++; }
215 extern RTPTimeInitializer timeinit;
224 return RTPTime((uint32_t)tv.tv_sec,(uint32_t)tv.tv_usec);
229 struct timespec req,rem;
231 req.tv_sec = (time_t)delay.sec;
232 req.tv_nsec = ((
long)delay.microsec)*1000;
233 nanosleep(&req,&rem);
241 if (t.microsec > microsec)
246 microsec -= t.microsec;
253 microsec += t.microsec;
254 if (microsec >= 1000000)
264 uint32_t msw = sec+RTP_NTPTIMEOFFSET;
268 x = microsec/1000000.0;
269 x *= (65536.0*65536.0);
275 inline bool RTPTime::operator<(
const RTPTime &t)
const
281 if (microsec < t.microsec)
286 inline bool RTPTime::operator>(
const RTPTime &t)
const
292 if (microsec > t.microsec)
297 inline bool RTPTime::operator<=(
const RTPTime &t)
const
303 if (microsec <= t.microsec)
308 inline bool RTPTime::operator>=(
const RTPTime &t)
const
314 if (microsec >= t.microsec)
318 #endif // RTPTIMEUTILITIES_H
uint32_t GetLSW() const
Returns the least significant word.
Definition: rtptimeutilities.h:68
RTPNTPTime(uint32_t m, uint32_t l)
This constructor creates and instance with MSW m and LSW l.
Definition: rtptimeutilities.h:62
This class is used to specify wallclock time, delay intervals etc.
Definition: rtptimeutilities.h:77
RTPTime(uint32_t seconds, uint32_t microseconds)
Creates an instance corresponding to seconds and microseconds.
Definition: rtptimeutilities.h:100
RTPNTPTime GetNTPTime() const
Returns the NTP time corresponding to the time stored in this instance.
Definition: rtptimeutilities.h:262
uint32_t GetMSW() const
Returns the most significant word.
Definition: rtptimeutilities.h:65
double GetDouble() const
Returns the time stored in this instance, expressed in units of seconds.
Definition: rtptimeutilities.h:109
RTPTime(double t)
Creates an RTPTime instance representing t, which is expressed in units of seconds.
Definition: rtptimeutilities.h:128
uint32_t GetMicroSeconds() const
Returns the number of microseconds stored in this instance.
Definition: rtptimeutilities.h:106
static void Wait(const RTPTime &delay)
This function waits the amount of time specified in delay.
Definition: rtptimeutilities.h:227
static RTPTime CurrentTime()
Returns an RTPTime instance representing the current wallclock time.
Definition: rtptimeutilities.h:219
This is a simple wrapper for the most significant word (MSW) and least significant word (LSW) of an N...
Definition: rtptimeutilities.h:58
uint32_t GetSeconds() const
Returns the number of seconds stored in this instance.
Definition: rtptimeutilities.h:103