Added support for multi-texturing.
[wine.git] / dlls / dsound / capture.c
blobd14b2f771e726f1e4a3baf450836283c74338338
1 /* DirectSoundCapture
3 * Copyright 1998 Marcus Meissner
4 * Copyright 1998 Rob Riggs
5 * Copyright 2000-2001 TransGaming Technologies, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * TODO:
23 * Implement DirectSoundFullDuplex support.
24 * Implement FX support.
27 #include "config.h"
28 #include <assert.h>
29 #include <stdio.h>
30 #include <sys/types.h>
31 #include <sys/fcntl.h>
32 #ifdef HAVE_UNISTD_H
33 # include <unistd.h>
34 #endif
35 #include <stdlib.h>
36 #include <string.h>
38 #include "windef.h"
39 #include "winbase.h"
40 #include "wingdi.h"
41 #include "winuser.h"
42 #include "winerror.h"
43 #include "mmsystem.h"
44 #include "mmddk.h"
45 #include "winternl.h"
46 #include "winnls.h"
47 #include "wine/debug.h"
48 #include "dsound.h"
49 #include "dsdriver.h"
50 #include "dsound_private.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(dsound);
54 static HRESULT WINAPI IDirectSoundCaptureImpl_Initialize(
55 LPDIRECTSOUNDCAPTURE iface,
56 LPCGUID lpcGUID );
57 static ULONG WINAPI IDirectSoundCaptureImpl_Release(
58 LPDIRECTSOUNDCAPTURE iface );
59 static ULONG WINAPI IDirectSoundCaptureBufferImpl_Release(
60 LPDIRECTSOUNDCAPTUREBUFFER8 iface );
61 static HRESULT DSOUND_CreateDirectSoundCaptureBuffer(
62 IDirectSoundCaptureImpl *ipDSC,
63 LPCDSCBUFFERDESC lpcDSCBufferDesc,
64 LPVOID* ppobj );
65 static HRESULT WINAPI IDirectSoundFullDuplexImpl_Initialize(
66 LPDIRECTSOUNDFULLDUPLEX iface,
67 LPCGUID pCaptureGuid,
68 LPCGUID pRendererGuid,
69 LPCDSCBUFFERDESC lpDscBufferDesc,
70 LPCDSBUFFERDESC lpDsBufferDesc,
71 HWND hWnd,
72 DWORD dwLevel,
73 LPLPDIRECTSOUNDCAPTUREBUFFER8 lplpDirectSoundCaptureBuffer8,
74 LPLPDIRECTSOUNDBUFFER8 lplpDirectSoundBuffer8 );
76 static ICOM_VTABLE(IDirectSoundCapture) dscvt;
77 static ICOM_VTABLE(IDirectSoundCaptureBuffer8) dscbvt;
78 static ICOM_VTABLE(IDirectSoundFullDuplex) dsfdvt;
80 IDirectSoundCaptureImpl* dsound_capture = NULL;
82 /***************************************************************************
83 * DirectSoundCaptureCreate [DSOUND.6]
85 * Create and initialize a DirectSoundCapture interface.
87 * PARAMS
88 * lpcGUID [I] Address of the GUID that identifies the sound capture device.
89 * lplpDSC [O] Address of a variable to receive the interface pointer.
90 * pUnkOuter [I] Must be NULL.
92 * RETURNS
93 * Success: DS_OK
94 * Failure: DSERR_NOAGGREGATION, DSERR_ALLOCATED, DSERR_INVALIDPARAM,
95 * DSERR_OUTOFMEMORY
97 * NOTES
98 * lpcGUID must be one of the values returned from DirectSoundCaptureEnumerate
99 * or NULL for the default device or DSDEVID_DefaultCapture or
100 * DSDEVID_DefaultVoiceCapture.
102 * DSERR_ALLOCATED is returned for sound devices that do not support full duplex.
104 HRESULT WINAPI
105 DirectSoundCaptureCreate8(
106 LPCGUID lpcGUID,
107 LPDIRECTSOUNDCAPTURE* lplpDSC,
108 LPUNKNOWN pUnkOuter )
110 IDirectSoundCaptureImpl** ippDSC=(IDirectSoundCaptureImpl**)lplpDSC;
111 TRACE("(%s,%p,%p)\n", debugstr_guid(lpcGUID), lplpDSC, pUnkOuter);
113 if ( pUnkOuter ) {
114 WARN("invalid parameter: pUnkOuter != NULL\n");
115 return DSERR_NOAGGREGATION;
118 if ( !lplpDSC ) {
119 WARN("invalid parameter: lplpDSC == NULL\n");
120 return DSERR_INVALIDPARAM;
123 /* Default device? */
124 if ( !lpcGUID || IsEqualGUID(lpcGUID, &GUID_NULL) )
125 lpcGUID = &DSDEVID_DefaultCapture;
127 *ippDSC = (IDirectSoundCaptureImpl*)HeapAlloc(GetProcessHeap(),
128 HEAP_ZERO_MEMORY, sizeof(IDirectSoundCaptureImpl));
130 if (*ippDSC == NULL) {
131 WARN("out of memory\n");
132 return DSERR_OUTOFMEMORY;
133 } else {
134 ICOM_THIS(IDirectSoundCaptureImpl, *ippDSC);
136 This->ref = 1;
137 This->state = STATE_STOPPED;
139 InitializeCriticalSection( &(This->lock) );
141 This->lpVtbl = &dscvt;
142 dsound_capture = This;
144 if (GetDeviceID(lpcGUID, &This->guid) == DS_OK) {
145 HRESULT hres;
146 hres = IDirectSoundCaptureImpl_Initialize( (LPDIRECTSOUNDCAPTURE)This, &This->guid);
147 if (hres != DS_OK)
148 WARN("IDirectSoundCaptureImpl_Initialize failed\n");
149 return hres;
152 WARN("invalid GUID: %s\n", debugstr_guid(lpcGUID));
153 return DSERR_INVALIDPARAM;
156 /***************************************************************************
157 * DirectSoundCaptureEnumerateA [DSOUND.7]
159 * Enumerate all DirectSound drivers installed in the system.
161 * PARAMS
162 * lpDSEnumCallback [I] Address of callback function.
163 * lpContext [I] Address of user defined context passed to callback function.
165 * RETURNS
166 * Success: DS_OK
167 * Failure: DSERR_INVALIDPARAM
169 HRESULT WINAPI
170 DirectSoundCaptureEnumerateA(
171 LPDSENUMCALLBACKA lpDSEnumCallback,
172 LPVOID lpContext)
174 unsigned devs, wid;
175 DSDRIVERDESC desc;
176 GUID guid;
177 int err;
179 TRACE("(%p,%p)\n", lpDSEnumCallback, lpContext );
181 if (lpDSEnumCallback == NULL) {
182 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
183 return DSERR_INVALIDPARAM;
186 devs = waveInGetNumDevs();
187 if (devs > 0) {
188 if (GetDeviceID(&DSDEVID_DefaultCapture, &guid) == DS_OK) {
189 GUID temp;
190 for (wid = 0; wid < devs; ++wid) {
191 err = mmErr(waveInMessage((HWAVEIN)wid,DRV_QUERYDSOUNDGUID,(DWORD)&temp,0));
192 if (err == DS_OK) {
193 if (IsEqualGUID( &guid, &temp ) ) {
194 err = mmErr(waveInMessage((HWAVEIN)wid,DRV_QUERYDSOUNDDESC,(DWORD)&desc,0));
195 if (err == DS_OK) {
196 TRACE("calling lpDSEnumCallback(%s,\"%s\",\"%s\",%p)\n",
197 debugstr_guid(&DSDEVID_DefaultCapture),"Primary Sound Capture Driver",desc.szDrvName,lpContext);
198 if (lpDSEnumCallback((LPGUID)&DSDEVID_DefaultCapture, "Primary Sound Capture Driver", desc.szDrvName, lpContext) == FALSE)
199 return DS_OK;
207 for (wid = 0; wid < devs; ++wid) {
208 err = mmErr(waveInMessage((HWAVEIN)wid,DRV_QUERYDSOUNDDESC,(DWORD)&desc,0));
209 if (err == DS_OK) {
210 err = mmErr(waveInMessage((HWAVEIN)wid,DRV_QUERYDSOUNDGUID,(DWORD)&guid,0));
211 if (err == DS_OK) {
212 TRACE("calling lpDSEnumCallback(%s,\"%s\",\"%s\",%p)\n",
213 debugstr_guid(&guid),desc.szDesc,desc.szDrvName,lpContext);
214 if (lpDSEnumCallback(&guid, desc.szDesc, desc.szDrvName, lpContext) == FALSE)
215 return DS_OK;
220 return DS_OK;
223 /***************************************************************************
224 * DirectSoundCaptureEnumerateW [DSOUND.8]
226 * Enumerate all DirectSound drivers installed in the system.
228 * PARAMS
229 * lpDSEnumCallback [I] Address of callback function.
230 * lpContext [I] Address of user defined context passed to callback function.
232 * RETURNS
233 * Success: DS_OK
234 * Failure: DSERR_INVALIDPARAM
236 HRESULT WINAPI
237 DirectSoundCaptureEnumerateW(
238 LPDSENUMCALLBACKW lpDSEnumCallback,
239 LPVOID lpContext)
241 unsigned devs, wid;
242 DSDRIVERDESC desc;
243 GUID guid;
244 int err;
245 WCHAR wDesc[MAXPNAMELEN];
246 WCHAR wName[MAXPNAMELEN];
248 TRACE("(%p,%p)\n", lpDSEnumCallback, lpContext );
250 if (lpDSEnumCallback == NULL) {
251 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
252 return DSERR_INVALIDPARAM;
255 devs = waveInGetNumDevs();
256 if (devs > 0) {
257 if (GetDeviceID(&DSDEVID_DefaultCapture, &guid) == DS_OK) {
258 GUID temp;
259 for (wid = 0; wid < devs; ++wid) {
260 err = mmErr(waveInMessage((HWAVEIN)wid,DRV_QUERYDSOUNDGUID,(DWORD)&temp,0));
261 if (err == DS_OK) {
262 if (IsEqualGUID( &guid, &temp ) ) {
263 err = mmErr(waveInMessage((HWAVEIN)wid,DRV_QUERYDSOUNDDESC,(DWORD)&desc,0));
264 if (err == DS_OK) {
265 TRACE("calling lpDSEnumCallback(%s,\"%s\",\"%s\",%p)\n",
266 debugstr_guid(&DSDEVID_DefaultCapture),"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((LPGUID)&DSDEVID_DefaultCapture, wDesc, wName, lpContext) == FALSE)
272 return DS_OK;
280 for (wid = 0; wid < devs; ++wid) {
281 err = mmErr(waveInMessage((HWAVEIN)wid,DRV_QUERYDSOUNDDESC,(DWORD)&desc,0));
282 if (err == DS_OK) {
283 err = mmErr(waveInMessage((HWAVEIN)wid,DRV_QUERYDSOUNDGUID,(DWORD)&guid,0));
284 if (err == DS_OK) {
285 TRACE("calling lpDSEnumCallback(%s,\"%s\",\"%s\",%p)\n",
286 debugstr_guid(&DSDEVID_DefaultCapture),desc.szDesc,desc.szDrvName,lpContext);
287 MultiByteToWideChar( CP_ACP, 0, desc.szDesc, -1,
288 wDesc, sizeof(wDesc)/sizeof(WCHAR) );
289 MultiByteToWideChar( CP_ACP, 0, desc.szDrvName, -1,
290 wName, sizeof(wName)/sizeof(WCHAR) );
291 if (lpDSEnumCallback((LPGUID)&DSDEVID_DefaultCapture, wDesc, wName, lpContext) == FALSE)
292 return DS_OK;
297 return DS_OK;
300 static void CALLBACK
301 DSOUND_capture_callback(
302 HWAVEIN hwi,
303 UINT msg,
304 DWORD dwUser,
305 DWORD dw1,
306 DWORD dw2 )
308 IDirectSoundCaptureImpl* This = (IDirectSoundCaptureImpl*)dwUser;
309 TRACE("(%p,%08x(%s),%08lx,%08lx,%08lx) entering at %ld\n",hwi,msg,
310 msg == MM_WIM_OPEN ? "MM_WIM_OPEN" : msg == MM_WIM_CLOSE ? "MM_WIM_CLOSE" :
311 msg == MM_WIM_DATA ? "MM_WIM_DATA" : "UNKNOWN",dwUser,dw1,dw2,GetTickCount());
313 if (msg == MM_WIM_DATA) {
314 EnterCriticalSection( &(This->lock) );
315 TRACE("DirectSoundCapture msg=MM_WIM_DATA, old This->state=%ld, old This->index=%d\n",This->state,This->index);
316 if (This->state != STATE_STOPPED) {
317 if (This->state == STATE_STARTING) {
318 MMTIME mtime;
319 mtime.wType = TIME_BYTES;
320 waveInGetPosition(This->hwi, &mtime, sizeof(mtime));
321 TRACE("mtime.u.cb=%ld,This->buflen=%ld\n", mtime.u.cb, This->buflen);
322 mtime.u.cb = mtime.u.cb % This->buflen;
323 This->read_position = mtime.u.cb;
324 This->state = STATE_CAPTURING;
326 This->index = (This->index + 1) % This->nrofpwaves;
327 waveInUnprepareHeader(hwi,&(This->pwave[This->index]),sizeof(WAVEHDR));
328 if (This->capture_buffer->notify && This->capture_buffer->notify->nrofnotifies)
329 SetEvent(This->capture_buffer->notify->notifies[This->index].hEventNotify);
330 if ( (This->index == 0) && !(This->capture_buffer->flags & DSCBSTART_LOOPING) ) {
331 TRACE("end of buffer\n");
332 This->state = STATE_STOPPED;
333 } else {
334 if (This->state == STATE_CAPTURING) {
335 waveInPrepareHeader(hwi,&(This->pwave[This->index]),sizeof(WAVEHDR));
336 waveInAddBuffer(hwi, &(This->pwave[This->index]), sizeof(WAVEHDR));
340 TRACE("DirectSoundCapture new This->state=%ld, new This->index=%d\n",This->state,This->index);
341 LeaveCriticalSection( &(This->lock) );
344 TRACE("completed\n");
347 static HRESULT WINAPI
348 IDirectSoundCaptureImpl_QueryInterface(
349 LPDIRECTSOUNDCAPTURE iface,
350 REFIID riid,
351 LPVOID* ppobj )
353 ICOM_THIS(IDirectSoundCaptureImpl,iface);
354 TRACE( "(%p,%s,%p)\n", This, debugstr_guid(riid), ppobj );
356 if (ppobj == NULL) {
357 WARN("invalid parameter\n");
358 return E_INVALIDARG;
361 *ppobj = NULL;
363 if (This->driver) {
364 HRESULT hres;
365 hres = IDsCaptureDriver_QueryInterface(This->driver, riid, ppobj);
366 if (hres != DS_OK)
367 WARN("IDsCaptureDriver_QueryInterface failed\n");
368 return hres;
371 WARN("unsupported riid: %s\n", debugstr_guid(riid));
372 return E_FAIL;
375 static ULONG WINAPI
376 IDirectSoundCaptureImpl_AddRef( LPDIRECTSOUNDCAPTURE iface )
378 ULONG uRef;
379 ICOM_THIS(IDirectSoundCaptureImpl,iface);
381 EnterCriticalSection( &(This->lock) );
383 TRACE( "(%p) was 0x%08lx\n", This, This->ref );
384 uRef = ++(This->ref);
386 if (This->driver)
387 IDsCaptureDriver_AddRef(This->driver);
389 LeaveCriticalSection( &(This->lock) );
391 return uRef;
394 static ULONG WINAPI
395 IDirectSoundCaptureImpl_Release( LPDIRECTSOUNDCAPTURE iface )
397 ULONG uRef;
398 ICOM_THIS(IDirectSoundCaptureImpl,iface);
400 EnterCriticalSection( &(This->lock) );
402 TRACE( "(%p) was 0x%08lx\n", This, This->ref );
403 uRef = --(This->ref);
405 LeaveCriticalSection( &(This->lock) );
407 if ( uRef == 0 ) {
408 TRACE("deleting object\n");
409 if (This->capture_buffer)
410 IDirectSoundCaptureBufferImpl_Release(
411 (LPDIRECTSOUNDCAPTUREBUFFER8) This->capture_buffer);
413 if (This->driver) {
414 IDsCaptureDriver_Close(This->driver);
415 IDsCaptureDriver_Release(This->driver);
418 DeleteCriticalSection( &(This->lock) );
419 HeapFree( GetProcessHeap(), 0, This );
420 dsound_capture = NULL;
423 TRACE( "returning 0x%08lx\n", uRef );
424 return uRef;
427 static HRESULT WINAPI
428 IDirectSoundCaptureImpl_CreateCaptureBuffer(
429 LPDIRECTSOUNDCAPTURE iface,
430 LPCDSCBUFFERDESC lpcDSCBufferDesc,
431 LPDIRECTSOUNDCAPTUREBUFFER* lplpDSCaptureBuffer,
432 LPUNKNOWN pUnk )
434 HRESULT hr;
435 ICOM_THIS(IDirectSoundCaptureImpl,iface);
437 TRACE( "(%p,%p,%p,%p)\n",This,lpcDSCBufferDesc,lplpDSCaptureBuffer,pUnk );
439 if (This == NULL) {
440 WARN("invalid parameter: This == NULL\n");
441 return DSERR_INVALIDPARAM;
444 if (lpcDSCBufferDesc == NULL) {
445 WARN("invalid parameter: lpcDSCBufferDesc == NULL)\n");
446 return DSERR_INVALIDPARAM;
449 if (lplpDSCaptureBuffer == NULL) {
450 WARN("invalid parameter: lplpDSCaptureBuffer == NULL\n");
451 return DSERR_INVALIDPARAM;
454 if (pUnk != NULL) {
455 WARN("invalid parameter: pUnk != NULL\n");
456 return DSERR_INVALIDPARAM;
459 /* FIXME: We can only have one buffer so what do we do here? */
460 if (This->capture_buffer) {
461 WARN("lnvalid parameter: already has buffer\n");
462 return DSERR_INVALIDPARAM; /* DSERR_GENERIC ? */
465 hr = DSOUND_CreateDirectSoundCaptureBuffer( This, lpcDSCBufferDesc,
466 (LPVOID*)lplpDSCaptureBuffer );
468 if (hr != DS_OK)
469 WARN("DSOUND_CreateDirectSoundCaptureBuffer failed\n");
471 return hr;
474 static HRESULT WINAPI
475 IDirectSoundCaptureImpl_GetCaps(
476 LPDIRECTSOUNDCAPTURE iface,
477 LPDSCCAPS lpDSCCaps )
479 ICOM_THIS(IDirectSoundCaptureImpl,iface);
480 TRACE("(%p,%p)\n",This,lpDSCCaps);
482 if (lpDSCCaps== NULL) {
483 WARN("invalid parameter: lpDSCCaps== NULL\n");
484 return DSERR_INVALIDPARAM;
487 if (lpDSCCaps->dwSize < sizeof(*lpDSCCaps)) {
488 WARN("invalid parameter: lpDSCCaps->dwSize = %ld < %d\n",
489 lpDSCCaps->dwSize, sizeof(*lpDSCCaps));
490 return DSERR_INVALIDPARAM;
493 if ( !(This->initialized) ) {
494 WARN("not initialized\n");
495 return DSERR_UNINITIALIZED;
498 lpDSCCaps->dwFlags = This->drvcaps.dwFlags;
499 lpDSCCaps->dwFormats = This->drvcaps.dwFormats;
500 lpDSCCaps->dwChannels = This->drvcaps.dwChannels;
502 TRACE("(flags=0x%08lx,format=0x%08lx,channels=%ld)\n",lpDSCCaps->dwFlags,
503 lpDSCCaps->dwFormats, lpDSCCaps->dwChannels);
505 return DS_OK;
508 static HRESULT WINAPI
509 IDirectSoundCaptureImpl_Initialize(
510 LPDIRECTSOUNDCAPTURE iface,
511 LPCGUID lpcGUID )
513 HRESULT err = DSERR_INVALIDPARAM;
514 unsigned wid, widn;
515 ICOM_THIS(IDirectSoundCaptureImpl,iface);
516 TRACE("(%p)\n", This);
518 if (!This) {
519 WARN("invalid parameter: This == NULL\n");
520 return DSERR_INVALIDPARAM;
523 if (This->initialized) {
524 WARN("already initialized\n");
525 return DSERR_ALREADYINITIALIZED;
528 widn = waveInGetNumDevs();
530 if (!widn) {
531 WARN("no audio devices found\n");
532 return DSERR_NODRIVER;
535 /* Get dsound configuration */
536 setup_dsound_options();
538 /* enumerate WINMM audio devices and find the one we want */
539 for (wid=0; wid<widn; wid++) {
540 GUID guid;
541 err = mmErr(waveInMessage((HWAVEIN)wid,DRV_QUERYDSOUNDGUID,(DWORD)(&guid),0));
542 if (err != DS_OK) {
543 WARN("waveInMessage failed; err=%lx\n",err);
544 return err;
546 if (IsEqualGUID( lpcGUID, &guid) ) {
547 err = DS_OK;
548 break;
552 if (err != DS_OK) {
553 WARN("invalid parameter\n");
554 return DSERR_INVALIDPARAM;
557 err = mmErr(waveInMessage((HWAVEIN)wid,DRV_QUERYDSOUNDIFACE,(DWORD)&(This->driver),0));
558 if ( (err != DS_OK) && (err != DSERR_UNSUPPORTED) ) {
559 WARN("waveInMessage failed; err=%lx\n",err);
560 return err;
562 err = DS_OK;
564 /* Disable the direct sound driver to force emulation if requested. */
565 if (ds_hw_accel == DS_HW_ACCEL_EMULATION)
566 This->driver = NULL;
568 /* Get driver description */
569 if (This->driver) {
570 TRACE("using DirectSound driver\n");
571 err = IDsCaptureDriver_GetDriverDesc(This->driver, &(This->drvdesc));
572 if (err != DS_OK) {
573 WARN("IDsCaptureDriver_GetDriverDesc failed\n");
574 return err;
576 } else {
577 TRACE("using WINMM\n");
578 /* if no DirectSound interface available, use WINMM API instead */
579 This->drvdesc.dwFlags = DSDDESC_DOMMSYSTEMOPEN |
580 DSDDESC_DOMMSYSTEMSETFORMAT;
583 This->drvdesc.dnDevNode = wid;
585 /* open the DirectSound driver if available */
586 if (This->driver && (err == DS_OK))
587 err = IDsCaptureDriver_Open(This->driver);
589 if (err == DS_OK) {
590 This->initialized = TRUE;
592 /* the driver is now open, so it's now allowed to call GetCaps */
593 if (This->driver) {
594 This->drvcaps.dwSize = sizeof(This->drvcaps);
595 err = IDsCaptureDriver_GetCaps(This->driver,&(This->drvcaps));
596 if (err != DS_OK) {
597 WARN("IDsCaptureDriver_GetCaps failed\n");
598 return err;
600 } else /*if (This->hwi)*/ {
601 WAVEINCAPSA wic;
602 err = mmErr(waveInGetDevCapsA((UINT)This->drvdesc.dnDevNode, &wic, sizeof(wic)));
604 if (err == DS_OK) {
605 This->drvcaps.dwFlags = 0;
606 strncpy(This->drvdesc.szDrvName, wic.szPname,
607 sizeof(This->drvdesc.szDrvName));
609 This->drvcaps.dwFlags |= DSCCAPS_EMULDRIVER;
610 This->drvcaps.dwFormats = wic.dwFormats;
611 This->drvcaps.dwChannels = wic.wChannels;
616 return err;
619 static ICOM_VTABLE(IDirectSoundCapture) dscvt =
621 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
622 /* IUnknown methods */
623 IDirectSoundCaptureImpl_QueryInterface,
624 IDirectSoundCaptureImpl_AddRef,
625 IDirectSoundCaptureImpl_Release,
627 /* IDirectSoundCapture methods */
628 IDirectSoundCaptureImpl_CreateCaptureBuffer,
629 IDirectSoundCaptureImpl_GetCaps,
630 IDirectSoundCaptureImpl_Initialize
633 static HRESULT
634 DSOUND_CreateDirectSoundCaptureBuffer(
635 IDirectSoundCaptureImpl *ipDSC,
636 LPCDSCBUFFERDESC lpcDSCBufferDesc,
637 LPVOID* ppobj )
639 LPWAVEFORMATEX wfex;
640 TRACE( "(%p,%p)\n", lpcDSCBufferDesc, ppobj );
642 if (ipDSC == NULL) {
643 WARN("invalid parameter: ipDSC == NULL\n");
644 return DSERR_INVALIDPARAM;
647 if (lpcDSCBufferDesc == NULL) {
648 WARN("invalid parameter: lpcDSCBufferDesc == NULL\n");
649 return DSERR_INVALIDPARAM;
652 if (ppobj == NULL) {
653 WARN("invalid parameter: ppobj == NULL\n");
654 return DSERR_INVALIDPARAM;
657 if ( (lpcDSCBufferDesc->dwSize < sizeof(DSCBUFFERDESC)) ||
658 (lpcDSCBufferDesc->dwBufferBytes == 0) ||
659 (lpcDSCBufferDesc->lpwfxFormat == NULL) ) {
660 WARN("invalid lpcDSCBufferDesc\n");
661 *ppobj = NULL;
662 return DSERR_INVALIDPARAM;
665 if ( !ipDSC->initialized ) {
666 WARN("not initialized\n");
667 *ppobj = NULL;
668 return DSERR_UNINITIALIZED;
671 wfex = lpcDSCBufferDesc->lpwfxFormat;
673 if (wfex) {
674 TRACE("(formattag=0x%04x,chans=%d,samplerate=%ld,"
675 "bytespersec=%ld,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
676 wfex->wFormatTag, wfex->nChannels, wfex->nSamplesPerSec,
677 wfex->nAvgBytesPerSec, wfex->nBlockAlign,
678 wfex->wBitsPerSample, wfex->cbSize);
680 if (wfex->wFormatTag == WAVE_FORMAT_PCM)
681 memcpy(&(ipDSC->wfx), wfex, sizeof(WAVEFORMATEX));
682 else {
683 WARN("non PCM formats not supported\n");
684 *ppobj = NULL;
685 return DSERR_BADFORMAT;
687 } else {
688 WARN("lpcDSCBufferDesc->lpwfxFormat == 0\n");
689 *ppobj = NULL;
690 return DSERR_INVALIDPARAM; /* FIXME: DSERR_BADFORMAT ? */
693 *ppobj = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,
694 sizeof(IDirectSoundCaptureBufferImpl));
696 if ( *ppobj == NULL ) {
697 WARN("out of memory\n");
698 *ppobj = NULL;
699 return DSERR_OUTOFMEMORY;
700 } else {
701 HRESULT err = DS_OK;
702 ICOM_THIS(IDirectSoundCaptureBufferImpl,*ppobj);
704 This->ref = 1;
705 This->dsound = ipDSC;
706 This->dsound->capture_buffer = This;
708 This->pdscbd = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,
709 lpcDSCBufferDesc->dwSize);
710 if (This->pdscbd)
711 memcpy(This->pdscbd, lpcDSCBufferDesc, lpcDSCBufferDesc->dwSize);
712 else {
713 WARN("no memory\n");
714 This->dsound->capture_buffer = 0;
715 HeapFree( GetProcessHeap(), 0, This );
716 *ppobj = NULL;
717 return DSERR_OUTOFMEMORY;
720 This->lpVtbl = &dscbvt;
722 if (ipDSC->driver) {
723 err = IDsCaptureDriver_CreateCaptureBuffer(ipDSC->driver,
724 &(ipDSC->wfx),0,0,&(ipDSC->buflen),&(ipDSC->buffer),(LPVOID*)&(ipDSC->hwbuf));
725 if (err != DS_OK) {
726 WARN("IDsCaptureDriver_CreateCaptureBuffer failed\n");
727 This->dsound->capture_buffer = 0;
728 HeapFree( GetProcessHeap(), 0, This );
729 *ppobj = NULL;
730 return err;
732 } else {
733 LPBYTE newbuf;
734 DWORD buflen;
735 DWORD flags = CALLBACK_FUNCTION;
736 if (ds_hw_accel != DS_HW_ACCEL_EMULATION)
737 flags |= WAVE_DIRECTSOUND;
738 err = mmErr(waveInOpen(&(ipDSC->hwi),
739 ipDSC->drvdesc.dnDevNode, &(ipDSC->wfx),
740 (DWORD)DSOUND_capture_callback, (DWORD)ipDSC, flags));
741 if (err != DS_OK) {
742 WARN("waveInOpen failed\n");
743 This->dsound->capture_buffer = 0;
744 HeapFree( GetProcessHeap(), 0, This );
745 *ppobj = NULL;
746 return err;
749 buflen = lpcDSCBufferDesc->dwBufferBytes;
750 TRACE("desired buflen=%ld, old buffer=%p\n", buflen, ipDSC->buffer);
751 newbuf = (LPBYTE)HeapReAlloc(GetProcessHeap(),0,ipDSC->buffer,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 */
757 } else {
758 ipDSC->buffer = newbuf;
759 ipDSC->buflen = buflen;
764 TRACE("returning DS_OK\n");
765 return DS_OK;
768 static HRESULT WINAPI
769 IDirectSoundCaptureBufferImpl_QueryInterface(
770 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
771 REFIID riid,
772 LPVOID* ppobj )
774 ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
775 TRACE( "(%p,%s,%p)\n", This, debugstr_guid(riid), ppobj );
777 if (ppobj == NULL) {
778 WARN("invalid parameter\n");
779 return E_INVALIDARG;
782 *ppobj = NULL;
784 if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ||
785 IsEqualGUID( &IID_IDirectSoundNotify8, riid ) ) {
786 if (!This->notify) {
787 This->notify = (IDirectSoundNotifyImpl*)HeapAlloc(GetProcessHeap(),
788 HEAP_ZERO_MEMORY, sizeof(*This->notify));
789 if (This->notify) {
790 This->notify->ref = 0; /* release when ref = -1 */
791 This->notify->lpVtbl = &dsnvt;
794 if (This->notify) {
795 if (This->dsound->hwbuf) {
796 HRESULT err;
798 err = IDsCaptureDriverBuffer_QueryInterface(This->dsound->hwbuf,
799 &IID_IDsDriverNotify, (LPVOID*)&(This->notify->hwnotify));
800 if (err != DS_OK) {
801 WARN("IDsCaptureDriverBuffer_QueryInterface failed\n");
802 *ppobj = 0;
803 return err;
807 IDirectSoundNotify_AddRef((LPDIRECTSOUNDNOTIFY)This->notify);
808 *ppobj = (LPVOID)This->notify;
809 return DS_OK;
812 *ppobj = 0;
813 WARN("IID_IDirectSoundNotify\n");
814 return E_FAIL;
817 if ( IsEqualGUID( &IID_IDirectSoundCaptureBuffer, riid ) ||
818 IsEqualGUID( &IID_IDirectSoundCaptureBuffer8, riid ) ) {
819 IDirectSoundCaptureBuffer8_AddRef(iface);
820 *ppobj = This;
821 return NO_ERROR;
824 FIXME("(%p,%s,%p) unsupported GUID\n", This, debugstr_guid(riid), ppobj);
826 *ppobj = 0;
828 return E_NOINTERFACE;
831 static ULONG WINAPI
832 IDirectSoundCaptureBufferImpl_AddRef( LPDIRECTSOUNDCAPTUREBUFFER8 iface )
834 ULONG uRef;
835 ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
836 TRACE( "(%p)\n", This );
838 assert(This->dsound);
840 EnterCriticalSection( &(This->dsound->lock) );
842 TRACE( "(%p) was 0x%08lx\n", This, This->ref );
843 uRef = ++(This->ref);
845 LeaveCriticalSection( &(This->dsound->lock) );
847 return uRef;
850 static ULONG WINAPI
851 IDirectSoundCaptureBufferImpl_Release( LPDIRECTSOUNDCAPTUREBUFFER8 iface )
853 ULONG uRef;
854 ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
855 TRACE( "(%p)\n", This );
857 assert(This->dsound);
859 EnterCriticalSection( &(This->dsound->lock) );
861 TRACE( "(%p) was 0x%08lx\n", This, This->ref );
862 uRef = --(This->ref);
864 LeaveCriticalSection( &(This->dsound->lock) );
866 if ( uRef == 0 ) {
867 TRACE("deleting object\n");
868 if (This->pdscbd)
869 HeapFree(GetProcessHeap(),0, This->pdscbd);
871 if (This->dsound->hwi) {
872 waveInReset(This->dsound->hwi);
873 waveInClose(This->dsound->hwi);
874 if (This->dsound->pwave) {
875 HeapFree(GetProcessHeap(),0, This->dsound->pwave);
876 This->dsound->pwave = 0;
878 This->dsound->hwi = 0;
881 if (This->dsound->hwbuf)
882 IDsCaptureDriverBuffer_Release(This->dsound->hwbuf);
884 /* remove from IDirectSoundCaptureImpl */
885 if (This->dsound)
886 This->dsound->capture_buffer = NULL;
887 else
888 ERR("does not reference dsound\n");
890 if (This->notify)
891 IDirectSoundNotify_Release((LPDIRECTSOUNDNOTIFY)This->notify);
893 HeapFree( GetProcessHeap(), 0, This );
896 TRACE( "returning 0x%08lx\n", uRef );
897 return uRef;
900 static HRESULT WINAPI
901 IDirectSoundCaptureBufferImpl_GetCaps(
902 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
903 LPDSCBCAPS lpDSCBCaps )
905 ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
906 TRACE( "(%p,%p)\n", This, lpDSCBCaps );
908 if (This == NULL) {
909 WARN("invalid parameter: This == NULL\n");
910 return DSERR_INVALIDPARAM;
913 if (lpDSCBCaps == NULL) {
914 WARN("invalid parameter: lpDSCBCaps == NULL\n");
915 return DSERR_INVALIDPARAM;
918 if (lpDSCBCaps->dwSize < sizeof(DSCBCAPS)) {
919 WARN("invalid parameter: lpDSCBCaps->dwSize = %ld < %d\n",
920 lpDSCBCaps->dwSize, sizeof(DSCBCAPS));
921 return DSERR_INVALIDPARAM;
924 if (This->dsound == NULL) {
925 WARN("invalid parameter: This->dsound == NULL\n");
926 return DSERR_INVALIDPARAM;
929 lpDSCBCaps->dwSize = sizeof(DSCBCAPS);
930 lpDSCBCaps->dwFlags = This->flags;
931 lpDSCBCaps->dwBufferBytes = This->pdscbd->dwBufferBytes;
932 lpDSCBCaps->dwReserved = 0;
934 TRACE("returning DS_OK\n");
935 return DS_OK;
938 static HRESULT WINAPI
939 IDirectSoundCaptureBufferImpl_GetCurrentPosition(
940 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
941 LPDWORD lpdwCapturePosition,
942 LPDWORD lpdwReadPosition )
944 ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
945 TRACE( "(%p,%p,%p)\n", This, lpdwCapturePosition, lpdwReadPosition );
947 if (This == NULL) {
948 WARN("invalid parameter: This == NULL\n");
949 return DSERR_INVALIDPARAM;
952 if (This->dsound == NULL) {
953 WARN("invalid parameter: This->dsound == NULL\n");
954 return DSERR_INVALIDPARAM;
957 if (This->dsound->driver) {
958 HRESULT hres;
959 hres = IDsCaptureDriverBuffer_GetPosition(This->dsound->hwbuf, lpdwCapturePosition, lpdwReadPosition );
960 if (hres != DS_OK) {
961 WARN("IDsCaptureDriverBuffer_GetPosition failed\n");
962 return hres;
964 } else if (This->dsound->hwi) {
965 EnterCriticalSection(&(This->dsound->lock));
966 TRACE("old This->dsound->state=%ld\n",This->dsound->state);
967 if (lpdwCapturePosition) {
968 MMTIME mtime;
969 mtime.wType = TIME_BYTES;
970 waveInGetPosition(This->dsound->hwi, &mtime, sizeof(mtime));
971 TRACE("mtime.u.cb=%ld,This->dsound->buflen=%ld\n", mtime.u.cb,
972 This->dsound->buflen);
973 mtime.u.cb = mtime.u.cb % This->dsound->buflen;
974 *lpdwCapturePosition = mtime.u.cb;
977 if (lpdwReadPosition) {
978 if (This->dsound->state == STATE_STARTING) {
979 if (lpdwCapturePosition)
980 This->dsound->read_position = *lpdwCapturePosition;
981 This->dsound->state = STATE_CAPTURING;
983 *lpdwReadPosition = This->dsound->read_position;
985 TRACE("new This->dsound->state=%ld\n",This->dsound->state);
986 LeaveCriticalSection(&(This->dsound->lock));
987 if (lpdwCapturePosition) TRACE("*lpdwCapturePosition=%ld\n",*lpdwCapturePosition);
988 if (lpdwReadPosition) TRACE("*lpdwReadPosition=%ld\n",*lpdwReadPosition);
989 } else {
990 WARN("no driver\n");
991 return DSERR_NODRIVER;
994 TRACE("returning DS_OK\n");
995 return DS_OK;
998 static HRESULT WINAPI
999 IDirectSoundCaptureBufferImpl_GetFormat(
1000 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
1001 LPWAVEFORMATEX lpwfxFormat,
1002 DWORD dwSizeAllocated,
1003 LPDWORD lpdwSizeWritten )
1005 ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
1006 TRACE( "(%p,%p,0x%08lx,%p)\n", This, lpwfxFormat, dwSizeAllocated,
1007 lpdwSizeWritten );
1009 if (This == NULL) {
1010 WARN("invalid parameter: This == NULL\n");
1011 return DSERR_INVALIDPARAM;
1014 if (This->dsound == NULL) {
1015 WARN("invalid parameter: This->dsound == NULL\n");
1016 return DSERR_INVALIDPARAM;
1019 /* FIXME: use real size for extended formats someday */
1020 if (dwSizeAllocated > sizeof(This->dsound->wfx))
1021 dwSizeAllocated = sizeof(This->dsound->wfx);
1022 if (lpwfxFormat) { /* NULL is valid (just want size) */
1023 memcpy(lpwfxFormat,&(This->dsound->wfx),dwSizeAllocated);
1024 if (lpdwSizeWritten)
1025 *lpdwSizeWritten = dwSizeAllocated;
1026 } else {
1027 if (lpdwSizeWritten)
1028 *lpdwSizeWritten = sizeof(This->dsound->wfx);
1029 else {
1030 TRACE("invalid parameter: lpdwSizeWritten = NULL\n");
1031 return DSERR_INVALIDPARAM;
1035 TRACE("returning DS_OK\n");
1036 return DS_OK;
1039 static HRESULT WINAPI
1040 IDirectSoundCaptureBufferImpl_GetStatus(
1041 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
1042 LPDWORD lpdwStatus )
1044 ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
1045 TRACE( "(%p, %p), thread is %04lx\n", This, lpdwStatus, GetCurrentThreadId() );
1047 if (This == NULL) {
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 (lpdwStatus == NULL) {
1058 WARN("invalid parameter: lpdwStatus == NULL\n");
1059 return DSERR_INVALIDPARAM;
1062 *lpdwStatus = 0;
1063 EnterCriticalSection(&(This->dsound->lock));
1065 TRACE("old This->dsound->state=%ld, old lpdwStatus=%08lx\n",This->dsound->state,*lpdwStatus);
1066 if ((This->dsound->state == STATE_STARTING) ||
1067 (This->dsound->state == STATE_CAPTURING)) {
1068 *lpdwStatus |= DSCBSTATUS_CAPTURING;
1069 if (This->flags & DSCBSTART_LOOPING)
1070 *lpdwStatus |= DSCBSTATUS_LOOPING;
1072 TRACE("new This->dsound->state=%ld, new lpdwStatus=%08lx\n",This->dsound->state,*lpdwStatus);
1073 LeaveCriticalSection(&(This->dsound->lock));
1075 TRACE("status=%lx\n", *lpdwStatus);
1076 TRACE("returning DS_OK\n");
1077 return DS_OK;
1080 static HRESULT WINAPI
1081 IDirectSoundCaptureBufferImpl_Initialize(
1082 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
1083 LPDIRECTSOUNDCAPTURE lpDSC,
1084 LPCDSCBUFFERDESC lpcDSCBDesc )
1086 ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
1088 FIXME( "(%p,%p,%p): stub\n", This, lpDSC, lpcDSCBDesc );
1090 return DS_OK;
1093 static HRESULT WINAPI
1094 IDirectSoundCaptureBufferImpl_Lock(
1095 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
1096 DWORD dwReadCusor,
1097 DWORD dwReadBytes,
1098 LPVOID* lplpvAudioPtr1,
1099 LPDWORD lpdwAudioBytes1,
1100 LPVOID* lplpvAudioPtr2,
1101 LPDWORD lpdwAudioBytes2,
1102 DWORD dwFlags )
1104 HRESULT err = DS_OK;
1105 ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
1106 TRACE( "(%p,%08lu,%08lu,%p,%p,%p,%p,0x%08lx) at %ld\n", This, dwReadCusor,
1107 dwReadBytes, lplpvAudioPtr1, lpdwAudioBytes1, lplpvAudioPtr2,
1108 lpdwAudioBytes2, dwFlags, GetTickCount() );
1110 if (This == NULL) {
1111 WARN("invalid parameter: This == NULL\n");
1112 return DSERR_INVALIDPARAM;
1115 if (This->dsound == NULL) {
1116 WARN("invalid parameter: This->dsound == NULL\n");
1117 return DSERR_INVALIDPARAM;
1120 if (lplpvAudioPtr1 == NULL) {
1121 WARN("invalid parameter: lplpvAudioPtr1 == NULL\n");
1122 return DSERR_INVALIDPARAM;
1125 if (lpdwAudioBytes1 == NULL) {
1126 WARN("invalid parameter: lpdwAudioBytes1 == NULL\n");
1127 return DSERR_INVALIDPARAM;
1130 EnterCriticalSection(&(This->dsound->lock));
1132 if (This->dsound->driver) {
1133 err = IDsCaptureDriverBuffer_Lock(This->dsound->hwbuf, lplpvAudioPtr1,
1134 lpdwAudioBytes1, lplpvAudioPtr2, lpdwAudioBytes2,
1135 dwReadCusor, dwReadBytes, dwFlags);
1136 if (err != DS_OK)
1137 WARN("IDsCaptureDriverBuffer_Lock failed\n");
1138 } else if (This->dsound->hwi) {
1139 *lplpvAudioPtr1 = This->dsound->buffer + dwReadCusor;
1140 if ( (dwReadCusor + dwReadBytes) > This->dsound->buflen) {
1141 *lpdwAudioBytes1 = This->dsound->buflen - dwReadCusor;
1142 if (lplpvAudioPtr2)
1143 *lplpvAudioPtr2 = This->dsound->buffer;
1144 if (lpdwAudioBytes2)
1145 *lpdwAudioBytes2 = dwReadBytes - *lpdwAudioBytes1;
1146 } else {
1147 *lpdwAudioBytes1 = dwReadBytes;
1148 if (lplpvAudioPtr2)
1149 *lplpvAudioPtr2 = 0;
1150 if (lpdwAudioBytes2)
1151 *lpdwAudioBytes2 = 0;
1153 } else {
1154 TRACE("invalid call\n");
1155 err = DSERR_INVALIDCALL; /* DSERR_NODRIVER ? */
1158 LeaveCriticalSection(&(This->dsound->lock));
1160 return err;
1163 static HRESULT WINAPI
1164 IDirectSoundCaptureBufferImpl_Start(
1165 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
1166 DWORD dwFlags )
1168 HRESULT err = DS_OK;
1169 ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
1170 TRACE( "(%p,0x%08lx)\n", This, dwFlags );
1172 if (This == NULL) {
1173 WARN("invalid parameter: This == NULL\n");
1174 return DSERR_INVALIDPARAM;
1177 if (This->dsound == NULL) {
1178 WARN("invalid parameter: This->dsound == NULL\n");
1179 return DSERR_INVALIDPARAM;
1182 if ( (This->dsound->driver == 0) && (This->dsound->hwi == 0) ) {
1183 WARN("no driver\n");
1184 return DSERR_NODRIVER;
1187 EnterCriticalSection(&(This->dsound->lock));
1189 This->flags = dwFlags;
1190 TRACE("old This->dsound->state=%ld\n",This->dsound->state);
1191 if (This->dsound->state == STATE_STOPPED)
1192 This->dsound->state = STATE_STARTING;
1193 else if (This->dsound->state == STATE_STOPPING)
1194 This->dsound->state = STATE_CAPTURING;
1195 TRACE("new This->dsound->state=%ld\n",This->dsound->state);
1197 LeaveCriticalSection(&(This->dsound->lock));
1199 if (This->dsound->driver) {
1200 err = IDsCaptureDriverBuffer_Start(This->dsound->hwbuf, dwFlags);
1201 if (err != DS_OK)
1202 WARN("IDsCaptureDriverBuffer_Start failed\n");
1203 return err;
1204 } else {
1205 IDirectSoundCaptureImpl* ipDSC = This->dsound;
1207 if (ipDSC->buffer) {
1208 if (This->notify && This->notify->nrofnotifies) {
1209 unsigned c;
1211 ipDSC->nrofpwaves = This->notify->nrofnotifies;
1213 /* prepare headers */
1214 ipDSC->pwave = HeapReAlloc(GetProcessHeap(),0,ipDSC->pwave,
1215 ipDSC->nrofpwaves*sizeof(WAVEHDR));
1217 for (c = 0; c < ipDSC->nrofpwaves; c++) {
1218 if (c == 0) {
1219 ipDSC->pwave[0].lpData = ipDSC->buffer;
1220 ipDSC->pwave[0].dwBufferLength =
1221 This->notify->notifies[0].dwOffset + 1;
1222 } else {
1223 ipDSC->pwave[c].lpData = ipDSC->buffer +
1224 This->notify->notifies[c-1].dwOffset + 1;
1225 ipDSC->pwave[c].dwBufferLength =
1226 This->notify->notifies[c].dwOffset -
1227 This->notify->notifies[c-1].dwOffset;
1229 ipDSC->pwave[c].dwUser = (DWORD)ipDSC;
1230 ipDSC->pwave[c].dwFlags = 0;
1231 ipDSC->pwave[c].dwLoops = 0;
1232 err = mmErr(waveInPrepareHeader(ipDSC->hwi,
1233 &(ipDSC->pwave[c]),sizeof(WAVEHDR)));
1234 if (err != DS_OK) {
1235 while (c--)
1236 waveInUnprepareHeader(ipDSC->hwi,
1237 &(ipDSC->pwave[c]),sizeof(WAVEHDR));
1238 break;
1242 memset(ipDSC->buffer,
1243 (ipDSC->wfx.wBitsPerSample == 16) ? 0 : 128, ipDSC->buflen);
1244 } else {
1245 TRACE("no notifiers specified\n");
1246 /* no notifiers specified so just create a single default header */
1247 ipDSC->nrofpwaves = 1;
1248 ipDSC->pwave = HeapReAlloc(GetProcessHeap(),0,ipDSC->pwave,sizeof(WAVEHDR));
1249 ipDSC->pwave[0].lpData = ipDSC->buffer;
1250 ipDSC->pwave[0].dwBufferLength = ipDSC->buflen;
1251 ipDSC->pwave[0].dwUser = (DWORD)ipDSC;
1252 ipDSC->pwave[0].dwFlags = 0;
1253 ipDSC->pwave[0].dwLoops = 0;
1255 err = mmErr(waveInPrepareHeader(ipDSC->hwi,
1256 &(ipDSC->pwave[0]),sizeof(WAVEHDR)));
1257 if (err != DS_OK) {
1258 WARN("waveInPrepareHeader failed\n");
1259 waveInUnprepareHeader(ipDSC->hwi,
1260 &(ipDSC->pwave[0]),sizeof(WAVEHDR));
1265 ipDSC->index = 0;
1266 ipDSC->read_position = 0;
1268 if (err == DS_OK) {
1269 err = mmErr(waveInReset(ipDSC->hwi));
1270 if (err == DS_OK) {
1271 /* add the first buffer to the queue */
1272 err = mmErr(waveInAddBuffer(ipDSC->hwi, &(ipDSC->pwave[0]), sizeof(WAVEHDR)));
1273 if (err == DS_OK) {
1274 /* start filling the first buffer */
1275 err = mmErr(waveInStart(ipDSC->hwi));
1276 } else
1277 WARN("waveInAddBuffer failed\n");
1278 } else
1279 WARN("waveInReset failed\n");
1283 if (err != DS_OK) {
1284 WARN("calling waveInClose because of error\n");
1285 waveInClose(This->dsound->hwi);
1286 This->dsound->hwi = 0;
1289 TRACE("returning %ld\n", err);
1290 return err;
1293 static HRESULT WINAPI
1294 IDirectSoundCaptureBufferImpl_Stop( LPDIRECTSOUNDCAPTUREBUFFER8 iface )
1296 HRESULT err = DS_OK;
1297 ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
1298 TRACE( "(%p)\n", This );
1300 if (This == NULL) {
1301 WARN("invalid parameter: This == NULL\n");
1302 return DSERR_INVALIDPARAM;
1305 if (This->dsound == NULL) {
1306 WARN("invalid parameter: This->dsound == NULL\n");
1307 return DSERR_INVALIDPARAM;
1310 EnterCriticalSection(&(This->dsound->lock));
1312 TRACE("old This->dsound->state=%ld\n",This->dsound->state);
1313 if (This->dsound->state == STATE_CAPTURING)
1314 This->dsound->state = STATE_STOPPING;
1315 else if (This->dsound->state == STATE_STARTING)
1316 This->dsound->state = STATE_STOPPED;
1317 TRACE("new This->dsound->state=%ld\n",This->dsound->state);
1319 LeaveCriticalSection(&(This->dsound->lock));
1321 if (This->dsound->driver) {
1322 err = IDsCaptureDriverBuffer_Stop(This->dsound->hwbuf);
1323 if (err == DSERR_BUFFERLOST) {
1324 /* Wine-only: the driver wants us to reopen the device */
1325 IDsCaptureDriverBuffer_Release(This->dsound->hwbuf);
1326 err = IDsCaptureDriver_CreateCaptureBuffer(This->dsound->driver,
1327 &(This->dsound->wfx),0,0,&(This->dsound->buflen),&(This->dsound->buffer),
1328 (LPVOID*)&(This->dsound->hwbuf));
1329 if (err != DS_OK) {
1330 WARN("IDsCaptureDriver_CreateCaptureBuffer failed\n");
1331 This->dsound->hwbuf = 0;
1333 } else if (err != DS_OK)
1334 WARN("IDsCaptureDriverBuffer_Stop failed\n");
1335 } else if (This->dsound->hwi) {
1336 err = waveInStop(This->dsound->hwi);
1337 } else {
1338 WARN("no driver\n");
1339 err = DSERR_NODRIVER;
1342 TRACE( "(%p) returning 0x%08lx\n", This,err);
1343 return err;
1346 static HRESULT WINAPI
1347 IDirectSoundCaptureBufferImpl_Unlock(
1348 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
1349 LPVOID lpvAudioPtr1,
1350 DWORD dwAudioBytes1,
1351 LPVOID lpvAudioPtr2,
1352 DWORD dwAudioBytes2 )
1354 ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
1355 TRACE( "(%p,%p,%08lu,%p,%08lu)\n", This, lpvAudioPtr1, dwAudioBytes1,
1356 lpvAudioPtr2, dwAudioBytes2 );
1358 if (This == NULL) {
1359 WARN("invalid parameter: This == NULL\n");
1360 return DSERR_INVALIDPARAM;
1363 if (lpvAudioPtr1 == NULL) {
1364 WARN("invalid parameter: lpvAudioPtr1 == NULL\n");
1365 return DSERR_INVALIDPARAM;
1368 if (This->dsound->driver) {
1369 HRESULT hres;
1370 hres = IDsCaptureDriverBuffer_Unlock(This->dsound->hwbuf, lpvAudioPtr1,
1371 dwAudioBytes1, lpvAudioPtr2, dwAudioBytes2);
1372 if (hres != DS_OK)
1373 WARN("IDsCaptureDriverBuffer_Unlock failed\n");
1374 return hres;
1375 } else if (This->dsound->hwi) {
1376 This->dsound->read_position = (This->dsound->read_position +
1377 (dwAudioBytes1 + dwAudioBytes2)) % This->dsound->buflen;
1378 } else {
1379 WARN("invalid call\n");
1380 return DSERR_INVALIDCALL;
1383 return DS_OK;
1386 static HRESULT WINAPI
1387 IDirectSoundCaptureBufferImpl_GetObjectInPath(
1388 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
1389 REFGUID rguidObject,
1390 DWORD dwIndex,
1391 REFGUID rguidInterface,
1392 LPVOID* ppObject )
1394 ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
1396 FIXME( "(%p,%s,%lu,%s,%p): stub\n", This, debugstr_guid(rguidObject),
1397 dwIndex, debugstr_guid(rguidInterface), ppObject );
1399 return DS_OK;
1402 static HRESULT WINAPI
1403 IDirectSoundCaptureBufferImpl_GetFXStatus(
1404 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
1405 DWORD dwFXCount,
1406 LPDWORD pdwFXStatus )
1408 ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
1410 FIXME( "(%p,%lu,%p): stub\n", This, dwFXCount, pdwFXStatus );
1412 return DS_OK;
1415 static ICOM_VTABLE(IDirectSoundCaptureBuffer8) dscbvt =
1417 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1418 /* IUnknown methods */
1419 IDirectSoundCaptureBufferImpl_QueryInterface,
1420 IDirectSoundCaptureBufferImpl_AddRef,
1421 IDirectSoundCaptureBufferImpl_Release,
1423 /* IDirectSoundCaptureBuffer methods */
1424 IDirectSoundCaptureBufferImpl_GetCaps,
1425 IDirectSoundCaptureBufferImpl_GetCurrentPosition,
1426 IDirectSoundCaptureBufferImpl_GetFormat,
1427 IDirectSoundCaptureBufferImpl_GetStatus,
1428 IDirectSoundCaptureBufferImpl_Initialize,
1429 IDirectSoundCaptureBufferImpl_Lock,
1430 IDirectSoundCaptureBufferImpl_Start,
1431 IDirectSoundCaptureBufferImpl_Stop,
1432 IDirectSoundCaptureBufferImpl_Unlock,
1434 /* IDirectSoundCaptureBuffer methods */
1435 IDirectSoundCaptureBufferImpl_GetObjectInPath,
1436 IDirectSoundCaptureBufferImpl_GetFXStatus
1439 /*******************************************************************************
1440 * DirectSoundCapture ClassFactory
1443 static HRESULT WINAPI
1444 DSCCF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj)
1446 ICOM_THIS(IClassFactoryImpl,iface);
1448 FIXME("(%p)->(%s,%p),stub!\n",This,debugstr_guid(riid),ppobj);
1449 return E_NOINTERFACE;
1452 static ULONG WINAPI
1453 DSCCF_AddRef(LPCLASSFACTORY iface)
1455 ICOM_THIS(IClassFactoryImpl,iface);
1456 TRACE("(%p) ref was %ld\n", This, This->ref);
1457 return ++(This->ref);
1460 static ULONG WINAPI
1461 DSCCF_Release(LPCLASSFACTORY iface)
1463 ICOM_THIS(IClassFactoryImpl,iface);
1464 /* static class, won't be freed */
1465 TRACE("(%p) ref was %ld\n", This, This->ref);
1466 return --(This->ref);
1469 static HRESULT WINAPI
1470 DSCCF_CreateInstance(
1471 LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj )
1473 ICOM_THIS(IClassFactoryImpl,iface);
1474 TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
1476 if (ppobj == NULL) {
1477 WARN("invalid parameter\n");
1478 return E_INVALIDARG;
1481 *ppobj = NULL;
1483 if ( IsEqualGUID( &IID_IDirectSoundCapture, riid ) ||
1484 IsEqualGUID( &IID_IDirectSoundCapture8, riid ) ) {
1485 return DirectSoundCaptureCreate8(0,(LPDIRECTSOUNDCAPTURE8*)ppobj,pOuter);
1488 WARN("(%p,%p,%s,%p) Interface not found!\n",This,pOuter,debugstr_guid(riid),ppobj);
1489 return E_NOINTERFACE;
1492 static HRESULT WINAPI
1493 DSCCF_LockServer(LPCLASSFACTORY iface,BOOL dolock)
1495 ICOM_THIS(IClassFactoryImpl,iface);
1496 FIXME("(%p)->(%d),stub!\n",This,dolock);
1497 return S_OK;
1500 static ICOM_VTABLE(IClassFactory) DSCCF_Vtbl =
1502 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1503 DSCCF_QueryInterface,
1504 DSCCF_AddRef,
1505 DSCCF_Release,
1506 DSCCF_CreateInstance,
1507 DSCCF_LockServer
1510 IClassFactoryImpl DSOUND_CAPTURE_CF = { &DSCCF_Vtbl, 1 };
1512 /***************************************************************************
1513 * DirectSoundFullDuplexCreate8 [DSOUND.10]
1515 * Create and initialize a DirectSoundFullDuplex interface.
1517 * PARAMS
1518 * pcGuidCaptureDevice [I] Address of sound capture device GUID.
1519 * pcGuidRenderDevice [I] Address of sound render device GUID.
1520 * pcDSCBufferDesc [I] Address of capture buffer description.
1521 * pcDSBufferDesc [I] Address of render buffer description.
1522 * hWnd [I] Handle to application window.
1523 * dwLevel [I] Cooperative level.
1524 * ppDSFD [O] Address where full duplex interface returned.
1525 * ppDSCBuffer8 [0] Address where capture buffer interface returned.
1526 * ppDSBuffer8 [0] Address where render buffer interface returned.
1527 * pUnkOuter [I] Must be NULL.
1529 * RETURNS
1530 * Success: DS_OK
1531 * Failure: DSERR_NOAGGREGATION, DSERR_ALLOCATED, DSERR_INVALIDPARAM,
1532 * DSERR_OUTOFMEMORY DSERR_INVALIDCALL DSERR_NODRIVER
1534 HRESULT WINAPI
1535 DirectSoundFullDuplexCreate8(
1536 LPCGUID pcGuidCaptureDevice,
1537 LPCGUID pcGuidRenderDevice,
1538 LPCDSCBUFFERDESC pcDSCBufferDesc,
1539 LPCDSBUFFERDESC pcDSBufferDesc,
1540 HWND hWnd,
1541 DWORD dwLevel,
1542 LPDIRECTSOUNDFULLDUPLEX *ppDSFD,
1543 LPDIRECTSOUNDCAPTUREBUFFER8 *ppDSCBuffer8,
1544 LPDIRECTSOUNDBUFFER8 *ppDSBuffer8,
1545 LPUNKNOWN pUnkOuter)
1547 IDirectSoundFullDuplexImpl** ippDSFD=(IDirectSoundFullDuplexImpl**)ppDSFD;
1548 TRACE("(%s,%s,%p,%p,%lx,%lx,%p,%p,%p,%p)\n", debugstr_guid(pcGuidCaptureDevice),
1549 debugstr_guid(pcGuidRenderDevice), pcDSCBufferDesc, pcDSBufferDesc,
1550 (DWORD)hWnd, dwLevel, ppDSFD, ppDSCBuffer8, ppDSBuffer8, pUnkOuter);
1552 if ( pUnkOuter ) {
1553 WARN("pUnkOuter != 0\n");
1554 return DSERR_NOAGGREGATION;
1557 *ippDSFD = (IDirectSoundFullDuplexImpl*)HeapAlloc(GetProcessHeap(),
1558 HEAP_ZERO_MEMORY, sizeof(IDirectSoundFullDuplexImpl));
1560 if (*ippDSFD == NULL) {
1561 WARN("out of memory\n");
1562 return DSERR_OUTOFMEMORY;
1563 } else {
1564 HRESULT hres;
1565 ICOM_THIS(IDirectSoundFullDuplexImpl, *ippDSFD);
1567 This->ref = 1;
1568 This->lpVtbl = &dsfdvt;
1570 InitializeCriticalSection( &(This->lock) );
1572 hres = IDirectSoundFullDuplexImpl_Initialize( (LPDIRECTSOUNDFULLDUPLEX)This,
1573 pcGuidCaptureDevice, pcGuidRenderDevice,
1574 pcDSCBufferDesc, pcDSBufferDesc,
1575 hWnd, dwLevel, ppDSCBuffer8, ppDSBuffer8);
1576 if (hres != DS_OK)
1577 WARN("IDirectSoundFullDuplexImpl_Initialize failed\n");
1578 return hres;
1581 return DSERR_GENERIC;
1584 static HRESULT WINAPI
1585 IDirectSoundFullDuplexImpl_QueryInterface(
1586 LPDIRECTSOUNDFULLDUPLEX iface,
1587 REFIID riid,
1588 LPVOID* ppobj )
1590 ICOM_THIS(IDirectSoundFullDuplexImpl,iface);
1591 TRACE( "(%p,%s,%p)\n", This, debugstr_guid(riid), ppobj );
1593 if (ppobj == NULL) {
1594 WARN("invalid parameter\n");
1595 return E_INVALIDARG;
1598 *ppobj = NULL;
1599 return E_NOINTERFACE;
1602 static ULONG WINAPI
1603 IDirectSoundFullDuplexImpl_AddRef( LPDIRECTSOUNDFULLDUPLEX iface )
1605 ULONG uRef;
1606 ICOM_THIS(IDirectSoundFullDuplexImpl,iface);
1608 EnterCriticalSection( &(This->lock) );
1610 TRACE( "(%p) was 0x%08lx\n", This, This->ref );
1611 uRef = ++(This->ref);
1613 LeaveCriticalSection( &(This->lock) );
1615 return uRef;
1618 static ULONG WINAPI
1619 IDirectSoundFullDuplexImpl_Release( LPDIRECTSOUNDFULLDUPLEX iface )
1621 ULONG uRef;
1622 ICOM_THIS(IDirectSoundFullDuplexImpl,iface);
1624 EnterCriticalSection( &(This->lock) );
1626 TRACE( "(%p) was 0x%08lx\n", This, This->ref );
1627 uRef = --(This->ref);
1629 LeaveCriticalSection( &(This->lock) );
1631 if ( uRef == 0 ) {
1632 TRACE("deleting object\n");
1633 DeleteCriticalSection( &(This->lock) );
1634 HeapFree( GetProcessHeap(), 0, This );
1637 return uRef;
1640 static HRESULT WINAPI
1641 IDirectSoundFullDuplexImpl_Initialize(
1642 LPDIRECTSOUNDFULLDUPLEX iface,
1643 LPCGUID pCaptureGuid,
1644 LPCGUID pRendererGuid,
1645 LPCDSCBUFFERDESC lpDscBufferDesc,
1646 LPCDSBUFFERDESC lpDsBufferDesc,
1647 HWND hWnd,
1648 DWORD dwLevel,
1649 LPLPDIRECTSOUNDCAPTUREBUFFER8 lplpDirectSoundCaptureBuffer8,
1650 LPLPDIRECTSOUNDBUFFER8 lplpDirectSoundBuffer8 )
1652 ICOM_THIS(IDirectSoundFullDuplexImpl,iface);
1653 IDirectSoundCaptureBufferImpl** ippdscb=(IDirectSoundCaptureBufferImpl**)lplpDirectSoundCaptureBuffer8;
1654 IDirectSoundBufferImpl** ippdsc=(IDirectSoundBufferImpl**)lplpDirectSoundBuffer8;
1656 FIXME( "(%p,%s,%s,%p,%p,%lx,%lx,%p,%p) stub!\n", This, debugstr_guid(pCaptureGuid),
1657 debugstr_guid(pRendererGuid), lpDscBufferDesc, lpDsBufferDesc, (DWORD)hWnd, dwLevel,
1658 ippdscb, ippdsc);
1660 return E_FAIL;
1663 static ICOM_VTABLE(IDirectSoundFullDuplex) dsfdvt =
1665 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1666 /* IUnknown methods */
1667 IDirectSoundFullDuplexImpl_QueryInterface,
1668 IDirectSoundFullDuplexImpl_AddRef,
1669 IDirectSoundFullDuplexImpl_Release,
1671 /* IDirectSoundFullDuplex methods */
1672 IDirectSoundFullDuplexImpl_Initialize
1675 /*******************************************************************************
1676 * DirectSoundFullDuplex ClassFactory
1679 static HRESULT WINAPI
1680 DSFDCF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj)
1682 ICOM_THIS(IClassFactoryImpl,iface);
1684 FIXME("(%p)->(%s,%p),stub!\n",This,debugstr_guid(riid),ppobj);
1685 return E_NOINTERFACE;
1688 static ULONG WINAPI
1689 DSFDCF_AddRef(LPCLASSFACTORY iface)
1691 ICOM_THIS(IClassFactoryImpl,iface);
1692 TRACE("(%p) ref was %ld\n", This, This->ref);
1693 return ++(This->ref);
1696 static ULONG WINAPI
1697 DSFDCF_Release(LPCLASSFACTORY iface)
1699 ICOM_THIS(IClassFactoryImpl,iface);
1700 /* static class, won't be freed */
1701 TRACE("(%p) ref was %ld\n", This, This->ref);
1702 return --(This->ref);
1705 static HRESULT WINAPI
1706 DSFDCF_CreateInstance(
1707 LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj )
1709 ICOM_THIS(IClassFactoryImpl,iface);
1711 TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
1713 if (ppobj == NULL) {
1714 WARN("invalid parameter\n");
1715 return E_INVALIDARG;
1718 *ppobj = NULL;
1720 if ( IsEqualGUID( &IID_IDirectSoundFullDuplex, riid ) ) {
1721 /* FIXME: how do we do this one ? */
1722 FIXME("not implemented\n");
1723 return E_NOINTERFACE;
1726 WARN("(%p,%p,%s,%p) Interface not found!\n",This,pOuter,debugstr_guid(riid),ppobj);
1727 return E_NOINTERFACE;
1730 static HRESULT WINAPI
1731 DSFDCF_LockServer(LPCLASSFACTORY iface,BOOL dolock)
1733 ICOM_THIS(IClassFactoryImpl,iface);
1734 FIXME("(%p)->(%d),stub!\n",This,dolock);
1735 return S_OK;
1738 static ICOM_VTABLE(IClassFactory) DSFDCF_Vtbl =
1740 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1741 DSFDCF_QueryInterface,
1742 DSFDCF_AddRef,
1743 DSFDCF_Release,
1744 DSFDCF_CreateInstance,
1745 DSFDCF_LockServer
1748 IClassFactoryImpl DSOUND_FULLDUPLEX_CF = { &DSFDCF_Vtbl, 1 };