mirror of
https://github.com/mpv-player/mpv
synced 2024-12-29 10:32:15 +00:00
6863eefc3d
Currently, running AO control wakes up the WASAPI renderer thread in the `WASAPI_THREAD_FEED` state, where `thread_feed` will be called. However, it seems that in recent Windows versions (tested on Windows 10 build 19044.3930 and Windows 11 build 22631.3007) we can't know if it is safe to feed more audio data in event-driven exclusive mode: - `IAudioClient_GetCurrentPadding` always returns `bufferFrameCount`, even if *NO* data has ever been written. This means we don't know how much free space we have that is available for writing. This is not the case in shared mode, where the return value correctly reflects the size of data waiting to be processed. As a sidenote, MS did not document the precise definition of the return value for an event-driven, exclusive stream [1]. - `IAudioRenderClient_GetBuffer` never fails. We can call it for 10 times in a roll, each time requesting an entire buffer (the unit at which data is exchanged in exclusive mode using event-driven buffering; there are 2 such buffers) and get a successful return code everytime. In shared mode, we get `AUDCLNT_E_BUFFER_TOO_LARGE` if we request a buffer larger than that currently available. As a result, `thread_feed` will always write `bufferFrameCount` frames of audio in exclusive mode. There will therefore be glitches each time `thread_control` is called due to the subsequent `thread_feed` overwriting frames yet to be processed. Also, an irreversible error is accumulated to `sample_count` as long as there is no AO reset, leading to eventual, unbounded A/V desync. As a fix to the issue, add a dedicated state for dispatch queue processing so that `thread_feed` is only called when signaled by the OS. The buffer checks in `thread_feed` that use `GetCurrentPadding` in exclusive mode are kept in case there are older versions where the two APIs behave differently. Closes #12615. [1] https://learn.microsoft.com/en-us/windows/win32/api/audioclient/nf-audioclient-iaudioclient-getcurrentpadding
118 lines
3.9 KiB
C
118 lines
3.9 KiB
C
/*
|
|
* This file is part of mpv.
|
|
*
|
|
* Original author: Jonathan Yong <10walls@gmail.com>
|
|
*
|
|
* 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 MP_AO_WASAPI_H_
|
|
#define MP_AO_WASAPI_H_
|
|
|
|
#include <stdatomic.h>
|
|
#include <stdlib.h>
|
|
#include <stdbool.h>
|
|
|
|
#include <windows.h>
|
|
#include <mmdeviceapi.h>
|
|
#include <audioclient.h>
|
|
#include <audiopolicy.h>
|
|
#include <endpointvolume.h>
|
|
|
|
#include "common/msg.h"
|
|
#include "osdep/windows_utils.h"
|
|
#include "internal.h"
|
|
#include "ao.h"
|
|
|
|
typedef struct change_notify {
|
|
IMMNotificationClient client; // this must be first in the structure!
|
|
IMMDeviceEnumerator *pEnumerator; // object where client is registered
|
|
LPWSTR monitored; // Monitored device
|
|
bool is_hotplug;
|
|
struct ao *ao;
|
|
} change_notify;
|
|
|
|
HRESULT wasapi_change_init(struct ao* ao, bool is_hotplug);
|
|
void wasapi_change_uninit(struct ao* ao);
|
|
|
|
enum wasapi_thread_state {
|
|
WASAPI_THREAD_FEED = 0,
|
|
WASAPI_THREAD_DISPATCH,
|
|
WASAPI_THREAD_RESUME,
|
|
WASAPI_THREAD_RESET,
|
|
WASAPI_THREAD_SHUTDOWN
|
|
};
|
|
|
|
typedef struct wasapi_state {
|
|
struct mp_log *log;
|
|
|
|
bool init_ok; // status of init phase
|
|
// Thread handles
|
|
HANDLE hInitDone; // set when init is complete in audio thread
|
|
HANDLE hAudioThread; // the audio thread itself
|
|
HANDLE hWake; // thread wakeup event
|
|
atomic_int thread_state; // enum wasapi_thread_state (what to do on wakeup)
|
|
struct mp_dispatch_queue *dispatch; // for volume/mute/session display
|
|
|
|
// for setting the audio thread priority
|
|
HANDLE hTask;
|
|
|
|
// ID of the device to use
|
|
LPWSTR deviceID;
|
|
// WASAPI object handles owned and used by audio thread
|
|
IMMDevice *pDevice;
|
|
IAudioClient *pAudioClient;
|
|
IAudioRenderClient *pRenderClient;
|
|
|
|
// WASAPI internal clock information, for estimating delay
|
|
IAudioClock *pAudioClock;
|
|
atomic_ullong sample_count; // samples per channel written by GetBuffer
|
|
UINT64 clock_frequency; // scale for position returned by GetPosition
|
|
LARGE_INTEGER qpc_frequency; // frequency of Windows' high resolution timer
|
|
|
|
// WASAPI control
|
|
IAudioSessionControl *pSessionControl; // setting the stream title
|
|
IAudioEndpointVolume *pEndpointVolume; // exclusive mode volume/mute
|
|
ISimpleAudioVolume *pAudioVolume; // shared mode volume/mute
|
|
DWORD vol_hw_support; // is hardware volume supported for exclusive-mode?
|
|
|
|
// ao options
|
|
int opt_exclusive;
|
|
|
|
// format info
|
|
WAVEFORMATEXTENSIBLE format;
|
|
AUDCLNT_SHAREMODE share_mode; // AUDCLNT_SHAREMODE_EXCLUSIVE / SHARED
|
|
UINT32 bufferFrameCount; // number of frames in buffer
|
|
struct ao_convert_fmt convert_format;
|
|
|
|
change_notify change;
|
|
} wasapi_state;
|
|
|
|
char *mp_PKEY_to_str_buf(char *buf, size_t buf_size, const PROPERTYKEY *pkey);
|
|
#define mp_PKEY_to_str(pkey) mp_PKEY_to_str_buf((char[42]){0}, 42, (pkey))
|
|
|
|
void wasapi_list_devs(struct ao *ao, struct ao_device_list *list);
|
|
bstr wasapi_get_specified_device_string(struct ao *ao);
|
|
LPWSTR wasapi_find_deviceID(struct ao *ao);
|
|
|
|
bool wasapi_thread_init(struct ao *ao);
|
|
void wasapi_thread_uninit(struct ao *ao);
|
|
|
|
#define EXIT_ON_ERROR(hres) \
|
|
do { if (FAILED(hres)) { goto exit_label; } } while(0)
|
|
#define SAFE_DESTROY(unk, release) \
|
|
do { if ((unk) != NULL) { release; (unk) = NULL; } } while(0)
|
|
|
|
#endif
|