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>
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
64 WINE_DEFAULT_DEBUG_CHANNEL(dsalsa);
66 typedef struct IDsCaptureDriverBufferImpl IDsCaptureDriverBufferImpl;
68 typedef struct IDsCaptureDriverImpl
70 const IDsCaptureDriverVtbl *lpVtbl;
72 IDsCaptureDriverBufferImpl* capture_buffer;
74 } IDsCaptureDriverImpl;
76 typedef struct IDsCaptureDriverNotifyImpl
78 const IDsDriverNotifyVtbl *lpVtbl;
80 IDsCaptureDriverBufferImpl *buffer;
81 DSBPOSITIONNOTIFY *notifies;
82 DWORD nrofnotifies, playpos;
84 } IDsCaptureDriverNotifyImpl;
86 struct IDsCaptureDriverBufferImpl
88 const IDsCaptureDriverBufferVtbl *lpVtbl;
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;
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 */
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)
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) {
117 SetEvent(event->hEventNotify);
118 TRACE("signalled event %p (%d)\n", event->hEventNotify, i);
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;
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)
149 if (playpos < last_playpos)
151 Capture_CheckNotify(This->notify, last_playpos, This->mmap_buflen_bytes);
153 Capture_CheckNotify(This->notify, 0, playpos);
155 else Capture_CheckNotify(This->notify, last_playpos, playpos - last_playpos);
158 LeaveCriticalSection(&This->pcm_crst);
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);
174 FIXME( "Unknown IID %s\n", debugstr_guid(riid));
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);
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);
198 This->buffer->notify = NULL;
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);
211 static HRESULT WINAPI IDsCaptureDriverNotifyImpl_SetNotificationPositions(PIDSDRIVERNOTIFY iface, DWORD howmuch, LPCDSBPOSITIONNOTIFY notify)
213 DWORD len = howmuch * sizeof(DSBPOSITIONNOTIFY);
216 IDsCaptureDriverNotifyImpl *This = (IDsCaptureDriverNotifyImpl *)iface;
217 TRACE("(%p,0x%08x,%p)\n",This,howmuch,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);
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. */
234 notifies = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->notifies, len);
236 notifies = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
240 LeaveCriticalSection(&This->buffer->pcm_crst);
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);
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);
261 static const IDsDriverNotifyVtbl dscdnvt =
263 IDsCaptureDriverNotifyImpl_QueryInterface,
264 IDsCaptureDriverNotifyImpl_AddRef,
265 IDsCaptureDriverNotifyImpl_Release,
266 IDsCaptureDriverNotifyImpl_SetNotificationPositions,
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;
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);
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;
310 DWORD ofs = realpos_to_fakepos(This, fromwhere);
311 DWORD remainder = This->mmap_buflen_bytes - ofs;
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);
323 CopyDataWrap(This, This->presented_buffer + ofs, realofs, remainder, realbuflen);
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;
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;
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;
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);
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);
389 WARN("Committing data: %ld / %s (%ld)\n", ret, snd_strerror(ret), putin);
392 This->mmap_pos += 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;
401 pos = realpos_to_fakepos(This, This->mmap_pos);
402 ret = snd_pcm_readi(This->pcm, This->presented_buffer + pos, putin);
405 This->mmap_pos += 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;
423 static void CheckXRUN(IDsCaptureDriverBufferImpl* This)
425 snd_pcm_state_t state = snd_pcm_state(This->pcm);
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");
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);
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
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;
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");
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;
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);
511 err = snd_pcm_mmap_begin(pcm, &areas, &ofs, &avail);
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);
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);
538 if ( IsEqualGUID( &IID_IDsDriverNotify, riid ) ) {
541 This->notify = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDsCaptureDriverNotifyImpl));
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;
555 if ( IsEqualGUID( &IID_IDsDriverPropertySet, riid ) ) {
556 FIXME("Unsupported interface IID_IDsDriverPropertySet\n");
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);
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);
584 EnterCriticalSection(&This->pcm_crst);
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);
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);
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);
610 *ppvAudio1 = (LPBYTE)This->presented_buffer + dwWritePosition;
612 if (dwWritePosition + dwWriteLen <= This->mmap_buflen_bytes) {
614 *pdwLen1 = dwWriteLen;
621 *pdwLen1 = This->mmap_buflen_bytes - dwWritePosition;
623 *ppvAudio2 = This->presented_buffer;
625 *pdwLen2 = dwWriteLen - (This->mmap_buflen_bytes - dwWritePosition);
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);
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;
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;
661 EnterCriticalSection(&This->pcm_crst);
663 err = snd_pcm_open(&pcm, wwi->pcmname, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK);
667 if (errno != EBUSY || !This->pcm)
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);
677 err = snd_pcm_open(&pcm, wwi->pcmname, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK);
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);
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);
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; }
723 err = snd_pcm_hw_params(pcm, hw_params);
724 if (err < 0) { WARN("Could not set hw parameters\n"); goto err; }
728 snd_pcm_drop(This->pcm);
729 snd_pcm_close(This->pcm);
734 snd_pcm_prepare(This->pcm);
738 LeaveCriticalSection(&This->pcm_crst);
743 WARN("Failed to apply changes: %s\n", snd_strerror(err));
751 snd_pcm_hw_params_current(This->pcm, This->hw_params);
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);
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)
775 hw_pptr = This->mmap_pos;
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;
785 hw_pptr = This->mmap_buflen_frames - used + This->mmap_pos;
787 hw_wptr = This->mmap_pos;
790 *lpdwCappos = realpos_to_fakepos(This, hw_pptr);
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);
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);
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);
819 TRACE("State: %d, flags: 0x%08x\n", state, *lpdwStatus);
823 static HRESULT WINAPI IDsCaptureDriverBufferImpl_Start(PIDSCDRIVERBUFFER iface, DWORD dwFlags)
825 IDsCaptureDriverBufferImpl *This = (IDsCaptureDriverBufferImpl *)iface;
826 TRACE("(%p,%x)\n",iface,dwFlags);
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);
842 Capture_CheckNotify(This->notify, 0, playpos);
843 This->notify->playpos = playpos;
847 LeaveCriticalSection(&This->pcm_crst);
851 static HRESULT WINAPI IDsCaptureDriverBufferImpl_Stop(PIDSCDRIVERBUFFER iface)
853 IDsCaptureDriverBufferImpl *This = (IDsCaptureDriverBufferImpl *)iface;
854 TRACE("(%p)\n",iface);
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);
866 LeaveCriticalSection(&This->pcm_crst);
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);
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);
911 HeapFree(GetProcessHeap(), 0, This);
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;
921 pDesc->dnDevNode = WInDev[This->wDevID].waveDesc.dnDevNode;
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;
935 static HRESULT WINAPI IDsCaptureDriverImpl_Open(PIDSCDRIVER iface)
938 IDsCaptureDriverImpl *This = (IDsCaptureDriverImpl *)iface;
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());
949 hr = DSERR_OUTOFMEMORY;
950 WARN("--> %08x\n", 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);
961 err = snd_pcm_hw_params_set_access (pcm, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
962 if (err < 0) goto err;
967 HeapFree(GetProcessHeap(), 0, hw_params);
972 WARN("Failed to open device: %s\n", snd_strerror(err));
975 HeapFree(GetProcessHeap(), 0, hw_params);
976 WARN("--> %08x\n", hr);
980 static HRESULT WINAPI IDsCaptureDriverImpl_Close(PIDSCDRIVER iface)
982 IDsCaptureDriverImpl *This = (IDsCaptureDriverImpl *)iface;
983 TRACE("(%p) stub, harmless\n",This);
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;
999 static HRESULT WINAPI IDsCaptureDriverImpl_CreateCaptureBuffer(PIDSCDRIVER iface,
1000 LPWAVEFORMATEX pwfx,
1001 DWORD dwFlags, DWORD dwCardAddress,
1002 LPDWORD pdwcbBufferSize,
1006 IDsCaptureDriverImpl *This = (IDsCaptureDriverImpl *)iface;
1007 IDsCaptureDriverBufferImpl** ippdsdb = (IDsCaptureDriverBufferImpl**)ppvObj;
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);
1040 WARN("Error occurred: %08x\n", err);
1043 *ppbBuffer = (*ippdsdb)->presented_buffer;
1045 /* buffer is ready to go */
1046 TRACE("buffer created at %p\n", *ippdsdb);
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);
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));
1080 return MMSYSERR_NOMEM;
1081 (*idrv)->lpVtbl = &dscdvt;
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;