msctf: Implement ITfInputProcessorProfiles::GetActiveLanguageProfile.
[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(UlongToHandle(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(UlongToHandle(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(UlongToHandle(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(UlongToHandle(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(&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     int 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(HWAVEIN hwi, UINT msg, DWORD_PTR dwUser, DWORD_PTR dw1,
414                         DWORD_PTR dw2)
415 {
416     DirectSoundCaptureDevice * This = (DirectSoundCaptureDevice*)dwUser;
417     IDirectSoundCaptureBufferImpl * Moi = This->capture_buffer;
418     TRACE("(%p,%08x(%s),%08lx,%08lx,%08lx) entering at %d\n",hwi,msg,
419         msg == MM_WIM_OPEN ? "MM_WIM_OPEN" : msg == MM_WIM_CLOSE ? "MM_WIM_CLOSE" :
420         msg == MM_WIM_DATA ? "MM_WIM_DATA" : "UNKNOWN",dwUser,dw1,dw2,GetTickCount());
421
422     if (msg == MM_WIM_DATA) {
423         EnterCriticalSection( &(This->lock) );
424         TRACE("DirectSoundCapture msg=MM_WIM_DATA, old This->state=%s, old This->index=%d\n",
425             captureStateString[This->state],This->index);
426         if (This->state != STATE_STOPPED) {
427             int index = This->index;
428             if (This->state == STATE_STARTING)
429                 This->state = STATE_CAPTURING;
430             capture_CheckNotify(Moi, (DWORD_PTR)This->pwave[index].lpData - (DWORD_PTR)This->buffer, This->pwave[index].dwBufferLength);
431             This->index = (++This->index) % This->nrofpwaves;
432             if ( (This->index == 0) && !(This->capture_buffer->flags & DSCBSTART_LOOPING) ) {
433                 TRACE("end of buffer\n");
434                 This->state = STATE_STOPPED;
435                 capture_CheckNotify(Moi, 0, 0);
436             } else {
437                 if (This->state == STATE_CAPTURING) {
438                     waveInUnprepareHeader(hwi, &(This->pwave[index]), sizeof(WAVEHDR));
439                     waveInPrepareHeader(hwi, &(This->pwave[index]), sizeof(WAVEHDR));
440                     waveInAddBuffer(hwi, &(This->pwave[index]), sizeof(WAVEHDR));
441                 } else if (This->state == STATE_STOPPING) {
442                     TRACE("stopping\n");
443                     This->state = STATE_STOPPED;
444                 }
445             }
446         }
447         TRACE("DirectSoundCapture new This->state=%s, new This->index=%d\n",
448             captureStateString[This->state],This->index);
449         LeaveCriticalSection( &(This->lock) );
450     }
451
452     TRACE("completed\n");
453 }
454
455 /***************************************************************************
456  * IDirectSoundCaptureImpl
457  */
458 static HRESULT WINAPI
459 IDirectSoundCaptureImpl_QueryInterface(
460     LPDIRECTSOUNDCAPTURE iface,
461     REFIID riid,
462     LPVOID* ppobj )
463 {
464     IDirectSoundCaptureImpl *This = (IDirectSoundCaptureImpl *)iface;
465     TRACE( "(%p,%s,%p)\n", This, debugstr_guid(riid), ppobj );
466
467     if (ppobj == NULL) {
468         WARN("invalid parameter\n");
469         return E_INVALIDARG;
470     }
471
472     *ppobj = NULL;
473
474     if (IsEqualIID(riid, &IID_IUnknown)) {
475         IDirectSoundCapture_AddRef((LPDIRECTSOUNDCAPTURE)This);
476         *ppobj = This;
477         return DS_OK;
478     } else if (IsEqualIID(riid, &IID_IDirectSoundCapture)) {
479         IDirectSoundCapture_AddRef((LPDIRECTSOUNDCAPTURE)This);
480         *ppobj = This;
481         return DS_OK;
482     }
483
484     WARN("unsupported riid: %s\n", debugstr_guid(riid));
485     return E_NOINTERFACE;
486 }
487
488 static ULONG WINAPI
489 IDirectSoundCaptureImpl_AddRef( LPDIRECTSOUNDCAPTURE iface )
490 {
491     IDirectSoundCaptureImpl *This = (IDirectSoundCaptureImpl *)iface;
492     ULONG ref = InterlockedIncrement(&(This->ref));
493     TRACE("(%p) ref was %d\n", This, ref - 1);
494     return ref;
495 }
496
497 static ULONG WINAPI
498 IDirectSoundCaptureImpl_Release( LPDIRECTSOUNDCAPTURE iface )
499 {
500     IDirectSoundCaptureImpl *This = (IDirectSoundCaptureImpl *)iface;
501     ULONG ref = InterlockedDecrement(&(This->ref));
502     TRACE("(%p) ref was %d\n", This, ref + 1);
503
504     if (!ref) {
505         if (This->device)
506             DirectSoundCaptureDevice_Release(This->device);
507
508         HeapFree( GetProcessHeap(), 0, This );
509         TRACE("(%p) released\n", This);
510     }
511     return ref;
512 }
513
514 HRESULT WINAPI IDirectSoundCaptureImpl_CreateCaptureBuffer(
515     LPDIRECTSOUNDCAPTURE iface,
516     LPCDSCBUFFERDESC lpcDSCBufferDesc,
517     LPDIRECTSOUNDCAPTUREBUFFER* lplpDSCaptureBuffer,
518     LPUNKNOWN pUnk )
519 {
520     HRESULT hr;
521     IDirectSoundCaptureImpl *This = (IDirectSoundCaptureImpl *)iface;
522
523     TRACE( "(%p,%p,%p,%p)\n",iface,lpcDSCBufferDesc,lplpDSCaptureBuffer,pUnk);
524
525     if (lpcDSCBufferDesc == NULL) {
526         WARN("invalid parameter: lpcDSCBufferDesc == NULL)\n");
527         return DSERR_INVALIDPARAM;
528     }
529
530     if (lplpDSCaptureBuffer == NULL) {
531         WARN("invalid parameter: lplpDSCaptureBuffer == NULL\n");
532         return DSERR_INVALIDPARAM;
533     }
534
535     if (pUnk != NULL) {
536         WARN("invalid parameter: pUnk != NULL\n");
537         return DSERR_INVALIDPARAM;
538     }
539
540     /* FIXME: We can only have one buffer so what do we do here? */
541     if (This->device->capture_buffer) {
542         WARN("lnvalid parameter: already has buffer\n");
543         return DSERR_INVALIDPARAM;    /* DSERR_GENERIC ? */
544     }
545
546     hr = IDirectSoundCaptureBufferImpl_Create(This->device,
547         (IDirectSoundCaptureBufferImpl **)lplpDSCaptureBuffer, lpcDSCBufferDesc);
548
549     if (hr != DS_OK)
550         WARN("IDirectSoundCaptureBufferImpl_Create failed\n");
551
552     return hr;
553 }
554
555 HRESULT WINAPI IDirectSoundCaptureImpl_GetCaps(
556     LPDIRECTSOUNDCAPTURE iface,
557     LPDSCCAPS lpDSCCaps )
558 {
559     IDirectSoundCaptureImpl *This = (IDirectSoundCaptureImpl *)iface;
560     TRACE("(%p,%p)\n",This,lpDSCCaps);
561
562     if (This->device == NULL) {
563         WARN("not initialized\n");
564         return DSERR_UNINITIALIZED;
565     }
566
567     if (lpDSCCaps== NULL) {
568         WARN("invalid parameter: lpDSCCaps== NULL\n");
569         return DSERR_INVALIDPARAM;
570     }
571
572     if (lpDSCCaps->dwSize < sizeof(*lpDSCCaps)) {
573         WARN("invalid parameter: lpDSCCaps->dwSize = %d\n", lpDSCCaps->dwSize);
574         return DSERR_INVALIDPARAM;
575     }
576
577     lpDSCCaps->dwFlags = This->device->drvcaps.dwFlags;
578     lpDSCCaps->dwFormats = This->device->drvcaps.dwFormats;
579     lpDSCCaps->dwChannels = This->device->drvcaps.dwChannels;
580
581     TRACE("(flags=0x%08x,format=0x%08x,channels=%d)\n",lpDSCCaps->dwFlags,
582         lpDSCCaps->dwFormats, lpDSCCaps->dwChannels);
583
584     return DS_OK;
585 }
586
587 HRESULT WINAPI IDirectSoundCaptureImpl_Initialize(
588     LPDIRECTSOUNDCAPTURE iface,
589     LPCGUID lpcGUID )
590 {
591     IDirectSoundCaptureImpl *This = (IDirectSoundCaptureImpl *)iface;
592     TRACE("(%p,%s)\n", This, debugstr_guid(lpcGUID));
593
594     if (This->device != NULL) {
595         WARN("already initialized\n");
596         return DSERR_ALREADYINITIALIZED;
597     }
598     return DirectSoundCaptureDevice_Initialize(&This->device, lpcGUID);
599 }
600
601 static const IDirectSoundCaptureVtbl dscvt =
602 {
603     /* IUnknown methods */
604     IDirectSoundCaptureImpl_QueryInterface,
605     IDirectSoundCaptureImpl_AddRef,
606     IDirectSoundCaptureImpl_Release,
607
608     /* IDirectSoundCapture methods */
609     IDirectSoundCaptureImpl_CreateCaptureBuffer,
610     IDirectSoundCaptureImpl_GetCaps,
611     IDirectSoundCaptureImpl_Initialize
612 };
613
614 static HRESULT IDirectSoundCaptureImpl_Create(
615     LPDIRECTSOUNDCAPTURE8 * ppDSC)
616 {
617     IDirectSoundCaptureImpl *pDSC;
618     TRACE("(%p)\n", ppDSC);
619
620     /* Allocate memory */
621     pDSC = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(IDirectSoundCaptureImpl));
622     if (pDSC == NULL) {
623         WARN("out of memory\n");
624         *ppDSC = NULL;
625         return DSERR_OUTOFMEMORY;
626     }
627
628     pDSC->lpVtbl = &dscvt;
629     pDSC->ref    = 0;
630     pDSC->device = NULL;
631
632     *ppDSC = (LPDIRECTSOUNDCAPTURE8)pDSC;
633
634     return DS_OK;
635 }
636
637 /*******************************************************************************
638  *              IDirectSoundCaptureNotify
639  */
640 static HRESULT WINAPI IDirectSoundCaptureNotifyImpl_QueryInterface(
641     LPDIRECTSOUNDNOTIFY iface,
642     REFIID riid,
643     LPVOID *ppobj)
644 {
645     IDirectSoundCaptureNotifyImpl *This = (IDirectSoundCaptureNotifyImpl *)iface;
646     TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
647
648     if (This->dscb == NULL) {
649         WARN("invalid parameter\n");
650         return E_INVALIDARG;
651     }
652
653     return IDirectSoundCaptureBuffer_QueryInterface((LPDIRECTSOUNDCAPTUREBUFFER)This->dscb, riid, ppobj);
654 }
655
656 static ULONG WINAPI IDirectSoundCaptureNotifyImpl_AddRef(LPDIRECTSOUNDNOTIFY iface)
657 {
658     IDirectSoundCaptureNotifyImpl *This = (IDirectSoundCaptureNotifyImpl *)iface;
659     ULONG ref = InterlockedIncrement(&(This->ref));
660     TRACE("(%p) ref was %d\n", This, ref - 1);
661     return ref;
662 }
663
664 static ULONG WINAPI IDirectSoundCaptureNotifyImpl_Release(LPDIRECTSOUNDNOTIFY iface)
665 {
666     IDirectSoundCaptureNotifyImpl *This = (IDirectSoundCaptureNotifyImpl *)iface;
667     ULONG ref = InterlockedDecrement(&(This->ref));
668     TRACE("(%p) ref was %d\n", This, ref + 1);
669
670     if (!ref) {
671         if (This->dscb->hwnotify)
672             IDsDriverNotify_Release(This->dscb->hwnotify);
673         This->dscb->notify=NULL;
674         IDirectSoundCaptureBuffer_Release((LPDIRECTSOUNDCAPTUREBUFFER)This->dscb);
675         HeapFree(GetProcessHeap(),0,This);
676         TRACE("(%p) released\n", This);
677     }
678     return ref;
679 }
680
681 static HRESULT WINAPI IDirectSoundCaptureNotifyImpl_SetNotificationPositions(
682     LPDIRECTSOUNDNOTIFY iface,
683     DWORD howmuch,
684     LPCDSBPOSITIONNOTIFY notify)
685 {
686     IDirectSoundCaptureNotifyImpl *This = (IDirectSoundCaptureNotifyImpl *)iface;
687     TRACE("(%p,0x%08x,%p)\n",This,howmuch,notify);
688
689     if (howmuch > 0 && notify == NULL) {
690         WARN("invalid parameter: notify == NULL\n");
691         return DSERR_INVALIDPARAM;
692     }
693
694     if (TRACE_ON(dsound)) {
695         unsigned int i;
696         for (i=0;i<howmuch;i++)
697             TRACE("notify at %d to %p\n",
698             notify[i].dwOffset,notify[i].hEventNotify);
699     }
700
701     if (This->dscb->hwnotify) {
702         HRESULT hres;
703         hres = IDsDriverNotify_SetNotificationPositions(This->dscb->hwnotify, howmuch, notify);
704         if (hres != DS_OK)
705             WARN("IDsDriverNotify_SetNotificationPositions failed\n");
706         return hres;
707     } else if (howmuch > 0) {
708         /* Make an internal copy of the caller-supplied array.
709          * Replace the existing copy if one is already present. */
710         if (This->dscb->notifies)
711             This->dscb->notifies = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
712                 This->dscb->notifies, howmuch * sizeof(DSBPOSITIONNOTIFY));
713         else
714             This->dscb->notifies = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
715                 howmuch * sizeof(DSBPOSITIONNOTIFY));
716
717         if (This->dscb->notifies == NULL) {
718             WARN("out of memory\n");
719             return DSERR_OUTOFMEMORY;
720         }
721         CopyMemory(This->dscb->notifies, notify, howmuch * sizeof(DSBPOSITIONNOTIFY));
722         This->dscb->nrofnotifies = howmuch;
723     } else {
724         HeapFree(GetProcessHeap(), 0, This->dscb->notifies);
725         This->dscb->notifies = NULL;
726         This->dscb->nrofnotifies = 0;
727     }
728
729     return S_OK;
730 }
731
732 static const IDirectSoundNotifyVtbl dscnvt =
733 {
734     IDirectSoundCaptureNotifyImpl_QueryInterface,
735     IDirectSoundCaptureNotifyImpl_AddRef,
736     IDirectSoundCaptureNotifyImpl_Release,
737     IDirectSoundCaptureNotifyImpl_SetNotificationPositions,
738 };
739
740 static HRESULT IDirectSoundCaptureNotifyImpl_Create(
741     IDirectSoundCaptureBufferImpl *dscb,
742     IDirectSoundCaptureNotifyImpl **pdscn)
743 {
744     IDirectSoundCaptureNotifyImpl * dscn;
745     TRACE("(%p,%p)\n",dscb,pdscn);
746
747     dscn = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*dscn));
748
749     if (dscn == NULL) {
750         WARN("out of memory\n");
751         return DSERR_OUTOFMEMORY;
752     }
753
754     dscn->ref = 0;
755     dscn->lpVtbl = &dscnvt;
756     dscn->dscb = dscb;
757     dscb->notify = dscn;
758     IDirectSoundCaptureBuffer_AddRef((LPDIRECTSOUNDCAPTUREBUFFER)dscb);
759
760     *pdscn = dscn;
761     return DS_OK;
762 }
763
764 /*******************************************************************************
765  *              IDirectSoundCaptureBuffer
766  */
767 static HRESULT WINAPI
768 IDirectSoundCaptureBufferImpl_QueryInterface(
769     LPDIRECTSOUNDCAPTUREBUFFER8 iface,
770     REFIID riid,
771     LPVOID* ppobj )
772 {
773     IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
774     HRESULT hres;
775     TRACE( "(%p,%s,%p)\n", This, debugstr_guid(riid), ppobj );
776
777     if (ppobj == NULL) {
778         WARN("invalid parameter\n");
779         return E_INVALIDARG;
780     }
781
782     *ppobj = NULL;
783
784     if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ) {
785         if (!This->notify)
786             hres = IDirectSoundCaptureNotifyImpl_Create(This, &This->notify);
787         if (This->notify) {
788             IDirectSoundNotify_AddRef((LPDIRECTSOUNDNOTIFY)This->notify);
789             if (This->device->hwbuf && !This->hwnotify) {
790                 hres = IDsCaptureDriverBuffer_QueryInterface(This->device->hwbuf,
791                     &IID_IDsDriverNotify, (LPVOID*)&(This->hwnotify));
792                 if (hres != DS_OK) {
793                     WARN("IDsCaptureDriverBuffer_QueryInterface failed\n");
794                     IDirectSoundNotify_Release((LPDIRECTSOUNDNOTIFY)This->notify);
795                     *ppobj = 0;
796                     return hres;
797                 }
798             }
799
800             *ppobj = This->notify;
801             return DS_OK;
802         }
803
804         WARN("IID_IDirectSoundNotify\n");
805         return E_FAIL;
806     }
807
808     if ( IsEqualGUID( &IID_IDirectSoundCaptureBuffer, riid ) ||
809          IsEqualGUID( &IID_IDirectSoundCaptureBuffer8, riid ) ) {
810         IDirectSoundCaptureBuffer8_AddRef(iface);
811         *ppobj = This;
812         return NO_ERROR;
813     }
814
815     FIXME("(%p,%s,%p) unsupported GUID\n", This, debugstr_guid(riid), ppobj);
816     return E_NOINTERFACE;
817 }
818
819 static ULONG WINAPI
820 IDirectSoundCaptureBufferImpl_AddRef( LPDIRECTSOUNDCAPTUREBUFFER8 iface )
821 {
822     IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
823     ULONG ref = InterlockedIncrement(&(This->ref));
824     TRACE("(%p) ref was %d\n", This, ref - 1);
825     return ref;
826 }
827
828 static ULONG WINAPI
829 IDirectSoundCaptureBufferImpl_Release( LPDIRECTSOUNDCAPTUREBUFFER8 iface )
830 {
831     IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
832     ULONG ref = InterlockedDecrement(&(This->ref));
833     TRACE("(%p) ref was %d\n", This, ref + 1);
834
835     if (!ref) {
836         TRACE("deleting object\n");
837         if (This->device->state == STATE_CAPTURING)
838             This->device->state = STATE_STOPPING;
839
840         HeapFree(GetProcessHeap(),0, This->pdscbd);
841
842         if (This->device->hwi) {
843             waveInReset(This->device->hwi);
844             waveInClose(This->device->hwi);
845             HeapFree(GetProcessHeap(),0, This->device->pwave);
846             This->device->pwave = 0;
847             This->device->hwi = 0;
848         }
849
850         if (This->device->hwbuf)
851             IDsCaptureDriverBuffer_Release(This->device->hwbuf);
852
853         /* remove from DirectSoundCaptureDevice */
854         This->device->capture_buffer = NULL;
855
856         if (This->notify)
857             IDirectSoundNotify_Release((LPDIRECTSOUNDNOTIFY)This->notify);
858
859         /* If driver manages its own buffer, IDsCaptureDriverBuffer_Release
860            should have freed the buffer. Prevent freeing it again in
861            IDirectSoundCaptureBufferImpl_Create */
862         if (!(This->device->drvdesc.dwFlags & DSDDESC_USESYSTEMMEMORY))
863             This->device->buffer = NULL;
864
865         HeapFree(GetProcessHeap(), 0, This->notifies);
866         HeapFree( GetProcessHeap(), 0, This );
867         TRACE("(%p) released\n", This);
868     }
869     return ref;
870 }
871
872 static HRESULT WINAPI
873 IDirectSoundCaptureBufferImpl_GetCaps(
874     LPDIRECTSOUNDCAPTUREBUFFER8 iface,
875     LPDSCBCAPS lpDSCBCaps )
876 {
877     IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
878     TRACE( "(%p,%p)\n", This, lpDSCBCaps );
879
880     if (lpDSCBCaps == NULL) {
881         WARN("invalid parameter: lpDSCBCaps == NULL\n");
882         return DSERR_INVALIDPARAM;
883     }
884
885     if (lpDSCBCaps->dwSize < sizeof(DSCBCAPS)) {
886         WARN("invalid parameter: lpDSCBCaps->dwSize = %d\n", lpDSCBCaps->dwSize);
887         return DSERR_INVALIDPARAM;
888     }
889
890     if (This->device == NULL) {
891         WARN("invalid parameter: This->device == NULL\n");
892         return DSERR_INVALIDPARAM;
893     }
894
895     lpDSCBCaps->dwSize = sizeof(DSCBCAPS);
896     lpDSCBCaps->dwFlags = This->flags;
897     lpDSCBCaps->dwBufferBytes = This->pdscbd->dwBufferBytes;
898     lpDSCBCaps->dwReserved = 0;
899
900     TRACE("returning DS_OK\n");
901     return DS_OK;
902 }
903
904 static HRESULT WINAPI
905 IDirectSoundCaptureBufferImpl_GetCurrentPosition(
906     LPDIRECTSOUNDCAPTUREBUFFER8 iface,
907     LPDWORD lpdwCapturePosition,
908     LPDWORD lpdwReadPosition )
909 {
910     IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
911     HRESULT hres = DS_OK;
912     TRACE( "(%p,%p,%p)\n", This, lpdwCapturePosition, lpdwReadPosition );
913
914     if (This->device == NULL) {
915         WARN("invalid parameter: This->device == NULL\n");
916         return DSERR_INVALIDPARAM;
917     }
918
919     if (This->device->driver) {
920         hres = IDsCaptureDriverBuffer_GetPosition(This->device->hwbuf, lpdwCapturePosition, lpdwReadPosition );
921         if (hres != DS_OK)
922             WARN("IDsCaptureDriverBuffer_GetPosition failed\n");
923     } else if (This->device->hwi) {
924         DWORD pos;
925
926         EnterCriticalSection(&This->device->lock);
927         pos = (DWORD_PTR)This->device->pwave[This->device->index].lpData - (DWORD_PTR)This->device->buffer;
928         if (lpdwCapturePosition)
929             *lpdwCapturePosition = (This->device->pwave[This->device->index].dwBufferLength + pos) % This->device->buflen;
930         if (lpdwReadPosition)
931             *lpdwReadPosition = pos;
932         LeaveCriticalSection(&This->device->lock);
933
934     } else {
935         WARN("no driver\n");
936         hres = DSERR_NODRIVER;
937     }
938
939     TRACE("cappos=%d readpos=%d\n", (lpdwCapturePosition?*lpdwCapturePosition:-1), (lpdwReadPosition?*lpdwReadPosition:-1));
940     TRACE("returning %08x\n", hres);
941     return hres;
942 }
943
944 static HRESULT WINAPI
945 IDirectSoundCaptureBufferImpl_GetFormat(
946     LPDIRECTSOUNDCAPTUREBUFFER8 iface,
947     LPWAVEFORMATEX lpwfxFormat,
948     DWORD dwSizeAllocated,
949     LPDWORD lpdwSizeWritten )
950 {
951     IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
952     HRESULT hres = DS_OK;
953     TRACE( "(%p,%p,0x%08x,%p)\n", This, lpwfxFormat, dwSizeAllocated,
954         lpdwSizeWritten );
955
956     if (This->device == NULL) {
957         WARN("invalid parameter: This->device == NULL\n");
958         return DSERR_INVALIDPARAM;
959     }
960
961     if (dwSizeAllocated > (sizeof(WAVEFORMATEX) + This->device->pwfx->cbSize))
962         dwSizeAllocated = sizeof(WAVEFORMATEX) + This->device->pwfx->cbSize;
963
964     if (lpwfxFormat) { /* NULL is valid (just want size) */
965         CopyMemory(lpwfxFormat, This->device->pwfx, dwSizeAllocated);
966         if (lpdwSizeWritten)
967             *lpdwSizeWritten = dwSizeAllocated;
968     } else {
969         if (lpdwSizeWritten)
970             *lpdwSizeWritten = sizeof(WAVEFORMATEX) + This->device->pwfx->cbSize;
971         else {
972             TRACE("invalid parameter: lpdwSizeWritten = NULL\n");
973             hres = DSERR_INVALIDPARAM;
974         }
975     }
976
977     TRACE("returning %08x\n", hres);
978     return hres;
979 }
980
981 static HRESULT WINAPI
982 IDirectSoundCaptureBufferImpl_GetStatus(
983     LPDIRECTSOUNDCAPTUREBUFFER8 iface,
984     LPDWORD lpdwStatus )
985 {
986     IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
987     TRACE( "(%p, %p), thread is %04x\n", This, lpdwStatus, GetCurrentThreadId() );
988
989     if (This->device == NULL) {
990         WARN("invalid parameter: This->device == NULL\n");
991         return DSERR_INVALIDPARAM;
992     }
993
994     if (lpdwStatus == NULL) {
995         WARN("invalid parameter: lpdwStatus == NULL\n");
996         return DSERR_INVALIDPARAM;
997     }
998
999     *lpdwStatus = 0;
1000     EnterCriticalSection(&(This->device->lock));
1001
1002     TRACE("old This->device->state=%s, old lpdwStatus=%08x\n",
1003         captureStateString[This->device->state],*lpdwStatus);
1004     if ((This->device->state == STATE_STARTING) ||
1005         (This->device->state == STATE_CAPTURING)) {
1006         *lpdwStatus |= DSCBSTATUS_CAPTURING;
1007         if (This->flags & DSCBSTART_LOOPING)
1008             *lpdwStatus |= DSCBSTATUS_LOOPING;
1009     }
1010     TRACE("new This->device->state=%s, new lpdwStatus=%08x\n",
1011         captureStateString[This->device->state],*lpdwStatus);
1012     LeaveCriticalSection(&(This->device->lock));
1013
1014     TRACE("status=%x\n", *lpdwStatus);
1015     TRACE("returning DS_OK\n");
1016     return DS_OK;
1017 }
1018
1019 static HRESULT WINAPI
1020 IDirectSoundCaptureBufferImpl_Initialize(
1021     LPDIRECTSOUNDCAPTUREBUFFER8 iface,
1022     LPDIRECTSOUNDCAPTURE lpDSC,
1023     LPCDSCBUFFERDESC lpcDSCBDesc )
1024 {
1025     IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
1026
1027     FIXME( "(%p,%p,%p): stub\n", This, lpDSC, lpcDSCBDesc );
1028
1029     return DS_OK;
1030 }
1031
1032 static HRESULT WINAPI
1033 IDirectSoundCaptureBufferImpl_Lock(
1034     LPDIRECTSOUNDCAPTUREBUFFER8 iface,
1035     DWORD dwReadCusor,
1036     DWORD dwReadBytes,
1037     LPVOID* lplpvAudioPtr1,
1038     LPDWORD lpdwAudioBytes1,
1039     LPVOID* lplpvAudioPtr2,
1040     LPDWORD lpdwAudioBytes2,
1041     DWORD dwFlags )
1042 {
1043     HRESULT hres = DS_OK;
1044     IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
1045     TRACE( "(%p,%08u,%08u,%p,%p,%p,%p,0x%08x) at %d\n", This, dwReadCusor,
1046         dwReadBytes, lplpvAudioPtr1, lpdwAudioBytes1, lplpvAudioPtr2,
1047         lpdwAudioBytes2, dwFlags, GetTickCount() );
1048
1049     if (This->device == NULL) {
1050         WARN("invalid parameter: This->device == NULL\n");
1051         return DSERR_INVALIDPARAM;
1052     }
1053
1054     if (lplpvAudioPtr1 == NULL) {
1055         WARN("invalid parameter: lplpvAudioPtr1 == NULL\n");
1056         return DSERR_INVALIDPARAM;
1057     }
1058
1059     if (lpdwAudioBytes1 == NULL) {
1060         WARN("invalid parameter: lpdwAudioBytes1 == NULL\n");
1061         return DSERR_INVALIDPARAM;
1062     }
1063
1064     EnterCriticalSection(&(This->device->lock));
1065
1066     if (This->device->driver) {
1067         hres = IDsCaptureDriverBuffer_Lock(This->device->hwbuf, lplpvAudioPtr1,
1068                                            lpdwAudioBytes1, lplpvAudioPtr2,
1069                                            lpdwAudioBytes2, dwReadCusor,
1070                                            dwReadBytes, dwFlags);
1071         if (hres != DS_OK)
1072             WARN("IDsCaptureDriverBuffer_Lock failed\n");
1073     } else if (This->device->hwi) {
1074         *lplpvAudioPtr1 = This->device->buffer + dwReadCusor;
1075         if ( (dwReadCusor + dwReadBytes) > This->device->buflen) {
1076             *lpdwAudioBytes1 = This->device->buflen - dwReadCusor;
1077             if (lplpvAudioPtr2)
1078                 *lplpvAudioPtr2 = This->device->buffer;
1079             if (lpdwAudioBytes2)
1080                 *lpdwAudioBytes2 = dwReadBytes - *lpdwAudioBytes1;
1081         } else {
1082             *lpdwAudioBytes1 = dwReadBytes;
1083             if (lplpvAudioPtr2)
1084                 *lplpvAudioPtr2 = 0;
1085             if (lpdwAudioBytes2)
1086                 *lpdwAudioBytes2 = 0;
1087         }
1088     } else {
1089         TRACE("invalid call\n");
1090         hres = DSERR_INVALIDCALL;   /* DSERR_NODRIVER ? */
1091     }
1092
1093     LeaveCriticalSection(&(This->device->lock));
1094
1095     TRACE("returning %08x\n", hres);
1096     return hres;
1097 }
1098
1099 static HRESULT WINAPI
1100 IDirectSoundCaptureBufferImpl_Start(
1101     LPDIRECTSOUNDCAPTUREBUFFER8 iface,
1102     DWORD dwFlags )
1103 {
1104     HRESULT hres = DS_OK;
1105     IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
1106     TRACE( "(%p,0x%08x)\n", This, dwFlags );
1107
1108     if (This->device == NULL) {
1109         WARN("invalid parameter: This->device == NULL\n");
1110         return DSERR_INVALIDPARAM;
1111     }
1112
1113     if ( (This->device->driver == 0) && (This->device->hwi == 0) ) {
1114         WARN("no driver\n");
1115         return DSERR_NODRIVER;
1116     }
1117
1118     EnterCriticalSection(&(This->device->lock));
1119
1120     This->flags = dwFlags;
1121     TRACE("old This->state=%s\n",captureStateString[This->device->state]);
1122     if (This->device->state == STATE_STOPPED)
1123         This->device->state = STATE_STARTING;
1124     else if (This->device->state == STATE_STOPPING)
1125         This->device->state = STATE_CAPTURING;
1126     TRACE("new This->device->state=%s\n",captureStateString[This->device->state]);
1127
1128     LeaveCriticalSection(&(This->device->lock));
1129
1130     if (This->device->driver) {
1131         hres = IDsCaptureDriverBuffer_Start(This->device->hwbuf, dwFlags);
1132         if (hres != DS_OK)
1133             WARN("IDsCaptureDriverBuffer_Start failed\n");
1134     } else if (This->device->hwi) {
1135         DirectSoundCaptureDevice *device = This->device;
1136
1137         if (device->buffer) {
1138             int c;
1139             DWORD blocksize = DSOUND_fraglen(device->pwfx->nSamplesPerSec, device->pwfx->nBlockAlign);
1140             device->nrofpwaves = device->buflen / blocksize + !!(device->buflen % blocksize);
1141             TRACE("nrofpwaves=%d\n", device->nrofpwaves);
1142
1143             /* prepare headers */
1144             if (device->pwave)
1145                 device->pwave = HeapReAlloc(GetProcessHeap(), 0,device->pwave, device->nrofpwaves*sizeof(WAVEHDR));
1146             else
1147                 device->pwave = HeapAlloc(GetProcessHeap(), 0, device->nrofpwaves*sizeof(WAVEHDR));
1148
1149             for (c = 0; c < device->nrofpwaves; ++c) {
1150                 device->pwave[c].lpData = (char *)device->buffer + c * blocksize;
1151                 if (c + 1 == device->nrofpwaves)
1152                     device->pwave[c].dwBufferLength = device->buflen - c * blocksize;
1153                 else
1154                     device->pwave[c].dwBufferLength = blocksize;
1155                 device->pwave[c].dwBytesRecorded = 0;
1156                 device->pwave[c].dwUser = (DWORD_PTR)device;
1157                 device->pwave[c].dwFlags = 0;
1158                 device->pwave[c].dwLoops = 0;
1159                 hres = mmErr(waveInPrepareHeader(device->hwi, &(device->pwave[c]),sizeof(WAVEHDR)));
1160                 if (hres != DS_OK) {
1161                     WARN("waveInPrepareHeader failed\n");
1162                     while (c--)
1163                         waveInUnprepareHeader(device->hwi, &(device->pwave[c]),sizeof(WAVEHDR));
1164                     break;
1165                 }
1166
1167                 hres = mmErr(waveInAddBuffer(device->hwi, &(device->pwave[c]), sizeof(WAVEHDR)));
1168                 if (hres != DS_OK) {
1169                     WARN("waveInAddBuffer failed\n");
1170                     while (c--)
1171                         waveInUnprepareHeader(device->hwi, &(device->pwave[c]),sizeof(WAVEHDR));
1172                     break;
1173                 }
1174             }
1175
1176             FillMemory(device->buffer, device->buflen, (device->pwfx->wBitsPerSample == 8) ? 128 : 0);
1177         }
1178
1179         device->index = 0;
1180
1181         if (hres == DS_OK) {
1182             /* start filling the first buffer */
1183             hres = mmErr(waveInStart(device->hwi));
1184             if (hres != DS_OK)
1185                 WARN("waveInStart failed\n");
1186         }
1187
1188         if (hres != DS_OK) {
1189             WARN("calling waveInClose because of error\n");
1190             waveInClose(device->hwi);
1191             device->hwi = 0;
1192         }
1193     } else {
1194         WARN("no driver\n");
1195         hres = DSERR_NODRIVER;
1196     }
1197
1198     TRACE("returning %08x\n", hres);
1199     return hres;
1200 }
1201
1202 static HRESULT WINAPI
1203 IDirectSoundCaptureBufferImpl_Stop( LPDIRECTSOUNDCAPTUREBUFFER8 iface )
1204 {
1205     HRESULT hres = DS_OK;
1206     IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
1207     TRACE( "(%p)\n", This );
1208
1209     if (This->device == NULL) {
1210         WARN("invalid parameter: This->device == NULL\n");
1211         return DSERR_INVALIDPARAM;
1212     }
1213
1214     EnterCriticalSection(&(This->device->lock));
1215
1216     TRACE("old This->device->state=%s\n",captureStateString[This->device->state]);
1217     if (This->device->state == STATE_CAPTURING)
1218         This->device->state = STATE_STOPPING;
1219     else if (This->device->state == STATE_STARTING)
1220         This->device->state = STATE_STOPPED;
1221     TRACE("new This->device->state=%s\n",captureStateString[This->device->state]);
1222
1223     LeaveCriticalSection(&(This->device->lock));
1224
1225     if (This->device->driver) {
1226         hres = IDsCaptureDriverBuffer_Stop(This->device->hwbuf);
1227         if (hres != DS_OK)
1228             WARN("IDsCaptureDriverBuffer_Stop() failed\n");
1229     } else if (This->device->hwi) {
1230         hres = mmErr(waveInReset(This->device->hwi));
1231         if (hres != DS_OK)
1232             WARN("waveInReset() failed\n");
1233     } else {
1234         WARN("no driver\n");
1235         hres = DSERR_NODRIVER;
1236     }
1237
1238     TRACE("returning %08x\n", hres);
1239     return hres;
1240 }
1241
1242 static HRESULT WINAPI
1243 IDirectSoundCaptureBufferImpl_Unlock(
1244     LPDIRECTSOUNDCAPTUREBUFFER8 iface,
1245     LPVOID lpvAudioPtr1,
1246     DWORD dwAudioBytes1,
1247     LPVOID lpvAudioPtr2,
1248     DWORD dwAudioBytes2 )
1249 {
1250     HRESULT hres = DS_OK;
1251     IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
1252     TRACE( "(%p,%p,%08u,%p,%08u)\n", This, lpvAudioPtr1, dwAudioBytes1,
1253         lpvAudioPtr2, dwAudioBytes2 );
1254
1255     if (lpvAudioPtr1 == NULL) {
1256         WARN("invalid parameter: lpvAudioPtr1 == NULL\n");
1257         return DSERR_INVALIDPARAM;
1258     }
1259
1260     if (This->device->driver) {
1261         hres = IDsCaptureDriverBuffer_Unlock(This->device->hwbuf, lpvAudioPtr1,
1262                                              dwAudioBytes1, lpvAudioPtr2, dwAudioBytes2);
1263         if (hres != DS_OK)
1264             WARN("IDsCaptureDriverBuffer_Unlock failed\n");
1265     } else if (!This->device->hwi) {
1266         WARN("invalid call\n");
1267         hres = DSERR_INVALIDCALL;
1268     }
1269
1270     TRACE("returning %08x\n", hres);
1271     return hres;
1272 }
1273
1274 static HRESULT WINAPI
1275 IDirectSoundCaptureBufferImpl_GetObjectInPath(
1276     LPDIRECTSOUNDCAPTUREBUFFER8 iface,
1277     REFGUID rguidObject,
1278     DWORD dwIndex,
1279     REFGUID rguidInterface,
1280     LPVOID* ppObject )
1281 {
1282     IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
1283
1284     FIXME( "(%p,%s,%u,%s,%p): stub\n", This, debugstr_guid(rguidObject),
1285         dwIndex, debugstr_guid(rguidInterface), ppObject );
1286
1287     return DS_OK;
1288 }
1289
1290 static HRESULT WINAPI
1291 IDirectSoundCaptureBufferImpl_GetFXStatus(
1292     LPDIRECTSOUNDCAPTUREBUFFER8 iface,
1293     DWORD dwFXCount,
1294     LPDWORD pdwFXStatus )
1295 {
1296     IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
1297
1298     FIXME( "(%p,%u,%p): stub\n", This, dwFXCount, pdwFXStatus );
1299
1300     return DS_OK;
1301 }
1302
1303 static const IDirectSoundCaptureBuffer8Vtbl dscbvt =
1304 {
1305     /* IUnknown methods */
1306     IDirectSoundCaptureBufferImpl_QueryInterface,
1307     IDirectSoundCaptureBufferImpl_AddRef,
1308     IDirectSoundCaptureBufferImpl_Release,
1309
1310     /* IDirectSoundCaptureBuffer methods */
1311     IDirectSoundCaptureBufferImpl_GetCaps,
1312     IDirectSoundCaptureBufferImpl_GetCurrentPosition,
1313     IDirectSoundCaptureBufferImpl_GetFormat,
1314     IDirectSoundCaptureBufferImpl_GetStatus,
1315     IDirectSoundCaptureBufferImpl_Initialize,
1316     IDirectSoundCaptureBufferImpl_Lock,
1317     IDirectSoundCaptureBufferImpl_Start,
1318     IDirectSoundCaptureBufferImpl_Stop,
1319     IDirectSoundCaptureBufferImpl_Unlock,
1320
1321     /* IDirectSoundCaptureBuffer methods */
1322     IDirectSoundCaptureBufferImpl_GetObjectInPath,
1323     IDirectSoundCaptureBufferImpl_GetFXStatus
1324 };
1325
1326 HRESULT IDirectSoundCaptureBufferImpl_Create(
1327     DirectSoundCaptureDevice *device,
1328     IDirectSoundCaptureBufferImpl ** ppobj,
1329     LPCDSCBUFFERDESC lpcDSCBufferDesc)
1330 {
1331     LPWAVEFORMATEX  wfex;
1332     TRACE( "(%p,%p,%p)\n", device, ppobj, lpcDSCBufferDesc);
1333
1334     if (ppobj == NULL) {
1335         WARN("invalid parameter: ppobj == NULL\n");
1336         return DSERR_INVALIDPARAM;
1337     }
1338
1339     if (!device) {
1340         WARN("not initialized\n");
1341         *ppobj = NULL;
1342         return DSERR_UNINITIALIZED;
1343     }
1344
1345     if (lpcDSCBufferDesc == NULL) {
1346         WARN("invalid parameter: lpcDSCBufferDesc == NULL\n");
1347         *ppobj = NULL;
1348         return DSERR_INVALIDPARAM;
1349     }
1350
1351     if ( ((lpcDSCBufferDesc->dwSize != sizeof(DSCBUFFERDESC)) &&
1352           (lpcDSCBufferDesc->dwSize != sizeof(DSCBUFFERDESC1))) ||
1353         (lpcDSCBufferDesc->dwBufferBytes == 0) ||
1354         (lpcDSCBufferDesc->lpwfxFormat == NULL) ) {
1355         WARN("invalid lpcDSCBufferDesc\n");
1356         *ppobj = NULL;
1357         return DSERR_INVALIDPARAM;
1358     }
1359
1360     wfex = lpcDSCBufferDesc->lpwfxFormat;
1361
1362     if (wfex) {
1363         TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
1364             "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
1365             wfex->wFormatTag, wfex->nChannels, wfex->nSamplesPerSec,
1366             wfex->nAvgBytesPerSec, wfex->nBlockAlign,
1367             wfex->wBitsPerSample, wfex->cbSize);
1368
1369         if (wfex->wFormatTag == WAVE_FORMAT_PCM) {
1370             device->pwfx = HeapAlloc(GetProcessHeap(),0,sizeof(WAVEFORMATEX));
1371             *device->pwfx = *wfex;
1372             device->pwfx->cbSize = 0;
1373         } else {
1374             device->pwfx = HeapAlloc(GetProcessHeap(),0,sizeof(WAVEFORMATEX)+wfex->cbSize);
1375             CopyMemory(device->pwfx, wfex, sizeof(WAVEFORMATEX)+wfex->cbSize);
1376         }
1377     } else {
1378         WARN("lpcDSCBufferDesc->lpwfxFormat == 0\n");
1379         *ppobj = NULL;
1380         return DSERR_INVALIDPARAM; /* FIXME: DSERR_BADFORMAT ? */
1381     }
1382
1383     *ppobj = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,
1384         sizeof(IDirectSoundCaptureBufferImpl));
1385
1386     if ( *ppobj == NULL ) {
1387         WARN("out of memory\n");
1388         *ppobj = NULL;
1389         return DSERR_OUTOFMEMORY;
1390     } else {
1391         HRESULT err = DS_OK;
1392         LPBYTE newbuf;
1393         DWORD buflen;
1394         IDirectSoundCaptureBufferImpl *This = *ppobj;
1395
1396         This->ref = 1;
1397         This->device = device;
1398         This->device->capture_buffer = This;
1399         This->notify = NULL;
1400         This->nrofnotifies = 0;
1401         This->hwnotify = NULL;
1402
1403         This->pdscbd = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,
1404             lpcDSCBufferDesc->dwSize);
1405         if (This->pdscbd)
1406             CopyMemory(This->pdscbd, lpcDSCBufferDesc, lpcDSCBufferDesc->dwSize);
1407         else {
1408             WARN("no memory\n");
1409             This->device->capture_buffer = 0;
1410             HeapFree( GetProcessHeap(), 0, This );
1411             *ppobj = NULL;
1412             return DSERR_OUTOFMEMORY;
1413         }
1414
1415         This->lpVtbl = &dscbvt;
1416
1417         if (device->driver) {
1418             if (This->device->drvdesc.dwFlags & DSDDESC_DOMMSYSTEMOPEN)
1419                 FIXME("DSDDESC_DOMMSYSTEMOPEN not supported\n");
1420
1421             if (This->device->drvdesc.dwFlags & DSDDESC_USESYSTEMMEMORY) {
1422                 /* allocate buffer from system memory */
1423                 buflen = lpcDSCBufferDesc->dwBufferBytes;
1424                 TRACE("desired buflen=%d, old buffer=%p\n", buflen, device->buffer);
1425                 if (device->buffer)
1426                     newbuf = HeapReAlloc(GetProcessHeap(),0,device->buffer,buflen);
1427                 else
1428                     newbuf = HeapAlloc(GetProcessHeap(),0,buflen);
1429
1430                 if (newbuf == NULL) {
1431                     WARN("failed to allocate capture buffer\n");
1432                     err = DSERR_OUTOFMEMORY;
1433                     /* but the old buffer might still exist and must be re-prepared */
1434                 } else {
1435                     device->buffer = newbuf;
1436                     device->buflen = buflen;
1437                 }
1438             } else {
1439                 /* let driver allocate memory */
1440                 device->buflen = lpcDSCBufferDesc->dwBufferBytes;
1441                 /* FIXME: */
1442                 HeapFree( GetProcessHeap(), 0, device->buffer);
1443                 device->buffer = NULL;
1444             }
1445
1446             err = IDsCaptureDriver_CreateCaptureBuffer(device->driver,
1447                 device->pwfx,0,0,&(device->buflen),&(device->buffer),(LPVOID*)&(device->hwbuf));
1448             if (err != DS_OK) {
1449                 WARN("IDsCaptureDriver_CreateCaptureBuffer failed\n");
1450                 This->device->capture_buffer = 0;
1451                 HeapFree( GetProcessHeap(), 0, This );
1452                 *ppobj = NULL;
1453                 return err;
1454             }
1455         } else {
1456             DWORD flags = CALLBACK_FUNCTION;
1457             err = mmErr(waveInOpen(&(device->hwi),
1458                 device->drvdesc.dnDevNode, device->pwfx,
1459                 (DWORD_PTR)DSOUND_capture_callback, (DWORD_PTR)device, flags));
1460             if (err != DS_OK) {
1461                 WARN("waveInOpen failed\n");
1462                 This->device->capture_buffer = 0;
1463                 HeapFree( GetProcessHeap(), 0, This );
1464                 *ppobj = NULL;
1465                 return err;
1466             }
1467
1468             buflen = lpcDSCBufferDesc->dwBufferBytes;
1469             TRACE("desired buflen=%d, old buffer=%p\n", buflen, device->buffer);
1470             if (device->buffer)
1471                 newbuf = HeapReAlloc(GetProcessHeap(),0,device->buffer,buflen);
1472             else
1473                 newbuf = HeapAlloc(GetProcessHeap(),0,buflen);
1474             if (newbuf == NULL) {
1475                 WARN("failed to allocate capture buffer\n");
1476                 err = DSERR_OUTOFMEMORY;
1477                 /* but the old buffer might still exist and must be re-prepared */
1478             } else {
1479                 device->buffer = newbuf;
1480                 device->buflen = buflen;
1481             }
1482         }
1483     }
1484
1485     TRACE("returning DS_OK\n");
1486     return DS_OK;
1487 }
1488
1489 /*******************************************************************************
1490  * DirectSoundCaptureDevice
1491  */
1492 HRESULT DirectSoundCaptureDevice_Initialize(
1493     DirectSoundCaptureDevice ** ppDevice,
1494     LPCGUID lpcGUID)
1495 {
1496     HRESULT err = DSERR_INVALIDPARAM;
1497     unsigned wid, widn;
1498     BOOLEAN found = FALSE;
1499     GUID devGUID;
1500     DirectSoundCaptureDevice *device = *ppDevice;
1501     TRACE("(%p, %s)\n", ppDevice, debugstr_guid(lpcGUID));
1502
1503     /* Default device? */
1504     if ( !lpcGUID || IsEqualGUID(lpcGUID, &GUID_NULL) )
1505         lpcGUID = &DSDEVID_DefaultCapture;
1506
1507     if (GetDeviceID(lpcGUID, &devGUID) != DS_OK) {
1508         WARN("invalid parameter: lpcGUID\n");
1509         return DSERR_INVALIDPARAM;
1510     }
1511
1512     widn = waveInGetNumDevs();
1513     if (!widn) {
1514         WARN("no audio devices found\n");
1515         return DSERR_NODRIVER;
1516     }
1517
1518     /* enumerate WINMM audio devices and find the one we want */
1519     for (wid=0; wid<widn; wid++) {
1520         if (IsEqualGUID( &devGUID, &DSOUND_capture_guids[wid]) ) {
1521             found = TRUE;
1522             break;
1523         }
1524     }
1525
1526     if (found == FALSE) {
1527         WARN("No device found matching given ID!\n");
1528         return DSERR_NODRIVER;
1529     }
1530
1531     if (DSOUND_capture[wid]) {
1532         WARN("already in use\n");
1533         return DSERR_ALLOCATED;
1534     }
1535
1536     err = DirectSoundCaptureDevice_Create(&(device));
1537     if (err != DS_OK) {
1538         WARN("DirectSoundCaptureDevice_Create failed\n");
1539         return err;
1540     }
1541
1542     *ppDevice = device;
1543     device->guid = devGUID;
1544
1545     /* Disable the direct sound driver to force emulation if requested. */
1546     device->driver = NULL;
1547     if (ds_hw_accel != DS_HW_ACCEL_EMULATION)
1548     {
1549         err = mmErr(waveInMessage(UlongToHandle(wid),DRV_QUERYDSOUNDIFACE,(DWORD_PTR)&device->driver,0));
1550         if ( (err != DS_OK) && (err != DSERR_UNSUPPORTED) ) {
1551             WARN("waveInMessage failed; err=%x\n",err);
1552             return err;
1553         }
1554     }
1555     err = DS_OK;
1556
1557     /* Get driver description */
1558     if (device->driver) {
1559         TRACE("using DirectSound driver\n");
1560         err = IDsCaptureDriver_GetDriverDesc(device->driver, &(device->drvdesc));
1561         if (err != DS_OK) {
1562             WARN("IDsCaptureDriver_GetDriverDesc failed\n");
1563             return err;
1564         }
1565     } else {
1566         TRACE("using WINMM\n");
1567         /* if no DirectSound interface available, use WINMM API instead */
1568         device->drvdesc.dwFlags = DSDDESC_DOMMSYSTEMOPEN |
1569             DSDDESC_DOMMSYSTEMSETFORMAT;
1570     }
1571
1572     device->drvdesc.dnDevNode = wid;
1573
1574     /* open the DirectSound driver if available */
1575     if (device->driver && (err == DS_OK))
1576         err = IDsCaptureDriver_Open(device->driver);
1577
1578     if (err == DS_OK) {
1579         *ppDevice = device;
1580
1581         /* the driver is now open, so it's now allowed to call GetCaps */
1582         if (device->driver) {
1583             device->drvcaps.dwSize = sizeof(device->drvcaps);
1584             err = IDsCaptureDriver_GetCaps(device->driver,&(device->drvcaps));
1585             if (err != DS_OK) {
1586                 WARN("IDsCaptureDriver_GetCaps failed\n");
1587                 return err;
1588             }
1589         } else /*if (device->hwi)*/ {
1590             WAVEINCAPSA    wic;
1591             err = mmErr(waveInGetDevCapsA((UINT)device->drvdesc.dnDevNode, &wic, sizeof(wic)));
1592
1593             if (err == DS_OK) {
1594                 device->drvcaps.dwFlags = 0;
1595                 lstrcpynA(device->drvdesc.szDrvname, wic.szPname,
1596                           sizeof(device->drvdesc.szDrvname));
1597
1598                 device->drvcaps.dwFlags |= DSCCAPS_EMULDRIVER;
1599                 device->drvcaps.dwFormats = wic.dwFormats;
1600                 device->drvcaps.dwChannels = wic.wChannels;
1601             }
1602         }
1603     }
1604
1605     return err;
1606 }
1607
1608 static HRESULT DirectSoundCaptureDevice_Create(
1609     DirectSoundCaptureDevice ** ppDevice)
1610 {
1611     DirectSoundCaptureDevice * device;
1612     TRACE("(%p)\n", ppDevice);
1613
1614     /* Allocate memory */
1615     device = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DirectSoundCaptureDevice));
1616
1617     if (device == NULL) {
1618         WARN("out of memory\n");
1619         return DSERR_OUTOFMEMORY;
1620     }
1621
1622     device->ref = 1;
1623     device->state = STATE_STOPPED;
1624
1625     InitializeCriticalSection( &(device->lock) );
1626     device->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": DirectSoundCaptureDevice.lock");
1627
1628     *ppDevice = device;
1629
1630     return DS_OK;
1631 }
1632
1633 ULONG DirectSoundCaptureDevice_Release(
1634     DirectSoundCaptureDevice * device)
1635 {
1636     ULONG ref = InterlockedDecrement(&(device->ref));
1637     TRACE("(%p) ref was %d\n", device, ref + 1);
1638
1639     if (!ref) {
1640         TRACE("deleting object\n");
1641         if (device->capture_buffer)
1642             IDirectSoundCaptureBufferImpl_Release(
1643                 (LPDIRECTSOUNDCAPTUREBUFFER8) device->capture_buffer);
1644
1645         if (device->driver) {
1646             IDsCaptureDriver_Close(device->driver);
1647             IDsCaptureDriver_Release(device->driver);
1648         }
1649
1650         HeapFree(GetProcessHeap(), 0, device->pwfx);
1651         device->lock.DebugInfo->Spare[0] = 0;
1652         DeleteCriticalSection( &(device->lock) );
1653         DSOUND_capture[device->drvdesc.dnDevNode] = NULL;
1654         HeapFree(GetProcessHeap(), 0, device);
1655         TRACE("(%p) released\n", device);
1656     }
1657     return ref;
1658 }