winecoreaudio.drv: Make driver sample accurate.
[wine/multimedia.git] / dlls / winecoreaudio.drv / mmdevdrv.c
blobae85f37d92a950fde2729115e02627d774d92580
1 /*
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
20 #define COBJMACROS
21 #include "config.h"
23 #include <stdarg.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winnls.h"
28 #include "winreg.h"
29 #include "wine/debug.h"
30 #include "wine/unicode.h"
31 #include "wine/list.h"
33 #include "ole2.h"
34 #include "mmdeviceapi.h"
35 #include "devpkey.h"
36 #include "dshow.h"
37 #include "dsound.h"
39 #include "initguid.h"
40 #include "endpointvolume.h"
41 #include "audioclient.h"
42 #include "audiopolicy.h"
44 #include <errno.h>
45 #include <limits.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <sys/types.h>
50 #include <sys/stat.h>
51 #include <sys/ioctl.h>
52 #include <fcntl.h>
53 #include <unistd.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 _QueuedBufInfo {
69 Float64 start_sampletime;
70 UINT64 start_pos;
71 UINT32 len_frames;
72 struct list entry;
73 } QueuedBufInfo;
75 typedef struct _AQBuffer {
76 AudioQueueBufferRef buf;
77 struct list entry;
78 } AQBuffer;
80 struct ACImpl;
81 typedef struct ACImpl ACImpl;
83 typedef struct _AudioSession {
84 GUID guid;
85 struct list clients;
87 IMMDevice *device;
89 float master_vol;
90 UINT32 channel_count;
91 float *channel_vols;
92 BOOL mute;
94 CRITICAL_SECTION lock;
96 struct list entry;
97 } AudioSession;
99 typedef struct _AudioSessionWrapper {
100 IAudioSessionControl2 IAudioSessionControl2_iface;
101 IChannelAudioVolume IChannelAudioVolume_iface;
102 ISimpleAudioVolume ISimpleAudioVolume_iface;
104 LONG ref;
106 ACImpl *client;
107 AudioSession *session;
108 } AudioSessionWrapper;
110 struct ACImpl {
111 IAudioClient IAudioClient_iface;
112 IAudioRenderClient IAudioRenderClient_iface;
113 IAudioCaptureClient IAudioCaptureClient_iface;
114 IAudioClock IAudioClock_iface;
115 IAudioClock2 IAudioClock2_iface;
116 IAudioStreamVolume IAudioStreamVolume_iface;
118 LONG ref;
120 IMMDevice *parent;
122 WAVEFORMATEX *fmt;
124 EDataFlow dataflow;
125 DWORD flags;
126 AUDCLNT_SHAREMODE share;
127 HANDLE event;
128 float *vols;
130 AudioDeviceID adevid;
131 AudioQueueRef aqueue;
132 AudioObjectPropertyScope scope;
133 HANDLE timer;
134 UINT32 period_ms, bufsize_frames, inbuf_frames;
135 UINT64 last_time, written_frames;
136 AudioQueueBufferRef public_buffer;
137 UINT32 getbuf_last;
138 int playing;
140 Float64 highest_sampletime;
142 AudioSession *session;
143 AudioSessionWrapper *session_wrapper;
145 struct list entry;
147 struct list avail_buffers;
148 struct list queued_bufinfos;
150 /* We can't use debug printing or {Enter,Leave}CriticalSection from
151 * OSX callback threads, so we use OSX's OSSpinLock for synchronization
152 * instead. OSSpinLock is not a recursive lock, so don't call
153 * synchronized functions while holding the lock. */
154 OSSpinLock lock;
157 enum PlayingStates {
158 StateStopped = 0,
159 StatePlaying,
160 StateInTransition
163 static const IAudioClientVtbl AudioClient_Vtbl;
164 static const IAudioRenderClientVtbl AudioRenderClient_Vtbl;
165 static const IAudioCaptureClientVtbl AudioCaptureClient_Vtbl;
166 static const IAudioSessionControl2Vtbl AudioSessionControl2_Vtbl;
167 static const ISimpleAudioVolumeVtbl SimpleAudioVolume_Vtbl;
168 static const IAudioClockVtbl AudioClock_Vtbl;
169 static const IAudioClock2Vtbl AudioClock2_Vtbl;
170 static const IAudioStreamVolumeVtbl AudioStreamVolume_Vtbl;
171 static const IChannelAudioVolumeVtbl ChannelAudioVolume_Vtbl;
172 static const IAudioSessionManager2Vtbl AudioSessionManager2_Vtbl;
174 typedef struct _SessionMgr {
175 IAudioSessionManager2 IAudioSessionManager2_iface;
177 LONG ref;
179 IMMDevice *device;
180 } SessionMgr;
182 static HANDLE g_timer_q;
184 static CRITICAL_SECTION g_sessions_lock;
185 static CRITICAL_SECTION_DEBUG g_sessions_lock_debug =
187 0, 0, &g_sessions_lock,
188 { &g_sessions_lock_debug.ProcessLocksList, &g_sessions_lock_debug.ProcessLocksList },
189 0, 0, { (DWORD_PTR)(__FILE__ ": g_sessions_lock") }
191 static CRITICAL_SECTION g_sessions_lock = { &g_sessions_lock_debug, -1, 0, 0, 0, 0 };
192 static struct list g_sessions = LIST_INIT(g_sessions);
194 static HRESULT AudioClock_GetPosition_nolock(ACImpl *This, UINT64 *pos,
195 UINT64 *qpctime);
196 static AudioSessionWrapper *AudioSessionWrapper_Create(ACImpl *client);
197 static HRESULT ca_setvol(ACImpl *This, UINT32 index);
199 static inline ACImpl *impl_from_IAudioClient(IAudioClient *iface)
201 return CONTAINING_RECORD(iface, ACImpl, IAudioClient_iface);
204 static inline ACImpl *impl_from_IAudioRenderClient(IAudioRenderClient *iface)
206 return CONTAINING_RECORD(iface, ACImpl, IAudioRenderClient_iface);
209 static inline ACImpl *impl_from_IAudioCaptureClient(IAudioCaptureClient *iface)
211 return CONTAINING_RECORD(iface, ACImpl, IAudioCaptureClient_iface);
214 static inline AudioSessionWrapper *impl_from_IAudioSessionControl2(IAudioSessionControl2 *iface)
216 return CONTAINING_RECORD(iface, AudioSessionWrapper, IAudioSessionControl2_iface);
219 static inline AudioSessionWrapper *impl_from_ISimpleAudioVolume(ISimpleAudioVolume *iface)
221 return CONTAINING_RECORD(iface, AudioSessionWrapper, ISimpleAudioVolume_iface);
224 static inline AudioSessionWrapper *impl_from_IChannelAudioVolume(IChannelAudioVolume *iface)
226 return CONTAINING_RECORD(iface, AudioSessionWrapper, IChannelAudioVolume_iface);
229 static inline ACImpl *impl_from_IAudioClock(IAudioClock *iface)
231 return CONTAINING_RECORD(iface, ACImpl, IAudioClock_iface);
234 static inline ACImpl *impl_from_IAudioClock2(IAudioClock2 *iface)
236 return CONTAINING_RECORD(iface, ACImpl, IAudioClock2_iface);
239 static inline ACImpl *impl_from_IAudioStreamVolume(IAudioStreamVolume *iface)
241 return CONTAINING_RECORD(iface, ACImpl, IAudioStreamVolume_iface);
244 static inline SessionMgr *impl_from_IAudioSessionManager2(IAudioSessionManager2 *iface)
246 return CONTAINING_RECORD(iface, SessionMgr, IAudioSessionManager2_iface);
249 BOOL WINAPI DllMain(HINSTANCE dll, DWORD reason, void *reserved)
251 switch (reason)
253 case DLL_PROCESS_ATTACH:
254 g_timer_q = CreateTimerQueue();
255 if(!g_timer_q)
256 return FALSE;
257 break;
259 case DLL_PROCESS_DETACH:
260 DeleteCriticalSection(&g_sessions_lock);
261 break;
263 return TRUE;
266 /* From <dlls/mmdevapi/mmdevapi.h> */
267 enum DriverPriority {
268 Priority_Unavailable = 0,
269 Priority_Low,
270 Priority_Neutral,
271 Priority_Preferred
274 int WINAPI AUDDRV_GetPriority(void)
276 return Priority_Neutral;
279 HRESULT WINAPI AUDDRV_GetEndpointIDs(EDataFlow flow, WCHAR ***ids,
280 AudioDeviceID ***keys, UINT *num, UINT *def_index)
282 UInt32 devsize, size;
283 AudioDeviceID *devices;
284 AudioDeviceID default_id;
285 AudioObjectPropertyAddress addr;
286 OSStatus sc;
287 int i, ndevices;
289 TRACE("%d %p %p %p\n", flow, ids, num, def_index);
291 addr.mScope = kAudioObjectPropertyScopeGlobal;
292 addr.mElement = kAudioObjectPropertyElementMaster;
293 if(flow == eRender)
294 addr.mSelector = kAudioHardwarePropertyDefaultOutputDevice;
295 else if(flow == eCapture)
296 addr.mSelector = kAudioHardwarePropertyDefaultInputDevice;
297 else
298 return E_INVALIDARG;
300 size = sizeof(default_id);
301 sc = AudioObjectGetPropertyData(kAudioObjectSystemObject, &addr, 0,
302 NULL, &size, &default_id);
303 if(sc != noErr){
304 WARN("Getting _DefaultInputDevice property failed: %lx\n", sc);
305 default_id = -1;
308 addr.mSelector = kAudioHardwarePropertyDevices;
309 sc = AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &addr, 0,
310 NULL, &devsize);
311 if(sc != noErr){
312 WARN("Getting _Devices property size failed: %lx\n", sc);
313 return E_FAIL;
316 devices = HeapAlloc(GetProcessHeap(), 0, devsize);
317 if(!devices)
318 return E_OUTOFMEMORY;
320 sc = AudioObjectGetPropertyData(kAudioObjectSystemObject, &addr, 0, NULL,
321 &devsize, devices);
322 if(sc != noErr){
323 WARN("Getting _Devices property failed: %lx\n", sc);
324 HeapFree(GetProcessHeap(), 0, devices);
325 return E_FAIL;
328 ndevices = devsize / sizeof(AudioDeviceID);
330 *ids = HeapAlloc(GetProcessHeap(), 0, ndevices * sizeof(WCHAR *));
331 if(!*ids){
332 HeapFree(GetProcessHeap(), 0, devices);
333 return E_OUTOFMEMORY;
336 *keys = HeapAlloc(GetProcessHeap(), 0, ndevices * sizeof(AudioDeviceID *));
337 if(!*ids){
338 HeapFree(GetProcessHeap(), 0, *ids);
339 HeapFree(GetProcessHeap(), 0, devices);
340 return E_OUTOFMEMORY;
343 *num = 0;
344 *def_index = (UINT)-1;
345 for(i = 0; i < ndevices; ++i){
346 AudioBufferList *buffers;
347 CFStringRef name;
348 SIZE_T len;
349 int j;
351 addr.mSelector = kAudioDevicePropertyStreamConfiguration;
352 if(flow == eRender)
353 addr.mScope = kAudioDevicePropertyScopeOutput;
354 else
355 addr.mScope = kAudioDevicePropertyScopeInput;
356 addr.mElement = 0;
357 sc = AudioObjectGetPropertyDataSize(devices[i], &addr, 0, NULL, &size);
358 if(sc != noErr){
359 WARN("Unable to get _StreamConfiguration property size for "
360 "device %lu: %lx\n", devices[i], sc);
361 continue;
364 buffers = HeapAlloc(GetProcessHeap(), 0, size);
365 if(!buffers){
366 HeapFree(GetProcessHeap(), 0, devices);
367 for(j = 0; j < *num; ++j){
368 HeapFree(GetProcessHeap(), 0, (*ids)[j]);
369 HeapFree(GetProcessHeap(), 0, (*keys)[j]);
371 HeapFree(GetProcessHeap(), 0, *keys);
372 HeapFree(GetProcessHeap(), 0, *ids);
373 return E_OUTOFMEMORY;
376 sc = AudioObjectGetPropertyData(devices[i], &addr, 0, NULL,
377 &size, buffers);
378 if(sc != noErr){
379 WARN("Unable to get _StreamConfiguration property for "
380 "device %lu: %lx\n", devices[i], sc);
381 HeapFree(GetProcessHeap(), 0, buffers);
382 continue;
385 /* check that there's at least one channel in this device before
386 * we claim it as usable */
387 for(j = 0; j < buffers->mNumberBuffers; ++j)
388 if(buffers->mBuffers[j].mNumberChannels > 0)
389 break;
390 if(j >= buffers->mNumberBuffers){
391 HeapFree(GetProcessHeap(), 0, buffers);
392 continue;
395 HeapFree(GetProcessHeap(), 0, buffers);
397 size = sizeof(name);
398 addr.mSelector = kAudioObjectPropertyName;
399 sc = AudioObjectGetPropertyData(devices[i], &addr, 0, NULL,
400 &size, &name);
401 if(sc != noErr){
402 WARN("Unable to get _Name property for device %lu: %lx\n",
403 devices[i], sc);
404 continue;
407 len = CFStringGetLength(name) + 1;
408 (*ids)[*num] = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
409 if(!(*ids)[*num]){
410 CFRelease(name);
411 HeapFree(GetProcessHeap(), 0, devices);
412 for(j = 0; j < *num; ++j){
413 HeapFree(GetProcessHeap(), 0, (*ids)[j]);
414 HeapFree(GetProcessHeap(), 0, (*keys)[j]);
416 HeapFree(GetProcessHeap(), 0, *ids);
417 HeapFree(GetProcessHeap(), 0, *keys);
418 return E_OUTOFMEMORY;
420 CFStringGetCharacters(name, CFRangeMake(0, len - 1), (UniChar*)(*ids)[*num]);
421 ((*ids)[*num])[len - 1] = 0;
422 CFRelease(name);
424 (*keys)[*num] = HeapAlloc(GetProcessHeap(), 0, sizeof(AudioDeviceID));
425 if(!(*keys)[*num]){
426 HeapFree(GetProcessHeap(), 0, devices);
427 HeapFree(GetProcessHeap(), 0, (*ids)[*num]);
428 for(j = 0; j < *num; ++j){
429 HeapFree(GetProcessHeap(), 0, (*ids)[j]);
430 HeapFree(GetProcessHeap(), 0, (*keys)[j]);
432 HeapFree(GetProcessHeap(), 0, *ids);
433 HeapFree(GetProcessHeap(), 0, *keys);
434 return E_OUTOFMEMORY;
436 *(*keys)[*num] = devices[i];
438 if(*def_index == (UINT)-1 && devices[i] == default_id)
439 *def_index = *num;
441 TRACE("device %u: id %s key %u%s\n", *num, debugstr_w((*ids)[*num]),
442 (unsigned int)*(*keys)[*num], (*def_index == *num) ? " (default)" : "");
444 (*num)++;
447 if(*def_index == (UINT)-1)
448 *def_index = 0;
450 HeapFree(GetProcessHeap(), 0, devices);
452 return S_OK;
455 HRESULT WINAPI AUDDRV_GetAudioEndpoint(AudioDeviceID *adevid, IMMDevice *dev,
456 EDataFlow dataflow, IAudioClient **out)
458 ACImpl *This;
460 TRACE("%u %p %d %p\n", (unsigned int)*adevid, dev, dataflow, out);
462 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ACImpl));
463 if(!This)
464 return E_OUTOFMEMORY;
466 This->IAudioClient_iface.lpVtbl = &AudioClient_Vtbl;
467 This->IAudioRenderClient_iface.lpVtbl = &AudioRenderClient_Vtbl;
468 This->IAudioCaptureClient_iface.lpVtbl = &AudioCaptureClient_Vtbl;
469 This->IAudioClock_iface.lpVtbl = &AudioClock_Vtbl;
470 This->IAudioClock2_iface.lpVtbl = &AudioClock2_Vtbl;
471 This->IAudioStreamVolume_iface.lpVtbl = &AudioStreamVolume_Vtbl;
473 This->dataflow = dataflow;
475 if(dataflow == eRender)
476 This->scope = kAudioDevicePropertyScopeOutput;
477 else if(dataflow == eCapture)
478 This->scope = kAudioDevicePropertyScopeInput;
479 else{
480 HeapFree(GetProcessHeap(), 0, This);
481 return E_INVALIDARG;
484 This->lock = 0;
486 This->parent = dev;
487 IMMDevice_AddRef(This->parent);
489 list_init(&This->avail_buffers);
490 list_init(&This->queued_bufinfos);
492 This->adevid = *adevid;
494 *out = &This->IAudioClient_iface;
495 IAudioClient_AddRef(&This->IAudioClient_iface);
497 return S_OK;
500 /* current position from start of stream */
501 #define BUFPOS_ABSOLUTE 1
502 /* current position from start of this buffer */
503 #define BUFPOS_RELATIVE 2
505 static UINT64 get_current_aqbuffer_position(ACImpl *This, int mode)
507 struct list *head;
508 QueuedBufInfo *bufinfo;
509 UINT64 ret;
511 head = list_head(&This->queued_bufinfos);
512 if(!head){
513 TRACE("No buffers queued\n");
514 if(mode == BUFPOS_ABSOLUTE)
515 return This->written_frames;
516 return 0;
518 bufinfo = LIST_ENTRY(head, QueuedBufInfo, entry);
520 if(This->playing == StatePlaying){
521 AudioTimeStamp tstamp;
522 OSStatus sc;
524 /* AudioQueueGetCurrentTime() is brain damaged. The returned
525 * mSampleTime member jumps backwards seemingly at random, so
526 * we record the highest sampletime and use that during these
527 * anomalies.
529 * It also behaves poorly when the queue is paused, jumping
530 * forwards during the pause and backwards again after resuming.
531 * So we record the sampletime when the queue is paused and use
532 * that. */
533 sc = AudioQueueGetCurrentTime(This->aqueue, NULL, &tstamp, NULL);
534 if(sc != noErr){
535 if(sc != kAudioQueueErr_InvalidRunState)
536 WARN("Unable to get current time: %lx\n", sc);
537 return 0;
540 if(!(tstamp.mFlags & kAudioTimeStampSampleTimeValid)){
541 FIXME("SampleTime not valid: %lx\n", tstamp.mFlags);
542 return 0;
545 if(tstamp.mSampleTime > This->highest_sampletime)
546 This->highest_sampletime = tstamp.mSampleTime;
549 while(This->highest_sampletime > bufinfo->start_sampletime + bufinfo->len_frames){
550 This->inbuf_frames -= bufinfo->len_frames;
551 list_remove(&bufinfo->entry);
552 HeapFree(GetProcessHeap(), 0, bufinfo);
554 head = list_head(&This->queued_bufinfos);
555 if(!head){
556 TRACE("No buffers queued\n");
557 if(mode == BUFPOS_ABSOLUTE)
558 return This->written_frames;
559 return 0;
561 bufinfo = LIST_ENTRY(head, QueuedBufInfo, entry);
564 if(This->highest_sampletime < bufinfo->start_sampletime)
565 ret = 0;
566 else
567 ret = This->highest_sampletime - bufinfo->start_sampletime;
569 if(mode == BUFPOS_ABSOLUTE)
570 ret += bufinfo->start_pos;
572 TRACE("%llu frames (%s)\n", ret,
573 mode == BUFPOS_ABSOLUTE ? "absolute" : "relative");
575 return ret;
578 static HRESULT WINAPI AudioClient_QueryInterface(IAudioClient *iface,
579 REFIID riid, void **ppv)
581 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
583 if(!ppv)
584 return E_POINTER;
585 *ppv = NULL;
586 if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IAudioClient))
587 *ppv = iface;
588 if(*ppv){
589 IUnknown_AddRef((IUnknown*)*ppv);
590 return S_OK;
592 WARN("Unknown interface %s\n", debugstr_guid(riid));
593 return E_NOINTERFACE;
596 static ULONG WINAPI AudioClient_AddRef(IAudioClient *iface)
598 ACImpl *This = impl_from_IAudioClient(iface);
599 ULONG ref;
600 ref = InterlockedIncrement(&This->ref);
601 TRACE("(%p) Refcount now %u\n", This, ref);
602 return ref;
605 static ULONG WINAPI AudioClient_Release(IAudioClient *iface)
607 ACImpl *This = impl_from_IAudioClient(iface);
608 ULONG ref;
609 ref = InterlockedDecrement(&This->ref);
610 TRACE("(%p) Refcount now %u\n", This, ref);
611 if(!ref){
612 if(This->aqueue){
613 AQBuffer *buf, *next;
614 QueuedBufInfo *bufinfo, *bufinfo2;
616 if(This->public_buffer){
617 buf = This->public_buffer->mUserData;
618 list_add_tail(&This->avail_buffers, &buf->entry);
621 IAudioClient_Stop(iface);
622 AudioQueueStop(This->aqueue, 1);
624 /* Stopped synchronously, all buffers returned. */
625 LIST_FOR_EACH_ENTRY_SAFE(buf, next, &This->avail_buffers, AQBuffer, entry){
626 AudioQueueFreeBuffer(This->aqueue, buf->buf);
627 HeapFree(GetProcessHeap(), 0, buf);
630 LIST_FOR_EACH_ENTRY_SAFE(bufinfo, bufinfo2, &This->queued_bufinfos,
631 QueuedBufInfo, entry)
632 HeapFree(GetProcessHeap(), 0, bufinfo);
634 AudioQueueDispose(This->aqueue, 1);
636 if(This->session){
637 EnterCriticalSection(&g_sessions_lock);
638 list_remove(&This->entry);
639 LeaveCriticalSection(&g_sessions_lock);
641 HeapFree(GetProcessHeap(), 0, This->vols);
642 CoTaskMemFree(This->fmt);
643 IMMDevice_Release(This->parent);
644 HeapFree(GetProcessHeap(), 0, This);
646 return ref;
649 static void dump_fmt(const WAVEFORMATEX *fmt)
651 TRACE("wFormatTag: 0x%x (", fmt->wFormatTag);
652 switch(fmt->wFormatTag){
653 case WAVE_FORMAT_PCM:
654 TRACE("WAVE_FORMAT_PCM");
655 break;
656 case WAVE_FORMAT_IEEE_FLOAT:
657 TRACE("WAVE_FORMAT_IEEE_FLOAT");
658 break;
659 case WAVE_FORMAT_EXTENSIBLE:
660 TRACE("WAVE_FORMAT_EXTENSIBLE");
661 break;
662 default:
663 TRACE("Unknown");
664 break;
666 TRACE(")\n");
668 TRACE("nChannels: %u\n", fmt->nChannels);
669 TRACE("nSamplesPerSec: %u\n", fmt->nSamplesPerSec);
670 TRACE("nAvgBytesPerSec: %u\n", fmt->nAvgBytesPerSec);
671 TRACE("nBlockAlign: %u\n", fmt->nBlockAlign);
672 TRACE("wBitsPerSample: %u\n", fmt->wBitsPerSample);
673 TRACE("cbSize: %u\n", fmt->cbSize);
675 if(fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE){
676 WAVEFORMATEXTENSIBLE *fmtex = (void*)fmt;
677 TRACE("dwChannelMask: %08x\n", fmtex->dwChannelMask);
678 TRACE("Samples: %04x\n", fmtex->Samples.wReserved);
679 TRACE("SubFormat: %s\n", wine_dbgstr_guid(&fmtex->SubFormat));
683 static DWORD get_channel_mask(unsigned int channels)
685 switch(channels){
686 case 0:
687 return 0;
688 case 1:
689 return KSAUDIO_SPEAKER_MONO;
690 case 2:
691 return KSAUDIO_SPEAKER_STEREO;
692 case 3:
693 return KSAUDIO_SPEAKER_STEREO | SPEAKER_LOW_FREQUENCY;
694 case 4:
695 return KSAUDIO_SPEAKER_QUAD; /* not _SURROUND */
696 case 5:
697 return KSAUDIO_SPEAKER_QUAD | SPEAKER_LOW_FREQUENCY;
698 case 6:
699 return KSAUDIO_SPEAKER_5POINT1; /* not 5POINT1_SURROUND */
700 case 7:
701 return KSAUDIO_SPEAKER_5POINT1 | SPEAKER_BACK_CENTER;
702 case 8:
703 return KSAUDIO_SPEAKER_7POINT1; /* not 7POINT1_SURROUND */
705 FIXME("Unknown speaker configuration: %u\n", channels);
706 return 0;
709 static WAVEFORMATEX *clone_format(const WAVEFORMATEX *fmt)
711 WAVEFORMATEX *ret;
712 size_t size;
714 if(fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
715 size = sizeof(WAVEFORMATEXTENSIBLE);
716 else
717 size = sizeof(WAVEFORMATEX);
719 ret = CoTaskMemAlloc(size);
720 if(!ret)
721 return NULL;
723 memcpy(ret, fmt, size);
725 ret->cbSize = size - sizeof(WAVEFORMATEX);
727 return ret;
730 static HRESULT ca_get_audiodesc(AudioStreamBasicDescription *desc,
731 const WAVEFORMATEX *fmt)
733 const WAVEFORMATEXTENSIBLE *fmtex = (const WAVEFORMATEXTENSIBLE *)fmt;
735 desc->mFormatFlags = 0;
737 if(fmt->wFormatTag == WAVE_FORMAT_PCM ||
738 (fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
739 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM))){
740 desc->mFormatID = kAudioFormatLinearPCM;
741 if(fmt->wBitsPerSample > 8)
742 desc->mFormatFlags = kAudioFormatFlagIsSignedInteger;
743 }else if(fmt->wFormatTag == WAVE_FORMAT_IEEE_FLOAT ||
744 (fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
745 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT))){
746 desc->mFormatID = kAudioFormatLinearPCM;
747 desc->mFormatFlags = kAudioFormatFlagIsFloat;
748 }else if(fmt->wFormatTag == WAVE_FORMAT_MULAW ||
749 (fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
750 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_MULAW))){
751 desc->mFormatID = kAudioFormatULaw;
752 }else if(fmt->wFormatTag == WAVE_FORMAT_ALAW ||
753 (fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
754 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_ALAW))){
755 desc->mFormatID = kAudioFormatALaw;
756 }else
757 return AUDCLNT_E_UNSUPPORTED_FORMAT;
759 desc->mSampleRate = fmt->nSamplesPerSec;
760 desc->mBytesPerPacket = fmt->nBlockAlign;
761 desc->mFramesPerPacket = 1;
762 desc->mBytesPerFrame = fmt->nBlockAlign;
763 desc->mChannelsPerFrame = fmt->nChannels;
764 desc->mBitsPerChannel = fmt->wBitsPerSample;
765 desc->mReserved = 0;
767 return S_OK;
770 static void ca_out_buffer_cb(void *user, AudioQueueRef aqueue,
771 AudioQueueBufferRef buffer)
773 ACImpl *This = user;
774 AQBuffer *buf = buffer->mUserData;
776 OSSpinLockLock(&This->lock);
777 list_add_tail(&This->avail_buffers, &buf->entry);
778 OSSpinLockUnlock(&This->lock);
781 static void ca_in_buffer_cb(void *user, AudioQueueRef aqueue,
782 AudioQueueBufferRef buffer, const AudioTimeStamp *start,
783 UInt32 ndesc, const AudioStreamPacketDescription *descs)
785 ACImpl *This = user;
786 AQBuffer *buf = buffer->mUserData;
788 OSSpinLockLock(&This->lock);
789 list_add_tail(&This->avail_buffers, &buf->entry);
790 This->inbuf_frames += buffer->mAudioDataByteSize / This->fmt->nBlockAlign;
791 OSSpinLockUnlock(&This->lock);
794 static HRESULT ca_setup_aqueue(AudioDeviceID did, EDataFlow flow,
795 const WAVEFORMATEX *fmt, void *user, AudioQueueRef *aqueue)
797 AudioStreamBasicDescription desc;
798 AudioObjectPropertyAddress addr;
799 CFStringRef uid;
800 OSStatus sc;
801 HRESULT hr;
802 UInt32 size;
804 addr.mScope = kAudioObjectPropertyScopeGlobal;
805 addr.mElement = 0;
806 addr.mSelector = kAudioDevicePropertyDeviceUID;
808 size = sizeof(uid);
809 sc = AudioObjectGetPropertyData(did, &addr, 0, NULL, &size, &uid);
810 if(sc != noErr){
811 WARN("Unable to get _DeviceUID property: %lx\n", sc);
812 return E_FAIL;
815 hr = ca_get_audiodesc(&desc, fmt);
816 if(FAILED(hr)){
817 CFRelease(uid);
818 return hr;
821 if(flow == eRender)
822 sc = AudioQueueNewOutput(&desc, ca_out_buffer_cb, user, NULL, NULL, 0,
823 aqueue);
824 else if(flow == eCapture)
825 sc = AudioQueueNewInput(&desc, ca_in_buffer_cb, user, NULL, NULL, 0,
826 aqueue);
827 else{
828 CFRelease(uid);
829 return E_UNEXPECTED;
831 if(sc != noErr){
832 WARN("Unable to create AudioQueue: %lx\n", sc);
833 CFRelease(uid);
834 return E_FAIL;
837 sc = AudioQueueSetProperty(*aqueue, kAudioQueueProperty_CurrentDevice,
838 &uid, sizeof(uid));
839 if(sc != noErr){
840 CFRelease(uid);
841 return E_FAIL;
844 CFRelease(uid);
846 return S_OK;
849 static void session_init_vols(AudioSession *session, UINT channels)
851 if(session->channel_count < channels){
852 UINT i;
854 if(session->channel_vols)
855 session->channel_vols = HeapReAlloc(GetProcessHeap(), 0,
856 session->channel_vols, sizeof(float) * channels);
857 else
858 session->channel_vols = HeapAlloc(GetProcessHeap(), 0,
859 sizeof(float) * channels);
860 if(!session->channel_vols)
861 return;
863 for(i = session->channel_count; i < channels; ++i)
864 session->channel_vols[i] = 1.f;
866 session->channel_count = channels;
870 static AudioSession *create_session(const GUID *guid, IMMDevice *device,
871 UINT num_channels)
873 AudioSession *ret;
875 ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(AudioSession));
876 if(!ret)
877 return NULL;
879 memcpy(&ret->guid, guid, sizeof(GUID));
881 ret->device = device;
883 list_init(&ret->clients);
885 list_add_head(&g_sessions, &ret->entry);
887 InitializeCriticalSection(&ret->lock);
888 ret->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": AudioSession.lock");
890 session_init_vols(ret, num_channels);
892 ret->master_vol = 1.f;
894 return ret;
897 /* if channels == 0, then this will return or create a session with
898 * matching dataflow and GUID. otherwise, channels must also match */
899 static HRESULT get_audio_session(const GUID *sessionguid,
900 IMMDevice *device, UINT channels, AudioSession **out)
902 AudioSession *session;
904 if(!sessionguid || IsEqualGUID(sessionguid, &GUID_NULL)){
905 *out = create_session(&GUID_NULL, device, channels);
906 if(!*out)
907 return E_OUTOFMEMORY;
909 return S_OK;
912 *out = NULL;
913 LIST_FOR_EACH_ENTRY(session, &g_sessions, AudioSession, entry){
914 if(session->device == device &&
915 IsEqualGUID(sessionguid, &session->guid)){
916 session_init_vols(session, channels);
917 *out = session;
918 break;
922 if(!*out){
923 *out = create_session(sessionguid, device, channels);
924 if(!*out)
925 return E_OUTOFMEMORY;
928 return S_OK;
931 static HRESULT WINAPI AudioClient_Initialize(IAudioClient *iface,
932 AUDCLNT_SHAREMODE mode, DWORD flags, REFERENCE_TIME duration,
933 REFERENCE_TIME period, const WAVEFORMATEX *fmt,
934 const GUID *sessionguid)
936 ACImpl *This = impl_from_IAudioClient(iface);
937 HRESULT hr;
938 OSStatus sc;
939 int i;
941 TRACE("(%p)->(%x, %x, %s, %s, %p, %s)\n", This, mode, flags,
942 wine_dbgstr_longlong(duration), wine_dbgstr_longlong(period), fmt, debugstr_guid(sessionguid));
944 if(!fmt)
945 return E_POINTER;
947 dump_fmt(fmt);
949 if(mode != AUDCLNT_SHAREMODE_SHARED && mode != AUDCLNT_SHAREMODE_EXCLUSIVE)
950 return AUDCLNT_E_NOT_INITIALIZED;
952 if(flags & ~(AUDCLNT_STREAMFLAGS_CROSSPROCESS |
953 AUDCLNT_STREAMFLAGS_LOOPBACK |
954 AUDCLNT_STREAMFLAGS_EVENTCALLBACK |
955 AUDCLNT_STREAMFLAGS_NOPERSIST |
956 AUDCLNT_STREAMFLAGS_RATEADJUST |
957 AUDCLNT_SESSIONFLAGS_EXPIREWHENUNOWNED |
958 AUDCLNT_SESSIONFLAGS_DISPLAY_HIDE |
959 AUDCLNT_SESSIONFLAGS_DISPLAY_HIDEWHENEXPIRED)){
960 TRACE("Unknown flags: %08x\n", flags);
961 return E_INVALIDARG;
964 OSSpinLockLock(&This->lock);
966 if(This->aqueue){
967 OSSpinLockUnlock(&This->lock);
968 return AUDCLNT_E_ALREADY_INITIALIZED;
971 hr = ca_setup_aqueue(This->adevid, This->dataflow, fmt, This, &This->aqueue);
972 if(FAILED(hr)){
973 OSSpinLockUnlock(&This->lock);
974 return hr;
977 This->fmt = clone_format(fmt);
978 if(!This->fmt){
979 AudioQueueDispose(This->aqueue, 1);
980 This->aqueue = NULL;
981 OSSpinLockUnlock(&This->lock);
982 return E_OUTOFMEMORY;
985 if(period){
986 This->period_ms = period / 10000;
987 if(This->period_ms == 0)
988 This->period_ms = 1;
989 }else
990 This->period_ms = MinimumPeriod / 10000;
992 if(!duration)
993 duration = 300000; /* 0.03s */
994 This->bufsize_frames = ceil(fmt->nSamplesPerSec * (duration / 10000000.));
996 if(This->dataflow == eCapture){
997 int i;
998 UInt32 bsize = ceil((This->bufsize_frames / (double)CAPTURE_BUFFERS) *
999 This->fmt->nBlockAlign);
1000 for(i = 0; i < CAPTURE_BUFFERS; ++i){
1001 AQBuffer *buf;
1003 buf = HeapAlloc(GetProcessHeap(), 0, sizeof(AQBuffer));
1004 if(!buf){
1005 AudioQueueDispose(This->aqueue, 1);
1006 This->aqueue = NULL;
1007 CoTaskMemFree(This->fmt);
1008 This->fmt = NULL;
1009 OSSpinLockUnlock(&This->lock);
1010 return E_OUTOFMEMORY;
1013 sc = AudioQueueAllocateBuffer(This->aqueue, bsize, &buf->buf);
1014 if(sc != noErr){
1015 AudioQueueDispose(This->aqueue, 1);
1016 This->aqueue = NULL;
1017 CoTaskMemFree(This->fmt);
1018 This->fmt = NULL;
1019 OSSpinLockUnlock(&This->lock);
1020 WARN("Couldn't allocate buffer: %lx\n", sc);
1021 return E_FAIL;
1024 buf->buf->mUserData = buf;
1026 sc = AudioQueueEnqueueBuffer(This->aqueue, buf->buf, 0, NULL);
1027 if(sc != noErr){
1028 ERR("Couldn't enqueue buffer: %lx\n", sc);
1029 break;
1034 This->vols = HeapAlloc(GetProcessHeap(), 0, fmt->nChannels * sizeof(float));
1035 if(!This->vols){
1036 AudioQueueDispose(This->aqueue, 1);
1037 This->aqueue = NULL;
1038 CoTaskMemFree(This->fmt);
1039 This->fmt = NULL;
1040 OSSpinLockUnlock(&This->lock);
1041 return E_OUTOFMEMORY;
1044 for(i = 0; i < fmt->nChannels; ++i)
1045 This->vols[i] = 1.f;
1047 This->share = mode;
1048 This->flags = flags;
1050 EnterCriticalSection(&g_sessions_lock);
1052 hr = get_audio_session(sessionguid, This->parent, fmt->nChannels,
1053 &This->session);
1054 if(FAILED(hr)){
1055 LeaveCriticalSection(&g_sessions_lock);
1056 AudioQueueDispose(This->aqueue, 1);
1057 This->aqueue = NULL;
1058 CoTaskMemFree(This->fmt);
1059 This->fmt = NULL;
1060 HeapFree(GetProcessHeap(), 0, This->vols);
1061 This->vols = NULL;
1062 OSSpinLockUnlock(&This->lock);
1063 return E_INVALIDARG;
1066 list_add_tail(&This->session->clients, &This->entry);
1068 LeaveCriticalSection(&g_sessions_lock);
1070 ca_setvol(This, -1);
1072 OSSpinLockUnlock(&This->lock);
1074 return S_OK;
1077 static HRESULT WINAPI AudioClient_GetBufferSize(IAudioClient *iface,
1078 UINT32 *frames)
1080 ACImpl *This = impl_from_IAudioClient(iface);
1082 TRACE("(%p)->(%p)\n", This, frames);
1084 if(!frames)
1085 return E_POINTER;
1087 OSSpinLockLock(&This->lock);
1089 if(!This->aqueue){
1090 OSSpinLockUnlock(&This->lock);
1091 return AUDCLNT_E_NOT_INITIALIZED;
1094 *frames = This->bufsize_frames;
1096 OSSpinLockUnlock(&This->lock);
1098 return S_OK;
1101 static HRESULT ca_get_max_stream_latency(ACImpl *This, UInt32 *max)
1103 AudioObjectPropertyAddress addr;
1104 AudioStreamID *ids;
1105 UInt32 size;
1106 OSStatus sc;
1107 int nstreams, i;
1109 addr.mScope = This->scope;
1110 addr.mElement = 0;
1111 addr.mSelector = kAudioDevicePropertyStreams;
1113 sc = AudioObjectGetPropertyDataSize(This->adevid, &addr, 0, NULL,
1114 &size);
1115 if(sc != noErr){
1116 WARN("Unable to get size for _Streams property: %lx\n", sc);
1117 return E_FAIL;
1120 ids = HeapAlloc(GetProcessHeap(), 0, size);
1121 if(!ids)
1122 return E_OUTOFMEMORY;
1124 sc = AudioObjectGetPropertyData(This->adevid, &addr, 0, NULL, &size, ids);
1125 if(sc != noErr){
1126 WARN("Unable to get _Streams property: %lx\n", sc);
1127 HeapFree(GetProcessHeap(), 0, ids);
1128 return E_FAIL;
1131 nstreams = size / sizeof(AudioStreamID);
1132 *max = 0;
1134 addr.mSelector = kAudioStreamPropertyLatency;
1135 for(i = 0; i < nstreams; ++i){
1136 UInt32 latency;
1138 size = sizeof(latency);
1139 sc = AudioObjectGetPropertyData(ids[i], &addr, 0, NULL,
1140 &size, &latency);
1141 if(sc != noErr){
1142 WARN("Unable to get _Latency property: %lx\n", sc);
1143 continue;
1146 if(latency > *max)
1147 *max = latency;
1150 HeapFree(GetProcessHeap(), 0, ids);
1152 return S_OK;
1155 static HRESULT WINAPI AudioClient_GetStreamLatency(IAudioClient *iface,
1156 REFERENCE_TIME *out)
1158 ACImpl *This = impl_from_IAudioClient(iface);
1159 UInt32 latency, stream_latency, size;
1160 AudioObjectPropertyAddress addr;
1161 OSStatus sc;
1162 HRESULT hr;
1164 TRACE("(%p)->(%p)\n", This, out);
1166 if(!out)
1167 return E_POINTER;
1169 OSSpinLockLock(&This->lock);
1171 if(!This->aqueue){
1172 OSSpinLockUnlock(&This->lock);
1173 return AUDCLNT_E_NOT_INITIALIZED;
1176 addr.mScope = This->scope;
1177 addr.mSelector = kAudioDevicePropertyLatency;
1178 addr.mElement = 0;
1180 size = sizeof(latency);
1181 sc = AudioObjectGetPropertyData(This->adevid, &addr, 0, NULL,
1182 &size, &latency);
1183 if(sc != noErr){
1184 WARN("Couldn't get _Latency property: %lx\n", sc);
1185 OSSpinLockUnlock(&This->lock);
1186 return E_FAIL;
1189 hr = ca_get_max_stream_latency(This, &stream_latency);
1190 if(FAILED(hr)){
1191 OSSpinLockUnlock(&This->lock);
1192 return hr;
1195 latency += stream_latency;
1196 /* pretend we process audio in Period chunks, so max latency includes
1197 * the period time */
1198 latency += DefaultPeriod;
1199 *out = (latency / (double)This->fmt->nSamplesPerSec) * 10000000;
1201 OSSpinLockUnlock(&This->lock);
1203 return S_OK;
1206 static HRESULT AudioClient_GetCurrentPadding_nolock(ACImpl *This,
1207 UINT32 *numpad)
1209 if(!This->aqueue)
1210 return AUDCLNT_E_NOT_INITIALIZED;
1212 if(This->dataflow == eRender){
1213 UINT64 bufpos;
1214 bufpos = get_current_aqbuffer_position(This, BUFPOS_RELATIVE);
1215 *numpad = This->inbuf_frames - bufpos;
1216 }else
1217 *numpad = This->inbuf_frames;
1219 return S_OK;
1222 static HRESULT WINAPI AudioClient_GetCurrentPadding(IAudioClient *iface,
1223 UINT32 *numpad)
1225 ACImpl *This = impl_from_IAudioClient(iface);
1226 HRESULT hr;
1228 TRACE("(%p)->(%p)\n", This, numpad);
1230 if(!numpad)
1231 return E_POINTER;
1233 OSSpinLockLock(&This->lock);
1235 hr = AudioClient_GetCurrentPadding_nolock(This, numpad);
1237 OSSpinLockUnlock(&This->lock);
1239 return hr;
1242 static HRESULT WINAPI AudioClient_IsFormatSupported(IAudioClient *iface,
1243 AUDCLNT_SHAREMODE mode, const WAVEFORMATEX *pwfx,
1244 WAVEFORMATEX **outpwfx)
1246 ACImpl *This = impl_from_IAudioClient(iface);
1247 WAVEFORMATEXTENSIBLE *fmtex = (WAVEFORMATEXTENSIBLE*)pwfx;
1248 AudioQueueRef aqueue;
1249 HRESULT hr;
1251 TRACE("(%p)->(%x, %p, %p)\n", This, mode, pwfx, outpwfx);
1253 if(!pwfx || (mode == AUDCLNT_SHAREMODE_SHARED && !outpwfx))
1254 return E_POINTER;
1256 if(mode != AUDCLNT_SHAREMODE_SHARED && mode != AUDCLNT_SHAREMODE_EXCLUSIVE)
1257 return E_INVALIDARG;
1259 if(pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
1260 pwfx->cbSize < sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX))
1261 return E_INVALIDARG;
1263 dump_fmt(pwfx);
1265 if(outpwfx)
1266 *outpwfx = NULL;
1268 if(pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
1269 fmtex->dwChannelMask != 0 &&
1270 fmtex->dwChannelMask != get_channel_mask(pwfx->nChannels))
1271 return AUDCLNT_E_UNSUPPORTED_FORMAT;
1273 OSSpinLockLock(&This->lock);
1275 hr = ca_setup_aqueue(This->adevid, This->dataflow, pwfx, NULL, &aqueue);
1276 if(SUCCEEDED(hr)){
1277 AudioQueueDispose(aqueue, 1);
1278 OSSpinLockUnlock(&This->lock);
1279 TRACE("returning %08x\n", S_OK);
1280 return S_OK;
1283 OSSpinLockUnlock(&This->lock);
1285 TRACE("returning %08x\n", AUDCLNT_E_UNSUPPORTED_FORMAT);
1286 return AUDCLNT_E_UNSUPPORTED_FORMAT;
1289 static HRESULT WINAPI AudioClient_GetMixFormat(IAudioClient *iface,
1290 WAVEFORMATEX **pwfx)
1292 ACImpl *This = impl_from_IAudioClient(iface);
1293 WAVEFORMATEXTENSIBLE *fmt;
1294 OSStatus sc;
1295 UInt32 size;
1296 Float64 rate;
1297 AudioBufferList *buffers;
1298 AudioObjectPropertyAddress addr;
1299 int i;
1301 TRACE("(%p)->(%p)\n", This, pwfx);
1303 if(!pwfx)
1304 return E_POINTER;
1305 *pwfx = NULL;
1307 fmt = CoTaskMemAlloc(sizeof(WAVEFORMATEXTENSIBLE));
1308 if(!fmt)
1309 return E_OUTOFMEMORY;
1311 fmt->Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
1313 addr.mScope = This->scope;
1314 addr.mElement = 0;
1315 addr.mSelector = kAudioDevicePropertyStreamConfiguration;
1317 sc = AudioObjectGetPropertyDataSize(This->adevid, &addr, 0, NULL, &size);
1318 if(sc != noErr){
1319 CoTaskMemFree(fmt);
1320 WARN("Unable to get size for _StreamConfiguration property: %lx\n", sc);
1321 return E_FAIL;
1324 buffers = HeapAlloc(GetProcessHeap(), 0, size);
1325 if(!buffers){
1326 CoTaskMemFree(fmt);
1327 return E_OUTOFMEMORY;
1330 sc = AudioObjectGetPropertyData(This->adevid, &addr, 0, NULL,
1331 &size, buffers);
1332 if(sc != noErr){
1333 CoTaskMemFree(fmt);
1334 HeapFree(GetProcessHeap(), 0, buffers);
1335 WARN("Unable to get _StreamConfiguration property: %lx\n", sc);
1336 return E_FAIL;
1339 fmt->Format.nChannels = 0;
1340 for(i = 0; i < buffers->mNumberBuffers; ++i)
1341 fmt->Format.nChannels += buffers->mBuffers[i].mNumberChannels;
1343 HeapFree(GetProcessHeap(), 0, buffers);
1345 fmt->dwChannelMask = get_channel_mask(fmt->Format.nChannels);
1347 addr.mSelector = kAudioDevicePropertyNominalSampleRate;
1348 size = sizeof(Float64);
1349 sc = AudioObjectGetPropertyData(This->adevid, &addr, 0, NULL, &size, &rate);
1350 if(sc != noErr){
1351 CoTaskMemFree(fmt);
1352 WARN("Unable to get _NominalSampleRate property: %lx\n", sc);
1353 return E_FAIL;
1355 fmt->Format.nSamplesPerSec = rate;
1357 fmt->Format.wBitsPerSample = 32;
1358 fmt->SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
1360 fmt->Format.nBlockAlign = (fmt->Format.wBitsPerSample *
1361 fmt->Format.nChannels) / 8;
1362 fmt->Format.nAvgBytesPerSec = fmt->Format.nSamplesPerSec *
1363 fmt->Format.nBlockAlign;
1365 fmt->Samples.wValidBitsPerSample = fmt->Format.wBitsPerSample;
1366 fmt->Format.cbSize = sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
1368 *pwfx = (WAVEFORMATEX*)fmt;
1369 dump_fmt(*pwfx);
1371 return S_OK;
1374 static HRESULT WINAPI AudioClient_GetDevicePeriod(IAudioClient *iface,
1375 REFERENCE_TIME *defperiod, REFERENCE_TIME *minperiod)
1377 ACImpl *This = impl_from_IAudioClient(iface);
1379 TRACE("(%p)->(%p, %p)\n", This, defperiod, minperiod);
1381 if(!defperiod && !minperiod)
1382 return E_POINTER;
1384 OSSpinLockLock(&This->lock);
1386 if(This->period_ms){
1387 if(defperiod)
1388 *defperiod = This->period_ms * 10000;
1389 if(minperiod)
1390 *minperiod = This->period_ms * 10000;
1391 }else{
1392 if(defperiod)
1393 *defperiod = DefaultPeriod;
1394 if(minperiod)
1395 *minperiod = MinimumPeriod;
1398 OSSpinLockUnlock(&This->lock);
1400 return S_OK;
1403 void CALLBACK ca_period_cb(void *user, BOOLEAN timer)
1405 ACImpl *This = user;
1407 OSSpinLockLock(&This->lock);
1408 if(This->event)
1409 SetEvent(This->event);
1410 OSSpinLockUnlock(&This->lock);
1413 static HRESULT WINAPI AudioClient_Start(IAudioClient *iface)
1415 ACImpl *This = impl_from_IAudioClient(iface);
1416 OSStatus sc;
1418 TRACE("(%p)\n", This);
1420 OSSpinLockLock(&This->lock);
1422 if(!This->aqueue){
1423 OSSpinLockUnlock(&This->lock);
1424 return AUDCLNT_E_NOT_INITIALIZED;
1427 if(This->playing != StateStopped){
1428 OSSpinLockUnlock(&This->lock);
1429 return AUDCLNT_E_NOT_STOPPED;
1432 if((This->flags & AUDCLNT_STREAMFLAGS_EVENTCALLBACK) && !This->event){
1433 OSSpinLockUnlock(&This->lock);
1434 return AUDCLNT_E_EVENTHANDLE_NOT_SET;
1437 if(This->event)
1438 if(!CreateTimerQueueTimer(&This->timer, g_timer_q,
1439 ca_period_cb, This, 0, This->period_ms, 0))
1440 ERR("Unable to create timer: %u\n", GetLastError());
1442 This->playing = StateInTransition;
1444 OSSpinLockUnlock(&This->lock);
1446 sc = AudioQueueStart(This->aqueue, NULL);
1447 if(sc != noErr){
1448 WARN("Unable to start audio queue: %lx\n", sc);
1449 return E_FAIL;
1452 OSSpinLockLock(&This->lock);
1454 This->playing = StatePlaying;
1456 OSSpinLockUnlock(&This->lock);
1458 return S_OK;
1461 static HRESULT WINAPI AudioClient_Stop(IAudioClient *iface)
1463 ACImpl *This = impl_from_IAudioClient(iface);
1464 AudioTimeStamp tstamp;
1465 OSStatus sc;
1467 TRACE("(%p)\n", This);
1469 OSSpinLockLock(&This->lock);
1471 if(!This->aqueue){
1472 OSSpinLockUnlock(&This->lock);
1473 return AUDCLNT_E_NOT_INITIALIZED;
1476 if(This->playing == StateStopped){
1477 OSSpinLockUnlock(&This->lock);
1478 return S_FALSE;
1481 if(This->playing == StateInTransition){
1482 OSSpinLockUnlock(&This->lock);
1483 return S_OK;
1486 if(This->timer && This->timer != INVALID_HANDLE_VALUE){
1487 DeleteTimerQueueTimer(g_timer_q, This->timer, INVALID_HANDLE_VALUE);
1488 This->timer = NULL;
1491 This->playing = StateInTransition;
1493 sc = AudioQueueGetCurrentTime(This->aqueue, NULL, &tstamp, NULL);
1494 if(sc == noErr){
1495 if(tstamp.mFlags & kAudioTimeStampSampleTimeValid){
1496 if(tstamp.mSampleTime > This->highest_sampletime)
1497 This->highest_sampletime = tstamp.mSampleTime;
1498 }else
1499 WARN("Returned tstamp mSampleTime not valid: %lx\n", tstamp.mFlags);
1500 }else
1501 WARN("GetCurrentTime failed: %lx\n", sc);
1503 OSSpinLockUnlock(&This->lock);
1505 sc = AudioQueueFlush(This->aqueue);
1506 if(sc != noErr)
1507 WARN("Unable to flush audio queue: %lx\n", sc);
1509 sc = AudioQueuePause(This->aqueue);
1510 if(sc != noErr){
1511 WARN("Unable to pause audio queue: %lx\n", sc);
1512 return E_FAIL;
1515 OSSpinLockLock(&This->lock);
1517 This->playing = StateStopped;
1519 OSSpinLockUnlock(&This->lock);
1521 return S_OK;
1524 static HRESULT WINAPI AudioClient_Reset(IAudioClient *iface)
1526 ACImpl *This = impl_from_IAudioClient(iface);
1527 OSStatus sc;
1528 QueuedBufInfo *bufinfo, *bufinfo2;
1530 TRACE("(%p)\n", This);
1532 OSSpinLockLock(&This->lock);
1534 if(!This->aqueue){
1535 OSSpinLockUnlock(&This->lock);
1536 return AUDCLNT_E_NOT_INITIALIZED;
1539 if(This->playing != StateStopped){
1540 OSSpinLockUnlock(&This->lock);
1541 return AUDCLNT_E_NOT_STOPPED;
1544 if(This->getbuf_last){
1545 OSSpinLockUnlock(&This->lock);
1546 return AUDCLNT_E_BUFFER_OPERATION_PENDING;
1549 This->written_frames = 0;
1550 This->inbuf_frames = 0;
1552 LIST_FOR_EACH_ENTRY_SAFE(bufinfo, bufinfo2, &This->queued_bufinfos,
1553 QueuedBufInfo, entry){
1554 list_remove(&bufinfo->entry);
1555 HeapFree(GetProcessHeap(), 0, bufinfo);
1558 OSSpinLockUnlock(&This->lock);
1560 sc = AudioQueueReset(This->aqueue);
1561 if(sc != noErr){
1562 WARN("Unable to reset audio queue: %lx\n", sc);
1563 return E_FAIL;
1566 return S_OK;
1569 static HRESULT WINAPI AudioClient_SetEventHandle(IAudioClient *iface,
1570 HANDLE event)
1572 ACImpl *This = impl_from_IAudioClient(iface);
1574 TRACE("(%p)->(%p)\n", This, event);
1576 if(!event)
1577 return E_INVALIDARG;
1579 OSSpinLockLock(&This->lock);
1581 if(!This->aqueue){
1582 OSSpinLockUnlock(&This->lock);
1583 return AUDCLNT_E_NOT_INITIALIZED;
1586 if(!(This->flags & AUDCLNT_STREAMFLAGS_EVENTCALLBACK)){
1587 OSSpinLockUnlock(&This->lock);
1588 return AUDCLNT_E_EVENTHANDLE_NOT_EXPECTED;
1591 This->event = event;
1593 OSSpinLockUnlock(&This->lock);
1595 return S_OK;
1598 static HRESULT WINAPI AudioClient_GetService(IAudioClient *iface, REFIID riid,
1599 void **ppv)
1601 ACImpl *This = impl_from_IAudioClient(iface);
1603 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
1605 if(!ppv)
1606 return E_POINTER;
1607 *ppv = NULL;
1609 OSSpinLockLock(&This->lock);
1611 if(!This->aqueue){
1612 OSSpinLockUnlock(&This->lock);
1613 return AUDCLNT_E_NOT_INITIALIZED;
1616 if(IsEqualIID(riid, &IID_IAudioRenderClient)){
1617 if(This->dataflow != eRender){
1618 OSSpinLockUnlock(&This->lock);
1619 return AUDCLNT_E_WRONG_ENDPOINT_TYPE;
1621 IAudioRenderClient_AddRef(&This->IAudioRenderClient_iface);
1622 *ppv = &This->IAudioRenderClient_iface;
1623 }else if(IsEqualIID(riid, &IID_IAudioCaptureClient)){
1624 if(This->dataflow != eCapture){
1625 OSSpinLockUnlock(&This->lock);
1626 return AUDCLNT_E_WRONG_ENDPOINT_TYPE;
1628 IAudioCaptureClient_AddRef(&This->IAudioCaptureClient_iface);
1629 *ppv = &This->IAudioCaptureClient_iface;
1630 }else if(IsEqualIID(riid, &IID_IAudioClock)){
1631 IAudioClock_AddRef(&This->IAudioClock_iface);
1632 *ppv = &This->IAudioClock_iface;
1633 }else if(IsEqualIID(riid, &IID_IAudioStreamVolume)){
1634 IAudioStreamVolume_AddRef(&This->IAudioStreamVolume_iface);
1635 *ppv = &This->IAudioStreamVolume_iface;
1636 }else if(IsEqualIID(riid, &IID_IAudioSessionControl)){
1637 if(!This->session_wrapper){
1638 This->session_wrapper = AudioSessionWrapper_Create(This);
1639 if(!This->session_wrapper){
1640 OSSpinLockUnlock(&This->lock);
1641 return E_OUTOFMEMORY;
1643 }else
1644 IAudioSessionControl2_AddRef(&This->session_wrapper->IAudioSessionControl2_iface);
1646 *ppv = &This->session_wrapper->IAudioSessionControl2_iface;
1647 }else if(IsEqualIID(riid, &IID_IChannelAudioVolume)){
1648 if(!This->session_wrapper){
1649 This->session_wrapper = AudioSessionWrapper_Create(This);
1650 if(!This->session_wrapper){
1651 OSSpinLockUnlock(&This->lock);
1652 return E_OUTOFMEMORY;
1654 }else
1655 IChannelAudioVolume_AddRef(&This->session_wrapper->IChannelAudioVolume_iface);
1657 *ppv = &This->session_wrapper->IChannelAudioVolume_iface;
1658 }else if(IsEqualIID(riid, &IID_ISimpleAudioVolume)){
1659 if(!This->session_wrapper){
1660 This->session_wrapper = AudioSessionWrapper_Create(This);
1661 if(!This->session_wrapper){
1662 OSSpinLockUnlock(&This->lock);
1663 return E_OUTOFMEMORY;
1665 }else
1666 ISimpleAudioVolume_AddRef(&This->session_wrapper->ISimpleAudioVolume_iface);
1668 *ppv = &This->session_wrapper->ISimpleAudioVolume_iface;
1671 if(*ppv){
1672 OSSpinLockUnlock(&This->lock);
1673 return S_OK;
1676 OSSpinLockUnlock(&This->lock);
1678 FIXME("stub %s\n", debugstr_guid(riid));
1679 return E_NOINTERFACE;
1682 static const IAudioClientVtbl AudioClient_Vtbl =
1684 AudioClient_QueryInterface,
1685 AudioClient_AddRef,
1686 AudioClient_Release,
1687 AudioClient_Initialize,
1688 AudioClient_GetBufferSize,
1689 AudioClient_GetStreamLatency,
1690 AudioClient_GetCurrentPadding,
1691 AudioClient_IsFormatSupported,
1692 AudioClient_GetMixFormat,
1693 AudioClient_GetDevicePeriod,
1694 AudioClient_Start,
1695 AudioClient_Stop,
1696 AudioClient_Reset,
1697 AudioClient_SetEventHandle,
1698 AudioClient_GetService
1701 static HRESULT WINAPI AudioRenderClient_QueryInterface(
1702 IAudioRenderClient *iface, REFIID riid, void **ppv)
1704 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
1706 if(!ppv)
1707 return E_POINTER;
1708 *ppv = NULL;
1710 if(IsEqualIID(riid, &IID_IUnknown) ||
1711 IsEqualIID(riid, &IID_IAudioRenderClient))
1712 *ppv = iface;
1713 if(*ppv){
1714 IUnknown_AddRef((IUnknown*)*ppv);
1715 return S_OK;
1718 WARN("Unknown interface %s\n", debugstr_guid(riid));
1719 return E_NOINTERFACE;
1722 static ULONG WINAPI AudioRenderClient_AddRef(IAudioRenderClient *iface)
1724 ACImpl *This = impl_from_IAudioRenderClient(iface);
1725 return AudioClient_AddRef(&This->IAudioClient_iface);
1728 static ULONG WINAPI AudioRenderClient_Release(IAudioRenderClient *iface)
1730 ACImpl *This = impl_from_IAudioRenderClient(iface);
1731 return AudioClient_Release(&This->IAudioClient_iface);
1734 static HRESULT WINAPI AudioRenderClient_GetBuffer(IAudioRenderClient *iface,
1735 UINT32 frames, BYTE **data)
1737 ACImpl *This = impl_from_IAudioRenderClient(iface);
1738 AQBuffer *buf;
1739 UINT32 pad, bytes = frames * This->fmt->nBlockAlign;
1740 HRESULT hr;
1741 OSStatus sc;
1743 TRACE("(%p)->(%u, %p)\n", This, frames, data);
1745 if(!data)
1746 return E_POINTER;
1747 *data = NULL;
1749 OSSpinLockLock(&This->lock);
1751 if(This->getbuf_last){
1752 OSSpinLockUnlock(&This->lock);
1753 return AUDCLNT_E_OUT_OF_ORDER;
1756 if(!frames){
1757 OSSpinLockUnlock(&This->lock);
1758 return S_OK;
1761 hr = AudioClient_GetCurrentPadding_nolock(This, &pad);
1762 if(FAILED(hr)){
1763 OSSpinLockUnlock(&This->lock);
1764 return hr;
1767 if(pad + frames > This->bufsize_frames){
1768 OSSpinLockUnlock(&This->lock);
1769 return AUDCLNT_E_BUFFER_TOO_LARGE;
1772 LIST_FOR_EACH_ENTRY(buf, &This->avail_buffers, AQBuffer, entry){
1773 if(buf->buf->mAudioDataBytesCapacity >= bytes){
1774 This->public_buffer = buf->buf;
1775 list_remove(&buf->entry);
1776 break;
1780 if(&buf->entry == &This->avail_buffers){
1781 sc = AudioQueueAllocateBuffer(This->aqueue, bytes,
1782 &This->public_buffer);
1783 if(sc != noErr){
1784 OSSpinLockUnlock(&This->lock);
1785 WARN("Unable to allocate buffer: %lx\n", sc);
1786 return E_FAIL;
1788 buf = HeapAlloc(GetProcessHeap(), 0, sizeof(AQBuffer));
1789 if(!buf){
1790 AudioQueueFreeBuffer(This->aqueue, This->public_buffer);
1791 This->public_buffer = NULL;
1792 OSSpinLockUnlock(&This->lock);
1793 return E_OUTOFMEMORY;
1795 buf->buf = This->public_buffer;
1796 This->public_buffer->mUserData = buf;
1799 *data = This->public_buffer->mAudioData;
1801 This->getbuf_last = frames;
1803 OSSpinLockUnlock(&This->lock);
1805 return S_OK;
1808 static HRESULT WINAPI AudioRenderClient_ReleaseBuffer(
1809 IAudioRenderClient *iface, UINT32 frames, DWORD flags)
1811 ACImpl *This = impl_from_IAudioRenderClient(iface);
1812 AudioTimeStamp start_time;
1813 OSStatus sc;
1815 TRACE("(%p)->(%u, %x)\n", This, frames, flags);
1817 OSSpinLockLock(&This->lock);
1819 if(!frames){
1820 This->getbuf_last = 0;
1821 if(This->public_buffer){
1822 AQBuffer *buf = This->public_buffer->mUserData;
1823 list_add_tail(&This->avail_buffers, &buf->entry);
1824 This->public_buffer = NULL;
1826 OSSpinLockUnlock(&This->lock);
1827 return S_OK;
1830 if(!This->getbuf_last){
1831 OSSpinLockUnlock(&This->lock);
1832 return AUDCLNT_E_OUT_OF_ORDER;
1835 if(frames > This->getbuf_last){
1836 OSSpinLockUnlock(&This->lock);
1837 return AUDCLNT_E_INVALID_SIZE;
1840 if(flags & AUDCLNT_BUFFERFLAGS_SILENT){
1841 WAVEFORMATEXTENSIBLE *fmtex = (WAVEFORMATEXTENSIBLE*)This->fmt;
1842 if((This->fmt->wFormatTag == WAVE_FORMAT_PCM ||
1843 (This->fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
1844 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM))) &&
1845 This->fmt->wBitsPerSample == 8)
1846 memset(This->public_buffer->mAudioData, 128,
1847 frames * This->fmt->nBlockAlign);
1848 else
1849 memset(This->public_buffer->mAudioData, 0,
1850 frames * This->fmt->nBlockAlign);
1853 This->public_buffer->mAudioDataByteSize = frames * This->fmt->nBlockAlign;
1855 sc = AudioQueueEnqueueBufferWithParameters(This->aqueue,
1856 This->public_buffer, 0, NULL, 0, 0, 0, NULL, NULL, &start_time);
1857 if(sc != noErr){
1858 OSSpinLockUnlock(&This->lock);
1859 WARN("Unable to enqueue buffer: %lx\n", sc);
1860 return E_FAIL;
1863 if(start_time.mFlags & kAudioTimeStampSampleTimeValid){
1864 QueuedBufInfo *bufinfo;
1866 bufinfo = HeapAlloc(GetProcessHeap(), 0, sizeof(*bufinfo));
1867 bufinfo->start_sampletime = start_time.mSampleTime;
1868 bufinfo->start_pos = This->written_frames;
1869 bufinfo->len_frames = frames;
1871 list_add_tail(&This->queued_bufinfos, &bufinfo->entry);
1872 }else
1873 WARN("Start time didn't contain valid SampleTime member\n");
1875 if(This->playing == StateStopped)
1876 AudioQueuePrime(This->aqueue, 0, NULL);
1878 This->public_buffer = NULL;
1879 This->getbuf_last = 0;
1880 This->written_frames += frames;
1881 This->inbuf_frames += frames;
1883 OSSpinLockUnlock(&This->lock);
1885 return S_OK;
1888 static const IAudioRenderClientVtbl AudioRenderClient_Vtbl = {
1889 AudioRenderClient_QueryInterface,
1890 AudioRenderClient_AddRef,
1891 AudioRenderClient_Release,
1892 AudioRenderClient_GetBuffer,
1893 AudioRenderClient_ReleaseBuffer
1896 static HRESULT WINAPI AudioCaptureClient_QueryInterface(
1897 IAudioCaptureClient *iface, REFIID riid, void **ppv)
1899 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
1901 if(!ppv)
1902 return E_POINTER;
1903 *ppv = NULL;
1905 if(IsEqualIID(riid, &IID_IUnknown) ||
1906 IsEqualIID(riid, &IID_IAudioCaptureClient))
1907 *ppv = iface;
1908 if(*ppv){
1909 IUnknown_AddRef((IUnknown*)*ppv);
1910 return S_OK;
1913 WARN("Unknown interface %s\n", debugstr_guid(riid));
1914 return E_NOINTERFACE;
1917 static ULONG WINAPI AudioCaptureClient_AddRef(IAudioCaptureClient *iface)
1919 ACImpl *This = impl_from_IAudioCaptureClient(iface);
1920 return IAudioClient_AddRef(&This->IAudioClient_iface);
1923 static ULONG WINAPI AudioCaptureClient_Release(IAudioCaptureClient *iface)
1925 ACImpl *This = impl_from_IAudioCaptureClient(iface);
1926 return IAudioClient_Release(&This->IAudioClient_iface);
1929 static HRESULT WINAPI AudioCaptureClient_GetBuffer(IAudioCaptureClient *iface,
1930 BYTE **data, UINT32 *frames, DWORD *flags, UINT64 *devpos,
1931 UINT64 *qpcpos)
1933 ACImpl *This = impl_from_IAudioCaptureClient(iface);
1935 TRACE("(%p)->(%p, %p, %p, %p, %p)\n", This, data, frames, flags,
1936 devpos, qpcpos);
1938 if(!data || !frames || !flags)
1939 return E_POINTER;
1941 OSSpinLockLock(&This->lock);
1943 if(This->getbuf_last){
1944 OSSpinLockUnlock(&This->lock);
1945 return AUDCLNT_E_OUT_OF_ORDER;
1948 if(This->public_buffer){
1949 *data = This->public_buffer->mAudioData;
1950 *frames =
1951 This->public_buffer->mAudioDataByteSize / This->fmt->nBlockAlign;
1952 }else{
1953 struct list *head = list_head(&This->avail_buffers);
1954 if(!head){
1955 *data = NULL;
1956 *frames = 0;
1957 }else{
1958 AQBuffer *buf = LIST_ENTRY(head, AQBuffer, entry);
1959 This->public_buffer = buf->buf;
1960 *data = This->public_buffer->mAudioData;
1961 *frames =
1962 This->public_buffer->mAudioDataByteSize / This->fmt->nBlockAlign;
1963 list_remove(&buf->entry);
1967 *flags = 0;
1968 This->written_frames += *frames;
1969 This->inbuf_frames -= *frames;
1970 This->getbuf_last = 1;
1972 if(devpos || qpcpos)
1973 AudioClock_GetPosition_nolock(This, devpos, qpcpos);
1975 OSSpinLockUnlock(&This->lock);
1977 return *frames ? S_OK : AUDCLNT_S_BUFFER_EMPTY;
1980 static HRESULT WINAPI AudioCaptureClient_ReleaseBuffer(
1981 IAudioCaptureClient *iface, UINT32 done)
1983 ACImpl *This = impl_from_IAudioCaptureClient(iface);
1984 UINT32 pbuf_frames;
1985 OSStatus sc;
1987 TRACE("(%p)->(%u)\n", This, done);
1989 OSSpinLockLock(&This->lock);
1991 if(!This->getbuf_last){
1992 OSSpinLockUnlock(&This->lock);
1993 return AUDCLNT_E_OUT_OF_ORDER;
1996 pbuf_frames = This->public_buffer->mAudioDataByteSize / This->fmt->nBlockAlign;
1997 if(done != 0 && done != pbuf_frames){
1998 OSSpinLockUnlock(&This->lock);
1999 return AUDCLNT_E_INVALID_SIZE;
2002 if(done){
2003 sc = AudioQueueEnqueueBuffer(This->aqueue, This->public_buffer,
2004 0, NULL);
2005 if(sc != noErr)
2006 WARN("Unable to enqueue buffer: %lx\n", sc);
2007 This->public_buffer = NULL;
2010 This->getbuf_last = 0;
2012 OSSpinLockUnlock(&This->lock);
2014 return S_OK;
2017 static HRESULT WINAPI AudioCaptureClient_GetNextPacketSize(
2018 IAudioCaptureClient *iface, UINT32 *frames)
2020 ACImpl *This = impl_from_IAudioCaptureClient(iface);
2021 struct list *head;
2022 AQBuffer *buf;
2024 TRACE("(%p)->(%p)\n", This, frames);
2026 if(!frames)
2027 return E_POINTER;
2029 OSSpinLockLock(&This->lock);
2031 head = list_head(&This->avail_buffers);
2033 if(!head){
2034 *frames = This->bufsize_frames / CAPTURE_BUFFERS;
2035 OSSpinLockUnlock(&This->lock);
2036 return S_OK;
2039 buf = LIST_ENTRY(head, AQBuffer, entry);
2040 *frames = buf->buf->mAudioDataByteSize / This->fmt->nBlockAlign;
2042 OSSpinLockUnlock(&This->lock);
2044 return S_OK;
2047 static const IAudioCaptureClientVtbl AudioCaptureClient_Vtbl =
2049 AudioCaptureClient_QueryInterface,
2050 AudioCaptureClient_AddRef,
2051 AudioCaptureClient_Release,
2052 AudioCaptureClient_GetBuffer,
2053 AudioCaptureClient_ReleaseBuffer,
2054 AudioCaptureClient_GetNextPacketSize
2057 static HRESULT WINAPI AudioClock_QueryInterface(IAudioClock *iface,
2058 REFIID riid, void **ppv)
2060 ACImpl *This = impl_from_IAudioClock(iface);
2062 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2064 if(!ppv)
2065 return E_POINTER;
2066 *ppv = NULL;
2068 if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IAudioClock))
2069 *ppv = iface;
2070 else if(IsEqualIID(riid, &IID_IAudioClock2))
2071 *ppv = &This->IAudioClock2_iface;
2072 if(*ppv){
2073 IUnknown_AddRef((IUnknown*)*ppv);
2074 return S_OK;
2077 WARN("Unknown interface %s\n", debugstr_guid(riid));
2078 return E_NOINTERFACE;
2081 static ULONG WINAPI AudioClock_AddRef(IAudioClock *iface)
2083 ACImpl *This = impl_from_IAudioClock(iface);
2084 return IAudioClient_AddRef(&This->IAudioClient_iface);
2087 static ULONG WINAPI AudioClock_Release(IAudioClock *iface)
2089 ACImpl *This = impl_from_IAudioClock(iface);
2090 return IAudioClient_Release(&This->IAudioClient_iface);
2093 static HRESULT WINAPI AudioClock_GetFrequency(IAudioClock *iface, UINT64 *freq)
2095 ACImpl *This = impl_from_IAudioClock(iface);
2097 TRACE("(%p)->(%p)\n", This, freq);
2099 *freq = This->fmt->nSamplesPerSec;
2101 return S_OK;
2104 static HRESULT AudioClock_GetPosition_nolock(ACImpl *This,
2105 UINT64 *pos, UINT64 *qpctime)
2107 if(This->dataflow == eRender)
2108 *pos = get_current_aqbuffer_position(This, BUFPOS_ABSOLUTE);
2109 else
2110 *pos = This->inbuf_frames + This->written_frames;
2112 if(qpctime){
2113 LARGE_INTEGER stamp, freq;
2114 QueryPerformanceCounter(&stamp);
2115 QueryPerformanceFrequency(&freq);
2116 *qpctime = (stamp.QuadPart * (INT64)10000000) / freq.QuadPart;
2119 return S_OK;
2122 static HRESULT WINAPI AudioClock_GetPosition(IAudioClock *iface, UINT64 *pos,
2123 UINT64 *qpctime)
2125 ACImpl *This = impl_from_IAudioClock(iface);
2126 HRESULT hr;
2128 TRACE("(%p)->(%p, %p)\n", This, pos, qpctime);
2130 if(!pos)
2131 return E_POINTER;
2133 OSSpinLockLock(&This->lock);
2135 hr = AudioClock_GetPosition_nolock(This, pos, qpctime);
2137 OSSpinLockUnlock(&This->lock);
2139 return hr;
2142 static HRESULT WINAPI AudioClock_GetCharacteristics(IAudioClock *iface,
2143 DWORD *chars)
2145 ACImpl *This = impl_from_IAudioClock(iface);
2147 TRACE("(%p)->(%p)\n", This, chars);
2149 if(!chars)
2150 return E_POINTER;
2152 *chars = AUDIOCLOCK_CHARACTERISTIC_FIXED_FREQ;
2154 return S_OK;
2157 static const IAudioClockVtbl AudioClock_Vtbl =
2159 AudioClock_QueryInterface,
2160 AudioClock_AddRef,
2161 AudioClock_Release,
2162 AudioClock_GetFrequency,
2163 AudioClock_GetPosition,
2164 AudioClock_GetCharacteristics
2167 static HRESULT WINAPI AudioClock2_QueryInterface(IAudioClock2 *iface,
2168 REFIID riid, void **ppv)
2170 ACImpl *This = impl_from_IAudioClock2(iface);
2171 return IAudioClock_QueryInterface(&This->IAudioClock_iface, riid, ppv);
2174 static ULONG WINAPI AudioClock2_AddRef(IAudioClock2 *iface)
2176 ACImpl *This = impl_from_IAudioClock2(iface);
2177 return IAudioClient_AddRef(&This->IAudioClient_iface);
2180 static ULONG WINAPI AudioClock2_Release(IAudioClock2 *iface)
2182 ACImpl *This = impl_from_IAudioClock2(iface);
2183 return IAudioClient_Release(&This->IAudioClient_iface);
2186 static HRESULT WINAPI AudioClock2_GetDevicePosition(IAudioClock2 *iface,
2187 UINT64 *pos, UINT64 *qpctime)
2189 ACImpl *This = impl_from_IAudioClock2(iface);
2191 FIXME("(%p)->(%p, %p)\n", This, pos, qpctime);
2193 return E_NOTIMPL;
2196 static const IAudioClock2Vtbl AudioClock2_Vtbl =
2198 AudioClock2_QueryInterface,
2199 AudioClock2_AddRef,
2200 AudioClock2_Release,
2201 AudioClock2_GetDevicePosition
2204 static AudioSessionWrapper *AudioSessionWrapper_Create(ACImpl *client)
2206 AudioSessionWrapper *ret;
2208 ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
2209 sizeof(AudioSessionWrapper));
2210 if(!ret)
2211 return NULL;
2213 ret->IAudioSessionControl2_iface.lpVtbl = &AudioSessionControl2_Vtbl;
2214 ret->ISimpleAudioVolume_iface.lpVtbl = &SimpleAudioVolume_Vtbl;
2215 ret->IChannelAudioVolume_iface.lpVtbl = &ChannelAudioVolume_Vtbl;
2217 ret->ref = 1;
2219 ret->client = client;
2220 if(client){
2221 ret->session = client->session;
2222 AudioClient_AddRef(&client->IAudioClient_iface);
2225 return ret;
2228 static HRESULT WINAPI AudioSessionControl_QueryInterface(
2229 IAudioSessionControl2 *iface, REFIID riid, void **ppv)
2231 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2233 if(!ppv)
2234 return E_POINTER;
2235 *ppv = NULL;
2237 if(IsEqualIID(riid, &IID_IUnknown) ||
2238 IsEqualIID(riid, &IID_IAudioSessionControl) ||
2239 IsEqualIID(riid, &IID_IAudioSessionControl2))
2240 *ppv = iface;
2241 if(*ppv){
2242 IUnknown_AddRef((IUnknown*)*ppv);
2243 return S_OK;
2246 WARN("Unknown interface %s\n", debugstr_guid(riid));
2247 return E_NOINTERFACE;
2250 static ULONG WINAPI AudioSessionControl_AddRef(IAudioSessionControl2 *iface)
2252 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2253 ULONG ref;
2254 ref = InterlockedIncrement(&This->ref);
2255 TRACE("(%p) Refcount now %u\n", This, ref);
2256 return ref;
2259 static ULONG WINAPI AudioSessionControl_Release(IAudioSessionControl2 *iface)
2261 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2262 ULONG ref;
2263 ref = InterlockedDecrement(&This->ref);
2264 TRACE("(%p) Refcount now %u\n", This, ref);
2265 if(!ref){
2266 if(This->client){
2267 OSSpinLockLock(&This->client->lock);
2268 This->client->session_wrapper = NULL;
2269 OSSpinLockUnlock(&This->client->lock);
2270 AudioClient_Release(&This->client->IAudioClient_iface);
2272 HeapFree(GetProcessHeap(), 0, This);
2274 return ref;
2277 static HRESULT WINAPI AudioSessionControl_GetState(IAudioSessionControl2 *iface,
2278 AudioSessionState *state)
2280 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2281 ACImpl *client;
2283 TRACE("(%p)->(%p)\n", This, state);
2285 if(!state)
2286 return NULL_PTR_ERR;
2288 EnterCriticalSection(&g_sessions_lock);
2290 if(list_empty(&This->session->clients)){
2291 *state = AudioSessionStateExpired;
2292 LeaveCriticalSection(&g_sessions_lock);
2293 return S_OK;
2296 LIST_FOR_EACH_ENTRY(client, &This->session->clients, ACImpl, entry){
2297 OSSpinLockLock(&client->lock);
2298 if(client->playing == StatePlaying ||
2299 client->playing == StateInTransition){
2300 *state = AudioSessionStateActive;
2301 OSSpinLockUnlock(&client->lock);
2302 LeaveCriticalSection(&g_sessions_lock);
2303 return S_OK;
2305 OSSpinLockUnlock(&client->lock);
2308 LeaveCriticalSection(&g_sessions_lock);
2310 *state = AudioSessionStateInactive;
2312 return S_OK;
2315 static HRESULT WINAPI AudioSessionControl_GetDisplayName(
2316 IAudioSessionControl2 *iface, WCHAR **name)
2318 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2320 FIXME("(%p)->(%p) - stub\n", This, name);
2322 return E_NOTIMPL;
2325 static HRESULT WINAPI AudioSessionControl_SetDisplayName(
2326 IAudioSessionControl2 *iface, const WCHAR *name, const GUID *session)
2328 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2330 FIXME("(%p)->(%p, %s) - stub\n", This, name, debugstr_guid(session));
2332 return E_NOTIMPL;
2335 static HRESULT WINAPI AudioSessionControl_GetIconPath(
2336 IAudioSessionControl2 *iface, WCHAR **path)
2338 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2340 FIXME("(%p)->(%p) - stub\n", This, path);
2342 return E_NOTIMPL;
2345 static HRESULT WINAPI AudioSessionControl_SetIconPath(
2346 IAudioSessionControl2 *iface, const WCHAR *path, const GUID *session)
2348 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2350 FIXME("(%p)->(%p, %s) - stub\n", This, path, debugstr_guid(session));
2352 return E_NOTIMPL;
2355 static HRESULT WINAPI AudioSessionControl_GetGroupingParam(
2356 IAudioSessionControl2 *iface, GUID *group)
2358 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2360 FIXME("(%p)->(%p) - stub\n", This, group);
2362 return E_NOTIMPL;
2365 static HRESULT WINAPI AudioSessionControl_SetGroupingParam(
2366 IAudioSessionControl2 *iface, const GUID *group, const GUID *session)
2368 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2370 FIXME("(%p)->(%s, %s) - stub\n", This, debugstr_guid(group),
2371 debugstr_guid(session));
2373 return E_NOTIMPL;
2376 static HRESULT WINAPI AudioSessionControl_RegisterAudioSessionNotification(
2377 IAudioSessionControl2 *iface, IAudioSessionEvents *events)
2379 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2381 FIXME("(%p)->(%p) - stub\n", This, events);
2383 return S_OK;
2386 static HRESULT WINAPI AudioSessionControl_UnregisterAudioSessionNotification(
2387 IAudioSessionControl2 *iface, IAudioSessionEvents *events)
2389 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2391 FIXME("(%p)->(%p) - stub\n", This, events);
2393 return S_OK;
2396 static HRESULT WINAPI AudioSessionControl_GetSessionIdentifier(
2397 IAudioSessionControl2 *iface, WCHAR **id)
2399 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2401 FIXME("(%p)->(%p) - stub\n", This, id);
2403 return E_NOTIMPL;
2406 static HRESULT WINAPI AudioSessionControl_GetSessionInstanceIdentifier(
2407 IAudioSessionControl2 *iface, WCHAR **id)
2409 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2411 FIXME("(%p)->(%p) - stub\n", This, id);
2413 return E_NOTIMPL;
2416 static HRESULT WINAPI AudioSessionControl_GetProcessId(
2417 IAudioSessionControl2 *iface, DWORD *pid)
2419 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2421 TRACE("(%p)->(%p)\n", This, pid);
2423 if(!pid)
2424 return E_POINTER;
2426 *pid = GetCurrentProcessId();
2428 return S_OK;
2431 static HRESULT WINAPI AudioSessionControl_IsSystemSoundsSession(
2432 IAudioSessionControl2 *iface)
2434 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2436 TRACE("(%p)\n", This);
2438 return S_FALSE;
2441 static HRESULT WINAPI AudioSessionControl_SetDuckingPreference(
2442 IAudioSessionControl2 *iface, BOOL optout)
2444 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2446 TRACE("(%p)->(%d)\n", This, optout);
2448 return S_OK;
2451 static const IAudioSessionControl2Vtbl AudioSessionControl2_Vtbl =
2453 AudioSessionControl_QueryInterface,
2454 AudioSessionControl_AddRef,
2455 AudioSessionControl_Release,
2456 AudioSessionControl_GetState,
2457 AudioSessionControl_GetDisplayName,
2458 AudioSessionControl_SetDisplayName,
2459 AudioSessionControl_GetIconPath,
2460 AudioSessionControl_SetIconPath,
2461 AudioSessionControl_GetGroupingParam,
2462 AudioSessionControl_SetGroupingParam,
2463 AudioSessionControl_RegisterAudioSessionNotification,
2464 AudioSessionControl_UnregisterAudioSessionNotification,
2465 AudioSessionControl_GetSessionIdentifier,
2466 AudioSessionControl_GetSessionInstanceIdentifier,
2467 AudioSessionControl_GetProcessId,
2468 AudioSessionControl_IsSystemSoundsSession,
2469 AudioSessionControl_SetDuckingPreference
2472 /* index == -1 means set all channels, otherwise sets only the given channel */
2473 static HRESULT ca_setvol(ACImpl *This, UINT32 index)
2475 float level;
2476 OSStatus sc;
2478 if(index == (UINT32)-1){
2479 HRESULT ret = S_OK;
2480 UINT32 i;
2481 for(i = 0; i < This->fmt->nChannels; ++i){
2482 HRESULT hr;
2483 hr = ca_setvol(This, i);
2484 if(FAILED(hr))
2485 ret = hr;
2487 return ret;
2490 if(This->session->mute)
2491 level = 0;
2492 else
2493 level = This->session->master_vol *
2494 This->session->channel_vols[index] * This->vols[index];
2496 sc = AudioQueueSetParameter(This->aqueue, kAudioQueueParam_Volume, level);
2497 if(sc != noErr)
2498 WARN("Setting _Volume property failed: %lx\n", sc);
2500 return S_OK;
2503 static HRESULT ca_session_setvol(AudioSession *session, UINT32 index)
2505 HRESULT ret = S_OK;
2506 ACImpl *client;
2508 LIST_FOR_EACH_ENTRY(client, &session->clients, ACImpl, entry){
2509 HRESULT hr;
2510 hr = ca_setvol(client, index);
2511 if(FAILED(hr))
2512 ret = hr;
2515 return ret;
2518 static HRESULT WINAPI SimpleAudioVolume_QueryInterface(
2519 ISimpleAudioVolume *iface, REFIID riid, void **ppv)
2521 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2523 if(!ppv)
2524 return E_POINTER;
2525 *ppv = NULL;
2527 if(IsEqualIID(riid, &IID_IUnknown) ||
2528 IsEqualIID(riid, &IID_ISimpleAudioVolume))
2529 *ppv = iface;
2530 if(*ppv){
2531 IUnknown_AddRef((IUnknown*)*ppv);
2532 return S_OK;
2535 WARN("Unknown interface %s\n", debugstr_guid(riid));
2536 return E_NOINTERFACE;
2539 static ULONG WINAPI SimpleAudioVolume_AddRef(ISimpleAudioVolume *iface)
2541 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2542 return AudioSessionControl_AddRef(&This->IAudioSessionControl2_iface);
2545 static ULONG WINAPI SimpleAudioVolume_Release(ISimpleAudioVolume *iface)
2547 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2548 return AudioSessionControl_Release(&This->IAudioSessionControl2_iface);
2551 static HRESULT WINAPI SimpleAudioVolume_SetMasterVolume(
2552 ISimpleAudioVolume *iface, float level, const GUID *context)
2554 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2555 AudioSession *session = This->session;
2556 HRESULT ret;
2558 TRACE("(%p)->(%f, %s)\n", session, level, wine_dbgstr_guid(context));
2560 if(level < 0.f || level > 1.f)
2561 return E_INVALIDARG;
2563 if(context)
2564 FIXME("Notifications not supported yet\n");
2566 EnterCriticalSection(&session->lock);
2568 session->master_vol = level;
2570 ret = ca_session_setvol(session, -1);
2572 LeaveCriticalSection(&session->lock);
2574 return ret;
2577 static HRESULT WINAPI SimpleAudioVolume_GetMasterVolume(
2578 ISimpleAudioVolume *iface, float *level)
2580 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2581 AudioSession *session = This->session;
2583 TRACE("(%p)->(%p)\n", session, level);
2585 if(!level)
2586 return NULL_PTR_ERR;
2588 *level = session->master_vol;
2590 return S_OK;
2593 static HRESULT WINAPI SimpleAudioVolume_SetMute(ISimpleAudioVolume *iface,
2594 BOOL mute, const GUID *context)
2596 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2597 AudioSession *session = This->session;
2599 TRACE("(%p)->(%u, %p)\n", session, mute, context);
2601 if(context)
2602 FIXME("Notifications not supported yet\n");
2604 EnterCriticalSection(&session->lock);
2606 session->mute = mute;
2608 ca_session_setvol(session, -1);
2610 LeaveCriticalSection(&session->lock);
2612 return S_OK;
2615 static HRESULT WINAPI SimpleAudioVolume_GetMute(ISimpleAudioVolume *iface,
2616 BOOL *mute)
2618 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2619 AudioSession *session = This->session;
2621 TRACE("(%p)->(%p)\n", session, mute);
2623 if(!mute)
2624 return NULL_PTR_ERR;
2626 *mute = session->mute;
2628 return S_OK;
2631 static const ISimpleAudioVolumeVtbl SimpleAudioVolume_Vtbl =
2633 SimpleAudioVolume_QueryInterface,
2634 SimpleAudioVolume_AddRef,
2635 SimpleAudioVolume_Release,
2636 SimpleAudioVolume_SetMasterVolume,
2637 SimpleAudioVolume_GetMasterVolume,
2638 SimpleAudioVolume_SetMute,
2639 SimpleAudioVolume_GetMute
2642 static HRESULT WINAPI AudioStreamVolume_QueryInterface(
2643 IAudioStreamVolume *iface, REFIID riid, void **ppv)
2645 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2647 if(!ppv)
2648 return E_POINTER;
2649 *ppv = NULL;
2651 if(IsEqualIID(riid, &IID_IUnknown) ||
2652 IsEqualIID(riid, &IID_IAudioStreamVolume))
2653 *ppv = iface;
2654 if(*ppv){
2655 IUnknown_AddRef((IUnknown*)*ppv);
2656 return S_OK;
2659 WARN("Unknown interface %s\n", debugstr_guid(riid));
2660 return E_NOINTERFACE;
2663 static ULONG WINAPI AudioStreamVolume_AddRef(IAudioStreamVolume *iface)
2665 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2666 return IAudioClient_AddRef(&This->IAudioClient_iface);
2669 static ULONG WINAPI AudioStreamVolume_Release(IAudioStreamVolume *iface)
2671 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2672 return IAudioClient_Release(&This->IAudioClient_iface);
2675 static HRESULT WINAPI AudioStreamVolume_GetChannelCount(
2676 IAudioStreamVolume *iface, UINT32 *out)
2678 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2680 TRACE("(%p)->(%p)\n", This, out);
2682 if(!out)
2683 return E_POINTER;
2685 *out = This->fmt->nChannels;
2687 return S_OK;
2690 static HRESULT WINAPI AudioStreamVolume_SetChannelVolume(
2691 IAudioStreamVolume *iface, UINT32 index, float level)
2693 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2694 HRESULT ret;
2696 TRACE("(%p)->(%d, %f)\n", This, index, level);
2698 if(level < 0.f || level > 1.f)
2699 return E_INVALIDARG;
2701 if(index >= This->fmt->nChannels)
2702 return E_INVALIDARG;
2704 OSSpinLockLock(&This->lock);
2706 This->vols[index] = level;
2708 WARN("AudioQueue doesn't support per-channel volume control\n");
2709 ret = ca_setvol(This, index);
2711 OSSpinLockUnlock(&This->lock);
2713 return ret;
2716 static HRESULT WINAPI AudioStreamVolume_GetChannelVolume(
2717 IAudioStreamVolume *iface, UINT32 index, float *level)
2719 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2721 TRACE("(%p)->(%d, %p)\n", This, index, level);
2723 if(!level)
2724 return E_POINTER;
2726 if(index >= This->fmt->nChannels)
2727 return E_INVALIDARG;
2729 *level = This->vols[index];
2731 return S_OK;
2734 static HRESULT WINAPI AudioStreamVolume_SetAllVolumes(
2735 IAudioStreamVolume *iface, UINT32 count, const float *levels)
2737 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2738 int i;
2739 HRESULT ret;
2741 TRACE("(%p)->(%d, %p)\n", This, count, levels);
2743 if(!levels)
2744 return E_POINTER;
2746 if(count != This->fmt->nChannels)
2747 return E_INVALIDARG;
2749 OSSpinLockLock(&This->lock);
2751 for(i = 0; i < count; ++i)
2752 This->vols[i] = levels[i];
2754 ret = ca_setvol(This, -1);
2756 OSSpinLockUnlock(&This->lock);
2758 return ret;
2761 static HRESULT WINAPI AudioStreamVolume_GetAllVolumes(
2762 IAudioStreamVolume *iface, UINT32 count, float *levels)
2764 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2765 int i;
2767 TRACE("(%p)->(%d, %p)\n", This, count, levels);
2769 if(!levels)
2770 return E_POINTER;
2772 if(count != This->fmt->nChannels)
2773 return E_INVALIDARG;
2775 OSSpinLockLock(&This->lock);
2777 for(i = 0; i < count; ++i)
2778 levels[i] = This->vols[i];
2780 OSSpinLockUnlock(&This->lock);
2782 return S_OK;
2785 static const IAudioStreamVolumeVtbl AudioStreamVolume_Vtbl =
2787 AudioStreamVolume_QueryInterface,
2788 AudioStreamVolume_AddRef,
2789 AudioStreamVolume_Release,
2790 AudioStreamVolume_GetChannelCount,
2791 AudioStreamVolume_SetChannelVolume,
2792 AudioStreamVolume_GetChannelVolume,
2793 AudioStreamVolume_SetAllVolumes,
2794 AudioStreamVolume_GetAllVolumes
2797 static HRESULT WINAPI ChannelAudioVolume_QueryInterface(
2798 IChannelAudioVolume *iface, REFIID riid, void **ppv)
2800 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2802 if(!ppv)
2803 return E_POINTER;
2804 *ppv = NULL;
2806 if(IsEqualIID(riid, &IID_IUnknown) ||
2807 IsEqualIID(riid, &IID_IChannelAudioVolume))
2808 *ppv = iface;
2809 if(*ppv){
2810 IUnknown_AddRef((IUnknown*)*ppv);
2811 return S_OK;
2814 WARN("Unknown interface %s\n", debugstr_guid(riid));
2815 return E_NOINTERFACE;
2818 static ULONG WINAPI ChannelAudioVolume_AddRef(IChannelAudioVolume *iface)
2820 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2821 return AudioSessionControl_AddRef(&This->IAudioSessionControl2_iface);
2824 static ULONG WINAPI ChannelAudioVolume_Release(IChannelAudioVolume *iface)
2826 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2827 return AudioSessionControl_Release(&This->IAudioSessionControl2_iface);
2830 static HRESULT WINAPI ChannelAudioVolume_GetChannelCount(
2831 IChannelAudioVolume *iface, UINT32 *out)
2833 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2834 AudioSession *session = This->session;
2836 TRACE("(%p)->(%p)\n", session, out);
2838 if(!out)
2839 return NULL_PTR_ERR;
2841 *out = session->channel_count;
2843 return S_OK;
2846 static HRESULT WINAPI ChannelAudioVolume_SetChannelVolume(
2847 IChannelAudioVolume *iface, UINT32 index, float level,
2848 const GUID *context)
2850 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2851 AudioSession *session = This->session;
2852 HRESULT ret;
2854 TRACE("(%p)->(%d, %f, %s)\n", session, index, level,
2855 wine_dbgstr_guid(context));
2857 if(level < 0.f || level > 1.f)
2858 return E_INVALIDARG;
2860 if(index >= session->channel_count)
2861 return E_INVALIDARG;
2863 if(context)
2864 FIXME("Notifications not supported yet\n");
2866 EnterCriticalSection(&session->lock);
2868 session->channel_vols[index] = level;
2870 WARN("AudioQueue doesn't support per-channel volume control\n");
2871 ret = ca_session_setvol(session, index);
2873 LeaveCriticalSection(&session->lock);
2875 return ret;
2878 static HRESULT WINAPI ChannelAudioVolume_GetChannelVolume(
2879 IChannelAudioVolume *iface, UINT32 index, float *level)
2881 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2882 AudioSession *session = This->session;
2884 TRACE("(%p)->(%d, %p)\n", session, index, level);
2886 if(!level)
2887 return NULL_PTR_ERR;
2889 if(index >= session->channel_count)
2890 return E_INVALIDARG;
2892 *level = session->channel_vols[index];
2894 return S_OK;
2897 static HRESULT WINAPI ChannelAudioVolume_SetAllVolumes(
2898 IChannelAudioVolume *iface, UINT32 count, const float *levels,
2899 const GUID *context)
2901 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2902 AudioSession *session = This->session;
2903 int i;
2904 HRESULT ret;
2906 TRACE("(%p)->(%d, %p, %s)\n", session, count, levels,
2907 wine_dbgstr_guid(context));
2909 if(!levels)
2910 return NULL_PTR_ERR;
2912 if(count != session->channel_count)
2913 return E_INVALIDARG;
2915 if(context)
2916 FIXME("Notifications not supported yet\n");
2918 EnterCriticalSection(&session->lock);
2920 for(i = 0; i < count; ++i)
2921 session->channel_vols[i] = levels[i];
2923 ret = ca_session_setvol(session, -1);
2925 LeaveCriticalSection(&session->lock);
2927 return ret;
2930 static HRESULT WINAPI ChannelAudioVolume_GetAllVolumes(
2931 IChannelAudioVolume *iface, UINT32 count, float *levels)
2933 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2934 AudioSession *session = This->session;
2935 int i;
2937 TRACE("(%p)->(%d, %p)\n", session, count, levels);
2939 if(!levels)
2940 return NULL_PTR_ERR;
2942 if(count != session->channel_count)
2943 return E_INVALIDARG;
2945 for(i = 0; i < count; ++i)
2946 levels[i] = session->channel_vols[i];
2948 return S_OK;
2951 static const IChannelAudioVolumeVtbl ChannelAudioVolume_Vtbl =
2953 ChannelAudioVolume_QueryInterface,
2954 ChannelAudioVolume_AddRef,
2955 ChannelAudioVolume_Release,
2956 ChannelAudioVolume_GetChannelCount,
2957 ChannelAudioVolume_SetChannelVolume,
2958 ChannelAudioVolume_GetChannelVolume,
2959 ChannelAudioVolume_SetAllVolumes,
2960 ChannelAudioVolume_GetAllVolumes
2963 static HRESULT WINAPI AudioSessionManager_QueryInterface(IAudioSessionManager2 *iface,
2964 REFIID riid, void **ppv)
2966 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2968 if(!ppv)
2969 return E_POINTER;
2970 *ppv = NULL;
2972 if(IsEqualIID(riid, &IID_IUnknown) ||
2973 IsEqualIID(riid, &IID_IAudioSessionManager) ||
2974 IsEqualIID(riid, &IID_IAudioSessionManager2))
2975 *ppv = iface;
2976 if(*ppv){
2977 IUnknown_AddRef((IUnknown*)*ppv);
2978 return S_OK;
2981 WARN("Unknown interface %s\n", debugstr_guid(riid));
2982 return E_NOINTERFACE;
2985 static ULONG WINAPI AudioSessionManager_AddRef(IAudioSessionManager2 *iface)
2987 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
2988 ULONG ref;
2989 ref = InterlockedIncrement(&This->ref);
2990 TRACE("(%p) Refcount now %u\n", This, ref);
2991 return ref;
2994 static ULONG WINAPI AudioSessionManager_Release(IAudioSessionManager2 *iface)
2996 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
2997 ULONG ref;
2998 ref = InterlockedDecrement(&This->ref);
2999 TRACE("(%p) Refcount now %u\n", This, ref);
3000 if(!ref)
3001 HeapFree(GetProcessHeap(), 0, This);
3002 return ref;
3005 static HRESULT WINAPI AudioSessionManager_GetAudioSessionControl(
3006 IAudioSessionManager2 *iface, const GUID *session_guid, DWORD flags,
3007 IAudioSessionControl **out)
3009 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
3010 AudioSession *session;
3011 AudioSessionWrapper *wrapper;
3012 HRESULT hr;
3014 TRACE("(%p)->(%s, %x, %p)\n", This, debugstr_guid(session_guid),
3015 flags, out);
3017 hr = get_audio_session(session_guid, This->device, 0, &session);
3018 if(FAILED(hr))
3019 return hr;
3021 wrapper = AudioSessionWrapper_Create(NULL);
3022 if(!wrapper)
3023 return E_OUTOFMEMORY;
3025 wrapper->session = session;
3027 *out = (IAudioSessionControl*)&wrapper->IAudioSessionControl2_iface;
3029 return S_OK;
3032 static HRESULT WINAPI AudioSessionManager_GetSimpleAudioVolume(
3033 IAudioSessionManager2 *iface, const GUID *session_guid, DWORD flags,
3034 ISimpleAudioVolume **out)
3036 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
3037 AudioSession *session;
3038 AudioSessionWrapper *wrapper;
3039 HRESULT hr;
3041 TRACE("(%p)->(%s, %x, %p)\n", This, debugstr_guid(session_guid),
3042 flags, out);
3044 hr = get_audio_session(session_guid, This->device, 0, &session);
3045 if(FAILED(hr))
3046 return hr;
3048 wrapper = AudioSessionWrapper_Create(NULL);
3049 if(!wrapper)
3050 return E_OUTOFMEMORY;
3052 wrapper->session = session;
3054 *out = &wrapper->ISimpleAudioVolume_iface;
3056 return S_OK;
3059 static HRESULT WINAPI AudioSessionManager_GetSessionEnumerator(
3060 IAudioSessionManager2 *iface, IAudioSessionEnumerator **out)
3062 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
3063 FIXME("(%p)->(%p) - stub\n", This, out);
3064 return E_NOTIMPL;
3067 static HRESULT WINAPI AudioSessionManager_RegisterSessionNotification(
3068 IAudioSessionManager2 *iface, IAudioSessionNotification *notification)
3070 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
3071 FIXME("(%p)->(%p) - stub\n", This, notification);
3072 return E_NOTIMPL;
3075 static HRESULT WINAPI AudioSessionManager_UnregisterSessionNotification(
3076 IAudioSessionManager2 *iface, IAudioSessionNotification *notification)
3078 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
3079 FIXME("(%p)->(%p) - stub\n", This, notification);
3080 return E_NOTIMPL;
3083 static HRESULT WINAPI AudioSessionManager_RegisterDuckNotification(
3084 IAudioSessionManager2 *iface, const WCHAR *session_id,
3085 IAudioVolumeDuckNotification *notification)
3087 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
3088 FIXME("(%p)->(%p) - stub\n", This, notification);
3089 return E_NOTIMPL;
3092 static HRESULT WINAPI AudioSessionManager_UnregisterDuckNotification(
3093 IAudioSessionManager2 *iface,
3094 IAudioVolumeDuckNotification *notification)
3096 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
3097 FIXME("(%p)->(%p) - stub\n", This, notification);
3098 return E_NOTIMPL;
3101 static const IAudioSessionManager2Vtbl AudioSessionManager2_Vtbl =
3103 AudioSessionManager_QueryInterface,
3104 AudioSessionManager_AddRef,
3105 AudioSessionManager_Release,
3106 AudioSessionManager_GetAudioSessionControl,
3107 AudioSessionManager_GetSimpleAudioVolume,
3108 AudioSessionManager_GetSessionEnumerator,
3109 AudioSessionManager_RegisterSessionNotification,
3110 AudioSessionManager_UnregisterSessionNotification,
3111 AudioSessionManager_RegisterDuckNotification,
3112 AudioSessionManager_UnregisterDuckNotification
3115 HRESULT WINAPI AUDDRV_GetAudioSessionManager(IMMDevice *device,
3116 IAudioSessionManager2 **out)
3118 SessionMgr *This;
3120 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(SessionMgr));
3121 if(!This)
3122 return E_OUTOFMEMORY;
3124 This->IAudioSessionManager2_iface.lpVtbl = &AudioSessionManager2_Vtbl;
3125 This->device = device;
3126 This->ref = 1;
3128 *out = &This->IAudioSessionManager2_iface;
3130 return S_OK;