2 * Copyright 2010 Maarten Lankhorst for CodeWeavers
3 * Copyright 2011 Andrew Eikum for CodeWeavers
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #define NONAMELESSUNION
31 #include "wine/debug.h"
32 #include "wine/unicode.h"
33 #include "wine/list.h"
36 #include "mmdeviceapi.h"
40 #include "endpointvolume.h"
43 #include "audioclient.h"
44 #include "audiopolicy.h"
47 #include <alsa/asoundlib.h>
49 WINE_DEFAULT_DEBUG_CHANNEL(alsa
);
50 WINE_DECLARE_DEBUG_CHANNEL(winediag
);
52 #define NULL_PTR_ERR MAKE_HRESULT(SEVERITY_ERROR, FACILITY_WIN32, RPC_X_NULL_REF_POINTER)
54 static const REFERENCE_TIME DefaultPeriod
= 200000;
55 static const REFERENCE_TIME MinimumPeriod
= 100000;
58 typedef struct ACImpl ACImpl
;
60 typedef struct _AudioSession
{
71 CRITICAL_SECTION lock
;
76 typedef struct _AudioSessionWrapper
{
77 IAudioSessionControl2 IAudioSessionControl2_iface
;
78 IChannelAudioVolume IChannelAudioVolume_iface
;
79 ISimpleAudioVolume ISimpleAudioVolume_iface
;
84 AudioSession
*session
;
85 } AudioSessionWrapper
;
88 IAudioClient IAudioClient_iface
;
89 IAudioRenderClient IAudioRenderClient_iface
;
90 IAudioCaptureClient IAudioCaptureClient_iface
;
91 IAudioClock IAudioClock_iface
;
92 IAudioClock2 IAudioClock2_iface
;
93 IAudioStreamVolume IAudioStreamVolume_iface
;
97 snd_pcm_t
*pcm_handle
;
98 snd_pcm_uframes_t period_alsa
, bufsize_alsa
;
99 snd_pcm_hw_params_t
*hw_params
; /* does not hold state between calls */
100 snd_pcm_format_t alsa_format
;
107 AUDCLNT_SHAREMODE share
;
111 BOOL initted
, started
;
112 UINT64 written_frames
;
113 UINT32 bufsize_frames
, held_frames
, tmp_buffer_frames
, period_us
;
114 UINT32 lcl_offs_frames
; /* offs into local_buffer where valid data starts */
117 BYTE
*local_buffer
, *tmp_buffer
;
120 CRITICAL_SECTION lock
;
122 AudioSession
*session
;
123 AudioSessionWrapper
*session_wrapper
;
130 LOCKED_NORMAL
, /* public buffer piece is from local_buffer */
131 LOCKED_WRAPPED
/* public buffer piece is wrapped around, in tmp_buffer */
134 typedef struct _SessionMgr
{
135 IAudioSessionManager2 IAudioSessionManager2_iface
;
142 static HANDLE g_timer_q
;
144 static CRITICAL_SECTION g_sessions_lock
;
145 static struct list g_sessions
= LIST_INIT(g_sessions
);
147 static const WCHAR defaultW
[] = {'d','e','f','a','u','l','t',0};
148 static const char defname
[] = "default";
150 static const IAudioClientVtbl AudioClient_Vtbl
;
151 static const IAudioRenderClientVtbl AudioRenderClient_Vtbl
;
152 static const IAudioCaptureClientVtbl AudioCaptureClient_Vtbl
;
153 static const IAudioSessionControl2Vtbl AudioSessionControl2_Vtbl
;
154 static const ISimpleAudioVolumeVtbl SimpleAudioVolume_Vtbl
;
155 static const IAudioClockVtbl AudioClock_Vtbl
;
156 static const IAudioClock2Vtbl AudioClock2_Vtbl
;
157 static const IAudioStreamVolumeVtbl AudioStreamVolume_Vtbl
;
158 static const IChannelAudioVolumeVtbl ChannelAudioVolume_Vtbl
;
159 static const IAudioSessionManager2Vtbl AudioSessionManager2_Vtbl
;
161 int wine_snd_pcm_recover(snd_pcm_t
*pcm
, int err
, int silent
);
162 static AudioSessionWrapper
*AudioSessionWrapper_Create(ACImpl
*client
);
164 static inline ACImpl
*impl_from_IAudioClient(IAudioClient
*iface
)
166 return CONTAINING_RECORD(iface
, ACImpl
, IAudioClient_iface
);
169 static inline ACImpl
*impl_from_IAudioRenderClient(IAudioRenderClient
*iface
)
171 return CONTAINING_RECORD(iface
, ACImpl
, IAudioRenderClient_iface
);
174 static inline ACImpl
*impl_from_IAudioCaptureClient(IAudioCaptureClient
*iface
)
176 return CONTAINING_RECORD(iface
, ACImpl
, IAudioCaptureClient_iface
);
179 static inline AudioSessionWrapper
*impl_from_IAudioSessionControl2(IAudioSessionControl2
*iface
)
181 return CONTAINING_RECORD(iface
, AudioSessionWrapper
, IAudioSessionControl2_iface
);
184 static inline AudioSessionWrapper
*impl_from_ISimpleAudioVolume(ISimpleAudioVolume
*iface
)
186 return CONTAINING_RECORD(iface
, AudioSessionWrapper
, ISimpleAudioVolume_iface
);
189 static inline AudioSessionWrapper
*impl_from_IChannelAudioVolume(IChannelAudioVolume
*iface
)
191 return CONTAINING_RECORD(iface
, AudioSessionWrapper
, IChannelAudioVolume_iface
);
194 static inline ACImpl
*impl_from_IAudioClock(IAudioClock
*iface
)
196 return CONTAINING_RECORD(iface
, ACImpl
, IAudioClock_iface
);
199 static inline ACImpl
*impl_from_IAudioClock2(IAudioClock2
*iface
)
201 return CONTAINING_RECORD(iface
, ACImpl
, IAudioClock2_iface
);
204 static inline ACImpl
*impl_from_IAudioStreamVolume(IAudioStreamVolume
*iface
)
206 return CONTAINING_RECORD(iface
, ACImpl
, IAudioStreamVolume_iface
);
209 static inline SessionMgr
*impl_from_IAudioSessionManager2(IAudioSessionManager2
*iface
)
211 return CONTAINING_RECORD(iface
, SessionMgr
, IAudioSessionManager2_iface
);
214 BOOL WINAPI
DllMain(HINSTANCE dll
, DWORD reason
, void *reserved
)
216 if(reason
== DLL_PROCESS_ATTACH
){
217 g_timer_q
= CreateTimerQueue();
221 InitializeCriticalSection(&g_sessions_lock
);
227 /* From <dlls/mmdevapi/mmdevapi.h> */
228 enum DriverPriority
{
229 Priority_Unavailable
= 0,
235 int WINAPI
AUDDRV_GetPriority(void)
237 return Priority_Neutral
;
240 static BOOL
alsa_try_open(const char *devnode
, snd_pcm_stream_t stream
)
245 if((err
= snd_pcm_open(&handle
, devnode
, stream
, SND_PCM_NONBLOCK
)) < 0){
246 WARN("The device \"%s\" failed to open: %d (%s).\n",
247 devnode
, err
, snd_strerror(err
));
251 snd_pcm_close(handle
);
255 static HRESULT
alsa_get_card_devices(snd_pcm_stream_t stream
, WCHAR
**ids
, char **keys
,
256 UINT
*num
, snd_ctl_t
*ctl
, int card
, const WCHAR
*cardnameW
,
259 static const WCHAR dashW
[] = {' ','-',' ',0};
261 snd_pcm_info_t
*info
;
263 info
= HeapAlloc(GetProcessHeap(), 0, snd_pcm_info_sizeof());
265 return E_OUTOFMEMORY
;
267 snd_pcm_info_set_subdevice(info
, 0);
268 snd_pcm_info_set_stream(info
, stream
);
271 for(err
= snd_ctl_pcm_next_device(ctl
, &device
); device
!= -1 && err
>= 0;
272 err
= snd_ctl_pcm_next_device(ctl
, &device
)){
276 snd_pcm_info_set_device(info
, device
);
278 if((err
= snd_ctl_pcm_info(ctl
, info
)) < 0){
280 /* This device doesn't have the right stream direction */
283 WARN("Failed to get info for card %d, device %d: %d (%s)\n",
284 card
, device
, err
, snd_strerror(err
));
288 sprintf(devnode
, "plughw:%d,%d", card
, device
);
289 if(!alsa_try_open(devnode
, stream
)){
298 devname
= snd_pcm_info_get_name(info
);
300 WARN("Unable to get device name for card %d, device %d\n", card
,
305 cardlen
= lstrlenW(cardnameW
);
306 len
= MultiByteToWideChar(CP_UNIXCP
, 0, devname
, -1, NULL
, 0);
307 len
+= lstrlenW(dashW
);
309 ids
[*num
] = HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
311 HeapFree(GetProcessHeap(), 0, info
);
312 return E_OUTOFMEMORY
;
314 memcpy(ids
[*num
], cardnameW
, cardlen
* sizeof(WCHAR
));
315 memcpy(ids
[*num
] + cardlen
, dashW
, lstrlenW(dashW
) * sizeof(WCHAR
));
316 cardlen
+= lstrlenW(dashW
);
317 MultiByteToWideChar(CP_UNIXCP
, 0, devname
, -1, ids
[*num
] + cardlen
,
320 keys
[*num
] = HeapAlloc(GetProcessHeap(), 0, 32);
322 HeapFree(GetProcessHeap(), 0, info
);
323 HeapFree(GetProcessHeap(), 0, ids
[*num
]);
324 return E_OUTOFMEMORY
;
326 memcpy(keys
[*num
], devnode
, sizeof(devnode
));
332 HeapFree(GetProcessHeap(), 0, info
);
335 WARN("Got a failure during device enumeration on card %d: %d (%s)\n",
336 card
, err
, snd_strerror(err
));
341 static HRESULT
alsa_enum_devices(EDataFlow flow
, WCHAR
**ids
, char **keys
,
342 UINT
*num
, BOOL count_failed
)
344 snd_pcm_stream_t stream
= (flow
== eRender
? SND_PCM_STREAM_PLAYBACK
:
345 SND_PCM_STREAM_CAPTURE
);
351 if(alsa_try_open(defname
, stream
)){
353 *ids
= HeapAlloc(GetProcessHeap(), 0, sizeof(defaultW
));
354 memcpy(*ids
, defaultW
, sizeof(defaultW
));
355 *keys
= HeapAlloc(GetProcessHeap(), 0, sizeof(defname
));
356 memcpy(*keys
, defname
, sizeof(defname
));
359 }else if(count_failed
)
362 for(err
= snd_card_next(&card
); card
!= -1 && err
>= 0;
363 err
= snd_card_next(&card
)){
365 const char *cardname
;
370 sprintf(cardpath
, "hw:%u", card
);
372 if((err
= snd_ctl_open(&ctl
, cardpath
, 0)) < 0){
373 WARN("Unable to open ctl for ALSA device %s: %d (%s)\n", cardpath
,
374 err
, snd_strerror(err
));
378 if((err
= snd_card_get_name(card
, (char **)&cardname
)) < 0){
379 WARN("Unable to get card name for ALSA device %s: %d (%s)\n",
380 cardpath
, err
, snd_strerror(err
));
381 /* FIXME: Should be localized */
382 cardname
= "Unknown soundcard";
385 len
= MultiByteToWideChar(CP_UNIXCP
, 0, cardname
, -1, NULL
, 0);
386 cardnameW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
389 return E_OUTOFMEMORY
;
391 MultiByteToWideChar(CP_UNIXCP
, 0, cardname
, -1, cardnameW
, len
);
393 alsa_get_card_devices(stream
, ids
, keys
, num
, ctl
, card
, cardnameW
,
396 HeapFree(GetProcessHeap(), 0, cardnameW
);
402 WARN("Got a failure during card enumeration: %d (%s)\n",
403 err
, snd_strerror(err
));
408 HRESULT WINAPI
AUDDRV_GetEndpointIDs(EDataFlow flow
, WCHAR
***ids
, char ***keys
,
409 UINT
*num
, UINT
*def_index
)
413 TRACE("%d %p %p %p %p\n", flow
, ids
, keys
, num
, def_index
);
415 hr
= alsa_enum_devices(flow
, NULL
, NULL
, num
, TRUE
);
426 *ids
= HeapAlloc(GetProcessHeap(), 0, *num
* sizeof(WCHAR
*));
427 *keys
= HeapAlloc(GetProcessHeap(), 0, *num
* sizeof(char *));
429 HeapFree(GetProcessHeap(), 0, *ids
);
430 HeapFree(GetProcessHeap(), 0, *keys
);
431 return E_OUTOFMEMORY
;
436 hr
= alsa_enum_devices(flow
, *ids
, *keys
, num
, FALSE
);
439 for(i
= 0; i
< *num
; ++i
){
440 HeapFree(GetProcessHeap(), 0, (*ids
)[i
]);
441 HeapFree(GetProcessHeap(), 0, (*keys
)[i
]);
443 HeapFree(GetProcessHeap(), 0, *ids
);
444 HeapFree(GetProcessHeap(), 0, *keys
);
445 return E_OUTOFMEMORY
;
451 /* Using the pulse PCM device from alsa-plugins 1.0.24 triggers a bug
452 * which causes audio to cease playing after a few minutes of playback.
453 * Setting handle_underrun=1 on pulse-backed ALSA devices seems to work
454 * around this issue. */
455 static snd_config_t
*make_handle_underrun_config(const char *name
)
457 snd_config_t
*lconf
, *dev_node
, *hu_node
, *type_node
;
458 char dev_node_name
[64];
459 const char *type_str
;
464 if((err
= snd_config_copy(&lconf
, snd_config
)) < 0){
465 WARN("snd_config_copy failed: %d (%s)\n", err
, snd_strerror(err
));
469 sprintf(dev_node_name
, "pcm.%s", name
);
470 err
= snd_config_search(lconf
, dev_node_name
, &dev_node
);
472 snd_config_delete(lconf
);
476 snd_config_delete(lconf
);
477 WARN("snd_config_search failed: %d (%s)\n", err
, snd_strerror(err
));
481 /* ALSA is extremely fragile. If it runs into a config setting it doesn't
482 * recognize, it tends to fail or assert. So we only want to inject
483 * handle_underrun=1 on devices that we know will recognize it. */
484 err
= snd_config_search(dev_node
, "type", &type_node
);
486 snd_config_delete(lconf
);
490 snd_config_delete(lconf
);
491 WARN("snd_config_search failed: %d (%s)\n", err
, snd_strerror(err
));
495 if((err
= snd_config_get_string(type_node
, &type_str
)) < 0){
496 snd_config_delete(lconf
);
500 if(strcmp(type_str
, "pulse") != 0){
501 snd_config_delete(lconf
);
505 err
= snd_config_search(dev_node
, "handle_underrun", &hu_node
);
507 /* user already has an explicit handle_underrun setting, so don't
508 * use a local config */
509 snd_config_delete(lconf
);
513 snd_config_delete(lconf
);
514 WARN("snd_config_search failed: %d (%s)\n", err
, snd_strerror(err
));
518 if((err
= snd_config_imake_integer(&hu_node
, "handle_underrun", 1)) < 0){
519 snd_config_delete(lconf
);
520 WARN("snd_config_imake_integer failed: %d (%s)\n", err
,
525 if((err
= snd_config_add(dev_node
, hu_node
)) < 0){
526 snd_config_delete(lconf
);
527 WARN("snd_config_add failed: %d (%s)\n", err
, snd_strerror(err
));
534 HRESULT WINAPI
AUDDRV_GetAudioEndpoint(const char *key
, IMMDevice
*dev
,
535 EDataFlow dataflow
, IAudioClient
**out
)
539 snd_pcm_stream_t stream
;
541 static int handle_underrun
= 1;
543 TRACE("\"%s\" %p %d %p\n", key
, dev
, dataflow
, out
);
545 This
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(ACImpl
));
547 return E_OUTOFMEMORY
;
549 This
->IAudioClient_iface
.lpVtbl
= &AudioClient_Vtbl
;
550 This
->IAudioRenderClient_iface
.lpVtbl
= &AudioRenderClient_Vtbl
;
551 This
->IAudioCaptureClient_iface
.lpVtbl
= &AudioCaptureClient_Vtbl
;
552 This
->IAudioClock_iface
.lpVtbl
= &AudioClock_Vtbl
;
553 This
->IAudioClock2_iface
.lpVtbl
= &AudioClock2_Vtbl
;
554 This
->IAudioStreamVolume_iface
.lpVtbl
= &AudioStreamVolume_Vtbl
;
556 if(dataflow
== eRender
)
557 stream
= SND_PCM_STREAM_PLAYBACK
;
558 else if(dataflow
== eCapture
)
559 stream
= SND_PCM_STREAM_CAPTURE
;
561 HeapFree(GetProcessHeap(), 0, This
);
565 This
->dataflow
= dataflow
;
566 if(handle_underrun
&& ((lconf
= make_handle_underrun_config(key
)))){
567 err
= snd_pcm_open_lconf(&This
->pcm_handle
, key
, stream
, SND_PCM_NONBLOCK
, lconf
);
568 TRACE("Opening PCM device \"%s\" with handle_underrun: %d\n", key
, err
);
569 snd_config_delete(lconf
);
570 /* Pulse <= 2010 returns EINVAL, it does not know handle_underrun. */
572 ERR_(winediag
)("PulseAudio \"%s\" %d without handle_underrun. Audio may hang."
573 " Please upgrade to alsa_plugins >= 1.0.24\n", key
, err
);
579 err
= snd_pcm_open(&This
->pcm_handle
, key
, stream
, SND_PCM_NONBLOCK
);
582 HeapFree(GetProcessHeap(), 0, This
);
583 WARN("Unable to open PCM \"%s\": %d (%s)\n", key
, err
, snd_strerror(err
));
587 This
->hw_params
= HeapAlloc(GetProcessHeap(), 0,
588 snd_pcm_hw_params_sizeof());
589 if(!This
->hw_params
){
590 HeapFree(GetProcessHeap(), 0, This
);
591 snd_pcm_close(This
->pcm_handle
);
592 return E_OUTOFMEMORY
;
595 InitializeCriticalSection(&This
->lock
);
598 IMMDevice_AddRef(This
->parent
);
600 *out
= &This
->IAudioClient_iface
;
601 IAudioClient_AddRef(&This
->IAudioClient_iface
);
606 static HRESULT WINAPI
AudioClient_QueryInterface(IAudioClient
*iface
,
607 REFIID riid
, void **ppv
)
609 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
614 if(IsEqualIID(riid
, &IID_IUnknown
) || IsEqualIID(riid
, &IID_IAudioClient
))
617 IUnknown_AddRef((IUnknown
*)*ppv
);
620 WARN("Unknown interface %s\n", debugstr_guid(riid
));
621 return E_NOINTERFACE
;
624 static ULONG WINAPI
AudioClient_AddRef(IAudioClient
*iface
)
626 ACImpl
*This
= impl_from_IAudioClient(iface
);
628 ref
= InterlockedIncrement(&This
->ref
);
629 TRACE("(%p) Refcount now %u\n", This
, ref
);
633 static ULONG WINAPI
AudioClient_Release(IAudioClient
*iface
)
635 ACImpl
*This
= impl_from_IAudioClient(iface
);
637 ref
= InterlockedDecrement(&This
->ref
);
638 TRACE("(%p) Refcount now %u\n", This
, ref
);
640 IAudioClient_Stop(iface
);
641 IMMDevice_Release(This
->parent
);
642 DeleteCriticalSection(&This
->lock
);
643 snd_pcm_drop(This
->pcm_handle
);
644 snd_pcm_close(This
->pcm_handle
);
646 EnterCriticalSection(&g_sessions_lock
);
647 list_remove(&This
->entry
);
648 LeaveCriticalSection(&g_sessions_lock
);
650 HeapFree(GetProcessHeap(), 0, This
->vols
);
651 HeapFree(GetProcessHeap(), 0, This
->local_buffer
);
652 HeapFree(GetProcessHeap(), 0, This
->tmp_buffer
);
653 HeapFree(GetProcessHeap(), 0, This
->hw_params
);
654 CoTaskMemFree(This
->fmt
);
655 HeapFree(GetProcessHeap(), 0, This
);
660 static void dump_fmt(const WAVEFORMATEX
*fmt
)
662 TRACE("wFormatTag: 0x%x (", fmt
->wFormatTag
);
663 switch(fmt
->wFormatTag
){
664 case WAVE_FORMAT_PCM
:
665 TRACE("WAVE_FORMAT_PCM");
667 case WAVE_FORMAT_IEEE_FLOAT
:
668 TRACE("WAVE_FORMAT_IEEE_FLOAT");
670 case WAVE_FORMAT_EXTENSIBLE
:
671 TRACE("WAVE_FORMAT_EXTENSIBLE");
679 TRACE("nChannels: %u\n", fmt
->nChannels
);
680 TRACE("nSamplesPerSec: %u\n", fmt
->nSamplesPerSec
);
681 TRACE("nAvgBytesPerSec: %u\n", fmt
->nAvgBytesPerSec
);
682 TRACE("nBlockAlign: %u\n", fmt
->nBlockAlign
);
683 TRACE("wBitsPerSample: %u\n", fmt
->wBitsPerSample
);
684 TRACE("cbSize: %u\n", fmt
->cbSize
);
686 if(fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
){
687 WAVEFORMATEXTENSIBLE
*fmtex
= (void*)fmt
;
688 TRACE("dwChannelMask: %08x\n", fmtex
->dwChannelMask
);
689 TRACE("Samples: %04x\n", fmtex
->Samples
.wReserved
);
690 TRACE("SubFormat: %s\n", wine_dbgstr_guid(&fmtex
->SubFormat
));
694 static WAVEFORMATEX
*clone_format(const WAVEFORMATEX
*fmt
)
699 if(fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
)
700 size
= sizeof(WAVEFORMATEXTENSIBLE
);
702 size
= sizeof(WAVEFORMATEX
);
704 ret
= CoTaskMemAlloc(size
);
708 memcpy(ret
, fmt
, size
);
710 ret
->cbSize
= size
- sizeof(WAVEFORMATEX
);
715 static void session_init_vols(AudioSession
*session
, UINT channels
)
717 if(session
->channel_count
< channels
){
720 if(session
->channel_vols
)
721 session
->channel_vols
= HeapReAlloc(GetProcessHeap(), 0,
722 session
->channel_vols
, sizeof(float) * channels
);
724 session
->channel_vols
= HeapAlloc(GetProcessHeap(), 0,
725 sizeof(float) * channels
);
726 if(!session
->channel_vols
)
729 for(i
= session
->channel_count
; i
< channels
; ++i
)
730 session
->channel_vols
[i
] = 1.f
;
732 session
->channel_count
= channels
;
736 static AudioSession
*create_session(const GUID
*guid
, IMMDevice
*device
,
741 ret
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(AudioSession
));
745 memcpy(&ret
->guid
, guid
, sizeof(GUID
));
747 ret
->device
= device
;
749 list_init(&ret
->clients
);
751 list_add_head(&g_sessions
, &ret
->entry
);
753 InitializeCriticalSection(&ret
->lock
);
755 session_init_vols(ret
, num_channels
);
757 ret
->master_vol
= 1.f
;
762 /* if channels == 0, then this will return or create a session with
763 * matching dataflow and GUID. otherwise, channels must also match */
764 static HRESULT
get_audio_session(const GUID
*sessionguid
,
765 IMMDevice
*device
, UINT channels
, AudioSession
**out
)
767 AudioSession
*session
;
769 if(!sessionguid
|| IsEqualGUID(sessionguid
, &GUID_NULL
)){
770 *out
= create_session(&GUID_NULL
, device
, channels
);
772 return E_OUTOFMEMORY
;
778 LIST_FOR_EACH_ENTRY(session
, &g_sessions
, AudioSession
, entry
){
779 if(session
->device
== device
&&
780 IsEqualGUID(sessionguid
, &session
->guid
)){
781 session_init_vols(session
, channels
);
788 *out
= create_session(sessionguid
, device
, channels
);
790 return E_OUTOFMEMORY
;
796 static HRESULT WINAPI
AudioClient_Initialize(IAudioClient
*iface
,
797 AUDCLNT_SHAREMODE mode
, DWORD flags
, REFERENCE_TIME duration
,
798 REFERENCE_TIME period
, const WAVEFORMATEX
*fmt
,
799 const GUID
*sessionguid
)
801 ACImpl
*This
= impl_from_IAudioClient(iface
);
802 snd_pcm_sw_params_t
*sw_params
= NULL
;
803 snd_pcm_format_t format
;
804 snd_pcm_uframes_t boundary
;
805 const WAVEFORMATEXTENSIBLE
*fmtex
= (const WAVEFORMATEXTENSIBLE
*)fmt
;
806 unsigned int time_us
, rate
;
810 TRACE("(%p)->(%x, %x, %s, %s, %p, %s)\n", This
, mode
, flags
,
811 wine_dbgstr_longlong(duration
), wine_dbgstr_longlong(period
), fmt
, debugstr_guid(sessionguid
));
816 if(mode
!= AUDCLNT_SHAREMODE_SHARED
&& mode
!= AUDCLNT_SHAREMODE_EXCLUSIVE
)
817 return AUDCLNT_E_NOT_INITIALIZED
;
819 if(flags
& ~(AUDCLNT_STREAMFLAGS_CROSSPROCESS
|
820 AUDCLNT_STREAMFLAGS_LOOPBACK
|
821 AUDCLNT_STREAMFLAGS_EVENTCALLBACK
|
822 AUDCLNT_STREAMFLAGS_NOPERSIST
|
823 AUDCLNT_STREAMFLAGS_RATEADJUST
|
824 AUDCLNT_SESSIONFLAGS_EXPIREWHENUNOWNED
|
825 AUDCLNT_SESSIONFLAGS_DISPLAY_HIDE
|
826 AUDCLNT_SESSIONFLAGS_DISPLAY_HIDEWHENEXPIRED
)){
827 TRACE("Unknown flags: %08x\n", flags
);
831 EnterCriticalSection(&This
->lock
);
834 LeaveCriticalSection(&This
->lock
);
835 return AUDCLNT_E_ALREADY_INITIALIZED
;
840 if((err
= snd_pcm_hw_params_any(This
->pcm_handle
, This
->hw_params
)) < 0){
841 WARN("Unable to get hw_params: %d (%s)\n", err
, snd_strerror(err
));
846 if((err
= snd_pcm_hw_params_set_access(This
->pcm_handle
, This
->hw_params
,
847 SND_PCM_ACCESS_RW_INTERLEAVED
)) < 0){
848 WARN("Unable to set access: %d (%s)\n", err
, snd_strerror(err
));
853 if(fmt
->wFormatTag
== WAVE_FORMAT_PCM
||
854 (fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
855 IsEqualGUID(&fmtex
->SubFormat
, &KSDATAFORMAT_SUBTYPE_PCM
))){
856 if(fmt
->wBitsPerSample
== 8)
857 format
= SND_PCM_FORMAT_U8
;
858 else if(fmt
->wBitsPerSample
== 16)
859 format
= SND_PCM_FORMAT_S16_LE
;
860 else if(fmt
->wBitsPerSample
== 24)
861 format
= SND_PCM_FORMAT_S24_3LE
;
862 else if(fmt
->wBitsPerSample
== 32)
863 format
= SND_PCM_FORMAT_S32_LE
;
865 WARN("Unsupported bit depth: %u\n", fmt
->wBitsPerSample
);
866 hr
= AUDCLNT_E_UNSUPPORTED_FORMAT
;
869 }else if(fmt
->wFormatTag
== WAVE_FORMAT_IEEE_FLOAT
||
870 (fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
871 IsEqualGUID(&fmtex
->SubFormat
, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
))){
872 if(fmt
->wBitsPerSample
== 32)
873 format
= SND_PCM_FORMAT_FLOAT_LE
;
874 else if(fmt
->wBitsPerSample
== 64)
875 format
= SND_PCM_FORMAT_FLOAT64_LE
;
877 WARN("Unsupported float size: %u\n", fmt
->wBitsPerSample
);
878 hr
= AUDCLNT_E_UNSUPPORTED_FORMAT
;
882 WARN("Unknown wave format: %04x\n", fmt
->wFormatTag
);
883 hr
= AUDCLNT_E_UNSUPPORTED_FORMAT
;
887 if((err
= snd_pcm_hw_params_set_format(This
->pcm_handle
, This
->hw_params
,
889 WARN("Unable to set ALSA format to %u: %d (%s)\n", format
, err
,
895 This
->alsa_format
= format
;
897 rate
= fmt
->nSamplesPerSec
;
898 if((err
= snd_pcm_hw_params_set_rate_near(This
->pcm_handle
, This
->hw_params
,
900 WARN("Unable to set rate to %u: %d (%s)\n", rate
, err
,
906 if((err
= snd_pcm_hw_params_set_channels(This
->pcm_handle
, This
->hw_params
,
907 fmt
->nChannels
)) < 0){
908 WARN("Unable to set channels to %u: %d (%s)\n", fmt
->nChannels
, err
,
914 time_us
= MinimumPeriod
/ 10;
915 if((err
= snd_pcm_hw_params_set_period_time_near(This
->pcm_handle
,
916 This
->hw_params
, &time_us
, NULL
)) < 0){
917 WARN("Unable to set max period time to %u: %d (%s)\n", time_us
,
918 err
, snd_strerror(err
));
923 if((err
= snd_pcm_hw_params(This
->pcm_handle
, This
->hw_params
)) < 0){
924 WARN("Unable to set hw params: %d (%s)\n", err
, snd_strerror(err
));
929 sw_params
= HeapAlloc(GetProcessHeap(), 0, snd_pcm_sw_params_sizeof());
935 if((err
= snd_pcm_sw_params_current(This
->pcm_handle
, sw_params
)) < 0){
936 WARN("Unable to get sw_params: %d (%s)\n", err
, snd_strerror(err
));
942 duration
= 300000; /* 0.03s */
943 This
->bufsize_frames
= ceil((duration
/ 10000000.) * fmt
->nSamplesPerSec
);
944 This
->local_buffer
= HeapAlloc(GetProcessHeap(), 0,
945 This
->bufsize_frames
* fmt
->nBlockAlign
);
946 if(!This
->local_buffer
){
950 if (fmt
->wBitsPerSample
== 8)
951 memset(This
->local_buffer
, 128, This
->bufsize_frames
* fmt
->nBlockAlign
);
953 memset(This
->local_buffer
, 0, This
->bufsize_frames
* fmt
->nBlockAlign
);
955 if((err
= snd_pcm_sw_params_get_boundary(sw_params
, &boundary
)) < 0){
956 WARN("Unable to get boundary: %d (%s)\n", err
, snd_strerror(err
));
961 if((err
= snd_pcm_sw_params_set_start_threshold(This
->pcm_handle
,
962 sw_params
, boundary
)) < 0){
963 WARN("Unable to set start threshold to %lx: %d (%s)\n", boundary
, err
,
969 if((err
= snd_pcm_sw_params_set_stop_threshold(This
->pcm_handle
,
970 sw_params
, boundary
)) < 0){
971 WARN("Unable to set stop threshold to %lx: %d (%s)\n", boundary
, err
,
977 if((err
= snd_pcm_sw_params_set_avail_min(This
->pcm_handle
,
979 WARN("Unable to set avail min to 0: %d (%s)\n", err
, snd_strerror(err
));
984 if((err
= snd_pcm_sw_params_set_silence_size(This
->pcm_handle
,
985 sw_params
, boundary
)) < 0){
986 WARN("Unable to set silence size to %lx: %d (%s)\n", boundary
, err
,
992 if((err
= snd_pcm_sw_params(This
->pcm_handle
, sw_params
)) < 0){
993 WARN("Unable to set sw params: %d (%s)\n", err
, snd_strerror(err
));
998 if((err
= snd_pcm_prepare(This
->pcm_handle
)) < 0){
999 WARN("Unable to prepare device: %d (%s)\n", err
, snd_strerror(err
));
1004 if((err
= snd_pcm_hw_params_get_buffer_size(This
->hw_params
,
1005 &This
->bufsize_alsa
)) < 0){
1006 WARN("Unable to get buffer size: %d (%s)\n", err
, snd_strerror(err
));
1011 if((err
= snd_pcm_hw_params_get_period_size(This
->hw_params
,
1012 &This
->period_alsa
, NULL
)) < 0){
1013 WARN("Unable to get period size: %d (%s)\n", err
, snd_strerror(err
));
1018 if((err
= snd_pcm_hw_params_get_period_time(This
->hw_params
,
1019 &This
->period_us
, NULL
)) < 0){
1020 WARN("Unable to get period time: %d (%s)\n", err
, snd_strerror(err
));
1025 This
->fmt
= clone_format(fmt
);
1031 This
->vols
= HeapAlloc(GetProcessHeap(), 0, fmt
->nChannels
* sizeof(float));
1037 for(i
= 0; i
< fmt
->nChannels
; ++i
)
1038 This
->vols
[i
] = 1.f
;
1041 This
->flags
= flags
;
1043 EnterCriticalSection(&g_sessions_lock
);
1045 hr
= get_audio_session(sessionguid
, This
->parent
, fmt
->nChannels
,
1048 LeaveCriticalSection(&g_sessions_lock
);
1052 list_add_tail(&This
->session
->clients
, &This
->entry
);
1054 LeaveCriticalSection(&g_sessions_lock
);
1056 This
->initted
= TRUE
;
1059 HeapFree(GetProcessHeap(), 0, sw_params
);
1061 if(This
->local_buffer
){
1062 HeapFree(GetProcessHeap(), 0, This
->local_buffer
);
1063 This
->local_buffer
= NULL
;
1066 CoTaskMemFree(This
->fmt
);
1070 HeapFree(GetProcessHeap(), 0, This
->vols
);
1075 LeaveCriticalSection(&This
->lock
);
1080 static HRESULT WINAPI
AudioClient_GetBufferSize(IAudioClient
*iface
,
1083 ACImpl
*This
= impl_from_IAudioClient(iface
);
1085 TRACE("(%p)->(%p)\n", This
, out
);
1090 EnterCriticalSection(&This
->lock
);
1093 LeaveCriticalSection(&This
->lock
);
1094 return AUDCLNT_E_NOT_INITIALIZED
;
1097 *out
= This
->bufsize_frames
;
1099 LeaveCriticalSection(&This
->lock
);
1104 static HRESULT WINAPI
AudioClient_GetStreamLatency(IAudioClient
*iface
,
1105 REFERENCE_TIME
*latency
)
1107 ACImpl
*This
= impl_from_IAudioClient(iface
);
1109 TRACE("(%p)->(%p)\n", This
, latency
);
1114 EnterCriticalSection(&This
->lock
);
1117 LeaveCriticalSection(&This
->lock
);
1118 return AUDCLNT_E_NOT_INITIALIZED
;
1121 LeaveCriticalSection(&This
->lock
);
1128 static HRESULT WINAPI
AudioClient_GetCurrentPadding(IAudioClient
*iface
,
1131 ACImpl
*This
= impl_from_IAudioClient(iface
);
1133 TRACE("(%p)->(%p)\n", This
, out
);
1138 EnterCriticalSection(&This
->lock
);
1141 LeaveCriticalSection(&This
->lock
);
1142 return AUDCLNT_E_NOT_INITIALIZED
;
1145 if(This
->dataflow
== eRender
){
1146 snd_pcm_sframes_t avail_frames
;
1148 avail_frames
= snd_pcm_avail_update(This
->pcm_handle
);
1150 if(This
->bufsize_alsa
< avail_frames
){
1151 WARN("Xrun detected\n");
1152 *out
= This
->held_frames
;
1154 *out
= This
->bufsize_alsa
- avail_frames
+ This
->held_frames
;
1155 }else if(This
->dataflow
== eCapture
){
1156 *out
= This
->held_frames
;
1158 LeaveCriticalSection(&This
->lock
);
1159 return E_UNEXPECTED
;
1162 LeaveCriticalSection(&This
->lock
);
1167 static DWORD
get_channel_mask(unsigned int channels
)
1173 return KSAUDIO_SPEAKER_MONO
;
1175 return KSAUDIO_SPEAKER_STEREO
;
1177 return KSAUDIO_SPEAKER_STEREO
| SPEAKER_LOW_FREQUENCY
;
1179 return KSAUDIO_SPEAKER_QUAD
; /* not _SURROUND */
1181 return KSAUDIO_SPEAKER_QUAD
| SPEAKER_LOW_FREQUENCY
;
1183 return KSAUDIO_SPEAKER_5POINT1
; /* not 5POINT1_SURROUND */
1185 return KSAUDIO_SPEAKER_5POINT1
| SPEAKER_BACK_CENTER
;
1187 return KSAUDIO_SPEAKER_7POINT1
; /* not 7POINT1_SURROUND */
1189 FIXME("Unknown speaker configuration: %u\n", channels
);
1193 static HRESULT WINAPI
AudioClient_IsFormatSupported(IAudioClient
*iface
,
1194 AUDCLNT_SHAREMODE mode
, const WAVEFORMATEX
*fmt
,
1197 ACImpl
*This
= impl_from_IAudioClient(iface
);
1198 snd_pcm_format_mask_t
*formats
= NULL
;
1200 WAVEFORMATEX
*closest
= NULL
;
1201 const WAVEFORMATEXTENSIBLE
*fmtex
= (const WAVEFORMATEXTENSIBLE
*)fmt
;
1202 unsigned int max
= 0, min
= 0;
1205 TRACE("(%p)->(%x, %p, %p)\n", This
, mode
, fmt
, out
);
1207 if(!fmt
|| (mode
== AUDCLNT_SHAREMODE_SHARED
&& !out
))
1210 if(mode
!= AUDCLNT_SHAREMODE_SHARED
&& mode
!= AUDCLNT_SHAREMODE_EXCLUSIVE
)
1211 return E_INVALIDARG
;
1213 if(fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
1214 fmt
->cbSize
< sizeof(WAVEFORMATEXTENSIBLE
) - sizeof(WAVEFORMATEX
))
1215 return E_INVALIDARG
;
1222 EnterCriticalSection(&This
->lock
);
1224 if((err
= snd_pcm_hw_params_any(This
->pcm_handle
, This
->hw_params
)) < 0){
1229 formats
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1230 snd_pcm_format_mask_sizeof());
1236 snd_pcm_hw_params_get_format_mask(This
->hw_params
, formats
);
1238 if(fmt
->wFormatTag
== WAVE_FORMAT_PCM
||
1239 (fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
1240 IsEqualGUID(&fmtex
->SubFormat
, &KSDATAFORMAT_SUBTYPE_PCM
))){
1241 switch(fmt
->wBitsPerSample
){
1243 if(!snd_pcm_format_mask_test(formats
, SND_PCM_FORMAT_U8
)){
1244 hr
= AUDCLNT_E_UNSUPPORTED_FORMAT
;
1249 if(!snd_pcm_format_mask_test(formats
, SND_PCM_FORMAT_S16_LE
)){
1250 hr
= AUDCLNT_E_UNSUPPORTED_FORMAT
;
1255 if(!snd_pcm_format_mask_test(formats
, SND_PCM_FORMAT_S24_3LE
)){
1256 hr
= AUDCLNT_E_UNSUPPORTED_FORMAT
;
1261 if(!snd_pcm_format_mask_test(formats
, SND_PCM_FORMAT_S32_LE
)){
1262 hr
= AUDCLNT_E_UNSUPPORTED_FORMAT
;
1267 hr
= AUDCLNT_E_UNSUPPORTED_FORMAT
;
1270 }else if(fmt
->wFormatTag
== WAVE_FORMAT_IEEE_FLOAT
||
1271 (fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
1272 IsEqualGUID(&fmtex
->SubFormat
, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
))){
1273 switch(fmt
->wBitsPerSample
){
1275 if(!snd_pcm_format_mask_test(formats
, SND_PCM_FORMAT_FLOAT_LE
)){
1276 hr
= AUDCLNT_E_UNSUPPORTED_FORMAT
;
1281 if(!snd_pcm_format_mask_test(formats
, SND_PCM_FORMAT_FLOAT64_LE
)){
1282 hr
= AUDCLNT_E_UNSUPPORTED_FORMAT
;
1287 hr
= AUDCLNT_E_UNSUPPORTED_FORMAT
;
1291 hr
= AUDCLNT_E_UNSUPPORTED_FORMAT
;
1295 closest
= clone_format(fmt
);
1301 if((err
= snd_pcm_hw_params_get_rate_min(This
->hw_params
, &min
, NULL
)) < 0){
1303 WARN("Unable to get min rate: %d (%s)\n", err
, snd_strerror(err
));
1307 if((err
= snd_pcm_hw_params_get_rate_max(This
->hw_params
, &max
, NULL
)) < 0){
1309 WARN("Unable to get max rate: %d (%s)\n", err
, snd_strerror(err
));
1313 if(fmt
->nSamplesPerSec
< min
|| fmt
->nSamplesPerSec
> max
){
1314 hr
= AUDCLNT_E_UNSUPPORTED_FORMAT
;
1318 if((err
= snd_pcm_hw_params_get_channels_min(This
->hw_params
, &min
)) < 0){
1320 WARN("Unable to get min channels: %d (%s)\n", err
, snd_strerror(err
));
1324 if((err
= snd_pcm_hw_params_get_channels_max(This
->hw_params
, &max
)) < 0){
1326 WARN("Unable to get max channels: %d (%s)\n", err
, snd_strerror(err
));
1331 if(fmt
->nChannels
> max
){
1333 closest
->nChannels
= max
;
1334 }else if(fmt
->nChannels
< min
){
1336 closest
->nChannels
= min
;
1339 if(closest
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
){
1340 DWORD mask
= get_channel_mask(closest
->nChannels
);
1342 ((WAVEFORMATEXTENSIBLE
*)closest
)->dwChannelMask
= mask
;
1344 if(fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
1345 fmtex
->dwChannelMask
!= mask
)
1350 LeaveCriticalSection(&This
->lock
);
1351 HeapFree(GetProcessHeap(), 0, formats
);
1353 if(hr
== S_FALSE
&& out
) {
1354 closest
->nBlockAlign
=
1355 closest
->nChannels
* closest
->wBitsPerSample
/ 8;
1356 closest
->nAvgBytesPerSec
=
1357 closest
->nBlockAlign
* closest
->nSamplesPerSec
;
1360 CoTaskMemFree(closest
);
1362 TRACE("returning: %08x\n", hr
);
1366 static HRESULT WINAPI
AudioClient_GetMixFormat(IAudioClient
*iface
,
1367 WAVEFORMATEX
**pwfx
)
1369 ACImpl
*This
= impl_from_IAudioClient(iface
);
1370 WAVEFORMATEXTENSIBLE
*fmt
;
1371 snd_pcm_format_mask_t
*formats
;
1372 unsigned int max_rate
, max_channels
;
1376 TRACE("(%p)->(%p)\n", This
, pwfx
);
1382 fmt
= CoTaskMemAlloc(sizeof(WAVEFORMATEXTENSIBLE
));
1384 return E_OUTOFMEMORY
;
1386 formats
= HeapAlloc(GetProcessHeap(), 0, snd_pcm_format_mask_sizeof());
1389 return E_OUTOFMEMORY
;
1392 EnterCriticalSection(&This
->lock
);
1394 if((err
= snd_pcm_hw_params_any(This
->pcm_handle
, This
->hw_params
)) < 0){
1395 WARN("Unable to get hw_params: %d (%s)\n", err
, snd_strerror(err
));
1400 snd_pcm_hw_params_get_format_mask(This
->hw_params
, formats
);
1402 fmt
->Format
.wFormatTag
= WAVE_FORMAT_EXTENSIBLE
;
1403 if(snd_pcm_format_mask_test(formats
, SND_PCM_FORMAT_FLOAT_LE
)){
1404 fmt
->Format
.wBitsPerSample
= 32;
1405 fmt
->SubFormat
= KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
;
1406 }else if(snd_pcm_format_mask_test(formats
, SND_PCM_FORMAT_S16_LE
)){
1407 fmt
->Format
.wBitsPerSample
= 16;
1408 fmt
->SubFormat
= KSDATAFORMAT_SUBTYPE_PCM
;
1409 }else if(snd_pcm_format_mask_test(formats
, SND_PCM_FORMAT_U8
)){
1410 fmt
->Format
.wBitsPerSample
= 8;
1411 fmt
->SubFormat
= KSDATAFORMAT_SUBTYPE_PCM
;
1412 }else if(snd_pcm_format_mask_test(formats
, SND_PCM_FORMAT_S32_LE
)){
1413 fmt
->Format
.wBitsPerSample
= 32;
1414 fmt
->SubFormat
= KSDATAFORMAT_SUBTYPE_PCM
;
1415 }else if(snd_pcm_format_mask_test(formats
, SND_PCM_FORMAT_S24_3LE
)){
1416 fmt
->Format
.wBitsPerSample
= 24;
1417 fmt
->SubFormat
= KSDATAFORMAT_SUBTYPE_PCM
;
1419 ERR("Didn't recognize any available ALSA formats\n");
1424 if((err
= snd_pcm_hw_params_get_channels_max(This
->hw_params
,
1425 &max_channels
)) < 0){
1426 WARN("Unable to get max channels: %d (%s)\n", err
, snd_strerror(err
));
1431 if(max_channels
> 2){
1432 FIXME("Don't know what to do with %u channels, pretending there's "
1433 "only 2 channels\n", max_channels
);
1434 fmt
->Format
.nChannels
= 2;
1436 fmt
->Format
.nChannels
= max_channels
;
1438 fmt
->dwChannelMask
= get_channel_mask(fmt
->Format
.nChannels
);
1440 if((err
= snd_pcm_hw_params_get_rate_max(This
->hw_params
, &max_rate
,
1442 WARN("Unable to get max rate: %d (%s)\n", err
, snd_strerror(err
));
1447 if(max_rate
>= 48000)
1448 fmt
->Format
.nSamplesPerSec
= 48000;
1449 else if(max_rate
>= 44100)
1450 fmt
->Format
.nSamplesPerSec
= 44100;
1451 else if(max_rate
>= 22050)
1452 fmt
->Format
.nSamplesPerSec
= 22050;
1453 else if(max_rate
>= 11025)
1454 fmt
->Format
.nSamplesPerSec
= 11025;
1455 else if(max_rate
>= 8000)
1456 fmt
->Format
.nSamplesPerSec
= 8000;
1458 ERR("Unknown max rate: %u\n", max_rate
);
1463 fmt
->Format
.nBlockAlign
= (fmt
->Format
.wBitsPerSample
*
1464 fmt
->Format
.nChannels
) / 8;
1465 fmt
->Format
.nAvgBytesPerSec
= fmt
->Format
.nSamplesPerSec
*
1466 fmt
->Format
.nBlockAlign
;
1468 fmt
->Samples
.wValidBitsPerSample
= fmt
->Format
.wBitsPerSample
;
1469 fmt
->Format
.cbSize
= sizeof(WAVEFORMATEXTENSIBLE
) - sizeof(WAVEFORMATEX
);
1471 dump_fmt((WAVEFORMATEX
*)fmt
);
1472 *pwfx
= (WAVEFORMATEX
*)fmt
;
1475 LeaveCriticalSection(&This
->lock
);
1478 HeapFree(GetProcessHeap(), 0, formats
);
1483 static HRESULT WINAPI
AudioClient_GetDevicePeriod(IAudioClient
*iface
,
1484 REFERENCE_TIME
*defperiod
, REFERENCE_TIME
*minperiod
)
1486 ACImpl
*This
= impl_from_IAudioClient(iface
);
1488 TRACE("(%p)->(%p, %p)\n", This
, defperiod
, minperiod
);
1490 if(!defperiod
&& !minperiod
)
1494 *defperiod
= DefaultPeriod
;
1496 *minperiod
= MinimumPeriod
;
1501 static snd_pcm_sframes_t
alsa_write_best_effort(snd_pcm_t
*handle
, BYTE
*buf
,
1502 snd_pcm_uframes_t frames
, ACImpl
*This
)
1504 snd_pcm_sframes_t written
;
1506 if(This
->session
->mute
){
1508 if((err
= snd_pcm_format_set_silence(This
->alsa_format
, buf
,
1509 frames
* This
->fmt
->nChannels
)) < 0)
1510 WARN("Setting buffer to silence failed: %d (%s)\n", err
,
1514 written
= snd_pcm_writei(handle
, buf
, frames
);
1518 if(written
== -EAGAIN
)
1522 WARN("writei failed, recovering: %ld (%s)\n", written
,
1523 snd_strerror(written
));
1525 ret
= wine_snd_pcm_recover(handle
, written
, 0);
1527 WARN("Could not recover: %d (%s)\n", ret
, snd_strerror(ret
));
1531 written
= snd_pcm_writei(handle
, buf
, frames
);
1537 static void alsa_write_data(ACImpl
*This
)
1539 snd_pcm_sframes_t written
;
1540 snd_pcm_uframes_t to_write
;
1542 This
->local_buffer
+ (This
->lcl_offs_frames
* This
->fmt
->nBlockAlign
);
1544 if(This
->lcl_offs_frames
+ This
->held_frames
> This
->bufsize_frames
)
1545 to_write
= This
->bufsize_frames
- This
->lcl_offs_frames
;
1547 to_write
= This
->held_frames
;
1549 written
= alsa_write_best_effort(This
->pcm_handle
, buf
, to_write
, This
);
1551 WARN("Couldn't write: %ld (%s)\n", written
, snd_strerror(written
));
1555 This
->lcl_offs_frames
+= written
;
1556 This
->lcl_offs_frames
%= This
->bufsize_frames
;
1557 This
->held_frames
-= written
;
1559 if(written
< to_write
){
1560 /* ALSA buffer probably full */
1564 if(This
->held_frames
){
1565 /* wrapped and have some data back at the start to write */
1566 written
= alsa_write_best_effort(This
->pcm_handle
, This
->local_buffer
,
1567 This
->held_frames
, This
);
1569 WARN("Couldn't write: %ld (%s)\n", written
, snd_strerror(written
));
1573 This
->lcl_offs_frames
+= written
;
1574 This
->lcl_offs_frames
%= This
->bufsize_frames
;
1575 This
->held_frames
-= written
;
1579 static void alsa_read_data(ACImpl
*This
)
1581 snd_pcm_sframes_t pos
, readable
, nread
;
1583 pos
= (This
->held_frames
+ This
->lcl_offs_frames
) % This
->bufsize_frames
;
1584 readable
= This
->bufsize_frames
- pos
;
1586 nread
= snd_pcm_readi(This
->pcm_handle
,
1587 This
->local_buffer
+ pos
* This
->fmt
->nBlockAlign
, readable
);
1591 WARN("read failed, recovering: %ld (%s)\n", nread
, snd_strerror(nread
));
1593 ret
= wine_snd_pcm_recover(This
->pcm_handle
, nread
, 0);
1595 WARN("Recover failed: %d (%s)\n", ret
, snd_strerror(ret
));
1599 nread
= snd_pcm_readi(This
->pcm_handle
,
1600 This
->local_buffer
+ pos
* This
->fmt
->nBlockAlign
, readable
);
1602 WARN("read failed: %ld (%s)\n", nread
, snd_strerror(nread
));
1607 if(This
->session
->mute
){
1609 if((err
= snd_pcm_format_set_silence(This
->alsa_format
,
1610 This
->local_buffer
+ pos
* This
->fmt
->nBlockAlign
,
1612 WARN("Setting buffer to silence failed: %d (%s)\n", err
,
1616 This
->held_frames
+= nread
;
1618 if(This
->held_frames
> This
->bufsize_frames
){
1619 WARN("Overflow of unread data\n");
1620 This
->lcl_offs_frames
+= This
->held_frames
;
1621 This
->lcl_offs_frames
%= This
->bufsize_frames
;
1622 This
->held_frames
= This
->bufsize_frames
;
1626 static void CALLBACK
alsa_push_buffer_data(void *user
, BOOLEAN timer
)
1628 ACImpl
*This
= user
;
1630 EnterCriticalSection(&This
->lock
);
1633 if(This
->dataflow
== eRender
&& This
->held_frames
)
1634 alsa_write_data(This
);
1635 else if(This
->dataflow
== eCapture
)
1636 alsa_read_data(This
);
1639 SetEvent(This
->event
);
1642 LeaveCriticalSection(&This
->lock
);
1645 static HRESULT
alsa_consider_start(ACImpl
*This
)
1647 snd_pcm_sframes_t avail
;
1650 avail
= snd_pcm_avail_update(This
->pcm_handle
);
1652 WARN("Unable to get avail_update: %ld (%s)\n", avail
,
1653 snd_strerror(avail
));
1657 if(This
->period_alsa
< This
->bufsize_alsa
- avail
){
1658 if((err
= snd_pcm_start(This
->pcm_handle
)) < 0){
1659 WARN("Start failed: %d (%s), state: %d\n", err
, snd_strerror(err
),
1660 snd_pcm_state(This
->pcm_handle
));
1670 static HRESULT WINAPI
AudioClient_Start(IAudioClient
*iface
)
1672 ACImpl
*This
= impl_from_IAudioClient(iface
);
1676 TRACE("(%p)\n", This
);
1678 EnterCriticalSection(&This
->lock
);
1681 LeaveCriticalSection(&This
->lock
);
1682 return AUDCLNT_E_NOT_INITIALIZED
;
1685 if((This
->flags
& AUDCLNT_STREAMFLAGS_EVENTCALLBACK
) && !This
->event
){
1686 LeaveCriticalSection(&This
->lock
);
1687 return AUDCLNT_E_EVENTHANDLE_NOT_SET
;
1691 LeaveCriticalSection(&This
->lock
);
1692 return AUDCLNT_E_NOT_STOPPED
;
1695 hr
= alsa_consider_start(This
);
1697 LeaveCriticalSection(&This
->lock
);
1701 period_ms
= This
->period_us
/ 1000;
1705 if(This
->dataflow
== eCapture
){
1706 /* dump any data that might be leftover in the ALSA capture buffer */
1707 snd_pcm_readi(This
->pcm_handle
, This
->local_buffer
,
1708 This
->bufsize_frames
);
1711 if(!CreateTimerQueueTimer(&This
->timer
, g_timer_q
, alsa_push_buffer_data
,
1712 This
, 0, period_ms
, WT_EXECUTEINTIMERTHREAD
)){
1713 LeaveCriticalSection(&This
->lock
);
1714 WARN("Unable to create timer: %u\n", GetLastError());
1718 This
->started
= TRUE
;
1720 LeaveCriticalSection(&This
->lock
);
1725 static HRESULT WINAPI
AudioClient_Stop(IAudioClient
*iface
)
1727 ACImpl
*This
= impl_from_IAudioClient(iface
);
1732 TRACE("(%p)\n", This
);
1734 EnterCriticalSection(&This
->lock
);
1737 LeaveCriticalSection(&This
->lock
);
1738 return AUDCLNT_E_NOT_INITIALIZED
;
1742 LeaveCriticalSection(&This
->lock
);
1746 event
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
1747 wait
= !DeleteTimerQueueTimer(g_timer_q
, This
->timer
, event
);
1749 WARN("DeleteTimerQueueTimer error %u\n", GetLastError());
1750 wait
= wait
&& GetLastError() == ERROR_IO_PENDING
;
1752 if((err
= snd_pcm_drop(This
->pcm_handle
)) < 0){
1753 LeaveCriticalSection(&This
->lock
);
1754 WARN("Drop failed: %d (%s)\n", err
, snd_strerror(err
));
1758 if((err
= snd_pcm_prepare(This
->pcm_handle
)) < 0){
1759 LeaveCriticalSection(&This
->lock
);
1760 WARN("Prepare failed: %d (%s)\n", err
, snd_strerror(err
));
1764 This
->started
= FALSE
;
1766 LeaveCriticalSection(&This
->lock
);
1769 WaitForSingleObject(event
, INFINITE
);
1775 static HRESULT WINAPI
AudioClient_Reset(IAudioClient
*iface
)
1777 ACImpl
*This
= impl_from_IAudioClient(iface
);
1779 TRACE("(%p)\n", This
);
1781 EnterCriticalSection(&This
->lock
);
1784 LeaveCriticalSection(&This
->lock
);
1785 return AUDCLNT_E_NOT_INITIALIZED
;
1789 LeaveCriticalSection(&This
->lock
);
1790 return AUDCLNT_E_NOT_STOPPED
;
1793 if(This
->buf_state
!= NOT_LOCKED
){
1794 LeaveCriticalSection(&This
->lock
);
1795 return AUDCLNT_E_BUFFER_OPERATION_PENDING
;
1798 This
->held_frames
= 0;
1799 This
->written_frames
= 0;
1800 This
->lcl_offs_frames
= 0;
1802 LeaveCriticalSection(&This
->lock
);
1807 static HRESULT WINAPI
AudioClient_SetEventHandle(IAudioClient
*iface
,
1810 ACImpl
*This
= impl_from_IAudioClient(iface
);
1812 TRACE("(%p)->(%p)\n", This
, event
);
1815 return E_INVALIDARG
;
1817 EnterCriticalSection(&This
->lock
);
1820 LeaveCriticalSection(&This
->lock
);
1821 return AUDCLNT_E_NOT_INITIALIZED
;
1824 if(!(This
->flags
& AUDCLNT_STREAMFLAGS_EVENTCALLBACK
)){
1825 LeaveCriticalSection(&This
->lock
);
1826 return AUDCLNT_E_EVENTHANDLE_NOT_EXPECTED
;
1829 This
->event
= event
;
1831 LeaveCriticalSection(&This
->lock
);
1836 static HRESULT WINAPI
AudioClient_GetService(IAudioClient
*iface
, REFIID riid
,
1839 ACImpl
*This
= impl_from_IAudioClient(iface
);
1841 TRACE("(%p)->(%s, %p)\n", This
, debugstr_guid(riid
), ppv
);
1847 EnterCriticalSection(&This
->lock
);
1850 LeaveCriticalSection(&This
->lock
);
1851 return AUDCLNT_E_NOT_INITIALIZED
;
1854 if(IsEqualIID(riid
, &IID_IAudioRenderClient
)){
1855 if(This
->dataflow
!= eRender
){
1856 LeaveCriticalSection(&This
->lock
);
1857 return AUDCLNT_E_WRONG_ENDPOINT_TYPE
;
1859 IAudioRenderClient_AddRef(&This
->IAudioRenderClient_iface
);
1860 *ppv
= &This
->IAudioRenderClient_iface
;
1861 }else if(IsEqualIID(riid
, &IID_IAudioCaptureClient
)){
1862 if(This
->dataflow
!= eCapture
){
1863 LeaveCriticalSection(&This
->lock
);
1864 return AUDCLNT_E_WRONG_ENDPOINT_TYPE
;
1866 IAudioCaptureClient_AddRef(&This
->IAudioCaptureClient_iface
);
1867 *ppv
= &This
->IAudioCaptureClient_iface
;
1868 }else if(IsEqualIID(riid
, &IID_IAudioClock
)){
1869 IAudioClock_AddRef(&This
->IAudioClock_iface
);
1870 *ppv
= &This
->IAudioClock_iface
;
1871 }else if(IsEqualIID(riid
, &IID_IAudioStreamVolume
)){
1872 IAudioStreamVolume_AddRef(&This
->IAudioStreamVolume_iface
);
1873 *ppv
= &This
->IAudioStreamVolume_iface
;
1874 }else if(IsEqualIID(riid
, &IID_IAudioSessionControl
)){
1875 if(!This
->session_wrapper
){
1876 This
->session_wrapper
= AudioSessionWrapper_Create(This
);
1877 if(!This
->session_wrapper
){
1878 LeaveCriticalSection(&This
->lock
);
1879 return E_OUTOFMEMORY
;
1882 IAudioSessionControl2_AddRef(&This
->session_wrapper
->IAudioSessionControl2_iface
);
1884 *ppv
= &This
->session_wrapper
->IAudioSessionControl2_iface
;
1885 }else if(IsEqualIID(riid
, &IID_IChannelAudioVolume
)){
1886 if(!This
->session_wrapper
){
1887 This
->session_wrapper
= AudioSessionWrapper_Create(This
);
1888 if(!This
->session_wrapper
){
1889 LeaveCriticalSection(&This
->lock
);
1890 return E_OUTOFMEMORY
;
1893 IChannelAudioVolume_AddRef(&This
->session_wrapper
->IChannelAudioVolume_iface
);
1895 *ppv
= &This
->session_wrapper
->IChannelAudioVolume_iface
;
1896 }else if(IsEqualIID(riid
, &IID_ISimpleAudioVolume
)){
1897 if(!This
->session_wrapper
){
1898 This
->session_wrapper
= AudioSessionWrapper_Create(This
);
1899 if(!This
->session_wrapper
){
1900 LeaveCriticalSection(&This
->lock
);
1901 return E_OUTOFMEMORY
;
1904 ISimpleAudioVolume_AddRef(&This
->session_wrapper
->ISimpleAudioVolume_iface
);
1906 *ppv
= &This
->session_wrapper
->ISimpleAudioVolume_iface
;
1910 LeaveCriticalSection(&This
->lock
);
1914 LeaveCriticalSection(&This
->lock
);
1916 FIXME("stub %s\n", debugstr_guid(riid
));
1917 return E_NOINTERFACE
;
1920 static const IAudioClientVtbl AudioClient_Vtbl
=
1922 AudioClient_QueryInterface
,
1924 AudioClient_Release
,
1925 AudioClient_Initialize
,
1926 AudioClient_GetBufferSize
,
1927 AudioClient_GetStreamLatency
,
1928 AudioClient_GetCurrentPadding
,
1929 AudioClient_IsFormatSupported
,
1930 AudioClient_GetMixFormat
,
1931 AudioClient_GetDevicePeriod
,
1935 AudioClient_SetEventHandle
,
1936 AudioClient_GetService
1939 static HRESULT WINAPI
AudioRenderClient_QueryInterface(
1940 IAudioRenderClient
*iface
, REFIID riid
, void **ppv
)
1942 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
1948 if(IsEqualIID(riid
, &IID_IUnknown
) ||
1949 IsEqualIID(riid
, &IID_IAudioRenderClient
))
1952 IUnknown_AddRef((IUnknown
*)*ppv
);
1956 WARN("Unknown interface %s\n", debugstr_guid(riid
));
1957 return E_NOINTERFACE
;
1960 static ULONG WINAPI
AudioRenderClient_AddRef(IAudioRenderClient
*iface
)
1962 ACImpl
*This
= impl_from_IAudioRenderClient(iface
);
1963 return AudioClient_AddRef(&This
->IAudioClient_iface
);
1966 static ULONG WINAPI
AudioRenderClient_Release(IAudioRenderClient
*iface
)
1968 ACImpl
*This
= impl_from_IAudioRenderClient(iface
);
1969 return AudioClient_Release(&This
->IAudioClient_iface
);
1972 static HRESULT WINAPI
AudioRenderClient_GetBuffer(IAudioRenderClient
*iface
,
1973 UINT32 frames
, BYTE
**data
)
1975 ACImpl
*This
= impl_from_IAudioRenderClient(iface
);
1980 TRACE("(%p)->(%u, %p)\n", This
, frames
, data
);
1985 EnterCriticalSection(&This
->lock
);
1987 if(This
->buf_state
!= NOT_LOCKED
){
1988 LeaveCriticalSection(&This
->lock
);
1989 return AUDCLNT_E_OUT_OF_ORDER
;
1993 This
->buf_state
= LOCKED_NORMAL
;
1994 LeaveCriticalSection(&This
->lock
);
1998 hr
= IAudioClient_GetCurrentPadding(&This
->IAudioClient_iface
, &pad
);
2000 LeaveCriticalSection(&This
->lock
);
2004 if(pad
+ frames
> This
->bufsize_frames
){
2005 LeaveCriticalSection(&This
->lock
);
2006 return AUDCLNT_E_BUFFER_TOO_LARGE
;
2010 (This
->lcl_offs_frames
+ This
->held_frames
) % This
->bufsize_frames
;
2011 if(write_pos
+ frames
> This
->bufsize_frames
){
2012 if(This
->tmp_buffer_frames
< frames
){
2013 if(This
->tmp_buffer
)
2014 This
->tmp_buffer
= HeapReAlloc(GetProcessHeap(), 0,
2015 This
->tmp_buffer
, frames
* This
->fmt
->nBlockAlign
);
2017 This
->tmp_buffer
= HeapAlloc(GetProcessHeap(), 0,
2018 frames
* This
->fmt
->nBlockAlign
);
2019 if(!This
->tmp_buffer
){
2020 LeaveCriticalSection(&This
->lock
);
2021 return E_OUTOFMEMORY
;
2023 This
->tmp_buffer_frames
= frames
;
2025 *data
= This
->tmp_buffer
;
2026 This
->buf_state
= LOCKED_WRAPPED
;
2028 *data
= This
->local_buffer
+ write_pos
* This
->fmt
->nBlockAlign
;
2029 This
->buf_state
= LOCKED_NORMAL
;
2032 LeaveCriticalSection(&This
->lock
);
2037 static void alsa_wrap_buffer(ACImpl
*This
, BYTE
*buffer
, UINT32 written_frames
)
2039 snd_pcm_uframes_t write_offs_frames
=
2040 (This
->lcl_offs_frames
+ This
->held_frames
) % This
->bufsize_frames
;
2041 UINT32 write_offs_bytes
= write_offs_frames
* This
->fmt
->nBlockAlign
;
2042 snd_pcm_uframes_t chunk_frames
= This
->bufsize_frames
- write_offs_frames
;
2043 UINT32 chunk_bytes
= chunk_frames
* This
->fmt
->nBlockAlign
;
2044 UINT32 written_bytes
= written_frames
* This
->fmt
->nBlockAlign
;
2046 if(written_bytes
<= chunk_bytes
){
2047 memcpy(This
->local_buffer
+ write_offs_bytes
, buffer
, written_bytes
);
2049 memcpy(This
->local_buffer
+ write_offs_bytes
, buffer
, chunk_bytes
);
2050 memcpy(This
->local_buffer
, buffer
+ chunk_bytes
,
2051 written_bytes
- chunk_bytes
);
2055 static HRESULT WINAPI
AudioRenderClient_ReleaseBuffer(
2056 IAudioRenderClient
*iface
, UINT32 written_frames
, DWORD flags
)
2058 ACImpl
*This
= impl_from_IAudioRenderClient(iface
);
2062 TRACE("(%p)->(%u, %x)\n", This
, written_frames
, flags
);
2064 EnterCriticalSection(&This
->lock
);
2066 if(This
->buf_state
== NOT_LOCKED
|| !written_frames
){
2067 This
->buf_state
= NOT_LOCKED
;
2068 LeaveCriticalSection(&This
->lock
);
2069 return written_frames
? AUDCLNT_E_OUT_OF_ORDER
: S_OK
;
2072 if(This
->buf_state
== LOCKED_NORMAL
)
2073 buffer
= This
->local_buffer
+ This
->fmt
->nBlockAlign
*
2074 ((This
->lcl_offs_frames
+ This
->held_frames
) % This
->bufsize_frames
);
2076 buffer
= This
->tmp_buffer
;
2078 if(flags
& AUDCLNT_BUFFERFLAGS_SILENT
){
2079 WAVEFORMATEXTENSIBLE
*fmtex
= (WAVEFORMATEXTENSIBLE
*)This
->fmt
;
2080 if((This
->fmt
->wFormatTag
== WAVE_FORMAT_PCM
||
2081 (This
->fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
2082 IsEqualGUID(&fmtex
->SubFormat
, &KSDATAFORMAT_SUBTYPE_PCM
))) &&
2083 This
->fmt
->wBitsPerSample
== 8)
2084 memset(buffer
, 128, written_frames
* This
->fmt
->nBlockAlign
);
2086 memset(buffer
, 0, written_frames
* This
->fmt
->nBlockAlign
);
2089 if(This
->held_frames
){
2090 if(This
->buf_state
== LOCKED_WRAPPED
)
2091 alsa_wrap_buffer(This
, buffer
, written_frames
);
2093 This
->held_frames
+= written_frames
;
2095 snd_pcm_sframes_t written
;
2097 written
= alsa_write_best_effort(This
->pcm_handle
, buffer
,
2098 written_frames
, This
);
2100 LeaveCriticalSection(&This
->lock
);
2101 WARN("write failed: %ld (%s)\n", written
, snd_strerror(written
));
2105 if(written
< written_frames
){
2106 if(This
->buf_state
== LOCKED_WRAPPED
)
2107 alsa_wrap_buffer(This
,
2108 This
->tmp_buffer
+ written
* This
->fmt
->nBlockAlign
,
2109 written_frames
- written
);
2111 This
->lcl_offs_frames
+= written
;
2112 This
->held_frames
= written_frames
- written
;
2117 snd_pcm_state(This
->pcm_handle
) == SND_PCM_STATE_PREPARED
){
2118 hr
= alsa_consider_start(This
);
2120 LeaveCriticalSection(&This
->lock
);
2125 This
->written_frames
+= written_frames
;
2126 This
->buf_state
= NOT_LOCKED
;
2128 LeaveCriticalSection(&This
->lock
);
2133 static const IAudioRenderClientVtbl AudioRenderClient_Vtbl
= {
2134 AudioRenderClient_QueryInterface
,
2135 AudioRenderClient_AddRef
,
2136 AudioRenderClient_Release
,
2137 AudioRenderClient_GetBuffer
,
2138 AudioRenderClient_ReleaseBuffer
2141 static HRESULT WINAPI
AudioCaptureClient_QueryInterface(
2142 IAudioCaptureClient
*iface
, REFIID riid
, void **ppv
)
2144 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
2150 if(IsEqualIID(riid
, &IID_IUnknown
) ||
2151 IsEqualIID(riid
, &IID_IAudioCaptureClient
))
2154 IUnknown_AddRef((IUnknown
*)*ppv
);
2158 WARN("Unknown interface %s\n", debugstr_guid(riid
));
2159 return E_NOINTERFACE
;
2162 static ULONG WINAPI
AudioCaptureClient_AddRef(IAudioCaptureClient
*iface
)
2164 ACImpl
*This
= impl_from_IAudioCaptureClient(iface
);
2165 return IAudioClient_AddRef(&This
->IAudioClient_iface
);
2168 static ULONG WINAPI
AudioCaptureClient_Release(IAudioCaptureClient
*iface
)
2170 ACImpl
*This
= impl_from_IAudioCaptureClient(iface
);
2171 return IAudioClient_Release(&This
->IAudioClient_iface
);
2174 static HRESULT WINAPI
AudioCaptureClient_GetBuffer(IAudioCaptureClient
*iface
,
2175 BYTE
**data
, UINT32
*frames
, DWORD
*flags
, UINT64
*devpos
,
2178 ACImpl
*This
= impl_from_IAudioCaptureClient(iface
);
2181 TRACE("(%p)->(%p, %p, %p, %p, %p)\n", This
, data
, frames
, flags
,
2184 if(!data
|| !frames
|| !flags
)
2187 EnterCriticalSection(&This
->lock
);
2189 if(This
->buf_state
!= NOT_LOCKED
){
2190 LeaveCriticalSection(&This
->lock
);
2191 return AUDCLNT_E_OUT_OF_ORDER
;
2194 hr
= IAudioCaptureClient_GetNextPacketSize(iface
, frames
);
2196 LeaveCriticalSection(&This
->lock
);
2202 if(This
->lcl_offs_frames
+ *frames
> This
->bufsize_frames
){
2203 UINT32 chunk_bytes
, offs_bytes
, frames_bytes
;
2204 if(This
->tmp_buffer_frames
< *frames
){
2205 if(This
->tmp_buffer
)
2206 This
->tmp_buffer
= HeapReAlloc(GetProcessHeap(), 0,
2207 This
->tmp_buffer
, *frames
* This
->fmt
->nBlockAlign
);
2209 This
->tmp_buffer
= HeapAlloc(GetProcessHeap(), 0,
2210 *frames
* This
->fmt
->nBlockAlign
);
2211 if(!This
->tmp_buffer
){
2212 LeaveCriticalSection(&This
->lock
);
2213 return E_OUTOFMEMORY
;
2215 This
->tmp_buffer_frames
= *frames
;
2218 *data
= This
->tmp_buffer
;
2219 chunk_bytes
= (This
->bufsize_frames
- This
->lcl_offs_frames
) *
2220 This
->fmt
->nBlockAlign
;
2221 offs_bytes
= This
->lcl_offs_frames
* This
->fmt
->nBlockAlign
;
2222 frames_bytes
= *frames
* This
->fmt
->nBlockAlign
;
2223 memcpy(This
->tmp_buffer
, This
->local_buffer
+ offs_bytes
, chunk_bytes
);
2224 memcpy(This
->tmp_buffer
+ chunk_bytes
, This
->local_buffer
,
2225 frames_bytes
- chunk_bytes
);
2227 *data
= This
->local_buffer
+
2228 This
->lcl_offs_frames
* This
->fmt
->nBlockAlign
;
2230 This
->buf_state
= LOCKED_NORMAL
;
2232 if(devpos
|| qpcpos
)
2233 IAudioClock_GetPosition(&This
->IAudioClock_iface
, devpos
, qpcpos
);
2235 LeaveCriticalSection(&This
->lock
);
2237 return *frames
? S_OK
: AUDCLNT_S_BUFFER_EMPTY
;
2240 static HRESULT WINAPI
AudioCaptureClient_ReleaseBuffer(
2241 IAudioCaptureClient
*iface
, UINT32 done
)
2243 ACImpl
*This
= impl_from_IAudioCaptureClient(iface
);
2245 TRACE("(%p)->(%u)\n", This
, done
);
2247 EnterCriticalSection(&This
->lock
);
2249 if(This
->buf_state
== NOT_LOCKED
){
2250 LeaveCriticalSection(&This
->lock
);
2251 return AUDCLNT_E_OUT_OF_ORDER
;
2254 This
->held_frames
-= done
;
2255 This
->lcl_offs_frames
+= done
;
2256 This
->lcl_offs_frames
%= This
->bufsize_frames
;
2258 This
->buf_state
= NOT_LOCKED
;
2260 LeaveCriticalSection(&This
->lock
);
2265 static HRESULT WINAPI
AudioCaptureClient_GetNextPacketSize(
2266 IAudioCaptureClient
*iface
, UINT32
*frames
)
2268 ACImpl
*This
= impl_from_IAudioCaptureClient(iface
);
2270 TRACE("(%p)->(%p)\n", This
, frames
);
2272 return AudioClient_GetCurrentPadding(&This
->IAudioClient_iface
, frames
);
2275 static const IAudioCaptureClientVtbl AudioCaptureClient_Vtbl
=
2277 AudioCaptureClient_QueryInterface
,
2278 AudioCaptureClient_AddRef
,
2279 AudioCaptureClient_Release
,
2280 AudioCaptureClient_GetBuffer
,
2281 AudioCaptureClient_ReleaseBuffer
,
2282 AudioCaptureClient_GetNextPacketSize
2285 static HRESULT WINAPI
AudioClock_QueryInterface(IAudioClock
*iface
,
2286 REFIID riid
, void **ppv
)
2288 ACImpl
*This
= impl_from_IAudioClock(iface
);
2290 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
2296 if(IsEqualIID(riid
, &IID_IUnknown
) || IsEqualIID(riid
, &IID_IAudioClock
))
2298 else if(IsEqualIID(riid
, &IID_IAudioClock2
))
2299 *ppv
= &This
->IAudioClock2_iface
;
2301 IUnknown_AddRef((IUnknown
*)*ppv
);
2305 WARN("Unknown interface %s\n", debugstr_guid(riid
));
2306 return E_NOINTERFACE
;
2309 static ULONG WINAPI
AudioClock_AddRef(IAudioClock
*iface
)
2311 ACImpl
*This
= impl_from_IAudioClock(iface
);
2312 return IAudioClient_AddRef(&This
->IAudioClient_iface
);
2315 static ULONG WINAPI
AudioClock_Release(IAudioClock
*iface
)
2317 ACImpl
*This
= impl_from_IAudioClock(iface
);
2318 return IAudioClient_Release(&This
->IAudioClient_iface
);
2321 static HRESULT WINAPI
AudioClock_GetFrequency(IAudioClock
*iface
, UINT64
*freq
)
2323 ACImpl
*This
= impl_from_IAudioClock(iface
);
2325 TRACE("(%p)->(%p)\n", This
, freq
);
2327 *freq
= This
->fmt
->nSamplesPerSec
;
2332 static HRESULT WINAPI
AudioClock_GetPosition(IAudioClock
*iface
, UINT64
*pos
,
2335 ACImpl
*This
= impl_from_IAudioClock(iface
);
2339 TRACE("(%p)->(%p, %p)\n", This
, pos
, qpctime
);
2344 EnterCriticalSection(&This
->lock
);
2346 hr
= IAudioClient_GetCurrentPadding(&This
->IAudioClient_iface
, &pad
);
2348 LeaveCriticalSection(&This
->lock
);
2352 if(This
->dataflow
== eRender
)
2353 *pos
= This
->written_frames
- pad
;
2354 else if(This
->dataflow
== eCapture
)
2355 *pos
= This
->written_frames
+ pad
;
2357 LeaveCriticalSection(&This
->lock
);
2360 LARGE_INTEGER stamp
, freq
;
2361 QueryPerformanceCounter(&stamp
);
2362 QueryPerformanceFrequency(&freq
);
2363 *qpctime
= (stamp
.QuadPart
* (INT64
)10000000) / freq
.QuadPart
;
2369 static HRESULT WINAPI
AudioClock_GetCharacteristics(IAudioClock
*iface
,
2372 ACImpl
*This
= impl_from_IAudioClock(iface
);
2374 TRACE("(%p)->(%p)\n", This
, chars
);
2379 *chars
= AUDIOCLOCK_CHARACTERISTIC_FIXED_FREQ
;
2384 static const IAudioClockVtbl AudioClock_Vtbl
=
2386 AudioClock_QueryInterface
,
2389 AudioClock_GetFrequency
,
2390 AudioClock_GetPosition
,
2391 AudioClock_GetCharacteristics
2394 static HRESULT WINAPI
AudioClock2_QueryInterface(IAudioClock2
*iface
,
2395 REFIID riid
, void **ppv
)
2397 ACImpl
*This
= impl_from_IAudioClock2(iface
);
2398 return IAudioClock_QueryInterface(&This
->IAudioClock_iface
, riid
, ppv
);
2401 static ULONG WINAPI
AudioClock2_AddRef(IAudioClock2
*iface
)
2403 ACImpl
*This
= impl_from_IAudioClock2(iface
);
2404 return IAudioClient_AddRef(&This
->IAudioClient_iface
);
2407 static ULONG WINAPI
AudioClock2_Release(IAudioClock2
*iface
)
2409 ACImpl
*This
= impl_from_IAudioClock2(iface
);
2410 return IAudioClient_Release(&This
->IAudioClient_iface
);
2413 static HRESULT WINAPI
AudioClock2_GetDevicePosition(IAudioClock2
*iface
,
2414 UINT64
*pos
, UINT64
*qpctime
)
2416 ACImpl
*This
= impl_from_IAudioClock2(iface
);
2418 FIXME("(%p)->(%p, %p)\n", This
, pos
, qpctime
);
2423 static const IAudioClock2Vtbl AudioClock2_Vtbl
=
2425 AudioClock2_QueryInterface
,
2427 AudioClock2_Release
,
2428 AudioClock2_GetDevicePosition
2431 static AudioSessionWrapper
*AudioSessionWrapper_Create(ACImpl
*client
)
2433 AudioSessionWrapper
*ret
;
2435 ret
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
2436 sizeof(AudioSessionWrapper
));
2440 ret
->IAudioSessionControl2_iface
.lpVtbl
= &AudioSessionControl2_Vtbl
;
2441 ret
->ISimpleAudioVolume_iface
.lpVtbl
= &SimpleAudioVolume_Vtbl
;
2442 ret
->IChannelAudioVolume_iface
.lpVtbl
= &ChannelAudioVolume_Vtbl
;
2446 ret
->client
= client
;
2448 ret
->session
= client
->session
;
2449 AudioClient_AddRef(&client
->IAudioClient_iface
);
2455 static HRESULT WINAPI
AudioSessionControl_QueryInterface(
2456 IAudioSessionControl2
*iface
, REFIID riid
, void **ppv
)
2458 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
2464 if(IsEqualIID(riid
, &IID_IUnknown
) ||
2465 IsEqualIID(riid
, &IID_IAudioSessionControl
) ||
2466 IsEqualIID(riid
, &IID_IAudioSessionControl2
))
2469 IUnknown_AddRef((IUnknown
*)*ppv
);
2473 WARN("Unknown interface %s\n", debugstr_guid(riid
));
2474 return E_NOINTERFACE
;
2477 static ULONG WINAPI
AudioSessionControl_AddRef(IAudioSessionControl2
*iface
)
2479 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2481 ref
= InterlockedIncrement(&This
->ref
);
2482 TRACE("(%p) Refcount now %u\n", This
, ref
);
2486 static ULONG WINAPI
AudioSessionControl_Release(IAudioSessionControl2
*iface
)
2488 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2490 ref
= InterlockedDecrement(&This
->ref
);
2491 TRACE("(%p) Refcount now %u\n", This
, ref
);
2494 EnterCriticalSection(&This
->client
->lock
);
2495 This
->client
->session_wrapper
= NULL
;
2496 LeaveCriticalSection(&This
->client
->lock
);
2497 AudioClient_Release(&This
->client
->IAudioClient_iface
);
2499 HeapFree(GetProcessHeap(), 0, This
);
2504 static HRESULT WINAPI
AudioSessionControl_GetState(IAudioSessionControl2
*iface
,
2505 AudioSessionState
*state
)
2507 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2510 TRACE("(%p)->(%p)\n", This
, state
);
2513 return NULL_PTR_ERR
;
2515 EnterCriticalSection(&g_sessions_lock
);
2517 if(list_empty(&This
->session
->clients
)){
2518 *state
= AudioSessionStateExpired
;
2519 LeaveCriticalSection(&g_sessions_lock
);
2523 LIST_FOR_EACH_ENTRY(client
, &This
->session
->clients
, ACImpl
, entry
){
2524 EnterCriticalSection(&client
->lock
);
2525 if(client
->started
){
2526 *state
= AudioSessionStateActive
;
2527 LeaveCriticalSection(&client
->lock
);
2528 LeaveCriticalSection(&g_sessions_lock
);
2531 LeaveCriticalSection(&client
->lock
);
2534 LeaveCriticalSection(&g_sessions_lock
);
2536 *state
= AudioSessionStateInactive
;
2541 static HRESULT WINAPI
AudioSessionControl_GetDisplayName(
2542 IAudioSessionControl2
*iface
, WCHAR
**name
)
2544 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2546 FIXME("(%p)->(%p) - stub\n", This
, name
);
2551 static HRESULT WINAPI
AudioSessionControl_SetDisplayName(
2552 IAudioSessionControl2
*iface
, const WCHAR
*name
, const GUID
*session
)
2554 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2556 FIXME("(%p)->(%p, %s) - stub\n", This
, name
, debugstr_guid(session
));
2561 static HRESULT WINAPI
AudioSessionControl_GetIconPath(
2562 IAudioSessionControl2
*iface
, WCHAR
**path
)
2564 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2566 FIXME("(%p)->(%p) - stub\n", This
, path
);
2571 static HRESULT WINAPI
AudioSessionControl_SetIconPath(
2572 IAudioSessionControl2
*iface
, const WCHAR
*path
, const GUID
*session
)
2574 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2576 FIXME("(%p)->(%p, %s) - stub\n", This
, path
, debugstr_guid(session
));
2581 static HRESULT WINAPI
AudioSessionControl_GetGroupingParam(
2582 IAudioSessionControl2
*iface
, GUID
*group
)
2584 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2586 FIXME("(%p)->(%p) - stub\n", This
, group
);
2591 static HRESULT WINAPI
AudioSessionControl_SetGroupingParam(
2592 IAudioSessionControl2
*iface
, const GUID
*group
, const GUID
*session
)
2594 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2596 FIXME("(%p)->(%s, %s) - stub\n", This
, debugstr_guid(group
),
2597 debugstr_guid(session
));
2602 static HRESULT WINAPI
AudioSessionControl_RegisterAudioSessionNotification(
2603 IAudioSessionControl2
*iface
, IAudioSessionEvents
*events
)
2605 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2607 FIXME("(%p)->(%p) - stub\n", This
, events
);
2612 static HRESULT WINAPI
AudioSessionControl_UnregisterAudioSessionNotification(
2613 IAudioSessionControl2
*iface
, IAudioSessionEvents
*events
)
2615 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2617 FIXME("(%p)->(%p) - stub\n", This
, events
);
2622 static HRESULT WINAPI
AudioSessionControl_GetSessionIdentifier(
2623 IAudioSessionControl2
*iface
, WCHAR
**id
)
2625 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2627 FIXME("(%p)->(%p) - stub\n", This
, id
);
2632 static HRESULT WINAPI
AudioSessionControl_GetSessionInstanceIdentifier(
2633 IAudioSessionControl2
*iface
, WCHAR
**id
)
2635 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2637 FIXME("(%p)->(%p) - stub\n", This
, id
);
2642 static HRESULT WINAPI
AudioSessionControl_GetProcessId(
2643 IAudioSessionControl2
*iface
, DWORD
*pid
)
2645 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2647 TRACE("(%p)->(%p)\n", This
, pid
);
2652 *pid
= GetCurrentProcessId();
2657 static HRESULT WINAPI
AudioSessionControl_IsSystemSoundsSession(
2658 IAudioSessionControl2
*iface
)
2660 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2662 TRACE("(%p)\n", This
);
2667 static HRESULT WINAPI
AudioSessionControl_SetDuckingPreference(
2668 IAudioSessionControl2
*iface
, BOOL optout
)
2670 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2672 TRACE("(%p)->(%d)\n", This
, optout
);
2677 static const IAudioSessionControl2Vtbl AudioSessionControl2_Vtbl
=
2679 AudioSessionControl_QueryInterface
,
2680 AudioSessionControl_AddRef
,
2681 AudioSessionControl_Release
,
2682 AudioSessionControl_GetState
,
2683 AudioSessionControl_GetDisplayName
,
2684 AudioSessionControl_SetDisplayName
,
2685 AudioSessionControl_GetIconPath
,
2686 AudioSessionControl_SetIconPath
,
2687 AudioSessionControl_GetGroupingParam
,
2688 AudioSessionControl_SetGroupingParam
,
2689 AudioSessionControl_RegisterAudioSessionNotification
,
2690 AudioSessionControl_UnregisterAudioSessionNotification
,
2691 AudioSessionControl_GetSessionIdentifier
,
2692 AudioSessionControl_GetSessionInstanceIdentifier
,
2693 AudioSessionControl_GetProcessId
,
2694 AudioSessionControl_IsSystemSoundsSession
,
2695 AudioSessionControl_SetDuckingPreference
2698 static HRESULT WINAPI
SimpleAudioVolume_QueryInterface(
2699 ISimpleAudioVolume
*iface
, REFIID riid
, void **ppv
)
2701 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
2707 if(IsEqualIID(riid
, &IID_IUnknown
) ||
2708 IsEqualIID(riid
, &IID_ISimpleAudioVolume
))
2711 IUnknown_AddRef((IUnknown
*)*ppv
);
2715 WARN("Unknown interface %s\n", debugstr_guid(riid
));
2716 return E_NOINTERFACE
;
2719 static ULONG WINAPI
SimpleAudioVolume_AddRef(ISimpleAudioVolume
*iface
)
2721 AudioSessionWrapper
*This
= impl_from_ISimpleAudioVolume(iface
);
2722 return AudioSessionControl_AddRef(&This
->IAudioSessionControl2_iface
);
2725 static ULONG WINAPI
SimpleAudioVolume_Release(ISimpleAudioVolume
*iface
)
2727 AudioSessionWrapper
*This
= impl_from_ISimpleAudioVolume(iface
);
2728 return AudioSessionControl_Release(&This
->IAudioSessionControl2_iface
);
2731 static HRESULT WINAPI
SimpleAudioVolume_SetMasterVolume(
2732 ISimpleAudioVolume
*iface
, float level
, const GUID
*context
)
2734 AudioSessionWrapper
*This
= impl_from_ISimpleAudioVolume(iface
);
2735 AudioSession
*session
= This
->session
;
2737 TRACE("(%p)->(%f, %s)\n", session
, level
, wine_dbgstr_guid(context
));
2739 if(level
< 0.f
|| level
> 1.f
)
2740 return E_INVALIDARG
;
2743 FIXME("Notifications not supported yet\n");
2745 TRACE("ALSA does not support volume control\n");
2747 EnterCriticalSection(&session
->lock
);
2749 session
->master_vol
= level
;
2751 LeaveCriticalSection(&session
->lock
);
2756 static HRESULT WINAPI
SimpleAudioVolume_GetMasterVolume(
2757 ISimpleAudioVolume
*iface
, float *level
)
2759 AudioSessionWrapper
*This
= impl_from_ISimpleAudioVolume(iface
);
2760 AudioSession
*session
= This
->session
;
2762 TRACE("(%p)->(%p)\n", session
, level
);
2765 return NULL_PTR_ERR
;
2767 *level
= session
->master_vol
;
2772 static HRESULT WINAPI
SimpleAudioVolume_SetMute(ISimpleAudioVolume
*iface
,
2773 BOOL mute
, const GUID
*context
)
2775 AudioSessionWrapper
*This
= impl_from_ISimpleAudioVolume(iface
);
2776 AudioSession
*session
= This
->session
;
2778 TRACE("(%p)->(%u, %p)\n", session
, mute
, context
);
2781 FIXME("Notifications not supported yet\n");
2783 session
->mute
= mute
;
2788 static HRESULT WINAPI
SimpleAudioVolume_GetMute(ISimpleAudioVolume
*iface
,
2791 AudioSessionWrapper
*This
= impl_from_ISimpleAudioVolume(iface
);
2792 AudioSession
*session
= This
->session
;
2794 TRACE("(%p)->(%p)\n", session
, mute
);
2797 return NULL_PTR_ERR
;
2799 *mute
= session
->mute
;
2804 static const ISimpleAudioVolumeVtbl SimpleAudioVolume_Vtbl
=
2806 SimpleAudioVolume_QueryInterface
,
2807 SimpleAudioVolume_AddRef
,
2808 SimpleAudioVolume_Release
,
2809 SimpleAudioVolume_SetMasterVolume
,
2810 SimpleAudioVolume_GetMasterVolume
,
2811 SimpleAudioVolume_SetMute
,
2812 SimpleAudioVolume_GetMute
2815 static HRESULT WINAPI
AudioStreamVolume_QueryInterface(
2816 IAudioStreamVolume
*iface
, REFIID riid
, void **ppv
)
2818 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
2824 if(IsEqualIID(riid
, &IID_IUnknown
) ||
2825 IsEqualIID(riid
, &IID_IAudioStreamVolume
))
2828 IUnknown_AddRef((IUnknown
*)*ppv
);
2832 WARN("Unknown interface %s\n", debugstr_guid(riid
));
2833 return E_NOINTERFACE
;
2836 static ULONG WINAPI
AudioStreamVolume_AddRef(IAudioStreamVolume
*iface
)
2838 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
2839 return IAudioClient_AddRef(&This
->IAudioClient_iface
);
2842 static ULONG WINAPI
AudioStreamVolume_Release(IAudioStreamVolume
*iface
)
2844 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
2845 return IAudioClient_Release(&This
->IAudioClient_iface
);
2848 static HRESULT WINAPI
AudioStreamVolume_GetChannelCount(
2849 IAudioStreamVolume
*iface
, UINT32
*out
)
2851 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
2853 TRACE("(%p)->(%p)\n", This
, out
);
2858 *out
= This
->fmt
->nChannels
;
2863 static HRESULT WINAPI
AudioStreamVolume_SetChannelVolume(
2864 IAudioStreamVolume
*iface
, UINT32 index
, float level
)
2866 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
2868 TRACE("(%p)->(%d, %f)\n", This
, index
, level
);
2870 if(level
< 0.f
|| level
> 1.f
)
2871 return E_INVALIDARG
;
2873 if(index
>= This
->fmt
->nChannels
)
2874 return E_INVALIDARG
;
2876 TRACE("ALSA does not support volume control\n");
2878 EnterCriticalSection(&This
->lock
);
2880 This
->vols
[index
] = level
;
2882 LeaveCriticalSection(&This
->lock
);
2887 static HRESULT WINAPI
AudioStreamVolume_GetChannelVolume(
2888 IAudioStreamVolume
*iface
, UINT32 index
, float *level
)
2890 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
2892 TRACE("(%p)->(%d, %p)\n", This
, index
, level
);
2897 if(index
>= This
->fmt
->nChannels
)
2898 return E_INVALIDARG
;
2900 *level
= This
->vols
[index
];
2905 static HRESULT WINAPI
AudioStreamVolume_SetAllVolumes(
2906 IAudioStreamVolume
*iface
, UINT32 count
, const float *levels
)
2908 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
2911 TRACE("(%p)->(%d, %p)\n", This
, count
, levels
);
2916 if(count
!= This
->fmt
->nChannels
)
2917 return E_INVALIDARG
;
2919 TRACE("ALSA does not support volume control\n");
2921 EnterCriticalSection(&This
->lock
);
2923 for(i
= 0; i
< count
; ++i
)
2924 This
->vols
[i
] = levels
[i
];
2926 LeaveCriticalSection(&This
->lock
);
2931 static HRESULT WINAPI
AudioStreamVolume_GetAllVolumes(
2932 IAudioStreamVolume
*iface
, UINT32 count
, float *levels
)
2934 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
2937 TRACE("(%p)->(%d, %p)\n", This
, count
, levels
);
2942 if(count
!= This
->fmt
->nChannels
)
2943 return E_INVALIDARG
;
2945 EnterCriticalSection(&This
->lock
);
2947 for(i
= 0; i
< count
; ++i
)
2948 levels
[i
] = This
->vols
[i
];
2950 LeaveCriticalSection(&This
->lock
);
2955 static const IAudioStreamVolumeVtbl AudioStreamVolume_Vtbl
=
2957 AudioStreamVolume_QueryInterface
,
2958 AudioStreamVolume_AddRef
,
2959 AudioStreamVolume_Release
,
2960 AudioStreamVolume_GetChannelCount
,
2961 AudioStreamVolume_SetChannelVolume
,
2962 AudioStreamVolume_GetChannelVolume
,
2963 AudioStreamVolume_SetAllVolumes
,
2964 AudioStreamVolume_GetAllVolumes
2967 static HRESULT WINAPI
ChannelAudioVolume_QueryInterface(
2968 IChannelAudioVolume
*iface
, REFIID riid
, void **ppv
)
2970 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
2976 if(IsEqualIID(riid
, &IID_IUnknown
) ||
2977 IsEqualIID(riid
, &IID_IChannelAudioVolume
))
2980 IUnknown_AddRef((IUnknown
*)*ppv
);
2984 WARN("Unknown interface %s\n", debugstr_guid(riid
));
2985 return E_NOINTERFACE
;
2988 static ULONG WINAPI
ChannelAudioVolume_AddRef(IChannelAudioVolume
*iface
)
2990 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
2991 return AudioSessionControl_AddRef(&This
->IAudioSessionControl2_iface
);
2994 static ULONG WINAPI
ChannelAudioVolume_Release(IChannelAudioVolume
*iface
)
2996 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
2997 return AudioSessionControl_Release(&This
->IAudioSessionControl2_iface
);
3000 static HRESULT WINAPI
ChannelAudioVolume_GetChannelCount(
3001 IChannelAudioVolume
*iface
, UINT32
*out
)
3003 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
3004 AudioSession
*session
= This
->session
;
3006 TRACE("(%p)->(%p)\n", session
, out
);
3009 return NULL_PTR_ERR
;
3011 *out
= session
->channel_count
;
3016 static HRESULT WINAPI
ChannelAudioVolume_SetChannelVolume(
3017 IChannelAudioVolume
*iface
, UINT32 index
, float level
,
3018 const GUID
*context
)
3020 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
3021 AudioSession
*session
= This
->session
;
3023 TRACE("(%p)->(%d, %f, %s)\n", session
, index
, level
,
3024 wine_dbgstr_guid(context
));
3026 if(level
< 0.f
|| level
> 1.f
)
3027 return E_INVALIDARG
;
3029 if(index
>= session
->channel_count
)
3030 return E_INVALIDARG
;
3033 FIXME("Notifications not supported yet\n");
3035 TRACE("ALSA does not support volume control\n");
3037 EnterCriticalSection(&session
->lock
);
3039 session
->channel_vols
[index
] = level
;
3041 LeaveCriticalSection(&session
->lock
);
3046 static HRESULT WINAPI
ChannelAudioVolume_GetChannelVolume(
3047 IChannelAudioVolume
*iface
, UINT32 index
, float *level
)
3049 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
3050 AudioSession
*session
= This
->session
;
3052 TRACE("(%p)->(%d, %p)\n", session
, index
, level
);
3055 return NULL_PTR_ERR
;
3057 if(index
>= session
->channel_count
)
3058 return E_INVALIDARG
;
3060 *level
= session
->channel_vols
[index
];
3065 static HRESULT WINAPI
ChannelAudioVolume_SetAllVolumes(
3066 IChannelAudioVolume
*iface
, UINT32 count
, const float *levels
,
3067 const GUID
*context
)
3069 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
3070 AudioSession
*session
= This
->session
;
3073 TRACE("(%p)->(%d, %p, %s)\n", session
, count
, levels
,
3074 wine_dbgstr_guid(context
));
3077 return NULL_PTR_ERR
;
3079 if(count
!= session
->channel_count
)
3080 return E_INVALIDARG
;
3083 FIXME("Notifications not supported yet\n");
3085 TRACE("ALSA does not support volume control\n");
3087 EnterCriticalSection(&session
->lock
);
3089 for(i
= 0; i
< count
; ++i
)
3090 session
->channel_vols
[i
] = levels
[i
];
3092 LeaveCriticalSection(&session
->lock
);
3097 static HRESULT WINAPI
ChannelAudioVolume_GetAllVolumes(
3098 IChannelAudioVolume
*iface
, UINT32 count
, float *levels
)
3100 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
3101 AudioSession
*session
= This
->session
;
3104 TRACE("(%p)->(%d, %p)\n", session
, count
, levels
);
3107 return NULL_PTR_ERR
;
3109 if(count
!= session
->channel_count
)
3110 return E_INVALIDARG
;
3112 for(i
= 0; i
< count
; ++i
)
3113 levels
[i
] = session
->channel_vols
[i
];
3118 static const IChannelAudioVolumeVtbl ChannelAudioVolume_Vtbl
=
3120 ChannelAudioVolume_QueryInterface
,
3121 ChannelAudioVolume_AddRef
,
3122 ChannelAudioVolume_Release
,
3123 ChannelAudioVolume_GetChannelCount
,
3124 ChannelAudioVolume_SetChannelVolume
,
3125 ChannelAudioVolume_GetChannelVolume
,
3126 ChannelAudioVolume_SetAllVolumes
,
3127 ChannelAudioVolume_GetAllVolumes
3130 static HRESULT WINAPI
AudioSessionManager_QueryInterface(IAudioSessionManager2
*iface
,
3131 REFIID riid
, void **ppv
)
3133 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
3139 if(IsEqualIID(riid
, &IID_IUnknown
) ||
3140 IsEqualIID(riid
, &IID_IAudioSessionManager
) ||
3141 IsEqualIID(riid
, &IID_IAudioSessionManager2
))
3144 IUnknown_AddRef((IUnknown
*)*ppv
);
3148 WARN("Unknown interface %s\n", debugstr_guid(riid
));
3149 return E_NOINTERFACE
;
3152 static ULONG WINAPI
AudioSessionManager_AddRef(IAudioSessionManager2
*iface
)
3154 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
3156 ref
= InterlockedIncrement(&This
->ref
);
3157 TRACE("(%p) Refcount now %u\n", This
, ref
);
3161 static ULONG WINAPI
AudioSessionManager_Release(IAudioSessionManager2
*iface
)
3163 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
3165 ref
= InterlockedDecrement(&This
->ref
);
3166 TRACE("(%p) Refcount now %u\n", This
, ref
);
3168 HeapFree(GetProcessHeap(), 0, This
);
3172 static HRESULT WINAPI
AudioSessionManager_GetAudioSessionControl(
3173 IAudioSessionManager2
*iface
, const GUID
*session_guid
, DWORD flags
,
3174 IAudioSessionControl
**out
)
3176 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
3177 AudioSession
*session
;
3178 AudioSessionWrapper
*wrapper
;
3181 TRACE("(%p)->(%s, %x, %p)\n", This
, debugstr_guid(session_guid
),
3184 hr
= get_audio_session(session_guid
, This
->device
, 0, &session
);
3188 wrapper
= AudioSessionWrapper_Create(NULL
);
3190 return E_OUTOFMEMORY
;
3192 wrapper
->session
= session
;
3194 *out
= (IAudioSessionControl
*)&wrapper
->IAudioSessionControl2_iface
;
3199 static HRESULT WINAPI
AudioSessionManager_GetSimpleAudioVolume(
3200 IAudioSessionManager2
*iface
, const GUID
*session_guid
, DWORD flags
,
3201 ISimpleAudioVolume
**out
)
3203 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
3204 AudioSession
*session
;
3205 AudioSessionWrapper
*wrapper
;
3208 TRACE("(%p)->(%s, %x, %p)\n", This
, debugstr_guid(session_guid
),
3211 hr
= get_audio_session(session_guid
, This
->device
, 0, &session
);
3215 wrapper
= AudioSessionWrapper_Create(NULL
);
3217 return E_OUTOFMEMORY
;
3219 wrapper
->session
= session
;
3221 *out
= &wrapper
->ISimpleAudioVolume_iface
;
3226 static HRESULT WINAPI
AudioSessionManager_GetSessionEnumerator(
3227 IAudioSessionManager2
*iface
, IAudioSessionEnumerator
**out
)
3229 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
3230 FIXME("(%p)->(%p) - stub\n", This
, out
);
3234 static HRESULT WINAPI
AudioSessionManager_RegisterSessionNotification(
3235 IAudioSessionManager2
*iface
, IAudioSessionNotification
*notification
)
3237 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
3238 FIXME("(%p)->(%p) - stub\n", This
, notification
);
3242 static HRESULT WINAPI
AudioSessionManager_UnregisterSessionNotification(
3243 IAudioSessionManager2
*iface
, IAudioSessionNotification
*notification
)
3245 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
3246 FIXME("(%p)->(%p) - stub\n", This
, notification
);
3250 static HRESULT WINAPI
AudioSessionManager_RegisterDuckNotification(
3251 IAudioSessionManager2
*iface
, const WCHAR
*session_id
,
3252 IAudioVolumeDuckNotification
*notification
)
3254 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
3255 FIXME("(%p)->(%p) - stub\n", This
, notification
);
3259 static HRESULT WINAPI
AudioSessionManager_UnregisterDuckNotification(
3260 IAudioSessionManager2
*iface
,
3261 IAudioVolumeDuckNotification
*notification
)
3263 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
3264 FIXME("(%p)->(%p) - stub\n", This
, notification
);
3268 static const IAudioSessionManager2Vtbl AudioSessionManager2_Vtbl
=
3270 AudioSessionManager_QueryInterface
,
3271 AudioSessionManager_AddRef
,
3272 AudioSessionManager_Release
,
3273 AudioSessionManager_GetAudioSessionControl
,
3274 AudioSessionManager_GetSimpleAudioVolume
,
3275 AudioSessionManager_GetSessionEnumerator
,
3276 AudioSessionManager_RegisterSessionNotification
,
3277 AudioSessionManager_UnregisterSessionNotification
,
3278 AudioSessionManager_RegisterDuckNotification
,
3279 AudioSessionManager_UnregisterDuckNotification
3282 HRESULT WINAPI
AUDDRV_GetAudioSessionManager(IMMDevice
*device
,
3283 IAudioSessionManager2
**out
)
3287 This
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(SessionMgr
));
3289 return E_OUTOFMEMORY
;
3291 This
->IAudioSessionManager2_iface
.lpVtbl
= &AudioSessionManager2_Vtbl
;
3292 This
->device
= device
;
3295 *out
= &This
->IAudioSessionManager2_iface
;