2 * Copyright 2009 Maarten Lankhorst
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 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 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #elif defined(HAVE_OPENAL_AL_H)
27 #include <OpenAL/al.h>
28 #include <OpenAL/alc.h>
31 #define NONAMELESSUNION
37 #include "wine/debug.h"
38 #include "wine/unicode.h"
41 #include "mmdeviceapi.h"
44 #include "audioclient.h"
45 #include "endpointvolume.h"
46 #include "audiopolicy.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(mmdevapi);
53 static const WCHAR software_mmdevapi[] =
54 { 'S','o','f','t','w','a','r','e','\\',
55 'M','i','c','r','o','s','o','f','t','\\',
56 'W','i','n','d','o','w','s','\\',
57 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
58 'M','M','D','e','v','i','c','e','s','\\',
59 'A','u','d','i','o',0};
60 static const WCHAR reg_render[] =
61 { 'R','e','n','d','e','r',0 };
62 static const WCHAR reg_capture[] =
63 { 'C','a','p','t','u','r','e',0 };
64 static const WCHAR reg_devicestate[] =
65 { 'D','e','v','i','c','e','S','t','a','t','e',0 };
66 static const WCHAR reg_properties[] =
67 { 'P','r','o','p','e','r','t','i','e','s',0 };
69 static HKEY key_render;
70 static HKEY key_capture;
72 typedef struct MMDevPropStoreImpl
74 const IPropertyStoreVtbl *lpVtbl;
80 typedef struct MMDevEnumImpl
82 const IMMDeviceEnumeratorVtbl *lpVtbl;
86 static MMDevEnumImpl *MMDevEnumerator;
87 static MMDevice **MMDevice_head;
88 static MMDevice *MMDevice_def_rec, *MMDevice_def_play;
89 static DWORD MMDevice_count;
90 static const IMMDeviceEnumeratorVtbl MMDevEnumVtbl;
91 static const IMMDeviceCollectionVtbl MMDevColVtbl;
92 static const IMMDeviceVtbl MMDeviceVtbl;
93 static const IPropertyStoreVtbl MMDevPropVtbl;
94 static const IMMEndpointVtbl MMEndpointVtbl;
96 typedef struct MMDevColImpl
98 const IMMDeviceCollectionVtbl *lpVtbl;
104 typedef struct IPropertyBagImpl {
105 const IPropertyBagVtbl *lpVtbl;
108 static const IPropertyBagVtbl PB_Vtbl;
110 static HRESULT MMDevPropStore_Create(MMDevice *This, DWORD access, IPropertyStore **ppv);
112 /* Creates or updates the state of a device
113 * If GUID is null, a random guid will be assigned
114 * and the device will be created
116 static void MMDevice_Create(MMDevice **dev, WCHAR *name, GUID *id, EDataFlow flow, DWORD state, BOOL setdefault)
123 for (i = 0; i < MMDevice_count; ++i)
125 cur = MMDevice_head[i];
126 if (cur->flow == flow && !lstrcmpW(cur->alname, name))
129 /* Same device, update state */
131 StringFromGUID2(&cur->devguid, guidstr, sizeof(guidstr)/sizeof(*guidstr));
132 ret = RegOpenKeyExW(flow == eRender ? key_render : key_capture, guidstr, 0, KEY_WRITE, &key);
133 if (ret == ERROR_SUCCESS)
135 RegSetValueExW(key, reg_devicestate, 0, REG_DWORD, (const BYTE*)&state, sizeof(DWORD));
142 /* No device found, allocate new one */
143 cur = HeapAlloc(GetProcessHeap(), 0, sizeof(*cur));
146 cur->alname = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(name)+1)*sizeof(WCHAR));
149 HeapFree(GetProcessHeap(), 0, cur);
152 lstrcpyW(cur->alname, name);
153 cur->lpVtbl = &MMDeviceVtbl;
154 cur->lpEndpointVtbl = &MMEndpointVtbl;
156 InitializeCriticalSection(&cur->crst);
157 cur->crst.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": MMDevice.crst");
167 StringFromGUID2(id, guidstr, sizeof(guidstr)/sizeof(*guidstr));
172 if (!RegCreateKeyExW(root, guidstr, 0, NULL, 0, KEY_WRITE|KEY_READ, NULL, &key, NULL))
175 RegSetValueExW(key, reg_devicestate, 0, REG_DWORD, (const BYTE*)&state, sizeof(DWORD));
176 if (!RegCreateKeyExW(key, reg_properties, 0, NULL, 0, KEY_WRITE|KEY_READ, NULL, &keyprop, NULL))
181 MMDevice_SetPropValue(id, flow, (const PROPERTYKEY*)&DEVPKEY_Device_FriendlyName, &pv);
182 MMDevice_SetPropValue(id, flow, (const PROPERTYKEY*)&DEVPKEY_Device_DeviceDesc, &pv);
183 RegCloseKey(keyprop);
188 MMDevice_head = HeapAlloc(GetProcessHeap(), 0, sizeof(*MMDevice_head));
190 MMDevice_head = HeapReAlloc(GetProcessHeap(), 0, MMDevice_head, sizeof(*MMDevice_head)*(1+MMDevice_count));
191 MMDevice_head[MMDevice_count++] = cur;
197 MMDevice_def_play = cur;
199 MMDevice_def_rec = cur;
205 static void MMDevice_Destroy(MMDevice *This)
208 TRACE("Freeing %s\n", debugstr_w(This->alname));
209 /* Since this function is called at destruction time, reordering of the list is unimportant */
210 for (i = 0; i < MMDevice_count; ++i)
212 if (MMDevice_head[i] == This)
214 MMDevice_head[i] = MMDevice_head[--MMDevice_count];
220 palcCloseDevice(This->device);
222 This->crst.DebugInfo->Spare[0] = 0;
223 DeleteCriticalSection(&This->crst);
224 HeapFree(GetProcessHeap(), 0, This->alname);
225 HeapFree(GetProcessHeap(), 0, This);
228 static HRESULT WINAPI MMDevice_QueryInterface(IMMDevice *iface, REFIID riid, void **ppv)
230 MMDevice *This = (MMDevice *)iface;
231 TRACE("(%p)->(%s,%p)\n", iface, debugstr_guid(riid), ppv);
236 if (IsEqualIID(riid, &IID_IUnknown)
237 || IsEqualIID(riid, &IID_IMMDevice))
239 else if (IsEqualIID(riid, &IID_IMMEndpoint))
240 *ppv = &This->lpEndpointVtbl;
243 IUnknown_AddRef((IUnknown*)*ppv);
246 WARN("Unknown interface %s\n", debugstr_guid(riid));
247 return E_NOINTERFACE;
250 static ULONG WINAPI MMDevice_AddRef(IMMDevice *iface)
252 MMDevice *This = (MMDevice *)iface;
255 ref = InterlockedIncrement(&This->ref);
256 TRACE("Refcount now %i\n", ref);
260 static ULONG WINAPI MMDevice_Release(IMMDevice *iface)
262 MMDevice *This = (MMDevice *)iface;
265 ref = InterlockedDecrement(&This->ref);
266 TRACE("Refcount now %i\n", ref);
270 static HRESULT WINAPI MMDevice_Activate(IMMDevice *iface, REFIID riid, DWORD clsctx, PROPVARIANT *params, void **ppv)
272 HRESULT hr = E_NOINTERFACE;
275 MMDevice *This = (MMDevice *)iface;
277 TRACE("(%p)->(%p,%x,%p,%p)\n", iface, riid, clsctx, params, ppv);
284 WARN("OpenAL is still not loaded\n");
285 hr = AUDCLNT_E_SERVICE_NOT_RUNNING;
287 else if (IsEqualIID(riid, &IID_IAudioClient))
288 hr = AudioClient_Create(This, (IAudioClient**)ppv);
289 else if (IsEqualIID(riid, &IID_IAudioEndpointVolume))
290 hr = AudioEndpointVolume_Create(This, (IAudioEndpointVolume**)ppv);
291 else if (IsEqualIID(riid, &IID_IAudioSessionManager)
292 || IsEqualIID(riid, &IID_IAudioSessionManager2))
294 FIXME("IID_IAudioSessionManager unsupported\n");
296 else if (IsEqualIID(riid, &IID_IBaseFilter))
298 if (This->flow == eRender)
299 hr = CoCreateInstance(&CLSID_DSoundRender, NULL, clsctx, riid, ppv);
301 ERR("Not supported for recording?\n");
304 IPersistPropertyBag *ppb;
305 hr = IUnknown_QueryInterface((IUnknown*)*ppv, &IID_IPersistPropertyBag, (void*)&ppb);
308 /* ::Load cannot assume the interface stays alive after the function returns,
309 * so just create the interface on the stack, saves a lot of complicated code */
310 IPropertyBagImpl bag = { &PB_Vtbl, This->devguid };
311 hr = IPersistPropertyBag_Load(ppb, (IPropertyBag*)&bag, NULL);
312 IPersistPropertyBag_Release(ppb);
314 IBaseFilter_Release((IBaseFilter*)*ppv);
318 FIXME("Wine doesn't support IPersistPropertyBag on DSoundRender yet, ignoring..\n");
323 else if (IsEqualIID(riid, &IID_IDeviceTopology))
325 FIXME("IID_IDeviceTopology unsupported\n");
327 else if (IsEqualIID(riid, &IID_IDirectSound)
328 || IsEqualIID(riid, &IID_IDirectSound8))
330 if (This->flow == eRender)
331 hr = CoCreateInstance(&CLSID_DirectSound8, NULL, clsctx, riid, ppv);
334 hr = IDirectSound_Initialize((IDirectSound*)*ppv, &This->devguid);
336 IDirectSound_Release((IDirectSound*)*ppv);
339 else if (IsEqualIID(riid, &IID_IDirectSoundCapture)
340 || IsEqualIID(riid, &IID_IDirectSoundCapture8))
342 if (This->flow == eCapture)
343 hr = CoCreateInstance(&CLSID_DirectSoundCapture8, NULL, clsctx, riid, ppv);
346 hr = IDirectSoundCapture_Initialize((IDirectSoundCapture*)*ppv, &This->devguid);
348 IDirectSoundCapture_Release((IDirectSoundCapture*)*ppv);
352 ERR("Invalid/unknown iid %s\n", debugstr_guid(riid));
354 if (!ppv) return E_POINTER;
355 hr = AUDCLNT_E_SERVICE_NOT_RUNNING;
361 TRACE("Returning %08x\n", hr);
365 static HRESULT WINAPI MMDevice_OpenPropertyStore(IMMDevice *iface, DWORD access, IPropertyStore **ppv)
367 MMDevice *This = (MMDevice *)iface;
368 TRACE("(%p)->(%x,%p)\n", This, access, ppv);
372 return MMDevPropStore_Create(This, access, ppv);
375 static HRESULT WINAPI MMDevice_GetId(IMMDevice *iface, WCHAR **itemid)
377 MMDevice *This = (MMDevice *)iface;
379 GUID *id = &This->devguid;
380 static const WCHAR formatW[] = { '{','0','.','0','.','0','.','0','0','0','0','0','0','0','0','}','.',
381 '{','%','0','8','X','-','%','0','4','X','-',
382 '%','0','4','X','-','%','0','2','X','%','0','2','X','-',
383 '%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X',
384 '%','0','2','X','%','0','2','X','}',0 };
386 TRACE("(%p)->(%p)\n", This, itemid);
389 *itemid = str = CoTaskMemAlloc(56 * sizeof(WCHAR));
391 return E_OUTOFMEMORY;
392 wsprintfW( str, formatW, id->Data1, id->Data2, id->Data3,
393 id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
394 id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7] );
398 static HRESULT WINAPI MMDevice_GetState(IMMDevice *iface, DWORD *state)
400 MMDevice *This = (MMDevice *)iface;
401 TRACE("(%p)->(%p)\n", iface, state);
405 *state = This->state;
409 static const IMMDeviceVtbl MMDeviceVtbl =
411 MMDevice_QueryInterface,
415 MMDevice_OpenPropertyStore,
420 static MMDevice *get_this_from_endpoint(IMMEndpoint *iface)
422 return (MMDevice*)((char*)iface - offsetof(MMDevice,lpEndpointVtbl));
425 static HRESULT WINAPI MMEndpoint_QueryInterface(IMMEndpoint *iface, REFIID riid, void **ppv)
427 MMDevice *This = get_this_from_endpoint(iface);
428 return IMMDevice_QueryInterface((IMMDevice*)This, riid, ppv);
431 static ULONG WINAPI MMEndpoint_AddRef(IMMEndpoint *iface)
433 MMDevice *This = get_this_from_endpoint(iface);
434 return IMMDevice_AddRef((IMMDevice*)This);
437 static ULONG WINAPI MMEndpoint_Release(IMMEndpoint *iface)
439 MMDevice *This = get_this_from_endpoint(iface);
440 return IMMDevice_Release((IMMDevice*)This);
443 static HRESULT WINAPI MMEndpoint_GetDataFlow(IMMEndpoint *iface, EDataFlow *flow)
445 MMDevice *This = get_this_from_endpoint(iface);
452 static const IMMEndpointVtbl MMEndpointVtbl =
454 MMEndpoint_QueryInterface,
457 MMEndpoint_GetDataFlow
460 static HRESULT MMDevCol_Create(IMMDeviceCollection **ppv, EDataFlow flow, DWORD state)
464 This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
467 return E_OUTOFMEMORY;
468 This->lpVtbl = &MMDevColVtbl;
472 *ppv = (IMMDeviceCollection*)This;
476 static void MMDevCol_Destroy(MMDevColImpl *This)
478 HeapFree(GetProcessHeap(), 0, This);
481 static HRESULT WINAPI MMDevCol_QueryInterface(IMMDeviceCollection *iface, REFIID riid, void **ppv)
483 MMDevColImpl *This = (MMDevColImpl*)iface;
487 if (IsEqualIID(riid, &IID_IUnknown)
488 || IsEqualIID(riid, &IID_IMMDeviceCollection))
493 return E_NOINTERFACE;
494 IUnknown_AddRef((IUnknown*)*ppv);
498 static ULONG WINAPI MMDevCol_AddRef(IMMDeviceCollection *iface)
500 MMDevColImpl *This = (MMDevColImpl*)iface;
501 LONG ref = InterlockedIncrement(&This->ref);
502 TRACE("Refcount now %i\n", ref);
506 static ULONG WINAPI MMDevCol_Release(IMMDeviceCollection *iface)
508 MMDevColImpl *This = (MMDevColImpl*)iface;
509 LONG ref = InterlockedDecrement(&This->ref);
510 TRACE("Refcount now %i\n", ref);
512 MMDevCol_Destroy(This);
516 static HRESULT WINAPI MMDevCol_GetCount(IMMDeviceCollection *iface, UINT *numdevs)
518 MMDevColImpl *This = (MMDevColImpl*)iface;
521 TRACE("(%p)->(%p)\n", This, numdevs);
526 for (i = 0; i < MMDevice_count; ++i)
528 MMDevice *cur = MMDevice_head[i];
529 if ((cur->flow == This->flow || This->flow == eAll)
530 && (cur->state & This->state))
536 static HRESULT WINAPI MMDevCol_Item(IMMDeviceCollection *iface, UINT n, IMMDevice **dev)
538 MMDevColImpl *This = (MMDevColImpl*)iface;
541 TRACE("(%p)->(%u, %p)\n", This, n, dev);
545 for (j = 0; j < MMDevice_count; ++j)
547 MMDevice *cur = MMDevice_head[j];
548 if ((cur->flow == This->flow || This->flow == eAll)
549 && (cur->state & This->state)
552 *dev = (IMMDevice *)cur;
553 IMMDevice_AddRef(*dev);
557 WARN("Could not obtain item %u\n", n);
562 static const IMMDeviceCollectionVtbl MMDevColVtbl =
564 MMDevCol_QueryInterface,
571 static const WCHAR propkey_formatW[] = {
572 '{','%','0','8','X','-','%','0','4','X','-',
573 '%','0','4','X','-','%','0','2','X','%','0','2','X','-',
574 '%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X',
575 '%','0','2','X','%','0','2','X','}',',','%','d',0 };
577 static HRESULT MMDevPropStore_OpenPropKey(const GUID *guid, DWORD flow, HKEY *propkey)
582 StringFromGUID2(guid, buffer, 39);
583 if ((ret = RegOpenKeyExW(flow == eRender ? key_render : key_capture, buffer, 0, KEY_READ|KEY_WRITE, &key)) != ERROR_SUCCESS)
585 WARN("Opening key %s failed with %u\n", debugstr_w(buffer), ret);
588 ret = RegOpenKeyExW(key, reg_properties, 0, KEY_READ|KEY_WRITE, propkey);
590 if (ret != ERROR_SUCCESS)
592 WARN("Opening key %s failed with %u\n", debugstr_w(reg_properties), ret);
598 HRESULT MMDevice_GetPropValue(const GUID *devguid, DWORD flow, REFPROPERTYKEY key, PROPVARIANT *pv)
601 const GUID *id = &key->fmtid;
607 hr = MMDevPropStore_OpenPropKey(devguid, flow, ®key);
610 wsprintfW( buffer, propkey_formatW, id->Data1, id->Data2, id->Data3,
611 id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
612 id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7], key->pid );
613 ret = RegGetValueW(regkey, NULL, buffer, RRF_RT_ANY, &type, NULL, &size);
614 if (ret != ERROR_SUCCESS)
616 WARN("Reading %s returned %d\n", debugstr_w(buffer), ret);
618 PropVariantClear(pv);
627 pv->u.pwszVal = CoTaskMemAlloc(size);
631 RegGetValueW(regkey, NULL, buffer, RRF_RT_REG_SZ, NULL, (BYTE*)pv->u.pwszVal, &size);
637 RegGetValueW(regkey, NULL, buffer, RRF_RT_REG_DWORD, NULL, (BYTE*)&pv->u.ulVal, &size);
643 pv->u.blob.cbSize = size;
644 pv->u.blob.pBlobData = CoTaskMemAlloc(size);
645 if (!pv->u.blob.pBlobData)
648 RegGetValueW(regkey, NULL, buffer, RRF_RT_REG_BINARY, NULL, (BYTE*)pv->u.blob.pBlobData, &size);
652 ERR("Unknown/unhandled type: %u\n", type);
653 PropVariantClear(pv);
660 HRESULT MMDevice_SetPropValue(const GUID *devguid, DWORD flow, REFPROPERTYKEY key, REFPROPVARIANT pv)
663 const GUID *id = &key->fmtid;
668 hr = MMDevPropStore_OpenPropKey(devguid, flow, ®key);
671 wsprintfW( buffer, propkey_formatW, id->Data1, id->Data2, id->Data3,
672 id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
673 id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7], key->pid );
678 ret = RegSetValueExW(regkey, buffer, 0, REG_DWORD, (const BYTE*)&pv->u.ulVal, sizeof(DWORD));
683 ret = RegSetValueExW(regkey, buffer, 0, REG_BINARY, pv->u.blob.pBlobData, pv->u.blob.cbSize);
684 TRACE("Blob %p %u\n", pv->u.blob.pBlobData, pv->u.blob.cbSize);
690 ret = RegSetValueExW(regkey, buffer, 0, REG_SZ, (const BYTE*)pv->u.pwszVal, sizeof(WCHAR)*(1+lstrlenW(pv->u.pwszVal)));
695 FIXME("Unhandled type %u\n", pv->vt);
700 TRACE("Writing %s returned %u\n", debugstr_w(buffer), ret);
706 static void openal_setformat(MMDevice *This, DWORD freq)
709 PROPVARIANT pv = { VT_EMPTY };
711 hr = MMDevice_GetPropValue(&This->devguid, This->flow, &PKEY_AudioEngine_DeviceFormat, &pv);
712 if (SUCCEEDED(hr) && pv.vt == VT_BLOB)
715 pwfx = (WAVEFORMATEX*)pv.u.blob.pBlobData;
716 if (pwfx->nSamplesPerSec != freq)
718 pwfx->nSamplesPerSec = freq;
719 pwfx->nAvgBytesPerSec = freq * pwfx->nBlockAlign;
720 MMDevice_SetPropValue(&This->devguid, This->flow, &PKEY_AudioEngine_DeviceFormat, &pv);
726 WAVEFORMATEXTENSIBLE wfxe;
728 wfxe.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
729 wfxe.Format.nChannels = 2;
730 wfxe.Format.wBitsPerSample = 32;
731 wfxe.Format.nBlockAlign = wfxe.Format.nChannels * wfxe.Format.wBitsPerSample/8;
732 wfxe.Format.nSamplesPerSec = freq;
733 wfxe.Format.nAvgBytesPerSec = wfxe.Format.nSamplesPerSec * wfxe.Format.nBlockAlign;
734 wfxe.Format.cbSize = sizeof(wfxe)-sizeof(WAVEFORMATEX);
735 wfxe.Samples.wValidBitsPerSample = 32;
736 wfxe.SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
737 wfxe.dwChannelMask = SPEAKER_FRONT_LEFT|SPEAKER_FRONT_RIGHT;
740 pv.u.blob.cbSize = sizeof(wfxe);
741 pv.u.blob.pBlobData = (BYTE*)&wfxe;
742 MMDevice_SetPropValue(&This->devguid, This->flow, &PKEY_AudioEngine_DeviceFormat, &pv);
743 MMDevice_SetPropValue(&This->devguid, This->flow, &PKEY_AudioEngine_OEMFormat, &pv);
747 static int blacklist_pulse;
749 static int blacklist(const char *dev) {
751 if (!strncmp(dev, "OSS ", 4))
754 if (blacklist_pulse && !strncmp(dev, "PulseAudio ", 11))
756 if (!strncmp(dev, "ALSA ", 5) && strstr(dev, "hw:"))
758 if (!strncmp(dev, "PortAudio ", 10))
763 static void pulse_fixup(const char *devstr, const char **defstr, int render) {
767 if (render && !blacklist_pulse && !local_contexts)
770 if (!blacklist_pulse || !devstr || !*devstr)
773 default_pulse = !strncmp(*defstr, "PulseAudio ", 11);
775 while (*devstr && !strncmp(devstr, "PulseAudio ", 11))
776 devstr += strlen(devstr) + 1;
778 /* Could still be a newer version, so check for 1.11 if more devices are enabled */
779 if (render && *devstr) {
780 ALCdevice *dev = palcOpenDevice(devstr);
781 ALCcontext *ctx = palcCreateContext(dev, NULL);
786 ver = palGetString(AL_VERSION);
788 palcDestroyContext(ctx);
790 if (!strcmp(ver, "1.1 ALSOFT 1.11.753")) {
792 palcCloseDevice(dev);
797 palcCloseDevice(dev);
801 ERR("Disabling pulseaudio because of old openal version\n");
802 ERR("Please upgrade to openal-soft v1.12 or newer\n");
804 TRACE("New default: %s\n", devstr);
809 static void openal_scanrender(void)
811 WCHAR name[MAX_PATH];
813 const ALCchar *devstr, *defaultstr;
815 EnterCriticalSection(&openal_crst);
816 if (palcIsExtensionPresent(NULL, "ALC_ENUMERATE_ALL_EXT")) {
817 defaultstr = palcGetString(NULL, ALC_DEFAULT_ALL_DEVICES_SPECIFIER);
818 devstr = palcGetString(NULL, ALC_ALL_DEVICES_SPECIFIER);
820 defaultstr = palcGetString(NULL, ALC_DEFAULT_DEVICE_SPECIFIER);
821 devstr = palcGetString(NULL, ALC_DEVICE_SPECIFIER);
823 pulse_fixup(devstr, &defaultstr, 1);
824 defblacklisted = blacklist(defaultstr);
826 WARN("Disabling blacklist because %s is blacklisted\n", defaultstr);
828 for (; *devstr; devstr += strlen(devstr)+1) {
830 MultiByteToWideChar( CP_UNIXCP, 0, devstr, -1,
831 name, sizeof(name)/sizeof(*name)-1 );
832 name[sizeof(name)/sizeof(*name)-1] = 0;
833 /* Only enable blacklist if the default device isn't blacklisted */
834 if (!defblacklisted && blacklist(devstr)) {
835 WARN("Not adding %s: device is blacklisted\n", devstr);
838 TRACE("Adding %s\n", devstr);
839 dev = palcOpenDevice(devstr);
840 MMDevice_Create(&mmdev, name, NULL, eRender, dev ? DEVICE_STATE_ACTIVE : DEVICE_STATE_NOTPRESENT, !strcmp(devstr, defaultstr));
844 palcGetIntegerv(dev, ALC_FREQUENCY, 1, &freq);
845 openal_setformat(mmdev, freq);
846 palcCloseDevice(dev);
849 WARN("Could not open device: %04x\n", palcGetError(NULL));
851 LeaveCriticalSection(&openal_crst);
854 static void openal_scancapture(void)
856 WCHAR name[MAX_PATH];
858 const ALCchar *devstr, *defaultstr;
861 EnterCriticalSection(&openal_crst);
862 devstr = palcGetString(NULL, ALC_CAPTURE_DEVICE_SPECIFIER);
863 defaultstr = palcGetString(NULL, ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER);
864 pulse_fixup(devstr, &defaultstr, 0);
865 defblacklisted = blacklist(defaultstr);
867 WARN("Disabling blacklist because %s is blacklisted\n", defaultstr);
868 if (devstr && *devstr)
869 for (; *devstr; devstr += strlen(devstr)+1) {
872 MultiByteToWideChar( CP_UNIXCP, 0, devstr, -1,
873 name, sizeof(name)/sizeof(*name)-1 );
874 name[sizeof(name)/sizeof(*name)-1] = 0;
875 if (!defblacklisted && blacklist(devstr)) {
876 WARN("Not adding %s: device is blacklisted\n", devstr);
879 TRACE("Adding %s\n", devstr);
880 dev = palcCaptureOpenDevice(devstr, freq, AL_FORMAT_MONO16, 65536);
881 MMDevice_Create(&mmdev, name, NULL, eCapture, dev ? DEVICE_STATE_ACTIVE : DEVICE_STATE_NOTPRESENT, !strcmp(devstr, defaultstr));
883 openal_setformat(mmdev, freq);
884 palcCaptureCloseDevice(dev);
886 WARN("Could not open device: %04x\n", palcGetError(NULL));
888 LeaveCriticalSection(&openal_crst);
890 #endif /*HAVE_OPENAL*/
892 HRESULT MMDevEnum_Create(REFIID riid, void **ppv)
894 MMDevEnumImpl *This = MMDevEnumerator;
903 This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
906 return E_OUTOFMEMORY;
908 This->lpVtbl = &MMDevEnumVtbl;
909 MMDevEnumerator = This;
911 ret = RegCreateKeyExW(HKEY_LOCAL_MACHINE, software_mmdevapi, 0, NULL, 0, KEY_WRITE|KEY_READ, NULL, &root, NULL);
912 if (ret == ERROR_SUCCESS)
913 ret = RegCreateKeyExW(root, reg_capture, 0, NULL, 0, KEY_READ|KEY_WRITE, NULL, &key_capture, NULL);
914 if (ret == ERROR_SUCCESS)
915 ret = RegCreateKeyExW(root, reg_render, 0, NULL, 0, KEY_READ|KEY_WRITE, NULL, &key_render, NULL);
919 if (ret != ERROR_SUCCESS)
921 RegCloseKey(key_capture);
922 key_render = key_capture = NULL;
923 WARN("Couldn't create key: %u\n", ret);
930 PROPVARIANT pv = { VT_EMPTY };
932 len = sizeof(guidvalue);
933 ret = RegEnumKeyExW(cur, i++, guidvalue, &len, NULL, NULL, NULL, NULL);
934 if (ret == ERROR_NO_MORE_ITEMS)
936 if (cur == key_capture)
945 if (ret != ERROR_SUCCESS)
947 if (SUCCEEDED(CLSIDFromString(guidvalue, &guid))
948 && SUCCEEDED(MMDevice_GetPropValue(&guid, curflow, (const PROPERTYKEY*)&DEVPKEY_Device_FriendlyName, &pv))
949 && pv.vt == VT_LPWSTR)
951 MMDevice_Create(NULL, pv.u.pwszVal, &guid, curflow,
952 DEVICE_STATE_NOTPRESENT, FALSE);
953 CoTaskMemFree(pv.u.pwszVal);
960 openal_scancapture();
963 FIXME("OpenAL support not enabled, application will not find sound devices\n");
965 ERR("OpenAL support not compiled in, application will not find sound devices\n");
966 #endif /*HAVE_OPENAL*/
968 return IUnknown_QueryInterface((IUnknown*)This, riid, ppv);
971 void MMDevEnum_Free(void)
973 while (MMDevice_count)
974 MMDevice_Destroy(MMDevice_head[0]);
975 RegCloseKey(key_render);
976 RegCloseKey(key_capture);
977 key_render = key_capture = NULL;
978 HeapFree(GetProcessHeap(), 0, MMDevEnumerator);
979 MMDevEnumerator = NULL;
982 static HRESULT WINAPI MMDevEnum_QueryInterface(IMMDeviceEnumerator *iface, REFIID riid, void **ppv)
984 MMDevEnumImpl *This = (MMDevEnumImpl*)iface;
988 if (IsEqualIID(riid, &IID_IUnknown)
989 || IsEqualIID(riid, &IID_IMMDeviceEnumerator))
994 return E_NOINTERFACE;
995 IUnknown_AddRef((IUnknown*)*ppv);
999 static ULONG WINAPI MMDevEnum_AddRef(IMMDeviceEnumerator *iface)
1001 MMDevEnumImpl *This = (MMDevEnumImpl*)iface;
1002 LONG ref = InterlockedIncrement(&This->ref);
1003 TRACE("Refcount now %i\n", ref);
1007 static ULONG WINAPI MMDevEnum_Release(IMMDeviceEnumerator *iface)
1009 MMDevEnumImpl *This = (MMDevEnumImpl*)iface;
1010 LONG ref = InterlockedDecrement(&This->ref);
1013 TRACE("Refcount now %i\n", ref);
1017 static HRESULT WINAPI MMDevEnum_EnumAudioEndpoints(IMMDeviceEnumerator *iface, EDataFlow flow, DWORD mask, IMMDeviceCollection **devices)
1019 MMDevEnumImpl *This = (MMDevEnumImpl*)iface;
1020 TRACE("(%p)->(%u,%u,%p)\n", This, flow, mask, devices);
1024 if (flow >= EDataFlow_enum_count)
1025 return E_INVALIDARG;
1026 if (mask & ~DEVICE_STATEMASK_ALL)
1027 return E_INVALIDARG;
1028 return MMDevCol_Create(devices, flow, mask);
1031 static HRESULT WINAPI MMDevEnum_GetDefaultAudioEndpoint(IMMDeviceEnumerator *iface, EDataFlow flow, ERole role, IMMDevice **device)
1033 MMDevEnumImpl *This = (MMDevEnumImpl*)iface;
1034 TRACE("(%p)->(%u,%u,%p)\n", This, flow, role, device);
1040 if (flow == eRender)
1041 *device = (IMMDevice*)MMDevice_def_play;
1042 else if (flow == eCapture)
1043 *device = (IMMDevice*)MMDevice_def_rec;
1046 WARN("Unknown flow %u\n", flow);
1047 return E_INVALIDARG;
1052 IMMDevice_AddRef(*device);
1056 static HRESULT WINAPI MMDevEnum_GetDevice(IMMDeviceEnumerator *iface, const WCHAR *name, IMMDevice **device)
1058 MMDevEnumImpl *This = (MMDevEnumImpl*)iface;
1060 IMMDevice *dev = NULL;
1062 TRACE("(%p)->(%s,%p)\n", This, debugstr_w(name), device);
1063 for (i = 0; i < MMDevice_count; ++i)
1066 dev = (IMMDevice*)MMDevice_head[i];
1067 IMMDevice_GetId(dev, &str);
1069 if (str && !lstrcmpW(str, name))
1078 IUnknown_AddRef(dev);
1082 WARN("Could not find device %s\n", debugstr_w(name));
1086 static HRESULT WINAPI MMDevEnum_RegisterEndpointNotificationCallback(IMMDeviceEnumerator *iface, IMMNotificationClient *client)
1088 MMDevEnumImpl *This = (MMDevEnumImpl*)iface;
1089 TRACE("(%p)->(%p)\n", This, client);
1094 static HRESULT WINAPI MMDevEnum_UnregisterEndpointNotificationCallback(IMMDeviceEnumerator *iface, IMMNotificationClient *client)
1096 MMDevEnumImpl *This = (MMDevEnumImpl*)iface;
1097 TRACE("(%p)->(%p)\n", This, client);
1102 static const IMMDeviceEnumeratorVtbl MMDevEnumVtbl =
1104 MMDevEnum_QueryInterface,
1107 MMDevEnum_EnumAudioEndpoints,
1108 MMDevEnum_GetDefaultAudioEndpoint,
1109 MMDevEnum_GetDevice,
1110 MMDevEnum_RegisterEndpointNotificationCallback,
1111 MMDevEnum_UnregisterEndpointNotificationCallback
1114 static HRESULT MMDevPropStore_Create(MMDevice *parent, DWORD access, IPropertyStore **ppv)
1116 MMDevPropStore *This;
1117 if (access != STGM_READ
1118 && access != STGM_WRITE
1119 && access != STGM_READWRITE)
1121 WARN("Invalid access %08x\n", access);
1122 return E_INVALIDARG;
1124 This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
1125 *ppv = (IPropertyStore*)This;
1127 return E_OUTOFMEMORY;
1128 This->lpVtbl = &MMDevPropVtbl;
1130 This->parent = parent;
1131 This->access = access;
1135 static void MMDevPropStore_Destroy(MMDevPropStore *This)
1137 HeapFree(GetProcessHeap(), 0, This);
1140 static HRESULT WINAPI MMDevPropStore_QueryInterface(IPropertyStore *iface, REFIID riid, void **ppv)
1142 MMDevPropStore *This = (MMDevPropStore*)iface;
1146 if (IsEqualIID(riid, &IID_IUnknown)
1147 || IsEqualIID(riid, &IID_IPropertyStore))
1152 return E_NOINTERFACE;
1153 IUnknown_AddRef((IUnknown*)*ppv);
1157 static ULONG WINAPI MMDevPropStore_AddRef(IPropertyStore *iface)
1159 MMDevPropStore *This = (MMDevPropStore*)iface;
1160 LONG ref = InterlockedIncrement(&This->ref);
1161 TRACE("Refcount now %i\n", ref);
1165 static ULONG WINAPI MMDevPropStore_Release(IPropertyStore *iface)
1167 MMDevPropStore *This = (MMDevPropStore*)iface;
1168 LONG ref = InterlockedDecrement(&This->ref);
1169 TRACE("Refcount now %i\n", ref);
1171 MMDevPropStore_Destroy(This);
1175 static HRESULT WINAPI MMDevPropStore_GetCount(IPropertyStore *iface, DWORD *nprops)
1177 MMDevPropStore *This = (MMDevPropStore*)iface;
1183 TRACE("(%p)->(%p)\n", iface, nprops);
1186 hr = MMDevPropStore_OpenPropKey(&This->parent->devguid, This->parent->flow, &propkey);
1191 DWORD len = sizeof(buffer)/sizeof(*buffer);
1192 if (RegEnumKeyExW(propkey, i, buffer, &len, NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
1196 RegCloseKey(propkey);
1197 TRACE("Returning %i\n", i);
1202 static HRESULT WINAPI MMDevPropStore_GetAt(IPropertyStore *iface, DWORD prop, PROPERTYKEY *key)
1204 MMDevPropStore *This = (MMDevPropStore*)iface;
1206 DWORD len = sizeof(buffer)/sizeof(*buffer);
1210 TRACE("(%p)->(%u,%p)\n", iface, prop, key);
1214 hr = MMDevPropStore_OpenPropKey(&This->parent->devguid, This->parent->flow, &propkey);
1218 if (RegEnumKeyExW(propkey, prop, buffer, &len, NULL, NULL, NULL, NULL) != ERROR_SUCCESS
1221 WARN("GetAt %u failed\n", prop);
1222 return E_INVALIDARG;
1224 RegCloseKey(propkey);
1226 CLSIDFromString(buffer, &key->fmtid);
1227 key->pid = atoiW(&buffer[40]);
1231 static HRESULT WINAPI MMDevPropStore_GetValue(IPropertyStore *iface, REFPROPERTYKEY key, PROPVARIANT *pv)
1233 MMDevPropStore *This = (MMDevPropStore*)iface;
1234 TRACE("(%p)->(\"%s,%u\", %p\n", This, debugstr_guid(&key->fmtid), key ? key->pid : 0, pv);
1238 if (This->access != STGM_READ
1239 && This->access != STGM_READWRITE)
1240 return STG_E_ACCESSDENIED;
1243 if (IsEqualPropertyKey(*key, PKEY_AudioEndpoint_GUID))
1246 pv->u.pwszVal = CoTaskMemAlloc(39 * sizeof(WCHAR));
1248 return E_OUTOFMEMORY;
1249 StringFromGUID2(&This->parent->devguid, pv->u.pwszVal, 39);
1253 return MMDevice_GetPropValue(&This->parent->devguid, This->parent->flow, key, pv);
1256 static HRESULT WINAPI MMDevPropStore_SetValue(IPropertyStore *iface, REFPROPERTYKEY key, REFPROPVARIANT pv)
1258 MMDevPropStore *This = (MMDevPropStore*)iface;
1263 if (This->access != STGM_WRITE
1264 && This->access != STGM_READWRITE)
1265 return STG_E_ACCESSDENIED;
1266 return MMDevice_SetPropValue(&This->parent->devguid, This->parent->flow, key, pv);
1269 static HRESULT WINAPI MMDevPropStore_Commit(IPropertyStore *iface)
1275 static const IPropertyStoreVtbl MMDevPropVtbl =
1277 MMDevPropStore_QueryInterface,
1278 MMDevPropStore_AddRef,
1279 MMDevPropStore_Release,
1280 MMDevPropStore_GetCount,
1281 MMDevPropStore_GetAt,
1282 MMDevPropStore_GetValue,
1283 MMDevPropStore_SetValue,
1284 MMDevPropStore_Commit
1288 /* Property bag for IBaseFilter activation */
1289 static HRESULT WINAPI PB_QueryInterface(IPropertyBag *iface, REFIID riid, void **ppv)
1291 ERR("Should not be called\n");
1293 return E_NOINTERFACE;
1296 static ULONG WINAPI PB_AddRef(IPropertyBag *iface)
1298 ERR("Should not be called\n");
1302 static ULONG WINAPI PB_Release(IPropertyBag *iface)
1304 ERR("Should not be called\n");
1308 static HRESULT WINAPI PB_Read(IPropertyBag *iface, LPCOLESTR name, VARIANT *var, IErrorLog *log)
1310 static const WCHAR dsguid[] = { 'D','S','G','u','i','d', 0 };
1311 IPropertyBagImpl *This = (IPropertyBagImpl*)iface;
1312 TRACE("Trying to read %s, type %u\n", debugstr_w(name), var->n1.n2.vt);
1313 if (!lstrcmpW(name, dsguid))
1316 StringFromGUID2(&This->devguid, guidstr,sizeof(guidstr)/sizeof(*guidstr));
1317 var->n1.n2.vt = VT_BSTR;
1318 var->n1.n2.n3.bstrVal = SysAllocString(guidstr);
1321 ERR("Unknown property '%s' queried\n", debugstr_w(name));
1325 static HRESULT WINAPI PB_Write(IPropertyBag *iface, LPCOLESTR name, VARIANT *var)
1327 ERR("Should not be called\n");
1331 static const IPropertyBagVtbl PB_Vtbl =