winebuild: Signal an error when a thiscall function doesn't take a pointer as first...
[wine/hramrach.git] / dlls / winealsa.drv / dscapture.c
blobf663b6c6f1a60807674e0711fce83f34a8e1c260
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"
52 #include "alsa.h"
53 #include "wine/library.h"
54 #include "wine/unicode.h"
55 #include "wine/debug.h"
57 #ifdef HAVE_ALSA
59 /* Notify timer checks every 10 ms with a resolution of 2 ms */
60 #define DS_TIME_DEL 10
61 #define DS_TIME_RES 2
63 WINE_DEFAULT_DEBUG_CHANNEL(dsalsa);
65 typedef struct IDsCaptureDriverBufferImpl IDsCaptureDriverBufferImpl;
67 typedef struct IDsCaptureDriverImpl
69 const IDsCaptureDriverVtbl *lpVtbl;
70 LONG ref;
71 IDsCaptureDriverBufferImpl* capture_buffer;
72 UINT wDevID;
73 } IDsCaptureDriverImpl;
75 typedef struct IDsCaptureDriverNotifyImpl
77 const IDsDriverNotifyVtbl *lpVtbl;
78 LONG ref;
79 IDsCaptureDriverBufferImpl *buffer;
80 DSBPOSITIONNOTIFY *notifies;
81 DWORD nrofnotifies, playpos;
82 UINT timerID;
83 } IDsCaptureDriverNotifyImpl;
85 struct IDsCaptureDriverBufferImpl
87 const IDsCaptureDriverBufferVtbl *lpVtbl;
88 LONG ref;
89 IDsCaptureDriverImpl *drv;
90 IDsCaptureDriverNotifyImpl *notify;
92 CRITICAL_SECTION pcm_crst;
93 LPBYTE mmap_buffer, presented_buffer;
94 DWORD mmap_buflen_bytes, play_looping, mmap_ofs_bytes;
95 BOOL mmap;
97 /* Note: snd_pcm_frames_to_bytes(This->pcm, mmap_buflen_frames) != mmap_buflen_bytes */
98 /* The actual buffer may differ in size from the wanted buffer size */
100 snd_pcm_t *pcm;
101 snd_pcm_hw_params_t *hw_params;
102 snd_pcm_sw_params_t *sw_params;
103 snd_pcm_uframes_t mmap_buflen_frames, mmap_pos;
106 static void Capture_CheckNotify(IDsCaptureDriverNotifyImpl *This, DWORD from, DWORD len)
108 unsigned i;
109 for (i = 0; i < This->nrofnotifies; ++i) {
110 LPDSBPOSITIONNOTIFY event = This->notifies + i;
111 DWORD offset = event->dwOffset;
112 TRACE("checking %d, position %d, event = %p\n", i, offset, event->hEventNotify);
114 if (offset == DSBPN_OFFSETSTOP) {
115 if (!from && !len) {
116 SetEvent(event->hEventNotify);
117 TRACE("signalled event %p (%d)\n", event->hEventNotify, i);
118 return;
120 else return;
123 if (offset >= from && offset < (from + len))
125 TRACE("signalled event %p (%d)\n", event->hEventNotify, i);
126 SetEvent(event->hEventNotify);
131 static void CALLBACK Capture_Notify(UINT timerID, UINT msg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
133 IDsCaptureDriverBufferImpl *This = (IDsCaptureDriverBufferImpl *)dwUser;
134 DWORD last_playpos, playpos;
135 PIDSCDRIVERBUFFER iface = (PIDSCDRIVERBUFFER)This;
137 /* **** */
138 /* Don't deadlock since there is a critical section can be held by the timer api itself while running this code */
139 if (!TryEnterCriticalSection(&This->pcm_crst)) return;
141 IDsDriverBuffer_GetPosition(iface, &playpos, NULL);
142 last_playpos = This->notify->playpos;
143 This->notify->playpos = playpos;
145 if (snd_pcm_state(This->pcm) != SND_PCM_STATE_RUNNING || last_playpos == playpos || !This->notify->nrofnotifies || !This->notify->notifies)
146 goto done;
148 if (playpos < last_playpos)
150 Capture_CheckNotify(This->notify, last_playpos, This->mmap_buflen_bytes);
151 if (playpos)
152 Capture_CheckNotify(This->notify, 0, playpos);
154 else Capture_CheckNotify(This->notify, last_playpos, playpos - last_playpos);
156 done:
157 LeaveCriticalSection(&This->pcm_crst);
158 /* **** */
161 static HRESULT WINAPI IDsCaptureDriverNotifyImpl_QueryInterface(PIDSDRIVERNOTIFY iface, REFIID riid, LPVOID *ppobj)
163 IDsCaptureDriverNotifyImpl *This = (IDsCaptureDriverNotifyImpl *)iface;
164 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
166 if ( IsEqualGUID(riid, &IID_IUnknown) ||
167 IsEqualGUID(riid, &IID_IDsDriverNotify) ) {
168 IDsDriverNotify_AddRef(iface);
169 *ppobj = This;
170 return DS_OK;
173 FIXME( "Unknown IID %s\n", debugstr_guid(riid));
175 *ppobj = 0;
176 return E_NOINTERFACE;
179 static ULONG WINAPI IDsCaptureDriverNotifyImpl_AddRef(PIDSDRIVERNOTIFY iface)
181 IDsCaptureDriverNotifyImpl *This = (IDsCaptureDriverNotifyImpl *)iface;
182 ULONG refCount = InterlockedIncrement(&This->ref);
184 TRACE("(%p) ref was %d\n", This, refCount - 1);
186 return refCount;
189 static ULONG WINAPI IDsCaptureDriverNotifyImpl_Release(PIDSDRIVERNOTIFY iface)
191 IDsCaptureDriverNotifyImpl *This = (IDsCaptureDriverNotifyImpl *)iface;
192 ULONG refCount = InterlockedDecrement(&This->ref);
194 TRACE("(%p) ref was %d\n", This, refCount + 1);
196 if (!refCount) {
197 This->buffer->notify = NULL;
198 if (This->timerID)
200 timeKillEvent(This->timerID);
201 timeEndPeriod(DS_TIME_RES);
203 HeapFree(GetProcessHeap(), 0, This->notifies);
204 HeapFree(GetProcessHeap(), 0, This);
205 TRACE("(%p) released\n", This);
207 return refCount;
210 static HRESULT WINAPI IDsCaptureDriverNotifyImpl_SetNotificationPositions(PIDSDRIVERNOTIFY iface, DWORD howmuch, LPCDSBPOSITIONNOTIFY notify)
212 DWORD len = howmuch * sizeof(DSBPOSITIONNOTIFY);
213 unsigned i;
214 LPVOID notifies;
215 IDsCaptureDriverNotifyImpl *This = (IDsCaptureDriverNotifyImpl *)iface;
216 TRACE("(%p,0x%08x,%p)\n",This,howmuch,notify);
218 if (!notify) {
219 WARN("invalid parameter\n");
220 return DSERR_INVALIDPARAM;
223 if (TRACE_ON(dsalsa))
224 for (i=0;i<howmuch; ++i)
225 TRACE("notify at %d to %p\n", notify[i].dwOffset, notify[i].hEventNotify);
227 /* **** */
228 EnterCriticalSection(&This->buffer->pcm_crst);
230 /* Make an internal copy of the caller-supplied array.
231 * Replace the existing copy if one is already present. */
232 if (This->notifies)
233 notifies = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->notifies, len);
234 else
235 notifies = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
237 if (!notifies)
239 LeaveCriticalSection(&This->buffer->pcm_crst);
240 /* **** */
241 return DSERR_OUTOFMEMORY;
243 This->notifies = notifies;
244 memcpy(This->notifies, notify, len);
245 This->nrofnotifies = howmuch;
246 IDsDriverBuffer_GetPosition((PIDSCDRIVERBUFFER)This->buffer, &This->playpos, NULL);
248 if (!This->timerID)
250 timeBeginPeriod(DS_TIME_RES);
251 This->timerID = timeSetEvent(DS_TIME_DEL, DS_TIME_RES, Capture_Notify, (DWORD_PTR)This->buffer, TIME_PERIODIC | TIME_KILL_SYNCHRONOUS);
254 LeaveCriticalSection(&This->buffer->pcm_crst);
255 /* **** */
257 return S_OK;
260 static const IDsDriverNotifyVtbl dscdnvt =
262 IDsCaptureDriverNotifyImpl_QueryInterface,
263 IDsCaptureDriverNotifyImpl_AddRef,
264 IDsCaptureDriverNotifyImpl_Release,
265 IDsCaptureDriverNotifyImpl_SetNotificationPositions,
268 #if 0
269 /** Convert the position an application sees into a position ALSA sees */
270 static snd_pcm_uframes_t fakepos_to_realpos(const IDsCaptureDriverBufferImpl* This, DWORD fakepos)
272 snd_pcm_uframes_t realpos;
273 if (fakepos < This->mmap_ofs_bytes)
274 realpos = This->mmap_buflen_bytes + fakepos - This->mmap_ofs_bytes;
275 else realpos = fakepos - This->mmap_ofs_bytes;
276 return snd_pcm_bytes_to_frames(This->pcm, realpos) % This->mmap_buflen_frames;
278 #endif
280 /** Convert the position ALSA sees into a position an application sees */
281 static DWORD realpos_to_fakepos(const IDsCaptureDriverBufferImpl* This, snd_pcm_uframes_t realpos)
283 DWORD realposb = snd_pcm_frames_to_bytes(This->pcm, realpos);
284 return (realposb + This->mmap_ofs_bytes) % This->mmap_buflen_bytes;
287 /** Raw copy data, with buffer wrap around */
288 static void CopyDataWrap(const IDsCaptureDriverBufferImpl* This, LPBYTE dest, DWORD fromwhere, DWORD copylen, DWORD buflen)
290 DWORD remainder = buflen - fromwhere;
291 if (remainder >= copylen)
293 CopyMemory(dest, This->mmap_buffer + fromwhere, copylen);
295 else
297 CopyMemory(dest, This->mmap_buffer + fromwhere, remainder);
298 copylen -= remainder;
299 CopyMemory(dest, This->mmap_buffer, copylen);
303 /** Copy data from the mmap buffer to backbuffer, taking into account all wraparounds that may occur */
304 static void CopyData(const IDsCaptureDriverBufferImpl* This, snd_pcm_uframes_t fromwhere, snd_pcm_uframes_t len)
306 DWORD dlen = snd_pcm_frames_to_bytes(This->pcm, len) % This->mmap_buflen_bytes;
308 /* Backbuffer */
309 DWORD ofs = realpos_to_fakepos(This, fromwhere);
310 DWORD remainder = This->mmap_buflen_bytes - ofs;
312 /* MMAP buffer */
313 DWORD realbuflen = snd_pcm_frames_to_bytes(This->pcm, This->mmap_buflen_frames);
314 DWORD realofs = snd_pcm_frames_to_bytes(This->pcm, fromwhere);
316 if (remainder >= dlen)
318 CopyDataWrap(This, This->presented_buffer + ofs, realofs, dlen, realbuflen);
320 else
322 CopyDataWrap(This, This->presented_buffer + ofs, realofs, remainder, realbuflen);
323 dlen -= remainder;
324 CopyDataWrap(This, This->presented_buffer, (realofs+remainder)%realbuflen, dlen, realbuflen);
328 /** Fill buffers, for starting and stopping
329 * Alsa won't start playing until everything is filled up
330 * This also updates mmap_pos
332 * Returns: Amount of periods in use so snd_pcm_avail_update
333 * doesn't have to be called up to 4x in GetPosition()
335 static snd_pcm_uframes_t CommitAll(IDsCaptureDriverBufferImpl *This, DWORD forced)
337 const snd_pcm_channel_area_t *areas;
338 snd_pcm_uframes_t used;
339 const snd_pcm_uframes_t commitahead = This->mmap_buflen_frames;
341 used = This->mmap_buflen_frames - snd_pcm_avail_update(This->pcm);
342 TRACE("%p needs to commit to %lu, used: %lu\n", This, commitahead, used);
343 if (used < commitahead && (forced || This->play_looping))
345 snd_pcm_uframes_t done, putin = commitahead - used;
346 if (This->mmap)
348 snd_pcm_mmap_begin(This->pcm, &areas, &This->mmap_pos, &putin);
349 CopyData(This, This->mmap_pos, putin);
350 done = snd_pcm_mmap_commit(This->pcm, This->mmap_pos, putin);
352 This->mmap_pos += done;
353 used += done;
354 putin = commitahead - used;
356 if (This->mmap_pos == This->mmap_buflen_frames && (snd_pcm_sframes_t)putin > 0 && This->play_looping)
358 This->mmap_ofs_bytes += snd_pcm_frames_to_bytes(This->pcm, This->mmap_buflen_frames);
359 This->mmap_ofs_bytes %= This->mmap_buflen_bytes;
361 snd_pcm_mmap_begin(This->pcm, &areas, &This->mmap_pos, &putin);
362 CopyData(This, This->mmap_pos, putin);
363 done = snd_pcm_mmap_commit(This->pcm, This->mmap_pos, putin);
365 This->mmap_pos += done;
366 used += done;
369 else
371 DWORD pos;
372 snd_pcm_sframes_t ret;
374 snd_pcm_uframes_t cap = snd_pcm_bytes_to_frames(This->pcm, This->mmap_buflen_bytes);
375 pos = realpos_to_fakepos(This, This->mmap_pos);
376 if (This->mmap_pos + putin > cap)
377 putin = cap - This->mmap_pos;
378 ret = snd_pcm_readi(This->pcm, This->presented_buffer + pos, putin);
379 if (ret == -EPIPE)
381 WARN("Underrun occurred\n");
382 snd_pcm_prepare(This->pcm);
383 ret = snd_pcm_readi(This->pcm, This->presented_buffer + pos, putin);
384 snd_pcm_start(This->pcm);
386 if (ret < 0)
388 WARN("Committing data: %ld / %s (%ld)\n", ret, snd_strerror(ret), putin);
389 ret = 0;
391 This->mmap_pos += ret;
392 used += ret;
393 /* At this point mmap_pos may be >= This->mmap_pos this is harmless
394 * realpos_to_fakepos handles it well, and below it is truncated
397 putin = commitahead - used;
398 if (putin > 0)
400 pos = realpos_to_fakepos(This, This->mmap_pos);
401 ret = snd_pcm_readi(This->pcm, This->presented_buffer + pos, putin);
402 if (ret > 0)
404 This->mmap_pos += ret;
405 used += ret;
412 if (This->mmap_pos >= This->mmap_buflen_frames)
414 This->mmap_ofs_bytes += snd_pcm_frames_to_bytes(This->pcm, This->mmap_buflen_frames);
415 This->mmap_ofs_bytes %= This->mmap_buflen_bytes;
416 This->mmap_pos -= This->mmap_buflen_frames;
419 return used;
422 static void CheckXRUN(IDsCaptureDriverBufferImpl* This)
424 snd_pcm_state_t state = snd_pcm_state(This->pcm);
425 snd_pcm_sframes_t delay;
426 int err;
428 snd_pcm_hwsync(This->pcm);
429 snd_pcm_delay(This->pcm, &delay);
430 if ( state == SND_PCM_STATE_XRUN )
432 err = snd_pcm_prepare(This->pcm);
433 CommitAll(This, FALSE);
434 snd_pcm_start(This->pcm);
435 WARN("xrun occurred\n");
436 if ( err < 0 )
437 ERR("recovery from xrun failed, prepare failed: %s\n", snd_strerror(err));
439 else if ( state == SND_PCM_STATE_SUSPENDED )
441 int err = snd_pcm_resume(This->pcm);
442 TRACE("recovery from suspension occurred\n");
443 if (err < 0 && err != -EAGAIN){
444 err = snd_pcm_prepare(This->pcm);
445 if (err < 0)
446 ERR("recovery from suspend failed, prepare failed: %s\n", snd_strerror(err));
449 else if ( state != SND_PCM_STATE_RUNNING)
451 WARN("Unhandled state: %d\n", state);
456 * Allocate the memory-mapped buffer for direct sound, and set up the
457 * callback.
459 static int CreateMMAP(IDsCaptureDriverBufferImpl* pdbi)
461 snd_pcm_t *pcm = pdbi->pcm;
462 snd_pcm_format_t format;
463 snd_pcm_uframes_t frames, ofs, avail, psize, boundary;
464 unsigned int channels, bits_per_sample, bits_per_frame;
465 int err, mmap_mode;
466 const snd_pcm_channel_area_t *areas;
467 snd_pcm_hw_params_t *hw_params = pdbi->hw_params;
468 snd_pcm_sw_params_t *sw_params = pdbi->sw_params;
470 mmap_mode = snd_pcm_type(pcm);
472 if (mmap_mode == SND_PCM_TYPE_HW)
473 TRACE("mmap'd buffer is a direct hardware buffer.\n");
474 else if (mmap_mode == SND_PCM_TYPE_DMIX)
475 TRACE("mmap'd buffer is an ALSA dmix buffer\n");
476 else
477 TRACE("mmap'd buffer is an ALSA type %d buffer\n", mmap_mode);
479 err = snd_pcm_hw_params_get_period_size(hw_params, &psize, NULL);
480 err = snd_pcm_hw_params_get_format(hw_params, &format);
481 err = snd_pcm_hw_params_get_buffer_size(hw_params, &frames);
482 err = snd_pcm_hw_params_get_channels(hw_params, &channels);
483 bits_per_sample = snd_pcm_format_physical_width(format);
484 bits_per_frame = bits_per_sample * channels;
486 if (TRACE_ON(dsalsa))
487 ALSA_TraceParameters(hw_params, NULL, FALSE);
489 TRACE("format=%s frames=%ld channels=%d bits_per_sample=%d bits_per_frame=%d\n",
490 snd_pcm_format_name(format), frames, channels, bits_per_sample, bits_per_frame);
492 pdbi->mmap_buflen_frames = frames;
493 snd_pcm_sw_params_current(pcm, sw_params);
494 snd_pcm_sw_params_set_start_threshold(pcm, sw_params, 0);
495 snd_pcm_sw_params_get_boundary(sw_params, &boundary);
496 snd_pcm_sw_params_set_stop_threshold(pcm, sw_params, boundary);
497 snd_pcm_sw_params_set_silence_threshold(pcm, sw_params, INT_MAX);
498 snd_pcm_sw_params_set_silence_size(pcm, sw_params, 0);
499 snd_pcm_sw_params_set_avail_min(pcm, sw_params, 0);
500 err = snd_pcm_sw_params(pcm, sw_params);
502 pdbi->mmap_ofs_bytes = 0;
503 if (!pdbi->mmap)
505 pdbi->mmap_buffer = NULL;
507 frames = snd_pcm_bytes_to_frames(pdbi->pcm, pdbi->mmap_buflen_bytes);
508 snd_pcm_format_set_silence(format, pdbi->presented_buffer, frames);
509 pdbi->mmap_pos = 0;
511 else
513 err = snd_pcm_mmap_begin(pcm, &areas, &ofs, &avail);
514 if ( err < 0 )
516 ERR("Can't map sound device for direct access: %s/%d\n", snd_strerror(err), err);
517 return DSERR_GENERIC;
519 snd_pcm_format_set_silence(format, areas->addr, pdbi->mmap_buflen_frames);
520 pdbi->mmap_pos = ofs + snd_pcm_mmap_commit(pcm, ofs, 0);
521 pdbi->mmap_buffer = areas->addr;
524 TRACE("created mmap buffer of %ld frames (%d bytes) at %p\n",
525 pdbi->mmap_buflen_frames, pdbi->mmap_buflen_bytes, pdbi->mmap_buffer);
527 return DS_OK;
530 static HRESULT WINAPI IDsCaptureDriverBufferImpl_QueryInterface(PIDSCDRIVERBUFFER iface, REFIID riid, LPVOID *ppobj)
532 IDsCaptureDriverBufferImpl *This = (IDsCaptureDriverBufferImpl *)iface;
533 if ( IsEqualGUID(riid, &IID_IUnknown) ||
534 IsEqualGUID(riid, &IID_IDsCaptureDriverBuffer) ) {
535 IDsCaptureDriverBuffer_AddRef(iface);
536 *ppobj = iface;
537 return DS_OK;
540 if ( IsEqualGUID( &IID_IDsDriverNotify, riid ) ) {
541 if (!This->notify)
543 This->notify = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDsCaptureDriverNotifyImpl));
544 if (!This->notify)
545 return DSERR_OUTOFMEMORY;
546 This->notify->lpVtbl = &dscdnvt;
547 This->notify->buffer = This;
549 /* Keep a lock on IDsDriverNotify for ourself, so it is destroyed when the buffer is */
550 IDsDriverNotify_AddRef((PIDSDRIVERNOTIFY)This->notify);
552 IDsDriverNotify_AddRef((PIDSDRIVERNOTIFY)This->notify);
553 *ppobj = This->notify;
554 return DS_OK;
557 if ( IsEqualGUID( &IID_IDsDriverPropertySet, riid ) ) {
558 FIXME("Unsupported interface IID_IDsDriverPropertySet\n");
559 return E_FAIL;
562 FIXME("(): Unknown interface %s\n", debugstr_guid(riid));
563 return DSERR_UNSUPPORTED;
566 static ULONG WINAPI IDsCaptureDriverBufferImpl_AddRef(PIDSCDRIVERBUFFER iface)
568 IDsCaptureDriverBufferImpl *This = (IDsCaptureDriverBufferImpl *)iface;
569 ULONG refCount = InterlockedIncrement(&This->ref);
571 TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
573 return refCount;
576 static ULONG WINAPI IDsCaptureDriverBufferImpl_Release(PIDSCDRIVERBUFFER iface)
578 IDsCaptureDriverBufferImpl *This = (IDsCaptureDriverBufferImpl *)iface;
579 ULONG refCount = InterlockedDecrement(&This->ref);
581 TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
583 if (refCount)
584 return refCount;
586 EnterCriticalSection(&This->pcm_crst);
587 if (This->notify)
588 IDsDriverNotify_Release((PIDSDRIVERNOTIFY)This->notify);
589 TRACE("mmap buffer %p destroyed\n", This->mmap_buffer);
591 This->drv->capture_buffer = NULL;
592 LeaveCriticalSection(&This->pcm_crst);
593 This->pcm_crst.DebugInfo->Spare[0] = 0;
594 DeleteCriticalSection(&This->pcm_crst);
596 snd_pcm_drop(This->pcm);
597 snd_pcm_close(This->pcm);
598 This->pcm = NULL;
599 HeapFree(GetProcessHeap(), 0, This->presented_buffer);
600 HeapFree(GetProcessHeap(), 0, This->sw_params);
601 HeapFree(GetProcessHeap(), 0, This->hw_params);
602 HeapFree(GetProcessHeap(), 0, This);
603 return 0;
606 static HRESULT WINAPI IDsCaptureDriverBufferImpl_Lock(PIDSCDRIVERBUFFER iface, LPVOID*ppvAudio1,LPDWORD pdwLen1,LPVOID*ppvAudio2,LPDWORD pdwLen2, DWORD dwWritePosition,DWORD dwWriteLen, DWORD dwFlags)
608 IDsCaptureDriverBufferImpl *This = (IDsCaptureDriverBufferImpl *)iface;
609 TRACE("(%p,%p,%p,%p,%p,%d,%d,0x%08x)\n", This, ppvAudio1, pdwLen1, ppvAudio2, pdwLen2, dwWritePosition, dwWriteLen, dwFlags);
611 if (ppvAudio1)
612 *ppvAudio1 = (LPBYTE)This->presented_buffer + dwWritePosition;
614 if (dwWritePosition + dwWriteLen <= This->mmap_buflen_bytes) {
615 if (pdwLen1)
616 *pdwLen1 = dwWriteLen;
617 if (ppvAudio2)
618 *ppvAudio2 = 0;
619 if (pdwLen2)
620 *pdwLen2 = 0;
621 } else {
622 if (pdwLen1)
623 *pdwLen1 = This->mmap_buflen_bytes - dwWritePosition;
624 if (ppvAudio2)
625 *ppvAudio2 = This->presented_buffer;
626 if (pdwLen2)
627 *pdwLen2 = dwWriteLen - (This->mmap_buflen_bytes - dwWritePosition);
629 return DS_OK;
632 static HRESULT WINAPI IDsCaptureDriverBufferImpl_Unlock(PIDSCDRIVERBUFFER iface, LPVOID pvAudio1,DWORD dwLen1, LPVOID pvAudio2,DWORD dwLen2)
634 IDsCaptureDriverBufferImpl *This = (IDsCaptureDriverBufferImpl *)iface;
635 TRACE("(%p,%p,%d,%p,%d)\n",This,pvAudio1,dwLen1,pvAudio2,dwLen2);
636 return DS_OK;
639 static HRESULT WINAPI IDsCaptureDriverBufferImpl_SetFormat(PIDSCDRIVERBUFFER iface, LPWAVEFORMATEX pwfx)
641 IDsCaptureDriverBufferImpl *This = (IDsCaptureDriverBufferImpl *)iface;
642 WINE_WAVEDEV *wwi = &WInDev[This->drv->wDevID];
643 snd_pcm_t *pcm = NULL;
644 snd_pcm_hw_params_t *hw_params = This->hw_params;
645 snd_pcm_format_t format = -1;
646 snd_pcm_uframes_t buffer_size;
647 DWORD rate = pwfx->nSamplesPerSec;
648 int err=0;
649 BOOL mmap;
651 TRACE("(%p, %p)\n", iface, pwfx);
653 switch (pwfx->wBitsPerSample)
655 case 8: format = SND_PCM_FORMAT_U8; break;
656 case 16: format = SND_PCM_FORMAT_S16_LE; break;
657 case 24: format = SND_PCM_FORMAT_S24_3LE; break;
658 case 32: format = SND_PCM_FORMAT_S32_LE; break;
659 default: FIXME("Unsupported bpp: %d\n", pwfx->wBitsPerSample); return DSERR_GENERIC;
662 /* **** */
663 EnterCriticalSection(&This->pcm_crst);
665 err = snd_pcm_open(&pcm, wwi->pcmname, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK);
667 if (err < 0)
669 if (errno != EBUSY || !This->pcm)
671 /* **** */
672 LeaveCriticalSection(&This->pcm_crst);
673 WARN("Cannot open sound device: %s\n", snd_strerror(err));
674 return DSERR_GENERIC;
676 snd_pcm_drop(This->pcm);
677 snd_pcm_close(This->pcm);
678 This->pcm = NULL;
679 err = snd_pcm_open(&pcm, wwi->pcmname, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK);
680 if (err < 0)
682 /* **** */
683 LeaveCriticalSection(&This->pcm_crst);
684 WARN("Cannot open sound device: %s\n", snd_strerror(err));
685 return DSERR_BUFFERLOST;
689 /* Set some defaults */
690 snd_pcm_hw_params_any(pcm, hw_params);
692 err = snd_pcm_hw_params_set_channels(pcm, hw_params, pwfx->nChannels);
693 if (err < 0) { WARN("Could not set channels to %d\n", pwfx->nChannels); goto err; }
695 err = snd_pcm_hw_params_set_format(pcm, hw_params, format);
696 if (err < 0) { WARN("Could not set format to %d bpp\n", pwfx->wBitsPerSample); goto err; }
698 err = snd_pcm_hw_params_set_rate_near(pcm, hw_params, &rate, NULL);
699 if (err < 0) { rate = pwfx->nSamplesPerSec; WARN("Could not set rate\n"); goto err; }
701 if (!ALSA_NearMatch(rate, pwfx->nSamplesPerSec))
703 WARN("Could not set sound rate to %d, but instead to %d\n", pwfx->nSamplesPerSec, rate);
704 pwfx->nSamplesPerSec = rate;
705 pwfx->nAvgBytesPerSec = rate * pwfx->nBlockAlign;
706 /* Let DirectSound detect this */
709 snd_pcm_hw_params_set_periods_integer(pcm, hw_params);
710 buffer_size = This->mmap_buflen_bytes / pwfx->nBlockAlign;
711 snd_pcm_hw_params_set_buffer_size_near(pcm, hw_params, &buffer_size);
712 buffer_size = 5000;
713 snd_pcm_hw_params_set_period_time_near(pcm, hw_params, (unsigned int*)&buffer_size, NULL);
715 err = snd_pcm_hw_params_set_access (pcm, hw_params, SND_PCM_ACCESS_MMAP_INTERLEAVED);
716 if (err < 0)
718 err = snd_pcm_hw_params_set_access (pcm, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
719 if (err < 0) { WARN("Could not set access\n"); goto err; }
720 mmap = 0;
722 else
723 mmap = 1;
725 err = snd_pcm_hw_params(pcm, hw_params);
726 if (err < 0) { WARN("Could not set hw parameters\n"); goto err; }
728 if (This->pcm)
730 snd_pcm_drop(This->pcm);
731 snd_pcm_close(This->pcm);
733 This->pcm = pcm;
734 This->mmap = mmap;
736 snd_pcm_prepare(This->pcm);
737 CreateMMAP(This);
739 /* **** */
740 LeaveCriticalSection(&This->pcm_crst);
741 return S_OK;
743 err:
744 if (err < 0)
745 WARN("Failed to apply changes: %s\n", snd_strerror(err));
747 if (!This->pcm)
748 This->pcm = pcm;
749 else
750 snd_pcm_close(pcm);
752 if (This->pcm)
753 snd_pcm_hw_params_current(This->pcm, This->hw_params);
755 /* **** */
756 LeaveCriticalSection(&This->pcm_crst);
757 return DSERR_BADFORMAT;
760 static HRESULT WINAPI IDsCaptureDriverBufferImpl_GetPosition(PIDSCDRIVERBUFFER iface, LPDWORD lpdwCappos, LPDWORD lpdwReadpos)
762 IDsCaptureDriverBufferImpl *This = (IDsCaptureDriverBufferImpl *)iface;
763 snd_pcm_uframes_t hw_pptr, hw_wptr;
765 EnterCriticalSection(&This->pcm_crst);
767 if (!This->pcm)
769 FIXME("Bad pointer for pcm: %p\n", This->pcm);
770 LeaveCriticalSection(&This->pcm_crst);
771 return DSERR_GENERIC;
774 if (snd_pcm_state(This->pcm) != SND_PCM_STATE_RUNNING)
776 CheckXRUN(This);
777 hw_pptr = This->mmap_pos;
779 else
781 /* FIXME: Unused at the moment */
782 snd_pcm_uframes_t used = CommitAll(This, FALSE);
784 if (This->mmap_pos > used)
785 hw_pptr = This->mmap_pos - used;
786 else
787 hw_pptr = This->mmap_buflen_frames - used + This->mmap_pos;
789 hw_wptr = This->mmap_pos;
791 if (lpdwCappos)
792 *lpdwCappos = realpos_to_fakepos(This, hw_pptr);
793 if (lpdwReadpos)
794 *lpdwReadpos = realpos_to_fakepos(This, hw_wptr);
796 LeaveCriticalSection(&This->pcm_crst);
798 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);
799 return DS_OK;
802 static HRESULT WINAPI IDsCaptureDriverBufferImpl_GetStatus(PIDSCDRIVERBUFFER iface, LPDWORD lpdwStatus)
804 IDsCaptureDriverBufferImpl *This = (IDsCaptureDriverBufferImpl *)iface;
805 snd_pcm_state_t state;
806 TRACE("(%p,%p)\n",iface,lpdwStatus);
808 state = snd_pcm_state(This->pcm);
809 switch (state)
811 case SND_PCM_STATE_XRUN:
812 case SND_PCM_STATE_SUSPENDED:
813 case SND_PCM_STATE_RUNNING:
814 *lpdwStatus = DSCBSTATUS_CAPTURING | (This->play_looping ? DSCBSTATUS_LOOPING : 0);
815 break;
816 default:
817 *lpdwStatus = 0;
818 break;
821 TRACE("State: %d, flags: 0x%08x\n", state, *lpdwStatus);
822 return DS_OK;
825 static HRESULT WINAPI IDsCaptureDriverBufferImpl_Start(PIDSCDRIVERBUFFER iface, DWORD dwFlags)
827 IDsCaptureDriverBufferImpl *This = (IDsCaptureDriverBufferImpl *)iface;
828 TRACE("(%p,%x)\n",iface,dwFlags);
830 /* **** */
831 EnterCriticalSection(&This->pcm_crst);
832 snd_pcm_start(This->pcm);
833 This->play_looping = !!(dwFlags & DSCBSTART_LOOPING);
834 if (!This->play_looping)
835 /* Not well supported because of the difference in ALSA size and DSOUND's notion of size
836 * what it does right now is fill the buffer once.. ALSA size */
837 FIXME("Non-looping buffers are not properly supported!\n");
838 CommitAll(This, TRUE);
840 if (This->notify && This->notify->nrofnotifies && This->notify->notifies)
842 DWORD playpos = realpos_to_fakepos(This, This->mmap_pos);
843 if (playpos)
844 Capture_CheckNotify(This->notify, 0, playpos);
845 This->notify->playpos = playpos;
848 /* **** */
849 LeaveCriticalSection(&This->pcm_crst);
850 return DS_OK;
853 static HRESULT WINAPI IDsCaptureDriverBufferImpl_Stop(PIDSCDRIVERBUFFER iface)
855 IDsCaptureDriverBufferImpl *This = (IDsCaptureDriverBufferImpl *)iface;
856 TRACE("(%p)\n",iface);
858 /* **** */
859 EnterCriticalSection(&This->pcm_crst);
860 This->play_looping = FALSE;
861 snd_pcm_drop(This->pcm);
862 snd_pcm_prepare(This->pcm);
864 if (This->notify && This->notify->notifies && This->notify->nrofnotifies)
865 Capture_CheckNotify(This->notify, 0, 0);
867 /* **** */
868 LeaveCriticalSection(&This->pcm_crst);
869 return DS_OK;
872 static const IDsCaptureDriverBufferVtbl dsdbvt =
874 IDsCaptureDriverBufferImpl_QueryInterface,
875 IDsCaptureDriverBufferImpl_AddRef,
876 IDsCaptureDriverBufferImpl_Release,
877 IDsCaptureDriverBufferImpl_Lock,
878 IDsCaptureDriverBufferImpl_Unlock,
879 IDsCaptureDriverBufferImpl_SetFormat,
880 IDsCaptureDriverBufferImpl_GetPosition,
881 IDsCaptureDriverBufferImpl_GetStatus,
882 IDsCaptureDriverBufferImpl_Start,
883 IDsCaptureDriverBufferImpl_Stop
886 static HRESULT WINAPI IDsCaptureDriverImpl_QueryInterface(PIDSCDRIVER iface, REFIID riid, LPVOID *ppobj)
888 /* IDsCaptureDriverImpl *This = (IDsCaptureDriverImpl *)iface; */
889 FIXME("(%p): stub!\n",iface);
890 return DSERR_UNSUPPORTED;
893 static ULONG WINAPI IDsCaptureDriverImpl_AddRef(PIDSCDRIVER iface)
895 IDsCaptureDriverImpl *This = (IDsCaptureDriverImpl *)iface;
896 ULONG refCount = InterlockedIncrement(&This->ref);
898 TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
900 return refCount;
903 static ULONG WINAPI IDsCaptureDriverImpl_Release(PIDSCDRIVER iface)
905 IDsCaptureDriverImpl *This = (IDsCaptureDriverImpl *)iface;
906 ULONG refCount = InterlockedDecrement(&This->ref);
908 TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
910 if (refCount)
911 return refCount;
913 HeapFree(GetProcessHeap(), 0, This);
914 return 0;
917 static HRESULT WINAPI IDsCaptureDriverImpl_GetDriverDesc(PIDSCDRIVER iface, PDSDRIVERDESC pDesc)
919 IDsCaptureDriverImpl *This = (IDsCaptureDriverImpl *)iface;
920 TRACE("(%p,%p)\n",iface,pDesc);
921 *pDesc = WInDev[This->wDevID].ds_desc;
922 pDesc->dwFlags = 0;
923 pDesc->dnDevNode = WInDev[This->wDevID].waveDesc.dnDevNode;
924 pDesc->wVxdId = 0;
925 pDesc->wReserved = 0;
926 pDesc->ulDeviceNum = This->wDevID;
927 pDesc->dwHeapType = DSDHEAP_NOHEAP;
928 pDesc->pvDirectDrawHeap = NULL;
929 pDesc->dwMemStartAddress = 0xDEAD0000;
930 pDesc->dwMemEndAddress = 0xDEAF0000;
931 pDesc->dwMemAllocExtra = 0;
932 pDesc->pvReserved1 = NULL;
933 pDesc->pvReserved2 = NULL;
934 return DS_OK;
937 static HRESULT WINAPI IDsCaptureDriverImpl_Open(PIDSCDRIVER iface)
939 HRESULT hr = S_OK;
940 IDsCaptureDriverImpl *This = (IDsCaptureDriverImpl *)iface;
941 int err=0;
942 snd_pcm_t *pcm = NULL;
943 snd_pcm_hw_params_t *hw_params;
945 /* While this is not really needed, it is a good idea to do this,
946 * to see if sound can be initialized */
948 hw_params = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, snd_pcm_hw_params_sizeof());
949 if (!hw_params)
951 hr = DSERR_OUTOFMEMORY;
952 WARN("--> %08x\n", hr);
953 return hr;
956 err = snd_pcm_open(&pcm, WInDev[This->wDevID].pcmname, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK);
957 if (err < 0) goto err;
958 err = snd_pcm_hw_params_any(pcm, hw_params);
959 if (err < 0) goto err;
960 err = snd_pcm_hw_params_set_access (pcm, hw_params, SND_PCM_ACCESS_MMAP_INTERLEAVED);
961 if (err < 0)
963 err = snd_pcm_hw_params_set_access (pcm, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
964 if (err < 0) goto err;
967 TRACE("Success\n");
968 snd_pcm_close(pcm);
969 HeapFree(GetProcessHeap(), 0, hw_params);
970 return hr;
972 err:
973 hr = DSERR_GENERIC;
974 WARN("Failed to open device: %s\n", snd_strerror(err));
975 if (pcm)
976 snd_pcm_close(pcm);
977 HeapFree(GetProcessHeap(), 0, hw_params);
978 WARN("--> %08x\n", hr);
979 return hr;
982 static HRESULT WINAPI IDsCaptureDriverImpl_Close(PIDSCDRIVER iface)
984 IDsCaptureDriverImpl *This = (IDsCaptureDriverImpl *)iface;
985 TRACE("(%p) stub, harmless\n",This);
986 return DS_OK;
989 static HRESULT WINAPI IDsCaptureDriverImpl_GetCaps(PIDSCDRIVER iface, PDSCDRIVERCAPS pCaps)
991 IDsCaptureDriverImpl *This = (IDsCaptureDriverImpl *)iface;
992 WINE_WAVEDEV *wwi = &WInDev[This->wDevID];
993 TRACE("(%p,%p)\n",iface,pCaps);
994 pCaps->dwSize = sizeof(DSCDRIVERCAPS);
995 pCaps->dwFlags = wwi->ds_caps.dwFlags;
996 pCaps->dwFormats = wwi->incaps.dwFormats;
997 pCaps->dwChannels = wwi->incaps.wChannels;
998 return DS_OK;
1001 static HRESULT WINAPI IDsCaptureDriverImpl_CreateCaptureBuffer(PIDSCDRIVER iface,
1002 LPWAVEFORMATEX pwfx,
1003 DWORD dwFlags, DWORD dwCardAddress,
1004 LPDWORD pdwcbBufferSize,
1005 LPBYTE *ppbBuffer,
1006 LPVOID *ppvObj)
1008 IDsCaptureDriverImpl *This = (IDsCaptureDriverImpl *)iface;
1009 IDsCaptureDriverBufferImpl** ippdsdb = (IDsCaptureDriverBufferImpl**)ppvObj;
1010 HRESULT err;
1012 TRACE("(%p,%p,%x,%x)\n",iface,pwfx,dwFlags,dwCardAddress);
1014 if (This->capture_buffer)
1015 return DSERR_ALLOCATED;
1017 This->capture_buffer = *ippdsdb = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDsCaptureDriverBufferImpl));
1018 if (*ippdsdb == NULL)
1019 return DSERR_OUTOFMEMORY;
1021 (*ippdsdb)->hw_params = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, snd_pcm_hw_params_sizeof());
1022 (*ippdsdb)->sw_params = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, snd_pcm_sw_params_sizeof());
1023 (*ippdsdb)->presented_buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *pdwcbBufferSize);
1024 if (!(*ippdsdb)->hw_params || !(*ippdsdb)->sw_params || !(*ippdsdb)->presented_buffer)
1026 HeapFree(GetProcessHeap(), 0, (*ippdsdb)->sw_params);
1027 HeapFree(GetProcessHeap(), 0, (*ippdsdb)->hw_params);
1028 HeapFree(GetProcessHeap(), 0, (*ippdsdb)->presented_buffer);
1029 return DSERR_OUTOFMEMORY;
1031 (*ippdsdb)->lpVtbl = &dsdbvt;
1032 (*ippdsdb)->ref = 1;
1033 (*ippdsdb)->drv = This;
1034 (*ippdsdb)->mmap_buflen_bytes = *pdwcbBufferSize;
1035 InitializeCriticalSection(&(*ippdsdb)->pcm_crst);
1036 (*ippdsdb)->pcm_crst.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": ALSA_DSCAPTURE.pcm_crst");
1038 /* SetFormat initialises pcm */
1039 err = IDsDriverBuffer_SetFormat((IDsDriverBuffer*)*ppvObj, pwfx);
1040 if (FAILED(err))
1042 WARN("Error occurred: %08x\n", err);
1043 goto err;
1045 *ppbBuffer = (*ippdsdb)->presented_buffer;
1047 /* buffer is ready to go */
1048 TRACE("buffer created at %p\n", *ippdsdb);
1049 return err;
1051 err:
1052 HeapFree(GetProcessHeap(), 0, (*ippdsdb)->presented_buffer);
1053 HeapFree(GetProcessHeap(), 0, (*ippdsdb)->sw_params);
1054 HeapFree(GetProcessHeap(), 0, (*ippdsdb)->hw_params);
1055 HeapFree(GetProcessHeap(), 0, *ippdsdb);
1056 *ippdsdb = NULL;
1057 return err;
1060 static const IDsCaptureDriverVtbl dscdvt =
1062 IDsCaptureDriverImpl_QueryInterface,
1063 IDsCaptureDriverImpl_AddRef,
1064 IDsCaptureDriverImpl_Release,
1065 IDsCaptureDriverImpl_GetDriverDesc,
1066 IDsCaptureDriverImpl_Open,
1067 IDsCaptureDriverImpl_Close,
1068 IDsCaptureDriverImpl_GetCaps,
1069 IDsCaptureDriverImpl_CreateCaptureBuffer
1072 /**************************************************************************
1073 * widDsCreate [internal]
1075 DWORD widDsCreate(UINT wDevID, PIDSCDRIVER* drv)
1077 IDsCaptureDriverImpl** idrv = (IDsCaptureDriverImpl**)drv;
1078 TRACE("(%d,%p)\n",wDevID,drv);
1080 *idrv = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(IDsCaptureDriverImpl));
1081 if (!*idrv)
1082 return MMSYSERR_NOMEM;
1083 (*idrv)->lpVtbl = &dscdvt;
1084 (*idrv)->ref = 1;
1086 (*idrv)->wDevID = wDevID;
1087 return MMSYSERR_NOERROR;
1090 /**************************************************************************
1091 * widDsDesc [internal]
1093 DWORD widDsDesc(UINT wDevID, PDSDRIVERDESC desc)
1095 *desc = WInDev[wDevID].ds_desc;
1096 return MMSYSERR_NOERROR;
1099 #endif /* HAVE_ALSA */