dplayx: Tests for checking the behaviour of groups in a p2p session.
[wine/gsoc_dplay.git] / dlls / winealsa.drv / dsoutput.c
blob4043ccf036a9fbd1b843857f27c2ad736194f09b
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);
333 LeaveCriticalSection(&This->pcm_crst);
334 /* **** */
335 return DS_OK;
338 static HRESULT WINAPI IDsDriverBufferImpl_Unlock(PIDSDRIVERBUFFER iface,
339 LPVOID pvAudio1,DWORD dwLen1,
340 LPVOID pvAudio2,DWORD dwLen2)
342 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
343 snd_pcm_uframes_t writepos;
345 if (!dwLen1)
346 return DS_OK;
348 /* **** */
349 EnterCriticalSection(&This->pcm_crst);
351 writepos = snd_pcm_bytes_to_frames(This->pcm, (DWORD_PTR)pvAudio1 - (DWORD_PTR)This->mmap_buffer);
352 if (writepos == This->mmap_pos)
354 const snd_pcm_channel_area_t *areas;
355 snd_pcm_uframes_t writelen = snd_pcm_bytes_to_frames(This->pcm, dwLen1);
356 TRACE("Committing data\n");
357 This->mmap_pos += snd_pcm_mmap_commit(This->pcm, This->mmap_pos, writelen);
358 if (This->mmap_pos == This->mmap_buflen_frames)
359 This->mmap_pos = 0;
360 if (!This->mmap_pos && dwLen2)
362 writelen = snd_pcm_bytes_to_frames(This->pcm, dwLen2);
363 snd_pcm_mmap_begin(This->pcm, &areas, &This->mmap_pos, &writelen);
364 This->mmap_pos += snd_pcm_mmap_commit(This->pcm, This->mmap_pos, writelen);
365 assert(This->mmap_pos < This->mmap_buflen_frames);
368 LeaveCriticalSection(&This->pcm_crst);
369 /* **** */
371 return DS_OK;
374 static HRESULT SetFormat(IDsDriverBufferImpl *This, LPWAVEFORMATEX pwfx)
376 snd_pcm_t *pcm = NULL;
377 snd_pcm_hw_params_t *hw_params = This->hw_params;
378 unsigned int buffer_time = 500000;
379 snd_pcm_format_t format = -1;
380 snd_pcm_uframes_t psize;
381 DWORD rate = pwfx->nSamplesPerSec;
382 int err=0;
384 switch (pwfx->wBitsPerSample)
386 case 8: format = SND_PCM_FORMAT_U8; break;
387 case 16: format = SND_PCM_FORMAT_S16_LE; break;
388 case 24: format = SND_PCM_FORMAT_S24_3LE; break;
389 case 32: format = SND_PCM_FORMAT_S32_LE; break;
390 default: FIXME("Unsupported bpp: %d\n", pwfx->wBitsPerSample); return DSERR_GENERIC;
393 err = snd_pcm_open(&pcm, WOutDev[This->drv->wDevID].pcmname, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
394 if (err < 0)
396 if (errno != EBUSY || !This->pcm)
398 WARN("Cannot open sound device: %s\n", snd_strerror(err));
399 return DSERR_GENERIC;
401 snd_pcm_drop(This->pcm);
402 snd_pcm_close(This->pcm);
403 This->pcm = NULL;
404 err = snd_pcm_open(&pcm, WOutDev[This->drv->wDevID].pcmname, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
405 if (err < 0)
407 WARN("Cannot open sound device: %s\n", snd_strerror(err));
408 return DSERR_BUFFERLOST;
412 /* Set some defaults */
413 snd_pcm_hw_params_any(pcm, hw_params);
414 err = snd_pcm_hw_params_set_channels(pcm, hw_params, pwfx->nChannels);
415 if (err < 0) { WARN("Could not set channels to %d\n", pwfx->nChannels); goto err; }
417 err = snd_pcm_hw_params_set_format(pcm, hw_params, format);
418 if (err < 0) { WARN("Could not set format to %d bpp\n", pwfx->wBitsPerSample); goto err; }
420 /* Alsa's rate resampling is only used if the application specifically requests
421 * a buffer at a certain frequency, else it is better to disable it due to unwanted
422 * side effects, which may include: Less granular pointer, changing buffer sizes, etc
424 #if SND_LIB_VERSION >= 0x010009
425 snd_pcm_hw_params_set_rate_resample(pcm, hw_params, 0);
426 #endif
428 err = snd_pcm_hw_params_set_rate_near(pcm, hw_params, &rate, NULL);
429 if (err < 0) { rate = pwfx->nSamplesPerSec; WARN("Could not set rate\n"); goto err; }
431 if (!ALSA_NearMatch(rate, pwfx->nSamplesPerSec))
433 WARN("Could not set sound rate to %d, but instead to %d\n", pwfx->nSamplesPerSec, rate);
434 pwfx->nSamplesPerSec = rate;
435 pwfx->nAvgBytesPerSec = rate * pwfx->nBlockAlign;
436 /* Let DirectSound detect this */
439 snd_pcm_hw_params_set_periods_integer(pcm, hw_params);
440 snd_pcm_hw_params_set_buffer_time_near(pcm, hw_params, &buffer_time, NULL);
441 buffer_time = 10000;
442 snd_pcm_hw_params_set_period_time_near(pcm, hw_params, &buffer_time, NULL);
443 err = snd_pcm_hw_params(pcm, hw_params);
444 err = snd_pcm_sw_params(pcm, This->sw_params);
445 snd_pcm_prepare(pcm);
447 err = snd_pcm_hw_params_get_period_size(hw_params, &psize, NULL);
448 TRACE("Period size is: %lu\n", psize);
450 /* ALSA needs at least 3 buffers to work successfully */
451 This->mmap_commitahead = 3 * psize;
452 while (This->mmap_commitahead <= 512)
453 This->mmap_commitahead += psize;
455 if (This->pcm)
457 snd_pcm_drop(This->pcm);
458 snd_pcm_close(This->pcm);
460 This->pcm = pcm;
461 snd_pcm_prepare(This->pcm);
462 DSDB_CreateMMAP(This);
463 return S_OK;
465 err:
466 if (err < 0)
467 WARN("Failed to apply changes: %s\n", snd_strerror(err));
469 if (!This->pcm)
470 This->pcm = pcm;
471 else
472 snd_pcm_close(pcm);
474 if (This->pcm)
475 snd_pcm_hw_params_current(This->pcm, This->hw_params);
477 return DSERR_BADFORMAT;
480 static HRESULT WINAPI IDsDriverBufferImpl_SetFormat(PIDSDRIVERBUFFER iface, LPWAVEFORMATEX pwfx)
482 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
483 HRESULT hr = S_OK;
485 TRACE("(%p, %p)\n", iface, pwfx);
487 /* **** */
488 EnterCriticalSection(&This->pcm_crst);
489 hr = SetFormat(This, pwfx);
490 /* **** */
491 LeaveCriticalSection(&This->pcm_crst);
493 if (hr == DS_OK)
494 return S_FALSE;
495 return hr;
498 static HRESULT WINAPI IDsDriverBufferImpl_SetFrequency(PIDSDRIVERBUFFER iface, DWORD dwFreq)
500 /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
501 FIXME("(%p,%d): stub\n",iface,dwFreq);
502 return S_OK;
505 static HRESULT WINAPI IDsDriverBufferImpl_SetVolumePan(PIDSDRIVERBUFFER iface, PDSVOLUMEPAN pVolPan)
507 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
508 FIXME("(%p,%p): stub\n",This,pVolPan);
509 /* TODO: Bring volume control back */
510 return DS_OK;
513 static HRESULT WINAPI IDsDriverBufferImpl_SetPosition(PIDSDRIVERBUFFER iface, DWORD dwNewPos)
515 /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
516 /* I don't even think alsa allows this */
517 FIXME("(%p,%d): stub\n",iface,dwNewPos);
518 return DSERR_UNSUPPORTED;
521 static HRESULT WINAPI IDsDriverBufferImpl_GetPosition(PIDSDRIVERBUFFER iface,
522 LPDWORD lpdwPlay, LPDWORD lpdwWrite)
524 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
525 snd_pcm_uframes_t hw_pptr, hw_wptr;
526 snd_pcm_state_t state;
528 /* **** */
529 EnterCriticalSection(&This->pcm_crst);
531 if (!This->pcm)
533 FIXME("Bad pointer for pcm: %p\n", This->pcm);
534 LeaveCriticalSection(&This->pcm_crst);
535 return DSERR_GENERIC;
538 if (!lpdwPlay && !lpdwWrite)
539 CommitAll(This);
541 state = snd_pcm_state(This->pcm);
543 if (state != SND_PCM_STATE_PREPARED && state != SND_PCM_STATE_RUNNING)
545 CheckXRUN(This);
546 state = snd_pcm_state(This->pcm);
548 if (state == SND_PCM_STATE_RUNNING)
550 snd_pcm_sframes_t used = This->mmap_buflen_frames - snd_pcm_avail_update(This->pcm);
552 if (used < 0)
554 snd_pcm_forward(This->pcm, -used);
555 used = 0;
558 if (This->mmap_pos > used)
559 hw_pptr = This->mmap_pos - used;
560 else
561 hw_pptr = This->mmap_buflen_frames + This->mmap_pos - used;
562 hw_pptr %= This->mmap_buflen_frames;
564 TRACE("At position: %ld (%ld) - Used %ld\n", hw_pptr, This->mmap_pos, used);
566 else hw_pptr = This->mmap_pos;
567 hw_wptr = This->mmap_pos;
569 LeaveCriticalSection(&This->pcm_crst);
570 /* **** */
572 if (lpdwPlay)
573 *lpdwPlay = snd_pcm_frames_to_bytes(This->pcm, hw_pptr);
574 if (lpdwWrite)
575 *lpdwWrite = snd_pcm_frames_to_bytes(This->pcm, hw_wptr);
577 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);
578 return DS_OK;
581 static HRESULT WINAPI IDsDriverBufferImpl_Play(PIDSDRIVERBUFFER iface, DWORD dwRes1, DWORD dwRes2, DWORD dwFlags)
583 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
584 TRACE("(%p,%x,%x,%x)\n",iface,dwRes1,dwRes2,dwFlags);
586 /* **** */
587 EnterCriticalSection(&This->pcm_crst);
588 snd_pcm_start(This->pcm);
589 /* **** */
590 LeaveCriticalSection(&This->pcm_crst);
591 return DS_OK;
594 static HRESULT WINAPI IDsDriverBufferImpl_Stop(PIDSDRIVERBUFFER iface)
596 const snd_pcm_channel_area_t *areas;
597 snd_pcm_uframes_t avail;
598 snd_pcm_format_t format;
599 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
600 TRACE("(%p)\n",iface);
602 /* **** */
603 EnterCriticalSection(&This->pcm_crst);
604 avail = This->mmap_buflen_frames;
605 snd_pcm_drop(This->pcm);
606 snd_pcm_prepare(This->pcm);
607 avail = snd_pcm_avail_update(This->pcm);
608 snd_pcm_mmap_begin(This->pcm, &areas, &This->mmap_pos, &avail);
609 snd_pcm_hw_params_get_format(This->hw_params, &format);
610 snd_pcm_format_set_silence(format, areas->addr, This->mmap_buflen_frames);
611 snd_pcm_mmap_commit(This->pcm, This->mmap_pos, 0);
613 /* **** */
614 LeaveCriticalSection(&This->pcm_crst);
615 return DS_OK;
618 static const IDsDriverBufferVtbl dsdbvt =
620 IDsDriverBufferImpl_QueryInterface,
621 IDsDriverBufferImpl_AddRef,
622 IDsDriverBufferImpl_Release,
623 IDsDriverBufferImpl_Lock,
624 IDsDriverBufferImpl_Unlock,
625 IDsDriverBufferImpl_SetFormat,
626 IDsDriverBufferImpl_SetFrequency,
627 IDsDriverBufferImpl_SetVolumePan,
628 IDsDriverBufferImpl_SetPosition,
629 IDsDriverBufferImpl_GetPosition,
630 IDsDriverBufferImpl_Play,
631 IDsDriverBufferImpl_Stop
634 static HRESULT WINAPI IDsDriverImpl_QueryInterface(PIDSDRIVER iface, REFIID riid, LPVOID *ppobj)
636 /* IDsDriverImpl *This = (IDsDriverImpl *)iface; */
637 FIXME("(%p): stub!\n",iface);
638 return DSERR_UNSUPPORTED;
641 static ULONG WINAPI IDsDriverImpl_AddRef(PIDSDRIVER iface)
643 IDsDriverImpl *This = (IDsDriverImpl *)iface;
644 ULONG refCount = InterlockedIncrement(&This->ref);
646 TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
648 return refCount;
651 static ULONG WINAPI IDsDriverImpl_Release(PIDSDRIVER iface)
653 IDsDriverImpl *This = (IDsDriverImpl *)iface;
654 ULONG refCount = InterlockedDecrement(&This->ref);
656 TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
658 if (refCount)
659 return refCount;
661 HeapFree(GetProcessHeap(), 0, This);
662 return 0;
665 static HRESULT WINAPI IDsDriverImpl_GetDriverDesc(PIDSDRIVER iface, PDSDRIVERDESC pDesc)
667 IDsDriverImpl *This = (IDsDriverImpl *)iface;
668 TRACE("(%p,%p)\n",iface,pDesc);
669 *pDesc = WOutDev[This->wDevID].ds_desc;
670 pDesc->dwFlags = DSDDESC_DONTNEEDSECONDARYLOCK | DSDDESC_DONTNEEDWRITELEAD;
671 pDesc->dnDevNode = WOutDev[This->wDevID].waveDesc.dnDevNode;
672 pDesc->wVxdId = 0;
673 pDesc->wReserved = 0;
674 pDesc->ulDeviceNum = This->wDevID;
675 pDesc->dwHeapType = DSDHEAP_NOHEAP;
676 pDesc->pvDirectDrawHeap = NULL;
677 pDesc->dwMemStartAddress = 0xDEAD0000;
678 pDesc->dwMemEndAddress = 0xDEAF0000;
679 pDesc->dwMemAllocExtra = 0;
680 pDesc->pvReserved1 = NULL;
681 pDesc->pvReserved2 = NULL;
682 return DS_OK;
685 static HRESULT WINAPI IDsDriverImpl_Open(PIDSDRIVER iface)
687 HRESULT hr = S_OK;
688 IDsDriverImpl *This = (IDsDriverImpl *)iface;
689 int err=0;
690 snd_pcm_t *pcm = NULL;
691 snd_pcm_hw_params_t *hw_params;
693 /* While this is not really needed, it is a good idea to do this,
694 * to see if sound can be initialized */
696 hw_params = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, snd_pcm_hw_params_sizeof());
698 if (!hw_params)
700 hr = DSERR_OUTOFMEMORY;
701 goto unalloc;
704 err = snd_pcm_open(&pcm, WOutDev[This->wDevID].pcmname, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
705 if (err < 0) goto err;
706 err = snd_pcm_hw_params_any(pcm, hw_params);
707 if (err < 0) goto err;
708 err = snd_pcm_hw_params_set_access (pcm, hw_params, SND_PCM_ACCESS_MMAP_INTERLEAVED);
709 if (err < 0) goto err;
711 TRACE("Success\n");
712 snd_pcm_close(pcm);
713 goto unalloc;
715 err:
716 hr = DSERR_GENERIC;
717 FIXME("Failed to open device: %s\n", snd_strerror(err));
718 if (pcm)
719 snd_pcm_close(pcm);
720 unalloc:
721 HeapFree(GetProcessHeap(), 0, hw_params);
722 if (hr != S_OK)
723 WARN("--> %08x\n", hr);
724 return hr;
727 static HRESULT WINAPI IDsDriverImpl_Close(PIDSDRIVER iface)
729 IDsDriverImpl *This = (IDsDriverImpl *)iface;
730 TRACE("(%p) stub, harmless\n",This);
731 return DS_OK;
734 static HRESULT WINAPI IDsDriverImpl_GetCaps(PIDSDRIVER iface, PDSDRIVERCAPS pCaps)
736 IDsDriverImpl *This = (IDsDriverImpl *)iface;
737 TRACE("(%p,%p)\n",iface,pCaps);
738 *pCaps = WOutDev[This->wDevID].ds_caps;
739 return DS_OK;
742 static HRESULT WINAPI IDsDriverImpl_CreateSoundBuffer(PIDSDRIVER iface,
743 LPWAVEFORMATEX pwfx,
744 DWORD dwFlags, DWORD dwCardAddress,
745 LPDWORD pdwcbBufferSize,
746 LPBYTE *ppbBuffer,
747 LPVOID *ppvObj)
749 IDsDriverImpl *This = (IDsDriverImpl *)iface;
750 IDsDriverBufferImpl** ippdsdb = (IDsDriverBufferImpl**)ppvObj;
751 HRESULT err;
753 TRACE("(%p,%p,%x,%x)\n",iface,pwfx,dwFlags,dwCardAddress);
754 /* we only support primary buffers... for now */
755 if (!(dwFlags & DSBCAPS_PRIMARYBUFFER))
756 return DSERR_UNSUPPORTED;
757 if (This->primary)
758 return DSERR_ALLOCATED;
760 *ippdsdb = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDsDriverBufferImpl));
761 if (*ippdsdb == NULL)
762 return DSERR_OUTOFMEMORY;
764 (*ippdsdb)->hw_params = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, snd_pcm_hw_params_sizeof());
765 (*ippdsdb)->sw_params = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, snd_pcm_sw_params_sizeof());
766 if (!(*ippdsdb)->hw_params || !(*ippdsdb)->sw_params)
768 HeapFree(GetProcessHeap(), 0, (*ippdsdb)->sw_params);
769 HeapFree(GetProcessHeap(), 0, (*ippdsdb)->hw_params);
770 return DSERR_OUTOFMEMORY;
772 (*ippdsdb)->lpVtbl = &dsdbvt;
773 (*ippdsdb)->ref = 1;
774 (*ippdsdb)->drv = This;
775 InitializeCriticalSection(&(*ippdsdb)->pcm_crst);
776 (*ippdsdb)->pcm_crst.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": ALSA_DSOUTPUT.pcm_crst");
778 /* SetFormat has to re-initialize pcm here anyway */
779 err = SetFormat(*ippdsdb, pwfx);
780 if (FAILED(err))
782 WARN("Error occurred: %08x\n", err);
783 goto err;
786 if (dwFlags & DSBCAPS_PRIMARYBUFFER)
787 This->primary = *ippdsdb;
789 *pdwcbBufferSize = (*ippdsdb)->mmap_buflen_bytes;
790 *ppbBuffer = (*ippdsdb)->mmap_buffer;
792 /* buffer is ready to go */
793 TRACE("buffer created at %p\n", *ippdsdb);
794 return err;
796 err:
797 HeapFree(GetProcessHeap(), 0, (*ippdsdb)->sw_params);
798 HeapFree(GetProcessHeap(), 0, (*ippdsdb)->hw_params);
799 HeapFree(GetProcessHeap(), 0, *ippdsdb);
800 *ippdsdb = NULL;
801 return err;
804 static HRESULT WINAPI IDsDriverImpl_DuplicateSoundBuffer(PIDSDRIVER iface,
805 PIDSDRIVERBUFFER pBuffer,
806 LPVOID *ppvObj)
808 IDsDriverImpl *This = (IDsDriverImpl *)iface;
809 FIXME("(%p,%p): stub\n",This,pBuffer);
810 return DSERR_INVALIDCALL;
813 static const IDsDriverVtbl dsdvt =
815 IDsDriverImpl_QueryInterface,
816 IDsDriverImpl_AddRef,
817 IDsDriverImpl_Release,
818 IDsDriverImpl_GetDriverDesc,
819 IDsDriverImpl_Open,
820 IDsDriverImpl_Close,
821 IDsDriverImpl_GetCaps,
822 IDsDriverImpl_CreateSoundBuffer,
823 IDsDriverImpl_DuplicateSoundBuffer
826 DWORD wodDsCreate(UINT wDevID, PIDSDRIVER* drv)
828 IDsDriverImpl** idrv = (IDsDriverImpl**)drv;
830 TRACE("driver created\n");
832 /* the HAL isn't much better than the HEL if we can't do mmap() */
833 if (!(WOutDev[wDevID].outcaps.dwSupport & WAVECAPS_DIRECTSOUND))
835 WARN("MMAP not supported for this device, falling back to waveout, should be harmless\n");
836 return MMSYSERR_NOTSUPPORTED;
839 *idrv = HeapAlloc(GetProcessHeap(),0,sizeof(IDsDriverImpl));
840 if (!*idrv)
841 return MMSYSERR_NOMEM;
842 (*idrv)->lpVtbl = &dsdvt;
843 (*idrv)->ref = 1;
845 (*idrv)->wDevID = wDevID;
846 (*idrv)->primary = NULL;
847 return MMSYSERR_NOERROR;
850 DWORD wodDsDesc(UINT wDevID, PDSDRIVERDESC desc)
852 *desc = WOutDev[wDevID].ds_desc;
853 return MMSYSERR_NOERROR;
856 #endif /* HAVE_ALSA */