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
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "wine/port.h"
36 #ifdef HAVE_SYS_IOCTL_H
37 # include <sys/ioctl.h>
39 #ifdef HAVE_SYS_MMAN_H
40 # include <sys/mman.h>
51 #include "wine/library.h"
52 #include "wine/debug.h"
54 WINE_DEFAULT_DEBUG_CHANNEL(wave);
57 #if defined(HAVE_ALSA) && ((SND_LIB_MAJOR == 0 && SND_LIB_MINOR >= 9) || SND_LIB_MAJOR >= 1)
59 /* internal ALSALIB functions */
60 snd_pcm_uframes_t _snd_pcm_mmap_hw_ptr(snd_pcm_t *pcm);
63 #define MAX_WAVEOUTDRV (1)
64 #define MAX_WAVEINDRV (1)
66 /* state diagram for waveOut writing:
68 * +---------+-------------+---------------+---------------------------------+
69 * | state | function | event | new state |
70 * +---------+-------------+---------------+---------------------------------+
71 * | | open() | | STOPPED |
72 * | PAUSED | write() | | PAUSED |
73 * | STOPPED | write() | <thrd create> | PLAYING |
74 * | PLAYING | write() | HEADER | PLAYING |
75 * | (other) | write() | <error> | |
76 * | (any) | pause() | PAUSING | PAUSED |
77 * | PAUSED | restart() | RESTARTING | PLAYING (if no thrd => STOPPED) |
78 * | (any) | reset() | RESETTING | STOPPED |
79 * | (any) | close() | CLOSING | CLOSED |
80 * +---------+-------------+---------------+---------------------------------+
83 /* states of the playing device */
84 #define WINE_WS_PLAYING 0
85 #define WINE_WS_PAUSED 1
86 #define WINE_WS_STOPPED 2
87 #define WINE_WS_CLOSED 3
89 /* events to be send to device */
91 WINE_WM_PAUSING = WM_USER + 1, WINE_WM_RESTARTING, WINE_WM_RESETTING, WINE_WM_HEADER,
92 WINE_WM_UPDATE, WINE_WM_BREAKLOOP, WINE_WM_CLOSING
96 enum win_wm_message msg; /* message identifier */
97 DWORD param; /* parameter for this message */
98 HANDLE hEvent; /* if message is synchronous, handle of event for synchro */
101 /* implement an in-process message ring for better performance
102 * (compared to passing thru the server)
103 * this ring will be used by the input (resp output) record (resp playback) routine
106 /* FIXME: this could be made a dynamically growing array (if needed) */
107 #define ALSA_RING_BUFFER_SIZE 30
108 ALSA_MSG messages[ALSA_RING_BUFFER_SIZE];
112 CRITICAL_SECTION msg_crst;
116 /* Windows information */
117 volatile int state; /* one of the WINE_WS_ manifest constants */
118 WAVEOPENDESC waveDesc;
120 PCMWAVEFORMAT format;
123 /* ALSA information (ALSA 0.9/1.x uses two different devices for playback/capture) */
125 snd_pcm_t* p_handle; /* handle to ALSA playback device */
126 snd_pcm_t* c_handle; /* handle to ALSA capture device */
127 snd_pcm_hw_params_t * hw_params; /* ALSA Hw params */
129 snd_ctl_t * ctl; /* control handle for the playback volume */
130 snd_ctl_elem_id_t * playback_eid; /* element id of the playback volume control */
131 snd_ctl_elem_value_t * playback_evalue; /* element value of the playback volume control */
132 snd_ctl_elem_info_t * playback_einfo; /* element info of the playback volume control */
134 snd_pcm_sframes_t (*write)(snd_pcm_t *, const void *, snd_pcm_uframes_t );
136 DWORD dwBufferSize; /* size of whole ALSA buffer in bytes */
137 LPWAVEHDR lpQueuePtr; /* start of queued WAVEHDRs (waiting to be notified) */
138 LPWAVEHDR lpPlayPtr; /* start of not yet fully played buffers */
140 LPWAVEHDR lpLoopPtr; /* pointer of first buffer in loop, if any */
141 DWORD dwLoops; /* private copy of loop counter */
145 /* synchronization stuff */
146 HANDLE hStartUpEvent;
149 ALSA_MSG_RING msgRing;
151 /* DirectSound stuff */
155 static WINE_WAVEOUT WOutDev [MAX_WAVEOUTDRV];
156 static DWORD ALSA_WodNumDevs;
158 static DWORD wodDsCreate(UINT wDevID, PIDSDRIVER* drv);
160 /* These strings used only for tracing */
162 static const char *wodPlayerCmdString[] = {
164 "WINE_WM_RESTARTING",
173 /*======================================================================*
174 * Low level WAVE implementation *
175 *======================================================================*/
177 /**************************************************************************
178 * ALSA_InitializeVolumeCtl [internal]
180 * used to initialize the PCM Volume Control
182 static int ALSA_InitializeVolumeCtl(WINE_WAVEOUT * wwo)
184 snd_ctl_t * ctl = NULL;
185 snd_ctl_card_info_t * cardinfo;
186 snd_ctl_elem_list_t * elemlist;
187 snd_ctl_elem_id_t * e_id;
188 snd_ctl_elem_info_t * einfo;
189 snd_hctl_t * hctl = NULL;
190 snd_hctl_elem_t * elem;
194 snd_ctl_card_info_alloca(&cardinfo);
195 memset(cardinfo,0,snd_ctl_card_info_sizeof());
197 snd_ctl_elem_list_alloca(&elemlist);
198 memset(elemlist,0,snd_ctl_elem_list_sizeof());
200 snd_ctl_elem_id_alloca(&e_id);
201 memset(e_id,0,snd_ctl_elem_id_sizeof());
203 snd_ctl_elem_info_alloca(&einfo);
204 memset(einfo,0,snd_ctl_elem_info_sizeof());
206 #define EXIT_ON_ERROR(f,txt) do \
209 if ( (err = (f) ) < 0) \
211 ERR(txt ": %s\n", snd_strerror(err)); \
213 snd_hctl_close(hctl); \
215 snd_ctl_close(ctl); \
220 EXIT_ON_ERROR( snd_ctl_open(&ctl,"hw:0",0) , "ctl open failed" );
221 EXIT_ON_ERROR( snd_ctl_card_info(ctl, cardinfo), "card info failed");
222 EXIT_ON_ERROR( snd_ctl_elem_list(ctl, elemlist), "elem list failed");
224 nCtrls = snd_ctl_elem_list_get_count(elemlist);
226 EXIT_ON_ERROR( snd_hctl_open(&hctl,"hw:0",0), "hctl open failed");
227 EXIT_ON_ERROR( snd_hctl_load(hctl), "hctl load failed" );
229 elem=snd_hctl_first_elem(hctl);
230 for ( i= 0; i<nCtrls; i++) {
232 memset(e_id,0,snd_ctl_elem_id_sizeof());
234 snd_hctl_elem_get_id(elem,e_id);
236 TRACE("ctl: #%d '%s'%d\n",
237 snd_ctl_elem_id_get_numid(e_id),
238 snd_ctl_elem_id_get_name(e_id),
239 snd_ctl_elem_id_get_index(e_id));
241 if ( !strcmp("PCM Playback Volume", snd_ctl_elem_id_get_name(e_id)) )
243 EXIT_ON_ERROR( snd_hctl_elem_info(elem,einfo), "hctl elem info failed" );
245 /* few sanity checks... you'll never know... */
246 if ( snd_ctl_elem_info_get_type(einfo) != SND_CTL_ELEM_TYPE_INTEGER )
247 WARN("playback volume control is not an integer\n");
248 if ( !snd_ctl_elem_info_is_readable(einfo) )
249 WARN("playback volume control is readable\n");
250 if ( !snd_ctl_elem_info_is_writable(einfo) )
251 WARN("playback volume control is readable\n");
253 TRACE(" ctrl range: min=%ld max=%ld step=%ld\n",
254 snd_ctl_elem_info_get_min(einfo),
255 snd_ctl_elem_info_get_max(einfo),
256 snd_ctl_elem_info_get_step(einfo));
258 EXIT_ON_ERROR( snd_ctl_elem_id_malloc(&wwo->playback_eid), "elem id malloc failed" );
259 EXIT_ON_ERROR( snd_ctl_elem_info_malloc(&wwo->playback_einfo), "elem info malloc failed" );
260 EXIT_ON_ERROR( snd_ctl_elem_value_malloc(&wwo->playback_evalue), "elem value malloc failed" );
262 /* ok, now we can safely save these objects for later */
263 snd_ctl_elem_id_copy(wwo->playback_eid, e_id);
264 snd_ctl_elem_info_copy(wwo->playback_einfo, einfo);
265 snd_ctl_elem_value_set_id(wwo->playback_evalue, wwo->playback_eid);
269 elem=snd_hctl_elem_next(elem);
271 snd_hctl_close(hctl);
276 /**************************************************************************
277 * ALSA_XRUNRecovery [internal]
279 * used to recovery from XRUN errors (buffer underflow/overflow)
281 static int ALSA_XRUNRecovery(WINE_WAVEOUT * wwo, int err)
283 if (err == -EPIPE) { /* under-run */
284 err = snd_pcm_prepare(wwo->p_handle);
286 ERR( "underrun recovery failed. prepare failed: %s\n", snd_strerror(err));
288 } else if (err == -ESTRPIPE) {
289 while ((err = snd_pcm_resume(wwo->p_handle)) == -EAGAIN)
290 sleep(1); /* wait until the suspend flag is released */
292 err = snd_pcm_prepare(wwo->p_handle);
294 ERR("recovery from suspend failed, prepare failed: %s\n", snd_strerror(err));
301 /**************************************************************************
302 * ALSA_TraceParameters [internal]
304 * used to trace format changes, hw and sw parameters
306 static void ALSA_TraceParameters(snd_pcm_hw_params_t * hw_params, snd_pcm_sw_params_t * sw, int full)
308 snd_pcm_format_t format = snd_pcm_hw_params_get_format(hw_params);
309 snd_pcm_access_t access = snd_pcm_hw_params_get_access(hw_params);
311 #define X(x) ((x)? "true" : "false")
313 TRACE("FLAGS: sampleres=%s overrng=%s pause=%s resume=%s syncstart=%s batch=%s block=%s double=%s "
314 "halfd=%s joint=%s \n",
315 X(snd_pcm_hw_params_can_mmap_sample_resolution(hw_params)),
316 X(snd_pcm_hw_params_can_overrange(hw_params)),
317 X(snd_pcm_hw_params_can_pause(hw_params)),
318 X(snd_pcm_hw_params_can_resume(hw_params)),
319 X(snd_pcm_hw_params_can_sync_start(hw_params)),
320 X(snd_pcm_hw_params_is_batch(hw_params)),
321 X(snd_pcm_hw_params_is_block_transfer(hw_params)),
322 X(snd_pcm_hw_params_is_double(hw_params)),
323 X(snd_pcm_hw_params_is_half_duplex(hw_params)),
324 X(snd_pcm_hw_params_is_joint_duplex(hw_params)));
328 TRACE("access=%s\n", snd_pcm_access_name(access));
331 snd_pcm_access_mask_t * acmask;
332 snd_pcm_access_mask_alloca(&acmask);
333 snd_pcm_hw_params_get_access_mask(hw_params, acmask);
334 for ( access = SND_PCM_ACCESS_MMAP_INTERLEAVED; access <= SND_PCM_ACCESS_LAST; access++)
335 if (snd_pcm_access_mask_test(acmask, access))
336 TRACE("access=%s\n", snd_pcm_access_name(access));
341 TRACE("format=%s\n", snd_pcm_format_name(format));
346 snd_pcm_format_mask_t * fmask;
348 snd_pcm_format_mask_alloca(&fmask);
349 snd_pcm_hw_params_get_format_mask(hw_params, fmask);
350 for ( format = SND_PCM_FORMAT_S8; format <= SND_PCM_FORMAT_LAST ; format++)
351 if ( snd_pcm_format_mask_test(fmask, format) )
352 TRACE("format=%s\n", snd_pcm_format_name(format));
356 int n = snd_pcm_hw_params_get_##x(hw_params); \
358 TRACE(#x "_min=%ld " #x "_max=%ld\n", \
359 (long int)snd_pcm_hw_params_get_##x##_min(hw_params), \
360 (long int)snd_pcm_hw_params_get_##x##_max(hw_params)); \
362 TRACE(#x "=%d\n", n); \
369 int n = snd_pcm_hw_params_get_##x(hw_params,0); \
371 TRACE(#x "_min=%ld " #x "_max=%ld\n", \
372 (long int)snd_pcm_hw_params_get_##x##_min(hw_params,0), \
373 (long int)snd_pcm_hw_params_get_##x##_max(hw_params,0)); \
375 TRACE(#x "=%d\n", n); \
393 /******************************************************************
396 * Initialize internal structures from ALSA information
398 LONG ALSA_WaveInit(void)
401 snd_pcm_info_t * info;
402 snd_pcm_hw_params_t * hw_params;
407 /* FIXME: use better values */
408 wwo->device = "hw:0,0";
409 wwo->caps.wMid = 0x0002;
410 wwo->caps.wPid = 0x0104;
411 strcpy(wwo->caps.szPname, "SB16 Wave Out");
412 wwo->caps.vDriverVersion = 0x0100;
413 wwo->caps.dwFormats = 0x00000000;
414 wwo->caps.dwSupport = WAVECAPS_VOLUME;
416 if (!wine_dlopen("libasound.so.2", RTLD_LAZY|RTLD_GLOBAL, NULL, 0))
418 ERR("Error: ALSA lib needs to be loaded with flags RTLD_LAZY and RTLD_GLOBAL.\n");
422 snd_pcm_info_alloca(&info);
423 snd_pcm_hw_params_alloca(&hw_params);
425 #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)
428 EXIT_ON_ERROR( snd_pcm_open(&h, wwo->device, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK) , "open pcm" );
431 EXIT_ON_ERROR( snd_pcm_info(h, info) , "pcm info" );
433 TRACE("dev=%d id=%s name=%s subdev=%d subdev_name=%s subdev_avail=%d subdev_num=%d stream=%s subclass=%s \n",
434 snd_pcm_info_get_device(info),
435 snd_pcm_info_get_id(info),
436 snd_pcm_info_get_name(info),
437 snd_pcm_info_get_subdevice(info),
438 snd_pcm_info_get_subdevice_name(info),
439 snd_pcm_info_get_subdevices_avail(info),
440 snd_pcm_info_get_subdevices_count(info),
441 snd_pcm_stream_name(snd_pcm_info_get_stream(info)),
442 (snd_pcm_info_get_subclass(info) == SND_PCM_SUBCLASS_GENERIC_MIX ? "GENERIC MIX": "MULTI MIX"));
444 EXIT_ON_ERROR( snd_pcm_hw_params_any(h, hw_params) , "pcm hw params" );
448 ALSA_TraceParameters(hw_params, NULL, TRUE);
451 snd_pcm_format_mask_t * fmask;
452 int ratemin = snd_pcm_hw_params_get_rate_min(hw_params, 0);
453 int ratemax = snd_pcm_hw_params_get_rate_max(hw_params, 0);
454 int chmin = snd_pcm_hw_params_get_channels_min(hw_params); \
455 int chmax = snd_pcm_hw_params_get_channels_max(hw_params); \
457 snd_pcm_format_mask_alloca(&fmask);
458 snd_pcm_hw_params_get_format_mask(hw_params, fmask);
461 if ( (r) >= ratemin && ( (r) <= ratemax || ratemax == -1) ) \
463 if (snd_pcm_format_mask_test( fmask, SND_PCM_FORMAT_U8)) \
465 if (chmin <= 1 && 1 <= chmax) \
466 wwo->caps.dwFormats |= WAVE_FORMAT_##v##S08; \
467 if (chmin <= 2 && 2 <= chmax) \
468 wwo->caps.dwFormats |= WAVE_FORMAT_##v##S08; \
470 if (snd_pcm_format_mask_test( fmask, SND_PCM_FORMAT_S16_LE)) \
472 if (chmin <= 1 && 1 <= chmax) \
473 wwo->caps.dwFormats |= WAVE_FORMAT_##v##S16; \
474 if (chmin <= 2 && 2 <= chmax) \
475 wwo->caps.dwFormats |= WAVE_FORMAT_##v##S16; \
484 if ( snd_pcm_hw_params_get_channels_min(hw_params) > 1) FIXME("-");
485 wwo->caps.wChannels = (snd_pcm_hw_params_get_channels_max(hw_params) >= 2) ? 2 : 1;
486 if (snd_pcm_hw_params_get_channels_min(hw_params) <= 2 && 2 <= snd_pcm_hw_params_get_channels_max(hw_params))
487 wwo->caps.dwSupport |= WAVECAPS_LRVOLUME;
489 /* FIXME: always true ? */
490 wwo->caps.dwSupport |= WAVECAPS_SAMPLEACCURATE;
493 snd_pcm_access_mask_t * acmask;
494 snd_pcm_access_mask_alloca(&acmask);
495 snd_pcm_hw_params_get_access_mask(hw_params, acmask);
497 /* FIXME: NONITERLEAVED and COMPLEX are not supported right now */
498 if ( snd_pcm_access_mask_test( acmask, SND_PCM_ACCESS_MMAP_INTERLEAVED ) )
499 wwo->caps.dwSupport |= WAVECAPS_DIRECTSOUND;
502 TRACE("Configured with dwFmts=%08lx dwSupport=%08lx\n",
503 wwo->caps.dwFormats, wwo->caps.dwSupport);
507 ALSA_InitializeVolumeCtl(wwo);
512 /******************************************************************
513 * ALSA_InitRingMessage
515 * Initialize the ring of messages for passing between driver's caller and playback/record
518 static int ALSA_InitRingMessage(ALSA_MSG_RING* omr)
522 omr->msg_event = CreateEventA(NULL, FALSE, FALSE, NULL);
523 memset(omr->messages, 0, sizeof(ALSA_MSG) * ALSA_RING_BUFFER_SIZE);
524 InitializeCriticalSection(&omr->msg_crst);
528 /******************************************************************
529 * ALSA_DestroyRingMessage
532 static int ALSA_DestroyRingMessage(ALSA_MSG_RING* omr)
534 CloseHandle(omr->msg_event);
535 DeleteCriticalSection(&omr->msg_crst);
539 /******************************************************************
540 * ALSA_AddRingMessage
542 * Inserts a new message into the ring (should be called from DriverProc derivated routines)
544 static int ALSA_AddRingMessage(ALSA_MSG_RING* omr, enum win_wm_message msg, DWORD param, BOOL wait)
546 HANDLE hEvent = INVALID_HANDLE_VALUE;
548 EnterCriticalSection(&omr->msg_crst);
549 if ((omr->msg_toget == ((omr->msg_tosave + 1) % ALSA_RING_BUFFER_SIZE))) /* buffer overflow ? */
551 ERR("buffer overflow !?\n");
552 LeaveCriticalSection(&omr->msg_crst);
557 hEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
558 if (hEvent == INVALID_HANDLE_VALUE)
560 ERR("can't create event !?\n");
561 LeaveCriticalSection(&omr->msg_crst);
564 if (omr->msg_toget != omr->msg_tosave && omr->messages[omr->msg_toget].msg != WINE_WM_HEADER)
565 FIXME("two fast messages in the queue!!!!\n");
567 /* fast messages have to be added at the start of the queue */
568 omr->msg_toget = (omr->msg_toget + ALSA_RING_BUFFER_SIZE - 1) % ALSA_RING_BUFFER_SIZE;
570 omr->messages[omr->msg_toget].msg = msg;
571 omr->messages[omr->msg_toget].param = param;
572 omr->messages[omr->msg_toget].hEvent = hEvent;
576 omr->messages[omr->msg_tosave].msg = msg;
577 omr->messages[omr->msg_tosave].param = param;
578 omr->messages[omr->msg_tosave].hEvent = INVALID_HANDLE_VALUE;
579 omr->msg_tosave = (omr->msg_tosave + 1) % ALSA_RING_BUFFER_SIZE;
581 LeaveCriticalSection(&omr->msg_crst);
582 /* signal a new message */
583 SetEvent(omr->msg_event);
586 /* wait for playback/record thread to have processed the message */
587 WaitForSingleObject(hEvent, INFINITE);
593 /******************************************************************
594 * ALSA_RetrieveRingMessage
596 * Get a message from the ring. Should be called by the playback/record thread.
598 static int ALSA_RetrieveRingMessage(ALSA_MSG_RING* omr,
599 enum win_wm_message *msg, DWORD *param, HANDLE *hEvent)
601 EnterCriticalSection(&omr->msg_crst);
603 if (omr->msg_toget == omr->msg_tosave) /* buffer empty ? */
605 LeaveCriticalSection(&omr->msg_crst);
609 *msg = omr->messages[omr->msg_toget].msg;
610 omr->messages[omr->msg_toget].msg = 0;
611 *param = omr->messages[omr->msg_toget].param;
612 *hEvent = omr->messages[omr->msg_toget].hEvent;
613 omr->msg_toget = (omr->msg_toget + 1) % ALSA_RING_BUFFER_SIZE;
614 LeaveCriticalSection(&omr->msg_crst);
618 /*======================================================================*
619 * Low level WAVE OUT implementation *
620 *======================================================================*/
623 /**************************************************************************
624 * wodNotifyClient [internal]
626 static DWORD wodNotifyClient(WINE_WAVEOUT* wwo, WORD wMsg, DWORD dwParam1, DWORD dwParam2)
628 TRACE("wMsg = 0x%04x dwParm1 = %04lX dwParam2 = %04lX\n", wMsg, dwParam1, dwParam2);
634 if (wwo->wFlags != DCB_NULL &&
635 !DriverCallback(wwo->waveDesc.dwCallback, wwo->wFlags, (HDRVR)wwo->waveDesc.hWave,
636 wMsg, wwo->waveDesc.dwInstance, dwParam1, dwParam2)) {
637 WARN("can't notify client !\n");
638 return MMSYSERR_ERROR;
642 FIXME("Unknown callback message %u\n", wMsg);
643 return MMSYSERR_INVALPARAM;
645 return MMSYSERR_NOERROR;
648 /**************************************************************************
649 * wodUpdatePlayedTotal [internal]
652 static BOOL wodUpdatePlayedTotal(WINE_WAVEOUT* wwo, snd_pcm_status_t* ps)
657 /**************************************************************************
658 * wodPlayer_BeginWaveHdr [internal]
660 * Makes the specified lpWaveHdr the currently playing wave header.
661 * If the specified wave header is a begin loop and we're not already in
662 * a loop, setup the loop.
664 static void wodPlayer_BeginWaveHdr(WINE_WAVEOUT* wwo, LPWAVEHDR lpWaveHdr)
666 wwo->lpPlayPtr = lpWaveHdr;
668 if (!lpWaveHdr) return;
670 wwo->lpPlayPtr->reserved = 0;
672 if (lpWaveHdr->dwFlags & WHDR_BEGINLOOP) {
673 if (wwo->lpLoopPtr) {
674 WARN("Already in a loop. Discarding loop on this header (%p)\n", lpWaveHdr);
676 TRACE("Starting loop (%ldx) with %p\n", lpWaveHdr->dwLoops, lpWaveHdr);
677 wwo->lpLoopPtr = lpWaveHdr;
678 /* Windows does not touch WAVEHDR.dwLoops,
679 * so we need to make an internal copy */
680 wwo->dwLoops = lpWaveHdr->dwLoops;
685 /**************************************************************************
686 * wodPlayer_PlayPtrNext [internal]
688 * Advance the play pointer to the next waveheader, looping if required.
690 static LPWAVEHDR wodPlayer_PlayPtrNext(WINE_WAVEOUT* wwo)
692 LPWAVEHDR lpWaveHdr = wwo->lpPlayPtr;
694 if ((lpWaveHdr->dwFlags & WHDR_ENDLOOP) && wwo->lpLoopPtr) {
695 /* We're at the end of a loop, loop if required */
696 if (--wwo->dwLoops > 0) {
697 wwo->lpPlayPtr = wwo->lpLoopPtr;
698 wwo->lpPlayPtr->reserved = 0;
700 /* Handle overlapping loops correctly */
701 if (wwo->lpLoopPtr != lpWaveHdr && (lpWaveHdr->dwFlags & WHDR_BEGINLOOP)) {
702 FIXME("Correctly handled case ? (ending loop buffer also starts a new loop)\n");
703 /* shall we consider the END flag for the closing loop or for
704 * the opening one or for both ???
705 * code assumes for closing loop only
708 lpWaveHdr = lpWaveHdr->lpNext;
710 wwo->lpLoopPtr = NULL;
711 wodPlayer_BeginWaveHdr(wwo, lpWaveHdr);
714 /* We're not in a loop. Advance to the next wave header */
715 wodPlayer_BeginWaveHdr(wwo, lpWaveHdr = lpWaveHdr->lpNext);
721 /**************************************************************************
722 * wodPlayer_DSPWait [internal]
723 * Returns the number of milliseconds to wait for the DSP buffer to play a
726 static DWORD wodPlayer_DSPWait(const WINE_WAVEOUT *wwo)
728 /* time for one period to be played */
729 return snd_pcm_hw_params_get_period_time(wwo->hw_params, 0) / 1000;
732 /**************************************************************************
733 * wodPllayer_NotifyWait [internal]
734 * Returns the number of milliseconds to wait before attempting to notify
735 * completion of the specified wavehdr.
736 * This is based on the number of bytes remaining to be written in the
739 static DWORD wodPlayer_NotifyWait(const WINE_WAVEOUT* wwo, LPWAVEHDR lpWaveHdr)
743 dwMillis = (lpWaveHdr->dwBufferLength - lpWaveHdr->reserved) * 1000 / wwo->format.wf.nAvgBytesPerSec;
744 if (!dwMillis) dwMillis = 1;
750 /**************************************************************************
751 * wodPlayer_WriteMaxFrags [internal]
752 * Writes the maximum number of frames possible to the DSP and returns
753 * the number of frames written.
755 static int wodPlayer_WriteMaxFrags(WINE_WAVEOUT* wwo, DWORD* frames)
757 /* Only attempt to write to free frames */
758 LPWAVEHDR lpWaveHdr = wwo->lpPlayPtr;
759 DWORD dwLength = snd_pcm_bytes_to_frames(wwo->p_handle, lpWaveHdr->dwBufferLength - lpWaveHdr->reserved);
760 int toWrite = min(dwLength, *frames);
763 TRACE("Writing wavehdr %p.%lu[%lu]\n", lpWaveHdr, lpWaveHdr->reserved, lpWaveHdr->dwBufferLength);
765 written = (wwo->write)(wwo->p_handle, lpWaveHdr->lpData + lpWaveHdr->reserved, toWrite);
768 /* XRUN occurred. let's try to recover */
769 ALSA_XRUNRecovery(wwo, written);
770 written = (wwo->write)(wwo->p_handle, lpWaveHdr->lpData + lpWaveHdr->reserved, toWrite);
775 ERR("Error in writing wavehdr. Reason: %s\n", snd_strerror(written));
779 lpWaveHdr->reserved += snd_pcm_frames_to_bytes(wwo->p_handle, written);
780 if ( lpWaveHdr->reserved >= lpWaveHdr->dwBufferLength) {
781 /* this will be used to check if the given wave header has been fully played or not... */
782 lpWaveHdr->reserved = lpWaveHdr->dwBufferLength;
783 /* If we wrote all current wavehdr, skip to the next one */
784 wodPlayer_PlayPtrNext(wwo);
787 wwo->dwPlayedTotal += snd_pcm_frames_to_bytes(wwo->p_handle, written);
793 /**************************************************************************
794 * wodPlayer_NotifyCompletions [internal]
796 * Notifies and remove from queue all wavehdrs which have been played to
797 * the speaker (ie. they have cleared the ALSA buffer). If force is true,
798 * we notify all wavehdrs and remove them all from the queue even if they
799 * are unplayed or part of a loop.
801 static DWORD wodPlayer_NotifyCompletions(WINE_WAVEOUT* wwo, BOOL force)
805 /* Start from lpQueuePtr and keep notifying until:
806 * - we hit an unwritten wavehdr
807 * - we hit the beginning of a running loop
808 * - we hit a wavehdr which hasn't finished playing
810 while ((lpWaveHdr = wwo->lpQueuePtr) &&
812 (lpWaveHdr != wwo->lpPlayPtr &&
813 lpWaveHdr != wwo->lpLoopPtr &&
814 lpWaveHdr->reserved == lpWaveHdr->dwBufferLength))) {
816 wwo->lpQueuePtr = lpWaveHdr->lpNext;
818 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
819 lpWaveHdr->dwFlags |= WHDR_DONE;
821 wodNotifyClient(wwo, WOM_DONE, (DWORD)lpWaveHdr, 0);
823 return (lpWaveHdr && lpWaveHdr != wwo->lpPlayPtr && lpWaveHdr != wwo->lpLoopPtr) ?
824 wodPlayer_NotifyWait(wwo, lpWaveHdr) : INFINITE;
827 /**************************************************************************
828 * wodPlayer_Reset [internal]
830 * wodPlayer helper. Resets current output stream.
832 static void wodPlayer_Reset(WINE_WAVEOUT* wwo)
834 enum win_wm_message msg;
839 /* updates current notify list */
840 wodPlayer_NotifyCompletions(wwo, FALSE);
842 if ( (err = snd_pcm_drop(wwo->p_handle)) < 0) {
843 FIXME("flush: %s\n", snd_strerror(err));
845 wwo->state = WINE_WS_STOPPED;
848 if ( (err = snd_pcm_prepare(wwo->p_handle)) < 0 )
849 ERR("pcm prepare failed: %s\n", snd_strerror(err));
851 /* remove any buffer */
852 wodPlayer_NotifyCompletions(wwo, TRUE);
854 wwo->lpPlayPtr = wwo->lpQueuePtr = wwo->lpLoopPtr = NULL;
855 wwo->state = WINE_WS_STOPPED;
857 /* remove any existing message in the ring */
858 EnterCriticalSection(&wwo->msgRing.msg_crst);
859 /* return all pending headers in queue */
860 while (ALSA_RetrieveRingMessage(&wwo->msgRing, &msg, ¶m, &ev))
862 if (msg != WINE_WM_HEADER)
864 FIXME("shouldn't have headers left\n");
868 ((LPWAVEHDR)param)->dwFlags &= ~WHDR_INQUEUE;
869 ((LPWAVEHDR)param)->dwFlags |= WHDR_DONE;
871 wodNotifyClient(wwo, WOM_DONE, param, 0);
873 ResetEvent(wwo->msgRing.msg_event);
874 LeaveCriticalSection(&wwo->msgRing.msg_crst);
877 /**************************************************************************
878 * wodPlayer_ProcessMessages [internal]
880 static void wodPlayer_ProcessMessages(WINE_WAVEOUT* wwo)
883 enum win_wm_message msg;
888 while (ALSA_RetrieveRingMessage(&wwo->msgRing, &msg, ¶m, &ev)) {
889 /* TRACE("Received %s %lx\n", wodPlayerCmdString[msg - WM_USER - 1], param); */
892 case WINE_WM_PAUSING:
893 if ( snd_pcm_state(wwo->p_handle) == SND_PCM_STATE_RUNNING )
895 err = snd_pcm_pause(wwo->p_handle, 1);
897 ERR("pcm_pause failed: %s\n", snd_strerror(err));
899 wwo->state = WINE_WS_PAUSED;
902 case WINE_WM_RESTARTING:
903 if (wwo->state == WINE_WS_PAUSED)
905 if ( snd_pcm_state(wwo->p_handle) == SND_PCM_STATE_PAUSED )
907 err = snd_pcm_pause(wwo->p_handle, 0);
909 ERR("pcm_pause failed: %s\n", snd_strerror(err));
911 wwo->state = WINE_WS_PLAYING;
916 lpWaveHdr = (LPWAVEHDR)param;
918 /* insert buffer at the end of queue */
921 for (wh = &(wwo->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
925 wodPlayer_BeginWaveHdr(wwo,lpWaveHdr);
926 if (wwo->state == WINE_WS_STOPPED)
927 wwo->state = WINE_WS_PLAYING;
929 case WINE_WM_RESETTING:
930 wodPlayer_Reset(wwo);
934 wodUpdatePlayedTotal(wwo, NULL);
937 case WINE_WM_BREAKLOOP:
938 if (wwo->state == WINE_WS_PLAYING && wwo->lpLoopPtr != NULL) {
939 /* ensure exit at end of current loop */
944 case WINE_WM_CLOSING:
945 /* sanity check: this should not happen since the device must have been reset before */
946 if (wwo->lpQueuePtr || wwo->lpPlayPtr) ERR("out of sync\n");
948 wwo->state = WINE_WS_CLOSED;
951 /* shouldn't go here */
953 FIXME("unknown message %d\n", msg);
959 /**************************************************************************
960 * wodPlayer_FeedDSP [internal]
961 * Feed as much sound data as we can into the DSP and return the number of
962 * milliseconds before it will be necessary to feed the DSP again.
964 static DWORD wodPlayer_FeedDSP(WINE_WAVEOUT* wwo)
966 DWORD availInQ = snd_pcm_avail_update(wwo->p_handle);
968 /* no more room... no need to try to feed */
969 while (wwo->lpPlayPtr && availInQ > 0)
970 if ( wodPlayer_WriteMaxFrags(wwo, &availInQ) < 0 )
973 return wodPlayer_DSPWait(wwo);
976 /**************************************************************************
977 * wodPlayer [internal]
979 static DWORD CALLBACK wodPlayer(LPVOID pmt)
981 WORD uDevID = (DWORD)pmt;
982 WINE_WAVEOUT* wwo = (WINE_WAVEOUT*)&WOutDev[uDevID];
983 DWORD dwNextFeedTime = INFINITE; /* Time before DSP needs feeding */
984 DWORD dwNextNotifyTime = INFINITE; /* Time before next wave completion */
987 wwo->state = WINE_WS_STOPPED;
988 SetEvent(wwo->hStartUpEvent);
991 /** Wait for the shortest time before an action is required. If there
992 * are no pending actions, wait forever for a command.
994 dwSleepTime = min(dwNextFeedTime, dwNextNotifyTime);
995 TRACE("waiting %lums (%lu,%lu)\n", dwSleepTime, dwNextFeedTime, dwNextNotifyTime);
996 WaitForSingleObject(wwo->msgRing.msg_event, dwSleepTime);
997 wodPlayer_ProcessMessages(wwo);
998 if (wwo->state == WINE_WS_PLAYING) {
999 dwNextFeedTime = wodPlayer_FeedDSP(wwo);
1000 dwNextNotifyTime = wodPlayer_NotifyCompletions(wwo, FALSE);
1002 dwNextFeedTime = dwNextNotifyTime = INFINITE;
1007 /**************************************************************************
1008 * wodGetDevCaps [internal]
1010 static DWORD wodGetDevCaps(WORD wDevID, LPWAVEOUTCAPSA lpCaps, DWORD dwSize)
1012 TRACE("(%u, %p, %lu);\n", wDevID, lpCaps, dwSize);
1014 if (lpCaps == NULL) return MMSYSERR_NOTENABLED;
1016 if (wDevID >= MAX_WAVEOUTDRV) {
1017 TRACE("MAX_WAVOUTDRV reached !\n");
1018 return MMSYSERR_BADDEVICEID;
1021 memcpy(lpCaps, &WOutDev[wDevID].caps, min(dwSize, sizeof(*lpCaps)));
1022 return MMSYSERR_NOERROR;
1025 /**************************************************************************
1026 * wodOpen [internal]
1028 static DWORD wodOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
1031 snd_pcm_hw_params_t * hw_params;
1032 snd_pcm_sw_params_t * sw_params;
1033 snd_pcm_access_t access;
1034 snd_pcm_format_t format;
1036 int buffer_size = 0x4000; /* in frames (1 frame = sizeof(sample) * n.channels) */
1037 int num_periods = 32;
1038 snd_pcm_uframes_t period_size;
1043 snd_pcm_hw_params_alloca(&hw_params);
1044 snd_pcm_sw_params_alloca(&sw_params);
1046 TRACE("(%u, %p, %08lX);\n", wDevID, lpDesc, dwFlags);
1047 if (lpDesc == NULL) {
1048 WARN("Invalid Parameter !\n");
1049 return MMSYSERR_INVALPARAM;
1051 if (wDevID >= MAX_WAVEOUTDRV) {
1052 TRACE("MAX_WAVOUTDRV reached !\n");
1053 return MMSYSERR_BADDEVICEID;
1056 /* only PCM format is supported so far... */
1057 if (lpDesc->lpFormat->wFormatTag != WAVE_FORMAT_PCM ||
1058 lpDesc->lpFormat->nChannels == 0 ||
1059 lpDesc->lpFormat->nSamplesPerSec == 0) {
1060 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
1061 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1062 lpDesc->lpFormat->nSamplesPerSec);
1063 return WAVERR_BADFORMAT;
1066 if (dwFlags & WAVE_FORMAT_QUERY) {
1067 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
1068 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1069 lpDesc->lpFormat->nSamplesPerSec);
1070 return MMSYSERR_NOERROR;
1073 wwo = &WOutDev[wDevID];
1075 if ((dwFlags & WAVE_DIRECTSOUND) && !(wwo->caps.dwSupport & WAVECAPS_DIRECTSOUND))
1076 /* not supported, ignore it */
1077 dwFlags &= ~WAVE_DIRECTSOUND;
1080 flags = SND_PCM_NONBLOCK;
1081 if ( dwFlags & WAVE_DIRECTSOUND )
1082 flags |= SND_PCM_ASYNC;
1084 if (snd_pcm_open(&pcm, wwo->device, SND_PCM_STREAM_PLAYBACK, dwFlags))
1086 ERR("Error open: %s\n", snd_strerror(errno));
1087 return MMSYSERR_NOTENABLED;
1090 wwo->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
1092 memcpy(&wwo->waveDesc, lpDesc, sizeof(WAVEOPENDESC));
1093 memcpy(&wwo->format, lpDesc->lpFormat, sizeof(PCMWAVEFORMAT));
1095 if (wwo->format.wBitsPerSample == 0) {
1096 WARN("Resetting zeroed wBitsPerSample\n");
1097 wwo->format.wBitsPerSample = 8 *
1098 (wwo->format.wf.nAvgBytesPerSec /
1099 wwo->format.wf.nSamplesPerSec) /
1100 wwo->format.wf.nChannels;
1103 snd_pcm_hw_params_any(pcm, hw_params);
1105 #define EXIT_ON_ERROR(f,e,txt) do \
1108 if ( (err = (f) ) < 0) \
1110 ERR(txt ": %s\n", snd_strerror(err)); \
1111 snd_pcm_close(pcm); \
1116 access = SND_PCM_ACCESS_MMAP_INTERLEAVED;
1117 if ( ( err = snd_pcm_hw_params_set_access(pcm, hw_params, access ) ) < 0) {
1118 WARN("mmap not available. switching to standard write.\n");
1119 access = SND_PCM_ACCESS_RW_INTERLEAVED;
1120 EXIT_ON_ERROR( snd_pcm_hw_params_set_access(pcm, hw_params, access ), MMSYSERR_INVALPARAM, "unable to set access for playback");
1121 wwo->write = snd_pcm_writei;
1124 wwo->write = snd_pcm_mmap_writei;
1126 EXIT_ON_ERROR( snd_pcm_hw_params_set_channels(pcm, hw_params, wwo->format.wf.nChannels), MMSYSERR_INVALPARAM, "unable to set required channels");
1128 format = (wwo->format.wBitsPerSample == 16) ? SND_PCM_FORMAT_S16_LE : SND_PCM_FORMAT_U8;
1129 EXIT_ON_ERROR( snd_pcm_hw_params_set_format(pcm, hw_params, format), MMSYSERR_INVALPARAM, "unable to set required format");
1131 EXIT_ON_ERROR( snd_pcm_hw_params_set_buffer_size_near(pcm, hw_params, buffer_size), MMSYSERR_NOMEM, "unable to get required buffer");
1132 buffer_size = snd_pcm_hw_params_get_buffer_size(hw_params);
1134 EXIT_ON_ERROR( snd_pcm_hw_params_set_period_size_near(pcm, hw_params, buffer_size/num_periods, 0), MMSYSERR_ERROR, "unable to set required period size");
1135 period_size = snd_pcm_hw_params_get_period_size(hw_params, 0);
1137 rate = snd_pcm_hw_params_set_rate_near(pcm, hw_params, wwo->format.wf.nSamplesPerSec, 0);
1139 ERR("Rate %ld Hz not available for playback: %s\n", wwo->format.wf.nSamplesPerSec, snd_strerror(rate));
1141 return WAVERR_BADFORMAT;
1143 if (rate != wwo->format.wf.nSamplesPerSec) {
1144 ERR("Rate doesn't match (requested %ld Hz, got %d Hz)\n", wwo->format.wf.nSamplesPerSec, rate);
1146 return WAVERR_BADFORMAT;
1149 EXIT_ON_ERROR( snd_pcm_hw_params(pcm, hw_params), MMSYSERR_INVALPARAM, "unable to set hw params for playback");
1151 snd_pcm_sw_params_current(pcm, sw_params);
1152 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");
1153 EXIT_ON_ERROR( snd_pcm_sw_params_set_silence_size(pcm, sw_params, period_size*2), MMSYSERR_ERROR, "unable to set silence size");
1154 EXIT_ON_ERROR( snd_pcm_sw_params_set_avail_min(pcm, sw_params, period_size), MMSYSERR_ERROR, "unable to set avail min");
1155 EXIT_ON_ERROR( snd_pcm_sw_params_set_xfer_align(pcm, sw_params, 1), MMSYSERR_ERROR, "unable to set xfer align");
1156 EXIT_ON_ERROR( snd_pcm_sw_params_set_silence_threshold(pcm, sw_params, 0), MMSYSERR_ERROR, "unable to set silence threshold");
1157 EXIT_ON_ERROR( snd_pcm_sw_params(pcm, sw_params), MMSYSERR_ERROR, "unable to set sw params for playback");
1158 #undef EXIT_ON_ERROR
1160 snd_pcm_prepare(pcm);
1163 ALSA_TraceParameters(hw_params, sw_params, FALSE);
1165 /* now, we can save all required data for later use... */
1166 if ( wwo->hw_params )
1167 snd_pcm_hw_params_free(wwo->hw_params);
1168 snd_pcm_hw_params_malloc(&(wwo->hw_params));
1169 snd_pcm_hw_params_copy(wwo->hw_params, hw_params);
1171 wwo->dwBufferSize = buffer_size;
1172 wwo->lpQueuePtr = wwo->lpPlayPtr = wwo->lpLoopPtr = NULL;
1173 wwo->p_handle = pcm;
1175 ALSA_InitRingMessage(&wwo->msgRing);
1177 if (!(dwFlags & WAVE_DIRECTSOUND)) {
1178 wwo->hStartUpEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
1179 wwo->hThread = CreateThread(NULL, 0, wodPlayer, (LPVOID)(DWORD)wDevID, 0, &(wwo->dwThreadID));
1180 WaitForSingleObject(wwo->hStartUpEvent, INFINITE);
1181 CloseHandle(wwo->hStartUpEvent);
1183 wwo->hThread = INVALID_HANDLE_VALUE;
1184 wwo->dwThreadID = 0;
1186 wwo->hStartUpEvent = INVALID_HANDLE_VALUE;
1188 TRACE("handle=%08lx \n", (DWORD)wwo->p_handle);
1189 /* if (wwo->dwFragmentSize % wwo->format.wf.nBlockAlign)
1190 ERR("Fragment doesn't contain an integral number of data blocks\n");
1192 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%lu, nSamplesPerSec=%lu, nChannels=%u nBlockAlign=%u!\n",
1193 wwo->format.wBitsPerSample, wwo->format.wf.nAvgBytesPerSec,
1194 wwo->format.wf.nSamplesPerSec, wwo->format.wf.nChannels,
1195 wwo->format.wf.nBlockAlign);
1197 return wodNotifyClient(wwo, WOM_OPEN, 0L, 0L);
1201 /**************************************************************************
1202 * wodClose [internal]
1204 static DWORD wodClose(WORD wDevID)
1206 DWORD ret = MMSYSERR_NOERROR;
1209 TRACE("(%u);\n", wDevID);
1211 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].p_handle == NULL) {
1212 WARN("bad device ID !\n");
1213 return MMSYSERR_BADDEVICEID;
1216 wwo = &WOutDev[wDevID];
1217 if (wwo->lpQueuePtr) {
1218 WARN("buffers still playing !\n");
1219 ret = WAVERR_STILLPLAYING;
1221 if (wwo->hThread != INVALID_HANDLE_VALUE) {
1222 ALSA_AddRingMessage(&wwo->msgRing, WINE_WM_CLOSING, 0, TRUE);
1224 ALSA_DestroyRingMessage(&wwo->msgRing);
1226 snd_pcm_hw_params_free(wwo->hw_params);
1227 wwo->hw_params = NULL;
1229 snd_pcm_close(wwo->p_handle);
1230 wwo->p_handle = NULL;
1232 ret = wodNotifyClient(wwo, WOM_CLOSE, 0L, 0L);
1238 /**************************************************************************
1239 * wodWrite [internal]
1242 static DWORD wodWrite(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1244 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1246 /* first, do the sanity checks... */
1247 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].p_handle == NULL) {
1248 WARN("bad dev ID !\n");
1249 return MMSYSERR_BADDEVICEID;
1252 if (lpWaveHdr->lpData == NULL || !(lpWaveHdr->dwFlags & WHDR_PREPARED))
1253 return WAVERR_UNPREPARED;
1255 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1256 return WAVERR_STILLPLAYING;
1258 lpWaveHdr->dwFlags &= ~WHDR_DONE;
1259 lpWaveHdr->dwFlags |= WHDR_INQUEUE;
1260 lpWaveHdr->lpNext = 0;
1262 ALSA_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_HEADER, (DWORD)lpWaveHdr, FALSE);
1264 return MMSYSERR_NOERROR;
1267 /**************************************************************************
1268 * wodPrepare [internal]
1270 static DWORD wodPrepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1272 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1274 if (wDevID >= MAX_WAVEOUTDRV) {
1275 WARN("bad device ID !\n");
1276 return MMSYSERR_BADDEVICEID;
1279 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1280 return WAVERR_STILLPLAYING;
1282 lpWaveHdr->dwFlags |= WHDR_PREPARED;
1283 lpWaveHdr->dwFlags &= ~WHDR_DONE;
1284 return MMSYSERR_NOERROR;
1287 /**************************************************************************
1288 * wodUnprepare [internal]
1290 static DWORD wodUnprepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1292 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1294 if (wDevID >= MAX_WAVEOUTDRV) {
1295 WARN("bad device ID !\n");
1296 return MMSYSERR_BADDEVICEID;
1299 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1300 return WAVERR_STILLPLAYING;
1302 lpWaveHdr->dwFlags &= ~WHDR_PREPARED;
1303 lpWaveHdr->dwFlags |= WHDR_DONE;
1305 return MMSYSERR_NOERROR;
1308 /**************************************************************************
1309 * wodPause [internal]
1311 static DWORD wodPause(WORD wDevID)
1313 TRACE("(%u);!\n", wDevID);
1315 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].p_handle == NULL) {
1316 WARN("bad device ID !\n");
1317 return MMSYSERR_BADDEVICEID;
1320 ALSA_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_PAUSING, 0, TRUE);
1322 return MMSYSERR_NOERROR;
1325 /**************************************************************************
1326 * wodRestart [internal]
1328 static DWORD wodRestart(WORD wDevID)
1330 TRACE("(%u);\n", wDevID);
1332 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].p_handle == NULL) {
1333 WARN("bad device ID !\n");
1334 return MMSYSERR_BADDEVICEID;
1337 if (WOutDev[wDevID].state == WINE_WS_PAUSED) {
1338 ALSA_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESTARTING, 0, TRUE);
1341 /* FIXME: is NotifyClient with WOM_DONE right ? (Comet Busters 1.3.3 needs this notification) */
1342 /* FIXME: Myst crashes with this ... hmm -MM
1343 return wodNotifyClient(wwo, WOM_DONE, 0L, 0L);
1346 return MMSYSERR_NOERROR;
1349 /**************************************************************************
1350 * wodReset [internal]
1352 static DWORD wodReset(WORD wDevID)
1354 TRACE("(%u);\n", wDevID);
1356 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].p_handle == NULL) {
1357 WARN("bad device ID !\n");
1358 return MMSYSERR_BADDEVICEID;
1361 ALSA_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESETTING, 0, TRUE);
1363 return MMSYSERR_NOERROR;
1366 /**************************************************************************
1367 * wodGetPosition [internal]
1369 static DWORD wodGetPosition(WORD wDevID, LPMMTIME lpTime, DWORD uSize)
1375 TRACE("(%u, %p, %lu);\n", wDevID, lpTime, uSize);
1377 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].p_handle == NULL) {
1378 WARN("bad device ID !\n");
1379 return MMSYSERR_BADDEVICEID;
1382 if (lpTime == NULL) return MMSYSERR_INVALPARAM;
1384 wwo = &WOutDev[wDevID];
1385 ALSA_AddRingMessage(&wwo->msgRing, WINE_WM_UPDATE, 0, TRUE);
1386 val = wwo->dwPlayedTotal;
1388 TRACE("wType=%04X wBitsPerSample=%u nSamplesPerSec=%lu nChannels=%u nAvgBytesPerSec=%lu\n",
1389 lpTime->wType, wwo->format.wBitsPerSample,
1390 wwo->format.wf.nSamplesPerSec, wwo->format.wf.nChannels,
1391 wwo->format.wf.nAvgBytesPerSec);
1392 TRACE("dwPlayedTotal=%lu\n", val);
1394 switch (lpTime->wType) {
1397 TRACE("TIME_BYTES=%lu\n", lpTime->u.cb);
1400 lpTime->u.sample = val * 8 / wwo->format.wBitsPerSample /wwo->format.wf.nChannels;
1401 TRACE("TIME_SAMPLES=%lu\n", lpTime->u.sample);
1404 time = val / (wwo->format.wf.nAvgBytesPerSec / 1000);
1405 lpTime->u.smpte.hour = time / 108000;
1406 time -= lpTime->u.smpte.hour * 108000;
1407 lpTime->u.smpte.min = time / 1800;
1408 time -= lpTime->u.smpte.min * 1800;
1409 lpTime->u.smpte.sec = time / 30;
1410 time -= lpTime->u.smpte.sec * 30;
1411 lpTime->u.smpte.frame = time;
1412 lpTime->u.smpte.fps = 30;
1413 TRACE("TIME_SMPTE=%02u:%02u:%02u:%02u\n",
1414 lpTime->u.smpte.hour, lpTime->u.smpte.min,
1415 lpTime->u.smpte.sec, lpTime->u.smpte.frame);
1418 FIXME("Format %d not supported ! use TIME_MS !\n", lpTime->wType);
1419 lpTime->wType = TIME_MS;
1421 lpTime->u.ms = val / (wwo->format.wf.nAvgBytesPerSec / 1000);
1422 TRACE("TIME_MS=%lu\n", lpTime->u.ms);
1425 return MMSYSERR_NOERROR;
1428 /**************************************************************************
1429 * wodBreakLoop [internal]
1431 static DWORD wodBreakLoop(WORD wDevID)
1433 TRACE("(%u);\n", wDevID);
1435 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].p_handle == NULL) {
1436 WARN("bad device ID !\n");
1437 return MMSYSERR_BADDEVICEID;
1439 ALSA_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_BREAKLOOP, 0, TRUE);
1440 return MMSYSERR_NOERROR;
1443 /**************************************************************************
1444 * wodGetVolume [internal]
1446 static DWORD wodGetVolume(WORD wDevID, LPDWORD lpdwVol)
1453 TRACE("(%u, %p);\n", wDevID, lpdwVol);
1454 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].p_handle == NULL) {
1455 WARN("bad device ID !\n");
1456 return MMSYSERR_BADDEVICEID;
1458 wwo = &WOutDev[wDevID];
1459 count = snd_ctl_elem_info_get_count(wwo->playback_einfo);
1460 min = snd_ctl_elem_info_get_min(wwo->playback_einfo);
1461 max = snd_ctl_elem_info_get_max(wwo->playback_einfo);
1463 #define VOLUME_ALSA_TO_WIN(x) (((x)-min) * 65536 /(max-min))
1464 if (lpdwVol == NULL)
1465 return MMSYSERR_NOTENABLED;
1470 left = VOLUME_ALSA_TO_WIN(snd_ctl_elem_value_get_integer(wwo->playback_evalue, 0));
1471 right = VOLUME_ALSA_TO_WIN(snd_ctl_elem_value_get_integer(wwo->playback_evalue, 1));
1474 left = right = VOLUME_ALSA_TO_WIN(snd_ctl_elem_value_get_integer(wwo->playback_evalue, 0));
1477 WARN("%d channels mixer not supported\n", count);
1478 return MMSYSERR_NOERROR;
1480 #undef VOLUME_ALSA_TO_WIN
1482 TRACE("left=%d right=%d !\n", left, right);
1483 *lpdwVol = MAKELONG( left, right );
1484 return MMSYSERR_NOERROR;
1487 /**************************************************************************
1488 * wodSetVolume [internal]
1490 static DWORD wodSetVolume(WORD wDevID, DWORD dwParam)
1497 TRACE("(%u, %08lX);\n", wDevID, dwParam);
1498 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].p_handle == NULL) {
1499 WARN("bad device ID !\n");
1500 return MMSYSERR_BADDEVICEID;
1502 wwo = &WOutDev[wDevID];
1503 count=snd_ctl_elem_info_get_count(wwo->playback_einfo);
1504 min = snd_ctl_elem_info_get_min(wwo->playback_einfo);
1505 max = snd_ctl_elem_info_get_max(wwo->playback_einfo);
1507 left = LOWORD(dwParam);
1508 right = HIWORD(dwParam);
1510 #define VOLUME_WIN_TO_ALSA(x) ( (((x) * (max-min)) / 65536) + min )
1514 snd_ctl_elem_value_set_integer(wwo->playback_evalue, 0, VOLUME_WIN_TO_ALSA(left));
1515 snd_ctl_elem_value_set_integer(wwo->playback_evalue, 1, VOLUME_WIN_TO_ALSA(right));
1518 snd_ctl_elem_value_set_integer(wwo->playback_evalue, 0, VOLUME_WIN_TO_ALSA(left));
1521 WARN("%d channels mixer not supported\n", count);
1523 #undef VOLUME_WIN_TO_ALSA
1524 if ( (err = snd_ctl_elem_write(wwo->ctl, wwo->playback_evalue)) < 0)
1526 ERR("error writing snd_ctl_elem_value: %s\n", snd_strerror(err));
1528 return MMSYSERR_NOERROR;
1531 /**************************************************************************
1532 * wodGetNumDevs [internal]
1534 static DWORD wodGetNumDevs(void)
1536 return ALSA_WodNumDevs;
1540 /**************************************************************************
1541 * wodMessage (WINEALSA.@)
1543 DWORD WINAPI ALSA_wodMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
1544 DWORD dwParam1, DWORD dwParam2)
1546 TRACE("(%u, %04X, %08lX, %08lX, %08lX);\n",
1547 wDevID, wMsg, dwUser, dwParam1, dwParam2);
1554 /* FIXME: Pretend this is supported */
1556 case WODM_OPEN: return wodOpen (wDevID, (LPWAVEOPENDESC)dwParam1, dwParam2);
1557 case WODM_CLOSE: return wodClose (wDevID);
1558 case WODM_GETDEVCAPS: return wodGetDevCaps (wDevID, (LPWAVEOUTCAPSA)dwParam1, dwParam2);
1559 case WODM_GETNUMDEVS: return wodGetNumDevs ();
1560 case WODM_GETPITCH: return MMSYSERR_NOTSUPPORTED;
1561 case WODM_SETPITCH: return MMSYSERR_NOTSUPPORTED;
1562 case WODM_GETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
1563 case WODM_SETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
1564 case WODM_WRITE: return wodWrite (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
1565 case WODM_PAUSE: return wodPause (wDevID);
1566 case WODM_GETPOS: return wodGetPosition (wDevID, (LPMMTIME)dwParam1, dwParam2);
1567 case WODM_BREAKLOOP: return wodBreakLoop (wDevID);
1568 case WODM_PREPARE: return wodPrepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
1569 case WODM_UNPREPARE: return wodUnprepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
1570 case WODM_GETVOLUME: return wodGetVolume (wDevID, (LPDWORD)dwParam1);
1571 case WODM_SETVOLUME: return wodSetVolume (wDevID, dwParam1);
1572 case WODM_RESTART: return wodRestart (wDevID);
1573 case WODM_RESET: return wodReset (wDevID);
1574 case DRV_QUERYDSOUNDIFACE: return wodDsCreate(wDevID, (PIDSDRIVER*)dwParam1);
1576 FIXME("unknown message %d!\n", wMsg);
1578 return MMSYSERR_NOTSUPPORTED;
1581 /*======================================================================*
1582 * Low level DSOUND implementation *
1583 *======================================================================*/
1585 typedef struct IDsDriverImpl IDsDriverImpl;
1586 typedef struct IDsDriverBufferImpl IDsDriverBufferImpl;
1588 struct IDsDriverImpl
1590 /* IUnknown fields */
1591 ICOM_VFIELD(IDsDriver);
1593 /* IDsDriverImpl fields */
1595 IDsDriverBufferImpl*primary;
1598 struct IDsDriverBufferImpl
1600 /* IUnknown fields */
1601 ICOM_VFIELD(IDsDriverBuffer);
1603 /* IDsDriverBufferImpl fields */
1606 CRITICAL_SECTION mmap_crst;
1608 DWORD mmap_buflen_bytes;
1609 snd_pcm_uframes_t mmap_buflen_frames;
1610 snd_pcm_channel_area_t * mmap_areas;
1611 snd_async_handler_t * mmap_async_handler;
1614 static void DSDB_CheckXRUN(IDsDriverBufferImpl* pdbi)
1616 WINE_WAVEOUT * wwo = &(WOutDev[pdbi->drv->wDevID]);
1617 snd_pcm_state_t state = snd_pcm_state(wwo->p_handle);
1619 if ( state == SND_PCM_STATE_XRUN )
1621 int err = snd_pcm_prepare(wwo->p_handle);
1622 TRACE("xrun occurred\n");
1624 ERR("recovery from xrun failed, prepare failed: %s\n", snd_strerror(err));
1626 else if ( state == SND_PCM_STATE_SUSPENDED )
1628 int err = snd_pcm_resume(wwo->p_handle);
1629 TRACE("recovery from suspension occurred\n");
1630 if (err < 0 && err != -EAGAIN){
1631 err = snd_pcm_prepare(wwo->p_handle);
1633 ERR("recovery from suspend failed, prepare failed: %s\n", snd_strerror(err));
1638 static void DSDB_MMAPCopy(IDsDriverBufferImpl* pdbi)
1640 WINE_WAVEOUT * wwo = &(WOutDev[pdbi->drv->wDevID]);
1642 snd_pcm_format_t format;
1643 snd_pcm_uframes_t period_size;
1644 snd_pcm_sframes_t avail;
1646 if ( !pdbi->mmap_buffer || !wwo->hw_params || !wwo->p_handle)
1649 channels = snd_pcm_hw_params_get_channels(wwo->hw_params);
1650 format = snd_pcm_hw_params_get_format(wwo->hw_params);
1651 period_size = snd_pcm_hw_params_get_period_size(wwo->hw_params, 0);
1652 avail = snd_pcm_avail_update(wwo->p_handle);
1654 DSDB_CheckXRUN(pdbi);
1656 TRACE("avail=%d format=%s channels=%d\n", (int)avail, snd_pcm_format_name(format), channels );
1658 while (avail >= period_size)
1660 const snd_pcm_channel_area_t *areas;
1661 snd_pcm_uframes_t ofs;
1662 snd_pcm_uframes_t frames;
1665 frames = avail / period_size * period_size; /* round down to a multiple of period_size */
1667 EnterCriticalSection(&pdbi->mmap_crst);
1669 snd_pcm_mmap_begin(wwo->p_handle, &areas, &ofs, &frames);
1670 snd_pcm_areas_copy(areas, ofs, pdbi->mmap_areas, ofs, channels, frames, format);
1671 err = snd_pcm_mmap_commit(wwo->p_handle, ofs, frames);
1673 LeaveCriticalSection(&pdbi->mmap_crst);
1675 if ( err != (snd_pcm_sframes_t) frames)
1676 ERR("mmap partially failed.\n");
1678 avail = snd_pcm_avail_update(wwo->p_handle);
1682 static void DSDB_PCMCallback(snd_async_handler_t *ahandler)
1684 /* snd_pcm_t * handle = snd_async_handler_get_pcm(ahandler); */
1685 IDsDriverBufferImpl* pdbi = snd_async_handler_get_callback_private(ahandler);
1686 TRACE("callback called\n");
1687 DSDB_MMAPCopy(pdbi);
1690 static int DSDB_CreateMMAP(IDsDriverBufferImpl* pdbi)
1692 WINE_WAVEOUT * wwo = &(WOutDev[pdbi->drv->wDevID]);
1693 snd_pcm_format_t format = snd_pcm_hw_params_get_format(wwo->hw_params);
1694 snd_pcm_uframes_t frames = snd_pcm_hw_params_get_buffer_size(wwo->hw_params);
1695 int channels = snd_pcm_hw_params_get_channels(wwo->hw_params);
1696 unsigned int bits_per_sample = snd_pcm_format_physical_width(format);
1697 unsigned int bits_per_frame = bits_per_sample * channels;
1698 snd_pcm_channel_area_t * a;
1703 ALSA_TraceParameters(wwo->hw_params, NULL, FALSE);
1705 TRACE("format=%s frames=%ld channels=%d bits_per_sample=%d bits_per_frame=%d\n",
1706 snd_pcm_format_name(format), frames, channels, bits_per_sample, bits_per_frame);
1708 pdbi->mmap_buflen_frames = frames;
1709 pdbi->mmap_buflen_bytes = snd_pcm_frames_to_bytes( wwo->p_handle, frames );
1710 pdbi->mmap_buffer = HeapAlloc(GetProcessHeap(),0,pdbi->mmap_buflen_bytes);
1711 if (!pdbi->mmap_buffer)
1712 return DSERR_OUTOFMEMORY;
1714 snd_pcm_format_set_silence(format, pdbi->mmap_buffer, frames );
1716 TRACE("created mmap buffer of %ld frames (%ld bytes) at %p\n",
1717 frames, pdbi->mmap_buflen_bytes, pdbi->mmap_buffer);
1719 pdbi->mmap_areas = HeapAlloc(GetProcessHeap(),0,channels*sizeof(snd_pcm_channel_area_t));
1720 if (!pdbi->mmap_areas)
1721 return DSERR_OUTOFMEMORY;
1723 a = pdbi->mmap_areas;
1724 for (c = 0; c < channels; c++, a++)
1726 a->addr = pdbi->mmap_buffer;
1727 a->first = bits_per_sample * c;
1728 a->step = bits_per_frame;
1729 TRACE("Area %d: addr=%p first=%d step=%d\n", c, a->addr, a->first, a->step);
1732 InitializeCriticalSection(&pdbi->mmap_crst);
1734 err = snd_async_add_pcm_handler(&pdbi->mmap_async_handler, wwo->p_handle, DSDB_PCMCallback, pdbi);
1737 ERR("add_pcm_handler failed. reason: %s\n", snd_strerror(err));
1738 return DSERR_GENERIC;
1744 static void DSDB_DestroyMMAP(IDsDriverBufferImpl* pdbi)
1746 TRACE("mmap buffer %p destroyed\n", pdbi->mmap_buffer);
1747 HeapFree(GetProcessHeap(), 0, pdbi->mmap_areas);
1748 HeapFree(GetProcessHeap(), 0, pdbi->mmap_buffer);
1749 pdbi->mmap_areas = NULL;
1750 pdbi->mmap_buffer = NULL;
1751 DeleteCriticalSection(&pdbi->mmap_crst);
1755 static HRESULT WINAPI IDsDriverBufferImpl_QueryInterface(PIDSDRIVERBUFFER iface, REFIID riid, LPVOID *ppobj)
1757 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
1758 FIXME("(): stub!\n");
1759 return DSERR_UNSUPPORTED;
1762 static ULONG WINAPI IDsDriverBufferImpl_AddRef(PIDSDRIVERBUFFER iface)
1764 ICOM_THIS(IDsDriverBufferImpl,iface);
1765 TRACE("(%p)\n",iface);
1769 static ULONG WINAPI IDsDriverBufferImpl_Release(PIDSDRIVERBUFFER iface)
1771 ICOM_THIS(IDsDriverBufferImpl,iface);
1772 TRACE("(%p)\n",iface);
1775 if (This == This->drv->primary)
1776 This->drv->primary = NULL;
1777 DSDB_DestroyMMAP(This);
1778 HeapFree(GetProcessHeap(), 0, This);
1782 static HRESULT WINAPI IDsDriverBufferImpl_Lock(PIDSDRIVERBUFFER iface,
1783 LPVOID*ppvAudio1,LPDWORD pdwLen1,
1784 LPVOID*ppvAudio2,LPDWORD pdwLen2,
1785 DWORD dwWritePosition,DWORD dwWriteLen,
1788 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
1789 TRACE("(%p)\n",iface);
1790 return DSERR_UNSUPPORTED;
1793 static HRESULT WINAPI IDsDriverBufferImpl_Unlock(PIDSDRIVERBUFFER iface,
1794 LPVOID pvAudio1,DWORD dwLen1,
1795 LPVOID pvAudio2,DWORD dwLen2)
1797 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
1798 TRACE("(%p)\n",iface);
1799 return DSERR_UNSUPPORTED;
1802 static HRESULT WINAPI IDsDriverBufferImpl_SetFormat(PIDSDRIVERBUFFER iface,
1803 LPWAVEFORMATEX pwfx)
1805 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
1806 TRACE("(%p,%p)\n",iface,pwfx);
1807 return DSERR_BUFFERLOST;
1810 static HRESULT WINAPI IDsDriverBufferImpl_SetFrequency(PIDSDRIVERBUFFER iface, DWORD dwFreq)
1812 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
1813 TRACE("(%p,%ld): stub\n",iface,dwFreq);
1814 return DSERR_UNSUPPORTED;
1817 static HRESULT WINAPI IDsDriverBufferImpl_SetVolumePan(PIDSDRIVERBUFFER iface, PDSVOLUMEPAN pVolPan)
1819 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
1820 FIXME("(%p,%p): stub!\n",iface,pVolPan);
1821 return DSERR_UNSUPPORTED;
1824 static HRESULT WINAPI IDsDriverBufferImpl_SetPosition(PIDSDRIVERBUFFER iface, DWORD dwNewPos)
1826 /* ICOM_THIS(IDsDriverImpl,iface); */
1827 TRACE("(%p,%ld): stub\n",iface,dwNewPos);
1828 return DSERR_UNSUPPORTED;
1831 static HRESULT WINAPI IDsDriverBufferImpl_GetPosition(PIDSDRIVERBUFFER iface,
1832 LPDWORD lpdwPlay, LPDWORD lpdwWrite)
1834 ICOM_THIS(IDsDriverBufferImpl,iface);
1835 WINE_WAVEOUT * wwo = &(WOutDev[This->drv->wDevID]);
1836 snd_pcm_uframes_t hw_ptr;
1837 snd_pcm_uframes_t period_size = snd_pcm_hw_params_get_period_size(wwo->hw_params, 0);
1839 /** we need to track down buffer underruns */
1840 DSDB_CheckXRUN(This);
1842 EnterCriticalSection(&This->mmap_crst);
1843 hw_ptr = _snd_pcm_mmap_hw_ptr(wwo->p_handle);
1845 *lpdwPlay = snd_pcm_frames_to_bytes(wwo->p_handle, hw_ptr/ period_size * period_size) % This->mmap_buflen_bytes;
1847 *lpdwWrite = snd_pcm_frames_to_bytes(wwo->p_handle, (hw_ptr / period_size + 1) * period_size ) % This->mmap_buflen_bytes;
1848 LeaveCriticalSection(&This->mmap_crst);
1850 TRACE("hw_ptr=0x%08x, playpos=%ld, writepos=%ld\n", (unsigned int)hw_ptr, lpdwPlay?*lpdwPlay:-1, lpdwWrite?*lpdwWrite:-1);
1854 static HRESULT WINAPI IDsDriverBufferImpl_Play(PIDSDRIVERBUFFER iface, DWORD dwRes1, DWORD dwRes2, DWORD dwFlags)
1856 ICOM_THIS(IDsDriverBufferImpl,iface);
1857 WINE_WAVEOUT * wwo = &(WOutDev[This->drv->wDevID]);
1858 snd_pcm_state_t state;
1861 TRACE("(%p,%lx,%lx,%lx)\n",iface,dwRes1,dwRes2,dwFlags);
1863 state = snd_pcm_state(wwo->p_handle);
1864 if ( state == SND_PCM_STATE_SETUP )
1866 err = snd_pcm_prepare(wwo->p_handle);
1867 state = snd_pcm_state(wwo->p_handle);
1869 if ( state == SND_PCM_STATE_PREPARED )
1871 DSDB_MMAPCopy(This);
1872 err = snd_pcm_start(wwo->p_handle);
1877 static HRESULT WINAPI IDsDriverBufferImpl_Stop(PIDSDRIVERBUFFER iface)
1879 ICOM_THIS(IDsDriverBufferImpl,iface);
1880 WINE_WAVEOUT * wwo = &(WOutDev[This->drv->wDevID]);
1885 TRACE("(%p)\n",iface);
1887 /* ring buffer wrap up detection */
1888 IDsDriverBufferImpl_GetPosition(iface, &play, &write);
1891 TRACE("writepos wrapper up\n");
1895 if ( ( err = snd_pcm_drop(wwo->p_handle)) < 0 )
1897 ERR("error while stopping pcm: %s\n", snd_strerror(err));
1898 return DSERR_GENERIC;
1903 static ICOM_VTABLE(IDsDriverBuffer) dsdbvt =
1905 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1906 IDsDriverBufferImpl_QueryInterface,
1907 IDsDriverBufferImpl_AddRef,
1908 IDsDriverBufferImpl_Release,
1909 IDsDriverBufferImpl_Lock,
1910 IDsDriverBufferImpl_Unlock,
1911 IDsDriverBufferImpl_SetFormat,
1912 IDsDriverBufferImpl_SetFrequency,
1913 IDsDriverBufferImpl_SetVolumePan,
1914 IDsDriverBufferImpl_SetPosition,
1915 IDsDriverBufferImpl_GetPosition,
1916 IDsDriverBufferImpl_Play,
1917 IDsDriverBufferImpl_Stop
1920 static HRESULT WINAPI IDsDriverImpl_QueryInterface(PIDSDRIVER iface, REFIID riid, LPVOID *ppobj)
1922 /* ICOM_THIS(IDsDriverImpl,iface); */
1923 FIXME("(%p): stub!\n",iface);
1924 return DSERR_UNSUPPORTED;
1927 static ULONG WINAPI IDsDriverImpl_AddRef(PIDSDRIVER iface)
1929 ICOM_THIS(IDsDriverImpl,iface);
1930 TRACE("(%p)\n",iface);
1935 static ULONG WINAPI IDsDriverImpl_Release(PIDSDRIVER iface)
1937 ICOM_THIS(IDsDriverImpl,iface);
1938 TRACE("(%p)\n",iface);
1941 HeapFree(GetProcessHeap(),0,This);
1945 static HRESULT WINAPI IDsDriverImpl_GetDriverDesc(PIDSDRIVER iface, PDSDRIVERDESC pDesc)
1947 ICOM_THIS(IDsDriverImpl,iface);
1948 TRACE("(%p,%p)\n",iface,pDesc);
1949 pDesc->dwFlags = DSDDESC_DOMMSYSTEMOPEN | DSDDESC_DOMMSYSTEMSETFORMAT |
1950 DSDDESC_USESYSTEMMEMORY;
1951 strcpy(pDesc->szDesc, "WineALSA DirectSound Driver");
1952 strcpy(pDesc->szDrvName, "winealsa.drv");
1953 pDesc->dnDevNode = WOutDev[This->wDevID].waveDesc.dnDevNode;
1955 pDesc->wReserved = 0;
1956 pDesc->ulDeviceNum = This->wDevID;
1957 pDesc->dwHeapType = DSDHEAP_NOHEAP;
1958 pDesc->pvDirectDrawHeap = NULL;
1959 pDesc->dwMemStartAddress = 0;
1960 pDesc->dwMemEndAddress = 0;
1961 pDesc->dwMemAllocExtra = 0;
1962 pDesc->pvReserved1 = NULL;
1963 pDesc->pvReserved2 = NULL;
1967 static HRESULT WINAPI IDsDriverImpl_Open(PIDSDRIVER iface)
1969 /* ICOM_THIS(IDsDriverImpl,iface); */
1970 TRACE("(%p)\n",iface);
1974 static HRESULT WINAPI IDsDriverImpl_Close(PIDSDRIVER iface)
1976 /* ICOM_THIS(IDsDriverImpl,iface); */
1977 TRACE("(%p)\n",iface);
1981 static HRESULT WINAPI IDsDriverImpl_GetCaps(PIDSDRIVER iface, PDSDRIVERCAPS pCaps)
1983 ICOM_THIS(IDsDriverImpl,iface);
1984 TRACE("(%p,%p)\n",iface,pCaps);
1985 memset(pCaps, 0, sizeof(*pCaps));
1987 pCaps->dwFlags = DSCAPS_PRIMARYMONO;
1988 if ( WOutDev[This->wDevID].caps.wChannels == 2 )
1989 pCaps->dwFlags |= DSCAPS_PRIMARYSTEREO;
1991 if ( WOutDev[This->wDevID].caps.dwFormats & (WAVE_FORMAT_1S08 | WAVE_FORMAT_2S08 | WAVE_FORMAT_4S08 ) )
1992 pCaps->dwFlags |= DSCAPS_PRIMARY8BIT;
1994 if ( WOutDev[This->wDevID].caps.dwFormats & (WAVE_FORMAT_1S16 | WAVE_FORMAT_2S16 | WAVE_FORMAT_4S16))
1995 pCaps->dwFlags |= DSCAPS_PRIMARY16BIT;
1997 pCaps->dwPrimaryBuffers = 1;
1998 TRACE("caps=0x%X\n",(unsigned int)pCaps->dwFlags);
2000 /* the other fields only apply to secondary buffers, which we don't support
2001 * (unless we want to mess with wavetable synthesizers and MIDI) */
2005 static HRESULT WINAPI IDsDriverImpl_CreateSoundBuffer(PIDSDRIVER iface,
2006 LPWAVEFORMATEX pwfx,
2007 DWORD dwFlags, DWORD dwCardAddress,
2008 LPDWORD pdwcbBufferSize,
2012 ICOM_THIS(IDsDriverImpl,iface);
2013 IDsDriverBufferImpl** ippdsdb = (IDsDriverBufferImpl**)ppvObj;
2016 TRACE("(%p,%p,%lx,%lx)\n",iface,pwfx,dwFlags,dwCardAddress);
2017 /* we only support primary buffers */
2018 if (!(dwFlags & DSBCAPS_PRIMARYBUFFER))
2019 return DSERR_UNSUPPORTED;
2021 return DSERR_ALLOCATED;
2022 if (dwFlags & (DSBCAPS_CTRLFREQUENCY | DSBCAPS_CTRLPAN))
2023 return DSERR_CONTROLUNAVAIL;
2025 *ippdsdb = (IDsDriverBufferImpl*)HeapAlloc(GetProcessHeap(),0,sizeof(IDsDriverBufferImpl));
2026 if (*ippdsdb == NULL)
2027 return DSERR_OUTOFMEMORY;
2028 ICOM_VTBL(*ippdsdb) = &dsdbvt;
2029 (*ippdsdb)->ref = 1;
2030 (*ippdsdb)->drv = This;
2032 err = DSDB_CreateMMAP((*ippdsdb));
2035 HeapFree(GetProcessHeap(), 0, *ippdsdb);
2039 *ppbBuffer = (*ippdsdb)->mmap_buffer;
2040 *pdwcbBufferSize = (*ippdsdb)->mmap_buflen_bytes;
2042 This->primary = *ippdsdb;
2044 /* buffer is ready to go */
2045 TRACE("buffer created at %p\n", *ippdsdb);
2049 static HRESULT WINAPI IDsDriverImpl_DuplicateSoundBuffer(PIDSDRIVER iface,
2050 PIDSDRIVERBUFFER pBuffer,
2053 /* ICOM_THIS(IDsDriverImpl,iface); */
2054 TRACE("(%p,%p): stub\n",iface,pBuffer);
2055 return DSERR_INVALIDCALL;
2058 static ICOM_VTABLE(IDsDriver) dsdvt =
2060 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
2061 IDsDriverImpl_QueryInterface,
2062 IDsDriverImpl_AddRef,
2063 IDsDriverImpl_Release,
2064 IDsDriverImpl_GetDriverDesc,
2066 IDsDriverImpl_Close,
2067 IDsDriverImpl_GetCaps,
2068 IDsDriverImpl_CreateSoundBuffer,
2069 IDsDriverImpl_DuplicateSoundBuffer
2072 static DWORD wodDsCreate(UINT wDevID, PIDSDRIVER* drv)
2074 IDsDriverImpl** idrv = (IDsDriverImpl**)drv;
2076 TRACE("driver created\n");
2078 /* the HAL isn't much better than the HEL if we can't do mmap() */
2079 if (!(WOutDev[wDevID].caps.dwSupport & WAVECAPS_DIRECTSOUND)) {
2080 ERR("DirectSound flag not set\n");
2081 MESSAGE("This sound card's driver does not support direct access\n");
2082 MESSAGE("The (slower) DirectSound HEL mode will be used instead.\n");
2083 return MMSYSERR_NOTSUPPORTED;
2086 *idrv = (IDsDriverImpl*)HeapAlloc(GetProcessHeap(),0,sizeof(IDsDriverImpl));
2088 return MMSYSERR_NOMEM;
2089 ICOM_VTBL(*idrv) = &dsdvt;
2092 (*idrv)->wDevID = wDevID;
2093 (*idrv)->primary = NULL;
2094 return MMSYSERR_NOERROR;
2100 /**************************************************************************
2101 * wodMessage (WINEALSA.@)
2103 DWORD WINAPI ALSA_wodMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
2104 DWORD dwParam1, DWORD dwParam2)
2106 FIXME("(%u, %04X, %08lX, %08lX, %08lX):stub\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
2107 return MMSYSERR_NOTENABLED;
2110 #endif /* HAVE_ALSA */