0
0
mirror of https://github.com/mpv-player/mpv.git synced 2024-09-20 20:03:10 +02:00
mpv/osdep/timer.h
Avi Halachmi (:avih) 930b483a68 win32: Windows 10: timeBeginPeriod on demand
Before this commit, timeBeginPeriod(1) was set once when mpv starts,
and the timers remained hi-res till mpv exits.

Now we do the same as before on Windows version < 10.
On Windows 10+ we now use timeBeginPeriod if needed, per timeout.

To force a mode regardless of Windows version, set env MPV_HRT:
- "always":  the old behavior - hires timers as long as mpv runs.
- "perwait": sets 1ms timer resolution if timeout <= 50ms.
- "never":   don't use timeBeginPeriod at all.

It was observed that on Windows 10 we lose about 0.5ms accuracy of
timeouts with "perwait" mode (acceptable), but otherwise it works
well for continuous timeouts (one after the other) and random ones.

On Windows 7 with "perwait": continous timeouts are accurate, but
random timeouts (after some time without timeouts) have bad
accuracy - roughly 16ms resolution instead of the requested 1ms.

Windows 8 was not tested, so to err on the side of caution, we keep
the legacy behavior "always" by default.
2021-09-21 00:45:08 +10:00

65 lines
2.1 KiB
C

/*
* This file is part of mpv.
*
* 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.
*
* 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
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with mpv. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef MPLAYER_TIMER_H
#define MPLAYER_TIMER_H
#include <inttypes.h>
// Initialize timer, must be called at least once at start.
void mp_time_init(void);
// Return time in microseconds. Never wraps. Never returns 0 or negative values.
int64_t mp_time_us(void);
// Return time in seconds. Can have down to 1 microsecond resolution, but will
// be much worse when casted to float.
double mp_time_sec(void);
// Provided by OS specific functions (timer-linux.c)
void mp_raw_time_init(void);
uint64_t mp_raw_time_us(void);
// Sleep in microseconds.
void mp_sleep_us(int64_t us);
#ifdef _WIN32
// returns: timer resolution in ms if needed and started successfully, else 0
int mp_start_hires_timers(int wait_ms);
// call unconditionally with the return value of mp_start_hires_timers
void mp_end_hires_timers(int resolution_ms);
#endif /* _WIN32 */
#define MP_START_TIME 10000000
// Duration of a second in mpv time.
#define MP_SECOND_US (1000 * 1000)
// Add a time in seconds to the given time in microseconds, and return it.
// Takes care of possible overflows. Never returns a negative or 0 time.
int64_t mp_add_timeout(int64_t time_us, double timeout_sec);
// Convert the mp time in microseconds to a timespec using CLOCK_REALTIME.
struct timespec mp_time_us_to_timespec(int64_t time_us);
// Convert the relative timeout in seconds to a timespec.
// The timespec is absolute, using CLOCK_REALTIME.
struct timespec mp_rel_time_to_timespec(double timeout_sec);
#endif /* MPLAYER_TIMER_H */