2 * Copyright 2009 Maarten Lankhorst
3 * Copyright 2011 Andrew Eikum for CodeWeavers
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
31 #include "propkeydef.h"
32 #include "mmdeviceapi.h"
35 #include "audioclient.h"
36 #include "endpointvolume.h"
37 #include "audiopolicy.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(mmdevapi
);
46 static HINSTANCE instance
;
50 const WCHAR drv_keyW
[] = {'S','o','f','t','w','a','r','e','\\',
51 'W','i','n','e','\\','D','r','i','v','e','r','s',0};
53 static const char *get_priority_string(int prio
)
56 case Priority_Unavailable
:
60 case Priority_Neutral
:
62 case Priority_Preferred
:
68 static BOOL
load_driver(const WCHAR
*name
, DriverFuncs
*driver
)
70 WCHAR driver_module
[264];
71 static const WCHAR wineW
[] = {'w','i','n','e',0};
72 static const WCHAR dotdrvW
[] = {'.','d','r','v',0};
74 lstrcpyW(driver_module
, wineW
);
75 lstrcatW(driver_module
, name
);
76 lstrcatW(driver_module
, dotdrvW
);
78 TRACE("Attempting to load %s\n", wine_dbgstr_w(driver_module
));
80 driver
->module
= LoadLibraryW(driver_module
);
82 TRACE("Unable to load %s: %u\n", wine_dbgstr_w(driver_module
),
87 #define LDFC(n) do { driver->p##n = (void*)GetProcAddress(driver->module, #n);\
88 if(!driver->p##n) { FreeLibrary(driver->module); return FALSE; } } while(0)
91 LDFC(GetAudioEndpoint
);
92 LDFC(GetAudioSessionManager
);
95 /* optional - do not fail if not found */
96 driver
->pGetPropValue
= (void*)GetProcAddress(driver
->module
, "GetPropValue");
98 driver
->priority
= driver
->pGetPriority();
99 lstrcpyW(driver
->module_name
, driver_module
);
101 TRACE("Successfully loaded %s with priority %s\n",
102 wine_dbgstr_w(driver_module
), get_priority_string(driver
->priority
));
107 static BOOL WINAPI
init_driver(INIT_ONCE
*once
, void *param
, void **context
)
109 static const WCHAR drv_value
[] = {'A','u','d','i','o',0};
111 static WCHAR default_list
[] = {'p','u','l','s','e',',','a','l','s','a',',','o','s','s',',',
112 'c','o','r','e','a','u','d','i','o',',','a','n','d','r','o','i','d',0};
116 WCHAR reg_list
[256], *p
, *next
, *driver_list
= default_list
;
118 if(RegOpenKeyW(HKEY_CURRENT_USER
, drv_keyW
, &key
) == ERROR_SUCCESS
){
119 DWORD size
= sizeof(reg_list
);
121 if(RegQueryValueExW(key
, drv_value
, 0, NULL
, (BYTE
*)reg_list
,
122 &size
) == ERROR_SUCCESS
){
123 if(reg_list
[0] == '\0'){
124 TRACE("User explicitly chose no driver\n");
129 driver_list
= reg_list
;
135 TRACE("Loading driver list %s\n", wine_dbgstr_w(driver_list
));
136 for(next
= p
= driver_list
; next
; p
= next
+ 1){
137 next
= wcschr(p
, ',');
141 driver
.priority
= Priority_Unavailable
;
142 if(load_driver(p
, &driver
)){
143 if(driver
.priority
== Priority_Unavailable
)
144 FreeLibrary(driver
.module
);
145 else if(!drvs
.module
|| driver
.priority
> drvs
.priority
){
146 TRACE("Selecting driver %s with priority %s\n",
147 wine_dbgstr_w(p
), get_priority_string(driver
.priority
));
149 FreeLibrary(drvs
.module
);
152 FreeLibrary(driver
.module
);
154 TRACE("Failed to load driver %s\n", wine_dbgstr_w(p
));
160 return drvs
.module
!= 0;
163 BOOL WINAPI
DllMain(HINSTANCE hinstDLL
, DWORD fdwReason
, LPVOID lpvReserved
)
165 TRACE("(0x%p, %d, %p)\n", hinstDLL
, fdwReason
, lpvReserved
);
169 case DLL_PROCESS_ATTACH
:
171 DisableThreadLibraryCalls(hinstDLL
);
173 case DLL_PROCESS_DETACH
:
183 HRESULT WINAPI
DllCanUnloadNow(void)
188 typedef HRESULT (*FnCreateInstance
)(REFIID riid
, LPVOID
*ppobj
);
191 IClassFactory IClassFactory_iface
;
193 FnCreateInstance pfnCreateInstance
;
196 static inline IClassFactoryImpl
*impl_from_IClassFactory(IClassFactory
*iface
)
198 return CONTAINING_RECORD(iface
, IClassFactoryImpl
, IClassFactory_iface
);
201 static HRESULT WINAPI
202 MMCF_QueryInterface(IClassFactory
*iface
, REFIID riid
, void **ppobj
)
204 IClassFactoryImpl
*This
= impl_from_IClassFactory(iface
);
205 TRACE("(%p, %s, %p)\n", This
, debugstr_guid(riid
), ppobj
);
208 if (IsEqualIID(riid
, &IID_IUnknown
) ||
209 IsEqualIID(riid
, &IID_IClassFactory
))
212 IClassFactory_AddRef(iface
);
216 return E_NOINTERFACE
;
219 static ULONG WINAPI
MMCF_AddRef(LPCLASSFACTORY iface
)
224 static ULONG WINAPI
MMCF_Release(LPCLASSFACTORY iface
)
226 /* static class, won't be freed */
230 static HRESULT WINAPI
MMCF_CreateInstance(
231 LPCLASSFACTORY iface
,
236 IClassFactoryImpl
*This
= impl_from_IClassFactory(iface
);
237 TRACE("(%p, %p, %s, %p)\n", This
, pOuter
, debugstr_guid(riid
), ppobj
);
240 return CLASS_E_NOAGGREGATION
;
243 WARN("invalid parameter\n");
247 return This
->pfnCreateInstance(riid
, ppobj
);
250 static HRESULT WINAPI
MMCF_LockServer(LPCLASSFACTORY iface
, BOOL dolock
)
252 IClassFactoryImpl
*This
= impl_from_IClassFactory(iface
);
253 FIXME("(%p, %d) stub!\n", This
, dolock
);
257 static const IClassFactoryVtbl MMCF_Vtbl
= {
265 static IClassFactoryImpl MMDEVAPI_CF
[] = {
266 { { &MMCF_Vtbl
}, &CLSID_MMDeviceEnumerator
, (FnCreateInstance
)MMDevEnum_Create
}
269 HRESULT WINAPI
DllGetClassObject(REFCLSID rclsid
, REFIID riid
, LPVOID
*ppv
)
271 static INIT_ONCE init_once
= INIT_ONCE_STATIC_INIT
;
273 TRACE("(%s, %s, %p)\n", debugstr_guid(rclsid
), debugstr_guid(riid
), ppv
);
275 if(!InitOnceExecuteOnce(&init_once
, init_driver
, NULL
, NULL
)) {
276 ERR("Driver initialization failed\n");
281 WARN("invalid parameter\n");
287 if (!IsEqualIID(riid
, &IID_IClassFactory
) &&
288 !IsEqualIID(riid
, &IID_IUnknown
)) {
289 WARN("no interface for %s\n", debugstr_guid(riid
));
290 return E_NOINTERFACE
;
293 for (i
= 0; i
< ARRAY_SIZE(MMDEVAPI_CF
); ++i
)
295 if (IsEqualGUID(rclsid
, MMDEVAPI_CF
[i
].rclsid
)) {
296 IClassFactory_AddRef(&MMDEVAPI_CF
[i
].IClassFactory_iface
);
297 *ppv
= &MMDEVAPI_CF
[i
];
302 WARN("(%s, %s, %p): no class found.\n", debugstr_guid(rclsid
),
303 debugstr_guid(riid
), ppv
);
304 return CLASS_E_CLASSNOTAVAILABLE
;
307 /***********************************************************************
308 * DllRegisterServer (MMDEVAPI.@)
310 HRESULT WINAPI
DllRegisterServer(void)
312 return __wine_register_resources( instance
);
315 /***********************************************************************
316 * DllUnregisterServer (MMDEVAPI.@)
318 HRESULT WINAPI
DllUnregisterServer(void)
320 return __wine_unregister_resources( instance
);
323 struct activate_async_op
{
324 IActivateAudioInterfaceAsyncOperation IActivateAudioInterfaceAsyncOperation_iface
;
327 IActivateAudioInterfaceCompletionHandler
*callback
;
329 IUnknown
*result_iface
;
332 static struct activate_async_op
*impl_from_IActivateAudioInterfaceAsyncOperation(IActivateAudioInterfaceAsyncOperation
*iface
)
334 return CONTAINING_RECORD(iface
, struct activate_async_op
, IActivateAudioInterfaceAsyncOperation_iface
);
337 static HRESULT WINAPI
activate_async_op_QueryInterface(IActivateAudioInterfaceAsyncOperation
*iface
,
338 REFIID riid
, void **ppv
)
340 struct activate_async_op
*This
= impl_from_IActivateAudioInterfaceAsyncOperation(iface
);
342 TRACE("(%p)->(%s, %p)\n", This
, debugstr_guid(riid
), ppv
);
347 if (IsEqualIID(riid
, &IID_IUnknown
) ||
348 IsEqualIID(riid
, &IID_IActivateAudioInterfaceAsyncOperation
)) {
349 *ppv
= &This
->IActivateAudioInterfaceAsyncOperation_iface
;
352 return E_NOINTERFACE
;
355 IUnknown_AddRef((IUnknown
*)*ppv
);
359 static ULONG WINAPI
activate_async_op_AddRef(IActivateAudioInterfaceAsyncOperation
*iface
)
361 struct activate_async_op
*This
= impl_from_IActivateAudioInterfaceAsyncOperation(iface
);
362 LONG ref
= InterlockedIncrement(&This
->ref
);
363 TRACE("(%p) refcount now %i\n", This
, ref
);
367 static ULONG WINAPI
activate_async_op_Release(IActivateAudioInterfaceAsyncOperation
*iface
)
369 struct activate_async_op
*This
= impl_from_IActivateAudioInterfaceAsyncOperation(iface
);
370 LONG ref
= InterlockedDecrement(&This
->ref
);
371 TRACE("(%p) refcount now %i\n", This
, ref
);
373 if(This
->result_iface
)
374 IUnknown_Release(This
->result_iface
);
375 IActivateAudioInterfaceCompletionHandler_Release(This
->callback
);
376 HeapFree(GetProcessHeap(), 0, This
);
381 static HRESULT WINAPI
activate_async_op_GetActivateResult(IActivateAudioInterfaceAsyncOperation
*iface
,
382 HRESULT
*result_hr
, IUnknown
**result_iface
)
384 struct activate_async_op
*This
= impl_from_IActivateAudioInterfaceAsyncOperation(iface
);
386 TRACE("(%p)->(%p, %p)\n", This
, result_hr
, result_iface
);
388 *result_hr
= This
->result_hr
;
390 if(This
->result_hr
== S_OK
){
391 *result_iface
= This
->result_iface
;
392 IUnknown_AddRef(*result_iface
);
398 static IActivateAudioInterfaceAsyncOperationVtbl IActivateAudioInterfaceAsyncOperation_vtbl
= {
399 activate_async_op_QueryInterface
,
400 activate_async_op_AddRef
,
401 activate_async_op_Release
,
402 activate_async_op_GetActivateResult
,
405 static DWORD WINAPI
activate_async_threadproc(void *user
)
407 struct activate_async_op
*op
= user
;
409 IActivateAudioInterfaceCompletionHandler_ActivateCompleted(op
->callback
, &op
->IActivateAudioInterfaceAsyncOperation_iface
);
411 IActivateAudioInterfaceAsyncOperation_Release(&op
->IActivateAudioInterfaceAsyncOperation_iface
);
416 static HRESULT
get_mmdevice_by_activatepath(const WCHAR
*path
, IMMDevice
**mmdev
)
418 IMMDeviceEnumerator
*devenum
;
421 static const WCHAR DEVINTERFACE_AUDIO_RENDER_WSTR
[] = L
"{E6327CAD-DCEC-4949-AE8A-991E976A79D2}";
422 static const WCHAR DEVINTERFACE_AUDIO_CAPTURE_WSTR
[] = L
"{2EEF81BE-33FA-4800-9670-1CD474972C3F}";
424 hr
= MMDevEnum_Create(&IID_IMMDeviceEnumerator
, (void**)&devenum
);
426 WARN("Failed to create MMDeviceEnumerator: %08x\n", hr
);
430 if (!lstrcmpiW(path
, DEVINTERFACE_AUDIO_RENDER_WSTR
)){
431 hr
= IMMDeviceEnumerator_GetDefaultAudioEndpoint(devenum
, eRender
, eMultimedia
, mmdev
);
432 } else if (!lstrcmpiW(path
, DEVINTERFACE_AUDIO_CAPTURE_WSTR
)){
433 hr
= IMMDeviceEnumerator_GetDefaultAudioEndpoint(devenum
, eCapture
, eMultimedia
, mmdev
);
435 FIXME("How to map path to device id? %s\n", debugstr_w(path
));
436 hr
= HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND
);
440 WARN("Failed to get requested device (%s): %08x\n", debugstr_w(path
), hr
);
442 hr
= HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND
);
445 IMMDeviceEnumerator_Release(devenum
);
450 /***********************************************************************
451 * ActivateAudioInterfaceAsync (MMDEVAPI.17)
453 HRESULT WINAPI
ActivateAudioInterfaceAsync(const WCHAR
*path
, REFIID riid
,
454 PROPVARIANT
*params
, IActivateAudioInterfaceCompletionHandler
*done_handler
,
455 IActivateAudioInterfaceAsyncOperation
**op_out
)
457 struct activate_async_op
*op
;
461 TRACE("(%s, %s, %p, %p, %p)\n", debugstr_w(path
), debugstr_guid(riid
),
462 params
, done_handler
, op_out
);
464 op
= HeapAlloc(GetProcessHeap(), 0, sizeof(*op
));
466 return E_OUTOFMEMORY
;
468 op
->ref
= 2; /* returned ref and threadproc ref */
469 op
->IActivateAudioInterfaceAsyncOperation_iface
.lpVtbl
= &IActivateAudioInterfaceAsyncOperation_vtbl
;
470 op
->callback
= done_handler
;
471 IActivateAudioInterfaceCompletionHandler_AddRef(done_handler
);
473 op
->result_hr
= get_mmdevice_by_activatepath(path
, &mmdev
);
474 if (SUCCEEDED(op
->result_hr
)) {
475 op
->result_hr
= IMMDevice_Activate(mmdev
, riid
, CLSCTX_INPROC_SERVER
, params
, (void**)&op
->result_iface
);
476 IMMDevice_Release(mmdev
);
478 op
->result_iface
= NULL
;
480 ht
= CreateThread(NULL
, 0, &activate_async_threadproc
, op
, 0, NULL
);
483 *op_out
= &op
->IActivateAudioInterfaceAsyncOperation_iface
;