2 * Sample Wine Driver for Advanced Linux Sound System (ALSA)
3 * Based on version <final> of the ALSA API
5 * Copyright 2007 - Maarten Lankhorst
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 /*======================================================================*
23 * Low level dsound input implementation *
24 *======================================================================*/
27 #include "wine/port.h"
39 #ifdef HAVE_SYS_IOCTL_H
40 # include <sys/ioctl.h>
42 #ifdef HAVE_SYS_MMAN_H
43 # include <sys/mman.h>
53 #include "wine/library.h"
54 #include "wine/unicode.h"
55 #include "wine/debug.h"
57 /* Notify timer checks every 10 ms with a resolution of 2 ms */
58 #define DS_TIME_DEL 10
61 WINE_DEFAULT_DEBUG_CHANNEL(dsalsa
);
63 typedef struct IDsCaptureDriverBufferImpl IDsCaptureDriverBufferImpl
;
65 typedef struct IDsCaptureDriverImpl
67 const IDsCaptureDriverVtbl
*lpVtbl
;
69 IDsCaptureDriverBufferImpl
* capture_buffer
;
71 } IDsCaptureDriverImpl
;
73 typedef struct IDsCaptureDriverNotifyImpl
75 const IDsDriverNotifyVtbl
*lpVtbl
;
77 IDsCaptureDriverBufferImpl
*buffer
;
78 DSBPOSITIONNOTIFY
*notifies
;
79 DWORD nrofnotifies
, playpos
;
81 } IDsCaptureDriverNotifyImpl
;
83 struct IDsCaptureDriverBufferImpl
85 const IDsCaptureDriverBufferVtbl
*lpVtbl
;
87 IDsCaptureDriverImpl
*drv
;
88 IDsCaptureDriverNotifyImpl
*notify
;
90 CRITICAL_SECTION pcm_crst
;
91 LPBYTE mmap_buffer
, presented_buffer
;
92 DWORD mmap_buflen_bytes
, play_looping
, mmap_ofs_bytes
;
95 /* Note: snd_pcm_frames_to_bytes(This->pcm, mmap_buflen_frames) != mmap_buflen_bytes */
96 /* The actual buffer may differ in size from the wanted buffer size */
99 snd_pcm_hw_params_t
*hw_params
;
100 snd_pcm_sw_params_t
*sw_params
;
101 snd_pcm_uframes_t mmap_buflen_frames
, mmap_pos
;
104 static void Capture_CheckNotify(IDsCaptureDriverNotifyImpl
*This
, DWORD from
, DWORD len
)
107 for (i
= 0; i
< This
->nrofnotifies
; ++i
) {
108 LPDSBPOSITIONNOTIFY event
= This
->notifies
+ i
;
109 DWORD offset
= event
->dwOffset
;
110 TRACE("checking %d, position %d, event = %p\n", i
, offset
, event
->hEventNotify
);
112 if (offset
== DSBPN_OFFSETSTOP
) {
114 SetEvent(event
->hEventNotify
);
115 TRACE("signalled event %p (%d)\n", event
->hEventNotify
, i
);
121 if (offset
>= from
&& offset
< (from
+ len
))
123 TRACE("signalled event %p (%d)\n", event
->hEventNotify
, i
);
124 SetEvent(event
->hEventNotify
);
129 static void CALLBACK
Capture_Notify(UINT timerID
, UINT msg
, DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
131 IDsCaptureDriverBufferImpl
*This
= (IDsCaptureDriverBufferImpl
*)dwUser
;
132 DWORD last_playpos
, playpos
;
133 PIDSCDRIVERBUFFER iface
= (PIDSCDRIVERBUFFER
)This
;
136 /* Don't deadlock since there is a critical section can be held by the timer api itself while running this code */
137 if (!TryEnterCriticalSection(&This
->pcm_crst
)) return;
139 IDsDriverBuffer_GetPosition(iface
, &playpos
, NULL
);
140 last_playpos
= This
->notify
->playpos
;
141 This
->notify
->playpos
= playpos
;
143 if (snd_pcm_state(This
->pcm
) != SND_PCM_STATE_RUNNING
|| last_playpos
== playpos
|| !This
->notify
->nrofnotifies
|| !This
->notify
->notifies
)
146 if (playpos
< last_playpos
)
148 Capture_CheckNotify(This
->notify
, last_playpos
, This
->mmap_buflen_bytes
);
150 Capture_CheckNotify(This
->notify
, 0, playpos
);
152 else Capture_CheckNotify(This
->notify
, last_playpos
, playpos
- last_playpos
);
155 LeaveCriticalSection(&This
->pcm_crst
);
159 static HRESULT WINAPI
IDsCaptureDriverNotifyImpl_QueryInterface(PIDSDRIVERNOTIFY iface
, REFIID riid
, LPVOID
*ppobj
)
161 IDsCaptureDriverNotifyImpl
*This
= (IDsCaptureDriverNotifyImpl
*)iface
;
162 TRACE("(%p,%s,%p)\n",This
,debugstr_guid(riid
),ppobj
);
164 if ( IsEqualGUID(riid
, &IID_IUnknown
) ||
165 IsEqualGUID(riid
, &IID_IDsDriverNotify
) ) {
166 IDsDriverNotify_AddRef(iface
);
171 FIXME( "Unknown IID %s\n", debugstr_guid(riid
));
174 return E_NOINTERFACE
;
177 static ULONG WINAPI
IDsCaptureDriverNotifyImpl_AddRef(PIDSDRIVERNOTIFY iface
)
179 IDsCaptureDriverNotifyImpl
*This
= (IDsCaptureDriverNotifyImpl
*)iface
;
180 ULONG refCount
= InterlockedIncrement(&This
->ref
);
182 TRACE("(%p) ref was %d\n", This
, refCount
- 1);
187 static ULONG WINAPI
IDsCaptureDriverNotifyImpl_Release(PIDSDRIVERNOTIFY iface
)
189 IDsCaptureDriverNotifyImpl
*This
= (IDsCaptureDriverNotifyImpl
*)iface
;
190 ULONG refCount
= InterlockedDecrement(&This
->ref
);
192 TRACE("(%p) ref was %d\n", This
, refCount
+ 1);
195 This
->buffer
->notify
= NULL
;
198 timeKillEvent(This
->timerID
);
199 timeEndPeriod(DS_TIME_RES
);
201 HeapFree(GetProcessHeap(), 0, This
->notifies
);
202 HeapFree(GetProcessHeap(), 0, This
);
203 TRACE("(%p) released\n", This
);
208 static HRESULT WINAPI
IDsCaptureDriverNotifyImpl_SetNotificationPositions(PIDSDRIVERNOTIFY iface
, DWORD howmuch
, LPCDSBPOSITIONNOTIFY notify
)
210 DWORD len
= howmuch
* sizeof(DSBPOSITIONNOTIFY
);
213 IDsCaptureDriverNotifyImpl
*This
= (IDsCaptureDriverNotifyImpl
*)iface
;
214 TRACE("(%p,0x%08x,%p)\n",This
,howmuch
,notify
);
217 WARN("invalid parameter\n");
218 return DSERR_INVALIDPARAM
;
221 if (TRACE_ON(dsalsa
))
222 for (i
=0;i
<howmuch
; ++i
)
223 TRACE("notify at %d to %p\n", notify
[i
].dwOffset
, notify
[i
].hEventNotify
);
226 EnterCriticalSection(&This
->buffer
->pcm_crst
);
228 /* Make an internal copy of the caller-supplied array.
229 * Replace the existing copy if one is already present. */
231 notifies
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, This
->notifies
, len
);
233 notifies
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
);
237 LeaveCriticalSection(&This
->buffer
->pcm_crst
);
239 return DSERR_OUTOFMEMORY
;
241 This
->notifies
= notifies
;
242 memcpy(This
->notifies
, notify
, len
);
243 This
->nrofnotifies
= howmuch
;
244 IDsDriverBuffer_GetPosition((PIDSCDRIVERBUFFER
)This
->buffer
, &This
->playpos
, NULL
);
248 timeBeginPeriod(DS_TIME_RES
);
249 This
->timerID
= timeSetEvent(DS_TIME_DEL
, DS_TIME_RES
, Capture_Notify
, (DWORD_PTR
)This
->buffer
, TIME_PERIODIC
| TIME_KILL_SYNCHRONOUS
);
252 LeaveCriticalSection(&This
->buffer
->pcm_crst
);
258 static const IDsDriverNotifyVtbl dscdnvt
=
260 IDsCaptureDriverNotifyImpl_QueryInterface
,
261 IDsCaptureDriverNotifyImpl_AddRef
,
262 IDsCaptureDriverNotifyImpl_Release
,
263 IDsCaptureDriverNotifyImpl_SetNotificationPositions
,
267 /** Convert the position an application sees into a position ALSA sees */
268 static snd_pcm_uframes_t
fakepos_to_realpos(const IDsCaptureDriverBufferImpl
* This
, DWORD fakepos
)
270 snd_pcm_uframes_t realpos
;
271 if (fakepos
< This
->mmap_ofs_bytes
)
272 realpos
= This
->mmap_buflen_bytes
+ fakepos
- This
->mmap_ofs_bytes
;
273 else realpos
= fakepos
- This
->mmap_ofs_bytes
;
274 return snd_pcm_bytes_to_frames(This
->pcm
, realpos
) % This
->mmap_buflen_frames
;
278 /** Convert the position ALSA sees into a position an application sees */
279 static DWORD
realpos_to_fakepos(const IDsCaptureDriverBufferImpl
* This
, snd_pcm_uframes_t realpos
)
281 DWORD realposb
= snd_pcm_frames_to_bytes(This
->pcm
, realpos
);
282 return (realposb
+ This
->mmap_ofs_bytes
) % This
->mmap_buflen_bytes
;
285 /** Raw copy data, with buffer wrap around */
286 static void CopyDataWrap(const IDsCaptureDriverBufferImpl
* This
, LPBYTE dest
, DWORD fromwhere
, DWORD copylen
, DWORD buflen
)
288 DWORD remainder
= buflen
- fromwhere
;
289 if (remainder
>= copylen
)
291 CopyMemory(dest
, This
->mmap_buffer
+ fromwhere
, copylen
);
295 CopyMemory(dest
, This
->mmap_buffer
+ fromwhere
, remainder
);
296 copylen
-= remainder
;
297 CopyMemory(dest
, This
->mmap_buffer
, copylen
);
301 /** Copy data from the mmap buffer to backbuffer, taking into account all wraparounds that may occur */
302 static void CopyData(const IDsCaptureDriverBufferImpl
* This
, snd_pcm_uframes_t fromwhere
, snd_pcm_uframes_t len
)
304 DWORD dlen
= snd_pcm_frames_to_bytes(This
->pcm
, len
) % This
->mmap_buflen_bytes
;
307 DWORD ofs
= realpos_to_fakepos(This
, fromwhere
);
308 DWORD remainder
= This
->mmap_buflen_bytes
- ofs
;
311 DWORD realbuflen
= snd_pcm_frames_to_bytes(This
->pcm
, This
->mmap_buflen_frames
);
312 DWORD realofs
= snd_pcm_frames_to_bytes(This
->pcm
, fromwhere
);
314 if (remainder
>= dlen
)
316 CopyDataWrap(This
, This
->presented_buffer
+ ofs
, realofs
, dlen
, realbuflen
);
320 CopyDataWrap(This
, This
->presented_buffer
+ ofs
, realofs
, remainder
, realbuflen
);
322 CopyDataWrap(This
, This
->presented_buffer
, (realofs
+remainder
)%realbuflen
, dlen
, realbuflen
);
326 /** Fill buffers, for starting and stopping
327 * Alsa won't start playing until everything is filled up
328 * This also updates mmap_pos
330 * Returns: Amount of periods in use so snd_pcm_avail_update
331 * doesn't have to be called up to 4x in GetPosition()
333 static snd_pcm_uframes_t
CommitAll(IDsCaptureDriverBufferImpl
*This
, DWORD forced
)
335 const snd_pcm_channel_area_t
*areas
;
336 snd_pcm_uframes_t used
;
337 const snd_pcm_uframes_t commitahead
= This
->mmap_buflen_frames
;
339 used
= This
->mmap_buflen_frames
- snd_pcm_avail_update(This
->pcm
);
340 TRACE("%p needs to commit to %lu, used: %lu\n", This
, commitahead
, used
);
341 if (used
< commitahead
&& (forced
|| This
->play_looping
))
343 snd_pcm_uframes_t done
, putin
= commitahead
- used
;
346 snd_pcm_mmap_begin(This
->pcm
, &areas
, &This
->mmap_pos
, &putin
);
347 CopyData(This
, This
->mmap_pos
, putin
);
348 done
= snd_pcm_mmap_commit(This
->pcm
, This
->mmap_pos
, putin
);
350 This
->mmap_pos
+= done
;
352 putin
= commitahead
- used
;
354 if (This
->mmap_pos
== This
->mmap_buflen_frames
&& (snd_pcm_sframes_t
)putin
> 0 && This
->play_looping
)
356 This
->mmap_ofs_bytes
+= snd_pcm_frames_to_bytes(This
->pcm
, This
->mmap_buflen_frames
);
357 This
->mmap_ofs_bytes
%= This
->mmap_buflen_bytes
;
359 snd_pcm_mmap_begin(This
->pcm
, &areas
, &This
->mmap_pos
, &putin
);
360 CopyData(This
, This
->mmap_pos
, putin
);
361 done
= snd_pcm_mmap_commit(This
->pcm
, This
->mmap_pos
, putin
);
363 This
->mmap_pos
+= done
;
370 snd_pcm_sframes_t ret
;
372 snd_pcm_uframes_t cap
= snd_pcm_bytes_to_frames(This
->pcm
, This
->mmap_buflen_bytes
);
373 pos
= realpos_to_fakepos(This
, This
->mmap_pos
);
374 if (This
->mmap_pos
+ putin
> cap
)
375 putin
= cap
- This
->mmap_pos
;
376 ret
= snd_pcm_readi(This
->pcm
, This
->presented_buffer
+ pos
, putin
);
379 WARN("Underrun occurred\n");
380 snd_pcm_prepare(This
->pcm
);
381 ret
= snd_pcm_readi(This
->pcm
, This
->presented_buffer
+ pos
, putin
);
382 snd_pcm_start(This
->pcm
);
386 WARN("Committing data: %ld / %s (%ld)\n", ret
, snd_strerror(ret
), putin
);
389 This
->mmap_pos
+= ret
;
391 /* At this point mmap_pos may be >= This->mmap_pos this is harmless
392 * realpos_to_fakepos handles it well, and below it is truncated
395 putin
= commitahead
- used
;
398 pos
= realpos_to_fakepos(This
, This
->mmap_pos
);
399 ret
= snd_pcm_readi(This
->pcm
, This
->presented_buffer
+ pos
, putin
);
402 This
->mmap_pos
+= ret
;
410 if (This
->mmap_pos
>= This
->mmap_buflen_frames
)
412 This
->mmap_ofs_bytes
+= snd_pcm_frames_to_bytes(This
->pcm
, This
->mmap_buflen_frames
);
413 This
->mmap_ofs_bytes
%= This
->mmap_buflen_bytes
;
414 This
->mmap_pos
-= This
->mmap_buflen_frames
;
420 static void CheckXRUN(IDsCaptureDriverBufferImpl
* This
)
422 snd_pcm_state_t state
= snd_pcm_state(This
->pcm
);
425 if ( state
== SND_PCM_STATE_XRUN
)
427 err
= snd_pcm_prepare(This
->pcm
);
428 CommitAll(This
, FALSE
);
429 snd_pcm_start(This
->pcm
);
430 WARN("xrun occurred\n");
432 ERR("recovery from xrun failed, prepare failed: %s\n", snd_strerror(err
));
434 else if ( state
== SND_PCM_STATE_SUSPENDED
)
436 int err
= snd_pcm_resume(This
->pcm
);
437 TRACE("recovery from suspension occurred\n");
438 if (err
< 0 && err
!= -EAGAIN
){
439 err
= snd_pcm_prepare(This
->pcm
);
441 ERR("recovery from suspend failed, prepare failed: %s\n", snd_strerror(err
));
444 else if ( state
!= SND_PCM_STATE_RUNNING
)
446 WARN("Unhandled state: %d\n", state
);
451 * Allocate the memory-mapped buffer for direct sound, and set up the
454 static int CreateMMAP(IDsCaptureDriverBufferImpl
* pdbi
)
456 snd_pcm_t
*pcm
= pdbi
->pcm
;
457 snd_pcm_format_t format
;
458 snd_pcm_uframes_t frames
, ofs
, avail
, psize
, boundary
;
459 unsigned int channels
, bits_per_sample
, bits_per_frame
;
461 const snd_pcm_channel_area_t
*areas
;
462 snd_pcm_hw_params_t
*hw_params
= pdbi
->hw_params
;
463 snd_pcm_sw_params_t
*sw_params
= pdbi
->sw_params
;
465 mmap_mode
= snd_pcm_type(pcm
);
467 if (mmap_mode
== SND_PCM_TYPE_HW
)
468 TRACE("mmap'd buffer is a direct hardware buffer.\n");
469 else if (mmap_mode
== SND_PCM_TYPE_DMIX
)
470 TRACE("mmap'd buffer is an ALSA dmix buffer\n");
472 TRACE("mmap'd buffer is an ALSA type %d buffer\n", mmap_mode
);
474 err
= snd_pcm_hw_params_get_period_size(hw_params
, &psize
, NULL
);
475 err
= snd_pcm_hw_params_get_format(hw_params
, &format
);
476 err
= snd_pcm_hw_params_get_buffer_size(hw_params
, &frames
);
477 err
= snd_pcm_hw_params_get_channels(hw_params
, &channels
);
478 bits_per_sample
= snd_pcm_format_physical_width(format
);
479 bits_per_frame
= bits_per_sample
* channels
;
481 if (TRACE_ON(dsalsa
))
482 ALSA_TraceParameters(hw_params
, NULL
, FALSE
);
484 TRACE("format=%s frames=%ld channels=%d bits_per_sample=%d bits_per_frame=%d\n",
485 snd_pcm_format_name(format
), frames
, channels
, bits_per_sample
, bits_per_frame
);
487 pdbi
->mmap_buflen_frames
= frames
;
488 snd_pcm_sw_params_current(pcm
, sw_params
);
489 snd_pcm_sw_params_set_start_threshold(pcm
, sw_params
, 0);
490 snd_pcm_sw_params_get_boundary(sw_params
, &boundary
);
491 snd_pcm_sw_params_set_stop_threshold(pcm
, sw_params
, boundary
);
492 snd_pcm_sw_params_set_silence_threshold(pcm
, sw_params
, INT_MAX
);
493 snd_pcm_sw_params_set_silence_size(pcm
, sw_params
, 0);
494 snd_pcm_sw_params_set_avail_min(pcm
, sw_params
, 0);
495 err
= snd_pcm_sw_params(pcm
, sw_params
);
497 pdbi
->mmap_ofs_bytes
= 0;
500 pdbi
->mmap_buffer
= NULL
;
502 frames
= snd_pcm_bytes_to_frames(pdbi
->pcm
, pdbi
->mmap_buflen_bytes
);
503 snd_pcm_format_set_silence(format
, pdbi
->presented_buffer
, frames
);
508 err
= snd_pcm_mmap_begin(pcm
, &areas
, &ofs
, &avail
);
511 ERR("Can't map sound device for direct access: %s/%d\n", snd_strerror(err
), err
);
512 return DSERR_GENERIC
;
514 snd_pcm_format_set_silence(format
, areas
->addr
, pdbi
->mmap_buflen_frames
);
515 pdbi
->mmap_pos
= ofs
+ snd_pcm_mmap_commit(pcm
, ofs
, 0);
516 pdbi
->mmap_buffer
= areas
->addr
;
519 TRACE("created mmap buffer of %ld frames (%d bytes) at %p\n",
520 pdbi
->mmap_buflen_frames
, pdbi
->mmap_buflen_bytes
, pdbi
->mmap_buffer
);
525 static HRESULT WINAPI
IDsCaptureDriverBufferImpl_QueryInterface(PIDSCDRIVERBUFFER iface
, REFIID riid
, LPVOID
*ppobj
)
527 IDsCaptureDriverBufferImpl
*This
= (IDsCaptureDriverBufferImpl
*)iface
;
528 if ( IsEqualGUID(riid
, &IID_IUnknown
) ||
529 IsEqualGUID(riid
, &IID_IDsCaptureDriverBuffer
) ) {
530 IDsCaptureDriverBuffer_AddRef(iface
);
535 if ( IsEqualGUID( &IID_IDsDriverNotify
, riid
) ) {
538 This
->notify
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(IDsCaptureDriverNotifyImpl
));
540 return DSERR_OUTOFMEMORY
;
541 This
->notify
->lpVtbl
= &dscdnvt
;
542 This
->notify
->buffer
= This
;
544 /* Keep a lock on IDsDriverNotify for ourself, so it is destroyed when the buffer is */
545 IDsDriverNotify_AddRef((PIDSDRIVERNOTIFY
)This
->notify
);
547 IDsDriverNotify_AddRef((PIDSDRIVERNOTIFY
)This
->notify
);
548 *ppobj
= This
->notify
;
552 if ( IsEqualGUID( &IID_IDsDriverPropertySet
, riid
) ) {
553 FIXME("Unsupported interface IID_IDsDriverPropertySet\n");
557 FIXME("(): Unknown interface %s\n", debugstr_guid(riid
));
558 return DSERR_UNSUPPORTED
;
561 static ULONG WINAPI
IDsCaptureDriverBufferImpl_AddRef(PIDSCDRIVERBUFFER iface
)
563 IDsCaptureDriverBufferImpl
*This
= (IDsCaptureDriverBufferImpl
*)iface
;
564 ULONG refCount
= InterlockedIncrement(&This
->ref
);
566 TRACE("(%p)->(ref before=%u)\n",This
, refCount
- 1);
571 static ULONG WINAPI
IDsCaptureDriverBufferImpl_Release(PIDSCDRIVERBUFFER iface
)
573 IDsCaptureDriverBufferImpl
*This
= (IDsCaptureDriverBufferImpl
*)iface
;
574 ULONG refCount
= InterlockedDecrement(&This
->ref
);
576 TRACE("(%p)->(ref before=%u)\n",This
, refCount
+ 1);
581 EnterCriticalSection(&This
->pcm_crst
);
583 IDsDriverNotify_Release((PIDSDRIVERNOTIFY
)This
->notify
);
584 TRACE("mmap buffer %p destroyed\n", This
->mmap_buffer
);
586 This
->drv
->capture_buffer
= NULL
;
587 LeaveCriticalSection(&This
->pcm_crst
);
588 This
->pcm_crst
.DebugInfo
->Spare
[0] = 0;
589 DeleteCriticalSection(&This
->pcm_crst
);
591 snd_pcm_drop(This
->pcm
);
592 snd_pcm_close(This
->pcm
);
594 HeapFree(GetProcessHeap(), 0, This
->presented_buffer
);
595 HeapFree(GetProcessHeap(), 0, This
->sw_params
);
596 HeapFree(GetProcessHeap(), 0, This
->hw_params
);
597 HeapFree(GetProcessHeap(), 0, This
);
601 static HRESULT WINAPI
IDsCaptureDriverBufferImpl_Lock(PIDSCDRIVERBUFFER iface
, LPVOID
*ppvAudio1
,LPDWORD pdwLen1
,LPVOID
*ppvAudio2
,LPDWORD pdwLen2
, DWORD dwWritePosition
,DWORD dwWriteLen
, DWORD dwFlags
)
603 IDsCaptureDriverBufferImpl
*This
= (IDsCaptureDriverBufferImpl
*)iface
;
604 TRACE("(%p,%p,%p,%p,%p,%d,%d,0x%08x)\n", This
, ppvAudio1
, pdwLen1
, ppvAudio2
, pdwLen2
, dwWritePosition
, dwWriteLen
, dwFlags
);
607 *ppvAudio1
= (LPBYTE
)This
->presented_buffer
+ dwWritePosition
;
609 if (dwWritePosition
+ dwWriteLen
<= This
->mmap_buflen_bytes
) {
611 *pdwLen1
= dwWriteLen
;
618 *pdwLen1
= This
->mmap_buflen_bytes
- dwWritePosition
;
620 *ppvAudio2
= This
->presented_buffer
;
622 *pdwLen2
= dwWriteLen
- (This
->mmap_buflen_bytes
- dwWritePosition
);
627 static HRESULT WINAPI
IDsCaptureDriverBufferImpl_Unlock(PIDSCDRIVERBUFFER iface
, LPVOID pvAudio1
,DWORD dwLen1
, LPVOID pvAudio2
,DWORD dwLen2
)
629 IDsCaptureDriverBufferImpl
*This
= (IDsCaptureDriverBufferImpl
*)iface
;
630 TRACE("(%p,%p,%d,%p,%d)\n",This
,pvAudio1
,dwLen1
,pvAudio2
,dwLen2
);
634 static HRESULT WINAPI
IDsCaptureDriverBufferImpl_SetFormat(PIDSCDRIVERBUFFER iface
, LPWAVEFORMATEX pwfx
)
636 IDsCaptureDriverBufferImpl
*This
= (IDsCaptureDriverBufferImpl
*)iface
;
637 WINE_WAVEDEV
*wwi
= &WInDev
[This
->drv
->wDevID
];
638 snd_pcm_t
*pcm
= NULL
;
639 snd_pcm_hw_params_t
*hw_params
= This
->hw_params
;
640 snd_pcm_format_t format
= -1;
641 snd_pcm_uframes_t buffer_size
;
642 DWORD rate
= pwfx
->nSamplesPerSec
;
646 TRACE("(%p, %p)\n", iface
, pwfx
);
648 switch (pwfx
->wBitsPerSample
)
650 case 8: format
= SND_PCM_FORMAT_U8
; break;
651 case 16: format
= SND_PCM_FORMAT_S16_LE
; break;
652 case 24: format
= SND_PCM_FORMAT_S24_3LE
; break;
653 case 32: format
= SND_PCM_FORMAT_S32_LE
; break;
654 default: FIXME("Unsupported bpp: %d\n", pwfx
->wBitsPerSample
); return DSERR_GENERIC
;
658 EnterCriticalSection(&This
->pcm_crst
);
660 err
= snd_pcm_open(&pcm
, wwi
->pcmname
, SND_PCM_STREAM_CAPTURE
, SND_PCM_NONBLOCK
);
664 if (errno
!= EBUSY
|| !This
->pcm
)
667 LeaveCriticalSection(&This
->pcm_crst
);
668 WARN("Cannot open sound device: %s\n", snd_strerror(err
));
669 return DSERR_GENERIC
;
671 snd_pcm_drop(This
->pcm
);
672 snd_pcm_close(This
->pcm
);
674 err
= snd_pcm_open(&pcm
, wwi
->pcmname
, SND_PCM_STREAM_CAPTURE
, SND_PCM_NONBLOCK
);
678 LeaveCriticalSection(&This
->pcm_crst
);
679 WARN("Cannot open sound device: %s\n", snd_strerror(err
));
680 return DSERR_BUFFERLOST
;
684 /* Set some defaults */
685 snd_pcm_hw_params_any(pcm
, hw_params
);
687 err
= snd_pcm_hw_params_set_channels(pcm
, hw_params
, pwfx
->nChannels
);
688 if (err
< 0) { WARN("Could not set channels to %d\n", pwfx
->nChannels
); goto err
; }
690 err
= snd_pcm_hw_params_set_format(pcm
, hw_params
, format
);
691 if (err
< 0) { WARN("Could not set format to %d bpp\n", pwfx
->wBitsPerSample
); goto err
; }
693 err
= snd_pcm_hw_params_set_rate_near(pcm
, hw_params
, &rate
, NULL
);
694 if (err
< 0) { rate
= pwfx
->nSamplesPerSec
; WARN("Could not set rate\n"); goto err
; }
696 if (!ALSA_NearMatch(rate
, pwfx
->nSamplesPerSec
))
698 WARN("Could not set sound rate to %d, but instead to %d\n", pwfx
->nSamplesPerSec
, rate
);
699 pwfx
->nSamplesPerSec
= rate
;
700 pwfx
->nAvgBytesPerSec
= rate
* pwfx
->nBlockAlign
;
701 /* Let DirectSound detect this */
704 snd_pcm_hw_params_set_periods_integer(pcm
, hw_params
);
705 buffer_size
= This
->mmap_buflen_bytes
/ pwfx
->nBlockAlign
;
706 snd_pcm_hw_params_set_buffer_size_near(pcm
, hw_params
, &buffer_size
);
708 snd_pcm_hw_params_set_period_time_near(pcm
, hw_params
, (unsigned int*)&buffer_size
, NULL
);
710 err
= snd_pcm_hw_params_set_access (pcm
, hw_params
, SND_PCM_ACCESS_MMAP_INTERLEAVED
);
713 err
= snd_pcm_hw_params_set_access (pcm
, hw_params
, SND_PCM_ACCESS_RW_INTERLEAVED
);
714 if (err
< 0) { WARN("Could not set access\n"); goto err
; }
720 err
= snd_pcm_hw_params(pcm
, hw_params
);
721 if (err
< 0) { WARN("Could not set hw parameters\n"); goto err
; }
725 snd_pcm_drop(This
->pcm
);
726 snd_pcm_close(This
->pcm
);
731 snd_pcm_prepare(This
->pcm
);
735 LeaveCriticalSection(&This
->pcm_crst
);
740 WARN("Failed to apply changes: %s\n", snd_strerror(err
));
748 snd_pcm_hw_params_current(This
->pcm
, This
->hw_params
);
751 LeaveCriticalSection(&This
->pcm_crst
);
752 return DSERR_BADFORMAT
;
755 static HRESULT WINAPI
IDsCaptureDriverBufferImpl_GetPosition(PIDSCDRIVERBUFFER iface
, LPDWORD lpdwCappos
, LPDWORD lpdwReadpos
)
757 IDsCaptureDriverBufferImpl
*This
= (IDsCaptureDriverBufferImpl
*)iface
;
758 snd_pcm_uframes_t hw_pptr
, hw_wptr
;
760 EnterCriticalSection(&This
->pcm_crst
);
764 FIXME("Bad pointer for pcm: %p\n", This
->pcm
);
765 LeaveCriticalSection(&This
->pcm_crst
);
766 return DSERR_GENERIC
;
769 if (snd_pcm_state(This
->pcm
) != SND_PCM_STATE_RUNNING
)
772 hw_pptr
= This
->mmap_pos
;
776 /* FIXME: Unused at the moment */
777 snd_pcm_uframes_t used
= CommitAll(This
, FALSE
);
779 if (This
->mmap_pos
> used
)
780 hw_pptr
= This
->mmap_pos
- used
;
782 hw_pptr
= This
->mmap_buflen_frames
- used
+ This
->mmap_pos
;
784 hw_wptr
= This
->mmap_pos
;
787 *lpdwCappos
= realpos_to_fakepos(This
, hw_pptr
);
789 *lpdwReadpos
= realpos_to_fakepos(This
, hw_wptr
);
791 LeaveCriticalSection(&This
->pcm_crst
);
793 TRACE("hw_pptr=%u, hw_wptr=%u playpos=%u(%p), writepos=%u(%p)\n", (unsigned int)hw_pptr
, (unsigned int)hw_wptr
, realpos_to_fakepos(This
, hw_pptr
), lpdwCappos
, realpos_to_fakepos(This
, hw_wptr
), lpdwReadpos
);
797 static HRESULT WINAPI
IDsCaptureDriverBufferImpl_GetStatus(PIDSCDRIVERBUFFER iface
, LPDWORD lpdwStatus
)
799 IDsCaptureDriverBufferImpl
*This
= (IDsCaptureDriverBufferImpl
*)iface
;
800 snd_pcm_state_t state
;
801 TRACE("(%p,%p)\n",iface
,lpdwStatus
);
803 state
= snd_pcm_state(This
->pcm
);
806 case SND_PCM_STATE_XRUN
:
807 case SND_PCM_STATE_SUSPENDED
:
808 case SND_PCM_STATE_RUNNING
:
809 *lpdwStatus
= DSCBSTATUS_CAPTURING
| (This
->play_looping
? DSCBSTATUS_LOOPING
: 0);
816 TRACE("State: %d, flags: 0x%08x\n", state
, *lpdwStatus
);
820 static HRESULT WINAPI
IDsCaptureDriverBufferImpl_Start(PIDSCDRIVERBUFFER iface
, DWORD dwFlags
)
822 IDsCaptureDriverBufferImpl
*This
= (IDsCaptureDriverBufferImpl
*)iface
;
823 TRACE("(%p,%x)\n",iface
,dwFlags
);
826 EnterCriticalSection(&This
->pcm_crst
);
827 snd_pcm_start(This
->pcm
);
828 This
->play_looping
= !!(dwFlags
& DSCBSTART_LOOPING
);
829 if (!This
->play_looping
)
830 /* Not well supported because of the difference in ALSA size and DSOUND's notion of size
831 * what it does right now is fill the buffer once.. ALSA size */
832 FIXME("Non-looping buffers are not properly supported!\n");
833 CommitAll(This
, TRUE
);
835 if (This
->notify
&& This
->notify
->nrofnotifies
&& This
->notify
->notifies
)
837 DWORD playpos
= realpos_to_fakepos(This
, This
->mmap_pos
);
839 Capture_CheckNotify(This
->notify
, 0, playpos
);
840 This
->notify
->playpos
= playpos
;
844 LeaveCriticalSection(&This
->pcm_crst
);
848 static HRESULT WINAPI
IDsCaptureDriverBufferImpl_Stop(PIDSCDRIVERBUFFER iface
)
850 IDsCaptureDriverBufferImpl
*This
= (IDsCaptureDriverBufferImpl
*)iface
;
851 TRACE("(%p)\n",iface
);
854 EnterCriticalSection(&This
->pcm_crst
);
855 This
->play_looping
= FALSE
;
856 snd_pcm_drop(This
->pcm
);
857 snd_pcm_prepare(This
->pcm
);
859 if (This
->notify
&& This
->notify
->notifies
&& This
->notify
->nrofnotifies
)
860 Capture_CheckNotify(This
->notify
, 0, 0);
863 LeaveCriticalSection(&This
->pcm_crst
);
867 static const IDsCaptureDriverBufferVtbl dsdbvt
=
869 IDsCaptureDriverBufferImpl_QueryInterface
,
870 IDsCaptureDriverBufferImpl_AddRef
,
871 IDsCaptureDriverBufferImpl_Release
,
872 IDsCaptureDriverBufferImpl_Lock
,
873 IDsCaptureDriverBufferImpl_Unlock
,
874 IDsCaptureDriverBufferImpl_SetFormat
,
875 IDsCaptureDriverBufferImpl_GetPosition
,
876 IDsCaptureDriverBufferImpl_GetStatus
,
877 IDsCaptureDriverBufferImpl_Start
,
878 IDsCaptureDriverBufferImpl_Stop
881 static HRESULT WINAPI
IDsCaptureDriverImpl_QueryInterface(PIDSCDRIVER iface
, REFIID riid
, LPVOID
*ppobj
)
883 /* IDsCaptureDriverImpl *This = (IDsCaptureDriverImpl *)iface; */
884 FIXME("(%p): stub!\n",iface
);
885 return DSERR_UNSUPPORTED
;
888 static ULONG WINAPI
IDsCaptureDriverImpl_AddRef(PIDSCDRIVER iface
)
890 IDsCaptureDriverImpl
*This
= (IDsCaptureDriverImpl
*)iface
;
891 ULONG refCount
= InterlockedIncrement(&This
->ref
);
893 TRACE("(%p)->(ref before=%u)\n",This
, refCount
- 1);
898 static ULONG WINAPI
IDsCaptureDriverImpl_Release(PIDSCDRIVER iface
)
900 IDsCaptureDriverImpl
*This
= (IDsCaptureDriverImpl
*)iface
;
901 ULONG refCount
= InterlockedDecrement(&This
->ref
);
903 TRACE("(%p)->(ref before=%u)\n",This
, refCount
+ 1);
908 HeapFree(GetProcessHeap(), 0, This
);
912 static HRESULT WINAPI
IDsCaptureDriverImpl_GetDriverDesc(PIDSCDRIVER iface
, PDSDRIVERDESC pDesc
)
914 IDsCaptureDriverImpl
*This
= (IDsCaptureDriverImpl
*)iface
;
915 TRACE("(%p,%p)\n",iface
,pDesc
);
916 *pDesc
= WInDev
[This
->wDevID
].ds_desc
;
918 pDesc
->dnDevNode
= WInDev
[This
->wDevID
].waveDesc
.dnDevNode
;
920 pDesc
->wReserved
= 0;
921 pDesc
->ulDeviceNum
= This
->wDevID
;
922 pDesc
->dwHeapType
= DSDHEAP_NOHEAP
;
923 pDesc
->pvDirectDrawHeap
= NULL
;
924 pDesc
->dwMemStartAddress
= 0xDEAD0000;
925 pDesc
->dwMemEndAddress
= 0xDEAF0000;
926 pDesc
->dwMemAllocExtra
= 0;
927 pDesc
->pvReserved1
= NULL
;
928 pDesc
->pvReserved2
= NULL
;
932 static HRESULT WINAPI
IDsCaptureDriverImpl_Open(PIDSCDRIVER iface
)
935 IDsCaptureDriverImpl
*This
= (IDsCaptureDriverImpl
*)iface
;
937 snd_pcm_t
*pcm
= NULL
;
938 snd_pcm_hw_params_t
*hw_params
;
940 /* While this is not really needed, it is a good idea to do this,
941 * to see if sound can be initialized */
943 hw_params
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, snd_pcm_hw_params_sizeof());
946 hr
= DSERR_OUTOFMEMORY
;
947 WARN("--> %08x\n", hr
);
951 err
= snd_pcm_open(&pcm
, WInDev
[This
->wDevID
].pcmname
, SND_PCM_STREAM_CAPTURE
, SND_PCM_NONBLOCK
);
952 if (err
< 0) goto err
;
953 err
= snd_pcm_hw_params_any(pcm
, hw_params
);
954 if (err
< 0) goto err
;
955 err
= snd_pcm_hw_params_set_access (pcm
, hw_params
, SND_PCM_ACCESS_MMAP_INTERLEAVED
);
958 err
= snd_pcm_hw_params_set_access (pcm
, hw_params
, SND_PCM_ACCESS_RW_INTERLEAVED
);
959 if (err
< 0) goto err
;
964 HeapFree(GetProcessHeap(), 0, hw_params
);
969 WARN("Failed to open device: %s\n", snd_strerror(err
));
972 HeapFree(GetProcessHeap(), 0, hw_params
);
973 WARN("--> %08x\n", hr
);
977 static HRESULT WINAPI
IDsCaptureDriverImpl_Close(PIDSCDRIVER iface
)
979 IDsCaptureDriverImpl
*This
= (IDsCaptureDriverImpl
*)iface
;
980 TRACE("(%p) stub, harmless\n",This
);
984 static HRESULT WINAPI
IDsCaptureDriverImpl_GetCaps(PIDSCDRIVER iface
, PDSCDRIVERCAPS pCaps
)
986 IDsCaptureDriverImpl
*This
= (IDsCaptureDriverImpl
*)iface
;
987 WINE_WAVEDEV
*wwi
= &WInDev
[This
->wDevID
];
988 TRACE("(%p,%p)\n",iface
,pCaps
);
989 pCaps
->dwSize
= sizeof(DSCDRIVERCAPS
);
990 pCaps
->dwFlags
= wwi
->ds_caps
.dwFlags
;
991 pCaps
->dwFormats
= wwi
->incaps
.dwFormats
;
992 pCaps
->dwChannels
= wwi
->incaps
.wChannels
;
996 static HRESULT WINAPI
IDsCaptureDriverImpl_CreateCaptureBuffer(PIDSCDRIVER iface
,
998 DWORD dwFlags
, DWORD dwCardAddress
,
999 LPDWORD pdwcbBufferSize
,
1003 IDsCaptureDriverImpl
*This
= (IDsCaptureDriverImpl
*)iface
;
1004 IDsCaptureDriverBufferImpl
** ippdsdb
= (IDsCaptureDriverBufferImpl
**)ppvObj
;
1007 TRACE("(%p,%p,%x,%x)\n",iface
,pwfx
,dwFlags
,dwCardAddress
);
1009 if (This
->capture_buffer
)
1010 return DSERR_ALLOCATED
;
1012 This
->capture_buffer
= *ippdsdb
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(IDsCaptureDriverBufferImpl
));
1013 if (*ippdsdb
== NULL
)
1014 return DSERR_OUTOFMEMORY
;
1016 (*ippdsdb
)->hw_params
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, snd_pcm_hw_params_sizeof());
1017 (*ippdsdb
)->sw_params
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, snd_pcm_sw_params_sizeof());
1018 (*ippdsdb
)->presented_buffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, *pdwcbBufferSize
);
1019 if (!(*ippdsdb
)->hw_params
|| !(*ippdsdb
)->sw_params
|| !(*ippdsdb
)->presented_buffer
)
1021 HeapFree(GetProcessHeap(), 0, (*ippdsdb
)->sw_params
);
1022 HeapFree(GetProcessHeap(), 0, (*ippdsdb
)->hw_params
);
1023 HeapFree(GetProcessHeap(), 0, (*ippdsdb
)->presented_buffer
);
1024 return DSERR_OUTOFMEMORY
;
1026 (*ippdsdb
)->lpVtbl
= &dsdbvt
;
1027 (*ippdsdb
)->ref
= 1;
1028 (*ippdsdb
)->drv
= This
;
1029 (*ippdsdb
)->mmap_buflen_bytes
= *pdwcbBufferSize
;
1030 InitializeCriticalSection(&(*ippdsdb
)->pcm_crst
);
1031 (*ippdsdb
)->pcm_crst
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": ALSA_DSCAPTURE.pcm_crst");
1033 /* SetFormat initialises pcm */
1034 err
= IDsDriverBuffer_SetFormat((IDsDriverBuffer
*)*ppvObj
, pwfx
);
1037 WARN("Error occurred: %08x\n", err
);
1040 *ppbBuffer
= (*ippdsdb
)->presented_buffer
;
1042 /* buffer is ready to go */
1043 TRACE("buffer created at %p\n", *ippdsdb
);
1047 HeapFree(GetProcessHeap(), 0, (*ippdsdb
)->presented_buffer
);
1048 HeapFree(GetProcessHeap(), 0, (*ippdsdb
)->sw_params
);
1049 HeapFree(GetProcessHeap(), 0, (*ippdsdb
)->hw_params
);
1050 HeapFree(GetProcessHeap(), 0, *ippdsdb
);
1055 static const IDsCaptureDriverVtbl dscdvt
=
1057 IDsCaptureDriverImpl_QueryInterface
,
1058 IDsCaptureDriverImpl_AddRef
,
1059 IDsCaptureDriverImpl_Release
,
1060 IDsCaptureDriverImpl_GetDriverDesc
,
1061 IDsCaptureDriverImpl_Open
,
1062 IDsCaptureDriverImpl_Close
,
1063 IDsCaptureDriverImpl_GetCaps
,
1064 IDsCaptureDriverImpl_CreateCaptureBuffer
1067 /**************************************************************************
1068 * widDsCreate [internal]
1070 DWORD
widDsCreate(UINT wDevID
, PIDSCDRIVER
* drv
)
1072 IDsCaptureDriverImpl
** idrv
= (IDsCaptureDriverImpl
**)drv
;
1073 TRACE("(%d,%p)\n",wDevID
,drv
);
1075 *idrv
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(IDsCaptureDriverImpl
));
1077 return MMSYSERR_NOMEM
;
1078 (*idrv
)->lpVtbl
= &dscdvt
;
1081 (*idrv
)->wDevID
= wDevID
;
1082 return MMSYSERR_NOERROR
;
1085 /**************************************************************************
1086 * widDsDesc [internal]
1088 DWORD
widDsDesc(UINT wDevID
, PDSDRIVERDESC desc
)
1090 *desc
= WInDev
[wDevID
].ds_desc
;
1091 return MMSYSERR_NOERROR
;