2 * Copyright 2011 Andrew Eikum for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #define NONAMELESSUNION
29 #include "wine/debug.h"
30 #include "wine/unicode.h"
31 #include "wine/list.h"
34 #include "mmdeviceapi.h"
38 #include "endpointvolume.h"
41 #include "audioclient.h"
42 #include "audiopolicy.h"
49 #include <sys/types.h>
51 #include <sys/ioctl.h>
55 #include <libkern/OSAtomic.h>
56 #include <CoreAudio/CoreAudio.h>
57 #include <AudioToolbox/AudioQueue.h>
59 WINE_DEFAULT_DEBUG_CHANNEL(coreaudio
);
61 #define NULL_PTR_ERR MAKE_HRESULT(SEVERITY_ERROR, FACILITY_WIN32, RPC_X_NULL_REF_POINTER)
63 #define CAPTURE_BUFFERS 5
65 static const REFERENCE_TIME DefaultPeriod
= 200000;
66 static const REFERENCE_TIME MinimumPeriod
= 100000;
68 typedef struct _AQBuffer
{
69 AudioQueueBufferRef buf
;
74 typedef struct ACImpl ACImpl
;
76 typedef struct _AudioSession
{
86 CRITICAL_SECTION lock
;
91 typedef struct _AudioSessionWrapper
{
92 IAudioSessionControl2 IAudioSessionControl2_iface
;
93 IChannelAudioVolume IChannelAudioVolume_iface
;
94 ISimpleAudioVolume ISimpleAudioVolume_iface
;
99 AudioSession
*session
;
100 } AudioSessionWrapper
;
103 IAudioClient IAudioClient_iface
;
104 IAudioRenderClient IAudioRenderClient_iface
;
105 IAudioCaptureClient IAudioCaptureClient_iface
;
106 IAudioClock IAudioClock_iface
;
107 IAudioClock2 IAudioClock2_iface
;
108 IAudioStreamVolume IAudioStreamVolume_iface
;
118 AUDCLNT_SHAREMODE share
;
122 AudioDeviceID adevid
;
123 AudioQueueRef aqueue
;
124 AudioObjectPropertyScope scope
;
126 UINT32 period_ms
, bufsize_frames
, inbuf_frames
, written_frames
;
128 AudioQueueBufferRef public_buffer
;
132 AudioSession
*session
;
133 AudioSessionWrapper
*session_wrapper
;
137 struct list avail_buffers
;
139 /* We can't use debug printing or {Enter,Leave}CriticalSection from
140 * OSX callback threads, so we use OSX's OSSpinLock for synchronization
141 * instead. OSSpinLock is not a recursive lock, so don't call
142 * synchronized functions while holding the lock. */
152 static const IAudioClientVtbl AudioClient_Vtbl
;
153 static const IAudioRenderClientVtbl AudioRenderClient_Vtbl
;
154 static const IAudioCaptureClientVtbl AudioCaptureClient_Vtbl
;
155 static const IAudioSessionControl2Vtbl AudioSessionControl2_Vtbl
;
156 static const ISimpleAudioVolumeVtbl SimpleAudioVolume_Vtbl
;
157 static const IAudioClockVtbl AudioClock_Vtbl
;
158 static const IAudioClock2Vtbl AudioClock2_Vtbl
;
159 static const IAudioStreamVolumeVtbl AudioStreamVolume_Vtbl
;
160 static const IChannelAudioVolumeVtbl ChannelAudioVolume_Vtbl
;
161 static const IAudioSessionManager2Vtbl AudioSessionManager2_Vtbl
;
163 typedef struct _SessionMgr
{
164 IAudioSessionManager2 IAudioSessionManager2_iface
;
171 static HANDLE g_timer_q
;
173 static CRITICAL_SECTION g_sessions_lock
;
174 static struct list g_sessions
= LIST_INIT(g_sessions
);
176 static HRESULT
AudioClock_GetPosition_nolock(ACImpl
*This
, UINT64
*pos
,
177 UINT64
*qpctime
, BOOL raw
);
178 static AudioSessionWrapper
*AudioSessionWrapper_Create(ACImpl
*client
);
179 static HRESULT
ca_setvol(ACImpl
*This
, UINT32 index
);
181 static inline ACImpl
*impl_from_IAudioClient(IAudioClient
*iface
)
183 return CONTAINING_RECORD(iface
, ACImpl
, IAudioClient_iface
);
186 static inline ACImpl
*impl_from_IAudioRenderClient(IAudioRenderClient
*iface
)
188 return CONTAINING_RECORD(iface
, ACImpl
, IAudioRenderClient_iface
);
191 static inline ACImpl
*impl_from_IAudioCaptureClient(IAudioCaptureClient
*iface
)
193 return CONTAINING_RECORD(iface
, ACImpl
, IAudioCaptureClient_iface
);
196 static inline AudioSessionWrapper
*impl_from_IAudioSessionControl2(IAudioSessionControl2
*iface
)
198 return CONTAINING_RECORD(iface
, AudioSessionWrapper
, IAudioSessionControl2_iface
);
201 static inline AudioSessionWrapper
*impl_from_ISimpleAudioVolume(ISimpleAudioVolume
*iface
)
203 return CONTAINING_RECORD(iface
, AudioSessionWrapper
, ISimpleAudioVolume_iface
);
206 static inline AudioSessionWrapper
*impl_from_IChannelAudioVolume(IChannelAudioVolume
*iface
)
208 return CONTAINING_RECORD(iface
, AudioSessionWrapper
, IChannelAudioVolume_iface
);
211 static inline ACImpl
*impl_from_IAudioClock(IAudioClock
*iface
)
213 return CONTAINING_RECORD(iface
, ACImpl
, IAudioClock_iface
);
216 static inline ACImpl
*impl_from_IAudioClock2(IAudioClock2
*iface
)
218 return CONTAINING_RECORD(iface
, ACImpl
, IAudioClock2_iface
);
221 static inline ACImpl
*impl_from_IAudioStreamVolume(IAudioStreamVolume
*iface
)
223 return CONTAINING_RECORD(iface
, ACImpl
, IAudioStreamVolume_iface
);
226 static inline SessionMgr
*impl_from_IAudioSessionManager2(IAudioSessionManager2
*iface
)
228 return CONTAINING_RECORD(iface
, SessionMgr
, IAudioSessionManager2_iface
);
231 BOOL WINAPI
DllMain(HINSTANCE dll
, DWORD reason
, void *reserved
)
233 if(reason
== DLL_PROCESS_ATTACH
){
234 g_timer_q
= CreateTimerQueue();
238 InitializeCriticalSection(&g_sessions_lock
);
244 HRESULT WINAPI
AUDDRV_GetEndpointIDs(EDataFlow flow
, WCHAR
***ids
,
245 AudioDeviceID
***keys
, UINT
*num
, UINT
*def_index
)
247 UInt32 devsize
, size
;
248 AudioDeviceID
*devices
;
249 AudioDeviceID default_id
;
250 AudioObjectPropertyAddress addr
;
254 TRACE("%d %p %p %p\n", flow
, ids
, num
, def_index
);
256 addr
.mScope
= kAudioObjectPropertyScopeGlobal
;
257 addr
.mElement
= kAudioObjectPropertyElementMaster
;
259 addr
.mSelector
= kAudioHardwarePropertyDefaultOutputDevice
;
260 else if(flow
== eCapture
)
261 addr
.mSelector
= kAudioHardwarePropertyDefaultInputDevice
;
265 size
= sizeof(default_id
);
266 sc
= AudioObjectGetPropertyData(kAudioObjectSystemObject
, &addr
, 0,
267 NULL
, &size
, &default_id
);
269 WARN("Getting _DefaultInputDevice property failed: %lx\n", sc
);
273 addr
.mSelector
= kAudioHardwarePropertyDevices
;
274 sc
= AudioObjectGetPropertyDataSize(kAudioObjectSystemObject
, &addr
, 0,
277 WARN("Getting _Devices property size failed: %lx\n", sc
);
281 devices
= HeapAlloc(GetProcessHeap(), 0, devsize
);
283 return E_OUTOFMEMORY
;
285 sc
= AudioObjectGetPropertyData(kAudioObjectSystemObject
, &addr
, 0, NULL
,
288 WARN("Getting _Devices property failed: %lx\n", sc
);
289 HeapFree(GetProcessHeap(), 0, devices
);
293 ndevices
= devsize
/ sizeof(AudioDeviceID
);
295 *ids
= HeapAlloc(GetProcessHeap(), 0, ndevices
* sizeof(WCHAR
*));
297 HeapFree(GetProcessHeap(), 0, devices
);
298 return E_OUTOFMEMORY
;
301 *keys
= HeapAlloc(GetProcessHeap(), 0, ndevices
* sizeof(AudioDeviceID
*));
303 HeapFree(GetProcessHeap(), 0, *ids
);
304 HeapFree(GetProcessHeap(), 0, devices
);
305 return E_OUTOFMEMORY
;
309 *def_index
= (UINT
)-1;
310 for(i
= 0; i
< ndevices
; ++i
){
311 AudioBufferList
*buffers
;
317 addr
.mSelector
= kAudioDevicePropertyStreamConfiguration
;
319 addr
.mScope
= kAudioDevicePropertyScopeOutput
;
321 addr
.mScope
= kAudioDevicePropertyScopeInput
;
323 sc
= AudioObjectGetPropertyDataSize(devices
[i
], &addr
, 0, NULL
, &size
);
325 WARN("Unable to get _StreamConfiguration property size for "
326 "device %lu: %lx\n", devices
[i
], sc
);
330 buffers
= HeapAlloc(GetProcessHeap(), 0, size
);
332 HeapFree(GetProcessHeap(), 0, devices
);
333 for(j
= 0; j
< *num
; ++j
)
334 HeapFree(GetProcessHeap(), 0, (*ids
)[j
]);
335 HeapFree(GetProcessHeap(), 0, *keys
);
336 HeapFree(GetProcessHeap(), 0, *ids
);
337 return E_OUTOFMEMORY
;
340 sc
= AudioObjectGetPropertyData(devices
[i
], &addr
, 0, NULL
,
343 WARN("Unable to get _StreamConfiguration property for "
344 "device %lu: %lx\n", devices
[i
], sc
);
345 HeapFree(GetProcessHeap(), 0, buffers
);
349 /* check that there's at least one channel in this device before
350 * we claim it as usable */
351 for(j
= 0; j
< buffers
->mNumberBuffers
; ++j
)
352 if(buffers
->mBuffers
[j
].mNumberChannels
> 0)
354 if(j
>= buffers
->mNumberBuffers
){
355 HeapFree(GetProcessHeap(), 0, buffers
);
359 HeapFree(GetProcessHeap(), 0, buffers
);
362 addr
.mSelector
= kAudioObjectPropertyName
;
363 sc
= AudioObjectGetPropertyData(devices
[i
], &addr
, 0, NULL
,
366 WARN("Unable to get _Name property for device %lu: %lx\n",
371 if(!CFStringGetCString(name
, nameA
, sizeof(nameA
),
372 kCFStringEncodingUTF8
)){
373 WARN("Error converting string to UTF8\n");
380 len
= MultiByteToWideChar(CP_UNIXCP
, 0, nameA
, -1, NULL
, 0);
381 (*ids
)[*num
] = HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
383 HeapFree(GetProcessHeap(), 0, devices
);
384 for(j
= 0; j
< *num
; ++j
){
385 HeapFree(GetProcessHeap(), 0, (*ids
)[j
]);
386 HeapFree(GetProcessHeap(), 0, (*keys
)[j
]);
388 HeapFree(GetProcessHeap(), 0, *ids
);
389 HeapFree(GetProcessHeap(), 0, *keys
);
390 return E_OUTOFMEMORY
;
392 MultiByteToWideChar(CP_UNIXCP
, 0, nameA
, -1, (*ids
)[*num
], len
);
394 (*keys
)[*num
] = HeapAlloc(GetProcessHeap(), 0, sizeof(AudioDeviceID
));
396 HeapFree(GetProcessHeap(), 0, devices
);
397 HeapFree(GetProcessHeap(), 0, (*ids
)[*num
]);
398 for(j
= 0; j
< *num
; ++j
){
399 HeapFree(GetProcessHeap(), 0, (*ids
)[j
]);
400 HeapFree(GetProcessHeap(), 0, (*keys
)[j
]);
402 HeapFree(GetProcessHeap(), 0, *ids
);
403 HeapFree(GetProcessHeap(), 0, *keys
);
404 return E_OUTOFMEMORY
;
406 *(*keys
)[*num
] = devices
[i
];
408 if(*def_index
== (UINT
)-1 && devices
[i
] == default_id
)
414 if(*def_index
== (UINT
)-1)
417 HeapFree(GetProcessHeap(), 0, devices
);
422 HRESULT WINAPI
AUDDRV_GetAudioEndpoint(AudioDeviceID
*adevid
, IMMDevice
*dev
,
423 EDataFlow dataflow
, IAudioClient
**out
)
427 TRACE("%p %d %p\n", dev
, dataflow
, out
);
429 This
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(ACImpl
));
431 return E_OUTOFMEMORY
;
433 This
->IAudioClient_iface
.lpVtbl
= &AudioClient_Vtbl
;
434 This
->IAudioRenderClient_iface
.lpVtbl
= &AudioRenderClient_Vtbl
;
435 This
->IAudioCaptureClient_iface
.lpVtbl
= &AudioCaptureClient_Vtbl
;
436 This
->IAudioClock_iface
.lpVtbl
= &AudioClock_Vtbl
;
437 This
->IAudioClock2_iface
.lpVtbl
= &AudioClock2_Vtbl
;
438 This
->IAudioStreamVolume_iface
.lpVtbl
= &AudioStreamVolume_Vtbl
;
440 This
->dataflow
= dataflow
;
442 if(dataflow
== eRender
)
443 This
->scope
= kAudioDevicePropertyScopeOutput
;
444 else if(dataflow
== eCapture
)
445 This
->scope
= kAudioDevicePropertyScopeInput
;
447 HeapFree(GetProcessHeap(), 0, This
);
454 IMMDevice_AddRef(This
->parent
);
456 list_init(&This
->avail_buffers
);
458 This
->adevid
= *adevid
;
460 *out
= &This
->IAudioClient_iface
;
461 IAudioClient_AddRef(&This
->IAudioClient_iface
);
466 static HRESULT WINAPI
AudioClient_QueryInterface(IAudioClient
*iface
,
467 REFIID riid
, void **ppv
)
469 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
474 if(IsEqualIID(riid
, &IID_IUnknown
) || IsEqualIID(riid
, &IID_IAudioClient
))
477 IUnknown_AddRef((IUnknown
*)*ppv
);
480 WARN("Unknown interface %s\n", debugstr_guid(riid
));
481 return E_NOINTERFACE
;
484 static ULONG WINAPI
AudioClient_AddRef(IAudioClient
*iface
)
486 ACImpl
*This
= impl_from_IAudioClient(iface
);
488 ref
= InterlockedIncrement(&This
->ref
);
489 TRACE("(%p) Refcount now %u\n", This
, ref
);
493 static ULONG WINAPI
AudioClient_Release(IAudioClient
*iface
)
495 ACImpl
*This
= impl_from_IAudioClient(iface
);
497 ref
= InterlockedDecrement(&This
->ref
);
498 TRACE("(%p) Refcount now %u\n", This
, ref
);
500 IAudioClient_Stop(iface
);
502 AudioQueueDispose(This
->aqueue
, 1);
504 EnterCriticalSection(&g_sessions_lock
);
505 list_remove(&This
->entry
);
506 LeaveCriticalSection(&g_sessions_lock
);
508 HeapFree(GetProcessHeap(), 0, This
->vols
);
509 HeapFree(GetProcessHeap(), 0, This
->public_buffer
);
510 CoTaskMemFree(This
->fmt
);
511 IMMDevice_Release(This
->parent
);
512 HeapFree(GetProcessHeap(), 0, This
);
517 static void dump_fmt(const WAVEFORMATEX
*fmt
)
519 TRACE("wFormatTag: 0x%x (", fmt
->wFormatTag
);
520 switch(fmt
->wFormatTag
){
521 case WAVE_FORMAT_PCM
:
522 TRACE("WAVE_FORMAT_PCM");
524 case WAVE_FORMAT_IEEE_FLOAT
:
525 TRACE("WAVE_FORMAT_IEEE_FLOAT");
527 case WAVE_FORMAT_EXTENSIBLE
:
528 TRACE("WAVE_FORMAT_EXTENSIBLE");
536 TRACE("nChannels: %u\n", fmt
->nChannels
);
537 TRACE("nSamplesPerSec: %u\n", fmt
->nSamplesPerSec
);
538 TRACE("nAvgBytesPerSec: %u\n", fmt
->nAvgBytesPerSec
);
539 TRACE("nBlockAlign: %u\n", fmt
->nBlockAlign
);
540 TRACE("wBitsPerSample: %u\n", fmt
->wBitsPerSample
);
541 TRACE("cbSize: %u\n", fmt
->cbSize
);
543 if(fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
){
544 WAVEFORMATEXTENSIBLE
*fmtex
= (void*)fmt
;
545 TRACE("dwChannelMask: %08x\n", fmtex
->dwChannelMask
);
546 TRACE("Samples: %04x\n", fmtex
->Samples
.wReserved
);
547 TRACE("SubFormat: %s\n", wine_dbgstr_guid(&fmtex
->SubFormat
));
551 static DWORD
get_channel_mask(unsigned int channels
)
557 return SPEAKER_FRONT_CENTER
;
559 return SPEAKER_FRONT_LEFT
| SPEAKER_FRONT_RIGHT
;
561 return SPEAKER_FRONT_LEFT
| SPEAKER_FRONT_RIGHT
|
562 SPEAKER_LOW_FREQUENCY
;
564 return SPEAKER_FRONT_LEFT
| SPEAKER_FRONT_RIGHT
| SPEAKER_BACK_LEFT
|
567 return SPEAKER_FRONT_LEFT
| SPEAKER_FRONT_RIGHT
| SPEAKER_BACK_LEFT
|
568 SPEAKER_BACK_RIGHT
| SPEAKER_LOW_FREQUENCY
;
570 return SPEAKER_FRONT_LEFT
| SPEAKER_FRONT_RIGHT
| SPEAKER_BACK_LEFT
|
571 SPEAKER_BACK_RIGHT
| SPEAKER_LOW_FREQUENCY
| SPEAKER_FRONT_CENTER
;
573 return SPEAKER_FRONT_LEFT
| SPEAKER_FRONT_RIGHT
| SPEAKER_BACK_LEFT
|
574 SPEAKER_BACK_RIGHT
| SPEAKER_LOW_FREQUENCY
| SPEAKER_FRONT_CENTER
|
577 FIXME("Unknown speaker configuration: %u\n", channels
);
581 static WAVEFORMATEX
*clone_format(const WAVEFORMATEX
*fmt
)
586 if(fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
)
587 size
= sizeof(WAVEFORMATEXTENSIBLE
);
589 size
= sizeof(WAVEFORMATEX
);
591 ret
= CoTaskMemAlloc(size
);
595 memcpy(ret
, fmt
, size
);
597 ret
->cbSize
= size
- sizeof(WAVEFORMATEX
);
602 static HRESULT
ca_get_audiodesc(AudioStreamBasicDescription
*desc
,
603 const WAVEFORMATEX
*fmt
)
605 const WAVEFORMATEXTENSIBLE
*fmtex
= (const WAVEFORMATEXTENSIBLE
*)fmt
;
607 desc
->mFormatFlags
= 0;
609 if(fmt
->wFormatTag
== WAVE_FORMAT_PCM
||
610 (fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
611 IsEqualGUID(&fmtex
->SubFormat
, &KSDATAFORMAT_SUBTYPE_PCM
))){
612 desc
->mFormatID
= kAudioFormatLinearPCM
;
613 if(fmt
->wBitsPerSample
> 8)
614 desc
->mFormatFlags
= kAudioFormatFlagIsSignedInteger
;
615 }else if(fmt
->wFormatTag
== WAVE_FORMAT_IEEE_FLOAT
||
616 (fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
617 IsEqualGUID(&fmtex
->SubFormat
, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
))){
618 desc
->mFormatID
= kAudioFormatLinearPCM
;
619 desc
->mFormatFlags
= kAudioFormatFlagIsFloat
;
620 }else if(fmt
->wFormatTag
== WAVE_FORMAT_MULAW
||
621 (fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
622 IsEqualGUID(&fmtex
->SubFormat
, &KSDATAFORMAT_SUBTYPE_MULAW
))){
623 desc
->mFormatID
= kAudioFormatULaw
;
624 }else if(fmt
->wFormatTag
== WAVE_FORMAT_ALAW
||
625 (fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
626 IsEqualGUID(&fmtex
->SubFormat
, &KSDATAFORMAT_SUBTYPE_ALAW
))){
627 desc
->mFormatID
= kAudioFormatALaw
;
629 return AUDCLNT_E_UNSUPPORTED_FORMAT
;
631 desc
->mSampleRate
= fmt
->nSamplesPerSec
;
632 desc
->mBytesPerPacket
= fmt
->nBlockAlign
;
633 desc
->mFramesPerPacket
= 1;
634 desc
->mBytesPerFrame
= fmt
->nBlockAlign
;
635 desc
->mChannelsPerFrame
= fmt
->nChannels
;
636 desc
->mBitsPerChannel
= fmt
->wBitsPerSample
;
642 static void ca_out_buffer_cb(void *user
, AudioQueueRef aqueue
,
643 AudioQueueBufferRef buffer
)
646 AQBuffer
*buf
= buffer
->mUserData
;
648 OSSpinLockLock(&This
->lock
);
649 list_add_tail(&This
->avail_buffers
, &buf
->entry
);
650 This
->inbuf_frames
-= buffer
->mAudioDataByteSize
/ This
->fmt
->nBlockAlign
;
651 OSSpinLockUnlock(&This
->lock
);
654 static void ca_in_buffer_cb(void *user
, AudioQueueRef aqueue
,
655 AudioQueueBufferRef buffer
, const AudioTimeStamp
*start
,
656 UInt32 ndesc
, const AudioStreamPacketDescription
*descs
)
659 AQBuffer
*buf
= buffer
->mUserData
;
661 OSSpinLockLock(&This
->lock
);
662 list_add_tail(&This
->avail_buffers
, &buf
->entry
);
663 This
->inbuf_frames
+= buffer
->mAudioDataByteSize
/ This
->fmt
->nBlockAlign
;
664 OSSpinLockUnlock(&This
->lock
);
667 static HRESULT
ca_setup_aqueue(AudioDeviceID did
, EDataFlow flow
,
668 const WAVEFORMATEX
*fmt
, void *user
, AudioQueueRef
*aqueue
)
670 AudioStreamBasicDescription desc
;
671 AudioObjectPropertyAddress addr
;
677 addr
.mScope
= kAudioObjectPropertyScopeGlobal
;
679 addr
.mSelector
= kAudioDevicePropertyDeviceUID
;
682 sc
= AudioObjectGetPropertyData(did
, &addr
, 0, NULL
, &size
, &uid
);
684 WARN("Unable to get _DeviceUID property: %lx\n", sc
);
688 hr
= ca_get_audiodesc(&desc
, fmt
);
695 sc
= AudioQueueNewOutput(&desc
, ca_out_buffer_cb
, user
, NULL
, NULL
, 0,
697 else if(flow
== eCapture
)
698 sc
= AudioQueueNewInput(&desc
, ca_in_buffer_cb
, user
, NULL
, NULL
, 0,
705 WARN("Unable to create AudioQueue: %lx\n", sc
);
710 sc
= AudioQueueSetProperty(*aqueue
, kAudioQueueProperty_CurrentDevice
,
722 static void session_init_vols(AudioSession
*session
, UINT channels
)
724 if(session
->channel_count
< channels
){
727 if(session
->channel_vols
)
728 session
->channel_vols
= HeapReAlloc(GetProcessHeap(), 0,
729 session
->channel_vols
, sizeof(float) * channels
);
731 session
->channel_vols
= HeapAlloc(GetProcessHeap(), 0,
732 sizeof(float) * channels
);
733 if(!session
->channel_vols
)
736 for(i
= session
->channel_count
; i
< channels
; ++i
)
737 session
->channel_vols
[i
] = 1.f
;
739 session
->channel_count
= channels
;
743 static AudioSession
*create_session(const GUID
*guid
, IMMDevice
*device
,
748 ret
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(AudioSession
));
752 memcpy(&ret
->guid
, guid
, sizeof(GUID
));
754 ret
->device
= device
;
756 list_init(&ret
->clients
);
758 list_add_head(&g_sessions
, &ret
->entry
);
760 InitializeCriticalSection(&ret
->lock
);
762 session_init_vols(ret
, num_channels
);
764 ret
->master_vol
= 1.f
;
769 /* if channels == 0, then this will return or create a session with
770 * matching dataflow and GUID. otherwise, channels must also match */
771 static HRESULT
get_audio_session(const GUID
*sessionguid
,
772 IMMDevice
*device
, UINT channels
, AudioSession
**out
)
774 AudioSession
*session
;
776 if(!sessionguid
|| IsEqualGUID(sessionguid
, &GUID_NULL
)){
777 *out
= create_session(&GUID_NULL
, device
, channels
);
779 return E_OUTOFMEMORY
;
785 LIST_FOR_EACH_ENTRY(session
, &g_sessions
, AudioSession
, entry
){
786 if(session
->device
== device
&&
787 IsEqualGUID(sessionguid
, &session
->guid
)){
788 session_init_vols(session
, channels
);
795 *out
= create_session(sessionguid
, device
, channels
);
797 return E_OUTOFMEMORY
;
803 static HRESULT WINAPI
AudioClient_Initialize(IAudioClient
*iface
,
804 AUDCLNT_SHAREMODE mode
, DWORD flags
, REFERENCE_TIME duration
,
805 REFERENCE_TIME period
, const WAVEFORMATEX
*fmt
,
806 const GUID
*sessionguid
)
808 ACImpl
*This
= impl_from_IAudioClient(iface
);
813 TRACE("(%p)->(%x, %x, %s, %s, %p, %s)\n", This
, mode
, flags
,
814 wine_dbgstr_longlong(duration
), wine_dbgstr_longlong(period
), fmt
, debugstr_guid(sessionguid
));
821 if(mode
!= AUDCLNT_SHAREMODE_SHARED
&& mode
!= AUDCLNT_SHAREMODE_EXCLUSIVE
)
822 return AUDCLNT_E_NOT_INITIALIZED
;
824 if(flags
& ~(AUDCLNT_STREAMFLAGS_CROSSPROCESS
|
825 AUDCLNT_STREAMFLAGS_LOOPBACK
|
826 AUDCLNT_STREAMFLAGS_EVENTCALLBACK
|
827 AUDCLNT_STREAMFLAGS_NOPERSIST
|
828 AUDCLNT_STREAMFLAGS_RATEADJUST
|
829 AUDCLNT_SESSIONFLAGS_EXPIREWHENUNOWNED
|
830 AUDCLNT_SESSIONFLAGS_DISPLAY_HIDE
|
831 AUDCLNT_SESSIONFLAGS_DISPLAY_HIDEWHENEXPIRED
)){
832 TRACE("Unknown flags: %08x\n", flags
);
836 OSSpinLockLock(&This
->lock
);
839 OSSpinLockUnlock(&This
->lock
);
840 return AUDCLNT_E_ALREADY_INITIALIZED
;
843 hr
= ca_setup_aqueue(This
->adevid
, This
->dataflow
, fmt
, This
, &This
->aqueue
);
845 OSSpinLockUnlock(&This
->lock
);
849 This
->fmt
= clone_format(fmt
);
851 AudioQueueDispose(This
->aqueue
, 1);
853 OSSpinLockUnlock(&This
->lock
);
854 return E_OUTOFMEMORY
;
858 This
->period_ms
= period
/ 10000;
859 if(This
->period_ms
== 0)
862 This
->period_ms
= MinimumPeriod
/ 10000;
864 This
->bufsize_frames
= ceil(fmt
->nSamplesPerSec
* (duration
/ 10000000.));
866 if(This
->dataflow
== eCapture
){
868 UInt32 bsize
= ceil((This
->bufsize_frames
/ (double)CAPTURE_BUFFERS
) *
869 This
->fmt
->nBlockAlign
);
870 for(i
= 0; i
< CAPTURE_BUFFERS
; ++i
){
873 buf
= HeapAlloc(GetProcessHeap(), 0, sizeof(AQBuffer
));
875 AudioQueueDispose(This
->aqueue
, 1);
877 CoTaskMemFree(This
->fmt
);
879 OSSpinLockUnlock(&This
->lock
);
880 return E_OUTOFMEMORY
;
883 sc
= AudioQueueAllocateBuffer(This
->aqueue
, bsize
, &buf
->buf
);
885 AudioQueueDispose(This
->aqueue
, 1);
887 CoTaskMemFree(This
->fmt
);
889 OSSpinLockUnlock(&This
->lock
);
890 WARN("Couldn't allocate buffer: %lx\n", sc
);
894 buf
->buf
->mUserData
= buf
;
896 sc
= AudioQueueEnqueueBuffer(This
->aqueue
, buf
->buf
, 0, NULL
);
898 ERR("Couldn't enqueue buffer: %lx\n", sc
);
904 This
->vols
= HeapAlloc(GetProcessHeap(), 0, fmt
->nChannels
* sizeof(float));
906 AudioQueueDispose(This
->aqueue
, 1);
908 CoTaskMemFree(This
->fmt
);
910 OSSpinLockUnlock(&This
->lock
);
911 return E_OUTOFMEMORY
;
914 for(i
= 0; i
< fmt
->nChannels
; ++i
)
920 EnterCriticalSection(&g_sessions_lock
);
922 hr
= get_audio_session(sessionguid
, This
->parent
, fmt
->nChannels
,
925 LeaveCriticalSection(&g_sessions_lock
);
926 AudioQueueDispose(This
->aqueue
, 1);
928 CoTaskMemFree(This
->fmt
);
930 HeapFree(GetProcessHeap(), 0, This
->vols
);
932 OSSpinLockUnlock(&This
->lock
);
936 list_add_tail(&This
->session
->clients
, &This
->entry
);
938 LeaveCriticalSection(&g_sessions_lock
);
942 OSSpinLockUnlock(&This
->lock
);
947 static HRESULT WINAPI
AudioClient_GetBufferSize(IAudioClient
*iface
,
950 ACImpl
*This
= impl_from_IAudioClient(iface
);
952 TRACE("(%p)->(%p)\n", This
, frames
);
957 OSSpinLockLock(&This
->lock
);
960 OSSpinLockUnlock(&This
->lock
);
961 return AUDCLNT_E_NOT_INITIALIZED
;
964 *frames
= This
->bufsize_frames
;
966 OSSpinLockUnlock(&This
->lock
);
971 static HRESULT
ca_get_max_stream_latency(ACImpl
*This
, UInt32
*max
)
973 AudioObjectPropertyAddress addr
;
979 addr
.mScope
= This
->scope
;
981 addr
.mSelector
= kAudioDevicePropertyStreams
;
983 sc
= AudioObjectGetPropertyDataSize(This
->adevid
, &addr
, 0, NULL
,
986 WARN("Unable to get size for _Streams property: %lx\n", sc
);
990 ids
= HeapAlloc(GetProcessHeap(), 0, size
);
992 return E_OUTOFMEMORY
;
994 sc
= AudioObjectGetPropertyData(This
->adevid
, &addr
, 0, NULL
, &size
, ids
);
996 WARN("Unable to get _Streams property: %lx\n", sc
);
997 HeapFree(GetProcessHeap(), 0, ids
);
1001 nstreams
= size
/ sizeof(AudioStreamID
);
1004 addr
.mSelector
= kAudioStreamPropertyLatency
;
1005 for(i
= 0; i
< nstreams
; ++i
){
1008 size
= sizeof(latency
);
1009 sc
= AudioObjectGetPropertyData(ids
[i
], &addr
, 0, NULL
,
1012 WARN("Unable to get _Latency property: %lx\n", sc
);
1020 HeapFree(GetProcessHeap(), 0, ids
);
1025 static HRESULT WINAPI
AudioClient_GetStreamLatency(IAudioClient
*iface
,
1026 REFERENCE_TIME
*out
)
1028 ACImpl
*This
= impl_from_IAudioClient(iface
);
1029 UInt32 latency
, stream_latency
, size
;
1030 AudioObjectPropertyAddress addr
;
1034 TRACE("(%p)->(%p)\n", This
, out
);
1039 OSSpinLockLock(&This
->lock
);
1042 OSSpinLockUnlock(&This
->lock
);
1043 return AUDCLNT_E_NOT_INITIALIZED
;
1046 addr
.mScope
= This
->scope
;
1047 addr
.mSelector
= kAudioDevicePropertyLatency
;
1050 size
= sizeof(latency
);
1051 sc
= AudioObjectGetPropertyData(This
->adevid
, &addr
, 0, NULL
,
1054 WARN("Couldn't get _Latency property: %lx\n", sc
);
1055 OSSpinLockUnlock(&This
->lock
);
1059 hr
= ca_get_max_stream_latency(This
, &stream_latency
);
1061 OSSpinLockUnlock(&This
->lock
);
1065 latency
+= stream_latency
;
1066 *out
= (latency
/ (double)This
->fmt
->nSamplesPerSec
) * 10000000;
1068 OSSpinLockUnlock(&This
->lock
);
1073 static HRESULT
AudioClient_GetCurrentPadding_nolock(ACImpl
*This
,
1077 return AUDCLNT_E_NOT_INITIALIZED
;
1079 *numpad
= This
->inbuf_frames
;
1084 static HRESULT WINAPI
AudioClient_GetCurrentPadding(IAudioClient
*iface
,
1087 ACImpl
*This
= impl_from_IAudioClient(iface
);
1090 TRACE("(%p)->(%p)\n", This
, numpad
);
1095 OSSpinLockLock(&This
->lock
);
1097 hr
= AudioClient_GetCurrentPadding_nolock(This
, numpad
);
1099 OSSpinLockUnlock(&This
->lock
);
1104 static HRESULT WINAPI
AudioClient_IsFormatSupported(IAudioClient
*iface
,
1105 AUDCLNT_SHAREMODE mode
, const WAVEFORMATEX
*pwfx
,
1106 WAVEFORMATEX
**outpwfx
)
1108 ACImpl
*This
= impl_from_IAudioClient(iface
);
1109 AudioQueueRef aqueue
;
1112 TRACE("(%p)->(%x, %p, %p)\n", This
, mode
, pwfx
, outpwfx
);
1114 if(!pwfx
|| (mode
== AUDCLNT_SHAREMODE_SHARED
&& !outpwfx
))
1117 if(mode
!= AUDCLNT_SHAREMODE_SHARED
&& mode
!= AUDCLNT_SHAREMODE_EXCLUSIVE
)
1118 return E_INVALIDARG
;
1120 if(pwfx
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
1121 pwfx
->cbSize
< sizeof(WAVEFORMATEXTENSIBLE
) - sizeof(WAVEFORMATEX
))
1122 return E_INVALIDARG
;
1126 OSSpinLockLock(&This
->lock
);
1128 hr
= ca_setup_aqueue(This
->adevid
, This
->dataflow
, pwfx
, NULL
, &aqueue
);
1130 AudioQueueDispose(aqueue
, 1);
1131 OSSpinLockUnlock(&This
->lock
);
1134 TRACE("returning %08x\n", S_OK
);
1138 OSSpinLockUnlock(&This
->lock
);
1143 TRACE("returning %08x\n", AUDCLNT_E_UNSUPPORTED_FORMAT
);
1144 return AUDCLNT_E_UNSUPPORTED_FORMAT
;
1147 static HRESULT WINAPI
AudioClient_GetMixFormat(IAudioClient
*iface
,
1148 WAVEFORMATEX
**pwfx
)
1150 ACImpl
*This
= impl_from_IAudioClient(iface
);
1151 WAVEFORMATEXTENSIBLE
*fmt
;
1155 AudioBufferList
*buffers
;
1156 AudioObjectPropertyAddress addr
;
1159 TRACE("(%p)->(%p)\n", This
, pwfx
);
1165 fmt
= CoTaskMemAlloc(sizeof(WAVEFORMATEXTENSIBLE
));
1167 return E_OUTOFMEMORY
;
1169 fmt
->Format
.wFormatTag
= WAVE_FORMAT_EXTENSIBLE
;
1171 addr
.mScope
= This
->scope
;
1173 addr
.mSelector
= kAudioDevicePropertyStreamConfiguration
;
1175 sc
= AudioObjectGetPropertyDataSize(This
->adevid
, &addr
, 0, NULL
, &size
);
1178 WARN("Unable to get size for _StreamConfiguration property: %lx\n", sc
);
1182 buffers
= HeapAlloc(GetProcessHeap(), 0, size
);
1185 return E_OUTOFMEMORY
;
1188 sc
= AudioObjectGetPropertyData(This
->adevid
, &addr
, 0, NULL
,
1192 HeapFree(GetProcessHeap(), 0, buffers
);
1193 WARN("Unable to get _StreamConfiguration property: %lx\n", sc
);
1197 fmt
->Format
.nChannels
= 0;
1198 for(i
= 0; i
< buffers
->mNumberBuffers
; ++i
)
1199 fmt
->Format
.nChannels
+= buffers
->mBuffers
[i
].mNumberChannels
;
1201 HeapFree(GetProcessHeap(), 0, buffers
);
1203 fmt
->dwChannelMask
= get_channel_mask(fmt
->Format
.nChannels
);
1205 addr
.mSelector
= kAudioDevicePropertyNominalSampleRate
;
1206 size
= sizeof(Float64
);
1207 sc
= AudioObjectGetPropertyData(This
->adevid
, &addr
, 0, NULL
, &size
, &rate
);
1210 WARN("Unable to get _NominalSampleRate property: %lx\n", sc
);
1213 fmt
->Format
.nSamplesPerSec
= rate
;
1215 fmt
->Format
.wBitsPerSample
= 32;
1216 fmt
->SubFormat
= KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
;
1218 fmt
->Format
.nBlockAlign
= (fmt
->Format
.wBitsPerSample
*
1219 fmt
->Format
.nChannels
) / 8;
1220 fmt
->Format
.nAvgBytesPerSec
= fmt
->Format
.nSamplesPerSec
*
1221 fmt
->Format
.nBlockAlign
;
1223 fmt
->Samples
.wValidBitsPerSample
= fmt
->Format
.wBitsPerSample
;
1224 fmt
->Format
.cbSize
= sizeof(WAVEFORMATEXTENSIBLE
) - sizeof(WAVEFORMATEX
);
1226 *pwfx
= (WAVEFORMATEX
*)fmt
;
1232 static HRESULT WINAPI
AudioClient_GetDevicePeriod(IAudioClient
*iface
,
1233 REFERENCE_TIME
*defperiod
, REFERENCE_TIME
*minperiod
)
1235 ACImpl
*This
= impl_from_IAudioClient(iface
);
1237 TRACE("(%p)->(%p, %p)\n", This
, defperiod
, minperiod
);
1239 if(!defperiod
&& !minperiod
)
1242 OSSpinLockLock(&This
->lock
);
1244 if(This
->period_ms
){
1246 *defperiod
= This
->period_ms
* 10000;
1248 *minperiod
= This
->period_ms
* 10000;
1251 *defperiod
= DefaultPeriod
;
1253 *minperiod
= MinimumPeriod
;
1256 OSSpinLockUnlock(&This
->lock
);
1261 void CALLBACK
ca_period_cb(void *user
, BOOLEAN timer
)
1263 ACImpl
*This
= user
;
1265 OSSpinLockLock(&This
->lock
);
1267 SetEvent(This
->event
);
1268 OSSpinLockUnlock(&This
->lock
);
1271 static HRESULT WINAPI
AudioClient_Start(IAudioClient
*iface
)
1273 ACImpl
*This
= impl_from_IAudioClient(iface
);
1276 TRACE("(%p)\n", This
);
1278 OSSpinLockLock(&This
->lock
);
1281 OSSpinLockUnlock(&This
->lock
);
1282 return AUDCLNT_E_NOT_INITIALIZED
;
1285 if(This
->playing
!= StateStopped
){
1286 OSSpinLockUnlock(&This
->lock
);
1287 return AUDCLNT_E_NOT_STOPPED
;
1290 if((This
->flags
& AUDCLNT_STREAMFLAGS_EVENTCALLBACK
) && !This
->event
){
1291 OSSpinLockUnlock(&This
->lock
);
1292 return AUDCLNT_E_EVENTHANDLE_NOT_SET
;
1296 if(!CreateTimerQueueTimer(&This
->timer
, g_timer_q
,
1297 ca_period_cb
, This
, 0, This
->period_ms
, 0))
1298 ERR("Unable to create timer: %u\n", GetLastError());
1300 This
->playing
= StateInTransition
;
1302 OSSpinLockUnlock(&This
->lock
);
1304 sc
= AudioQueueStart(This
->aqueue
, NULL
);
1306 WARN("Unable to start audio queue: %lx\n", sc
);
1310 OSSpinLockLock(&This
->lock
);
1312 This
->playing
= StatePlaying
;
1314 OSSpinLockUnlock(&This
->lock
);
1319 static HRESULT WINAPI
AudioClient_Stop(IAudioClient
*iface
)
1321 ACImpl
*This
= impl_from_IAudioClient(iface
);
1324 TRACE("(%p)\n", This
);
1326 OSSpinLockLock(&This
->lock
);
1329 OSSpinLockUnlock(&This
->lock
);
1330 return AUDCLNT_E_NOT_INITIALIZED
;
1333 if(This
->playing
== StateStopped
){
1334 OSSpinLockUnlock(&This
->lock
);
1338 if(This
->playing
== StateInTransition
){
1339 OSSpinLockUnlock(&This
->lock
);
1343 if(This
->timer
&& This
->timer
!= INVALID_HANDLE_VALUE
){
1344 DeleteTimerQueueTimer(g_timer_q
, This
->timer
, INVALID_HANDLE_VALUE
);
1348 This
->playing
= StateInTransition
;
1350 OSSpinLockUnlock(&This
->lock
);
1352 sc
= AudioQueueFlush(This
->aqueue
);
1354 WARN("Unable to flush audio queue: %lx\n", sc
);
1356 sc
= AudioQueuePause(This
->aqueue
);
1358 WARN("Unable to pause audio queue: %lx\n", sc
);
1362 OSSpinLockLock(&This
->lock
);
1364 This
->playing
= StateStopped
;
1366 OSSpinLockUnlock(&This
->lock
);
1371 static HRESULT WINAPI
AudioClient_Reset(IAudioClient
*iface
)
1373 ACImpl
*This
= impl_from_IAudioClient(iface
);
1377 TRACE("(%p)\n", This
);
1379 OSSpinLockLock(&This
->lock
);
1382 OSSpinLockUnlock(&This
->lock
);
1383 return AUDCLNT_E_NOT_INITIALIZED
;
1386 if(This
->playing
!= StateStopped
){
1387 OSSpinLockUnlock(&This
->lock
);
1388 return AUDCLNT_E_NOT_STOPPED
;
1391 This
->written_frames
= 0;
1393 hr
= AudioClock_GetPosition_nolock(This
, &This
->last_time
, NULL
, TRUE
);
1395 OSSpinLockUnlock(&This
->lock
);
1399 OSSpinLockUnlock(&This
->lock
);
1401 sc
= AudioQueueReset(This
->aqueue
);
1403 WARN("Unable to reset audio queue: %lx\n", sc
);
1410 static HRESULT WINAPI
AudioClient_SetEventHandle(IAudioClient
*iface
,
1413 ACImpl
*This
= impl_from_IAudioClient(iface
);
1415 TRACE("(%p)->(%p)\n", This
, event
);
1418 return E_INVALIDARG
;
1420 OSSpinLockLock(&This
->lock
);
1423 OSSpinLockUnlock(&This
->lock
);
1424 return AUDCLNT_E_NOT_INITIALIZED
;
1427 if(!(This
->flags
& AUDCLNT_STREAMFLAGS_EVENTCALLBACK
)){
1428 OSSpinLockUnlock(&This
->lock
);
1429 return AUDCLNT_E_EVENTHANDLE_NOT_EXPECTED
;
1432 This
->event
= event
;
1434 OSSpinLockUnlock(&This
->lock
);
1439 static HRESULT WINAPI
AudioClient_GetService(IAudioClient
*iface
, REFIID riid
,
1442 ACImpl
*This
= impl_from_IAudioClient(iface
);
1444 TRACE("(%p)->(%s, %p)\n", This
, debugstr_guid(riid
), ppv
);
1450 OSSpinLockLock(&This
->lock
);
1453 OSSpinLockUnlock(&This
->lock
);
1454 return AUDCLNT_E_NOT_INITIALIZED
;
1457 if(IsEqualIID(riid
, &IID_IAudioRenderClient
)){
1458 if(This
->dataflow
!= eRender
){
1459 OSSpinLockUnlock(&This
->lock
);
1460 return AUDCLNT_E_WRONG_ENDPOINT_TYPE
;
1462 *ppv
= &This
->IAudioRenderClient_iface
;
1463 }else if(IsEqualIID(riid
, &IID_IAudioCaptureClient
)){
1464 if(This
->dataflow
!= eCapture
){
1465 OSSpinLockUnlock(&This
->lock
);
1466 return AUDCLNT_E_WRONG_ENDPOINT_TYPE
;
1468 *ppv
= &This
->IAudioCaptureClient_iface
;
1469 }else if(IsEqualIID(riid
, &IID_IAudioClock
)){
1470 *ppv
= &This
->IAudioClock_iface
;
1471 }else if(IsEqualIID(riid
, &IID_IAudioStreamVolume
)){
1472 *ppv
= &This
->IAudioStreamVolume_iface
;
1473 }else if(IsEqualIID(riid
, &IID_IAudioSessionControl
)){
1474 if(!This
->session_wrapper
){
1475 This
->session_wrapper
= AudioSessionWrapper_Create(This
);
1476 if(!This
->session_wrapper
){
1477 OSSpinLockUnlock(&This
->lock
);
1478 return E_OUTOFMEMORY
;
1482 *ppv
= &This
->session_wrapper
->IAudioSessionControl2_iface
;
1483 }else if(IsEqualIID(riid
, &IID_IChannelAudioVolume
)){
1484 if(!This
->session_wrapper
){
1485 This
->session_wrapper
= AudioSessionWrapper_Create(This
);
1486 if(!This
->session_wrapper
){
1487 OSSpinLockUnlock(&This
->lock
);
1488 return E_OUTOFMEMORY
;
1492 *ppv
= &This
->session_wrapper
->IChannelAudioVolume_iface
;
1493 }else if(IsEqualIID(riid
, &IID_ISimpleAudioVolume
)){
1494 if(!This
->session_wrapper
){
1495 This
->session_wrapper
= AudioSessionWrapper_Create(This
);
1496 if(!This
->session_wrapper
){
1497 OSSpinLockUnlock(&This
->lock
);
1498 return E_OUTOFMEMORY
;
1502 *ppv
= &This
->session_wrapper
->ISimpleAudioVolume_iface
;
1506 IUnknown_AddRef((IUnknown
*)*ppv
);
1507 OSSpinLockUnlock(&This
->lock
);
1511 OSSpinLockUnlock(&This
->lock
);
1513 FIXME("stub %s\n", debugstr_guid(riid
));
1514 return E_NOINTERFACE
;
1517 static const IAudioClientVtbl AudioClient_Vtbl
=
1519 AudioClient_QueryInterface
,
1521 AudioClient_Release
,
1522 AudioClient_Initialize
,
1523 AudioClient_GetBufferSize
,
1524 AudioClient_GetStreamLatency
,
1525 AudioClient_GetCurrentPadding
,
1526 AudioClient_IsFormatSupported
,
1527 AudioClient_GetMixFormat
,
1528 AudioClient_GetDevicePeriod
,
1532 AudioClient_SetEventHandle
,
1533 AudioClient_GetService
1536 static HRESULT WINAPI
AudioRenderClient_QueryInterface(
1537 IAudioRenderClient
*iface
, REFIID riid
, void **ppv
)
1539 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
1545 if(IsEqualIID(riid
, &IID_IUnknown
) ||
1546 IsEqualIID(riid
, &IID_IAudioRenderClient
))
1549 IUnknown_AddRef((IUnknown
*)*ppv
);
1553 WARN("Unknown interface %s\n", debugstr_guid(riid
));
1554 return E_NOINTERFACE
;
1557 static ULONG WINAPI
AudioRenderClient_AddRef(IAudioRenderClient
*iface
)
1559 ACImpl
*This
= impl_from_IAudioRenderClient(iface
);
1560 return AudioClient_AddRef(&This
->IAudioClient_iface
);
1563 static ULONG WINAPI
AudioRenderClient_Release(IAudioRenderClient
*iface
)
1565 ACImpl
*This
= impl_from_IAudioRenderClient(iface
);
1566 return AudioClient_Release(&This
->IAudioClient_iface
);
1569 static HRESULT WINAPI
AudioRenderClient_GetBuffer(IAudioRenderClient
*iface
,
1570 UINT32 frames
, BYTE
**data
)
1572 ACImpl
*This
= impl_from_IAudioRenderClient(iface
);
1574 UINT32 pad
, bytes
= frames
* This
->fmt
->nBlockAlign
;
1578 TRACE("(%p)->(%u, %p)\n", This
, frames
, data
);
1583 OSSpinLockLock(&This
->lock
);
1585 if(This
->getbuf_last
){
1586 OSSpinLockUnlock(&This
->lock
);
1587 return AUDCLNT_E_OUT_OF_ORDER
;
1591 This
->getbuf_last
= TRUE
;
1592 OSSpinLockUnlock(&This
->lock
);
1596 hr
= AudioClient_GetCurrentPadding_nolock(This
, &pad
);
1598 OSSpinLockUnlock(&This
->lock
);
1602 if(pad
+ frames
> This
->bufsize_frames
){
1603 OSSpinLockUnlock(&This
->lock
);
1604 return AUDCLNT_E_BUFFER_TOO_LARGE
;
1607 LIST_FOR_EACH_ENTRY(buf
, &This
->avail_buffers
, AQBuffer
, entry
){
1608 if(buf
->buf
->mAudioDataBytesCapacity
>= bytes
){
1609 This
->public_buffer
= buf
->buf
;
1610 list_remove(&buf
->entry
);
1615 if(&buf
->entry
== &This
->avail_buffers
){
1616 sc
= AudioQueueAllocateBuffer(This
->aqueue
, bytes
,
1617 &This
->public_buffer
);
1619 OSSpinLockUnlock(&This
->lock
);
1620 WARN("Unable to allocate buffer: %lx\n", sc
);
1623 buf
= HeapAlloc(GetProcessHeap(), 0, sizeof(AQBuffer
));
1625 AudioQueueFreeBuffer(This
->aqueue
, This
->public_buffer
);
1626 This
->public_buffer
= NULL
;
1627 OSSpinLockUnlock(&This
->lock
);
1628 return E_OUTOFMEMORY
;
1630 buf
->buf
= This
->public_buffer
;
1631 This
->public_buffer
->mUserData
= buf
;
1634 *data
= This
->public_buffer
->mAudioData
;
1636 This
->getbuf_last
= TRUE
;
1638 OSSpinLockUnlock(&This
->lock
);
1643 static HRESULT WINAPI
AudioRenderClient_ReleaseBuffer(
1644 IAudioRenderClient
*iface
, UINT32 frames
, DWORD flags
)
1646 ACImpl
*This
= impl_from_IAudioRenderClient(iface
);
1649 TRACE("(%p)->(%u, %x)\n", This
, frames
, flags
);
1651 OSSpinLockLock(&This
->lock
);
1653 if(!This
->getbuf_last
){
1654 OSSpinLockUnlock(&This
->lock
);
1655 return AUDCLNT_E_OUT_OF_ORDER
;
1658 if(flags
& AUDCLNT_BUFFERFLAGS_SILENT
){
1659 WAVEFORMATEXTENSIBLE
*fmtex
= (WAVEFORMATEXTENSIBLE
*)This
->fmt
;
1660 if((This
->fmt
->wFormatTag
== WAVE_FORMAT_PCM
||
1661 (This
->fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
1662 IsEqualGUID(&fmtex
->SubFormat
, &KSDATAFORMAT_SUBTYPE_PCM
))) &&
1663 This
->fmt
->wBitsPerSample
== 8)
1664 memset(This
->public_buffer
->mAudioData
, 128,
1665 frames
* This
->fmt
->nBlockAlign
);
1667 memset(This
->public_buffer
->mAudioData
, 0,
1668 frames
* This
->fmt
->nBlockAlign
);
1671 This
->public_buffer
->mAudioDataByteSize
= frames
* This
->fmt
->nBlockAlign
;
1673 sc
= AudioQueueEnqueueBuffer(This
->aqueue
, This
->public_buffer
, 0, NULL
);
1675 OSSpinLockUnlock(&This
->lock
);
1676 WARN("Unable to enqueue buffer: %lx\n", sc
);
1680 if(This
->playing
== StateStopped
)
1681 AudioQueuePrime(This
->aqueue
, 0, NULL
);
1683 This
->public_buffer
= NULL
;
1684 This
->getbuf_last
= FALSE
;
1685 This
->written_frames
+= frames
;
1686 This
->inbuf_frames
+= frames
;
1688 OSSpinLockUnlock(&This
->lock
);
1693 static const IAudioRenderClientVtbl AudioRenderClient_Vtbl
= {
1694 AudioRenderClient_QueryInterface
,
1695 AudioRenderClient_AddRef
,
1696 AudioRenderClient_Release
,
1697 AudioRenderClient_GetBuffer
,
1698 AudioRenderClient_ReleaseBuffer
1701 static HRESULT WINAPI
AudioCaptureClient_QueryInterface(
1702 IAudioCaptureClient
*iface
, REFIID riid
, void **ppv
)
1704 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
1710 if(IsEqualIID(riid
, &IID_IUnknown
) ||
1711 IsEqualIID(riid
, &IID_IAudioCaptureClient
))
1714 IUnknown_AddRef((IUnknown
*)*ppv
);
1718 WARN("Unknown interface %s\n", debugstr_guid(riid
));
1719 return E_NOINTERFACE
;
1722 static ULONG WINAPI
AudioCaptureClient_AddRef(IAudioCaptureClient
*iface
)
1724 ACImpl
*This
= impl_from_IAudioCaptureClient(iface
);
1725 return IAudioClient_AddRef(&This
->IAudioClient_iface
);
1728 static ULONG WINAPI
AudioCaptureClient_Release(IAudioCaptureClient
*iface
)
1730 ACImpl
*This
= impl_from_IAudioCaptureClient(iface
);
1731 return IAudioClient_Release(&This
->IAudioClient_iface
);
1734 static HRESULT WINAPI
AudioCaptureClient_GetBuffer(IAudioCaptureClient
*iface
,
1735 BYTE
**data
, UINT32
*frames
, DWORD
*flags
, UINT64
*devpos
,
1738 ACImpl
*This
= impl_from_IAudioCaptureClient(iface
);
1740 TRACE("(%p)->(%p, %p, %p, %p, %p)\n", This
, data
, frames
, flags
,
1743 if(!data
|| !frames
|| !flags
)
1746 OSSpinLockLock(&This
->lock
);
1748 if(This
->getbuf_last
){
1749 OSSpinLockUnlock(&This
->lock
);
1750 return AUDCLNT_E_OUT_OF_ORDER
;
1753 if(This
->public_buffer
){
1754 *data
= This
->public_buffer
->mAudioData
;
1756 This
->public_buffer
->mAudioDataByteSize
/ This
->fmt
->nBlockAlign
;
1758 struct list
*head
= list_head(&This
->avail_buffers
);
1763 AQBuffer
*buf
= LIST_ENTRY(head
, AQBuffer
, entry
);
1764 This
->public_buffer
= buf
->buf
;
1765 *data
= This
->public_buffer
->mAudioData
;
1767 This
->public_buffer
->mAudioDataByteSize
/ This
->fmt
->nBlockAlign
;
1768 list_remove(&buf
->entry
);
1773 This
->written_frames
+= *frames
;
1774 This
->inbuf_frames
-= *frames
;
1775 This
->getbuf_last
= TRUE
;
1777 if(devpos
|| qpcpos
)
1778 AudioClock_GetPosition_nolock(This
, devpos
, qpcpos
, FALSE
);
1780 OSSpinLockUnlock(&This
->lock
);
1782 return *frames
? S_OK
: AUDCLNT_S_BUFFER_EMPTY
;
1785 static HRESULT WINAPI
AudioCaptureClient_ReleaseBuffer(
1786 IAudioCaptureClient
*iface
, UINT32 done
)
1788 ACImpl
*This
= impl_from_IAudioCaptureClient(iface
);
1789 UINT32 pbuf_frames
=
1790 This
->public_buffer
->mAudioDataByteSize
/ This
->fmt
->nBlockAlign
;
1793 TRACE("(%p)->(%u)\n", This
, done
);
1795 OSSpinLockLock(&This
->lock
);
1797 if(!This
->getbuf_last
){
1798 OSSpinLockUnlock(&This
->lock
);
1799 return AUDCLNT_E_OUT_OF_ORDER
;
1802 if(done
!= 0 && done
!= pbuf_frames
){
1803 OSSpinLockUnlock(&This
->lock
);
1804 return AUDCLNT_E_INVALID_SIZE
;
1808 sc
= AudioQueueEnqueueBuffer(This
->aqueue
, This
->public_buffer
,
1811 WARN("Unable to enqueue buffer: %lx\n", sc
);
1812 This
->public_buffer
= NULL
;
1815 This
->getbuf_last
= FALSE
;
1817 OSSpinLockUnlock(&This
->lock
);
1822 static HRESULT WINAPI
AudioCaptureClient_GetNextPacketSize(
1823 IAudioCaptureClient
*iface
, UINT32
*frames
)
1825 ACImpl
*This
= impl_from_IAudioCaptureClient(iface
);
1829 TRACE("(%p)->(%p)\n", This
, frames
);
1834 OSSpinLockLock(&This
->lock
);
1836 head
= list_head(&This
->avail_buffers
);
1839 *frames
= This
->bufsize_frames
/ CAPTURE_BUFFERS
;
1840 OSSpinLockUnlock(&This
->lock
);
1844 buf
= LIST_ENTRY(head
, AQBuffer
, entry
);
1845 *frames
= buf
->buf
->mAudioDataByteSize
/ This
->fmt
->nBlockAlign
;
1847 OSSpinLockUnlock(&This
->lock
);
1852 static const IAudioCaptureClientVtbl AudioCaptureClient_Vtbl
=
1854 AudioCaptureClient_QueryInterface
,
1855 AudioCaptureClient_AddRef
,
1856 AudioCaptureClient_Release
,
1857 AudioCaptureClient_GetBuffer
,
1858 AudioCaptureClient_ReleaseBuffer
,
1859 AudioCaptureClient_GetNextPacketSize
1862 static HRESULT WINAPI
AudioClock_QueryInterface(IAudioClock
*iface
,
1863 REFIID riid
, void **ppv
)
1865 ACImpl
*This
= impl_from_IAudioClock(iface
);
1867 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
1873 if(IsEqualIID(riid
, &IID_IUnknown
) || IsEqualIID(riid
, &IID_IAudioClock
))
1875 else if(IsEqualIID(riid
, &IID_IAudioClock2
))
1876 *ppv
= &This
->IAudioClock2_iface
;
1878 IUnknown_AddRef((IUnknown
*)*ppv
);
1882 WARN("Unknown interface %s\n", debugstr_guid(riid
));
1883 return E_NOINTERFACE
;
1886 static ULONG WINAPI
AudioClock_AddRef(IAudioClock
*iface
)
1888 ACImpl
*This
= impl_from_IAudioClock(iface
);
1889 return IAudioClient_AddRef(&This
->IAudioClient_iface
);
1892 static ULONG WINAPI
AudioClock_Release(IAudioClock
*iface
)
1894 ACImpl
*This
= impl_from_IAudioClock(iface
);
1895 return IAudioClient_Release(&This
->IAudioClient_iface
);
1898 static HRESULT WINAPI
AudioClock_GetFrequency(IAudioClock
*iface
, UINT64
*freq
)
1900 ACImpl
*This
= impl_from_IAudioClock(iface
);
1902 TRACE("(%p)->(%p)\n", This
, freq
);
1904 *freq
= This
->fmt
->nSamplesPerSec
;
1909 static HRESULT
AudioClock_GetPosition_nolock(ACImpl
*This
,
1910 UINT64
*pos
, UINT64
*qpctime
, BOOL raw
)
1912 AudioTimeStamp time
;
1915 sc
= AudioQueueGetCurrentTime(This
->aqueue
, NULL
, &time
, NULL
);
1916 if(sc
== kAudioQueueErr_InvalidRunState
){
1918 }else if(sc
== noErr
){
1919 if(!(time
.mFlags
& kAudioTimeStampSampleTimeValid
)){
1920 FIXME("Sample time not valid, should calculate from something else\n");
1925 *pos
= time
.mSampleTime
;
1927 *pos
= time
.mSampleTime
- This
->last_time
;
1929 WARN("Unable to get current time: %lx\n", sc
);
1934 LARGE_INTEGER stamp
, freq
;
1935 QueryPerformanceCounter(&stamp
);
1936 QueryPerformanceFrequency(&freq
);
1937 *qpctime
= (stamp
.QuadPart
* (INT64
)10000000) / freq
.QuadPart
;
1943 static HRESULT WINAPI
AudioClock_GetPosition(IAudioClock
*iface
, UINT64
*pos
,
1946 ACImpl
*This
= impl_from_IAudioClock(iface
);
1949 TRACE("(%p)->(%p, %p)\n", This
, pos
, qpctime
);
1954 OSSpinLockLock(&This
->lock
);
1956 hr
= AudioClock_GetPosition_nolock(This
, pos
, qpctime
, FALSE
);
1958 OSSpinLockUnlock(&This
->lock
);
1963 static HRESULT WINAPI
AudioClock_GetCharacteristics(IAudioClock
*iface
,
1966 ACImpl
*This
= impl_from_IAudioClock(iface
);
1968 TRACE("(%p)->(%p)\n", This
, chars
);
1973 *chars
= AUDIOCLOCK_CHARACTERISTIC_FIXED_FREQ
;
1978 static const IAudioClockVtbl AudioClock_Vtbl
=
1980 AudioClock_QueryInterface
,
1983 AudioClock_GetFrequency
,
1984 AudioClock_GetPosition
,
1985 AudioClock_GetCharacteristics
1988 static HRESULT WINAPI
AudioClock2_QueryInterface(IAudioClock2
*iface
,
1989 REFIID riid
, void **ppv
)
1991 ACImpl
*This
= impl_from_IAudioClock2(iface
);
1992 return IAudioClock_QueryInterface(&This
->IAudioClock_iface
, riid
, ppv
);
1995 static ULONG WINAPI
AudioClock2_AddRef(IAudioClock2
*iface
)
1997 ACImpl
*This
= impl_from_IAudioClock2(iface
);
1998 return IAudioClient_AddRef(&This
->IAudioClient_iface
);
2001 static ULONG WINAPI
AudioClock2_Release(IAudioClock2
*iface
)
2003 ACImpl
*This
= impl_from_IAudioClock2(iface
);
2004 return IAudioClient_Release(&This
->IAudioClient_iface
);
2007 static HRESULT WINAPI
AudioClock2_GetDevicePosition(IAudioClock2
*iface
,
2008 UINT64
*pos
, UINT64
*qpctime
)
2010 ACImpl
*This
= impl_from_IAudioClock2(iface
);
2012 FIXME("(%p)->(%p, %p)\n", This
, pos
, qpctime
);
2017 static const IAudioClock2Vtbl AudioClock2_Vtbl
=
2019 AudioClock2_QueryInterface
,
2021 AudioClock2_Release
,
2022 AudioClock2_GetDevicePosition
2025 static AudioSessionWrapper
*AudioSessionWrapper_Create(ACImpl
*client
)
2027 AudioSessionWrapper
*ret
;
2029 ret
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
2030 sizeof(AudioSessionWrapper
));
2034 ret
->IAudioSessionControl2_iface
.lpVtbl
= &AudioSessionControl2_Vtbl
;
2035 ret
->ISimpleAudioVolume_iface
.lpVtbl
= &SimpleAudioVolume_Vtbl
;
2036 ret
->IChannelAudioVolume_iface
.lpVtbl
= &ChannelAudioVolume_Vtbl
;
2038 ret
->client
= client
;
2040 ret
->session
= client
->session
;
2041 AudioClient_AddRef(&client
->IAudioClient_iface
);
2047 static HRESULT WINAPI
AudioSessionControl_QueryInterface(
2048 IAudioSessionControl2
*iface
, REFIID riid
, void **ppv
)
2050 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
2056 if(IsEqualIID(riid
, &IID_IUnknown
) ||
2057 IsEqualIID(riid
, &IID_IAudioSessionControl
) ||
2058 IsEqualIID(riid
, &IID_IAudioSessionControl2
))
2061 IUnknown_AddRef((IUnknown
*)*ppv
);
2065 WARN("Unknown interface %s\n", debugstr_guid(riid
));
2066 return E_NOINTERFACE
;
2069 static ULONG WINAPI
AudioSessionControl_AddRef(IAudioSessionControl2
*iface
)
2071 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2073 ref
= InterlockedIncrement(&This
->ref
);
2074 TRACE("(%p) Refcount now %u\n", This
, ref
);
2078 static ULONG WINAPI
AudioSessionControl_Release(IAudioSessionControl2
*iface
)
2080 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2082 ref
= InterlockedDecrement(&This
->ref
);
2083 TRACE("(%p) Refcount now %u\n", This
, ref
);
2085 OSSpinLockLock(&This
->client
->lock
);
2086 This
->client
->session_wrapper
= NULL
;
2087 OSSpinLockUnlock(&This
->client
->lock
);
2088 AudioClient_Release(&This
->client
->IAudioClient_iface
);
2089 HeapFree(GetProcessHeap(), 0, This
);
2094 static HRESULT WINAPI
AudioSessionControl_GetState(IAudioSessionControl2
*iface
,
2095 AudioSessionState
*state
)
2097 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2100 TRACE("(%p)->(%p)\n", This
, state
);
2103 return NULL_PTR_ERR
;
2105 EnterCriticalSection(&g_sessions_lock
);
2107 if(list_empty(&This
->session
->clients
)){
2108 *state
= AudioSessionStateExpired
;
2109 LeaveCriticalSection(&g_sessions_lock
);
2113 LIST_FOR_EACH_ENTRY(client
, &This
->session
->clients
, ACImpl
, entry
){
2114 OSSpinLockLock(&client
->lock
);
2115 if(client
->playing
== StatePlaying
||
2116 client
->playing
== StateInTransition
){
2117 *state
= AudioSessionStateActive
;
2118 OSSpinLockUnlock(&client
->lock
);
2119 LeaveCriticalSection(&g_sessions_lock
);
2122 OSSpinLockUnlock(&client
->lock
);
2125 LeaveCriticalSection(&g_sessions_lock
);
2127 *state
= AudioSessionStateInactive
;
2132 static HRESULT WINAPI
AudioSessionControl_GetDisplayName(
2133 IAudioSessionControl2
*iface
, WCHAR
**name
)
2135 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2137 FIXME("(%p)->(%p) - stub\n", This
, name
);
2142 static HRESULT WINAPI
AudioSessionControl_SetDisplayName(
2143 IAudioSessionControl2
*iface
, const WCHAR
*name
, const GUID
*session
)
2145 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2147 FIXME("(%p)->(%p, %s) - stub\n", This
, name
, debugstr_guid(session
));
2152 static HRESULT WINAPI
AudioSessionControl_GetIconPath(
2153 IAudioSessionControl2
*iface
, WCHAR
**path
)
2155 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2157 FIXME("(%p)->(%p) - stub\n", This
, path
);
2162 static HRESULT WINAPI
AudioSessionControl_SetIconPath(
2163 IAudioSessionControl2
*iface
, const WCHAR
*path
, const GUID
*session
)
2165 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2167 FIXME("(%p)->(%p, %s) - stub\n", This
, path
, debugstr_guid(session
));
2172 static HRESULT WINAPI
AudioSessionControl_GetGroupingParam(
2173 IAudioSessionControl2
*iface
, GUID
*group
)
2175 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2177 FIXME("(%p)->(%p) - stub\n", This
, group
);
2182 static HRESULT WINAPI
AudioSessionControl_SetGroupingParam(
2183 IAudioSessionControl2
*iface
, GUID
*group
, const GUID
*session
)
2185 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2187 FIXME("(%p)->(%s, %s) - stub\n", This
, debugstr_guid(group
),
2188 debugstr_guid(session
));
2193 static HRESULT WINAPI
AudioSessionControl_RegisterAudioSessionNotification(
2194 IAudioSessionControl2
*iface
, IAudioSessionEvents
*events
)
2196 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2198 FIXME("(%p)->(%p) - stub\n", This
, events
);
2203 static HRESULT WINAPI
AudioSessionControl_UnregisterAudioSessionNotification(
2204 IAudioSessionControl2
*iface
, IAudioSessionEvents
*events
)
2206 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2208 FIXME("(%p)->(%p) - stub\n", This
, events
);
2213 static HRESULT WINAPI
AudioSessionControl_GetSessionIdentifier(
2214 IAudioSessionControl2
*iface
, WCHAR
**id
)
2216 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2218 FIXME("(%p)->(%p) - stub\n", This
, id
);
2223 static HRESULT WINAPI
AudioSessionControl_GetSessionInstanceIdentifier(
2224 IAudioSessionControl2
*iface
, WCHAR
**id
)
2226 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2228 FIXME("(%p)->(%p) - stub\n", This
, id
);
2233 static HRESULT WINAPI
AudioSessionControl_GetProcessId(
2234 IAudioSessionControl2
*iface
, DWORD
*pid
)
2236 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2238 TRACE("(%p)->(%p)\n", This
, pid
);
2243 *pid
= GetCurrentProcessId();
2248 static HRESULT WINAPI
AudioSessionControl_IsSystemSoundsSession(
2249 IAudioSessionControl2
*iface
)
2251 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2253 TRACE("(%p)\n", This
);
2258 static HRESULT WINAPI
AudioSessionControl_SetDuckingPreference(
2259 IAudioSessionControl2
*iface
, BOOL optout
)
2261 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2263 TRACE("(%p)->(%d)\n", This
, optout
);
2268 static const IAudioSessionControl2Vtbl AudioSessionControl2_Vtbl
=
2270 AudioSessionControl_QueryInterface
,
2271 AudioSessionControl_AddRef
,
2272 AudioSessionControl_Release
,
2273 AudioSessionControl_GetState
,
2274 AudioSessionControl_GetDisplayName
,
2275 AudioSessionControl_SetDisplayName
,
2276 AudioSessionControl_GetIconPath
,
2277 AudioSessionControl_SetIconPath
,
2278 AudioSessionControl_GetGroupingParam
,
2279 AudioSessionControl_SetGroupingParam
,
2280 AudioSessionControl_RegisterAudioSessionNotification
,
2281 AudioSessionControl_UnregisterAudioSessionNotification
,
2282 AudioSessionControl_GetSessionIdentifier
,
2283 AudioSessionControl_GetSessionInstanceIdentifier
,
2284 AudioSessionControl_GetProcessId
,
2285 AudioSessionControl_IsSystemSoundsSession
,
2286 AudioSessionControl_SetDuckingPreference
2289 /* index == -1 means set all channels, otherwise sets only the given channel */
2290 static HRESULT
ca_setvol(ACImpl
*This
, UINT32 index
)
2295 if(index
== (UINT32
)-1){
2298 for(i
= 0; i
< This
->fmt
->nChannels
; ++i
){
2300 hr
= ca_setvol(This
, i
);
2307 level
= This
->session
->master_vol
* This
->session
->channel_vols
[index
] *
2310 sc
= AudioQueueSetParameter(This
->aqueue
, kAudioQueueParam_Volume
, level
);
2312 WARN("Setting _Volume property failed: %lx\n", sc
);
2319 static HRESULT
ca_session_setvol(AudioSession
*session
, UINT32 index
)
2324 LIST_FOR_EACH_ENTRY(client
, &session
->clients
, ACImpl
, entry
){
2326 hr
= ca_setvol(client
, index
);
2334 static HRESULT WINAPI
SimpleAudioVolume_QueryInterface(
2335 ISimpleAudioVolume
*iface
, REFIID riid
, void **ppv
)
2337 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
2343 if(IsEqualIID(riid
, &IID_IUnknown
) ||
2344 IsEqualIID(riid
, &IID_ISimpleAudioVolume
))
2347 IUnknown_AddRef((IUnknown
*)*ppv
);
2351 WARN("Unknown interface %s\n", debugstr_guid(riid
));
2352 return E_NOINTERFACE
;
2355 static ULONG WINAPI
SimpleAudioVolume_AddRef(ISimpleAudioVolume
*iface
)
2357 AudioSessionWrapper
*This
= impl_from_ISimpleAudioVolume(iface
);
2358 return AudioSessionControl_AddRef(&This
->IAudioSessionControl2_iface
);
2361 static ULONG WINAPI
SimpleAudioVolume_Release(ISimpleAudioVolume
*iface
)
2363 AudioSessionWrapper
*This
= impl_from_ISimpleAudioVolume(iface
);
2364 return AudioSessionControl_Release(&This
->IAudioSessionControl2_iface
);
2367 static HRESULT WINAPI
SimpleAudioVolume_SetMasterVolume(
2368 ISimpleAudioVolume
*iface
, float level
, const GUID
*context
)
2370 AudioSessionWrapper
*This
= impl_from_ISimpleAudioVolume(iface
);
2371 AudioSession
*session
= This
->session
;
2374 TRACE("(%p)->(%f, %s)\n", session
, level
, wine_dbgstr_guid(context
));
2376 if(level
< 0.f
|| level
> 1.f
)
2377 return E_INVALIDARG
;
2380 FIXME("Notifications not supported yet\n");
2382 EnterCriticalSection(&session
->lock
);
2384 session
->master_vol
= level
;
2386 ret
= ca_session_setvol(session
, -1);
2388 LeaveCriticalSection(&session
->lock
);
2393 static HRESULT WINAPI
SimpleAudioVolume_GetMasterVolume(
2394 ISimpleAudioVolume
*iface
, float *level
)
2396 AudioSessionWrapper
*This
= impl_from_ISimpleAudioVolume(iface
);
2397 AudioSession
*session
= This
->session
;
2399 TRACE("(%p)->(%p)\n", session
, level
);
2402 return NULL_PTR_ERR
;
2404 *level
= session
->master_vol
;
2409 static HRESULT WINAPI
SimpleAudioVolume_SetMute(ISimpleAudioVolume
*iface
,
2410 BOOL mute
, const GUID
*context
)
2412 AudioSessionWrapper
*This
= impl_from_ISimpleAudioVolume(iface
);
2413 AudioSession
*session
= This
->session
;
2415 FIXME("(%p)->(%u, %p) - stub\n", session
, mute
, context
);
2420 static HRESULT WINAPI
SimpleAudioVolume_GetMute(ISimpleAudioVolume
*iface
,
2423 AudioSessionWrapper
*This
= impl_from_ISimpleAudioVolume(iface
);
2424 AudioSession
*session
= This
->session
;
2426 FIXME("(%p)->(%p) - stub\n", session
, mute
);
2429 return NULL_PTR_ERR
;
2434 static const ISimpleAudioVolumeVtbl SimpleAudioVolume_Vtbl
=
2436 SimpleAudioVolume_QueryInterface
,
2437 SimpleAudioVolume_AddRef
,
2438 SimpleAudioVolume_Release
,
2439 SimpleAudioVolume_SetMasterVolume
,
2440 SimpleAudioVolume_GetMasterVolume
,
2441 SimpleAudioVolume_SetMute
,
2442 SimpleAudioVolume_GetMute
2445 static HRESULT WINAPI
AudioStreamVolume_QueryInterface(
2446 IAudioStreamVolume
*iface
, REFIID riid
, void **ppv
)
2448 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
2454 if(IsEqualIID(riid
, &IID_IUnknown
) ||
2455 IsEqualIID(riid
, &IID_IAudioStreamVolume
))
2458 IUnknown_AddRef((IUnknown
*)*ppv
);
2462 WARN("Unknown interface %s\n", debugstr_guid(riid
));
2463 return E_NOINTERFACE
;
2466 static ULONG WINAPI
AudioStreamVolume_AddRef(IAudioStreamVolume
*iface
)
2468 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
2469 return IAudioClient_AddRef(&This
->IAudioClient_iface
);
2472 static ULONG WINAPI
AudioStreamVolume_Release(IAudioStreamVolume
*iface
)
2474 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
2475 return IAudioClient_Release(&This
->IAudioClient_iface
);
2478 static HRESULT WINAPI
AudioStreamVolume_GetChannelCount(
2479 IAudioStreamVolume
*iface
, UINT32
*out
)
2481 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
2483 TRACE("(%p)->(%p)\n", This
, out
);
2488 *out
= This
->fmt
->nChannels
;
2493 static HRESULT WINAPI
AudioStreamVolume_SetChannelVolume(
2494 IAudioStreamVolume
*iface
, UINT32 index
, float level
)
2496 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
2499 TRACE("(%p)->(%d, %f)\n", This
, index
, level
);
2501 if(level
< 0.f
|| level
> 1.f
)
2502 return E_INVALIDARG
;
2504 if(index
>= This
->fmt
->nChannels
)
2505 return E_INVALIDARG
;
2507 OSSpinLockLock(&This
->lock
);
2509 This
->vols
[index
] = level
;
2511 WARN("AudioQueue doesn't support per-channel volume control\n");
2512 ret
= ca_setvol(This
, index
);
2514 OSSpinLockUnlock(&This
->lock
);
2519 static HRESULT WINAPI
AudioStreamVolume_GetChannelVolume(
2520 IAudioStreamVolume
*iface
, UINT32 index
, float *level
)
2522 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
2524 TRACE("(%p)->(%d, %p)\n", This
, index
, level
);
2529 if(index
>= This
->fmt
->nChannels
)
2530 return E_INVALIDARG
;
2532 *level
= This
->vols
[index
];
2537 static HRESULT WINAPI
AudioStreamVolume_SetAllVolumes(
2538 IAudioStreamVolume
*iface
, UINT32 count
, const float *levels
)
2540 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
2544 TRACE("(%p)->(%d, %p)\n", This
, count
, levels
);
2549 if(count
!= This
->fmt
->nChannels
)
2550 return E_INVALIDARG
;
2552 OSSpinLockLock(&This
->lock
);
2554 for(i
= 0; i
< count
; ++i
)
2555 This
->vols
[i
] = levels
[i
];
2557 ret
= ca_setvol(This
, -1);
2559 OSSpinLockUnlock(&This
->lock
);
2564 static HRESULT WINAPI
AudioStreamVolume_GetAllVolumes(
2565 IAudioStreamVolume
*iface
, UINT32 count
, float *levels
)
2567 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
2570 TRACE("(%p)->(%d, %p)\n", This
, count
, levels
);
2575 if(count
!= This
->fmt
->nChannels
)
2576 return E_INVALIDARG
;
2578 OSSpinLockLock(&This
->lock
);
2580 for(i
= 0; i
< count
; ++i
)
2581 levels
[i
] = This
->vols
[i
];
2583 OSSpinLockUnlock(&This
->lock
);
2588 static const IAudioStreamVolumeVtbl AudioStreamVolume_Vtbl
=
2590 AudioStreamVolume_QueryInterface
,
2591 AudioStreamVolume_AddRef
,
2592 AudioStreamVolume_Release
,
2593 AudioStreamVolume_GetChannelCount
,
2594 AudioStreamVolume_SetChannelVolume
,
2595 AudioStreamVolume_GetChannelVolume
,
2596 AudioStreamVolume_SetAllVolumes
,
2597 AudioStreamVolume_GetAllVolumes
2600 static HRESULT WINAPI
ChannelAudioVolume_QueryInterface(
2601 IChannelAudioVolume
*iface
, REFIID riid
, void **ppv
)
2603 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
2609 if(IsEqualIID(riid
, &IID_IUnknown
) ||
2610 IsEqualIID(riid
, &IID_IChannelAudioVolume
))
2613 IUnknown_AddRef((IUnknown
*)*ppv
);
2617 WARN("Unknown interface %s\n", debugstr_guid(riid
));
2618 return E_NOINTERFACE
;
2621 static ULONG WINAPI
ChannelAudioVolume_AddRef(IChannelAudioVolume
*iface
)
2623 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
2624 return AudioSessionControl_AddRef(&This
->IAudioSessionControl2_iface
);
2627 static ULONG WINAPI
ChannelAudioVolume_Release(IChannelAudioVolume
*iface
)
2629 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
2630 return AudioSessionControl_Release(&This
->IAudioSessionControl2_iface
);
2633 static HRESULT WINAPI
ChannelAudioVolume_GetChannelCount(
2634 IChannelAudioVolume
*iface
, UINT32
*out
)
2636 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
2637 AudioSession
*session
= This
->session
;
2639 TRACE("(%p)->(%p)\n", session
, out
);
2642 return NULL_PTR_ERR
;
2644 *out
= session
->channel_count
;
2649 static HRESULT WINAPI
ChannelAudioVolume_SetChannelVolume(
2650 IChannelAudioVolume
*iface
, UINT32 index
, float level
,
2651 const GUID
*context
)
2653 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
2654 AudioSession
*session
= This
->session
;
2657 TRACE("(%p)->(%d, %f, %s)\n", session
, index
, level
,
2658 wine_dbgstr_guid(context
));
2660 if(level
< 0.f
|| level
> 1.f
)
2661 return E_INVALIDARG
;
2663 if(index
>= session
->channel_count
)
2664 return E_INVALIDARG
;
2667 FIXME("Notifications not supported yet\n");
2669 EnterCriticalSection(&session
->lock
);
2671 session
->channel_vols
[index
] = level
;
2673 WARN("AudioQueue doesn't support per-channel volume control\n");
2674 ret
= ca_session_setvol(session
, index
);
2676 LeaveCriticalSection(&session
->lock
);
2681 static HRESULT WINAPI
ChannelAudioVolume_GetChannelVolume(
2682 IChannelAudioVolume
*iface
, UINT32 index
, float *level
)
2684 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
2685 AudioSession
*session
= This
->session
;
2687 TRACE("(%p)->(%d, %p)\n", session
, index
, level
);
2690 return NULL_PTR_ERR
;
2692 if(index
>= session
->channel_count
)
2693 return E_INVALIDARG
;
2695 *level
= session
->channel_vols
[index
];
2700 static HRESULT WINAPI
ChannelAudioVolume_SetAllVolumes(
2701 IChannelAudioVolume
*iface
, UINT32 count
, const float *levels
,
2702 const GUID
*context
)
2704 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
2705 AudioSession
*session
= This
->session
;
2709 TRACE("(%p)->(%d, %p, %s)\n", session
, count
, levels
,
2710 wine_dbgstr_guid(context
));
2713 return NULL_PTR_ERR
;
2715 if(count
!= session
->channel_count
)
2716 return E_INVALIDARG
;
2719 FIXME("Notifications not supported yet\n");
2721 EnterCriticalSection(&session
->lock
);
2723 for(i
= 0; i
< count
; ++i
)
2724 session
->channel_vols
[i
] = levels
[i
];
2726 ret
= ca_session_setvol(session
, -1);
2728 LeaveCriticalSection(&session
->lock
);
2733 static HRESULT WINAPI
ChannelAudioVolume_GetAllVolumes(
2734 IChannelAudioVolume
*iface
, UINT32 count
, float *levels
)
2736 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
2737 AudioSession
*session
= This
->session
;
2740 TRACE("(%p)->(%d, %p)\n", session
, count
, levels
);
2743 return NULL_PTR_ERR
;
2745 if(count
!= session
->channel_count
)
2746 return E_INVALIDARG
;
2748 for(i
= 0; i
< count
; ++i
)
2749 levels
[i
] = session
->channel_vols
[i
];
2754 static const IChannelAudioVolumeVtbl ChannelAudioVolume_Vtbl
=
2756 ChannelAudioVolume_QueryInterface
,
2757 ChannelAudioVolume_AddRef
,
2758 ChannelAudioVolume_Release
,
2759 ChannelAudioVolume_GetChannelCount
,
2760 ChannelAudioVolume_SetChannelVolume
,
2761 ChannelAudioVolume_GetChannelVolume
,
2762 ChannelAudioVolume_SetAllVolumes
,
2763 ChannelAudioVolume_GetAllVolumes
2766 HRESULT WINAPI
AudioSessionManager_QueryInterface(IAudioSessionManager2
*iface
,
2767 REFIID riid
, void **ppv
)
2769 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
2775 if(IsEqualIID(riid
, &IID_IUnknown
) ||
2776 IsEqualIID(riid
, &IID_IAudioSessionManager
) ||
2777 IsEqualIID(riid
, &IID_IAudioSessionManager2
))
2780 IUnknown_AddRef((IUnknown
*)*ppv
);
2784 WARN("Unknown interface %s\n", debugstr_guid(riid
));
2785 return E_NOINTERFACE
;
2788 ULONG WINAPI
AudioSessionManager_AddRef(IAudioSessionManager2
*iface
)
2790 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
2792 ref
= InterlockedIncrement(&This
->ref
);
2793 TRACE("(%p) Refcount now %u\n", This
, ref
);
2797 ULONG WINAPI
AudioSessionManager_Release(IAudioSessionManager2
*iface
)
2799 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
2801 ref
= InterlockedDecrement(&This
->ref
);
2802 TRACE("(%p) Refcount now %u\n", This
, ref
);
2804 HeapFree(GetProcessHeap(), 0, This
);
2808 HRESULT WINAPI
AudioSessionManager_GetAudioSessionControl(
2809 IAudioSessionManager2
*iface
, const GUID
*session_guid
, DWORD flags
,
2810 IAudioSessionControl
**out
)
2812 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
2813 AudioSession
*session
;
2814 AudioSessionWrapper
*wrapper
;
2817 TRACE("(%p)->(%s, %x, %p)\n", This
, debugstr_guid(session_guid
),
2820 hr
= get_audio_session(session_guid
, This
->device
, 0, &session
);
2824 wrapper
= AudioSessionWrapper_Create(NULL
);
2826 return E_OUTOFMEMORY
;
2828 wrapper
->session
= session
;
2830 *out
= (IAudioSessionControl
*)&wrapper
->IAudioSessionControl2_iface
;
2835 HRESULT WINAPI
AudioSessionManager_GetSimpleAudioVolume(
2836 IAudioSessionManager2
*iface
, const GUID
*session_guid
, DWORD flags
,
2837 ISimpleAudioVolume
**out
)
2839 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
2840 AudioSession
*session
;
2841 AudioSessionWrapper
*wrapper
;
2844 TRACE("(%p)->(%s, %x, %p)\n", This
, debugstr_guid(session_guid
),
2847 hr
= get_audio_session(session_guid
, This
->device
, 0, &session
);
2851 wrapper
= AudioSessionWrapper_Create(NULL
);
2853 return E_OUTOFMEMORY
;
2855 wrapper
->session
= session
;
2857 *out
= &wrapper
->ISimpleAudioVolume_iface
;
2862 HRESULT WINAPI
AudioSessionManager_GetSessionEnumerator(
2863 IAudioSessionManager2
*iface
, IAudioSessionEnumerator
**out
)
2865 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
2866 FIXME("(%p)->(%p) - stub\n", This
, out
);
2870 HRESULT WINAPI
AudioSessionManager_RegisterSessionNotification(
2871 IAudioSessionManager2
*iface
, IAudioSessionNotification
*notification
)
2873 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
2874 FIXME("(%p)->(%p) - stub\n", This
, notification
);
2878 HRESULT WINAPI
AudioSessionManager_UnregisterSessionNotification(
2879 IAudioSessionManager2
*iface
, IAudioSessionNotification
*notification
)
2881 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
2882 FIXME("(%p)->(%p) - stub\n", This
, notification
);
2886 HRESULT WINAPI
AudioSessionManager_RegisterDuckNotification(
2887 IAudioSessionManager2
*iface
, const WCHAR
*session_id
,
2888 IAudioVolumeDuckNotification
*notification
)
2890 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
2891 FIXME("(%p)->(%p) - stub\n", This
, notification
);
2895 HRESULT WINAPI
AudioSessionManager_UnregisterDuckNotification(
2896 IAudioSessionManager2
*iface
,
2897 IAudioVolumeDuckNotification
*notification
)
2899 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
2900 FIXME("(%p)->(%p) - stub\n", This
, notification
);
2904 static const IAudioSessionManager2Vtbl AudioSessionManager2_Vtbl
=
2906 AudioSessionManager_QueryInterface
,
2907 AudioSessionManager_AddRef
,
2908 AudioSessionManager_Release
,
2909 AudioSessionManager_GetAudioSessionControl
,
2910 AudioSessionManager_GetSimpleAudioVolume
,
2911 AudioSessionManager_GetSessionEnumerator
,
2912 AudioSessionManager_RegisterSessionNotification
,
2913 AudioSessionManager_UnregisterSessionNotification
,
2914 AudioSessionManager_RegisterDuckNotification
,
2915 AudioSessionManager_UnregisterDuckNotification
2918 HRESULT WINAPI
AUDDRV_GetAudioSessionManager(IMMDevice
*device
,
2919 IAudioSessionManager2
**out
)
2923 This
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(SessionMgr
));
2925 return E_OUTOFMEMORY
;
2927 This
->IAudioSessionManager2_iface
.lpVtbl
= &AudioSessionManager2_Vtbl
;
2928 This
->device
= device
;
2931 *out
= &This
->IAudioSessionManager2_iface
;