ntdll/tests: Don't test function directly when reporting GetLastError().
[wine/multimedia.git] / dlls / winealsa.drv / dscapture.c
blob70e70ede995b449c606ef74064cec78ac3e26093
1 /*
2 * Sample Wine Driver for Advanced Linux Sound System (ALSA)
3 * Based on version <final> of the ALSA API
5 * Copyright 2007 - Maarten Lankhorst
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 /*======================================================================*
23 * Low level dsound input implementation *
24 *======================================================================*/
26 #include "config.h"
27 #include "wine/port.h"
29 #include <stdlib.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <string.h>
33 #ifdef HAVE_UNISTD_H
34 # include <unistd.h>
35 #endif
36 #include <errno.h>
37 #include <limits.h>
38 #include <fcntl.h>
39 #ifdef HAVE_SYS_IOCTL_H
40 # include <sys/ioctl.h>
41 #endif
42 #ifdef HAVE_SYS_MMAN_H
43 # include <sys/mman.h>
44 #endif
45 #include "windef.h"
46 #include "winbase.h"
47 #include "wingdi.h"
48 #include "winerror.h"
49 #include "winuser.h"
50 #include "mmddk.h"
51 #include "mmreg.h"
52 #include "dsound.h"
53 #include "dsdriver.h"
55 #include "alsa.h"
56 #include "wine/library.h"
57 #include "wine/unicode.h"
58 #include "wine/debug.h"
60 /* Notify timer checks every 10 ms with a resolution of 2 ms */
61 #define DS_TIME_DEL 10
62 #define DS_TIME_RES 2
64 WINE_DEFAULT_DEBUG_CHANNEL(dsalsa);
66 typedef struct IDsCaptureDriverBufferImpl IDsCaptureDriverBufferImpl;
68 typedef struct IDsCaptureDriverImpl
70 const IDsCaptureDriverVtbl *lpVtbl;
71 LONG ref;
72 IDsCaptureDriverBufferImpl* capture_buffer;
73 UINT wDevID;
74 } IDsCaptureDriverImpl;
76 typedef struct IDsCaptureDriverNotifyImpl
78 const IDsDriverNotifyVtbl *lpVtbl;
79 LONG ref;
80 IDsCaptureDriverBufferImpl *buffer;
81 DSBPOSITIONNOTIFY *notifies;
82 DWORD nrofnotifies, playpos;
83 UINT timerID;
84 } IDsCaptureDriverNotifyImpl;
86 struct IDsCaptureDriverBufferImpl
88 const IDsCaptureDriverBufferVtbl *lpVtbl;
89 LONG ref;
90 IDsCaptureDriverImpl *drv;
91 IDsCaptureDriverNotifyImpl *notify;
93 CRITICAL_SECTION pcm_crst;
94 LPBYTE mmap_buffer, presented_buffer;
95 DWORD mmap_buflen_bytes, play_looping, mmap_ofs_bytes;
96 BOOL mmap;
98 /* Note: snd_pcm_frames_to_bytes(This->pcm, mmap_buflen_frames) != mmap_buflen_bytes */
99 /* The actual buffer may differ in size from the wanted buffer size */
101 snd_pcm_t *pcm;
102 snd_pcm_hw_params_t *hw_params;
103 snd_pcm_sw_params_t *sw_params;
104 snd_pcm_uframes_t mmap_buflen_frames, mmap_pos;
107 static void Capture_CheckNotify(IDsCaptureDriverNotifyImpl *This, DWORD from, DWORD len)
109 unsigned i;
110 for (i = 0; i < This->nrofnotifies; ++i) {
111 LPDSBPOSITIONNOTIFY event = This->notifies + i;
112 DWORD offset = event->dwOffset;
113 TRACE("checking %d, position %d, event = %p\n", i, offset, event->hEventNotify);
115 if (offset == DSBPN_OFFSETSTOP) {
116 if (!from && !len) {
117 SetEvent(event->hEventNotify);
118 TRACE("signalled event %p (%d)\n", event->hEventNotify, i);
119 return;
121 else return;
124 if (offset >= from && offset < (from + len))
126 TRACE("signalled event %p (%d)\n", event->hEventNotify, i);
127 SetEvent(event->hEventNotify);
132 static void CALLBACK Capture_Notify(UINT timerID, UINT msg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
134 IDsCaptureDriverBufferImpl *This = (IDsCaptureDriverBufferImpl *)dwUser;
135 DWORD last_playpos, playpos;
136 PIDSCDRIVERBUFFER iface = (PIDSCDRIVERBUFFER)This;
138 /* **** */
139 /* Don't deadlock since there is a critical section can be held by the timer api itself while running this code */
140 if (!TryEnterCriticalSection(&This->pcm_crst)) return;
142 IDsDriverBuffer_GetPosition(iface, &playpos, NULL);
143 last_playpos = This->notify->playpos;
144 This->notify->playpos = playpos;
146 if (snd_pcm_state(This->pcm) != SND_PCM_STATE_RUNNING || last_playpos == playpos || !This->notify->nrofnotifies || !This->notify->notifies)
147 goto done;
149 if (playpos < last_playpos)
151 Capture_CheckNotify(This->notify, last_playpos, This->mmap_buflen_bytes);
152 if (playpos)
153 Capture_CheckNotify(This->notify, 0, playpos);
155 else Capture_CheckNotify(This->notify, last_playpos, playpos - last_playpos);
157 done:
158 LeaveCriticalSection(&This->pcm_crst);
159 /* **** */
162 static HRESULT WINAPI IDsCaptureDriverNotifyImpl_QueryInterface(PIDSDRIVERNOTIFY iface, REFIID riid, LPVOID *ppobj)
164 IDsCaptureDriverNotifyImpl *This = (IDsCaptureDriverNotifyImpl *)iface;
165 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
167 if ( IsEqualGUID(riid, &IID_IUnknown) ||
168 IsEqualGUID(riid, &IID_IDsDriverNotify) ) {
169 IDsDriverNotify_AddRef(iface);
170 *ppobj = This;
171 return DS_OK;
174 FIXME( "Unknown IID %s\n", debugstr_guid(riid));
176 *ppobj = 0;
177 return E_NOINTERFACE;
180 static ULONG WINAPI IDsCaptureDriverNotifyImpl_AddRef(PIDSDRIVERNOTIFY iface)
182 IDsCaptureDriverNotifyImpl *This = (IDsCaptureDriverNotifyImpl *)iface;
183 ULONG refCount = InterlockedIncrement(&This->ref);
185 TRACE("(%p) ref was %d\n", This, refCount - 1);
187 return refCount;
190 static ULONG WINAPI IDsCaptureDriverNotifyImpl_Release(PIDSDRIVERNOTIFY iface)
192 IDsCaptureDriverNotifyImpl *This = (IDsCaptureDriverNotifyImpl *)iface;
193 ULONG refCount = InterlockedDecrement(&This->ref);
195 TRACE("(%p) ref was %d\n", This, refCount + 1);
197 if (!refCount) {
198 This->buffer->notify = NULL;
199 if (This->timerID)
201 timeKillEvent(This->timerID);
202 timeEndPeriod(DS_TIME_RES);
204 HeapFree(GetProcessHeap(), 0, This->notifies);
205 HeapFree(GetProcessHeap(), 0, This);
206 TRACE("(%p) released\n", This);
208 return refCount;
211 static HRESULT WINAPI IDsCaptureDriverNotifyImpl_SetNotificationPositions(PIDSDRIVERNOTIFY iface, DWORD howmuch, LPCDSBPOSITIONNOTIFY notify)
213 DWORD len = howmuch * sizeof(DSBPOSITIONNOTIFY);
214 unsigned i;
215 LPVOID notifies;
216 IDsCaptureDriverNotifyImpl *This = (IDsCaptureDriverNotifyImpl *)iface;
217 TRACE("(%p,0x%08x,%p)\n",This,howmuch,notify);
219 if (!notify) {
220 WARN("invalid parameter\n");
221 return DSERR_INVALIDPARAM;
224 if (TRACE_ON(dsalsa))
225 for (i=0;i<howmuch; ++i)
226 TRACE("notify at %d to %p\n", notify[i].dwOffset, notify[i].hEventNotify);
228 /* **** */
229 EnterCriticalSection(&This->buffer->pcm_crst);
231 /* Make an internal copy of the caller-supplied array.
232 * Replace the existing copy if one is already present. */
233 if (This->notifies)
234 notifies = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->notifies, len);
235 else
236 notifies = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
238 if (!notifies)
240 LeaveCriticalSection(&This->buffer->pcm_crst);
241 /* **** */
242 return DSERR_OUTOFMEMORY;
244 This->notifies = notifies;
245 memcpy(This->notifies, notify, len);
246 This->nrofnotifies = howmuch;
247 IDsDriverBuffer_GetPosition((PIDSCDRIVERBUFFER)This->buffer, &This->playpos, NULL);
249 if (!This->timerID)
251 timeBeginPeriod(DS_TIME_RES);
252 This->timerID = timeSetEvent(DS_TIME_DEL, DS_TIME_RES, Capture_Notify, (DWORD_PTR)This->buffer, TIME_PERIODIC | TIME_KILL_SYNCHRONOUS);
255 LeaveCriticalSection(&This->buffer->pcm_crst);
256 /* **** */
258 return S_OK;
261 static const IDsDriverNotifyVtbl dscdnvt =
263 IDsCaptureDriverNotifyImpl_QueryInterface,
264 IDsCaptureDriverNotifyImpl_AddRef,
265 IDsCaptureDriverNotifyImpl_Release,
266 IDsCaptureDriverNotifyImpl_SetNotificationPositions,
269 #if 0
270 /** Convert the position an application sees into a position ALSA sees */
271 static snd_pcm_uframes_t fakepos_to_realpos(const IDsCaptureDriverBufferImpl* This, DWORD fakepos)
273 snd_pcm_uframes_t realpos;
274 if (fakepos < This->mmap_ofs_bytes)
275 realpos = This->mmap_buflen_bytes + fakepos - This->mmap_ofs_bytes;
276 else realpos = fakepos - This->mmap_ofs_bytes;
277 return snd_pcm_bytes_to_frames(This->pcm, realpos) % This->mmap_buflen_frames;
279 #endif
281 /** Convert the position ALSA sees into a position an application sees */
282 static DWORD realpos_to_fakepos(const IDsCaptureDriverBufferImpl* This, snd_pcm_uframes_t realpos)
284 DWORD realposb = snd_pcm_frames_to_bytes(This->pcm, realpos);
285 return (realposb + This->mmap_ofs_bytes) % This->mmap_buflen_bytes;
288 /** Raw copy data, with buffer wrap around */
289 static void CopyDataWrap(const IDsCaptureDriverBufferImpl* This, LPBYTE dest, DWORD fromwhere, DWORD copylen, DWORD buflen)
291 DWORD remainder = buflen - fromwhere;
292 if (remainder >= copylen)
294 CopyMemory(dest, This->mmap_buffer + fromwhere, copylen);
296 else
298 CopyMemory(dest, This->mmap_buffer + fromwhere, remainder);
299 copylen -= remainder;
300 CopyMemory(dest, This->mmap_buffer, copylen);
304 /** Copy data from the mmap buffer to backbuffer, taking into account all wraparounds that may occur */
305 static void CopyData(const IDsCaptureDriverBufferImpl* This, snd_pcm_uframes_t fromwhere, snd_pcm_uframes_t len)
307 DWORD dlen = snd_pcm_frames_to_bytes(This->pcm, len) % This->mmap_buflen_bytes;
309 /* Backbuffer */
310 DWORD ofs = realpos_to_fakepos(This, fromwhere);
311 DWORD remainder = This->mmap_buflen_bytes - ofs;
313 /* MMAP buffer */
314 DWORD realbuflen = snd_pcm_frames_to_bytes(This->pcm, This->mmap_buflen_frames);
315 DWORD realofs = snd_pcm_frames_to_bytes(This->pcm, fromwhere);
317 if (remainder >= dlen)
319 CopyDataWrap(This, This->presented_buffer + ofs, realofs, dlen, realbuflen);
321 else
323 CopyDataWrap(This, This->presented_buffer + ofs, realofs, remainder, realbuflen);
324 dlen -= remainder;
325 CopyDataWrap(This, This->presented_buffer, (realofs+remainder)%realbuflen, dlen, realbuflen);
329 /** Fill buffers, for starting and stopping
330 * Alsa won't start playing until everything is filled up
331 * This also updates mmap_pos
333 * Returns: Amount of periods in use so snd_pcm_avail_update
334 * doesn't have to be called up to 4x in GetPosition()
336 static snd_pcm_uframes_t CommitAll(IDsCaptureDriverBufferImpl *This, DWORD forced)
338 const snd_pcm_channel_area_t *areas;
339 snd_pcm_uframes_t used;
340 const snd_pcm_uframes_t commitahead = This->mmap_buflen_frames;
342 used = This->mmap_buflen_frames - snd_pcm_avail_update(This->pcm);
343 TRACE("%p needs to commit to %lu, used: %lu\n", This, commitahead, used);
344 if (used < commitahead && (forced || This->play_looping))
346 snd_pcm_uframes_t done, putin = commitahead - used;
347 if (This->mmap)
349 snd_pcm_mmap_begin(This->pcm, &areas, &This->mmap_pos, &putin);
350 CopyData(This, This->mmap_pos, putin);
351 done = snd_pcm_mmap_commit(This->pcm, This->mmap_pos, putin);
353 This->mmap_pos += done;
354 used += done;
355 putin = commitahead - used;
357 if (This->mmap_pos == This->mmap_buflen_frames && (snd_pcm_sframes_t)putin > 0 && This->play_looping)
359 This->mmap_ofs_bytes += snd_pcm_frames_to_bytes(This->pcm, This->mmap_buflen_frames);
360 This->mmap_ofs_bytes %= This->mmap_buflen_bytes;
362 snd_pcm_mmap_begin(This->pcm, &areas, &This->mmap_pos, &putin);
363 CopyData(This, This->mmap_pos, putin);
364 done = snd_pcm_mmap_commit(This->pcm, This->mmap_pos, putin);
366 This->mmap_pos += done;
367 used += done;
370 else
372 DWORD pos;
373 snd_pcm_sframes_t ret;
375 snd_pcm_uframes_t cap = snd_pcm_bytes_to_frames(This->pcm, This->mmap_buflen_bytes);
376 pos = realpos_to_fakepos(This, This->mmap_pos);
377 if (This->mmap_pos + putin > cap)
378 putin = cap - This->mmap_pos;
379 ret = snd_pcm_readi(This->pcm, This->presented_buffer + pos, putin);
380 if (ret == -EPIPE)
382 WARN("Underrun occurred\n");
383 snd_pcm_prepare(This->pcm);
384 ret = snd_pcm_readi(This->pcm, This->presented_buffer + pos, putin);
385 snd_pcm_start(This->pcm);
387 if (ret < 0)
389 WARN("Committing data: %ld / %s (%ld)\n", ret, snd_strerror(ret), putin);
390 ret = 0;
392 This->mmap_pos += ret;
393 used += ret;
394 /* At this point mmap_pos may be >= This->mmap_pos this is harmless
395 * realpos_to_fakepos handles it well, and below it is truncated
398 putin = commitahead - used;
399 if (putin > 0)
401 pos = realpos_to_fakepos(This, This->mmap_pos);
402 ret = snd_pcm_readi(This->pcm, This->presented_buffer + pos, putin);
403 if (ret > 0)
405 This->mmap_pos += ret;
406 used += ret;
413 if (This->mmap_pos >= This->mmap_buflen_frames)
415 This->mmap_ofs_bytes += snd_pcm_frames_to_bytes(This->pcm, This->mmap_buflen_frames);
416 This->mmap_ofs_bytes %= This->mmap_buflen_bytes;
417 This->mmap_pos -= This->mmap_buflen_frames;
420 return used;
423 static void CheckXRUN(IDsCaptureDriverBufferImpl* This)
425 snd_pcm_state_t state = snd_pcm_state(This->pcm);
426 int err;
428 if ( state == SND_PCM_STATE_XRUN )
430 err = snd_pcm_prepare(This->pcm);
431 CommitAll(This, FALSE);
432 snd_pcm_start(This->pcm);
433 WARN("xrun occurred\n");
434 if ( err < 0 )
435 ERR("recovery from xrun failed, prepare failed: %s\n", snd_strerror(err));
437 else if ( state == SND_PCM_STATE_SUSPENDED )
439 int err = snd_pcm_resume(This->pcm);
440 TRACE("recovery from suspension occurred\n");
441 if (err < 0 && err != -EAGAIN){
442 err = snd_pcm_prepare(This->pcm);
443 if (err < 0)
444 ERR("recovery from suspend failed, prepare failed: %s\n", snd_strerror(err));
447 else if ( state != SND_PCM_STATE_RUNNING)
449 WARN("Unhandled state: %d\n", state);
454 * Allocate the memory-mapped buffer for direct sound, and set up the
455 * callback.
457 static int CreateMMAP(IDsCaptureDriverBufferImpl* pdbi)
459 snd_pcm_t *pcm = pdbi->pcm;
460 snd_pcm_format_t format;
461 snd_pcm_uframes_t frames, ofs, avail, psize, boundary;
462 unsigned int channels, bits_per_sample, bits_per_frame;
463 int err, mmap_mode;
464 const snd_pcm_channel_area_t *areas;
465 snd_pcm_hw_params_t *hw_params = pdbi->hw_params;
466 snd_pcm_sw_params_t *sw_params = pdbi->sw_params;
468 mmap_mode = snd_pcm_type(pcm);
470 if (mmap_mode == SND_PCM_TYPE_HW)
471 TRACE("mmap'd buffer is a direct hardware buffer.\n");
472 else if (mmap_mode == SND_PCM_TYPE_DMIX)
473 TRACE("mmap'd buffer is an ALSA dmix buffer\n");
474 else
475 TRACE("mmap'd buffer is an ALSA type %d buffer\n", mmap_mode);
477 err = snd_pcm_hw_params_get_period_size(hw_params, &psize, NULL);
478 err = snd_pcm_hw_params_get_format(hw_params, &format);
479 err = snd_pcm_hw_params_get_buffer_size(hw_params, &frames);
480 err = snd_pcm_hw_params_get_channels(hw_params, &channels);
481 bits_per_sample = snd_pcm_format_physical_width(format);
482 bits_per_frame = bits_per_sample * channels;
484 if (TRACE_ON(dsalsa))
485 ALSA_TraceParameters(hw_params, NULL, FALSE);
487 TRACE("format=%s frames=%ld channels=%d bits_per_sample=%d bits_per_frame=%d\n",
488 snd_pcm_format_name(format), frames, channels, bits_per_sample, bits_per_frame);
490 pdbi->mmap_buflen_frames = frames;
491 snd_pcm_sw_params_current(pcm, sw_params);
492 snd_pcm_sw_params_set_start_threshold(pcm, sw_params, 0);
493 snd_pcm_sw_params_get_boundary(sw_params, &boundary);
494 snd_pcm_sw_params_set_stop_threshold(pcm, sw_params, boundary);
495 snd_pcm_sw_params_set_silence_threshold(pcm, sw_params, INT_MAX);
496 snd_pcm_sw_params_set_silence_size(pcm, sw_params, 0);
497 snd_pcm_sw_params_set_avail_min(pcm, sw_params, 0);
498 err = snd_pcm_sw_params(pcm, sw_params);
500 pdbi->mmap_ofs_bytes = 0;
501 if (!pdbi->mmap)
503 pdbi->mmap_buffer = NULL;
505 frames = snd_pcm_bytes_to_frames(pdbi->pcm, pdbi->mmap_buflen_bytes);
506 snd_pcm_format_set_silence(format, pdbi->presented_buffer, frames);
507 pdbi->mmap_pos = 0;
509 else
511 err = snd_pcm_mmap_begin(pcm, &areas, &ofs, &avail);
512 if ( err < 0 )
514 ERR("Can't map sound device for direct access: %s/%d\n", snd_strerror(err), err);
515 return DSERR_GENERIC;
517 snd_pcm_format_set_silence(format, areas->addr, pdbi->mmap_buflen_frames);
518 pdbi->mmap_pos = ofs + snd_pcm_mmap_commit(pcm, ofs, 0);
519 pdbi->mmap_buffer = areas->addr;
522 TRACE("created mmap buffer of %ld frames (%d bytes) at %p\n",
523 pdbi->mmap_buflen_frames, pdbi->mmap_buflen_bytes, pdbi->mmap_buffer);
525 return DS_OK;
528 static HRESULT WINAPI IDsCaptureDriverBufferImpl_QueryInterface(PIDSCDRIVERBUFFER iface, REFIID riid, LPVOID *ppobj)
530 IDsCaptureDriverBufferImpl *This = (IDsCaptureDriverBufferImpl *)iface;
531 if ( IsEqualGUID(riid, &IID_IUnknown) ||
532 IsEqualGUID(riid, &IID_IDsCaptureDriverBuffer) ) {
533 IDsCaptureDriverBuffer_AddRef(iface);
534 *ppobj = iface;
535 return DS_OK;
538 if ( IsEqualGUID( &IID_IDsDriverNotify, riid ) ) {
539 if (!This->notify)
541 This->notify = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDsCaptureDriverNotifyImpl));
542 if (!This->notify)
543 return DSERR_OUTOFMEMORY;
544 This->notify->lpVtbl = &dscdnvt;
545 This->notify->buffer = This;
547 /* Keep a lock on IDsDriverNotify for ourself, so it is destroyed when the buffer is */
548 IDsDriverNotify_AddRef((PIDSDRIVERNOTIFY)This->notify);
550 IDsDriverNotify_AddRef((PIDSDRIVERNOTIFY)This->notify);
551 *ppobj = This->notify;
552 return DS_OK;
555 if ( IsEqualGUID( &IID_IDsDriverPropertySet, riid ) ) {
556 FIXME("Unsupported interface IID_IDsDriverPropertySet\n");
557 return E_FAIL;
560 FIXME("(): Unknown interface %s\n", debugstr_guid(riid));
561 return DSERR_UNSUPPORTED;
564 static ULONG WINAPI IDsCaptureDriverBufferImpl_AddRef(PIDSCDRIVERBUFFER iface)
566 IDsCaptureDriverBufferImpl *This = (IDsCaptureDriverBufferImpl *)iface;
567 ULONG refCount = InterlockedIncrement(&This->ref);
569 TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
571 return refCount;
574 static ULONG WINAPI IDsCaptureDriverBufferImpl_Release(PIDSCDRIVERBUFFER iface)
576 IDsCaptureDriverBufferImpl *This = (IDsCaptureDriverBufferImpl *)iface;
577 ULONG refCount = InterlockedDecrement(&This->ref);
579 TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
581 if (refCount)
582 return refCount;
584 EnterCriticalSection(&This->pcm_crst);
585 if (This->notify)
586 IDsDriverNotify_Release((PIDSDRIVERNOTIFY)This->notify);
587 TRACE("mmap buffer %p destroyed\n", This->mmap_buffer);
589 This->drv->capture_buffer = NULL;
590 LeaveCriticalSection(&This->pcm_crst);
591 This->pcm_crst.DebugInfo->Spare[0] = 0;
592 DeleteCriticalSection(&This->pcm_crst);
594 snd_pcm_drop(This->pcm);
595 snd_pcm_close(This->pcm);
596 This->pcm = NULL;
597 HeapFree(GetProcessHeap(), 0, This->presented_buffer);
598 HeapFree(GetProcessHeap(), 0, This->sw_params);
599 HeapFree(GetProcessHeap(), 0, This->hw_params);
600 HeapFree(GetProcessHeap(), 0, This);
601 return 0;
604 static HRESULT WINAPI IDsCaptureDriverBufferImpl_Lock(PIDSCDRIVERBUFFER iface, LPVOID*ppvAudio1,LPDWORD pdwLen1,LPVOID*ppvAudio2,LPDWORD pdwLen2, DWORD dwWritePosition,DWORD dwWriteLen, DWORD dwFlags)
606 IDsCaptureDriverBufferImpl *This = (IDsCaptureDriverBufferImpl *)iface;
607 TRACE("(%p,%p,%p,%p,%p,%d,%d,0x%08x)\n", This, ppvAudio1, pdwLen1, ppvAudio2, pdwLen2, dwWritePosition, dwWriteLen, dwFlags);
609 if (ppvAudio1)
610 *ppvAudio1 = (LPBYTE)This->presented_buffer + dwWritePosition;
612 if (dwWritePosition + dwWriteLen <= This->mmap_buflen_bytes) {
613 if (pdwLen1)
614 *pdwLen1 = dwWriteLen;
615 if (ppvAudio2)
616 *ppvAudio2 = 0;
617 if (pdwLen2)
618 *pdwLen2 = 0;
619 } else {
620 if (pdwLen1)
621 *pdwLen1 = This->mmap_buflen_bytes - dwWritePosition;
622 if (ppvAudio2)
623 *ppvAudio2 = This->presented_buffer;
624 if (pdwLen2)
625 *pdwLen2 = dwWriteLen - (This->mmap_buflen_bytes - dwWritePosition);
627 return DS_OK;
630 static HRESULT WINAPI IDsCaptureDriverBufferImpl_Unlock(PIDSCDRIVERBUFFER iface, LPVOID pvAudio1,DWORD dwLen1, LPVOID pvAudio2,DWORD dwLen2)
632 IDsCaptureDriverBufferImpl *This = (IDsCaptureDriverBufferImpl *)iface;
633 TRACE("(%p,%p,%d,%p,%d)\n",This,pvAudio1,dwLen1,pvAudio2,dwLen2);
634 return DS_OK;
637 static HRESULT WINAPI IDsCaptureDriverBufferImpl_SetFormat(PIDSCDRIVERBUFFER iface, LPWAVEFORMATEX pwfx)
639 IDsCaptureDriverBufferImpl *This = (IDsCaptureDriverBufferImpl *)iface;
640 WINE_WAVEDEV *wwi = &WInDev[This->drv->wDevID];
641 snd_pcm_t *pcm = NULL;
642 snd_pcm_hw_params_t *hw_params = This->hw_params;
643 snd_pcm_format_t format = -1;
644 snd_pcm_uframes_t buffer_size;
645 DWORD rate = pwfx->nSamplesPerSec;
646 int err=0;
647 BOOL mmap;
649 TRACE("(%p, %p)\n", iface, pwfx);
651 switch (pwfx->wBitsPerSample)
653 case 8: format = SND_PCM_FORMAT_U8; break;
654 case 16: format = SND_PCM_FORMAT_S16_LE; break;
655 case 24: format = SND_PCM_FORMAT_S24_3LE; break;
656 case 32: format = SND_PCM_FORMAT_S32_LE; break;
657 default: FIXME("Unsupported bpp: %d\n", pwfx->wBitsPerSample); return DSERR_GENERIC;
660 /* **** */
661 EnterCriticalSection(&This->pcm_crst);
663 err = snd_pcm_open(&pcm, wwi->pcmname, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK);
665 if (err < 0)
667 if (errno != EBUSY || !This->pcm)
669 /* **** */
670 LeaveCriticalSection(&This->pcm_crst);
671 WARN("Cannot open sound device: %s\n", snd_strerror(err));
672 return DSERR_GENERIC;
674 snd_pcm_drop(This->pcm);
675 snd_pcm_close(This->pcm);
676 This->pcm = NULL;
677 err = snd_pcm_open(&pcm, wwi->pcmname, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK);
678 if (err < 0)
680 /* **** */
681 LeaveCriticalSection(&This->pcm_crst);
682 WARN("Cannot open sound device: %s\n", snd_strerror(err));
683 return DSERR_BUFFERLOST;
687 /* Set some defaults */
688 snd_pcm_hw_params_any(pcm, hw_params);
690 err = snd_pcm_hw_params_set_channels(pcm, hw_params, pwfx->nChannels);
691 if (err < 0) { WARN("Could not set channels to %d\n", pwfx->nChannels); goto err; }
693 err = snd_pcm_hw_params_set_format(pcm, hw_params, format);
694 if (err < 0) { WARN("Could not set format to %d bpp\n", pwfx->wBitsPerSample); goto err; }
696 err = snd_pcm_hw_params_set_rate_near(pcm, hw_params, &rate, NULL);
697 if (err < 0) { rate = pwfx->nSamplesPerSec; WARN("Could not set rate\n"); goto err; }
699 if (!ALSA_NearMatch(rate, pwfx->nSamplesPerSec))
701 WARN("Could not set sound rate to %d, but instead to %d\n", pwfx->nSamplesPerSec, rate);
702 pwfx->nSamplesPerSec = rate;
703 pwfx->nAvgBytesPerSec = rate * pwfx->nBlockAlign;
704 /* Let DirectSound detect this */
707 snd_pcm_hw_params_set_periods_integer(pcm, hw_params);
708 buffer_size = This->mmap_buflen_bytes / pwfx->nBlockAlign;
709 snd_pcm_hw_params_set_buffer_size_near(pcm, hw_params, &buffer_size);
710 buffer_size = 5000;
711 snd_pcm_hw_params_set_period_time_near(pcm, hw_params, (unsigned int*)&buffer_size, NULL);
713 err = snd_pcm_hw_params_set_access (pcm, hw_params, SND_PCM_ACCESS_MMAP_INTERLEAVED);
714 if (err < 0)
716 err = snd_pcm_hw_params_set_access (pcm, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
717 if (err < 0) { WARN("Could not set access\n"); goto err; }
718 mmap = 0;
720 else
721 mmap = 1;
723 err = snd_pcm_hw_params(pcm, hw_params);
724 if (err < 0) { WARN("Could not set hw parameters\n"); goto err; }
726 if (This->pcm)
728 snd_pcm_drop(This->pcm);
729 snd_pcm_close(This->pcm);
731 This->pcm = pcm;
732 This->mmap = mmap;
734 snd_pcm_prepare(This->pcm);
735 CreateMMAP(This);
737 /* **** */
738 LeaveCriticalSection(&This->pcm_crst);
739 return S_OK;
741 err:
742 if (err < 0)
743 WARN("Failed to apply changes: %s\n", snd_strerror(err));
745 if (!This->pcm)
746 This->pcm = pcm;
747 else
748 snd_pcm_close(pcm);
750 if (This->pcm)
751 snd_pcm_hw_params_current(This->pcm, This->hw_params);
753 /* **** */
754 LeaveCriticalSection(&This->pcm_crst);
755 return DSERR_BADFORMAT;
758 static HRESULT WINAPI IDsCaptureDriverBufferImpl_GetPosition(PIDSCDRIVERBUFFER iface, LPDWORD lpdwCappos, LPDWORD lpdwReadpos)
760 IDsCaptureDriverBufferImpl *This = (IDsCaptureDriverBufferImpl *)iface;
761 snd_pcm_uframes_t hw_pptr, hw_wptr;
763 EnterCriticalSection(&This->pcm_crst);
765 if (!This->pcm)
767 FIXME("Bad pointer for pcm: %p\n", This->pcm);
768 LeaveCriticalSection(&This->pcm_crst);
769 return DSERR_GENERIC;
772 if (snd_pcm_state(This->pcm) != SND_PCM_STATE_RUNNING)
774 CheckXRUN(This);
775 hw_pptr = This->mmap_pos;
777 else
779 /* FIXME: Unused at the moment */
780 snd_pcm_uframes_t used = CommitAll(This, FALSE);
782 if (This->mmap_pos > used)
783 hw_pptr = This->mmap_pos - used;
784 else
785 hw_pptr = This->mmap_buflen_frames - used + This->mmap_pos;
787 hw_wptr = This->mmap_pos;
789 if (lpdwCappos)
790 *lpdwCappos = realpos_to_fakepos(This, hw_pptr);
791 if (lpdwReadpos)
792 *lpdwReadpos = realpos_to_fakepos(This, hw_wptr);
794 LeaveCriticalSection(&This->pcm_crst);
796 TRACE("hw_pptr=%u, hw_wptr=%u playpos=%u(%p), writepos=%u(%p)\n", (unsigned int)hw_pptr, (unsigned int)hw_wptr, realpos_to_fakepos(This, hw_pptr), lpdwCappos, realpos_to_fakepos(This, hw_wptr), lpdwReadpos);
797 return DS_OK;
800 static HRESULT WINAPI IDsCaptureDriverBufferImpl_GetStatus(PIDSCDRIVERBUFFER iface, LPDWORD lpdwStatus)
802 IDsCaptureDriverBufferImpl *This = (IDsCaptureDriverBufferImpl *)iface;
803 snd_pcm_state_t state;
804 TRACE("(%p,%p)\n",iface,lpdwStatus);
806 state = snd_pcm_state(This->pcm);
807 switch (state)
809 case SND_PCM_STATE_XRUN:
810 case SND_PCM_STATE_SUSPENDED:
811 case SND_PCM_STATE_RUNNING:
812 *lpdwStatus = DSCBSTATUS_CAPTURING | (This->play_looping ? DSCBSTATUS_LOOPING : 0);
813 break;
814 default:
815 *lpdwStatus = 0;
816 break;
819 TRACE("State: %d, flags: 0x%08x\n", state, *lpdwStatus);
820 return DS_OK;
823 static HRESULT WINAPI IDsCaptureDriverBufferImpl_Start(PIDSCDRIVERBUFFER iface, DWORD dwFlags)
825 IDsCaptureDriverBufferImpl *This = (IDsCaptureDriverBufferImpl *)iface;
826 TRACE("(%p,%x)\n",iface,dwFlags);
828 /* **** */
829 EnterCriticalSection(&This->pcm_crst);
830 snd_pcm_start(This->pcm);
831 This->play_looping = !!(dwFlags & DSCBSTART_LOOPING);
832 if (!This->play_looping)
833 /* Not well supported because of the difference in ALSA size and DSOUND's notion of size
834 * what it does right now is fill the buffer once.. ALSA size */
835 FIXME("Non-looping buffers are not properly supported!\n");
836 CommitAll(This, TRUE);
838 if (This->notify && This->notify->nrofnotifies && This->notify->notifies)
840 DWORD playpos = realpos_to_fakepos(This, This->mmap_pos);
841 if (playpos)
842 Capture_CheckNotify(This->notify, 0, playpos);
843 This->notify->playpos = playpos;
846 /* **** */
847 LeaveCriticalSection(&This->pcm_crst);
848 return DS_OK;
851 static HRESULT WINAPI IDsCaptureDriverBufferImpl_Stop(PIDSCDRIVERBUFFER iface)
853 IDsCaptureDriverBufferImpl *This = (IDsCaptureDriverBufferImpl *)iface;
854 TRACE("(%p)\n",iface);
856 /* **** */
857 EnterCriticalSection(&This->pcm_crst);
858 This->play_looping = FALSE;
859 snd_pcm_drop(This->pcm);
860 snd_pcm_prepare(This->pcm);
862 if (This->notify && This->notify->notifies && This->notify->nrofnotifies)
863 Capture_CheckNotify(This->notify, 0, 0);
865 /* **** */
866 LeaveCriticalSection(&This->pcm_crst);
867 return DS_OK;
870 static const IDsCaptureDriverBufferVtbl dsdbvt =
872 IDsCaptureDriverBufferImpl_QueryInterface,
873 IDsCaptureDriverBufferImpl_AddRef,
874 IDsCaptureDriverBufferImpl_Release,
875 IDsCaptureDriverBufferImpl_Lock,
876 IDsCaptureDriverBufferImpl_Unlock,
877 IDsCaptureDriverBufferImpl_SetFormat,
878 IDsCaptureDriverBufferImpl_GetPosition,
879 IDsCaptureDriverBufferImpl_GetStatus,
880 IDsCaptureDriverBufferImpl_Start,
881 IDsCaptureDriverBufferImpl_Stop
884 static HRESULT WINAPI IDsCaptureDriverImpl_QueryInterface(PIDSCDRIVER iface, REFIID riid, LPVOID *ppobj)
886 /* IDsCaptureDriverImpl *This = (IDsCaptureDriverImpl *)iface; */
887 FIXME("(%p): stub!\n",iface);
888 return DSERR_UNSUPPORTED;
891 static ULONG WINAPI IDsCaptureDriverImpl_AddRef(PIDSCDRIVER iface)
893 IDsCaptureDriverImpl *This = (IDsCaptureDriverImpl *)iface;
894 ULONG refCount = InterlockedIncrement(&This->ref);
896 TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
898 return refCount;
901 static ULONG WINAPI IDsCaptureDriverImpl_Release(PIDSCDRIVER iface)
903 IDsCaptureDriverImpl *This = (IDsCaptureDriverImpl *)iface;
904 ULONG refCount = InterlockedDecrement(&This->ref);
906 TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
908 if (refCount)
909 return refCount;
911 HeapFree(GetProcessHeap(), 0, This);
912 return 0;
915 static HRESULT WINAPI IDsCaptureDriverImpl_GetDriverDesc(PIDSCDRIVER iface, PDSDRIVERDESC pDesc)
917 IDsCaptureDriverImpl *This = (IDsCaptureDriverImpl *)iface;
918 TRACE("(%p,%p)\n",iface,pDesc);
919 *pDesc = WInDev[This->wDevID].ds_desc;
920 pDesc->dwFlags = 0;
921 pDesc->dnDevNode = WInDev[This->wDevID].waveDesc.dnDevNode;
922 pDesc->wVxdId = 0;
923 pDesc->wReserved = 0;
924 pDesc->ulDeviceNum = This->wDevID;
925 pDesc->dwHeapType = DSDHEAP_NOHEAP;
926 pDesc->pvDirectDrawHeap = NULL;
927 pDesc->dwMemStartAddress = 0xDEAD0000;
928 pDesc->dwMemEndAddress = 0xDEAF0000;
929 pDesc->dwMemAllocExtra = 0;
930 pDesc->pvReserved1 = NULL;
931 pDesc->pvReserved2 = NULL;
932 return DS_OK;
935 static HRESULT WINAPI IDsCaptureDriverImpl_Open(PIDSCDRIVER iface)
937 HRESULT hr = S_OK;
938 IDsCaptureDriverImpl *This = (IDsCaptureDriverImpl *)iface;
939 int err=0;
940 snd_pcm_t *pcm = NULL;
941 snd_pcm_hw_params_t *hw_params;
943 /* While this is not really needed, it is a good idea to do this,
944 * to see if sound can be initialized */
946 hw_params = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, snd_pcm_hw_params_sizeof());
947 if (!hw_params)
949 hr = DSERR_OUTOFMEMORY;
950 WARN("--> %08x\n", hr);
951 return hr;
954 err = snd_pcm_open(&pcm, WInDev[This->wDevID].pcmname, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK);
955 if (err < 0) goto err;
956 err = snd_pcm_hw_params_any(pcm, hw_params);
957 if (err < 0) goto err;
958 err = snd_pcm_hw_params_set_access (pcm, hw_params, SND_PCM_ACCESS_MMAP_INTERLEAVED);
959 if (err < 0)
961 err = snd_pcm_hw_params_set_access (pcm, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
962 if (err < 0) goto err;
965 TRACE("Success\n");
966 snd_pcm_close(pcm);
967 HeapFree(GetProcessHeap(), 0, hw_params);
968 return hr;
970 err:
971 hr = DSERR_GENERIC;
972 WARN("Failed to open device: %s\n", snd_strerror(err));
973 if (pcm)
974 snd_pcm_close(pcm);
975 HeapFree(GetProcessHeap(), 0, hw_params);
976 WARN("--> %08x\n", hr);
977 return hr;
980 static HRESULT WINAPI IDsCaptureDriverImpl_Close(PIDSCDRIVER iface)
982 IDsCaptureDriverImpl *This = (IDsCaptureDriverImpl *)iface;
983 TRACE("(%p) stub, harmless\n",This);
984 return DS_OK;
987 static HRESULT WINAPI IDsCaptureDriverImpl_GetCaps(PIDSCDRIVER iface, PDSCDRIVERCAPS pCaps)
989 IDsCaptureDriverImpl *This = (IDsCaptureDriverImpl *)iface;
990 WINE_WAVEDEV *wwi = &WInDev[This->wDevID];
991 TRACE("(%p,%p)\n",iface,pCaps);
992 pCaps->dwSize = sizeof(DSCDRIVERCAPS);
993 pCaps->dwFlags = wwi->ds_caps.dwFlags;
994 pCaps->dwFormats = wwi->incaps.dwFormats;
995 pCaps->dwChannels = wwi->incaps.wChannels;
996 return DS_OK;
999 static HRESULT WINAPI IDsCaptureDriverImpl_CreateCaptureBuffer(PIDSCDRIVER iface,
1000 LPWAVEFORMATEX pwfx,
1001 DWORD dwFlags, DWORD dwCardAddress,
1002 LPDWORD pdwcbBufferSize,
1003 LPBYTE *ppbBuffer,
1004 LPVOID *ppvObj)
1006 IDsCaptureDriverImpl *This = (IDsCaptureDriverImpl *)iface;
1007 IDsCaptureDriverBufferImpl** ippdsdb = (IDsCaptureDriverBufferImpl**)ppvObj;
1008 HRESULT err;
1010 TRACE("(%p,%p,%x,%x)\n",iface,pwfx,dwFlags,dwCardAddress);
1012 if (This->capture_buffer)
1013 return DSERR_ALLOCATED;
1015 This->capture_buffer = *ippdsdb = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDsCaptureDriverBufferImpl));
1016 if (*ippdsdb == NULL)
1017 return DSERR_OUTOFMEMORY;
1019 (*ippdsdb)->hw_params = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, snd_pcm_hw_params_sizeof());
1020 (*ippdsdb)->sw_params = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, snd_pcm_sw_params_sizeof());
1021 (*ippdsdb)->presented_buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *pdwcbBufferSize);
1022 if (!(*ippdsdb)->hw_params || !(*ippdsdb)->sw_params || !(*ippdsdb)->presented_buffer)
1024 HeapFree(GetProcessHeap(), 0, (*ippdsdb)->sw_params);
1025 HeapFree(GetProcessHeap(), 0, (*ippdsdb)->hw_params);
1026 HeapFree(GetProcessHeap(), 0, (*ippdsdb)->presented_buffer);
1027 return DSERR_OUTOFMEMORY;
1029 (*ippdsdb)->lpVtbl = &dsdbvt;
1030 (*ippdsdb)->ref = 1;
1031 (*ippdsdb)->drv = This;
1032 (*ippdsdb)->mmap_buflen_bytes = *pdwcbBufferSize;
1033 InitializeCriticalSection(&(*ippdsdb)->pcm_crst);
1034 (*ippdsdb)->pcm_crst.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": ALSA_DSCAPTURE.pcm_crst");
1036 /* SetFormat initialises pcm */
1037 err = IDsDriverBuffer_SetFormat((IDsDriverBuffer*)*ppvObj, pwfx);
1038 if (FAILED(err))
1040 WARN("Error occurred: %08x\n", err);
1041 goto err;
1043 *ppbBuffer = (*ippdsdb)->presented_buffer;
1045 /* buffer is ready to go */
1046 TRACE("buffer created at %p\n", *ippdsdb);
1047 return err;
1049 err:
1050 HeapFree(GetProcessHeap(), 0, (*ippdsdb)->presented_buffer);
1051 HeapFree(GetProcessHeap(), 0, (*ippdsdb)->sw_params);
1052 HeapFree(GetProcessHeap(), 0, (*ippdsdb)->hw_params);
1053 HeapFree(GetProcessHeap(), 0, *ippdsdb);
1054 *ippdsdb = NULL;
1055 return err;
1058 static const IDsCaptureDriverVtbl dscdvt =
1060 IDsCaptureDriverImpl_QueryInterface,
1061 IDsCaptureDriverImpl_AddRef,
1062 IDsCaptureDriverImpl_Release,
1063 IDsCaptureDriverImpl_GetDriverDesc,
1064 IDsCaptureDriverImpl_Open,
1065 IDsCaptureDriverImpl_Close,
1066 IDsCaptureDriverImpl_GetCaps,
1067 IDsCaptureDriverImpl_CreateCaptureBuffer
1070 /**************************************************************************
1071 * widDsCreate [internal]
1073 DWORD widDsCreate(UINT wDevID, PIDSCDRIVER* drv)
1075 IDsCaptureDriverImpl** idrv = (IDsCaptureDriverImpl**)drv;
1076 TRACE("(%d,%p)\n",wDevID,drv);
1078 *idrv = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(IDsCaptureDriverImpl));
1079 if (!*idrv)
1080 return MMSYSERR_NOMEM;
1081 (*idrv)->lpVtbl = &dscdvt;
1082 (*idrv)->ref = 1;
1084 (*idrv)->wDevID = wDevID;
1085 return MMSYSERR_NOERROR;
1088 /**************************************************************************
1089 * widDsDesc [internal]
1091 DWORD widDsDesc(UINT wDevID, PDSDRIVERDESC desc)
1093 *desc = WInDev[wDevID].ds_desc;
1094 return MMSYSERR_NOERROR;