1 /* DirectSound capture buffer interface
3 * Copyright 2009 Maarten Lankhorst
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #define NONAMELESSSTRUCT
26 #define NONAMELESSUNION
35 #include "wine/debug.h"
38 #include "dsound_private.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(dsound
);
51 #include "dsound_private.h"
53 #ifndef DSCBPN_OFFSET_STOP
54 #define DSCBPN_OFFSET_STOP 0xffffffff
57 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_PCM
, 0x00000001, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71);
61 typedef struct DSCImpl DSCImpl
;
62 typedef struct DSCBuffer DSCBuffer
;
65 /* IDirectSoundCapture and IDirectSoundCapture8 are aliases */
66 IDirectSoundCapture IDirectSoundCapture_iface
;
73 CRITICAL_SECTION crst
;
77 IDirectSoundCaptureBuffer8 IDirectSoundCaptureBuffer8_iface
;
78 IDirectSoundNotify IDirectSoundNotify_iface
;
87 DSBPOSITIONNOTIFY
*notify
;
91 BOOL playing
, looping
;
94 static const IDirectSoundCaptureVtbl DSC_Vtbl
;
95 static const IDirectSoundCaptureBuffer8Vtbl DSCBuffer_Vtbl
;
96 static const IDirectSoundNotifyVtbl DSCNot_Vtbl
;
98 static void DSCImpl_Destroy(DSCImpl
*This
);
100 static HRESULT
DSCBuffer_Create(DSCBuffer
**buf
)
102 DSCBuffer
*This
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*This
));
103 if (!This
) return E_OUTOFMEMORY
;
105 This
->IDirectSoundCaptureBuffer8_iface
.lpVtbl
= (IDirectSoundCaptureBuffer8Vtbl
*)&DSCBuffer_Vtbl
;
106 This
->IDirectSoundNotify_iface
.lpVtbl
= (IDirectSoundNotifyVtbl
*)&DSCNot_Vtbl
;
108 This
->all_ref
= This
->ref
= 1;
114 static void trigger_notifies(DSCBuffer
*buf
, DWORD lastpos
, DWORD curpos
)
118 if (lastpos
== curpos
)
121 for (i
= 0; i
< buf
->nnotify
; ++i
)
123 DSBPOSITIONNOTIFY
*not = &buf
->notify
[i
];
124 HANDLE event
= not->hEventNotify
;
125 DWORD ofs
= not->dwOffset
;
127 if (ofs
== DSCBPN_OFFSET_STOP
)
130 /* Wraparound case */
131 if (curpos
< lastpos
)
133 if (ofs
< curpos
|| ofs
>= lastpos
)
139 if (ofs
>= lastpos
&& ofs
< curpos
)
144 static void CALLBACK
DSCBuffer_timer(UINT timerID
, UINT msg
, DWORD_PTR dwUser
,
145 DWORD_PTR dw1
, DWORD_PTR dw2
)
147 DSCImpl
*This
= (DSCImpl
*)dwUser
;
155 EnterCriticalSection(&This
->crst
);
157 if (!buf
|| !buf
->dev
|| !buf
->playing
)
160 alcGetIntegerv(buf
->dev
, ALC_CAPTURE_SAMPLES
, 1, &avail
);
163 avail
*= buf
->format
->nBlockAlign
;
164 if (avail
+ buf
->pos
> buf
->buf_size
)
165 avail
= buf
->buf_size
- buf
->pos
;
167 alcCaptureSamples(buf
->dev
, buf
->buf
+ buf
->pos
, avail
/buf
->format
->nBlockAlign
);
168 trigger_notifies(buf
, buf
->pos
, buf
->pos
+ avail
);
171 if (buf
->pos
== buf
->buf_size
)
175 IDirectSoundCaptureBuffer8_Stop(&buf
->IDirectSoundCaptureBuffer8_iface
);
179 alcGetIntegerv(buf
->dev
, ALC_CAPTURE_SAMPLES
, 1, &avail
);
180 avail
*= buf
->format
->nBlockAlign
;
181 if ((ALCuint
)avail
>= buf
->buf_size
)
183 ERR("TOO MUCH AVAIL: %u/%"LONGFMT
"u\n", avail
, buf
->buf_size
);
184 avail
= buf
->buf_size
;
189 alcCaptureSamples(buf
->dev
, buf
->buf
+ buf
->pos
, avail
/buf
->format
->nBlockAlign
);
190 trigger_notifies(buf
, buf
->pos
, buf
->pos
+ avail
);
198 LeaveCriticalSection(&This
->crst
);
202 static void DSCBuffer_starttimer(DSCImpl
*prim
)
205 ALint refresh
= FAKE_REFRESH_COUNT
;
206 DWORD triggertime
, res
= DS_TIME_RES
;
211 timeGetDevCaps(&time
, sizeof(TIMECAPS
));
212 triggertime
= 1000 / refresh
;
213 if (triggertime
< time
.wPeriodMin
)
214 triggertime
= time
.wPeriodMin
;
215 TRACE("Calling timer every %"LONGFMT
"u ms for %i refreshes per second\n", triggertime
, refresh
);
216 if (res
< time
.wPeriodMin
)
217 res
= time
.wPeriodMin
;
218 if (timeBeginPeriod(res
) == TIMERR_NOCANDO
)
219 WARN("Could not set minimum resolution, don't expect sound\n");
220 prim
->timer_res
= res
;
221 prim
->timer_id
= timeSetEvent(triggertime
, res
, DSCBuffer_timer
, (DWORD_PTR
)prim
, TIME_PERIODIC
| TIME_KILL_SYNCHRONOUS
);
224 static void DSCBuffer_Destroy(DSCBuffer
*This
)
229 alcCaptureStop(This
->dev
);
230 alcCaptureCloseDevice(This
->dev
);
233 This
->parent
->buf
= NULL
;
234 HeapFree(GetProcessHeap(), 0, This
->notify
);
235 HeapFree(GetProcessHeap(), 0, This
->format
);
236 HeapFree(GetProcessHeap(), 0, This
->buf
);
237 HeapFree(GetProcessHeap(), 0, This
);
240 static inline DSCBuffer
*impl_from_IDirectSoundCaptureBuffer8(IDirectSoundCaptureBuffer8
*iface
)
242 return CONTAINING_RECORD(iface
, DSCBuffer
, IDirectSoundCaptureBuffer8_iface
);
245 static HRESULT WINAPI
DSCBuffer_QueryInterface(IDirectSoundCaptureBuffer8
*iface
, REFIID riid
, void **ppv
)
247 DSCBuffer
*This
= impl_from_IDirectSoundCaptureBuffer8(iface
);
249 TRACE("(%p)->(%s,%p)\n", This
, debugstr_guid(riid
), ppv
);
255 if (IsEqualIID(riid
, &IID_IDirectSoundNotify
))
256 *ppv
= &This
->IDirectSoundNotify_iface
;
257 else if (IsEqualIID(riid
, &IID_IUnknown
) ||
258 IsEqualIID(riid
, &IID_IDirectSoundCaptureBuffer
) ||
259 IsEqualIID(riid
, &IID_IDirectSoundCaptureBuffer8
))
260 *ppv
= &This
->IDirectSoundCaptureBuffer8_iface
;
263 return E_NOINTERFACE
;
264 IUnknown_AddRef((IUnknown
*)*ppv
);
268 static ULONG WINAPI
DSCBuffer_AddRef(IDirectSoundCaptureBuffer8
*iface
)
270 DSCBuffer
*This
= impl_from_IDirectSoundCaptureBuffer8(iface
);
273 InterlockedIncrement(&This
->all_ref
);
274 ref
= InterlockedIncrement(&This
->ref
);
275 TRACE("Reference count incremented to %"LONGFMT
"i\n", ref
);
280 static ULONG WINAPI
DSCBuffer_Release(IDirectSoundCaptureBuffer8
*iface
)
282 DSCBuffer
*This
= impl_from_IDirectSoundCaptureBuffer8(iface
);
285 ref
= InterlockedDecrement(&This
->ref
);
286 TRACE("Reference count decremented to %"LONGFMT
"i\n", ref
);
287 if(InterlockedDecrement(&This
->all_ref
) == 0)
288 DSCBuffer_Destroy(This
);
293 static HRESULT WINAPI
DSCBuffer_GetCaps(IDirectSoundCaptureBuffer8
*iface
, DSCBCAPS
*caps
)
295 DSCBuffer
*This
= impl_from_IDirectSoundCaptureBuffer8(iface
);
297 if (!caps
|| caps
->dwSize
< sizeof(*caps
))
298 return DSERR_INVALIDPARAM
;
299 caps
->dwSize
= sizeof(*caps
);
301 caps
->dwBufferBytes
= This
->buf_size
;
305 static HRESULT WINAPI
DSCBuffer_GetCurrentPosition(IDirectSoundCaptureBuffer8
*iface
, DWORD
*cappos
, DWORD
*readpos
)
307 DSCBuffer
*This
= impl_from_IDirectSoundCaptureBuffer8(iface
);
310 EnterCriticalSection(&This
->parent
->crst
);
314 pos2
= This
->format
->nSamplesPerSec
/ 100;
315 pos2
*= This
->format
->nBlockAlign
;
317 if (!This
->looping
&& pos2
>= This
->buf_size
)
320 pos2
%= This
->buf_size
;
324 LeaveCriticalSection(&This
->parent
->crst
);
326 if(cappos
) *cappos
= pos1
;
327 if(readpos
) *readpos
= pos2
;
332 static HRESULT WINAPI
DSCBuffer_GetFormat(IDirectSoundCaptureBuffer8
*iface
, WAVEFORMATEX
*wfx
, DWORD size
, DWORD
*written
)
334 DSCBuffer
*This
= impl_from_IDirectSoundCaptureBuffer8(iface
);
335 TRACE("(%p,%p,%"LONGFMT
"u,%p)\n", This
, wfx
, size
, written
);
337 if (size
> sizeof(WAVEFORMATEX
) + This
->format
->cbSize
)
338 size
= sizeof(WAVEFORMATEX
) + This
->format
->cbSize
;
342 CopyMemory(wfx
, This
->format
, size
);
347 *written
= sizeof(WAVEFORMATEX
) + This
->format
->cbSize
;
349 return DSERR_INVALIDPARAM
;
354 static HRESULT WINAPI
DSCBuffer_GetStatus(IDirectSoundCaptureBuffer8
*iface
, DWORD
*status
)
356 DSCBuffer
*This
= impl_from_IDirectSoundCaptureBuffer8(iface
);
357 TRACE("(%p)->(%p)\n", This
, status
);
360 return DSERR_INVALIDPARAM
;
361 EnterCriticalSection(&This
->parent
->crst
);
365 *status
|= DSCBSTATUS_CAPTURING
;
367 *status
|= DSCBSTATUS_LOOPING
;
369 LeaveCriticalSection(&This
->parent
->crst
);
374 static HRESULT WINAPI
DSCBuffer_Initialize(IDirectSoundCaptureBuffer8
*iface
, IDirectSoundCapture
*parent
, const DSCBUFFERDESC
*desc
)
376 DSCBuffer
*This
= impl_from_IDirectSoundCaptureBuffer8(iface
);
377 WAVEFORMATEX
*format
;
378 ALenum buf_format
= -1;
381 return DSERR_ALREADYINITIALIZED
;
382 This
->parent
= (DSCImpl
*)parent
;
384 if (!desc
->lpwfxFormat
)
385 return DSERR_INVALIDPARAM
;
387 format
= desc
->lpwfxFormat
;
388 if (format
->nChannels
> 2)
390 WARN("nChannels > 2 not supported for recording\n");
391 return DSERR_INVALIDPARAM
;
395 This
->format
= HeapAlloc(GetProcessHeap(), 0, sizeof(WAVEFORMATEXTENSIBLE
));
397 return DSERR_OUTOFMEMORY
;
399 if (format
->wFormatTag
== WAVE_FORMAT_PCM
||
400 format
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
)
402 if (format
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
)
404 WAVEFORMATEXTENSIBLE
*wfe
= (WAVEFORMATEXTENSIBLE
*)format
;
405 if (format
->cbSize
< sizeof(WAVEFORMATEXTENSIBLE
)-sizeof(WAVEFORMATEX
))
406 return DSERR_INVALIDPARAM
;
407 else if (format
->cbSize
> sizeof(WAVEFORMATEXTENSIBLE
)-sizeof(WAVEFORMATEX
)
408 && format
->cbSize
!= sizeof(WAVEFORMATEXTENSIBLE
))
409 return DSERR_CONTROLUNAVAIL
;
410 else if (!IsEqualGUID(&wfe
->SubFormat
, &KSDATAFORMAT_SUBTYPE_PCM
))
411 return DSERR_BADFORMAT
;
414 if (format
->nChannels
== 1)
416 switch (format
->wBitsPerSample
)
418 case 8: buf_format
= AL_FORMAT_MONO8
; break;
419 case 16: buf_format
= AL_FORMAT_MONO16
; break;
421 WARN("Unsupported bpp %u\n", format
->wBitsPerSample
);
422 return DSERR_BADFORMAT
;
425 else if (format
->nChannels
== 2)
427 switch (format
->wBitsPerSample
)
429 case 8: buf_format
= AL_FORMAT_STEREO8
; break;
430 case 16: buf_format
= AL_FORMAT_STEREO16
; break;
432 WARN("Unsupported bpp %u\n", format
->wBitsPerSample
);
433 return DSERR_BADFORMAT
;
436 memcpy(This
->format
, format
, sizeof(*format
) + format
->cbSize
);
437 if (format
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
)
438 This
->format
->cbSize
= sizeof(WAVEFORMATEXTENSIBLE
) - sizeof(WAVEFORMATEX
);
440 This
->format
->cbSize
= 0;
441 This
->format
->nBlockAlign
= This
->format
->wBitsPerSample
* This
->format
->nChannels
/ 8;
442 This
->format
->nAvgBytesPerSec
= This
->format
->nSamplesPerSec
* This
->format
->nBlockAlign
;
444 else if (format
->wFormatTag
)
445 WARN("Unhandled formattag %x\n", format
->wFormatTag
);
447 This
->buf_size
= desc
->dwBufferBytes
;
450 WARN("Could not get OpenAL format\n");
451 return DSERR_INVALIDPARAM
;
454 This
->dev
= alcCaptureOpenDevice(This
->parent
->device
, This
->format
->nSamplesPerSec
, buf_format
, This
->buf_size
/ This
->format
->nBlockAlign
);
457 ERR("couldn't open device %s %x@%"LONGFMT
"u, reason: %04x\n", This
->parent
->device
, buf_format
, This
->format
->nSamplesPerSec
, alcGetError(NULL
));
458 return DSERR_INVALIDPARAM
;
461 This
->buf
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, This
->buf_size
);
464 alcCaptureCloseDevice(This
->dev
);
465 WARN("Out of memory\n");
466 return DSERR_INVALIDPARAM
;
472 static HRESULT WINAPI
DSCBuffer_Lock(IDirectSoundCaptureBuffer8
*iface
, DWORD ofs
, DWORD bytes
, void **ptr1
, DWORD
*len1
, void **ptr2
, DWORD
*len2
, DWORD flags
)
474 DSCBuffer
*This
= impl_from_IDirectSoundCaptureBuffer8(iface
);
477 TRACE("(%p)->(%"LONGFMT
"u, %"LONGFMT
"u, %p, %p, %p, %p, %#"LONGFMT
"x)\n", This
, ofs
, bytes
, ptr1
, len1
, ptr2
, len2
, flags
);
479 EnterCriticalSection(&This
->parent
->crst
);
480 hr
= DSERR_INVALIDPARAM
;
482 if(ptr1
) *ptr1
= NULL
;
484 if(ptr2
) *ptr2
= NULL
;
487 if (ofs
>= This
->buf_size
)
489 WARN("Invalid ofs %"LONGFMT
"u\n", ofs
);
494 WARN("Invalid pointer/len %p %p\n", ptr1
, len1
);
497 if((flags
&DSCBLOCK_ENTIREBUFFER
))
498 bytes
= This
->buf_size
;
499 else if(bytes
> This
->buf_size
)
501 WARN("Invalid size %"LONGFMT
"u\n", bytes
);
505 if (ofs
+ bytes
>= This
->buf_size
)
507 *len1
= This
->buf_size
- ofs
;
508 remain
= bytes
- *len1
;
515 *ptr1
= This
->buf
+ ofs
;
517 if (ptr2
&& len2
&& remain
)
525 LeaveCriticalSection(&This
->parent
->crst
);
529 static HRESULT WINAPI
DSCBuffer_Start(IDirectSoundCaptureBuffer8
*iface
, DWORD flags
)
531 DSCBuffer
*This
= impl_from_IDirectSoundCaptureBuffer8(iface
);
532 TRACE("(%p)->(%08"LONGFMT
"x)\n", This
, flags
);
534 EnterCriticalSection(&This
->parent
->crst
);
537 DSCBuffer_starttimer(This
->parent
);
539 alcCaptureStart(This
->dev
);
541 This
->looping
= !!(flags
& DSCBSTART_LOOPING
);
542 LeaveCriticalSection(&This
->parent
->crst
);
546 static HRESULT WINAPI
DSCBuffer_Stop(IDirectSoundCaptureBuffer8
*iface
)
548 DSCBuffer
*This
= impl_from_IDirectSoundCaptureBuffer8(iface
);
549 TRACE("(%p)\n", This
);
551 EnterCriticalSection(&This
->parent
->crst
);
556 for (i
= 0; i
< This
->nnotify
; ++i
)
557 if (This
->notify
[i
].dwOffset
== DSCBPN_OFFSET_STOP
)
559 SetEvent(This
->notify
[i
].hEventNotify
);
562 This
->playing
= This
->looping
= 0;
563 alcCaptureStop(This
->dev
);
565 LeaveCriticalSection(&This
->parent
->crst
);
569 static HRESULT WINAPI
DSCBuffer_Unlock(IDirectSoundCaptureBuffer8
*iface
, void *ptr1
, DWORD len1
, void *ptr2
, DWORD len2
)
571 DSCBuffer
*This
= impl_from_IDirectSoundCaptureBuffer8(iface
);
572 TRACE("(%p)->(%p,%"LONGFMT
"u,%p,%"LONGFMT
"u)\n", This
, ptr1
, len1
, ptr2
, len2
);
575 return DSERR_INVALIDPARAM
;
579 static HRESULT WINAPI
DSCBuffer_GetObjectInPath(IDirectSoundCaptureBuffer8
*iface
, REFGUID guid
, DWORD num
, REFGUID riid
, void **ppv
)
581 DSCBuffer
*This
= impl_from_IDirectSoundCaptureBuffer8(iface
);
582 FIXME("(%p)->(%s %"LONGFMT
"u %s %p) stub\n", This
, debugstr_guid(guid
), num
, debugstr_guid(riid
), ppv
);
586 static HRESULT WINAPI
DSCBuffer_GetFXStatus(IDirectSoundCaptureBuffer8
*iface
, DWORD count
, DWORD
*status
)
588 DSCBuffer
*This
= impl_from_IDirectSoundCaptureBuffer8(iface
);
589 FIXME("(%p)->(%"LONGFMT
"u %p) stub\n", This
, count
, status
);
593 static const IDirectSoundCaptureBuffer8Vtbl DSCBuffer_Vtbl
=
595 DSCBuffer_QueryInterface
,
599 DSCBuffer_GetCurrentPosition
,
602 DSCBuffer_Initialize
,
607 DSCBuffer_GetObjectInPath
,
608 DSCBuffer_GetFXStatus
611 static inline DSCBuffer
*impl_from_IDirectSoundNotify(IDirectSoundNotify
*iface
)
613 return CONTAINING_RECORD(iface
, DSCBuffer
, IDirectSoundNotify_iface
);
616 static HRESULT WINAPI
DSCBufferNot_QueryInterface(IDirectSoundNotify
*iface
, REFIID riid
, void **ppv
)
618 DSCBuffer
*This
= impl_from_IDirectSoundNotify(iface
);
619 return IDirectSoundCaptureBuffer_QueryInterface((IDirectSoundCaptureBuffer
*)This
, riid
, ppv
);
622 static ULONG WINAPI
DSCBufferNot_AddRef(IDirectSoundNotify
*iface
)
624 DSCBuffer
*This
= impl_from_IDirectSoundNotify(iface
);
627 InterlockedIncrement(&This
->all_ref
);
628 ret
= InterlockedIncrement(&This
->not_ref
);
629 TRACE("new refcount %"LONGFMT
"d\n", ret
);
633 static ULONG WINAPI
DSCBufferNot_Release(IDirectSoundNotify
*iface
)
635 DSCBuffer
*This
= impl_from_IDirectSoundNotify(iface
);
638 ret
= InterlockedDecrement(&This
->not_ref
);
639 TRACE("new refcount %"LONGFMT
"d\n", ret
);
640 if(InterlockedDecrement(&This
->all_ref
) == 0)
641 DSCBuffer_Destroy(This
);
646 static HRESULT WINAPI
DSCBufferNot_SetNotificationPositions(IDirectSoundNotify
*iface
, DWORD count
, const DSBPOSITIONNOTIFY
*notifications
)
648 DSCBuffer
*This
= impl_from_IDirectSoundNotify(iface
);
649 DSBPOSITIONNOTIFY
*nots
;
653 EnterCriticalSection(&This
->parent
->crst
);
654 hr
= DSERR_INVALIDPARAM
;
655 if (count
&& !notifications
)
658 hr
= DSCBuffer_GetStatus(&This
->IDirectSoundCaptureBuffer8_iface
, &state
);
662 hr
= DSERR_INVALIDCALL
;
663 if (state
& DSCBSTATUS_CAPTURING
)
668 HeapFree(GetProcessHeap(), 0, This
->notify
);
675 hr
= DSERR_INVALIDPARAM
;
676 for (i
= 0; i
< count
; ++i
)
678 if (notifications
[i
].dwOffset
>= This
->buf_size
679 && notifications
[i
].dwOffset
!= DSCBPN_OFFSET_STOP
)
683 nots
= HeapAlloc(GetProcessHeap(), 0, count
*sizeof(*nots
));
686 memcpy(nots
, notifications
, count
*sizeof(*nots
));
687 HeapFree(GetProcessHeap(), 0, This
->notify
);
689 This
->nnotify
= count
;
694 LeaveCriticalSection(&This
->parent
->crst
);
698 static const IDirectSoundNotifyVtbl DSCNot_Vtbl
=
700 DSCBufferNot_QueryInterface
,
702 DSCBufferNot_Release
,
703 DSCBufferNot_SetNotificationPositions
706 HRESULT
DSOUND_CaptureCreate(REFIID riid
, void **cap
)
712 This
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*This
));
713 if(!This
) return DSERR_OUTOFMEMORY
;
715 This
->IDirectSoundCapture_iface
.lpVtbl
= (IDirectSoundCaptureVtbl
*)&DSC_Vtbl
;
717 InitializeCriticalSection(&This
->crst
);
718 This
->crst
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": DSCImpl.crst");
720 if(FAILED(IDirectSoundCapture_QueryInterface(&This
->IDirectSoundCapture_iface
, riid
, cap
)))
722 DSCImpl_Destroy(This
);
723 return E_NOINTERFACE
;
728 static void DSCImpl_Destroy(DSCImpl
*This
)
732 timeKillEvent(This
->timer_id
);
733 timeEndPeriod(This
->timer_res
);
736 EnterCriticalSection(&This
->crst
);
738 DSCBuffer_Destroy(This
->buf
);
739 LeaveCriticalSection(&This
->crst
);
741 HeapFree(GetProcessHeap(), 0, This
->device
);
743 This
->crst
.DebugInfo
->Spare
[0] = 0;
744 DeleteCriticalSection(&This
->crst
);
746 HeapFree(GetProcessHeap(), 0, This
);
749 static HRESULT WINAPI
DSCImpl_QueryInterface(IDirectSoundCapture
*iface
, REFIID riid
, void **ppv
)
752 if(IsEqualIID(riid
, &IID_IUnknown
) ||
753 IsEqualIID(riid
, &IID_IDirectSoundCapture
))
757 return E_NOINTERFACE
;
758 IUnknown_AddRef((IUnknown
*)*ppv
);
762 static ULONG WINAPI
DSCImpl_AddRef(IDirectSoundCapture8
*iface
)
764 DSCImpl
*This
= (DSCImpl
*)iface
;
767 ref
= InterlockedIncrement(&This
->ref
);
768 TRACE("Reference count incremented to %"LONGFMT
"i\n", ref
);
773 static ULONG WINAPI
DSCImpl_Release(IDirectSoundCapture8
*iface
)
775 DSCImpl
*This
= (DSCImpl
*)iface
;
778 ref
= InterlockedDecrement(&This
->ref
);
779 TRACE("Reference count decremented to %"LONGFMT
"i\n", ref
);
781 DSCImpl_Destroy(This
);
786 static HRESULT WINAPI
DSCImpl_CreateCaptureBuffer(IDirectSoundCapture8
*iface
, const DSCBUFFERDESC
*desc
, IDirectSoundCaptureBuffer
**ppv
, IUnknown
*unk
)
788 DSCImpl
*This
= (DSCImpl
*)iface
;
790 TRACE("(%p)->(%p,%p,%p)\n", This
, desc
, ppv
, unk
);
794 WARN("Aggregation isn't supported\n");
795 return DSERR_NOAGGREGATION
;
798 if (!desc
|| desc
->dwSize
< sizeof(DSCBUFFERDESC1
))
800 WARN("Passed invalid description %p %"LONGFMT
"u\n", desc
, desc
?desc
->dwSize
:0);
801 return DSERR_INVALIDPARAM
;
805 WARN("Passed null pointer\n");
806 return DSERR_INVALIDPARAM
;
810 EnterCriticalSection(&This
->crst
);
813 hr
= DSERR_UNINITIALIZED
;
814 WARN("Not initialized\n");
819 hr
= DSERR_ALLOCATED
;
820 WARN("Capture buffer already allocated\n");
824 hr
= DSCBuffer_Create(&This
->buf
);
827 hr
= IDirectSoundCaptureBuffer_Initialize((IDirectSoundCaptureBuffer
*)This
->buf
, iface
, desc
);
830 DSCBuffer_Destroy(This
->buf
);
834 *ppv
= (IDirectSoundCaptureBuffer
*)This
->buf
;
836 LeaveCriticalSection(&This
->crst
);
840 static HRESULT WINAPI
DSCImpl_GetCaps(IDirectSoundCapture8
*iface
, DSCCAPS
*caps
)
842 DSCImpl
*This
= (DSCImpl
*)iface
;
843 TRACE("(%p,%p)\n", This
, caps
);
846 WARN("Not initialized\n");
847 return DSERR_UNINITIALIZED
;
851 WARN("Caps is null\n");
852 return DSERR_INVALIDPARAM
;
855 if (caps
->dwSize
< sizeof(*caps
)) {
856 WARN("Invalid size %"LONGFMT
"d\n", caps
->dwSize
);
857 return DSERR_INVALIDPARAM
;
861 /* Support all WAVE_FORMAT formats specified in mmsystem.h */
862 caps
->dwFormats
= 0x000fffff;
863 caps
->dwChannels
= 2;
868 static HRESULT WINAPI
DSCImpl_Initialize(IDirectSoundCapture8
*iface
, const GUID
*devguid
)
870 DSCImpl
*This
= (DSCImpl
*)iface
;
872 const ALCchar
*devs
, *drv_name
;
875 TRACE("(%p,%p)\n", This
, devguid
);
879 ERR("OpenAL not loaded!\n");
880 return DSERR_NODRIVER
;
884 WARN("Already initialized\n");
885 return DSERR_ALREADYINITIALIZED
;
889 devguid
= &DSDEVID_DefaultCapture
;
891 hr
= GetDeviceID(devguid
, &guid
);
893 return DSERR_INVALIDPARAM
;
896 EnterCriticalSection(&This
->crst
);
897 EnterCriticalSection(&openal_crst
);
898 devs
= DSOUND_getcapturedevicestrings();
902 if(memcmp(devguid
, &DSOUND_capture_guid
, sizeof(GUID
)-1) ||
905 WARN("No driver found\n");
911 const ALCchar
*str
= devs
;
914 str
+= strlen(str
) + 1;
917 WARN("No driver string found\n");
926 This
->device
= HeapAlloc(GetProcessHeap(), 0, strlen(drv_name
)+1);
929 WARN("Out of memory to allocate %s\n", drv_name
);
932 strcpy(This
->device
, drv_name
);
936 LeaveCriticalSection(&openal_crst
);
937 LeaveCriticalSection(&This
->crst
);
941 static const IDirectSoundCaptureVtbl DSC_Vtbl
=
943 DSCImpl_QueryInterface
,
946 DSCImpl_CreateCaptureBuffer
,