From 1bd0bdcdc236099d5c0d179696951f35f5310fa5 Mon Sep 17 00:00:00 2001 From: Martin Storsjö Date: Wed, 22 Oct 2014 12:14:37 +0300 Subject: lavu: Add av_gettime_relative MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Since av_gettime() is used in a number of places where actual real time clock is required, the monotonic clock introduced in ebef9f5a5 would have consequences that are hard to handle. Instead split it into a separate function that can be used in the cases where only relative time is desired. On platform where no monotonic clock is available, the difference between the two av_gettime functions is not clear, and one could mistakenly use the relative clock where an absolute one is required. Therefore add an offset, to make it evident that the time returned from av_gettime_relative never is actual current real time, even though it is based on av_gettime. Based on a patch by Olivier Langlois. Signed-off-by: Martin Storsjö --- libavutil/time.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) (limited to 'libavutil/time.c') diff --git a/libavutil/time.c b/libavutil/time.c index 42497dad3c..7a3604e651 100644 --- a/libavutil/time.c +++ b/libavutil/time.c @@ -23,7 +23,8 @@ #include #if HAVE_CLOCK_GETTIME #include -#elif HAVE_GETTIMEOFDAY +#endif +#if HAVE_GETTIMEOFDAY #include #endif #if HAVE_UNISTD_H @@ -38,11 +39,7 @@ int64_t av_gettime(void) { -#if HAVE_CLOCK_GETTIME - struct timespec ts; - clock_gettime(CLOCK_MONOTONIC, &ts); - return (int64_t)ts.tv_sec * 1000000 + ts.tv_nsec / 1000; -#elif HAVE_GETTIMEOFDAY +#if HAVE_GETTIMEOFDAY struct timeval tv; gettimeofday(&tv, NULL); return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec; @@ -57,6 +54,17 @@ int64_t av_gettime(void) #endif } +int64_t av_gettime_relative(void) +{ +#if HAVE_CLOCK_GETTIME + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + return (int64_t)ts.tv_sec * 1000000 + ts.tv_nsec / 1000; +#else + return av_gettime() + 42 * 60 * 60 * INT64_C(1000000); +#endif +} + int av_usleep(unsigned usec) { #if HAVE_NANOSLEEP -- cgit v1.2.3