dsound: fixup IDirectSoundCaptureBuffer_QueryInterface
[wine/multimedia.git] / dlls / dsound / capture.c
blob412b59e46daca3a6d2de879171b52aaa03187995
1 /* DirectSoundCapture
3 * Copyright 1998 Marcus Meissner
4 * Copyright 1998 Rob Riggs
5 * Copyright 2000-2001 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
22 * TODO:
23 * Implement FX support.
24 * Implement both IDirectSoundCaptureBuffer and IDirectSoundCaptureBuffer8
25 * Make DirectSoundCaptureCreate and DirectSoundCaptureCreate8 behave differently
28 #include <stdarg.h>
30 #define COBJMACROS
32 #include "windef.h"
33 #include "winbase.h"
34 #include "winuser.h"
35 #include "mmsystem.h"
36 #include "mmddk.h"
37 #include "winternl.h"
38 #include "winnls.h"
39 #include "wine/debug.h"
40 #include "dsound.h"
41 #include "dsound_private.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(dsound);
45 typedef struct DirectSoundCaptureDevice DirectSoundCaptureDevice;
47 /* IDirectSoundCaptureBuffer implementation structure */
48 typedef struct IDirectSoundCaptureBufferImpl
50 IDirectSoundCaptureBuffer8 IDirectSoundCaptureBuffer8_iface;
51 IDirectSoundNotify IDirectSoundNotify_iface;
52 LONG numIfaces; /* "in use interfaces" refcount */
53 LONG ref, refn, has_dsc8;
54 /* IDirectSoundCaptureBuffer fields */
55 DirectSoundCaptureDevice *device;
56 DSCBUFFERDESC *pdscbd;
57 DWORD flags;
58 /* IDirectSoundNotify fields */
59 DSBPOSITIONNOTIFY *notifies;
60 int nrofnotifies;
61 HANDLE thread;
62 HANDLE sleepev;
63 } IDirectSoundCaptureBufferImpl;
65 /* DirectSoundCaptureDevice implementation structure */
66 struct DirectSoundCaptureDevice
68 GUID guid;
69 LONG ref;
70 DSCCAPS drvcaps;
71 BYTE *buffer;
72 DWORD buflen, write_pos_bytes;
73 WAVEFORMATEX *pwfx;
74 IDirectSoundCaptureBufferImpl *capture_buffer;
75 DWORD state;
76 CRITICAL_SECTION lock;
77 IMMDevice *mmdevice;
78 IAudioClient *client;
79 IAudioCaptureClient *capture;
80 struct list entry;
83 static DWORD WINAPI DSOUND_capture_thread(void *user);
85 static void capturebuffer_destroy(IDirectSoundCaptureBufferImpl *This)
87 if (This->device->state == STATE_CAPTURING)
88 This->device->state = STATE_STOPPING;
90 if(This->thread){
91 SetEvent(This->sleepev);
92 WaitForSingleObject(This->thread, INFINITE);
93 CloseHandle(This->thread);
95 CloseHandle(This->sleepev);
97 HeapFree(GetProcessHeap(),0, This->pdscbd);
99 if (This->device->client) {
100 IAudioClient_Release(This->device->client);
101 This->device->client = NULL;
104 if (This->device->capture) {
105 IAudioCaptureClient_Release(This->device->capture);
106 This->device->capture = NULL;
109 /* remove from DirectSoundCaptureDevice */
110 This->device->capture_buffer = NULL;
112 HeapFree(GetProcessHeap(), 0, This->notifies);
113 HeapFree(GetProcessHeap(), 0, This);
114 TRACE("(%p) released\n", This);
117 /*******************************************************************************
118 * IDirectSoundNotify
120 static inline struct IDirectSoundCaptureBufferImpl *impl_from_IDirectSoundNotify(IDirectSoundNotify *iface)
122 return CONTAINING_RECORD(iface, IDirectSoundCaptureBufferImpl, IDirectSoundNotify_iface);
125 static HRESULT WINAPI IDirectSoundNotifyImpl_QueryInterface(IDirectSoundNotify *iface, REFIID riid,
126 void **ppobj)
128 IDirectSoundCaptureBufferImpl *This = impl_from_IDirectSoundNotify(iface);
130 TRACE("(%p,%s,%p)\n", This, debugstr_guid(riid), ppobj);
132 return IDirectSoundCaptureBuffer_QueryInterface(&This->IDirectSoundCaptureBuffer8_iface, riid, ppobj);
135 static ULONG WINAPI IDirectSoundNotifyImpl_AddRef(IDirectSoundNotify *iface)
137 IDirectSoundCaptureBufferImpl *This = impl_from_IDirectSoundNotify(iface);
138 ULONG ref = InterlockedIncrement(&This->refn);
140 TRACE("(%p) ref was %d\n", This, ref - 1);
142 if(ref == 1)
143 InterlockedIncrement(&This->numIfaces);
145 return ref;
148 static ULONG WINAPI IDirectSoundNotifyImpl_Release(IDirectSoundNotify *iface)
150 IDirectSoundCaptureBufferImpl *This = impl_from_IDirectSoundNotify(iface);
151 ULONG ref = InterlockedDecrement(&This->refn);
153 TRACE("(%p) ref was %d\n", This, ref + 1);
155 if (!ref && !InterlockedDecrement(&This->numIfaces))
156 capturebuffer_destroy(This);
158 return ref;
161 static HRESULT WINAPI IDirectSoundNotifyImpl_SetNotificationPositions(IDirectSoundNotify *iface,
162 DWORD howmuch, const DSBPOSITIONNOTIFY *notify)
164 IDirectSoundCaptureBufferImpl *This = impl_from_IDirectSoundNotify(iface);
165 TRACE("(%p,0x%08x,%p)\n",This,howmuch,notify);
167 if (howmuch > 0 && notify == NULL) {
168 WARN("invalid parameter: notify == NULL\n");
169 return DSERR_INVALIDPARAM;
172 if (TRACE_ON(dsound)) {
173 unsigned int i;
174 for (i=0;i<howmuch;i++)
175 TRACE("notify at %d to %p\n",
176 notify[i].dwOffset,notify[i].hEventNotify);
179 if (howmuch > 0) {
180 /* Make an internal copy of the caller-supplied array.
181 * Replace the existing copy if one is already present. */
182 if (This->notifies)
183 This->notifies = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->notifies,
184 howmuch * sizeof(DSBPOSITIONNOTIFY));
185 else
186 This->notifies = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
187 howmuch * sizeof(DSBPOSITIONNOTIFY));
189 if (!This->notifies) {
190 WARN("out of memory\n");
191 return DSERR_OUTOFMEMORY;
193 CopyMemory(This->notifies, notify, howmuch * sizeof(DSBPOSITIONNOTIFY));
194 This->nrofnotifies = howmuch;
195 } else {
196 HeapFree(GetProcessHeap(), 0, This->notifies);
197 This->notifies = NULL;
198 This->nrofnotifies = 0;
201 return S_OK;
204 static const IDirectSoundNotifyVtbl dscnvt =
206 IDirectSoundNotifyImpl_QueryInterface,
207 IDirectSoundNotifyImpl_AddRef,
208 IDirectSoundNotifyImpl_Release,
209 IDirectSoundNotifyImpl_SetNotificationPositions
213 static const char * const captureStateString[] = {
214 "STATE_STOPPED",
215 "STATE_STARTING",
216 "STATE_CAPTURING",
217 "STATE_STOPPING"
221 /*******************************************************************************
222 * IDirectSoundCaptureBuffer
224 static inline IDirectSoundCaptureBufferImpl *impl_from_IDirectSoundCaptureBuffer8(IDirectSoundCaptureBuffer8 *iface)
226 return CONTAINING_RECORD(iface, IDirectSoundCaptureBufferImpl, IDirectSoundCaptureBuffer8_iface);
229 static HRESULT WINAPI IDirectSoundCaptureBufferImpl_QueryInterface(IDirectSoundCaptureBuffer8 *iface,
230 REFIID riid, void **ppobj)
232 IDirectSoundCaptureBufferImpl *This = impl_from_IDirectSoundCaptureBuffer8(iface);
234 TRACE( "(%p,%s,%p)\n", This, debugstr_guid(riid), ppobj );
236 if (ppobj == NULL) {
237 WARN("invalid parameter\n");
238 return E_INVALIDARG;
241 *ppobj = NULL;
243 if ( IsEqualIID( &IID_IUnknown, riid ) ||
244 IsEqualIID( &IID_IDirectSoundCaptureBuffer, riid ) ||
245 (This->has_dsc8 && IsEqualIID( &IID_IDirectSoundCaptureBuffer8, riid )) ) {
246 IDirectSoundCaptureBuffer8_AddRef(iface);
247 *ppobj = iface;
248 return S_OK;
251 if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ) {
252 IDirectSoundNotify_AddRef(&This->IDirectSoundNotify_iface);
253 *ppobj = &This->IDirectSoundNotify_iface;
254 return S_OK;
257 FIXME("(%p,%s,%p) unsupported GUID\n", This, debugstr_guid(riid), ppobj);
258 return E_NOINTERFACE;
261 static ULONG WINAPI IDirectSoundCaptureBufferImpl_AddRef(IDirectSoundCaptureBuffer8 *iface)
263 IDirectSoundCaptureBufferImpl *This = impl_from_IDirectSoundCaptureBuffer8(iface);
264 ULONG ref = InterlockedIncrement(&This->ref);
266 TRACE("(%p) ref was %d\n", This, ref - 1);
268 if(ref == 1)
269 InterlockedIncrement(&This->numIfaces);
271 return ref;
274 static ULONG WINAPI IDirectSoundCaptureBufferImpl_Release(IDirectSoundCaptureBuffer8 *iface)
276 IDirectSoundCaptureBufferImpl *This = impl_from_IDirectSoundCaptureBuffer8(iface);
277 ULONG ref = InterlockedDecrement(&This->ref);
279 TRACE("(%p) ref was %d\n", This, ref + 1);
281 if (!ref && !InterlockedDecrement(&This->numIfaces))
282 capturebuffer_destroy(This);
284 return ref;
287 static HRESULT WINAPI IDirectSoundCaptureBufferImpl_GetCaps(IDirectSoundCaptureBuffer8 *iface,
288 DSCBCAPS *lpDSCBCaps)
290 IDirectSoundCaptureBufferImpl *This = impl_from_IDirectSoundCaptureBuffer8(iface);
291 TRACE( "(%p,%p)\n", This, lpDSCBCaps );
293 if (lpDSCBCaps == NULL) {
294 WARN("invalid parameter: lpDSCBCaps == NULL\n");
295 return DSERR_INVALIDPARAM;
298 if (lpDSCBCaps->dwSize < sizeof(DSCBCAPS)) {
299 WARN("invalid parameter: lpDSCBCaps->dwSize = %d\n", lpDSCBCaps->dwSize);
300 return DSERR_INVALIDPARAM;
303 if (This->device == NULL) {
304 WARN("invalid parameter: This->device == NULL\n");
305 return DSERR_INVALIDPARAM;
308 lpDSCBCaps->dwSize = sizeof(DSCBCAPS);
309 lpDSCBCaps->dwFlags = This->flags;
310 lpDSCBCaps->dwBufferBytes = This->pdscbd->dwBufferBytes;
311 lpDSCBCaps->dwReserved = 0;
313 TRACE("returning DS_OK\n");
314 return DS_OK;
317 static HRESULT WINAPI IDirectSoundCaptureBufferImpl_GetCurrentPosition(IDirectSoundCaptureBuffer8 *iface,
318 DWORD *lpdwCapturePosition, DWORD *lpdwReadPosition)
320 IDirectSoundCaptureBufferImpl *This = impl_from_IDirectSoundCaptureBuffer8(iface);
322 TRACE( "(%p,%p,%p)\n", This, lpdwCapturePosition, lpdwReadPosition );
324 if (This->device == NULL) {
325 WARN("invalid parameter: This->device == NULL\n");
326 return DSERR_INVALIDPARAM;
329 EnterCriticalSection(&This->device->lock);
331 if (!This->device->client) {
332 LeaveCriticalSection(&This->device->lock);
333 WARN("no driver\n");
334 return DSERR_NODRIVER;
337 if(lpdwCapturePosition)
338 *lpdwCapturePosition = This->device->write_pos_bytes;
340 if(lpdwReadPosition)
341 *lpdwReadPosition = This->device->write_pos_bytes;
343 LeaveCriticalSection(&This->device->lock);
345 TRACE("cappos=%d readpos=%d\n", (lpdwCapturePosition?*lpdwCapturePosition:-1), (lpdwReadPosition?*lpdwReadPosition:-1));
346 TRACE("returning DS_OK\n");
348 return DS_OK;
351 static HRESULT WINAPI IDirectSoundCaptureBufferImpl_GetFormat(IDirectSoundCaptureBuffer8 *iface,
352 WAVEFORMATEX *lpwfxFormat, DWORD dwSizeAllocated, DWORD *lpdwSizeWritten)
354 IDirectSoundCaptureBufferImpl *This = impl_from_IDirectSoundCaptureBuffer8(iface);
355 HRESULT hres = DS_OK;
357 TRACE("(%p,%p,0x%08x,%p)\n", This, lpwfxFormat, dwSizeAllocated, lpdwSizeWritten);
359 if (This->device == NULL) {
360 WARN("invalid parameter: This->device == NULL\n");
361 return DSERR_INVALIDPARAM;
364 if (dwSizeAllocated > (sizeof(WAVEFORMATEX) + This->device->pwfx->cbSize))
365 dwSizeAllocated = sizeof(WAVEFORMATEX) + This->device->pwfx->cbSize;
367 if (lpwfxFormat) { /* NULL is valid (just want size) */
368 CopyMemory(lpwfxFormat, This->device->pwfx, dwSizeAllocated);
369 if (lpdwSizeWritten)
370 *lpdwSizeWritten = dwSizeAllocated;
371 } else {
372 if (lpdwSizeWritten)
373 *lpdwSizeWritten = sizeof(WAVEFORMATEX) + This->device->pwfx->cbSize;
374 else {
375 TRACE("invalid parameter: lpdwSizeWritten = NULL\n");
376 hres = DSERR_INVALIDPARAM;
380 TRACE("returning %08x\n", hres);
381 return hres;
384 static HRESULT WINAPI IDirectSoundCaptureBufferImpl_GetStatus(IDirectSoundCaptureBuffer8 *iface,
385 DWORD *lpdwStatus)
387 IDirectSoundCaptureBufferImpl *This = impl_from_IDirectSoundCaptureBuffer8(iface);
389 TRACE( "(%p, %p), thread is %04x\n", This, lpdwStatus, GetCurrentThreadId() );
391 if (This->device == NULL) {
392 WARN("invalid parameter: This->device == NULL\n");
393 return DSERR_INVALIDPARAM;
396 if (lpdwStatus == NULL) {
397 WARN("invalid parameter: lpdwStatus == NULL\n");
398 return DSERR_INVALIDPARAM;
401 *lpdwStatus = 0;
402 EnterCriticalSection(&(This->device->lock));
404 TRACE("old This->device->state=%s, old lpdwStatus=%08x\n",
405 captureStateString[This->device->state],*lpdwStatus);
406 if ((This->device->state == STATE_STARTING) ||
407 (This->device->state == STATE_CAPTURING)) {
408 *lpdwStatus |= DSCBSTATUS_CAPTURING;
409 if (This->flags & DSCBSTART_LOOPING)
410 *lpdwStatus |= DSCBSTATUS_LOOPING;
412 TRACE("new This->device->state=%s, new lpdwStatus=%08x\n",
413 captureStateString[This->device->state],*lpdwStatus);
414 LeaveCriticalSection(&(This->device->lock));
416 TRACE("status=%x\n", *lpdwStatus);
417 TRACE("returning DS_OK\n");
418 return DS_OK;
421 static HRESULT WINAPI IDirectSoundCaptureBufferImpl_Initialize(IDirectSoundCaptureBuffer8 *iface,
422 IDirectSoundCapture *lpDSC, const DSCBUFFERDESC *lpcDSCBDesc)
424 IDirectSoundCaptureBufferImpl *This = impl_from_IDirectSoundCaptureBuffer8(iface);
426 FIXME( "(%p,%p,%p): stub\n", This, lpDSC, lpcDSCBDesc );
428 return DS_OK;
431 static HRESULT WINAPI IDirectSoundCaptureBufferImpl_Lock(IDirectSoundCaptureBuffer8 *iface,
432 DWORD dwReadCusor, DWORD dwReadBytes, void **lplpvAudioPtr1, DWORD *lpdwAudioBytes1,
433 void **lplpvAudioPtr2, DWORD *lpdwAudioBytes2, DWORD dwFlags)
435 IDirectSoundCaptureBufferImpl *This = impl_from_IDirectSoundCaptureBuffer8(iface);
436 HRESULT hres = DS_OK;
438 TRACE( "(%p,%08u,%08u,%p,%p,%p,%p,0x%08x) at %d\n", This, dwReadCusor,
439 dwReadBytes, lplpvAudioPtr1, lpdwAudioBytes1, lplpvAudioPtr2,
440 lpdwAudioBytes2, dwFlags, GetTickCount() );
442 if (This->device == NULL) {
443 WARN("invalid parameter: This->device == NULL\n");
444 return DSERR_INVALIDPARAM;
447 if (lplpvAudioPtr1 == NULL) {
448 WARN("invalid parameter: lplpvAudioPtr1 == NULL\n");
449 return DSERR_INVALIDPARAM;
452 if (lpdwAudioBytes1 == NULL) {
453 WARN("invalid parameter: lpdwAudioBytes1 == NULL\n");
454 return DSERR_INVALIDPARAM;
457 EnterCriticalSection(&(This->device->lock));
459 if (This->device->client) {
460 *lplpvAudioPtr1 = This->device->buffer + dwReadCusor;
461 if ( (dwReadCusor + dwReadBytes) > This->device->buflen) {
462 *lpdwAudioBytes1 = This->device->buflen - dwReadCusor;
463 if (lplpvAudioPtr2)
464 *lplpvAudioPtr2 = This->device->buffer;
465 if (lpdwAudioBytes2)
466 *lpdwAudioBytes2 = dwReadBytes - *lpdwAudioBytes1;
467 } else {
468 *lpdwAudioBytes1 = dwReadBytes;
469 if (lplpvAudioPtr2)
470 *lplpvAudioPtr2 = 0;
471 if (lpdwAudioBytes2)
472 *lpdwAudioBytes2 = 0;
474 } else {
475 TRACE("invalid call\n");
476 hres = DSERR_INVALIDCALL; /* DSERR_NODRIVER ? */
479 LeaveCriticalSection(&(This->device->lock));
481 TRACE("returning %08x\n", hres);
482 return hres;
485 static HRESULT WINAPI IDirectSoundCaptureBufferImpl_Start(IDirectSoundCaptureBuffer8 *iface,
486 DWORD dwFlags)
488 IDirectSoundCaptureBufferImpl *This = impl_from_IDirectSoundCaptureBuffer8(iface);
489 HRESULT hres;
491 TRACE( "(%p,0x%08x)\n", This, dwFlags );
493 if (This->device == NULL) {
494 WARN("invalid parameter: This->device == NULL\n");
495 return DSERR_INVALIDPARAM;
498 if ( !This->device->client ) {
499 WARN("no driver\n");
500 return DSERR_NODRIVER;
503 EnterCriticalSection(&(This->device->lock));
505 if (This->device->state == STATE_STOPPED)
506 This->device->state = STATE_STARTING;
507 else if (This->device->state == STATE_STOPPING)
508 This->device->state = STATE_CAPTURING;
509 else
510 goto out;
511 TRACE("new This->device->state=%s\n",captureStateString[This->device->state]);
512 This->flags = dwFlags;
514 if (This->device->buffer)
515 FillMemory(This->device->buffer, This->device->buflen, (This->device->pwfx->wBitsPerSample == 8) ? 128 : 0);
517 hres = IAudioClient_Start(This->device->client);
518 if(FAILED(hres)){
519 WARN("Start failed: %08x\n", hres);
520 LeaveCriticalSection(&This->device->lock);
521 return hres;
524 out:
525 LeaveCriticalSection(&This->device->lock);
527 TRACE("returning DS_OK\n");
528 return DS_OK;
531 static HRESULT WINAPI IDirectSoundCaptureBufferImpl_Stop(IDirectSoundCaptureBuffer8 *iface)
533 IDirectSoundCaptureBufferImpl *This = impl_from_IDirectSoundCaptureBuffer8(iface);
534 HRESULT hres;
536 TRACE("(%p)\n", This);
538 if (This->device == NULL) {
539 WARN("invalid parameter: This->device == NULL\n");
540 return DSERR_INVALIDPARAM;
543 EnterCriticalSection(&(This->device->lock));
545 TRACE("old This->device->state=%s\n",captureStateString[This->device->state]);
546 if (This->device->state == STATE_CAPTURING)
547 This->device->state = STATE_STOPPING;
548 else if (This->device->state == STATE_STARTING)
549 This->device->state = STATE_STOPPED;
550 TRACE("new This->device->state=%s\n",captureStateString[This->device->state]);
552 if(This->device->client){
553 hres = IAudioClient_Stop(This->device->client);
554 if(FAILED(hres)){
555 LeaveCriticalSection(&This->device->lock);
556 return hres;
560 LeaveCriticalSection(&(This->device->lock));
562 TRACE("returning DS_OK\n");
563 return DS_OK;
566 static HRESULT WINAPI IDirectSoundCaptureBufferImpl_Unlock(IDirectSoundCaptureBuffer8 *iface,
567 void *lpvAudioPtr1, DWORD dwAudioBytes1, void *lpvAudioPtr2, DWORD dwAudioBytes2)
569 IDirectSoundCaptureBufferImpl *This = impl_from_IDirectSoundCaptureBuffer8(iface);
570 HRESULT hres = DS_OK;
572 TRACE( "(%p,%p,%08u,%p,%08u)\n", This, lpvAudioPtr1, dwAudioBytes1,
573 lpvAudioPtr2, dwAudioBytes2 );
575 if (lpvAudioPtr1 == NULL) {
576 WARN("invalid parameter: lpvAudioPtr1 == NULL\n");
577 return DSERR_INVALIDPARAM;
580 if (!This->device->client) {
581 WARN("invalid call\n");
582 hres = DSERR_INVALIDCALL;
585 TRACE("returning %08x\n", hres);
586 return hres;
589 static HRESULT WINAPI IDirectSoundCaptureBufferImpl_GetObjectInPath(IDirectSoundCaptureBuffer8 *iface,
590 REFGUID rguidObject, DWORD dwIndex, REFGUID rguidInterface, void **ppObject)
592 IDirectSoundCaptureBufferImpl *This = impl_from_IDirectSoundCaptureBuffer8(iface);
594 FIXME( "(%p,%s,%u,%s,%p): stub\n", This, debugstr_guid(rguidObject),
595 dwIndex, debugstr_guid(rguidInterface), ppObject );
597 if (!ppObject)
598 return DSERR_INVALIDPARAM;
600 *ppObject = NULL;
601 return DSERR_CONTROLUNAVAIL;
604 static HRESULT WINAPI IDirectSoundCaptureBufferImpl_GetFXStatus(IDirectSoundCaptureBuffer8 *iface,
605 DWORD dwFXCount, DWORD *pdwFXStatus)
607 IDirectSoundCaptureBufferImpl *This = impl_from_IDirectSoundCaptureBuffer8(iface);
609 FIXME( "(%p,%u,%p): stub\n", This, dwFXCount, pdwFXStatus );
611 return DS_OK;
614 static const IDirectSoundCaptureBuffer8Vtbl dscbvt =
616 /* IUnknown methods */
617 IDirectSoundCaptureBufferImpl_QueryInterface,
618 IDirectSoundCaptureBufferImpl_AddRef,
619 IDirectSoundCaptureBufferImpl_Release,
621 /* IDirectSoundCaptureBuffer methods */
622 IDirectSoundCaptureBufferImpl_GetCaps,
623 IDirectSoundCaptureBufferImpl_GetCurrentPosition,
624 IDirectSoundCaptureBufferImpl_GetFormat,
625 IDirectSoundCaptureBufferImpl_GetStatus,
626 IDirectSoundCaptureBufferImpl_Initialize,
627 IDirectSoundCaptureBufferImpl_Lock,
628 IDirectSoundCaptureBufferImpl_Start,
629 IDirectSoundCaptureBufferImpl_Stop,
630 IDirectSoundCaptureBufferImpl_Unlock,
632 /* IDirectSoundCaptureBuffer methods */
633 IDirectSoundCaptureBufferImpl_GetObjectInPath,
634 IDirectSoundCaptureBufferImpl_GetFXStatus
637 static void capture_CheckNotify(IDirectSoundCaptureBufferImpl *This, DWORD from, DWORD len)
639 int i;
640 for (i = 0; i < This->nrofnotifies; ++i) {
641 LPDSBPOSITIONNOTIFY event = This->notifies + i;
642 DWORD offset = event->dwOffset;
643 TRACE("checking %d, position %d, event = %p\n", i, offset, event->hEventNotify);
645 if (offset == DSBPN_OFFSETSTOP) {
646 if (!from && !len) {
647 SetEvent(event->hEventNotify);
648 TRACE("signalled event %p (%d)\n", event->hEventNotify, i);
649 return;
651 else return;
654 if (offset >= from && offset < (from + len))
656 TRACE("signalled event %p (%d)\n", event->hEventNotify, i);
657 SetEvent(event->hEventNotify);
662 static HRESULT IDirectSoundCaptureBufferImpl_Create(
663 DirectSoundCaptureDevice *device,
664 IDirectSoundCaptureBufferImpl ** ppobj,
665 LPCDSCBUFFERDESC lpcDSCBufferDesc)
667 LPWAVEFORMATEX wfex;
668 IDirectSoundCaptureBufferImpl *This;
669 TRACE( "(%p,%p,%p)\n", device, ppobj, lpcDSCBufferDesc);
671 if (ppobj == NULL) {
672 WARN("invalid parameter: ppobj == NULL\n");
673 return DSERR_INVALIDPARAM;
676 *ppobj = NULL;
678 if (!device) {
679 WARN("not initialized\n");
680 return DSERR_UNINITIALIZED;
683 if (lpcDSCBufferDesc == NULL) {
684 WARN("invalid parameter: lpcDSCBufferDesc == NULL\n");
685 return DSERR_INVALIDPARAM;
688 if ( ((lpcDSCBufferDesc->dwSize != sizeof(DSCBUFFERDESC)) &&
689 (lpcDSCBufferDesc->dwSize != sizeof(DSCBUFFERDESC1))) ||
690 (lpcDSCBufferDesc->dwBufferBytes == 0) ||
691 (lpcDSCBufferDesc->lpwfxFormat == NULL) ) { /* FIXME: DSERR_BADFORMAT ? */
692 WARN("invalid lpcDSCBufferDesc\n");
693 return DSERR_INVALIDPARAM;
696 wfex = lpcDSCBufferDesc->lpwfxFormat;
698 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
699 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
700 wfex->wFormatTag, wfex->nChannels, wfex->nSamplesPerSec,
701 wfex->nAvgBytesPerSec, wfex->nBlockAlign,
702 wfex->wBitsPerSample, wfex->cbSize);
704 device->pwfx = DSOUND_CopyFormat(wfex);
705 if ( device->pwfx == NULL )
706 return DSERR_OUTOFMEMORY;
708 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,
709 sizeof(IDirectSoundCaptureBufferImpl));
711 if ( This == NULL ) {
712 WARN("out of memory\n");
713 return DSERR_OUTOFMEMORY;
714 } else {
715 HRESULT err = DS_OK;
716 LPBYTE newbuf;
717 DWORD buflen;
719 This->numIfaces = 0;
720 This->ref = 0;
721 This->refn = 0;
722 This->device = device;
723 This->device->capture_buffer = This;
724 This->nrofnotifies = 0;
726 This->pdscbd = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,
727 lpcDSCBufferDesc->dwSize);
728 if (This->pdscbd)
729 CopyMemory(This->pdscbd, lpcDSCBufferDesc, lpcDSCBufferDesc->dwSize);
730 else {
731 WARN("no memory\n");
732 This->device->capture_buffer = 0;
733 HeapFree( GetProcessHeap(), 0, This );
734 return DSERR_OUTOFMEMORY;
737 This->IDirectSoundCaptureBuffer8_iface.lpVtbl = &dscbvt;
738 This->IDirectSoundNotify_iface.lpVtbl = &dscnvt;
740 err = IMMDevice_Activate(device->mmdevice, &IID_IAudioClient,
741 CLSCTX_INPROC_SERVER, NULL, (void**)&device->client);
742 if(FAILED(err)){
743 WARN("Activate failed: %08x\n", err);
744 HeapFree(GetProcessHeap(), 0, This->pdscbd);
745 This->device->capture_buffer = 0;
746 HeapFree( GetProcessHeap(), 0, This );
747 return err;
750 err = IAudioClient_Initialize(device->client,
751 AUDCLNT_SHAREMODE_SHARED, AUDCLNT_STREAMFLAGS_NOPERSIST | AUDCLNT_STREAMFLAGS_EVENTCALLBACK,
752 200 * 100000, 0, device->pwfx, NULL);
753 if(FAILED(err)){
754 WARN("Initialize failed: %08x\n", err);
755 IAudioClient_Release(device->client);
756 device->client = NULL;
757 HeapFree(GetProcessHeap(), 0, This->pdscbd);
758 This->device->capture_buffer = 0;
759 HeapFree( GetProcessHeap(), 0, This );
760 if(err == AUDCLNT_E_UNSUPPORTED_FORMAT)
761 return DSERR_BADFORMAT;
762 return err;
765 This->sleepev = CreateEventW(NULL, 0, 0, NULL);
767 err = IAudioClient_SetEventHandle(device->client, This->sleepev);
768 if(FAILED(err)){
769 WARN("SetEventHandle failed: %08x\n", err);
770 IAudioClient_Release(device->client);
771 device->client = NULL;
772 CloseHandle(This->sleepev);
773 HeapFree(GetProcessHeap(), 0, This->pdscbd);
774 This->device->capture_buffer = 0;
775 HeapFree( GetProcessHeap(), 0, This );
776 return err;
779 err = IAudioClient_GetService(device->client, &IID_IAudioCaptureClient,
780 (void**)&device->capture);
781 if(FAILED(err)){
782 WARN("GetService failed: %08x\n", err);
783 IAudioClient_Release(device->client);
784 device->client = NULL;
785 CloseHandle(This->sleepev);
786 HeapFree(GetProcessHeap(), 0, This->pdscbd);
787 This->device->capture_buffer = 0;
788 HeapFree( GetProcessHeap(), 0, This );
789 return err;
792 buflen = lpcDSCBufferDesc->dwBufferBytes;
793 TRACE("desired buflen=%d, old buffer=%p\n", buflen, device->buffer);
794 if (device->buffer)
795 newbuf = HeapReAlloc(GetProcessHeap(),0,device->buffer,buflen);
796 else
797 newbuf = HeapAlloc(GetProcessHeap(),0,buflen);
798 if (newbuf == NULL) {
799 IAudioClient_Release(device->client);
800 device->client = NULL;
801 IAudioCaptureClient_Release(device->capture);
802 device->capture = NULL;
803 CloseHandle(This->sleepev);
804 HeapFree(GetProcessHeap(), 0, This->pdscbd);
805 This->device->capture_buffer = 0;
806 HeapFree( GetProcessHeap(), 0, This );
807 return DSERR_OUTOFMEMORY;
809 device->buffer = newbuf;
810 device->buflen = buflen;
811 This->thread = CreateThread(NULL, 0, DSOUND_capture_thread, This, 0, NULL);
814 IDirectSoundCaptureBuffer_AddRef(&This->IDirectSoundCaptureBuffer8_iface);
815 *ppobj = This;
817 TRACE("returning DS_OK\n");
818 return DS_OK;
822 /*******************************************************************************
823 * DirectSoundCaptureDevice
825 static HRESULT DirectSoundCaptureDevice_Create(
826 DirectSoundCaptureDevice ** ppDevice)
828 DirectSoundCaptureDevice * device;
829 TRACE("(%p)\n", ppDevice);
831 /* Allocate memory */
832 device = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DirectSoundCaptureDevice));
834 if (device == NULL) {
835 WARN("out of memory\n");
836 return DSERR_OUTOFMEMORY;
839 device->ref = 1;
840 device->state = STATE_STOPPED;
842 InitializeCriticalSection( &(device->lock) );
843 device->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": DirectSoundCaptureDevice.lock");
845 *ppDevice = device;
847 return DS_OK;
850 static ULONG DirectSoundCaptureDevice_Release(
851 DirectSoundCaptureDevice * device)
853 ULONG ref = InterlockedDecrement(&(device->ref));
854 TRACE("(%p) ref was %d\n", device, ref + 1);
856 if (!ref) {
857 TRACE("deleting object\n");
859 EnterCriticalSection(&DSOUND_capturers_lock);
860 list_remove(&device->entry);
861 LeaveCriticalSection(&DSOUND_capturers_lock);
863 if (device->capture_buffer)
864 IDirectSoundCaptureBufferImpl_Release(&device->capture_buffer->IDirectSoundCaptureBuffer8_iface);
866 if(device->mmdevice)
867 IMMDevice_Release(device->mmdevice);
868 HeapFree(GetProcessHeap(), 0, device->pwfx);
869 device->lock.DebugInfo->Spare[0] = 0;
870 DeleteCriticalSection( &(device->lock) );
871 HeapFree(GetProcessHeap(), 0, device);
872 TRACE("(%p) released\n", device);
874 return ref;
877 static HRESULT DSOUND_capture_data(DirectSoundCaptureDevice *device)
879 HRESULT hr;
880 UINT32 packet_frames, packet_bytes, avail_bytes, skip_bytes = 0;
881 DWORD flags;
882 BYTE *buf;
884 if(!device->capture_buffer || device->state == STATE_STOPPED)
885 return S_FALSE;
887 if(device->state == STATE_STOPPING){
888 device->state = STATE_STOPPED;
889 return S_FALSE;
892 if(device->state == STATE_STARTING)
893 device->state = STATE_CAPTURING;
895 hr = IAudioCaptureClient_GetBuffer(device->capture, &buf, &packet_frames,
896 &flags, NULL, NULL);
897 if(FAILED(hr)){
898 WARN("GetBuffer failed: %08x\n", hr);
899 return hr;
902 packet_bytes = packet_frames * device->pwfx->nBlockAlign;
903 if(packet_bytes > device->buflen){
904 TRACE("audio glitch: dsound buffer too small for data\n");
905 skip_bytes = packet_bytes - device->buflen;
906 packet_bytes = device->buflen;
909 avail_bytes = device->buflen - device->write_pos_bytes;
910 if(avail_bytes > packet_bytes)
911 avail_bytes = packet_bytes;
913 memcpy(device->buffer + device->write_pos_bytes, buf + skip_bytes, avail_bytes);
914 capture_CheckNotify(device->capture_buffer, device->write_pos_bytes, avail_bytes);
916 packet_bytes -= avail_bytes;
917 if(packet_bytes > 0){
918 if(device->capture_buffer->flags & DSCBSTART_LOOPING){
919 memcpy(device->buffer, buf + skip_bytes + avail_bytes, packet_bytes);
920 capture_CheckNotify(device->capture_buffer, 0, packet_bytes);
921 }else{
922 device->state = STATE_STOPPED;
923 capture_CheckNotify(device->capture_buffer, 0, 0);
927 device->write_pos_bytes += avail_bytes + packet_bytes;
928 device->write_pos_bytes %= device->buflen;
930 hr = IAudioCaptureClient_ReleaseBuffer(device->capture, packet_frames);
931 if(FAILED(hr)){
932 WARN("ReleaseBuffer failed: %08x\n", hr);
933 return hr;
936 return S_OK;
939 static DWORD WINAPI DSOUND_capture_thread(void *user)
941 IDirectSoundCaptureBufferImpl *buffer = user;
942 HRESULT hr;
943 DWORD ret, wait_ms;
944 REFERENCE_TIME period;
946 hr = IAudioClient_GetDevicePeriod(buffer->device->client, &period, NULL);
947 if(FAILED(hr)){
948 WARN("GetDevicePeriod failed: %08x\n", hr);
949 wait_ms = 5;
950 }else
951 wait_ms = MulDiv(5, period, 10000);
953 while(buffer->ref){
954 ret = WaitForSingleObject(buffer->sleepev, wait_ms);
956 if(!buffer->device->ref)
957 break;
959 if(ret == WAIT_OBJECT_0){
960 EnterCriticalSection(&buffer->device->lock);
962 DSOUND_capture_data(buffer->device);
964 LeaveCriticalSection(&buffer->device->lock);
965 }else if(ret != WAIT_TIMEOUT)
966 WARN("WaitForSingleObject failed: %u\n", GetLastError());
969 return 0;
972 static struct _TestFormat {
973 DWORD flag;
974 DWORD rate;
975 DWORD depth;
976 WORD channels;
977 } formats_to_test[] = {
978 { WAVE_FORMAT_1M08, 11025, 8, 1 },
979 { WAVE_FORMAT_1M16, 11025, 16, 1 },
980 { WAVE_FORMAT_1S08, 11025, 8, 2 },
981 { WAVE_FORMAT_1S16, 11025, 16, 2 },
982 { WAVE_FORMAT_2M08, 22050, 8, 1 },
983 { WAVE_FORMAT_2M16, 22050, 16, 1 },
984 { WAVE_FORMAT_2S08, 22050, 8, 2 },
985 { WAVE_FORMAT_2S16, 22050, 16, 2 },
986 { WAVE_FORMAT_4M08, 44100, 8, 1 },
987 { WAVE_FORMAT_4M16, 44100, 16, 1 },
988 { WAVE_FORMAT_4S08, 44100, 8, 2 },
989 { WAVE_FORMAT_4S16, 44100, 16, 2 },
990 { WAVE_FORMAT_48M08, 48000, 8, 1 },
991 { WAVE_FORMAT_48M16, 48000, 16, 1 },
992 { WAVE_FORMAT_48S08, 48000, 8, 2 },
993 { WAVE_FORMAT_48S16, 48000, 16, 2 },
994 { WAVE_FORMAT_96M08, 96000, 8, 1 },
995 { WAVE_FORMAT_96M16, 96000, 16, 1 },
996 { WAVE_FORMAT_96S08, 96000, 8, 2 },
997 { WAVE_FORMAT_96S16, 96000, 16, 2 },
1001 static HRESULT DirectSoundCaptureDevice_Initialize(
1002 DirectSoundCaptureDevice ** ppDevice,
1003 LPCGUID lpcGUID)
1005 HRESULT hr;
1006 GUID devGUID;
1007 IMMDevice *mmdevice;
1008 struct _TestFormat *fmt;
1009 DirectSoundCaptureDevice *device;
1010 IAudioClient *client;
1012 TRACE("(%p, %s)\n", ppDevice, debugstr_guid(lpcGUID));
1014 /* Default device? */
1015 if ( !lpcGUID || IsEqualGUID(lpcGUID, &GUID_NULL) )
1016 lpcGUID = &DSDEVID_DefaultCapture;
1018 if(IsEqualGUID(lpcGUID, &DSDEVID_DefaultPlayback) ||
1019 IsEqualGUID(lpcGUID, &DSDEVID_DefaultVoicePlayback))
1020 return DSERR_NODRIVER;
1022 if (GetDeviceID(lpcGUID, &devGUID) != DS_OK) {
1023 WARN("invalid parameter: lpcGUID\n");
1024 return DSERR_INVALIDPARAM;
1027 hr = get_mmdevice(eCapture, &devGUID, &mmdevice);
1028 if(FAILED(hr))
1029 return hr;
1031 EnterCriticalSection(&DSOUND_capturers_lock);
1033 hr = DirectSoundCaptureDevice_Create(&device);
1034 if (hr != DS_OK) {
1035 WARN("DirectSoundCaptureDevice_Create failed\n");
1036 LeaveCriticalSection(&DSOUND_capturers_lock);
1037 return hr;
1040 device->guid = devGUID;
1042 device->mmdevice = mmdevice;
1044 device->drvcaps.dwFlags = 0;
1046 device->drvcaps.dwFormats = 0;
1047 device->drvcaps.dwChannels = 0;
1048 hr = IMMDevice_Activate(mmdevice, &IID_IAudioClient,
1049 CLSCTX_INPROC_SERVER, NULL, (void**)&client);
1050 if(FAILED(hr)){
1051 device->lock.DebugInfo->Spare[0] = 0;
1052 DeleteCriticalSection(&device->lock);
1053 HeapFree(GetProcessHeap(), 0, device);
1054 LeaveCriticalSection(&DSOUND_capturers_lock);
1055 return DSERR_NODRIVER;
1058 for(fmt = formats_to_test; fmt->flag; ++fmt){
1059 if(DSOUND_check_supported(client, fmt->rate, fmt->depth, fmt->channels)){
1060 device->drvcaps.dwFormats |= fmt->flag;
1061 if(fmt->channels > device->drvcaps.dwChannels)
1062 device->drvcaps.dwChannels = fmt->channels;
1065 IAudioClient_Release(client);
1067 list_add_tail(&DSOUND_capturers, &device->entry);
1069 *ppDevice = device;
1071 LeaveCriticalSection(&DSOUND_capturers_lock);
1073 return S_OK;
1077 /*****************************************************************************
1078 * IDirectSoundCapture implementation structure
1080 typedef struct IDirectSoundCaptureImpl
1082 IUnknown IUnknown_inner;
1083 IDirectSoundCapture IDirectSoundCapture_iface;
1084 LONG ref, refdsc, numIfaces;
1085 IUnknown *outer_unk; /* internal */
1086 DirectSoundCaptureDevice *device;
1087 BOOL has_dsc8;
1088 } IDirectSoundCaptureImpl;
1090 static void capture_destroy(IDirectSoundCaptureImpl *This)
1092 if (This->device)
1093 DirectSoundCaptureDevice_Release(This->device);
1094 HeapFree(GetProcessHeap(),0,This);
1095 TRACE("(%p) released\n", This);
1098 /*******************************************************************************
1099 * IUnknown Implementation for DirectSoundCapture
1101 static inline IDirectSoundCaptureImpl *impl_from_IUnknown(IUnknown *iface)
1103 return CONTAINING_RECORD(iface, IDirectSoundCaptureImpl, IUnknown_inner);
1106 static HRESULT WINAPI IUnknownImpl_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
1108 IDirectSoundCaptureImpl *This = impl_from_IUnknown(iface);
1110 TRACE("(%p,%s,%p)\n", This, debugstr_guid(riid), ppv);
1112 if (!ppv) {
1113 WARN("invalid parameter\n");
1114 return E_INVALIDARG;
1116 *ppv = NULL;
1118 if (IsEqualIID(riid, &IID_IUnknown))
1119 *ppv = &This->IUnknown_inner;
1120 else if (IsEqualIID(riid, &IID_IDirectSoundCapture))
1121 *ppv = &This->IDirectSoundCapture_iface;
1122 else {
1123 WARN("unknown IID %s\n", debugstr_guid(riid));
1124 return E_NOINTERFACE;
1127 IUnknown_AddRef((IUnknown*)*ppv);
1128 return S_OK;
1131 static ULONG WINAPI IUnknownImpl_AddRef(IUnknown *iface)
1133 IDirectSoundCaptureImpl *This = impl_from_IUnknown(iface);
1134 ULONG ref = InterlockedIncrement(&This->ref);
1136 TRACE("(%p) ref=%d\n", This, ref);
1138 if(ref == 1)
1139 InterlockedIncrement(&This->numIfaces);
1140 return ref;
1143 static ULONG WINAPI IUnknownImpl_Release(IUnknown *iface)
1145 IDirectSoundCaptureImpl *This = impl_from_IUnknown(iface);
1146 ULONG ref = InterlockedDecrement(&This->ref);
1148 TRACE("(%p) ref=%d\n", This, ref);
1150 if (!ref && !InterlockedDecrement(&This->numIfaces))
1151 capture_destroy(This);
1152 return ref;
1155 static const IUnknownVtbl unk_vtbl =
1157 IUnknownImpl_QueryInterface,
1158 IUnknownImpl_AddRef,
1159 IUnknownImpl_Release
1162 /***************************************************************************
1163 * IDirectSoundCaptureImpl
1165 static inline struct IDirectSoundCaptureImpl *impl_from_IDirectSoundCapture(IDirectSoundCapture *iface)
1167 return CONTAINING_RECORD(iface, struct IDirectSoundCaptureImpl, IDirectSoundCapture_iface);
1170 static HRESULT WINAPI IDirectSoundCaptureImpl_QueryInterface(IDirectSoundCapture *iface,
1171 REFIID riid, void **ppv)
1173 IDirectSoundCaptureImpl *This = impl_from_IDirectSoundCapture(iface);
1174 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(riid), ppv);
1175 return IUnknown_QueryInterface(This->outer_unk, riid, ppv);
1178 static ULONG WINAPI IDirectSoundCaptureImpl_AddRef(IDirectSoundCapture *iface)
1180 IDirectSoundCaptureImpl *This = impl_from_IDirectSoundCapture(iface);
1181 ULONG ref = InterlockedIncrement(&This->refdsc);
1183 TRACE("(%p) ref=%d\n", This, ref);
1185 if(ref == 1)
1186 InterlockedIncrement(&This->numIfaces);
1187 return ref;
1190 static ULONG WINAPI IDirectSoundCaptureImpl_Release(IDirectSoundCapture *iface)
1192 IDirectSoundCaptureImpl *This = impl_from_IDirectSoundCapture(iface);
1193 ULONG ref = InterlockedDecrement(&This->refdsc);
1195 TRACE("(%p) ref=%d\n", This, ref);
1197 if (!ref && !InterlockedDecrement(&This->numIfaces))
1198 capture_destroy(This);
1199 return ref;
1202 static HRESULT WINAPI IDirectSoundCaptureImpl_CreateCaptureBuffer(IDirectSoundCapture *iface,
1203 LPCDSCBUFFERDESC lpcDSCBufferDesc, IDirectSoundCaptureBuffer **lplpDSCaptureBuffer,
1204 IUnknown *pUnk)
1206 IDirectSoundCaptureImpl *This = impl_from_IDirectSoundCapture(iface);
1207 HRESULT hr;
1209 TRACE( "(%p,%p,%p,%p)\n",iface,lpcDSCBufferDesc,lplpDSCaptureBuffer,pUnk);
1211 if (pUnk) {
1212 WARN("invalid parameter: pUnk != NULL\n");
1213 return DSERR_NOAGGREGATION;
1216 if (lpcDSCBufferDesc == NULL) {
1217 WARN("invalid parameter: lpcDSCBufferDesc == NULL)\n");
1218 return DSERR_INVALIDPARAM;
1221 if (lplpDSCaptureBuffer == NULL) {
1222 WARN("invalid parameter: lplpDSCaptureBuffer == NULL\n");
1223 return DSERR_INVALIDPARAM;
1226 /* FIXME: We can only have one buffer so what do we do here? */
1227 if (This->device->capture_buffer) {
1228 WARN("invalid parameter: already has buffer\n");
1229 return DSERR_INVALIDPARAM; /* DSERR_GENERIC ? */
1232 hr = IDirectSoundCaptureBufferImpl_Create(This->device,
1233 (IDirectSoundCaptureBufferImpl **)lplpDSCaptureBuffer, lpcDSCBufferDesc);
1235 if (hr != DS_OK)
1236 WARN("IDirectSoundCaptureBufferImpl_Create failed\n");
1237 else
1238 This->device->capture_buffer->has_dsc8 = This->has_dsc8;
1240 return hr;
1243 static HRESULT WINAPI IDirectSoundCaptureImpl_GetCaps(IDirectSoundCapture *iface,
1244 LPDSCCAPS lpDSCCaps)
1246 IDirectSoundCaptureImpl *This = impl_from_IDirectSoundCapture(iface);
1248 TRACE("(%p,%p)\n",This,lpDSCCaps);
1250 if (This->device == NULL) {
1251 WARN("not initialized\n");
1252 return DSERR_UNINITIALIZED;
1255 if (lpDSCCaps== NULL) {
1256 WARN("invalid parameter: lpDSCCaps== NULL\n");
1257 return DSERR_INVALIDPARAM;
1260 if (lpDSCCaps->dwSize < sizeof(*lpDSCCaps)) {
1261 WARN("invalid parameter: lpDSCCaps->dwSize = %d\n", lpDSCCaps->dwSize);
1262 return DSERR_INVALIDPARAM;
1265 lpDSCCaps->dwFlags = This->device->drvcaps.dwFlags;
1266 lpDSCCaps->dwFormats = This->device->drvcaps.dwFormats;
1267 lpDSCCaps->dwChannels = This->device->drvcaps.dwChannels;
1269 TRACE("(flags=0x%08x,format=0x%08x,channels=%d)\n",lpDSCCaps->dwFlags,
1270 lpDSCCaps->dwFormats, lpDSCCaps->dwChannels);
1272 return DS_OK;
1275 static HRESULT WINAPI IDirectSoundCaptureImpl_Initialize(IDirectSoundCapture *iface,
1276 LPCGUID lpcGUID)
1278 IDirectSoundCaptureImpl *This = impl_from_IDirectSoundCapture(iface);
1280 TRACE("(%p,%s)\n", This, debugstr_guid(lpcGUID));
1282 if (This->device != NULL) {
1283 WARN("already initialized\n");
1284 return DSERR_ALREADYINITIALIZED;
1286 return DirectSoundCaptureDevice_Initialize(&This->device, lpcGUID);
1289 static const IDirectSoundCaptureVtbl dscvt =
1291 /* IUnknown methods */
1292 IDirectSoundCaptureImpl_QueryInterface,
1293 IDirectSoundCaptureImpl_AddRef,
1294 IDirectSoundCaptureImpl_Release,
1296 /* IDirectSoundCapture methods */
1297 IDirectSoundCaptureImpl_CreateCaptureBuffer,
1298 IDirectSoundCaptureImpl_GetCaps,
1299 IDirectSoundCaptureImpl_Initialize
1302 HRESULT IDirectSoundCaptureImpl_Create(IUnknown *outer_unk, REFIID riid, void **ppv, BOOL has_dsc8)
1304 IDirectSoundCaptureImpl *obj;
1305 HRESULT hr;
1307 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
1309 *ppv = NULL;
1310 obj = HeapAlloc(GetProcessHeap(), 0, sizeof(*obj));
1311 if (obj == NULL) {
1312 WARN("out of memory\n");
1313 return DSERR_OUTOFMEMORY;
1316 setup_dsound_options();
1318 obj->IUnknown_inner.lpVtbl = &unk_vtbl;
1319 obj->IDirectSoundCapture_iface.lpVtbl = &dscvt;
1320 obj->ref = 1;
1321 obj->refdsc = 0;
1322 obj->numIfaces = 1;
1323 obj->device = NULL;
1324 obj->has_dsc8 = has_dsc8;
1326 /* COM aggregation supported only internally */
1327 if (outer_unk)
1328 obj->outer_unk = outer_unk;
1329 else
1330 obj->outer_unk = &obj->IUnknown_inner;
1332 hr = IUnknown_QueryInterface(&obj->IUnknown_inner, riid, ppv);
1333 IUnknown_Release(&obj->IUnknown_inner);
1335 return hr;
1338 HRESULT DSOUND_CaptureCreate(REFIID riid, void **ppv)
1340 return IDirectSoundCaptureImpl_Create(NULL, riid, ppv, FALSE);
1343 HRESULT DSOUND_CaptureCreate8(REFIID riid, void **ppv)
1345 return IDirectSoundCaptureImpl_Create(NULL, riid, ppv, TRUE);
1348 /***************************************************************************
1349 * DirectSoundCaptureCreate [DSOUND.6]
1351 * Create and initialize a DirectSoundCapture interface.
1353 * PARAMS
1354 * lpcGUID [I] Address of the GUID that identifies the sound capture device.
1355 * lplpDSC [O] Address of a variable to receive the interface pointer.
1356 * pUnkOuter [I] Must be NULL.
1358 * RETURNS
1359 * Success: DS_OK
1360 * Failure: DSERR_NOAGGREGATION, DSERR_ALLOCATED, DSERR_INVALIDPARAM,
1361 * DSERR_OUTOFMEMORY
1363 * NOTES
1364 * lpcGUID must be one of the values returned from DirectSoundCaptureEnumerate
1365 * or NULL for the default device or DSDEVID_DefaultCapture or
1366 * DSDEVID_DefaultVoiceCapture.
1368 * DSERR_ALLOCATED is returned for sound devices that do not support full duplex.
1370 HRESULT WINAPI DirectSoundCaptureCreate(LPCGUID lpcGUID, IDirectSoundCapture **ppDSC,
1371 IUnknown *pUnkOuter)
1373 HRESULT hr;
1374 IDirectSoundCapture *pDSC;
1376 TRACE("(%s,%p,%p)\n", debugstr_guid(lpcGUID), ppDSC, pUnkOuter);
1378 if (ppDSC == NULL) {
1379 WARN("invalid parameter: ppDSC == NULL\n");
1380 return DSERR_INVALIDPARAM;
1383 if (pUnkOuter) {
1384 WARN("invalid parameter: pUnkOuter != NULL\n");
1385 return DSERR_NOAGGREGATION;
1388 hr = DSOUND_CaptureCreate(&IID_IDirectSoundCapture, (void**)&pDSC);
1389 if (hr == DS_OK) {
1390 hr = IDirectSoundCapture_Initialize(pDSC, lpcGUID);
1391 if (hr != DS_OK) {
1392 IDirectSoundCapture_Release(pDSC);
1393 pDSC = 0;
1397 *ppDSC = pDSC;
1399 return hr;
1402 /***************************************************************************
1403 * DirectSoundCaptureCreate8 [DSOUND.12]
1405 * Create and initialize a DirectSoundCapture interface.
1407 * PARAMS
1408 * lpcGUID [I] Address of the GUID that identifies the sound capture device.
1409 * lplpDSC [O] Address of a variable to receive the interface pointer.
1410 * pUnkOuter [I] Must be NULL.
1412 * RETURNS
1413 * Success: DS_OK
1414 * Failure: DSERR_NOAGGREGATION, DSERR_ALLOCATED, DSERR_INVALIDPARAM,
1415 * DSERR_OUTOFMEMORY
1417 * NOTES
1418 * lpcGUID must be one of the values returned from DirectSoundCaptureEnumerate
1419 * or NULL for the default device or DSDEVID_DefaultCapture or
1420 * DSDEVID_DefaultVoiceCapture.
1422 * DSERR_ALLOCATED is returned for sound devices that do not support full duplex.
1424 HRESULT WINAPI DirectSoundCaptureCreate8(
1425 LPCGUID lpcGUID,
1426 LPDIRECTSOUNDCAPTURE8 *ppDSC8,
1427 LPUNKNOWN pUnkOuter)
1429 HRESULT hr;
1430 LPDIRECTSOUNDCAPTURE8 pDSC8;
1431 TRACE("(%s,%p,%p)\n", debugstr_guid(lpcGUID), ppDSC8, pUnkOuter);
1433 if (ppDSC8 == NULL) {
1434 WARN("invalid parameter: ppDSC8 == NULL\n");
1435 return DSERR_INVALIDPARAM;
1438 if (pUnkOuter) {
1439 WARN("invalid parameter: pUnkOuter != NULL\n");
1440 *ppDSC8 = NULL;
1441 return DSERR_NOAGGREGATION;
1444 hr = DSOUND_CaptureCreate8(&IID_IDirectSoundCapture8, (void**)&pDSC8);
1445 if (hr == DS_OK) {
1446 hr = IDirectSoundCapture_Initialize(pDSC8, lpcGUID);
1447 if (hr != DS_OK) {
1448 IDirectSoundCapture_Release(pDSC8);
1449 pDSC8 = 0;
1453 *ppDSC8 = pDSC8;
1455 return hr;