1 /* DirectSound COM interface
3 * Copyright 2009 Maarten Lankhorst
5 * Some code taken from the original dsound-openal implementation
6 * Copyright 2007-2009 Chris Robinson
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
28 #define NONAMELESSSTRUCT
29 #define NONAMELESSUNION
38 #include "wine/debug.h"
41 #include "dsound_private.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(dsound
);
51 #include "dsound_private.h"
53 #ifndef DSSPEAKER_7POINT1
54 #define DSSPEAKER_7POINT1 7
59 static DS8Impl
**devicelist
;
60 static UINT devicelistsize
;
62 static const IDirectSound8Vtbl DS8_Vtbl
;
64 static inline DS8Impl
*impl_from_IDirectSound8(IDirectSound8
*iface
)
66 return CONTAINING_RECORD(iface
, DS8Impl
, IDirectSound8_iface
);
69 HRESULT
DSOUND_Create(REFIID riid
, void **ds
)
73 if(IsEqualIID(riid
, &IID_IDirectSound8
))
75 hr
= DSOUND_Create8(riid
, ds
);
78 DS8Impl
*impl
= impl_from_IDirectSound8(*ds
);
84 static void DS8Impl_Destroy(DS8Impl
*This
);
86 static const WCHAR speakerconfigkey
[] = {
87 'S','Y','S','T','E','M','\\',
88 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
89 'C','o','n','t','r','o','l','\\',
90 'M','e','d','i','a','R','e','s','o','u','r','c','e','s','\\',
91 'D','i','r','e','c','t','S','o','u','n','d','\\',
92 'S','p','e','a','k','e','r',' ','C','o','n','f','i','g','u','r','a','t','i','o','n',0
95 static const WCHAR speakerconfig
[] = {
96 'S','p','e','a','k','e','r',' ','C','o','n','f','i','g','u','r','a','t','i','o','n',0
99 HRESULT
DSOUND_Create8(REFIID riid
, LPVOID
*ds
)
106 This
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*This
));
108 return E_OUTOFMEMORY
;
109 This
->IDirectSound8_iface
.lpVtbl
= (IDirectSound8Vtbl
*)&DS8_Vtbl
;
112 This
->speaker_config
= DSSPEAKER_COMBINED(DSSPEAKER_5POINT1
, DSSPEAKER_GEOMETRY_WIDE
);
114 if(RegOpenKeyExW(HKEY_LOCAL_MACHINE
, speakerconfigkey
, 0, KEY_READ
, ®key
) == ERROR_SUCCESS
)
116 DWORD type
, conf
, confsize
= sizeof(DWORD
);
118 if(RegQueryValueExW(regkey
, speakerconfig
, NULL
, &type
, (BYTE
*)&conf
, &confsize
) == ERROR_SUCCESS
)
120 if(type
== REG_DWORD
)
121 This
->speaker_config
= conf
;
126 /*RegGetValueW(HKEY_LOCAL_MACHINE, speakerconfigkey, speakerconfig, RRF_RT_REG_DWORD, NULL, &This->speaker_config, NULL);*/
128 hr
= IDirectSound8_QueryInterface(&This
->IDirectSound8_iface
, riid
, ds
);
130 DS8Impl_Destroy(This
);
135 EnterCriticalSection(&openal_crst
);
137 temp
= HeapReAlloc(GetProcessHeap(), 0, devicelist
, sizeof(*devicelist
)*(devicelistsize
+1));
139 temp
= HeapAlloc(GetProcessHeap(), 0, sizeof(*devicelist
)*(devicelistsize
+1));
143 devicelist
[devicelistsize
++] = This
;
145 LeaveCriticalSection(&openal_crst
);
150 static void DS8Impl_Destroy(DS8Impl
*This
)
154 EnterCriticalSection(&openal_crst
);
155 for(i
= 0;i
< devicelistsize
;i
++)
157 if(devicelist
[i
] == This
)
159 devicelist
[i
] = devicelist
[--devicelistsize
];
160 if(devicelistsize
== 0)
162 HeapFree(GetProcessHeap(), 0, devicelist
);
168 LeaveCriticalSection(&openal_crst
);
170 if(This
->deviceref
&& InterlockedDecrement(This
->deviceref
) == 0)
173 DS8Primary_Destroy(This
->primary
);
175 alcCloseDevice(This
->device
);
177 HeapFree(GetProcessHeap(), 0, This
->deviceref
);
181 EnterCriticalSection(&openal_crst
);
182 if(This
->primary
&& This
->primary
->parent
== This
)
184 /* If the primary is referencing this as its parent, update it to
185 * reference another handle for the device */
186 for(i
= 0;i
< devicelistsize
;i
++)
188 if(devicelist
[i
]->primary
== This
->primary
)
190 This
->primary
->parent
= devicelist
[i
];
195 LeaveCriticalSection(&openal_crst
);
198 HeapFree(GetProcessHeap(), 0, This
);
202 static HRESULT WINAPI
DS8_QueryInterface(IDirectSound8
*iface
, REFIID riid
, LPVOID
*ppv
)
204 DS8Impl
*This
= impl_from_IDirectSound8(iface
);
206 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
209 if(IsEqualIID(riid
, &IID_IUnknown
) ||
210 IsEqualIID(riid
, &IID_IDirectSound
))
211 *ppv
= &This
->IDirectSound8_iface
;
212 else if((IsEqualIID(riid
, &IID_IDirectSound8
)))
215 *ppv
= &This
->IDirectSound8_iface
;
218 FIXME("Unhandled GUID: %s\n", debugstr_guid(riid
));
222 IUnknown_AddRef((IUnknown
*)*ppv
);
226 return E_NOINTERFACE
;
229 static ULONG WINAPI
DS8_AddRef(IDirectSound8
*iface
)
231 DS8Impl
*This
= impl_from_IDirectSound8(iface
);
234 ref
= InterlockedIncrement(&This
->ref
);
235 TRACE("Reference count incremented to %d\n", ref
);
240 static ULONG WINAPI
DS8_Release(IDirectSound8
*iface
)
242 DS8Impl
*This
= impl_from_IDirectSound8(iface
);
245 ref
= InterlockedDecrement(&This
->ref
);
246 TRACE("Reference count decremented to %d\n", ref
);
248 DS8Impl_Destroy(This
);
253 static HRESULT WINAPI
DS8_CreateSoundBuffer(IDirectSound8
*iface
, LPCDSBUFFERDESC desc
, LPLPDIRECTSOUNDBUFFER buf
, IUnknown
*pUnkOuter
)
255 DS8Impl
*This
= impl_from_IDirectSound8(iface
);
258 TRACE("(%p)->(%p, %p, %p)\n", iface
, desc
, buf
, pUnkOuter
);
262 WARN("buf is null\n");
263 return DSERR_INVALIDPARAM
;
269 WARN("Aggregation isn't supported\n");
270 return DSERR_NOAGGREGATION
;
272 if(!desc
|| desc
->dwSize
< sizeof(DSBUFFERDESC1
))
274 WARN("Invalid buffer %p/%u\n", desc
, desc
?desc
->dwSize
:0);
275 return DSERR_INVALIDPARAM
;
278 TRACE("Requested buffer:\n"
281 " BufferBytes = %u\n",
282 (UINT
)desc
->dwSize
, (UINT
)desc
->dwFlags
,
283 (UINT
)desc
->dwBufferBytes
);
285 if(desc
->dwSize
>= sizeof(DSBUFFERDESC
))
287 if(!(desc
->dwFlags
&DSBCAPS_CTRL3D
))
289 if(!IsEqualGUID(&desc
->guid3DAlgorithm
, &GUID_NULL
))
291 WARN("Invalid 3D algorithm GUID specified for non-3D buffer: %s\n", debugstr_guid(&desc
->guid3DAlgorithm
));
292 return DSERR_INVALIDPARAM
;
296 TRACE("Requested 3D algorithm GUID: %s\n", debugstr_guid(&desc
->guid3DAlgorithm
));
299 /* OpenAL doesn't support playing with 3d and panning at same time.. */
300 if((desc
->dwFlags
&(DSBCAPS_CTRL3D
|DSBCAPS_CTRLPAN
)) == (DSBCAPS_CTRL3D
|DSBCAPS_CTRLPAN
))
303 ERR("Cannot create buffers with 3D and panning control\n");
305 WARN("Cannot create buffers with 3D and panning control\n");
306 return DSERR_INVALIDPARAM
;
309 EnterCriticalSection(&openal_crst
);
311 hr
= DSERR_UNINITIALIZED
;
312 else if((desc
->dwFlags
&DSBCAPS_PRIMARYBUFFER
))
314 IDirectSoundBuffer
*prim
= &This
->primary
->IDirectSoundBuffer_iface
;
317 if(IDirectSoundBuffer_AddRef(prim
) == 1)
319 hr
= IDirectSoundBuffer_Initialize(prim
, (IDirectSound
*)&This
->IDirectSound8_iface
, desc
);
322 IDirectSoundBuffer_Release(prim
);
332 hr
= DS8Buffer_Create(&dsb
, This
->primary
, NULL
);
335 hr
= IDirectSoundBuffer8_Initialize(&dsb
->IDirectSoundBuffer8_iface
, (IDirectSound
*)&This
->IDirectSound8_iface
, desc
);
337 IDirectSoundBuffer8_Release(&dsb
->IDirectSoundBuffer8_iface
);
340 dsb
->bufferlost
= (This
->prio_level
== DSSCL_WRITEPRIMARY
);
341 *buf
= (IDirectSoundBuffer
*)&dsb
->IDirectSoundBuffer8_iface
;
345 LeaveCriticalSection(&openal_crst
);
351 static HRESULT WINAPI
DS8_GetCaps(IDirectSound8
*iface
, LPDSCAPS caps
)
353 DS8Impl
*This
= impl_from_IDirectSound8(iface
);
356 TRACE("(%p)->(%p)\n", iface
, caps
);
358 EnterCriticalSection(&openal_crst
);
360 hr
= DSERR_UNINITIALIZED
;
361 else if(!caps
|| caps
->dwSize
< sizeof(*caps
))
362 hr
= DSERR_INVALIDPARAM
;
365 LONG count
= This
->primary
->max_sources
;
367 setALContext(This
->primary
->ctx
);
368 caps
->dwFlags
= DSCAPS_CONTINUOUSRATE
|
369 DSCAPS_PRIMARY16BIT
| DSCAPS_PRIMARYSTEREO
|
370 DSCAPS_PRIMARY8BIT
| DSCAPS_PRIMARYMONO
|
371 DSCAPS_SECONDARY16BIT
| DSCAPS_SECONDARY8BIT
|
372 DSCAPS_SECONDARYMONO
| DSCAPS_SECONDARYSTEREO
;
373 caps
->dwPrimaryBuffers
= 1;
374 caps
->dwMinSecondarySampleRate
= DSBFREQUENCY_MIN
;
375 caps
->dwMaxSecondarySampleRate
= DSBFREQUENCY_MAX
;
376 caps
->dwMaxHwMixingAllBuffers
=
377 caps
->dwMaxHwMixingStaticBuffers
=
378 caps
->dwMaxHwMixingStreamingBuffers
=
379 caps
->dwMaxHw3DAllBuffers
=
380 caps
->dwMaxHw3DStaticBuffers
=
381 caps
->dwMaxHw3DStreamingBuffers
= count
;
382 count
-= This
->primary
->nbuffers
;
385 ERR("How did the count drop below 0?\n");
388 caps
->dwFreeHwMixingAllBuffers
=
389 caps
->dwFreeHwMixingStaticBuffers
=
390 caps
->dwFreeHwMixingStreamingBuffers
=
391 caps
->dwFreeHw3DAllBuffers
=
392 caps
->dwFreeHw3DStaticBuffers
=
393 caps
->dwFreeHw3DStreamingBuffers
= count
;
394 caps
->dwTotalHwMemBytes
=
395 caps
->dwFreeHwMemBytes
= 64 * 1024 * 1024;
396 caps
->dwMaxContigFreeHwMemBytes
= caps
->dwFreeHwMemBytes
;
397 caps
->dwUnlockTransferRateHwBuffers
= 4096;
398 caps
->dwPlayCpuOverheadSwBuffers
= 0;
401 LeaveCriticalSection(&openal_crst
);
405 static HRESULT WINAPI
DS8_DuplicateSoundBuffer(IDirectSound8
*iface
, IDirectSoundBuffer
*in
, IDirectSoundBuffer
**out
)
407 DS8Impl
*This
= impl_from_IDirectSound8(iface
);
410 TRACE("(%p)->(%p, %p)\n", iface
, in
, out
);
412 EnterCriticalSection(&openal_crst
);
414 hr
= DSERR_UNINITIALIZED
;
416 hr
= DSERR_INVALIDPARAM
;
424 caps
.dwSize
= sizeof(caps
);
425 hr
= IDirectSoundBuffer_GetCaps(in
, &caps
);
426 if(SUCCEEDED(hr
) && (caps
.dwFlags
&DSBCAPS_PRIMARYBUFFER
))
428 WARN("Cannot duplicate buffer %p, which has DSBCAPS_PRIMARYBUFFER\n", in
);
429 hr
= DSERR_INVALIDPARAM
;
431 if(SUCCEEDED(hr
) && (caps
.dwFlags
&DSBCAPS_CTRLFX
))
433 WARN("Cannot duplicate buffer %p, which has DSBCAPS_CTRLFX\n", in
);
434 hr
= DSERR_INVALIDPARAM
;
437 hr
= DS8Buffer_Create(&buf
, This
->primary
, in
);
440 *out
= (IDirectSoundBuffer
*)&buf
->IDirectSoundBuffer8_iface
;
441 hr
= IDirectSoundBuffer_Initialize(*out
, NULL
, NULL
);
445 /* According to MSDN volume isn't copied */
446 if((caps
.dwFlags
&DSBCAPS_CTRLPAN
))
449 if(SUCCEEDED(IDirectSoundBuffer_GetPan(in
, &pan
)))
450 IDirectSoundBuffer_SetPan(*out
, pan
);
452 if((caps
.dwFlags
&DSBCAPS_CTRLFREQUENCY
))
455 if(SUCCEEDED(IDirectSoundBuffer_GetFrequency(in
, &freq
)))
456 IDirectSoundBuffer_SetFrequency(*out
, freq
);
458 if((caps
.dwFlags
&DSBCAPS_CTRL3D
))
460 IDirectSound3DBuffer
*buf3d
;
461 DS3DBUFFER DS3DBuffer
;
464 subhr
= IDirectSound_QueryInterface(in
, &IID_IDirectSound3DBuffer
, (void**)&buf3d
);
467 DS3DBuffer
.dwSize
= sizeof(DS3DBuffer
);
468 subhr
= IDirectSound3DBuffer_GetAllParameters(buf3d
, &DS3DBuffer
);
469 IDirectSound3DBuffer_Release(buf3d
);
472 subhr
= IDirectSoundBuffer_QueryInterface(*out
, &IID_IDirectSound3DBuffer
, (void**)&buf3d
);
475 subhr
= IDirectSound3DBuffer_SetAllParameters(buf3d
, &DS3DBuffer
, DS3D_IMMEDIATE
);
476 IDirectSound3DBuffer_Release(buf3d
);
483 IDirectSoundBuffer_Release(*out
);
489 LeaveCriticalSection(&openal_crst
);
493 static HRESULT WINAPI
DS8_SetCooperativeLevel(IDirectSound8
*iface
, HWND hwnd
, DWORD level
)
495 DS8Impl
*This
= impl_from_IDirectSound8(iface
);
498 TRACE("(%p)->(%p, %u)\n", iface
, hwnd
, level
);
500 EnterCriticalSection(&openal_crst
);
502 hr
= DSERR_UNINITIALIZED
;
503 else if(level
> DSSCL_WRITEPRIMARY
|| level
< DSSCL_NORMAL
)
505 else if(level
== DSSCL_WRITEPRIMARY
&& (This
->prio_level
!= DSSCL_WRITEPRIMARY
))
509 for(i
= 0; i
< This
->primary
->nbuffers
; ++i
)
511 DS8Buffer
*buf
= This
->primary
->buffers
[i
];
512 if(FAILED(IDirectSoundBuffer_GetStatus(&buf
->IDirectSoundBuffer8_iface
, &state
)) ||
513 (state
&DSBSTATUS_PLAYING
))
515 WARN("DSSCL_WRITEPRIMARY set with playing buffers!\n");
516 hr
= DSERR_INVALIDCALL
;
519 /* Mark buffer as lost */
522 if(This
->primary
->write_emu
)
524 ERR("Why was there a write_emu?\n");
526 IDirectSoundBuffer8_Release(This
->primary
->write_emu
);
527 This
->primary
->write_emu
= NULL
;
529 if(This
->primary
->flags
)
531 /* Primary has open references.. create write_emu */
535 memset(&desc
, 0, sizeof(desc
));
536 desc
.dwSize
= sizeof(desc
);
537 desc
.dwFlags
= DSBCAPS_LOCHARDWARE
| (This
->primary
->flags
&DSBCAPS_CTRLPAN
);
538 desc
.dwBufferBytes
= This
->primary
->buf_size
;
539 desc
.lpwfxFormat
= &This
->primary
->format
.Format
;
541 hr
= DS8Buffer_Create(&emu
, This
->primary
, NULL
);
544 This
->primary
->write_emu
= &emu
->IDirectSoundBuffer8_iface
;
545 hr
= IDirectSoundBuffer8_Initialize(This
->primary
->write_emu
, (IDirectSound
*)&This
->IDirectSound8_iface
, &desc
);
548 IDirectSoundBuffer8_Release(This
->primary
->write_emu
);
549 This
->primary
->write_emu
= NULL
;
554 else if(This
->prio_level
== DSSCL_WRITEPRIMARY
&& level
!= DSSCL_WRITEPRIMARY
&& This
->primary
->write_emu
)
556 TRACE("Nuking write_emu\n");
558 IDirectSoundBuffer8_Release(This
->primary
->write_emu
);
559 This
->primary
->write_emu
= NULL
;
562 This
->prio_level
= level
;
564 LeaveCriticalSection(&openal_crst
);
569 static HRESULT WINAPI
DS8_Compact(IDirectSound8
*iface
)
571 DS8Impl
*This
= impl_from_IDirectSound8(iface
);
574 TRACE("(%p)->()\n", iface
);
576 EnterCriticalSection(&openal_crst
);
578 hr
= DSERR_UNINITIALIZED
;
579 else if(This
->prio_level
< DSSCL_PRIORITY
)
580 hr
= DSERR_PRIOLEVELNEEDED
;
581 LeaveCriticalSection(&openal_crst
);
586 static HRESULT WINAPI
DS8_GetSpeakerConfig(IDirectSound8
*iface
, DWORD
*config
)
588 DS8Impl
*This
= impl_from_IDirectSound8(iface
);
591 TRACE("(%p)->(%p)\n", iface
, config
);
594 return DSERR_INVALIDPARAM
;
596 EnterCriticalSection(&openal_crst
);
598 hr
= DSERR_UNINITIALIZED
;
600 *config
= This
->speaker_config
;
601 LeaveCriticalSection(&openal_crst
);
606 static HRESULT WINAPI
DS8_SetSpeakerConfig(IDirectSound8
*iface
, DWORD config
)
608 DS8Impl
*This
= impl_from_IDirectSound8(iface
);
611 TRACE("(%p)->(0x%08x)\n", iface
, config
);
613 EnterCriticalSection(&openal_crst
);
615 hr
= DSERR_UNINITIALIZED
;
621 geo
= DSSPEAKER_GEOMETRY(config
);
622 speaker
= DSSPEAKER_CONFIG(config
);
624 hr
= DSERR_INVALIDPARAM
;
625 if(geo
&& (geo
< DSSPEAKER_GEOMETRY_MIN
|| geo
> DSSPEAKER_GEOMETRY_MAX
))
627 WARN("Invalid speaker angle %u\n", geo
);
630 if(speaker
< DSSPEAKER_HEADPHONE
|| speaker
> DSSPEAKER_7POINT1
)
632 WARN("Invalid speaker config %u\n", speaker
);
637 if(!RegCreateKeyExW(HKEY_LOCAL_MACHINE
, speakerconfigkey
, 0, NULL
, 0, KEY_WRITE
, NULL
, &key
, NULL
))
639 RegSetValueExW(key
, speakerconfig
, 0, REG_DWORD
, (const BYTE
*)&config
, sizeof(DWORD
));
640 This
->speaker_config
= config
;
646 LeaveCriticalSection(&openal_crst
);
651 static HRESULT WINAPI
DS8_Initialize(IDirectSound8
*iface
, const GUID
*devguid
)
653 DS8Impl
*This
= impl_from_IDirectSound8(iface
);
654 const ALCchar
*drv_name
;
658 TRACE("(%p)->(%s)\n", iface
, debugstr_guid(devguid
));
661 return DSERR_NODRIVER
;
664 devguid
= &DSDEVID_DefaultPlayback
;
666 EnterCriticalSection(&openal_crst
);
668 hr
= DSERR_ALREADYINITIALIZED
;
672 hr
= GetDeviceID(devguid
, &This
->guid
);
676 for(n
= 0;n
< devicelistsize
;n
++)
678 if(devicelist
[n
]->device
&& devicelist
[n
]->is_8
== This
->is_8
&&
679 IsEqualGUID(&devicelist
[n
]->guid
, &This
->guid
))
681 TRACE("Matched already open device %p\n", devicelist
[n
]);
683 This
->device
= devicelist
[n
]->device
;
684 This
->primary
= devicelist
[n
]->primary
;
685 This
->deviceref
= devicelist
[n
]->deviceref
;
686 InterlockedIncrement(This
->deviceref
);
695 hr
= DSERR_OUTOFMEMORY
;
696 if(!(This
->deviceref
=HeapAlloc(GetProcessHeap(), 0, sizeof(LONG
))))
698 This
->deviceref
[0] = 1;
702 if(!(drv_name
=DSOUND_getdevicestrings()) ||
703 memcmp(&This
->guid
, &DSOUND_renderer_guid
, sizeof(GUID
)-1) != 0)
705 WARN("No device found\n");
709 n
= This
->guid
.Data4
[7];
710 while(*drv_name
&& n
--)
711 drv_name
+= strlen(drv_name
) + 1;
714 WARN("No device string found\n");
718 This
->device
= alcOpenDevice(drv_name
);
722 WARN("Couldn't open device \"%s\"\n", drv_name
);
725 TRACE("Opened device: %s\n", alcGetString(This
->device
, ALC_DEVICE_SPECIFIER
));
727 hr
= DS8Primary_Create(&This
->primary
, This
);
730 alcCloseDevice(This
->device
);
735 LeaveCriticalSection(&openal_crst
);
740 /* I, Maarten Lankhorst, hereby declare this driver certified
741 * What this means.. ? An extra bit set
743 static HRESULT WINAPI
DS8_VerifyCertification(IDirectSound8
*iface
, DWORD
*certified
)
745 DS8Impl
*This
= impl_from_IDirectSound8(iface
);
748 TRACE("(%p)->(%p)\n", iface
, certified
);
751 return DSERR_INVALIDPARAM
;
753 EnterCriticalSection(&openal_crst
);
756 hr
= DSERR_UNINITIALIZED
;
758 *certified
= DS_CERTIFIED
;
759 LeaveCriticalSection(&openal_crst
);
764 static const IDirectSound8Vtbl DS8_Vtbl
=
769 DS8_CreateSoundBuffer
,
771 DS8_DuplicateSoundBuffer
,
772 DS8_SetCooperativeLevel
,
774 DS8_GetSpeakerConfig
,
775 DS8_SetSpeakerConfig
,
777 DS8_VerifyCertification