2 * Copyright 2011 Andrew Eikum for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #define NONAMELESSUNION
29 #include "wine/debug.h"
30 #include "wine/unicode.h"
31 #include "wine/list.h"
34 #include "mmdeviceapi.h"
38 #include "endpointvolume.h"
41 #include "audioclient.h"
42 #include "audiopolicy.h"
49 #include <sys/types.h>
51 #include <sys/ioctl.h>
55 #include <libkern/OSAtomic.h>
56 #include <CoreAudio/CoreAudio.h>
57 #include <AudioToolbox/AudioQueue.h>
59 WINE_DEFAULT_DEBUG_CHANNEL(coreaudio);
61 #define NULL_PTR_ERR MAKE_HRESULT(SEVERITY_ERROR, FACILITY_WIN32, RPC_X_NULL_REF_POINTER)
63 #define CAPTURE_BUFFERS 5
65 static const REFERENCE_TIME DefaultPeriod = 200000;
66 static const REFERENCE_TIME MinimumPeriod = 100000;
68 typedef struct _AQBuffer {
69 AudioQueueBufferRef buf;
74 typedef struct ACImpl ACImpl;
76 typedef struct _AudioSession {
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, 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 HRESULT WINAPI AUDDRV_GetEndpointIDs(EDataFlow flow, WCHAR ***ids,
246 AudioDeviceID ***keys, UINT *num, UINT *def_index)
248 UInt32 devsize, size;
249 AudioDeviceID *devices;
250 AudioDeviceID default_id;
251 AudioObjectPropertyAddress addr;
255 TRACE("%d %p %p %p\n", flow, ids, num, def_index);
257 addr.mScope = kAudioObjectPropertyScopeGlobal;
258 addr.mElement = kAudioObjectPropertyElementMaster;
260 addr.mSelector = kAudioHardwarePropertyDefaultOutputDevice;
261 else if(flow == eCapture)
262 addr.mSelector = kAudioHardwarePropertyDefaultInputDevice;
266 size = sizeof(default_id);
267 sc = AudioObjectGetPropertyData(kAudioObjectSystemObject, &addr, 0,
268 NULL, &size, &default_id);
270 WARN("Getting _DefaultInputDevice property failed: %lx\n", sc);
274 addr.mSelector = kAudioHardwarePropertyDevices;
275 sc = AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &addr, 0,
278 WARN("Getting _Devices property size failed: %lx\n", sc);
282 devices = HeapAlloc(GetProcessHeap(), 0, devsize);
284 return E_OUTOFMEMORY;
286 sc = AudioObjectGetPropertyData(kAudioObjectSystemObject, &addr, 0, NULL,
289 WARN("Getting _Devices property failed: %lx\n", sc);
290 HeapFree(GetProcessHeap(), 0, devices);
294 ndevices = devsize / sizeof(AudioDeviceID);
296 *ids = HeapAlloc(GetProcessHeap(), 0, ndevices * sizeof(WCHAR *));
298 HeapFree(GetProcessHeap(), 0, devices);
299 return E_OUTOFMEMORY;
302 *keys = HeapAlloc(GetProcessHeap(), 0, ndevices * sizeof(AudioDeviceID *));
304 HeapFree(GetProcessHeap(), 0, *ids);
305 HeapFree(GetProcessHeap(), 0, devices);
306 return E_OUTOFMEMORY;
310 *def_index = (UINT)-1;
311 for(i = 0; i < ndevices; ++i){
312 AudioBufferList *buffers;
318 addr.mSelector = kAudioDevicePropertyStreamConfiguration;
320 addr.mScope = kAudioDevicePropertyScopeOutput;
322 addr.mScope = kAudioDevicePropertyScopeInput;
324 sc = AudioObjectGetPropertyDataSize(devices[i], &addr, 0, NULL, &size);
326 WARN("Unable to get _StreamConfiguration property size for "
327 "device %lu: %lx\n", devices[i], sc);
331 buffers = HeapAlloc(GetProcessHeap(), 0, size);
333 HeapFree(GetProcessHeap(), 0, devices);
334 for(j = 0; j < *num; ++j)
335 HeapFree(GetProcessHeap(), 0, (*ids)[j]);
336 HeapFree(GetProcessHeap(), 0, *keys);
337 HeapFree(GetProcessHeap(), 0, *ids);
338 return E_OUTOFMEMORY;
341 sc = AudioObjectGetPropertyData(devices[i], &addr, 0, NULL,
344 WARN("Unable to get _StreamConfiguration property for "
345 "device %lu: %lx\n", devices[i], sc);
346 HeapFree(GetProcessHeap(), 0, buffers);
350 /* check that there's at least one channel in this device before
351 * we claim it as usable */
352 for(j = 0; j < buffers->mNumberBuffers; ++j)
353 if(buffers->mBuffers[j].mNumberChannels > 0)
355 if(j >= buffers->mNumberBuffers){
356 HeapFree(GetProcessHeap(), 0, buffers);
360 HeapFree(GetProcessHeap(), 0, buffers);
363 addr.mSelector = kAudioObjectPropertyName;
364 sc = AudioObjectGetPropertyData(devices[i], &addr, 0, NULL,
367 WARN("Unable to get _Name property for device %lu: %lx\n",
372 if(!CFStringGetCString(name, nameA, sizeof(nameA),
373 kCFStringEncodingUTF8)){
374 WARN("Error converting string to UTF8\n");
381 len = MultiByteToWideChar(CP_UNIXCP, 0, nameA, -1, NULL, 0);
382 (*ids)[*num] = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
384 HeapFree(GetProcessHeap(), 0, devices);
385 for(j = 0; j < *num; ++j){
386 HeapFree(GetProcessHeap(), 0, (*ids)[j]);
387 HeapFree(GetProcessHeap(), 0, (*keys)[j]);
389 HeapFree(GetProcessHeap(), 0, *ids);
390 HeapFree(GetProcessHeap(), 0, *keys);
391 return E_OUTOFMEMORY;
393 MultiByteToWideChar(CP_UNIXCP, 0, nameA, -1, (*ids)[*num], len);
395 (*keys)[*num] = HeapAlloc(GetProcessHeap(), 0, sizeof(AudioDeviceID));
397 HeapFree(GetProcessHeap(), 0, devices);
398 HeapFree(GetProcessHeap(), 0, (*ids)[*num]);
399 for(j = 0; j < *num; ++j){
400 HeapFree(GetProcessHeap(), 0, (*ids)[j]);
401 HeapFree(GetProcessHeap(), 0, (*keys)[j]);
403 HeapFree(GetProcessHeap(), 0, *ids);
404 HeapFree(GetProcessHeap(), 0, *keys);
405 return E_OUTOFMEMORY;
407 *(*keys)[*num] = devices[i];
409 if(*def_index == (UINT)-1 && devices[i] == default_id)
415 if(*def_index == (UINT)-1)
418 HeapFree(GetProcessHeap(), 0, devices);
423 HRESULT WINAPI AUDDRV_GetAudioEndpoint(AudioDeviceID *adevid, IMMDevice *dev,
424 EDataFlow dataflow, IAudioClient **out)
428 TRACE("%p %d %p\n", dev, dataflow, out);
430 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ACImpl));
432 return E_OUTOFMEMORY;
434 This->IAudioClient_iface.lpVtbl = &AudioClient_Vtbl;
435 This->IAudioRenderClient_iface.lpVtbl = &AudioRenderClient_Vtbl;
436 This->IAudioCaptureClient_iface.lpVtbl = &AudioCaptureClient_Vtbl;
437 This->IAudioClock_iface.lpVtbl = &AudioClock_Vtbl;
438 This->IAudioClock2_iface.lpVtbl = &AudioClock2_Vtbl;
439 This->IAudioStreamVolume_iface.lpVtbl = &AudioStreamVolume_Vtbl;
441 This->dataflow = dataflow;
443 if(dataflow == eRender)
444 This->scope = kAudioDevicePropertyScopeOutput;
445 else if(dataflow == eCapture)
446 This->scope = kAudioDevicePropertyScopeInput;
448 HeapFree(GetProcessHeap(), 0, This);
455 IMMDevice_AddRef(This->parent);
457 list_init(&This->avail_buffers);
459 This->adevid = *adevid;
461 *out = &This->IAudioClient_iface;
462 IAudioClient_AddRef(&This->IAudioClient_iface);
467 static HRESULT WINAPI AudioClient_QueryInterface(IAudioClient *iface,
468 REFIID riid, void **ppv)
470 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
475 if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IAudioClient))
478 IUnknown_AddRef((IUnknown*)*ppv);
481 WARN("Unknown interface %s\n", debugstr_guid(riid));
482 return E_NOINTERFACE;
485 static ULONG WINAPI AudioClient_AddRef(IAudioClient *iface)
487 ACImpl *This = impl_from_IAudioClient(iface);
489 ref = InterlockedIncrement(&This->ref);
490 TRACE("(%p) Refcount now %u\n", This, ref);
494 static ULONG WINAPI AudioClient_Release(IAudioClient *iface)
496 ACImpl *This = impl_from_IAudioClient(iface);
498 ref = InterlockedDecrement(&This->ref);
499 TRACE("(%p) Refcount now %u\n", This, ref);
501 IAudioClient_Stop(iface);
503 AudioQueueDispose(This->aqueue, 1);
505 EnterCriticalSection(&g_sessions_lock);
506 list_remove(&This->entry);
507 LeaveCriticalSection(&g_sessions_lock);
509 HeapFree(GetProcessHeap(), 0, This->vols);
510 HeapFree(GetProcessHeap(), 0, This->public_buffer);
511 CoTaskMemFree(This->fmt);
512 IMMDevice_Release(This->parent);
513 HeapFree(GetProcessHeap(), 0, This);
518 static void dump_fmt(const WAVEFORMATEX *fmt)
520 TRACE("wFormatTag: 0x%x (", fmt->wFormatTag);
521 switch(fmt->wFormatTag){
522 case WAVE_FORMAT_PCM:
523 TRACE("WAVE_FORMAT_PCM");
525 case WAVE_FORMAT_IEEE_FLOAT:
526 TRACE("WAVE_FORMAT_IEEE_FLOAT");
528 case WAVE_FORMAT_EXTENSIBLE:
529 TRACE("WAVE_FORMAT_EXTENSIBLE");
537 TRACE("nChannels: %u\n", fmt->nChannels);
538 TRACE("nSamplesPerSec: %u\n", fmt->nSamplesPerSec);
539 TRACE("nAvgBytesPerSec: %u\n", fmt->nAvgBytesPerSec);
540 TRACE("nBlockAlign: %u\n", fmt->nBlockAlign);
541 TRACE("wBitsPerSample: %u\n", fmt->wBitsPerSample);
542 TRACE("cbSize: %u\n", fmt->cbSize);
544 if(fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE){
545 WAVEFORMATEXTENSIBLE *fmtex = (void*)fmt;
546 TRACE("dwChannelMask: %08x\n", fmtex->dwChannelMask);
547 TRACE("Samples: %04x\n", fmtex->Samples.wReserved);
548 TRACE("SubFormat: %s\n", wine_dbgstr_guid(&fmtex->SubFormat));
552 static DWORD get_channel_mask(unsigned int channels)
558 return SPEAKER_FRONT_CENTER;
560 return SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
562 return SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT |
563 SPEAKER_LOW_FREQUENCY;
565 return SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_BACK_LEFT |
568 return SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_BACK_LEFT |
569 SPEAKER_BACK_RIGHT | SPEAKER_LOW_FREQUENCY;
571 return SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_BACK_LEFT |
572 SPEAKER_BACK_RIGHT | SPEAKER_LOW_FREQUENCY | SPEAKER_FRONT_CENTER;
574 return SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_BACK_LEFT |
575 SPEAKER_BACK_RIGHT | SPEAKER_LOW_FREQUENCY | SPEAKER_FRONT_CENTER |
578 FIXME("Unknown speaker configuration: %u\n", channels);
582 static WAVEFORMATEX *clone_format(const WAVEFORMATEX *fmt)
587 if(fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
588 size = sizeof(WAVEFORMATEXTENSIBLE);
590 size = sizeof(WAVEFORMATEX);
592 ret = CoTaskMemAlloc(size);
596 memcpy(ret, fmt, size);
598 ret->cbSize = size - sizeof(WAVEFORMATEX);
603 static HRESULT ca_get_audiodesc(AudioStreamBasicDescription *desc,
604 const WAVEFORMATEX *fmt)
606 const WAVEFORMATEXTENSIBLE *fmtex = (const WAVEFORMATEXTENSIBLE *)fmt;
608 desc->mFormatFlags = 0;
610 if(fmt->wFormatTag == WAVE_FORMAT_PCM ||
611 (fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
612 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM))){
613 desc->mFormatID = kAudioFormatLinearPCM;
614 if(fmt->wBitsPerSample > 8)
615 desc->mFormatFlags = kAudioFormatFlagIsSignedInteger;
616 }else if(fmt->wFormatTag == WAVE_FORMAT_IEEE_FLOAT ||
617 (fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
618 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT))){
619 desc->mFormatID = kAudioFormatLinearPCM;
620 desc->mFormatFlags = kAudioFormatFlagIsFloat;
621 }else if(fmt->wFormatTag == WAVE_FORMAT_MULAW ||
622 (fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
623 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_MULAW))){
624 desc->mFormatID = kAudioFormatULaw;
625 }else if(fmt->wFormatTag == WAVE_FORMAT_ALAW ||
626 (fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
627 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_ALAW))){
628 desc->mFormatID = kAudioFormatALaw;
630 return AUDCLNT_E_UNSUPPORTED_FORMAT;
632 desc->mSampleRate = fmt->nSamplesPerSec;
633 desc->mBytesPerPacket = fmt->nBlockAlign;
634 desc->mFramesPerPacket = 1;
635 desc->mBytesPerFrame = fmt->nBlockAlign;
636 desc->mChannelsPerFrame = fmt->nChannels;
637 desc->mBitsPerChannel = fmt->wBitsPerSample;
643 static void ca_out_buffer_cb(void *user, AudioQueueRef aqueue,
644 AudioQueueBufferRef buffer)
647 AQBuffer *buf = buffer->mUserData;
649 OSSpinLockLock(&This->lock);
650 list_add_tail(&This->avail_buffers, &buf->entry);
651 This->inbuf_frames -= buffer->mAudioDataByteSize / This->fmt->nBlockAlign;
652 OSSpinLockUnlock(&This->lock);
655 static void ca_in_buffer_cb(void *user, AudioQueueRef aqueue,
656 AudioQueueBufferRef buffer, const AudioTimeStamp *start,
657 UInt32 ndesc, const AudioStreamPacketDescription *descs)
660 AQBuffer *buf = buffer->mUserData;
662 OSSpinLockLock(&This->lock);
663 list_add_tail(&This->avail_buffers, &buf->entry);
664 This->inbuf_frames += buffer->mAudioDataByteSize / This->fmt->nBlockAlign;
665 OSSpinLockUnlock(&This->lock);
668 static HRESULT ca_setup_aqueue(AudioDeviceID did, EDataFlow flow,
669 const WAVEFORMATEX *fmt, void *user, AudioQueueRef *aqueue)
671 AudioStreamBasicDescription desc;
672 AudioObjectPropertyAddress addr;
678 addr.mScope = kAudioObjectPropertyScopeGlobal;
680 addr.mSelector = kAudioDevicePropertyDeviceUID;
683 sc = AudioObjectGetPropertyData(did, &addr, 0, NULL, &size, &uid);
685 WARN("Unable to get _DeviceUID property: %lx\n", sc);
689 hr = ca_get_audiodesc(&desc, fmt);
696 sc = AudioQueueNewOutput(&desc, ca_out_buffer_cb, user, NULL, NULL, 0,
698 else if(flow == eCapture)
699 sc = AudioQueueNewInput(&desc, ca_in_buffer_cb, user, NULL, NULL, 0,
706 WARN("Unable to create AudioQueue: %lx\n", sc);
711 sc = AudioQueueSetProperty(*aqueue, kAudioQueueProperty_CurrentDevice,
723 static void session_init_vols(AudioSession *session, UINT channels)
725 if(session->channel_count < channels){
728 if(session->channel_vols)
729 session->channel_vols = HeapReAlloc(GetProcessHeap(), 0,
730 session->channel_vols, sizeof(float) * channels);
732 session->channel_vols = HeapAlloc(GetProcessHeap(), 0,
733 sizeof(float) * channels);
734 if(!session->channel_vols)
737 for(i = session->channel_count; i < channels; ++i)
738 session->channel_vols[i] = 1.f;
740 session->channel_count = channels;
744 static AudioSession *create_session(const GUID *guid, IMMDevice *device,
749 ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(AudioSession));
753 memcpy(&ret->guid, guid, sizeof(GUID));
755 ret->device = device;
757 list_init(&ret->clients);
759 list_add_head(&g_sessions, &ret->entry);
761 InitializeCriticalSection(&ret->lock);
763 session_init_vols(ret, num_channels);
765 ret->master_vol = 1.f;
770 /* if channels == 0, then this will return or create a session with
771 * matching dataflow and GUID. otherwise, channels must also match */
772 static HRESULT get_audio_session(const GUID *sessionguid,
773 IMMDevice *device, UINT channels, AudioSession **out)
775 AudioSession *session;
777 if(!sessionguid || IsEqualGUID(sessionguid, &GUID_NULL)){
778 *out = create_session(&GUID_NULL, device, channels);
780 return E_OUTOFMEMORY;
786 LIST_FOR_EACH_ENTRY(session, &g_sessions, AudioSession, entry){
787 if(session->device == device &&
788 IsEqualGUID(sessionguid, &session->guid)){
789 session_init_vols(session, channels);
796 *out = create_session(sessionguid, device, channels);
798 return E_OUTOFMEMORY;
804 static HRESULT WINAPI AudioClient_Initialize(IAudioClient *iface,
805 AUDCLNT_SHAREMODE mode, DWORD flags, REFERENCE_TIME duration,
806 REFERENCE_TIME period, const WAVEFORMATEX *fmt,
807 const GUID *sessionguid)
809 ACImpl *This = impl_from_IAudioClient(iface);
814 TRACE("(%p)->(%x, %x, %s, %s, %p, %s)\n", This, mode, flags,
815 wine_dbgstr_longlong(duration), wine_dbgstr_longlong(period), fmt, debugstr_guid(sessionguid));
822 if(mode != AUDCLNT_SHAREMODE_SHARED && mode != AUDCLNT_SHAREMODE_EXCLUSIVE)
823 return AUDCLNT_E_NOT_INITIALIZED;
825 if(flags & ~(AUDCLNT_STREAMFLAGS_CROSSPROCESS |
826 AUDCLNT_STREAMFLAGS_LOOPBACK |
827 AUDCLNT_STREAMFLAGS_EVENTCALLBACK |
828 AUDCLNT_STREAMFLAGS_NOPERSIST |
829 AUDCLNT_STREAMFLAGS_RATEADJUST |
830 AUDCLNT_SESSIONFLAGS_EXPIREWHENUNOWNED |
831 AUDCLNT_SESSIONFLAGS_DISPLAY_HIDE |
832 AUDCLNT_SESSIONFLAGS_DISPLAY_HIDEWHENEXPIRED)){
833 TRACE("Unknown flags: %08x\n", flags);
837 OSSpinLockLock(&This->lock);
840 OSSpinLockUnlock(&This->lock);
841 return AUDCLNT_E_ALREADY_INITIALIZED;
844 hr = ca_setup_aqueue(This->adevid, This->dataflow, fmt, This, &This->aqueue);
846 OSSpinLockUnlock(&This->lock);
850 This->fmt = clone_format(fmt);
852 AudioQueueDispose(This->aqueue, 1);
854 OSSpinLockUnlock(&This->lock);
855 return E_OUTOFMEMORY;
859 This->period_ms = period / 10000;
860 if(This->period_ms == 0)
863 This->period_ms = MinimumPeriod / 10000;
866 duration = 300000; /* 0.03s */
867 This->bufsize_frames = ceil(fmt->nSamplesPerSec * (duration / 10000000.));
869 if(This->dataflow == eCapture){
871 UInt32 bsize = ceil((This->bufsize_frames / (double)CAPTURE_BUFFERS) *
872 This->fmt->nBlockAlign);
873 for(i = 0; i < CAPTURE_BUFFERS; ++i){
876 buf = HeapAlloc(GetProcessHeap(), 0, sizeof(AQBuffer));
878 AudioQueueDispose(This->aqueue, 1);
880 CoTaskMemFree(This->fmt);
882 OSSpinLockUnlock(&This->lock);
883 return E_OUTOFMEMORY;
886 sc = AudioQueueAllocateBuffer(This->aqueue, bsize, &buf->buf);
888 AudioQueueDispose(This->aqueue, 1);
890 CoTaskMemFree(This->fmt);
892 OSSpinLockUnlock(&This->lock);
893 WARN("Couldn't allocate buffer: %lx\n", sc);
897 buf->buf->mUserData = buf;
899 sc = AudioQueueEnqueueBuffer(This->aqueue, buf->buf, 0, NULL);
901 ERR("Couldn't enqueue buffer: %lx\n", sc);
907 This->vols = HeapAlloc(GetProcessHeap(), 0, fmt->nChannels * sizeof(float));
909 AudioQueueDispose(This->aqueue, 1);
911 CoTaskMemFree(This->fmt);
913 OSSpinLockUnlock(&This->lock);
914 return E_OUTOFMEMORY;
917 for(i = 0; i < fmt->nChannels; ++i)
923 EnterCriticalSection(&g_sessions_lock);
925 hr = get_audio_session(sessionguid, This->parent, fmt->nChannels,
928 LeaveCriticalSection(&g_sessions_lock);
929 AudioQueueDispose(This->aqueue, 1);
931 CoTaskMemFree(This->fmt);
933 HeapFree(GetProcessHeap(), 0, This->vols);
935 OSSpinLockUnlock(&This->lock);
939 list_add_tail(&This->session->clients, &This->entry);
941 LeaveCriticalSection(&g_sessions_lock);
945 OSSpinLockUnlock(&This->lock);
950 static HRESULT WINAPI AudioClient_GetBufferSize(IAudioClient *iface,
953 ACImpl *This = impl_from_IAudioClient(iface);
955 TRACE("(%p)->(%p)\n", This, frames);
960 OSSpinLockLock(&This->lock);
963 OSSpinLockUnlock(&This->lock);
964 return AUDCLNT_E_NOT_INITIALIZED;
967 *frames = This->bufsize_frames;
969 OSSpinLockUnlock(&This->lock);
974 static HRESULT ca_get_max_stream_latency(ACImpl *This, UInt32 *max)
976 AudioObjectPropertyAddress addr;
982 addr.mScope = This->scope;
984 addr.mSelector = kAudioDevicePropertyStreams;
986 sc = AudioObjectGetPropertyDataSize(This->adevid, &addr, 0, NULL,
989 WARN("Unable to get size for _Streams property: %lx\n", sc);
993 ids = HeapAlloc(GetProcessHeap(), 0, size);
995 return E_OUTOFMEMORY;
997 sc = AudioObjectGetPropertyData(This->adevid, &addr, 0, NULL, &size, ids);
999 WARN("Unable to get _Streams property: %lx\n", sc);
1000 HeapFree(GetProcessHeap(), 0, ids);
1004 nstreams = size / sizeof(AudioStreamID);
1007 addr.mSelector = kAudioStreamPropertyLatency;
1008 for(i = 0; i < nstreams; ++i){
1011 size = sizeof(latency);
1012 sc = AudioObjectGetPropertyData(ids[i], &addr, 0, NULL,
1015 WARN("Unable to get _Latency property: %lx\n", sc);
1023 HeapFree(GetProcessHeap(), 0, ids);
1028 static HRESULT WINAPI AudioClient_GetStreamLatency(IAudioClient *iface,
1029 REFERENCE_TIME *out)
1031 ACImpl *This = impl_from_IAudioClient(iface);
1032 UInt32 latency, stream_latency, size;
1033 AudioObjectPropertyAddress addr;
1037 TRACE("(%p)->(%p)\n", This, out);
1042 OSSpinLockLock(&This->lock);
1045 OSSpinLockUnlock(&This->lock);
1046 return AUDCLNT_E_NOT_INITIALIZED;
1049 addr.mScope = This->scope;
1050 addr.mSelector = kAudioDevicePropertyLatency;
1053 size = sizeof(latency);
1054 sc = AudioObjectGetPropertyData(This->adevid, &addr, 0, NULL,
1057 WARN("Couldn't get _Latency property: %lx\n", sc);
1058 OSSpinLockUnlock(&This->lock);
1062 hr = ca_get_max_stream_latency(This, &stream_latency);
1064 OSSpinLockUnlock(&This->lock);
1068 latency += stream_latency;
1069 *out = (latency / (double)This->fmt->nSamplesPerSec) * 10000000;
1071 OSSpinLockUnlock(&This->lock);
1076 static HRESULT AudioClient_GetCurrentPadding_nolock(ACImpl *This,
1080 return AUDCLNT_E_NOT_INITIALIZED;
1082 *numpad = This->inbuf_frames;
1087 static HRESULT WINAPI AudioClient_GetCurrentPadding(IAudioClient *iface,
1090 ACImpl *This = impl_from_IAudioClient(iface);
1093 TRACE("(%p)->(%p)\n", This, numpad);
1098 OSSpinLockLock(&This->lock);
1100 hr = AudioClient_GetCurrentPadding_nolock(This, numpad);
1102 OSSpinLockUnlock(&This->lock);
1107 static HRESULT WINAPI AudioClient_IsFormatSupported(IAudioClient *iface,
1108 AUDCLNT_SHAREMODE mode, const WAVEFORMATEX *pwfx,
1109 WAVEFORMATEX **outpwfx)
1111 ACImpl *This = impl_from_IAudioClient(iface);
1112 AudioQueueRef aqueue;
1115 TRACE("(%p)->(%x, %p, %p)\n", This, mode, pwfx, outpwfx);
1117 if(!pwfx || (mode == AUDCLNT_SHAREMODE_SHARED && !outpwfx))
1120 if(mode != AUDCLNT_SHAREMODE_SHARED && mode != AUDCLNT_SHAREMODE_EXCLUSIVE)
1121 return E_INVALIDARG;
1123 if(pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
1124 pwfx->cbSize < sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX))
1125 return E_INVALIDARG;
1129 OSSpinLockLock(&This->lock);
1131 hr = ca_setup_aqueue(This->adevid, This->dataflow, pwfx, NULL, &aqueue);
1133 AudioQueueDispose(aqueue, 1);
1134 OSSpinLockUnlock(&This->lock);
1137 TRACE("returning %08x\n", S_OK);
1141 OSSpinLockUnlock(&This->lock);
1146 TRACE("returning %08x\n", AUDCLNT_E_UNSUPPORTED_FORMAT);
1147 return AUDCLNT_E_UNSUPPORTED_FORMAT;
1150 static HRESULT WINAPI AudioClient_GetMixFormat(IAudioClient *iface,
1151 WAVEFORMATEX **pwfx)
1153 ACImpl *This = impl_from_IAudioClient(iface);
1154 WAVEFORMATEXTENSIBLE *fmt;
1158 AudioBufferList *buffers;
1159 AudioObjectPropertyAddress addr;
1162 TRACE("(%p)->(%p)\n", This, pwfx);
1168 fmt = CoTaskMemAlloc(sizeof(WAVEFORMATEXTENSIBLE));
1170 return E_OUTOFMEMORY;
1172 fmt->Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
1174 addr.mScope = This->scope;
1176 addr.mSelector = kAudioDevicePropertyStreamConfiguration;
1178 sc = AudioObjectGetPropertyDataSize(This->adevid, &addr, 0, NULL, &size);
1181 WARN("Unable to get size for _StreamConfiguration property: %lx\n", sc);
1185 buffers = HeapAlloc(GetProcessHeap(), 0, size);
1188 return E_OUTOFMEMORY;
1191 sc = AudioObjectGetPropertyData(This->adevid, &addr, 0, NULL,
1195 HeapFree(GetProcessHeap(), 0, buffers);
1196 WARN("Unable to get _StreamConfiguration property: %lx\n", sc);
1200 fmt->Format.nChannels = 0;
1201 for(i = 0; i < buffers->mNumberBuffers; ++i)
1202 fmt->Format.nChannels += buffers->mBuffers[i].mNumberChannels;
1204 HeapFree(GetProcessHeap(), 0, buffers);
1206 fmt->dwChannelMask = get_channel_mask(fmt->Format.nChannels);
1208 addr.mSelector = kAudioDevicePropertyNominalSampleRate;
1209 size = sizeof(Float64);
1210 sc = AudioObjectGetPropertyData(This->adevid, &addr, 0, NULL, &size, &rate);
1213 WARN("Unable to get _NominalSampleRate property: %lx\n", sc);
1216 fmt->Format.nSamplesPerSec = rate;
1218 fmt->Format.wBitsPerSample = 32;
1219 fmt->SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
1221 fmt->Format.nBlockAlign = (fmt->Format.wBitsPerSample *
1222 fmt->Format.nChannels) / 8;
1223 fmt->Format.nAvgBytesPerSec = fmt->Format.nSamplesPerSec *
1224 fmt->Format.nBlockAlign;
1226 fmt->Samples.wValidBitsPerSample = fmt->Format.wBitsPerSample;
1227 fmt->Format.cbSize = sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
1229 *pwfx = (WAVEFORMATEX*)fmt;
1235 static HRESULT WINAPI AudioClient_GetDevicePeriod(IAudioClient *iface,
1236 REFERENCE_TIME *defperiod, REFERENCE_TIME *minperiod)
1238 ACImpl *This = impl_from_IAudioClient(iface);
1240 TRACE("(%p)->(%p, %p)\n", This, defperiod, minperiod);
1242 if(!defperiod && !minperiod)
1245 OSSpinLockLock(&This->lock);
1247 if(This->period_ms){
1249 *defperiod = This->period_ms * 10000;
1251 *minperiod = This->period_ms * 10000;
1254 *defperiod = DefaultPeriod;
1256 *minperiod = MinimumPeriod;
1259 OSSpinLockUnlock(&This->lock);
1264 void CALLBACK ca_period_cb(void *user, BOOLEAN timer)
1266 ACImpl *This = user;
1268 OSSpinLockLock(&This->lock);
1270 SetEvent(This->event);
1271 OSSpinLockUnlock(&This->lock);
1274 static HRESULT WINAPI AudioClient_Start(IAudioClient *iface)
1276 ACImpl *This = impl_from_IAudioClient(iface);
1279 TRACE("(%p)\n", This);
1281 OSSpinLockLock(&This->lock);
1284 OSSpinLockUnlock(&This->lock);
1285 return AUDCLNT_E_NOT_INITIALIZED;
1288 if(This->playing != StateStopped){
1289 OSSpinLockUnlock(&This->lock);
1290 return AUDCLNT_E_NOT_STOPPED;
1293 if((This->flags & AUDCLNT_STREAMFLAGS_EVENTCALLBACK) && !This->event){
1294 OSSpinLockUnlock(&This->lock);
1295 return AUDCLNT_E_EVENTHANDLE_NOT_SET;
1299 if(!CreateTimerQueueTimer(&This->timer, g_timer_q,
1300 ca_period_cb, This, 0, This->period_ms, 0))
1301 ERR("Unable to create timer: %u\n", GetLastError());
1303 This->playing = StateInTransition;
1305 OSSpinLockUnlock(&This->lock);
1307 sc = AudioQueueStart(This->aqueue, NULL);
1309 WARN("Unable to start audio queue: %lx\n", sc);
1313 OSSpinLockLock(&This->lock);
1315 This->playing = StatePlaying;
1317 OSSpinLockUnlock(&This->lock);
1322 static HRESULT WINAPI AudioClient_Stop(IAudioClient *iface)
1324 ACImpl *This = impl_from_IAudioClient(iface);
1327 TRACE("(%p)\n", This);
1329 OSSpinLockLock(&This->lock);
1332 OSSpinLockUnlock(&This->lock);
1333 return AUDCLNT_E_NOT_INITIALIZED;
1336 if(This->playing == StateStopped){
1337 OSSpinLockUnlock(&This->lock);
1341 if(This->playing == StateInTransition){
1342 OSSpinLockUnlock(&This->lock);
1346 if(This->timer && This->timer != INVALID_HANDLE_VALUE){
1347 DeleteTimerQueueTimer(g_timer_q, This->timer, INVALID_HANDLE_VALUE);
1351 This->playing = StateInTransition;
1353 OSSpinLockUnlock(&This->lock);
1355 sc = AudioQueueFlush(This->aqueue);
1357 WARN("Unable to flush audio queue: %lx\n", sc);
1359 sc = AudioQueuePause(This->aqueue);
1361 WARN("Unable to pause audio queue: %lx\n", sc);
1365 OSSpinLockLock(&This->lock);
1367 This->playing = StateStopped;
1369 OSSpinLockUnlock(&This->lock);
1374 static HRESULT WINAPI AudioClient_Reset(IAudioClient *iface)
1376 ACImpl *This = impl_from_IAudioClient(iface);
1380 TRACE("(%p)\n", This);
1382 OSSpinLockLock(&This->lock);
1385 OSSpinLockUnlock(&This->lock);
1386 return AUDCLNT_E_NOT_INITIALIZED;
1389 if(This->playing != StateStopped){
1390 OSSpinLockUnlock(&This->lock);
1391 return AUDCLNT_E_NOT_STOPPED;
1394 This->written_frames = 0;
1396 hr = AudioClock_GetPosition_nolock(This, &This->last_time, NULL, TRUE);
1398 OSSpinLockUnlock(&This->lock);
1402 OSSpinLockUnlock(&This->lock);
1404 sc = AudioQueueReset(This->aqueue);
1406 WARN("Unable to reset audio queue: %lx\n", sc);
1413 static HRESULT WINAPI AudioClient_SetEventHandle(IAudioClient *iface,
1416 ACImpl *This = impl_from_IAudioClient(iface);
1418 TRACE("(%p)->(%p)\n", This, event);
1421 return E_INVALIDARG;
1423 OSSpinLockLock(&This->lock);
1426 OSSpinLockUnlock(&This->lock);
1427 return AUDCLNT_E_NOT_INITIALIZED;
1430 if(!(This->flags & AUDCLNT_STREAMFLAGS_EVENTCALLBACK)){
1431 OSSpinLockUnlock(&This->lock);
1432 return AUDCLNT_E_EVENTHANDLE_NOT_EXPECTED;
1435 This->event = event;
1437 OSSpinLockUnlock(&This->lock);
1442 static HRESULT WINAPI AudioClient_GetService(IAudioClient *iface, REFIID riid,
1445 ACImpl *This = impl_from_IAudioClient(iface);
1447 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
1453 OSSpinLockLock(&This->lock);
1456 OSSpinLockUnlock(&This->lock);
1457 return AUDCLNT_E_NOT_INITIALIZED;
1460 if(IsEqualIID(riid, &IID_IAudioRenderClient)){
1461 if(This->dataflow != eRender){
1462 OSSpinLockUnlock(&This->lock);
1463 return AUDCLNT_E_WRONG_ENDPOINT_TYPE;
1465 IAudioRenderClient_AddRef(&This->IAudioRenderClient_iface);
1466 *ppv = &This->IAudioRenderClient_iface;
1467 }else if(IsEqualIID(riid, &IID_IAudioCaptureClient)){
1468 if(This->dataflow != eCapture){
1469 OSSpinLockUnlock(&This->lock);
1470 return AUDCLNT_E_WRONG_ENDPOINT_TYPE;
1472 IAudioCaptureClient_AddRef(&This->IAudioCaptureClient_iface);
1473 *ppv = &This->IAudioCaptureClient_iface;
1474 }else if(IsEqualIID(riid, &IID_IAudioClock)){
1475 IAudioClock_AddRef(&This->IAudioClock_iface);
1476 *ppv = &This->IAudioClock_iface;
1477 }else if(IsEqualIID(riid, &IID_IAudioStreamVolume)){
1478 IAudioStreamVolume_AddRef(&This->IAudioStreamVolume_iface);
1479 *ppv = &This->IAudioStreamVolume_iface;
1480 }else if(IsEqualIID(riid, &IID_IAudioSessionControl)){
1481 if(!This->session_wrapper){
1482 This->session_wrapper = AudioSessionWrapper_Create(This);
1483 if(!This->session_wrapper){
1484 OSSpinLockUnlock(&This->lock);
1485 return E_OUTOFMEMORY;
1488 IAudioSessionControl2_AddRef(&This->session_wrapper->IAudioSessionControl2_iface);
1490 *ppv = &This->session_wrapper->IAudioSessionControl2_iface;
1491 }else if(IsEqualIID(riid, &IID_IChannelAudioVolume)){
1492 if(!This->session_wrapper){
1493 This->session_wrapper = AudioSessionWrapper_Create(This);
1494 if(!This->session_wrapper){
1495 OSSpinLockUnlock(&This->lock);
1496 return E_OUTOFMEMORY;
1499 IChannelAudioVolume_AddRef(&This->session_wrapper->IChannelAudioVolume_iface);
1501 *ppv = &This->session_wrapper->IChannelAudioVolume_iface;
1502 }else if(IsEqualIID(riid, &IID_ISimpleAudioVolume)){
1503 if(!This->session_wrapper){
1504 This->session_wrapper = AudioSessionWrapper_Create(This);
1505 if(!This->session_wrapper){
1506 OSSpinLockUnlock(&This->lock);
1507 return E_OUTOFMEMORY;
1510 ISimpleAudioVolume_AddRef(&This->session_wrapper->ISimpleAudioVolume_iface);
1512 *ppv = &This->session_wrapper->ISimpleAudioVolume_iface;
1516 OSSpinLockUnlock(&This->lock);
1520 OSSpinLockUnlock(&This->lock);
1522 FIXME("stub %s\n", debugstr_guid(riid));
1523 return E_NOINTERFACE;
1526 static const IAudioClientVtbl AudioClient_Vtbl =
1528 AudioClient_QueryInterface,
1530 AudioClient_Release,
1531 AudioClient_Initialize,
1532 AudioClient_GetBufferSize,
1533 AudioClient_GetStreamLatency,
1534 AudioClient_GetCurrentPadding,
1535 AudioClient_IsFormatSupported,
1536 AudioClient_GetMixFormat,
1537 AudioClient_GetDevicePeriod,
1541 AudioClient_SetEventHandle,
1542 AudioClient_GetService
1545 static HRESULT WINAPI AudioRenderClient_QueryInterface(
1546 IAudioRenderClient *iface, REFIID riid, void **ppv)
1548 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
1554 if(IsEqualIID(riid, &IID_IUnknown) ||
1555 IsEqualIID(riid, &IID_IAudioRenderClient))
1558 IUnknown_AddRef((IUnknown*)*ppv);
1562 WARN("Unknown interface %s\n", debugstr_guid(riid));
1563 return E_NOINTERFACE;
1566 static ULONG WINAPI AudioRenderClient_AddRef(IAudioRenderClient *iface)
1568 ACImpl *This = impl_from_IAudioRenderClient(iface);
1569 return AudioClient_AddRef(&This->IAudioClient_iface);
1572 static ULONG WINAPI AudioRenderClient_Release(IAudioRenderClient *iface)
1574 ACImpl *This = impl_from_IAudioRenderClient(iface);
1575 return AudioClient_Release(&This->IAudioClient_iface);
1578 static HRESULT WINAPI AudioRenderClient_GetBuffer(IAudioRenderClient *iface,
1579 UINT32 frames, BYTE **data)
1581 ACImpl *This = impl_from_IAudioRenderClient(iface);
1583 UINT32 pad, bytes = frames * This->fmt->nBlockAlign;
1587 TRACE("(%p)->(%u, %p)\n", This, frames, data);
1592 OSSpinLockLock(&This->lock);
1594 if(This->getbuf_last){
1595 OSSpinLockUnlock(&This->lock);
1596 return AUDCLNT_E_OUT_OF_ORDER;
1600 This->getbuf_last = TRUE;
1601 OSSpinLockUnlock(&This->lock);
1605 hr = AudioClient_GetCurrentPadding_nolock(This, &pad);
1607 OSSpinLockUnlock(&This->lock);
1611 if(pad + frames > This->bufsize_frames){
1612 OSSpinLockUnlock(&This->lock);
1613 return AUDCLNT_E_BUFFER_TOO_LARGE;
1616 LIST_FOR_EACH_ENTRY(buf, &This->avail_buffers, AQBuffer, entry){
1617 if(buf->buf->mAudioDataBytesCapacity >= bytes){
1618 This->public_buffer = buf->buf;
1619 list_remove(&buf->entry);
1624 if(&buf->entry == &This->avail_buffers){
1625 sc = AudioQueueAllocateBuffer(This->aqueue, bytes,
1626 &This->public_buffer);
1628 OSSpinLockUnlock(&This->lock);
1629 WARN("Unable to allocate buffer: %lx\n", sc);
1632 buf = HeapAlloc(GetProcessHeap(), 0, sizeof(AQBuffer));
1634 AudioQueueFreeBuffer(This->aqueue, This->public_buffer);
1635 This->public_buffer = NULL;
1636 OSSpinLockUnlock(&This->lock);
1637 return E_OUTOFMEMORY;
1639 buf->buf = This->public_buffer;
1640 This->public_buffer->mUserData = buf;
1643 *data = This->public_buffer->mAudioData;
1645 This->getbuf_last = TRUE;
1647 OSSpinLockUnlock(&This->lock);
1652 static HRESULT WINAPI AudioRenderClient_ReleaseBuffer(
1653 IAudioRenderClient *iface, UINT32 frames, DWORD flags)
1655 ACImpl *This = impl_from_IAudioRenderClient(iface);
1658 TRACE("(%p)->(%u, %x)\n", This, frames, flags);
1660 OSSpinLockLock(&This->lock);
1662 if(!This->getbuf_last){
1663 OSSpinLockUnlock(&This->lock);
1664 return AUDCLNT_E_OUT_OF_ORDER;
1667 if(flags & AUDCLNT_BUFFERFLAGS_SILENT){
1668 WAVEFORMATEXTENSIBLE *fmtex = (WAVEFORMATEXTENSIBLE*)This->fmt;
1669 if((This->fmt->wFormatTag == WAVE_FORMAT_PCM ||
1670 (This->fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
1671 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM))) &&
1672 This->fmt->wBitsPerSample == 8)
1673 memset(This->public_buffer->mAudioData, 128,
1674 frames * This->fmt->nBlockAlign);
1676 memset(This->public_buffer->mAudioData, 0,
1677 frames * This->fmt->nBlockAlign);
1680 This->public_buffer->mAudioDataByteSize = frames * This->fmt->nBlockAlign;
1682 sc = AudioQueueEnqueueBuffer(This->aqueue, This->public_buffer, 0, NULL);
1684 OSSpinLockUnlock(&This->lock);
1685 WARN("Unable to enqueue buffer: %lx\n", sc);
1689 if(This->playing == StateStopped)
1690 AudioQueuePrime(This->aqueue, 0, NULL);
1692 This->public_buffer = NULL;
1693 This->getbuf_last = FALSE;
1694 This->written_frames += frames;
1695 This->inbuf_frames += frames;
1697 OSSpinLockUnlock(&This->lock);
1702 static const IAudioRenderClientVtbl AudioRenderClient_Vtbl = {
1703 AudioRenderClient_QueryInterface,
1704 AudioRenderClient_AddRef,
1705 AudioRenderClient_Release,
1706 AudioRenderClient_GetBuffer,
1707 AudioRenderClient_ReleaseBuffer
1710 static HRESULT WINAPI AudioCaptureClient_QueryInterface(
1711 IAudioCaptureClient *iface, REFIID riid, void **ppv)
1713 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
1719 if(IsEqualIID(riid, &IID_IUnknown) ||
1720 IsEqualIID(riid, &IID_IAudioCaptureClient))
1723 IUnknown_AddRef((IUnknown*)*ppv);
1727 WARN("Unknown interface %s\n", debugstr_guid(riid));
1728 return E_NOINTERFACE;
1731 static ULONG WINAPI AudioCaptureClient_AddRef(IAudioCaptureClient *iface)
1733 ACImpl *This = impl_from_IAudioCaptureClient(iface);
1734 return IAudioClient_AddRef(&This->IAudioClient_iface);
1737 static ULONG WINAPI AudioCaptureClient_Release(IAudioCaptureClient *iface)
1739 ACImpl *This = impl_from_IAudioCaptureClient(iface);
1740 return IAudioClient_Release(&This->IAudioClient_iface);
1743 static HRESULT WINAPI AudioCaptureClient_GetBuffer(IAudioCaptureClient *iface,
1744 BYTE **data, UINT32 *frames, DWORD *flags, UINT64 *devpos,
1747 ACImpl *This = impl_from_IAudioCaptureClient(iface);
1749 TRACE("(%p)->(%p, %p, %p, %p, %p)\n", This, data, frames, flags,
1752 if(!data || !frames || !flags)
1755 OSSpinLockLock(&This->lock);
1757 if(This->getbuf_last){
1758 OSSpinLockUnlock(&This->lock);
1759 return AUDCLNT_E_OUT_OF_ORDER;
1762 if(This->public_buffer){
1763 *data = This->public_buffer->mAudioData;
1765 This->public_buffer->mAudioDataByteSize / This->fmt->nBlockAlign;
1767 struct list *head = list_head(&This->avail_buffers);
1772 AQBuffer *buf = LIST_ENTRY(head, AQBuffer, entry);
1773 This->public_buffer = buf->buf;
1774 *data = This->public_buffer->mAudioData;
1776 This->public_buffer->mAudioDataByteSize / This->fmt->nBlockAlign;
1777 list_remove(&buf->entry);
1782 This->written_frames += *frames;
1783 This->inbuf_frames -= *frames;
1784 This->getbuf_last = TRUE;
1786 if(devpos || qpcpos)
1787 AudioClock_GetPosition_nolock(This, devpos, qpcpos, FALSE);
1789 OSSpinLockUnlock(&This->lock);
1791 return *frames ? S_OK : AUDCLNT_S_BUFFER_EMPTY;
1794 static HRESULT WINAPI AudioCaptureClient_ReleaseBuffer(
1795 IAudioCaptureClient *iface, UINT32 done)
1797 ACImpl *This = impl_from_IAudioCaptureClient(iface);
1798 UINT32 pbuf_frames =
1799 This->public_buffer->mAudioDataByteSize / This->fmt->nBlockAlign;
1802 TRACE("(%p)->(%u)\n", This, done);
1804 OSSpinLockLock(&This->lock);
1806 if(!This->getbuf_last){
1807 OSSpinLockUnlock(&This->lock);
1808 return AUDCLNT_E_OUT_OF_ORDER;
1811 if(done != 0 && done != pbuf_frames){
1812 OSSpinLockUnlock(&This->lock);
1813 return AUDCLNT_E_INVALID_SIZE;
1817 sc = AudioQueueEnqueueBuffer(This->aqueue, This->public_buffer,
1820 WARN("Unable to enqueue buffer: %lx\n", sc);
1821 This->public_buffer = NULL;
1824 This->getbuf_last = FALSE;
1826 OSSpinLockUnlock(&This->lock);
1831 static HRESULT WINAPI AudioCaptureClient_GetNextPacketSize(
1832 IAudioCaptureClient *iface, UINT32 *frames)
1834 ACImpl *This = impl_from_IAudioCaptureClient(iface);
1838 TRACE("(%p)->(%p)\n", This, frames);
1843 OSSpinLockLock(&This->lock);
1845 head = list_head(&This->avail_buffers);
1848 *frames = This->bufsize_frames / CAPTURE_BUFFERS;
1849 OSSpinLockUnlock(&This->lock);
1853 buf = LIST_ENTRY(head, AQBuffer, entry);
1854 *frames = buf->buf->mAudioDataByteSize / This->fmt->nBlockAlign;
1856 OSSpinLockUnlock(&This->lock);
1861 static const IAudioCaptureClientVtbl AudioCaptureClient_Vtbl =
1863 AudioCaptureClient_QueryInterface,
1864 AudioCaptureClient_AddRef,
1865 AudioCaptureClient_Release,
1866 AudioCaptureClient_GetBuffer,
1867 AudioCaptureClient_ReleaseBuffer,
1868 AudioCaptureClient_GetNextPacketSize
1871 static HRESULT WINAPI AudioClock_QueryInterface(IAudioClock *iface,
1872 REFIID riid, void **ppv)
1874 ACImpl *This = impl_from_IAudioClock(iface);
1876 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
1882 if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IAudioClock))
1884 else if(IsEqualIID(riid, &IID_IAudioClock2))
1885 *ppv = &This->IAudioClock2_iface;
1887 IUnknown_AddRef((IUnknown*)*ppv);
1891 WARN("Unknown interface %s\n", debugstr_guid(riid));
1892 return E_NOINTERFACE;
1895 static ULONG WINAPI AudioClock_AddRef(IAudioClock *iface)
1897 ACImpl *This = impl_from_IAudioClock(iface);
1898 return IAudioClient_AddRef(&This->IAudioClient_iface);
1901 static ULONG WINAPI AudioClock_Release(IAudioClock *iface)
1903 ACImpl *This = impl_from_IAudioClock(iface);
1904 return IAudioClient_Release(&This->IAudioClient_iface);
1907 static HRESULT WINAPI AudioClock_GetFrequency(IAudioClock *iface, UINT64 *freq)
1909 ACImpl *This = impl_from_IAudioClock(iface);
1911 TRACE("(%p)->(%p)\n", This, freq);
1913 *freq = This->fmt->nSamplesPerSec;
1918 static HRESULT AudioClock_GetPosition_nolock(ACImpl *This,
1919 UINT64 *pos, UINT64 *qpctime, BOOL raw)
1921 AudioTimeStamp time;
1924 sc = AudioQueueGetCurrentTime(This->aqueue, NULL, &time, NULL);
1925 if(sc == kAudioQueueErr_InvalidRunState){
1927 }else if(sc == noErr){
1928 if(!(time.mFlags & kAudioTimeStampSampleTimeValid)){
1929 FIXME("Sample time not valid, should calculate from something else\n");
1934 *pos = time.mSampleTime;
1936 *pos = time.mSampleTime - This->last_time;
1938 WARN("Unable to get current time: %lx\n", sc);
1943 LARGE_INTEGER stamp, freq;
1944 QueryPerformanceCounter(&stamp);
1945 QueryPerformanceFrequency(&freq);
1946 *qpctime = (stamp.QuadPart * (INT64)10000000) / freq.QuadPart;
1952 static HRESULT WINAPI AudioClock_GetPosition(IAudioClock *iface, UINT64 *pos,
1955 ACImpl *This = impl_from_IAudioClock(iface);
1958 TRACE("(%p)->(%p, %p)\n", This, pos, qpctime);
1963 OSSpinLockLock(&This->lock);
1965 hr = AudioClock_GetPosition_nolock(This, pos, qpctime, FALSE);
1967 OSSpinLockUnlock(&This->lock);
1972 static HRESULT WINAPI AudioClock_GetCharacteristics(IAudioClock *iface,
1975 ACImpl *This = impl_from_IAudioClock(iface);
1977 TRACE("(%p)->(%p)\n", This, chars);
1982 *chars = AUDIOCLOCK_CHARACTERISTIC_FIXED_FREQ;
1987 static const IAudioClockVtbl AudioClock_Vtbl =
1989 AudioClock_QueryInterface,
1992 AudioClock_GetFrequency,
1993 AudioClock_GetPosition,
1994 AudioClock_GetCharacteristics
1997 static HRESULT WINAPI AudioClock2_QueryInterface(IAudioClock2 *iface,
1998 REFIID riid, void **ppv)
2000 ACImpl *This = impl_from_IAudioClock2(iface);
2001 return IAudioClock_QueryInterface(&This->IAudioClock_iface, riid, ppv);
2004 static ULONG WINAPI AudioClock2_AddRef(IAudioClock2 *iface)
2006 ACImpl *This = impl_from_IAudioClock2(iface);
2007 return IAudioClient_AddRef(&This->IAudioClient_iface);
2010 static ULONG WINAPI AudioClock2_Release(IAudioClock2 *iface)
2012 ACImpl *This = impl_from_IAudioClock2(iface);
2013 return IAudioClient_Release(&This->IAudioClient_iface);
2016 static HRESULT WINAPI AudioClock2_GetDevicePosition(IAudioClock2 *iface,
2017 UINT64 *pos, UINT64 *qpctime)
2019 ACImpl *This = impl_from_IAudioClock2(iface);
2021 FIXME("(%p)->(%p, %p)\n", This, pos, qpctime);
2026 static const IAudioClock2Vtbl AudioClock2_Vtbl =
2028 AudioClock2_QueryInterface,
2030 AudioClock2_Release,
2031 AudioClock2_GetDevicePosition
2034 static AudioSessionWrapper *AudioSessionWrapper_Create(ACImpl *client)
2036 AudioSessionWrapper *ret;
2038 ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
2039 sizeof(AudioSessionWrapper));
2043 ret->IAudioSessionControl2_iface.lpVtbl = &AudioSessionControl2_Vtbl;
2044 ret->ISimpleAudioVolume_iface.lpVtbl = &SimpleAudioVolume_Vtbl;
2045 ret->IChannelAudioVolume_iface.lpVtbl = &ChannelAudioVolume_Vtbl;
2049 ret->client = client;
2051 ret->session = client->session;
2052 AudioClient_AddRef(&client->IAudioClient_iface);
2058 static HRESULT WINAPI AudioSessionControl_QueryInterface(
2059 IAudioSessionControl2 *iface, REFIID riid, void **ppv)
2061 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2067 if(IsEqualIID(riid, &IID_IUnknown) ||
2068 IsEqualIID(riid, &IID_IAudioSessionControl) ||
2069 IsEqualIID(riid, &IID_IAudioSessionControl2))
2072 IUnknown_AddRef((IUnknown*)*ppv);
2076 WARN("Unknown interface %s\n", debugstr_guid(riid));
2077 return E_NOINTERFACE;
2080 static ULONG WINAPI AudioSessionControl_AddRef(IAudioSessionControl2 *iface)
2082 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2084 ref = InterlockedIncrement(&This->ref);
2085 TRACE("(%p) Refcount now %u\n", This, ref);
2089 static ULONG WINAPI AudioSessionControl_Release(IAudioSessionControl2 *iface)
2091 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2093 ref = InterlockedDecrement(&This->ref);
2094 TRACE("(%p) Refcount now %u\n", This, ref);
2097 OSSpinLockLock(&This->client->lock);
2098 This->client->session_wrapper = NULL;
2099 OSSpinLockUnlock(&This->client->lock);
2100 AudioClient_Release(&This->client->IAudioClient_iface);
2102 HeapFree(GetProcessHeap(), 0, This);
2107 static HRESULT WINAPI AudioSessionControl_GetState(IAudioSessionControl2 *iface,
2108 AudioSessionState *state)
2110 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2113 TRACE("(%p)->(%p)\n", This, state);
2116 return NULL_PTR_ERR;
2118 EnterCriticalSection(&g_sessions_lock);
2120 if(list_empty(&This->session->clients)){
2121 *state = AudioSessionStateExpired;
2122 LeaveCriticalSection(&g_sessions_lock);
2126 LIST_FOR_EACH_ENTRY(client, &This->session->clients, ACImpl, entry){
2127 OSSpinLockLock(&client->lock);
2128 if(client->playing == StatePlaying ||
2129 client->playing == StateInTransition){
2130 *state = AudioSessionStateActive;
2131 OSSpinLockUnlock(&client->lock);
2132 LeaveCriticalSection(&g_sessions_lock);
2135 OSSpinLockUnlock(&client->lock);
2138 LeaveCriticalSection(&g_sessions_lock);
2140 *state = AudioSessionStateInactive;
2145 static HRESULT WINAPI AudioSessionControl_GetDisplayName(
2146 IAudioSessionControl2 *iface, WCHAR **name)
2148 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2150 FIXME("(%p)->(%p) - stub\n", This, name);
2155 static HRESULT WINAPI AudioSessionControl_SetDisplayName(
2156 IAudioSessionControl2 *iface, const WCHAR *name, const GUID *session)
2158 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2160 FIXME("(%p)->(%p, %s) - stub\n", This, name, debugstr_guid(session));
2165 static HRESULT WINAPI AudioSessionControl_GetIconPath(
2166 IAudioSessionControl2 *iface, WCHAR **path)
2168 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2170 FIXME("(%p)->(%p) - stub\n", This, path);
2175 static HRESULT WINAPI AudioSessionControl_SetIconPath(
2176 IAudioSessionControl2 *iface, const WCHAR *path, const GUID *session)
2178 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2180 FIXME("(%p)->(%p, %s) - stub\n", This, path, debugstr_guid(session));
2185 static HRESULT WINAPI AudioSessionControl_GetGroupingParam(
2186 IAudioSessionControl2 *iface, GUID *group)
2188 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2190 FIXME("(%p)->(%p) - stub\n", This, group);
2195 static HRESULT WINAPI AudioSessionControl_SetGroupingParam(
2196 IAudioSessionControl2 *iface, const GUID *group, const GUID *session)
2198 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2200 FIXME("(%p)->(%s, %s) - stub\n", This, debugstr_guid(group),
2201 debugstr_guid(session));
2206 static HRESULT WINAPI AudioSessionControl_RegisterAudioSessionNotification(
2207 IAudioSessionControl2 *iface, IAudioSessionEvents *events)
2209 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2211 FIXME("(%p)->(%p) - stub\n", This, events);
2216 static HRESULT WINAPI AudioSessionControl_UnregisterAudioSessionNotification(
2217 IAudioSessionControl2 *iface, IAudioSessionEvents *events)
2219 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2221 FIXME("(%p)->(%p) - stub\n", This, events);
2226 static HRESULT WINAPI AudioSessionControl_GetSessionIdentifier(
2227 IAudioSessionControl2 *iface, WCHAR **id)
2229 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2231 FIXME("(%p)->(%p) - stub\n", This, id);
2236 static HRESULT WINAPI AudioSessionControl_GetSessionInstanceIdentifier(
2237 IAudioSessionControl2 *iface, WCHAR **id)
2239 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2241 FIXME("(%p)->(%p) - stub\n", This, id);
2246 static HRESULT WINAPI AudioSessionControl_GetProcessId(
2247 IAudioSessionControl2 *iface, DWORD *pid)
2249 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2251 TRACE("(%p)->(%p)\n", This, pid);
2256 *pid = GetCurrentProcessId();
2261 static HRESULT WINAPI AudioSessionControl_IsSystemSoundsSession(
2262 IAudioSessionControl2 *iface)
2264 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2266 TRACE("(%p)\n", This);
2271 static HRESULT WINAPI AudioSessionControl_SetDuckingPreference(
2272 IAudioSessionControl2 *iface, BOOL optout)
2274 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2276 TRACE("(%p)->(%d)\n", This, optout);
2281 static const IAudioSessionControl2Vtbl AudioSessionControl2_Vtbl =
2283 AudioSessionControl_QueryInterface,
2284 AudioSessionControl_AddRef,
2285 AudioSessionControl_Release,
2286 AudioSessionControl_GetState,
2287 AudioSessionControl_GetDisplayName,
2288 AudioSessionControl_SetDisplayName,
2289 AudioSessionControl_GetIconPath,
2290 AudioSessionControl_SetIconPath,
2291 AudioSessionControl_GetGroupingParam,
2292 AudioSessionControl_SetGroupingParam,
2293 AudioSessionControl_RegisterAudioSessionNotification,
2294 AudioSessionControl_UnregisterAudioSessionNotification,
2295 AudioSessionControl_GetSessionIdentifier,
2296 AudioSessionControl_GetSessionInstanceIdentifier,
2297 AudioSessionControl_GetProcessId,
2298 AudioSessionControl_IsSystemSoundsSession,
2299 AudioSessionControl_SetDuckingPreference
2302 /* index == -1 means set all channels, otherwise sets only the given channel */
2303 static HRESULT ca_setvol(ACImpl *This, UINT32 index)
2308 if(index == (UINT32)-1){
2311 for(i = 0; i < This->fmt->nChannels; ++i){
2313 hr = ca_setvol(This, i);
2320 if(This->session->mute)
2323 level = This->session->master_vol *
2324 This->session->channel_vols[index] * This->vols[index];
2326 sc = AudioQueueSetParameter(This->aqueue, kAudioQueueParam_Volume, level);
2328 WARN("Setting _Volume property failed: %lx\n", sc);
2335 static HRESULT ca_session_setvol(AudioSession *session, UINT32 index)
2340 LIST_FOR_EACH_ENTRY(client, &session->clients, ACImpl, entry){
2342 hr = ca_setvol(client, index);
2350 static HRESULT WINAPI SimpleAudioVolume_QueryInterface(
2351 ISimpleAudioVolume *iface, REFIID riid, void **ppv)
2353 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2359 if(IsEqualIID(riid, &IID_IUnknown) ||
2360 IsEqualIID(riid, &IID_ISimpleAudioVolume))
2363 IUnknown_AddRef((IUnknown*)*ppv);
2367 WARN("Unknown interface %s\n", debugstr_guid(riid));
2368 return E_NOINTERFACE;
2371 static ULONG WINAPI SimpleAudioVolume_AddRef(ISimpleAudioVolume *iface)
2373 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2374 return AudioSessionControl_AddRef(&This->IAudioSessionControl2_iface);
2377 static ULONG WINAPI SimpleAudioVolume_Release(ISimpleAudioVolume *iface)
2379 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2380 return AudioSessionControl_Release(&This->IAudioSessionControl2_iface);
2383 static HRESULT WINAPI SimpleAudioVolume_SetMasterVolume(
2384 ISimpleAudioVolume *iface, float level, const GUID *context)
2386 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2387 AudioSession *session = This->session;
2390 TRACE("(%p)->(%f, %s)\n", session, level, wine_dbgstr_guid(context));
2392 if(level < 0.f || level > 1.f)
2393 return E_INVALIDARG;
2396 FIXME("Notifications not supported yet\n");
2398 EnterCriticalSection(&session->lock);
2400 session->master_vol = level;
2402 ret = ca_session_setvol(session, -1);
2404 LeaveCriticalSection(&session->lock);
2409 static HRESULT WINAPI SimpleAudioVolume_GetMasterVolume(
2410 ISimpleAudioVolume *iface, float *level)
2412 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2413 AudioSession *session = This->session;
2415 TRACE("(%p)->(%p)\n", session, level);
2418 return NULL_PTR_ERR;
2420 *level = session->master_vol;
2425 static HRESULT WINAPI SimpleAudioVolume_SetMute(ISimpleAudioVolume *iface,
2426 BOOL mute, const GUID *context)
2428 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2429 AudioSession *session = This->session;
2431 TRACE("(%p)->(%u, %p)\n", session, mute, context);
2434 FIXME("Notifications not supported yet\n");
2436 EnterCriticalSection(&session->lock);
2438 session->mute = mute;
2440 ca_session_setvol(session, -1);
2442 LeaveCriticalSection(&session->lock);
2447 static HRESULT WINAPI SimpleAudioVolume_GetMute(ISimpleAudioVolume *iface,
2450 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2451 AudioSession *session = This->session;
2453 TRACE("(%p)->(%p)\n", session, mute);
2456 return NULL_PTR_ERR;
2458 *mute = session->mute;
2463 static const ISimpleAudioVolumeVtbl SimpleAudioVolume_Vtbl =
2465 SimpleAudioVolume_QueryInterface,
2466 SimpleAudioVolume_AddRef,
2467 SimpleAudioVolume_Release,
2468 SimpleAudioVolume_SetMasterVolume,
2469 SimpleAudioVolume_GetMasterVolume,
2470 SimpleAudioVolume_SetMute,
2471 SimpleAudioVolume_GetMute
2474 static HRESULT WINAPI AudioStreamVolume_QueryInterface(
2475 IAudioStreamVolume *iface, REFIID riid, void **ppv)
2477 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2483 if(IsEqualIID(riid, &IID_IUnknown) ||
2484 IsEqualIID(riid, &IID_IAudioStreamVolume))
2487 IUnknown_AddRef((IUnknown*)*ppv);
2491 WARN("Unknown interface %s\n", debugstr_guid(riid));
2492 return E_NOINTERFACE;
2495 static ULONG WINAPI AudioStreamVolume_AddRef(IAudioStreamVolume *iface)
2497 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2498 return IAudioClient_AddRef(&This->IAudioClient_iface);
2501 static ULONG WINAPI AudioStreamVolume_Release(IAudioStreamVolume *iface)
2503 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2504 return IAudioClient_Release(&This->IAudioClient_iface);
2507 static HRESULT WINAPI AudioStreamVolume_GetChannelCount(
2508 IAudioStreamVolume *iface, UINT32 *out)
2510 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2512 TRACE("(%p)->(%p)\n", This, out);
2517 *out = This->fmt->nChannels;
2522 static HRESULT WINAPI AudioStreamVolume_SetChannelVolume(
2523 IAudioStreamVolume *iface, UINT32 index, float level)
2525 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2528 TRACE("(%p)->(%d, %f)\n", This, index, level);
2530 if(level < 0.f || level > 1.f)
2531 return E_INVALIDARG;
2533 if(index >= This->fmt->nChannels)
2534 return E_INVALIDARG;
2536 OSSpinLockLock(&This->lock);
2538 This->vols[index] = level;
2540 WARN("AudioQueue doesn't support per-channel volume control\n");
2541 ret = ca_setvol(This, index);
2543 OSSpinLockUnlock(&This->lock);
2548 static HRESULT WINAPI AudioStreamVolume_GetChannelVolume(
2549 IAudioStreamVolume *iface, UINT32 index, float *level)
2551 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2553 TRACE("(%p)->(%d, %p)\n", This, index, level);
2558 if(index >= This->fmt->nChannels)
2559 return E_INVALIDARG;
2561 *level = This->vols[index];
2566 static HRESULT WINAPI AudioStreamVolume_SetAllVolumes(
2567 IAudioStreamVolume *iface, UINT32 count, const float *levels)
2569 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2573 TRACE("(%p)->(%d, %p)\n", This, count, levels);
2578 if(count != This->fmt->nChannels)
2579 return E_INVALIDARG;
2581 OSSpinLockLock(&This->lock);
2583 for(i = 0; i < count; ++i)
2584 This->vols[i] = levels[i];
2586 ret = ca_setvol(This, -1);
2588 OSSpinLockUnlock(&This->lock);
2593 static HRESULT WINAPI AudioStreamVolume_GetAllVolumes(
2594 IAudioStreamVolume *iface, UINT32 count, float *levels)
2596 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2599 TRACE("(%p)->(%d, %p)\n", This, count, levels);
2604 if(count != This->fmt->nChannels)
2605 return E_INVALIDARG;
2607 OSSpinLockLock(&This->lock);
2609 for(i = 0; i < count; ++i)
2610 levels[i] = This->vols[i];
2612 OSSpinLockUnlock(&This->lock);
2617 static const IAudioStreamVolumeVtbl AudioStreamVolume_Vtbl =
2619 AudioStreamVolume_QueryInterface,
2620 AudioStreamVolume_AddRef,
2621 AudioStreamVolume_Release,
2622 AudioStreamVolume_GetChannelCount,
2623 AudioStreamVolume_SetChannelVolume,
2624 AudioStreamVolume_GetChannelVolume,
2625 AudioStreamVolume_SetAllVolumes,
2626 AudioStreamVolume_GetAllVolumes
2629 static HRESULT WINAPI ChannelAudioVolume_QueryInterface(
2630 IChannelAudioVolume *iface, REFIID riid, void **ppv)
2632 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2638 if(IsEqualIID(riid, &IID_IUnknown) ||
2639 IsEqualIID(riid, &IID_IChannelAudioVolume))
2642 IUnknown_AddRef((IUnknown*)*ppv);
2646 WARN("Unknown interface %s\n", debugstr_guid(riid));
2647 return E_NOINTERFACE;
2650 static ULONG WINAPI ChannelAudioVolume_AddRef(IChannelAudioVolume *iface)
2652 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2653 return AudioSessionControl_AddRef(&This->IAudioSessionControl2_iface);
2656 static ULONG WINAPI ChannelAudioVolume_Release(IChannelAudioVolume *iface)
2658 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2659 return AudioSessionControl_Release(&This->IAudioSessionControl2_iface);
2662 static HRESULT WINAPI ChannelAudioVolume_GetChannelCount(
2663 IChannelAudioVolume *iface, UINT32 *out)
2665 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2666 AudioSession *session = This->session;
2668 TRACE("(%p)->(%p)\n", session, out);
2671 return NULL_PTR_ERR;
2673 *out = session->channel_count;
2678 static HRESULT WINAPI ChannelAudioVolume_SetChannelVolume(
2679 IChannelAudioVolume *iface, UINT32 index, float level,
2680 const GUID *context)
2682 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2683 AudioSession *session = This->session;
2686 TRACE("(%p)->(%d, %f, %s)\n", session, index, level,
2687 wine_dbgstr_guid(context));
2689 if(level < 0.f || level > 1.f)
2690 return E_INVALIDARG;
2692 if(index >= session->channel_count)
2693 return E_INVALIDARG;
2696 FIXME("Notifications not supported yet\n");
2698 EnterCriticalSection(&session->lock);
2700 session->channel_vols[index] = level;
2702 WARN("AudioQueue doesn't support per-channel volume control\n");
2703 ret = ca_session_setvol(session, index);
2705 LeaveCriticalSection(&session->lock);
2710 static HRESULT WINAPI ChannelAudioVolume_GetChannelVolume(
2711 IChannelAudioVolume *iface, UINT32 index, float *level)
2713 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2714 AudioSession *session = This->session;
2716 TRACE("(%p)->(%d, %p)\n", session, index, level);
2719 return NULL_PTR_ERR;
2721 if(index >= session->channel_count)
2722 return E_INVALIDARG;
2724 *level = session->channel_vols[index];
2729 static HRESULT WINAPI ChannelAudioVolume_SetAllVolumes(
2730 IChannelAudioVolume *iface, UINT32 count, const float *levels,
2731 const GUID *context)
2733 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2734 AudioSession *session = This->session;
2738 TRACE("(%p)->(%d, %p, %s)\n", session, count, levels,
2739 wine_dbgstr_guid(context));
2742 return NULL_PTR_ERR;
2744 if(count != session->channel_count)
2745 return E_INVALIDARG;
2748 FIXME("Notifications not supported yet\n");
2750 EnterCriticalSection(&session->lock);
2752 for(i = 0; i < count; ++i)
2753 session->channel_vols[i] = levels[i];
2755 ret = ca_session_setvol(session, -1);
2757 LeaveCriticalSection(&session->lock);
2762 static HRESULT WINAPI ChannelAudioVolume_GetAllVolumes(
2763 IChannelAudioVolume *iface, UINT32 count, float *levels)
2765 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2766 AudioSession *session = This->session;
2769 TRACE("(%p)->(%d, %p)\n", session, count, levels);
2772 return NULL_PTR_ERR;
2774 if(count != session->channel_count)
2775 return E_INVALIDARG;
2777 for(i = 0; i < count; ++i)
2778 levels[i] = session->channel_vols[i];
2783 static const IChannelAudioVolumeVtbl ChannelAudioVolume_Vtbl =
2785 ChannelAudioVolume_QueryInterface,
2786 ChannelAudioVolume_AddRef,
2787 ChannelAudioVolume_Release,
2788 ChannelAudioVolume_GetChannelCount,
2789 ChannelAudioVolume_SetChannelVolume,
2790 ChannelAudioVolume_GetChannelVolume,
2791 ChannelAudioVolume_SetAllVolumes,
2792 ChannelAudioVolume_GetAllVolumes
2795 static HRESULT WINAPI AudioSessionManager_QueryInterface(IAudioSessionManager2 *iface,
2796 REFIID riid, void **ppv)
2798 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2804 if(IsEqualIID(riid, &IID_IUnknown) ||
2805 IsEqualIID(riid, &IID_IAudioSessionManager) ||
2806 IsEqualIID(riid, &IID_IAudioSessionManager2))
2809 IUnknown_AddRef((IUnknown*)*ppv);
2813 WARN("Unknown interface %s\n", debugstr_guid(riid));
2814 return E_NOINTERFACE;
2817 static ULONG WINAPI AudioSessionManager_AddRef(IAudioSessionManager2 *iface)
2819 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
2821 ref = InterlockedIncrement(&This->ref);
2822 TRACE("(%p) Refcount now %u\n", This, ref);
2826 static ULONG WINAPI AudioSessionManager_Release(IAudioSessionManager2 *iface)
2828 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
2830 ref = InterlockedDecrement(&This->ref);
2831 TRACE("(%p) Refcount now %u\n", This, ref);
2833 HeapFree(GetProcessHeap(), 0, This);
2837 static HRESULT WINAPI AudioSessionManager_GetAudioSessionControl(
2838 IAudioSessionManager2 *iface, const GUID *session_guid, DWORD flags,
2839 IAudioSessionControl **out)
2841 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
2842 AudioSession *session;
2843 AudioSessionWrapper *wrapper;
2846 TRACE("(%p)->(%s, %x, %p)\n", This, debugstr_guid(session_guid),
2849 hr = get_audio_session(session_guid, This->device, 0, &session);
2853 wrapper = AudioSessionWrapper_Create(NULL);
2855 return E_OUTOFMEMORY;
2857 wrapper->session = session;
2859 *out = (IAudioSessionControl*)&wrapper->IAudioSessionControl2_iface;
2864 static HRESULT WINAPI AudioSessionManager_GetSimpleAudioVolume(
2865 IAudioSessionManager2 *iface, const GUID *session_guid, DWORD flags,
2866 ISimpleAudioVolume **out)
2868 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
2869 AudioSession *session;
2870 AudioSessionWrapper *wrapper;
2873 TRACE("(%p)->(%s, %x, %p)\n", This, debugstr_guid(session_guid),
2876 hr = get_audio_session(session_guid, This->device, 0, &session);
2880 wrapper = AudioSessionWrapper_Create(NULL);
2882 return E_OUTOFMEMORY;
2884 wrapper->session = session;
2886 *out = &wrapper->ISimpleAudioVolume_iface;
2891 static HRESULT WINAPI AudioSessionManager_GetSessionEnumerator(
2892 IAudioSessionManager2 *iface, IAudioSessionEnumerator **out)
2894 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
2895 FIXME("(%p)->(%p) - stub\n", This, out);
2899 static HRESULT WINAPI AudioSessionManager_RegisterSessionNotification(
2900 IAudioSessionManager2 *iface, IAudioSessionNotification *notification)
2902 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
2903 FIXME("(%p)->(%p) - stub\n", This, notification);
2907 static HRESULT WINAPI AudioSessionManager_UnregisterSessionNotification(
2908 IAudioSessionManager2 *iface, IAudioSessionNotification *notification)
2910 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
2911 FIXME("(%p)->(%p) - stub\n", This, notification);
2915 static HRESULT WINAPI AudioSessionManager_RegisterDuckNotification(
2916 IAudioSessionManager2 *iface, const WCHAR *session_id,
2917 IAudioVolumeDuckNotification *notification)
2919 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
2920 FIXME("(%p)->(%p) - stub\n", This, notification);
2924 static HRESULT WINAPI AudioSessionManager_UnregisterDuckNotification(
2925 IAudioSessionManager2 *iface,
2926 IAudioVolumeDuckNotification *notification)
2928 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
2929 FIXME("(%p)->(%p) - stub\n", This, notification);
2933 static const IAudioSessionManager2Vtbl AudioSessionManager2_Vtbl =
2935 AudioSessionManager_QueryInterface,
2936 AudioSessionManager_AddRef,
2937 AudioSessionManager_Release,
2938 AudioSessionManager_GetAudioSessionControl,
2939 AudioSessionManager_GetSimpleAudioVolume,
2940 AudioSessionManager_GetSessionEnumerator,
2941 AudioSessionManager_RegisterSessionNotification,
2942 AudioSessionManager_UnregisterSessionNotification,
2943 AudioSessionManager_RegisterDuckNotification,
2944 AudioSessionManager_UnregisterDuckNotification
2947 HRESULT WINAPI AUDDRV_GetAudioSessionManager(IMMDevice *device,
2948 IAudioSessionManager2 **out)
2952 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(SessionMgr));
2954 return E_OUTOFMEMORY;
2956 This->IAudioSessionManager2_iface.lpVtbl = &AudioSessionManager2_Vtbl;
2957 This->device = device;
2960 *out = &This->IAudioSessionManager2_iface;