Commit 5ead92ff authored by Pierre d'Herbemont's avatar 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.
parent 627eb87b
...@@ -359,12 +359,26 @@ static inline void __vlc_cond_wait( const char * psz_file, int i_line, ...@@ -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 ) \ #define vlc_cond_timedwait( P_COND, P_MUTEX, DEADLINE ) \
__vlc_cond_timedwait( __FILE__, __LINE__, 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, static inline int __vlc_cond_timedwait( const char * psz_file, int i_line,
vlc_cond_t *p_condvar, vlc_cond_t *p_condvar,
vlc_mutex_t *p_mutex, vlc_mutex_t *p_mutex,
mtime_t deadline ) mtime_t deadline )
{ {
#if defined(LIBVLC_USE_PTHREAD) #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 ); lldiv_t d = lldiv( deadline, 1000000 );
struct timespec ts = { d.quot, d.rem * 1000 }; struct timespec ts = { d.quot, d.rem * 1000 };
......
...@@ -60,6 +60,11 @@ ...@@ -60,6 +60,11 @@
# include <sys/time.h> # include <sys/time.h>
#endif #endif
#ifdef __APPLE__
# include <mach/mach.h>
# include <mach/mach_time.h>
#endif
#if !defined(HAVE_STRUCT_TIMESPEC) #if !defined(HAVE_STRUCT_TIMESPEC)
struct timespec struct timespec
{ {
...@@ -172,6 +177,15 @@ static inline unsigned mprec( void ) ...@@ -172,6 +177,15 @@ static inline unsigned mprec( void )
#endif #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 * Return high precision date
* *
...@@ -197,6 +211,16 @@ mtime_t mdate( void ) ...@@ -197,6 +211,16 @@ mtime_t mdate( void )
#elif defined( HAVE_KERNEL_OS_H ) #elif defined( HAVE_KERNEL_OS_H )
res = real_time_clock_usecs(); 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 ) #elif defined( WIN32 ) || defined( UNDER_CE )
/* We don't need the real date, just the value of a high precision timer */ /* We don't need the real date, just the value of a high precision timer */
static mtime_t freq = INT64_C(-1); static mtime_t freq = INT64_C(-1);
...@@ -340,6 +364,11 @@ void mwait( mtime_t date ) ...@@ -340,6 +364,11 @@ void mwait( mtime_t date )
ts.tv_sec = d.quot; ts.tv_nsec = d.rem * 1000; ts.tv_sec = d.quot; ts.tv_nsec = d.rem * 1000;
while( clock_nanosleep( CLOCK_REALTIME, 0, &ts, NULL ) == EINTR ); 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 #else
mtime_t delay = date - mdate(); mtime_t delay = date - mdate();
...@@ -385,6 +414,11 @@ void msleep( mtime_t delay ) ...@@ -385,6 +414,11 @@ void msleep( mtime_t delay )
while( nanosleep( &ts_delay, &ts_delay ) && ( errno == EINTR ) ); 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 #else
struct timeval tv_delay; struct timeval tv_delay;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment