Release 0.9.39.
[wine/gsoc-2012-control.git] / dlls / winealsa.drv / dsoutput.c
blob996ca453f58012f02b1d270fd3fdd99813ef0310
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(wave);
63 WINE_DECLARE_DEBUG_CHANNEL(waveloop);
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 /* IUnknown fields */
82 const IDsDriverBufferVtbl *lpVtbl;
83 LONG ref;
84 /* IDsDriverBufferImpl fields */
85 IDsDriverImpl* drv;
87 /* Sound fields */
88 snd_pcm_t *pcm;
89 snd_pcm_hw_params_t *hw_params;
90 snd_pcm_sw_params_t *sw_params;
92 LPVOID mmap_buffer;
93 DWORD mmap_buflen_bytes;
94 snd_pcm_uframes_t mmap_buflen_frames;
95 snd_pcm_channel_area_t * mmap_areas;
96 snd_pcm_uframes_t mmap_ppos; /* play position */
97 snd_pcm_uframes_t mmap_wpos; /* write position */
98 HANDLE mmap_thread;
99 ALSA_MSG_RING mmap_ring; /* sync object */
102 static void DSDB_CheckXRUN(IDsDriverBufferImpl* pdbi)
104 snd_pcm_t *pcm = pdbi->pcm;
105 snd_pcm_state_t state = snd_pcm_state(pcm);
106 snd_pcm_avail_update(pcm);
108 if ( state == SND_PCM_STATE_XRUN )
110 int err = snd_pcm_prepare(pcm);
111 WARN_(waveloop)("xrun occurred\n");
112 if ( err < 0 )
113 ERR("recovery from xrun failed, prepare failed: %s\n", snd_strerror(err));
115 else if ( state == SND_PCM_STATE_SUSPENDED )
117 int err = snd_pcm_resume(pcm);
118 TRACE_(waveloop)("recovery from suspension occurred\n");
119 if (err < 0 && err != -EAGAIN){
120 err = snd_pcm_prepare(pcm);
121 if (err < 0)
122 ERR("recovery from suspend failed, prepare failed: %s\n", snd_strerror(err));
124 } else if ( state != SND_PCM_STATE_RUNNING ) {
125 WARN_(waveloop)("Unhandled state: %d\n", state);
130 * The helper thread for DirectSound
132 * Basically it does an infinite loop until it is told to die
134 * snd_pcm_wait() is a call that polls the sound buffer and waits
135 * until there is at least 1 period free before it returns.
137 * We then commit the buffer filled by the owner of this
138 * IDSDriverBuffer */
139 static DWORD CALLBACK DBSB_MMAPLoop(LPVOID data)
141 IDsDriverBufferImpl* pdbi = (IDsDriverBufferImpl*)data;
142 IDsDriverBufferImpl* This = pdbi;
143 snd_pcm_uframes_t frames, wanted, ofs;
144 const snd_pcm_channel_area_t *areas;
145 int state = WINE_WS_STOPPED;
146 snd_pcm_state_t alsastate;
148 TRACE_(waveloop)("0x%8p\n", data);
149 TRACE("0x%8p, framelength: %lu, area: %8p\n", data, pdbi->mmap_buflen_frames, pdbi->mmap_areas);
151 do {
152 enum win_wm_message msg;
153 DWORD param;
154 HANDLE hEvent;
155 int err;
156 snd_pcm_format_t format;
158 if (state != WINE_WS_PLAYING)
160 ALSA_WaitRingMessage(&pdbi->mmap_ring, 1000000);
161 ALSA_RetrieveRingMessage(&pdbi->mmap_ring, &msg, &param, &hEvent);
163 else
164 while (!ALSA_RetrieveRingMessage(&pdbi->mmap_ring, &msg, &param, &hEvent))
166 snd_pcm_wait(This->pcm, -1);
167 DSDB_CheckXRUN(pdbi);
169 wanted = frames = pdbi->mmap_buflen_frames;
171 err = snd_pcm_mmap_begin(This->pcm, &areas, &ofs, &frames);
172 snd_pcm_mmap_commit(This->pcm, ofs, frames);
174 /* mark our current play position */
175 pdbi->mmap_ppos = ofs;
176 TRACE_(waveloop)("Updated position to %lx [%d, 0x%8p, %lu]\n", ofs, err, areas, frames);
177 /* Check to make sure we committed all we want to commit. ALSA
178 * only gives a contiguous linear region, so we need to check this
179 * in case we've reached the end of the buffer, in which case we
180 * can wrap around back to the beginning. */
181 if (frames < wanted) {
182 frames = wanted - frames;
183 snd_pcm_mmap_begin(This->pcm, &areas, &ofs, &frames);
184 snd_pcm_mmap_commit(This->pcm, ofs, frames);
186 wanted = 0;
187 snd_pcm_mmap_begin(This->pcm, &areas, &ofs, &wanted);
188 snd_pcm_mmap_commit(This->pcm, ofs, wanted);
189 pdbi->mmap_wpos = ofs;
192 switch (msg) {
193 case WINE_WM_STARTING:
194 if (state == WINE_WS_PLAYING)
195 break;
197 alsastate = snd_pcm_state(This->pcm);
198 if ( alsastate == SND_PCM_STATE_SETUP )
200 err = snd_pcm_prepare(This->pcm);
201 alsastate = snd_pcm_state(This->pcm);
204 snd_pcm_avail_update(This->pcm);
206 /* Rewind, and initialise */
207 frames = 0;
208 snd_pcm_mmap_begin(This->pcm, &areas, &ofs, &frames);
209 snd_pcm_mmap_commit(This->pcm, ofs, frames);
210 snd_pcm_rewind(This->pcm, ofs);
211 snd_pcm_hw_params_get_format(pdbi->hw_params, &format);
212 snd_pcm_format_set_silence(format, pdbi->mmap_buffer, pdbi->mmap_buflen_frames);
213 pdbi->mmap_ppos = 0;
214 snd_pcm_hw_params_get_period_size(pdbi->hw_params, &wanted, NULL);
215 pdbi->mmap_wpos = 2*wanted;
216 if ( alsastate == SND_PCM_STATE_PREPARED )
218 err = snd_pcm_start(This->pcm);
219 TRACE("Starting 0x%8p err: %d\n", This->pcm, err);
221 state = WINE_WS_PLAYING;
222 break;
224 case WINE_WM_STOPPING:
225 state = WINE_WS_STOPPED;
226 snd_pcm_drop(This->pcm);
227 break;
229 case WINE_WM_CLOSING:
230 if (This->pcm)
231 snd_pcm_drop(This->pcm);
232 pdbi->mmap_thread = NULL;
233 SetEvent(hEvent);
234 goto out;
235 default:
236 ERR("Unhandled event %s\n", ALSA_getCmdString(msg));
237 break;
239 if (hEvent != INVALID_HANDLE_VALUE)
240 SetEvent(hEvent);
241 } while (1);
242 out:
243 TRACE_(waveloop)("Destroyed MMAP thread\n");
244 TRACE("Destroyed MMAP thread\n");
245 return 0;
249 * Allocate the memory-mapped buffer for direct sound, and set up the
250 * callback.
252 static int DSDB_CreateMMAP(IDsDriverBufferImpl* pdbi)
254 snd_pcm_t *pcm = pdbi->pcm;
255 snd_pcm_format_t format;
256 snd_pcm_uframes_t frames, ofs, avail, psize;
257 unsigned int channels, bits_per_sample, bits_per_frame;
258 int err, mmap_mode;
259 snd_pcm_hw_params_t *hw_params = pdbi->hw_params;
260 snd_pcm_sw_params_t *sw_params = pdbi->sw_params;
262 mmap_mode = snd_pcm_type(pcm);
263 /* TODO: put a bullet in the line below this one */
264 ALSA_InitRingMessage(&pdbi->mmap_ring);
266 if (mmap_mode == SND_PCM_TYPE_HW)
267 TRACE("mmap'd buffer is a hardware buffer.\n");
268 else
269 TRACE("mmap'd buffer is an ALSA emulation of hardware buffer.\n");
271 err = snd_pcm_hw_params_get_period_size(hw_params, &psize, NULL);
273 snd_pcm_sw_params_current(pcm, sw_params);
274 snd_pcm_sw_params_set_start_threshold(pcm, sw_params, INT_MAX);
275 snd_pcm_sw_params_set_silence_size(pcm, sw_params, 0);
276 snd_pcm_sw_params_set_avail_min(pcm, sw_params, psize);
277 snd_pcm_sw_params_set_xfer_align(pcm, sw_params, 1);
278 snd_pcm_sw_params_set_silence_threshold(pcm, sw_params, 0);
279 snd_pcm_sw_params_set_xrun_mode(pcm, sw_params, SND_PCM_XRUN_NONE);
280 err = snd_pcm_sw_params(pcm, sw_params);
282 err = snd_pcm_hw_params_get_format(hw_params, &format);
283 err = snd_pcm_hw_params_get_buffer_size(hw_params, &frames);
284 err = snd_pcm_hw_params_get_channels(hw_params, &channels);
285 bits_per_sample = snd_pcm_format_physical_width(format);
286 bits_per_frame = bits_per_sample * channels;
288 if (TRACE_ON(wave))
289 ALSA_TraceParameters(hw_params, NULL, FALSE);
291 TRACE("format=%s frames=%ld channels=%d bits_per_sample=%d bits_per_frame=%d\n",
292 snd_pcm_format_name(format), frames, channels, bits_per_sample, bits_per_frame);
294 pdbi->mmap_buflen_frames = frames;
295 pdbi->mmap_buflen_bytes = snd_pcm_frames_to_bytes( pcm, frames );
297 avail = snd_pcm_avail_update(pcm);
298 if (avail < 0)
300 ERR("No buffer is available: %s.\n", snd_strerror(avail));
301 return DSERR_GENERIC;
303 err = snd_pcm_mmap_begin(pcm, (const snd_pcm_channel_area_t **)&pdbi->mmap_areas, &ofs, &avail);
304 if ( err < 0 )
306 ERR("Can't map sound device for direct access: %s\n", snd_strerror(err));
307 return DSERR_GENERIC;
309 avail = 0;/* We don't have any data to commit yet */
310 err = snd_pcm_mmap_commit(pcm, ofs, avail);
311 if (ofs > 0)
312 err = snd_pcm_rewind(pcm, ofs);
313 pdbi->mmap_buffer = pdbi->mmap_areas->addr;
314 pdbi->mmap_thread = NULL;
316 TRACE("created mmap buffer of %ld frames (%d bytes) at %p\n",
317 frames, pdbi->mmap_buflen_bytes, pdbi->mmap_buffer);
319 return DS_OK;
322 static HRESULT WINAPI IDsDriverBufferImpl_QueryInterface(PIDSDRIVERBUFFER iface, REFIID riid, LPVOID *ppobj)
324 /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
325 FIXME("(): stub!\n");
326 return DSERR_UNSUPPORTED;
329 static ULONG WINAPI IDsDriverBufferImpl_AddRef(PIDSDRIVERBUFFER iface)
331 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
332 ULONG refCount = InterlockedIncrement(&This->ref);
334 TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
336 return refCount;
339 static ULONG WINAPI IDsDriverBufferImpl_Release(PIDSDRIVERBUFFER iface)
341 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
342 ULONG refCount = InterlockedDecrement(&This->ref);
344 TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
346 if (refCount)
347 return refCount;
349 if (This->mmap_thread != NULL)
350 ALSA_AddRingMessage(&This->mmap_ring, WINE_WM_CLOSING, 0, 1);
352 TRACE("mmap buffer %p destroyed\n", This->mmap_buffer);
353 ALSA_DestroyRingMessage(&This->mmap_ring);
355 if (This == This->drv->primary)
356 This->drv->primary = NULL;
358 snd_pcm_close(This->pcm);
359 This->pcm = NULL;
360 HeapFree(GetProcessHeap(), 0, This->sw_params);
361 HeapFree(GetProcessHeap(), 0, This->hw_params);
362 HeapFree(GetProcessHeap(), 0, This);
363 return 0;
366 static HRESULT WINAPI IDsDriverBufferImpl_Lock(PIDSDRIVERBUFFER iface,
367 LPVOID*ppvAudio1,LPDWORD pdwLen1,
368 LPVOID*ppvAudio2,LPDWORD pdwLen2,
369 DWORD dwWritePosition,DWORD dwWriteLen,
370 DWORD dwFlags)
372 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
373 FIXME("(%p): stub\n", This);
374 return DSERR_UNSUPPORTED;
377 static HRESULT WINAPI IDsDriverBufferImpl_Unlock(PIDSDRIVERBUFFER iface,
378 LPVOID pvAudio1,DWORD dwLen1,
379 LPVOID pvAudio2,DWORD dwLen2)
381 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
382 FIXME("(%p): stub\n", This);
383 return DSERR_UNSUPPORTED;
386 static HRESULT SetFormat(IDsDriverBufferImpl *This, LPWAVEFORMATEX pwfx, LPDWORD buflen, LPBYTE *newbuf, BOOL forced)
388 snd_pcm_t *pcm = This->pcm;
389 snd_pcm_hw_params_t *hw_params = This->hw_params;
390 unsigned int buffer_time = 500000;
391 snd_pcm_format_t format = -1;
392 snd_pcm_uframes_t psize;
393 DWORD rate = pwfx->nSamplesPerSec;
394 int err=0;
396 TRACE("(%p,%p, %p, %p)\n",This,pwfx,buflen,newbuf);
398 switch (pwfx->wBitsPerSample)
400 case 8: format = SND_PCM_FORMAT_U8; break;
401 case 16: format = SND_PCM_FORMAT_S16_LE; break;
402 case 24: format = SND_PCM_FORMAT_S24_LE; break;
403 case 32: format = SND_PCM_FORMAT_S32_LE; break;
404 default: FIXME("Unsupported format: %d\n", pwfx->wBitsPerSample); return DSERR_GENERIC;
407 err = snd_pcm_open(&pcm, WOutDev[This->drv->wDevID].pcmname, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
408 if (err < 0)
410 WARN("Can not open sound device: %s\n", snd_strerror(err));
411 return DSERR_GENERIC;
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 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, forced);
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);
448 err = snd_pcm_hw_params_get_period_size(hw_params, &psize, NULL);
449 if (err >= 0)
450 TRACE("Period size is: %d\n", pwfx->cbSize);
452 if (This->pcm)
454 if (This->mmap_thread)
455 ALSA_AddRingMessage(&This->mmap_ring, WINE_WM_CLOSING, 0, 1);
456 snd_pcm_close(This->pcm);
458 This->pcm = pcm;
460 DSDB_CreateMMAP(This);
461 *newbuf = This->mmap_buffer;
462 *buflen = This->mmap_buflen_bytes;
464 return S_OK;
466 err:
467 if (err < 0)
468 WARN("Failed to apply changes: %s\n", snd_strerror(err));
470 if (This->pcm)
471 snd_pcm_hw_params_current(This->pcm, This->hw_params);
473 return DSERR_BADFORMAT;
476 static HRESULT WINAPI IDsDriverBufferImpl_SetFormat(PIDSDRIVERBUFFER iface, LPWAVEFORMATEX pwfx)
478 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
479 DWORD buflen;
480 LPBYTE newbuf;
481 HRESULT hr;
483 TRACE("(%p, %p)\n", iface, pwfx);
485 hr = SetFormat(This, pwfx, &buflen, &newbuf, TRUE);
487 if (hr == S_OK)
488 /* Buffer size / Location changed, so tell dsound to recreate */
489 return DSERR_BUFFERLOST;
490 return hr;
493 static HRESULT WINAPI IDsDriverBufferImpl_SetFrequency(PIDSDRIVERBUFFER iface, DWORD dwFreq)
495 /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
496 FIXME("(%p,%d): stub\n",iface,dwFreq);
497 return S_OK;
500 static HRESULT WINAPI IDsDriverBufferImpl_SetVolumePan(PIDSDRIVERBUFFER iface, PDSVOLUMEPAN pVolPan)
502 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
503 FIXME("(%p,%p): stub\n",This,pVolPan);
504 /* TODO: Bring volume control back */
505 return DS_OK;
508 static HRESULT WINAPI IDsDriverBufferImpl_SetPosition(PIDSDRIVERBUFFER iface, DWORD dwNewPos)
510 /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
511 FIXME("(%p,%d): stub\n",iface,dwNewPos);
512 return DSERR_UNSUPPORTED;
515 static HRESULT WINAPI IDsDriverBufferImpl_GetPosition(PIDSDRIVERBUFFER iface,
516 LPDWORD lpdwPlay, LPDWORD lpdwWrite)
518 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
519 snd_pcm_t *pcm = This->pcm;
520 snd_pcm_uframes_t hw_pptr, hw_wptr;
521 snd_pcm_state_t state;
523 if (!pcm)
525 FIXME("Bad pointer for pcm: %p\n", pcm);
526 return DSERR_GENERIC;
529 state = snd_pcm_state(pcm);
530 if (state == SND_PCM_STATE_RUNNING)
532 hw_pptr = This->mmap_ppos;
533 hw_wptr = This->mmap_wpos;
535 else
536 hw_pptr = hw_wptr = 0;
538 if (lpdwPlay)
539 *lpdwPlay = snd_pcm_frames_to_bytes(pcm, hw_pptr) % This->mmap_buflen_bytes;
540 if (lpdwWrite)
541 *lpdwWrite = snd_pcm_frames_to_bytes(pcm, hw_wptr) % This->mmap_buflen_bytes;
543 TRACE_(waveloop)("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);
544 return DS_OK;
547 static HRESULT WINAPI IDsDriverBufferImpl_Play(PIDSDRIVERBUFFER iface, DWORD dwRes1, DWORD dwRes2, DWORD dwFlags)
549 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
551 TRACE("(%p,%x,%x,%x)\n",iface,dwRes1,dwRes2,dwFlags);
553 if (This->mmap_thread == NULL)
554 This->mmap_thread = CreateThread(NULL, 0, DBSB_MMAPLoop, (LPVOID)(DWORD)This, 0, NULL);
556 if (This->mmap_thread == NULL)
557 ERR("Cannot create sound thread, don't expect any sound at all\n");
558 else
559 ALSA_AddRingMessage(&This->mmap_ring, WINE_WM_STARTING, 0, 1);
561 return DS_OK;
564 static HRESULT WINAPI IDsDriverBufferImpl_Stop(PIDSDRIVERBUFFER iface)
566 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
568 TRACE("(%p)\n",iface);
570 if (This->mmap_thread != NULL)
571 ALSA_AddRingMessage(&This->mmap_ring, WINE_WM_STOPPING, 0, 1);
573 return DS_OK;
576 static const IDsDriverBufferVtbl dsdbvt =
578 IDsDriverBufferImpl_QueryInterface,
579 IDsDriverBufferImpl_AddRef,
580 IDsDriverBufferImpl_Release,
581 IDsDriverBufferImpl_Lock,
582 IDsDriverBufferImpl_Unlock,
583 IDsDriverBufferImpl_SetFormat,
584 IDsDriverBufferImpl_SetFrequency,
585 IDsDriverBufferImpl_SetVolumePan,
586 IDsDriverBufferImpl_SetPosition,
587 IDsDriverBufferImpl_GetPosition,
588 IDsDriverBufferImpl_Play,
589 IDsDriverBufferImpl_Stop
592 static HRESULT WINAPI IDsDriverImpl_QueryInterface(PIDSDRIVER iface, REFIID riid, LPVOID *ppobj)
594 /* IDsDriverImpl *This = (IDsDriverImpl *)iface; */
595 FIXME("(%p): stub!\n",iface);
596 return DSERR_UNSUPPORTED;
599 static ULONG WINAPI IDsDriverImpl_AddRef(PIDSDRIVER iface)
601 IDsDriverImpl *This = (IDsDriverImpl *)iface;
602 ULONG refCount = InterlockedIncrement(&This->ref);
604 TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
606 return refCount;
609 static ULONG WINAPI IDsDriverImpl_Release(PIDSDRIVER iface)
611 IDsDriverImpl *This = (IDsDriverImpl *)iface;
612 ULONG refCount = InterlockedDecrement(&This->ref);
614 TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
616 if (refCount)
617 return refCount;
619 HeapFree(GetProcessHeap(), 0, This);
620 return 0;
623 static HRESULT WINAPI IDsDriverImpl_GetDriverDesc(PIDSDRIVER iface, PDSDRIVERDESC pDesc)
625 IDsDriverImpl *This = (IDsDriverImpl *)iface;
626 TRACE("(%p,%p)\n",iface,pDesc);
627 memcpy(pDesc, &(WOutDev[This->wDevID].ds_desc), sizeof(DSDRIVERDESC));
628 pDesc->dwFlags = DSDDESC_DONTNEEDPRIMARYLOCK;
629 pDesc->dnDevNode = WOutDev[This->wDevID].waveDesc.dnDevNode;
630 pDesc->wVxdId = 0;
631 pDesc->wReserved = 0;
632 pDesc->ulDeviceNum = This->wDevID;
633 pDesc->dwHeapType = DSDHEAP_NOHEAP;
634 pDesc->pvDirectDrawHeap = NULL;
635 pDesc->dwMemStartAddress = 0xDEAD0000;
636 pDesc->dwMemEndAddress = 0xDEAF0000;
637 pDesc->dwMemAllocExtra = 0;
638 pDesc->pvReserved1 = NULL;
639 pDesc->pvReserved2 = NULL;
640 return DS_OK;
643 static HRESULT WINAPI IDsDriverImpl_Open(PIDSDRIVER iface)
645 HRESULT hr = S_OK;
646 IDsDriverImpl *This = (IDsDriverImpl *)iface;
647 int err=0;
648 snd_pcm_t *pcm = NULL;
649 snd_pcm_hw_params_t *hw_params;
651 /* While this is not really needed, it is a good idea to do this,
652 * to see if sound can be initialized */
654 hw_params = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, snd_pcm_hw_params_sizeof());
656 if (!hw_params)
658 hr = DSERR_OUTOFMEMORY;
659 goto unalloc;
662 err = snd_pcm_open(&pcm, WOutDev[This->wDevID].pcmname, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
663 if (err < 0) goto err;
664 err = snd_pcm_hw_params_any(pcm, hw_params);
665 if (err < 0) goto err;
666 err = snd_pcm_hw_params_set_access (pcm, hw_params, SND_PCM_ACCESS_MMAP_INTERLEAVED);
667 if (err < 0) goto err;
669 TRACE("Success\n");
670 snd_pcm_close(pcm);
671 goto unalloc;
673 err:
674 hr = DSERR_GENERIC;
675 FIXME("Failed to open device: %s\n", snd_strerror(err));
676 if (pcm)
677 snd_pcm_close(pcm);
678 unalloc:
679 HeapFree(GetProcessHeap(), 0, hw_params);
680 if (hr != S_OK)
681 WARN("--> %08x\n", hr);
682 return hr;
685 static HRESULT WINAPI IDsDriverImpl_Close(PIDSDRIVER iface)
687 IDsDriverImpl *This = (IDsDriverImpl *)iface;
688 TRACE("(%p) stub, harmless\n",This);
689 return DS_OK;
692 static HRESULT WINAPI IDsDriverImpl_GetCaps(PIDSDRIVER iface, PDSDRIVERCAPS pCaps)
694 IDsDriverImpl *This = (IDsDriverImpl *)iface;
695 TRACE("(%p,%p)\n",iface,pCaps);
696 memcpy(pCaps, &(WOutDev[This->wDevID].ds_caps), sizeof(DSDRIVERCAPS));
697 return DS_OK;
700 static HRESULT WINAPI IDsDriverImpl_CreateSoundBuffer(PIDSDRIVER iface,
701 LPWAVEFORMATEX pwfx,
702 DWORD dwFlags, DWORD dwCardAddress,
703 LPDWORD pdwcbBufferSize,
704 LPBYTE *ppbBuffer,
705 LPVOID *ppvObj)
707 IDsDriverImpl *This = (IDsDriverImpl *)iface;
708 IDsDriverBufferImpl** ippdsdb = (IDsDriverBufferImpl**)ppvObj;
709 HRESULT err;
711 TRACE("(%p,%p,%x,%x)\n",iface,pwfx,dwFlags,dwCardAddress);
712 /* we only support primary buffers... for now */
713 if (!(dwFlags & DSBCAPS_PRIMARYBUFFER))
714 return DSERR_UNSUPPORTED;
715 if (This->primary)
716 return DSERR_ALLOCATED;
718 *ippdsdb = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDsDriverBufferImpl));
719 if (*ippdsdb == NULL)
720 return DSERR_OUTOFMEMORY;
722 (*ippdsdb)->hw_params = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, snd_pcm_hw_params_sizeof());
723 (*ippdsdb)->sw_params = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, snd_pcm_sw_params_sizeof());
724 if (!(*ippdsdb)->hw_params || !(*ippdsdb)->sw_params)
726 HeapFree(GetProcessHeap(), 0, (*ippdsdb)->sw_params);
727 HeapFree(GetProcessHeap(), 0, (*ippdsdb)->hw_params);
728 return DSERR_OUTOFMEMORY;
730 (*ippdsdb)->lpVtbl = &dsdbvt;
731 (*ippdsdb)->ref = 1;
732 (*ippdsdb)->drv = This;
734 /* SetFormat has to re-initialize pcm here anyway */
735 err = SetFormat(*ippdsdb, pwfx, pdwcbBufferSize, ppbBuffer, FALSE);
736 if (FAILED(err))
738 WARN("Error occured: %08x\n", err);
739 goto err;
742 if (dwFlags & DSBCAPS_PRIMARYBUFFER)
743 This->primary = *ippdsdb;
745 /* buffer is ready to go */
746 TRACE("buffer created at %p\n", *ippdsdb);
747 return err;
749 err:
750 HeapFree(GetProcessHeap(), 0, (*ippdsdb)->sw_params);
751 HeapFree(GetProcessHeap(), 0, (*ippdsdb)->hw_params);
752 HeapFree(GetProcessHeap(), 0, *ippdsdb);
753 *ippdsdb = NULL;
754 return err;
757 static HRESULT WINAPI IDsDriverImpl_DuplicateSoundBuffer(PIDSDRIVER iface,
758 PIDSDRIVERBUFFER pBuffer,
759 LPVOID *ppvObj)
761 IDsDriverImpl *This = (IDsDriverImpl *)iface;
762 FIXME("(%p,%p): stub\n",This,pBuffer);
763 return DSERR_INVALIDCALL;
766 static const IDsDriverVtbl dsdvt =
768 IDsDriverImpl_QueryInterface,
769 IDsDriverImpl_AddRef,
770 IDsDriverImpl_Release,
771 IDsDriverImpl_GetDriverDesc,
772 IDsDriverImpl_Open,
773 IDsDriverImpl_Close,
774 IDsDriverImpl_GetCaps,
775 IDsDriverImpl_CreateSoundBuffer,
776 IDsDriverImpl_DuplicateSoundBuffer
779 DWORD wodDsCreate(UINT wDevID, PIDSDRIVER* drv)
781 IDsDriverImpl** idrv = (IDsDriverImpl**)drv;
783 TRACE("driver created\n");
785 /* the HAL isn't much better than the HEL if we can't do mmap() */
786 if (!(WOutDev[wDevID].outcaps.dwSupport & WAVECAPS_DIRECTSOUND)) {
787 ERR("DirectSound flag not set\n");
788 MESSAGE("This sound card's driver does not support direct access\n");
789 MESSAGE("The (slower) DirectSound HEL mode will be used instead.\n");
790 return MMSYSERR_NOTSUPPORTED;
793 *idrv = HeapAlloc(GetProcessHeap(),0,sizeof(IDsDriverImpl));
794 if (!*idrv)
795 return MMSYSERR_NOMEM;
796 (*idrv)->lpVtbl = &dsdvt;
797 (*idrv)->ref = 1;
799 (*idrv)->wDevID = wDevID;
800 (*idrv)->primary = NULL;
801 return MMSYSERR_NOERROR;
804 DWORD wodDsDesc(UINT wDevID, PDSDRIVERDESC desc)
806 memcpy(desc, &(WOutDev[wDevID].ds_desc), sizeof(DSDRIVERDESC));
807 return MMSYSERR_NOERROR;
810 #endif /* HAVE_ALSA */