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"
53 #include "dsound_private.h"
55 WINE_DEFAULT_DEBUG_CHANNEL(dsound
);
57 #define DS_HEL_BUFLEN 0x8000 /* HEL: The buffer length of the emulated buffer */
58 #define DS_SND_QUEUE_MAX 10 /* max number of fragments to prebuffer, each fragment is approximately 10 ms long */
59 #define DS_SND_QUEUE_MIN 6 /* If the minimum of prebuffered fragments go below this, forcibly take all locks to prevent underruns */
61 DirectSoundDevice
* DSOUND_renderer
[MAXWAVEDRIVERS
];
62 GUID DSOUND_renderer_guids
[MAXWAVEDRIVERS
];
63 GUID DSOUND_capture_guids
[MAXWAVEDRIVERS
];
65 HRESULT
mmErr(UINT err
)
68 case MMSYSERR_NOERROR
:
70 case MMSYSERR_ALLOCATED
:
71 return DSERR_ALLOCATED
;
73 case MMSYSERR_INVALHANDLE
:
74 case WAVERR_STILLPLAYING
:
75 return DSERR_GENERIC
; /* FIXME */
76 case MMSYSERR_NODRIVER
:
77 return DSERR_NODRIVER
;
79 return DSERR_OUTOFMEMORY
;
80 case MMSYSERR_INVALPARAM
:
81 case WAVERR_BADFORMAT
:
82 case WAVERR_UNPREPARED
:
83 return DSERR_INVALIDPARAM
;
84 case MMSYSERR_NOTSUPPORTED
:
85 return DSERR_UNSUPPORTED
;
87 FIXME("Unknown MMSYS error %d\n",err
);
92 int ds_emuldriver
= 0;
93 int ds_hel_buflen
= DS_HEL_BUFLEN
;
94 int ds_snd_queue_max
= DS_SND_QUEUE_MAX
;
95 int ds_snd_queue_min
= DS_SND_QUEUE_MIN
;
96 int ds_hw_accel
= DS_HW_ACCEL_FULL
;
97 int ds_default_playback
= 0;
98 int ds_default_capture
= 0;
99 int ds_default_sample_rate
= 44100;
100 int ds_default_bits_per_sample
= 16;
103 * Get a config key from either the app-specific or the default config
106 static inline DWORD
get_config_key( HKEY defkey
, HKEY appkey
, const char *name
,
107 char *buffer
, DWORD size
)
109 if (appkey
&& !RegQueryValueExA( appkey
, name
, 0, NULL
, (LPBYTE
)buffer
, &size
)) return 0;
110 if (defkey
&& !RegQueryValueExA( defkey
, name
, 0, NULL
, (LPBYTE
)buffer
, &size
)) return 0;
111 return ERROR_FILE_NOT_FOUND
;
116 * Setup the dsound options.
119 void setup_dsound_options(void)
121 char buffer
[MAX_PATH
+16];
122 HKEY hkey
, appkey
= 0;
125 buffer
[MAX_PATH
]='\0';
127 /* @@ Wine registry key: HKCU\Software\Wine\DirectSound */
128 if (RegOpenKeyA( HKEY_CURRENT_USER
, "Software\\Wine\\DirectSound", &hkey
)) hkey
= 0;
130 len
= GetModuleFileNameA( 0, buffer
, MAX_PATH
);
131 if (len
&& len
< MAX_PATH
)
134 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\DirectSound */
135 if (!RegOpenKeyA( HKEY_CURRENT_USER
, "Software\\Wine\\AppDefaults", &tmpkey
))
137 char *p
, *appname
= buffer
;
138 if ((p
= strrchr( appname
, '/' ))) appname
= p
+ 1;
139 if ((p
= strrchr( appname
, '\\' ))) appname
= p
+ 1;
140 strcat( appname
, "\\DirectSound" );
141 TRACE("appname = [%s]\n", appname
);
142 if (RegOpenKeyA( tmpkey
, appname
, &appkey
)) appkey
= 0;
143 RegCloseKey( tmpkey
);
149 if (!get_config_key( hkey
, appkey
, "EmulDriver", buffer
, MAX_PATH
))
150 ds_emuldriver
= strcmp(buffer
, "N");
152 if (!get_config_key( hkey
, appkey
, "HelBuflen", buffer
, MAX_PATH
))
153 ds_hel_buflen
= atoi(buffer
);
155 if (!get_config_key( hkey
, appkey
, "SndQueueMax", buffer
, MAX_PATH
))
156 ds_snd_queue_max
= atoi(buffer
);
158 if (!get_config_key( hkey
, appkey
, "SndQueueMin", buffer
, MAX_PATH
))
159 ds_snd_queue_min
= atoi(buffer
);
161 if (!get_config_key( hkey
, appkey
, "HardwareAcceleration", buffer
, MAX_PATH
)) {
162 if (strcmp(buffer
, "Full") == 0)
163 ds_hw_accel
= DS_HW_ACCEL_FULL
;
164 else if (strcmp(buffer
, "Standard") == 0)
165 ds_hw_accel
= DS_HW_ACCEL_STANDARD
;
166 else if (strcmp(buffer
, "Basic") == 0)
167 ds_hw_accel
= DS_HW_ACCEL_BASIC
;
168 else if (strcmp(buffer
, "Emulation") == 0)
169 ds_hw_accel
= DS_HW_ACCEL_EMULATION
;
172 if (!get_config_key( hkey
, appkey
, "DefaultPlayback", buffer
, MAX_PATH
))
173 ds_default_playback
= atoi(buffer
);
175 if (!get_config_key( hkey
, appkey
, "DefaultCapture", buffer
, MAX_PATH
))
176 ds_default_capture
= atoi(buffer
);
178 if (!get_config_key( hkey
, appkey
, "DefaultSampleRate", buffer
, MAX_PATH
))
179 ds_default_sample_rate
= atoi(buffer
);
181 if (!get_config_key( hkey
, appkey
, "DefaultBitsPerSample", buffer
, MAX_PATH
))
182 ds_default_bits_per_sample
= atoi(buffer
);
184 if (appkey
) RegCloseKey( appkey
);
185 if (hkey
) RegCloseKey( hkey
);
188 WARN("ds_emuldriver = %d (default=0)\n",ds_emuldriver
);
189 if (ds_hel_buflen
!= DS_HEL_BUFLEN
)
190 WARN("ds_hel_buflen = %d (default=%d)\n",ds_hel_buflen
,DS_HEL_BUFLEN
);
191 if (ds_snd_queue_max
!= DS_SND_QUEUE_MAX
)
192 WARN("ds_snd_queue_max = %d (default=%d)\n",ds_snd_queue_max
,DS_SND_QUEUE_MAX
);
193 if (ds_snd_queue_min
!= DS_SND_QUEUE_MIN
)
194 WARN("ds_snd_queue_min = %d (default=%d)\n",ds_snd_queue_min
,DS_SND_QUEUE_MIN
);
195 if (ds_hw_accel
!= DS_HW_ACCEL_FULL
)
196 WARN("ds_hw_accel = %s (default=Full)\n",
197 ds_hw_accel
==DS_HW_ACCEL_FULL
? "Full" :
198 ds_hw_accel
==DS_HW_ACCEL_STANDARD
? "Standard" :
199 ds_hw_accel
==DS_HW_ACCEL_BASIC
? "Basic" :
200 ds_hw_accel
==DS_HW_ACCEL_EMULATION
? "Emulation" :
202 if (ds_default_playback
!= 0)
203 WARN("ds_default_playback = %d (default=0)\n",ds_default_playback
);
204 if (ds_default_capture
!= 0)
205 WARN("ds_default_capture = %d (default=0)\n",ds_default_playback
);
206 if (ds_default_sample_rate
!= 44100)
207 WARN("ds_default_sample_rate = %d (default=44100)\n",ds_default_sample_rate
);
208 if (ds_default_bits_per_sample
!= 16)
209 WARN("ds_default_bits_per_sample = %d (default=16)\n",ds_default_bits_per_sample
);
212 static const char * get_device_id(LPCGUID pGuid
)
214 if (IsEqualGUID(&DSDEVID_DefaultPlayback
, pGuid
))
215 return "DSDEVID_DefaultPlayback";
216 else if (IsEqualGUID(&DSDEVID_DefaultVoicePlayback
, pGuid
))
217 return "DSDEVID_DefaultVoicePlayback";
218 else if (IsEqualGUID(&DSDEVID_DefaultCapture
, pGuid
))
219 return "DSDEVID_DefaultCapture";
220 else if (IsEqualGUID(&DSDEVID_DefaultVoiceCapture
, pGuid
))
221 return "DSDEVID_DefaultVoiceCapture";
222 return debugstr_guid(pGuid
);
225 /***************************************************************************
226 * GetDeviceID [DSOUND.9]
228 * Retrieves unique identifier of default device specified
231 * pGuidSrc [I] Address of device GUID.
232 * pGuidDest [O] Address to receive unique device GUID.
236 * Failure: DSERR_INVALIDPARAM
239 * pGuidSrc is a valid device GUID or DSDEVID_DefaultPlayback,
240 * DSDEVID_DefaultCapture, DSDEVID_DefaultVoicePlayback, or
241 * DSDEVID_DefaultVoiceCapture.
242 * Returns pGuidSrc if pGuidSrc is a valid device or the device
243 * GUID for the specified constants.
245 HRESULT WINAPI
GetDeviceID(LPCGUID pGuidSrc
, LPGUID pGuidDest
)
247 TRACE("(%s,%p)\n", get_device_id(pGuidSrc
),pGuidDest
);
249 if ( pGuidSrc
== NULL
) {
250 WARN("invalid parameter: pGuidSrc == NULL\n");
251 return DSERR_INVALIDPARAM
;
254 if ( pGuidDest
== NULL
) {
255 WARN("invalid parameter: pGuidDest == NULL\n");
256 return DSERR_INVALIDPARAM
;
259 if ( IsEqualGUID( &DSDEVID_DefaultPlayback
, pGuidSrc
) ||
260 IsEqualGUID( &DSDEVID_DefaultVoicePlayback
, pGuidSrc
) ) {
261 CopyMemory(pGuidDest
, &DSOUND_renderer_guids
[ds_default_playback
], sizeof(GUID
));
262 TRACE("returns %s\n", get_device_id(pGuidDest
));
266 if ( IsEqualGUID( &DSDEVID_DefaultCapture
, pGuidSrc
) ||
267 IsEqualGUID( &DSDEVID_DefaultVoiceCapture
, pGuidSrc
) ) {
268 CopyMemory(pGuidDest
, &DSOUND_capture_guids
[ds_default_capture
], sizeof(GUID
));
269 TRACE("returns %s\n", get_device_id(pGuidDest
));
273 CopyMemory(pGuidDest
, pGuidSrc
, sizeof(GUID
));
274 TRACE("returns %s\n", get_device_id(pGuidDest
));
280 /***************************************************************************
281 * DirectSoundEnumerateA [DSOUND.2]
283 * Enumerate all DirectSound drivers installed in the system
286 * lpDSEnumCallback [I] Address of callback function.
287 * lpContext [I] Address of user defined context passed to callback function.
291 * Failure: DSERR_INVALIDPARAM
293 HRESULT WINAPI
DirectSoundEnumerateA(
294 LPDSENUMCALLBACKA lpDSEnumCallback
,
302 TRACE("lpDSEnumCallback = %p, lpContext = %p\n",
303 lpDSEnumCallback
, lpContext
);
305 if (lpDSEnumCallback
== NULL
) {
306 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
307 return DSERR_INVALIDPARAM
;
310 devs
= waveOutGetNumDevs();
312 if (GetDeviceID(&DSDEVID_DefaultPlayback
, &guid
) == DS_OK
) {
313 for (wod
= 0; wod
< devs
; ++wod
) {
314 if (IsEqualGUID( &guid
, &DSOUND_renderer_guids
[wod
]) ) {
315 err
= mmErr(waveOutMessage((HWAVEOUT
)wod
,DRV_QUERYDSOUNDDESC
,(DWORD_PTR
)&desc
,0));
317 TRACE("calling lpDSEnumCallback(NULL,\"%s\",\"%s\",%p)\n",
318 "Primary Sound Driver",desc
.szDrvname
,lpContext
);
319 if (lpDSEnumCallback(NULL
, "Primary Sound Driver", desc
.szDrvname
, lpContext
) == FALSE
)
327 for (wod
= 0; wod
< devs
; ++wod
) {
328 err
= mmErr(waveOutMessage((HWAVEOUT
)wod
,DRV_QUERYDSOUNDDESC
,(DWORD_PTR
)&desc
,0));
330 TRACE("calling lpDSEnumCallback(%s,\"%s\",\"%s\",%p)\n",
331 debugstr_guid(&DSOUND_renderer_guids
[wod
]),desc
.szDesc
,desc
.szDrvname
,lpContext
);
332 if (lpDSEnumCallback(&DSOUND_renderer_guids
[wod
], desc
.szDesc
, desc
.szDrvname
, lpContext
) == FALSE
)
339 /***************************************************************************
340 * DirectSoundEnumerateW [DSOUND.3]
342 * Enumerate all DirectSound drivers installed in the system
345 * lpDSEnumCallback [I] Address of callback function.
346 * lpContext [I] Address of user defined context passed to callback function.
350 * Failure: DSERR_INVALIDPARAM
352 HRESULT WINAPI
DirectSoundEnumerateW(
353 LPDSENUMCALLBACKW lpDSEnumCallback
,
360 WCHAR wDesc
[MAXPNAMELEN
];
361 WCHAR wName
[MAXPNAMELEN
];
363 TRACE("lpDSEnumCallback = %p, lpContext = %p\n",
364 lpDSEnumCallback
, lpContext
);
366 if (lpDSEnumCallback
== NULL
) {
367 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
368 return DSERR_INVALIDPARAM
;
371 devs
= waveOutGetNumDevs();
373 if (GetDeviceID(&DSDEVID_DefaultPlayback
, &guid
) == DS_OK
) {
374 for (wod
= 0; wod
< devs
; ++wod
) {
375 if (IsEqualGUID( &guid
, &DSOUND_renderer_guids
[wod
] ) ) {
376 err
= mmErr(waveOutMessage((HWAVEOUT
)wod
,DRV_QUERYDSOUNDDESC
,(DWORD_PTR
)&desc
,0));
378 TRACE("calling lpDSEnumCallback(NULL,\"%s\",\"%s\",%p)\n",
379 "Primary Sound Driver",desc
.szDrvname
,lpContext
);
380 MultiByteToWideChar( CP_ACP
, 0, "Primary Sound Driver", -1,
381 wDesc
, sizeof(wDesc
)/sizeof(WCHAR
) );
382 MultiByteToWideChar( CP_ACP
, 0, desc
.szDrvname
, -1,
383 wName
, sizeof(wName
)/sizeof(WCHAR
) );
384 if (lpDSEnumCallback(NULL
, wDesc
, wName
, lpContext
) == FALSE
)
392 for (wod
= 0; wod
< devs
; ++wod
) {
393 err
= mmErr(waveOutMessage((HWAVEOUT
)wod
,DRV_QUERYDSOUNDDESC
,(DWORD_PTR
)&desc
,0));
395 TRACE("calling lpDSEnumCallback(%s,\"%s\",\"%s\",%p)\n",
396 debugstr_guid(&DSOUND_renderer_guids
[wod
]),desc
.szDesc
,desc
.szDrvname
,lpContext
);
397 MultiByteToWideChar( CP_ACP
, 0, desc
.szDesc
, -1,
398 wDesc
, sizeof(wDesc
)/sizeof(WCHAR
) );
399 MultiByteToWideChar( CP_ACP
, 0, desc
.szDrvname
, -1,
400 wName
, sizeof(wName
)/sizeof(WCHAR
) );
401 if (lpDSEnumCallback(&DSOUND_renderer_guids
[wod
], wDesc
, wName
, lpContext
) == FALSE
)
408 /*******************************************************************************
409 * DirectSound ClassFactory
412 typedef HRESULT (*FnCreateInstance
)(REFIID riid
, LPVOID
*ppobj
);
415 const IClassFactoryVtbl
*lpVtbl
;
418 FnCreateInstance pfnCreateInstance
;
421 static HRESULT WINAPI
422 DSCF_QueryInterface(LPCLASSFACTORY iface
, REFIID riid
, LPVOID
*ppobj
)
424 IClassFactoryImpl
*This
= (IClassFactoryImpl
*)iface
;
425 TRACE("(%p, %s, %p)\n", This
, debugstr_guid(riid
), ppobj
);
428 if (IsEqualIID(riid
, &IID_IUnknown
) ||
429 IsEqualIID(riid
, &IID_IClassFactory
))
432 IUnknown_AddRef(iface
);
436 return E_NOINTERFACE
;
439 static ULONG WINAPI
DSCF_AddRef(LPCLASSFACTORY iface
)
441 IClassFactoryImpl
*This
= (IClassFactoryImpl
*)iface
;
442 ULONG ref
= InterlockedIncrement(&(This
->ref
));
443 TRACE("(%p) ref was %d\n", This
, ref
- 1);
447 static ULONG WINAPI
DSCF_Release(LPCLASSFACTORY iface
)
449 IClassFactoryImpl
*This
= (IClassFactoryImpl
*)iface
;
450 ULONG ref
= InterlockedDecrement(&(This
->ref
));
451 TRACE("(%p) ref was %d\n", This
, ref
+ 1);
452 /* static class, won't be freed */
456 static HRESULT WINAPI
DSCF_CreateInstance(
457 LPCLASSFACTORY iface
,
462 IClassFactoryImpl
*This
= (IClassFactoryImpl
*)iface
;
463 TRACE("(%p, %p, %s, %p)\n", This
, pOuter
, debugstr_guid(riid
), ppobj
);
466 return CLASS_E_NOAGGREGATION
;
469 WARN("invalid parameter\n");
470 return DSERR_INVALIDPARAM
;
473 return This
->pfnCreateInstance(riid
, ppobj
);
476 static HRESULT WINAPI
DSCF_LockServer(LPCLASSFACTORY iface
, BOOL dolock
)
478 IClassFactoryImpl
*This
= (IClassFactoryImpl
*)iface
;
479 FIXME("(%p, %d) stub!\n", This
, dolock
);
483 static const IClassFactoryVtbl DSCF_Vtbl
= {
491 static IClassFactoryImpl DSOUND_CF
[] = {
492 { &DSCF_Vtbl
, 1, &CLSID_DirectSound
, (FnCreateInstance
)DSOUND_Create
},
493 { &DSCF_Vtbl
, 1, &CLSID_DirectSound8
, (FnCreateInstance
)DSOUND_Create8
},
494 { &DSCF_Vtbl
, 1, &CLSID_DirectSoundCapture
, (FnCreateInstance
)DSOUND_CaptureCreate
},
495 { &DSCF_Vtbl
, 1, &CLSID_DirectSoundCapture8
, (FnCreateInstance
)DSOUND_CaptureCreate8
},
496 { &DSCF_Vtbl
, 1, &CLSID_DirectSoundFullDuplex
, (FnCreateInstance
)DSOUND_FullDuplexCreate
},
497 { &DSCF_Vtbl
, 1, &CLSID_DirectSoundPrivate
, (FnCreateInstance
)IKsPrivatePropertySetImpl_Create
},
498 { NULL
, 0, NULL
, NULL
}
501 /*******************************************************************************
502 * DllGetClassObject [DSOUND.@]
503 * Retrieves class object from a DLL object
506 * Docs say returns STDAPI
509 * rclsid [I] CLSID for the class object
510 * riid [I] Reference to identifier of interface for class object
511 * ppv [O] Address of variable to receive interface pointer for riid
515 * Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
518 HRESULT WINAPI
DllGetClassObject(REFCLSID rclsid
, REFIID riid
, LPVOID
*ppv
)
521 TRACE("(%s, %s, %p)\n", debugstr_guid(rclsid
), debugstr_guid(riid
), ppv
);
524 WARN("invalid parameter\n");
530 if (!IsEqualIID(riid
, &IID_IClassFactory
) &&
531 !IsEqualIID(riid
, &IID_IUnknown
)) {
532 WARN("no interface for %s\n", debugstr_guid(riid
));
533 return E_NOINTERFACE
;
536 while (NULL
!= DSOUND_CF
[i
].rclsid
) {
537 if (IsEqualGUID(rclsid
, DSOUND_CF
[i
].rclsid
)) {
538 DSCF_AddRef((IClassFactory
*) &DSOUND_CF
[i
]);
539 *ppv
= &DSOUND_CF
[i
];
545 WARN("(%s, %s, %p): no class found.\n", debugstr_guid(rclsid
),
546 debugstr_guid(riid
), ppv
);
547 return CLASS_E_CLASSNOTAVAILABLE
;
551 /*******************************************************************************
552 * DllCanUnloadNow [DSOUND.4]
553 * Determines whether the DLL is in use.
559 HRESULT WINAPI
DllCanUnloadNow(void)
561 FIXME("(void): stub\n");
565 #define INIT_GUID(guid, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \
566 guid.Data1 = l; guid.Data2 = w1; guid.Data3 = w2; \
567 guid.Data4[0] = b1; guid.Data4[1] = b2; guid.Data4[2] = b3; \
568 guid.Data4[3] = b4; guid.Data4[4] = b5; guid.Data4[5] = b6; \
569 guid.Data4[6] = b7; guid.Data4[7] = b8;
571 /***********************************************************************
572 * DllMain (DSOUND.init)
574 BOOL WINAPI
DllMain(HINSTANCE hInstDLL
, DWORD fdwReason
, LPVOID lpvReserved
)
577 TRACE("(%p %d %p)\n", hInstDLL
, fdwReason
, lpvReserved
);
580 case DLL_PROCESS_ATTACH
:
581 TRACE("DLL_PROCESS_ATTACH\n");
582 for (i
= 0; i
< MAXWAVEDRIVERS
; i
++) {
583 DSOUND_renderer
[i
] = NULL
;
584 DSOUND_capture
[i
] = NULL
;
585 INIT_GUID(DSOUND_renderer_guids
[i
], 0xbd6dd71a, 0x3deb, 0x11d1, 0xb1, 0x71, 0x00, 0xc0, 0x4f, 0xc2, 0x00, 0x00 + i
);
586 INIT_GUID(DSOUND_capture_guids
[i
], 0xbd6dd71b, 0x3deb, 0x11d1, 0xb1, 0x71, 0x00, 0xc0, 0x4f, 0xc2, 0x00, 0x00 + i
);
588 DisableThreadLibraryCalls(hInstDLL
);
589 /* Increase refcount on dsound by 1 */
590 GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
, (LPCWSTR
)hInstDLL
, &hInstDLL
);
592 case DLL_PROCESS_DETACH
:
593 TRACE("DLL_PROCESS_DETACH\n");
596 TRACE("UNKNOWN REASON\n");