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