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
32 #include "dsound_private.h"
34 #ifndef DSSPEAKER_7POINT1
35 #define DSSPEAKER_7POINT1 7
39 static DWORD CALLBACK
DSShare_thread(void *dwUser
)
41 DeviceShare
*share
= (DeviceShare
*)dwUser
;
42 BYTE
*scratch_mem
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, 2048);
45 SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL
);
47 TRACE("Shared device (%p) message loop start\n", share
);
48 while(WaitForSingleObject(share
->timer_evt
, INFINITE
) == WAIT_OBJECT_0
&& !share
->quit_now
)
50 EnterCriticalSection(&share
->crst
);
51 setALContext(share
->ctx
);
53 for(i
= 0;i
< share
->nprimaries
;++i
)
55 DSPrimary_triggernots(share
->primaries
[i
]);
56 if(!HAS_EXTENSION(share
, SOFTX_MAP_BUFFER
))
57 DSPrimary_streamfeeder(share
->primaries
[i
], scratch_mem
);
61 LeaveCriticalSection(&share
->crst
);
63 TRACE("Shared device (%p) message loop quit\n", share
);
65 HeapFree(GetProcessHeap(), 0, scratch_mem
);
71 TlsSetValue(TlsThreadPtr
, NULL
);
77 static void CALLBACK
DSShare_timer(void *arg
, BOOLEAN unused
)
80 SetEvent((HANDLE
)arg
);
83 static void DSShare_starttimer(DeviceShare
*share
)
87 if(share
->queue_timer
)
90 triggertime
= 1000 / share
->refresh
* 2 / 3;
91 TRACE("Calling timer every %lu ms for %d refreshes per second\n",
92 triggertime
, share
->refresh
);
94 CreateTimerQueueTimer(&share
->queue_timer
, NULL
, DSShare_timer
, share
->timer_evt
,
95 triggertime
, triggertime
, WT_EXECUTEINTIMERTHREAD
);
100 static DeviceShare
**sharelist
;
101 static UINT sharelistsize
;
103 static void DSShare_Destroy(DeviceShare
*share
)
107 EnterCriticalSection(&openal_crst
);
108 for(i
= 0;i
< sharelistsize
;i
++)
110 if(sharelist
[i
] == share
)
112 sharelist
[i
] = sharelist
[--sharelistsize
];
113 if(sharelistsize
== 0)
115 HeapFree(GetProcessHeap(), 0, sharelist
);
121 LeaveCriticalSection(&openal_crst
);
123 if(share
->queue_timer
)
124 DeleteTimerQueueTimer(NULL
, share
->queue_timer
, INVALID_HANDLE_VALUE
);
125 share
->queue_timer
= NULL
;
127 if(share
->thread_hdl
)
129 InterlockedExchange(&share
->quit_now
, TRUE
);
130 SetEvent(share
->timer_evt
);
132 if(WaitForSingleObject(share
->thread_hdl
, 1000) != WAIT_OBJECT_0
)
133 ERR("Thread wait timed out\n");
135 CloseHandle(share
->thread_hdl
);
136 share
->thread_hdl
= NULL
;
140 CloseHandle(share
->timer_evt
);
141 share
->timer_evt
= NULL
;
145 /* Calling setALContext is not appropriate here, since we *have* to
146 * unset the context before destroying it
148 EnterCriticalSection(&openal_crst
);
149 set_context(share
->ctx
);
151 if(share
->sources
.maxhw_alloc
+ share
->sources
.maxsw_alloc
)
152 alDeleteSources(share
->sources
.maxhw_alloc
+share
->sources
.maxsw_alloc
,
154 share
->sources
.maxhw_alloc
= share
->sources
.maxsw_alloc
= 0;
156 if(share
->auxslot
[3])
157 alDeleteAuxiliaryEffectSlots(4, share
->auxslot
);
158 else if(share
->auxslot
[2])
159 alDeleteAuxiliaryEffectSlots(3, share
->auxslot
);
160 else if(share
->auxslot
[1])
161 alDeleteAuxiliaryEffectSlots(2, share
->auxslot
);
162 else if(share
->auxslot
[0])
163 alDeleteAuxiliaryEffectSlots(1, share
->auxslot
);
164 share
->auxslot
[0] = share
->auxslot
[1] =
165 share
->auxslot
[2] = share
->auxslot
[3] = 0;
168 TlsSetValue(TlsThreadPtr
, NULL
);
169 alcDestroyContext(share
->ctx
);
171 LeaveCriticalSection(&openal_crst
);
175 alcCloseDevice(share
->device
);
176 share
->device
= NULL
;
178 DeleteCriticalSection(&share
->crst
);
180 HeapFree(GetProcessHeap(), 0, share
->primaries
);
181 HeapFree(GetProcessHeap(), 0, share
);
183 TRACE("Closed shared device %p\n", share
);
186 static HRESULT
DSShare_Create(REFIID guid
, DeviceShare
**out
)
188 static const struct {
189 const char extname
[64];
191 } extensions
[MAX_EXTENSIONS
] = {
192 { "ALC_EXT_EFX", EXT_EFX
},
193 { "AL_EXT_FLOAT32", EXT_FLOAT32
},
194 { "AL_EXT_MCFORMATS", EXT_MCFORMATS
},
195 { "AL_SOFT_deferred_updates", SOFT_DEFERRED_UPDATES
},
196 { "AL_SOFT_source_spatialize", SOFT_SOURCE_SPATIALIZE
},
197 { "AL_SOFTX_filter_gain_ex", SOFTX_FILTER_GAIN_EX
},
198 { "AL_SOFTX_map_buffer", SOFTX_MAP_BUFFER
},
200 OLECHAR
*guid_str
= NULL
;
209 share
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*share
));
210 if(!share
) return DSERR_OUTOFMEMORY
;
212 share
->refresh
= FAKE_REFRESH_COUNT
;
213 share
->speaker_config
= DSSPEAKER_7POINT1_SURROUND
;
215 TRACE("Creating shared device %p\n", share
);
217 hr
= get_mmdevice(eRender
, guid
, &mmdev
);
220 IPropertyStore
*store
;
222 hr
= IMMDevice_OpenPropertyStore(mmdev
, STGM_READ
, &store
);
224 WARN("IMMDevice_OpenPropertyStore failed: %08lx\n", hr
);
227 ULONG phys_speakers
= 0;
230 PropVariantInit(&pv
);
232 hr
= IPropertyStore_GetValue(store
, &PKEY_AudioEndpoint_PhysicalSpeakers
, &pv
);
234 WARN("IPropertyStore_GetValue failed: %08lx\n", hr
);
235 else if(pv
.vt
!= VT_UI4
)
236 WARN("PKEY_AudioEndpoint_PhysicalSpeakers is not a ULONG: 0x%04x\n", pv
.vt
);
239 phys_speakers
= pv
.ulVal
;
241 #define BIT_MATCH(v, b) (((v)&(b)) == (b))
242 if(BIT_MATCH(phys_speakers
, KSAUDIO_SPEAKER_7POINT1
))
243 share
->speaker_config
= DSSPEAKER_7POINT1
;
244 else if(BIT_MATCH(phys_speakers
, KSAUDIO_SPEAKER_7POINT1_SURROUND
))
245 share
->speaker_config
= DSSPEAKER_7POINT1_SURROUND
;
246 else if(BIT_MATCH(phys_speakers
, KSAUDIO_SPEAKER_5POINT1
))
247 share
->speaker_config
= DSSPEAKER_5POINT1_BACK
;
248 else if(BIT_MATCH(phys_speakers
, KSAUDIO_SPEAKER_5POINT1_SURROUND
))
249 share
->speaker_config
= DSSPEAKER_5POINT1_SURROUND
;
250 else if(BIT_MATCH(phys_speakers
, KSAUDIO_SPEAKER_QUAD
))
251 share
->speaker_config
= DSSPEAKER_QUAD
;
252 else if(BIT_MATCH(phys_speakers
, KSAUDIO_SPEAKER_STEREO
))
253 share
->speaker_config
= DSSPEAKER_COMBINED(DSSPEAKER_STEREO
, DSSPEAKER_GEOMETRY_WIDE
);
254 else if(BIT_MATCH(phys_speakers
, KSAUDIO_SPEAKER_MONO
))
255 share
->speaker_config
= DSSPEAKER_MONO
;
257 FIXME("Unhandled physical speaker layout: 0x%08lx\n", phys_speakers
);
261 /* If the device has a stereo layout, check the formfactor to see
262 * if it's really headphones/headset.
264 if(DSSPEAKER_CONFIG(share
->speaker_config
) == DSSPEAKER_STEREO
)
266 hr
= IPropertyStore_GetValue(store
, &PKEY_AudioEndpoint_FormFactor
, &pv
);
268 WARN("IPropertyStore_GetValue failed: %08lx\n", hr
);
269 else if(pv
.vt
!= VT_UI4
)
270 WARN("PKEY_AudioEndpoint_FormFactor is not a ULONG: 0x%04x\n", pv
.vt
);
273 if(pv
.ulVal
== Headphones
|| pv
.ulVal
== Headset
)
274 share
->speaker_config
= DSSPEAKER_HEADPHONE
;
278 TRACE("Got speaker config %d:%d from physical speakers 0x%08lx\n",
279 DSSPEAKER_GEOMETRY(share
->speaker_config
),
280 DSSPEAKER_CONFIG(share
->speaker_config
), phys_speakers
);
282 PropVariantClear(&pv
);
283 IPropertyStore_Release(store
);
286 IMMDevice_Release(mmdev
);
290 InitializeCriticalSection(&share
->crst
);
292 hr
= StringFromCLSID(guid
, &guid_str
);
295 ERR("Failed to convert GUID to string\n");
298 WideCharToMultiByte(CP_UTF8
, 0, guid_str
, -1, drv_name
, sizeof(drv_name
), NULL
, NULL
);
299 drv_name
[sizeof(drv_name
)-1] = 0;
300 CoTaskMemFree(guid_str
);
304 share
->device
= alcOpenDevice(drv_name
);
308 WARN("Couldn't open device \"%s\"\n", drv_name
);
311 TRACE("Opened AL device: %s\n",
312 alcIsExtensionPresent(share
->device
, "ALC_ENUMERATE_ALL_EXT") ?
313 alcGetString(share
->device
, ALC_ALL_DEVICES_SPECIFIER
) :
314 alcGetString(share
->device
, ALC_DEVICE_SPECIFIER
));
317 attrs
[i
++] = ALC_MONO_SOURCES
;
318 attrs
[i
++] = MAX_SOURCES
;
319 attrs
[i
++] = ALC_STEREO_SOURCES
;
321 if(alcIsExtensionPresent(share
->device
, "ALC_EXT_EFX"))
323 attrs
[i
++] = ALC_MAX_AUXILIARY_SENDS
;
324 attrs
[i
++] = EAX_MAX_ACTIVE_FXSLOTS
;
327 share
->ctx
= alcCreateContext(share
->device
, attrs
);
330 ALCenum err
= alcGetError(share
->device
);
331 ERR("Could not create context (0x%x)!\n", err
);
337 setALContext(share
->ctx
);
338 alcGetIntegerv(share
->device
, ALC_REFRESH
, 1, &share
->refresh
);
339 checkALCError(share
->device
);
341 for(i
= 0;i
< MAX_EXTENSIONS
;i
++)
343 if((strncmp(extensions
[i
].extname
, "ALC", 3) == 0) ?
344 alcIsExtensionPresent(share
->device
, extensions
[i
].extname
) :
345 alIsExtensionPresent(extensions
[i
].extname
))
347 TRACE("Found %s\n", extensions
[i
].extname
);
348 BITFIELD_SET(share
->Exts
, extensions
[i
].extenum
);
352 share
->sources
.maxhw_alloc
= 0;
353 while(share
->sources
.maxhw_alloc
< MAX_SOURCES
)
355 alGenSources(1, &share
->sources
.ids
[share
->sources
.maxhw_alloc
]);
356 if(alGetError() != AL_NO_ERROR
) break;
357 share
->sources
.maxhw_alloc
++;
360 share
->num_slots
= 0;
361 if(HAS_EXTENSION(share
, EXT_EFX
))
363 alcGetIntegerv(share
->device
, ALC_MAX_AUXILIARY_SENDS
, 1, &share
->num_sends
);
364 checkALCError(share
->device
);
365 if(share
->num_sends
> EAX_MAX_ACTIVE_FXSLOTS
)
366 share
->num_sends
= EAX_MAX_ACTIVE_FXSLOTS
;
367 TRACE("Got %d auxiliary source send%s\n", share
->num_sends
, (share
->num_sends
==1)?"":"s");
369 for(i
= 0;i
< EAX_MAX_FXSLOTS
;++i
)
371 alGenAuxiliaryEffectSlots(1, &share
->auxslot
[i
]);
372 if(alGetError() != AL_NO_ERROR
) break;
375 TRACE("Allocated %d auxiliary effect slot%s\n", i
, (i
==1)?"":"s");
376 while(i
< EAX_MAX_FXSLOTS
)
377 share
->auxslot
[i
++] = 0;
382 if(share
->sources
.maxhw_alloc
< 128)
384 ERR("Could only allocate %lu sources (minimum 128 required)\n",
385 share
->sources
.maxhw_alloc
);
389 if(share
->sources
.maxhw_alloc
> MAX_HWBUFFERS
)
391 share
->sources
.maxsw_alloc
= share
->sources
.maxhw_alloc
- MAX_HWBUFFERS
;
392 share
->sources
.maxhw_alloc
= MAX_HWBUFFERS
;
394 else if(share
->sources
.maxhw_alloc
> MAX_HWBUFFERS
/2)
396 share
->sources
.maxsw_alloc
= share
->sources
.maxhw_alloc
- MAX_HWBUFFERS
/2;
397 share
->sources
.maxhw_alloc
= MAX_HWBUFFERS
/2;
401 share
->sources
.maxsw_alloc
= share
->sources
.maxhw_alloc
- MAX_HWBUFFERS
/4;
402 share
->sources
.maxhw_alloc
= MAX_HWBUFFERS
/4;
404 share
->sources
.availhw_num
= share
->sources
.maxhw_alloc
;
405 share
->sources
.availsw_num
= share
->sources
.maxsw_alloc
;
406 TRACE("Allocated %lu hardware sources and %lu software sources\n",
407 share
->sources
.maxhw_alloc
, share
->sources
.maxsw_alloc
);
410 temp
= HeapReAlloc(GetProcessHeap(), 0, sharelist
, sizeof(*sharelist
)*(sharelistsize
+1));
412 temp
= HeapAlloc(GetProcessHeap(), 0, sizeof(*sharelist
)*(sharelistsize
+1));
416 sharelist
[sharelistsize
++] = share
;
421 share
->quit_now
= FALSE
;
422 share
->timer_evt
= CreateEventA(NULL
, FALSE
, FALSE
, NULL
);
423 if(!share
->timer_evt
) goto fail
;
425 share
->queue_timer
= NULL
;
427 share
->thread_hdl
= CreateThread(NULL
, 0, DSShare_thread
, share
, 0, &share
->thread_id
);
428 if(!share
->thread_hdl
) goto fail
;
430 DSShare_starttimer(share
);
436 DSShare_Destroy(share
);
440 static ULONG
DSShare_AddRef(DeviceShare
*share
)
442 ULONG ref
= InterlockedIncrement(&share
->ref
);
446 static ULONG
DSShare_Release(DeviceShare
*share
)
448 ULONG ref
= InterlockedDecrement(&share
->ref
);
449 if(ref
== 0) DSShare_Destroy(share
);
454 static IDirectSound8Vtbl DS8_Vtbl
;
455 static IDirectSoundVtbl DS_Vtbl
;
456 static IUnknownVtbl DS8_Unknown_Vtbl
;
458 static HRESULT
DSDevice_Create(BOOL is8
, REFIID riid
, LPVOID
*ds
);
459 static void DSDevice_Destroy(DSDevice
*This
);
460 static HRESULT
DSDevice_GetInterface(DSDevice
*This
, REFIID riid
, LPVOID
*ppv
);
462 /*******************************************************************************
465 static inline DSDevice
*impl_from_IUnknown(IUnknown
*iface
)
467 return CONTAINING_RECORD(iface
, DSDevice
, IUnknown_iface
);
470 static HRESULT WINAPI
DSDevice_IUnknown_QueryInterface(IUnknown
*iface
, REFIID riid
, void **ppobj
)
472 DSDevice
*This
= impl_from_IUnknown(iface
);
473 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppobj
);
474 return DSDevice_GetInterface(This
, riid
, ppobj
);
477 static ULONG WINAPI
DSDevice_IUnknown_AddRef(IUnknown
*iface
)
479 DSDevice
*This
= impl_from_IUnknown(iface
);
482 InterlockedIncrement(&(This
->ref
));
483 ref
= InterlockedIncrement(&(This
->unkref
));
484 TRACE("(%p) ref %lu\n", iface
, ref
);
489 static ULONG WINAPI
DSDevice_IUnknown_Release(IUnknown
*iface
)
491 DSDevice
*This
= impl_from_IUnknown(iface
);
492 ULONG ref
= InterlockedDecrement(&(This
->unkref
));
493 TRACE("(%p) ref %lu\n", iface
, ref
);
494 if(InterlockedDecrement(&(This
->ref
)) == 0)
495 DSDevice_Destroy(This
);
499 static IUnknownVtbl DS8_Unknown_Vtbl
= {
500 DSDevice_IUnknown_QueryInterface
,
501 DSDevice_IUnknown_AddRef
,
502 DSDevice_IUnknown_Release
506 static inline DSDevice
*impl_from_IDirectSound8(IDirectSound8
*iface
)
508 return CONTAINING_RECORD(iface
, DSDevice
, IDirectSound8_iface
);
511 static inline DSDevice
*impl_from_IDirectSound(IDirectSound
*iface
)
513 return CONTAINING_RECORD(iface
, DSDevice
, IDirectSound_iface
);
517 HRESULT
DSOUND_Create(REFIID riid
, void **ds
)
518 { return DSDevice_Create(FALSE
, riid
, ds
); }
520 HRESULT
DSOUND_Create8(REFIID riid
, LPVOID
*ds
)
521 { return DSDevice_Create(TRUE
, riid
, ds
); }
523 static HRESULT
DSDevice_Create(BOOL is8
, REFIID riid
, LPVOID
*ds
)
529 This
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*This
));
530 if(!This
) return DSERR_OUTOFMEMORY
;
532 TRACE("Creating device instance %p\n", This
);
533 This
->IDirectSound8_iface
.lpVtbl
= &DS8_Vtbl
;
534 This
->IDirectSound_iface
.lpVtbl
= &DS_Vtbl
;
535 This
->IUnknown_iface
.lpVtbl
= &DS8_Unknown_Vtbl
;
539 hr
= DSDevice_GetInterface(This
, riid
, ds
);
540 if(FAILED(hr
)) DSDevice_Destroy(This
);
544 static void DSDevice_Destroy(DSDevice
*This
)
546 DeviceShare
*share
= This
->share
;
548 TRACE("Destroying device instance %p\n", This
);
553 EnterCriticalSection(&share
->crst
);
555 for(i
= 0;i
< share
->nprimaries
;++i
)
557 if(share
->primaries
[i
] == &This
->primary
)
559 share
->nprimaries
-= 1;
560 share
->primaries
[i
] = share
->primaries
[share
->nprimaries
];
565 LeaveCriticalSection(&share
->crst
);
568 DSPrimary_Clear(&This
->primary
);
570 DSShare_Release(This
->share
);
573 HeapFree(GetProcessHeap(), 0, This
);
576 static HRESULT
DSDevice_GetInterface(DSDevice
*This
, REFIID riid
, LPVOID
*ppv
)
579 if(IsEqualIID(riid
, &IID_IUnknown
))
580 *ppv
= &This
->IUnknown_iface
;
581 else if(IsEqualIID(riid
, &IID_IDirectSound8
))
584 *ppv
= &This
->IDirectSound8_iface
;
586 else if(IsEqualIID(riid
, &IID_IDirectSound
))
587 *ppv
= &This
->IDirectSound_iface
;
589 FIXME("Unhandled GUID: %s\n", debugstr_guid(riid
));
593 IUnknown_AddRef((IUnknown
*)*ppv
);
597 return E_NOINTERFACE
;
601 static HRESULT WINAPI
DS8_QueryInterface(IDirectSound8
*iface
, REFIID riid
, LPVOID
*ppv
)
603 DSDevice
*This
= impl_from_IDirectSound8(iface
);
604 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
605 return DSDevice_GetInterface(This
, riid
, ppv
);
608 static ULONG WINAPI
DS8_AddRef(IDirectSound8
*iface
)
610 DSDevice
*This
= impl_from_IDirectSound8(iface
);
613 InterlockedIncrement(&This
->ref
);
614 ref
= InterlockedIncrement(&This
->dsref
);
615 TRACE("(%p) ref %lu\n", iface
, ref
);
620 static ULONG WINAPI
DS8_Release(IDirectSound8
*iface
)
622 DSDevice
*This
= impl_from_IDirectSound8(iface
);
625 ref
= InterlockedDecrement(&This
->dsref
);
626 TRACE("(%p) ref %lu\n", iface
, ref
);
627 if(InterlockedDecrement(&This
->ref
) == 0)
628 DSDevice_Destroy(This
);
633 static HRESULT WINAPI
DS8_CreateSoundBuffer(IDirectSound8
*iface
, LPCDSBUFFERDESC desc
, LPLPDIRECTSOUNDBUFFER buf
, IUnknown
*pUnkOuter
)
635 DSDevice
*This
= impl_from_IDirectSound8(iface
);
638 TRACE("(%p)->(%p, %p, %p)\n", iface
, desc
, buf
, pUnkOuter
);
642 WARN("buf is null\n");
643 return DSERR_INVALIDPARAM
;
649 WARN("Aggregation isn't supported\n");
650 return DSERR_NOAGGREGATION
;
652 if(!desc
|| desc
->dwSize
< sizeof(DSBUFFERDESC1
))
654 WARN("Invalid buffer %p/%lu\n", desc
, desc
?desc
->dwSize
:0);
655 return DSERR_INVALIDPARAM
;
660 WARN("Device not initialized\n");
661 return DSERR_UNINITIALIZED
;
664 TRACE("Requested buffer:\n"
667 " BufferBytes = %lu\n",
668 desc
->dwSize
, desc
->dwFlags
, desc
->dwBufferBytes
);
670 if(desc
->dwSize
>= sizeof(DSBUFFERDESC
))
672 if(!(desc
->dwFlags
&DSBCAPS_CTRL3D
))
674 if(!IsEqualGUID(&desc
->guid3DAlgorithm
, &GUID_NULL
))
676 WARN("Invalid 3D algorithm GUID specified for non-3D buffer: %s\n", debugstr_guid(&desc
->guid3DAlgorithm
));
677 return DSERR_INVALIDPARAM
;
681 TRACE("Requested 3D algorithm GUID: %s\n", debugstr_guid(&desc
->guid3DAlgorithm
));
684 /* OpenAL doesn't support playing with 3d and panning at same time.. */
685 if((desc
->dwFlags
&(DSBCAPS_CTRL3D
|DSBCAPS_CTRLPAN
)) == (DSBCAPS_CTRL3D
|DSBCAPS_CTRLPAN
))
691 FIXME("Buffers with 3D and panning control ignore panning\n");
695 WARN("Cannot create buffers with 3D and panning control\n");
696 return DSERR_INVALIDPARAM
;
700 EnterCriticalSection(&This
->share
->crst
);
701 if((desc
->dwFlags
&DSBCAPS_PRIMARYBUFFER
))
703 IDirectSoundBuffer
*prim
= &This
->primary
.IDirectSoundBuffer_iface
;
706 if(IDirectSoundBuffer_AddRef(prim
) == 1)
708 hr
= DSPrimary_Initialize(prim
, &This
->IDirectSound_iface
, desc
);
711 IDirectSoundBuffer_Release(prim
);
721 hr
= DSBuffer_Create(&dsb
, &This
->primary
, NULL
);
724 hr
= DSBuffer_Initialize(&dsb
->IDirectSoundBuffer8_iface
, &This
->IDirectSound_iface
, desc
);
727 dsb
->bufferlost
= (This
->prio_level
== DSSCL_WRITEPRIMARY
);
728 hr
= DSBuffer_GetInterface(dsb
, &IID_IDirectSoundBuffer
, (void**)buf
);
731 DSBuffer_Destroy(dsb
);
734 LeaveCriticalSection(&This
->share
->crst
);
736 TRACE("%08lx\n", hr
);
740 static HRESULT WINAPI
DS8_GetCaps(IDirectSound8
*iface
, LPDSCAPS caps
)
742 DSDevice
*This
= impl_from_IDirectSound8(iface
);
743 struct DSBufferGroup
*bufgroup
, *endgroup
;
746 TRACE("(%p)->(%p)\n", iface
, caps
);
750 WARN("Device not initialized\n");
751 return DSERR_UNINITIALIZED
;
754 if(!caps
|| caps
->dwSize
< sizeof(*caps
))
756 WARN("Invalid DSCAPS (%p, %lu)\n", caps
, (caps
?caps
->dwSize
:0));
757 return DSERR_INVALIDPARAM
;
760 EnterCriticalSection(&This
->share
->crst
);
762 free_bufs
= This
->share
->sources
.maxhw_alloc
;
763 bufgroup
= This
->primary
.BufferGroups
;
764 endgroup
= bufgroup
+ This
->primary
.NumBufferGroups
;
765 for(;free_bufs
&& bufgroup
!= endgroup
;++bufgroup
)
767 DWORD64 usemask
= ~bufgroup
->FreeBuffers
;
770 int idx
= CTZ64(usemask
);
771 DSBuffer
*buf
= bufgroup
->Buffers
+ idx
;
772 usemask
&= ~(U64(1) << idx
);
774 if(buf
->loc_status
== DSBSTATUS_LOCHARDWARE
)
782 caps
->dwFlags
= DSCAPS_CONTINUOUSRATE
| DSCAPS_CERTIFIED
|
783 DSCAPS_PRIMARY16BIT
| DSCAPS_PRIMARYSTEREO
|
784 DSCAPS_PRIMARY8BIT
| DSCAPS_PRIMARYMONO
|
785 DSCAPS_SECONDARY16BIT
| DSCAPS_SECONDARY8BIT
|
786 DSCAPS_SECONDARYMONO
| DSCAPS_SECONDARYSTEREO
;
787 caps
->dwPrimaryBuffers
= 1;
788 caps
->dwMinSecondarySampleRate
= DSBFREQUENCY_MIN
;
789 caps
->dwMaxSecondarySampleRate
= DSBFREQUENCY_MAX
;
790 caps
->dwMaxHwMixingAllBuffers
=
791 caps
->dwMaxHwMixingStaticBuffers
=
792 caps
->dwMaxHwMixingStreamingBuffers
=
793 caps
->dwMaxHw3DAllBuffers
=
794 caps
->dwMaxHw3DStaticBuffers
=
795 caps
->dwMaxHw3DStreamingBuffers
= This
->share
->sources
.maxhw_alloc
;
796 caps
->dwFreeHwMixingAllBuffers
=
797 caps
->dwFreeHwMixingStaticBuffers
=
798 caps
->dwFreeHwMixingStreamingBuffers
=
799 caps
->dwFreeHw3DAllBuffers
=
800 caps
->dwFreeHw3DStaticBuffers
=
801 caps
->dwFreeHw3DStreamingBuffers
= free_bufs
;
802 caps
->dwTotalHwMemBytes
=
803 caps
->dwFreeHwMemBytes
= 64 * 1024 * 1024;
804 caps
->dwMaxContigFreeHwMemBytes
= caps
->dwFreeHwMemBytes
;
805 caps
->dwUnlockTransferRateHwBuffers
= 4096;
806 caps
->dwPlayCpuOverheadSwBuffers
= 0;
808 LeaveCriticalSection(&This
->share
->crst
);
812 static HRESULT WINAPI
DS8_DuplicateSoundBuffer(IDirectSound8
*iface
, IDirectSoundBuffer
*in
, IDirectSoundBuffer
**out
)
814 DSDevice
*This
= impl_from_IDirectSound8(iface
);
815 DSBuffer
*buf
= NULL
;
819 TRACE("(%p)->(%p, %p)\n", iface
, in
, out
);
823 WARN("Device not initialized\n");
824 return DSERR_UNINITIALIZED
;
829 WARN("Invalid pointer: in = %p, out = %p\n", in
, out
);
830 return DSERR_INVALIDPARAM
;
834 caps
.dwSize
= sizeof(caps
);
835 hr
= IDirectSoundBuffer_GetCaps(in
, &caps
);
836 if(SUCCEEDED(hr
) && (caps
.dwFlags
&DSBCAPS_PRIMARYBUFFER
))
838 WARN("Cannot duplicate buffer %p, which has DSBCAPS_PRIMARYBUFFER\n", in
);
839 hr
= DSERR_INVALIDPARAM
;
841 if(SUCCEEDED(hr
) && (caps
.dwFlags
&DSBCAPS_CTRLFX
))
843 WARN("Cannot duplicate buffer %p, which has DSBCAPS_CTRLFX\n", in
);
844 hr
= DSERR_INVALIDPARAM
;
847 hr
= DSBuffer_Create(&buf
, &This
->primary
, in
);
850 hr
= DSBuffer_Initialize(&buf
->IDirectSoundBuffer8_iface
, NULL
, NULL
);
852 hr
= DSBuffer_GetInterface(buf
, &IID_IDirectSoundBuffer
, (void**)out
);
854 DSBuffer_Destroy(buf
);
860 static HRESULT WINAPI
DS8_SetCooperativeLevel(IDirectSound8
*iface
, HWND hwnd
, DWORD level
)
862 DSDevice
*This
= impl_from_IDirectSound8(iface
);
865 TRACE("(%p)->(%p, %lu)\n", iface
, hwnd
, level
);
869 WARN("Device not initialized\n");
870 return DSERR_UNINITIALIZED
;
873 if(level
> DSSCL_WRITEPRIMARY
|| level
< DSSCL_NORMAL
)
875 WARN("Invalid coop level: %lu\n", level
);
876 return DSERR_INVALIDPARAM
;
879 EnterCriticalSection(&This
->share
->crst
);
880 if(level
== DSSCL_WRITEPRIMARY
&& (This
->prio_level
!= DSSCL_WRITEPRIMARY
))
882 struct DSBufferGroup
*bufgroup
= This
->primary
.BufferGroups
;
885 if(This
->primary
.write_emu
)
887 ERR("Why was there a write_emu?\n");
889 IDirectSoundBuffer_Release(This
->primary
.write_emu
);
890 This
->primary
.write_emu
= NULL
;
893 for(i
= 0;i
< This
->primary
.NumBufferGroups
;++i
)
895 DWORD64 usemask
= ~bufgroup
[i
].FreeBuffers
;
898 int idx
= CTZ64(usemask
);
899 DSBuffer
*buf
= bufgroup
[i
].Buffers
+ idx
;
900 usemask
&= ~(U64(1) << idx
);
902 if(FAILED(DSBuffer_GetStatus(&buf
->IDirectSoundBuffer8_iface
, &state
)) ||
903 (state
&DSBSTATUS_PLAYING
))
905 WARN("DSSCL_WRITEPRIMARY set with playing buffers!\n");
906 hr
= DSERR_INVALIDCALL
;
909 /* Mark buffer as lost */
914 if(This
->primary
.flags
)
916 /* Primary has open references.. create write_emu */
920 memset(&desc
, 0, sizeof(desc
));
921 desc
.dwSize
= sizeof(desc
);
922 desc
.dwFlags
= DSBCAPS_LOCHARDWARE
| (This
->primary
.flags
&DSBCAPS_CTRLPAN
);
923 desc
.dwBufferBytes
= This
->primary
.buf_size
;
924 desc
.lpwfxFormat
= &This
->primary
.format
.Format
;
926 hr
= DSBuffer_Create(&emu
, &This
->primary
, NULL
);
929 hr
= DSBuffer_Initialize(&emu
->IDirectSoundBuffer8_iface
,
930 &This
->IDirectSound_iface
, &desc
);
932 hr
= DSBuffer_GetInterface(emu
, &IID_IDirectSoundBuffer
,
933 (void**)&This
->primary
.write_emu
);
935 DSBuffer_Destroy(emu
);
939 else if(This
->prio_level
== DSSCL_WRITEPRIMARY
&& level
!= DSSCL_WRITEPRIMARY
)
942 TRACE("Nuking write_emu\n");
943 if(This
->primary
.write_emu
)
944 IDirectSoundBuffer_Release(This
->primary
.write_emu
);
945 This
->primary
.write_emu
= NULL
;
948 This
->prio_level
= level
;
950 LeaveCriticalSection(&This
->share
->crst
);
955 static HRESULT WINAPI
DS8_Compact(IDirectSound8
*iface
)
957 DSDevice
*This
= impl_from_IDirectSound8(iface
);
960 TRACE("(%p)->()\n", iface
);
964 WARN("Device not initialized\n");
965 return DSERR_UNINITIALIZED
;
968 EnterCriticalSection(&This
->share
->crst
);
969 if(This
->prio_level
< DSSCL_PRIORITY
)
971 WARN("Coop level not high enough (%lu)\n", This
->prio_level
);
972 hr
= DSERR_PRIOLEVELNEEDED
;
974 LeaveCriticalSection(&This
->share
->crst
);
979 static HRESULT WINAPI
DS8_GetSpeakerConfig(IDirectSound8
*iface
, DWORD
*config
)
981 DSDevice
*This
= impl_from_IDirectSound8(iface
);
983 TRACE("(%p)->(%p)\n", iface
, config
);
986 return DSERR_INVALIDPARAM
;
991 WARN("Device not initialized\n");
992 return DSERR_UNINITIALIZED
;
995 *config
= This
->share
->speaker_config
;
1000 static HRESULT WINAPI
DS8_SetSpeakerConfig(IDirectSound8
*iface
, DWORD config
)
1002 DSDevice
*This
= impl_from_IDirectSound8(iface
);
1005 TRACE("(%p)->(0x%08lx)\n", iface
, config
);
1009 WARN("Device not initialized\n");
1010 return DSERR_UNINITIALIZED
;
1013 geo
= DSSPEAKER_GEOMETRY(config
);
1014 speaker
= DSSPEAKER_CONFIG(config
);
1016 if(geo
&& (geo
< DSSPEAKER_GEOMETRY_MIN
|| geo
> DSSPEAKER_GEOMETRY_MAX
))
1018 WARN("Invalid speaker angle %lu\n", geo
);
1019 return DSERR_INVALIDPARAM
;
1021 if(speaker
< DSSPEAKER_HEADPHONE
|| speaker
> DSSPEAKER_7POINT1
)
1023 WARN("Invalid speaker config %lu\n", speaker
);
1024 return DSERR_INVALIDPARAM
;
1027 /* No-op on Vista+. */
1031 static HRESULT WINAPI
DS8_Initialize(IDirectSound8
*iface
, const GUID
*devguid
)
1033 DSDevice
*This
= impl_from_IDirectSound8(iface
);
1038 TRACE("(%p)->(%s)\n", iface
, debugstr_guid(devguid
));
1041 return DSERR_NODRIVER
;
1045 WARN("Device already initialized\n");
1046 return DSERR_ALREADYINITIALIZED
;
1049 if(!devguid
|| IsEqualGUID(devguid
, &GUID_NULL
))
1050 devguid
= &DSDEVID_DefaultPlayback
;
1051 else if(IsEqualGUID(devguid
, &DSDEVID_DefaultCapture
) ||
1052 IsEqualGUID(devguid
, &DSDEVID_DefaultVoiceCapture
))
1053 return DSERR_NODRIVER
;
1055 hr
= DSOAL_GetDeviceID(devguid
, &guid
);
1056 if(FAILED(hr
)) return hr
;
1058 EnterCriticalSection(&openal_crst
);
1060 TRACE("Searching shared devices for %s\n", debugstr_guid(&guid
));
1061 for(n
= 0;n
< sharelistsize
;n
++)
1063 if(IsEqualGUID(&sharelist
[n
]->guid
, &guid
))
1065 TRACE("Matched shared device %p\n", sharelist
[n
]);
1067 DSShare_AddRef(sharelist
[n
]);
1068 This
->share
= sharelist
[n
];
1074 hr
= DSShare_Create(&guid
, &This
->share
);
1077 This
->device
= This
->share
->device
;
1078 hr
= DSPrimary_PreInit(&This
->primary
, This
);
1083 DeviceShare
*share
= This
->share
;
1086 EnterCriticalSection(&share
->crst
);
1088 prims
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1089 (share
->nprimaries
+1) * sizeof(*prims
));
1091 hr
= DSERR_OUTOFMEMORY
;
1095 for(i
= 0;i
< share
->nprimaries
;++i
)
1096 prims
[i
] = share
->primaries
[i
];
1097 prims
[i
] = &This
->primary
;
1099 HeapFree(GetProcessHeap(), 0, share
->primaries
);
1100 share
->primaries
= prims
;
1101 share
->nprimaries
+= 1;
1104 LeaveCriticalSection(&share
->crst
);
1110 DSShare_Release(This
->share
);
1114 LeaveCriticalSection(&openal_crst
);
1118 /* I, Maarten Lankhorst, hereby declare this driver certified
1119 * What this means.. ? An extra bit set
1121 static HRESULT WINAPI
DS8_VerifyCertification(IDirectSound8
*iface
, DWORD
*certified
)
1123 DSDevice
*This
= impl_from_IDirectSound8(iface
);
1125 TRACE("(%p)->(%p)\n", iface
, certified
);
1128 return DSERR_INVALIDPARAM
;
1133 WARN("Device not initialized\n");
1134 return DSERR_UNINITIALIZED
;
1137 *certified
= DS_CERTIFIED
;
1142 static IDirectSound8Vtbl DS8_Vtbl
= {
1146 DS8_CreateSoundBuffer
,
1148 DS8_DuplicateSoundBuffer
,
1149 DS8_SetCooperativeLevel
,
1151 DS8_GetSpeakerConfig
,
1152 DS8_SetSpeakerConfig
,
1154 DS8_VerifyCertification
1158 static HRESULT WINAPI
DS_QueryInterface(IDirectSound
*iface
, REFIID riid
, LPVOID
*ppv
)
1160 DSDevice
*This
= impl_from_IDirectSound(iface
);
1161 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
1162 return DSDevice_GetInterface(This
, riid
, ppv
);
1165 static ULONG WINAPI
DS_AddRef(IDirectSound
*iface
)
1167 DSDevice
*This
= impl_from_IDirectSound(iface
);
1168 return DS8_AddRef(&This
->IDirectSound8_iface
);
1171 static ULONG WINAPI
DS_Release(IDirectSound
*iface
)
1173 DSDevice
*This
= impl_from_IDirectSound(iface
);
1174 return DS8_Release(&This
->IDirectSound8_iface
);
1177 static HRESULT WINAPI
DS_CreateSoundBuffer(IDirectSound
*iface
, LPCDSBUFFERDESC desc
, LPLPDIRECTSOUNDBUFFER buf
, IUnknown
*pUnkOuter
)
1179 DSDevice
*This
= impl_from_IDirectSound(iface
);
1180 return DS8_CreateSoundBuffer(&This
->IDirectSound8_iface
, desc
, buf
, pUnkOuter
);
1183 static HRESULT WINAPI
DS_GetCaps(IDirectSound
*iface
, LPDSCAPS caps
)
1185 DSDevice
*This
= impl_from_IDirectSound(iface
);
1186 return DS8_GetCaps(&This
->IDirectSound8_iface
, caps
);
1188 static HRESULT WINAPI
DS_DuplicateSoundBuffer(IDirectSound
*iface
, IDirectSoundBuffer
*in
, IDirectSoundBuffer
**out
)
1190 DSDevice
*This
= impl_from_IDirectSound(iface
);
1191 return DS8_DuplicateSoundBuffer(&This
->IDirectSound8_iface
, in
, out
);
1194 static HRESULT WINAPI
DS_SetCooperativeLevel(IDirectSound
*iface
, HWND hwnd
, DWORD level
)
1196 DSDevice
*This
= impl_from_IDirectSound(iface
);
1197 return DS8_SetCooperativeLevel(&This
->IDirectSound8_iface
, hwnd
, level
);
1200 static HRESULT WINAPI
DS_Compact(IDirectSound
*iface
)
1202 DSDevice
*This
= impl_from_IDirectSound(iface
);
1203 return DS8_Compact(&This
->IDirectSound8_iface
);
1206 static HRESULT WINAPI
DS_GetSpeakerConfig(IDirectSound
*iface
, DWORD
*config
)
1208 DSDevice
*This
= impl_from_IDirectSound(iface
);
1209 return DS8_GetSpeakerConfig(&This
->IDirectSound8_iface
, config
);
1212 static HRESULT WINAPI
DS_SetSpeakerConfig(IDirectSound
*iface
, DWORD config
)
1214 DSDevice
*This
= impl_from_IDirectSound(iface
);
1215 return DS8_SetSpeakerConfig(&This
->IDirectSound8_iface
, config
);
1218 static HRESULT WINAPI
DS_Initialize(IDirectSound
*iface
, const GUID
*devguid
)
1220 DSDevice
*This
= impl_from_IDirectSound(iface
);
1221 return DS8_Initialize(&This
->IDirectSound8_iface
, devguid
);
1224 static IDirectSoundVtbl DS_Vtbl
= {
1228 DS_CreateSoundBuffer
,
1230 DS_DuplicateSoundBuffer
,
1231 DS_SetCooperativeLevel
,
1233 DS_GetSpeakerConfig
,
1234 DS_SetSpeakerConfig
,