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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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
|
|
|
|
2014-11-23 08:15:53 +00:00
|
|
|
static HRESULT get_device_delay(struct wasapi_state *state, double *delay) {
|
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;
|
|
|
|
|
2014-11-23 08:15:53 +00:00
|
|
|
hr = IAudioClock_GetPosition(state->pAudioClock, &position, &qpc_position);
|
|
|
|
/* GetPosition succeeded, but the result may be inaccurate due to the length of the call */
|
|
|
|
/* http://msdn.microsoft.com/en-us/library/windows/desktop/dd370889%28v=vs.85%29.aspx */
|
2015-01-02 22:26:08 +00:00
|
|
|
if (hr == S_FALSE) {
|
2014-11-23 08:15:53 +00:00
|
|
|
MP_DBG(state, "Possibly inaccurate device position.\n");
|
|
|
|
hr = S_OK;
|
2014-03-09 22:05:43 +00:00
|
|
|
}
|
2014-11-23 08:15:53 +00:00
|
|
|
EXIT_ON_ERROR(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;
|
|
|
|
|
2015-01-02 22:26:08 +00:00
|
|
|
position += state->clock_frequency * (uint64_t) (qpc_diff / 1e7);
|
2014-03-09 22:05:43 +00:00
|
|
|
|
|
|
|
/* convert position to the same base as sample_count */
|
|
|
|
position = position * state->format.Format.nSamplesPerSec / state->clock_frequency;
|
|
|
|
|
|
|
|
double diff = sample_count - position;
|
2014-11-23 08:15:53 +00:00
|
|
|
*delay = diff / state->format.Format.nSamplesPerSec;
|
2014-03-09 22:05:43 +00:00
|
|
|
|
2014-11-23 08:15:53 +00:00
|
|
|
MP_TRACE(state, "Device delay: %g samples (%g ms)\n", diff, *delay * 1000);
|
2014-03-09 22:05:43 +00:00
|
|
|
|
2014-11-23 08:15:53 +00:00
|
|
|
return S_OK;
|
|
|
|
exit_label:
|
2015-04-01 09:20:12 +00:00
|
|
|
MP_ERR(state, "Error getting device delay: %s\n", mp_HRESULT_to_str(hr));
|
2014-11-23 08:15:53 +00:00
|
|
|
return hr;
|
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
|
|
|
{
|
2015-03-31 21:12:24 +00:00
|
|
|
struct wasapi_state *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-11-18 12:17:20 +00:00
|
|
|
MP_TRACE(ao, "Frame to fill: %"PRIu32". Padding: %"PRIu32"\n", frame_count, padding);
|
2013-07-20 01:53:08 +00:00
|
|
|
}
|
2014-11-23 08:15:53 +00:00
|
|
|
double delay;
|
|
|
|
hr = get_device_delay(state, &delay);
|
|
|
|
EXIT_ON_ERROR(hr);
|
2013-07-20 01:53:08 +00:00
|
|
|
|
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};
|
2014-11-23 08:15:53 +00:00
|
|
|
|
2014-03-09 22:05:43 +00:00
|
|
|
ao_read_data(ao, (void**)data, frame_count, (int64_t) (
|
2014-11-23 08:15:53 +00:00
|
|
|
mp_time_us() + delay * 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:
|
2015-04-01 09:20:12 +00:00
|
|
|
MP_ERR(state, "Error feeding audio: %s\n", mp_HRESULT_to_str(hr));
|
2014-11-17 11:15:13 +00:00
|
|
|
MP_VERBOSE(ao, "Requesting ao reload\n");
|
|
|
|
ao_request_reload(ao);
|
2013-06-13 13:15:39 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-02-23 02:27:58 +00:00
|
|
|
static void thread_resume(struct ao *ao)
|
|
|
|
{
|
2015-03-31 21:12:24 +00:00
|
|
|
struct wasapi_state *state = ao->priv;
|
2015-02-23 02:27:58 +00:00
|
|
|
HRESULT hr;
|
|
|
|
|
|
|
|
MP_DBG(state, "Thread Resume\n");
|
|
|
|
UINT32 padding = 0;
|
|
|
|
hr = IAudioClient_GetCurrentPadding(state->pAudioClient, &padding);
|
|
|
|
if (hr != S_OK) {
|
2015-04-01 09:20:12 +00:00
|
|
|
MP_ERR(state, "IAudioClient_GetCurrentPadding returned %s\n",
|
|
|
|
mp_HRESULT_to_str(hr));
|
2015-02-23 02:27:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Fill the buffer before starting, but only if there is no audio queued to play. */
|
|
|
|
/* This prevents overfilling the buffer, which leads to problems in exclusive mode */
|
2015-03-29 00:12:48 +00:00
|
|
|
if (padding < (UINT32) state->bufferFrameCount)
|
2015-02-23 02:27:58 +00:00
|
|
|
thread_feed(ao);
|
|
|
|
|
2015-03-29 00:12:48 +00:00
|
|
|
// start feeding next wakeup if something else hasn't been requested
|
|
|
|
int expected = WASAPI_THREAD_RESUME;
|
|
|
|
atomic_compare_exchange_strong(&state->thread_state, &expected, WASAPI_THREAD_FEED);
|
2015-02-23 02:27:58 +00:00
|
|
|
hr = IAudioClient_Start(state->pAudioClient);
|
2015-04-01 09:20:12 +00:00
|
|
|
if (hr != S_OK)
|
|
|
|
MP_ERR(state, "IAudioClient_Start returned %s\n", mp_HRESULT_to_str(hr));
|
2015-02-23 02:27:58 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-02-23 02:47:50 +00:00
|
|
|
static void thread_reset(struct ao *ao)
|
|
|
|
{
|
2015-03-31 21:12:24 +00:00
|
|
|
struct wasapi_state *state = ao->priv;
|
2015-02-23 02:47:50 +00:00
|
|
|
HRESULT hr;
|
|
|
|
MP_DBG(state, "Thread Reset\n");
|
|
|
|
hr = IAudioClient_Stop(state->pAudioClient);
|
|
|
|
/* we may get S_FALSE if the stream is already stopped */
|
2015-04-01 09:20:12 +00:00
|
|
|
if (hr != S_OK && hr != S_FALSE)
|
|
|
|
MP_ERR(state, "IAudioClient_Stop returned: %s\n", mp_HRESULT_to_str(hr));
|
2015-02-23 02:47:50 +00:00
|
|
|
|
|
|
|
/* we may get S_FALSE if the stream is already reset */
|
|
|
|
hr = IAudioClient_Reset(state->pAudioClient);
|
2015-04-01 09:20:12 +00:00
|
|
|
if (hr != S_OK && hr != S_FALSE)
|
|
|
|
MP_ERR(state, "IAudioClient_Reset returned: %s\n", mp_HRESULT_to_str(hr));
|
2015-02-23 02:47:50 +00:00
|
|
|
|
|
|
|
atomic_store(&state->sample_count, 0);
|
2015-03-29 00:12:48 +00:00
|
|
|
// start feeding next wakeup if something else hasn't been requested
|
|
|
|
int expected = WASAPI_THREAD_RESET;
|
|
|
|
atomic_compare_exchange_strong(&state->thread_state, &expected, WASAPI_THREAD_FEED);
|
2015-02-23 02:47:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-29 00:12:48 +00:00
|
|
|
static DWORD __stdcall AudioThread(void *lpParameter)
|
2013-06-13 13:15:39 +00:00
|
|
|
{
|
|
|
|
struct ao *ao = lpParameter;
|
2015-03-31 21:12:24 +00:00
|
|
|
struct wasapi_state *state = ao->priv;
|
2014-11-15 06:47:28 +00:00
|
|
|
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
|
2014-11-17 10:48:02 +00:00
|
|
|
|
|
|
|
state->init_ret = wasapi_thread_init(ao);
|
2015-03-29 00:12:48 +00:00
|
|
|
SetEvent(state->hInitDone);
|
2014-11-28 18:12:35 +00:00
|
|
|
if (state->init_ret != S_OK)
|
2013-06-13 13:15:39 +00:00
|
|
|
goto exit_label;
|
2014-11-15 06:47:28 +00:00
|
|
|
|
2014-11-17 10:48:02 +00:00
|
|
|
MP_DBG(ao, "Entering dispatch loop\n");
|
2015-01-02 22:26:08 +00:00
|
|
|
while (true) { /* watch events */
|
2015-03-29 00:12:48 +00:00
|
|
|
HANDLE events[] = {state->hWake};
|
|
|
|
switch (MsgWaitForMultipleObjects(MP_ARRAY_SIZE(events), events, FALSE, INFINITE,
|
|
|
|
QS_POSTMESSAGE | QS_SENDMESSAGE)) {
|
|
|
|
/* AudioThread wakeup */
|
|
|
|
case WAIT_OBJECT_0:
|
|
|
|
switch (atomic_load(&state->thread_state)) {
|
|
|
|
case WASAPI_THREAD_FEED:
|
|
|
|
thread_feed(ao);
|
|
|
|
break;
|
|
|
|
case WASAPI_THREAD_RESET:
|
|
|
|
thread_reset(ao);
|
|
|
|
break;
|
|
|
|
case WASAPI_THREAD_RESUME:
|
|
|
|
thread_reset(ao);
|
|
|
|
thread_resume(ao);
|
|
|
|
break;
|
|
|
|
case WASAPI_THREAD_SHUTDOWN:
|
|
|
|
thread_reset(ao);
|
|
|
|
goto exit_label;
|
|
|
|
default:
|
|
|
|
MP_ERR(ao, "Unhandled thread state\n");
|
|
|
|
goto exit_label;
|
|
|
|
}
|
2015-02-23 02:27:58 +00:00
|
|
|
break;
|
2015-03-29 00:12:48 +00:00
|
|
|
/* messages to dispatch (COM marshalling) */
|
|
|
|
case (WAIT_OBJECT_0 + MP_ARRAY_SIZE(events)):
|
|
|
|
wasapi_dispatch(ao);
|
2013-06-13 13:15:39 +00:00
|
|
|
break;
|
2014-11-17 10:48:02 +00:00
|
|
|
default:
|
2015-03-29 00:12:48 +00:00
|
|
|
MP_ERR(ao, "Unhandled thread event\n");
|
2014-11-17 10:48:02 +00:00
|
|
|
goto exit_label;
|
2013-06-13 13:15:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
exit_label:
|
2014-11-15 06:47:28 +00:00
|
|
|
wasapi_thread_uninit(ao);
|
2014-11-17 10:48:02 +00:00
|
|
|
|
2014-11-15 06:47:28 +00:00
|
|
|
CoUninitialize();
|
2014-11-28 18:12:35 +00:00
|
|
|
MP_DBG(ao, "Thread return\n");
|
|
|
|
return 0;
|
2013-06-13 13:15:39 +00:00
|
|
|
}
|
|
|
|
|
2015-03-29 00:12:48 +00:00
|
|
|
static void set_thread_state(struct ao *ao, enum wasapi_thread_state thread_state)
|
2013-06-13 13:15:39 +00:00
|
|
|
{
|
2015-03-31 21:12:24 +00:00
|
|
|
struct wasapi_state *state = ao->priv;
|
2015-03-29 00:12:48 +00:00
|
|
|
atomic_store(&state->thread_state, thread_state);
|
|
|
|
SetEvent(state->hWake);
|
2013-06-13 13:15:39 +00:00
|
|
|
}
|
|
|
|
|
2014-03-08 23:49:39 +00:00
|
|
|
static void uninit(struct ao *ao)
|
2013-06-13 13:15:39 +00:00
|
|
|
{
|
2014-11-17 10:48:02 +00:00
|
|
|
MP_DBG(ao, "Uninit wasapi\n");
|
2015-03-31 21:12:24 +00:00
|
|
|
struct wasapi_state *state = ao->priv;
|
2014-03-11 03:47:33 +00:00
|
|
|
wasapi_release_proxies(state);
|
2015-03-29 00:12:48 +00:00
|
|
|
if (state->hWake)
|
|
|
|
set_thread_state(ao, WASAPI_THREAD_SHUTDOWN);
|
|
|
|
|
2013-06-13 13:15:39 +00:00
|
|
|
/* wait up to 10 seconds */
|
2015-03-29 00:12:48 +00:00
|
|
|
if (state->hAudioThread &&
|
|
|
|
WaitForSingleObject(state->hAudioThread, 10000) == WAIT_TIMEOUT)
|
|
|
|
{
|
2014-11-28 17:53:38 +00:00
|
|
|
MP_ERR(ao, "Audio loop thread refuses to abort\n");
|
2014-11-18 07:46:38 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-06-13 13:15:39 +00:00
|
|
|
if (state->VistaBlob.hAvrt)
|
|
|
|
FreeLibrary(state->VistaBlob.hAvrt);
|
2015-03-29 00:12:48 +00:00
|
|
|
|
|
|
|
SAFE_RELEASE(state->hInitDone, CloseHandle(state->hInitDone));
|
|
|
|
SAFE_RELEASE(state->hWake, CloseHandle(state->hWake));
|
|
|
|
SAFE_RELEASE(state->hAudioThread,CloseHandle(state->hAudioThread));
|
|
|
|
|
2014-11-28 02:44:06 +00:00
|
|
|
CoUninitialize();
|
2014-11-17 10:48:02 +00:00
|
|
|
MP_DBG(ao, "Uninit wasapi done\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
|
|
|
{
|
2014-11-17 10:48:02 +00:00
|
|
|
MP_DBG(ao, "Init wasapi\n");
|
2014-11-28 02:44:06 +00:00
|
|
|
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
|
|
|
|
|
2015-03-31 21:12:24 +00:00
|
|
|
struct wasapi_state *state = ao->priv;
|
2013-12-21 17:34:42 +00:00
|
|
|
state->log = ao->log;
|
2014-11-28 18:52:12 +00:00
|
|
|
if(!wasapi_fill_VistaBlob(state))
|
|
|
|
MP_WARN(ao, "Error loading thread priority functions\n");
|
2013-07-19 12:40:14 +00:00
|
|
|
|
2015-03-29 00:12:48 +00:00
|
|
|
state->hInitDone = CreateEventW(NULL, FALSE, FALSE, NULL);
|
|
|
|
state->hWake = CreateEventW(NULL, FALSE, FALSE, NULL);
|
|
|
|
if (!state->hInitDone || !state->hWake) {
|
|
|
|
MP_ERR(ao, "Error creating events\n");
|
2014-11-28 15:31:05 +00:00
|
|
|
uninit(ao);
|
2013-06-13 13:15:39 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2014-11-15 06:47:28 +00:00
|
|
|
|
2014-11-17 10:48:02 +00:00
|
|
|
state->init_ret = E_FAIL;
|
2015-03-29 00:12:48 +00:00
|
|
|
state->hAudioThread = CreateThread(NULL, 0, &AudioThread, ao, 0, NULL);
|
|
|
|
if (!state->hAudioThread) {
|
|
|
|
MP_ERR(ao, "Failed to create audio thread\n");
|
2014-11-28 15:31:05 +00:00
|
|
|
uninit(ao);
|
2013-06-13 13:15:39 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2014-11-15 06:47:28 +00:00
|
|
|
|
2015-03-29 00:12:48 +00:00
|
|
|
WaitForSingleObject(state->hInitDone, INFINITE); /* wait on init complete */
|
|
|
|
SAFE_RELEASE(state->hInitDone,CloseHandle(state->hInitDone));
|
2014-11-17 10:48:02 +00:00
|
|
|
if (state->init_ret != S_OK) {
|
2014-11-15 06:47:28 +00:00
|
|
|
if (!ao->probing)
|
2014-11-17 10:48:02 +00:00
|
|
|
MP_ERR(ao, "Received failure from audio thread\n");
|
2014-11-18 07:30:49 +00:00
|
|
|
uninit(ao);
|
2014-11-17 10:48:02 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2014-03-11 03:47:33 +00:00
|
|
|
|
|
|
|
wasapi_setup_proxies(state);
|
2014-11-15 06:47:28 +00:00
|
|
|
MP_DBG(ao, "Init wasapi done\n");
|
2014-11-17 10:48:02 +00:00
|
|
|
return 0;
|
2013-06-13 13:15:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int control(struct ao *ao, enum aocontrol cmd, void *arg)
|
|
|
|
{
|
2015-03-31 21:12:24 +00:00
|
|
|
struct wasapi_state *state = ao->priv;
|
|
|
|
ao_control_vol_t *vol = 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) {
|
2014-11-17 10:48:02 +00:00
|
|
|
MP_VERBOSE(state, "Mixer difference: %.2g now, expected %.2g\n",
|
2014-03-11 05:50:49 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2015-03-29 00:12:48 +00:00
|
|
|
set_thread_state(ao, WASAPI_THREAD_RESET);
|
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
|
|
|
{
|
2015-03-29 00:12:48 +00:00
|
|
|
set_thread_state(ao, WASAPI_THREAD_RESUME);
|
2013-06-13 13:15:39 +00:00
|
|
|
}
|
|
|
|
|
2015-03-31 08:56:17 +00:00
|
|
|
static void hotplug_uninit(struct ao *ao)
|
|
|
|
{
|
|
|
|
MP_DBG(ao, "Hotplug uninit\n");
|
2015-03-31 21:12:24 +00:00
|
|
|
struct wasapi_state *state = ao->priv;
|
2015-03-31 19:53:05 +00:00
|
|
|
wasapi_change_uninit(ao);
|
|
|
|
SAFE_RELEASE(state->pEnumerator, IMMDeviceEnumerator_Release(state->pEnumerator));
|
2015-03-31 08:56:17 +00:00
|
|
|
CoUninitialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hotplug_init(struct ao *ao)
|
|
|
|
{
|
|
|
|
MP_DBG(ao, "Hotplug init\n");
|
2015-03-31 21:12:24 +00:00
|
|
|
struct wasapi_state *state = ao->priv;
|
2015-03-31 08:56:17 +00:00
|
|
|
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
|
2015-03-31 19:53:05 +00:00
|
|
|
HRESULT hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL, CLSCTX_ALL,
|
|
|
|
&IID_IMMDeviceEnumerator, (void **)&state->pEnumerator);
|
|
|
|
EXIT_ON_ERROR(hr);
|
|
|
|
hr = wasapi_change_init(ao, true);
|
|
|
|
EXIT_ON_ERROR(hr);
|
2015-03-31 08:56:17 +00:00
|
|
|
|
2015-03-31 19:53:05 +00:00
|
|
|
return 0;
|
|
|
|
exit_label:
|
2015-04-01 09:20:12 +00:00
|
|
|
MP_ERR(state, "Error setting up audio hotplug: %s\n", mp_HRESULT_to_str(hr));
|
2015-03-31 19:53:05 +00:00
|
|
|
hotplug_uninit(ao);
|
|
|
|
return -1;
|
2014-10-13 16:21:45 +00:00
|
|
|
}
|
|
|
|
|
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 = {
|
2015-04-03 05:16:57 +00:00
|
|
|
.description = "Windows WASAPI audio output (event mode)",
|
|
|
|
.name = "wasapi",
|
|
|
|
.init = init,
|
|
|
|
.uninit = uninit,
|
|
|
|
.control = control,
|
|
|
|
.reset = audio_reset,
|
|
|
|
.resume = audio_resume,
|
|
|
|
.list_devs = wasapi_list_devs,
|
|
|
|
.hotplug_init = hotplug_init,
|
2015-03-31 08:56:17 +00:00
|
|
|
.hotplug_uninit = hotplug_uninit,
|
2015-04-03 05:16:57 +00:00
|
|
|
.priv_size = sizeof(wasapi_state),
|
|
|
|
.options = (const struct m_option[]) {
|
2013-07-20 23:33:49 +00:00
|
|
|
OPT_FLAG("exclusive", opt_exclusive, 0),
|
2015-03-31 09:33:24 +00:00
|
|
|
OPT_STRING("device", opt_device, 0),
|
2013-07-20 23:33:49 +00:00
|
|
|
{NULL},
|
|
|
|
},
|
2013-06-13 13:15:39 +00:00
|
|
|
};
|