twain_32/tests: Compile with -D__WINESRC__.
[wine/multimedia.git] / dlls / dsound / capture.c
blob40f1702b64630ddb2fad254487dbd0df9c381795
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
31 #define NONAMELESSSTRUCT
32 #define NONAMELESSUNION
33 #include "windef.h"
34 #include "winbase.h"
35 #include "winuser.h"
36 #include "mmsystem.h"
37 #include "mmddk.h"
38 #include "winternl.h"
39 #include "winnls.h"
40 #include "wine/debug.h"
41 #include "dsound.h"
42 #include "dsound_private.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(dsound);
46 typedef struct DirectSoundCaptureDevice DirectSoundCaptureDevice;
48 /* IDirectSoundCaptureBuffer implementation structure */
49 typedef struct IDirectSoundCaptureBufferImpl
51 IDirectSoundCaptureBuffer8 IDirectSoundCaptureBuffer8_iface;
52 IDirectSoundNotify IDirectSoundNotify_iface;
53 LONG numIfaces; /* "in use interfaces" refcount */
54 LONG ref, refn;
55 /* IDirectSoundCaptureBuffer fields */
56 DirectSoundCaptureDevice *device;
57 DSCBUFFERDESC *pdscbd;
58 DWORD flags;
59 /* IDirectSoundNotify fields */
60 DSBPOSITIONNOTIFY *notifies;
61 int nrofnotifies;
62 HANDLE thread;
63 HANDLE sleepev;
64 } IDirectSoundCaptureBufferImpl;
66 /* DirectSoundCaptureDevice implementation structure */
67 struct DirectSoundCaptureDevice
69 GUID guid;
70 LONG ref;
71 DSCCAPS drvcaps;
72 BYTE *buffer;
73 DWORD buflen, write_pos_bytes;
74 WAVEFORMATEX *pwfx;
75 IDirectSoundCaptureBufferImpl *capture_buffer;
76 DWORD state;
77 CRITICAL_SECTION lock;
78 IMMDevice *mmdevice;
79 IAudioClient *client;
80 IAudioCaptureClient *capture;
81 struct list entry;
84 static DWORD WINAPI DSOUND_capture_thread(void *user);
86 static void capturebuffer_destroy(IDirectSoundCaptureBufferImpl *This)
88 if (This->device->state == STATE_CAPTURING)
89 This->device->state = STATE_STOPPING;
91 if(This->thread){
92 SetEvent(This->sleepev);
93 WaitForSingleObject(This->thread, INFINITE);
94 CloseHandle(This->thread);
96 CloseHandle(This->sleepev);
98 HeapFree(GetProcessHeap(),0, This->pdscbd);
100 if (This->device->client) {
101 IAudioClient_Release(This->device->client);
102 This->device->client = NULL;
105 if (This->device->capture) {
106 IAudioCaptureClient_Release(This->device->capture);
107 This->device->capture = NULL;
110 /* remove from DirectSoundCaptureDevice */
111 This->device->capture_buffer = NULL;
113 HeapFree(GetProcessHeap(), 0, This->notifies);
114 HeapFree(GetProcessHeap(), 0, This);
115 TRACE("(%p) released\n", This);
118 /*******************************************************************************
119 * IDirectSoundNotify
121 static inline struct IDirectSoundCaptureBufferImpl *impl_from_IDirectSoundNotify(IDirectSoundNotify *iface)
123 return CONTAINING_RECORD(iface, IDirectSoundCaptureBufferImpl, IDirectSoundNotify_iface);
126 static HRESULT WINAPI IDirectSoundNotifyImpl_QueryInterface(IDirectSoundNotify *iface, REFIID riid,
127 void **ppobj)
129 IDirectSoundCaptureBufferImpl *This = impl_from_IDirectSoundNotify(iface);
131 TRACE("(%p,%s,%p)\n", This, debugstr_guid(riid), ppobj);
133 return IDirectSoundCaptureBuffer_QueryInterface(&This->IDirectSoundCaptureBuffer8_iface, riid, ppobj);
136 static ULONG WINAPI IDirectSoundNotifyImpl_AddRef(IDirectSoundNotify *iface)
138 IDirectSoundCaptureBufferImpl *This = impl_from_IDirectSoundNotify(iface);
139 ULONG ref = InterlockedIncrement(&This->refn);
141 TRACE("(%p) ref was %d\n", This, ref - 1);
143 if(ref == 1)
144 InterlockedIncrement(&This->numIfaces);
146 return ref;
149 static ULONG WINAPI IDirectSoundNotifyImpl_Release(IDirectSoundNotify *iface)
151 IDirectSoundCaptureBufferImpl *This = impl_from_IDirectSoundNotify(iface);
152 ULONG ref = InterlockedDecrement(&This->refn);
154 TRACE("(%p) ref was %d\n", This, ref + 1);
156 if (!ref && !InterlockedDecrement(&This->numIfaces))
157 capturebuffer_destroy(This);
159 return ref;
162 static HRESULT WINAPI IDirectSoundNotifyImpl_SetNotificationPositions(IDirectSoundNotify *iface,
163 DWORD howmuch, const DSBPOSITIONNOTIFY *notify)
165 IDirectSoundCaptureBufferImpl *This = impl_from_IDirectSoundNotify(iface);
166 TRACE("(%p,0x%08x,%p)\n",This,howmuch,notify);
168 if (howmuch > 0 && notify == NULL) {
169 WARN("invalid parameter: notify == NULL\n");
170 return DSERR_INVALIDPARAM;
173 if (TRACE_ON(dsound)) {
174 unsigned int i;
175 for (i=0;i<howmuch;i++)
176 TRACE("notify at %d to %p\n",
177 notify[i].dwOffset,notify[i].hEventNotify);
180 if (howmuch > 0) {
181 /* Make an internal copy of the caller-supplied array.
182 * Replace the existing copy if one is already present. */
183 if (This->notifies)
184 This->notifies = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->notifies,
185 howmuch * sizeof(DSBPOSITIONNOTIFY));
186 else
187 This->notifies = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
188 howmuch * sizeof(DSBPOSITIONNOTIFY));
190 if (!This->notifies) {
191 WARN("out of memory\n");
192 return DSERR_OUTOFMEMORY;
194 CopyMemory(This->notifies, notify, howmuch * sizeof(DSBPOSITIONNOTIFY));
195 This->nrofnotifies = howmuch;
196 } else {
197 HeapFree(GetProcessHeap(), 0, This->notifies);
198 This->notifies = NULL;
199 This->nrofnotifies = 0;
202 return S_OK;
205 static const IDirectSoundNotifyVtbl dscnvt =
207 IDirectSoundNotifyImpl_QueryInterface,
208 IDirectSoundNotifyImpl_AddRef,
209 IDirectSoundNotifyImpl_Release,
210 IDirectSoundNotifyImpl_SetNotificationPositions
214 static const char * const captureStateString[] = {
215 "STATE_STOPPED",
216 "STATE_STARTING",
217 "STATE_CAPTURING",
218 "STATE_STOPPING"
222 /*******************************************************************************
223 * IDirectSoundCaptureBuffer
225 static inline IDirectSoundCaptureBufferImpl *impl_from_IDirectSoundCaptureBuffer8(IDirectSoundCaptureBuffer8 *iface)
227 return CONTAINING_RECORD(iface, IDirectSoundCaptureBufferImpl, IDirectSoundCaptureBuffer8_iface);
230 static HRESULT WINAPI IDirectSoundCaptureBufferImpl_QueryInterface(IDirectSoundCaptureBuffer8 *iface,
231 REFIID riid, void **ppobj)
233 IDirectSoundCaptureBufferImpl *This = impl_from_IDirectSoundCaptureBuffer8(iface);
235 TRACE( "(%p,%s,%p)\n", This, debugstr_guid(riid), ppobj );
237 if (ppobj == NULL) {
238 WARN("invalid parameter\n");
239 return E_INVALIDARG;
242 *ppobj = NULL;
244 if ( IsEqualGUID( &IID_IDirectSoundCaptureBuffer, riid ) ||
245 IsEqualGUID( &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 if (pUnk != NULL) {
1227 WARN("invalid parameter: pUnk != NULL\n");
1228 return DSERR_INVALIDPARAM;
1231 /* FIXME: We can only have one buffer so what do we do here? */
1232 if (This->device->capture_buffer) {
1233 WARN("invalid parameter: already has buffer\n");
1234 return DSERR_INVALIDPARAM; /* DSERR_GENERIC ? */
1237 hr = IDirectSoundCaptureBufferImpl_Create(This->device,
1238 (IDirectSoundCaptureBufferImpl **)lplpDSCaptureBuffer, lpcDSCBufferDesc);
1240 if (hr != DS_OK)
1241 WARN("IDirectSoundCaptureBufferImpl_Create failed\n");
1243 return hr;
1246 static HRESULT WINAPI IDirectSoundCaptureImpl_GetCaps(IDirectSoundCapture *iface,
1247 LPDSCCAPS lpDSCCaps)
1249 IDirectSoundCaptureImpl *This = impl_from_IDirectSoundCapture(iface);
1251 TRACE("(%p,%p)\n",This,lpDSCCaps);
1253 if (This->device == NULL) {
1254 WARN("not initialized\n");
1255 return DSERR_UNINITIALIZED;
1258 if (lpDSCCaps== NULL) {
1259 WARN("invalid parameter: lpDSCCaps== NULL\n");
1260 return DSERR_INVALIDPARAM;
1263 if (lpDSCCaps->dwSize < sizeof(*lpDSCCaps)) {
1264 WARN("invalid parameter: lpDSCCaps->dwSize = %d\n", lpDSCCaps->dwSize);
1265 return DSERR_INVALIDPARAM;
1268 lpDSCCaps->dwFlags = This->device->drvcaps.dwFlags;
1269 lpDSCCaps->dwFormats = This->device->drvcaps.dwFormats;
1270 lpDSCCaps->dwChannels = This->device->drvcaps.dwChannels;
1272 TRACE("(flags=0x%08x,format=0x%08x,channels=%d)\n",lpDSCCaps->dwFlags,
1273 lpDSCCaps->dwFormats, lpDSCCaps->dwChannels);
1275 return DS_OK;
1278 static HRESULT WINAPI IDirectSoundCaptureImpl_Initialize(IDirectSoundCapture *iface,
1279 LPCGUID lpcGUID)
1281 IDirectSoundCaptureImpl *This = impl_from_IDirectSoundCapture(iface);
1283 TRACE("(%p,%s)\n", This, debugstr_guid(lpcGUID));
1285 if (This->device != NULL) {
1286 WARN("already initialized\n");
1287 return DSERR_ALREADYINITIALIZED;
1289 return DirectSoundCaptureDevice_Initialize(&This->device, lpcGUID);
1292 static const IDirectSoundCaptureVtbl dscvt =
1294 /* IUnknown methods */
1295 IDirectSoundCaptureImpl_QueryInterface,
1296 IDirectSoundCaptureImpl_AddRef,
1297 IDirectSoundCaptureImpl_Release,
1299 /* IDirectSoundCapture methods */
1300 IDirectSoundCaptureImpl_CreateCaptureBuffer,
1301 IDirectSoundCaptureImpl_GetCaps,
1302 IDirectSoundCaptureImpl_Initialize
1305 HRESULT IDirectSoundCaptureImpl_Create(IUnknown *outer_unk, REFIID riid, void **ppv, BOOL has_dsc8)
1307 IDirectSoundCaptureImpl *obj;
1308 HRESULT hr;
1310 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
1312 *ppv = NULL;
1313 obj = HeapAlloc(GetProcessHeap(), 0, sizeof(*obj));
1314 if (obj == NULL) {
1315 WARN("out of memory\n");
1316 return DSERR_OUTOFMEMORY;
1319 setup_dsound_options();
1321 obj->IUnknown_inner.lpVtbl = &unk_vtbl;
1322 obj->IDirectSoundCapture_iface.lpVtbl = &dscvt;
1323 obj->ref = 1;
1324 obj->refdsc = 0;
1325 obj->numIfaces = 1;
1326 obj->device = NULL;
1327 obj->has_dsc8 = has_dsc8;
1329 /* COM aggregation supported only internally */
1330 if (outer_unk)
1331 obj->outer_unk = outer_unk;
1332 else
1333 obj->outer_unk = &obj->IUnknown_inner;
1335 hr = IUnknown_QueryInterface(&obj->IUnknown_inner, riid, ppv);
1336 IUnknown_Release(&obj->IUnknown_inner);
1338 return hr;
1341 HRESULT DSOUND_CaptureCreate(REFIID riid, void **ppv)
1343 return IDirectSoundCaptureImpl_Create(NULL, riid, ppv, FALSE);
1346 HRESULT DSOUND_CaptureCreate8(REFIID riid, void **ppv)
1348 return IDirectSoundCaptureImpl_Create(NULL, riid, ppv, TRUE);
1351 /***************************************************************************
1352 * DirectSoundCaptureCreate [DSOUND.6]
1354 * Create and initialize a DirectSoundCapture interface.
1356 * PARAMS
1357 * lpcGUID [I] Address of the GUID that identifies the sound capture device.
1358 * lplpDSC [O] Address of a variable to receive the interface pointer.
1359 * pUnkOuter [I] Must be NULL.
1361 * RETURNS
1362 * Success: DS_OK
1363 * Failure: DSERR_NOAGGREGATION, DSERR_ALLOCATED, DSERR_INVALIDPARAM,
1364 * DSERR_OUTOFMEMORY
1366 * NOTES
1367 * lpcGUID must be one of the values returned from DirectSoundCaptureEnumerate
1368 * or NULL for the default device or DSDEVID_DefaultCapture or
1369 * DSDEVID_DefaultVoiceCapture.
1371 * DSERR_ALLOCATED is returned for sound devices that do not support full duplex.
1373 HRESULT WINAPI DirectSoundCaptureCreate(LPCGUID lpcGUID, IDirectSoundCapture **ppDSC,
1374 IUnknown *pUnkOuter)
1376 HRESULT hr;
1377 IDirectSoundCapture *pDSC;
1379 TRACE("(%s,%p,%p)\n", debugstr_guid(lpcGUID), ppDSC, pUnkOuter);
1381 if (ppDSC == NULL) {
1382 WARN("invalid parameter: ppDSC == NULL\n");
1383 return DSERR_INVALIDPARAM;
1386 if (pUnkOuter) {
1387 WARN("invalid parameter: pUnkOuter != NULL\n");
1388 return DSERR_NOAGGREGATION;
1391 hr = DSOUND_CaptureCreate(&IID_IDirectSoundCapture, (void**)&pDSC);
1392 if (hr == DS_OK) {
1393 hr = IDirectSoundCapture_Initialize(pDSC, lpcGUID);
1394 if (hr != DS_OK) {
1395 IDirectSoundCapture_Release(pDSC);
1396 pDSC = 0;
1400 *ppDSC = pDSC;
1402 return hr;
1405 /***************************************************************************
1406 * DirectSoundCaptureCreate8 [DSOUND.12]
1408 * Create and initialize a DirectSoundCapture interface.
1410 * PARAMS
1411 * lpcGUID [I] Address of the GUID that identifies the sound capture device.
1412 * lplpDSC [O] Address of a variable to receive the interface pointer.
1413 * pUnkOuter [I] Must be NULL.
1415 * RETURNS
1416 * Success: DS_OK
1417 * Failure: DSERR_NOAGGREGATION, DSERR_ALLOCATED, DSERR_INVALIDPARAM,
1418 * DSERR_OUTOFMEMORY
1420 * NOTES
1421 * lpcGUID must be one of the values returned from DirectSoundCaptureEnumerate
1422 * or NULL for the default device or DSDEVID_DefaultCapture or
1423 * DSDEVID_DefaultVoiceCapture.
1425 * DSERR_ALLOCATED is returned for sound devices that do not support full duplex.
1427 HRESULT WINAPI DirectSoundCaptureCreate8(
1428 LPCGUID lpcGUID,
1429 LPDIRECTSOUNDCAPTURE8 *ppDSC8,
1430 LPUNKNOWN pUnkOuter)
1432 HRESULT hr;
1433 LPDIRECTSOUNDCAPTURE8 pDSC8;
1434 TRACE("(%s,%p,%p)\n", debugstr_guid(lpcGUID), ppDSC8, pUnkOuter);
1436 if (ppDSC8 == NULL) {
1437 WARN("invalid parameter: ppDSC8 == NULL\n");
1438 return DSERR_INVALIDPARAM;
1441 if (pUnkOuter) {
1442 WARN("invalid parameter: pUnkOuter != NULL\n");
1443 *ppDSC8 = NULL;
1444 return DSERR_NOAGGREGATION;
1447 hr = DSOUND_CaptureCreate8(&IID_IDirectSoundCapture8, (void**)&pDSC8);
1448 if (hr == DS_OK) {
1449 hr = IDirectSoundCapture_Initialize(pDSC8, lpcGUID);
1450 if (hr != DS_OK) {
1451 IDirectSoundCapture_Release(pDSC8);
1452 pDSC8 = 0;
1456 *ppDSC8 = pDSC8;
1458 return hr;