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 */
152 DSDRIVERDESC ds_desc
;
156 static WINE_WAVEOUT WOutDev
[MAX_WAVEOUTDRV
];
157 static DWORD ALSA_WodNumDevs
;
159 static DWORD
wodDsCreate(UINT wDevID
, PIDSDRIVER
* drv
);
160 static DWORD
wodDsDesc(UINT wDevID
, PDSDRIVERDESC desc
);
161 static DWORD
wodDsGuid(UINT wDevID
, LPGUID pGuid
);
163 /* These strings used only for tracing */
165 static const char *wodPlayerCmdString
[] = {
167 "WINE_WM_RESTARTING",
176 /*======================================================================*
177 * Low level WAVE implementation *
178 *======================================================================*/
180 /**************************************************************************
181 * ALSA_InitializeVolumeCtl [internal]
183 * used to initialize the PCM Volume Control
185 static int ALSA_InitializeVolumeCtl(WINE_WAVEOUT
* wwo
)
187 snd_ctl_t
* ctl
= NULL
;
188 snd_ctl_card_info_t
* cardinfo
;
189 snd_ctl_elem_list_t
* elemlist
;
190 snd_ctl_elem_id_t
* e_id
;
191 snd_ctl_elem_info_t
* einfo
;
192 snd_hctl_t
* hctl
= NULL
;
193 snd_hctl_elem_t
* elem
;
197 snd_ctl_card_info_alloca(&cardinfo
);
198 memset(cardinfo
,0,snd_ctl_card_info_sizeof());
200 snd_ctl_elem_list_alloca(&elemlist
);
201 memset(elemlist
,0,snd_ctl_elem_list_sizeof());
203 snd_ctl_elem_id_alloca(&e_id
);
204 memset(e_id
,0,snd_ctl_elem_id_sizeof());
206 snd_ctl_elem_info_alloca(&einfo
);
207 memset(einfo
,0,snd_ctl_elem_info_sizeof());
209 #define EXIT_ON_ERROR(f,txt) do \
212 if ( (err = (f) ) < 0) \
214 ERR(txt ": %s\n", snd_strerror(err)); \
216 snd_hctl_close(hctl); \
218 snd_ctl_close(ctl); \
223 EXIT_ON_ERROR( snd_ctl_open(&ctl
,"hw",0) , "ctl open failed" );
224 EXIT_ON_ERROR( snd_ctl_card_info(ctl
, cardinfo
), "card info failed");
225 EXIT_ON_ERROR( snd_ctl_elem_list(ctl
, elemlist
), "elem list failed");
227 nCtrls
= snd_ctl_elem_list_get_count(elemlist
);
229 EXIT_ON_ERROR( snd_hctl_open(&hctl
,"hw",0), "hctl open failed");
230 EXIT_ON_ERROR( snd_hctl_load(hctl
), "hctl load failed" );
232 elem
=snd_hctl_first_elem(hctl
);
233 for ( i
= 0; i
<nCtrls
; i
++) {
235 memset(e_id
,0,snd_ctl_elem_id_sizeof());
237 snd_hctl_elem_get_id(elem
,e_id
);
239 TRACE("ctl: #%d '%s'%d\n",
240 snd_ctl_elem_id_get_numid(e_id),
241 snd_ctl_elem_id_get_name(e_id),
242 snd_ctl_elem_id_get_index(e_id));
244 if ( !strcmp("PCM Playback Volume", snd_ctl_elem_id_get_name(e_id
)) )
246 EXIT_ON_ERROR( snd_hctl_elem_info(elem
,einfo
), "hctl elem info failed" );
248 /* few sanity checks... you'll never know... */
249 if ( snd_ctl_elem_info_get_type(einfo
) != SND_CTL_ELEM_TYPE_INTEGER
)
250 WARN("playback volume control is not an integer\n");
251 if ( !snd_ctl_elem_info_is_readable(einfo
) )
252 WARN("playback volume control is readable\n");
253 if ( !snd_ctl_elem_info_is_writable(einfo
) )
254 WARN("playback volume control is readable\n");
256 TRACE(" ctrl range: min=%ld max=%ld step=%ld\n",
257 snd_ctl_elem_info_get_min(einfo
),
258 snd_ctl_elem_info_get_max(einfo
),
259 snd_ctl_elem_info_get_step(einfo
));
261 EXIT_ON_ERROR( snd_ctl_elem_id_malloc(&wwo
->playback_eid
), "elem id malloc failed" );
262 EXIT_ON_ERROR( snd_ctl_elem_info_malloc(&wwo
->playback_einfo
), "elem info malloc failed" );
263 EXIT_ON_ERROR( snd_ctl_elem_value_malloc(&wwo
->playback_evalue
), "elem value malloc failed" );
265 /* ok, now we can safely save these objects for later */
266 snd_ctl_elem_id_copy(wwo
->playback_eid
, e_id
);
267 snd_ctl_elem_info_copy(wwo
->playback_einfo
, einfo
);
268 snd_ctl_elem_value_set_id(wwo
->playback_evalue
, wwo
->playback_eid
);
272 elem
=snd_hctl_elem_next(elem
);
274 snd_hctl_close(hctl
);
279 /**************************************************************************
280 * ALSA_XRUNRecovery [internal]
282 * used to recovery from XRUN errors (buffer underflow/overflow)
284 static int ALSA_XRUNRecovery(WINE_WAVEOUT
* wwo
, int err
)
286 if (err
== -EPIPE
) { /* under-run */
287 err
= snd_pcm_prepare(wwo
->p_handle
);
289 ERR( "underrun recovery failed. prepare failed: %s\n", snd_strerror(err
));
291 } else if (err
== -ESTRPIPE
) {
292 while ((err
= snd_pcm_resume(wwo
->p_handle
)) == -EAGAIN
)
293 sleep(1); /* wait until the suspend flag is released */
295 err
= snd_pcm_prepare(wwo
->p_handle
);
297 ERR("recovery from suspend failed, prepare failed: %s\n", snd_strerror(err
));
304 /**************************************************************************
305 * ALSA_TraceParameters [internal]
307 * used to trace format changes, hw and sw parameters
309 static void ALSA_TraceParameters(snd_pcm_hw_params_t
* hw_params
, snd_pcm_sw_params_t
* sw
, int full
)
311 snd_pcm_format_t format
= snd_pcm_hw_params_get_format(hw_params
);
312 snd_pcm_access_t access
= snd_pcm_hw_params_get_access(hw_params
);
314 #define X(x) ((x)? "true" : "false")
316 TRACE("FLAGS: sampleres=%s overrng=%s pause=%s resume=%s syncstart=%s batch=%s block=%s double=%s "
317 "halfd=%s joint=%s \n",
318 X(snd_pcm_hw_params_can_mmap_sample_resolution(hw_params
)),
319 X(snd_pcm_hw_params_can_overrange(hw_params
)),
320 X(snd_pcm_hw_params_can_pause(hw_params
)),
321 X(snd_pcm_hw_params_can_resume(hw_params
)),
322 X(snd_pcm_hw_params_can_sync_start(hw_params
)),
323 X(snd_pcm_hw_params_is_batch(hw_params
)),
324 X(snd_pcm_hw_params_is_block_transfer(hw_params
)),
325 X(snd_pcm_hw_params_is_double(hw_params
)),
326 X(snd_pcm_hw_params_is_half_duplex(hw_params
)),
327 X(snd_pcm_hw_params_is_joint_duplex(hw_params
)));
331 TRACE("access=%s\n", snd_pcm_access_name(access
));
334 snd_pcm_access_mask_t
* acmask
;
335 snd_pcm_access_mask_alloca(&acmask
);
336 snd_pcm_hw_params_get_access_mask(hw_params
, acmask
);
337 for ( access
= SND_PCM_ACCESS_MMAP_INTERLEAVED
; access
<= SND_PCM_ACCESS_LAST
; access
++)
338 if (snd_pcm_access_mask_test(acmask
, access
))
339 TRACE("access=%s\n", snd_pcm_access_name(access
));
344 TRACE("format=%s\n", snd_pcm_format_name(format
));
349 snd_pcm_format_mask_t
* fmask
;
351 snd_pcm_format_mask_alloca(&fmask
);
352 snd_pcm_hw_params_get_format_mask(hw_params
, fmask
);
353 for ( format
= SND_PCM_FORMAT_S8
; format
<= SND_PCM_FORMAT_LAST
; format
++)
354 if ( snd_pcm_format_mask_test(fmask
, format
) )
355 TRACE("format=%s\n", snd_pcm_format_name(format
));
359 int n = snd_pcm_hw_params_get_##x(hw_params); \
361 TRACE(#x "_min=%ld " #x "_max=%ld\n", \
362 (long int)snd_pcm_hw_params_get_##x##_min(hw_params), \
363 (long int)snd_pcm_hw_params_get_##x##_max(hw_params)); \
365 TRACE(#x "=%d\n", n); \
372 int n = snd_pcm_hw_params_get_##x(hw_params,0); \
374 TRACE(#x "_min=%ld " #x "_max=%ld\n", \
375 (long int)snd_pcm_hw_params_get_##x##_min(hw_params,0), \
376 (long int)snd_pcm_hw_params_get_##x##_max(hw_params,0)); \
378 TRACE(#x "=%d\n", n); \
396 /******************************************************************
399 * Initialize internal structures from ALSA information
401 LONG
ALSA_WaveInit(void)
404 snd_pcm_info_t
* info
;
405 snd_pcm_hw_params_t
* hw_params
;
410 /* FIXME: use better values */
412 wwo
->caps
.wMid
= 0x0002;
413 wwo
->caps
.wPid
= 0x0104;
414 strcpy(wwo
->caps
.szPname
, "SB16 Wave Out");
415 wwo
->caps
.vDriverVersion
= 0x0100;
416 wwo
->caps
.dwFormats
= 0x00000000;
417 wwo
->caps
.dwSupport
= WAVECAPS_VOLUME
;
418 strcpy(wwo
->ds_desc
.szDesc
, "WineALSA DirectSound Driver");
419 strcpy(wwo
->ds_desc
.szDrvName
, "winealsa.drv");
420 wwo
->ds_guid
= DSDEVID_DefaultPlayback
;
422 if (!wine_dlopen("libasound.so.2", RTLD_LAZY
|RTLD_GLOBAL
, NULL
, 0))
424 ERR("Error: ALSA lib needs to be loaded with flags RTLD_LAZY and RTLD_GLOBAL.\n");
428 snd_pcm_info_alloca(&info
);
429 snd_pcm_hw_params_alloca(&hw_params
);
431 #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)
434 EXIT_ON_ERROR( snd_pcm_open(&h
, wwo
->device
, SND_PCM_STREAM_PLAYBACK
, SND_PCM_NONBLOCK
) , "open pcm" );
437 EXIT_ON_ERROR( snd_pcm_info(h
, info
) , "pcm info" );
439 TRACE("dev=%d id=%s name=%s subdev=%d subdev_name=%s subdev_avail=%d subdev_num=%d stream=%s subclass=%s \n",
440 snd_pcm_info_get_device(info
),
441 snd_pcm_info_get_id(info
),
442 snd_pcm_info_get_name(info
),
443 snd_pcm_info_get_subdevice(info
),
444 snd_pcm_info_get_subdevice_name(info
),
445 snd_pcm_info_get_subdevices_avail(info
),
446 snd_pcm_info_get_subdevices_count(info
),
447 snd_pcm_stream_name(snd_pcm_info_get_stream(info
)),
448 (snd_pcm_info_get_subclass(info
) == SND_PCM_SUBCLASS_GENERIC_MIX
? "GENERIC MIX": "MULTI MIX"));
450 EXIT_ON_ERROR( snd_pcm_hw_params_any(h
, hw_params
) , "pcm hw params" );
454 ALSA_TraceParameters(hw_params
, NULL
, TRUE
);
457 snd_pcm_format_mask_t
* fmask
;
458 int ratemin
= snd_pcm_hw_params_get_rate_min(hw_params
, 0);
459 int ratemax
= snd_pcm_hw_params_get_rate_max(hw_params
, 0);
460 int chmin
= snd_pcm_hw_params_get_channels_min(hw_params
); \
461 int chmax
= snd_pcm_hw_params_get_channels_max(hw_params
); \
463 snd_pcm_format_mask_alloca(&fmask
);
464 snd_pcm_hw_params_get_format_mask(hw_params
, fmask
);
467 if ( (r) >= ratemin && ( (r) <= ratemax || ratemax == -1) ) \
469 if (snd_pcm_format_mask_test( fmask, SND_PCM_FORMAT_U8)) \
471 if (chmin <= 1 && 1 <= chmax) \
472 wwo->caps.dwFormats |= WAVE_FORMAT_##v##S08; \
473 if (chmin <= 2 && 2 <= chmax) \
474 wwo->caps.dwFormats |= WAVE_FORMAT_##v##S08; \
476 if (snd_pcm_format_mask_test( fmask, SND_PCM_FORMAT_S16_LE)) \
478 if (chmin <= 1 && 1 <= chmax) \
479 wwo->caps.dwFormats |= WAVE_FORMAT_##v##S16; \
480 if (chmin <= 2 && 2 <= chmax) \
481 wwo->caps.dwFormats |= WAVE_FORMAT_##v##S16; \
490 if ( snd_pcm_hw_params_get_channels_min(hw_params
) > 1) FIXME("-\n");
491 wwo
->caps
.wChannels
= (snd_pcm_hw_params_get_channels_max(hw_params
) >= 2) ? 2 : 1;
492 if (snd_pcm_hw_params_get_channels_min(hw_params
) <= 2 && 2 <= snd_pcm_hw_params_get_channels_max(hw_params
))
493 wwo
->caps
.dwSupport
|= WAVECAPS_LRVOLUME
;
495 /* FIXME: always true ? */
496 wwo
->caps
.dwSupport
|= WAVECAPS_SAMPLEACCURATE
;
499 snd_pcm_access_mask_t
* acmask
;
500 snd_pcm_access_mask_alloca(&acmask
);
501 snd_pcm_hw_params_get_access_mask(hw_params
, acmask
);
503 /* FIXME: NONITERLEAVED and COMPLEX are not supported right now */
504 if ( snd_pcm_access_mask_test( acmask
, SND_PCM_ACCESS_MMAP_INTERLEAVED
) )
505 wwo
->caps
.dwSupport
|= WAVECAPS_DIRECTSOUND
;
508 TRACE("Configured with dwFmts=%08lx dwSupport=%08lx\n",
509 wwo
->caps
.dwFormats
, wwo
->caps
.dwSupport
);
513 ALSA_InitializeVolumeCtl(wwo
);
518 /******************************************************************
519 * ALSA_InitRingMessage
521 * Initialize the ring of messages for passing between driver's caller and playback/record
524 static int ALSA_InitRingMessage(ALSA_MSG_RING
* omr
)
528 omr
->msg_event
= CreateEventA(NULL
, FALSE
, FALSE
, NULL
);
529 memset(omr
->messages
, 0, sizeof(ALSA_MSG
) * ALSA_RING_BUFFER_SIZE
);
530 InitializeCriticalSection(&omr
->msg_crst
);
534 /******************************************************************
535 * ALSA_DestroyRingMessage
538 static int ALSA_DestroyRingMessage(ALSA_MSG_RING
* omr
)
540 CloseHandle(omr
->msg_event
);
541 DeleteCriticalSection(&omr
->msg_crst
);
545 /******************************************************************
546 * ALSA_AddRingMessage
548 * Inserts a new message into the ring (should be called from DriverProc derivated routines)
550 static int ALSA_AddRingMessage(ALSA_MSG_RING
* omr
, enum win_wm_message msg
, DWORD param
, BOOL wait
)
552 HANDLE hEvent
= INVALID_HANDLE_VALUE
;
554 EnterCriticalSection(&omr
->msg_crst
);
555 if ((omr
->msg_toget
== ((omr
->msg_tosave
+ 1) % ALSA_RING_BUFFER_SIZE
))) /* buffer overflow ? */
557 ERR("buffer overflow !?\n");
558 LeaveCriticalSection(&omr
->msg_crst
);
563 hEvent
= CreateEventA(NULL
, FALSE
, FALSE
, NULL
);
564 if (hEvent
== INVALID_HANDLE_VALUE
)
566 ERR("can't create event !?\n");
567 LeaveCriticalSection(&omr
->msg_crst
);
570 if (omr
->msg_toget
!= omr
->msg_tosave
&& omr
->messages
[omr
->msg_toget
].msg
!= WINE_WM_HEADER
)
571 FIXME("two fast messages in the queue!!!!\n");
573 /* fast messages have to be added at the start of the queue */
574 omr
->msg_toget
= (omr
->msg_toget
+ ALSA_RING_BUFFER_SIZE
- 1) % ALSA_RING_BUFFER_SIZE
;
576 omr
->messages
[omr
->msg_toget
].msg
= msg
;
577 omr
->messages
[omr
->msg_toget
].param
= param
;
578 omr
->messages
[omr
->msg_toget
].hEvent
= hEvent
;
582 omr
->messages
[omr
->msg_tosave
].msg
= msg
;
583 omr
->messages
[omr
->msg_tosave
].param
= param
;
584 omr
->messages
[omr
->msg_tosave
].hEvent
= INVALID_HANDLE_VALUE
;
585 omr
->msg_tosave
= (omr
->msg_tosave
+ 1) % ALSA_RING_BUFFER_SIZE
;
587 LeaveCriticalSection(&omr
->msg_crst
);
588 /* signal a new message */
589 SetEvent(omr
->msg_event
);
592 /* wait for playback/record thread to have processed the message */
593 WaitForSingleObject(hEvent
, INFINITE
);
599 /******************************************************************
600 * ALSA_RetrieveRingMessage
602 * Get a message from the ring. Should be called by the playback/record thread.
604 static int ALSA_RetrieveRingMessage(ALSA_MSG_RING
* omr
,
605 enum win_wm_message
*msg
, DWORD
*param
, HANDLE
*hEvent
)
607 EnterCriticalSection(&omr
->msg_crst
);
609 if (omr
->msg_toget
== omr
->msg_tosave
) /* buffer empty ? */
611 LeaveCriticalSection(&omr
->msg_crst
);
615 *msg
= omr
->messages
[omr
->msg_toget
].msg
;
616 omr
->messages
[omr
->msg_toget
].msg
= 0;
617 *param
= omr
->messages
[omr
->msg_toget
].param
;
618 *hEvent
= omr
->messages
[omr
->msg_toget
].hEvent
;
619 omr
->msg_toget
= (omr
->msg_toget
+ 1) % ALSA_RING_BUFFER_SIZE
;
620 LeaveCriticalSection(&omr
->msg_crst
);
624 /*======================================================================*
625 * Low level WAVE OUT implementation *
626 *======================================================================*/
629 /**************************************************************************
630 * wodNotifyClient [internal]
632 static DWORD
wodNotifyClient(WINE_WAVEOUT
* wwo
, WORD wMsg
, DWORD dwParam1
, DWORD dwParam2
)
634 TRACE("wMsg = 0x%04x dwParm1 = %04lX dwParam2 = %04lX\n", wMsg
, dwParam1
, dwParam2
);
640 if (wwo
->wFlags
!= DCB_NULL
&&
641 !DriverCallback(wwo
->waveDesc
.dwCallback
, wwo
->wFlags
, (HDRVR
)wwo
->waveDesc
.hWave
,
642 wMsg
, wwo
->waveDesc
.dwInstance
, dwParam1
, dwParam2
)) {
643 WARN("can't notify client !\n");
644 return MMSYSERR_ERROR
;
648 FIXME("Unknown callback message %u\n", wMsg
);
649 return MMSYSERR_INVALPARAM
;
651 return MMSYSERR_NOERROR
;
654 /**************************************************************************
655 * wodUpdatePlayedTotal [internal]
658 static BOOL
wodUpdatePlayedTotal(WINE_WAVEOUT
* wwo
, snd_pcm_status_t
* ps
)
663 /**************************************************************************
664 * wodPlayer_BeginWaveHdr [internal]
666 * Makes the specified lpWaveHdr the currently playing wave header.
667 * If the specified wave header is a begin loop and we're not already in
668 * a loop, setup the loop.
670 static void wodPlayer_BeginWaveHdr(WINE_WAVEOUT
* wwo
, LPWAVEHDR lpWaveHdr
)
672 wwo
->lpPlayPtr
= lpWaveHdr
;
674 if (!lpWaveHdr
) return;
676 wwo
->lpPlayPtr
->reserved
= 0;
678 if (lpWaveHdr
->dwFlags
& WHDR_BEGINLOOP
) {
679 if (wwo
->lpLoopPtr
) {
680 WARN("Already in a loop. Discarding loop on this header (%p)\n", lpWaveHdr
);
682 TRACE("Starting loop (%ldx) with %p\n", lpWaveHdr
->dwLoops
, lpWaveHdr
);
683 wwo
->lpLoopPtr
= lpWaveHdr
;
684 /* Windows does not touch WAVEHDR.dwLoops,
685 * so we need to make an internal copy */
686 wwo
->dwLoops
= lpWaveHdr
->dwLoops
;
691 /**************************************************************************
692 * wodPlayer_PlayPtrNext [internal]
694 * Advance the play pointer to the next waveheader, looping if required.
696 static LPWAVEHDR
wodPlayer_PlayPtrNext(WINE_WAVEOUT
* wwo
)
698 LPWAVEHDR lpWaveHdr
= wwo
->lpPlayPtr
;
700 if ((lpWaveHdr
->dwFlags
& WHDR_ENDLOOP
) && wwo
->lpLoopPtr
) {
701 /* We're at the end of a loop, loop if required */
702 if (--wwo
->dwLoops
> 0) {
703 wwo
->lpPlayPtr
= wwo
->lpLoopPtr
;
704 wwo
->lpPlayPtr
->reserved
= 0;
706 /* Handle overlapping loops correctly */
707 if (wwo
->lpLoopPtr
!= lpWaveHdr
&& (lpWaveHdr
->dwFlags
& WHDR_BEGINLOOP
)) {
708 FIXME("Correctly handled case ? (ending loop buffer also starts a new loop)\n");
709 /* shall we consider the END flag for the closing loop or for
710 * the opening one or for both ???
711 * code assumes for closing loop only
714 lpWaveHdr
= lpWaveHdr
->lpNext
;
716 wwo
->lpLoopPtr
= NULL
;
717 wodPlayer_BeginWaveHdr(wwo
, lpWaveHdr
);
720 /* We're not in a loop. Advance to the next wave header */
721 wodPlayer_BeginWaveHdr(wwo
, lpWaveHdr
= lpWaveHdr
->lpNext
);
727 /**************************************************************************
728 * wodPlayer_DSPWait [internal]
729 * Returns the number of milliseconds to wait for the DSP buffer to play a
732 static DWORD
wodPlayer_DSPWait(const WINE_WAVEOUT
*wwo
)
734 /* time for one period to be played */
735 return snd_pcm_hw_params_get_period_time(wwo
->hw_params
, 0) / 1000;
738 /**************************************************************************
739 * wodPllayer_NotifyWait [internal]
740 * Returns the number of milliseconds to wait before attempting to notify
741 * completion of the specified wavehdr.
742 * This is based on the number of bytes remaining to be written in the
745 static DWORD
wodPlayer_NotifyWait(const WINE_WAVEOUT
* wwo
, LPWAVEHDR lpWaveHdr
)
749 dwMillis
= (lpWaveHdr
->dwBufferLength
- lpWaveHdr
->reserved
) * 1000 / wwo
->format
.wf
.nAvgBytesPerSec
;
750 if (!dwMillis
) dwMillis
= 1;
756 /**************************************************************************
757 * wodPlayer_WriteMaxFrags [internal]
758 * Writes the maximum number of frames possible to the DSP and returns
759 * the number of frames written.
761 static int wodPlayer_WriteMaxFrags(WINE_WAVEOUT
* wwo
, DWORD
* frames
)
763 /* Only attempt to write to free frames */
764 LPWAVEHDR lpWaveHdr
= wwo
->lpPlayPtr
;
765 DWORD dwLength
= snd_pcm_bytes_to_frames(wwo
->p_handle
, lpWaveHdr
->dwBufferLength
- lpWaveHdr
->reserved
);
766 int toWrite
= min(dwLength
, *frames
);
769 TRACE("Writing wavehdr %p.%lu[%lu]\n", lpWaveHdr
, lpWaveHdr
->reserved
, lpWaveHdr
->dwBufferLength
);
771 written
= (wwo
->write
)(wwo
->p_handle
, lpWaveHdr
->lpData
+ lpWaveHdr
->reserved
, toWrite
);
774 /* XRUN occurred. let's try to recover */
775 ALSA_XRUNRecovery(wwo
, written
);
776 written
= (wwo
->write
)(wwo
->p_handle
, lpWaveHdr
->lpData
+ lpWaveHdr
->reserved
, toWrite
);
781 ERR("Error in writing wavehdr. Reason: %s\n", snd_strerror(written
));
785 lpWaveHdr
->reserved
+= snd_pcm_frames_to_bytes(wwo
->p_handle
, written
);
786 if ( lpWaveHdr
->reserved
>= lpWaveHdr
->dwBufferLength
) {
787 /* this will be used to check if the given wave header has been fully played or not... */
788 lpWaveHdr
->reserved
= lpWaveHdr
->dwBufferLength
;
789 /* If we wrote all current wavehdr, skip to the next one */
790 wodPlayer_PlayPtrNext(wwo
);
793 wwo
->dwPlayedTotal
+= snd_pcm_frames_to_bytes(wwo
->p_handle
, written
);
799 /**************************************************************************
800 * wodPlayer_NotifyCompletions [internal]
802 * Notifies and remove from queue all wavehdrs which have been played to
803 * the speaker (ie. they have cleared the ALSA buffer). If force is true,
804 * we notify all wavehdrs and remove them all from the queue even if they
805 * are unplayed or part of a loop.
807 static DWORD
wodPlayer_NotifyCompletions(WINE_WAVEOUT
* wwo
, BOOL force
)
811 /* Start from lpQueuePtr and keep notifying until:
812 * - we hit an unwritten wavehdr
813 * - we hit the beginning of a running loop
814 * - we hit a wavehdr which hasn't finished playing
816 while ((lpWaveHdr
= wwo
->lpQueuePtr
) &&
818 (lpWaveHdr
!= wwo
->lpPlayPtr
&&
819 lpWaveHdr
!= wwo
->lpLoopPtr
&&
820 lpWaveHdr
->reserved
== lpWaveHdr
->dwBufferLength
))) {
822 wwo
->lpQueuePtr
= lpWaveHdr
->lpNext
;
824 lpWaveHdr
->dwFlags
&= ~WHDR_INQUEUE
;
825 lpWaveHdr
->dwFlags
|= WHDR_DONE
;
827 wodNotifyClient(wwo
, WOM_DONE
, (DWORD
)lpWaveHdr
, 0);
829 return (lpWaveHdr
&& lpWaveHdr
!= wwo
->lpPlayPtr
&& lpWaveHdr
!= wwo
->lpLoopPtr
) ?
830 wodPlayer_NotifyWait(wwo
, lpWaveHdr
) : INFINITE
;
833 /**************************************************************************
834 * wodPlayer_Reset [internal]
836 * wodPlayer helper. Resets current output stream.
838 static void wodPlayer_Reset(WINE_WAVEOUT
* wwo
)
840 enum win_wm_message msg
;
845 /* updates current notify list */
846 wodPlayer_NotifyCompletions(wwo
, FALSE
);
848 if ( (err
= snd_pcm_drop(wwo
->p_handle
)) < 0) {
849 FIXME("flush: %s\n", snd_strerror(err
));
851 wwo
->state
= WINE_WS_STOPPED
;
854 if ( (err
= snd_pcm_prepare(wwo
->p_handle
)) < 0 )
855 ERR("pcm prepare failed: %s\n", snd_strerror(err
));
857 /* remove any buffer */
858 wodPlayer_NotifyCompletions(wwo
, TRUE
);
860 wwo
->lpPlayPtr
= wwo
->lpQueuePtr
= wwo
->lpLoopPtr
= NULL
;
861 wwo
->state
= WINE_WS_STOPPED
;
863 /* remove any existing message in the ring */
864 EnterCriticalSection(&wwo
->msgRing
.msg_crst
);
865 /* return all pending headers in queue */
866 while (ALSA_RetrieveRingMessage(&wwo
->msgRing
, &msg
, ¶m
, &ev
))
868 if (msg
!= WINE_WM_HEADER
)
870 FIXME("shouldn't have headers left\n");
874 ((LPWAVEHDR
)param
)->dwFlags
&= ~WHDR_INQUEUE
;
875 ((LPWAVEHDR
)param
)->dwFlags
|= WHDR_DONE
;
877 wodNotifyClient(wwo
, WOM_DONE
, param
, 0);
879 ResetEvent(wwo
->msgRing
.msg_event
);
880 LeaveCriticalSection(&wwo
->msgRing
.msg_crst
);
883 /**************************************************************************
884 * wodPlayer_ProcessMessages [internal]
886 static void wodPlayer_ProcessMessages(WINE_WAVEOUT
* wwo
)
889 enum win_wm_message msg
;
894 while (ALSA_RetrieveRingMessage(&wwo
->msgRing
, &msg
, ¶m
, &ev
)) {
895 /* TRACE("Received %s %lx\n", wodPlayerCmdString[msg - WM_USER - 1], param); */
898 case WINE_WM_PAUSING
:
899 if ( snd_pcm_state(wwo
->p_handle
) == SND_PCM_STATE_RUNNING
)
901 err
= snd_pcm_pause(wwo
->p_handle
, 1);
903 ERR("pcm_pause failed: %s\n", snd_strerror(err
));
905 wwo
->state
= WINE_WS_PAUSED
;
908 case WINE_WM_RESTARTING
:
909 if (wwo
->state
== WINE_WS_PAUSED
)
911 if ( snd_pcm_state(wwo
->p_handle
) == SND_PCM_STATE_PAUSED
)
913 err
= snd_pcm_pause(wwo
->p_handle
, 0);
915 ERR("pcm_pause failed: %s\n", snd_strerror(err
));
917 wwo
->state
= WINE_WS_PLAYING
;
922 lpWaveHdr
= (LPWAVEHDR
)param
;
924 /* insert buffer at the end of queue */
927 for (wh
= &(wwo
->lpQueuePtr
); *wh
; wh
= &((*wh
)->lpNext
));
931 wodPlayer_BeginWaveHdr(wwo
,lpWaveHdr
);
932 if (wwo
->state
== WINE_WS_STOPPED
)
933 wwo
->state
= WINE_WS_PLAYING
;
935 case WINE_WM_RESETTING
:
936 wodPlayer_Reset(wwo
);
940 wodUpdatePlayedTotal(wwo
, NULL
);
943 case WINE_WM_BREAKLOOP
:
944 if (wwo
->state
== WINE_WS_PLAYING
&& wwo
->lpLoopPtr
!= NULL
) {
945 /* ensure exit at end of current loop */
950 case WINE_WM_CLOSING
:
951 /* sanity check: this should not happen since the device must have been reset before */
952 if (wwo
->lpQueuePtr
|| wwo
->lpPlayPtr
) ERR("out of sync\n");
954 wwo
->state
= WINE_WS_CLOSED
;
957 /* shouldn't go here */
959 FIXME("unknown message %d\n", msg
);
965 /**************************************************************************
966 * wodPlayer_FeedDSP [internal]
967 * Feed as much sound data as we can into the DSP and return the number of
968 * milliseconds before it will be necessary to feed the DSP again.
970 static DWORD
wodPlayer_FeedDSP(WINE_WAVEOUT
* wwo
)
972 DWORD availInQ
= snd_pcm_avail_update(wwo
->p_handle
);
974 /* no more room... no need to try to feed */
975 while (wwo
->lpPlayPtr
&& availInQ
> 0)
976 if ( wodPlayer_WriteMaxFrags(wwo
, &availInQ
) < 0 )
979 return wodPlayer_DSPWait(wwo
);
982 /**************************************************************************
983 * wodPlayer [internal]
985 static DWORD CALLBACK
wodPlayer(LPVOID pmt
)
987 WORD uDevID
= (DWORD
)pmt
;
988 WINE_WAVEOUT
* wwo
= (WINE_WAVEOUT
*)&WOutDev
[uDevID
];
989 DWORD dwNextFeedTime
= INFINITE
; /* Time before DSP needs feeding */
990 DWORD dwNextNotifyTime
= INFINITE
; /* Time before next wave completion */
993 wwo
->state
= WINE_WS_STOPPED
;
994 SetEvent(wwo
->hStartUpEvent
);
997 /** Wait for the shortest time before an action is required. If there
998 * are no pending actions, wait forever for a command.
1000 dwSleepTime
= min(dwNextFeedTime
, dwNextNotifyTime
);
1001 TRACE("waiting %lums (%lu,%lu)\n", dwSleepTime
, dwNextFeedTime
, dwNextNotifyTime
);
1002 WaitForSingleObject(wwo
->msgRing
.msg_event
, dwSleepTime
);
1003 wodPlayer_ProcessMessages(wwo
);
1004 if (wwo
->state
== WINE_WS_PLAYING
) {
1005 dwNextFeedTime
= wodPlayer_FeedDSP(wwo
);
1006 dwNextNotifyTime
= wodPlayer_NotifyCompletions(wwo
, FALSE
);
1008 dwNextFeedTime
= dwNextNotifyTime
= INFINITE
;
1013 /**************************************************************************
1014 * wodGetDevCaps [internal]
1016 static DWORD
wodGetDevCaps(WORD wDevID
, LPWAVEOUTCAPSA lpCaps
, DWORD dwSize
)
1018 TRACE("(%u, %p, %lu);\n", wDevID
, lpCaps
, dwSize
);
1020 if (lpCaps
== NULL
) return MMSYSERR_NOTENABLED
;
1022 if (wDevID
>= MAX_WAVEOUTDRV
) {
1023 TRACE("MAX_WAVOUTDRV reached !\n");
1024 return MMSYSERR_BADDEVICEID
;
1027 memcpy(lpCaps
, &WOutDev
[wDevID
].caps
, min(dwSize
, sizeof(*lpCaps
)));
1028 return MMSYSERR_NOERROR
;
1031 /**************************************************************************
1032 * wodOpen [internal]
1034 static DWORD
wodOpen(WORD wDevID
, LPWAVEOPENDESC lpDesc
, DWORD dwFlags
)
1037 snd_pcm_hw_params_t
* hw_params
;
1038 snd_pcm_sw_params_t
* sw_params
;
1039 snd_pcm_access_t access
;
1040 snd_pcm_format_t format
;
1042 unsigned int buffer_time
= 500000;
1043 unsigned int period_time
= 10000;
1045 snd_pcm_uframes_t period_size
;
1050 snd_pcm_hw_params_alloca(&hw_params
);
1051 snd_pcm_sw_params_alloca(&sw_params
);
1053 TRACE("(%u, %p, %08lX);\n", wDevID
, lpDesc
, dwFlags
);
1054 if (lpDesc
== NULL
) {
1055 WARN("Invalid Parameter !\n");
1056 return MMSYSERR_INVALPARAM
;
1058 if (wDevID
>= MAX_WAVEOUTDRV
) {
1059 TRACE("MAX_WAVOUTDRV reached !\n");
1060 return MMSYSERR_BADDEVICEID
;
1063 /* only PCM format is supported so far... */
1064 if (lpDesc
->lpFormat
->wFormatTag
!= WAVE_FORMAT_PCM
||
1065 lpDesc
->lpFormat
->nChannels
== 0 ||
1066 lpDesc
->lpFormat
->nSamplesPerSec
== 0) {
1067 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
1068 lpDesc
->lpFormat
->wFormatTag
, lpDesc
->lpFormat
->nChannels
,
1069 lpDesc
->lpFormat
->nSamplesPerSec
);
1070 return WAVERR_BADFORMAT
;
1073 if (dwFlags
& WAVE_FORMAT_QUERY
) {
1074 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
1075 lpDesc
->lpFormat
->wFormatTag
, lpDesc
->lpFormat
->nChannels
,
1076 lpDesc
->lpFormat
->nSamplesPerSec
);
1077 return MMSYSERR_NOERROR
;
1080 wwo
= &WOutDev
[wDevID
];
1082 if ((dwFlags
& WAVE_DIRECTSOUND
) && !(wwo
->caps
.dwSupport
& WAVECAPS_DIRECTSOUND
))
1083 /* not supported, ignore it */
1084 dwFlags
&= ~WAVE_DIRECTSOUND
;
1087 flags
= SND_PCM_NONBLOCK
;
1088 if ( dwFlags
& WAVE_DIRECTSOUND
)
1089 flags
|= SND_PCM_ASYNC
;
1091 if (snd_pcm_open(&pcm
, wwo
->device
, SND_PCM_STREAM_PLAYBACK
, dwFlags
))
1093 ERR("Error open: %s\n", snd_strerror(errno
));
1094 return MMSYSERR_NOTENABLED
;
1097 wwo
->wFlags
= HIWORD(dwFlags
& CALLBACK_TYPEMASK
);
1099 memcpy(&wwo
->waveDesc
, lpDesc
, sizeof(WAVEOPENDESC
));
1100 memcpy(&wwo
->format
, lpDesc
->lpFormat
, sizeof(PCMWAVEFORMAT
));
1102 if (wwo
->format
.wBitsPerSample
== 0) {
1103 WARN("Resetting zeroed wBitsPerSample\n");
1104 wwo
->format
.wBitsPerSample
= 8 *
1105 (wwo
->format
.wf
.nAvgBytesPerSec
/
1106 wwo
->format
.wf
.nSamplesPerSec
) /
1107 wwo
->format
.wf
.nChannels
;
1110 snd_pcm_hw_params_any(pcm
, hw_params
);
1112 #define EXIT_ON_ERROR(f,e,txt) do \
1115 if ( (err = (f) ) < 0) \
1117 ERR(txt ": %s\n", snd_strerror(err)); \
1118 snd_pcm_close(pcm); \
1123 access
= SND_PCM_ACCESS_MMAP_INTERLEAVED
;
1124 if ( ( err
= snd_pcm_hw_params_set_access(pcm
, hw_params
, access
) ) < 0) {
1125 WARN("mmap not available. switching to standard write.\n");
1126 access
= SND_PCM_ACCESS_RW_INTERLEAVED
;
1127 EXIT_ON_ERROR( snd_pcm_hw_params_set_access(pcm
, hw_params
, access
), MMSYSERR_INVALPARAM
, "unable to set access for playback");
1128 wwo
->write
= snd_pcm_writei
;
1131 wwo
->write
= snd_pcm_mmap_writei
;
1133 EXIT_ON_ERROR( snd_pcm_hw_params_set_channels(pcm
, hw_params
, wwo
->format
.wf
.nChannels
), MMSYSERR_INVALPARAM
, "unable to set required channels");
1135 format
= (wwo
->format
.wBitsPerSample
== 16) ? SND_PCM_FORMAT_S16_LE
: SND_PCM_FORMAT_U8
;
1136 EXIT_ON_ERROR( snd_pcm_hw_params_set_format(pcm
, hw_params
, format
), MMSYSERR_INVALPARAM
, "unable to set required format");
1138 rate
= snd_pcm_hw_params_set_rate_near(pcm
, hw_params
, wwo
->format
.wf
.nSamplesPerSec
, 0);
1140 ERR("Rate %ld Hz not available for playback: %s\n", wwo
->format
.wf
.nSamplesPerSec
, snd_strerror(rate
));
1142 return WAVERR_BADFORMAT
;
1144 if (rate
!= wwo
->format
.wf
.nSamplesPerSec
) {
1145 ERR("Rate doesn't match (requested %ld Hz, got %d Hz)\n", wwo
->format
.wf
.nSamplesPerSec
, rate
);
1147 return WAVERR_BADFORMAT
;
1150 EXIT_ON_ERROR( snd_pcm_hw_params_set_buffer_time_near(pcm
, hw_params
, buffer_time
, 0), MMSYSERR_INVALPARAM
, "unable to set buffer time");
1151 EXIT_ON_ERROR( snd_pcm_hw_params_set_period_time_near(pcm
, hw_params
, period_time
, 0), MMSYSERR_INVALPARAM
, "unable to set period time");
1153 EXIT_ON_ERROR( snd_pcm_hw_params(pcm
, hw_params
), MMSYSERR_INVALPARAM
, "unable to set hw params for playback");
1155 period_size
= snd_pcm_hw_params_get_period_size(hw_params
, 0);
1156 buffer_size
= snd_pcm_hw_params_get_buffer_size(hw_params
);
1158 snd_pcm_sw_params_current(pcm
, sw_params
);
1159 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");
1160 EXIT_ON_ERROR( snd_pcm_sw_params_set_silence_size(pcm
, sw_params
, period_size
*2), MMSYSERR_ERROR
, "unable to set silence size");
1161 EXIT_ON_ERROR( snd_pcm_sw_params_set_avail_min(pcm
, sw_params
, period_size
), MMSYSERR_ERROR
, "unable to set avail min");
1162 EXIT_ON_ERROR( snd_pcm_sw_params_set_xfer_align(pcm
, sw_params
, 1), MMSYSERR_ERROR
, "unable to set xfer align");
1163 EXIT_ON_ERROR( snd_pcm_sw_params_set_silence_threshold(pcm
, sw_params
, 0), MMSYSERR_ERROR
, "unable to set silence threshold");
1164 EXIT_ON_ERROR( snd_pcm_sw_params(pcm
, sw_params
), MMSYSERR_ERROR
, "unable to set sw params for playback");
1165 #undef EXIT_ON_ERROR
1167 snd_pcm_prepare(pcm
);
1170 ALSA_TraceParameters(hw_params
, sw_params
, FALSE
);
1172 /* now, we can save all required data for later use... */
1173 if ( wwo
->hw_params
)
1174 snd_pcm_hw_params_free(wwo
->hw_params
);
1175 snd_pcm_hw_params_malloc(&(wwo
->hw_params
));
1176 snd_pcm_hw_params_copy(wwo
->hw_params
, hw_params
);
1178 wwo
->dwBufferSize
= buffer_size
;
1179 wwo
->lpQueuePtr
= wwo
->lpPlayPtr
= wwo
->lpLoopPtr
= NULL
;
1180 wwo
->p_handle
= pcm
;
1182 ALSA_InitRingMessage(&wwo
->msgRing
);
1184 if (!(dwFlags
& WAVE_DIRECTSOUND
)) {
1185 wwo
->hStartUpEvent
= CreateEventA(NULL
, FALSE
, FALSE
, NULL
);
1186 wwo
->hThread
= CreateThread(NULL
, 0, wodPlayer
, (LPVOID
)(DWORD
)wDevID
, 0, &(wwo
->dwThreadID
));
1187 WaitForSingleObject(wwo
->hStartUpEvent
, INFINITE
);
1188 CloseHandle(wwo
->hStartUpEvent
);
1190 wwo
->hThread
= INVALID_HANDLE_VALUE
;
1191 wwo
->dwThreadID
= 0;
1193 wwo
->hStartUpEvent
= INVALID_HANDLE_VALUE
;
1195 TRACE("handle=%08lx \n", (DWORD
)wwo
->p_handle
);
1196 /* if (wwo->dwFragmentSize % wwo->format.wf.nBlockAlign)
1197 ERR("Fragment doesn't contain an integral number of data blocks\n");
1199 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%lu, nSamplesPerSec=%lu, nChannels=%u nBlockAlign=%u!\n",
1200 wwo
->format
.wBitsPerSample
, wwo
->format
.wf
.nAvgBytesPerSec
,
1201 wwo
->format
.wf
.nSamplesPerSec
, wwo
->format
.wf
.nChannels
,
1202 wwo
->format
.wf
.nBlockAlign
);
1204 return wodNotifyClient(wwo
, WOM_OPEN
, 0L, 0L);
1208 /**************************************************************************
1209 * wodClose [internal]
1211 static DWORD
wodClose(WORD wDevID
)
1213 DWORD ret
= MMSYSERR_NOERROR
;
1216 TRACE("(%u);\n", wDevID
);
1218 if (wDevID
>= MAX_WAVEOUTDRV
|| WOutDev
[wDevID
].p_handle
== NULL
) {
1219 WARN("bad device ID !\n");
1220 return MMSYSERR_BADDEVICEID
;
1223 wwo
= &WOutDev
[wDevID
];
1224 if (wwo
->lpQueuePtr
) {
1225 WARN("buffers still playing !\n");
1226 ret
= WAVERR_STILLPLAYING
;
1228 if (wwo
->hThread
!= INVALID_HANDLE_VALUE
) {
1229 ALSA_AddRingMessage(&wwo
->msgRing
, WINE_WM_CLOSING
, 0, TRUE
);
1231 ALSA_DestroyRingMessage(&wwo
->msgRing
);
1233 snd_pcm_hw_params_free(wwo
->hw_params
);
1234 wwo
->hw_params
= NULL
;
1236 snd_pcm_close(wwo
->p_handle
);
1237 wwo
->p_handle
= NULL
;
1239 ret
= wodNotifyClient(wwo
, WOM_CLOSE
, 0L, 0L);
1245 /**************************************************************************
1246 * wodWrite [internal]
1249 static DWORD
wodWrite(WORD wDevID
, LPWAVEHDR lpWaveHdr
, DWORD dwSize
)
1251 TRACE("(%u, %p, %08lX);\n", wDevID
, lpWaveHdr
, dwSize
);
1253 /* first, do the sanity checks... */
1254 if (wDevID
>= MAX_WAVEOUTDRV
|| WOutDev
[wDevID
].p_handle
== NULL
) {
1255 WARN("bad dev ID !\n");
1256 return MMSYSERR_BADDEVICEID
;
1259 if (lpWaveHdr
->lpData
== NULL
|| !(lpWaveHdr
->dwFlags
& WHDR_PREPARED
))
1260 return WAVERR_UNPREPARED
;
1262 if (lpWaveHdr
->dwFlags
& WHDR_INQUEUE
)
1263 return WAVERR_STILLPLAYING
;
1265 lpWaveHdr
->dwFlags
&= ~WHDR_DONE
;
1266 lpWaveHdr
->dwFlags
|= WHDR_INQUEUE
;
1267 lpWaveHdr
->lpNext
= 0;
1269 ALSA_AddRingMessage(&WOutDev
[wDevID
].msgRing
, WINE_WM_HEADER
, (DWORD
)lpWaveHdr
, FALSE
);
1271 return MMSYSERR_NOERROR
;
1274 /**************************************************************************
1275 * wodPrepare [internal]
1277 static DWORD
wodPrepare(WORD wDevID
, LPWAVEHDR lpWaveHdr
, DWORD dwSize
)
1279 TRACE("(%u, %p, %08lX);\n", wDevID
, lpWaveHdr
, dwSize
);
1281 if (wDevID
>= MAX_WAVEOUTDRV
) {
1282 WARN("bad device ID !\n");
1283 return MMSYSERR_BADDEVICEID
;
1286 if (lpWaveHdr
->dwFlags
& WHDR_INQUEUE
)
1287 return WAVERR_STILLPLAYING
;
1289 lpWaveHdr
->dwFlags
|= WHDR_PREPARED
;
1290 lpWaveHdr
->dwFlags
&= ~WHDR_DONE
;
1291 return MMSYSERR_NOERROR
;
1294 /**************************************************************************
1295 * wodUnprepare [internal]
1297 static DWORD
wodUnprepare(WORD wDevID
, LPWAVEHDR lpWaveHdr
, DWORD dwSize
)
1299 TRACE("(%u, %p, %08lX);\n", wDevID
, lpWaveHdr
, dwSize
);
1301 if (wDevID
>= MAX_WAVEOUTDRV
) {
1302 WARN("bad device ID !\n");
1303 return MMSYSERR_BADDEVICEID
;
1306 if (lpWaveHdr
->dwFlags
& WHDR_INQUEUE
)
1307 return WAVERR_STILLPLAYING
;
1309 lpWaveHdr
->dwFlags
&= ~WHDR_PREPARED
;
1310 lpWaveHdr
->dwFlags
|= WHDR_DONE
;
1312 return MMSYSERR_NOERROR
;
1315 /**************************************************************************
1316 * wodPause [internal]
1318 static DWORD
wodPause(WORD wDevID
)
1320 TRACE("(%u);!\n", wDevID
);
1322 if (wDevID
>= MAX_WAVEOUTDRV
|| WOutDev
[wDevID
].p_handle
== NULL
) {
1323 WARN("bad device ID !\n");
1324 return MMSYSERR_BADDEVICEID
;
1327 ALSA_AddRingMessage(&WOutDev
[wDevID
].msgRing
, WINE_WM_PAUSING
, 0, TRUE
);
1329 return MMSYSERR_NOERROR
;
1332 /**************************************************************************
1333 * wodRestart [internal]
1335 static DWORD
wodRestart(WORD wDevID
)
1337 TRACE("(%u);\n", wDevID
);
1339 if (wDevID
>= MAX_WAVEOUTDRV
|| WOutDev
[wDevID
].p_handle
== NULL
) {
1340 WARN("bad device ID !\n");
1341 return MMSYSERR_BADDEVICEID
;
1344 if (WOutDev
[wDevID
].state
== WINE_WS_PAUSED
) {
1345 ALSA_AddRingMessage(&WOutDev
[wDevID
].msgRing
, WINE_WM_RESTARTING
, 0, TRUE
);
1348 /* FIXME: is NotifyClient with WOM_DONE right ? (Comet Busters 1.3.3 needs this notification) */
1349 /* FIXME: Myst crashes with this ... hmm -MM
1350 return wodNotifyClient(wwo, WOM_DONE, 0L, 0L);
1353 return MMSYSERR_NOERROR
;
1356 /**************************************************************************
1357 * wodReset [internal]
1359 static DWORD
wodReset(WORD wDevID
)
1361 TRACE("(%u);\n", wDevID
);
1363 if (wDevID
>= MAX_WAVEOUTDRV
|| WOutDev
[wDevID
].p_handle
== NULL
) {
1364 WARN("bad device ID !\n");
1365 return MMSYSERR_BADDEVICEID
;
1368 ALSA_AddRingMessage(&WOutDev
[wDevID
].msgRing
, WINE_WM_RESETTING
, 0, TRUE
);
1370 return MMSYSERR_NOERROR
;
1373 /**************************************************************************
1374 * wodGetPosition [internal]
1376 static DWORD
wodGetPosition(WORD wDevID
, LPMMTIME lpTime
, DWORD uSize
)
1382 TRACE("(%u, %p, %lu);\n", wDevID
, lpTime
, uSize
);
1384 if (wDevID
>= MAX_WAVEOUTDRV
|| WOutDev
[wDevID
].p_handle
== NULL
) {
1385 WARN("bad device ID !\n");
1386 return MMSYSERR_BADDEVICEID
;
1389 if (lpTime
== NULL
) return MMSYSERR_INVALPARAM
;
1391 wwo
= &WOutDev
[wDevID
];
1392 ALSA_AddRingMessage(&wwo
->msgRing
, WINE_WM_UPDATE
, 0, TRUE
);
1393 val
= wwo
->dwPlayedTotal
;
1395 TRACE("wType=%04X wBitsPerSample=%u nSamplesPerSec=%lu nChannels=%u nAvgBytesPerSec=%lu\n",
1396 lpTime
->wType
, wwo
->format
.wBitsPerSample
,
1397 wwo
->format
.wf
.nSamplesPerSec
, wwo
->format
.wf
.nChannels
,
1398 wwo
->format
.wf
.nAvgBytesPerSec
);
1399 TRACE("dwPlayedTotal=%lu\n", val
);
1401 switch (lpTime
->wType
) {
1404 TRACE("TIME_BYTES=%lu\n", lpTime
->u
.cb
);
1407 lpTime
->u
.sample
= val
* 8 / wwo
->format
.wBitsPerSample
/wwo
->format
.wf
.nChannels
;
1408 TRACE("TIME_SAMPLES=%lu\n", lpTime
->u
.sample
);
1411 time
= val
/ (wwo
->format
.wf
.nAvgBytesPerSec
/ 1000);
1412 lpTime
->u
.smpte
.hour
= time
/ 108000;
1413 time
-= lpTime
->u
.smpte
.hour
* 108000;
1414 lpTime
->u
.smpte
.min
= time
/ 1800;
1415 time
-= lpTime
->u
.smpte
.min
* 1800;
1416 lpTime
->u
.smpte
.sec
= time
/ 30;
1417 time
-= lpTime
->u
.smpte
.sec
* 30;
1418 lpTime
->u
.smpte
.frame
= time
;
1419 lpTime
->u
.smpte
.fps
= 30;
1420 TRACE("TIME_SMPTE=%02u:%02u:%02u:%02u\n",
1421 lpTime
->u
.smpte
.hour
, lpTime
->u
.smpte
.min
,
1422 lpTime
->u
.smpte
.sec
, lpTime
->u
.smpte
.frame
);
1425 FIXME("Format %d not supported ! use TIME_MS !\n", lpTime
->wType
);
1426 lpTime
->wType
= TIME_MS
;
1428 lpTime
->u
.ms
= val
/ (wwo
->format
.wf
.nAvgBytesPerSec
/ 1000);
1429 TRACE("TIME_MS=%lu\n", lpTime
->u
.ms
);
1432 return MMSYSERR_NOERROR
;
1435 /**************************************************************************
1436 * wodBreakLoop [internal]
1438 static DWORD
wodBreakLoop(WORD wDevID
)
1440 TRACE("(%u);\n", wDevID
);
1442 if (wDevID
>= MAX_WAVEOUTDRV
|| WOutDev
[wDevID
].p_handle
== NULL
) {
1443 WARN("bad device ID !\n");
1444 return MMSYSERR_BADDEVICEID
;
1446 ALSA_AddRingMessage(&WOutDev
[wDevID
].msgRing
, WINE_WM_BREAKLOOP
, 0, TRUE
);
1447 return MMSYSERR_NOERROR
;
1450 /**************************************************************************
1451 * wodGetVolume [internal]
1453 static DWORD
wodGetVolume(WORD wDevID
, LPDWORD lpdwVol
)
1460 TRACE("(%u, %p);\n", wDevID
, lpdwVol
);
1461 if (wDevID
>= MAX_WAVEOUTDRV
|| WOutDev
[wDevID
].p_handle
== NULL
) {
1462 WARN("bad device ID !\n");
1463 return MMSYSERR_BADDEVICEID
;
1465 wwo
= &WOutDev
[wDevID
];
1466 count
= snd_ctl_elem_info_get_count(wwo
->playback_einfo
);
1467 min
= snd_ctl_elem_info_get_min(wwo
->playback_einfo
);
1468 max
= snd_ctl_elem_info_get_max(wwo
->playback_einfo
);
1470 #define VOLUME_ALSA_TO_WIN(x) (((x)-min) * 65536 /(max-min))
1471 if (lpdwVol
== NULL
)
1472 return MMSYSERR_NOTENABLED
;
1477 left
= VOLUME_ALSA_TO_WIN(snd_ctl_elem_value_get_integer(wwo
->playback_evalue
, 0));
1478 right
= VOLUME_ALSA_TO_WIN(snd_ctl_elem_value_get_integer(wwo
->playback_evalue
, 1));
1481 left
= right
= VOLUME_ALSA_TO_WIN(snd_ctl_elem_value_get_integer(wwo
->playback_evalue
, 0));
1484 WARN("%d channels mixer not supported\n", count
);
1485 return MMSYSERR_NOERROR
;
1487 #undef VOLUME_ALSA_TO_WIN
1489 TRACE("left=%d right=%d !\n", left
, right
);
1490 *lpdwVol
= MAKELONG( left
, right
);
1491 return MMSYSERR_NOERROR
;
1494 /**************************************************************************
1495 * wodSetVolume [internal]
1497 static DWORD
wodSetVolume(WORD wDevID
, DWORD dwParam
)
1504 TRACE("(%u, %08lX);\n", wDevID
, dwParam
);
1505 if (wDevID
>= MAX_WAVEOUTDRV
|| WOutDev
[wDevID
].p_handle
== NULL
) {
1506 WARN("bad device ID !\n");
1507 return MMSYSERR_BADDEVICEID
;
1509 wwo
= &WOutDev
[wDevID
];
1510 count
=snd_ctl_elem_info_get_count(wwo
->playback_einfo
);
1511 min
= snd_ctl_elem_info_get_min(wwo
->playback_einfo
);
1512 max
= snd_ctl_elem_info_get_max(wwo
->playback_einfo
);
1514 left
= LOWORD(dwParam
);
1515 right
= HIWORD(dwParam
);
1517 #define VOLUME_WIN_TO_ALSA(x) ( (((x) * (max-min)) / 65536) + min )
1521 snd_ctl_elem_value_set_integer(wwo
->playback_evalue
, 0, VOLUME_WIN_TO_ALSA(left
));
1522 snd_ctl_elem_value_set_integer(wwo
->playback_evalue
, 1, VOLUME_WIN_TO_ALSA(right
));
1525 snd_ctl_elem_value_set_integer(wwo
->playback_evalue
, 0, VOLUME_WIN_TO_ALSA(left
));
1528 WARN("%d channels mixer not supported\n", count
);
1530 #undef VOLUME_WIN_TO_ALSA
1531 if ( (err
= snd_ctl_elem_write(wwo
->ctl
, wwo
->playback_evalue
)) < 0)
1533 ERR("error writing snd_ctl_elem_value: %s\n", snd_strerror(err
));
1535 return MMSYSERR_NOERROR
;
1538 /**************************************************************************
1539 * wodGetNumDevs [internal]
1541 static DWORD
wodGetNumDevs(void)
1543 return ALSA_WodNumDevs
;
1547 /**************************************************************************
1548 * wodMessage (WINEALSA.@)
1550 DWORD WINAPI
ALSA_wodMessage(UINT wDevID
, UINT wMsg
, DWORD dwUser
,
1551 DWORD dwParam1
, DWORD dwParam2
)
1553 TRACE("(%u, %04X, %08lX, %08lX, %08lX);\n",
1554 wDevID
, wMsg
, dwUser
, dwParam1
, dwParam2
);
1561 /* FIXME: Pretend this is supported */
1563 case WODM_OPEN
: return wodOpen (wDevID
, (LPWAVEOPENDESC
)dwParam1
, dwParam2
);
1564 case WODM_CLOSE
: return wodClose (wDevID
);
1565 case WODM_GETDEVCAPS
: return wodGetDevCaps (wDevID
, (LPWAVEOUTCAPSA
)dwParam1
, dwParam2
);
1566 case WODM_GETNUMDEVS
: return wodGetNumDevs ();
1567 case WODM_GETPITCH
: return MMSYSERR_NOTSUPPORTED
;
1568 case WODM_SETPITCH
: return MMSYSERR_NOTSUPPORTED
;
1569 case WODM_GETPLAYBACKRATE
: return MMSYSERR_NOTSUPPORTED
;
1570 case WODM_SETPLAYBACKRATE
: return MMSYSERR_NOTSUPPORTED
;
1571 case WODM_WRITE
: return wodWrite (wDevID
, (LPWAVEHDR
)dwParam1
, dwParam2
);
1572 case WODM_PAUSE
: return wodPause (wDevID
);
1573 case WODM_GETPOS
: return wodGetPosition (wDevID
, (LPMMTIME
)dwParam1
, dwParam2
);
1574 case WODM_BREAKLOOP
: return wodBreakLoop (wDevID
);
1575 case WODM_PREPARE
: return wodPrepare (wDevID
, (LPWAVEHDR
)dwParam1
, dwParam2
);
1576 case WODM_UNPREPARE
: return wodUnprepare (wDevID
, (LPWAVEHDR
)dwParam1
, dwParam2
);
1577 case WODM_GETVOLUME
: return wodGetVolume (wDevID
, (LPDWORD
)dwParam1
);
1578 case WODM_SETVOLUME
: return wodSetVolume (wDevID
, dwParam1
);
1579 case WODM_RESTART
: return wodRestart (wDevID
);
1580 case WODM_RESET
: return wodReset (wDevID
);
1581 case DRV_QUERYDSOUNDIFACE
: return wodDsCreate (wDevID
, (PIDSDRIVER
*)dwParam1
);
1582 case DRV_QUERYDSOUNDDESC
: return wodDsDesc (wDevID
, (PDSDRIVERDESC
)dwParam1
);
1583 case DRV_QUERYDSOUNDGUID
: return wodDsGuid (wDevID
, (LPGUID
)dwParam1
);
1586 FIXME("unknown message %d!\n", wMsg
);
1588 return MMSYSERR_NOTSUPPORTED
;
1591 /*======================================================================*
1592 * Low level DSOUND implementation *
1593 *======================================================================*/
1595 typedef struct IDsDriverImpl IDsDriverImpl
;
1596 typedef struct IDsDriverBufferImpl IDsDriverBufferImpl
;
1598 struct IDsDriverImpl
1600 /* IUnknown fields */
1601 ICOM_VFIELD(IDsDriver
);
1603 /* IDsDriverImpl fields */
1605 IDsDriverBufferImpl
*primary
;
1608 struct IDsDriverBufferImpl
1610 /* IUnknown fields */
1611 ICOM_VFIELD(IDsDriverBuffer
);
1613 /* IDsDriverBufferImpl fields */
1616 CRITICAL_SECTION mmap_crst
;
1618 DWORD mmap_buflen_bytes
;
1619 snd_pcm_uframes_t mmap_buflen_frames
;
1620 snd_pcm_channel_area_t
* mmap_areas
;
1621 snd_async_handler_t
* mmap_async_handler
;
1624 static void DSDB_CheckXRUN(IDsDriverBufferImpl
* pdbi
)
1626 WINE_WAVEOUT
* wwo
= &(WOutDev
[pdbi
->drv
->wDevID
]);
1627 snd_pcm_state_t state
= snd_pcm_state(wwo
->p_handle
);
1629 if ( state
== SND_PCM_STATE_XRUN
)
1631 int err
= snd_pcm_prepare(wwo
->p_handle
);
1632 TRACE("xrun occurred\n");
1634 ERR("recovery from xrun failed, prepare failed: %s\n", snd_strerror(err
));
1636 else if ( state
== SND_PCM_STATE_SUSPENDED
)
1638 int err
= snd_pcm_resume(wwo
->p_handle
);
1639 TRACE("recovery from suspension occurred\n");
1640 if (err
< 0 && err
!= -EAGAIN
){
1641 err
= snd_pcm_prepare(wwo
->p_handle
);
1643 ERR("recovery from suspend failed, prepare failed: %s\n", snd_strerror(err
));
1648 static void DSDB_MMAPCopy(IDsDriverBufferImpl
* pdbi
)
1650 WINE_WAVEOUT
* wwo
= &(WOutDev
[pdbi
->drv
->wDevID
]);
1652 snd_pcm_format_t format
;
1653 snd_pcm_uframes_t period_size
;
1654 snd_pcm_sframes_t avail
;
1656 if ( !pdbi
->mmap_buffer
|| !wwo
->hw_params
|| !wwo
->p_handle
)
1659 channels
= snd_pcm_hw_params_get_channels(wwo
->hw_params
);
1660 format
= snd_pcm_hw_params_get_format(wwo
->hw_params
);
1661 period_size
= snd_pcm_hw_params_get_period_size(wwo
->hw_params
, 0);
1662 avail
= snd_pcm_avail_update(wwo
->p_handle
);
1664 DSDB_CheckXRUN(pdbi
);
1666 TRACE("avail=%d format=%s channels=%d\n", (int)avail
, snd_pcm_format_name(format
), channels
);
1668 while (avail
>= period_size
)
1670 const snd_pcm_channel_area_t
*areas
;
1671 snd_pcm_uframes_t ofs
;
1672 snd_pcm_uframes_t frames
;
1675 frames
= avail
/ period_size
* period_size
; /* round down to a multiple of period_size */
1677 EnterCriticalSection(&pdbi
->mmap_crst
);
1679 snd_pcm_mmap_begin(wwo
->p_handle
, &areas
, &ofs
, &frames
);
1680 snd_pcm_areas_copy(areas
, ofs
, pdbi
->mmap_areas
, ofs
, channels
, frames
, format
);
1681 err
= snd_pcm_mmap_commit(wwo
->p_handle
, ofs
, frames
);
1683 LeaveCriticalSection(&pdbi
->mmap_crst
);
1685 if ( err
!= (snd_pcm_sframes_t
) frames
)
1686 ERR("mmap partially failed.\n");
1688 avail
= snd_pcm_avail_update(wwo
->p_handle
);
1692 static void DSDB_PCMCallback(snd_async_handler_t
*ahandler
)
1694 /* snd_pcm_t * handle = snd_async_handler_get_pcm(ahandler); */
1695 IDsDriverBufferImpl
* pdbi
= snd_async_handler_get_callback_private(ahandler
);
1696 TRACE("callback called\n");
1697 DSDB_MMAPCopy(pdbi
);
1700 static int DSDB_CreateMMAP(IDsDriverBufferImpl
* pdbi
)
1702 WINE_WAVEOUT
* wwo
= &(WOutDev
[pdbi
->drv
->wDevID
]);
1703 snd_pcm_format_t format
= snd_pcm_hw_params_get_format(wwo
->hw_params
);
1704 snd_pcm_uframes_t frames
= snd_pcm_hw_params_get_buffer_size(wwo
->hw_params
);
1705 int channels
= snd_pcm_hw_params_get_channels(wwo
->hw_params
);
1706 unsigned int bits_per_sample
= snd_pcm_format_physical_width(format
);
1707 unsigned int bits_per_frame
= bits_per_sample
* channels
;
1708 snd_pcm_channel_area_t
* a
;
1713 ALSA_TraceParameters(wwo
->hw_params
, NULL
, FALSE
);
1715 TRACE("format=%s frames=%ld channels=%d bits_per_sample=%d bits_per_frame=%d\n",
1716 snd_pcm_format_name(format
), frames
, channels
, bits_per_sample
, bits_per_frame
);
1718 pdbi
->mmap_buflen_frames
= frames
;
1719 pdbi
->mmap_buflen_bytes
= snd_pcm_frames_to_bytes( wwo
->p_handle
, frames
);
1720 pdbi
->mmap_buffer
= HeapAlloc(GetProcessHeap(),0,pdbi
->mmap_buflen_bytes
);
1721 if (!pdbi
->mmap_buffer
)
1722 return DSERR_OUTOFMEMORY
;
1724 snd_pcm_format_set_silence(format
, pdbi
->mmap_buffer
, frames
);
1726 TRACE("created mmap buffer of %ld frames (%ld bytes) at %p\n",
1727 frames
, pdbi
->mmap_buflen_bytes
, pdbi
->mmap_buffer
);
1729 pdbi
->mmap_areas
= HeapAlloc(GetProcessHeap(),0,channels
*sizeof(snd_pcm_channel_area_t
));
1730 if (!pdbi
->mmap_areas
)
1731 return DSERR_OUTOFMEMORY
;
1733 a
= pdbi
->mmap_areas
;
1734 for (c
= 0; c
< channels
; c
++, a
++)
1736 a
->addr
= pdbi
->mmap_buffer
;
1737 a
->first
= bits_per_sample
* c
;
1738 a
->step
= bits_per_frame
;
1739 TRACE("Area %d: addr=%p first=%d step=%d\n", c
, a
->addr
, a
->first
, a
->step
);
1742 InitializeCriticalSection(&pdbi
->mmap_crst
);
1744 err
= snd_async_add_pcm_handler(&pdbi
->mmap_async_handler
, wwo
->p_handle
, DSDB_PCMCallback
, pdbi
);
1747 ERR("add_pcm_handler failed. reason: %s\n", snd_strerror(err
));
1748 return DSERR_GENERIC
;
1754 static void DSDB_DestroyMMAP(IDsDriverBufferImpl
* pdbi
)
1756 TRACE("mmap buffer %p destroyed\n", pdbi
->mmap_buffer
);
1757 HeapFree(GetProcessHeap(), 0, pdbi
->mmap_areas
);
1758 HeapFree(GetProcessHeap(), 0, pdbi
->mmap_buffer
);
1759 pdbi
->mmap_areas
= NULL
;
1760 pdbi
->mmap_buffer
= NULL
;
1761 DeleteCriticalSection(&pdbi
->mmap_crst
);
1765 static HRESULT WINAPI
IDsDriverBufferImpl_QueryInterface(PIDSDRIVERBUFFER iface
, REFIID riid
, LPVOID
*ppobj
)
1767 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
1768 FIXME("(): stub!\n");
1769 return DSERR_UNSUPPORTED
;
1772 static ULONG WINAPI
IDsDriverBufferImpl_AddRef(PIDSDRIVERBUFFER iface
)
1774 ICOM_THIS(IDsDriverBufferImpl
,iface
);
1775 TRACE("(%p)\n",iface
);
1779 static ULONG WINAPI
IDsDriverBufferImpl_Release(PIDSDRIVERBUFFER iface
)
1781 ICOM_THIS(IDsDriverBufferImpl
,iface
);
1782 TRACE("(%p)\n",iface
);
1785 if (This
== This
->drv
->primary
)
1786 This
->drv
->primary
= NULL
;
1787 DSDB_DestroyMMAP(This
);
1788 HeapFree(GetProcessHeap(), 0, This
);
1792 static HRESULT WINAPI
IDsDriverBufferImpl_Lock(PIDSDRIVERBUFFER iface
,
1793 LPVOID
*ppvAudio1
,LPDWORD pdwLen1
,
1794 LPVOID
*ppvAudio2
,LPDWORD pdwLen2
,
1795 DWORD dwWritePosition
,DWORD dwWriteLen
,
1798 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
1799 TRACE("(%p)\n",iface
);
1800 return DSERR_UNSUPPORTED
;
1803 static HRESULT WINAPI
IDsDriverBufferImpl_Unlock(PIDSDRIVERBUFFER iface
,
1804 LPVOID pvAudio1
,DWORD dwLen1
,
1805 LPVOID pvAudio2
,DWORD dwLen2
)
1807 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
1808 TRACE("(%p)\n",iface
);
1809 return DSERR_UNSUPPORTED
;
1812 static HRESULT WINAPI
IDsDriverBufferImpl_SetFormat(PIDSDRIVERBUFFER iface
,
1813 LPWAVEFORMATEX pwfx
)
1815 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
1816 TRACE("(%p,%p)\n",iface
,pwfx
);
1817 return DSERR_BUFFERLOST
;
1820 static HRESULT WINAPI
IDsDriverBufferImpl_SetFrequency(PIDSDRIVERBUFFER iface
, DWORD dwFreq
)
1822 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
1823 TRACE("(%p,%ld): stub\n",iface
,dwFreq
);
1824 return DSERR_UNSUPPORTED
;
1827 static HRESULT WINAPI
IDsDriverBufferImpl_SetVolumePan(PIDSDRIVERBUFFER iface
, PDSVOLUMEPAN pVolPan
)
1829 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
1830 FIXME("(%p,%p): stub!\n",iface
,pVolPan
);
1831 return DSERR_UNSUPPORTED
;
1834 static HRESULT WINAPI
IDsDriverBufferImpl_SetPosition(PIDSDRIVERBUFFER iface
, DWORD dwNewPos
)
1836 /* ICOM_THIS(IDsDriverImpl,iface); */
1837 TRACE("(%p,%ld): stub\n",iface
,dwNewPos
);
1838 return DSERR_UNSUPPORTED
;
1841 static HRESULT WINAPI
IDsDriverBufferImpl_GetPosition(PIDSDRIVERBUFFER iface
,
1842 LPDWORD lpdwPlay
, LPDWORD lpdwWrite
)
1844 ICOM_THIS(IDsDriverBufferImpl
,iface
);
1845 WINE_WAVEOUT
* wwo
= &(WOutDev
[This
->drv
->wDevID
]);
1846 snd_pcm_uframes_t hw_ptr
;
1847 snd_pcm_uframes_t period_size
= snd_pcm_hw_params_get_period_size(wwo
->hw_params
, 0);
1849 /** we need to track down buffer underruns */
1850 DSDB_CheckXRUN(This
);
1852 EnterCriticalSection(&This
->mmap_crst
);
1853 hw_ptr
= _snd_pcm_mmap_hw_ptr(wwo
->p_handle
);
1855 *lpdwPlay
= snd_pcm_frames_to_bytes(wwo
->p_handle
, hw_ptr
/ period_size
* period_size
) % This
->mmap_buflen_bytes
;
1857 *lpdwWrite
= snd_pcm_frames_to_bytes(wwo
->p_handle
, (hw_ptr
/ period_size
+ 1) * period_size
) % This
->mmap_buflen_bytes
;
1858 LeaveCriticalSection(&This
->mmap_crst
);
1860 TRACE("hw_ptr=0x%08x, playpos=%ld, writepos=%ld\n", (unsigned int)hw_ptr
, lpdwPlay
?*lpdwPlay
:-1, lpdwWrite
?*lpdwWrite
:-1);
1864 static HRESULT WINAPI
IDsDriverBufferImpl_Play(PIDSDRIVERBUFFER iface
, DWORD dwRes1
, DWORD dwRes2
, DWORD dwFlags
)
1866 ICOM_THIS(IDsDriverBufferImpl
,iface
);
1867 WINE_WAVEOUT
* wwo
= &(WOutDev
[This
->drv
->wDevID
]);
1868 snd_pcm_state_t state
;
1871 TRACE("(%p,%lx,%lx,%lx)\n",iface
,dwRes1
,dwRes2
,dwFlags
);
1873 state
= snd_pcm_state(wwo
->p_handle
);
1874 if ( state
== SND_PCM_STATE_SETUP
)
1876 err
= snd_pcm_prepare(wwo
->p_handle
);
1877 state
= snd_pcm_state(wwo
->p_handle
);
1879 if ( state
== SND_PCM_STATE_PREPARED
)
1881 DSDB_MMAPCopy(This
);
1882 err
= snd_pcm_start(wwo
->p_handle
);
1887 static HRESULT WINAPI
IDsDriverBufferImpl_Stop(PIDSDRIVERBUFFER iface
)
1889 ICOM_THIS(IDsDriverBufferImpl
,iface
);
1890 WINE_WAVEOUT
* wwo
= &(WOutDev
[This
->drv
->wDevID
]);
1895 TRACE("(%p)\n",iface
);
1897 /* ring buffer wrap up detection */
1898 IDsDriverBufferImpl_GetPosition(iface
, &play
, &write
);
1901 TRACE("writepos wrapper up\n");
1905 if ( ( err
= snd_pcm_drop(wwo
->p_handle
)) < 0 )
1907 ERR("error while stopping pcm: %s\n", snd_strerror(err
));
1908 return DSERR_GENERIC
;
1913 static ICOM_VTABLE(IDsDriverBuffer
) dsdbvt
=
1915 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1916 IDsDriverBufferImpl_QueryInterface
,
1917 IDsDriverBufferImpl_AddRef
,
1918 IDsDriverBufferImpl_Release
,
1919 IDsDriverBufferImpl_Lock
,
1920 IDsDriverBufferImpl_Unlock
,
1921 IDsDriverBufferImpl_SetFormat
,
1922 IDsDriverBufferImpl_SetFrequency
,
1923 IDsDriverBufferImpl_SetVolumePan
,
1924 IDsDriverBufferImpl_SetPosition
,
1925 IDsDriverBufferImpl_GetPosition
,
1926 IDsDriverBufferImpl_Play
,
1927 IDsDriverBufferImpl_Stop
1930 static HRESULT WINAPI
IDsDriverImpl_QueryInterface(PIDSDRIVER iface
, REFIID riid
, LPVOID
*ppobj
)
1932 /* ICOM_THIS(IDsDriverImpl,iface); */
1933 FIXME("(%p): stub!\n",iface
);
1934 return DSERR_UNSUPPORTED
;
1937 static ULONG WINAPI
IDsDriverImpl_AddRef(PIDSDRIVER iface
)
1939 ICOM_THIS(IDsDriverImpl
,iface
);
1940 TRACE("(%p)\n",iface
);
1945 static ULONG WINAPI
IDsDriverImpl_Release(PIDSDRIVER iface
)
1947 ICOM_THIS(IDsDriverImpl
,iface
);
1948 TRACE("(%p)\n",iface
);
1951 HeapFree(GetProcessHeap(),0,This
);
1955 static HRESULT WINAPI
IDsDriverImpl_GetDriverDesc(PIDSDRIVER iface
, PDSDRIVERDESC pDesc
)
1957 ICOM_THIS(IDsDriverImpl
,iface
);
1958 TRACE("(%p,%p)\n",iface
,pDesc
);
1959 memcpy(pDesc
, &(WOutDev
[This
->wDevID
].ds_desc
), sizeof(DSDRIVERDESC
));
1960 pDesc
->dwFlags
= DSDDESC_DOMMSYSTEMOPEN
| DSDDESC_DOMMSYSTEMSETFORMAT
|
1961 DSDDESC_USESYSTEMMEMORY
;
1962 pDesc
->dnDevNode
= WOutDev
[This
->wDevID
].waveDesc
.dnDevNode
;
1964 pDesc
->wReserved
= 0;
1965 pDesc
->ulDeviceNum
= This
->wDevID
;
1966 pDesc
->dwHeapType
= DSDHEAP_NOHEAP
;
1967 pDesc
->pvDirectDrawHeap
= NULL
;
1968 pDesc
->dwMemStartAddress
= 0;
1969 pDesc
->dwMemEndAddress
= 0;
1970 pDesc
->dwMemAllocExtra
= 0;
1971 pDesc
->pvReserved1
= NULL
;
1972 pDesc
->pvReserved2
= NULL
;
1976 static HRESULT WINAPI
IDsDriverImpl_Open(PIDSDRIVER iface
)
1978 /* ICOM_THIS(IDsDriverImpl,iface); */
1979 TRACE("(%p)\n",iface
);
1983 static HRESULT WINAPI
IDsDriverImpl_Close(PIDSDRIVER iface
)
1985 /* ICOM_THIS(IDsDriverImpl,iface); */
1986 TRACE("(%p)\n",iface
);
1990 static HRESULT WINAPI
IDsDriverImpl_GetCaps(PIDSDRIVER iface
, PDSDRIVERCAPS pCaps
)
1992 ICOM_THIS(IDsDriverImpl
,iface
);
1993 TRACE("(%p,%p)\n",iface
,pCaps
);
1994 memset(pCaps
, 0, sizeof(*pCaps
));
1996 pCaps
->dwFlags
= DSCAPS_PRIMARYMONO
;
1997 if ( WOutDev
[This
->wDevID
].caps
.wChannels
== 2 )
1998 pCaps
->dwFlags
|= DSCAPS_PRIMARYSTEREO
;
2000 if ( WOutDev
[This
->wDevID
].caps
.dwFormats
& (WAVE_FORMAT_1S08
| WAVE_FORMAT_2S08
| WAVE_FORMAT_4S08
) )
2001 pCaps
->dwFlags
|= DSCAPS_PRIMARY8BIT
;
2003 if ( WOutDev
[This
->wDevID
].caps
.dwFormats
& (WAVE_FORMAT_1S16
| WAVE_FORMAT_2S16
| WAVE_FORMAT_4S16
))
2004 pCaps
->dwFlags
|= DSCAPS_PRIMARY16BIT
;
2006 pCaps
->dwPrimaryBuffers
= 1;
2007 TRACE("caps=0x%X\n",(unsigned int)pCaps
->dwFlags
);
2009 /* the other fields only apply to secondary buffers, which we don't support
2010 * (unless we want to mess with wavetable synthesizers and MIDI) */
2014 static HRESULT WINAPI
IDsDriverImpl_CreateSoundBuffer(PIDSDRIVER iface
,
2015 LPWAVEFORMATEX pwfx
,
2016 DWORD dwFlags
, DWORD dwCardAddress
,
2017 LPDWORD pdwcbBufferSize
,
2021 ICOM_THIS(IDsDriverImpl
,iface
);
2022 IDsDriverBufferImpl
** ippdsdb
= (IDsDriverBufferImpl
**)ppvObj
;
2025 TRACE("(%p,%p,%lx,%lx)\n",iface
,pwfx
,dwFlags
,dwCardAddress
);
2026 /* we only support primary buffers */
2027 if (!(dwFlags
& DSBCAPS_PRIMARYBUFFER
))
2028 return DSERR_UNSUPPORTED
;
2030 return DSERR_ALLOCATED
;
2031 if (dwFlags
& (DSBCAPS_CTRLFREQUENCY
| DSBCAPS_CTRLPAN
))
2032 return DSERR_CONTROLUNAVAIL
;
2034 *ippdsdb
= (IDsDriverBufferImpl
*)HeapAlloc(GetProcessHeap(),0,sizeof(IDsDriverBufferImpl
));
2035 if (*ippdsdb
== NULL
)
2036 return DSERR_OUTOFMEMORY
;
2037 ICOM_VTBL(*ippdsdb
) = &dsdbvt
;
2038 (*ippdsdb
)->ref
= 1;
2039 (*ippdsdb
)->drv
= This
;
2041 err
= DSDB_CreateMMAP((*ippdsdb
));
2044 HeapFree(GetProcessHeap(), 0, *ippdsdb
);
2048 *ppbBuffer
= (*ippdsdb
)->mmap_buffer
;
2049 *pdwcbBufferSize
= (*ippdsdb
)->mmap_buflen_bytes
;
2051 This
->primary
= *ippdsdb
;
2053 /* buffer is ready to go */
2054 TRACE("buffer created at %p\n", *ippdsdb
);
2058 static HRESULT WINAPI
IDsDriverImpl_DuplicateSoundBuffer(PIDSDRIVER iface
,
2059 PIDSDRIVERBUFFER pBuffer
,
2062 /* ICOM_THIS(IDsDriverImpl,iface); */
2063 TRACE("(%p,%p): stub\n",iface
,pBuffer
);
2064 return DSERR_INVALIDCALL
;
2067 static ICOM_VTABLE(IDsDriver
) dsdvt
=
2069 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
2070 IDsDriverImpl_QueryInterface
,
2071 IDsDriverImpl_AddRef
,
2072 IDsDriverImpl_Release
,
2073 IDsDriverImpl_GetDriverDesc
,
2075 IDsDriverImpl_Close
,
2076 IDsDriverImpl_GetCaps
,
2077 IDsDriverImpl_CreateSoundBuffer
,
2078 IDsDriverImpl_DuplicateSoundBuffer
2081 static DWORD
wodDsCreate(UINT wDevID
, PIDSDRIVER
* drv
)
2083 IDsDriverImpl
** idrv
= (IDsDriverImpl
**)drv
;
2085 TRACE("driver created\n");
2087 /* the HAL isn't much better than the HEL if we can't do mmap() */
2088 if (!(WOutDev
[wDevID
].caps
.dwSupport
& WAVECAPS_DIRECTSOUND
)) {
2089 ERR("DirectSound flag not set\n");
2090 MESSAGE("This sound card's driver does not support direct access\n");
2091 MESSAGE("The (slower) DirectSound HEL mode will be used instead.\n");
2092 return MMSYSERR_NOTSUPPORTED
;
2095 *idrv
= (IDsDriverImpl
*)HeapAlloc(GetProcessHeap(),0,sizeof(IDsDriverImpl
));
2097 return MMSYSERR_NOMEM
;
2098 ICOM_VTBL(*idrv
) = &dsdvt
;
2101 (*idrv
)->wDevID
= wDevID
;
2102 (*idrv
)->primary
= NULL
;
2103 return MMSYSERR_NOERROR
;
2106 static DWORD
wodDsDesc(UINT wDevID
, PDSDRIVERDESC desc
)
2108 memcpy(desc
, &(WOutDev
[wDevID
].ds_desc
), sizeof(DSDRIVERDESC
));
2109 return MMSYSERR_NOERROR
;
2112 static DWORD
wodDsGuid(UINT wDevID
, LPGUID pGuid
)
2114 TRACE("(%d,%p)\n",wDevID
,pGuid
);
2116 memcpy(pGuid
, &(WOutDev
[wDevID
].ds_guid
), sizeof(GUID
));
2118 return MMSYSERR_NOERROR
;
2125 /**************************************************************************
2126 * wodMessage (WINEALSA.@)
2128 DWORD WINAPI
ALSA_wodMessage(WORD wDevID
, WORD wMsg
, DWORD dwUser
,
2129 DWORD dwParam1
, DWORD dwParam2
)
2131 FIXME("(%u, %04X, %08lX, %08lX, %08lX):stub\n", wDevID
, wMsg
, dwUser
, dwParam1
, dwParam2
);
2132 return MMSYSERR_NOTENABLED
;
2135 #endif /* HAVE_ALSA */