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"
40 #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
{
87 CRITICAL_SECTION lock
;
92 typedef struct _AudioSessionWrapper
{
93 IAudioSessionControl2 IAudioSessionControl2_iface
;
94 IChannelAudioVolume IChannelAudioVolume_iface
;
95 ISimpleAudioVolume ISimpleAudioVolume_iface
;
100 AudioSession
*session
;
101 } AudioSessionWrapper
;
104 IAudioClient IAudioClient_iface
;
105 IAudioRenderClient IAudioRenderClient_iface
;
106 IAudioCaptureClient IAudioCaptureClient_iface
;
107 IAudioClock IAudioClock_iface
;
108 IAudioClock2 IAudioClock2_iface
;
109 IAudioStreamVolume IAudioStreamVolume_iface
;
119 AUDCLNT_SHAREMODE share
;
123 AudioDeviceID adevid
;
124 AudioQueueRef aqueue
;
125 AudioObjectPropertyScope scope
;
127 UINT32 period_ms
, bufsize_frames
, inbuf_frames
;
128 UINT64 last_time
, written_frames
;
129 AudioQueueBufferRef public_buffer
;
133 AudioSession
*session
;
134 AudioSessionWrapper
*session_wrapper
;
138 struct list avail_buffers
;
140 /* We can't use debug printing or {Enter,Leave}CriticalSection from
141 * OSX callback threads, so we use OSX's OSSpinLock for synchronization
142 * instead. OSSpinLock is not a recursive lock, so don't call
143 * synchronized functions while holding the lock. */
153 static const IAudioClientVtbl AudioClient_Vtbl
;
154 static const IAudioRenderClientVtbl AudioRenderClient_Vtbl
;
155 static const IAudioCaptureClientVtbl AudioCaptureClient_Vtbl
;
156 static const IAudioSessionControl2Vtbl AudioSessionControl2_Vtbl
;
157 static const ISimpleAudioVolumeVtbl SimpleAudioVolume_Vtbl
;
158 static const IAudioClockVtbl AudioClock_Vtbl
;
159 static const IAudioClock2Vtbl AudioClock2_Vtbl
;
160 static const IAudioStreamVolumeVtbl AudioStreamVolume_Vtbl
;
161 static const IChannelAudioVolumeVtbl ChannelAudioVolume_Vtbl
;
162 static const IAudioSessionManager2Vtbl AudioSessionManager2_Vtbl
;
164 typedef struct _SessionMgr
{
165 IAudioSessionManager2 IAudioSessionManager2_iface
;
172 static HANDLE g_timer_q
;
174 static CRITICAL_SECTION g_sessions_lock
;
175 static struct list g_sessions
= LIST_INIT(g_sessions
);
177 static HRESULT
AudioClock_GetPosition_nolock(ACImpl
*This
, UINT64
*pos
,
178 UINT64
*qpctime
, BOOL raw
);
179 static AudioSessionWrapper
*AudioSessionWrapper_Create(ACImpl
*client
);
180 static HRESULT
ca_setvol(ACImpl
*This
, UINT32 index
);
182 static inline ACImpl
*impl_from_IAudioClient(IAudioClient
*iface
)
184 return CONTAINING_RECORD(iface
, ACImpl
, IAudioClient_iface
);
187 static inline ACImpl
*impl_from_IAudioRenderClient(IAudioRenderClient
*iface
)
189 return CONTAINING_RECORD(iface
, ACImpl
, IAudioRenderClient_iface
);
192 static inline ACImpl
*impl_from_IAudioCaptureClient(IAudioCaptureClient
*iface
)
194 return CONTAINING_RECORD(iface
, ACImpl
, IAudioCaptureClient_iface
);
197 static inline AudioSessionWrapper
*impl_from_IAudioSessionControl2(IAudioSessionControl2
*iface
)
199 return CONTAINING_RECORD(iface
, AudioSessionWrapper
, IAudioSessionControl2_iface
);
202 static inline AudioSessionWrapper
*impl_from_ISimpleAudioVolume(ISimpleAudioVolume
*iface
)
204 return CONTAINING_RECORD(iface
, AudioSessionWrapper
, ISimpleAudioVolume_iface
);
207 static inline AudioSessionWrapper
*impl_from_IChannelAudioVolume(IChannelAudioVolume
*iface
)
209 return CONTAINING_RECORD(iface
, AudioSessionWrapper
, IChannelAudioVolume_iface
);
212 static inline ACImpl
*impl_from_IAudioClock(IAudioClock
*iface
)
214 return CONTAINING_RECORD(iface
, ACImpl
, IAudioClock_iface
);
217 static inline ACImpl
*impl_from_IAudioClock2(IAudioClock2
*iface
)
219 return CONTAINING_RECORD(iface
, ACImpl
, IAudioClock2_iface
);
222 static inline ACImpl
*impl_from_IAudioStreamVolume(IAudioStreamVolume
*iface
)
224 return CONTAINING_RECORD(iface
, ACImpl
, IAudioStreamVolume_iface
);
227 static inline SessionMgr
*impl_from_IAudioSessionManager2(IAudioSessionManager2
*iface
)
229 return CONTAINING_RECORD(iface
, SessionMgr
, IAudioSessionManager2_iface
);
232 BOOL WINAPI
DllMain(HINSTANCE dll
, DWORD reason
, void *reserved
)
234 if(reason
== DLL_PROCESS_ATTACH
){
235 g_timer_q
= CreateTimerQueue();
239 InitializeCriticalSection(&g_sessions_lock
);
245 /* From <dlls/mmdevapi/mmdevapi.h> */
246 enum DriverPriority
{
247 Priority_Unavailable
= 0,
253 int WINAPI
AUDDRV_GetPriority(void)
255 return Priority_Neutral
;
258 HRESULT WINAPI
AUDDRV_GetEndpointIDs(EDataFlow flow
, WCHAR
***ids
,
259 AudioDeviceID
***keys
, UINT
*num
, UINT
*def_index
)
261 UInt32 devsize
, size
;
262 AudioDeviceID
*devices
;
263 AudioDeviceID default_id
;
264 AudioObjectPropertyAddress addr
;
268 TRACE("%d %p %p %p\n", flow
, ids
, num
, def_index
);
270 addr
.mScope
= kAudioObjectPropertyScopeGlobal
;
271 addr
.mElement
= kAudioObjectPropertyElementMaster
;
273 addr
.mSelector
= kAudioHardwarePropertyDefaultOutputDevice
;
274 else if(flow
== eCapture
)
275 addr
.mSelector
= kAudioHardwarePropertyDefaultInputDevice
;
279 size
= sizeof(default_id
);
280 sc
= AudioObjectGetPropertyData(kAudioObjectSystemObject
, &addr
, 0,
281 NULL
, &size
, &default_id
);
283 WARN("Getting _DefaultInputDevice property failed: %lx\n", sc
);
287 addr
.mSelector
= kAudioHardwarePropertyDevices
;
288 sc
= AudioObjectGetPropertyDataSize(kAudioObjectSystemObject
, &addr
, 0,
291 WARN("Getting _Devices property size failed: %lx\n", sc
);
295 devices
= HeapAlloc(GetProcessHeap(), 0, devsize
);
297 return E_OUTOFMEMORY
;
299 sc
= AudioObjectGetPropertyData(kAudioObjectSystemObject
, &addr
, 0, NULL
,
302 WARN("Getting _Devices property failed: %lx\n", sc
);
303 HeapFree(GetProcessHeap(), 0, devices
);
307 ndevices
= devsize
/ sizeof(AudioDeviceID
);
309 *ids
= HeapAlloc(GetProcessHeap(), 0, ndevices
* sizeof(WCHAR
*));
311 HeapFree(GetProcessHeap(), 0, devices
);
312 return E_OUTOFMEMORY
;
315 *keys
= HeapAlloc(GetProcessHeap(), 0, ndevices
* sizeof(AudioDeviceID
*));
317 HeapFree(GetProcessHeap(), 0, *ids
);
318 HeapFree(GetProcessHeap(), 0, devices
);
319 return E_OUTOFMEMORY
;
323 *def_index
= (UINT
)-1;
324 for(i
= 0; i
< ndevices
; ++i
){
325 AudioBufferList
*buffers
;
330 addr
.mSelector
= kAudioDevicePropertyStreamConfiguration
;
332 addr
.mScope
= kAudioDevicePropertyScopeOutput
;
334 addr
.mScope
= kAudioDevicePropertyScopeInput
;
336 sc
= AudioObjectGetPropertyDataSize(devices
[i
], &addr
, 0, NULL
, &size
);
338 WARN("Unable to get _StreamConfiguration property size for "
339 "device %lu: %lx\n", devices
[i
], sc
);
343 buffers
= HeapAlloc(GetProcessHeap(), 0, size
);
345 HeapFree(GetProcessHeap(), 0, devices
);
346 for(j
= 0; j
< *num
; ++j
){
347 HeapFree(GetProcessHeap(), 0, (*ids
)[j
]);
348 HeapFree(GetProcessHeap(), 0, (*keys
)[j
]);
350 HeapFree(GetProcessHeap(), 0, *keys
);
351 HeapFree(GetProcessHeap(), 0, *ids
);
352 return E_OUTOFMEMORY
;
355 sc
= AudioObjectGetPropertyData(devices
[i
], &addr
, 0, NULL
,
358 WARN("Unable to get _StreamConfiguration property for "
359 "device %lu: %lx\n", devices
[i
], sc
);
360 HeapFree(GetProcessHeap(), 0, buffers
);
364 /* check that there's at least one channel in this device before
365 * we claim it as usable */
366 for(j
= 0; j
< buffers
->mNumberBuffers
; ++j
)
367 if(buffers
->mBuffers
[j
].mNumberChannels
> 0)
369 if(j
>= buffers
->mNumberBuffers
){
370 HeapFree(GetProcessHeap(), 0, buffers
);
374 HeapFree(GetProcessHeap(), 0, buffers
);
377 addr
.mSelector
= kAudioObjectPropertyName
;
378 sc
= AudioObjectGetPropertyData(devices
[i
], &addr
, 0, NULL
,
381 WARN("Unable to get _Name property for device %lu: %lx\n",
386 len
= CFStringGetLength(name
) + 1;
387 (*ids
)[*num
] = HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
390 HeapFree(GetProcessHeap(), 0, devices
);
391 for(j
= 0; j
< *num
; ++j
){
392 HeapFree(GetProcessHeap(), 0, (*ids
)[j
]);
393 HeapFree(GetProcessHeap(), 0, (*keys
)[j
]);
395 HeapFree(GetProcessHeap(), 0, *ids
);
396 HeapFree(GetProcessHeap(), 0, *keys
);
397 return E_OUTOFMEMORY
;
399 CFStringGetCharacters(name
, CFRangeMake(0, len
- 1), (UniChar
*)(*ids
)[*num
]);
400 ((*ids
)[*num
])[len
- 1] = 0;
403 (*keys
)[*num
] = HeapAlloc(GetProcessHeap(), 0, sizeof(AudioDeviceID
));
405 HeapFree(GetProcessHeap(), 0, devices
);
406 HeapFree(GetProcessHeap(), 0, (*ids
)[*num
]);
407 for(j
= 0; j
< *num
; ++j
){
408 HeapFree(GetProcessHeap(), 0, (*ids
)[j
]);
409 HeapFree(GetProcessHeap(), 0, (*keys
)[j
]);
411 HeapFree(GetProcessHeap(), 0, *ids
);
412 HeapFree(GetProcessHeap(), 0, *keys
);
413 return E_OUTOFMEMORY
;
415 *(*keys
)[*num
] = devices
[i
];
417 if(*def_index
== (UINT
)-1 && devices
[i
] == default_id
)
420 TRACE("device %u: id %s key %u%s\n", *num
, debugstr_w((*ids
)[*num
]),
421 (unsigned int)*(*keys
)[*num
], (*def_index
== *num
) ? " (default)" : "");
426 if(*def_index
== (UINT
)-1)
429 HeapFree(GetProcessHeap(), 0, devices
);
434 HRESULT WINAPI
AUDDRV_GetAudioEndpoint(AudioDeviceID
*adevid
, IMMDevice
*dev
,
435 EDataFlow dataflow
, IAudioClient
**out
)
439 TRACE("%u %p %d %p\n", (unsigned int)*adevid
, dev
, dataflow
, out
);
441 This
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(ACImpl
));
443 return E_OUTOFMEMORY
;
445 This
->IAudioClient_iface
.lpVtbl
= &AudioClient_Vtbl
;
446 This
->IAudioRenderClient_iface
.lpVtbl
= &AudioRenderClient_Vtbl
;
447 This
->IAudioCaptureClient_iface
.lpVtbl
= &AudioCaptureClient_Vtbl
;
448 This
->IAudioClock_iface
.lpVtbl
= &AudioClock_Vtbl
;
449 This
->IAudioClock2_iface
.lpVtbl
= &AudioClock2_Vtbl
;
450 This
->IAudioStreamVolume_iface
.lpVtbl
= &AudioStreamVolume_Vtbl
;
452 This
->dataflow
= dataflow
;
454 if(dataflow
== eRender
)
455 This
->scope
= kAudioDevicePropertyScopeOutput
;
456 else if(dataflow
== eCapture
)
457 This
->scope
= kAudioDevicePropertyScopeInput
;
459 HeapFree(GetProcessHeap(), 0, This
);
466 IMMDevice_AddRef(This
->parent
);
468 list_init(&This
->avail_buffers
);
470 This
->adevid
= *adevid
;
472 *out
= &This
->IAudioClient_iface
;
473 IAudioClient_AddRef(&This
->IAudioClient_iface
);
478 static HRESULT WINAPI
AudioClient_QueryInterface(IAudioClient
*iface
,
479 REFIID riid
, void **ppv
)
481 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
486 if(IsEqualIID(riid
, &IID_IUnknown
) || IsEqualIID(riid
, &IID_IAudioClient
))
489 IUnknown_AddRef((IUnknown
*)*ppv
);
492 WARN("Unknown interface %s\n", debugstr_guid(riid
));
493 return E_NOINTERFACE
;
496 static ULONG WINAPI
AudioClient_AddRef(IAudioClient
*iface
)
498 ACImpl
*This
= impl_from_IAudioClient(iface
);
500 ref
= InterlockedIncrement(&This
->ref
);
501 TRACE("(%p) Refcount now %u\n", This
, ref
);
505 static ULONG WINAPI
AudioClient_Release(IAudioClient
*iface
)
507 ACImpl
*This
= impl_from_IAudioClient(iface
);
509 ref
= InterlockedDecrement(&This
->ref
);
510 TRACE("(%p) Refcount now %u\n", This
, ref
);
513 AQBuffer
*buf
, *next
;
514 if(This
->public_buffer
){
515 buf
= This
->public_buffer
->mUserData
;
516 list_add_tail(&This
->avail_buffers
, &buf
->entry
);
518 IAudioClient_Stop(iface
);
519 AudioQueueStop(This
->aqueue
, 1);
520 /* Stopped synchronously, all buffers returned. */
521 LIST_FOR_EACH_ENTRY_SAFE(buf
, next
, &This
->avail_buffers
, AQBuffer
, entry
){
522 AudioQueueFreeBuffer(This
->aqueue
, buf
->buf
);
523 HeapFree(GetProcessHeap(), 0, buf
);
525 AudioQueueDispose(This
->aqueue
, 1);
528 EnterCriticalSection(&g_sessions_lock
);
529 list_remove(&This
->entry
);
530 LeaveCriticalSection(&g_sessions_lock
);
532 HeapFree(GetProcessHeap(), 0, This
->vols
);
533 CoTaskMemFree(This
->fmt
);
534 IMMDevice_Release(This
->parent
);
535 HeapFree(GetProcessHeap(), 0, This
);
540 static void dump_fmt(const WAVEFORMATEX
*fmt
)
542 TRACE("wFormatTag: 0x%x (", fmt
->wFormatTag
);
543 switch(fmt
->wFormatTag
){
544 case WAVE_FORMAT_PCM
:
545 TRACE("WAVE_FORMAT_PCM");
547 case WAVE_FORMAT_IEEE_FLOAT
:
548 TRACE("WAVE_FORMAT_IEEE_FLOAT");
550 case WAVE_FORMAT_EXTENSIBLE
:
551 TRACE("WAVE_FORMAT_EXTENSIBLE");
559 TRACE("nChannels: %u\n", fmt
->nChannels
);
560 TRACE("nSamplesPerSec: %u\n", fmt
->nSamplesPerSec
);
561 TRACE("nAvgBytesPerSec: %u\n", fmt
->nAvgBytesPerSec
);
562 TRACE("nBlockAlign: %u\n", fmt
->nBlockAlign
);
563 TRACE("wBitsPerSample: %u\n", fmt
->wBitsPerSample
);
564 TRACE("cbSize: %u\n", fmt
->cbSize
);
566 if(fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
){
567 WAVEFORMATEXTENSIBLE
*fmtex
= (void*)fmt
;
568 TRACE("dwChannelMask: %08x\n", fmtex
->dwChannelMask
);
569 TRACE("Samples: %04x\n", fmtex
->Samples
.wReserved
);
570 TRACE("SubFormat: %s\n", wine_dbgstr_guid(&fmtex
->SubFormat
));
574 static DWORD
get_channel_mask(unsigned int channels
)
580 return KSAUDIO_SPEAKER_MONO
;
582 return KSAUDIO_SPEAKER_STEREO
;
584 return KSAUDIO_SPEAKER_STEREO
| SPEAKER_LOW_FREQUENCY
;
586 return KSAUDIO_SPEAKER_QUAD
; /* not _SURROUND */
588 return KSAUDIO_SPEAKER_QUAD
| SPEAKER_LOW_FREQUENCY
;
590 return KSAUDIO_SPEAKER_5POINT1
; /* not 5POINT1_SURROUND */
592 return KSAUDIO_SPEAKER_5POINT1
| SPEAKER_BACK_CENTER
;
594 return KSAUDIO_SPEAKER_7POINT1
; /* not 7POINT1_SURROUND */
596 FIXME("Unknown speaker configuration: %u\n", channels
);
600 static WAVEFORMATEX
*clone_format(const WAVEFORMATEX
*fmt
)
605 if(fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
)
606 size
= sizeof(WAVEFORMATEXTENSIBLE
);
608 size
= sizeof(WAVEFORMATEX
);
610 ret
= CoTaskMemAlloc(size
);
614 memcpy(ret
, fmt
, size
);
616 ret
->cbSize
= size
- sizeof(WAVEFORMATEX
);
621 static HRESULT
ca_get_audiodesc(AudioStreamBasicDescription
*desc
,
622 const WAVEFORMATEX
*fmt
)
624 const WAVEFORMATEXTENSIBLE
*fmtex
= (const WAVEFORMATEXTENSIBLE
*)fmt
;
626 desc
->mFormatFlags
= 0;
628 if(fmt
->wFormatTag
== WAVE_FORMAT_PCM
||
629 (fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
630 IsEqualGUID(&fmtex
->SubFormat
, &KSDATAFORMAT_SUBTYPE_PCM
))){
631 desc
->mFormatID
= kAudioFormatLinearPCM
;
632 if(fmt
->wBitsPerSample
> 8)
633 desc
->mFormatFlags
= kAudioFormatFlagIsSignedInteger
;
634 }else if(fmt
->wFormatTag
== WAVE_FORMAT_IEEE_FLOAT
||
635 (fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
636 IsEqualGUID(&fmtex
->SubFormat
, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
))){
637 desc
->mFormatID
= kAudioFormatLinearPCM
;
638 desc
->mFormatFlags
= kAudioFormatFlagIsFloat
;
639 }else if(fmt
->wFormatTag
== WAVE_FORMAT_MULAW
||
640 (fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
641 IsEqualGUID(&fmtex
->SubFormat
, &KSDATAFORMAT_SUBTYPE_MULAW
))){
642 desc
->mFormatID
= kAudioFormatULaw
;
643 }else if(fmt
->wFormatTag
== WAVE_FORMAT_ALAW
||
644 (fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
645 IsEqualGUID(&fmtex
->SubFormat
, &KSDATAFORMAT_SUBTYPE_ALAW
))){
646 desc
->mFormatID
= kAudioFormatALaw
;
648 return AUDCLNT_E_UNSUPPORTED_FORMAT
;
650 desc
->mSampleRate
= fmt
->nSamplesPerSec
;
651 desc
->mBytesPerPacket
= fmt
->nBlockAlign
;
652 desc
->mFramesPerPacket
= 1;
653 desc
->mBytesPerFrame
= fmt
->nBlockAlign
;
654 desc
->mChannelsPerFrame
= fmt
->nChannels
;
655 desc
->mBitsPerChannel
= fmt
->wBitsPerSample
;
661 static void ca_out_buffer_cb(void *user
, AudioQueueRef aqueue
,
662 AudioQueueBufferRef buffer
)
665 AQBuffer
*buf
= buffer
->mUserData
;
667 OSSpinLockLock(&This
->lock
);
668 list_add_tail(&This
->avail_buffers
, &buf
->entry
);
669 This
->inbuf_frames
-= buffer
->mAudioDataByteSize
/ This
->fmt
->nBlockAlign
;
670 OSSpinLockUnlock(&This
->lock
);
673 static void ca_in_buffer_cb(void *user
, AudioQueueRef aqueue
,
674 AudioQueueBufferRef buffer
, const AudioTimeStamp
*start
,
675 UInt32 ndesc
, const AudioStreamPacketDescription
*descs
)
678 AQBuffer
*buf
= buffer
->mUserData
;
680 OSSpinLockLock(&This
->lock
);
681 list_add_tail(&This
->avail_buffers
, &buf
->entry
);
682 This
->inbuf_frames
+= buffer
->mAudioDataByteSize
/ This
->fmt
->nBlockAlign
;
683 OSSpinLockUnlock(&This
->lock
);
686 static HRESULT
ca_setup_aqueue(AudioDeviceID did
, EDataFlow flow
,
687 const WAVEFORMATEX
*fmt
, void *user
, AudioQueueRef
*aqueue
)
689 AudioStreamBasicDescription desc
;
690 AudioObjectPropertyAddress addr
;
696 addr
.mScope
= kAudioObjectPropertyScopeGlobal
;
698 addr
.mSelector
= kAudioDevicePropertyDeviceUID
;
701 sc
= AudioObjectGetPropertyData(did
, &addr
, 0, NULL
, &size
, &uid
);
703 WARN("Unable to get _DeviceUID property: %lx\n", sc
);
707 hr
= ca_get_audiodesc(&desc
, fmt
);
714 sc
= AudioQueueNewOutput(&desc
, ca_out_buffer_cb
, user
, NULL
, NULL
, 0,
716 else if(flow
== eCapture
)
717 sc
= AudioQueueNewInput(&desc
, ca_in_buffer_cb
, user
, NULL
, NULL
, 0,
724 WARN("Unable to create AudioQueue: %lx\n", sc
);
729 sc
= AudioQueueSetProperty(*aqueue
, kAudioQueueProperty_CurrentDevice
,
741 static void session_init_vols(AudioSession
*session
, UINT channels
)
743 if(session
->channel_count
< channels
){
746 if(session
->channel_vols
)
747 session
->channel_vols
= HeapReAlloc(GetProcessHeap(), 0,
748 session
->channel_vols
, sizeof(float) * channels
);
750 session
->channel_vols
= HeapAlloc(GetProcessHeap(), 0,
751 sizeof(float) * channels
);
752 if(!session
->channel_vols
)
755 for(i
= session
->channel_count
; i
< channels
; ++i
)
756 session
->channel_vols
[i
] = 1.f
;
758 session
->channel_count
= channels
;
762 static AudioSession
*create_session(const GUID
*guid
, IMMDevice
*device
,
767 ret
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(AudioSession
));
771 memcpy(&ret
->guid
, guid
, sizeof(GUID
));
773 ret
->device
= device
;
775 list_init(&ret
->clients
);
777 list_add_head(&g_sessions
, &ret
->entry
);
779 InitializeCriticalSection(&ret
->lock
);
781 session_init_vols(ret
, num_channels
);
783 ret
->master_vol
= 1.f
;
788 /* if channels == 0, then this will return or create a session with
789 * matching dataflow and GUID. otherwise, channels must also match */
790 static HRESULT
get_audio_session(const GUID
*sessionguid
,
791 IMMDevice
*device
, UINT channels
, AudioSession
**out
)
793 AudioSession
*session
;
795 if(!sessionguid
|| IsEqualGUID(sessionguid
, &GUID_NULL
)){
796 *out
= create_session(&GUID_NULL
, device
, channels
);
798 return E_OUTOFMEMORY
;
804 LIST_FOR_EACH_ENTRY(session
, &g_sessions
, AudioSession
, entry
){
805 if(session
->device
== device
&&
806 IsEqualGUID(sessionguid
, &session
->guid
)){
807 session_init_vols(session
, channels
);
814 *out
= create_session(sessionguid
, device
, channels
);
816 return E_OUTOFMEMORY
;
822 static HRESULT WINAPI
AudioClient_Initialize(IAudioClient
*iface
,
823 AUDCLNT_SHAREMODE mode
, DWORD flags
, REFERENCE_TIME duration
,
824 REFERENCE_TIME period
, const WAVEFORMATEX
*fmt
,
825 const GUID
*sessionguid
)
827 ACImpl
*This
= impl_from_IAudioClient(iface
);
832 TRACE("(%p)->(%x, %x, %s, %s, %p, %s)\n", This
, mode
, flags
,
833 wine_dbgstr_longlong(duration
), wine_dbgstr_longlong(period
), fmt
, debugstr_guid(sessionguid
));
840 if(mode
!= AUDCLNT_SHAREMODE_SHARED
&& mode
!= AUDCLNT_SHAREMODE_EXCLUSIVE
)
841 return AUDCLNT_E_NOT_INITIALIZED
;
843 if(flags
& ~(AUDCLNT_STREAMFLAGS_CROSSPROCESS
|
844 AUDCLNT_STREAMFLAGS_LOOPBACK
|
845 AUDCLNT_STREAMFLAGS_EVENTCALLBACK
|
846 AUDCLNT_STREAMFLAGS_NOPERSIST
|
847 AUDCLNT_STREAMFLAGS_RATEADJUST
|
848 AUDCLNT_SESSIONFLAGS_EXPIREWHENUNOWNED
|
849 AUDCLNT_SESSIONFLAGS_DISPLAY_HIDE
|
850 AUDCLNT_SESSIONFLAGS_DISPLAY_HIDEWHENEXPIRED
)){
851 TRACE("Unknown flags: %08x\n", flags
);
855 OSSpinLockLock(&This
->lock
);
858 OSSpinLockUnlock(&This
->lock
);
859 return AUDCLNT_E_ALREADY_INITIALIZED
;
862 hr
= ca_setup_aqueue(This
->adevid
, This
->dataflow
, fmt
, This
, &This
->aqueue
);
864 OSSpinLockUnlock(&This
->lock
);
868 This
->fmt
= clone_format(fmt
);
870 AudioQueueDispose(This
->aqueue
, 1);
872 OSSpinLockUnlock(&This
->lock
);
873 return E_OUTOFMEMORY
;
877 This
->period_ms
= period
/ 10000;
878 if(This
->period_ms
== 0)
881 This
->period_ms
= MinimumPeriod
/ 10000;
884 duration
= 300000; /* 0.03s */
885 This
->bufsize_frames
= ceil(fmt
->nSamplesPerSec
* (duration
/ 10000000.));
887 if(This
->dataflow
== eCapture
){
889 UInt32 bsize
= ceil((This
->bufsize_frames
/ (double)CAPTURE_BUFFERS
) *
890 This
->fmt
->nBlockAlign
);
891 for(i
= 0; i
< CAPTURE_BUFFERS
; ++i
){
894 buf
= HeapAlloc(GetProcessHeap(), 0, sizeof(AQBuffer
));
896 AudioQueueDispose(This
->aqueue
, 1);
898 CoTaskMemFree(This
->fmt
);
900 OSSpinLockUnlock(&This
->lock
);
901 return E_OUTOFMEMORY
;
904 sc
= AudioQueueAllocateBuffer(This
->aqueue
, bsize
, &buf
->buf
);
906 AudioQueueDispose(This
->aqueue
, 1);
908 CoTaskMemFree(This
->fmt
);
910 OSSpinLockUnlock(&This
->lock
);
911 WARN("Couldn't allocate buffer: %lx\n", sc
);
915 buf
->buf
->mUserData
= buf
;
917 sc
= AudioQueueEnqueueBuffer(This
->aqueue
, buf
->buf
, 0, NULL
);
919 ERR("Couldn't enqueue buffer: %lx\n", sc
);
925 This
->vols
= HeapAlloc(GetProcessHeap(), 0, fmt
->nChannels
* sizeof(float));
927 AudioQueueDispose(This
->aqueue
, 1);
929 CoTaskMemFree(This
->fmt
);
931 OSSpinLockUnlock(&This
->lock
);
932 return E_OUTOFMEMORY
;
935 for(i
= 0; i
< fmt
->nChannels
; ++i
)
941 EnterCriticalSection(&g_sessions_lock
);
943 hr
= get_audio_session(sessionguid
, This
->parent
, fmt
->nChannels
,
946 LeaveCriticalSection(&g_sessions_lock
);
947 AudioQueueDispose(This
->aqueue
, 1);
949 CoTaskMemFree(This
->fmt
);
951 HeapFree(GetProcessHeap(), 0, This
->vols
);
953 OSSpinLockUnlock(&This
->lock
);
957 list_add_tail(&This
->session
->clients
, &This
->entry
);
959 LeaveCriticalSection(&g_sessions_lock
);
963 OSSpinLockUnlock(&This
->lock
);
968 static HRESULT WINAPI
AudioClient_GetBufferSize(IAudioClient
*iface
,
971 ACImpl
*This
= impl_from_IAudioClient(iface
);
973 TRACE("(%p)->(%p)\n", This
, frames
);
978 OSSpinLockLock(&This
->lock
);
981 OSSpinLockUnlock(&This
->lock
);
982 return AUDCLNT_E_NOT_INITIALIZED
;
985 *frames
= This
->bufsize_frames
;
987 OSSpinLockUnlock(&This
->lock
);
992 static HRESULT
ca_get_max_stream_latency(ACImpl
*This
, UInt32
*max
)
994 AudioObjectPropertyAddress addr
;
1000 addr
.mScope
= This
->scope
;
1002 addr
.mSelector
= kAudioDevicePropertyStreams
;
1004 sc
= AudioObjectGetPropertyDataSize(This
->adevid
, &addr
, 0, NULL
,
1007 WARN("Unable to get size for _Streams property: %lx\n", sc
);
1011 ids
= HeapAlloc(GetProcessHeap(), 0, size
);
1013 return E_OUTOFMEMORY
;
1015 sc
= AudioObjectGetPropertyData(This
->adevid
, &addr
, 0, NULL
, &size
, ids
);
1017 WARN("Unable to get _Streams property: %lx\n", sc
);
1018 HeapFree(GetProcessHeap(), 0, ids
);
1022 nstreams
= size
/ sizeof(AudioStreamID
);
1025 addr
.mSelector
= kAudioStreamPropertyLatency
;
1026 for(i
= 0; i
< nstreams
; ++i
){
1029 size
= sizeof(latency
);
1030 sc
= AudioObjectGetPropertyData(ids
[i
], &addr
, 0, NULL
,
1033 WARN("Unable to get _Latency property: %lx\n", sc
);
1041 HeapFree(GetProcessHeap(), 0, ids
);
1046 static HRESULT WINAPI
AudioClient_GetStreamLatency(IAudioClient
*iface
,
1047 REFERENCE_TIME
*out
)
1049 ACImpl
*This
= impl_from_IAudioClient(iface
);
1050 UInt32 latency
, stream_latency
, size
;
1051 AudioObjectPropertyAddress addr
;
1055 TRACE("(%p)->(%p)\n", This
, out
);
1060 OSSpinLockLock(&This
->lock
);
1063 OSSpinLockUnlock(&This
->lock
);
1064 return AUDCLNT_E_NOT_INITIALIZED
;
1067 addr
.mScope
= This
->scope
;
1068 addr
.mSelector
= kAudioDevicePropertyLatency
;
1071 size
= sizeof(latency
);
1072 sc
= AudioObjectGetPropertyData(This
->adevid
, &addr
, 0, NULL
,
1075 WARN("Couldn't get _Latency property: %lx\n", sc
);
1076 OSSpinLockUnlock(&This
->lock
);
1080 hr
= ca_get_max_stream_latency(This
, &stream_latency
);
1082 OSSpinLockUnlock(&This
->lock
);
1086 latency
+= stream_latency
;
1087 *out
= (latency
/ (double)This
->fmt
->nSamplesPerSec
) * 10000000;
1089 OSSpinLockUnlock(&This
->lock
);
1094 static HRESULT
AudioClient_GetCurrentPadding_nolock(ACImpl
*This
,
1098 return AUDCLNT_E_NOT_INITIALIZED
;
1100 *numpad
= This
->inbuf_frames
;
1105 static HRESULT WINAPI
AudioClient_GetCurrentPadding(IAudioClient
*iface
,
1108 ACImpl
*This
= impl_from_IAudioClient(iface
);
1111 TRACE("(%p)->(%p)\n", This
, numpad
);
1116 OSSpinLockLock(&This
->lock
);
1118 hr
= AudioClient_GetCurrentPadding_nolock(This
, numpad
);
1120 OSSpinLockUnlock(&This
->lock
);
1125 static HRESULT WINAPI
AudioClient_IsFormatSupported(IAudioClient
*iface
,
1126 AUDCLNT_SHAREMODE mode
, const WAVEFORMATEX
*pwfx
,
1127 WAVEFORMATEX
**outpwfx
)
1129 ACImpl
*This
= impl_from_IAudioClient(iface
);
1130 WAVEFORMATEXTENSIBLE
*fmtex
= (WAVEFORMATEXTENSIBLE
*)pwfx
;
1131 AudioQueueRef aqueue
;
1134 TRACE("(%p)->(%x, %p, %p)\n", This
, mode
, pwfx
, outpwfx
);
1136 if(!pwfx
|| (mode
== AUDCLNT_SHAREMODE_SHARED
&& !outpwfx
))
1139 if(mode
!= AUDCLNT_SHAREMODE_SHARED
&& mode
!= AUDCLNT_SHAREMODE_EXCLUSIVE
)
1140 return E_INVALIDARG
;
1142 if(pwfx
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
1143 pwfx
->cbSize
< sizeof(WAVEFORMATEXTENSIBLE
) - sizeof(WAVEFORMATEX
))
1144 return E_INVALIDARG
;
1148 if(pwfx
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
1149 fmtex
->dwChannelMask
!= 0 &&
1150 fmtex
->dwChannelMask
!= get_channel_mask(pwfx
->nChannels
))
1151 return AUDCLNT_E_UNSUPPORTED_FORMAT
;
1153 OSSpinLockLock(&This
->lock
);
1155 hr
= ca_setup_aqueue(This
->adevid
, This
->dataflow
, pwfx
, NULL
, &aqueue
);
1157 AudioQueueDispose(aqueue
, 1);
1158 OSSpinLockUnlock(&This
->lock
);
1161 TRACE("returning %08x\n", S_OK
);
1165 OSSpinLockUnlock(&This
->lock
);
1170 TRACE("returning %08x\n", AUDCLNT_E_UNSUPPORTED_FORMAT
);
1171 return AUDCLNT_E_UNSUPPORTED_FORMAT
;
1174 static HRESULT WINAPI
AudioClient_GetMixFormat(IAudioClient
*iface
,
1175 WAVEFORMATEX
**pwfx
)
1177 ACImpl
*This
= impl_from_IAudioClient(iface
);
1178 WAVEFORMATEXTENSIBLE
*fmt
;
1182 AudioBufferList
*buffers
;
1183 AudioObjectPropertyAddress addr
;
1186 TRACE("(%p)->(%p)\n", This
, pwfx
);
1192 fmt
= CoTaskMemAlloc(sizeof(WAVEFORMATEXTENSIBLE
));
1194 return E_OUTOFMEMORY
;
1196 fmt
->Format
.wFormatTag
= WAVE_FORMAT_EXTENSIBLE
;
1198 addr
.mScope
= This
->scope
;
1200 addr
.mSelector
= kAudioDevicePropertyStreamConfiguration
;
1202 sc
= AudioObjectGetPropertyDataSize(This
->adevid
, &addr
, 0, NULL
, &size
);
1205 WARN("Unable to get size for _StreamConfiguration property: %lx\n", sc
);
1209 buffers
= HeapAlloc(GetProcessHeap(), 0, size
);
1212 return E_OUTOFMEMORY
;
1215 sc
= AudioObjectGetPropertyData(This
->adevid
, &addr
, 0, NULL
,
1219 HeapFree(GetProcessHeap(), 0, buffers
);
1220 WARN("Unable to get _StreamConfiguration property: %lx\n", sc
);
1224 fmt
->Format
.nChannels
= 0;
1225 for(i
= 0; i
< buffers
->mNumberBuffers
; ++i
)
1226 fmt
->Format
.nChannels
+= buffers
->mBuffers
[i
].mNumberChannels
;
1228 HeapFree(GetProcessHeap(), 0, buffers
);
1230 fmt
->dwChannelMask
= get_channel_mask(fmt
->Format
.nChannels
);
1232 addr
.mSelector
= kAudioDevicePropertyNominalSampleRate
;
1233 size
= sizeof(Float64
);
1234 sc
= AudioObjectGetPropertyData(This
->adevid
, &addr
, 0, NULL
, &size
, &rate
);
1237 WARN("Unable to get _NominalSampleRate property: %lx\n", sc
);
1240 fmt
->Format
.nSamplesPerSec
= rate
;
1242 fmt
->Format
.wBitsPerSample
= 32;
1243 fmt
->SubFormat
= KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
;
1245 fmt
->Format
.nBlockAlign
= (fmt
->Format
.wBitsPerSample
*
1246 fmt
->Format
.nChannels
) / 8;
1247 fmt
->Format
.nAvgBytesPerSec
= fmt
->Format
.nSamplesPerSec
*
1248 fmt
->Format
.nBlockAlign
;
1250 fmt
->Samples
.wValidBitsPerSample
= fmt
->Format
.wBitsPerSample
;
1251 fmt
->Format
.cbSize
= sizeof(WAVEFORMATEXTENSIBLE
) - sizeof(WAVEFORMATEX
);
1253 *pwfx
= (WAVEFORMATEX
*)fmt
;
1259 static HRESULT WINAPI
AudioClient_GetDevicePeriod(IAudioClient
*iface
,
1260 REFERENCE_TIME
*defperiod
, REFERENCE_TIME
*minperiod
)
1262 ACImpl
*This
= impl_from_IAudioClient(iface
);
1264 TRACE("(%p)->(%p, %p)\n", This
, defperiod
, minperiod
);
1266 if(!defperiod
&& !minperiod
)
1269 OSSpinLockLock(&This
->lock
);
1271 if(This
->period_ms
){
1273 *defperiod
= This
->period_ms
* 10000;
1275 *minperiod
= This
->period_ms
* 10000;
1278 *defperiod
= DefaultPeriod
;
1280 *minperiod
= MinimumPeriod
;
1283 OSSpinLockUnlock(&This
->lock
);
1288 void CALLBACK
ca_period_cb(void *user
, BOOLEAN timer
)
1290 ACImpl
*This
= user
;
1292 OSSpinLockLock(&This
->lock
);
1294 SetEvent(This
->event
);
1295 OSSpinLockUnlock(&This
->lock
);
1298 static HRESULT WINAPI
AudioClient_Start(IAudioClient
*iface
)
1300 ACImpl
*This
= impl_from_IAudioClient(iface
);
1303 TRACE("(%p)\n", This
);
1305 OSSpinLockLock(&This
->lock
);
1308 OSSpinLockUnlock(&This
->lock
);
1309 return AUDCLNT_E_NOT_INITIALIZED
;
1312 if(This
->playing
!= StateStopped
){
1313 OSSpinLockUnlock(&This
->lock
);
1314 return AUDCLNT_E_NOT_STOPPED
;
1317 if((This
->flags
& AUDCLNT_STREAMFLAGS_EVENTCALLBACK
) && !This
->event
){
1318 OSSpinLockUnlock(&This
->lock
);
1319 return AUDCLNT_E_EVENTHANDLE_NOT_SET
;
1323 if(!CreateTimerQueueTimer(&This
->timer
, g_timer_q
,
1324 ca_period_cb
, This
, 0, This
->period_ms
, 0))
1325 ERR("Unable to create timer: %u\n", GetLastError());
1327 This
->playing
= StateInTransition
;
1329 OSSpinLockUnlock(&This
->lock
);
1331 sc
= AudioQueueStart(This
->aqueue
, NULL
);
1333 WARN("Unable to start audio queue: %lx\n", sc
);
1337 OSSpinLockLock(&This
->lock
);
1339 This
->playing
= StatePlaying
;
1341 OSSpinLockUnlock(&This
->lock
);
1346 static HRESULT WINAPI
AudioClient_Stop(IAudioClient
*iface
)
1348 ACImpl
*This
= impl_from_IAudioClient(iface
);
1351 TRACE("(%p)\n", This
);
1353 OSSpinLockLock(&This
->lock
);
1356 OSSpinLockUnlock(&This
->lock
);
1357 return AUDCLNT_E_NOT_INITIALIZED
;
1360 if(This
->playing
== StateStopped
){
1361 OSSpinLockUnlock(&This
->lock
);
1365 if(This
->playing
== StateInTransition
){
1366 OSSpinLockUnlock(&This
->lock
);
1370 if(This
->timer
&& This
->timer
!= INVALID_HANDLE_VALUE
){
1371 DeleteTimerQueueTimer(g_timer_q
, This
->timer
, INVALID_HANDLE_VALUE
);
1375 This
->playing
= StateInTransition
;
1377 OSSpinLockUnlock(&This
->lock
);
1379 sc
= AudioQueueFlush(This
->aqueue
);
1381 WARN("Unable to flush audio queue: %lx\n", sc
);
1383 sc
= AudioQueuePause(This
->aqueue
);
1385 WARN("Unable to pause audio queue: %lx\n", sc
);
1389 OSSpinLockLock(&This
->lock
);
1391 This
->playing
= StateStopped
;
1393 OSSpinLockUnlock(&This
->lock
);
1398 static HRESULT WINAPI
AudioClient_Reset(IAudioClient
*iface
)
1400 ACImpl
*This
= impl_from_IAudioClient(iface
);
1404 TRACE("(%p)\n", This
);
1406 OSSpinLockLock(&This
->lock
);
1409 OSSpinLockUnlock(&This
->lock
);
1410 return AUDCLNT_E_NOT_INITIALIZED
;
1413 if(This
->playing
!= StateStopped
){
1414 OSSpinLockUnlock(&This
->lock
);
1415 return AUDCLNT_E_NOT_STOPPED
;
1418 if(This
->getbuf_last
){
1419 OSSpinLockUnlock(&This
->lock
);
1420 return AUDCLNT_E_BUFFER_OPERATION_PENDING
;
1423 This
->written_frames
= 0;
1425 hr
= AudioClock_GetPosition_nolock(This
, &This
->last_time
, NULL
, TRUE
);
1427 OSSpinLockUnlock(&This
->lock
);
1431 OSSpinLockUnlock(&This
->lock
);
1433 sc
= AudioQueueReset(This
->aqueue
);
1435 WARN("Unable to reset audio queue: %lx\n", sc
);
1442 static HRESULT WINAPI
AudioClient_SetEventHandle(IAudioClient
*iface
,
1445 ACImpl
*This
= impl_from_IAudioClient(iface
);
1447 TRACE("(%p)->(%p)\n", This
, event
);
1450 return E_INVALIDARG
;
1452 OSSpinLockLock(&This
->lock
);
1455 OSSpinLockUnlock(&This
->lock
);
1456 return AUDCLNT_E_NOT_INITIALIZED
;
1459 if(!(This
->flags
& AUDCLNT_STREAMFLAGS_EVENTCALLBACK
)){
1460 OSSpinLockUnlock(&This
->lock
);
1461 return AUDCLNT_E_EVENTHANDLE_NOT_EXPECTED
;
1464 This
->event
= event
;
1466 OSSpinLockUnlock(&This
->lock
);
1471 static HRESULT WINAPI
AudioClient_GetService(IAudioClient
*iface
, REFIID riid
,
1474 ACImpl
*This
= impl_from_IAudioClient(iface
);
1476 TRACE("(%p)->(%s, %p)\n", This
, debugstr_guid(riid
), ppv
);
1482 OSSpinLockLock(&This
->lock
);
1485 OSSpinLockUnlock(&This
->lock
);
1486 return AUDCLNT_E_NOT_INITIALIZED
;
1489 if(IsEqualIID(riid
, &IID_IAudioRenderClient
)){
1490 if(This
->dataflow
!= eRender
){
1491 OSSpinLockUnlock(&This
->lock
);
1492 return AUDCLNT_E_WRONG_ENDPOINT_TYPE
;
1494 IAudioRenderClient_AddRef(&This
->IAudioRenderClient_iface
);
1495 *ppv
= &This
->IAudioRenderClient_iface
;
1496 }else if(IsEqualIID(riid
, &IID_IAudioCaptureClient
)){
1497 if(This
->dataflow
!= eCapture
){
1498 OSSpinLockUnlock(&This
->lock
);
1499 return AUDCLNT_E_WRONG_ENDPOINT_TYPE
;
1501 IAudioCaptureClient_AddRef(&This
->IAudioCaptureClient_iface
);
1502 *ppv
= &This
->IAudioCaptureClient_iface
;
1503 }else if(IsEqualIID(riid
, &IID_IAudioClock
)){
1504 IAudioClock_AddRef(&This
->IAudioClock_iface
);
1505 *ppv
= &This
->IAudioClock_iface
;
1506 }else if(IsEqualIID(riid
, &IID_IAudioStreamVolume
)){
1507 IAudioStreamVolume_AddRef(&This
->IAudioStreamVolume_iface
);
1508 *ppv
= &This
->IAudioStreamVolume_iface
;
1509 }else if(IsEqualIID(riid
, &IID_IAudioSessionControl
)){
1510 if(!This
->session_wrapper
){
1511 This
->session_wrapper
= AudioSessionWrapper_Create(This
);
1512 if(!This
->session_wrapper
){
1513 OSSpinLockUnlock(&This
->lock
);
1514 return E_OUTOFMEMORY
;
1517 IAudioSessionControl2_AddRef(&This
->session_wrapper
->IAudioSessionControl2_iface
);
1519 *ppv
= &This
->session_wrapper
->IAudioSessionControl2_iface
;
1520 }else if(IsEqualIID(riid
, &IID_IChannelAudioVolume
)){
1521 if(!This
->session_wrapper
){
1522 This
->session_wrapper
= AudioSessionWrapper_Create(This
);
1523 if(!This
->session_wrapper
){
1524 OSSpinLockUnlock(&This
->lock
);
1525 return E_OUTOFMEMORY
;
1528 IChannelAudioVolume_AddRef(&This
->session_wrapper
->IChannelAudioVolume_iface
);
1530 *ppv
= &This
->session_wrapper
->IChannelAudioVolume_iface
;
1531 }else if(IsEqualIID(riid
, &IID_ISimpleAudioVolume
)){
1532 if(!This
->session_wrapper
){
1533 This
->session_wrapper
= AudioSessionWrapper_Create(This
);
1534 if(!This
->session_wrapper
){
1535 OSSpinLockUnlock(&This
->lock
);
1536 return E_OUTOFMEMORY
;
1539 ISimpleAudioVolume_AddRef(&This
->session_wrapper
->ISimpleAudioVolume_iface
);
1541 *ppv
= &This
->session_wrapper
->ISimpleAudioVolume_iface
;
1545 OSSpinLockUnlock(&This
->lock
);
1549 OSSpinLockUnlock(&This
->lock
);
1551 FIXME("stub %s\n", debugstr_guid(riid
));
1552 return E_NOINTERFACE
;
1555 static const IAudioClientVtbl AudioClient_Vtbl
=
1557 AudioClient_QueryInterface
,
1559 AudioClient_Release
,
1560 AudioClient_Initialize
,
1561 AudioClient_GetBufferSize
,
1562 AudioClient_GetStreamLatency
,
1563 AudioClient_GetCurrentPadding
,
1564 AudioClient_IsFormatSupported
,
1565 AudioClient_GetMixFormat
,
1566 AudioClient_GetDevicePeriod
,
1570 AudioClient_SetEventHandle
,
1571 AudioClient_GetService
1574 static HRESULT WINAPI
AudioRenderClient_QueryInterface(
1575 IAudioRenderClient
*iface
, REFIID riid
, void **ppv
)
1577 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
1583 if(IsEqualIID(riid
, &IID_IUnknown
) ||
1584 IsEqualIID(riid
, &IID_IAudioRenderClient
))
1587 IUnknown_AddRef((IUnknown
*)*ppv
);
1591 WARN("Unknown interface %s\n", debugstr_guid(riid
));
1592 return E_NOINTERFACE
;
1595 static ULONG WINAPI
AudioRenderClient_AddRef(IAudioRenderClient
*iface
)
1597 ACImpl
*This
= impl_from_IAudioRenderClient(iface
);
1598 return AudioClient_AddRef(&This
->IAudioClient_iface
);
1601 static ULONG WINAPI
AudioRenderClient_Release(IAudioRenderClient
*iface
)
1603 ACImpl
*This
= impl_from_IAudioRenderClient(iface
);
1604 return AudioClient_Release(&This
->IAudioClient_iface
);
1607 static HRESULT WINAPI
AudioRenderClient_GetBuffer(IAudioRenderClient
*iface
,
1608 UINT32 frames
, BYTE
**data
)
1610 ACImpl
*This
= impl_from_IAudioRenderClient(iface
);
1612 UINT32 pad
, bytes
= frames
* This
->fmt
->nBlockAlign
;
1616 TRACE("(%p)->(%u, %p)\n", This
, frames
, data
);
1622 OSSpinLockLock(&This
->lock
);
1624 if(This
->getbuf_last
){
1625 OSSpinLockUnlock(&This
->lock
);
1626 return AUDCLNT_E_OUT_OF_ORDER
;
1630 OSSpinLockUnlock(&This
->lock
);
1634 hr
= AudioClient_GetCurrentPadding_nolock(This
, &pad
);
1636 OSSpinLockUnlock(&This
->lock
);
1640 if(pad
+ frames
> This
->bufsize_frames
){
1641 OSSpinLockUnlock(&This
->lock
);
1642 return AUDCLNT_E_BUFFER_TOO_LARGE
;
1645 LIST_FOR_EACH_ENTRY(buf
, &This
->avail_buffers
, AQBuffer
, entry
){
1646 if(buf
->buf
->mAudioDataBytesCapacity
>= bytes
){
1647 This
->public_buffer
= buf
->buf
;
1648 list_remove(&buf
->entry
);
1653 if(&buf
->entry
== &This
->avail_buffers
){
1654 sc
= AudioQueueAllocateBuffer(This
->aqueue
, bytes
,
1655 &This
->public_buffer
);
1657 OSSpinLockUnlock(&This
->lock
);
1658 WARN("Unable to allocate buffer: %lx\n", sc
);
1661 buf
= HeapAlloc(GetProcessHeap(), 0, sizeof(AQBuffer
));
1663 AudioQueueFreeBuffer(This
->aqueue
, This
->public_buffer
);
1664 This
->public_buffer
= NULL
;
1665 OSSpinLockUnlock(&This
->lock
);
1666 return E_OUTOFMEMORY
;
1668 buf
->buf
= This
->public_buffer
;
1669 This
->public_buffer
->mUserData
= buf
;
1672 *data
= This
->public_buffer
->mAudioData
;
1674 This
->getbuf_last
= frames
;
1676 OSSpinLockUnlock(&This
->lock
);
1681 static HRESULT WINAPI
AudioRenderClient_ReleaseBuffer(
1682 IAudioRenderClient
*iface
, UINT32 frames
, DWORD flags
)
1684 ACImpl
*This
= impl_from_IAudioRenderClient(iface
);
1687 TRACE("(%p)->(%u, %x)\n", This
, frames
, flags
);
1689 OSSpinLockLock(&This
->lock
);
1692 This
->getbuf_last
= 0;
1693 if(This
->public_buffer
){
1694 AQBuffer
*buf
= This
->public_buffer
->mUserData
;
1695 list_add_tail(&This
->avail_buffers
, &buf
->entry
);
1696 This
->public_buffer
= NULL
;
1698 OSSpinLockUnlock(&This
->lock
);
1702 if(!This
->getbuf_last
){
1703 OSSpinLockUnlock(&This
->lock
);
1704 return AUDCLNT_E_OUT_OF_ORDER
;
1707 if(frames
> This
->getbuf_last
){
1708 OSSpinLockUnlock(&This
->lock
);
1709 return AUDCLNT_E_INVALID_SIZE
;
1712 if(flags
& AUDCLNT_BUFFERFLAGS_SILENT
){
1713 WAVEFORMATEXTENSIBLE
*fmtex
= (WAVEFORMATEXTENSIBLE
*)This
->fmt
;
1714 if((This
->fmt
->wFormatTag
== WAVE_FORMAT_PCM
||
1715 (This
->fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
1716 IsEqualGUID(&fmtex
->SubFormat
, &KSDATAFORMAT_SUBTYPE_PCM
))) &&
1717 This
->fmt
->wBitsPerSample
== 8)
1718 memset(This
->public_buffer
->mAudioData
, 128,
1719 frames
* This
->fmt
->nBlockAlign
);
1721 memset(This
->public_buffer
->mAudioData
, 0,
1722 frames
* This
->fmt
->nBlockAlign
);
1725 This
->public_buffer
->mAudioDataByteSize
= frames
* This
->fmt
->nBlockAlign
;
1727 sc
= AudioQueueEnqueueBuffer(This
->aqueue
, This
->public_buffer
, 0, NULL
);
1729 OSSpinLockUnlock(&This
->lock
);
1730 WARN("Unable to enqueue buffer: %lx\n", sc
);
1734 if(This
->playing
== StateStopped
)
1735 AudioQueuePrime(This
->aqueue
, 0, NULL
);
1737 This
->public_buffer
= NULL
;
1738 This
->getbuf_last
= 0;
1739 This
->written_frames
+= frames
;
1740 This
->inbuf_frames
+= frames
;
1742 OSSpinLockUnlock(&This
->lock
);
1747 static const IAudioRenderClientVtbl AudioRenderClient_Vtbl
= {
1748 AudioRenderClient_QueryInterface
,
1749 AudioRenderClient_AddRef
,
1750 AudioRenderClient_Release
,
1751 AudioRenderClient_GetBuffer
,
1752 AudioRenderClient_ReleaseBuffer
1755 static HRESULT WINAPI
AudioCaptureClient_QueryInterface(
1756 IAudioCaptureClient
*iface
, REFIID riid
, void **ppv
)
1758 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
1764 if(IsEqualIID(riid
, &IID_IUnknown
) ||
1765 IsEqualIID(riid
, &IID_IAudioCaptureClient
))
1768 IUnknown_AddRef((IUnknown
*)*ppv
);
1772 WARN("Unknown interface %s\n", debugstr_guid(riid
));
1773 return E_NOINTERFACE
;
1776 static ULONG WINAPI
AudioCaptureClient_AddRef(IAudioCaptureClient
*iface
)
1778 ACImpl
*This
= impl_from_IAudioCaptureClient(iface
);
1779 return IAudioClient_AddRef(&This
->IAudioClient_iface
);
1782 static ULONG WINAPI
AudioCaptureClient_Release(IAudioCaptureClient
*iface
)
1784 ACImpl
*This
= impl_from_IAudioCaptureClient(iface
);
1785 return IAudioClient_Release(&This
->IAudioClient_iface
);
1788 static HRESULT WINAPI
AudioCaptureClient_GetBuffer(IAudioCaptureClient
*iface
,
1789 BYTE
**data
, UINT32
*frames
, DWORD
*flags
, UINT64
*devpos
,
1792 ACImpl
*This
= impl_from_IAudioCaptureClient(iface
);
1794 TRACE("(%p)->(%p, %p, %p, %p, %p)\n", This
, data
, frames
, flags
,
1797 if(!data
|| !frames
|| !flags
)
1800 OSSpinLockLock(&This
->lock
);
1802 if(This
->getbuf_last
){
1803 OSSpinLockUnlock(&This
->lock
);
1804 return AUDCLNT_E_OUT_OF_ORDER
;
1807 if(This
->public_buffer
){
1808 *data
= This
->public_buffer
->mAudioData
;
1810 This
->public_buffer
->mAudioDataByteSize
/ This
->fmt
->nBlockAlign
;
1812 struct list
*head
= list_head(&This
->avail_buffers
);
1817 AQBuffer
*buf
= LIST_ENTRY(head
, AQBuffer
, entry
);
1818 This
->public_buffer
= buf
->buf
;
1819 *data
= This
->public_buffer
->mAudioData
;
1821 This
->public_buffer
->mAudioDataByteSize
/ This
->fmt
->nBlockAlign
;
1822 list_remove(&buf
->entry
);
1827 This
->written_frames
+= *frames
;
1828 This
->inbuf_frames
-= *frames
;
1829 This
->getbuf_last
= 1;
1831 if(devpos
|| qpcpos
)
1832 AudioClock_GetPosition_nolock(This
, devpos
, qpcpos
, FALSE
);
1834 OSSpinLockUnlock(&This
->lock
);
1836 return *frames
? S_OK
: AUDCLNT_S_BUFFER_EMPTY
;
1839 static HRESULT WINAPI
AudioCaptureClient_ReleaseBuffer(
1840 IAudioCaptureClient
*iface
, UINT32 done
)
1842 ACImpl
*This
= impl_from_IAudioCaptureClient(iface
);
1846 TRACE("(%p)->(%u)\n", This
, done
);
1848 OSSpinLockLock(&This
->lock
);
1850 if(!This
->getbuf_last
){
1851 OSSpinLockUnlock(&This
->lock
);
1852 return AUDCLNT_E_OUT_OF_ORDER
;
1855 pbuf_frames
= This
->public_buffer
->mAudioDataByteSize
/ This
->fmt
->nBlockAlign
;
1856 if(done
!= 0 && done
!= pbuf_frames
){
1857 OSSpinLockUnlock(&This
->lock
);
1858 return AUDCLNT_E_INVALID_SIZE
;
1862 sc
= AudioQueueEnqueueBuffer(This
->aqueue
, This
->public_buffer
,
1865 WARN("Unable to enqueue buffer: %lx\n", sc
);
1866 This
->public_buffer
= NULL
;
1869 This
->getbuf_last
= 0;
1871 OSSpinLockUnlock(&This
->lock
);
1876 static HRESULT WINAPI
AudioCaptureClient_GetNextPacketSize(
1877 IAudioCaptureClient
*iface
, UINT32
*frames
)
1879 ACImpl
*This
= impl_from_IAudioCaptureClient(iface
);
1883 TRACE("(%p)->(%p)\n", This
, frames
);
1888 OSSpinLockLock(&This
->lock
);
1890 head
= list_head(&This
->avail_buffers
);
1893 *frames
= This
->bufsize_frames
/ CAPTURE_BUFFERS
;
1894 OSSpinLockUnlock(&This
->lock
);
1898 buf
= LIST_ENTRY(head
, AQBuffer
, entry
);
1899 *frames
= buf
->buf
->mAudioDataByteSize
/ This
->fmt
->nBlockAlign
;
1901 OSSpinLockUnlock(&This
->lock
);
1906 static const IAudioCaptureClientVtbl AudioCaptureClient_Vtbl
=
1908 AudioCaptureClient_QueryInterface
,
1909 AudioCaptureClient_AddRef
,
1910 AudioCaptureClient_Release
,
1911 AudioCaptureClient_GetBuffer
,
1912 AudioCaptureClient_ReleaseBuffer
,
1913 AudioCaptureClient_GetNextPacketSize
1916 static HRESULT WINAPI
AudioClock_QueryInterface(IAudioClock
*iface
,
1917 REFIID riid
, void **ppv
)
1919 ACImpl
*This
= impl_from_IAudioClock(iface
);
1921 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
1927 if(IsEqualIID(riid
, &IID_IUnknown
) || IsEqualIID(riid
, &IID_IAudioClock
))
1929 else if(IsEqualIID(riid
, &IID_IAudioClock2
))
1930 *ppv
= &This
->IAudioClock2_iface
;
1932 IUnknown_AddRef((IUnknown
*)*ppv
);
1936 WARN("Unknown interface %s\n", debugstr_guid(riid
));
1937 return E_NOINTERFACE
;
1940 static ULONG WINAPI
AudioClock_AddRef(IAudioClock
*iface
)
1942 ACImpl
*This
= impl_from_IAudioClock(iface
);
1943 return IAudioClient_AddRef(&This
->IAudioClient_iface
);
1946 static ULONG WINAPI
AudioClock_Release(IAudioClock
*iface
)
1948 ACImpl
*This
= impl_from_IAudioClock(iface
);
1949 return IAudioClient_Release(&This
->IAudioClient_iface
);
1952 static HRESULT WINAPI
AudioClock_GetFrequency(IAudioClock
*iface
, UINT64
*freq
)
1954 ACImpl
*This
= impl_from_IAudioClock(iface
);
1956 TRACE("(%p)->(%p)\n", This
, freq
);
1958 *freq
= This
->fmt
->nSamplesPerSec
;
1963 static HRESULT
AudioClock_GetPosition_nolock(ACImpl
*This
,
1964 UINT64
*pos
, UINT64
*qpctime
, BOOL raw
)
1966 AudioTimeStamp time
;
1969 sc
= AudioQueueGetCurrentTime(This
->aqueue
, NULL
, &time
, NULL
);
1970 if(sc
== kAudioQueueErr_InvalidRunState
){
1972 }else if(sc
== noErr
){
1973 if(!(time
.mFlags
& kAudioTimeStampSampleTimeValid
)){
1974 FIXME("Sample time not valid, should calculate from something else\n");
1979 *pos
= time
.mSampleTime
;
1981 *pos
= time
.mSampleTime
- This
->last_time
;
1983 WARN("Unable to get current time: %lx\n", sc
);
1988 LARGE_INTEGER stamp
, freq
;
1989 QueryPerformanceCounter(&stamp
);
1990 QueryPerformanceFrequency(&freq
);
1991 *qpctime
= (stamp
.QuadPart
* (INT64
)10000000) / freq
.QuadPart
;
1997 static HRESULT WINAPI
AudioClock_GetPosition(IAudioClock
*iface
, UINT64
*pos
,
2000 ACImpl
*This
= impl_from_IAudioClock(iface
);
2003 TRACE("(%p)->(%p, %p)\n", This
, pos
, qpctime
);
2008 OSSpinLockLock(&This
->lock
);
2010 hr
= AudioClock_GetPosition_nolock(This
, pos
, qpctime
, FALSE
);
2012 OSSpinLockUnlock(&This
->lock
);
2017 static HRESULT WINAPI
AudioClock_GetCharacteristics(IAudioClock
*iface
,
2020 ACImpl
*This
= impl_from_IAudioClock(iface
);
2022 TRACE("(%p)->(%p)\n", This
, chars
);
2027 *chars
= AUDIOCLOCK_CHARACTERISTIC_FIXED_FREQ
;
2032 static const IAudioClockVtbl AudioClock_Vtbl
=
2034 AudioClock_QueryInterface
,
2037 AudioClock_GetFrequency
,
2038 AudioClock_GetPosition
,
2039 AudioClock_GetCharacteristics
2042 static HRESULT WINAPI
AudioClock2_QueryInterface(IAudioClock2
*iface
,
2043 REFIID riid
, void **ppv
)
2045 ACImpl
*This
= impl_from_IAudioClock2(iface
);
2046 return IAudioClock_QueryInterface(&This
->IAudioClock_iface
, riid
, ppv
);
2049 static ULONG WINAPI
AudioClock2_AddRef(IAudioClock2
*iface
)
2051 ACImpl
*This
= impl_from_IAudioClock2(iface
);
2052 return IAudioClient_AddRef(&This
->IAudioClient_iface
);
2055 static ULONG WINAPI
AudioClock2_Release(IAudioClock2
*iface
)
2057 ACImpl
*This
= impl_from_IAudioClock2(iface
);
2058 return IAudioClient_Release(&This
->IAudioClient_iface
);
2061 static HRESULT WINAPI
AudioClock2_GetDevicePosition(IAudioClock2
*iface
,
2062 UINT64
*pos
, UINT64
*qpctime
)
2064 ACImpl
*This
= impl_from_IAudioClock2(iface
);
2066 FIXME("(%p)->(%p, %p)\n", This
, pos
, qpctime
);
2071 static const IAudioClock2Vtbl AudioClock2_Vtbl
=
2073 AudioClock2_QueryInterface
,
2075 AudioClock2_Release
,
2076 AudioClock2_GetDevicePosition
2079 static AudioSessionWrapper
*AudioSessionWrapper_Create(ACImpl
*client
)
2081 AudioSessionWrapper
*ret
;
2083 ret
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
2084 sizeof(AudioSessionWrapper
));
2088 ret
->IAudioSessionControl2_iface
.lpVtbl
= &AudioSessionControl2_Vtbl
;
2089 ret
->ISimpleAudioVolume_iface
.lpVtbl
= &SimpleAudioVolume_Vtbl
;
2090 ret
->IChannelAudioVolume_iface
.lpVtbl
= &ChannelAudioVolume_Vtbl
;
2094 ret
->client
= client
;
2096 ret
->session
= client
->session
;
2097 AudioClient_AddRef(&client
->IAudioClient_iface
);
2103 static HRESULT WINAPI
AudioSessionControl_QueryInterface(
2104 IAudioSessionControl2
*iface
, REFIID riid
, void **ppv
)
2106 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
2112 if(IsEqualIID(riid
, &IID_IUnknown
) ||
2113 IsEqualIID(riid
, &IID_IAudioSessionControl
) ||
2114 IsEqualIID(riid
, &IID_IAudioSessionControl2
))
2117 IUnknown_AddRef((IUnknown
*)*ppv
);
2121 WARN("Unknown interface %s\n", debugstr_guid(riid
));
2122 return E_NOINTERFACE
;
2125 static ULONG WINAPI
AudioSessionControl_AddRef(IAudioSessionControl2
*iface
)
2127 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2129 ref
= InterlockedIncrement(&This
->ref
);
2130 TRACE("(%p) Refcount now %u\n", This
, ref
);
2134 static ULONG WINAPI
AudioSessionControl_Release(IAudioSessionControl2
*iface
)
2136 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2138 ref
= InterlockedDecrement(&This
->ref
);
2139 TRACE("(%p) Refcount now %u\n", This
, ref
);
2142 OSSpinLockLock(&This
->client
->lock
);
2143 This
->client
->session_wrapper
= NULL
;
2144 OSSpinLockUnlock(&This
->client
->lock
);
2145 AudioClient_Release(&This
->client
->IAudioClient_iface
);
2147 HeapFree(GetProcessHeap(), 0, This
);
2152 static HRESULT WINAPI
AudioSessionControl_GetState(IAudioSessionControl2
*iface
,
2153 AudioSessionState
*state
)
2155 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2158 TRACE("(%p)->(%p)\n", This
, state
);
2161 return NULL_PTR_ERR
;
2163 EnterCriticalSection(&g_sessions_lock
);
2165 if(list_empty(&This
->session
->clients
)){
2166 *state
= AudioSessionStateExpired
;
2167 LeaveCriticalSection(&g_sessions_lock
);
2171 LIST_FOR_EACH_ENTRY(client
, &This
->session
->clients
, ACImpl
, entry
){
2172 OSSpinLockLock(&client
->lock
);
2173 if(client
->playing
== StatePlaying
||
2174 client
->playing
== StateInTransition
){
2175 *state
= AudioSessionStateActive
;
2176 OSSpinLockUnlock(&client
->lock
);
2177 LeaveCriticalSection(&g_sessions_lock
);
2180 OSSpinLockUnlock(&client
->lock
);
2183 LeaveCriticalSection(&g_sessions_lock
);
2185 *state
= AudioSessionStateInactive
;
2190 static HRESULT WINAPI
AudioSessionControl_GetDisplayName(
2191 IAudioSessionControl2
*iface
, WCHAR
**name
)
2193 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2195 FIXME("(%p)->(%p) - stub\n", This
, name
);
2200 static HRESULT WINAPI
AudioSessionControl_SetDisplayName(
2201 IAudioSessionControl2
*iface
, const WCHAR
*name
, const GUID
*session
)
2203 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2205 FIXME("(%p)->(%p, %s) - stub\n", This
, name
, debugstr_guid(session
));
2210 static HRESULT WINAPI
AudioSessionControl_GetIconPath(
2211 IAudioSessionControl2
*iface
, WCHAR
**path
)
2213 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2215 FIXME("(%p)->(%p) - stub\n", This
, path
);
2220 static HRESULT WINAPI
AudioSessionControl_SetIconPath(
2221 IAudioSessionControl2
*iface
, const WCHAR
*path
, const GUID
*session
)
2223 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2225 FIXME("(%p)->(%p, %s) - stub\n", This
, path
, debugstr_guid(session
));
2230 static HRESULT WINAPI
AudioSessionControl_GetGroupingParam(
2231 IAudioSessionControl2
*iface
, GUID
*group
)
2233 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2235 FIXME("(%p)->(%p) - stub\n", This
, group
);
2240 static HRESULT WINAPI
AudioSessionControl_SetGroupingParam(
2241 IAudioSessionControl2
*iface
, const GUID
*group
, const GUID
*session
)
2243 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2245 FIXME("(%p)->(%s, %s) - stub\n", This
, debugstr_guid(group
),
2246 debugstr_guid(session
));
2251 static HRESULT WINAPI
AudioSessionControl_RegisterAudioSessionNotification(
2252 IAudioSessionControl2
*iface
, IAudioSessionEvents
*events
)
2254 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2256 FIXME("(%p)->(%p) - stub\n", This
, events
);
2261 static HRESULT WINAPI
AudioSessionControl_UnregisterAudioSessionNotification(
2262 IAudioSessionControl2
*iface
, IAudioSessionEvents
*events
)
2264 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2266 FIXME("(%p)->(%p) - stub\n", This
, events
);
2271 static HRESULT WINAPI
AudioSessionControl_GetSessionIdentifier(
2272 IAudioSessionControl2
*iface
, WCHAR
**id
)
2274 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2276 FIXME("(%p)->(%p) - stub\n", This
, id
);
2281 static HRESULT WINAPI
AudioSessionControl_GetSessionInstanceIdentifier(
2282 IAudioSessionControl2
*iface
, WCHAR
**id
)
2284 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2286 FIXME("(%p)->(%p) - stub\n", This
, id
);
2291 static HRESULT WINAPI
AudioSessionControl_GetProcessId(
2292 IAudioSessionControl2
*iface
, DWORD
*pid
)
2294 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2296 TRACE("(%p)->(%p)\n", This
, pid
);
2301 *pid
= GetCurrentProcessId();
2306 static HRESULT WINAPI
AudioSessionControl_IsSystemSoundsSession(
2307 IAudioSessionControl2
*iface
)
2309 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2311 TRACE("(%p)\n", This
);
2316 static HRESULT WINAPI
AudioSessionControl_SetDuckingPreference(
2317 IAudioSessionControl2
*iface
, BOOL optout
)
2319 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2321 TRACE("(%p)->(%d)\n", This
, optout
);
2326 static const IAudioSessionControl2Vtbl AudioSessionControl2_Vtbl
=
2328 AudioSessionControl_QueryInterface
,
2329 AudioSessionControl_AddRef
,
2330 AudioSessionControl_Release
,
2331 AudioSessionControl_GetState
,
2332 AudioSessionControl_GetDisplayName
,
2333 AudioSessionControl_SetDisplayName
,
2334 AudioSessionControl_GetIconPath
,
2335 AudioSessionControl_SetIconPath
,
2336 AudioSessionControl_GetGroupingParam
,
2337 AudioSessionControl_SetGroupingParam
,
2338 AudioSessionControl_RegisterAudioSessionNotification
,
2339 AudioSessionControl_UnregisterAudioSessionNotification
,
2340 AudioSessionControl_GetSessionIdentifier
,
2341 AudioSessionControl_GetSessionInstanceIdentifier
,
2342 AudioSessionControl_GetProcessId
,
2343 AudioSessionControl_IsSystemSoundsSession
,
2344 AudioSessionControl_SetDuckingPreference
2347 /* index == -1 means set all channels, otherwise sets only the given channel */
2348 static HRESULT
ca_setvol(ACImpl
*This
, UINT32 index
)
2353 if(index
== (UINT32
)-1){
2356 for(i
= 0; i
< This
->fmt
->nChannels
; ++i
){
2358 hr
= ca_setvol(This
, i
);
2365 if(This
->session
->mute
)
2368 level
= This
->session
->master_vol
*
2369 This
->session
->channel_vols
[index
] * This
->vols
[index
];
2371 sc
= AudioQueueSetParameter(This
->aqueue
, kAudioQueueParam_Volume
, level
);
2373 WARN("Setting _Volume property failed: %lx\n", sc
);
2378 static HRESULT
ca_session_setvol(AudioSession
*session
, UINT32 index
)
2383 LIST_FOR_EACH_ENTRY(client
, &session
->clients
, ACImpl
, entry
){
2385 hr
= ca_setvol(client
, index
);
2393 static HRESULT WINAPI
SimpleAudioVolume_QueryInterface(
2394 ISimpleAudioVolume
*iface
, REFIID riid
, void **ppv
)
2396 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
2402 if(IsEqualIID(riid
, &IID_IUnknown
) ||
2403 IsEqualIID(riid
, &IID_ISimpleAudioVolume
))
2406 IUnknown_AddRef((IUnknown
*)*ppv
);
2410 WARN("Unknown interface %s\n", debugstr_guid(riid
));
2411 return E_NOINTERFACE
;
2414 static ULONG WINAPI
SimpleAudioVolume_AddRef(ISimpleAudioVolume
*iface
)
2416 AudioSessionWrapper
*This
= impl_from_ISimpleAudioVolume(iface
);
2417 return AudioSessionControl_AddRef(&This
->IAudioSessionControl2_iface
);
2420 static ULONG WINAPI
SimpleAudioVolume_Release(ISimpleAudioVolume
*iface
)
2422 AudioSessionWrapper
*This
= impl_from_ISimpleAudioVolume(iface
);
2423 return AudioSessionControl_Release(&This
->IAudioSessionControl2_iface
);
2426 static HRESULT WINAPI
SimpleAudioVolume_SetMasterVolume(
2427 ISimpleAudioVolume
*iface
, float level
, const GUID
*context
)
2429 AudioSessionWrapper
*This
= impl_from_ISimpleAudioVolume(iface
);
2430 AudioSession
*session
= This
->session
;
2433 TRACE("(%p)->(%f, %s)\n", session
, level
, wine_dbgstr_guid(context
));
2435 if(level
< 0.f
|| level
> 1.f
)
2436 return E_INVALIDARG
;
2439 FIXME("Notifications not supported yet\n");
2441 EnterCriticalSection(&session
->lock
);
2443 session
->master_vol
= level
;
2445 ret
= ca_session_setvol(session
, -1);
2447 LeaveCriticalSection(&session
->lock
);
2452 static HRESULT WINAPI
SimpleAudioVolume_GetMasterVolume(
2453 ISimpleAudioVolume
*iface
, float *level
)
2455 AudioSessionWrapper
*This
= impl_from_ISimpleAudioVolume(iface
);
2456 AudioSession
*session
= This
->session
;
2458 TRACE("(%p)->(%p)\n", session
, level
);
2461 return NULL_PTR_ERR
;
2463 *level
= session
->master_vol
;
2468 static HRESULT WINAPI
SimpleAudioVolume_SetMute(ISimpleAudioVolume
*iface
,
2469 BOOL mute
, const GUID
*context
)
2471 AudioSessionWrapper
*This
= impl_from_ISimpleAudioVolume(iface
);
2472 AudioSession
*session
= This
->session
;
2474 TRACE("(%p)->(%u, %p)\n", session
, mute
, context
);
2477 FIXME("Notifications not supported yet\n");
2479 EnterCriticalSection(&session
->lock
);
2481 session
->mute
= mute
;
2483 ca_session_setvol(session
, -1);
2485 LeaveCriticalSection(&session
->lock
);
2490 static HRESULT WINAPI
SimpleAudioVolume_GetMute(ISimpleAudioVolume
*iface
,
2493 AudioSessionWrapper
*This
= impl_from_ISimpleAudioVolume(iface
);
2494 AudioSession
*session
= This
->session
;
2496 TRACE("(%p)->(%p)\n", session
, mute
);
2499 return NULL_PTR_ERR
;
2501 *mute
= session
->mute
;
2506 static const ISimpleAudioVolumeVtbl SimpleAudioVolume_Vtbl
=
2508 SimpleAudioVolume_QueryInterface
,
2509 SimpleAudioVolume_AddRef
,
2510 SimpleAudioVolume_Release
,
2511 SimpleAudioVolume_SetMasterVolume
,
2512 SimpleAudioVolume_GetMasterVolume
,
2513 SimpleAudioVolume_SetMute
,
2514 SimpleAudioVolume_GetMute
2517 static HRESULT WINAPI
AudioStreamVolume_QueryInterface(
2518 IAudioStreamVolume
*iface
, REFIID riid
, void **ppv
)
2520 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
2526 if(IsEqualIID(riid
, &IID_IUnknown
) ||
2527 IsEqualIID(riid
, &IID_IAudioStreamVolume
))
2530 IUnknown_AddRef((IUnknown
*)*ppv
);
2534 WARN("Unknown interface %s\n", debugstr_guid(riid
));
2535 return E_NOINTERFACE
;
2538 static ULONG WINAPI
AudioStreamVolume_AddRef(IAudioStreamVolume
*iface
)
2540 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
2541 return IAudioClient_AddRef(&This
->IAudioClient_iface
);
2544 static ULONG WINAPI
AudioStreamVolume_Release(IAudioStreamVolume
*iface
)
2546 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
2547 return IAudioClient_Release(&This
->IAudioClient_iface
);
2550 static HRESULT WINAPI
AudioStreamVolume_GetChannelCount(
2551 IAudioStreamVolume
*iface
, UINT32
*out
)
2553 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
2555 TRACE("(%p)->(%p)\n", This
, out
);
2560 *out
= This
->fmt
->nChannels
;
2565 static HRESULT WINAPI
AudioStreamVolume_SetChannelVolume(
2566 IAudioStreamVolume
*iface
, UINT32 index
, float level
)
2568 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
2571 TRACE("(%p)->(%d, %f)\n", This
, index
, level
);
2573 if(level
< 0.f
|| level
> 1.f
)
2574 return E_INVALIDARG
;
2576 if(index
>= This
->fmt
->nChannels
)
2577 return E_INVALIDARG
;
2579 OSSpinLockLock(&This
->lock
);
2581 This
->vols
[index
] = level
;
2583 WARN("AudioQueue doesn't support per-channel volume control\n");
2584 ret
= ca_setvol(This
, index
);
2586 OSSpinLockUnlock(&This
->lock
);
2591 static HRESULT WINAPI
AudioStreamVolume_GetChannelVolume(
2592 IAudioStreamVolume
*iface
, UINT32 index
, float *level
)
2594 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
2596 TRACE("(%p)->(%d, %p)\n", This
, index
, level
);
2601 if(index
>= This
->fmt
->nChannels
)
2602 return E_INVALIDARG
;
2604 *level
= This
->vols
[index
];
2609 static HRESULT WINAPI
AudioStreamVolume_SetAllVolumes(
2610 IAudioStreamVolume
*iface
, UINT32 count
, const float *levels
)
2612 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
2616 TRACE("(%p)->(%d, %p)\n", This
, count
, levels
);
2621 if(count
!= This
->fmt
->nChannels
)
2622 return E_INVALIDARG
;
2624 OSSpinLockLock(&This
->lock
);
2626 for(i
= 0; i
< count
; ++i
)
2627 This
->vols
[i
] = levels
[i
];
2629 ret
= ca_setvol(This
, -1);
2631 OSSpinLockUnlock(&This
->lock
);
2636 static HRESULT WINAPI
AudioStreamVolume_GetAllVolumes(
2637 IAudioStreamVolume
*iface
, UINT32 count
, float *levels
)
2639 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
2642 TRACE("(%p)->(%d, %p)\n", This
, count
, levels
);
2647 if(count
!= This
->fmt
->nChannels
)
2648 return E_INVALIDARG
;
2650 OSSpinLockLock(&This
->lock
);
2652 for(i
= 0; i
< count
; ++i
)
2653 levels
[i
] = This
->vols
[i
];
2655 OSSpinLockUnlock(&This
->lock
);
2660 static const IAudioStreamVolumeVtbl AudioStreamVolume_Vtbl
=
2662 AudioStreamVolume_QueryInterface
,
2663 AudioStreamVolume_AddRef
,
2664 AudioStreamVolume_Release
,
2665 AudioStreamVolume_GetChannelCount
,
2666 AudioStreamVolume_SetChannelVolume
,
2667 AudioStreamVolume_GetChannelVolume
,
2668 AudioStreamVolume_SetAllVolumes
,
2669 AudioStreamVolume_GetAllVolumes
2672 static HRESULT WINAPI
ChannelAudioVolume_QueryInterface(
2673 IChannelAudioVolume
*iface
, REFIID riid
, void **ppv
)
2675 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
2681 if(IsEqualIID(riid
, &IID_IUnknown
) ||
2682 IsEqualIID(riid
, &IID_IChannelAudioVolume
))
2685 IUnknown_AddRef((IUnknown
*)*ppv
);
2689 WARN("Unknown interface %s\n", debugstr_guid(riid
));
2690 return E_NOINTERFACE
;
2693 static ULONG WINAPI
ChannelAudioVolume_AddRef(IChannelAudioVolume
*iface
)
2695 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
2696 return AudioSessionControl_AddRef(&This
->IAudioSessionControl2_iface
);
2699 static ULONG WINAPI
ChannelAudioVolume_Release(IChannelAudioVolume
*iface
)
2701 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
2702 return AudioSessionControl_Release(&This
->IAudioSessionControl2_iface
);
2705 static HRESULT WINAPI
ChannelAudioVolume_GetChannelCount(
2706 IChannelAudioVolume
*iface
, UINT32
*out
)
2708 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
2709 AudioSession
*session
= This
->session
;
2711 TRACE("(%p)->(%p)\n", session
, out
);
2714 return NULL_PTR_ERR
;
2716 *out
= session
->channel_count
;
2721 static HRESULT WINAPI
ChannelAudioVolume_SetChannelVolume(
2722 IChannelAudioVolume
*iface
, UINT32 index
, float level
,
2723 const GUID
*context
)
2725 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
2726 AudioSession
*session
= This
->session
;
2729 TRACE("(%p)->(%d, %f, %s)\n", session
, index
, level
,
2730 wine_dbgstr_guid(context
));
2732 if(level
< 0.f
|| level
> 1.f
)
2733 return E_INVALIDARG
;
2735 if(index
>= session
->channel_count
)
2736 return E_INVALIDARG
;
2739 FIXME("Notifications not supported yet\n");
2741 EnterCriticalSection(&session
->lock
);
2743 session
->channel_vols
[index
] = level
;
2745 WARN("AudioQueue doesn't support per-channel volume control\n");
2746 ret
= ca_session_setvol(session
, index
);
2748 LeaveCriticalSection(&session
->lock
);
2753 static HRESULT WINAPI
ChannelAudioVolume_GetChannelVolume(
2754 IChannelAudioVolume
*iface
, UINT32 index
, float *level
)
2756 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
2757 AudioSession
*session
= This
->session
;
2759 TRACE("(%p)->(%d, %p)\n", session
, index
, level
);
2762 return NULL_PTR_ERR
;
2764 if(index
>= session
->channel_count
)
2765 return E_INVALIDARG
;
2767 *level
= session
->channel_vols
[index
];
2772 static HRESULT WINAPI
ChannelAudioVolume_SetAllVolumes(
2773 IChannelAudioVolume
*iface
, UINT32 count
, const float *levels
,
2774 const GUID
*context
)
2776 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
2777 AudioSession
*session
= This
->session
;
2781 TRACE("(%p)->(%d, %p, %s)\n", session
, count
, levels
,
2782 wine_dbgstr_guid(context
));
2785 return NULL_PTR_ERR
;
2787 if(count
!= session
->channel_count
)
2788 return E_INVALIDARG
;
2791 FIXME("Notifications not supported yet\n");
2793 EnterCriticalSection(&session
->lock
);
2795 for(i
= 0; i
< count
; ++i
)
2796 session
->channel_vols
[i
] = levels
[i
];
2798 ret
= ca_session_setvol(session
, -1);
2800 LeaveCriticalSection(&session
->lock
);
2805 static HRESULT WINAPI
ChannelAudioVolume_GetAllVolumes(
2806 IChannelAudioVolume
*iface
, UINT32 count
, float *levels
)
2808 AudioSessionWrapper
*This
= impl_from_IChannelAudioVolume(iface
);
2809 AudioSession
*session
= This
->session
;
2812 TRACE("(%p)->(%d, %p)\n", session
, count
, levels
);
2815 return NULL_PTR_ERR
;
2817 if(count
!= session
->channel_count
)
2818 return E_INVALIDARG
;
2820 for(i
= 0; i
< count
; ++i
)
2821 levels
[i
] = session
->channel_vols
[i
];
2826 static const IChannelAudioVolumeVtbl ChannelAudioVolume_Vtbl
=
2828 ChannelAudioVolume_QueryInterface
,
2829 ChannelAudioVolume_AddRef
,
2830 ChannelAudioVolume_Release
,
2831 ChannelAudioVolume_GetChannelCount
,
2832 ChannelAudioVolume_SetChannelVolume
,
2833 ChannelAudioVolume_GetChannelVolume
,
2834 ChannelAudioVolume_SetAllVolumes
,
2835 ChannelAudioVolume_GetAllVolumes
2838 static HRESULT WINAPI
AudioSessionManager_QueryInterface(IAudioSessionManager2
*iface
,
2839 REFIID riid
, void **ppv
)
2841 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
2847 if(IsEqualIID(riid
, &IID_IUnknown
) ||
2848 IsEqualIID(riid
, &IID_IAudioSessionManager
) ||
2849 IsEqualIID(riid
, &IID_IAudioSessionManager2
))
2852 IUnknown_AddRef((IUnknown
*)*ppv
);
2856 WARN("Unknown interface %s\n", debugstr_guid(riid
));
2857 return E_NOINTERFACE
;
2860 static ULONG WINAPI
AudioSessionManager_AddRef(IAudioSessionManager2
*iface
)
2862 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
2864 ref
= InterlockedIncrement(&This
->ref
);
2865 TRACE("(%p) Refcount now %u\n", This
, ref
);
2869 static ULONG WINAPI
AudioSessionManager_Release(IAudioSessionManager2
*iface
)
2871 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
2873 ref
= InterlockedDecrement(&This
->ref
);
2874 TRACE("(%p) Refcount now %u\n", This
, ref
);
2876 HeapFree(GetProcessHeap(), 0, This
);
2880 static HRESULT WINAPI
AudioSessionManager_GetAudioSessionControl(
2881 IAudioSessionManager2
*iface
, const GUID
*session_guid
, DWORD flags
,
2882 IAudioSessionControl
**out
)
2884 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
2885 AudioSession
*session
;
2886 AudioSessionWrapper
*wrapper
;
2889 TRACE("(%p)->(%s, %x, %p)\n", This
, debugstr_guid(session_guid
),
2892 hr
= get_audio_session(session_guid
, This
->device
, 0, &session
);
2896 wrapper
= AudioSessionWrapper_Create(NULL
);
2898 return E_OUTOFMEMORY
;
2900 wrapper
->session
= session
;
2902 *out
= (IAudioSessionControl
*)&wrapper
->IAudioSessionControl2_iface
;
2907 static HRESULT WINAPI
AudioSessionManager_GetSimpleAudioVolume(
2908 IAudioSessionManager2
*iface
, const GUID
*session_guid
, DWORD flags
,
2909 ISimpleAudioVolume
**out
)
2911 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
2912 AudioSession
*session
;
2913 AudioSessionWrapper
*wrapper
;
2916 TRACE("(%p)->(%s, %x, %p)\n", This
, debugstr_guid(session_guid
),
2919 hr
= get_audio_session(session_guid
, This
->device
, 0, &session
);
2923 wrapper
= AudioSessionWrapper_Create(NULL
);
2925 return E_OUTOFMEMORY
;
2927 wrapper
->session
= session
;
2929 *out
= &wrapper
->ISimpleAudioVolume_iface
;
2934 static HRESULT WINAPI
AudioSessionManager_GetSessionEnumerator(
2935 IAudioSessionManager2
*iface
, IAudioSessionEnumerator
**out
)
2937 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
2938 FIXME("(%p)->(%p) - stub\n", This
, out
);
2942 static HRESULT WINAPI
AudioSessionManager_RegisterSessionNotification(
2943 IAudioSessionManager2
*iface
, IAudioSessionNotification
*notification
)
2945 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
2946 FIXME("(%p)->(%p) - stub\n", This
, notification
);
2950 static HRESULT WINAPI
AudioSessionManager_UnregisterSessionNotification(
2951 IAudioSessionManager2
*iface
, IAudioSessionNotification
*notification
)
2953 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
2954 FIXME("(%p)->(%p) - stub\n", This
, notification
);
2958 static HRESULT WINAPI
AudioSessionManager_RegisterDuckNotification(
2959 IAudioSessionManager2
*iface
, const WCHAR
*session_id
,
2960 IAudioVolumeDuckNotification
*notification
)
2962 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
2963 FIXME("(%p)->(%p) - stub\n", This
, notification
);
2967 static HRESULT WINAPI
AudioSessionManager_UnregisterDuckNotification(
2968 IAudioSessionManager2
*iface
,
2969 IAudioVolumeDuckNotification
*notification
)
2971 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
2972 FIXME("(%p)->(%p) - stub\n", This
, notification
);
2976 static const IAudioSessionManager2Vtbl AudioSessionManager2_Vtbl
=
2978 AudioSessionManager_QueryInterface
,
2979 AudioSessionManager_AddRef
,
2980 AudioSessionManager_Release
,
2981 AudioSessionManager_GetAudioSessionControl
,
2982 AudioSessionManager_GetSimpleAudioVolume
,
2983 AudioSessionManager_GetSessionEnumerator
,
2984 AudioSessionManager_RegisterSessionNotification
,
2985 AudioSessionManager_UnregisterSessionNotification
,
2986 AudioSessionManager_RegisterDuckNotification
,
2987 AudioSessionManager_UnregisterDuckNotification
2990 HRESULT WINAPI
AUDDRV_GetAudioSessionManager(IMMDevice
*device
,
2991 IAudioSessionManager2
**out
)
2995 This
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(SessionMgr
));
2997 return E_OUTOFMEMORY
;
2999 This
->IAudioSessionManager2_iface
.lpVtbl
= &AudioSessionManager2_Vtbl
;
3000 This
->device
= device
;
3003 *out
= &This
->IAudioSessionManager2_iface
;