Added stubs for AccessCheckByType, AddAuditAccessAce,
[wine] / dlls / dsound / capture.c
1 /*              DirectSoundCapture
2  *
3  * Copyright 1998 Marcus Meissner
4  * Copyright 1998 Rob Riggs
5  * Copyright 2000-2001 TransGaming Technologies, Inc.
6  *
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.
11  *
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.
16  *
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
20  */
21 /*
22  * TODO:
23  *      Implement DirectSoundFullDuplex support.
24  *      Implement FX support.
25  */
26
27 #include "config.h"
28 #include <assert.h>
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include <sys/types.h>
32 #include <sys/fcntl.h>
33 #ifdef HAVE_UNISTD_H
34 # include <unistd.h>
35 #endif
36 #include <stdlib.h>
37 #include <string.h>
38
39 #include "windef.h"
40 #include "winbase.h"
41 #include "wingdi.h"
42 #include "winuser.h"
43 #include "winerror.h"
44 #include "mmsystem.h"
45 #include "mmddk.h"
46 #include "winreg.h"
47 #include "winternl.h"
48 #include "winnls.h"
49 #include "wine/debug.h"
50 #include "dsound.h"
51 #include "dsdriver.h"
52 #include "dsound_private.h"
53
54 WINE_DEFAULT_DEBUG_CHANNEL(dsound);
55
56 static HRESULT WINAPI IDirectSoundCaptureImpl_Initialize(
57     LPDIRECTSOUNDCAPTURE iface,
58     LPCGUID lpcGUID );
59 static ULONG WINAPI IDirectSoundCaptureImpl_Release( 
60     LPDIRECTSOUNDCAPTURE iface );
61 static ULONG WINAPI IDirectSoundCaptureBufferImpl_Release( 
62     LPDIRECTSOUNDCAPTUREBUFFER8 iface );
63 static HRESULT DSOUND_CreateDirectSoundCaptureBuffer(
64     IDirectSoundCaptureImpl *ipDSC, 
65     LPCDSCBUFFERDESC lpcDSCBufferDesc, 
66     LPVOID* ppobj );
67 static HRESULT WINAPI IDirectSoundFullDuplexImpl_Initialize(
68     LPDIRECTSOUNDFULLDUPLEX iface,
69     LPCGUID pCaptureGuid,
70     LPCGUID pRendererGuid,
71     LPCDSCBUFFERDESC lpDscBufferDesc,
72     LPCDSBUFFERDESC lpDsBufferDesc,
73     HWND hWnd,
74     DWORD dwLevel,
75     LPLPDIRECTSOUNDCAPTUREBUFFER8 lplpDirectSoundCaptureBuffer8,
76     LPLPDIRECTSOUNDBUFFER8 lplpDirectSoundBuffer8 );
77
78 static ICOM_VTABLE(IDirectSoundCapture) dscvt;
79 static ICOM_VTABLE(IDirectSoundCaptureBuffer8) dscbvt;
80 static ICOM_VTABLE(IDirectSoundFullDuplex) dsfdvt;
81
82 static IDirectSoundCaptureImpl*       dsound_capture = NULL;
83
84 /***************************************************************************
85  * DirectSoundCaptureCreate [DSOUND.6]
86  *
87  * Create and initialize a DirectSoundCapture interface.
88  *
89  * PARAMS
90  *    lpcGUID   [I] Address of the GUID that identifies the sound capture device.
91  *    lplpDSC   [O] Address of a variable to receive the interface pointer.
92  *    pUnkOuter [I] Must be NULL.
93  *
94  * RETURNS
95  *    Success: DS_OK
96  *    Failure: DSERR_NOAGGREGATION, DSERR_ALLOCATED, DSERR_INVALIDPARAM,
97  *             DSERR_OUTOFMEMORY
98  *
99  * NOTES
100  *    lpcGUID must be one of the values returned from DirectSoundCaptureEnumerate
101  *    or NULL for the default device or DSDEVID_DefaultCapture or 
102  *    DSDEVID_DefaultVoiceCapture.
103  *
104  *    DSERR_ALLOCATED is returned for sound devices that do not support full duplex.
105  */
106 HRESULT WINAPI 
107 DirectSoundCaptureCreate8(
108     LPCGUID lpcGUID,
109     LPDIRECTSOUNDCAPTURE* lplpDSC,
110     LPUNKNOWN pUnkOuter )
111 {
112     IDirectSoundCaptureImpl** ippDSC=(IDirectSoundCaptureImpl**)lplpDSC;
113     TRACE("(%s,%p,%p)\n", debugstr_guid(lpcGUID), lplpDSC, pUnkOuter);
114
115     if ( pUnkOuter ) {
116         WARN("invalid parameter: pUnkOuter != NULL\n");
117         return DSERR_NOAGGREGATION;
118     }
119
120     if ( !lplpDSC ) {
121         WARN("invalid parameter: lplpDSC == NULL\n");
122         return DSERR_INVALIDPARAM;
123     }
124
125     /* Default device? */
126     if ( !lpcGUID || IsEqualGUID(lpcGUID, &GUID_NULL) ) 
127         lpcGUID = &DSDEVID_DefaultCapture;
128
129     *ippDSC = (IDirectSoundCaptureImpl*)HeapAlloc(GetProcessHeap(),
130         HEAP_ZERO_MEMORY, sizeof(IDirectSoundCaptureImpl));
131
132     if (*ippDSC == NULL) {
133         WARN("out of memory\n");
134         return DSERR_OUTOFMEMORY;
135     } else {
136         ICOM_THIS(IDirectSoundCaptureImpl, *ippDSC);
137
138         This->ref = 1;
139         This->state = STATE_STOPPED;
140
141         InitializeCriticalSection( &(This->lock) );
142
143         This->lpVtbl = &dscvt;
144         dsound_capture = This;
145
146         if (GetDeviceID(lpcGUID, &This->guid) == DS_OK) {
147             HRESULT     hres; 
148             hres = IDirectSoundCaptureImpl_Initialize( (LPDIRECTSOUNDCAPTURE)This, &This->guid);
149             if (hres != DS_OK)
150                 WARN("IDirectSoundCaptureImpl_Initialize failed\n");
151             return hres;
152         }
153     }
154     WARN("invalid GUID: %s\n", debugstr_guid(lpcGUID));
155     return DSERR_INVALIDPARAM;
156 }
157
158 /***************************************************************************
159  * DirectSoundCaptureEnumerateA [DSOUND.7]
160  *
161  * Enumerate all DirectSound drivers installed in the system.
162  *
163  * PARAMS
164  *    lpDSEnumCallback  [I] Address of callback function.
165  *    lpContext         [I] Address of user defined context passed to callback function.
166  *
167  * RETURNS
168  *    Success: DS_OK
169  *    Failure: DSERR_INVALIDPARAM
170  */
171 HRESULT WINAPI 
172 DirectSoundCaptureEnumerateA(
173     LPDSENUMCALLBACKA lpDSEnumCallback,
174     LPVOID lpContext)
175 {
176     unsigned devs, wid;
177     DSDRIVERDESC desc;
178     GUID guid;
179     int err;
180
181     TRACE("(%p,%p)\n", lpDSEnumCallback, lpContext );
182
183     if (lpDSEnumCallback == NULL) {
184         WARN("invalid parameter: lpDSEnumCallback == NULL\n");
185         return DSERR_INVALIDPARAM;
186     }
187
188     devs = waveInGetNumDevs();
189     if (devs > 0) {
190         if (GetDeviceID(&DSDEVID_DefaultCapture, &guid) == DS_OK) {
191             GUID temp;
192             for (wid = 0; wid < devs; ++wid) {
193                 err = mmErr(waveInMessage((HWAVEIN)wid,DRV_QUERYDSOUNDGUID,(DWORD)&temp,0));
194                 if (err == DS_OK) {
195                     if (IsEqualGUID( &guid, &temp ) ) {
196                         err = mmErr(waveInMessage((HWAVEIN)wid,DRV_QUERYDSOUNDDESC,(DWORD)&desc,0));
197                         if (err == DS_OK) {
198                             TRACE("calling lpDSEnumCallback(NULL,\"%s\",\"%s\",%p)\n",
199                                 "Primary Sound Capture Driver",desc.szDrvName,lpContext);
200                             if (lpDSEnumCallback(NULL, "Primary Sound Capture Driver", desc.szDrvName, lpContext) == FALSE)
201                                 return DS_OK;
202                         }
203                     }
204                 }
205             }
206         }
207     }
208
209     for (wid = 0; wid < devs; ++wid) {
210         err = mmErr(waveInMessage((HWAVEIN)wid,DRV_QUERYDSOUNDDESC,(DWORD)&desc,0));
211         if (err == DS_OK) {
212             err = mmErr(waveInMessage((HWAVEIN)wid,DRV_QUERYDSOUNDGUID,(DWORD)&guid,0));
213             if (err == DS_OK) {
214                 TRACE("calling lpDSEnumCallback(%s,\"%s\",\"%s\",%p)\n",
215                     debugstr_guid(&guid),desc.szDesc,desc.szDrvName,lpContext);
216                 if (lpDSEnumCallback(&guid, desc.szDesc, desc.szDrvName, lpContext) == FALSE)
217                     return DS_OK;
218             }
219         } 
220     }
221
222     return DS_OK;
223 }
224
225 /***************************************************************************
226  * DirectSoundCaptureEnumerateW [DSOUND.8]
227  *
228  * Enumerate all DirectSound drivers installed in the system.
229  *
230  * PARAMS
231  *    lpDSEnumCallback  [I] Address of callback function.
232  *    lpContext         [I] Address of user defined context passed to callback function.
233  *
234  * RETURNS
235  *    Success: DS_OK
236  *    Failure: DSERR_INVALIDPARAM
237  */
238 HRESULT WINAPI 
239 DirectSoundCaptureEnumerateW(
240     LPDSENUMCALLBACKW lpDSEnumCallback,
241     LPVOID lpContext)
242 {
243     unsigned devs, wid;
244     DSDRIVERDESC desc;
245     GUID guid;
246     int err;
247     WCHAR wDesc[MAXPNAMELEN];
248     WCHAR wName[MAXPNAMELEN];
249
250     TRACE("(%p,%p)\n", lpDSEnumCallback, lpContext );
251
252     if (lpDSEnumCallback == NULL) {
253         WARN("invalid parameter: lpDSEnumCallback == NULL\n");
254         return DSERR_INVALIDPARAM;
255     }
256
257     devs = waveInGetNumDevs();
258     if (devs > 0) {
259         if (GetDeviceID(&DSDEVID_DefaultCapture, &guid) == DS_OK) {
260             GUID temp;
261             for (wid = 0; wid < devs; ++wid) {
262                 err = mmErr(waveInMessage((HWAVEIN)wid,DRV_QUERYDSOUNDGUID,(DWORD)&temp,0));
263                 if (err == DS_OK) {
264                     if (IsEqualGUID( &guid, &temp ) ) {
265                         err = mmErr(waveInMessage((HWAVEIN)wid,DRV_QUERYDSOUNDDESC,(DWORD)&desc,0));
266                         if (err == DS_OK) {
267                             TRACE("calling lpDSEnumCallback(NULL,\"%s\",\"%s\",%p)\n",
268                                 "Primary Sound Capture Driver",desc.szDrvName,lpContext);
269                             MultiByteToWideChar( CP_ACP, 0, "Primary Sound Capture Driver", -1, 
270                                 wDesc, sizeof(wDesc)/sizeof(WCHAR) );
271                             MultiByteToWideChar( CP_ACP, 0, desc.szDrvName, -1, 
272                                 wName, sizeof(wName)/sizeof(WCHAR) );
273                             if (lpDSEnumCallback(NULL, wDesc, wName, lpContext) == FALSE)
274                                 return DS_OK;
275                         }
276                     }
277                 }
278             }
279         }
280     }
281
282     for (wid = 0; wid < devs; ++wid) {
283         err = mmErr(waveInMessage((HWAVEIN)wid,DRV_QUERYDSOUNDDESC,(DWORD)&desc,0));
284         if (err == DS_OK) {
285             err = mmErr(waveInMessage((HWAVEIN)wid,DRV_QUERYDSOUNDGUID,(DWORD)&guid,0));
286             if (err == DS_OK) {
287                 TRACE("calling lpDSEnumCallback(%s,\"%s\",\"%s\",%p)\n",
288                     debugstr_guid(&DSDEVID_DefaultCapture),desc.szDesc,desc.szDrvName,lpContext);
289                 MultiByteToWideChar( CP_ACP, 0, desc.szDesc, -1, 
290                     wDesc, sizeof(wDesc)/sizeof(WCHAR) );
291                 MultiByteToWideChar( CP_ACP, 0, desc.szDrvName, -1, 
292                     wName, sizeof(wName)/sizeof(WCHAR) );
293                 if (lpDSEnumCallback((LPGUID)&DSDEVID_DefaultCapture, wDesc, wName, lpContext) == FALSE)
294                     return DS_OK;
295             }
296         } 
297     }
298
299     return DS_OK;
300 }
301
302 static void CALLBACK 
303 DSOUND_capture_callback(
304     HWAVEIN hwi, 
305     UINT msg, 
306     DWORD dwUser, 
307     DWORD dw1, 
308     DWORD dw2 )
309 {
310     IDirectSoundCaptureImpl* This = (IDirectSoundCaptureImpl*)dwUser;
311     TRACE("(%p,%08x(%s),%08lx,%08lx,%08lx) entering at %ld\n",hwi,msg,
312         msg == MM_WIM_OPEN ? "MM_WIM_OPEN" : msg == MM_WIM_CLOSE ? "MM_WIM_CLOSE" :
313         msg == MM_WIM_DATA ? "MM_WIM_DATA" : "UNKNOWN",dwUser,dw1,dw2,GetTickCount());
314
315     if (msg == MM_WIM_DATA) {
316         EnterCriticalSection( &(This->lock) );
317         TRACE("DirectSoundCapture msg=MM_WIM_DATA, old This->state=%ld, old This->index=%d\n",This->state,This->index);
318         if (This->state != STATE_STOPPED) {
319             int index = This->index;
320             if (This->state == STATE_STARTING) { 
321                 MMTIME mtime;
322                 mtime.wType = TIME_BYTES;
323                 waveInGetPosition(This->hwi, &mtime, sizeof(mtime));
324                 TRACE("mtime.u.cb=%ld,This->buflen=%ld\n", mtime.u.cb, This->buflen);
325                 mtime.u.cb = mtime.u.cb % This->buflen;
326                 This->read_position = mtime.u.cb;
327                 This->state = STATE_CAPTURING;
328             }
329             waveInUnprepareHeader(hwi,&(This->pwave[This->index]),sizeof(WAVEHDR));
330             if (This->capture_buffer->nrofnotifies)
331                 SetEvent(This->capture_buffer->notifies[This->index].hEventNotify);
332             This->index = (This->index + 1) % This->nrofpwaves;
333             if ( (This->index == 0) && !(This->capture_buffer->flags & DSCBSTART_LOOPING) ) {
334                 TRACE("end of buffer\n");
335                 This->state = STATE_STOPPED;
336             } else {
337                 if (This->state == STATE_CAPTURING) {
338                     waveInPrepareHeader(hwi,&(This->pwave[index]),sizeof(WAVEHDR));
339                     waveInAddBuffer(hwi, &(This->pwave[index]), sizeof(WAVEHDR));
340                 }
341             }
342         }
343         TRACE("DirectSoundCapture new This->state=%ld, new This->index=%d\n",This->state,This->index);
344         LeaveCriticalSection( &(This->lock) );
345     }
346
347     TRACE("completed\n");
348 }
349
350 static HRESULT WINAPI
351 IDirectSoundCaptureImpl_QueryInterface(
352     LPDIRECTSOUNDCAPTURE iface,
353     REFIID riid,
354     LPVOID* ppobj )
355 {
356     ICOM_THIS(IDirectSoundCaptureImpl,iface);
357     TRACE( "(%p,%s,%p)\n", This, debugstr_guid(riid), ppobj );
358
359     if (ppobj == NULL) {
360         WARN("invalid parameter\n");
361         return E_INVALIDARG;
362     }
363
364     *ppobj = NULL;
365
366     if (This->driver) {
367         HRESULT hres; 
368         hres = IDsCaptureDriver_QueryInterface(This->driver, riid, ppobj);
369         if (hres != DS_OK)
370             WARN("IDsCaptureDriver_QueryInterface failed\n");
371         return hres;
372     }
373
374     WARN("unsupported riid: %s\n", debugstr_guid(riid));
375     return E_FAIL;
376 }
377
378 static ULONG WINAPI
379 IDirectSoundCaptureImpl_AddRef( LPDIRECTSOUNDCAPTURE iface )
380 {
381     ULONG uRef;
382     ICOM_THIS(IDirectSoundCaptureImpl,iface);
383     TRACE("(%p) ref was %ld, thread is %04lx\n",This, This->ref, GetCurrentThreadId());
384
385     EnterCriticalSection( &(This->lock) );
386     uRef = ++(This->ref);
387
388     if (This->driver) 
389         IDsCaptureDriver_AddRef(This->driver);
390
391     LeaveCriticalSection( &(This->lock) );
392
393     return uRef;
394 }
395
396 static ULONG WINAPI
397 IDirectSoundCaptureImpl_Release( LPDIRECTSOUNDCAPTURE iface )
398 {
399     ULONG uRef;
400     ICOM_THIS(IDirectSoundCaptureImpl,iface);
401     TRACE("(%p) ref was %ld, thread is %04lx\n",This, This->ref, GetCurrentThreadId());
402
403     EnterCriticalSection( &(This->lock) );
404
405     uRef = --(This->ref);
406
407     LeaveCriticalSection( &(This->lock) );
408
409     if ( uRef == 0 ) {
410         TRACE("deleting object\n");
411         if (This->capture_buffer)
412             IDirectSoundCaptureBufferImpl_Release(
413                 (LPDIRECTSOUNDCAPTUREBUFFER8) This->capture_buffer);
414
415         if (This->driver) { 
416             IDsCaptureDriver_Close(This->driver);
417             IDsCaptureDriver_Release(This->driver);
418         }
419
420         if (This->pwfx)
421             HeapFree(GetProcessHeap(), 0, This->pwfx);
422
423         DeleteCriticalSection( &(This->lock) );
424         HeapFree( GetProcessHeap(), 0, This );
425         dsound_capture = NULL;
426         TRACE("(%p) released\n",This);
427     }
428
429     return uRef;
430 }
431
432 static HRESULT WINAPI
433 IDirectSoundCaptureImpl_CreateCaptureBuffer(
434     LPDIRECTSOUNDCAPTURE iface,
435     LPCDSCBUFFERDESC lpcDSCBufferDesc,
436     LPDIRECTSOUNDCAPTUREBUFFER* lplpDSCaptureBuffer,
437     LPUNKNOWN pUnk )
438 {
439     HRESULT hr;
440     ICOM_THIS(IDirectSoundCaptureImpl,iface);
441
442     TRACE( "(%p,%p,%p,%p)\n",This,lpcDSCBufferDesc,lplpDSCaptureBuffer,pUnk );
443
444     if (This == NULL) {
445         WARN("invalid parameter: This == NULL\n");
446         return DSERR_INVALIDPARAM;
447     }
448
449     if (lpcDSCBufferDesc == NULL) {
450         WARN("invalid parameter: lpcDSCBufferDesc == NULL)\n");
451         return DSERR_INVALIDPARAM;
452     }
453
454     if (lplpDSCaptureBuffer == NULL) {
455         WARN("invalid parameter: lplpDSCaptureBuffer == NULL\n");
456         return DSERR_INVALIDPARAM;
457     }
458
459     if (pUnk != NULL) {
460         WARN("invalid parameter: pUnk != NULL\n");
461         return DSERR_INVALIDPARAM;
462     }
463
464     /* FIXME: We can only have one buffer so what do we do here? */
465     if (This->capture_buffer) {
466         WARN("lnvalid parameter: already has buffer\n");
467         return DSERR_INVALIDPARAM;    /* DSERR_GENERIC ? */
468     }
469
470     hr = DSOUND_CreateDirectSoundCaptureBuffer( This, lpcDSCBufferDesc, 
471         (LPVOID*)lplpDSCaptureBuffer );
472
473     if (hr != DS_OK)
474         WARN("DSOUND_CreateDirectSoundCaptureBuffer failed\n");
475
476     return hr;
477 }
478
479 static HRESULT WINAPI
480 IDirectSoundCaptureImpl_GetCaps(
481     LPDIRECTSOUNDCAPTURE iface,
482     LPDSCCAPS lpDSCCaps )
483 {
484     ICOM_THIS(IDirectSoundCaptureImpl,iface);
485     TRACE("(%p,%p)\n",This,lpDSCCaps);
486
487     if (lpDSCCaps== NULL) {
488         WARN("invalid parameter: lpDSCCaps== NULL\n");
489         return DSERR_INVALIDPARAM;
490     }
491
492     if (lpDSCCaps->dwSize < sizeof(*lpDSCCaps)) {
493         WARN("invalid parameter: lpDSCCaps->dwSize = %ld < %d\n", 
494             lpDSCCaps->dwSize, sizeof(*lpDSCCaps));
495         return DSERR_INVALIDPARAM;
496     }
497
498     if ( !(This->initialized) ) {
499         WARN("not initialized\n");
500         return DSERR_UNINITIALIZED;
501     }
502
503     lpDSCCaps->dwFlags = This->drvcaps.dwFlags;
504     lpDSCCaps->dwFormats = This->drvcaps.dwFormats;
505     lpDSCCaps->dwChannels = This->drvcaps.dwChannels;
506
507     TRACE("(flags=0x%08lx,format=0x%08lx,channels=%ld)\n",lpDSCCaps->dwFlags,
508         lpDSCCaps->dwFormats, lpDSCCaps->dwChannels);
509
510     return DS_OK;
511 }
512
513 static HRESULT WINAPI
514 IDirectSoundCaptureImpl_Initialize(
515     LPDIRECTSOUNDCAPTURE iface,
516     LPCGUID lpcGUID )
517 {
518     HRESULT err = DSERR_INVALIDPARAM;
519     unsigned wid, widn;
520     ICOM_THIS(IDirectSoundCaptureImpl,iface);
521     TRACE("(%p)\n", This);
522
523     if (!This) {
524         WARN("invalid parameter: This == NULL\n");
525         return DSERR_INVALIDPARAM;
526     }
527
528     if (This->initialized) {
529         WARN("already initialized\n");
530         return DSERR_ALREADYINITIALIZED;
531     }
532
533     widn = waveInGetNumDevs();
534
535     if (!widn) { 
536         WARN("no audio devices found\n");
537         return DSERR_NODRIVER;
538     }
539
540     /* Get dsound configuration */
541     setup_dsound_options();
542
543     /* enumerate WINMM audio devices and find the one we want */
544     for (wid=0; wid<widn; wid++) {
545         GUID guid;
546         err = mmErr(waveInMessage((HWAVEIN)wid,DRV_QUERYDSOUNDGUID,(DWORD)(&guid),0));
547         if (err != DS_OK) {
548             WARN("waveInMessage failed; err=%lx\n",err);
549             return err;
550         }
551         if (IsEqualGUID( lpcGUID, &guid) ) {
552             err = DS_OK;
553             break;
554         }
555     }
556
557     if (err != DS_OK) {
558         WARN("invalid parameter\n");
559         return DSERR_INVALIDPARAM;
560     }
561
562     err = mmErr(waveInMessage((HWAVEIN)wid,DRV_QUERYDSOUNDIFACE,(DWORD)&(This->driver),0));
563     if ( (err != DS_OK) && (err != DSERR_UNSUPPORTED) ) {
564         WARN("waveInMessage failed; err=%lx\n",err);
565         return err;
566     }
567     err = DS_OK;
568
569     /* Disable the direct sound driver to force emulation if requested. */
570     if (ds_hw_accel == DS_HW_ACCEL_EMULATION)
571         This->driver = NULL;
572
573     /* Get driver description */
574     if (This->driver) {
575         TRACE("using DirectSound driver\n");
576         err = IDsCaptureDriver_GetDriverDesc(This->driver, &(This->drvdesc));
577         if (err != DS_OK) {
578             WARN("IDsCaptureDriver_GetDriverDesc failed\n");
579             return err;
580         }
581     } else {
582         TRACE("using WINMM\n");
583         /* if no DirectSound interface available, use WINMM API instead */
584         This->drvdesc.dwFlags = DSDDESC_DOMMSYSTEMOPEN | 
585             DSDDESC_DOMMSYSTEMSETFORMAT;
586     }
587
588     This->drvdesc.dnDevNode = wid;
589     
590     /* open the DirectSound driver if available */
591     if (This->driver && (err == DS_OK))
592         err = IDsCaptureDriver_Open(This->driver);
593
594     if (err == DS_OK) {
595         This->initialized = TRUE;
596
597         /* the driver is now open, so it's now allowed to call GetCaps */
598         if (This->driver) {
599             This->drvcaps.dwSize = sizeof(This->drvcaps);
600             err = IDsCaptureDriver_GetCaps(This->driver,&(This->drvcaps));
601             if (err != DS_OK) {
602                 WARN("IDsCaptureDriver_GetCaps failed\n");
603                 return err;
604             }
605         } else /*if (This->hwi)*/ {
606             WAVEINCAPSA    wic;
607             err = mmErr(waveInGetDevCapsA((UINT)This->drvdesc.dnDevNode, &wic, sizeof(wic)));
608
609             if (err == DS_OK) {
610                 This->drvcaps.dwFlags = 0;
611                 strncpy(This->drvdesc.szDrvName, wic.szPname, 
612                     sizeof(This->drvdesc.szDrvName)); 
613
614                 This->drvcaps.dwFlags |= DSCCAPS_EMULDRIVER;
615                 This->drvcaps.dwFormats = wic.dwFormats;
616                 This->drvcaps.dwChannels = wic.wChannels;
617             }
618         }
619     }
620
621     return err;
622 }
623
624 static ICOM_VTABLE(IDirectSoundCapture) dscvt =
625 {
626     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
627     /* IUnknown methods */
628     IDirectSoundCaptureImpl_QueryInterface,
629     IDirectSoundCaptureImpl_AddRef,
630     IDirectSoundCaptureImpl_Release,
631
632     /* IDirectSoundCapture methods */
633     IDirectSoundCaptureImpl_CreateCaptureBuffer,
634     IDirectSoundCaptureImpl_GetCaps,
635     IDirectSoundCaptureImpl_Initialize
636 };
637
638 static HRESULT
639 DSOUND_CreateDirectSoundCaptureBuffer(
640     IDirectSoundCaptureImpl *ipDSC, 
641     LPCDSCBUFFERDESC lpcDSCBufferDesc, 
642     LPVOID* ppobj )
643 {
644     LPWAVEFORMATEX  wfex;
645     TRACE( "(%p,%p)\n", lpcDSCBufferDesc, ppobj );
646
647     if (ipDSC == NULL) {
648         WARN("invalid parameter: ipDSC == NULL\n");
649         return DSERR_INVALIDPARAM;
650     }
651
652     if (lpcDSCBufferDesc == NULL) {
653         WARN("invalid parameter: lpcDSCBufferDesc == NULL\n");
654         return DSERR_INVALIDPARAM;
655     }
656
657     if (ppobj == NULL) {
658         WARN("invalid parameter: ppobj == NULL\n");
659         return DSERR_INVALIDPARAM;
660     }
661
662     if ( (lpcDSCBufferDesc->dwSize < sizeof(DSCBUFFERDESC)) || 
663         (lpcDSCBufferDesc->dwBufferBytes == 0) ||
664         (lpcDSCBufferDesc->lpwfxFormat == NULL) ) {
665         WARN("invalid lpcDSCBufferDesc\n");
666         *ppobj = NULL;
667         return DSERR_INVALIDPARAM;
668     }
669
670     if ( !ipDSC->initialized ) {
671         WARN("not initialized\n");
672         *ppobj = NULL;
673         return DSERR_UNINITIALIZED;
674     }
675
676     wfex = lpcDSCBufferDesc->lpwfxFormat;
677
678     if (wfex) {
679         TRACE("(formattag=0x%04x,chans=%d,samplerate=%ld,"
680             "bytespersec=%ld,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
681             wfex->wFormatTag, wfex->nChannels, wfex->nSamplesPerSec,
682             wfex->nAvgBytesPerSec, wfex->nBlockAlign,
683             wfex->wBitsPerSample, wfex->cbSize);
684
685         if (wfex->wFormatTag == WAVE_FORMAT_PCM) {
686             ipDSC->pwfx = HeapAlloc(GetProcessHeap(),0,sizeof(WAVEFORMATEX));
687             memcpy(ipDSC->pwfx, wfex, sizeof(WAVEFORMATEX));
688             ipDSC->pwfx->cbSize = 0;
689         } else {
690             ipDSC->pwfx = HeapAlloc(GetProcessHeap(),0,sizeof(WAVEFORMATEX)+wfex->cbSize);
691             memcpy(ipDSC->pwfx, wfex, sizeof(WAVEFORMATEX)+wfex->cbSize);
692         }
693     } else {
694         WARN("lpcDSCBufferDesc->lpwfxFormat == 0\n");
695         *ppobj = NULL;
696         return DSERR_INVALIDPARAM; /* FIXME: DSERR_BADFORMAT ? */
697     }
698
699     *ppobj = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,
700         sizeof(IDirectSoundCaptureBufferImpl));
701
702     if ( *ppobj == NULL ) {
703         WARN("out of memory\n");
704         *ppobj = NULL;
705         return DSERR_OUTOFMEMORY;
706     } else {
707         HRESULT err = DS_OK;
708         ICOM_THIS(IDirectSoundCaptureBufferImpl,*ppobj);
709
710         This->ref = 1;
711         This->dsound = ipDSC;
712         This->dsound->capture_buffer = This;
713         This->notify = NULL;
714         This->nrofnotifies = 0;
715         This->hwnotify = NULL;
716
717         This->pdscbd = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,
718             lpcDSCBufferDesc->dwSize);
719         if (This->pdscbd)     
720             memcpy(This->pdscbd, lpcDSCBufferDesc, lpcDSCBufferDesc->dwSize);
721         else {
722             WARN("no memory\n");
723             This->dsound->capture_buffer = 0;
724             HeapFree( GetProcessHeap(), 0, This );
725             *ppobj = NULL;
726             return DSERR_OUTOFMEMORY; 
727         }
728
729         This->lpVtbl = &dscbvt;
730
731         if (ipDSC->driver) {
732             err = IDsCaptureDriver_CreateCaptureBuffer(ipDSC->driver, 
733                 ipDSC->pwfx,0,0,&(ipDSC->buflen),&(ipDSC->buffer),(LPVOID*)&(ipDSC->hwbuf));
734             if (err != DS_OK) {
735                 WARN("IDsCaptureDriver_CreateCaptureBuffer failed\n");
736                 This->dsound->capture_buffer = 0;
737                 HeapFree( GetProcessHeap(), 0, This );
738                 *ppobj = NULL;
739                 return err;
740             }
741         } else {
742             LPBYTE newbuf;
743             DWORD buflen;
744             DWORD flags = CALLBACK_FUNCTION;
745             if (ds_hw_accel != DS_HW_ACCEL_EMULATION)
746                 flags |= WAVE_DIRECTSOUND;
747             err = mmErr(waveInOpen(&(ipDSC->hwi),
748                 ipDSC->drvdesc.dnDevNode, ipDSC->pwfx,
749                 (DWORD)DSOUND_capture_callback, (DWORD)ipDSC, flags));
750             if (err != DS_OK) {
751                 WARN("waveInOpen failed\n");
752                 This->dsound->capture_buffer = 0;
753                 HeapFree( GetProcessHeap(), 0, This );
754                 *ppobj = NULL;
755                 return err;
756             }
757
758             buflen = lpcDSCBufferDesc->dwBufferBytes;
759             TRACE("desired buflen=%ld, old buffer=%p\n", buflen, ipDSC->buffer);
760             if (ipDSC->buffer)
761                 newbuf = (LPBYTE)HeapReAlloc(GetProcessHeap(),0,ipDSC->buffer,buflen);
762             else
763                 newbuf = (LPBYTE)HeapAlloc(GetProcessHeap(),0,buflen);      
764             if (newbuf == NULL) {
765                 WARN("failed to allocate capture buffer\n");
766                 err = DSERR_OUTOFMEMORY;
767                 /* but the old buffer might still exist and must be re-prepared */
768             } else {
769                 ipDSC->buffer = newbuf;
770                 ipDSC->buflen = buflen;
771             }
772         }
773     }
774
775     TRACE("returning DS_OK\n");
776     return DS_OK;
777 }
778
779 /*******************************************************************************
780  *              IDirectSoundCaptureNotify
781  */
782 static HRESULT WINAPI IDirectSoundCaptureNotifyImpl_QueryInterface(
783     LPDIRECTSOUNDNOTIFY iface,
784     REFIID riid,
785     LPVOID *ppobj)
786 {
787     ICOM_THIS(IDirectSoundCaptureNotifyImpl,iface);
788     TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
789
790     if (This->dscb == NULL) {
791         WARN("invalid parameter\n");
792         return E_INVALIDARG;
793     }
794
795     return IDirectSoundCaptureBuffer_QueryInterface((LPDIRECTSOUNDCAPTUREBUFFER)This->dscb, riid, ppobj);
796 }
797
798 static ULONG WINAPI IDirectSoundCaptureNotifyImpl_AddRef(LPDIRECTSOUNDNOTIFY iface)
799 {
800     ICOM_THIS(IDirectSoundCaptureNotifyImpl,iface);
801     DWORD ref;
802
803     TRACE("(%p) ref was %ld, thread is %04lx\n",This, This->ref, GetCurrentThreadId());
804
805     ref = InterlockedIncrement(&(This->ref));
806     return ref;
807 }
808
809 static ULONG WINAPI IDirectSoundCaptureNotifyImpl_Release(LPDIRECTSOUNDNOTIFY iface)
810 {
811     ICOM_THIS(IDirectSoundCaptureNotifyImpl,iface);
812     DWORD ref;
813
814     TRACE("(%p) ref was %ld, thread is %04lx\n",This, This->ref, GetCurrentThreadId());
815
816     ref = InterlockedDecrement(&(This->ref));
817     if (ref == 0) {
818         This->dscb->notify=NULL;
819         IDirectSoundCaptureBuffer_Release((LPDIRECTSOUNDCAPTUREBUFFER)This->dscb);
820         HeapFree(GetProcessHeap(),0,This);
821         TRACE("(%p) released\n",This);
822     }
823     return ref;
824 }
825
826 static HRESULT WINAPI IDirectSoundCaptureNotifyImpl_SetNotificationPositions(
827     LPDIRECTSOUNDNOTIFY iface,
828     DWORD howmuch,
829     LPCDSBPOSITIONNOTIFY notify)
830 {
831     ICOM_THIS(IDirectSoundCaptureNotifyImpl,iface);
832     TRACE("(%p,0x%08lx,%p)\n",This,howmuch,notify);
833
834     if (notify == NULL) {
835         WARN("invalid parameter: notify == NULL\n");
836         return DSERR_INVALIDPARAM;
837     }
838
839     if (TRACE_ON(dsound)) {
840         int     i;
841         for (i=0;i<howmuch;i++)
842             TRACE("notify at %ld to 0x%08lx\n",
843             notify[i].dwOffset,(DWORD)notify[i].hEventNotify);
844     }
845
846     if (This->dscb->hwnotify) {
847         HRESULT hres;
848         hres = IDsDriverNotify_SetNotificationPositions(This->dscb->hwnotify, howmuch, notify);
849         if (hres != DS_OK)
850             WARN("IDsDriverNotify_SetNotificationPositions failed\n");
851         return hres;
852     } else {
853         /* Make an internal copy of the caller-supplied array.
854          * Replace the existing copy if one is already present. */
855         if (This->dscb->notifies)
856             This->dscb->notifies = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 
857                 This->dscb->notifies, howmuch * sizeof(DSBPOSITIONNOTIFY));
858         else
859             This->dscb->notifies = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 
860                 howmuch * sizeof(DSBPOSITIONNOTIFY));
861
862         if (This->dscb->notifies == NULL) {
863             WARN("out of memory\n");
864             return DSERR_OUTOFMEMORY;
865         }
866         memcpy(This->dscb->notifies, notify, howmuch * sizeof(DSBPOSITIONNOTIFY));
867         This->dscb->nrofnotifies = howmuch;
868     }
869
870     return S_OK;
871 }
872
873 ICOM_VTABLE(IDirectSoundNotify) dscnvt =
874 {
875     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
876     IDirectSoundCaptureNotifyImpl_QueryInterface,
877     IDirectSoundCaptureNotifyImpl_AddRef,
878     IDirectSoundCaptureNotifyImpl_Release,
879     IDirectSoundCaptureNotifyImpl_SetNotificationPositions,
880 };
881
882 HRESULT WINAPI IDirectSoundCaptureNotifyImpl_Create(
883     IDirectSoundCaptureBufferImpl *dscb,
884     IDirectSoundCaptureNotifyImpl **pdscn)
885 {
886     IDirectSoundCaptureNotifyImpl * dscn;
887     TRACE("(%p,%p)\n",dscb,pdscn);
888
889     dscn = (IDirectSoundCaptureNotifyImpl*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(dscn));
890
891     if (dscn == NULL) {
892         WARN("out of memory\n");
893         return DSERR_OUTOFMEMORY;
894     }
895
896     dscn->ref = 0;
897     dscn->lpVtbl = &dscnvt;
898     dscn->dscb = dscb;
899     dscb->notify = dscn;
900     IDirectSoundCaptureBuffer_AddRef((LPDIRECTSOUNDCAPTUREBUFFER)dscb);
901
902     *pdscn = dscn;
903     return DS_OK;
904 }
905
906 /*******************************************************************************
907  *              IDirectSoundCaptureBuffer
908  */
909 static HRESULT WINAPI
910 IDirectSoundCaptureBufferImpl_QueryInterface(
911     LPDIRECTSOUNDCAPTUREBUFFER8 iface,
912     REFIID riid,
913     LPVOID* ppobj )
914 {
915     ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
916     HRESULT hres;
917     TRACE( "(%p,%s,%p)\n", This, debugstr_guid(riid), ppobj );
918
919     if (ppobj == NULL) {
920         WARN("invalid parameter\n");
921         return E_INVALIDARG;
922     }
923
924     *ppobj = NULL;
925
926     if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ||
927          IsEqualGUID( &IID_IDirectSoundNotify8, riid ) ) {
928         if (!This->notify)
929             hres = IDirectSoundCaptureNotifyImpl_Create(This, &This->notify);
930         if (This->notify) {
931             if (This->dsound->hwbuf) {
932                 hres = IDsCaptureDriverBuffer_QueryInterface(This->dsound->hwbuf, 
933                     &IID_IDsDriverNotify, (LPVOID*)&(This->hwnotify));
934                 if (hres != DS_OK) {
935                     WARN("IDsCaptureDriverBuffer_QueryInterface failed\n");
936                     *ppobj = 0;
937                     return hres;
938                 }
939             }
940
941             IDirectSoundNotify_AddRef((LPDIRECTSOUNDNOTIFY)This->notify);
942             *ppobj = (LPVOID)This->notify;
943             return DS_OK;
944         }
945
946         WARN("IID_IDirectSoundNotify\n");
947         return E_FAIL;
948     }
949
950     if ( IsEqualGUID( &IID_IDirectSoundCaptureBuffer, riid ) ||
951          IsEqualGUID( &IID_IDirectSoundCaptureBuffer8, riid ) ) {
952         IDirectSoundCaptureBuffer8_AddRef(iface);
953         *ppobj = This;
954         return NO_ERROR;
955     }
956
957     FIXME("(%p,%s,%p) unsupported GUID\n", This, debugstr_guid(riid), ppobj);
958     return E_NOINTERFACE;
959 }
960
961 static ULONG WINAPI
962 IDirectSoundCaptureBufferImpl_AddRef( LPDIRECTSOUNDCAPTUREBUFFER8 iface )
963 {
964     ULONG uRef;
965     ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
966     TRACE("(%p) ref was %ld, thread is %04lx\n",This, This->ref, GetCurrentThreadId());
967
968     assert(This->dsound);
969
970     EnterCriticalSection( &(This->dsound->lock) );
971
972     uRef = ++(This->ref);
973
974     LeaveCriticalSection( &(This->dsound->lock) );
975
976     return uRef;
977 }
978
979 static ULONG WINAPI
980 IDirectSoundCaptureBufferImpl_Release( LPDIRECTSOUNDCAPTUREBUFFER8 iface )
981 {
982     ULONG uRef;
983     ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
984     TRACE("(%p) ref was %ld, thread is %04lx\n",This, This->ref, GetCurrentThreadId());
985
986     assert(This->dsound);
987
988     EnterCriticalSection( &(This->dsound->lock) );
989
990     uRef = --(This->ref);
991
992     LeaveCriticalSection( &(This->dsound->lock) );
993
994     if ( uRef == 0 ) {
995         TRACE("deleting object\n");
996         if (This->pdscbd)
997             HeapFree(GetProcessHeap(),0, This->pdscbd);
998
999         if (This->dsound->hwi) {
1000             waveInReset(This->dsound->hwi);
1001             waveInClose(This->dsound->hwi);
1002             if (This->dsound->pwave) {
1003                 HeapFree(GetProcessHeap(),0, This->dsound->pwave);
1004                 This->dsound->pwave = 0;
1005             }
1006             This->dsound->hwi = 0;
1007         }
1008
1009         if (This->dsound->hwbuf) 
1010             IDsCaptureDriverBuffer_Release(This->dsound->hwbuf);
1011
1012         /* remove from IDirectSoundCaptureImpl */
1013         if (This->dsound)
1014             This->dsound->capture_buffer = NULL;
1015         else
1016             ERR("does not reference dsound\n");
1017
1018         if (This->notify)
1019             IDirectSoundNotify_Release((LPDIRECTSOUNDNOTIFY)This->notify);
1020         
1021         if (This->notifies != NULL)
1022                 HeapFree(GetProcessHeap(), 0, This->notifies);
1023
1024         HeapFree( GetProcessHeap(), 0, This );
1025         TRACE("(%p) released\n",This);
1026     }
1027
1028     return uRef;
1029 }
1030
1031 static HRESULT WINAPI
1032 IDirectSoundCaptureBufferImpl_GetCaps(
1033     LPDIRECTSOUNDCAPTUREBUFFER8 iface,
1034     LPDSCBCAPS lpDSCBCaps )
1035 {
1036     ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
1037     TRACE( "(%p,%p)\n", This, lpDSCBCaps );
1038
1039     if (This == NULL) {
1040         WARN("invalid parameter: This == NULL\n");
1041         return DSERR_INVALIDPARAM;
1042     }
1043
1044     if (lpDSCBCaps == NULL) {
1045         WARN("invalid parameter: lpDSCBCaps == NULL\n");
1046         return DSERR_INVALIDPARAM;
1047     }
1048
1049     if (lpDSCBCaps->dwSize < sizeof(DSCBCAPS)) {
1050         WARN("invalid parameter: lpDSCBCaps->dwSize = %ld < %d\n", 
1051             lpDSCBCaps->dwSize, sizeof(DSCBCAPS));
1052         return DSERR_INVALIDPARAM;
1053     }
1054
1055     if (This->dsound == NULL) {
1056         WARN("invalid parameter: This->dsound == NULL\n");
1057         return DSERR_INVALIDPARAM;
1058     }
1059
1060     lpDSCBCaps->dwSize = sizeof(DSCBCAPS);
1061     lpDSCBCaps->dwFlags = This->flags;
1062     lpDSCBCaps->dwBufferBytes = This->pdscbd->dwBufferBytes;
1063     lpDSCBCaps->dwReserved = 0;
1064
1065     TRACE("returning DS_OK\n");
1066     return DS_OK;
1067 }
1068
1069 static HRESULT WINAPI
1070 IDirectSoundCaptureBufferImpl_GetCurrentPosition(
1071     LPDIRECTSOUNDCAPTUREBUFFER8 iface,
1072     LPDWORD lpdwCapturePosition,
1073     LPDWORD lpdwReadPosition )
1074 {
1075     ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
1076     TRACE( "(%p,%p,%p)\n", This, lpdwCapturePosition, lpdwReadPosition );
1077
1078     if (This == NULL) {
1079         WARN("invalid parameter: This == NULL\n");
1080         return DSERR_INVALIDPARAM;
1081     }
1082
1083     if (This->dsound == NULL) {
1084         WARN("invalid parameter: This->dsound == NULL\n");
1085         return DSERR_INVALIDPARAM;
1086     }
1087
1088     if (This->dsound->driver) {
1089         HRESULT hres;
1090         hres = IDsCaptureDriverBuffer_GetPosition(This->dsound->hwbuf, lpdwCapturePosition, lpdwReadPosition );
1091         if (hres != DS_OK) {
1092             WARN("IDsCaptureDriverBuffer_GetPosition failed\n");
1093             return hres;
1094         }
1095     } else if (This->dsound->hwi) {
1096         EnterCriticalSection(&(This->dsound->lock));
1097         TRACE("old This->dsound->state=%ld\n",This->dsound->state);
1098         if (lpdwCapturePosition) {
1099             MMTIME mtime;
1100             mtime.wType = TIME_BYTES;
1101             waveInGetPosition(This->dsound->hwi, &mtime, sizeof(mtime));
1102             TRACE("mtime.u.cb=%ld,This->dsound->buflen=%ld\n", mtime.u.cb, 
1103                 This->dsound->buflen);
1104             mtime.u.cb = mtime.u.cb % This->dsound->buflen;
1105             *lpdwCapturePosition = mtime.u.cb;
1106         }
1107     
1108         if (lpdwReadPosition) {
1109             if (This->dsound->state == STATE_STARTING) { 
1110                 if (lpdwCapturePosition)
1111                     This->dsound->read_position = *lpdwCapturePosition;
1112                 This->dsound->state = STATE_CAPTURING;
1113             } 
1114             *lpdwReadPosition = This->dsound->read_position;
1115         }
1116         TRACE("new This->dsound->state=%ld\n",This->dsound->state);
1117         LeaveCriticalSection(&(This->dsound->lock));
1118         if (lpdwCapturePosition) TRACE("*lpdwCapturePosition=%ld\n",*lpdwCapturePosition);
1119         if (lpdwReadPosition) TRACE("*lpdwReadPosition=%ld\n",*lpdwReadPosition);
1120     } else {
1121         WARN("no driver\n");
1122         return DSERR_NODRIVER;
1123     }
1124     
1125     TRACE("returning DS_OK\n");
1126     return DS_OK;
1127 }
1128
1129 static HRESULT WINAPI
1130 IDirectSoundCaptureBufferImpl_GetFormat(
1131     LPDIRECTSOUNDCAPTUREBUFFER8 iface,
1132     LPWAVEFORMATEX lpwfxFormat,
1133     DWORD dwSizeAllocated,
1134     LPDWORD lpdwSizeWritten )
1135 {
1136     ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
1137     TRACE( "(%p,%p,0x%08lx,%p)\n", This, lpwfxFormat, dwSizeAllocated, 
1138         lpdwSizeWritten );
1139
1140     if (This == NULL) {
1141         WARN("invalid parameter: This == NULL\n");
1142         return DSERR_INVALIDPARAM;
1143     }
1144
1145     if (This->dsound == NULL) {
1146         WARN("invalid parameter: This->dsound == NULL\n");
1147         return DSERR_INVALIDPARAM;
1148     }
1149
1150     if (dwSizeAllocated > (sizeof(WAVEFORMATEX) + This->dsound->pwfx->cbSize))
1151         dwSizeAllocated = sizeof(WAVEFORMATEX) + This->dsound->pwfx->cbSize;
1152
1153     if (lpwfxFormat) { /* NULL is valid (just want size) */
1154         memcpy(lpwfxFormat, This->dsound->pwfx, dwSizeAllocated);
1155         if (lpdwSizeWritten)
1156             *lpdwSizeWritten = dwSizeAllocated;
1157     } else {
1158         if (lpdwSizeWritten)
1159             *lpdwSizeWritten = sizeof(WAVEFORMATEX) + This->dsound->pwfx->cbSize;
1160         else {
1161             TRACE("invalid parameter: lpdwSizeWritten = NULL\n");
1162             return DSERR_INVALIDPARAM;
1163         }
1164     }
1165
1166     TRACE("returning DS_OK\n");
1167     return DS_OK;
1168 }
1169
1170 static HRESULT WINAPI
1171 IDirectSoundCaptureBufferImpl_GetStatus(
1172     LPDIRECTSOUNDCAPTUREBUFFER8 iface,
1173     LPDWORD lpdwStatus )
1174 {
1175     ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
1176     TRACE( "(%p, %p), thread is %04lx\n", This, lpdwStatus, GetCurrentThreadId() );
1177
1178     if (This == NULL) {
1179         WARN("invalid parameter: This == NULL\n");
1180         return DSERR_INVALIDPARAM;
1181     }
1182
1183     if (This->dsound == NULL) {
1184         WARN("invalid parameter: This->dsound == NULL\n");
1185         return DSERR_INVALIDPARAM;
1186     }
1187
1188     if (lpdwStatus == NULL) {
1189         WARN("invalid parameter: lpdwStatus == NULL\n");
1190         return DSERR_INVALIDPARAM;
1191     }
1192
1193     *lpdwStatus = 0;
1194     EnterCriticalSection(&(This->dsound->lock));
1195
1196     TRACE("old This->dsound->state=%ld, old lpdwStatus=%08lx\n",This->dsound->state,*lpdwStatus);
1197     if ((This->dsound->state == STATE_STARTING) || 
1198         (This->dsound->state == STATE_CAPTURING)) {
1199         *lpdwStatus |= DSCBSTATUS_CAPTURING;
1200         if (This->flags & DSCBSTART_LOOPING)
1201             *lpdwStatus |= DSCBSTATUS_LOOPING;
1202     }
1203     TRACE("new This->dsound->state=%ld, new lpdwStatus=%08lx\n",This->dsound->state,*lpdwStatus);
1204     LeaveCriticalSection(&(This->dsound->lock));
1205
1206     TRACE("status=%lx\n", *lpdwStatus);
1207     TRACE("returning DS_OK\n");
1208     return DS_OK;
1209 }
1210
1211 static HRESULT WINAPI
1212 IDirectSoundCaptureBufferImpl_Initialize(
1213     LPDIRECTSOUNDCAPTUREBUFFER8 iface,
1214     LPDIRECTSOUNDCAPTURE lpDSC,
1215     LPCDSCBUFFERDESC lpcDSCBDesc )
1216 {
1217     ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
1218
1219     FIXME( "(%p,%p,%p): stub\n", This, lpDSC, lpcDSCBDesc );
1220
1221     return DS_OK;
1222 }
1223
1224 static HRESULT WINAPI
1225 IDirectSoundCaptureBufferImpl_Lock(
1226     LPDIRECTSOUNDCAPTUREBUFFER8 iface,
1227     DWORD dwReadCusor,
1228     DWORD dwReadBytes,
1229     LPVOID* lplpvAudioPtr1,
1230     LPDWORD lpdwAudioBytes1,
1231     LPVOID* lplpvAudioPtr2,
1232     LPDWORD lpdwAudioBytes2,
1233     DWORD dwFlags )
1234 {
1235     HRESULT err = DS_OK;
1236     ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
1237     TRACE( "(%p,%08lu,%08lu,%p,%p,%p,%p,0x%08lx) at %ld\n", This, dwReadCusor,
1238         dwReadBytes, lplpvAudioPtr1, lpdwAudioBytes1, lplpvAudioPtr2,
1239         lpdwAudioBytes2, dwFlags, GetTickCount() );
1240
1241     if (This == NULL) {
1242         WARN("invalid parameter: This == NULL\n");
1243         return DSERR_INVALIDPARAM;
1244     }
1245
1246     if (This->dsound == NULL) {
1247         WARN("invalid parameter: This->dsound == NULL\n");
1248         return DSERR_INVALIDPARAM;
1249     }
1250
1251     if (lplpvAudioPtr1 == NULL) {
1252         WARN("invalid parameter: lplpvAudioPtr1 == NULL\n");
1253         return DSERR_INVALIDPARAM;
1254     }
1255
1256     if (lpdwAudioBytes1 == NULL) {
1257         WARN("invalid parameter: lpdwAudioBytes1 == NULL\n");
1258         return DSERR_INVALIDPARAM;
1259     }
1260
1261     EnterCriticalSection(&(This->dsound->lock));
1262
1263     if (This->dsound->driver) {
1264         err = IDsCaptureDriverBuffer_Lock(This->dsound->hwbuf, lplpvAudioPtr1, 
1265                                            lpdwAudioBytes1, lplpvAudioPtr2, lpdwAudioBytes2,
1266                                            dwReadCusor, dwReadBytes, dwFlags);
1267         if (err != DS_OK)
1268             WARN("IDsCaptureDriverBuffer_Lock failed\n");
1269     } else if (This->dsound->hwi) {
1270         *lplpvAudioPtr1 = This->dsound->buffer + dwReadCusor;
1271         if ( (dwReadCusor + dwReadBytes) > This->dsound->buflen) {
1272             *lpdwAudioBytes1 = This->dsound->buflen - dwReadCusor;
1273             if (lplpvAudioPtr2)
1274                 *lplpvAudioPtr2 = This->dsound->buffer;
1275             if (lpdwAudioBytes2)
1276                 *lpdwAudioBytes2 = dwReadBytes - *lpdwAudioBytes1;
1277         } else {
1278             *lpdwAudioBytes1 = dwReadBytes;
1279             if (lplpvAudioPtr2)
1280                 *lplpvAudioPtr2 = 0;
1281             if (lpdwAudioBytes2)
1282                 *lpdwAudioBytes2 = 0;
1283         }
1284     } else {
1285         TRACE("invalid call\n");
1286         err = DSERR_INVALIDCALL;   /* DSERR_NODRIVER ? */
1287     }
1288
1289     LeaveCriticalSection(&(This->dsound->lock));
1290
1291     return err;
1292 }
1293
1294 static HRESULT WINAPI
1295 IDirectSoundCaptureBufferImpl_Start(
1296     LPDIRECTSOUNDCAPTUREBUFFER8 iface,
1297     DWORD dwFlags )
1298 {
1299     HRESULT err = DS_OK;
1300     ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
1301     TRACE( "(%p,0x%08lx)\n", This, dwFlags );
1302
1303     if (This == NULL) {
1304         WARN("invalid parameter: This == NULL\n");
1305         return DSERR_INVALIDPARAM;
1306     }
1307
1308     if (This->dsound == NULL) {
1309         WARN("invalid parameter: This->dsound == NULL\n");
1310         return DSERR_INVALIDPARAM;
1311     }
1312
1313     if ( (This->dsound->driver == 0) && (This->dsound->hwi == 0) ) {
1314         WARN("no driver\n");
1315         return DSERR_NODRIVER;
1316     }
1317
1318     EnterCriticalSection(&(This->dsound->lock));
1319
1320     This->flags = dwFlags;
1321     TRACE("old This->dsound->state=%ld\n",This->dsound->state);
1322     if (This->dsound->state == STATE_STOPPED)
1323         This->dsound->state = STATE_STARTING;
1324     else if (This->dsound->state == STATE_STOPPING)
1325         This->dsound->state = STATE_CAPTURING;
1326     TRACE("new This->dsound->state=%ld\n",This->dsound->state);
1327
1328     LeaveCriticalSection(&(This->dsound->lock));
1329
1330     if (This->dsound->driver) {
1331         err = IDsCaptureDriverBuffer_Start(This->dsound->hwbuf, dwFlags);
1332         if (err != DS_OK)
1333             WARN("IDsCaptureDriverBuffer_Start failed\n");
1334         return err;
1335     } else {
1336         IDirectSoundCaptureImpl* ipDSC = This->dsound;
1337
1338         if (ipDSC->buffer) {
1339             if (This->nrofnotifies) {
1340                 unsigned c;
1341
1342                 ipDSC->nrofpwaves = This->nrofnotifies;
1343                 TRACE("nrofnotifies=%d\n", This->nrofnotifies);
1344
1345                 /* prepare headers */
1346                 if (ipDSC->pwave)
1347                     ipDSC->pwave = HeapReAlloc(GetProcessHeap(),0,ipDSC->pwave,
1348                         ipDSC->nrofpwaves*sizeof(WAVEHDR));
1349                 else 
1350                     ipDSC->pwave = HeapAlloc(GetProcessHeap(),0,
1351                         ipDSC->nrofpwaves*sizeof(WAVEHDR));
1352
1353                 for (c = 0; c < ipDSC->nrofpwaves; c++) {
1354                     if (c == 0) {
1355                         ipDSC->pwave[0].lpData = ipDSC->buffer;
1356                         ipDSC->pwave[0].dwBufferLength = 
1357                             This->notifies[0].dwOffset + 1;
1358                     } else {
1359                         ipDSC->pwave[c].lpData = ipDSC->buffer + 
1360                             This->notifies[c-1].dwOffset + 1;
1361                         ipDSC->pwave[c].dwBufferLength = 
1362                             This->notifies[c].dwOffset - 
1363                             This->notifies[c-1].dwOffset;
1364                     }
1365                     ipDSC->pwave[c].dwUser = (DWORD)ipDSC;
1366                     ipDSC->pwave[c].dwFlags = 0;
1367                     ipDSC->pwave[c].dwLoops = 0;
1368                     err = mmErr(waveInPrepareHeader(ipDSC->hwi,
1369                         &(ipDSC->pwave[c]),sizeof(WAVEHDR)));
1370                     if (err != DS_OK) {
1371                         while (c--)
1372                             waveInUnprepareHeader(ipDSC->hwi,
1373                                 &(ipDSC->pwave[c]),sizeof(WAVEHDR));
1374                         break;
1375                     }
1376
1377                     err = mmErr(waveInAddBuffer(ipDSC->hwi, 
1378                         &(ipDSC->pwave[c]), sizeof(WAVEHDR)));
1379                     if (err != DS_OK) {
1380                         while (c--)
1381                             waveInUnprepareHeader(ipDSC->hwi,
1382                                 &(ipDSC->pwave[c]),sizeof(WAVEHDR));
1383                         break;
1384                     }
1385                 }
1386
1387                 memset(ipDSC->buffer, 
1388                     (ipDSC->pwfx->wBitsPerSample == 8) ? 128 : 0, ipDSC->buflen);
1389             } else {
1390                 TRACE("no notifiers specified\n");
1391                 /* no notifiers specified so just create a single default header */
1392                 ipDSC->nrofpwaves = 1;
1393                 if (ipDSC->pwave)
1394                     ipDSC->pwave = HeapReAlloc(GetProcessHeap(),0,ipDSC->pwave,sizeof(WAVEHDR));
1395                 else
1396                     ipDSC->pwave = HeapAlloc(GetProcessHeap(),0,sizeof(WAVEHDR));
1397
1398                 ipDSC->pwave[0].lpData = ipDSC->buffer;
1399                 ipDSC->pwave[0].dwBufferLength = ipDSC->buflen; 
1400                 ipDSC->pwave[0].dwUser = (DWORD)ipDSC;
1401                 ipDSC->pwave[0].dwFlags = 0;
1402                 ipDSC->pwave[0].dwLoops = 0;
1403
1404                 err = mmErr(waveInPrepareHeader(ipDSC->hwi,
1405                     &(ipDSC->pwave[0]),sizeof(WAVEHDR)));
1406                 if (err != DS_OK) {
1407                     WARN("waveInPrepareHeader failed\n");
1408                     waveInUnprepareHeader(ipDSC->hwi,
1409                         &(ipDSC->pwave[0]),sizeof(WAVEHDR));
1410                 }
1411                 err = mmErr(waveInAddBuffer(ipDSC->hwi, 
1412                     &(ipDSC->pwave[0]), sizeof(WAVEHDR)));
1413                 if (err != DS_OK) {
1414                     waveInUnprepareHeader(ipDSC->hwi,
1415                         &(ipDSC->pwave[0]),sizeof(WAVEHDR));
1416                 }
1417             }
1418         }
1419
1420         ipDSC->index = 0;
1421         ipDSC->read_position = 0;
1422
1423         if (err == DS_OK) {
1424             /* start filling the first buffer */
1425             err = mmErr(waveInStart(ipDSC->hwi));
1426         }
1427     }
1428
1429     if (err != DS_OK) {
1430         WARN("calling waveInClose because of error\n");
1431         waveInClose(This->dsound->hwi);
1432         This->dsound->hwi = 0;
1433     }
1434
1435     TRACE("returning %ld\n", err);
1436     return err;
1437 }
1438
1439 static HRESULT WINAPI
1440 IDirectSoundCaptureBufferImpl_Stop( LPDIRECTSOUNDCAPTUREBUFFER8 iface )
1441 {
1442     HRESULT err = DS_OK;
1443     ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
1444     TRACE( "(%p)\n", This );
1445
1446     if (This == NULL) {
1447         WARN("invalid parameter: This == NULL\n");
1448         return DSERR_INVALIDPARAM;
1449     }
1450
1451     if (This->dsound == NULL) {
1452         WARN("invalid parameter: This->dsound == NULL\n");
1453         return DSERR_INVALIDPARAM;
1454     }
1455
1456     EnterCriticalSection(&(This->dsound->lock));
1457
1458     TRACE("old This->dsound->state=%ld\n",This->dsound->state);
1459     if (This->dsound->state == STATE_CAPTURING)
1460         This->dsound->state = STATE_STOPPING;
1461     else if (This->dsound->state == STATE_STARTING)
1462         This->dsound->state = STATE_STOPPED;
1463     TRACE("new This->dsound->state=%ld\n",This->dsound->state);
1464
1465     LeaveCriticalSection(&(This->dsound->lock));
1466
1467     if (This->dsound->driver) {
1468         err = IDsCaptureDriverBuffer_Stop(This->dsound->hwbuf);
1469         if (err == DSERR_BUFFERLOST) {
1470             /* Wine-only: the driver wants us to reopen the device */
1471             IDsCaptureDriverBuffer_Release(This->dsound->hwbuf);
1472             err = IDsCaptureDriver_CreateCaptureBuffer(This->dsound->driver,
1473                 This->dsound->pwfx,0,0,&(This->dsound->buflen),&(This->dsound->buffer),
1474                 (LPVOID*)&(This->dsound->hwbuf));
1475             if (err != DS_OK) {
1476                 WARN("IDsCaptureDriver_CreateCaptureBuffer failed\n");
1477                 This->dsound->hwbuf = 0;
1478             }
1479         } else if (err != DS_OK) 
1480             WARN("IDsCaptureDriverBuffer_Stop failed\n");
1481     } else if (This->dsound->hwi) {
1482         err = waveInStop(This->dsound->hwi);
1483     } else {
1484         WARN("no driver\n");
1485         err = DSERR_NODRIVER;
1486     }
1487
1488     TRACE( "(%p) returning 0x%08lx\n", This,err);
1489     return err;
1490 }
1491
1492 static HRESULT WINAPI
1493 IDirectSoundCaptureBufferImpl_Unlock(
1494     LPDIRECTSOUNDCAPTUREBUFFER8 iface,
1495     LPVOID lpvAudioPtr1,
1496     DWORD dwAudioBytes1,
1497     LPVOID lpvAudioPtr2,
1498     DWORD dwAudioBytes2 )
1499 {
1500     ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
1501     TRACE( "(%p,%p,%08lu,%p,%08lu)\n", This, lpvAudioPtr1, dwAudioBytes1, 
1502         lpvAudioPtr2, dwAudioBytes2 );
1503
1504     if (This == NULL) {
1505         WARN("invalid parameter: This == NULL\n");
1506         return DSERR_INVALIDPARAM;
1507     }
1508
1509     if (lpvAudioPtr1 == NULL) {
1510         WARN("invalid parameter: lpvAudioPtr1 == NULL\n");
1511         return DSERR_INVALIDPARAM;
1512     }
1513
1514     if (This->dsound->driver) {
1515         HRESULT hres;
1516         hres = IDsCaptureDriverBuffer_Unlock(This->dsound->hwbuf, lpvAudioPtr1, 
1517                                            dwAudioBytes1, lpvAudioPtr2, dwAudioBytes2);
1518         if (hres != DS_OK)
1519             WARN("IDsCaptureDriverBuffer_Unlock failed\n");
1520         return hres;
1521     } else if (This->dsound->hwi) {
1522         This->dsound->read_position = (This->dsound->read_position + 
1523             (dwAudioBytes1 + dwAudioBytes2)) % This->dsound->buflen;
1524     } else {
1525         WARN("invalid call\n");
1526         return DSERR_INVALIDCALL;
1527     }
1528
1529     return DS_OK;
1530 }
1531
1532 static HRESULT WINAPI
1533 IDirectSoundCaptureBufferImpl_GetObjectInPath(
1534     LPDIRECTSOUNDCAPTUREBUFFER8 iface,
1535     REFGUID rguidObject,
1536     DWORD dwIndex,
1537     REFGUID rguidInterface,
1538     LPVOID* ppObject )
1539 {
1540     ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
1541
1542     FIXME( "(%p,%s,%lu,%s,%p): stub\n", This, debugstr_guid(rguidObject), 
1543         dwIndex, debugstr_guid(rguidInterface), ppObject );
1544
1545     return DS_OK;
1546 }
1547
1548 static HRESULT WINAPI
1549 IDirectSoundCaptureBufferImpl_GetFXStatus(
1550     LPDIRECTSOUNDCAPTUREBUFFER8 iface,
1551     DWORD dwFXCount,
1552     LPDWORD pdwFXStatus )
1553 {
1554     ICOM_THIS(IDirectSoundCaptureBufferImpl,iface);
1555
1556     FIXME( "(%p,%lu,%p): stub\n", This, dwFXCount, pdwFXStatus );
1557
1558     return DS_OK;
1559 }
1560
1561 static ICOM_VTABLE(IDirectSoundCaptureBuffer8) dscbvt =
1562 {
1563     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1564     /* IUnknown methods */
1565     IDirectSoundCaptureBufferImpl_QueryInterface,
1566     IDirectSoundCaptureBufferImpl_AddRef,
1567     IDirectSoundCaptureBufferImpl_Release,
1568
1569     /* IDirectSoundCaptureBuffer methods */
1570     IDirectSoundCaptureBufferImpl_GetCaps,
1571     IDirectSoundCaptureBufferImpl_GetCurrentPosition,
1572     IDirectSoundCaptureBufferImpl_GetFormat,
1573     IDirectSoundCaptureBufferImpl_GetStatus,
1574     IDirectSoundCaptureBufferImpl_Initialize,
1575     IDirectSoundCaptureBufferImpl_Lock,
1576     IDirectSoundCaptureBufferImpl_Start,
1577     IDirectSoundCaptureBufferImpl_Stop,
1578     IDirectSoundCaptureBufferImpl_Unlock,
1579
1580     /* IDirectSoundCaptureBuffer methods */
1581     IDirectSoundCaptureBufferImpl_GetObjectInPath,
1582     IDirectSoundCaptureBufferImpl_GetFXStatus
1583 };
1584
1585 /*******************************************************************************
1586  * DirectSoundCapture ClassFactory
1587  */
1588
1589 static HRESULT WINAPI
1590 DSCCF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj) 
1591 {
1592     ICOM_THIS(IClassFactoryImpl,iface);
1593
1594     FIXME("(%p)->(%s,%p),stub!\n",This,debugstr_guid(riid),ppobj);
1595     return E_NOINTERFACE;
1596 }
1597
1598 static ULONG WINAPI
1599 DSCCF_AddRef(LPCLASSFACTORY iface)
1600 {
1601     ICOM_THIS(IClassFactoryImpl,iface);
1602     TRACE("(%p) ref was %ld\n", This, This->ref);
1603     return ++(This->ref);
1604 }
1605
1606 static ULONG WINAPI 
1607 DSCCF_Release(LPCLASSFACTORY iface)
1608 {
1609     ICOM_THIS(IClassFactoryImpl,iface);
1610     /* static class, won't be  freed */
1611     TRACE("(%p) ref was %ld\n", This, This->ref);
1612     return --(This->ref);
1613 }
1614
1615 static HRESULT WINAPI 
1616 DSCCF_CreateInstance(
1617         LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj )
1618 {
1619     ICOM_THIS(IClassFactoryImpl,iface);
1620     TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
1621
1622     if (ppobj == NULL) {
1623         WARN("invalid parameter\n");
1624         return E_INVALIDARG;
1625     }
1626
1627     *ppobj = NULL;
1628
1629     if ( IsEqualGUID( &IID_IDirectSoundCapture, riid ) ||
1630          IsEqualGUID( &IID_IDirectSoundCapture8, riid ) ) {
1631         return DirectSoundCaptureCreate8(0,(LPDIRECTSOUNDCAPTURE8*)ppobj,pOuter);
1632     }
1633
1634     WARN("(%p,%p,%s,%p) Interface not found!\n",This,pOuter,debugstr_guid(riid),ppobj); 
1635     return E_NOINTERFACE;
1636 }
1637
1638 static HRESULT WINAPI 
1639 DSCCF_LockServer(LPCLASSFACTORY iface,BOOL dolock)
1640 {
1641     ICOM_THIS(IClassFactoryImpl,iface);
1642     FIXME("(%p)->(%d),stub!\n",This,dolock);
1643     return S_OK;
1644 }
1645
1646 static ICOM_VTABLE(IClassFactory) DSCCF_Vtbl =
1647 {
1648     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1649     DSCCF_QueryInterface,
1650     DSCCF_AddRef,
1651     DSCCF_Release,
1652     DSCCF_CreateInstance,
1653     DSCCF_LockServer
1654 };
1655
1656 IClassFactoryImpl DSOUND_CAPTURE_CF = { &DSCCF_Vtbl, 1 };
1657
1658 /***************************************************************************
1659  * DirectSoundFullDuplexCreate8 [DSOUND.10]
1660  *
1661  * Create and initialize a DirectSoundFullDuplex interface.
1662  *
1663  * PARAMS
1664  *    pcGuidCaptureDevice [I] Address of sound capture device GUID. 
1665  *    pcGuidRenderDevice  [I] Address of sound render device GUID.
1666  *    pcDSCBufferDesc     [I] Address of capture buffer description.
1667  *    pcDSBufferDesc      [I] Address of  render buffer description.
1668  *    hWnd                [I] Handle to application window.
1669  *    dwLevel             [I] Cooperative level.
1670  *    ppDSFD              [O] Address where full duplex interface returned.
1671  *    ppDSCBuffer8        [0] Address where capture buffer interface returned.
1672  *    ppDSBuffer8         [0] Address where render buffer interface returned.
1673  *    pUnkOuter           [I] Must be NULL.
1674  *
1675  * RETURNS
1676  *    Success: DS_OK
1677  *    Failure: DSERR_NOAGGREGATION, DSERR_ALLOCATED, DSERR_INVALIDPARAM,
1678  *             DSERR_OUTOFMEMORY DSERR_INVALIDCALL DSERR_NODRIVER
1679  */
1680 HRESULT WINAPI 
1681 DirectSoundFullDuplexCreate8(
1682     LPCGUID pcGuidCaptureDevice,
1683     LPCGUID pcGuidRenderDevice,
1684     LPCDSCBUFFERDESC pcDSCBufferDesc,
1685     LPCDSBUFFERDESC pcDSBufferDesc,
1686     HWND hWnd,
1687     DWORD dwLevel,
1688     LPDIRECTSOUNDFULLDUPLEX *ppDSFD,
1689     LPDIRECTSOUNDCAPTUREBUFFER8 *ppDSCBuffer8,
1690     LPDIRECTSOUNDBUFFER8 *ppDSBuffer8,
1691     LPUNKNOWN pUnkOuter)
1692 {
1693     IDirectSoundFullDuplexImpl** ippDSFD=(IDirectSoundFullDuplexImpl**)ppDSFD;
1694     TRACE("(%s,%s,%p,%p,%lx,%lx,%p,%p,%p,%p)\n", debugstr_guid(pcGuidCaptureDevice), 
1695         debugstr_guid(pcGuidRenderDevice), pcDSCBufferDesc, pcDSBufferDesc,
1696         (DWORD)hWnd, dwLevel, ppDSFD, ppDSCBuffer8, ppDSBuffer8, pUnkOuter);
1697
1698     if ( pUnkOuter ) {
1699         WARN("pUnkOuter != 0\n");
1700         return DSERR_NOAGGREGATION;
1701     }
1702
1703     *ippDSFD = (IDirectSoundFullDuplexImpl*)HeapAlloc(GetProcessHeap(),
1704         HEAP_ZERO_MEMORY, sizeof(IDirectSoundFullDuplexImpl));
1705
1706     if (*ippDSFD == NULL) {
1707         WARN("out of memory\n");
1708         return DSERR_OUTOFMEMORY;
1709     } else {
1710         HRESULT hres;
1711         ICOM_THIS(IDirectSoundFullDuplexImpl, *ippDSFD);
1712
1713         This->ref = 1;
1714         This->lpVtbl = &dsfdvt;
1715
1716         InitializeCriticalSection( &(This->lock) );
1717
1718         hres = IDirectSoundFullDuplexImpl_Initialize( (LPDIRECTSOUNDFULLDUPLEX)This,
1719                                                       pcGuidCaptureDevice, pcGuidRenderDevice,
1720                                                       pcDSCBufferDesc, pcDSBufferDesc,
1721                                                       hWnd, dwLevel, ppDSCBuffer8, ppDSBuffer8);
1722         if (hres != DS_OK)
1723             WARN("IDirectSoundFullDuplexImpl_Initialize failed\n");
1724         return hres;
1725     }
1726
1727     return DSERR_GENERIC;
1728 }
1729
1730 static HRESULT WINAPI
1731 IDirectSoundFullDuplexImpl_QueryInterface(
1732     LPDIRECTSOUNDFULLDUPLEX iface,
1733     REFIID riid,
1734     LPVOID* ppobj )
1735 {
1736     ICOM_THIS(IDirectSoundFullDuplexImpl,iface);
1737     TRACE( "(%p,%s,%p)\n", This, debugstr_guid(riid), ppobj );
1738
1739     if (ppobj == NULL) {
1740         WARN("invalid parameter\n");
1741         return E_INVALIDARG;
1742     }
1743
1744     *ppobj = NULL;
1745     return E_NOINTERFACE;
1746 }
1747
1748 static ULONG WINAPI
1749 IDirectSoundFullDuplexImpl_AddRef( LPDIRECTSOUNDFULLDUPLEX iface )
1750 {
1751     ULONG uRef;
1752     ICOM_THIS(IDirectSoundFullDuplexImpl,iface);
1753     TRACE("(%p) ref was %ld, thread is %04lx\n",This, This->ref, GetCurrentThreadId());
1754
1755     EnterCriticalSection( &(This->lock) );
1756
1757     uRef = ++(This->ref);
1758
1759     LeaveCriticalSection( &(This->lock) );
1760
1761     return uRef;
1762 }
1763
1764 static ULONG WINAPI
1765 IDirectSoundFullDuplexImpl_Release( LPDIRECTSOUNDFULLDUPLEX iface )
1766 {
1767     ULONG uRef;
1768     ICOM_THIS(IDirectSoundFullDuplexImpl,iface);
1769     TRACE("(%p) ref was %ld, thread is %04lx\n",This, This->ref, GetCurrentThreadId());
1770
1771     EnterCriticalSection( &(This->lock) );
1772
1773     uRef = --(This->ref);
1774
1775     LeaveCriticalSection( &(This->lock) );
1776
1777     if ( uRef == 0 ) {
1778         DeleteCriticalSection( &(This->lock) );
1779         HeapFree( GetProcessHeap(), 0, This );
1780         TRACE("(%p) released\n",This);
1781     }
1782
1783     return uRef;
1784 }
1785
1786 static HRESULT WINAPI
1787 IDirectSoundFullDuplexImpl_Initialize(
1788     LPDIRECTSOUNDFULLDUPLEX iface,
1789     LPCGUID pCaptureGuid,
1790     LPCGUID pRendererGuid,
1791     LPCDSCBUFFERDESC lpDscBufferDesc,
1792     LPCDSBUFFERDESC lpDsBufferDesc,
1793     HWND hWnd,
1794     DWORD dwLevel,
1795     LPLPDIRECTSOUNDCAPTUREBUFFER8 lplpDirectSoundCaptureBuffer8,
1796     LPLPDIRECTSOUNDBUFFER8 lplpDirectSoundBuffer8 )
1797 {
1798     ICOM_THIS(IDirectSoundFullDuplexImpl,iface);
1799     IDirectSoundCaptureBufferImpl** ippdscb=(IDirectSoundCaptureBufferImpl**)lplpDirectSoundCaptureBuffer8;
1800     IDirectSoundBufferImpl** ippdsc=(IDirectSoundBufferImpl**)lplpDirectSoundBuffer8;
1801
1802     FIXME( "(%p,%s,%s,%p,%p,%lx,%lx,%p,%p) stub!\n", This, debugstr_guid(pCaptureGuid), 
1803         debugstr_guid(pRendererGuid), lpDscBufferDesc, lpDsBufferDesc, (DWORD)hWnd, dwLevel,
1804         ippdscb, ippdsc);
1805
1806     return E_FAIL;
1807 }
1808
1809 static ICOM_VTABLE(IDirectSoundFullDuplex) dsfdvt =
1810 {
1811     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1812     /* IUnknown methods */
1813     IDirectSoundFullDuplexImpl_QueryInterface,
1814     IDirectSoundFullDuplexImpl_AddRef,
1815     IDirectSoundFullDuplexImpl_Release,
1816
1817     /* IDirectSoundFullDuplex methods */
1818     IDirectSoundFullDuplexImpl_Initialize
1819 };
1820
1821 /*******************************************************************************
1822  * DirectSoundFullDuplex ClassFactory
1823  */
1824
1825 static HRESULT WINAPI
1826 DSFDCF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj)
1827 {
1828     ICOM_THIS(IClassFactoryImpl,iface);
1829
1830     FIXME("(%p)->(%s,%p),stub!\n",This,debugstr_guid(riid),ppobj);
1831     return E_NOINTERFACE;
1832 }
1833
1834 static ULONG WINAPI
1835 DSFDCF_AddRef(LPCLASSFACTORY iface)
1836 {
1837     ICOM_THIS(IClassFactoryImpl,iface);
1838     TRACE("(%p) ref was %ld\n", This, This->ref);
1839     return ++(This->ref);
1840 }
1841
1842 static ULONG WINAPI 
1843 DSFDCF_Release(LPCLASSFACTORY iface) 
1844 {
1845     ICOM_THIS(IClassFactoryImpl,iface);
1846     /* static class, won't be  freed */
1847     TRACE("(%p) ref was %ld\n", This, This->ref);
1848     return --(This->ref);
1849 }
1850
1851 static HRESULT WINAPI 
1852 DSFDCF_CreateInstance(
1853     LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj )
1854 {
1855     ICOM_THIS(IClassFactoryImpl,iface);
1856
1857     TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
1858
1859     if (ppobj == NULL) {
1860         WARN("invalid parameter\n");
1861         return E_INVALIDARG;
1862     }
1863
1864     *ppobj = NULL;
1865
1866     if ( IsEqualGUID( &IID_IDirectSoundFullDuplex, riid ) ) {
1867         /* FIXME: how do we do this one ? */
1868         FIXME("not implemented\n");
1869         return E_NOINTERFACE;
1870     }
1871
1872     WARN("(%p,%p,%s,%p) Interface not found!\n",This,pOuter,debugstr_guid(riid),ppobj); 
1873     return E_NOINTERFACE;
1874 }
1875
1876 static HRESULT WINAPI 
1877 DSFDCF_LockServer(LPCLASSFACTORY iface,BOOL dolock)
1878 {
1879     ICOM_THIS(IClassFactoryImpl,iface);
1880     FIXME("(%p)->(%d),stub!\n",This,dolock);
1881     return S_OK;
1882 }
1883
1884 static ICOM_VTABLE(IClassFactory) DSFDCF_Vtbl =
1885 {
1886     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1887     DSFDCF_QueryInterface,
1888     DSFDCF_AddRef,
1889     DSFDCF_Release,
1890     DSFDCF_CreateInstance,
1891     DSFDCF_LockServer
1892 };
1893
1894 IClassFactoryImpl DSOUND_FULLDUPLEX_CF = { &DSFDCF_Vtbl, 1 };