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>
31 #include <devpropdef.h>
36 #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_DEVPROPKEY(DEVPKEY_Device_FriendlyName
, 0xa45c254e, 0xdf1c, 0x4efd, 0x80,0x20, 0x67,0xd1,0x46,0xa8,0x50,0xe0, 14);
50 #define MONO SPEAKER_FRONT_CENTER
51 #define STEREO (SPEAKER_FRONT_LEFT|SPEAKER_FRONT_RIGHT)
52 #define QUAD (SPEAKER_FRONT_LEFT|SPEAKER_FRONT_RIGHT|SPEAKER_BACK_LEFT|SPEAKER_BACK_RIGHT)
53 #define X5DOT1 (SPEAKER_FRONT_LEFT|SPEAKER_FRONT_RIGHT|SPEAKER_FRONT_CENTER|SPEAKER_LOW_FREQUENCY|SPEAKER_BACK_LEFT|SPEAKER_BACK_RIGHT)
54 #define X5DOT1SIDE (SPEAKER_FRONT_LEFT|SPEAKER_FRONT_RIGHT|SPEAKER_FRONT_CENTER|SPEAKER_LOW_FREQUENCY|SPEAKER_SIDE_LEFT|SPEAKER_SIDE_RIGHT)
55 #define X6DOT1 (SPEAKER_FRONT_LEFT|SPEAKER_FRONT_RIGHT|SPEAKER_FRONT_CENTER|SPEAKER_LOW_FREQUENCY|SPEAKER_BACK_CENTER|SPEAKER_SIDE_LEFT|SPEAKER_SIDE_RIGHT)
56 #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)
64 IAudioRenderClient
*render
;
69 volatile UINT32 Padding
;
81 static DevMap
*PlaybackDeviceList
;
82 static ALuint NumPlaybackDevices
;
83 static DevMap
*CaptureDeviceList
;
84 static ALuint NumCaptureDevices
;
87 static HANDLE ThreadHdl
;
88 static DWORD ThreadID
;
95 #define WM_USER_OpenDevice (WM_USER+0)
96 #define WM_USER_ResetDevice (WM_USER+1)
97 #define WM_USER_StartDevice (WM_USER+2)
98 #define WM_USER_StopDevice (WM_USER+3)
99 #define WM_USER_CloseDevice (WM_USER+4)
100 #define WM_USER_Enumerate (WM_USER+5)
102 static HRESULT
WaitForResponse(ThreadRequest
*req
)
104 if(WaitForSingleObject(req
->FinishedEvt
, INFINITE
) == WAIT_OBJECT_0
)
106 ERR("Message response error: %lu\n", GetLastError());
111 static ALCchar
*get_device_name(IMMDevice
*device
)
113 ALCchar
*name
= NULL
;
119 hr
= IMMDevice_OpenPropertyStore(device
, STGM_READ
, &ps
);
122 WARN("OpenPropertyStore failed: 0x%08lx\n", hr
);
126 PropVariantInit(&pvname
);
128 hr
= IPropertyStore_GetValue(ps
, (const PROPERTYKEY
*)&DEVPKEY_Device_FriendlyName
, &pvname
);
131 WARN("GetValue failed: 0x%08lx\n", hr
);
136 if((len
=WideCharToMultiByte(CP_ACP
, 0, pvname
.pwszVal
, -1, NULL
, 0, NULL
, NULL
)) > 0)
138 name
= calloc(1, len
);
139 WideCharToMultiByte(CP_ACP
, 0, pvname
.pwszVal
, -1, name
, len
, NULL
, NULL
);
143 PropVariantClear(&pvname
);
144 IPropertyStore_Release(ps
);
149 static void add_device(IMMDevice
*device
, DevMap
*devmap
)
154 hr
= IMMDevice_GetId(device
, &devid
);
157 devmap
->devid
= strdupW(devid
);
158 devmap
->name
= get_device_name(device
);
159 TRACE("Got device \"%s\", \"%ls\"\n", devmap
->name
, devmap
->devid
);
160 CoTaskMemFree(devid
);
164 static DevMap
*ProbeDevices(IMMDeviceEnumerator
*devenum
, EDataFlow flowdir
, ALuint
*numdevs
)
166 IMMDeviceCollection
*coll
;
167 IMMDevice
*defdev
= NULL
;
168 DevMap
*devlist
= NULL
;
174 hr
= IMMDeviceEnumerator_EnumAudioEndpoints(devenum
, flowdir
, DEVICE_STATE_ACTIVE
, &coll
);
177 ERR("Failed to enumerate audio endpoints: 0x%08lx\n", hr
);
182 hr
= IMMDeviceCollection_GetCount(coll
, &count
);
183 if(SUCCEEDED(hr
) && count
> 0)
185 devlist
= calloc(count
, sizeof(*devlist
));
188 IMMDeviceCollection_Release(coll
);
192 hr
= IMMDeviceEnumerator_GetDefaultAudioEndpoint(devenum
, flowdir
,
193 eMultimedia
, &defdev
);
195 if(SUCCEEDED(hr
) && defdev
!= NULL
)
196 add_device(defdev
, &devlist
[idx
++]);
198 for(i
= 0;i
< count
&& idx
< count
;++i
)
202 if(FAILED(IMMDeviceCollection_Item(coll
, i
, &device
)))
206 add_device(device
, &devlist
[idx
++]);
208 IMMDevice_Release(device
);
211 if(defdev
) IMMDevice_Release(defdev
);
212 IMMDeviceCollection_Release(coll
);
219 static ALuint
MMDevApiProc(ALvoid
*ptr
)
221 ALCdevice
*device
= ptr
;
222 MMDevApiData
*data
= device
->ExtraData
;
223 UINT32 buffer_len
, written
;
224 ALuint update_size
, len
;
228 hr
= CoInitialize(NULL
);
231 ERR("CoInitialize(NULL) failed: 0x%08lx\n", hr
);
232 aluHandleDisconnect(device
);
238 update_size
= device
->UpdateSize
;
239 buffer_len
= update_size
* device
->NumUpdates
;
240 while(!data
->killNow
)
242 hr
= IAudioClient_GetCurrentPadding(data
->client
, &written
);
245 ERR("Failed to get padding: 0x%08lx\n", hr
);
246 aluHandleDisconnect(device
);
249 data
->Padding
= written
;
251 len
= buffer_len
- written
;
252 if(len
< update_size
)
255 res
= WaitForSingleObjectEx(data
->NotifyEvent
, 2000, FALSE
);
256 if(res
!= WAIT_OBJECT_0
)
257 ERR("WaitForSingleObjectEx error: 0x%lx\n", res
);
260 len
-= len
%update_size
;
262 hr
= IAudioRenderClient_GetBuffer(data
->render
, len
, &buffer
);
265 ALCdevice_Lock(device
);
266 aluMixData(device
, buffer
, len
);
267 data
->Padding
= written
+ len
;
268 ALCdevice_Unlock(device
);
269 hr
= IAudioRenderClient_ReleaseBuffer(data
->render
, len
, 0);
273 ERR("Failed to buffer data: 0x%08lx\n", hr
);
274 aluHandleDisconnect(device
);
285 static ALCboolean
MakeExtensible(WAVEFORMATEXTENSIBLE
*out
, const WAVEFORMATEX
*in
)
287 memset(out
, 0, sizeof(*out
));
288 if(in
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
)
289 *out
= *(const WAVEFORMATEXTENSIBLE
*)in
;
290 else if(in
->wFormatTag
== WAVE_FORMAT_PCM
)
293 out
->Format
.wFormatTag
= WAVE_FORMAT_EXTENSIBLE
;
294 out
->Format
.cbSize
= sizeof(*out
) - sizeof(*in
);
295 if(out
->Format
.nChannels
== 1)
296 out
->dwChannelMask
= MONO
;
297 else if(out
->Format
.nChannels
== 2)
298 out
->dwChannelMask
= STEREO
;
300 ERR("Unhandled PCM channel count: %d\n", out
->Format
.nChannels
);
301 out
->SubFormat
= KSDATAFORMAT_SUBTYPE_PCM
;
303 else if(in
->wFormatTag
== WAVE_FORMAT_IEEE_FLOAT
)
306 out
->Format
.wFormatTag
= WAVE_FORMAT_EXTENSIBLE
;
307 out
->Format
.cbSize
= sizeof(*out
) - sizeof(*in
);
308 if(out
->Format
.nChannels
== 1)
309 out
->dwChannelMask
= MONO
;
310 else if(out
->Format
.nChannels
== 2)
311 out
->dwChannelMask
= STEREO
;
313 ERR("Unhandled IEEE float channel count: %d\n", out
->Format
.nChannels
);
314 out
->SubFormat
= KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
;
318 ERR("Unhandled format tag: 0x%04x\n", in
->wFormatTag
);
324 static HRESULT
DoReset(ALCdevice
*device
)
326 MMDevApiData
*data
= device
->ExtraData
;
327 WAVEFORMATEXTENSIBLE OutputType
;
328 WAVEFORMATEX
*wfx
= NULL
;
329 REFERENCE_TIME min_per
, buf_time
;
330 UINT32 buffer_len
, min_len
;
333 hr
= IAudioClient_GetMixFormat(data
->client
, &wfx
);
336 ERR("Failed to get mix format: 0x%08lx\n", hr
);
340 if(!MakeExtensible(&OutputType
, wfx
))
348 buf_time
= ((REFERENCE_TIME
)device
->UpdateSize
*device
->NumUpdates
*10000000 +
349 device
->Frequency
-1) / device
->Frequency
;
351 if(!(device
->Flags
&DEVICE_FREQUENCY_REQUEST
))
352 device
->Frequency
= OutputType
.Format
.nSamplesPerSec
;
353 if(!(device
->Flags
&DEVICE_CHANNELS_REQUEST
))
355 if(OutputType
.Format
.nChannels
== 1 && OutputType
.dwChannelMask
== MONO
)
356 device
->FmtChans
= DevFmtMono
;
357 else if(OutputType
.Format
.nChannels
== 2 && OutputType
.dwChannelMask
== STEREO
)
358 device
->FmtChans
= DevFmtStereo
;
359 else if(OutputType
.Format
.nChannels
== 4 && OutputType
.dwChannelMask
== QUAD
)
360 device
->FmtChans
= DevFmtQuad
;
361 else if(OutputType
.Format
.nChannels
== 6 && OutputType
.dwChannelMask
== X5DOT1
)
362 device
->FmtChans
= DevFmtX51
;
363 else if(OutputType
.Format
.nChannels
== 6 && OutputType
.dwChannelMask
== X5DOT1SIDE
)
364 device
->FmtChans
= DevFmtX51Side
;
365 else if(OutputType
.Format
.nChannels
== 7 && OutputType
.dwChannelMask
== X6DOT1
)
366 device
->FmtChans
= DevFmtX61
;
367 else if(OutputType
.Format
.nChannels
== 8 && OutputType
.dwChannelMask
== X7DOT1
)
368 device
->FmtChans
= DevFmtX71
;
370 ERR("Unhandled channel config: %d -- 0x%08lx\n", OutputType
.Format
.nChannels
, OutputType
.dwChannelMask
);
373 switch(device
->FmtChans
)
376 OutputType
.Format
.nChannels
= 1;
377 OutputType
.dwChannelMask
= MONO
;
380 OutputType
.Format
.nChannels
= 2;
381 OutputType
.dwChannelMask
= STEREO
;
384 OutputType
.Format
.nChannels
= 4;
385 OutputType
.dwChannelMask
= QUAD
;
388 OutputType
.Format
.nChannels
= 6;
389 OutputType
.dwChannelMask
= X5DOT1
;
392 OutputType
.Format
.nChannels
= 6;
393 OutputType
.dwChannelMask
= X5DOT1SIDE
;
396 OutputType
.Format
.nChannels
= 7;
397 OutputType
.dwChannelMask
= X6DOT1
;
400 OutputType
.Format
.nChannels
= 8;
401 OutputType
.dwChannelMask
= X7DOT1
;
404 switch(device
->FmtType
)
407 device
->FmtType
= DevFmtUByte
;
410 OutputType
.Format
.wBitsPerSample
= 8;
411 OutputType
.Samples
.wValidBitsPerSample
= 8;
412 OutputType
.SubFormat
= KSDATAFORMAT_SUBTYPE_PCM
;
415 device
->FmtType
= DevFmtShort
;
418 OutputType
.Format
.wBitsPerSample
= 16;
419 OutputType
.Samples
.wValidBitsPerSample
= 16;
420 OutputType
.SubFormat
= KSDATAFORMAT_SUBTYPE_PCM
;
423 device
->FmtType
= DevFmtInt
;
426 OutputType
.Format
.wBitsPerSample
= 32;
427 OutputType
.Samples
.wValidBitsPerSample
= 32;
428 OutputType
.SubFormat
= KSDATAFORMAT_SUBTYPE_PCM
;
431 OutputType
.Format
.wBitsPerSample
= 32;
432 OutputType
.Samples
.wValidBitsPerSample
= 32;
433 OutputType
.SubFormat
= KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
;
436 OutputType
.Format
.nSamplesPerSec
= device
->Frequency
;
438 OutputType
.Format
.nBlockAlign
= OutputType
.Format
.nChannels
*
439 OutputType
.Format
.wBitsPerSample
/ 8;
440 OutputType
.Format
.nAvgBytesPerSec
= OutputType
.Format
.nSamplesPerSec
*
441 OutputType
.Format
.nBlockAlign
;
443 hr
= IAudioClient_IsFormatSupported(data
->client
, AUDCLNT_SHAREMODE_SHARED
, &OutputType
.Format
, &wfx
);
446 ERR("Failed to check format support: 0x%08lx\n", hr
);
447 hr
= IAudioClient_GetMixFormat(data
->client
, &wfx
);
451 ERR("Failed to find a supported format: 0x%08lx\n", hr
);
457 if(!MakeExtensible(&OutputType
, wfx
))
465 device
->Frequency
= OutputType
.Format
.nSamplesPerSec
;
466 if(OutputType
.Format
.nChannels
== 1 && OutputType
.dwChannelMask
== MONO
)
467 device
->FmtChans
= DevFmtMono
;
468 else if(OutputType
.Format
.nChannels
== 2 && OutputType
.dwChannelMask
== STEREO
)
469 device
->FmtChans
= DevFmtStereo
;
470 else if(OutputType
.Format
.nChannels
== 4 && OutputType
.dwChannelMask
== QUAD
)
471 device
->FmtChans
= DevFmtQuad
;
472 else if(OutputType
.Format
.nChannels
== 6 && OutputType
.dwChannelMask
== X5DOT1
)
473 device
->FmtChans
= DevFmtX51
;
474 else if(OutputType
.Format
.nChannels
== 6 && OutputType
.dwChannelMask
== X5DOT1SIDE
)
475 device
->FmtChans
= DevFmtX51Side
;
476 else if(OutputType
.Format
.nChannels
== 7 && OutputType
.dwChannelMask
== X6DOT1
)
477 device
->FmtChans
= DevFmtX61
;
478 else if(OutputType
.Format
.nChannels
== 8 && OutputType
.dwChannelMask
== X7DOT1
)
479 device
->FmtChans
= DevFmtX71
;
482 ERR("Unhandled extensible channels: %d -- 0x%08lx\n", OutputType
.Format
.nChannels
, OutputType
.dwChannelMask
);
483 device
->FmtChans
= DevFmtStereo
;
484 OutputType
.Format
.nChannels
= 2;
485 OutputType
.dwChannelMask
= STEREO
;
488 if(IsEqualGUID(&OutputType
.SubFormat
, &KSDATAFORMAT_SUBTYPE_PCM
))
490 if(OutputType
.Format
.wBitsPerSample
== 8)
491 device
->FmtType
= DevFmtUByte
;
492 else if(OutputType
.Format
.wBitsPerSample
== 16)
493 device
->FmtType
= DevFmtShort
;
494 else if(OutputType
.Format
.wBitsPerSample
== 32)
495 device
->FmtType
= DevFmtInt
;
498 device
->FmtType
= DevFmtShort
;
499 OutputType
.Format
.wBitsPerSample
= 16;
502 else if(IsEqualGUID(&OutputType
.SubFormat
, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
))
504 device
->FmtType
= DevFmtFloat
;
505 OutputType
.Format
.wBitsPerSample
= 32;
509 ERR("Unhandled format sub-type\n");
510 device
->FmtType
= DevFmtShort
;
511 OutputType
.Format
.wBitsPerSample
= 16;
512 OutputType
.SubFormat
= KSDATAFORMAT_SUBTYPE_PCM
;
514 OutputType
.Samples
.wValidBitsPerSample
= OutputType
.Format
.wBitsPerSample
;
517 SetDefaultWFXChannelOrder(device
);
519 hr
= IAudioClient_Initialize(data
->client
, AUDCLNT_SHAREMODE_SHARED
,
520 AUDCLNT_STREAMFLAGS_EVENTCALLBACK
,
521 buf_time
, 0, &OutputType
.Format
, NULL
);
524 ERR("Failed to initialize audio client: 0x%08lx\n", hr
);
528 hr
= IAudioClient_GetDevicePeriod(data
->client
, &min_per
, NULL
);
531 min_len
= (UINT32
)((min_per
*device
->Frequency
+ 10000000-1) / 10000000);
532 /* Find the nearest multiple of the period size to the update size */
533 if(min_len
< device
->UpdateSize
)
534 min_len
*= (device
->UpdateSize
+ min_len
/2)/min_len
;
535 hr
= IAudioClient_GetBufferSize(data
->client
, &buffer_len
);
539 ERR("Failed to get audio buffer info: 0x%08lx\n", hr
);
543 device
->UpdateSize
= min_len
;
544 device
->NumUpdates
= buffer_len
/ device
->UpdateSize
;
545 if(device
->NumUpdates
<= 1)
547 ERR("Audio client returned buffer_len < period*2; expect break up\n");
548 device
->NumUpdates
= 2;
549 device
->UpdateSize
= buffer_len
/ device
->NumUpdates
;
556 static DWORD CALLBACK
MMDevApiMsgProc(void *ptr
)
558 ThreadRequest
*req
= ptr
;
559 IMMDeviceEnumerator
*Enumerator
;
560 ALuint deviceCount
= 0;
566 TRACE("Starting message thread\n");
568 cohr
= CoInitialize(NULL
);
571 WARN("Failed to initialize COM: 0x%08lx\n", cohr
);
573 SetEvent(req
->FinishedEvt
);
577 hr
= CoCreateInstance(&CLSID_MMDeviceEnumerator
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IMMDeviceEnumerator
, &ptr
);
580 WARN("Failed to create IMMDeviceEnumerator instance: 0x%08lx\n", hr
);
583 SetEvent(req
->FinishedEvt
);
587 IMMDeviceEnumerator_Release(Enumerator
);
593 SetEvent(req
->FinishedEvt
);
595 TRACE("Starting message loop\n");
596 while(GetMessage(&msg
, NULL
, 0, 0))
598 TRACE("Got message %u\n", msg
.message
);
601 case WM_USER_OpenDevice
:
602 req
= (ThreadRequest
*)msg
.wParam
;
603 device
= (ALCdevice
*)msg
.lParam
;
604 data
= device
->ExtraData
;
607 if(++deviceCount
== 1)
608 hr
= cohr
= CoInitialize(NULL
);
610 hr
= CoCreateInstance(&CLSID_MMDeviceEnumerator
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IMMDeviceEnumerator
, &ptr
);
615 hr
= IMMDeviceEnumerator_GetDefaultAudioEndpoint(Enumerator
, eRender
, eMultimedia
, &data
->mmdev
);
617 hr
= IMMDeviceEnumerator_GetDevice(Enumerator
, data
->devid
, &data
->mmdev
);
618 IMMDeviceEnumerator_Release(Enumerator
);
622 hr
= IMMDevice_Activate(data
->mmdev
, &IID_IAudioClient
, CLSCTX_INPROC_SERVER
, NULL
, &ptr
);
626 device
->DeviceName
= get_device_name(data
->mmdev
);
632 IMMDevice_Release(data
->mmdev
);
634 if(--deviceCount
== 0 && SUCCEEDED(cohr
))
639 SetEvent(req
->FinishedEvt
);
642 case WM_USER_ResetDevice
:
643 req
= (ThreadRequest
*)msg
.wParam
;
644 device
= (ALCdevice
*)msg
.lParam
;
646 req
->result
= DoReset(device
);
647 SetEvent(req
->FinishedEvt
);
650 case WM_USER_StartDevice
:
651 req
= (ThreadRequest
*)msg
.wParam
;
652 device
= (ALCdevice
*)msg
.lParam
;
653 data
= device
->ExtraData
;
655 ResetEvent(data
->NotifyEvent
);
656 hr
= IAudioClient_SetEventHandle(data
->client
, data
->NotifyEvent
);
658 ERR("Failed to set event handle: 0x%08lx\n", hr
);
661 hr
= IAudioClient_Start(data
->client
);
663 ERR("Failed to start audio client: 0x%08lx\n", hr
);
667 hr
= IAudioClient_GetService(data
->client
, &IID_IAudioRenderClient
, &ptr
);
671 data
->thread
= StartThread(MMDevApiProc
, device
);
675 IAudioRenderClient_Release(data
->render
);
677 IAudioClient_Stop(data
->client
);
678 ERR("Failed to start thread\n");
684 SetEvent(req
->FinishedEvt
);
687 case WM_USER_StopDevice
:
688 req
= (ThreadRequest
*)msg
.wParam
;
689 device
= (ALCdevice
*)msg
.lParam
;
690 data
= device
->ExtraData
;
695 StopThread(data
->thread
);
700 IAudioRenderClient_Release(data
->render
);
702 IAudioClient_Stop(data
->client
);
706 SetEvent(req
->FinishedEvt
);
709 case WM_USER_CloseDevice
:
710 req
= (ThreadRequest
*)msg
.wParam
;
711 device
= (ALCdevice
*)msg
.lParam
;
712 data
= device
->ExtraData
;
714 IAudioClient_Release(data
->client
);
717 IMMDevice_Release(data
->mmdev
);
720 if(--deviceCount
== 0)
724 SetEvent(req
->FinishedEvt
);
727 case WM_USER_Enumerate
:
728 req
= (ThreadRequest
*)msg
.wParam
;
731 if(++deviceCount
== 1)
732 hr
= cohr
= CoInitialize(NULL
);
734 hr
= CoCreateInstance(&CLSID_MMDeviceEnumerator
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IMMDeviceEnumerator
, &ptr
);
743 if(msg
.lParam
== CAPTURE_DEVICE_PROBE
)
746 devlist
= &CaptureDeviceList
;
747 numdevs
= &NumCaptureDevices
;
752 devlist
= &PlaybackDeviceList
;
753 numdevs
= &NumPlaybackDevices
;
756 for(i
= 0;i
< *numdevs
;i
++)
758 free((*devlist
)[i
].name
);
759 free((*devlist
)[i
].devid
);
765 *devlist
= ProbeDevices(Enumerator
, flowdir
, numdevs
);
767 IMMDeviceEnumerator_Release(Enumerator
);
771 if(--deviceCount
== 0 && SUCCEEDED(cohr
))
775 SetEvent(req
->FinishedEvt
);
779 ERR("Unexpected message: %u\n", msg
.message
);
783 TRACE("Message loop finished\n");
789 static BOOL
MMDevApiLoad(void)
791 static HRESULT InitResult
;
797 req
.FinishedEvt
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
798 if(req
.FinishedEvt
== NULL
)
799 ERR("Failed to create event: %lu\n", GetLastError());
802 ThreadHdl
= CreateThread(NULL
, 0, MMDevApiMsgProc
, &req
, 0, &ThreadID
);
803 if(ThreadHdl
!= NULL
)
804 InitResult
= WaitForResponse(&req
);
805 CloseHandle(req
.FinishedEvt
);
808 return SUCCEEDED(InitResult
);
812 static ALCenum
MMDevApiOpenPlayback(ALCdevice
*device
, const ALCchar
*deviceName
)
814 MMDevApiData
*data
= NULL
;
817 //Initialise requested device
818 data
= calloc(1, sizeof(MMDevApiData
));
820 return ALC_OUT_OF_MEMORY
;
821 device
->ExtraData
= data
;
824 data
->NotifyEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
825 data
->MsgEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
826 if(data
->NotifyEvent
== NULL
|| data
->MsgEvent
== NULL
)
835 if(!PlaybackDeviceList
)
837 ThreadRequest req
= { data
->MsgEvent
, 0 };
838 if(PostThreadMessage(ThreadID
, WM_USER_Enumerate
, (WPARAM
)&req
, ALL_DEVICE_PROBE
))
839 (void)WaitForResponse(&req
);
843 for(i
= 0;i
< NumPlaybackDevices
;i
++)
845 if(strcmp(deviceName
, PlaybackDeviceList
[i
].name
) == 0)
847 data
->devid
= strdupW(PlaybackDeviceList
[i
].devid
);
857 ThreadRequest req
= { data
->MsgEvent
, 0 };
860 if(PostThreadMessage(ThreadID
, WM_USER_OpenDevice
, (WPARAM
)&req
, (LPARAM
)device
))
861 hr
= WaitForResponse(&req
);
866 if(data
->NotifyEvent
!= NULL
)
867 CloseHandle(data
->NotifyEvent
);
868 data
->NotifyEvent
= NULL
;
869 if(data
->MsgEvent
!= NULL
)
870 CloseHandle(data
->MsgEvent
);
871 data
->MsgEvent
= NULL
;
874 device
->ExtraData
= NULL
;
876 ERR("Device init failed: 0x%08lx\n", hr
);
877 return ALC_INVALID_VALUE
;
883 static void MMDevApiClosePlayback(ALCdevice
*device
)
885 MMDevApiData
*data
= device
->ExtraData
;
886 ThreadRequest req
= { data
->MsgEvent
, 0 };
888 if(PostThreadMessage(ThreadID
, WM_USER_CloseDevice
, (WPARAM
)&req
, (LPARAM
)device
))
889 (void)WaitForResponse(&req
);
891 CloseHandle(data
->MsgEvent
);
892 data
->MsgEvent
= NULL
;
894 CloseHandle(data
->NotifyEvent
);
895 data
->NotifyEvent
= NULL
;
901 device
->ExtraData
= NULL
;
904 static ALCboolean
MMDevApiResetPlayback(ALCdevice
*device
)
906 MMDevApiData
*data
= device
->ExtraData
;
907 ThreadRequest req
= { data
->MsgEvent
, 0 };
910 if(PostThreadMessage(ThreadID
, WM_USER_ResetDevice
, (WPARAM
)&req
, (LPARAM
)device
))
911 hr
= WaitForResponse(&req
);
913 return SUCCEEDED(hr
) ? ALC_TRUE
: ALC_FALSE
;
916 static ALCboolean
MMDevApiStartPlayback(ALCdevice
*device
)
918 MMDevApiData
*data
= device
->ExtraData
;
919 ThreadRequest req
= { data
->MsgEvent
, 0 };
922 if(PostThreadMessage(ThreadID
, WM_USER_StartDevice
, (WPARAM
)&req
, (LPARAM
)device
))
923 hr
= WaitForResponse(&req
);
925 return SUCCEEDED(hr
) ? ALC_TRUE
: ALC_FALSE
;
928 static void MMDevApiStopPlayback(ALCdevice
*device
)
930 MMDevApiData
*data
= device
->ExtraData
;
931 ThreadRequest req
= { data
->MsgEvent
, 0 };
933 if(PostThreadMessage(ThreadID
, WM_USER_StopDevice
, (WPARAM
)&req
, (LPARAM
)device
))
934 (void)WaitForResponse(&req
);
938 static ALint64
MMDevApiGetLatency(ALCdevice
*device
)
940 MMDevApiData
*data
= device
->ExtraData
;
942 return (ALint64
)data
->Padding
* 1000000000 / device
->Frequency
;
946 static const BackendFuncs MMDevApiFuncs
= {
947 MMDevApiOpenPlayback
,
948 MMDevApiClosePlayback
,
949 MMDevApiResetPlayback
,
950 MMDevApiStartPlayback
,
951 MMDevApiStopPlayback
,
958 ALCdevice_LockDefault
,
959 ALCdevice_UnlockDefault
,
964 ALCboolean
alcMMDevApiInit(BackendFuncs
*FuncList
)
968 *FuncList
= MMDevApiFuncs
;
972 void alcMMDevApiDeinit(void)
976 for(i
= 0;i
< NumPlaybackDevices
;i
++)
978 free(PlaybackDeviceList
[i
].name
);
979 free(PlaybackDeviceList
[i
].devid
);
981 free(PlaybackDeviceList
);
982 PlaybackDeviceList
= NULL
;
983 NumPlaybackDevices
= 0;
985 for(i
= 0;i
< NumCaptureDevices
;i
++)
987 free(CaptureDeviceList
[i
].name
);
988 free(CaptureDeviceList
[i
].devid
);
990 free(CaptureDeviceList
);
991 CaptureDeviceList
= NULL
;
992 NumCaptureDevices
= 0;
996 TRACE("Sending WM_QUIT to Thread %04lx\n", ThreadID
);
997 PostThreadMessage(ThreadID
, WM_QUIT
, 0, 0);
998 CloseHandle(ThreadHdl
);
1003 void alcMMDevApiProbe(enum DevProbe type
)
1005 ThreadRequest req
= { NULL
, 0 };
1006 HRESULT hr
= E_FAIL
;
1010 case ALL_DEVICE_PROBE
:
1011 req
.FinishedEvt
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1012 if(req
.FinishedEvt
== NULL
)
1013 ERR("Failed to create event: %lu\n", GetLastError());
1014 else if(PostThreadMessage(ThreadID
, WM_USER_Enumerate
, (WPARAM
)&req
, type
))
1015 hr
= WaitForResponse(&req
);
1019 for(i
= 0;i
< NumPlaybackDevices
;i
++)
1021 if(PlaybackDeviceList
[i
].name
)
1022 AppendAllDevicesList(PlaybackDeviceList
[i
].name
);
1027 case CAPTURE_DEVICE_PROBE
:
1030 if(req
.FinishedEvt
!= NULL
)
1031 CloseHandle(req
.FinishedEvt
);
1032 req
.FinishedEvt
= NULL
;