Commit Graph

147 Commits

Author SHA1 Message Date
sunpenghao 503a0f184c ao_wasapi: add `--wasapi-exclusive-buffer` option
This allows users to set buffer duration in exclusive mode. We have
been using the default device period as the buffer size and it is
robust enough in most cases. However, on some devices there are
horrible glitches after a stream reset. Unfortunately, the issue is not
consistently reproducible, but using a smaller buffer size (e.g., the
minimum device period) seems to resolve the problem.

Fixes #13715.
2024-04-19 02:28:23 +02:00
sfan5 9565675488 various: use correct PATH_MAX for win32
In commit c09245cdf2
long-path support was enabled for mpv without actually
making sure that there was no code left that used the
old limit (260 Unicode chars) for buffer sizes.
This commit fixes all but one case.
2023-12-27 22:55:56 +01:00
Kacper Michajłow b323d2877a ao_wasapi: clean GUID definitions
Add ifndefs to define only when needed and remove some already defined
ones in mingw.
2023-12-03 22:24:13 +01:00
Kacper Michajłow a436af0f26 ao_wasapi: fix MP3 GUID
While CEA-861 defines MP2 as 0x5 and MP3 as 0x4, the GUIDs defined in
ksmedia.h are in reverse order.

See: https://github.com/MicrosoftDocs/windows-driver-docs/pull/3742
2023-12-03 22:24:13 +01:00
Kacper Michajłow ed107c4116 meson: adjust win32 defines
- Don't define _GNU_SOURCE on Windows, no need
- Define WIN32_LEAN_AND_MEAN to strip some unneded headers from
  windows.h
- Define NOMINMAX and _USE_MATH_DEFINES as they are common for Windows
  headers
2023-11-25 12:38:20 +01:00
Kacper Michajłow fd0e2af1f2 ao_wasapi: add missing comma in strings array 2023-11-18 23:55:28 +00:00
Dudemanguy de9b800879 timer: add convenience time unit conversion macros
There's a lot of wild 1e6, 1000, etc. lying around in the code. A macro
is much easier to read and understand at a glance. Add some helpers for
this. We don't need to convert everything now but there's some simple
things that can be done so they are included in this commit.
2023-10-16 15:38:59 +00:00
Dudemanguy 59dd7d94af timer: change mp_sleep_us to mp_sleep_ns
Linux and macOS already use nanosecond resolution for their sleep
functions. It was just being converted from microseconds before. Since
we have mp_time_ns now, go ahead and bump the precision here. The timer
for windows uses some timeBeginPeriod thing which I'm not sure what it
does really but whatever just convert the units to ms like they were
doing before. There's really no reason to keep the mp_sleep_us helper
around. A multiplication by 1000 is trivial and underlying OS clocks
have nanosecond precision.
2023-10-10 19:10:55 +00:00
Kacper Michajłow 4f0b654503 wasapi: clamp number of output channels to 8
This is the most supported in standard layout, if we request more it
tends to fallback to stereo instead. Also channels mask is 32-bit and it
can get truncated.
2023-09-29 02:35:10 +00:00
Kacper Michajłow db59a1c1a7 audio/chmap: link string buffer size to MP_NUM_CHANNELS 2023-09-29 02:35:10 +00:00
Kacper Michajłow 71c80826f1 ao_wasapi: use client name instead of hardcoded string 2023-06-21 17:31:59 +02:00
Kevin Mitchell 3aad89829f ao_wasapi: try mix format except for chmap
In shared mode, we previously tried to feed the full native format to
IsFormatSupported in the hopes that the "closest match" returned was
actually that.

Turns out, IsFormatSupported will always return the mix format if we
don't use the mix format's sample rate. This will also clobber our
choice of channel map with the mix format channel map even if our
desired channel map is supported due to surround emulation.

The solution is to not bother trying to use anything other than the mix
format sample rate. While we're at it, we might as well use the mix
format PCM sample format (always float32) since this conversion will
happen anyway and may avoid unecessary dithering to intermediate
integer formats if we are already resampling or channel mixing.
2020-03-19 20:39:44 +02:00
Kevin Mitchell 609d0ef478 ao_wasapi: handle S_FALSE in mp_format_res_str
IsFormatSupported may return S_FALSE (considered SUCCESS) if the
requested format is not suppported, but is close to one that is.
2020-03-19 20:39:44 +02:00
Rafael Rivera c40554295a ao_wasapi_utils: remove invalid audio session icon path (fixes #7269) 2020-01-31 23:08:47 +11:00
wm4 d36ff64b29 audio: fix annyoing af_get_best_sample_formats() definition
The af_get_best_sample_formats() function had an argument of
int[AF_FORMAT_COUNT], which is slightly incorrect, because it's 0
terminated and should in theory have AF_FORMAT_COUNT+1 entries. It won't
actually write this many formats (since some formats are fundamentally
incompatible), but it still feels annoying and incorrect. So fix it, and
require that callers pass an AF_FORMAT_COUNT+1 array.

Note that the array size has no meaning in C function arguments (just
another issue with C static arrays being weird and stupid), so get rid
of it completely.

Not changing the af_lavcac3enc use, since that is rewritten in another
branch anyway.
2018-01-25 20:18:32 -08:00
Kevin Mitchell 12cafdc868 ao_wasapi: remove old comment 2017-08-07 16:33:29 -07:00
Kevin Mitchell 6f40c211a5 ao_wasapi: reorganize wasapi.h
Remove dead declarations. Move macro only used in wasapi_utils.c closer to use.
Rearrange declaration order.
2017-08-07 14:33:03 -07:00
Kevin Mitchell 434d3d4976 ao_wasapi: deduplicate wasapi sample format selection 2017-08-07 14:33:03 -07:00
Kevin Mitchell 15eb1e1ad3 ao_wasapi: clean up find_formats logic
There were too many functions within functions, too much going on in if
clauses and duplicated code. Fix it.
2017-08-07 14:33:03 -07:00
Kevin Mitchell bee602da82 ao_wasapi: return bool instead of HRESULT from thread_init
Any bad HRESULTs should have been printed already and lots of failure modes
don't have an HRESULT leading to awkward hr = E_FAIL business.

This also checks the exit status of GetBufferSize in the align hack. A final
fatal message is added if either of the retry hacks fail.
2017-08-07 14:33:03 -07:00
Kevin Mitchell c5dfd66e14 ao_wasapi: remove redundant / outdated comment
Where this was moved from, it made slightly more sense. Here what the comment is
trying to say is already pretty obvious from the code.
2017-07-10 21:01:39 -07:00
Kevin Mitchell 63b6aa3f57 ao_waspi: use switch for handling fix_format errors 2017-07-10 21:01:39 -07:00
Kevin Mitchell 4389ddcc34 ao_wasapi: don't repeat format negotiation on align hack
Even if it did return a different result, the bufferFrameCount from the align
hack would be wrong anyway.
2017-07-10 21:01:39 -07:00
Kevin Mitchell 71cc28b804 ao_wasapi: fix leak on align hack 2017-07-10 21:01:39 -07:00
Kevin Mitchell 6666b25b73 ao_wasapi: enable packed 24 bit output 2017-07-09 13:46:13 -07:00
Kevin Mitchell 03abd704ec ao_wasapi: reorder channels and samplerates to speed up search
This shouldn't affect which are chosen, but it should speed up the search by
putting more common configurations earlier so that a working sample format and
sample rates can be found sooner obviating the need to search them for each
iteration of the outer loops.
2017-07-09 13:46:13 -07:00
Kevin Mitchell 7568715563 ao_wasapi: minor cosmetic fixes 2017-07-09 13:44:09 -07:00
Kevin Mitchell 2514e542e5 ao_wasapi: try correct initial format
The loop to select the native wasapi_format for the incoming audio was
not breaking correctly when it found the most desirable format. It
therefore executed completely leaving the least desirable format (u8) as
the choice.

fixes #4582
2017-07-09 13:43:54 -07:00
wm4 4e11549593 ao_wasapi_utils: be slightly more clever when converting channel map 2017-07-07 17:56:18 +02:00
wm4 951c1a4907 ao_wasapi: drop use of AF_FORMAT_S24
Do conversion directly, using the infrastructure that was added before.

This also rewrites part of format negotation, I guess.

I couldn't test the format that was used for S24 - my hardware does not
report support for it. So I commented it, as it could be buggy. Testing
this with the wasapi_formats[] entry for 24/24 uncommented would be
appreciated.
2017-07-07 17:56:18 +02:00
wm4 3e9075787f ao_wasapi: UWP wrapper hack support
UWP does not support the whole IMMDevice API. Instead, you need to use a
new API (available starting from Windows 8), which is in addition not in
MinGW, and extremely unpleasant to use.

The wasapiuwp2.dll wrapper is a small custom MSVC DLL, which does this
instead, and returns a normal IAudioClient.

Before this, ao_wasapi did not initialize on UWP.
2017-06-29 10:38:05 +02:00
Pedro Pombeiro 4637b029cd Universal Windows Plaform (UWP) support
libmpv only. Some things are still missing.

Heavily reworked.

Signed-off-by: wm4 <wm4@nowhere>
2017-06-29 10:36:16 +02:00
Pedro Pombeiro f22d12ac51 ao_wasapi: do not use deprecated wchar functions
These break on UWP. Based on a patch by Pedro Pombeiro.
2017-06-29 10:35:25 +02:00
wm4 3a3a0aced2 ao_wasapi: remove subtly duplicated code
Seems like this can be slightly simplified.
2017-06-28 18:43:19 +02:00
wm4 3b7e292844 ao_wasapi: remove duplicate code for creating IAudioClient
The code accounting for the terrible AUDCLNT_E_BUFFER_SIZE_NOT_ALIGNED
semantics (which MSDN claims can happen "starting with Windows 7" - so
probably on Windows 10 too) duplicated the call for creating the
IAudioClient. That's not great, so get rid of it.

Let wasapi_thread_init() handle this. It has a retry loop anyway. This
redoes device lookup and format negotiation, but potential failures due
to race conditions (what if the driver decides to change behavior)
shouldn't be worse than before.
2017-06-28 18:43:18 +02:00
wm4 c68be80a63 ao_wasapi: do not pass nonsense to drivers with double
This tried to use AF_FORMAT_DOUBLE as KSDATAFORMAT_SUBTYPE_IEEE_FLOAT,
with wBitsPerSample==64. This is probably not allowed, and drivers
appear to react inconsistently to it. (With one user, the format was
accepted during format negotiation, but then rejected on actual init.)

Remove it, which essentially forces it to fall back to some other
format. (Looks like it'll use af_select_best_samplerate(), which would
probably make it try S32 next.)

The af_fmt_from_planar() is so that we don't have to care about
AF_FORMAT_FLOATP. Wasapi always requires packed data anyway.

This should actually handle other potentially unknown sample formats
better.

This changes that set_waveformat() always set the exact format. Now it
might set a "close" format instead. But all callers seem to deal with
this well. Although in theory, callers should probably handle the
fallback. The next cleanup (if ever) can take care of this.
2017-03-29 15:19:25 +02:00
James Ross-Gowan 9692814502 win32: add COM-specific SAFE_RELEASE to windows_utils.h
See: https://msdn.microsoft.com/en-us/library/windows/desktop/dd743946.aspx

Microsoft example code often uses a SAFE_RELEASE macro like the one in
the above link. This makes it easier to avoid errors when releasing COM
interfaces. It also reduces noise in COM-heavy code.

ao_wasapi.h also had a macro called SAFE_RELEASE, though unlike the
version above, its SAFE_RELEASE macro accepted a second parameter which
allowed it to destroy arbitrary objects other than just COM interfaces.
This renames ao_wasapi's SAFE_RELEASE to SAFE_DESTROY, which should more
accurately reflect what it does and prevent confusion with the Microsoft
version.
2017-01-30 00:22:30 +11:00
wm4 ec74a79e12 ao_wasapi: log return code when probing audio formats
We log a large number of formats, but we rarely log the result of the
probing. Change this.

The logic in try_format_exclusive() changes slightly, but should be
equivalent. EXIT_ON_ERROR() checks for FAILED(), which should be
exclusive to SUCCEEDED().
2016-11-30 17:56:33 +01:00
pavelxdd 3203d6003c ao_wasapi_utils: remove unused variable
Introduced in 1a2319f3e4
Produced a warning during compilation on Windows.
2016-11-27 20:32:33 +01:00
wm4 1a2319f3e4 options: remove deprecated sub-option handling for --vo and --ao
Long planned. Leads to some sanity.

There still are some rather gross things. Especially g_groups is ugly,
and a hack that can hopefully be removed. (There is a plan for it, but
whether it's implemented depends on how much energy is left.)
2016-11-25 21:17:25 +01:00
James Ross-Gowan 3751065f97 win32: build with -DINITGUID
We always want to use __declspec(selectany) to declare GUIDs, but
manually including <initguid.h> in every file that used GUIDs was
error-prone. Since all <initguid.h> does is define INITGUID and include
<guiddef.h>, we can remove all references to <initguid.h> and just
compile with -DINITGUID to get the same effect.

Also, this partially reverts 622bcb0 by re-adding libuuid.a to the
build, since apparently some GUIDs (such as GUID_NULL) are not declared
in the source file, even when INITGUID is set.
2016-09-28 21:38:52 +10:00
wm4 b2e5eb13bc ao_wasapi: in exclusive mode do not output multichannel by default
Exactly the same situation as with ao_alsa in commit 0b144eac (except
that we can detect the situation better under wasapi).

Essentially, wasapi will allow us to output any sample format, and not
just the one configured by the user in the audio system settings.
2016-08-05 16:11:42 +02:00
Kevin Mitchell 96053d53a7 ao_wasapi: use new af_select_best_samplerate function
It duplicates the logic that was previously used here.
2016-03-17 02:31:05 -07:00
Kevin Mitchell 534571f794 ao_wasapi: use MP_FATAL for stuff that leads to init failure 2016-02-26 15:43:51 -08:00
Kevin Mitchell 82f102cfe3 ao_wasapi: set buffer size to device period in exclusive mode
This eliminates some intermittent pops heard in a HRT MicroStreamer DAC
uncorrelated with user interaction. As a bonus, this resolves #1773 which I can
o longer reproduce as of this commit. Leave the 50ms buffer for shared mode
since that seems to be working quite well.

This is also the way exclusive mode is done in the MSDN example code:
https://msdn.microsoft.com/en-us/library/windows/desktop/dd370844%28v=vs.85%29.aspx

This was originally increased in c545c40 to mitigate glitches that subsequent
refactorings have eliminated.
2016-02-26 15:43:51 -08:00
Kevin Mitchell 84a3c21beb ao_wasapi: replace laggy COM messaging with mp_dispatch_queue
A COM message loop is apparently totally inappropriate for a low latency
thread. It leads to audio glitches because the thread doesn't wake up fast
enough when it should. It also causes mysterious correlations between the vo
and ao thread (i.e., toggling fullscreen delays audio feed events). Instead use
an mp_dispatch_queue to set/get volume/mute/session display name from the audio
thread. This has the added benefit of obviating the need to marshal the
associated interfaces from the audio thread.
2016-02-26 15:43:51 -08:00
Kevin Mitchell a842ad8f50 ao_wasapi: use SUCCEEDED/FAILED macros 2016-02-26 15:43:51 -08:00
Jan Ekström ff0112e08d Initial Android support
* Adds an 'android' feature, which is automatically detected.
* Android has a broken strnlen, so a wrapper is added from FreeBSD.
2016-02-10 21:29:36 +01:00
Kevin Mitchell 4d5d25fdbb ao_wasapi: add "wasapi" prefix to non-static find_deviceID function 2016-01-28 00:56:03 -08:00
Kevin Mitchell e927ff1666 ao_wasapi: correct check for specified device on default change
Correctly avoid a reload if the current device was specified by the user through
--audio-device. Previously, we only recognized if the user had specified
--ao=wasapi:device=.
2016-01-28 00:55:58 -08:00