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
;
414 /* FIXME: use better values */
416 wwo
->caps
.wMid
= 0x0002;
417 wwo
->caps
.wPid
= 0x0104;
418 strcpy(wwo
->caps
.szPname
, "SB16 Wave Out");
419 wwo
->caps
.vDriverVersion
= 0x0100;
420 wwo
->caps
.dwFormats
= 0x00000000;
421 wwo
->caps
.dwSupport
= WAVECAPS_VOLUME
;
422 strcpy(wwo
->ds_desc
.szDesc
, "WineALSA DirectSound Driver");
423 strcpy(wwo
->ds_desc
.szDrvName
, "winealsa.drv");
424 wwo
->ds_guid
= DSDEVID_DefaultPlayback
;
426 if (!wine_dlopen("libasound.so.2", RTLD_LAZY
|RTLD_GLOBAL
, NULL
, 0))
428 ERR("Error: ALSA lib needs to be loaded with flags RTLD_LAZY and RTLD_GLOBAL.\n");
432 snd_pcm_info_alloca(&info
);
433 snd_pcm_hw_params_alloca(&hw_params
);
435 #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)
438 EXIT_ON_ERROR( snd_pcm_open(&h
, wwo
->device
, SND_PCM_STREAM_PLAYBACK
, SND_PCM_NONBLOCK
) , "open pcm" );
442 EXIT_ON_ERROR( snd_pcm_info(h
, info
) , "pcm info" );
444 TRACE("dev=%d id=%s name=%s subdev=%d subdev_name=%s subdev_avail=%d subdev_num=%d stream=%s subclass=%s \n",
445 snd_pcm_info_get_device(info
),
446 snd_pcm_info_get_id(info
),
447 snd_pcm_info_get_name(info
),
448 snd_pcm_info_get_subdevice(info
),
449 snd_pcm_info_get_subdevice_name(info
),
450 snd_pcm_info_get_subdevices_avail(info
),
451 snd_pcm_info_get_subdevices_count(info
),
452 snd_pcm_stream_name(snd_pcm_info_get_stream(info
)),
453 (snd_pcm_info_get_subclass(info
) == SND_PCM_SUBCLASS_GENERIC_MIX
? "GENERIC MIX": "MULTI MIX"));
455 EXIT_ON_ERROR( snd_pcm_hw_params_any(h
, hw_params
) , "pcm hw params" );
459 ALSA_TraceParameters(hw_params
, NULL
, TRUE
);
462 snd_pcm_format_mask_t
* fmask
;
463 int ratemin
= snd_pcm_hw_params_get_rate_min(hw_params
, 0);
464 int ratemax
= snd_pcm_hw_params_get_rate_max(hw_params
, 0);
465 int chmin
= snd_pcm_hw_params_get_channels_min(hw_params
); \
466 int chmax
= snd_pcm_hw_params_get_channels_max(hw_params
); \
468 snd_pcm_format_mask_alloca(&fmask
);
469 snd_pcm_hw_params_get_format_mask(hw_params
, fmask
);
472 if ( (r) >= ratemin && ( (r) <= ratemax || ratemax == -1) ) \
474 if (snd_pcm_format_mask_test( fmask, SND_PCM_FORMAT_U8)) \
476 if (chmin <= 1 && 1 <= chmax) \
477 wwo->caps.dwFormats |= WAVE_FORMAT_##v##S08; \
478 if (chmin <= 2 && 2 <= chmax) \
479 wwo->caps.dwFormats |= WAVE_FORMAT_##v##S08; \
481 if (snd_pcm_format_mask_test( fmask, SND_PCM_FORMAT_S16_LE)) \
483 if (chmin <= 1 && 1 <= chmax) \
484 wwo->caps.dwFormats |= WAVE_FORMAT_##v##S16; \
485 if (chmin <= 2 && 2 <= chmax) \
486 wwo->caps.dwFormats |= WAVE_FORMAT_##v##S16; \
495 if ( snd_pcm_hw_params_get_channels_min(hw_params
) > 1) FIXME("-\n");
496 wwo
->caps
.wChannels
= (snd_pcm_hw_params_get_channels_max(hw_params
) >= 2) ? 2 : 1;
497 if (snd_pcm_hw_params_get_channels_min(hw_params
) <= 2 && 2 <= snd_pcm_hw_params_get_channels_max(hw_params
))
498 wwo
->caps
.dwSupport
|= WAVECAPS_LRVOLUME
;
500 /* FIXME: always true ? */
501 wwo
->caps
.dwSupport
|= WAVECAPS_SAMPLEACCURATE
;
504 snd_pcm_access_mask_t
* acmask
;
505 snd_pcm_access_mask_alloca(&acmask
);
506 snd_pcm_hw_params_get_access_mask(hw_params
, acmask
);
508 /* FIXME: NONITERLEAVED and COMPLEX are not supported right now */
509 if ( snd_pcm_access_mask_test( acmask
, SND_PCM_ACCESS_MMAP_INTERLEAVED
) )
510 wwo
->caps
.dwSupport
|= WAVECAPS_DIRECTSOUND
;
513 TRACE("Configured with dwFmts=%08lx dwSupport=%08lx\n",
514 wwo
->caps
.dwFormats
, wwo
->caps
.dwSupport
);
518 ALSA_InitializeVolumeCtl(wwo
);
523 /******************************************************************
524 * ALSA_InitRingMessage
526 * Initialize the ring of messages for passing between driver's caller and playback/record
529 static int ALSA_InitRingMessage(ALSA_MSG_RING
* omr
)
533 omr
->msg_event
= CreateEventA(NULL
, FALSE
, FALSE
, NULL
);
534 omr
->ring_buffer_size
= ALSA_RING_BUFFER_INCREMENT
;
535 omr
->messages
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,omr
->ring_buffer_size
* sizeof(ALSA_MSG
));
537 InitializeCriticalSection(&omr
->msg_crst
);
541 /******************************************************************
542 * ALSA_DestroyRingMessage
545 static int ALSA_DestroyRingMessage(ALSA_MSG_RING
* omr
)
547 CloseHandle(omr
->msg_event
);
548 HeapFree(GetProcessHeap(),0,omr
->messages
);
549 DeleteCriticalSection(&omr
->msg_crst
);
553 /******************************************************************
554 * ALSA_AddRingMessage
556 * Inserts a new message into the ring (should be called from DriverProc derivated routines)
558 static int ALSA_AddRingMessage(ALSA_MSG_RING
* omr
, enum win_wm_message msg
, DWORD param
, BOOL wait
)
560 HANDLE hEvent
= INVALID_HANDLE_VALUE
;
562 EnterCriticalSection(&omr
->msg_crst
);
563 if ((omr
->msg_toget
== ((omr
->msg_tosave
+ 1) % omr
->ring_buffer_size
)))
565 omr
->ring_buffer_size
+= ALSA_RING_BUFFER_INCREMENT
;
566 TRACE("omr->ring_buffer_size=%d\n",omr
->ring_buffer_size
);
567 omr
->messages
= HeapReAlloc(GetProcessHeap(),0,omr
->messages
, omr
->ring_buffer_size
* sizeof(ALSA_MSG
));
571 hEvent
= CreateEventA(NULL
, FALSE
, FALSE
, NULL
);
572 if (hEvent
== INVALID_HANDLE_VALUE
)
574 ERR("can't create event !?\n");
575 LeaveCriticalSection(&omr
->msg_crst
);
578 if (omr
->msg_toget
!= omr
->msg_tosave
&& omr
->messages
[omr
->msg_toget
].msg
!= WINE_WM_HEADER
)
579 FIXME("two fast messages in the queue!!!!\n");
581 /* fast messages have to be added at the start of the queue */
582 omr
->msg_toget
= (omr
->msg_toget
+ omr
->ring_buffer_size
- 1) % omr
->ring_buffer_size
;
584 omr
->messages
[omr
->msg_toget
].msg
= msg
;
585 omr
->messages
[omr
->msg_toget
].param
= param
;
586 omr
->messages
[omr
->msg_toget
].hEvent
= hEvent
;
590 omr
->messages
[omr
->msg_tosave
].msg
= msg
;
591 omr
->messages
[omr
->msg_tosave
].param
= param
;
592 omr
->messages
[omr
->msg_tosave
].hEvent
= INVALID_HANDLE_VALUE
;
593 omr
->msg_tosave
= (omr
->msg_tosave
+ 1) % omr
->ring_buffer_size
;
595 LeaveCriticalSection(&omr
->msg_crst
);
596 /* signal a new message */
597 SetEvent(omr
->msg_event
);
600 /* wait for playback/record thread to have processed the message */
601 WaitForSingleObject(hEvent
, INFINITE
);
607 /******************************************************************
608 * ALSA_RetrieveRingMessage
610 * Get a message from the ring. Should be called by the playback/record thread.
612 static int ALSA_RetrieveRingMessage(ALSA_MSG_RING
* omr
,
613 enum win_wm_message
*msg
, DWORD
*param
, HANDLE
*hEvent
)
615 EnterCriticalSection(&omr
->msg_crst
);
617 if (omr
->msg_toget
== omr
->msg_tosave
) /* buffer empty ? */
619 LeaveCriticalSection(&omr
->msg_crst
);
623 *msg
= omr
->messages
[omr
->msg_toget
].msg
;
624 omr
->messages
[omr
->msg_toget
].msg
= 0;
625 *param
= omr
->messages
[omr
->msg_toget
].param
;
626 *hEvent
= omr
->messages
[omr
->msg_toget
].hEvent
;
627 omr
->msg_toget
= (omr
->msg_toget
+ 1) % omr
->ring_buffer_size
;
628 LeaveCriticalSection(&omr
->msg_crst
);
632 /*======================================================================*
633 * Low level WAVE OUT implementation *
634 *======================================================================*/
637 /**************************************************************************
638 * wodNotifyClient [internal]
640 static DWORD
wodNotifyClient(WINE_WAVEOUT
* wwo
, WORD wMsg
, DWORD dwParam1
, DWORD dwParam2
)
642 TRACE("wMsg = 0x%04x dwParm1 = %04lX dwParam2 = %04lX\n", wMsg
, dwParam1
, dwParam2
);
648 if (wwo
->wFlags
!= DCB_NULL
&&
649 !DriverCallback(wwo
->waveDesc
.dwCallback
, wwo
->wFlags
, (HDRVR
)wwo
->waveDesc
.hWave
,
650 wMsg
, wwo
->waveDesc
.dwInstance
, dwParam1
, dwParam2
)) {
651 WARN("can't notify client !\n");
652 return MMSYSERR_ERROR
;
656 FIXME("Unknown callback message %u\n", wMsg
);
657 return MMSYSERR_INVALPARAM
;
659 return MMSYSERR_NOERROR
;
662 /**************************************************************************
663 * wodUpdatePlayedTotal [internal]
666 static BOOL
wodUpdatePlayedTotal(WINE_WAVEOUT
* wwo
, snd_pcm_status_t
* ps
)
671 /**************************************************************************
672 * wodPlayer_BeginWaveHdr [internal]
674 * Makes the specified lpWaveHdr the currently playing wave header.
675 * If the specified wave header is a begin loop and we're not already in
676 * a loop, setup the loop.
678 static void wodPlayer_BeginWaveHdr(WINE_WAVEOUT
* wwo
, LPWAVEHDR lpWaveHdr
)
680 wwo
->lpPlayPtr
= lpWaveHdr
;
682 if (!lpWaveHdr
) return;
684 wwo
->lpPlayPtr
->reserved
= 0;
686 if (lpWaveHdr
->dwFlags
& WHDR_BEGINLOOP
) {
687 if (wwo
->lpLoopPtr
) {
688 WARN("Already in a loop. Discarding loop on this header (%p)\n", lpWaveHdr
);
690 TRACE("Starting loop (%ldx) with %p\n", lpWaveHdr
->dwLoops
, lpWaveHdr
);
691 wwo
->lpLoopPtr
= lpWaveHdr
;
692 /* Windows does not touch WAVEHDR.dwLoops,
693 * so we need to make an internal copy */
694 wwo
->dwLoops
= lpWaveHdr
->dwLoops
;
699 /**************************************************************************
700 * wodPlayer_PlayPtrNext [internal]
702 * Advance the play pointer to the next waveheader, looping if required.
704 static LPWAVEHDR
wodPlayer_PlayPtrNext(WINE_WAVEOUT
* wwo
)
706 LPWAVEHDR lpWaveHdr
= wwo
->lpPlayPtr
;
708 if ((lpWaveHdr
->dwFlags
& WHDR_ENDLOOP
) && wwo
->lpLoopPtr
) {
709 /* We're at the end of a loop, loop if required */
710 if (--wwo
->dwLoops
> 0) {
711 wwo
->lpPlayPtr
= wwo
->lpLoopPtr
;
712 wwo
->lpPlayPtr
->reserved
= 0;
714 /* Handle overlapping loops correctly */
715 if (wwo
->lpLoopPtr
!= lpWaveHdr
&& (lpWaveHdr
->dwFlags
& WHDR_BEGINLOOP
)) {
716 FIXME("Correctly handled case ? (ending loop buffer also starts a new loop)\n");
717 /* shall we consider the END flag for the closing loop or for
718 * the opening one or for both ???
719 * code assumes for closing loop only
722 lpWaveHdr
= lpWaveHdr
->lpNext
;
724 wwo
->lpLoopPtr
= NULL
;
725 wodPlayer_BeginWaveHdr(wwo
, lpWaveHdr
);
728 /* We're not in a loop. Advance to the next wave header */
729 wodPlayer_BeginWaveHdr(wwo
, lpWaveHdr
= lpWaveHdr
->lpNext
);
735 /**************************************************************************
736 * wodPlayer_DSPWait [internal]
737 * Returns the number of milliseconds to wait for the DSP buffer to play a
740 static DWORD
wodPlayer_DSPWait(const WINE_WAVEOUT
*wwo
)
742 /* time for one period to be played */
743 return snd_pcm_hw_params_get_period_time(wwo
->hw_params
, 0) / 1000;
746 /**************************************************************************
747 * wodPllayer_NotifyWait [internal]
748 * Returns the number of milliseconds to wait before attempting to notify
749 * completion of the specified wavehdr.
750 * This is based on the number of bytes remaining to be written in the
753 static DWORD
wodPlayer_NotifyWait(const WINE_WAVEOUT
* wwo
, LPWAVEHDR lpWaveHdr
)
757 dwMillis
= (lpWaveHdr
->dwBufferLength
- lpWaveHdr
->reserved
) * 1000 / wwo
->format
.wf
.nAvgBytesPerSec
;
758 if (!dwMillis
) dwMillis
= 1;
764 /**************************************************************************
765 * wodPlayer_WriteMaxFrags [internal]
766 * Writes the maximum number of frames possible to the DSP and returns
767 * the number of frames written.
769 static int wodPlayer_WriteMaxFrags(WINE_WAVEOUT
* wwo
, DWORD
* frames
)
771 /* Only attempt to write to free frames */
772 LPWAVEHDR lpWaveHdr
= wwo
->lpPlayPtr
;
773 DWORD dwLength
= snd_pcm_bytes_to_frames(wwo
->p_handle
, lpWaveHdr
->dwBufferLength
- lpWaveHdr
->reserved
);
774 int toWrite
= min(dwLength
, *frames
);
777 TRACE("Writing wavehdr %p.%lu[%lu]\n", lpWaveHdr
, lpWaveHdr
->reserved
, lpWaveHdr
->dwBufferLength
);
779 written
= (wwo
->write
)(wwo
->p_handle
, lpWaveHdr
->lpData
+ lpWaveHdr
->reserved
, toWrite
);
782 /* XRUN occurred. let's try to recover */
783 ALSA_XRUNRecovery(wwo
, written
);
784 written
= (wwo
->write
)(wwo
->p_handle
, lpWaveHdr
->lpData
+ lpWaveHdr
->reserved
, toWrite
);
789 ERR("Error in writing wavehdr. Reason: %s\n", snd_strerror(written
));
793 lpWaveHdr
->reserved
+= snd_pcm_frames_to_bytes(wwo
->p_handle
, written
);
794 if ( lpWaveHdr
->reserved
>= lpWaveHdr
->dwBufferLength
) {
795 /* this will be used to check if the given wave header has been fully played or not... */
796 lpWaveHdr
->reserved
= lpWaveHdr
->dwBufferLength
;
797 /* If we wrote all current wavehdr, skip to the next one */
798 wodPlayer_PlayPtrNext(wwo
);
801 wwo
->dwPlayedTotal
+= snd_pcm_frames_to_bytes(wwo
->p_handle
, written
);
807 /**************************************************************************
808 * wodPlayer_NotifyCompletions [internal]
810 * Notifies and remove from queue all wavehdrs which have been played to
811 * the speaker (ie. they have cleared the ALSA buffer). If force is true,
812 * we notify all wavehdrs and remove them all from the queue even if they
813 * are unplayed or part of a loop.
815 static DWORD
wodPlayer_NotifyCompletions(WINE_WAVEOUT
* wwo
, BOOL force
)
819 /* Start from lpQueuePtr and keep notifying until:
820 * - we hit an unwritten wavehdr
821 * - we hit the beginning of a running loop
822 * - we hit a wavehdr which hasn't finished playing
824 while ((lpWaveHdr
= wwo
->lpQueuePtr
) &&
826 (lpWaveHdr
!= wwo
->lpPlayPtr
&&
827 lpWaveHdr
!= wwo
->lpLoopPtr
&&
828 lpWaveHdr
->reserved
== lpWaveHdr
->dwBufferLength
))) {
830 wwo
->lpQueuePtr
= lpWaveHdr
->lpNext
;
832 lpWaveHdr
->dwFlags
&= ~WHDR_INQUEUE
;
833 lpWaveHdr
->dwFlags
|= WHDR_DONE
;
835 wodNotifyClient(wwo
, WOM_DONE
, (DWORD
)lpWaveHdr
, 0);
837 return (lpWaveHdr
&& lpWaveHdr
!= wwo
->lpPlayPtr
&& lpWaveHdr
!= wwo
->lpLoopPtr
) ?
838 wodPlayer_NotifyWait(wwo
, lpWaveHdr
) : INFINITE
;
842 void wait_for_poll(snd_pcm_t
*handle
, struct pollfd
*ufds
, unsigned int count
)
844 unsigned short revents
;
847 poll(ufds
, count
, -1);
848 snd_pcm_poll_descriptors_revents(handle
, ufds
, count
, &revents
);
850 if (revents
& POLLERR
)
853 /*if (revents & POLLOUT)
859 /**************************************************************************
860 * wodPlayer_Reset [internal]
862 * wodPlayer helper. Resets current output stream.
864 static void wodPlayer_Reset(WINE_WAVEOUT
* wwo
)
866 enum win_wm_message msg
;
871 /* flush all possible output */
872 wait_for_poll(wwo
->p_handle
, wwo
->ufds
, wwo
->count
);
874 /* updates current notify list */
875 wodPlayer_NotifyCompletions(wwo
, FALSE
);
877 if ( (err
= snd_pcm_drop(wwo
->p_handle
)) < 0) {
878 FIXME("flush: %s\n", snd_strerror(err
));
880 wwo
->state
= WINE_WS_STOPPED
;
883 if ( (err
= snd_pcm_prepare(wwo
->p_handle
)) < 0 )
884 ERR("pcm prepare failed: %s\n", snd_strerror(err
));
886 /* remove any buffer */
887 wodPlayer_NotifyCompletions(wwo
, TRUE
);
889 wwo
->lpPlayPtr
= wwo
->lpQueuePtr
= wwo
->lpLoopPtr
= NULL
;
890 wwo
->state
= WINE_WS_STOPPED
;
892 /* remove any existing message in the ring */
893 EnterCriticalSection(&wwo
->msgRing
.msg_crst
);
894 /* return all pending headers in queue */
895 while (ALSA_RetrieveRingMessage(&wwo
->msgRing
, &msg
, ¶m
, &ev
))
897 if (msg
!= WINE_WM_HEADER
)
899 FIXME("shouldn't have headers left\n");
903 ((LPWAVEHDR
)param
)->dwFlags
&= ~WHDR_INQUEUE
;
904 ((LPWAVEHDR
)param
)->dwFlags
|= WHDR_DONE
;
906 wodNotifyClient(wwo
, WOM_DONE
, param
, 0);
908 ResetEvent(wwo
->msgRing
.msg_event
);
909 LeaveCriticalSection(&wwo
->msgRing
.msg_crst
);
912 /**************************************************************************
913 * wodPlayer_ProcessMessages [internal]
915 static void wodPlayer_ProcessMessages(WINE_WAVEOUT
* wwo
)
918 enum win_wm_message msg
;
923 while (ALSA_RetrieveRingMessage(&wwo
->msgRing
, &msg
, ¶m
, &ev
)) {
924 /* TRACE("Received %s %lx\n", wodPlayerCmdString[msg - WM_USER - 1], param); */
927 case WINE_WM_PAUSING
:
928 if ( snd_pcm_state(wwo
->p_handle
) == SND_PCM_STATE_RUNNING
)
930 err
= snd_pcm_pause(wwo
->p_handle
, 1);
932 ERR("pcm_pause failed: %s\n", snd_strerror(err
));
934 wwo
->state
= WINE_WS_PAUSED
;
937 case WINE_WM_RESTARTING
:
938 if (wwo
->state
== WINE_WS_PAUSED
)
940 if ( snd_pcm_state(wwo
->p_handle
) == SND_PCM_STATE_PAUSED
)
942 err
= snd_pcm_pause(wwo
->p_handle
, 0);
944 ERR("pcm_pause failed: %s\n", snd_strerror(err
));
946 wwo
->state
= WINE_WS_PLAYING
;
951 lpWaveHdr
= (LPWAVEHDR
)param
;
953 /* insert buffer at the end of queue */
956 for (wh
= &(wwo
->lpQueuePtr
); *wh
; wh
= &((*wh
)->lpNext
));
960 wodPlayer_BeginWaveHdr(wwo
,lpWaveHdr
);
961 if (wwo
->state
== WINE_WS_STOPPED
)
962 wwo
->state
= WINE_WS_PLAYING
;
964 case WINE_WM_RESETTING
:
965 wodPlayer_Reset(wwo
);
969 wodUpdatePlayedTotal(wwo
, NULL
);
972 case WINE_WM_BREAKLOOP
:
973 if (wwo
->state
== WINE_WS_PLAYING
&& wwo
->lpLoopPtr
!= NULL
) {
974 /* ensure exit at end of current loop */
979 case WINE_WM_CLOSING
:
980 /* sanity check: this should not happen since the device must have been reset before */
981 if (wwo
->lpQueuePtr
|| wwo
->lpPlayPtr
) ERR("out of sync\n");
983 wwo
->state
= WINE_WS_CLOSED
;
986 /* shouldn't go here */
988 FIXME("unknown message %d\n", msg
);
994 /**************************************************************************
995 * wodPlayer_FeedDSP [internal]
996 * Feed as much sound data as we can into the DSP and return the number of
997 * milliseconds before it will be necessary to feed the DSP again.
999 static DWORD
wodPlayer_FeedDSP(WINE_WAVEOUT
* wwo
)
1001 DWORD availInQ
= snd_pcm_avail_update(wwo
->p_handle
);
1003 /* no more room... no need to try to feed */
1004 while (wwo
->lpPlayPtr
&& availInQ
> 0)
1005 if ( wodPlayer_WriteMaxFrags(wwo
, &availInQ
) < 0 )
1008 return wodPlayer_DSPWait(wwo
);
1011 /**************************************************************************
1012 * wodPlayer [internal]
1014 static DWORD CALLBACK
wodPlayer(LPVOID pmt
)
1016 WORD uDevID
= (DWORD
)pmt
;
1017 WINE_WAVEOUT
* wwo
= (WINE_WAVEOUT
*)&WOutDev
[uDevID
];
1018 DWORD dwNextFeedTime
= INFINITE
; /* Time before DSP needs feeding */
1019 DWORD dwNextNotifyTime
= INFINITE
; /* Time before next wave completion */
1022 wwo
->state
= WINE_WS_STOPPED
;
1023 SetEvent(wwo
->hStartUpEvent
);
1026 /** Wait for the shortest time before an action is required. If there
1027 * are no pending actions, wait forever for a command.
1029 dwSleepTime
= min(dwNextFeedTime
, dwNextNotifyTime
);
1030 TRACE("waiting %lums (%lu,%lu)\n", dwSleepTime
, dwNextFeedTime
, dwNextNotifyTime
);
1031 WaitForSingleObject(wwo
->msgRing
.msg_event
, dwSleepTime
);
1032 wodPlayer_ProcessMessages(wwo
);
1033 if (wwo
->state
== WINE_WS_PLAYING
) {
1034 dwNextFeedTime
= wodPlayer_FeedDSP(wwo
);
1035 dwNextNotifyTime
= wodPlayer_NotifyCompletions(wwo
, FALSE
);
1037 dwNextFeedTime
= dwNextNotifyTime
= INFINITE
;
1042 /**************************************************************************
1043 * wodGetDevCaps [internal]
1045 static DWORD
wodGetDevCaps(WORD wDevID
, LPWAVEOUTCAPSA lpCaps
, DWORD dwSize
)
1047 TRACE("(%u, %p, %lu);\n", wDevID
, lpCaps
, dwSize
);
1049 if (lpCaps
== NULL
) return MMSYSERR_NOTENABLED
;
1051 if (wDevID
>= MAX_WAVEOUTDRV
) {
1052 TRACE("MAX_WAVOUTDRV reached !\n");
1053 return MMSYSERR_BADDEVICEID
;
1056 memcpy(lpCaps
, &WOutDev
[wDevID
].caps
, min(dwSize
, sizeof(*lpCaps
)));
1057 return MMSYSERR_NOERROR
;
1060 /**************************************************************************
1061 * wodOpen [internal]
1063 static DWORD
wodOpen(WORD wDevID
, LPWAVEOPENDESC lpDesc
, DWORD dwFlags
)
1066 snd_pcm_hw_params_t
* hw_params
;
1067 snd_pcm_sw_params_t
* sw_params
;
1068 snd_pcm_access_t access
;
1069 snd_pcm_format_t format
;
1071 unsigned int buffer_time
= 500000;
1072 unsigned int period_time
= 10000;
1074 snd_pcm_uframes_t period_size
;
1079 snd_pcm_hw_params_alloca(&hw_params
);
1080 snd_pcm_sw_params_alloca(&sw_params
);
1082 TRACE("(%u, %p, %08lX);\n", wDevID
, lpDesc
, dwFlags
);
1083 if (lpDesc
== NULL
) {
1084 WARN("Invalid Parameter !\n");
1085 return MMSYSERR_INVALPARAM
;
1087 if (wDevID
>= MAX_WAVEOUTDRV
) {
1088 TRACE("MAX_WAVOUTDRV reached !\n");
1089 return MMSYSERR_BADDEVICEID
;
1092 /* only PCM format is supported so far... */
1093 if (lpDesc
->lpFormat
->wFormatTag
!= WAVE_FORMAT_PCM
||
1094 lpDesc
->lpFormat
->nChannels
== 0 ||
1095 lpDesc
->lpFormat
->nSamplesPerSec
== 0) {
1096 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
1097 lpDesc
->lpFormat
->wFormatTag
, lpDesc
->lpFormat
->nChannels
,
1098 lpDesc
->lpFormat
->nSamplesPerSec
);
1099 return WAVERR_BADFORMAT
;
1102 if (dwFlags
& WAVE_FORMAT_QUERY
) {
1103 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
1104 lpDesc
->lpFormat
->wFormatTag
, lpDesc
->lpFormat
->nChannels
,
1105 lpDesc
->lpFormat
->nSamplesPerSec
);
1106 return MMSYSERR_NOERROR
;
1109 wwo
= &WOutDev
[wDevID
];
1111 if ((dwFlags
& WAVE_DIRECTSOUND
) && !(wwo
->caps
.dwSupport
& WAVECAPS_DIRECTSOUND
))
1112 /* not supported, ignore it */
1113 dwFlags
&= ~WAVE_DIRECTSOUND
;
1116 flags
= SND_PCM_NONBLOCK
;
1117 if ( dwFlags
& WAVE_DIRECTSOUND
)
1118 flags
|= SND_PCM_ASYNC
;
1120 if (snd_pcm_open(&pcm
, wwo
->device
, SND_PCM_STREAM_PLAYBACK
, dwFlags
))
1122 ERR("Error open: %s\n", snd_strerror(errno
));
1123 return MMSYSERR_NOTENABLED
;
1126 wwo
->wFlags
= HIWORD(dwFlags
& CALLBACK_TYPEMASK
);
1128 memcpy(&wwo
->waveDesc
, lpDesc
, sizeof(WAVEOPENDESC
));
1129 memcpy(&wwo
->format
, lpDesc
->lpFormat
, sizeof(PCMWAVEFORMAT
));
1131 if (wwo
->format
.wBitsPerSample
== 0) {
1132 WARN("Resetting zeroed wBitsPerSample\n");
1133 wwo
->format
.wBitsPerSample
= 8 *
1134 (wwo
->format
.wf
.nAvgBytesPerSec
/
1135 wwo
->format
.wf
.nSamplesPerSec
) /
1136 wwo
->format
.wf
.nChannels
;
1139 snd_pcm_hw_params_any(pcm
, hw_params
);
1141 #define EXIT_ON_ERROR(f,e,txt) do \
1144 if ( (err = (f) ) < 0) \
1146 ERR(txt ": %s\n", snd_strerror(err)); \
1147 snd_pcm_close(pcm); \
1152 access
= SND_PCM_ACCESS_MMAP_INTERLEAVED
;
1153 if ( ( err
= snd_pcm_hw_params_set_access(pcm
, hw_params
, access
) ) < 0) {
1154 WARN("mmap not available. switching to standard write.\n");
1155 access
= SND_PCM_ACCESS_RW_INTERLEAVED
;
1156 EXIT_ON_ERROR( snd_pcm_hw_params_set_access(pcm
, hw_params
, access
), MMSYSERR_INVALPARAM
, "unable to set access for playback");
1157 wwo
->write
= snd_pcm_writei
;
1160 wwo
->write
= snd_pcm_mmap_writei
;
1162 EXIT_ON_ERROR( snd_pcm_hw_params_set_channels(pcm
, hw_params
, wwo
->format
.wf
.nChannels
), MMSYSERR_INVALPARAM
, "unable to set required channels");
1164 format
= (wwo
->format
.wBitsPerSample
== 16) ? SND_PCM_FORMAT_S16_LE
: SND_PCM_FORMAT_U8
;
1165 EXIT_ON_ERROR( snd_pcm_hw_params_set_format(pcm
, hw_params
, format
), MMSYSERR_INVALPARAM
, "unable to set required format");
1167 rate
= snd_pcm_hw_params_set_rate_near(pcm
, hw_params
, wwo
->format
.wf
.nSamplesPerSec
, 0);
1169 ERR("Rate %ld Hz not available for playback: %s\n", wwo
->format
.wf
.nSamplesPerSec
, snd_strerror(rate
));
1171 return WAVERR_BADFORMAT
;
1173 if (rate
!= wwo
->format
.wf
.nSamplesPerSec
) {
1174 ERR("Rate doesn't match (requested %ld Hz, got %d Hz)\n", wwo
->format
.wf
.nSamplesPerSec
, rate
);
1176 return WAVERR_BADFORMAT
;
1179 EXIT_ON_ERROR( snd_pcm_hw_params_set_buffer_time_near(pcm
, hw_params
, buffer_time
, 0), MMSYSERR_INVALPARAM
, "unable to set buffer time");
1180 EXIT_ON_ERROR( snd_pcm_hw_params_set_period_time_near(pcm
, hw_params
, period_time
, 0), MMSYSERR_INVALPARAM
, "unable to set period time");
1182 EXIT_ON_ERROR( snd_pcm_hw_params(pcm
, hw_params
), MMSYSERR_INVALPARAM
, "unable to set hw params for playback");
1184 period_size
= snd_pcm_hw_params_get_period_size(hw_params
, 0);
1185 buffer_size
= snd_pcm_hw_params_get_buffer_size(hw_params
);
1187 snd_pcm_sw_params_current(pcm
, sw_params
);
1188 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");
1189 EXIT_ON_ERROR( snd_pcm_sw_params_set_silence_size(pcm
, sw_params
, 0), MMSYSERR_ERROR
, "unable to set silence size");
1190 EXIT_ON_ERROR( snd_pcm_sw_params_set_avail_min(pcm
, sw_params
, period_size
), MMSYSERR_ERROR
, "unable to set avail min");
1191 EXIT_ON_ERROR( snd_pcm_sw_params_set_xfer_align(pcm
, sw_params
, 1), MMSYSERR_ERROR
, "unable to set xfer align");
1192 EXIT_ON_ERROR( snd_pcm_sw_params_set_silence_threshold(pcm
, sw_params
, 0), MMSYSERR_ERROR
, "unable to set silence threshold");
1193 EXIT_ON_ERROR( snd_pcm_sw_params(pcm
, sw_params
), MMSYSERR_ERROR
, "unable to set sw params for playback");
1194 #undef EXIT_ON_ERROR
1196 snd_pcm_prepare(pcm
);
1199 ALSA_TraceParameters(hw_params
, sw_params
, FALSE
);
1201 /* now, we can save all required data for later use... */
1202 if ( wwo
->hw_params
)
1203 snd_pcm_hw_params_free(wwo
->hw_params
);
1204 snd_pcm_hw_params_malloc(&(wwo
->hw_params
));
1205 snd_pcm_hw_params_copy(wwo
->hw_params
, hw_params
);
1207 wwo
->dwBufferSize
= buffer_size
;
1208 wwo
->lpQueuePtr
= wwo
->lpPlayPtr
= wwo
->lpLoopPtr
= NULL
;
1209 wwo
->p_handle
= pcm
;
1211 ALSA_InitRingMessage(&wwo
->msgRing
);
1213 wwo
->count
= snd_pcm_poll_descriptors_count (wwo
->p_handle
);
1214 if (wwo
->count
<= 0) {
1215 ERR("Invalid poll descriptors count\n");
1216 return MMSYSERR_ERROR
;
1219 wwo
->ufds
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
, sizeof(struct pollfd
) * wwo
->count
);
1220 if (wwo
->ufds
== NULL
) {
1221 ERR("No enough memory\n");
1222 return MMSYSERR_NOMEM
;
1224 if ((err
= snd_pcm_poll_descriptors(wwo
->p_handle
, wwo
->ufds
, wwo
->count
)) < 0) {
1225 ERR("Unable to obtain poll descriptors for playback: %s\n", snd_strerror(err
));
1226 return MMSYSERR_ERROR
;
1229 if (!(dwFlags
& WAVE_DIRECTSOUND
)) {
1230 wwo
->hStartUpEvent
= CreateEventA(NULL
, FALSE
, FALSE
, NULL
);
1231 wwo
->hThread
= CreateThread(NULL
, 0, wodPlayer
, (LPVOID
)(DWORD
)wDevID
, 0, &(wwo
->dwThreadID
));
1232 WaitForSingleObject(wwo
->hStartUpEvent
, INFINITE
);
1233 CloseHandle(wwo
->hStartUpEvent
);
1235 wwo
->hThread
= INVALID_HANDLE_VALUE
;
1236 wwo
->dwThreadID
= 0;
1238 wwo
->hStartUpEvent
= INVALID_HANDLE_VALUE
;
1240 TRACE("handle=%08lx \n", (DWORD
)wwo
->p_handle
);
1241 /* if (wwo->dwFragmentSize % wwo->format.wf.nBlockAlign)
1242 ERR("Fragment doesn't contain an integral number of data blocks\n");
1244 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%lu, nSamplesPerSec=%lu, nChannels=%u nBlockAlign=%u!\n",
1245 wwo
->format
.wBitsPerSample
, wwo
->format
.wf
.nAvgBytesPerSec
,
1246 wwo
->format
.wf
.nSamplesPerSec
, wwo
->format
.wf
.nChannels
,
1247 wwo
->format
.wf
.nBlockAlign
);
1249 return wodNotifyClient(wwo
, WOM_OPEN
, 0L, 0L);
1253 /**************************************************************************
1254 * wodClose [internal]
1256 static DWORD
wodClose(WORD wDevID
)
1258 DWORD ret
= MMSYSERR_NOERROR
;
1261 TRACE("(%u);\n", wDevID
);
1263 if (wDevID
>= MAX_WAVEOUTDRV
|| WOutDev
[wDevID
].p_handle
== NULL
) {
1264 WARN("bad device ID !\n");
1265 return MMSYSERR_BADDEVICEID
;
1268 wwo
= &WOutDev
[wDevID
];
1269 if (wwo
->lpQueuePtr
) {
1270 WARN("buffers still playing !\n");
1271 ret
= WAVERR_STILLPLAYING
;
1273 if (wwo
->hThread
!= INVALID_HANDLE_VALUE
) {
1274 ALSA_AddRingMessage(&wwo
->msgRing
, WINE_WM_CLOSING
, 0, TRUE
);
1276 ALSA_DestroyRingMessage(&wwo
->msgRing
);
1278 snd_pcm_hw_params_free(wwo
->hw_params
);
1279 wwo
->hw_params
= NULL
;
1281 snd_pcm_close(wwo
->p_handle
);
1282 wwo
->p_handle
= NULL
;
1284 ret
= wodNotifyClient(wwo
, WOM_CLOSE
, 0L, 0L);
1287 HeapFree(GetProcessHeap(), 0, wwo
->ufds
);
1292 /**************************************************************************
1293 * wodWrite [internal]
1296 static DWORD
wodWrite(WORD wDevID
, LPWAVEHDR lpWaveHdr
, DWORD dwSize
)
1298 TRACE("(%u, %p, %08lX);\n", wDevID
, lpWaveHdr
, dwSize
);
1300 /* first, do the sanity checks... */
1301 if (wDevID
>= MAX_WAVEOUTDRV
|| WOutDev
[wDevID
].p_handle
== NULL
) {
1302 WARN("bad dev ID !\n");
1303 return MMSYSERR_BADDEVICEID
;
1306 if (lpWaveHdr
->lpData
== NULL
|| !(lpWaveHdr
->dwFlags
& WHDR_PREPARED
))
1307 return WAVERR_UNPREPARED
;
1309 if (lpWaveHdr
->dwFlags
& WHDR_INQUEUE
)
1310 return WAVERR_STILLPLAYING
;
1312 lpWaveHdr
->dwFlags
&= ~WHDR_DONE
;
1313 lpWaveHdr
->dwFlags
|= WHDR_INQUEUE
;
1314 lpWaveHdr
->lpNext
= 0;
1316 ALSA_AddRingMessage(&WOutDev
[wDevID
].msgRing
, WINE_WM_HEADER
, (DWORD
)lpWaveHdr
, FALSE
);
1318 return MMSYSERR_NOERROR
;
1321 /**************************************************************************
1322 * wodPrepare [internal]
1324 static DWORD
wodPrepare(WORD wDevID
, LPWAVEHDR lpWaveHdr
, DWORD dwSize
)
1326 TRACE("(%u, %p, %08lX);\n", wDevID
, lpWaveHdr
, dwSize
);
1328 if (wDevID
>= MAX_WAVEOUTDRV
) {
1329 WARN("bad device ID !\n");
1330 return MMSYSERR_BADDEVICEID
;
1333 if (lpWaveHdr
->dwFlags
& WHDR_INQUEUE
)
1334 return WAVERR_STILLPLAYING
;
1336 lpWaveHdr
->dwFlags
|= WHDR_PREPARED
;
1337 lpWaveHdr
->dwFlags
&= ~WHDR_DONE
;
1338 return MMSYSERR_NOERROR
;
1341 /**************************************************************************
1342 * wodUnprepare [internal]
1344 static DWORD
wodUnprepare(WORD wDevID
, LPWAVEHDR lpWaveHdr
, DWORD dwSize
)
1346 TRACE("(%u, %p, %08lX);\n", wDevID
, lpWaveHdr
, dwSize
);
1348 if (wDevID
>= MAX_WAVEOUTDRV
) {
1349 WARN("bad device ID !\n");
1350 return MMSYSERR_BADDEVICEID
;
1353 if (lpWaveHdr
->dwFlags
& WHDR_INQUEUE
)
1354 return WAVERR_STILLPLAYING
;
1356 lpWaveHdr
->dwFlags
&= ~WHDR_PREPARED
;
1357 lpWaveHdr
->dwFlags
|= WHDR_DONE
;
1359 return MMSYSERR_NOERROR
;
1362 /**************************************************************************
1363 * wodPause [internal]
1365 static DWORD
wodPause(WORD wDevID
)
1367 TRACE("(%u);!\n", wDevID
);
1369 if (wDevID
>= MAX_WAVEOUTDRV
|| WOutDev
[wDevID
].p_handle
== NULL
) {
1370 WARN("bad device ID !\n");
1371 return MMSYSERR_BADDEVICEID
;
1374 ALSA_AddRingMessage(&WOutDev
[wDevID
].msgRing
, WINE_WM_PAUSING
, 0, TRUE
);
1376 return MMSYSERR_NOERROR
;
1379 /**************************************************************************
1380 * wodRestart [internal]
1382 static DWORD
wodRestart(WORD wDevID
)
1384 TRACE("(%u);\n", wDevID
);
1386 if (wDevID
>= MAX_WAVEOUTDRV
|| WOutDev
[wDevID
].p_handle
== NULL
) {
1387 WARN("bad device ID !\n");
1388 return MMSYSERR_BADDEVICEID
;
1391 if (WOutDev
[wDevID
].state
== WINE_WS_PAUSED
) {
1392 ALSA_AddRingMessage(&WOutDev
[wDevID
].msgRing
, WINE_WM_RESTARTING
, 0, TRUE
);
1395 /* FIXME: is NotifyClient with WOM_DONE right ? (Comet Busters 1.3.3 needs this notification) */
1396 /* FIXME: Myst crashes with this ... hmm -MM
1397 return wodNotifyClient(wwo, WOM_DONE, 0L, 0L);
1400 return MMSYSERR_NOERROR
;
1403 /**************************************************************************
1404 * wodReset [internal]
1406 static DWORD
wodReset(WORD wDevID
)
1408 TRACE("(%u);\n", wDevID
);
1410 if (wDevID
>= MAX_WAVEOUTDRV
|| WOutDev
[wDevID
].p_handle
== NULL
) {
1411 WARN("bad device ID !\n");
1412 return MMSYSERR_BADDEVICEID
;
1415 ALSA_AddRingMessage(&WOutDev
[wDevID
].msgRing
, WINE_WM_RESETTING
, 0, TRUE
);
1417 return MMSYSERR_NOERROR
;
1420 /**************************************************************************
1421 * wodGetPosition [internal]
1423 static DWORD
wodGetPosition(WORD wDevID
, LPMMTIME lpTime
, DWORD uSize
)
1429 TRACE("(%u, %p, %lu);\n", wDevID
, lpTime
, uSize
);
1431 if (wDevID
>= MAX_WAVEOUTDRV
|| WOutDev
[wDevID
].p_handle
== NULL
) {
1432 WARN("bad device ID !\n");
1433 return MMSYSERR_BADDEVICEID
;
1436 if (lpTime
== NULL
) return MMSYSERR_INVALPARAM
;
1438 wwo
= &WOutDev
[wDevID
];
1439 ALSA_AddRingMessage(&wwo
->msgRing
, WINE_WM_UPDATE
, 0, TRUE
);
1440 val
= wwo
->dwPlayedTotal
;
1442 TRACE("wType=%04X wBitsPerSample=%u nSamplesPerSec=%lu nChannels=%u nAvgBytesPerSec=%lu\n",
1443 lpTime
->wType
, wwo
->format
.wBitsPerSample
,
1444 wwo
->format
.wf
.nSamplesPerSec
, wwo
->format
.wf
.nChannels
,
1445 wwo
->format
.wf
.nAvgBytesPerSec
);
1446 TRACE("dwPlayedTotal=%lu\n", val
);
1448 switch (lpTime
->wType
) {
1451 TRACE("TIME_BYTES=%lu\n", lpTime
->u
.cb
);
1454 lpTime
->u
.sample
= val
* 8 / wwo
->format
.wBitsPerSample
/wwo
->format
.wf
.nChannels
;
1455 TRACE("TIME_SAMPLES=%lu\n", lpTime
->u
.sample
);
1458 time
= val
/ (wwo
->format
.wf
.nAvgBytesPerSec
/ 1000);
1459 lpTime
->u
.smpte
.hour
= time
/ 108000;
1460 time
-= lpTime
->u
.smpte
.hour
* 108000;
1461 lpTime
->u
.smpte
.min
= time
/ 1800;
1462 time
-= lpTime
->u
.smpte
.min
* 1800;
1463 lpTime
->u
.smpte
.sec
= time
/ 30;
1464 time
-= lpTime
->u
.smpte
.sec
* 30;
1465 lpTime
->u
.smpte
.frame
= time
;
1466 lpTime
->u
.smpte
.fps
= 30;
1467 TRACE("TIME_SMPTE=%02u:%02u:%02u:%02u\n",
1468 lpTime
->u
.smpte
.hour
, lpTime
->u
.smpte
.min
,
1469 lpTime
->u
.smpte
.sec
, lpTime
->u
.smpte
.frame
);
1472 FIXME("Format %d not supported ! use TIME_MS !\n", lpTime
->wType
);
1473 lpTime
->wType
= TIME_MS
;
1475 lpTime
->u
.ms
= val
/ (wwo
->format
.wf
.nAvgBytesPerSec
/ 1000);
1476 TRACE("TIME_MS=%lu\n", lpTime
->u
.ms
);
1479 return MMSYSERR_NOERROR
;
1482 /**************************************************************************
1483 * wodBreakLoop [internal]
1485 static DWORD
wodBreakLoop(WORD wDevID
)
1487 TRACE("(%u);\n", wDevID
);
1489 if (wDevID
>= MAX_WAVEOUTDRV
|| WOutDev
[wDevID
].p_handle
== NULL
) {
1490 WARN("bad device ID !\n");
1491 return MMSYSERR_BADDEVICEID
;
1493 ALSA_AddRingMessage(&WOutDev
[wDevID
].msgRing
, WINE_WM_BREAKLOOP
, 0, TRUE
);
1494 return MMSYSERR_NOERROR
;
1497 /**************************************************************************
1498 * wodGetVolume [internal]
1500 static DWORD
wodGetVolume(WORD wDevID
, LPDWORD lpdwVol
)
1507 TRACE("(%u, %p);\n", wDevID
, lpdwVol
);
1508 if (wDevID
>= MAX_WAVEOUTDRV
|| WOutDev
[wDevID
].p_handle
== NULL
) {
1509 WARN("bad device ID !\n");
1510 return MMSYSERR_BADDEVICEID
;
1512 wwo
= &WOutDev
[wDevID
];
1513 count
= snd_ctl_elem_info_get_count(wwo
->playback_einfo
);
1514 min
= snd_ctl_elem_info_get_min(wwo
->playback_einfo
);
1515 max
= snd_ctl_elem_info_get_max(wwo
->playback_einfo
);
1517 #define VOLUME_ALSA_TO_WIN(x) (((x)-min) * 65536 /(max-min))
1518 if (lpdwVol
== NULL
)
1519 return MMSYSERR_NOTENABLED
;
1524 left
= VOLUME_ALSA_TO_WIN(snd_ctl_elem_value_get_integer(wwo
->playback_evalue
, 0));
1525 right
= VOLUME_ALSA_TO_WIN(snd_ctl_elem_value_get_integer(wwo
->playback_evalue
, 1));
1528 left
= right
= VOLUME_ALSA_TO_WIN(snd_ctl_elem_value_get_integer(wwo
->playback_evalue
, 0));
1531 WARN("%d channels mixer not supported\n", count
);
1532 return MMSYSERR_NOERROR
;
1534 #undef VOLUME_ALSA_TO_WIN
1536 TRACE("left=%d right=%d !\n", left
, right
);
1537 *lpdwVol
= MAKELONG( left
, right
);
1538 return MMSYSERR_NOERROR
;
1541 /**************************************************************************
1542 * wodSetVolume [internal]
1544 static DWORD
wodSetVolume(WORD wDevID
, DWORD dwParam
)
1551 TRACE("(%u, %08lX);\n", wDevID
, dwParam
);
1552 if (wDevID
>= MAX_WAVEOUTDRV
|| WOutDev
[wDevID
].p_handle
== NULL
) {
1553 WARN("bad device ID !\n");
1554 return MMSYSERR_BADDEVICEID
;
1556 wwo
= &WOutDev
[wDevID
];
1557 count
=snd_ctl_elem_info_get_count(wwo
->playback_einfo
);
1558 min
= snd_ctl_elem_info_get_min(wwo
->playback_einfo
);
1559 max
= snd_ctl_elem_info_get_max(wwo
->playback_einfo
);
1561 left
= LOWORD(dwParam
);
1562 right
= HIWORD(dwParam
);
1564 #define VOLUME_WIN_TO_ALSA(x) ( (((x) * (max-min)) / 65536) + min )
1568 snd_ctl_elem_value_set_integer(wwo
->playback_evalue
, 0, VOLUME_WIN_TO_ALSA(left
));
1569 snd_ctl_elem_value_set_integer(wwo
->playback_evalue
, 1, VOLUME_WIN_TO_ALSA(right
));
1572 snd_ctl_elem_value_set_integer(wwo
->playback_evalue
, 0, VOLUME_WIN_TO_ALSA(left
));
1575 WARN("%d channels mixer not supported\n", count
);
1577 #undef VOLUME_WIN_TO_ALSA
1578 if ( (err
= snd_ctl_elem_write(wwo
->ctl
, wwo
->playback_evalue
)) < 0)
1580 ERR("error writing snd_ctl_elem_value: %s\n", snd_strerror(err
));
1582 return MMSYSERR_NOERROR
;
1585 /**************************************************************************
1586 * wodGetNumDevs [internal]
1588 static DWORD
wodGetNumDevs(void)
1590 return ALSA_WodNumDevs
;
1594 /**************************************************************************
1595 * wodMessage (WINEALSA.@)
1597 DWORD WINAPI
ALSA_wodMessage(UINT wDevID
, UINT wMsg
, DWORD dwUser
,
1598 DWORD dwParam1
, DWORD dwParam2
)
1600 TRACE("(%u, %04X, %08lX, %08lX, %08lX);\n",
1601 wDevID
, wMsg
, dwUser
, dwParam1
, dwParam2
);
1608 /* FIXME: Pretend this is supported */
1610 case WODM_OPEN
: return wodOpen (wDevID
, (LPWAVEOPENDESC
)dwParam1
, dwParam2
);
1611 case WODM_CLOSE
: return wodClose (wDevID
);
1612 case WODM_GETDEVCAPS
: return wodGetDevCaps (wDevID
, (LPWAVEOUTCAPSA
)dwParam1
, dwParam2
);
1613 case WODM_GETNUMDEVS
: return wodGetNumDevs ();
1614 case WODM_GETPITCH
: return MMSYSERR_NOTSUPPORTED
;
1615 case WODM_SETPITCH
: return MMSYSERR_NOTSUPPORTED
;
1616 case WODM_GETPLAYBACKRATE
: return MMSYSERR_NOTSUPPORTED
;
1617 case WODM_SETPLAYBACKRATE
: return MMSYSERR_NOTSUPPORTED
;
1618 case WODM_WRITE
: return wodWrite (wDevID
, (LPWAVEHDR
)dwParam1
, dwParam2
);
1619 case WODM_PAUSE
: return wodPause (wDevID
);
1620 case WODM_GETPOS
: return wodGetPosition (wDevID
, (LPMMTIME
)dwParam1
, dwParam2
);
1621 case WODM_BREAKLOOP
: return wodBreakLoop (wDevID
);
1622 case WODM_PREPARE
: return wodPrepare (wDevID
, (LPWAVEHDR
)dwParam1
, dwParam2
);
1623 case WODM_UNPREPARE
: return wodUnprepare (wDevID
, (LPWAVEHDR
)dwParam1
, dwParam2
);
1624 case WODM_GETVOLUME
: return wodGetVolume (wDevID
, (LPDWORD
)dwParam1
);
1625 case WODM_SETVOLUME
: return wodSetVolume (wDevID
, dwParam1
);
1626 case WODM_RESTART
: return wodRestart (wDevID
);
1627 case WODM_RESET
: return wodReset (wDevID
);
1628 case DRV_QUERYDSOUNDIFACE
: return wodDsCreate (wDevID
, (PIDSDRIVER
*)dwParam1
);
1629 case DRV_QUERYDSOUNDDESC
: return wodDsDesc (wDevID
, (PDSDRIVERDESC
)dwParam1
);
1630 case DRV_QUERYDSOUNDGUID
: return wodDsGuid (wDevID
, (LPGUID
)dwParam1
);
1633 FIXME("unknown message %d!\n", wMsg
);
1635 return MMSYSERR_NOTSUPPORTED
;
1638 /*======================================================================*
1639 * Low level DSOUND implementation *
1640 *======================================================================*/
1642 typedef struct IDsDriverImpl IDsDriverImpl
;
1643 typedef struct IDsDriverBufferImpl IDsDriverBufferImpl
;
1645 struct IDsDriverImpl
1647 /* IUnknown fields */
1648 ICOM_VFIELD(IDsDriver
);
1650 /* IDsDriverImpl fields */
1652 IDsDriverBufferImpl
*primary
;
1655 struct IDsDriverBufferImpl
1657 /* IUnknown fields */
1658 ICOM_VFIELD(IDsDriverBuffer
);
1660 /* IDsDriverBufferImpl fields */
1663 CRITICAL_SECTION mmap_crst
;
1665 DWORD mmap_buflen_bytes
;
1666 snd_pcm_uframes_t mmap_buflen_frames
;
1667 snd_pcm_channel_area_t
* mmap_areas
;
1668 snd_async_handler_t
* mmap_async_handler
;
1671 static void DSDB_CheckXRUN(IDsDriverBufferImpl
* pdbi
)
1673 WINE_WAVEOUT
* wwo
= &(WOutDev
[pdbi
->drv
->wDevID
]);
1674 snd_pcm_state_t state
= snd_pcm_state(wwo
->p_handle
);
1676 if ( state
== SND_PCM_STATE_XRUN
)
1678 int err
= snd_pcm_prepare(wwo
->p_handle
);
1679 TRACE("xrun occurred\n");
1681 ERR("recovery from xrun failed, prepare failed: %s\n", snd_strerror(err
));
1683 else if ( state
== SND_PCM_STATE_SUSPENDED
)
1685 int err
= snd_pcm_resume(wwo
->p_handle
);
1686 TRACE("recovery from suspension occurred\n");
1687 if (err
< 0 && err
!= -EAGAIN
){
1688 err
= snd_pcm_prepare(wwo
->p_handle
);
1690 ERR("recovery from suspend failed, prepare failed: %s\n", snd_strerror(err
));
1695 static void DSDB_MMAPCopy(IDsDriverBufferImpl
* pdbi
)
1697 WINE_WAVEOUT
* wwo
= &(WOutDev
[pdbi
->drv
->wDevID
]);
1699 snd_pcm_format_t format
;
1700 snd_pcm_uframes_t period_size
;
1701 snd_pcm_sframes_t avail
;
1703 if ( !pdbi
->mmap_buffer
|| !wwo
->hw_params
|| !wwo
->p_handle
)
1706 channels
= snd_pcm_hw_params_get_channels(wwo
->hw_params
);
1707 format
= snd_pcm_hw_params_get_format(wwo
->hw_params
);
1708 period_size
= snd_pcm_hw_params_get_period_size(wwo
->hw_params
, 0);
1709 avail
= snd_pcm_avail_update(wwo
->p_handle
);
1711 DSDB_CheckXRUN(pdbi
);
1713 TRACE("avail=%d format=%s channels=%d\n", (int)avail
, snd_pcm_format_name(format
), channels
);
1715 while (avail
>= period_size
)
1717 const snd_pcm_channel_area_t
*areas
;
1718 snd_pcm_uframes_t ofs
;
1719 snd_pcm_uframes_t frames
;
1722 frames
= avail
/ period_size
* period_size
; /* round down to a multiple of period_size */
1724 EnterCriticalSection(&pdbi
->mmap_crst
);
1726 snd_pcm_mmap_begin(wwo
->p_handle
, &areas
, &ofs
, &frames
);
1727 snd_pcm_areas_copy(areas
, ofs
, pdbi
->mmap_areas
, ofs
, channels
, frames
, format
);
1728 err
= snd_pcm_mmap_commit(wwo
->p_handle
, ofs
, frames
);
1730 LeaveCriticalSection(&pdbi
->mmap_crst
);
1732 if ( err
!= (snd_pcm_sframes_t
) frames
)
1733 ERR("mmap partially failed.\n");
1735 avail
= snd_pcm_avail_update(wwo
->p_handle
);
1739 static void DSDB_PCMCallback(snd_async_handler_t
*ahandler
)
1741 /* snd_pcm_t * handle = snd_async_handler_get_pcm(ahandler); */
1742 IDsDriverBufferImpl
* pdbi
= snd_async_handler_get_callback_private(ahandler
);
1743 TRACE("callback called\n");
1744 DSDB_MMAPCopy(pdbi
);
1747 static int DSDB_CreateMMAP(IDsDriverBufferImpl
* pdbi
)
1749 WINE_WAVEOUT
* wwo
= &(WOutDev
[pdbi
->drv
->wDevID
]);
1750 snd_pcm_format_t format
= snd_pcm_hw_params_get_format(wwo
->hw_params
);
1751 snd_pcm_uframes_t frames
= snd_pcm_hw_params_get_buffer_size(wwo
->hw_params
);
1752 int channels
= snd_pcm_hw_params_get_channels(wwo
->hw_params
);
1753 unsigned int bits_per_sample
= snd_pcm_format_physical_width(format
);
1754 unsigned int bits_per_frame
= bits_per_sample
* channels
;
1755 snd_pcm_channel_area_t
* a
;
1760 ALSA_TraceParameters(wwo
->hw_params
, NULL
, FALSE
);
1762 TRACE("format=%s frames=%ld channels=%d bits_per_sample=%d bits_per_frame=%d\n",
1763 snd_pcm_format_name(format
), frames
, channels
, bits_per_sample
, bits_per_frame
);
1765 pdbi
->mmap_buflen_frames
= frames
;
1766 pdbi
->mmap_buflen_bytes
= snd_pcm_frames_to_bytes( wwo
->p_handle
, frames
);
1767 pdbi
->mmap_buffer
= HeapAlloc(GetProcessHeap(),0,pdbi
->mmap_buflen_bytes
);
1768 if (!pdbi
->mmap_buffer
)
1769 return DSERR_OUTOFMEMORY
;
1771 snd_pcm_format_set_silence(format
, pdbi
->mmap_buffer
, frames
);
1773 TRACE("created mmap buffer of %ld frames (%ld bytes) at %p\n",
1774 frames
, pdbi
->mmap_buflen_bytes
, pdbi
->mmap_buffer
);
1776 pdbi
->mmap_areas
= HeapAlloc(GetProcessHeap(),0,channels
*sizeof(snd_pcm_channel_area_t
));
1777 if (!pdbi
->mmap_areas
)
1778 return DSERR_OUTOFMEMORY
;
1780 a
= pdbi
->mmap_areas
;
1781 for (c
= 0; c
< channels
; c
++, a
++)
1783 a
->addr
= pdbi
->mmap_buffer
;
1784 a
->first
= bits_per_sample
* c
;
1785 a
->step
= bits_per_frame
;
1786 TRACE("Area %d: addr=%p first=%d step=%d\n", c
, a
->addr
, a
->first
, a
->step
);
1789 InitializeCriticalSection(&pdbi
->mmap_crst
);
1791 err
= snd_async_add_pcm_handler(&pdbi
->mmap_async_handler
, wwo
->p_handle
, DSDB_PCMCallback
, pdbi
);
1794 ERR("add_pcm_handler failed. reason: %s\n", snd_strerror(err
));
1795 return DSERR_GENERIC
;
1801 static void DSDB_DestroyMMAP(IDsDriverBufferImpl
* pdbi
)
1803 TRACE("mmap buffer %p destroyed\n", pdbi
->mmap_buffer
);
1804 HeapFree(GetProcessHeap(), 0, pdbi
->mmap_areas
);
1805 HeapFree(GetProcessHeap(), 0, pdbi
->mmap_buffer
);
1806 pdbi
->mmap_areas
= NULL
;
1807 pdbi
->mmap_buffer
= NULL
;
1808 DeleteCriticalSection(&pdbi
->mmap_crst
);
1812 static HRESULT WINAPI
IDsDriverBufferImpl_QueryInterface(PIDSDRIVERBUFFER iface
, REFIID riid
, LPVOID
*ppobj
)
1814 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
1815 FIXME("(): stub!\n");
1816 return DSERR_UNSUPPORTED
;
1819 static ULONG WINAPI
IDsDriverBufferImpl_AddRef(PIDSDRIVERBUFFER iface
)
1821 ICOM_THIS(IDsDriverBufferImpl
,iface
);
1822 TRACE("(%p)\n",iface
);
1826 static ULONG WINAPI
IDsDriverBufferImpl_Release(PIDSDRIVERBUFFER iface
)
1828 ICOM_THIS(IDsDriverBufferImpl
,iface
);
1829 TRACE("(%p)\n",iface
);
1832 if (This
== This
->drv
->primary
)
1833 This
->drv
->primary
= NULL
;
1834 DSDB_DestroyMMAP(This
);
1835 HeapFree(GetProcessHeap(), 0, This
);
1839 static HRESULT WINAPI
IDsDriverBufferImpl_Lock(PIDSDRIVERBUFFER iface
,
1840 LPVOID
*ppvAudio1
,LPDWORD pdwLen1
,
1841 LPVOID
*ppvAudio2
,LPDWORD pdwLen2
,
1842 DWORD dwWritePosition
,DWORD dwWriteLen
,
1845 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
1846 TRACE("(%p)\n",iface
);
1847 return DSERR_UNSUPPORTED
;
1850 static HRESULT WINAPI
IDsDriverBufferImpl_Unlock(PIDSDRIVERBUFFER iface
,
1851 LPVOID pvAudio1
,DWORD dwLen1
,
1852 LPVOID pvAudio2
,DWORD dwLen2
)
1854 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
1855 TRACE("(%p)\n",iface
);
1856 return DSERR_UNSUPPORTED
;
1859 static HRESULT WINAPI
IDsDriverBufferImpl_SetFormat(PIDSDRIVERBUFFER iface
,
1860 LPWAVEFORMATEX pwfx
)
1862 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
1863 TRACE("(%p,%p)\n",iface
,pwfx
);
1864 return DSERR_BUFFERLOST
;
1867 static HRESULT WINAPI
IDsDriverBufferImpl_SetFrequency(PIDSDRIVERBUFFER iface
, DWORD dwFreq
)
1869 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
1870 TRACE("(%p,%ld): stub\n",iface
,dwFreq
);
1871 return DSERR_UNSUPPORTED
;
1874 static HRESULT WINAPI
IDsDriverBufferImpl_SetVolumePan(PIDSDRIVERBUFFER iface
, PDSVOLUMEPAN pVolPan
)
1876 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
1877 FIXME("(%p,%p): stub!\n",iface
,pVolPan
);
1881 static HRESULT WINAPI
IDsDriverBufferImpl_SetPosition(PIDSDRIVERBUFFER iface
, DWORD dwNewPos
)
1883 /* ICOM_THIS(IDsDriverImpl,iface); */
1884 TRACE("(%p,%ld): stub\n",iface
,dwNewPos
);
1885 return DSERR_UNSUPPORTED
;
1888 static HRESULT WINAPI
IDsDriverBufferImpl_GetPosition(PIDSDRIVERBUFFER iface
,
1889 LPDWORD lpdwPlay
, LPDWORD lpdwWrite
)
1891 ICOM_THIS(IDsDriverBufferImpl
,iface
);
1892 WINE_WAVEOUT
* wwo
= &(WOutDev
[This
->drv
->wDevID
]);
1893 snd_pcm_uframes_t hw_ptr
;
1894 snd_pcm_uframes_t period_size
;
1896 if (wwo
->hw_params
== NULL
) return DSERR_GENERIC
;
1898 period_size
= snd_pcm_hw_params_get_period_size(wwo
->hw_params
, 0);
1900 if (wwo
->p_handle
== NULL
) return DSERR_GENERIC
;
1901 /** we need to track down buffer underruns */
1902 DSDB_CheckXRUN(This
);
1904 EnterCriticalSection(&This
->mmap_crst
);
1905 hw_ptr
= _snd_pcm_mmap_hw_ptr(wwo
->p_handle
);
1907 *lpdwPlay
= snd_pcm_frames_to_bytes(wwo
->p_handle
, hw_ptr
/ period_size
* period_size
) % This
->mmap_buflen_bytes
;
1909 *lpdwWrite
= snd_pcm_frames_to_bytes(wwo
->p_handle
, (hw_ptr
/ period_size
+ 1) * period_size
) % This
->mmap_buflen_bytes
;
1910 LeaveCriticalSection(&This
->mmap_crst
);
1912 TRACE("hw_ptr=0x%08x, playpos=%ld, writepos=%ld\n", (unsigned int)hw_ptr
, lpdwPlay
?*lpdwPlay
:-1, lpdwWrite
?*lpdwWrite
:-1);
1916 static HRESULT WINAPI
IDsDriverBufferImpl_Play(PIDSDRIVERBUFFER iface
, DWORD dwRes1
, DWORD dwRes2
, DWORD dwFlags
)
1918 ICOM_THIS(IDsDriverBufferImpl
,iface
);
1919 WINE_WAVEOUT
* wwo
= &(WOutDev
[This
->drv
->wDevID
]);
1920 snd_pcm_state_t state
;
1923 TRACE("(%p,%lx,%lx,%lx)\n",iface
,dwRes1
,dwRes2
,dwFlags
);
1925 if (wwo
->p_handle
== NULL
) return DSERR_GENERIC
;
1927 state
= snd_pcm_state(wwo
->p_handle
);
1928 if ( state
== SND_PCM_STATE_SETUP
)
1930 err
= snd_pcm_prepare(wwo
->p_handle
);
1931 state
= snd_pcm_state(wwo
->p_handle
);
1933 if ( state
== SND_PCM_STATE_PREPARED
)
1935 DSDB_MMAPCopy(This
);
1936 err
= snd_pcm_start(wwo
->p_handle
);
1941 static HRESULT WINAPI
IDsDriverBufferImpl_Stop(PIDSDRIVERBUFFER iface
)
1943 ICOM_THIS(IDsDriverBufferImpl
,iface
);
1944 WINE_WAVEOUT
* wwo
= &(WOutDev
[This
->drv
->wDevID
]);
1949 TRACE("(%p)\n",iface
);
1951 if (wwo
->p_handle
== NULL
) return DSERR_GENERIC
;
1953 /* ring buffer wrap up detection */
1954 IDsDriverBufferImpl_GetPosition(iface
, &play
, &write
);
1957 TRACE("writepos wrapper up\n");
1961 if ( ( err
= snd_pcm_drop(wwo
->p_handle
)) < 0 )
1963 ERR("error while stopping pcm: %s\n", snd_strerror(err
));
1964 return DSERR_GENERIC
;
1969 static ICOM_VTABLE(IDsDriverBuffer
) dsdbvt
=
1971 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1972 IDsDriverBufferImpl_QueryInterface
,
1973 IDsDriverBufferImpl_AddRef
,
1974 IDsDriverBufferImpl_Release
,
1975 IDsDriverBufferImpl_Lock
,
1976 IDsDriverBufferImpl_Unlock
,
1977 IDsDriverBufferImpl_SetFormat
,
1978 IDsDriverBufferImpl_SetFrequency
,
1979 IDsDriverBufferImpl_SetVolumePan
,
1980 IDsDriverBufferImpl_SetPosition
,
1981 IDsDriverBufferImpl_GetPosition
,
1982 IDsDriverBufferImpl_Play
,
1983 IDsDriverBufferImpl_Stop
1986 static HRESULT WINAPI
IDsDriverImpl_QueryInterface(PIDSDRIVER iface
, REFIID riid
, LPVOID
*ppobj
)
1988 /* ICOM_THIS(IDsDriverImpl,iface); */
1989 FIXME("(%p): stub!\n",iface
);
1990 return DSERR_UNSUPPORTED
;
1993 static ULONG WINAPI
IDsDriverImpl_AddRef(PIDSDRIVER iface
)
1995 ICOM_THIS(IDsDriverImpl
,iface
);
1996 TRACE("(%p)\n",iface
);
2001 static ULONG WINAPI
IDsDriverImpl_Release(PIDSDRIVER iface
)
2003 ICOM_THIS(IDsDriverImpl
,iface
);
2004 TRACE("(%p)\n",iface
);
2007 HeapFree(GetProcessHeap(),0,This
);
2011 static HRESULT WINAPI
IDsDriverImpl_GetDriverDesc(PIDSDRIVER iface
, PDSDRIVERDESC pDesc
)
2013 ICOM_THIS(IDsDriverImpl
,iface
);
2014 TRACE("(%p,%p)\n",iface
,pDesc
);
2015 memcpy(pDesc
, &(WOutDev
[This
->wDevID
].ds_desc
), sizeof(DSDRIVERDESC
));
2016 pDesc
->dwFlags
= DSDDESC_DOMMSYSTEMOPEN
| DSDDESC_DOMMSYSTEMSETFORMAT
|
2017 DSDDESC_USESYSTEMMEMORY
;
2018 pDesc
->dnDevNode
= WOutDev
[This
->wDevID
].waveDesc
.dnDevNode
;
2020 pDesc
->wReserved
= 0;
2021 pDesc
->ulDeviceNum
= This
->wDevID
;
2022 pDesc
->dwHeapType
= DSDHEAP_NOHEAP
;
2023 pDesc
->pvDirectDrawHeap
= NULL
;
2024 pDesc
->dwMemStartAddress
= 0;
2025 pDesc
->dwMemEndAddress
= 0;
2026 pDesc
->dwMemAllocExtra
= 0;
2027 pDesc
->pvReserved1
= NULL
;
2028 pDesc
->pvReserved2
= NULL
;
2032 static HRESULT WINAPI
IDsDriverImpl_Open(PIDSDRIVER iface
)
2034 /* ICOM_THIS(IDsDriverImpl,iface); */
2035 TRACE("(%p)\n",iface
);
2039 static HRESULT WINAPI
IDsDriverImpl_Close(PIDSDRIVER iface
)
2041 /* ICOM_THIS(IDsDriverImpl,iface); */
2042 TRACE("(%p)\n",iface
);
2046 static HRESULT WINAPI
IDsDriverImpl_GetCaps(PIDSDRIVER iface
, PDSDRIVERCAPS pCaps
)
2048 ICOM_THIS(IDsDriverImpl
,iface
);
2049 TRACE("(%p,%p)\n",iface
,pCaps
);
2050 memset(pCaps
, 0, sizeof(*pCaps
));
2052 pCaps
->dwFlags
= DSCAPS_PRIMARYMONO
;
2053 if ( WOutDev
[This
->wDevID
].caps
.wChannels
== 2 )
2054 pCaps
->dwFlags
|= DSCAPS_PRIMARYSTEREO
;
2056 if ( WOutDev
[This
->wDevID
].caps
.dwFormats
& (WAVE_FORMAT_1S08
| WAVE_FORMAT_2S08
| WAVE_FORMAT_4S08
) )
2057 pCaps
->dwFlags
|= DSCAPS_PRIMARY8BIT
;
2059 if ( WOutDev
[This
->wDevID
].caps
.dwFormats
& (WAVE_FORMAT_1S16
| WAVE_FORMAT_2S16
| WAVE_FORMAT_4S16
))
2060 pCaps
->dwFlags
|= DSCAPS_PRIMARY16BIT
;
2062 pCaps
->dwPrimaryBuffers
= 1;
2063 TRACE("caps=0x%X\n",(unsigned int)pCaps
->dwFlags
);
2064 pCaps
->dwMinSecondarySampleRate
= DSBFREQUENCY_MIN
;
2065 pCaps
->dwMaxSecondarySampleRate
= DSBFREQUENCY_MAX
;
2067 /* the other fields only apply to secondary buffers, which we don't support
2068 * (unless we want to mess with wavetable synthesizers and MIDI) */
2072 static HRESULT WINAPI
IDsDriverImpl_CreateSoundBuffer(PIDSDRIVER iface
,
2073 LPWAVEFORMATEX pwfx
,
2074 DWORD dwFlags
, DWORD dwCardAddress
,
2075 LPDWORD pdwcbBufferSize
,
2079 ICOM_THIS(IDsDriverImpl
,iface
);
2080 IDsDriverBufferImpl
** ippdsdb
= (IDsDriverBufferImpl
**)ppvObj
;
2083 TRACE("(%p,%p,%lx,%lx)\n",iface
,pwfx
,dwFlags
,dwCardAddress
);
2084 /* we only support primary buffers */
2085 if (!(dwFlags
& DSBCAPS_PRIMARYBUFFER
))
2086 return DSERR_UNSUPPORTED
;
2088 return DSERR_ALLOCATED
;
2089 if (dwFlags
& (DSBCAPS_CTRLFREQUENCY
| DSBCAPS_CTRLPAN
))
2090 return DSERR_CONTROLUNAVAIL
;
2092 *ippdsdb
= (IDsDriverBufferImpl
*)HeapAlloc(GetProcessHeap(),0,sizeof(IDsDriverBufferImpl
));
2093 if (*ippdsdb
== NULL
)
2094 return DSERR_OUTOFMEMORY
;
2095 (*ippdsdb
)->lpVtbl
= &dsdbvt
;
2096 (*ippdsdb
)->ref
= 1;
2097 (*ippdsdb
)->drv
= This
;
2099 err
= DSDB_CreateMMAP((*ippdsdb
));
2102 HeapFree(GetProcessHeap(), 0, *ippdsdb
);
2106 *ppbBuffer
= (*ippdsdb
)->mmap_buffer
;
2107 *pdwcbBufferSize
= (*ippdsdb
)->mmap_buflen_bytes
;
2109 This
->primary
= *ippdsdb
;
2111 /* buffer is ready to go */
2112 TRACE("buffer created at %p\n", *ippdsdb
);
2116 static HRESULT WINAPI
IDsDriverImpl_DuplicateSoundBuffer(PIDSDRIVER iface
,
2117 PIDSDRIVERBUFFER pBuffer
,
2120 /* ICOM_THIS(IDsDriverImpl,iface); */
2121 TRACE("(%p,%p): stub\n",iface
,pBuffer
);
2122 return DSERR_INVALIDCALL
;
2125 static ICOM_VTABLE(IDsDriver
) dsdvt
=
2127 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
2128 IDsDriverImpl_QueryInterface
,
2129 IDsDriverImpl_AddRef
,
2130 IDsDriverImpl_Release
,
2131 IDsDriverImpl_GetDriverDesc
,
2133 IDsDriverImpl_Close
,
2134 IDsDriverImpl_GetCaps
,
2135 IDsDriverImpl_CreateSoundBuffer
,
2136 IDsDriverImpl_DuplicateSoundBuffer
2139 static DWORD
wodDsCreate(UINT wDevID
, PIDSDRIVER
* drv
)
2141 IDsDriverImpl
** idrv
= (IDsDriverImpl
**)drv
;
2143 TRACE("driver created\n");
2145 /* the HAL isn't much better than the HEL if we can't do mmap() */
2146 if (!(WOutDev
[wDevID
].caps
.dwSupport
& WAVECAPS_DIRECTSOUND
)) {
2147 ERR("DirectSound flag not set\n");
2148 MESSAGE("This sound card's driver does not support direct access\n");
2149 MESSAGE("The (slower) DirectSound HEL mode will be used instead.\n");
2150 return MMSYSERR_NOTSUPPORTED
;
2153 *idrv
= (IDsDriverImpl
*)HeapAlloc(GetProcessHeap(),0,sizeof(IDsDriverImpl
));
2155 return MMSYSERR_NOMEM
;
2156 (*idrv
)->lpVtbl
= &dsdvt
;
2159 (*idrv
)->wDevID
= wDevID
;
2160 (*idrv
)->primary
= NULL
;
2161 return MMSYSERR_NOERROR
;
2164 static DWORD
wodDsDesc(UINT wDevID
, PDSDRIVERDESC desc
)
2166 memcpy(desc
, &(WOutDev
[wDevID
].ds_desc
), sizeof(DSDRIVERDESC
));
2167 return MMSYSERR_NOERROR
;
2170 static DWORD
wodDsGuid(UINT wDevID
, LPGUID pGuid
)
2172 TRACE("(%d,%p)\n",wDevID
,pGuid
);
2174 memcpy(pGuid
, &(WOutDev
[wDevID
].ds_guid
), sizeof(GUID
));
2176 return MMSYSERR_NOERROR
;
2183 /**************************************************************************
2184 * wodMessage (WINEALSA.@)
2186 DWORD WINAPI
ALSA_wodMessage(WORD wDevID
, WORD wMsg
, DWORD dwUser
,
2187 DWORD dwParam1
, DWORD dwParam2
)
2189 FIXME("(%u, %04X, %08lX, %08lX, %08lX):stub\n", wDevID
, wMsg
, dwUser
, dwParam1
, dwParam2
);
2190 return MMSYSERR_NOTENABLED
;
2193 #endif /* HAVE_ALSA */