Skip to content

Commit

Permalink
win32: thread: bring back the winmm clock source
Browse files Browse the repository at this point in the history
It is the default on 3.0 and might be useful when QueryPerformanceCounter() is
not accurate or too slow to call.

* https://docs.microsoft.com/en-US/troubleshoot/windows-server/performance/programs-queryperformancecounter-function-perform-poorly
* https://www.virtualdub.org/blog2/entry_106.html
* https://chromium.googlesource.com/chromium/src/base/+/refs/heads/main/time/time_win.cc

Partially reverts fdbbae7. Now we link
statically with winmm. It should be on all Win7+ machines.

We also don't force a period value smaller than wPeriodMin.
  • Loading branch information
robUx4 committed Aug 19, 2022
1 parent 0dee192 commit 5b4a021
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ if HAVE_WINSTORE
libvlccore_la_SOURCES += posix/timer.c win32/dirs-uap.c
else
libvlccore_la_SOURCES += win32/timer.c win32/dirs.c
libvlccore_la_LIBADD += -lwinmm
endif
libvlccore_la_LIBADD += -lbcrypt
endif
Expand Down
6 changes: 6 additions & 0 deletions src/libvlc-module.c
Original file line number Diff line number Diff line change
Expand Up @@ -1140,10 +1140,16 @@ static const char* const ppsz_restore_playback_desc[] = {
#ifdef _WIN32
static const char *const clock_sources[] = {
"", "perf",
#if WINAPI_FAMILY_PARTITION (WINAPI_PARTITION_DESKTOP)
"multimedia",
#endif
};

static const char *const clock_sources_text[] = {
N_("Auto"), "Performance counters",
#if WINAPI_FAMILY_PARTITION (WINAPI_PARTITION_DESKTOP)
"Multimedia timers",
#endif
};
#endif

Expand Down
32 changes: 32 additions & 0 deletions src/win32/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
#include <time.h>
#include <vlc_atomic.h>

#if WINAPI_FAMILY_PARTITION (WINAPI_PARTITION_DESKTOP)
#include <mmsystem.h>
#endif

#ifndef NTDDI_WIN10_RS3
#define NTDDI_WIN10_RS3 0x0A000004
#endif
Expand Down Expand Up @@ -559,6 +563,17 @@ static vlc_tick_t mdate_wall (void)
return VLC_TICK_FROM_MSFTIME(s.QuadPart);
}

#if WINAPI_FAMILY_PARTITION (WINAPI_PARTITION_DESKTOP)
static vlc_tick_t mdate_multimedia(void)
{
DWORD ts = timeGetTime ();

/* milliseconds */
static_assert ((CLOCK_FREQ % 1000) == 0, "Broken frequencies ratio");
return VLC_TICK_FROM_MS(ts);
}
#endif

static vlc_tick_t (*mdate_selected) (void) = mdate_wall;

vlc_tick_t vlc_tick_now (void)
Expand Down Expand Up @@ -621,11 +636,28 @@ void (vlc_tick_sleep)(vlc_tick_t delay)
static void SelectClockSource(libvlc_int_t *obj)
{
// speed comparison / granularity / counts during sleep
// multimedia:102000 / 5 ms / no
// perf: 155612 / ~100 ns / yes
// wall: 181593 / 100 ns / yes

char *str = var_InheritString(obj, "clock-source");
const char *name = str != NULL ? str : "perf";
#if WINAPI_FAMILY_PARTITION (WINAPI_PARTITION_DESKTOP)
if (!strcmp (name, "multimedia"))
{
TIMECAPS caps;

msg_Dbg (obj, "using multimedia timers as clock source");
if (timeGetDevCaps (&caps, sizeof (caps)) != MMSYSERR_NOERROR)
abort();
msg_Dbg (obj, " min period: %u ms, max period: %u ms",
caps.wPeriodMin, caps.wPeriodMax);
mdate_selected = mdate_multimedia;

timeBeginPeriod(__MAX(5, caps.wPeriodMin));
}
else
#endif
if (!strcmp (name, "perf"))
{
msg_Dbg (obj, "using performance counters as clock source");
Expand Down

0 comments on commit 5b4a021

Please sign in to comment.