dsound/winealsa: Remove writelead for alsa directsound.
[wine/multimedia.git] / dlls / winealsa.drv / dsoutput.c
blob2879d2e43120e6e5185ea2464baf3aca78b9c847
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_commitahead, 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 * 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;
115 used += 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;
123 used += done;
127 if (This->mmap_pos == This->mmap_buflen_frames)
128 This->mmap_pos = 0;
130 return used;
133 static void CheckXRUN(IDsDriverBufferImpl* This)
135 snd_pcm_state_t state = snd_pcm_state(This->pcm);
136 snd_pcm_sframes_t delay;
137 int err;
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);
144 CommitAll(This);
145 snd_pcm_start(This->pcm);
146 WARN("xrun occurred\n");
147 if ( err < 0 )
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);
156 if (err < 0)
157 ERR("recovery from suspend failed, prepare failed: %s\n", snd_strerror(err));
159 } else if ( state != SND_PCM_STATE_RUNNING ) {
160 WARN("Unhandled state: %d\n", state);
165 * Allocate the memory-mapped buffer for direct sound, and set up the
166 * callback.
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;
174 int err, mmap_mode;
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");
185 else
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);
216 if (avail < 0)
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);
222 if ( err < 0 )
224 ERR("Can't map sound device for direct access: %s\n", snd_strerror(err));
225 return DSERR_GENERIC;
227 err = snd_pcm_mmap_commit(pcm, ofs, avail);
228 pdbi->mmap_buffer = areas->addr;
230 TRACE("created mmap buffer of %ld frames (%d bytes) at %p\n",
231 frames, pdbi->mmap_buflen_bytes, pdbi->mmap_buffer);
233 return DS_OK;
236 static HRESULT WINAPI IDsDriverBufferImpl_QueryInterface(PIDSDRIVERBUFFER iface, REFIID riid, LPVOID *ppobj)
238 /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
239 FIXME("(): stub!\n");
240 return DSERR_UNSUPPORTED;
243 static ULONG WINAPI IDsDriverBufferImpl_AddRef(PIDSDRIVERBUFFER iface)
245 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
246 ULONG refCount = InterlockedIncrement(&This->ref);
248 TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
250 return refCount;
253 static ULONG WINAPI IDsDriverBufferImpl_Release(PIDSDRIVERBUFFER iface)
255 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
256 ULONG refCount = InterlockedDecrement(&This->ref);
258 TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
260 if (refCount)
261 return refCount;
263 TRACE("mmap buffer %p destroyed\n", This->mmap_buffer);
265 if (This == This->drv->primary)
266 This->drv->primary = NULL;
268 This->pcm_crst.DebugInfo->Spare[0] = 0;
269 DeleteCriticalSection(&This->pcm_crst);
271 snd_pcm_drop(This->pcm);
272 snd_pcm_close(This->pcm);
273 This->pcm = NULL;
274 HeapFree(GetProcessHeap(), 0, This->sw_params);
275 HeapFree(GetProcessHeap(), 0, This->hw_params);
276 HeapFree(GetProcessHeap(), 0, This);
277 return 0;
280 static HRESULT WINAPI IDsDriverBufferImpl_Lock(PIDSDRIVERBUFFER iface,
281 LPVOID*ppvAudio1,LPDWORD pdwLen1,
282 LPVOID*ppvAudio2,LPDWORD pdwLen2,
283 DWORD dwWritePosition,DWORD dwWriteLen,
284 DWORD dwFlags)
286 FIXME("(%p) stub\n", iface);
287 return DSERR_UNSUPPORTED;
290 static HRESULT WINAPI IDsDriverBufferImpl_Unlock(PIDSDRIVERBUFFER iface,
291 LPVOID pvAudio1,DWORD dwLen1,
292 LPVOID pvAudio2,DWORD dwLen2)
294 FIXME("(%p) stub\n", iface);
295 return DSERR_UNSUPPORTED;
298 static int warnonce;
300 static HRESULT SetFormat(IDsDriverBufferImpl *This, LPWAVEFORMATEX pwfx, BOOL forced)
302 snd_pcm_t *pcm = NULL;
303 snd_pcm_hw_params_t *hw_params = This->hw_params;
304 unsigned int buffer_time = 500000;
305 snd_pcm_format_t format = -1;
306 snd_pcm_uframes_t psize;
307 DWORD rate = pwfx->nSamplesPerSec;
308 int err=0;
310 switch (pwfx->wBitsPerSample)
312 case 8: format = SND_PCM_FORMAT_U8; break;
313 case 16: format = SND_PCM_FORMAT_S16_LE; break;
314 case 24: format = SND_PCM_FORMAT_S24_LE; break;
315 case 32: format = SND_PCM_FORMAT_S32_LE; break;
316 default: FIXME("Unsupported bpp: %d\n", pwfx->wBitsPerSample); return DSERR_GENERIC;
319 /* **** */
320 EnterCriticalSection(&This->pcm_crst);
322 err = snd_pcm_open(&pcm, WOutDev[This->drv->wDevID].pcmname, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
324 if (err < 0)
326 if (errno != EBUSY || !This->pcm)
328 /* **** */
329 LeaveCriticalSection(&This->pcm_crst);
330 WARN("Can not open sound device: %s\n", snd_strerror(err));
331 return DSERR_GENERIC;
333 snd_pcm_drop(This->pcm);
334 snd_pcm_close(This->pcm);
335 This->pcm = NULL;
336 err = snd_pcm_open(&pcm, WOutDev[This->drv->wDevID].pcmname, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
337 if (err < 0)
339 /* **** */
340 LeaveCriticalSection(&This->pcm_crst);
341 WARN("Can not open sound device: %s\n", snd_strerror(err));
342 return DSERR_BUFFERLOST;
346 /* Set some defaults */
347 snd_pcm_hw_params_any(pcm, hw_params);
348 err = snd_pcm_hw_params_set_channels(pcm, hw_params, pwfx->nChannels);
349 if (err < 0) { WARN("Could not set channels to %d\n", pwfx->nChannels); goto err; }
351 err = snd_pcm_hw_params_set_format(pcm, hw_params, format);
352 if (err < 0) { WARN("Could not set format to %d bpp\n", pwfx->wBitsPerSample); goto err; }
354 /* Alsa's rate resampling is only used if the application specifically requests
355 * a buffer at a certain frequency, else it is better to disable due to unwanted
356 * side effects, which may include: Less granular pointer, changing buffer sizes, etc
358 #if SND_LIB_VERSION >= 0x010009
359 snd_pcm_hw_params_set_rate_resample(pcm, hw_params, 0 && forced);
360 #endif
362 err = snd_pcm_hw_params_set_rate_near(pcm, hw_params, &rate, NULL);
363 if (err < 0) { rate = pwfx->nSamplesPerSec; WARN("Could not set rate\n"); goto err; }
365 if (!ALSA_NearMatch(rate, pwfx->nSamplesPerSec))
367 WARN("Could not set sound rate to %d, but instead to %d\n", pwfx->nSamplesPerSec, rate);
368 pwfx->nSamplesPerSec = rate;
369 pwfx->nAvgBytesPerSec = rate * pwfx->nBlockAlign;
370 /* Let DirectSound detect this */
373 snd_pcm_hw_params_set_periods_integer(pcm, hw_params);
374 snd_pcm_hw_params_set_buffer_time_near(pcm, hw_params, &buffer_time, NULL);
375 buffer_time = 10000;
376 snd_pcm_hw_params_set_period_time_near(pcm, hw_params, &buffer_time, NULL);
377 err = snd_pcm_hw_params(pcm, hw_params);
378 err = snd_pcm_sw_params(pcm, This->sw_params);
379 snd_pcm_prepare(pcm);
381 err = snd_pcm_hw_params_get_period_size(hw_params, &psize, NULL);
382 TRACE("Period size is: %lu\n", psize);
384 /* If period size is 'high', try to commit less
385 * dmix needs at least 2 buffers to work succesfully but prefers 3
386 * however it seems to work ok if I just commit 2 1/2 buffers
388 if (psize >= 512)
390 if (psize > 512 && ++warnonce == 1)
391 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");
392 This->mmap_commitahead = 2 * psize + psize/2;
393 This->mmap_writeahead = 2 * psize;
395 else
397 This->mmap_commitahead = 3 * psize;
398 while (This->mmap_commitahead <= 512)
399 This->mmap_commitahead += psize;
400 This->mmap_writeahead = This->mmap_commitahead;
403 if (This->pcm)
405 snd_pcm_drop(This->pcm);
406 snd_pcm_close(This->pcm);
408 This->pcm = pcm;
410 snd_pcm_prepare(This->pcm);
411 DSDB_CreateMMAP(This);
413 /* **** */
414 LeaveCriticalSection(&This->pcm_crst);
415 return S_OK;
417 err:
418 if (err < 0)
419 WARN("Failed to apply changes: %s\n", snd_strerror(err));
421 if (!This->pcm)
422 This->pcm = pcm;
423 else
424 snd_pcm_close(pcm);
426 if (This->pcm)
427 snd_pcm_hw_params_current(This->pcm, This->hw_params);
429 /* **** */
430 LeaveCriticalSection(&This->pcm_crst);
431 return DSERR_BADFORMAT;
434 static HRESULT WINAPI IDsDriverBufferImpl_SetFormat(PIDSDRIVERBUFFER iface, LPWAVEFORMATEX pwfx)
436 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
437 HRESULT hr = S_OK;
439 TRACE("(%p, %p)\n", iface, pwfx);
441 hr = SetFormat(This, pwfx, TRUE);
443 if (hr == S_OK)
444 /* Buffer size / Location changed, so tell dsound to recreate */
445 return DSERR_BUFFERLOST;
446 return hr;
449 static HRESULT WINAPI IDsDriverBufferImpl_SetFrequency(PIDSDRIVERBUFFER iface, DWORD dwFreq)
451 /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
452 FIXME("(%p,%d): stub\n",iface,dwFreq);
453 return S_OK;
456 static HRESULT WINAPI IDsDriverBufferImpl_SetVolumePan(PIDSDRIVERBUFFER iface, PDSVOLUMEPAN pVolPan)
458 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
459 FIXME("(%p,%p): stub\n",This,pVolPan);
460 /* TODO: Bring volume control back */
461 return DS_OK;
464 static HRESULT WINAPI IDsDriverBufferImpl_SetPosition(PIDSDRIVERBUFFER iface, DWORD dwNewPos)
466 /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
467 /* I don't even think alsa allows this */
468 FIXME("(%p,%d): stub\n",iface,dwNewPos);
469 return DSERR_UNSUPPORTED;
472 static HRESULT WINAPI IDsDriverBufferImpl_GetPosition(PIDSDRIVERBUFFER iface,
473 LPDWORD lpdwPlay, LPDWORD lpdwWrite)
475 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
476 snd_pcm_uframes_t hw_pptr=0, hw_wptr=0;
477 snd_pcm_state_t state;
479 EnterCriticalSection(&This->pcm_crst);
481 if (!This->pcm)
483 FIXME("Bad pointer for pcm: %p\n", This->pcm);
484 LeaveCriticalSection(&This->pcm_crst);
485 return DSERR_GENERIC;
488 state = snd_pcm_state(This->pcm);
490 if (state != SND_PCM_STATE_RUNNING)
491 CheckXRUN(This);
492 else
494 snd_pcm_uframes_t used = CommitAll(This);
496 if (This->mmap_pos > used)
497 hw_pptr = This->mmap_pos - used;
498 else
499 hw_pptr = This->mmap_buflen_frames - used + This->mmap_pos;
501 hw_wptr = hw_pptr + (This->mmap_writeahead > used ? used : This->mmap_writeahead);
502 hw_wptr %= This->mmap_buflen_frames;
503 hw_pptr %= This->mmap_buflen_frames;
505 TRACE("At position: %ld (%ld) - Used %ld\n", hw_pptr, This->mmap_pos, used);
508 LeaveCriticalSection(&This->pcm_crst);
510 if (lpdwPlay)
511 *lpdwPlay = snd_pcm_frames_to_bytes(This->pcm, hw_pptr);
512 if (lpdwWrite)
513 *lpdwWrite = snd_pcm_frames_to_bytes(This->pcm, hw_wptr);
515 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);
516 return DS_OK;
519 static HRESULT WINAPI IDsDriverBufferImpl_Play(PIDSDRIVERBUFFER iface, DWORD dwRes1, DWORD dwRes2, DWORD dwFlags)
521 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
522 TRACE("(%p,%x,%x,%x)\n",iface,dwRes1,dwRes2,dwFlags);
524 /* **** */
525 EnterCriticalSection(&This->pcm_crst);
526 snd_pcm_start(This->pcm);
527 CommitAll(This);
528 /* **** */
529 LeaveCriticalSection(&This->pcm_crst);
530 return DS_OK;
533 static HRESULT WINAPI IDsDriverBufferImpl_Stop(PIDSDRIVERBUFFER iface)
535 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
536 TRACE("(%p)\n",iface);
538 /* **** */
539 EnterCriticalSection(&This->pcm_crst);
540 snd_pcm_drop(This->pcm);
541 snd_pcm_prepare(This->pcm);
542 /* **** */
543 LeaveCriticalSection(&This->pcm_crst);
544 return DS_OK;
547 static const IDsDriverBufferVtbl dsdbvt =
549 IDsDriverBufferImpl_QueryInterface,
550 IDsDriverBufferImpl_AddRef,
551 IDsDriverBufferImpl_Release,
552 IDsDriverBufferImpl_Lock,
553 IDsDriverBufferImpl_Unlock,
554 IDsDriverBufferImpl_SetFormat,
555 IDsDriverBufferImpl_SetFrequency,
556 IDsDriverBufferImpl_SetVolumePan,
557 IDsDriverBufferImpl_SetPosition,
558 IDsDriverBufferImpl_GetPosition,
559 IDsDriverBufferImpl_Play,
560 IDsDriverBufferImpl_Stop
563 static HRESULT WINAPI IDsDriverImpl_QueryInterface(PIDSDRIVER iface, REFIID riid, LPVOID *ppobj)
565 /* IDsDriverImpl *This = (IDsDriverImpl *)iface; */
566 FIXME("(%p): stub!\n",iface);
567 return DSERR_UNSUPPORTED;
570 static ULONG WINAPI IDsDriverImpl_AddRef(PIDSDRIVER iface)
572 IDsDriverImpl *This = (IDsDriverImpl *)iface;
573 ULONG refCount = InterlockedIncrement(&This->ref);
575 TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
577 return refCount;
580 static ULONG WINAPI IDsDriverImpl_Release(PIDSDRIVER iface)
582 IDsDriverImpl *This = (IDsDriverImpl *)iface;
583 ULONG refCount = InterlockedDecrement(&This->ref);
585 TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
587 if (refCount)
588 return refCount;
590 HeapFree(GetProcessHeap(), 0, This);
591 return 0;
594 static HRESULT WINAPI IDsDriverImpl_GetDriverDesc(PIDSDRIVER iface, PDSDRIVERDESC pDesc)
596 IDsDriverImpl *This = (IDsDriverImpl *)iface;
597 TRACE("(%p,%p)\n",iface,pDesc);
598 memcpy(pDesc, &(WOutDev[This->wDevID].ds_desc), sizeof(DSDRIVERDESC));
599 pDesc->dwFlags = DSDDESC_DONTNEEDPRIMARYLOCK | DSDDESC_DONTNEEDSECONDARYLOCK | DSDDESC_DONTNEEDWRITELEAD;
600 pDesc->dnDevNode = WOutDev[This->wDevID].waveDesc.dnDevNode;
601 pDesc->wVxdId = 0;
602 pDesc->wReserved = 0;
603 pDesc->ulDeviceNum = This->wDevID;
604 pDesc->dwHeapType = DSDHEAP_NOHEAP;
605 pDesc->pvDirectDrawHeap = NULL;
606 pDesc->dwMemStartAddress = 0xDEAD0000;
607 pDesc->dwMemEndAddress = 0xDEAF0000;
608 pDesc->dwMemAllocExtra = 0;
609 pDesc->pvReserved1 = NULL;
610 pDesc->pvReserved2 = NULL;
611 return DS_OK;
614 static HRESULT WINAPI IDsDriverImpl_Open(PIDSDRIVER iface)
616 HRESULT hr = S_OK;
617 IDsDriverImpl *This = (IDsDriverImpl *)iface;
618 int err=0;
619 snd_pcm_t *pcm = NULL;
620 snd_pcm_hw_params_t *hw_params;
622 /* While this is not really needed, it is a good idea to do this,
623 * to see if sound can be initialized */
625 hw_params = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, snd_pcm_hw_params_sizeof());
627 if (!hw_params)
629 hr = DSERR_OUTOFMEMORY;
630 goto unalloc;
633 err = snd_pcm_open(&pcm, WOutDev[This->wDevID].pcmname, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
634 if (err < 0) goto err;
635 err = snd_pcm_hw_params_any(pcm, hw_params);
636 if (err < 0) goto err;
637 err = snd_pcm_hw_params_set_access (pcm, hw_params, SND_PCM_ACCESS_MMAP_INTERLEAVED);
638 if (err < 0) goto err;
640 TRACE("Success\n");
641 snd_pcm_close(pcm);
642 goto unalloc;
644 err:
645 hr = DSERR_GENERIC;
646 FIXME("Failed to open device: %s\n", snd_strerror(err));
647 if (pcm)
648 snd_pcm_close(pcm);
649 unalloc:
650 HeapFree(GetProcessHeap(), 0, hw_params);
651 if (hr != S_OK)
652 WARN("--> %08x\n", hr);
653 return hr;
656 static HRESULT WINAPI IDsDriverImpl_Close(PIDSDRIVER iface)
658 IDsDriverImpl *This = (IDsDriverImpl *)iface;
659 TRACE("(%p) stub, harmless\n",This);
660 return DS_OK;
663 static HRESULT WINAPI IDsDriverImpl_GetCaps(PIDSDRIVER iface, PDSDRIVERCAPS pCaps)
665 IDsDriverImpl *This = (IDsDriverImpl *)iface;
666 TRACE("(%p,%p)\n",iface,pCaps);
667 memcpy(pCaps, &(WOutDev[This->wDevID].ds_caps), sizeof(DSDRIVERCAPS));
668 return DS_OK;
671 static HRESULT WINAPI IDsDriverImpl_CreateSoundBuffer(PIDSDRIVER iface,
672 LPWAVEFORMATEX pwfx,
673 DWORD dwFlags, DWORD dwCardAddress,
674 LPDWORD pdwcbBufferSize,
675 LPBYTE *ppbBuffer,
676 LPVOID *ppvObj)
678 IDsDriverImpl *This = (IDsDriverImpl *)iface;
679 IDsDriverBufferImpl** ippdsdb = (IDsDriverBufferImpl**)ppvObj;
680 HRESULT err;
682 TRACE("(%p,%p,%x,%x)\n",iface,pwfx,dwFlags,dwCardAddress);
683 /* we only support primary buffers... for now */
684 if (!(dwFlags & DSBCAPS_PRIMARYBUFFER))
685 return DSERR_UNSUPPORTED;
686 if (This->primary)
687 return DSERR_ALLOCATED;
689 *ippdsdb = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDsDriverBufferImpl));
690 if (*ippdsdb == NULL)
691 return DSERR_OUTOFMEMORY;
693 (*ippdsdb)->hw_params = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, snd_pcm_hw_params_sizeof());
694 (*ippdsdb)->sw_params = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, snd_pcm_sw_params_sizeof());
695 if (!(*ippdsdb)->hw_params || !(*ippdsdb)->sw_params)
697 HeapFree(GetProcessHeap(), 0, (*ippdsdb)->sw_params);
698 HeapFree(GetProcessHeap(), 0, (*ippdsdb)->hw_params);
699 return DSERR_OUTOFMEMORY;
701 (*ippdsdb)->lpVtbl = &dsdbvt;
702 (*ippdsdb)->ref = 1;
703 (*ippdsdb)->drv = This;
704 InitializeCriticalSection(&(*ippdsdb)->pcm_crst);
705 (*ippdsdb)->pcm_crst.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": ALSA_DSOUTPUT.pcm_crst");
707 /* SetFormat has to re-initialize pcm here anyway */
708 err = SetFormat(*ippdsdb, pwfx, FALSE);
709 if (FAILED(err))
711 WARN("Error occured: %08x\n", err);
712 goto err;
715 if (dwFlags & DSBCAPS_PRIMARYBUFFER)
716 This->primary = *ippdsdb;
718 *pdwcbBufferSize = (*ippdsdb)->mmap_buflen_bytes;
719 *ppbBuffer = (*ippdsdb)->mmap_buffer;
721 /* buffer is ready to go */
722 TRACE("buffer created at %p\n", *ippdsdb);
723 return err;
725 err:
726 HeapFree(GetProcessHeap(), 0, (*ippdsdb)->sw_params);
727 HeapFree(GetProcessHeap(), 0, (*ippdsdb)->hw_params);
728 HeapFree(GetProcessHeap(), 0, *ippdsdb);
729 *ippdsdb = NULL;
730 return err;
733 static HRESULT WINAPI IDsDriverImpl_DuplicateSoundBuffer(PIDSDRIVER iface,
734 PIDSDRIVERBUFFER pBuffer,
735 LPVOID *ppvObj)
737 IDsDriverImpl *This = (IDsDriverImpl *)iface;
738 FIXME("(%p,%p): stub\n",This,pBuffer);
739 return DSERR_INVALIDCALL;
742 static const IDsDriverVtbl dsdvt =
744 IDsDriverImpl_QueryInterface,
745 IDsDriverImpl_AddRef,
746 IDsDriverImpl_Release,
747 IDsDriverImpl_GetDriverDesc,
748 IDsDriverImpl_Open,
749 IDsDriverImpl_Close,
750 IDsDriverImpl_GetCaps,
751 IDsDriverImpl_CreateSoundBuffer,
752 IDsDriverImpl_DuplicateSoundBuffer
755 DWORD wodDsCreate(UINT wDevID, PIDSDRIVER* drv)
757 IDsDriverImpl** idrv = (IDsDriverImpl**)drv;
759 TRACE("driver created\n");
761 /* the HAL isn't much better than the HEL if we can't do mmap() */
762 if (!(WOutDev[wDevID].outcaps.dwSupport & WAVECAPS_DIRECTSOUND))
764 WARN("MMAP not supported for this device, falling back to waveout, should be harmless\n");
765 return MMSYSERR_NOTSUPPORTED;
768 *idrv = HeapAlloc(GetProcessHeap(),0,sizeof(IDsDriverImpl));
769 if (!*idrv)
770 return MMSYSERR_NOMEM;
771 (*idrv)->lpVtbl = &dsdvt;
772 (*idrv)->ref = 1;
774 (*idrv)->wDevID = wDevID;
775 (*idrv)->primary = NULL;
776 return MMSYSERR_NOERROR;
779 DWORD wodDsDesc(UINT wDevID, PDSDRIVERDESC desc)
781 memcpy(desc, &(WOutDev[wDevID].ds_desc), sizeof(DSDRIVERDESC));
782 return MMSYSERR_NOERROR;
785 #endif /* HAVE_ALSA */