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
40 #include "wine/debug.h"
43 #include "dsound_private.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(dsound);
47 DirectSoundCaptureDevice * DSOUND_capture[MAXWAVEDRIVERS];
49 static HRESULT DirectSoundCaptureDevice_Create(DirectSoundCaptureDevice ** ppDevice);
51 static const char * captureStateString[] = {
58 static HRESULT DSOUND_CaptureCreate(
59 LPDIRECTSOUNDCAPTURE *ppDSC,
62 LPDIRECTSOUNDCAPTURE pDSC;
64 TRACE("(%p,%p)\n",ppDSC,pUnkOuter);
66 /* Get dsound configuration */
67 setup_dsound_options();
69 hr = IDirectSoundCaptureImpl_Create(&pDSC);
71 IDirectSoundCapture_AddRef(pDSC);
74 WARN("IDirectSoundCaptureImpl_Create failed\n");
81 static HRESULT DSOUND_CaptureCreate8(
82 LPDIRECTSOUNDCAPTURE8 *ppDSC8,
85 LPDIRECTSOUNDCAPTURE8 pDSC8;
87 TRACE("(%p,%p)\n",ppDSC8,pUnkOuter);
89 /* Get dsound configuration */
90 setup_dsound_options();
92 hr = IDirectSoundCaptureImpl_Create(&pDSC8);
94 IDirectSoundCapture_AddRef(pDSC8);
97 WARN("IDirectSoundCaptureImpl_Create failed\n");
104 /***************************************************************************
105 * DirectSoundCaptureCreate [DSOUND.6]
107 * Create and initialize a DirectSoundCapture interface.
110 * lpcGUID [I] Address of the GUID that identifies the sound capture device.
111 * lplpDSC [O] Address of a variable to receive the interface pointer.
112 * pUnkOuter [I] Must be NULL.
116 * Failure: DSERR_NOAGGREGATION, DSERR_ALLOCATED, DSERR_INVALIDPARAM,
120 * lpcGUID must be one of the values returned from DirectSoundCaptureEnumerate
121 * or NULL for the default device or DSDEVID_DefaultCapture or
122 * DSDEVID_DefaultVoiceCapture.
124 * DSERR_ALLOCATED is returned for sound devices that do not support full duplex.
126 HRESULT WINAPI DirectSoundCaptureCreate(
128 LPDIRECTSOUNDCAPTURE *ppDSC,
132 LPDIRECTSOUNDCAPTURE pDSC;
133 TRACE("(%s,%p,%p)\n", debugstr_guid(lpcGUID), ppDSC, pUnkOuter);
136 WARN("invalid parameter: ppDSC == NULL\n");
137 return DSERR_INVALIDPARAM;
141 WARN("invalid parameter: pUnkOuter != NULL\n");
143 return DSERR_NOAGGREGATION;
146 hr = DSOUND_CaptureCreate(&pDSC, (IUnknown *)pUnkOuter);
148 hr = IDirectSoundCapture_Initialize(pDSC, lpcGUID);
150 IDirectSoundCapture_Release(pDSC);
160 /***************************************************************************
161 * DirectSoundCaptureCreate8 [DSOUND.12]
163 * Create and initialize a DirectSoundCapture interface.
166 * lpcGUID [I] Address of the GUID that identifies the sound capture device.
167 * lplpDSC [O] Address of a variable to receive the interface pointer.
168 * pUnkOuter [I] Must be NULL.
172 * Failure: DSERR_NOAGGREGATION, DSERR_ALLOCATED, DSERR_INVALIDPARAM,
176 * lpcGUID must be one of the values returned from DirectSoundCaptureEnumerate
177 * or NULL for the default device or DSDEVID_DefaultCapture or
178 * DSDEVID_DefaultVoiceCapture.
180 * DSERR_ALLOCATED is returned for sound devices that do not support full duplex.
182 HRESULT WINAPI DirectSoundCaptureCreate8(
184 LPDIRECTSOUNDCAPTURE8 *ppDSC8,
188 LPDIRECTSOUNDCAPTURE8 pDSC8;
189 TRACE("(%s,%p,%p)\n", debugstr_guid(lpcGUID), ppDSC8, pUnkOuter);
191 if (ppDSC8 == NULL) {
192 WARN("invalid parameter: ppDSC8 == NULL\n");
193 return DSERR_INVALIDPARAM;
197 WARN("invalid parameter: pUnkOuter != NULL\n");
199 return DSERR_NOAGGREGATION;
202 hr = DSOUND_CaptureCreate8(&pDSC8, (IUnknown *)pUnkOuter);
204 hr = IDirectSoundCapture_Initialize(pDSC8, lpcGUID);
206 IDirectSoundCapture_Release(pDSC8);
216 /***************************************************************************
217 * DirectSoundCaptureEnumerateA [DSOUND.7]
219 * Enumerate all DirectSound drivers installed in the system.
222 * lpDSEnumCallback [I] Address of callback function.
223 * lpContext [I] Address of user defined context passed to callback function.
227 * Failure: DSERR_INVALIDPARAM
230 DirectSoundCaptureEnumerateA(
231 LPDSENUMCALLBACKA lpDSEnumCallback,
239 TRACE("(%p,%p)\n", lpDSEnumCallback, lpContext );
241 if (lpDSEnumCallback == NULL) {
242 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
243 return DSERR_INVALIDPARAM;
246 devs = waveInGetNumDevs();
248 if (GetDeviceID(&DSDEVID_DefaultCapture, &guid) == DS_OK) {
249 for (wid = 0; wid < devs; ++wid) {
250 if (IsEqualGUID( &guid, &DSOUND_capture_guids[wid] ) ) {
251 err = mmErr(waveInMessage((HWAVEIN)wid,DRV_QUERYDSOUNDDESC,(DWORD_PTR)&desc,0));
253 TRACE("calling lpDSEnumCallback(NULL,\"%s\",\"%s\",%p)\n",
254 "Primary Sound Capture Driver",desc.szDrvname,lpContext);
255 if (lpDSEnumCallback(NULL, "Primary Sound Capture Driver", desc.szDrvname, lpContext) == FALSE)
263 for (wid = 0; wid < devs; ++wid) {
264 err = mmErr(waveInMessage((HWAVEIN)wid,DRV_QUERYDSOUNDDESC,(DWORD_PTR)&desc,0));
266 TRACE("calling lpDSEnumCallback(%s,\"%s\",\"%s\",%p)\n",
267 debugstr_guid(&DSOUND_capture_guids[wid]),desc.szDesc,desc.szDrvname,lpContext);
268 if (lpDSEnumCallback(&DSOUND_capture_guids[wid], desc.szDesc, desc.szDrvname, lpContext) == FALSE)
276 /***************************************************************************
277 * DirectSoundCaptureEnumerateW [DSOUND.8]
279 * Enumerate all DirectSound drivers installed in the system.
282 * lpDSEnumCallback [I] Address of callback function.
283 * lpContext [I] Address of user defined context passed to callback function.
287 * Failure: DSERR_INVALIDPARAM
290 DirectSoundCaptureEnumerateW(
291 LPDSENUMCALLBACKW lpDSEnumCallback,
298 WCHAR wDesc[MAXPNAMELEN];
299 WCHAR wName[MAXPNAMELEN];
301 TRACE("(%p,%p)\n", lpDSEnumCallback, lpContext );
303 if (lpDSEnumCallback == NULL) {
304 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
305 return DSERR_INVALIDPARAM;
308 devs = waveInGetNumDevs();
310 if (GetDeviceID(&DSDEVID_DefaultCapture, &guid) == DS_OK) {
311 for (wid = 0; wid < devs; ++wid) {
312 if (IsEqualGUID( &guid, &DSOUND_capture_guids[wid] ) ) {
313 err = mmErr(waveInMessage((HWAVEIN)wid,DRV_QUERYDSOUNDDESC,(DWORD_PTR)&desc,0));
315 TRACE("calling lpDSEnumCallback(NULL,\"%s\",\"%s\",%p)\n",
316 "Primary Sound Capture Driver",desc.szDrvname,lpContext);
317 MultiByteToWideChar( CP_ACP, 0, "Primary Sound Capture Driver", -1,
318 wDesc, sizeof(wDesc)/sizeof(WCHAR) );
319 MultiByteToWideChar( CP_ACP, 0, desc.szDrvname, -1,
320 wName, sizeof(wName)/sizeof(WCHAR) );
321 if (lpDSEnumCallback(NULL, wDesc, wName, lpContext) == FALSE)
329 for (wid = 0; wid < devs; ++wid) {
330 err = mmErr(waveInMessage((HWAVEIN)wid,DRV_QUERYDSOUNDDESC,(DWORD_PTR)&desc,0));
332 TRACE("calling lpDSEnumCallback(%s,\"%s\",\"%s\",%p)\n",
333 debugstr_guid(&DSOUND_capture_guids[wid]),desc.szDesc,desc.szDrvname,lpContext);
334 MultiByteToWideChar( CP_ACP, 0, desc.szDesc, -1,
335 wDesc, sizeof(wDesc)/sizeof(WCHAR) );
336 MultiByteToWideChar( CP_ACP, 0, desc.szDrvname, -1,
337 wName, sizeof(wName)/sizeof(WCHAR) );
338 if (lpDSEnumCallback((LPGUID)&DSOUND_capture_guids[wid], wDesc, wName, lpContext) == FALSE)
347 DSOUND_capture_callback(
354 DirectSoundCaptureDevice * This = (DirectSoundCaptureDevice*)dwUser;
355 TRACE("(%p,%08x(%s),%08lx,%08lx,%08lx) entering at %ld\n",hwi,msg,
356 msg == MM_WIM_OPEN ? "MM_WIM_OPEN" : msg == MM_WIM_CLOSE ? "MM_WIM_CLOSE" :
357 msg == MM_WIM_DATA ? "MM_WIM_DATA" : "UNKNOWN",dwUser,dw1,dw2,GetTickCount());
359 if (msg == MM_WIM_DATA) {
360 LPWAVEHDR pHdr = (LPWAVEHDR)dw1;
361 EnterCriticalSection( &(This->lock) );
362 TRACE("DirectSoundCapture msg=MM_WIM_DATA, old This->state=%s, old This->index=%d\n",
363 captureStateString[This->state],This->index);
364 if (This->state != STATE_STOPPED) {
365 int index = This->index;
366 if (This->state == STATE_STARTING) {
367 This->read_position = pHdr->dwBytesRecorded;
368 This->state = STATE_CAPTURING;
370 if (This->capture_buffer->nrofnotifies)
371 SetEvent(This->capture_buffer->notifies[This->index].hEventNotify);
372 This->index = (This->index + 1) % This->nrofpwaves;
373 if ( (This->index == 0) && !(This->capture_buffer->flags & DSCBSTART_LOOPING) ) {
374 TRACE("end of buffer\n");
375 This->state = STATE_STOPPED;
377 if (This->state == STATE_CAPTURING) {
378 waveInAddBuffer(hwi, &(This->pwave[index]), sizeof(WAVEHDR));
379 } else if (This->state == STATE_STOPPING) {
381 This->state = STATE_STOPPED;
385 TRACE("DirectSoundCapture new This->state=%s, new This->index=%d\n",
386 captureStateString[This->state],This->index);
387 LeaveCriticalSection( &(This->lock) );
390 TRACE("completed\n");
393 /***************************************************************************
394 * IDirectSoundCaptureImpl
396 static HRESULT WINAPI
397 IDirectSoundCaptureImpl_QueryInterface(
398 LPDIRECTSOUNDCAPTURE iface,
402 IDirectSoundCaptureImpl *This = (IDirectSoundCaptureImpl *)iface;
403 TRACE( "(%p,%s,%p)\n", This, debugstr_guid(riid), ppobj );
406 WARN("invalid parameter\n");
412 if (IsEqualIID(riid, &IID_IUnknown)) {
413 IDirectSoundCapture_AddRef((LPDIRECTSOUNDCAPTURE)This);
416 } else if (IsEqualIID(riid, &IID_IDirectSoundCapture)) {
417 IDirectSoundCapture_AddRef((LPDIRECTSOUNDCAPTURE)This);
422 WARN("unsupported riid: %s\n", debugstr_guid(riid));
423 return E_NOINTERFACE;
427 IDirectSoundCaptureImpl_AddRef( LPDIRECTSOUNDCAPTURE iface )
429 IDirectSoundCaptureImpl *This = (IDirectSoundCaptureImpl *)iface;
430 ULONG ref = InterlockedIncrement(&(This->ref));
431 TRACE("(%p) ref was %ld\n", This, ref - 1);
436 IDirectSoundCaptureImpl_Release( LPDIRECTSOUNDCAPTURE iface )
438 IDirectSoundCaptureImpl *This = (IDirectSoundCaptureImpl *)iface;
439 ULONG ref = InterlockedDecrement(&(This->ref));
440 TRACE("(%p) ref was %ld\n", This, ref + 1);
444 DirectSoundCaptureDevice_Release(This->device);
446 HeapFree( GetProcessHeap(), 0, This );
447 TRACE("(%p) released\n", This);
452 HRESULT WINAPI IDirectSoundCaptureImpl_CreateCaptureBuffer(
453 LPDIRECTSOUNDCAPTURE iface,
454 LPCDSCBUFFERDESC lpcDSCBufferDesc,
455 LPDIRECTSOUNDCAPTUREBUFFER* lplpDSCaptureBuffer,
459 IDirectSoundCaptureImpl *This = (IDirectSoundCaptureImpl *)iface;
461 TRACE( "(%p,%p,%p,%p)\n",iface,lpcDSCBufferDesc,lplpDSCaptureBuffer,pUnk);
463 if (lpcDSCBufferDesc == NULL) {
464 WARN("invalid parameter: lpcDSCBufferDesc == NULL)\n");
465 return DSERR_INVALIDPARAM;
468 if (lplpDSCaptureBuffer == NULL) {
469 WARN("invalid parameter: lplpDSCaptureBuffer == NULL\n");
470 return DSERR_INVALIDPARAM;
474 WARN("invalid parameter: pUnk != NULL\n");
475 return DSERR_INVALIDPARAM;
478 /* FIXME: We can only have one buffer so what do we do here? */
479 if (This->device->capture_buffer) {
480 WARN("lnvalid parameter: already has buffer\n");
481 return DSERR_INVALIDPARAM; /* DSERR_GENERIC ? */
484 hr = IDirectSoundCaptureBufferImpl_Create(This->device,
485 (IDirectSoundCaptureBufferImpl **)lplpDSCaptureBuffer, lpcDSCBufferDesc);
488 WARN("IDirectSoundCaptureBufferImpl_Create failed\n");
493 HRESULT WINAPI IDirectSoundCaptureImpl_GetCaps(
494 LPDIRECTSOUNDCAPTURE iface,
495 LPDSCCAPS lpDSCCaps )
497 IDirectSoundCaptureImpl *This = (IDirectSoundCaptureImpl *)iface;
498 TRACE("(%p,%p)\n",This,lpDSCCaps);
500 if (This->device == NULL) {
501 WARN("not initialized\n");
502 return DSERR_UNINITIALIZED;
505 if (lpDSCCaps== NULL) {
506 WARN("invalid parameter: lpDSCCaps== NULL\n");
507 return DSERR_INVALIDPARAM;
510 if (lpDSCCaps->dwSize < sizeof(*lpDSCCaps)) {
511 WARN("invalid parameter: lpDSCCaps->dwSize = %ld < %d\n",
512 lpDSCCaps->dwSize, sizeof(*lpDSCCaps));
513 return DSERR_INVALIDPARAM;
516 lpDSCCaps->dwFlags = This->device->drvcaps.dwFlags;
517 lpDSCCaps->dwFormats = This->device->drvcaps.dwFormats;
518 lpDSCCaps->dwChannels = This->device->drvcaps.dwChannels;
520 TRACE("(flags=0x%08lx,format=0x%08lx,channels=%ld)\n",lpDSCCaps->dwFlags,
521 lpDSCCaps->dwFormats, lpDSCCaps->dwChannels);
526 HRESULT WINAPI IDirectSoundCaptureImpl_Initialize(
527 LPDIRECTSOUNDCAPTURE iface,
530 IDirectSoundCaptureImpl *This = (IDirectSoundCaptureImpl *)iface;
531 TRACE("(%p,%s)\n", This, debugstr_guid(lpcGUID));
533 if (This->device != NULL) {
534 WARN("already initialized\n");
535 return DSERR_ALREADYINITIALIZED;
538 return DirectSoundCaptureDevice_Initialize(&This->device, lpcGUID);
541 static const IDirectSoundCaptureVtbl dscvt =
543 /* IUnknown methods */
544 IDirectSoundCaptureImpl_QueryInterface,
545 IDirectSoundCaptureImpl_AddRef,
546 IDirectSoundCaptureImpl_Release,
548 /* IDirectSoundCapture methods */
549 IDirectSoundCaptureImpl_CreateCaptureBuffer,
550 IDirectSoundCaptureImpl_GetCaps,
551 IDirectSoundCaptureImpl_Initialize
554 HRESULT IDirectSoundCaptureImpl_Create(
555 LPDIRECTSOUNDCAPTURE8 * ppDSC)
557 IDirectSoundCaptureImpl *pDSC;
558 TRACE("(%p)\n", ppDSC);
560 /* Allocate memory */
561 pDSC = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(IDirectSoundCaptureImpl));
563 WARN("out of memory\n");
565 return DSERR_OUTOFMEMORY;
568 pDSC->lpVtbl = &dscvt;
572 *ppDSC = (LPDIRECTSOUNDCAPTURE8)pDSC;
577 /*******************************************************************************
578 * IDirectSoundCaptureNotify
580 static HRESULT WINAPI IDirectSoundCaptureNotifyImpl_QueryInterface(
581 LPDIRECTSOUNDNOTIFY iface,
585 IDirectSoundCaptureNotifyImpl *This = (IDirectSoundCaptureNotifyImpl *)iface;
586 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
588 if (This->dscb == NULL) {
589 WARN("invalid parameter\n");
593 return IDirectSoundCaptureBuffer_QueryInterface((LPDIRECTSOUNDCAPTUREBUFFER)This->dscb, riid, ppobj);
596 static ULONG WINAPI IDirectSoundCaptureNotifyImpl_AddRef(LPDIRECTSOUNDNOTIFY iface)
598 IDirectSoundCaptureNotifyImpl *This = (IDirectSoundCaptureNotifyImpl *)iface;
599 ULONG ref = InterlockedIncrement(&(This->ref));
600 TRACE("(%p) ref was %ld\n", This, ref - 1);
604 static ULONG WINAPI IDirectSoundCaptureNotifyImpl_Release(LPDIRECTSOUNDNOTIFY iface)
606 IDirectSoundCaptureNotifyImpl *This = (IDirectSoundCaptureNotifyImpl *)iface;
607 ULONG ref = InterlockedDecrement(&(This->ref));
608 TRACE("(%p) ref was %ld\n", This, ref + 1);
611 if (This->dscb->hwnotify)
612 IDsDriverNotify_Release(This->dscb->hwnotify);
613 This->dscb->notify=NULL;
614 IDirectSoundCaptureBuffer_Release((LPDIRECTSOUNDCAPTUREBUFFER)This->dscb);
615 HeapFree(GetProcessHeap(),0,This);
616 TRACE("(%p) released\n", This);
621 static HRESULT WINAPI IDirectSoundCaptureNotifyImpl_SetNotificationPositions(
622 LPDIRECTSOUNDNOTIFY iface,
624 LPCDSBPOSITIONNOTIFY notify)
626 IDirectSoundCaptureNotifyImpl *This = (IDirectSoundCaptureNotifyImpl *)iface;
627 TRACE("(%p,0x%08lx,%p)\n",This,howmuch,notify);
629 if (howmuch > 0 && notify == NULL) {
630 WARN("invalid parameter: notify == NULL\n");
631 return DSERR_INVALIDPARAM;
634 if (TRACE_ON(dsound)) {
636 for (i=0;i<howmuch;i++)
637 TRACE("notify at %ld to %p\n",
638 notify[i].dwOffset,notify[i].hEventNotify);
641 if (This->dscb->hwnotify) {
643 hres = IDsDriverNotify_SetNotificationPositions(This->dscb->hwnotify, howmuch, notify);
645 WARN("IDsDriverNotify_SetNotificationPositions failed\n");
647 } else if (howmuch > 0) {
648 /* Make an internal copy of the caller-supplied array.
649 * Replace the existing copy if one is already present. */
650 if (This->dscb->notifies)
651 This->dscb->notifies = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
652 This->dscb->notifies, howmuch * sizeof(DSBPOSITIONNOTIFY));
654 This->dscb->notifies = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
655 howmuch * sizeof(DSBPOSITIONNOTIFY));
657 if (This->dscb->notifies == NULL) {
658 WARN("out of memory\n");
659 return DSERR_OUTOFMEMORY;
661 CopyMemory(This->dscb->notifies, notify, howmuch * sizeof(DSBPOSITIONNOTIFY));
662 This->dscb->nrofnotifies = howmuch;
664 HeapFree(GetProcessHeap(), 0, This->dscb->notifies);
665 This->dscb->notifies = NULL;
666 This->dscb->nrofnotifies = 0;
672 static const IDirectSoundNotifyVtbl dscnvt =
674 IDirectSoundCaptureNotifyImpl_QueryInterface,
675 IDirectSoundCaptureNotifyImpl_AddRef,
676 IDirectSoundCaptureNotifyImpl_Release,
677 IDirectSoundCaptureNotifyImpl_SetNotificationPositions,
680 HRESULT IDirectSoundCaptureNotifyImpl_Create(
681 IDirectSoundCaptureBufferImpl *dscb,
682 IDirectSoundCaptureNotifyImpl **pdscn)
684 IDirectSoundCaptureNotifyImpl * dscn;
685 TRACE("(%p,%p)\n",dscb,pdscn);
687 dscn = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(dscn));
690 WARN("out of memory\n");
691 return DSERR_OUTOFMEMORY;
695 dscn->lpVtbl = &dscnvt;
698 IDirectSoundCaptureBuffer_AddRef((LPDIRECTSOUNDCAPTUREBUFFER)dscb);
704 /*******************************************************************************
705 * IDirectSoundCaptureBuffer
707 static HRESULT WINAPI
708 IDirectSoundCaptureBufferImpl_QueryInterface(
709 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
713 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
715 TRACE( "(%p,%s,%p)\n", This, debugstr_guid(riid), ppobj );
718 WARN("invalid parameter\n");
724 if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ) {
726 hres = IDirectSoundCaptureNotifyImpl_Create(This, &This->notify);
728 if (This->device->hwbuf) {
729 hres = IDsCaptureDriverBuffer_QueryInterface(This->device->hwbuf,
730 &IID_IDsDriverNotify, (LPVOID*)&(This->hwnotify));
732 WARN("IDsCaptureDriverBuffer_QueryInterface failed\n");
738 IDirectSoundNotify_AddRef((LPDIRECTSOUNDNOTIFY)This->notify);
739 *ppobj = (LPVOID)This->notify;
743 WARN("IID_IDirectSoundNotify\n");
747 if ( IsEqualGUID( &IID_IDirectSoundCaptureBuffer, riid ) ||
748 IsEqualGUID( &IID_IDirectSoundCaptureBuffer8, riid ) ) {
749 IDirectSoundCaptureBuffer8_AddRef(iface);
754 FIXME("(%p,%s,%p) unsupported GUID\n", This, debugstr_guid(riid), ppobj);
755 return E_NOINTERFACE;
759 IDirectSoundCaptureBufferImpl_AddRef( LPDIRECTSOUNDCAPTUREBUFFER8 iface )
761 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
762 ULONG ref = InterlockedIncrement(&(This->ref));
763 TRACE("(%p) ref was %ld\n", This, ref - 1);
768 IDirectSoundCaptureBufferImpl_Release( LPDIRECTSOUNDCAPTUREBUFFER8 iface )
770 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
771 ULONG ref = InterlockedDecrement(&(This->ref));
772 TRACE("(%p) ref was %ld\n", This, ref + 1);
775 TRACE("deleting object\n");
776 if (This->device->state == STATE_CAPTURING)
777 This->device->state = STATE_STOPPING;
779 HeapFree(GetProcessHeap(),0, This->pdscbd);
781 if (This->device->hwi) {
782 waveInReset(This->device->hwi);
783 waveInClose(This->device->hwi);
784 HeapFree(GetProcessHeap(),0, This->device->pwave);
785 This->device->pwave = 0;
786 This->device->hwi = 0;
789 if (This->device->hwbuf)
790 IDsCaptureDriverBuffer_Release(This->device->hwbuf);
792 /* remove from DirectSoundCaptureDevice */
793 This->device->capture_buffer = NULL;
796 IDirectSoundNotify_Release((LPDIRECTSOUNDNOTIFY)This->notify);
798 HeapFree(GetProcessHeap(), 0, This->notifies);
799 HeapFree( GetProcessHeap(), 0, This );
800 TRACE("(%p) released\n", This);
805 static HRESULT WINAPI
806 IDirectSoundCaptureBufferImpl_GetCaps(
807 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
808 LPDSCBCAPS lpDSCBCaps )
810 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
811 TRACE( "(%p,%p)\n", This, lpDSCBCaps );
813 if (lpDSCBCaps == NULL) {
814 WARN("invalid parameter: lpDSCBCaps == NULL\n");
815 return DSERR_INVALIDPARAM;
818 if (lpDSCBCaps->dwSize < sizeof(DSCBCAPS)) {
819 WARN("invalid parameter: lpDSCBCaps->dwSize = %ld < %d\n",
820 lpDSCBCaps->dwSize, sizeof(DSCBCAPS));
821 return DSERR_INVALIDPARAM;
824 if (This->device == NULL) {
825 WARN("invalid parameter: This->device == NULL\n");
826 return DSERR_INVALIDPARAM;
829 lpDSCBCaps->dwSize = sizeof(DSCBCAPS);
830 lpDSCBCaps->dwFlags = This->flags;
831 lpDSCBCaps->dwBufferBytes = This->pdscbd->dwBufferBytes;
832 lpDSCBCaps->dwReserved = 0;
834 TRACE("returning DS_OK\n");
838 static HRESULT WINAPI
839 IDirectSoundCaptureBufferImpl_GetCurrentPosition(
840 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
841 LPDWORD lpdwCapturePosition,
842 LPDWORD lpdwReadPosition )
844 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
845 HRESULT hres = DS_OK;
846 TRACE( "(%p,%p,%p)\n", This, lpdwCapturePosition, lpdwReadPosition );
848 if (This->device == NULL) {
849 WARN("invalid parameter: This->device == NULL\n");
850 return DSERR_INVALIDPARAM;
853 if (This->device->driver) {
854 hres = IDsCaptureDriverBuffer_GetPosition(This->device->hwbuf, lpdwCapturePosition, lpdwReadPosition );
856 WARN("IDsCaptureDriverBuffer_GetPosition failed\n");
857 } else if (This->device->hwi) {
858 EnterCriticalSection(&(This->device->lock));
859 TRACE("old This->device->state=%s\n",captureStateString[This->device->state]);
860 if (lpdwCapturePosition) {
862 mtime.wType = TIME_BYTES;
863 waveInGetPosition(This->device->hwi, &mtime, sizeof(mtime));
864 TRACE("mtime.u.cb=%ld,This->device->buflen=%ld\n", mtime.u.cb,
865 This->device->buflen);
866 mtime.u.cb = mtime.u.cb % This->device->buflen;
867 *lpdwCapturePosition = mtime.u.cb;
870 if (lpdwReadPosition) {
871 if (This->device->state == STATE_STARTING) {
872 if (lpdwCapturePosition)
873 This->device->read_position = *lpdwCapturePosition;
874 This->device->state = STATE_CAPTURING;
876 *lpdwReadPosition = This->device->read_position;
878 TRACE("new This->device->state=%s\n",captureStateString[This->device->state]);
879 LeaveCriticalSection(&(This->device->lock));
880 if (lpdwCapturePosition) TRACE("*lpdwCapturePosition=%ld\n",*lpdwCapturePosition);
881 if (lpdwReadPosition) TRACE("*lpdwReadPosition=%ld\n",*lpdwReadPosition);
884 hres = DSERR_NODRIVER;
887 TRACE("returning %08lx\n", hres);
891 static HRESULT WINAPI
892 IDirectSoundCaptureBufferImpl_GetFormat(
893 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
894 LPWAVEFORMATEX lpwfxFormat,
895 DWORD dwSizeAllocated,
896 LPDWORD lpdwSizeWritten )
898 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
899 HRESULT hres = DS_OK;
900 TRACE( "(%p,%p,0x%08lx,%p)\n", This, lpwfxFormat, dwSizeAllocated,
903 if (This->device == NULL) {
904 WARN("invalid parameter: This->device == NULL\n");
905 return DSERR_INVALIDPARAM;
908 if (dwSizeAllocated > (sizeof(WAVEFORMATEX) + This->device->pwfx->cbSize))
909 dwSizeAllocated = sizeof(WAVEFORMATEX) + This->device->pwfx->cbSize;
911 if (lpwfxFormat) { /* NULL is valid (just want size) */
912 CopyMemory(lpwfxFormat, This->device->pwfx, dwSizeAllocated);
914 *lpdwSizeWritten = dwSizeAllocated;
917 *lpdwSizeWritten = sizeof(WAVEFORMATEX) + This->device->pwfx->cbSize;
919 TRACE("invalid parameter: lpdwSizeWritten = NULL\n");
920 hres = DSERR_INVALIDPARAM;
924 TRACE("returning %08lx\n", hres);
928 static HRESULT WINAPI
929 IDirectSoundCaptureBufferImpl_GetStatus(
930 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
933 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
934 TRACE( "(%p, %p), thread is %04lx\n", This, lpdwStatus, GetCurrentThreadId() );
936 if (This->device == NULL) {
937 WARN("invalid parameter: This->device == NULL\n");
938 return DSERR_INVALIDPARAM;
941 if (lpdwStatus == NULL) {
942 WARN("invalid parameter: lpdwStatus == NULL\n");
943 return DSERR_INVALIDPARAM;
947 EnterCriticalSection(&(This->device->lock));
949 TRACE("old This->device->state=%s, old lpdwStatus=%08lx\n",
950 captureStateString[This->device->state],*lpdwStatus);
951 if ((This->device->state == STATE_STARTING) ||
952 (This->device->state == STATE_CAPTURING)) {
953 *lpdwStatus |= DSCBSTATUS_CAPTURING;
954 if (This->flags & DSCBSTART_LOOPING)
955 *lpdwStatus |= DSCBSTATUS_LOOPING;
957 TRACE("new This->device->state=%s, new lpdwStatus=%08lx\n",
958 captureStateString[This->device->state],*lpdwStatus);
959 LeaveCriticalSection(&(This->device->lock));
961 TRACE("status=%lx\n", *lpdwStatus);
962 TRACE("returning DS_OK\n");
966 static HRESULT WINAPI
967 IDirectSoundCaptureBufferImpl_Initialize(
968 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
969 LPDIRECTSOUNDCAPTURE lpDSC,
970 LPCDSCBUFFERDESC lpcDSCBDesc )
972 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
974 FIXME( "(%p,%p,%p): stub\n", This, lpDSC, lpcDSCBDesc );
979 static HRESULT WINAPI
980 IDirectSoundCaptureBufferImpl_Lock(
981 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
984 LPVOID* lplpvAudioPtr1,
985 LPDWORD lpdwAudioBytes1,
986 LPVOID* lplpvAudioPtr2,
987 LPDWORD lpdwAudioBytes2,
990 HRESULT hres = DS_OK;
991 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
992 TRACE( "(%p,%08lu,%08lu,%p,%p,%p,%p,0x%08lx) at %ld\n", This, dwReadCusor,
993 dwReadBytes, lplpvAudioPtr1, lpdwAudioBytes1, lplpvAudioPtr2,
994 lpdwAudioBytes2, dwFlags, GetTickCount() );
996 if (This->device == NULL) {
997 WARN("invalid parameter: This->device == NULL\n");
998 return DSERR_INVALIDPARAM;
1001 if (lplpvAudioPtr1 == NULL) {
1002 WARN("invalid parameter: lplpvAudioPtr1 == NULL\n");
1003 return DSERR_INVALIDPARAM;
1006 if (lpdwAudioBytes1 == NULL) {
1007 WARN("invalid parameter: lpdwAudioBytes1 == NULL\n");
1008 return DSERR_INVALIDPARAM;
1011 EnterCriticalSection(&(This->device->lock));
1013 if (This->device->driver) {
1014 hres = IDsCaptureDriverBuffer_Lock(This->device->hwbuf, lplpvAudioPtr1,
1015 lpdwAudioBytes1, lplpvAudioPtr2,
1016 lpdwAudioBytes2, dwReadCusor,
1017 dwReadBytes, dwFlags);
1019 WARN("IDsCaptureDriverBuffer_Lock failed\n");
1020 } else if (This->device->hwi) {
1021 *lplpvAudioPtr1 = This->device->buffer + dwReadCusor;
1022 if ( (dwReadCusor + dwReadBytes) > This->device->buflen) {
1023 *lpdwAudioBytes1 = This->device->buflen - dwReadCusor;
1025 *lplpvAudioPtr2 = This->device->buffer;
1026 if (lpdwAudioBytes2)
1027 *lpdwAudioBytes2 = dwReadBytes - *lpdwAudioBytes1;
1029 *lpdwAudioBytes1 = dwReadBytes;
1031 *lplpvAudioPtr2 = 0;
1032 if (lpdwAudioBytes2)
1033 *lpdwAudioBytes2 = 0;
1036 TRACE("invalid call\n");
1037 hres = DSERR_INVALIDCALL; /* DSERR_NODRIVER ? */
1040 LeaveCriticalSection(&(This->device->lock));
1042 TRACE("returning %08lx\n", hres);
1046 static HRESULT WINAPI
1047 IDirectSoundCaptureBufferImpl_Start(
1048 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
1051 HRESULT hres = DS_OK;
1052 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
1053 TRACE( "(%p,0x%08lx)\n", This, dwFlags );
1055 if (This->device == NULL) {
1056 WARN("invalid parameter: This->device == NULL\n");
1057 return DSERR_INVALIDPARAM;
1060 if ( (This->device->driver == 0) && (This->device->hwi == 0) ) {
1061 WARN("no driver\n");
1062 return DSERR_NODRIVER;
1065 EnterCriticalSection(&(This->device->lock));
1067 This->flags = dwFlags;
1068 TRACE("old This->state=%s\n",captureStateString[This->device->state]);
1069 if (This->device->state == STATE_STOPPED)
1070 This->device->state = STATE_STARTING;
1071 else if (This->device->state == STATE_STOPPING)
1072 This->device->state = STATE_CAPTURING;
1073 TRACE("new This->device->state=%s\n",captureStateString[This->device->state]);
1075 LeaveCriticalSection(&(This->device->lock));
1077 if (This->device->driver) {
1078 hres = IDsCaptureDriverBuffer_Start(This->device->hwbuf, dwFlags);
1080 WARN("IDsCaptureDriverBuffer_Start failed\n");
1081 } else if (This->device->hwi) {
1082 DirectSoundCaptureDevice *device = This->device;
1084 if (device->buffer) {
1085 if (This->nrofnotifies) {
1088 device->nrofpwaves = This->nrofnotifies;
1089 TRACE("nrofnotifies=%d\n", This->nrofnotifies);
1091 /* prepare headers */
1093 device->pwave = HeapReAlloc(GetProcessHeap(),0,device->pwave,
1094 device->nrofpwaves*sizeof(WAVEHDR));
1096 device->pwave = HeapAlloc(GetProcessHeap(),0,
1097 device->nrofpwaves*sizeof(WAVEHDR));
1099 for (c = 0; c < device->nrofpwaves; c++) {
1100 if (This->notifies[c].dwOffset == DSBPN_OFFSETSTOP) {
1101 TRACE("got DSBPN_OFFSETSTOP\n");
1102 device->nrofpwaves = c;
1106 device->pwave[0].lpData = (LPSTR)device->buffer;
1107 device->pwave[0].dwBufferLength =
1108 This->notifies[0].dwOffset + 1;
1110 device->pwave[c].lpData = (LPSTR)device->buffer +
1111 This->notifies[c-1].dwOffset + 1;
1112 device->pwave[c].dwBufferLength =
1113 This->notifies[c].dwOffset -
1114 This->notifies[c-1].dwOffset;
1116 device->pwave[c].dwBytesRecorded = 0;
1117 device->pwave[c].dwUser = (DWORD)device;
1118 device->pwave[c].dwFlags = 0;
1119 device->pwave[c].dwLoops = 0;
1120 hres = mmErr(waveInPrepareHeader(device->hwi,
1121 &(device->pwave[c]),sizeof(WAVEHDR)));
1122 if (hres != DS_OK) {
1123 WARN("waveInPrepareHeader failed\n");
1125 waveInUnprepareHeader(device->hwi,
1126 &(device->pwave[c]),sizeof(WAVEHDR));
1130 hres = mmErr(waveInAddBuffer(device->hwi,
1131 &(device->pwave[c]), sizeof(WAVEHDR)));
1132 if (hres != DS_OK) {
1133 WARN("waveInAddBuffer failed\n");
1135 waveInUnprepareHeader(device->hwi,
1136 &(device->pwave[c]),sizeof(WAVEHDR));
1141 FillMemory(device->buffer, device->buflen,
1142 (device->pwfx->wBitsPerSample == 8) ? 128 : 0);
1144 TRACE("no notifiers specified\n");
1145 /* no notifiers specified so just create a single default header */
1146 device->nrofpwaves = 1;
1148 device->pwave = HeapReAlloc(GetProcessHeap(),0,device->pwave,sizeof(WAVEHDR));
1150 device->pwave = HeapAlloc(GetProcessHeap(),0,sizeof(WAVEHDR));
1152 device->pwave[0].lpData = (LPSTR)device->buffer;
1153 device->pwave[0].dwBufferLength = device->buflen;
1154 device->pwave[0].dwBytesRecorded = 0;
1155 device->pwave[0].dwUser = (DWORD)device;
1156 device->pwave[0].dwFlags = 0;
1157 device->pwave[0].dwLoops = 0;
1159 hres = mmErr(waveInPrepareHeader(device->hwi,
1160 &(device->pwave[0]),sizeof(WAVEHDR)));
1161 if (hres != DS_OK) {
1162 WARN("waveInPrepareHeader failed\n");
1163 waveInUnprepareHeader(device->hwi,
1164 &(device->pwave[0]),sizeof(WAVEHDR));
1166 hres = mmErr(waveInAddBuffer(device->hwi,
1167 &(device->pwave[0]), sizeof(WAVEHDR)));
1168 if (hres != DS_OK) {
1169 WARN("waveInAddBuffer failed\n");
1170 waveInUnprepareHeader(device->hwi,
1171 &(device->pwave[0]),sizeof(WAVEHDR));
1177 device->read_position = 0;
1179 if (hres == DS_OK) {
1180 /* start filling the first buffer */
1181 hres = mmErr(waveInStart(device->hwi));
1183 WARN("waveInStart failed\n");
1186 if (hres != DS_OK) {
1187 WARN("calling waveInClose because of error\n");
1188 waveInClose(device->hwi);
1192 WARN("no driver\n");
1193 hres = DSERR_NODRIVER;
1196 TRACE("returning %08lx\n", hres);
1200 static HRESULT WINAPI
1201 IDirectSoundCaptureBufferImpl_Stop( LPDIRECTSOUNDCAPTUREBUFFER8 iface )
1203 HRESULT hres = DS_OK;
1204 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
1205 TRACE( "(%p)\n", This );
1207 if (This->device == NULL) {
1208 WARN("invalid parameter: This->device == NULL\n");
1209 return DSERR_INVALIDPARAM;
1212 EnterCriticalSection(&(This->device->lock));
1214 TRACE("old This->device->state=%s\n",captureStateString[This->device->state]);
1215 if (This->device->state == STATE_CAPTURING)
1216 This->device->state = STATE_STOPPING;
1217 else if (This->device->state == STATE_STARTING)
1218 This->device->state = STATE_STOPPED;
1219 TRACE("new This->device->state=%s\n",captureStateString[This->device->state]);
1221 LeaveCriticalSection(&(This->device->lock));
1223 if (This->device->driver) {
1224 hres = IDsCaptureDriverBuffer_Stop(This->device->hwbuf);
1226 WARN("IDsCaptureDriverBuffer_Stop() failed\n");
1227 } else if (This->device->hwi) {
1228 hres = mmErr(waveInReset(This->device->hwi));
1230 WARN("waveInReset() failed\n");
1232 WARN("no driver\n");
1233 hres = DSERR_NODRIVER;
1236 TRACE("returning %08lx\n", hres);
1240 static HRESULT WINAPI
1241 IDirectSoundCaptureBufferImpl_Unlock(
1242 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
1243 LPVOID lpvAudioPtr1,
1244 DWORD dwAudioBytes1,
1245 LPVOID lpvAudioPtr2,
1246 DWORD dwAudioBytes2 )
1248 HRESULT hres = DS_OK;
1249 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
1250 TRACE( "(%p,%p,%08lu,%p,%08lu)\n", This, lpvAudioPtr1, dwAudioBytes1,
1251 lpvAudioPtr2, dwAudioBytes2 );
1253 if (lpvAudioPtr1 == NULL) {
1254 WARN("invalid parameter: lpvAudioPtr1 == NULL\n");
1255 return DSERR_INVALIDPARAM;
1258 if (This->device->driver) {
1259 hres = IDsCaptureDriverBuffer_Unlock(This->device->hwbuf, lpvAudioPtr1,
1260 dwAudioBytes1, lpvAudioPtr2, dwAudioBytes2);
1262 WARN("IDsCaptureDriverBuffer_Unlock failed\n");
1263 } else if (This->device->hwi) {
1264 This->device->read_position = (This->device->read_position +
1265 (dwAudioBytes1 + dwAudioBytes2)) % This->device->buflen;
1267 WARN("invalid call\n");
1268 hres = DSERR_INVALIDCALL;
1271 TRACE("returning %08lx\n", hres);
1275 static HRESULT WINAPI
1276 IDirectSoundCaptureBufferImpl_GetObjectInPath(
1277 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
1278 REFGUID rguidObject,
1280 REFGUID rguidInterface,
1283 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
1285 FIXME( "(%p,%s,%lu,%s,%p): stub\n", This, debugstr_guid(rguidObject),
1286 dwIndex, debugstr_guid(rguidInterface), ppObject );
1291 static HRESULT WINAPI
1292 IDirectSoundCaptureBufferImpl_GetFXStatus(
1293 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
1295 LPDWORD pdwFXStatus )
1297 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
1299 FIXME( "(%p,%lu,%p): stub\n", This, dwFXCount, pdwFXStatus );
1304 static const IDirectSoundCaptureBuffer8Vtbl dscbvt =
1306 /* IUnknown methods */
1307 IDirectSoundCaptureBufferImpl_QueryInterface,
1308 IDirectSoundCaptureBufferImpl_AddRef,
1309 IDirectSoundCaptureBufferImpl_Release,
1311 /* IDirectSoundCaptureBuffer methods */
1312 IDirectSoundCaptureBufferImpl_GetCaps,
1313 IDirectSoundCaptureBufferImpl_GetCurrentPosition,
1314 IDirectSoundCaptureBufferImpl_GetFormat,
1315 IDirectSoundCaptureBufferImpl_GetStatus,
1316 IDirectSoundCaptureBufferImpl_Initialize,
1317 IDirectSoundCaptureBufferImpl_Lock,
1318 IDirectSoundCaptureBufferImpl_Start,
1319 IDirectSoundCaptureBufferImpl_Stop,
1320 IDirectSoundCaptureBufferImpl_Unlock,
1322 /* IDirectSoundCaptureBuffer methods */
1323 IDirectSoundCaptureBufferImpl_GetObjectInPath,
1324 IDirectSoundCaptureBufferImpl_GetFXStatus
1327 HRESULT IDirectSoundCaptureBufferImpl_Create(
1328 DirectSoundCaptureDevice *device,
1329 IDirectSoundCaptureBufferImpl ** ppobj,
1330 LPCDSCBUFFERDESC lpcDSCBufferDesc)
1332 LPWAVEFORMATEX wfex;
1333 TRACE( "(%p,%p,%p)\n", device, ppobj, lpcDSCBufferDesc);
1335 if (ppobj == NULL) {
1336 WARN("invalid parameter: ppobj == NULL\n");
1337 return DSERR_INVALIDPARAM;
1341 WARN("not initialized\n");
1343 return DSERR_UNINITIALIZED;
1346 if (lpcDSCBufferDesc == NULL) {
1347 WARN("invalid parameter: lpcDSCBufferDesc == NULL\n");
1349 return DSERR_INVALIDPARAM;
1352 if ( ((lpcDSCBufferDesc->dwSize != sizeof(DSCBUFFERDESC)) &&
1353 (lpcDSCBufferDesc->dwSize != sizeof(DSCBUFFERDESC1))) ||
1354 (lpcDSCBufferDesc->dwBufferBytes == 0) ||
1355 (lpcDSCBufferDesc->lpwfxFormat == NULL) ) {
1356 WARN("invalid lpcDSCBufferDesc\n");
1358 return DSERR_INVALIDPARAM;
1361 wfex = lpcDSCBufferDesc->lpwfxFormat;
1364 TRACE("(formattag=0x%04x,chans=%d,samplerate=%ld,"
1365 "bytespersec=%ld,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
1366 wfex->wFormatTag, wfex->nChannels, wfex->nSamplesPerSec,
1367 wfex->nAvgBytesPerSec, wfex->nBlockAlign,
1368 wfex->wBitsPerSample, wfex->cbSize);
1370 if (wfex->wFormatTag == WAVE_FORMAT_PCM) {
1371 device->pwfx = HeapAlloc(GetProcessHeap(),0,sizeof(WAVEFORMATEX));
1372 CopyMemory(device->pwfx, wfex, sizeof(WAVEFORMATEX));
1373 device->pwfx->cbSize = 0;
1375 device->pwfx = HeapAlloc(GetProcessHeap(),0,sizeof(WAVEFORMATEX)+wfex->cbSize);
1376 CopyMemory(device->pwfx, wfex, sizeof(WAVEFORMATEX)+wfex->cbSize);
1379 WARN("lpcDSCBufferDesc->lpwfxFormat == 0\n");
1381 return DSERR_INVALIDPARAM; /* FIXME: DSERR_BADFORMAT ? */
1384 *ppobj = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,
1385 sizeof(IDirectSoundCaptureBufferImpl));
1387 if ( *ppobj == NULL ) {
1388 WARN("out of memory\n");
1390 return DSERR_OUTOFMEMORY;
1392 HRESULT err = DS_OK;
1395 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)*ppobj;
1398 This->device = device;
1399 This->device->capture_buffer = This;
1400 This->notify = NULL;
1401 This->nrofnotifies = 0;
1402 This->hwnotify = NULL;
1404 This->pdscbd = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,
1405 lpcDSCBufferDesc->dwSize);
1407 CopyMemory(This->pdscbd, lpcDSCBufferDesc, lpcDSCBufferDesc->dwSize);
1409 WARN("no memory\n");
1410 This->device->capture_buffer = 0;
1411 HeapFree( GetProcessHeap(), 0, This );
1413 return DSERR_OUTOFMEMORY;
1416 This->lpVtbl = &dscbvt;
1418 if (device->driver) {
1419 if (This->device->drvdesc.dwFlags & DSDDESC_DOMMSYSTEMOPEN)
1420 FIXME("DSDDESC_DOMMSYSTEMOPEN not supported\n");
1422 if (This->device->drvdesc.dwFlags & DSDDESC_USESYSTEMMEMORY) {
1423 /* allocate buffer from system memory */
1424 buflen = lpcDSCBufferDesc->dwBufferBytes;
1425 TRACE("desired buflen=%ld, old buffer=%p\n", buflen, device->buffer);
1427 newbuf = HeapReAlloc(GetProcessHeap(),0,device->buffer,buflen);
1429 newbuf = HeapAlloc(GetProcessHeap(),0,buflen);
1431 if (newbuf == NULL) {
1432 WARN("failed to allocate capture buffer\n");
1433 err = DSERR_OUTOFMEMORY;
1434 /* but the old buffer might still exist and must be re-prepared */
1436 device->buffer = newbuf;
1437 device->buflen = buflen;
1440 /* let driver allocate memory */
1441 device->buflen = lpcDSCBufferDesc->dwBufferBytes;
1443 HeapFree( GetProcessHeap(), 0, device->buffer);
1444 device->buffer = NULL;
1447 err = IDsCaptureDriver_CreateCaptureBuffer(device->driver,
1448 device->pwfx,0,0,&(device->buflen),&(device->buffer),(LPVOID*)&(device->hwbuf));
1450 WARN("IDsCaptureDriver_CreateCaptureBuffer failed\n");
1451 This->device->capture_buffer = 0;
1452 HeapFree( GetProcessHeap(), 0, This );
1457 DWORD flags = CALLBACK_FUNCTION;
1458 if (ds_hw_accel != DS_HW_ACCEL_EMULATION)
1459 flags |= WAVE_DIRECTSOUND;
1460 err = mmErr(waveInOpen(&(device->hwi),
1461 device->drvdesc.dnDevNode, device->pwfx,
1462 (DWORD_PTR)DSOUND_capture_callback, (DWORD)device, flags));
1464 WARN("waveInOpen failed\n");
1465 This->device->capture_buffer = 0;
1466 HeapFree( GetProcessHeap(), 0, This );
1471 buflen = lpcDSCBufferDesc->dwBufferBytes;
1472 TRACE("desired buflen=%ld, old buffer=%p\n", buflen, device->buffer);
1474 newbuf = HeapReAlloc(GetProcessHeap(),0,device->buffer,buflen);
1476 newbuf = HeapAlloc(GetProcessHeap(),0,buflen);
1477 if (newbuf == NULL) {
1478 WARN("failed to allocate capture buffer\n");
1479 err = DSERR_OUTOFMEMORY;
1480 /* but the old buffer might still exist and must be re-prepared */
1482 device->buffer = newbuf;
1483 device->buflen = buflen;
1488 TRACE("returning DS_OK\n");
1492 /*******************************************************************************
1493 * DirectSoundCaptureDevice
1495 HRESULT DirectSoundCaptureDevice_Initialize(
1496 DirectSoundCaptureDevice ** ppDevice,
1499 HRESULT err = DSERR_INVALIDPARAM;
1501 BOOLEAN found = FALSE;
1503 DirectSoundCaptureDevice *device = *ppDevice;
1504 TRACE("(%p, %s)\n", ppDevice, debugstr_guid(lpcGUID));
1506 /* Default device? */
1507 if ( !lpcGUID || IsEqualGUID(lpcGUID, &GUID_NULL) )
1508 lpcGUID = &DSDEVID_DefaultCapture;
1510 if (GetDeviceID(lpcGUID, &devGUID) != DS_OK) {
1511 WARN("invalid parameter: lpcGUID\n");
1512 return DSERR_INVALIDPARAM;
1515 widn = waveInGetNumDevs();
1517 WARN("no audio devices found\n");
1518 return DSERR_NODRIVER;
1521 /* enumerate WINMM audio devices and find the one we want */
1522 for (wid=0; wid<widn; wid++) {
1523 if (IsEqualGUID( &devGUID, &DSOUND_capture_guids[wid]) ) {
1529 if (found == FALSE) {
1530 WARN("No device found matching given ID!\n");
1531 return DSERR_NODRIVER;
1534 if (DSOUND_capture[wid]) {
1535 WARN("already in use\n");
1536 return DSERR_ALLOCATED;
1539 err = DirectSoundCaptureDevice_Create(&(device));
1541 WARN("DirectSoundCaptureDevice_Create failed\n");
1546 device->guid = devGUID;
1548 err = mmErr(waveInMessage((HWAVEIN)wid,DRV_QUERYDSOUNDIFACE,(DWORD_PTR)&(device->driver),0));
1549 if ( (err != DS_OK) && (err != DSERR_UNSUPPORTED) ) {
1550 WARN("waveInMessage failed; err=%lx\n",err);
1555 /* Disable the direct sound driver to force emulation if requested. */
1556 if (ds_hw_accel == DS_HW_ACCEL_EMULATION)
1557 device->driver = NULL;
1559 /* Get driver description */
1560 if (device->driver) {
1561 TRACE("using DirectSound driver\n");
1562 err = IDsCaptureDriver_GetDriverDesc(device->driver, &(device->drvdesc));
1564 WARN("IDsCaptureDriver_GetDriverDesc failed\n");
1568 TRACE("using WINMM\n");
1569 /* if no DirectSound interface available, use WINMM API instead */
1570 device->drvdesc.dwFlags = DSDDESC_DOMMSYSTEMOPEN |
1571 DSDDESC_DOMMSYSTEMSETFORMAT;
1574 device->drvdesc.dnDevNode = wid;
1576 /* open the DirectSound driver if available */
1577 if (device->driver && (err == DS_OK))
1578 err = IDsCaptureDriver_Open(device->driver);
1583 /* the driver is now open, so it's now allowed to call GetCaps */
1584 if (device->driver) {
1585 device->drvcaps.dwSize = sizeof(device->drvcaps);
1586 err = IDsCaptureDriver_GetCaps(device->driver,&(device->drvcaps));
1588 WARN("IDsCaptureDriver_GetCaps failed\n");
1591 } else /*if (device->hwi)*/ {
1593 err = mmErr(waveInGetDevCapsA((UINT)device->drvdesc.dnDevNode, &wic, sizeof(wic)));
1596 device->drvcaps.dwFlags = 0;
1597 lstrcpynA(device->drvdesc.szDrvname, wic.szPname,
1598 sizeof(device->drvdesc.szDrvname));
1600 device->drvcaps.dwFlags |= DSCCAPS_EMULDRIVER;
1601 device->drvcaps.dwFormats = wic.dwFormats;
1602 device->drvcaps.dwChannels = wic.wChannels;
1610 static HRESULT DirectSoundCaptureDevice_Create(
1611 DirectSoundCaptureDevice ** ppDevice)
1613 DirectSoundCaptureDevice * device;
1614 TRACE("(%p)\n", ppDevice);
1616 /* Allocate memory */
1617 device = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DirectSoundCaptureDevice));
1619 if (device == NULL) {
1620 WARN("out of memory\n");
1621 return DSERR_OUTOFMEMORY;
1625 device->state = STATE_STOPPED;
1627 InitializeCriticalSection( &(device->lock) );
1628 device->lock.DebugInfo->Spare[0] = (DWORD_PTR)"DSCAPTURE_lock";
1635 ULONG DirectSoundCaptureDevice_AddRef(
1636 DirectSoundCaptureDevice * device)
1638 ULONG ref = InterlockedIncrement(&(device->ref));
1639 TRACE("(%p) ref was %ld\n", device, ref - 1);
1643 ULONG DirectSoundCaptureDevice_Release(
1644 DirectSoundCaptureDevice * device)
1646 ULONG ref = InterlockedDecrement(&(device->ref));
1647 TRACE("(%p) ref was %ld\n", device, ref + 1);
1650 TRACE("deleting object\n");
1651 if (device->capture_buffer)
1652 IDirectSoundCaptureBufferImpl_Release(
1653 (LPDIRECTSOUNDCAPTUREBUFFER8) device->capture_buffer);
1655 if (device->driver) {
1656 IDsCaptureDriver_Close(device->driver);
1657 IDsCaptureDriver_Release(device->driver);
1660 HeapFree(GetProcessHeap(), 0, device->pwfx);
1661 device->lock.DebugInfo->Spare[0] = 0;
1662 DeleteCriticalSection( &(device->lock) );
1663 DSOUND_capture[device->drvdesc.dnDevNode] = NULL;
1664 HeapFree(GetProcessHeap(), 0, device);
1665 TRACE("(%p) released\n", device);
1670 /*******************************************************************************
1671 * DirectSoundCapture ClassFactory
1674 static HRESULT WINAPI
1675 DSCCF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj)
1677 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
1679 FIXME("(%p)->(%s,%p),stub!\n",This,debugstr_guid(riid),ppobj);
1680 return E_NOINTERFACE;
1684 DSCCF_AddRef(LPCLASSFACTORY iface)
1686 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
1687 ULONG ref = InterlockedIncrement(&(This->ref));
1688 TRACE("(%p) ref was %ld\n", This, ref - 1);
1693 DSCCF_Release(LPCLASSFACTORY iface)
1695 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
1696 ULONG ref = InterlockedDecrement(&(This->ref));
1697 TRACE("(%p) ref was %ld\n", This, ref + 1);
1698 /* static class, won't be freed */
1702 static HRESULT WINAPI
1703 DSCCF_CreateInstance(
1704 LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj )
1706 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
1707 TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
1710 WARN("aggregation not supported\n");
1711 return CLASS_E_NOAGGREGATION;
1714 if (ppobj == NULL) {
1715 WARN("invalid parameter\n");
1716 return E_INVALIDARG;
1721 if ( IsEqualGUID( &IID_IDirectSoundCapture, riid ) )
1722 return DSOUND_CaptureCreate8((LPDIRECTSOUNDCAPTURE*)ppobj,pOuter);
1724 WARN("(%p,%p,%s,%p) Interface not found!\n",This,pOuter,debugstr_guid(riid),ppobj);
1725 return E_NOINTERFACE;
1728 static HRESULT WINAPI
1729 DSCCF_LockServer(LPCLASSFACTORY iface,BOOL dolock)
1731 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
1732 FIXME("(%p)->(%d),stub!\n",This,dolock);
1736 static const IClassFactoryVtbl DSCCF_Vtbl =
1738 DSCCF_QueryInterface,
1741 DSCCF_CreateInstance,
1745 IClassFactoryImpl DSOUND_CAPTURE_CF = { &DSCCF_Vtbl, 1 };