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 DirectSoundDevice* DSOUND_renderer[MAXWAVEDRIVERS];
61 GUID DSOUND_renderer_guid = { 0xbd6dd71a,0x3deb,0x11d1, {0xb1,0x71,0x00,0xc0,0x4f,0xc2,0x00,0x00} };
62 GUID DSOUND_capture_guid = { 0xbd6dd71b,0x3deb,0x11d1, {0xb1,0x71,0x00,0xc0,0x4f,0xc2,0x00,0x00} };
64 HRESULT mmErr(UINT err)
67 case MMSYSERR_NOERROR:
69 case MMSYSERR_ALLOCATED:
70 return DSERR_ALLOCATED;
72 case MMSYSERR_INVALHANDLE:
73 case WAVERR_STILLPLAYING:
74 return DSERR_GENERIC; /* FIXME */
75 case MMSYSERR_NODRIVER:
76 return DSERR_NODRIVER;
78 return DSERR_OUTOFMEMORY;
79 case MMSYSERR_INVALPARAM:
80 case WAVERR_BADFORMAT:
81 case WAVERR_UNPREPARED:
82 return DSERR_INVALIDPARAM;
83 case MMSYSERR_NOTSUPPORTED:
84 return DSERR_UNSUPPORTED;
86 FIXME("Unknown MMSYS error %d\n",err);
91 /* All default settings, you most likely don't want to touch these, see wiki on UsefulRegistryKeys */
92 int ds_emuldriver = 0;
93 int ds_hel_buflen = 32768 * 2;
94 int ds_snd_queue_max = 10;
95 int ds_snd_queue_min = 6;
96 int ds_snd_shadow_maxsize = 2;
97 int ds_hw_accel = DS_HW_ACCEL_FULL;
98 int ds_default_sample_rate = 44100;
99 int ds_default_bits_per_sample = 16;
100 static int ds_default_playback;
101 static int ds_default_capture;
104 * Get a config key from either the app-specific or the default config
107 static inline DWORD get_config_key( HKEY defkey, HKEY appkey, const char *name,
108 char *buffer, DWORD size )
110 if (appkey && !RegQueryValueExA( appkey, name, 0, NULL, (LPBYTE)buffer, &size )) return 0;
111 if (defkey && !RegQueryValueExA( defkey, name, 0, NULL, (LPBYTE)buffer, &size )) return 0;
112 return ERROR_FILE_NOT_FOUND;
117 * Setup the dsound options.
120 void setup_dsound_options(void)
122 char buffer[MAX_PATH+16];
123 HKEY hkey, appkey = 0;
126 buffer[MAX_PATH]='\0';
128 /* @@ Wine registry key: HKCU\Software\Wine\DirectSound */
129 if (RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\DirectSound", &hkey )) hkey = 0;
131 len = GetModuleFileNameA( 0, buffer, MAX_PATH );
132 if (len && len < MAX_PATH)
135 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\DirectSound */
136 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey ))
138 char *p, *appname = buffer;
139 if ((p = strrchr( appname, '/' ))) appname = p + 1;
140 if ((p = strrchr( appname, '\\' ))) appname = p + 1;
141 strcat( appname, "\\DirectSound" );
142 TRACE("appname = [%s]\n", appname);
143 if (RegOpenKeyA( tmpkey, appname, &appkey )) appkey = 0;
144 RegCloseKey( tmpkey );
150 if (!get_config_key( hkey, appkey, "EmulDriver", buffer, MAX_PATH ))
151 ds_emuldriver = strcmp(buffer, "N");
153 if (!get_config_key( hkey, appkey, "HelBuflen", buffer, MAX_PATH ))
154 ds_hel_buflen = atoi(buffer);
156 if (!get_config_key( hkey, appkey, "SndQueueMax", buffer, MAX_PATH ))
157 ds_snd_queue_max = atoi(buffer);
159 if (!get_config_key( hkey, appkey, "SndQueueMin", buffer, MAX_PATH ))
160 ds_snd_queue_min = atoi(buffer);
162 if (!get_config_key( hkey, appkey, "HardwareAcceleration", buffer, MAX_PATH )) {
163 if (strcmp(buffer, "Full") == 0)
164 ds_hw_accel = DS_HW_ACCEL_FULL;
165 else if (strcmp(buffer, "Standard") == 0)
166 ds_hw_accel = DS_HW_ACCEL_STANDARD;
167 else if (strcmp(buffer, "Basic") == 0)
168 ds_hw_accel = DS_HW_ACCEL_BASIC;
169 else if (strcmp(buffer, "Emulation") == 0)
170 ds_hw_accel = DS_HW_ACCEL_EMULATION;
173 if (!get_config_key( hkey, appkey, "DefaultPlayback", buffer, MAX_PATH ))
174 ds_default_playback = atoi(buffer);
176 if (!get_config_key( hkey, appkey, "MaxShadowSize", buffer, MAX_PATH ))
177 ds_snd_shadow_maxsize = atoi(buffer);
179 if (!get_config_key( hkey, appkey, "DefaultCapture", buffer, MAX_PATH ))
180 ds_default_capture = atoi(buffer);
182 if (!get_config_key( hkey, appkey, "DefaultSampleRate", buffer, MAX_PATH ))
183 ds_default_sample_rate = atoi(buffer);
185 if (!get_config_key( hkey, appkey, "DefaultBitsPerSample", buffer, MAX_PATH ))
186 ds_default_bits_per_sample = atoi(buffer);
188 if (appkey) RegCloseKey( appkey );
189 if (hkey) RegCloseKey( hkey );
191 TRACE("ds_emuldriver = %d\n", ds_emuldriver);
192 TRACE("ds_hel_buflen = %d\n", ds_hel_buflen);
193 TRACE("ds_snd_queue_max = %d\n", ds_snd_queue_max);
194 TRACE("ds_snd_queue_min = %d\n", ds_snd_queue_min);
195 TRACE("ds_hw_accel = %s\n",
196 ds_hw_accel==DS_HW_ACCEL_FULL ? "Full" :
197 ds_hw_accel==DS_HW_ACCEL_STANDARD ? "Standard" :
198 ds_hw_accel==DS_HW_ACCEL_BASIC ? "Basic" :
199 ds_hw_accel==DS_HW_ACCEL_EMULATION ? "Emulation" :
201 TRACE("ds_default_playback = %d\n", ds_default_playback);
202 TRACE("ds_default_capture = %d\n", ds_default_playback);
203 TRACE("ds_default_sample_rate = %d\n", ds_default_sample_rate);
204 TRACE("ds_default_bits_per_sample = %d\n", ds_default_bits_per_sample);
205 TRACE("ds_snd_shadow_maxsize = %d\n", ds_snd_shadow_maxsize);
208 static const char * get_device_id(LPCGUID pGuid)
210 if (IsEqualGUID(&DSDEVID_DefaultPlayback, pGuid))
211 return "DSDEVID_DefaultPlayback";
212 else if (IsEqualGUID(&DSDEVID_DefaultVoicePlayback, pGuid))
213 return "DSDEVID_DefaultVoicePlayback";
214 else if (IsEqualGUID(&DSDEVID_DefaultCapture, pGuid))
215 return "DSDEVID_DefaultCapture";
216 else if (IsEqualGUID(&DSDEVID_DefaultVoiceCapture, pGuid))
217 return "DSDEVID_DefaultVoiceCapture";
218 return debugstr_guid(pGuid);
221 /***************************************************************************
222 * GetDeviceID [DSOUND.9]
224 * Retrieves unique identifier of default device specified
227 * pGuidSrc [I] Address of device GUID.
228 * pGuidDest [O] Address to receive unique device GUID.
232 * Failure: DSERR_INVALIDPARAM
235 * pGuidSrc is a valid device GUID or DSDEVID_DefaultPlayback,
236 * DSDEVID_DefaultCapture, DSDEVID_DefaultVoicePlayback, or
237 * DSDEVID_DefaultVoiceCapture.
238 * Returns pGuidSrc if pGuidSrc is a valid device or the device
239 * GUID for the specified constants.
241 HRESULT WINAPI GetDeviceID(LPCGUID pGuidSrc, LPGUID pGuidDest)
243 TRACE("(%s,%p)\n", get_device_id(pGuidSrc),pGuidDest);
245 if ( pGuidSrc == NULL) {
246 WARN("invalid parameter: pGuidSrc == NULL\n");
247 return DSERR_INVALIDPARAM;
250 if ( pGuidDest == NULL ) {
251 WARN("invalid parameter: pGuidDest == NULL\n");
252 return DSERR_INVALIDPARAM;
255 if ( IsEqualGUID( &DSDEVID_DefaultPlayback, pGuidSrc ) ||
256 IsEqualGUID( &DSDEVID_DefaultVoicePlayback, pGuidSrc ) ) {
257 *pGuidDest = DSOUND_renderer_guid;
258 pGuidDest->Data4[7] = ds_default_playback;
259 TRACE("returns %s\n", get_device_id(pGuidDest));
263 if ( IsEqualGUID( &DSDEVID_DefaultCapture, pGuidSrc ) ||
264 IsEqualGUID( &DSDEVID_DefaultVoiceCapture, pGuidSrc ) ) {
265 *pGuidDest = DSOUND_capture_guid;
266 pGuidDest->Data4[7] = ds_default_capture;
267 TRACE("returns %s\n", get_device_id(pGuidDest));
271 *pGuidDest = *pGuidSrc;
272 TRACE("returns %s\n", get_device_id(pGuidDest));
279 LPDSENUMCALLBACKA callA;
283 static BOOL CALLBACK a_to_w_callback(LPGUID guid, LPCWSTR descW, LPCWSTR modW, LPVOID data)
285 struct morecontext *context = data;
286 char descA[MAXPNAMELEN], modA[MAXPNAMELEN];
288 WideCharToMultiByte(CP_ACP, 0, descW, -1, descA, sizeof(descA), NULL, NULL);
289 WideCharToMultiByte(CP_ACP, 0, modW, -1, modA, sizeof(modA), NULL, NULL);
291 return context->callA(guid, descA, modA, context->data);
294 /***************************************************************************
295 * DirectSoundEnumerateA [DSOUND.2]
297 * Enumerate all DirectSound drivers installed in the system
300 * lpDSEnumCallback [I] Address of callback function.
301 * lpContext [I] Address of user defined context passed to callback function.
305 * Failure: DSERR_INVALIDPARAM
307 HRESULT WINAPI DirectSoundEnumerateA(
308 LPDSENUMCALLBACKA lpDSEnumCallback,
311 struct morecontext context;
313 if (lpDSEnumCallback == NULL) {
314 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
315 return DSERR_INVALIDPARAM;
318 context.callA = lpDSEnumCallback;
319 context.data = lpContext;
321 return DirectSoundEnumerateW(a_to_w_callback, &context);
324 /***************************************************************************
325 * DirectSoundEnumerateW [DSOUND.3]
327 * Enumerate all DirectSound drivers installed in the system
330 * lpDSEnumCallback [I] Address of callback function.
331 * lpContext [I] Address of user defined context passed to callback function.
335 * Failure: DSERR_INVALIDPARAM
337 HRESULT WINAPI DirectSoundEnumerateW(
338 LPDSENUMCALLBACKW lpDSEnumCallback,
345 WCHAR wDesc[MAXPNAMELEN];
346 WCHAR wName[MAXPNAMELEN];
348 TRACE("lpDSEnumCallback = %p, lpContext = %p\n",
349 lpDSEnumCallback, lpContext);
351 if (lpDSEnumCallback == NULL) {
352 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
353 return DSERR_INVALIDPARAM;
356 devs = waveOutGetNumDevs();
358 if (GetDeviceID(&DSDEVID_DefaultPlayback, &guid) == DS_OK) {
359 static const WCHAR empty[] = { 0 };
362 err = mmErr(waveOutMessage(UlongToHandle(wod),DRV_QUERYDSOUNDDESC,(DWORD_PTR)&desc,0));
364 TRACE("calling lpDSEnumCallback(NULL,\"%s\",\"%s\",%p)\n",
365 "Primary Sound Driver",desc.szDrvname,lpContext);
366 MultiByteToWideChar( CP_ACP, 0, "Primary Sound Driver", -1,
367 wDesc, sizeof(wDesc)/sizeof(WCHAR) );
368 if (lpDSEnumCallback(NULL, wDesc, empty, lpContext) == FALSE)
375 for (wod = 0; wod < devs; ++wod) {
376 err = mmErr(waveOutMessage(UlongToHandle(wod),DRV_QUERYDSOUNDDESC,(DWORD_PTR)&desc,0));
379 TRACE("calling lpDSEnumCallback(%s,\"%s\",\"%s\",%p)\n",
380 debugstr_guid(&guid),desc.szDesc,desc.szDrvname,lpContext);
381 MultiByteToWideChar( CP_ACP, 0, desc.szDesc, -1,
382 wDesc, sizeof(wDesc)/sizeof(WCHAR) );
383 MultiByteToWideChar( CP_ACP, 0, desc.szDrvname, -1,
384 wName, sizeof(wName)/sizeof(WCHAR) );
385 if (lpDSEnumCallback(&guid, wDesc, wName, lpContext) == FALSE)
392 /***************************************************************************
393 * DirectSoundCaptureEnumerateA [DSOUND.7]
395 * Enumerate all DirectSound drivers installed in the system.
398 * lpDSEnumCallback [I] Address of callback function.
399 * lpContext [I] Address of user defined context passed to callback function.
403 * Failure: DSERR_INVALIDPARAM
405 HRESULT WINAPI DirectSoundCaptureEnumerateA(
406 LPDSENUMCALLBACKA lpDSEnumCallback,
409 struct morecontext context;
411 if (lpDSEnumCallback == NULL) {
412 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
413 return DSERR_INVALIDPARAM;
416 context.callA = lpDSEnumCallback;
417 context.data = lpContext;
419 return DirectSoundCaptureEnumerateW(a_to_w_callback, &context);
422 /***************************************************************************
423 * DirectSoundCaptureEnumerateW [DSOUND.8]
425 * Enumerate all DirectSound drivers installed in the system.
428 * lpDSEnumCallback [I] Address of callback function.
429 * lpContext [I] Address of user defined context passed to callback function.
433 * Failure: DSERR_INVALIDPARAM
436 DirectSoundCaptureEnumerateW(
437 LPDSENUMCALLBACKW lpDSEnumCallback,
444 WCHAR wDesc[MAXPNAMELEN];
445 WCHAR wName[MAXPNAMELEN];
447 TRACE("(%p,%p)\n", lpDSEnumCallback, lpContext );
449 if (lpDSEnumCallback == NULL) {
450 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
451 return DSERR_INVALIDPARAM;
454 devs = waveInGetNumDevs();
456 if (GetDeviceID(&DSDEVID_DefaultCapture, &guid) == DS_OK) {
459 err = mmErr(waveInMessage(UlongToHandle(wid),DRV_QUERYDSOUNDDESC,(DWORD_PTR)&desc,0));
461 TRACE("calling lpDSEnumCallback(NULL,\"%s\",\"%s\",%p)\n",
462 "Primary Sound Capture Driver",desc.szDrvname,lpContext);
463 MultiByteToWideChar( CP_ACP, 0, "Primary Sound Capture Driver", -1,
464 wDesc, sizeof(wDesc)/sizeof(WCHAR) );
465 MultiByteToWideChar( CP_ACP, 0, desc.szDrvname, -1,
466 wName, sizeof(wName)/sizeof(WCHAR) );
467 if (lpDSEnumCallback(NULL, wDesc, wName, lpContext) == FALSE)
474 for (wid = 0; wid < devs; ++wid) {
475 err = mmErr(waveInMessage(UlongToHandle(wid),DRV_QUERYDSOUNDDESC,(DWORD_PTR)&desc,0));
478 TRACE("calling lpDSEnumCallback(%s,\"%s\",\"%s\",%p)\n",
479 debugstr_guid(&guid),desc.szDesc,desc.szDrvname,lpContext);
480 MultiByteToWideChar( CP_ACP, 0, desc.szDesc, -1,
481 wDesc, sizeof(wDesc)/sizeof(WCHAR) );
482 MultiByteToWideChar( CP_ACP, 0, desc.szDrvname, -1,
483 wName, sizeof(wName)/sizeof(WCHAR) );
484 if (lpDSEnumCallback(&guid, wDesc, wName, lpContext) == FALSE)
492 /*******************************************************************************
493 * DirectSound ClassFactory
496 typedef HRESULT (*FnCreateInstance)(REFIID riid, LPVOID *ppobj);
499 const IClassFactoryVtbl *lpVtbl;
502 FnCreateInstance pfnCreateInstance;
505 static HRESULT WINAPI
506 DSCF_QueryInterface(LPCLASSFACTORY iface, REFIID riid, LPVOID *ppobj)
508 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
509 TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
512 if (IsEqualIID(riid, &IID_IUnknown) ||
513 IsEqualIID(riid, &IID_IClassFactory))
516 IUnknown_AddRef(iface);
520 return E_NOINTERFACE;
523 static ULONG WINAPI DSCF_AddRef(LPCLASSFACTORY iface)
525 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
526 ULONG ref = InterlockedIncrement(&(This->ref));
527 TRACE("(%p) ref was %d\n", This, ref - 1);
531 static ULONG WINAPI DSCF_Release(LPCLASSFACTORY iface)
533 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
534 ULONG ref = InterlockedDecrement(&(This->ref));
535 TRACE("(%p) ref was %d\n", This, ref + 1);
536 /* static class, won't be freed */
540 static HRESULT WINAPI DSCF_CreateInstance(
541 LPCLASSFACTORY iface,
546 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
547 TRACE("(%p, %p, %s, %p)\n", This, pOuter, debugstr_guid(riid), ppobj);
550 return CLASS_E_NOAGGREGATION;
553 WARN("invalid parameter\n");
554 return DSERR_INVALIDPARAM;
557 return This->pfnCreateInstance(riid, ppobj);
560 static HRESULT WINAPI DSCF_LockServer(LPCLASSFACTORY iface, BOOL dolock)
562 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
563 FIXME("(%p, %d) stub!\n", This, dolock);
567 static const IClassFactoryVtbl DSCF_Vtbl = {
575 static IClassFactoryImpl DSOUND_CF[] = {
576 { &DSCF_Vtbl, 1, &CLSID_DirectSound, (FnCreateInstance)DSOUND_Create },
577 { &DSCF_Vtbl, 1, &CLSID_DirectSound8, (FnCreateInstance)DSOUND_Create8 },
578 { &DSCF_Vtbl, 1, &CLSID_DirectSoundCapture, (FnCreateInstance)DSOUND_CaptureCreate },
579 { &DSCF_Vtbl, 1, &CLSID_DirectSoundCapture8, (FnCreateInstance)DSOUND_CaptureCreate8 },
580 { &DSCF_Vtbl, 1, &CLSID_DirectSoundFullDuplex, (FnCreateInstance)DSOUND_FullDuplexCreate },
581 { &DSCF_Vtbl, 1, &CLSID_DirectSoundPrivate, (FnCreateInstance)IKsPrivatePropertySetImpl_Create },
582 { NULL, 0, NULL, NULL }
585 /*******************************************************************************
586 * DllGetClassObject [DSOUND.@]
587 * Retrieves class object from a DLL object
590 * Docs say returns STDAPI
593 * rclsid [I] CLSID for the class object
594 * riid [I] Reference to identifier of interface for class object
595 * ppv [O] Address of variable to receive interface pointer for riid
599 * Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
602 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
605 TRACE("(%s, %s, %p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
608 WARN("invalid parameter\n");
614 if (!IsEqualIID(riid, &IID_IClassFactory) &&
615 !IsEqualIID(riid, &IID_IUnknown)) {
616 WARN("no interface for %s\n", debugstr_guid(riid));
617 return E_NOINTERFACE;
620 while (NULL != DSOUND_CF[i].rclsid) {
621 if (IsEqualGUID(rclsid, DSOUND_CF[i].rclsid)) {
622 DSCF_AddRef((IClassFactory*) &DSOUND_CF[i]);
623 *ppv = &DSOUND_CF[i];
629 WARN("(%s, %s, %p): no class found.\n", debugstr_guid(rclsid),
630 debugstr_guid(riid), ppv);
631 return CLASS_E_CLASSNOTAVAILABLE;
635 /*******************************************************************************
636 * DllCanUnloadNow [DSOUND.4]
637 * Determines whether the DLL is in use.
643 HRESULT WINAPI DllCanUnloadNow(void)
645 FIXME("(void): stub\n");
649 #define INIT_GUID(guid, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \
650 guid.Data1 = l; guid.Data2 = w1; guid.Data3 = w2; \
651 guid.Data4[0] = b1; guid.Data4[1] = b2; guid.Data4[2] = b3; \
652 guid.Data4[3] = b4; guid.Data4[4] = b5; guid.Data4[5] = b6; \
653 guid.Data4[6] = b7; guid.Data4[7] = b8;
655 /***********************************************************************
656 * DllMain (DSOUND.init)
658 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpvReserved)
661 TRACE("(%p %d %p)\n", hInstDLL, fdwReason, lpvReserved);
664 case DLL_PROCESS_ATTACH:
665 TRACE("DLL_PROCESS_ATTACH\n");
666 for (i = 0; i < MAXWAVEDRIVERS; i++) {
667 DSOUND_renderer[i] = NULL;
668 DSOUND_capture[i] = NULL;
670 DisableThreadLibraryCalls(hInstDLL);
671 /* Increase refcount on dsound by 1 */
672 GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCWSTR)hInstDLL, &hInstDLL);
674 case DLL_PROCESS_DETACH:
675 TRACE("DLL_PROCESS_DETACH\n");
678 TRACE("UNKNOWN REASON\n");