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 CRITICAL_SECTION_DEBUG g_sessions_lock_debug =
177 0, 0, &g_sessions_lock,
178 { &g_sessions_lock_debug.ProcessLocksList, &g_sessions_lock_debug.ProcessLocksList },
179 0, 0, { (DWORD_PTR)(__FILE__ ": g_sessions_lock") }
181 static CRITICAL_SECTION g_sessions_lock = { &g_sessions_lock_debug, -1, 0, 0, 0, 0 };
182 static struct list g_sessions = LIST_INIT(g_sessions);
184 static HRESULT AudioClock_GetPosition_nolock(ACImpl *This, UINT64 *pos,
185 UINT64 *qpctime, BOOL raw);
186 static AudioSessionWrapper *AudioSessionWrapper_Create(ACImpl *client);
187 static HRESULT ca_setvol(ACImpl *This, UINT32 index);
189 static inline ACImpl *impl_from_IAudioClient(IAudioClient *iface)
191 return CONTAINING_RECORD(iface, ACImpl, IAudioClient_iface);
194 static inline ACImpl *impl_from_IAudioRenderClient(IAudioRenderClient *iface)
196 return CONTAINING_RECORD(iface, ACImpl, IAudioRenderClient_iface);
199 static inline ACImpl *impl_from_IAudioCaptureClient(IAudioCaptureClient *iface)
201 return CONTAINING_RECORD(iface, ACImpl, IAudioCaptureClient_iface);
204 static inline AudioSessionWrapper *impl_from_IAudioSessionControl2(IAudioSessionControl2 *iface)
206 return CONTAINING_RECORD(iface, AudioSessionWrapper, IAudioSessionControl2_iface);
209 static inline AudioSessionWrapper *impl_from_ISimpleAudioVolume(ISimpleAudioVolume *iface)
211 return CONTAINING_RECORD(iface, AudioSessionWrapper, ISimpleAudioVolume_iface);
214 static inline AudioSessionWrapper *impl_from_IChannelAudioVolume(IChannelAudioVolume *iface)
216 return CONTAINING_RECORD(iface, AudioSessionWrapper, IChannelAudioVolume_iface);
219 static inline ACImpl *impl_from_IAudioClock(IAudioClock *iface)
221 return CONTAINING_RECORD(iface, ACImpl, IAudioClock_iface);
224 static inline ACImpl *impl_from_IAudioClock2(IAudioClock2 *iface)
226 return CONTAINING_RECORD(iface, ACImpl, IAudioClock2_iface);
229 static inline ACImpl *impl_from_IAudioStreamVolume(IAudioStreamVolume *iface)
231 return CONTAINING_RECORD(iface, ACImpl, IAudioStreamVolume_iface);
234 static inline SessionMgr *impl_from_IAudioSessionManager2(IAudioSessionManager2 *iface)
236 return CONTAINING_RECORD(iface, SessionMgr, IAudioSessionManager2_iface);
239 BOOL WINAPI DllMain(HINSTANCE dll, DWORD reason, void *reserved)
243 case DLL_PROCESS_ATTACH:
244 g_timer_q = CreateTimerQueue();
249 case DLL_PROCESS_DETACH:
250 g_sessions_lock.DebugInfo->Spare[0] = 0;
251 DeleteCriticalSection(&g_sessions_lock);
257 /* From <dlls/mmdevapi/mmdevapi.h> */
258 enum DriverPriority {
259 Priority_Unavailable = 0,
265 int WINAPI AUDDRV_GetPriority(void)
267 return Priority_Neutral;
270 HRESULT WINAPI AUDDRV_GetEndpointIDs(EDataFlow flow, WCHAR ***ids,
271 AudioDeviceID ***keys, UINT *num, UINT *def_index)
273 UInt32 devsize, size;
274 AudioDeviceID *devices;
275 AudioDeviceID default_id;
276 AudioObjectPropertyAddress addr;
280 TRACE("%d %p %p %p\n", flow, ids, num, def_index);
282 addr.mScope = kAudioObjectPropertyScopeGlobal;
283 addr.mElement = kAudioObjectPropertyElementMaster;
285 addr.mSelector = kAudioHardwarePropertyDefaultOutputDevice;
286 else if(flow == eCapture)
287 addr.mSelector = kAudioHardwarePropertyDefaultInputDevice;
291 size = sizeof(default_id);
292 sc = AudioObjectGetPropertyData(kAudioObjectSystemObject, &addr, 0,
293 NULL, &size, &default_id);
295 WARN("Getting _DefaultInputDevice property failed: %lx\n", sc);
299 addr.mSelector = kAudioHardwarePropertyDevices;
300 sc = AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &addr, 0,
303 WARN("Getting _Devices property size failed: %lx\n", sc);
307 devices = HeapAlloc(GetProcessHeap(), 0, devsize);
309 return E_OUTOFMEMORY;
311 sc = AudioObjectGetPropertyData(kAudioObjectSystemObject, &addr, 0, NULL,
314 WARN("Getting _Devices property failed: %lx\n", sc);
315 HeapFree(GetProcessHeap(), 0, devices);
319 ndevices = devsize / sizeof(AudioDeviceID);
321 *ids = HeapAlloc(GetProcessHeap(), 0, ndevices * sizeof(WCHAR *));
323 HeapFree(GetProcessHeap(), 0, devices);
324 return E_OUTOFMEMORY;
327 *keys = HeapAlloc(GetProcessHeap(), 0, ndevices * sizeof(AudioDeviceID *));
329 HeapFree(GetProcessHeap(), 0, *ids);
330 HeapFree(GetProcessHeap(), 0, devices);
331 return E_OUTOFMEMORY;
335 *def_index = (UINT)-1;
336 for(i = 0; i < ndevices; ++i){
337 AudioBufferList *buffers;
342 addr.mSelector = kAudioDevicePropertyStreamConfiguration;
344 addr.mScope = kAudioDevicePropertyScopeOutput;
346 addr.mScope = kAudioDevicePropertyScopeInput;
348 sc = AudioObjectGetPropertyDataSize(devices[i], &addr, 0, NULL, &size);
350 WARN("Unable to get _StreamConfiguration property size for "
351 "device %lu: %lx\n", devices[i], sc);
355 buffers = HeapAlloc(GetProcessHeap(), 0, size);
357 HeapFree(GetProcessHeap(), 0, devices);
358 for(j = 0; j < *num; ++j){
359 HeapFree(GetProcessHeap(), 0, (*ids)[j]);
360 HeapFree(GetProcessHeap(), 0, (*keys)[j]);
362 HeapFree(GetProcessHeap(), 0, *keys);
363 HeapFree(GetProcessHeap(), 0, *ids);
364 return E_OUTOFMEMORY;
367 sc = AudioObjectGetPropertyData(devices[i], &addr, 0, NULL,
370 WARN("Unable to get _StreamConfiguration property for "
371 "device %lu: %lx\n", devices[i], sc);
372 HeapFree(GetProcessHeap(), 0, buffers);
376 /* check that there's at least one channel in this device before
377 * we claim it as usable */
378 for(j = 0; j < buffers->mNumberBuffers; ++j)
379 if(buffers->mBuffers[j].mNumberChannels > 0)
381 if(j >= buffers->mNumberBuffers){
382 HeapFree(GetProcessHeap(), 0, buffers);
386 HeapFree(GetProcessHeap(), 0, buffers);
389 addr.mSelector = kAudioObjectPropertyName;
390 sc = AudioObjectGetPropertyData(devices[i], &addr, 0, NULL,
393 WARN("Unable to get _Name property for device %lu: %lx\n",
398 len = CFStringGetLength(name) + 1;
399 (*ids)[*num] = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
402 HeapFree(GetProcessHeap(), 0, devices);
403 for(j = 0; j < *num; ++j){
404 HeapFree(GetProcessHeap(), 0, (*ids)[j]);
405 HeapFree(GetProcessHeap(), 0, (*keys)[j]);
407 HeapFree(GetProcessHeap(), 0, *ids);
408 HeapFree(GetProcessHeap(), 0, *keys);
409 return E_OUTOFMEMORY;
411 CFStringGetCharacters(name, CFRangeMake(0, len - 1), (UniChar*)(*ids)[*num]);
412 ((*ids)[*num])[len - 1] = 0;
415 (*keys)[*num] = HeapAlloc(GetProcessHeap(), 0, sizeof(AudioDeviceID));
417 HeapFree(GetProcessHeap(), 0, devices);
418 HeapFree(GetProcessHeap(), 0, (*ids)[*num]);
419 for(j = 0; j < *num; ++j){
420 HeapFree(GetProcessHeap(), 0, (*ids)[j]);
421 HeapFree(GetProcessHeap(), 0, (*keys)[j]);
423 HeapFree(GetProcessHeap(), 0, *ids);
424 HeapFree(GetProcessHeap(), 0, *keys);
425 return E_OUTOFMEMORY;
427 *(*keys)[*num] = devices[i];
429 if(*def_index == (UINT)-1 && devices[i] == default_id)
432 TRACE("device %u: id %s key %u%s\n", *num, debugstr_w((*ids)[*num]),
433 (unsigned int)*(*keys)[*num], (*def_index == *num) ? " (default)" : "");
438 if(*def_index == (UINT)-1)
441 HeapFree(GetProcessHeap(), 0, devices);
446 HRESULT WINAPI AUDDRV_GetAudioEndpoint(AudioDeviceID *adevid, IMMDevice *dev,
447 EDataFlow dataflow, IAudioClient **out)
451 TRACE("%u %p %d %p\n", (unsigned int)*adevid, dev, dataflow, out);
453 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ACImpl));
455 return E_OUTOFMEMORY;
457 This->IAudioClient_iface.lpVtbl = &AudioClient_Vtbl;
458 This->IAudioRenderClient_iface.lpVtbl = &AudioRenderClient_Vtbl;
459 This->IAudioCaptureClient_iface.lpVtbl = &AudioCaptureClient_Vtbl;
460 This->IAudioClock_iface.lpVtbl = &AudioClock_Vtbl;
461 This->IAudioClock2_iface.lpVtbl = &AudioClock2_Vtbl;
462 This->IAudioStreamVolume_iface.lpVtbl = &AudioStreamVolume_Vtbl;
464 This->dataflow = dataflow;
466 if(dataflow == eRender)
467 This->scope = kAudioDevicePropertyScopeOutput;
468 else if(dataflow == eCapture)
469 This->scope = kAudioDevicePropertyScopeInput;
471 HeapFree(GetProcessHeap(), 0, This);
478 IMMDevice_AddRef(This->parent);
480 list_init(&This->avail_buffers);
482 This->adevid = *adevid;
484 *out = &This->IAudioClient_iface;
485 IAudioClient_AddRef(&This->IAudioClient_iface);
490 static HRESULT WINAPI AudioClient_QueryInterface(IAudioClient *iface,
491 REFIID riid, void **ppv)
493 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
498 if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IAudioClient))
501 IUnknown_AddRef((IUnknown*)*ppv);
504 WARN("Unknown interface %s\n", debugstr_guid(riid));
505 return E_NOINTERFACE;
508 static ULONG WINAPI AudioClient_AddRef(IAudioClient *iface)
510 ACImpl *This = impl_from_IAudioClient(iface);
512 ref = InterlockedIncrement(&This->ref);
513 TRACE("(%p) Refcount now %u\n", This, ref);
517 static ULONG WINAPI AudioClient_Release(IAudioClient *iface)
519 ACImpl *This = impl_from_IAudioClient(iface);
521 ref = InterlockedDecrement(&This->ref);
522 TRACE("(%p) Refcount now %u\n", This, ref);
525 AQBuffer *buf, *next;
526 if(This->public_buffer){
527 buf = This->public_buffer->mUserData;
528 list_add_tail(&This->avail_buffers, &buf->entry);
530 IAudioClient_Stop(iface);
531 AudioQueueStop(This->aqueue, 1);
532 /* Stopped synchronously, all buffers returned. */
533 LIST_FOR_EACH_ENTRY_SAFE(buf, next, &This->avail_buffers, AQBuffer, entry){
534 AudioQueueFreeBuffer(This->aqueue, buf->buf);
535 HeapFree(GetProcessHeap(), 0, buf);
537 AudioQueueDispose(This->aqueue, 1);
540 EnterCriticalSection(&g_sessions_lock);
541 list_remove(&This->entry);
542 LeaveCriticalSection(&g_sessions_lock);
544 HeapFree(GetProcessHeap(), 0, This->vols);
545 CoTaskMemFree(This->fmt);
546 IMMDevice_Release(This->parent);
547 HeapFree(GetProcessHeap(), 0, This);
552 static void dump_fmt(const WAVEFORMATEX *fmt)
554 TRACE("wFormatTag: 0x%x (", fmt->wFormatTag);
555 switch(fmt->wFormatTag){
556 case WAVE_FORMAT_PCM:
557 TRACE("WAVE_FORMAT_PCM");
559 case WAVE_FORMAT_IEEE_FLOAT:
560 TRACE("WAVE_FORMAT_IEEE_FLOAT");
562 case WAVE_FORMAT_EXTENSIBLE:
563 TRACE("WAVE_FORMAT_EXTENSIBLE");
571 TRACE("nChannels: %u\n", fmt->nChannels);
572 TRACE("nSamplesPerSec: %u\n", fmt->nSamplesPerSec);
573 TRACE("nAvgBytesPerSec: %u\n", fmt->nAvgBytesPerSec);
574 TRACE("nBlockAlign: %u\n", fmt->nBlockAlign);
575 TRACE("wBitsPerSample: %u\n", fmt->wBitsPerSample);
576 TRACE("cbSize: %u\n", fmt->cbSize);
578 if(fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE){
579 WAVEFORMATEXTENSIBLE *fmtex = (void*)fmt;
580 TRACE("dwChannelMask: %08x\n", fmtex->dwChannelMask);
581 TRACE("Samples: %04x\n", fmtex->Samples.wReserved);
582 TRACE("SubFormat: %s\n", wine_dbgstr_guid(&fmtex->SubFormat));
586 static DWORD get_channel_mask(unsigned int channels)
592 return KSAUDIO_SPEAKER_MONO;
594 return KSAUDIO_SPEAKER_STEREO;
596 return KSAUDIO_SPEAKER_STEREO | SPEAKER_LOW_FREQUENCY;
598 return KSAUDIO_SPEAKER_QUAD; /* not _SURROUND */
600 return KSAUDIO_SPEAKER_QUAD | SPEAKER_LOW_FREQUENCY;
602 return KSAUDIO_SPEAKER_5POINT1; /* not 5POINT1_SURROUND */
604 return KSAUDIO_SPEAKER_5POINT1 | SPEAKER_BACK_CENTER;
606 return KSAUDIO_SPEAKER_7POINT1; /* not 7POINT1_SURROUND */
608 FIXME("Unknown speaker configuration: %u\n", channels);
612 static WAVEFORMATEX *clone_format(const WAVEFORMATEX *fmt)
617 if(fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
618 size = sizeof(WAVEFORMATEXTENSIBLE);
620 size = sizeof(WAVEFORMATEX);
622 ret = CoTaskMemAlloc(size);
626 memcpy(ret, fmt, size);
628 ret->cbSize = size - sizeof(WAVEFORMATEX);
633 static HRESULT ca_get_audiodesc(AudioStreamBasicDescription *desc,
634 const WAVEFORMATEX *fmt)
636 const WAVEFORMATEXTENSIBLE *fmtex = (const WAVEFORMATEXTENSIBLE *)fmt;
638 desc->mFormatFlags = 0;
640 if(fmt->wFormatTag == WAVE_FORMAT_PCM ||
641 (fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
642 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM))){
643 desc->mFormatID = kAudioFormatLinearPCM;
644 if(fmt->wBitsPerSample > 8)
645 desc->mFormatFlags = kAudioFormatFlagIsSignedInteger;
646 }else if(fmt->wFormatTag == WAVE_FORMAT_IEEE_FLOAT ||
647 (fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
648 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT))){
649 desc->mFormatID = kAudioFormatLinearPCM;
650 desc->mFormatFlags = kAudioFormatFlagIsFloat;
651 }else if(fmt->wFormatTag == WAVE_FORMAT_MULAW ||
652 (fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
653 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_MULAW))){
654 desc->mFormatID = kAudioFormatULaw;
655 }else if(fmt->wFormatTag == WAVE_FORMAT_ALAW ||
656 (fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
657 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_ALAW))){
658 desc->mFormatID = kAudioFormatALaw;
660 return AUDCLNT_E_UNSUPPORTED_FORMAT;
662 desc->mSampleRate = fmt->nSamplesPerSec;
663 desc->mBytesPerPacket = fmt->nBlockAlign;
664 desc->mFramesPerPacket = 1;
665 desc->mBytesPerFrame = fmt->nBlockAlign;
666 desc->mChannelsPerFrame = fmt->nChannels;
667 desc->mBitsPerChannel = fmt->wBitsPerSample;
673 static void ca_out_buffer_cb(void *user, AudioQueueRef aqueue,
674 AudioQueueBufferRef buffer)
677 AQBuffer *buf = buffer->mUserData;
679 OSSpinLockLock(&This->lock);
680 list_add_tail(&This->avail_buffers, &buf->entry);
681 This->inbuf_frames -= buffer->mAudioDataByteSize / This->fmt->nBlockAlign;
682 OSSpinLockUnlock(&This->lock);
685 static void ca_in_buffer_cb(void *user, AudioQueueRef aqueue,
686 AudioQueueBufferRef buffer, const AudioTimeStamp *start,
687 UInt32 ndesc, const AudioStreamPacketDescription *descs)
690 AQBuffer *buf = buffer->mUserData;
692 OSSpinLockLock(&This->lock);
693 list_add_tail(&This->avail_buffers, &buf->entry);
694 This->inbuf_frames += buffer->mAudioDataByteSize / This->fmt->nBlockAlign;
695 OSSpinLockUnlock(&This->lock);
698 static HRESULT ca_setup_aqueue(AudioDeviceID did, EDataFlow flow,
699 const WAVEFORMATEX *fmt, void *user, AudioQueueRef *aqueue)
701 AudioStreamBasicDescription desc;
702 AudioObjectPropertyAddress addr;
708 addr.mScope = kAudioObjectPropertyScopeGlobal;
710 addr.mSelector = kAudioDevicePropertyDeviceUID;
713 sc = AudioObjectGetPropertyData(did, &addr, 0, NULL, &size, &uid);
715 WARN("Unable to get _DeviceUID property: %lx\n", sc);
719 hr = ca_get_audiodesc(&desc, fmt);
726 sc = AudioQueueNewOutput(&desc, ca_out_buffer_cb, user, NULL, NULL, 0,
728 else if(flow == eCapture)
729 sc = AudioQueueNewInput(&desc, ca_in_buffer_cb, user, NULL, NULL, 0,
736 WARN("Unable to create AudioQueue: %lx\n", sc);
741 sc = AudioQueueSetProperty(*aqueue, kAudioQueueProperty_CurrentDevice,
753 static void session_init_vols(AudioSession *session, UINT channels)
755 if(session->channel_count < channels){
758 if(session->channel_vols)
759 session->channel_vols = HeapReAlloc(GetProcessHeap(), 0,
760 session->channel_vols, sizeof(float) * channels);
762 session->channel_vols = HeapAlloc(GetProcessHeap(), 0,
763 sizeof(float) * channels);
764 if(!session->channel_vols)
767 for(i = session->channel_count; i < channels; ++i)
768 session->channel_vols[i] = 1.f;
770 session->channel_count = channels;
774 static AudioSession *create_session(const GUID *guid, IMMDevice *device,
779 ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(AudioSession));
783 memcpy(&ret->guid, guid, sizeof(GUID));
785 ret->device = device;
787 list_init(&ret->clients);
789 list_add_head(&g_sessions, &ret->entry);
791 InitializeCriticalSection(&ret->lock);
792 ret->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": AudioSession.lock");
794 session_init_vols(ret, num_channels);
796 ret->master_vol = 1.f;
801 /* if channels == 0, then this will return or create a session with
802 * matching dataflow and GUID. otherwise, channels must also match */
803 static HRESULT get_audio_session(const GUID *sessionguid,
804 IMMDevice *device, UINT channels, AudioSession **out)
806 AudioSession *session;
808 if(!sessionguid || IsEqualGUID(sessionguid, &GUID_NULL)){
809 *out = create_session(&GUID_NULL, device, channels);
811 return E_OUTOFMEMORY;
817 LIST_FOR_EACH_ENTRY(session, &g_sessions, AudioSession, entry){
818 if(session->device == device &&
819 IsEqualGUID(sessionguid, &session->guid)){
820 session_init_vols(session, channels);
827 *out = create_session(sessionguid, device, channels);
829 return E_OUTOFMEMORY;
835 static HRESULT WINAPI AudioClient_Initialize(IAudioClient *iface,
836 AUDCLNT_SHAREMODE mode, DWORD flags, REFERENCE_TIME duration,
837 REFERENCE_TIME period, const WAVEFORMATEX *fmt,
838 const GUID *sessionguid)
840 ACImpl *This = impl_from_IAudioClient(iface);
845 TRACE("(%p)->(%x, %x, %s, %s, %p, %s)\n", This, mode, flags,
846 wine_dbgstr_longlong(duration), wine_dbgstr_longlong(period), fmt, debugstr_guid(sessionguid));
853 if(mode != AUDCLNT_SHAREMODE_SHARED && mode != AUDCLNT_SHAREMODE_EXCLUSIVE)
854 return AUDCLNT_E_NOT_INITIALIZED;
856 if(flags & ~(AUDCLNT_STREAMFLAGS_CROSSPROCESS |
857 AUDCLNT_STREAMFLAGS_LOOPBACK |
858 AUDCLNT_STREAMFLAGS_EVENTCALLBACK |
859 AUDCLNT_STREAMFLAGS_NOPERSIST |
860 AUDCLNT_STREAMFLAGS_RATEADJUST |
861 AUDCLNT_SESSIONFLAGS_EXPIREWHENUNOWNED |
862 AUDCLNT_SESSIONFLAGS_DISPLAY_HIDE |
863 AUDCLNT_SESSIONFLAGS_DISPLAY_HIDEWHENEXPIRED)){
864 TRACE("Unknown flags: %08x\n", flags);
868 OSSpinLockLock(&This->lock);
871 OSSpinLockUnlock(&This->lock);
872 return AUDCLNT_E_ALREADY_INITIALIZED;
875 hr = ca_setup_aqueue(This->adevid, This->dataflow, fmt, This, &This->aqueue);
877 OSSpinLockUnlock(&This->lock);
881 This->fmt = clone_format(fmt);
883 AudioQueueDispose(This->aqueue, 1);
885 OSSpinLockUnlock(&This->lock);
886 return E_OUTOFMEMORY;
890 This->period_ms = period / 10000;
891 if(This->period_ms == 0)
894 This->period_ms = MinimumPeriod / 10000;
897 duration = 300000; /* 0.03s */
898 This->bufsize_frames = ceil(fmt->nSamplesPerSec * (duration / 10000000.));
900 if(This->dataflow == eCapture){
902 UInt32 bsize = ceil((This->bufsize_frames / (double)CAPTURE_BUFFERS) *
903 This->fmt->nBlockAlign);
904 for(i = 0; i < CAPTURE_BUFFERS; ++i){
907 buf = HeapAlloc(GetProcessHeap(), 0, sizeof(AQBuffer));
909 AudioQueueDispose(This->aqueue, 1);
911 CoTaskMemFree(This->fmt);
913 OSSpinLockUnlock(&This->lock);
914 return E_OUTOFMEMORY;
917 sc = AudioQueueAllocateBuffer(This->aqueue, bsize, &buf->buf);
919 AudioQueueDispose(This->aqueue, 1);
921 CoTaskMemFree(This->fmt);
923 OSSpinLockUnlock(&This->lock);
924 WARN("Couldn't allocate buffer: %lx\n", sc);
928 buf->buf->mUserData = buf;
930 sc = AudioQueueEnqueueBuffer(This->aqueue, buf->buf, 0, NULL);
932 ERR("Couldn't enqueue buffer: %lx\n", sc);
938 This->vols = HeapAlloc(GetProcessHeap(), 0, fmt->nChannels * sizeof(float));
940 AudioQueueDispose(This->aqueue, 1);
942 CoTaskMemFree(This->fmt);
944 OSSpinLockUnlock(&This->lock);
945 return E_OUTOFMEMORY;
948 for(i = 0; i < fmt->nChannels; ++i)
954 EnterCriticalSection(&g_sessions_lock);
956 hr = get_audio_session(sessionguid, This->parent, fmt->nChannels,
959 LeaveCriticalSection(&g_sessions_lock);
960 AudioQueueDispose(This->aqueue, 1);
962 CoTaskMemFree(This->fmt);
964 HeapFree(GetProcessHeap(), 0, This->vols);
966 OSSpinLockUnlock(&This->lock);
970 list_add_tail(&This->session->clients, &This->entry);
972 LeaveCriticalSection(&g_sessions_lock);
976 OSSpinLockUnlock(&This->lock);
981 static HRESULT WINAPI AudioClient_GetBufferSize(IAudioClient *iface,
984 ACImpl *This = impl_from_IAudioClient(iface);
986 TRACE("(%p)->(%p)\n", This, frames);
991 OSSpinLockLock(&This->lock);
994 OSSpinLockUnlock(&This->lock);
995 return AUDCLNT_E_NOT_INITIALIZED;
998 *frames = This->bufsize_frames;
1000 OSSpinLockUnlock(&This->lock);
1005 static HRESULT ca_get_max_stream_latency(ACImpl *This, UInt32 *max)
1007 AudioObjectPropertyAddress addr;
1013 addr.mScope = This->scope;
1015 addr.mSelector = kAudioDevicePropertyStreams;
1017 sc = AudioObjectGetPropertyDataSize(This->adevid, &addr, 0, NULL,
1020 WARN("Unable to get size for _Streams property: %lx\n", sc);
1024 ids = HeapAlloc(GetProcessHeap(), 0, size);
1026 return E_OUTOFMEMORY;
1028 sc = AudioObjectGetPropertyData(This->adevid, &addr, 0, NULL, &size, ids);
1030 WARN("Unable to get _Streams property: %lx\n", sc);
1031 HeapFree(GetProcessHeap(), 0, ids);
1035 nstreams = size / sizeof(AudioStreamID);
1038 addr.mSelector = kAudioStreamPropertyLatency;
1039 for(i = 0; i < nstreams; ++i){
1042 size = sizeof(latency);
1043 sc = AudioObjectGetPropertyData(ids[i], &addr, 0, NULL,
1046 WARN("Unable to get _Latency property: %lx\n", sc);
1054 HeapFree(GetProcessHeap(), 0, ids);
1059 static HRESULT WINAPI AudioClient_GetStreamLatency(IAudioClient *iface,
1060 REFERENCE_TIME *out)
1062 ACImpl *This = impl_from_IAudioClient(iface);
1063 UInt32 latency, stream_latency, size;
1064 AudioObjectPropertyAddress addr;
1068 TRACE("(%p)->(%p)\n", This, out);
1073 OSSpinLockLock(&This->lock);
1076 OSSpinLockUnlock(&This->lock);
1077 return AUDCLNT_E_NOT_INITIALIZED;
1080 addr.mScope = This->scope;
1081 addr.mSelector = kAudioDevicePropertyLatency;
1084 size = sizeof(latency);
1085 sc = AudioObjectGetPropertyData(This->adevid, &addr, 0, NULL,
1088 WARN("Couldn't get _Latency property: %lx\n", sc);
1089 OSSpinLockUnlock(&This->lock);
1093 hr = ca_get_max_stream_latency(This, &stream_latency);
1095 OSSpinLockUnlock(&This->lock);
1099 latency += stream_latency;
1100 *out = (latency / (double)This->fmt->nSamplesPerSec) * 10000000;
1102 OSSpinLockUnlock(&This->lock);
1107 static HRESULT AudioClient_GetCurrentPadding_nolock(ACImpl *This,
1111 return AUDCLNT_E_NOT_INITIALIZED;
1113 *numpad = This->inbuf_frames;
1118 static HRESULT WINAPI AudioClient_GetCurrentPadding(IAudioClient *iface,
1121 ACImpl *This = impl_from_IAudioClient(iface);
1124 TRACE("(%p)->(%p)\n", This, numpad);
1129 OSSpinLockLock(&This->lock);
1131 hr = AudioClient_GetCurrentPadding_nolock(This, numpad);
1133 OSSpinLockUnlock(&This->lock);
1138 static HRESULT WINAPI AudioClient_IsFormatSupported(IAudioClient *iface,
1139 AUDCLNT_SHAREMODE mode, const WAVEFORMATEX *pwfx,
1140 WAVEFORMATEX **outpwfx)
1142 ACImpl *This = impl_from_IAudioClient(iface);
1143 WAVEFORMATEXTENSIBLE *fmtex = (WAVEFORMATEXTENSIBLE*)pwfx;
1144 AudioQueueRef aqueue;
1147 TRACE("(%p)->(%x, %p, %p)\n", This, mode, pwfx, outpwfx);
1149 if(!pwfx || (mode == AUDCLNT_SHAREMODE_SHARED && !outpwfx))
1152 if(mode != AUDCLNT_SHAREMODE_SHARED && mode != AUDCLNT_SHAREMODE_EXCLUSIVE)
1153 return E_INVALIDARG;
1155 if(pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
1156 pwfx->cbSize < sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX))
1157 return E_INVALIDARG;
1164 if(pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
1165 fmtex->dwChannelMask != 0 &&
1166 fmtex->dwChannelMask != get_channel_mask(pwfx->nChannels))
1167 return AUDCLNT_E_UNSUPPORTED_FORMAT;
1169 OSSpinLockLock(&This->lock);
1171 hr = ca_setup_aqueue(This->adevid, This->dataflow, pwfx, NULL, &aqueue);
1173 AudioQueueDispose(aqueue, 1);
1174 OSSpinLockUnlock(&This->lock);
1175 TRACE("returning %08x\n", S_OK);
1179 OSSpinLockUnlock(&This->lock);
1181 TRACE("returning %08x\n", AUDCLNT_E_UNSUPPORTED_FORMAT);
1182 return AUDCLNT_E_UNSUPPORTED_FORMAT;
1185 static HRESULT WINAPI AudioClient_GetMixFormat(IAudioClient *iface,
1186 WAVEFORMATEX **pwfx)
1188 ACImpl *This = impl_from_IAudioClient(iface);
1189 WAVEFORMATEXTENSIBLE *fmt;
1193 AudioBufferList *buffers;
1194 AudioObjectPropertyAddress addr;
1197 TRACE("(%p)->(%p)\n", This, pwfx);
1203 fmt = CoTaskMemAlloc(sizeof(WAVEFORMATEXTENSIBLE));
1205 return E_OUTOFMEMORY;
1207 fmt->Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
1209 addr.mScope = This->scope;
1211 addr.mSelector = kAudioDevicePropertyStreamConfiguration;
1213 sc = AudioObjectGetPropertyDataSize(This->adevid, &addr, 0, NULL, &size);
1216 WARN("Unable to get size for _StreamConfiguration property: %lx\n", sc);
1220 buffers = HeapAlloc(GetProcessHeap(), 0, size);
1223 return E_OUTOFMEMORY;
1226 sc = AudioObjectGetPropertyData(This->adevid, &addr, 0, NULL,
1230 HeapFree(GetProcessHeap(), 0, buffers);
1231 WARN("Unable to get _StreamConfiguration property: %lx\n", sc);
1235 fmt->Format.nChannels = 0;
1236 for(i = 0; i < buffers->mNumberBuffers; ++i)
1237 fmt->Format.nChannels += buffers->mBuffers[i].mNumberChannels;
1239 HeapFree(GetProcessHeap(), 0, buffers);
1241 fmt->dwChannelMask = get_channel_mask(fmt->Format.nChannels);
1243 addr.mSelector = kAudioDevicePropertyNominalSampleRate;
1244 size = sizeof(Float64);
1245 sc = AudioObjectGetPropertyData(This->adevid, &addr, 0, NULL, &size, &rate);
1248 WARN("Unable to get _NominalSampleRate property: %lx\n", sc);
1251 fmt->Format.nSamplesPerSec = rate;
1253 fmt->Format.wBitsPerSample = 32;
1254 fmt->SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
1256 fmt->Format.nBlockAlign = (fmt->Format.wBitsPerSample *
1257 fmt->Format.nChannels) / 8;
1258 fmt->Format.nAvgBytesPerSec = fmt->Format.nSamplesPerSec *
1259 fmt->Format.nBlockAlign;
1261 fmt->Samples.wValidBitsPerSample = fmt->Format.wBitsPerSample;
1262 fmt->Format.cbSize = sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
1264 *pwfx = (WAVEFORMATEX*)fmt;
1270 static HRESULT WINAPI AudioClient_GetDevicePeriod(IAudioClient *iface,
1271 REFERENCE_TIME *defperiod, REFERENCE_TIME *minperiod)
1273 ACImpl *This = impl_from_IAudioClient(iface);
1275 TRACE("(%p)->(%p, %p)\n", This, defperiod, minperiod);
1277 if(!defperiod && !minperiod)
1280 OSSpinLockLock(&This->lock);
1282 if(This->period_ms){
1284 *defperiod = This->period_ms * 10000;
1286 *minperiod = This->period_ms * 10000;
1289 *defperiod = DefaultPeriod;
1291 *minperiod = MinimumPeriod;
1294 OSSpinLockUnlock(&This->lock);
1299 void CALLBACK ca_period_cb(void *user, BOOLEAN timer)
1301 ACImpl *This = user;
1303 OSSpinLockLock(&This->lock);
1305 SetEvent(This->event);
1306 OSSpinLockUnlock(&This->lock);
1309 static HRESULT WINAPI AudioClient_Start(IAudioClient *iface)
1311 ACImpl *This = impl_from_IAudioClient(iface);
1314 TRACE("(%p)\n", This);
1316 OSSpinLockLock(&This->lock);
1319 OSSpinLockUnlock(&This->lock);
1320 return AUDCLNT_E_NOT_INITIALIZED;
1323 if(This->playing != StateStopped){
1324 OSSpinLockUnlock(&This->lock);
1325 return AUDCLNT_E_NOT_STOPPED;
1328 if((This->flags & AUDCLNT_STREAMFLAGS_EVENTCALLBACK) && !This->event){
1329 OSSpinLockUnlock(&This->lock);
1330 return AUDCLNT_E_EVENTHANDLE_NOT_SET;
1334 if(!CreateTimerQueueTimer(&This->timer, g_timer_q,
1335 ca_period_cb, This, 0, This->period_ms, 0))
1336 ERR("Unable to create timer: %u\n", GetLastError());
1338 This->playing = StateInTransition;
1340 OSSpinLockUnlock(&This->lock);
1342 sc = AudioQueueStart(This->aqueue, NULL);
1344 WARN("Unable to start audio queue: %lx\n", sc);
1348 OSSpinLockLock(&This->lock);
1350 This->playing = StatePlaying;
1352 OSSpinLockUnlock(&This->lock);
1357 static HRESULT WINAPI AudioClient_Stop(IAudioClient *iface)
1359 ACImpl *This = impl_from_IAudioClient(iface);
1362 TRACE("(%p)\n", This);
1364 OSSpinLockLock(&This->lock);
1367 OSSpinLockUnlock(&This->lock);
1368 return AUDCLNT_E_NOT_INITIALIZED;
1371 if(This->playing == StateStopped){
1372 OSSpinLockUnlock(&This->lock);
1376 if(This->playing == StateInTransition){
1377 OSSpinLockUnlock(&This->lock);
1381 if(This->timer && This->timer != INVALID_HANDLE_VALUE){
1382 DeleteTimerQueueTimer(g_timer_q, This->timer, INVALID_HANDLE_VALUE);
1386 This->playing = StateInTransition;
1388 OSSpinLockUnlock(&This->lock);
1390 sc = AudioQueueFlush(This->aqueue);
1392 WARN("Unable to flush audio queue: %lx\n", sc);
1394 sc = AudioQueuePause(This->aqueue);
1396 WARN("Unable to pause audio queue: %lx\n", sc);
1400 OSSpinLockLock(&This->lock);
1402 This->playing = StateStopped;
1404 OSSpinLockUnlock(&This->lock);
1409 static HRESULT WINAPI AudioClient_Reset(IAudioClient *iface)
1411 ACImpl *This = impl_from_IAudioClient(iface);
1415 TRACE("(%p)\n", This);
1417 OSSpinLockLock(&This->lock);
1420 OSSpinLockUnlock(&This->lock);
1421 return AUDCLNT_E_NOT_INITIALIZED;
1424 if(This->playing != StateStopped){
1425 OSSpinLockUnlock(&This->lock);
1426 return AUDCLNT_E_NOT_STOPPED;
1429 if(This->getbuf_last){
1430 OSSpinLockUnlock(&This->lock);
1431 return AUDCLNT_E_BUFFER_OPERATION_PENDING;
1434 This->written_frames = 0;
1436 hr = AudioClock_GetPosition_nolock(This, &This->last_time, NULL, TRUE);
1438 OSSpinLockUnlock(&This->lock);
1442 OSSpinLockUnlock(&This->lock);
1444 sc = AudioQueueReset(This->aqueue);
1446 WARN("Unable to reset audio queue: %lx\n", sc);
1453 static HRESULT WINAPI AudioClient_SetEventHandle(IAudioClient *iface,
1456 ACImpl *This = impl_from_IAudioClient(iface);
1458 TRACE("(%p)->(%p)\n", This, event);
1461 return E_INVALIDARG;
1463 OSSpinLockLock(&This->lock);
1466 OSSpinLockUnlock(&This->lock);
1467 return AUDCLNT_E_NOT_INITIALIZED;
1470 if(!(This->flags & AUDCLNT_STREAMFLAGS_EVENTCALLBACK)){
1471 OSSpinLockUnlock(&This->lock);
1472 return AUDCLNT_E_EVENTHANDLE_NOT_EXPECTED;
1475 This->event = event;
1477 OSSpinLockUnlock(&This->lock);
1482 static HRESULT WINAPI AudioClient_GetService(IAudioClient *iface, REFIID riid,
1485 ACImpl *This = impl_from_IAudioClient(iface);
1487 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
1493 OSSpinLockLock(&This->lock);
1496 OSSpinLockUnlock(&This->lock);
1497 return AUDCLNT_E_NOT_INITIALIZED;
1500 if(IsEqualIID(riid, &IID_IAudioRenderClient)){
1501 if(This->dataflow != eRender){
1502 OSSpinLockUnlock(&This->lock);
1503 return AUDCLNT_E_WRONG_ENDPOINT_TYPE;
1505 IAudioRenderClient_AddRef(&This->IAudioRenderClient_iface);
1506 *ppv = &This->IAudioRenderClient_iface;
1507 }else if(IsEqualIID(riid, &IID_IAudioCaptureClient)){
1508 if(This->dataflow != eCapture){
1509 OSSpinLockUnlock(&This->lock);
1510 return AUDCLNT_E_WRONG_ENDPOINT_TYPE;
1512 IAudioCaptureClient_AddRef(&This->IAudioCaptureClient_iface);
1513 *ppv = &This->IAudioCaptureClient_iface;
1514 }else if(IsEqualIID(riid, &IID_IAudioClock)){
1515 IAudioClock_AddRef(&This->IAudioClock_iface);
1516 *ppv = &This->IAudioClock_iface;
1517 }else if(IsEqualIID(riid, &IID_IAudioStreamVolume)){
1518 IAudioStreamVolume_AddRef(&This->IAudioStreamVolume_iface);
1519 *ppv = &This->IAudioStreamVolume_iface;
1520 }else if(IsEqualIID(riid, &IID_IAudioSessionControl)){
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 IAudioSessionControl2_AddRef(&This->session_wrapper->IAudioSessionControl2_iface);
1530 *ppv = &This->session_wrapper->IAudioSessionControl2_iface;
1531 }else if(IsEqualIID(riid, &IID_IChannelAudioVolume)){
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 IChannelAudioVolume_AddRef(&This->session_wrapper->IChannelAudioVolume_iface);
1541 *ppv = &This->session_wrapper->IChannelAudioVolume_iface;
1542 }else if(IsEqualIID(riid, &IID_ISimpleAudioVolume)){
1543 if(!This->session_wrapper){
1544 This->session_wrapper = AudioSessionWrapper_Create(This);
1545 if(!This->session_wrapper){
1546 OSSpinLockUnlock(&This->lock);
1547 return E_OUTOFMEMORY;
1550 ISimpleAudioVolume_AddRef(&This->session_wrapper->ISimpleAudioVolume_iface);
1552 *ppv = &This->session_wrapper->ISimpleAudioVolume_iface;
1556 OSSpinLockUnlock(&This->lock);
1560 OSSpinLockUnlock(&This->lock);
1562 FIXME("stub %s\n", debugstr_guid(riid));
1563 return E_NOINTERFACE;
1566 static const IAudioClientVtbl AudioClient_Vtbl =
1568 AudioClient_QueryInterface,
1570 AudioClient_Release,
1571 AudioClient_Initialize,
1572 AudioClient_GetBufferSize,
1573 AudioClient_GetStreamLatency,
1574 AudioClient_GetCurrentPadding,
1575 AudioClient_IsFormatSupported,
1576 AudioClient_GetMixFormat,
1577 AudioClient_GetDevicePeriod,
1581 AudioClient_SetEventHandle,
1582 AudioClient_GetService
1585 static HRESULT WINAPI AudioRenderClient_QueryInterface(
1586 IAudioRenderClient *iface, REFIID riid, void **ppv)
1588 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
1594 if(IsEqualIID(riid, &IID_IUnknown) ||
1595 IsEqualIID(riid, &IID_IAudioRenderClient))
1598 IUnknown_AddRef((IUnknown*)*ppv);
1602 WARN("Unknown interface %s\n", debugstr_guid(riid));
1603 return E_NOINTERFACE;
1606 static ULONG WINAPI AudioRenderClient_AddRef(IAudioRenderClient *iface)
1608 ACImpl *This = impl_from_IAudioRenderClient(iface);
1609 return AudioClient_AddRef(&This->IAudioClient_iface);
1612 static ULONG WINAPI AudioRenderClient_Release(IAudioRenderClient *iface)
1614 ACImpl *This = impl_from_IAudioRenderClient(iface);
1615 return AudioClient_Release(&This->IAudioClient_iface);
1618 static HRESULT WINAPI AudioRenderClient_GetBuffer(IAudioRenderClient *iface,
1619 UINT32 frames, BYTE **data)
1621 ACImpl *This = impl_from_IAudioRenderClient(iface);
1623 UINT32 pad, bytes = frames * This->fmt->nBlockAlign;
1627 TRACE("(%p)->(%u, %p)\n", This, frames, data);
1633 OSSpinLockLock(&This->lock);
1635 if(This->getbuf_last){
1636 OSSpinLockUnlock(&This->lock);
1637 return AUDCLNT_E_OUT_OF_ORDER;
1641 OSSpinLockUnlock(&This->lock);
1645 hr = AudioClient_GetCurrentPadding_nolock(This, &pad);
1647 OSSpinLockUnlock(&This->lock);
1651 if(pad + frames > This->bufsize_frames){
1652 OSSpinLockUnlock(&This->lock);
1653 return AUDCLNT_E_BUFFER_TOO_LARGE;
1656 LIST_FOR_EACH_ENTRY(buf, &This->avail_buffers, AQBuffer, entry){
1657 if(buf->buf->mAudioDataBytesCapacity >= bytes){
1658 This->public_buffer = buf->buf;
1659 list_remove(&buf->entry);
1664 if(&buf->entry == &This->avail_buffers){
1665 sc = AudioQueueAllocateBuffer(This->aqueue, bytes,
1666 &This->public_buffer);
1668 OSSpinLockUnlock(&This->lock);
1669 WARN("Unable to allocate buffer: %lx\n", sc);
1672 buf = HeapAlloc(GetProcessHeap(), 0, sizeof(AQBuffer));
1674 AudioQueueFreeBuffer(This->aqueue, This->public_buffer);
1675 This->public_buffer = NULL;
1676 OSSpinLockUnlock(&This->lock);
1677 return E_OUTOFMEMORY;
1679 buf->buf = This->public_buffer;
1680 This->public_buffer->mUserData = buf;
1683 *data = This->public_buffer->mAudioData;
1685 This->getbuf_last = frames;
1687 OSSpinLockUnlock(&This->lock);
1692 static HRESULT WINAPI AudioRenderClient_ReleaseBuffer(
1693 IAudioRenderClient *iface, UINT32 frames, DWORD flags)
1695 ACImpl *This = impl_from_IAudioRenderClient(iface);
1698 TRACE("(%p)->(%u, %x)\n", This, frames, flags);
1700 OSSpinLockLock(&This->lock);
1703 This->getbuf_last = 0;
1704 if(This->public_buffer){
1705 AQBuffer *buf = This->public_buffer->mUserData;
1706 list_add_tail(&This->avail_buffers, &buf->entry);
1707 This->public_buffer = NULL;
1709 OSSpinLockUnlock(&This->lock);
1713 if(!This->getbuf_last){
1714 OSSpinLockUnlock(&This->lock);
1715 return AUDCLNT_E_OUT_OF_ORDER;
1718 if(frames > This->getbuf_last){
1719 OSSpinLockUnlock(&This->lock);
1720 return AUDCLNT_E_INVALID_SIZE;
1723 if(flags & AUDCLNT_BUFFERFLAGS_SILENT){
1724 WAVEFORMATEXTENSIBLE *fmtex = (WAVEFORMATEXTENSIBLE*)This->fmt;
1725 if((This->fmt->wFormatTag == WAVE_FORMAT_PCM ||
1726 (This->fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
1727 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM))) &&
1728 This->fmt->wBitsPerSample == 8)
1729 memset(This->public_buffer->mAudioData, 128,
1730 frames * This->fmt->nBlockAlign);
1732 memset(This->public_buffer->mAudioData, 0,
1733 frames * This->fmt->nBlockAlign);
1736 This->public_buffer->mAudioDataByteSize = frames * This->fmt->nBlockAlign;
1738 sc = AudioQueueEnqueueBuffer(This->aqueue, This->public_buffer, 0, NULL);
1740 OSSpinLockUnlock(&This->lock);
1741 WARN("Unable to enqueue buffer: %lx\n", sc);
1745 if(This->playing == StateStopped)
1746 AudioQueuePrime(This->aqueue, 0, NULL);
1748 This->public_buffer = NULL;
1749 This->getbuf_last = 0;
1750 This->written_frames += frames;
1751 This->inbuf_frames += frames;
1753 OSSpinLockUnlock(&This->lock);
1758 static const IAudioRenderClientVtbl AudioRenderClient_Vtbl = {
1759 AudioRenderClient_QueryInterface,
1760 AudioRenderClient_AddRef,
1761 AudioRenderClient_Release,
1762 AudioRenderClient_GetBuffer,
1763 AudioRenderClient_ReleaseBuffer
1766 static HRESULT WINAPI AudioCaptureClient_QueryInterface(
1767 IAudioCaptureClient *iface, REFIID riid, void **ppv)
1769 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
1775 if(IsEqualIID(riid, &IID_IUnknown) ||
1776 IsEqualIID(riid, &IID_IAudioCaptureClient))
1779 IUnknown_AddRef((IUnknown*)*ppv);
1783 WARN("Unknown interface %s\n", debugstr_guid(riid));
1784 return E_NOINTERFACE;
1787 static ULONG WINAPI AudioCaptureClient_AddRef(IAudioCaptureClient *iface)
1789 ACImpl *This = impl_from_IAudioCaptureClient(iface);
1790 return IAudioClient_AddRef(&This->IAudioClient_iface);
1793 static ULONG WINAPI AudioCaptureClient_Release(IAudioCaptureClient *iface)
1795 ACImpl *This = impl_from_IAudioCaptureClient(iface);
1796 return IAudioClient_Release(&This->IAudioClient_iface);
1799 static HRESULT WINAPI AudioCaptureClient_GetBuffer(IAudioCaptureClient *iface,
1800 BYTE **data, UINT32 *frames, DWORD *flags, UINT64 *devpos,
1803 ACImpl *This = impl_from_IAudioCaptureClient(iface);
1805 TRACE("(%p)->(%p, %p, %p, %p, %p)\n", This, data, frames, flags,
1808 if(!data || !frames || !flags)
1811 OSSpinLockLock(&This->lock);
1813 if(This->getbuf_last){
1814 OSSpinLockUnlock(&This->lock);
1815 return AUDCLNT_E_OUT_OF_ORDER;
1818 if(This->public_buffer){
1819 *data = This->public_buffer->mAudioData;
1821 This->public_buffer->mAudioDataByteSize / This->fmt->nBlockAlign;
1823 struct list *head = list_head(&This->avail_buffers);
1828 AQBuffer *buf = LIST_ENTRY(head, AQBuffer, entry);
1829 This->public_buffer = buf->buf;
1830 *data = This->public_buffer->mAudioData;
1832 This->public_buffer->mAudioDataByteSize / This->fmt->nBlockAlign;
1833 list_remove(&buf->entry);
1838 This->written_frames += *frames;
1839 This->inbuf_frames -= *frames;
1840 This->getbuf_last = 1;
1842 if(devpos || qpcpos)
1843 AudioClock_GetPosition_nolock(This, devpos, qpcpos, FALSE);
1845 OSSpinLockUnlock(&This->lock);
1847 return *frames ? S_OK : AUDCLNT_S_BUFFER_EMPTY;
1850 static HRESULT WINAPI AudioCaptureClient_ReleaseBuffer(
1851 IAudioCaptureClient *iface, UINT32 done)
1853 ACImpl *This = impl_from_IAudioCaptureClient(iface);
1857 TRACE("(%p)->(%u)\n", This, done);
1859 OSSpinLockLock(&This->lock);
1861 if(!This->getbuf_last){
1862 OSSpinLockUnlock(&This->lock);
1863 return AUDCLNT_E_OUT_OF_ORDER;
1866 pbuf_frames = This->public_buffer->mAudioDataByteSize / This->fmt->nBlockAlign;
1867 if(done != 0 && done != pbuf_frames){
1868 OSSpinLockUnlock(&This->lock);
1869 return AUDCLNT_E_INVALID_SIZE;
1873 sc = AudioQueueEnqueueBuffer(This->aqueue, This->public_buffer,
1876 WARN("Unable to enqueue buffer: %lx\n", sc);
1877 This->public_buffer = NULL;
1880 This->getbuf_last = 0;
1882 OSSpinLockUnlock(&This->lock);
1887 static HRESULT WINAPI AudioCaptureClient_GetNextPacketSize(
1888 IAudioCaptureClient *iface, UINT32 *frames)
1890 ACImpl *This = impl_from_IAudioCaptureClient(iface);
1894 TRACE("(%p)->(%p)\n", This, frames);
1899 OSSpinLockLock(&This->lock);
1901 head = list_head(&This->avail_buffers);
1904 *frames = This->bufsize_frames / CAPTURE_BUFFERS;
1905 OSSpinLockUnlock(&This->lock);
1909 buf = LIST_ENTRY(head, AQBuffer, entry);
1910 *frames = buf->buf->mAudioDataByteSize / This->fmt->nBlockAlign;
1912 OSSpinLockUnlock(&This->lock);
1917 static const IAudioCaptureClientVtbl AudioCaptureClient_Vtbl =
1919 AudioCaptureClient_QueryInterface,
1920 AudioCaptureClient_AddRef,
1921 AudioCaptureClient_Release,
1922 AudioCaptureClient_GetBuffer,
1923 AudioCaptureClient_ReleaseBuffer,
1924 AudioCaptureClient_GetNextPacketSize
1927 static HRESULT WINAPI AudioClock_QueryInterface(IAudioClock *iface,
1928 REFIID riid, void **ppv)
1930 ACImpl *This = impl_from_IAudioClock(iface);
1932 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
1938 if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IAudioClock))
1940 else if(IsEqualIID(riid, &IID_IAudioClock2))
1941 *ppv = &This->IAudioClock2_iface;
1943 IUnknown_AddRef((IUnknown*)*ppv);
1947 WARN("Unknown interface %s\n", debugstr_guid(riid));
1948 return E_NOINTERFACE;
1951 static ULONG WINAPI AudioClock_AddRef(IAudioClock *iface)
1953 ACImpl *This = impl_from_IAudioClock(iface);
1954 return IAudioClient_AddRef(&This->IAudioClient_iface);
1957 static ULONG WINAPI AudioClock_Release(IAudioClock *iface)
1959 ACImpl *This = impl_from_IAudioClock(iface);
1960 return IAudioClient_Release(&This->IAudioClient_iface);
1963 static HRESULT WINAPI AudioClock_GetFrequency(IAudioClock *iface, UINT64 *freq)
1965 ACImpl *This = impl_from_IAudioClock(iface);
1967 TRACE("(%p)->(%p)\n", This, freq);
1969 *freq = This->fmt->nSamplesPerSec;
1974 static HRESULT AudioClock_GetPosition_nolock(ACImpl *This,
1975 UINT64 *pos, UINT64 *qpctime, BOOL raw)
1977 AudioTimeStamp time;
1980 sc = AudioQueueGetCurrentTime(This->aqueue, NULL, &time, NULL);
1981 if(sc == kAudioQueueErr_InvalidRunState){
1983 }else if(sc == noErr){
1984 if(!(time.mFlags & kAudioTimeStampSampleTimeValid)){
1985 FIXME("Sample time not valid, should calculate from something else\n");
1990 *pos = time.mSampleTime;
1992 *pos = time.mSampleTime - This->last_time;
1994 WARN("Unable to get current time: %lx\n", sc);
1999 LARGE_INTEGER stamp, freq;
2000 QueryPerformanceCounter(&stamp);
2001 QueryPerformanceFrequency(&freq);
2002 *qpctime = (stamp.QuadPart * (INT64)10000000) / freq.QuadPart;
2008 static HRESULT WINAPI AudioClock_GetPosition(IAudioClock *iface, UINT64 *pos,
2011 ACImpl *This = impl_from_IAudioClock(iface);
2014 TRACE("(%p)->(%p, %p)\n", This, pos, qpctime);
2019 OSSpinLockLock(&This->lock);
2021 hr = AudioClock_GetPosition_nolock(This, pos, qpctime, FALSE);
2023 OSSpinLockUnlock(&This->lock);
2028 static HRESULT WINAPI AudioClock_GetCharacteristics(IAudioClock *iface,
2031 ACImpl *This = impl_from_IAudioClock(iface);
2033 TRACE("(%p)->(%p)\n", This, chars);
2038 *chars = AUDIOCLOCK_CHARACTERISTIC_FIXED_FREQ;
2043 static const IAudioClockVtbl AudioClock_Vtbl =
2045 AudioClock_QueryInterface,
2048 AudioClock_GetFrequency,
2049 AudioClock_GetPosition,
2050 AudioClock_GetCharacteristics
2053 static HRESULT WINAPI AudioClock2_QueryInterface(IAudioClock2 *iface,
2054 REFIID riid, void **ppv)
2056 ACImpl *This = impl_from_IAudioClock2(iface);
2057 return IAudioClock_QueryInterface(&This->IAudioClock_iface, riid, ppv);
2060 static ULONG WINAPI AudioClock2_AddRef(IAudioClock2 *iface)
2062 ACImpl *This = impl_from_IAudioClock2(iface);
2063 return IAudioClient_AddRef(&This->IAudioClient_iface);
2066 static ULONG WINAPI AudioClock2_Release(IAudioClock2 *iface)
2068 ACImpl *This = impl_from_IAudioClock2(iface);
2069 return IAudioClient_Release(&This->IAudioClient_iface);
2072 static HRESULT WINAPI AudioClock2_GetDevicePosition(IAudioClock2 *iface,
2073 UINT64 *pos, UINT64 *qpctime)
2075 ACImpl *This = impl_from_IAudioClock2(iface);
2077 FIXME("(%p)->(%p, %p)\n", This, pos, qpctime);
2082 static const IAudioClock2Vtbl AudioClock2_Vtbl =
2084 AudioClock2_QueryInterface,
2086 AudioClock2_Release,
2087 AudioClock2_GetDevicePosition
2090 static AudioSessionWrapper *AudioSessionWrapper_Create(ACImpl *client)
2092 AudioSessionWrapper *ret;
2094 ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
2095 sizeof(AudioSessionWrapper));
2099 ret->IAudioSessionControl2_iface.lpVtbl = &AudioSessionControl2_Vtbl;
2100 ret->ISimpleAudioVolume_iface.lpVtbl = &SimpleAudioVolume_Vtbl;
2101 ret->IChannelAudioVolume_iface.lpVtbl = &ChannelAudioVolume_Vtbl;
2105 ret->client = client;
2107 ret->session = client->session;
2108 AudioClient_AddRef(&client->IAudioClient_iface);
2114 static HRESULT WINAPI AudioSessionControl_QueryInterface(
2115 IAudioSessionControl2 *iface, REFIID riid, void **ppv)
2117 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2123 if(IsEqualIID(riid, &IID_IUnknown) ||
2124 IsEqualIID(riid, &IID_IAudioSessionControl) ||
2125 IsEqualIID(riid, &IID_IAudioSessionControl2))
2128 IUnknown_AddRef((IUnknown*)*ppv);
2132 WARN("Unknown interface %s\n", debugstr_guid(riid));
2133 return E_NOINTERFACE;
2136 static ULONG WINAPI AudioSessionControl_AddRef(IAudioSessionControl2 *iface)
2138 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2140 ref = InterlockedIncrement(&This->ref);
2141 TRACE("(%p) Refcount now %u\n", This, ref);
2145 static ULONG WINAPI AudioSessionControl_Release(IAudioSessionControl2 *iface)
2147 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2149 ref = InterlockedDecrement(&This->ref);
2150 TRACE("(%p) Refcount now %u\n", This, ref);
2153 OSSpinLockLock(&This->client->lock);
2154 This->client->session_wrapper = NULL;
2155 OSSpinLockUnlock(&This->client->lock);
2156 AudioClient_Release(&This->client->IAudioClient_iface);
2158 HeapFree(GetProcessHeap(), 0, This);
2163 static HRESULT WINAPI AudioSessionControl_GetState(IAudioSessionControl2 *iface,
2164 AudioSessionState *state)
2166 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2169 TRACE("(%p)->(%p)\n", This, state);
2172 return NULL_PTR_ERR;
2174 EnterCriticalSection(&g_sessions_lock);
2176 if(list_empty(&This->session->clients)){
2177 *state = AudioSessionStateExpired;
2178 LeaveCriticalSection(&g_sessions_lock);
2182 LIST_FOR_EACH_ENTRY(client, &This->session->clients, ACImpl, entry){
2183 OSSpinLockLock(&client->lock);
2184 if(client->playing == StatePlaying ||
2185 client->playing == StateInTransition){
2186 *state = AudioSessionStateActive;
2187 OSSpinLockUnlock(&client->lock);
2188 LeaveCriticalSection(&g_sessions_lock);
2191 OSSpinLockUnlock(&client->lock);
2194 LeaveCriticalSection(&g_sessions_lock);
2196 *state = AudioSessionStateInactive;
2201 static HRESULT WINAPI AudioSessionControl_GetDisplayName(
2202 IAudioSessionControl2 *iface, WCHAR **name)
2204 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2206 FIXME("(%p)->(%p) - stub\n", This, name);
2211 static HRESULT WINAPI AudioSessionControl_SetDisplayName(
2212 IAudioSessionControl2 *iface, const WCHAR *name, const GUID *session)
2214 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2216 FIXME("(%p)->(%p, %s) - stub\n", This, name, debugstr_guid(session));
2221 static HRESULT WINAPI AudioSessionControl_GetIconPath(
2222 IAudioSessionControl2 *iface, WCHAR **path)
2224 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2226 FIXME("(%p)->(%p) - stub\n", This, path);
2231 static HRESULT WINAPI AudioSessionControl_SetIconPath(
2232 IAudioSessionControl2 *iface, const WCHAR *path, const GUID *session)
2234 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2236 FIXME("(%p)->(%p, %s) - stub\n", This, path, debugstr_guid(session));
2241 static HRESULT WINAPI AudioSessionControl_GetGroupingParam(
2242 IAudioSessionControl2 *iface, GUID *group)
2244 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2246 FIXME("(%p)->(%p) - stub\n", This, group);
2251 static HRESULT WINAPI AudioSessionControl_SetGroupingParam(
2252 IAudioSessionControl2 *iface, const GUID *group, const GUID *session)
2254 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2256 FIXME("(%p)->(%s, %s) - stub\n", This, debugstr_guid(group),
2257 debugstr_guid(session));
2262 static HRESULT WINAPI AudioSessionControl_RegisterAudioSessionNotification(
2263 IAudioSessionControl2 *iface, IAudioSessionEvents *events)
2265 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2267 FIXME("(%p)->(%p) - stub\n", This, events);
2272 static HRESULT WINAPI AudioSessionControl_UnregisterAudioSessionNotification(
2273 IAudioSessionControl2 *iface, IAudioSessionEvents *events)
2275 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2277 FIXME("(%p)->(%p) - stub\n", This, events);
2282 static HRESULT WINAPI AudioSessionControl_GetSessionIdentifier(
2283 IAudioSessionControl2 *iface, WCHAR **id)
2285 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2287 FIXME("(%p)->(%p) - stub\n", This, id);
2292 static HRESULT WINAPI AudioSessionControl_GetSessionInstanceIdentifier(
2293 IAudioSessionControl2 *iface, WCHAR **id)
2295 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2297 FIXME("(%p)->(%p) - stub\n", This, id);
2302 static HRESULT WINAPI AudioSessionControl_GetProcessId(
2303 IAudioSessionControl2 *iface, DWORD *pid)
2305 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2307 TRACE("(%p)->(%p)\n", This, pid);
2312 *pid = GetCurrentProcessId();
2317 static HRESULT WINAPI AudioSessionControl_IsSystemSoundsSession(
2318 IAudioSessionControl2 *iface)
2320 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2322 TRACE("(%p)\n", This);
2327 static HRESULT WINAPI AudioSessionControl_SetDuckingPreference(
2328 IAudioSessionControl2 *iface, BOOL optout)
2330 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2332 TRACE("(%p)->(%d)\n", This, optout);
2337 static const IAudioSessionControl2Vtbl AudioSessionControl2_Vtbl =
2339 AudioSessionControl_QueryInterface,
2340 AudioSessionControl_AddRef,
2341 AudioSessionControl_Release,
2342 AudioSessionControl_GetState,
2343 AudioSessionControl_GetDisplayName,
2344 AudioSessionControl_SetDisplayName,
2345 AudioSessionControl_GetIconPath,
2346 AudioSessionControl_SetIconPath,
2347 AudioSessionControl_GetGroupingParam,
2348 AudioSessionControl_SetGroupingParam,
2349 AudioSessionControl_RegisterAudioSessionNotification,
2350 AudioSessionControl_UnregisterAudioSessionNotification,
2351 AudioSessionControl_GetSessionIdentifier,
2352 AudioSessionControl_GetSessionInstanceIdentifier,
2353 AudioSessionControl_GetProcessId,
2354 AudioSessionControl_IsSystemSoundsSession,
2355 AudioSessionControl_SetDuckingPreference
2358 /* index == -1 means set all channels, otherwise sets only the given channel */
2359 static HRESULT ca_setvol(ACImpl *This, UINT32 index)
2364 if(index == (UINT32)-1){
2367 for(i = 0; i < This->fmt->nChannels; ++i){
2369 hr = ca_setvol(This, i);
2376 if(This->session->mute)
2379 level = This->session->master_vol *
2380 This->session->channel_vols[index] * This->vols[index];
2382 sc = AudioQueueSetParameter(This->aqueue, kAudioQueueParam_Volume, level);
2384 WARN("Setting _Volume property failed: %lx\n", sc);
2389 static HRESULT ca_session_setvol(AudioSession *session, UINT32 index)
2394 LIST_FOR_EACH_ENTRY(client, &session->clients, ACImpl, entry){
2396 hr = ca_setvol(client, index);
2404 static HRESULT WINAPI SimpleAudioVolume_QueryInterface(
2405 ISimpleAudioVolume *iface, REFIID riid, void **ppv)
2407 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2413 if(IsEqualIID(riid, &IID_IUnknown) ||
2414 IsEqualIID(riid, &IID_ISimpleAudioVolume))
2417 IUnknown_AddRef((IUnknown*)*ppv);
2421 WARN("Unknown interface %s\n", debugstr_guid(riid));
2422 return E_NOINTERFACE;
2425 static ULONG WINAPI SimpleAudioVolume_AddRef(ISimpleAudioVolume *iface)
2427 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2428 return AudioSessionControl_AddRef(&This->IAudioSessionControl2_iface);
2431 static ULONG WINAPI SimpleAudioVolume_Release(ISimpleAudioVolume *iface)
2433 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2434 return AudioSessionControl_Release(&This->IAudioSessionControl2_iface);
2437 static HRESULT WINAPI SimpleAudioVolume_SetMasterVolume(
2438 ISimpleAudioVolume *iface, float level, const GUID *context)
2440 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2441 AudioSession *session = This->session;
2444 TRACE("(%p)->(%f, %s)\n", session, level, wine_dbgstr_guid(context));
2446 if(level < 0.f || level > 1.f)
2447 return E_INVALIDARG;
2450 FIXME("Notifications not supported yet\n");
2452 EnterCriticalSection(&session->lock);
2454 session->master_vol = level;
2456 ret = ca_session_setvol(session, -1);
2458 LeaveCriticalSection(&session->lock);
2463 static HRESULT WINAPI SimpleAudioVolume_GetMasterVolume(
2464 ISimpleAudioVolume *iface, float *level)
2466 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2467 AudioSession *session = This->session;
2469 TRACE("(%p)->(%p)\n", session, level);
2472 return NULL_PTR_ERR;
2474 *level = session->master_vol;
2479 static HRESULT WINAPI SimpleAudioVolume_SetMute(ISimpleAudioVolume *iface,
2480 BOOL mute, const GUID *context)
2482 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2483 AudioSession *session = This->session;
2485 TRACE("(%p)->(%u, %p)\n", session, mute, context);
2488 FIXME("Notifications not supported yet\n");
2490 EnterCriticalSection(&session->lock);
2492 session->mute = mute;
2494 ca_session_setvol(session, -1);
2496 LeaveCriticalSection(&session->lock);
2501 static HRESULT WINAPI SimpleAudioVolume_GetMute(ISimpleAudioVolume *iface,
2504 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2505 AudioSession *session = This->session;
2507 TRACE("(%p)->(%p)\n", session, mute);
2510 return NULL_PTR_ERR;
2512 *mute = session->mute;
2517 static const ISimpleAudioVolumeVtbl SimpleAudioVolume_Vtbl =
2519 SimpleAudioVolume_QueryInterface,
2520 SimpleAudioVolume_AddRef,
2521 SimpleAudioVolume_Release,
2522 SimpleAudioVolume_SetMasterVolume,
2523 SimpleAudioVolume_GetMasterVolume,
2524 SimpleAudioVolume_SetMute,
2525 SimpleAudioVolume_GetMute
2528 static HRESULT WINAPI AudioStreamVolume_QueryInterface(
2529 IAudioStreamVolume *iface, REFIID riid, void **ppv)
2531 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2537 if(IsEqualIID(riid, &IID_IUnknown) ||
2538 IsEqualIID(riid, &IID_IAudioStreamVolume))
2541 IUnknown_AddRef((IUnknown*)*ppv);
2545 WARN("Unknown interface %s\n", debugstr_guid(riid));
2546 return E_NOINTERFACE;
2549 static ULONG WINAPI AudioStreamVolume_AddRef(IAudioStreamVolume *iface)
2551 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2552 return IAudioClient_AddRef(&This->IAudioClient_iface);
2555 static ULONG WINAPI AudioStreamVolume_Release(IAudioStreamVolume *iface)
2557 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2558 return IAudioClient_Release(&This->IAudioClient_iface);
2561 static HRESULT WINAPI AudioStreamVolume_GetChannelCount(
2562 IAudioStreamVolume *iface, UINT32 *out)
2564 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2566 TRACE("(%p)->(%p)\n", This, out);
2571 *out = This->fmt->nChannels;
2576 static HRESULT WINAPI AudioStreamVolume_SetChannelVolume(
2577 IAudioStreamVolume *iface, UINT32 index, float level)
2579 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2582 TRACE("(%p)->(%d, %f)\n", This, index, level);
2584 if(level < 0.f || level > 1.f)
2585 return E_INVALIDARG;
2587 if(index >= This->fmt->nChannels)
2588 return E_INVALIDARG;
2590 OSSpinLockLock(&This->lock);
2592 This->vols[index] = level;
2594 WARN("AudioQueue doesn't support per-channel volume control\n");
2595 ret = ca_setvol(This, index);
2597 OSSpinLockUnlock(&This->lock);
2602 static HRESULT WINAPI AudioStreamVolume_GetChannelVolume(
2603 IAudioStreamVolume *iface, UINT32 index, float *level)
2605 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2607 TRACE("(%p)->(%d, %p)\n", This, index, level);
2612 if(index >= This->fmt->nChannels)
2613 return E_INVALIDARG;
2615 *level = This->vols[index];
2620 static HRESULT WINAPI AudioStreamVolume_SetAllVolumes(
2621 IAudioStreamVolume *iface, UINT32 count, const float *levels)
2623 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2627 TRACE("(%p)->(%d, %p)\n", This, count, levels);
2632 if(count != This->fmt->nChannels)
2633 return E_INVALIDARG;
2635 OSSpinLockLock(&This->lock);
2637 for(i = 0; i < count; ++i)
2638 This->vols[i] = levels[i];
2640 ret = ca_setvol(This, -1);
2642 OSSpinLockUnlock(&This->lock);
2647 static HRESULT WINAPI AudioStreamVolume_GetAllVolumes(
2648 IAudioStreamVolume *iface, UINT32 count, float *levels)
2650 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2653 TRACE("(%p)->(%d, %p)\n", This, count, levels);
2658 if(count != This->fmt->nChannels)
2659 return E_INVALIDARG;
2661 OSSpinLockLock(&This->lock);
2663 for(i = 0; i < count; ++i)
2664 levels[i] = This->vols[i];
2666 OSSpinLockUnlock(&This->lock);
2671 static const IAudioStreamVolumeVtbl AudioStreamVolume_Vtbl =
2673 AudioStreamVolume_QueryInterface,
2674 AudioStreamVolume_AddRef,
2675 AudioStreamVolume_Release,
2676 AudioStreamVolume_GetChannelCount,
2677 AudioStreamVolume_SetChannelVolume,
2678 AudioStreamVolume_GetChannelVolume,
2679 AudioStreamVolume_SetAllVolumes,
2680 AudioStreamVolume_GetAllVolumes
2683 static HRESULT WINAPI ChannelAudioVolume_QueryInterface(
2684 IChannelAudioVolume *iface, REFIID riid, void **ppv)
2686 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2692 if(IsEqualIID(riid, &IID_IUnknown) ||
2693 IsEqualIID(riid, &IID_IChannelAudioVolume))
2696 IUnknown_AddRef((IUnknown*)*ppv);
2700 WARN("Unknown interface %s\n", debugstr_guid(riid));
2701 return E_NOINTERFACE;
2704 static ULONG WINAPI ChannelAudioVolume_AddRef(IChannelAudioVolume *iface)
2706 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2707 return AudioSessionControl_AddRef(&This->IAudioSessionControl2_iface);
2710 static ULONG WINAPI ChannelAudioVolume_Release(IChannelAudioVolume *iface)
2712 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2713 return AudioSessionControl_Release(&This->IAudioSessionControl2_iface);
2716 static HRESULT WINAPI ChannelAudioVolume_GetChannelCount(
2717 IChannelAudioVolume *iface, UINT32 *out)
2719 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2720 AudioSession *session = This->session;
2722 TRACE("(%p)->(%p)\n", session, out);
2725 return NULL_PTR_ERR;
2727 *out = session->channel_count;
2732 static HRESULT WINAPI ChannelAudioVolume_SetChannelVolume(
2733 IChannelAudioVolume *iface, UINT32 index, float level,
2734 const GUID *context)
2736 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2737 AudioSession *session = This->session;
2740 TRACE("(%p)->(%d, %f, %s)\n", session, index, level,
2741 wine_dbgstr_guid(context));
2743 if(level < 0.f || level > 1.f)
2744 return E_INVALIDARG;
2746 if(index >= session->channel_count)
2747 return E_INVALIDARG;
2750 FIXME("Notifications not supported yet\n");
2752 EnterCriticalSection(&session->lock);
2754 session->channel_vols[index] = level;
2756 WARN("AudioQueue doesn't support per-channel volume control\n");
2757 ret = ca_session_setvol(session, index);
2759 LeaveCriticalSection(&session->lock);
2764 static HRESULT WINAPI ChannelAudioVolume_GetChannelVolume(
2765 IChannelAudioVolume *iface, UINT32 index, float *level)
2767 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2768 AudioSession *session = This->session;
2770 TRACE("(%p)->(%d, %p)\n", session, index, level);
2773 return NULL_PTR_ERR;
2775 if(index >= session->channel_count)
2776 return E_INVALIDARG;
2778 *level = session->channel_vols[index];
2783 static HRESULT WINAPI ChannelAudioVolume_SetAllVolumes(
2784 IChannelAudioVolume *iface, UINT32 count, const float *levels,
2785 const GUID *context)
2787 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2788 AudioSession *session = This->session;
2792 TRACE("(%p)->(%d, %p, %s)\n", session, count, levels,
2793 wine_dbgstr_guid(context));
2796 return NULL_PTR_ERR;
2798 if(count != session->channel_count)
2799 return E_INVALIDARG;
2802 FIXME("Notifications not supported yet\n");
2804 EnterCriticalSection(&session->lock);
2806 for(i = 0; i < count; ++i)
2807 session->channel_vols[i] = levels[i];
2809 ret = ca_session_setvol(session, -1);
2811 LeaveCriticalSection(&session->lock);
2816 static HRESULT WINAPI ChannelAudioVolume_GetAllVolumes(
2817 IChannelAudioVolume *iface, UINT32 count, float *levels)
2819 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2820 AudioSession *session = This->session;
2823 TRACE("(%p)->(%d, %p)\n", session, count, levels);
2826 return NULL_PTR_ERR;
2828 if(count != session->channel_count)
2829 return E_INVALIDARG;
2831 for(i = 0; i < count; ++i)
2832 levels[i] = session->channel_vols[i];
2837 static const IChannelAudioVolumeVtbl ChannelAudioVolume_Vtbl =
2839 ChannelAudioVolume_QueryInterface,
2840 ChannelAudioVolume_AddRef,
2841 ChannelAudioVolume_Release,
2842 ChannelAudioVolume_GetChannelCount,
2843 ChannelAudioVolume_SetChannelVolume,
2844 ChannelAudioVolume_GetChannelVolume,
2845 ChannelAudioVolume_SetAllVolumes,
2846 ChannelAudioVolume_GetAllVolumes
2849 static HRESULT WINAPI AudioSessionManager_QueryInterface(IAudioSessionManager2 *iface,
2850 REFIID riid, void **ppv)
2852 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2858 if(IsEqualIID(riid, &IID_IUnknown) ||
2859 IsEqualIID(riid, &IID_IAudioSessionManager) ||
2860 IsEqualIID(riid, &IID_IAudioSessionManager2))
2863 IUnknown_AddRef((IUnknown*)*ppv);
2867 WARN("Unknown interface %s\n", debugstr_guid(riid));
2868 return E_NOINTERFACE;
2871 static ULONG WINAPI AudioSessionManager_AddRef(IAudioSessionManager2 *iface)
2873 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
2875 ref = InterlockedIncrement(&This->ref);
2876 TRACE("(%p) Refcount now %u\n", This, ref);
2880 static ULONG WINAPI AudioSessionManager_Release(IAudioSessionManager2 *iface)
2882 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
2884 ref = InterlockedDecrement(&This->ref);
2885 TRACE("(%p) Refcount now %u\n", This, ref);
2887 HeapFree(GetProcessHeap(), 0, This);
2891 static HRESULT WINAPI AudioSessionManager_GetAudioSessionControl(
2892 IAudioSessionManager2 *iface, const GUID *session_guid, DWORD flags,
2893 IAudioSessionControl **out)
2895 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
2896 AudioSession *session;
2897 AudioSessionWrapper *wrapper;
2900 TRACE("(%p)->(%s, %x, %p)\n", This, debugstr_guid(session_guid),
2903 hr = get_audio_session(session_guid, This->device, 0, &session);
2907 wrapper = AudioSessionWrapper_Create(NULL);
2909 return E_OUTOFMEMORY;
2911 wrapper->session = session;
2913 *out = (IAudioSessionControl*)&wrapper->IAudioSessionControl2_iface;
2918 static HRESULT WINAPI AudioSessionManager_GetSimpleAudioVolume(
2919 IAudioSessionManager2 *iface, const GUID *session_guid, DWORD flags,
2920 ISimpleAudioVolume **out)
2922 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
2923 AudioSession *session;
2924 AudioSessionWrapper *wrapper;
2927 TRACE("(%p)->(%s, %x, %p)\n", This, debugstr_guid(session_guid),
2930 hr = get_audio_session(session_guid, This->device, 0, &session);
2934 wrapper = AudioSessionWrapper_Create(NULL);
2936 return E_OUTOFMEMORY;
2938 wrapper->session = session;
2940 *out = &wrapper->ISimpleAudioVolume_iface;
2945 static HRESULT WINAPI AudioSessionManager_GetSessionEnumerator(
2946 IAudioSessionManager2 *iface, IAudioSessionEnumerator **out)
2948 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
2949 FIXME("(%p)->(%p) - stub\n", This, out);
2953 static HRESULT WINAPI AudioSessionManager_RegisterSessionNotification(
2954 IAudioSessionManager2 *iface, IAudioSessionNotification *notification)
2956 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
2957 FIXME("(%p)->(%p) - stub\n", This, notification);
2961 static HRESULT WINAPI AudioSessionManager_UnregisterSessionNotification(
2962 IAudioSessionManager2 *iface, IAudioSessionNotification *notification)
2964 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
2965 FIXME("(%p)->(%p) - stub\n", This, notification);
2969 static HRESULT WINAPI AudioSessionManager_RegisterDuckNotification(
2970 IAudioSessionManager2 *iface, const WCHAR *session_id,
2971 IAudioVolumeDuckNotification *notification)
2973 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
2974 FIXME("(%p)->(%p) - stub\n", This, notification);
2978 static HRESULT WINAPI AudioSessionManager_UnregisterDuckNotification(
2979 IAudioSessionManager2 *iface,
2980 IAudioVolumeDuckNotification *notification)
2982 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
2983 FIXME("(%p)->(%p) - stub\n", This, notification);
2987 static const IAudioSessionManager2Vtbl AudioSessionManager2_Vtbl =
2989 AudioSessionManager_QueryInterface,
2990 AudioSessionManager_AddRef,
2991 AudioSessionManager_Release,
2992 AudioSessionManager_GetAudioSessionControl,
2993 AudioSessionManager_GetSimpleAudioVolume,
2994 AudioSessionManager_GetSessionEnumerator,
2995 AudioSessionManager_RegisterSessionNotification,
2996 AudioSessionManager_UnregisterSessionNotification,
2997 AudioSessionManager_RegisterDuckNotification,
2998 AudioSessionManager_UnregisterDuckNotification
3001 HRESULT WINAPI AUDDRV_GetAudioSessionManager(IMMDevice *device,
3002 IAudioSessionManager2 **out)
3006 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(SessionMgr));
3008 return E_OUTOFMEMORY;
3010 This->IAudioSessionManager2_iface.lpVtbl = &AudioSessionManager2_Vtbl;
3011 This->device = device;
3014 *out = &This->IAudioSessionManager2_iface;