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
38 #include "wine/debug.h"
39 #include "wine/unicode.h"
42 #include "mmdeviceapi.h"
45 #include "audioclient.h"
46 #include "endpointvolume.h"
47 #include "audiopolicy.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(mmdevapi
);
54 static const WCHAR software_mmdevapi
[] =
55 { 'S','o','f','t','w','a','r','e','\\',
56 'M','i','c','r','o','s','o','f','t','\\',
57 'W','i','n','d','o','w','s','\\',
58 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
59 'M','M','D','e','v','i','c','e','s','\\',
60 'A','u','d','i','o',0};
61 static const WCHAR reg_render
[] =
62 { 'R','e','n','d','e','r',0 };
63 static const WCHAR reg_capture
[] =
64 { 'C','a','p','t','u','r','e',0 };
65 static const WCHAR reg_devicestate
[] =
66 { 'D','e','v','i','c','e','S','t','a','t','e',0 };
67 static const WCHAR reg_properties
[] =
68 { 'P','r','o','p','e','r','t','i','e','s',0 };
70 static HKEY key_render
;
71 static HKEY key_capture
;
73 typedef struct MMDevPropStoreImpl
75 const IPropertyStoreVtbl
*lpVtbl
;
81 typedef struct MMDevEnumImpl
83 const IMMDeviceEnumeratorVtbl
*lpVtbl
;
87 static MMDevEnumImpl
*MMDevEnumerator
;
88 static MMDevice
**MMDevice_head
;
89 static MMDevice
*MMDevice_def_rec
, *MMDevice_def_play
;
90 static DWORD MMDevice_count
;
91 static const IMMDeviceEnumeratorVtbl MMDevEnumVtbl
;
92 static const IMMDeviceCollectionVtbl MMDevColVtbl
;
93 static const IMMDeviceVtbl MMDeviceVtbl
;
94 static const IPropertyStoreVtbl MMDevPropVtbl
;
95 static const IMMEndpointVtbl MMEndpointVtbl
;
97 typedef struct MMDevColImpl
99 const IMMDeviceCollectionVtbl
*lpVtbl
;
105 typedef struct IPropertyBagImpl
{
106 const IPropertyBagVtbl
*lpVtbl
;
109 static const IPropertyBagVtbl PB_Vtbl
;
111 static HRESULT
MMDevPropStore_Create(MMDevice
*This
, DWORD access
, IPropertyStore
**ppv
);
113 /* Creates or updates the state of a device
114 * If GUID is null, a random guid will be assigned
115 * and the device will be created
117 static void MMDevice_Create(MMDevice
**dev
, WCHAR
*name
, GUID
*id
, EDataFlow flow
, DWORD state
, BOOL setdefault
)
124 for (i
= 0; i
< MMDevice_count
; ++i
)
126 cur
= MMDevice_head
[i
];
127 if (cur
->flow
== flow
&& !lstrcmpW(cur
->alname
, name
))
130 /* Same device, update state */
132 StringFromGUID2(&cur
->devguid
, guidstr
, sizeof(guidstr
)/sizeof(*guidstr
));
133 ret
= RegOpenKeyExW(flow
== eRender
? key_render
: key_capture
, guidstr
, 0, KEY_WRITE
, &key
);
134 if (ret
== ERROR_SUCCESS
)
136 RegSetValueExW(key
, reg_devicestate
, 0, REG_DWORD
, (const BYTE
*)&state
, sizeof(DWORD
));
143 /* No device found, allocate new one */
144 cur
= HeapAlloc(GetProcessHeap(), 0, sizeof(*cur
));
147 cur
->alname
= HeapAlloc(GetProcessHeap(), 0, (lstrlenW(name
)+1)*sizeof(WCHAR
));
150 HeapFree(GetProcessHeap(), 0, cur
);
153 lstrcpyW(cur
->alname
, name
);
154 cur
->lpVtbl
= &MMDeviceVtbl
;
155 cur
->lpEndpointVtbl
= &MMEndpointVtbl
;
157 InitializeCriticalSection(&cur
->crst
);
158 cur
->crst
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": MMDevice.crst");
168 StringFromGUID2(id
, guidstr
, sizeof(guidstr
)/sizeof(*guidstr
));
173 if (!RegCreateKeyExW(root
, guidstr
, 0, NULL
, 0, KEY_WRITE
|KEY_READ
, NULL
, &key
, NULL
))
176 RegSetValueExW(key
, reg_devicestate
, 0, REG_DWORD
, (const BYTE
*)&state
, sizeof(DWORD
));
177 if (!RegCreateKeyExW(key
, reg_properties
, 0, NULL
, 0, KEY_WRITE
|KEY_READ
, NULL
, &keyprop
, NULL
))
182 MMDevice_SetPropValue(id
, flow
, (const PROPERTYKEY
*)&DEVPKEY_Device_FriendlyName
, &pv
);
183 MMDevice_SetPropValue(id
, flow
, (const PROPERTYKEY
*)&DEVPKEY_Device_DeviceDesc
, &pv
);
184 RegCloseKey(keyprop
);
189 MMDevice_head
= HeapAlloc(GetProcessHeap(), 0, sizeof(*MMDevice_head
));
191 MMDevice_head
= HeapReAlloc(GetProcessHeap(), 0, MMDevice_head
, sizeof(*MMDevice_head
)*(1+MMDevice_count
));
192 MMDevice_head
[MMDevice_count
++] = cur
;
198 MMDevice_def_play
= cur
;
200 MMDevice_def_rec
= cur
;
206 static void MMDevice_Destroy(MMDevice
*This
)
209 TRACE("Freeing %s\n", debugstr_w(This
->alname
));
210 /* Since this function is called at destruction time, reordering of the list is unimportant */
211 for (i
= 0; i
< MMDevice_count
; ++i
)
213 if (MMDevice_head
[i
] == This
)
215 MMDevice_head
[i
] = MMDevice_head
[--MMDevice_count
];
221 palcCloseDevice(This
->device
);
223 This
->crst
.DebugInfo
->Spare
[0] = 0;
224 DeleteCriticalSection(&This
->crst
);
225 HeapFree(GetProcessHeap(), 0, This
->alname
);
226 HeapFree(GetProcessHeap(), 0, This
);
229 static HRESULT WINAPI
MMDevice_QueryInterface(IMMDevice
*iface
, REFIID riid
, void **ppv
)
231 MMDevice
*This
= (MMDevice
*)iface
;
232 TRACE("(%p)->(%s,%p)\n", iface
, debugstr_guid(riid
), ppv
);
237 if (IsEqualIID(riid
, &IID_IUnknown
)
238 || IsEqualIID(riid
, &IID_IMMDevice
))
240 else if (IsEqualIID(riid
, &IID_IMMEndpoint
))
241 *ppv
= &This
->lpEndpointVtbl
;
244 IUnknown_AddRef((IUnknown
*)*ppv
);
247 WARN("Unknown interface %s\n", debugstr_guid(riid
));
248 return E_NOINTERFACE
;
251 static ULONG WINAPI
MMDevice_AddRef(IMMDevice
*iface
)
253 MMDevice
*This
= (MMDevice
*)iface
;
256 ref
= InterlockedIncrement(&This
->ref
);
257 TRACE("Refcount now %i\n", ref
);
261 static ULONG WINAPI
MMDevice_Release(IMMDevice
*iface
)
263 MMDevice
*This
= (MMDevice
*)iface
;
266 ref
= InterlockedDecrement(&This
->ref
);
267 TRACE("Refcount now %i\n", ref
);
271 static HRESULT WINAPI
MMDevice_Activate(IMMDevice
*iface
, REFIID riid
, DWORD clsctx
, PROPVARIANT
*params
, void **ppv
)
273 HRESULT hr
= E_NOINTERFACE
;
276 MMDevice
*This
= (MMDevice
*)iface
;
278 TRACE("(%p)->(%p,%x,%p,%p)\n", iface
, riid
, clsctx
, params
, ppv
);
285 WARN("OpenAL is still not loaded\n");
286 hr
= AUDCLNT_E_SERVICE_NOT_RUNNING
;
288 else if (IsEqualIID(riid
, &IID_IAudioClient
))
289 hr
= AudioClient_Create(This
, (IAudioClient
**)ppv
);
290 else if (IsEqualIID(riid
, &IID_IAudioEndpointVolume
))
291 hr
= AudioEndpointVolume_Create(This
, (IAudioEndpointVolume
**)ppv
);
292 else if (IsEqualIID(riid
, &IID_IAudioSessionManager
)
293 || IsEqualIID(riid
, &IID_IAudioSessionManager2
))
295 FIXME("IID_IAudioSessionManager unsupported\n");
297 else if (IsEqualIID(riid
, &IID_IBaseFilter
))
299 if (This
->flow
== eRender
)
300 hr
= CoCreateInstance(&CLSID_DSoundRender
, NULL
, clsctx
, riid
, ppv
);
302 ERR("Not supported for recording?\n");
305 IPersistPropertyBag
*ppb
;
306 hr
= IUnknown_QueryInterface((IUnknown
*)*ppv
, &IID_IPersistPropertyBag
, (void*)&ppb
);
309 /* ::Load cannot assume the interface stays alive after the function returns,
310 * so just create the interface on the stack, saves a lot of complicated code */
311 IPropertyBagImpl bag
= { &PB_Vtbl
, This
->devguid
};
312 hr
= IPersistPropertyBag_Load(ppb
, (IPropertyBag
*)&bag
, NULL
);
313 IPersistPropertyBag_Release(ppb
);
315 IBaseFilter_Release((IBaseFilter
*)*ppv
);
319 FIXME("Wine doesn't support IPersistPropertyBag on DSoundRender yet, ignoring..\n");
324 else if (IsEqualIID(riid
, &IID_IDeviceTopology
))
326 FIXME("IID_IDeviceTopology unsupported\n");
328 else if (IsEqualIID(riid
, &IID_IDirectSound
)
329 || IsEqualIID(riid
, &IID_IDirectSound8
))
331 if (This
->flow
== eRender
)
332 hr
= CoCreateInstance(&CLSID_DirectSound8
, NULL
, clsctx
, riid
, ppv
);
335 hr
= IDirectSound_Initialize((IDirectSound
*)*ppv
, &This
->devguid
);
337 IDirectSound_Release((IDirectSound
*)*ppv
);
340 else if (IsEqualIID(riid
, &IID_IDirectSoundCapture
)
341 || IsEqualIID(riid
, &IID_IDirectSoundCapture8
))
343 if (This
->flow
== eCapture
)
344 hr
= CoCreateInstance(&CLSID_DirectSoundCapture8
, NULL
, clsctx
, riid
, ppv
);
347 hr
= IDirectSoundCapture_Initialize((IDirectSoundCapture
*)*ppv
, &This
->devguid
);
349 IDirectSoundCapture_Release((IDirectSoundCapture
*)*ppv
);
353 ERR("Invalid/unknown iid %s\n", debugstr_guid(riid
));
355 if (!ppv
) return E_POINTER
;
356 hr
= AUDCLNT_E_SERVICE_NOT_RUNNING
;
362 TRACE("Returning %08x\n", hr
);
366 static HRESULT WINAPI
MMDevice_OpenPropertyStore(IMMDevice
*iface
, DWORD access
, IPropertyStore
**ppv
)
368 MMDevice
*This
= (MMDevice
*)iface
;
369 TRACE("(%p)->(%x,%p)\n", This
, access
, ppv
);
373 return MMDevPropStore_Create(This
, access
, ppv
);
376 static HRESULT WINAPI
MMDevice_GetId(IMMDevice
*iface
, WCHAR
**itemid
)
378 MMDevice
*This
= (MMDevice
*)iface
;
380 GUID
*id
= &This
->devguid
;
381 static const WCHAR formatW
[] = { '{','0','.','0','.','0','.','0','0','0','0','0','0','0','0','}','.',
382 '{','%','0','8','X','-','%','0','4','X','-',
383 '%','0','4','X','-','%','0','2','X','%','0','2','X','-',
384 '%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X',
385 '%','0','2','X','%','0','2','X','}',0 };
387 TRACE("(%p)->(%p)\n", This
, itemid
);
390 *itemid
= str
= CoTaskMemAlloc(56 * sizeof(WCHAR
));
392 return E_OUTOFMEMORY
;
393 wsprintfW( str
, formatW
, id
->Data1
, id
->Data2
, id
->Data3
,
394 id
->Data4
[0], id
->Data4
[1], id
->Data4
[2], id
->Data4
[3],
395 id
->Data4
[4], id
->Data4
[5], id
->Data4
[6], id
->Data4
[7] );
399 static HRESULT WINAPI
MMDevice_GetState(IMMDevice
*iface
, DWORD
*state
)
401 MMDevice
*This
= (MMDevice
*)iface
;
402 TRACE("(%p)->(%p)\n", iface
, state
);
406 *state
= This
->state
;
410 static const IMMDeviceVtbl MMDeviceVtbl
=
412 MMDevice_QueryInterface
,
416 MMDevice_OpenPropertyStore
,
421 static MMDevice
*get_this_from_endpoint(IMMEndpoint
*iface
)
423 return (MMDevice
*)((char*)iface
- offsetof(MMDevice
,lpEndpointVtbl
));
426 static HRESULT WINAPI
MMEndpoint_QueryInterface(IMMEndpoint
*iface
, REFIID riid
, void **ppv
)
428 MMDevice
*This
= get_this_from_endpoint(iface
);
429 return IMMDevice_QueryInterface((IMMDevice
*)This
, riid
, ppv
);
432 static ULONG WINAPI
MMEndpoint_AddRef(IMMEndpoint
*iface
)
434 MMDevice
*This
= get_this_from_endpoint(iface
);
435 return IMMDevice_AddRef((IMMDevice
*)This
);
438 static ULONG WINAPI
MMEndpoint_Release(IMMEndpoint
*iface
)
440 MMDevice
*This
= get_this_from_endpoint(iface
);
441 return IMMDevice_Release((IMMDevice
*)This
);
444 static HRESULT WINAPI
MMEndpoint_GetDataFlow(IMMEndpoint
*iface
, EDataFlow
*flow
)
446 MMDevice
*This
= get_this_from_endpoint(iface
);
453 static const IMMEndpointVtbl MMEndpointVtbl
=
455 MMEndpoint_QueryInterface
,
458 MMEndpoint_GetDataFlow
461 static HRESULT
MMDevCol_Create(IMMDeviceCollection
**ppv
, EDataFlow flow
, DWORD state
)
465 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(*This
));
468 return E_OUTOFMEMORY
;
469 This
->lpVtbl
= &MMDevColVtbl
;
473 *ppv
= (IMMDeviceCollection
*)This
;
477 static void MMDevCol_Destroy(MMDevColImpl
*This
)
479 HeapFree(GetProcessHeap(), 0, This
);
482 static HRESULT WINAPI
MMDevCol_QueryInterface(IMMDeviceCollection
*iface
, REFIID riid
, void **ppv
)
484 MMDevColImpl
*This
= (MMDevColImpl
*)iface
;
488 if (IsEqualIID(riid
, &IID_IUnknown
)
489 || IsEqualIID(riid
, &IID_IMMDeviceCollection
))
494 return E_NOINTERFACE
;
495 IUnknown_AddRef((IUnknown
*)*ppv
);
499 static ULONG WINAPI
MMDevCol_AddRef(IMMDeviceCollection
*iface
)
501 MMDevColImpl
*This
= (MMDevColImpl
*)iface
;
502 LONG ref
= InterlockedIncrement(&This
->ref
);
503 TRACE("Refcount now %i\n", ref
);
507 static ULONG WINAPI
MMDevCol_Release(IMMDeviceCollection
*iface
)
509 MMDevColImpl
*This
= (MMDevColImpl
*)iface
;
510 LONG ref
= InterlockedDecrement(&This
->ref
);
511 TRACE("Refcount now %i\n", ref
);
513 MMDevCol_Destroy(This
);
517 static HRESULT WINAPI
MMDevCol_GetCount(IMMDeviceCollection
*iface
, UINT
*numdevs
)
519 MMDevColImpl
*This
= (MMDevColImpl
*)iface
;
522 TRACE("(%p)->(%p)\n", This
, numdevs
);
527 for (i
= 0; i
< MMDevice_count
; ++i
)
529 MMDevice
*cur
= MMDevice_head
[i
];
530 if ((cur
->flow
== This
->flow
|| This
->flow
== eAll
)
531 && (cur
->state
& This
->state
))
537 static HRESULT WINAPI
MMDevCol_Item(IMMDeviceCollection
*iface
, UINT n
, IMMDevice
**dev
)
539 MMDevColImpl
*This
= (MMDevColImpl
*)iface
;
542 TRACE("(%p)->(%u, %p)\n", This
, n
, dev
);
546 for (j
= 0; j
< MMDevice_count
; ++j
)
548 MMDevice
*cur
= MMDevice_head
[j
];
549 if ((cur
->flow
== This
->flow
|| This
->flow
== eAll
)
550 && (cur
->state
& This
->state
)
553 *dev
= (IMMDevice
*)cur
;
554 IMMDevice_AddRef(*dev
);
558 WARN("Could not obtain item %u\n", n
);
563 static const IMMDeviceCollectionVtbl MMDevColVtbl
=
565 MMDevCol_QueryInterface
,
572 static const WCHAR propkey_formatW
[] = {
573 '{','%','0','8','X','-','%','0','4','X','-',
574 '%','0','4','X','-','%','0','2','X','%','0','2','X','-',
575 '%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X',
576 '%','0','2','X','%','0','2','X','}',',','%','d',0 };
578 static HRESULT
MMDevPropStore_OpenPropKey(const GUID
*guid
, DWORD flow
, HKEY
*propkey
)
583 StringFromGUID2(guid
, buffer
, 39);
584 if ((ret
= RegOpenKeyExW(flow
== eRender
? key_render
: key_capture
, buffer
, 0, KEY_READ
|KEY_WRITE
, &key
)) != ERROR_SUCCESS
)
586 WARN("Opening key %s failed with %u\n", debugstr_w(buffer
), ret
);
589 ret
= RegOpenKeyExW(key
, reg_properties
, 0, KEY_READ
|KEY_WRITE
, propkey
);
591 if (ret
!= ERROR_SUCCESS
)
593 WARN("Opening key %s failed with %u\n", debugstr_w(reg_properties
), ret
);
599 HRESULT
MMDevice_GetPropValue(const GUID
*devguid
, DWORD flow
, REFPROPERTYKEY key
, PROPVARIANT
*pv
)
602 const GUID
*id
= &key
->fmtid
;
608 hr
= MMDevPropStore_OpenPropKey(devguid
, flow
, ®key
);
611 wsprintfW( buffer
, propkey_formatW
, id
->Data1
, id
->Data2
, id
->Data3
,
612 id
->Data4
[0], id
->Data4
[1], id
->Data4
[2], id
->Data4
[3],
613 id
->Data4
[4], id
->Data4
[5], id
->Data4
[6], id
->Data4
[7], key
->pid
);
614 ret
= RegGetValueW(regkey
, NULL
, buffer
, RRF_RT_ANY
, &type
, NULL
, &size
);
615 if (ret
!= ERROR_SUCCESS
)
617 WARN("Reading %s returned %d\n", debugstr_w(buffer
), ret
);
619 PropVariantClear(pv
);
628 pv
->u
.pwszVal
= CoTaskMemAlloc(size
);
632 RegGetValueW(regkey
, NULL
, buffer
, RRF_RT_REG_SZ
, NULL
, (BYTE
*)pv
->u
.pwszVal
, &size
);
638 RegGetValueW(regkey
, NULL
, buffer
, RRF_RT_REG_DWORD
, NULL
, (BYTE
*)&pv
->u
.ulVal
, &size
);
644 pv
->u
.blob
.cbSize
= size
;
645 pv
->u
.blob
.pBlobData
= CoTaskMemAlloc(size
);
646 if (!pv
->u
.blob
.pBlobData
)
649 RegGetValueW(regkey
, NULL
, buffer
, RRF_RT_REG_BINARY
, NULL
, (BYTE
*)pv
->u
.blob
.pBlobData
, &size
);
653 ERR("Unknown/unhandled type: %u\n", type
);
654 PropVariantClear(pv
);
661 HRESULT
MMDevice_SetPropValue(const GUID
*devguid
, DWORD flow
, REFPROPERTYKEY key
, REFPROPVARIANT pv
)
664 const GUID
*id
= &key
->fmtid
;
669 hr
= MMDevPropStore_OpenPropKey(devguid
, flow
, ®key
);
672 wsprintfW( buffer
, propkey_formatW
, id
->Data1
, id
->Data2
, id
->Data3
,
673 id
->Data4
[0], id
->Data4
[1], id
->Data4
[2], id
->Data4
[3],
674 id
->Data4
[4], id
->Data4
[5], id
->Data4
[6], id
->Data4
[7], key
->pid
);
679 ret
= RegSetValueExW(regkey
, buffer
, 0, REG_DWORD
, (const BYTE
*)&pv
->u
.ulVal
, sizeof(DWORD
));
684 ret
= RegSetValueExW(regkey
, buffer
, 0, REG_BINARY
, pv
->u
.blob
.pBlobData
, pv
->u
.blob
.cbSize
);
685 TRACE("Blob %p %u\n", pv
->u
.blob
.pBlobData
, pv
->u
.blob
.cbSize
);
691 ret
= RegSetValueExW(regkey
, buffer
, 0, REG_SZ
, (const BYTE
*)pv
->u
.pwszVal
, sizeof(WCHAR
)*(1+lstrlenW(pv
->u
.pwszVal
)));
696 FIXME("Unhandled type %u\n", pv
->vt
);
701 TRACE("Writing %s returned %u\n", debugstr_w(buffer
), ret
);
707 static void openal_setformat(MMDevice
*This
, DWORD freq
)
710 PROPVARIANT pv
= { VT_EMPTY
};
712 hr
= MMDevice_GetPropValue(&This
->devguid
, This
->flow
, &PKEY_AudioEngine_DeviceFormat
, &pv
);
713 if (SUCCEEDED(hr
) && pv
.vt
== VT_BLOB
)
716 pwfx
= (WAVEFORMATEX
*)pv
.u
.blob
.pBlobData
;
717 if (pwfx
->nSamplesPerSec
!= freq
)
719 pwfx
->nSamplesPerSec
= freq
;
720 pwfx
->nAvgBytesPerSec
= freq
* pwfx
->nBlockAlign
;
721 MMDevice_SetPropValue(&This
->devguid
, This
->flow
, &PKEY_AudioEngine_DeviceFormat
, &pv
);
727 WAVEFORMATEXTENSIBLE wfxe
;
729 wfxe
.Format
.wFormatTag
= WAVE_FORMAT_EXTENSIBLE
;
730 wfxe
.Format
.nChannels
= 2;
731 wfxe
.Format
.wBitsPerSample
= 32;
732 wfxe
.Format
.nBlockAlign
= wfxe
.Format
.nChannels
* wfxe
.Format
.wBitsPerSample
/8;
733 wfxe
.Format
.nSamplesPerSec
= freq
;
734 wfxe
.Format
.nAvgBytesPerSec
= wfxe
.Format
.nSamplesPerSec
* wfxe
.Format
.nBlockAlign
;
735 wfxe
.Format
.cbSize
= sizeof(wfxe
)-sizeof(WAVEFORMATEX
);
736 wfxe
.Samples
.wValidBitsPerSample
= 32;
737 wfxe
.SubFormat
= KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
;
738 wfxe
.dwChannelMask
= SPEAKER_FRONT_LEFT
|SPEAKER_FRONT_RIGHT
;
741 pv
.u
.blob
.cbSize
= sizeof(wfxe
);
742 pv
.u
.blob
.pBlobData
= (BYTE
*)&wfxe
;
743 MMDevice_SetPropValue(&This
->devguid
, This
->flow
, &PKEY_AudioEngine_DeviceFormat
, &pv
);
744 MMDevice_SetPropValue(&This
->devguid
, This
->flow
, &PKEY_AudioEngine_OEMFormat
, &pv
);
748 static int blacklist_pulse
;
750 static int blacklist(const char *dev
) {
752 if (!strncmp(dev
, "OSS ", 4))
755 if (blacklist_pulse
&& !strncmp(dev
, "PulseAudio ", 11))
757 if (!strncmp(dev
, "ALSA ", 5) && strstr(dev
, "hw:"))
759 if (!strncmp(dev
, "PortAudio ", 10))
764 static void pulse_fixup(const char *devstr
, const char **defstr
, int render
) {
768 if (render
&& !blacklist_pulse
&& !local_contexts
)
771 if (!blacklist_pulse
|| !devstr
|| !*devstr
)
774 default_pulse
= !strncmp(*defstr
, "PulseAudio ", 11);
776 while (*devstr
&& !strncmp(devstr
, "PulseAudio ", 11))
777 devstr
+= strlen(devstr
) + 1;
779 /* Could still be a newer version, so check for 1.11 if more devices are enabled */
780 if (render
&& *devstr
) {
781 ALCdevice
*dev
= palcOpenDevice(devstr
);
782 ALCcontext
*ctx
= palcCreateContext(dev
, NULL
);
787 ver
= palGetString(AL_VERSION
);
789 palcDestroyContext(ctx
);
791 if (!strcmp(ver
, "1.1 ALSOFT 1.11.753")) {
793 palcCloseDevice(dev
);
798 palcCloseDevice(dev
);
802 ERR("Disabling pulseaudio because of old openal version\n");
803 ERR("Please upgrade to openal-soft v1.12 or newer\n");
805 TRACE("New default: %s\n", devstr
);
810 static void openal_scanrender(void)
812 WCHAR name
[MAX_PATH
];
814 const ALCchar
*devstr
, *defaultstr
;
816 EnterCriticalSection(&openal_crst
);
817 if (palcIsExtensionPresent(NULL
, "ALC_ENUMERATE_ALL_EXT")) {
818 defaultstr
= palcGetString(NULL
, ALC_DEFAULT_ALL_DEVICES_SPECIFIER
);
819 devstr
= palcGetString(NULL
, ALC_ALL_DEVICES_SPECIFIER
);
821 defaultstr
= palcGetString(NULL
, ALC_DEFAULT_DEVICE_SPECIFIER
);
822 devstr
= palcGetString(NULL
, ALC_DEVICE_SPECIFIER
);
824 pulse_fixup(devstr
, &defaultstr
, 1);
825 defblacklisted
= blacklist(defaultstr
);
827 WARN("Disabling blacklist because %s is blacklisted\n", defaultstr
);
829 for (; *devstr
; devstr
+= strlen(devstr
)+1) {
831 MultiByteToWideChar( CP_UNIXCP
, 0, devstr
, -1,
832 name
, sizeof(name
)/sizeof(*name
)-1 );
833 name
[sizeof(name
)/sizeof(*name
)-1] = 0;
834 /* Only enable blacklist if the default device isn't blacklisted */
835 if (!defblacklisted
&& blacklist(devstr
)) {
836 WARN("Not adding %s: device is blacklisted\n", devstr
);
839 TRACE("Adding %s\n", devstr
);
840 dev
= palcOpenDevice(devstr
);
841 MMDevice_Create(&mmdev
, name
, NULL
, eRender
, dev
? DEVICE_STATE_ACTIVE
: DEVICE_STATE_NOTPRESENT
, !strcmp(devstr
, defaultstr
));
845 palcGetIntegerv(dev
, ALC_FREQUENCY
, 1, &freq
);
846 openal_setformat(mmdev
, freq
);
847 palcCloseDevice(dev
);
850 WARN("Could not open device: %04x\n", palcGetError(NULL
));
852 LeaveCriticalSection(&openal_crst
);
855 static void openal_scancapture(void)
857 WCHAR name
[MAX_PATH
];
859 const ALCchar
*devstr
, *defaultstr
;
862 EnterCriticalSection(&openal_crst
);
863 devstr
= palcGetString(NULL
, ALC_CAPTURE_DEVICE_SPECIFIER
);
864 defaultstr
= palcGetString(NULL
, ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER
);
865 pulse_fixup(devstr
, &defaultstr
, 0);
866 defblacklisted
= blacklist(defaultstr
);
868 WARN("Disabling blacklist because %s is blacklisted\n", defaultstr
);
869 if (devstr
&& *devstr
)
870 for (; *devstr
; devstr
+= strlen(devstr
)+1) {
873 MultiByteToWideChar( CP_UNIXCP
, 0, devstr
, -1,
874 name
, sizeof(name
)/sizeof(*name
)-1 );
875 name
[sizeof(name
)/sizeof(*name
)-1] = 0;
876 if (!defblacklisted
&& blacklist(devstr
)) {
877 WARN("Not adding %s: device is blacklisted\n", devstr
);
880 TRACE("Adding %s\n", devstr
);
881 dev
= palcCaptureOpenDevice(devstr
, freq
, AL_FORMAT_MONO16
, 65536);
882 MMDevice_Create(&mmdev
, name
, NULL
, eCapture
, dev
? DEVICE_STATE_ACTIVE
: DEVICE_STATE_NOTPRESENT
, !strcmp(devstr
, defaultstr
));
884 openal_setformat(mmdev
, freq
);
885 palcCaptureCloseDevice(dev
);
887 WARN("Could not open device: %04x\n", palcGetError(NULL
));
889 LeaveCriticalSection(&openal_crst
);
891 #endif /*HAVE_OPENAL*/
893 HRESULT
MMDevEnum_Create(REFIID riid
, void **ppv
)
895 MMDevEnumImpl
*This
= MMDevEnumerator
;
904 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(*This
));
907 return E_OUTOFMEMORY
;
909 This
->lpVtbl
= &MMDevEnumVtbl
;
910 MMDevEnumerator
= This
;
912 ret
= RegCreateKeyExW(HKEY_LOCAL_MACHINE
, software_mmdevapi
, 0, NULL
, 0, KEY_WRITE
|KEY_READ
, NULL
, &root
, NULL
);
913 if (ret
== ERROR_SUCCESS
)
914 ret
= RegCreateKeyExW(root
, reg_capture
, 0, NULL
, 0, KEY_READ
|KEY_WRITE
, NULL
, &key_capture
, NULL
);
915 if (ret
== ERROR_SUCCESS
)
916 ret
= RegCreateKeyExW(root
, reg_render
, 0, NULL
, 0, KEY_READ
|KEY_WRITE
, NULL
, &key_render
, NULL
);
920 if (ret
!= ERROR_SUCCESS
)
922 RegCloseKey(key_capture
);
923 key_render
= key_capture
= NULL
;
924 WARN("Couldn't create key: %u\n", ret
);
931 PROPVARIANT pv
= { VT_EMPTY
};
933 len
= sizeof(guidvalue
);
934 ret
= RegEnumKeyExW(cur
, i
++, guidvalue
, &len
, NULL
, NULL
, NULL
, NULL
);
935 if (ret
== ERROR_NO_MORE_ITEMS
)
937 if (cur
== key_capture
)
946 if (ret
!= ERROR_SUCCESS
)
948 if (SUCCEEDED(CLSIDFromString(guidvalue
, &guid
))
949 && SUCCEEDED(MMDevice_GetPropValue(&guid
, curflow
, (const PROPERTYKEY
*)&DEVPKEY_Device_FriendlyName
, &pv
))
950 && pv
.vt
== VT_LPWSTR
)
952 MMDevice_Create(NULL
, pv
.u
.pwszVal
, &guid
, curflow
,
953 DEVICE_STATE_NOTPRESENT
, FALSE
);
954 CoTaskMemFree(pv
.u
.pwszVal
);
961 openal_scancapture();
964 FIXME("OpenAL support not enabled, application will not find sound devices\n");
966 ERR("OpenAL support not compiled in, application will not find sound devices\n");
967 #endif /*HAVE_OPENAL*/
969 return IUnknown_QueryInterface((IUnknown
*)This
, riid
, ppv
);
972 void MMDevEnum_Free(void)
974 while (MMDevice_count
)
975 MMDevice_Destroy(MMDevice_head
[0]);
976 RegCloseKey(key_render
);
977 RegCloseKey(key_capture
);
978 key_render
= key_capture
= NULL
;
979 HeapFree(GetProcessHeap(), 0, MMDevEnumerator
);
980 MMDevEnumerator
= NULL
;
983 static HRESULT WINAPI
MMDevEnum_QueryInterface(IMMDeviceEnumerator
*iface
, REFIID riid
, void **ppv
)
985 MMDevEnumImpl
*This
= (MMDevEnumImpl
*)iface
;
989 if (IsEqualIID(riid
, &IID_IUnknown
)
990 || IsEqualIID(riid
, &IID_IMMDeviceEnumerator
))
995 return E_NOINTERFACE
;
996 IUnknown_AddRef((IUnknown
*)*ppv
);
1000 static ULONG WINAPI
MMDevEnum_AddRef(IMMDeviceEnumerator
*iface
)
1002 MMDevEnumImpl
*This
= (MMDevEnumImpl
*)iface
;
1003 LONG ref
= InterlockedIncrement(&This
->ref
);
1004 TRACE("Refcount now %i\n", ref
);
1008 static ULONG WINAPI
MMDevEnum_Release(IMMDeviceEnumerator
*iface
)
1010 MMDevEnumImpl
*This
= (MMDevEnumImpl
*)iface
;
1011 LONG ref
= InterlockedDecrement(&This
->ref
);
1014 TRACE("Refcount now %i\n", ref
);
1018 static HRESULT WINAPI
MMDevEnum_EnumAudioEndpoints(IMMDeviceEnumerator
*iface
, EDataFlow flow
, DWORD mask
, IMMDeviceCollection
**devices
)
1020 MMDevEnumImpl
*This
= (MMDevEnumImpl
*)iface
;
1021 TRACE("(%p)->(%u,%u,%p)\n", This
, flow
, mask
, devices
);
1025 if (flow
>= EDataFlow_enum_count
)
1026 return E_INVALIDARG
;
1027 if (mask
& ~DEVICE_STATEMASK_ALL
)
1028 return E_INVALIDARG
;
1029 return MMDevCol_Create(devices
, flow
, mask
);
1032 static HRESULT WINAPI
MMDevEnum_GetDefaultAudioEndpoint(IMMDeviceEnumerator
*iface
, EDataFlow flow
, ERole role
, IMMDevice
**device
)
1034 MMDevEnumImpl
*This
= (MMDevEnumImpl
*)iface
;
1035 TRACE("(%p)->(%u,%u,%p)\n", This
, flow
, role
, device
);
1041 if (flow
== eRender
)
1042 *device
= (IMMDevice
*)MMDevice_def_play
;
1043 else if (flow
== eCapture
)
1044 *device
= (IMMDevice
*)MMDevice_def_rec
;
1047 WARN("Unknown flow %u\n", flow
);
1048 return E_INVALIDARG
;
1053 IMMDevice_AddRef(*device
);
1057 static HRESULT WINAPI
MMDevEnum_GetDevice(IMMDeviceEnumerator
*iface
, const WCHAR
*name
, IMMDevice
**device
)
1059 MMDevEnumImpl
*This
= (MMDevEnumImpl
*)iface
;
1061 IMMDevice
*dev
= NULL
;
1063 TRACE("(%p)->(%s,%p)\n", This
, debugstr_w(name
), device
);
1064 for (i
= 0; i
< MMDevice_count
; ++i
)
1067 dev
= (IMMDevice
*)MMDevice_head
[i
];
1068 IMMDevice_GetId(dev
, &str
);
1070 if (str
&& !lstrcmpW(str
, name
))
1079 IUnknown_AddRef(dev
);
1083 WARN("Could not find device %s\n", debugstr_w(name
));
1087 static HRESULT WINAPI
MMDevEnum_RegisterEndpointNotificationCallback(IMMDeviceEnumerator
*iface
, IMMNotificationClient
*client
)
1089 MMDevEnumImpl
*This
= (MMDevEnumImpl
*)iface
;
1090 TRACE("(%p)->(%p)\n", This
, client
);
1095 static HRESULT WINAPI
MMDevEnum_UnregisterEndpointNotificationCallback(IMMDeviceEnumerator
*iface
, IMMNotificationClient
*client
)
1097 MMDevEnumImpl
*This
= (MMDevEnumImpl
*)iface
;
1098 TRACE("(%p)->(%p)\n", This
, client
);
1103 static const IMMDeviceEnumeratorVtbl MMDevEnumVtbl
=
1105 MMDevEnum_QueryInterface
,
1108 MMDevEnum_EnumAudioEndpoints
,
1109 MMDevEnum_GetDefaultAudioEndpoint
,
1110 MMDevEnum_GetDevice
,
1111 MMDevEnum_RegisterEndpointNotificationCallback
,
1112 MMDevEnum_UnregisterEndpointNotificationCallback
1115 static HRESULT
MMDevPropStore_Create(MMDevice
*parent
, DWORD access
, IPropertyStore
**ppv
)
1117 MMDevPropStore
*This
;
1118 if (access
!= STGM_READ
1119 && access
!= STGM_WRITE
1120 && access
!= STGM_READWRITE
)
1122 WARN("Invalid access %08x\n", access
);
1123 return E_INVALIDARG
;
1125 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(*This
));
1126 *ppv
= (IPropertyStore
*)This
;
1128 return E_OUTOFMEMORY
;
1129 This
->lpVtbl
= &MMDevPropVtbl
;
1131 This
->parent
= parent
;
1132 This
->access
= access
;
1136 static void MMDevPropStore_Destroy(MMDevPropStore
*This
)
1138 HeapFree(GetProcessHeap(), 0, This
);
1141 static HRESULT WINAPI
MMDevPropStore_QueryInterface(IPropertyStore
*iface
, REFIID riid
, void **ppv
)
1143 MMDevPropStore
*This
= (MMDevPropStore
*)iface
;
1147 if (IsEqualIID(riid
, &IID_IUnknown
)
1148 || IsEqualIID(riid
, &IID_IPropertyStore
))
1153 return E_NOINTERFACE
;
1154 IUnknown_AddRef((IUnknown
*)*ppv
);
1158 static ULONG WINAPI
MMDevPropStore_AddRef(IPropertyStore
*iface
)
1160 MMDevPropStore
*This
= (MMDevPropStore
*)iface
;
1161 LONG ref
= InterlockedIncrement(&This
->ref
);
1162 TRACE("Refcount now %i\n", ref
);
1166 static ULONG WINAPI
MMDevPropStore_Release(IPropertyStore
*iface
)
1168 MMDevPropStore
*This
= (MMDevPropStore
*)iface
;
1169 LONG ref
= InterlockedDecrement(&This
->ref
);
1170 TRACE("Refcount now %i\n", ref
);
1172 MMDevPropStore_Destroy(This
);
1176 static HRESULT WINAPI
MMDevPropStore_GetCount(IPropertyStore
*iface
, DWORD
*nprops
)
1178 MMDevPropStore
*This
= (MMDevPropStore
*)iface
;
1184 TRACE("(%p)->(%p)\n", iface
, nprops
);
1187 hr
= MMDevPropStore_OpenPropKey(&This
->parent
->devguid
, This
->parent
->flow
, &propkey
);
1192 DWORD len
= sizeof(buffer
)/sizeof(*buffer
);
1193 if (RegEnumKeyExW(propkey
, i
, buffer
, &len
, NULL
, NULL
, NULL
, NULL
) != ERROR_SUCCESS
)
1197 RegCloseKey(propkey
);
1198 TRACE("Returning %i\n", i
);
1203 static HRESULT WINAPI
MMDevPropStore_GetAt(IPropertyStore
*iface
, DWORD prop
, PROPERTYKEY
*key
)
1205 MMDevPropStore
*This
= (MMDevPropStore
*)iface
;
1207 DWORD len
= sizeof(buffer
)/sizeof(*buffer
);
1211 TRACE("(%p)->(%u,%p)\n", iface
, prop
, key
);
1215 hr
= MMDevPropStore_OpenPropKey(&This
->parent
->devguid
, This
->parent
->flow
, &propkey
);
1219 if (RegEnumKeyExW(propkey
, prop
, buffer
, &len
, NULL
, NULL
, NULL
, NULL
) != ERROR_SUCCESS
1222 WARN("GetAt %u failed\n", prop
);
1223 return E_INVALIDARG
;
1225 RegCloseKey(propkey
);
1227 CLSIDFromString(buffer
, &key
->fmtid
);
1228 key
->pid
= atoiW(&buffer
[40]);
1232 static HRESULT WINAPI
MMDevPropStore_GetValue(IPropertyStore
*iface
, REFPROPERTYKEY key
, PROPVARIANT
*pv
)
1234 MMDevPropStore
*This
= (MMDevPropStore
*)iface
;
1235 TRACE("(%p)->(\"%s,%u\", %p\n", This
, debugstr_guid(&key
->fmtid
), key
? key
->pid
: 0, pv
);
1239 if (This
->access
!= STGM_READ
1240 && This
->access
!= STGM_READWRITE
)
1241 return STG_E_ACCESSDENIED
;
1244 if (IsEqualPropertyKey(*key
, PKEY_AudioEndpoint_GUID
))
1247 pv
->u
.pwszVal
= CoTaskMemAlloc(39 * sizeof(WCHAR
));
1249 return E_OUTOFMEMORY
;
1250 StringFromGUID2(&This
->parent
->devguid
, pv
->u
.pwszVal
, 39);
1254 return MMDevice_GetPropValue(&This
->parent
->devguid
, This
->parent
->flow
, key
, pv
);
1257 static HRESULT WINAPI
MMDevPropStore_SetValue(IPropertyStore
*iface
, REFPROPERTYKEY key
, REFPROPVARIANT pv
)
1259 MMDevPropStore
*This
= (MMDevPropStore
*)iface
;
1264 if (This
->access
!= STGM_WRITE
1265 && This
->access
!= STGM_READWRITE
)
1266 return STG_E_ACCESSDENIED
;
1267 return MMDevice_SetPropValue(&This
->parent
->devguid
, This
->parent
->flow
, key
, pv
);
1270 static HRESULT WINAPI
MMDevPropStore_Commit(IPropertyStore
*iface
)
1276 static const IPropertyStoreVtbl MMDevPropVtbl
=
1278 MMDevPropStore_QueryInterface
,
1279 MMDevPropStore_AddRef
,
1280 MMDevPropStore_Release
,
1281 MMDevPropStore_GetCount
,
1282 MMDevPropStore_GetAt
,
1283 MMDevPropStore_GetValue
,
1284 MMDevPropStore_SetValue
,
1285 MMDevPropStore_Commit
1289 /* Property bag for IBaseFilter activation */
1290 static HRESULT WINAPI
PB_QueryInterface(IPropertyBag
*iface
, REFIID riid
, void **ppv
)
1292 ERR("Should not be called\n");
1294 return E_NOINTERFACE
;
1297 static ULONG WINAPI
PB_AddRef(IPropertyBag
*iface
)
1299 ERR("Should not be called\n");
1303 static ULONG WINAPI
PB_Release(IPropertyBag
*iface
)
1305 ERR("Should not be called\n");
1309 static HRESULT WINAPI
PB_Read(IPropertyBag
*iface
, LPCOLESTR name
, VARIANT
*var
, IErrorLog
*log
)
1311 static const WCHAR dsguid
[] = { 'D','S','G','u','i','d', 0 };
1312 IPropertyBagImpl
*This
= (IPropertyBagImpl
*)iface
;
1313 TRACE("Trying to read %s, type %u\n", debugstr_w(name
), var
->n1
.n2
.vt
);
1314 if (!lstrcmpW(name
, dsguid
))
1317 StringFromGUID2(&This
->devguid
, guidstr
,sizeof(guidstr
)/sizeof(*guidstr
));
1318 var
->n1
.n2
.vt
= VT_BSTR
;
1319 var
->n1
.n2
.n3
.bstrVal
= SysAllocString(guidstr
);
1322 ERR("Unknown property '%s' queried\n", debugstr_w(name
));
1326 static HRESULT WINAPI
PB_Write(IPropertyBag
*iface
, LPCOLESTR name
, VARIANT
*var
)
1328 ERR("Should not be called\n");
1332 static const IPropertyBagVtbl PB_Vtbl
=