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
22 * Most thread locking is complete. There may be a few race
23 * conditions still lurking.
25 * Tested with a Soundblaster clone, a Gravis UltraSound Classic,
26 * and a Turtle Beach Tropez+.
29 * Implement SetCooperativeLevel properly (need to address focus issues)
30 * Implement DirectSound3DBuffers (stubs in place)
31 * Use hardware 3D support if available
32 * Add critical section locking inside Release and AddRef methods
33 * Handle static buffers - put those in hardware, non-static not in hardware
34 * Hardware DuplicateSoundBuffer
35 * Proper volume calculation, and setting volume in HEL primary buffer
36 * Optimize WINMM and negotiate fragment size, decrease DS_HEL_MARGIN
42 #define NONAMELESSSTRUCT
43 #define NONAMELESSUNION
52 #include "wine/debug.h"
55 #include "dsound_private.h"
58 WINE_DEFAULT_DEBUG_CHANNEL(dsound);
60 /* these are eligible for tuning... they must be high on slow machines... */
61 /* some stuff may get more responsive with lower values though... */
62 #define DS_EMULDRIVER 0 /* some games (Quake 2, UT) refuse to accept
63 emulated dsound devices. set to 0 ! */
64 #define DS_HEL_MARGIN 5 /* HEL only: number of waveOut fragments ahead to mix in new buffers
65 * (keep this close or equal to DS_HEL_QUEUE for best results) */
66 #define DS_HEL_QUEUE 5 /* HEL only: number of waveOut fragments ahead to queue to driver
67 * (this will affect HEL sound reliability and latency) */
69 #define DS_SND_QUEUE_MAX 28 /* max number of fragments to prebuffer */
70 #define DS_SND_QUEUE_MIN 12 /* min number of fragments to prebuffer */
72 DirectSoundDevice* DSOUND_renderer[MAXWAVEDRIVERS];
73 GUID DSOUND_renderer_guids[MAXWAVEDRIVERS];
74 GUID DSOUND_capture_guids[MAXWAVEDRIVERS];
76 HRESULT mmErr(UINT err)
79 case MMSYSERR_NOERROR:
81 case MMSYSERR_ALLOCATED:
82 return DSERR_ALLOCATED;
84 case MMSYSERR_INVALHANDLE:
85 case WAVERR_STILLPLAYING:
86 return DSERR_GENERIC; /* FIXME */
87 case MMSYSERR_NODRIVER:
88 return DSERR_NODRIVER;
90 return DSERR_OUTOFMEMORY;
91 case MMSYSERR_INVALPARAM:
92 case WAVERR_BADFORMAT:
93 case WAVERR_UNPREPARED:
94 return DSERR_INVALIDPARAM;
95 case MMSYSERR_NOTSUPPORTED:
96 return DSERR_UNSUPPORTED;
98 FIXME("Unknown MMSYS error %d\n",err);
103 int ds_emuldriver = DS_EMULDRIVER;
104 int ds_hel_margin = DS_HEL_MARGIN;
105 int ds_hel_queue = DS_HEL_QUEUE;
106 int ds_snd_queue_max = DS_SND_QUEUE_MAX;
107 int ds_snd_queue_min = DS_SND_QUEUE_MIN;
108 int ds_hw_accel = DS_HW_ACCEL_FULL;
109 int ds_default_playback = 0;
110 int ds_default_capture = 0;
111 int ds_default_sample_rate = 22050;
112 int ds_default_bits_per_sample = 8;
115 * Get a config key from either the app-specific or the default config
118 inline static DWORD get_config_key( HKEY defkey, HKEY appkey, const char *name,
119 char *buffer, DWORD size )
121 if (appkey && !RegQueryValueExA( appkey, name, 0, NULL, (LPBYTE)buffer, &size )) return 0;
122 if (defkey && !RegQueryValueExA( defkey, name, 0, NULL, (LPBYTE)buffer, &size )) return 0;
123 return ERROR_FILE_NOT_FOUND;
128 * Setup the dsound options.
131 void setup_dsound_options(void)
133 char buffer[MAX_PATH+16];
134 HKEY hkey, appkey = 0;
137 buffer[MAX_PATH]='\0';
139 /* @@ Wine registry key: HKCU\Software\Wine\DirectSound */
140 if (RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\DirectSound", &hkey )) hkey = 0;
142 len = GetModuleFileNameA( 0, buffer, MAX_PATH );
143 if (len && len < MAX_PATH)
146 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\DirectSound */
147 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey ))
149 char *p, *appname = buffer;
150 if ((p = strrchr( appname, '/' ))) appname = p + 1;
151 if ((p = strrchr( appname, '\\' ))) appname = p + 1;
152 strcat( appname, "\\DirectSound" );
153 TRACE("appname = [%s]\n", appname);
154 if (RegOpenKeyA( tmpkey, appname, &appkey )) appkey = 0;
155 RegCloseKey( tmpkey );
161 if (!get_config_key( hkey, appkey, "EmulDriver", buffer, MAX_PATH ))
162 ds_emuldriver = strcmp(buffer, "N");
164 if (!get_config_key( hkey, appkey, "HELmargin", buffer, MAX_PATH ))
165 ds_hel_margin = atoi(buffer);
167 if (!get_config_key( hkey, appkey, "HELqueue", buffer, MAX_PATH ))
168 ds_hel_queue = atoi(buffer);
170 if (!get_config_key( hkey, appkey, "SndQueueMax", buffer, MAX_PATH ))
171 ds_snd_queue_max = atoi(buffer);
173 if (!get_config_key( hkey, appkey, "SndQueueMin", buffer, MAX_PATH ))
174 ds_snd_queue_min = atoi(buffer);
176 if (!get_config_key( hkey, appkey, "HardwareAcceleration", buffer, MAX_PATH )) {
177 if (strcmp(buffer, "Full") == 0)
178 ds_hw_accel = DS_HW_ACCEL_FULL;
179 else if (strcmp(buffer, "Standard") == 0)
180 ds_hw_accel = DS_HW_ACCEL_STANDARD;
181 else if (strcmp(buffer, "Basic") == 0)
182 ds_hw_accel = DS_HW_ACCEL_BASIC;
183 else if (strcmp(buffer, "Emulation") == 0)
184 ds_hw_accel = DS_HW_ACCEL_EMULATION;
187 if (!get_config_key( hkey, appkey, "DefaultPlayback", buffer, MAX_PATH ))
188 ds_default_playback = atoi(buffer);
190 if (!get_config_key( hkey, appkey, "DefaultCapture", buffer, MAX_PATH ))
191 ds_default_capture = atoi(buffer);
193 if (!get_config_key( hkey, appkey, "DefaultSampleRate", buffer, MAX_PATH ))
194 ds_default_sample_rate = atoi(buffer);
196 if (!get_config_key( hkey, appkey, "DefaultBitsPerSample", buffer, MAX_PATH ))
197 ds_default_bits_per_sample = atoi(buffer);
199 if (appkey) RegCloseKey( appkey );
200 if (hkey) RegCloseKey( hkey );
202 if (ds_emuldriver != DS_EMULDRIVER )
203 WARN("ds_emuldriver = %d (default=%d)\n",ds_emuldriver, DS_EMULDRIVER);
204 if (ds_hel_margin != DS_HEL_MARGIN )
205 WARN("ds_hel_margin = %d (default=%d)\n",ds_hel_margin, DS_HEL_MARGIN );
206 if (ds_hel_queue != DS_HEL_QUEUE )
207 WARN("ds_hel_queue = %d (default=%d)\n",ds_hel_queue, DS_HEL_QUEUE );
208 if (ds_snd_queue_max != DS_SND_QUEUE_MAX)
209 WARN("ds_snd_queue_max = %d (default=%d)\n",ds_snd_queue_max ,DS_SND_QUEUE_MAX);
210 if (ds_snd_queue_min != DS_SND_QUEUE_MIN)
211 WARN("ds_snd_queue_min = %d (default=%d)\n",ds_snd_queue_min ,DS_SND_QUEUE_MIN);
212 if (ds_hw_accel != DS_HW_ACCEL_FULL)
213 WARN("ds_hw_accel = %s (default=Full)\n",
214 ds_hw_accel==DS_HW_ACCEL_FULL ? "Full" :
215 ds_hw_accel==DS_HW_ACCEL_STANDARD ? "Standard" :
216 ds_hw_accel==DS_HW_ACCEL_BASIC ? "Basic" :
217 ds_hw_accel==DS_HW_ACCEL_EMULATION ? "Emulation" :
219 if (ds_default_playback != 0)
220 WARN("ds_default_playback = %d (default=0)\n",ds_default_playback);
221 if (ds_default_capture != 0)
222 WARN("ds_default_capture = %d (default=0)\n",ds_default_playback);
223 if (ds_default_sample_rate != 22050)
224 WARN("ds_default_sample_rate = %d (default=22050)\n",ds_default_sample_rate);
225 if (ds_default_bits_per_sample != 8)
226 WARN("ds_default_bits_per_sample = %d (default=8)\n",ds_default_bits_per_sample);
229 static const char * get_device_id(LPCGUID pGuid)
231 if (IsEqualGUID(&DSDEVID_DefaultPlayback, pGuid))
232 return "DSDEVID_DefaultPlayback";
233 else if (IsEqualGUID(&DSDEVID_DefaultVoicePlayback, pGuid))
234 return "DSDEVID_DefaultVoicePlayback";
235 else if (IsEqualGUID(&DSDEVID_DefaultCapture, pGuid))
236 return "DSDEVID_DefaultCapture";
237 else if (IsEqualGUID(&DSDEVID_DefaultVoiceCapture, pGuid))
238 return "DSDEVID_DefaultVoiceCapture";
239 return debugstr_guid(pGuid);
242 /***************************************************************************
243 * GetDeviceID [DSOUND.9]
245 * Retrieves unique identifier of default device specified
248 * pGuidSrc [I] Address of device GUID.
249 * pGuidDest [O] Address to receive unique device GUID.
253 * Failure: DSERR_INVALIDPARAM
256 * pGuidSrc is a valid device GUID or DSDEVID_DefaultPlayback,
257 * DSDEVID_DefaultCapture, DSDEVID_DefaultVoicePlayback, or
258 * DSDEVID_DefaultVoiceCapture.
259 * Returns pGuidSrc if pGuidSrc is a valid device or the device
260 * GUID for the specified constants.
262 HRESULT WINAPI GetDeviceID(LPCGUID pGuidSrc, LPGUID pGuidDest)
264 TRACE("(%s,%p)\n", get_device_id(pGuidSrc),pGuidDest);
266 if ( pGuidSrc == NULL) {
267 WARN("invalid parameter: pGuidSrc == NULL\n");
268 return DSERR_INVALIDPARAM;
271 if ( pGuidDest == NULL ) {
272 WARN("invalid parameter: pGuidDest == NULL\n");
273 return DSERR_INVALIDPARAM;
276 if ( IsEqualGUID( &DSDEVID_DefaultPlayback, pGuidSrc ) ||
277 IsEqualGUID( &DSDEVID_DefaultVoicePlayback, pGuidSrc ) ) {
278 CopyMemory(pGuidDest, &DSOUND_renderer_guids[ds_default_playback], sizeof(GUID));
279 TRACE("returns %s\n", get_device_id(pGuidDest));
283 if ( IsEqualGUID( &DSDEVID_DefaultCapture, pGuidSrc ) ||
284 IsEqualGUID( &DSDEVID_DefaultVoiceCapture, pGuidSrc ) ) {
285 CopyMemory(pGuidDest, &DSOUND_capture_guids[ds_default_capture], sizeof(GUID));
286 TRACE("returns %s\n", get_device_id(pGuidDest));
290 CopyMemory(pGuidDest, pGuidSrc, sizeof(GUID));
291 TRACE("returns %s\n", get_device_id(pGuidDest));
297 /***************************************************************************
298 * DirectSoundEnumerateA [DSOUND.2]
300 * Enumerate all DirectSound drivers installed in the system
303 * lpDSEnumCallback [I] Address of callback function.
304 * lpContext [I] Address of user defined context passed to callback function.
308 * Failure: DSERR_INVALIDPARAM
310 HRESULT WINAPI DirectSoundEnumerateA(
311 LPDSENUMCALLBACKA lpDSEnumCallback,
319 TRACE("lpDSEnumCallback = %p, lpContext = %p\n",
320 lpDSEnumCallback, lpContext);
322 if (lpDSEnumCallback == NULL) {
323 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
324 return DSERR_INVALIDPARAM;
327 devs = waveOutGetNumDevs();
329 if (GetDeviceID(&DSDEVID_DefaultPlayback, &guid) == DS_OK) {
330 for (wod = 0; wod < devs; ++wod) {
331 if (IsEqualGUID( &guid, &DSOUND_renderer_guids[wod]) ) {
332 err = mmErr(waveOutMessage((HWAVEOUT)wod,DRV_QUERYDSOUNDDESC,(DWORD_PTR)&desc,0));
334 TRACE("calling lpDSEnumCallback(NULL,\"%s\",\"%s\",%p)\n",
335 "Primary Sound Driver",desc.szDrvname,lpContext);
336 if (lpDSEnumCallback(NULL, "Primary Sound Driver", desc.szDrvname, lpContext) == FALSE)
344 for (wod = 0; wod < devs; ++wod) {
345 err = mmErr(waveOutMessage((HWAVEOUT)wod,DRV_QUERYDSOUNDDESC,(DWORD_PTR)&desc,0));
347 TRACE("calling lpDSEnumCallback(%s,\"%s\",\"%s\",%p)\n",
348 debugstr_guid(&DSOUND_renderer_guids[wod]),desc.szDesc,desc.szDrvname,lpContext);
349 if (lpDSEnumCallback(&DSOUND_renderer_guids[wod], desc.szDesc, desc.szDrvname, lpContext) == FALSE)
356 /***************************************************************************
357 * DirectSoundEnumerateW [DSOUND.3]
359 * Enumerate all DirectSound drivers installed in the system
362 * lpDSEnumCallback [I] Address of callback function.
363 * lpContext [I] Address of user defined context passed to callback function.
367 * Failure: DSERR_INVALIDPARAM
369 HRESULT WINAPI DirectSoundEnumerateW(
370 LPDSENUMCALLBACKW lpDSEnumCallback,
377 WCHAR wDesc[MAXPNAMELEN];
378 WCHAR wName[MAXPNAMELEN];
380 TRACE("lpDSEnumCallback = %p, lpContext = %p\n",
381 lpDSEnumCallback, lpContext);
383 if (lpDSEnumCallback == NULL) {
384 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
385 return DSERR_INVALIDPARAM;
388 devs = waveOutGetNumDevs();
390 if (GetDeviceID(&DSDEVID_DefaultPlayback, &guid) == DS_OK) {
391 for (wod = 0; wod < devs; ++wod) {
392 if (IsEqualGUID( &guid, &DSOUND_renderer_guids[wod] ) ) {
393 err = mmErr(waveOutMessage((HWAVEOUT)wod,DRV_QUERYDSOUNDDESC,(DWORD_PTR)&desc,0));
395 TRACE("calling lpDSEnumCallback(NULL,\"%s\",\"%s\",%p)\n",
396 "Primary Sound Driver",desc.szDrvname,lpContext);
397 MultiByteToWideChar( CP_ACP, 0, "Primary Sound Driver", -1,
398 wDesc, sizeof(wDesc)/sizeof(WCHAR) );
399 MultiByteToWideChar( CP_ACP, 0, desc.szDrvname, -1,
400 wName, sizeof(wName)/sizeof(WCHAR) );
401 if (lpDSEnumCallback(NULL, wDesc, wName, lpContext) == FALSE)
409 for (wod = 0; wod < devs; ++wod) {
410 err = mmErr(waveOutMessage((HWAVEOUT)wod,DRV_QUERYDSOUNDDESC,(DWORD_PTR)&desc,0));
412 TRACE("calling lpDSEnumCallback(%s,\"%s\",\"%s\",%p)\n",
413 debugstr_guid(&DSOUND_renderer_guids[wod]),desc.szDesc,desc.szDrvname,lpContext);
414 MultiByteToWideChar( CP_ACP, 0, desc.szDesc, -1,
415 wDesc, sizeof(wDesc)/sizeof(WCHAR) );
416 MultiByteToWideChar( CP_ACP, 0, desc.szDrvname, -1,
417 wName, sizeof(wName)/sizeof(WCHAR) );
418 if (lpDSEnumCallback(&DSOUND_renderer_guids[wod], wDesc, wName, lpContext) == FALSE)
425 /*******************************************************************************
426 * DirectSound ClassFactory
429 typedef HRESULT (*FnCreateInstance)(REFIID riid, LPVOID *ppobj);
432 const IClassFactoryVtbl *lpVtbl;
435 FnCreateInstance pfnCreateInstance;
438 static HRESULT WINAPI
439 DSCF_QueryInterface(LPCLASSFACTORY iface, REFIID riid, LPVOID *ppobj)
441 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
442 FIXME("(%p, %s, %p) stub!\n", This, debugstr_guid(riid), ppobj);
445 return E_NOINTERFACE;
448 static ULONG WINAPI DSCF_AddRef(LPCLASSFACTORY iface)
450 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
451 ULONG ref = InterlockedIncrement(&(This->ref));
452 TRACE("(%p) ref was %d\n", This, ref - 1);
456 static ULONG WINAPI DSCF_Release(LPCLASSFACTORY iface)
458 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
459 ULONG ref = InterlockedDecrement(&(This->ref));
460 TRACE("(%p) ref was %d\n", This, ref + 1);
461 /* static class, won't be freed */
465 static HRESULT WINAPI DSCF_CreateInstance(
466 LPCLASSFACTORY iface,
471 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
472 TRACE("(%p, %p, %s, %p)\n", This, pOuter, debugstr_guid(riid), ppobj);
475 return CLASS_E_NOAGGREGATION;
478 WARN("invalid parameter\n");
479 return DSERR_INVALIDPARAM;
482 return This->pfnCreateInstance(riid, ppobj);
485 static HRESULT WINAPI DSCF_LockServer(LPCLASSFACTORY iface, BOOL dolock)
487 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
488 FIXME("(%p, %d) stub!\n", This, dolock);
492 static const IClassFactoryVtbl DSCF_Vtbl = {
500 static IClassFactoryImpl DSOUND_CF[] = {
501 { &DSCF_Vtbl, 1, &CLSID_DirectSound, (FnCreateInstance)DSOUND_Create },
502 { &DSCF_Vtbl, 1, &CLSID_DirectSound8, (FnCreateInstance)DSOUND_Create8 },
503 { &DSCF_Vtbl, 1, &CLSID_DirectSoundCapture, (FnCreateInstance)DSOUND_CaptureCreate },
504 { &DSCF_Vtbl, 1, &CLSID_DirectSoundCapture8, (FnCreateInstance)DSOUND_CaptureCreate8 },
505 { &DSCF_Vtbl, 1, &CLSID_DirectSoundFullDuplex, (FnCreateInstance)DSOUND_FullDuplexCreate },
506 { &DSCF_Vtbl, 1, &CLSID_DirectSoundPrivate, (FnCreateInstance)IKsPrivatePropertySetImpl_Create },
507 { NULL, 0, NULL, NULL }
510 /*******************************************************************************
511 * DllGetClassObject [DSOUND.@]
512 * Retrieves class object from a DLL object
515 * Docs say returns STDAPI
518 * rclsid [I] CLSID for the class object
519 * riid [I] Reference to identifier of interface for class object
520 * ppv [O] Address of variable to receive interface pointer for riid
524 * Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
527 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
530 TRACE("(%s, %s, %p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
533 WARN("invalid parameter\n");
539 if (!IsEqualIID(riid, &IID_IClassFactory) &&
540 !IsEqualIID(riid, &IID_IUnknown)) {
541 WARN("no interface for %s\n", debugstr_guid(riid));
542 return E_NOINTERFACE;
545 while (NULL != DSOUND_CF[i].rclsid) {
546 if (IsEqualGUID(rclsid, DSOUND_CF[i].rclsid)) {
547 DSCF_AddRef((IClassFactory*) &DSOUND_CF[i]);
548 *ppv = &DSOUND_CF[i];
554 WARN("(%s, %s, %p): no class found.\n", debugstr_guid(rclsid),
555 debugstr_guid(riid), ppv);
556 return CLASS_E_CLASSNOTAVAILABLE;
560 /*******************************************************************************
561 * DllCanUnloadNow [DSOUND.4]
562 * Determines whether the DLL is in use.
568 HRESULT WINAPI DllCanUnloadNow(void)
570 FIXME("(void): stub\n");
574 #define INIT_GUID(guid, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \
575 guid.Data1 = l; guid.Data2 = w1; guid.Data3 = w2; \
576 guid.Data4[0] = b1; guid.Data4[1] = b2; guid.Data4[2] = b3; \
577 guid.Data4[3] = b4; guid.Data4[4] = b5; guid.Data4[5] = b6; \
578 guid.Data4[6] = b7; guid.Data4[7] = b8;
580 /***********************************************************************
581 * DllMain (DSOUND.init)
583 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpvReserved)
586 TRACE("(%p %d %p)\n", hInstDLL, fdwReason, lpvReserved);
589 case DLL_PROCESS_ATTACH:
590 TRACE("DLL_PROCESS_ATTACH\n");
591 for (i = 0; i < MAXWAVEDRIVERS; i++) {
592 DSOUND_renderer[i] = NULL;
593 DSOUND_capture[i] = NULL;
594 INIT_GUID(DSOUND_renderer_guids[i], 0xbd6dd71a, 0x3deb, 0x11d1, 0xb1, 0x71, 0x00, 0xc0, 0x4f, 0xc2, 0x00, 0x00 + i);
595 INIT_GUID(DSOUND_capture_guids[i], 0xbd6dd71b, 0x3deb, 0x11d1, 0xb1, 0x71, 0x00, 0xc0, 0x4f, 0xc2, 0x00, 0x00 + i);
598 case DLL_PROCESS_DETACH:
599 TRACE("DLL_PROCESS_DETACH\n");
601 case DLL_THREAD_ATTACH:
602 TRACE("DLL_THREAD_ATTACH\n");
604 case DLL_THREAD_DETACH:
605 TRACE("DLL_THREAD_DETACH\n");
608 TRACE("UNKNOWN REASON\n");