webservices: Accept zero write option in WsWriteType.
[wine.git] / dlls / dsound / capture.c
blob30870ae6ba5110ab65669ea9485aaef0e8e786f3
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 COBJMACROS
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 "dsound_private.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(dsound);
45 typedef struct DirectSoundCaptureDevice DirectSoundCaptureDevice;
47 /* IDirectSoundCaptureBuffer implementation structure */
48 typedef struct IDirectSoundCaptureBufferImpl
50 IDirectSoundCaptureBuffer8 IDirectSoundCaptureBuffer8_iface;
51 IDirectSoundNotify IDirectSoundNotify_iface;
52 LONG numIfaces; /* "in use interfaces" refcount */
53 LONG ref, refn, has_dsc8;
54 /* IDirectSoundCaptureBuffer fields */
55 DirectSoundCaptureDevice *device;
56 DSCBUFFERDESC *pdscbd;
57 DWORD flags;
58 /* IDirectSoundNotify fields */
59 DSBPOSITIONNOTIFY *notifies;
60 int nrofnotifies;
61 HANDLE thread;
62 HANDLE sleepev;
63 } IDirectSoundCaptureBufferImpl;
65 /* DirectSoundCaptureDevice implementation structure */
66 struct DirectSoundCaptureDevice
68 GUID guid;
69 LONG ref;
70 DSCCAPS drvcaps;
71 BYTE *buffer;
72 DWORD buflen, write_pos_bytes;
73 WAVEFORMATEX *pwfx;
74 IDirectSoundCaptureBufferImpl *capture_buffer;
75 DWORD state;
76 CRITICAL_SECTION lock;
77 IMMDevice *mmdevice;
78 IAudioClient *client;
79 IAudioCaptureClient *capture;
80 struct list entry;
83 static DWORD WINAPI DSOUND_capture_thread(void *user);
85 static void capturebuffer_destroy(IDirectSoundCaptureBufferImpl *This)
87 if (This->device->state == STATE_CAPTURING)
88 This->device->state = STATE_STOPPING;
90 if(This->thread){
91 SetEvent(This->sleepev);
92 WaitForSingleObject(This->thread, INFINITE);
93 CloseHandle(This->thread);
95 CloseHandle(This->sleepev);
97 HeapFree(GetProcessHeap(),0, This->pdscbd);
99 if (This->device->client) {
100 IAudioClient_Release(This->device->client);
101 This->device->client = NULL;
104 if (This->device->capture) {
105 IAudioCaptureClient_Release(This->device->capture);
106 This->device->capture = NULL;
109 /* remove from DirectSoundCaptureDevice */
110 This->device->capture_buffer = NULL;
112 HeapFree(GetProcessHeap(), 0, This->notifies);
113 HeapFree(GetProcessHeap(), 0, This);
114 TRACE("(%p) released\n", This);
117 /*******************************************************************************
118 * IDirectSoundNotify
120 static inline struct IDirectSoundCaptureBufferImpl *impl_from_IDirectSoundNotify(IDirectSoundNotify *iface)
122 return CONTAINING_RECORD(iface, IDirectSoundCaptureBufferImpl, IDirectSoundNotify_iface);
125 static HRESULT WINAPI IDirectSoundNotifyImpl_QueryInterface(IDirectSoundNotify *iface, REFIID riid,
126 void **ppobj)
128 IDirectSoundCaptureBufferImpl *This = impl_from_IDirectSoundNotify(iface);
130 TRACE("(%p,%s,%p)\n", This, debugstr_guid(riid), ppobj);
132 return IDirectSoundCaptureBuffer_QueryInterface(&This->IDirectSoundCaptureBuffer8_iface, riid, ppobj);
135 static ULONG WINAPI IDirectSoundNotifyImpl_AddRef(IDirectSoundNotify *iface)
137 IDirectSoundCaptureBufferImpl *This = impl_from_IDirectSoundNotify(iface);
138 ULONG ref = InterlockedIncrement(&This->refn);
140 TRACE("(%p) ref was %d\n", This, ref - 1);
142 if(ref == 1)
143 InterlockedIncrement(&This->numIfaces);
145 return ref;
148 static ULONG WINAPI IDirectSoundNotifyImpl_Release(IDirectSoundNotify *iface)
150 IDirectSoundCaptureBufferImpl *This = impl_from_IDirectSoundNotify(iface);
151 ULONG ref = InterlockedDecrement(&This->refn);
153 TRACE("(%p) ref was %d\n", This, ref + 1);
155 if (!ref && !InterlockedDecrement(&This->numIfaces))
156 capturebuffer_destroy(This);
158 return ref;
161 static HRESULT WINAPI IDirectSoundNotifyImpl_SetNotificationPositions(IDirectSoundNotify *iface,
162 DWORD howmuch, const DSBPOSITIONNOTIFY *notify)
164 IDirectSoundCaptureBufferImpl *This = impl_from_IDirectSoundNotify(iface);
165 TRACE("(%p,0x%08x,%p)\n",This,howmuch,notify);
167 if (howmuch > 0 && notify == NULL) {
168 WARN("invalid parameter: notify == NULL\n");
169 return DSERR_INVALIDPARAM;
172 if (TRACE_ON(dsound)) {
173 unsigned int i;
174 for (i=0;i<howmuch;i++)
175 TRACE("notify at %d to %p\n",
176 notify[i].dwOffset,notify[i].hEventNotify);
179 if (howmuch > 0) {
180 /* Make an internal copy of the caller-supplied array.
181 * Replace the existing copy if one is already present. */
182 if (This->notifies)
183 This->notifies = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->notifies,
184 howmuch * sizeof(DSBPOSITIONNOTIFY));
185 else
186 This->notifies = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
187 howmuch * sizeof(DSBPOSITIONNOTIFY));
189 if (!This->notifies) {
190 WARN("out of memory\n");
191 return DSERR_OUTOFMEMORY;
193 CopyMemory(This->notifies, notify, howmuch * sizeof(DSBPOSITIONNOTIFY));
194 This->nrofnotifies = howmuch;
195 } else {
196 HeapFree(GetProcessHeap(), 0, This->notifies);
197 This->notifies = NULL;
198 This->nrofnotifies = 0;
201 return S_OK;
204 static const IDirectSoundNotifyVtbl dscnvt =
206 IDirectSoundNotifyImpl_QueryInterface,
207 IDirectSoundNotifyImpl_AddRef,
208 IDirectSoundNotifyImpl_Release,
209 IDirectSoundNotifyImpl_SetNotificationPositions
213 static const char * const captureStateString[] = {
214 "STATE_STOPPED",
215 "STATE_STARTING",
216 "STATE_CAPTURING",
217 "STATE_STOPPING"
221 /*******************************************************************************
222 * IDirectSoundCaptureBuffer
224 static inline IDirectSoundCaptureBufferImpl *impl_from_IDirectSoundCaptureBuffer8(IDirectSoundCaptureBuffer8 *iface)
226 return CONTAINING_RECORD(iface, IDirectSoundCaptureBufferImpl, IDirectSoundCaptureBuffer8_iface);
229 static HRESULT WINAPI IDirectSoundCaptureBufferImpl_QueryInterface(IDirectSoundCaptureBuffer8 *iface,
230 REFIID riid, void **ppobj)
232 IDirectSoundCaptureBufferImpl *This = impl_from_IDirectSoundCaptureBuffer8(iface);
234 TRACE( "(%p,%s,%p)\n", This, debugstr_guid(riid), ppobj );
236 if (ppobj == NULL) {
237 WARN("invalid parameter\n");
238 return E_INVALIDARG;
241 *ppobj = NULL;
243 if ( IsEqualIID( &IID_IUnknown, riid ) ||
244 IsEqualIID( &IID_IDirectSoundCaptureBuffer, riid ) ||
245 (This->has_dsc8 && IsEqualIID( &IID_IDirectSoundCaptureBuffer8, riid )) ) {
246 IDirectSoundCaptureBuffer8_AddRef(iface);
247 *ppobj = iface;
248 return S_OK;
251 if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ) {
252 IDirectSoundNotify_AddRef(&This->IDirectSoundNotify_iface);
253 *ppobj = &This->IDirectSoundNotify_iface;
254 return S_OK;
257 FIXME("(%p,%s,%p) unsupported GUID\n", This, debugstr_guid(riid), ppobj);
258 return E_NOINTERFACE;
261 static ULONG WINAPI IDirectSoundCaptureBufferImpl_AddRef(IDirectSoundCaptureBuffer8 *iface)
263 IDirectSoundCaptureBufferImpl *This = impl_from_IDirectSoundCaptureBuffer8(iface);
264 ULONG ref = InterlockedIncrement(&This->ref);
266 TRACE("(%p) ref was %d\n", This, ref - 1);
268 if(ref == 1)
269 InterlockedIncrement(&This->numIfaces);
271 return ref;
274 static ULONG WINAPI IDirectSoundCaptureBufferImpl_Release(IDirectSoundCaptureBuffer8 *iface)
276 IDirectSoundCaptureBufferImpl *This = impl_from_IDirectSoundCaptureBuffer8(iface);
277 ULONG ref = InterlockedDecrement(&This->ref);
279 TRACE("(%p) ref was %d\n", This, ref + 1);
281 if (!ref && !InterlockedDecrement(&This->numIfaces))
282 capturebuffer_destroy(This);
284 return ref;
287 static HRESULT WINAPI IDirectSoundCaptureBufferImpl_GetCaps(IDirectSoundCaptureBuffer8 *iface,
288 DSCBCAPS *lpDSCBCaps)
290 IDirectSoundCaptureBufferImpl *This = impl_from_IDirectSoundCaptureBuffer8(iface);
291 TRACE( "(%p,%p)\n", This, lpDSCBCaps );
293 if (lpDSCBCaps == NULL) {
294 WARN("invalid parameter: lpDSCBCaps == NULL\n");
295 return DSERR_INVALIDPARAM;
298 if (lpDSCBCaps->dwSize < sizeof(DSCBCAPS)) {
299 WARN("invalid parameter: lpDSCBCaps->dwSize = %d\n", lpDSCBCaps->dwSize);
300 return DSERR_INVALIDPARAM;
303 if (This->device == NULL) {
304 WARN("invalid parameter: This->device == NULL\n");
305 return DSERR_INVALIDPARAM;
308 lpDSCBCaps->dwSize = sizeof(DSCBCAPS);
309 lpDSCBCaps->dwFlags = This->flags;
310 lpDSCBCaps->dwBufferBytes = This->pdscbd->dwBufferBytes;
311 lpDSCBCaps->dwReserved = 0;
313 TRACE("returning DS_OK\n");
314 return DS_OK;
317 static HRESULT WINAPI IDirectSoundCaptureBufferImpl_GetCurrentPosition(IDirectSoundCaptureBuffer8 *iface,
318 DWORD *lpdwCapturePosition, DWORD *lpdwReadPosition)
320 IDirectSoundCaptureBufferImpl *This = impl_from_IDirectSoundCaptureBuffer8(iface);
322 TRACE( "(%p,%p,%p)\n", This, lpdwCapturePosition, lpdwReadPosition );
324 if (This->device == NULL) {
325 WARN("invalid parameter: This->device == NULL\n");
326 return DSERR_INVALIDPARAM;
329 EnterCriticalSection(&This->device->lock);
331 if (!This->device->client) {
332 LeaveCriticalSection(&This->device->lock);
333 WARN("no driver\n");
334 return DSERR_NODRIVER;
337 if(lpdwCapturePosition)
338 *lpdwCapturePosition = This->device->write_pos_bytes;
340 if(lpdwReadPosition)
341 *lpdwReadPosition = This->device->write_pos_bytes;
343 LeaveCriticalSection(&This->device->lock);
345 TRACE("cappos=%d readpos=%d\n", (lpdwCapturePosition?*lpdwCapturePosition:-1), (lpdwReadPosition?*lpdwReadPosition:-1));
346 TRACE("returning DS_OK\n");
348 return DS_OK;
351 static HRESULT WINAPI IDirectSoundCaptureBufferImpl_GetFormat(IDirectSoundCaptureBuffer8 *iface,
352 WAVEFORMATEX *lpwfxFormat, DWORD dwSizeAllocated, DWORD *lpdwSizeWritten)
354 IDirectSoundCaptureBufferImpl *This = impl_from_IDirectSoundCaptureBuffer8(iface);
355 HRESULT hres = DS_OK;
357 TRACE("(%p,%p,0x%08x,%p)\n", This, lpwfxFormat, dwSizeAllocated, lpdwSizeWritten);
359 if (This->device == NULL) {
360 WARN("invalid parameter: This->device == NULL\n");
361 return DSERR_INVALIDPARAM;
364 if (dwSizeAllocated > (sizeof(WAVEFORMATEX) + This->device->pwfx->cbSize))
365 dwSizeAllocated = sizeof(WAVEFORMATEX) + This->device->pwfx->cbSize;
367 if (lpwfxFormat) { /* NULL is valid (just want size) */
368 CopyMemory(lpwfxFormat, This->device->pwfx, dwSizeAllocated);
369 if (lpdwSizeWritten)
370 *lpdwSizeWritten = dwSizeAllocated;
371 } else {
372 if (lpdwSizeWritten)
373 *lpdwSizeWritten = sizeof(WAVEFORMATEX) + This->device->pwfx->cbSize;
374 else {
375 TRACE("invalid parameter: lpdwSizeWritten = NULL\n");
376 hres = DSERR_INVALIDPARAM;
380 TRACE("returning %08x\n", hres);
381 return hres;
384 static HRESULT WINAPI IDirectSoundCaptureBufferImpl_GetStatus(IDirectSoundCaptureBuffer8 *iface,
385 DWORD *lpdwStatus)
387 IDirectSoundCaptureBufferImpl *This = impl_from_IDirectSoundCaptureBuffer8(iface);
389 TRACE( "(%p, %p), thread is %04x\n", This, lpdwStatus, GetCurrentThreadId() );
391 if (This->device == NULL) {
392 WARN("invalid parameter: This->device == NULL\n");
393 return DSERR_INVALIDPARAM;
396 if (lpdwStatus == NULL) {
397 WARN("invalid parameter: lpdwStatus == NULL\n");
398 return DSERR_INVALIDPARAM;
401 *lpdwStatus = 0;
402 EnterCriticalSection(&(This->device->lock));
404 TRACE("old This->device->state=%s, old lpdwStatus=%08x\n",
405 captureStateString[This->device->state],*lpdwStatus);
406 if ((This->device->state == STATE_STARTING) ||
407 (This->device->state == STATE_CAPTURING)) {
408 *lpdwStatus |= DSCBSTATUS_CAPTURING;
409 if (This->flags & DSCBSTART_LOOPING)
410 *lpdwStatus |= DSCBSTATUS_LOOPING;
412 TRACE("new This->device->state=%s, new lpdwStatus=%08x\n",
413 captureStateString[This->device->state],*lpdwStatus);
414 LeaveCriticalSection(&(This->device->lock));
416 TRACE("status=%x\n", *lpdwStatus);
417 TRACE("returning DS_OK\n");
418 return DS_OK;
421 static HRESULT WINAPI IDirectSoundCaptureBufferImpl_Initialize(IDirectSoundCaptureBuffer8 *iface,
422 IDirectSoundCapture *lpDSC, const DSCBUFFERDESC *lpcDSCBDesc)
424 IDirectSoundCaptureBufferImpl *This = impl_from_IDirectSoundCaptureBuffer8(iface);
426 FIXME( "(%p,%p,%p): stub\n", This, lpDSC, lpcDSCBDesc );
428 return DS_OK;
431 static HRESULT WINAPI IDirectSoundCaptureBufferImpl_Lock(IDirectSoundCaptureBuffer8 *iface,
432 DWORD dwReadCusor, DWORD dwReadBytes, void **lplpvAudioPtr1, DWORD *lpdwAudioBytes1,
433 void **lplpvAudioPtr2, DWORD *lpdwAudioBytes2, DWORD dwFlags)
435 IDirectSoundCaptureBufferImpl *This = impl_from_IDirectSoundCaptureBuffer8(iface);
436 HRESULT hres = DS_OK;
438 TRACE( "(%p,%08u,%08u,%p,%p,%p,%p,0x%08x) at %d\n", This, dwReadCusor,
439 dwReadBytes, lplpvAudioPtr1, lpdwAudioBytes1, lplpvAudioPtr2,
440 lpdwAudioBytes2, dwFlags, GetTickCount() );
442 if (This->device == NULL) {
443 WARN("invalid parameter: This->device == NULL\n");
444 return DSERR_INVALIDPARAM;
447 if (lplpvAudioPtr1 == NULL) {
448 WARN("invalid parameter: lplpvAudioPtr1 == NULL\n");
449 return DSERR_INVALIDPARAM;
452 if (lpdwAudioBytes1 == NULL) {
453 WARN("invalid parameter: lpdwAudioBytes1 == NULL\n");
454 return DSERR_INVALIDPARAM;
457 EnterCriticalSection(&(This->device->lock));
459 if (This->device->client) {
460 *lplpvAudioPtr1 = This->device->buffer + dwReadCusor;
461 if ( (dwReadCusor + dwReadBytes) > This->device->buflen) {
462 *lpdwAudioBytes1 = This->device->buflen - dwReadCusor;
463 if (lplpvAudioPtr2)
464 *lplpvAudioPtr2 = This->device->buffer;
465 if (lpdwAudioBytes2)
466 *lpdwAudioBytes2 = dwReadBytes - *lpdwAudioBytes1;
467 } else {
468 *lpdwAudioBytes1 = dwReadBytes;
469 if (lplpvAudioPtr2)
470 *lplpvAudioPtr2 = 0;
471 if (lpdwAudioBytes2)
472 *lpdwAudioBytes2 = 0;
474 } else {
475 TRACE("invalid call\n");
476 hres = DSERR_INVALIDCALL; /* DSERR_NODRIVER ? */
479 LeaveCriticalSection(&(This->device->lock));
481 TRACE("returning %08x\n", hres);
482 return hres;
485 static HRESULT WINAPI IDirectSoundCaptureBufferImpl_Start(IDirectSoundCaptureBuffer8 *iface,
486 DWORD dwFlags)
488 IDirectSoundCaptureBufferImpl *This = impl_from_IDirectSoundCaptureBuffer8(iface);
489 HRESULT hres;
491 TRACE( "(%p,0x%08x)\n", This, dwFlags );
493 if (This->device == NULL) {
494 WARN("invalid parameter: This->device == NULL\n");
495 return DSERR_INVALIDPARAM;
498 if ( !This->device->client ) {
499 WARN("no driver\n");
500 return DSERR_NODRIVER;
503 EnterCriticalSection(&(This->device->lock));
505 if (This->device->state == STATE_STOPPED)
506 This->device->state = STATE_STARTING;
507 else if (This->device->state == STATE_STOPPING)
508 This->device->state = STATE_CAPTURING;
509 else
510 goto out;
511 TRACE("new This->device->state=%s\n",captureStateString[This->device->state]);
512 This->flags = dwFlags;
514 if (This->device->buffer)
515 FillMemory(This->device->buffer, This->device->buflen, (This->device->pwfx->wBitsPerSample == 8) ? 128 : 0);
517 hres = IAudioClient_Start(This->device->client);
518 if(FAILED(hres)){
519 WARN("Start failed: %08x\n", hres);
520 LeaveCriticalSection(&This->device->lock);
521 return hres;
524 out:
525 LeaveCriticalSection(&This->device->lock);
527 TRACE("returning DS_OK\n");
528 return DS_OK;
531 static HRESULT WINAPI IDirectSoundCaptureBufferImpl_Stop(IDirectSoundCaptureBuffer8 *iface)
533 IDirectSoundCaptureBufferImpl *This = impl_from_IDirectSoundCaptureBuffer8(iface);
534 HRESULT hres;
536 TRACE("(%p)\n", This);
538 if (This->device == NULL) {
539 WARN("invalid parameter: This->device == NULL\n");
540 return DSERR_INVALIDPARAM;
543 EnterCriticalSection(&(This->device->lock));
545 TRACE("old This->device->state=%s\n",captureStateString[This->device->state]);
546 if (This->device->state == STATE_CAPTURING)
547 This->device->state = STATE_STOPPING;
548 else if (This->device->state == STATE_STARTING)
549 This->device->state = STATE_STOPPED;
550 TRACE("new This->device->state=%s\n",captureStateString[This->device->state]);
552 if(This->device->client){
553 hres = IAudioClient_Stop(This->device->client);
554 if(FAILED(hres)){
555 LeaveCriticalSection(&This->device->lock);
556 return hres;
560 LeaveCriticalSection(&(This->device->lock));
562 TRACE("returning DS_OK\n");
563 return DS_OK;
566 static HRESULT WINAPI IDirectSoundCaptureBufferImpl_Unlock(IDirectSoundCaptureBuffer8 *iface,
567 void *lpvAudioPtr1, DWORD dwAudioBytes1, void *lpvAudioPtr2, DWORD dwAudioBytes2)
569 IDirectSoundCaptureBufferImpl *This = impl_from_IDirectSoundCaptureBuffer8(iface);
570 HRESULT hres = DS_OK;
572 TRACE( "(%p,%p,%08u,%p,%08u)\n", This, lpvAudioPtr1, dwAudioBytes1,
573 lpvAudioPtr2, dwAudioBytes2 );
575 if (lpvAudioPtr1 == NULL) {
576 WARN("invalid parameter: lpvAudioPtr1 == NULL\n");
577 return DSERR_INVALIDPARAM;
580 if (!This->device->client) {
581 WARN("invalid call\n");
582 hres = DSERR_INVALIDCALL;
585 TRACE("returning %08x\n", hres);
586 return hres;
589 static HRESULT WINAPI IDirectSoundCaptureBufferImpl_GetObjectInPath(IDirectSoundCaptureBuffer8 *iface,
590 REFGUID rguidObject, DWORD dwIndex, REFGUID rguidInterface, void **ppObject)
592 IDirectSoundCaptureBufferImpl *This = impl_from_IDirectSoundCaptureBuffer8(iface);
594 FIXME( "(%p,%s,%u,%s,%p): stub\n", This, debugstr_guid(rguidObject),
595 dwIndex, debugstr_guid(rguidInterface), ppObject );
597 if (!ppObject)
598 return DSERR_INVALIDPARAM;
600 *ppObject = NULL;
601 return DSERR_CONTROLUNAVAIL;
604 static HRESULT WINAPI IDirectSoundCaptureBufferImpl_GetFXStatus(IDirectSoundCaptureBuffer8 *iface,
605 DWORD dwFXCount, DWORD *pdwFXStatus)
607 IDirectSoundCaptureBufferImpl *This = impl_from_IDirectSoundCaptureBuffer8(iface);
609 FIXME( "(%p,%u,%p): stub\n", This, dwFXCount, pdwFXStatus );
611 return DS_OK;
614 static const IDirectSoundCaptureBuffer8Vtbl dscbvt =
616 /* IUnknown methods */
617 IDirectSoundCaptureBufferImpl_QueryInterface,
618 IDirectSoundCaptureBufferImpl_AddRef,
619 IDirectSoundCaptureBufferImpl_Release,
621 /* IDirectSoundCaptureBuffer methods */
622 IDirectSoundCaptureBufferImpl_GetCaps,
623 IDirectSoundCaptureBufferImpl_GetCurrentPosition,
624 IDirectSoundCaptureBufferImpl_GetFormat,
625 IDirectSoundCaptureBufferImpl_GetStatus,
626 IDirectSoundCaptureBufferImpl_Initialize,
627 IDirectSoundCaptureBufferImpl_Lock,
628 IDirectSoundCaptureBufferImpl_Start,
629 IDirectSoundCaptureBufferImpl_Stop,
630 IDirectSoundCaptureBufferImpl_Unlock,
632 /* IDirectSoundCaptureBuffer methods */
633 IDirectSoundCaptureBufferImpl_GetObjectInPath,
634 IDirectSoundCaptureBufferImpl_GetFXStatus
637 static void capture_CheckNotify(IDirectSoundCaptureBufferImpl *This, DWORD from, DWORD len)
639 int i;
640 for (i = 0; i < This->nrofnotifies; ++i) {
641 LPDSBPOSITIONNOTIFY event = This->notifies + i;
642 DWORD offset = event->dwOffset;
643 TRACE("checking %d, position %d, event = %p\n", i, offset, event->hEventNotify);
645 if (offset == DSBPN_OFFSETSTOP) {
646 if (!from && !len) {
647 SetEvent(event->hEventNotify);
648 TRACE("signalled event %p (%d)\n", event->hEventNotify, i);
649 return;
651 else return;
654 if (offset >= from && offset < (from + len))
656 TRACE("signalled event %p (%d)\n", event->hEventNotify, i);
657 SetEvent(event->hEventNotify);
662 static HRESULT IDirectSoundCaptureBufferImpl_Create(
663 DirectSoundCaptureDevice *device,
664 IDirectSoundCaptureBufferImpl ** ppobj,
665 LPCDSCBUFFERDESC lpcDSCBufferDesc)
667 LPWAVEFORMATEX wfex;
668 IDirectSoundCaptureBufferImpl *This;
669 TRACE( "(%p,%p,%p)\n", device, ppobj, lpcDSCBufferDesc);
671 if (ppobj == NULL) {
672 WARN("invalid parameter: ppobj == NULL\n");
673 return DSERR_INVALIDPARAM;
676 *ppobj = NULL;
678 if (!device) {
679 WARN("not initialized\n");
680 return DSERR_UNINITIALIZED;
683 if (lpcDSCBufferDesc == NULL) {
684 WARN("invalid parameter: lpcDSCBufferDesc == NULL\n");
685 return DSERR_INVALIDPARAM;
688 if ( ((lpcDSCBufferDesc->dwSize != sizeof(DSCBUFFERDESC)) &&
689 (lpcDSCBufferDesc->dwSize != sizeof(DSCBUFFERDESC1))) ||
690 (lpcDSCBufferDesc->dwBufferBytes == 0) ||
691 (lpcDSCBufferDesc->lpwfxFormat == NULL) ) { /* FIXME: DSERR_BADFORMAT ? */
692 WARN("invalid lpcDSCBufferDesc\n");
693 return DSERR_INVALIDPARAM;
696 wfex = lpcDSCBufferDesc->lpwfxFormat;
698 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
699 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
700 wfex->wFormatTag, wfex->nChannels, wfex->nSamplesPerSec,
701 wfex->nAvgBytesPerSec, wfex->nBlockAlign,
702 wfex->wBitsPerSample, wfex->cbSize);
704 device->pwfx = DSOUND_CopyFormat(wfex);
705 if ( device->pwfx == NULL )
706 return DSERR_OUTOFMEMORY;
708 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,
709 sizeof(IDirectSoundCaptureBufferImpl));
711 if ( This == NULL ) {
712 WARN("out of memory\n");
713 return DSERR_OUTOFMEMORY;
714 } else {
715 HRESULT err = DS_OK;
716 LPBYTE newbuf;
717 DWORD buflen;
719 This->numIfaces = 0;
720 This->ref = 0;
721 This->refn = 0;
722 This->device = device;
723 This->device->capture_buffer = This;
724 This->nrofnotifies = 0;
726 This->pdscbd = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,
727 lpcDSCBufferDesc->dwSize);
728 if (This->pdscbd)
729 CopyMemory(This->pdscbd, lpcDSCBufferDesc, lpcDSCBufferDesc->dwSize);
730 else {
731 WARN("no memory\n");
732 This->device->capture_buffer = 0;
733 HeapFree( GetProcessHeap(), 0, This );
734 return DSERR_OUTOFMEMORY;
737 This->IDirectSoundCaptureBuffer8_iface.lpVtbl = &dscbvt;
738 This->IDirectSoundNotify_iface.lpVtbl = &dscnvt;
740 err = IMMDevice_Activate(device->mmdevice, &IID_IAudioClient,
741 CLSCTX_INPROC_SERVER, NULL, (void**)&device->client);
742 if(FAILED(err)){
743 WARN("Activate failed: %08x\n", err);
744 HeapFree(GetProcessHeap(), 0, This->pdscbd);
745 This->device->capture_buffer = 0;
746 HeapFree( GetProcessHeap(), 0, This );
747 return err;
750 err = IAudioClient_Initialize(device->client,
751 AUDCLNT_SHAREMODE_SHARED, AUDCLNT_STREAMFLAGS_NOPERSIST | AUDCLNT_STREAMFLAGS_EVENTCALLBACK,
752 200 * 100000, 0, device->pwfx, NULL);
753 if(FAILED(err)){
754 WARN("Initialize failed: %08x\n", err);
755 IAudioClient_Release(device->client);
756 device->client = NULL;
757 HeapFree(GetProcessHeap(), 0, This->pdscbd);
758 This->device->capture_buffer = 0;
759 HeapFree( GetProcessHeap(), 0, This );
760 if(err == AUDCLNT_E_UNSUPPORTED_FORMAT)
761 return DSERR_BADFORMAT;
762 return err;
765 This->sleepev = CreateEventW(NULL, 0, 0, NULL);
767 err = IAudioClient_SetEventHandle(device->client, This->sleepev);
768 if(FAILED(err)){
769 WARN("SetEventHandle failed: %08x\n", err);
770 IAudioClient_Release(device->client);
771 device->client = NULL;
772 CloseHandle(This->sleepev);
773 HeapFree(GetProcessHeap(), 0, This->pdscbd);
774 This->device->capture_buffer = 0;
775 HeapFree( GetProcessHeap(), 0, This );
776 return err;
779 err = IAudioClient_GetService(device->client, &IID_IAudioCaptureClient,
780 (void**)&device->capture);
781 if(FAILED(err)){
782 WARN("GetService failed: %08x\n", err);
783 IAudioClient_Release(device->client);
784 device->client = NULL;
785 CloseHandle(This->sleepev);
786 HeapFree(GetProcessHeap(), 0, This->pdscbd);
787 This->device->capture_buffer = 0;
788 HeapFree( GetProcessHeap(), 0, This );
789 return err;
792 buflen = lpcDSCBufferDesc->dwBufferBytes;
793 TRACE("desired buflen=%d, old buffer=%p\n", buflen, device->buffer);
794 if (device->buffer)
795 newbuf = HeapReAlloc(GetProcessHeap(),0,device->buffer,buflen);
796 else
797 newbuf = HeapAlloc(GetProcessHeap(),0,buflen);
798 if (newbuf == NULL) {
799 IAudioClient_Release(device->client);
800 device->client = NULL;
801 IAudioCaptureClient_Release(device->capture);
802 device->capture = NULL;
803 CloseHandle(This->sleepev);
804 HeapFree(GetProcessHeap(), 0, This->pdscbd);
805 This->device->capture_buffer = 0;
806 HeapFree( GetProcessHeap(), 0, This );
807 return DSERR_OUTOFMEMORY;
809 device->buffer = newbuf;
810 device->buflen = buflen;
811 This->thread = CreateThread(NULL, 0, DSOUND_capture_thread, This, 0, NULL);
814 IDirectSoundCaptureBuffer_AddRef(&This->IDirectSoundCaptureBuffer8_iface);
815 *ppobj = This;
817 TRACE("returning DS_OK\n");
818 return DS_OK;
822 /*******************************************************************************
823 * DirectSoundCaptureDevice
825 static HRESULT DirectSoundCaptureDevice_Create(
826 DirectSoundCaptureDevice ** ppDevice)
828 DirectSoundCaptureDevice * device;
829 TRACE("(%p)\n", ppDevice);
831 /* Allocate memory */
832 device = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DirectSoundCaptureDevice));
834 if (device == NULL) {
835 WARN("out of memory\n");
836 return DSERR_OUTOFMEMORY;
839 device->ref = 1;
840 device->state = STATE_STOPPED;
842 InitializeCriticalSection( &(device->lock) );
843 device->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": DirectSoundCaptureDevice.lock");
845 *ppDevice = device;
847 return DS_OK;
850 static ULONG DirectSoundCaptureDevice_Release(
851 DirectSoundCaptureDevice * device)
853 ULONG ref = InterlockedDecrement(&(device->ref));
854 TRACE("(%p) ref was %d\n", device, ref + 1);
856 if (!ref) {
857 TRACE("deleting object\n");
859 EnterCriticalSection(&DSOUND_capturers_lock);
860 list_remove(&device->entry);
861 LeaveCriticalSection(&DSOUND_capturers_lock);
863 if (device->capture_buffer)
864 IDirectSoundCaptureBufferImpl_Release(&device->capture_buffer->IDirectSoundCaptureBuffer8_iface);
866 if(device->mmdevice)
867 IMMDevice_Release(device->mmdevice);
868 HeapFree(GetProcessHeap(), 0, device->pwfx);
869 device->lock.DebugInfo->Spare[0] = 0;
870 DeleteCriticalSection( &(device->lock) );
871 HeapFree(GetProcessHeap(), 0, device);
872 TRACE("(%p) released\n", device);
874 return ref;
877 static HRESULT DSOUND_capture_data(DirectSoundCaptureDevice *device)
879 if(!device->capture_buffer || device->state == STATE_STOPPED)
880 return S_FALSE;
882 if(device->state == STATE_STOPPING){
883 device->state = STATE_STOPPED;
884 return S_FALSE;
887 if(device->state == STATE_STARTING)
888 device->state = STATE_CAPTURING;
890 while(1){
891 HRESULT hr;
892 UINT32 packet_frames, packet_bytes, avail_bytes, skip_bytes = 0;
893 DWORD flags;
894 BYTE *buf;
896 hr = IAudioCaptureClient_GetBuffer(device->capture, &buf, &packet_frames,
897 &flags, NULL, NULL);
898 if(FAILED(hr)){
899 WARN("GetBuffer failed: %08x\n", hr);
900 return hr;
902 if(hr == AUDCLNT_S_BUFFER_EMPTY)
903 break;
905 packet_bytes = packet_frames * device->pwfx->nBlockAlign;
906 if(packet_bytes > device->buflen){
907 TRACE("audio glitch: dsound buffer too small for data\n");
908 skip_bytes = packet_bytes - device->buflen;
909 packet_bytes = device->buflen;
912 avail_bytes = device->buflen - device->write_pos_bytes;
913 if(avail_bytes > packet_bytes)
914 avail_bytes = packet_bytes;
916 memcpy(device->buffer + device->write_pos_bytes, buf + skip_bytes, avail_bytes);
917 capture_CheckNotify(device->capture_buffer, device->write_pos_bytes, avail_bytes);
919 packet_bytes -= avail_bytes;
920 if(packet_bytes > 0){
921 if(device->capture_buffer->flags & DSCBSTART_LOOPING){
922 memcpy(device->buffer, buf + skip_bytes + avail_bytes, packet_bytes);
923 capture_CheckNotify(device->capture_buffer, 0, packet_bytes);
924 }else{
925 device->state = STATE_STOPPED;
926 capture_CheckNotify(device->capture_buffer, 0, 0);
930 device->write_pos_bytes += avail_bytes + packet_bytes;
931 device->write_pos_bytes %= device->buflen;
933 hr = IAudioCaptureClient_ReleaseBuffer(device->capture, packet_frames);
934 if(FAILED(hr)){
935 WARN("ReleaseBuffer failed: %08x\n", hr);
936 return hr;
940 return S_OK;
943 static DWORD WINAPI DSOUND_capture_thread(void *user)
945 IDirectSoundCaptureBufferImpl *buffer = user;
946 HRESULT hr;
947 DWORD ret, wait_ms;
948 REFERENCE_TIME period;
950 hr = IAudioClient_GetDevicePeriod(buffer->device->client, &period, NULL);
951 if(FAILED(hr)){
952 WARN("GetDevicePeriod failed: %08x\n", hr);
953 wait_ms = 5;
954 }else
955 wait_ms = MulDiv(5, period, 10000);
957 while(buffer->ref){
958 ret = WaitForSingleObject(buffer->sleepev, wait_ms);
960 if(!buffer->device->ref)
961 break;
963 if(ret == WAIT_OBJECT_0){
964 EnterCriticalSection(&buffer->device->lock);
966 DSOUND_capture_data(buffer->device);
968 LeaveCriticalSection(&buffer->device->lock);
969 }else if(ret != WAIT_TIMEOUT)
970 WARN("WaitForSingleObject failed: %u\n", GetLastError());
973 return 0;
976 static struct _TestFormat {
977 DWORD flag;
978 DWORD rate;
979 DWORD depth;
980 WORD channels;
981 } formats_to_test[] = {
982 { WAVE_FORMAT_1M08, 11025, 8, 1 },
983 { WAVE_FORMAT_1M16, 11025, 16, 1 },
984 { WAVE_FORMAT_1S08, 11025, 8, 2 },
985 { WAVE_FORMAT_1S16, 11025, 16, 2 },
986 { WAVE_FORMAT_2M08, 22050, 8, 1 },
987 { WAVE_FORMAT_2M16, 22050, 16, 1 },
988 { WAVE_FORMAT_2S08, 22050, 8, 2 },
989 { WAVE_FORMAT_2S16, 22050, 16, 2 },
990 { WAVE_FORMAT_4M08, 44100, 8, 1 },
991 { WAVE_FORMAT_4M16, 44100, 16, 1 },
992 { WAVE_FORMAT_4S08, 44100, 8, 2 },
993 { WAVE_FORMAT_4S16, 44100, 16, 2 },
994 { WAVE_FORMAT_48M08, 48000, 8, 1 },
995 { WAVE_FORMAT_48M16, 48000, 16, 1 },
996 { WAVE_FORMAT_48S08, 48000, 8, 2 },
997 { WAVE_FORMAT_48S16, 48000, 16, 2 },
998 { WAVE_FORMAT_96M08, 96000, 8, 1 },
999 { WAVE_FORMAT_96M16, 96000, 16, 1 },
1000 { WAVE_FORMAT_96S08, 96000, 8, 2 },
1001 { WAVE_FORMAT_96S16, 96000, 16, 2 },
1005 static HRESULT DirectSoundCaptureDevice_Initialize(
1006 DirectSoundCaptureDevice ** ppDevice,
1007 LPCGUID lpcGUID)
1009 HRESULT hr;
1010 GUID devGUID;
1011 IMMDevice *mmdevice;
1012 struct _TestFormat *fmt;
1013 DirectSoundCaptureDevice *device;
1014 IAudioClient *client;
1016 TRACE("(%p, %s)\n", ppDevice, debugstr_guid(lpcGUID));
1018 /* Default device? */
1019 if ( !lpcGUID || IsEqualGUID(lpcGUID, &GUID_NULL) )
1020 lpcGUID = &DSDEVID_DefaultCapture;
1022 if(IsEqualGUID(lpcGUID, &DSDEVID_DefaultPlayback) ||
1023 IsEqualGUID(lpcGUID, &DSDEVID_DefaultVoicePlayback))
1024 return DSERR_NODRIVER;
1026 if (GetDeviceID(lpcGUID, &devGUID) != DS_OK) {
1027 WARN("invalid parameter: lpcGUID\n");
1028 return DSERR_INVALIDPARAM;
1031 hr = get_mmdevice(eCapture, &devGUID, &mmdevice);
1032 if(FAILED(hr))
1033 return hr;
1035 EnterCriticalSection(&DSOUND_capturers_lock);
1037 hr = DirectSoundCaptureDevice_Create(&device);
1038 if (hr != DS_OK) {
1039 WARN("DirectSoundCaptureDevice_Create failed\n");
1040 LeaveCriticalSection(&DSOUND_capturers_lock);
1041 return hr;
1044 device->guid = devGUID;
1046 device->mmdevice = mmdevice;
1048 device->drvcaps.dwFlags = 0;
1050 device->drvcaps.dwFormats = 0;
1051 device->drvcaps.dwChannels = 0;
1052 hr = IMMDevice_Activate(mmdevice, &IID_IAudioClient,
1053 CLSCTX_INPROC_SERVER, NULL, (void**)&client);
1054 if(FAILED(hr)){
1055 device->lock.DebugInfo->Spare[0] = 0;
1056 DeleteCriticalSection(&device->lock);
1057 HeapFree(GetProcessHeap(), 0, device);
1058 LeaveCriticalSection(&DSOUND_capturers_lock);
1059 return DSERR_NODRIVER;
1062 for(fmt = formats_to_test; fmt->flag; ++fmt){
1063 if(DSOUND_check_supported(client, fmt->rate, fmt->depth, fmt->channels)){
1064 device->drvcaps.dwFormats |= fmt->flag;
1065 if(fmt->channels > device->drvcaps.dwChannels)
1066 device->drvcaps.dwChannels = fmt->channels;
1069 IAudioClient_Release(client);
1071 list_add_tail(&DSOUND_capturers, &device->entry);
1073 *ppDevice = device;
1075 LeaveCriticalSection(&DSOUND_capturers_lock);
1077 return S_OK;
1081 /*****************************************************************************
1082 * IDirectSoundCapture implementation structure
1084 typedef struct IDirectSoundCaptureImpl
1086 IUnknown IUnknown_inner;
1087 IDirectSoundCapture IDirectSoundCapture_iface;
1088 LONG ref, refdsc, numIfaces;
1089 IUnknown *outer_unk; /* internal */
1090 DirectSoundCaptureDevice *device;
1091 BOOL has_dsc8;
1092 } IDirectSoundCaptureImpl;
1094 static void capture_destroy(IDirectSoundCaptureImpl *This)
1096 if (This->device)
1097 DirectSoundCaptureDevice_Release(This->device);
1098 HeapFree(GetProcessHeap(),0,This);
1099 TRACE("(%p) released\n", This);
1102 /*******************************************************************************
1103 * IUnknown Implementation for DirectSoundCapture
1105 static inline IDirectSoundCaptureImpl *impl_from_IUnknown(IUnknown *iface)
1107 return CONTAINING_RECORD(iface, IDirectSoundCaptureImpl, IUnknown_inner);
1110 static HRESULT WINAPI IUnknownImpl_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
1112 IDirectSoundCaptureImpl *This = impl_from_IUnknown(iface);
1114 TRACE("(%p,%s,%p)\n", This, debugstr_guid(riid), ppv);
1116 if (!ppv) {
1117 WARN("invalid parameter\n");
1118 return E_INVALIDARG;
1120 *ppv = NULL;
1122 if (IsEqualIID(riid, &IID_IUnknown))
1123 *ppv = &This->IUnknown_inner;
1124 else if (IsEqualIID(riid, &IID_IDirectSoundCapture))
1125 *ppv = &This->IDirectSoundCapture_iface;
1126 else {
1127 WARN("unknown IID %s\n", debugstr_guid(riid));
1128 return E_NOINTERFACE;
1131 IUnknown_AddRef((IUnknown*)*ppv);
1132 return S_OK;
1135 static ULONG WINAPI IUnknownImpl_AddRef(IUnknown *iface)
1137 IDirectSoundCaptureImpl *This = impl_from_IUnknown(iface);
1138 ULONG ref = InterlockedIncrement(&This->ref);
1140 TRACE("(%p) ref=%d\n", This, ref);
1142 if(ref == 1)
1143 InterlockedIncrement(&This->numIfaces);
1144 return ref;
1147 static ULONG WINAPI IUnknownImpl_Release(IUnknown *iface)
1149 IDirectSoundCaptureImpl *This = impl_from_IUnknown(iface);
1150 ULONG ref = InterlockedDecrement(&This->ref);
1152 TRACE("(%p) ref=%d\n", This, ref);
1154 if (!ref && !InterlockedDecrement(&This->numIfaces))
1155 capture_destroy(This);
1156 return ref;
1159 static const IUnknownVtbl unk_vtbl =
1161 IUnknownImpl_QueryInterface,
1162 IUnknownImpl_AddRef,
1163 IUnknownImpl_Release
1166 /***************************************************************************
1167 * IDirectSoundCaptureImpl
1169 static inline struct IDirectSoundCaptureImpl *impl_from_IDirectSoundCapture(IDirectSoundCapture *iface)
1171 return CONTAINING_RECORD(iface, struct IDirectSoundCaptureImpl, IDirectSoundCapture_iface);
1174 static HRESULT WINAPI IDirectSoundCaptureImpl_QueryInterface(IDirectSoundCapture *iface,
1175 REFIID riid, void **ppv)
1177 IDirectSoundCaptureImpl *This = impl_from_IDirectSoundCapture(iface);
1178 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(riid), ppv);
1179 return IUnknown_QueryInterface(This->outer_unk, riid, ppv);
1182 static ULONG WINAPI IDirectSoundCaptureImpl_AddRef(IDirectSoundCapture *iface)
1184 IDirectSoundCaptureImpl *This = impl_from_IDirectSoundCapture(iface);
1185 ULONG ref = InterlockedIncrement(&This->refdsc);
1187 TRACE("(%p) ref=%d\n", This, ref);
1189 if(ref == 1)
1190 InterlockedIncrement(&This->numIfaces);
1191 return ref;
1194 static ULONG WINAPI IDirectSoundCaptureImpl_Release(IDirectSoundCapture *iface)
1196 IDirectSoundCaptureImpl *This = impl_from_IDirectSoundCapture(iface);
1197 ULONG ref = InterlockedDecrement(&This->refdsc);
1199 TRACE("(%p) ref=%d\n", This, ref);
1201 if (!ref && !InterlockedDecrement(&This->numIfaces))
1202 capture_destroy(This);
1203 return ref;
1206 static HRESULT WINAPI IDirectSoundCaptureImpl_CreateCaptureBuffer(IDirectSoundCapture *iface,
1207 LPCDSCBUFFERDESC lpcDSCBufferDesc, IDirectSoundCaptureBuffer **lplpDSCaptureBuffer,
1208 IUnknown *pUnk)
1210 IDirectSoundCaptureImpl *This = impl_from_IDirectSoundCapture(iface);
1211 HRESULT hr;
1213 TRACE( "(%p,%p,%p,%p)\n",iface,lpcDSCBufferDesc,lplpDSCaptureBuffer,pUnk);
1215 if (pUnk) {
1216 WARN("invalid parameter: pUnk != NULL\n");
1217 return DSERR_NOAGGREGATION;
1220 if (lpcDSCBufferDesc == NULL) {
1221 WARN("invalid parameter: lpcDSCBufferDesc == NULL)\n");
1222 return DSERR_INVALIDPARAM;
1225 if (lplpDSCaptureBuffer == NULL) {
1226 WARN("invalid parameter: lplpDSCaptureBuffer == NULL\n");
1227 return DSERR_INVALIDPARAM;
1230 /* FIXME: We can only have one buffer so what do we do here? */
1231 if (This->device->capture_buffer) {
1232 WARN("invalid parameter: already has buffer\n");
1233 return DSERR_INVALIDPARAM; /* DSERR_GENERIC ? */
1236 hr = IDirectSoundCaptureBufferImpl_Create(This->device,
1237 (IDirectSoundCaptureBufferImpl **)lplpDSCaptureBuffer, lpcDSCBufferDesc);
1239 if (hr != DS_OK)
1240 WARN("IDirectSoundCaptureBufferImpl_Create failed\n");
1241 else
1242 This->device->capture_buffer->has_dsc8 = This->has_dsc8;
1244 return hr;
1247 static HRESULT WINAPI IDirectSoundCaptureImpl_GetCaps(IDirectSoundCapture *iface,
1248 LPDSCCAPS lpDSCCaps)
1250 IDirectSoundCaptureImpl *This = impl_from_IDirectSoundCapture(iface);
1252 TRACE("(%p,%p)\n",This,lpDSCCaps);
1254 if (This->device == NULL) {
1255 WARN("not initialized\n");
1256 return DSERR_UNINITIALIZED;
1259 if (lpDSCCaps== NULL) {
1260 WARN("invalid parameter: lpDSCCaps== NULL\n");
1261 return DSERR_INVALIDPARAM;
1264 if (lpDSCCaps->dwSize < sizeof(*lpDSCCaps)) {
1265 WARN("invalid parameter: lpDSCCaps->dwSize = %d\n", lpDSCCaps->dwSize);
1266 return DSERR_INVALIDPARAM;
1269 lpDSCCaps->dwFlags = This->device->drvcaps.dwFlags;
1270 lpDSCCaps->dwFormats = This->device->drvcaps.dwFormats;
1271 lpDSCCaps->dwChannels = This->device->drvcaps.dwChannels;
1273 TRACE("(flags=0x%08x,format=0x%08x,channels=%d)\n",lpDSCCaps->dwFlags,
1274 lpDSCCaps->dwFormats, lpDSCCaps->dwChannels);
1276 return DS_OK;
1279 static HRESULT WINAPI IDirectSoundCaptureImpl_Initialize(IDirectSoundCapture *iface,
1280 LPCGUID lpcGUID)
1282 IDirectSoundCaptureImpl *This = impl_from_IDirectSoundCapture(iface);
1284 TRACE("(%p,%s)\n", This, debugstr_guid(lpcGUID));
1286 if (This->device != NULL) {
1287 WARN("already initialized\n");
1288 return DSERR_ALREADYINITIALIZED;
1290 return DirectSoundCaptureDevice_Initialize(&This->device, lpcGUID);
1293 static const IDirectSoundCaptureVtbl dscvt =
1295 /* IUnknown methods */
1296 IDirectSoundCaptureImpl_QueryInterface,
1297 IDirectSoundCaptureImpl_AddRef,
1298 IDirectSoundCaptureImpl_Release,
1300 /* IDirectSoundCapture methods */
1301 IDirectSoundCaptureImpl_CreateCaptureBuffer,
1302 IDirectSoundCaptureImpl_GetCaps,
1303 IDirectSoundCaptureImpl_Initialize
1306 HRESULT IDirectSoundCaptureImpl_Create(IUnknown *outer_unk, REFIID riid, void **ppv, BOOL has_dsc8)
1308 IDirectSoundCaptureImpl *obj;
1309 HRESULT hr;
1311 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
1313 *ppv = NULL;
1314 obj = HeapAlloc(GetProcessHeap(), 0, sizeof(*obj));
1315 if (obj == NULL) {
1316 WARN("out of memory\n");
1317 return DSERR_OUTOFMEMORY;
1320 setup_dsound_options();
1322 obj->IUnknown_inner.lpVtbl = &unk_vtbl;
1323 obj->IDirectSoundCapture_iface.lpVtbl = &dscvt;
1324 obj->ref = 1;
1325 obj->refdsc = 0;
1326 obj->numIfaces = 1;
1327 obj->device = NULL;
1328 obj->has_dsc8 = has_dsc8;
1330 /* COM aggregation supported only internally */
1331 if (outer_unk)
1332 obj->outer_unk = outer_unk;
1333 else
1334 obj->outer_unk = &obj->IUnknown_inner;
1336 hr = IUnknown_QueryInterface(&obj->IUnknown_inner, riid, ppv);
1337 IUnknown_Release(&obj->IUnknown_inner);
1339 return hr;
1342 HRESULT DSOUND_CaptureCreate(REFIID riid, void **ppv)
1344 return IDirectSoundCaptureImpl_Create(NULL, riid, ppv, FALSE);
1347 HRESULT DSOUND_CaptureCreate8(REFIID riid, void **ppv)
1349 return IDirectSoundCaptureImpl_Create(NULL, riid, ppv, TRUE);
1352 /***************************************************************************
1353 * DirectSoundCaptureCreate [DSOUND.6]
1355 * Create and initialize a DirectSoundCapture interface.
1357 * PARAMS
1358 * lpcGUID [I] Address of the GUID that identifies the sound capture device.
1359 * lplpDSC [O] Address of a variable to receive the interface pointer.
1360 * pUnkOuter [I] Must be NULL.
1362 * RETURNS
1363 * Success: DS_OK
1364 * Failure: DSERR_NOAGGREGATION, DSERR_ALLOCATED, DSERR_INVALIDPARAM,
1365 * DSERR_OUTOFMEMORY
1367 * NOTES
1368 * lpcGUID must be one of the values returned from DirectSoundCaptureEnumerate
1369 * or NULL for the default device or DSDEVID_DefaultCapture or
1370 * DSDEVID_DefaultVoiceCapture.
1372 * DSERR_ALLOCATED is returned for sound devices that do not support full duplex.
1374 HRESULT WINAPI DirectSoundCaptureCreate(LPCGUID lpcGUID, IDirectSoundCapture **ppDSC,
1375 IUnknown *pUnkOuter)
1377 HRESULT hr;
1378 IDirectSoundCapture *pDSC;
1380 TRACE("(%s,%p,%p)\n", debugstr_guid(lpcGUID), ppDSC, pUnkOuter);
1382 if (ppDSC == NULL) {
1383 WARN("invalid parameter: ppDSC == NULL\n");
1384 return DSERR_INVALIDPARAM;
1387 if (pUnkOuter) {
1388 WARN("invalid parameter: pUnkOuter != NULL\n");
1389 return DSERR_NOAGGREGATION;
1392 hr = DSOUND_CaptureCreate(&IID_IDirectSoundCapture, (void**)&pDSC);
1393 if (hr == DS_OK) {
1394 hr = IDirectSoundCapture_Initialize(pDSC, lpcGUID);
1395 if (hr != DS_OK) {
1396 IDirectSoundCapture_Release(pDSC);
1397 pDSC = 0;
1401 *ppDSC = pDSC;
1403 return hr;
1406 /***************************************************************************
1407 * DirectSoundCaptureCreate8 [DSOUND.12]
1409 * Create and initialize a DirectSoundCapture interface.
1411 * PARAMS
1412 * lpcGUID [I] Address of the GUID that identifies the sound capture device.
1413 * lplpDSC [O] Address of a variable to receive the interface pointer.
1414 * pUnkOuter [I] Must be NULL.
1416 * RETURNS
1417 * Success: DS_OK
1418 * Failure: DSERR_NOAGGREGATION, DSERR_ALLOCATED, DSERR_INVALIDPARAM,
1419 * DSERR_OUTOFMEMORY
1421 * NOTES
1422 * lpcGUID must be one of the values returned from DirectSoundCaptureEnumerate
1423 * or NULL for the default device or DSDEVID_DefaultCapture or
1424 * DSDEVID_DefaultVoiceCapture.
1426 * DSERR_ALLOCATED is returned for sound devices that do not support full duplex.
1428 HRESULT WINAPI DirectSoundCaptureCreate8(
1429 LPCGUID lpcGUID,
1430 LPDIRECTSOUNDCAPTURE8 *ppDSC8,
1431 LPUNKNOWN pUnkOuter)
1433 HRESULT hr;
1434 LPDIRECTSOUNDCAPTURE8 pDSC8;
1435 TRACE("(%s,%p,%p)\n", debugstr_guid(lpcGUID), ppDSC8, pUnkOuter);
1437 if (ppDSC8 == NULL) {
1438 WARN("invalid parameter: ppDSC8 == NULL\n");
1439 return DSERR_INVALIDPARAM;
1442 if (pUnkOuter) {
1443 WARN("invalid parameter: pUnkOuter != NULL\n");
1444 *ppDSC8 = NULL;
1445 return DSERR_NOAGGREGATION;
1448 hr = DSOUND_CaptureCreate8(&IID_IDirectSoundCapture8, (void**)&pDSC8);
1449 if (hr == DS_OK) {
1450 hr = IDirectSoundCapture_Initialize(pDSC8, lpcGUID);
1451 if (hr != DS_OK) {
1452 IDirectSoundCapture_Release(pDSC8);
1453 pDSC8 = 0;
1457 *ppDSC8 = pDSC8;
1459 return hr;