msvcirt: Add implementation of streambuf::sputbackc.
[wine.git] / dlls / dsound / capture.c
blob0764261fc0be1ef1c2e0ba654bdfd0429833267e
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;
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 ( IsEqualGUID( &IID_IDirectSoundCaptureBuffer, riid ) ||
244 IsEqualGUID( &IID_IDirectSoundCaptureBuffer8, riid ) ) {
245 IDirectSoundCaptureBuffer8_AddRef(iface);
246 *ppobj = iface;
247 return S_OK;
250 if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ) {
251 IDirectSoundNotify_AddRef(&This->IDirectSoundNotify_iface);
252 *ppobj = &This->IDirectSoundNotify_iface;
253 return S_OK;
256 FIXME("(%p,%s,%p) unsupported GUID\n", This, debugstr_guid(riid), ppobj);
257 return E_NOINTERFACE;
260 static ULONG WINAPI IDirectSoundCaptureBufferImpl_AddRef(IDirectSoundCaptureBuffer8 *iface)
262 IDirectSoundCaptureBufferImpl *This = impl_from_IDirectSoundCaptureBuffer8(iface);
263 ULONG ref = InterlockedIncrement(&This->ref);
265 TRACE("(%p) ref was %d\n", This, ref - 1);
267 if(ref == 1)
268 InterlockedIncrement(&This->numIfaces);
270 return ref;
273 static ULONG WINAPI IDirectSoundCaptureBufferImpl_Release(IDirectSoundCaptureBuffer8 *iface)
275 IDirectSoundCaptureBufferImpl *This = impl_from_IDirectSoundCaptureBuffer8(iface);
276 ULONG ref = InterlockedDecrement(&This->ref);
278 TRACE("(%p) ref was %d\n", This, ref + 1);
280 if (!ref && !InterlockedDecrement(&This->numIfaces))
281 capturebuffer_destroy(This);
283 return ref;
286 static HRESULT WINAPI IDirectSoundCaptureBufferImpl_GetCaps(IDirectSoundCaptureBuffer8 *iface,
287 DSCBCAPS *lpDSCBCaps)
289 IDirectSoundCaptureBufferImpl *This = impl_from_IDirectSoundCaptureBuffer8(iface);
290 TRACE( "(%p,%p)\n", This, lpDSCBCaps );
292 if (lpDSCBCaps == NULL) {
293 WARN("invalid parameter: lpDSCBCaps == NULL\n");
294 return DSERR_INVALIDPARAM;
297 if (lpDSCBCaps->dwSize < sizeof(DSCBCAPS)) {
298 WARN("invalid parameter: lpDSCBCaps->dwSize = %d\n", lpDSCBCaps->dwSize);
299 return DSERR_INVALIDPARAM;
302 if (This->device == NULL) {
303 WARN("invalid parameter: This->device == NULL\n");
304 return DSERR_INVALIDPARAM;
307 lpDSCBCaps->dwSize = sizeof(DSCBCAPS);
308 lpDSCBCaps->dwFlags = This->flags;
309 lpDSCBCaps->dwBufferBytes = This->pdscbd->dwBufferBytes;
310 lpDSCBCaps->dwReserved = 0;
312 TRACE("returning DS_OK\n");
313 return DS_OK;
316 static HRESULT WINAPI IDirectSoundCaptureBufferImpl_GetCurrentPosition(IDirectSoundCaptureBuffer8 *iface,
317 DWORD *lpdwCapturePosition, DWORD *lpdwReadPosition)
319 IDirectSoundCaptureBufferImpl *This = impl_from_IDirectSoundCaptureBuffer8(iface);
321 TRACE( "(%p,%p,%p)\n", This, lpdwCapturePosition, lpdwReadPosition );
323 if (This->device == NULL) {
324 WARN("invalid parameter: This->device == NULL\n");
325 return DSERR_INVALIDPARAM;
328 EnterCriticalSection(&This->device->lock);
330 if (!This->device->client) {
331 LeaveCriticalSection(&This->device->lock);
332 WARN("no driver\n");
333 return DSERR_NODRIVER;
336 if(lpdwCapturePosition)
337 *lpdwCapturePosition = This->device->write_pos_bytes;
339 if(lpdwReadPosition)
340 *lpdwReadPosition = This->device->write_pos_bytes;
342 LeaveCriticalSection(&This->device->lock);
344 TRACE("cappos=%d readpos=%d\n", (lpdwCapturePosition?*lpdwCapturePosition:-1), (lpdwReadPosition?*lpdwReadPosition:-1));
345 TRACE("returning DS_OK\n");
347 return DS_OK;
350 static HRESULT WINAPI IDirectSoundCaptureBufferImpl_GetFormat(IDirectSoundCaptureBuffer8 *iface,
351 WAVEFORMATEX *lpwfxFormat, DWORD dwSizeAllocated, DWORD *lpdwSizeWritten)
353 IDirectSoundCaptureBufferImpl *This = impl_from_IDirectSoundCaptureBuffer8(iface);
354 HRESULT hres = DS_OK;
356 TRACE("(%p,%p,0x%08x,%p)\n", This, lpwfxFormat, dwSizeAllocated, lpdwSizeWritten);
358 if (This->device == NULL) {
359 WARN("invalid parameter: This->device == NULL\n");
360 return DSERR_INVALIDPARAM;
363 if (dwSizeAllocated > (sizeof(WAVEFORMATEX) + This->device->pwfx->cbSize))
364 dwSizeAllocated = sizeof(WAVEFORMATEX) + This->device->pwfx->cbSize;
366 if (lpwfxFormat) { /* NULL is valid (just want size) */
367 CopyMemory(lpwfxFormat, This->device->pwfx, dwSizeAllocated);
368 if (lpdwSizeWritten)
369 *lpdwSizeWritten = dwSizeAllocated;
370 } else {
371 if (lpdwSizeWritten)
372 *lpdwSizeWritten = sizeof(WAVEFORMATEX) + This->device->pwfx->cbSize;
373 else {
374 TRACE("invalid parameter: lpdwSizeWritten = NULL\n");
375 hres = DSERR_INVALIDPARAM;
379 TRACE("returning %08x\n", hres);
380 return hres;
383 static HRESULT WINAPI IDirectSoundCaptureBufferImpl_GetStatus(IDirectSoundCaptureBuffer8 *iface,
384 DWORD *lpdwStatus)
386 IDirectSoundCaptureBufferImpl *This = impl_from_IDirectSoundCaptureBuffer8(iface);
388 TRACE( "(%p, %p), thread is %04x\n", This, lpdwStatus, GetCurrentThreadId() );
390 if (This->device == NULL) {
391 WARN("invalid parameter: This->device == NULL\n");
392 return DSERR_INVALIDPARAM;
395 if (lpdwStatus == NULL) {
396 WARN("invalid parameter: lpdwStatus == NULL\n");
397 return DSERR_INVALIDPARAM;
400 *lpdwStatus = 0;
401 EnterCriticalSection(&(This->device->lock));
403 TRACE("old This->device->state=%s, old lpdwStatus=%08x\n",
404 captureStateString[This->device->state],*lpdwStatus);
405 if ((This->device->state == STATE_STARTING) ||
406 (This->device->state == STATE_CAPTURING)) {
407 *lpdwStatus |= DSCBSTATUS_CAPTURING;
408 if (This->flags & DSCBSTART_LOOPING)
409 *lpdwStatus |= DSCBSTATUS_LOOPING;
411 TRACE("new This->device->state=%s, new lpdwStatus=%08x\n",
412 captureStateString[This->device->state],*lpdwStatus);
413 LeaveCriticalSection(&(This->device->lock));
415 TRACE("status=%x\n", *lpdwStatus);
416 TRACE("returning DS_OK\n");
417 return DS_OK;
420 static HRESULT WINAPI IDirectSoundCaptureBufferImpl_Initialize(IDirectSoundCaptureBuffer8 *iface,
421 IDirectSoundCapture *lpDSC, const DSCBUFFERDESC *lpcDSCBDesc)
423 IDirectSoundCaptureBufferImpl *This = impl_from_IDirectSoundCaptureBuffer8(iface);
425 FIXME( "(%p,%p,%p): stub\n", This, lpDSC, lpcDSCBDesc );
427 return DS_OK;
430 static HRESULT WINAPI IDirectSoundCaptureBufferImpl_Lock(IDirectSoundCaptureBuffer8 *iface,
431 DWORD dwReadCusor, DWORD dwReadBytes, void **lplpvAudioPtr1, DWORD *lpdwAudioBytes1,
432 void **lplpvAudioPtr2, DWORD *lpdwAudioBytes2, DWORD dwFlags)
434 IDirectSoundCaptureBufferImpl *This = impl_from_IDirectSoundCaptureBuffer8(iface);
435 HRESULT hres = DS_OK;
437 TRACE( "(%p,%08u,%08u,%p,%p,%p,%p,0x%08x) at %d\n", This, dwReadCusor,
438 dwReadBytes, lplpvAudioPtr1, lpdwAudioBytes1, lplpvAudioPtr2,
439 lpdwAudioBytes2, dwFlags, GetTickCount() );
441 if (This->device == NULL) {
442 WARN("invalid parameter: This->device == NULL\n");
443 return DSERR_INVALIDPARAM;
446 if (lplpvAudioPtr1 == NULL) {
447 WARN("invalid parameter: lplpvAudioPtr1 == NULL\n");
448 return DSERR_INVALIDPARAM;
451 if (lpdwAudioBytes1 == NULL) {
452 WARN("invalid parameter: lpdwAudioBytes1 == NULL\n");
453 return DSERR_INVALIDPARAM;
456 EnterCriticalSection(&(This->device->lock));
458 if (This->device->client) {
459 *lplpvAudioPtr1 = This->device->buffer + dwReadCusor;
460 if ( (dwReadCusor + dwReadBytes) > This->device->buflen) {
461 *lpdwAudioBytes1 = This->device->buflen - dwReadCusor;
462 if (lplpvAudioPtr2)
463 *lplpvAudioPtr2 = This->device->buffer;
464 if (lpdwAudioBytes2)
465 *lpdwAudioBytes2 = dwReadBytes - *lpdwAudioBytes1;
466 } else {
467 *lpdwAudioBytes1 = dwReadBytes;
468 if (lplpvAudioPtr2)
469 *lplpvAudioPtr2 = 0;
470 if (lpdwAudioBytes2)
471 *lpdwAudioBytes2 = 0;
473 } else {
474 TRACE("invalid call\n");
475 hres = DSERR_INVALIDCALL; /* DSERR_NODRIVER ? */
478 LeaveCriticalSection(&(This->device->lock));
480 TRACE("returning %08x\n", hres);
481 return hres;
484 static HRESULT WINAPI IDirectSoundCaptureBufferImpl_Start(IDirectSoundCaptureBuffer8 *iface,
485 DWORD dwFlags)
487 IDirectSoundCaptureBufferImpl *This = impl_from_IDirectSoundCaptureBuffer8(iface);
488 HRESULT hres;
490 TRACE( "(%p,0x%08x)\n", This, dwFlags );
492 if (This->device == NULL) {
493 WARN("invalid parameter: This->device == NULL\n");
494 return DSERR_INVALIDPARAM;
497 if ( !This->device->client ) {
498 WARN("no driver\n");
499 return DSERR_NODRIVER;
502 EnterCriticalSection(&(This->device->lock));
504 if (This->device->state == STATE_STOPPED)
505 This->device->state = STATE_STARTING;
506 else if (This->device->state == STATE_STOPPING)
507 This->device->state = STATE_CAPTURING;
508 else
509 goto out;
510 TRACE("new This->device->state=%s\n",captureStateString[This->device->state]);
511 This->flags = dwFlags;
513 if (This->device->buffer)
514 FillMemory(This->device->buffer, This->device->buflen, (This->device->pwfx->wBitsPerSample == 8) ? 128 : 0);
516 hres = IAudioClient_Start(This->device->client);
517 if(FAILED(hres)){
518 WARN("Start failed: %08x\n", hres);
519 LeaveCriticalSection(&This->device->lock);
520 return hres;
523 out:
524 LeaveCriticalSection(&This->device->lock);
526 TRACE("returning DS_OK\n");
527 return DS_OK;
530 static HRESULT WINAPI IDirectSoundCaptureBufferImpl_Stop(IDirectSoundCaptureBuffer8 *iface)
532 IDirectSoundCaptureBufferImpl *This = impl_from_IDirectSoundCaptureBuffer8(iface);
533 HRESULT hres;
535 TRACE("(%p)\n", This);
537 if (This->device == NULL) {
538 WARN("invalid parameter: This->device == NULL\n");
539 return DSERR_INVALIDPARAM;
542 EnterCriticalSection(&(This->device->lock));
544 TRACE("old This->device->state=%s\n",captureStateString[This->device->state]);
545 if (This->device->state == STATE_CAPTURING)
546 This->device->state = STATE_STOPPING;
547 else if (This->device->state == STATE_STARTING)
548 This->device->state = STATE_STOPPED;
549 TRACE("new This->device->state=%s\n",captureStateString[This->device->state]);
551 if(This->device->client){
552 hres = IAudioClient_Stop(This->device->client);
553 if(FAILED(hres)){
554 LeaveCriticalSection(&This->device->lock);
555 return hres;
559 LeaveCriticalSection(&(This->device->lock));
561 TRACE("returning DS_OK\n");
562 return DS_OK;
565 static HRESULT WINAPI IDirectSoundCaptureBufferImpl_Unlock(IDirectSoundCaptureBuffer8 *iface,
566 void *lpvAudioPtr1, DWORD dwAudioBytes1, void *lpvAudioPtr2, DWORD dwAudioBytes2)
568 IDirectSoundCaptureBufferImpl *This = impl_from_IDirectSoundCaptureBuffer8(iface);
569 HRESULT hres = DS_OK;
571 TRACE( "(%p,%p,%08u,%p,%08u)\n", This, lpvAudioPtr1, dwAudioBytes1,
572 lpvAudioPtr2, dwAudioBytes2 );
574 if (lpvAudioPtr1 == NULL) {
575 WARN("invalid parameter: lpvAudioPtr1 == NULL\n");
576 return DSERR_INVALIDPARAM;
579 if (!This->device->client) {
580 WARN("invalid call\n");
581 hres = DSERR_INVALIDCALL;
584 TRACE("returning %08x\n", hres);
585 return hres;
588 static HRESULT WINAPI IDirectSoundCaptureBufferImpl_GetObjectInPath(IDirectSoundCaptureBuffer8 *iface,
589 REFGUID rguidObject, DWORD dwIndex, REFGUID rguidInterface, void **ppObject)
591 IDirectSoundCaptureBufferImpl *This = impl_from_IDirectSoundCaptureBuffer8(iface);
593 FIXME( "(%p,%s,%u,%s,%p): stub\n", This, debugstr_guid(rguidObject),
594 dwIndex, debugstr_guid(rguidInterface), ppObject );
596 if (!ppObject)
597 return DSERR_INVALIDPARAM;
599 *ppObject = NULL;
600 return DSERR_CONTROLUNAVAIL;
603 static HRESULT WINAPI IDirectSoundCaptureBufferImpl_GetFXStatus(IDirectSoundCaptureBuffer8 *iface,
604 DWORD dwFXCount, DWORD *pdwFXStatus)
606 IDirectSoundCaptureBufferImpl *This = impl_from_IDirectSoundCaptureBuffer8(iface);
608 FIXME( "(%p,%u,%p): stub\n", This, dwFXCount, pdwFXStatus );
610 return DS_OK;
613 static const IDirectSoundCaptureBuffer8Vtbl dscbvt =
615 /* IUnknown methods */
616 IDirectSoundCaptureBufferImpl_QueryInterface,
617 IDirectSoundCaptureBufferImpl_AddRef,
618 IDirectSoundCaptureBufferImpl_Release,
620 /* IDirectSoundCaptureBuffer methods */
621 IDirectSoundCaptureBufferImpl_GetCaps,
622 IDirectSoundCaptureBufferImpl_GetCurrentPosition,
623 IDirectSoundCaptureBufferImpl_GetFormat,
624 IDirectSoundCaptureBufferImpl_GetStatus,
625 IDirectSoundCaptureBufferImpl_Initialize,
626 IDirectSoundCaptureBufferImpl_Lock,
627 IDirectSoundCaptureBufferImpl_Start,
628 IDirectSoundCaptureBufferImpl_Stop,
629 IDirectSoundCaptureBufferImpl_Unlock,
631 /* IDirectSoundCaptureBuffer methods */
632 IDirectSoundCaptureBufferImpl_GetObjectInPath,
633 IDirectSoundCaptureBufferImpl_GetFXStatus
636 static void capture_CheckNotify(IDirectSoundCaptureBufferImpl *This, DWORD from, DWORD len)
638 int i;
639 for (i = 0; i < This->nrofnotifies; ++i) {
640 LPDSBPOSITIONNOTIFY event = This->notifies + i;
641 DWORD offset = event->dwOffset;
642 TRACE("checking %d, position %d, event = %p\n", i, offset, event->hEventNotify);
644 if (offset == DSBPN_OFFSETSTOP) {
645 if (!from && !len) {
646 SetEvent(event->hEventNotify);
647 TRACE("signalled event %p (%d)\n", event->hEventNotify, i);
648 return;
650 else return;
653 if (offset >= from && offset < (from + len))
655 TRACE("signalled event %p (%d)\n", event->hEventNotify, i);
656 SetEvent(event->hEventNotify);
661 static HRESULT IDirectSoundCaptureBufferImpl_Create(
662 DirectSoundCaptureDevice *device,
663 IDirectSoundCaptureBufferImpl ** ppobj,
664 LPCDSCBUFFERDESC lpcDSCBufferDesc)
666 LPWAVEFORMATEX wfex;
667 IDirectSoundCaptureBufferImpl *This;
668 TRACE( "(%p,%p,%p)\n", device, ppobj, lpcDSCBufferDesc);
670 if (ppobj == NULL) {
671 WARN("invalid parameter: ppobj == NULL\n");
672 return DSERR_INVALIDPARAM;
675 *ppobj = NULL;
677 if (!device) {
678 WARN("not initialized\n");
679 return DSERR_UNINITIALIZED;
682 if (lpcDSCBufferDesc == NULL) {
683 WARN("invalid parameter: lpcDSCBufferDesc == NULL\n");
684 return DSERR_INVALIDPARAM;
687 if ( ((lpcDSCBufferDesc->dwSize != sizeof(DSCBUFFERDESC)) &&
688 (lpcDSCBufferDesc->dwSize != sizeof(DSCBUFFERDESC1))) ||
689 (lpcDSCBufferDesc->dwBufferBytes == 0) ||
690 (lpcDSCBufferDesc->lpwfxFormat == NULL) ) { /* FIXME: DSERR_BADFORMAT ? */
691 WARN("invalid lpcDSCBufferDesc\n");
692 return DSERR_INVALIDPARAM;
695 wfex = lpcDSCBufferDesc->lpwfxFormat;
697 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
698 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
699 wfex->wFormatTag, wfex->nChannels, wfex->nSamplesPerSec,
700 wfex->nAvgBytesPerSec, wfex->nBlockAlign,
701 wfex->wBitsPerSample, wfex->cbSize);
703 device->pwfx = DSOUND_CopyFormat(wfex);
704 if ( device->pwfx == NULL )
705 return DSERR_OUTOFMEMORY;
707 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,
708 sizeof(IDirectSoundCaptureBufferImpl));
710 if ( This == NULL ) {
711 WARN("out of memory\n");
712 return DSERR_OUTOFMEMORY;
713 } else {
714 HRESULT err = DS_OK;
715 LPBYTE newbuf;
716 DWORD buflen;
718 This->numIfaces = 0;
719 This->ref = 0;
720 This->refn = 0;
721 This->device = device;
722 This->device->capture_buffer = This;
723 This->nrofnotifies = 0;
725 This->pdscbd = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,
726 lpcDSCBufferDesc->dwSize);
727 if (This->pdscbd)
728 CopyMemory(This->pdscbd, lpcDSCBufferDesc, lpcDSCBufferDesc->dwSize);
729 else {
730 WARN("no memory\n");
731 This->device->capture_buffer = 0;
732 HeapFree( GetProcessHeap(), 0, This );
733 return DSERR_OUTOFMEMORY;
736 This->IDirectSoundCaptureBuffer8_iface.lpVtbl = &dscbvt;
737 This->IDirectSoundNotify_iface.lpVtbl = &dscnvt;
739 err = IMMDevice_Activate(device->mmdevice, &IID_IAudioClient,
740 CLSCTX_INPROC_SERVER, NULL, (void**)&device->client);
741 if(FAILED(err)){
742 WARN("Activate failed: %08x\n", err);
743 HeapFree(GetProcessHeap(), 0, This->pdscbd);
744 This->device->capture_buffer = 0;
745 HeapFree( GetProcessHeap(), 0, This );
746 return err;
749 err = IAudioClient_Initialize(device->client,
750 AUDCLNT_SHAREMODE_SHARED, AUDCLNT_STREAMFLAGS_NOPERSIST | AUDCLNT_STREAMFLAGS_EVENTCALLBACK,
751 200 * 100000, 0, device->pwfx, NULL);
752 if(FAILED(err)){
753 WARN("Initialize failed: %08x\n", err);
754 IAudioClient_Release(device->client);
755 device->client = NULL;
756 HeapFree(GetProcessHeap(), 0, This->pdscbd);
757 This->device->capture_buffer = 0;
758 HeapFree( GetProcessHeap(), 0, This );
759 if(err == AUDCLNT_E_UNSUPPORTED_FORMAT)
760 return DSERR_BADFORMAT;
761 return err;
764 This->sleepev = CreateEventW(NULL, 0, 0, NULL);
766 err = IAudioClient_SetEventHandle(device->client, This->sleepev);
767 if(FAILED(err)){
768 WARN("SetEventHandle failed: %08x\n", err);
769 IAudioClient_Release(device->client);
770 device->client = NULL;
771 CloseHandle(This->sleepev);
772 HeapFree(GetProcessHeap(), 0, This->pdscbd);
773 This->device->capture_buffer = 0;
774 HeapFree( GetProcessHeap(), 0, This );
775 return err;
778 err = IAudioClient_GetService(device->client, &IID_IAudioCaptureClient,
779 (void**)&device->capture);
780 if(FAILED(err)){
781 WARN("GetService failed: %08x\n", err);
782 IAudioClient_Release(device->client);
783 device->client = NULL;
784 CloseHandle(This->sleepev);
785 HeapFree(GetProcessHeap(), 0, This->pdscbd);
786 This->device->capture_buffer = 0;
787 HeapFree( GetProcessHeap(), 0, This );
788 return err;
791 buflen = lpcDSCBufferDesc->dwBufferBytes;
792 TRACE("desired buflen=%d, old buffer=%p\n", buflen, device->buffer);
793 if (device->buffer)
794 newbuf = HeapReAlloc(GetProcessHeap(),0,device->buffer,buflen);
795 else
796 newbuf = HeapAlloc(GetProcessHeap(),0,buflen);
797 if (newbuf == NULL) {
798 IAudioClient_Release(device->client);
799 device->client = NULL;
800 IAudioCaptureClient_Release(device->capture);
801 device->capture = NULL;
802 CloseHandle(This->sleepev);
803 HeapFree(GetProcessHeap(), 0, This->pdscbd);
804 This->device->capture_buffer = 0;
805 HeapFree( GetProcessHeap(), 0, This );
806 return DSERR_OUTOFMEMORY;
808 device->buffer = newbuf;
809 device->buflen = buflen;
810 This->thread = CreateThread(NULL, 0, DSOUND_capture_thread, This, 0, NULL);
813 IDirectSoundCaptureBuffer_AddRef(&This->IDirectSoundCaptureBuffer8_iface);
814 *ppobj = This;
816 TRACE("returning DS_OK\n");
817 return DS_OK;
821 /*******************************************************************************
822 * DirectSoundCaptureDevice
824 static HRESULT DirectSoundCaptureDevice_Create(
825 DirectSoundCaptureDevice ** ppDevice)
827 DirectSoundCaptureDevice * device;
828 TRACE("(%p)\n", ppDevice);
830 /* Allocate memory */
831 device = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DirectSoundCaptureDevice));
833 if (device == NULL) {
834 WARN("out of memory\n");
835 return DSERR_OUTOFMEMORY;
838 device->ref = 1;
839 device->state = STATE_STOPPED;
841 InitializeCriticalSection( &(device->lock) );
842 device->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": DirectSoundCaptureDevice.lock");
844 *ppDevice = device;
846 return DS_OK;
849 static ULONG DirectSoundCaptureDevice_Release(
850 DirectSoundCaptureDevice * device)
852 ULONG ref = InterlockedDecrement(&(device->ref));
853 TRACE("(%p) ref was %d\n", device, ref + 1);
855 if (!ref) {
856 TRACE("deleting object\n");
858 EnterCriticalSection(&DSOUND_capturers_lock);
859 list_remove(&device->entry);
860 LeaveCriticalSection(&DSOUND_capturers_lock);
862 if (device->capture_buffer)
863 IDirectSoundCaptureBufferImpl_Release(&device->capture_buffer->IDirectSoundCaptureBuffer8_iface);
865 if(device->mmdevice)
866 IMMDevice_Release(device->mmdevice);
867 HeapFree(GetProcessHeap(), 0, device->pwfx);
868 device->lock.DebugInfo->Spare[0] = 0;
869 DeleteCriticalSection( &(device->lock) );
870 HeapFree(GetProcessHeap(), 0, device);
871 TRACE("(%p) released\n", device);
873 return ref;
876 static HRESULT DSOUND_capture_data(DirectSoundCaptureDevice *device)
878 HRESULT hr;
879 UINT32 packet_frames, packet_bytes, avail_bytes, skip_bytes = 0;
880 DWORD flags;
881 BYTE *buf;
883 if(!device->capture_buffer || device->state == STATE_STOPPED)
884 return S_FALSE;
886 if(device->state == STATE_STOPPING){
887 device->state = STATE_STOPPED;
888 return S_FALSE;
891 if(device->state == STATE_STARTING)
892 device->state = STATE_CAPTURING;
894 hr = IAudioCaptureClient_GetBuffer(device->capture, &buf, &packet_frames,
895 &flags, NULL, NULL);
896 if(FAILED(hr)){
897 WARN("GetBuffer failed: %08x\n", hr);
898 return hr;
901 packet_bytes = packet_frames * device->pwfx->nBlockAlign;
902 if(packet_bytes > device->buflen){
903 TRACE("audio glitch: dsound buffer too small for data\n");
904 skip_bytes = packet_bytes - device->buflen;
905 packet_bytes = device->buflen;
908 avail_bytes = device->buflen - device->write_pos_bytes;
909 if(avail_bytes > packet_bytes)
910 avail_bytes = packet_bytes;
912 memcpy(device->buffer + device->write_pos_bytes, buf + skip_bytes, avail_bytes);
913 capture_CheckNotify(device->capture_buffer, device->write_pos_bytes, avail_bytes);
915 packet_bytes -= avail_bytes;
916 if(packet_bytes > 0){
917 if(device->capture_buffer->flags & DSCBSTART_LOOPING){
918 memcpy(device->buffer, buf + skip_bytes + avail_bytes, packet_bytes);
919 capture_CheckNotify(device->capture_buffer, 0, packet_bytes);
920 }else{
921 device->state = STATE_STOPPED;
922 capture_CheckNotify(device->capture_buffer, 0, 0);
926 device->write_pos_bytes += avail_bytes + packet_bytes;
927 device->write_pos_bytes %= device->buflen;
929 hr = IAudioCaptureClient_ReleaseBuffer(device->capture, packet_frames);
930 if(FAILED(hr)){
931 WARN("ReleaseBuffer failed: %08x\n", hr);
932 return hr;
935 return S_OK;
938 static DWORD WINAPI DSOUND_capture_thread(void *user)
940 IDirectSoundCaptureBufferImpl *buffer = user;
941 HRESULT hr;
942 DWORD ret, wait_ms;
943 REFERENCE_TIME period;
945 hr = IAudioClient_GetDevicePeriod(buffer->device->client, &period, NULL);
946 if(FAILED(hr)){
947 WARN("GetDevicePeriod failed: %08x\n", hr);
948 wait_ms = 5;
949 }else
950 wait_ms = MulDiv(5, period, 10000);
952 while(buffer->ref){
953 ret = WaitForSingleObject(buffer->sleepev, wait_ms);
955 if(!buffer->device->ref)
956 break;
958 if(ret == WAIT_OBJECT_0){
959 EnterCriticalSection(&buffer->device->lock);
961 DSOUND_capture_data(buffer->device);
963 LeaveCriticalSection(&buffer->device->lock);
964 }else if(ret != WAIT_TIMEOUT)
965 WARN("WaitForSingleObject failed: %u\n", GetLastError());
968 return 0;
971 static struct _TestFormat {
972 DWORD flag;
973 DWORD rate;
974 DWORD depth;
975 WORD channels;
976 } formats_to_test[] = {
977 { WAVE_FORMAT_1M08, 11025, 8, 1 },
978 { WAVE_FORMAT_1M16, 11025, 16, 1 },
979 { WAVE_FORMAT_1S08, 11025, 8, 2 },
980 { WAVE_FORMAT_1S16, 11025, 16, 2 },
981 { WAVE_FORMAT_2M08, 22050, 8, 1 },
982 { WAVE_FORMAT_2M16, 22050, 16, 1 },
983 { WAVE_FORMAT_2S08, 22050, 8, 2 },
984 { WAVE_FORMAT_2S16, 22050, 16, 2 },
985 { WAVE_FORMAT_4M08, 44100, 8, 1 },
986 { WAVE_FORMAT_4M16, 44100, 16, 1 },
987 { WAVE_FORMAT_4S08, 44100, 8, 2 },
988 { WAVE_FORMAT_4S16, 44100, 16, 2 },
989 { WAVE_FORMAT_48M08, 48000, 8, 1 },
990 { WAVE_FORMAT_48M16, 48000, 16, 1 },
991 { WAVE_FORMAT_48S08, 48000, 8, 2 },
992 { WAVE_FORMAT_48S16, 48000, 16, 2 },
993 { WAVE_FORMAT_96M08, 96000, 8, 1 },
994 { WAVE_FORMAT_96M16, 96000, 16, 1 },
995 { WAVE_FORMAT_96S08, 96000, 8, 2 },
996 { WAVE_FORMAT_96S16, 96000, 16, 2 },
1000 static HRESULT DirectSoundCaptureDevice_Initialize(
1001 DirectSoundCaptureDevice ** ppDevice,
1002 LPCGUID lpcGUID)
1004 HRESULT hr;
1005 GUID devGUID;
1006 IMMDevice *mmdevice;
1007 struct _TestFormat *fmt;
1008 DirectSoundCaptureDevice *device;
1009 IAudioClient *client;
1011 TRACE("(%p, %s)\n", ppDevice, debugstr_guid(lpcGUID));
1013 /* Default device? */
1014 if ( !lpcGUID || IsEqualGUID(lpcGUID, &GUID_NULL) )
1015 lpcGUID = &DSDEVID_DefaultCapture;
1017 if(IsEqualGUID(lpcGUID, &DSDEVID_DefaultPlayback) ||
1018 IsEqualGUID(lpcGUID, &DSDEVID_DefaultVoicePlayback))
1019 return DSERR_NODRIVER;
1021 if (GetDeviceID(lpcGUID, &devGUID) != DS_OK) {
1022 WARN("invalid parameter: lpcGUID\n");
1023 return DSERR_INVALIDPARAM;
1026 hr = get_mmdevice(eCapture, &devGUID, &mmdevice);
1027 if(FAILED(hr))
1028 return hr;
1030 EnterCriticalSection(&DSOUND_capturers_lock);
1032 hr = DirectSoundCaptureDevice_Create(&device);
1033 if (hr != DS_OK) {
1034 WARN("DirectSoundCaptureDevice_Create failed\n");
1035 LeaveCriticalSection(&DSOUND_capturers_lock);
1036 return hr;
1039 device->guid = devGUID;
1041 device->mmdevice = mmdevice;
1043 device->drvcaps.dwFlags = 0;
1045 device->drvcaps.dwFormats = 0;
1046 device->drvcaps.dwChannels = 0;
1047 hr = IMMDevice_Activate(mmdevice, &IID_IAudioClient,
1048 CLSCTX_INPROC_SERVER, NULL, (void**)&client);
1049 if(FAILED(hr)){
1050 device->lock.DebugInfo->Spare[0] = 0;
1051 DeleteCriticalSection(&device->lock);
1052 HeapFree(GetProcessHeap(), 0, device);
1053 LeaveCriticalSection(&DSOUND_capturers_lock);
1054 return DSERR_NODRIVER;
1057 for(fmt = formats_to_test; fmt->flag; ++fmt){
1058 if(DSOUND_check_supported(client, fmt->rate, fmt->depth, fmt->channels)){
1059 device->drvcaps.dwFormats |= fmt->flag;
1060 if(fmt->channels > device->drvcaps.dwChannels)
1061 device->drvcaps.dwChannels = fmt->channels;
1064 IAudioClient_Release(client);
1066 list_add_tail(&DSOUND_capturers, &device->entry);
1068 *ppDevice = device;
1070 LeaveCriticalSection(&DSOUND_capturers_lock);
1072 return S_OK;
1076 /*****************************************************************************
1077 * IDirectSoundCapture implementation structure
1079 typedef struct IDirectSoundCaptureImpl
1081 IUnknown IUnknown_inner;
1082 IDirectSoundCapture IDirectSoundCapture_iface;
1083 LONG ref, refdsc, numIfaces;
1084 IUnknown *outer_unk; /* internal */
1085 DirectSoundCaptureDevice *device;
1086 BOOL has_dsc8;
1087 } IDirectSoundCaptureImpl;
1089 static void capture_destroy(IDirectSoundCaptureImpl *This)
1091 if (This->device)
1092 DirectSoundCaptureDevice_Release(This->device);
1093 HeapFree(GetProcessHeap(),0,This);
1094 TRACE("(%p) released\n", This);
1097 /*******************************************************************************
1098 * IUnknown Implementation for DirectSoundCapture
1100 static inline IDirectSoundCaptureImpl *impl_from_IUnknown(IUnknown *iface)
1102 return CONTAINING_RECORD(iface, IDirectSoundCaptureImpl, IUnknown_inner);
1105 static HRESULT WINAPI IUnknownImpl_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
1107 IDirectSoundCaptureImpl *This = impl_from_IUnknown(iface);
1109 TRACE("(%p,%s,%p)\n", This, debugstr_guid(riid), ppv);
1111 if (!ppv) {
1112 WARN("invalid parameter\n");
1113 return E_INVALIDARG;
1115 *ppv = NULL;
1117 if (IsEqualIID(riid, &IID_IUnknown))
1118 *ppv = &This->IUnknown_inner;
1119 else if (IsEqualIID(riid, &IID_IDirectSoundCapture))
1120 *ppv = &This->IDirectSoundCapture_iface;
1121 else {
1122 WARN("unknown IID %s\n", debugstr_guid(riid));
1123 return E_NOINTERFACE;
1126 IUnknown_AddRef((IUnknown*)*ppv);
1127 return S_OK;
1130 static ULONG WINAPI IUnknownImpl_AddRef(IUnknown *iface)
1132 IDirectSoundCaptureImpl *This = impl_from_IUnknown(iface);
1133 ULONG ref = InterlockedIncrement(&This->ref);
1135 TRACE("(%p) ref=%d\n", This, ref);
1137 if(ref == 1)
1138 InterlockedIncrement(&This->numIfaces);
1139 return ref;
1142 static ULONG WINAPI IUnknownImpl_Release(IUnknown *iface)
1144 IDirectSoundCaptureImpl *This = impl_from_IUnknown(iface);
1145 ULONG ref = InterlockedDecrement(&This->ref);
1147 TRACE("(%p) ref=%d\n", This, ref);
1149 if (!ref && !InterlockedDecrement(&This->numIfaces))
1150 capture_destroy(This);
1151 return ref;
1154 static const IUnknownVtbl unk_vtbl =
1156 IUnknownImpl_QueryInterface,
1157 IUnknownImpl_AddRef,
1158 IUnknownImpl_Release
1161 /***************************************************************************
1162 * IDirectSoundCaptureImpl
1164 static inline struct IDirectSoundCaptureImpl *impl_from_IDirectSoundCapture(IDirectSoundCapture *iface)
1166 return CONTAINING_RECORD(iface, struct IDirectSoundCaptureImpl, IDirectSoundCapture_iface);
1169 static HRESULT WINAPI IDirectSoundCaptureImpl_QueryInterface(IDirectSoundCapture *iface,
1170 REFIID riid, void **ppv)
1172 IDirectSoundCaptureImpl *This = impl_from_IDirectSoundCapture(iface);
1173 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(riid), ppv);
1174 return IUnknown_QueryInterface(This->outer_unk, riid, ppv);
1177 static ULONG WINAPI IDirectSoundCaptureImpl_AddRef(IDirectSoundCapture *iface)
1179 IDirectSoundCaptureImpl *This = impl_from_IDirectSoundCapture(iface);
1180 ULONG ref = InterlockedIncrement(&This->refdsc);
1182 TRACE("(%p) ref=%d\n", This, ref);
1184 if(ref == 1)
1185 InterlockedIncrement(&This->numIfaces);
1186 return ref;
1189 static ULONG WINAPI IDirectSoundCaptureImpl_Release(IDirectSoundCapture *iface)
1191 IDirectSoundCaptureImpl *This = impl_from_IDirectSoundCapture(iface);
1192 ULONG ref = InterlockedDecrement(&This->refdsc);
1194 TRACE("(%p) ref=%d\n", This, ref);
1196 if (!ref && !InterlockedDecrement(&This->numIfaces))
1197 capture_destroy(This);
1198 return ref;
1201 static HRESULT WINAPI IDirectSoundCaptureImpl_CreateCaptureBuffer(IDirectSoundCapture *iface,
1202 LPCDSCBUFFERDESC lpcDSCBufferDesc, IDirectSoundCaptureBuffer **lplpDSCaptureBuffer,
1203 IUnknown *pUnk)
1205 IDirectSoundCaptureImpl *This = impl_from_IDirectSoundCapture(iface);
1206 HRESULT hr;
1208 TRACE( "(%p,%p,%p,%p)\n",iface,lpcDSCBufferDesc,lplpDSCaptureBuffer,pUnk);
1210 if (pUnk) {
1211 WARN("invalid parameter: pUnk != NULL\n");
1212 return DSERR_NOAGGREGATION;
1215 if (lpcDSCBufferDesc == NULL) {
1216 WARN("invalid parameter: lpcDSCBufferDesc == NULL)\n");
1217 return DSERR_INVALIDPARAM;
1220 if (lplpDSCaptureBuffer == NULL) {
1221 WARN("invalid parameter: lplpDSCaptureBuffer == NULL\n");
1222 return DSERR_INVALIDPARAM;
1225 /* FIXME: We can only have one buffer so what do we do here? */
1226 if (This->device->capture_buffer) {
1227 WARN("invalid parameter: already has buffer\n");
1228 return DSERR_INVALIDPARAM; /* DSERR_GENERIC ? */
1231 hr = IDirectSoundCaptureBufferImpl_Create(This->device,
1232 (IDirectSoundCaptureBufferImpl **)lplpDSCaptureBuffer, lpcDSCBufferDesc);
1234 if (hr != DS_OK)
1235 WARN("IDirectSoundCaptureBufferImpl_Create failed\n");
1237 return hr;
1240 static HRESULT WINAPI IDirectSoundCaptureImpl_GetCaps(IDirectSoundCapture *iface,
1241 LPDSCCAPS lpDSCCaps)
1243 IDirectSoundCaptureImpl *This = impl_from_IDirectSoundCapture(iface);
1245 TRACE("(%p,%p)\n",This,lpDSCCaps);
1247 if (This->device == NULL) {
1248 WARN("not initialized\n");
1249 return DSERR_UNINITIALIZED;
1252 if (lpDSCCaps== NULL) {
1253 WARN("invalid parameter: lpDSCCaps== NULL\n");
1254 return DSERR_INVALIDPARAM;
1257 if (lpDSCCaps->dwSize < sizeof(*lpDSCCaps)) {
1258 WARN("invalid parameter: lpDSCCaps->dwSize = %d\n", lpDSCCaps->dwSize);
1259 return DSERR_INVALIDPARAM;
1262 lpDSCCaps->dwFlags = This->device->drvcaps.dwFlags;
1263 lpDSCCaps->dwFormats = This->device->drvcaps.dwFormats;
1264 lpDSCCaps->dwChannels = This->device->drvcaps.dwChannels;
1266 TRACE("(flags=0x%08x,format=0x%08x,channels=%d)\n",lpDSCCaps->dwFlags,
1267 lpDSCCaps->dwFormats, lpDSCCaps->dwChannels);
1269 return DS_OK;
1272 static HRESULT WINAPI IDirectSoundCaptureImpl_Initialize(IDirectSoundCapture *iface,
1273 LPCGUID lpcGUID)
1275 IDirectSoundCaptureImpl *This = impl_from_IDirectSoundCapture(iface);
1277 TRACE("(%p,%s)\n", This, debugstr_guid(lpcGUID));
1279 if (This->device != NULL) {
1280 WARN("already initialized\n");
1281 return DSERR_ALREADYINITIALIZED;
1283 return DirectSoundCaptureDevice_Initialize(&This->device, lpcGUID);
1286 static const IDirectSoundCaptureVtbl dscvt =
1288 /* IUnknown methods */
1289 IDirectSoundCaptureImpl_QueryInterface,
1290 IDirectSoundCaptureImpl_AddRef,
1291 IDirectSoundCaptureImpl_Release,
1293 /* IDirectSoundCapture methods */
1294 IDirectSoundCaptureImpl_CreateCaptureBuffer,
1295 IDirectSoundCaptureImpl_GetCaps,
1296 IDirectSoundCaptureImpl_Initialize
1299 HRESULT IDirectSoundCaptureImpl_Create(IUnknown *outer_unk, REFIID riid, void **ppv, BOOL has_dsc8)
1301 IDirectSoundCaptureImpl *obj;
1302 HRESULT hr;
1304 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
1306 *ppv = NULL;
1307 obj = HeapAlloc(GetProcessHeap(), 0, sizeof(*obj));
1308 if (obj == NULL) {
1309 WARN("out of memory\n");
1310 return DSERR_OUTOFMEMORY;
1313 setup_dsound_options();
1315 obj->IUnknown_inner.lpVtbl = &unk_vtbl;
1316 obj->IDirectSoundCapture_iface.lpVtbl = &dscvt;
1317 obj->ref = 1;
1318 obj->refdsc = 0;
1319 obj->numIfaces = 1;
1320 obj->device = NULL;
1321 obj->has_dsc8 = has_dsc8;
1323 /* COM aggregation supported only internally */
1324 if (outer_unk)
1325 obj->outer_unk = outer_unk;
1326 else
1327 obj->outer_unk = &obj->IUnknown_inner;
1329 hr = IUnknown_QueryInterface(&obj->IUnknown_inner, riid, ppv);
1330 IUnknown_Release(&obj->IUnknown_inner);
1332 return hr;
1335 HRESULT DSOUND_CaptureCreate(REFIID riid, void **ppv)
1337 return IDirectSoundCaptureImpl_Create(NULL, riid, ppv, FALSE);
1340 HRESULT DSOUND_CaptureCreate8(REFIID riid, void **ppv)
1342 return IDirectSoundCaptureImpl_Create(NULL, riid, ppv, TRUE);
1345 /***************************************************************************
1346 * DirectSoundCaptureCreate [DSOUND.6]
1348 * Create and initialize a DirectSoundCapture interface.
1350 * PARAMS
1351 * lpcGUID [I] Address of the GUID that identifies the sound capture device.
1352 * lplpDSC [O] Address of a variable to receive the interface pointer.
1353 * pUnkOuter [I] Must be NULL.
1355 * RETURNS
1356 * Success: DS_OK
1357 * Failure: DSERR_NOAGGREGATION, DSERR_ALLOCATED, DSERR_INVALIDPARAM,
1358 * DSERR_OUTOFMEMORY
1360 * NOTES
1361 * lpcGUID must be one of the values returned from DirectSoundCaptureEnumerate
1362 * or NULL for the default device or DSDEVID_DefaultCapture or
1363 * DSDEVID_DefaultVoiceCapture.
1365 * DSERR_ALLOCATED is returned for sound devices that do not support full duplex.
1367 HRESULT WINAPI DirectSoundCaptureCreate(LPCGUID lpcGUID, IDirectSoundCapture **ppDSC,
1368 IUnknown *pUnkOuter)
1370 HRESULT hr;
1371 IDirectSoundCapture *pDSC;
1373 TRACE("(%s,%p,%p)\n", debugstr_guid(lpcGUID), ppDSC, pUnkOuter);
1375 if (ppDSC == NULL) {
1376 WARN("invalid parameter: ppDSC == NULL\n");
1377 return DSERR_INVALIDPARAM;
1380 if (pUnkOuter) {
1381 WARN("invalid parameter: pUnkOuter != NULL\n");
1382 return DSERR_NOAGGREGATION;
1385 hr = DSOUND_CaptureCreate(&IID_IDirectSoundCapture, (void**)&pDSC);
1386 if (hr == DS_OK) {
1387 hr = IDirectSoundCapture_Initialize(pDSC, lpcGUID);
1388 if (hr != DS_OK) {
1389 IDirectSoundCapture_Release(pDSC);
1390 pDSC = 0;
1394 *ppDSC = pDSC;
1396 return hr;
1399 /***************************************************************************
1400 * DirectSoundCaptureCreate8 [DSOUND.12]
1402 * Create and initialize a DirectSoundCapture interface.
1404 * PARAMS
1405 * lpcGUID [I] Address of the GUID that identifies the sound capture device.
1406 * lplpDSC [O] Address of a variable to receive the interface pointer.
1407 * pUnkOuter [I] Must be NULL.
1409 * RETURNS
1410 * Success: DS_OK
1411 * Failure: DSERR_NOAGGREGATION, DSERR_ALLOCATED, DSERR_INVALIDPARAM,
1412 * DSERR_OUTOFMEMORY
1414 * NOTES
1415 * lpcGUID must be one of the values returned from DirectSoundCaptureEnumerate
1416 * or NULL for the default device or DSDEVID_DefaultCapture or
1417 * DSDEVID_DefaultVoiceCapture.
1419 * DSERR_ALLOCATED is returned for sound devices that do not support full duplex.
1421 HRESULT WINAPI DirectSoundCaptureCreate8(
1422 LPCGUID lpcGUID,
1423 LPDIRECTSOUNDCAPTURE8 *ppDSC8,
1424 LPUNKNOWN pUnkOuter)
1426 HRESULT hr;
1427 LPDIRECTSOUNDCAPTURE8 pDSC8;
1428 TRACE("(%s,%p,%p)\n", debugstr_guid(lpcGUID), ppDSC8, pUnkOuter);
1430 if (ppDSC8 == NULL) {
1431 WARN("invalid parameter: ppDSC8 == NULL\n");
1432 return DSERR_INVALIDPARAM;
1435 if (pUnkOuter) {
1436 WARN("invalid parameter: pUnkOuter != NULL\n");
1437 *ppDSC8 = NULL;
1438 return DSERR_NOAGGREGATION;
1441 hr = DSOUND_CaptureCreate8(&IID_IDirectSoundCapture8, (void**)&pDSC8);
1442 if (hr == DS_OK) {
1443 hr = IDirectSoundCapture_Initialize(pDSC8, lpcGUID);
1444 if (hr != DS_OK) {
1445 IDirectSoundCapture_Release(pDSC8);
1446 pDSC8 = 0;
1450 *ppDSC8 = pDSC8;
1452 return hr;