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
38 #define NONAMELESSSTRUCT
39 #define NONAMELESSUNION
48 #include "wine/debug.h"
56 #include "dsound_private.h"
58 WINE_DEFAULT_DEBUG_CHANNEL(dsound
);
60 #define DS_HEL_BUFLEN 0x8000 /* HEL: The buffer length of the emulated buffer */
61 #define DS_SND_QUEUE_MAX 10 /* max number of fragments to prebuffer, each fragment is approximately 10 ms long */
62 #define DS_SND_QUEUE_MIN 6 /* If the minimum of prebuffered fragments go below this, forcibly take all locks to prevent underruns */
64 DirectSoundDevice
* DSOUND_renderer
[MAXWAVEDRIVERS
];
65 GUID DSOUND_renderer_guids
[MAXWAVEDRIVERS
];
66 GUID DSOUND_capture_guids
[MAXWAVEDRIVERS
];
68 HRESULT
mmErr(UINT err
)
71 case MMSYSERR_NOERROR
:
73 case MMSYSERR_ALLOCATED
:
74 return DSERR_ALLOCATED
;
76 case MMSYSERR_INVALHANDLE
:
77 case WAVERR_STILLPLAYING
:
78 return DSERR_GENERIC
; /* FIXME */
79 case MMSYSERR_NODRIVER
:
80 return DSERR_NODRIVER
;
82 return DSERR_OUTOFMEMORY
;
83 case MMSYSERR_INVALPARAM
:
84 case WAVERR_BADFORMAT
:
85 case WAVERR_UNPREPARED
:
86 return DSERR_INVALIDPARAM
;
87 case MMSYSERR_NOTSUPPORTED
:
88 return DSERR_UNSUPPORTED
;
90 FIXME("Unknown MMSYS error %d\n",err
);
95 int ds_emuldriver
= 0;
96 int ds_hel_buflen
= DS_HEL_BUFLEN
;
97 int ds_snd_queue_max
= DS_SND_QUEUE_MAX
;
98 int ds_snd_queue_min
= DS_SND_QUEUE_MIN
;
99 int ds_hw_accel
= DS_HW_ACCEL_FULL
;
100 int ds_default_playback
= 0;
101 int ds_default_capture
= 0;
102 int ds_default_sample_rate
= 44100;
103 int ds_default_bits_per_sample
= 16;
106 * Get a config key from either the app-specific or the default config
109 static inline DWORD
get_config_key( HKEY defkey
, HKEY appkey
, const char *name
,
110 char *buffer
, DWORD size
)
112 if (appkey
&& !RegQueryValueExA( appkey
, name
, 0, NULL
, (LPBYTE
)buffer
, &size
)) return 0;
113 if (defkey
&& !RegQueryValueExA( defkey
, name
, 0, NULL
, (LPBYTE
)buffer
, &size
)) return 0;
114 return ERROR_FILE_NOT_FOUND
;
119 * Setup the dsound options.
122 void setup_dsound_options(void)
124 char buffer
[MAX_PATH
+16];
125 HKEY hkey
, appkey
= 0;
128 buffer
[MAX_PATH
]='\0';
130 /* @@ Wine registry key: HKCU\Software\Wine\DirectSound */
131 if (RegOpenKeyA( HKEY_CURRENT_USER
, "Software\\Wine\\DirectSound", &hkey
)) hkey
= 0;
133 len
= GetModuleFileNameA( 0, buffer
, MAX_PATH
);
134 if (len
&& len
< MAX_PATH
)
137 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\DirectSound */
138 if (!RegOpenKeyA( HKEY_CURRENT_USER
, "Software\\Wine\\AppDefaults", &tmpkey
))
140 char *p
, *appname
= buffer
;
141 if ((p
= strrchr( appname
, '/' ))) appname
= p
+ 1;
142 if ((p
= strrchr( appname
, '\\' ))) appname
= p
+ 1;
143 strcat( appname
, "\\DirectSound" );
144 TRACE("appname = [%s]\n", appname
);
145 if (RegOpenKeyA( tmpkey
, appname
, &appkey
)) appkey
= 0;
146 RegCloseKey( tmpkey
);
152 if (!get_config_key( hkey
, appkey
, "EmulDriver", buffer
, MAX_PATH
))
153 ds_emuldriver
= strcmp(buffer
, "N");
155 if (!get_config_key( hkey
, appkey
, "HelBuflen", buffer
, MAX_PATH
))
156 ds_hel_buflen
= atoi(buffer
);
158 if (!get_config_key( hkey
, appkey
, "SndQueueMax", buffer
, MAX_PATH
))
159 ds_snd_queue_max
= atoi(buffer
);
161 if (!get_config_key( hkey
, appkey
, "SndQueueMin", buffer
, MAX_PATH
))
162 ds_snd_queue_min
= atoi(buffer
);
164 if (!get_config_key( hkey
, appkey
, "HardwareAcceleration", buffer
, MAX_PATH
)) {
165 if (strcmp(buffer
, "Full") == 0)
166 ds_hw_accel
= DS_HW_ACCEL_FULL
;
167 else if (strcmp(buffer
, "Standard") == 0)
168 ds_hw_accel
= DS_HW_ACCEL_STANDARD
;
169 else if (strcmp(buffer
, "Basic") == 0)
170 ds_hw_accel
= DS_HW_ACCEL_BASIC
;
171 else if (strcmp(buffer
, "Emulation") == 0)
172 ds_hw_accel
= DS_HW_ACCEL_EMULATION
;
175 if (!get_config_key( hkey
, appkey
, "DefaultPlayback", buffer
, MAX_PATH
))
176 ds_default_playback
= atoi(buffer
);
178 if (!get_config_key( hkey
, appkey
, "DefaultCapture", buffer
, MAX_PATH
))
179 ds_default_capture
= atoi(buffer
);
181 if (!get_config_key( hkey
, appkey
, "DefaultSampleRate", buffer
, MAX_PATH
))
182 ds_default_sample_rate
= atoi(buffer
);
184 if (!get_config_key( hkey
, appkey
, "DefaultBitsPerSample", buffer
, MAX_PATH
))
185 ds_default_bits_per_sample
= atoi(buffer
);
187 if (appkey
) RegCloseKey( appkey
);
188 if (hkey
) RegCloseKey( hkey
);
191 WARN("ds_emuldriver = %d (default=0)\n",ds_emuldriver
);
192 if (ds_hel_buflen
!= DS_HEL_BUFLEN
)
193 WARN("ds_hel_buflen = %d (default=%d)\n",ds_hel_buflen
,DS_HEL_BUFLEN
);
194 if (ds_snd_queue_max
!= DS_SND_QUEUE_MAX
)
195 WARN("ds_snd_queue_max = %d (default=%d)\n",ds_snd_queue_max
,DS_SND_QUEUE_MAX
);
196 if (ds_snd_queue_min
!= DS_SND_QUEUE_MIN
)
197 WARN("ds_snd_queue_min = %d (default=%d)\n",ds_snd_queue_min
,DS_SND_QUEUE_MIN
);
198 if (ds_hw_accel
!= DS_HW_ACCEL_FULL
)
199 WARN("ds_hw_accel = %s (default=Full)\n",
200 ds_hw_accel
==DS_HW_ACCEL_FULL
? "Full" :
201 ds_hw_accel
==DS_HW_ACCEL_STANDARD
? "Standard" :
202 ds_hw_accel
==DS_HW_ACCEL_BASIC
? "Basic" :
203 ds_hw_accel
==DS_HW_ACCEL_EMULATION
? "Emulation" :
205 if (ds_default_playback
!= 0)
206 WARN("ds_default_playback = %d (default=0)\n",ds_default_playback
);
207 if (ds_default_capture
!= 0)
208 WARN("ds_default_capture = %d (default=0)\n",ds_default_playback
);
209 if (ds_default_sample_rate
!= 44100)
210 WARN("ds_default_sample_rate = %d (default=44100)\n",ds_default_sample_rate
);
211 if (ds_default_bits_per_sample
!= 16)
212 WARN("ds_default_bits_per_sample = %d (default=16)\n",ds_default_bits_per_sample
);
215 static const char * get_device_id(LPCGUID pGuid
)
217 if (IsEqualGUID(&DSDEVID_DefaultPlayback
, pGuid
))
218 return "DSDEVID_DefaultPlayback";
219 else if (IsEqualGUID(&DSDEVID_DefaultVoicePlayback
, pGuid
))
220 return "DSDEVID_DefaultVoicePlayback";
221 else if (IsEqualGUID(&DSDEVID_DefaultCapture
, pGuid
))
222 return "DSDEVID_DefaultCapture";
223 else if (IsEqualGUID(&DSDEVID_DefaultVoiceCapture
, pGuid
))
224 return "DSDEVID_DefaultVoiceCapture";
225 return debugstr_guid(pGuid
);
228 /***************************************************************************
229 * GetDeviceID [DSOUND.9]
231 * Retrieves unique identifier of default device specified
234 * pGuidSrc [I] Address of device GUID.
235 * pGuidDest [O] Address to receive unique device GUID.
239 * Failure: DSERR_INVALIDPARAM
242 * pGuidSrc is a valid device GUID or DSDEVID_DefaultPlayback,
243 * DSDEVID_DefaultCapture, DSDEVID_DefaultVoicePlayback, or
244 * DSDEVID_DefaultVoiceCapture.
245 * Returns pGuidSrc if pGuidSrc is a valid device or the device
246 * GUID for the specified constants.
248 HRESULT WINAPI
GetDeviceID(LPCGUID pGuidSrc
, LPGUID pGuidDest
)
250 TRACE("(%s,%p)\n", get_device_id(pGuidSrc
),pGuidDest
);
252 if ( pGuidSrc
== NULL
) {
253 WARN("invalid parameter: pGuidSrc == NULL\n");
254 return DSERR_INVALIDPARAM
;
257 if ( pGuidDest
== NULL
) {
258 WARN("invalid parameter: pGuidDest == NULL\n");
259 return DSERR_INVALIDPARAM
;
262 if ( IsEqualGUID( &DSDEVID_DefaultPlayback
, pGuidSrc
) ||
263 IsEqualGUID( &DSDEVID_DefaultVoicePlayback
, pGuidSrc
) ) {
264 CopyMemory(pGuidDest
, &DSOUND_renderer_guids
[ds_default_playback
], sizeof(GUID
));
265 TRACE("returns %s\n", get_device_id(pGuidDest
));
269 if ( IsEqualGUID( &DSDEVID_DefaultCapture
, pGuidSrc
) ||
270 IsEqualGUID( &DSDEVID_DefaultVoiceCapture
, pGuidSrc
) ) {
271 CopyMemory(pGuidDest
, &DSOUND_capture_guids
[ds_default_capture
], sizeof(GUID
));
272 TRACE("returns %s\n", get_device_id(pGuidDest
));
276 CopyMemory(pGuidDest
, pGuidSrc
, sizeof(GUID
));
277 TRACE("returns %s\n", get_device_id(pGuidDest
));
283 /***************************************************************************
284 * DirectSoundEnumerateA [DSOUND.2]
286 * Enumerate all DirectSound drivers installed in the system
289 * lpDSEnumCallback [I] Address of callback function.
290 * lpContext [I] Address of user defined context passed to callback function.
294 * Failure: DSERR_INVALIDPARAM
296 HRESULT WINAPI
DirectSoundEnumerateA(
297 LPDSENUMCALLBACKA lpDSEnumCallback
,
305 TRACE("lpDSEnumCallback = %p, lpContext = %p\n",
306 lpDSEnumCallback
, lpContext
);
308 if (lpDSEnumCallback
== NULL
) {
309 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
310 return DSERR_INVALIDPARAM
;
313 devs
= waveOutGetNumDevs();
315 if (GetDeviceID(&DSDEVID_DefaultPlayback
, &guid
) == DS_OK
) {
316 for (wod
= 0; wod
< devs
; ++wod
) {
317 if (IsEqualGUID( &guid
, &DSOUND_renderer_guids
[wod
]) ) {
318 err
= mmErr(waveOutMessage((HWAVEOUT
)wod
,DRV_QUERYDSOUNDDESC
,(DWORD_PTR
)&desc
,0));
320 TRACE("calling lpDSEnumCallback(NULL,\"%s\",\"%s\",%p)\n",
321 "Primary Sound Driver",desc
.szDrvname
,lpContext
);
322 if (lpDSEnumCallback(NULL
, "Primary Sound Driver", desc
.szDrvname
, lpContext
) == FALSE
)
330 for (wod
= 0; wod
< devs
; ++wod
) {
331 err
= mmErr(waveOutMessage((HWAVEOUT
)wod
,DRV_QUERYDSOUNDDESC
,(DWORD_PTR
)&desc
,0));
333 TRACE("calling lpDSEnumCallback(%s,\"%s\",\"%s\",%p)\n",
334 debugstr_guid(&DSOUND_renderer_guids
[wod
]),desc
.szDesc
,desc
.szDrvname
,lpContext
);
335 if (lpDSEnumCallback(&DSOUND_renderer_guids
[wod
], desc
.szDesc
, desc
.szDrvname
, lpContext
) == FALSE
)
342 /***************************************************************************
343 * DirectSoundEnumerateW [DSOUND.3]
345 * Enumerate all DirectSound drivers installed in the system
348 * lpDSEnumCallback [I] Address of callback function.
349 * lpContext [I] Address of user defined context passed to callback function.
353 * Failure: DSERR_INVALIDPARAM
355 HRESULT WINAPI
DirectSoundEnumerateW(
356 LPDSENUMCALLBACKW lpDSEnumCallback
,
363 WCHAR wDesc
[MAXPNAMELEN
];
364 WCHAR wName
[MAXPNAMELEN
];
366 TRACE("lpDSEnumCallback = %p, lpContext = %p\n",
367 lpDSEnumCallback
, lpContext
);
369 if (lpDSEnumCallback
== NULL
) {
370 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
371 return DSERR_INVALIDPARAM
;
374 devs
= waveOutGetNumDevs();
376 if (GetDeviceID(&DSDEVID_DefaultPlayback
, &guid
) == DS_OK
) {
377 for (wod
= 0; wod
< devs
; ++wod
) {
378 if (IsEqualGUID( &guid
, &DSOUND_renderer_guids
[wod
] ) ) {
379 err
= mmErr(waveOutMessage((HWAVEOUT
)wod
,DRV_QUERYDSOUNDDESC
,(DWORD_PTR
)&desc
,0));
381 TRACE("calling lpDSEnumCallback(NULL,\"%s\",\"%s\",%p)\n",
382 "Primary Sound Driver",desc
.szDrvname
,lpContext
);
383 MultiByteToWideChar( CP_ACP
, 0, "Primary Sound Driver", -1,
384 wDesc
, sizeof(wDesc
)/sizeof(WCHAR
) );
385 MultiByteToWideChar( CP_ACP
, 0, desc
.szDrvname
, -1,
386 wName
, sizeof(wName
)/sizeof(WCHAR
) );
387 if (lpDSEnumCallback(NULL
, wDesc
, wName
, lpContext
) == FALSE
)
395 for (wod
= 0; wod
< devs
; ++wod
) {
396 err
= mmErr(waveOutMessage((HWAVEOUT
)wod
,DRV_QUERYDSOUNDDESC
,(DWORD_PTR
)&desc
,0));
398 TRACE("calling lpDSEnumCallback(%s,\"%s\",\"%s\",%p)\n",
399 debugstr_guid(&DSOUND_renderer_guids
[wod
]),desc
.szDesc
,desc
.szDrvname
,lpContext
);
400 MultiByteToWideChar( CP_ACP
, 0, desc
.szDesc
, -1,
401 wDesc
, sizeof(wDesc
)/sizeof(WCHAR
) );
402 MultiByteToWideChar( CP_ACP
, 0, desc
.szDrvname
, -1,
403 wName
, sizeof(wName
)/sizeof(WCHAR
) );
404 if (lpDSEnumCallback(&DSOUND_renderer_guids
[wod
], wDesc
, wName
, lpContext
) == FALSE
)
411 /*******************************************************************************
412 * DirectSound ClassFactory
415 typedef HRESULT (*FnCreateInstance
)(REFIID riid
, LPVOID
*ppobj
);
418 const IClassFactoryVtbl
*lpVtbl
;
421 FnCreateInstance pfnCreateInstance
;
424 static HRESULT WINAPI
425 DSCF_QueryInterface(LPCLASSFACTORY iface
, REFIID riid
, LPVOID
*ppobj
)
427 IClassFactoryImpl
*This
= (IClassFactoryImpl
*)iface
;
428 TRACE("(%p, %s, %p)\n", This
, debugstr_guid(riid
), ppobj
);
431 if (IsEqualIID(riid
, &IID_IUnknown
) ||
432 IsEqualIID(riid
, &IID_IClassFactory
))
435 IUnknown_AddRef(iface
);
439 return E_NOINTERFACE
;
442 static ULONG WINAPI
DSCF_AddRef(LPCLASSFACTORY iface
)
444 IClassFactoryImpl
*This
= (IClassFactoryImpl
*)iface
;
445 ULONG ref
= InterlockedIncrement(&(This
->ref
));
446 TRACE("(%p) ref was %d\n", This
, ref
- 1);
450 static ULONG WINAPI
DSCF_Release(LPCLASSFACTORY iface
)
452 IClassFactoryImpl
*This
= (IClassFactoryImpl
*)iface
;
453 ULONG ref
= InterlockedDecrement(&(This
->ref
));
454 TRACE("(%p) ref was %d\n", This
, ref
+ 1);
455 /* static class, won't be freed */
459 static HRESULT WINAPI
DSCF_CreateInstance(
460 LPCLASSFACTORY iface
,
465 IClassFactoryImpl
*This
= (IClassFactoryImpl
*)iface
;
466 TRACE("(%p, %p, %s, %p)\n", This
, pOuter
, debugstr_guid(riid
), ppobj
);
469 return CLASS_E_NOAGGREGATION
;
472 WARN("invalid parameter\n");
473 return DSERR_INVALIDPARAM
;
476 return This
->pfnCreateInstance(riid
, ppobj
);
479 static HRESULT WINAPI
DSCF_LockServer(LPCLASSFACTORY iface
, BOOL dolock
)
481 IClassFactoryImpl
*This
= (IClassFactoryImpl
*)iface
;
482 FIXME("(%p, %d) stub!\n", This
, dolock
);
486 static const IClassFactoryVtbl DSCF_Vtbl
= {
494 static IClassFactoryImpl DSOUND_CF
[] = {
495 { &DSCF_Vtbl
, 1, &CLSID_DirectSound
, (FnCreateInstance
)DSOUND_Create
},
496 { &DSCF_Vtbl
, 1, &CLSID_DirectSound8
, (FnCreateInstance
)DSOUND_Create8
},
497 { &DSCF_Vtbl
, 1, &CLSID_DirectSoundCapture
, (FnCreateInstance
)DSOUND_CaptureCreate
},
498 { &DSCF_Vtbl
, 1, &CLSID_DirectSoundCapture8
, (FnCreateInstance
)DSOUND_CaptureCreate8
},
499 { &DSCF_Vtbl
, 1, &CLSID_DirectSoundFullDuplex
, (FnCreateInstance
)DSOUND_FullDuplexCreate
},
500 { &DSCF_Vtbl
, 1, &CLSID_DirectSoundPrivate
, (FnCreateInstance
)IKsPrivatePropertySetImpl_Create
},
501 { NULL
, 0, NULL
, NULL
}
504 /*******************************************************************************
505 * DllGetClassObject [DSOUND.@]
506 * Retrieves class object from a DLL object
509 * Docs say returns STDAPI
512 * rclsid [I] CLSID for the class object
513 * riid [I] Reference to identifier of interface for class object
514 * ppv [O] Address of variable to receive interface pointer for riid
518 * Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
521 HRESULT WINAPI
DllGetClassObject(REFCLSID rclsid
, REFIID riid
, LPVOID
*ppv
)
524 TRACE("(%s, %s, %p)\n", debugstr_guid(rclsid
), debugstr_guid(riid
), ppv
);
527 WARN("invalid parameter\n");
533 if (!IsEqualIID(riid
, &IID_IClassFactory
) &&
534 !IsEqualIID(riid
, &IID_IUnknown
)) {
535 WARN("no interface for %s\n", debugstr_guid(riid
));
536 return E_NOINTERFACE
;
539 while (NULL
!= DSOUND_CF
[i
].rclsid
) {
540 if (IsEqualGUID(rclsid
, DSOUND_CF
[i
].rclsid
)) {
541 DSCF_AddRef((IClassFactory
*) &DSOUND_CF
[i
]);
542 *ppv
= &DSOUND_CF
[i
];
548 WARN("(%s, %s, %p): no class found.\n", debugstr_guid(rclsid
),
549 debugstr_guid(riid
), ppv
);
550 return CLASS_E_CLASSNOTAVAILABLE
;
554 /*******************************************************************************
555 * DllCanUnloadNow [DSOUND.4]
556 * Determines whether the DLL is in use.
562 HRESULT WINAPI
DllCanUnloadNow(void)
564 FIXME("(void): stub\n");
568 #define INIT_GUID(guid, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \
569 guid.Data1 = l; guid.Data2 = w1; guid.Data3 = w2; \
570 guid.Data4[0] = b1; guid.Data4[1] = b2; guid.Data4[2] = b3; \
571 guid.Data4[3] = b4; guid.Data4[4] = b5; guid.Data4[5] = b6; \
572 guid.Data4[6] = b7; guid.Data4[7] = b8;
574 /***********************************************************************
575 * DllMain (DSOUND.init)
577 BOOL WINAPI
DllMain(HINSTANCE hInstDLL
, DWORD fdwReason
, LPVOID lpvReserved
)
580 TRACE("(%p %d %p)\n", hInstDLL
, fdwReason
, lpvReserved
);
583 case DLL_PROCESS_ATTACH
:
584 TRACE("DLL_PROCESS_ATTACH\n");
585 for (i
= 0; i
< MAXWAVEDRIVERS
; i
++) {
586 DSOUND_renderer
[i
] = NULL
;
587 DSOUND_capture
[i
] = NULL
;
588 INIT_GUID(DSOUND_renderer_guids
[i
], 0xbd6dd71a, 0x3deb, 0x11d1, 0xb1, 0x71, 0x00, 0xc0, 0x4f, 0xc2, 0x00, 0x00 + i
);
589 INIT_GUID(DSOUND_capture_guids
[i
], 0xbd6dd71b, 0x3deb, 0x11d1, 0xb1, 0x71, 0x00, 0xc0, 0x4f, 0xc2, 0x00, 0x00 + i
);
591 DisableThreadLibraryCalls(hInstDLL
);
592 /* Increase refcount on dsound by 1 */
593 GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
, (LPCWSTR
)hInstDLL
, &hInstDLL
);
595 case DLL_PROCESS_DETACH
:
596 TRACE("DLL_PROCESS_DETACH\n");
599 TRACE("UNKNOWN REASON\n");