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"
37 #ifdef HAVE_SYS_IOCTL_H
38 # include <sys/ioctl.h>
40 #ifdef HAVE_SYS_MMAN_H
41 # include <sys/mman.h>
52 #include "wine/library.h"
53 #include "wine/debug.h"
55 WINE_DEFAULT_DEBUG_CHANNEL(wave
);
58 #if defined(HAVE_ALSA) && ((SND_LIB_MAJOR == 0 && SND_LIB_MINOR >= 9) || SND_LIB_MAJOR >= 1)
60 /* internal ALSALIB functions */
61 snd_pcm_uframes_t
_snd_pcm_mmap_hw_ptr(snd_pcm_t
*pcm
);
64 #define MAX_WAVEOUTDRV (1)
65 #define MAX_WAVEINDRV (1)
67 /* state diagram for waveOut writing:
69 * +---------+-------------+---------------+---------------------------------+
70 * | state | function | event | new state |
71 * +---------+-------------+---------------+---------------------------------+
72 * | | open() | | STOPPED |
73 * | PAUSED | write() | | PAUSED |
74 * | STOPPED | write() | <thrd create> | PLAYING |
75 * | PLAYING | write() | HEADER | PLAYING |
76 * | (other) | write() | <error> | |
77 * | (any) | pause() | PAUSING | PAUSED |
78 * | PAUSED | restart() | RESTARTING | PLAYING (if no thrd => STOPPED) |
79 * | (any) | reset() | RESETTING | STOPPED |
80 * | (any) | close() | CLOSING | CLOSED |
81 * +---------+-------------+---------------+---------------------------------+
84 /* states of the playing device */
85 #define WINE_WS_PLAYING 0
86 #define WINE_WS_PAUSED 1
87 #define WINE_WS_STOPPED 2
88 #define WINE_WS_CLOSED 3
90 /* events to be send to device */
92 WINE_WM_PAUSING
= WM_USER
+ 1, WINE_WM_RESTARTING
, WINE_WM_RESETTING
, WINE_WM_HEADER
,
93 WINE_WM_UPDATE
, WINE_WM_BREAKLOOP
, WINE_WM_CLOSING
97 enum win_wm_message msg
; /* message identifier */
98 DWORD param
; /* parameter for this message */
99 HANDLE hEvent
; /* if message is synchronous, handle of event for synchro */
102 /* implement an in-process message ring for better performance
103 * (compared to passing thru the server)
104 * this ring will be used by the input (resp output) record (resp playback) routine
106 #define ALSA_RING_BUFFER_INCREMENT 64
109 int ring_buffer_size
;
113 CRITICAL_SECTION msg_crst
;
117 /* Windows information */
118 volatile int state
; /* one of the WINE_WS_ manifest constants */
119 WAVEOPENDESC waveDesc
;
121 PCMWAVEFORMAT format
;
124 /* ALSA information (ALSA 0.9/1.x uses two different devices for playback/capture) */
126 snd_pcm_t
* p_handle
; /* handle to ALSA playback device */
127 snd_pcm_t
* c_handle
; /* handle to ALSA capture device */
128 snd_pcm_hw_params_t
* hw_params
; /* ALSA Hw params */
130 snd_ctl_t
* ctl
; /* control handle for the playback volume */
131 snd_ctl_elem_id_t
* playback_eid
; /* element id of the playback volume control */
132 snd_ctl_elem_value_t
* playback_evalue
; /* element value of the playback volume control */
133 snd_ctl_elem_info_t
* playback_einfo
; /* element info of the playback volume control */
135 snd_pcm_sframes_t (*write
)(snd_pcm_t
*, const void *, snd_pcm_uframes_t
);
140 DWORD dwBufferSize
; /* size of whole ALSA buffer in bytes */
141 LPWAVEHDR lpQueuePtr
; /* start of queued WAVEHDRs (waiting to be notified) */
142 LPWAVEHDR lpPlayPtr
; /* start of not yet fully played buffers */
144 LPWAVEHDR lpLoopPtr
; /* pointer of first buffer in loop, if any */
145 DWORD dwLoops
; /* private copy of loop counter */
149 /* synchronization stuff */
150 HANDLE hStartUpEvent
;
153 ALSA_MSG_RING msgRing
;
155 /* DirectSound stuff */
156 DSDRIVERDESC ds_desc
;
160 static WINE_WAVEOUT WOutDev
[MAX_WAVEOUTDRV
];
161 static DWORD ALSA_WodNumDevs
;
163 static DWORD
wodDsCreate(UINT wDevID
, PIDSDRIVER
* drv
);
164 static DWORD
wodDsDesc(UINT wDevID
, PDSDRIVERDESC desc
);
165 static DWORD
wodDsGuid(UINT wDevID
, LPGUID pGuid
);
167 /* These strings used only for tracing */
169 static const char *wodPlayerCmdString
[] = {
171 "WINE_WM_RESTARTING",
180 /*======================================================================*
181 * Low level WAVE implementation *
182 *======================================================================*/
184 /**************************************************************************
185 * ALSA_InitializeVolumeCtl [internal]
187 * used to initialize the PCM Volume Control
189 static int ALSA_InitializeVolumeCtl(WINE_WAVEOUT
* wwo
)
191 snd_ctl_t
* ctl
= NULL
;
192 snd_ctl_card_info_t
* cardinfo
;
193 snd_ctl_elem_list_t
* elemlist
;
194 snd_ctl_elem_id_t
* e_id
;
195 snd_ctl_elem_info_t
* einfo
;
196 snd_hctl_t
* hctl
= NULL
;
197 snd_hctl_elem_t
* elem
;
201 snd_ctl_card_info_alloca(&cardinfo
);
202 memset(cardinfo
,0,snd_ctl_card_info_sizeof());
204 snd_ctl_elem_list_alloca(&elemlist
);
205 memset(elemlist
,0,snd_ctl_elem_list_sizeof());
207 snd_ctl_elem_id_alloca(&e_id
);
208 memset(e_id
,0,snd_ctl_elem_id_sizeof());
210 snd_ctl_elem_info_alloca(&einfo
);
211 memset(einfo
,0,snd_ctl_elem_info_sizeof());
213 #define EXIT_ON_ERROR(f,txt) do \
216 if ( (err = (f) ) < 0) \
218 ERR(txt ": %s\n", snd_strerror(err)); \
220 snd_hctl_close(hctl); \
222 snd_ctl_close(ctl); \
227 EXIT_ON_ERROR( snd_ctl_open(&ctl
,"hw",0) , "ctl open failed" );
228 EXIT_ON_ERROR( snd_ctl_card_info(ctl
, cardinfo
), "card info failed");
229 EXIT_ON_ERROR( snd_ctl_elem_list(ctl
, elemlist
), "elem list failed");
231 nCtrls
= snd_ctl_elem_list_get_count(elemlist
);
233 EXIT_ON_ERROR( snd_hctl_open(&hctl
,"hw",0), "hctl open failed");
234 EXIT_ON_ERROR( snd_hctl_load(hctl
), "hctl load failed" );
236 elem
=snd_hctl_first_elem(hctl
);
237 for ( i
= 0; i
<nCtrls
; i
++) {
239 memset(e_id
,0,snd_ctl_elem_id_sizeof());
241 snd_hctl_elem_get_id(elem
,e_id
);
243 TRACE("ctl: #%d '%s'%d\n",
244 snd_ctl_elem_id_get_numid(e_id),
245 snd_ctl_elem_id_get_name(e_id),
246 snd_ctl_elem_id_get_index(e_id));
248 if ( !strcmp("PCM Playback Volume", snd_ctl_elem_id_get_name(e_id
)) )
250 EXIT_ON_ERROR( snd_hctl_elem_info(elem
,einfo
), "hctl elem info failed" );
252 /* few sanity checks... you'll never know... */
253 if ( snd_ctl_elem_info_get_type(einfo
) != SND_CTL_ELEM_TYPE_INTEGER
)
254 WARN("playback volume control is not an integer\n");
255 if ( !snd_ctl_elem_info_is_readable(einfo
) )
256 WARN("playback volume control is readable\n");
257 if ( !snd_ctl_elem_info_is_writable(einfo
) )
258 WARN("playback volume control is readable\n");
260 TRACE(" ctrl range: min=%ld max=%ld step=%ld\n",
261 snd_ctl_elem_info_get_min(einfo
),
262 snd_ctl_elem_info_get_max(einfo
),
263 snd_ctl_elem_info_get_step(einfo
));
265 EXIT_ON_ERROR( snd_ctl_elem_id_malloc(&wwo
->playback_eid
), "elem id malloc failed" );
266 EXIT_ON_ERROR( snd_ctl_elem_info_malloc(&wwo
->playback_einfo
), "elem info malloc failed" );
267 EXIT_ON_ERROR( snd_ctl_elem_value_malloc(&wwo
->playback_evalue
), "elem value malloc failed" );
269 /* ok, now we can safely save these objects for later */
270 snd_ctl_elem_id_copy(wwo
->playback_eid
, e_id
);
271 snd_ctl_elem_info_copy(wwo
->playback_einfo
, einfo
);
272 snd_ctl_elem_value_set_id(wwo
->playback_evalue
, wwo
->playback_eid
);
276 elem
=snd_hctl_elem_next(elem
);
278 snd_hctl_close(hctl
);
283 /**************************************************************************
284 * ALSA_XRUNRecovery [internal]
286 * used to recovery from XRUN errors (buffer underflow/overflow)
288 static int ALSA_XRUNRecovery(WINE_WAVEOUT
* wwo
, int err
)
290 if (err
== -EPIPE
) { /* under-run */
291 err
= snd_pcm_prepare(wwo
->p_handle
);
293 ERR( "underrun recovery failed. prepare failed: %s\n", snd_strerror(err
));
295 } else if (err
== -ESTRPIPE
) {
296 while ((err
= snd_pcm_resume(wwo
->p_handle
)) == -EAGAIN
)
297 sleep(1); /* wait until the suspend flag is released */
299 err
= snd_pcm_prepare(wwo
->p_handle
);
301 ERR("recovery from suspend failed, prepare failed: %s\n", snd_strerror(err
));
308 /**************************************************************************
309 * ALSA_TraceParameters [internal]
311 * used to trace format changes, hw and sw parameters
313 static void ALSA_TraceParameters(snd_pcm_hw_params_t
* hw_params
, snd_pcm_sw_params_t
* sw
, int full
)
315 snd_pcm_format_t format
= snd_pcm_hw_params_get_format(hw_params
);
316 snd_pcm_access_t access
= snd_pcm_hw_params_get_access(hw_params
);
318 #define X(x) ((x)? "true" : "false")
320 TRACE("FLAGS: sampleres=%s overrng=%s pause=%s resume=%s syncstart=%s batch=%s block=%s double=%s "
321 "halfd=%s joint=%s \n",
322 X(snd_pcm_hw_params_can_mmap_sample_resolution(hw_params
)),
323 X(snd_pcm_hw_params_can_overrange(hw_params
)),
324 X(snd_pcm_hw_params_can_pause(hw_params
)),
325 X(snd_pcm_hw_params_can_resume(hw_params
)),
326 X(snd_pcm_hw_params_can_sync_start(hw_params
)),
327 X(snd_pcm_hw_params_is_batch(hw_params
)),
328 X(snd_pcm_hw_params_is_block_transfer(hw_params
)),
329 X(snd_pcm_hw_params_is_double(hw_params
)),
330 X(snd_pcm_hw_params_is_half_duplex(hw_params
)),
331 X(snd_pcm_hw_params_is_joint_duplex(hw_params
)));
335 TRACE("access=%s\n", snd_pcm_access_name(access
));
338 snd_pcm_access_mask_t
* acmask
;
339 snd_pcm_access_mask_alloca(&acmask
);
340 snd_pcm_hw_params_get_access_mask(hw_params
, acmask
);
341 for ( access
= SND_PCM_ACCESS_MMAP_INTERLEAVED
; access
<= SND_PCM_ACCESS_LAST
; access
++)
342 if (snd_pcm_access_mask_test(acmask
, access
))
343 TRACE("access=%s\n", snd_pcm_access_name(access
));
348 TRACE("format=%s\n", snd_pcm_format_name(format
));
353 snd_pcm_format_mask_t
* fmask
;
355 snd_pcm_format_mask_alloca(&fmask
);
356 snd_pcm_hw_params_get_format_mask(hw_params
, fmask
);
357 for ( format
= SND_PCM_FORMAT_S8
; format
<= SND_PCM_FORMAT_LAST
; format
++)
358 if ( snd_pcm_format_mask_test(fmask
, format
) )
359 TRACE("format=%s\n", snd_pcm_format_name(format
));
363 int n = snd_pcm_hw_params_get_##x(hw_params); \
365 TRACE(#x "_min=%ld " #x "_max=%ld\n", \
366 (long int)snd_pcm_hw_params_get_##x##_min(hw_params), \
367 (long int)snd_pcm_hw_params_get_##x##_max(hw_params)); \
369 TRACE(#x "=%d\n", n); \
376 int n = snd_pcm_hw_params_get_##x(hw_params,0); \
378 TRACE(#x "_min=%ld " #x "_max=%ld\n", \
379 (long int)snd_pcm_hw_params_get_##x##_min(hw_params,0), \
380 (long int)snd_pcm_hw_params_get_##x##_max(hw_params,0)); \
382 TRACE(#x "=%d\n", n); \
400 /******************************************************************
403 * Initialize internal structures from ALSA information
405 LONG
ALSA_WaveInit(void)
408 snd_pcm_info_t
* info
;
409 snd_pcm_hw_params_t
* hw_params
;
411 char device
[] = "hw";
415 /* FIXME: use better values */
416 wwo
->device
= device
;
417 wwo
->caps
.wMid
= 0x0002;
418 wwo
->caps
.wPid
= 0x0104;
419 strcpy(wwo
->caps
.szPname
, "SB16 Wave Out");
420 wwo
->caps
.vDriverVersion
= 0x0100;
421 wwo
->caps
.dwFormats
= 0x00000000;
422 wwo
->caps
.dwSupport
= WAVECAPS_VOLUME
;
423 strcpy(wwo
->ds_desc
.szDesc
, "WineALSA DirectSound Driver");
424 strcpy(wwo
->ds_desc
.szDrvName
, "winealsa.drv");
425 wwo
->ds_guid
= DSDEVID_DefaultPlayback
;
427 if (!wine_dlopen("libasound.so.2", RTLD_LAZY
|RTLD_GLOBAL
, NULL
, 0))
429 ERR("Error: ALSA lib needs to be loaded with flags RTLD_LAZY and RTLD_GLOBAL.\n");
433 snd_pcm_info_alloca(&info
);
434 snd_pcm_hw_params_alloca(&hw_params
);
436 #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)
439 EXIT_ON_ERROR( snd_pcm_open(&h
, wwo
->device
, SND_PCM_STREAM_PLAYBACK
, SND_PCM_NONBLOCK
) , "open pcm" );
443 EXIT_ON_ERROR( snd_pcm_info(h
, info
) , "pcm info" );
445 TRACE("dev=%d id=%s name=%s subdev=%d subdev_name=%s subdev_avail=%d subdev_num=%d stream=%s subclass=%s \n",
446 snd_pcm_info_get_device(info
),
447 snd_pcm_info_get_id(info
),
448 snd_pcm_info_get_name(info
),
449 snd_pcm_info_get_subdevice(info
),
450 snd_pcm_info_get_subdevice_name(info
),
451 snd_pcm_info_get_subdevices_avail(info
),
452 snd_pcm_info_get_subdevices_count(info
),
453 snd_pcm_stream_name(snd_pcm_info_get_stream(info
)),
454 (snd_pcm_info_get_subclass(info
) == SND_PCM_SUBCLASS_GENERIC_MIX
? "GENERIC MIX": "MULTI MIX"));
456 EXIT_ON_ERROR( snd_pcm_hw_params_any(h
, hw_params
) , "pcm hw params" );
460 ALSA_TraceParameters(hw_params
, NULL
, TRUE
);
463 snd_pcm_format_mask_t
* fmask
;
464 int ratemin
= snd_pcm_hw_params_get_rate_min(hw_params
, 0);
465 int ratemax
= snd_pcm_hw_params_get_rate_max(hw_params
, 0);
466 int chmin
= snd_pcm_hw_params_get_channels_min(hw_params
); \
467 int chmax
= snd_pcm_hw_params_get_channels_max(hw_params
); \
469 snd_pcm_format_mask_alloca(&fmask
);
470 snd_pcm_hw_params_get_format_mask(hw_params
, fmask
);
473 if ( (r) >= ratemin && ( (r) <= ratemax || ratemax == -1) ) \
475 if (snd_pcm_format_mask_test( fmask, SND_PCM_FORMAT_U8)) \
477 if (chmin <= 1 && 1 <= chmax) \
478 wwo->caps.dwFormats |= WAVE_FORMAT_##v##S08; \
479 if (chmin <= 2 && 2 <= chmax) \
480 wwo->caps.dwFormats |= WAVE_FORMAT_##v##S08; \
482 if (snd_pcm_format_mask_test( fmask, SND_PCM_FORMAT_S16_LE)) \
484 if (chmin <= 1 && 1 <= chmax) \
485 wwo->caps.dwFormats |= WAVE_FORMAT_##v##S16; \
486 if (chmin <= 2 && 2 <= chmax) \
487 wwo->caps.dwFormats |= WAVE_FORMAT_##v##S16; \
496 if ( snd_pcm_hw_params_get_channels_min(hw_params
) > 1) FIXME("-\n");
497 wwo
->caps
.wChannels
= (snd_pcm_hw_params_get_channels_max(hw_params
) >= 2) ? 2 : 1;
498 if (snd_pcm_hw_params_get_channels_min(hw_params
) <= 2 && 2 <= snd_pcm_hw_params_get_channels_max(hw_params
))
499 wwo
->caps
.dwSupport
|= WAVECAPS_LRVOLUME
;
501 /* FIXME: always true ? */
502 wwo
->caps
.dwSupport
|= WAVECAPS_SAMPLEACCURATE
;
505 snd_pcm_access_mask_t
* acmask
;
506 snd_pcm_access_mask_alloca(&acmask
);
507 snd_pcm_hw_params_get_access_mask(hw_params
, acmask
);
509 /* FIXME: NONITERLEAVED and COMPLEX are not supported right now */
510 if ( snd_pcm_access_mask_test( acmask
, SND_PCM_ACCESS_MMAP_INTERLEAVED
) )
511 wwo
->caps
.dwSupport
|= WAVECAPS_DIRECTSOUND
;
514 TRACE("Configured with dwFmts=%08lx dwSupport=%08lx\n",
515 wwo
->caps
.dwFormats
, wwo
->caps
.dwSupport
);
519 ALSA_InitializeVolumeCtl(wwo
);
524 /******************************************************************
525 * ALSA_InitRingMessage
527 * Initialize the ring of messages for passing between driver's caller and playback/record
530 static int ALSA_InitRingMessage(ALSA_MSG_RING
* omr
)
534 omr
->msg_event
= CreateEventA(NULL
, FALSE
, FALSE
, NULL
);
535 omr
->ring_buffer_size
= ALSA_RING_BUFFER_INCREMENT
;
536 omr
->messages
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,omr
->ring_buffer_size
* sizeof(ALSA_MSG
));
538 InitializeCriticalSection(&omr
->msg_crst
);
542 /******************************************************************
543 * ALSA_DestroyRingMessage
546 static int ALSA_DestroyRingMessage(ALSA_MSG_RING
* omr
)
548 CloseHandle(omr
->msg_event
);
549 HeapFree(GetProcessHeap(),0,omr
->messages
);
550 DeleteCriticalSection(&omr
->msg_crst
);
554 /******************************************************************
555 * ALSA_AddRingMessage
557 * Inserts a new message into the ring (should be called from DriverProc derivated routines)
559 static int ALSA_AddRingMessage(ALSA_MSG_RING
* omr
, enum win_wm_message msg
, DWORD param
, BOOL wait
)
561 HANDLE hEvent
= INVALID_HANDLE_VALUE
;
563 EnterCriticalSection(&omr
->msg_crst
);
564 if ((omr
->msg_toget
== ((omr
->msg_tosave
+ 1) % omr
->ring_buffer_size
)))
566 omr
->ring_buffer_size
+= ALSA_RING_BUFFER_INCREMENT
;
567 TRACE("omr->ring_buffer_size=%d\n",omr
->ring_buffer_size
);
568 omr
->messages
= HeapReAlloc(GetProcessHeap(),0,omr
->messages
, omr
->ring_buffer_size
* sizeof(ALSA_MSG
));
572 hEvent
= CreateEventA(NULL
, FALSE
, FALSE
, NULL
);
573 if (hEvent
== INVALID_HANDLE_VALUE
)
575 ERR("can't create event !?\n");
576 LeaveCriticalSection(&omr
->msg_crst
);
579 if (omr
->msg_toget
!= omr
->msg_tosave
&& omr
->messages
[omr
->msg_toget
].msg
!= WINE_WM_HEADER
)
580 FIXME("two fast messages in the queue!!!!\n");
582 /* fast messages have to be added at the start of the queue */
583 omr
->msg_toget
= (omr
->msg_toget
+ omr
->ring_buffer_size
- 1) % omr
->ring_buffer_size
;
585 omr
->messages
[omr
->msg_toget
].msg
= msg
;
586 omr
->messages
[omr
->msg_toget
].param
= param
;
587 omr
->messages
[omr
->msg_toget
].hEvent
= hEvent
;
591 omr
->messages
[omr
->msg_tosave
].msg
= msg
;
592 omr
->messages
[omr
->msg_tosave
].param
= param
;
593 omr
->messages
[omr
->msg_tosave
].hEvent
= INVALID_HANDLE_VALUE
;
594 omr
->msg_tosave
= (omr
->msg_tosave
+ 1) % omr
->ring_buffer_size
;
596 LeaveCriticalSection(&omr
->msg_crst
);
597 /* signal a new message */
598 SetEvent(omr
->msg_event
);
601 /* wait for playback/record thread to have processed the message */
602 WaitForSingleObject(hEvent
, INFINITE
);
608 /******************************************************************
609 * ALSA_RetrieveRingMessage
611 * Get a message from the ring. Should be called by the playback/record thread.
613 static int ALSA_RetrieveRingMessage(ALSA_MSG_RING
* omr
,
614 enum win_wm_message
*msg
, DWORD
*param
, HANDLE
*hEvent
)
616 EnterCriticalSection(&omr
->msg_crst
);
618 if (omr
->msg_toget
== omr
->msg_tosave
) /* buffer empty ? */
620 LeaveCriticalSection(&omr
->msg_crst
);
624 *msg
= omr
->messages
[omr
->msg_toget
].msg
;
625 omr
->messages
[omr
->msg_toget
].msg
= 0;
626 *param
= omr
->messages
[omr
->msg_toget
].param
;
627 *hEvent
= omr
->messages
[omr
->msg_toget
].hEvent
;
628 omr
->msg_toget
= (omr
->msg_toget
+ 1) % omr
->ring_buffer_size
;
629 LeaveCriticalSection(&omr
->msg_crst
);
633 /*======================================================================*
634 * Low level WAVE OUT implementation *
635 *======================================================================*/
638 /**************************************************************************
639 * wodNotifyClient [internal]
641 static DWORD
wodNotifyClient(WINE_WAVEOUT
* wwo
, WORD wMsg
, DWORD dwParam1
, DWORD dwParam2
)
643 TRACE("wMsg = 0x%04x dwParm1 = %04lX dwParam2 = %04lX\n", wMsg
, dwParam1
, dwParam2
);
649 if (wwo
->wFlags
!= DCB_NULL
&&
650 !DriverCallback(wwo
->waveDesc
.dwCallback
, wwo
->wFlags
, (HDRVR
)wwo
->waveDesc
.hWave
,
651 wMsg
, wwo
->waveDesc
.dwInstance
, dwParam1
, dwParam2
)) {
652 WARN("can't notify client !\n");
653 return MMSYSERR_ERROR
;
657 FIXME("Unknown callback message %u\n", wMsg
);
658 return MMSYSERR_INVALPARAM
;
660 return MMSYSERR_NOERROR
;
663 /**************************************************************************
664 * wodUpdatePlayedTotal [internal]
667 static BOOL
wodUpdatePlayedTotal(WINE_WAVEOUT
* wwo
, snd_pcm_status_t
* ps
)
672 /**************************************************************************
673 * wodPlayer_BeginWaveHdr [internal]
675 * Makes the specified lpWaveHdr the currently playing wave header.
676 * If the specified wave header is a begin loop and we're not already in
677 * a loop, setup the loop.
679 static void wodPlayer_BeginWaveHdr(WINE_WAVEOUT
* wwo
, LPWAVEHDR lpWaveHdr
)
681 wwo
->lpPlayPtr
= lpWaveHdr
;
683 if (!lpWaveHdr
) return;
685 wwo
->lpPlayPtr
->reserved
= 0;
687 if (lpWaveHdr
->dwFlags
& WHDR_BEGINLOOP
) {
688 if (wwo
->lpLoopPtr
) {
689 WARN("Already in a loop. Discarding loop on this header (%p)\n", lpWaveHdr
);
691 TRACE("Starting loop (%ldx) with %p\n", lpWaveHdr
->dwLoops
, lpWaveHdr
);
692 wwo
->lpLoopPtr
= lpWaveHdr
;
693 /* Windows does not touch WAVEHDR.dwLoops,
694 * so we need to make an internal copy */
695 wwo
->dwLoops
= lpWaveHdr
->dwLoops
;
700 /**************************************************************************
701 * wodPlayer_PlayPtrNext [internal]
703 * Advance the play pointer to the next waveheader, looping if required.
705 static LPWAVEHDR
wodPlayer_PlayPtrNext(WINE_WAVEOUT
* wwo
)
707 LPWAVEHDR lpWaveHdr
= wwo
->lpPlayPtr
;
709 if ((lpWaveHdr
->dwFlags
& WHDR_ENDLOOP
) && wwo
->lpLoopPtr
) {
710 /* We're at the end of a loop, loop if required */
711 if (--wwo
->dwLoops
> 0) {
712 wwo
->lpPlayPtr
= wwo
->lpLoopPtr
;
713 wwo
->lpPlayPtr
->reserved
= 0;
715 /* Handle overlapping loops correctly */
716 if (wwo
->lpLoopPtr
!= lpWaveHdr
&& (lpWaveHdr
->dwFlags
& WHDR_BEGINLOOP
)) {
717 FIXME("Correctly handled case ? (ending loop buffer also starts a new loop)\n");
718 /* shall we consider the END flag for the closing loop or for
719 * the opening one or for both ???
720 * code assumes for closing loop only
723 lpWaveHdr
= lpWaveHdr
->lpNext
;
725 wwo
->lpLoopPtr
= NULL
;
726 wodPlayer_BeginWaveHdr(wwo
, lpWaveHdr
);
729 /* We're not in a loop. Advance to the next wave header */
730 wodPlayer_BeginWaveHdr(wwo
, lpWaveHdr
= lpWaveHdr
->lpNext
);
736 /**************************************************************************
737 * wodPlayer_DSPWait [internal]
738 * Returns the number of milliseconds to wait for the DSP buffer to play a
741 static DWORD
wodPlayer_DSPWait(const WINE_WAVEOUT
*wwo
)
743 /* time for one period to be played */
744 return snd_pcm_hw_params_get_period_time(wwo
->hw_params
, 0) / 1000;
747 /**************************************************************************
748 * wodPllayer_NotifyWait [internal]
749 * Returns the number of milliseconds to wait before attempting to notify
750 * completion of the specified wavehdr.
751 * This is based on the number of bytes remaining to be written in the
754 static DWORD
wodPlayer_NotifyWait(const WINE_WAVEOUT
* wwo
, LPWAVEHDR lpWaveHdr
)
758 dwMillis
= (lpWaveHdr
->dwBufferLength
- lpWaveHdr
->reserved
) * 1000 / wwo
->format
.wf
.nAvgBytesPerSec
;
759 if (!dwMillis
) dwMillis
= 1;
765 /**************************************************************************
766 * wodPlayer_WriteMaxFrags [internal]
767 * Writes the maximum number of frames possible to the DSP and returns
768 * the number of frames written.
770 static int wodPlayer_WriteMaxFrags(WINE_WAVEOUT
* wwo
, DWORD
* frames
)
772 /* Only attempt to write to free frames */
773 LPWAVEHDR lpWaveHdr
= wwo
->lpPlayPtr
;
774 DWORD dwLength
= snd_pcm_bytes_to_frames(wwo
->p_handle
, lpWaveHdr
->dwBufferLength
- lpWaveHdr
->reserved
);
775 int toWrite
= min(dwLength
, *frames
);
778 TRACE("Writing wavehdr %p.%lu[%lu]\n", lpWaveHdr
, lpWaveHdr
->reserved
, lpWaveHdr
->dwBufferLength
);
780 written
= (wwo
->write
)(wwo
->p_handle
, lpWaveHdr
->lpData
+ lpWaveHdr
->reserved
, toWrite
);
783 /* XRUN occurred. let's try to recover */
784 ALSA_XRUNRecovery(wwo
, written
);
785 written
= (wwo
->write
)(wwo
->p_handle
, lpWaveHdr
->lpData
+ lpWaveHdr
->reserved
, toWrite
);
790 ERR("Error in writing wavehdr. Reason: %s\n", snd_strerror(written
));
794 lpWaveHdr
->reserved
+= snd_pcm_frames_to_bytes(wwo
->p_handle
, written
);
795 if ( lpWaveHdr
->reserved
>= lpWaveHdr
->dwBufferLength
) {
796 /* this will be used to check if the given wave header has been fully played or not... */
797 lpWaveHdr
->reserved
= lpWaveHdr
->dwBufferLength
;
798 /* If we wrote all current wavehdr, skip to the next one */
799 wodPlayer_PlayPtrNext(wwo
);
802 wwo
->dwPlayedTotal
+= snd_pcm_frames_to_bytes(wwo
->p_handle
, written
);
808 /**************************************************************************
809 * wodPlayer_NotifyCompletions [internal]
811 * Notifies and remove from queue all wavehdrs which have been played to
812 * the speaker (ie. they have cleared the ALSA buffer). If force is true,
813 * we notify all wavehdrs and remove them all from the queue even if they
814 * are unplayed or part of a loop.
816 static DWORD
wodPlayer_NotifyCompletions(WINE_WAVEOUT
* wwo
, BOOL force
)
820 /* Start from lpQueuePtr and keep notifying until:
821 * - we hit an unwritten wavehdr
822 * - we hit the beginning of a running loop
823 * - we hit a wavehdr which hasn't finished playing
825 while ((lpWaveHdr
= wwo
->lpQueuePtr
) &&
827 (lpWaveHdr
!= wwo
->lpPlayPtr
&&
828 lpWaveHdr
!= wwo
->lpLoopPtr
&&
829 lpWaveHdr
->reserved
== lpWaveHdr
->dwBufferLength
))) {
831 wwo
->lpQueuePtr
= lpWaveHdr
->lpNext
;
833 lpWaveHdr
->dwFlags
&= ~WHDR_INQUEUE
;
834 lpWaveHdr
->dwFlags
|= WHDR_DONE
;
836 wodNotifyClient(wwo
, WOM_DONE
, (DWORD
)lpWaveHdr
, 0);
838 return (lpWaveHdr
&& lpWaveHdr
!= wwo
->lpPlayPtr
&& lpWaveHdr
!= wwo
->lpLoopPtr
) ?
839 wodPlayer_NotifyWait(wwo
, lpWaveHdr
) : INFINITE
;
843 void wait_for_poll(snd_pcm_t
*handle
, struct pollfd
*ufds
, unsigned int count
)
845 unsigned short revents
;
848 poll(ufds
, count
, -1);
849 snd_pcm_poll_descriptors_revents(handle
, ufds
, count
, &revents
);
851 if (revents
& POLLERR
)
854 /*if (revents & POLLOUT)
860 /**************************************************************************
861 * wodPlayer_Reset [internal]
863 * wodPlayer helper. Resets current output stream.
865 static void wodPlayer_Reset(WINE_WAVEOUT
* wwo
)
867 enum win_wm_message msg
;
872 /* flush all possible output */
873 wait_for_poll(wwo
->p_handle
, wwo
->ufds
, wwo
->count
);
875 /* updates current notify list */
876 wodPlayer_NotifyCompletions(wwo
, FALSE
);
878 if ( (err
= snd_pcm_drop(wwo
->p_handle
)) < 0) {
879 FIXME("flush: %s\n", snd_strerror(err
));
881 wwo
->state
= WINE_WS_STOPPED
;
884 if ( (err
= snd_pcm_prepare(wwo
->p_handle
)) < 0 )
885 ERR("pcm prepare failed: %s\n", snd_strerror(err
));
887 /* remove any buffer */
888 wodPlayer_NotifyCompletions(wwo
, TRUE
);
890 wwo
->lpPlayPtr
= wwo
->lpQueuePtr
= wwo
->lpLoopPtr
= NULL
;
891 wwo
->state
= WINE_WS_STOPPED
;
893 /* remove any existing message in the ring */
894 EnterCriticalSection(&wwo
->msgRing
.msg_crst
);
895 /* return all pending headers in queue */
896 while (ALSA_RetrieveRingMessage(&wwo
->msgRing
, &msg
, ¶m
, &ev
))
898 if (msg
!= WINE_WM_HEADER
)
900 FIXME("shouldn't have headers left\n");
904 ((LPWAVEHDR
)param
)->dwFlags
&= ~WHDR_INQUEUE
;
905 ((LPWAVEHDR
)param
)->dwFlags
|= WHDR_DONE
;
907 wodNotifyClient(wwo
, WOM_DONE
, param
, 0);
909 ResetEvent(wwo
->msgRing
.msg_event
);
910 LeaveCriticalSection(&wwo
->msgRing
.msg_crst
);
913 /**************************************************************************
914 * wodPlayer_ProcessMessages [internal]
916 static void wodPlayer_ProcessMessages(WINE_WAVEOUT
* wwo
)
919 enum win_wm_message msg
;
924 while (ALSA_RetrieveRingMessage(&wwo
->msgRing
, &msg
, ¶m
, &ev
)) {
925 /* TRACE("Received %s %lx\n", wodPlayerCmdString[msg - WM_USER - 1], param); */
928 case WINE_WM_PAUSING
:
929 if ( snd_pcm_state(wwo
->p_handle
) == SND_PCM_STATE_RUNNING
)
931 err
= snd_pcm_pause(wwo
->p_handle
, 1);
933 ERR("pcm_pause failed: %s\n", snd_strerror(err
));
935 wwo
->state
= WINE_WS_PAUSED
;
938 case WINE_WM_RESTARTING
:
939 if (wwo
->state
== WINE_WS_PAUSED
)
941 if ( snd_pcm_state(wwo
->p_handle
) == SND_PCM_STATE_PAUSED
)
943 err
= snd_pcm_pause(wwo
->p_handle
, 0);
945 ERR("pcm_pause failed: %s\n", snd_strerror(err
));
947 wwo
->state
= WINE_WS_PLAYING
;
952 lpWaveHdr
= (LPWAVEHDR
)param
;
954 /* insert buffer at the end of queue */
957 for (wh
= &(wwo
->lpQueuePtr
); *wh
; wh
= &((*wh
)->lpNext
));
961 wodPlayer_BeginWaveHdr(wwo
,lpWaveHdr
);
962 if (wwo
->state
== WINE_WS_STOPPED
)
963 wwo
->state
= WINE_WS_PLAYING
;
965 case WINE_WM_RESETTING
:
966 wodPlayer_Reset(wwo
);
970 wodUpdatePlayedTotal(wwo
, NULL
);
973 case WINE_WM_BREAKLOOP
:
974 if (wwo
->state
== WINE_WS_PLAYING
&& wwo
->lpLoopPtr
!= NULL
) {
975 /* ensure exit at end of current loop */
980 case WINE_WM_CLOSING
:
981 /* sanity check: this should not happen since the device must have been reset before */
982 if (wwo
->lpQueuePtr
|| wwo
->lpPlayPtr
) ERR("out of sync\n");
984 wwo
->state
= WINE_WS_CLOSED
;
987 /* shouldn't go here */
989 FIXME("unknown message %d\n", msg
);
995 /**************************************************************************
996 * wodPlayer_FeedDSP [internal]
997 * Feed as much sound data as we can into the DSP and return the number of
998 * milliseconds before it will be necessary to feed the DSP again.
1000 static DWORD
wodPlayer_FeedDSP(WINE_WAVEOUT
* wwo
)
1002 DWORD availInQ
= snd_pcm_avail_update(wwo
->p_handle
);
1004 /* no more room... no need to try to feed */
1005 while (wwo
->lpPlayPtr
&& availInQ
> 0)
1006 if ( wodPlayer_WriteMaxFrags(wwo
, &availInQ
) < 0 )
1009 return wodPlayer_DSPWait(wwo
);
1012 /**************************************************************************
1013 * wodPlayer [internal]
1015 static DWORD CALLBACK
wodPlayer(LPVOID pmt
)
1017 WORD uDevID
= (DWORD
)pmt
;
1018 WINE_WAVEOUT
* wwo
= (WINE_WAVEOUT
*)&WOutDev
[uDevID
];
1019 DWORD dwNextFeedTime
= INFINITE
; /* Time before DSP needs feeding */
1020 DWORD dwNextNotifyTime
= INFINITE
; /* Time before next wave completion */
1023 wwo
->state
= WINE_WS_STOPPED
;
1024 SetEvent(wwo
->hStartUpEvent
);
1027 /** Wait for the shortest time before an action is required. If there
1028 * are no pending actions, wait forever for a command.
1030 dwSleepTime
= min(dwNextFeedTime
, dwNextNotifyTime
);
1031 TRACE("waiting %lums (%lu,%lu)\n", dwSleepTime
, dwNextFeedTime
, dwNextNotifyTime
);
1032 WaitForSingleObject(wwo
->msgRing
.msg_event
, dwSleepTime
);
1033 wodPlayer_ProcessMessages(wwo
);
1034 if (wwo
->state
== WINE_WS_PLAYING
) {
1035 dwNextFeedTime
= wodPlayer_FeedDSP(wwo
);
1036 dwNextNotifyTime
= wodPlayer_NotifyCompletions(wwo
, FALSE
);
1038 dwNextFeedTime
= dwNextNotifyTime
= INFINITE
;
1043 /**************************************************************************
1044 * wodGetDevCaps [internal]
1046 static DWORD
wodGetDevCaps(WORD wDevID
, LPWAVEOUTCAPSA lpCaps
, DWORD dwSize
)
1048 TRACE("(%u, %p, %lu);\n", wDevID
, lpCaps
, dwSize
);
1050 if (lpCaps
== NULL
) return MMSYSERR_NOTENABLED
;
1052 if (wDevID
>= MAX_WAVEOUTDRV
) {
1053 TRACE("MAX_WAVOUTDRV reached !\n");
1054 return MMSYSERR_BADDEVICEID
;
1057 memcpy(lpCaps
, &WOutDev
[wDevID
].caps
, min(dwSize
, sizeof(*lpCaps
)));
1058 return MMSYSERR_NOERROR
;
1061 /**************************************************************************
1062 * wodOpen [internal]
1064 static DWORD
wodOpen(WORD wDevID
, LPWAVEOPENDESC lpDesc
, DWORD dwFlags
)
1067 snd_pcm_hw_params_t
* hw_params
;
1068 snd_pcm_sw_params_t
* sw_params
;
1069 snd_pcm_access_t access
;
1070 snd_pcm_format_t format
;
1072 unsigned int buffer_time
= 500000;
1073 unsigned int period_time
= 10000;
1075 snd_pcm_uframes_t period_size
;
1080 snd_pcm_hw_params_alloca(&hw_params
);
1081 snd_pcm_sw_params_alloca(&sw_params
);
1083 TRACE("(%u, %p, %08lX);\n", wDevID
, lpDesc
, dwFlags
);
1084 if (lpDesc
== NULL
) {
1085 WARN("Invalid Parameter !\n");
1086 return MMSYSERR_INVALPARAM
;
1088 if (wDevID
>= MAX_WAVEOUTDRV
) {
1089 TRACE("MAX_WAVOUTDRV reached !\n");
1090 return MMSYSERR_BADDEVICEID
;
1093 /* only PCM format is supported so far... */
1094 if (lpDesc
->lpFormat
->wFormatTag
!= WAVE_FORMAT_PCM
||
1095 lpDesc
->lpFormat
->nChannels
== 0 ||
1096 lpDesc
->lpFormat
->nSamplesPerSec
== 0) {
1097 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
1098 lpDesc
->lpFormat
->wFormatTag
, lpDesc
->lpFormat
->nChannels
,
1099 lpDesc
->lpFormat
->nSamplesPerSec
);
1100 return WAVERR_BADFORMAT
;
1103 if (dwFlags
& WAVE_FORMAT_QUERY
) {
1104 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
1105 lpDesc
->lpFormat
->wFormatTag
, lpDesc
->lpFormat
->nChannels
,
1106 lpDesc
->lpFormat
->nSamplesPerSec
);
1107 return MMSYSERR_NOERROR
;
1110 wwo
= &WOutDev
[wDevID
];
1112 if ((dwFlags
& WAVE_DIRECTSOUND
) && !(wwo
->caps
.dwSupport
& WAVECAPS_DIRECTSOUND
))
1113 /* not supported, ignore it */
1114 dwFlags
&= ~WAVE_DIRECTSOUND
;
1117 flags
= SND_PCM_NONBLOCK
;
1118 if ( dwFlags
& WAVE_DIRECTSOUND
)
1119 flags
|= SND_PCM_ASYNC
;
1121 if (snd_pcm_open(&pcm
, wwo
->device
, SND_PCM_STREAM_PLAYBACK
, dwFlags
))
1123 ERR("Error open: %s\n", snd_strerror(errno
));
1124 return MMSYSERR_NOTENABLED
;
1127 wwo
->wFlags
= HIWORD(dwFlags
& CALLBACK_TYPEMASK
);
1129 memcpy(&wwo
->waveDesc
, lpDesc
, sizeof(WAVEOPENDESC
));
1130 memcpy(&wwo
->format
, lpDesc
->lpFormat
, sizeof(PCMWAVEFORMAT
));
1132 if (wwo
->format
.wBitsPerSample
== 0) {
1133 WARN("Resetting zeroed wBitsPerSample\n");
1134 wwo
->format
.wBitsPerSample
= 8 *
1135 (wwo
->format
.wf
.nAvgBytesPerSec
/
1136 wwo
->format
.wf
.nSamplesPerSec
) /
1137 wwo
->format
.wf
.nChannels
;
1140 snd_pcm_hw_params_any(pcm
, hw_params
);
1142 #define EXIT_ON_ERROR(f,e,txt) do \
1145 if ( (err = (f) ) < 0) \
1147 ERR(txt ": %s\n", snd_strerror(err)); \
1148 snd_pcm_close(pcm); \
1153 access
= SND_PCM_ACCESS_MMAP_INTERLEAVED
;
1154 if ( ( err
= snd_pcm_hw_params_set_access(pcm
, hw_params
, access
) ) < 0) {
1155 WARN("mmap not available. switching to standard write.\n");
1156 access
= SND_PCM_ACCESS_RW_INTERLEAVED
;
1157 EXIT_ON_ERROR( snd_pcm_hw_params_set_access(pcm
, hw_params
, access
), MMSYSERR_INVALPARAM
, "unable to set access for playback");
1158 wwo
->write
= snd_pcm_writei
;
1161 wwo
->write
= snd_pcm_mmap_writei
;
1163 EXIT_ON_ERROR( snd_pcm_hw_params_set_channels(pcm
, hw_params
, wwo
->format
.wf
.nChannels
), MMSYSERR_INVALPARAM
, "unable to set required channels");
1165 format
= (wwo
->format
.wBitsPerSample
== 16) ? SND_PCM_FORMAT_S16_LE
: SND_PCM_FORMAT_U8
;
1166 EXIT_ON_ERROR( snd_pcm_hw_params_set_format(pcm
, hw_params
, format
), MMSYSERR_INVALPARAM
, "unable to set required format");
1168 rate
= snd_pcm_hw_params_set_rate_near(pcm
, hw_params
, wwo
->format
.wf
.nSamplesPerSec
, 0);
1170 ERR("Rate %ld Hz not available for playback: %s\n", wwo
->format
.wf
.nSamplesPerSec
, snd_strerror(rate
));
1172 return WAVERR_BADFORMAT
;
1174 if (rate
!= wwo
->format
.wf
.nSamplesPerSec
) {
1175 ERR("Rate doesn't match (requested %ld Hz, got %d Hz)\n", wwo
->format
.wf
.nSamplesPerSec
, rate
);
1177 return WAVERR_BADFORMAT
;
1180 EXIT_ON_ERROR( snd_pcm_hw_params_set_buffer_time_near(pcm
, hw_params
, buffer_time
, 0), MMSYSERR_INVALPARAM
, "unable to set buffer time");
1181 EXIT_ON_ERROR( snd_pcm_hw_params_set_period_time_near(pcm
, hw_params
, period_time
, 0), MMSYSERR_INVALPARAM
, "unable to set period time");
1183 EXIT_ON_ERROR( snd_pcm_hw_params(pcm
, hw_params
), MMSYSERR_INVALPARAM
, "unable to set hw params for playback");
1185 period_size
= snd_pcm_hw_params_get_period_size(hw_params
, 0);
1186 buffer_size
= snd_pcm_hw_params_get_buffer_size(hw_params
);
1188 snd_pcm_sw_params_current(pcm
, sw_params
);
1189 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");
1190 EXIT_ON_ERROR( snd_pcm_sw_params_set_silence_size(pcm
, sw_params
, 0), MMSYSERR_ERROR
, "unable to set silence size");
1191 EXIT_ON_ERROR( snd_pcm_sw_params_set_avail_min(pcm
, sw_params
, period_size
), MMSYSERR_ERROR
, "unable to set avail min");
1192 EXIT_ON_ERROR( snd_pcm_sw_params_set_xfer_align(pcm
, sw_params
, 1), MMSYSERR_ERROR
, "unable to set xfer align");
1193 EXIT_ON_ERROR( snd_pcm_sw_params_set_silence_threshold(pcm
, sw_params
, 0), MMSYSERR_ERROR
, "unable to set silence threshold");
1194 EXIT_ON_ERROR( snd_pcm_sw_params(pcm
, sw_params
), MMSYSERR_ERROR
, "unable to set sw params for playback");
1195 #undef EXIT_ON_ERROR
1197 snd_pcm_prepare(pcm
);
1200 ALSA_TraceParameters(hw_params
, sw_params
, FALSE
);
1202 /* now, we can save all required data for later use... */
1203 if ( wwo
->hw_params
)
1204 snd_pcm_hw_params_free(wwo
->hw_params
);
1205 snd_pcm_hw_params_malloc(&(wwo
->hw_params
));
1206 snd_pcm_hw_params_copy(wwo
->hw_params
, hw_params
);
1208 wwo
->dwBufferSize
= buffer_size
;
1209 wwo
->lpQueuePtr
= wwo
->lpPlayPtr
= wwo
->lpLoopPtr
= NULL
;
1210 wwo
->p_handle
= pcm
;
1212 ALSA_InitRingMessage(&wwo
->msgRing
);
1214 wwo
->count
= snd_pcm_poll_descriptors_count (wwo
->p_handle
);
1215 if (wwo
->count
<= 0) {
1216 ERR("Invalid poll descriptors count\n");
1217 return MMSYSERR_ERROR
;
1220 wwo
->ufds
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
, sizeof(struct pollfd
) * wwo
->count
);
1221 if (wwo
->ufds
== NULL
) {
1222 ERR("No enough memory\n");
1223 return MMSYSERR_NOMEM
;
1225 if ((err
= snd_pcm_poll_descriptors(wwo
->p_handle
, wwo
->ufds
, wwo
->count
)) < 0) {
1226 ERR("Unable to obtain poll descriptors for playback: %s\n", snd_strerror(err
));
1227 return MMSYSERR_ERROR
;
1230 if (!(dwFlags
& WAVE_DIRECTSOUND
)) {
1231 wwo
->hStartUpEvent
= CreateEventA(NULL
, FALSE
, FALSE
, NULL
);
1232 wwo
->hThread
= CreateThread(NULL
, 0, wodPlayer
, (LPVOID
)(DWORD
)wDevID
, 0, &(wwo
->dwThreadID
));
1233 WaitForSingleObject(wwo
->hStartUpEvent
, INFINITE
);
1234 CloseHandle(wwo
->hStartUpEvent
);
1236 wwo
->hThread
= INVALID_HANDLE_VALUE
;
1237 wwo
->dwThreadID
= 0;
1239 wwo
->hStartUpEvent
= INVALID_HANDLE_VALUE
;
1241 TRACE("handle=%08lx \n", (DWORD
)wwo
->p_handle
);
1242 /* if (wwo->dwFragmentSize % wwo->format.wf.nBlockAlign)
1243 ERR("Fragment doesn't contain an integral number of data blocks\n");
1245 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%lu, nSamplesPerSec=%lu, nChannels=%u nBlockAlign=%u!\n",
1246 wwo
->format
.wBitsPerSample
, wwo
->format
.wf
.nAvgBytesPerSec
,
1247 wwo
->format
.wf
.nSamplesPerSec
, wwo
->format
.wf
.nChannels
,
1248 wwo
->format
.wf
.nBlockAlign
);
1250 return wodNotifyClient(wwo
, WOM_OPEN
, 0L, 0L);
1254 /**************************************************************************
1255 * wodClose [internal]
1257 static DWORD
wodClose(WORD wDevID
)
1259 DWORD ret
= MMSYSERR_NOERROR
;
1262 TRACE("(%u);\n", wDevID
);
1264 if (wDevID
>= MAX_WAVEOUTDRV
|| WOutDev
[wDevID
].p_handle
== NULL
) {
1265 WARN("bad device ID !\n");
1266 return MMSYSERR_BADDEVICEID
;
1269 wwo
= &WOutDev
[wDevID
];
1270 if (wwo
->lpQueuePtr
) {
1271 WARN("buffers still playing !\n");
1272 ret
= WAVERR_STILLPLAYING
;
1274 if (wwo
->hThread
!= INVALID_HANDLE_VALUE
) {
1275 ALSA_AddRingMessage(&wwo
->msgRing
, WINE_WM_CLOSING
, 0, TRUE
);
1277 ALSA_DestroyRingMessage(&wwo
->msgRing
);
1279 snd_pcm_hw_params_free(wwo
->hw_params
);
1280 wwo
->hw_params
= NULL
;
1282 snd_pcm_close(wwo
->p_handle
);
1283 wwo
->p_handle
= NULL
;
1285 ret
= wodNotifyClient(wwo
, WOM_CLOSE
, 0L, 0L);
1288 HeapFree(GetProcessHeap(), 0, wwo
->ufds
);
1293 /**************************************************************************
1294 * wodWrite [internal]
1297 static DWORD
wodWrite(WORD wDevID
, LPWAVEHDR lpWaveHdr
, DWORD dwSize
)
1299 TRACE("(%u, %p, %08lX);\n", wDevID
, lpWaveHdr
, dwSize
);
1301 /* first, do the sanity checks... */
1302 if (wDevID
>= MAX_WAVEOUTDRV
|| WOutDev
[wDevID
].p_handle
== NULL
) {
1303 WARN("bad dev ID !\n");
1304 return MMSYSERR_BADDEVICEID
;
1307 if (lpWaveHdr
->lpData
== NULL
|| !(lpWaveHdr
->dwFlags
& WHDR_PREPARED
))
1308 return WAVERR_UNPREPARED
;
1310 if (lpWaveHdr
->dwFlags
& WHDR_INQUEUE
)
1311 return WAVERR_STILLPLAYING
;
1313 lpWaveHdr
->dwFlags
&= ~WHDR_DONE
;
1314 lpWaveHdr
->dwFlags
|= WHDR_INQUEUE
;
1315 lpWaveHdr
->lpNext
= 0;
1317 ALSA_AddRingMessage(&WOutDev
[wDevID
].msgRing
, WINE_WM_HEADER
, (DWORD
)lpWaveHdr
, FALSE
);
1319 return MMSYSERR_NOERROR
;
1322 /**************************************************************************
1323 * wodPrepare [internal]
1325 static DWORD
wodPrepare(WORD wDevID
, LPWAVEHDR lpWaveHdr
, DWORD dwSize
)
1327 TRACE("(%u, %p, %08lX);\n", wDevID
, lpWaveHdr
, dwSize
);
1329 if (wDevID
>= MAX_WAVEOUTDRV
) {
1330 WARN("bad device ID !\n");
1331 return MMSYSERR_BADDEVICEID
;
1334 if (lpWaveHdr
->dwFlags
& WHDR_INQUEUE
)
1335 return WAVERR_STILLPLAYING
;
1337 lpWaveHdr
->dwFlags
|= WHDR_PREPARED
;
1338 lpWaveHdr
->dwFlags
&= ~WHDR_DONE
;
1339 return MMSYSERR_NOERROR
;
1342 /**************************************************************************
1343 * wodUnprepare [internal]
1345 static DWORD
wodUnprepare(WORD wDevID
, LPWAVEHDR lpWaveHdr
, DWORD dwSize
)
1347 TRACE("(%u, %p, %08lX);\n", wDevID
, lpWaveHdr
, dwSize
);
1349 if (wDevID
>= MAX_WAVEOUTDRV
) {
1350 WARN("bad device ID !\n");
1351 return MMSYSERR_BADDEVICEID
;
1354 if (lpWaveHdr
->dwFlags
& WHDR_INQUEUE
)
1355 return WAVERR_STILLPLAYING
;
1357 lpWaveHdr
->dwFlags
&= ~WHDR_PREPARED
;
1358 lpWaveHdr
->dwFlags
|= WHDR_DONE
;
1360 return MMSYSERR_NOERROR
;
1363 /**************************************************************************
1364 * wodPause [internal]
1366 static DWORD
wodPause(WORD wDevID
)
1368 TRACE("(%u);!\n", wDevID
);
1370 if (wDevID
>= MAX_WAVEOUTDRV
|| WOutDev
[wDevID
].p_handle
== NULL
) {
1371 WARN("bad device ID !\n");
1372 return MMSYSERR_BADDEVICEID
;
1375 ALSA_AddRingMessage(&WOutDev
[wDevID
].msgRing
, WINE_WM_PAUSING
, 0, TRUE
);
1377 return MMSYSERR_NOERROR
;
1380 /**************************************************************************
1381 * wodRestart [internal]
1383 static DWORD
wodRestart(WORD wDevID
)
1385 TRACE("(%u);\n", wDevID
);
1387 if (wDevID
>= MAX_WAVEOUTDRV
|| WOutDev
[wDevID
].p_handle
== NULL
) {
1388 WARN("bad device ID !\n");
1389 return MMSYSERR_BADDEVICEID
;
1392 if (WOutDev
[wDevID
].state
== WINE_WS_PAUSED
) {
1393 ALSA_AddRingMessage(&WOutDev
[wDevID
].msgRing
, WINE_WM_RESTARTING
, 0, TRUE
);
1396 /* FIXME: is NotifyClient with WOM_DONE right ? (Comet Busters 1.3.3 needs this notification) */
1397 /* FIXME: Myst crashes with this ... hmm -MM
1398 return wodNotifyClient(wwo, WOM_DONE, 0L, 0L);
1401 return MMSYSERR_NOERROR
;
1404 /**************************************************************************
1405 * wodReset [internal]
1407 static DWORD
wodReset(WORD wDevID
)
1409 TRACE("(%u);\n", wDevID
);
1411 if (wDevID
>= MAX_WAVEOUTDRV
|| WOutDev
[wDevID
].p_handle
== NULL
) {
1412 WARN("bad device ID !\n");
1413 return MMSYSERR_BADDEVICEID
;
1416 ALSA_AddRingMessage(&WOutDev
[wDevID
].msgRing
, WINE_WM_RESETTING
, 0, TRUE
);
1418 return MMSYSERR_NOERROR
;
1421 /**************************************************************************
1422 * wodGetPosition [internal]
1424 static DWORD
wodGetPosition(WORD wDevID
, LPMMTIME lpTime
, DWORD uSize
)
1430 TRACE("(%u, %p, %lu);\n", wDevID
, lpTime
, uSize
);
1432 if (wDevID
>= MAX_WAVEOUTDRV
|| WOutDev
[wDevID
].p_handle
== NULL
) {
1433 WARN("bad device ID !\n");
1434 return MMSYSERR_BADDEVICEID
;
1437 if (lpTime
== NULL
) return MMSYSERR_INVALPARAM
;
1439 wwo
= &WOutDev
[wDevID
];
1440 ALSA_AddRingMessage(&wwo
->msgRing
, WINE_WM_UPDATE
, 0, TRUE
);
1441 val
= wwo
->dwPlayedTotal
;
1443 TRACE("wType=%04X wBitsPerSample=%u nSamplesPerSec=%lu nChannels=%u nAvgBytesPerSec=%lu\n",
1444 lpTime
->wType
, wwo
->format
.wBitsPerSample
,
1445 wwo
->format
.wf
.nSamplesPerSec
, wwo
->format
.wf
.nChannels
,
1446 wwo
->format
.wf
.nAvgBytesPerSec
);
1447 TRACE("dwPlayedTotal=%lu\n", val
);
1449 switch (lpTime
->wType
) {
1452 TRACE("TIME_BYTES=%lu\n", lpTime
->u
.cb
);
1455 lpTime
->u
.sample
= val
* 8 / wwo
->format
.wBitsPerSample
/wwo
->format
.wf
.nChannels
;
1456 TRACE("TIME_SAMPLES=%lu\n", lpTime
->u
.sample
);
1459 time
= val
/ (wwo
->format
.wf
.nAvgBytesPerSec
/ 1000);
1460 lpTime
->u
.smpte
.hour
= time
/ 108000;
1461 time
-= lpTime
->u
.smpte
.hour
* 108000;
1462 lpTime
->u
.smpte
.min
= time
/ 1800;
1463 time
-= lpTime
->u
.smpte
.min
* 1800;
1464 lpTime
->u
.smpte
.sec
= time
/ 30;
1465 time
-= lpTime
->u
.smpte
.sec
* 30;
1466 lpTime
->u
.smpte
.frame
= time
;
1467 lpTime
->u
.smpte
.fps
= 30;
1468 TRACE("TIME_SMPTE=%02u:%02u:%02u:%02u\n",
1469 lpTime
->u
.smpte
.hour
, lpTime
->u
.smpte
.min
,
1470 lpTime
->u
.smpte
.sec
, lpTime
->u
.smpte
.frame
);
1473 FIXME("Format %d not supported ! use TIME_MS !\n", lpTime
->wType
);
1474 lpTime
->wType
= TIME_MS
;
1476 lpTime
->u
.ms
= val
/ (wwo
->format
.wf
.nAvgBytesPerSec
/ 1000);
1477 TRACE("TIME_MS=%lu\n", lpTime
->u
.ms
);
1480 return MMSYSERR_NOERROR
;
1483 /**************************************************************************
1484 * wodBreakLoop [internal]
1486 static DWORD
wodBreakLoop(WORD wDevID
)
1488 TRACE("(%u);\n", wDevID
);
1490 if (wDevID
>= MAX_WAVEOUTDRV
|| WOutDev
[wDevID
].p_handle
== NULL
) {
1491 WARN("bad device ID !\n");
1492 return MMSYSERR_BADDEVICEID
;
1494 ALSA_AddRingMessage(&WOutDev
[wDevID
].msgRing
, WINE_WM_BREAKLOOP
, 0, TRUE
);
1495 return MMSYSERR_NOERROR
;
1498 /**************************************************************************
1499 * wodGetVolume [internal]
1501 static DWORD
wodGetVolume(WORD wDevID
, LPDWORD lpdwVol
)
1508 TRACE("(%u, %p);\n", wDevID
, lpdwVol
);
1509 if (wDevID
>= MAX_WAVEOUTDRV
|| WOutDev
[wDevID
].p_handle
== NULL
) {
1510 WARN("bad device ID !\n");
1511 return MMSYSERR_BADDEVICEID
;
1513 wwo
= &WOutDev
[wDevID
];
1514 count
= snd_ctl_elem_info_get_count(wwo
->playback_einfo
);
1515 min
= snd_ctl_elem_info_get_min(wwo
->playback_einfo
);
1516 max
= snd_ctl_elem_info_get_max(wwo
->playback_einfo
);
1518 #define VOLUME_ALSA_TO_WIN(x) (((x)-min) * 65536 /(max-min))
1519 if (lpdwVol
== NULL
)
1520 return MMSYSERR_NOTENABLED
;
1525 left
= VOLUME_ALSA_TO_WIN(snd_ctl_elem_value_get_integer(wwo
->playback_evalue
, 0));
1526 right
= VOLUME_ALSA_TO_WIN(snd_ctl_elem_value_get_integer(wwo
->playback_evalue
, 1));
1529 left
= right
= VOLUME_ALSA_TO_WIN(snd_ctl_elem_value_get_integer(wwo
->playback_evalue
, 0));
1532 WARN("%d channels mixer not supported\n", count
);
1533 return MMSYSERR_NOERROR
;
1535 #undef VOLUME_ALSA_TO_WIN
1537 TRACE("left=%d right=%d !\n", left
, right
);
1538 *lpdwVol
= MAKELONG( left
, right
);
1539 return MMSYSERR_NOERROR
;
1542 /**************************************************************************
1543 * wodSetVolume [internal]
1545 static DWORD
wodSetVolume(WORD wDevID
, DWORD dwParam
)
1552 TRACE("(%u, %08lX);\n", wDevID
, dwParam
);
1553 if (wDevID
>= MAX_WAVEOUTDRV
|| WOutDev
[wDevID
].p_handle
== NULL
) {
1554 WARN("bad device ID !\n");
1555 return MMSYSERR_BADDEVICEID
;
1557 wwo
= &WOutDev
[wDevID
];
1558 count
=snd_ctl_elem_info_get_count(wwo
->playback_einfo
);
1559 min
= snd_ctl_elem_info_get_min(wwo
->playback_einfo
);
1560 max
= snd_ctl_elem_info_get_max(wwo
->playback_einfo
);
1562 left
= LOWORD(dwParam
);
1563 right
= HIWORD(dwParam
);
1565 #define VOLUME_WIN_TO_ALSA(x) ( (((x) * (max-min)) / 65536) + min )
1569 snd_ctl_elem_value_set_integer(wwo
->playback_evalue
, 0, VOLUME_WIN_TO_ALSA(left
));
1570 snd_ctl_elem_value_set_integer(wwo
->playback_evalue
, 1, VOLUME_WIN_TO_ALSA(right
));
1573 snd_ctl_elem_value_set_integer(wwo
->playback_evalue
, 0, VOLUME_WIN_TO_ALSA(left
));
1576 WARN("%d channels mixer not supported\n", count
);
1578 #undef VOLUME_WIN_TO_ALSA
1579 if ( (err
= snd_ctl_elem_write(wwo
->ctl
, wwo
->playback_evalue
)) < 0)
1581 ERR("error writing snd_ctl_elem_value: %s\n", snd_strerror(err
));
1583 return MMSYSERR_NOERROR
;
1586 /**************************************************************************
1587 * wodGetNumDevs [internal]
1589 static DWORD
wodGetNumDevs(void)
1591 return ALSA_WodNumDevs
;
1595 /**************************************************************************
1596 * wodMessage (WINEALSA.@)
1598 DWORD WINAPI
ALSA_wodMessage(UINT wDevID
, UINT wMsg
, DWORD dwUser
,
1599 DWORD dwParam1
, DWORD dwParam2
)
1601 TRACE("(%u, %04X, %08lX, %08lX, %08lX);\n",
1602 wDevID
, wMsg
, dwUser
, dwParam1
, dwParam2
);
1609 /* FIXME: Pretend this is supported */
1611 case WODM_OPEN
: return wodOpen (wDevID
, (LPWAVEOPENDESC
)dwParam1
, dwParam2
);
1612 case WODM_CLOSE
: return wodClose (wDevID
);
1613 case WODM_GETDEVCAPS
: return wodGetDevCaps (wDevID
, (LPWAVEOUTCAPSA
)dwParam1
, dwParam2
);
1614 case WODM_GETNUMDEVS
: return wodGetNumDevs ();
1615 case WODM_GETPITCH
: return MMSYSERR_NOTSUPPORTED
;
1616 case WODM_SETPITCH
: return MMSYSERR_NOTSUPPORTED
;
1617 case WODM_GETPLAYBACKRATE
: return MMSYSERR_NOTSUPPORTED
;
1618 case WODM_SETPLAYBACKRATE
: return MMSYSERR_NOTSUPPORTED
;
1619 case WODM_WRITE
: return wodWrite (wDevID
, (LPWAVEHDR
)dwParam1
, dwParam2
);
1620 case WODM_PAUSE
: return wodPause (wDevID
);
1621 case WODM_GETPOS
: return wodGetPosition (wDevID
, (LPMMTIME
)dwParam1
, dwParam2
);
1622 case WODM_BREAKLOOP
: return wodBreakLoop (wDevID
);
1623 case WODM_PREPARE
: return wodPrepare (wDevID
, (LPWAVEHDR
)dwParam1
, dwParam2
);
1624 case WODM_UNPREPARE
: return wodUnprepare (wDevID
, (LPWAVEHDR
)dwParam1
, dwParam2
);
1625 case WODM_GETVOLUME
: return wodGetVolume (wDevID
, (LPDWORD
)dwParam1
);
1626 case WODM_SETVOLUME
: return wodSetVolume (wDevID
, dwParam1
);
1627 case WODM_RESTART
: return wodRestart (wDevID
);
1628 case WODM_RESET
: return wodReset (wDevID
);
1629 case DRV_QUERYDSOUNDIFACE
: return wodDsCreate (wDevID
, (PIDSDRIVER
*)dwParam1
);
1630 case DRV_QUERYDSOUNDDESC
: return wodDsDesc (wDevID
, (PDSDRIVERDESC
)dwParam1
);
1631 case DRV_QUERYDSOUNDGUID
: return wodDsGuid (wDevID
, (LPGUID
)dwParam1
);
1634 FIXME("unknown message %d!\n", wMsg
);
1636 return MMSYSERR_NOTSUPPORTED
;
1639 /*======================================================================*
1640 * Low level DSOUND implementation *
1641 *======================================================================*/
1643 typedef struct IDsDriverImpl IDsDriverImpl
;
1644 typedef struct IDsDriverBufferImpl IDsDriverBufferImpl
;
1646 struct IDsDriverImpl
1648 /* IUnknown fields */
1649 ICOM_VFIELD(IDsDriver
);
1651 /* IDsDriverImpl fields */
1653 IDsDriverBufferImpl
*primary
;
1656 struct IDsDriverBufferImpl
1658 /* IUnknown fields */
1659 ICOM_VFIELD(IDsDriverBuffer
);
1661 /* IDsDriverBufferImpl fields */
1664 CRITICAL_SECTION mmap_crst
;
1666 DWORD mmap_buflen_bytes
;
1667 snd_pcm_uframes_t mmap_buflen_frames
;
1668 snd_pcm_channel_area_t
* mmap_areas
;
1669 snd_async_handler_t
* mmap_async_handler
;
1672 static void DSDB_CheckXRUN(IDsDriverBufferImpl
* pdbi
)
1674 WINE_WAVEOUT
* wwo
= &(WOutDev
[pdbi
->drv
->wDevID
]);
1675 snd_pcm_state_t state
= snd_pcm_state(wwo
->p_handle
);
1677 if ( state
== SND_PCM_STATE_XRUN
)
1679 int err
= snd_pcm_prepare(wwo
->p_handle
);
1680 TRACE("xrun occurred\n");
1682 ERR("recovery from xrun failed, prepare failed: %s\n", snd_strerror(err
));
1684 else if ( state
== SND_PCM_STATE_SUSPENDED
)
1686 int err
= snd_pcm_resume(wwo
->p_handle
);
1687 TRACE("recovery from suspension occurred\n");
1688 if (err
< 0 && err
!= -EAGAIN
){
1689 err
= snd_pcm_prepare(wwo
->p_handle
);
1691 ERR("recovery from suspend failed, prepare failed: %s\n", snd_strerror(err
));
1696 static void DSDB_MMAPCopy(IDsDriverBufferImpl
* pdbi
)
1698 WINE_WAVEOUT
* wwo
= &(WOutDev
[pdbi
->drv
->wDevID
]);
1700 snd_pcm_format_t format
;
1701 snd_pcm_uframes_t period_size
;
1702 snd_pcm_sframes_t avail
;
1704 if ( !pdbi
->mmap_buffer
|| !wwo
->hw_params
|| !wwo
->p_handle
)
1707 channels
= snd_pcm_hw_params_get_channels(wwo
->hw_params
);
1708 format
= snd_pcm_hw_params_get_format(wwo
->hw_params
);
1709 period_size
= snd_pcm_hw_params_get_period_size(wwo
->hw_params
, 0);
1710 avail
= snd_pcm_avail_update(wwo
->p_handle
);
1712 DSDB_CheckXRUN(pdbi
);
1714 TRACE("avail=%d format=%s channels=%d\n", (int)avail
, snd_pcm_format_name(format
), channels
);
1716 while (avail
>= period_size
)
1718 const snd_pcm_channel_area_t
*areas
;
1719 snd_pcm_uframes_t ofs
;
1720 snd_pcm_uframes_t frames
;
1723 frames
= avail
/ period_size
* period_size
; /* round down to a multiple of period_size */
1725 EnterCriticalSection(&pdbi
->mmap_crst
);
1727 snd_pcm_mmap_begin(wwo
->p_handle
, &areas
, &ofs
, &frames
);
1728 snd_pcm_areas_copy(areas
, ofs
, pdbi
->mmap_areas
, ofs
, channels
, frames
, format
);
1729 err
= snd_pcm_mmap_commit(wwo
->p_handle
, ofs
, frames
);
1731 LeaveCriticalSection(&pdbi
->mmap_crst
);
1733 if ( err
!= (snd_pcm_sframes_t
) frames
)
1734 ERR("mmap partially failed.\n");
1736 avail
= snd_pcm_avail_update(wwo
->p_handle
);
1740 static void DSDB_PCMCallback(snd_async_handler_t
*ahandler
)
1742 /* snd_pcm_t * handle = snd_async_handler_get_pcm(ahandler); */
1743 IDsDriverBufferImpl
* pdbi
= snd_async_handler_get_callback_private(ahandler
);
1744 TRACE("callback called\n");
1745 DSDB_MMAPCopy(pdbi
);
1748 static int DSDB_CreateMMAP(IDsDriverBufferImpl
* pdbi
)
1750 WINE_WAVEOUT
* wwo
= &(WOutDev
[pdbi
->drv
->wDevID
]);
1751 snd_pcm_format_t format
= snd_pcm_hw_params_get_format(wwo
->hw_params
);
1752 snd_pcm_uframes_t frames
= snd_pcm_hw_params_get_buffer_size(wwo
->hw_params
);
1753 int channels
= snd_pcm_hw_params_get_channels(wwo
->hw_params
);
1754 unsigned int bits_per_sample
= snd_pcm_format_physical_width(format
);
1755 unsigned int bits_per_frame
= bits_per_sample
* channels
;
1756 snd_pcm_channel_area_t
* a
;
1761 ALSA_TraceParameters(wwo
->hw_params
, NULL
, FALSE
);
1763 TRACE("format=%s frames=%ld channels=%d bits_per_sample=%d bits_per_frame=%d\n",
1764 snd_pcm_format_name(format
), frames
, channels
, bits_per_sample
, bits_per_frame
);
1766 pdbi
->mmap_buflen_frames
= frames
;
1767 pdbi
->mmap_buflen_bytes
= snd_pcm_frames_to_bytes( wwo
->p_handle
, frames
);
1768 pdbi
->mmap_buffer
= HeapAlloc(GetProcessHeap(),0,pdbi
->mmap_buflen_bytes
);
1769 if (!pdbi
->mmap_buffer
)
1770 return DSERR_OUTOFMEMORY
;
1772 snd_pcm_format_set_silence(format
, pdbi
->mmap_buffer
, frames
);
1774 TRACE("created mmap buffer of %ld frames (%ld bytes) at %p\n",
1775 frames
, pdbi
->mmap_buflen_bytes
, pdbi
->mmap_buffer
);
1777 pdbi
->mmap_areas
= HeapAlloc(GetProcessHeap(),0,channels
*sizeof(snd_pcm_channel_area_t
));
1778 if (!pdbi
->mmap_areas
)
1779 return DSERR_OUTOFMEMORY
;
1781 a
= pdbi
->mmap_areas
;
1782 for (c
= 0; c
< channels
; c
++, a
++)
1784 a
->addr
= pdbi
->mmap_buffer
;
1785 a
->first
= bits_per_sample
* c
;
1786 a
->step
= bits_per_frame
;
1787 TRACE("Area %d: addr=%p first=%d step=%d\n", c
, a
->addr
, a
->first
, a
->step
);
1790 InitializeCriticalSection(&pdbi
->mmap_crst
);
1792 err
= snd_async_add_pcm_handler(&pdbi
->mmap_async_handler
, wwo
->p_handle
, DSDB_PCMCallback
, pdbi
);
1795 ERR("add_pcm_handler failed. reason: %s\n", snd_strerror(err
));
1796 return DSERR_GENERIC
;
1802 static void DSDB_DestroyMMAP(IDsDriverBufferImpl
* pdbi
)
1804 TRACE("mmap buffer %p destroyed\n", pdbi
->mmap_buffer
);
1805 HeapFree(GetProcessHeap(), 0, pdbi
->mmap_areas
);
1806 HeapFree(GetProcessHeap(), 0, pdbi
->mmap_buffer
);
1807 pdbi
->mmap_areas
= NULL
;
1808 pdbi
->mmap_buffer
= NULL
;
1809 DeleteCriticalSection(&pdbi
->mmap_crst
);
1813 static HRESULT WINAPI
IDsDriverBufferImpl_QueryInterface(PIDSDRIVERBUFFER iface
, REFIID riid
, LPVOID
*ppobj
)
1815 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
1816 FIXME("(): stub!\n");
1817 return DSERR_UNSUPPORTED
;
1820 static ULONG WINAPI
IDsDriverBufferImpl_AddRef(PIDSDRIVERBUFFER iface
)
1822 ICOM_THIS(IDsDriverBufferImpl
,iface
);
1823 TRACE("(%p)\n",iface
);
1827 static ULONG WINAPI
IDsDriverBufferImpl_Release(PIDSDRIVERBUFFER iface
)
1829 ICOM_THIS(IDsDriverBufferImpl
,iface
);
1830 TRACE("(%p)\n",iface
);
1833 if (This
== This
->drv
->primary
)
1834 This
->drv
->primary
= NULL
;
1835 DSDB_DestroyMMAP(This
);
1836 HeapFree(GetProcessHeap(), 0, This
);
1840 static HRESULT WINAPI
IDsDriverBufferImpl_Lock(PIDSDRIVERBUFFER iface
,
1841 LPVOID
*ppvAudio1
,LPDWORD pdwLen1
,
1842 LPVOID
*ppvAudio2
,LPDWORD pdwLen2
,
1843 DWORD dwWritePosition
,DWORD dwWriteLen
,
1846 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
1847 TRACE("(%p)\n",iface
);
1848 return DSERR_UNSUPPORTED
;
1851 static HRESULT WINAPI
IDsDriverBufferImpl_Unlock(PIDSDRIVERBUFFER iface
,
1852 LPVOID pvAudio1
,DWORD dwLen1
,
1853 LPVOID pvAudio2
,DWORD dwLen2
)
1855 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
1856 TRACE("(%p)\n",iface
);
1857 return DSERR_UNSUPPORTED
;
1860 static HRESULT WINAPI
IDsDriverBufferImpl_SetFormat(PIDSDRIVERBUFFER iface
,
1861 LPWAVEFORMATEX pwfx
)
1863 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
1864 TRACE("(%p,%p)\n",iface
,pwfx
);
1865 return DSERR_BUFFERLOST
;
1868 static HRESULT WINAPI
IDsDriverBufferImpl_SetFrequency(PIDSDRIVERBUFFER iface
, DWORD dwFreq
)
1870 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
1871 TRACE("(%p,%ld): stub\n",iface
,dwFreq
);
1872 return DSERR_UNSUPPORTED
;
1875 static HRESULT WINAPI
IDsDriverBufferImpl_SetVolumePan(PIDSDRIVERBUFFER iface
, PDSVOLUMEPAN pVolPan
)
1877 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
1878 FIXME("(%p,%p): stub!\n",iface
,pVolPan
);
1882 static HRESULT WINAPI
IDsDriverBufferImpl_SetPosition(PIDSDRIVERBUFFER iface
, DWORD dwNewPos
)
1884 /* ICOM_THIS(IDsDriverImpl,iface); */
1885 TRACE("(%p,%ld): stub\n",iface
,dwNewPos
);
1886 return DSERR_UNSUPPORTED
;
1889 static HRESULT WINAPI
IDsDriverBufferImpl_GetPosition(PIDSDRIVERBUFFER iface
,
1890 LPDWORD lpdwPlay
, LPDWORD lpdwWrite
)
1892 ICOM_THIS(IDsDriverBufferImpl
,iface
);
1893 WINE_WAVEOUT
* wwo
= &(WOutDev
[This
->drv
->wDevID
]);
1894 snd_pcm_uframes_t hw_ptr
;
1895 snd_pcm_uframes_t period_size
;
1897 if (wwo
->hw_params
== NULL
) return DSERR_GENERIC
;
1899 period_size
= snd_pcm_hw_params_get_period_size(wwo
->hw_params
, 0);
1901 if (wwo
->p_handle
== NULL
) return DSERR_GENERIC
;
1902 /** we need to track down buffer underruns */
1903 DSDB_CheckXRUN(This
);
1905 EnterCriticalSection(&This
->mmap_crst
);
1906 hw_ptr
= _snd_pcm_mmap_hw_ptr(wwo
->p_handle
);
1908 *lpdwPlay
= snd_pcm_frames_to_bytes(wwo
->p_handle
, hw_ptr
/ period_size
* period_size
) % This
->mmap_buflen_bytes
;
1910 *lpdwWrite
= snd_pcm_frames_to_bytes(wwo
->p_handle
, (hw_ptr
/ period_size
+ 1) * period_size
) % This
->mmap_buflen_bytes
;
1911 LeaveCriticalSection(&This
->mmap_crst
);
1913 TRACE("hw_ptr=0x%08x, playpos=%ld, writepos=%ld\n", (unsigned int)hw_ptr
, lpdwPlay
?*lpdwPlay
:-1, lpdwWrite
?*lpdwWrite
:-1);
1917 static HRESULT WINAPI
IDsDriverBufferImpl_Play(PIDSDRIVERBUFFER iface
, DWORD dwRes1
, DWORD dwRes2
, DWORD dwFlags
)
1919 ICOM_THIS(IDsDriverBufferImpl
,iface
);
1920 WINE_WAVEOUT
* wwo
= &(WOutDev
[This
->drv
->wDevID
]);
1921 snd_pcm_state_t state
;
1924 TRACE("(%p,%lx,%lx,%lx)\n",iface
,dwRes1
,dwRes2
,dwFlags
);
1926 if (wwo
->p_handle
== NULL
) return DSERR_GENERIC
;
1928 state
= snd_pcm_state(wwo
->p_handle
);
1929 if ( state
== SND_PCM_STATE_SETUP
)
1931 err
= snd_pcm_prepare(wwo
->p_handle
);
1932 state
= snd_pcm_state(wwo
->p_handle
);
1934 if ( state
== SND_PCM_STATE_PREPARED
)
1936 DSDB_MMAPCopy(This
);
1937 err
= snd_pcm_start(wwo
->p_handle
);
1942 static HRESULT WINAPI
IDsDriverBufferImpl_Stop(PIDSDRIVERBUFFER iface
)
1944 ICOM_THIS(IDsDriverBufferImpl
,iface
);
1945 WINE_WAVEOUT
* wwo
= &(WOutDev
[This
->drv
->wDevID
]);
1950 TRACE("(%p)\n",iface
);
1952 if (wwo
->p_handle
== NULL
) return DSERR_GENERIC
;
1954 /* ring buffer wrap up detection */
1955 IDsDriverBufferImpl_GetPosition(iface
, &play
, &write
);
1958 TRACE("writepos wrapper up\n");
1962 if ( ( err
= snd_pcm_drop(wwo
->p_handle
)) < 0 )
1964 ERR("error while stopping pcm: %s\n", snd_strerror(err
));
1965 return DSERR_GENERIC
;
1970 static ICOM_VTABLE(IDsDriverBuffer
) dsdbvt
=
1972 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1973 IDsDriverBufferImpl_QueryInterface
,
1974 IDsDriverBufferImpl_AddRef
,
1975 IDsDriverBufferImpl_Release
,
1976 IDsDriverBufferImpl_Lock
,
1977 IDsDriverBufferImpl_Unlock
,
1978 IDsDriverBufferImpl_SetFormat
,
1979 IDsDriverBufferImpl_SetFrequency
,
1980 IDsDriverBufferImpl_SetVolumePan
,
1981 IDsDriverBufferImpl_SetPosition
,
1982 IDsDriverBufferImpl_GetPosition
,
1983 IDsDriverBufferImpl_Play
,
1984 IDsDriverBufferImpl_Stop
1987 static HRESULT WINAPI
IDsDriverImpl_QueryInterface(PIDSDRIVER iface
, REFIID riid
, LPVOID
*ppobj
)
1989 /* ICOM_THIS(IDsDriverImpl,iface); */
1990 FIXME("(%p): stub!\n",iface
);
1991 return DSERR_UNSUPPORTED
;
1994 static ULONG WINAPI
IDsDriverImpl_AddRef(PIDSDRIVER iface
)
1996 ICOM_THIS(IDsDriverImpl
,iface
);
1997 TRACE("(%p)\n",iface
);
2002 static ULONG WINAPI
IDsDriverImpl_Release(PIDSDRIVER iface
)
2004 ICOM_THIS(IDsDriverImpl
,iface
);
2005 TRACE("(%p)\n",iface
);
2008 HeapFree(GetProcessHeap(),0,This
);
2012 static HRESULT WINAPI
IDsDriverImpl_GetDriverDesc(PIDSDRIVER iface
, PDSDRIVERDESC pDesc
)
2014 ICOM_THIS(IDsDriverImpl
,iface
);
2015 TRACE("(%p,%p)\n",iface
,pDesc
);
2016 memcpy(pDesc
, &(WOutDev
[This
->wDevID
].ds_desc
), sizeof(DSDRIVERDESC
));
2017 pDesc
->dwFlags
= DSDDESC_DOMMSYSTEMOPEN
| DSDDESC_DOMMSYSTEMSETFORMAT
|
2018 DSDDESC_USESYSTEMMEMORY
;
2019 pDesc
->dnDevNode
= WOutDev
[This
->wDevID
].waveDesc
.dnDevNode
;
2021 pDesc
->wReserved
= 0;
2022 pDesc
->ulDeviceNum
= This
->wDevID
;
2023 pDesc
->dwHeapType
= DSDHEAP_NOHEAP
;
2024 pDesc
->pvDirectDrawHeap
= NULL
;
2025 pDesc
->dwMemStartAddress
= 0;
2026 pDesc
->dwMemEndAddress
= 0;
2027 pDesc
->dwMemAllocExtra
= 0;
2028 pDesc
->pvReserved1
= NULL
;
2029 pDesc
->pvReserved2
= NULL
;
2033 static HRESULT WINAPI
IDsDriverImpl_Open(PIDSDRIVER iface
)
2035 /* ICOM_THIS(IDsDriverImpl,iface); */
2036 TRACE("(%p)\n",iface
);
2040 static HRESULT WINAPI
IDsDriverImpl_Close(PIDSDRIVER iface
)
2042 /* ICOM_THIS(IDsDriverImpl,iface); */
2043 TRACE("(%p)\n",iface
);
2047 static HRESULT WINAPI
IDsDriverImpl_GetCaps(PIDSDRIVER iface
, PDSDRIVERCAPS pCaps
)
2049 ICOM_THIS(IDsDriverImpl
,iface
);
2050 TRACE("(%p,%p)\n",iface
,pCaps
);
2051 memset(pCaps
, 0, sizeof(*pCaps
));
2053 pCaps
->dwFlags
= DSCAPS_PRIMARYMONO
;
2054 if ( WOutDev
[This
->wDevID
].caps
.wChannels
== 2 )
2055 pCaps
->dwFlags
|= DSCAPS_PRIMARYSTEREO
;
2057 if ( WOutDev
[This
->wDevID
].caps
.dwFormats
& (WAVE_FORMAT_1S08
| WAVE_FORMAT_2S08
| WAVE_FORMAT_4S08
) )
2058 pCaps
->dwFlags
|= DSCAPS_PRIMARY8BIT
;
2060 if ( WOutDev
[This
->wDevID
].caps
.dwFormats
& (WAVE_FORMAT_1S16
| WAVE_FORMAT_2S16
| WAVE_FORMAT_4S16
))
2061 pCaps
->dwFlags
|= DSCAPS_PRIMARY16BIT
;
2063 pCaps
->dwPrimaryBuffers
= 1;
2064 TRACE("caps=0x%X\n",(unsigned int)pCaps
->dwFlags
);
2065 pCaps
->dwMinSecondarySampleRate
= DSBFREQUENCY_MIN
;
2066 pCaps
->dwMaxSecondarySampleRate
= DSBFREQUENCY_MAX
;
2068 /* the other fields only apply to secondary buffers, which we don't support
2069 * (unless we want to mess with wavetable synthesizers and MIDI) */
2073 static HRESULT WINAPI
IDsDriverImpl_CreateSoundBuffer(PIDSDRIVER iface
,
2074 LPWAVEFORMATEX pwfx
,
2075 DWORD dwFlags
, DWORD dwCardAddress
,
2076 LPDWORD pdwcbBufferSize
,
2080 ICOM_THIS(IDsDriverImpl
,iface
);
2081 IDsDriverBufferImpl
** ippdsdb
= (IDsDriverBufferImpl
**)ppvObj
;
2084 TRACE("(%p,%p,%lx,%lx)\n",iface
,pwfx
,dwFlags
,dwCardAddress
);
2085 /* we only support primary buffers */
2086 if (!(dwFlags
& DSBCAPS_PRIMARYBUFFER
))
2087 return DSERR_UNSUPPORTED
;
2089 return DSERR_ALLOCATED
;
2090 if (dwFlags
& (DSBCAPS_CTRLFREQUENCY
| DSBCAPS_CTRLPAN
))
2091 return DSERR_CONTROLUNAVAIL
;
2093 *ippdsdb
= (IDsDriverBufferImpl
*)HeapAlloc(GetProcessHeap(),0,sizeof(IDsDriverBufferImpl
));
2094 if (*ippdsdb
== NULL
)
2095 return DSERR_OUTOFMEMORY
;
2096 (*ippdsdb
)->lpVtbl
= &dsdbvt
;
2097 (*ippdsdb
)->ref
= 1;
2098 (*ippdsdb
)->drv
= This
;
2100 err
= DSDB_CreateMMAP((*ippdsdb
));
2103 HeapFree(GetProcessHeap(), 0, *ippdsdb
);
2107 *ppbBuffer
= (*ippdsdb
)->mmap_buffer
;
2108 *pdwcbBufferSize
= (*ippdsdb
)->mmap_buflen_bytes
;
2110 This
->primary
= *ippdsdb
;
2112 /* buffer is ready to go */
2113 TRACE("buffer created at %p\n", *ippdsdb
);
2117 static HRESULT WINAPI
IDsDriverImpl_DuplicateSoundBuffer(PIDSDRIVER iface
,
2118 PIDSDRIVERBUFFER pBuffer
,
2121 /* ICOM_THIS(IDsDriverImpl,iface); */
2122 TRACE("(%p,%p): stub\n",iface
,pBuffer
);
2123 return DSERR_INVALIDCALL
;
2126 static ICOM_VTABLE(IDsDriver
) dsdvt
=
2128 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
2129 IDsDriverImpl_QueryInterface
,
2130 IDsDriverImpl_AddRef
,
2131 IDsDriverImpl_Release
,
2132 IDsDriverImpl_GetDriverDesc
,
2134 IDsDriverImpl_Close
,
2135 IDsDriverImpl_GetCaps
,
2136 IDsDriverImpl_CreateSoundBuffer
,
2137 IDsDriverImpl_DuplicateSoundBuffer
2140 static DWORD
wodDsCreate(UINT wDevID
, PIDSDRIVER
* drv
)
2142 IDsDriverImpl
** idrv
= (IDsDriverImpl
**)drv
;
2144 TRACE("driver created\n");
2146 /* the HAL isn't much better than the HEL if we can't do mmap() */
2147 if (!(WOutDev
[wDevID
].caps
.dwSupport
& WAVECAPS_DIRECTSOUND
)) {
2148 ERR("DirectSound flag not set\n");
2149 MESSAGE("This sound card's driver does not support direct access\n");
2150 MESSAGE("The (slower) DirectSound HEL mode will be used instead.\n");
2151 return MMSYSERR_NOTSUPPORTED
;
2154 *idrv
= (IDsDriverImpl
*)HeapAlloc(GetProcessHeap(),0,sizeof(IDsDriverImpl
));
2156 return MMSYSERR_NOMEM
;
2157 (*idrv
)->lpVtbl
= &dsdvt
;
2160 (*idrv
)->wDevID
= wDevID
;
2161 (*idrv
)->primary
= NULL
;
2162 return MMSYSERR_NOERROR
;
2165 static DWORD
wodDsDesc(UINT wDevID
, PDSDRIVERDESC desc
)
2167 memcpy(desc
, &(WOutDev
[wDevID
].ds_desc
), sizeof(DSDRIVERDESC
));
2168 return MMSYSERR_NOERROR
;
2171 static DWORD
wodDsGuid(UINT wDevID
, LPGUID pGuid
)
2173 TRACE("(%d,%p)\n",wDevID
,pGuid
);
2175 memcpy(pGuid
, &(WOutDev
[wDevID
].ds_guid
), sizeof(GUID
));
2177 return MMSYSERR_NOERROR
;
2184 /**************************************************************************
2185 * wodMessage (WINEALSA.@)
2187 DWORD WINAPI
ALSA_wodMessage(WORD wDevID
, WORD wMsg
, DWORD dwUser
,
2188 DWORD dwParam1
, DWORD dwParam2
)
2190 FIXME("(%u, %04X, %08lX, %08lX, %08lX):stub\n", wDevID
, wMsg
, dwUser
, dwParam1
, dwParam2
);
2191 return MMSYSERR_NOTENABLED
;
2194 #endif /* HAVE_ALSA */