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