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_writeahead
;
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 static void CommitAll(IDsDriverBufferImpl
*This
)
100 const snd_pcm_channel_area_t
*areas
;
101 snd_pcm_uframes_t used
;
102 const snd_pcm_uframes_t writeahead
= This
->mmap_writeahead
;
104 TRACE("%p want to %ld\n", This
, writeahead
);
105 used
= This
->mmap_buflen_frames
- snd_pcm_avail_update(This
->pcm
);
106 if (used
< writeahead
)
108 snd_pcm_uframes_t putin
= writeahead
- used
;
109 snd_pcm_mmap_begin(This
->pcm
, &areas
, &This
->mmap_pos
, &putin
);
110 This
->mmap_pos
+= snd_pcm_mmap_commit(This
->pcm
, This
->mmap_pos
, putin
);
112 used
= This
->mmap_buflen_frames
- snd_pcm_avail_update(This
->pcm
);
113 if (This
->mmap_pos
== This
->mmap_buflen_frames
)
116 used
= This
->mmap_buflen_frames
- snd_pcm_avail_update(This
->pcm
);
119 if ((snd_pcm_sframes_t
)putin
> 0)
121 snd_pcm_mmap_begin(This
->pcm
, &areas
, &This
->mmap_pos
, &putin
);
122 This
->mmap_pos
+= snd_pcm_mmap_commit(This
->pcm
, This
->mmap_pos
, putin
);
129 static void CheckXRUN(IDsDriverBufferImpl
* This
)
131 snd_pcm_state_t state
= snd_pcm_state(This
->pcm
);
132 snd_pcm_sframes_t delay
;
135 snd_pcm_hwsync(This
->pcm
);
136 snd_pcm_delay(This
->pcm
, &delay
);
137 if ( state
== SND_PCM_STATE_XRUN
)
139 err
= snd_pcm_prepare(This
->pcm
);
141 snd_pcm_start(This
->pcm
);
142 WARN("xrun occurred\n");
144 ERR("recovery from xrun failed, prepare failed: %s\n", snd_strerror(err
));
146 else if ( state
== SND_PCM_STATE_SUSPENDED
)
148 int err
= snd_pcm_resume(This
->pcm
);
149 TRACE("recovery from suspension occurred\n");
150 if (err
< 0 && err
!= -EAGAIN
){
151 err
= snd_pcm_prepare(This
->pcm
);
153 ERR("recovery from suspend failed, prepare failed: %s\n", snd_strerror(err
));
155 } else if ( state
!= SND_PCM_STATE_RUNNING
) {
156 WARN("Unhandled state: %d\n", state
);
161 * Allocate the memory-mapped buffer for direct sound, and set up the
164 static int DSDB_CreateMMAP(IDsDriverBufferImpl
* pdbi
)
166 snd_pcm_t
*pcm
= pdbi
->pcm
;
167 snd_pcm_format_t format
;
168 snd_pcm_uframes_t frames
, ofs
, avail
, psize
, boundary
;
169 unsigned int channels
, bits_per_sample
, bits_per_frame
;
171 const snd_pcm_channel_area_t
*areas
;
172 snd_pcm_hw_params_t
*hw_params
= pdbi
->hw_params
;
173 snd_pcm_sw_params_t
*sw_params
= pdbi
->sw_params
;
175 mmap_mode
= snd_pcm_type(pcm
);
177 if (mmap_mode
== SND_PCM_TYPE_HW
)
178 TRACE("mmap'd buffer is a hardware buffer.\n");
180 TRACE("mmap'd buffer is an ALSA emulation of hardware buffer.\n");
182 err
= snd_pcm_hw_params_get_period_size(hw_params
, &psize
, NULL
);
184 err
= snd_pcm_hw_params_get_format(hw_params
, &format
);
185 err
= snd_pcm_hw_params_get_buffer_size(hw_params
, &frames
);
186 err
= snd_pcm_hw_params_get_channels(hw_params
, &channels
);
187 bits_per_sample
= snd_pcm_format_physical_width(format
);
188 bits_per_frame
= bits_per_sample
* channels
;
190 if (TRACE_ON(dsalsa
))
191 ALSA_TraceParameters(hw_params
, NULL
, FALSE
);
193 TRACE("format=%s frames=%ld channels=%d bits_per_sample=%d bits_per_frame=%d\n",
194 snd_pcm_format_name(format
), frames
, channels
, bits_per_sample
, bits_per_frame
);
196 pdbi
->mmap_buflen_frames
= frames
;
197 pdbi
->mmap_buflen_bytes
= snd_pcm_frames_to_bytes( pcm
, frames
);
199 snd_pcm_sw_params_current(pcm
, sw_params
);
200 snd_pcm_sw_params_set_start_threshold(pcm
, sw_params
, 0);
201 snd_pcm_sw_params_get_boundary(sw_params
, &boundary
);
202 snd_pcm_sw_params_set_stop_threshold(pcm
, sw_params
, boundary
);
203 snd_pcm_sw_params_set_silence_threshold(pcm
, sw_params
, INT_MAX
);
204 snd_pcm_sw_params_set_silence_size(pcm
, sw_params
, 0);
205 snd_pcm_sw_params_set_avail_min(pcm
, sw_params
, 0);
206 snd_pcm_sw_params_set_xrun_mode(pcm
, sw_params
, SND_PCM_XRUN_NONE
);
207 err
= snd_pcm_sw_params(pcm
, sw_params
);
209 avail
= snd_pcm_avail_update(pcm
);
212 ERR("No buffer is available: %s.\n", snd_strerror(avail
));
213 return DSERR_GENERIC
;
215 err
= snd_pcm_mmap_begin(pcm
, &areas
, &ofs
, &avail
);
218 ERR("Can't map sound device for direct access: %s\n", snd_strerror(err
));
219 return DSERR_GENERIC
;
221 err
= snd_pcm_mmap_commit(pcm
, ofs
, avail
);
222 pdbi
->mmap_buffer
= areas
->addr
;
224 TRACE("created mmap buffer of %ld frames (%d bytes) at %p\n",
225 frames
, pdbi
->mmap_buflen_bytes
, pdbi
->mmap_buffer
);
230 static HRESULT WINAPI
IDsDriverBufferImpl_QueryInterface(PIDSDRIVERBUFFER iface
, REFIID riid
, LPVOID
*ppobj
)
232 /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
233 FIXME("(): stub!\n");
234 return DSERR_UNSUPPORTED
;
237 static ULONG WINAPI
IDsDriverBufferImpl_AddRef(PIDSDRIVERBUFFER iface
)
239 IDsDriverBufferImpl
*This
= (IDsDriverBufferImpl
*)iface
;
240 ULONG refCount
= InterlockedIncrement(&This
->ref
);
242 TRACE("(%p)->(ref before=%u)\n",This
, refCount
- 1);
247 static ULONG WINAPI
IDsDriverBufferImpl_Release(PIDSDRIVERBUFFER iface
)
249 IDsDriverBufferImpl
*This
= (IDsDriverBufferImpl
*)iface
;
250 ULONG refCount
= InterlockedDecrement(&This
->ref
);
252 TRACE("(%p)->(ref before=%u)\n",This
, refCount
+ 1);
257 TRACE("mmap buffer %p destroyed\n", This
->mmap_buffer
);
259 if (This
== This
->drv
->primary
)
260 This
->drv
->primary
= NULL
;
262 This
->pcm_crst
.DebugInfo
->Spare
[0] = 0;
263 DeleteCriticalSection(&This
->pcm_crst
);
265 snd_pcm_drop(This
->pcm
);
266 snd_pcm_close(This
->pcm
);
268 HeapFree(GetProcessHeap(), 0, This
->sw_params
);
269 HeapFree(GetProcessHeap(), 0, This
->hw_params
);
270 HeapFree(GetProcessHeap(), 0, This
);
274 static HRESULT WINAPI
IDsDriverBufferImpl_Lock(PIDSDRIVERBUFFER iface
,
275 LPVOID
*ppvAudio1
,LPDWORD pdwLen1
,
276 LPVOID
*ppvAudio2
,LPDWORD pdwLen2
,
277 DWORD dwWritePosition
,DWORD dwWriteLen
,
280 FIXME("(%p) stub\n", iface
);
281 return DSERR_UNSUPPORTED
;
284 static HRESULT WINAPI
IDsDriverBufferImpl_Unlock(PIDSDRIVERBUFFER iface
,
285 LPVOID pvAudio1
,DWORD dwLen1
,
286 LPVOID pvAudio2
,DWORD dwLen2
)
288 FIXME("(%p) stub\n", iface
);
289 return DSERR_UNSUPPORTED
;
294 static HRESULT
SetFormat(IDsDriverBufferImpl
*This
, LPWAVEFORMATEX pwfx
, BOOL forced
)
296 snd_pcm_t
*pcm
= NULL
;
297 snd_pcm_hw_params_t
*hw_params
= This
->hw_params
;
298 unsigned int buffer_time
= 500000;
299 snd_pcm_format_t format
= -1;
300 snd_pcm_uframes_t psize
;
301 DWORD rate
= pwfx
->nSamplesPerSec
;
304 switch (pwfx
->wBitsPerSample
)
306 case 8: format
= SND_PCM_FORMAT_U8
; break;
307 case 16: format
= SND_PCM_FORMAT_S16_LE
; break;
308 case 24: format
= SND_PCM_FORMAT_S24_LE
; break;
309 case 32: format
= SND_PCM_FORMAT_S32_LE
; break;
310 default: FIXME("Unsupported bpp: %d\n", pwfx
->wBitsPerSample
); return DSERR_GENERIC
;
314 EnterCriticalSection(&This
->pcm_crst
);
316 err
= snd_pcm_open(&pcm
, WOutDev
[This
->drv
->wDevID
].pcmname
, SND_PCM_STREAM_PLAYBACK
, SND_PCM_NONBLOCK
);
320 if (errno
!= EBUSY
|| !This
->pcm
)
323 LeaveCriticalSection(&This
->pcm_crst
);
324 WARN("Can not open sound device: %s\n", snd_strerror(err
));
325 return DSERR_GENERIC
;
327 snd_pcm_drop(This
->pcm
);
328 snd_pcm_close(This
->pcm
);
330 err
= snd_pcm_open(&pcm
, WOutDev
[This
->drv
->wDevID
].pcmname
, SND_PCM_STREAM_PLAYBACK
, SND_PCM_NONBLOCK
);
334 LeaveCriticalSection(&This
->pcm_crst
);
335 WARN("Can not open sound device: %s\n", snd_strerror(err
));
336 return DSERR_BUFFERLOST
;
340 /* Set some defaults */
341 snd_pcm_hw_params_any(pcm
, hw_params
);
342 err
= snd_pcm_hw_params_set_channels(pcm
, hw_params
, pwfx
->nChannels
);
343 if (err
< 0) { WARN("Could not set channels to %d\n", pwfx
->nChannels
); goto err
; }
345 err
= snd_pcm_hw_params_set_format(pcm
, hw_params
, format
);
346 if (err
< 0) { WARN("Could not set format to %d bpp\n", pwfx
->wBitsPerSample
); goto err
; }
348 /* Alsa's rate resampling is only used if the application specifically requests
349 * a buffer at a certain frequency, else it is better to disable due to unwanted
350 * side effects, which may include: Less granular pointer, changing buffer sizes, etc
352 #if SND_LIB_VERSION >= 0x010009
353 snd_pcm_hw_params_set_rate_resample(pcm
, hw_params
, 0 && forced
);
356 err
= snd_pcm_hw_params_set_rate_near(pcm
, hw_params
, &rate
, NULL
);
357 if (err
< 0) { rate
= pwfx
->nSamplesPerSec
; WARN("Could not set rate\n"); goto err
; }
359 if (!ALSA_NearMatch(rate
, pwfx
->nSamplesPerSec
))
361 WARN("Could not set sound rate to %d, but instead to %d\n", pwfx
->nSamplesPerSec
, rate
);
362 pwfx
->nSamplesPerSec
= rate
;
363 pwfx
->nAvgBytesPerSec
= rate
* pwfx
->nBlockAlign
;
364 /* Let DirectSound detect this */
367 snd_pcm_hw_params_set_periods_integer(pcm
, hw_params
);
368 snd_pcm_hw_params_set_buffer_time_near(pcm
, hw_params
, &buffer_time
, NULL
);
370 snd_pcm_hw_params_set_period_time_near(pcm
, hw_params
, &buffer_time
, NULL
);
371 err
= snd_pcm_hw_params(pcm
, hw_params
);
372 err
= snd_pcm_sw_params(pcm
, This
->sw_params
);
373 snd_pcm_prepare(pcm
);
375 err
= snd_pcm_hw_params_get_period_size(hw_params
, &psize
, NULL
);
376 TRACE("Period size is: %lu\n", psize
);
381 FIXME("Your alsa dmix period size is excessively high, unfortunately this is alsa default, try decreasing it to 512 or 256 (but double the amount of periods) if possible\n");
382 This
->mmap_writeahead
= 3 * psize
;
386 This
->mmap_writeahead
= 3 * psize
;
387 while (This
->mmap_writeahead
<= 512) This
->mmap_writeahead
+= psize
;
392 snd_pcm_drop(This
->pcm
);
393 snd_pcm_close(This
->pcm
);
397 snd_pcm_prepare(This
->pcm
);
398 DSDB_CreateMMAP(This
);
401 LeaveCriticalSection(&This
->pcm_crst
);
406 WARN("Failed to apply changes: %s\n", snd_strerror(err
));
409 snd_pcm_hw_params_current(This
->pcm
, This
->hw_params
);
412 LeaveCriticalSection(&This
->pcm_crst
);
413 return DSERR_BADFORMAT
;
416 static HRESULT WINAPI
IDsDriverBufferImpl_SetFormat(PIDSDRIVERBUFFER iface
, LPWAVEFORMATEX pwfx
)
418 IDsDriverBufferImpl
*This
= (IDsDriverBufferImpl
*)iface
;
421 TRACE("(%p, %p)\n", iface
, pwfx
);
423 hr
= SetFormat(This
, pwfx
, TRUE
);
426 /* Buffer size / Location changed, so tell dsound to recreate */
427 return DSERR_BUFFERLOST
;
431 static HRESULT WINAPI
IDsDriverBufferImpl_SetFrequency(PIDSDRIVERBUFFER iface
, DWORD dwFreq
)
433 /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
434 FIXME("(%p,%d): stub\n",iface
,dwFreq
);
438 static HRESULT WINAPI
IDsDriverBufferImpl_SetVolumePan(PIDSDRIVERBUFFER iface
, PDSVOLUMEPAN pVolPan
)
440 IDsDriverBufferImpl
*This
= (IDsDriverBufferImpl
*)iface
;
441 FIXME("(%p,%p): stub\n",This
,pVolPan
);
442 /* TODO: Bring volume control back */
446 static HRESULT WINAPI
IDsDriverBufferImpl_SetPosition(PIDSDRIVERBUFFER iface
, DWORD dwNewPos
)
448 /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
449 /* I don't even think alsa allows this */
450 FIXME("(%p,%d): stub\n",iface
,dwNewPos
);
451 return DSERR_UNSUPPORTED
;
454 static HRESULT WINAPI
IDsDriverBufferImpl_GetPosition(PIDSDRIVERBUFFER iface
,
455 LPDWORD lpdwPlay
, LPDWORD lpdwWrite
)
457 IDsDriverBufferImpl
*This
= (IDsDriverBufferImpl
*)iface
;
458 snd_pcm_uframes_t hw_pptr
=0, hw_wptr
=0;
459 snd_pcm_state_t state
;
461 EnterCriticalSection(&This
->pcm_crst
);
465 FIXME("Bad pointer for pcm: %p\n", This
->pcm
);
466 LeaveCriticalSection(&This
->pcm_crst
);
467 return DSERR_GENERIC
;
470 state
= snd_pcm_state(This
->pcm
);
471 if (state
== SND_PCM_STATE_RUNNING
)
473 snd_pcm_uframes_t avail
;
474 avail
= snd_pcm_avail_update(This
->pcm
);
475 hw_pptr
= This
->mmap_pos
+ avail
;
476 hw_pptr
%= This
->mmap_buflen_frames
;
477 hw_wptr
= hw_pptr
+ This
->mmap_writeahead
- 512;
478 hw_wptr
%= This
->mmap_buflen_frames
;
479 WARN("At position: %ld (%ld) - Avail %ld\n", hw_pptr
, This
->mmap_pos
, avail
);
480 assert((snd_pcm_sframes_t
)hw_pptr
>= 0);
482 else WARN("%p: State = %d\n", lpdwPlay
, state
);
487 LeaveCriticalSection(&This
->pcm_crst
);
490 *lpdwPlay
= snd_pcm_frames_to_bytes(This
->pcm
, hw_pptr
);
492 *lpdwWrite
= snd_pcm_frames_to_bytes(This
->pcm
, hw_wptr
);
494 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);
498 static HRESULT WINAPI
IDsDriverBufferImpl_Play(PIDSDRIVERBUFFER iface
, DWORD dwRes1
, DWORD dwRes2
, DWORD dwFlags
)
500 IDsDriverBufferImpl
*This
= (IDsDriverBufferImpl
*)iface
;
501 TRACE("(%p,%x,%x,%x)\n",iface
,dwRes1
,dwRes2
,dwFlags
);
504 EnterCriticalSection(&This
->pcm_crst
);
505 snd_pcm_start(This
->pcm
);
508 LeaveCriticalSection(&This
->pcm_crst
);
512 static HRESULT WINAPI
IDsDriverBufferImpl_Stop(PIDSDRIVERBUFFER iface
)
514 IDsDriverBufferImpl
*This
= (IDsDriverBufferImpl
*)iface
;
515 TRACE("(%p)\n",iface
);
518 EnterCriticalSection(&This
->pcm_crst
);
519 snd_pcm_drop(This
->pcm
);
520 snd_pcm_prepare(This
->pcm
);
522 LeaveCriticalSection(&This
->pcm_crst
);
526 static const IDsDriverBufferVtbl dsdbvt
=
528 IDsDriverBufferImpl_QueryInterface
,
529 IDsDriverBufferImpl_AddRef
,
530 IDsDriverBufferImpl_Release
,
531 IDsDriverBufferImpl_Lock
,
532 IDsDriverBufferImpl_Unlock
,
533 IDsDriverBufferImpl_SetFormat
,
534 IDsDriverBufferImpl_SetFrequency
,
535 IDsDriverBufferImpl_SetVolumePan
,
536 IDsDriverBufferImpl_SetPosition
,
537 IDsDriverBufferImpl_GetPosition
,
538 IDsDriverBufferImpl_Play
,
539 IDsDriverBufferImpl_Stop
542 static HRESULT WINAPI
IDsDriverImpl_QueryInterface(PIDSDRIVER iface
, REFIID riid
, LPVOID
*ppobj
)
544 /* IDsDriverImpl *This = (IDsDriverImpl *)iface; */
545 FIXME("(%p): stub!\n",iface
);
546 return DSERR_UNSUPPORTED
;
549 static ULONG WINAPI
IDsDriverImpl_AddRef(PIDSDRIVER iface
)
551 IDsDriverImpl
*This
= (IDsDriverImpl
*)iface
;
552 ULONG refCount
= InterlockedIncrement(&This
->ref
);
554 TRACE("(%p)->(ref before=%u)\n",This
, refCount
- 1);
559 static ULONG WINAPI
IDsDriverImpl_Release(PIDSDRIVER iface
)
561 IDsDriverImpl
*This
= (IDsDriverImpl
*)iface
;
562 ULONG refCount
= InterlockedDecrement(&This
->ref
);
564 TRACE("(%p)->(ref before=%u)\n",This
, refCount
+ 1);
569 HeapFree(GetProcessHeap(), 0, This
);
573 static HRESULT WINAPI
IDsDriverImpl_GetDriverDesc(PIDSDRIVER iface
, PDSDRIVERDESC pDesc
)
575 IDsDriverImpl
*This
= (IDsDriverImpl
*)iface
;
576 TRACE("(%p,%p)\n",iface
,pDesc
);
577 memcpy(pDesc
, &(WOutDev
[This
->wDevID
].ds_desc
), sizeof(DSDRIVERDESC
));
578 pDesc
->dwFlags
= DSDDESC_DONTNEEDPRIMARYLOCK
| DSDDESC_DONTNEEDSECONDARYLOCK
;
579 pDesc
->dnDevNode
= WOutDev
[This
->wDevID
].waveDesc
.dnDevNode
;
581 pDesc
->wReserved
= 0;
582 pDesc
->ulDeviceNum
= This
->wDevID
;
583 pDesc
->dwHeapType
= DSDHEAP_NOHEAP
;
584 pDesc
->pvDirectDrawHeap
= NULL
;
585 pDesc
->dwMemStartAddress
= 0xDEAD0000;
586 pDesc
->dwMemEndAddress
= 0xDEAF0000;
587 pDesc
->dwMemAllocExtra
= 0;
588 pDesc
->pvReserved1
= NULL
;
589 pDesc
->pvReserved2
= NULL
;
593 static HRESULT WINAPI
IDsDriverImpl_Open(PIDSDRIVER iface
)
596 IDsDriverImpl
*This
= (IDsDriverImpl
*)iface
;
598 snd_pcm_t
*pcm
= NULL
;
599 snd_pcm_hw_params_t
*hw_params
;
601 /* While this is not really needed, it is a good idea to do this,
602 * to see if sound can be initialized */
604 hw_params
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, snd_pcm_hw_params_sizeof());
608 hr
= DSERR_OUTOFMEMORY
;
612 err
= snd_pcm_open(&pcm
, WOutDev
[This
->wDevID
].pcmname
, SND_PCM_STREAM_PLAYBACK
, SND_PCM_NONBLOCK
);
613 if (err
< 0) goto err
;
614 err
= snd_pcm_hw_params_any(pcm
, hw_params
);
615 if (err
< 0) goto err
;
616 err
= snd_pcm_hw_params_set_access (pcm
, hw_params
, SND_PCM_ACCESS_MMAP_INTERLEAVED
);
617 if (err
< 0) goto err
;
625 FIXME("Failed to open device: %s\n", snd_strerror(err
));
629 HeapFree(GetProcessHeap(), 0, hw_params
);
631 WARN("--> %08x\n", hr
);
635 static HRESULT WINAPI
IDsDriverImpl_Close(PIDSDRIVER iface
)
637 IDsDriverImpl
*This
= (IDsDriverImpl
*)iface
;
638 TRACE("(%p) stub, harmless\n",This
);
642 static HRESULT WINAPI
IDsDriverImpl_GetCaps(PIDSDRIVER iface
, PDSDRIVERCAPS pCaps
)
644 IDsDriverImpl
*This
= (IDsDriverImpl
*)iface
;
645 TRACE("(%p,%p)\n",iface
,pCaps
);
646 memcpy(pCaps
, &(WOutDev
[This
->wDevID
].ds_caps
), sizeof(DSDRIVERCAPS
));
650 static HRESULT WINAPI
IDsDriverImpl_CreateSoundBuffer(PIDSDRIVER iface
,
652 DWORD dwFlags
, DWORD dwCardAddress
,
653 LPDWORD pdwcbBufferSize
,
657 IDsDriverImpl
*This
= (IDsDriverImpl
*)iface
;
658 IDsDriverBufferImpl
** ippdsdb
= (IDsDriverBufferImpl
**)ppvObj
;
661 TRACE("(%p,%p,%x,%x)\n",iface
,pwfx
,dwFlags
,dwCardAddress
);
662 /* we only support primary buffers... for now */
663 if (!(dwFlags
& DSBCAPS_PRIMARYBUFFER
))
664 return DSERR_UNSUPPORTED
;
666 return DSERR_ALLOCATED
;
668 *ippdsdb
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(IDsDriverBufferImpl
));
669 if (*ippdsdb
== NULL
)
670 return DSERR_OUTOFMEMORY
;
672 (*ippdsdb
)->hw_params
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, snd_pcm_hw_params_sizeof());
673 (*ippdsdb
)->sw_params
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, snd_pcm_sw_params_sizeof());
674 if (!(*ippdsdb
)->hw_params
|| !(*ippdsdb
)->sw_params
)
676 HeapFree(GetProcessHeap(), 0, (*ippdsdb
)->sw_params
);
677 HeapFree(GetProcessHeap(), 0, (*ippdsdb
)->hw_params
);
678 return DSERR_OUTOFMEMORY
;
680 (*ippdsdb
)->lpVtbl
= &dsdbvt
;
682 (*ippdsdb
)->drv
= This
;
683 InitializeCriticalSection(&(*ippdsdb
)->pcm_crst
);
684 (*ippdsdb
)->pcm_crst
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": ALSA_DSOUTPUT.pcm_crst");
686 /* SetFormat has to re-initialize pcm here anyway */
687 err
= SetFormat(*ippdsdb
, pwfx
, FALSE
);
690 WARN("Error occured: %08x\n", err
);
694 if (dwFlags
& DSBCAPS_PRIMARYBUFFER
)
695 This
->primary
= *ippdsdb
;
697 *pdwcbBufferSize
= (*ippdsdb
)->mmap_buflen_bytes
;
698 *ppbBuffer
= (*ippdsdb
)->mmap_buffer
;
700 /* buffer is ready to go */
701 TRACE("buffer created at %p\n", *ippdsdb
);
705 HeapFree(GetProcessHeap(), 0, (*ippdsdb
)->sw_params
);
706 HeapFree(GetProcessHeap(), 0, (*ippdsdb
)->hw_params
);
707 HeapFree(GetProcessHeap(), 0, *ippdsdb
);
712 static HRESULT WINAPI
IDsDriverImpl_DuplicateSoundBuffer(PIDSDRIVER iface
,
713 PIDSDRIVERBUFFER pBuffer
,
716 IDsDriverImpl
*This
= (IDsDriverImpl
*)iface
;
717 FIXME("(%p,%p): stub\n",This
,pBuffer
);
718 return DSERR_INVALIDCALL
;
721 static const IDsDriverVtbl dsdvt
=
723 IDsDriverImpl_QueryInterface
,
724 IDsDriverImpl_AddRef
,
725 IDsDriverImpl_Release
,
726 IDsDriverImpl_GetDriverDesc
,
729 IDsDriverImpl_GetCaps
,
730 IDsDriverImpl_CreateSoundBuffer
,
731 IDsDriverImpl_DuplicateSoundBuffer
734 DWORD
wodDsCreate(UINT wDevID
, PIDSDRIVER
* drv
)
736 IDsDriverImpl
** idrv
= (IDsDriverImpl
**)drv
;
738 TRACE("driver created\n");
740 /* the HAL isn't much better than the HEL if we can't do mmap() */
741 if (!(WOutDev
[wDevID
].outcaps
.dwSupport
& WAVECAPS_DIRECTSOUND
)) {
742 ERR("DirectSound flag not set\n");
743 MESSAGE("This sound card's driver does not support direct access\n");
744 MESSAGE("The (slower) DirectSound HEL mode will be used instead.\n");
745 return MMSYSERR_NOTSUPPORTED
;
748 *idrv
= HeapAlloc(GetProcessHeap(),0,sizeof(IDsDriverImpl
));
750 return MMSYSERR_NOMEM
;
751 (*idrv
)->lpVtbl
= &dsdvt
;
754 (*idrv
)->wDevID
= wDevID
;
755 (*idrv
)->primary
= NULL
;
756 return MMSYSERR_NOERROR
;
759 DWORD
wodDsDesc(UINT wDevID
, PDSDRIVERDESC desc
)
761 memcpy(desc
, &(WOutDev
[wDevID
].ds_desc
), sizeof(DSDRIVERDESC
));
762 return MMSYSERR_NOERROR
;
765 #endif /* HAVE_ALSA */