2
* OpenAL cross platform audio library
3
* Copyright (C) 2011 by authors.
4
* This library is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU Library General Public
6
* License as published by the Free Software Foundation; either
7
* version 2 of the License, or (at your option) any later version.
9
* This library is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
* Library General Public License for more details.
14
* You should have received a copy of the GNU Library General Public
15
* License along with this library; if not, write to the
16
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17
* Boston, MA 02111-1307, USA.
18
* Or go to http://www.gnu.org/copyleft/lgpl.html
28
#include <mmdeviceapi.h>
29
#include <audioclient.h>
35
#ifndef _WAVEFORMATEXTENSIBLE_
45
DEFINE_GUID(KSDATAFORMAT_SUBTYPE_PCM, 0x00000001, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71);
46
DEFINE_GUID(KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, 0x00000003, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71);
48
#define MONO SPEAKER_FRONT_CENTER
49
#define STEREO (SPEAKER_FRONT_LEFT|SPEAKER_FRONT_RIGHT)
50
#define QUAD (SPEAKER_FRONT_LEFT|SPEAKER_FRONT_RIGHT|SPEAKER_BACK_LEFT|SPEAKER_BACK_RIGHT)
51
#define X5DOT1 (SPEAKER_FRONT_LEFT|SPEAKER_FRONT_RIGHT|SPEAKER_FRONT_CENTER|SPEAKER_LOW_FREQUENCY|SPEAKER_BACK_LEFT|SPEAKER_BACK_RIGHT)
52
#define X5DOT1SIDE (SPEAKER_FRONT_LEFT|SPEAKER_FRONT_RIGHT|SPEAKER_FRONT_CENTER|SPEAKER_LOW_FREQUENCY|SPEAKER_SIDE_LEFT|SPEAKER_SIDE_RIGHT)
53
#define X6DOT1 (SPEAKER_FRONT_LEFT|SPEAKER_FRONT_RIGHT|SPEAKER_FRONT_CENTER|SPEAKER_LOW_FREQUENCY|SPEAKER_BACK_CENTER|SPEAKER_SIDE_LEFT|SPEAKER_SIDE_RIGHT)
54
#define X7DOT1 (SPEAKER_FRONT_LEFT|SPEAKER_FRONT_RIGHT|SPEAKER_FRONT_CENTER|SPEAKER_LOW_FREQUENCY|SPEAKER_BACK_LEFT|SPEAKER_BACK_RIGHT|SPEAKER_SIDE_LEFT|SPEAKER_SIDE_RIGHT)
62
IAudioRenderClient *render;
77
static DevMap *PlaybackDeviceList;
78
static ALuint NumPlaybackDevices;
79
static DevMap *CaptureDeviceList;
80
static ALuint NumCaptureDevices;
83
static HANDLE ThreadHdl;
84
static DWORD ThreadID;
91
#define WM_USER_OpenDevice (WM_USER+0)
92
#define WM_USER_ResetDevice (WM_USER+1)
93
#define WM_USER_StartDevice (WM_USER+2)
94
#define WM_USER_StopDevice (WM_USER+3)
95
#define WM_USER_CloseDevice (WM_USER+4)
96
#define WM_USER_Enumerate (WM_USER+5)
98
static HRESULT WaitForResponse(ThreadRequest *req)
100
if(WaitForSingleObject(req->FinishedEvt, INFINITE) == WAIT_OBJECT_0)
102
ERR("Message response error: %lu\n", GetLastError());
107
static ALCchar *get_device_name(IMMDevice *device)
109
ALCchar *name = NULL;
115
hr = IMMDevice_OpenPropertyStore(device, STGM_READ, &ps);
118
WARN("OpenPropertyStore failed: 0x%08lx\n", hr);
122
PropVariantInit(&pvname);
124
hr = IPropertyStore_GetValue(ps, (const PROPERTYKEY*)&DEVPKEY_Device_FriendlyName, &pvname);
127
WARN("GetValue failed: 0x%08lx\n", hr);
132
if((len=WideCharToMultiByte(CP_ACP, 0, pvname.pwszVal, -1, NULL, 0, NULL, NULL)) > 0)
134
name = calloc(1, len);
135
WideCharToMultiByte(CP_ACP, 0, pvname.pwszVal, -1, name, len, NULL, NULL);
139
PropVariantClear(&pvname);
140
IPropertyStore_Release(ps);
145
static void add_device(IMMDevice *device, DevMap *devmap)
150
hr = IMMDevice_GetId(device, &devid);
153
devmap->devid = strdupW(devid);
154
devmap->name = get_device_name(device);
155
TRACE("Got device \"%s\", \"%ls\"\n", devmap->name, devmap->devid);
156
CoTaskMemFree(devid);
160
static DevMap *ProbeDevices(IMMDeviceEnumerator *devenum, EDataFlow flowdir, ALuint *numdevs)
162
IMMDeviceCollection *coll;
163
IMMDevice *defdev = NULL;
164
DevMap *devlist = NULL;
170
hr = IMMDeviceEnumerator_EnumAudioEndpoints(devenum, flowdir, DEVICE_STATE_ACTIVE, &coll);
173
ERR("Failed to enumerate audio endpoints: 0x%08lx\n", hr);
178
hr = IMMDeviceCollection_GetCount(coll, &count);
179
if(SUCCEEDED(hr) && count > 0)
181
devlist = calloc(count, sizeof(*devlist));
184
IMMDeviceCollection_Release(coll);
188
hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(devenum, flowdir,
189
eMultimedia, &defdev);
191
if(SUCCEEDED(hr) && defdev != NULL)
192
add_device(defdev, &devlist[idx++]);
194
for(i = 0;i < count && idx < count;++i)
198
if(FAILED(IMMDeviceCollection_Item(coll, i, &device)))
202
add_device(device, &devlist[idx++]);
204
IMMDevice_Release(device);
207
if(defdev) IMMDevice_Release(defdev);
208
IMMDeviceCollection_Release(coll);
215
static ALuint MMDevApiProc(ALvoid *ptr)
217
ALCdevice *device = ptr;
218
MMDevApiData *data = device->ExtraData;
219
UINT32 buffer_len, written;
220
ALuint update_size, len;
224
hr = CoInitialize(NULL);
227
ERR("CoInitialize(NULL) failed: 0x%08lx\n", hr);
228
aluHandleDisconnect(device);
232
hr = IAudioClient_GetBufferSize(data->client, &buffer_len);
235
ERR("Failed to get audio buffer size: 0x%08lx\n", hr);
236
aluHandleDisconnect(device);
243
update_size = device->UpdateSize;
244
while(!data->killNow)
246
hr = IAudioClient_GetCurrentPadding(data->client, &written);
249
ERR("Failed to get padding: 0x%08lx\n", hr);
250
aluHandleDisconnect(device);
254
len = buffer_len - written;
255
if(len < update_size)
258
res = WaitForSingleObjectEx(data->hNotifyEvent, 2000, FALSE);
259
if(res != WAIT_OBJECT_0)
260
ERR("WaitForSingleObjectEx error: 0x%lx\n", res);
263
len -= len%update_size;
265
hr = IAudioRenderClient_GetBuffer(data->render, len, &buffer);
268
aluMixData(device, buffer, len);
269
hr = IAudioRenderClient_ReleaseBuffer(data->render, len, 0);
273
ERR("Failed to buffer data: 0x%08lx\n", hr);
274
aluHandleDisconnect(device);
284
static ALCboolean MakeExtensible(WAVEFORMATEXTENSIBLE *out, const WAVEFORMATEX *in)
286
memset(out, 0, sizeof(*out));
287
if(in->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
288
*out = *(const WAVEFORMATEXTENSIBLE*)in;
289
else if(in->wFormatTag == WAVE_FORMAT_PCM)
292
out->Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
293
out->Format.cbSize = sizeof(*out) - sizeof(*in);
294
if(out->Format.nChannels == 1)
295
out->dwChannelMask = MONO;
296
else if(out->Format.nChannels == 2)
297
out->dwChannelMask = STEREO;
299
ERR("Unhandled PCM channel count: %d\n", out->Format.nChannels);
300
out->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
302
else if(in->wFormatTag == WAVE_FORMAT_IEEE_FLOAT)
305
out->Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
306
out->Format.cbSize = sizeof(*out) - sizeof(*in);
307
if(out->Format.nChannels == 1)
308
out->dwChannelMask = MONO;
309
else if(out->Format.nChannels == 2)
310
out->dwChannelMask = STEREO;
312
ERR("Unhandled IEEE float channel count: %d\n", out->Format.nChannels);
313
out->SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
317
ERR("Unhandled format tag: 0x%04x\n", in->wFormatTag);
323
static HRESULT DoReset(ALCdevice *device)
325
MMDevApiData *data = device->ExtraData;
326
WAVEFORMATEXTENSIBLE OutputType;
327
WAVEFORMATEX *wfx = NULL;
328
REFERENCE_TIME min_per, buf_time;
329
UINT32 buffer_len, min_len;
332
hr = IAudioClient_GetMixFormat(data->client, &wfx);
335
ERR("Failed to get mix format: 0x%08lx\n", hr);
339
if(!MakeExtensible(&OutputType, wfx))
347
buf_time = ((REFERENCE_TIME)device->UpdateSize*device->NumUpdates*10000000 +
348
device->Frequency-1) / device->Frequency;
350
if(!(device->Flags&DEVICE_FREQUENCY_REQUEST))
351
device->Frequency = OutputType.Format.nSamplesPerSec;
352
if(!(device->Flags&DEVICE_CHANNELS_REQUEST))
354
if(OutputType.Format.nChannels == 1 && OutputType.dwChannelMask == MONO)
355
device->FmtChans = DevFmtMono;
356
else if(OutputType.Format.nChannels == 2 && OutputType.dwChannelMask == STEREO)
357
device->FmtChans = DevFmtStereo;
358
else if(OutputType.Format.nChannels == 4 && OutputType.dwChannelMask == QUAD)
359
device->FmtChans = DevFmtQuad;
360
else if(OutputType.Format.nChannels == 6 && OutputType.dwChannelMask == X5DOT1)
361
device->FmtChans = DevFmtX51;
362
else if(OutputType.Format.nChannels == 6 && OutputType.dwChannelMask == X5DOT1SIDE)
363
device->FmtChans = DevFmtX51Side;
364
else if(OutputType.Format.nChannels == 7 && OutputType.dwChannelMask == X6DOT1)
365
device->FmtChans = DevFmtX61;
366
else if(OutputType.Format.nChannels == 8 && OutputType.dwChannelMask == X7DOT1)
367
device->FmtChans = DevFmtX71;
369
ERR("Unhandled channel config: %d -- 0x%08lx\n", OutputType.Format.nChannels, OutputType.dwChannelMask);
372
switch(device->FmtChans)
375
OutputType.Format.nChannels = 1;
376
OutputType.dwChannelMask = MONO;
379
OutputType.Format.nChannels = 2;
380
OutputType.dwChannelMask = STEREO;
383
OutputType.Format.nChannels = 4;
384
OutputType.dwChannelMask = QUAD;
387
OutputType.Format.nChannels = 6;
388
OutputType.dwChannelMask = X5DOT1;
391
OutputType.Format.nChannels = 6;
392
OutputType.dwChannelMask = X5DOT1SIDE;
395
OutputType.Format.nChannels = 7;
396
OutputType.dwChannelMask = X6DOT1;
399
OutputType.Format.nChannels = 8;
400
OutputType.dwChannelMask = X7DOT1;
403
switch(device->FmtType)
406
device->FmtType = DevFmtUByte;
409
OutputType.Format.wBitsPerSample = 8;
410
OutputType.Samples.wValidBitsPerSample = 8;
411
OutputType.SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
414
device->FmtType = DevFmtShort;
417
OutputType.Format.wBitsPerSample = 16;
418
OutputType.Samples.wValidBitsPerSample = 16;
419
OutputType.SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
422
device->FmtType = DevFmtInt;
425
OutputType.Format.wBitsPerSample = 32;
426
OutputType.Samples.wValidBitsPerSample = 32;
427
OutputType.SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
430
OutputType.Format.wBitsPerSample = 32;
431
OutputType.Samples.wValidBitsPerSample = 32;
432
OutputType.SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
435
OutputType.Format.nSamplesPerSec = device->Frequency;
437
OutputType.Format.nBlockAlign = OutputType.Format.nChannels *
438
OutputType.Format.wBitsPerSample / 8;
439
OutputType.Format.nAvgBytesPerSec = OutputType.Format.nSamplesPerSec *
440
OutputType.Format.nBlockAlign;
442
hr = IAudioClient_IsFormatSupported(data->client, AUDCLNT_SHAREMODE_SHARED, &OutputType.Format, &wfx);
445
ERR("Failed to check format support: 0x%08lx\n", hr);
446
hr = IAudioClient_GetMixFormat(data->client, &wfx);
450
ERR("Failed to find a supported format: 0x%08lx\n", hr);
456
if(!MakeExtensible(&OutputType, wfx))
464
device->Frequency = OutputType.Format.nSamplesPerSec;
465
if(OutputType.Format.nChannels == 1 && OutputType.dwChannelMask == MONO)
466
device->FmtChans = DevFmtMono;
467
else if(OutputType.Format.nChannels == 2 && OutputType.dwChannelMask == STEREO)
468
device->FmtChans = DevFmtStereo;
469
else if(OutputType.Format.nChannels == 4 && OutputType.dwChannelMask == QUAD)
470
device->FmtChans = DevFmtQuad;
471
else if(OutputType.Format.nChannels == 6 && OutputType.dwChannelMask == X5DOT1)
472
device->FmtChans = DevFmtX51;
473
else if(OutputType.Format.nChannels == 6 && OutputType.dwChannelMask == X5DOT1SIDE)
474
device->FmtChans = DevFmtX51Side;
475
else if(OutputType.Format.nChannels == 7 && OutputType.dwChannelMask == X6DOT1)
476
device->FmtChans = DevFmtX61;
477
else if(OutputType.Format.nChannels == 8 && OutputType.dwChannelMask == X7DOT1)
478
device->FmtChans = DevFmtX71;
481
ERR("Unhandled extensible channels: %d -- 0x%08lx\n", OutputType.Format.nChannels, OutputType.dwChannelMask);
482
device->FmtChans = DevFmtStereo;
483
OutputType.Format.nChannels = 2;
484
OutputType.dwChannelMask = STEREO;
487
if(IsEqualGUID(&OutputType.SubFormat, &KSDATAFORMAT_SUBTYPE_PCM))
489
if(OutputType.Format.wBitsPerSample == 8)
490
device->FmtType = DevFmtUByte;
491
else if(OutputType.Format.wBitsPerSample == 16)
492
device->FmtType = DevFmtShort;
493
else if(OutputType.Format.wBitsPerSample == 32)
494
device->FmtType = DevFmtInt;
497
device->FmtType = DevFmtShort;
498
OutputType.Format.wBitsPerSample = 16;
501
else if(IsEqualGUID(&OutputType.SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT))
503
device->FmtType = DevFmtFloat;
504
OutputType.Format.wBitsPerSample = 32;
508
ERR("Unhandled format sub-type\n");
509
device->FmtType = DevFmtShort;
510
OutputType.Format.wBitsPerSample = 16;
511
OutputType.SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
513
OutputType.Samples.wValidBitsPerSample = OutputType.Format.wBitsPerSample;
516
SetDefaultWFXChannelOrder(device);
518
hr = IAudioClient_Initialize(data->client, AUDCLNT_SHAREMODE_SHARED,
519
AUDCLNT_STREAMFLAGS_EVENTCALLBACK,
520
buf_time, 0, &OutputType.Format, NULL);
523
ERR("Failed to initialize audio client: 0x%08lx\n", hr);
527
hr = IAudioClient_GetDevicePeriod(data->client, &min_per, NULL);
530
min_len = (UINT32)((min_per*device->Frequency + 10000000-1) / 10000000);
531
/* Find the nearest multiple of the period size to the update size */
532
if(min_len < device->UpdateSize)
533
min_len *= (device->UpdateSize + min_len/2)/min_len;
534
hr = IAudioClient_GetBufferSize(data->client, &buffer_len);
538
ERR("Failed to get audio buffer info: 0x%08lx\n", hr);
542
device->UpdateSize = min_len;
543
device->NumUpdates = buffer_len / device->UpdateSize;
544
if(device->NumUpdates <= 1)
546
ERR("Audio client returned buffer_len < period*2; expect break up\n");
547
device->NumUpdates = 2;
548
device->UpdateSize = buffer_len / device->NumUpdates;
555
static DWORD CALLBACK MMDevApiMsgProc(void *ptr)
557
ThreadRequest *req = ptr;
558
IMMDeviceEnumerator *Enumerator;
559
ALuint deviceCount = 0;
565
TRACE("Starting message thread\n");
567
cohr = CoInitialize(NULL);
570
WARN("Failed to initialize COM: 0x%08lx\n", cohr);
572
SetEvent(req->FinishedEvt);
576
hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL, CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, &ptr);
579
WARN("Failed to create IMMDeviceEnumerator instance: 0x%08lx\n", hr);
582
SetEvent(req->FinishedEvt);
586
IMMDeviceEnumerator_Release(Enumerator);
592
SetEvent(req->FinishedEvt);
594
TRACE("Starting message loop\n");
595
while(GetMessage(&msg, NULL, 0, 0))
597
TRACE("Got message %u\n", msg.message);
600
case WM_USER_OpenDevice:
601
req = (ThreadRequest*)msg.wParam;
602
device = (ALCdevice*)msg.lParam;
603
data = device->ExtraData;
606
if(++deviceCount == 1)
607
hr = cohr = CoInitialize(NULL);
609
hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL, CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, &ptr);
614
hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(Enumerator, eRender, eMultimedia, &data->mmdev);
616
hr = IMMDeviceEnumerator_GetDevice(Enumerator, data->devid, &data->mmdev);
617
IMMDeviceEnumerator_Release(Enumerator);
621
hr = IMMDevice_Activate(data->mmdev, &IID_IAudioClient, CLSCTX_INPROC_SERVER, NULL, &ptr);
625
device->szDeviceName = get_device_name(data->mmdev);
631
IMMDevice_Release(data->mmdev);
633
if(--deviceCount == 0 && SUCCEEDED(cohr))
638
SetEvent(req->FinishedEvt);
641
case WM_USER_ResetDevice:
642
req = (ThreadRequest*)msg.wParam;
643
device = (ALCdevice*)msg.lParam;
645
req->result = DoReset(device);
646
SetEvent(req->FinishedEvt);
649
case WM_USER_StartDevice:
650
req = (ThreadRequest*)msg.wParam;
651
device = (ALCdevice*)msg.lParam;
652
data = device->ExtraData;
654
ResetEvent(data->hNotifyEvent);
655
hr = IAudioClient_SetEventHandle(data->client, data->hNotifyEvent);
657
ERR("Failed to set event handle: 0x%08lx\n", hr);
660
hr = IAudioClient_Start(data->client);
662
ERR("Failed to start audio client: 0x%08lx\n", hr);
666
hr = IAudioClient_GetService(data->client, &IID_IAudioRenderClient, &ptr);
670
data->thread = StartThread(MMDevApiProc, device);
674
IAudioRenderClient_Release(data->render);
676
IAudioClient_Stop(data->client);
677
ERR("Failed to start thread\n");
683
SetEvent(req->FinishedEvt);
686
case WM_USER_StopDevice:
687
req = (ThreadRequest*)msg.wParam;
688
device = (ALCdevice*)msg.lParam;
689
data = device->ExtraData;
694
StopThread(data->thread);
699
IAudioRenderClient_Release(data->render);
701
IAudioClient_Stop(data->client);
705
SetEvent(req->FinishedEvt);
708
case WM_USER_CloseDevice:
709
req = (ThreadRequest*)msg.wParam;
710
device = (ALCdevice*)msg.lParam;
711
data = device->ExtraData;
713
IAudioClient_Release(data->client);
716
IMMDevice_Release(data->mmdev);
719
if(--deviceCount == 0)
723
SetEvent(req->FinishedEvt);
726
case WM_USER_Enumerate:
727
req = (ThreadRequest*)msg.wParam;
730
if(++deviceCount == 1)
731
hr = cohr = CoInitialize(NULL);
733
hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL, CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, &ptr);
742
if(msg.lParam == CAPTURE_DEVICE_PROBE)
745
devlist = &CaptureDeviceList;
746
numdevs = &NumCaptureDevices;
751
devlist = &PlaybackDeviceList;
752
numdevs = &NumPlaybackDevices;
755
for(i = 0;i < *numdevs;i++)
757
free((*devlist)[i].name);
758
free((*devlist)[i].devid);
764
*devlist = ProbeDevices(Enumerator, flowdir, numdevs);
766
IMMDeviceEnumerator_Release(Enumerator);
770
if(--deviceCount == 0 && SUCCEEDED(cohr))
774
SetEvent(req->FinishedEvt);
778
ERR("Unexpected message: %u\n", msg.message);
782
TRACE("Message loop finished\n");
788
static BOOL MMDevApiLoad(void)
790
static HRESULT InitResult;
796
req.FinishedEvt = CreateEvent(NULL, FALSE, FALSE, NULL);
797
if(req.FinishedEvt == NULL)
798
ERR("Failed to create event: %lu\n", GetLastError());
801
ThreadHdl = CreateThread(NULL, 0, MMDevApiMsgProc, &req, 0, &ThreadID);
802
if(ThreadHdl != NULL)
803
InitResult = WaitForResponse(&req);
804
CloseHandle(req.FinishedEvt);
807
return SUCCEEDED(InitResult);
811
static ALCenum MMDevApiOpenPlayback(ALCdevice *device, const ALCchar *deviceName)
813
MMDevApiData *data = NULL;
816
//Initialise requested device
817
data = calloc(1, sizeof(MMDevApiData));
819
return ALC_OUT_OF_MEMORY;
820
device->ExtraData = data;
823
data->hNotifyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
824
data->MsgEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
825
if(data->hNotifyEvent == NULL || data->MsgEvent == NULL)
834
if(!PlaybackDeviceList)
836
ThreadRequest req = { data->MsgEvent, 0 };
837
if(PostThreadMessage(ThreadID, WM_USER_Enumerate, (WPARAM)&req, ALL_DEVICE_PROBE))
838
(void)WaitForResponse(&req);
842
for(i = 0;i < NumPlaybackDevices;i++)
844
if(strcmp(deviceName, PlaybackDeviceList[i].name) == 0)
846
data->devid = strdupW(PlaybackDeviceList[i].devid);
856
ThreadRequest req = { data->MsgEvent, 0 };
859
if(PostThreadMessage(ThreadID, WM_USER_OpenDevice, (WPARAM)&req, (LPARAM)device))
860
hr = WaitForResponse(&req);
865
if(data->hNotifyEvent != NULL)
866
CloseHandle(data->hNotifyEvent);
867
data->hNotifyEvent = NULL;
868
if(data->MsgEvent != NULL)
869
CloseHandle(data->MsgEvent);
870
data->MsgEvent = NULL;
873
device->ExtraData = NULL;
875
ERR("Device init failed: 0x%08lx\n", hr);
876
return ALC_INVALID_VALUE;
882
static void MMDevApiClosePlayback(ALCdevice *device)
884
MMDevApiData *data = device->ExtraData;
885
ThreadRequest req = { data->MsgEvent, 0 };
887
if(PostThreadMessage(ThreadID, WM_USER_CloseDevice, (WPARAM)&req, (LPARAM)device))
888
(void)WaitForResponse(&req);
890
CloseHandle(data->MsgEvent);
891
data->MsgEvent = NULL;
893
CloseHandle(data->hNotifyEvent);
894
data->hNotifyEvent = NULL;
900
device->ExtraData = NULL;
903
static ALCboolean MMDevApiResetPlayback(ALCdevice *device)
905
MMDevApiData *data = device->ExtraData;
906
ThreadRequest req = { data->MsgEvent, 0 };
909
if(PostThreadMessage(ThreadID, WM_USER_ResetDevice, (WPARAM)&req, (LPARAM)device))
910
hr = WaitForResponse(&req);
912
return SUCCEEDED(hr) ? ALC_TRUE : ALC_FALSE;
915
static ALCboolean MMDevApiStartPlayback(ALCdevice *device)
917
MMDevApiData *data = device->ExtraData;
918
ThreadRequest req = { data->MsgEvent, 0 };
921
if(PostThreadMessage(ThreadID, WM_USER_StartDevice, (WPARAM)&req, (LPARAM)device))
922
hr = WaitForResponse(&req);
924
return SUCCEEDED(hr) ? ALC_TRUE : ALC_FALSE;
927
static void MMDevApiStopPlayback(ALCdevice *device)
929
MMDevApiData *data = device->ExtraData;
930
ThreadRequest req = { data->MsgEvent, 0 };
932
if(PostThreadMessage(ThreadID, WM_USER_StopDevice, (WPARAM)&req, (LPARAM)device))
933
(void)WaitForResponse(&req);
937
static const BackendFuncs MMDevApiFuncs = {
938
MMDevApiOpenPlayback,
939
MMDevApiClosePlayback,
940
MMDevApiResetPlayback,
941
MMDevApiStartPlayback,
942
MMDevApiStopPlayback,
952
ALCboolean alcMMDevApiInit(BackendFuncs *FuncList)
956
*FuncList = MMDevApiFuncs;
960
void alcMMDevApiDeinit(void)
964
for(i = 0;i < NumPlaybackDevices;i++)
966
free(PlaybackDeviceList[i].name);
967
free(PlaybackDeviceList[i].devid);
969
free(PlaybackDeviceList);
970
PlaybackDeviceList = NULL;
971
NumPlaybackDevices = 0;
973
for(i = 0;i < NumCaptureDevices;i++)
975
free(CaptureDeviceList[i].name);
976
free(CaptureDeviceList[i].devid);
978
free(CaptureDeviceList);
979
CaptureDeviceList = NULL;
980
NumCaptureDevices = 0;
984
TRACE("Sending WM_QUIT to Thread %04lx\n", ThreadID);
985
PostThreadMessage(ThreadID, WM_QUIT, 0, 0);
986
CloseHandle(ThreadHdl);
991
void alcMMDevApiProbe(enum DevProbe type)
993
ThreadRequest req = { NULL, 0 };
998
case ALL_DEVICE_PROBE:
999
req.FinishedEvt = CreateEvent(NULL, FALSE, FALSE, NULL);
1000
if(req.FinishedEvt == NULL)
1001
ERR("Failed to create event: %lu\n", GetLastError());
1002
else if(PostThreadMessage(ThreadID, WM_USER_Enumerate, (WPARAM)&req, type))
1003
hr = WaitForResponse(&req);
1007
for(i = 0;i < NumPlaybackDevices;i++)
1009
if(PlaybackDeviceList[i].name)
1010
AppendAllDeviceList(PlaybackDeviceList[i].name);
1015
case CAPTURE_DEVICE_PROBE:
1018
if(req.FinishedEvt != NULL)
1019
CloseHandle(req.FinishedEvt);
1020
req.FinishedEvt = NULL;