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
23 #define NONAMELESSUNION
29 #include "wine/debug.h"
30 #include "wine/unicode.h"
34 #include "mmdeviceapi.h"
37 #include "audioclient.h"
38 #include "endpointvolume.h"
39 #include "audiopolicy.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(mmdevapi);
46 static const WCHAR software_mmdevapi[] =
47 { 'S','o','f','t','w','a','r','e','\\',
48 'M','i','c','r','o','s','o','f','t','\\',
49 'W','i','n','d','o','w','s','\\',
50 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
51 'M','M','D','e','v','i','c','e','s','\\',
52 'A','u','d','i','o',0};
53 static const WCHAR reg_render[] =
54 { 'R','e','n','d','e','r',0 };
55 static const WCHAR reg_capture[] =
56 { 'C','a','p','t','u','r','e',0 };
57 static const WCHAR reg_devicestate[] =
58 { 'D','e','v','i','c','e','S','t','a','t','e',0 };
59 static const WCHAR reg_properties[] =
60 { 'P','r','o','p','e','r','t','i','e','s',0 };
62 static HKEY key_render;
63 static HKEY key_capture;
65 typedef struct MMDevPropStoreImpl
67 IPropertyStore IPropertyStore_iface;
73 typedef struct MMDevEnumImpl
75 IMMDeviceEnumerator IMMDeviceEnumerator_iface;
79 static MMDevEnumImpl *MMDevEnumerator;
80 static MMDevice **MMDevice_head;
81 static MMDevice *MMDevice_def_rec, *MMDevice_def_play;
82 static DWORD MMDevice_count;
83 static const IMMDeviceEnumeratorVtbl MMDevEnumVtbl;
84 static const IMMDeviceCollectionVtbl MMDevColVtbl;
85 static const IMMDeviceVtbl MMDeviceVtbl;
86 static const IPropertyStoreVtbl MMDevPropVtbl;
87 static const IMMEndpointVtbl MMEndpointVtbl;
89 static IMMDevice info_device;
91 typedef struct MMDevColImpl
93 IMMDeviceCollection IMMDeviceCollection_iface;
99 typedef struct IPropertyBagImpl {
100 IPropertyBag IPropertyBag_iface;
104 static const IPropertyBagVtbl PB_Vtbl;
106 static HRESULT MMDevPropStore_Create(MMDevice *This, DWORD access, IPropertyStore **ppv);
108 static inline MMDevPropStore *impl_from_IPropertyStore(IPropertyStore *iface)
110 return CONTAINING_RECORD(iface, MMDevPropStore, IPropertyStore_iface);
113 static inline MMDevEnumImpl *impl_from_IMMDeviceEnumerator(IMMDeviceEnumerator *iface)
115 return CONTAINING_RECORD(iface, MMDevEnumImpl, IMMDeviceEnumerator_iface);
118 static inline MMDevColImpl *impl_from_IMMDeviceCollection(IMMDeviceCollection *iface)
120 return CONTAINING_RECORD(iface, MMDevColImpl, IMMDeviceCollection_iface);
123 static inline IPropertyBagImpl *impl_from_IPropertyBag(IPropertyBag *iface)
125 return CONTAINING_RECORD(iface, IPropertyBagImpl, IPropertyBag_iface);
128 static const WCHAR propkey_formatW[] = {
129 '{','%','0','8','X','-','%','0','4','X','-',
130 '%','0','4','X','-','%','0','2','X','%','0','2','X','-',
131 '%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X',
132 '%','0','2','X','%','0','2','X','}',',','%','d',0 };
134 static HRESULT MMDevPropStore_OpenPropKey(const GUID *guid, DWORD flow, HKEY *propkey)
139 StringFromGUID2(guid, buffer, 39);
140 if ((ret = RegOpenKeyExW(flow == eRender ? key_render : key_capture, buffer, 0, KEY_READ|KEY_WRITE, &key)) != ERROR_SUCCESS)
142 WARN("Opening key %s failed with %u\n", debugstr_w(buffer), ret);
145 ret = RegOpenKeyExW(key, reg_properties, 0, KEY_READ|KEY_WRITE, propkey);
147 if (ret != ERROR_SUCCESS)
149 WARN("Opening key %s failed with %u\n", debugstr_w(reg_properties), ret);
155 static HRESULT MMDevice_GetPropValue(const GUID *devguid, DWORD flow, REFPROPERTYKEY key, PROPVARIANT *pv)
158 const GUID *id = &key->fmtid;
164 hr = MMDevPropStore_OpenPropKey(devguid, flow, ®key);
167 wsprintfW( buffer, propkey_formatW, id->Data1, id->Data2, id->Data3,
168 id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
169 id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7], key->pid );
170 ret = RegGetValueW(regkey, NULL, buffer, RRF_RT_ANY, &type, NULL, &size);
171 if (ret != ERROR_SUCCESS)
173 WARN("Reading %s returned %d\n", debugstr_w(buffer), ret);
175 PropVariantClear(pv);
184 pv->u.pwszVal = CoTaskMemAlloc(size);
188 RegGetValueW(regkey, NULL, buffer, RRF_RT_REG_SZ, NULL, (BYTE*)pv->u.pwszVal, &size);
194 RegGetValueW(regkey, NULL, buffer, RRF_RT_REG_DWORD, NULL, (BYTE*)&pv->u.ulVal, &size);
200 pv->u.blob.cbSize = size;
201 pv->u.blob.pBlobData = CoTaskMemAlloc(size);
202 if (!pv->u.blob.pBlobData)
205 RegGetValueW(regkey, NULL, buffer, RRF_RT_REG_BINARY, NULL, (BYTE*)pv->u.blob.pBlobData, &size);
209 ERR("Unknown/unhandled type: %u\n", type);
210 PropVariantClear(pv);
217 static HRESULT MMDevice_SetPropValue(const GUID *devguid, DWORD flow, REFPROPERTYKEY key, REFPROPVARIANT pv)
220 const GUID *id = &key->fmtid;
225 hr = MMDevPropStore_OpenPropKey(devguid, flow, ®key);
228 wsprintfW( buffer, propkey_formatW, id->Data1, id->Data2, id->Data3,
229 id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
230 id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7], key->pid );
235 ret = RegSetValueExW(regkey, buffer, 0, REG_DWORD, (const BYTE*)&pv->u.ulVal, sizeof(DWORD));
240 ret = RegSetValueExW(regkey, buffer, 0, REG_BINARY, pv->u.blob.pBlobData, pv->u.blob.cbSize);
241 TRACE("Blob %p %u\n", pv->u.blob.pBlobData, pv->u.blob.cbSize);
247 ret = RegSetValueExW(regkey, buffer, 0, REG_SZ, (const BYTE*)pv->u.pwszVal, sizeof(WCHAR)*(1+lstrlenW(pv->u.pwszVal)));
252 FIXME("Unhandled type %u\n", pv->vt);
257 TRACE("Writing %s returned %u\n", debugstr_w(buffer), ret);
261 /* Creates or updates the state of a device
262 * If GUID is null, a random guid will be assigned
263 * and the device will be created
265 static MMDevice *MMDevice_Create(WCHAR *name, GUID *id, EDataFlow flow, DWORD state, BOOL setdefault)
268 MMDevice *cur = NULL;
272 static const PROPERTYKEY deviceinterface_key = {
273 {0x233164c8, 0x1b2c, 0x4c7d, {0xbc, 0x68, 0xb6, 0x71, 0x68, 0x7a, 0x25, 0x67}}, 1
276 for (i = 0; i < MMDevice_count; ++i)
278 MMDevice *device = MMDevice_head[i];
279 if (device->flow == flow && IsEqualGUID(&device->devguid, id)){
286 /* No device found, allocate new one */
287 cur = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*cur));
291 cur->IMMDevice_iface.lpVtbl = &MMDeviceVtbl;
292 cur->IMMEndpoint_iface.lpVtbl = &MMEndpointVtbl;
294 InitializeCriticalSection(&cur->crst);
295 cur->crst.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": MMDevice.crst");
298 MMDevice_head = HeapAlloc(GetProcessHeap(), 0, sizeof(*MMDevice_head));
300 MMDevice_head = HeapReAlloc(GetProcessHeap(), 0, MMDevice_head, sizeof(*MMDevice_head)*(1+MMDevice_count));
301 MMDevice_head[MMDevice_count++] = cur;
302 }else if(cur->ref > 0)
303 WARN("Modifying an MMDevice with postitive reference count!\n");
305 HeapFree(GetProcessHeap(), 0, cur->drv_id);
312 StringFromGUID2(&cur->devguid, guidstr, sizeof(guidstr)/sizeof(*guidstr));
319 if (RegCreateKeyExW(root, guidstr, 0, NULL, 0, KEY_WRITE|KEY_READ, NULL, &key, NULL) == ERROR_SUCCESS)
322 RegSetValueExW(key, reg_devicestate, 0, REG_DWORD, (const BYTE*)&state, sizeof(DWORD));
323 if (!RegCreateKeyExW(key, reg_properties, 0, NULL, 0, KEY_WRITE|KEY_READ, NULL, &keyprop, NULL))
329 MMDevice_SetPropValue(id, flow, (const PROPERTYKEY*)&DEVPKEY_Device_FriendlyName, &pv);
330 MMDevice_SetPropValue(id, flow, (const PROPERTYKEY*)&DEVPKEY_Device_DeviceDesc, &pv);
332 pv.u.pwszVal = guidstr;
333 MMDevice_SetPropValue(id, flow, &deviceinterface_key, &pv);
335 RegCloseKey(keyprop);
343 MMDevice_def_play = cur;
345 MMDevice_def_rec = cur;
350 static HRESULT load_devices_from_reg(void)
357 ret = RegCreateKeyExW(HKEY_LOCAL_MACHINE, software_mmdevapi, 0, NULL, 0, KEY_WRITE|KEY_READ, NULL, &root, NULL);
358 if (ret == ERROR_SUCCESS)
359 ret = RegCreateKeyExW(root, reg_capture, 0, NULL, 0, KEY_READ|KEY_WRITE, NULL, &key_capture, NULL);
360 if (ret == ERROR_SUCCESS)
361 ret = RegCreateKeyExW(root, reg_render, 0, NULL, 0, KEY_READ|KEY_WRITE, NULL, &key_render, NULL);
365 if (ret != ERROR_SUCCESS)
367 RegCloseKey(key_capture);
368 key_render = key_capture = NULL;
369 WARN("Couldn't create key: %u\n", ret);
377 PROPVARIANT pv = { VT_EMPTY };
379 len = sizeof(guidvalue)/sizeof(guidvalue[0]);
380 ret = RegEnumKeyExW(cur, i++, guidvalue, &len, NULL, NULL, NULL, NULL);
381 if (ret == ERROR_NO_MORE_ITEMS)
383 if (cur == key_capture)
392 if (ret != ERROR_SUCCESS)
394 if (SUCCEEDED(CLSIDFromString(guidvalue, &guid))
395 && SUCCEEDED(MMDevice_GetPropValue(&guid, curflow, (const PROPERTYKEY*)&DEVPKEY_Device_FriendlyName, &pv))
396 && pv.vt == VT_LPWSTR)
398 DWORD size_bytes = (strlenW(pv.u.pwszVal) + 1) * sizeof(WCHAR);
399 WCHAR *name = HeapAlloc(GetProcessHeap(), 0, size_bytes);
400 memcpy(name, pv.u.pwszVal, size_bytes);
401 MMDevice_Create(name, &guid, curflow,
402 DEVICE_STATE_NOTPRESENT, FALSE);
403 CoTaskMemFree(pv.u.pwszVal);
410 static HRESULT set_format(MMDevice *dev)
413 IAudioClient *client;
415 PROPVARIANT pv = { VT_EMPTY };
417 hr = drvs.pGetAudioEndpoint(&dev->devguid, &dev->IMMDevice_iface, &client);
421 hr = IAudioClient_GetMixFormat(client, &fmt);
423 IAudioClient_Release(client);
427 IAudioClient_Release(client);
430 pv.u.blob.cbSize = sizeof(WAVEFORMATEX) + fmt->cbSize;
431 pv.u.blob.pBlobData = (BYTE*)fmt;
432 MMDevice_SetPropValue(&dev->devguid, dev->flow,
433 &PKEY_AudioEngine_DeviceFormat, &pv);
434 MMDevice_SetPropValue(&dev->devguid, dev->flow,
435 &PKEY_AudioEngine_OEMFormat, &pv);
440 static HRESULT load_driver_devices(EDataFlow flow)
447 if(!drvs.pGetEndpointIDs)
450 hr = drvs.pGetEndpointIDs(flow, &ids, &guids, &num, &def);
454 for(i = 0; i < num; ++i){
456 dev = MMDevice_Create(ids[i], &guids[i], flow, DEVICE_STATE_ACTIVE,
461 HeapFree(GetProcessHeap(), 0, guids);
462 HeapFree(GetProcessHeap(), 0, ids);
467 static void MMDevice_Destroy(MMDevice *This)
470 TRACE("Freeing %s\n", debugstr_w(This->drv_id));
471 /* Since this function is called at destruction time, reordering of the list is unimportant */
472 for (i = 0; i < MMDevice_count; ++i)
474 if (MMDevice_head[i] == This)
476 MMDevice_head[i] = MMDevice_head[--MMDevice_count];
480 This->crst.DebugInfo->Spare[0] = 0;
481 DeleteCriticalSection(&This->crst);
482 HeapFree(GetProcessHeap(), 0, This->drv_id);
483 HeapFree(GetProcessHeap(), 0, This);
486 static inline MMDevice *impl_from_IMMDevice(IMMDevice *iface)
488 return CONTAINING_RECORD(iface, MMDevice, IMMDevice_iface);
491 static HRESULT WINAPI MMDevice_QueryInterface(IMMDevice *iface, REFIID riid, void **ppv)
493 MMDevice *This = impl_from_IMMDevice(iface);
494 TRACE("(%p)->(%s,%p)\n", iface, debugstr_guid(riid), ppv);
499 if (IsEqualIID(riid, &IID_IUnknown)
500 || IsEqualIID(riid, &IID_IMMDevice))
502 else if (IsEqualIID(riid, &IID_IMMEndpoint))
503 *ppv = &This->IMMEndpoint_iface;
506 IUnknown_AddRef((IUnknown*)*ppv);
509 WARN("Unknown interface %s\n", debugstr_guid(riid));
510 return E_NOINTERFACE;
513 static ULONG WINAPI MMDevice_AddRef(IMMDevice *iface)
515 MMDevice *This = impl_from_IMMDevice(iface);
518 ref = InterlockedIncrement(&This->ref);
519 TRACE("Refcount now %i\n", ref);
523 static ULONG WINAPI MMDevice_Release(IMMDevice *iface)
525 MMDevice *This = impl_from_IMMDevice(iface);
528 ref = InterlockedDecrement(&This->ref);
529 TRACE("Refcount now %i\n", ref);
533 static HRESULT WINAPI MMDevice_Activate(IMMDevice *iface, REFIID riid, DWORD clsctx, PROPVARIANT *params, void **ppv)
535 HRESULT hr = E_NOINTERFACE;
536 MMDevice *This = impl_from_IMMDevice(iface);
538 TRACE("(%p)->(%p,%x,%p,%p)\n", iface, riid, clsctx, params, ppv);
543 if (IsEqualIID(riid, &IID_IAudioClient)){
544 hr = drvs.pGetAudioEndpoint(&This->devguid, iface, (IAudioClient**)ppv);
545 }else if (IsEqualIID(riid, &IID_IAudioEndpointVolume))
546 hr = AudioEndpointVolume_Create(This, (IAudioEndpointVolume**)ppv);
547 else if (IsEqualIID(riid, &IID_IAudioSessionManager)
548 || IsEqualIID(riid, &IID_IAudioSessionManager2))
550 hr = drvs.pGetAudioSessionManager(iface, (IAudioSessionManager2**)ppv);
552 else if (IsEqualIID(riid, &IID_IBaseFilter))
554 if (This->flow == eRender)
555 hr = CoCreateInstance(&CLSID_DSoundRender, NULL, clsctx, riid, ppv);
557 ERR("Not supported for recording?\n");
560 IPersistPropertyBag *ppb;
561 hr = IUnknown_QueryInterface((IUnknown*)*ppv, &IID_IPersistPropertyBag, (void*)&ppb);
564 /* ::Load cannot assume the interface stays alive after the function returns,
565 * so just create the interface on the stack, saves a lot of complicated code */
566 IPropertyBagImpl bag = { { &PB_Vtbl }, This->devguid };
567 hr = IPersistPropertyBag_Load(ppb, &bag.IPropertyBag_iface, NULL);
568 IPersistPropertyBag_Release(ppb);
570 IBaseFilter_Release((IBaseFilter*)*ppv);
574 FIXME("Wine doesn't support IPersistPropertyBag on DSoundRender yet, ignoring..\n");
579 else if (IsEqualIID(riid, &IID_IDeviceTopology))
581 FIXME("IID_IDeviceTopology unsupported\n");
583 else if (IsEqualIID(riid, &IID_IDirectSound)
584 || IsEqualIID(riid, &IID_IDirectSound8))
586 if (This->flow == eRender)
587 hr = CoCreateInstance(&CLSID_DirectSound8, NULL, clsctx, riid, ppv);
590 hr = IDirectSound_Initialize((IDirectSound*)*ppv, &This->devguid);
592 IDirectSound_Release((IDirectSound*)*ppv);
595 else if (IsEqualIID(riid, &IID_IDirectSoundCapture)
596 || IsEqualIID(riid, &IID_IDirectSoundCapture8))
598 if (This->flow == eCapture)
599 hr = CoCreateInstance(&CLSID_DirectSoundCapture8, NULL, clsctx, riid, ppv);
602 hr = IDirectSoundCapture_Initialize((IDirectSoundCapture*)*ppv, &This->devguid);
604 IDirectSoundCapture_Release((IDirectSoundCapture*)*ppv);
608 ERR("Invalid/unknown iid %s\n", debugstr_guid(riid));
613 TRACE("Returning %08x\n", hr);
617 static HRESULT WINAPI MMDevice_OpenPropertyStore(IMMDevice *iface, DWORD access, IPropertyStore **ppv)
619 MMDevice *This = impl_from_IMMDevice(iface);
620 TRACE("(%p)->(%x,%p)\n", This, access, ppv);
624 return MMDevPropStore_Create(This, access, ppv);
627 static HRESULT WINAPI MMDevice_GetId(IMMDevice *iface, WCHAR **itemid)
629 MMDevice *This = impl_from_IMMDevice(iface);
631 GUID *id = &This->devguid;
632 static const WCHAR formatW[] = { '{','0','.','0','.','%','u','.','0','0','0','0','0','0','0','0','}','.',
633 '{','%','0','8','X','-','%','0','4','X','-',
634 '%','0','4','X','-','%','0','2','X','%','0','2','X','-',
635 '%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X',
636 '%','0','2','X','%','0','2','X','}',0 };
638 TRACE("(%p)->(%p)\n", This, itemid);
641 *itemid = str = CoTaskMemAlloc(56 * sizeof(WCHAR));
643 return E_OUTOFMEMORY;
644 wsprintfW( str, formatW, This->flow, id->Data1, id->Data2, id->Data3,
645 id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
646 id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7] );
647 TRACE("returning %s\n", wine_dbgstr_w(str));
651 static HRESULT WINAPI MMDevice_GetState(IMMDevice *iface, DWORD *state)
653 MMDevice *This = impl_from_IMMDevice(iface);
654 TRACE("(%p)->(%p)\n", iface, state);
658 *state = This->state;
662 static const IMMDeviceVtbl MMDeviceVtbl =
664 MMDevice_QueryInterface,
668 MMDevice_OpenPropertyStore,
673 static inline MMDevice *impl_from_IMMEndpoint(IMMEndpoint *iface)
675 return CONTAINING_RECORD(iface, MMDevice, IMMEndpoint_iface);
678 static HRESULT WINAPI MMEndpoint_QueryInterface(IMMEndpoint *iface, REFIID riid, void **ppv)
680 MMDevice *This = impl_from_IMMEndpoint(iface);
681 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
682 return IMMDevice_QueryInterface(&This->IMMDevice_iface, riid, ppv);
685 static ULONG WINAPI MMEndpoint_AddRef(IMMEndpoint *iface)
687 MMDevice *This = impl_from_IMMEndpoint(iface);
688 TRACE("(%p)\n", This);
689 return IMMDevice_AddRef(&This->IMMDevice_iface);
692 static ULONG WINAPI MMEndpoint_Release(IMMEndpoint *iface)
694 MMDevice *This = impl_from_IMMEndpoint(iface);
695 TRACE("(%p)\n", This);
696 return IMMDevice_Release(&This->IMMDevice_iface);
699 static HRESULT WINAPI MMEndpoint_GetDataFlow(IMMEndpoint *iface, EDataFlow *flow)
701 MMDevice *This = impl_from_IMMEndpoint(iface);
702 TRACE("(%p)->(%p)\n", This, flow);
709 static const IMMEndpointVtbl MMEndpointVtbl =
711 MMEndpoint_QueryInterface,
714 MMEndpoint_GetDataFlow
717 static HRESULT MMDevCol_Create(IMMDeviceCollection **ppv, EDataFlow flow, DWORD state)
721 This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
724 return E_OUTOFMEMORY;
725 This->IMMDeviceCollection_iface.lpVtbl = &MMDevColVtbl;
729 *ppv = &This->IMMDeviceCollection_iface;
733 static void MMDevCol_Destroy(MMDevColImpl *This)
735 HeapFree(GetProcessHeap(), 0, This);
738 static HRESULT WINAPI MMDevCol_QueryInterface(IMMDeviceCollection *iface, REFIID riid, void **ppv)
740 MMDevColImpl *This = impl_from_IMMDeviceCollection(iface);
741 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
745 if (IsEqualIID(riid, &IID_IUnknown)
746 || IsEqualIID(riid, &IID_IMMDeviceCollection))
751 return E_NOINTERFACE;
752 IUnknown_AddRef((IUnknown*)*ppv);
756 static ULONG WINAPI MMDevCol_AddRef(IMMDeviceCollection *iface)
758 MMDevColImpl *This = impl_from_IMMDeviceCollection(iface);
759 LONG ref = InterlockedIncrement(&This->ref);
760 TRACE("Refcount now %i\n", ref);
764 static ULONG WINAPI MMDevCol_Release(IMMDeviceCollection *iface)
766 MMDevColImpl *This = impl_from_IMMDeviceCollection(iface);
767 LONG ref = InterlockedDecrement(&This->ref);
768 TRACE("Refcount now %i\n", ref);
770 MMDevCol_Destroy(This);
774 static HRESULT WINAPI MMDevCol_GetCount(IMMDeviceCollection *iface, UINT *numdevs)
776 MMDevColImpl *This = impl_from_IMMDeviceCollection(iface);
779 TRACE("(%p)->(%p)\n", This, numdevs);
784 for (i = 0; i < MMDevice_count; ++i)
786 MMDevice *cur = MMDevice_head[i];
787 if ((cur->flow == This->flow || This->flow == eAll)
788 && (cur->state & This->state))
794 static HRESULT WINAPI MMDevCol_Item(IMMDeviceCollection *iface, UINT n, IMMDevice **dev)
796 MMDevColImpl *This = impl_from_IMMDeviceCollection(iface);
799 TRACE("(%p)->(%u, %p)\n", This, n, dev);
803 for (j = 0; j < MMDevice_count; ++j)
805 MMDevice *cur = MMDevice_head[j];
806 if ((cur->flow == This->flow || This->flow == eAll)
807 && (cur->state & This->state)
810 *dev = &cur->IMMDevice_iface;
811 IMMDevice_AddRef(*dev);
815 WARN("Could not obtain item %u\n", n);
820 static const IMMDeviceCollectionVtbl MMDevColVtbl =
822 MMDevCol_QueryInterface,
829 HRESULT MMDevEnum_Create(REFIID riid, void **ppv)
831 MMDevEnumImpl *This = MMDevEnumerator;
835 This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
838 return E_OUTOFMEMORY;
840 This->IMMDeviceEnumerator_iface.lpVtbl = &MMDevEnumVtbl;
841 MMDevEnumerator = This;
843 load_devices_from_reg();
844 load_driver_devices(eRender);
845 load_driver_devices(eCapture);
847 return IUnknown_QueryInterface((IUnknown*)This, riid, ppv);
850 void MMDevEnum_Free(void)
852 while (MMDevice_count)
853 MMDevice_Destroy(MMDevice_head[0]);
854 RegCloseKey(key_render);
855 RegCloseKey(key_capture);
856 key_render = key_capture = NULL;
857 HeapFree(GetProcessHeap(), 0, MMDevEnumerator);
858 MMDevEnumerator = NULL;
861 static HRESULT WINAPI MMDevEnum_QueryInterface(IMMDeviceEnumerator *iface, REFIID riid, void **ppv)
863 MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
864 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
868 if (IsEqualIID(riid, &IID_IUnknown)
869 || IsEqualIID(riid, &IID_IMMDeviceEnumerator))
874 return E_NOINTERFACE;
875 IUnknown_AddRef((IUnknown*)*ppv);
879 static ULONG WINAPI MMDevEnum_AddRef(IMMDeviceEnumerator *iface)
881 MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
882 LONG ref = InterlockedIncrement(&This->ref);
883 TRACE("Refcount now %i\n", ref);
887 static ULONG WINAPI MMDevEnum_Release(IMMDeviceEnumerator *iface)
889 MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
890 LONG ref = InterlockedDecrement(&This->ref);
893 TRACE("Refcount now %i\n", ref);
897 static HRESULT WINAPI MMDevEnum_EnumAudioEndpoints(IMMDeviceEnumerator *iface, EDataFlow flow, DWORD mask, IMMDeviceCollection **devices)
899 MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
900 TRACE("(%p)->(%u,%u,%p)\n", This, flow, mask, devices);
904 if (flow >= EDataFlow_enum_count)
906 if (mask & ~DEVICE_STATEMASK_ALL)
908 return MMDevCol_Create(devices, flow, mask);
911 static HRESULT WINAPI MMDevEnum_GetDefaultAudioEndpoint(IMMDeviceEnumerator *iface, EDataFlow flow, ERole role, IMMDevice **device)
913 MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
918 static const WCHAR slashW[] = {'\\',0};
919 static const WCHAR reg_out_nameW[] = {'D','e','f','a','u','l','t','O','u','t','p','u','t',0};
920 static const WCHAR reg_vout_nameW[] = {'D','e','f','a','u','l','t','V','o','i','c','e','O','u','t','p','u','t',0};
921 static const WCHAR reg_in_nameW[] = {'D','e','f','a','u','l','t','I','n','p','u','t',0};
922 static const WCHAR reg_vin_nameW[] = {'D','e','f','a','u','l','t','V','o','i','c','e','I','n','p','u','t',0};
924 TRACE("(%p)->(%u,%u,%p)\n", This, flow, role, device);
929 if((flow != eRender && flow != eCapture) ||
930 (role != eConsole && role != eMultimedia && role != eCommunications)){
931 WARN("Unknown flow (%u) or role (%u)\n", flow, role);
937 if(!drvs.module_name[0])
940 lstrcpyW(reg_key, drv_keyW);
941 lstrcatW(reg_key, slashW);
942 lstrcatW(reg_key, drvs.module_name);
944 if(RegOpenKeyW(HKEY_CURRENT_USER, reg_key, &key) == ERROR_SUCCESS){
945 const WCHAR *reg_x_name, *reg_vx_name;
947 DWORD size = sizeof(def_id), state;
950 reg_x_name = reg_out_nameW;
951 reg_vx_name = reg_vout_nameW;
953 reg_x_name = reg_in_nameW;
954 reg_vx_name = reg_vin_nameW;
957 if(role == eCommunications &&
958 RegQueryValueExW(key, reg_vx_name, 0, NULL,
959 (BYTE*)def_id, &size) == ERROR_SUCCESS){
960 hr = IMMDeviceEnumerator_GetDevice(iface, def_id, device);
962 if(SUCCEEDED(IMMDevice_GetState(*device, &state)) &&
963 state == DEVICE_STATE_ACTIVE){
969 TRACE("Unable to find voice device %s\n", wine_dbgstr_w(def_id));
972 if(RegQueryValueExW(key, reg_x_name, 0, NULL,
973 (BYTE*)def_id, &size) == ERROR_SUCCESS){
974 hr = IMMDeviceEnumerator_GetDevice(iface, def_id, device);
976 if(SUCCEEDED(IMMDevice_GetState(*device, &state)) &&
977 state == DEVICE_STATE_ACTIVE){
983 TRACE("Unable to find device %s\n", wine_dbgstr_w(def_id));
990 *device = &MMDevice_def_play->IMMDevice_iface;
992 *device = &MMDevice_def_rec->IMMDevice_iface;
996 IMMDevice_AddRef(*device);
1000 static HRESULT WINAPI MMDevEnum_GetDevice(IMMDeviceEnumerator *iface, const WCHAR *name, IMMDevice **device)
1002 MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
1004 IMMDevice *dev = NULL;
1006 static const WCHAR wine_info_deviceW[] = {'W','i','n','e',' ',
1007 'i','n','f','o',' ','d','e','v','i','c','e',0};
1009 TRACE("(%p)->(%s,%p)\n", This, debugstr_w(name), device);
1011 if(!name || !device)
1014 if(!lstrcmpW(name, wine_info_deviceW)){
1015 *device = &info_device;
1019 for (i = 0; i < MMDevice_count; ++i)
1022 dev = &MMDevice_head[i]->IMMDevice_iface;
1023 IMMDevice_GetId(dev, &str);
1025 if (str && !lstrcmpW(str, name))
1028 IMMDevice_AddRef(dev);
1034 TRACE("Could not find device %s\n", debugstr_w(name));
1035 return E_INVALIDARG;
1038 static HRESULT WINAPI MMDevEnum_RegisterEndpointNotificationCallback(IMMDeviceEnumerator *iface, IMMNotificationClient *client)
1040 MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
1041 TRACE("(%p)->(%p)\n", This, client);
1046 static HRESULT WINAPI MMDevEnum_UnregisterEndpointNotificationCallback(IMMDeviceEnumerator *iface, IMMNotificationClient *client)
1048 MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
1049 TRACE("(%p)->(%p)\n", This, client);
1054 static const IMMDeviceEnumeratorVtbl MMDevEnumVtbl =
1056 MMDevEnum_QueryInterface,
1059 MMDevEnum_EnumAudioEndpoints,
1060 MMDevEnum_GetDefaultAudioEndpoint,
1061 MMDevEnum_GetDevice,
1062 MMDevEnum_RegisterEndpointNotificationCallback,
1063 MMDevEnum_UnregisterEndpointNotificationCallback
1066 static HRESULT MMDevPropStore_Create(MMDevice *parent, DWORD access, IPropertyStore **ppv)
1068 MMDevPropStore *This;
1069 if (access != STGM_READ
1070 && access != STGM_WRITE
1071 && access != STGM_READWRITE)
1073 WARN("Invalid access %08x\n", access);
1074 return E_INVALIDARG;
1076 This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
1077 *ppv = &This->IPropertyStore_iface;
1079 return E_OUTOFMEMORY;
1080 This->IPropertyStore_iface.lpVtbl = &MMDevPropVtbl;
1082 This->parent = parent;
1083 This->access = access;
1087 static void MMDevPropStore_Destroy(MMDevPropStore *This)
1089 HeapFree(GetProcessHeap(), 0, This);
1092 static HRESULT WINAPI MMDevPropStore_QueryInterface(IPropertyStore *iface, REFIID riid, void **ppv)
1094 MMDevPropStore *This = impl_from_IPropertyStore(iface);
1095 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
1099 if (IsEqualIID(riid, &IID_IUnknown)
1100 || IsEqualIID(riid, &IID_IPropertyStore))
1105 return E_NOINTERFACE;
1106 IUnknown_AddRef((IUnknown*)*ppv);
1110 static ULONG WINAPI MMDevPropStore_AddRef(IPropertyStore *iface)
1112 MMDevPropStore *This = impl_from_IPropertyStore(iface);
1113 LONG ref = InterlockedIncrement(&This->ref);
1114 TRACE("Refcount now %i\n", ref);
1118 static ULONG WINAPI MMDevPropStore_Release(IPropertyStore *iface)
1120 MMDevPropStore *This = impl_from_IPropertyStore(iface);
1121 LONG ref = InterlockedDecrement(&This->ref);
1122 TRACE("Refcount now %i\n", ref);
1124 MMDevPropStore_Destroy(This);
1128 static HRESULT WINAPI MMDevPropStore_GetCount(IPropertyStore *iface, DWORD *nprops)
1130 MMDevPropStore *This = impl_from_IPropertyStore(iface);
1136 TRACE("(%p)->(%p)\n", iface, nprops);
1139 hr = MMDevPropStore_OpenPropKey(&This->parent->devguid, This->parent->flow, &propkey);
1144 DWORD len = sizeof(buffer)/sizeof(*buffer);
1145 if (RegEnumKeyExW(propkey, i, buffer, &len, NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
1149 RegCloseKey(propkey);
1150 TRACE("Returning %i\n", i);
1155 static HRESULT WINAPI MMDevPropStore_GetAt(IPropertyStore *iface, DWORD prop, PROPERTYKEY *key)
1157 MMDevPropStore *This = impl_from_IPropertyStore(iface);
1159 DWORD len = sizeof(buffer)/sizeof(*buffer);
1163 TRACE("(%p)->(%u,%p)\n", iface, prop, key);
1167 hr = MMDevPropStore_OpenPropKey(&This->parent->devguid, This->parent->flow, &propkey);
1171 if (RegEnumKeyExW(propkey, prop, buffer, &len, NULL, NULL, NULL, NULL) != ERROR_SUCCESS
1174 WARN("GetAt %u failed\n", prop);
1175 return E_INVALIDARG;
1177 RegCloseKey(propkey);
1179 CLSIDFromString(buffer, &key->fmtid);
1180 key->pid = atoiW(&buffer[40]);
1184 static HRESULT WINAPI MMDevPropStore_GetValue(IPropertyStore *iface, REFPROPERTYKEY key, PROPVARIANT *pv)
1186 MMDevPropStore *This = impl_from_IPropertyStore(iface);
1187 TRACE("(%p)->(\"%s,%u\", %p)\n", This, key ? debugstr_guid(&key->fmtid) : NULL, key ? key->pid : 0, pv);
1191 if (This->access != STGM_READ
1192 && This->access != STGM_READWRITE)
1193 return STG_E_ACCESSDENIED;
1196 if (IsEqualPropertyKey(*key, PKEY_AudioEndpoint_GUID))
1199 pv->u.pwszVal = CoTaskMemAlloc(39 * sizeof(WCHAR));
1201 return E_OUTOFMEMORY;
1202 StringFromGUID2(&This->parent->devguid, pv->u.pwszVal, 39);
1206 return MMDevice_GetPropValue(&This->parent->devguid, This->parent->flow, key, pv);
1209 static HRESULT WINAPI MMDevPropStore_SetValue(IPropertyStore *iface, REFPROPERTYKEY key, REFPROPVARIANT pv)
1211 MMDevPropStore *This = impl_from_IPropertyStore(iface);
1212 TRACE("(%p)->(\"%s,%u\", %p)\n", This, key ? debugstr_guid(&key->fmtid) : NULL, key ? key->pid : 0, pv);
1217 if (This->access != STGM_WRITE
1218 && This->access != STGM_READWRITE)
1219 return STG_E_ACCESSDENIED;
1220 return MMDevice_SetPropValue(&This->parent->devguid, This->parent->flow, key, pv);
1223 static HRESULT WINAPI MMDevPropStore_Commit(IPropertyStore *iface)
1229 static const IPropertyStoreVtbl MMDevPropVtbl =
1231 MMDevPropStore_QueryInterface,
1232 MMDevPropStore_AddRef,
1233 MMDevPropStore_Release,
1234 MMDevPropStore_GetCount,
1235 MMDevPropStore_GetAt,
1236 MMDevPropStore_GetValue,
1237 MMDevPropStore_SetValue,
1238 MMDevPropStore_Commit
1242 /* Property bag for IBaseFilter activation */
1243 static HRESULT WINAPI PB_QueryInterface(IPropertyBag *iface, REFIID riid, void **ppv)
1245 ERR("Should not be called\n");
1247 return E_NOINTERFACE;
1250 static ULONG WINAPI PB_AddRef(IPropertyBag *iface)
1252 ERR("Should not be called\n");
1256 static ULONG WINAPI PB_Release(IPropertyBag *iface)
1258 ERR("Should not be called\n");
1262 static HRESULT WINAPI PB_Read(IPropertyBag *iface, LPCOLESTR name, VARIANT *var, IErrorLog *log)
1264 static const WCHAR dsguid[] = { 'D','S','G','u','i','d', 0 };
1265 IPropertyBagImpl *This = impl_from_IPropertyBag(iface);
1266 TRACE("Trying to read %s, type %u\n", debugstr_w(name), var->n1.n2.vt);
1267 if (!lstrcmpW(name, dsguid))
1270 StringFromGUID2(&This->devguid, guidstr,sizeof(guidstr)/sizeof(*guidstr));
1271 var->n1.n2.vt = VT_BSTR;
1272 var->n1.n2.n3.bstrVal = SysAllocString(guidstr);
1275 ERR("Unknown property '%s' queried\n", debugstr_w(name));
1279 static HRESULT WINAPI PB_Write(IPropertyBag *iface, LPCOLESTR name, VARIANT *var)
1281 ERR("Should not be called\n");
1285 static const IPropertyBagVtbl PB_Vtbl =
1294 static ULONG WINAPI info_device_ps_AddRef(IPropertyStore *iface)
1299 static ULONG WINAPI info_device_ps_Release(IPropertyStore *iface)
1304 static HRESULT WINAPI info_device_ps_GetValue(IPropertyStore *iface,
1305 REFPROPERTYKEY key, PROPVARIANT *pv)
1307 TRACE("(static)->(\"%s,%u\", %p)\n", debugstr_guid(&key->fmtid), key ? key->pid : 0, pv);
1312 if (IsEqualPropertyKey(*key, DEVPKEY_Device_Driver))
1314 INT size = (lstrlenW(drvs.module_name) + 1) * sizeof(WCHAR);
1316 pv->u.pwszVal = CoTaskMemAlloc(size);
1318 return E_OUTOFMEMORY;
1319 memcpy(pv->u.pwszVal, drvs.module_name, size);
1323 return E_INVALIDARG;
1326 static const IPropertyStoreVtbl info_device_ps_Vtbl =
1329 info_device_ps_AddRef,
1330 info_device_ps_Release,
1333 info_device_ps_GetValue,
1338 static IPropertyStore info_device_ps = {
1339 &info_device_ps_Vtbl
1342 static ULONG WINAPI info_device_AddRef(IMMDevice *iface)
1347 static ULONG WINAPI info_device_Release(IMMDevice *iface)
1352 static HRESULT WINAPI info_device_OpenPropertyStore(IMMDevice *iface,
1353 DWORD access, IPropertyStore **ppv)
1355 TRACE("(static)->(%x, %p)\n", access, ppv);
1356 *ppv = &info_device_ps;
1360 static const IMMDeviceVtbl info_device_Vtbl =
1364 info_device_Release,
1366 info_device_OpenPropertyStore,
1371 static IMMDevice info_device = {