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 2 /* 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 2 /* 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 10 /* max number of fragments to prebuffer */
71 DirectSoundDevice* DSOUND_renderer[MAXWAVEDRIVERS];
72 GUID DSOUND_renderer_guids[MAXWAVEDRIVERS];
73 GUID DSOUND_capture_guids[MAXWAVEDRIVERS];
75 HRESULT mmErr(UINT err)
78 case MMSYSERR_NOERROR:
80 case MMSYSERR_ALLOCATED:
81 return DSERR_ALLOCATED;
83 case MMSYSERR_INVALHANDLE:
84 case WAVERR_STILLPLAYING:
85 return DSERR_GENERIC; /* FIXME */
86 case MMSYSERR_NODRIVER:
87 return DSERR_NODRIVER;
89 return DSERR_OUTOFMEMORY;
90 case MMSYSERR_INVALPARAM:
91 case WAVERR_BADFORMAT:
92 case WAVERR_UNPREPARED:
93 return DSERR_INVALIDPARAM;
94 case MMSYSERR_NOTSUPPORTED:
95 return DSERR_UNSUPPORTED;
97 FIXME("Unknown MMSYS error %d\n",err);
102 int ds_emuldriver = DS_EMULDRIVER;
103 int ds_hel_margin = DS_HEL_MARGIN;
104 int ds_hel_queue = DS_HEL_QUEUE;
105 int ds_snd_queue_max = DS_SND_QUEUE_MAX;
106 int ds_hw_accel = DS_HW_ACCEL_FULL;
107 int ds_default_playback = 0;
108 int ds_default_capture = 0;
109 int ds_default_sample_rate = 22050;
110 int ds_default_bits_per_sample = 8;
113 * Get a config key from either the app-specific or the default config
116 static inline DWORD get_config_key( HKEY defkey, HKEY appkey, const char *name,
117 char *buffer, DWORD size )
119 if (appkey && !RegQueryValueExA( appkey, name, 0, NULL, (LPBYTE)buffer, &size )) return 0;
120 if (defkey && !RegQueryValueExA( defkey, name, 0, NULL, (LPBYTE)buffer, &size )) return 0;
121 return ERROR_FILE_NOT_FOUND;
126 * Setup the dsound options.
129 void setup_dsound_options(void)
131 char buffer[MAX_PATH+16];
132 HKEY hkey, appkey = 0;
135 buffer[MAX_PATH]='\0';
137 /* @@ Wine registry key: HKCU\Software\Wine\DirectSound */
138 if (RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\DirectSound", &hkey )) hkey = 0;
140 len = GetModuleFileNameA( 0, buffer, MAX_PATH );
141 if (len && len < MAX_PATH)
144 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\DirectSound */
145 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey ))
147 char *p, *appname = buffer;
148 if ((p = strrchr( appname, '/' ))) appname = p + 1;
149 if ((p = strrchr( appname, '\\' ))) appname = p + 1;
150 strcat( appname, "\\DirectSound" );
151 TRACE("appname = [%s]\n", appname);
152 if (RegOpenKeyA( tmpkey, appname, &appkey )) appkey = 0;
153 RegCloseKey( tmpkey );
159 if (!get_config_key( hkey, appkey, "EmulDriver", buffer, MAX_PATH ))
160 ds_emuldriver = strcmp(buffer, "N");
162 if (!get_config_key( hkey, appkey, "HELmargin", buffer, MAX_PATH ))
163 ds_hel_margin = atoi(buffer);
165 if (!get_config_key( hkey, appkey, "HELqueue", buffer, MAX_PATH ))
166 ds_hel_queue = atoi(buffer);
168 if (!get_config_key( hkey, appkey, "SndQueueMax", buffer, MAX_PATH ))
169 ds_snd_queue_max = atoi(buffer);
171 if (!get_config_key( hkey, appkey, "HardwareAcceleration", buffer, MAX_PATH )) {
172 if (strcmp(buffer, "Full") == 0)
173 ds_hw_accel = DS_HW_ACCEL_FULL;
174 else if (strcmp(buffer, "Standard") == 0)
175 ds_hw_accel = DS_HW_ACCEL_STANDARD;
176 else if (strcmp(buffer, "Basic") == 0)
177 ds_hw_accel = DS_HW_ACCEL_BASIC;
178 else if (strcmp(buffer, "Emulation") == 0)
179 ds_hw_accel = DS_HW_ACCEL_EMULATION;
182 if (!get_config_key( hkey, appkey, "DefaultPlayback", buffer, MAX_PATH ))
183 ds_default_playback = atoi(buffer);
185 if (!get_config_key( hkey, appkey, "DefaultCapture", buffer, MAX_PATH ))
186 ds_default_capture = atoi(buffer);
188 if (!get_config_key( hkey, appkey, "DefaultSampleRate", buffer, MAX_PATH ))
189 ds_default_sample_rate = atoi(buffer);
191 if (!get_config_key( hkey, appkey, "DefaultBitsPerSample", buffer, MAX_PATH ))
192 ds_default_bits_per_sample = atoi(buffer);
194 if (appkey) RegCloseKey( appkey );
195 if (hkey) RegCloseKey( hkey );
197 if (ds_emuldriver != DS_EMULDRIVER )
198 WARN("ds_emuldriver = %d (default=%d)\n",ds_emuldriver, DS_EMULDRIVER);
199 if (ds_hel_margin != DS_HEL_MARGIN )
200 WARN("ds_hel_margin = %d (default=%d)\n",ds_hel_margin, DS_HEL_MARGIN );
201 if (ds_hel_queue != DS_HEL_QUEUE )
202 WARN("ds_hel_queue = %d (default=%d)\n",ds_hel_queue, DS_HEL_QUEUE );
203 if (ds_snd_queue_max != DS_SND_QUEUE_MAX)
204 WARN("ds_snd_queue_max = %d (default=%d)\n",ds_snd_queue_max ,DS_SND_QUEUE_MAX);
205 if (ds_hw_accel != DS_HW_ACCEL_FULL)
206 WARN("ds_hw_accel = %s (default=Full)\n",
207 ds_hw_accel==DS_HW_ACCEL_FULL ? "Full" :
208 ds_hw_accel==DS_HW_ACCEL_STANDARD ? "Standard" :
209 ds_hw_accel==DS_HW_ACCEL_BASIC ? "Basic" :
210 ds_hw_accel==DS_HW_ACCEL_EMULATION ? "Emulation" :
212 if (ds_default_playback != 0)
213 WARN("ds_default_playback = %d (default=0)\n",ds_default_playback);
214 if (ds_default_capture != 0)
215 WARN("ds_default_capture = %d (default=0)\n",ds_default_playback);
216 if (ds_default_sample_rate != 22050)
217 WARN("ds_default_sample_rate = %d (default=22050)\n",ds_default_sample_rate);
218 if (ds_default_bits_per_sample != 8)
219 WARN("ds_default_bits_per_sample = %d (default=8)\n",ds_default_bits_per_sample);
222 static const char * get_device_id(LPCGUID pGuid)
224 if (IsEqualGUID(&DSDEVID_DefaultPlayback, pGuid))
225 return "DSDEVID_DefaultPlayback";
226 else if (IsEqualGUID(&DSDEVID_DefaultVoicePlayback, pGuid))
227 return "DSDEVID_DefaultVoicePlayback";
228 else if (IsEqualGUID(&DSDEVID_DefaultCapture, pGuid))
229 return "DSDEVID_DefaultCapture";
230 else if (IsEqualGUID(&DSDEVID_DefaultVoiceCapture, pGuid))
231 return "DSDEVID_DefaultVoiceCapture";
232 return debugstr_guid(pGuid);
235 /***************************************************************************
236 * GetDeviceID [DSOUND.9]
238 * Retrieves unique identifier of default device specified
241 * pGuidSrc [I] Address of device GUID.
242 * pGuidDest [O] Address to receive unique device GUID.
246 * Failure: DSERR_INVALIDPARAM
249 * pGuidSrc is a valid device GUID or DSDEVID_DefaultPlayback,
250 * DSDEVID_DefaultCapture, DSDEVID_DefaultVoicePlayback, or
251 * DSDEVID_DefaultVoiceCapture.
252 * Returns pGuidSrc if pGuidSrc is a valid device or the device
253 * GUID for the specified constants.
255 HRESULT WINAPI GetDeviceID(LPCGUID pGuidSrc, LPGUID pGuidDest)
257 TRACE("(%s,%p)\n", get_device_id(pGuidSrc),pGuidDest);
259 if ( pGuidSrc == NULL) {
260 WARN("invalid parameter: pGuidSrc == NULL\n");
261 return DSERR_INVALIDPARAM;
264 if ( pGuidDest == NULL ) {
265 WARN("invalid parameter: pGuidDest == NULL\n");
266 return DSERR_INVALIDPARAM;
269 if ( IsEqualGUID( &DSDEVID_DefaultPlayback, pGuidSrc ) ||
270 IsEqualGUID( &DSDEVID_DefaultVoicePlayback, pGuidSrc ) ) {
271 CopyMemory(pGuidDest, &DSOUND_renderer_guids[ds_default_playback], sizeof(GUID));
272 TRACE("returns %s\n", get_device_id(pGuidDest));
276 if ( IsEqualGUID( &DSDEVID_DefaultCapture, pGuidSrc ) ||
277 IsEqualGUID( &DSDEVID_DefaultVoiceCapture, pGuidSrc ) ) {
278 CopyMemory(pGuidDest, &DSOUND_capture_guids[ds_default_capture], sizeof(GUID));
279 TRACE("returns %s\n", get_device_id(pGuidDest));
283 CopyMemory(pGuidDest, pGuidSrc, sizeof(GUID));
284 TRACE("returns %s\n", get_device_id(pGuidDest));
290 /***************************************************************************
291 * DirectSoundEnumerateA [DSOUND.2]
293 * Enumerate all DirectSound drivers installed in the system
296 * lpDSEnumCallback [I] Address of callback function.
297 * lpContext [I] Address of user defined context passed to callback function.
301 * Failure: DSERR_INVALIDPARAM
303 HRESULT WINAPI DirectSoundEnumerateA(
304 LPDSENUMCALLBACKA lpDSEnumCallback,
312 TRACE("lpDSEnumCallback = %p, lpContext = %p\n",
313 lpDSEnumCallback, lpContext);
315 if (lpDSEnumCallback == NULL) {
316 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
317 return DSERR_INVALIDPARAM;
320 devs = waveOutGetNumDevs();
322 if (GetDeviceID(&DSDEVID_DefaultPlayback, &guid) == DS_OK) {
323 for (wod = 0; wod < devs; ++wod) {
324 if (IsEqualGUID( &guid, &DSOUND_renderer_guids[wod]) ) {
325 err = mmErr(waveOutMessage((HWAVEOUT)wod,DRV_QUERYDSOUNDDESC,(DWORD_PTR)&desc,0));
327 TRACE("calling lpDSEnumCallback(NULL,\"%s\",\"%s\",%p)\n",
328 "Primary Sound Driver",desc.szDrvname,lpContext);
329 if (lpDSEnumCallback(NULL, "Primary Sound Driver", desc.szDrvname, lpContext) == FALSE)
337 for (wod = 0; wod < devs; ++wod) {
338 err = mmErr(waveOutMessage((HWAVEOUT)wod,DRV_QUERYDSOUNDDESC,(DWORD_PTR)&desc,0));
340 TRACE("calling lpDSEnumCallback(%s,\"%s\",\"%s\",%p)\n",
341 debugstr_guid(&DSOUND_renderer_guids[wod]),desc.szDesc,desc.szDrvname,lpContext);
342 if (lpDSEnumCallback(&DSOUND_renderer_guids[wod], desc.szDesc, desc.szDrvname, lpContext) == FALSE)
349 /***************************************************************************
350 * DirectSoundEnumerateW [DSOUND.3]
352 * Enumerate all DirectSound drivers installed in the system
355 * lpDSEnumCallback [I] Address of callback function.
356 * lpContext [I] Address of user defined context passed to callback function.
360 * Failure: DSERR_INVALIDPARAM
362 HRESULT WINAPI DirectSoundEnumerateW(
363 LPDSENUMCALLBACKW lpDSEnumCallback,
370 WCHAR wDesc[MAXPNAMELEN];
371 WCHAR wName[MAXPNAMELEN];
373 TRACE("lpDSEnumCallback = %p, lpContext = %p\n",
374 lpDSEnumCallback, lpContext);
376 if (lpDSEnumCallback == NULL) {
377 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
378 return DSERR_INVALIDPARAM;
381 devs = waveOutGetNumDevs();
383 if (GetDeviceID(&DSDEVID_DefaultPlayback, &guid) == DS_OK) {
384 for (wod = 0; wod < devs; ++wod) {
385 if (IsEqualGUID( &guid, &DSOUND_renderer_guids[wod] ) ) {
386 err = mmErr(waveOutMessage((HWAVEOUT)wod,DRV_QUERYDSOUNDDESC,(DWORD_PTR)&desc,0));
388 TRACE("calling lpDSEnumCallback(NULL,\"%s\",\"%s\",%p)\n",
389 "Primary Sound Driver",desc.szDrvname,lpContext);
390 MultiByteToWideChar( CP_ACP, 0, "Primary Sound Driver", -1,
391 wDesc, sizeof(wDesc)/sizeof(WCHAR) );
392 MultiByteToWideChar( CP_ACP, 0, desc.szDrvname, -1,
393 wName, sizeof(wName)/sizeof(WCHAR) );
394 if (lpDSEnumCallback(NULL, wDesc, wName, lpContext) == FALSE)
402 for (wod = 0; wod < devs; ++wod) {
403 err = mmErr(waveOutMessage((HWAVEOUT)wod,DRV_QUERYDSOUNDDESC,(DWORD_PTR)&desc,0));
405 TRACE("calling lpDSEnumCallback(%s,\"%s\",\"%s\",%p)\n",
406 debugstr_guid(&DSOUND_renderer_guids[wod]),desc.szDesc,desc.szDrvname,lpContext);
407 MultiByteToWideChar( CP_ACP, 0, desc.szDesc, -1,
408 wDesc, sizeof(wDesc)/sizeof(WCHAR) );
409 MultiByteToWideChar( CP_ACP, 0, desc.szDrvname, -1,
410 wName, sizeof(wName)/sizeof(WCHAR) );
411 if (lpDSEnumCallback(&DSOUND_renderer_guids[wod], wDesc, wName, lpContext) == FALSE)
418 /*******************************************************************************
419 * DirectSound ClassFactory
422 typedef HRESULT (*FnCreateInstance)(REFIID riid, LPVOID *ppobj);
425 const IClassFactoryVtbl *lpVtbl;
428 FnCreateInstance pfnCreateInstance;
431 static HRESULT WINAPI
432 DSCF_QueryInterface(LPCLASSFACTORY iface, REFIID riid, LPVOID *ppobj)
434 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
435 TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
438 if (IsEqualIID(riid, &IID_IUnknown) ||
439 IsEqualIID(riid, &IID_IClassFactory))
442 IUnknown_AddRef(iface);
446 return E_NOINTERFACE;
449 static ULONG WINAPI DSCF_AddRef(LPCLASSFACTORY iface)
451 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
452 ULONG ref = InterlockedIncrement(&(This->ref));
453 TRACE("(%p) ref was %d\n", This, ref - 1);
457 static ULONG WINAPI DSCF_Release(LPCLASSFACTORY iface)
459 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
460 ULONG ref = InterlockedDecrement(&(This->ref));
461 TRACE("(%p) ref was %d\n", This, ref + 1);
462 /* static class, won't be freed */
466 static HRESULT WINAPI DSCF_CreateInstance(
467 LPCLASSFACTORY iface,
472 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
473 TRACE("(%p, %p, %s, %p)\n", This, pOuter, debugstr_guid(riid), ppobj);
476 return CLASS_E_NOAGGREGATION;
479 WARN("invalid parameter\n");
480 return DSERR_INVALIDPARAM;
483 return This->pfnCreateInstance(riid, ppobj);
486 static HRESULT WINAPI DSCF_LockServer(LPCLASSFACTORY iface, BOOL dolock)
488 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
489 FIXME("(%p, %d) stub!\n", This, dolock);
493 static const IClassFactoryVtbl DSCF_Vtbl = {
501 static IClassFactoryImpl DSOUND_CF[] = {
502 { &DSCF_Vtbl, 1, &CLSID_DirectSound, (FnCreateInstance)DSOUND_Create },
503 { &DSCF_Vtbl, 1, &CLSID_DirectSound8, (FnCreateInstance)DSOUND_Create8 },
504 { &DSCF_Vtbl, 1, &CLSID_DirectSoundCapture, (FnCreateInstance)DSOUND_CaptureCreate },
505 { &DSCF_Vtbl, 1, &CLSID_DirectSoundCapture8, (FnCreateInstance)DSOUND_CaptureCreate8 },
506 { &DSCF_Vtbl, 1, &CLSID_DirectSoundFullDuplex, (FnCreateInstance)DSOUND_FullDuplexCreate },
507 { &DSCF_Vtbl, 1, &CLSID_DirectSoundPrivate, (FnCreateInstance)IKsPrivatePropertySetImpl_Create },
508 { NULL, 0, NULL, NULL }
511 /*******************************************************************************
512 * DllGetClassObject [DSOUND.@]
513 * Retrieves class object from a DLL object
516 * Docs say returns STDAPI
519 * rclsid [I] CLSID for the class object
520 * riid [I] Reference to identifier of interface for class object
521 * ppv [O] Address of variable to receive interface pointer for riid
525 * Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
528 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
531 TRACE("(%s, %s, %p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
534 WARN("invalid parameter\n");
540 if (!IsEqualIID(riid, &IID_IClassFactory) &&
541 !IsEqualIID(riid, &IID_IUnknown)) {
542 WARN("no interface for %s\n", debugstr_guid(riid));
543 return E_NOINTERFACE;
546 while (NULL != DSOUND_CF[i].rclsid) {
547 if (IsEqualGUID(rclsid, DSOUND_CF[i].rclsid)) {
548 DSCF_AddRef((IClassFactory*) &DSOUND_CF[i]);
549 *ppv = &DSOUND_CF[i];
555 WARN("(%s, %s, %p): no class found.\n", debugstr_guid(rclsid),
556 debugstr_guid(riid), ppv);
557 return CLASS_E_CLASSNOTAVAILABLE;
561 /*******************************************************************************
562 * DllCanUnloadNow [DSOUND.4]
563 * Determines whether the DLL is in use.
569 HRESULT WINAPI DllCanUnloadNow(void)
571 FIXME("(void): stub\n");
575 #define INIT_GUID(guid, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \
576 guid.Data1 = l; guid.Data2 = w1; guid.Data3 = w2; \
577 guid.Data4[0] = b1; guid.Data4[1] = b2; guid.Data4[2] = b3; \
578 guid.Data4[3] = b4; guid.Data4[4] = b5; guid.Data4[5] = b6; \
579 guid.Data4[6] = b7; guid.Data4[7] = b8;
581 /***********************************************************************
582 * DllMain (DSOUND.init)
584 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpvReserved)
587 TRACE("(%p %d %p)\n", hInstDLL, fdwReason, lpvReserved);
590 case DLL_PROCESS_ATTACH:
591 TRACE("DLL_PROCESS_ATTACH\n");
592 for (i = 0; i < MAXWAVEDRIVERS; i++) {
593 DSOUND_renderer[i] = NULL;
594 DSOUND_capture[i] = NULL;
595 INIT_GUID(DSOUND_renderer_guids[i], 0xbd6dd71a, 0x3deb, 0x11d1, 0xb1, 0x71, 0x00, 0xc0, 0x4f, 0xc2, 0x00, 0x00 + i);
596 INIT_GUID(DSOUND_capture_guids[i], 0xbd6dd71b, 0x3deb, 0x11d1, 0xb1, 0x71, 0x00, 0xc0, 0x4f, 0xc2, 0x00, 0x00 + i);
599 case DLL_PROCESS_DETACH:
600 TRACE("DLL_PROCESS_DETACH\n");
602 case DLL_THREAD_ATTACH:
603 TRACE("DLL_THREAD_ATTACH\n");
605 case DLL_THREAD_DETACH:
606 TRACE("DLL_THREAD_DETACH\n");
609 TRACE("UNKNOWN REASON\n");