3 * Copyright 1998 Marcus Meissner
4 * Copyright 1998 Rob Riggs
5 * Copyright 2000-2002 TransGaming Technologies, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 * Most thread locking is complete. There may be a few race
22 * conditions still lurking.
25 * Implement SetCooperativeLevel properly (need to address focus issues)
26 * Implement DirectSound3DBuffers (stubs in place)
27 * Use hardware 3D support if available
28 * Add critical section locking inside Release and AddRef methods
29 * Handle static buffers - put those in hardware, non-static not in hardware
30 * Hardware DuplicateSoundBuffer
31 * Proper volume calculation for 3d buffers
32 * Remove DS_HEL_FRAGS and use mixer fragment length for it
45 #include "wine/debug.h"
58 #include "propkeydef.h"
62 #include "dsound_private.h"
64 WINE_DEFAULT_DEBUG_CHANNEL(dsound
);
66 struct list DSOUND_renderers
= LIST_INIT(DSOUND_renderers
);
67 CRITICAL_SECTION DSOUND_renderers_lock
;
68 static CRITICAL_SECTION_DEBUG DSOUND_renderers_lock_debug
=
70 0, 0, &DSOUND_renderers_lock
,
71 { &DSOUND_renderers_lock_debug
.ProcessLocksList
, &DSOUND_renderers_lock_debug
.ProcessLocksList
},
72 0, 0, { (DWORD_PTR
)(__FILE__
": DSOUND_renderers_lock") }
74 CRITICAL_SECTION DSOUND_renderers_lock
= { &DSOUND_renderers_lock_debug
, -1, 0, 0, 0, 0 };
76 /* Some applications expect the GUID pointers emitted from DirectSoundCaptureEnumerate to remain
77 * valid at least until the next time DirectSoundCaptureEnumerate is called, so we store them in
78 * these dynamically allocated arrays. */
79 GUID
*DSOUND_renderer_guids
;
80 GUID
*DSOUND_capture_guids
;
82 const WCHAR wine_vxd_drv
[] = L
"winemm.vxd";
84 /* All default settings, you most likely don't want to touch these, see wiki on UsefulRegistryKeys */
85 int ds_hel_buflen
= 32768 * 2;
88 * Get a config key from either the app-specific or the default config
91 static inline DWORD
get_config_key( HKEY defkey
, HKEY appkey
, const char *name
,
92 char *buffer
, DWORD size
)
94 if (appkey
&& !RegQueryValueExA( appkey
, name
, 0, NULL
, (LPBYTE
)buffer
, &size
)) return 0;
95 if (defkey
&& !RegQueryValueExA( defkey
, name
, 0, NULL
, (LPBYTE
)buffer
, &size
)) return 0;
96 return ERROR_FILE_NOT_FOUND
;
101 * Setup the dsound options.
104 void setup_dsound_options(void)
106 char buffer
[MAX_PATH
+16];
107 HKEY hkey
, appkey
= 0;
110 buffer
[MAX_PATH
]='\0';
112 /* @@ Wine registry key: HKCU\Software\Wine\DirectSound */
113 if (RegOpenKeyA( HKEY_CURRENT_USER
, "Software\\Wine\\DirectSound", &hkey
)) hkey
= 0;
115 len
= GetModuleFileNameA( 0, buffer
, MAX_PATH
);
116 if (len
&& len
< MAX_PATH
)
119 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\DirectSound */
120 if (!RegOpenKeyA( HKEY_CURRENT_USER
, "Software\\Wine\\AppDefaults", &tmpkey
))
122 char *p
, *appname
= buffer
;
123 if ((p
= strrchr( appname
, '/' ))) appname
= p
+ 1;
124 if ((p
= strrchr( appname
, '\\' ))) appname
= p
+ 1;
125 strcat( appname
, "\\DirectSound" );
126 TRACE("appname = [%s]\n", appname
);
127 if (RegOpenKeyA( tmpkey
, appname
, &appkey
)) appkey
= 0;
128 RegCloseKey( tmpkey
);
134 if (!get_config_key( hkey
, appkey
, "HelBuflen", buffer
, MAX_PATH
))
135 ds_hel_buflen
= atoi(buffer
);
137 if (appkey
) RegCloseKey( appkey
);
138 if (hkey
) RegCloseKey( hkey
);
140 TRACE("ds_hel_buflen = %d\n", ds_hel_buflen
);
143 static const char * get_device_id(LPCGUID pGuid
)
145 if (IsEqualGUID(&DSDEVID_DefaultPlayback
, pGuid
))
146 return "DSDEVID_DefaultPlayback";
147 else if (IsEqualGUID(&DSDEVID_DefaultVoicePlayback
, pGuid
))
148 return "DSDEVID_DefaultVoicePlayback";
149 else if (IsEqualGUID(&DSDEVID_DefaultCapture
, pGuid
))
150 return "DSDEVID_DefaultCapture";
151 else if (IsEqualGUID(&DSDEVID_DefaultVoiceCapture
, pGuid
))
152 return "DSDEVID_DefaultVoiceCapture";
153 return debugstr_guid(pGuid
);
156 static HRESULT
get_mmdevenum(IMMDeviceEnumerator
**devenum
)
160 init_hr
= CoInitialize(NULL
);
162 hr
= CoCreateInstance(&CLSID_MMDeviceEnumerator
, NULL
,
163 CLSCTX_INPROC_SERVER
, &IID_IMMDeviceEnumerator
, (void**)devenum
);
165 if(SUCCEEDED(init_hr
))
168 ERR("CoCreateInstance failed: %08lx\n", hr
);
175 static void release_mmdevenum(IMMDeviceEnumerator
*devenum
, HRESULT init_hr
)
177 IMMDeviceEnumerator_Release(devenum
);
178 if(SUCCEEDED(init_hr
))
182 static HRESULT
get_mmdevice_guid(IMMDevice
*device
, IPropertyStore
*ps
,
189 hr
= IMMDevice_OpenPropertyStore(device
, STGM_READ
, &ps
);
191 WARN("OpenPropertyStore failed: %08lx\n", hr
);
195 IPropertyStore_AddRef(ps
);
197 PropVariantInit(&pv
);
199 hr
= IPropertyStore_GetValue(ps
, &PKEY_AudioEndpoint_GUID
, &pv
);
201 IPropertyStore_Release(ps
);
202 WARN("GetValue(GUID) failed: %08lx\n", hr
);
206 CLSIDFromString(pv
.pwszVal
, guid
);
208 PropVariantClear(&pv
);
209 IPropertyStore_Release(ps
);
214 /***************************************************************************
215 * GetDeviceID [DSOUND.9]
217 * Retrieves unique identifier of default device specified
220 * pGuidSrc [I] Address of device GUID.
221 * pGuidDest [O] Address to receive unique device GUID.
225 * Failure: DSERR_INVALIDPARAM
228 * pGuidSrc is a valid device GUID or DSDEVID_DefaultPlayback,
229 * DSDEVID_DefaultCapture, DSDEVID_DefaultVoicePlayback, or
230 * DSDEVID_DefaultVoiceCapture.
231 * Returns pGuidSrc if pGuidSrc is a valid device or the device
232 * GUID for the specified constants.
234 HRESULT WINAPI
GetDeviceID(LPCGUID pGuidSrc
, LPGUID pGuidDest
)
236 IMMDeviceEnumerator
*devenum
;
237 EDataFlow flow
= (EDataFlow
)-1;
238 ERole role
= (ERole
)-1;
241 TRACE("(%s,%p)\n", get_device_id(pGuidSrc
),pGuidDest
);
243 if(!pGuidSrc
|| !pGuidDest
)
244 return DSERR_INVALIDPARAM
;
246 init_hr
= get_mmdevenum(&devenum
);
250 if(IsEqualGUID(&DSDEVID_DefaultPlayback
, pGuidSrc
)){
253 }else if(IsEqualGUID(&DSDEVID_DefaultVoicePlayback
, pGuidSrc
)){
254 role
= eCommunications
;
256 }else if(IsEqualGUID(&DSDEVID_DefaultCapture
, pGuidSrc
)){
259 }else if(IsEqualGUID(&DSDEVID_DefaultVoiceCapture
, pGuidSrc
)){
260 role
= eCommunications
;
264 if(role
!= (ERole
)-1 && flow
!= (EDataFlow
)-1){
267 hr
= IMMDeviceEnumerator_GetDefaultAudioEndpoint(devenum
,
268 flow
, role
, &device
);
270 WARN("GetDefaultAudioEndpoint failed: %08lx\n", hr
);
271 release_mmdevenum(devenum
, init_hr
);
272 return DSERR_NODRIVER
;
275 hr
= get_mmdevice_guid(device
, NULL
, pGuidDest
);
276 IMMDevice_Release(device
);
278 release_mmdevenum(devenum
, init_hr
);
280 return (hr
== S_OK
) ? DS_OK
: hr
;
283 release_mmdevenum(devenum
, init_hr
);
285 *pGuidDest
= *pGuidSrc
;
292 LPDSENUMCALLBACKA callA
;
296 static BOOL CALLBACK
a_to_w_callback(LPGUID guid
, LPCWSTR descW
, LPCWSTR modW
, LPVOID data
)
298 struct morecontext
*context
= data
;
299 char descA
[MAXPNAMELEN
], modA
[MAXPNAMELEN
];
301 WideCharToMultiByte(CP_ACP
, 0, descW
, -1, descA
, sizeof(descA
), NULL
, NULL
);
302 WideCharToMultiByte(CP_ACP
, 0, modW
, -1, modA
, sizeof(modA
), NULL
, NULL
);
304 return context
->callA(guid
, descA
, modA
, context
->data
);
307 /***************************************************************************
308 * DirectSoundEnumerateA [DSOUND.2]
310 * Enumerate all DirectSound drivers installed in the system
313 * lpDSEnumCallback [I] Address of callback function.
314 * lpContext [I] Address of user defined context passed to callback function.
318 * Failure: DSERR_INVALIDPARAM
320 HRESULT WINAPI
DirectSoundEnumerateA(
321 LPDSENUMCALLBACKA lpDSEnumCallback
,
324 struct morecontext context
;
326 if (lpDSEnumCallback
== NULL
) {
327 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
328 return DSERR_INVALIDPARAM
;
331 context
.callA
= lpDSEnumCallback
;
332 context
.data
= lpContext
;
334 return DirectSoundEnumerateW(a_to_w_callback
, &context
);
337 HRESULT
get_mmdevice(EDataFlow flow
, const GUID
*tgt
, IMMDevice
**device
)
339 IMMDeviceEnumerator
*devenum
;
340 IMMDeviceCollection
*coll
;
344 init_hr
= get_mmdevenum(&devenum
);
348 hr
= IMMDeviceEnumerator_EnumAudioEndpoints(devenum
, flow
,
349 DEVICE_STATE_ACTIVE
, &coll
);
351 WARN("EnumAudioEndpoints failed: %08lx\n", hr
);
352 release_mmdevenum(devenum
, init_hr
);
356 hr
= IMMDeviceCollection_GetCount(coll
, &count
);
358 IMMDeviceCollection_Release(coll
);
359 release_mmdevenum(devenum
, init_hr
);
360 WARN("GetCount failed: %08lx\n", hr
);
364 for(i
= 0; i
< count
; ++i
){
367 hr
= IMMDeviceCollection_Item(coll
, i
, device
);
371 hr
= get_mmdevice_guid(*device
, NULL
, &guid
);
373 IMMDevice_Release(*device
);
377 if(IsEqualGUID(&guid
, tgt
)){
378 IMMDeviceCollection_Release(coll
);
379 release_mmdevenum(devenum
, init_hr
);
383 IMMDevice_Release(*device
);
386 WARN("No device with GUID %s found!\n", wine_dbgstr_guid(tgt
));
388 IMMDeviceCollection_Release(coll
);
389 release_mmdevenum(devenum
, init_hr
);
391 return DSERR_INVALIDPARAM
;
394 static BOOL
send_device(IMMDevice
*device
, GUID
*guid
,
395 LPDSENUMCALLBACKW cb
, void *user
)
402 PropVariantInit(&pv
);
404 hr
= IMMDevice_OpenPropertyStore(device
, STGM_READ
, &ps
);
406 WARN("OpenPropertyStore failed: %08lx\n", hr
);
410 hr
= get_mmdevice_guid(device
, ps
, guid
);
412 IPropertyStore_Release(ps
);
416 hr
= IPropertyStore_GetValue(ps
,
417 (const PROPERTYKEY
*)&DEVPKEY_Device_FriendlyName
, &pv
);
419 IPropertyStore_Release(ps
);
420 WARN("GetValue(FriendlyName) failed: %08lx\n", hr
);
424 TRACE("Calling back with %s (%s)\n", wine_dbgstr_guid(guid
),
425 wine_dbgstr_w(pv
.pwszVal
));
427 keep_going
= cb(guid
, pv
.pwszVal
, wine_vxd_drv
, user
);
429 PropVariantClear(&pv
);
430 IPropertyStore_Release(ps
);
435 /* S_FALSE means the callback returned FALSE at some point
436 * S_OK means the callback always returned TRUE */
437 HRESULT
enumerate_mmdevices(EDataFlow flow
, GUID
*guids
,
438 LPDSENUMCALLBACKW cb
, void *user
)
440 IMMDeviceEnumerator
*devenum
;
441 IMMDeviceCollection
*coll
;
442 IMMDevice
*defdev
= NULL
;
447 init_hr
= get_mmdevenum(&devenum
);
451 hr
= IMMDeviceEnumerator_EnumAudioEndpoints(devenum
, flow
,
452 DEVICE_STATE_ACTIVE
, &coll
);
454 release_mmdevenum(devenum
, init_hr
);
455 WARN("EnumAudioEndpoints failed: %08lx\n", hr
);
459 hr
= IMMDeviceCollection_GetCount(coll
, &count
);
461 IMMDeviceCollection_Release(coll
);
462 release_mmdevenum(devenum
, init_hr
);
463 WARN("GetCount failed: %08lx\n", hr
);
469 IMMDeviceCollection_Release(coll
);
470 release_mmdevenum(devenum
, init_hr
);
474 guids
= malloc((count
+ 1) * sizeof(GUID
));
476 IMMDeviceCollection_Release(coll
);
477 release_mmdevenum(devenum
, init_hr
);
478 return E_OUTOFMEMORY
;
481 TRACE("Calling back with NULL (Primary Sound Driver)\n");
482 keep_going
= cb(NULL
, L
"Primary Sound Driver", L
"", user
);
484 /* always send the default device first */
486 hr
= IMMDeviceEnumerator_GetDefaultAudioEndpoint(devenum
, flow
,
487 eMultimedia
, &defdev
);
492 keep_going
= send_device(defdev
, &guids
[0], cb
, user
);
497 for(i
= 0; keep_going
&& i
< count
; ++i
){
500 hr
= IMMDeviceCollection_Item(coll
, i
, &device
);
502 WARN("Item failed: %08lx\n", hr
);
506 if(device
!= defdev
){
507 keep_going
= send_device(device
, &guids
[n
], cb
, user
);
511 IMMDevice_Release(device
);
515 IMMDevice_Release(defdev
);
516 IMMDeviceCollection_Release(coll
);
518 release_mmdevenum(devenum
, init_hr
);
520 return keep_going
? S_OK
: S_FALSE
;
523 /***************************************************************************
524 * DirectSoundEnumerateW [DSOUND.3]
526 * Enumerate all DirectSound drivers installed in the system
529 * lpDSEnumCallback [I] Address of callback function.
530 * lpContext [I] Address of user defined context passed to callback function.
534 * Failure: DSERR_INVALIDPARAM
536 HRESULT WINAPI
DirectSoundEnumerateW(
537 LPDSENUMCALLBACKW lpDSEnumCallback
,
542 TRACE("(%p,%p)\n", lpDSEnumCallback
, lpContext
);
544 if (lpDSEnumCallback
== NULL
) {
545 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
546 return DSERR_INVALIDPARAM
;
549 setup_dsound_options();
551 hr
= enumerate_mmdevices(eRender
, DSOUND_renderer_guids
,
552 lpDSEnumCallback
, lpContext
);
553 return SUCCEEDED(hr
) ? DS_OK
: hr
;
556 /***************************************************************************
557 * DirectSoundCaptureEnumerateA [DSOUND.7]
559 * Enumerate all DirectSound drivers installed in the system.
562 * lpDSEnumCallback [I] Address of callback function.
563 * lpContext [I] Address of user defined context passed to callback function.
567 * Failure: DSERR_INVALIDPARAM
569 HRESULT WINAPI
DirectSoundCaptureEnumerateA(
570 LPDSENUMCALLBACKA lpDSEnumCallback
,
573 struct morecontext context
;
575 if (lpDSEnumCallback
== NULL
) {
576 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
577 return DSERR_INVALIDPARAM
;
580 context
.callA
= lpDSEnumCallback
;
581 context
.data
= lpContext
;
583 return DirectSoundCaptureEnumerateW(a_to_w_callback
, &context
);
586 /***************************************************************************
587 * DirectSoundCaptureEnumerateW [DSOUND.8]
589 * Enumerate all DirectSound drivers installed in the system.
592 * lpDSEnumCallback [I] Address of callback function.
593 * lpContext [I] Address of user defined context passed to callback function.
597 * Failure: DSERR_INVALIDPARAM
600 DirectSoundCaptureEnumerateW(
601 LPDSENUMCALLBACKW lpDSEnumCallback
,
606 TRACE("(%p,%p)\n", lpDSEnumCallback
, lpContext
);
608 if (lpDSEnumCallback
== NULL
) {
609 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
610 return DSERR_INVALIDPARAM
;
613 setup_dsound_options();
615 hr
= enumerate_mmdevices(eCapture
, DSOUND_capture_guids
,
616 lpDSEnumCallback
, lpContext
);
617 return SUCCEEDED(hr
) ? DS_OK
: hr
;
620 /*******************************************************************************
621 * DirectSound ClassFactory
624 typedef HRESULT (*FnCreateInstance
)(REFIID riid
, LPVOID
*ppobj
);
627 IClassFactory IClassFactory_iface
;
629 FnCreateInstance pfnCreateInstance
;
632 static inline IClassFactoryImpl
*impl_from_IClassFactory(IClassFactory
*iface
)
634 return CONTAINING_RECORD(iface
, IClassFactoryImpl
, IClassFactory_iface
);
637 static HRESULT WINAPI
638 DSCF_QueryInterface(IClassFactory
*iface
, REFIID riid
, LPVOID
*ppobj
)
640 IClassFactoryImpl
*This
= impl_from_IClassFactory(iface
);
641 TRACE("(%p, %s, %p)\n", This
, debugstr_guid(riid
), ppobj
);
644 if (IsEqualIID(riid
, &IID_IUnknown
) ||
645 IsEqualIID(riid
, &IID_IClassFactory
))
648 IClassFactory_AddRef(iface
);
652 return E_NOINTERFACE
;
655 static ULONG WINAPI
DSCF_AddRef(LPCLASSFACTORY iface
)
660 static ULONG WINAPI
DSCF_Release(LPCLASSFACTORY iface
)
662 /* static class, won't be freed */
666 static HRESULT WINAPI
DSCF_CreateInstance(
667 LPCLASSFACTORY iface
,
672 IClassFactoryImpl
*This
= impl_from_IClassFactory(iface
);
673 TRACE("(%p, %p, %s, %p)\n", This
, pOuter
, debugstr_guid(riid
), ppobj
);
676 return CLASS_E_NOAGGREGATION
;
679 WARN("invalid parameter\n");
680 return DSERR_INVALIDPARAM
;
683 return This
->pfnCreateInstance(riid
, ppobj
);
686 static HRESULT WINAPI
DSCF_LockServer(LPCLASSFACTORY iface
, BOOL dolock
)
688 IClassFactoryImpl
*This
= impl_from_IClassFactory(iface
);
689 FIXME("(%p, %d) stub!\n", This
, dolock
);
693 static const IClassFactoryVtbl DSCF_Vtbl
= {
701 static IClassFactoryImpl DSOUND_CF
[] = {
702 { { &DSCF_Vtbl
}, &CLSID_DirectSound
, DSOUND_Create
},
703 { { &DSCF_Vtbl
}, &CLSID_DirectSound8
, DSOUND_Create8
},
704 { { &DSCF_Vtbl
}, &CLSID_DirectSoundCapture
, DSOUND_CaptureCreate
},
705 { { &DSCF_Vtbl
}, &CLSID_DirectSoundCapture8
, DSOUND_CaptureCreate8
},
706 { { &DSCF_Vtbl
}, &CLSID_DirectSoundFullDuplex
, DSOUND_FullDuplexCreate
},
707 { { &DSCF_Vtbl
}, &CLSID_DirectSoundPrivate
, IKsPrivatePropertySetImpl_Create
},
708 { { NULL
}, NULL
, NULL
}
711 /*******************************************************************************
712 * DllGetClassObject [DSOUND.@]
713 * Retrieves class object from a DLL object
716 * Docs say returns STDAPI
719 * rclsid [I] CLSID for the class object
720 * riid [I] Reference to identifier of interface for class object
721 * ppv [O] Address of variable to receive interface pointer for riid
725 * Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
728 HRESULT WINAPI
DllGetClassObject(REFCLSID rclsid
, REFIID riid
, LPVOID
*ppv
)
731 TRACE("(%s, %s, %p)\n", debugstr_guid(rclsid
), debugstr_guid(riid
), ppv
);
734 WARN("invalid parameter\n");
740 if (!IsEqualIID(riid
, &IID_IClassFactory
) &&
741 !IsEqualIID(riid
, &IID_IUnknown
)) {
742 WARN("no interface for %s\n", debugstr_guid(riid
));
743 return E_NOINTERFACE
;
746 while (NULL
!= DSOUND_CF
[i
].rclsid
) {
747 if (IsEqualGUID(rclsid
, DSOUND_CF
[i
].rclsid
)) {
748 DSCF_AddRef(&DSOUND_CF
[i
].IClassFactory_iface
);
749 *ppv
= &DSOUND_CF
[i
];
755 WARN("(%s, %s, %p): no class found.\n", debugstr_guid(rclsid
),
756 debugstr_guid(riid
), ppv
);
757 return CLASS_E_CLASSNOTAVAILABLE
;
761 #define INIT_GUID(guid, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \
762 guid.Data1 = l; guid.Data2 = w1; guid.Data3 = w2; \
763 guid.Data4[0] = b1; guid.Data4[1] = b2; guid.Data4[2] = b3; \
764 guid.Data4[3] = b4; guid.Data4[4] = b5; guid.Data4[5] = b6; \
765 guid.Data4[6] = b7; guid.Data4[7] = b8;
767 /***********************************************************************
768 * DllMain (DSOUND.init)
770 BOOL WINAPI
DllMain(HINSTANCE hInstDLL
, DWORD fdwReason
, LPVOID lpvReserved
)
772 TRACE("(%p %ld %p)\n", hInstDLL
, fdwReason
, lpvReserved
);
775 case DLL_PROCESS_ATTACH
:
776 DisableThreadLibraryCalls(hInstDLL
);
777 /* Increase refcount on dsound by 1 */
778 GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
, (LPCWSTR
)hInstDLL
, &hInstDLL
);
780 case DLL_PROCESS_DETACH
:
781 if (lpvReserved
) break;
782 DeleteCriticalSection(&DSOUND_renderers_lock
);