[vlc-devel] commit: Forwardport 5ead92ffd58088828ed802173d963b2571f0cd69 (Pierre d' Herbemont )

git version control git at videolan.org
Fri Feb 20 15:49:27 CET 2009


vlc | branch: master | Pierre d'Herbemont <pdherbemont at videolan.org> | Sun Feb  8 00:31:33 2009 +0100| [5a005c66194a61f1e24e1ea0a9547f996f541011] | committer: Derk-Jan Hartman 

Forwardport 5ead92ffd58088828ed802173d963b2571f0cd69
mtime: Use mach_absolute_time on Mac OS X instead of gettimeofday.

mach_absolute_time() is a monotic clock, whereas gettimeofday() is not. For that reason setting your computer time while playing a movie won't halt playback anymore.

Seems to fix the dropped frame issue on Mac OS X.

Signed-off-by: Derk-Jan Hartman <hartman at videolan.org>

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=5a005c66194a61f1e24e1ea0a9547f996f541011
---

 src/misc/mtime.c   |   34 ++++++++++++++++++++++++++++++++++
 src/misc/threads.c |   14 ++++++++++++++
 2 files changed, 48 insertions(+), 0 deletions(-)

diff --git a/src/misc/mtime.c b/src/misc/mtime.c
index c3d9abd..0dbb4df 100644
--- a/src/misc/mtime.c
+++ b/src/misc/mtime.c
@@ -56,6 +56,11 @@
 #   include <sys/time.h>
 #endif
 
+#ifdef __APPLE__
+#   include <mach/mach.h>
+#   include <mach/mach_time.h>
+#endif
+
 #if !defined(HAVE_STRUCT_TIMESPEC)
 struct timespec
 {
@@ -168,6 +173,15 @@ static inline unsigned mprec( void )
 #endif
 }
 
+#ifdef __APPLE__
+static mach_timebase_info_data_t mtime_timebase_info;
+static pthread_once_t mtime_timebase_info_once = PTHREAD_ONCE_INIT;
+static void mtime_init_timebase(void)
+{
+    mach_timebase_info(&mtime_timebase_info);
+}
+#endif
+
 /**
  * Return high precision date
  *
@@ -193,6 +207,16 @@ mtime_t mdate( void )
 #elif defined( HAVE_KERNEL_OS_H )
     res = real_time_clock_usecs();
 
+#elif defined( __APPLE__ )
+    pthread_once(&mtime_timebase_info_once, mtime_init_timebase);
+    uint64_t date = mach_absolute_time();
+
+    /* Convert to nanoseconds */
+    date *= mtime_timebase_info.numer;
+    date /= mtime_timebase_info.denom;
+
+    /* Convert to microseconds */
+    res = date / 1000;
 #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 = INT64_C(-1);
@@ -299,6 +323,11 @@ mtime_t mdate( void )
         i_previous_time = res;
         LeaveCriticalSection( &date_lock );
     }
+#elif defined( __APPLE__ ) /* The version that should be used, if it was cancelable */
+    pthread_once(&mtime_timebase_info_once, mtime_init_timebase);
+    uint64_t mach_time = date * 1000 * mtime_timebase_info.denom / mtime_timebase_info.numer;
+    mach_wait_until(mach_time);
+
 #else
     struct timeval tv_date;
 
@@ -395,6 +424,11 @@ void msleep( mtime_t delay )
 
     while( nanosleep( &ts_delay, &ts_delay ) && ( errno == EINTR ) );
 
+#elif defined( __APPLE__ ) /* The version that should be used, if it was cancelable */
+    pthread_once(&mtime_timebase_info_once, mtime_init_timebase);
+    uint64_t mach_time = delay * 1000 * mtime_timebase_info.denom / mtime_timebase_info.numer;
+    mach_wait_until(mach_time + mach_absolute_time());
+
 #else
     struct timeval tv_delay;
 
diff --git a/src/misc/threads.c b/src/misc/threads.c
index f5b0d7e..8875d15 100644
--- a/src/misc/threads.c
+++ b/src/misc/threads.c
@@ -52,6 +52,10 @@ static vlc_threadvar_t cancel_key;
 # include <execinfo.h>
 #endif
 
+#ifdef __APPLE__
+# include <sys/time.h> /* gettimeofday in vlc_cond_timedwait */
+#endif
+
 /**
  * Print a backtrace to the standard error for debugging purpose.
  */
@@ -556,6 +560,16 @@ int vlc_cond_timedwait (vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex,
                         mtime_t deadline)
 {
 #if defined(LIBVLC_USE_PTHREAD)
+#ifdef __APPLE__
+    /* mdate() is mac_absolute_time on osx, which we must convert to do
+     * the same base than gettimeofday() on which pthread_cond_timedwait
+     * counts on. */
+    mtime_t oldbase = mdate();
+    struct timeval tv;
+    gettimeofday(&tv, NULL);
+    mtime_t newbase = (mtime_t)tv.tv_sec * 1000000 + (mtime_t) tv.tv_usec;
+    deadline = deadline - oldbase + newbase;
+#endif
     lldiv_t d = lldiv( deadline, CLOCK_FREQ );
     struct timespec ts = { d.quot, d.rem * (1000000000 / CLOCK_FREQ) };
 




More information about the vlc-devel mailing list