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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Implement DirectSoundFullDuplex support.
24 * Implement FX support.
31 #include <sys/types.h>
32 #include <sys/fcntl.h>
49 #include "wine/debug.h"
52 #include "dsound_private.h"
54 WINE_DEFAULT_DEBUG_CHANNEL(dsound);
56 static HRESULT WINAPI IDirectSoundCaptureImpl_Initialize(
57 LPDIRECTSOUNDCAPTURE iface,
59 static ULONG WINAPI IDirectSoundCaptureImpl_Release(
60 LPDIRECTSOUNDCAPTURE iface );
61 static ULONG WINAPI IDirectSoundCaptureBufferImpl_Release(
62 LPDIRECTSOUNDCAPTUREBUFFER8 iface );
63 static HRESULT DSOUND_CreateDirectSoundCaptureBuffer(
64 IDirectSoundCaptureImpl *ipDSC,
65 LPCDSCBUFFERDESC lpcDSCBufferDesc,
67 static HRESULT WINAPI IDirectSoundFullDuplexImpl_Initialize(
68 LPDIRECTSOUNDFULLDUPLEX iface,
70 LPCGUID pRendererGuid,
71 LPCDSCBUFFERDESC lpDscBufferDesc,
72 LPCDSBUFFERDESC lpDsBufferDesc,
75 LPLPDIRECTSOUNDCAPTUREBUFFER8 lplpDirectSoundCaptureBuffer8,
76 LPLPDIRECTSOUNDBUFFER8 lplpDirectSoundBuffer8 );
78 static IDirectSoundCaptureVtbl dscvt;
79 static IDirectSoundCaptureBuffer8Vtbl dscbvt;
80 static IDirectSoundFullDuplexVtbl dsfdvt;
82 static IDirectSoundCaptureImpl* dsound_capture = NULL;
84 static const char * captureStateString[] = {
91 /***************************************************************************
92 * DirectSoundCaptureCreate [DSOUND.6]
94 * Create and initialize a DirectSoundCapture interface.
97 * lpcGUID [I] Address of the GUID that identifies the sound capture device.
98 * lplpDSC [O] Address of a variable to receive the interface pointer.
99 * pUnkOuter [I] Must be NULL.
103 * Failure: DSERR_NOAGGREGATION, DSERR_ALLOCATED, DSERR_INVALIDPARAM,
107 * lpcGUID must be one of the values returned from DirectSoundCaptureEnumerate
108 * or NULL for the default device or DSDEVID_DefaultCapture or
109 * DSDEVID_DefaultVoiceCapture.
111 * DSERR_ALLOCATED is returned for sound devices that do not support full duplex.
114 DirectSoundCaptureCreate8(
116 LPDIRECTSOUNDCAPTURE* lplpDSC,
117 LPUNKNOWN pUnkOuter )
119 IDirectSoundCaptureImpl** ippDSC=(IDirectSoundCaptureImpl**)lplpDSC;
120 TRACE("(%s,%p,%p)\n", debugstr_guid(lpcGUID), lplpDSC, pUnkOuter);
123 WARN("invalid parameter: pUnkOuter != NULL\n");
124 return DSERR_NOAGGREGATION;
128 WARN("invalid parameter: lplpDSC == NULL\n");
129 return DSERR_INVALIDPARAM;
132 /* Default device? */
133 if ( !lpcGUID || IsEqualGUID(lpcGUID, &GUID_NULL) )
134 lpcGUID = &DSDEVID_DefaultCapture;
136 *ippDSC = HeapAlloc(GetProcessHeap(),
137 HEAP_ZERO_MEMORY, sizeof(IDirectSoundCaptureImpl));
139 if (*ippDSC == NULL) {
140 WARN("out of memory\n");
141 return DSERR_OUTOFMEMORY;
143 IDirectSoundCaptureImpl *This = (IDirectSoundCaptureImpl *)*ippDSC;
146 This->state = STATE_STOPPED;
148 InitializeCriticalSection( &(This->lock) );
149 This->lock.DebugInfo->Spare[1] = (DWORD)"DSCAPTURE_lock";
151 This->lpVtbl = &dscvt;
152 dsound_capture = This;
154 if (GetDeviceID(lpcGUID, &This->guid) == DS_OK) {
156 hres = IDirectSoundCaptureImpl_Initialize( (LPDIRECTSOUNDCAPTURE)This, &This->guid);
158 WARN("IDirectSoundCaptureImpl_Initialize failed\n");
162 WARN("invalid GUID: %s\n", debugstr_guid(lpcGUID));
163 return DSERR_INVALIDPARAM;
166 /***************************************************************************
167 * DirectSoundCaptureEnumerateA [DSOUND.7]
169 * Enumerate all DirectSound drivers installed in the system.
172 * lpDSEnumCallback [I] Address of callback function.
173 * lpContext [I] Address of user defined context passed to callback function.
177 * Failure: DSERR_INVALIDPARAM
180 DirectSoundCaptureEnumerateA(
181 LPDSENUMCALLBACKA lpDSEnumCallback,
189 TRACE("(%p,%p)\n", lpDSEnumCallback, lpContext );
191 if (lpDSEnumCallback == NULL) {
192 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
193 return DSERR_INVALIDPARAM;
196 devs = waveInGetNumDevs();
198 if (GetDeviceID(&DSDEVID_DefaultCapture, &guid) == DS_OK) {
199 for (wid = 0; wid < devs; ++wid) {
200 if (IsEqualGUID( &guid, &capture_guids[wid] ) ) {
201 err = mmErr(waveInMessage((HWAVEIN)wid,DRV_QUERYDSOUNDDESC,(DWORD)&desc,0));
203 TRACE("calling lpDSEnumCallback(NULL,\"%s\",\"%s\",%p)\n",
204 "Primary Sound Capture Driver",desc.szDrvName,lpContext);
205 if (lpDSEnumCallback(NULL, "Primary Sound Capture Driver", desc.szDrvName, lpContext) == FALSE)
213 for (wid = 0; wid < devs; ++wid) {
214 err = mmErr(waveInMessage((HWAVEIN)wid,DRV_QUERYDSOUNDDESC,(DWORD)&desc,0));
216 TRACE("calling lpDSEnumCallback(%s,\"%s\",\"%s\",%p)\n",
217 debugstr_guid(&capture_guids[wid]),desc.szDesc,desc.szDrvName,lpContext);
218 if (lpDSEnumCallback(&capture_guids[wid], desc.szDesc, desc.szDrvName, lpContext) == FALSE)
226 /***************************************************************************
227 * DirectSoundCaptureEnumerateW [DSOUND.8]
229 * Enumerate all DirectSound drivers installed in the system.
232 * lpDSEnumCallback [I] Address of callback function.
233 * lpContext [I] Address of user defined context passed to callback function.
237 * Failure: DSERR_INVALIDPARAM
240 DirectSoundCaptureEnumerateW(
241 LPDSENUMCALLBACKW lpDSEnumCallback,
248 WCHAR wDesc[MAXPNAMELEN];
249 WCHAR wName[MAXPNAMELEN];
251 TRACE("(%p,%p)\n", lpDSEnumCallback, lpContext );
253 if (lpDSEnumCallback == NULL) {
254 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
255 return DSERR_INVALIDPARAM;
258 devs = waveInGetNumDevs();
260 if (GetDeviceID(&DSDEVID_DefaultCapture, &guid) == DS_OK) {
261 for (wid = 0; wid < devs; ++wid) {
262 if (IsEqualGUID( &guid, &capture_guids[wid] ) ) {
263 err = mmErr(waveInMessage((HWAVEIN)wid,DRV_QUERYDSOUNDDESC,(DWORD)&desc,0));
265 TRACE("calling lpDSEnumCallback(NULL,\"%s\",\"%s\",%p)\n",
266 "Primary Sound Capture Driver",desc.szDrvName,lpContext);
267 MultiByteToWideChar( CP_ACP, 0, "Primary Sound Capture Driver", -1,
268 wDesc, sizeof(wDesc)/sizeof(WCHAR) );
269 MultiByteToWideChar( CP_ACP, 0, desc.szDrvName, -1,
270 wName, sizeof(wName)/sizeof(WCHAR) );
271 if (lpDSEnumCallback(NULL, wDesc, wName, lpContext) == FALSE)
279 for (wid = 0; wid < devs; ++wid) {
280 err = mmErr(waveInMessage((HWAVEIN)wid,DRV_QUERYDSOUNDDESC,(DWORD)&desc,0));
282 TRACE("calling lpDSEnumCallback(%s,\"%s\",\"%s\",%p)\n",
283 debugstr_guid(&capture_guids[wid]),desc.szDesc,desc.szDrvName,lpContext);
284 MultiByteToWideChar( CP_ACP, 0, desc.szDesc, -1,
285 wDesc, sizeof(wDesc)/sizeof(WCHAR) );
286 MultiByteToWideChar( CP_ACP, 0, desc.szDrvName, -1,
287 wName, sizeof(wName)/sizeof(WCHAR) );
288 if (lpDSEnumCallback((LPGUID)&capture_guids[wid], wDesc, wName, lpContext) == FALSE)
297 DSOUND_capture_callback(
304 IDirectSoundCaptureImpl* This = (IDirectSoundCaptureImpl*)dwUser;
305 TRACE("(%p,%08x(%s),%08lx,%08lx,%08lx) entering at %ld\n",hwi,msg,
306 msg == MM_WIM_OPEN ? "MM_WIM_OPEN" : msg == MM_WIM_CLOSE ? "MM_WIM_CLOSE" :
307 msg == MM_WIM_DATA ? "MM_WIM_DATA" : "UNKNOWN",dwUser,dw1,dw2,GetTickCount());
309 if (msg == MM_WIM_DATA) {
310 LPWAVEHDR pHdr = (LPWAVEHDR)dw1;
311 EnterCriticalSection( &(This->lock) );
312 TRACE("DirectSoundCapture msg=MM_WIM_DATA, old This->state=%s, old This->index=%d\n",
313 captureStateString[This->state],This->index);
314 if (This->state != STATE_STOPPED) {
315 int index = This->index;
316 if (This->state == STATE_STARTING) {
317 This->read_position = pHdr->dwBytesRecorded;
318 This->state = STATE_CAPTURING;
320 waveInUnprepareHeader(hwi,&(This->pwave[This->index]),sizeof(WAVEHDR));
321 if (This->capture_buffer->nrofnotifies)
322 SetEvent(This->capture_buffer->notifies[This->index].hEventNotify);
323 This->index = (This->index + 1) % This->nrofpwaves;
324 if ( (This->index == 0) && !(This->capture_buffer->flags & DSCBSTART_LOOPING) ) {
325 TRACE("end of buffer\n");
326 This->state = STATE_STOPPED;
328 if (This->state == STATE_CAPTURING) {
329 waveInPrepareHeader(hwi,&(This->pwave[index]),sizeof(WAVEHDR));
330 waveInAddBuffer(hwi, &(This->pwave[index]), sizeof(WAVEHDR));
331 } else if (This->state == STATE_STOPPING) {
333 This->state = STATE_STOPPED;
337 TRACE("DirectSoundCapture new This->state=%s, new This->index=%d\n",
338 captureStateString[This->state],This->index);
339 LeaveCriticalSection( &(This->lock) );
342 TRACE("completed\n");
345 static HRESULT WINAPI
346 IDirectSoundCaptureImpl_QueryInterface(
347 LPDIRECTSOUNDCAPTURE iface,
351 IDirectSoundCaptureImpl *This = (IDirectSoundCaptureImpl *)iface;
352 TRACE( "(%p,%s,%p)\n", This, debugstr_guid(riid), ppobj );
355 WARN("invalid parameter\n");
363 hres = IDsCaptureDriver_QueryInterface(This->driver, riid, ppobj);
365 WARN("IDsCaptureDriver_QueryInterface failed\n");
369 WARN("unsupported riid: %s\n", debugstr_guid(riid));
374 IDirectSoundCaptureImpl_AddRef( LPDIRECTSOUNDCAPTURE iface )
376 IDirectSoundCaptureImpl *This = (IDirectSoundCaptureImpl *)iface;
377 TRACE("(%p) ref was %ld, thread is %04lx\n",This, This->ref, GetCurrentThreadId());
378 return InterlockedIncrement(&(This->ref));
382 IDirectSoundCaptureImpl_Release( LPDIRECTSOUNDCAPTURE iface )
385 IDirectSoundCaptureImpl *This = (IDirectSoundCaptureImpl *)iface;
386 TRACE("(%p) ref was %ld, thread is %04lx\n",This, This->ref, GetCurrentThreadId());
388 uRef = InterlockedDecrement(&(This->ref));
390 TRACE("deleting object\n");
391 if (This->capture_buffer)
392 IDirectSoundCaptureBufferImpl_Release(
393 (LPDIRECTSOUNDCAPTUREBUFFER8) This->capture_buffer);
396 IDsCaptureDriver_Close(This->driver);
397 IDsCaptureDriver_Release(This->driver);
401 HeapFree(GetProcessHeap(), 0, This->pwfx);
403 This->lock.DebugInfo->Spare[1] = 0;
404 DeleteCriticalSection( &(This->lock) );
405 HeapFree( GetProcessHeap(), 0, This );
406 dsound_capture = NULL;
407 TRACE("(%p) released\n",This);
413 static HRESULT WINAPI
414 IDirectSoundCaptureImpl_CreateCaptureBuffer(
415 LPDIRECTSOUNDCAPTURE iface,
416 LPCDSCBUFFERDESC lpcDSCBufferDesc,
417 LPDIRECTSOUNDCAPTUREBUFFER* lplpDSCaptureBuffer,
421 IDirectSoundCaptureImpl *This = (IDirectSoundCaptureImpl *)iface;
423 TRACE( "(%p,%p,%p,%p)\n",This,lpcDSCBufferDesc,lplpDSCaptureBuffer,pUnk );
426 WARN("invalid parameter: This == NULL\n");
427 return DSERR_INVALIDPARAM;
430 if (lpcDSCBufferDesc == NULL) {
431 WARN("invalid parameter: lpcDSCBufferDesc == NULL)\n");
432 return DSERR_INVALIDPARAM;
435 if (lplpDSCaptureBuffer == NULL) {
436 WARN("invalid parameter: lplpDSCaptureBuffer == NULL\n");
437 return DSERR_INVALIDPARAM;
441 WARN("invalid parameter: pUnk != NULL\n");
442 return DSERR_INVALIDPARAM;
445 /* FIXME: We can only have one buffer so what do we do here? */
446 if (This->capture_buffer) {
447 WARN("lnvalid parameter: already has buffer\n");
448 return DSERR_INVALIDPARAM; /* DSERR_GENERIC ? */
451 hr = DSOUND_CreateDirectSoundCaptureBuffer( This, lpcDSCBufferDesc,
452 (LPVOID*)lplpDSCaptureBuffer );
455 WARN("DSOUND_CreateDirectSoundCaptureBuffer failed\n");
460 static HRESULT WINAPI
461 IDirectSoundCaptureImpl_GetCaps(
462 LPDIRECTSOUNDCAPTURE iface,
463 LPDSCCAPS lpDSCCaps )
465 IDirectSoundCaptureImpl *This = (IDirectSoundCaptureImpl *)iface;
466 TRACE("(%p,%p)\n",This,lpDSCCaps);
468 if (lpDSCCaps== NULL) {
469 WARN("invalid parameter: lpDSCCaps== NULL\n");
470 return DSERR_INVALIDPARAM;
473 if (lpDSCCaps->dwSize < sizeof(*lpDSCCaps)) {
474 WARN("invalid parameter: lpDSCCaps->dwSize = %ld < %d\n",
475 lpDSCCaps->dwSize, sizeof(*lpDSCCaps));
476 return DSERR_INVALIDPARAM;
479 if ( !(This->initialized) ) {
480 WARN("not initialized\n");
481 return DSERR_UNINITIALIZED;
484 lpDSCCaps->dwFlags = This->drvcaps.dwFlags;
485 lpDSCCaps->dwFormats = This->drvcaps.dwFormats;
486 lpDSCCaps->dwChannels = This->drvcaps.dwChannels;
488 TRACE("(flags=0x%08lx,format=0x%08lx,channels=%ld)\n",lpDSCCaps->dwFlags,
489 lpDSCCaps->dwFormats, lpDSCCaps->dwChannels);
494 static HRESULT WINAPI
495 IDirectSoundCaptureImpl_Initialize(
496 LPDIRECTSOUNDCAPTURE iface,
499 HRESULT err = DSERR_INVALIDPARAM;
501 IDirectSoundCaptureImpl *This = (IDirectSoundCaptureImpl *)iface;
502 TRACE("(%p)\n", This);
505 WARN("invalid parameter: This == NULL\n");
506 return DSERR_INVALIDPARAM;
509 if (This->initialized) {
510 WARN("already initialized\n");
511 return DSERR_ALREADYINITIALIZED;
514 widn = waveInGetNumDevs();
517 WARN("no audio devices found\n");
518 return DSERR_NODRIVER;
521 /* Get dsound configuration */
522 setup_dsound_options();
524 /* enumerate WINMM audio devices and find the one we want */
525 for (wid=0; wid<widn; wid++) {
526 if (IsEqualGUID( lpcGUID, &capture_guids[wid]) ) {
533 WARN("invalid parameter\n");
534 return DSERR_INVALIDPARAM;
537 err = mmErr(waveInMessage((HWAVEIN)wid,DRV_QUERYDSOUNDIFACE,(DWORD)&(This->driver),0));
538 if ( (err != DS_OK) && (err != DSERR_UNSUPPORTED) ) {
539 WARN("waveInMessage failed; err=%lx\n",err);
544 /* Disable the direct sound driver to force emulation if requested. */
545 if (ds_hw_accel == DS_HW_ACCEL_EMULATION)
548 /* Get driver description */
550 TRACE("using DirectSound driver\n");
551 err = IDsCaptureDriver_GetDriverDesc(This->driver, &(This->drvdesc));
553 WARN("IDsCaptureDriver_GetDriverDesc failed\n");
557 TRACE("using WINMM\n");
558 /* if no DirectSound interface available, use WINMM API instead */
559 This->drvdesc.dwFlags = DSDDESC_DOMMSYSTEMOPEN |
560 DSDDESC_DOMMSYSTEMSETFORMAT;
563 This->drvdesc.dnDevNode = wid;
565 /* open the DirectSound driver if available */
566 if (This->driver && (err == DS_OK))
567 err = IDsCaptureDriver_Open(This->driver);
570 This->initialized = TRUE;
572 /* the driver is now open, so it's now allowed to call GetCaps */
574 This->drvcaps.dwSize = sizeof(This->drvcaps);
575 err = IDsCaptureDriver_GetCaps(This->driver,&(This->drvcaps));
577 WARN("IDsCaptureDriver_GetCaps failed\n");
580 } else /*if (This->hwi)*/ {
582 err = mmErr(waveInGetDevCapsA((UINT)This->drvdesc.dnDevNode, &wic, sizeof(wic)));
585 This->drvcaps.dwFlags = 0;
586 strncpy(This->drvdesc.szDrvName, wic.szPname,
587 sizeof(This->drvdesc.szDrvName));
589 This->drvcaps.dwFlags |= DSCCAPS_EMULDRIVER;
590 This->drvcaps.dwFormats = wic.dwFormats;
591 This->drvcaps.dwChannels = wic.wChannels;
599 static IDirectSoundCaptureVtbl dscvt =
601 /* IUnknown methods */
602 IDirectSoundCaptureImpl_QueryInterface,
603 IDirectSoundCaptureImpl_AddRef,
604 IDirectSoundCaptureImpl_Release,
606 /* IDirectSoundCapture methods */
607 IDirectSoundCaptureImpl_CreateCaptureBuffer,
608 IDirectSoundCaptureImpl_GetCaps,
609 IDirectSoundCaptureImpl_Initialize
613 DSOUND_CreateDirectSoundCaptureBuffer(
614 IDirectSoundCaptureImpl *ipDSC,
615 LPCDSCBUFFERDESC lpcDSCBufferDesc,
619 TRACE( "(%p,%p)\n", lpcDSCBufferDesc, ppobj );
622 WARN("invalid parameter: ipDSC == NULL\n");
623 return DSERR_INVALIDPARAM;
626 if (lpcDSCBufferDesc == NULL) {
627 WARN("invalid parameter: lpcDSCBufferDesc == NULL\n");
628 return DSERR_INVALIDPARAM;
632 WARN("invalid parameter: ppobj == NULL\n");
633 return DSERR_INVALIDPARAM;
636 if ( ((lpcDSCBufferDesc->dwSize != sizeof(DSCBUFFERDESC)) &&
637 (lpcDSCBufferDesc->dwSize != sizeof(DSCBUFFERDESC1))) ||
638 (lpcDSCBufferDesc->dwBufferBytes == 0) ||
639 (lpcDSCBufferDesc->lpwfxFormat == NULL) ) {
640 WARN("invalid lpcDSCBufferDesc\n");
642 return DSERR_INVALIDPARAM;
645 if ( !ipDSC->initialized ) {
646 WARN("not initialized\n");
648 return DSERR_UNINITIALIZED;
651 wfex = lpcDSCBufferDesc->lpwfxFormat;
654 TRACE("(formattag=0x%04x,chans=%d,samplerate=%ld,"
655 "bytespersec=%ld,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
656 wfex->wFormatTag, wfex->nChannels, wfex->nSamplesPerSec,
657 wfex->nAvgBytesPerSec, wfex->nBlockAlign,
658 wfex->wBitsPerSample, wfex->cbSize);
660 if (wfex->wFormatTag == WAVE_FORMAT_PCM) {
661 ipDSC->pwfx = HeapAlloc(GetProcessHeap(),0,sizeof(WAVEFORMATEX));
662 memcpy(ipDSC->pwfx, wfex, sizeof(WAVEFORMATEX));
663 ipDSC->pwfx->cbSize = 0;
665 ipDSC->pwfx = HeapAlloc(GetProcessHeap(),0,sizeof(WAVEFORMATEX)+wfex->cbSize);
666 memcpy(ipDSC->pwfx, wfex, sizeof(WAVEFORMATEX)+wfex->cbSize);
669 WARN("lpcDSCBufferDesc->lpwfxFormat == 0\n");
671 return DSERR_INVALIDPARAM; /* FIXME: DSERR_BADFORMAT ? */
674 *ppobj = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,
675 sizeof(IDirectSoundCaptureBufferImpl));
677 if ( *ppobj == NULL ) {
678 WARN("out of memory\n");
680 return DSERR_OUTOFMEMORY;
685 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)*ppobj;
688 This->dsound = ipDSC;
689 This->dsound->capture_buffer = This;
691 This->nrofnotifies = 0;
692 This->hwnotify = NULL;
694 This->pdscbd = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,
695 lpcDSCBufferDesc->dwSize);
697 memcpy(This->pdscbd, lpcDSCBufferDesc, lpcDSCBufferDesc->dwSize);
700 This->dsound->capture_buffer = 0;
701 HeapFree( GetProcessHeap(), 0, This );
703 return DSERR_OUTOFMEMORY;
706 This->lpVtbl = &dscbvt;
709 if (This->dsound->drvdesc.dwFlags & DSDDESC_DOMMSYSTEMOPEN)
710 FIXME("DSDDESC_DOMMSYSTEMOPEN not supported\n");
712 if (This->dsound->drvdesc.dwFlags & DSDDESC_USESYSTEMMEMORY) {
713 /* allocate buffer from system memory */
714 buflen = lpcDSCBufferDesc->dwBufferBytes;
715 TRACE("desired buflen=%ld, old buffer=%p\n", buflen, ipDSC->buffer);
717 newbuf = HeapReAlloc(GetProcessHeap(),0,ipDSC->buffer,buflen);
719 newbuf = HeapAlloc(GetProcessHeap(),0,buflen);
721 if (newbuf == NULL) {
722 WARN("failed to allocate capture buffer\n");
723 err = DSERR_OUTOFMEMORY;
724 /* but the old buffer might still exist and must be re-prepared */
726 ipDSC->buffer = newbuf;
727 ipDSC->buflen = buflen;
730 /* let driver allocate memory */
731 ipDSC->buflen = lpcDSCBufferDesc->dwBufferBytes;
734 HeapFree( GetProcessHeap(), 0, ipDSC->buffer);
735 ipDSC->buffer = NULL;
738 err = IDsCaptureDriver_CreateCaptureBuffer(ipDSC->driver,
739 ipDSC->pwfx,0,0,&(ipDSC->buflen),&(ipDSC->buffer),(LPVOID*)&(ipDSC->hwbuf));
741 WARN("IDsCaptureDriver_CreateCaptureBuffer failed\n");
742 This->dsound->capture_buffer = 0;
743 HeapFree( GetProcessHeap(), 0, This );
748 DWORD flags = CALLBACK_FUNCTION;
749 if (ds_hw_accel != DS_HW_ACCEL_EMULATION)
750 flags |= WAVE_DIRECTSOUND;
751 err = mmErr(waveInOpen(&(ipDSC->hwi),
752 ipDSC->drvdesc.dnDevNode, ipDSC->pwfx,
753 (DWORD)DSOUND_capture_callback, (DWORD)ipDSC, flags));
755 WARN("waveInOpen failed\n");
756 This->dsound->capture_buffer = 0;
757 HeapFree( GetProcessHeap(), 0, This );
762 buflen = lpcDSCBufferDesc->dwBufferBytes;
763 TRACE("desired buflen=%ld, old buffer=%p\n", buflen, ipDSC->buffer);
765 newbuf = HeapReAlloc(GetProcessHeap(),0,ipDSC->buffer,buflen);
767 newbuf = HeapAlloc(GetProcessHeap(),0,buflen);
768 if (newbuf == NULL) {
769 WARN("failed to allocate capture buffer\n");
770 err = DSERR_OUTOFMEMORY;
771 /* but the old buffer might still exist and must be re-prepared */
773 ipDSC->buffer = newbuf;
774 ipDSC->buflen = buflen;
779 TRACE("returning DS_OK\n");
783 /*******************************************************************************
784 * IDirectSoundCaptureNotify
786 static HRESULT WINAPI IDirectSoundCaptureNotifyImpl_QueryInterface(
787 LPDIRECTSOUNDNOTIFY iface,
791 IDirectSoundCaptureNotifyImpl *This = (IDirectSoundCaptureNotifyImpl *)iface;
792 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
794 if (This->dscb == NULL) {
795 WARN("invalid parameter\n");
799 return IDirectSoundCaptureBuffer_QueryInterface((LPDIRECTSOUNDCAPTUREBUFFER)This->dscb, riid, ppobj);
802 static ULONG WINAPI IDirectSoundCaptureNotifyImpl_AddRef(LPDIRECTSOUNDNOTIFY iface)
804 IDirectSoundCaptureNotifyImpl *This = (IDirectSoundCaptureNotifyImpl *)iface;
805 TRACE("(%p) ref was %ld, thread is %04lx\n",This, This->ref, GetCurrentThreadId());
806 return InterlockedIncrement(&(This->ref));
809 static ULONG WINAPI IDirectSoundCaptureNotifyImpl_Release(LPDIRECTSOUNDNOTIFY iface)
811 IDirectSoundCaptureNotifyImpl *This = (IDirectSoundCaptureNotifyImpl *)iface;
814 TRACE("(%p) ref was %ld, thread is %04lx\n",This, This->ref, GetCurrentThreadId());
816 ref = InterlockedDecrement(&(This->ref));
818 if (This->dscb->hwnotify)
819 IDsDriverNotify_Release(This->dscb->hwnotify);
820 This->dscb->notify=NULL;
821 IDirectSoundCaptureBuffer_Release((LPDIRECTSOUNDCAPTUREBUFFER)This->dscb);
822 HeapFree(GetProcessHeap(),0,This);
823 TRACE("(%p) released\n",This);
828 static HRESULT WINAPI IDirectSoundCaptureNotifyImpl_SetNotificationPositions(
829 LPDIRECTSOUNDNOTIFY iface,
831 LPCDSBPOSITIONNOTIFY notify)
833 IDirectSoundCaptureNotifyImpl *This = (IDirectSoundCaptureNotifyImpl *)iface;
834 TRACE("(%p,0x%08lx,%p)\n",This,howmuch,notify);
836 if (howmuch > 0 && notify == NULL) {
837 WARN("invalid parameter: notify == NULL\n");
838 return DSERR_INVALIDPARAM;
841 if (TRACE_ON(dsound)) {
843 for (i=0;i<howmuch;i++)
844 TRACE("notify at %ld to 0x%08lx\n",
845 notify[i].dwOffset,(DWORD)notify[i].hEventNotify);
848 if (This->dscb->hwnotify) {
850 hres = IDsDriverNotify_SetNotificationPositions(This->dscb->hwnotify, howmuch, notify);
852 WARN("IDsDriverNotify_SetNotificationPositions failed\n");
854 } else if (howmuch > 0) {
855 /* Make an internal copy of the caller-supplied array.
856 * Replace the existing copy if one is already present. */
857 if (This->dscb->notifies)
858 This->dscb->notifies = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
859 This->dscb->notifies, howmuch * sizeof(DSBPOSITIONNOTIFY));
861 This->dscb->notifies = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
862 howmuch * sizeof(DSBPOSITIONNOTIFY));
864 if (This->dscb->notifies == NULL) {
865 WARN("out of memory\n");
866 return DSERR_OUTOFMEMORY;
868 memcpy(This->dscb->notifies, notify, howmuch * sizeof(DSBPOSITIONNOTIFY));
869 This->dscb->nrofnotifies = howmuch;
871 if (This->dscb->notifies) {
872 HeapFree(GetProcessHeap(), 0, This->dscb->notifies);
873 This->dscb->notifies = NULL;
875 This->dscb->nrofnotifies = 0;
881 IDirectSoundNotifyVtbl dscnvt =
883 IDirectSoundCaptureNotifyImpl_QueryInterface,
884 IDirectSoundCaptureNotifyImpl_AddRef,
885 IDirectSoundCaptureNotifyImpl_Release,
886 IDirectSoundCaptureNotifyImpl_SetNotificationPositions,
889 HRESULT WINAPI IDirectSoundCaptureNotifyImpl_Create(
890 IDirectSoundCaptureBufferImpl *dscb,
891 IDirectSoundCaptureNotifyImpl **pdscn)
893 IDirectSoundCaptureNotifyImpl * dscn;
894 TRACE("(%p,%p)\n",dscb,pdscn);
896 dscn = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(dscn));
899 WARN("out of memory\n");
900 return DSERR_OUTOFMEMORY;
904 dscn->lpVtbl = &dscnvt;
907 IDirectSoundCaptureBuffer_AddRef((LPDIRECTSOUNDCAPTUREBUFFER)dscb);
913 /*******************************************************************************
914 * IDirectSoundCaptureBuffer
916 static HRESULT WINAPI
917 IDirectSoundCaptureBufferImpl_QueryInterface(
918 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
922 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
924 TRACE( "(%p,%s,%p)\n", This, debugstr_guid(riid), ppobj );
927 WARN("invalid parameter\n");
933 if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ) {
935 hres = IDirectSoundCaptureNotifyImpl_Create(This, &This->notify);
937 if (This->dsound->hwbuf) {
938 hres = IDsCaptureDriverBuffer_QueryInterface(This->dsound->hwbuf,
939 &IID_IDsDriverNotify, (LPVOID*)&(This->hwnotify));
941 WARN("IDsCaptureDriverBuffer_QueryInterface failed\n");
947 IDirectSoundNotify_AddRef((LPDIRECTSOUNDNOTIFY)This->notify);
948 *ppobj = (LPVOID)This->notify;
952 WARN("IID_IDirectSoundNotify\n");
956 if ( IsEqualGUID( &IID_IDirectSoundCaptureBuffer, riid ) ||
957 IsEqualGUID( &IID_IDirectSoundCaptureBuffer8, riid ) ) {
958 IDirectSoundCaptureBuffer8_AddRef(iface);
963 FIXME("(%p,%s,%p) unsupported GUID\n", This, debugstr_guid(riid), ppobj);
964 return E_NOINTERFACE;
968 IDirectSoundCaptureBufferImpl_AddRef( LPDIRECTSOUNDCAPTUREBUFFER8 iface )
970 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
971 TRACE("(%p) ref was %ld, thread is %04lx\n",This, This->ref, GetCurrentThreadId());
972 return InterlockedIncrement(&(This->ref));
976 IDirectSoundCaptureBufferImpl_Release( LPDIRECTSOUNDCAPTUREBUFFER8 iface )
979 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
980 TRACE("(%p) ref was %ld, thread is %04lx\n",This, This->ref, GetCurrentThreadId());
982 uRef = InterlockedDecrement(&(This->ref));
984 TRACE("deleting object\n");
985 if (This->dsound->state == STATE_CAPTURING)
986 This->dsound->state = STATE_STOPPING;
989 HeapFree(GetProcessHeap(),0, This->pdscbd);
991 if (This->dsound->hwi) {
992 waveInReset(This->dsound->hwi);
993 waveInClose(This->dsound->hwi);
994 if (This->dsound->pwave) {
995 HeapFree(GetProcessHeap(),0, This->dsound->pwave);
996 This->dsound->pwave = 0;
998 This->dsound->hwi = 0;
1001 if (This->dsound->hwbuf)
1002 IDsCaptureDriverBuffer_Release(This->dsound->hwbuf);
1004 /* remove from IDirectSoundCaptureImpl */
1006 This->dsound->capture_buffer = NULL;
1008 ERR("does not reference dsound\n");
1011 IDirectSoundNotify_Release((LPDIRECTSOUNDNOTIFY)This->notify);
1013 if (This->notifies != NULL)
1014 HeapFree(GetProcessHeap(), 0, This->notifies);
1016 HeapFree( GetProcessHeap(), 0, This );
1017 TRACE("(%p) released\n",This);
1023 static HRESULT WINAPI
1024 IDirectSoundCaptureBufferImpl_GetCaps(
1025 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
1026 LPDSCBCAPS lpDSCBCaps )
1028 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
1029 TRACE( "(%p,%p)\n", This, lpDSCBCaps );
1032 WARN("invalid parameter: This == NULL\n");
1033 return DSERR_INVALIDPARAM;
1036 if (lpDSCBCaps == NULL) {
1037 WARN("invalid parameter: lpDSCBCaps == NULL\n");
1038 return DSERR_INVALIDPARAM;
1041 if (lpDSCBCaps->dwSize < sizeof(DSCBCAPS)) {
1042 WARN("invalid parameter: lpDSCBCaps->dwSize = %ld < %d\n",
1043 lpDSCBCaps->dwSize, sizeof(DSCBCAPS));
1044 return DSERR_INVALIDPARAM;
1047 if (This->dsound == NULL) {
1048 WARN("invalid parameter: This->dsound == NULL\n");
1049 return DSERR_INVALIDPARAM;
1052 lpDSCBCaps->dwSize = sizeof(DSCBCAPS);
1053 lpDSCBCaps->dwFlags = This->flags;
1054 lpDSCBCaps->dwBufferBytes = This->pdscbd->dwBufferBytes;
1055 lpDSCBCaps->dwReserved = 0;
1057 TRACE("returning DS_OK\n");
1061 static HRESULT WINAPI
1062 IDirectSoundCaptureBufferImpl_GetCurrentPosition(
1063 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
1064 LPDWORD lpdwCapturePosition,
1065 LPDWORD lpdwReadPosition )
1067 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
1068 HRESULT hres = DS_OK;
1069 TRACE( "(%p,%p,%p)\n", This, lpdwCapturePosition, lpdwReadPosition );
1072 WARN("invalid parameter: This == NULL\n");
1073 return DSERR_INVALIDPARAM;
1076 if (This->dsound == NULL) {
1077 WARN("invalid parameter: This->dsound == NULL\n");
1078 return DSERR_INVALIDPARAM;
1081 if (This->dsound->driver) {
1082 hres = IDsCaptureDriverBuffer_GetPosition(This->dsound->hwbuf, lpdwCapturePosition, lpdwReadPosition );
1084 WARN("IDsCaptureDriverBuffer_GetPosition failed\n");
1085 } else if (This->dsound->hwi) {
1086 EnterCriticalSection(&(This->dsound->lock));
1087 TRACE("old This->dsound->state=%s\n",captureStateString[This->dsound->state]);
1088 if (lpdwCapturePosition) {
1090 mtime.wType = TIME_BYTES;
1091 waveInGetPosition(This->dsound->hwi, &mtime, sizeof(mtime));
1092 TRACE("mtime.u.cb=%ld,This->dsound->buflen=%ld\n", mtime.u.cb,
1093 This->dsound->buflen);
1094 mtime.u.cb = mtime.u.cb % This->dsound->buflen;
1095 *lpdwCapturePosition = mtime.u.cb;
1098 if (lpdwReadPosition) {
1099 if (This->dsound->state == STATE_STARTING) {
1100 if (lpdwCapturePosition)
1101 This->dsound->read_position = *lpdwCapturePosition;
1102 This->dsound->state = STATE_CAPTURING;
1104 *lpdwReadPosition = This->dsound->read_position;
1106 TRACE("new This->dsound->state=%s\n",captureStateString[This->dsound->state]);
1107 LeaveCriticalSection(&(This->dsound->lock));
1108 if (lpdwCapturePosition) TRACE("*lpdwCapturePosition=%ld\n",*lpdwCapturePosition);
1109 if (lpdwReadPosition) TRACE("*lpdwReadPosition=%ld\n",*lpdwReadPosition);
1111 WARN("no driver\n");
1112 hres = DSERR_NODRIVER;
1115 TRACE("returning %08lx\n", hres);
1119 static HRESULT WINAPI
1120 IDirectSoundCaptureBufferImpl_GetFormat(
1121 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
1122 LPWAVEFORMATEX lpwfxFormat,
1123 DWORD dwSizeAllocated,
1124 LPDWORD lpdwSizeWritten )
1126 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
1127 HRESULT hres = DS_OK;
1128 TRACE( "(%p,%p,0x%08lx,%p)\n", This, lpwfxFormat, dwSizeAllocated,
1132 WARN("invalid parameter: This == NULL\n");
1133 return DSERR_INVALIDPARAM;
1136 if (This->dsound == NULL) {
1137 WARN("invalid parameter: This->dsound == NULL\n");
1138 return DSERR_INVALIDPARAM;
1141 if (dwSizeAllocated > (sizeof(WAVEFORMATEX) + This->dsound->pwfx->cbSize))
1142 dwSizeAllocated = sizeof(WAVEFORMATEX) + This->dsound->pwfx->cbSize;
1144 if (lpwfxFormat) { /* NULL is valid (just want size) */
1145 memcpy(lpwfxFormat, This->dsound->pwfx, dwSizeAllocated);
1146 if (lpdwSizeWritten)
1147 *lpdwSizeWritten = dwSizeAllocated;
1149 if (lpdwSizeWritten)
1150 *lpdwSizeWritten = sizeof(WAVEFORMATEX) + This->dsound->pwfx->cbSize;
1152 TRACE("invalid parameter: lpdwSizeWritten = NULL\n");
1153 hres = DSERR_INVALIDPARAM;
1157 TRACE("returning %08lx\n", hres);
1161 static HRESULT WINAPI
1162 IDirectSoundCaptureBufferImpl_GetStatus(
1163 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
1164 LPDWORD lpdwStatus )
1166 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
1167 TRACE( "(%p, %p), thread is %04lx\n", This, lpdwStatus, GetCurrentThreadId() );
1170 WARN("invalid parameter: This == NULL\n");
1171 return DSERR_INVALIDPARAM;
1174 if (This->dsound == NULL) {
1175 WARN("invalid parameter: This->dsound == NULL\n");
1176 return DSERR_INVALIDPARAM;
1179 if (lpdwStatus == NULL) {
1180 WARN("invalid parameter: lpdwStatus == NULL\n");
1181 return DSERR_INVALIDPARAM;
1185 EnterCriticalSection(&(This->dsound->lock));
1187 TRACE("old This->dsound->state=%s, old lpdwStatus=%08lx\n",
1188 captureStateString[This->dsound->state],*lpdwStatus);
1189 if ((This->dsound->state == STATE_STARTING) ||
1190 (This->dsound->state == STATE_CAPTURING)) {
1191 *lpdwStatus |= DSCBSTATUS_CAPTURING;
1192 if (This->flags & DSCBSTART_LOOPING)
1193 *lpdwStatus |= DSCBSTATUS_LOOPING;
1195 TRACE("new This->dsound->state=%s, new lpdwStatus=%08lx\n",
1196 captureStateString[This->dsound->state],*lpdwStatus);
1197 LeaveCriticalSection(&(This->dsound->lock));
1199 TRACE("status=%lx\n", *lpdwStatus);
1200 TRACE("returning DS_OK\n");
1204 static HRESULT WINAPI
1205 IDirectSoundCaptureBufferImpl_Initialize(
1206 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
1207 LPDIRECTSOUNDCAPTURE lpDSC,
1208 LPCDSCBUFFERDESC lpcDSCBDesc )
1210 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
1212 FIXME( "(%p,%p,%p): stub\n", This, lpDSC, lpcDSCBDesc );
1217 static HRESULT WINAPI
1218 IDirectSoundCaptureBufferImpl_Lock(
1219 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
1222 LPVOID* lplpvAudioPtr1,
1223 LPDWORD lpdwAudioBytes1,
1224 LPVOID* lplpvAudioPtr2,
1225 LPDWORD lpdwAudioBytes2,
1228 HRESULT hres = DS_OK;
1229 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
1230 TRACE( "(%p,%08lu,%08lu,%p,%p,%p,%p,0x%08lx) at %ld\n", This, dwReadCusor,
1231 dwReadBytes, lplpvAudioPtr1, lpdwAudioBytes1, lplpvAudioPtr2,
1232 lpdwAudioBytes2, dwFlags, GetTickCount() );
1235 WARN("invalid parameter: This == NULL\n");
1236 return DSERR_INVALIDPARAM;
1239 if (This->dsound == NULL) {
1240 WARN("invalid parameter: This->dsound == NULL\n");
1241 return DSERR_INVALIDPARAM;
1244 if (lplpvAudioPtr1 == NULL) {
1245 WARN("invalid parameter: lplpvAudioPtr1 == NULL\n");
1246 return DSERR_INVALIDPARAM;
1249 if (lpdwAudioBytes1 == NULL) {
1250 WARN("invalid parameter: lpdwAudioBytes1 == NULL\n");
1251 return DSERR_INVALIDPARAM;
1254 EnterCriticalSection(&(This->dsound->lock));
1256 if (This->dsound->driver) {
1257 hres = IDsCaptureDriverBuffer_Lock(This->dsound->hwbuf, lplpvAudioPtr1,
1258 lpdwAudioBytes1, lplpvAudioPtr2,
1259 lpdwAudioBytes2, dwReadCusor,
1260 dwReadBytes, dwFlags);
1262 WARN("IDsCaptureDriverBuffer_Lock failed\n");
1263 } else if (This->dsound->hwi) {
1264 *lplpvAudioPtr1 = This->dsound->buffer + dwReadCusor;
1265 if ( (dwReadCusor + dwReadBytes) > This->dsound->buflen) {
1266 *lpdwAudioBytes1 = This->dsound->buflen - dwReadCusor;
1268 *lplpvAudioPtr2 = This->dsound->buffer;
1269 if (lpdwAudioBytes2)
1270 *lpdwAudioBytes2 = dwReadBytes - *lpdwAudioBytes1;
1272 *lpdwAudioBytes1 = dwReadBytes;
1274 *lplpvAudioPtr2 = 0;
1275 if (lpdwAudioBytes2)
1276 *lpdwAudioBytes2 = 0;
1279 TRACE("invalid call\n");
1280 hres = DSERR_INVALIDCALL; /* DSERR_NODRIVER ? */
1283 LeaveCriticalSection(&(This->dsound->lock));
1285 TRACE("returning %08lx\n", hres);
1289 static HRESULT WINAPI
1290 IDirectSoundCaptureBufferImpl_Start(
1291 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
1294 HRESULT hres = DS_OK;
1295 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
1296 TRACE( "(%p,0x%08lx)\n", This, dwFlags );
1299 WARN("invalid parameter: This == NULL\n");
1300 return DSERR_INVALIDPARAM;
1303 if (This->dsound == NULL) {
1304 WARN("invalid parameter: This->dsound == NULL\n");
1305 return DSERR_INVALIDPARAM;
1308 if ( (This->dsound->driver == 0) && (This->dsound->hwi == 0) ) {
1309 WARN("no driver\n");
1310 return DSERR_NODRIVER;
1313 EnterCriticalSection(&(This->dsound->lock));
1315 This->flags = dwFlags;
1316 TRACE("old This->dsound->state=%s\n",captureStateString[This->dsound->state]);
1317 if (This->dsound->state == STATE_STOPPED)
1318 This->dsound->state = STATE_STARTING;
1319 else if (This->dsound->state == STATE_STOPPING)
1320 This->dsound->state = STATE_CAPTURING;
1321 TRACE("new This->dsound->state=%s\n",captureStateString[This->dsound->state]);
1323 LeaveCriticalSection(&(This->dsound->lock));
1325 if (This->dsound->driver) {
1326 hres = IDsCaptureDriverBuffer_Start(This->dsound->hwbuf, dwFlags);
1328 WARN("IDsCaptureDriverBuffer_Start failed\n");
1329 } else if (This->dsound->hwi) {
1330 IDirectSoundCaptureImpl* ipDSC = This->dsound;
1332 if (ipDSC->buffer) {
1333 if (This->nrofnotifies) {
1336 ipDSC->nrofpwaves = This->nrofnotifies;
1337 TRACE("nrofnotifies=%d\n", This->nrofnotifies);
1339 /* prepare headers */
1341 ipDSC->pwave = HeapReAlloc(GetProcessHeap(),0,ipDSC->pwave,
1342 ipDSC->nrofpwaves*sizeof(WAVEHDR));
1344 ipDSC->pwave = HeapAlloc(GetProcessHeap(),0,
1345 ipDSC->nrofpwaves*sizeof(WAVEHDR));
1347 for (c = 0; c < ipDSC->nrofpwaves; c++) {
1348 if (This->notifies[c].dwOffset == DSBPN_OFFSETSTOP) {
1349 TRACE("got DSBPN_OFFSETSTOP\n");
1350 ipDSC->nrofpwaves = c;
1354 ipDSC->pwave[0].lpData = ipDSC->buffer;
1355 ipDSC->pwave[0].dwBufferLength =
1356 This->notifies[0].dwOffset + 1;
1358 ipDSC->pwave[c].lpData = ipDSC->buffer +
1359 This->notifies[c-1].dwOffset + 1;
1360 ipDSC->pwave[c].dwBufferLength =
1361 This->notifies[c].dwOffset -
1362 This->notifies[c-1].dwOffset;
1364 ipDSC->pwave[c].dwBytesRecorded = 0;
1365 ipDSC->pwave[c].dwUser = (DWORD)ipDSC;
1366 ipDSC->pwave[c].dwFlags = 0;
1367 ipDSC->pwave[c].dwLoops = 0;
1368 hres = mmErr(waveInPrepareHeader(ipDSC->hwi,
1369 &(ipDSC->pwave[c]),sizeof(WAVEHDR)));
1370 if (hres != DS_OK) {
1371 WARN("waveInPrepareHeader failed\n");
1373 waveInUnprepareHeader(ipDSC->hwi,
1374 &(ipDSC->pwave[c]),sizeof(WAVEHDR));
1378 hres = mmErr(waveInAddBuffer(ipDSC->hwi,
1379 &(ipDSC->pwave[c]), sizeof(WAVEHDR)));
1380 if (hres != DS_OK) {
1381 WARN("waveInAddBuffer failed\n");
1383 waveInUnprepareHeader(ipDSC->hwi,
1384 &(ipDSC->pwave[c]),sizeof(WAVEHDR));
1389 memset(ipDSC->buffer,
1390 (ipDSC->pwfx->wBitsPerSample == 8) ? 128 : 0, ipDSC->buflen);
1392 TRACE("no notifiers specified\n");
1393 /* no notifiers specified so just create a single default header */
1394 ipDSC->nrofpwaves = 1;
1396 ipDSC->pwave = HeapReAlloc(GetProcessHeap(),0,ipDSC->pwave,sizeof(WAVEHDR));
1398 ipDSC->pwave = HeapAlloc(GetProcessHeap(),0,sizeof(WAVEHDR));
1400 ipDSC->pwave[0].lpData = ipDSC->buffer;
1401 ipDSC->pwave[0].dwBufferLength = ipDSC->buflen;
1402 ipDSC->pwave[0].dwBytesRecorded = 0;
1403 ipDSC->pwave[0].dwUser = (DWORD)ipDSC;
1404 ipDSC->pwave[0].dwFlags = 0;
1405 ipDSC->pwave[0].dwLoops = 0;
1407 hres = mmErr(waveInPrepareHeader(ipDSC->hwi,
1408 &(ipDSC->pwave[0]),sizeof(WAVEHDR)));
1409 if (hres != DS_OK) {
1410 WARN("waveInPrepareHeader failed\n");
1411 waveInUnprepareHeader(ipDSC->hwi,
1412 &(ipDSC->pwave[0]),sizeof(WAVEHDR));
1414 hres = mmErr(waveInAddBuffer(ipDSC->hwi,
1415 &(ipDSC->pwave[0]), sizeof(WAVEHDR)));
1416 if (hres != DS_OK) {
1417 WARN("waveInAddBuffer failed\n");
1418 waveInUnprepareHeader(ipDSC->hwi,
1419 &(ipDSC->pwave[0]),sizeof(WAVEHDR));
1425 ipDSC->read_position = 0;
1427 if (hres == DS_OK) {
1428 /* start filling the first buffer */
1429 hres = mmErr(waveInStart(ipDSC->hwi));
1431 WARN("waveInStart failed\n");
1434 if (hres != DS_OK) {
1435 WARN("calling waveInClose because of error\n");
1436 waveInClose(This->dsound->hwi);
1437 This->dsound->hwi = 0;
1440 WARN("no driver\n");
1441 hres = DSERR_NODRIVER;
1444 TRACE("returning %08lx\n", hres);
1448 static HRESULT WINAPI
1449 IDirectSoundCaptureBufferImpl_Stop( LPDIRECTSOUNDCAPTUREBUFFER8 iface )
1451 HRESULT hres = DS_OK;
1452 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
1453 TRACE( "(%p)\n", This );
1456 WARN("invalid parameter: This == NULL\n");
1457 return DSERR_INVALIDPARAM;
1460 if (This->dsound == NULL) {
1461 WARN("invalid parameter: This->dsound == NULL\n");
1462 return DSERR_INVALIDPARAM;
1465 EnterCriticalSection(&(This->dsound->lock));
1467 TRACE("old This->dsound->state=%s\n",captureStateString[This->dsound->state]);
1468 if (This->dsound->state == STATE_CAPTURING)
1469 This->dsound->state = STATE_STOPPING;
1470 else if (This->dsound->state == STATE_STARTING)
1471 This->dsound->state = STATE_STOPPED;
1472 TRACE("new This->dsound->state=%s\n",captureStateString[This->dsound->state]);
1474 LeaveCriticalSection(&(This->dsound->lock));
1476 if (This->dsound->driver) {
1477 hres = IDsCaptureDriverBuffer_Stop(This->dsound->hwbuf);
1479 WARN("IDsCaptureDriverBuffer_Stop() failed\n");
1480 } else if (This->dsound->hwi) {
1481 hres = mmErr(waveInReset(This->dsound->hwi));
1483 WARN("waveInReset() failed\n");
1485 WARN("no driver\n");
1486 hres = DSERR_NODRIVER;
1489 TRACE("returning %08lx\n", hres);
1493 static HRESULT WINAPI
1494 IDirectSoundCaptureBufferImpl_Unlock(
1495 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
1496 LPVOID lpvAudioPtr1,
1497 DWORD dwAudioBytes1,
1498 LPVOID lpvAudioPtr2,
1499 DWORD dwAudioBytes2 )
1501 HRESULT hres = DS_OK;
1502 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
1503 TRACE( "(%p,%p,%08lu,%p,%08lu)\n", This, lpvAudioPtr1, dwAudioBytes1,
1504 lpvAudioPtr2, dwAudioBytes2 );
1507 WARN("invalid parameter: This == NULL\n");
1508 return DSERR_INVALIDPARAM;
1511 if (lpvAudioPtr1 == NULL) {
1512 WARN("invalid parameter: lpvAudioPtr1 == NULL\n");
1513 return DSERR_INVALIDPARAM;
1516 if (This->dsound->driver) {
1517 hres = IDsCaptureDriverBuffer_Unlock(This->dsound->hwbuf, lpvAudioPtr1,
1518 dwAudioBytes1, lpvAudioPtr2, dwAudioBytes2);
1520 WARN("IDsCaptureDriverBuffer_Unlock failed\n");
1521 } else if (This->dsound->hwi) {
1522 This->dsound->read_position = (This->dsound->read_position +
1523 (dwAudioBytes1 + dwAudioBytes2)) % This->dsound->buflen;
1525 WARN("invalid call\n");
1526 hres = DSERR_INVALIDCALL;
1529 TRACE("returning %08lx\n", hres);
1533 static HRESULT WINAPI
1534 IDirectSoundCaptureBufferImpl_GetObjectInPath(
1535 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
1536 REFGUID rguidObject,
1538 REFGUID rguidInterface,
1541 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
1543 FIXME( "(%p,%s,%lu,%s,%p): stub\n", This, debugstr_guid(rguidObject),
1544 dwIndex, debugstr_guid(rguidInterface), ppObject );
1549 static HRESULT WINAPI
1550 IDirectSoundCaptureBufferImpl_GetFXStatus(
1551 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
1553 LPDWORD pdwFXStatus )
1555 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
1557 FIXME( "(%p,%lu,%p): stub\n", This, dwFXCount, pdwFXStatus );
1562 static IDirectSoundCaptureBuffer8Vtbl dscbvt =
1564 /* IUnknown methods */
1565 IDirectSoundCaptureBufferImpl_QueryInterface,
1566 IDirectSoundCaptureBufferImpl_AddRef,
1567 IDirectSoundCaptureBufferImpl_Release,
1569 /* IDirectSoundCaptureBuffer methods */
1570 IDirectSoundCaptureBufferImpl_GetCaps,
1571 IDirectSoundCaptureBufferImpl_GetCurrentPosition,
1572 IDirectSoundCaptureBufferImpl_GetFormat,
1573 IDirectSoundCaptureBufferImpl_GetStatus,
1574 IDirectSoundCaptureBufferImpl_Initialize,
1575 IDirectSoundCaptureBufferImpl_Lock,
1576 IDirectSoundCaptureBufferImpl_Start,
1577 IDirectSoundCaptureBufferImpl_Stop,
1578 IDirectSoundCaptureBufferImpl_Unlock,
1580 /* IDirectSoundCaptureBuffer methods */
1581 IDirectSoundCaptureBufferImpl_GetObjectInPath,
1582 IDirectSoundCaptureBufferImpl_GetFXStatus
1585 /*******************************************************************************
1586 * DirectSoundCapture ClassFactory
1589 static HRESULT WINAPI
1590 DSCCF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj)
1592 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
1594 FIXME("(%p)->(%s,%p),stub!\n",This,debugstr_guid(riid),ppobj);
1595 return E_NOINTERFACE;
1599 DSCCF_AddRef(LPCLASSFACTORY iface)
1601 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
1602 TRACE("(%p) ref was %ld\n", This, This->ref);
1603 return InterlockedIncrement(&(This->ref));
1607 DSCCF_Release(LPCLASSFACTORY iface)
1609 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
1610 /* static class, won't be freed */
1611 TRACE("(%p) ref was %ld\n", This, This->ref);
1612 return InterlockedDecrement(&(This->ref));
1615 static HRESULT WINAPI
1616 DSCCF_CreateInstance(
1617 LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj )
1619 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
1620 TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
1623 WARN("aggregation not supported\n");
1624 return CLASS_E_NOAGGREGATION;
1627 if (ppobj == NULL) {
1628 WARN("invalid parameter\n");
1629 return E_INVALIDARG;
1634 if ( IsEqualGUID( &IID_IDirectSoundCapture8, riid ) )
1635 return DirectSoundCaptureCreate8(0,(LPDIRECTSOUNDCAPTURE8*)ppobj,pOuter);
1637 WARN("(%p,%p,%s,%p) Interface not found!\n",This,pOuter,debugstr_guid(riid),ppobj);
1638 return E_NOINTERFACE;
1641 static HRESULT WINAPI
1642 DSCCF_LockServer(LPCLASSFACTORY iface,BOOL dolock)
1644 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
1645 FIXME("(%p)->(%d),stub!\n",This,dolock);
1649 static IClassFactoryVtbl DSCCF_Vtbl =
1651 DSCCF_QueryInterface,
1654 DSCCF_CreateInstance,
1658 IClassFactoryImpl DSOUND_CAPTURE_CF = { &DSCCF_Vtbl, 1 };
1660 /***************************************************************************
1661 * DirectSoundFullDuplexCreate [DSOUND.10]
1663 * Create and initialize a DirectSoundFullDuplex interface.
1666 * pcGuidCaptureDevice [I] Address of sound capture device GUID.
1667 * pcGuidRenderDevice [I] Address of sound render device GUID.
1668 * pcDSCBufferDesc [I] Address of capture buffer description.
1669 * pcDSBufferDesc [I] Address of render buffer description.
1670 * hWnd [I] Handle to application window.
1671 * dwLevel [I] Cooperative level.
1672 * ppDSFD [O] Address where full duplex interface returned.
1673 * ppDSCBuffer8 [0] Address where capture buffer interface returned.
1674 * ppDSBuffer8 [0] Address where render buffer interface returned.
1675 * pUnkOuter [I] Must be NULL.
1679 * Failure: DSERR_NOAGGREGATION, DSERR_ALLOCATED, DSERR_INVALIDPARAM,
1680 * DSERR_OUTOFMEMORY DSERR_INVALIDCALL DSERR_NODRIVER
1683 DirectSoundFullDuplexCreate(
1684 LPCGUID pcGuidCaptureDevice,
1685 LPCGUID pcGuidRenderDevice,
1686 LPCDSCBUFFERDESC pcDSCBufferDesc,
1687 LPCDSBUFFERDESC pcDSBufferDesc,
1690 LPDIRECTSOUNDFULLDUPLEX *ppDSFD,
1691 LPDIRECTSOUNDCAPTUREBUFFER8 *ppDSCBuffer8,
1692 LPDIRECTSOUNDBUFFER8 *ppDSBuffer8,
1693 LPUNKNOWN pUnkOuter)
1695 IDirectSoundFullDuplexImpl** ippDSFD=(IDirectSoundFullDuplexImpl**)ppDSFD;
1696 TRACE("(%s,%s,%p,%p,%lx,%lx,%p,%p,%p,%p)\n", debugstr_guid(pcGuidCaptureDevice),
1697 debugstr_guid(pcGuidRenderDevice), pcDSCBufferDesc, pcDSBufferDesc,
1698 (DWORD)hWnd, dwLevel, ppDSFD, ppDSCBuffer8, ppDSBuffer8, pUnkOuter);
1701 WARN("pUnkOuter != 0\n");
1702 return DSERR_NOAGGREGATION;
1705 *ippDSFD = HeapAlloc(GetProcessHeap(),
1706 HEAP_ZERO_MEMORY, sizeof(IDirectSoundFullDuplexImpl));
1708 if (*ippDSFD == NULL) {
1709 WARN("out of memory\n");
1710 return DSERR_OUTOFMEMORY;
1713 IDirectSoundFullDuplexImpl *This = (IDirectSoundFullDuplexImpl *)*ippDSFD;
1716 This->lpVtbl = &dsfdvt;
1718 InitializeCriticalSection( &(This->lock) );
1719 This->lock.DebugInfo->Spare[1] = (DWORD)"DSDUPLEX_lock";
1721 hres = IDirectSoundFullDuplexImpl_Initialize( (LPDIRECTSOUNDFULLDUPLEX)This,
1722 pcGuidCaptureDevice, pcGuidRenderDevice,
1723 pcDSCBufferDesc, pcDSBufferDesc,
1724 hWnd, dwLevel, ppDSCBuffer8, ppDSBuffer8);
1726 WARN("IDirectSoundFullDuplexImpl_Initialize failed\n");
1730 return DSERR_GENERIC;
1733 static HRESULT WINAPI
1734 IDirectSoundFullDuplexImpl_QueryInterface(
1735 LPDIRECTSOUNDFULLDUPLEX iface,
1739 IDirectSoundFullDuplexImpl *This = (IDirectSoundFullDuplexImpl *)iface;
1740 TRACE( "(%p,%s,%p)\n", This, debugstr_guid(riid), ppobj );
1742 if (ppobj == NULL) {
1743 WARN("invalid parameter\n");
1744 return E_INVALIDARG;
1748 return E_NOINTERFACE;
1752 IDirectSoundFullDuplexImpl_AddRef( LPDIRECTSOUNDFULLDUPLEX iface )
1754 IDirectSoundFullDuplexImpl *This = (IDirectSoundFullDuplexImpl *)iface;
1755 TRACE("(%p) ref was %ld, thread is %04lx\n",This, This->ref, GetCurrentThreadId());
1756 return InterlockedIncrement(&(This->ref));
1760 IDirectSoundFullDuplexImpl_Release( LPDIRECTSOUNDFULLDUPLEX iface )
1763 IDirectSoundFullDuplexImpl *This = (IDirectSoundFullDuplexImpl *)iface;
1764 TRACE("(%p) ref was %ld, thread is %04lx\n",This, This->ref, GetCurrentThreadId());
1766 uRef = InterlockedDecrement(&(This->ref));
1768 This->lock.DebugInfo->Spare[1] = 0;
1769 DeleteCriticalSection( &(This->lock) );
1770 HeapFree( GetProcessHeap(), 0, This );
1771 TRACE("(%p) released\n",This);
1777 static HRESULT WINAPI
1778 IDirectSoundFullDuplexImpl_Initialize(
1779 LPDIRECTSOUNDFULLDUPLEX iface,
1780 LPCGUID pCaptureGuid,
1781 LPCGUID pRendererGuid,
1782 LPCDSCBUFFERDESC lpDscBufferDesc,
1783 LPCDSBUFFERDESC lpDsBufferDesc,
1786 LPLPDIRECTSOUNDCAPTUREBUFFER8 lplpDirectSoundCaptureBuffer8,
1787 LPLPDIRECTSOUNDBUFFER8 lplpDirectSoundBuffer8 )
1789 IDirectSoundFullDuplexImpl *This = (IDirectSoundFullDuplexImpl *)iface;
1790 IDirectSoundCaptureBufferImpl** ippdscb=(IDirectSoundCaptureBufferImpl**)lplpDirectSoundCaptureBuffer8;
1791 IDirectSoundBufferImpl** ippdsc=(IDirectSoundBufferImpl**)lplpDirectSoundBuffer8;
1793 FIXME( "(%p,%s,%s,%p,%p,%lx,%lx,%p,%p) stub!\n", This, debugstr_guid(pCaptureGuid),
1794 debugstr_guid(pRendererGuid), lpDscBufferDesc, lpDsBufferDesc, (DWORD)hWnd, dwLevel,
1800 static IDirectSoundFullDuplexVtbl dsfdvt =
1802 /* IUnknown methods */
1803 IDirectSoundFullDuplexImpl_QueryInterface,
1804 IDirectSoundFullDuplexImpl_AddRef,
1805 IDirectSoundFullDuplexImpl_Release,
1807 /* IDirectSoundFullDuplex methods */
1808 IDirectSoundFullDuplexImpl_Initialize
1811 /*******************************************************************************
1812 * DirectSoundFullDuplex ClassFactory
1815 static HRESULT WINAPI
1816 DSFDCF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj)
1818 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
1820 FIXME("(%p)->(%s,%p),stub!\n",This,debugstr_guid(riid),ppobj);
1821 return E_NOINTERFACE;
1825 DSFDCF_AddRef(LPCLASSFACTORY iface)
1827 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
1828 TRACE("(%p) ref was %ld\n", This, This->ref);
1829 return InterlockedIncrement(&(This->ref));
1833 DSFDCF_Release(LPCLASSFACTORY iface)
1835 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
1836 /* static class, won't be freed */
1837 TRACE("(%p) ref was %ld\n", This, This->ref);
1838 return InterlockedDecrement(&(This->ref));
1841 static HRESULT WINAPI
1842 DSFDCF_CreateInstance(
1843 LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj )
1845 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
1847 TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
1850 WARN("aggregation not supported\n");
1851 return CLASS_E_NOAGGREGATION;
1854 if (ppobj == NULL) {
1855 WARN("invalid parameter\n");
1856 return E_INVALIDARG;
1861 if ( IsEqualGUID( &IID_IDirectSoundFullDuplex, riid ) ) {
1862 /* FIXME: how do we do this one ? */
1863 FIXME("not implemented\n");
1864 return E_NOINTERFACE;
1867 WARN("(%p,%p,%s,%p) Interface not found!\n",This,pOuter,debugstr_guid(riid),ppobj);
1868 return E_NOINTERFACE;
1871 static HRESULT WINAPI
1872 DSFDCF_LockServer(LPCLASSFACTORY iface,BOOL dolock)
1874 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
1875 FIXME("(%p)->(%d),stub!\n",This,dolock);
1879 static IClassFactoryVtbl DSFDCF_Vtbl =
1881 DSFDCF_QueryInterface,
1884 DSFDCF_CreateInstance,
1888 IClassFactoryImpl DSOUND_FULLDUPLEX_CF = { &DSFDCF_Vtbl, 1 };