]> git.sesse.net Git - vlc/blobdiff - src/misc/mtime.c
NTPtime64() returns an NTP timestamp
[vlc] / src / misc / mtime.c
index cc3911548ea714d22f0c2662b475e9fe977fd522..1c543325e2456413332d0eb45c975ae4873b1fff 100644 (file)
@@ -1,8 +1,9 @@
 /*****************************************************************************
  * mtime.c: high resolution time management functions
- * Functions are prototyped in mtime.h.
+ * Functions are prototyped in vlc_mtime.h.
  *****************************************************************************
  * Copyright (C) 1998-2004 the VideoLAN team
+ * Copyright © 2006-2007 Rémi Denis-Courmont
  * $Id$
  *
  * Authors: Vincent Seguin <seguin@via.ecp.fr>
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
+
+#include <vlc/vlc.h>
+
 #include <stdio.h>                                              /* sprintf() */
 #include <time.h>                      /* clock_gettime(), clock_nanosleep() */
-#include <stdlib.h>                                                /* ldiv() */
+#include <stdlib.h>                                               /* lldiv() */
+#include <assert.h>
 
-#include <vlc/vlc.h>
 
 #if defined( PTH_INIT_IN_PTH_H )                                  /* GNU Pth */
 #   include <pth.h>
@@ -113,13 +117,25 @@ char *secstotimestr( char *psz_buffer, int i_seconds )
  */
 mtime_t mdate( void )
 {
-#if defined( HAVE_KERNEL_OS_H )
+#if defined (HAVE_CLOCK_NANOSLEEP)
+    struct timespec ts;
+
+# if (_POSIX_MONOTONIC_CLOCK - 0 >= 0)
+    /* Try to use POSIX monotonic clock if available */
+    if( clock_gettime( CLOCK_MONOTONIC, &ts ) )
+# endif
+        /* Run-time fallback to real-time clock (always available) */
+        (void)clock_gettime( CLOCK_REALTIME, &ts );
+
+    return ((mtime_t)ts.tv_sec * (mtime_t)1000000)
+           + (mtime_t)(ts.tv_nsec / 1000);
+
+#elif defined( HAVE_KERNEL_OS_H )
     return( real_time_clock_usecs() );
 
 #elif defined( WIN32 ) || defined( UNDER_CE )
     /* We don't need the real date, just the value of a high precision timer */
     static mtime_t freq = I64C(-1);
-    mtime_t usec_time;
 
     if( freq == I64C(-1) )
     {
@@ -141,17 +157,24 @@ mtime_t mdate( void )
          * the RTC rather than the TSC.  If it's anything else, we
          * presume that the performance counter is unreliable.
          */
+        LARGE_INTEGER buf;
 
-        freq = ( QueryPerformanceFrequency( (LARGE_INTEGER *)&freq ) &&
-                 (freq == I64C(1193182) || freq == I64C(3579545) ) )
-               ? freq : 0;
+        freq = ( QueryPerformanceFrequency( &buf ) &&
+                 (buf.QuadPart == I64C(1193182) || buf.QuadPart == I64C(3579545) ) )
+               ? buf.QuadPart : 0;
     }
 
     if( freq != 0 )
     {
-        /* Microsecond resolution */
-        QueryPerformanceCounter( (LARGE_INTEGER *)&usec_time );
-        return ( usec_time * 1000000 ) / freq;
+        LARGE_INTEGER counter;
+        QueryPerformanceCounter (&counter);
+
+        /* Convert to from (1/freq) to microsecond resolution */
+        /* We need to split the division to avoid 63-bits overflow */
+        lldiv_t d = lldiv (counter.QuadPart, freq);
+
+        return (d.quot * 1000000)
+             + ((d.rem * 1000000) / freq);
     }
     else
     {
@@ -163,6 +186,7 @@ mtime_t mdate( void )
         static CRITICAL_SECTION date_lock;
         static mtime_t i_previous_time = I64C(-1);
         static int i_wrap_counts = -1;
+        mtime_t usec_time;
 
         if( i_wrap_counts == -1 )
         {
@@ -179,25 +203,13 @@ mtime_t mdate( void )
         {
             /* Counter wrapped */
             i_wrap_counts++;
-            usec_time += I64C(0x100000000000);
+            usec_time += I64C(0x100000000) * 1000;
         }
         i_previous_time = usec_time;
         LeaveCriticalSection( &date_lock );
 
         return usec_time;
     }
-
-#elif defined (HAVE_CLOCK_GETTIME)
-    struct timespec ts;
-
-# if (_POSIX_MONOTONIC_CLOCK >= 0)
-    /* Try to use POSIX monotonic clock if available */
-    if( clock_gettime( CLOCK_MONOTONIC, &ts ) )
-# endif
-        /* Run-time fallback to real-time clock (always available) */
-        (void)clock_gettime( CLOCK_REALTIME, &ts );
-
-    return (ts.tv_sec * 1000000) + (ts.tv_nsec / 1000);
 #else
     struct timeval tv_date;
 
@@ -217,95 +229,19 @@ mtime_t mdate( void )
  */
 void mwait( mtime_t date )
 {
-#if defined( HAVE_KERNEL_OS_H )
-    mtime_t delay;
-
-    delay = date - real_time_clock_usecs();
-    if( delay <= 0 )
-    {
-        return;
-    }
-    snooze( delay );
-
-#elif defined( WIN32 ) || defined( UNDER_CE )
-    mtime_t usec_time, delay;
-
-    usec_time = mdate();
-    delay = date - usec_time;
-    if( delay <= 0 )
-    {
-        return;
-    }
-    msleep( delay );
-
-#elif defined (HAVE_CLOCK_GETTIME)
-    struct timespec ts;
-    ldiv_t d;
-
-# if 1
-    /*
-     * Ideally, we'd use absolute time (TIMER_ABSTIME), instead of
-     * computing the time difference... but VLC mtime_t type seems to
-     * overflow way too quickly for this to work properly, or maybe it's a
-     * signedness problem (??).
-     */
-    date -= mdate();
-    if( date <= 0 )
-        return;
-# endif
-    d = ldiv( date, 1000000 );
-    ts.tv_sec = d.quot;
-    ts.tv_nsec = d.rem * 1000;
+#if defined (HAVE_CLOCK_NANOSLEEP)
+    lldiv_t d = lldiv( date, 1000000 );
+    struct timespec ts = { d.quot, d.rem * 1000 };
 
-# if (_POSIX_MONOTONIC_CLOCK >= 0)
-    if( clock_nanosleep( CLOCK_MONOTONIC, 0 /*TIMER_ABSTIME*/, &ts, NULL ) )
+# if (_POSIX_MONOTONIC_CLOCK - 0 >= 0)
+    if( clock_nanosleep( CLOCK_MONOTONIC, TIMER_ABSTIME, &ts, NULL ) )
 # endif
-        (void)clock_nanosleep( CLOCK_REALTIME, 0, &ts, NULL );
+        clock_nanosleep( CLOCK_REALTIME, TIMER_ABSTIME, &ts, NULL );
 #else
 
-    struct timeval tv_date;
-    mtime_t        delay;          /* delay in msec, signed to detect errors */
-
-    /* see mdate() about gettimeofday() possible errors */
-    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;
-
-    /* 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 */
-    {
-        return;
-    }
-
-#   if defined( PTH_INIT_IN_PTH_H )
-    pth_usleep( delay );
-
-#   elif defined( ST_INIT_IN_ST_H )
-    st_usleep( delay );
-
-#   else
-
-#       if defined( HAVE_NANOSLEEP )
-    {
-        struct timespec ts_delay;
-        ts_delay.tv_sec = delay / 1000000;
-        ts_delay.tv_nsec = (delay % 1000000) * 1000;
-
-        nanosleep( &ts_delay, NULL );
-    }
-
-#       else
-    tv_date.tv_sec = delay / 1000000;
-    tv_date.tv_usec = delay % 1000000;
-    /* see msleep() about select() errors */
-    select( 0, NULL, NULL, NULL, &tv_date );
-#       endif
-
-#   endif
+    mtime_t delay = date - mdate();
+    if( delay > 0 )
+        msleep( delay );
 
 #endif
 }
@@ -318,7 +254,16 @@ void mwait( mtime_t date )
  */
 void msleep( mtime_t delay )
 {
-#if defined( HAVE_KERNEL_OS_H )
+#if defined( HAVE_CLOCK_NANOSLEEP ) 
+    lldiv_t d = lldiv( delay, 1000000 );
+    struct timespec ts = { d.quot, d.rem * 1000 };
+
+# if (_POSIX_MONOTONIC_CLOCK - 0 >= 0)
+    if( clock_nanosleep( CLOCK_MONOTONIC, 0, &ts, NULL ) )
+# endif
+        clock_nanosleep( CLOCK_REALTIME, 0, &ts, NULL );
+
+#elif defined( HAVE_KERNEL_OS_H )
     snooze( delay );
 
 #elif defined( PTH_INIT_IN_PTH_H )
@@ -349,7 +294,6 @@ void msleep( mtime_t delay )
      * (i.e. when a signal is sent to the thread, or when memory is full), and
      * can be ignored. */
     select( 0, NULL, NULL, NULL, &tv_delay );
-
 #endif
 }
 
@@ -444,3 +388,34 @@ mtime_t date_Increment( date_t *p_date, uint32_t i_nb_samples )
 
     return p_date->date;
 }
+
+/**
+ * @return NTP 64-bits timestamp in host byte order.
+ */
+uint64_t NTPtime64 (void)
+{
+    struct timespec ts;
+#if defined (CLOCK_REALTIME)
+    clock_gettime (CLOCK_REALTIME, &ts);
+#else
+    {
+        struct timeval tv;
+        gettimeofday (&tv, NULL);
+        ts.tv_sec = tv.tv_sec;
+        ts.tv_nsec = tv.tv_usec * 1000;
+    }
+#endif
+
+    /* Convert nanoseconds to 32-bits fraction (232 picosecond units) */
+    uint64_t t = (uint64_t)(ts.tv_nsec) << 32;
+    t /= 1000000000;
+
+
+    /* There is 70 years (incl. 17 leap ones) offset to the Unix Epoch.
+     * No leap seconds during that period since they were not invented yet.
+     */
+    assert (t < 0x100000000);
+    t |= ((70LL * 365 + 17) * 24 * 60 * 60 + ts.tv_sec) << 32;
+    return t;
+}
+