]> git.sesse.net Git - vlc/blobdiff - src/misc/mtime.c
* More precise way to retrieve a PTS from the bit stream.
[vlc] / src / misc / mtime.c
index 5b02400d9ff349554ccf1533088a1e336d49ccb2..140752c925626f7d4a30e893047f0403dfe8e2a6 100644 (file)
@@ -2,8 +2,8 @@
  * mtime.c: high rezolution time management functions
  * Functions are prototyped in mtime.h.
  *****************************************************************************
- * Copyright (C) 1998, 1999, 2000 VideoLAN
- * $Id: mtime.c,v 1.17 2001/04/28 03:36:25 sam Exp $
+ * Copyright (C) 1998-2001 VideoLAN
+ * $Id: mtime.c,v 1.27 2001/12/30 07:09:56 sam Exp $
  *
  * Authors: Vincent Seguin <seguin@via.ecp.fr>
  *
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
-#include "defs.h"
-
 #include <stdio.h>                                              /* sprintf() */
-#include <unistd.h>                                              /* select() */
-#include <sys/time.h>
 
-#ifdef HAVE_KERNEL_OS_H
-#include <kernel/OS.h>
+#include <videolan/vlc.h>
+
+#if defined( PTH_INIT_IN_PTH_H )                                  /* GNU Pth */
+#   include <pth.h>
 #endif
 
-#ifdef WIN32
-#include <windows.h>
+#ifdef HAVE_UNISTD_H
+#   include <unistd.h>                                           /* select() */
+#endif
+
+#ifdef HAVE_KERNEL_OS_H
+#   include <kernel/OS.h>
 #endif
 
-#include "config.h"
-#include "common.h"
-#include "mtime.h"
+#if defined( WIN32 )
+#   include <windows.h>
+#else
+#   include <sys/time.h>
+#endif
 
 /*****************************************************************************
  * mstrtime: return a date in a readable format
 char *mstrtime( char *psz_buffer, mtime_t date )
 {
     sprintf( psz_buffer, "%02d:%02d:%02d-%03d.%03d",
-             (int) (date / (1000LL * 1000LL * 60LL * 60LL) % 24LL),
-             (int) (date / (1000LL * 1000LL * 60LL) % 60LL),
-             (int) (date / (1000LL * 1000LL) % 60LL),
-             (int) (date / 1000LL % 1000LL),
-             (int) (date % 1000LL) );
+             (int) (date / (I64C(1000) * I64C(1000) * I64C(60) * I64C(60)) % I64C(24)),
+             (int) (date / (I64C(1000) * I64C(1000) * I64C(60)) % I64C(60)),
+             (int) (date / (I64C(1000) * I64C(1000)) % I64C(60)),
+             (int) (date / I64C(1000) % I64C(1000)),
+             (int) (date % I64C(1000)) );
     return( psz_buffer );
 }
 
@@ -81,22 +85,17 @@ mtime_t mdate( void )
     /* We don't get the real date, just the value of a high precision timer.
      * this is because the usual time functions have at best only a milisecond
      * resolution */
-    mtime_t freq,usec_time;
+    mtime_t freq, usec_time;
 
-    if( !QueryPerformanceFrequency((LARGE_INTEGER *)&freq) )
-    {
-        /* Milisecond resolution */
-        FILETIME file_time;
-        GetSystemTimeAsFileTime((FILETIME *)&file_time);
-        usec_time *= 1000;
-    }
-    else
+    if( QueryPerformanceFrequency( (LARGE_INTEGER *)&freq ) )
     {
         /* Microsecond resolution */
-        QueryPerformanceCounter((LARGE_INTEGER *)&usec_time);
-       usec_time /= (freq/1000000);
+        QueryPerformanceCounter( (LARGE_INTEGER *)&usec_time );
+        return ( usec_time * 1000000 ) / freq;
     }
-    return( usec_time );
+
+    /* Milisecond resolution */
+    return 1000 * GetTickCount();
 
 #else
     struct timeval tv_date;
@@ -138,8 +137,7 @@ void mwait( mtime_t date )
     {
         return;
     }
-    /* Sleep only has milisecond resolution */
-    Sleep( (DWORD)(delay/1000) );
+    msleep( delay );
 
 #else
 
@@ -154,7 +152,10 @@ void mwait( mtime_t date )
     gettimeofday( &tv_date, NULL );
 
     /* calculate delay and check if current date is before wished date */
-    delay = date - (mtime_t) tv_date.tv_sec * 1000000 - (mtime_t) tv_date.tv_usec - 10000;
+    delay = date - (mtime_t) tv_date.tv_sec * 1000000
+                 - (mtime_t) tv_date.tv_usec
+                 - 10000;
+
     /* Linux/i386 has a granularity of 10 ms. It's better to be in advance
      * than to be late. */
     if( delay <= 0 )                 /* wished date is now or already passed */
@@ -162,14 +163,18 @@ void mwait( mtime_t date )
         return;
     }
 
-#   ifdef HAVE_USLEEP
+#   if defined( PTH_INIT_IN_PTH_H )
+    pth_usleep( delay );
+
+#   elif defined( HAVE_USLEEP )
     usleep( delay );
+
 #   else
     tv_delay.tv_sec = delay / 1000000;
     tv_delay.tv_usec = delay % 1000000;
-
     /* see msleep() about select() errors */
     select( 0, NULL, NULL, NULL, &tv_delay );
+
 #   endif
 
 #endif
@@ -185,14 +190,18 @@ void msleep( mtime_t delay )
 #if defined( HAVE_KERNEL_OS_H )
     snooze( delay );
 
-#elif defined( WIN32 )
-    Sleep( delay/1000 );             /* Sleep only has milisecond resolution */
-  /* Maybe we could use the multimedia timer to reach the right resolution,  */
-  /* or the old Winsock select() function ?*/
+#elif defined( PTH_INIT_IN_PTH_H )
+    struct timeval tv_delay;
+    tv_delay.tv_sec = delay / 1000000;
+    tv_delay.tv_usec = delay % 1000000;
+    pth_select( 0, NULL, NULL, NULL, &tv_delay );
 
 #elif defined( HAVE_USLEEP )
     usleep( delay );
 
+#elif defined( WIN32 )
+    Sleep( (int) (delay / 1000) );
+
 #else
     struct timeval tv_delay;