winealsa: Reduce writeahead to next multiple of psize greater than 512.
[wine/wine64.git] / dlls / winealsa.drv / dsoutput.c
blobe4c7d3fd1e65ce1a049f13a7b2f966e9955c8e05
1 /*
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 *======================================================================*/
29 #include "config.h"
30 #include "wine/port.h"
32 #include <stdlib.h>
33 #include <stdarg.h>
34 #include <stdio.h>
35 #include <string.h>
36 #ifdef HAVE_UNISTD_H
37 # include <unistd.h>
38 #endif
39 #include <errno.h>
40 #include <limits.h>
41 #include <fcntl.h>
42 #ifdef HAVE_SYS_IOCTL_H
43 # include <sys/ioctl.h>
44 #endif
45 #ifdef HAVE_SYS_MMAN_H
46 # include <sys/mman.h>
47 #endif
48 #include "windef.h"
49 #include "winbase.h"
50 #include "wingdi.h"
51 #include "winerror.h"
52 #include "winuser.h"
53 #include "mmddk.h"
55 #include "alsa.h"
56 #include "wine/library.h"
57 #include "wine/unicode.h"
58 #include "wine/debug.h"
60 #ifdef HAVE_ALSA
62 WINE_DEFAULT_DEBUG_CHANNEL(dsalsa);
64 typedef struct IDsDriverImpl IDsDriverImpl;
65 typedef struct IDsDriverBufferImpl IDsDriverBufferImpl;
67 struct IDsDriverImpl
69 /* IUnknown fields */
70 const IDsDriverVtbl *lpVtbl;
71 LONG ref;
73 /* IDsDriverImpl fields */
74 IDsDriverBufferImpl* primary;
75 UINT wDevID;
78 struct IDsDriverBufferImpl
80 const IDsDriverBufferVtbl *lpVtbl;
81 LONG ref;
82 IDsDriverImpl* drv;
84 CRITICAL_SECTION pcm_crst;
85 LPVOID mmap_buffer;
86 DWORD mmap_buflen_bytes;
88 snd_pcm_t *pcm;
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)
115 putin = writeahead;
116 used = This->mmap_buflen_frames - snd_pcm_avail_update(This->pcm);
117 putin -= used;
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;
133 int err;
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);
140 CommitAll(This);
141 snd_pcm_start(This->pcm);
142 WARN("xrun occurred\n");
143 if ( err < 0 )
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);
152 if (err < 0)
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
162 * callback.
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;
170 int err, mmap_mode;
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");
179 else
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);
210 if (avail < 0)
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);
216 if ( err < 0 )
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);
227 return DS_OK;
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);
244 return refCount;
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);
254 if (refCount)
255 return refCount;
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);
267 This->pcm = NULL;
268 HeapFree(GetProcessHeap(), 0, This->sw_params);
269 HeapFree(GetProcessHeap(), 0, This->hw_params);
270 HeapFree(GetProcessHeap(), 0, This);
271 return 0;
274 static HRESULT WINAPI IDsDriverBufferImpl_Lock(PIDSDRIVERBUFFER iface,
275 LPVOID*ppvAudio1,LPDWORD pdwLen1,
276 LPVOID*ppvAudio2,LPDWORD pdwLen2,
277 DWORD dwWritePosition,DWORD dwWriteLen,
278 DWORD dwFlags)
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;
292 static int warnonce;
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;
302 int err=0;
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;
313 /* **** */
314 EnterCriticalSection(&This->pcm_crst);
316 err = snd_pcm_open(&pcm, WOutDev[This->drv->wDevID].pcmname, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
318 if (err < 0)
320 if (errno != EBUSY || !This->pcm)
322 /* **** */
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);
329 This->pcm = NULL;
330 err = snd_pcm_open(&pcm, WOutDev[This->drv->wDevID].pcmname, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
331 if (err < 0)
333 /* **** */
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);
354 #endif
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);
369 buffer_time = 10000;
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);
378 if (psize > 512)
380 if (++warnonce == 1)
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;
384 else
386 This->mmap_writeahead = 3 * psize;
387 while (This->mmap_writeahead <= 512) This->mmap_writeahead += psize;
390 if (This->pcm)
392 snd_pcm_drop(This->pcm);
393 snd_pcm_close(This->pcm);
395 This->pcm = pcm;
397 snd_pcm_prepare(This->pcm);
398 DSDB_CreateMMAP(This);
400 /* **** */
401 LeaveCriticalSection(&This->pcm_crst);
402 return S_OK;
404 err:
405 if (err < 0)
406 WARN("Failed to apply changes: %s\n", snd_strerror(err));
408 if (This->pcm)
409 snd_pcm_hw_params_current(This->pcm, This->hw_params);
411 /* **** */
412 LeaveCriticalSection(&This->pcm_crst);
413 return DSERR_BADFORMAT;
416 static HRESULT WINAPI IDsDriverBufferImpl_SetFormat(PIDSDRIVERBUFFER iface, LPWAVEFORMATEX pwfx)
418 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
419 HRESULT hr = S_OK;
421 TRACE("(%p, %p)\n", iface, pwfx);
423 hr = SetFormat(This, pwfx, TRUE);
425 if (hr == S_OK)
426 /* Buffer size / Location changed, so tell dsound to recreate */
427 return DSERR_BUFFERLOST;
428 return hr;
431 static HRESULT WINAPI IDsDriverBufferImpl_SetFrequency(PIDSDRIVERBUFFER iface, DWORD dwFreq)
433 /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
434 FIXME("(%p,%d): stub\n",iface,dwFreq);
435 return S_OK;
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 */
443 return DS_OK;
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);
463 if (!This->pcm)
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);
484 CheckXRUN(This);
485 CommitAll(This);
487 LeaveCriticalSection(&This->pcm_crst);
489 if (lpdwPlay)
490 *lpdwPlay = snd_pcm_frames_to_bytes(This->pcm, hw_pptr);
491 if (lpdwWrite)
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);
495 return DS_OK;
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);
503 /* **** */
504 EnterCriticalSection(&This->pcm_crst);
505 snd_pcm_start(This->pcm);
506 CommitAll(This);
507 /* **** */
508 LeaveCriticalSection(&This->pcm_crst);
509 return DS_OK;
512 static HRESULT WINAPI IDsDriverBufferImpl_Stop(PIDSDRIVERBUFFER iface)
514 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
515 TRACE("(%p)\n",iface);
517 /* **** */
518 EnterCriticalSection(&This->pcm_crst);
519 snd_pcm_drop(This->pcm);
520 snd_pcm_prepare(This->pcm);
521 /* **** */
522 LeaveCriticalSection(&This->pcm_crst);
523 return DS_OK;
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);
556 return refCount;
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);
566 if (refCount)
567 return refCount;
569 HeapFree(GetProcessHeap(), 0, This);
570 return 0;
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;
580 pDesc->wVxdId = 0;
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;
590 return DS_OK;
593 static HRESULT WINAPI IDsDriverImpl_Open(PIDSDRIVER iface)
595 HRESULT hr = S_OK;
596 IDsDriverImpl *This = (IDsDriverImpl *)iface;
597 int err=0;
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());
606 if (!hw_params)
608 hr = DSERR_OUTOFMEMORY;
609 goto unalloc;
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;
619 TRACE("Success\n");
620 snd_pcm_close(pcm);
621 goto unalloc;
623 err:
624 hr = DSERR_GENERIC;
625 FIXME("Failed to open device: %s\n", snd_strerror(err));
626 if (pcm)
627 snd_pcm_close(pcm);
628 unalloc:
629 HeapFree(GetProcessHeap(), 0, hw_params);
630 if (hr != S_OK)
631 WARN("--> %08x\n", hr);
632 return hr;
635 static HRESULT WINAPI IDsDriverImpl_Close(PIDSDRIVER iface)
637 IDsDriverImpl *This = (IDsDriverImpl *)iface;
638 TRACE("(%p) stub, harmless\n",This);
639 return DS_OK;
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));
647 return DS_OK;
650 static HRESULT WINAPI IDsDriverImpl_CreateSoundBuffer(PIDSDRIVER iface,
651 LPWAVEFORMATEX pwfx,
652 DWORD dwFlags, DWORD dwCardAddress,
653 LPDWORD pdwcbBufferSize,
654 LPBYTE *ppbBuffer,
655 LPVOID *ppvObj)
657 IDsDriverImpl *This = (IDsDriverImpl *)iface;
658 IDsDriverBufferImpl** ippdsdb = (IDsDriverBufferImpl**)ppvObj;
659 HRESULT err;
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;
665 if (This->primary)
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;
681 (*ippdsdb)->ref = 1;
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);
688 if (FAILED(err))
690 WARN("Error occured: %08x\n", err);
691 goto 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);
702 return err;
704 err:
705 HeapFree(GetProcessHeap(), 0, (*ippdsdb)->sw_params);
706 HeapFree(GetProcessHeap(), 0, (*ippdsdb)->hw_params);
707 HeapFree(GetProcessHeap(), 0, *ippdsdb);
708 *ippdsdb = NULL;
709 return err;
712 static HRESULT WINAPI IDsDriverImpl_DuplicateSoundBuffer(PIDSDRIVER iface,
713 PIDSDRIVERBUFFER pBuffer,
714 LPVOID *ppvObj)
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,
727 IDsDriverImpl_Open,
728 IDsDriverImpl_Close,
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));
749 if (!*idrv)
750 return MMSYSERR_NOMEM;
751 (*idrv)->lpVtbl = &dsdvt;
752 (*idrv)->ref = 1;
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 */