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 *======================================================================*/
27 #include "wine/port.h"
39 #ifdef HAVE_SYS_IOCTL_H
40 # include <sys/ioctl.h>
42 #ifdef HAVE_SYS_MMAN_H
43 # include <sys/mman.h>
53 #include "wine/library.h"
54 #include "wine/unicode.h"
55 #include "wine/debug.h"
59 /* Notify timer checks every 10 ms with a resolution of 2 ms */
60 #define DS_TIME_DEL 10
63 WINE_DEFAULT_DEBUG_CHANNEL(dsalsa);
65 typedef struct IDsCaptureDriverBufferImpl IDsCaptureDriverBufferImpl;
67 typedef struct IDsCaptureDriverImpl
69 const IDsCaptureDriverVtbl *lpVtbl;
71 IDsCaptureDriverBufferImpl* capture_buffer;
73 } IDsCaptureDriverImpl;
75 typedef struct IDsCaptureDriverNotifyImpl
77 const IDsDriverNotifyVtbl *lpVtbl;
79 IDsCaptureDriverBufferImpl *buffer;
80 DSBPOSITIONNOTIFY *notifies;
81 DWORD nrofnotifies, playpos;
83 } IDsCaptureDriverNotifyImpl;
85 struct IDsCaptureDriverBufferImpl
87 const IDsCaptureDriverBufferVtbl *lpVtbl;
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;
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 */
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)
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) {
116 SetEvent(event->hEventNotify);
117 TRACE("signalled event %p (%d)\n", event->hEventNotify, i);
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;
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)
148 if (playpos < last_playpos)
150 Capture_CheckNotify(This->notify, last_playpos, This->mmap_buflen_bytes);
152 Capture_CheckNotify(This->notify, 0, playpos);
154 else Capture_CheckNotify(This->notify, last_playpos, playpos - last_playpos);
157 LeaveCriticalSection(&This->pcm_crst);
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);
173 FIXME( "Unknown IID %s\n", debugstr_guid(riid));
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);
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);
197 This->buffer->notify = NULL;
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);
210 static HRESULT WINAPI IDsCaptureDriverNotifyImpl_SetNotificationPositions(PIDSDRIVERNOTIFY iface, DWORD howmuch, LPCDSBPOSITIONNOTIFY notify)
212 DWORD len = howmuch * sizeof(DSBPOSITIONNOTIFY);
215 IDsCaptureDriverNotifyImpl *This = (IDsCaptureDriverNotifyImpl *)iface;
216 TRACE("(%p,0x%08x,%p)\n",This,howmuch,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);
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. */
233 notifies = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->notifies, len);
235 notifies = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
239 LeaveCriticalSection(&This->buffer->pcm_crst);
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);
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);
260 static const IDsDriverNotifyVtbl dscdnvt =
262 IDsCaptureDriverNotifyImpl_QueryInterface,
263 IDsCaptureDriverNotifyImpl_AddRef,
264 IDsCaptureDriverNotifyImpl_Release,
265 IDsCaptureDriverNotifyImpl_SetNotificationPositions,
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;
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);
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;
309 DWORD ofs = realpos_to_fakepos(This, fromwhere);
310 DWORD remainder = This->mmap_buflen_bytes - ofs;
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);
322 CopyDataWrap(This, This->presented_buffer + ofs, realofs, remainder, realbuflen);
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;
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;
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;
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);
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);
388 WARN("Committing data: %ld / %s (%ld)\n", ret, snd_strerror(ret), putin);
391 This->mmap_pos += 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;
400 pos = realpos_to_fakepos(This, This->mmap_pos);
401 ret = snd_pcm_readi(This->pcm, This->presented_buffer + pos, putin);
404 This->mmap_pos += 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;
422 static void CheckXRUN(IDsCaptureDriverBufferImpl* This)
424 snd_pcm_state_t state = snd_pcm_state(This->pcm);
427 if ( state == SND_PCM_STATE_XRUN )
429 err = snd_pcm_prepare(This->pcm);
430 CommitAll(This, FALSE);
431 snd_pcm_start(This->pcm);
432 WARN("xrun occurred\n");
434 ERR("recovery from xrun failed, prepare failed: %s\n", snd_strerror(err));
436 else if ( state == SND_PCM_STATE_SUSPENDED )
438 int err = snd_pcm_resume(This->pcm);
439 TRACE("recovery from suspension occurred\n");
440 if (err < 0 && err != -EAGAIN){
441 err = snd_pcm_prepare(This->pcm);
443 ERR("recovery from suspend failed, prepare failed: %s\n", snd_strerror(err));
446 else if ( state != SND_PCM_STATE_RUNNING)
448 WARN("Unhandled state: %d\n", state);
453 * Allocate the memory-mapped buffer for direct sound, and set up the
456 static int CreateMMAP(IDsCaptureDriverBufferImpl* pdbi)
458 snd_pcm_t *pcm = pdbi->pcm;
459 snd_pcm_format_t format;
460 snd_pcm_uframes_t frames, ofs, avail, psize, boundary;
461 unsigned int channels, bits_per_sample, bits_per_frame;
463 const snd_pcm_channel_area_t *areas;
464 snd_pcm_hw_params_t *hw_params = pdbi->hw_params;
465 snd_pcm_sw_params_t *sw_params = pdbi->sw_params;
467 mmap_mode = snd_pcm_type(pcm);
469 if (mmap_mode == SND_PCM_TYPE_HW)
470 TRACE("mmap'd buffer is a direct hardware buffer.\n");
471 else if (mmap_mode == SND_PCM_TYPE_DMIX)
472 TRACE("mmap'd buffer is an ALSA dmix buffer\n");
474 TRACE("mmap'd buffer is an ALSA type %d buffer\n", mmap_mode);
476 err = snd_pcm_hw_params_get_period_size(hw_params, &psize, NULL);
477 err = snd_pcm_hw_params_get_format(hw_params, &format);
478 err = snd_pcm_hw_params_get_buffer_size(hw_params, &frames);
479 err = snd_pcm_hw_params_get_channels(hw_params, &channels);
480 bits_per_sample = snd_pcm_format_physical_width(format);
481 bits_per_frame = bits_per_sample * channels;
483 if (TRACE_ON(dsalsa))
484 ALSA_TraceParameters(hw_params, NULL, FALSE);
486 TRACE("format=%s frames=%ld channels=%d bits_per_sample=%d bits_per_frame=%d\n",
487 snd_pcm_format_name(format), frames, channels, bits_per_sample, bits_per_frame);
489 pdbi->mmap_buflen_frames = frames;
490 snd_pcm_sw_params_current(pcm, sw_params);
491 snd_pcm_sw_params_set_start_threshold(pcm, sw_params, 0);
492 snd_pcm_sw_params_get_boundary(sw_params, &boundary);
493 snd_pcm_sw_params_set_stop_threshold(pcm, sw_params, boundary);
494 snd_pcm_sw_params_set_silence_threshold(pcm, sw_params, INT_MAX);
495 snd_pcm_sw_params_set_silence_size(pcm, sw_params, 0);
496 snd_pcm_sw_params_set_avail_min(pcm, sw_params, 0);
497 err = snd_pcm_sw_params(pcm, sw_params);
499 pdbi->mmap_ofs_bytes = 0;
502 pdbi->mmap_buffer = NULL;
504 frames = snd_pcm_bytes_to_frames(pdbi->pcm, pdbi->mmap_buflen_bytes);
505 snd_pcm_format_set_silence(format, pdbi->presented_buffer, frames);
510 err = snd_pcm_mmap_begin(pcm, &areas, &ofs, &avail);
513 ERR("Can't map sound device for direct access: %s/%d\n", snd_strerror(err), err);
514 return DSERR_GENERIC;
516 snd_pcm_format_set_silence(format, areas->addr, pdbi->mmap_buflen_frames);
517 pdbi->mmap_pos = ofs + snd_pcm_mmap_commit(pcm, ofs, 0);
518 pdbi->mmap_buffer = areas->addr;
521 TRACE("created mmap buffer of %ld frames (%d bytes) at %p\n",
522 pdbi->mmap_buflen_frames, pdbi->mmap_buflen_bytes, pdbi->mmap_buffer);
527 static HRESULT WINAPI IDsCaptureDriverBufferImpl_QueryInterface(PIDSCDRIVERBUFFER iface, REFIID riid, LPVOID *ppobj)
529 IDsCaptureDriverBufferImpl *This = (IDsCaptureDriverBufferImpl *)iface;
530 if ( IsEqualGUID(riid, &IID_IUnknown) ||
531 IsEqualGUID(riid, &IID_IDsCaptureDriverBuffer) ) {
532 IDsCaptureDriverBuffer_AddRef(iface);
537 if ( IsEqualGUID( &IID_IDsDriverNotify, riid ) ) {
540 This->notify = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDsCaptureDriverNotifyImpl));
542 return DSERR_OUTOFMEMORY;
543 This->notify->lpVtbl = &dscdnvt;
544 This->notify->buffer = This;
546 /* Keep a lock on IDsDriverNotify for ourself, so it is destroyed when the buffer is */
547 IDsDriverNotify_AddRef((PIDSDRIVERNOTIFY)This->notify);
549 IDsDriverNotify_AddRef((PIDSDRIVERNOTIFY)This->notify);
550 *ppobj = This->notify;
554 if ( IsEqualGUID( &IID_IDsDriverPropertySet, riid ) ) {
555 FIXME("Unsupported interface IID_IDsDriverPropertySet\n");
559 FIXME("(): Unknown interface %s\n", debugstr_guid(riid));
560 return DSERR_UNSUPPORTED;
563 static ULONG WINAPI IDsCaptureDriverBufferImpl_AddRef(PIDSCDRIVERBUFFER iface)
565 IDsCaptureDriverBufferImpl *This = (IDsCaptureDriverBufferImpl *)iface;
566 ULONG refCount = InterlockedIncrement(&This->ref);
568 TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
573 static ULONG WINAPI IDsCaptureDriverBufferImpl_Release(PIDSCDRIVERBUFFER iface)
575 IDsCaptureDriverBufferImpl *This = (IDsCaptureDriverBufferImpl *)iface;
576 ULONG refCount = InterlockedDecrement(&This->ref);
578 TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
583 EnterCriticalSection(&This->pcm_crst);
585 IDsDriverNotify_Release((PIDSDRIVERNOTIFY)This->notify);
586 TRACE("mmap buffer %p destroyed\n", This->mmap_buffer);
588 This->drv->capture_buffer = NULL;
589 LeaveCriticalSection(&This->pcm_crst);
590 This->pcm_crst.DebugInfo->Spare[0] = 0;
591 DeleteCriticalSection(&This->pcm_crst);
593 snd_pcm_drop(This->pcm);
594 snd_pcm_close(This->pcm);
596 HeapFree(GetProcessHeap(), 0, This->presented_buffer);
597 HeapFree(GetProcessHeap(), 0, This->sw_params);
598 HeapFree(GetProcessHeap(), 0, This->hw_params);
599 HeapFree(GetProcessHeap(), 0, This);
603 static HRESULT WINAPI IDsCaptureDriverBufferImpl_Lock(PIDSCDRIVERBUFFER iface, LPVOID*ppvAudio1,LPDWORD pdwLen1,LPVOID*ppvAudio2,LPDWORD pdwLen2, DWORD dwWritePosition,DWORD dwWriteLen, DWORD dwFlags)
605 IDsCaptureDriverBufferImpl *This = (IDsCaptureDriverBufferImpl *)iface;
606 TRACE("(%p,%p,%p,%p,%p,%d,%d,0x%08x)\n", This, ppvAudio1, pdwLen1, ppvAudio2, pdwLen2, dwWritePosition, dwWriteLen, dwFlags);
609 *ppvAudio1 = (LPBYTE)This->presented_buffer + dwWritePosition;
611 if (dwWritePosition + dwWriteLen <= This->mmap_buflen_bytes) {
613 *pdwLen1 = dwWriteLen;
620 *pdwLen1 = This->mmap_buflen_bytes - dwWritePosition;
622 *ppvAudio2 = This->presented_buffer;
624 *pdwLen2 = dwWriteLen - (This->mmap_buflen_bytes - dwWritePosition);
629 static HRESULT WINAPI IDsCaptureDriverBufferImpl_Unlock(PIDSCDRIVERBUFFER iface, LPVOID pvAudio1,DWORD dwLen1, LPVOID pvAudio2,DWORD dwLen2)
631 IDsCaptureDriverBufferImpl *This = (IDsCaptureDriverBufferImpl *)iface;
632 TRACE("(%p,%p,%d,%p,%d)\n",This,pvAudio1,dwLen1,pvAudio2,dwLen2);
636 static HRESULT WINAPI IDsCaptureDriverBufferImpl_SetFormat(PIDSCDRIVERBUFFER iface, LPWAVEFORMATEX pwfx)
638 IDsCaptureDriverBufferImpl *This = (IDsCaptureDriverBufferImpl *)iface;
639 WINE_WAVEDEV *wwi = &WInDev[This->drv->wDevID];
640 snd_pcm_t *pcm = NULL;
641 snd_pcm_hw_params_t *hw_params = This->hw_params;
642 snd_pcm_format_t format = -1;
643 snd_pcm_uframes_t buffer_size;
644 DWORD rate = pwfx->nSamplesPerSec;
648 TRACE("(%p, %p)\n", iface, pwfx);
650 switch (pwfx->wBitsPerSample)
652 case 8: format = SND_PCM_FORMAT_U8; break;
653 case 16: format = SND_PCM_FORMAT_S16_LE; break;
654 case 24: format = SND_PCM_FORMAT_S24_3LE; break;
655 case 32: format = SND_PCM_FORMAT_S32_LE; break;
656 default: FIXME("Unsupported bpp: %d\n", pwfx->wBitsPerSample); return DSERR_GENERIC;
660 EnterCriticalSection(&This->pcm_crst);
662 err = snd_pcm_open(&pcm, wwi->pcmname, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK);
666 if (errno != EBUSY || !This->pcm)
669 LeaveCriticalSection(&This->pcm_crst);
670 WARN("Cannot open sound device: %s\n", snd_strerror(err));
671 return DSERR_GENERIC;
673 snd_pcm_drop(This->pcm);
674 snd_pcm_close(This->pcm);
676 err = snd_pcm_open(&pcm, wwi->pcmname, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK);
680 LeaveCriticalSection(&This->pcm_crst);
681 WARN("Cannot open sound device: %s\n", snd_strerror(err));
682 return DSERR_BUFFERLOST;
686 /* Set some defaults */
687 snd_pcm_hw_params_any(pcm, hw_params);
689 err = snd_pcm_hw_params_set_channels(pcm, hw_params, pwfx->nChannels);
690 if (err < 0) { WARN("Could not set channels to %d\n", pwfx->nChannels); goto err; }
692 err = snd_pcm_hw_params_set_format(pcm, hw_params, format);
693 if (err < 0) { WARN("Could not set format to %d bpp\n", pwfx->wBitsPerSample); goto err; }
695 err = snd_pcm_hw_params_set_rate_near(pcm, hw_params, &rate, NULL);
696 if (err < 0) { rate = pwfx->nSamplesPerSec; WARN("Could not set rate\n"); goto err; }
698 if (!ALSA_NearMatch(rate, pwfx->nSamplesPerSec))
700 WARN("Could not set sound rate to %d, but instead to %d\n", pwfx->nSamplesPerSec, rate);
701 pwfx->nSamplesPerSec = rate;
702 pwfx->nAvgBytesPerSec = rate * pwfx->nBlockAlign;
703 /* Let DirectSound detect this */
706 snd_pcm_hw_params_set_periods_integer(pcm, hw_params);
707 buffer_size = This->mmap_buflen_bytes / pwfx->nBlockAlign;
708 snd_pcm_hw_params_set_buffer_size_near(pcm, hw_params, &buffer_size);
710 snd_pcm_hw_params_set_period_time_near(pcm, hw_params, (unsigned int*)&buffer_size, NULL);
712 err = snd_pcm_hw_params_set_access (pcm, hw_params, SND_PCM_ACCESS_MMAP_INTERLEAVED);
715 err = snd_pcm_hw_params_set_access (pcm, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
716 if (err < 0) { WARN("Could not set access\n"); goto err; }
722 err = snd_pcm_hw_params(pcm, hw_params);
723 if (err < 0) { WARN("Could not set hw parameters\n"); goto err; }
727 snd_pcm_drop(This->pcm);
728 snd_pcm_close(This->pcm);
733 snd_pcm_prepare(This->pcm);
737 LeaveCriticalSection(&This->pcm_crst);
742 WARN("Failed to apply changes: %s\n", snd_strerror(err));
750 snd_pcm_hw_params_current(This->pcm, This->hw_params);
753 LeaveCriticalSection(&This->pcm_crst);
754 return DSERR_BADFORMAT;
757 static HRESULT WINAPI IDsCaptureDriverBufferImpl_GetPosition(PIDSCDRIVERBUFFER iface, LPDWORD lpdwCappos, LPDWORD lpdwReadpos)
759 IDsCaptureDriverBufferImpl *This = (IDsCaptureDriverBufferImpl *)iface;
760 snd_pcm_uframes_t hw_pptr, hw_wptr;
762 EnterCriticalSection(&This->pcm_crst);
766 FIXME("Bad pointer for pcm: %p\n", This->pcm);
767 LeaveCriticalSection(&This->pcm_crst);
768 return DSERR_GENERIC;
771 if (snd_pcm_state(This->pcm) != SND_PCM_STATE_RUNNING)
774 hw_pptr = This->mmap_pos;
778 /* FIXME: Unused at the moment */
779 snd_pcm_uframes_t used = CommitAll(This, FALSE);
781 if (This->mmap_pos > used)
782 hw_pptr = This->mmap_pos - used;
784 hw_pptr = This->mmap_buflen_frames - used + This->mmap_pos;
786 hw_wptr = This->mmap_pos;
789 *lpdwCappos = realpos_to_fakepos(This, hw_pptr);
791 *lpdwReadpos = realpos_to_fakepos(This, hw_wptr);
793 LeaveCriticalSection(&This->pcm_crst);
795 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 static HRESULT WINAPI IDsCaptureDriverBufferImpl_GetStatus(PIDSCDRIVERBUFFER iface, LPDWORD lpdwStatus)
801 IDsCaptureDriverBufferImpl *This = (IDsCaptureDriverBufferImpl *)iface;
802 snd_pcm_state_t state;
803 TRACE("(%p,%p)\n",iface,lpdwStatus);
805 state = snd_pcm_state(This->pcm);
808 case SND_PCM_STATE_XRUN:
809 case SND_PCM_STATE_SUSPENDED:
810 case SND_PCM_STATE_RUNNING:
811 *lpdwStatus = DSCBSTATUS_CAPTURING | (This->play_looping ? DSCBSTATUS_LOOPING : 0);
818 TRACE("State: %d, flags: 0x%08x\n", state, *lpdwStatus);
822 static HRESULT WINAPI IDsCaptureDriverBufferImpl_Start(PIDSCDRIVERBUFFER iface, DWORD dwFlags)
824 IDsCaptureDriverBufferImpl *This = (IDsCaptureDriverBufferImpl *)iface;
825 TRACE("(%p,%x)\n",iface,dwFlags);
828 EnterCriticalSection(&This->pcm_crst);
829 snd_pcm_start(This->pcm);
830 This->play_looping = !!(dwFlags & DSCBSTART_LOOPING);
831 if (!This->play_looping)
832 /* Not well supported because of the difference in ALSA size and DSOUND's notion of size
833 * what it does right now is fill the buffer once.. ALSA size */
834 FIXME("Non-looping buffers are not properly supported!\n");
835 CommitAll(This, TRUE);
837 if (This->notify && This->notify->nrofnotifies && This->notify->notifies)
839 DWORD playpos = realpos_to_fakepos(This, This->mmap_pos);
841 Capture_CheckNotify(This->notify, 0, playpos);
842 This->notify->playpos = playpos;
846 LeaveCriticalSection(&This->pcm_crst);
850 static HRESULT WINAPI IDsCaptureDriverBufferImpl_Stop(PIDSCDRIVERBUFFER iface)
852 IDsCaptureDriverBufferImpl *This = (IDsCaptureDriverBufferImpl *)iface;
853 TRACE("(%p)\n",iface);
856 EnterCriticalSection(&This->pcm_crst);
857 This->play_looping = FALSE;
858 snd_pcm_drop(This->pcm);
859 snd_pcm_prepare(This->pcm);
861 if (This->notify && This->notify->notifies && This->notify->nrofnotifies)
862 Capture_CheckNotify(This->notify, 0, 0);
865 LeaveCriticalSection(&This->pcm_crst);
869 static const IDsCaptureDriverBufferVtbl dsdbvt =
871 IDsCaptureDriverBufferImpl_QueryInterface,
872 IDsCaptureDriverBufferImpl_AddRef,
873 IDsCaptureDriverBufferImpl_Release,
874 IDsCaptureDriverBufferImpl_Lock,
875 IDsCaptureDriverBufferImpl_Unlock,
876 IDsCaptureDriverBufferImpl_SetFormat,
877 IDsCaptureDriverBufferImpl_GetPosition,
878 IDsCaptureDriverBufferImpl_GetStatus,
879 IDsCaptureDriverBufferImpl_Start,
880 IDsCaptureDriverBufferImpl_Stop
883 static HRESULT WINAPI IDsCaptureDriverImpl_QueryInterface(PIDSCDRIVER iface, REFIID riid, LPVOID *ppobj)
885 /* IDsCaptureDriverImpl *This = (IDsCaptureDriverImpl *)iface; */
886 FIXME("(%p): stub!\n",iface);
887 return DSERR_UNSUPPORTED;
890 static ULONG WINAPI IDsCaptureDriverImpl_AddRef(PIDSCDRIVER iface)
892 IDsCaptureDriverImpl *This = (IDsCaptureDriverImpl *)iface;
893 ULONG refCount = InterlockedIncrement(&This->ref);
895 TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
900 static ULONG WINAPI IDsCaptureDriverImpl_Release(PIDSCDRIVER iface)
902 IDsCaptureDriverImpl *This = (IDsCaptureDriverImpl *)iface;
903 ULONG refCount = InterlockedDecrement(&This->ref);
905 TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
910 HeapFree(GetProcessHeap(), 0, This);
914 static HRESULT WINAPI IDsCaptureDriverImpl_GetDriverDesc(PIDSCDRIVER iface, PDSDRIVERDESC pDesc)
916 IDsCaptureDriverImpl *This = (IDsCaptureDriverImpl *)iface;
917 TRACE("(%p,%p)\n",iface,pDesc);
918 *pDesc = WInDev[This->wDevID].ds_desc;
920 pDesc->dnDevNode = WInDev[This->wDevID].waveDesc.dnDevNode;
922 pDesc->wReserved = 0;
923 pDesc->ulDeviceNum = This->wDevID;
924 pDesc->dwHeapType = DSDHEAP_NOHEAP;
925 pDesc->pvDirectDrawHeap = NULL;
926 pDesc->dwMemStartAddress = 0xDEAD0000;
927 pDesc->dwMemEndAddress = 0xDEAF0000;
928 pDesc->dwMemAllocExtra = 0;
929 pDesc->pvReserved1 = NULL;
930 pDesc->pvReserved2 = NULL;
934 static HRESULT WINAPI IDsCaptureDriverImpl_Open(PIDSCDRIVER iface)
937 IDsCaptureDriverImpl *This = (IDsCaptureDriverImpl *)iface;
939 snd_pcm_t *pcm = NULL;
940 snd_pcm_hw_params_t *hw_params;
942 /* While this is not really needed, it is a good idea to do this,
943 * to see if sound can be initialized */
945 hw_params = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, snd_pcm_hw_params_sizeof());
948 hr = DSERR_OUTOFMEMORY;
949 WARN("--> %08x\n", hr);
953 err = snd_pcm_open(&pcm, WInDev[This->wDevID].pcmname, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK);
954 if (err < 0) goto err;
955 err = snd_pcm_hw_params_any(pcm, hw_params);
956 if (err < 0) goto err;
957 err = snd_pcm_hw_params_set_access (pcm, hw_params, SND_PCM_ACCESS_MMAP_INTERLEAVED);
960 err = snd_pcm_hw_params_set_access (pcm, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
961 if (err < 0) goto err;
966 HeapFree(GetProcessHeap(), 0, hw_params);
971 WARN("Failed to open device: %s\n", snd_strerror(err));
974 HeapFree(GetProcessHeap(), 0, hw_params);
975 WARN("--> %08x\n", hr);
979 static HRESULT WINAPI IDsCaptureDriverImpl_Close(PIDSCDRIVER iface)
981 IDsCaptureDriverImpl *This = (IDsCaptureDriverImpl *)iface;
982 TRACE("(%p) stub, harmless\n",This);
986 static HRESULT WINAPI IDsCaptureDriverImpl_GetCaps(PIDSCDRIVER iface, PDSCDRIVERCAPS pCaps)
988 IDsCaptureDriverImpl *This = (IDsCaptureDriverImpl *)iface;
989 WINE_WAVEDEV *wwi = &WInDev[This->wDevID];
990 TRACE("(%p,%p)\n",iface,pCaps);
991 pCaps->dwSize = sizeof(DSCDRIVERCAPS);
992 pCaps->dwFlags = wwi->ds_caps.dwFlags;
993 pCaps->dwFormats = wwi->incaps.dwFormats;
994 pCaps->dwChannels = wwi->incaps.wChannels;
998 static HRESULT WINAPI IDsCaptureDriverImpl_CreateCaptureBuffer(PIDSCDRIVER iface,
1000 DWORD dwFlags, DWORD dwCardAddress,
1001 LPDWORD pdwcbBufferSize,
1005 IDsCaptureDriverImpl *This = (IDsCaptureDriverImpl *)iface;
1006 IDsCaptureDriverBufferImpl** ippdsdb = (IDsCaptureDriverBufferImpl**)ppvObj;
1009 TRACE("(%p,%p,%x,%x)\n",iface,pwfx,dwFlags,dwCardAddress);
1011 if (This->capture_buffer)
1012 return DSERR_ALLOCATED;
1014 This->capture_buffer = *ippdsdb = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDsCaptureDriverBufferImpl));
1015 if (*ippdsdb == NULL)
1016 return DSERR_OUTOFMEMORY;
1018 (*ippdsdb)->hw_params = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, snd_pcm_hw_params_sizeof());
1019 (*ippdsdb)->sw_params = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, snd_pcm_sw_params_sizeof());
1020 (*ippdsdb)->presented_buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *pdwcbBufferSize);
1021 if (!(*ippdsdb)->hw_params || !(*ippdsdb)->sw_params || !(*ippdsdb)->presented_buffer)
1023 HeapFree(GetProcessHeap(), 0, (*ippdsdb)->sw_params);
1024 HeapFree(GetProcessHeap(), 0, (*ippdsdb)->hw_params);
1025 HeapFree(GetProcessHeap(), 0, (*ippdsdb)->presented_buffer);
1026 return DSERR_OUTOFMEMORY;
1028 (*ippdsdb)->lpVtbl = &dsdbvt;
1029 (*ippdsdb)->ref = 1;
1030 (*ippdsdb)->drv = This;
1031 (*ippdsdb)->mmap_buflen_bytes = *pdwcbBufferSize;
1032 InitializeCriticalSection(&(*ippdsdb)->pcm_crst);
1033 (*ippdsdb)->pcm_crst.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": ALSA_DSCAPTURE.pcm_crst");
1035 /* SetFormat initialises pcm */
1036 err = IDsDriverBuffer_SetFormat((IDsDriverBuffer*)*ppvObj, pwfx);
1039 WARN("Error occurred: %08x\n", err);
1042 *ppbBuffer = (*ippdsdb)->presented_buffer;
1044 /* buffer is ready to go */
1045 TRACE("buffer created at %p\n", *ippdsdb);
1049 HeapFree(GetProcessHeap(), 0, (*ippdsdb)->presented_buffer);
1050 HeapFree(GetProcessHeap(), 0, (*ippdsdb)->sw_params);
1051 HeapFree(GetProcessHeap(), 0, (*ippdsdb)->hw_params);
1052 HeapFree(GetProcessHeap(), 0, *ippdsdb);
1057 static const IDsCaptureDriverVtbl dscdvt =
1059 IDsCaptureDriverImpl_QueryInterface,
1060 IDsCaptureDriverImpl_AddRef,
1061 IDsCaptureDriverImpl_Release,
1062 IDsCaptureDriverImpl_GetDriverDesc,
1063 IDsCaptureDriverImpl_Open,
1064 IDsCaptureDriverImpl_Close,
1065 IDsCaptureDriverImpl_GetCaps,
1066 IDsCaptureDriverImpl_CreateCaptureBuffer
1069 /**************************************************************************
1070 * widDsCreate [internal]
1072 DWORD widDsCreate(UINT wDevID, PIDSCDRIVER* drv)
1074 IDsCaptureDriverImpl** idrv = (IDsCaptureDriverImpl**)drv;
1075 TRACE("(%d,%p)\n",wDevID,drv);
1077 *idrv = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(IDsCaptureDriverImpl));
1079 return MMSYSERR_NOMEM;
1080 (*idrv)->lpVtbl = &dscdvt;
1083 (*idrv)->wDevID = wDevID;
1084 return MMSYSERR_NOERROR;
1087 /**************************************************************************
1088 * widDsDesc [internal]
1090 DWORD widDsDesc(UINT wDevID, PDSDRIVERDESC desc)
1092 *desc = WInDev[wDevID].ds_desc;
1093 return MMSYSERR_NOERROR;
1096 #endif /* HAVE_ALSA */