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"
42 #ifdef HAVE_SYS_IOCTL_H
43 # include <sys/ioctl.h>
45 #ifdef HAVE_SYS_MMAN_H
46 # include <sys/mman.h>
56 #include "wine/library.h"
57 #include "wine/unicode.h"
58 #include "wine/debug.h"
62 WINE_DEFAULT_DEBUG_CHANNEL(dsalsa
);
64 typedef struct IDsDriverImpl IDsDriverImpl
;
65 typedef struct IDsDriverBufferImpl IDsDriverBufferImpl
;
70 const IDsDriverVtbl
*lpVtbl
;
73 /* IDsDriverImpl fields */
74 IDsDriverBufferImpl
* primary
;
78 struct IDsDriverBufferImpl
80 const IDsDriverBufferVtbl
*lpVtbl
;
84 CRITICAL_SECTION pcm_crst
;
86 DWORD mmap_buflen_bytes
;
89 snd_pcm_hw_params_t
*hw_params
;
90 snd_pcm_sw_params_t
*sw_params
;
91 snd_pcm_uframes_t mmap_buflen_frames
, mmap_pos
, mmap_commitahead
;
94 /** Fill buffers, for starting and stopping
95 * Alsa won't start playing until everything is filled up
96 * This also updates mmap_pos
98 * Returns: Amount of periods in use so snd_pcm_avail_update
99 * doesn't have to be called up to 4x in GetPosition()
101 static snd_pcm_uframes_t
CommitAll(IDsDriverBufferImpl
*This
)
103 const snd_pcm_channel_area_t
*areas
;
104 snd_pcm_uframes_t used
;
105 const snd_pcm_uframes_t commitahead
= This
->mmap_commitahead
;
107 used
= This
->mmap_buflen_frames
- snd_pcm_avail_update(This
->pcm
);
108 TRACE("%p needs to commit to %lu, used: %lu\n", This
, commitahead
, used
);
109 if (used
< commitahead
)
111 snd_pcm_uframes_t done
, putin
= commitahead
- used
;
112 snd_pcm_mmap_begin(This
->pcm
, &areas
, &This
->mmap_pos
, &putin
);
113 done
= snd_pcm_mmap_commit(This
->pcm
, This
->mmap_pos
, putin
);
114 This
->mmap_pos
+= done
;
116 putin
= commitahead
- used
;
118 if (This
->mmap_pos
== This
->mmap_buflen_frames
&& (snd_pcm_sframes_t
)putin
> 0)
120 snd_pcm_mmap_begin(This
->pcm
, &areas
, &This
->mmap_pos
, &putin
);
121 done
= snd_pcm_mmap_commit(This
->pcm
, This
->mmap_pos
, putin
);
122 This
->mmap_pos
+= done
;
127 if (This
->mmap_pos
== This
->mmap_buflen_frames
)
133 static void CheckXRUN(IDsDriverBufferImpl
* This
)
135 snd_pcm_state_t state
= snd_pcm_state(This
->pcm
);
136 snd_pcm_sframes_t delay
;
139 snd_pcm_hwsync(This
->pcm
);
140 snd_pcm_delay(This
->pcm
, &delay
);
141 if ( state
== SND_PCM_STATE_XRUN
)
143 err
= snd_pcm_prepare(This
->pcm
);
145 snd_pcm_start(This
->pcm
);
146 WARN("xrun occurred\n");
148 ERR("recovery from xrun failed, prepare failed: %s\n", snd_strerror(err
));
150 else if ( state
== SND_PCM_STATE_SUSPENDED
)
152 int err
= snd_pcm_resume(This
->pcm
);
153 TRACE("recovery from suspension occurred\n");
154 if (err
< 0 && err
!= -EAGAIN
){
155 err
= snd_pcm_prepare(This
->pcm
);
157 ERR("recovery from suspend failed, prepare failed: %s\n", snd_strerror(err
));
159 } else if ( state
!= SND_PCM_STATE_RUNNING
) {
160 FIXME("Unhandled state: %d\n", state
);
165 * Allocate the memory-mapped buffer for direct sound, and set up the
168 static int DSDB_CreateMMAP(IDsDriverBufferImpl
* pdbi
)
170 snd_pcm_t
*pcm
= pdbi
->pcm
;
171 snd_pcm_format_t format
;
172 snd_pcm_uframes_t frames
, ofs
, avail
, psize
, boundary
;
173 unsigned int channels
, bits_per_sample
, bits_per_frame
;
175 const snd_pcm_channel_area_t
*areas
;
176 snd_pcm_hw_params_t
*hw_params
= pdbi
->hw_params
;
177 snd_pcm_sw_params_t
*sw_params
= pdbi
->sw_params
;
179 mmap_mode
= snd_pcm_type(pcm
);
181 if (mmap_mode
== SND_PCM_TYPE_HW
)
182 TRACE("mmap'd buffer is a direct hardware buffer.\n");
183 else if (mmap_mode
== SND_PCM_TYPE_DMIX
)
184 TRACE("mmap'd buffer is an ALSA dmix buffer\n");
186 TRACE("mmap'd buffer is an ALSA type %d buffer\n", mmap_mode
);
188 err
= snd_pcm_hw_params_get_period_size(hw_params
, &psize
, NULL
);
190 err
= snd_pcm_hw_params_get_format(hw_params
, &format
);
191 err
= snd_pcm_hw_params_get_buffer_size(hw_params
, &frames
);
192 err
= snd_pcm_hw_params_get_channels(hw_params
, &channels
);
193 bits_per_sample
= snd_pcm_format_physical_width(format
);
194 bits_per_frame
= bits_per_sample
* channels
;
196 if (TRACE_ON(dsalsa
))
197 ALSA_TraceParameters(hw_params
, NULL
, FALSE
);
199 TRACE("format=%s frames=%ld channels=%d bits_per_sample=%d bits_per_frame=%d\n",
200 snd_pcm_format_name(format
), frames
, channels
, bits_per_sample
, bits_per_frame
);
202 pdbi
->mmap_buflen_frames
= frames
;
203 pdbi
->mmap_buflen_bytes
= snd_pcm_frames_to_bytes( pcm
, frames
);
205 snd_pcm_sw_params_current(pcm
, sw_params
);
206 snd_pcm_sw_params_set_start_threshold(pcm
, sw_params
, 0);
207 snd_pcm_sw_params_get_boundary(sw_params
, &boundary
);
208 snd_pcm_sw_params_set_stop_threshold(pcm
, sw_params
, boundary
);
209 snd_pcm_sw_params_set_silence_threshold(pcm
, sw_params
, INT_MAX
);
210 snd_pcm_sw_params_set_silence_size(pcm
, sw_params
, 0);
211 snd_pcm_sw_params_set_avail_min(pcm
, sw_params
, 0);
212 snd_pcm_sw_params_set_xrun_mode(pcm
, sw_params
, SND_PCM_XRUN_NONE
);
213 err
= snd_pcm_sw_params(pcm
, sw_params
);
215 avail
= snd_pcm_avail_update(pcm
);
218 ERR("No buffer is available: %s.\n", snd_strerror(avail
));
219 return DSERR_GENERIC
;
221 err
= snd_pcm_mmap_begin(pcm
, &areas
, &ofs
, &avail
);
224 ERR("Can't map sound device for direct access: %s\n", snd_strerror(err
));
225 return DSERR_GENERIC
;
227 snd_pcm_format_set_silence(format
, areas
->addr
, pdbi
->mmap_buflen_frames
);
228 pdbi
->mmap_pos
= ofs
+ snd_pcm_mmap_commit(pcm
, ofs
, 0);
229 pdbi
->mmap_buffer
= areas
->addr
;
231 TRACE("created mmap buffer of %ld frames (%d bytes) at %p\n",
232 frames
, pdbi
->mmap_buflen_bytes
, pdbi
->mmap_buffer
);
237 static HRESULT WINAPI
IDsDriverBufferImpl_QueryInterface(PIDSDRIVERBUFFER iface
, REFIID riid
, LPVOID
*ppobj
)
239 /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
240 FIXME("(): stub!\n");
241 return DSERR_UNSUPPORTED
;
244 static ULONG WINAPI
IDsDriverBufferImpl_AddRef(PIDSDRIVERBUFFER iface
)
246 IDsDriverBufferImpl
*This
= (IDsDriverBufferImpl
*)iface
;
247 ULONG refCount
= InterlockedIncrement(&This
->ref
);
249 TRACE("(%p)->(ref before=%u)\n",This
, refCount
- 1);
254 static ULONG WINAPI
IDsDriverBufferImpl_Release(PIDSDRIVERBUFFER iface
)
256 IDsDriverBufferImpl
*This
= (IDsDriverBufferImpl
*)iface
;
257 ULONG refCount
= InterlockedDecrement(&This
->ref
);
259 TRACE("(%p)->(ref before=%u)\n",This
, refCount
+ 1);
264 TRACE("mmap buffer %p destroyed\n", This
->mmap_buffer
);
266 if (This
== This
->drv
->primary
)
267 This
->drv
->primary
= NULL
;
269 This
->pcm_crst
.DebugInfo
->Spare
[0] = 0;
270 DeleteCriticalSection(&This
->pcm_crst
);
272 snd_pcm_drop(This
->pcm
);
273 snd_pcm_close(This
->pcm
);
275 HeapFree(GetProcessHeap(), 0, This
->sw_params
);
276 HeapFree(GetProcessHeap(), 0, This
->hw_params
);
277 HeapFree(GetProcessHeap(), 0, This
);
281 static HRESULT WINAPI
IDsDriverBufferImpl_Lock(PIDSDRIVERBUFFER iface
,
282 LPVOID
*ppvAudio1
,LPDWORD pdwLen1
,
283 LPVOID
*ppvAudio2
,LPDWORD pdwLen2
,
284 DWORD dwWritePosition
,DWORD dwWriteLen
,
287 IDsDriverBufferImpl
*This
= (IDsDriverBufferImpl
*)iface
;
288 snd_pcm_uframes_t writepos
;
290 TRACE("%d bytes from %d\n", dwWriteLen
, dwWritePosition
);
293 EnterCriticalSection(&This
->pcm_crst
);
295 if (dwWriteLen
> This
->mmap_buflen_bytes
|| dwWritePosition
>= This
->mmap_buflen_bytes
)
298 LeaveCriticalSection(&This
->pcm_crst
);
299 return DSERR_INVALIDPARAM
;
302 if (ppvAudio2
) *ppvAudio2
= NULL
;
303 if (pdwLen2
) *pdwLen2
= 0;
305 *ppvAudio1
= (LPBYTE
)This
->mmap_buffer
+ dwWritePosition
;
306 *pdwLen1
= dwWriteLen
;
308 if (dwWritePosition
+dwWriteLen
> This
->mmap_buflen_bytes
)
310 DWORD remainder
= This
->mmap_buflen_bytes
- dwWritePosition
;
311 *pdwLen1
= remainder
;
313 if (ppvAudio2
&& pdwLen2
)
315 *ppvAudio2
= This
->mmap_buffer
;
316 *pdwLen2
= dwWriteLen
- remainder
;
318 else dwWriteLen
= remainder
;
321 writepos
= snd_pcm_bytes_to_frames(This
->pcm
, dwWritePosition
);
322 if (writepos
== This
->mmap_pos
)
324 const snd_pcm_channel_area_t
*areas
;
325 snd_pcm_uframes_t writelen
= snd_pcm_bytes_to_frames(This
->pcm
, dwWriteLen
), putin
= writelen
;
326 TRACE("Hit mmap_pos, locking data!\n");
327 snd_pcm_mmap_begin(This
->pcm
, &areas
, &This
->mmap_pos
, &putin
);
330 LeaveCriticalSection(&This
->pcm_crst
);
335 static HRESULT WINAPI
IDsDriverBufferImpl_Unlock(PIDSDRIVERBUFFER iface
,
336 LPVOID pvAudio1
,DWORD dwLen1
,
337 LPVOID pvAudio2
,DWORD dwLen2
)
339 IDsDriverBufferImpl
*This
= (IDsDriverBufferImpl
*)iface
;
340 snd_pcm_uframes_t writepos
;
346 EnterCriticalSection(&This
->pcm_crst
);
348 writepos
= snd_pcm_bytes_to_frames(This
->pcm
, (DWORD_PTR
)pvAudio1
- (DWORD_PTR
)This
->mmap_buffer
);
349 if (writepos
== This
->mmap_pos
)
351 const snd_pcm_channel_area_t
*areas
;
352 snd_pcm_uframes_t writelen
= snd_pcm_bytes_to_frames(This
->pcm
, dwLen1
);
353 TRACE("Committing data\n");
354 This
->mmap_pos
+= snd_pcm_mmap_commit(This
->pcm
, This
->mmap_pos
, writelen
);
355 if (This
->mmap_pos
== This
->mmap_buflen_frames
)
357 if (!This
->mmap_pos
&& dwLen2
)
359 writelen
= snd_pcm_bytes_to_frames(This
->pcm
, dwLen2
);
360 snd_pcm_mmap_begin(This
->pcm
, &areas
, &This
->mmap_pos
, &writelen
);
361 This
->mmap_pos
+= snd_pcm_mmap_commit(This
->pcm
, This
->mmap_pos
, writelen
);
362 assert(This
->mmap_pos
< This
->mmap_buflen_frames
);
365 LeaveCriticalSection(&This
->pcm_crst
);
371 static HRESULT
SetFormat(IDsDriverBufferImpl
*This
, LPWAVEFORMATEX pwfx
, BOOL forced
)
373 snd_pcm_t
*pcm
= NULL
;
374 snd_pcm_hw_params_t
*hw_params
= This
->hw_params
;
375 unsigned int buffer_time
= 500000;
376 snd_pcm_format_t format
= -1;
377 snd_pcm_uframes_t psize
;
378 DWORD rate
= pwfx
->nSamplesPerSec
;
381 switch (pwfx
->wBitsPerSample
)
383 case 8: format
= SND_PCM_FORMAT_U8
; break;
384 case 16: format
= SND_PCM_FORMAT_S16_LE
; break;
385 case 24: format
= SND_PCM_FORMAT_S24_LE
; break;
386 case 32: format
= SND_PCM_FORMAT_S32_LE
; break;
387 default: FIXME("Unsupported bpp: %d\n", pwfx
->wBitsPerSample
); return DSERR_GENERIC
;
391 EnterCriticalSection(&This
->pcm_crst
);
393 err
= snd_pcm_open(&pcm
, WOutDev
[This
->drv
->wDevID
].pcmname
, SND_PCM_STREAM_PLAYBACK
, SND_PCM_NONBLOCK
);
397 if (errno
!= EBUSY
|| !This
->pcm
)
400 LeaveCriticalSection(&This
->pcm_crst
);
401 WARN("Cannot open sound device: %s\n", snd_strerror(err
));
402 return DSERR_GENERIC
;
404 snd_pcm_drop(This
->pcm
);
405 snd_pcm_close(This
->pcm
);
407 err
= snd_pcm_open(&pcm
, WOutDev
[This
->drv
->wDevID
].pcmname
, SND_PCM_STREAM_PLAYBACK
, SND_PCM_NONBLOCK
);
411 LeaveCriticalSection(&This
->pcm_crst
);
412 WARN("Cannot open sound device: %s\n", snd_strerror(err
));
413 return DSERR_BUFFERLOST
;
417 /* Set some defaults */
418 snd_pcm_hw_params_any(pcm
, hw_params
);
419 err
= snd_pcm_hw_params_set_channels(pcm
, hw_params
, pwfx
->nChannels
);
420 if (err
< 0) { WARN("Could not set channels to %d\n", pwfx
->nChannels
); goto err
; }
422 err
= snd_pcm_hw_params_set_format(pcm
, hw_params
, format
);
423 if (err
< 0) { WARN("Could not set format to %d bpp\n", pwfx
->wBitsPerSample
); goto err
; }
425 /* Alsa's rate resampling is only used if the application specifically requests
426 * a buffer at a certain frequency, else it is better to disable it due to unwanted
427 * side effects, which may include: Less granular pointer, changing buffer sizes, etc
429 #if SND_LIB_VERSION >= 0x010009
430 snd_pcm_hw_params_set_rate_resample(pcm
, hw_params
, 0 && forced
);
433 err
= snd_pcm_hw_params_set_rate_near(pcm
, hw_params
, &rate
, NULL
);
434 if (err
< 0) { rate
= pwfx
->nSamplesPerSec
; WARN("Could not set rate\n"); goto err
; }
436 if (!ALSA_NearMatch(rate
, pwfx
->nSamplesPerSec
))
438 WARN("Could not set sound rate to %d, but instead to %d\n", pwfx
->nSamplesPerSec
, rate
);
439 pwfx
->nSamplesPerSec
= rate
;
440 pwfx
->nAvgBytesPerSec
= rate
* pwfx
->nBlockAlign
;
441 /* Let DirectSound detect this */
444 snd_pcm_hw_params_set_periods_integer(pcm
, hw_params
);
445 snd_pcm_hw_params_set_buffer_time_near(pcm
, hw_params
, &buffer_time
, NULL
);
447 snd_pcm_hw_params_set_period_time_near(pcm
, hw_params
, &buffer_time
, NULL
);
448 err
= snd_pcm_hw_params(pcm
, hw_params
);
449 err
= snd_pcm_sw_params(pcm
, This
->sw_params
);
450 snd_pcm_prepare(pcm
);
452 err
= snd_pcm_hw_params_get_period_size(hw_params
, &psize
, NULL
);
453 TRACE("Period size is: %lu\n", psize
);
455 /* ALSA needs at least 3 buffers to work successfully */
456 This
->mmap_commitahead
= 3 * psize
;
457 while (This
->mmap_commitahead
<= 512)
458 This
->mmap_commitahead
+= psize
;
462 snd_pcm_drop(This
->pcm
);
463 snd_pcm_close(This
->pcm
);
467 snd_pcm_prepare(This
->pcm
);
468 DSDB_CreateMMAP(This
);
471 LeaveCriticalSection(&This
->pcm_crst
);
476 WARN("Failed to apply changes: %s\n", snd_strerror(err
));
484 snd_pcm_hw_params_current(This
->pcm
, This
->hw_params
);
487 LeaveCriticalSection(&This
->pcm_crst
);
488 return DSERR_BADFORMAT
;
491 static HRESULT WINAPI
IDsDriverBufferImpl_SetFormat(PIDSDRIVERBUFFER iface
, LPWAVEFORMATEX pwfx
)
493 IDsDriverBufferImpl
*This
= (IDsDriverBufferImpl
*)iface
;
496 TRACE("(%p, %p)\n", iface
, pwfx
);
498 hr
= SetFormat(This
, pwfx
, TRUE
);
501 /* Buffer size / Location changed, so tell dsound to recreate */
502 return DSERR_BUFFERLOST
;
506 static HRESULT WINAPI
IDsDriverBufferImpl_SetFrequency(PIDSDRIVERBUFFER iface
, DWORD dwFreq
)
508 /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
509 FIXME("(%p,%d): stub\n",iface
,dwFreq
);
513 static HRESULT WINAPI
IDsDriverBufferImpl_SetVolumePan(PIDSDRIVERBUFFER iface
, PDSVOLUMEPAN pVolPan
)
515 IDsDriverBufferImpl
*This
= (IDsDriverBufferImpl
*)iface
;
516 FIXME("(%p,%p): stub\n",This
,pVolPan
);
517 /* TODO: Bring volume control back */
521 static HRESULT WINAPI
IDsDriverBufferImpl_SetPosition(PIDSDRIVERBUFFER iface
, DWORD dwNewPos
)
523 /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
524 /* I don't even think alsa allows this */
525 FIXME("(%p,%d): stub\n",iface
,dwNewPos
);
526 return DSERR_UNSUPPORTED
;
529 static HRESULT WINAPI
IDsDriverBufferImpl_GetPosition(PIDSDRIVERBUFFER iface
,
530 LPDWORD lpdwPlay
, LPDWORD lpdwWrite
)
532 IDsDriverBufferImpl
*This
= (IDsDriverBufferImpl
*)iface
;
533 snd_pcm_uframes_t hw_pptr
, hw_wptr
;
534 snd_pcm_state_t state
;
537 EnterCriticalSection(&This
->pcm_crst
);
541 FIXME("Bad pointer for pcm: %p\n", This
->pcm
);
542 LeaveCriticalSection(&This
->pcm_crst
);
543 return DSERR_GENERIC
;
546 if (!lpdwPlay
&& !lpdwWrite
)
549 state
= snd_pcm_state(This
->pcm
);
551 if (state
!= SND_PCM_STATE_PREPARED
&& state
!= SND_PCM_STATE_RUNNING
)
554 state
= snd_pcm_state(This
->pcm
);
556 if (state
== SND_PCM_STATE_RUNNING
)
558 snd_pcm_uframes_t used
= This
->mmap_buflen_frames
- snd_pcm_avail_update(This
->pcm
);
560 if (This
->mmap_pos
> used
)
561 hw_pptr
= This
->mmap_pos
- used
;
563 hw_pptr
= This
->mmap_buflen_frames
+ This
->mmap_pos
- used
;
564 hw_pptr
%= This
->mmap_buflen_frames
;
566 TRACE("At position: %ld (%ld) - Used %ld\n", hw_pptr
, This
->mmap_pos
, used
);
568 else hw_pptr
= This
->mmap_pos
;
569 hw_wptr
= This
->mmap_pos
;
571 LeaveCriticalSection(&This
->pcm_crst
);
575 *lpdwPlay
= snd_pcm_frames_to_bytes(This
->pcm
, hw_pptr
);
577 *lpdwWrite
= snd_pcm_frames_to_bytes(This
->pcm
, hw_wptr
);
579 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);
583 static HRESULT WINAPI
IDsDriverBufferImpl_Play(PIDSDRIVERBUFFER iface
, DWORD dwRes1
, DWORD dwRes2
, DWORD dwFlags
)
585 IDsDriverBufferImpl
*This
= (IDsDriverBufferImpl
*)iface
;
586 TRACE("(%p,%x,%x,%x)\n",iface
,dwRes1
,dwRes2
,dwFlags
);
589 EnterCriticalSection(&This
->pcm_crst
);
591 snd_pcm_start(This
->pcm
);
593 LeaveCriticalSection(&This
->pcm_crst
);
597 static HRESULT WINAPI
IDsDriverBufferImpl_Stop(PIDSDRIVERBUFFER iface
)
599 const snd_pcm_channel_area_t
*areas
;
600 snd_pcm_uframes_t avail
;
601 snd_pcm_format_t format
;
602 IDsDriverBufferImpl
*This
= (IDsDriverBufferImpl
*)iface
;
603 TRACE("(%p)\n",iface
);
606 EnterCriticalSection(&This
->pcm_crst
);
607 avail
= This
->mmap_buflen_frames
;
608 snd_pcm_drop(This
->pcm
);
609 snd_pcm_prepare(This
->pcm
);
610 snd_pcm_mmap_begin(This
->pcm
, &areas
, &This
->mmap_pos
, &avail
);
611 snd_pcm_hw_params_get_format(This
->hw_params
, &format
);
612 snd_pcm_format_set_silence(format
, areas
->addr
, This
->mmap_buflen_frames
);
613 This
->mmap_pos
+= snd_pcm_mmap_commit(This
->pcm
, This
->mmap_pos
, avail
);
616 LeaveCriticalSection(&This
->pcm_crst
);
620 static const IDsDriverBufferVtbl dsdbvt
=
622 IDsDriverBufferImpl_QueryInterface
,
623 IDsDriverBufferImpl_AddRef
,
624 IDsDriverBufferImpl_Release
,
625 IDsDriverBufferImpl_Lock
,
626 IDsDriverBufferImpl_Unlock
,
627 IDsDriverBufferImpl_SetFormat
,
628 IDsDriverBufferImpl_SetFrequency
,
629 IDsDriverBufferImpl_SetVolumePan
,
630 IDsDriverBufferImpl_SetPosition
,
631 IDsDriverBufferImpl_GetPosition
,
632 IDsDriverBufferImpl_Play
,
633 IDsDriverBufferImpl_Stop
636 static HRESULT WINAPI
IDsDriverImpl_QueryInterface(PIDSDRIVER iface
, REFIID riid
, LPVOID
*ppobj
)
638 /* IDsDriverImpl *This = (IDsDriverImpl *)iface; */
639 FIXME("(%p): stub!\n",iface
);
640 return DSERR_UNSUPPORTED
;
643 static ULONG WINAPI
IDsDriverImpl_AddRef(PIDSDRIVER iface
)
645 IDsDriverImpl
*This
= (IDsDriverImpl
*)iface
;
646 ULONG refCount
= InterlockedIncrement(&This
->ref
);
648 TRACE("(%p)->(ref before=%u)\n",This
, refCount
- 1);
653 static ULONG WINAPI
IDsDriverImpl_Release(PIDSDRIVER iface
)
655 IDsDriverImpl
*This
= (IDsDriverImpl
*)iface
;
656 ULONG refCount
= InterlockedDecrement(&This
->ref
);
658 TRACE("(%p)->(ref before=%u)\n",This
, refCount
+ 1);
663 HeapFree(GetProcessHeap(), 0, This
);
667 static HRESULT WINAPI
IDsDriverImpl_GetDriverDesc(PIDSDRIVER iface
, PDSDRIVERDESC pDesc
)
669 IDsDriverImpl
*This
= (IDsDriverImpl
*)iface
;
670 TRACE("(%p,%p)\n",iface
,pDesc
);
671 memcpy(pDesc
, &(WOutDev
[This
->wDevID
].ds_desc
), sizeof(DSDRIVERDESC
));
672 pDesc
->dwFlags
= DSDDESC_DONTNEEDSECONDARYLOCK
| DSDDESC_DONTNEEDWRITELEAD
;
673 pDesc
->dnDevNode
= WOutDev
[This
->wDevID
].waveDesc
.dnDevNode
;
675 pDesc
->wReserved
= 0;
676 pDesc
->ulDeviceNum
= This
->wDevID
;
677 pDesc
->dwHeapType
= DSDHEAP_NOHEAP
;
678 pDesc
->pvDirectDrawHeap
= NULL
;
679 pDesc
->dwMemStartAddress
= 0xDEAD0000;
680 pDesc
->dwMemEndAddress
= 0xDEAF0000;
681 pDesc
->dwMemAllocExtra
= 0;
682 pDesc
->pvReserved1
= NULL
;
683 pDesc
->pvReserved2
= NULL
;
687 static HRESULT WINAPI
IDsDriverImpl_Open(PIDSDRIVER iface
)
690 IDsDriverImpl
*This
= (IDsDriverImpl
*)iface
;
692 snd_pcm_t
*pcm
= NULL
;
693 snd_pcm_hw_params_t
*hw_params
;
695 /* While this is not really needed, it is a good idea to do this,
696 * to see if sound can be initialized */
698 hw_params
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, snd_pcm_hw_params_sizeof());
702 hr
= DSERR_OUTOFMEMORY
;
706 err
= snd_pcm_open(&pcm
, WOutDev
[This
->wDevID
].pcmname
, SND_PCM_STREAM_PLAYBACK
, SND_PCM_NONBLOCK
);
707 if (err
< 0) goto err
;
708 err
= snd_pcm_hw_params_any(pcm
, hw_params
);
709 if (err
< 0) goto err
;
710 err
= snd_pcm_hw_params_set_access (pcm
, hw_params
, SND_PCM_ACCESS_MMAP_INTERLEAVED
);
711 if (err
< 0) goto err
;
719 FIXME("Failed to open device: %s\n", snd_strerror(err
));
723 HeapFree(GetProcessHeap(), 0, hw_params
);
725 WARN("--> %08x\n", hr
);
729 static HRESULT WINAPI
IDsDriverImpl_Close(PIDSDRIVER iface
)
731 IDsDriverImpl
*This
= (IDsDriverImpl
*)iface
;
732 TRACE("(%p) stub, harmless\n",This
);
736 static HRESULT WINAPI
IDsDriverImpl_GetCaps(PIDSDRIVER iface
, PDSDRIVERCAPS pCaps
)
738 IDsDriverImpl
*This
= (IDsDriverImpl
*)iface
;
739 TRACE("(%p,%p)\n",iface
,pCaps
);
740 memcpy(pCaps
, &(WOutDev
[This
->wDevID
].ds_caps
), sizeof(DSDRIVERCAPS
));
744 static HRESULT WINAPI
IDsDriverImpl_CreateSoundBuffer(PIDSDRIVER iface
,
746 DWORD dwFlags
, DWORD dwCardAddress
,
747 LPDWORD pdwcbBufferSize
,
751 IDsDriverImpl
*This
= (IDsDriverImpl
*)iface
;
752 IDsDriverBufferImpl
** ippdsdb
= (IDsDriverBufferImpl
**)ppvObj
;
755 TRACE("(%p,%p,%x,%x)\n",iface
,pwfx
,dwFlags
,dwCardAddress
);
756 /* we only support primary buffers... for now */
757 if (!(dwFlags
& DSBCAPS_PRIMARYBUFFER
))
758 return DSERR_UNSUPPORTED
;
760 return DSERR_ALLOCATED
;
762 *ippdsdb
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(IDsDriverBufferImpl
));
763 if (*ippdsdb
== NULL
)
764 return DSERR_OUTOFMEMORY
;
766 (*ippdsdb
)->hw_params
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, snd_pcm_hw_params_sizeof());
767 (*ippdsdb
)->sw_params
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, snd_pcm_sw_params_sizeof());
768 if (!(*ippdsdb
)->hw_params
|| !(*ippdsdb
)->sw_params
)
770 HeapFree(GetProcessHeap(), 0, (*ippdsdb
)->sw_params
);
771 HeapFree(GetProcessHeap(), 0, (*ippdsdb
)->hw_params
);
772 return DSERR_OUTOFMEMORY
;
774 (*ippdsdb
)->lpVtbl
= &dsdbvt
;
776 (*ippdsdb
)->drv
= This
;
777 InitializeCriticalSection(&(*ippdsdb
)->pcm_crst
);
778 (*ippdsdb
)->pcm_crst
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": ALSA_DSOUTPUT.pcm_crst");
780 /* SetFormat has to re-initialize pcm here anyway */
781 err
= SetFormat(*ippdsdb
, pwfx
, FALSE
);
784 WARN("Error occurred: %08x\n", err
);
788 if (dwFlags
& DSBCAPS_PRIMARYBUFFER
)
789 This
->primary
= *ippdsdb
;
791 *pdwcbBufferSize
= (*ippdsdb
)->mmap_buflen_bytes
;
792 *ppbBuffer
= (*ippdsdb
)->mmap_buffer
;
794 /* buffer is ready to go */
795 TRACE("buffer created at %p\n", *ippdsdb
);
799 HeapFree(GetProcessHeap(), 0, (*ippdsdb
)->sw_params
);
800 HeapFree(GetProcessHeap(), 0, (*ippdsdb
)->hw_params
);
801 HeapFree(GetProcessHeap(), 0, *ippdsdb
);
806 static HRESULT WINAPI
IDsDriverImpl_DuplicateSoundBuffer(PIDSDRIVER iface
,
807 PIDSDRIVERBUFFER pBuffer
,
810 IDsDriverImpl
*This
= (IDsDriverImpl
*)iface
;
811 FIXME("(%p,%p): stub\n",This
,pBuffer
);
812 return DSERR_INVALIDCALL
;
815 static const IDsDriverVtbl dsdvt
=
817 IDsDriverImpl_QueryInterface
,
818 IDsDriverImpl_AddRef
,
819 IDsDriverImpl_Release
,
820 IDsDriverImpl_GetDriverDesc
,
823 IDsDriverImpl_GetCaps
,
824 IDsDriverImpl_CreateSoundBuffer
,
825 IDsDriverImpl_DuplicateSoundBuffer
828 DWORD
wodDsCreate(UINT wDevID
, PIDSDRIVER
* drv
)
830 IDsDriverImpl
** idrv
= (IDsDriverImpl
**)drv
;
832 TRACE("driver created\n");
834 /* the HAL isn't much better than the HEL if we can't do mmap() */
835 if (!(WOutDev
[wDevID
].outcaps
.dwSupport
& WAVECAPS_DIRECTSOUND
))
837 WARN("MMAP not supported for this device, falling back to waveout, should be harmless\n");
838 return MMSYSERR_NOTSUPPORTED
;
841 *idrv
= HeapAlloc(GetProcessHeap(),0,sizeof(IDsDriverImpl
));
843 return MMSYSERR_NOMEM
;
844 (*idrv
)->lpVtbl
= &dsdvt
;
847 (*idrv
)->wDevID
= wDevID
;
848 (*idrv
)->primary
= NULL
;
849 return MMSYSERR_NOERROR
;
852 DWORD
wodDsDesc(UINT wDevID
, PDSDRIVERDESC desc
)
854 memcpy(desc
, &(WOutDev
[wDevID
].ds_desc
), sizeof(DSDRIVERDESC
));
855 return MMSYSERR_NOERROR
;
858 #endif /* HAVE_ALSA */