[vlc-devel] commit: mtime: Use mach_absolute_time on Mac OS X instead of gettimeofday. (Pierre d'Herbemont )

git version control git at videolan.org
Sun Feb 8 01:03:05 CET 2009


vlc | branch: 0.9-bugfix | Pierre d'Herbemont <pdherbemont at videolan.org> | Sun Feb  8 00:31:33 2009 +0100| [5ead92ffd58088828ed802173d963b2571f0cd69] | committer: Pierre d'Herbemont 

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.

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

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

diff --git a/include/vlc_threads.h b/include/vlc_threads.h
index e2810e9..8a21f84 100644
--- a/include/vlc_threads.h
+++ b/include/vlc_threads.h
@@ -359,12 +359,26 @@ static inline void __vlc_cond_wait( const char * psz_file, int i_line,
 #define vlc_cond_timedwait( P_COND, P_MUTEX, DEADLINE )                      \
     __vlc_cond_timedwait( __FILE__, __LINE__, P_COND, P_MUTEX, DEADLINE  )
 
+#ifdef __APPLE__
+# include <sys/time.h> /* gettimeofday in vlc_cond_timedwait */
+#endif
+
 static inline int __vlc_cond_timedwait( const char * psz_file, int i_line,
                                         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, 1000000 );
     struct timespec ts = { d.quot, d.rem * 1000 };
 
diff --git a/src/misc/mtime.c b/src/misc/mtime.c
index 6c13eac..ed65f03 100644
--- a/src/misc/mtime.c
+++ b/src/misc/mtime.c
@@ -60,6 +60,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
 {
@@ -172,6 +177,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
  *
@@ -197,6 +211,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);
@@ -340,6 +364,11 @@ void mwait( mtime_t date )
         ts.tv_sec = d.quot; ts.tv_nsec = d.rem * 1000;
         while( clock_nanosleep( CLOCK_REALTIME, 0, &ts, NULL ) == 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 = date * 1000 * mtime_timebase_info.denom / mtime_timebase_info.numer;
+    mach_wait_until(mach_time);
+
 #else
 
     mtime_t delay = date - mdate();
@@ -385,6 +414,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;
 




More information about the vlc-devel mailing list