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)
75 static const ALCchar mmDevice
[] = "WASAPI Default";
76 static DevMap
*PlaybackDeviceList
;
77 static ALuint NumPlaybackDevices
;
78 static DevMap
*CaptureDeviceList
;
79 static ALuint NumCaptureDevices
;
82 static HANDLE ThreadHdl
;
83 static DWORD ThreadID
;
90 #define WM_USER_OpenDevice (WM_USER+0)
91 #define WM_USER_ResetDevice (WM_USER+1)
92 #define WM_USER_StopDevice (WM_USER+2)
93 #define WM_USER_CloseDevice (WM_USER+3)
94 #define WM_USER_Enumerate (WM_USER+4)
96 static HRESULT
WaitForResponse(ThreadRequest
*req
)
98 if(WaitForSingleObject(req
->FinishedEvt
, INFINITE
) == WAIT_OBJECT_0
)
100 ERR("Message response error: %lu\n", GetLastError());
105 static HRESULT
get_mmdevice_by_guid(IMMDeviceEnumerator
*devenum
, EDataFlow flowdir
, const GUID
*guid
, IMMDevice
**out
)
107 IMMDeviceCollection
*coll
;
111 hr
= IMMDeviceEnumerator_EnumAudioEndpoints(devenum
, flowdir
, DEVICE_STATE_ACTIVE
, &coll
);
114 ERR("Failed to enumerate audio endpoints: 0x%08lx\n", hr
);
119 IMMDeviceCollection_GetCount(coll
, &count
);
120 for(i
= 0;i
< count
;++i
)
127 if(FAILED(IMMDeviceCollection_Item(coll
, i
, &device
)))
130 hr
= IMMDevice_OpenPropertyStore(device
, STGM_READ
, &ps
);
133 WARN("OpenPropertyStore failed: 0x%08lx\n", hr
);
137 PropVariantInit(&pv
);
139 hr
= IPropertyStore_GetValue(ps
, &PKEY_AudioEndpoint_GUID
, &pv
);
142 PropVariantClear(&pv
);
143 IPropertyStore_Release(ps
);
144 WARN("GetValue failed: 0x%08lx\n", hr
);
147 CLSIDFromString(pv
.pwszVal
, &devid
);
149 PropVariantClear(&pv
);
150 IPropertyStore_Release(ps
);
152 if(IsEqualGUID(&devid
, guid
))
158 IMMDevice_Release(device
);
160 hr
= ((i
==count
) ? E_FAIL
: S_OK
);
162 IMMDeviceCollection_Release(coll
);
167 static void add_device(IMMDevice
*device
, DevMap
*devmap
)
175 hr
= IMMDevice_OpenPropertyStore(device
, STGM_READ
, &ps
);
178 WARN("OpenPropertyStore failed: 0x%08lx\n", hr
);
182 PropVariantInit(&pvguid
);
183 PropVariantInit(&pvname
);
185 hr
= IPropertyStore_GetValue(ps
, &PKEY_AudioEndpoint_GUID
, &pvguid
);
187 WARN("GetValue failed: 0x%08lx\n", hr
);
190 hr
= IPropertyStore_GetValue(ps
, (const PROPERTYKEY
*)&DEVPKEY_Device_FriendlyName
, &pvname
);
192 WARN("GetValue failed: 0x%08lx\n", hr
);
196 CLSIDFromString(pvguid
.pwszVal
, &devmap
->guid
);
197 if((len
=WideCharToMultiByte(CP_ACP
, 0, pvname
.pwszVal
, -1, NULL
, 0, NULL
, NULL
)) > 0)
199 devmap
->name
= calloc(1, len
);
200 WideCharToMultiByte(CP_ACP
, 0, pvname
.pwszVal
, -1, devmap
->name
, len
, NULL
, NULL
);
204 PropVariantClear(&pvname
);
205 PropVariantClear(&pvguid
);
206 IPropertyStore_Release(ps
);
209 static DevMap
*ProbeDevices(IMMDeviceEnumerator
*devenum
, EDataFlow flowdir
, ALuint
*numdevs
)
211 IMMDeviceCollection
*coll
;
212 IMMDevice
*defdev
= NULL
;
219 hr
= IMMDeviceEnumerator_EnumAudioEndpoints(devenum
, flowdir
, DEVICE_STATE_ACTIVE
, &coll
);
222 ERR("Failed to enumerate audio endpoints: 0x%08lx\n", hr
);
227 hr
= IMMDeviceCollection_GetCount(coll
, &count
);
228 if(SUCCEEDED(hr
) && count
> 0)
230 devlist
= calloc(count
, sizeof(*devlist
));
233 IMMDeviceCollection_Release(coll
);
237 hr
= IMMDeviceEnumerator_GetDefaultAudioEndpoint(devenum
, flowdir
,
238 eMultimedia
, &defdev
);
240 if(SUCCEEDED(hr
) && defdev
!= NULL
)
241 add_device(defdev
, &devlist
[idx
++]);
243 for(i
= 0;i
< count
&& idx
< count
;++i
)
247 if(FAILED(IMMDeviceCollection_Item(coll
, i
, &device
)))
251 add_device(device
, &devlist
[idx
++]);
253 IMMDevice_Release(device
);
256 if(defdev
) IMMDevice_Release(defdev
);
257 IMMDeviceCollection_Release(coll
);
264 static ALuint
MMDevApiProc(ALvoid
*ptr
)
266 ALCdevice
*device
= ptr
;
267 MMDevApiData
*data
= device
->ExtraData
;
269 IAudioRenderClient
*iface
;
276 hr
= CoInitialize(NULL
);
279 ERR("CoInitialize(NULL) failed: 0x%08lx\n", hr
);
280 aluHandleDisconnect(device
);
284 hr
= IAudioClient_GetService(data
->client
, &IID_IAudioRenderClient
, &render
.ptr
);
287 ERR("Failed to get AudioRenderClient service: 0x%08lx\n", hr
);
288 aluHandleDisconnect(device
);
294 while(!data
->killNow
)
296 hr
= IAudioClient_GetCurrentPadding(data
->client
, &written
);
299 ERR("Failed to get padding: 0x%08lx\n", hr
);
300 aluHandleDisconnect(device
);
304 len
= device
->UpdateSize
*device
->NumUpdates
- written
;
305 if(len
< device
->UpdateSize
)
308 res
= WaitForSingleObjectEx(data
->hNotifyEvent
, 2000, FALSE
);
309 if(res
!= WAIT_OBJECT_0
)
310 ERR("WaitForSingleObjectEx error: 0x%lx\n", res
);
313 len
-= len
%device
->UpdateSize
;
315 hr
= IAudioRenderClient_GetBuffer(render
.iface
, len
, &buffer
);
318 aluMixData(device
, buffer
, len
);
319 hr
= IAudioRenderClient_ReleaseBuffer(render
.iface
, len
, 0);
323 ERR("Failed to buffer data: 0x%08lx\n", hr
);
324 aluHandleDisconnect(device
);
329 IAudioRenderClient_Release(render
.iface
);
336 static ALCboolean
MakeExtensible(WAVEFORMATEXTENSIBLE
*out
, const WAVEFORMATEX
*in
)
338 memset(out
, 0, sizeof(*out
));
339 if(in
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
)
340 *out
= *(WAVEFORMATEXTENSIBLE
*)in
;
341 else if(in
->wFormatTag
== WAVE_FORMAT_PCM
)
344 out
->Format
.wFormatTag
= WAVE_FORMAT_EXTENSIBLE
;
345 out
->Format
.cbSize
= sizeof(*out
) - sizeof(*in
);
346 if(out
->Format
.nChannels
== 1)
347 out
->dwChannelMask
= MONO
;
348 else if(out
->Format
.nChannels
== 2)
349 out
->dwChannelMask
= STEREO
;
351 ERR("Unhandled PCM channel count: %d\n", out
->Format
.nChannels
);
352 out
->SubFormat
= KSDATAFORMAT_SUBTYPE_PCM
;
354 else if(in
->wFormatTag
== WAVE_FORMAT_IEEE_FLOAT
)
357 out
->Format
.wFormatTag
= WAVE_FORMAT_EXTENSIBLE
;
358 out
->Format
.cbSize
= sizeof(*out
) - sizeof(*in
);
359 if(out
->Format
.nChannels
== 1)
360 out
->dwChannelMask
= MONO
;
361 else if(out
->Format
.nChannels
== 2)
362 out
->dwChannelMask
= STEREO
;
364 ERR("Unhandled IEEE float channel count: %d\n", out
->Format
.nChannels
);
365 out
->SubFormat
= KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
;
369 ERR("Unhandled format tag: 0x%04x\n", in
->wFormatTag
);
375 static HRESULT
DoReset(ALCdevice
*device
)
377 MMDevApiData
*data
= device
->ExtraData
;
378 WAVEFORMATEXTENSIBLE OutputType
;
379 WAVEFORMATEX
*wfx
= NULL
;
380 REFERENCE_TIME min_per
, buf_time
;
381 UINT32 buffer_len
, min_len
;
384 hr
= IAudioClient_GetMixFormat(data
->client
, &wfx
);
387 ERR("Failed to get mix format: 0x%08lx\n", hr
);
391 if(!MakeExtensible(&OutputType
, wfx
))
399 buf_time
= ((REFERENCE_TIME
)device
->UpdateSize
*device
->NumUpdates
*10000000 +
400 device
->Frequency
-1) / device
->Frequency
;
402 if(!(device
->Flags
&DEVICE_FREQUENCY_REQUEST
))
403 device
->Frequency
= OutputType
.Format
.nSamplesPerSec
;
404 if(!(device
->Flags
&DEVICE_CHANNELS_REQUEST
))
406 if(OutputType
.Format
.nChannels
== 1 && OutputType
.dwChannelMask
== MONO
)
407 device
->FmtChans
= DevFmtMono
;
408 else if(OutputType
.Format
.nChannels
== 2 && OutputType
.dwChannelMask
== STEREO
)
409 device
->FmtChans
= DevFmtStereo
;
410 else if(OutputType
.Format
.nChannels
== 4 && OutputType
.dwChannelMask
== QUAD
)
411 device
->FmtChans
= DevFmtQuad
;
412 else if(OutputType
.Format
.nChannels
== 6 && OutputType
.dwChannelMask
== X5DOT1
)
413 device
->FmtChans
= DevFmtX51
;
414 else if(OutputType
.Format
.nChannels
== 6 && OutputType
.dwChannelMask
== X5DOT1SIDE
)
415 device
->FmtChans
= DevFmtX51Side
;
416 else if(OutputType
.Format
.nChannels
== 7 && OutputType
.dwChannelMask
== X6DOT1
)
417 device
->FmtChans
= DevFmtX61
;
418 else if(OutputType
.Format
.nChannels
== 8 && OutputType
.dwChannelMask
== X7DOT1
)
419 device
->FmtChans
= DevFmtX71
;
421 ERR("Unhandled channel config: %d -- 0x%08lx\n", OutputType
.Format
.nChannels
, OutputType
.dwChannelMask
);
424 switch(device
->FmtChans
)
427 OutputType
.Format
.nChannels
= 1;
428 OutputType
.dwChannelMask
= MONO
;
431 OutputType
.Format
.nChannels
= 2;
432 OutputType
.dwChannelMask
= STEREO
;
435 OutputType
.Format
.nChannels
= 4;
436 OutputType
.dwChannelMask
= QUAD
;
439 OutputType
.Format
.nChannels
= 6;
440 OutputType
.dwChannelMask
= X5DOT1
;
443 OutputType
.Format
.nChannels
= 6;
444 OutputType
.dwChannelMask
= X5DOT1SIDE
;
447 OutputType
.Format
.nChannels
= 7;
448 OutputType
.dwChannelMask
= X6DOT1
;
451 OutputType
.Format
.nChannels
= 8;
452 OutputType
.dwChannelMask
= X7DOT1
;
455 switch(device
->FmtType
)
458 device
->FmtType
= DevFmtUByte
;
461 OutputType
.Format
.wBitsPerSample
= 8;
462 OutputType
.Samples
.wValidBitsPerSample
= 8;
463 OutputType
.SubFormat
= KSDATAFORMAT_SUBTYPE_PCM
;
466 device
->FmtType
= DevFmtShort
;
469 OutputType
.Format
.wBitsPerSample
= 16;
470 OutputType
.Samples
.wValidBitsPerSample
= 16;
471 OutputType
.SubFormat
= KSDATAFORMAT_SUBTYPE_PCM
;
474 device
->FmtType
= DevFmtInt
;
477 OutputType
.Format
.wBitsPerSample
= 32;
478 OutputType
.Samples
.wValidBitsPerSample
= 32;
479 OutputType
.SubFormat
= KSDATAFORMAT_SUBTYPE_PCM
;
482 OutputType
.Format
.wBitsPerSample
= 32;
483 OutputType
.Samples
.wValidBitsPerSample
= 32;
484 OutputType
.SubFormat
= KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
;
487 OutputType
.Format
.nSamplesPerSec
= device
->Frequency
;
489 OutputType
.Format
.nBlockAlign
= OutputType
.Format
.nChannels
*
490 OutputType
.Format
.wBitsPerSample
/ 8;
491 OutputType
.Format
.nAvgBytesPerSec
= OutputType
.Format
.nSamplesPerSec
*
492 OutputType
.Format
.nBlockAlign
;
494 hr
= IAudioClient_IsFormatSupported(data
->client
, AUDCLNT_SHAREMODE_SHARED
, &OutputType
.Format
, &wfx
);
497 ERR("Failed to check format support: 0x%08lx\n", hr
);
498 hr
= IAudioClient_GetMixFormat(data
->client
, &wfx
);
502 ERR("Failed to find a supported format: 0x%08lx\n", hr
);
508 if(!MakeExtensible(&OutputType
, wfx
))
516 device
->Frequency
= OutputType
.Format
.nSamplesPerSec
;
517 if(OutputType
.Format
.nChannels
== 1 && OutputType
.dwChannelMask
== MONO
)
518 device
->FmtChans
= DevFmtMono
;
519 else if(OutputType
.Format
.nChannels
== 2 && OutputType
.dwChannelMask
== STEREO
)
520 device
->FmtChans
= DevFmtStereo
;
521 else if(OutputType
.Format
.nChannels
== 4 && OutputType
.dwChannelMask
== QUAD
)
522 device
->FmtChans
= DevFmtQuad
;
523 else if(OutputType
.Format
.nChannels
== 6 && OutputType
.dwChannelMask
== X5DOT1
)
524 device
->FmtChans
= DevFmtX51
;
525 else if(OutputType
.Format
.nChannels
== 6 && OutputType
.dwChannelMask
== X5DOT1SIDE
)
526 device
->FmtChans
= DevFmtX51Side
;
527 else if(OutputType
.Format
.nChannels
== 7 && OutputType
.dwChannelMask
== X6DOT1
)
528 device
->FmtChans
= DevFmtX61
;
529 else if(OutputType
.Format
.nChannels
== 8 && OutputType
.dwChannelMask
== X7DOT1
)
530 device
->FmtChans
= DevFmtX71
;
533 ERR("Unhandled extensible channels: %d -- 0x%08lx\n", OutputType
.Format
.nChannels
, OutputType
.dwChannelMask
);
534 device
->FmtChans
= DevFmtStereo
;
535 OutputType
.Format
.nChannels
= 2;
536 OutputType
.dwChannelMask
= STEREO
;
539 if(IsEqualGUID(&OutputType
.SubFormat
, &KSDATAFORMAT_SUBTYPE_PCM
))
541 if(OutputType
.Format
.wBitsPerSample
== 8)
542 device
->FmtType
= DevFmtUByte
;
543 else if(OutputType
.Format
.wBitsPerSample
== 16)
544 device
->FmtType
= DevFmtShort
;
545 else if(OutputType
.Format
.wBitsPerSample
== 32)
546 device
->FmtType
= DevFmtInt
;
549 device
->FmtType
= DevFmtShort
;
550 OutputType
.Format
.wBitsPerSample
= 16;
553 else if(IsEqualGUID(&OutputType
.SubFormat
, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
))
555 device
->FmtType
= DevFmtFloat
;
556 OutputType
.Format
.wBitsPerSample
= 32;
560 ERR("Unhandled format sub-type\n");
561 device
->FmtType
= DevFmtShort
;
562 OutputType
.Format
.wBitsPerSample
= 16;
563 OutputType
.SubFormat
= KSDATAFORMAT_SUBTYPE_PCM
;
565 OutputType
.Samples
.wValidBitsPerSample
= OutputType
.Format
.wBitsPerSample
;
568 SetDefaultWFXChannelOrder(device
);
570 hr
= IAudioClient_Initialize(data
->client
, AUDCLNT_SHAREMODE_SHARED
,
571 AUDCLNT_STREAMFLAGS_EVENTCALLBACK
,
572 buf_time
, 0, &OutputType
.Format
, NULL
);
575 ERR("Failed to initialize audio client: 0x%08lx\n", hr
);
579 hr
= IAudioClient_GetDevicePeriod(data
->client
, &min_per
, NULL
);
582 min_len
= (min_per
*device
->Frequency
+ 10000000-1) / 10000000;
583 hr
= IAudioClient_GetBufferSize(data
->client
, &buffer_len
);
587 ERR("Failed to get audio buffer info: 0x%08lx\n", hr
);
591 device
->UpdateSize
= min_len
;
592 device
->NumUpdates
= buffer_len
/ device
->UpdateSize
;
593 if(device
->NumUpdates
<= 1)
595 device
->NumUpdates
= 1;
596 ERR("Audio client returned buffer_len < period*2; expect break up\n");
599 ResetEvent(data
->hNotifyEvent
);
600 hr
= IAudioClient_SetEventHandle(data
->client
, data
->hNotifyEvent
);
602 hr
= IAudioClient_Start(data
->client
);
605 ERR("Failed to start audio client: 0x%08lx\n", hr
);
609 data
->thread
= StartThread(MMDevApiProc
, device
);
612 IAudioClient_Stop(data
->client
);
613 ERR("Failed to start thread\n");
621 static DWORD CALLBACK
MMDevApiMsgProc(void *ptr
)
623 ThreadRequest
*req
= ptr
;
624 IMMDeviceEnumerator
*Enumerator
;
625 ALuint deviceCount
= 0;
631 TRACE("Starting message thread\n");
633 cohr
= CoInitialize(NULL
);
636 WARN("Failed to initialize COM: 0x%08lx\n", cohr
);
638 SetEvent(req
->FinishedEvt
);
642 hr
= CoCreateInstance(&CLSID_MMDeviceEnumerator
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IMMDeviceEnumerator
, &ptr
);
645 WARN("Failed to create IMMDeviceEnumerator instance: 0x%08lx\n", hr
);
648 SetEvent(req
->FinishedEvt
);
652 IMMDeviceEnumerator_Release(Enumerator
);
658 SetEvent(req
->FinishedEvt
);
660 TRACE("Starting message loop\n");
661 while(GetMessage(&msg
, NULL
, 0, 0))
663 TRACE("Got message %u\n", msg
.message
);
666 case WM_USER_OpenDevice
:
667 req
= (ThreadRequest
*)msg
.wParam
;
668 device
= (ALCdevice
*)msg
.lParam
;
669 data
= device
->ExtraData
;
672 if(++deviceCount
== 1)
673 hr
= cohr
= CoInitialize(NULL
);
675 hr
= CoCreateInstance(&CLSID_MMDeviceEnumerator
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IMMDeviceEnumerator
, &ptr
);
679 if(IsEqualGUID(&data
->guid
, &GUID_NULL
))
680 hr
= IMMDeviceEnumerator_GetDefaultAudioEndpoint(Enumerator
, eRender
, eMultimedia
, &data
->mmdev
);
682 hr
= get_mmdevice_by_guid(Enumerator
, eRender
, &data
->guid
, &data
->mmdev
);
683 IMMDeviceEnumerator_Release(Enumerator
);
687 hr
= IMMDevice_Activate(data
->mmdev
, &IID_IAudioClient
, CLSCTX_INPROC_SERVER
, NULL
, &ptr
);
694 IMMDevice_Release(data
->mmdev
);
696 if(--deviceCount
== 0 && SUCCEEDED(cohr
))
701 SetEvent(req
->FinishedEvt
);
704 case WM_USER_ResetDevice
:
705 req
= (ThreadRequest
*)msg
.wParam
;
706 device
= (ALCdevice
*)msg
.lParam
;
708 req
->result
= DoReset(device
);
709 SetEvent(req
->FinishedEvt
);
712 case WM_USER_StopDevice
:
713 req
= (ThreadRequest
*)msg
.wParam
;
714 device
= (ALCdevice
*)msg
.lParam
;
715 data
= device
->ExtraData
;
720 StopThread(data
->thread
);
725 IAudioClient_Stop(data
->client
);
729 SetEvent(req
->FinishedEvt
);
732 case WM_USER_CloseDevice
:
733 req
= (ThreadRequest
*)msg
.wParam
;
734 device
= (ALCdevice
*)msg
.lParam
;
735 data
= device
->ExtraData
;
737 IAudioClient_Release(data
->client
);
740 IMMDevice_Release(data
->mmdev
);
743 if(--deviceCount
== 0)
747 SetEvent(req
->FinishedEvt
);
750 case WM_USER_Enumerate
:
751 req
= (ThreadRequest
*)msg
.wParam
;
754 if(++deviceCount
== 1)
755 hr
= cohr
= CoInitialize(NULL
);
757 hr
= CoCreateInstance(&CLSID_MMDeviceEnumerator
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IMMDeviceEnumerator
, &ptr
);
766 if(msg
.lParam
== CAPTURE_DEVICE_PROBE
)
769 devlist
= &CaptureDeviceList
;
770 numdevs
= &NumCaptureDevices
;
775 devlist
= &PlaybackDeviceList
;
776 numdevs
= &NumPlaybackDevices
;
779 for(i
= 0;i
< *numdevs
;i
++)
780 free((*devlist
)[i
].name
);
785 *devlist
= ProbeDevices(Enumerator
, flowdir
, numdevs
);
787 IMMDeviceEnumerator_Release(Enumerator
);
791 if(--deviceCount
== 0 && SUCCEEDED(cohr
))
795 SetEvent(req
->FinishedEvt
);
799 ERR("Unexpected message: %u\n", msg
.message
);
803 TRACE("Message loop finished\n");
809 static BOOL
MMDevApiLoad(void)
811 static HRESULT InitResult
;
817 req
.FinishedEvt
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
818 if(req
.FinishedEvt
== NULL
)
819 ERR("Failed to create event: %lu\n", GetLastError());
822 ThreadHdl
= CreateThread(NULL
, 0, MMDevApiMsgProc
, &req
, 0, &ThreadID
);
823 if(ThreadHdl
!= NULL
)
824 InitResult
= WaitForResponse(&req
);
825 CloseHandle(req
.FinishedEvt
);
828 return SUCCEEDED(InitResult
);
832 static ALCenum
MMDevApiOpenPlayback(ALCdevice
*device
, const ALCchar
*deviceName
)
834 MMDevApiData
*data
= NULL
;
837 //Initialise requested device
838 data
= calloc(1, sizeof(MMDevApiData
));
840 return ALC_OUT_OF_MEMORY
;
841 device
->ExtraData
= data
;
844 data
->hNotifyEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
845 data
->MsgEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
846 if(data
->hNotifyEvent
== NULL
|| data
->MsgEvent
== NULL
)
851 data
->guid
= GUID_NULL
;
854 deviceName
= mmDevice
;
855 else if(strcmp(deviceName
, mmDevice
) != 0)
859 if(!PlaybackDeviceList
)
861 ThreadRequest req
= { data
->MsgEvent
, 0 };
863 if(PostThreadMessage(ThreadID
, WM_USER_Enumerate
, (WPARAM
)&req
, ALL_DEVICE_PROBE
))
864 (void)WaitForResponse(&req
);
868 for(i
= 0;i
< NumPlaybackDevices
;i
++)
870 if(PlaybackDeviceList
[i
].name
&&
871 strcmp(deviceName
, PlaybackDeviceList
[i
].name
) == 0)
873 data
->guid
= PlaybackDeviceList
[i
].guid
;
883 ThreadRequest req
= { data
->MsgEvent
, 0 };
886 if(PostThreadMessage(ThreadID
, WM_USER_OpenDevice
, (WPARAM
)&req
, (LPARAM
)device
))
887 hr
= WaitForResponse(&req
);
892 if(data
->hNotifyEvent
!= NULL
)
893 CloseHandle(data
->hNotifyEvent
);
894 data
->hNotifyEvent
= NULL
;
895 if(data
->MsgEvent
!= NULL
)
896 CloseHandle(data
->MsgEvent
);
897 data
->MsgEvent
= NULL
;
900 device
->ExtraData
= NULL
;
902 ERR("Device init failed: 0x%08lx\n", hr
);
903 return ALC_INVALID_VALUE
;
906 device
->szDeviceName
= strdup(deviceName
);
910 static void MMDevApiClosePlayback(ALCdevice
*device
)
912 MMDevApiData
*data
= device
->ExtraData
;
913 ThreadRequest req
= { data
->MsgEvent
, 0 };
915 if(PostThreadMessage(ThreadID
, WM_USER_CloseDevice
, (WPARAM
)&req
, (LPARAM
)device
))
916 (void)WaitForResponse(&req
);
918 CloseHandle(data
->MsgEvent
);
919 data
->MsgEvent
= NULL
;
921 CloseHandle(data
->hNotifyEvent
);
922 data
->hNotifyEvent
= NULL
;
925 device
->ExtraData
= NULL
;
928 static ALCboolean
MMDevApiResetPlayback(ALCdevice
*device
)
930 MMDevApiData
*data
= device
->ExtraData
;
931 ThreadRequest req
= { data
->MsgEvent
, 0 };
934 if(PostThreadMessage(ThreadID
, WM_USER_ResetDevice
, (WPARAM
)&req
, (LPARAM
)device
))
935 hr
= WaitForResponse(&req
);
937 return SUCCEEDED(hr
) ? ALC_TRUE
: ALC_FALSE
;
940 static void MMDevApiStopPlayback(ALCdevice
*device
)
942 MMDevApiData
*data
= device
->ExtraData
;
943 ThreadRequest req
= { data
->MsgEvent
, 0 };
945 if(PostThreadMessage(ThreadID
, WM_USER_StopDevice
, (WPARAM
)&req
, (LPARAM
)device
))
946 (void)WaitForResponse(&req
);
950 static const BackendFuncs MMDevApiFuncs
= {
951 MMDevApiOpenPlayback
,
952 MMDevApiClosePlayback
,
953 MMDevApiResetPlayback
,
954 MMDevApiStopPlayback
,
964 ALCboolean
alcMMDevApiInit(BackendFuncs
*FuncList
)
968 *FuncList
= MMDevApiFuncs
;
972 void alcMMDevApiDeinit(void)
976 TRACE("Sending WM_QUIT to Thread %04lx\n", ThreadID
);
977 PostThreadMessage(ThreadID
, WM_QUIT
, 0, 0);
978 CloseHandle(ThreadHdl
);
983 void alcMMDevApiProbe(enum DevProbe type
)
991 AppendDeviceList(mmDevice
);
994 case ALL_DEVICE_PROBE
:
995 req
.FinishedEvt
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
996 if(req
.FinishedEvt
== NULL
)
997 ERR("Failed to create event: %lu\n", GetLastError());
998 else if(PostThreadMessage(ThreadID
, WM_USER_Enumerate
, (WPARAM
)&req
, type
))
999 hr
= WaitForResponse(&req
);
1003 for(i
= 0;i
< NumPlaybackDevices
;i
++)
1005 if(PlaybackDeviceList
[i
].name
)
1006 AppendAllDeviceList(PlaybackDeviceList
[i
].name
);
1009 if(req
.FinishedEvt
!= NULL
)
1010 CloseHandle(req
.FinishedEvt
);
1013 case CAPTURE_DEVICE_PROBE
: