jrtplib  3.6.0
rtptimeutilities.h
Go to the documentation of this file.
1 /*
2 
3  This file is a part of JRTPLIB
4  Copyright (c) 1999-2006 Jori Liesenborgs
5 
6  Contact: jori.liesenborgs@gmail.com
7 
8  This library was developed at the "Expertisecentrum Digitale Media"
9  (http://www.edm.uhasselt.be), a research center of the Hasselt University
10  (http://www.uhasselt.be). The library is based upon work done for
11  my thesis at the School for Knowledge Technology (Belgium/The Netherlands).
12 
13  Permission is hereby granted, free of charge, to any person obtaining a
14  copy of this software and associated documentation files (the "Software"),
15  to deal in the Software without restriction, including without limitation
16  the rights to use, copy, modify, merge, publish, distribute, sublicense,
17  and/or sell copies of the Software, and to permit persons to whom the
18  Software is furnished to do so, subject to the following conditions:
19 
20  The above copyright notice and this permission notice shall be included
21  in all copies or substantial portions of the Software.
22 
23  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
29  IN THE SOFTWARE.
30 
31 */
32 
37 #ifndef RTPTIMEUTILITIES_H
38 
39 #define RTPTIMEUTILITIES_H
40 
41 #include "rtpconfig.h"
42 #include "rtptypes.h"
43 #ifndef WIN32
44  #include <sys/time.h>
45  #include <time.h>
46 #else
47  #ifndef _WIN32_WCE
48  #include <sys/timeb.h>
49  #endif // _WIN32_WINCE
50 #endif // WIN32
51 
52 #define RTP_NTPTIMEOFFSET 2208988800UL
53 
59 {
60 public:
62  RTPNTPTime(uint32_t m,uint32_t l) { msw = m ; lsw = l; }
63 
65  uint32_t GetMSW() const { return msw; }
66 
68  uint32_t GetLSW() const { return lsw; }
69 private:
70  uint32_t msw,lsw;
71 };
72 
77 class RTPTime
78 {
79 public:
84  static RTPTime CurrentTime();
85 
87  static void Wait(const RTPTime &delay);
88 
90  RTPTime(double t);
91 
97  RTPTime(RTPNTPTime ntptime);
98 
100  RTPTime(uint32_t seconds,uint32_t microseconds) { sec = seconds; microsec = microseconds; }
101 
103  uint32_t GetSeconds() const { return sec; }
104 
106  uint32_t GetMicroSeconds() const { return microsec; }
107 
109  double GetDouble() const { return (((double)sec)+(((double)microsec)/1000000.0)); }
110 
112  RTPNTPTime GetNTPTime() const;
113 
114  RTPTime &operator-=(const RTPTime &t);
115  RTPTime &operator+=(const RTPTime &t);
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;
120 private:
121  uint32_t sec,microsec;
122 };
123 
124 inline RTPTime::RTPTime(double t)
125 {
126  sec = (uint32_t)t;
127 
128  double t2 = t-((double)sec);
129  t2 *= 1000000.0;
130  microsec = (uint32_t)t2;
131 }
132 
134 {
135  if (ntptime.GetMSW() < RTP_NTPTIMEOFFSET)
136  {
137  sec = 0;
138  microsec = 0;
139  }
140  else
141  {
142  sec = ntptime.GetMSW() - RTP_NTPTIMEOFFSET;
143 
144  double x = (double)ntptime.GetLSW();
145  x /= (65536.0*65536.0);
146  x *= 1000000.0;
147  microsec = (uint32_t)x;
148  }
149 }
150 
151 #if (defined(WIN32) || defined(_WIN32_WCE))
152 
154 {
155  static int inited = 0;
156  static unsigned __int64 microseconds, initmicroseconds;
157  static LARGE_INTEGER performancefrequency;
158 
159  unsigned __int64 emulate_microseconds, microdiff;
160  SYSTEMTIME systemtime;
161  FILETIME filetime;
162 
163  LARGE_INTEGER performancecount;
164 
165  QueryPerformanceCounter(&performancecount);
166 
167  if(!inited){
168  inited = 1;
169  QueryPerformanceFrequency(&performancefrequency);
170  GetSystemTime(&systemtime);
171  SystemTimeToFileTime(&systemtime,&filetime);
172  microseconds = ( ((unsigned __int64)(filetime.dwHighDateTime) << 32) + (unsigned __int64)(filetime.dwLowDateTime) ) / 10ui64;
173  microseconds-= 11644473600000000ui64; // EPOCH
174  initmicroseconds = ( ( performancecount.QuadPart * 1000000ui64 ) / performancefrequency.QuadPart );
175  }
176 
177  emulate_microseconds = ( ( performancecount.QuadPart * 1000000ui64 ) / performancefrequency.QuadPart );
178 
179  microdiff = emulate_microseconds - initmicroseconds;
180 
181  return RTPTime((uint32_t)((microseconds + microdiff) / 1000000ui64),((uint32_t)((microseconds + microdiff) % 1000000ui64)));
182 }
183 
184 inline void RTPTime::Wait(const RTPTime &delay)
185 {
186  DWORD t;
187 
188  t = ((DWORD)delay.GetSeconds())*1000+(((DWORD)delay.GetMicroSeconds())/1000);
189  Sleep(t);
190 }
191 
192 class RTPTimeInitializer
193 {
194 public:
195  RTPTimeInitializer();
196  void Dummy() { dummy++; }
197 private:
198  int dummy;
199 };
200 
201 extern RTPTimeInitializer timeinit;
202 
203 #else // unix style
204 
206 {
207  struct timeval tv;
208 
209  gettimeofday(&tv,0);
210  return RTPTime((uint32_t)tv.tv_sec,(uint32_t)tv.tv_usec);
211 }
212 
213 inline void RTPTime::Wait(const RTPTime &delay)
214 {
215  struct timespec req,rem;
216 
217  req.tv_sec = (time_t)delay.sec;
218  req.tv_nsec = ((long)delay.microsec)*1000;
219  nanosleep(&req,&rem);
220 }
221 
222 #endif // WIN32
223 
224 inline RTPTime &RTPTime::operator-=(const RTPTime &t)
225 {
226  sec -= t.sec;
227  if (t.microsec > microsec)
228  {
229  sec--;
230  microsec += 1000000;
231  }
232  microsec -= t.microsec;
233  return *this;
234 }
235 
236 inline RTPTime &RTPTime::operator+=(const RTPTime &t)
237 {
238  sec += t.sec;
239  microsec += t.microsec;
240  if (microsec >= 1000000)
241  {
242  sec++;
243  microsec -= 1000000;
244  }
245  return *this;
246 }
247 
249 {
250  uint32_t msw = sec+RTP_NTPTIMEOFFSET;
251  uint32_t lsw;
252  double x;
253 
254  x = microsec/1000000.0;
255  x *= (65536.0*65536.0);
256  lsw = (uint32_t)x;
257 
258  return RTPNTPTime(msw,lsw);
259 }
260 
261 inline bool RTPTime::operator<(const RTPTime &t) const
262 {
263  if (sec < t.sec)
264  return true;
265  if (sec > t.sec)
266  return false;
267  if (microsec < t.microsec)
268  return true;
269  return false;
270 }
271 
272 inline bool RTPTime::operator>(const RTPTime &t) const
273 {
274  if (sec > t.sec)
275  return true;
276  if (sec < t.sec)
277  return false;
278  if (microsec > t.microsec)
279  return true;
280  return false;
281 }
282 
283 inline bool RTPTime::operator<=(const RTPTime &t) const
284 {
285  if (sec < t.sec)
286  return true;
287  if (sec > t.sec)
288  return false;
289  if (microsec <= t.microsec)
290  return true;
291  return false;
292 }
293 
294 inline bool RTPTime::operator>=(const RTPTime &t) const
295 {
296  if (sec > t.sec)
297  return true;
298  if (sec < t.sec)
299  return false;
300  if (microsec >= t.microsec)
301  return true;
302  return false;
303 }
304 #endif // RTPTIMEUTILITIES_H
305 
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:248
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:124
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:213
static RTPTime CurrentTime()
Returns an RTPTime instance representing the current wallclock time.
Definition: rtptimeutilities.h:205
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