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 HRESULT
DSOUND_Create(REFIID riid
, LPVOID
*ds
)
68 if(IsEqualIID(riid
, &IID_IDirectSound8
))
70 hr
= DSOUND_Create8(riid
, ds
);
72 ((DS8Impl
*)*ds
)->is_8
= FALSE
;
76 static void DS8Impl_Destroy(DS8Impl
*This
);
78 static const WCHAR speakerconfigkey
[] = {
79 'S','Y','S','T','E','M','\\',
80 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
81 'C','o','n','t','r','o','l','\\',
82 'M','e','d','i','a','R','e','s','o','u','r','c','e','s','\\',
83 'D','i','r','e','c','t','S','o','u','n','d','\\',
84 'S','p','e','a','k','e','r',' ','C','o','n','f','i','g','u','r','a','t','i','o','n',0
87 static const WCHAR speakerconfig
[] = {
88 'S','p','e','a','k','e','r',' ','C','o','n','f','i','g','u','r','a','t','i','o','n',0
91 HRESULT
DSOUND_Create8(REFIID riid
, LPVOID
*ds
)
98 This
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*This
));
100 return E_OUTOFMEMORY
;
101 This
->IDirectSound8_iface
.lpVtbl
= (IDirectSound8Vtbl
*)&DS8_Vtbl
;
104 This
->speaker_config
= DSSPEAKER_COMBINED(DSSPEAKER_5POINT1
, DSSPEAKER_GEOMETRY_WIDE
);
106 if(RegOpenKeyExW(HKEY_LOCAL_MACHINE
, speakerconfigkey
, 0, KEY_READ
, ®key
) == ERROR_SUCCESS
)
108 DWORD type
, conf
, confsize
= sizeof(DWORD
);
110 if(RegQueryValueExW(regkey
, speakerconfig
, NULL
, &type
, (BYTE
*)&conf
, &confsize
) == ERROR_SUCCESS
)
112 if(type
== REG_DWORD
)
113 This
->speaker_config
= conf
;
118 /*RegGetValueW(HKEY_LOCAL_MACHINE, speakerconfigkey, speakerconfig, RRF_RT_REG_DWORD, NULL, &This->speaker_config, NULL);*/
120 hr
= IUnknown_QueryInterface(&This
->IDirectSound8_iface
, riid
, ds
);
122 DS8Impl_Destroy(This
);
127 EnterCriticalSection(&openal_crst
);
129 temp
= HeapReAlloc(GetProcessHeap(), 0, devicelist
, sizeof(*devicelist
)*(devicelistsize
+1));
131 temp
= HeapAlloc(GetProcessHeap(), 0, sizeof(*devicelist
)*(devicelistsize
+1));
135 devicelist
[devicelistsize
++] = This
;
137 LeaveCriticalSection(&openal_crst
);
142 static void DS8Impl_Destroy(DS8Impl
*This
)
146 EnterCriticalSection(&openal_crst
);
147 for(i
= 0;i
< devicelistsize
;i
++)
149 if(devicelist
[i
] == This
)
151 devicelist
[i
] = devicelist
[--devicelistsize
];
152 if(devicelistsize
== 0)
154 HeapFree(GetProcessHeap(), 0, devicelist
);
161 if(This
->deviceref
&& InterlockedDecrement(This
->deviceref
) == 0)
164 DS8Primary_Destroy(This
->primary
);
166 alcCloseDevice(This
->device
);
168 HeapFree(GetProcessHeap(), 0, This
->deviceref
);
170 else if(This
->primary
&& This
->primary
->parent
== This
)
172 /* If the primary is referencing this as its parent, update it to
173 * reference another handle for the device */
174 for(i
= 0;i
< devicelistsize
;i
++)
176 if(devicelist
[i
]->primary
== This
->primary
)
178 This
->primary
->parent
= devicelist
[i
];
183 LeaveCriticalSection(&openal_crst
);
185 HeapFree(GetProcessHeap(), 0, This
);
188 static inline DS8Impl
*impl_from_IDirectSound8(IDirectSound8
*iface
)
190 return CONTAINING_RECORD(iface
, DS8Impl
, IDirectSound8_iface
);
193 static HRESULT WINAPI
DS8_QueryInterface(IDirectSound8
*iface
, REFIID riid
, LPVOID
*ppv
)
195 DS8Impl
*This
= impl_from_IDirectSound8(iface
);
197 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
200 if(IsEqualIID(riid
, &IID_IUnknown
) ||
201 IsEqualIID(riid
, &IID_IDirectSound
))
202 *ppv
= &This
->IDirectSound8_iface
;
203 else if((IsEqualIID(riid
, &IID_IDirectSound8
)))
206 *ppv
= &This
->IDirectSound8_iface
;
209 FIXME("Unhandled GUID: %s\n", debugstr_guid(riid
));
213 IUnknown_AddRef((IUnknown
*)*ppv
);
217 return E_NOINTERFACE
;
220 static ULONG WINAPI
DS8_AddRef(IDirectSound8
*iface
)
222 DS8Impl
*This
= impl_from_IDirectSound8(iface
);
225 ref
= InterlockedIncrement(&This
->ref
);
226 TRACE("Reference count incremented to %d\n", ref
);
231 static ULONG WINAPI
DS8_Release(IDirectSound8
*iface
)
233 DS8Impl
*This
= impl_from_IDirectSound8(iface
);
236 ref
= InterlockedDecrement(&This
->ref
);
237 TRACE("Reference count decremented to %d\n", ref
);
239 DS8Impl_Destroy(This
);
244 static HRESULT WINAPI
DS8_CreateSoundBuffer(IDirectSound8
*iface
, LPCDSBUFFERDESC desc
, LPLPDIRECTSOUNDBUFFER buf
, IUnknown
*pUnkOuter
)
246 DS8Impl
*This
= impl_from_IDirectSound8(iface
);
249 TRACE("(%p)->(%p, %p, %p)\n", iface
, desc
, buf
, pUnkOuter
);
253 WARN("buf is null\n");
254 return DSERR_INVALIDPARAM
;
260 WARN("Aggregation isn't supported\n");
261 return DSERR_NOAGGREGATION
;
263 if(!desc
|| desc
->dwSize
< sizeof(DSBUFFERDESC1
))
265 WARN("Invalid buffer %p/%u\n", desc
, desc
?desc
->dwSize
:0);
266 return DSERR_INVALIDPARAM
;
269 TRACE("Requested buffer:\n"
272 " BufferBytes = %u\n",
273 (UINT
)desc
->dwSize
, (UINT
)desc
->dwFlags
,
274 (UINT
)desc
->dwBufferBytes
);
276 if(desc
->dwSize
>= sizeof(DSBUFFERDESC
))
278 if(!(desc
->dwFlags
&DSBCAPS_CTRL3D
))
280 if(!IsEqualGUID(&desc
->guid3DAlgorithm
, &GUID_NULL
))
282 WARN("Invalid 3D algorithm GUID specified for non-3D buffer: %s\n", debugstr_guid(&desc
->guid3DAlgorithm
));
283 return DSERR_INVALIDPARAM
;
287 TRACE("Requested 3D algorithm GUID: %s\n", debugstr_guid(&desc
->guid3DAlgorithm
));
290 /* OpenAL doesn't support playing with 3d and panning at same time.. */
291 if((desc
->dwFlags
&(DSBCAPS_CTRL3D
|DSBCAPS_CTRLPAN
)) == (DSBCAPS_CTRL3D
|DSBCAPS_CTRLPAN
))
294 ERR("Cannot create buffers with 3D and panning control\n");
296 WARN("Cannot create buffers with 3D and panning control\n");
297 return DSERR_INVALIDPARAM
;
300 EnterCriticalSection(&openal_crst
);
302 hr
= DSERR_UNINITIALIZED
;
303 else if((desc
->dwFlags
&DSBCAPS_PRIMARYBUFFER
))
305 IDirectSoundBuffer
*prim
= &This
->primary
->IDirectSoundBuffer_iface
;
308 if(IDirectSoundBuffer_AddRef(prim
) == 1)
310 hr
= IDirectSoundBuffer_Initialize(prim
, (IDirectSound
*)&This
->IDirectSound8_iface
, desc
);
313 IDirectSoundBuffer_Release(prim
);
323 hr
= DS8Buffer_Create(&dsb
, This
->primary
, NULL
);
326 hr
= IDirectSoundBuffer8_Initialize(&dsb
->IDirectSoundBuffer8_iface
, (IDirectSound
*)&This
->IDirectSound8_iface
, desc
);
328 IDirectSoundBuffer8_Release(&dsb
->IDirectSoundBuffer8_iface
);
331 dsb
->bufferlost
= (This
->prio_level
== DSSCL_WRITEPRIMARY
);
332 *buf
= (IDirectSoundBuffer
*)&dsb
->IDirectSoundBuffer8_iface
;
336 LeaveCriticalSection(&openal_crst
);
342 static HRESULT WINAPI
DS8_GetCaps(IDirectSound8
*iface
, LPDSCAPS caps
)
344 DS8Impl
*This
= impl_from_IDirectSound8(iface
);
347 TRACE("(%p)->(%p)\n", iface
, caps
);
349 EnterCriticalSection(&openal_crst
);
351 hr
= DSERR_UNINITIALIZED
;
352 else if(!caps
|| caps
->dwSize
< sizeof(*caps
))
353 hr
= DSERR_INVALIDPARAM
;
356 LONG count
= This
->primary
->max_sources
;
358 setALContext(This
->primary
->ctx
);
359 caps
->dwFlags
= DSCAPS_CONTINUOUSRATE
|
360 DSCAPS_PRIMARY16BIT
| DSCAPS_PRIMARYSTEREO
|
361 DSCAPS_PRIMARY8BIT
| DSCAPS_PRIMARYMONO
|
362 DSCAPS_SECONDARY16BIT
| DSCAPS_SECONDARY8BIT
|
363 DSCAPS_SECONDARYMONO
| DSCAPS_SECONDARYSTEREO
;
364 caps
->dwPrimaryBuffers
= 1;
365 caps
->dwMinSecondarySampleRate
= DSBFREQUENCY_MIN
;
366 caps
->dwMaxSecondarySampleRate
= DSBFREQUENCY_MAX
;
367 caps
->dwMaxHwMixingAllBuffers
=
368 caps
->dwMaxHwMixingStaticBuffers
=
369 caps
->dwMaxHwMixingStreamingBuffers
=
370 caps
->dwMaxHw3DAllBuffers
=
371 caps
->dwMaxHw3DStaticBuffers
=
372 caps
->dwMaxHw3DStreamingBuffers
= count
;
373 count
-= This
->primary
->nbuffers
;
376 ERR("How did the count drop below 0?\n");
379 caps
->dwFreeHwMixingAllBuffers
=
380 caps
->dwFreeHwMixingStaticBuffers
=
381 caps
->dwFreeHwMixingStreamingBuffers
=
382 caps
->dwFreeHw3DAllBuffers
=
383 caps
->dwFreeHw3DStaticBuffers
=
384 caps
->dwFreeHw3DStreamingBuffers
= count
;
385 caps
->dwTotalHwMemBytes
=
386 caps
->dwFreeHwMemBytes
= 64 * 1024 * 1024;
387 caps
->dwMaxContigFreeHwMemBytes
= caps
->dwFreeHwMemBytes
;
388 caps
->dwUnlockTransferRateHwBuffers
= 4096;
389 caps
->dwPlayCpuOverheadSwBuffers
= 0;
392 LeaveCriticalSection(&openal_crst
);
396 static HRESULT WINAPI
DS8_DuplicateSoundBuffer(IDirectSound8
*iface
, IDirectSoundBuffer
*in
, IDirectSoundBuffer
**out
)
398 DS8Impl
*This
= impl_from_IDirectSound8(iface
);
401 TRACE("(%p)->(%p, %p)\n", iface
, in
, out
);
403 EnterCriticalSection(&openal_crst
);
405 hr
= DSERR_UNINITIALIZED
;
407 hr
= DSERR_INVALIDPARAM
;
416 for(i
= 0; i
< This
->primary
->nbuffers
; ++i
)
418 if(This
->primary
->buffers
[i
] == (DS8Buffer
*)in
)
421 if(i
== This
->primary
->nbuffers
)
423 hr
= DSERR_INVALIDPARAM
;
424 WARN("Buffer %p not found\n", in
);
428 caps
.dwSize
= sizeof(caps
);
429 hr
= IDirectSoundBuffer_GetCaps(in
, &caps
);
430 if(SUCCEEDED(hr
) && (caps
.dwFlags
&DSBCAPS_CTRLFX
))
432 WARN("Cannot duplicate buffer %p, which has DSBCAPS_CTRLFX\n", in
);
433 hr
= DSERR_INVALIDPARAM
;
436 hr
= DS8Buffer_Create(&buf
, This
->primary
, (DS8Buffer
*)in
);
439 *out
= (IDirectSoundBuffer
*)&buf
->IDirectSoundBuffer8_iface
;
440 hr
= IDirectSoundBuffer_Initialize(*out
, NULL
, NULL
);
444 DWORD savelost
= ((DS8Buffer
*)in
)->bufferlost
;
445 ((DS8Buffer
*)in
)->bufferlost
= 0;
446 /* According to MSDN volume isn't copied
448 if((caps
.dwFlags
&DSBCAPS_CTRLPAN
))
451 if(SUCCEEDED(IDirectSoundBuffer_GetPan(in
, &pan
)))
452 IDirectSoundBuffer_SetPan(*out
, pan
);
454 if((caps
.dwFlags
&DSBCAPS_CTRLFREQUENCY
))
457 if(SUCCEEDED(IDirectSoundBuffer_GetFrequency(in
, &freq
)))
458 IDirectSoundBuffer_SetFrequency(*out
, freq
);
460 if((caps
.dwFlags
&DSBCAPS_CTRL3D
))
462 IDirectSound3DBuffer
*buf3d
;
463 DS3DBUFFER DS3DBuffer
;
466 subhr
= IDirectSound_QueryInterface(in
, &IID_IDirectSound3DBuffer
, (void**)&buf3d
);
469 DS3DBuffer
.dwSize
= sizeof(DS3DBuffer
);
470 subhr
= IDirectSound3DBuffer_GetAllParameters(buf3d
, &DS3DBuffer
);
471 IDirectSound3DBuffer_Release(buf3d
);
474 subhr
= IDirectSoundBuffer_QueryInterface(*out
, &IID_IDirectSound3DBuffer
, (void**)&buf3d
);
477 subhr
= IDirectSound3DBuffer_SetAllParameters(buf3d
, &DS3DBuffer
, DS3D_IMMEDIATE
);
478 IDirectSound3DBuffer_Release(buf3d
);
481 ((DS8Buffer
*)in
)->bufferlost
= buf
->bufferlost
= savelost
;
486 IDirectSoundBuffer_Release(*out
);
492 LeaveCriticalSection(&openal_crst
);
496 static HRESULT WINAPI
DS8_SetCooperativeLevel(IDirectSound8
*iface
, HWND hwnd
, DWORD level
)
498 DS8Impl
*This
= impl_from_IDirectSound8(iface
);
501 TRACE("(%p)->(%p, %u)\n", iface
, hwnd
, level
);
503 EnterCriticalSection(&openal_crst
);
505 hr
= DSERR_UNINITIALIZED
;
506 else if(level
> DSSCL_WRITEPRIMARY
|| level
< DSSCL_NORMAL
)
508 else if(level
== DSSCL_WRITEPRIMARY
&& (This
->prio_level
!= DSSCL_WRITEPRIMARY
))
512 for(i
= 0; i
< This
->primary
->nbuffers
; ++i
)
514 DS8Buffer
*buf
= This
->primary
->buffers
[i
];
515 if(FAILED(IDirectSoundBuffer_GetStatus(&buf
->IDirectSoundBuffer8_iface
, &state
)) ||
516 (state
&DSBSTATUS_PLAYING
))
518 WARN("DSSCL_WRITEPRIMARY set with playing buffers!\n");
519 hr
= DSERR_INVALIDCALL
;
522 /* Mark buffer as lost */
525 if(This
->primary
->write_emu
)
527 ERR("Why was there a write_emu?\n");
529 IDirectSoundBuffer8_Release(This
->primary
->write_emu
);
530 This
->primary
->write_emu
= NULL
;
532 if(This
->primary
->flags
)
534 /* Primary has open references.. create write_emu */
538 memset(&desc
, 0, sizeof(desc
));
539 desc
.dwSize
= sizeof(desc
);
540 desc
.dwFlags
= DSBCAPS_LOCHARDWARE
| (This
->primary
->flags
&DSBCAPS_CTRLPAN
);
541 desc
.dwBufferBytes
= This
->primary
->buf_size
;
542 desc
.lpwfxFormat
= &This
->primary
->format
.Format
;
544 hr
= DS8Buffer_Create(&emu
, This
->primary
, NULL
);
547 This
->primary
->write_emu
= &emu
->IDirectSoundBuffer8_iface
;
548 hr
= IDirectSoundBuffer8_Initialize(This
->primary
->write_emu
, (IDirectSound
*)&This
->IDirectSound8_iface
, &desc
);
551 IDirectSoundBuffer8_Release(This
->primary
->write_emu
);
552 This
->primary
->write_emu
= NULL
;
557 else if(This
->prio_level
== DSSCL_WRITEPRIMARY
&& level
!= DSSCL_WRITEPRIMARY
&& This
->primary
->write_emu
)
559 TRACE("Nuking write_emu\n");
561 IDirectSoundBuffer8_Release(This
->primary
->write_emu
);
562 This
->primary
->write_emu
= NULL
;
565 This
->prio_level
= level
;
567 LeaveCriticalSection(&openal_crst
);
572 static HRESULT WINAPI
DS8_Compact(IDirectSound8
*iface
)
574 DS8Impl
*This
= impl_from_IDirectSound8(iface
);
577 TRACE("(%p)->()\n", iface
);
579 EnterCriticalSection(&openal_crst
);
581 hr
= DSERR_UNINITIALIZED
;
582 else if(This
->prio_level
< DSSCL_PRIORITY
)
583 hr
= DSERR_PRIOLEVELNEEDED
;
584 LeaveCriticalSection(&openal_crst
);
589 static HRESULT WINAPI
DS8_GetSpeakerConfig(IDirectSound8
*iface
, DWORD
*config
)
591 DS8Impl
*This
= impl_from_IDirectSound8(iface
);
594 TRACE("(%p)->(%p)\n", iface
, config
);
597 return DSERR_INVALIDPARAM
;
599 EnterCriticalSection(&openal_crst
);
601 hr
= DSERR_UNINITIALIZED
;
603 *config
= This
->speaker_config
;
604 LeaveCriticalSection(&openal_crst
);
609 static HRESULT WINAPI
DS8_SetSpeakerConfig(IDirectSound8
*iface
, DWORD config
)
611 DS8Impl
*This
= impl_from_IDirectSound8(iface
);
614 TRACE("(%p)->(0x%08x)\n", iface
, config
);
616 EnterCriticalSection(&openal_crst
);
618 hr
= DSERR_UNINITIALIZED
;
624 geo
= DSSPEAKER_GEOMETRY(config
);
625 speaker
= DSSPEAKER_CONFIG(config
);
627 hr
= DSERR_INVALIDPARAM
;
628 if(geo
&& (geo
< DSSPEAKER_GEOMETRY_MIN
|| geo
> DSSPEAKER_GEOMETRY_MAX
))
630 WARN("Invalid speaker angle %u\n", geo
);
633 if(speaker
< DSSPEAKER_HEADPHONE
|| speaker
> DSSPEAKER_7POINT1
)
635 WARN("Invalid speaker config %u\n", speaker
);
640 if(!RegCreateKeyExW(HKEY_LOCAL_MACHINE
, speakerconfigkey
, 0, NULL
, 0, KEY_WRITE
, NULL
, &key
, NULL
))
642 RegSetValueExW(key
, speakerconfig
, 0, REG_DWORD
, (const BYTE
*)&config
, sizeof(DWORD
));
643 This
->speaker_config
= config
;
649 LeaveCriticalSection(&openal_crst
);
654 static HRESULT WINAPI
DS8_Initialize(IDirectSound8
*iface
, const GUID
*devguid
)
656 DS8Impl
*This
= impl_from_IDirectSound8(iface
);
657 const ALCchar
*drv_name
;
661 TRACE("(%p)->(%s)\n", iface
, debugstr_guid(devguid
));
664 return DSERR_NODRIVER
;
667 devguid
= &DSDEVID_DefaultPlayback
;
669 EnterCriticalSection(&openal_crst
);
671 hr
= DSERR_ALREADYINITIALIZED
;
675 hr
= GetDeviceID(devguid
, &This
->guid
);
679 for(n
= 0;n
< devicelistsize
;n
++)
681 if(devicelist
[n
]->device
&& devicelist
[n
]->is_8
== This
->is_8
&&
682 IsEqualGUID(&devicelist
[n
]->guid
, &This
->guid
))
684 TRACE("Matched already open device %p\n", devicelist
[n
]);
686 This
->device
= devicelist
[n
]->device
;
687 This
->primary
= devicelist
[n
]->primary
;
688 This
->deviceref
= devicelist
[n
]->deviceref
;
689 InterlockedIncrement(This
->deviceref
);
698 hr
= DSERR_OUTOFMEMORY
;
699 if(!(This
->deviceref
=HeapAlloc(GetProcessHeap(), 0, sizeof(LONG
))))
701 This
->deviceref
[0] = 1;
705 if(!(drv_name
=DSOUND_getdevicestrings()) ||
706 memcmp(&This
->guid
, &DSOUND_renderer_guid
, sizeof(GUID
)-1) != 0)
708 WARN("No device found\n");
712 n
= This
->guid
.Data4
[7];
713 while(*drv_name
&& n
--)
714 drv_name
+= strlen(drv_name
) + 1;
717 WARN("No device string found\n");
721 This
->device
= alcOpenDevice(drv_name
);
725 WARN("Couldn't open device \"%s\"\n", drv_name
);
728 TRACE("Opened device: %s\n", alcGetString(This
->device
, ALC_DEVICE_SPECIFIER
));
730 hr
= DS8Primary_Create(&This
->primary
, This
);
733 alcCloseDevice(This
->device
);
738 LeaveCriticalSection(&openal_crst
);
743 /* I, Maarten Lankhorst, hereby declare this driver certified
744 * What this means.. ? An extra bit set
746 static HRESULT WINAPI
DS8_VerifyCertification(IDirectSound8
*iface
, DWORD
*certified
)
748 DS8Impl
*This
= impl_from_IDirectSound8(iface
);
751 TRACE("(%p)->(%p)\n", iface
, certified
);
754 return DSERR_INVALIDPARAM
;
756 EnterCriticalSection(&openal_crst
);
759 hr
= DSERR_UNINITIALIZED
;
761 *certified
= DS_CERTIFIED
;
762 LeaveCriticalSection(&openal_crst
);
767 static const IDirectSound8Vtbl DS8_Vtbl
=
772 DS8_CreateSoundBuffer
,
774 DS8_DuplicateSoundBuffer
,
775 DS8_SetCooperativeLevel
,
777 DS8_GetSpeakerConfig
,
778 DS8_SetSpeakerConfig
,
780 DS8_VerifyCertification