1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
3 * Sample Wine Driver for Advanced Linux Sound System (ALSA)
4 * Based on version <final> of the ALSA API
6 * Copyright 2002 Eric Pouech
7 * 2002 Marco Pietrobono
8 * 2003 Christian Costa : WaveIn support
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 /* unless someone makes a wineserver kernel module, Unix pipes are faster than win32 events */
29 #include "wine/port.h"
41 #ifdef HAVE_SYS_IOCTL_H
42 # include <sys/ioctl.h>
44 #ifdef HAVE_SYS_MMAN_H
45 # include <sys/mman.h>
57 #define ALSA_PCM_OLD_HW_PARAMS_API
58 #define ALSA_PCM_OLD_SW_PARAMS_API
60 #include "wine/library.h"
61 #include "wine/debug.h"
63 WINE_DEFAULT_DEBUG_CHANNEL(wave);
66 #if defined(HAVE_ALSA) && ((SND_LIB_MAJOR == 0 && SND_LIB_MINOR >= 9) || SND_LIB_MAJOR >= 1)
68 /* internal ALSALIB functions */
69 snd_pcm_uframes_t _snd_pcm_mmap_hw_ptr(snd_pcm_t *pcm);
72 #define MAX_WAVEOUTDRV (1)
73 #define MAX_WAVEINDRV (1)
75 /* state diagram for waveOut writing:
77 * +---------+-------------+---------------+---------------------------------+
78 * | state | function | event | new state |
79 * +---------+-------------+---------------+---------------------------------+
80 * | | open() | | STOPPED |
81 * | PAUSED | write() | | PAUSED |
82 * | STOPPED | write() | <thrd create> | PLAYING |
83 * | PLAYING | write() | HEADER | PLAYING |
84 * | (other) | write() | <error> | |
85 * | (any) | pause() | PAUSING | PAUSED |
86 * | PAUSED | restart() | RESTARTING | PLAYING (if no thrd => STOPPED) |
87 * | (any) | reset() | RESETTING | STOPPED |
88 * | (any) | close() | CLOSING | CLOSED |
89 * +---------+-------------+---------------+---------------------------------+
92 /* states of the playing device */
93 #define WINE_WS_PLAYING 0
94 #define WINE_WS_PAUSED 1
95 #define WINE_WS_STOPPED 2
96 #define WINE_WS_CLOSED 3
98 /* events to be send to device */
100 WINE_WM_PAUSING = WM_USER + 1, WINE_WM_RESTARTING, WINE_WM_RESETTING, WINE_WM_HEADER,
101 WINE_WM_UPDATE, WINE_WM_BREAKLOOP, WINE_WM_CLOSING, WINE_WM_STARTING, WINE_WM_STOPPING
105 #define SIGNAL_OMR(omr) do { int x = 0; write((omr)->msg_pipe[1], &x, sizeof(x)); } while (0)
106 #define CLEAR_OMR(omr) do { int x = 0; read((omr)->msg_pipe[0], &x, sizeof(x)); } while (0)
107 #define RESET_OMR(omr) do { } while (0)
108 #define WAIT_OMR(omr, sleep) \
109 do { struct pollfd pfd; pfd.fd = (omr)->msg_pipe[0]; \
110 pfd.events = POLLIN; poll(&pfd, 1, sleep); } while (0)
112 #define SIGNAL_OMR(omr) do { SetEvent((omr)->msg_event); } while (0)
113 #define CLEAR_OMR(omr) do { } while (0)
114 #define RESET_OMR(omr) do { ResetEvent((omr)->msg_event); } while (0)
115 #define WAIT_OMR(omr, sleep) \
116 do { WaitForSingleObject((omr)->msg_event, sleep); } while (0)
120 enum win_wm_message msg; /* message identifier */
121 DWORD param; /* parameter for this message */
122 HANDLE hEvent; /* if message is synchronous, handle of event for synchro */
125 /* implement an in-process message ring for better performance
126 * (compared to passing thru the server)
127 * this ring will be used by the input (resp output) record (resp playback) routine
129 #define ALSA_RING_BUFFER_INCREMENT 64
132 int ring_buffer_size;
140 CRITICAL_SECTION msg_crst;
144 /* Windows information */
145 volatile int state; /* one of the WINE_WS_ manifest constants */
146 WAVEOPENDESC waveDesc;
148 PCMWAVEFORMAT format;
151 /* ALSA information (ALSA 0.9/1.x uses two different devices for playback/capture) */
153 snd_pcm_t* p_handle; /* handle to ALSA playback device */
154 snd_pcm_t* c_handle; /* handle to ALSA capture device */
155 snd_pcm_hw_params_t * hw_params; /* ALSA Hw params */
157 snd_ctl_t * ctl; /* control handle for the playback volume */
158 snd_ctl_elem_id_t * playback_eid; /* element id of the playback volume control */
159 snd_ctl_elem_value_t * playback_evalue; /* element value of the playback volume control */
160 snd_ctl_elem_info_t * playback_einfo; /* element info of the playback volume control */
162 snd_pcm_sframes_t (*write)(snd_pcm_t *, const void *, snd_pcm_uframes_t );
167 DWORD dwBufferSize; /* size of whole ALSA buffer in bytes */
168 LPWAVEHDR lpQueuePtr; /* start of queued WAVEHDRs (waiting to be notified) */
169 LPWAVEHDR lpPlayPtr; /* start of not yet fully played buffers */
170 DWORD dwPartialOffset; /* Offset of not yet written bytes in lpPlayPtr */
172 LPWAVEHDR lpLoopPtr; /* pointer of first buffer in loop, if any */
173 DWORD dwLoops; /* private copy of loop counter */
175 DWORD dwPlayedTotal; /* number of bytes actually played since opening */
176 DWORD dwWrittenTotal; /* number of bytes written to ALSA buffer since opening */
178 /* synchronization stuff */
179 HANDLE hStartUpEvent;
182 ALSA_MSG_RING msgRing;
184 /* DirectSound stuff */
185 DSDRIVERDESC ds_desc;
190 /* Windows information */
191 volatile int state; /* one of the WINE_WS_ manifest constants */
192 WAVEOPENDESC waveDesc;
194 PCMWAVEFORMAT format;
197 /* ALSA information (ALSA 0.9/1.x uses two different devices for playback/capture) */
199 snd_pcm_t* p_handle; /* handle to ALSA playback device */
200 snd_pcm_t* c_handle; /* handle to ALSA capture device */
201 snd_pcm_hw_params_t * hw_params; /* ALSA Hw params */
203 snd_ctl_t * ctl; /* control handle for the playback volume */
204 snd_ctl_elem_id_t * playback_eid; /* element id of the playback volume control */
205 snd_ctl_elem_value_t * playback_evalue; /* element value of the playback volume control */
206 snd_ctl_elem_info_t * playback_einfo; /* element info of the playback volume control */
208 snd_pcm_sframes_t (*read)(snd_pcm_t *, void *, snd_pcm_uframes_t );
213 DWORD dwPeriodSize; /* size of OSS buffer period */
214 DWORD dwBufferSize; /* size of whole ALSA buffer in bytes */
215 LPWAVEHDR lpQueuePtr; /* start of queued WAVEHDRs (waiting to be notified) */
216 LPWAVEHDR lpPlayPtr; /* start of not yet fully played buffers */
218 LPWAVEHDR lpLoopPtr; /* pointer of first buffer in loop, if any */
219 DWORD dwLoops; /* private copy of loop counter */
221 /*DWORD dwPlayedTotal; */
222 DWORD dwTotalRecorded;
224 /* synchronization stuff */
225 HANDLE hStartUpEvent;
228 ALSA_MSG_RING msgRing;
230 /* DirectSound stuff */
231 DSDRIVERDESC ds_desc;
235 static WINE_WAVEOUT WOutDev [MAX_WAVEOUTDRV];
236 static DWORD ALSA_WodNumDevs;
237 static WINE_WAVEIN WInDev [MAX_WAVEINDRV];
238 static DWORD ALSA_WidNumDevs;
240 static DWORD wodDsCreate(UINT wDevID, PIDSDRIVER* drv);
241 static DWORD wodDsDesc(UINT wDevID, PDSDRIVERDESC desc);
242 static DWORD wodDsGuid(UINT wDevID, LPGUID pGuid);
244 /* These strings used only for tracing */
246 static const char *wodPlayerCmdString[] = {
248 "WINE_WM_RESTARTING",
259 /*======================================================================*
260 * Low level WAVE implementation *
261 *======================================================================*/
263 /**************************************************************************
264 * ALSA_InitializeVolumeCtl [internal]
266 * used to initialize the PCM Volume Control
268 static int ALSA_InitializeVolumeCtl(WINE_WAVEOUT * wwo)
270 snd_ctl_t * ctl = NULL;
271 snd_ctl_card_info_t * cardinfo;
272 snd_ctl_elem_list_t * elemlist;
273 snd_ctl_elem_id_t * e_id;
274 snd_ctl_elem_info_t * einfo;
275 snd_hctl_t * hctl = NULL;
276 snd_hctl_elem_t * elem;
280 snd_ctl_card_info_alloca(&cardinfo);
281 memset(cardinfo,0,snd_ctl_card_info_sizeof());
283 snd_ctl_elem_list_alloca(&elemlist);
284 memset(elemlist,0,snd_ctl_elem_list_sizeof());
286 snd_ctl_elem_id_alloca(&e_id);
287 memset(e_id,0,snd_ctl_elem_id_sizeof());
289 snd_ctl_elem_info_alloca(&einfo);
290 memset(einfo,0,snd_ctl_elem_info_sizeof());
292 #define EXIT_ON_ERROR(f,txt) do \
295 if ( (err = (f) ) < 0) \
297 ERR(txt ": %s\n", snd_strerror(err)); \
299 snd_hctl_close(hctl); \
301 snd_ctl_close(ctl); \
306 EXIT_ON_ERROR( snd_ctl_open(&ctl,"hw",0) , "ctl open failed" );
307 EXIT_ON_ERROR( snd_ctl_card_info(ctl, cardinfo), "card info failed");
308 EXIT_ON_ERROR( snd_ctl_elem_list(ctl, elemlist), "elem list failed");
310 nCtrls = snd_ctl_elem_list_get_count(elemlist);
312 EXIT_ON_ERROR( snd_hctl_open(&hctl,"hw",0), "hctl open failed");
313 EXIT_ON_ERROR( snd_hctl_load(hctl), "hctl load failed" );
315 elem=snd_hctl_first_elem(hctl);
316 for ( i= 0; i<nCtrls; i++) {
318 memset(e_id,0,snd_ctl_elem_id_sizeof());
320 snd_hctl_elem_get_id(elem,e_id);
322 TRACE("ctl: #%d '%s'%d\n",
323 snd_ctl_elem_id_get_numid(e_id),
324 snd_ctl_elem_id_get_name(e_id),
325 snd_ctl_elem_id_get_index(e_id));
327 if ( !strcmp("PCM Playback Volume", snd_ctl_elem_id_get_name(e_id)) )
329 EXIT_ON_ERROR( snd_hctl_elem_info(elem,einfo), "hctl elem info failed" );
331 /* few sanity checks... you'll never know... */
332 if ( snd_ctl_elem_info_get_type(einfo) != SND_CTL_ELEM_TYPE_INTEGER )
333 WARN("playback volume control is not an integer\n");
334 if ( !snd_ctl_elem_info_is_readable(einfo) )
335 WARN("playback volume control is readable\n");
336 if ( !snd_ctl_elem_info_is_writable(einfo) )
337 WARN("playback volume control is readable\n");
339 TRACE(" ctrl range: min=%ld max=%ld step=%ld\n",
340 snd_ctl_elem_info_get_min(einfo),
341 snd_ctl_elem_info_get_max(einfo),
342 snd_ctl_elem_info_get_step(einfo));
344 EXIT_ON_ERROR( snd_ctl_elem_id_malloc(&wwo->playback_eid), "elem id malloc failed" );
345 EXIT_ON_ERROR( snd_ctl_elem_info_malloc(&wwo->playback_einfo), "elem info malloc failed" );
346 EXIT_ON_ERROR( snd_ctl_elem_value_malloc(&wwo->playback_evalue), "elem value malloc failed" );
348 /* ok, now we can safely save these objects for later */
349 snd_ctl_elem_id_copy(wwo->playback_eid, e_id);
350 snd_ctl_elem_info_copy(wwo->playback_einfo, einfo);
351 snd_ctl_elem_value_set_id(wwo->playback_evalue, wwo->playback_eid);
355 elem=snd_hctl_elem_next(elem);
357 snd_hctl_close(hctl);
362 /**************************************************************************
363 * ALSA_XRUNRecovery [internal]
365 * used to recovery from XRUN errors (buffer underflow/overflow)
367 static int ALSA_XRUNRecovery(WINE_WAVEOUT * wwo, int err)
369 if (err == -EPIPE) { /* under-run */
370 err = snd_pcm_prepare(wwo->p_handle);
372 ERR( "underrun recovery failed. prepare failed: %s\n", snd_strerror(err));
374 } else if (err == -ESTRPIPE) {
375 while ((err = snd_pcm_resume(wwo->p_handle)) == -EAGAIN)
376 sleep(1); /* wait until the suspend flag is released */
378 err = snd_pcm_prepare(wwo->p_handle);
380 ERR("recovery from suspend failed, prepare failed: %s\n", snd_strerror(err));
387 /**************************************************************************
388 * ALSA_TraceParameters [internal]
390 * used to trace format changes, hw and sw parameters
392 static void ALSA_TraceParameters(snd_pcm_hw_params_t * hw_params, snd_pcm_sw_params_t * sw, int full)
394 snd_pcm_format_t format = snd_pcm_hw_params_get_format(hw_params);
395 snd_pcm_access_t access = snd_pcm_hw_params_get_access(hw_params);
397 #define X(x) ((x)? "true" : "false")
399 TRACE("FLAGS: sampleres=%s overrng=%s pause=%s resume=%s syncstart=%s batch=%s block=%s double=%s "
400 "halfd=%s joint=%s \n",
401 X(snd_pcm_hw_params_can_mmap_sample_resolution(hw_params)),
402 X(snd_pcm_hw_params_can_overrange(hw_params)),
403 X(snd_pcm_hw_params_can_pause(hw_params)),
404 X(snd_pcm_hw_params_can_resume(hw_params)),
405 X(snd_pcm_hw_params_can_sync_start(hw_params)),
406 X(snd_pcm_hw_params_is_batch(hw_params)),
407 X(snd_pcm_hw_params_is_block_transfer(hw_params)),
408 X(snd_pcm_hw_params_is_double(hw_params)),
409 X(snd_pcm_hw_params_is_half_duplex(hw_params)),
410 X(snd_pcm_hw_params_is_joint_duplex(hw_params)));
414 TRACE("access=%s\n", snd_pcm_access_name(access));
417 snd_pcm_access_mask_t * acmask;
418 snd_pcm_access_mask_alloca(&acmask);
419 snd_pcm_hw_params_get_access_mask(hw_params, acmask);
420 for ( access = SND_PCM_ACCESS_MMAP_INTERLEAVED; access <= SND_PCM_ACCESS_LAST; access++)
421 if (snd_pcm_access_mask_test(acmask, access))
422 TRACE("access=%s\n", snd_pcm_access_name(access));
427 TRACE("format=%s\n", snd_pcm_format_name(format));
432 snd_pcm_format_mask_t * fmask;
434 snd_pcm_format_mask_alloca(&fmask);
435 snd_pcm_hw_params_get_format_mask(hw_params, fmask);
436 for ( format = SND_PCM_FORMAT_S8; format <= SND_PCM_FORMAT_LAST ; format++)
437 if ( snd_pcm_format_mask_test(fmask, format) )
438 TRACE("format=%s\n", snd_pcm_format_name(format));
442 int n = snd_pcm_hw_params_get_##x(hw_params); \
444 TRACE(#x "_min=%ld " #x "_max=%ld\n", \
445 (long int)snd_pcm_hw_params_get_##x##_min(hw_params), \
446 (long int)snd_pcm_hw_params_get_##x##_max(hw_params)); \
448 TRACE(#x "=%d\n", n); \
455 int n = snd_pcm_hw_params_get_##x(hw_params,0); \
457 TRACE(#x "_min=%ld " #x "_max=%ld\n", \
458 (long int)snd_pcm_hw_params_get_##x##_min(hw_params,0), \
459 (long int)snd_pcm_hw_params_get_##x##_max(hw_params,0)); \
461 TRACE(#x "=%d\n", n); \
477 /* return a string duplicated on the win32 process heap, free with HeapFree */
478 static char* ALSA_strdup(char *s) {
479 char *result = HeapAlloc(GetProcessHeap(), 0, strlen(s)+1);
484 /******************************************************************
485 * ALSA_GetDeviceFromReg
487 * Returns either "default" or reads the registry so the user can
488 * override the playback/record device used.
490 char *ALSA_GetDeviceFromReg(char *value)
494 HKEY playbackKey = 0;
498 res = RegOpenKeyExA(HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\ALSA", 0, KEY_QUERY_VALUE, &playbackKey);
499 if (res != ERROR_SUCCESS) goto end;
501 res = RegQueryValueExA(playbackKey, value, NULL, &type, NULL, &resultSize);
502 if (res != ERROR_SUCCESS) goto end;
504 if (type != REG_SZ) {
505 ERR("Registry key [HKEY_LOCAL_MACHINE\\Software\\Wine\\Wine\\ALSA\\%s] must be a string\n", value);
509 result = HeapAlloc(GetProcessHeap(), 0, resultSize);
510 res = RegQueryValueExA(playbackKey, value, NULL, NULL, result, &resultSize);
513 if (!result) result = ALSA_strdup("default");
514 if (playbackKey) RegCloseKey(playbackKey);
518 /******************************************************************
521 * Initialize internal structures from ALSA information
523 LONG ALSA_WaveInit(void)
526 snd_pcm_info_t * info;
527 snd_pcm_hw_params_t * hw_params;
533 /* FIXME: use better values */
534 wwo->device = ALSA_GetDeviceFromReg("PlaybackDevice");
535 TRACE("using waveout device \"%s\"\n", wwo->device);
537 wwo->caps.wMid = 0x0002;
538 wwo->caps.wPid = 0x0104;
539 strcpy(wwo->caps.szPname, "SB16 Wave Out");
540 wwo->caps.vDriverVersion = 0x0100;
541 wwo->caps.dwFormats = 0x00000000;
542 wwo->caps.dwSupport = WAVECAPS_VOLUME;
543 strcpy(wwo->ds_desc.szDesc, "WineALSA DirectSound Driver");
544 strcpy(wwo->ds_desc.szDrvName, "winealsa.drv");
545 wwo->ds_guid = DSDEVID_DefaultPlayback;
547 if (!wine_dlopen("libasound.so.2", RTLD_LAZY|RTLD_GLOBAL, NULL, 0))
549 ERR("Error: ALSA lib needs to be loaded with flags RTLD_LAZY and RTLD_GLOBAL.\n");
553 snd_pcm_info_alloca(&info);
554 snd_pcm_hw_params_alloca(&hw_params);
556 #define EXIT_ON_ERROR(f,txt) do { int err; if ( (err = (f) ) < 0) { ERR(txt ": %s\n", snd_strerror(err)); if (h) snd_pcm_close(h); return -1; } } while(0)
559 EXIT_ON_ERROR( snd_pcm_open(&h, wwo->device, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK) , "open pcm" );
563 EXIT_ON_ERROR( snd_pcm_info(h, info) , "pcm info" );
565 TRACE("dev=%d id=%s name=%s subdev=%d subdev_name=%s subdev_avail=%d subdev_num=%d stream=%s subclass=%s \n",
566 snd_pcm_info_get_device(info),
567 snd_pcm_info_get_id(info),
568 snd_pcm_info_get_name(info),
569 snd_pcm_info_get_subdevice(info),
570 snd_pcm_info_get_subdevice_name(info),
571 snd_pcm_info_get_subdevices_avail(info),
572 snd_pcm_info_get_subdevices_count(info),
573 snd_pcm_stream_name(snd_pcm_info_get_stream(info)),
574 (snd_pcm_info_get_subclass(info) == SND_PCM_SUBCLASS_GENERIC_MIX ? "GENERIC MIX": "MULTI MIX"));
576 EXIT_ON_ERROR( snd_pcm_hw_params_any(h, hw_params) , "pcm hw params" );
580 ALSA_TraceParameters(hw_params, NULL, TRUE);
583 snd_pcm_format_mask_t * fmask;
584 int ratemin = snd_pcm_hw_params_get_rate_min(hw_params, 0);
585 int ratemax = snd_pcm_hw_params_get_rate_max(hw_params, 0);
586 int chmin = snd_pcm_hw_params_get_channels_min(hw_params); \
587 int chmax = snd_pcm_hw_params_get_channels_max(hw_params); \
589 snd_pcm_format_mask_alloca(&fmask);
590 snd_pcm_hw_params_get_format_mask(hw_params, fmask);
593 if ( (r) >= ratemin && ( (r) <= ratemax || ratemax == -1) ) \
595 if (snd_pcm_format_mask_test( fmask, SND_PCM_FORMAT_U8)) \
597 if (chmin <= 1 && 1 <= chmax) \
598 wwo->caps.dwFormats |= WAVE_FORMAT_##v##S08; \
599 if (chmin <= 2 && 2 <= chmax) \
600 wwo->caps.dwFormats |= WAVE_FORMAT_##v##S08; \
602 if (snd_pcm_format_mask_test( fmask, SND_PCM_FORMAT_S16_LE)) \
604 if (chmin <= 1 && 1 <= chmax) \
605 wwo->caps.dwFormats |= WAVE_FORMAT_##v##S16; \
606 if (chmin <= 2 && 2 <= chmax) \
607 wwo->caps.dwFormats |= WAVE_FORMAT_##v##S16; \
618 if ( snd_pcm_hw_params_get_channels_min(hw_params) > 1) FIXME("-\n");
619 wwo->caps.wChannels = (snd_pcm_hw_params_get_channels_max(hw_params) >= 2) ? 2 : 1;
620 if (snd_pcm_hw_params_get_channels_min(hw_params) <= 2 && 2 <= snd_pcm_hw_params_get_channels_max(hw_params))
621 wwo->caps.dwSupport |= WAVECAPS_LRVOLUME;
623 /* FIXME: always true ? */
624 wwo->caps.dwSupport |= WAVECAPS_SAMPLEACCURATE;
627 snd_pcm_access_mask_t * acmask;
628 snd_pcm_access_mask_alloca(&acmask);
629 snd_pcm_hw_params_get_access_mask(hw_params, acmask);
631 /* FIXME: NONITERLEAVED and COMPLEX are not supported right now */
632 if ( snd_pcm_access_mask_test( acmask, SND_PCM_ACCESS_MMAP_INTERLEAVED ) )
633 wwo->caps.dwSupport |= WAVECAPS_DIRECTSOUND;
636 TRACE("Configured with dwFmts=%08lx dwSupport=%08lx\n",
637 wwo->caps.dwFormats, wwo->caps.dwSupport);
641 ALSA_InitializeVolumeCtl(wwo);
645 /* FIXME: use better values */
646 wwi->device = ALSA_GetDeviceFromReg("RecordDevice");
647 TRACE("using wavein device \"%s\"\n", wwi->device);
649 wwi->caps.wMid = 0x0002;
650 wwi->caps.wPid = 0x0104;
651 strcpy(wwi->caps.szPname, "SB16 Wave In");
652 wwi->caps.vDriverVersion = 0x0100;
653 wwi->caps.dwFormats = 0x00000000;
654 wwi->caps.dwSupport = WAVECAPS_VOLUME;
655 strcpy(wwi->ds_desc.szDesc, "WineALSA DirectSound Driver");
656 strcpy(wwi->ds_desc.szDrvName, "winealsa.drv");
657 wwi->ds_guid = DSDEVID_DefaultPlayback;
659 snd_pcm_info_alloca(&info);
660 snd_pcm_hw_params_alloca(&hw_params);
662 #define EXIT_ON_ERROR(f,txt) do { int err; if ( (err = (f) ) < 0) { ERR(txt ": %s\n", snd_strerror(err)); if (h) snd_pcm_close(h); return -1; } } while(0)
665 EXIT_ON_ERROR( snd_pcm_open(&h, wwi->device, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK) , "open pcm" );
669 EXIT_ON_ERROR( snd_pcm_info(h, info) , "pcm info" );
671 TRACE("dev=%d id=%s name=%s subdev=%d subdev_name=%s subdev_avail=%d subdev_num=%d stream=%s subclass=%s \n",
672 snd_pcm_info_get_device(info),
673 snd_pcm_info_get_id(info),
674 snd_pcm_info_get_name(info),
675 snd_pcm_info_get_subdevice(info),
676 snd_pcm_info_get_subdevice_name(info),
677 snd_pcm_info_get_subdevices_avail(info),
678 snd_pcm_info_get_subdevices_count(info),
679 snd_pcm_stream_name(snd_pcm_info_get_stream(info)),
680 (snd_pcm_info_get_subclass(info) == SND_PCM_SUBCLASS_GENERIC_MIX ? "GENERIC MIX": "MULTI MIX"));
682 EXIT_ON_ERROR( snd_pcm_hw_params_any(h, hw_params) , "pcm hw params" );
686 ALSA_TraceParameters(hw_params, NULL, TRUE);
689 snd_pcm_format_mask_t * fmask;
690 int ratemin = snd_pcm_hw_params_get_rate_min(hw_params, 0);
691 int ratemax = snd_pcm_hw_params_get_rate_max(hw_params, 0);
692 int chmin = snd_pcm_hw_params_get_channels_min(hw_params); \
693 int chmax = snd_pcm_hw_params_get_channels_max(hw_params); \
695 snd_pcm_format_mask_alloca(&fmask);
696 snd_pcm_hw_params_get_format_mask(hw_params, fmask);
699 if ( (r) >= ratemin && ( (r) <= ratemax || ratemax == -1) ) \
701 if (snd_pcm_format_mask_test( fmask, SND_PCM_FORMAT_U8)) \
703 if (chmin <= 1 && 1 <= chmax) \
704 wwi->caps.dwFormats |= WAVE_FORMAT_##v##S08; \
705 if (chmin <= 2 && 2 <= chmax) \
706 wwi->caps.dwFormats |= WAVE_FORMAT_##v##S08; \
708 if (snd_pcm_format_mask_test( fmask, SND_PCM_FORMAT_S16_LE)) \
710 if (chmin <= 1 && 1 <= chmax) \
711 wwi->caps.dwFormats |= WAVE_FORMAT_##v##S16; \
712 if (chmin <= 2 && 2 <= chmax) \
713 wwi->caps.dwFormats |= WAVE_FORMAT_##v##S16; \
724 if ( snd_pcm_hw_params_get_channels_min(hw_params) > 1) FIXME("-\n");
725 wwi->caps.wChannels = (snd_pcm_hw_params_get_channels_max(hw_params) >= 2) ? 2 : 1;
726 if (snd_pcm_hw_params_get_channels_min(hw_params) <= 2 && 2 <= snd_pcm_hw_params_get_channels_max(hw_params))
727 wwi->caps.dwSupport |= WAVECAPS_LRVOLUME;
729 /* FIXME: always true ? */
730 wwi->caps.dwSupport |= WAVECAPS_SAMPLEACCURATE;
733 snd_pcm_access_mask_t * acmask;
734 snd_pcm_access_mask_alloca(&acmask);
735 snd_pcm_hw_params_get_access_mask(hw_params, acmask);
737 /* FIXME: NONITERLEAVED and COMPLEX are not supported right now */
738 if ( snd_pcm_access_mask_test( acmask, SND_PCM_ACCESS_MMAP_INTERLEAVED ) )
739 wwi->caps.dwSupport |= WAVECAPS_DIRECTSOUND;
742 TRACE("Configured with dwFmts=%08lx dwSupport=%08lx\n",
743 wwi->caps.dwFormats, wwi->caps.dwSupport);
750 /******************************************************************
751 * ALSA_InitRingMessage
753 * Initialize the ring of messages for passing between driver's caller and playback/record
756 static int ALSA_InitRingMessage(ALSA_MSG_RING* omr)
761 if (pipe(omr->msg_pipe) < 0) {
762 omr->msg_pipe[0] = -1;
763 omr->msg_pipe[1] = -1;
764 ERR("could not create pipe, error=%s\n", strerror(errno));
767 omr->msg_event = CreateEventA(NULL, FALSE, FALSE, NULL);
769 omr->ring_buffer_size = ALSA_RING_BUFFER_INCREMENT;
770 omr->messages = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,omr->ring_buffer_size * sizeof(ALSA_MSG));
772 InitializeCriticalSection(&omr->msg_crst);
776 /******************************************************************
777 * ALSA_DestroyRingMessage
780 static int ALSA_DestroyRingMessage(ALSA_MSG_RING* omr)
783 close(omr->msg_pipe[0]);
784 close(omr->msg_pipe[1]);
786 CloseHandle(omr->msg_event);
788 HeapFree(GetProcessHeap(),0,omr->messages);
789 DeleteCriticalSection(&omr->msg_crst);
793 /******************************************************************
794 * ALSA_AddRingMessage
796 * Inserts a new message into the ring (should be called from DriverProc derivated routines)
798 static int ALSA_AddRingMessage(ALSA_MSG_RING* omr, enum win_wm_message msg, DWORD param, BOOL wait)
800 HANDLE hEvent = INVALID_HANDLE_VALUE;
802 EnterCriticalSection(&omr->msg_crst);
803 if ((omr->msg_toget == ((omr->msg_tosave + 1) % omr->ring_buffer_size)))
805 int old_ring_buffer_size = omr->ring_buffer_size;
806 omr->ring_buffer_size += ALSA_RING_BUFFER_INCREMENT;
807 TRACE("omr->ring_buffer_size=%d\n",omr->ring_buffer_size);
808 omr->messages = HeapReAlloc(GetProcessHeap(),0,omr->messages, omr->ring_buffer_size * sizeof(ALSA_MSG));
809 /* Now we need to rearrange the ring buffer so that the new
810 buffers just allocated are in between omr->msg_tosave and
813 if (omr->msg_tosave < omr->msg_toget)
815 memmove(&(omr->messages[omr->msg_toget + ALSA_RING_BUFFER_INCREMENT]),
816 &(omr->messages[omr->msg_toget]),
817 sizeof(ALSA_MSG)*(old_ring_buffer_size - omr->msg_toget)
819 omr->msg_toget += ALSA_RING_BUFFER_INCREMENT;
824 hEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
825 if (hEvent == INVALID_HANDLE_VALUE)
827 ERR("can't create event !?\n");
828 LeaveCriticalSection(&omr->msg_crst);
831 if (omr->msg_toget != omr->msg_tosave && omr->messages[omr->msg_toget].msg != WINE_WM_HEADER)
832 FIXME("two fast messages in the queue!!!!\n");
834 /* fast messages have to be added at the start of the queue */
835 omr->msg_toget = (omr->msg_toget + omr->ring_buffer_size - 1) % omr->ring_buffer_size;
837 omr->messages[omr->msg_toget].msg = msg;
838 omr->messages[omr->msg_toget].param = param;
839 omr->messages[omr->msg_toget].hEvent = hEvent;
843 omr->messages[omr->msg_tosave].msg = msg;
844 omr->messages[omr->msg_tosave].param = param;
845 omr->messages[omr->msg_tosave].hEvent = INVALID_HANDLE_VALUE;
846 omr->msg_tosave = (omr->msg_tosave + 1) % omr->ring_buffer_size;
848 LeaveCriticalSection(&omr->msg_crst);
849 /* signal a new message */
853 /* wait for playback/record thread to have processed the message */
854 WaitForSingleObject(hEvent, INFINITE);
860 /******************************************************************
861 * ALSA_RetrieveRingMessage
863 * Get a message from the ring. Should be called by the playback/record thread.
865 static int ALSA_RetrieveRingMessage(ALSA_MSG_RING* omr,
866 enum win_wm_message *msg, DWORD *param, HANDLE *hEvent)
868 EnterCriticalSection(&omr->msg_crst);
870 if (omr->msg_toget == omr->msg_tosave) /* buffer empty ? */
872 LeaveCriticalSection(&omr->msg_crst);
876 *msg = omr->messages[omr->msg_toget].msg;
877 omr->messages[omr->msg_toget].msg = 0;
878 *param = omr->messages[omr->msg_toget].param;
879 *hEvent = omr->messages[omr->msg_toget].hEvent;
880 omr->msg_toget = (omr->msg_toget + 1) % omr->ring_buffer_size;
882 LeaveCriticalSection(&omr->msg_crst);
886 /******************************************************************
887 * ALSA_PeekRingMessage
889 * Peek at a message from the ring but do not remove it.
890 * Should be called by the playback/record thread.
892 static int ALSA_PeekRingMessage(ALSA_MSG_RING* omr,
893 enum win_wm_message *msg,
894 DWORD *param, HANDLE *hEvent)
896 EnterCriticalSection(&omr->msg_crst);
898 if (omr->msg_toget == omr->msg_tosave) /* buffer empty ? */
900 LeaveCriticalSection(&omr->msg_crst);
904 *msg = omr->messages[omr->msg_toget].msg;
905 *param = omr->messages[omr->msg_toget].param;
906 *hEvent = omr->messages[omr->msg_toget].hEvent;
907 LeaveCriticalSection(&omr->msg_crst);
911 /*======================================================================*
912 * Low level WAVE OUT implementation *
913 *======================================================================*/
915 /**************************************************************************
916 * wodNotifyClient [internal]
918 static DWORD wodNotifyClient(WINE_WAVEOUT* wwo, WORD wMsg, DWORD dwParam1, DWORD dwParam2)
920 TRACE("wMsg = 0x%04x dwParm1 = %04lX dwParam2 = %04lX\n", wMsg, dwParam1, dwParam2);
926 if (wwo->wFlags != DCB_NULL &&
927 !DriverCallback(wwo->waveDesc.dwCallback, wwo->wFlags, (HDRVR)wwo->waveDesc.hWave,
928 wMsg, wwo->waveDesc.dwInstance, dwParam1, dwParam2)) {
929 WARN("can't notify client !\n");
930 return MMSYSERR_ERROR;
934 FIXME("Unknown callback message %u\n", wMsg);
935 return MMSYSERR_INVALPARAM;
937 return MMSYSERR_NOERROR;
940 /**************************************************************************
941 * wodUpdatePlayedTotal [internal]
944 static BOOL wodUpdatePlayedTotal(WINE_WAVEOUT* wwo, snd_pcm_status_t* ps)
946 snd_pcm_sframes_t delay = 0;
947 snd_pcm_delay(wwo->p_handle, &delay);
948 wwo->dwPlayedTotal = wwo->dwWrittenTotal - delay;
952 /**************************************************************************
953 * wodPlayer_BeginWaveHdr [internal]
955 * Makes the specified lpWaveHdr the currently playing wave header.
956 * If the specified wave header is a begin loop and we're not already in
957 * a loop, setup the loop.
959 static void wodPlayer_BeginWaveHdr(WINE_WAVEOUT* wwo, LPWAVEHDR lpWaveHdr)
961 wwo->lpPlayPtr = lpWaveHdr;
963 if (!lpWaveHdr) return;
965 if (lpWaveHdr->dwFlags & WHDR_BEGINLOOP) {
966 if (wwo->lpLoopPtr) {
967 WARN("Already in a loop. Discarding loop on this header (%p)\n", lpWaveHdr);
969 TRACE("Starting loop (%ldx) with %p\n", lpWaveHdr->dwLoops, lpWaveHdr);
970 wwo->lpLoopPtr = lpWaveHdr;
971 /* Windows does not touch WAVEHDR.dwLoops,
972 * so we need to make an internal copy */
973 wwo->dwLoops = lpWaveHdr->dwLoops;
976 wwo->dwPartialOffset = 0;
979 /**************************************************************************
980 * wodPlayer_PlayPtrNext [internal]
982 * Advance the play pointer to the next waveheader, looping if required.
984 static LPWAVEHDR wodPlayer_PlayPtrNext(WINE_WAVEOUT* wwo)
986 LPWAVEHDR lpWaveHdr = wwo->lpPlayPtr;
988 wwo->dwPartialOffset = 0;
989 if ((lpWaveHdr->dwFlags & WHDR_ENDLOOP) && wwo->lpLoopPtr) {
990 /* We're at the end of a loop, loop if required */
991 if (--wwo->dwLoops > 0) {
992 wwo->lpPlayPtr = wwo->lpLoopPtr;
994 /* Handle overlapping loops correctly */
995 if (wwo->lpLoopPtr != lpWaveHdr && (lpWaveHdr->dwFlags & WHDR_BEGINLOOP)) {
996 FIXME("Correctly handled case ? (ending loop buffer also starts a new loop)\n");
997 /* shall we consider the END flag for the closing loop or for
998 * the opening one or for both ???
999 * code assumes for closing loop only
1002 lpWaveHdr = lpWaveHdr->lpNext;
1004 wwo->lpLoopPtr = NULL;
1005 wodPlayer_BeginWaveHdr(wwo, lpWaveHdr);
1008 /* We're not in a loop. Advance to the next wave header */
1009 wodPlayer_BeginWaveHdr(wwo, lpWaveHdr = lpWaveHdr->lpNext);
1015 /**************************************************************************
1016 * wodPlayer_DSPWait [internal]
1017 * Returns the number of milliseconds to wait for the DSP buffer to play a
1020 static DWORD wodPlayer_DSPWait(const WINE_WAVEOUT *wwo)
1022 /* time for one period to be played */
1023 return snd_pcm_hw_params_get_period_time(wwo->hw_params, 0) / 1000;
1026 /**************************************************************************
1027 * wodPlayer_NotifyWait [internal]
1028 * Returns the number of milliseconds to wait before attempting to notify
1029 * completion of the specified wavehdr.
1030 * This is based on the number of bytes remaining to be written in the
1033 static DWORD wodPlayer_NotifyWait(const WINE_WAVEOUT* wwo, LPWAVEHDR lpWaveHdr)
1037 if (lpWaveHdr->reserved < wwo->dwPlayedTotal) {
1040 dwMillis = (lpWaveHdr->reserved - wwo->dwPlayedTotal) * 1000 / wwo->format.wf.nAvgBytesPerSec;
1041 if (!dwMillis) dwMillis = 1;
1048 /**************************************************************************
1049 * wodPlayer_WriteMaxFrags [internal]
1050 * Writes the maximum number of frames possible to the DSP and returns
1051 * the number of frames written.
1053 static int wodPlayer_WriteMaxFrags(WINE_WAVEOUT* wwo, DWORD* frames)
1055 /* Only attempt to write to free frames */
1056 LPWAVEHDR lpWaveHdr = wwo->lpPlayPtr;
1057 DWORD dwLength = snd_pcm_bytes_to_frames(wwo->p_handle, lpWaveHdr->dwBufferLength - wwo->dwPartialOffset);
1058 int toWrite = min(dwLength, *frames);
1061 TRACE("Writing wavehdr %p.%lu[%lu]\n", lpWaveHdr, wwo->dwPartialOffset, lpWaveHdr->dwBufferLength);
1063 written = (wwo->write)(wwo->p_handle, lpWaveHdr->lpData + wwo->dwPartialOffset, toWrite);
1066 /* XRUN occurred. let's try to recover */
1067 ALSA_XRUNRecovery(wwo, written);
1068 written = (wwo->write)(wwo->p_handle, lpWaveHdr->lpData + wwo->dwPartialOffset, toWrite);
1072 /* still in error */
1073 ERR("Error in writing wavehdr. Reason: %s\n", snd_strerror(written));
1077 wwo->dwPartialOffset += snd_pcm_frames_to_bytes(wwo->p_handle, written);
1078 if ( wwo->dwPartialOffset >= lpWaveHdr->dwBufferLength) {
1079 /* this will be used to check if the given wave header has been fully played or not... */
1080 wwo->dwPartialOffset = lpWaveHdr->dwBufferLength;
1081 /* If we wrote all current wavehdr, skip to the next one */
1082 wodPlayer_PlayPtrNext(wwo);
1085 wwo->dwWrittenTotal += snd_pcm_frames_to_bytes(wwo->p_handle, written);
1091 /**************************************************************************
1092 * wodPlayer_NotifyCompletions [internal]
1094 * Notifies and remove from queue all wavehdrs which have been played to
1095 * the speaker (ie. they have cleared the ALSA buffer). If force is true,
1096 * we notify all wavehdrs and remove them all from the queue even if they
1097 * are unplayed or part of a loop.
1099 static DWORD wodPlayer_NotifyCompletions(WINE_WAVEOUT* wwo, BOOL force)
1101 LPWAVEHDR lpWaveHdr;
1103 /* Start from lpQueuePtr and keep notifying until:
1104 * - we hit an unwritten wavehdr
1105 * - we hit the beginning of a running loop
1106 * - we hit a wavehdr which hasn't finished playing
1109 while ((lpWaveHdr = wwo->lpQueuePtr) &&
1111 (lpWaveHdr != wwo->lpPlayPtr &&
1112 lpWaveHdr != wwo->lpLoopPtr &&
1113 lpWaveHdr->reserved <= wwo->dwPlayedTotal))) {
1115 wwo->lpQueuePtr = lpWaveHdr->lpNext;
1117 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
1118 lpWaveHdr->dwFlags |= WHDR_DONE;
1120 wodNotifyClient(wwo, WOM_DONE, (DWORD)lpWaveHdr, 0);
1125 lpWaveHdr = wwo->lpQueuePtr;
1126 if (!lpWaveHdr) {TRACE("Empty queue\n"); break;}
1129 if (lpWaveHdr == wwo->lpPlayPtr) {TRACE("play %p\n", lpWaveHdr); break;}
1130 if (lpWaveHdr == wwo->lpLoopPtr) {TRACE("loop %p\n", lpWaveHdr); break;}
1131 if (lpWaveHdr->reserved > wwo->dwPlayedTotal){TRACE("still playing %p (%lu/%lu)\n", lpWaveHdr, lpWaveHdr->reserved, wwo->dwPlayedTotal);break;}
1133 wwo->lpQueuePtr = lpWaveHdr->lpNext;
1135 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
1136 lpWaveHdr->dwFlags |= WHDR_DONE;
1138 wodNotifyClient(wwo, WOM_DONE, (DWORD)lpWaveHdr, 0);
1141 return (lpWaveHdr && lpWaveHdr != wwo->lpPlayPtr && lpWaveHdr != wwo->lpLoopPtr) ?
1142 wodPlayer_NotifyWait(wwo, lpWaveHdr) : INFINITE;
1146 void wait_for_poll(snd_pcm_t *handle, struct pollfd *ufds, unsigned int count)
1148 unsigned short revents;
1150 if (snd_pcm_state(handle) != SND_PCM_STATE_RUNNING)
1154 poll(ufds, count, -1);
1155 snd_pcm_poll_descriptors_revents(handle, ufds, count, &revents);
1157 if (revents & POLLERR)
1160 /*if (revents & POLLOUT)
1166 /**************************************************************************
1167 * wodPlayer_Reset [internal]
1169 * wodPlayer helper. Resets current output stream.
1171 static void wodPlayer_Reset(WINE_WAVEOUT* wwo)
1173 enum win_wm_message msg;
1178 /* flush all possible output */
1179 wait_for_poll(wwo->p_handle, wwo->ufds, wwo->count);
1181 wodUpdatePlayedTotal(wwo, NULL);
1182 /* updates current notify list */
1183 wodPlayer_NotifyCompletions(wwo, FALSE);
1185 if ( (err = snd_pcm_drop(wwo->p_handle)) < 0) {
1186 FIXME("flush: %s\n", snd_strerror(err));
1188 wwo->state = WINE_WS_STOPPED;
1191 if ( (err = snd_pcm_prepare(wwo->p_handle)) < 0 )
1192 ERR("pcm prepare failed: %s\n", snd_strerror(err));
1194 /* remove any buffer */
1195 wodPlayer_NotifyCompletions(wwo, TRUE);
1197 wwo->lpPlayPtr = wwo->lpQueuePtr = wwo->lpLoopPtr = NULL;
1198 wwo->state = WINE_WS_STOPPED;
1199 wwo->dwPlayedTotal = wwo->dwWrittenTotal = 0;
1200 /* Clear partial wavehdr */
1201 wwo->dwPartialOffset = 0;
1203 /* remove any existing message in the ring */
1204 EnterCriticalSection(&wwo->msgRing.msg_crst);
1205 /* return all pending headers in queue */
1206 while (ALSA_RetrieveRingMessage(&wwo->msgRing, &msg, ¶m, &ev))
1208 if (msg != WINE_WM_HEADER)
1210 FIXME("shouldn't have headers left\n");
1214 ((LPWAVEHDR)param)->dwFlags &= ~WHDR_INQUEUE;
1215 ((LPWAVEHDR)param)->dwFlags |= WHDR_DONE;
1217 wodNotifyClient(wwo, WOM_DONE, param, 0);
1219 RESET_OMR(&wwo->msgRing);
1220 LeaveCriticalSection(&wwo->msgRing.msg_crst);
1223 /**************************************************************************
1224 * wodPlayer_ProcessMessages [internal]
1226 static void wodPlayer_ProcessMessages(WINE_WAVEOUT* wwo)
1228 LPWAVEHDR lpWaveHdr;
1229 enum win_wm_message msg;
1234 while (ALSA_RetrieveRingMessage(&wwo->msgRing, &msg, ¶m, &ev)) {
1235 TRACE("Received %s %lx\n", wodPlayerCmdString[msg - WM_USER - 1], param);
1238 case WINE_WM_PAUSING:
1239 if ( snd_pcm_state(wwo->p_handle) == SND_PCM_STATE_RUNNING )
1241 err = snd_pcm_pause(wwo->p_handle, 1);
1243 ERR("pcm_pause failed: %s\n", snd_strerror(err));
1245 wwo->state = WINE_WS_PAUSED;
1248 case WINE_WM_RESTARTING:
1249 if (wwo->state == WINE_WS_PAUSED)
1251 if ( snd_pcm_state(wwo->p_handle) == SND_PCM_STATE_PAUSED )
1253 err = snd_pcm_pause(wwo->p_handle, 0);
1255 ERR("pcm_pause failed: %s\n", snd_strerror(err));
1257 wwo->state = WINE_WS_PLAYING;
1261 case WINE_WM_HEADER:
1262 lpWaveHdr = (LPWAVEHDR)param;
1264 /* insert buffer at the end of queue */
1267 for (wh = &(wwo->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
1270 if (!wwo->lpPlayPtr)
1271 wodPlayer_BeginWaveHdr(wwo,lpWaveHdr);
1272 if (wwo->state == WINE_WS_STOPPED)
1273 wwo->state = WINE_WS_PLAYING;
1275 case WINE_WM_RESETTING:
1276 wodPlayer_Reset(wwo);
1279 case WINE_WM_UPDATE:
1280 wodUpdatePlayedTotal(wwo, NULL);
1283 case WINE_WM_BREAKLOOP:
1284 if (wwo->state == WINE_WS_PLAYING && wwo->lpLoopPtr != NULL) {
1285 /* ensure exit at end of current loop */
1290 case WINE_WM_CLOSING:
1291 /* sanity check: this should not happen since the device must have been reset before */
1292 if (wwo->lpQueuePtr || wwo->lpPlayPtr) ERR("out of sync\n");
1294 wwo->state = WINE_WS_CLOSED;
1297 /* shouldn't go here */
1299 FIXME("unknown message %d\n", msg);
1305 /**************************************************************************
1306 * wodPlayer_FeedDSP [internal]
1307 * Feed as much sound data as we can into the DSP and return the number of
1308 * milliseconds before it will be necessary to feed the DSP again.
1310 static DWORD wodPlayer_FeedDSP(WINE_WAVEOUT* wwo)
1314 wodUpdatePlayedTotal(wwo, NULL);
1315 availInQ = snd_pcm_avail_update(wwo->p_handle);
1318 /* input queue empty and output buffer with less than one fragment to play */
1319 if (!wwo->lpPlayPtr && wwo->dwBufferSize < availInQ + wwo->dwFragmentSize) {
1320 TRACE("Run out of wavehdr:s...\n");
1325 /* no more room... no need to try to feed */
1327 /* Feed from partial wavehdr */
1328 if (wwo->lpPlayPtr && wwo->dwPartialOffset != 0) {
1329 wodPlayer_WriteMaxFrags(wwo, &availInQ);
1332 /* Feed wavehdrs until we run out of wavehdrs or DSP space */
1333 if (wwo->dwPartialOffset == 0 && wwo->lpPlayPtr) {
1335 TRACE("Setting time to elapse for %p to %lu\n",
1336 wwo->lpPlayPtr, wwo->dwWrittenTotal + wwo->lpPlayPtr->dwBufferLength);
1337 /* note the value that dwPlayedTotal will return when this wave finishes playing */
1338 wwo->lpPlayPtr->reserved = wwo->dwWrittenTotal + wwo->lpPlayPtr->dwBufferLength;
1339 } while (wodPlayer_WriteMaxFrags(wwo, &availInQ) && wwo->lpPlayPtr && availInQ > 0);
1343 return wodPlayer_DSPWait(wwo);
1346 /**************************************************************************
1347 * wodPlayer [internal]
1349 static DWORD CALLBACK wodPlayer(LPVOID pmt)
1351 WORD uDevID = (DWORD)pmt;
1352 WINE_WAVEOUT* wwo = (WINE_WAVEOUT*)&WOutDev[uDevID];
1353 DWORD dwNextFeedTime = INFINITE; /* Time before DSP needs feeding */
1354 DWORD dwNextNotifyTime = INFINITE; /* Time before next wave completion */
1357 wwo->state = WINE_WS_STOPPED;
1358 SetEvent(wwo->hStartUpEvent);
1361 /** Wait for the shortest time before an action is required. If there
1362 * are no pending actions, wait forever for a command.
1364 dwSleepTime = min(dwNextFeedTime, dwNextNotifyTime);
1365 TRACE("waiting %lums (%lu,%lu)\n", dwSleepTime, dwNextFeedTime, dwNextNotifyTime);
1366 WAIT_OMR(&wwo->msgRing, dwSleepTime);
1367 wodPlayer_ProcessMessages(wwo);
1368 if (wwo->state == WINE_WS_PLAYING) {
1369 dwNextFeedTime = wodPlayer_FeedDSP(wwo);
1370 dwNextNotifyTime = wodPlayer_NotifyCompletions(wwo, FALSE);
1371 if (dwNextFeedTime == INFINITE) {
1372 /* FeedDSP ran out of data, but before giving up, */
1373 /* check that a notification didn't give us more */
1374 wodPlayer_ProcessMessages(wwo);
1375 if (wwo->lpPlayPtr) {
1376 TRACE("recovering\n");
1377 dwNextFeedTime = wodPlayer_FeedDSP(wwo);
1381 dwNextFeedTime = dwNextNotifyTime = INFINITE;
1386 /**************************************************************************
1387 * wodGetDevCaps [internal]
1389 static DWORD wodGetDevCaps(WORD wDevID, LPWAVEOUTCAPSA lpCaps, DWORD dwSize)
1391 TRACE("(%u, %p, %lu);\n", wDevID, lpCaps, dwSize);
1393 if (lpCaps == NULL) return MMSYSERR_NOTENABLED;
1395 if (wDevID >= MAX_WAVEOUTDRV) {
1396 TRACE("MAX_WAVOUTDRV reached !\n");
1397 return MMSYSERR_BADDEVICEID;
1400 memcpy(lpCaps, &WOutDev[wDevID].caps, min(dwSize, sizeof(*lpCaps)));
1401 return MMSYSERR_NOERROR;
1404 /**************************************************************************
1405 * wodOpen [internal]
1407 static DWORD wodOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
1410 snd_pcm_hw_params_t * hw_params;
1411 snd_pcm_sw_params_t * sw_params;
1412 snd_pcm_access_t access;
1413 snd_pcm_format_t format;
1415 unsigned int buffer_time = 500000;
1416 unsigned int period_time = 10000;
1418 snd_pcm_uframes_t period_size;
1423 snd_pcm_hw_params_alloca(&hw_params);
1424 snd_pcm_sw_params_alloca(&sw_params);
1426 TRACE("(%u, %p, %08lX);\n", wDevID, lpDesc, dwFlags);
1427 if (lpDesc == NULL) {
1428 WARN("Invalid Parameter !\n");
1429 return MMSYSERR_INVALPARAM;
1431 if (wDevID >= MAX_WAVEOUTDRV) {
1432 TRACE("MAX_WAVOUTDRV reached !\n");
1433 return MMSYSERR_BADDEVICEID;
1436 /* only PCM format is supported so far... */
1437 if (lpDesc->lpFormat->wFormatTag != WAVE_FORMAT_PCM ||
1438 lpDesc->lpFormat->nChannels == 0 ||
1439 lpDesc->lpFormat->nSamplesPerSec == 0 ||
1440 (lpDesc->lpFormat->wBitsPerSample!=8 && lpDesc->lpFormat->wBitsPerSample!=16)) {
1441 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
1442 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1443 lpDesc->lpFormat->nSamplesPerSec);
1444 return WAVERR_BADFORMAT;
1447 if (dwFlags & WAVE_FORMAT_QUERY) {
1448 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
1449 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1450 lpDesc->lpFormat->nSamplesPerSec);
1451 return MMSYSERR_NOERROR;
1454 wwo = &WOutDev[wDevID];
1456 if ((dwFlags & WAVE_DIRECTSOUND) && !(wwo->caps.dwSupport & WAVECAPS_DIRECTSOUND))
1457 /* not supported, ignore it */
1458 dwFlags &= ~WAVE_DIRECTSOUND;
1461 flags = SND_PCM_NONBLOCK;
1462 if ( dwFlags & WAVE_DIRECTSOUND )
1463 flags |= SND_PCM_ASYNC;
1465 if ( (err = snd_pcm_open(&pcm, wwo->device, SND_PCM_STREAM_PLAYBACK, flags)) < 0)
1467 ERR("Error open: %s\n", snd_strerror(err));
1468 return MMSYSERR_NOTENABLED;
1471 wwo->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
1473 memcpy(&wwo->waveDesc, lpDesc, sizeof(WAVEOPENDESC));
1474 memcpy(&wwo->format, lpDesc->lpFormat, sizeof(PCMWAVEFORMAT));
1476 if (wwo->format.wBitsPerSample == 0) {
1477 WARN("Resetting zeroed wBitsPerSample\n");
1478 wwo->format.wBitsPerSample = 8 *
1479 (wwo->format.wf.nAvgBytesPerSec /
1480 wwo->format.wf.nSamplesPerSec) /
1481 wwo->format.wf.nChannels;
1484 snd_pcm_hw_params_any(pcm, hw_params);
1486 #define EXIT_ON_ERROR(f,e,txt) do \
1489 if ( (err = (f) ) < 0) \
1491 ERR(txt ": %s\n", snd_strerror(err)); \
1492 snd_pcm_close(pcm); \
1497 access = SND_PCM_ACCESS_MMAP_INTERLEAVED;
1498 if ( ( err = snd_pcm_hw_params_set_access(pcm, hw_params, access ) ) < 0) {
1499 WARN("mmap not available. switching to standard write.\n");
1500 access = SND_PCM_ACCESS_RW_INTERLEAVED;
1501 EXIT_ON_ERROR( snd_pcm_hw_params_set_access(pcm, hw_params, access ), MMSYSERR_INVALPARAM, "unable to set access for playback");
1502 wwo->write = snd_pcm_writei;
1505 wwo->write = snd_pcm_mmap_writei;
1507 EXIT_ON_ERROR( snd_pcm_hw_params_set_channels(pcm, hw_params, wwo->format.wf.nChannels), MMSYSERR_INVALPARAM, "unable to set required channels");
1509 format = (wwo->format.wBitsPerSample == 16) ? SND_PCM_FORMAT_S16_LE : SND_PCM_FORMAT_U8;
1510 EXIT_ON_ERROR( snd_pcm_hw_params_set_format(pcm, hw_params, format), MMSYSERR_INVALPARAM, "unable to set required format");
1512 rate = snd_pcm_hw_params_set_rate_near(pcm, hw_params, wwo->format.wf.nSamplesPerSec, 0);
1514 ERR("Rate %ld Hz not available for playback: %s\n", wwo->format.wf.nSamplesPerSec, snd_strerror(rate));
1516 return WAVERR_BADFORMAT;
1518 if (rate != wwo->format.wf.nSamplesPerSec) {
1519 ERR("Rate doesn't match (requested %ld Hz, got %d Hz)\n", wwo->format.wf.nSamplesPerSec, rate);
1521 return WAVERR_BADFORMAT;
1524 EXIT_ON_ERROR( snd_pcm_hw_params_set_buffer_time_near(pcm, hw_params, buffer_time, 0), MMSYSERR_INVALPARAM, "unable to set buffer time");
1525 EXIT_ON_ERROR( snd_pcm_hw_params_set_period_time_near(pcm, hw_params, period_time, 0), MMSYSERR_INVALPARAM, "unable to set period time");
1527 EXIT_ON_ERROR( snd_pcm_hw_params(pcm, hw_params), MMSYSERR_INVALPARAM, "unable to set hw params for playback");
1529 period_size = snd_pcm_hw_params_get_period_size(hw_params, 0);
1530 buffer_size = snd_pcm_hw_params_get_buffer_size(hw_params);
1532 snd_pcm_sw_params_current(pcm, sw_params);
1533 EXIT_ON_ERROR( snd_pcm_sw_params_set_start_threshold(pcm, sw_params, dwFlags & WAVE_DIRECTSOUND ? INT_MAX : 1 ), MMSYSERR_ERROR, "unable to set start threshold");
1534 EXIT_ON_ERROR( snd_pcm_sw_params_set_silence_size(pcm, sw_params, 0), MMSYSERR_ERROR, "unable to set silence size");
1535 EXIT_ON_ERROR( snd_pcm_sw_params_set_avail_min(pcm, sw_params, period_size), MMSYSERR_ERROR, "unable to set avail min");
1536 EXIT_ON_ERROR( snd_pcm_sw_params_set_xfer_align(pcm, sw_params, 1), MMSYSERR_ERROR, "unable to set xfer align");
1537 EXIT_ON_ERROR( snd_pcm_sw_params_set_silence_threshold(pcm, sw_params, 0), MMSYSERR_ERROR, "unable to set silence threshold");
1538 EXIT_ON_ERROR( snd_pcm_sw_params(pcm, sw_params), MMSYSERR_ERROR, "unable to set sw params for playback");
1539 #undef EXIT_ON_ERROR
1541 snd_pcm_prepare(pcm);
1544 ALSA_TraceParameters(hw_params, sw_params, FALSE);
1546 /* now, we can save all required data for later use... */
1547 if ( wwo->hw_params )
1548 snd_pcm_hw_params_free(wwo->hw_params);
1549 snd_pcm_hw_params_malloc(&(wwo->hw_params));
1550 snd_pcm_hw_params_copy(wwo->hw_params, hw_params);
1552 wwo->dwBufferSize = buffer_size;
1553 wwo->lpQueuePtr = wwo->lpPlayPtr = wwo->lpLoopPtr = NULL;
1554 wwo->p_handle = pcm;
1556 ALSA_InitRingMessage(&wwo->msgRing);
1558 wwo->count = snd_pcm_poll_descriptors_count (wwo->p_handle);
1559 if (wwo->count <= 0) {
1560 ERR("Invalid poll descriptors count\n");
1561 return MMSYSERR_ERROR;
1564 wwo->ufds = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY, sizeof(struct pollfd) * wwo->count);
1565 if (wwo->ufds == NULL) {
1566 ERR("No enough memory\n");
1567 return MMSYSERR_NOMEM;
1569 if ((err = snd_pcm_poll_descriptors(wwo->p_handle, wwo->ufds, wwo->count)) < 0) {
1570 ERR("Unable to obtain poll descriptors for playback: %s\n", snd_strerror(err));
1571 return MMSYSERR_ERROR;
1574 if (!(dwFlags & WAVE_DIRECTSOUND)) {
1575 wwo->hStartUpEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
1576 wwo->hThread = CreateThread(NULL, 0, wodPlayer, (LPVOID)(DWORD)wDevID, 0, &(wwo->dwThreadID));
1577 WaitForSingleObject(wwo->hStartUpEvent, INFINITE);
1578 CloseHandle(wwo->hStartUpEvent);
1580 wwo->hThread = INVALID_HANDLE_VALUE;
1581 wwo->dwThreadID = 0;
1583 wwo->hStartUpEvent = INVALID_HANDLE_VALUE;
1585 TRACE("handle=%08lx \n", (DWORD)wwo->p_handle);
1586 /* if (wwo->dwFragmentSize % wwo->format.wf.nBlockAlign)
1587 ERR("Fragment doesn't contain an integral number of data blocks\n");
1589 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%lu, nSamplesPerSec=%lu, nChannels=%u nBlockAlign=%u!\n",
1590 wwo->format.wBitsPerSample, wwo->format.wf.nAvgBytesPerSec,
1591 wwo->format.wf.nSamplesPerSec, wwo->format.wf.nChannels,
1592 wwo->format.wf.nBlockAlign);
1594 return wodNotifyClient(wwo, WOM_OPEN, 0L, 0L);
1598 /**************************************************************************
1599 * wodClose [internal]
1601 static DWORD wodClose(WORD wDevID)
1603 DWORD ret = MMSYSERR_NOERROR;
1606 TRACE("(%u);\n", wDevID);
1608 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].p_handle == NULL) {
1609 WARN("bad device ID !\n");
1610 return MMSYSERR_BADDEVICEID;
1613 wwo = &WOutDev[wDevID];
1614 if (wwo->lpQueuePtr) {
1615 WARN("buffers still playing !\n");
1616 ret = WAVERR_STILLPLAYING;
1618 if (wwo->hThread != INVALID_HANDLE_VALUE) {
1619 ALSA_AddRingMessage(&wwo->msgRing, WINE_WM_CLOSING, 0, TRUE);
1621 ALSA_DestroyRingMessage(&wwo->msgRing);
1623 snd_pcm_hw_params_free(wwo->hw_params);
1624 wwo->hw_params = NULL;
1626 snd_pcm_close(wwo->p_handle);
1627 wwo->p_handle = NULL;
1629 ret = wodNotifyClient(wwo, WOM_CLOSE, 0L, 0L);
1632 HeapFree(GetProcessHeap(), 0, wwo->ufds);
1637 /**************************************************************************
1638 * wodWrite [internal]
1641 static DWORD wodWrite(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1643 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1645 /* first, do the sanity checks... */
1646 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].p_handle == NULL) {
1647 WARN("bad dev ID !\n");
1648 return MMSYSERR_BADDEVICEID;
1651 if (lpWaveHdr->lpData == NULL || !(lpWaveHdr->dwFlags & WHDR_PREPARED))
1652 return WAVERR_UNPREPARED;
1654 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1655 return WAVERR_STILLPLAYING;
1657 lpWaveHdr->dwFlags &= ~WHDR_DONE;
1658 lpWaveHdr->dwFlags |= WHDR_INQUEUE;
1659 lpWaveHdr->lpNext = 0;
1661 ALSA_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_HEADER, (DWORD)lpWaveHdr, FALSE);
1663 return MMSYSERR_NOERROR;
1666 /**************************************************************************
1667 * wodPrepare [internal]
1669 static DWORD wodPrepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1671 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1673 if (wDevID >= MAX_WAVEOUTDRV) {
1674 WARN("bad device ID !\n");
1675 return MMSYSERR_BADDEVICEID;
1678 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1679 return WAVERR_STILLPLAYING;
1681 lpWaveHdr->dwFlags |= WHDR_PREPARED;
1682 lpWaveHdr->dwFlags &= ~WHDR_DONE;
1683 return MMSYSERR_NOERROR;
1686 /**************************************************************************
1687 * wodUnprepare [internal]
1689 static DWORD wodUnprepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1691 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1693 if (wDevID >= MAX_WAVEOUTDRV) {
1694 WARN("bad device ID !\n");
1695 return MMSYSERR_BADDEVICEID;
1698 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1699 return WAVERR_STILLPLAYING;
1701 lpWaveHdr->dwFlags &= ~WHDR_PREPARED;
1702 lpWaveHdr->dwFlags |= WHDR_DONE;
1704 return MMSYSERR_NOERROR;
1707 /**************************************************************************
1708 * wodPause [internal]
1710 static DWORD wodPause(WORD wDevID)
1712 TRACE("(%u);!\n", wDevID);
1714 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].p_handle == NULL) {
1715 WARN("bad device ID !\n");
1716 return MMSYSERR_BADDEVICEID;
1719 ALSA_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_PAUSING, 0, TRUE);
1721 return MMSYSERR_NOERROR;
1724 /**************************************************************************
1725 * wodRestart [internal]
1727 static DWORD wodRestart(WORD wDevID)
1729 TRACE("(%u);\n", wDevID);
1731 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].p_handle == NULL) {
1732 WARN("bad device ID !\n");
1733 return MMSYSERR_BADDEVICEID;
1736 if (WOutDev[wDevID].state == WINE_WS_PAUSED) {
1737 ALSA_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESTARTING, 0, TRUE);
1740 /* FIXME: is NotifyClient with WOM_DONE right ? (Comet Busters 1.3.3 needs this notification) */
1741 /* FIXME: Myst crashes with this ... hmm -MM
1742 return wodNotifyClient(wwo, WOM_DONE, 0L, 0L);
1745 return MMSYSERR_NOERROR;
1748 /**************************************************************************
1749 * wodReset [internal]
1751 static DWORD wodReset(WORD wDevID)
1753 TRACE("(%u);\n", wDevID);
1755 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].p_handle == NULL) {
1756 WARN("bad device ID !\n");
1757 return MMSYSERR_BADDEVICEID;
1760 ALSA_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESETTING, 0, TRUE);
1762 return MMSYSERR_NOERROR;
1765 /**************************************************************************
1766 * wodGetPosition [internal]
1768 static DWORD wodGetPosition(WORD wDevID, LPMMTIME lpTime, DWORD uSize)
1774 TRACE("(%u, %p, %lu);\n", wDevID, lpTime, uSize);
1776 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].p_handle == NULL) {
1777 WARN("bad device ID !\n");
1778 return MMSYSERR_BADDEVICEID;
1781 if (lpTime == NULL) return MMSYSERR_INVALPARAM;
1783 wwo = &WOutDev[wDevID];
1784 ALSA_AddRingMessage(&wwo->msgRing, WINE_WM_UPDATE, 0, TRUE);
1785 val = wwo->dwPlayedTotal;
1787 TRACE("wType=%04X wBitsPerSample=%u nSamplesPerSec=%lu nChannels=%u nAvgBytesPerSec=%lu\n",
1788 lpTime->wType, wwo->format.wBitsPerSample,
1789 wwo->format.wf.nSamplesPerSec, wwo->format.wf.nChannels,
1790 wwo->format.wf.nAvgBytesPerSec);
1791 TRACE("dwPlayedTotal=%lu\n", val);
1793 switch (lpTime->wType) {
1796 TRACE("TIME_BYTES=%lu\n", lpTime->u.cb);
1799 lpTime->u.sample = val * 8 / wwo->format.wBitsPerSample /wwo->format.wf.nChannels;
1800 TRACE("TIME_SAMPLES=%lu\n", lpTime->u.sample);
1803 time = val / (wwo->format.wf.nAvgBytesPerSec / 1000);
1804 lpTime->u.smpte.hour = time / (60 * 60 * 1000);
1805 time -= lpTime->u.smpte.hour * (60 * 60 * 1000);
1806 lpTime->u.smpte.min = time / (60 * 1000);
1807 time -= lpTime->u.smpte.min * (60 * 1000);
1808 lpTime->u.smpte.sec = time / 1000;
1809 time -= lpTime->u.smpte.sec * 1000;
1810 lpTime->u.smpte.frame = time * 30 / 1000;
1811 lpTime->u.smpte.fps = 30;
1812 TRACE("TIME_SMPTE=%02u:%02u:%02u:%02u\n",
1813 lpTime->u.smpte.hour, lpTime->u.smpte.min,
1814 lpTime->u.smpte.sec, lpTime->u.smpte.frame);
1817 FIXME("Format %d not supported ! use TIME_MS !\n", lpTime->wType);
1818 lpTime->wType = TIME_MS;
1820 lpTime->u.ms = val / (wwo->format.wf.nAvgBytesPerSec / 1000);
1821 TRACE("TIME_MS=%lu\n", lpTime->u.ms);
1824 return MMSYSERR_NOERROR;
1827 /**************************************************************************
1828 * wodBreakLoop [internal]
1830 static DWORD wodBreakLoop(WORD wDevID)
1832 TRACE("(%u);\n", wDevID);
1834 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].p_handle == NULL) {
1835 WARN("bad device ID !\n");
1836 return MMSYSERR_BADDEVICEID;
1838 ALSA_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_BREAKLOOP, 0, TRUE);
1839 return MMSYSERR_NOERROR;
1842 /**************************************************************************
1843 * wodGetVolume [internal]
1845 static DWORD wodGetVolume(WORD wDevID, LPDWORD lpdwVol)
1852 TRACE("(%u, %p);\n", wDevID, lpdwVol);
1853 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].p_handle == NULL) {
1854 WARN("bad device ID !\n");
1855 return MMSYSERR_BADDEVICEID;
1857 wwo = &WOutDev[wDevID];
1858 count = snd_ctl_elem_info_get_count(wwo->playback_einfo);
1859 min = snd_ctl_elem_info_get_min(wwo->playback_einfo);
1860 max = snd_ctl_elem_info_get_max(wwo->playback_einfo);
1862 #define VOLUME_ALSA_TO_WIN(x) (((x)-min) * 65536 /(max-min))
1863 if (lpdwVol == NULL)
1864 return MMSYSERR_NOTENABLED;
1869 left = VOLUME_ALSA_TO_WIN(snd_ctl_elem_value_get_integer(wwo->playback_evalue, 0));
1870 right = VOLUME_ALSA_TO_WIN(snd_ctl_elem_value_get_integer(wwo->playback_evalue, 1));
1873 left = right = VOLUME_ALSA_TO_WIN(snd_ctl_elem_value_get_integer(wwo->playback_evalue, 0));
1876 WARN("%d channels mixer not supported\n", count);
1877 return MMSYSERR_NOERROR;
1879 #undef VOLUME_ALSA_TO_WIN
1881 TRACE("left=%d right=%d !\n", left, right);
1882 *lpdwVol = MAKELONG( left, right );
1883 return MMSYSERR_NOERROR;
1886 /**************************************************************************
1887 * wodSetVolume [internal]
1889 static DWORD wodSetVolume(WORD wDevID, DWORD dwParam)
1896 TRACE("(%u, %08lX);\n", wDevID, dwParam);
1897 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].p_handle == NULL) {
1898 WARN("bad device ID !\n");
1899 return MMSYSERR_BADDEVICEID;
1901 wwo = &WOutDev[wDevID];
1902 count=snd_ctl_elem_info_get_count(wwo->playback_einfo);
1903 min = snd_ctl_elem_info_get_min(wwo->playback_einfo);
1904 max = snd_ctl_elem_info_get_max(wwo->playback_einfo);
1906 left = LOWORD(dwParam);
1907 right = HIWORD(dwParam);
1909 #define VOLUME_WIN_TO_ALSA(x) ( (((x) * (max-min)) / 65536) + min )
1913 snd_ctl_elem_value_set_integer(wwo->playback_evalue, 0, VOLUME_WIN_TO_ALSA(left));
1914 snd_ctl_elem_value_set_integer(wwo->playback_evalue, 1, VOLUME_WIN_TO_ALSA(right));
1917 snd_ctl_elem_value_set_integer(wwo->playback_evalue, 0, VOLUME_WIN_TO_ALSA(left));
1920 WARN("%d channels mixer not supported\n", count);
1922 #undef VOLUME_WIN_TO_ALSA
1923 if ( (err = snd_ctl_elem_write(wwo->ctl, wwo->playback_evalue)) < 0)
1925 ERR("error writing snd_ctl_elem_value: %s\n", snd_strerror(err));
1927 return MMSYSERR_NOERROR;
1930 /**************************************************************************
1931 * wodGetNumDevs [internal]
1933 static DWORD wodGetNumDevs(void)
1935 return ALSA_WodNumDevs;
1938 /**************************************************************************
1939 * wodDevInterfaceSize [internal]
1941 static DWORD wodDevInterfaceSize(UINT wDevID, LPDWORD dwParam1)
1943 TRACE("(%u, %p)\n", wDevID, dwParam1);
1945 *dwParam1 = MultiByteToWideChar(CP_ACP, 0, WOutDev[wDevID].device, -1,
1946 NULL, 0 ) * sizeof(WCHAR);
1947 return MMSYSERR_NOERROR;
1950 /**************************************************************************
1951 * wodDevInterface [internal]
1953 static DWORD wodDevInterface(UINT wDevID, PWCHAR dwParam1, DWORD dwParam2)
1955 if (dwParam2 >= MultiByteToWideChar(CP_ACP, 0, WOutDev[wDevID].device, -1,
1956 NULL, 0 ) * sizeof(WCHAR))
1958 MultiByteToWideChar(CP_ACP, 0, WOutDev[wDevID].device, -1,
1959 dwParam1, dwParam2 / sizeof(WCHAR));
1960 return MMSYSERR_NOERROR;
1962 return MMSYSERR_INVALPARAM;
1965 /**************************************************************************
1966 * wodMessage (WINEALSA.@)
1968 DWORD WINAPI ALSA_wodMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
1969 DWORD dwParam1, DWORD dwParam2)
1971 TRACE("(%u, %04X, %08lX, %08lX, %08lX);\n",
1972 wDevID, wMsg, dwUser, dwParam1, dwParam2);
1979 /* FIXME: Pretend this is supported */
1981 case WODM_OPEN: return wodOpen (wDevID, (LPWAVEOPENDESC)dwParam1, dwParam2);
1982 case WODM_CLOSE: return wodClose (wDevID);
1983 case WODM_GETDEVCAPS: return wodGetDevCaps (wDevID, (LPWAVEOUTCAPSA)dwParam1, dwParam2);
1984 case WODM_GETNUMDEVS: return wodGetNumDevs ();
1985 case WODM_GETPITCH: return MMSYSERR_NOTSUPPORTED;
1986 case WODM_SETPITCH: return MMSYSERR_NOTSUPPORTED;
1987 case WODM_GETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
1988 case WODM_SETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
1989 case WODM_WRITE: return wodWrite (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
1990 case WODM_PAUSE: return wodPause (wDevID);
1991 case WODM_GETPOS: return wodGetPosition (wDevID, (LPMMTIME)dwParam1, dwParam2);
1992 case WODM_BREAKLOOP: return wodBreakLoop (wDevID);
1993 case WODM_PREPARE: return wodPrepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
1994 case WODM_UNPREPARE: return wodUnprepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
1995 case WODM_GETVOLUME: return wodGetVolume (wDevID, (LPDWORD)dwParam1);
1996 case WODM_SETVOLUME: return wodSetVolume (wDevID, dwParam1);
1997 case WODM_RESTART: return wodRestart (wDevID);
1998 case WODM_RESET: return wodReset (wDevID);
1999 case DRV_QUERYDEVICEINTERFACESIZE: return wodDevInterfaceSize (wDevID, (LPDWORD)dwParam1);
2000 case DRV_QUERYDEVICEINTERFACE: return wodDevInterface (wDevID, (PWCHAR)dwParam1, dwParam2);
2001 case DRV_QUERYDSOUNDIFACE: return wodDsCreate (wDevID, (PIDSDRIVER*)dwParam1);
2002 case DRV_QUERYDSOUNDDESC: return wodDsDesc (wDevID, (PDSDRIVERDESC)dwParam1);
2003 case DRV_QUERYDSOUNDGUID: return wodDsGuid (wDevID, (LPGUID)dwParam1);
2006 FIXME("unknown message %d!\n", wMsg);
2008 return MMSYSERR_NOTSUPPORTED;
2011 /*======================================================================*
2012 * Low level DSOUND implementation *
2013 *======================================================================*/
2015 typedef struct IDsDriverImpl IDsDriverImpl;
2016 typedef struct IDsDriverBufferImpl IDsDriverBufferImpl;
2018 struct IDsDriverImpl
2020 /* IUnknown fields */
2021 ICOM_VFIELD(IDsDriver);
2023 /* IDsDriverImpl fields */
2025 IDsDriverBufferImpl*primary;
2028 struct IDsDriverBufferImpl
2030 /* IUnknown fields */
2031 ICOM_VFIELD(IDsDriverBuffer);
2033 /* IDsDriverBufferImpl fields */
2036 CRITICAL_SECTION mmap_crst;
2038 DWORD mmap_buflen_bytes;
2039 snd_pcm_uframes_t mmap_buflen_frames;
2040 snd_pcm_channel_area_t * mmap_areas;
2041 snd_async_handler_t * mmap_async_handler;
2044 static void DSDB_CheckXRUN(IDsDriverBufferImpl* pdbi)
2046 WINE_WAVEOUT * wwo = &(WOutDev[pdbi->drv->wDevID]);
2047 snd_pcm_state_t state = snd_pcm_state(wwo->p_handle);
2049 if ( state == SND_PCM_STATE_XRUN )
2051 int err = snd_pcm_prepare(wwo->p_handle);
2052 TRACE("xrun occurred\n");
2054 ERR("recovery from xrun failed, prepare failed: %s\n", snd_strerror(err));
2056 else if ( state == SND_PCM_STATE_SUSPENDED )
2058 int err = snd_pcm_resume(wwo->p_handle);
2059 TRACE("recovery from suspension occurred\n");
2060 if (err < 0 && err != -EAGAIN){
2061 err = snd_pcm_prepare(wwo->p_handle);
2063 ERR("recovery from suspend failed, prepare failed: %s\n", snd_strerror(err));
2068 static void DSDB_MMAPCopy(IDsDriverBufferImpl* pdbi)
2070 WINE_WAVEOUT * wwo = &(WOutDev[pdbi->drv->wDevID]);
2072 snd_pcm_format_t format;
2073 snd_pcm_uframes_t period_size;
2074 snd_pcm_sframes_t avail;
2076 if ( !pdbi->mmap_buffer || !wwo->hw_params || !wwo->p_handle)
2079 channels = snd_pcm_hw_params_get_channels(wwo->hw_params);
2080 format = snd_pcm_hw_params_get_format(wwo->hw_params);
2081 period_size = snd_pcm_hw_params_get_period_size(wwo->hw_params, 0);
2082 avail = snd_pcm_avail_update(wwo->p_handle);
2084 DSDB_CheckXRUN(pdbi);
2086 TRACE("avail=%d format=%s channels=%d\n", (int)avail, snd_pcm_format_name(format), channels );
2088 while (avail >= period_size)
2090 const snd_pcm_channel_area_t *areas;
2091 snd_pcm_uframes_t ofs;
2092 snd_pcm_uframes_t frames;
2095 frames = avail / period_size * period_size; /* round down to a multiple of period_size */
2097 EnterCriticalSection(&pdbi->mmap_crst);
2099 snd_pcm_mmap_begin(wwo->p_handle, &areas, &ofs, &frames);
2100 snd_pcm_areas_copy(areas, ofs, pdbi->mmap_areas, ofs, channels, frames, format);
2101 err = snd_pcm_mmap_commit(wwo->p_handle, ofs, frames);
2103 LeaveCriticalSection(&pdbi->mmap_crst);
2105 if ( err != (snd_pcm_sframes_t) frames)
2106 ERR("mmap partially failed.\n");
2108 avail = snd_pcm_avail_update(wwo->p_handle);
2112 static void DSDB_PCMCallback(snd_async_handler_t *ahandler)
2114 /* snd_pcm_t * handle = snd_async_handler_get_pcm(ahandler); */
2115 IDsDriverBufferImpl* pdbi = snd_async_handler_get_callback_private(ahandler);
2116 TRACE("callback called\n");
2117 DSDB_MMAPCopy(pdbi);
2120 static int DSDB_CreateMMAP(IDsDriverBufferImpl* pdbi)
2122 WINE_WAVEOUT * wwo = &(WOutDev[pdbi->drv->wDevID]);
2123 snd_pcm_format_t format = snd_pcm_hw_params_get_format(wwo->hw_params);
2124 snd_pcm_uframes_t frames = snd_pcm_hw_params_get_buffer_size(wwo->hw_params);
2125 int channels = snd_pcm_hw_params_get_channels(wwo->hw_params);
2126 unsigned int bits_per_sample = snd_pcm_format_physical_width(format);
2127 unsigned int bits_per_frame = bits_per_sample * channels;
2128 snd_pcm_channel_area_t * a;
2133 ALSA_TraceParameters(wwo->hw_params, NULL, FALSE);
2135 TRACE("format=%s frames=%ld channels=%d bits_per_sample=%d bits_per_frame=%d\n",
2136 snd_pcm_format_name(format), frames, channels, bits_per_sample, bits_per_frame);
2138 pdbi->mmap_buflen_frames = frames;
2139 pdbi->mmap_buflen_bytes = snd_pcm_frames_to_bytes( wwo->p_handle, frames );
2140 pdbi->mmap_buffer = HeapAlloc(GetProcessHeap(),0,pdbi->mmap_buflen_bytes);
2141 if (!pdbi->mmap_buffer)
2142 return DSERR_OUTOFMEMORY;
2144 snd_pcm_format_set_silence(format, pdbi->mmap_buffer, frames );
2146 TRACE("created mmap buffer of %ld frames (%ld bytes) at %p\n",
2147 frames, pdbi->mmap_buflen_bytes, pdbi->mmap_buffer);
2149 pdbi->mmap_areas = HeapAlloc(GetProcessHeap(),0,channels*sizeof(snd_pcm_channel_area_t));
2150 if (!pdbi->mmap_areas)
2151 return DSERR_OUTOFMEMORY;
2153 a = pdbi->mmap_areas;
2154 for (c = 0; c < channels; c++, a++)
2156 a->addr = pdbi->mmap_buffer;
2157 a->first = bits_per_sample * c;
2158 a->step = bits_per_frame;
2159 TRACE("Area %d: addr=%p first=%d step=%d\n", c, a->addr, a->first, a->step);
2162 InitializeCriticalSection(&pdbi->mmap_crst);
2164 err = snd_async_add_pcm_handler(&pdbi->mmap_async_handler, wwo->p_handle, DSDB_PCMCallback, pdbi);
2167 ERR("add_pcm_handler failed. reason: %s\n", snd_strerror(err));
2168 return DSERR_GENERIC;
2174 static void DSDB_DestroyMMAP(IDsDriverBufferImpl* pdbi)
2176 TRACE("mmap buffer %p destroyed\n", pdbi->mmap_buffer);
2177 HeapFree(GetProcessHeap(), 0, pdbi->mmap_areas);
2178 HeapFree(GetProcessHeap(), 0, pdbi->mmap_buffer);
2179 pdbi->mmap_areas = NULL;
2180 pdbi->mmap_buffer = NULL;
2181 DeleteCriticalSection(&pdbi->mmap_crst);
2185 static HRESULT WINAPI IDsDriverBufferImpl_QueryInterface(PIDSDRIVERBUFFER iface, REFIID riid, LPVOID *ppobj)
2187 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
2188 FIXME("(): stub!\n");
2189 return DSERR_UNSUPPORTED;
2192 static ULONG WINAPI IDsDriverBufferImpl_AddRef(PIDSDRIVERBUFFER iface)
2194 ICOM_THIS(IDsDriverBufferImpl,iface);
2195 TRACE("(%p)\n",iface);
2199 static ULONG WINAPI IDsDriverBufferImpl_Release(PIDSDRIVERBUFFER iface)
2201 ICOM_THIS(IDsDriverBufferImpl,iface);
2202 TRACE("(%p)\n",iface);
2205 if (This == This->drv->primary)
2206 This->drv->primary = NULL;
2207 DSDB_DestroyMMAP(This);
2208 HeapFree(GetProcessHeap(), 0, This);
2212 static HRESULT WINAPI IDsDriverBufferImpl_Lock(PIDSDRIVERBUFFER iface,
2213 LPVOID*ppvAudio1,LPDWORD pdwLen1,
2214 LPVOID*ppvAudio2,LPDWORD pdwLen2,
2215 DWORD dwWritePosition,DWORD dwWriteLen,
2218 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
2219 TRACE("(%p)\n",iface);
2220 return DSERR_UNSUPPORTED;
2223 static HRESULT WINAPI IDsDriverBufferImpl_Unlock(PIDSDRIVERBUFFER iface,
2224 LPVOID pvAudio1,DWORD dwLen1,
2225 LPVOID pvAudio2,DWORD dwLen2)
2227 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
2228 TRACE("(%p)\n",iface);
2229 return DSERR_UNSUPPORTED;
2232 static HRESULT WINAPI IDsDriverBufferImpl_SetFormat(PIDSDRIVERBUFFER iface,
2233 LPWAVEFORMATEX pwfx)
2235 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
2236 TRACE("(%p,%p)\n",iface,pwfx);
2237 return DSERR_BUFFERLOST;
2240 static HRESULT WINAPI IDsDriverBufferImpl_SetFrequency(PIDSDRIVERBUFFER iface, DWORD dwFreq)
2242 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
2243 TRACE("(%p,%ld): stub\n",iface,dwFreq);
2244 return DSERR_UNSUPPORTED;
2247 static HRESULT WINAPI IDsDriverBufferImpl_SetVolumePan(PIDSDRIVERBUFFER iface, PDSVOLUMEPAN pVolPan)
2250 ICOM_THIS(IDsDriverBufferImpl,iface);
2251 TRACE("(%p,%p)\n",iface,pVolPan);
2252 vol = pVolPan->dwTotalLeftAmpFactor | (pVolPan->dwTotalRightAmpFactor << 16);
2254 if (wodSetVolume(This->drv->wDevID, vol) != MMSYSERR_NOERROR) {
2255 WARN("wodSetVolume failed\n");
2256 return DSERR_INVALIDPARAM;
2262 static HRESULT WINAPI IDsDriverBufferImpl_SetPosition(PIDSDRIVERBUFFER iface, DWORD dwNewPos)
2264 /* ICOM_THIS(IDsDriverImpl,iface); */
2265 TRACE("(%p,%ld): stub\n",iface,dwNewPos);
2266 return DSERR_UNSUPPORTED;
2269 static HRESULT WINAPI IDsDriverBufferImpl_GetPosition(PIDSDRIVERBUFFER iface,
2270 LPDWORD lpdwPlay, LPDWORD lpdwWrite)
2272 ICOM_THIS(IDsDriverBufferImpl,iface);
2273 WINE_WAVEOUT * wwo = &(WOutDev[This->drv->wDevID]);
2274 snd_pcm_uframes_t hw_ptr;
2275 snd_pcm_uframes_t period_size;
2277 if (wwo->hw_params == NULL) return DSERR_GENERIC;
2279 period_size = snd_pcm_hw_params_get_period_size(wwo->hw_params, 0);
2281 if (wwo->p_handle == NULL) return DSERR_GENERIC;
2282 /** we need to track down buffer underruns */
2283 DSDB_CheckXRUN(This);
2285 EnterCriticalSection(&This->mmap_crst);
2286 hw_ptr = _snd_pcm_mmap_hw_ptr(wwo->p_handle);
2288 *lpdwPlay = snd_pcm_frames_to_bytes(wwo->p_handle, hw_ptr/ period_size * period_size) % This->mmap_buflen_bytes;
2290 *lpdwWrite = snd_pcm_frames_to_bytes(wwo->p_handle, (hw_ptr / period_size + 1) * period_size ) % This->mmap_buflen_bytes;
2291 LeaveCriticalSection(&This->mmap_crst);
2293 TRACE("hw_ptr=0x%08x, playpos=%ld, writepos=%ld\n", (unsigned int)hw_ptr, lpdwPlay?*lpdwPlay:-1, lpdwWrite?*lpdwWrite:-1);
2297 static HRESULT WINAPI IDsDriverBufferImpl_Play(PIDSDRIVERBUFFER iface, DWORD dwRes1, DWORD dwRes2, DWORD dwFlags)
2299 ICOM_THIS(IDsDriverBufferImpl,iface);
2300 WINE_WAVEOUT * wwo = &(WOutDev[This->drv->wDevID]);
2301 snd_pcm_state_t state;
2304 TRACE("(%p,%lx,%lx,%lx)\n",iface,dwRes1,dwRes2,dwFlags);
2306 if (wwo->p_handle == NULL) return DSERR_GENERIC;
2308 state = snd_pcm_state(wwo->p_handle);
2309 if ( state == SND_PCM_STATE_SETUP )
2311 err = snd_pcm_prepare(wwo->p_handle);
2312 state = snd_pcm_state(wwo->p_handle);
2314 if ( state == SND_PCM_STATE_PREPARED )
2316 DSDB_MMAPCopy(This);
2317 err = snd_pcm_start(wwo->p_handle);
2322 static HRESULT WINAPI IDsDriverBufferImpl_Stop(PIDSDRIVERBUFFER iface)
2324 ICOM_THIS(IDsDriverBufferImpl,iface);
2325 WINE_WAVEOUT * wwo = &(WOutDev[This->drv->wDevID]);
2330 TRACE("(%p)\n",iface);
2332 if (wwo->p_handle == NULL) return DSERR_GENERIC;
2334 /* ring buffer wrap up detection */
2335 IDsDriverBufferImpl_GetPosition(iface, &play, &write);
2338 TRACE("writepos wrapper up\n");
2342 if ( ( err = snd_pcm_drop(wwo->p_handle)) < 0 )
2344 ERR("error while stopping pcm: %s\n", snd_strerror(err));
2345 return DSERR_GENERIC;
2350 static ICOM_VTABLE(IDsDriverBuffer) dsdbvt =
2352 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
2353 IDsDriverBufferImpl_QueryInterface,
2354 IDsDriverBufferImpl_AddRef,
2355 IDsDriverBufferImpl_Release,
2356 IDsDriverBufferImpl_Lock,
2357 IDsDriverBufferImpl_Unlock,
2358 IDsDriverBufferImpl_SetFormat,
2359 IDsDriverBufferImpl_SetFrequency,
2360 IDsDriverBufferImpl_SetVolumePan,
2361 IDsDriverBufferImpl_SetPosition,
2362 IDsDriverBufferImpl_GetPosition,
2363 IDsDriverBufferImpl_Play,
2364 IDsDriverBufferImpl_Stop
2367 static HRESULT WINAPI IDsDriverImpl_QueryInterface(PIDSDRIVER iface, REFIID riid, LPVOID *ppobj)
2369 /* ICOM_THIS(IDsDriverImpl,iface); */
2370 FIXME("(%p): stub!\n",iface);
2371 return DSERR_UNSUPPORTED;
2374 static ULONG WINAPI IDsDriverImpl_AddRef(PIDSDRIVER iface)
2376 ICOM_THIS(IDsDriverImpl,iface);
2377 TRACE("(%p)\n",iface);
2382 static ULONG WINAPI IDsDriverImpl_Release(PIDSDRIVER iface)
2384 ICOM_THIS(IDsDriverImpl,iface);
2385 TRACE("(%p)\n",iface);
2388 HeapFree(GetProcessHeap(),0,This);
2392 static HRESULT WINAPI IDsDriverImpl_GetDriverDesc(PIDSDRIVER iface, PDSDRIVERDESC pDesc)
2394 ICOM_THIS(IDsDriverImpl,iface);
2395 TRACE("(%p,%p)\n",iface,pDesc);
2396 memcpy(pDesc, &(WOutDev[This->wDevID].ds_desc), sizeof(DSDRIVERDESC));
2397 pDesc->dwFlags = DSDDESC_DOMMSYSTEMOPEN | DSDDESC_DOMMSYSTEMSETFORMAT |
2398 DSDDESC_USESYSTEMMEMORY | DSDDESC_DONTNEEDPRIMARYLOCK;
2399 pDesc->dnDevNode = WOutDev[This->wDevID].waveDesc.dnDevNode;
2401 pDesc->wReserved = 0;
2402 pDesc->ulDeviceNum = This->wDevID;
2403 pDesc->dwHeapType = DSDHEAP_NOHEAP;
2404 pDesc->pvDirectDrawHeap = NULL;
2405 pDesc->dwMemStartAddress = 0;
2406 pDesc->dwMemEndAddress = 0;
2407 pDesc->dwMemAllocExtra = 0;
2408 pDesc->pvReserved1 = NULL;
2409 pDesc->pvReserved2 = NULL;
2413 static HRESULT WINAPI IDsDriverImpl_Open(PIDSDRIVER iface)
2415 /* ICOM_THIS(IDsDriverImpl,iface); */
2416 TRACE("(%p)\n",iface);
2420 static HRESULT WINAPI IDsDriverImpl_Close(PIDSDRIVER iface)
2422 /* ICOM_THIS(IDsDriverImpl,iface); */
2423 TRACE("(%p)\n",iface);
2427 static HRESULT WINAPI IDsDriverImpl_GetCaps(PIDSDRIVER iface, PDSDRIVERCAPS pCaps)
2429 ICOM_THIS(IDsDriverImpl,iface);
2430 TRACE("(%p,%p)\n",iface,pCaps);
2431 memset(pCaps, 0, sizeof(*pCaps));
2433 pCaps->dwFlags = DSCAPS_PRIMARYMONO;
2434 if ( WOutDev[This->wDevID].caps.wChannels == 2 )
2435 pCaps->dwFlags |= DSCAPS_PRIMARYSTEREO;
2437 if ( WOutDev[This->wDevID].caps.dwFormats & (WAVE_FORMAT_1S08 | WAVE_FORMAT_2S08 | WAVE_FORMAT_4S08 ) )
2438 pCaps->dwFlags |= DSCAPS_PRIMARY8BIT;
2440 if ( WOutDev[This->wDevID].caps.dwFormats & (WAVE_FORMAT_1S16 | WAVE_FORMAT_2S16 | WAVE_FORMAT_4S16))
2441 pCaps->dwFlags |= DSCAPS_PRIMARY16BIT;
2443 pCaps->dwPrimaryBuffers = 1;
2444 TRACE("caps=0x%X\n",(unsigned int)pCaps->dwFlags);
2445 pCaps->dwMinSecondarySampleRate = DSBFREQUENCY_MIN;
2446 pCaps->dwMaxSecondarySampleRate = DSBFREQUENCY_MAX;
2448 /* the other fields only apply to secondary buffers, which we don't support
2449 * (unless we want to mess with wavetable synthesizers and MIDI) */
2453 static HRESULT WINAPI IDsDriverImpl_CreateSoundBuffer(PIDSDRIVER iface,
2454 LPWAVEFORMATEX pwfx,
2455 DWORD dwFlags, DWORD dwCardAddress,
2456 LPDWORD pdwcbBufferSize,
2460 ICOM_THIS(IDsDriverImpl,iface);
2461 IDsDriverBufferImpl** ippdsdb = (IDsDriverBufferImpl**)ppvObj;
2464 TRACE("(%p,%p,%lx,%lx)\n",iface,pwfx,dwFlags,dwCardAddress);
2465 /* we only support primary buffers */
2466 if (!(dwFlags & DSBCAPS_PRIMARYBUFFER))
2467 return DSERR_UNSUPPORTED;
2469 return DSERR_ALLOCATED;
2470 if (dwFlags & (DSBCAPS_CTRLFREQUENCY | DSBCAPS_CTRLPAN))
2471 return DSERR_CONTROLUNAVAIL;
2473 *ippdsdb = (IDsDriverBufferImpl*)HeapAlloc(GetProcessHeap(),0,sizeof(IDsDriverBufferImpl));
2474 if (*ippdsdb == NULL)
2475 return DSERR_OUTOFMEMORY;
2476 (*ippdsdb)->lpVtbl = &dsdbvt;
2477 (*ippdsdb)->ref = 1;
2478 (*ippdsdb)->drv = This;
2480 err = DSDB_CreateMMAP((*ippdsdb));
2483 HeapFree(GetProcessHeap(), 0, *ippdsdb);
2487 *ppbBuffer = (*ippdsdb)->mmap_buffer;
2488 *pdwcbBufferSize = (*ippdsdb)->mmap_buflen_bytes;
2490 This->primary = *ippdsdb;
2492 /* buffer is ready to go */
2493 TRACE("buffer created at %p\n", *ippdsdb);
2497 static HRESULT WINAPI IDsDriverImpl_DuplicateSoundBuffer(PIDSDRIVER iface,
2498 PIDSDRIVERBUFFER pBuffer,
2501 /* ICOM_THIS(IDsDriverImpl,iface); */
2502 TRACE("(%p,%p): stub\n",iface,pBuffer);
2503 return DSERR_INVALIDCALL;
2506 static ICOM_VTABLE(IDsDriver) dsdvt =
2508 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
2509 IDsDriverImpl_QueryInterface,
2510 IDsDriverImpl_AddRef,
2511 IDsDriverImpl_Release,
2512 IDsDriverImpl_GetDriverDesc,
2514 IDsDriverImpl_Close,
2515 IDsDriverImpl_GetCaps,
2516 IDsDriverImpl_CreateSoundBuffer,
2517 IDsDriverImpl_DuplicateSoundBuffer
2520 static DWORD wodDsCreate(UINT wDevID, PIDSDRIVER* drv)
2522 IDsDriverImpl** idrv = (IDsDriverImpl**)drv;
2524 TRACE("driver created\n");
2526 /* the HAL isn't much better than the HEL if we can't do mmap() */
2527 if (!(WOutDev[wDevID].caps.dwSupport & WAVECAPS_DIRECTSOUND)) {
2528 ERR("DirectSound flag not set\n");
2529 MESSAGE("This sound card's driver does not support direct access\n");
2530 MESSAGE("The (slower) DirectSound HEL mode will be used instead.\n");
2531 return MMSYSERR_NOTSUPPORTED;
2534 *idrv = (IDsDriverImpl*)HeapAlloc(GetProcessHeap(),0,sizeof(IDsDriverImpl));
2536 return MMSYSERR_NOMEM;
2537 (*idrv)->lpVtbl = &dsdvt;
2540 (*idrv)->wDevID = wDevID;
2541 (*idrv)->primary = NULL;
2542 return MMSYSERR_NOERROR;
2545 static DWORD wodDsDesc(UINT wDevID, PDSDRIVERDESC desc)
2547 memcpy(desc, &(WOutDev[wDevID].ds_desc), sizeof(DSDRIVERDESC));
2548 return MMSYSERR_NOERROR;
2551 static DWORD wodDsGuid(UINT wDevID, LPGUID pGuid)
2553 TRACE("(%d,%p)\n",wDevID,pGuid);
2555 memcpy(pGuid, &(WOutDev[wDevID].ds_guid), sizeof(GUID));
2557 return MMSYSERR_NOERROR;
2560 /*======================================================================*
2561 * Low level WAVE IN implementation *
2562 *======================================================================*/
2564 /**************************************************************************
2565 * widNotifyClient [internal]
2567 static DWORD widNotifyClient(WINE_WAVEIN* wwi, WORD wMsg, DWORD dwParam1, DWORD dwParam2)
2569 TRACE("wMsg = 0x%04x dwParm1 = %04lX dwParam2 = %04lX\n", wMsg, dwParam1, dwParam2);
2575 if (wwi->wFlags != DCB_NULL &&
2576 !DriverCallback(wwi->waveDesc.dwCallback, wwi->wFlags, (HDRVR)wwi->waveDesc.hWave,
2577 wMsg, wwi->waveDesc.dwInstance, dwParam1, dwParam2)) {
2578 WARN("can't notify client !\n");
2579 return MMSYSERR_ERROR;
2583 FIXME("Unknown callback message %u\n", wMsg);
2584 return MMSYSERR_INVALPARAM;
2586 return MMSYSERR_NOERROR;
2589 /**************************************************************************
2590 * widGetDevCaps [internal]
2592 static DWORD widGetDevCaps(WORD wDevID, LPWAVEOUTCAPSA lpCaps, DWORD dwSize)
2594 TRACE("(%u, %p, %lu);\n", wDevID, lpCaps, dwSize);
2596 if (lpCaps == NULL) return MMSYSERR_NOTENABLED;
2598 if (wDevID >= MAX_WAVEINDRV) {
2599 TRACE("MAX_WAVOUTDRV reached !\n");
2600 return MMSYSERR_BADDEVICEID;
2603 memcpy(lpCaps, &WInDev[wDevID].caps, min(dwSize, sizeof(*lpCaps)));
2604 return MMSYSERR_NOERROR;
2607 /**************************************************************************
2608 * widRecorder_ReadHeaders [internal]
2610 static void widRecorder_ReadHeaders(WINE_WAVEIN * wwi)
2612 enum win_wm_message tmp_msg;
2617 while (ALSA_RetrieveRingMessage(&wwi->msgRing, &tmp_msg, &tmp_param, &tmp_ev)) {
2618 if (tmp_msg == WINE_WM_HEADER) {
2620 lpWaveHdr = (LPWAVEHDR)tmp_param;
2621 lpWaveHdr->lpNext = 0;
2623 if (wwi->lpQueuePtr == 0)
2624 wwi->lpQueuePtr = lpWaveHdr;
2626 for (wh = &(wwi->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
2630 ERR("should only have headers left\n");
2635 /**************************************************************************
2636 * widRecorder [internal]
2638 static DWORD CALLBACK widRecorder(LPVOID pmt)
2640 WORD uDevID = (DWORD)pmt;
2641 WINE_WAVEIN* wwi = (WINE_WAVEIN*)&WInDev[uDevID];
2645 LPVOID buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, wwi->dwPeriodSize);
2646 char *pOffset = buffer;
2647 enum win_wm_message msg;
2650 DWORD frames_per_period;
2652 wwi->state = WINE_WS_STOPPED;
2653 wwi->dwTotalRecorded = 0;
2654 wwi->lpQueuePtr = NULL;
2656 SetEvent(wwi->hStartUpEvent);
2658 /* make sleep time to be # of ms to output a period */
2659 dwSleepTime = (1024/*wwi-dwPeriodSize => overrun!*/ * 1000) / wwi->format.wf.nAvgBytesPerSec;
2660 frames_per_period = snd_pcm_bytes_to_frames(wwi->p_handle, wwi->dwPeriodSize);
2661 TRACE("sleeptime=%ld ms\n", dwSleepTime);
2664 /* wait for dwSleepTime or an event in thread's queue */
2665 /* FIXME: could improve wait time depending on queue state,
2666 * ie, number of queued fragments
2668 if (wwi->lpQueuePtr != NULL && wwi->state == WINE_WS_PLAYING)
2675 lpWaveHdr = wwi->lpQueuePtr;
2676 /* read all the fragments accumulated so far */
2677 frames = snd_pcm_avail_update(wwi->p_handle);
2678 bytes = snd_pcm_frames_to_bytes(wwi->p_handle, frames);
2679 TRACE("frames = %ld bytes = %ld\n", frames, bytes);
2680 periods = bytes / wwi->dwPeriodSize;
2681 while ((periods > 0) && (wwi->lpQueuePtr))
2684 bytes = wwi->dwPeriodSize;
2685 TRACE("bytes = %ld\n",bytes);
2686 if (lpWaveHdr->dwBufferLength - lpWaveHdr->dwBytesRecorded >= wwi->dwPeriodSize)
2688 /* directly read fragment in wavehdr */
2689 read = wwi->read(wwi->p_handle, lpWaveHdr->lpData + lpWaveHdr->dwBytesRecorded, frames_per_period);
2690 bytesRead = snd_pcm_frames_to_bytes(wwi->p_handle, read);
2692 TRACE("bytesRead=%ld (direct)\n", bytesRead);
2693 if (bytesRead != (DWORD) -1)
2695 /* update number of bytes recorded in current buffer and by this device */
2696 lpWaveHdr->dwBytesRecorded += bytesRead;
2697 wwi->dwTotalRecorded += bytesRead;
2699 /* buffer is full. notify client */
2700 if (lpWaveHdr->dwBytesRecorded == lpWaveHdr->dwBufferLength)
2702 /* must copy the value of next waveHdr, because we have no idea of what
2703 * will be done with the content of lpWaveHdr in callback
2705 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
2707 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
2708 lpWaveHdr->dwFlags |= WHDR_DONE;
2710 wwi->lpQueuePtr = lpNext;
2711 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
2718 /* read the fragment in a local buffer */
2719 read = wwi->read(wwi->p_handle, buffer, frames_per_period);
2720 bytesRead = snd_pcm_frames_to_bytes(wwi->p_handle, read);
2723 TRACE("bytesRead=%ld (local)\n", bytesRead);
2725 /* copy data in client buffers */
2726 while (bytesRead != (DWORD) -1 && bytesRead > 0)
2728 DWORD dwToCopy = min (bytesRead, lpWaveHdr->dwBufferLength - lpWaveHdr->dwBytesRecorded);
2730 memcpy(lpWaveHdr->lpData + lpWaveHdr->dwBytesRecorded,
2734 /* update number of bytes recorded in current buffer and by this device */
2735 lpWaveHdr->dwBytesRecorded += dwToCopy;
2736 wwi->dwTotalRecorded += dwToCopy;
2737 bytesRead -= dwToCopy;
2738 pOffset += dwToCopy;
2740 /* client buffer is full. notify client */
2741 if (lpWaveHdr->dwBytesRecorded == lpWaveHdr->dwBufferLength)
2743 /* must copy the value of next waveHdr, because we have no idea of what
2744 * will be done with the content of lpWaveHdr in callback
2746 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
2747 TRACE("lpNext=%p\n", lpNext);
2749 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
2750 lpWaveHdr->dwFlags |= WHDR_DONE;
2752 wwi->lpQueuePtr = lpNext;
2753 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
2756 if (!lpNext && bytesRead) {
2757 /* before we give up, check for more header messages */
2758 while (ALSA_PeekRingMessage(&wwi->msgRing, &msg, ¶m, &ev))
2760 if (msg == WINE_WM_HEADER) {
2762 ALSA_RetrieveRingMessage(&wwi->msgRing, &msg, ¶m, &ev);
2763 hdr = ((LPWAVEHDR)param);
2764 TRACE("msg = %s, hdr = %p, ev = %p\n", wodPlayerCmdString[msg - WM_USER - 1], hdr, ev);
2766 if (lpWaveHdr == 0) {
2767 /* new head of queue */
2768 wwi->lpQueuePtr = lpWaveHdr = hdr;
2770 /* insert buffer at the end of queue */
2772 for (wh = &(wwi->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
2779 if (lpWaveHdr == 0) {
2780 /* no more buffer to copy data to, but we did read more.
2781 * what hasn't been copied will be dropped
2783 WARN("buffer under run! %lu bytes dropped.\n", bytesRead);
2784 wwi->lpQueuePtr = NULL;
2794 WAIT_OMR(&wwi->msgRing, dwSleepTime);
2796 while (ALSA_RetrieveRingMessage(&wwi->msgRing, &msg, ¶m, &ev))
2798 TRACE("msg=%s param=0x%lx\n", wodPlayerCmdString[msg - WM_USER - 1], param);
2800 case WINE_WM_PAUSING:
2801 wwi->state = WINE_WS_PAUSED;
2802 /*FIXME("Device should stop recording\n");*/
2805 case WINE_WM_STARTING:
2806 wwi->state = WINE_WS_PLAYING;
2807 snd_pcm_start(wwi->p_handle);
2810 case WINE_WM_HEADER:
2811 lpWaveHdr = (LPWAVEHDR)param;
2812 lpWaveHdr->lpNext = 0;
2814 /* insert buffer at the end of queue */
2817 for (wh = &(wwi->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
2821 case WINE_WM_STOPPING:
2822 if (wwi->state != WINE_WS_STOPPED)
2824 snd_pcm_drain(wwi->p_handle);
2826 /* read any headers in queue */
2827 widRecorder_ReadHeaders(wwi);
2829 /* return current buffer to app */
2830 lpWaveHdr = wwi->lpQueuePtr;
2833 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
2834 TRACE("stop %p %p\n", lpWaveHdr, lpWaveHdr->lpNext);
2835 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
2836 lpWaveHdr->dwFlags |= WHDR_DONE;
2837 wwi->lpQueuePtr = lpNext;
2838 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
2841 wwi->state = WINE_WS_STOPPED;
2844 case WINE_WM_RESETTING:
2845 if (wwi->state != WINE_WS_STOPPED)
2847 snd_pcm_drain(wwi->p_handle);
2849 wwi->state = WINE_WS_STOPPED;
2850 wwi->dwTotalRecorded = 0;
2852 /* read any headers in queue */
2853 widRecorder_ReadHeaders(wwi);
2855 /* return all buffers to the app */
2856 for (lpWaveHdr = wwi->lpQueuePtr; lpWaveHdr; lpWaveHdr = lpWaveHdr->lpNext) {
2857 TRACE("reset %p %p\n", lpWaveHdr, lpWaveHdr->lpNext);
2858 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
2859 lpWaveHdr->dwFlags |= WHDR_DONE;
2860 wwi->lpQueuePtr = lpWaveHdr->lpNext;
2861 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
2864 wwi->lpQueuePtr = NULL;
2867 case WINE_WM_CLOSING:
2869 wwi->state = WINE_WS_CLOSED;
2871 HeapFree(GetProcessHeap(), 0, buffer);
2873 /* shouldn't go here */
2874 case WINE_WM_UPDATE:
2879 FIXME("unknown message %d\n", msg);
2885 /* just for not generating compilation warnings... should never be executed */
2889 /**************************************************************************
2890 * widOpen [internal]
2892 static DWORD widOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
2895 snd_pcm_hw_params_t * hw_params;
2896 snd_pcm_sw_params_t * sw_params;
2897 snd_pcm_access_t access;
2898 snd_pcm_format_t format;
2900 unsigned int buffer_time = 500000;
2901 unsigned int period_time = 10000;
2903 snd_pcm_uframes_t period_size;
2908 snd_pcm_hw_params_alloca(&hw_params);
2909 snd_pcm_sw_params_alloca(&sw_params);
2911 TRACE("(%u, %p, %08lX);\n", wDevID, lpDesc, dwFlags);
2912 if (lpDesc == NULL) {
2913 WARN("Invalid Parameter !\n");
2914 return MMSYSERR_INVALPARAM;
2916 if (wDevID >= MAX_WAVEOUTDRV) {
2917 TRACE("MAX_WAVOUTDRV reached !\n");
2918 return MMSYSERR_BADDEVICEID;
2921 /* only PCM format is supported so far... */
2922 if (lpDesc->lpFormat->wFormatTag != WAVE_FORMAT_PCM ||
2923 lpDesc->lpFormat->nChannels == 0 ||
2924 lpDesc->lpFormat->nSamplesPerSec == 0 ||
2925 (lpDesc->lpFormat->wBitsPerSample!=8 && lpDesc->lpFormat->wBitsPerSample!=16)) {
2926 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
2927 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
2928 lpDesc->lpFormat->nSamplesPerSec);
2929 return WAVERR_BADFORMAT;
2932 if (dwFlags & WAVE_FORMAT_QUERY) {
2933 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
2934 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
2935 lpDesc->lpFormat->nSamplesPerSec);
2936 return MMSYSERR_NOERROR;
2939 wwi = &WInDev[wDevID];
2941 if ((dwFlags & WAVE_DIRECTSOUND) && !(wwi->caps.dwSupport & WAVECAPS_DIRECTSOUND))
2942 /* not supported, ignore it */
2943 dwFlags &= ~WAVE_DIRECTSOUND;
2946 flags = SND_PCM_NONBLOCK;
2947 if ( dwFlags & WAVE_DIRECTSOUND )
2948 flags |= SND_PCM_ASYNC;
2950 if ( (err=snd_pcm_open(&pcm, wwi->device, SND_PCM_STREAM_CAPTURE, dwFlags)) < 0 )
2952 ERR("Error open: %s\n", snd_strerror(err));
2953 return MMSYSERR_NOTENABLED;
2956 wwi->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
2958 memcpy(&wwi->waveDesc, lpDesc, sizeof(WAVEOPENDESC));
2959 memcpy(&wwi->format, lpDesc->lpFormat, sizeof(PCMWAVEFORMAT));
2961 if (wwi->format.wBitsPerSample == 0) {
2962 WARN("Resetting zeroed wBitsPerSample\n");
2963 wwi->format.wBitsPerSample = 8 *
2964 (wwi->format.wf.nAvgBytesPerSec /
2965 wwi->format.wf.nSamplesPerSec) /
2966 wwi->format.wf.nChannels;
2969 snd_pcm_hw_params_any(pcm, hw_params);
2971 #define EXIT_ON_ERROR(f,e,txt) do \
2974 if ( (err = (f) ) < 0) \
2976 ERR(txt ": %s\n", snd_strerror(err)); \
2977 snd_pcm_close(pcm); \
2982 access = SND_PCM_ACCESS_MMAP_INTERLEAVED;
2983 if ( ( err = snd_pcm_hw_params_set_access(pcm, hw_params, access ) ) < 0) {
2984 WARN("mmap not available. switching to standard write.\n");
2985 access = SND_PCM_ACCESS_RW_INTERLEAVED;
2986 EXIT_ON_ERROR( snd_pcm_hw_params_set_access(pcm, hw_params, access ), MMSYSERR_INVALPARAM, "unable to set access for playback");
2987 wwi->read = snd_pcm_readi;
2990 wwi->read = snd_pcm_mmap_readi;
2992 EXIT_ON_ERROR( snd_pcm_hw_params_set_channels(pcm, hw_params, wwi->format.wf.nChannels), MMSYSERR_INVALPARAM, "unable to set required channels");
2994 format = (wwi->format.wBitsPerSample == 16) ? SND_PCM_FORMAT_S16_LE : SND_PCM_FORMAT_U8;
2995 EXIT_ON_ERROR( snd_pcm_hw_params_set_format(pcm, hw_params, format), MMSYSERR_INVALPARAM, "unable to set required format");
2997 rate = snd_pcm_hw_params_set_rate_near(pcm, hw_params, wwi->format.wf.nSamplesPerSec, 0);
2999 ERR("Rate %ld Hz not available for playback: %s\n", wwi->format.wf.nSamplesPerSec, snd_strerror(rate));
3001 return WAVERR_BADFORMAT;
3003 if (rate != wwi->format.wf.nSamplesPerSec) {
3004 ERR("Rate doesn't match (requested %ld Hz, got %d Hz)\n", wwi->format.wf.nSamplesPerSec, rate);
3006 return WAVERR_BADFORMAT;
3009 EXIT_ON_ERROR( snd_pcm_hw_params_set_buffer_time_near(pcm, hw_params, buffer_time, 0), MMSYSERR_INVALPARAM, "unable to set buffer time");
3010 EXIT_ON_ERROR( snd_pcm_hw_params_set_period_time_near(pcm, hw_params, period_time, 0), MMSYSERR_INVALPARAM, "unable to set period time");
3012 EXIT_ON_ERROR( snd_pcm_hw_params(pcm, hw_params), MMSYSERR_INVALPARAM, "unable to set hw params for playback");
3014 period_size = snd_pcm_hw_params_get_period_size(hw_params, 0);
3015 buffer_size = snd_pcm_hw_params_get_buffer_size(hw_params);
3017 snd_pcm_sw_params_current(pcm, sw_params);
3018 EXIT_ON_ERROR( snd_pcm_sw_params_set_start_threshold(pcm, sw_params, dwFlags & WAVE_DIRECTSOUND ? INT_MAX : 1 ), MMSYSERR_ERROR, "unable to set start threshold");
3019 EXIT_ON_ERROR( snd_pcm_sw_params_set_silence_size(pcm, sw_params, 0), MMSYSERR_ERROR, "unable to set silence size");
3020 EXIT_ON_ERROR( snd_pcm_sw_params_set_avail_min(pcm, sw_params, period_size), MMSYSERR_ERROR, "unable to set avail min");
3021 EXIT_ON_ERROR( snd_pcm_sw_params_set_xfer_align(pcm, sw_params, 1), MMSYSERR_ERROR, "unable to set xfer align");
3022 EXIT_ON_ERROR( snd_pcm_sw_params_set_silence_threshold(pcm, sw_params, 0), MMSYSERR_ERROR, "unable to set silence threshold");
3023 EXIT_ON_ERROR( snd_pcm_sw_params(pcm, sw_params), MMSYSERR_ERROR, "unable to set sw params for playback");
3024 #undef EXIT_ON_ERROR
3026 snd_pcm_prepare(pcm);
3029 ALSA_TraceParameters(hw_params, sw_params, FALSE);
3031 /* now, we can save all required data for later use... */
3032 if ( wwi->hw_params )
3033 snd_pcm_hw_params_free(wwi->hw_params);
3034 snd_pcm_hw_params_malloc(&(wwi->hw_params));
3035 snd_pcm_hw_params_copy(wwi->hw_params, hw_params);
3037 wwi->dwBufferSize = buffer_size;
3038 wwi->lpQueuePtr = wwi->lpPlayPtr = wwi->lpLoopPtr = NULL;
3039 wwi->p_handle = pcm;
3041 ALSA_InitRingMessage(&wwi->msgRing);
3043 wwi->count = snd_pcm_poll_descriptors_count (wwi->p_handle);
3044 if (wwi->count <= 0) {
3045 ERR("Invalid poll descriptors count\n");
3046 return MMSYSERR_ERROR;
3049 wwi->ufds = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY, sizeof(struct pollfd) * wwi->count);
3050 if (wwi->ufds == NULL) {
3051 ERR("No enough memory\n");
3052 return MMSYSERR_NOMEM;
3054 if ((err = snd_pcm_poll_descriptors(wwi->p_handle, wwi->ufds, wwi->count)) < 0) {
3055 ERR("Unable to obtain poll descriptors for playback: %s\n", snd_strerror(err));
3056 return MMSYSERR_ERROR;
3059 wwi->dwPeriodSize = period_size;
3060 /*if (wwi->dwFragmentSize % wwi->format.wf.nBlockAlign)
3061 ERR("Fragment doesn't contain an integral number of data blocks\n");
3063 TRACE("dwPeriodSize=%lu\n", wwi->dwPeriodSize);
3064 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%lu, nSamplesPerSec=%lu, nChannels=%u nBlockAlign=%u!\n",
3065 wwi->format.wBitsPerSample, wwi->format.wf.nAvgBytesPerSec,
3066 wwi->format.wf.nSamplesPerSec, wwi->format.wf.nChannels,
3067 wwi->format.wf.nBlockAlign);
3069 if (!(dwFlags & WAVE_DIRECTSOUND)) {
3070 wwi->hStartUpEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
3071 wwi->hThread = CreateThread(NULL, 0, widRecorder, (LPVOID)(DWORD)wDevID, 0, &(wwi->dwThreadID));
3072 WaitForSingleObject(wwi->hStartUpEvent, INFINITE);
3073 CloseHandle(wwi->hStartUpEvent);
3075 wwi->hThread = INVALID_HANDLE_VALUE;
3076 wwi->dwThreadID = 0;
3078 wwi->hStartUpEvent = INVALID_HANDLE_VALUE;
3080 return widNotifyClient(wwi, WIM_OPEN, 0L, 0L);
3084 /**************************************************************************
3085 * widClose [internal]
3087 static DWORD widClose(WORD wDevID)
3089 DWORD ret = MMSYSERR_NOERROR;
3092 TRACE("(%u);\n", wDevID);
3094 if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].p_handle == NULL) {
3095 WARN("bad device ID !\n");
3096 return MMSYSERR_BADDEVICEID;
3099 wwi = &WInDev[wDevID];
3100 if (wwi->lpQueuePtr) {
3101 WARN("buffers still playing !\n");
3102 ret = WAVERR_STILLPLAYING;
3104 if (wwi->hThread != INVALID_HANDLE_VALUE) {
3105 ALSA_AddRingMessage(&wwi->msgRing, WINE_WM_CLOSING, 0, TRUE);
3107 ALSA_DestroyRingMessage(&wwi->msgRing);
3109 snd_pcm_hw_params_free(wwi->hw_params);
3110 wwi->hw_params = NULL;
3112 snd_pcm_close(wwi->p_handle);
3113 wwi->p_handle = NULL;
3115 ret = widNotifyClient(wwi, WIM_CLOSE, 0L, 0L);
3118 HeapFree(GetProcessHeap(), 0, wwi->ufds);
3122 /**************************************************************************
3123 * widAddBuffer [internal]
3126 static DWORD widAddBuffer(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
3128 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
3130 /* first, do the sanity checks... */
3131 if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].p_handle == NULL) {
3132 WARN("bad dev ID !\n");
3133 return MMSYSERR_BADDEVICEID;
3136 if (lpWaveHdr->lpData == NULL || !(lpWaveHdr->dwFlags & WHDR_PREPARED))
3137 return WAVERR_UNPREPARED;
3139 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
3140 return WAVERR_STILLPLAYING;
3142 lpWaveHdr->dwFlags &= ~WHDR_DONE;
3143 lpWaveHdr->dwFlags |= WHDR_INQUEUE;
3144 lpWaveHdr->lpNext = 0;
3146 ALSA_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_HEADER, (DWORD)lpWaveHdr, FALSE);
3148 return MMSYSERR_NOERROR;
3151 /**************************************************************************
3152 * widPrepare [internal]
3154 static DWORD widPrepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
3156 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
3158 if (wDevID >= MAX_WAVEINDRV) {
3159 WARN("bad device ID !\n");
3160 return MMSYSERR_BADDEVICEID;
3163 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
3164 return WAVERR_STILLPLAYING;
3166 lpWaveHdr->dwFlags |= WHDR_PREPARED;
3167 lpWaveHdr->dwFlags &= ~WHDR_DONE;
3168 return MMSYSERR_NOERROR;
3171 /**************************************************************************
3172 * widUnprepare [internal]
3174 static DWORD widUnprepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
3176 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
3178 if (wDevID >= MAX_WAVEINDRV) {
3179 WARN("bad device ID !\n");
3180 return MMSYSERR_BADDEVICEID;
3183 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
3184 return WAVERR_STILLPLAYING;
3186 lpWaveHdr->dwFlags &= ~WHDR_PREPARED;
3187 lpWaveHdr->dwFlags |= WHDR_DONE;
3189 return MMSYSERR_NOERROR;
3192 /**************************************************************************
3193 * widStart [internal]
3196 static DWORD widStart(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
3198 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
3200 /* first, do the sanity checks... */
3201 if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].p_handle == NULL) {
3202 WARN("bad dev ID !\n");
3203 return MMSYSERR_BADDEVICEID;
3206 ALSA_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_STARTING, 0, TRUE);
3210 return MMSYSERR_NOERROR;
3213 /**************************************************************************
3214 * widStop [internal]
3217 static DWORD widStop(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
3219 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
3221 /* first, do the sanity checks... */
3222 if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].p_handle == NULL) {
3223 WARN("bad dev ID !\n");
3224 return MMSYSERR_BADDEVICEID;
3227 ALSA_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_STOPPING, 0, TRUE);
3229 return MMSYSERR_NOERROR;
3232 /**************************************************************************
3233 * widReset [internal]
3235 static DWORD widReset(WORD wDevID)
3237 TRACE("(%u);\n", wDevID);
3238 if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].state == WINE_WS_CLOSED) {
3239 WARN("can't reset !\n");
3240 return MMSYSERR_INVALHANDLE;
3242 ALSA_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_RESETTING, 0, TRUE);
3243 return MMSYSERR_NOERROR;
3246 /**************************************************************************
3247 * widGetPosition [internal]
3249 static DWORD widGetPosition(WORD wDevID, LPMMTIME lpTime, DWORD uSize)
3254 FIXME("(%u, %p, %lu);\n", wDevID, lpTime, uSize);
3256 if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].state == WINE_WS_CLOSED) {
3257 WARN("can't get pos !\n");
3258 return MMSYSERR_INVALHANDLE;
3260 if (lpTime == NULL) return MMSYSERR_INVALPARAM;
3262 wwi = &WInDev[wDevID];
3263 ALSA_AddRingMessage(&wwi->msgRing, WINE_WM_UPDATE, 0, TRUE);
3265 TRACE("wType=%04X !\n", lpTime->wType);
3266 TRACE("wBitsPerSample=%u\n", wwi->format.wBitsPerSample);
3267 TRACE("nSamplesPerSec=%lu\n", wwi->format.wf.nSamplesPerSec);
3268 TRACE("nChannels=%u\n", wwi->format.wf.nChannels);
3269 TRACE("nAvgBytesPerSec=%lu\n", wwi->format.wf.nAvgBytesPerSec);
3270 FIXME("dwTotalRecorded=%lu\n",wwi->dwTotalRecorded);
3271 switch (lpTime->wType) {
3273 lpTime->u.cb = wwi->dwTotalRecorded;
3274 TRACE("TIME_BYTES=%lu\n", lpTime->u.cb);
3277 lpTime->u.sample = wwi->dwTotalRecorded * 8 /
3278 wwi->format.wBitsPerSample / wwi->format.wf.nChannels;
3279 TRACE("TIME_SAMPLES=%lu\n", lpTime->u.sample);
3282 time = wwi->dwTotalRecorded /
3283 (wwi->format.wf.nAvgBytesPerSec / 1000);
3284 lpTime->u.smpte.hour = time / (60 * 60 * 1000);
3285 time -= lpTime->u.smpte.hour * (60 * 60 * 1000);
3286 lpTime->u.smpte.min = time / (60 * 1000);
3287 time -= lpTime->u.smpte.min * (60 * 1000);
3288 lpTime->u.smpte.sec = time / 1000;
3289 time -= lpTime->u.smpte.sec * 1000;
3290 lpTime->u.smpte.frame = time * 30 / 1000;
3291 lpTime->u.smpte.fps = 30;
3292 TRACE("TIME_SMPTE=%02u:%02u:%02u:%02u\n",
3293 lpTime->u.smpte.hour, lpTime->u.smpte.min,
3294 lpTime->u.smpte.sec, lpTime->u.smpte.frame);
3297 FIXME("format not supported (%u) ! use TIME_MS !\n", lpTime->wType);
3298 lpTime->wType = TIME_MS;
3300 lpTime->u.ms = wwi->dwTotalRecorded /
3301 (wwi->format.wf.nAvgBytesPerSec / 1000);
3302 TRACE("TIME_MS=%lu\n", lpTime->u.ms);
3305 return MMSYSERR_NOERROR;
3308 /**************************************************************************
3309 * widGetNumDevs [internal]
3311 static DWORD widGetNumDevs(void)
3313 return ALSA_WidNumDevs;
3316 /**************************************************************************
3317 * widMessage (WINEALSA.@)
3319 DWORD WINAPI ALSA_widMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
3320 DWORD dwParam1, DWORD dwParam2)
3322 TRACE("(%u, %04X, %08lX, %08lX, %08lX);\n",
3323 wDevID, wMsg, dwUser, dwParam1, dwParam2);
3330 /* FIXME: Pretend this is supported */
3332 case WIDM_OPEN: return widOpen (wDevID, (LPWAVEOPENDESC)dwParam1, dwParam2);
3333 case WIDM_CLOSE: return widClose (wDevID);
3334 case WIDM_ADDBUFFER: return widAddBuffer (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
3335 case WIDM_PREPARE: return widPrepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
3336 case WIDM_UNPREPARE: return widUnprepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
3337 case WIDM_GETDEVCAPS: return widGetDevCaps (wDevID, (LPWAVEOUTCAPSA)dwParam1, dwParam2);
3338 case WIDM_GETNUMDEVS: return widGetNumDevs ();
3339 case WIDM_GETPOS: return widGetPosition (wDevID, (LPMMTIME)dwParam1, dwParam2);
3340 case WIDM_RESET: return widReset (wDevID);
3341 case WIDM_START: return widStart (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
3342 case WIDM_STOP: return widStop (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
3343 /*case DRV_QUERYDEVICEINTERFACESIZE: return wdDevInterfaceSize (wDevID, (LPDWORD)dwParam1);
3344 case DRV_QUERYDEVICEINTERFACE: return wdDevInterface (wDevID, (PWCHAR)dwParam1, dwParam2);
3345 case DRV_QUERYDSOUNDIFACE: return widDsCreate (wDevID, (PIDSCDRIVER*)dwParam1);
3346 case DRV_QUERYDSOUNDDESC: return widDsDesc (wDevID, (PDSDRIVERDESC)dwParam1);
3347 case DRV_QUERYDSOUNDGUID: return widDsGuid (wDevID, (LPGUID)dwParam1);*/
3349 FIXME("unknown message %d!\n", wMsg);
3351 return MMSYSERR_NOTSUPPORTED;
3356 #if !(defined(HAVE_ALSA) && ((SND_LIB_MAJOR == 0 && SND_LIB_MINOR >= 9) || SND_LIB_MAJOR >= 1))
3358 /**************************************************************************
3359 * widMessage (WINEALSA.@)
3361 DWORD WINAPI ALSA_widMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
3362 DWORD dwParam1, DWORD dwParam2)
3364 FIXME("(%u, %04X, %08lX, %08lX, %08lX):stub\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
3365 return MMSYSERR_NOTENABLED;
3372 /**************************************************************************
3373 * wodMessage (WINEALSA.@)
3375 DWORD WINAPI ALSA_wodMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
3376 DWORD dwParam1, DWORD dwParam2)
3378 FIXME("(%u, %04X, %08lX, %08lX, %08lX):stub\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
3379 return MMSYSERR_NOTENABLED;
3382 #endif /* HAVE_ALSA */