2013-06-13 13:15:39 +00:00
|
|
|
/*
|
|
|
|
* This file is part of mpv.
|
|
|
|
*
|
2013-10-23 17:07:27 +00:00
|
|
|
* Original author: Jonathan Yong <10walls@gmail.com>
|
|
|
|
*
|
2013-06-13 13:15:39 +00:00
|
|
|
* mpv is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with mpv. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define COBJMACROS 1
|
|
|
|
#define _WIN32_WINNT 0x600
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2013-07-19 02:16:42 +00:00
|
|
|
#include <inttypes.h>
|
2013-06-13 13:15:39 +00:00
|
|
|
#include <process.h>
|
|
|
|
#include <initguid.h>
|
|
|
|
#include <audioclient.h>
|
|
|
|
#include <endpointvolume.h>
|
|
|
|
#include <mmdeviceapi.h>
|
|
|
|
#include <avrt.h>
|
|
|
|
|
2014-03-09 23:13:36 +00:00
|
|
|
#include "audio/out/ao_wasapi.h"
|
|
|
|
#include "audio/out/ao_wasapi_utils.h"
|
|
|
|
|
2013-06-13 13:15:39 +00:00
|
|
|
#include "audio/format.h"
|
2014-03-09 22:05:43 +00:00
|
|
|
#include "osdep/timer.h"
|
2014-03-11 18:50:09 +00:00
|
|
|
#include "osdep/io.h"
|
2013-06-13 13:15:39 +00:00
|
|
|
|
|
|
|
#define EXIT_ON_ERROR(hres) \
|
2013-07-21 03:25:05 +00:00
|
|
|
do { if (FAILED(hres)) { goto exit_label; } } while(0)
|
2013-07-19 12:40:14 +00:00
|
|
|
#define SAFE_RELEASE(unk, release) \
|
2013-07-21 03:25:05 +00:00
|
|
|
do { if ((unk) != NULL) { release; (unk) = NULL; } } while(0)
|
2013-06-13 13:15:39 +00:00
|
|
|
|
2014-03-09 22:05:43 +00:00
|
|
|
static double get_device_delay(struct wasapi_state *state) {
|
2014-05-20 23:04:47 +00:00
|
|
|
UINT64 sample_count = atomic_load(&state->sample_count);
|
2014-03-09 22:05:43 +00:00
|
|
|
UINT64 position, qpc_position;
|
|
|
|
HRESULT hr;
|
|
|
|
|
|
|
|
switch (hr = IAudioClock_GetPosition(state->pAudioClock, &position, &qpc_position)) {
|
|
|
|
case S_OK: case S_FALSE:
|
|
|
|
break;
|
|
|
|
default:
|
2014-03-09 23:13:36 +00:00
|
|
|
MP_ERR(state, "IAudioClock::GetPosition returned %s\n", wasapi_explain_err(hr));
|
2014-03-09 22:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
LARGE_INTEGER qpc_count;
|
|
|
|
QueryPerformanceCounter(&qpc_count);
|
|
|
|
double qpc_diff = (qpc_count.QuadPart * 1e7 / state->qpc_frequency.QuadPart) - qpc_position;
|
|
|
|
|
|
|
|
position += state->clock_frequency * (uint64_t)(qpc_diff / 1e7);
|
|
|
|
|
|
|
|
/* convert position to the same base as sample_count */
|
|
|
|
position = position * state->format.Format.nSamplesPerSec / state->clock_frequency;
|
|
|
|
|
|
|
|
double diff = sample_count - position;
|
|
|
|
double delay = diff / state->format.Format.nSamplesPerSec;
|
|
|
|
|
|
|
|
MP_TRACE(state, "device delay: %g samples (%g ms)\n", diff, delay * 1000);
|
|
|
|
|
|
|
|
return delay;
|
2013-06-13 13:15:39 +00:00
|
|
|
}
|
|
|
|
|
2014-03-09 22:05:43 +00:00
|
|
|
static void thread_feed(struct ao *ao)
|
2013-06-13 13:15:39 +00:00
|
|
|
{
|
2014-03-09 22:05:43 +00:00
|
|
|
struct wasapi_state *state = (struct wasapi_state *)ao->priv;
|
2013-07-20 01:53:08 +00:00
|
|
|
HRESULT hr;
|
|
|
|
|
|
|
|
UINT32 frame_count = state->bufferFrameCount;
|
|
|
|
|
|
|
|
if (state->share_mode == AUDCLNT_SHAREMODE_SHARED) {
|
|
|
|
UINT32 padding = 0;
|
|
|
|
hr = IAudioClient_GetCurrentPadding(state->pAudioClient, &padding);
|
2013-07-21 03:25:05 +00:00
|
|
|
EXIT_ON_ERROR(hr);
|
2013-07-20 01:53:08 +00:00
|
|
|
|
|
|
|
frame_count -= padding;
|
|
|
|
}
|
|
|
|
|
2014-03-09 22:05:43 +00:00
|
|
|
BYTE *pData;
|
2013-07-20 01:53:08 +00:00
|
|
|
hr = IAudioRenderClient_GetBuffer(state->pRenderClient,
|
|
|
|
frame_count, &pData);
|
2013-07-21 03:25:05 +00:00
|
|
|
EXIT_ON_ERROR(hr);
|
2014-03-06 18:50:22 +00:00
|
|
|
|
2014-03-09 22:05:43 +00:00
|
|
|
BYTE *data[1] = {pData};
|
|
|
|
ao_read_data(ao, (void**)data, frame_count, (int64_t) (
|
|
|
|
mp_time_us() + get_device_delay(state) * 1e6 +
|
|
|
|
frame_count * 1e6 / state->format.Format.nSamplesPerSec));
|
2014-03-06 18:50:22 +00:00
|
|
|
|
2013-06-13 13:15:39 +00:00
|
|
|
hr = IAudioRenderClient_ReleaseBuffer(state->pRenderClient,
|
2013-07-20 01:53:08 +00:00
|
|
|
frame_count, 0);
|
2013-07-21 03:25:05 +00:00
|
|
|
EXIT_ON_ERROR(hr);
|
2014-03-06 18:50:22 +00:00
|
|
|
|
2014-05-20 23:04:47 +00:00
|
|
|
atomic_fetch_add(&state->sample_count, frame_count);
|
2014-03-06 18:50:22 +00:00
|
|
|
|
2013-06-13 13:15:39 +00:00
|
|
|
return;
|
|
|
|
exit_label:
|
2013-12-21 17:34:42 +00:00
|
|
|
MP_ERR(state, "thread_feed fails with %"PRIx32"!\n", (uint32_t)hr);
|
2013-06-13 13:15:39 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-07-30 13:33:00 +00:00
|
|
|
static DWORD __stdcall ThreadLoop(void *lpParameter)
|
2013-06-13 13:15:39 +00:00
|
|
|
{
|
|
|
|
struct ao *ao = lpParameter;
|
|
|
|
if (!ao || !ao->priv)
|
|
|
|
return -1;
|
2013-07-20 17:13:39 +00:00
|
|
|
struct wasapi_state *state = (struct wasapi_state *)ao->priv;
|
2014-03-11 03:47:33 +00:00
|
|
|
if (wasapi_thread_init(ao))
|
2013-06-13 13:15:39 +00:00
|
|
|
goto exit_label;
|
|
|
|
|
2014-03-11 03:47:33 +00:00
|
|
|
MSG msg;
|
2013-06-13 13:15:39 +00:00
|
|
|
DWORD waitstatus = WAIT_FAILED;
|
|
|
|
HANDLE playcontrol[] =
|
2014-03-11 03:47:33 +00:00
|
|
|
{state->hUninit, state->hFeed, state->hForceFeed, NULL};
|
2013-12-21 17:34:42 +00:00
|
|
|
MP_VERBOSE(ao, "Entering dispatch loop!\n");
|
2014-03-11 03:47:33 +00:00
|
|
|
while (1) { /* watch events */
|
|
|
|
waitstatus = MsgWaitForMultipleObjects(3, playcontrol, FALSE, INFINITE,
|
|
|
|
QS_POSTMESSAGE | QS_SENDMESSAGE);
|
2013-06-13 13:15:39 +00:00
|
|
|
switch (waitstatus) {
|
|
|
|
case WAIT_OBJECT_0: /*shutdown*/
|
2014-03-11 03:47:33 +00:00
|
|
|
wasapi_thread_uninit(state);
|
2013-06-13 13:15:39 +00:00
|
|
|
goto exit_label;
|
2014-03-11 03:47:33 +00:00
|
|
|
case (WAIT_OBJECT_0 + 1): /* feed */
|
|
|
|
thread_feed(ao);
|
2013-06-13 13:15:39 +00:00
|
|
|
break;
|
2014-03-11 03:47:33 +00:00
|
|
|
case (WAIT_OBJECT_0 + 2): /* force feed */
|
2014-03-09 22:05:43 +00:00
|
|
|
thread_feed(ao);
|
2014-03-11 03:47:33 +00:00
|
|
|
SetEvent(state->hFeedDone);
|
2013-06-13 13:15:39 +00:00
|
|
|
break;
|
2014-03-11 03:47:33 +00:00
|
|
|
case (WAIT_OBJECT_0 + 3): /* messages to dispatch (COM marshalling) */
|
|
|
|
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
|
|
|
|
DispatchMessage(&msg);
|
|
|
|
}
|
2013-06-13 13:15:39 +00:00
|
|
|
break;
|
|
|
|
case WAIT_FAILED: /* ??? */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
exit_label:
|
2014-03-11 03:47:33 +00:00
|
|
|
/* dispatch any possible pending messages */
|
|
|
|
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
|
|
|
|
DispatchMessage(&msg);
|
|
|
|
}
|
2013-06-13 13:15:39 +00:00
|
|
|
return state->init_ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void closehandles(struct ao *ao)
|
|
|
|
{
|
2013-07-20 17:13:39 +00:00
|
|
|
struct wasapi_state *state = (struct wasapi_state *)ao->priv;
|
2013-06-13 13:15:39 +00:00
|
|
|
if (state->init_done)
|
|
|
|
CloseHandle(state->init_done);
|
|
|
|
if (state->hUninit)
|
|
|
|
CloseHandle(state->hUninit);
|
|
|
|
if (state->hFeed)
|
|
|
|
CloseHandle(state->hFeed);
|
|
|
|
}
|
|
|
|
|
2014-03-08 23:49:39 +00:00
|
|
|
static void uninit(struct ao *ao)
|
2013-06-13 13:15:39 +00:00
|
|
|
{
|
2013-12-21 17:34:42 +00:00
|
|
|
MP_VERBOSE(ao, "uninit!\n");
|
2013-07-20 17:13:39 +00:00
|
|
|
struct wasapi_state *state = (struct wasapi_state *)ao->priv;
|
2014-03-11 03:47:33 +00:00
|
|
|
wasapi_release_proxies(state);
|
2013-06-13 13:15:39 +00:00
|
|
|
SetEvent(state->hUninit);
|
|
|
|
/* wait up to 10 seconds */
|
|
|
|
if (WaitForSingleObject(state->threadLoop, 10000) == WAIT_TIMEOUT)
|
2014-03-11 03:47:33 +00:00
|
|
|
MP_ERR(ao, "audio loop thread refuses to abort!");
|
2013-06-13 13:15:39 +00:00
|
|
|
if (state->VistaBlob.hAvrt)
|
|
|
|
FreeLibrary(state->VistaBlob.hAvrt);
|
|
|
|
closehandles(ao);
|
2013-12-21 17:34:42 +00:00
|
|
|
MP_VERBOSE(ao, "uninit END!\n");
|
2013-06-13 13:15:39 +00:00
|
|
|
}
|
|
|
|
|
2013-07-22 20:57:51 +00:00
|
|
|
static int init(struct ao *ao)
|
2013-06-13 13:15:39 +00:00
|
|
|
{
|
2013-12-21 17:34:42 +00:00
|
|
|
MP_VERBOSE(ao, "init!\n");
|
2013-11-10 22:15:02 +00:00
|
|
|
ao->format = af_fmt_from_planar(ao->format);
|
2013-06-13 13:15:39 +00:00
|
|
|
struct mp_chmap_sel sel = {0};
|
|
|
|
mp_chmap_sel_add_waveext(&sel);
|
|
|
|
if (!ao_chmap_sel_adjust(ao, &sel, &ao->channels))
|
|
|
|
return -1;
|
2013-07-20 23:33:49 +00:00
|
|
|
struct wasapi_state *state = (struct wasapi_state *)ao->priv;
|
2013-12-21 17:34:42 +00:00
|
|
|
state->log = ao->log;
|
2014-03-09 23:13:36 +00:00
|
|
|
wasapi_fill_VistaBlob(state);
|
2013-07-19 12:40:14 +00:00
|
|
|
|
|
|
|
if (state->opt_list) {
|
2014-10-13 16:21:45 +00:00
|
|
|
wasapi_enumerate_devices(state->log, NULL, NULL);
|
2013-07-19 12:40:14 +00:00
|
|
|
}
|
|
|
|
|
2013-07-20 01:53:08 +00:00
|
|
|
if (state->opt_exclusive) {
|
|
|
|
state->share_mode = AUDCLNT_SHAREMODE_EXCLUSIVE;
|
|
|
|
} else {
|
|
|
|
state->share_mode = AUDCLNT_SHAREMODE_SHARED;
|
|
|
|
}
|
|
|
|
|
2013-06-13 13:15:39 +00:00
|
|
|
state->init_done = CreateEventW(NULL, FALSE, FALSE, NULL);
|
|
|
|
state->hUninit = CreateEventW(NULL, FALSE, FALSE, NULL);
|
2014-03-11 03:47:33 +00:00
|
|
|
state->hFeed = CreateEventW(NULL, FALSE, FALSE, NULL); /* for wasapi event mode */
|
|
|
|
state->hForceFeed = CreateEventW(NULL, FALSE, FALSE, NULL);
|
|
|
|
state->hFeedDone = CreateEventW(NULL, FALSE, FALSE, NULL);
|
|
|
|
if (!state->init_done || !state->hFeed || !state->hUninit ||
|
|
|
|
!state->hForceFeed || !state->hFeedDone)
|
2013-06-13 13:15:39 +00:00
|
|
|
{
|
|
|
|
closehandles(ao);
|
|
|
|
/* failed to init events */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
state->init_ret = -1;
|
2013-07-19 02:16:42 +00:00
|
|
|
state->threadLoop = (HANDLE)CreateThread(NULL, 0, &ThreadLoop, ao, 0, NULL);
|
2013-06-13 13:15:39 +00:00
|
|
|
if (!state->threadLoop) {
|
|
|
|
/* failed to init thread */
|
2013-12-21 17:34:42 +00:00
|
|
|
MP_ERR(ao, "fail to create thread!\n");
|
2013-06-13 13:15:39 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
WaitForSingleObject(state->init_done, INFINITE); /* wait on init complete */
|
2013-07-20 02:57:58 +00:00
|
|
|
if (state->init_ret) {
|
|
|
|
if (!ao->probing) {
|
2013-12-21 17:34:42 +00:00
|
|
|
MP_ERR(ao, "thread_init failed!\n");
|
2013-07-20 02:57:58 +00:00
|
|
|
}
|
2014-03-09 22:05:43 +00:00
|
|
|
} else
|
2013-12-21 17:34:42 +00:00
|
|
|
MP_VERBOSE(ao, "Init Done!\n");
|
2014-03-11 03:47:33 +00:00
|
|
|
|
|
|
|
wasapi_setup_proxies(state);
|
2013-06-13 13:15:39 +00:00
|
|
|
return state->init_ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int control(struct ao *ao, enum aocontrol cmd, void *arg)
|
|
|
|
{
|
2013-07-20 17:13:39 +00:00
|
|
|
struct wasapi_state *state = (struct wasapi_state *)ao->priv;
|
2013-06-13 13:15:39 +00:00
|
|
|
ao_control_vol_t *vol = (ao_control_vol_t *)arg;
|
2014-03-11 05:50:49 +00:00
|
|
|
BOOL mute;
|
2013-06-13 13:15:39 +00:00
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case AOCONTROL_GET_VOLUME:
|
2014-03-11 05:50:49 +00:00
|
|
|
if (state->opt_exclusive)
|
|
|
|
IAudioEndpointVolume_GetMasterVolumeLevelScalar(state->pEndpointVolumeProxy,
|
|
|
|
&state->audio_volume);
|
|
|
|
else
|
|
|
|
ISimpleAudioVolume_GetMasterVolume(state->pAudioVolumeProxy,
|
|
|
|
&state->audio_volume);
|
|
|
|
|
|
|
|
/* check to see if user manually changed volume through mixer;
|
|
|
|
this information is used in exclusive mode for restoring the mixer volume on uninit */
|
|
|
|
if (state->audio_volume != state->previous_volume) {
|
|
|
|
MP_VERBOSE(state, "mixer difference: %.2g now, expected %.2g\n",
|
|
|
|
state->audio_volume, state->previous_volume);
|
|
|
|
state->initial_volume = state->audio_volume;
|
|
|
|
}
|
|
|
|
|
2014-03-11 03:47:33 +00:00
|
|
|
vol->left = vol->right = 100.0f * state->audio_volume;
|
|
|
|
return CONTROL_OK;
|
2013-06-13 13:15:39 +00:00
|
|
|
case AOCONTROL_SET_VOLUME:
|
|
|
|
state->audio_volume = vol->left / 100.f;
|
2014-03-11 05:50:49 +00:00
|
|
|
if (state->opt_exclusive)
|
|
|
|
IAudioEndpointVolume_SetMasterVolumeLevelScalar(state->pEndpointVolumeProxy,
|
|
|
|
state->audio_volume, NULL);
|
|
|
|
else
|
|
|
|
ISimpleAudioVolume_SetMasterVolume(state->pAudioVolumeProxy,
|
|
|
|
state->audio_volume, NULL);
|
|
|
|
|
|
|
|
state->previous_volume = state->audio_volume;
|
|
|
|
return CONTROL_OK;
|
|
|
|
case AOCONTROL_GET_MUTE:
|
|
|
|
if (state->opt_exclusive)
|
|
|
|
IAudioEndpointVolume_GetMute(state->pEndpointVolumeProxy, &mute);
|
|
|
|
else
|
|
|
|
ISimpleAudioVolume_GetMute(state->pAudioVolumeProxy, &mute);
|
|
|
|
*(bool*)arg = mute;
|
|
|
|
|
|
|
|
return CONTROL_OK;
|
|
|
|
case AOCONTROL_SET_MUTE:
|
|
|
|
mute = *(bool*)arg;
|
|
|
|
if (state->opt_exclusive)
|
|
|
|
IAudioEndpointVolume_SetMute(state->pEndpointVolumeProxy, mute, NULL);
|
|
|
|
else
|
|
|
|
ISimpleAudioVolume_SetMute(state->pAudioVolumeProxy, mute, NULL);
|
|
|
|
|
2014-03-11 03:47:33 +00:00
|
|
|
return CONTROL_OK;
|
2014-09-06 00:30:57 +00:00
|
|
|
case AOCONTROL_HAS_PER_APP_VOLUME:
|
|
|
|
return CONTROL_TRUE;
|
2014-03-11 06:46:22 +00:00
|
|
|
case AOCONTROL_UPDATE_STREAM_TITLE: {
|
|
|
|
MP_VERBOSE(state, "Updating stream title to \"%s\"\n", (char*)arg);
|
2014-03-11 18:50:09 +00:00
|
|
|
wchar_t *title = mp_from_utf8(NULL, (char*)arg);
|
2014-03-11 06:46:22 +00:00
|
|
|
|
|
|
|
wchar_t *tmp = NULL;
|
|
|
|
|
|
|
|
/* There is a weird race condition in the IAudioSessionControl itself --
|
|
|
|
it seems that *sometimes* the SetDisplayName does not take effect and it still shows
|
|
|
|
the old title. Use this loop to insist until it works. */
|
|
|
|
do {
|
|
|
|
IAudioSessionControl_SetDisplayName(state->pSessionControlProxy, title, NULL);
|
|
|
|
|
|
|
|
SAFE_RELEASE(tmp, CoTaskMemFree(tmp));
|
|
|
|
IAudioSessionControl_GetDisplayName(state->pSessionControlProxy, &tmp);
|
|
|
|
} while (lstrcmpW(title, tmp));
|
|
|
|
SAFE_RELEASE(tmp, CoTaskMemFree(tmp));
|
2014-03-11 18:50:09 +00:00
|
|
|
talloc_free(title);
|
2014-03-11 06:46:22 +00:00
|
|
|
|
|
|
|
return CONTROL_OK;
|
|
|
|
}
|
2013-06-13 13:15:39 +00:00
|
|
|
default:
|
|
|
|
return CONTROL_UNKNOWN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-11 03:47:33 +00:00
|
|
|
|
audio/out/pull: remove race conditions
There were subtle and minor race conditions in the pull.c code, and AOs
using it (jack, portaudio, sdl, wasapi). Attempt to remove these.
There was at least a race condition in the ao_reset() implementation:
mp_ring_reset() was called concurrently to the audio callback. While the
ringbuffer uses atomics to allow concurrent access, the reset function
wasn't concurrency-safe (and can't easily be made to).
Fix this by stopping the audio callback before doing a reset. After
that, we can do anything without needing synchronization. The callback
is resumed when resuming playback at a later point.
Don't call driver->pause, and make driver->resume and driver->reset
start/stop the audio callback. In the initial state, the audio callback
must be disabled.
JackAudio of course is different. Maybe there is no way to suspend the
audio callback without "disconnecting" it (what jack_deactivate() would
do), so I'm not trying my luck, and implemented a really bad hack doing
active waiting until we get the audio callback into a state where it
won't interfere. Once the callback goes from AO_STATE_WAIT to NONE, we
can be sure that the callback doesn't access the ringbuffer or anything
else anymore. Since both sched_yield() and pthread_yield() apparently
are not always available, use mp_sleep_us(1) to avoid burning CPU during
active waiting.
The ao_jack.c change also removes a race condition: apparently we didn't
initialize _all_ ao fields before starting the audio callback.
In ao_wasapi.c, I'm not sure whether reset really waits for the audio
callback to return. Kovensky says it's not guaranteed, so disable the
reset callback - for now the behavior of ao_wasapi.c is like with
ao_jack.c, and active waiting is used to deal with the audio callback.
2014-05-29 00:24:17 +00:00
|
|
|
static void audio_reset(struct ao *ao)
|
2013-06-13 13:15:39 +00:00
|
|
|
{
|
2013-07-20 17:13:39 +00:00
|
|
|
struct wasapi_state *state = (struct wasapi_state *)ao->priv;
|
2014-03-11 03:47:33 +00:00
|
|
|
|
|
|
|
IAudioClient_Stop(state->pAudioClientProxy);
|
|
|
|
IAudioClient_Reset(state->pAudioClientProxy);
|
2014-05-20 23:04:47 +00:00
|
|
|
atomic_store(&state->sample_count, 0);
|
2013-06-13 13:15:39 +00:00
|
|
|
}
|
|
|
|
|
2014-03-11 03:47:33 +00:00
|
|
|
static void audio_resume(struct ao *ao)
|
2013-06-13 13:15:39 +00:00
|
|
|
{
|
2013-07-20 17:13:39 +00:00
|
|
|
struct wasapi_state *state = (struct wasapi_state *)ao->priv;
|
2014-03-11 03:47:33 +00:00
|
|
|
|
|
|
|
SetEvent(state->hForceFeed);
|
|
|
|
WaitForSingleObject(state->hFeedDone, INFINITE);
|
|
|
|
IAudioClient_Start(state->pAudioClientProxy);
|
2013-06-13 13:15:39 +00:00
|
|
|
}
|
|
|
|
|
2014-10-13 16:21:45 +00:00
|
|
|
static void list_devs(struct ao *ao, struct ao_device_list *list)
|
|
|
|
{
|
|
|
|
wasapi_enumerate_devices(mp_null_log, ao, list);
|
|
|
|
}
|
|
|
|
|
2013-07-20 23:33:49 +00:00
|
|
|
#define OPT_BASE_STRUCT struct wasapi_state
|
|
|
|
|
2013-07-20 17:13:39 +00:00
|
|
|
const struct ao_driver audio_out_wasapi = {
|
2013-10-23 17:07:27 +00:00
|
|
|
.description = "Windows WASAPI audio output (event mode)",
|
|
|
|
.name = "wasapi",
|
2013-06-13 13:15:39 +00:00
|
|
|
.init = init,
|
|
|
|
.uninit = uninit,
|
|
|
|
.control = control,
|
audio/out/pull: remove race conditions
There were subtle and minor race conditions in the pull.c code, and AOs
using it (jack, portaudio, sdl, wasapi). Attempt to remove these.
There was at least a race condition in the ao_reset() implementation:
mp_ring_reset() was called concurrently to the audio callback. While the
ringbuffer uses atomics to allow concurrent access, the reset function
wasn't concurrency-safe (and can't easily be made to).
Fix this by stopping the audio callback before doing a reset. After
that, we can do anything without needing synchronization. The callback
is resumed when resuming playback at a later point.
Don't call driver->pause, and make driver->resume and driver->reset
start/stop the audio callback. In the initial state, the audio callback
must be disabled.
JackAudio of course is different. Maybe there is no way to suspend the
audio callback without "disconnecting" it (what jack_deactivate() would
do), so I'm not trying my luck, and implemented a really bad hack doing
active waiting until we get the audio callback into a state where it
won't interfere. Once the callback goes from AO_STATE_WAIT to NONE, we
can be sure that the callback doesn't access the ringbuffer or anything
else anymore. Since both sched_yield() and pthread_yield() apparently
are not always available, use mp_sleep_us(1) to avoid burning CPU during
active waiting.
The ao_jack.c change also removes a race condition: apparently we didn't
initialize _all_ ao fields before starting the audio callback.
In ao_wasapi.c, I'm not sure whether reset really waits for the audio
callback to return. Kovensky says it's not guaranteed, so disable the
reset callback - for now the behavior of ao_wasapi.c is like with
ao_jack.c, and active waiting is used to deal with the audio callback.
2014-05-29 00:24:17 +00:00
|
|
|
//.reset = audio_reset, <- doesn't wait for audio callback to return
|
2013-06-13 13:15:39 +00:00
|
|
|
.resume = audio_resume,
|
2014-10-13 16:21:45 +00:00
|
|
|
.list_devs = list_devs,
|
2013-07-20 23:33:49 +00:00
|
|
|
.priv_size = sizeof(wasapi_state),
|
|
|
|
.options = (const struct m_option[]) {
|
|
|
|
OPT_FLAG("exclusive", opt_exclusive, 0),
|
|
|
|
OPT_FLAG("list", opt_list, 0),
|
2014-03-09 23:13:36 +00:00
|
|
|
OPT_STRING_VALIDATE("device", opt_device, 0, wasapi_validate_device),
|
2013-07-20 23:33:49 +00:00
|
|
|
{NULL},
|
|
|
|
},
|
2013-06-13 13:15:39 +00:00
|
|
|
};
|