2013-05-17 17:54:37 +00:00
|
|
|
/*
|
|
|
|
* This file is part of mpv.
|
|
|
|
*
|
Relicense some non-MPlayer source files to LGPL 2.1 or later
This covers source files which were added in mplayer2 and mpv times
only, and where all code is covered by LGPL relicensing agreements.
There are probably more files to which this applies, but I'm being
conservative here.
A file named ao_sdl.c exists in MPlayer too, but the mpv one is a
complete rewrite, and was added some time after the original ao_sdl.c
was removed. The same applies to vo_sdl.c, for which the SDL2 API is
radically different in addition (MPlayer supports SDL 1.2 only).
common.c contains only code written by me. But common.h is a strange
case: although it originally was named mp_common.h and exists in MPlayer
too, by now it contains only definitions written by uau and me. The
exceptions are the CONTROL_ defines - thus not changing the license of
common.h yet.
codec_tags.c contained once large tables generated from MPlayer's
codecs.conf, but all of these tables were removed.
From demux_playlist.c I'm removing a code fragment from someone who was
not asked; this probably could be done later (see commit 15dccc37).
misc.c is a bit complicated to reason about (it was split off mplayer.c
and thus contains random functions out of this file), but actually all
functions have been added post-MPlayer. Except get_relative_time(),
which was written by uau, but looks similar to 3 different versions of
something similar in each of the Unix/win32/OSX timer source files. I'm
not sure what that means in regards to copyright, so I've just moved it
into another still-GPL source file for now.
screenshot.c once had some minor parts of MPlayer's vf_screenshot.c, but
they're all gone.
2016-01-19 17:36:06 +00:00
|
|
|
* mpv is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2013-05-17 17:54:37 +00:00
|
|
|
*
|
|
|
|
* mpv is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Relicense some non-MPlayer source files to LGPL 2.1 or later
This covers source files which were added in mplayer2 and mpv times
only, and where all code is covered by LGPL relicensing agreements.
There are probably more files to which this applies, but I'm being
conservative here.
A file named ao_sdl.c exists in MPlayer too, but the mpv one is a
complete rewrite, and was added some time after the original ao_sdl.c
was removed. The same applies to vo_sdl.c, for which the SDL2 API is
radically different in addition (MPlayer supports SDL 1.2 only).
common.c contains only code written by me. But common.h is a strange
case: although it originally was named mp_common.h and exists in MPlayer
too, by now it contains only definitions written by uau and me. The
exceptions are the CONTROL_ defines - thus not changing the license of
common.h yet.
codec_tags.c contained once large tables generated from MPlayer's
codecs.conf, but all of these tables were removed.
From demux_playlist.c I'm removing a code fragment from someone who was
not asked; this probably could be done later (see commit 15dccc37).
misc.c is a bit complicated to reason about (it was split off mplayer.c
and thus contains random functions out of this file), but actually all
functions have been added post-MPlayer. Except get_relative_time(),
which was written by uau, but looks similar to 3 different versions of
something similar in each of the Unix/win32/OSX timer source files. I'm
not sure what that means in regards to copyright, so I've just moved it
into another still-GPL source file for now.
screenshot.c once had some minor parts of MPlayer's vf_screenshot.c, but
they're all gone.
2016-01-19 17:36:06 +00:00
|
|
|
* GNU Lesser General Public License for more details.
|
2013-05-17 17:54:37 +00:00
|
|
|
*
|
Relicense some non-MPlayer source files to LGPL 2.1 or later
This covers source files which were added in mplayer2 and mpv times
only, and where all code is covered by LGPL relicensing agreements.
There are probably more files to which this applies, but I'm being
conservative here.
A file named ao_sdl.c exists in MPlayer too, but the mpv one is a
complete rewrite, and was added some time after the original ao_sdl.c
was removed. The same applies to vo_sdl.c, for which the SDL2 API is
radically different in addition (MPlayer supports SDL 1.2 only).
common.c contains only code written by me. But common.h is a strange
case: although it originally was named mp_common.h and exists in MPlayer
too, by now it contains only definitions written by uau and me. The
exceptions are the CONTROL_ defines - thus not changing the license of
common.h yet.
codec_tags.c contained once large tables generated from MPlayer's
codecs.conf, but all of these tables were removed.
From demux_playlist.c I'm removing a code fragment from someone who was
not asked; this probably could be done later (see commit 15dccc37).
misc.c is a bit complicated to reason about (it was split off mplayer.c
and thus contains random functions out of this file), but actually all
functions have been added post-MPlayer. Except get_relative_time(),
which was written by uau, but looks similar to 3 different versions of
something similar in each of the Unix/win32/OSX timer source files. I'm
not sure what that means in regards to copyright, so I've just moved it
into another still-GPL source file for now.
screenshot.c once had some minor parts of MPlayer's vf_screenshot.c, but
they're all gone.
2016-01-19 17:36:06 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with mpv. If not, see <http://www.gnu.org/licenses/>.
|
2013-05-17 17:54:37 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2014-02-06 20:04:24 +00:00
|
|
|
#include <pthread.h>
|
2014-05-18 14:36:08 +00:00
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <assert.h>
|
2013-05-17 17:54:37 +00:00
|
|
|
|
2014-05-18 14:36:08 +00:00
|
|
|
#include "common/common.h"
|
2014-02-28 22:18:52 +00:00
|
|
|
#include "common/msg.h"
|
2014-05-18 14:36:08 +00:00
|
|
|
#include "timer.h"
|
2013-05-17 17:54:37 +00:00
|
|
|
|
|
|
|
static uint64_t raw_time_offset;
|
2015-05-01 19:17:56 +00:00
|
|
|
static pthread_once_t timer_init_once = PTHREAD_ONCE_INIT;
|
2013-05-17 17:54:37 +00:00
|
|
|
|
2014-02-06 20:04:24 +00:00
|
|
|
static void do_timer_init(void)
|
2013-05-17 17:54:37 +00:00
|
|
|
{
|
|
|
|
mp_raw_time_init();
|
|
|
|
srand(mp_raw_time_us());
|
|
|
|
raw_time_offset = mp_raw_time_us();
|
|
|
|
// Arbitrary additional offset to avoid confusing relative/absolute times.
|
|
|
|
// Also,we rule that the timer never returns 0 (so default-initialized
|
|
|
|
// time values will be always in the past).
|
2015-01-26 10:31:02 +00:00
|
|
|
raw_time_offset -= MP_START_TIME;
|
2013-05-17 17:54:37 +00:00
|
|
|
}
|
|
|
|
|
2014-02-06 20:04:24 +00:00
|
|
|
void mp_time_init(void)
|
|
|
|
{
|
|
|
|
pthread_once(&timer_init_once, do_timer_init);
|
|
|
|
}
|
|
|
|
|
2013-05-17 17:54:37 +00:00
|
|
|
int64_t mp_time_us(void)
|
|
|
|
{
|
2015-07-04 15:24:10 +00:00
|
|
|
int64_t r = mp_raw_time_us() - raw_time_offset;
|
|
|
|
if (r < MP_START_TIME)
|
|
|
|
r = MP_START_TIME;
|
|
|
|
return r;
|
2013-05-17 17:54:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
double mp_time_sec(void)
|
|
|
|
{
|
|
|
|
return mp_time_us() / (double)(1000 * 1000);
|
|
|
|
}
|
|
|
|
|
2014-05-18 14:36:08 +00:00
|
|
|
int64_t mp_add_timeout(int64_t time_us, double timeout_sec)
|
|
|
|
{
|
|
|
|
assert(time_us > 0); // mp_time_us() returns strictly positive values
|
2014-05-22 20:32:57 +00:00
|
|
|
double t = MPCLAMP(timeout_sec * (1000 * 1000), -0x1p63, 0x1p63);
|
2014-05-22 20:50:39 +00:00
|
|
|
int64_t ti = t == 0x1p63 ? INT64_MAX : (int64_t)t;
|
2014-05-22 20:32:57 +00:00
|
|
|
if (ti > INT64_MAX - time_us)
|
2014-05-18 14:36:08 +00:00
|
|
|
return INT64_MAX;
|
2014-05-22 20:32:57 +00:00
|
|
|
if (ti <= -time_us)
|
2014-05-18 14:36:08 +00:00
|
|
|
return 1;
|
2014-05-22 20:32:57 +00:00
|
|
|
return time_us + ti;
|
2014-05-18 14:36:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void get_realtime(struct timespec *out_ts)
|
|
|
|
{
|
|
|
|
#if defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0
|
|
|
|
clock_gettime(CLOCK_REALTIME, out_ts);
|
|
|
|
#else
|
|
|
|
// OSX
|
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
out_ts->tv_sec = tv.tv_sec;
|
|
|
|
out_ts->tv_nsec = tv.tv_usec * 1000UL;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
struct timespec mp_time_us_to_timespec(int64_t time_us)
|
|
|
|
{
|
|
|
|
struct timespec ts;
|
|
|
|
get_realtime(&ts);
|
|
|
|
// We don't know what time source mp_time_us() uses, but usually it's not
|
|
|
|
// CLOCK_REALTIME - so we have to remap the times.
|
|
|
|
int64_t unow = mp_time_us();
|
|
|
|
int64_t diff_us = time_us - unow;
|
2014-05-22 18:55:36 +00:00
|
|
|
int64_t diff_secs = diff_us / (1000L * 1000L);
|
2014-05-18 19:22:09 +00:00
|
|
|
long diff_nsecs = (diff_us - diff_secs * (1000L * 1000L)) * 1000L;
|
|
|
|
if (diff_nsecs < 0) {
|
|
|
|
diff_secs -= 1;
|
|
|
|
diff_nsecs += 1000000000L;
|
|
|
|
}
|
2014-05-18 14:36:08 +00:00
|
|
|
if (diff_nsecs + ts.tv_nsec >= 1000000000UL) {
|
|
|
|
diff_secs += 1;
|
|
|
|
diff_nsecs -= 1000000000UL;
|
|
|
|
}
|
2014-05-24 12:02:45 +00:00
|
|
|
// OSX can't deal with large timeouts. Also handles tv_sec/time_t overflows.
|
2014-05-22 22:20:57 +00:00
|
|
|
diff_secs = MPMIN(diff_secs, 10000000);
|
2014-05-18 14:36:08 +00:00
|
|
|
ts.tv_sec += diff_secs;
|
|
|
|
ts.tv_nsec += diff_nsecs;
|
|
|
|
return ts;
|
|
|
|
}
|
|
|
|
|
2015-05-11 21:44:36 +00:00
|
|
|
struct timespec mp_rel_time_to_timespec(double timeout_sec)
|
|
|
|
{
|
|
|
|
return mp_time_us_to_timespec(mp_add_timeout(mp_time_us(), timeout_sec));
|
|
|
|
}
|
|
|
|
|
2013-05-17 17:54:37 +00:00
|
|
|
#if 0
|
|
|
|
#include <stdio.h>
|
2014-05-18 14:36:08 +00:00
|
|
|
#include "threads.h"
|
|
|
|
|
|
|
|
#define TEST_SLEEP 1
|
2013-05-17 17:54:37 +00:00
|
|
|
|
|
|
|
int main(void) {
|
2014-05-18 14:36:08 +00:00
|
|
|
int c = 2000000;
|
2013-05-17 17:54:37 +00:00
|
|
|
int64_t j, r, t = 0;
|
2014-05-18 14:36:08 +00:00
|
|
|
pthread_mutex_t mtx;
|
|
|
|
pthread_mutex_init(&mtx, NULL);
|
|
|
|
pthread_cond_t cnd;
|
|
|
|
pthread_cond_init(&cnd, NULL);
|
2013-05-17 17:54:37 +00:00
|
|
|
|
|
|
|
mp_time_init();
|
|
|
|
|
|
|
|
for (int i = 0; i < c; i++) {
|
|
|
|
const int delay = rand() / (RAND_MAX / 1e5);
|
|
|
|
r = mp_time_us();
|
2014-05-18 14:36:08 +00:00
|
|
|
#if TEST_SLEEP
|
2013-05-17 17:54:37 +00:00
|
|
|
mp_sleep_us(delay);
|
2014-05-18 14:36:08 +00:00
|
|
|
#else
|
2015-05-11 21:44:36 +00:00
|
|
|
struct timespec ts = mp_time_us_to_timespec(r + delay);
|
|
|
|
pthread_cond_timedwait(&cnd, &mtx, &ts);
|
2014-05-18 14:36:08 +00:00
|
|
|
#endif
|
2013-05-17 17:54:37 +00:00
|
|
|
j = (mp_time_us() - r) - delay;
|
2014-05-18 14:36:08 +00:00
|
|
|
printf("sleep time: t=%"PRId64" sleep=%8i err=%5i\n", r, delay, (int)j);
|
2013-05-17 17:54:37 +00:00
|
|
|
t += j;
|
|
|
|
}
|
|
|
|
fprintf(stderr, "average error:\t%i\n", (int)(t / c));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|