2 * Sample Wine Driver for Advanced Linux Sound System (ALSA)
3 * Based on version <final> of the ALSA API
5 * Copyright 2002 Eric Pouech
6 * 2002 Marco Pietrobono
7 * 2003 Christian Costa : WaveIn support
8 * 2006-2007 Maarten Lankhorst
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 /*======================================================================*
26 * Low level dsound output implementation *
27 *======================================================================*/
30 #include "wine/port.h"
43 #ifdef HAVE_SYS_IOCTL_H
44 # include <sys/ioctl.h>
46 #ifdef HAVE_SYS_MMAN_H
47 # include <sys/mman.h>
57 #include "wine/library.h"
58 #include "wine/unicode.h"
59 #include "wine/debug.h"
63 WINE_DEFAULT_DEBUG_CHANNEL(dsalsa
);
65 typedef struct IDsDriverImpl IDsDriverImpl
;
66 typedef struct IDsDriverBufferImpl IDsDriverBufferImpl
;
71 const IDsDriverVtbl
*lpVtbl
;
74 /* IDsDriverImpl fields */
75 IDsDriverBufferImpl
* primary
;
79 struct IDsDriverBufferImpl
81 const IDsDriverBufferVtbl
*lpVtbl
;
85 CRITICAL_SECTION pcm_crst
;
87 DWORD mmap_buflen_bytes
;
91 snd_pcm_hw_params_t
*hw_params
;
92 snd_pcm_sw_params_t
*sw_params
;
93 snd_pcm_uframes_t mmap_buflen_frames
, mmap_pos
, mmap_commitahead
;
96 /** Fill buffers, for starting and stopping
97 * Alsa won't start playing until everything is filled up
98 * This also updates mmap_pos
100 * Returns: Amount of periods in use so snd_pcm_avail_update
101 * doesn't have to be called up to 4x in GetPosition()
103 static snd_pcm_uframes_t
CommitAll(IDsDriverBufferImpl
*This
)
105 const snd_pcm_channel_area_t
*areas
;
106 snd_pcm_sframes_t used
;
107 const snd_pcm_uframes_t commitahead
= This
->mmap_commitahead
;
109 used
= This
->mmap_buflen_frames
- snd_pcm_avail_update(This
->pcm
);
110 if (used
< 0) used
= 0;
111 TRACE("%p needs to commit to %lu, used: %ld\n", This
, commitahead
, used
);
112 if (used
< commitahead
)
114 snd_pcm_uframes_t done
, putin
= commitahead
- used
;
117 snd_pcm_mmap_begin(This
->pcm
, &areas
, &This
->mmap_pos
, &putin
);
118 done
= snd_pcm_mmap_commit(This
->pcm
, This
->mmap_pos
, putin
);
122 if (putin
+ This
->mmap_pos
> This
->mmap_buflen_frames
)
123 putin
= This
->mmap_buflen_frames
- This
->mmap_pos
;
125 snd_pcm_writei(This
->pcm
, This
->mmap_buffer
+ snd_pcm_frames_to_bytes(This
->pcm
, This
->mmap_pos
), putin
);
127 This
->mmap_pos
+= done
;
129 putin
= commitahead
- used
;
131 if (This
->mmap_pos
== This
->mmap_buflen_frames
&& (snd_pcm_sframes_t
)putin
> 0)
135 snd_pcm_mmap_begin(This
->pcm
, &areas
, &This
->mmap_pos
, &putin
);
136 done
= snd_pcm_mmap_commit(This
->pcm
, This
->mmap_pos
, putin
);
137 This
->mmap_pos
+= done
;
141 snd_pcm_writei(This
->pcm
, This
->mmap_buffer
, putin
);
142 This
->mmap_pos
= done
= putin
;
148 if (This
->mmap_pos
== This
->mmap_buflen_frames
)
154 static void CheckXRUN(IDsDriverBufferImpl
* This
)
156 snd_pcm_state_t state
= snd_pcm_state(This
->pcm
);
157 snd_pcm_sframes_t delay
;
160 snd_pcm_hwsync(This
->pcm
);
161 snd_pcm_delay(This
->pcm
, &delay
);
162 if ( state
== SND_PCM_STATE_XRUN
)
164 err
= snd_pcm_prepare(This
->pcm
);
166 snd_pcm_start(This
->pcm
);
167 WARN("xrun occurred\n");
169 ERR("recovery from xrun failed, prepare failed: %s\n", snd_strerror(err
));
171 else if ( state
== SND_PCM_STATE_SUSPENDED
)
173 int err
= snd_pcm_resume(This
->pcm
);
174 TRACE("recovery from suspension occurred\n");
175 if (err
< 0 && err
!= -EAGAIN
){
176 err
= snd_pcm_prepare(This
->pcm
);
178 ERR("recovery from suspend failed, prepare failed: %s\n", snd_strerror(err
));
180 } else if ( state
!= SND_PCM_STATE_RUNNING
) {
181 FIXME("Unhandled state: %d\n", state
);
186 * Allocate the memory-mapped buffer for direct sound, and set up the
189 static int DSDB_CreateMMAP(IDsDriverBufferImpl
* pdbi
)
191 snd_pcm_t
*pcm
= pdbi
->pcm
;
192 snd_pcm_format_t format
;
193 snd_pcm_uframes_t frames
, ofs
, avail
, psize
, boundary
;
194 unsigned int channels
, bits_per_sample
, bits_per_frame
;
196 const snd_pcm_channel_area_t
*areas
;
197 snd_pcm_hw_params_t
*hw_params
= pdbi
->hw_params
;
198 snd_pcm_sw_params_t
*sw_params
= pdbi
->sw_params
;
201 mmap_mode
= snd_pcm_type(pcm
);
203 if (mmap_mode
== SND_PCM_TYPE_HW
)
204 TRACE("mmap'd buffer is a direct hardware buffer.\n");
205 else if (mmap_mode
== SND_PCM_TYPE_DMIX
)
206 TRACE("mmap'd buffer is an ALSA dmix buffer\n");
208 TRACE("mmap'd buffer is an ALSA type %d buffer\n", mmap_mode
);
210 err
= snd_pcm_hw_params_get_period_size(hw_params
, &psize
, NULL
);
212 err
= snd_pcm_hw_params_get_format(hw_params
, &format
);
213 err
= snd_pcm_hw_params_get_buffer_size(hw_params
, &frames
);
214 err
= snd_pcm_hw_params_get_channels(hw_params
, &channels
);
215 bits_per_sample
= snd_pcm_format_physical_width(format
);
216 bits_per_frame
= bits_per_sample
* channels
;
218 if (TRACE_ON(dsalsa
))
219 ALSA_TraceParameters(hw_params
, NULL
, FALSE
);
221 TRACE("format=%s frames=%ld channels=%d bits_per_sample=%d bits_per_frame=%d\n",
222 snd_pcm_format_name(format
), frames
, channels
, bits_per_sample
, bits_per_frame
);
224 pdbi
->mmap_buflen_frames
= frames
;
225 pdbi
->mmap_buflen_bytes
= snd_pcm_frames_to_bytes( pcm
, frames
);
227 snd_pcm_sw_params_current(pcm
, sw_params
);
228 snd_pcm_sw_params_set_start_threshold(pcm
, sw_params
, 0);
229 snd_pcm_sw_params_get_boundary(sw_params
, &boundary
);
230 snd_pcm_sw_params_set_stop_threshold(pcm
, sw_params
, boundary
);
231 snd_pcm_sw_params_set_silence_threshold(pcm
, sw_params
, boundary
);
232 snd_pcm_sw_params_set_silence_size(pcm
, sw_params
, 0);
233 snd_pcm_sw_params_set_avail_min(pcm
, sw_params
, 0);
234 err
= snd_pcm_sw_params(pcm
, sw_params
);
236 avail
= snd_pcm_avail_update(pcm
);
237 if ((snd_pcm_sframes_t
)avail
< 0)
239 ERR("No buffer is available: %s.\n", snd_strerror(avail
));
240 return DSERR_GENERIC
;
245 buf
= pdbi
->mmap_buffer
= HeapAlloc(GetProcessHeap(), 0, pdbi
->mmap_buflen_bytes
);
247 return DSERR_OUTOFMEMORY
;
249 snd_pcm_format_set_silence(format
, buf
, pdbi
->mmap_buflen_frames
);
253 err
= snd_pcm_mmap_begin(pcm
, &areas
, &ofs
, &avail
);
256 ERR("Can't map sound device for direct access: %s/%d\n", snd_strerror(err
), err
);
257 return DSERR_GENERIC
;
259 snd_pcm_format_set_silence(format
, areas
->addr
, pdbi
->mmap_buflen_frames
);
260 pdbi
->mmap_pos
= ofs
+ snd_pcm_mmap_commit(pcm
, ofs
, 0);
261 pdbi
->mmap_buffer
= areas
->addr
;
264 TRACE("created mmap buffer of %ld frames (%d bytes) at %p\n",
265 frames
, pdbi
->mmap_buflen_bytes
, pdbi
->mmap_buffer
);
270 static HRESULT WINAPI
IDsDriverBufferImpl_QueryInterface(PIDSDRIVERBUFFER iface
, REFIID riid
, LPVOID
*ppobj
)
272 /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
273 FIXME("(): stub!\n");
274 return DSERR_UNSUPPORTED
;
277 static ULONG WINAPI
IDsDriverBufferImpl_AddRef(PIDSDRIVERBUFFER iface
)
279 IDsDriverBufferImpl
*This
= (IDsDriverBufferImpl
*)iface
;
280 ULONG refCount
= InterlockedIncrement(&This
->ref
);
282 TRACE("(%p)->(ref before=%u)\n",This
, refCount
- 1);
287 static ULONG WINAPI
IDsDriverBufferImpl_Release(PIDSDRIVERBUFFER iface
)
289 IDsDriverBufferImpl
*This
= (IDsDriverBufferImpl
*)iface
;
290 ULONG refCount
= InterlockedDecrement(&This
->ref
);
292 TRACE("(%p)->(ref before=%u)\n",This
, refCount
+ 1);
297 TRACE("mmap buffer %p destroyed\n", This
->mmap_buffer
);
299 if (This
== This
->drv
->primary
)
300 This
->drv
->primary
= NULL
;
302 This
->pcm_crst
.DebugInfo
->Spare
[0] = 0;
303 DeleteCriticalSection(&This
->pcm_crst
);
305 snd_pcm_drop(This
->pcm
);
306 snd_pcm_close(This
->pcm
);
308 HeapFree(GetProcessHeap(), 0, This
->sw_params
);
309 HeapFree(GetProcessHeap(), 0, This
->hw_params
);
311 HeapFree(GetProcessHeap(), 0, This
->mmap_buffer
);
312 HeapFree(GetProcessHeap(), 0, This
);
316 static HRESULT WINAPI
IDsDriverBufferImpl_Lock(PIDSDRIVERBUFFER iface
,
317 LPVOID
*ppvAudio1
,LPDWORD pdwLen1
,
318 LPVOID
*ppvAudio2
,LPDWORD pdwLen2
,
319 DWORD dwWritePosition
,DWORD dwWriteLen
,
322 IDsDriverBufferImpl
*This
= (IDsDriverBufferImpl
*)iface
;
323 snd_pcm_uframes_t writepos
;
325 TRACE("%d bytes from %d\n", dwWriteLen
, dwWritePosition
);
328 EnterCriticalSection(&This
->pcm_crst
);
330 if (dwFlags
& DSBLOCK_ENTIREBUFFER
)
331 dwWriteLen
= This
->mmap_buflen_bytes
;
333 if (dwWriteLen
> This
->mmap_buflen_bytes
|| dwWritePosition
>= This
->mmap_buflen_bytes
)
336 LeaveCriticalSection(&This
->pcm_crst
);
337 return DSERR_INVALIDPARAM
;
340 if (ppvAudio2
) *ppvAudio2
= NULL
;
341 if (pdwLen2
) *pdwLen2
= 0;
343 *ppvAudio1
= This
->mmap_buffer
+ dwWritePosition
;
344 *pdwLen1
= dwWriteLen
;
346 if (dwWritePosition
+dwWriteLen
> This
->mmap_buflen_bytes
)
348 DWORD remainder
= This
->mmap_buflen_bytes
- dwWritePosition
;
349 *pdwLen1
= remainder
;
351 if (ppvAudio2
&& pdwLen2
)
353 *ppvAudio2
= This
->mmap_buffer
;
354 *pdwLen2
= dwWriteLen
- remainder
;
356 else dwWriteLen
= remainder
;
359 writepos
= snd_pcm_bytes_to_frames(This
->pcm
, dwWritePosition
);
360 if (writepos
== This
->mmap_pos
)
362 const snd_pcm_channel_area_t
*areas
;
363 snd_pcm_uframes_t writelen
= snd_pcm_bytes_to_frames(This
->pcm
, dwWriteLen
), putin
= writelen
;
364 TRACE("Hit mmap_pos, locking data!\n");
366 snd_pcm_mmap_begin(This
->pcm
, &areas
, &This
->mmap_pos
, &putin
);
369 WARN("mmap_pos (%lu) != writepos (%lu) not locking data!\n", This
->mmap_pos
, writepos
);
371 LeaveCriticalSection(&This
->pcm_crst
);
376 static HRESULT WINAPI
IDsDriverBufferImpl_Unlock(PIDSDRIVERBUFFER iface
,
377 LPVOID pvAudio1
,DWORD dwLen1
,
378 LPVOID pvAudio2
,DWORD dwLen2
)
380 IDsDriverBufferImpl
*This
= (IDsDriverBufferImpl
*)iface
;
381 snd_pcm_uframes_t writepos
;
387 EnterCriticalSection(&This
->pcm_crst
);
389 writepos
= snd_pcm_bytes_to_frames(This
->pcm
, (DWORD_PTR
)pvAudio1
- (DWORD_PTR
)This
->mmap_buffer
);
390 if (writepos
== This
->mmap_pos
)
392 const snd_pcm_channel_area_t
*areas
;
393 snd_pcm_uframes_t writelen
= snd_pcm_bytes_to_frames(This
->pcm
, dwLen1
);
394 TRACE("Committing data\n");
396 This
->mmap_pos
+= snd_pcm_mmap_commit(This
->pcm
, This
->mmap_pos
, writelen
);
400 ret
= snd_pcm_writei(This
->pcm
, pvAudio1
, writelen
);
403 WARN("Underrun occured\n");
404 snd_pcm_prepare(This
->pcm
);
405 ret
= snd_pcm_writei(This
->pcm
, pvAudio1
, writelen
);
406 snd_pcm_start(This
->pcm
);
409 WARN("Committing data: %d / %s (%p %ld)\n", ret
, snd_strerror(ret
), pvAudio1
, writelen
);
410 This
->mmap_pos
+= writelen
;
413 if (This
->mmap_pos
== This
->mmap_buflen_frames
)
415 if (!This
->mmap_pos
&& dwLen2
)
417 writelen
= snd_pcm_bytes_to_frames(This
->pcm
, dwLen2
);
420 snd_pcm_mmap_begin(This
->pcm
, &areas
, &This
->mmap_pos
, &writelen
);
421 This
->mmap_pos
+= snd_pcm_mmap_commit(This
->pcm
, This
->mmap_pos
, writelen
);
426 ret
= snd_pcm_writei(This
->pcm
, pvAudio2
, writelen
);
427 This
->mmap_pos
= writelen
;
429 assert(This
->mmap_pos
< This
->mmap_buflen_frames
);
432 LeaveCriticalSection(&This
->pcm_crst
);
438 static HRESULT
SetFormat(IDsDriverBufferImpl
*This
, LPWAVEFORMATEX pwfx
)
440 snd_pcm_t
*pcm
= NULL
;
441 snd_pcm_hw_params_t
*hw_params
= This
->hw_params
;
442 unsigned int buffer_time
= 500000;
443 snd_pcm_format_t format
= -1;
444 snd_pcm_uframes_t psize
;
445 DWORD rate
= pwfx
->nSamplesPerSec
;
448 switch (pwfx
->wBitsPerSample
)
450 case 8: format
= SND_PCM_FORMAT_U8
; break;
451 case 16: format
= SND_PCM_FORMAT_S16_LE
; break;
452 case 24: format
= SND_PCM_FORMAT_S24_3LE
; break;
453 case 32: format
= SND_PCM_FORMAT_S32_LE
; break;
454 default: FIXME("Unsupported bpp: %d\n", pwfx
->wBitsPerSample
); return DSERR_GENERIC
;
457 err
= snd_pcm_open(&pcm
, WOutDev
[This
->drv
->wDevID
].pcmname
, SND_PCM_STREAM_PLAYBACK
, SND_PCM_NONBLOCK
);
460 if (errno
!= EBUSY
|| !This
->pcm
)
462 WARN("Cannot open sound device: %s\n", snd_strerror(err
));
463 return DSERR_GENERIC
;
465 snd_pcm_drop(This
->pcm
);
466 snd_pcm_close(This
->pcm
);
468 err
= snd_pcm_open(&pcm
, WOutDev
[This
->drv
->wDevID
].pcmname
, SND_PCM_STREAM_PLAYBACK
, SND_PCM_NONBLOCK
);
471 WARN("Cannot open sound device: %s\n", snd_strerror(err
));
472 return DSERR_BUFFERLOST
;
476 /* Set some defaults */
477 snd_pcm_hw_params_any(pcm
, hw_params
);
478 err
= snd_pcm_hw_params_set_channels(pcm
, hw_params
, pwfx
->nChannels
);
479 if (err
< 0) { WARN("Could not set channels to %d\n", pwfx
->nChannels
); goto err
; }
481 err
= snd_pcm_hw_params_set_format(pcm
, hw_params
, format
);
482 if (err
< 0) { WARN("Could not set format to %d bpp\n", pwfx
->wBitsPerSample
); goto err
; }
484 /* Alsa's rate resampling is only used if the application specifically requests
485 * a buffer at a certain frequency, else it is better to disable it due to unwanted
486 * side effects, which may include: Less granular pointer, changing buffer sizes, etc
488 #if SND_LIB_VERSION >= 0x010009
489 snd_pcm_hw_params_set_rate_resample(pcm
, hw_params
, 0);
492 err
= snd_pcm_hw_params_set_rate_near(pcm
, hw_params
, &rate
, NULL
);
493 if (err
< 0) { rate
= pwfx
->nSamplesPerSec
; WARN("Could not set rate\n"); goto err
; }
495 if (!ALSA_NearMatch(rate
, pwfx
->nSamplesPerSec
))
497 WARN("Could not set sound rate to %d, but instead to %d\n", pwfx
->nSamplesPerSec
, rate
);
498 pwfx
->nSamplesPerSec
= rate
;
499 pwfx
->nAvgBytesPerSec
= rate
* pwfx
->nBlockAlign
;
500 /* Let DirectSound detect this */
503 snd_pcm_hw_params_set_periods_integer(pcm
, hw_params
);
504 snd_pcm_hw_params_set_buffer_time_near(pcm
, hw_params
, &buffer_time
, NULL
);
506 snd_pcm_hw_params_set_period_time_near(pcm
, hw_params
, &buffer_time
, NULL
);
508 err
= snd_pcm_hw_params_get_period_size(hw_params
, &psize
, NULL
);
510 snd_pcm_hw_params_set_periods_near(pcm
, hw_params
, &buffer_time
, NULL
);
514 HeapFree(GetProcessHeap(), 0, This
->mmap_buffer
);
515 This
->mmap_buffer
= NULL
;
518 err
= snd_pcm_hw_params_set_access (pcm
, hw_params
, SND_PCM_ACCESS_MMAP_INTERLEAVED
);
524 err
= snd_pcm_hw_params_set_access (pcm
, hw_params
, SND_PCM_ACCESS_RW_INTERLEAVED
);
527 err
= snd_pcm_hw_params(pcm
, hw_params
);
528 err
= snd_pcm_sw_params(pcm
, This
->sw_params
);
529 snd_pcm_prepare(pcm
);
531 /* ALSA needs at least 3 buffers to work successfully */
532 This
->mmap_commitahead
= 3 * psize
;
533 while (This
->mmap_commitahead
<= 512)
534 This
->mmap_commitahead
+= psize
;
538 snd_pcm_drop(This
->pcm
);
539 snd_pcm_close(This
->pcm
);
542 snd_pcm_prepare(This
->pcm
);
543 DSDB_CreateMMAP(This
);
548 WARN("Failed to apply changes: %s\n", snd_strerror(err
));
556 snd_pcm_hw_params_current(This
->pcm
, This
->hw_params
);
558 return DSERR_BADFORMAT
;
561 static HRESULT WINAPI
IDsDriverBufferImpl_SetFormat(PIDSDRIVERBUFFER iface
, LPWAVEFORMATEX pwfx
)
563 IDsDriverBufferImpl
*This
= (IDsDriverBufferImpl
*)iface
;
566 TRACE("(%p, %p)\n", iface
, pwfx
);
569 EnterCriticalSection(&This
->pcm_crst
);
570 hr
= SetFormat(This
, pwfx
);
572 LeaveCriticalSection(&This
->pcm_crst
);
579 static HRESULT WINAPI
IDsDriverBufferImpl_SetFrequency(PIDSDRIVERBUFFER iface
, DWORD dwFreq
)
581 /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
582 FIXME("(%p,%d): stub\n",iface
,dwFreq
);
586 static HRESULT WINAPI
IDsDriverBufferImpl_SetVolumePan(PIDSDRIVERBUFFER iface
, PDSVOLUMEPAN pVolPan
)
588 IDsDriverBufferImpl
*This
= (IDsDriverBufferImpl
*)iface
;
589 FIXME("(%p,%p): stub\n",This
,pVolPan
);
590 /* TODO: Bring volume control back */
594 static HRESULT WINAPI
IDsDriverBufferImpl_SetPosition(PIDSDRIVERBUFFER iface
, DWORD dwNewPos
)
596 /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
597 /* I don't even think alsa allows this */
598 FIXME("(%p,%d): stub\n",iface
,dwNewPos
);
599 return DSERR_UNSUPPORTED
;
602 static HRESULT WINAPI
IDsDriverBufferImpl_GetPosition(PIDSDRIVERBUFFER iface
,
603 LPDWORD lpdwPlay
, LPDWORD lpdwWrite
)
605 IDsDriverBufferImpl
*This
= (IDsDriverBufferImpl
*)iface
;
606 snd_pcm_uframes_t hw_pptr
, hw_wptr
;
607 snd_pcm_state_t state
;
610 EnterCriticalSection(&This
->pcm_crst
);
614 FIXME("Bad pointer for pcm: %p\n", This
->pcm
);
615 LeaveCriticalSection(&This
->pcm_crst
);
616 return DSERR_GENERIC
;
619 if (!lpdwPlay
&& !lpdwWrite
)
622 state
= snd_pcm_state(This
->pcm
);
624 if (state
!= SND_PCM_STATE_PREPARED
&& state
!= SND_PCM_STATE_RUNNING
)
627 state
= snd_pcm_state(This
->pcm
);
629 if (state
== SND_PCM_STATE_RUNNING
)
631 snd_pcm_sframes_t used
= This
->mmap_buflen_frames
- snd_pcm_avail_update(This
->pcm
);
635 WARN("Underrun: %ld / %ld\n", used
, snd_pcm_avail_update(This
->pcm
));
638 snd_pcm_forward(This
->pcm
, -used
);
639 This
->mmap_pos
+= -used
;
640 This
->mmap_pos
%= This
->mmap_buflen_frames
;
645 if (This
->mmap_pos
> used
)
646 hw_pptr
= This
->mmap_pos
- used
;
648 hw_pptr
= This
->mmap_buflen_frames
+ This
->mmap_pos
- used
;
649 hw_pptr
%= This
->mmap_buflen_frames
;
651 TRACE("At position: %ld (%ld) - Used %ld\n", hw_pptr
, This
->mmap_pos
, used
);
653 else hw_pptr
= This
->mmap_pos
;
654 hw_wptr
= This
->mmap_pos
;
656 LeaveCriticalSection(&This
->pcm_crst
);
660 *lpdwPlay
= snd_pcm_frames_to_bytes(This
->pcm
, hw_pptr
);
662 *lpdwWrite
= snd_pcm_frames_to_bytes(This
->pcm
, hw_wptr
);
664 TRACE("hw_pptr=0x%08x, hw_wptr=0x%08x playpos=%d, writepos=%d\n", (unsigned int)hw_pptr
, (unsigned int)hw_wptr
, lpdwPlay
?*lpdwPlay
:-1, lpdwWrite
?*lpdwWrite
:-1);
668 static HRESULT WINAPI
IDsDriverBufferImpl_Play(PIDSDRIVERBUFFER iface
, DWORD dwRes1
, DWORD dwRes2
, DWORD dwFlags
)
670 IDsDriverBufferImpl
*This
= (IDsDriverBufferImpl
*)iface
;
671 TRACE("(%p,%x,%x,%x)\n",iface
,dwRes1
,dwRes2
,dwFlags
);
674 EnterCriticalSection(&This
->pcm_crst
);
675 snd_pcm_start(This
->pcm
);
677 LeaveCriticalSection(&This
->pcm_crst
);
681 static HRESULT WINAPI
IDsDriverBufferImpl_Stop(PIDSDRIVERBUFFER iface
)
683 const snd_pcm_channel_area_t
*areas
;
684 snd_pcm_uframes_t avail
;
685 snd_pcm_format_t format
;
686 IDsDriverBufferImpl
*This
= (IDsDriverBufferImpl
*)iface
;
687 TRACE("(%p)\n",iface
);
690 EnterCriticalSection(&This
->pcm_crst
);
691 avail
= This
->mmap_buflen_frames
;
692 snd_pcm_drop(This
->pcm
);
693 snd_pcm_prepare(This
->pcm
);
694 avail
= snd_pcm_avail_update(This
->pcm
);
695 snd_pcm_hw_params_get_format(This
->hw_params
, &format
);
698 snd_pcm_mmap_begin(This
->pcm
, &areas
, &This
->mmap_pos
, &avail
);
699 snd_pcm_format_set_silence(format
, areas
->addr
, This
->mmap_buflen_frames
);
700 snd_pcm_mmap_commit(This
->pcm
, This
->mmap_pos
, 0);
704 snd_pcm_format_set_silence(format
, This
->mmap_buffer
, This
->mmap_buflen_frames
);
705 snd_pcm_writei(This
->pcm
, This
->mmap_buffer
, This
->mmap_buflen_frames
);
710 LeaveCriticalSection(&This
->pcm_crst
);
714 static const IDsDriverBufferVtbl dsdbvt
=
716 IDsDriverBufferImpl_QueryInterface
,
717 IDsDriverBufferImpl_AddRef
,
718 IDsDriverBufferImpl_Release
,
719 IDsDriverBufferImpl_Lock
,
720 IDsDriverBufferImpl_Unlock
,
721 IDsDriverBufferImpl_SetFormat
,
722 IDsDriverBufferImpl_SetFrequency
,
723 IDsDriverBufferImpl_SetVolumePan
,
724 IDsDriverBufferImpl_SetPosition
,
725 IDsDriverBufferImpl_GetPosition
,
726 IDsDriverBufferImpl_Play
,
727 IDsDriverBufferImpl_Stop
730 static HRESULT WINAPI
IDsDriverImpl_QueryInterface(PIDSDRIVER iface
, REFIID riid
, LPVOID
*ppobj
)
732 /* IDsDriverImpl *This = (IDsDriverImpl *)iface; */
733 FIXME("(%p): stub!\n",iface
);
734 return DSERR_UNSUPPORTED
;
737 static ULONG WINAPI
IDsDriverImpl_AddRef(PIDSDRIVER iface
)
739 IDsDriverImpl
*This
= (IDsDriverImpl
*)iface
;
740 ULONG refCount
= InterlockedIncrement(&This
->ref
);
742 TRACE("(%p)->(ref before=%u)\n",This
, refCount
- 1);
747 static ULONG WINAPI
IDsDriverImpl_Release(PIDSDRIVER iface
)
749 IDsDriverImpl
*This
= (IDsDriverImpl
*)iface
;
750 ULONG refCount
= InterlockedDecrement(&This
->ref
);
752 TRACE("(%p)->(ref before=%u)\n",This
, refCount
+ 1);
757 HeapFree(GetProcessHeap(), 0, This
);
761 static HRESULT WINAPI
IDsDriverImpl_GetDriverDesc(PIDSDRIVER iface
, PDSDRIVERDESC pDesc
)
763 IDsDriverImpl
*This
= (IDsDriverImpl
*)iface
;
764 TRACE("(%p,%p)\n",iface
,pDesc
);
765 *pDesc
= WOutDev
[This
->wDevID
].ds_desc
;
766 pDesc
->dwFlags
= DSDDESC_DONTNEEDSECONDARYLOCK
| DSDDESC_DONTNEEDWRITELEAD
;
767 pDesc
->dnDevNode
= WOutDev
[This
->wDevID
].waveDesc
.dnDevNode
;
769 pDesc
->wReserved
= 0;
770 pDesc
->ulDeviceNum
= This
->wDevID
;
771 pDesc
->dwHeapType
= DSDHEAP_NOHEAP
;
772 pDesc
->pvDirectDrawHeap
= NULL
;
773 pDesc
->dwMemStartAddress
= 0xDEAD0000;
774 pDesc
->dwMemEndAddress
= 0xDEAF0000;
775 pDesc
->dwMemAllocExtra
= 0;
776 pDesc
->pvReserved1
= NULL
;
777 pDesc
->pvReserved2
= NULL
;
781 static HRESULT WINAPI
IDsDriverImpl_Open(PIDSDRIVER iface
)
784 IDsDriverImpl
*This
= (IDsDriverImpl
*)iface
;
786 snd_pcm_t
*pcm
= NULL
;
787 snd_pcm_hw_params_t
*hw_params
;
789 /* While this is not really needed, it is a good idea to do this,
790 * to see if sound can be initialized */
792 hw_params
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, snd_pcm_hw_params_sizeof());
796 hr
= DSERR_OUTOFMEMORY
;
800 err
= snd_pcm_open(&pcm
, WOutDev
[This
->wDevID
].pcmname
, SND_PCM_STREAM_PLAYBACK
, SND_PCM_NONBLOCK
);
801 if (err
< 0) goto err
;
802 err
= snd_pcm_hw_params_any(pcm
, hw_params
);
803 if (err
< 0) goto err
;
804 err
= snd_pcm_hw_params_set_access (pcm
, hw_params
, SND_PCM_ACCESS_MMAP_INTERLEAVED
);
806 err
= snd_pcm_hw_params_set_access (pcm
, hw_params
, SND_PCM_ACCESS_RW_INTERLEAVED
);
807 if (err
< 0) goto err
;
815 FIXME("Failed to open device: %s\n", snd_strerror(err
));
819 HeapFree(GetProcessHeap(), 0, hw_params
);
821 WARN("--> %08x\n", hr
);
825 static HRESULT WINAPI
IDsDriverImpl_Close(PIDSDRIVER iface
)
827 IDsDriverImpl
*This
= (IDsDriverImpl
*)iface
;
828 TRACE("(%p) stub, harmless\n",This
);
832 static HRESULT WINAPI
IDsDriverImpl_GetCaps(PIDSDRIVER iface
, PDSDRIVERCAPS pCaps
)
834 IDsDriverImpl
*This
= (IDsDriverImpl
*)iface
;
835 TRACE("(%p,%p)\n",iface
,pCaps
);
836 *pCaps
= WOutDev
[This
->wDevID
].ds_caps
;
840 static HRESULT WINAPI
IDsDriverImpl_CreateSoundBuffer(PIDSDRIVER iface
,
842 DWORD dwFlags
, DWORD dwCardAddress
,
843 LPDWORD pdwcbBufferSize
,
847 IDsDriverImpl
*This
= (IDsDriverImpl
*)iface
;
848 IDsDriverBufferImpl
** ippdsdb
= (IDsDriverBufferImpl
**)ppvObj
;
851 TRACE("(%p,%p,%x,%x)\n",iface
,pwfx
,dwFlags
,dwCardAddress
);
852 /* we only support primary buffers... for now */
853 if (!(dwFlags
& DSBCAPS_PRIMARYBUFFER
))
854 return DSERR_UNSUPPORTED
;
856 return DSERR_ALLOCATED
;
858 *ippdsdb
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(IDsDriverBufferImpl
));
859 if (*ippdsdb
== NULL
)
860 return DSERR_OUTOFMEMORY
;
862 (*ippdsdb
)->hw_params
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, snd_pcm_hw_params_sizeof());
863 (*ippdsdb
)->sw_params
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, snd_pcm_sw_params_sizeof());
864 if (!(*ippdsdb
)->hw_params
|| !(*ippdsdb
)->sw_params
)
866 HeapFree(GetProcessHeap(), 0, (*ippdsdb
)->sw_params
);
867 HeapFree(GetProcessHeap(), 0, (*ippdsdb
)->hw_params
);
868 return DSERR_OUTOFMEMORY
;
870 (*ippdsdb
)->lpVtbl
= &dsdbvt
;
872 (*ippdsdb
)->drv
= This
;
873 InitializeCriticalSection(&(*ippdsdb
)->pcm_crst
);
874 (*ippdsdb
)->pcm_crst
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": ALSA_DSOUTPUT.pcm_crst");
876 /* SetFormat has to re-initialize pcm here anyway */
877 err
= SetFormat(*ippdsdb
, pwfx
);
880 WARN("Error occurred: %08x\n", err
);
884 if (dwFlags
& DSBCAPS_PRIMARYBUFFER
)
885 This
->primary
= *ippdsdb
;
887 *pdwcbBufferSize
= (*ippdsdb
)->mmap_buflen_bytes
;
888 *ppbBuffer
= (*ippdsdb
)->mmap_buffer
;
890 /* buffer is ready to go */
891 TRACE("buffer created at %p\n", *ippdsdb
);
895 HeapFree(GetProcessHeap(), 0, (*ippdsdb
)->sw_params
);
896 HeapFree(GetProcessHeap(), 0, (*ippdsdb
)->hw_params
);
897 HeapFree(GetProcessHeap(), 0, *ippdsdb
);
902 static HRESULT WINAPI
IDsDriverImpl_DuplicateSoundBuffer(PIDSDRIVER iface
,
903 PIDSDRIVERBUFFER pBuffer
,
906 IDsDriverImpl
*This
= (IDsDriverImpl
*)iface
;
907 FIXME("(%p,%p): stub\n",This
,pBuffer
);
908 return DSERR_INVALIDCALL
;
911 static const IDsDriverVtbl dsdvt
=
913 IDsDriverImpl_QueryInterface
,
914 IDsDriverImpl_AddRef
,
915 IDsDriverImpl_Release
,
916 IDsDriverImpl_GetDriverDesc
,
919 IDsDriverImpl_GetCaps
,
920 IDsDriverImpl_CreateSoundBuffer
,
921 IDsDriverImpl_DuplicateSoundBuffer
924 DWORD
wodDsCreate(UINT wDevID
, PIDSDRIVER
* drv
)
926 IDsDriverImpl
** idrv
= (IDsDriverImpl
**)drv
;
928 TRACE("driver created\n");
930 *idrv
= HeapAlloc(GetProcessHeap(),0,sizeof(IDsDriverImpl
));
932 return MMSYSERR_NOMEM
;
933 (*idrv
)->lpVtbl
= &dsdvt
;
936 (*idrv
)->wDevID
= wDevID
;
937 (*idrv
)->primary
= NULL
;
938 return MMSYSERR_NOERROR
;
941 DWORD
wodDsDesc(UINT wDevID
, PDSDRIVERDESC desc
)
943 *desc
= WOutDev
[wDevID
].ds_desc
;
944 return MMSYSERR_NOERROR
;
947 #endif /* HAVE_ALSA */