ws2_32/tests: Don't use socklen_t.
[wine/hacks.git] / dlls / dsound / capture.c
blob2a8252f9c0fa65c964314cfcd2674eef61fce904
1 /* DirectSoundCapture
3 * Copyright 1998 Marcus Meissner
4 * Copyright 1998 Rob Riggs
5 * Copyright 2000-2001 TransGaming Technologies, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 * TODO:
23 * Implement FX support.
24 * Implement both IDirectSoundCaptureBuffer and IDirectSoundCaptureBuffer8
25 * Make DirectSoundCaptureCreate and DirectSoundCaptureCreate8 behave differently
28 #include <stdarg.h>
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"
44 WINE_DEFAULT_DEBUG_CHANNEL(dsound);
46 /*****************************************************************************
47 * IDirectSoundCapture implementation structure
49 struct IDirectSoundCaptureImpl
51 /* IUnknown fields */
52 const IDirectSoundCaptureVtbl *lpVtbl;
53 LONG ref;
55 DirectSoundCaptureDevice *device;
58 static HRESULT IDirectSoundCaptureImpl_Create(LPDIRECTSOUNDCAPTURE8 * ppds);
61 /*****************************************************************************
62 * IDirectSoundCaptureNotify implementation structure
64 struct IDirectSoundCaptureNotifyImpl
66 /* IUnknown fields */
67 const IDirectSoundNotifyVtbl *lpVtbl;
68 LONG ref;
69 IDirectSoundCaptureBufferImpl* dscb;
72 static HRESULT IDirectSoundCaptureNotifyImpl_Create(IDirectSoundCaptureBufferImpl *dscb,
73 IDirectSoundCaptureNotifyImpl ** pdscn);
76 DirectSoundCaptureDevice * DSOUND_capture[MAXWAVEDRIVERS];
78 static HRESULT DirectSoundCaptureDevice_Create(DirectSoundCaptureDevice ** ppDevice);
80 static const char * const captureStateString[] = {
81 "STATE_STOPPED",
82 "STATE_STARTING",
83 "STATE_CAPTURING",
84 "STATE_STOPPING"
87 HRESULT DSOUND_CaptureCreate(
88 REFIID riid,
89 LPDIRECTSOUNDCAPTURE *ppDSC)
91 LPDIRECTSOUNDCAPTURE pDSC;
92 HRESULT hr;
93 TRACE("(%s, %p)\n", debugstr_guid(riid), ppDSC);
95 if (!IsEqualIID(riid, &IID_IUnknown) &&
96 !IsEqualIID(riid, &IID_IDirectSoundCapture)) {
97 *ppDSC = 0;
98 return E_NOINTERFACE;
101 /* Get dsound configuration */
102 setup_dsound_options();
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;
113 return hr;
116 HRESULT DSOUND_CaptureCreate8(
117 REFIID riid,
118 LPDIRECTSOUNDCAPTURE8 *ppDSC8)
120 LPDIRECTSOUNDCAPTURE8 pDSC8;
121 HRESULT hr;
122 TRACE("(%s, %p)\n", debugstr_guid(riid), ppDSC8);
124 if (!IsEqualIID(riid, &IID_IUnknown) &&
125 !IsEqualIID(riid, &IID_IDirectSoundCapture8)) {
126 *ppDSC8 = 0;
127 return E_NOINTERFACE;
130 /* Get dsound configuration */
131 setup_dsound_options();
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;
142 return hr;
145 /***************************************************************************
146 * DirectSoundCaptureCreate [DSOUND.6]
148 * Create and initialize a DirectSoundCapture interface.
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.
155 * RETURNS
156 * Success: DS_OK
157 * Failure: DSERR_NOAGGREGATION, DSERR_ALLOCATED, DSERR_INVALIDPARAM,
158 * DSERR_OUTOFMEMORY
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.
165 * DSERR_ALLOCATED is returned for sound devices that do not support full duplex.
167 HRESULT WINAPI DirectSoundCaptureCreate(
168 LPCGUID lpcGUID,
169 LPDIRECTSOUNDCAPTURE *ppDSC,
170 LPUNKNOWN pUnkOuter)
172 HRESULT hr;
173 LPDIRECTSOUNDCAPTURE pDSC;
174 TRACE("(%s,%p,%p)\n", debugstr_guid(lpcGUID), ppDSC, pUnkOuter);
176 if (ppDSC == NULL) {
177 WARN("invalid parameter: ppDSC == NULL\n");
178 return DSERR_INVALIDPARAM;
181 if (pUnkOuter) {
182 WARN("invalid parameter: pUnkOuter != NULL\n");
183 *ppDSC = NULL;
184 return DSERR_NOAGGREGATION;
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;
196 *ppDSC = pDSC;
198 return hr;
201 /***************************************************************************
202 * DirectSoundCaptureCreate8 [DSOUND.12]
204 * Create and initialize a DirectSoundCapture interface.
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.
211 * RETURNS
212 * Success: DS_OK
213 * Failure: DSERR_NOAGGREGATION, DSERR_ALLOCATED, DSERR_INVALIDPARAM,
214 * DSERR_OUTOFMEMORY
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.
221 * DSERR_ALLOCATED is returned for sound devices that do not support full duplex.
223 HRESULT WINAPI DirectSoundCaptureCreate8(
224 LPCGUID lpcGUID,
225 LPDIRECTSOUNDCAPTURE8 *ppDSC8,
226 LPUNKNOWN pUnkOuter)
228 HRESULT hr;
229 LPDIRECTSOUNDCAPTURE8 pDSC8;
230 TRACE("(%s,%p,%p)\n", debugstr_guid(lpcGUID), ppDSC8, pUnkOuter);
232 if (ppDSC8 == NULL) {
233 WARN("invalid parameter: ppDSC8 == NULL\n");
234 return DSERR_INVALIDPARAM;
237 if (pUnkOuter) {
238 WARN("invalid parameter: pUnkOuter != NULL\n");
239 *ppDSC8 = NULL;
240 return DSERR_NOAGGREGATION;
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;
252 *ppDSC8 = pDSC8;
254 return hr;
257 /***************************************************************************
258 * DirectSoundCaptureEnumerateA [DSOUND.7]
260 * Enumerate all DirectSound drivers installed in the system.
262 * PARAMS
263 * lpDSEnumCallback [I] Address of callback function.
264 * lpContext [I] Address of user defined context passed to callback function.
266 * RETURNS
267 * Success: DS_OK
268 * Failure: DSERR_INVALIDPARAM
270 HRESULT WINAPI
271 DirectSoundCaptureEnumerateA(
272 LPDSENUMCALLBACKA lpDSEnumCallback,
273 LPVOID lpContext)
275 unsigned devs, wid;
276 DSDRIVERDESC desc;
277 GUID guid;
278 int err;
280 TRACE("(%p,%p)\n", lpDSEnumCallback, lpContext );
282 if (lpDSEnumCallback == NULL) {
283 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
284 return DSERR_INVALIDPARAM;
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;
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;
314 return DS_OK;
317 /***************************************************************************
318 * DirectSoundCaptureEnumerateW [DSOUND.8]
320 * Enumerate all DirectSound drivers installed in the system.
322 * PARAMS
323 * lpDSEnumCallback [I] Address of callback function.
324 * lpContext [I] Address of user defined context passed to callback function.
326 * RETURNS
327 * Success: DS_OK
328 * Failure: DSERR_INVALIDPARAM
330 HRESULT WINAPI
331 DirectSoundCaptureEnumerateW(
332 LPDSENUMCALLBACKW lpDSEnumCallback,
333 LPVOID lpContext)
335 unsigned devs, wid;
336 DSDRIVERDESC desc;
337 GUID guid;
338 int err;
339 WCHAR wDesc[MAXPNAMELEN];
340 WCHAR wName[MAXPNAMELEN];
342 TRACE("(%p,%p)\n", lpDSEnumCallback, lpContext );
344 if (lpDSEnumCallback == NULL) {
345 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
346 return DSERR_INVALIDPARAM;
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;
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;
384 return DS_OK;
387 static void capture_CheckNotify(IDirectSoundCaptureBufferImpl *This, DWORD from, DWORD len)
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);
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;
401 else return;
404 if (offset >= from && offset < (from + len))
406 TRACE("signalled event %p (%d)\n", event->hEventNotify, i);
407 SetEvent(event->hEventNotify);
412 static void CALLBACK
413 DSOUND_capture_callback(HWAVEIN hwi, UINT msg, DWORD_PTR dwUser, DWORD_PTR dw1,
414 DWORD_PTR dw2)
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());
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;
447 TRACE("DirectSoundCapture new This->state=%s, new This->index=%d\n",
448 captureStateString[This->state],This->index);
449 LeaveCriticalSection( &(This->lock) );
452 TRACE("completed\n");
455 /***************************************************************************
456 * IDirectSoundCaptureImpl
458 static HRESULT WINAPI
459 IDirectSoundCaptureImpl_QueryInterface(
460 LPDIRECTSOUNDCAPTURE iface,
461 REFIID riid,
462 LPVOID* ppobj )
464 IDirectSoundCaptureImpl *This = (IDirectSoundCaptureImpl *)iface;
465 TRACE( "(%p,%s,%p)\n", This, debugstr_guid(riid), ppobj );
467 if (ppobj == NULL) {
468 WARN("invalid parameter\n");
469 return E_INVALIDARG;
472 *ppobj = NULL;
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;
484 WARN("unsupported riid: %s\n", debugstr_guid(riid));
485 return E_NOINTERFACE;
488 static ULONG WINAPI
489 IDirectSoundCaptureImpl_AddRef( LPDIRECTSOUNDCAPTURE iface )
491 IDirectSoundCaptureImpl *This = (IDirectSoundCaptureImpl *)iface;
492 ULONG ref = InterlockedIncrement(&(This->ref));
493 TRACE("(%p) ref was %d\n", This, ref - 1);
494 return ref;
497 static ULONG WINAPI
498 IDirectSoundCaptureImpl_Release( LPDIRECTSOUNDCAPTURE iface )
500 IDirectSoundCaptureImpl *This = (IDirectSoundCaptureImpl *)iface;
501 ULONG ref = InterlockedDecrement(&(This->ref));
502 TRACE("(%p) ref was %d\n", This, ref + 1);
504 if (!ref) {
505 if (This->device)
506 DirectSoundCaptureDevice_Release(This->device);
508 HeapFree( GetProcessHeap(), 0, This );
509 TRACE("(%p) released\n", This);
511 return ref;
514 HRESULT WINAPI IDirectSoundCaptureImpl_CreateCaptureBuffer(
515 LPDIRECTSOUNDCAPTURE iface,
516 LPCDSCBUFFERDESC lpcDSCBufferDesc,
517 LPDIRECTSOUNDCAPTUREBUFFER* lplpDSCaptureBuffer,
518 LPUNKNOWN pUnk )
520 HRESULT hr;
521 IDirectSoundCaptureImpl *This = (IDirectSoundCaptureImpl *)iface;
523 TRACE( "(%p,%p,%p,%p)\n",iface,lpcDSCBufferDesc,lplpDSCaptureBuffer,pUnk);
525 if (lpcDSCBufferDesc == NULL) {
526 WARN("invalid parameter: lpcDSCBufferDesc == NULL)\n");
527 return DSERR_INVALIDPARAM;
530 if (lplpDSCaptureBuffer == NULL) {
531 WARN("invalid parameter: lplpDSCaptureBuffer == NULL\n");
532 return DSERR_INVALIDPARAM;
535 if (pUnk != NULL) {
536 WARN("invalid parameter: pUnk != NULL\n");
537 return DSERR_INVALIDPARAM;
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 ? */
546 hr = IDirectSoundCaptureBufferImpl_Create(This->device,
547 (IDirectSoundCaptureBufferImpl **)lplpDSCaptureBuffer, lpcDSCBufferDesc);
549 if (hr != DS_OK)
550 WARN("IDirectSoundCaptureBufferImpl_Create failed\n");
552 return hr;
555 HRESULT WINAPI IDirectSoundCaptureImpl_GetCaps(
556 LPDIRECTSOUNDCAPTURE iface,
557 LPDSCCAPS lpDSCCaps )
559 IDirectSoundCaptureImpl *This = (IDirectSoundCaptureImpl *)iface;
560 TRACE("(%p,%p)\n",This,lpDSCCaps);
562 if (This->device == NULL) {
563 WARN("not initialized\n");
564 return DSERR_UNINITIALIZED;
567 if (lpDSCCaps== NULL) {
568 WARN("invalid parameter: lpDSCCaps== NULL\n");
569 return DSERR_INVALIDPARAM;
572 if (lpDSCCaps->dwSize < sizeof(*lpDSCCaps)) {
573 WARN("invalid parameter: lpDSCCaps->dwSize = %d\n", lpDSCCaps->dwSize);
574 return DSERR_INVALIDPARAM;
577 lpDSCCaps->dwFlags = This->device->drvcaps.dwFlags;
578 lpDSCCaps->dwFormats = This->device->drvcaps.dwFormats;
579 lpDSCCaps->dwChannels = This->device->drvcaps.dwChannels;
581 TRACE("(flags=0x%08x,format=0x%08x,channels=%d)\n",lpDSCCaps->dwFlags,
582 lpDSCCaps->dwFormats, lpDSCCaps->dwChannels);
584 return DS_OK;
587 HRESULT WINAPI IDirectSoundCaptureImpl_Initialize(
588 LPDIRECTSOUNDCAPTURE iface,
589 LPCGUID lpcGUID )
591 IDirectSoundCaptureImpl *This = (IDirectSoundCaptureImpl *)iface;
592 TRACE("(%p,%s)\n", This, debugstr_guid(lpcGUID));
594 if (This->device != NULL) {
595 WARN("already initialized\n");
596 return DSERR_ALREADYINITIALIZED;
598 return DirectSoundCaptureDevice_Initialize(&This->device, lpcGUID);
601 static const IDirectSoundCaptureVtbl dscvt =
603 /* IUnknown methods */
604 IDirectSoundCaptureImpl_QueryInterface,
605 IDirectSoundCaptureImpl_AddRef,
606 IDirectSoundCaptureImpl_Release,
608 /* IDirectSoundCapture methods */
609 IDirectSoundCaptureImpl_CreateCaptureBuffer,
610 IDirectSoundCaptureImpl_GetCaps,
611 IDirectSoundCaptureImpl_Initialize
614 static HRESULT IDirectSoundCaptureImpl_Create(
615 LPDIRECTSOUNDCAPTURE8 * ppDSC)
617 IDirectSoundCaptureImpl *pDSC;
618 TRACE("(%p)\n", ppDSC);
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;
628 pDSC->lpVtbl = &dscvt;
629 pDSC->ref = 0;
630 pDSC->device = NULL;
632 *ppDSC = (LPDIRECTSOUNDCAPTURE8)pDSC;
634 return DS_OK;
637 /*******************************************************************************
638 * IDirectSoundCaptureNotify
640 static HRESULT WINAPI IDirectSoundCaptureNotifyImpl_QueryInterface(
641 LPDIRECTSOUNDNOTIFY iface,
642 REFIID riid,
643 LPVOID *ppobj)
645 IDirectSoundCaptureNotifyImpl *This = (IDirectSoundCaptureNotifyImpl *)iface;
646 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
648 if (This->dscb == NULL) {
649 WARN("invalid parameter\n");
650 return E_INVALIDARG;
653 return IDirectSoundCaptureBuffer_QueryInterface((LPDIRECTSOUNDCAPTUREBUFFER)This->dscb, riid, ppobj);
656 static ULONG WINAPI IDirectSoundCaptureNotifyImpl_AddRef(LPDIRECTSOUNDNOTIFY iface)
658 IDirectSoundCaptureNotifyImpl *This = (IDirectSoundCaptureNotifyImpl *)iface;
659 ULONG ref = InterlockedIncrement(&(This->ref));
660 TRACE("(%p) ref was %d\n", This, ref - 1);
661 return ref;
664 static ULONG WINAPI IDirectSoundCaptureNotifyImpl_Release(LPDIRECTSOUNDNOTIFY iface)
666 IDirectSoundCaptureNotifyImpl *This = (IDirectSoundCaptureNotifyImpl *)iface;
667 ULONG ref = InterlockedDecrement(&(This->ref));
668 TRACE("(%p) ref was %d\n", This, ref + 1);
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);
678 return ref;
681 static HRESULT WINAPI IDirectSoundCaptureNotifyImpl_SetNotificationPositions(
682 LPDIRECTSOUNDNOTIFY iface,
683 DWORD howmuch,
684 LPCDSBPOSITIONNOTIFY notify)
686 IDirectSoundCaptureNotifyImpl *This = (IDirectSoundCaptureNotifyImpl *)iface;
687 TRACE("(%p,0x%08x,%p)\n",This,howmuch,notify);
689 if (howmuch > 0 && notify == NULL) {
690 WARN("invalid parameter: notify == NULL\n");
691 return DSERR_INVALIDPARAM;
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);
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));
717 if (This->dscb->notifies == NULL) {
718 WARN("out of memory\n");
719 return DSERR_OUTOFMEMORY;
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;
729 return S_OK;
732 static const IDirectSoundNotifyVtbl dscnvt =
734 IDirectSoundCaptureNotifyImpl_QueryInterface,
735 IDirectSoundCaptureNotifyImpl_AddRef,
736 IDirectSoundCaptureNotifyImpl_Release,
737 IDirectSoundCaptureNotifyImpl_SetNotificationPositions,
740 static HRESULT IDirectSoundCaptureNotifyImpl_Create(
741 IDirectSoundCaptureBufferImpl *dscb,
742 IDirectSoundCaptureNotifyImpl **pdscn)
744 IDirectSoundCaptureNotifyImpl * dscn;
745 TRACE("(%p,%p)\n",dscb,pdscn);
747 dscn = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*dscn));
749 if (dscn == NULL) {
750 WARN("out of memory\n");
751 return DSERR_OUTOFMEMORY;
754 dscn->ref = 0;
755 dscn->lpVtbl = &dscnvt;
756 dscn->dscb = dscb;
757 dscb->notify = dscn;
758 IDirectSoundCaptureBuffer_AddRef((LPDIRECTSOUNDCAPTUREBUFFER)dscb);
760 *pdscn = dscn;
761 return DS_OK;
764 /*******************************************************************************
765 * IDirectSoundCaptureBuffer
767 static HRESULT WINAPI
768 IDirectSoundCaptureBufferImpl_QueryInterface(
769 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
770 REFIID riid,
771 LPVOID* ppobj )
773 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
774 HRESULT hres;
775 TRACE( "(%p,%s,%p)\n", This, debugstr_guid(riid), ppobj );
777 if (ppobj == NULL) {
778 WARN("invalid parameter\n");
779 return E_INVALIDARG;
782 *ppobj = NULL;
784 if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ) {
785 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;
800 *ppobj = This->notify;
801 return DS_OK;
804 WARN("IID_IDirectSoundNotify\n");
805 return E_FAIL;
808 if ( IsEqualGUID( &IID_IDirectSoundCaptureBuffer, riid ) ||
809 IsEqualGUID( &IID_IDirectSoundCaptureBuffer8, riid ) ) {
810 IDirectSoundCaptureBuffer8_AddRef(iface);
811 *ppobj = This;
812 return NO_ERROR;
815 FIXME("(%p,%s,%p) unsupported GUID\n", This, debugstr_guid(riid), ppobj);
816 return E_NOINTERFACE;
819 static ULONG WINAPI
820 IDirectSoundCaptureBufferImpl_AddRef( LPDIRECTSOUNDCAPTUREBUFFER8 iface )
822 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
823 ULONG ref = InterlockedIncrement(&(This->ref));
824 TRACE("(%p) ref was %d\n", This, ref - 1);
825 return ref;
828 static ULONG WINAPI
829 IDirectSoundCaptureBufferImpl_Release( LPDIRECTSOUNDCAPTUREBUFFER8 iface )
831 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
832 ULONG ref = InterlockedDecrement(&(This->ref));
833 TRACE("(%p) ref was %d\n", This, ref + 1);
835 if (!ref) {
836 TRACE("deleting object\n");
837 if (This->device->state == STATE_CAPTURING)
838 This->device->state = STATE_STOPPING;
840 HeapFree(GetProcessHeap(),0, This->pdscbd);
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;
850 if (This->device->hwbuf)
851 IDsCaptureDriverBuffer_Release(This->device->hwbuf);
853 /* remove from DirectSoundCaptureDevice */
854 This->device->capture_buffer = NULL;
856 if (This->notify)
857 IDirectSoundNotify_Release((LPDIRECTSOUNDNOTIFY)This->notify);
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;
865 HeapFree(GetProcessHeap(), 0, This->notifies);
866 HeapFree( GetProcessHeap(), 0, This );
867 TRACE("(%p) released\n", This);
869 return ref;
872 static HRESULT WINAPI
873 IDirectSoundCaptureBufferImpl_GetCaps(
874 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
875 LPDSCBCAPS lpDSCBCaps )
877 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
878 TRACE( "(%p,%p)\n", This, lpDSCBCaps );
880 if (lpDSCBCaps == NULL) {
881 WARN("invalid parameter: lpDSCBCaps == NULL\n");
882 return DSERR_INVALIDPARAM;
885 if (lpDSCBCaps->dwSize < sizeof(DSCBCAPS)) {
886 WARN("invalid parameter: lpDSCBCaps->dwSize = %d\n", lpDSCBCaps->dwSize);
887 return DSERR_INVALIDPARAM;
890 if (This->device == NULL) {
891 WARN("invalid parameter: This->device == NULL\n");
892 return DSERR_INVALIDPARAM;
895 lpDSCBCaps->dwSize = sizeof(DSCBCAPS);
896 lpDSCBCaps->dwFlags = This->flags;
897 lpDSCBCaps->dwBufferBytes = This->pdscbd->dwBufferBytes;
898 lpDSCBCaps->dwReserved = 0;
900 TRACE("returning DS_OK\n");
901 return DS_OK;
904 static HRESULT WINAPI
905 IDirectSoundCaptureBufferImpl_GetCurrentPosition(
906 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
907 LPDWORD lpdwCapturePosition,
908 LPDWORD lpdwReadPosition )
910 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
911 HRESULT hres = DS_OK;
912 TRACE( "(%p,%p,%p)\n", This, lpdwCapturePosition, lpdwReadPosition );
914 if (This->device == NULL) {
915 WARN("invalid parameter: This->device == NULL\n");
916 return DSERR_INVALIDPARAM;
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;
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);
934 } else {
935 WARN("no driver\n");
936 hres = DSERR_NODRIVER;
939 TRACE("cappos=%d readpos=%d\n", (lpdwCapturePosition?*lpdwCapturePosition:-1), (lpdwReadPosition?*lpdwReadPosition:-1));
940 TRACE("returning %08x\n", hres);
941 return hres;
944 static HRESULT WINAPI
945 IDirectSoundCaptureBufferImpl_GetFormat(
946 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
947 LPWAVEFORMATEX lpwfxFormat,
948 DWORD dwSizeAllocated,
949 LPDWORD lpdwSizeWritten )
951 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
952 HRESULT hres = DS_OK;
953 TRACE( "(%p,%p,0x%08x,%p)\n", This, lpwfxFormat, dwSizeAllocated,
954 lpdwSizeWritten );
956 if (This->device == NULL) {
957 WARN("invalid parameter: This->device == NULL\n");
958 return DSERR_INVALIDPARAM;
961 if (dwSizeAllocated > (sizeof(WAVEFORMATEX) + This->device->pwfx->cbSize))
962 dwSizeAllocated = sizeof(WAVEFORMATEX) + This->device->pwfx->cbSize;
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;
977 TRACE("returning %08x\n", hres);
978 return hres;
981 static HRESULT WINAPI
982 IDirectSoundCaptureBufferImpl_GetStatus(
983 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
984 LPDWORD lpdwStatus )
986 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
987 TRACE( "(%p, %p), thread is %04x\n", This, lpdwStatus, GetCurrentThreadId() );
989 if (This->device == NULL) {
990 WARN("invalid parameter: This->device == NULL\n");
991 return DSERR_INVALIDPARAM;
994 if (lpdwStatus == NULL) {
995 WARN("invalid parameter: lpdwStatus == NULL\n");
996 return DSERR_INVALIDPARAM;
999 *lpdwStatus = 0;
1000 EnterCriticalSection(&(This->device->lock));
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;
1010 TRACE("new This->device->state=%s, new lpdwStatus=%08x\n",
1011 captureStateString[This->device->state],*lpdwStatus);
1012 LeaveCriticalSection(&(This->device->lock));
1014 TRACE("status=%x\n", *lpdwStatus);
1015 TRACE("returning DS_OK\n");
1016 return DS_OK;
1019 static HRESULT WINAPI
1020 IDirectSoundCaptureBufferImpl_Initialize(
1021 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
1022 LPDIRECTSOUNDCAPTURE lpDSC,
1023 LPCDSCBUFFERDESC lpcDSCBDesc )
1025 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
1027 FIXME( "(%p,%p,%p): stub\n", This, lpDSC, lpcDSCBDesc );
1029 return DS_OK;
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 )
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() );
1049 if (This->device == NULL) {
1050 WARN("invalid parameter: This->device == NULL\n");
1051 return DSERR_INVALIDPARAM;
1054 if (lplpvAudioPtr1 == NULL) {
1055 WARN("invalid parameter: lplpvAudioPtr1 == NULL\n");
1056 return DSERR_INVALIDPARAM;
1059 if (lpdwAudioBytes1 == NULL) {
1060 WARN("invalid parameter: lpdwAudioBytes1 == NULL\n");
1061 return DSERR_INVALIDPARAM;
1064 EnterCriticalSection(&(This->device->lock));
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;
1088 } else {
1089 TRACE("invalid call\n");
1090 hres = DSERR_INVALIDCALL; /* DSERR_NODRIVER ? */
1093 LeaveCriticalSection(&(This->device->lock));
1095 TRACE("returning %08x\n", hres);
1096 return hres;
1099 static HRESULT WINAPI
1100 IDirectSoundCaptureBufferImpl_Start(
1101 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
1102 DWORD dwFlags )
1104 HRESULT hres = DS_OK;
1105 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
1106 TRACE( "(%p,0x%08x)\n", This, dwFlags );
1108 if (This->device == NULL) {
1109 WARN("invalid parameter: This->device == NULL\n");
1110 return DSERR_INVALIDPARAM;
1113 if ( (This->device->driver == 0) && (This->device->hwi == 0) ) {
1114 WARN("no driver\n");
1115 return DSERR_NODRIVER;
1118 EnterCriticalSection(&(This->device->lock));
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]);
1128 LeaveCriticalSection(&(This->device->lock));
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;
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);
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));
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;
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;
1176 FillMemory(device->buffer, device->buflen, (device->pwfx->wBitsPerSample == 8) ? 128 : 0);
1179 device->index = 0;
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");
1188 if (hres != DS_OK) {
1189 WARN("calling waveInClose because of error\n");
1190 waveInClose(device->hwi);
1191 device->hwi = 0;
1193 } else {
1194 WARN("no driver\n");
1195 hres = DSERR_NODRIVER;
1198 TRACE("returning %08x\n", hres);
1199 return hres;
1202 static HRESULT WINAPI
1203 IDirectSoundCaptureBufferImpl_Stop( LPDIRECTSOUNDCAPTUREBUFFER8 iface )
1205 HRESULT hres = DS_OK;
1206 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
1207 TRACE( "(%p)\n", This );
1209 if (This->device == NULL) {
1210 WARN("invalid parameter: This->device == NULL\n");
1211 return DSERR_INVALIDPARAM;
1214 EnterCriticalSection(&(This->device->lock));
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]);
1223 LeaveCriticalSection(&(This->device->lock));
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;
1238 TRACE("returning %08x\n", hres);
1239 return hres;
1242 static HRESULT WINAPI
1243 IDirectSoundCaptureBufferImpl_Unlock(
1244 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
1245 LPVOID lpvAudioPtr1,
1246 DWORD dwAudioBytes1,
1247 LPVOID lpvAudioPtr2,
1248 DWORD dwAudioBytes2 )
1250 HRESULT hres = DS_OK;
1251 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
1252 TRACE( "(%p,%p,%08u,%p,%08u)\n", This, lpvAudioPtr1, dwAudioBytes1,
1253 lpvAudioPtr2, dwAudioBytes2 );
1255 if (lpvAudioPtr1 == NULL) {
1256 WARN("invalid parameter: lpvAudioPtr1 == NULL\n");
1257 return DSERR_INVALIDPARAM;
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;
1270 TRACE("returning %08x\n", hres);
1271 return hres;
1274 static HRESULT WINAPI
1275 IDirectSoundCaptureBufferImpl_GetObjectInPath(
1276 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
1277 REFGUID rguidObject,
1278 DWORD dwIndex,
1279 REFGUID rguidInterface,
1280 LPVOID* ppObject )
1282 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
1284 FIXME( "(%p,%s,%u,%s,%p): stub\n", This, debugstr_guid(rguidObject),
1285 dwIndex, debugstr_guid(rguidInterface), ppObject );
1287 return DS_OK;
1290 static HRESULT WINAPI
1291 IDirectSoundCaptureBufferImpl_GetFXStatus(
1292 LPDIRECTSOUNDCAPTUREBUFFER8 iface,
1293 DWORD dwFXCount,
1294 LPDWORD pdwFXStatus )
1296 IDirectSoundCaptureBufferImpl *This = (IDirectSoundCaptureBufferImpl *)iface;
1298 FIXME( "(%p,%u,%p): stub\n", This, dwFXCount, pdwFXStatus );
1300 return DS_OK;
1303 static const IDirectSoundCaptureBuffer8Vtbl dscbvt =
1305 /* IUnknown methods */
1306 IDirectSoundCaptureBufferImpl_QueryInterface,
1307 IDirectSoundCaptureBufferImpl_AddRef,
1308 IDirectSoundCaptureBufferImpl_Release,
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,
1321 /* IDirectSoundCaptureBuffer methods */
1322 IDirectSoundCaptureBufferImpl_GetObjectInPath,
1323 IDirectSoundCaptureBufferImpl_GetFXStatus
1326 HRESULT IDirectSoundCaptureBufferImpl_Create(
1327 DirectSoundCaptureDevice *device,
1328 IDirectSoundCaptureBufferImpl ** ppobj,
1329 LPCDSCBUFFERDESC lpcDSCBufferDesc)
1331 LPWAVEFORMATEX wfex;
1332 TRACE( "(%p,%p,%p)\n", device, ppobj, lpcDSCBufferDesc);
1334 if (ppobj == NULL) {
1335 WARN("invalid parameter: ppobj == NULL\n");
1336 return DSERR_INVALIDPARAM;
1339 if (!device) {
1340 WARN("not initialized\n");
1341 *ppobj = NULL;
1342 return DSERR_UNINITIALIZED;
1345 if (lpcDSCBufferDesc == NULL) {
1346 WARN("invalid parameter: lpcDSCBufferDesc == NULL\n");
1347 *ppobj = NULL;
1348 return DSERR_INVALIDPARAM;
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;
1360 wfex = lpcDSCBufferDesc->lpwfxFormat;
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);
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);
1377 } else {
1378 WARN("lpcDSCBufferDesc->lpwfxFormat == 0\n");
1379 *ppobj = NULL;
1380 return DSERR_INVALIDPARAM; /* FIXME: DSERR_BADFORMAT ? */
1383 *ppobj = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,
1384 sizeof(IDirectSoundCaptureBufferImpl));
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;
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;
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;
1415 This->lpVtbl = &dscbvt;
1417 if (device->driver) {
1418 if (This->device->drvdesc.dwFlags & DSDDESC_DOMMSYSTEMOPEN)
1419 FIXME("DSDDESC_DOMMSYSTEMOPEN not supported\n");
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);
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;
1438 } else {
1439 /* let driver allocate memory */
1440 device->buflen = lpcDSCBufferDesc->dwBufferBytes;
1441 /* FIXME: */
1442 HeapFree( GetProcessHeap(), 0, device->buffer);
1443 device->buffer = NULL;
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;
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;
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;
1485 TRACE("returning DS_OK\n");
1486 return DS_OK;
1489 /*******************************************************************************
1490 * DirectSoundCaptureDevice
1492 HRESULT DirectSoundCaptureDevice_Initialize(
1493 DirectSoundCaptureDevice ** ppDevice,
1494 LPCGUID lpcGUID)
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));
1503 /* Default device? */
1504 if ( !lpcGUID || IsEqualGUID(lpcGUID, &GUID_NULL) )
1505 lpcGUID = &DSDEVID_DefaultCapture;
1507 if (GetDeviceID(lpcGUID, &devGUID) != DS_OK) {
1508 WARN("invalid parameter: lpcGUID\n");
1509 return DSERR_INVALIDPARAM;
1512 widn = waveInGetNumDevs();
1513 if (!widn) {
1514 WARN("no audio devices found\n");
1515 return DSERR_NODRIVER;
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;
1526 if (found == FALSE) {
1527 WARN("No device found matching given ID!\n");
1528 return DSERR_NODRIVER;
1531 if (DSOUND_capture[wid]) {
1532 WARN("already in use\n");
1533 return DSERR_ALLOCATED;
1536 err = DirectSoundCaptureDevice_Create(&(device));
1537 if (err != DS_OK) {
1538 WARN("DirectSoundCaptureDevice_Create failed\n");
1539 return err;
1542 *ppDevice = device;
1543 device->guid = devGUID;
1545 /* Disable the direct sound driver to force emulation if requested. */
1546 device->driver = NULL;
1547 if (ds_hw_accel != DS_HW_ACCEL_EMULATION)
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;
1555 err = DS_OK;
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;
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;
1572 device->drvdesc.dnDevNode = wid;
1574 /* open the DirectSound driver if available */
1575 if (device->driver && (err == DS_OK))
1576 err = IDsCaptureDriver_Open(device->driver);
1578 if (err == DS_OK) {
1579 *ppDevice = device;
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;
1589 } else /*if (device->hwi)*/ {
1590 WAVEINCAPSA wic;
1591 err = mmErr(waveInGetDevCapsA((UINT)device->drvdesc.dnDevNode, &wic, sizeof(wic)));
1593 if (err == DS_OK) {
1594 device->drvcaps.dwFlags = 0;
1595 lstrcpynA(device->drvdesc.szDrvname, wic.szPname,
1596 sizeof(device->drvdesc.szDrvname));
1598 device->drvcaps.dwFlags |= DSCCAPS_EMULDRIVER;
1599 device->drvcaps.dwFormats = wic.dwFormats;
1600 device->drvcaps.dwChannels = wic.wChannels;
1605 return err;
1608 static HRESULT DirectSoundCaptureDevice_Create(
1609 DirectSoundCaptureDevice ** ppDevice)
1611 DirectSoundCaptureDevice * device;
1612 TRACE("(%p)\n", ppDevice);
1614 /* Allocate memory */
1615 device = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DirectSoundCaptureDevice));
1617 if (device == NULL) {
1618 WARN("out of memory\n");
1619 return DSERR_OUTOFMEMORY;
1622 device->ref = 1;
1623 device->state = STATE_STOPPED;
1625 InitializeCriticalSection( &(device->lock) );
1626 device->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": DirectSoundCaptureDevice.lock");
1628 *ppDevice = device;
1630 return DS_OK;
1633 ULONG DirectSoundCaptureDevice_Release(
1634 DirectSoundCaptureDevice * device)
1636 ULONG ref = InterlockedDecrement(&(device->ref));
1637 TRACE("(%p) ref was %d\n", device, ref + 1);
1639 if (!ref) {
1640 TRACE("deleting object\n");
1641 if (device->capture_buffer)
1642 IDirectSoundCaptureBufferImpl_Release(
1643 (LPDIRECTSOUNDCAPTUREBUFFER8) device->capture_buffer);
1645 if (device->driver) {
1646 IDsCaptureDriver_Close(device->driver);
1647 IDsCaptureDriver_Release(device->driver);
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);
1657 return ref;