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"
51 #include "dsound_private.h"
54 WINE_DEFAULT_DEBUG_CHANNEL(dsound);
56 #define DS_HEL_BUFLEN 0x8000 /* HEL: The buffer length of the emulated buffer */
57 #define DS_SND_QUEUE_MAX 10 /* max number of fragments to prebuffer, each fragment is approximately 10 ms long */
58 #define DS_SND_QUEUE_MIN 6 /* If the minimum of prebuffered fragments go below this, forcibly take all locks to prevent underruns */
60 DirectSoundDevice* DSOUND_renderer[MAXWAVEDRIVERS];
61 GUID DSOUND_renderer_guids[MAXWAVEDRIVERS];
62 GUID DSOUND_capture_guids[MAXWAVEDRIVERS];
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 int ds_emuldriver = 0;
92 int ds_hel_buflen = DS_HEL_BUFLEN;
93 int ds_snd_queue_max = DS_SND_QUEUE_MAX;
94 int ds_snd_queue_min = DS_SND_QUEUE_MIN;
95 int ds_hw_accel = DS_HW_ACCEL_FULL;
96 int ds_default_playback = 0;
97 int ds_default_capture = 0;
98 int ds_default_sample_rate = 22050;
99 int ds_default_bits_per_sample = 8;
102 * Get a config key from either the app-specific or the default config
105 static inline DWORD get_config_key( HKEY defkey, HKEY appkey, const char *name,
106 char *buffer, DWORD size )
108 if (appkey && !RegQueryValueExA( appkey, name, 0, NULL, (LPBYTE)buffer, &size )) return 0;
109 if (defkey && !RegQueryValueExA( defkey, name, 0, NULL, (LPBYTE)buffer, &size )) return 0;
110 return ERROR_FILE_NOT_FOUND;
115 * Setup the dsound options.
118 void setup_dsound_options(void)
120 char buffer[MAX_PATH+16];
121 HKEY hkey, appkey = 0;
124 buffer[MAX_PATH]='\0';
126 /* @@ Wine registry key: HKCU\Software\Wine\DirectSound */
127 if (RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\DirectSound", &hkey )) hkey = 0;
129 len = GetModuleFileNameA( 0, buffer, MAX_PATH );
130 if (len && len < MAX_PATH)
133 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\DirectSound */
134 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey ))
136 char *p, *appname = buffer;
137 if ((p = strrchr( appname, '/' ))) appname = p + 1;
138 if ((p = strrchr( appname, '\\' ))) appname = p + 1;
139 strcat( appname, "\\DirectSound" );
140 TRACE("appname = [%s]\n", appname);
141 if (RegOpenKeyA( tmpkey, appname, &appkey )) appkey = 0;
142 RegCloseKey( tmpkey );
148 if (!get_config_key( hkey, appkey, "EmulDriver", buffer, MAX_PATH ))
149 ds_emuldriver = strcmp(buffer, "N");
151 if (!get_config_key( hkey, appkey, "HelBuflen", buffer, MAX_PATH ))
152 ds_hel_buflen = atoi(buffer);
154 if (!get_config_key( hkey, appkey, "SndQueueMax", buffer, MAX_PATH ))
155 ds_snd_queue_max = atoi(buffer);
157 if (!get_config_key( hkey, appkey, "SndQueueMin", buffer, MAX_PATH ))
158 ds_snd_queue_min = atoi(buffer);
160 if (!get_config_key( hkey, appkey, "HardwareAcceleration", buffer, MAX_PATH )) {
161 if (strcmp(buffer, "Full") == 0)
162 ds_hw_accel = DS_HW_ACCEL_FULL;
163 else if (strcmp(buffer, "Standard") == 0)
164 ds_hw_accel = DS_HW_ACCEL_STANDARD;
165 else if (strcmp(buffer, "Basic") == 0)
166 ds_hw_accel = DS_HW_ACCEL_BASIC;
167 else if (strcmp(buffer, "Emulation") == 0)
168 ds_hw_accel = DS_HW_ACCEL_EMULATION;
171 if (!get_config_key( hkey, appkey, "DefaultPlayback", buffer, MAX_PATH ))
172 ds_default_playback = atoi(buffer);
174 if (!get_config_key( hkey, appkey, "DefaultCapture", buffer, MAX_PATH ))
175 ds_default_capture = atoi(buffer);
177 if (!get_config_key( hkey, appkey, "DefaultSampleRate", buffer, MAX_PATH ))
178 ds_default_sample_rate = atoi(buffer);
180 if (!get_config_key( hkey, appkey, "DefaultBitsPerSample", buffer, MAX_PATH ))
181 ds_default_bits_per_sample = atoi(buffer);
183 if (appkey) RegCloseKey( appkey );
184 if (hkey) RegCloseKey( hkey );
187 WARN("ds_emuldriver = %d (default=0)\n",ds_emuldriver);
188 if (ds_hel_buflen != DS_HEL_BUFLEN)
189 WARN("ds_hel_buflen = %d (default=%d)\n",ds_hel_buflen ,DS_HEL_BUFLEN);
190 if (ds_snd_queue_max != DS_SND_QUEUE_MAX)
191 WARN("ds_snd_queue_max = %d (default=%d)\n",ds_snd_queue_max ,DS_SND_QUEUE_MAX);
192 if (ds_snd_queue_min != DS_SND_QUEUE_MIN)
193 WARN("ds_snd_queue_min = %d (default=%d)\n",ds_snd_queue_min ,DS_SND_QUEUE_MIN);
194 if (ds_hw_accel != DS_HW_ACCEL_FULL)
195 WARN("ds_hw_accel = %s (default=Full)\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 if (ds_default_playback != 0)
202 WARN("ds_default_playback = %d (default=0)\n",ds_default_playback);
203 if (ds_default_capture != 0)
204 WARN("ds_default_capture = %d (default=0)\n",ds_default_playback);
205 if (ds_default_sample_rate != 22050)
206 WARN("ds_default_sample_rate = %d (default=22050)\n",ds_default_sample_rate);
207 if (ds_default_bits_per_sample != 8)
208 WARN("ds_default_bits_per_sample = %d (default=8)\n",ds_default_bits_per_sample);
211 static const char * get_device_id(LPCGUID pGuid)
213 if (IsEqualGUID(&DSDEVID_DefaultPlayback, pGuid))
214 return "DSDEVID_DefaultPlayback";
215 else if (IsEqualGUID(&DSDEVID_DefaultVoicePlayback, pGuid))
216 return "DSDEVID_DefaultVoicePlayback";
217 else if (IsEqualGUID(&DSDEVID_DefaultCapture, pGuid))
218 return "DSDEVID_DefaultCapture";
219 else if (IsEqualGUID(&DSDEVID_DefaultVoiceCapture, pGuid))
220 return "DSDEVID_DefaultVoiceCapture";
221 return debugstr_guid(pGuid);
224 /***************************************************************************
225 * GetDeviceID [DSOUND.9]
227 * Retrieves unique identifier of default device specified
230 * pGuidSrc [I] Address of device GUID.
231 * pGuidDest [O] Address to receive unique device GUID.
235 * Failure: DSERR_INVALIDPARAM
238 * pGuidSrc is a valid device GUID or DSDEVID_DefaultPlayback,
239 * DSDEVID_DefaultCapture, DSDEVID_DefaultVoicePlayback, or
240 * DSDEVID_DefaultVoiceCapture.
241 * Returns pGuidSrc if pGuidSrc is a valid device or the device
242 * GUID for the specified constants.
244 HRESULT WINAPI GetDeviceID(LPCGUID pGuidSrc, LPGUID pGuidDest)
246 TRACE("(%s,%p)\n", get_device_id(pGuidSrc),pGuidDest);
248 if ( pGuidSrc == NULL) {
249 WARN("invalid parameter: pGuidSrc == NULL\n");
250 return DSERR_INVALIDPARAM;
253 if ( pGuidDest == NULL ) {
254 WARN("invalid parameter: pGuidDest == NULL\n");
255 return DSERR_INVALIDPARAM;
258 if ( IsEqualGUID( &DSDEVID_DefaultPlayback, pGuidSrc ) ||
259 IsEqualGUID( &DSDEVID_DefaultVoicePlayback, pGuidSrc ) ) {
260 CopyMemory(pGuidDest, &DSOUND_renderer_guids[ds_default_playback], sizeof(GUID));
261 TRACE("returns %s\n", get_device_id(pGuidDest));
265 if ( IsEqualGUID( &DSDEVID_DefaultCapture, pGuidSrc ) ||
266 IsEqualGUID( &DSDEVID_DefaultVoiceCapture, pGuidSrc ) ) {
267 CopyMemory(pGuidDest, &DSOUND_capture_guids[ds_default_capture], sizeof(GUID));
268 TRACE("returns %s\n", get_device_id(pGuidDest));
272 CopyMemory(pGuidDest, pGuidSrc, sizeof(GUID));
273 TRACE("returns %s\n", get_device_id(pGuidDest));
279 /***************************************************************************
280 * DirectSoundEnumerateA [DSOUND.2]
282 * Enumerate all DirectSound drivers installed in the system
285 * lpDSEnumCallback [I] Address of callback function.
286 * lpContext [I] Address of user defined context passed to callback function.
290 * Failure: DSERR_INVALIDPARAM
292 HRESULT WINAPI DirectSoundEnumerateA(
293 LPDSENUMCALLBACKA lpDSEnumCallback,
301 TRACE("lpDSEnumCallback = %p, lpContext = %p\n",
302 lpDSEnumCallback, lpContext);
304 if (lpDSEnumCallback == NULL) {
305 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
306 return DSERR_INVALIDPARAM;
309 devs = waveOutGetNumDevs();
311 if (GetDeviceID(&DSDEVID_DefaultPlayback, &guid) == DS_OK) {
312 for (wod = 0; wod < devs; ++wod) {
313 if (IsEqualGUID( &guid, &DSOUND_renderer_guids[wod]) ) {
314 err = mmErr(waveOutMessage((HWAVEOUT)wod,DRV_QUERYDSOUNDDESC,(DWORD_PTR)&desc,0));
316 TRACE("calling lpDSEnumCallback(NULL,\"%s\",\"%s\",%p)\n",
317 "Primary Sound Driver",desc.szDrvname,lpContext);
318 if (lpDSEnumCallback(NULL, "Primary Sound Driver", desc.szDrvname, lpContext) == FALSE)
326 for (wod = 0; wod < devs; ++wod) {
327 err = mmErr(waveOutMessage((HWAVEOUT)wod,DRV_QUERYDSOUNDDESC,(DWORD_PTR)&desc,0));
329 TRACE("calling lpDSEnumCallback(%s,\"%s\",\"%s\",%p)\n",
330 debugstr_guid(&DSOUND_renderer_guids[wod]),desc.szDesc,desc.szDrvname,lpContext);
331 if (lpDSEnumCallback(&DSOUND_renderer_guids[wod], desc.szDesc, desc.szDrvname, lpContext) == FALSE)
338 /***************************************************************************
339 * DirectSoundEnumerateW [DSOUND.3]
341 * Enumerate all DirectSound drivers installed in the system
344 * lpDSEnumCallback [I] Address of callback function.
345 * lpContext [I] Address of user defined context passed to callback function.
349 * Failure: DSERR_INVALIDPARAM
351 HRESULT WINAPI DirectSoundEnumerateW(
352 LPDSENUMCALLBACKW lpDSEnumCallback,
359 WCHAR wDesc[MAXPNAMELEN];
360 WCHAR wName[MAXPNAMELEN];
362 TRACE("lpDSEnumCallback = %p, lpContext = %p\n",
363 lpDSEnumCallback, lpContext);
365 if (lpDSEnumCallback == NULL) {
366 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
367 return DSERR_INVALIDPARAM;
370 devs = waveOutGetNumDevs();
372 if (GetDeviceID(&DSDEVID_DefaultPlayback, &guid) == DS_OK) {
373 for (wod = 0; wod < devs; ++wod) {
374 if (IsEqualGUID( &guid, &DSOUND_renderer_guids[wod] ) ) {
375 err = mmErr(waveOutMessage((HWAVEOUT)wod,DRV_QUERYDSOUNDDESC,(DWORD_PTR)&desc,0));
377 TRACE("calling lpDSEnumCallback(NULL,\"%s\",\"%s\",%p)\n",
378 "Primary Sound Driver",desc.szDrvname,lpContext);
379 MultiByteToWideChar( CP_ACP, 0, "Primary Sound Driver", -1,
380 wDesc, sizeof(wDesc)/sizeof(WCHAR) );
381 MultiByteToWideChar( CP_ACP, 0, desc.szDrvname, -1,
382 wName, sizeof(wName)/sizeof(WCHAR) );
383 if (lpDSEnumCallback(NULL, wDesc, wName, lpContext) == FALSE)
391 for (wod = 0; wod < devs; ++wod) {
392 err = mmErr(waveOutMessage((HWAVEOUT)wod,DRV_QUERYDSOUNDDESC,(DWORD_PTR)&desc,0));
394 TRACE("calling lpDSEnumCallback(%s,\"%s\",\"%s\",%p)\n",
395 debugstr_guid(&DSOUND_renderer_guids[wod]),desc.szDesc,desc.szDrvname,lpContext);
396 MultiByteToWideChar( CP_ACP, 0, desc.szDesc, -1,
397 wDesc, sizeof(wDesc)/sizeof(WCHAR) );
398 MultiByteToWideChar( CP_ACP, 0, desc.szDrvname, -1,
399 wName, sizeof(wName)/sizeof(WCHAR) );
400 if (lpDSEnumCallback(&DSOUND_renderer_guids[wod], wDesc, wName, lpContext) == FALSE)
407 /*******************************************************************************
408 * DirectSound ClassFactory
411 typedef HRESULT (*FnCreateInstance)(REFIID riid, LPVOID *ppobj);
414 const IClassFactoryVtbl *lpVtbl;
417 FnCreateInstance pfnCreateInstance;
420 static HRESULT WINAPI
421 DSCF_QueryInterface(LPCLASSFACTORY iface, REFIID riid, LPVOID *ppobj)
423 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
424 TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
427 if (IsEqualIID(riid, &IID_IUnknown) ||
428 IsEqualIID(riid, &IID_IClassFactory))
431 IUnknown_AddRef(iface);
435 return E_NOINTERFACE;
438 static ULONG WINAPI DSCF_AddRef(LPCLASSFACTORY iface)
440 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
441 ULONG ref = InterlockedIncrement(&(This->ref));
442 TRACE("(%p) ref was %d\n", This, ref - 1);
446 static ULONG WINAPI DSCF_Release(LPCLASSFACTORY iface)
448 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
449 ULONG ref = InterlockedDecrement(&(This->ref));
450 TRACE("(%p) ref was %d\n", This, ref + 1);
451 /* static class, won't be freed */
455 static HRESULT WINAPI DSCF_CreateInstance(
456 LPCLASSFACTORY iface,
461 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
462 TRACE("(%p, %p, %s, %p)\n", This, pOuter, debugstr_guid(riid), ppobj);
465 return CLASS_E_NOAGGREGATION;
468 WARN("invalid parameter\n");
469 return DSERR_INVALIDPARAM;
472 return This->pfnCreateInstance(riid, ppobj);
475 static HRESULT WINAPI DSCF_LockServer(LPCLASSFACTORY iface, BOOL dolock)
477 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
478 FIXME("(%p, %d) stub!\n", This, dolock);
482 static const IClassFactoryVtbl DSCF_Vtbl = {
490 static IClassFactoryImpl DSOUND_CF[] = {
491 { &DSCF_Vtbl, 1, &CLSID_DirectSound, (FnCreateInstance)DSOUND_Create },
492 { &DSCF_Vtbl, 1, &CLSID_DirectSound8, (FnCreateInstance)DSOUND_Create8 },
493 { &DSCF_Vtbl, 1, &CLSID_DirectSoundCapture, (FnCreateInstance)DSOUND_CaptureCreate },
494 { &DSCF_Vtbl, 1, &CLSID_DirectSoundCapture8, (FnCreateInstance)DSOUND_CaptureCreate8 },
495 { &DSCF_Vtbl, 1, &CLSID_DirectSoundFullDuplex, (FnCreateInstance)DSOUND_FullDuplexCreate },
496 { &DSCF_Vtbl, 1, &CLSID_DirectSoundPrivate, (FnCreateInstance)IKsPrivatePropertySetImpl_Create },
497 { NULL, 0, NULL, NULL }
500 /*******************************************************************************
501 * DllGetClassObject [DSOUND.@]
502 * Retrieves class object from a DLL object
505 * Docs say returns STDAPI
508 * rclsid [I] CLSID for the class object
509 * riid [I] Reference to identifier of interface for class object
510 * ppv [O] Address of variable to receive interface pointer for riid
514 * Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
517 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
520 TRACE("(%s, %s, %p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
523 WARN("invalid parameter\n");
529 if (!IsEqualIID(riid, &IID_IClassFactory) &&
530 !IsEqualIID(riid, &IID_IUnknown)) {
531 WARN("no interface for %s\n", debugstr_guid(riid));
532 return E_NOINTERFACE;
535 while (NULL != DSOUND_CF[i].rclsid) {
536 if (IsEqualGUID(rclsid, DSOUND_CF[i].rclsid)) {
537 DSCF_AddRef((IClassFactory*) &DSOUND_CF[i]);
538 *ppv = &DSOUND_CF[i];
544 WARN("(%s, %s, %p): no class found.\n", debugstr_guid(rclsid),
545 debugstr_guid(riid), ppv);
546 return CLASS_E_CLASSNOTAVAILABLE;
550 /*******************************************************************************
551 * DllCanUnloadNow [DSOUND.4]
552 * Determines whether the DLL is in use.
558 HRESULT WINAPI DllCanUnloadNow(void)
560 FIXME("(void): stub\n");
564 #define INIT_GUID(guid, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \
565 guid.Data1 = l; guid.Data2 = w1; guid.Data3 = w2; \
566 guid.Data4[0] = b1; guid.Data4[1] = b2; guid.Data4[2] = b3; \
567 guid.Data4[3] = b4; guid.Data4[4] = b5; guid.Data4[5] = b6; \
568 guid.Data4[6] = b7; guid.Data4[7] = b8;
570 /***********************************************************************
571 * DllMain (DSOUND.init)
573 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpvReserved)
576 TRACE("(%p %d %p)\n", hInstDLL, fdwReason, lpvReserved);
579 case DLL_PROCESS_ATTACH:
580 TRACE("DLL_PROCESS_ATTACH\n");
581 for (i = 0; i < MAXWAVEDRIVERS; i++) {
582 DSOUND_renderer[i] = NULL;
583 DSOUND_capture[i] = NULL;
584 INIT_GUID(DSOUND_renderer_guids[i], 0xbd6dd71a, 0x3deb, 0x11d1, 0xb1, 0x71, 0x00, 0xc0, 0x4f, 0xc2, 0x00, 0x00 + i);
585 INIT_GUID(DSOUND_capture_guids[i], 0xbd6dd71b, 0x3deb, 0x11d1, 0xb1, 0x71, 0x00, 0xc0, 0x4f, 0xc2, 0x00, 0x00 + i);
588 case DLL_PROCESS_DETACH:
589 TRACE("DLL_PROCESS_DETACH\n");
591 case DLL_THREAD_ATTACH:
592 TRACE("DLL_THREAD_ATTACH\n");
594 case DLL_THREAD_DETACH:
595 TRACE("DLL_THREAD_DETACH\n");
598 TRACE("UNKNOWN REASON\n");