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
23 * Implement FX support.
24 * Implement both IDirectSoundCaptureBuffer and IDirectSoundCaptureBuffer8
25 * Make DirectSoundCaptureCreate and DirectSoundCaptureCreate8 behave differently
30 #define NONAMELESSSTRUCT
31 #define NONAMELESSUNION
39 #include "wine/debug.h"
42 #include "dsound_private.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(dsound);
46 /*****************************************************************************
47 * IDirectSoundCapture implementation structure
49 struct IDirectSoundCaptureImpl
52 const IDirectSoundCaptureVtbl *lpVtbl;
55 DirectSoundCaptureDevice *device;
58 static HRESULT IDirectSoundCaptureImpl_Create(LPDIRECTSOUNDCAPTURE8 * ppds);
61 /*****************************************************************************
62 * IDirectSoundCaptureNotify implementation structure
64 struct IDirectSoundCaptureNotifyImpl
67 const IDirectSoundNotifyVtbl *lpVtbl;
69 IDirectSoundCaptureBufferImpl* dscb;
72 static HRESULT IDirectSoundCaptureNotifyImpl_Create(IDirectSoundCaptureBufferImpl *dscb,
73 IDirectSoundCaptureNotifyImpl ** pdscn);
76 DirectSoundCaptureDevice * DSOUND_capture[MAXWAVEDRIVERS];
78 static HRESULT DirectSoundCaptureDevice_Create(DirectSoundCaptureDevice ** ppDevice);
80 static const char * const captureStateString[] = {
87 HRESULT DSOUND_CaptureCreate(
89 LPDIRECTSOUNDCAPTURE *ppDSC)
91 LPDIRECTSOUNDCAPTURE pDSC;
93 TRACE("(%s, %p)\n", debugstr_guid(riid), ppDSC);
95 if (!IsEqualIID(riid, &IID_IUnknown) &&
96 !IsEqualIID(riid, &IID_IDirectSoundCapture)) {
101 /* Get dsound configuration */
102 setup_dsound_options();
104 hr = IDirectSoundCaptureImpl_Create(&pDSC);
106 IDirectSoundCapture_AddRef(pDSC);
109 WARN("IDirectSoundCaptureImpl_Create failed\n");
116 HRESULT DSOUND_CaptureCreate8(
118 LPDIRECTSOUNDCAPTURE8 *ppDSC8)
120 LPDIRECTSOUNDCAPTURE8 pDSC8;
122 TRACE("(%s, %p)\n", debugstr_guid(riid), ppDSC8);
124 if (!IsEqualIID(riid, &IID_IUnknown) &&
125 !IsEqualIID(riid, &IID_IDirectSoundCapture8)) {
127 return E_NOINTERFACE;
130 /* Get dsound configuration */
131 setup_dsound_options();
133 hr = IDirectSoundCaptureImpl_Create(&pDSC8);
135 IDirectSoundCapture_AddRef(pDSC8);
138 WARN("IDirectSoundCaptureImpl_Create failed\n");
145 /***************************************************************************
146 * DirectSoundCaptureCreate [DSOUND.6]
148 * Create and initialize a DirectSoundCapture interface.
151 * lpcGUID [I] Address of the GUID that identifies the sound capture device.
152 * lplpDSC [O] Address of a variable to receive the interface pointer.
153 * pUnkOuter [I] Must be NULL.
157 * Failure: DSERR_NOAGGREGATION, DSERR_ALLOCATED, DSERR_INVALIDPARAM,
161 * lpcGUID must be one of the values returned from DirectSoundCaptureEnumerate
162 * or NULL for the default device or DSDEVID_DefaultCapture or
163 * DSDEVID_DefaultVoiceCapture.
165 * DSERR_ALLOCATED is returned for sound devices that do not support full duplex.
167 HRESULT WINAPI DirectSoundCaptureCreate(
169 LPDIRECTSOUNDCAPTURE *ppDSC,
173 LPDIRECTSOUNDCAPTURE pDSC;
174 TRACE("(%s,%p,%p)\n", debugstr_guid(lpcGUID), ppDSC, pUnkOuter);
177 WARN("invalid parameter: ppDSC == NULL\n");
178 return DSERR_INVALIDPARAM;
182 WARN("invalid parameter: pUnkOuter != NULL\n");
184 return DSERR_NOAGGREGATION;
187 hr = DSOUND_CaptureCreate(&IID_IDirectSoundCapture, &pDSC);
189 hr = IDirectSoundCapture_Initialize(pDSC, lpcGUID);
191 IDirectSoundCapture_Release(pDSC);
201 /***************************************************************************
202 * DirectSoundCaptureCreate8 [DSOUND.12]
204 * Create and initialize a DirectSoundCapture interface.
207 * lpcGUID [I] Address of the GUID that identifies the sound capture device.
208 * lplpDSC [O] Address of a variable to receive the interface pointer.
209 * pUnkOuter [I] Must be NULL.
213 * Failure: DSERR_NOAGGREGATION, DSERR_ALLOCATED, DSERR_INVALIDPARAM,
217 * lpcGUID must be one of the values returned from DirectSoundCaptureEnumerate
218 * or NULL for the default device or DSDEVID_DefaultCapture or
219 * DSDEVID_DefaultVoiceCapture.
221 * DSERR_ALLOCATED is returned for sound devices that do not support full duplex.
223 HRESULT WINAPI DirectSoundCaptureCreate8(
225 LPDIRECTSOUNDCAPTURE8 *ppDSC8,
229 LPDIRECTSOUNDCAPTURE8 pDSC8;
230 TRACE("(%s,%p,%p)\n", debugstr_guid(lpcGUID), ppDSC8, pUnkOuter);
232 if (ppDSC8 == NULL) {
233 WARN("invalid parameter: ppDSC8 == NULL\n");
234 return DSERR_INVALIDPARAM;
238 WARN("invalid parameter: pUnkOuter != NULL\n");
240 return DSERR_NOAGGREGATION;
243 hr = DSOUND_CaptureCreate8(&IID_IDirectSoundCapture8, &pDSC8);
245 hr = IDirectSoundCapture_Initialize(pDSC8, lpcGUID);
247 IDirectSoundCapture_Release(pDSC8);
257 static void capture_CheckNotify(IDirectSoundCaptureBufferImpl *This, DWORD from, DWORD len)
260 for (i = 0; i < This->nrofnotifies; ++i) {
261 LPDSBPOSITIONNOTIFY event = This->notifies + i;
262 DWORD offset = event->dwOffset;
263 TRACE("checking %d, position %d, event = %p\n", i, offset, event->hEventNotify);
265 if (offset == DSBPN_OFFSETSTOP) {
267 SetEvent(event->hEventNotify);
268 TRACE("signalled event %p (%d)\n", event->hEventNotify, i);
274 if (offset >= from && offset < (from + len))
276 TRACE("signalled event %p (%d)\n", event->hEventNotify, i);
277 SetEvent(event->hEventNotify);
283 DSOUND_capture_callback(HWAVEIN hwi, UINT msg, DWORD_PTR dwUser, DWORD_PTR dw1,
286 DirectSoundCaptureDevice * This = (DirectSoundCaptureDevice*)dwUser;
287 IDirectSoundCaptureBufferImpl * Moi = This->capture_buffer;
288 TRACE("(%p,%08x(%s),%08lx,%08lx,%08lx) entering at %d\n",hwi,msg,
289 msg == MM_WIM_OPEN ? "MM_WIM_OPEN" : msg == MM_WIM_CLOSE ? "MM_WIM_CLOSE" :
290 msg == MM_WIM_DATA ? "MM_WIM_DATA" : "UNKNOWN",dwUser,dw1,dw2,GetTickCount());
292 if (msg == MM_WIM_DATA) {
293 EnterCriticalSection( &(This->lock) );
294 TRACE("DirectSoundCapture msg=MM_WIM_DATA, old This->state=%s, old This->index=%d\n",
295 captureStateString[This->state],This->index);
296 if (This->state != STATE_STOPPED) {
297 int index = This->index;
298 if (This->state == STATE_STARTING)
299 This->state = STATE_CAPTURING;
300 capture_CheckNotify(Moi, (DWORD_PTR)This->pwave[index].lpData - (DWORD_PTR)This->buffer, This->pwave[index].dwBufferLength);
301 This->index = (This->index + 1) % This->nrofpwaves;
302 if ( (This->index == 0) && !(This->capture_buffer->flags & DSCBSTART_LOOPING) ) {
303 TRACE("end of buffer\n");
304 This->state = STATE_STOPPED;
305 capture_CheckNotify(Moi, 0, 0);
307 if (This->state == STATE_CAPTURING) {
308 waveInUnprepareHeader(hwi, &(This->pwave[index]), sizeof(WAVEHDR));
309 waveInPrepareHeader(hwi, &(This->pwave[index]), sizeof(WAVEHDR));
310 waveInAddBuffer(hwi, &(This->pwave[index]), sizeof(WAVEHDR));
311 } else if (This->state == STATE_STOPPING) {
313 This->state = STATE_STOPPED;
317 TRACE("DirectSoundCapture new This->state=%s, new This->index=%d\n",
318 captureStateString[This->state],This->index);
319 LeaveCriticalSection( &(This->lock) );
322 TRACE("completed\n");
325 /***************************************************************************
326 * IDirectSoundCaptureImpl
328 static HRESULT WINAPI
329 IDirectSoundCaptureImpl_QueryInterface(
330 LPDIRECTSOUNDCAPTURE iface,
334 IDirectSoundCaptureImpl *This = (IDirectSoundCaptureImpl *)iface;
335 TRACE( "(%p,%s,%p)\n", This, debugstr_guid(riid), ppobj );
338 WARN("invalid parameter\n");
344 if (IsEqualIID(riid, &IID_IUnknown)) {
345 IDirectSoundCapture_AddRef((LPDIRECTSOUNDCAPTURE)This);
348 } else if (IsEqualIID(riid, &IID_IDirectSoundCapture)) {
349 IDirectSoundCapture_AddRef((LPDIRECTSOUNDCAPTURE)This);
354 WARN("unsupported riid: %s\n", debugstr_guid(riid));
355 return E_NOINTERFACE;
359 IDirectSoundCaptureImpl_AddRef( LPDIRECTSOUNDCAPTURE iface )
361 IDirectSoundCaptureImpl *This = (IDirectSoundCaptureImpl *)iface;
362 ULONG ref = InterlockedIncrement(&(This->ref));
363 TRACE("(%p) ref was %d\n", This, ref - 1);
368 IDirectSoundCaptureImpl_Release( LPDIRECTSOUNDCAPTURE iface )
370 IDirectSoundCaptureImpl *This = (IDirectSoundCaptureImpl *)iface;
371 ULONG ref = InterlockedDecrement(&(This->ref));
372 TRACE("(%p) ref was %d\n", This, ref + 1);
376 DirectSoundCaptureDevice_Release(This->device);
378 HeapFree( GetProcessHeap(), 0, This );
379 TRACE("(%p) released\n", This);
384 HRESULT WINAPI IDirectSoundCaptureImpl_CreateCaptureBuffer(
385 LPDIRECTSOUNDCAPTURE iface,
386 LPCDSCBUFFERDESC lpcDSCBufferDesc,
387 LPDIRECTSOUNDCAPTUREBUFFER* lplpDSCaptureBuffer,
391 IDirectSoundCaptureImpl *This = (IDirectSoundCaptureImpl *)iface;
393 TRACE( "(%p,%p,%p,%p)\n",iface,lpcDSCBufferDesc,lplpDSCaptureBuffer,pUnk);
395 if (lpcDSCBufferDesc == NULL) {
396 WARN("invalid parameter: lpcDSCBufferDesc == NULL)\n");
397 return DSERR_INVALIDPARAM;
400 if (lplpDSCaptureBuffer == NULL) {
401 WARN("invalid parameter: lplpDSCaptureBuffer == NULL\n");
402 return DSERR_INVALIDPARAM;
406 WARN("invalid parameter: pUnk != NULL\n");
407 return DSERR_INVALIDPARAM;
410 /* FIXME: We can only have one buffer so what do we do here? */
411 if (This->device->capture_buffer) {
412 WARN("lnvalid parameter: already has buffer\n");
413 return DSERR_INVALIDPARAM; /* DSERR_GENERIC ? */
416 hr = IDirectSoundCaptureBufferImpl_Create(This->device,
417 (IDirectSoundCaptureBufferImpl **)lplpDSCaptureBuffer, lpcDSCBufferDesc);
420 WARN("IDirectSoundCaptureBufferImpl_Create failed\n");
425 HRESULT WINAPI IDirectSoundCaptureImpl_GetCaps(
426 LPDIRECTSOUNDCAPTURE iface,
427 LPDSCCAPS lpDSCCaps )
429 IDirectSoundCaptureImpl *This = (IDirectSoundCaptureImpl *)iface;
430 TRACE("(%p,%p)\n",This,lpDSCCaps);
432 if (This->device == NULL) {
433 WARN("not initialized\n");
434 return DSERR_UNINITIALIZED;
437 if (lpDSCCaps== NULL) {
438 WARN("invalid parameter: lpDSCCaps== NULL\n");
439 return DSERR_INVALIDPARAM;
442 if (lpDSCCaps->dwSize < sizeof(*lpDSCCaps)) {
443 WARN("invalid parameter: lpDSCCaps->dwSize = %d\n", lpDSCCaps->dwSize);
444 return DSERR_INVALIDPARAM;
447 lpDSCCaps->dwFlags = This->device->drvcaps.dwFlags;
448 lpDSCCaps->dwFormats = This->device->drvcaps.dwFormats;
449 lpDSCCaps->dwChannels = This->device->drvcaps.dwChannels;
451 TRACE("(flags=0x%08x,format=0x%08x,channels=%d)\n",lpDSCCaps->dwFlags,
452 lpDSCCaps->dwFormats, lpDSCCaps->dwChannels);
457 HRESULT WINAPI IDirectSoundCaptureImpl_Initialize(
458 LPDIRECTSOUNDCAPTURE iface,
461 IDirectSoundCaptureImpl *This = (IDirectSoundCaptureImpl *)iface;
462 TRACE("(%p,%s)\n", This, debugstr_guid(lpcGUID));
464 if (This->device != NULL) {
465 WARN("already initialized\n");
466 return DSERR_ALREADYINITIALIZED;
468 return DirectSoundCaptureDevice_Initialize(&This->device, lpcGUID);
471 static const IDirectSoundCaptureVtbl dscvt =
473 /* IUnknown methods */
474 IDirectSoundCaptureImpl_QueryInterface,
475 IDirectSoundCaptureImpl_AddRef,
476 IDirectSoundCaptureImpl_Release,
478 /* IDirectSoundCapture methods */
479 IDirectSoundCaptureImpl_CreateCaptureBuffer,
480 IDirectSoundCaptureImpl_GetCaps,
481 IDirectSoundCaptureImpl_Initialize
484 static HRESULT IDirectSoundCaptureImpl_Create(
485 LPDIRECTSOUNDCAPTURE8 * ppDSC)
487 IDirectSoundCaptureImpl *pDSC;
488 TRACE("(%p)\n", ppDSC);
490 /* Allocate memory */
491 pDSC = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(IDirectSoundCaptureImpl));
493 WARN("out of memory\n");
495 return DSERR_OUTOFMEMORY;
498 pDSC->lpVtbl = &dscvt;
502 *ppDSC = (LPDIRECTSOUNDCAPTURE8)pDSC;
507 /*******************************************************************************
508 * IDirectSoundCaptureNotify
510 static HRESULT WINAPI IDirectSoundCaptureNotifyImpl_QueryInterface(
511 LPDIRECTSOUNDNOTIFY iface,
515 IDirectSoundCaptureNotifyImpl *This = (IDirectSoundCaptureNotifyImpl *)iface;
516 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
518 if (This->dscb == NULL) {
519 WARN("invalid parameter\n");
523 return IDirectSoundCaptureBuffer_QueryInterface((LPDIRECTSOUNDCAPTUREBUFFER)This->dscb, riid, ppobj);
526 static ULONG WINAPI IDirectSoundCaptureNotifyImpl_AddRef(LPDIRECTSOUNDNOTIFY iface)
528 IDirectSoundCaptureNotifyImpl *This = (IDirectSoundCaptureNotifyImpl *)iface;
529 ULONG ref = InterlockedIncrement(&(This->ref));
530 TRACE("(%p) ref was %d\n", This, ref - 1);
534 static ULONG WINAPI IDirectSoundCaptureNotifyImpl_Release(LPDIRECTSOUNDNOTIFY iface)
536 IDirectSoundCaptureNotifyImpl *This = (IDirectSoundCaptureNotifyImpl *)iface;
537 ULONG ref = InterlockedDecrement(&(This->ref));
538 TRACE("(%p) ref was %d\n", This, ref + 1);
541 if (This->dscb->hwnotify)
542 IDsDriverNotify_Release(This->dscb->hwnotify);
543 This->dscb->notify=NULL;
544 IDirectSoundCaptureBuffer_Release((LPDIRECTSOUNDCAPTUREBUFFER)This->dscb);
545 HeapFree(GetProcessHeap(),0,This);
546 TRACE("(%p) released\n", This);
551 static HRESULT WINAPI IDirectSoundCaptureNotifyImpl_SetNotificationPositions(
552 LPDIRECTSOUNDNOTIFY iface,
554 LPCDSBPOSITIONNOTIFY notify)
556 IDirectSoundCaptureNotifyImpl *This = (IDirectSoundCaptureNotifyImpl *)iface;
557 TRACE("(%p,0x%08x,%p)\n",This,howmuch,notify);
559 if (howmuch > 0 && notify == NULL) {
560 WARN("invalid parameter: notify == NULL\n");
561 return DSERR_INVALIDPARAM;
564 if (TRACE_ON(dsound)) {
566 for (i=0;i<howmuch;i++)
567 TRACE("notify at %d to %p\n",
568 notify[i].dwOffset,notify[i].hEventNotify);
571 if (This->dscb->hwnotify) {
573 hres = IDsDriverNotify_SetNotificationPositions(This->dscb->hwnotify, howmuch, notify);
575 WARN("IDsDriverNotify_SetNotificationPositions failed\n");
577 } else if (howmuch > 0) {
578 /* Make an internal copy of the caller-supplied array.
579 * Replace the existing copy if one is already present. */
580 if (This->dscb->notifies)
581 This->dscb->notifies = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
582 This->dscb->notifies, howmuch * sizeof(DSBPOSITIONNOTIFY));
584 This->dscb->notifies = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
585 howmuch * sizeof(DSBPOSITIONNOTIFY));
587 if (This->dscb->notifies == NULL) {
588 WARN("out of memory\n");
589 return DSERR_OUTOFMEMORY;
591 CopyMemory(This->dscb->notifies, notify, howmuch * sizeof(DSBPOSITIONNOTIFY));
592 This->dscb->nrofnotifies = howmuch;
594 HeapFree(GetProcessHeap(), 0, This->dscb->notifies);
595 This->dscb->notifies = NULL;
596 This->dscb->nrofnotifies = 0;
602 static const IDirectSoundNotifyVtbl dscnvt =
604 IDirectSoundCaptureNotifyImpl_QueryInterface,
605 IDirectSoundCaptureNotifyImpl_AddRef,
606 IDirectSoundCaptureNotifyImpl_Release,
607 IDirectSoundCaptureNotifyImpl_SetNotificationPositions,
610 static HRESULT IDirectSoundCaptureNotifyImpl_Create(
611 IDirectSoundCaptureBufferImpl *dscb,
612 IDirectSoundCaptureNotifyImpl **pdscn)
614 IDirectSoundCaptureNotifyImpl * dscn;
615 TRACE("(%p,%p)\n",dscb,pdscn);
617 dscn = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*dscn));
620 WARN("out of memory\n");
621 return DSERR_OUTOFMEMORY;
625 dscn->lpVtbl = &dscnvt;
628 IDirectSoundCaptureBuffer_AddRef((LPDIRECTSOUNDCAPTUREBUFFER)dscb);
634 /*******************************************************************************
635 * IDirectSoundCaptureBuffer
637 static HRESULT WINAPI
638 IDirectSoundCaptureBufferImpl_QueryInterface(
639 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
643 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
645 TRACE( "(%p,%s,%p)\n", This, debugstr_guid(riid), ppobj );
648 WARN("invalid parameter\n");
654 if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ) {
656 hres = IDirectSoundCaptureNotifyImpl_Create(This, &This->notify);
658 IDirectSoundNotify_AddRef((LPDIRECTSOUNDNOTIFY)This->notify);
659 if (This->device->hwbuf && !This->hwnotify) {
660 hres = IDsCaptureDriverBuffer_QueryInterface(This->device->hwbuf,
661 &IID_IDsDriverNotify, (LPVOID*)&(This->hwnotify));
663 WARN("IDsCaptureDriverBuffer_QueryInterface failed\n");
664 IDirectSoundNotify_Release((LPDIRECTSOUNDNOTIFY)This->notify);
670 *ppobj = This->notify;
674 WARN("IID_IDirectSoundNotify\n");
678 if ( IsEqualGUID( &IID_IDirectSoundCaptureBuffer, riid ) ||
679 IsEqualGUID( &IID_IDirectSoundCaptureBuffer8, riid ) ) {
680 IDirectSoundCaptureBuffer8_AddRef(iface);
685 FIXME("(%p,%s,%p) unsupported GUID\n", This, debugstr_guid(riid), ppobj);
686 return E_NOINTERFACE;
690 IDirectSoundCaptureBufferImpl_AddRef( LPDIRECTSOUNDCAPTUREBUFFER8 iface )
692 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
693 ULONG ref = InterlockedIncrement(&(This->ref));
694 TRACE("(%p) ref was %d\n", This, ref - 1);
699 IDirectSoundCaptureBufferImpl_Release( LPDIRECTSOUNDCAPTUREBUFFER8 iface )
701 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
702 ULONG ref = InterlockedDecrement(&(This->ref));
703 TRACE("(%p) ref was %d\n", This, ref + 1);
706 TRACE("deleting object\n");
707 if (This->device->state == STATE_CAPTURING)
708 This->device->state = STATE_STOPPING;
710 HeapFree(GetProcessHeap(),0, This->pdscbd);
712 if (This->device->hwi) {
713 waveInReset(This->device->hwi);
714 waveInClose(This->device->hwi);
715 HeapFree(GetProcessHeap(),0, This->device->pwave);
716 This->device->pwave = 0;
717 This->device->hwi = 0;
720 if (This->device->hwbuf)
721 IDsCaptureDriverBuffer_Release(This->device->hwbuf);
723 /* remove from DirectSoundCaptureDevice */
724 This->device->capture_buffer = NULL;
727 IDirectSoundNotify_Release((LPDIRECTSOUNDNOTIFY)This->notify);
729 /* If driver manages its own buffer, IDsCaptureDriverBuffer_Release
730 should have freed the buffer. Prevent freeing it again in
731 IDirectSoundCaptureBufferImpl_Create */
732 if (!(This->device->drvdesc.dwFlags & DSDDESC_USESYSTEMMEMORY))
733 This->device->buffer = NULL;
735 HeapFree(GetProcessHeap(), 0, This->notifies);
736 HeapFree( GetProcessHeap(), 0, This );
737 TRACE("(%p) released\n", This);
742 static HRESULT WINAPI
743 IDirectSoundCaptureBufferImpl_GetCaps(
744 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
745 LPDSCBCAPS lpDSCBCaps )
747 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
748 TRACE( "(%p,%p)\n", This, lpDSCBCaps );
750 if (lpDSCBCaps == NULL) {
751 WARN("invalid parameter: lpDSCBCaps == NULL\n");
752 return DSERR_INVALIDPARAM;
755 if (lpDSCBCaps->dwSize < sizeof(DSCBCAPS)) {
756 WARN("invalid parameter: lpDSCBCaps->dwSize = %d\n", lpDSCBCaps->dwSize);
757 return DSERR_INVALIDPARAM;
760 if (This->device == NULL) {
761 WARN("invalid parameter: This->device == NULL\n");
762 return DSERR_INVALIDPARAM;
765 lpDSCBCaps->dwSize = sizeof(DSCBCAPS);
766 lpDSCBCaps->dwFlags = This->flags;
767 lpDSCBCaps->dwBufferBytes = This->pdscbd->dwBufferBytes;
768 lpDSCBCaps->dwReserved = 0;
770 TRACE("returning DS_OK\n");
774 static HRESULT WINAPI
775 IDirectSoundCaptureBufferImpl_GetCurrentPosition(
776 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
777 LPDWORD lpdwCapturePosition,
778 LPDWORD lpdwReadPosition )
780 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
781 HRESULT hres = DS_OK;
782 TRACE( "(%p,%p,%p)\n", This, lpdwCapturePosition, lpdwReadPosition );
784 if (This->device == NULL) {
785 WARN("invalid parameter: This->device == NULL\n");
786 return DSERR_INVALIDPARAM;
789 if (This->device->driver) {
790 hres = IDsCaptureDriverBuffer_GetPosition(This->device->hwbuf, lpdwCapturePosition, lpdwReadPosition );
792 WARN("IDsCaptureDriverBuffer_GetPosition failed\n");
793 } else if (This->device->hwi) {
796 EnterCriticalSection(&This->device->lock);
797 pos = (DWORD_PTR)This->device->pwave[This->device->index].lpData - (DWORD_PTR)This->device->buffer;
798 if (lpdwCapturePosition)
799 *lpdwCapturePosition = (This->device->pwave[This->device->index].dwBufferLength + pos) % This->device->buflen;
800 if (lpdwReadPosition)
801 *lpdwReadPosition = pos;
802 LeaveCriticalSection(&This->device->lock);
806 hres = DSERR_NODRIVER;
809 TRACE("cappos=%d readpos=%d\n", (lpdwCapturePosition?*lpdwCapturePosition:-1), (lpdwReadPosition?*lpdwReadPosition:-1));
810 TRACE("returning %08x\n", hres);
814 static HRESULT WINAPI
815 IDirectSoundCaptureBufferImpl_GetFormat(
816 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
817 LPWAVEFORMATEX lpwfxFormat,
818 DWORD dwSizeAllocated,
819 LPDWORD lpdwSizeWritten )
821 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
822 HRESULT hres = DS_OK;
823 TRACE( "(%p,%p,0x%08x,%p)\n", This, lpwfxFormat, dwSizeAllocated,
826 if (This->device == NULL) {
827 WARN("invalid parameter: This->device == NULL\n");
828 return DSERR_INVALIDPARAM;
831 if (dwSizeAllocated > (sizeof(WAVEFORMATEX) + This->device->pwfx->cbSize))
832 dwSizeAllocated = sizeof(WAVEFORMATEX) + This->device->pwfx->cbSize;
834 if (lpwfxFormat) { /* NULL is valid (just want size) */
835 CopyMemory(lpwfxFormat, This->device->pwfx, dwSizeAllocated);
837 *lpdwSizeWritten = dwSizeAllocated;
840 *lpdwSizeWritten = sizeof(WAVEFORMATEX) + This->device->pwfx->cbSize;
842 TRACE("invalid parameter: lpdwSizeWritten = NULL\n");
843 hres = DSERR_INVALIDPARAM;
847 TRACE("returning %08x\n", hres);
851 static HRESULT WINAPI
852 IDirectSoundCaptureBufferImpl_GetStatus(
853 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
856 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
857 TRACE( "(%p, %p), thread is %04x\n", This, lpdwStatus, GetCurrentThreadId() );
859 if (This->device == NULL) {
860 WARN("invalid parameter: This->device == NULL\n");
861 return DSERR_INVALIDPARAM;
864 if (lpdwStatus == NULL) {
865 WARN("invalid parameter: lpdwStatus == NULL\n");
866 return DSERR_INVALIDPARAM;
870 EnterCriticalSection(&(This->device->lock));
872 TRACE("old This->device->state=%s, old lpdwStatus=%08x\n",
873 captureStateString[This->device->state],*lpdwStatus);
874 if ((This->device->state == STATE_STARTING) ||
875 (This->device->state == STATE_CAPTURING)) {
876 *lpdwStatus |= DSCBSTATUS_CAPTURING;
877 if (This->flags & DSCBSTART_LOOPING)
878 *lpdwStatus |= DSCBSTATUS_LOOPING;
880 TRACE("new This->device->state=%s, new lpdwStatus=%08x\n",
881 captureStateString[This->device->state],*lpdwStatus);
882 LeaveCriticalSection(&(This->device->lock));
884 TRACE("status=%x\n", *lpdwStatus);
885 TRACE("returning DS_OK\n");
889 static HRESULT WINAPI
890 IDirectSoundCaptureBufferImpl_Initialize(
891 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
892 LPDIRECTSOUNDCAPTURE lpDSC,
893 LPCDSCBUFFERDESC lpcDSCBDesc )
895 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
897 FIXME( "(%p,%p,%p): stub\n", This, lpDSC, lpcDSCBDesc );
902 static HRESULT WINAPI
903 IDirectSoundCaptureBufferImpl_Lock(
904 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
907 LPVOID* lplpvAudioPtr1,
908 LPDWORD lpdwAudioBytes1,
909 LPVOID* lplpvAudioPtr2,
910 LPDWORD lpdwAudioBytes2,
913 HRESULT hres = DS_OK;
914 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
915 TRACE( "(%p,%08u,%08u,%p,%p,%p,%p,0x%08x) at %d\n", This, dwReadCusor,
916 dwReadBytes, lplpvAudioPtr1, lpdwAudioBytes1, lplpvAudioPtr2,
917 lpdwAudioBytes2, dwFlags, GetTickCount() );
919 if (This->device == NULL) {
920 WARN("invalid parameter: This->device == NULL\n");
921 return DSERR_INVALIDPARAM;
924 if (lplpvAudioPtr1 == NULL) {
925 WARN("invalid parameter: lplpvAudioPtr1 == NULL\n");
926 return DSERR_INVALIDPARAM;
929 if (lpdwAudioBytes1 == NULL) {
930 WARN("invalid parameter: lpdwAudioBytes1 == NULL\n");
931 return DSERR_INVALIDPARAM;
934 EnterCriticalSection(&(This->device->lock));
936 if (This->device->driver) {
937 hres = IDsCaptureDriverBuffer_Lock(This->device->hwbuf, lplpvAudioPtr1,
938 lpdwAudioBytes1, lplpvAudioPtr2,
939 lpdwAudioBytes2, dwReadCusor,
940 dwReadBytes, dwFlags);
942 WARN("IDsCaptureDriverBuffer_Lock failed\n");
943 } else if (This->device->hwi) {
944 *lplpvAudioPtr1 = This->device->buffer + dwReadCusor;
945 if ( (dwReadCusor + dwReadBytes) > This->device->buflen) {
946 *lpdwAudioBytes1 = This->device->buflen - dwReadCusor;
948 *lplpvAudioPtr2 = This->device->buffer;
950 *lpdwAudioBytes2 = dwReadBytes - *lpdwAudioBytes1;
952 *lpdwAudioBytes1 = dwReadBytes;
956 *lpdwAudioBytes2 = 0;
959 TRACE("invalid call\n");
960 hres = DSERR_INVALIDCALL; /* DSERR_NODRIVER ? */
963 LeaveCriticalSection(&(This->device->lock));
965 TRACE("returning %08x\n", hres);
969 static HRESULT WINAPI
970 IDirectSoundCaptureBufferImpl_Start(
971 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
974 HRESULT hres = DS_OK;
975 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
976 TRACE( "(%p,0x%08x)\n", This, dwFlags );
978 if (This->device == NULL) {
979 WARN("invalid parameter: This->device == NULL\n");
980 return DSERR_INVALIDPARAM;
983 if ( (This->device->driver == 0) && (This->device->hwi == 0) ) {
985 return DSERR_NODRIVER;
988 EnterCriticalSection(&(This->device->lock));
990 This->flags = dwFlags;
991 TRACE("old This->state=%s\n",captureStateString[This->device->state]);
992 if (This->device->state == STATE_STOPPED)
993 This->device->state = STATE_STARTING;
994 else if (This->device->state == STATE_STOPPING)
995 This->device->state = STATE_CAPTURING;
996 TRACE("new This->device->state=%s\n",captureStateString[This->device->state]);
998 LeaveCriticalSection(&(This->device->lock));
1000 if (This->device->driver) {
1001 hres = IDsCaptureDriverBuffer_Start(This->device->hwbuf, dwFlags);
1003 WARN("IDsCaptureDriverBuffer_Start failed\n");
1004 } else if (This->device->hwi) {
1005 DirectSoundCaptureDevice *device = This->device;
1007 if (device->buffer) {
1009 DWORD blocksize = DSOUND_fraglen(device->pwfx->nSamplesPerSec, device->pwfx->nBlockAlign);
1010 device->nrofpwaves = device->buflen / blocksize + !!(device->buflen % blocksize);
1011 TRACE("nrofpwaves=%d\n", device->nrofpwaves);
1013 /* prepare headers */
1015 device->pwave = HeapReAlloc(GetProcessHeap(), 0,device->pwave, device->nrofpwaves*sizeof(WAVEHDR));
1017 device->pwave = HeapAlloc(GetProcessHeap(), 0, device->nrofpwaves*sizeof(WAVEHDR));
1019 for (c = 0; c < device->nrofpwaves; ++c) {
1020 device->pwave[c].lpData = (char *)device->buffer + c * blocksize;
1021 if (c + 1 == device->nrofpwaves)
1022 device->pwave[c].dwBufferLength = device->buflen - c * blocksize;
1024 device->pwave[c].dwBufferLength = blocksize;
1025 device->pwave[c].dwBytesRecorded = 0;
1026 device->pwave[c].dwUser = (DWORD_PTR)device;
1027 device->pwave[c].dwFlags = 0;
1028 device->pwave[c].dwLoops = 0;
1029 hres = mmErr(waveInPrepareHeader(device->hwi, &(device->pwave[c]),sizeof(WAVEHDR)));
1030 if (hres != DS_OK) {
1031 WARN("waveInPrepareHeader failed\n");
1033 waveInUnprepareHeader(device->hwi, &(device->pwave[c]),sizeof(WAVEHDR));
1037 hres = mmErr(waveInAddBuffer(device->hwi, &(device->pwave[c]), sizeof(WAVEHDR)));
1038 if (hres != DS_OK) {
1039 WARN("waveInAddBuffer failed\n");
1041 waveInUnprepareHeader(device->hwi, &(device->pwave[c]),sizeof(WAVEHDR));
1046 FillMemory(device->buffer, device->buflen, (device->pwfx->wBitsPerSample == 8) ? 128 : 0);
1051 if (hres == DS_OK) {
1052 /* start filling the first buffer */
1053 hres = mmErr(waveInStart(device->hwi));
1055 WARN("waveInStart failed\n");
1058 if (hres != DS_OK) {
1059 WARN("calling waveInClose because of error\n");
1060 waveInClose(device->hwi);
1064 WARN("no driver\n");
1065 hres = DSERR_NODRIVER;
1068 TRACE("returning %08x\n", hres);
1072 static HRESULT WINAPI
1073 IDirectSoundCaptureBufferImpl_Stop( LPDIRECTSOUNDCAPTUREBUFFER8 iface )
1075 HRESULT hres = DS_OK;
1076 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
1077 TRACE( "(%p)\n", This );
1079 if (This->device == NULL) {
1080 WARN("invalid parameter: This->device == NULL\n");
1081 return DSERR_INVALIDPARAM;
1084 EnterCriticalSection(&(This->device->lock));
1086 TRACE("old This->device->state=%s\n",captureStateString[This->device->state]);
1087 if (This->device->state == STATE_CAPTURING)
1088 This->device->state = STATE_STOPPING;
1089 else if (This->device->state == STATE_STARTING)
1090 This->device->state = STATE_STOPPED;
1091 TRACE("new This->device->state=%s\n",captureStateString[This->device->state]);
1093 LeaveCriticalSection(&(This->device->lock));
1095 if (This->device->driver) {
1096 hres = IDsCaptureDriverBuffer_Stop(This->device->hwbuf);
1098 WARN("IDsCaptureDriverBuffer_Stop() failed\n");
1099 } else if (This->device->hwi) {
1100 hres = mmErr(waveInReset(This->device->hwi));
1102 WARN("waveInReset() failed\n");
1104 WARN("no driver\n");
1105 hres = DSERR_NODRIVER;
1108 TRACE("returning %08x\n", hres);
1112 static HRESULT WINAPI
1113 IDirectSoundCaptureBufferImpl_Unlock(
1114 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
1115 LPVOID lpvAudioPtr1,
1116 DWORD dwAudioBytes1,
1117 LPVOID lpvAudioPtr2,
1118 DWORD dwAudioBytes2 )
1120 HRESULT hres = DS_OK;
1121 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
1122 TRACE( "(%p,%p,%08u,%p,%08u)\n", This, lpvAudioPtr1, dwAudioBytes1,
1123 lpvAudioPtr2, dwAudioBytes2 );
1125 if (lpvAudioPtr1 == NULL) {
1126 WARN("invalid parameter: lpvAudioPtr1 == NULL\n");
1127 return DSERR_INVALIDPARAM;
1130 if (This->device->driver) {
1131 hres = IDsCaptureDriverBuffer_Unlock(This->device->hwbuf, lpvAudioPtr1,
1132 dwAudioBytes1, lpvAudioPtr2, dwAudioBytes2);
1134 WARN("IDsCaptureDriverBuffer_Unlock failed\n");
1135 } else if (!This->device->hwi) {
1136 WARN("invalid call\n");
1137 hres = DSERR_INVALIDCALL;
1140 TRACE("returning %08x\n", hres);
1144 static HRESULT WINAPI
1145 IDirectSoundCaptureBufferImpl_GetObjectInPath(
1146 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
1147 REFGUID rguidObject,
1149 REFGUID rguidInterface,
1152 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
1154 FIXME( "(%p,%s,%u,%s,%p): stub\n", This, debugstr_guid(rguidObject),
1155 dwIndex, debugstr_guid(rguidInterface), ppObject );
1160 static HRESULT WINAPI
1161 IDirectSoundCaptureBufferImpl_GetFXStatus(
1162 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
1164 LPDWORD pdwFXStatus )
1166 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
1168 FIXME( "(%p,%u,%p): stub\n", This, dwFXCount, pdwFXStatus );
1173 static const IDirectSoundCaptureBuffer8Vtbl dscbvt =
1175 /* IUnknown methods */
1176 IDirectSoundCaptureBufferImpl_QueryInterface,
1177 IDirectSoundCaptureBufferImpl_AddRef,
1178 IDirectSoundCaptureBufferImpl_Release,
1180 /* IDirectSoundCaptureBuffer methods */
1181 IDirectSoundCaptureBufferImpl_GetCaps,
1182 IDirectSoundCaptureBufferImpl_GetCurrentPosition,
1183 IDirectSoundCaptureBufferImpl_GetFormat,
1184 IDirectSoundCaptureBufferImpl_GetStatus,
1185 IDirectSoundCaptureBufferImpl_Initialize,
1186 IDirectSoundCaptureBufferImpl_Lock,
1187 IDirectSoundCaptureBufferImpl_Start,
1188 IDirectSoundCaptureBufferImpl_Stop,
1189 IDirectSoundCaptureBufferImpl_Unlock,
1191 /* IDirectSoundCaptureBuffer methods */
1192 IDirectSoundCaptureBufferImpl_GetObjectInPath,
1193 IDirectSoundCaptureBufferImpl_GetFXStatus
1196 HRESULT IDirectSoundCaptureBufferImpl_Create(
1197 DirectSoundCaptureDevice *device,
1198 IDirectSoundCaptureBufferImpl ** ppobj,
1199 LPCDSCBUFFERDESC lpcDSCBufferDesc)
1201 LPWAVEFORMATEX wfex;
1202 TRACE( "(%p,%p,%p)\n", device, ppobj, lpcDSCBufferDesc);
1204 if (ppobj == NULL) {
1205 WARN("invalid parameter: ppobj == NULL\n");
1206 return DSERR_INVALIDPARAM;
1210 WARN("not initialized\n");
1212 return DSERR_UNINITIALIZED;
1215 if (lpcDSCBufferDesc == NULL) {
1216 WARN("invalid parameter: lpcDSCBufferDesc == NULL\n");
1218 return DSERR_INVALIDPARAM;
1221 if ( ((lpcDSCBufferDesc->dwSize != sizeof(DSCBUFFERDESC)) &&
1222 (lpcDSCBufferDesc->dwSize != sizeof(DSCBUFFERDESC1))) ||
1223 (lpcDSCBufferDesc->dwBufferBytes == 0) ||
1224 (lpcDSCBufferDesc->lpwfxFormat == NULL) ) { /* FIXME: DSERR_BADFORMAT ? */
1225 WARN("invalid lpcDSCBufferDesc\n");
1227 return DSERR_INVALIDPARAM;
1230 wfex = lpcDSCBufferDesc->lpwfxFormat;
1232 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
1233 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
1234 wfex->wFormatTag, wfex->nChannels, wfex->nSamplesPerSec,
1235 wfex->nAvgBytesPerSec, wfex->nBlockAlign,
1236 wfex->wBitsPerSample, wfex->cbSize);
1238 device->pwfx = DSOUND_CopyFormat(wfex);
1239 if ( device->pwfx == NULL ) {
1241 return DSERR_OUTOFMEMORY;
1244 *ppobj = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,
1245 sizeof(IDirectSoundCaptureBufferImpl));
1247 if ( *ppobj == NULL ) {
1248 WARN("out of memory\n");
1250 return DSERR_OUTOFMEMORY;
1252 HRESULT err = DS_OK;
1255 IDirectSoundCaptureBufferImpl *This = *ppobj;
1258 This->device = device;
1259 This->device->capture_buffer = This;
1260 This->notify = NULL;
1261 This->nrofnotifies = 0;
1262 This->hwnotify = NULL;
1264 This->pdscbd = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,
1265 lpcDSCBufferDesc->dwSize);
1267 CopyMemory(This->pdscbd, lpcDSCBufferDesc, lpcDSCBufferDesc->dwSize);
1269 WARN("no memory\n");
1270 This->device->capture_buffer = 0;
1271 HeapFree( GetProcessHeap(), 0, This );
1273 return DSERR_OUTOFMEMORY;
1276 This->lpVtbl = &dscbvt;
1278 if (device->driver) {
1279 if (This->device->drvdesc.dwFlags & DSDDESC_DOMMSYSTEMOPEN)
1280 FIXME("DSDDESC_DOMMSYSTEMOPEN not supported\n");
1282 if (This->device->drvdesc.dwFlags & DSDDESC_USESYSTEMMEMORY) {
1283 /* allocate buffer from system memory */
1284 buflen = lpcDSCBufferDesc->dwBufferBytes;
1285 TRACE("desired buflen=%d, old buffer=%p\n", buflen, device->buffer);
1287 newbuf = HeapReAlloc(GetProcessHeap(),0,device->buffer,buflen);
1289 newbuf = HeapAlloc(GetProcessHeap(),0,buflen);
1291 if (newbuf == NULL) {
1292 WARN("failed to allocate capture buffer\n");
1293 err = DSERR_OUTOFMEMORY;
1294 /* but the old buffer might still exist and must be re-prepared */
1296 device->buffer = newbuf;
1297 device->buflen = buflen;
1300 /* let driver allocate memory */
1301 device->buflen = lpcDSCBufferDesc->dwBufferBytes;
1303 HeapFree( GetProcessHeap(), 0, device->buffer);
1304 device->buffer = NULL;
1307 err = IDsCaptureDriver_CreateCaptureBuffer(device->driver,
1308 device->pwfx,0,0,&(device->buflen),&(device->buffer),(LPVOID*)&(device->hwbuf));
1310 WARN("IDsCaptureDriver_CreateCaptureBuffer failed\n");
1311 This->device->capture_buffer = 0;
1312 HeapFree( GetProcessHeap(), 0, This );
1317 DWORD flags = CALLBACK_FUNCTION;
1318 err = mmErr(waveInOpen(&(device->hwi),
1319 device->drvdesc.dnDevNode, device->pwfx,
1320 (DWORD_PTR)DSOUND_capture_callback, (DWORD_PTR)device, flags));
1322 WARN("waveInOpen failed\n");
1323 This->device->capture_buffer = 0;
1324 HeapFree( GetProcessHeap(), 0, This );
1329 buflen = lpcDSCBufferDesc->dwBufferBytes;
1330 TRACE("desired buflen=%d, old buffer=%p\n", buflen, device->buffer);
1332 newbuf = HeapReAlloc(GetProcessHeap(),0,device->buffer,buflen);
1334 newbuf = HeapAlloc(GetProcessHeap(),0,buflen);
1335 if (newbuf == NULL) {
1336 WARN("failed to allocate capture buffer\n");
1337 err = DSERR_OUTOFMEMORY;
1338 /* but the old buffer might still exist and must be re-prepared */
1340 device->buffer = newbuf;
1341 device->buflen = buflen;
1346 TRACE("returning DS_OK\n");
1350 /*******************************************************************************
1351 * DirectSoundCaptureDevice
1353 HRESULT DirectSoundCaptureDevice_Initialize(
1354 DirectSoundCaptureDevice ** ppDevice,
1357 HRESULT err = DSERR_INVALIDPARAM;
1359 BOOLEAN found = FALSE;
1361 DirectSoundCaptureDevice *device = *ppDevice;
1362 TRACE("(%p, %s)\n", ppDevice, debugstr_guid(lpcGUID));
1364 /* Default device? */
1365 if ( !lpcGUID || IsEqualGUID(lpcGUID, &GUID_NULL) )
1366 lpcGUID = &DSDEVID_DefaultCapture;
1368 if (GetDeviceID(lpcGUID, &devGUID) != DS_OK) {
1369 WARN("invalid parameter: lpcGUID\n");
1370 return DSERR_INVALIDPARAM;
1373 widn = waveInGetNumDevs();
1375 WARN("no audio devices found\n");
1376 return DSERR_NODRIVER;
1379 /* enumerate WINMM audio devices and find the one we want */
1380 for (wid=0; wid<widn; wid++) {
1381 if (IsEqualGUID( &devGUID, &DSOUND_capture_guids[wid]) ) {
1387 if (found == FALSE) {
1388 WARN("No device found matching given ID!\n");
1389 return DSERR_NODRIVER;
1392 if (DSOUND_capture[wid]) {
1393 WARN("already in use\n");
1394 return DSERR_ALLOCATED;
1397 err = DirectSoundCaptureDevice_Create(&(device));
1399 WARN("DirectSoundCaptureDevice_Create failed\n");
1404 device->guid = devGUID;
1406 /* Disable the direct sound driver to force emulation if requested. */
1407 device->driver = NULL;
1408 if (ds_hw_accel != DS_HW_ACCEL_EMULATION)
1410 err = mmErr(waveInMessage(UlongToHandle(wid),DRV_QUERYDSOUNDIFACE,(DWORD_PTR)&device->driver,0));
1411 if ( (err != DS_OK) && (err != DSERR_UNSUPPORTED) ) {
1412 WARN("waveInMessage failed; err=%x\n",err);
1418 /* Get driver description */
1419 if (device->driver) {
1420 TRACE("using DirectSound driver\n");
1421 err = IDsCaptureDriver_GetDriverDesc(device->driver, &(device->drvdesc));
1423 WARN("IDsCaptureDriver_GetDriverDesc failed\n");
1427 TRACE("using WINMM\n");
1428 /* if no DirectSound interface available, use WINMM API instead */
1429 device->drvdesc.dwFlags = DSDDESC_DOMMSYSTEMOPEN |
1430 DSDDESC_DOMMSYSTEMSETFORMAT;
1433 device->drvdesc.dnDevNode = wid;
1435 /* open the DirectSound driver if available */
1436 if (device->driver && (err == DS_OK))
1437 err = IDsCaptureDriver_Open(device->driver);
1442 /* the driver is now open, so it's now allowed to call GetCaps */
1443 if (device->driver) {
1444 device->drvcaps.dwSize = sizeof(device->drvcaps);
1445 err = IDsCaptureDriver_GetCaps(device->driver,&(device->drvcaps));
1447 WARN("IDsCaptureDriver_GetCaps failed\n");
1450 } else /*if (device->hwi)*/ {
1452 err = mmErr(waveInGetDevCapsA((UINT)device->drvdesc.dnDevNode, &wic, sizeof(wic)));
1455 device->drvcaps.dwFlags = 0;
1456 lstrcpynA(device->drvdesc.szDrvname, wic.szPname,
1457 sizeof(device->drvdesc.szDrvname));
1459 device->drvcaps.dwFlags |= DSCCAPS_EMULDRIVER;
1460 device->drvcaps.dwFormats = wic.dwFormats;
1461 device->drvcaps.dwChannels = wic.wChannels;
1469 static HRESULT DirectSoundCaptureDevice_Create(
1470 DirectSoundCaptureDevice ** ppDevice)
1472 DirectSoundCaptureDevice * device;
1473 TRACE("(%p)\n", ppDevice);
1475 /* Allocate memory */
1476 device = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DirectSoundCaptureDevice));
1478 if (device == NULL) {
1479 WARN("out of memory\n");
1480 return DSERR_OUTOFMEMORY;
1484 device->state = STATE_STOPPED;
1486 InitializeCriticalSection( &(device->lock) );
1487 device->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": DirectSoundCaptureDevice.lock");
1494 ULONG DirectSoundCaptureDevice_Release(
1495 DirectSoundCaptureDevice * device)
1497 ULONG ref = InterlockedDecrement(&(device->ref));
1498 TRACE("(%p) ref was %d\n", device, ref + 1);
1501 TRACE("deleting object\n");
1502 if (device->capture_buffer)
1503 IDirectSoundCaptureBufferImpl_Release(
1504 (LPDIRECTSOUNDCAPTUREBUFFER8) device->capture_buffer);
1506 if (device->driver) {
1507 IDsCaptureDriver_Close(device->driver);
1508 IDsCaptureDriver_Release(device->driver);
1511 HeapFree(GetProcessHeap(), 0, device->pwfx);
1512 device->lock.DebugInfo->Spare[0] = 0;
1513 DeleteCriticalSection( &(device->lock) );
1514 DSOUND_capture[device->drvdesc.dnDevNode] = NULL;
1515 HeapFree(GetProcessHeap(), 0, device);
1516 TRACE("(%p) released\n", device);