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
68 uint32_t
GetMSW()
const {
return msw; }
71 uint32_t
GetLSW()
const {
return lsw; }
103 RTPTime(uint32_t seconds, uint32_t microseconds) { sec = seconds; microsec = microseconds; }
112 double GetDouble()
const {
return (((
double)sec)+(((
double)microsec)/1000000.0)); }
119 bool operator<(
const RTPTime &t)
const;
120 bool operator>(
const RTPTime &t)
const;
121 bool operator<=(
const RTPTime &t)
const;
122 bool operator>=(
const RTPTime &t)
const;
124 #if (defined(WIN32) || defined(_WIN32_WCE))
125 static inline unsigned __int64 CalculateMicroseconds(
unsigned __int64 performancecount,
unsigned __int64 performancefrequency);
126 #endif // WIN32 || _WIN32_WCE
128 uint32_t sec,microsec;
135 double t2 = t-((double)sec);
137 microsec = (uint32_t)t2;
142 if (ntptime.
GetMSW() < RTP_NTPTIMEOFFSET)
149 sec = ntptime.
GetMSW() - RTP_NTPTIMEOFFSET;
151 double x = (double)ntptime.
GetLSW();
152 x /= (65536.0*65536.0);
154 microsec = (uint32_t)x;
158 #if (defined(WIN32) || defined(_WIN32_WCE))
160 inline unsigned __int64 RTPTime::CalculateMicroseconds(
unsigned __int64 performancecount,
unsigned __int64 performancefrequency)
162 unsigned __int64 f = performancefrequency;
163 unsigned __int64 a = performancecount;
164 unsigned __int64 b = a/f;
165 unsigned __int64 c = a%f;
167 return b*1000000ui64+(c*1000000ui64)/f;
172 static int inited = 0;
173 static unsigned __int64 microseconds, initmicroseconds;
174 static LARGE_INTEGER performancefrequency;
176 unsigned __int64 emulate_microseconds, microdiff;
177 SYSTEMTIME systemtime;
180 LARGE_INTEGER performancecount;
182 QueryPerformanceCounter(&performancecount);
186 QueryPerformanceFrequency(&performancefrequency);
187 GetSystemTime(&systemtime);
188 SystemTimeToFileTime(&systemtime,&filetime);
189 microseconds = ( ((
unsigned __int64)(filetime.dwHighDateTime) << 32) + (
unsigned __int64)(filetime.dwLowDateTime) ) / 10ui64;
190 microseconds-= 11644473600000000ui64;
191 initmicroseconds = CalculateMicroseconds(performancecount.QuadPart, performancefrequency.QuadPart);
194 emulate_microseconds = CalculateMicroseconds(performancecount.QuadPart, performancefrequency.QuadPart);
196 microdiff = emulate_microseconds - initmicroseconds;
198 return RTPTime((uint32_t)((microseconds + microdiff) / 1000000ui64),((uint32_t)((microseconds + microdiff) % 1000000ui64)));
205 t = ((DWORD)delay.GetSeconds())*1000+(((DWORD)delay.GetMicroSeconds())/1000);
209 class RTPTimeInitializer
212 RTPTimeInitializer();
213 void Dummy() { dummy++; }
218 extern RTPTimeInitializer timeinit;
227 return RTPTime((uint32_t)tv.tv_sec,(uint32_t)tv.tv_usec);
232 struct timespec req,rem;
234 req.tv_sec = (time_t)delay.sec;
235 req.tv_nsec = ((
long)delay.microsec)*1000;
236 nanosleep(&req,&rem);
244 if (t.microsec > microsec)
249 microsec -= t.microsec;
253 inline RTPTime &RTPTime::operator+=(
const RTPTime &t)
256 microsec += t.microsec;
257 if (microsec >= 1000000)
267 uint32_t msw = sec+RTP_NTPTIMEOFFSET;
271 x = microsec/1000000.0;
272 x *= (65536.0*65536.0);
278 inline bool RTPTime::operator<(
const RTPTime &t)
const
284 if (microsec < t.microsec)
289 inline bool RTPTime::operator>(
const RTPTime &t)
const
295 if (microsec > t.microsec)
300 inline bool RTPTime::operator<=(
const RTPTime &t)
const
306 if (microsec <= t.microsec)
311 inline bool RTPTime::operator>=(
const RTPTime &t)
const
317 if (microsec >= t.microsec)
324 #endif // RTPTIMEUTILITIES_H
uint32_t GetMicroSeconds() const
Returns the number of microseconds stored in this instance.
Definition: rtptimeutilities.h:109
RTPNTPTime GetNTPTime() const
Returns the NTP time corresponding to the time stored in this instance.
Definition: rtptimeutilities.h:265
This is a simple wrapper for the most significant word (MSW) and least significant word (LSW) of an N...
Definition: rtptimeutilities.h:61
RTPNTPTime(uint32_t m, uint32_t l)
This constructor creates and instance with MSW m and LSW l.
Definition: rtptimeutilities.h:65
RTPTime(uint32_t seconds, uint32_t microseconds)
Creates an instance corresponding to seconds and microseconds.
Definition: rtptimeutilities.h:103
double GetDouble() const
Returns the time stored in this instance, expressed in units of seconds.
Definition: rtptimeutilities.h:112
RTPTime(double t)
Creates an RTPTime instance representing t, which is expressed in units of seconds.
Definition: rtptimeutilities.h:131
This class is used to specify wallclock time, delay intervals etc.
Definition: rtptimeutilities.h:80
uint32_t GetLSW() const
Returns the least significant word.
Definition: rtptimeutilities.h:71
uint32_t GetMSW() const
Returns the most significant word.
Definition: rtptimeutilities.h:68
uint32_t GetSeconds() const
Returns the number of seconds 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:230
static RTPTime CurrentTime()
Returns an RTPTime instance representing the current wallclock time.
Definition: rtptimeutilities.h:222