push cc8bc80451cc24f4d7cf75168b569f0ebfe19547
[wine/hacks.git] / dlls / winealsa.drv / dsoutput.c
blob15cff5f1fbf92a264d73c4534407929e9b3244c7
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 <assert.h>
34 #include <stdarg.h>
35 #include <stdio.h>
36 #include <string.h>
37 #ifdef HAVE_UNISTD_H
38 # include <unistd.h>
39 #endif
40 #include <errno.h>
41 #include <limits.h>
42 #include <fcntl.h>
43 #ifdef HAVE_SYS_IOCTL_H
44 # include <sys/ioctl.h>
45 #endif
46 #ifdef HAVE_SYS_MMAN_H
47 # include <sys/mman.h>
48 #endif
49 #include "windef.h"
50 #include "winbase.h"
51 #include "wingdi.h"
52 #include "winerror.h"
53 #include "winuser.h"
54 #include "mmddk.h"
56 #include "alsa.h"
57 #include "wine/library.h"
58 #include "wine/unicode.h"
59 #include "wine/debug.h"
61 #ifdef HAVE_ALSA
63 WINE_DEFAULT_DEBUG_CHANNEL(dsalsa);
65 typedef struct IDsDriverImpl IDsDriverImpl;
66 typedef struct IDsDriverBufferImpl IDsDriverBufferImpl;
68 struct IDsDriverImpl
70 /* IUnknown fields */
71 const IDsDriverVtbl *lpVtbl;
72 LONG ref;
74 /* IDsDriverImpl fields */
75 IDsDriverBufferImpl* primary;
76 UINT wDevID;
79 struct IDsDriverBufferImpl
81 const IDsDriverBufferVtbl *lpVtbl;
82 LONG ref;
83 IDsDriverImpl* drv;
85 CRITICAL_SECTION pcm_crst;
86 LPVOID mmap_buffer;
87 DWORD mmap_buflen_bytes;
89 snd_pcm_t *pcm;
90 snd_pcm_hw_params_t *hw_params;
91 snd_pcm_sw_params_t *sw_params;
92 snd_pcm_uframes_t mmap_buflen_frames, mmap_pos, mmap_commitahead;
95 /** Fill buffers, for starting and stopping
96 * Alsa won't start playing until everything is filled up
97 * This also updates mmap_pos
99 * Returns: Amount of periods in use so snd_pcm_avail_update
100 * doesn't have to be called up to 4x in GetPosition()
102 static snd_pcm_uframes_t CommitAll(IDsDriverBufferImpl *This)
104 const snd_pcm_channel_area_t *areas;
105 snd_pcm_uframes_t used;
106 const snd_pcm_uframes_t commitahead = This->mmap_commitahead;
108 used = This->mmap_buflen_frames - snd_pcm_avail_update(This->pcm);
109 TRACE("%p needs to commit to %lu, used: %lu\n", This, commitahead, used);
110 if (used < commitahead)
112 snd_pcm_uframes_t done, putin = commitahead - used;
113 snd_pcm_mmap_begin(This->pcm, &areas, &This->mmap_pos, &putin);
114 done = snd_pcm_mmap_commit(This->pcm, This->mmap_pos, putin);
115 This->mmap_pos += done;
116 used += done;
117 putin = commitahead - used;
119 if (This->mmap_pos == This->mmap_buflen_frames && (snd_pcm_sframes_t)putin > 0)
121 snd_pcm_mmap_begin(This->pcm, &areas, &This->mmap_pos, &putin);
122 done = snd_pcm_mmap_commit(This->pcm, This->mmap_pos, putin);
123 This->mmap_pos += done;
124 used += done;
128 if (This->mmap_pos == This->mmap_buflen_frames)
129 This->mmap_pos = 0;
131 return used;
134 static void CheckXRUN(IDsDriverBufferImpl* This)
136 snd_pcm_state_t state = snd_pcm_state(This->pcm);
137 snd_pcm_sframes_t delay;
138 int err;
140 snd_pcm_hwsync(This->pcm);
141 snd_pcm_delay(This->pcm, &delay);
142 if ( state == SND_PCM_STATE_XRUN )
144 err = snd_pcm_prepare(This->pcm);
145 CommitAll(This);
146 snd_pcm_start(This->pcm);
147 WARN("xrun occurred\n");
148 if ( err < 0 )
149 ERR("recovery from xrun failed, prepare failed: %s\n", snd_strerror(err));
151 else if ( state == SND_PCM_STATE_SUSPENDED )
153 int err = snd_pcm_resume(This->pcm);
154 TRACE("recovery from suspension occurred\n");
155 if (err < 0 && err != -EAGAIN){
156 err = snd_pcm_prepare(This->pcm);
157 if (err < 0)
158 ERR("recovery from suspend failed, prepare failed: %s\n", snd_strerror(err));
160 } else if ( state != SND_PCM_STATE_RUNNING ) {
161 FIXME("Unhandled state: %d\n", state);
166 * Allocate the memory-mapped buffer for direct sound, and set up the
167 * callback.
169 static int DSDB_CreateMMAP(IDsDriverBufferImpl* pdbi)
171 snd_pcm_t *pcm = pdbi->pcm;
172 snd_pcm_format_t format;
173 snd_pcm_uframes_t frames, ofs, avail, psize, boundary;
174 unsigned int channels, bits_per_sample, bits_per_frame;
175 int err, mmap_mode;
176 const snd_pcm_channel_area_t *areas;
177 snd_pcm_hw_params_t *hw_params = pdbi->hw_params;
178 snd_pcm_sw_params_t *sw_params = pdbi->sw_params;
180 mmap_mode = snd_pcm_type(pcm);
182 if (mmap_mode == SND_PCM_TYPE_HW)
183 TRACE("mmap'd buffer is a direct hardware buffer.\n");
184 else if (mmap_mode == SND_PCM_TYPE_DMIX)
185 TRACE("mmap'd buffer is an ALSA dmix buffer\n");
186 else
187 TRACE("mmap'd buffer is an ALSA type %d buffer\n", mmap_mode);
189 err = snd_pcm_hw_params_get_period_size(hw_params, &psize, NULL);
191 err = snd_pcm_hw_params_get_format(hw_params, &format);
192 err = snd_pcm_hw_params_get_buffer_size(hw_params, &frames);
193 err = snd_pcm_hw_params_get_channels(hw_params, &channels);
194 bits_per_sample = snd_pcm_format_physical_width(format);
195 bits_per_frame = bits_per_sample * channels;
197 if (TRACE_ON(dsalsa))
198 ALSA_TraceParameters(hw_params, NULL, FALSE);
200 TRACE("format=%s frames=%ld channels=%d bits_per_sample=%d bits_per_frame=%d\n",
201 snd_pcm_format_name(format), frames, channels, bits_per_sample, bits_per_frame);
203 pdbi->mmap_buflen_frames = frames;
204 pdbi->mmap_buflen_bytes = snd_pcm_frames_to_bytes( pcm, frames );
206 snd_pcm_sw_params_current(pcm, sw_params);
207 snd_pcm_sw_params_set_start_threshold(pcm, sw_params, 0);
208 snd_pcm_sw_params_get_boundary(sw_params, &boundary);
209 snd_pcm_sw_params_set_stop_threshold(pcm, sw_params, boundary);
210 snd_pcm_sw_params_set_silence_threshold(pcm, sw_params, boundary);
211 snd_pcm_sw_params_set_silence_size(pcm, sw_params, 0);
212 snd_pcm_sw_params_set_avail_min(pcm, sw_params, 0);
213 err = snd_pcm_sw_params(pcm, sw_params);
215 avail = snd_pcm_avail_update(pcm);
216 if ((snd_pcm_sframes_t)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 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);
234 return DS_OK;
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);
251 return refCount;
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);
261 if (refCount)
262 return refCount;
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);
274 This->pcm = NULL;
275 HeapFree(GetProcessHeap(), 0, This->sw_params);
276 HeapFree(GetProcessHeap(), 0, This->hw_params);
277 HeapFree(GetProcessHeap(), 0, This);
278 return 0;
281 static HRESULT WINAPI IDsDriverBufferImpl_Lock(PIDSDRIVERBUFFER iface,
282 LPVOID*ppvAudio1,LPDWORD pdwLen1,
283 LPVOID*ppvAudio2,LPDWORD pdwLen2,
284 DWORD dwWritePosition,DWORD dwWriteLen,
285 DWORD dwFlags)
287 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
288 snd_pcm_uframes_t writepos;
290 TRACE("%d bytes from %d\n", dwWriteLen, dwWritePosition);
292 /* **** */
293 EnterCriticalSection(&This->pcm_crst);
295 if (dwFlags & DSBLOCK_ENTIREBUFFER)
296 dwWriteLen = This->mmap_buflen_bytes;
298 if (dwWriteLen > This->mmap_buflen_bytes || dwWritePosition >= This->mmap_buflen_bytes)
300 /* **** */
301 LeaveCriticalSection(&This->pcm_crst);
302 return DSERR_INVALIDPARAM;
305 if (ppvAudio2) *ppvAudio2 = NULL;
306 if (pdwLen2) *pdwLen2 = 0;
308 *ppvAudio1 = (LPBYTE)This->mmap_buffer + dwWritePosition;
309 *pdwLen1 = dwWriteLen;
311 if (dwWritePosition+dwWriteLen > This->mmap_buflen_bytes)
313 DWORD remainder = This->mmap_buflen_bytes - dwWritePosition;
314 *pdwLen1 = remainder;
316 if (ppvAudio2 && pdwLen2)
318 *ppvAudio2 = This->mmap_buffer;
319 *pdwLen2 = dwWriteLen - remainder;
321 else dwWriteLen = remainder;
324 writepos = snd_pcm_bytes_to_frames(This->pcm, dwWritePosition);
325 if (writepos == This->mmap_pos)
327 const snd_pcm_channel_area_t *areas;
328 snd_pcm_uframes_t writelen = snd_pcm_bytes_to_frames(This->pcm, dwWriteLen), putin = writelen;
329 TRACE("Hit mmap_pos, locking data!\n");
330 snd_pcm_mmap_begin(This->pcm, &areas, &This->mmap_pos, &putin);
332 else
333 WARN("mmap_pos (%lu) != writepos (%lu) not locking data!\n", This->mmap_pos, writepos);
335 LeaveCriticalSection(&This->pcm_crst);
336 /* **** */
337 return DS_OK;
340 static HRESULT WINAPI IDsDriverBufferImpl_Unlock(PIDSDRIVERBUFFER iface,
341 LPVOID pvAudio1,DWORD dwLen1,
342 LPVOID pvAudio2,DWORD dwLen2)
344 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
345 snd_pcm_uframes_t writepos;
347 if (!dwLen1)
348 return DS_OK;
350 /* **** */
351 EnterCriticalSection(&This->pcm_crst);
353 writepos = snd_pcm_bytes_to_frames(This->pcm, (DWORD_PTR)pvAudio1 - (DWORD_PTR)This->mmap_buffer);
354 if (writepos == This->mmap_pos)
356 const snd_pcm_channel_area_t *areas;
357 snd_pcm_uframes_t writelen = snd_pcm_bytes_to_frames(This->pcm, dwLen1);
358 TRACE("Committing data\n");
359 This->mmap_pos += snd_pcm_mmap_commit(This->pcm, This->mmap_pos, writelen);
360 if (This->mmap_pos == This->mmap_buflen_frames)
361 This->mmap_pos = 0;
362 if (!This->mmap_pos && dwLen2)
364 writelen = snd_pcm_bytes_to_frames(This->pcm, dwLen2);
365 snd_pcm_mmap_begin(This->pcm, &areas, &This->mmap_pos, &writelen);
366 This->mmap_pos += snd_pcm_mmap_commit(This->pcm, This->mmap_pos, writelen);
367 assert(This->mmap_pos < This->mmap_buflen_frames);
370 LeaveCriticalSection(&This->pcm_crst);
371 /* **** */
373 return DS_OK;
376 static HRESULT SetFormat(IDsDriverBufferImpl *This, LPWAVEFORMATEX pwfx)
378 snd_pcm_t *pcm = NULL;
379 snd_pcm_hw_params_t *hw_params = This->hw_params;
380 unsigned int buffer_time = 500000;
381 snd_pcm_format_t format = -1;
382 snd_pcm_uframes_t psize;
383 DWORD rate = pwfx->nSamplesPerSec;
384 int err=0;
386 switch (pwfx->wBitsPerSample)
388 case 8: format = SND_PCM_FORMAT_U8; break;
389 case 16: format = SND_PCM_FORMAT_S16_LE; break;
390 case 24: format = SND_PCM_FORMAT_S24_3LE; break;
391 case 32: format = SND_PCM_FORMAT_S32_LE; break;
392 default: FIXME("Unsupported bpp: %d\n", pwfx->wBitsPerSample); return DSERR_GENERIC;
395 err = snd_pcm_open(&pcm, WOutDev[This->drv->wDevID].pcmname, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
396 if (err < 0)
398 if (errno != EBUSY || !This->pcm)
400 WARN("Cannot open sound device: %s\n", snd_strerror(err));
401 return DSERR_GENERIC;
403 snd_pcm_drop(This->pcm);
404 snd_pcm_close(This->pcm);
405 This->pcm = NULL;
406 err = snd_pcm_open(&pcm, WOutDev[This->drv->wDevID].pcmname, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
407 if (err < 0)
409 WARN("Cannot open sound device: %s\n", snd_strerror(err));
410 return DSERR_BUFFERLOST;
414 /* Set some defaults */
415 snd_pcm_hw_params_any(pcm, hw_params);
416 err = snd_pcm_hw_params_set_channels(pcm, hw_params, pwfx->nChannels);
417 if (err < 0) { WARN("Could not set channels to %d\n", pwfx->nChannels); goto err; }
419 err = snd_pcm_hw_params_set_format(pcm, hw_params, format);
420 if (err < 0) { WARN("Could not set format to %d bpp\n", pwfx->wBitsPerSample); goto err; }
422 /* Alsa's rate resampling is only used if the application specifically requests
423 * a buffer at a certain frequency, else it is better to disable it due to unwanted
424 * side effects, which may include: Less granular pointer, changing buffer sizes, etc
426 #if SND_LIB_VERSION >= 0x010009
427 snd_pcm_hw_params_set_rate_resample(pcm, hw_params, 0);
428 #endif
430 err = snd_pcm_hw_params_set_rate_near(pcm, hw_params, &rate, NULL);
431 if (err < 0) { rate = pwfx->nSamplesPerSec; WARN("Could not set rate\n"); goto err; }
433 if (!ALSA_NearMatch(rate, pwfx->nSamplesPerSec))
435 WARN("Could not set sound rate to %d, but instead to %d\n", pwfx->nSamplesPerSec, rate);
436 pwfx->nSamplesPerSec = rate;
437 pwfx->nAvgBytesPerSec = rate * pwfx->nBlockAlign;
438 /* Let DirectSound detect this */
441 snd_pcm_hw_params_set_periods_integer(pcm, hw_params);
442 snd_pcm_hw_params_set_buffer_time_near(pcm, hw_params, &buffer_time, NULL);
443 buffer_time = 10000;
444 snd_pcm_hw_params_set_period_time_near(pcm, hw_params, &buffer_time, NULL);
445 err = snd_pcm_hw_params(pcm, hw_params);
446 err = snd_pcm_sw_params(pcm, This->sw_params);
447 snd_pcm_prepare(pcm);
449 err = snd_pcm_hw_params_get_period_size(hw_params, &psize, NULL);
450 TRACE("Period size is: %lu\n", psize);
452 /* ALSA needs at least 3 buffers to work successfully */
453 This->mmap_commitahead = 3 * psize;
454 while (This->mmap_commitahead <= 512)
455 This->mmap_commitahead += psize;
457 if (This->pcm)
459 snd_pcm_drop(This->pcm);
460 snd_pcm_close(This->pcm);
462 This->pcm = pcm;
463 snd_pcm_prepare(This->pcm);
464 DSDB_CreateMMAP(This);
465 return S_OK;
467 err:
468 if (err < 0)
469 WARN("Failed to apply changes: %s\n", snd_strerror(err));
471 if (!This->pcm)
472 This->pcm = pcm;
473 else
474 snd_pcm_close(pcm);
476 if (This->pcm)
477 snd_pcm_hw_params_current(This->pcm, This->hw_params);
479 return DSERR_BADFORMAT;
482 static HRESULT WINAPI IDsDriverBufferImpl_SetFormat(PIDSDRIVERBUFFER iface, LPWAVEFORMATEX pwfx)
484 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
485 HRESULT hr = S_OK;
487 TRACE("(%p, %p)\n", iface, pwfx);
489 /* **** */
490 EnterCriticalSection(&This->pcm_crst);
491 hr = SetFormat(This, pwfx);
492 /* **** */
493 LeaveCriticalSection(&This->pcm_crst);
495 if (hr == DS_OK)
496 return S_FALSE;
497 return hr;
500 static HRESULT WINAPI IDsDriverBufferImpl_SetFrequency(PIDSDRIVERBUFFER iface, DWORD dwFreq)
502 /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
503 FIXME("(%p,%d): stub\n",iface,dwFreq);
504 return S_OK;
507 static HRESULT WINAPI IDsDriverBufferImpl_SetVolumePan(PIDSDRIVERBUFFER iface, PDSVOLUMEPAN pVolPan)
509 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
510 FIXME("(%p,%p): stub\n",This,pVolPan);
511 /* TODO: Bring volume control back */
512 return DS_OK;
515 static HRESULT WINAPI IDsDriverBufferImpl_SetPosition(PIDSDRIVERBUFFER iface, DWORD dwNewPos)
517 /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
518 /* I don't even think alsa allows this */
519 FIXME("(%p,%d): stub\n",iface,dwNewPos);
520 return DSERR_UNSUPPORTED;
523 static HRESULT WINAPI IDsDriverBufferImpl_GetPosition(PIDSDRIVERBUFFER iface,
524 LPDWORD lpdwPlay, LPDWORD lpdwWrite)
526 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
527 snd_pcm_uframes_t hw_pptr, hw_wptr;
528 snd_pcm_state_t state;
530 /* **** */
531 EnterCriticalSection(&This->pcm_crst);
533 if (!This->pcm)
535 FIXME("Bad pointer for pcm: %p\n", This->pcm);
536 LeaveCriticalSection(&This->pcm_crst);
537 return DSERR_GENERIC;
540 if (!lpdwPlay && !lpdwWrite)
541 CommitAll(This);
543 state = snd_pcm_state(This->pcm);
545 if (state != SND_PCM_STATE_PREPARED && state != SND_PCM_STATE_RUNNING)
547 CheckXRUN(This);
548 state = snd_pcm_state(This->pcm);
550 if (state == SND_PCM_STATE_RUNNING)
552 snd_pcm_sframes_t used = This->mmap_buflen_frames - snd_pcm_avail_update(This->pcm);
554 if (used < 0)
556 This->mmap_pos += -used;
557 snd_pcm_forward(This->pcm, -used);
558 This->mmap_pos %= This->mmap_buflen_frames;
559 used = 0;
562 if (This->mmap_pos > used)
563 hw_pptr = This->mmap_pos - used;
564 else
565 hw_pptr = This->mmap_buflen_frames + This->mmap_pos - used;
566 hw_pptr %= This->mmap_buflen_frames;
568 TRACE("At position: %ld (%ld) - Used %ld\n", hw_pptr, This->mmap_pos, used);
570 else hw_pptr = This->mmap_pos;
571 hw_wptr = This->mmap_pos;
573 LeaveCriticalSection(&This->pcm_crst);
574 /* **** */
576 if (lpdwPlay)
577 *lpdwPlay = snd_pcm_frames_to_bytes(This->pcm, hw_pptr);
578 if (lpdwWrite)
579 *lpdwWrite = snd_pcm_frames_to_bytes(This->pcm, hw_wptr);
581 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);
582 return DS_OK;
585 static HRESULT WINAPI IDsDriverBufferImpl_Play(PIDSDRIVERBUFFER iface, DWORD dwRes1, DWORD dwRes2, DWORD dwFlags)
587 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
588 TRACE("(%p,%x,%x,%x)\n",iface,dwRes1,dwRes2,dwFlags);
590 /* **** */
591 EnterCriticalSection(&This->pcm_crst);
592 snd_pcm_start(This->pcm);
593 /* **** */
594 LeaveCriticalSection(&This->pcm_crst);
595 return DS_OK;
598 static HRESULT WINAPI IDsDriverBufferImpl_Stop(PIDSDRIVERBUFFER iface)
600 const snd_pcm_channel_area_t *areas;
601 snd_pcm_uframes_t avail;
602 snd_pcm_format_t format;
603 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
604 TRACE("(%p)\n",iface);
606 /* **** */
607 EnterCriticalSection(&This->pcm_crst);
608 avail = This->mmap_buflen_frames;
609 snd_pcm_drop(This->pcm);
610 snd_pcm_prepare(This->pcm);
611 avail = snd_pcm_avail_update(This->pcm);
612 snd_pcm_mmap_begin(This->pcm, &areas, &This->mmap_pos, &avail);
613 snd_pcm_hw_params_get_format(This->hw_params, &format);
614 snd_pcm_format_set_silence(format, areas->addr, This->mmap_buflen_frames);
615 snd_pcm_mmap_commit(This->pcm, This->mmap_pos, 0);
617 /* **** */
618 LeaveCriticalSection(&This->pcm_crst);
619 return DS_OK;
622 static const IDsDriverBufferVtbl dsdbvt =
624 IDsDriverBufferImpl_QueryInterface,
625 IDsDriverBufferImpl_AddRef,
626 IDsDriverBufferImpl_Release,
627 IDsDriverBufferImpl_Lock,
628 IDsDriverBufferImpl_Unlock,
629 IDsDriverBufferImpl_SetFormat,
630 IDsDriverBufferImpl_SetFrequency,
631 IDsDriverBufferImpl_SetVolumePan,
632 IDsDriverBufferImpl_SetPosition,
633 IDsDriverBufferImpl_GetPosition,
634 IDsDriverBufferImpl_Play,
635 IDsDriverBufferImpl_Stop
638 static HRESULT WINAPI IDsDriverImpl_QueryInterface(PIDSDRIVER iface, REFIID riid, LPVOID *ppobj)
640 /* IDsDriverImpl *This = (IDsDriverImpl *)iface; */
641 FIXME("(%p): stub!\n",iface);
642 return DSERR_UNSUPPORTED;
645 static ULONG WINAPI IDsDriverImpl_AddRef(PIDSDRIVER iface)
647 IDsDriverImpl *This = (IDsDriverImpl *)iface;
648 ULONG refCount = InterlockedIncrement(&This->ref);
650 TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
652 return refCount;
655 static ULONG WINAPI IDsDriverImpl_Release(PIDSDRIVER iface)
657 IDsDriverImpl *This = (IDsDriverImpl *)iface;
658 ULONG refCount = InterlockedDecrement(&This->ref);
660 TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
662 if (refCount)
663 return refCount;
665 HeapFree(GetProcessHeap(), 0, This);
666 return 0;
669 static HRESULT WINAPI IDsDriverImpl_GetDriverDesc(PIDSDRIVER iface, PDSDRIVERDESC pDesc)
671 IDsDriverImpl *This = (IDsDriverImpl *)iface;
672 TRACE("(%p,%p)\n",iface,pDesc);
673 *pDesc = WOutDev[This->wDevID].ds_desc;
674 pDesc->dwFlags = DSDDESC_DONTNEEDSECONDARYLOCK | DSDDESC_DONTNEEDWRITELEAD;
675 pDesc->dnDevNode = WOutDev[This->wDevID].waveDesc.dnDevNode;
676 pDesc->wVxdId = 0;
677 pDesc->wReserved = 0;
678 pDesc->ulDeviceNum = This->wDevID;
679 pDesc->dwHeapType = DSDHEAP_NOHEAP;
680 pDesc->pvDirectDrawHeap = NULL;
681 pDesc->dwMemStartAddress = 0xDEAD0000;
682 pDesc->dwMemEndAddress = 0xDEAF0000;
683 pDesc->dwMemAllocExtra = 0;
684 pDesc->pvReserved1 = NULL;
685 pDesc->pvReserved2 = NULL;
686 return DS_OK;
689 static HRESULT WINAPI IDsDriverImpl_Open(PIDSDRIVER iface)
691 HRESULT hr = S_OK;
692 IDsDriverImpl *This = (IDsDriverImpl *)iface;
693 int err=0;
694 snd_pcm_t *pcm = NULL;
695 snd_pcm_hw_params_t *hw_params;
697 /* While this is not really needed, it is a good idea to do this,
698 * to see if sound can be initialized */
700 hw_params = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, snd_pcm_hw_params_sizeof());
702 if (!hw_params)
704 hr = DSERR_OUTOFMEMORY;
705 goto unalloc;
708 err = snd_pcm_open(&pcm, WOutDev[This->wDevID].pcmname, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
709 if (err < 0) goto err;
710 err = snd_pcm_hw_params_any(pcm, hw_params);
711 if (err < 0) goto err;
712 err = snd_pcm_hw_params_set_access (pcm, hw_params, SND_PCM_ACCESS_MMAP_INTERLEAVED);
713 if (err < 0) goto err;
715 TRACE("Success\n");
716 snd_pcm_close(pcm);
717 goto unalloc;
719 err:
720 hr = DSERR_GENERIC;
721 FIXME("Failed to open device: %s\n", snd_strerror(err));
722 if (pcm)
723 snd_pcm_close(pcm);
724 unalloc:
725 HeapFree(GetProcessHeap(), 0, hw_params);
726 if (hr != S_OK)
727 WARN("--> %08x\n", hr);
728 return hr;
731 static HRESULT WINAPI IDsDriverImpl_Close(PIDSDRIVER iface)
733 IDsDriverImpl *This = (IDsDriverImpl *)iface;
734 TRACE("(%p) stub, harmless\n",This);
735 return DS_OK;
738 static HRESULT WINAPI IDsDriverImpl_GetCaps(PIDSDRIVER iface, PDSDRIVERCAPS pCaps)
740 IDsDriverImpl *This = (IDsDriverImpl *)iface;
741 TRACE("(%p,%p)\n",iface,pCaps);
742 *pCaps = WOutDev[This->wDevID].ds_caps;
743 return DS_OK;
746 static HRESULT WINAPI IDsDriverImpl_CreateSoundBuffer(PIDSDRIVER iface,
747 LPWAVEFORMATEX pwfx,
748 DWORD dwFlags, DWORD dwCardAddress,
749 LPDWORD pdwcbBufferSize,
750 LPBYTE *ppbBuffer,
751 LPVOID *ppvObj)
753 IDsDriverImpl *This = (IDsDriverImpl *)iface;
754 IDsDriverBufferImpl** ippdsdb = (IDsDriverBufferImpl**)ppvObj;
755 HRESULT err;
757 TRACE("(%p,%p,%x,%x)\n",iface,pwfx,dwFlags,dwCardAddress);
758 /* we only support primary buffers... for now */
759 if (!(dwFlags & DSBCAPS_PRIMARYBUFFER))
760 return DSERR_UNSUPPORTED;
761 if (This->primary)
762 return DSERR_ALLOCATED;
764 *ippdsdb = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDsDriverBufferImpl));
765 if (*ippdsdb == NULL)
766 return DSERR_OUTOFMEMORY;
768 (*ippdsdb)->hw_params = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, snd_pcm_hw_params_sizeof());
769 (*ippdsdb)->sw_params = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, snd_pcm_sw_params_sizeof());
770 if (!(*ippdsdb)->hw_params || !(*ippdsdb)->sw_params)
772 HeapFree(GetProcessHeap(), 0, (*ippdsdb)->sw_params);
773 HeapFree(GetProcessHeap(), 0, (*ippdsdb)->hw_params);
774 return DSERR_OUTOFMEMORY;
776 (*ippdsdb)->lpVtbl = &dsdbvt;
777 (*ippdsdb)->ref = 1;
778 (*ippdsdb)->drv = This;
779 InitializeCriticalSection(&(*ippdsdb)->pcm_crst);
780 (*ippdsdb)->pcm_crst.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": ALSA_DSOUTPUT.pcm_crst");
782 /* SetFormat has to re-initialize pcm here anyway */
783 err = SetFormat(*ippdsdb, pwfx);
784 if (FAILED(err))
786 WARN("Error occurred: %08x\n", err);
787 goto err;
790 if (dwFlags & DSBCAPS_PRIMARYBUFFER)
791 This->primary = *ippdsdb;
793 *pdwcbBufferSize = (*ippdsdb)->mmap_buflen_bytes;
794 *ppbBuffer = (*ippdsdb)->mmap_buffer;
796 /* buffer is ready to go */
797 TRACE("buffer created at %p\n", *ippdsdb);
798 return err;
800 err:
801 HeapFree(GetProcessHeap(), 0, (*ippdsdb)->sw_params);
802 HeapFree(GetProcessHeap(), 0, (*ippdsdb)->hw_params);
803 HeapFree(GetProcessHeap(), 0, *ippdsdb);
804 *ippdsdb = NULL;
805 return err;
808 static HRESULT WINAPI IDsDriverImpl_DuplicateSoundBuffer(PIDSDRIVER iface,
809 PIDSDRIVERBUFFER pBuffer,
810 LPVOID *ppvObj)
812 IDsDriverImpl *This = (IDsDriverImpl *)iface;
813 FIXME("(%p,%p): stub\n",This,pBuffer);
814 return DSERR_INVALIDCALL;
817 static const IDsDriverVtbl dsdvt =
819 IDsDriverImpl_QueryInterface,
820 IDsDriverImpl_AddRef,
821 IDsDriverImpl_Release,
822 IDsDriverImpl_GetDriverDesc,
823 IDsDriverImpl_Open,
824 IDsDriverImpl_Close,
825 IDsDriverImpl_GetCaps,
826 IDsDriverImpl_CreateSoundBuffer,
827 IDsDriverImpl_DuplicateSoundBuffer
830 DWORD wodDsCreate(UINT wDevID, PIDSDRIVER* drv)
832 IDsDriverImpl** idrv = (IDsDriverImpl**)drv;
834 TRACE("driver created\n");
836 /* the HAL isn't much better than the HEL if we can't do mmap() */
837 if (!(WOutDev[wDevID].outcaps.dwSupport & WAVECAPS_DIRECTSOUND))
839 WARN("MMAP not supported for this device, falling back to waveout, should be harmless\n");
840 return MMSYSERR_NOTSUPPORTED;
843 *idrv = HeapAlloc(GetProcessHeap(),0,sizeof(IDsDriverImpl));
844 if (!*idrv)
845 return MMSYSERR_NOMEM;
846 (*idrv)->lpVtbl = &dsdvt;
847 (*idrv)->ref = 1;
849 (*idrv)->wDevID = wDevID;
850 (*idrv)->primary = NULL;
851 return MMSYSERR_NOERROR;
854 DWORD wodDsDesc(UINT wDevID, PDSDRIVERDESC desc)
856 *desc = WOutDev[wDevID].ds_desc;
857 return MMSYSERR_NOERROR;
860 #endif /* HAVE_ALSA */