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"
33 #include "mmdeviceapi.h"
36 #include "audioclient.h"
37 #include "endpointvolume.h"
38 #include "audiopolicy.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(mmdevapi
);
45 static const WCHAR software_mmdevapi
[] =
46 { 'S','o','f','t','w','a','r','e','\\',
47 'M','i','c','r','o','s','o','f','t','\\',
48 'W','i','n','d','o','w','s','\\',
49 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
50 'M','M','D','e','v','i','c','e','s','\\',
51 'A','u','d','i','o',0};
52 static const WCHAR reg_render
[] =
53 { 'R','e','n','d','e','r',0 };
54 static const WCHAR reg_capture
[] =
55 { 'C','a','p','t','u','r','e',0 };
56 static const WCHAR reg_devicestate
[] =
57 { 'D','e','v','i','c','e','S','t','a','t','e',0 };
58 static const WCHAR reg_properties
[] =
59 { 'P','r','o','p','e','r','t','i','e','s',0 };
61 static HKEY key_render
;
62 static HKEY key_capture
;
64 typedef struct MMDevPropStoreImpl
66 IPropertyStore IPropertyStore_iface
;
72 typedef struct MMDevEnumImpl
74 IMMDeviceEnumerator IMMDeviceEnumerator_iface
;
78 static MMDevEnumImpl
*MMDevEnumerator
;
79 static MMDevice
**MMDevice_head
;
80 static MMDevice
*MMDevice_def_rec
, *MMDevice_def_play
;
81 static DWORD MMDevice_count
;
82 static const IMMDeviceEnumeratorVtbl MMDevEnumVtbl
;
83 static const IMMDeviceCollectionVtbl MMDevColVtbl
;
84 static const IMMDeviceVtbl MMDeviceVtbl
;
85 static const IPropertyStoreVtbl MMDevPropVtbl
;
86 static const IMMEndpointVtbl MMEndpointVtbl
;
88 static IMMDevice info_device
;
90 typedef struct MMDevColImpl
92 IMMDeviceCollection IMMDeviceCollection_iface
;
98 typedef struct IPropertyBagImpl
{
99 IPropertyBag IPropertyBag_iface
;
103 static const IPropertyBagVtbl PB_Vtbl
;
105 static HRESULT
MMDevPropStore_Create(MMDevice
*This
, DWORD access
, IPropertyStore
**ppv
);
107 static inline MMDevPropStore
*impl_from_IPropertyStore(IPropertyStore
*iface
)
109 return CONTAINING_RECORD(iface
, MMDevPropStore
, IPropertyStore_iface
);
112 static inline MMDevEnumImpl
*impl_from_IMMDeviceEnumerator(IMMDeviceEnumerator
*iface
)
114 return CONTAINING_RECORD(iface
, MMDevEnumImpl
, IMMDeviceEnumerator_iface
);
117 static inline MMDevColImpl
*impl_from_IMMDeviceCollection(IMMDeviceCollection
*iface
)
119 return CONTAINING_RECORD(iface
, MMDevColImpl
, IMMDeviceCollection_iface
);
122 static inline IPropertyBagImpl
*impl_from_IPropertyBag(IPropertyBag
*iface
)
124 return CONTAINING_RECORD(iface
, IPropertyBagImpl
, IPropertyBag_iface
);
127 static const WCHAR propkey_formatW
[] = {
128 '{','%','0','8','X','-','%','0','4','X','-',
129 '%','0','4','X','-','%','0','2','X','%','0','2','X','-',
130 '%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X',
131 '%','0','2','X','%','0','2','X','}',',','%','d',0 };
133 static HRESULT
MMDevPropStore_OpenPropKey(const GUID
*guid
, DWORD flow
, HKEY
*propkey
)
138 StringFromGUID2(guid
, buffer
, 39);
139 if ((ret
= RegOpenKeyExW(flow
== eRender
? key_render
: key_capture
, buffer
, 0, KEY_READ
|KEY_WRITE
, &key
)) != ERROR_SUCCESS
)
141 WARN("Opening key %s failed with %u\n", debugstr_w(buffer
), ret
);
144 ret
= RegOpenKeyExW(key
, reg_properties
, 0, KEY_READ
|KEY_WRITE
, propkey
);
146 if (ret
!= ERROR_SUCCESS
)
148 WARN("Opening key %s failed with %u\n", debugstr_w(reg_properties
), ret
);
154 static HRESULT
MMDevice_GetPropValue(const GUID
*devguid
, DWORD flow
, REFPROPERTYKEY key
, PROPVARIANT
*pv
)
157 const GUID
*id
= &key
->fmtid
;
163 hr
= MMDevPropStore_OpenPropKey(devguid
, flow
, ®key
);
166 wsprintfW( buffer
, propkey_formatW
, id
->Data1
, id
->Data2
, id
->Data3
,
167 id
->Data4
[0], id
->Data4
[1], id
->Data4
[2], id
->Data4
[3],
168 id
->Data4
[4], id
->Data4
[5], id
->Data4
[6], id
->Data4
[7], key
->pid
);
169 ret
= RegGetValueW(regkey
, NULL
, buffer
, RRF_RT_ANY
, &type
, NULL
, &size
);
170 if (ret
!= ERROR_SUCCESS
)
172 WARN("Reading %s returned %d\n", debugstr_w(buffer
), ret
);
174 PropVariantClear(pv
);
183 pv
->u
.pwszVal
= CoTaskMemAlloc(size
);
187 RegGetValueW(regkey
, NULL
, buffer
, RRF_RT_REG_SZ
, NULL
, (BYTE
*)pv
->u
.pwszVal
, &size
);
193 RegGetValueW(regkey
, NULL
, buffer
, RRF_RT_REG_DWORD
, NULL
, (BYTE
*)&pv
->u
.ulVal
, &size
);
199 pv
->u
.blob
.cbSize
= size
;
200 pv
->u
.blob
.pBlobData
= CoTaskMemAlloc(size
);
201 if (!pv
->u
.blob
.pBlobData
)
204 RegGetValueW(regkey
, NULL
, buffer
, RRF_RT_REG_BINARY
, NULL
, (BYTE
*)pv
->u
.blob
.pBlobData
, &size
);
208 ERR("Unknown/unhandled type: %u\n", type
);
209 PropVariantClear(pv
);
216 static HRESULT
MMDevice_SetPropValue(const GUID
*devguid
, DWORD flow
, REFPROPERTYKEY key
, REFPROPVARIANT pv
)
219 const GUID
*id
= &key
->fmtid
;
224 hr
= MMDevPropStore_OpenPropKey(devguid
, flow
, ®key
);
227 wsprintfW( buffer
, propkey_formatW
, id
->Data1
, id
->Data2
, id
->Data3
,
228 id
->Data4
[0], id
->Data4
[1], id
->Data4
[2], id
->Data4
[3],
229 id
->Data4
[4], id
->Data4
[5], id
->Data4
[6], id
->Data4
[7], key
->pid
);
234 ret
= RegSetValueExW(regkey
, buffer
, 0, REG_DWORD
, (const BYTE
*)&pv
->u
.ulVal
, sizeof(DWORD
));
239 ret
= RegSetValueExW(regkey
, buffer
, 0, REG_BINARY
, pv
->u
.blob
.pBlobData
, pv
->u
.blob
.cbSize
);
240 TRACE("Blob %p %u\n", pv
->u
.blob
.pBlobData
, pv
->u
.blob
.cbSize
);
246 ret
= RegSetValueExW(regkey
, buffer
, 0, REG_SZ
, (const BYTE
*)pv
->u
.pwszVal
, sizeof(WCHAR
)*(1+lstrlenW(pv
->u
.pwszVal
)));
251 FIXME("Unhandled type %u\n", pv
->vt
);
256 TRACE("Writing %s returned %u\n", debugstr_w(buffer
), ret
);
260 /* Creates or updates the state of a device
261 * If GUID is null, a random guid will be assigned
262 * and the device will be created
264 static MMDevice
*MMDevice_Create(WCHAR
*name
, GUID
*id
, EDataFlow flow
, DWORD state
, BOOL setdefault
)
267 MMDevice
*cur
= NULL
;
271 static const PROPERTYKEY deviceinterface_key
= {
272 {0x233164c8, 0x1b2c, 0x4c7d, {0xbc, 0x68, 0xb6, 0x71, 0x68, 0x7a, 0x25, 0x67}}, 1
275 for (i
= 0; i
< MMDevice_count
; ++i
)
277 MMDevice
*device
= MMDevice_head
[i
];
278 if (device
->flow
== flow
&& IsEqualGUID(&device
->devguid
, id
)){
285 /* No device found, allocate new one */
286 cur
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*cur
));
290 cur
->IMMDevice_iface
.lpVtbl
= &MMDeviceVtbl
;
291 cur
->IMMEndpoint_iface
.lpVtbl
= &MMEndpointVtbl
;
293 InitializeCriticalSection(&cur
->crst
);
294 cur
->crst
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": MMDevice.crst");
297 MMDevice_head
= HeapAlloc(GetProcessHeap(), 0, sizeof(*MMDevice_head
));
299 MMDevice_head
= HeapReAlloc(GetProcessHeap(), 0, MMDevice_head
, sizeof(*MMDevice_head
)*(1+MMDevice_count
));
300 MMDevice_head
[MMDevice_count
++] = cur
;
301 }else if(cur
->ref
> 0)
302 WARN("Modifying an MMDevice with postitive reference count!\n");
304 HeapFree(GetProcessHeap(), 0, cur
->drv_id
);
311 StringFromGUID2(&cur
->devguid
, guidstr
, sizeof(guidstr
)/sizeof(*guidstr
));
318 if (RegCreateKeyExW(root
, guidstr
, 0, NULL
, 0, KEY_WRITE
|KEY_READ
, NULL
, &key
, NULL
) == ERROR_SUCCESS
)
321 RegSetValueExW(key
, reg_devicestate
, 0, REG_DWORD
, (const BYTE
*)&state
, sizeof(DWORD
));
322 if (!RegCreateKeyExW(key
, reg_properties
, 0, NULL
, 0, KEY_WRITE
|KEY_READ
, NULL
, &keyprop
, NULL
))
328 MMDevice_SetPropValue(id
, flow
, (const PROPERTYKEY
*)&DEVPKEY_Device_FriendlyName
, &pv
);
329 MMDevice_SetPropValue(id
, flow
, (const PROPERTYKEY
*)&DEVPKEY_Device_DeviceDesc
, &pv
);
331 pv
.u
.pwszVal
= guidstr
;
332 MMDevice_SetPropValue(id
, flow
, &deviceinterface_key
, &pv
);
334 RegCloseKey(keyprop
);
342 MMDevice_def_play
= cur
;
344 MMDevice_def_rec
= cur
;
349 static HRESULT
load_devices_from_reg(void)
356 ret
= RegCreateKeyExW(HKEY_LOCAL_MACHINE
, software_mmdevapi
, 0, NULL
, 0, KEY_WRITE
|KEY_READ
, NULL
, &root
, NULL
);
357 if (ret
== ERROR_SUCCESS
)
358 ret
= RegCreateKeyExW(root
, reg_capture
, 0, NULL
, 0, KEY_READ
|KEY_WRITE
, NULL
, &key_capture
, NULL
);
359 if (ret
== ERROR_SUCCESS
)
360 ret
= RegCreateKeyExW(root
, reg_render
, 0, NULL
, 0, KEY_READ
|KEY_WRITE
, NULL
, &key_render
, NULL
);
364 if (ret
!= ERROR_SUCCESS
)
366 RegCloseKey(key_capture
);
367 key_render
= key_capture
= NULL
;
368 WARN("Couldn't create key: %u\n", ret
);
376 PROPVARIANT pv
= { VT_EMPTY
};
378 len
= sizeof(guidvalue
)/sizeof(guidvalue
[0]);
379 ret
= RegEnumKeyExW(cur
, i
++, guidvalue
, &len
, NULL
, NULL
, NULL
, NULL
);
380 if (ret
== ERROR_NO_MORE_ITEMS
)
382 if (cur
== key_capture
)
391 if (ret
!= ERROR_SUCCESS
)
393 if (SUCCEEDED(CLSIDFromString(guidvalue
, &guid
))
394 && SUCCEEDED(MMDevice_GetPropValue(&guid
, curflow
, (const PROPERTYKEY
*)&DEVPKEY_Device_FriendlyName
, &pv
))
395 && pv
.vt
== VT_LPWSTR
)
397 DWORD size_bytes
= (strlenW(pv
.u
.pwszVal
) + 1) * sizeof(WCHAR
);
398 WCHAR
*name
= HeapAlloc(GetProcessHeap(), 0, size_bytes
);
399 memcpy(name
, pv
.u
.pwszVal
, size_bytes
);
400 MMDevice_Create(name
, &guid
, curflow
,
401 DEVICE_STATE_NOTPRESENT
, FALSE
);
402 CoTaskMemFree(pv
.u
.pwszVal
);
409 static HRESULT
set_format(MMDevice
*dev
)
412 IAudioClient
*client
;
414 PROPVARIANT pv
= { VT_EMPTY
};
416 hr
= drvs
.pGetAudioEndpoint(&dev
->devguid
, &dev
->IMMDevice_iface
, &client
);
420 hr
= IAudioClient_GetMixFormat(client
, &fmt
);
422 IAudioClient_Release(client
);
426 IAudioClient_Release(client
);
429 pv
.u
.blob
.cbSize
= sizeof(WAVEFORMATEX
) + fmt
->cbSize
;
430 pv
.u
.blob
.pBlobData
= (BYTE
*)fmt
;
431 MMDevice_SetPropValue(&dev
->devguid
, dev
->flow
,
432 &PKEY_AudioEngine_DeviceFormat
, &pv
);
433 MMDevice_SetPropValue(&dev
->devguid
, dev
->flow
,
434 &PKEY_AudioEngine_OEMFormat
, &pv
);
439 static HRESULT
load_driver_devices(EDataFlow flow
)
446 if(!drvs
.pGetEndpointIDs
)
449 hr
= drvs
.pGetEndpointIDs(flow
, &ids
, &guids
, &num
, &def
);
453 for(i
= 0; i
< num
; ++i
){
455 dev
= MMDevice_Create(ids
[i
], &guids
[i
], flow
, DEVICE_STATE_ACTIVE
,
460 HeapFree(GetProcessHeap(), 0, guids
);
461 HeapFree(GetProcessHeap(), 0, ids
);
466 static void MMDevice_Destroy(MMDevice
*This
)
469 TRACE("Freeing %s\n", debugstr_w(This
->drv_id
));
470 /* Since this function is called at destruction time, reordering of the list is unimportant */
471 for (i
= 0; i
< MMDevice_count
; ++i
)
473 if (MMDevice_head
[i
] == This
)
475 MMDevice_head
[i
] = MMDevice_head
[--MMDevice_count
];
479 This
->crst
.DebugInfo
->Spare
[0] = 0;
480 DeleteCriticalSection(&This
->crst
);
481 HeapFree(GetProcessHeap(), 0, This
->drv_id
);
482 HeapFree(GetProcessHeap(), 0, This
);
485 static inline MMDevice
*impl_from_IMMDevice(IMMDevice
*iface
)
487 return CONTAINING_RECORD(iface
, MMDevice
, IMMDevice_iface
);
490 static HRESULT WINAPI
MMDevice_QueryInterface(IMMDevice
*iface
, REFIID riid
, void **ppv
)
492 MMDevice
*This
= impl_from_IMMDevice(iface
);
493 TRACE("(%p)->(%s,%p)\n", iface
, debugstr_guid(riid
), ppv
);
498 if (IsEqualIID(riid
, &IID_IUnknown
)
499 || IsEqualIID(riid
, &IID_IMMDevice
))
501 else if (IsEqualIID(riid
, &IID_IMMEndpoint
))
502 *ppv
= &This
->IMMEndpoint_iface
;
505 IUnknown_AddRef((IUnknown
*)*ppv
);
508 WARN("Unknown interface %s\n", debugstr_guid(riid
));
509 return E_NOINTERFACE
;
512 static ULONG WINAPI
MMDevice_AddRef(IMMDevice
*iface
)
514 MMDevice
*This
= impl_from_IMMDevice(iface
);
517 ref
= InterlockedIncrement(&This
->ref
);
518 TRACE("Refcount now %i\n", ref
);
522 static ULONG WINAPI
MMDevice_Release(IMMDevice
*iface
)
524 MMDevice
*This
= impl_from_IMMDevice(iface
);
527 ref
= InterlockedDecrement(&This
->ref
);
528 TRACE("Refcount now %i\n", ref
);
532 static HRESULT WINAPI
MMDevice_Activate(IMMDevice
*iface
, REFIID riid
, DWORD clsctx
, PROPVARIANT
*params
, void **ppv
)
534 HRESULT hr
= E_NOINTERFACE
;
535 MMDevice
*This
= impl_from_IMMDevice(iface
);
537 TRACE("(%p)->(%p,%x,%p,%p)\n", iface
, riid
, clsctx
, params
, ppv
);
542 if (IsEqualIID(riid
, &IID_IAudioClient
)){
543 hr
= drvs
.pGetAudioEndpoint(&This
->devguid
, iface
, (IAudioClient
**)ppv
);
544 }else if (IsEqualIID(riid
, &IID_IAudioEndpointVolume
))
545 hr
= AudioEndpointVolume_Create(This
, (IAudioEndpointVolume
**)ppv
);
546 else if (IsEqualIID(riid
, &IID_IAudioSessionManager
)
547 || IsEqualIID(riid
, &IID_IAudioSessionManager2
))
549 hr
= drvs
.pGetAudioSessionManager(iface
, (IAudioSessionManager2
**)ppv
);
551 else if (IsEqualIID(riid
, &IID_IBaseFilter
))
553 if (This
->flow
== eRender
)
554 hr
= CoCreateInstance(&CLSID_DSoundRender
, NULL
, clsctx
, riid
, ppv
);
556 ERR("Not supported for recording?\n");
559 IPersistPropertyBag
*ppb
;
560 hr
= IUnknown_QueryInterface((IUnknown
*)*ppv
, &IID_IPersistPropertyBag
, (void*)&ppb
);
563 /* ::Load cannot assume the interface stays alive after the function returns,
564 * so just create the interface on the stack, saves a lot of complicated code */
565 IPropertyBagImpl bag
= { { &PB_Vtbl
}, This
->devguid
};
566 hr
= IPersistPropertyBag_Load(ppb
, &bag
.IPropertyBag_iface
, NULL
);
567 IPersistPropertyBag_Release(ppb
);
569 IBaseFilter_Release((IBaseFilter
*)*ppv
);
573 FIXME("Wine doesn't support IPersistPropertyBag on DSoundRender yet, ignoring..\n");
578 else if (IsEqualIID(riid
, &IID_IDeviceTopology
))
580 FIXME("IID_IDeviceTopology unsupported\n");
582 else if (IsEqualIID(riid
, &IID_IDirectSound
)
583 || IsEqualIID(riid
, &IID_IDirectSound8
))
585 if (This
->flow
== eRender
)
586 hr
= CoCreateInstance(&CLSID_DirectSound8
, NULL
, clsctx
, riid
, ppv
);
589 hr
= IDirectSound_Initialize((IDirectSound
*)*ppv
, &This
->devguid
);
591 IDirectSound_Release((IDirectSound
*)*ppv
);
594 else if (IsEqualIID(riid
, &IID_IDirectSoundCapture
)
595 || IsEqualIID(riid
, &IID_IDirectSoundCapture8
))
597 if (This
->flow
== eCapture
)
598 hr
= CoCreateInstance(&CLSID_DirectSoundCapture8
, NULL
, clsctx
, riid
, ppv
);
601 hr
= IDirectSoundCapture_Initialize((IDirectSoundCapture
*)*ppv
, &This
->devguid
);
603 IDirectSoundCapture_Release((IDirectSoundCapture
*)*ppv
);
607 ERR("Invalid/unknown iid %s\n", debugstr_guid(riid
));
612 TRACE("Returning %08x\n", hr
);
616 static HRESULT WINAPI
MMDevice_OpenPropertyStore(IMMDevice
*iface
, DWORD access
, IPropertyStore
**ppv
)
618 MMDevice
*This
= impl_from_IMMDevice(iface
);
619 TRACE("(%p)->(%x,%p)\n", This
, access
, ppv
);
623 return MMDevPropStore_Create(This
, access
, ppv
);
626 static HRESULT WINAPI
MMDevice_GetId(IMMDevice
*iface
, WCHAR
**itemid
)
628 MMDevice
*This
= impl_from_IMMDevice(iface
);
630 GUID
*id
= &This
->devguid
;
631 static const WCHAR formatW
[] = { '{','0','.','0','.','%','u','.','0','0','0','0','0','0','0','0','}','.',
632 '{','%','0','8','X','-','%','0','4','X','-',
633 '%','0','4','X','-','%','0','2','X','%','0','2','X','-',
634 '%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X',
635 '%','0','2','X','%','0','2','X','}',0 };
637 TRACE("(%p)->(%p)\n", This
, itemid
);
640 *itemid
= str
= CoTaskMemAlloc(56 * sizeof(WCHAR
));
642 return E_OUTOFMEMORY
;
643 wsprintfW( str
, formatW
, This
->flow
, id
->Data1
, id
->Data2
, id
->Data3
,
644 id
->Data4
[0], id
->Data4
[1], id
->Data4
[2], id
->Data4
[3],
645 id
->Data4
[4], id
->Data4
[5], id
->Data4
[6], id
->Data4
[7] );
646 TRACE("returning %s\n", wine_dbgstr_w(str
));
650 static HRESULT WINAPI
MMDevice_GetState(IMMDevice
*iface
, DWORD
*state
)
652 MMDevice
*This
= impl_from_IMMDevice(iface
);
653 TRACE("(%p)->(%p)\n", iface
, state
);
657 *state
= This
->state
;
661 static const IMMDeviceVtbl MMDeviceVtbl
=
663 MMDevice_QueryInterface
,
667 MMDevice_OpenPropertyStore
,
672 static inline MMDevice
*impl_from_IMMEndpoint(IMMEndpoint
*iface
)
674 return CONTAINING_RECORD(iface
, MMDevice
, IMMEndpoint_iface
);
677 static HRESULT WINAPI
MMEndpoint_QueryInterface(IMMEndpoint
*iface
, REFIID riid
, void **ppv
)
679 MMDevice
*This
= impl_from_IMMEndpoint(iface
);
680 TRACE("(%p)->(%s, %p)\n", This
, debugstr_guid(riid
), ppv
);
681 return IMMDevice_QueryInterface(&This
->IMMDevice_iface
, riid
, ppv
);
684 static ULONG WINAPI
MMEndpoint_AddRef(IMMEndpoint
*iface
)
686 MMDevice
*This
= impl_from_IMMEndpoint(iface
);
687 TRACE("(%p)\n", This
);
688 return IMMDevice_AddRef(&This
->IMMDevice_iface
);
691 static ULONG WINAPI
MMEndpoint_Release(IMMEndpoint
*iface
)
693 MMDevice
*This
= impl_from_IMMEndpoint(iface
);
694 TRACE("(%p)\n", This
);
695 return IMMDevice_Release(&This
->IMMDevice_iface
);
698 static HRESULT WINAPI
MMEndpoint_GetDataFlow(IMMEndpoint
*iface
, EDataFlow
*flow
)
700 MMDevice
*This
= impl_from_IMMEndpoint(iface
);
701 TRACE("(%p)->(%p)\n", This
, flow
);
708 static const IMMEndpointVtbl MMEndpointVtbl
=
710 MMEndpoint_QueryInterface
,
713 MMEndpoint_GetDataFlow
716 static HRESULT
MMDevCol_Create(IMMDeviceCollection
**ppv
, EDataFlow flow
, DWORD state
)
720 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(*This
));
723 return E_OUTOFMEMORY
;
724 This
->IMMDeviceCollection_iface
.lpVtbl
= &MMDevColVtbl
;
728 *ppv
= &This
->IMMDeviceCollection_iface
;
732 static void MMDevCol_Destroy(MMDevColImpl
*This
)
734 HeapFree(GetProcessHeap(), 0, This
);
737 static HRESULT WINAPI
MMDevCol_QueryInterface(IMMDeviceCollection
*iface
, REFIID riid
, void **ppv
)
739 MMDevColImpl
*This
= impl_from_IMMDeviceCollection(iface
);
740 TRACE("(%p)->(%s, %p)\n", This
, debugstr_guid(riid
), ppv
);
744 if (IsEqualIID(riid
, &IID_IUnknown
)
745 || IsEqualIID(riid
, &IID_IMMDeviceCollection
))
750 return E_NOINTERFACE
;
751 IUnknown_AddRef((IUnknown
*)*ppv
);
755 static ULONG WINAPI
MMDevCol_AddRef(IMMDeviceCollection
*iface
)
757 MMDevColImpl
*This
= impl_from_IMMDeviceCollection(iface
);
758 LONG ref
= InterlockedIncrement(&This
->ref
);
759 TRACE("Refcount now %i\n", ref
);
763 static ULONG WINAPI
MMDevCol_Release(IMMDeviceCollection
*iface
)
765 MMDevColImpl
*This
= impl_from_IMMDeviceCollection(iface
);
766 LONG ref
= InterlockedDecrement(&This
->ref
);
767 TRACE("Refcount now %i\n", ref
);
769 MMDevCol_Destroy(This
);
773 static HRESULT WINAPI
MMDevCol_GetCount(IMMDeviceCollection
*iface
, UINT
*numdevs
)
775 MMDevColImpl
*This
= impl_from_IMMDeviceCollection(iface
);
778 TRACE("(%p)->(%p)\n", This
, numdevs
);
783 for (i
= 0; i
< MMDevice_count
; ++i
)
785 MMDevice
*cur
= MMDevice_head
[i
];
786 if ((cur
->flow
== This
->flow
|| This
->flow
== eAll
)
787 && (cur
->state
& This
->state
))
793 static HRESULT WINAPI
MMDevCol_Item(IMMDeviceCollection
*iface
, UINT n
, IMMDevice
**dev
)
795 MMDevColImpl
*This
= impl_from_IMMDeviceCollection(iface
);
798 TRACE("(%p)->(%u, %p)\n", This
, n
, dev
);
802 for (j
= 0; j
< MMDevice_count
; ++j
)
804 MMDevice
*cur
= MMDevice_head
[j
];
805 if ((cur
->flow
== This
->flow
|| This
->flow
== eAll
)
806 && (cur
->state
& This
->state
)
809 *dev
= &cur
->IMMDevice_iface
;
810 IMMDevice_AddRef(*dev
);
814 WARN("Could not obtain item %u\n", n
);
819 static const IMMDeviceCollectionVtbl MMDevColVtbl
=
821 MMDevCol_QueryInterface
,
828 HRESULT
MMDevEnum_Create(REFIID riid
, void **ppv
)
830 MMDevEnumImpl
*This
= MMDevEnumerator
;
834 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(*This
));
837 return E_OUTOFMEMORY
;
839 This
->IMMDeviceEnumerator_iface
.lpVtbl
= &MMDevEnumVtbl
;
840 MMDevEnumerator
= This
;
842 load_devices_from_reg();
843 load_driver_devices(eRender
);
844 load_driver_devices(eCapture
);
846 return IUnknown_QueryInterface((IUnknown
*)This
, riid
, ppv
);
849 void MMDevEnum_Free(void)
851 while (MMDevice_count
)
852 MMDevice_Destroy(MMDevice_head
[0]);
853 RegCloseKey(key_render
);
854 RegCloseKey(key_capture
);
855 key_render
= key_capture
= NULL
;
856 HeapFree(GetProcessHeap(), 0, MMDevEnumerator
);
857 MMDevEnumerator
= NULL
;
860 static HRESULT WINAPI
MMDevEnum_QueryInterface(IMMDeviceEnumerator
*iface
, REFIID riid
, void **ppv
)
862 MMDevEnumImpl
*This
= impl_from_IMMDeviceEnumerator(iface
);
863 TRACE("(%p)->(%s, %p)\n", This
, debugstr_guid(riid
), ppv
);
867 if (IsEqualIID(riid
, &IID_IUnknown
)
868 || IsEqualIID(riid
, &IID_IMMDeviceEnumerator
))
873 return E_NOINTERFACE
;
874 IUnknown_AddRef((IUnknown
*)*ppv
);
878 static ULONG WINAPI
MMDevEnum_AddRef(IMMDeviceEnumerator
*iface
)
880 MMDevEnumImpl
*This
= impl_from_IMMDeviceEnumerator(iface
);
881 LONG ref
= InterlockedIncrement(&This
->ref
);
882 TRACE("Refcount now %i\n", ref
);
886 static ULONG WINAPI
MMDevEnum_Release(IMMDeviceEnumerator
*iface
)
888 MMDevEnumImpl
*This
= impl_from_IMMDeviceEnumerator(iface
);
889 LONG ref
= InterlockedDecrement(&This
->ref
);
892 TRACE("Refcount now %i\n", ref
);
896 static HRESULT WINAPI
MMDevEnum_EnumAudioEndpoints(IMMDeviceEnumerator
*iface
, EDataFlow flow
, DWORD mask
, IMMDeviceCollection
**devices
)
898 MMDevEnumImpl
*This
= impl_from_IMMDeviceEnumerator(iface
);
899 TRACE("(%p)->(%u,%u,%p)\n", This
, flow
, mask
, devices
);
903 if (flow
>= EDataFlow_enum_count
)
905 if (mask
& ~DEVICE_STATEMASK_ALL
)
907 return MMDevCol_Create(devices
, flow
, mask
);
910 static HRESULT WINAPI
MMDevEnum_GetDefaultAudioEndpoint(IMMDeviceEnumerator
*iface
, EDataFlow flow
, ERole role
, IMMDevice
**device
)
912 MMDevEnumImpl
*This
= impl_from_IMMDeviceEnumerator(iface
);
917 static const WCHAR slashW
[] = {'\\',0};
918 static const WCHAR reg_out_nameW
[] = {'D','e','f','a','u','l','t','O','u','t','p','u','t',0};
919 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};
920 static const WCHAR reg_in_nameW
[] = {'D','e','f','a','u','l','t','I','n','p','u','t',0};
921 static const WCHAR reg_vin_nameW
[] = {'D','e','f','a','u','l','t','V','o','i','c','e','I','n','p','u','t',0};
923 TRACE("(%p)->(%u,%u,%p)\n", This
, flow
, role
, device
);
928 if((flow
!= eRender
&& flow
!= eCapture
) ||
929 (role
!= eConsole
&& role
!= eMultimedia
&& role
!= eCommunications
)){
930 WARN("Unknown flow (%u) or role (%u)\n", flow
, role
);
936 if(!drvs
.module_name
[0])
939 lstrcpyW(reg_key
, drv_keyW
);
940 lstrcatW(reg_key
, slashW
);
941 lstrcatW(reg_key
, drvs
.module_name
);
943 if(RegOpenKeyW(HKEY_CURRENT_USER
, reg_key
, &key
) == ERROR_SUCCESS
){
944 const WCHAR
*reg_x_name
, *reg_vx_name
;
946 DWORD size
= sizeof(def_id
), state
;
949 reg_x_name
= reg_out_nameW
;
950 reg_vx_name
= reg_vout_nameW
;
952 reg_x_name
= reg_in_nameW
;
953 reg_vx_name
= reg_vin_nameW
;
956 if(role
== eCommunications
&&
957 RegQueryValueExW(key
, reg_vx_name
, 0, NULL
,
958 (BYTE
*)def_id
, &size
) == ERROR_SUCCESS
){
959 hr
= IMMDeviceEnumerator_GetDevice(iface
, def_id
, device
);
961 if(SUCCEEDED(IMMDevice_GetState(*device
, &state
)) &&
962 state
== DEVICE_STATE_ACTIVE
){
968 TRACE("Unable to find voice device %s\n", wine_dbgstr_w(def_id
));
971 if(RegQueryValueExW(key
, reg_x_name
, 0, NULL
,
972 (BYTE
*)def_id
, &size
) == ERROR_SUCCESS
){
973 hr
= IMMDeviceEnumerator_GetDevice(iface
, def_id
, device
);
975 if(SUCCEEDED(IMMDevice_GetState(*device
, &state
)) &&
976 state
== DEVICE_STATE_ACTIVE
){
982 TRACE("Unable to find device %s\n", wine_dbgstr_w(def_id
));
989 *device
= &MMDevice_def_play
->IMMDevice_iface
;
991 *device
= &MMDevice_def_rec
->IMMDevice_iface
;
995 IMMDevice_AddRef(*device
);
999 static HRESULT WINAPI
MMDevEnum_GetDevice(IMMDeviceEnumerator
*iface
, const WCHAR
*name
, IMMDevice
**device
)
1001 MMDevEnumImpl
*This
= impl_from_IMMDeviceEnumerator(iface
);
1003 IMMDevice
*dev
= NULL
;
1005 static const WCHAR wine_info_deviceW
[] = {'W','i','n','e',' ',
1006 'i','n','f','o',' ','d','e','v','i','c','e',0};
1008 TRACE("(%p)->(%s,%p)\n", This
, debugstr_w(name
), device
);
1010 if(!name
|| !device
)
1013 if(!lstrcmpW(name
, wine_info_deviceW
)){
1014 *device
= &info_device
;
1018 for (i
= 0; i
< MMDevice_count
; ++i
)
1021 dev
= &MMDevice_head
[i
]->IMMDevice_iface
;
1022 IMMDevice_GetId(dev
, &str
);
1024 if (str
&& !lstrcmpW(str
, name
))
1027 IUnknown_AddRef(dev
);
1033 TRACE("Could not find device %s\n", debugstr_w(name
));
1034 return E_INVALIDARG
;
1037 static HRESULT WINAPI
MMDevEnum_RegisterEndpointNotificationCallback(IMMDeviceEnumerator
*iface
, IMMNotificationClient
*client
)
1039 MMDevEnumImpl
*This
= impl_from_IMMDeviceEnumerator(iface
);
1040 TRACE("(%p)->(%p)\n", This
, client
);
1045 static HRESULT WINAPI
MMDevEnum_UnregisterEndpointNotificationCallback(IMMDeviceEnumerator
*iface
, IMMNotificationClient
*client
)
1047 MMDevEnumImpl
*This
= impl_from_IMMDeviceEnumerator(iface
);
1048 TRACE("(%p)->(%p)\n", This
, client
);
1053 static const IMMDeviceEnumeratorVtbl MMDevEnumVtbl
=
1055 MMDevEnum_QueryInterface
,
1058 MMDevEnum_EnumAudioEndpoints
,
1059 MMDevEnum_GetDefaultAudioEndpoint
,
1060 MMDevEnum_GetDevice
,
1061 MMDevEnum_RegisterEndpointNotificationCallback
,
1062 MMDevEnum_UnregisterEndpointNotificationCallback
1065 static HRESULT
MMDevPropStore_Create(MMDevice
*parent
, DWORD access
, IPropertyStore
**ppv
)
1067 MMDevPropStore
*This
;
1068 if (access
!= STGM_READ
1069 && access
!= STGM_WRITE
1070 && access
!= STGM_READWRITE
)
1072 WARN("Invalid access %08x\n", access
);
1073 return E_INVALIDARG
;
1075 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(*This
));
1076 *ppv
= &This
->IPropertyStore_iface
;
1078 return E_OUTOFMEMORY
;
1079 This
->IPropertyStore_iface
.lpVtbl
= &MMDevPropVtbl
;
1081 This
->parent
= parent
;
1082 This
->access
= access
;
1086 static void MMDevPropStore_Destroy(MMDevPropStore
*This
)
1088 HeapFree(GetProcessHeap(), 0, This
);
1091 static HRESULT WINAPI
MMDevPropStore_QueryInterface(IPropertyStore
*iface
, REFIID riid
, void **ppv
)
1093 MMDevPropStore
*This
= impl_from_IPropertyStore(iface
);
1094 TRACE("(%p)->(%s, %p)\n", This
, debugstr_guid(riid
), ppv
);
1098 if (IsEqualIID(riid
, &IID_IUnknown
)
1099 || IsEqualIID(riid
, &IID_IPropertyStore
))
1104 return E_NOINTERFACE
;
1105 IUnknown_AddRef((IUnknown
*)*ppv
);
1109 static ULONG WINAPI
MMDevPropStore_AddRef(IPropertyStore
*iface
)
1111 MMDevPropStore
*This
= impl_from_IPropertyStore(iface
);
1112 LONG ref
= InterlockedIncrement(&This
->ref
);
1113 TRACE("Refcount now %i\n", ref
);
1117 static ULONG WINAPI
MMDevPropStore_Release(IPropertyStore
*iface
)
1119 MMDevPropStore
*This
= impl_from_IPropertyStore(iface
);
1120 LONG ref
= InterlockedDecrement(&This
->ref
);
1121 TRACE("Refcount now %i\n", ref
);
1123 MMDevPropStore_Destroy(This
);
1127 static HRESULT WINAPI
MMDevPropStore_GetCount(IPropertyStore
*iface
, DWORD
*nprops
)
1129 MMDevPropStore
*This
= impl_from_IPropertyStore(iface
);
1135 TRACE("(%p)->(%p)\n", iface
, nprops
);
1138 hr
= MMDevPropStore_OpenPropKey(&This
->parent
->devguid
, This
->parent
->flow
, &propkey
);
1143 DWORD len
= sizeof(buffer
)/sizeof(*buffer
);
1144 if (RegEnumKeyExW(propkey
, i
, buffer
, &len
, NULL
, NULL
, NULL
, NULL
) != ERROR_SUCCESS
)
1148 RegCloseKey(propkey
);
1149 TRACE("Returning %i\n", i
);
1154 static HRESULT WINAPI
MMDevPropStore_GetAt(IPropertyStore
*iface
, DWORD prop
, PROPERTYKEY
*key
)
1156 MMDevPropStore
*This
= impl_from_IPropertyStore(iface
);
1158 DWORD len
= sizeof(buffer
)/sizeof(*buffer
);
1162 TRACE("(%p)->(%u,%p)\n", iface
, prop
, key
);
1166 hr
= MMDevPropStore_OpenPropKey(&This
->parent
->devguid
, This
->parent
->flow
, &propkey
);
1170 if (RegEnumKeyExW(propkey
, prop
, buffer
, &len
, NULL
, NULL
, NULL
, NULL
) != ERROR_SUCCESS
1173 WARN("GetAt %u failed\n", prop
);
1174 return E_INVALIDARG
;
1176 RegCloseKey(propkey
);
1178 CLSIDFromString(buffer
, &key
->fmtid
);
1179 key
->pid
= atoiW(&buffer
[40]);
1183 static HRESULT WINAPI
MMDevPropStore_GetValue(IPropertyStore
*iface
, REFPROPERTYKEY key
, PROPVARIANT
*pv
)
1185 MMDevPropStore
*This
= impl_from_IPropertyStore(iface
);
1186 TRACE("(%p)->(\"%s,%u\", %p)\n", This
, key
? debugstr_guid(&key
->fmtid
) : NULL
, key
? key
->pid
: 0, pv
);
1190 if (This
->access
!= STGM_READ
1191 && This
->access
!= STGM_READWRITE
)
1192 return STG_E_ACCESSDENIED
;
1195 if (IsEqualPropertyKey(*key
, PKEY_AudioEndpoint_GUID
))
1198 pv
->u
.pwszVal
= CoTaskMemAlloc(39 * sizeof(WCHAR
));
1200 return E_OUTOFMEMORY
;
1201 StringFromGUID2(&This
->parent
->devguid
, pv
->u
.pwszVal
, 39);
1205 return MMDevice_GetPropValue(&This
->parent
->devguid
, This
->parent
->flow
, key
, pv
);
1208 static HRESULT WINAPI
MMDevPropStore_SetValue(IPropertyStore
*iface
, REFPROPERTYKEY key
, REFPROPVARIANT pv
)
1210 MMDevPropStore
*This
= impl_from_IPropertyStore(iface
);
1211 TRACE("(%p)->(\"%s,%u\", %p)\n", This
, key
? debugstr_guid(&key
->fmtid
) : NULL
, key
? key
->pid
: 0, pv
);
1216 if (This
->access
!= STGM_WRITE
1217 && This
->access
!= STGM_READWRITE
)
1218 return STG_E_ACCESSDENIED
;
1219 return MMDevice_SetPropValue(&This
->parent
->devguid
, This
->parent
->flow
, key
, pv
);
1222 static HRESULT WINAPI
MMDevPropStore_Commit(IPropertyStore
*iface
)
1228 static const IPropertyStoreVtbl MMDevPropVtbl
=
1230 MMDevPropStore_QueryInterface
,
1231 MMDevPropStore_AddRef
,
1232 MMDevPropStore_Release
,
1233 MMDevPropStore_GetCount
,
1234 MMDevPropStore_GetAt
,
1235 MMDevPropStore_GetValue
,
1236 MMDevPropStore_SetValue
,
1237 MMDevPropStore_Commit
1241 /* Property bag for IBaseFilter activation */
1242 static HRESULT WINAPI
PB_QueryInterface(IPropertyBag
*iface
, REFIID riid
, void **ppv
)
1244 ERR("Should not be called\n");
1246 return E_NOINTERFACE
;
1249 static ULONG WINAPI
PB_AddRef(IPropertyBag
*iface
)
1251 ERR("Should not be called\n");
1255 static ULONG WINAPI
PB_Release(IPropertyBag
*iface
)
1257 ERR("Should not be called\n");
1261 static HRESULT WINAPI
PB_Read(IPropertyBag
*iface
, LPCOLESTR name
, VARIANT
*var
, IErrorLog
*log
)
1263 static const WCHAR dsguid
[] = { 'D','S','G','u','i','d', 0 };
1264 IPropertyBagImpl
*This
= impl_from_IPropertyBag(iface
);
1265 TRACE("Trying to read %s, type %u\n", debugstr_w(name
), var
->n1
.n2
.vt
);
1266 if (!lstrcmpW(name
, dsguid
))
1269 StringFromGUID2(&This
->devguid
, guidstr
,sizeof(guidstr
)/sizeof(*guidstr
));
1270 var
->n1
.n2
.vt
= VT_BSTR
;
1271 var
->n1
.n2
.n3
.bstrVal
= SysAllocString(guidstr
);
1274 ERR("Unknown property '%s' queried\n", debugstr_w(name
));
1278 static HRESULT WINAPI
PB_Write(IPropertyBag
*iface
, LPCOLESTR name
, VARIANT
*var
)
1280 ERR("Should not be called\n");
1284 static const IPropertyBagVtbl PB_Vtbl
=
1293 static ULONG WINAPI
info_device_ps_AddRef(IPropertyStore
*iface
)
1298 static ULONG WINAPI
info_device_ps_Release(IPropertyStore
*iface
)
1303 static HRESULT WINAPI
info_device_ps_GetValue(IPropertyStore
*iface
,
1304 REFPROPERTYKEY key
, PROPVARIANT
*pv
)
1306 TRACE("(static)->(\"%s,%u\", %p)\n", debugstr_guid(&key
->fmtid
), key
? key
->pid
: 0, pv
);
1311 if (IsEqualPropertyKey(*key
, DEVPKEY_Device_Driver
))
1313 INT size
= (lstrlenW(drvs
.module_name
) + 1) * sizeof(WCHAR
);
1315 pv
->u
.pwszVal
= CoTaskMemAlloc(size
);
1317 return E_OUTOFMEMORY
;
1318 memcpy(pv
->u
.pwszVal
, drvs
.module_name
, size
);
1322 return E_INVALIDARG
;
1325 static const IPropertyStoreVtbl info_device_ps_Vtbl
=
1328 info_device_ps_AddRef
,
1329 info_device_ps_Release
,
1332 info_device_ps_GetValue
,
1337 static IPropertyStore info_device_ps
= {
1338 &info_device_ps_Vtbl
1341 static ULONG WINAPI
info_device_AddRef(IMMDevice
*iface
)
1346 static ULONG WINAPI
info_device_Release(IMMDevice
*iface
)
1351 static HRESULT WINAPI
info_device_OpenPropertyStore(IMMDevice
*iface
,
1352 DWORD access
, IPropertyStore
**ppv
)
1354 TRACE("(static)->(%x, %p)\n", access
, ppv
);
1355 *ppv
= &info_device_ps
;
1359 static const IMMDeviceVtbl info_device_Vtbl
=
1363 info_device_Release
,
1365 info_device_OpenPropertyStore
,
1370 static IMMDevice info_device
= {