| 1 |
/* |
| 2 |
win32_wasapi: audio output for Windows wasapi exclusive mode audio |
| 3 |
|
| 4 |
copyright ?-2013 by the mpg123 project - free software under the terms of the LGPL 2.1 |
| 5 |
see COPYING and AUTHORS files in distribution or http://mpg123.org |
| 6 |
|
| 7 |
based on win32.c |
| 8 |
*/ |
| 9 |
#if defined(_WIN32_WINNT) |
| 10 |
# if _WIN32_WINNT < 0x0601 |
| 11 |
# error "wrong _WIN32_WINNT value" |
| 12 |
# endif |
| 13 |
#else |
| 14 |
# define _WIN32_WINNT 0x0601 |
| 15 |
#endif |
| 16 |
#define COBJMACROS 1 |
| 17 |
#include "../out123_int.h" |
| 18 |
#include <inttypes.h> |
| 19 |
#include <initguid.h> |
| 20 |
#include <audioclient.h> |
| 21 |
#include <mmdeviceapi.h> |
| 22 |
#include <avrt.h> |
| 23 |
#include <functiondiscoverykeys_devpkey.h> |
| 24 |
#include "../../common/debug.h" |
| 25 |
|
| 26 |
#ifdef _MSC_VER |
| 27 |
|
| 28 |
/* When compiling C code with MSVC it is only possible to declare, but not |
| 29 |
define the WASAPI interface GUIDs using MS headers. So we define them |
| 30 |
ourselves. */ |
| 31 |
|
| 32 |
#ifndef GUID_SECT |
| 33 |
#define GUID_SECT |
| 34 |
#endif |
| 35 |
|
| 36 |
#define __DEFINE_GUID(n,l,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8) static const GUID n GUID_SECT = {l,w1,w2,{b1,b2,b3,b4,b5,b6,b7,b8}} |
| 37 |
#define __DEFINE_IID(n,l,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8) static const IID n GUID_SECT = {l,w1,w2,{b1,b2,b3,b4,b5,b6,b7,b8}} |
| 38 |
#define __DEFINE_CLSID(n,l,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8) static const CLSID n GUID_SECT = {l,w1,w2,{b1,b2,b3,b4,b5,b6,b7,b8}} |
| 39 |
#define __DEFINE_PROPERTYKEY(n,l,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8,pid) static const PROPERTYKEY n GUID_SECT = {{l,w1,w2,{b1,b2,b3,b4,b5,b6,b7,b8}},pid} |
| 40 |
#define MPG123_DEFINE_CLSID(className, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \ |
| 41 |
__DEFINE_CLSID(mpg123_CLSID_##className, 0x##l, 0x##w1, 0x##w2, 0x##b1, 0x##b2, 0x##b3, 0x##b4, 0x##b5, 0x##b6, 0x##b7, 0x##b8) |
| 42 |
#define MPG123_DEFINE_IID(interfaceName, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \ |
| 43 |
__DEFINE_IID(mpg123_IID_##interfaceName, 0x##l, 0x##w1, 0x##w2, 0x##b1, 0x##b2, 0x##b3, 0x##b4, 0x##b5, 0x##b6, 0x##b7, 0x##b8) |
| 44 |
#define MPG123_DEFINE_PROPERTYKEY(interfaceName, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8, pid) \ |
| 45 |
__DEFINE_PROPERTYKEY(mpg123_PKEY_##interfaceName, 0x##l, 0x##w1, 0x##w2, 0x##b1, 0x##b2, 0x##b3, 0x##b4, 0x##b5, 0x##b6, 0x##b7, 0x##b8, pid) |
| 46 |
|
| 47 |
// "1CB9AD4C-DBFA-4c32-B178-C2F568A703B2" |
| 48 |
MPG123_DEFINE_IID(IAudioClient, 1cb9ad4c, dbfa, 4c32, b1, 78, c2, f5, 68, a7, 03, b2); |
| 49 |
// "A95664D2-9614-4F35-A746-DE8DB63617E6" |
| 50 |
MPG123_DEFINE_IID(IMMDeviceEnumerator, a95664d2, 9614, 4f35, a7, 46, de, 8d, b6, 36, 17, e6); |
| 51 |
// "BCDE0395-E52F-467C-8E3D-C4579291692E" |
| 52 |
MPG123_DEFINE_CLSID(IMMDeviceEnumerator, bcde0395, e52f, 467c, 8e, 3d, c4, 57, 92, 91, 69, 2e); |
| 53 |
// "F294ACFC-3146-4483-A7BF-ADDCA7C260E2" |
| 54 |
MPG123_DEFINE_IID(IAudioRenderClient, f294acfc, 3146, 4483, a7, bf, ad, dc, a7, c2, 60, e2); |
| 55 |
MPG123_DEFINE_PROPERTYKEY(Device_FriendlyName, a45c254e, df1c, 4efd, 80, 20, 67, d1, 46, a8, 50, e0, 14); |
| 56 |
#else |
| 57 |
#define mpg123_IID_IAudioClient IID_IAudioClient |
| 58 |
#define mpg123_IID_IMMDeviceEnumerator IID_IMMDeviceEnumerator |
| 59 |
#define mpg123_CLSID_IMMDeviceEnumerator CLSID_MMDeviceEnumerator |
| 60 |
#define mpg123_IID_IAudioRenderClient IID_IAudioRenderClient |
| 61 |
#define mpg123_PKEY_Device_FriendlyName PKEY_Device_FriendlyName |
| 62 |
#endif |
| 63 |
|
| 64 |
/* Push mode does not work right yet, noisy audio, probably something to do with timing and buffers */ |
| 65 |
#define MOD_STRING "Experimental Audio output for Windows" |
| 66 |
#define BUFFER_TIME 20000000.0 |
| 67 |
|
| 68 |
static int init_win32(out123_handle* ao); |
| 69 |
static void flush_win32(out123_handle *ao); |
| 70 |
/* |
| 71 |
Module information data structure |
| 72 |
*/ |
| 73 |
mpg123_module_t mpg123_output_module_info = { |
| 74 |
/* api_version */ MPG123_MODULE_API_VERSION, |
| 75 |
/* name */ "win32_wasapi", |
| 76 |
/* description */ MOD_STRING, |
| 77 |
/* revision */ "$Rev:$", |
| 78 |
/* handle */ NULL, |
| 79 |
/* init_output */ init_win32, |
| 80 |
}; |
| 81 |
|
| 82 |
// REFERENCE_TIME time units per second and per millisecond |
| 83 |
#define REFTIMES_PER_SEC 10000000 |
| 84 |
#define REFTIMES_PER_MILLISEC 10000 |
| 85 |
|
| 86 |
#define EXIT_ON_ERROR(hres) \ |
| 87 |
if (FAILED(hres)) { goto Exit; } |
| 88 |
|
| 89 |
/* todo: move into handle struct */ |
| 90 |
typedef struct _wasapi_state_struct { |
| 91 |
IMMDeviceEnumerator *pEnumerator; |
| 92 |
IMMDevice *pDevice; |
| 93 |
IAudioClient *pAudioClient; |
| 94 |
IAudioRenderClient *pRenderClient; |
| 95 |
BYTE *pData; |
| 96 |
UINT32 bufferFrameCount, numFramesAvailable; |
| 97 |
REFERENCE_TIME hnsRequestedDuration; |
| 98 |
HANDLE hEvent; |
| 99 |
HANDLE hTask; |
| 100 |
size_t pData_off; |
| 101 |
DWORD taskIndex; |
| 102 |
char is_playing; |
| 103 |
DWORD framesize; |
| 104 |
DWORD renderMode; |
| 105 |
char is_EventMode; |
| 106 |
} wasapi_state_struct; |
| 107 |
|
| 108 |
/* setup endpoints */ |
| 109 |
static int open_win32(out123_handle *ao){ |
| 110 |
HRESULT hr = 0; |
| 111 |
wchar_t *device = NULL; |
| 112 |
int devlen; |
| 113 |
wasapi_state_struct *state; |
| 114 |
|
| 115 |
debug1("%s",__FUNCTION__); |
| 116 |
if(!ao || ao->userptr) return -1; /* userptr should really be null */ |
| 117 |
state = calloc(sizeof(*state),1); |
| 118 |
|
| 119 |
if(!state) return -1; |
| 120 |
state->hnsRequestedDuration = REFTIMES_PER_SEC; |
| 121 |
ao->userptr = (void *)state; |
| 122 |
|
| 123 |
state->renderMode = AUDCLNT_SHAREMODE_SHARED; |
| 124 |
state->is_EventMode = 1; |
| 125 |
|
| 126 |
hr = CoCreateInstance(&mpg123_CLSID_IMMDeviceEnumerator,NULL,CLSCTX_ALL, &mpg123_IID_IMMDeviceEnumerator,(void**)&state->pEnumerator); |
| 127 |
mdebug("CoCreateInstance %x", hr); |
| 128 |
EXIT_ON_ERROR(hr) |
| 129 |
|
| 130 |
if (ao->device) { |
| 131 |
devlen = INT123_win32_utf8_wide(ao->device, &device, NULL); |
| 132 |
if(device && devlen > 0) { |
| 133 |
hr = IMMDeviceEnumerator_GetDevice(state->pEnumerator, device, &state->pDevice); |
| 134 |
mdebug("IMMDeviceEnumerator_GetDevice %x", hr); |
| 135 |
free(device); |
| 136 |
} else { |
| 137 |
debug("IMMDeviceEnumerator_GetDevice convert fail"); |
| 138 |
return -1; |
| 139 |
} |
| 140 |
} else { |
| 141 |
hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(state->pEnumerator,eRender, eConsole, &state->pDevice); |
| 142 |
mdebug("IMMDeviceEnumerator_GetDefaultAudioEndpoint %x", hr); |
| 143 |
} |
| 144 |
EXIT_ON_ERROR(hr) |
| 145 |
|
| 146 |
hr = IMMDeviceActivator_Activate(state->pDevice, |
| 147 |
&mpg123_IID_IAudioClient, CLSCTX_ALL, |
| 148 |
NULL, (void**)&state->pAudioClient); |
| 149 |
debug("IMMDeviceActivator_Activate"); |
| 150 |
EXIT_ON_ERROR(hr) |
| 151 |
|
| 152 |
return 0; |
| 153 |
Exit: |
| 154 |
mdebug("%s failed with %lx", __FUNCTION__, hr); |
| 155 |
return 1; |
| 156 |
} |
| 157 |
|
| 158 |
static int formats_generator(const out123_handle * const ao, const int waveformat, WAVEFORMATEX *const format){ |
| 159 |
DWORD bytes_per_sample = 0; |
| 160 |
WORD tag = WAVE_FORMAT_PCM; |
| 161 |
mdebug("%s",__FUNCTION__); |
| 162 |
int ret = waveformat; |
| 163 |
switch(waveformat){ |
| 164 |
case MPG123_ENC_ULAW_8: |
| 165 |
tag = WAVE_FORMAT_MULAW; |
| 166 |
bytes_per_sample = 1; |
| 167 |
break; |
| 168 |
case MPG123_ENC_ALAW_8: |
| 169 |
tag = WAVE_FORMAT_ALAW; |
| 170 |
bytes_per_sample = 1; |
| 171 |
break; |
| 172 |
case MPG123_ENC_SIGNED_8: |
| 173 |
bytes_per_sample = 1; |
| 174 |
break; |
| 175 |
case MPG123_ENC_FLOAT_32: |
| 176 |
tag = WAVE_FORMAT_IEEE_FLOAT; |
| 177 |
bytes_per_sample = 4; |
| 178 |
case MPG123_ENC_SIGNED_32: |
| 179 |
bytes_per_sample = 4; |
| 180 |
break; |
| 181 |
case MPG123_ENC_SIGNED_16: |
| 182 |
bytes_per_sample = 2; |
| 183 |
break; |
| 184 |
case MPG123_ENC_SIGNED_24: |
| 185 |
bytes_per_sample = 3; |
| 186 |
break; |
| 187 |
default: |
| 188 |
mdebug("uh oh unknown %x",waveformat); |
| 189 |
ret = 0; |
| 190 |
break; |
| 191 |
} |
| 192 |
format->wFormatTag = tag; |
| 193 |
format->nChannels = ao->channels; |
| 194 |
format->nSamplesPerSec = ao->rate; |
| 195 |
format->nAvgBytesPerSec = ao->channels * bytes_per_sample * ao->rate; |
| 196 |
format->nBlockAlign = ao->channels * bytes_per_sample; |
| 197 |
format->wBitsPerSample = bytes_per_sample * 8; |
| 198 |
format->cbSize = 0; |
| 199 |
return ret; |
| 200 |
} |
| 201 |
|
| 202 |
/* check supported formats */ |
| 203 |
typedef struct { |
| 204 |
unsigned int format; |
| 205 |
const char *name; |
| 206 |
} tknown_formats; |
| 207 |
|
| 208 |
static const tknown_formats known_formats[] = { |
| 209 |
{ .format = MPG123_ENC_SIGNED_8, .name = "MPG123_ENC_SIGNED_8" }, |
| 210 |
{ .format = MPG123_ENC_SIGNED_16, .name = "MPG123_ENC_SIGNED_16" }, |
| 211 |
{ .format = MPG123_ENC_SIGNED_24, .name = "MPG123_ENC_SIGNED_24" }, |
| 212 |
{ .format = MPG123_ENC_SIGNED_32, .name = "MPG123_ENC_SIGNED_32" }, |
| 213 |
{ .format = MPG123_ENC_FLOAT_32, .name = "MPG123_ENC_FLOAT_16" }, |
| 214 |
{ .format = MPG123_ENC_ALAW_8, .name = "MPG123_ENC_ALAW_8" }, |
| 215 |
{ .format = MPG123_ENC_ULAW_8, .name = "MPG123_ENC_ULAW_8" }, |
| 216 |
{ .format = 0, .name = NULL } |
| 217 |
}; |
| 218 |
|
| 219 |
static int get_formats_win32(out123_handle *ao){ |
| 220 |
/* PLEASE check with write_init and write_win32 buffer size calculation in case it is able to support something other than 16bit */ |
| 221 |
HRESULT hr; |
| 222 |
WAVEFORMATEX wf, *pClosestMatch = NULL; |
| 223 |
int ret = 0, i; |
| 224 |
mdebug("%s",__FUNCTION__); |
| 225 |
|
| 226 |
if(!ao || !ao->userptr) return -1; |
| 227 |
wasapi_state_struct *state = (wasapi_state_struct *) ao->userptr; |
| 228 |
mdebug("format %lx, channels %d, rate %ld",ao->format, ao->channels, ao->rate); |
| 229 |
|
| 230 |
for(i = 0; known_formats[i].name; i++) { |
| 231 |
if(ao->format & known_formats[i].format) { |
| 232 |
formats_generator(ao, known_formats[i].format, &wf); |
| 233 |
if((hr = IAudioClient_IsFormatSupported(state->pAudioClient, state->renderMode, &wf, &pClosestMatch)) == S_OK) { |
| 234 |
mdebug("OK format %s rate %ld supported", known_formats[i].name, ao->rate); |
| 235 |
ret |= known_formats[i].format; |
| 236 |
} else { |
| 237 |
if(hr & AUDCLNT_E_UNSUPPORTED_FORMAT) debug2("format %s rate %ld not supported", known_formats[i].name, ao->rate); |
| 238 |
if(pClosestMatch) { |
| 239 |
mdebug("Suggested: wFormatTag %x", pClosestMatch->wFormatTag); |
| 240 |
mdebug("Suggested: nChannels %u", pClosestMatch->nChannels); |
| 241 |
mdebug("Suggested: nSamplesPerSec %lu", pClosestMatch->nSamplesPerSec); |
| 242 |
mdebug("Suggested: nAvgBytesPerSec %lu", pClosestMatch->nAvgBytesPerSec); |
| 243 |
mdebug("Suggested: nBlockAlign %u", pClosestMatch->nBlockAlign); |
| 244 |
mdebug("Suggested: wBitsPerSample %u", pClosestMatch->wBitsPerSample); |
| 245 |
mdebug("Suggested: cbSize %u", pClosestMatch->cbSize); |
| 246 |
} |
| 247 |
} |
| 248 |
if(pClosestMatch) CoTaskMemFree(pClosestMatch); |
| 249 |
pClosestMatch = NULL; |
| 250 |
} |
| 251 |
} |
| 252 |
mdebug("supported format %lx, channels %d, rate %ld",ret, ao->channels, ao->rate); |
| 253 |
|
| 254 |
return ret; /* afaik only 16bit 44.1kHz/48kHz has been known to work */ |
| 255 |
} |
| 256 |
|
| 257 |
/* setup with agreed on format, for now only MPG123_ENC_SIGNED_16 */ |
| 258 |
static int write_init(out123_handle *ao){ |
| 259 |
HRESULT hr; |
| 260 |
double offset = 0.5; |
| 261 |
|
| 262 |
mdebug("%s",__FUNCTION__); |
| 263 |
if(!ao || !ao->userptr) return -1; |
| 264 |
wasapi_state_struct *state = (wasapi_state_struct *) ao->userptr; |
| 265 |
|
| 266 |
WAVEFORMATEX s16; |
| 267 |
formats_generator(ao,ao->format,&s16); |
| 268 |
state->framesize = s16.nBlockAlign; |
| 269 |
mdebug("block size %ld", state->framesize); |
| 270 |
/* cargo cult code */ |
| 271 |
hr = IAudioClient_GetDevicePeriod(state->pAudioClient,NULL, &state->hnsRequestedDuration); |
| 272 |
mdebug("IAudioClient_GetDevicePeriod %x", hr); |
| 273 |
reinit: |
| 274 |
hr = IAudioClient_Initialize(state->pAudioClient, |
| 275 |
state->renderMode, |
| 276 |
state->is_EventMode ? AUDCLNT_STREAMFLAGS_EVENTCALLBACK : 0, |
| 277 |
state->hnsRequestedDuration, |
| 278 |
state->renderMode == AUDCLNT_SHAREMODE_EXCLUSIVE ? state->hnsRequestedDuration : 0, |
| 279 |
&s16, |
| 280 |
NULL); |
| 281 |
mdebug("IAudioClient_Initialize %x", hr); |
| 282 |
if(hr == AUDCLNT_E_DEVICE_IN_USE){ |
| 283 |
debug("AUDCLNT_SHAREMODE_EXCLUSIVE not possible, another application is using IAudioClient"); |
| 284 |
} |
| 285 |
/* something about buffer sizes on Win7, fixme might loop forever */ |
| 286 |
if (hr == AUDCLNT_E_BUFFER_SIZE_NOT_ALIGNED){ |
| 287 |
if (offset > 10.0) goto Exit; /* is 10 enough to break out of the loop?*/ |
| 288 |
debug("AUDCLNT_E_BUFFER_SIZE_NOT_ALIGNED"); |
| 289 |
IAudioClient_GetBufferSize(state->pAudioClient,&state->bufferFrameCount); |
| 290 |
/* double buffered */ |
| 291 |
state->hnsRequestedDuration = (REFERENCE_TIME)((BUFFER_TIME / s16.nSamplesPerSec * state->bufferFrameCount) + offset); |
| 292 |
offset += 0.5; |
| 293 |
IAudioClient_Release(state->pAudioClient); |
| 294 |
state->pAudioClient = NULL; |
| 295 |
hr = IMMDeviceActivator_Activate(state->pDevice, |
| 296 |
&mpg123_IID_IAudioClient, CLSCTX_ALL, |
| 297 |
NULL, (void**)&state->pAudioClient); |
| 298 |
mdebug("IMMDeviceActivator_Activate %x", hr); |
| 299 |
goto reinit; |
| 300 |
} |
| 301 |
EXIT_ON_ERROR(hr) |
| 302 |
hr = IAudioClient_GetService(state->pAudioClient, |
| 303 |
&mpg123_IID_IAudioRenderClient, |
| 304 |
(void**)&state->pRenderClient); |
| 305 |
mdebug("IAudioClient_GetService %x", hr); |
| 306 |
EXIT_ON_ERROR(hr) |
| 307 |
if (state->is_EventMode) { |
| 308 |
state->hEvent = CreateEvent(NULL, FALSE, FALSE, NULL); |
| 309 |
mdebug("CreateEvent %p", state->hEvent); |
| 310 |
if(!state->hEvent) goto Exit; |
| 311 |
hr = IAudioClient_SetEventHandle(state->pAudioClient,state->hEvent); |
| 312 |
mdebug("IAudioClient_SetEventHandle %x", hr); |
| 313 |
EXIT_ON_ERROR(hr); |
| 314 |
} |
| 315 |
hr = IAudioClient_GetBufferSize(state->pAudioClient,&state->bufferFrameCount); |
| 316 |
mdebug("IAudioClient_GetBufferSize %x", hr); |
| 317 |
EXIT_ON_ERROR(hr) |
| 318 |
return 0; |
| 319 |
Exit: |
| 320 |
mdebug("%s failed with %lx", __FUNCTION__, hr); |
| 321 |
return 1; |
| 322 |
} |
| 323 |
|
| 324 |
/* Set play mode if unset, also raise thread priority */ |
| 325 |
static HRESULT play_init(out123_handle *ao){ |
| 326 |
HRESULT hr = S_OK; |
| 327 |
if(!ao || !ao->userptr) return -1; |
| 328 |
wasapi_state_struct *state = (wasapi_state_struct *) ao->userptr; |
| 329 |
if(!state->is_playing){ |
| 330 |
mdebug("%s",__FUNCTION__); |
| 331 |
state->hTask = AvSetMmThreadCharacteristicsW(L"Pro Audio", &state->taskIndex); |
| 332 |
hr = IAudioClient_Start(state->pAudioClient); |
| 333 |
mdebug("IAudioClient_Start %x", hr); |
| 334 |
EXIT_ON_ERROR(hr) |
| 335 |
state->is_playing = 1; |
| 336 |
} |
| 337 |
return hr; |
| 338 |
Exit: |
| 339 |
mdebug("%s failed with %lx", __FUNCTION__, hr); |
| 340 |
return hr; |
| 341 |
} |
| 342 |
|
| 343 |
/* copy audio into IAudioRenderClient provided buffer */ |
| 344 |
static int write_win32(out123_handle *ao, unsigned char *buf, int len){ |
| 345 |
HRESULT hr; |
| 346 |
UINT32 numFramesPadding = 0; |
| 347 |
size_t to_copy = 0; |
| 348 |
mdebug("%s",__FUNCTION__); |
| 349 |
if(!ao || !ao->userptr) return -1; |
| 350 |
wasapi_state_struct *state = (wasapi_state_struct *) ao->userptr; |
| 351 |
if(!len) return 0; |
| 352 |
if(!state->pRenderClient) write_init(ao); |
| 353 |
size_t frames_in = len/state->framesize; /* Frames in buf, is framesize even correct? */ |
| 354 |
mdebug("%s mode entered", state->is_EventMode ? "Event" : "Push"); |
| 355 |
|
| 356 |
if(state->is_EventMode) { |
| 357 |
/* Event mode WASAPI */ |
| 358 |
DWORD retval = -1; |
| 359 |
int flag = 0; /* Silence flag */ |
| 360 |
feed_again_event: |
| 361 |
if(!state->pData){ |
| 362 |
/* Acquire buffer */ |
| 363 |
hr = IAudioClient_GetCurrentPadding(state->pAudioClient,&numFramesPadding); |
| 364 |
mdebug("IAudioClient_GetCurrentPadding %x", hr); |
| 365 |
EXIT_ON_ERROR(hr) |
| 366 |
|
| 367 |
state->numFramesAvailable = state->bufferFrameCount - numFramesPadding; |
| 368 |
|
| 369 |
hr = IAudioRenderClient_GetBuffer(state->pRenderClient, state->numFramesAvailable, &state->pData); |
| 370 |
mdebug("IAudioRenderClient_GetBuffer %lu %x", state->numFramesAvailable, hr); |
| 371 |
EXIT_ON_ERROR(hr) |
| 372 |
} |
| 373 |
if(frames_in){ /* Did we get half a frame?? non-zero len smaller than framesize? */ |
| 374 |
/* We must put in exactly the amount of frames specified by IAudioRenderClient_GetBuffer */ |
| 375 |
mdebug("frames_in %"PRIu64", state->pData_off %u, numFramesAvailable %u", frames_in, state->pData_off, state->numFramesAvailable); |
| 376 |
while(state->pData_off < state->numFramesAvailable){ |
| 377 |
to_copy = state->numFramesAvailable - state->pData_off; |
| 378 |
mdebug("pData_off %"PRId64", numFramesAvailable %d, to_copy %"PRIu64, state->pData_off, state->numFramesAvailable, to_copy); |
| 379 |
if(to_copy > frames_in){ |
| 380 |
/* buf can fit in provided buffer space */ |
| 381 |
mdebug("all buffers copied, %"PRIu64, frames_in); |
| 382 |
memcpy(state->pData+state->pData_off*state->framesize,buf,state->framesize*(frames_in)); |
| 383 |
state->pData_off += frames_in; |
| 384 |
frames_in = 0; |
| 385 |
break; |
| 386 |
} else { |
| 387 |
/* buf too big, needs spliting */ |
| 388 |
mdebug("partial buffers %"PRIu64, to_copy); |
| 389 |
memcpy(state->pData+state->pData_off*state->framesize,buf,state->framesize*(to_copy)); |
| 390 |
state->pData_off += to_copy; |
| 391 |
buf+=(to_copy*state->framesize); |
| 392 |
frames_in -= to_copy; |
| 393 |
} |
| 394 |
} |
| 395 |
} else { |
| 396 |
debug("AUDCLNT_BUFFERFLAGS_SILENT"); |
| 397 |
/* In case we ever get half a frame, is it possible? */ |
| 398 |
flag = AUDCLNT_BUFFERFLAGS_SILENT; |
| 399 |
} |
| 400 |
mdebug("Copied %"PRIu64", left %"PRIu64, state->pData_off, frames_in); |
| 401 |
if(state->pData_off == state->numFramesAvailable) { |
| 402 |
/* Tell IAudioRenderClient that buffer is filled and released */ |
| 403 |
hr = IAudioRenderClient_ReleaseBuffer(state->pRenderClient,state->pData_off, flag); |
| 404 |
state->pData_off = 0; |
| 405 |
state->pData = NULL; |
| 406 |
debug("IAudioRenderClient_ReleaseBuffer"); |
| 407 |
EXIT_ON_ERROR(hr) |
| 408 |
if(!state->is_playing){ |
| 409 |
hr = play_init(ao); |
| 410 |
EXIT_ON_ERROR(hr) |
| 411 |
} |
| 412 |
/* wait for next pull event */ |
| 413 |
retval = WaitForSingleObject(state->hEvent, 2000); |
| 414 |
if (retval != WAIT_OBJECT_0){ |
| 415 |
/* Event handle timed out after a 2-second wait, something went very wrong */ |
| 416 |
IAudioClient_Stop(state->pAudioClient); |
| 417 |
hr = ERROR_TIMEOUT; |
| 418 |
goto Exit; |
| 419 |
} |
| 420 |
} |
| 421 |
if(frames_in > 0) |
| 422 |
goto feed_again_event; |
| 423 |
} else { /* PUSH mode code */ |
| 424 |
mdebug("block size %"PRIu64", %d len, %"PRIu64" number of frames", state->framesize, len, frames_in); |
| 425 |
feed_again_push: |
| 426 |
/* How much buffer do we get to use? */ |
| 427 |
hr = IAudioClient_GetBufferSize(state->pAudioClient,&state->bufferFrameCount); |
| 428 |
mdebug("IAudioClient_GetBufferSize %x", hr); |
| 429 |
EXIT_ON_ERROR(hr) |
| 430 |
hr = IAudioClient_GetCurrentPadding(state->pAudioClient,&numFramesPadding); |
| 431 |
mdebug("IAudioClient_GetCurrentPadding %x", hr); |
| 432 |
EXIT_ON_ERROR(hr) |
| 433 |
/* How much buffer is writable at the moment? */ |
| 434 |
state->numFramesAvailable = state->bufferFrameCount - numFramesPadding; |
| 435 |
mdebug("numFramesAvailable %d, bufferFrameCount %d, numFramesPadding %d", state->numFramesAvailable, state->bufferFrameCount, numFramesPadding); |
| 436 |
|
| 437 |
if(state->numFramesAvailable > frames_in){ |
| 438 |
/* can fit all frames now */ |
| 439 |
state->pData_off = 0; |
| 440 |
to_copy = frames_in; |
| 441 |
} else { |
| 442 |
/* copy whatever that fits in the buffer */ |
| 443 |
state->pData_off = frames_in - state->numFramesAvailable; |
| 444 |
to_copy = state->numFramesAvailable; |
| 445 |
} |
| 446 |
|
| 447 |
/* Acquire buffer */ |
| 448 |
hr = IAudioRenderClient_GetBuffer(state->pRenderClient, to_copy, &state->pData); |
| 449 |
mdebug("IAudioRenderClient_GetBuffer %"PRIu64" frames", to_copy); |
| 450 |
EXIT_ON_ERROR(hr) |
| 451 |
|
| 452 |
/* Copy buffer */ |
| 453 |
memcpy(state->pData, buf + state->pData_off * state->framesize, to_copy * state->framesize); |
| 454 |
|
| 455 |
/* Release buffer */ |
| 456 |
hr = IAudioRenderClient_ReleaseBuffer(state->pRenderClient, to_copy, 0); |
| 457 |
mdebug("IAudioRenderClient_ReleaseBuffer %x", hr); |
| 458 |
|
| 459 |
EXIT_ON_ERROR(hr) |
| 460 |
if(!state->is_playing){ |
| 461 |
hr = play_init(ao); |
| 462 |
EXIT_ON_ERROR(hr) |
| 463 |
} |
| 464 |
frames_in -= to_copy; |
| 465 |
/* Wait sometime for buffer to empty? */ |
| 466 |
DWORD sleeptime = ((double)REFTIMES_PER_SEC) * to_copy / ao->rate / REFTIMES_PER_MILLISEC / 2; |
| 467 |
mdebug("Sleeping %ld msec", sleeptime); |
| 468 |
Sleep(sleeptime); |
| 469 |
if (frames_in > 0) |
| 470 |
goto feed_again_push; |
| 471 |
} |
| 472 |
return len; |
| 473 |
Exit: |
| 474 |
mdebug("%s failed with %lx", __FUNCTION__, hr); |
| 475 |
return -1; |
| 476 |
} |
| 477 |
|
| 478 |
static void flush_win32(out123_handle *ao){ |
| 479 |
/* Wait for the last buffer to play before stopping. */ |
| 480 |
mdebug("%s",__FUNCTION__); |
| 481 |
if(!ao || !ao->userptr) return; |
| 482 |
wasapi_state_struct *state = (wasapi_state_struct *) ao->userptr; |
| 483 |
HRESULT hr; |
| 484 |
if(!state->pAudioClient) return; |
| 485 |
state->pData = NULL; |
| 486 |
hr = IAudioClient_Stop(state->pAudioClient); |
| 487 |
EXIT_ON_ERROR(hr) |
| 488 |
IAudioClient_Reset(state->pAudioClient); |
| 489 |
EXIT_ON_ERROR(hr) |
| 490 |
return; |
| 491 |
Exit: |
| 492 |
mdebug("%s IAudioClient_Stop with %lx", __FUNCTION__, hr); |
| 493 |
} |
| 494 |
|
| 495 |
static int close_win32(out123_handle *ao) |
| 496 |
{ |
| 497 |
mdebug("%s",__FUNCTION__); |
| 498 |
if(!ao || !ao->userptr) return -1; |
| 499 |
wasapi_state_struct *state = (wasapi_state_struct *) ao->userptr; |
| 500 |
if(state->is_EventMode && state->pData){ |
| 501 |
/* Play all in buffer before closing */ |
| 502 |
debug("Flushing remaining buffers"); |
| 503 |
IAudioRenderClient_ReleaseBuffer(state->pRenderClient,state->bufferFrameCount, 0); |
| 504 |
WaitForSingleObject(state->hEvent, 2000); |
| 505 |
state->pData = NULL; |
| 506 |
} |
| 507 |
if(state->pAudioClient) IAudioClient_Stop(state->pAudioClient); |
| 508 |
if(state->pRenderClient) IAudioRenderClient_Release(state->pRenderClient); |
| 509 |
if(state->pAudioClient) IAudioClient_Release(state->pAudioClient); |
| 510 |
if(state->hTask) AvRevertMmThreadCharacteristics(state->hTask); |
| 511 |
if(state->pEnumerator) IMMDeviceEnumerator_Release(state->pEnumerator); |
| 512 |
if(state->pDevice) IMMDevice_Release(state->pDevice); |
| 513 |
free(state); |
| 514 |
ao->userptr = NULL; |
| 515 |
return 0; |
| 516 |
} |
| 517 |
|
| 518 |
static int enumerate_win32( out123_handle *ao, int (*store_device)(void *devlist |
| 519 |
, const char *name, const char *description), void *devlist ) |
| 520 |
{ |
| 521 |
char *pszID = NULL, *pszDesc = NULL; |
| 522 |
HRESULT hr = S_OK; |
| 523 |
UINT pcDevices = 0, i = 0; |
| 524 |
LPWSTR pwszID = NULL; |
| 525 |
IMMDeviceEnumerator *pEnumerator = NULL; |
| 526 |
IMMDeviceCollection *pCollection = NULL; |
| 527 |
IPropertyStore *pProps = NULL; |
| 528 |
IMMDevice *pDevice = NULL; |
| 529 |
PROPVARIANT varName; |
| 530 |
|
| 531 |
hr = CoCreateInstance(&mpg123_CLSID_IMMDeviceEnumerator,NULL,CLSCTX_ALL, &mpg123_IID_IMMDeviceEnumerator,(void**)&pEnumerator); |
| 532 |
if(FAILED(hr) || pEnumerator == NULL) goto Exit; |
| 533 |
hr = IMMDeviceEnumerator_EnumAudioEndpoints(pEnumerator, eRender, DEVICE_STATE_ACTIVE, &pCollection); |
| 534 |
if(FAILED(hr) || pCollection == NULL) goto Exit; |
| 535 |
hr = IMMDeviceCollection_GetCount(pCollection, &pcDevices); |
| 536 |
EXIT_ON_ERROR(hr) |
| 537 |
|
| 538 |
for(i = 0; i < pcDevices; i++) { |
| 539 |
/* init */ |
| 540 |
hr = IMMDeviceCollection_Item(pCollection, i, &pDevice); |
| 541 |
if(FAILED(hr) || pDevice == NULL) goto Exit; |
| 542 |
hr = IMMDevice_GetId(pDevice, &pwszID); |
| 543 |
if(FAILED(hr) || pwszID == NULL) goto Exit; |
| 544 |
hr = IMMDevice_OpenPropertyStore(pDevice, STGM_READ, &pProps); |
| 545 |
if(FAILED(hr) || pProps == NULL) goto Exit; |
| 546 |
|
| 547 |
/* get ID */ |
| 548 |
INT123_win32_wide_utf8(pwszID, &pszID, NULL); |
| 549 |
if(pszID == NULL) goto Exit; |
| 550 |
|
| 551 |
/* get Property */ |
| 552 |
PropVariantInit(&varName); |
| 553 |
hr = IPropertyStore_GetValue(pProps, &mpg123_PKEY_Device_FriendlyName, &varName); |
| 554 |
if(FAILED(hr)) { |
| 555 |
PropVariantClear(&varName); |
| 556 |
goto Exit; |
| 557 |
} |
| 558 |
|
| 559 |
/* get Description*/ |
| 560 |
INT123_win32_wide_utf8(varName.pwszVal, &pszDesc, NULL); |
| 561 |
PropVariantClear(&varName); |
| 562 |
if(pszDesc == NULL) goto Exit; |
| 563 |
|
| 564 |
/* store */ |
| 565 |
if(store_device(devlist, pszID, pszDesc)) |
| 566 |
goto Exit; |
| 567 |
|
| 568 |
/* release */ |
| 569 |
free(pszID); |
| 570 |
pszID = NULL; |
| 571 |
free(pszDesc); |
| 572 |
pszDesc = NULL; |
| 573 |
CoTaskMemFree(pwszID); |
| 574 |
pwszID = NULL; |
| 575 |
IMMDeviceEnumerator_Release(pDevice); |
| 576 |
} |
| 577 |
|
| 578 |
IMMDeviceCollection_Release(pCollection); |
| 579 |
IMMDeviceEnumerator_Release(pEnumerator); |
| 580 |
return 0; |
| 581 |
|
| 582 |
Exit: |
| 583 |
if(pszDesc) free(pszDesc); |
| 584 |
if(pszID) free(pszID); |
| 585 |
if(pwszID) CoTaskMemFree(pwszID); |
| 586 |
if(pProps) IPropertyStore_Release(pProps); |
| 587 |
if(pDevice) IMMDeviceEnumerator_Release(pDevice); |
| 588 |
if(pCollection) IMMDeviceCollection_Release(pCollection); |
| 589 |
if(pEnumerator) IMMDeviceEnumerator_Release(pEnumerator); |
| 590 |
return -1; |
| 591 |
} |
| 592 |
static void deinit_win32(out123_handle* ao) |
| 593 |
{ |
| 594 |
/* Deinitialize things from init_win32(). */ |
| 595 |
CoUninitialize(); |
| 596 |
} |
| 597 |
|
| 598 |
static int init_win32(out123_handle* ao){ |
| 599 |
HRESULT hr; |
| 600 |
mdebug("%s",__FUNCTION__); |
| 601 |
if(!ao) return -1; |
| 602 |
|
| 603 |
hr = CoInitializeEx(NULL, COINIT_MULTITHREADED); |
| 604 |
mdebug("CoInitializeEx %x", hr); |
| 605 |
if(!(hr == S_OK || hr == S_FALSE)) |
| 606 |
return -1; |
| 607 |
|
| 608 |
/* Set callbacks */ |
| 609 |
ao->open = open_win32; |
| 610 |
ao->flush = flush_win32; |
| 611 |
ao->write = write_win32; |
| 612 |
ao->get_formats = get_formats_win32; |
| 613 |
ao->close = close_win32; |
| 614 |
ao->userptr = NULL; |
| 615 |
ao->enumerate = enumerate_win32; |
| 616 |
ao->deinit = deinit_win32; |
| 617 |
|
| 618 |
/* Success */ |
| 619 |
return 0; |
| 620 |
} |