[Live-devel] Timestamp conversion in RTPSink.cpp

Ross Finlayson finlayson at live555.com
Tue Nov 6 07:00:20 PST 2012


> You can find alot occurences of this problem in WinCE world. The problem is the "GetSystemTime()" call, which should fill the SYSTEMTIME struct, but it does not, at least not the milliseconds field (which is always 0). The solution I used:

>  
> int gettimeofday(struct timeval* tp, int* /*tz*/) {
> #if defined(_WIN32_WCE)
>   /* FILETIME of Jan 1 1970 00:00:00. */
>   static const unsigned __int64 epoch = 116444736000000000LL;
>   static Boolean isFirstCall = True;
>   static LONGLONG unixStartTime = 0;
>   static DWORD firstTickCount=0;
>  
>   if (isFirstCall) {
>  
>     FILETIME fileTime;
>  
>     GetSystemTimeAsFileTime(&fileTime);
>  
>     LARGE_INTEGER date;
>     date.HighPart = fileTime.dwHighDateTime;
>     date.LowPart = fileTime.dwLowDateTime;
>  
>     unixStartTime= (date.QuadPart - epoch) / 10000000L;
>  
>     firstTickCount = GetTickCount();
>  
>     tp->tv_sec=(long)unixStartTime;
>     tp->tv_usec= 0L;
>  
>     isFirstCall = False; // for next time
>  
>   } else {
>     // add elapsed seconds
>     tp->tv_sec= (long)unixStartTime + (GetTickCount()-firstTickCount)/1000;
>     tp->tv_usec=(GetTickCount()-firstTickCount)%1000 * 1000;
> }
>  
> #else

Correction: On further thought, I don't want to make this change 'as is'.  The problem is that it's possible for the "gettimeofday()" function to be called concurrently from multiple threads.  (This is legal for LIVE555-based systems that use different "UsageEnvironment" and "TaskScheduler" objects for each thread.)  So, the implementation needs to be 'thread safe'.  If the "if" branch of the "if (isFirstCall)" statement gets executed concurrently by more than one thread, then "unixStartTime" and/or "firstTickCount" might get set to bad values.

So, please rewrite your implementation to ensure that the "if" branch of the code (i.e., the part of the code that sets static variables) is executed only once, even if the code is called by multiple threads.  (Because this code is for WinCE only, it's OK to use some WinCE-specific locking mechanism, if necessary.)

(However, I'll make the change to the timestamp conversion code in the next release of the software.)

Ross Finlayson
Live Networks, Inc.
http://www.live555.com/

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.live555.com/pipermail/live-devel/attachments/20121106/a19273c9/attachment.html>


More information about the live-devel mailing list