winealsa: Fix return value checking in wavein.
[wine/wine64.git] / dlls / winealsa.drv / wavein.c
blob6d1cf246c27df0002e0feca46da1bcf1374611ba
1 /*
2 * Sample Wine Driver for Advanced Linux Sound System (ALSA)
3 * Based on version <final> of the ALSA API
5 * Copyright 2002 Eric Pouech
6 * 2002 Marco Pietrobono
7 * 2003 Christian Costa : WaveIn support
8 * 2006-2007 Maarten Lankhorst
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 /*======================================================================*
26 * Low level WAVE IN implementation *
27 *======================================================================*/
29 #include "config.h"
30 #include "wine/port.h"
32 #include <stdlib.h>
33 #include <stdarg.h>
34 #include <stdio.h>
35 #include <string.h>
36 #ifdef HAVE_UNISTD_H
37 # include <unistd.h>
38 #endif
39 #include <errno.h>
40 #include <limits.h>
41 #include <fcntl.h>
42 #ifdef HAVE_SYS_IOCTL_H
43 # include <sys/ioctl.h>
44 #endif
45 #ifdef HAVE_SYS_MMAN_H
46 # include <sys/mman.h>
47 #endif
48 #include "windef.h"
49 #include "winbase.h"
50 #include "wingdi.h"
51 #include "winuser.h"
52 #include "winnls.h"
53 #include "mmddk.h"
55 #include "alsa.h"
56 #include "wine/library.h"
57 #include "wine/unicode.h"
58 #include "wine/debug.h"
60 WINE_DEFAULT_DEBUG_CHANNEL(wave);
62 #ifdef HAVE_ALSA
64 WINE_WAVEDEV *WInDev;
65 DWORD ALSA_WidNumMallocedDevs;
66 DWORD ALSA_WidNumDevs;
68 /**************************************************************************
69 * widNotifyClient [internal]
71 static DWORD widNotifyClient(WINE_WAVEDEV* wwi, WORD wMsg, DWORD dwParam1, DWORD dwParam2)
73 TRACE("wMsg = 0x%04x dwParm1 = %04X dwParam2 = %04X\n", wMsg, dwParam1, dwParam2);
75 switch (wMsg) {
76 case WIM_OPEN:
77 case WIM_CLOSE:
78 case WIM_DATA:
79 if (wwi->wFlags != DCB_NULL &&
80 !DriverCallback(wwi->waveDesc.dwCallback, wwi->wFlags, (HDRVR)wwi->waveDesc.hWave,
81 wMsg, wwi->waveDesc.dwInstance, dwParam1, dwParam2)) {
82 WARN("can't notify client !\n");
83 return MMSYSERR_ERROR;
85 break;
86 default:
87 FIXME("Unknown callback message %u\n", wMsg);
88 return MMSYSERR_INVALPARAM;
90 return MMSYSERR_NOERROR;
93 /**************************************************************************
94 * widGetDevCaps [internal]
96 static DWORD widGetDevCaps(WORD wDevID, LPWAVEINCAPSW lpCaps, DWORD dwSize)
98 TRACE("(%u, %p, %u);\n", wDevID, lpCaps, dwSize);
100 if (lpCaps == NULL) return MMSYSERR_NOTENABLED;
102 if (wDevID >= ALSA_WidNumDevs) {
103 TRACE("Requested device %d, but only %d are known!\n", wDevID, ALSA_WidNumDevs);
104 return MMSYSERR_BADDEVICEID;
107 memcpy(lpCaps, &WInDev[wDevID].incaps, min(dwSize, sizeof(*lpCaps)));
108 return MMSYSERR_NOERROR;
111 /**************************************************************************
112 * widRecorder_ReadHeaders [internal]
114 static void widRecorder_ReadHeaders(WINE_WAVEDEV * wwi)
116 enum win_wm_message tmp_msg;
117 DWORD tmp_param;
118 HANDLE tmp_ev;
119 WAVEHDR* lpWaveHdr;
121 while (ALSA_RetrieveRingMessage(&wwi->msgRing, &tmp_msg, &tmp_param, &tmp_ev)) {
122 if (tmp_msg == WINE_WM_HEADER) {
123 LPWAVEHDR* wh;
124 lpWaveHdr = (LPWAVEHDR)tmp_param;
125 lpWaveHdr->lpNext = 0;
127 if (wwi->lpQueuePtr == 0)
128 wwi->lpQueuePtr = lpWaveHdr;
129 else {
130 for (wh = &(wwi->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
131 *wh = lpWaveHdr;
133 } else {
134 ERR("should only have headers left\n");
139 /**************************************************************************
140 * widRecorder [internal]
142 static DWORD CALLBACK widRecorder(LPVOID pmt)
144 WORD uDevID = (DWORD)pmt;
145 WINE_WAVEDEV* wwi = (WINE_WAVEDEV*)&WInDev[uDevID];
146 WAVEHDR* lpWaveHdr;
147 DWORD dwSleepTime;
148 DWORD bytesRead;
149 LPVOID buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, wwi->dwPeriodSize);
150 char *pOffset = buffer;
151 enum win_wm_message msg;
152 DWORD param;
153 HANDLE ev;
154 DWORD frames_per_period;
156 wwi->state = WINE_WS_STOPPED;
157 InterlockedExchange((LONG*)&wwi->dwTotalRecorded, 0);
158 wwi->lpQueuePtr = NULL;
160 SetEvent(wwi->hStartUpEvent);
162 /* make sleep time to be # of ms to output a period */
163 dwSleepTime = (wwi->dwPeriodSize * 1000) / wwi->format.Format.nAvgBytesPerSec;
164 frames_per_period = snd_pcm_bytes_to_frames(wwi->pcm, wwi->dwPeriodSize);
165 TRACE("sleeptime=%d ms, total buffer length=%d ms (%d bytes)\n", dwSleepTime, wwi->dwBufferSize * 1000 / wwi->format.Format.nAvgBytesPerSec, wwi->dwBufferSize);
167 for (;;) {
168 /* wait for dwSleepTime or an event in thread's queue */
169 /* FIXME: could improve wait time depending on queue state,
170 * ie, number of queued fragments
172 if (wwi->lpQueuePtr != NULL && wwi->state == WINE_WS_PLAYING)
174 int periods;
175 DWORD frames;
176 DWORD bytes;
177 DWORD read;
179 lpWaveHdr = wwi->lpQueuePtr;
180 /* read all the fragments accumulated so far */
181 frames = snd_pcm_avail_update(wwi->pcm);
182 bytes = snd_pcm_frames_to_bytes(wwi->pcm, frames);
184 TRACE("frames = %d bytes = %d\n", frames, bytes);
185 periods = bytes / wwi->dwPeriodSize;
186 while ((periods > 0) && (wwi->lpQueuePtr))
188 periods--;
189 bytes = wwi->dwPeriodSize;
190 TRACE("bytes = %d\n",bytes);
191 if (lpWaveHdr->dwBufferLength - lpWaveHdr->dwBytesRecorded >= wwi->dwPeriodSize)
193 /* directly read fragment in wavehdr */
194 read = wwi->read(wwi->pcm, lpWaveHdr->lpData + lpWaveHdr->dwBytesRecorded, frames_per_period);
195 bytesRead = snd_pcm_frames_to_bytes(wwi->pcm, read);
197 TRACE("bytesRead=%d (direct)\n", bytesRead);
198 if (read != (DWORD) -1)
200 /* update number of bytes recorded in current buffer and by this device */
201 lpWaveHdr->dwBytesRecorded += bytesRead;
202 InterlockedExchangeAdd((LONG*)&wwi->dwTotalRecorded, bytesRead);
204 /* buffer is full. notify client */
205 if (lpWaveHdr->dwBytesRecorded == lpWaveHdr->dwBufferLength)
207 /* must copy the value of next waveHdr, because we have no idea of what
208 * will be done with the content of lpWaveHdr in callback
210 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
212 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
213 lpWaveHdr->dwFlags |= WHDR_DONE;
215 wwi->lpQueuePtr = lpNext;
216 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
217 lpWaveHdr = lpNext;
219 } else {
220 FIXME("read(%s, %p, %d) failed (%s)\n", wwi->pcmname,
221 lpWaveHdr->lpData + lpWaveHdr->dwBytesRecorded,
222 frames_per_period, snd_strerror(read));
225 else
227 /* read the fragment in a local buffer */
228 read = wwi->read(wwi->pcm, buffer, frames_per_period);
229 bytesRead = snd_pcm_frames_to_bytes(wwi->pcm, read);
230 pOffset = buffer;
232 TRACE("bytesRead=%d (local)\n", bytesRead);
234 if (read == (DWORD) -1) {
235 TRACE("read(%s, %p, %d) failed (%s)\n", wwi->pcmname,
236 buffer, frames_per_period, strerror(errno));
237 continue;
240 /* copy data in client buffers */
241 while (read != (DWORD) -1 && bytesRead > 0)
243 DWORD dwToCopy = min (bytesRead, lpWaveHdr->dwBufferLength - lpWaveHdr->dwBytesRecorded);
245 memcpy(lpWaveHdr->lpData + lpWaveHdr->dwBytesRecorded,
246 pOffset,
247 dwToCopy);
249 /* update number of bytes recorded in current buffer and by this device */
250 lpWaveHdr->dwBytesRecorded += dwToCopy;
251 InterlockedExchangeAdd((LONG*)&wwi->dwTotalRecorded, dwToCopy);
252 bytesRead -= dwToCopy;
253 pOffset += dwToCopy;
255 /* client buffer is full. notify client */
256 if (lpWaveHdr->dwBytesRecorded == lpWaveHdr->dwBufferLength)
258 /* must copy the value of next waveHdr, because we have no idea of what
259 * will be done with the content of lpWaveHdr in callback
261 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
262 TRACE("lpNext=%p\n", lpNext);
264 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
265 lpWaveHdr->dwFlags |= WHDR_DONE;
267 wwi->lpQueuePtr = lpNext;
268 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
270 lpWaveHdr = lpNext;
271 if (!lpNext && bytesRead) {
272 /* before we give up, check for more header messages */
273 while (ALSA_PeekRingMessage(&wwi->msgRing, &msg, &param, &ev))
275 if (msg == WINE_WM_HEADER) {
276 LPWAVEHDR hdr;
277 ALSA_RetrieveRingMessage(&wwi->msgRing, &msg, &param, &ev);
278 hdr = ((LPWAVEHDR)param);
279 TRACE("msg = %s, hdr = %p, ev = %p\n", ALSA_getCmdString(msg), hdr, ev);
280 hdr->lpNext = 0;
281 if (lpWaveHdr == 0) {
282 /* new head of queue */
283 wwi->lpQueuePtr = lpWaveHdr = hdr;
284 } else {
285 /* insert buffer at the end of queue */
286 LPWAVEHDR* wh;
287 for (wh = &(wwi->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
288 *wh = hdr;
290 } else
291 break;
294 if (lpWaveHdr == 0) {
295 /* no more buffer to copy data to, but we did read more.
296 * what hasn't been copied will be dropped
298 WARN("buffer under run! %u bytes dropped.\n", bytesRead);
299 wwi->lpQueuePtr = NULL;
300 break;
309 ALSA_WaitRingMessage(&wwi->msgRing, dwSleepTime);
311 while (ALSA_RetrieveRingMessage(&wwi->msgRing, &msg, &param, &ev))
313 TRACE("msg=%s param=0x%x\n", ALSA_getCmdString(msg), param);
314 switch (msg) {
315 case WINE_WM_PAUSING:
316 wwi->state = WINE_WS_PAUSED;
317 /*FIXME("Device should stop recording\n");*/
318 SetEvent(ev);
319 break;
320 case WINE_WM_STARTING:
321 wwi->state = WINE_WS_PLAYING;
322 snd_pcm_start(wwi->pcm);
323 SetEvent(ev);
324 break;
325 case WINE_WM_HEADER:
326 lpWaveHdr = (LPWAVEHDR)param;
327 lpWaveHdr->lpNext = 0;
329 /* insert buffer at the end of queue */
331 LPWAVEHDR* wh;
332 for (wh = &(wwi->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
333 *wh = lpWaveHdr;
335 break;
336 case WINE_WM_STOPPING:
337 if (wwi->state != WINE_WS_STOPPED)
339 snd_pcm_drain(wwi->pcm);
341 /* read any headers in queue */
342 widRecorder_ReadHeaders(wwi);
344 /* return current buffer to app */
345 lpWaveHdr = wwi->lpQueuePtr;
346 if (lpWaveHdr)
348 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
349 TRACE("stop %p %p\n", lpWaveHdr, lpWaveHdr->lpNext);
350 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
351 lpWaveHdr->dwFlags |= WHDR_DONE;
352 wwi->lpQueuePtr = lpNext;
353 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
356 wwi->state = WINE_WS_STOPPED;
357 SetEvent(ev);
358 break;
359 case WINE_WM_RESETTING:
360 if (wwi->state != WINE_WS_STOPPED)
362 snd_pcm_drain(wwi->pcm);
364 wwi->state = WINE_WS_STOPPED;
365 wwi->dwTotalRecorded = 0;
367 /* read any headers in queue */
368 widRecorder_ReadHeaders(wwi);
370 /* return all buffers to the app */
371 for (lpWaveHdr = wwi->lpQueuePtr; lpWaveHdr; lpWaveHdr = lpWaveHdr->lpNext) {
372 TRACE("reset %p %p\n", lpWaveHdr, lpWaveHdr->lpNext);
373 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
374 lpWaveHdr->dwFlags |= WHDR_DONE;
375 wwi->lpQueuePtr = lpWaveHdr->lpNext;
376 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
379 wwi->lpQueuePtr = NULL;
380 SetEvent(ev);
381 break;
382 case WINE_WM_CLOSING:
383 wwi->hThread = 0;
384 wwi->state = WINE_WS_CLOSED;
385 SetEvent(ev);
386 HeapFree(GetProcessHeap(), 0, buffer);
387 ExitThread(0);
388 /* shouldn't go here */
389 default:
390 FIXME("unknown message %d\n", msg);
391 break;
395 ExitThread(0);
396 /* just for not generating compilation warnings... should never be executed */
397 return 0;
400 /**************************************************************************
401 * widOpen [internal]
403 static DWORD widOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
405 WINE_WAVEDEV* wwi;
406 snd_pcm_hw_params_t * hw_params;
407 snd_pcm_sw_params_t * sw_params;
408 snd_pcm_access_t access;
409 snd_pcm_format_t format;
410 unsigned int rate;
411 unsigned int buffer_time = 500000;
412 unsigned int period_time = 10000;
413 snd_pcm_uframes_t buffer_size;
414 snd_pcm_uframes_t period_size;
415 int flags;
416 snd_pcm_t * pcm;
417 int err;
418 int dir;
419 DWORD ret;
421 /* JPW TODO - review this code */
422 TRACE("(%u, %p, %08X);\n", wDevID, lpDesc, dwFlags);
423 if (lpDesc == NULL) {
424 WARN("Invalid Parameter !\n");
425 return MMSYSERR_INVALPARAM;
427 if (wDevID >= ALSA_WidNumDevs) {
428 TRACE("Requested device %d, but only %d are known!\n", wDevID, ALSA_WidNumDevs);
429 return MMSYSERR_BADDEVICEID;
432 /* only PCM format is supported so far... */
433 if (!ALSA_supportedFormat(lpDesc->lpFormat)) {
434 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
435 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
436 lpDesc->lpFormat->nSamplesPerSec);
437 return WAVERR_BADFORMAT;
440 if (dwFlags & WAVE_FORMAT_QUERY) {
441 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
442 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
443 lpDesc->lpFormat->nSamplesPerSec);
444 return MMSYSERR_NOERROR;
447 wwi = &WInDev[wDevID];
449 if (wwi->pcm != NULL) {
450 WARN("already allocated\n");
451 return MMSYSERR_ALLOCATED;
454 wwi->pcm = 0;
455 flags = SND_PCM_NONBLOCK;
457 if ( (err=snd_pcm_open(&pcm, wwi->pcmname, SND_PCM_STREAM_CAPTURE, flags)) < 0 )
459 ERR("Error open: %s\n", snd_strerror(err));
460 return MMSYSERR_NOTENABLED;
463 wwi->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
465 memcpy(&wwi->waveDesc, lpDesc, sizeof(WAVEOPENDESC));
466 ALSA_copyFormat(lpDesc->lpFormat, &wwi->format);
468 if (wwi->format.Format.wBitsPerSample == 0) {
469 WARN("Resetting zeroed wBitsPerSample\n");
470 wwi->format.Format.wBitsPerSample = 8 *
471 (wwi->format.Format.nAvgBytesPerSec /
472 wwi->format.Format.nSamplesPerSec) /
473 wwi->format.Format.nChannels;
476 hw_params = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, snd_pcm_hw_params_sizeof() );
477 sw_params = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, snd_pcm_sw_params_sizeof() );
479 snd_pcm_hw_params_any(pcm, hw_params);
481 #define EXIT_ON_ERROR(f,e,txt) do \
483 int err; \
484 if ( (err = (f) ) < 0) \
486 WARN(txt ": %s\n", snd_strerror(err)); \
487 ret = (e); \
488 goto error; \
490 } while(0)
492 access = SND_PCM_ACCESS_MMAP_INTERLEAVED;
493 if ( ( err = snd_pcm_hw_params_set_access(pcm, hw_params, access ) ) < 0) {
494 WARN("mmap not available. switching to standard write.\n");
495 access = SND_PCM_ACCESS_RW_INTERLEAVED;
496 EXIT_ON_ERROR( snd_pcm_hw_params_set_access(pcm, hw_params, access ), MMSYSERR_INVALPARAM, "unable to set access for playback");
497 wwi->read = snd_pcm_readi;
499 else
500 wwi->read = snd_pcm_mmap_readi;
502 EXIT_ON_ERROR( snd_pcm_hw_params_set_channels(pcm, hw_params, wwi->format.Format.nChannels), WAVERR_BADFORMAT, "unable to set required channels");
504 if ((wwi->format.Format.wFormatTag == WAVE_FORMAT_PCM) ||
505 ((wwi->format.Format.wFormatTag == WAVE_FORMAT_EXTENSIBLE) &&
506 IsEqualGUID(&wwi->format.SubFormat, &KSDATAFORMAT_SUBTYPE_PCM))) {
507 format = (wwi->format.Format.wBitsPerSample == 8) ? SND_PCM_FORMAT_U8 :
508 (wwi->format.Format.wBitsPerSample == 16) ? SND_PCM_FORMAT_S16_LE :
509 (wwi->format.Format.wBitsPerSample == 24) ? SND_PCM_FORMAT_S24_3LE :
510 (wwi->format.Format.wBitsPerSample == 32) ? SND_PCM_FORMAT_S32_LE : -1;
511 } else if ((wwi->format.Format.wFormatTag == WAVE_FORMAT_EXTENSIBLE) &&
512 IsEqualGUID(&wwi->format.SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT)){
513 format = (wwi->format.Format.wBitsPerSample == 32) ? SND_PCM_FORMAT_FLOAT_LE : -1;
514 } else if (wwi->format.Format.wFormatTag == WAVE_FORMAT_MULAW) {
515 FIXME("unimplemented format: WAVE_FORMAT_MULAW\n");
516 ret = WAVERR_BADFORMAT;
517 goto error;
518 } else if (wwi->format.Format.wFormatTag == WAVE_FORMAT_ALAW) {
519 FIXME("unimplemented format: WAVE_FORMAT_ALAW\n");
520 ret = WAVERR_BADFORMAT;
521 goto error;
522 } else if (wwi->format.Format.wFormatTag == WAVE_FORMAT_ADPCM) {
523 FIXME("unimplemented format: WAVE_FORMAT_ADPCM\n");
524 ret = WAVERR_BADFORMAT;
525 goto error;
526 } else {
527 ERR("invalid format: %0x04x\n", wwi->format.Format.wFormatTag);
528 ret = WAVERR_BADFORMAT;
529 goto error;
532 EXIT_ON_ERROR( snd_pcm_hw_params_set_format(pcm, hw_params, format), WAVERR_BADFORMAT, "unable to set required format");
534 rate = wwi->format.Format.nSamplesPerSec;
535 dir = 0;
536 err = snd_pcm_hw_params_set_rate_near(pcm, hw_params, &rate, &dir);
537 if (err < 0) {
538 WARN("Rate %d Hz not available for playback: %s\n", wwi->format.Format.nSamplesPerSec, snd_strerror(rate));
539 ret = WAVERR_BADFORMAT;
540 goto error;
542 if (!ALSA_NearMatch(rate, wwi->format.Format.nSamplesPerSec)) {
543 WARN("Rate doesn't match (requested %d Hz, got %d Hz)\n", wwi->format.Format.nSamplesPerSec, rate);
544 ret = WAVERR_BADFORMAT;
545 goto error;
548 dir=0;
549 EXIT_ON_ERROR( snd_pcm_hw_params_set_buffer_time_near(pcm, hw_params, &buffer_time, &dir), MMSYSERR_INVALPARAM, "unable to set buffer time");
550 dir=0;
551 EXIT_ON_ERROR( snd_pcm_hw_params_set_period_time_near(pcm, hw_params, &period_time, &dir), MMSYSERR_INVALPARAM, "unable to set period time");
553 EXIT_ON_ERROR( snd_pcm_hw_params(pcm, hw_params), MMSYSERR_INVALPARAM, "unable to set hw params for playback");
555 dir=0;
556 err = snd_pcm_hw_params_get_period_size(hw_params, &period_size, &dir);
557 err = snd_pcm_hw_params_get_buffer_size(hw_params, &buffer_size);
559 snd_pcm_sw_params_current(pcm, sw_params);
560 EXIT_ON_ERROR( snd_pcm_sw_params_set_start_threshold(pcm, sw_params, 1), MMSYSERR_ERROR, "unable to set start threshold");
561 EXIT_ON_ERROR( snd_pcm_sw_params_set_silence_size(pcm, sw_params, 0), MMSYSERR_ERROR, "unable to set silence size");
562 EXIT_ON_ERROR( snd_pcm_sw_params_set_avail_min(pcm, sw_params, period_size), MMSYSERR_ERROR, "unable to set avail min");
563 EXIT_ON_ERROR( snd_pcm_sw_params_set_xfer_align(pcm, sw_params, 1), MMSYSERR_ERROR, "unable to set xfer align");
564 EXIT_ON_ERROR( snd_pcm_sw_params_set_silence_threshold(pcm, sw_params, 0), MMSYSERR_ERROR, "unable to set silence threshold");
565 EXIT_ON_ERROR( snd_pcm_sw_params(pcm, sw_params), MMSYSERR_ERROR, "unable to set sw params for playback");
566 #undef EXIT_ON_ERROR
568 snd_pcm_prepare(pcm);
570 if (TRACE_ON(wave))
571 ALSA_TraceParameters(hw_params, sw_params, FALSE);
573 /* now, we can save all required data for later use... */
574 if ( wwi->hw_params )
575 snd_pcm_hw_params_free(wwi->hw_params);
576 snd_pcm_hw_params_malloc(&(wwi->hw_params));
577 snd_pcm_hw_params_copy(wwi->hw_params, hw_params);
579 wwi->dwBufferSize = snd_pcm_frames_to_bytes(pcm, buffer_size);
580 wwi->lpQueuePtr = wwi->lpPlayPtr = wwi->lpLoopPtr = NULL;
581 wwi->pcm = pcm;
583 ALSA_InitRingMessage(&wwi->msgRing);
585 wwi->dwPeriodSize = snd_pcm_frames_to_bytes(pcm, period_size);
586 TRACE("dwPeriodSize=%u\n", wwi->dwPeriodSize);
587 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%u, nSamplesPerSec=%u, nChannels=%u nBlockAlign=%u!\n",
588 wwi->format.Format.wBitsPerSample, wwi->format.Format.nAvgBytesPerSec,
589 wwi->format.Format.nSamplesPerSec, wwi->format.Format.nChannels,
590 wwi->format.Format.nBlockAlign);
592 wwi->hStartUpEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
593 wwi->hThread = CreateThread(NULL, 0, widRecorder, (LPVOID)(DWORD)wDevID, 0, &(wwi->dwThreadID));
594 if (wwi->hThread)
595 SetThreadPriority(wwi->hThread, THREAD_PRIORITY_TIME_CRITICAL);
596 WaitForSingleObject(wwi->hStartUpEvent, INFINITE);
597 CloseHandle(wwi->hStartUpEvent);
598 wwi->hStartUpEvent = INVALID_HANDLE_VALUE;
600 HeapFree( GetProcessHeap(), 0, hw_params );
601 HeapFree( GetProcessHeap(), 0, sw_params );
602 return widNotifyClient(wwi, WIM_OPEN, 0L, 0L);
604 error:
605 snd_pcm_close(pcm);
606 HeapFree( GetProcessHeap(), 0, hw_params );
607 HeapFree( GetProcessHeap(), 0, sw_params );
608 return ret;
612 /**************************************************************************
613 * widClose [internal]
615 static DWORD widClose(WORD wDevID)
617 DWORD ret = MMSYSERR_NOERROR;
618 WINE_WAVEDEV* wwi;
620 TRACE("(%u);\n", wDevID);
622 if (wDevID >= ALSA_WidNumDevs) {
623 TRACE("Requested device %d, but only %d are known!\n", wDevID, ALSA_WidNumDevs);
624 return MMSYSERR_BADDEVICEID;
627 if (WInDev[wDevID].pcm == NULL) {
628 WARN("Requested to close already closed device %d!\n", wDevID);
629 return MMSYSERR_BADDEVICEID;
632 wwi = &WInDev[wDevID];
633 if (wwi->lpQueuePtr) {
634 WARN("buffers still playing !\n");
635 ret = WAVERR_STILLPLAYING;
636 } else {
637 if (wwi->hThread != INVALID_HANDLE_VALUE) {
638 ALSA_AddRingMessage(&wwi->msgRing, WINE_WM_CLOSING, 0, TRUE);
640 ALSA_DestroyRingMessage(&wwi->msgRing);
642 snd_pcm_hw_params_free(wwi->hw_params);
643 wwi->hw_params = NULL;
645 snd_pcm_close(wwi->pcm);
646 wwi->pcm = NULL;
648 ret = widNotifyClient(wwi, WIM_CLOSE, 0L, 0L);
651 return ret;
654 /**************************************************************************
655 * widAddBuffer [internal]
658 static DWORD widAddBuffer(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
660 TRACE("(%u, %p, %08X);\n", wDevID, lpWaveHdr, dwSize);
662 /* first, do the sanity checks... */
663 if (wDevID >= ALSA_WidNumDevs) {
664 TRACE("Requested device %d, but only %d are known!\n", wDevID, ALSA_WidNumDevs);
665 return MMSYSERR_BADDEVICEID;
668 if (WInDev[wDevID].pcm == NULL) {
669 WARN("Requested to add buffer to already closed device %d!\n", wDevID);
670 return MMSYSERR_BADDEVICEID;
673 if (lpWaveHdr->lpData == NULL || !(lpWaveHdr->dwFlags & WHDR_PREPARED))
674 return WAVERR_UNPREPARED;
676 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
677 return WAVERR_STILLPLAYING;
679 lpWaveHdr->dwFlags &= ~WHDR_DONE;
680 lpWaveHdr->dwFlags |= WHDR_INQUEUE;
681 lpWaveHdr->lpNext = 0;
683 ALSA_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_HEADER, (DWORD)lpWaveHdr, FALSE);
685 return MMSYSERR_NOERROR;
688 /**************************************************************************
689 * widStart [internal]
692 static DWORD widStart(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
694 TRACE("(%u, %p, %08X);\n", wDevID, lpWaveHdr, dwSize);
696 /* first, do the sanity checks... */
697 if (wDevID >= ALSA_WidNumDevs) {
698 TRACE("Requested device %d, but only %d are known!\n", wDevID, ALSA_WidNumDevs);
699 return MMSYSERR_BADDEVICEID;
702 if (WInDev[wDevID].pcm == NULL) {
703 WARN("Requested to start closed device %d!\n", wDevID);
704 return MMSYSERR_BADDEVICEID;
707 ALSA_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_STARTING, 0, TRUE);
709 return MMSYSERR_NOERROR;
712 /**************************************************************************
713 * widStop [internal]
716 static DWORD widStop(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
718 TRACE("(%u, %p, %08X);\n", wDevID, lpWaveHdr, dwSize);
720 /* first, do the sanity checks... */
721 if (wDevID >= ALSA_WidNumDevs) {
722 TRACE("Requested device %d, but only %d are known!\n", wDevID, ALSA_WidNumDevs);
723 return MMSYSERR_BADDEVICEID;
726 if (WInDev[wDevID].pcm == NULL) {
727 WARN("Requested to stop closed device %d!\n", wDevID);
728 return MMSYSERR_BADDEVICEID;
731 ALSA_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_STOPPING, 0, TRUE);
733 return MMSYSERR_NOERROR;
736 /**************************************************************************
737 * widReset [internal]
739 static DWORD widReset(WORD wDevID)
741 TRACE("(%u);\n", wDevID);
742 if (wDevID >= ALSA_WidNumDevs) {
743 TRACE("Requested device %d, but only %d are known!\n", wDevID, ALSA_WidNumDevs);
744 return MMSYSERR_BADDEVICEID;
747 if (WInDev[wDevID].pcm == NULL) {
748 WARN("Requested to reset closed device %d!\n", wDevID);
749 return MMSYSERR_BADDEVICEID;
752 ALSA_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_RESETTING, 0, TRUE);
753 return MMSYSERR_NOERROR;
756 /**************************************************************************
757 * widGetPosition [internal]
759 static DWORD widGetPosition(WORD wDevID, LPMMTIME lpTime, DWORD uSize)
761 WINE_WAVEDEV* wwi;
763 TRACE("(%u, %p, %u);\n", wDevID, lpTime, uSize);
765 if (wDevID >= ALSA_WidNumDevs) {
766 TRACE("Requested device %d, but only %d are known!\n", wDevID, ALSA_WidNumDevs);
767 return MMSYSERR_BADDEVICEID;
770 if (WInDev[wDevID].state == WINE_WS_CLOSED) {
771 WARN("Requested position of closed device %d!\n", wDevID);
772 return MMSYSERR_BADDEVICEID;
775 if (lpTime == NULL) {
776 WARN("invalid parameter: lpTime = NULL\n");
777 return MMSYSERR_INVALPARAM;
780 wwi = &WInDev[wDevID];
781 return ALSA_bytes_to_mmtime(lpTime, wwi->dwTotalRecorded, &wwi->format);
784 /**************************************************************************
785 * widGetNumDevs [internal]
787 static DWORD widGetNumDevs(void)
789 return ALSA_WidNumDevs;
792 /**************************************************************************
793 * widDevInterfaceSize [internal]
795 static DWORD widDevInterfaceSize(UINT wDevID, LPDWORD dwParam1)
797 TRACE("(%u, %p)\n", wDevID, dwParam1);
799 *dwParam1 = MultiByteToWideChar(CP_ACP, 0, WInDev[wDevID].interface_name, -1,
800 NULL, 0 ) * sizeof(WCHAR);
801 return MMSYSERR_NOERROR;
804 /**************************************************************************
805 * widDevInterface [internal]
807 static DWORD widDevInterface(UINT wDevID, PWCHAR dwParam1, DWORD dwParam2)
809 if (dwParam2 >= MultiByteToWideChar(CP_ACP, 0, WInDev[wDevID].interface_name, -1,
810 NULL, 0 ) * sizeof(WCHAR))
812 MultiByteToWideChar(CP_ACP, 0, WInDev[wDevID].interface_name, -1,
813 dwParam1, dwParam2 / sizeof(WCHAR));
814 return MMSYSERR_NOERROR;
816 return MMSYSERR_INVALPARAM;
819 /**************************************************************************
820 * widMessage (WINEALSA.@)
822 DWORD WINAPI ALSA_widMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
823 DWORD dwParam1, DWORD dwParam2)
825 TRACE("(%u, %s, %08X, %08X, %08X);\n",
826 wDevID, ALSA_getMessage(wMsg), dwUser, dwParam1, dwParam2);
828 switch (wMsg) {
829 case DRVM_INIT:
830 case DRVM_EXIT:
831 case DRVM_ENABLE:
832 case DRVM_DISABLE:
833 /* FIXME: Pretend this is supported */
834 return 0;
835 case WIDM_OPEN: return widOpen (wDevID, (LPWAVEOPENDESC)dwParam1, dwParam2);
836 case WIDM_CLOSE: return widClose (wDevID);
837 case WIDM_ADDBUFFER: return widAddBuffer (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
838 case WIDM_PREPARE: return MMSYSERR_NOTSUPPORTED;
839 case WIDM_UNPREPARE: return MMSYSERR_NOTSUPPORTED;
840 case WIDM_GETDEVCAPS: return widGetDevCaps (wDevID, (LPWAVEINCAPSW)dwParam1, dwParam2);
841 case WIDM_GETNUMDEVS: return widGetNumDevs ();
842 case WIDM_GETPOS: return widGetPosition (wDevID, (LPMMTIME)dwParam1, dwParam2);
843 case WIDM_RESET: return widReset (wDevID);
844 case WIDM_START: return widStart (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
845 case WIDM_STOP: return widStop (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
846 case DRV_QUERYDEVICEINTERFACESIZE: return widDevInterfaceSize (wDevID, (LPDWORD)dwParam1);
847 case DRV_QUERYDEVICEINTERFACE: return widDevInterface (wDevID, (PWCHAR)dwParam1, dwParam2);
848 case DRV_QUERYDSOUNDIFACE: return widDsCreate (wDevID, (PIDSCDRIVER*)dwParam1);
849 case DRV_QUERYDSOUNDDESC: return widDsDesc (wDevID, (PDSDRIVERDESC)dwParam1);
850 default:
851 FIXME("unknown message %d!\n", wMsg);
853 return MMSYSERR_NOTSUPPORTED;
856 #else /* HAVE_ALSA */
858 /**************************************************************************
859 * widMessage (WINEALSA.@)
861 DWORD WINAPI ALSA_widMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
862 DWORD dwParam1, DWORD dwParam2)
864 FIXME("(%u, %04X, %08X, %08X, %08X):stub\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
865 return MMSYSERR_NOTENABLED;
868 #endif /* HAVE_ALSA */