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.
29 #define NONAMELESSSTRUCT
30 #define NONAMELESSUNION
38 #include "wine/debug.h"
41 #include "dsound_private.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(dsound);
45 static HRESULT WINAPI IDirectSoundCaptureImpl_Initialize(
46 LPDIRECTSOUNDCAPTURE iface,
48 static ULONG WINAPI IDirectSoundCaptureImpl_Release(
49 LPDIRECTSOUNDCAPTURE iface );
50 static ULONG WINAPI IDirectSoundCaptureBufferImpl_Release(
51 LPDIRECTSOUNDCAPTUREBUFFER8 iface );
52 static HRESULT DSOUND_CreateDirectSoundCaptureBuffer(
53 IDirectSoundCaptureImpl *ipDSC,
54 LPCDSCBUFFERDESC lpcDSCBufferDesc,
56 static HRESULT WINAPI IDirectSoundFullDuplexImpl_Initialize(
57 LPDIRECTSOUNDFULLDUPLEX iface,
59 LPCGUID pRendererGuid,
60 LPCDSCBUFFERDESC lpDscBufferDesc,
61 LPCDSBUFFERDESC lpDsBufferDesc,
64 LPLPDIRECTSOUNDCAPTUREBUFFER8 lplpDirectSoundCaptureBuffer8,
65 LPLPDIRECTSOUNDBUFFER8 lplpDirectSoundBuffer8 );
67 static IDirectSoundCaptureVtbl dscvt;
68 static IDirectSoundCaptureBuffer8Vtbl dscbvt;
69 static IDirectSoundFullDuplexVtbl dsfdvt;
71 static IDirectSoundCaptureImpl* dsound_capture = NULL;
73 static const char * captureStateString[] = {
80 /***************************************************************************
81 * DirectSoundCaptureCreate [DSOUND.6]
83 * Create and initialize a DirectSoundCapture interface.
86 * lpcGUID [I] Address of the GUID that identifies the sound capture device.
87 * lplpDSC [O] Address of a variable to receive the interface pointer.
88 * pUnkOuter [I] Must be NULL.
92 * Failure: DSERR_NOAGGREGATION, DSERR_ALLOCATED, DSERR_INVALIDPARAM,
96 * lpcGUID must be one of the values returned from DirectSoundCaptureEnumerate
97 * or NULL for the default device or DSDEVID_DefaultCapture or
98 * DSDEVID_DefaultVoiceCapture.
100 * DSERR_ALLOCATED is returned for sound devices that do not support full duplex.
103 DirectSoundCaptureCreate8(
105 LPDIRECTSOUNDCAPTURE* lplpDSC,
106 LPUNKNOWN pUnkOuter )
108 IDirectSoundCaptureImpl** ippDSC=(IDirectSoundCaptureImpl**)lplpDSC;
109 TRACE("(%s,%p,%p)\n", debugstr_guid(lpcGUID), lplpDSC, pUnkOuter);
112 WARN("invalid parameter: pUnkOuter != NULL\n");
113 return DSERR_NOAGGREGATION;
117 WARN("invalid parameter: lplpDSC == NULL\n");
118 return DSERR_INVALIDPARAM;
121 /* Default device? */
122 if ( !lpcGUID || IsEqualGUID(lpcGUID, &GUID_NULL) )
123 lpcGUID = &DSDEVID_DefaultCapture;
125 *ippDSC = HeapAlloc(GetProcessHeap(),
126 HEAP_ZERO_MEMORY, sizeof(IDirectSoundCaptureImpl));
128 if (*ippDSC == NULL) {
129 WARN("out of memory\n");
130 return DSERR_OUTOFMEMORY;
132 IDirectSoundCaptureImpl *This = (IDirectSoundCaptureImpl *)*ippDSC;
135 This->state = STATE_STOPPED;
137 InitializeCriticalSection( &(This->lock) );
138 This->lock.DebugInfo->Spare[1] = (DWORD)"DSCAPTURE_lock";
140 This->lpVtbl = &dscvt;
141 dsound_capture = This;
143 if (GetDeviceID(lpcGUID, &This->guid) == DS_OK) {
145 hres = IDirectSoundCaptureImpl_Initialize( (LPDIRECTSOUNDCAPTURE)This, &This->guid);
147 WARN("IDirectSoundCaptureImpl_Initialize failed\n");
151 WARN("invalid GUID: %s\n", debugstr_guid(lpcGUID));
152 return DSERR_INVALIDPARAM;
155 /***************************************************************************
156 * DirectSoundCaptureEnumerateA [DSOUND.7]
158 * Enumerate all DirectSound drivers installed in the system.
161 * lpDSEnumCallback [I] Address of callback function.
162 * lpContext [I] Address of user defined context passed to callback function.
166 * Failure: DSERR_INVALIDPARAM
169 DirectSoundCaptureEnumerateA(
170 LPDSENUMCALLBACKA lpDSEnumCallback,
178 TRACE("(%p,%p)\n", lpDSEnumCallback, lpContext );
180 if (lpDSEnumCallback == NULL) {
181 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
182 return DSERR_INVALIDPARAM;
185 devs = waveInGetNumDevs();
187 if (GetDeviceID(&DSDEVID_DefaultCapture, &guid) == DS_OK) {
188 for (wid = 0; wid < devs; ++wid) {
189 if (IsEqualGUID( &guid, &capture_guids[wid] ) ) {
190 err = mmErr(waveInMessage((HWAVEIN)wid,DRV_QUERYDSOUNDDESC,(DWORD)&desc,0));
192 TRACE("calling lpDSEnumCallback(NULL,\"%s\",\"%s\",%p)\n",
193 "Primary Sound Capture Driver",desc.szDrvname,lpContext);
194 if (lpDSEnumCallback(NULL, "Primary Sound Capture Driver", desc.szDrvname, lpContext) == FALSE)
202 for (wid = 0; wid < devs; ++wid) {
203 err = mmErr(waveInMessage((HWAVEIN)wid,DRV_QUERYDSOUNDDESC,(DWORD)&desc,0));
205 TRACE("calling lpDSEnumCallback(%s,\"%s\",\"%s\",%p)\n",
206 debugstr_guid(&capture_guids[wid]),desc.szDesc,desc.szDrvname,lpContext);
207 if (lpDSEnumCallback(&capture_guids[wid], desc.szDesc, desc.szDrvname, lpContext) == FALSE)
215 /***************************************************************************
216 * DirectSoundCaptureEnumerateW [DSOUND.8]
218 * Enumerate all DirectSound drivers installed in the system.
221 * lpDSEnumCallback [I] Address of callback function.
222 * lpContext [I] Address of user defined context passed to callback function.
226 * Failure: DSERR_INVALIDPARAM
229 DirectSoundCaptureEnumerateW(
230 LPDSENUMCALLBACKW lpDSEnumCallback,
237 WCHAR wDesc[MAXPNAMELEN];
238 WCHAR wName[MAXPNAMELEN];
240 TRACE("(%p,%p)\n", lpDSEnumCallback, lpContext );
242 if (lpDSEnumCallback == NULL) {
243 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
244 return DSERR_INVALIDPARAM;
247 devs = waveInGetNumDevs();
249 if (GetDeviceID(&DSDEVID_DefaultCapture, &guid) == DS_OK) {
250 for (wid = 0; wid < devs; ++wid) {
251 if (IsEqualGUID( &guid, &capture_guids[wid] ) ) {
252 err = mmErr(waveInMessage((HWAVEIN)wid,DRV_QUERYDSOUNDDESC,(DWORD)&desc,0));
254 TRACE("calling lpDSEnumCallback(NULL,\"%s\",\"%s\",%p)\n",
255 "Primary Sound Capture Driver",desc.szDrvname,lpContext);
256 MultiByteToWideChar( CP_ACP, 0, "Primary Sound Capture Driver", -1,
257 wDesc, sizeof(wDesc)/sizeof(WCHAR) );
258 MultiByteToWideChar( CP_ACP, 0, desc.szDrvname, -1,
259 wName, sizeof(wName)/sizeof(WCHAR) );
260 if (lpDSEnumCallback(NULL, wDesc, wName, lpContext) == FALSE)
268 for (wid = 0; wid < devs; ++wid) {
269 err = mmErr(waveInMessage((HWAVEIN)wid,DRV_QUERYDSOUNDDESC,(DWORD)&desc,0));
271 TRACE("calling lpDSEnumCallback(%s,\"%s\",\"%s\",%p)\n",
272 debugstr_guid(&capture_guids[wid]),desc.szDesc,desc.szDrvname,lpContext);
273 MultiByteToWideChar( CP_ACP, 0, desc.szDesc, -1,
274 wDesc, sizeof(wDesc)/sizeof(WCHAR) );
275 MultiByteToWideChar( CP_ACP, 0, desc.szDrvname, -1,
276 wName, sizeof(wName)/sizeof(WCHAR) );
277 if (lpDSEnumCallback((LPGUID)&capture_guids[wid], wDesc, wName, lpContext) == FALSE)
286 DSOUND_capture_callback(
293 IDirectSoundCaptureImpl* This = (IDirectSoundCaptureImpl*)dwUser;
294 TRACE("(%p,%08x(%s),%08lx,%08lx,%08lx) entering at %ld\n",hwi,msg,
295 msg == MM_WIM_OPEN ? "MM_WIM_OPEN" : msg == MM_WIM_CLOSE ? "MM_WIM_CLOSE" :
296 msg == MM_WIM_DATA ? "MM_WIM_DATA" : "UNKNOWN",dwUser,dw1,dw2,GetTickCount());
298 if (msg == MM_WIM_DATA) {
299 LPWAVEHDR pHdr = (LPWAVEHDR)dw1;
300 EnterCriticalSection( &(This->lock) );
301 TRACE("DirectSoundCapture msg=MM_WIM_DATA, old This->state=%s, old This->index=%d\n",
302 captureStateString[This->state],This->index);
303 if (This->state != STATE_STOPPED) {
304 int index = This->index;
305 if (This->state == STATE_STARTING) {
306 This->read_position = pHdr->dwBytesRecorded;
307 This->state = STATE_CAPTURING;
309 waveInUnprepareHeader(hwi,&(This->pwave[This->index]),sizeof(WAVEHDR));
310 if (This->capture_buffer->nrofnotifies)
311 SetEvent(This->capture_buffer->notifies[This->index].hEventNotify);
312 This->index = (This->index + 1) % This->nrofpwaves;
313 if ( (This->index == 0) && !(This->capture_buffer->flags & DSCBSTART_LOOPING) ) {
314 TRACE("end of buffer\n");
315 This->state = STATE_STOPPED;
317 if (This->state == STATE_CAPTURING) {
318 waveInPrepareHeader(hwi,&(This->pwave[index]),sizeof(WAVEHDR));
319 waveInAddBuffer(hwi, &(This->pwave[index]), sizeof(WAVEHDR));
320 } else if (This->state == STATE_STOPPING) {
322 This->state = STATE_STOPPED;
326 TRACE("DirectSoundCapture new This->state=%s, new This->index=%d\n",
327 captureStateString[This->state],This->index);
328 LeaveCriticalSection( &(This->lock) );
331 TRACE("completed\n");
334 static HRESULT WINAPI
335 IDirectSoundCaptureImpl_QueryInterface(
336 LPDIRECTSOUNDCAPTURE iface,
340 IDirectSoundCaptureImpl *This = (IDirectSoundCaptureImpl *)iface;
341 TRACE( "(%p,%s,%p)\n", This, debugstr_guid(riid), ppobj );
344 WARN("invalid parameter\n");
352 hres = IDsCaptureDriver_QueryInterface(This->driver, riid, ppobj);
354 WARN("IDsCaptureDriver_QueryInterface failed\n");
358 WARN("unsupported riid: %s\n", debugstr_guid(riid));
363 IDirectSoundCaptureImpl_AddRef( LPDIRECTSOUNDCAPTURE iface )
365 IDirectSoundCaptureImpl *This = (IDirectSoundCaptureImpl *)iface;
366 ULONG ref = InterlockedIncrement(&(This->ref));
367 TRACE("(%p) ref was %ld\n", This, ref - 1);
372 IDirectSoundCaptureImpl_Release( LPDIRECTSOUNDCAPTURE iface )
374 IDirectSoundCaptureImpl *This = (IDirectSoundCaptureImpl *)iface;
375 ULONG ref = InterlockedDecrement(&(This->ref));
376 TRACE("(%p) ref was %ld\n", This, ref + 1);
379 TRACE("deleting object\n");
380 if (This->capture_buffer)
381 IDirectSoundCaptureBufferImpl_Release(
382 (LPDIRECTSOUNDCAPTUREBUFFER8) This->capture_buffer);
385 IDsCaptureDriver_Close(This->driver);
386 IDsCaptureDriver_Release(This->driver);
389 HeapFree(GetProcessHeap(), 0, This->pwfx);
390 This->lock.DebugInfo->Spare[1] = 0;
391 DeleteCriticalSection( &(This->lock) );
392 HeapFree( GetProcessHeap(), 0, This );
393 dsound_capture = NULL;
394 TRACE("(%p) released\n", This);
399 static HRESULT WINAPI
400 IDirectSoundCaptureImpl_CreateCaptureBuffer(
401 LPDIRECTSOUNDCAPTURE iface,
402 LPCDSCBUFFERDESC lpcDSCBufferDesc,
403 LPDIRECTSOUNDCAPTUREBUFFER* lplpDSCaptureBuffer,
407 IDirectSoundCaptureImpl *This = (IDirectSoundCaptureImpl *)iface;
409 TRACE( "(%p,%p,%p,%p)\n",This,lpcDSCBufferDesc,lplpDSCaptureBuffer,pUnk );
412 WARN("invalid parameter: This == NULL\n");
413 return DSERR_INVALIDPARAM;
416 if (lpcDSCBufferDesc == NULL) {
417 WARN("invalid parameter: lpcDSCBufferDesc == NULL)\n");
418 return DSERR_INVALIDPARAM;
421 if (lplpDSCaptureBuffer == NULL) {
422 WARN("invalid parameter: lplpDSCaptureBuffer == NULL\n");
423 return DSERR_INVALIDPARAM;
427 WARN("invalid parameter: pUnk != NULL\n");
428 return DSERR_INVALIDPARAM;
431 /* FIXME: We can only have one buffer so what do we do here? */
432 if (This->capture_buffer) {
433 WARN("lnvalid parameter: already has buffer\n");
434 return DSERR_INVALIDPARAM; /* DSERR_GENERIC ? */
437 hr = DSOUND_CreateDirectSoundCaptureBuffer( This, lpcDSCBufferDesc,
438 (LPVOID*)lplpDSCaptureBuffer );
441 WARN("DSOUND_CreateDirectSoundCaptureBuffer failed\n");
446 static HRESULT WINAPI
447 IDirectSoundCaptureImpl_GetCaps(
448 LPDIRECTSOUNDCAPTURE iface,
449 LPDSCCAPS lpDSCCaps )
451 IDirectSoundCaptureImpl *This = (IDirectSoundCaptureImpl *)iface;
452 TRACE("(%p,%p)\n",This,lpDSCCaps);
454 if (lpDSCCaps== NULL) {
455 WARN("invalid parameter: lpDSCCaps== NULL\n");
456 return DSERR_INVALIDPARAM;
459 if (lpDSCCaps->dwSize < sizeof(*lpDSCCaps)) {
460 WARN("invalid parameter: lpDSCCaps->dwSize = %ld < %d\n",
461 lpDSCCaps->dwSize, sizeof(*lpDSCCaps));
462 return DSERR_INVALIDPARAM;
465 if ( !(This->initialized) ) {
466 WARN("not initialized\n");
467 return DSERR_UNINITIALIZED;
470 lpDSCCaps->dwFlags = This->drvcaps.dwFlags;
471 lpDSCCaps->dwFormats = This->drvcaps.dwFormats;
472 lpDSCCaps->dwChannels = This->drvcaps.dwChannels;
474 TRACE("(flags=0x%08lx,format=0x%08lx,channels=%ld)\n",lpDSCCaps->dwFlags,
475 lpDSCCaps->dwFormats, lpDSCCaps->dwChannels);
480 static HRESULT WINAPI
481 IDirectSoundCaptureImpl_Initialize(
482 LPDIRECTSOUNDCAPTURE iface,
485 HRESULT err = DSERR_INVALIDPARAM;
487 IDirectSoundCaptureImpl *This = (IDirectSoundCaptureImpl *)iface;
488 TRACE("(%p)\n", This);
491 WARN("invalid parameter: This == NULL\n");
492 return DSERR_INVALIDPARAM;
495 if (This->initialized) {
496 WARN("already initialized\n");
497 return DSERR_ALREADYINITIALIZED;
500 widn = waveInGetNumDevs();
503 WARN("no audio devices found\n");
504 return DSERR_NODRIVER;
507 /* Get dsound configuration */
508 setup_dsound_options();
510 /* enumerate WINMM audio devices and find the one we want */
511 for (wid=0; wid<widn; wid++) {
512 if (IsEqualGUID( lpcGUID, &capture_guids[wid]) ) {
519 WARN("invalid parameter\n");
520 return DSERR_INVALIDPARAM;
523 err = mmErr(waveInMessage((HWAVEIN)wid,DRV_QUERYDSOUNDIFACE,(DWORD)&(This->driver),0));
524 if ( (err != DS_OK) && (err != DSERR_UNSUPPORTED) ) {
525 WARN("waveInMessage failed; err=%lx\n",err);
530 /* Disable the direct sound driver to force emulation if requested. */
531 if (ds_hw_accel == DS_HW_ACCEL_EMULATION)
534 /* Get driver description */
536 TRACE("using DirectSound driver\n");
537 err = IDsCaptureDriver_GetDriverDesc(This->driver, &(This->drvdesc));
539 WARN("IDsCaptureDriver_GetDriverDesc failed\n");
543 TRACE("using WINMM\n");
544 /* if no DirectSound interface available, use WINMM API instead */
545 This->drvdesc.dwFlags = DSDDESC_DOMMSYSTEMOPEN |
546 DSDDESC_DOMMSYSTEMSETFORMAT;
549 This->drvdesc.dnDevNode = wid;
551 /* open the DirectSound driver if available */
552 if (This->driver && (err == DS_OK))
553 err = IDsCaptureDriver_Open(This->driver);
556 This->initialized = TRUE;
558 /* the driver is now open, so it's now allowed to call GetCaps */
560 This->drvcaps.dwSize = sizeof(This->drvcaps);
561 err = IDsCaptureDriver_GetCaps(This->driver,&(This->drvcaps));
563 WARN("IDsCaptureDriver_GetCaps failed\n");
566 } else /*if (This->hwi)*/ {
568 err = mmErr(waveInGetDevCapsA((UINT)This->drvdesc.dnDevNode, &wic, sizeof(wic)));
571 This->drvcaps.dwFlags = 0;
572 strncpy(This->drvdesc.szDrvname, wic.szPname,
573 sizeof(This->drvdesc.szDrvname));
575 This->drvcaps.dwFlags |= DSCCAPS_EMULDRIVER;
576 This->drvcaps.dwFormats = wic.dwFormats;
577 This->drvcaps.dwChannels = wic.wChannels;
585 static IDirectSoundCaptureVtbl dscvt =
587 /* IUnknown methods */
588 IDirectSoundCaptureImpl_QueryInterface,
589 IDirectSoundCaptureImpl_AddRef,
590 IDirectSoundCaptureImpl_Release,
592 /* IDirectSoundCapture methods */
593 IDirectSoundCaptureImpl_CreateCaptureBuffer,
594 IDirectSoundCaptureImpl_GetCaps,
595 IDirectSoundCaptureImpl_Initialize
599 DSOUND_CreateDirectSoundCaptureBuffer(
600 IDirectSoundCaptureImpl *ipDSC,
601 LPCDSCBUFFERDESC lpcDSCBufferDesc,
605 TRACE( "(%p,%p)\n", lpcDSCBufferDesc, ppobj );
608 WARN("invalid parameter: ipDSC == NULL\n");
609 return DSERR_INVALIDPARAM;
612 if (lpcDSCBufferDesc == NULL) {
613 WARN("invalid parameter: lpcDSCBufferDesc == NULL\n");
614 return DSERR_INVALIDPARAM;
618 WARN("invalid parameter: ppobj == NULL\n");
619 return DSERR_INVALIDPARAM;
622 if ( ((lpcDSCBufferDesc->dwSize != sizeof(DSCBUFFERDESC)) &&
623 (lpcDSCBufferDesc->dwSize != sizeof(DSCBUFFERDESC1))) ||
624 (lpcDSCBufferDesc->dwBufferBytes == 0) ||
625 (lpcDSCBufferDesc->lpwfxFormat == NULL) ) {
626 WARN("invalid lpcDSCBufferDesc\n");
628 return DSERR_INVALIDPARAM;
631 if ( !ipDSC->initialized ) {
632 WARN("not initialized\n");
634 return DSERR_UNINITIALIZED;
637 wfex = lpcDSCBufferDesc->lpwfxFormat;
640 TRACE("(formattag=0x%04x,chans=%d,samplerate=%ld,"
641 "bytespersec=%ld,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
642 wfex->wFormatTag, wfex->nChannels, wfex->nSamplesPerSec,
643 wfex->nAvgBytesPerSec, wfex->nBlockAlign,
644 wfex->wBitsPerSample, wfex->cbSize);
646 if (wfex->wFormatTag == WAVE_FORMAT_PCM) {
647 ipDSC->pwfx = HeapAlloc(GetProcessHeap(),0,sizeof(WAVEFORMATEX));
648 CopyMemory(ipDSC->pwfx, wfex, sizeof(WAVEFORMATEX));
649 ipDSC->pwfx->cbSize = 0;
651 ipDSC->pwfx = HeapAlloc(GetProcessHeap(),0,sizeof(WAVEFORMATEX)+wfex->cbSize);
652 CopyMemory(ipDSC->pwfx, wfex, sizeof(WAVEFORMATEX)+wfex->cbSize);
655 WARN("lpcDSCBufferDesc->lpwfxFormat == 0\n");
657 return DSERR_INVALIDPARAM; /* FIXME: DSERR_BADFORMAT ? */
660 *ppobj = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,
661 sizeof(IDirectSoundCaptureBufferImpl));
663 if ( *ppobj == NULL ) {
664 WARN("out of memory\n");
666 return DSERR_OUTOFMEMORY;
671 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)*ppobj;
674 This->dsound = ipDSC;
675 This->dsound->capture_buffer = This;
677 This->nrofnotifies = 0;
678 This->hwnotify = NULL;
680 This->pdscbd = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,
681 lpcDSCBufferDesc->dwSize);
683 CopyMemory(This->pdscbd, lpcDSCBufferDesc, lpcDSCBufferDesc->dwSize);
686 This->dsound->capture_buffer = 0;
687 HeapFree( GetProcessHeap(), 0, This );
689 return DSERR_OUTOFMEMORY;
692 This->lpVtbl = &dscbvt;
695 if (This->dsound->drvdesc.dwFlags & DSDDESC_DOMMSYSTEMOPEN)
696 FIXME("DSDDESC_DOMMSYSTEMOPEN not supported\n");
698 if (This->dsound->drvdesc.dwFlags & DSDDESC_USESYSTEMMEMORY) {
699 /* allocate buffer from system memory */
700 buflen = lpcDSCBufferDesc->dwBufferBytes;
701 TRACE("desired buflen=%ld, old buffer=%p\n", buflen, ipDSC->buffer);
703 newbuf = HeapReAlloc(GetProcessHeap(),0,ipDSC->buffer,buflen);
705 newbuf = HeapAlloc(GetProcessHeap(),0,buflen);
707 if (newbuf == NULL) {
708 WARN("failed to allocate capture buffer\n");
709 err = DSERR_OUTOFMEMORY;
710 /* but the old buffer might still exist and must be re-prepared */
712 ipDSC->buffer = newbuf;
713 ipDSC->buflen = buflen;
716 /* let driver allocate memory */
717 ipDSC->buflen = lpcDSCBufferDesc->dwBufferBytes;
719 HeapFree( GetProcessHeap(), 0, ipDSC->buffer);
720 ipDSC->buffer = NULL;
723 err = IDsCaptureDriver_CreateCaptureBuffer(ipDSC->driver,
724 ipDSC->pwfx,0,0,&(ipDSC->buflen),&(ipDSC->buffer),(LPVOID*)&(ipDSC->hwbuf));
726 WARN("IDsCaptureDriver_CreateCaptureBuffer failed\n");
727 This->dsound->capture_buffer = 0;
728 HeapFree( GetProcessHeap(), 0, This );
733 DWORD flags = CALLBACK_FUNCTION;
734 if (ds_hw_accel != DS_HW_ACCEL_EMULATION)
735 flags |= WAVE_DIRECTSOUND;
736 err = mmErr(waveInOpen(&(ipDSC->hwi),
737 ipDSC->drvdesc.dnDevNode, ipDSC->pwfx,
738 (DWORD)DSOUND_capture_callback, (DWORD)ipDSC, flags));
740 WARN("waveInOpen failed\n");
741 This->dsound->capture_buffer = 0;
742 HeapFree( GetProcessHeap(), 0, This );
747 buflen = lpcDSCBufferDesc->dwBufferBytes;
748 TRACE("desired buflen=%ld, old buffer=%p\n", buflen, ipDSC->buffer);
750 newbuf = HeapReAlloc(GetProcessHeap(),0,ipDSC->buffer,buflen);
752 newbuf = HeapAlloc(GetProcessHeap(),0,buflen);
753 if (newbuf == NULL) {
754 WARN("failed to allocate capture buffer\n");
755 err = DSERR_OUTOFMEMORY;
756 /* but the old buffer might still exist and must be re-prepared */
758 ipDSC->buffer = newbuf;
759 ipDSC->buflen = buflen;
764 TRACE("returning DS_OK\n");
768 /*******************************************************************************
769 * IDirectSoundCaptureNotify
771 static HRESULT WINAPI IDirectSoundCaptureNotifyImpl_QueryInterface(
772 LPDIRECTSOUNDNOTIFY iface,
776 IDirectSoundCaptureNotifyImpl *This = (IDirectSoundCaptureNotifyImpl *)iface;
777 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
779 if (This->dscb == NULL) {
780 WARN("invalid parameter\n");
784 return IDirectSoundCaptureBuffer_QueryInterface((LPDIRECTSOUNDCAPTUREBUFFER)This->dscb, riid, ppobj);
787 static ULONG WINAPI IDirectSoundCaptureNotifyImpl_AddRef(LPDIRECTSOUNDNOTIFY iface)
789 IDirectSoundCaptureNotifyImpl *This = (IDirectSoundCaptureNotifyImpl *)iface;
790 ULONG ref = InterlockedIncrement(&(This->ref));
791 TRACE("(%p) ref was %ld\n", This, ref - 1);
795 static ULONG WINAPI IDirectSoundCaptureNotifyImpl_Release(LPDIRECTSOUNDNOTIFY iface)
797 IDirectSoundCaptureNotifyImpl *This = (IDirectSoundCaptureNotifyImpl *)iface;
798 ULONG ref = InterlockedDecrement(&(This->ref));
799 TRACE("(%p) ref was %ld\n", This, ref + 1);
802 if (This->dscb->hwnotify)
803 IDsDriverNotify_Release(This->dscb->hwnotify);
804 This->dscb->notify=NULL;
805 IDirectSoundCaptureBuffer_Release((LPDIRECTSOUNDCAPTUREBUFFER)This->dscb);
806 HeapFree(GetProcessHeap(),0,This);
807 TRACE("(%p) released\n", This);
812 static HRESULT WINAPI IDirectSoundCaptureNotifyImpl_SetNotificationPositions(
813 LPDIRECTSOUNDNOTIFY iface,
815 LPCDSBPOSITIONNOTIFY notify)
817 IDirectSoundCaptureNotifyImpl *This = (IDirectSoundCaptureNotifyImpl *)iface;
818 TRACE("(%p,0x%08lx,%p)\n",This,howmuch,notify);
820 if (howmuch > 0 && notify == NULL) {
821 WARN("invalid parameter: notify == NULL\n");
822 return DSERR_INVALIDPARAM;
825 if (TRACE_ON(dsound)) {
827 for (i=0;i<howmuch;i++)
828 TRACE("notify at %ld to 0x%08lx\n",
829 notify[i].dwOffset,(DWORD)notify[i].hEventNotify);
832 if (This->dscb->hwnotify) {
834 hres = IDsDriverNotify_SetNotificationPositions(This->dscb->hwnotify, howmuch, notify);
836 WARN("IDsDriverNotify_SetNotificationPositions failed\n");
838 } else if (howmuch > 0) {
839 /* Make an internal copy of the caller-supplied array.
840 * Replace the existing copy if one is already present. */
841 if (This->dscb->notifies)
842 This->dscb->notifies = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
843 This->dscb->notifies, howmuch * sizeof(DSBPOSITIONNOTIFY));
845 This->dscb->notifies = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
846 howmuch * sizeof(DSBPOSITIONNOTIFY));
848 if (This->dscb->notifies == NULL) {
849 WARN("out of memory\n");
850 return DSERR_OUTOFMEMORY;
852 CopyMemory(This->dscb->notifies, notify, howmuch * sizeof(DSBPOSITIONNOTIFY));
853 This->dscb->nrofnotifies = howmuch;
855 HeapFree(GetProcessHeap(), 0, This->dscb->notifies);
856 This->dscb->notifies = NULL;
857 This->dscb->nrofnotifies = 0;
863 IDirectSoundNotifyVtbl dscnvt =
865 IDirectSoundCaptureNotifyImpl_QueryInterface,
866 IDirectSoundCaptureNotifyImpl_AddRef,
867 IDirectSoundCaptureNotifyImpl_Release,
868 IDirectSoundCaptureNotifyImpl_SetNotificationPositions,
871 HRESULT WINAPI IDirectSoundCaptureNotifyImpl_Create(
872 IDirectSoundCaptureBufferImpl *dscb,
873 IDirectSoundCaptureNotifyImpl **pdscn)
875 IDirectSoundCaptureNotifyImpl * dscn;
876 TRACE("(%p,%p)\n",dscb,pdscn);
878 dscn = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(dscn));
881 WARN("out of memory\n");
882 return DSERR_OUTOFMEMORY;
886 dscn->lpVtbl = &dscnvt;
889 IDirectSoundCaptureBuffer_AddRef((LPDIRECTSOUNDCAPTUREBUFFER)dscb);
895 /*******************************************************************************
896 * IDirectSoundCaptureBuffer
898 static HRESULT WINAPI
899 IDirectSoundCaptureBufferImpl_QueryInterface(
900 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
904 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
906 TRACE( "(%p,%s,%p)\n", This, debugstr_guid(riid), ppobj );
909 WARN("invalid parameter\n");
915 if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ) {
917 hres = IDirectSoundCaptureNotifyImpl_Create(This, &This->notify);
919 if (This->dsound->hwbuf) {
920 hres = IDsCaptureDriverBuffer_QueryInterface(This->dsound->hwbuf,
921 &IID_IDsDriverNotify, (LPVOID*)&(This->hwnotify));
923 WARN("IDsCaptureDriverBuffer_QueryInterface failed\n");
929 IDirectSoundNotify_AddRef((LPDIRECTSOUNDNOTIFY)This->notify);
930 *ppobj = (LPVOID)This->notify;
934 WARN("IID_IDirectSoundNotify\n");
938 if ( IsEqualGUID( &IID_IDirectSoundCaptureBuffer, riid ) ||
939 IsEqualGUID( &IID_IDirectSoundCaptureBuffer8, riid ) ) {
940 IDirectSoundCaptureBuffer8_AddRef(iface);
945 FIXME("(%p,%s,%p) unsupported GUID\n", This, debugstr_guid(riid), ppobj);
946 return E_NOINTERFACE;
950 IDirectSoundCaptureBufferImpl_AddRef( LPDIRECTSOUNDCAPTUREBUFFER8 iface )
952 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
953 ULONG ref = InterlockedIncrement(&(This->ref));
954 TRACE("(%p) ref was %ld\n", This, ref - 1);
959 IDirectSoundCaptureBufferImpl_Release( LPDIRECTSOUNDCAPTUREBUFFER8 iface )
961 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
962 ULONG ref = InterlockedDecrement(&(This->ref));
963 TRACE("(%p) ref was %ld\n", This, ref + 1);
966 TRACE("deleting object\n");
967 if (This->dsound->state == STATE_CAPTURING)
968 This->dsound->state = STATE_STOPPING;
970 HeapFree(GetProcessHeap(),0, This->pdscbd);
972 if (This->dsound->hwi) {
973 waveInReset(This->dsound->hwi);
974 waveInClose(This->dsound->hwi);
975 HeapFree(GetProcessHeap(),0, This->dsound->pwave);
976 This->dsound->pwave = 0;
977 This->dsound->hwi = 0;
980 if (This->dsound->hwbuf)
981 IDsCaptureDriverBuffer_Release(This->dsound->hwbuf);
983 /* remove from IDirectSoundCaptureImpl */
985 This->dsound->capture_buffer = NULL;
987 ERR("does not reference dsound\n");
990 IDirectSoundNotify_Release((LPDIRECTSOUNDNOTIFY)This->notify);
992 HeapFree(GetProcessHeap(), 0, This->notifies);
993 HeapFree( GetProcessHeap(), 0, This );
994 TRACE("(%p) released\n", This);
999 static HRESULT WINAPI
1000 IDirectSoundCaptureBufferImpl_GetCaps(
1001 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
1002 LPDSCBCAPS lpDSCBCaps )
1004 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
1005 TRACE( "(%p,%p)\n", This, lpDSCBCaps );
1008 WARN("invalid parameter: This == NULL\n");
1009 return DSERR_INVALIDPARAM;
1012 if (lpDSCBCaps == NULL) {
1013 WARN("invalid parameter: lpDSCBCaps == NULL\n");
1014 return DSERR_INVALIDPARAM;
1017 if (lpDSCBCaps->dwSize < sizeof(DSCBCAPS)) {
1018 WARN("invalid parameter: lpDSCBCaps->dwSize = %ld < %d\n",
1019 lpDSCBCaps->dwSize, sizeof(DSCBCAPS));
1020 return DSERR_INVALIDPARAM;
1023 if (This->dsound == NULL) {
1024 WARN("invalid parameter: This->dsound == NULL\n");
1025 return DSERR_INVALIDPARAM;
1028 lpDSCBCaps->dwSize = sizeof(DSCBCAPS);
1029 lpDSCBCaps->dwFlags = This->flags;
1030 lpDSCBCaps->dwBufferBytes = This->pdscbd->dwBufferBytes;
1031 lpDSCBCaps->dwReserved = 0;
1033 TRACE("returning DS_OK\n");
1037 static HRESULT WINAPI
1038 IDirectSoundCaptureBufferImpl_GetCurrentPosition(
1039 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
1040 LPDWORD lpdwCapturePosition,
1041 LPDWORD lpdwReadPosition )
1043 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
1044 HRESULT hres = DS_OK;
1045 TRACE( "(%p,%p,%p)\n", This, lpdwCapturePosition, lpdwReadPosition );
1048 WARN("invalid parameter: This == NULL\n");
1049 return DSERR_INVALIDPARAM;
1052 if (This->dsound == NULL) {
1053 WARN("invalid parameter: This->dsound == NULL\n");
1054 return DSERR_INVALIDPARAM;
1057 if (This->dsound->driver) {
1058 hres = IDsCaptureDriverBuffer_GetPosition(This->dsound->hwbuf, lpdwCapturePosition, lpdwReadPosition );
1060 WARN("IDsCaptureDriverBuffer_GetPosition failed\n");
1061 } else if (This->dsound->hwi) {
1062 EnterCriticalSection(&(This->dsound->lock));
1063 TRACE("old This->dsound->state=%s\n",captureStateString[This->dsound->state]);
1064 if (lpdwCapturePosition) {
1066 mtime.wType = TIME_BYTES;
1067 waveInGetPosition(This->dsound->hwi, &mtime, sizeof(mtime));
1068 TRACE("mtime.u.cb=%ld,This->dsound->buflen=%ld\n", mtime.u.cb,
1069 This->dsound->buflen);
1070 mtime.u.cb = mtime.u.cb % This->dsound->buflen;
1071 *lpdwCapturePosition = mtime.u.cb;
1074 if (lpdwReadPosition) {
1075 if (This->dsound->state == STATE_STARTING) {
1076 if (lpdwCapturePosition)
1077 This->dsound->read_position = *lpdwCapturePosition;
1078 This->dsound->state = STATE_CAPTURING;
1080 *lpdwReadPosition = This->dsound->read_position;
1082 TRACE("new This->dsound->state=%s\n",captureStateString[This->dsound->state]);
1083 LeaveCriticalSection(&(This->dsound->lock));
1084 if (lpdwCapturePosition) TRACE("*lpdwCapturePosition=%ld\n",*lpdwCapturePosition);
1085 if (lpdwReadPosition) TRACE("*lpdwReadPosition=%ld\n",*lpdwReadPosition);
1087 WARN("no driver\n");
1088 hres = DSERR_NODRIVER;
1091 TRACE("returning %08lx\n", hres);
1095 static HRESULT WINAPI
1096 IDirectSoundCaptureBufferImpl_GetFormat(
1097 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
1098 LPWAVEFORMATEX lpwfxFormat,
1099 DWORD dwSizeAllocated,
1100 LPDWORD lpdwSizeWritten )
1102 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
1103 HRESULT hres = DS_OK;
1104 TRACE( "(%p,%p,0x%08lx,%p)\n", This, lpwfxFormat, dwSizeAllocated,
1108 WARN("invalid parameter: This == NULL\n");
1109 return DSERR_INVALIDPARAM;
1112 if (This->dsound == NULL) {
1113 WARN("invalid parameter: This->dsound == NULL\n");
1114 return DSERR_INVALIDPARAM;
1117 if (dwSizeAllocated > (sizeof(WAVEFORMATEX) + This->dsound->pwfx->cbSize))
1118 dwSizeAllocated = sizeof(WAVEFORMATEX) + This->dsound->pwfx->cbSize;
1120 if (lpwfxFormat) { /* NULL is valid (just want size) */
1121 CopyMemory(lpwfxFormat, This->dsound->pwfx, dwSizeAllocated);
1122 if (lpdwSizeWritten)
1123 *lpdwSizeWritten = dwSizeAllocated;
1125 if (lpdwSizeWritten)
1126 *lpdwSizeWritten = sizeof(WAVEFORMATEX) + This->dsound->pwfx->cbSize;
1128 TRACE("invalid parameter: lpdwSizeWritten = NULL\n");
1129 hres = DSERR_INVALIDPARAM;
1133 TRACE("returning %08lx\n", hres);
1137 static HRESULT WINAPI
1138 IDirectSoundCaptureBufferImpl_GetStatus(
1139 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
1140 LPDWORD lpdwStatus )
1142 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
1143 TRACE( "(%p, %p), thread is %04lx\n", This, lpdwStatus, GetCurrentThreadId() );
1146 WARN("invalid parameter: This == NULL\n");
1147 return DSERR_INVALIDPARAM;
1150 if (This->dsound == NULL) {
1151 WARN("invalid parameter: This->dsound == NULL\n");
1152 return DSERR_INVALIDPARAM;
1155 if (lpdwStatus == NULL) {
1156 WARN("invalid parameter: lpdwStatus == NULL\n");
1157 return DSERR_INVALIDPARAM;
1161 EnterCriticalSection(&(This->dsound->lock));
1163 TRACE("old This->dsound->state=%s, old lpdwStatus=%08lx\n",
1164 captureStateString[This->dsound->state],*lpdwStatus);
1165 if ((This->dsound->state == STATE_STARTING) ||
1166 (This->dsound->state == STATE_CAPTURING)) {
1167 *lpdwStatus |= DSCBSTATUS_CAPTURING;
1168 if (This->flags & DSCBSTART_LOOPING)
1169 *lpdwStatus |= DSCBSTATUS_LOOPING;
1171 TRACE("new This->dsound->state=%s, new lpdwStatus=%08lx\n",
1172 captureStateString[This->dsound->state],*lpdwStatus);
1173 LeaveCriticalSection(&(This->dsound->lock));
1175 TRACE("status=%lx\n", *lpdwStatus);
1176 TRACE("returning DS_OK\n");
1180 static HRESULT WINAPI
1181 IDirectSoundCaptureBufferImpl_Initialize(
1182 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
1183 LPDIRECTSOUNDCAPTURE lpDSC,
1184 LPCDSCBUFFERDESC lpcDSCBDesc )
1186 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
1188 FIXME( "(%p,%p,%p): stub\n", This, lpDSC, lpcDSCBDesc );
1193 static HRESULT WINAPI
1194 IDirectSoundCaptureBufferImpl_Lock(
1195 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
1198 LPVOID* lplpvAudioPtr1,
1199 LPDWORD lpdwAudioBytes1,
1200 LPVOID* lplpvAudioPtr2,
1201 LPDWORD lpdwAudioBytes2,
1204 HRESULT hres = DS_OK;
1205 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
1206 TRACE( "(%p,%08lu,%08lu,%p,%p,%p,%p,0x%08lx) at %ld\n", This, dwReadCusor,
1207 dwReadBytes, lplpvAudioPtr1, lpdwAudioBytes1, lplpvAudioPtr2,
1208 lpdwAudioBytes2, dwFlags, GetTickCount() );
1211 WARN("invalid parameter: This == NULL\n");
1212 return DSERR_INVALIDPARAM;
1215 if (This->dsound == NULL) {
1216 WARN("invalid parameter: This->dsound == NULL\n");
1217 return DSERR_INVALIDPARAM;
1220 if (lplpvAudioPtr1 == NULL) {
1221 WARN("invalid parameter: lplpvAudioPtr1 == NULL\n");
1222 return DSERR_INVALIDPARAM;
1225 if (lpdwAudioBytes1 == NULL) {
1226 WARN("invalid parameter: lpdwAudioBytes1 == NULL\n");
1227 return DSERR_INVALIDPARAM;
1230 EnterCriticalSection(&(This->dsound->lock));
1232 if (This->dsound->driver) {
1233 hres = IDsCaptureDriverBuffer_Lock(This->dsound->hwbuf, lplpvAudioPtr1,
1234 lpdwAudioBytes1, lplpvAudioPtr2,
1235 lpdwAudioBytes2, dwReadCusor,
1236 dwReadBytes, dwFlags);
1238 WARN("IDsCaptureDriverBuffer_Lock failed\n");
1239 } else if (This->dsound->hwi) {
1240 *lplpvAudioPtr1 = This->dsound->buffer + dwReadCusor;
1241 if ( (dwReadCusor + dwReadBytes) > This->dsound->buflen) {
1242 *lpdwAudioBytes1 = This->dsound->buflen - dwReadCusor;
1244 *lplpvAudioPtr2 = This->dsound->buffer;
1245 if (lpdwAudioBytes2)
1246 *lpdwAudioBytes2 = dwReadBytes - *lpdwAudioBytes1;
1248 *lpdwAudioBytes1 = dwReadBytes;
1250 *lplpvAudioPtr2 = 0;
1251 if (lpdwAudioBytes2)
1252 *lpdwAudioBytes2 = 0;
1255 TRACE("invalid call\n");
1256 hres = DSERR_INVALIDCALL; /* DSERR_NODRIVER ? */
1259 LeaveCriticalSection(&(This->dsound->lock));
1261 TRACE("returning %08lx\n", hres);
1265 static HRESULT WINAPI
1266 IDirectSoundCaptureBufferImpl_Start(
1267 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
1270 HRESULT hres = DS_OK;
1271 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
1272 TRACE( "(%p,0x%08lx)\n", This, dwFlags );
1275 WARN("invalid parameter: This == NULL\n");
1276 return DSERR_INVALIDPARAM;
1279 if (This->dsound == NULL) {
1280 WARN("invalid parameter: This->dsound == NULL\n");
1281 return DSERR_INVALIDPARAM;
1284 if ( (This->dsound->driver == 0) && (This->dsound->hwi == 0) ) {
1285 WARN("no driver\n");
1286 return DSERR_NODRIVER;
1289 EnterCriticalSection(&(This->dsound->lock));
1291 This->flags = dwFlags;
1292 TRACE("old This->dsound->state=%s\n",captureStateString[This->dsound->state]);
1293 if (This->dsound->state == STATE_STOPPED)
1294 This->dsound->state = STATE_STARTING;
1295 else if (This->dsound->state == STATE_STOPPING)
1296 This->dsound->state = STATE_CAPTURING;
1297 TRACE("new This->dsound->state=%s\n",captureStateString[This->dsound->state]);
1299 LeaveCriticalSection(&(This->dsound->lock));
1301 if (This->dsound->driver) {
1302 hres = IDsCaptureDriverBuffer_Start(This->dsound->hwbuf, dwFlags);
1304 WARN("IDsCaptureDriverBuffer_Start failed\n");
1305 } else if (This->dsound->hwi) {
1306 IDirectSoundCaptureImpl* ipDSC = This->dsound;
1308 if (ipDSC->buffer) {
1309 if (This->nrofnotifies) {
1312 ipDSC->nrofpwaves = This->nrofnotifies;
1313 TRACE("nrofnotifies=%d\n", This->nrofnotifies);
1315 /* prepare headers */
1317 ipDSC->pwave = HeapReAlloc(GetProcessHeap(),0,ipDSC->pwave,
1318 ipDSC->nrofpwaves*sizeof(WAVEHDR));
1320 ipDSC->pwave = HeapAlloc(GetProcessHeap(),0,
1321 ipDSC->nrofpwaves*sizeof(WAVEHDR));
1323 for (c = 0; c < ipDSC->nrofpwaves; c++) {
1324 if (This->notifies[c].dwOffset == DSBPN_OFFSETSTOP) {
1325 TRACE("got DSBPN_OFFSETSTOP\n");
1326 ipDSC->nrofpwaves = c;
1330 ipDSC->pwave[0].lpData = ipDSC->buffer;
1331 ipDSC->pwave[0].dwBufferLength =
1332 This->notifies[0].dwOffset + 1;
1334 ipDSC->pwave[c].lpData = ipDSC->buffer +
1335 This->notifies[c-1].dwOffset + 1;
1336 ipDSC->pwave[c].dwBufferLength =
1337 This->notifies[c].dwOffset -
1338 This->notifies[c-1].dwOffset;
1340 ipDSC->pwave[c].dwBytesRecorded = 0;
1341 ipDSC->pwave[c].dwUser = (DWORD)ipDSC;
1342 ipDSC->pwave[c].dwFlags = 0;
1343 ipDSC->pwave[c].dwLoops = 0;
1344 hres = mmErr(waveInPrepareHeader(ipDSC->hwi,
1345 &(ipDSC->pwave[c]),sizeof(WAVEHDR)));
1346 if (hres != DS_OK) {
1347 WARN("waveInPrepareHeader failed\n");
1349 waveInUnprepareHeader(ipDSC->hwi,
1350 &(ipDSC->pwave[c]),sizeof(WAVEHDR));
1354 hres = mmErr(waveInAddBuffer(ipDSC->hwi,
1355 &(ipDSC->pwave[c]), sizeof(WAVEHDR)));
1356 if (hres != DS_OK) {
1357 WARN("waveInAddBuffer failed\n");
1359 waveInUnprepareHeader(ipDSC->hwi,
1360 &(ipDSC->pwave[c]),sizeof(WAVEHDR));
1365 FillMemory(ipDSC->buffer, ipDSC->buflen,
1366 (ipDSC->pwfx->wBitsPerSample == 8) ? 128 : 0);
1368 TRACE("no notifiers specified\n");
1369 /* no notifiers specified so just create a single default header */
1370 ipDSC->nrofpwaves = 1;
1372 ipDSC->pwave = HeapReAlloc(GetProcessHeap(),0,ipDSC->pwave,sizeof(WAVEHDR));
1374 ipDSC->pwave = HeapAlloc(GetProcessHeap(),0,sizeof(WAVEHDR));
1376 ipDSC->pwave[0].lpData = ipDSC->buffer;
1377 ipDSC->pwave[0].dwBufferLength = ipDSC->buflen;
1378 ipDSC->pwave[0].dwBytesRecorded = 0;
1379 ipDSC->pwave[0].dwUser = (DWORD)ipDSC;
1380 ipDSC->pwave[0].dwFlags = 0;
1381 ipDSC->pwave[0].dwLoops = 0;
1383 hres = mmErr(waveInPrepareHeader(ipDSC->hwi,
1384 &(ipDSC->pwave[0]),sizeof(WAVEHDR)));
1385 if (hres != DS_OK) {
1386 WARN("waveInPrepareHeader failed\n");
1387 waveInUnprepareHeader(ipDSC->hwi,
1388 &(ipDSC->pwave[0]),sizeof(WAVEHDR));
1390 hres = mmErr(waveInAddBuffer(ipDSC->hwi,
1391 &(ipDSC->pwave[0]), sizeof(WAVEHDR)));
1392 if (hres != DS_OK) {
1393 WARN("waveInAddBuffer failed\n");
1394 waveInUnprepareHeader(ipDSC->hwi,
1395 &(ipDSC->pwave[0]),sizeof(WAVEHDR));
1401 ipDSC->read_position = 0;
1403 if (hres == DS_OK) {
1404 /* start filling the first buffer */
1405 hres = mmErr(waveInStart(ipDSC->hwi));
1407 WARN("waveInStart failed\n");
1410 if (hres != DS_OK) {
1411 WARN("calling waveInClose because of error\n");
1412 waveInClose(This->dsound->hwi);
1413 This->dsound->hwi = 0;
1416 WARN("no driver\n");
1417 hres = DSERR_NODRIVER;
1420 TRACE("returning %08lx\n", hres);
1424 static HRESULT WINAPI
1425 IDirectSoundCaptureBufferImpl_Stop( LPDIRECTSOUNDCAPTUREBUFFER8 iface )
1427 HRESULT hres = DS_OK;
1428 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
1429 TRACE( "(%p)\n", This );
1432 WARN("invalid parameter: This == NULL\n");
1433 return DSERR_INVALIDPARAM;
1436 if (This->dsound == NULL) {
1437 WARN("invalid parameter: This->dsound == NULL\n");
1438 return DSERR_INVALIDPARAM;
1441 EnterCriticalSection(&(This->dsound->lock));
1443 TRACE("old This->dsound->state=%s\n",captureStateString[This->dsound->state]);
1444 if (This->dsound->state == STATE_CAPTURING)
1445 This->dsound->state = STATE_STOPPING;
1446 else if (This->dsound->state == STATE_STARTING)
1447 This->dsound->state = STATE_STOPPED;
1448 TRACE("new This->dsound->state=%s\n",captureStateString[This->dsound->state]);
1450 LeaveCriticalSection(&(This->dsound->lock));
1452 if (This->dsound->driver) {
1453 hres = IDsCaptureDriverBuffer_Stop(This->dsound->hwbuf);
1455 WARN("IDsCaptureDriverBuffer_Stop() failed\n");
1456 } else if (This->dsound->hwi) {
1457 hres = mmErr(waveInReset(This->dsound->hwi));
1459 WARN("waveInReset() failed\n");
1461 WARN("no driver\n");
1462 hres = DSERR_NODRIVER;
1465 TRACE("returning %08lx\n", hres);
1469 static HRESULT WINAPI
1470 IDirectSoundCaptureBufferImpl_Unlock(
1471 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
1472 LPVOID lpvAudioPtr1,
1473 DWORD dwAudioBytes1,
1474 LPVOID lpvAudioPtr2,
1475 DWORD dwAudioBytes2 )
1477 HRESULT hres = DS_OK;
1478 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
1479 TRACE( "(%p,%p,%08lu,%p,%08lu)\n", This, lpvAudioPtr1, dwAudioBytes1,
1480 lpvAudioPtr2, dwAudioBytes2 );
1483 WARN("invalid parameter: This == NULL\n");
1484 return DSERR_INVALIDPARAM;
1487 if (lpvAudioPtr1 == NULL) {
1488 WARN("invalid parameter: lpvAudioPtr1 == NULL\n");
1489 return DSERR_INVALIDPARAM;
1492 if (This->dsound->driver) {
1493 hres = IDsCaptureDriverBuffer_Unlock(This->dsound->hwbuf, lpvAudioPtr1,
1494 dwAudioBytes1, lpvAudioPtr2, dwAudioBytes2);
1496 WARN("IDsCaptureDriverBuffer_Unlock failed\n");
1497 } else if (This->dsound->hwi) {
1498 This->dsound->read_position = (This->dsound->read_position +
1499 (dwAudioBytes1 + dwAudioBytes2)) % This->dsound->buflen;
1501 WARN("invalid call\n");
1502 hres = DSERR_INVALIDCALL;
1505 TRACE("returning %08lx\n", hres);
1509 static HRESULT WINAPI
1510 IDirectSoundCaptureBufferImpl_GetObjectInPath(
1511 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
1512 REFGUID rguidObject,
1514 REFGUID rguidInterface,
1517 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
1519 FIXME( "(%p,%s,%lu,%s,%p): stub\n", This, debugstr_guid(rguidObject),
1520 dwIndex, debugstr_guid(rguidInterface), ppObject );
1525 static HRESULT WINAPI
1526 IDirectSoundCaptureBufferImpl_GetFXStatus(
1527 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
1529 LPDWORD pdwFXStatus )
1531 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
1533 FIXME( "(%p,%lu,%p): stub\n", This, dwFXCount, pdwFXStatus );
1538 static IDirectSoundCaptureBuffer8Vtbl dscbvt =
1540 /* IUnknown methods */
1541 IDirectSoundCaptureBufferImpl_QueryInterface,
1542 IDirectSoundCaptureBufferImpl_AddRef,
1543 IDirectSoundCaptureBufferImpl_Release,
1545 /* IDirectSoundCaptureBuffer methods */
1546 IDirectSoundCaptureBufferImpl_GetCaps,
1547 IDirectSoundCaptureBufferImpl_GetCurrentPosition,
1548 IDirectSoundCaptureBufferImpl_GetFormat,
1549 IDirectSoundCaptureBufferImpl_GetStatus,
1550 IDirectSoundCaptureBufferImpl_Initialize,
1551 IDirectSoundCaptureBufferImpl_Lock,
1552 IDirectSoundCaptureBufferImpl_Start,
1553 IDirectSoundCaptureBufferImpl_Stop,
1554 IDirectSoundCaptureBufferImpl_Unlock,
1556 /* IDirectSoundCaptureBuffer methods */
1557 IDirectSoundCaptureBufferImpl_GetObjectInPath,
1558 IDirectSoundCaptureBufferImpl_GetFXStatus
1561 /*******************************************************************************
1562 * DirectSoundCapture ClassFactory
1565 static HRESULT WINAPI
1566 DSCCF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj)
1568 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
1570 FIXME("(%p)->(%s,%p),stub!\n",This,debugstr_guid(riid),ppobj);
1571 return E_NOINTERFACE;
1575 DSCCF_AddRef(LPCLASSFACTORY iface)
1577 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
1578 ULONG ref = InterlockedIncrement(&(This->ref));
1579 TRACE("(%p) ref was %ld\n", This, ref - 1);
1584 DSCCF_Release(LPCLASSFACTORY iface)
1586 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
1587 ULONG ref = InterlockedDecrement(&(This->ref));
1588 TRACE("(%p) ref was %ld\n", This, ref + 1);
1589 /* static class, won't be freed */
1593 static HRESULT WINAPI
1594 DSCCF_CreateInstance(
1595 LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj )
1597 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
1598 TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
1601 WARN("aggregation not supported\n");
1602 return CLASS_E_NOAGGREGATION;
1605 if (ppobj == NULL) {
1606 WARN("invalid parameter\n");
1607 return E_INVALIDARG;
1612 if ( IsEqualGUID( &IID_IDirectSoundCapture8, riid ) )
1613 return DirectSoundCaptureCreate8(0,(LPDIRECTSOUNDCAPTURE8*)ppobj,pOuter);
1615 WARN("(%p,%p,%s,%p) Interface not found!\n",This,pOuter,debugstr_guid(riid),ppobj);
1616 return E_NOINTERFACE;
1619 static HRESULT WINAPI
1620 DSCCF_LockServer(LPCLASSFACTORY iface,BOOL dolock)
1622 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
1623 FIXME("(%p)->(%d),stub!\n",This,dolock);
1627 static IClassFactoryVtbl DSCCF_Vtbl =
1629 DSCCF_QueryInterface,
1632 DSCCF_CreateInstance,
1636 IClassFactoryImpl DSOUND_CAPTURE_CF = { &DSCCF_Vtbl, 1 };
1638 /***************************************************************************
1639 * DirectSoundFullDuplexCreate [DSOUND.10]
1641 * Create and initialize a DirectSoundFullDuplex interface.
1644 * pcGuidCaptureDevice [I] Address of sound capture device GUID.
1645 * pcGuidRenderDevice [I] Address of sound render device GUID.
1646 * pcDSCBufferDesc [I] Address of capture buffer description.
1647 * pcDSBufferDesc [I] Address of render buffer description.
1648 * hWnd [I] Handle to application window.
1649 * dwLevel [I] Cooperative level.
1650 * ppDSFD [O] Address where full duplex interface returned.
1651 * ppDSCBuffer8 [0] Address where capture buffer interface returned.
1652 * ppDSBuffer8 [0] Address where render buffer interface returned.
1653 * pUnkOuter [I] Must be NULL.
1657 * Failure: DSERR_NOAGGREGATION, DSERR_ALLOCATED, DSERR_INVALIDPARAM,
1658 * DSERR_OUTOFMEMORY DSERR_INVALIDCALL DSERR_NODRIVER
1661 DirectSoundFullDuplexCreate(
1662 LPCGUID pcGuidCaptureDevice,
1663 LPCGUID pcGuidRenderDevice,
1664 LPCDSCBUFFERDESC pcDSCBufferDesc,
1665 LPCDSBUFFERDESC pcDSBufferDesc,
1668 LPDIRECTSOUNDFULLDUPLEX *ppDSFD,
1669 LPDIRECTSOUNDCAPTUREBUFFER8 *ppDSCBuffer8,
1670 LPDIRECTSOUNDBUFFER8 *ppDSBuffer8,
1671 LPUNKNOWN pUnkOuter)
1673 IDirectSoundFullDuplexImpl** ippDSFD=(IDirectSoundFullDuplexImpl**)ppDSFD;
1674 TRACE("(%s,%s,%p,%p,%lx,%lx,%p,%p,%p,%p)\n", debugstr_guid(pcGuidCaptureDevice),
1675 debugstr_guid(pcGuidRenderDevice), pcDSCBufferDesc, pcDSBufferDesc,
1676 (DWORD)hWnd, dwLevel, ppDSFD, ppDSCBuffer8, ppDSBuffer8, pUnkOuter);
1679 WARN("pUnkOuter != 0\n");
1680 return DSERR_NOAGGREGATION;
1683 *ippDSFD = HeapAlloc(GetProcessHeap(),
1684 HEAP_ZERO_MEMORY, sizeof(IDirectSoundFullDuplexImpl));
1686 if (*ippDSFD == NULL) {
1687 WARN("out of memory\n");
1688 return DSERR_OUTOFMEMORY;
1691 IDirectSoundFullDuplexImpl *This = (IDirectSoundFullDuplexImpl *)*ippDSFD;
1694 This->lpVtbl = &dsfdvt;
1696 InitializeCriticalSection( &(This->lock) );
1697 This->lock.DebugInfo->Spare[1] = (DWORD)"DSDUPLEX_lock";
1699 hres = IDirectSoundFullDuplexImpl_Initialize( (LPDIRECTSOUNDFULLDUPLEX)This,
1700 pcGuidCaptureDevice, pcGuidRenderDevice,
1701 pcDSCBufferDesc, pcDSBufferDesc,
1702 hWnd, dwLevel, ppDSCBuffer8, ppDSBuffer8);
1704 WARN("IDirectSoundFullDuplexImpl_Initialize failed\n");
1709 static HRESULT WINAPI
1710 IDirectSoundFullDuplexImpl_QueryInterface(
1711 LPDIRECTSOUNDFULLDUPLEX iface,
1715 IDirectSoundFullDuplexImpl *This = (IDirectSoundFullDuplexImpl *)iface;
1716 TRACE( "(%p,%s,%p)\n", This, debugstr_guid(riid), ppobj );
1718 if (ppobj == NULL) {
1719 WARN("invalid parameter\n");
1720 return E_INVALIDARG;
1724 return E_NOINTERFACE;
1728 IDirectSoundFullDuplexImpl_AddRef( LPDIRECTSOUNDFULLDUPLEX iface )
1730 IDirectSoundFullDuplexImpl *This = (IDirectSoundFullDuplexImpl *)iface;
1731 ULONG ref = InterlockedIncrement(&(This->ref));
1732 TRACE("(%p) ref was %ld\n", This, ref - 1);
1737 IDirectSoundFullDuplexImpl_Release( LPDIRECTSOUNDFULLDUPLEX iface )
1739 IDirectSoundFullDuplexImpl *This = (IDirectSoundFullDuplexImpl *)iface;
1740 ULONG ref = InterlockedDecrement(&(This->ref));
1741 TRACE("(%p) ref was %ld\n", This, ref - 1);
1744 This->lock.DebugInfo->Spare[1] = 0;
1745 DeleteCriticalSection( &(This->lock) );
1746 HeapFree( GetProcessHeap(), 0, This );
1747 TRACE("(%p) released\n", This);
1752 static HRESULT WINAPI
1753 IDirectSoundFullDuplexImpl_Initialize(
1754 LPDIRECTSOUNDFULLDUPLEX iface,
1755 LPCGUID pCaptureGuid,
1756 LPCGUID pRendererGuid,
1757 LPCDSCBUFFERDESC lpDscBufferDesc,
1758 LPCDSBUFFERDESC lpDsBufferDesc,
1761 LPLPDIRECTSOUNDCAPTUREBUFFER8 lplpDirectSoundCaptureBuffer8,
1762 LPLPDIRECTSOUNDBUFFER8 lplpDirectSoundBuffer8 )
1764 IDirectSoundFullDuplexImpl *This = (IDirectSoundFullDuplexImpl *)iface;
1765 IDirectSoundCaptureBufferImpl** ippdscb=(IDirectSoundCaptureBufferImpl**)lplpDirectSoundCaptureBuffer8;
1766 IDirectSoundBufferImpl** ippdsc=(IDirectSoundBufferImpl**)lplpDirectSoundBuffer8;
1768 FIXME( "(%p,%s,%s,%p,%p,%lx,%lx,%p,%p) stub!\n", This, debugstr_guid(pCaptureGuid),
1769 debugstr_guid(pRendererGuid), lpDscBufferDesc, lpDsBufferDesc, (DWORD)hWnd, dwLevel,
1775 static IDirectSoundFullDuplexVtbl dsfdvt =
1777 /* IUnknown methods */
1778 IDirectSoundFullDuplexImpl_QueryInterface,
1779 IDirectSoundFullDuplexImpl_AddRef,
1780 IDirectSoundFullDuplexImpl_Release,
1782 /* IDirectSoundFullDuplex methods */
1783 IDirectSoundFullDuplexImpl_Initialize
1786 /*******************************************************************************
1787 * DirectSoundFullDuplex ClassFactory
1790 static HRESULT WINAPI
1791 DSFDCF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj)
1793 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
1795 FIXME("(%p)->(%s,%p),stub!\n",This,debugstr_guid(riid),ppobj);
1796 return E_NOINTERFACE;
1800 DSFDCF_AddRef(LPCLASSFACTORY iface)
1802 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
1803 TRACE("(%p) ref was %ld\n", This, This->ref);
1804 return InterlockedIncrement(&(This->ref));
1808 DSFDCF_Release(LPCLASSFACTORY iface)
1810 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
1811 /* static class, won't be freed */
1812 TRACE("(%p) ref was %ld\n", This, This->ref);
1813 return InterlockedDecrement(&(This->ref));
1816 static HRESULT WINAPI
1817 DSFDCF_CreateInstance(
1818 LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj )
1820 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
1822 TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
1825 WARN("aggregation not supported\n");
1826 return CLASS_E_NOAGGREGATION;
1829 if (ppobj == NULL) {
1830 WARN("invalid parameter\n");
1831 return E_INVALIDARG;
1836 if ( IsEqualGUID( &IID_IDirectSoundFullDuplex, riid ) ) {
1837 /* FIXME: how do we do this one ? */
1838 FIXME("not implemented\n");
1839 return E_NOINTERFACE;
1842 WARN("(%p,%p,%s,%p) Interface not found!\n",This,pOuter,debugstr_guid(riid),ppobj);
1843 return E_NOINTERFACE;
1846 static HRESULT WINAPI
1847 DSFDCF_LockServer(LPCLASSFACTORY iface,BOOL dolock)
1849 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
1850 FIXME("(%p)->(%d),stub!\n",This,dolock);
1854 static IClassFactoryVtbl DSFDCF_Vtbl =
1856 DSFDCF_QueryInterface,
1859 DSFDCF_CreateInstance,
1863 IClassFactoryImpl DSOUND_FULLDUPLEX_CF = { &DSFDCF_Vtbl, 1 };