2013-06-13 15:15:39 +02:00
|
|
|
/*
|
|
|
|
* This file is part of mpv.
|
|
|
|
*
|
2013-10-23 19:07:27 +02:00
|
|
|
* Original author: Jonathan Yong <10walls@gmail.com>
|
|
|
|
*
|
2013-06-13 15:15:39 +02: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 04:16:42 +02:00
|
|
|
#include <inttypes.h>
|
2013-06-13 15:15:39 +02:00
|
|
|
#include <process.h>
|
|
|
|
#include <initguid.h>
|
|
|
|
#include <audioclient.h>
|
|
|
|
#include <endpointvolume.h>
|
|
|
|
#include <mmdeviceapi.h>
|
|
|
|
#include <avrt.h>
|
|
|
|
|
2014-03-10 00:13:36 +01:00
|
|
|
#include "audio/out/ao_wasapi.h"
|
|
|
|
#include "audio/out/ao_wasapi_utils.h"
|
|
|
|
|
2013-12-17 02:02:25 +01:00
|
|
|
#include "options/m_option.h"
|
2013-12-18 17:14:31 +01:00
|
|
|
#include "options/m_config.h"
|
2013-06-13 15:15:39 +02:00
|
|
|
#include "audio/format.h"
|
2013-12-17 02:39:45 +01:00
|
|
|
#include "common/msg.h"
|
2013-12-18 17:14:31 +01:00
|
|
|
#include "misc/ring.h"
|
2013-06-13 15:15:39 +02:00
|
|
|
#include "ao.h"
|
2014-03-07 15:24:32 +01:00
|
|
|
#include "internal.h"
|
2014-03-06 19:50:22 +01:00
|
|
|
#include "compat/atomics.h"
|
2014-03-09 23:05:43 +01:00
|
|
|
#include "osdep/timer.h"
|
2013-06-13 15:15:39 +02:00
|
|
|
|
|
|
|
#define EXIT_ON_ERROR(hres) \
|
2013-07-21 05:25:05 +02:00
|
|
|
do { if (FAILED(hres)) { goto exit_label; } } while(0)
|
2013-07-19 14:40:14 +02:00
|
|
|
#define SAFE_RELEASE(unk, release) \
|
2013-07-21 05:25:05 +02:00
|
|
|
do { if ((unk) != NULL) { release; (unk) = NULL; } } while(0)
|
2013-06-13 15:15:39 +02:00
|
|
|
|
|
|
|
static int thread_init(struct ao *ao)
|
|
|
|
{
|
2013-07-20 19:13:39 +02:00
|
|
|
struct wasapi_state *state = (struct wasapi_state *)ao->priv;
|
2013-06-13 15:15:39 +02:00
|
|
|
HRESULT hr;
|
|
|
|
CoInitialize(NULL);
|
|
|
|
|
2013-07-19 14:40:14 +02:00
|
|
|
if (!state->opt_device) {
|
2013-07-21 17:38:01 +02:00
|
|
|
IMMDeviceEnumerator *pEnumerator;
|
|
|
|
hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL, CLSCTX_ALL,
|
|
|
|
&IID_IMMDeviceEnumerator, (void**)&pEnumerator);
|
2013-07-21 05:25:05 +02:00
|
|
|
EXIT_ON_ERROR(hr);
|
2013-07-21 17:38:01 +02:00
|
|
|
|
|
|
|
hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(pEnumerator,
|
2013-07-19 14:40:14 +02:00
|
|
|
eRender, eConsole,
|
|
|
|
&state->pDevice);
|
2013-07-21 17:38:01 +02:00
|
|
|
SAFE_RELEASE(pEnumerator, IMMDeviceEnumerator_Release(pEnumerator));
|
2013-07-21 01:00:51 +02:00
|
|
|
|
2014-03-10 00:13:36 +01:00
|
|
|
char *id = wasapi_get_device_id(state->pDevice);
|
2013-12-21 18:34:42 +01:00
|
|
|
MP_VERBOSE(ao, "default device ID: %s\n", id);
|
2013-07-21 01:00:51 +02:00
|
|
|
free(id);
|
2013-07-19 14:40:14 +02:00
|
|
|
} else {
|
2014-03-10 00:13:36 +01:00
|
|
|
hr = wasapi_find_and_load_device(ao, &state->pDevice, state->opt_device);
|
2013-07-19 14:40:14 +02:00
|
|
|
}
|
2013-07-21 05:25:05 +02:00
|
|
|
EXIT_ON_ERROR(hr);
|
2013-06-13 15:15:39 +02:00
|
|
|
|
2014-03-10 00:13:36 +01:00
|
|
|
char *name = wasapi_get_device_name(state->pDevice);
|
2013-12-21 18:34:42 +01:00
|
|
|
MP_VERBOSE(ao, "device loaded: %s\n", name);
|
2013-07-21 01:00:51 +02:00
|
|
|
free(name);
|
|
|
|
|
2013-06-13 15:15:39 +02:00
|
|
|
hr = IMMDeviceActivator_Activate(state->pDevice, &IID_IAudioClient,
|
|
|
|
CLSCTX_ALL, NULL, (void **)&state->pAudioClient);
|
2013-07-21 05:25:05 +02:00
|
|
|
EXIT_ON_ERROR(hr);
|
2013-06-13 15:15:39 +02:00
|
|
|
|
|
|
|
hr = IMMDeviceActivator_Activate(state->pDevice, &IID_IAudioEndpointVolume,
|
|
|
|
CLSCTX_ALL, NULL,
|
|
|
|
(void **)&state->pEndpointVolume);
|
2013-07-21 05:25:05 +02:00
|
|
|
EXIT_ON_ERROR(hr);
|
2013-06-13 15:15:39 +02:00
|
|
|
IAudioEndpointVolume_QueryHardwareSupport(state->pEndpointVolume,
|
|
|
|
&state->vol_hw_support);
|
|
|
|
|
2014-03-10 00:13:36 +01:00
|
|
|
state->init_ret = wasapi_find_formats(ao); /* Probe support formats */
|
2013-06-13 15:15:39 +02:00
|
|
|
if (state->init_ret)
|
|
|
|
goto exit_label;
|
2014-03-10 00:13:36 +01:00
|
|
|
if (!wasapi_fix_format(state)) { /* now that we're sure what format to use */
|
2013-12-21 18:34:42 +01:00
|
|
|
MP_VERBOSE(ao, "thread_init OK!\n");
|
2013-06-13 15:15:39 +02:00
|
|
|
SetEvent(state->init_done);
|
|
|
|
return state->init_ret;
|
|
|
|
}
|
|
|
|
exit_label:
|
|
|
|
state->init_ret = -1;
|
|
|
|
SetEvent(state->init_done);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-03-09 23:05:43 +01:00
|
|
|
|
|
|
|
static double get_device_delay(struct wasapi_state *state) {
|
|
|
|
UINT64 sample_count = state->sample_count;
|
|
|
|
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-10 00:13:36 +01:00
|
|
|
MP_ERR(state, "IAudioClock::GetPosition returned %s\n", wasapi_explain_err(hr));
|
2014-03-09 23:05:43 +01: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 15:15:39 +02:00
|
|
|
}
|
|
|
|
|
2014-03-09 23:05:43 +01:00
|
|
|
static void thread_feed(struct ao *ao)
|
2013-06-13 15:15:39 +02:00
|
|
|
{
|
2014-03-09 23:05:43 +01:00
|
|
|
struct wasapi_state *state = (struct wasapi_state *)ao->priv;
|
2013-07-20 03:53:08 +02: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 05:25:05 +02:00
|
|
|
EXIT_ON_ERROR(hr);
|
2013-07-20 03:53:08 +02:00
|
|
|
|
|
|
|
frame_count -= padding;
|
|
|
|
}
|
|
|
|
|
2014-03-09 23:05:43 +01:00
|
|
|
BYTE *pData;
|
2013-07-20 03:53:08 +02:00
|
|
|
hr = IAudioRenderClient_GetBuffer(state->pRenderClient,
|
|
|
|
frame_count, &pData);
|
2013-07-21 05:25:05 +02:00
|
|
|
EXIT_ON_ERROR(hr);
|
2014-03-06 19:50:22 +01:00
|
|
|
|
2014-03-09 23:05:43 +01: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 19:50:22 +01:00
|
|
|
|
2013-06-13 15:15:39 +02:00
|
|
|
hr = IAudioRenderClient_ReleaseBuffer(state->pRenderClient,
|
2013-07-20 03:53:08 +02:00
|
|
|
frame_count, 0);
|
2013-07-21 05:25:05 +02:00
|
|
|
EXIT_ON_ERROR(hr);
|
2014-03-06 19:50:22 +01:00
|
|
|
|
2014-03-09 23:05:43 +01:00
|
|
|
state->sample_count += frame_count;
|
2014-03-06 19:50:22 +01:00
|
|
|
|
2013-06-13 15:15:39 +02:00
|
|
|
return;
|
|
|
|
exit_label:
|
2013-12-21 18:34:42 +01:00
|
|
|
MP_ERR(state, "thread_feed fails with %"PRIx32"!\n", (uint32_t)hr);
|
2013-06-13 15:15:39 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-09 23:05:43 +01:00
|
|
|
static void thread_pause(struct ao *ao)
|
|
|
|
{
|
|
|
|
struct wasapi_state *state = (struct wasapi_state *)ao->priv;
|
|
|
|
|
|
|
|
state->is_playing = 0;
|
|
|
|
IAudioClient_Stop(state->pAudioClient);
|
|
|
|
IAudioClient_Reset(state->pAudioClient);
|
|
|
|
state->sample_count = 0;
|
|
|
|
mp_memory_barrier();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void thread_resume(struct ao *ao)
|
2013-06-13 15:15:39 +02:00
|
|
|
{
|
2014-03-09 23:05:43 +01:00
|
|
|
struct wasapi_state *state = (struct wasapi_state *)ao->priv;
|
|
|
|
|
2013-07-19 18:06:14 +02:00
|
|
|
state->is_playing = 1;
|
2014-03-09 23:05:43 +01:00
|
|
|
thread_feed(ao);
|
2013-06-13 15:15:39 +02:00
|
|
|
IAudioClient_Start(state->pAudioClient);
|
|
|
|
}
|
|
|
|
|
2014-03-09 23:05:43 +01:00
|
|
|
static void thread_reset(struct ao *ao)
|
2013-07-19 18:06:14 +02:00
|
|
|
{
|
2014-03-09 23:05:43 +01:00
|
|
|
thread_pause(ao);
|
2013-07-19 18:06:14 +02:00
|
|
|
}
|
|
|
|
|
2013-07-20 19:13:39 +02:00
|
|
|
static void thread_getVol(wasapi_state *state)
|
2013-06-13 15:15:39 +02:00
|
|
|
{
|
|
|
|
IAudioEndpointVolume_GetMasterVolumeLevelScalar(state->pEndpointVolume,
|
|
|
|
&state->audio_volume);
|
|
|
|
SetEvent(state->hDoneVol);
|
|
|
|
}
|
|
|
|
|
2013-07-20 19:13:39 +02:00
|
|
|
static void thread_setVol(wasapi_state *state)
|
2013-06-13 15:15:39 +02:00
|
|
|
{
|
|
|
|
IAudioEndpointVolume_SetMasterVolumeLevelScalar(state->pEndpointVolume,
|
|
|
|
state->audio_volume, NULL);
|
|
|
|
SetEvent(state->hDoneVol);
|
|
|
|
}
|
|
|
|
|
2013-07-20 19:13:39 +02:00
|
|
|
static void thread_uninit(wasapi_state *state)
|
2013-06-13 15:15:39 +02:00
|
|
|
{
|
|
|
|
if (state->pAudioClient)
|
|
|
|
IAudioClient_Stop(state->pAudioClient);
|
|
|
|
if (state->pRenderClient)
|
|
|
|
IAudioRenderClient_Release(state->pRenderClient);
|
2014-03-06 19:50:22 +01:00
|
|
|
if (state->pAudioClock)
|
|
|
|
IAudioClock_Release(state->pAudioClock);
|
2013-06-13 15:15:39 +02:00
|
|
|
if (state->pAudioClient)
|
|
|
|
IAudioClient_Release(state->pAudioClient);
|
|
|
|
if (state->pDevice)
|
|
|
|
IMMDevice_Release(state->pDevice);
|
|
|
|
if (state->hTask)
|
|
|
|
state->VistaBlob.pAvRevertMmThreadCharacteristics(state->hTask);
|
|
|
|
CoUninitialize();
|
2013-07-19 04:16:42 +02:00
|
|
|
ExitThread(0);
|
2013-06-13 15:15:39 +02:00
|
|
|
}
|
|
|
|
|
2014-03-09 23:05:43 +01:00
|
|
|
static void audio_drain(struct ao *ao)
|
|
|
|
{
|
|
|
|
struct wasapi_state *state = (struct wasapi_state *)ao->priv;
|
|
|
|
while (1) {
|
|
|
|
if (WaitForSingleObject(state->hFeed,2000) == WAIT_OBJECT_0 &&
|
|
|
|
ao->api->get_delay(ao))
|
|
|
|
{
|
|
|
|
thread_feed(ao);
|
|
|
|
} else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-30 15:33:00 +02:00
|
|
|
static DWORD __stdcall ThreadLoop(void *lpParameter)
|
2013-06-13 15:15:39 +02:00
|
|
|
{
|
|
|
|
struct ao *ao = lpParameter;
|
|
|
|
int feedwatch = 0;
|
|
|
|
if (!ao || !ao->priv)
|
|
|
|
return -1;
|
2013-07-20 19:13:39 +02:00
|
|
|
struct wasapi_state *state = (struct wasapi_state *)ao->priv;
|
2013-06-13 15:15:39 +02:00
|
|
|
if (thread_init(ao))
|
|
|
|
goto exit_label;
|
|
|
|
|
|
|
|
DWORD waitstatus = WAIT_FAILED;
|
|
|
|
HANDLE playcontrol[] =
|
|
|
|
{state->hUninit, state->hPause, state->hReset, state->hGetvol,
|
|
|
|
state->hSetvol, state->hPlay, state->hFeed, NULL};
|
2013-12-21 18:34:42 +01:00
|
|
|
MP_VERBOSE(ao, "Entering dispatch loop!\n");
|
2013-06-13 15:15:39 +02:00
|
|
|
while (1) { /* watch events, poll at least every 2 seconds */
|
|
|
|
waitstatus = WaitForMultipleObjects(7, playcontrol, FALSE, 2000);
|
|
|
|
switch (waitstatus) {
|
|
|
|
case WAIT_OBJECT_0: /*shutdown*/
|
|
|
|
feedwatch = 0;
|
|
|
|
thread_uninit(state);
|
|
|
|
goto exit_label;
|
|
|
|
case (WAIT_OBJECT_0 + 1): /* pause */
|
|
|
|
feedwatch = 0;
|
2014-03-09 23:05:43 +01:00
|
|
|
thread_pause(ao);
|
2013-06-13 15:15:39 +02:00
|
|
|
break;
|
|
|
|
case (WAIT_OBJECT_0 + 2): /* reset */
|
|
|
|
feedwatch = 0;
|
2014-03-09 23:05:43 +01:00
|
|
|
thread_reset(ao);
|
2013-06-13 15:15:39 +02:00
|
|
|
break;
|
|
|
|
case (WAIT_OBJECT_0 + 3): /* getVolume */
|
|
|
|
thread_getVol(state);
|
|
|
|
break;
|
|
|
|
case (WAIT_OBJECT_0 + 4): /* setVolume */
|
|
|
|
thread_setVol(state);
|
|
|
|
break;
|
|
|
|
case (WAIT_OBJECT_0 + 5): /* play */
|
|
|
|
feedwatch = 0;
|
2014-03-09 23:05:43 +01:00
|
|
|
thread_resume(ao);
|
2013-06-13 15:15:39 +02:00
|
|
|
break;
|
|
|
|
case (WAIT_OBJECT_0 + 6): /* feed */
|
2013-07-27 06:28:42 +02:00
|
|
|
if (state->is_playing)
|
|
|
|
feedwatch = 1;
|
2014-03-09 23:05:43 +01:00
|
|
|
thread_feed(ao);
|
2013-06-13 15:15:39 +02:00
|
|
|
break;
|
|
|
|
case WAIT_TIMEOUT: /* Did our feed die? */
|
|
|
|
if (feedwatch)
|
|
|
|
return -1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
case WAIT_FAILED: /* ??? */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
exit_label:
|
|
|
|
return state->init_ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void closehandles(struct ao *ao)
|
|
|
|
{
|
2013-07-20 19:13:39 +02:00
|
|
|
struct wasapi_state *state = (struct wasapi_state *)ao->priv;
|
2013-06-13 15:15:39 +02:00
|
|
|
if (state->init_done)
|
|
|
|
CloseHandle(state->init_done);
|
|
|
|
if (state->hPlay)
|
|
|
|
CloseHandle(state->hPlay);
|
|
|
|
if (state->hPause)
|
|
|
|
CloseHandle(state->hPause);
|
|
|
|
if (state->hReset)
|
|
|
|
CloseHandle(state->hReset);
|
|
|
|
if (state->hUninit)
|
|
|
|
CloseHandle(state->hUninit);
|
|
|
|
if (state->hFeed)
|
|
|
|
CloseHandle(state->hFeed);
|
|
|
|
if (state->hGetvol)
|
|
|
|
CloseHandle(state->hGetvol);
|
|
|
|
if (state->hSetvol)
|
|
|
|
CloseHandle(state->hSetvol);
|
|
|
|
if (state->hDoneVol)
|
|
|
|
CloseHandle(state->hDoneVol);
|
|
|
|
}
|
|
|
|
|
2014-03-09 00:49:39 +01:00
|
|
|
static void uninit(struct ao *ao)
|
2013-06-13 15:15:39 +02:00
|
|
|
{
|
2013-12-21 18:34:42 +01:00
|
|
|
MP_VERBOSE(ao, "uninit!\n");
|
2013-07-20 19:13:39 +02:00
|
|
|
struct wasapi_state *state = (struct wasapi_state *)ao->priv;
|
2014-03-09 00:49:39 +01:00
|
|
|
state->immed = 1;
|
2013-06-13 15:15:39 +02:00
|
|
|
SetEvent(state->hUninit);
|
|
|
|
/* wait up to 10 seconds */
|
|
|
|
if (WaitForSingleObject(state->threadLoop, 10000) == WAIT_TIMEOUT)
|
|
|
|
SetEvent(state->fatal_error);
|
|
|
|
if (state->VistaBlob.hAvrt)
|
|
|
|
FreeLibrary(state->VistaBlob.hAvrt);
|
|
|
|
closehandles(ao);
|
2013-12-21 18:34:42 +01:00
|
|
|
MP_VERBOSE(ao, "uninit END!\n");
|
2013-06-13 15:15:39 +02:00
|
|
|
}
|
|
|
|
|
2013-07-22 22:57:51 +02:00
|
|
|
static int init(struct ao *ao)
|
2013-06-13 15:15:39 +02:00
|
|
|
{
|
2013-12-21 18:34:42 +01:00
|
|
|
MP_VERBOSE(ao, "init!\n");
|
2013-11-10 23:15:02 +01:00
|
|
|
ao->format = af_fmt_from_planar(ao->format);
|
2013-06-13 15:15:39 +02: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-21 01:33:49 +02:00
|
|
|
struct wasapi_state *state = (struct wasapi_state *)ao->priv;
|
2013-12-21 18:34:42 +01:00
|
|
|
state->log = ao->log;
|
2014-03-10 00:13:36 +01:00
|
|
|
wasapi_fill_VistaBlob(state);
|
2013-07-19 14:40:14 +02:00
|
|
|
|
|
|
|
if (state->opt_list) {
|
2014-03-10 00:13:36 +01:00
|
|
|
wasapi_enumerate_devices(state->log);
|
2013-07-19 14:40:14 +02:00
|
|
|
}
|
|
|
|
|
2013-07-20 03:53:08 +02:00
|
|
|
if (state->opt_exclusive) {
|
|
|
|
state->share_mode = AUDCLNT_SHAREMODE_EXCLUSIVE;
|
|
|
|
} else {
|
|
|
|
state->share_mode = AUDCLNT_SHAREMODE_SHARED;
|
|
|
|
}
|
|
|
|
|
2013-06-13 15:15:39 +02:00
|
|
|
state->init_done = CreateEventW(NULL, FALSE, FALSE, NULL);
|
|
|
|
state->hPlay = CreateEventW(NULL, FALSE, FALSE, NULL); /* kick start audio feed */
|
|
|
|
state->hPause = CreateEventW(NULL, FALSE, FALSE, NULL);
|
|
|
|
state->hReset = CreateEventW(NULL, FALSE, FALSE, NULL);
|
|
|
|
state->hGetvol = CreateEventW(NULL, FALSE, FALSE, NULL);
|
|
|
|
state->hSetvol = CreateEventW(NULL, FALSE, FALSE, NULL);
|
|
|
|
state->hDoneVol = CreateEventW(NULL, FALSE, FALSE, NULL);
|
|
|
|
state->hUninit = CreateEventW(NULL, FALSE, FALSE, NULL);
|
|
|
|
state->fatal_error = CreateEventW(NULL, TRUE, FALSE, NULL);
|
|
|
|
state->hFeed = CreateEvent(NULL, FALSE, FALSE, NULL); /* for wasapi event mode */
|
|
|
|
if (!state->init_done || !state->fatal_error || !state->hPlay ||
|
|
|
|
!state->hPause || !state->hFeed || !state->hReset || !state->hGetvol ||
|
|
|
|
!state->hSetvol || !state->hDoneVol)
|
|
|
|
{
|
|
|
|
closehandles(ao);
|
|
|
|
/* failed to init events */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
state->init_ret = -1;
|
2013-07-19 04:16:42 +02:00
|
|
|
state->threadLoop = (HANDLE)CreateThread(NULL, 0, &ThreadLoop, ao, 0, NULL);
|
2013-06-13 15:15:39 +02:00
|
|
|
if (!state->threadLoop) {
|
|
|
|
/* failed to init thread */
|
2013-12-21 18:34:42 +01:00
|
|
|
MP_ERR(ao, "fail to create thread!\n");
|
2013-06-13 15:15:39 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
WaitForSingleObject(state->init_done, INFINITE); /* wait on init complete */
|
2013-07-20 04:57:58 +02:00
|
|
|
if (state->init_ret) {
|
|
|
|
if (!ao->probing) {
|
2013-12-21 18:34:42 +01:00
|
|
|
MP_ERR(ao, "thread_init failed!\n");
|
2013-07-20 04:57:58 +02:00
|
|
|
}
|
2014-03-09 23:05:43 +01:00
|
|
|
} else
|
2013-12-21 18:34:42 +01:00
|
|
|
MP_VERBOSE(ao, "Init Done!\n");
|
2013-06-13 15:15:39 +02:00
|
|
|
return state->init_ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int control(struct ao *ao, enum aocontrol cmd, void *arg)
|
|
|
|
{
|
2013-07-20 19:13:39 +02:00
|
|
|
struct wasapi_state *state = (struct wasapi_state *)ao->priv;
|
2013-06-13 15:15:39 +02:00
|
|
|
if (!(state->vol_hw_support & ENDPOINT_HARDWARE_SUPPORT_VOLUME))
|
|
|
|
return CONTROL_UNKNOWN; /* hw does not support volume controls in exclusive mode */
|
|
|
|
|
|
|
|
ao_control_vol_t *vol = (ao_control_vol_t *)arg;
|
|
|
|
ResetEvent(state->hDoneVol);
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case AOCONTROL_GET_VOLUME:
|
|
|
|
SetEvent(state->hGetvol);
|
|
|
|
if (WaitForSingleObject(state->hDoneVol, 100) == WAIT_OBJECT_0) {
|
|
|
|
vol->left = vol->right = 100.0f * state->audio_volume;
|
|
|
|
return CONTROL_OK;
|
|
|
|
}
|
|
|
|
return CONTROL_UNKNOWN;
|
|
|
|
case AOCONTROL_SET_VOLUME:
|
|
|
|
state->audio_volume = vol->left / 100.f;
|
|
|
|
SetEvent(state->hSetvol);
|
|
|
|
if (WaitForSingleObject(state->hDoneVol, 100) == WAIT_OBJECT_0)
|
|
|
|
return CONTROL_OK;
|
|
|
|
return CONTROL_UNKNOWN;
|
|
|
|
default:
|
|
|
|
return CONTROL_UNKNOWN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void audio_resume(struct ao *ao)
|
|
|
|
{
|
2013-07-20 19:13:39 +02:00
|
|
|
struct wasapi_state *state = (struct wasapi_state *)ao->priv;
|
2013-06-13 15:15:39 +02:00
|
|
|
ResetEvent(state->hPause);
|
|
|
|
ResetEvent(state->hReset);
|
|
|
|
SetEvent(state->hPlay);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void audio_pause(struct ao *ao)
|
|
|
|
{
|
2013-07-20 19:13:39 +02:00
|
|
|
struct wasapi_state *state = (struct wasapi_state *)ao->priv;
|
2013-06-13 15:15:39 +02:00
|
|
|
ResetEvent(state->hPlay);
|
|
|
|
SetEvent(state->hPause);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void reset(struct ao *ao)
|
|
|
|
{
|
2013-07-20 19:13:39 +02:00
|
|
|
struct wasapi_state *state = (struct wasapi_state *)ao->priv;
|
2013-06-13 15:15:39 +02:00
|
|
|
ResetEvent(state->hPlay);
|
|
|
|
SetEvent(state->hReset);
|
|
|
|
}
|
|
|
|
|
2013-07-21 01:33:49 +02:00
|
|
|
#define OPT_BASE_STRUCT struct wasapi_state
|
|
|
|
|
2013-07-20 19:13:39 +02:00
|
|
|
const struct ao_driver audio_out_wasapi = {
|
2013-10-23 19:07:27 +02:00
|
|
|
.description = "Windows WASAPI audio output (event mode)",
|
|
|
|
.name = "wasapi",
|
2013-06-13 15:15:39 +02:00
|
|
|
.init = init,
|
|
|
|
.uninit = uninit,
|
|
|
|
.control = control,
|
|
|
|
.pause = audio_pause,
|
|
|
|
.resume = audio_resume,
|
|
|
|
.reset = reset,
|
2014-03-09 23:05:43 +01:00
|
|
|
.drain = audio_drain,
|
2013-07-21 01:33:49 +02: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-10 00:13:36 +01:00
|
|
|
OPT_STRING_VALIDATE("device", opt_device, 0, wasapi_validate_device),
|
2013-07-21 01:33:49 +02:00
|
|
|
{NULL},
|
|
|
|
},
|
2013-06-13 15:15:39 +02:00
|
|
|
};
|