mmdevapi: Enforce limits on period and duration.
[wine/multimedia.git] / dlls / winecoreaudio.drv / mmdevdrv.c
bloba2c72dae13c38a42ca83d91e4b3a93863ab56a78
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 if(mode == AUDCLNT_SHAREMODE_SHARED){
965 period = DefaultPeriod;
966 if( duration < 3 * period)
967 duration = 3 * period;
968 }else{
969 if(!period)
970 period = DefaultPeriod; /* not minimum */
971 if(period < MinimumPeriod || period > 5000000)
972 return AUDCLNT_E_INVALID_DEVICE_PERIOD;
973 if(duration > 20000000) /* the smaller the period, the lower this limit */
974 return AUDCLNT_E_BUFFER_SIZE_ERROR;
975 if(flags & AUDCLNT_STREAMFLAGS_EVENTCALLBACK){
976 if(duration != period)
977 return AUDCLNT_E_BUFDURATION_PERIOD_NOT_EQUAL;
978 FIXME("EXCLUSIVE mode with EVENTCALLBACK\n");
979 return AUDCLNT_E_DEVICE_IN_USE;
980 }else{
981 if( duration < 8 * period)
982 duration = 8 * period; /* may grow above 2s */
986 OSSpinLockLock(&This->lock);
988 if(This->aqueue){
989 OSSpinLockUnlock(&This->lock);
990 return AUDCLNT_E_ALREADY_INITIALIZED;
993 hr = ca_setup_aqueue(This->adevid, This->dataflow, fmt, This, &This->aqueue);
994 if(FAILED(hr)){
995 OSSpinLockUnlock(&This->lock);
996 return hr;
999 This->fmt = clone_format(fmt);
1000 if(!This->fmt){
1001 AudioQueueDispose(This->aqueue, 1);
1002 This->aqueue = NULL;
1003 OSSpinLockUnlock(&This->lock);
1004 return E_OUTOFMEMORY;
1007 This->period_ms = period / 10000;
1009 This->bufsize_frames = ceil(fmt->nSamplesPerSec * (duration / 10000000.));
1011 if(This->dataflow == eCapture){
1012 int i;
1013 UInt32 bsize = ceil((This->bufsize_frames / (double)CAPTURE_BUFFERS) *
1014 This->fmt->nBlockAlign);
1015 for(i = 0; i < CAPTURE_BUFFERS; ++i){
1016 AQBuffer *buf;
1018 buf = HeapAlloc(GetProcessHeap(), 0, sizeof(AQBuffer));
1019 if(!buf){
1020 AudioQueueDispose(This->aqueue, 1);
1021 This->aqueue = NULL;
1022 CoTaskMemFree(This->fmt);
1023 This->fmt = NULL;
1024 OSSpinLockUnlock(&This->lock);
1025 return E_OUTOFMEMORY;
1028 sc = AudioQueueAllocateBuffer(This->aqueue, bsize, &buf->buf);
1029 if(sc != noErr){
1030 AudioQueueDispose(This->aqueue, 1);
1031 This->aqueue = NULL;
1032 CoTaskMemFree(This->fmt);
1033 This->fmt = NULL;
1034 OSSpinLockUnlock(&This->lock);
1035 WARN("Couldn't allocate buffer: %lx\n", sc);
1036 return E_FAIL;
1039 buf->buf->mUserData = buf;
1041 sc = AudioQueueEnqueueBuffer(This->aqueue, buf->buf, 0, NULL);
1042 if(sc != noErr){
1043 ERR("Couldn't enqueue buffer: %lx\n", sc);
1044 break;
1049 This->vols = HeapAlloc(GetProcessHeap(), 0, fmt->nChannels * sizeof(float));
1050 if(!This->vols){
1051 AudioQueueDispose(This->aqueue, 1);
1052 This->aqueue = NULL;
1053 CoTaskMemFree(This->fmt);
1054 This->fmt = NULL;
1055 OSSpinLockUnlock(&This->lock);
1056 return E_OUTOFMEMORY;
1059 for(i = 0; i < fmt->nChannels; ++i)
1060 This->vols[i] = 1.f;
1062 This->share = mode;
1063 This->flags = flags;
1065 EnterCriticalSection(&g_sessions_lock);
1067 hr = get_audio_session(sessionguid, This->parent, fmt->nChannels,
1068 &This->session);
1069 if(FAILED(hr)){
1070 LeaveCriticalSection(&g_sessions_lock);
1071 AudioQueueDispose(This->aqueue, 1);
1072 This->aqueue = NULL;
1073 CoTaskMemFree(This->fmt);
1074 This->fmt = NULL;
1075 HeapFree(GetProcessHeap(), 0, This->vols);
1076 This->vols = NULL;
1077 OSSpinLockUnlock(&This->lock);
1078 return E_INVALIDARG;
1081 list_add_tail(&This->session->clients, &This->entry);
1083 LeaveCriticalSection(&g_sessions_lock);
1085 ca_setvol(This, -1);
1087 OSSpinLockUnlock(&This->lock);
1089 return S_OK;
1092 static HRESULT WINAPI AudioClient_GetBufferSize(IAudioClient *iface,
1093 UINT32 *frames)
1095 ACImpl *This = impl_from_IAudioClient(iface);
1097 TRACE("(%p)->(%p)\n", This, frames);
1099 if(!frames)
1100 return E_POINTER;
1102 OSSpinLockLock(&This->lock);
1104 if(!This->aqueue){
1105 OSSpinLockUnlock(&This->lock);
1106 return AUDCLNT_E_NOT_INITIALIZED;
1109 *frames = This->bufsize_frames;
1111 OSSpinLockUnlock(&This->lock);
1113 return S_OK;
1116 static HRESULT ca_get_max_stream_latency(ACImpl *This, UInt32 *max)
1118 AudioObjectPropertyAddress addr;
1119 AudioStreamID *ids;
1120 UInt32 size;
1121 OSStatus sc;
1122 int nstreams, i;
1124 addr.mScope = This->scope;
1125 addr.mElement = 0;
1126 addr.mSelector = kAudioDevicePropertyStreams;
1128 sc = AudioObjectGetPropertyDataSize(This->adevid, &addr, 0, NULL,
1129 &size);
1130 if(sc != noErr){
1131 WARN("Unable to get size for _Streams property: %lx\n", sc);
1132 return E_FAIL;
1135 ids = HeapAlloc(GetProcessHeap(), 0, size);
1136 if(!ids)
1137 return E_OUTOFMEMORY;
1139 sc = AudioObjectGetPropertyData(This->adevid, &addr, 0, NULL, &size, ids);
1140 if(sc != noErr){
1141 WARN("Unable to get _Streams property: %lx\n", sc);
1142 HeapFree(GetProcessHeap(), 0, ids);
1143 return E_FAIL;
1146 nstreams = size / sizeof(AudioStreamID);
1147 *max = 0;
1149 addr.mSelector = kAudioStreamPropertyLatency;
1150 for(i = 0; i < nstreams; ++i){
1151 UInt32 latency;
1153 size = sizeof(latency);
1154 sc = AudioObjectGetPropertyData(ids[i], &addr, 0, NULL,
1155 &size, &latency);
1156 if(sc != noErr){
1157 WARN("Unable to get _Latency property: %lx\n", sc);
1158 continue;
1161 if(latency > *max)
1162 *max = latency;
1165 HeapFree(GetProcessHeap(), 0, ids);
1167 return S_OK;
1170 static HRESULT WINAPI AudioClient_GetStreamLatency(IAudioClient *iface,
1171 REFERENCE_TIME *out)
1173 ACImpl *This = impl_from_IAudioClient(iface);
1174 UInt32 latency, stream_latency, size;
1175 AudioObjectPropertyAddress addr;
1176 OSStatus sc;
1177 HRESULT hr;
1179 TRACE("(%p)->(%p)\n", This, out);
1181 if(!out)
1182 return E_POINTER;
1184 OSSpinLockLock(&This->lock);
1186 if(!This->aqueue){
1187 OSSpinLockUnlock(&This->lock);
1188 return AUDCLNT_E_NOT_INITIALIZED;
1191 addr.mScope = This->scope;
1192 addr.mSelector = kAudioDevicePropertyLatency;
1193 addr.mElement = 0;
1195 size = sizeof(latency);
1196 sc = AudioObjectGetPropertyData(This->adevid, &addr, 0, NULL,
1197 &size, &latency);
1198 if(sc != noErr){
1199 WARN("Couldn't get _Latency property: %lx\n", sc);
1200 OSSpinLockUnlock(&This->lock);
1201 return E_FAIL;
1204 hr = ca_get_max_stream_latency(This, &stream_latency);
1205 if(FAILED(hr)){
1206 OSSpinLockUnlock(&This->lock);
1207 return hr;
1210 latency += stream_latency;
1211 /* pretend we process audio in Period chunks, so max latency includes
1212 * the period time */
1213 latency += DefaultPeriod;
1214 *out = (latency / (double)This->fmt->nSamplesPerSec) * 10000000;
1216 OSSpinLockUnlock(&This->lock);
1218 return S_OK;
1221 static HRESULT AudioClient_GetCurrentPadding_nolock(ACImpl *This,
1222 UINT32 *numpad)
1224 if(!This->aqueue)
1225 return AUDCLNT_E_NOT_INITIALIZED;
1227 if(This->dataflow == eRender){
1228 UINT64 bufpos;
1229 bufpos = get_current_aqbuffer_position(This, BUFPOS_RELATIVE);
1230 *numpad = This->inbuf_frames - bufpos;
1231 }else
1232 *numpad = This->inbuf_frames;
1234 return S_OK;
1237 static HRESULT WINAPI AudioClient_GetCurrentPadding(IAudioClient *iface,
1238 UINT32 *numpad)
1240 ACImpl *This = impl_from_IAudioClient(iface);
1241 HRESULT hr;
1243 TRACE("(%p)->(%p)\n", This, numpad);
1245 if(!numpad)
1246 return E_POINTER;
1248 OSSpinLockLock(&This->lock);
1250 hr = AudioClient_GetCurrentPadding_nolock(This, numpad);
1252 OSSpinLockUnlock(&This->lock);
1254 return hr;
1257 static HRESULT WINAPI AudioClient_IsFormatSupported(IAudioClient *iface,
1258 AUDCLNT_SHAREMODE mode, const WAVEFORMATEX *pwfx,
1259 WAVEFORMATEX **outpwfx)
1261 ACImpl *This = impl_from_IAudioClient(iface);
1262 WAVEFORMATEXTENSIBLE *fmtex = (WAVEFORMATEXTENSIBLE*)pwfx;
1263 AudioQueueRef aqueue;
1264 HRESULT hr;
1266 TRACE("(%p)->(%x, %p, %p)\n", This, mode, pwfx, outpwfx);
1268 if(!pwfx || (mode == AUDCLNT_SHAREMODE_SHARED && !outpwfx))
1269 return E_POINTER;
1271 if(mode != AUDCLNT_SHAREMODE_SHARED && mode != AUDCLNT_SHAREMODE_EXCLUSIVE)
1272 return E_INVALIDARG;
1274 if(pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
1275 pwfx->cbSize < sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX))
1276 return E_INVALIDARG;
1278 dump_fmt(pwfx);
1280 if(outpwfx)
1281 *outpwfx = NULL;
1283 if(pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
1284 fmtex->dwChannelMask != 0 &&
1285 fmtex->dwChannelMask != get_channel_mask(pwfx->nChannels))
1286 return AUDCLNT_E_UNSUPPORTED_FORMAT;
1288 OSSpinLockLock(&This->lock);
1290 hr = ca_setup_aqueue(This->adevid, This->dataflow, pwfx, NULL, &aqueue);
1291 if(SUCCEEDED(hr)){
1292 AudioQueueDispose(aqueue, 1);
1293 OSSpinLockUnlock(&This->lock);
1294 TRACE("returning %08x\n", S_OK);
1295 return S_OK;
1298 OSSpinLockUnlock(&This->lock);
1300 TRACE("returning %08x\n", AUDCLNT_E_UNSUPPORTED_FORMAT);
1301 return AUDCLNT_E_UNSUPPORTED_FORMAT;
1304 static HRESULT WINAPI AudioClient_GetMixFormat(IAudioClient *iface,
1305 WAVEFORMATEX **pwfx)
1307 ACImpl *This = impl_from_IAudioClient(iface);
1308 WAVEFORMATEXTENSIBLE *fmt;
1309 OSStatus sc;
1310 UInt32 size;
1311 Float64 rate;
1312 AudioBufferList *buffers;
1313 AudioObjectPropertyAddress addr;
1314 int i;
1316 TRACE("(%p)->(%p)\n", This, pwfx);
1318 if(!pwfx)
1319 return E_POINTER;
1320 *pwfx = NULL;
1322 fmt = CoTaskMemAlloc(sizeof(WAVEFORMATEXTENSIBLE));
1323 if(!fmt)
1324 return E_OUTOFMEMORY;
1326 fmt->Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
1328 addr.mScope = This->scope;
1329 addr.mElement = 0;
1330 addr.mSelector = kAudioDevicePropertyStreamConfiguration;
1332 sc = AudioObjectGetPropertyDataSize(This->adevid, &addr, 0, NULL, &size);
1333 if(sc != noErr){
1334 CoTaskMemFree(fmt);
1335 WARN("Unable to get size for _StreamConfiguration property: %lx\n", sc);
1336 return E_FAIL;
1339 buffers = HeapAlloc(GetProcessHeap(), 0, size);
1340 if(!buffers){
1341 CoTaskMemFree(fmt);
1342 return E_OUTOFMEMORY;
1345 sc = AudioObjectGetPropertyData(This->adevid, &addr, 0, NULL,
1346 &size, buffers);
1347 if(sc != noErr){
1348 CoTaskMemFree(fmt);
1349 HeapFree(GetProcessHeap(), 0, buffers);
1350 WARN("Unable to get _StreamConfiguration property: %lx\n", sc);
1351 return E_FAIL;
1354 fmt->Format.nChannels = 0;
1355 for(i = 0; i < buffers->mNumberBuffers; ++i)
1356 fmt->Format.nChannels += buffers->mBuffers[i].mNumberChannels;
1358 HeapFree(GetProcessHeap(), 0, buffers);
1360 fmt->dwChannelMask = get_channel_mask(fmt->Format.nChannels);
1362 addr.mSelector = kAudioDevicePropertyNominalSampleRate;
1363 size = sizeof(Float64);
1364 sc = AudioObjectGetPropertyData(This->adevid, &addr, 0, NULL, &size, &rate);
1365 if(sc != noErr){
1366 CoTaskMemFree(fmt);
1367 WARN("Unable to get _NominalSampleRate property: %lx\n", sc);
1368 return E_FAIL;
1370 fmt->Format.nSamplesPerSec = rate;
1372 fmt->Format.wBitsPerSample = 32;
1373 fmt->SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
1375 fmt->Format.nBlockAlign = (fmt->Format.wBitsPerSample *
1376 fmt->Format.nChannels) / 8;
1377 fmt->Format.nAvgBytesPerSec = fmt->Format.nSamplesPerSec *
1378 fmt->Format.nBlockAlign;
1380 fmt->Samples.wValidBitsPerSample = fmt->Format.wBitsPerSample;
1381 fmt->Format.cbSize = sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
1383 *pwfx = (WAVEFORMATEX*)fmt;
1384 dump_fmt(*pwfx);
1386 return S_OK;
1389 static HRESULT WINAPI AudioClient_GetDevicePeriod(IAudioClient *iface,
1390 REFERENCE_TIME *defperiod, REFERENCE_TIME *minperiod)
1392 ACImpl *This = impl_from_IAudioClient(iface);
1394 TRACE("(%p)->(%p, %p)\n", This, defperiod, minperiod);
1396 if(!defperiod && !minperiod)
1397 return E_POINTER;
1399 OSSpinLockLock(&This->lock);
1401 if(This->period_ms){
1402 if(defperiod)
1403 *defperiod = This->period_ms * 10000;
1404 if(minperiod)
1405 *minperiod = This->period_ms * 10000;
1406 }else{
1407 if(defperiod)
1408 *defperiod = DefaultPeriod;
1409 if(minperiod)
1410 *minperiod = MinimumPeriod;
1413 OSSpinLockUnlock(&This->lock);
1415 return S_OK;
1418 void CALLBACK ca_period_cb(void *user, BOOLEAN timer)
1420 ACImpl *This = user;
1422 OSSpinLockLock(&This->lock);
1423 if(This->event)
1424 SetEvent(This->event);
1425 OSSpinLockUnlock(&This->lock);
1428 static HRESULT WINAPI AudioClient_Start(IAudioClient *iface)
1430 ACImpl *This = impl_from_IAudioClient(iface);
1431 OSStatus sc;
1433 TRACE("(%p)\n", This);
1435 OSSpinLockLock(&This->lock);
1437 if(!This->aqueue){
1438 OSSpinLockUnlock(&This->lock);
1439 return AUDCLNT_E_NOT_INITIALIZED;
1442 if(This->playing != StateStopped){
1443 OSSpinLockUnlock(&This->lock);
1444 return AUDCLNT_E_NOT_STOPPED;
1447 if((This->flags & AUDCLNT_STREAMFLAGS_EVENTCALLBACK) && !This->event){
1448 OSSpinLockUnlock(&This->lock);
1449 return AUDCLNT_E_EVENTHANDLE_NOT_SET;
1452 if(This->event)
1453 if(!CreateTimerQueueTimer(&This->timer, g_timer_q,
1454 ca_period_cb, This, 0, This->period_ms, 0))
1455 ERR("Unable to create timer: %u\n", GetLastError());
1457 This->playing = StateInTransition;
1459 OSSpinLockUnlock(&This->lock);
1461 sc = AudioQueueStart(This->aqueue, NULL);
1462 if(sc != noErr){
1463 WARN("Unable to start audio queue: %lx\n", sc);
1464 return E_FAIL;
1467 OSSpinLockLock(&This->lock);
1469 This->playing = StatePlaying;
1471 OSSpinLockUnlock(&This->lock);
1473 return S_OK;
1476 static HRESULT WINAPI AudioClient_Stop(IAudioClient *iface)
1478 ACImpl *This = impl_from_IAudioClient(iface);
1479 AudioTimeStamp tstamp;
1480 OSStatus sc;
1482 TRACE("(%p)\n", This);
1484 OSSpinLockLock(&This->lock);
1486 if(!This->aqueue){
1487 OSSpinLockUnlock(&This->lock);
1488 return AUDCLNT_E_NOT_INITIALIZED;
1491 if(This->playing == StateStopped){
1492 OSSpinLockUnlock(&This->lock);
1493 return S_FALSE;
1496 if(This->playing == StateInTransition){
1497 OSSpinLockUnlock(&This->lock);
1498 return S_OK;
1501 if(This->timer && This->timer != INVALID_HANDLE_VALUE){
1502 DeleteTimerQueueTimer(g_timer_q, This->timer, INVALID_HANDLE_VALUE);
1503 This->timer = NULL;
1506 This->playing = StateInTransition;
1508 sc = AudioQueueGetCurrentTime(This->aqueue, NULL, &tstamp, NULL);
1509 if(sc == noErr){
1510 if(tstamp.mFlags & kAudioTimeStampSampleTimeValid){
1511 if(tstamp.mSampleTime > This->highest_sampletime)
1512 This->highest_sampletime = tstamp.mSampleTime;
1513 }else
1514 WARN("Returned tstamp mSampleTime not valid: %lx\n", tstamp.mFlags);
1515 }else
1516 WARN("GetCurrentTime failed: %lx\n", sc);
1518 OSSpinLockUnlock(&This->lock);
1520 sc = AudioQueueFlush(This->aqueue);
1521 if(sc != noErr)
1522 WARN("Unable to flush audio queue: %lx\n", sc);
1524 sc = AudioQueuePause(This->aqueue);
1525 if(sc != noErr){
1526 WARN("Unable to pause audio queue: %lx\n", sc);
1527 return E_FAIL;
1530 OSSpinLockLock(&This->lock);
1532 This->playing = StateStopped;
1534 OSSpinLockUnlock(&This->lock);
1536 return S_OK;
1539 static HRESULT WINAPI AudioClient_Reset(IAudioClient *iface)
1541 ACImpl *This = impl_from_IAudioClient(iface);
1542 OSStatus sc;
1543 QueuedBufInfo *bufinfo, *bufinfo2;
1545 TRACE("(%p)\n", This);
1547 OSSpinLockLock(&This->lock);
1549 if(!This->aqueue){
1550 OSSpinLockUnlock(&This->lock);
1551 return AUDCLNT_E_NOT_INITIALIZED;
1554 if(This->playing != StateStopped){
1555 OSSpinLockUnlock(&This->lock);
1556 return AUDCLNT_E_NOT_STOPPED;
1559 if(This->getbuf_last){
1560 OSSpinLockUnlock(&This->lock);
1561 return AUDCLNT_E_BUFFER_OPERATION_PENDING;
1564 This->written_frames = 0;
1565 This->inbuf_frames = 0;
1567 LIST_FOR_EACH_ENTRY_SAFE(bufinfo, bufinfo2, &This->queued_bufinfos,
1568 QueuedBufInfo, entry){
1569 list_remove(&bufinfo->entry);
1570 HeapFree(GetProcessHeap(), 0, bufinfo);
1573 OSSpinLockUnlock(&This->lock);
1575 sc = AudioQueueReset(This->aqueue);
1576 if(sc != noErr){
1577 WARN("Unable to reset audio queue: %lx\n", sc);
1578 return E_FAIL;
1581 return S_OK;
1584 static HRESULT WINAPI AudioClient_SetEventHandle(IAudioClient *iface,
1585 HANDLE event)
1587 ACImpl *This = impl_from_IAudioClient(iface);
1589 TRACE("(%p)->(%p)\n", This, event);
1591 if(!event)
1592 return E_INVALIDARG;
1594 OSSpinLockLock(&This->lock);
1596 if(!This->aqueue){
1597 OSSpinLockUnlock(&This->lock);
1598 return AUDCLNT_E_NOT_INITIALIZED;
1601 if(!(This->flags & AUDCLNT_STREAMFLAGS_EVENTCALLBACK)){
1602 OSSpinLockUnlock(&This->lock);
1603 return AUDCLNT_E_EVENTHANDLE_NOT_EXPECTED;
1606 This->event = event;
1608 OSSpinLockUnlock(&This->lock);
1610 return S_OK;
1613 static HRESULT WINAPI AudioClient_GetService(IAudioClient *iface, REFIID riid,
1614 void **ppv)
1616 ACImpl *This = impl_from_IAudioClient(iface);
1618 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
1620 if(!ppv)
1621 return E_POINTER;
1622 *ppv = NULL;
1624 OSSpinLockLock(&This->lock);
1626 if(!This->aqueue){
1627 OSSpinLockUnlock(&This->lock);
1628 return AUDCLNT_E_NOT_INITIALIZED;
1631 if(IsEqualIID(riid, &IID_IAudioRenderClient)){
1632 if(This->dataflow != eRender){
1633 OSSpinLockUnlock(&This->lock);
1634 return AUDCLNT_E_WRONG_ENDPOINT_TYPE;
1636 IAudioRenderClient_AddRef(&This->IAudioRenderClient_iface);
1637 *ppv = &This->IAudioRenderClient_iface;
1638 }else if(IsEqualIID(riid, &IID_IAudioCaptureClient)){
1639 if(This->dataflow != eCapture){
1640 OSSpinLockUnlock(&This->lock);
1641 return AUDCLNT_E_WRONG_ENDPOINT_TYPE;
1643 IAudioCaptureClient_AddRef(&This->IAudioCaptureClient_iface);
1644 *ppv = &This->IAudioCaptureClient_iface;
1645 }else if(IsEqualIID(riid, &IID_IAudioClock)){
1646 IAudioClock_AddRef(&This->IAudioClock_iface);
1647 *ppv = &This->IAudioClock_iface;
1648 }else if(IsEqualIID(riid, &IID_IAudioStreamVolume)){
1649 IAudioStreamVolume_AddRef(&This->IAudioStreamVolume_iface);
1650 *ppv = &This->IAudioStreamVolume_iface;
1651 }else if(IsEqualIID(riid, &IID_IAudioSessionControl)){
1652 if(!This->session_wrapper){
1653 This->session_wrapper = AudioSessionWrapper_Create(This);
1654 if(!This->session_wrapper){
1655 OSSpinLockUnlock(&This->lock);
1656 return E_OUTOFMEMORY;
1658 }else
1659 IAudioSessionControl2_AddRef(&This->session_wrapper->IAudioSessionControl2_iface);
1661 *ppv = &This->session_wrapper->IAudioSessionControl2_iface;
1662 }else if(IsEqualIID(riid, &IID_IChannelAudioVolume)){
1663 if(!This->session_wrapper){
1664 This->session_wrapper = AudioSessionWrapper_Create(This);
1665 if(!This->session_wrapper){
1666 OSSpinLockUnlock(&This->lock);
1667 return E_OUTOFMEMORY;
1669 }else
1670 IChannelAudioVolume_AddRef(&This->session_wrapper->IChannelAudioVolume_iface);
1672 *ppv = &This->session_wrapper->IChannelAudioVolume_iface;
1673 }else if(IsEqualIID(riid, &IID_ISimpleAudioVolume)){
1674 if(!This->session_wrapper){
1675 This->session_wrapper = AudioSessionWrapper_Create(This);
1676 if(!This->session_wrapper){
1677 OSSpinLockUnlock(&This->lock);
1678 return E_OUTOFMEMORY;
1680 }else
1681 ISimpleAudioVolume_AddRef(&This->session_wrapper->ISimpleAudioVolume_iface);
1683 *ppv = &This->session_wrapper->ISimpleAudioVolume_iface;
1686 if(*ppv){
1687 OSSpinLockUnlock(&This->lock);
1688 return S_OK;
1691 OSSpinLockUnlock(&This->lock);
1693 FIXME("stub %s\n", debugstr_guid(riid));
1694 return E_NOINTERFACE;
1697 static const IAudioClientVtbl AudioClient_Vtbl =
1699 AudioClient_QueryInterface,
1700 AudioClient_AddRef,
1701 AudioClient_Release,
1702 AudioClient_Initialize,
1703 AudioClient_GetBufferSize,
1704 AudioClient_GetStreamLatency,
1705 AudioClient_GetCurrentPadding,
1706 AudioClient_IsFormatSupported,
1707 AudioClient_GetMixFormat,
1708 AudioClient_GetDevicePeriod,
1709 AudioClient_Start,
1710 AudioClient_Stop,
1711 AudioClient_Reset,
1712 AudioClient_SetEventHandle,
1713 AudioClient_GetService
1716 static HRESULT WINAPI AudioRenderClient_QueryInterface(
1717 IAudioRenderClient *iface, REFIID riid, void **ppv)
1719 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
1721 if(!ppv)
1722 return E_POINTER;
1723 *ppv = NULL;
1725 if(IsEqualIID(riid, &IID_IUnknown) ||
1726 IsEqualIID(riid, &IID_IAudioRenderClient))
1727 *ppv = iface;
1728 if(*ppv){
1729 IUnknown_AddRef((IUnknown*)*ppv);
1730 return S_OK;
1733 WARN("Unknown interface %s\n", debugstr_guid(riid));
1734 return E_NOINTERFACE;
1737 static ULONG WINAPI AudioRenderClient_AddRef(IAudioRenderClient *iface)
1739 ACImpl *This = impl_from_IAudioRenderClient(iface);
1740 return AudioClient_AddRef(&This->IAudioClient_iface);
1743 static ULONG WINAPI AudioRenderClient_Release(IAudioRenderClient *iface)
1745 ACImpl *This = impl_from_IAudioRenderClient(iface);
1746 return AudioClient_Release(&This->IAudioClient_iface);
1749 static HRESULT WINAPI AudioRenderClient_GetBuffer(IAudioRenderClient *iface,
1750 UINT32 frames, BYTE **data)
1752 ACImpl *This = impl_from_IAudioRenderClient(iface);
1753 AQBuffer *buf;
1754 UINT32 pad, bytes = frames * This->fmt->nBlockAlign;
1755 HRESULT hr;
1756 OSStatus sc;
1758 TRACE("(%p)->(%u, %p)\n", This, frames, data);
1760 if(!data)
1761 return E_POINTER;
1762 *data = NULL;
1764 OSSpinLockLock(&This->lock);
1766 if(This->getbuf_last){
1767 OSSpinLockUnlock(&This->lock);
1768 return AUDCLNT_E_OUT_OF_ORDER;
1771 if(!frames){
1772 OSSpinLockUnlock(&This->lock);
1773 return S_OK;
1776 hr = AudioClient_GetCurrentPadding_nolock(This, &pad);
1777 if(FAILED(hr)){
1778 OSSpinLockUnlock(&This->lock);
1779 return hr;
1782 if(pad + frames > This->bufsize_frames){
1783 OSSpinLockUnlock(&This->lock);
1784 return AUDCLNT_E_BUFFER_TOO_LARGE;
1787 LIST_FOR_EACH_ENTRY(buf, &This->avail_buffers, AQBuffer, entry){
1788 if(buf->buf->mAudioDataBytesCapacity >= bytes){
1789 This->public_buffer = buf->buf;
1790 list_remove(&buf->entry);
1791 break;
1795 if(&buf->entry == &This->avail_buffers){
1796 sc = AudioQueueAllocateBuffer(This->aqueue, bytes,
1797 &This->public_buffer);
1798 if(sc != noErr){
1799 OSSpinLockUnlock(&This->lock);
1800 WARN("Unable to allocate buffer: %lx\n", sc);
1801 return E_FAIL;
1803 buf = HeapAlloc(GetProcessHeap(), 0, sizeof(AQBuffer));
1804 if(!buf){
1805 AudioQueueFreeBuffer(This->aqueue, This->public_buffer);
1806 This->public_buffer = NULL;
1807 OSSpinLockUnlock(&This->lock);
1808 return E_OUTOFMEMORY;
1810 buf->buf = This->public_buffer;
1811 This->public_buffer->mUserData = buf;
1814 *data = This->public_buffer->mAudioData;
1816 This->getbuf_last = frames;
1818 OSSpinLockUnlock(&This->lock);
1820 return S_OK;
1823 static HRESULT WINAPI AudioRenderClient_ReleaseBuffer(
1824 IAudioRenderClient *iface, UINT32 frames, DWORD flags)
1826 ACImpl *This = impl_from_IAudioRenderClient(iface);
1827 AudioTimeStamp start_time;
1828 OSStatus sc;
1830 TRACE("(%p)->(%u, %x)\n", This, frames, flags);
1832 OSSpinLockLock(&This->lock);
1834 if(!frames){
1835 This->getbuf_last = 0;
1836 if(This->public_buffer){
1837 AQBuffer *buf = This->public_buffer->mUserData;
1838 list_add_tail(&This->avail_buffers, &buf->entry);
1839 This->public_buffer = NULL;
1841 OSSpinLockUnlock(&This->lock);
1842 return S_OK;
1845 if(!This->getbuf_last){
1846 OSSpinLockUnlock(&This->lock);
1847 return AUDCLNT_E_OUT_OF_ORDER;
1850 if(frames > This->getbuf_last){
1851 OSSpinLockUnlock(&This->lock);
1852 return AUDCLNT_E_INVALID_SIZE;
1855 if(flags & AUDCLNT_BUFFERFLAGS_SILENT){
1856 WAVEFORMATEXTENSIBLE *fmtex = (WAVEFORMATEXTENSIBLE*)This->fmt;
1857 if((This->fmt->wFormatTag == WAVE_FORMAT_PCM ||
1858 (This->fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
1859 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM))) &&
1860 This->fmt->wBitsPerSample == 8)
1861 memset(This->public_buffer->mAudioData, 128,
1862 frames * This->fmt->nBlockAlign);
1863 else
1864 memset(This->public_buffer->mAudioData, 0,
1865 frames * This->fmt->nBlockAlign);
1868 This->public_buffer->mAudioDataByteSize = frames * This->fmt->nBlockAlign;
1870 sc = AudioQueueEnqueueBufferWithParameters(This->aqueue,
1871 This->public_buffer, 0, NULL, 0, 0, 0, NULL, NULL, &start_time);
1872 if(sc != noErr){
1873 OSSpinLockUnlock(&This->lock);
1874 WARN("Unable to enqueue buffer: %lx\n", sc);
1875 return E_FAIL;
1878 if(start_time.mFlags & kAudioTimeStampSampleTimeValid){
1879 QueuedBufInfo *bufinfo;
1881 bufinfo = HeapAlloc(GetProcessHeap(), 0, sizeof(*bufinfo));
1882 bufinfo->start_sampletime = start_time.mSampleTime;
1883 bufinfo->start_pos = This->written_frames;
1884 bufinfo->len_frames = frames;
1886 list_add_tail(&This->queued_bufinfos, &bufinfo->entry);
1887 }else
1888 WARN("Start time didn't contain valid SampleTime member\n");
1890 if(This->playing == StateStopped)
1891 AudioQueuePrime(This->aqueue, 0, NULL);
1893 This->public_buffer = NULL;
1894 This->getbuf_last = 0;
1895 This->written_frames += frames;
1896 This->inbuf_frames += frames;
1898 OSSpinLockUnlock(&This->lock);
1900 return S_OK;
1903 static const IAudioRenderClientVtbl AudioRenderClient_Vtbl = {
1904 AudioRenderClient_QueryInterface,
1905 AudioRenderClient_AddRef,
1906 AudioRenderClient_Release,
1907 AudioRenderClient_GetBuffer,
1908 AudioRenderClient_ReleaseBuffer
1911 static HRESULT WINAPI AudioCaptureClient_QueryInterface(
1912 IAudioCaptureClient *iface, REFIID riid, void **ppv)
1914 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
1916 if(!ppv)
1917 return E_POINTER;
1918 *ppv = NULL;
1920 if(IsEqualIID(riid, &IID_IUnknown) ||
1921 IsEqualIID(riid, &IID_IAudioCaptureClient))
1922 *ppv = iface;
1923 if(*ppv){
1924 IUnknown_AddRef((IUnknown*)*ppv);
1925 return S_OK;
1928 WARN("Unknown interface %s\n", debugstr_guid(riid));
1929 return E_NOINTERFACE;
1932 static ULONG WINAPI AudioCaptureClient_AddRef(IAudioCaptureClient *iface)
1934 ACImpl *This = impl_from_IAudioCaptureClient(iface);
1935 return IAudioClient_AddRef(&This->IAudioClient_iface);
1938 static ULONG WINAPI AudioCaptureClient_Release(IAudioCaptureClient *iface)
1940 ACImpl *This = impl_from_IAudioCaptureClient(iface);
1941 return IAudioClient_Release(&This->IAudioClient_iface);
1944 static HRESULT WINAPI AudioCaptureClient_GetBuffer(IAudioCaptureClient *iface,
1945 BYTE **data, UINT32 *frames, DWORD *flags, UINT64 *devpos,
1946 UINT64 *qpcpos)
1948 ACImpl *This = impl_from_IAudioCaptureClient(iface);
1950 TRACE("(%p)->(%p, %p, %p, %p, %p)\n", This, data, frames, flags,
1951 devpos, qpcpos);
1953 if(!data || !frames || !flags)
1954 return E_POINTER;
1956 OSSpinLockLock(&This->lock);
1958 if(This->getbuf_last){
1959 OSSpinLockUnlock(&This->lock);
1960 return AUDCLNT_E_OUT_OF_ORDER;
1963 if(This->public_buffer){
1964 *data = This->public_buffer->mAudioData;
1965 *frames =
1966 This->public_buffer->mAudioDataByteSize / This->fmt->nBlockAlign;
1967 }else{
1968 struct list *head = list_head(&This->avail_buffers);
1969 if(!head){
1970 *data = NULL;
1971 *frames = 0;
1972 }else{
1973 AQBuffer *buf = LIST_ENTRY(head, AQBuffer, entry);
1974 This->public_buffer = buf->buf;
1975 *data = This->public_buffer->mAudioData;
1976 *frames =
1977 This->public_buffer->mAudioDataByteSize / This->fmt->nBlockAlign;
1978 list_remove(&buf->entry);
1982 *flags = 0;
1983 This->written_frames += *frames;
1984 This->inbuf_frames -= *frames;
1985 This->getbuf_last = 1;
1987 if(devpos || qpcpos)
1988 AudioClock_GetPosition_nolock(This, devpos, qpcpos);
1990 OSSpinLockUnlock(&This->lock);
1992 return *frames ? S_OK : AUDCLNT_S_BUFFER_EMPTY;
1995 static HRESULT WINAPI AudioCaptureClient_ReleaseBuffer(
1996 IAudioCaptureClient *iface, UINT32 done)
1998 ACImpl *This = impl_from_IAudioCaptureClient(iface);
1999 UINT32 pbuf_frames;
2000 OSStatus sc;
2002 TRACE("(%p)->(%u)\n", This, done);
2004 OSSpinLockLock(&This->lock);
2006 if(!This->getbuf_last){
2007 OSSpinLockUnlock(&This->lock);
2008 return AUDCLNT_E_OUT_OF_ORDER;
2011 pbuf_frames = This->public_buffer->mAudioDataByteSize / This->fmt->nBlockAlign;
2012 if(done != 0 && done != pbuf_frames){
2013 OSSpinLockUnlock(&This->lock);
2014 return AUDCLNT_E_INVALID_SIZE;
2017 if(done){
2018 sc = AudioQueueEnqueueBuffer(This->aqueue, This->public_buffer,
2019 0, NULL);
2020 if(sc != noErr)
2021 WARN("Unable to enqueue buffer: %lx\n", sc);
2022 This->public_buffer = NULL;
2025 This->getbuf_last = 0;
2027 OSSpinLockUnlock(&This->lock);
2029 return S_OK;
2032 static HRESULT WINAPI AudioCaptureClient_GetNextPacketSize(
2033 IAudioCaptureClient *iface, UINT32 *frames)
2035 ACImpl *This = impl_from_IAudioCaptureClient(iface);
2036 struct list *head;
2037 AQBuffer *buf;
2039 TRACE("(%p)->(%p)\n", This, frames);
2041 if(!frames)
2042 return E_POINTER;
2044 OSSpinLockLock(&This->lock);
2046 head = list_head(&This->avail_buffers);
2048 if(!head){
2049 *frames = This->bufsize_frames / CAPTURE_BUFFERS;
2050 OSSpinLockUnlock(&This->lock);
2051 return S_OK;
2054 buf = LIST_ENTRY(head, AQBuffer, entry);
2055 *frames = buf->buf->mAudioDataByteSize / This->fmt->nBlockAlign;
2057 OSSpinLockUnlock(&This->lock);
2059 return S_OK;
2062 static const IAudioCaptureClientVtbl AudioCaptureClient_Vtbl =
2064 AudioCaptureClient_QueryInterface,
2065 AudioCaptureClient_AddRef,
2066 AudioCaptureClient_Release,
2067 AudioCaptureClient_GetBuffer,
2068 AudioCaptureClient_ReleaseBuffer,
2069 AudioCaptureClient_GetNextPacketSize
2072 static HRESULT WINAPI AudioClock_QueryInterface(IAudioClock *iface,
2073 REFIID riid, void **ppv)
2075 ACImpl *This = impl_from_IAudioClock(iface);
2077 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2079 if(!ppv)
2080 return E_POINTER;
2081 *ppv = NULL;
2083 if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IAudioClock))
2084 *ppv = iface;
2085 else if(IsEqualIID(riid, &IID_IAudioClock2))
2086 *ppv = &This->IAudioClock2_iface;
2087 if(*ppv){
2088 IUnknown_AddRef((IUnknown*)*ppv);
2089 return S_OK;
2092 WARN("Unknown interface %s\n", debugstr_guid(riid));
2093 return E_NOINTERFACE;
2096 static ULONG WINAPI AudioClock_AddRef(IAudioClock *iface)
2098 ACImpl *This = impl_from_IAudioClock(iface);
2099 return IAudioClient_AddRef(&This->IAudioClient_iface);
2102 static ULONG WINAPI AudioClock_Release(IAudioClock *iface)
2104 ACImpl *This = impl_from_IAudioClock(iface);
2105 return IAudioClient_Release(&This->IAudioClient_iface);
2108 static HRESULT WINAPI AudioClock_GetFrequency(IAudioClock *iface, UINT64 *freq)
2110 ACImpl *This = impl_from_IAudioClock(iface);
2112 TRACE("(%p)->(%p)\n", This, freq);
2114 *freq = This->fmt->nSamplesPerSec;
2116 return S_OK;
2119 static HRESULT AudioClock_GetPosition_nolock(ACImpl *This,
2120 UINT64 *pos, UINT64 *qpctime)
2122 if(This->dataflow == eRender)
2123 *pos = get_current_aqbuffer_position(This, BUFPOS_ABSOLUTE);
2124 else
2125 *pos = This->inbuf_frames + This->written_frames;
2127 if(qpctime){
2128 LARGE_INTEGER stamp, freq;
2129 QueryPerformanceCounter(&stamp);
2130 QueryPerformanceFrequency(&freq);
2131 *qpctime = (stamp.QuadPart * (INT64)10000000) / freq.QuadPart;
2134 return S_OK;
2137 static HRESULT WINAPI AudioClock_GetPosition(IAudioClock *iface, UINT64 *pos,
2138 UINT64 *qpctime)
2140 ACImpl *This = impl_from_IAudioClock(iface);
2141 HRESULT hr;
2143 TRACE("(%p)->(%p, %p)\n", This, pos, qpctime);
2145 if(!pos)
2146 return E_POINTER;
2148 OSSpinLockLock(&This->lock);
2150 hr = AudioClock_GetPosition_nolock(This, pos, qpctime);
2152 OSSpinLockUnlock(&This->lock);
2154 return hr;
2157 static HRESULT WINAPI AudioClock_GetCharacteristics(IAudioClock *iface,
2158 DWORD *chars)
2160 ACImpl *This = impl_from_IAudioClock(iface);
2162 TRACE("(%p)->(%p)\n", This, chars);
2164 if(!chars)
2165 return E_POINTER;
2167 *chars = AUDIOCLOCK_CHARACTERISTIC_FIXED_FREQ;
2169 return S_OK;
2172 static const IAudioClockVtbl AudioClock_Vtbl =
2174 AudioClock_QueryInterface,
2175 AudioClock_AddRef,
2176 AudioClock_Release,
2177 AudioClock_GetFrequency,
2178 AudioClock_GetPosition,
2179 AudioClock_GetCharacteristics
2182 static HRESULT WINAPI AudioClock2_QueryInterface(IAudioClock2 *iface,
2183 REFIID riid, void **ppv)
2185 ACImpl *This = impl_from_IAudioClock2(iface);
2186 return IAudioClock_QueryInterface(&This->IAudioClock_iface, riid, ppv);
2189 static ULONG WINAPI AudioClock2_AddRef(IAudioClock2 *iface)
2191 ACImpl *This = impl_from_IAudioClock2(iface);
2192 return IAudioClient_AddRef(&This->IAudioClient_iface);
2195 static ULONG WINAPI AudioClock2_Release(IAudioClock2 *iface)
2197 ACImpl *This = impl_from_IAudioClock2(iface);
2198 return IAudioClient_Release(&This->IAudioClient_iface);
2201 static HRESULT WINAPI AudioClock2_GetDevicePosition(IAudioClock2 *iface,
2202 UINT64 *pos, UINT64 *qpctime)
2204 ACImpl *This = impl_from_IAudioClock2(iface);
2206 FIXME("(%p)->(%p, %p)\n", This, pos, qpctime);
2208 return E_NOTIMPL;
2211 static const IAudioClock2Vtbl AudioClock2_Vtbl =
2213 AudioClock2_QueryInterface,
2214 AudioClock2_AddRef,
2215 AudioClock2_Release,
2216 AudioClock2_GetDevicePosition
2219 static AudioSessionWrapper *AudioSessionWrapper_Create(ACImpl *client)
2221 AudioSessionWrapper *ret;
2223 ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
2224 sizeof(AudioSessionWrapper));
2225 if(!ret)
2226 return NULL;
2228 ret->IAudioSessionControl2_iface.lpVtbl = &AudioSessionControl2_Vtbl;
2229 ret->ISimpleAudioVolume_iface.lpVtbl = &SimpleAudioVolume_Vtbl;
2230 ret->IChannelAudioVolume_iface.lpVtbl = &ChannelAudioVolume_Vtbl;
2232 ret->ref = 1;
2234 ret->client = client;
2235 if(client){
2236 ret->session = client->session;
2237 AudioClient_AddRef(&client->IAudioClient_iface);
2240 return ret;
2243 static HRESULT WINAPI AudioSessionControl_QueryInterface(
2244 IAudioSessionControl2 *iface, REFIID riid, void **ppv)
2246 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2248 if(!ppv)
2249 return E_POINTER;
2250 *ppv = NULL;
2252 if(IsEqualIID(riid, &IID_IUnknown) ||
2253 IsEqualIID(riid, &IID_IAudioSessionControl) ||
2254 IsEqualIID(riid, &IID_IAudioSessionControl2))
2255 *ppv = iface;
2256 if(*ppv){
2257 IUnknown_AddRef((IUnknown*)*ppv);
2258 return S_OK;
2261 WARN("Unknown interface %s\n", debugstr_guid(riid));
2262 return E_NOINTERFACE;
2265 static ULONG WINAPI AudioSessionControl_AddRef(IAudioSessionControl2 *iface)
2267 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2268 ULONG ref;
2269 ref = InterlockedIncrement(&This->ref);
2270 TRACE("(%p) Refcount now %u\n", This, ref);
2271 return ref;
2274 static ULONG WINAPI AudioSessionControl_Release(IAudioSessionControl2 *iface)
2276 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2277 ULONG ref;
2278 ref = InterlockedDecrement(&This->ref);
2279 TRACE("(%p) Refcount now %u\n", This, ref);
2280 if(!ref){
2281 if(This->client){
2282 OSSpinLockLock(&This->client->lock);
2283 This->client->session_wrapper = NULL;
2284 OSSpinLockUnlock(&This->client->lock);
2285 AudioClient_Release(&This->client->IAudioClient_iface);
2287 HeapFree(GetProcessHeap(), 0, This);
2289 return ref;
2292 static HRESULT WINAPI AudioSessionControl_GetState(IAudioSessionControl2 *iface,
2293 AudioSessionState *state)
2295 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2296 ACImpl *client;
2298 TRACE("(%p)->(%p)\n", This, state);
2300 if(!state)
2301 return NULL_PTR_ERR;
2303 EnterCriticalSection(&g_sessions_lock);
2305 if(list_empty(&This->session->clients)){
2306 *state = AudioSessionStateExpired;
2307 LeaveCriticalSection(&g_sessions_lock);
2308 return S_OK;
2311 LIST_FOR_EACH_ENTRY(client, &This->session->clients, ACImpl, entry){
2312 OSSpinLockLock(&client->lock);
2313 if(client->playing == StatePlaying ||
2314 client->playing == StateInTransition){
2315 *state = AudioSessionStateActive;
2316 OSSpinLockUnlock(&client->lock);
2317 LeaveCriticalSection(&g_sessions_lock);
2318 return S_OK;
2320 OSSpinLockUnlock(&client->lock);
2323 LeaveCriticalSection(&g_sessions_lock);
2325 *state = AudioSessionStateInactive;
2327 return S_OK;
2330 static HRESULT WINAPI AudioSessionControl_GetDisplayName(
2331 IAudioSessionControl2 *iface, WCHAR **name)
2333 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2335 FIXME("(%p)->(%p) - stub\n", This, name);
2337 return E_NOTIMPL;
2340 static HRESULT WINAPI AudioSessionControl_SetDisplayName(
2341 IAudioSessionControl2 *iface, const WCHAR *name, const GUID *session)
2343 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2345 FIXME("(%p)->(%p, %s) - stub\n", This, name, debugstr_guid(session));
2347 return E_NOTIMPL;
2350 static HRESULT WINAPI AudioSessionControl_GetIconPath(
2351 IAudioSessionControl2 *iface, WCHAR **path)
2353 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2355 FIXME("(%p)->(%p) - stub\n", This, path);
2357 return E_NOTIMPL;
2360 static HRESULT WINAPI AudioSessionControl_SetIconPath(
2361 IAudioSessionControl2 *iface, const WCHAR *path, const GUID *session)
2363 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2365 FIXME("(%p)->(%p, %s) - stub\n", This, path, debugstr_guid(session));
2367 return E_NOTIMPL;
2370 static HRESULT WINAPI AudioSessionControl_GetGroupingParam(
2371 IAudioSessionControl2 *iface, GUID *group)
2373 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2375 FIXME("(%p)->(%p) - stub\n", This, group);
2377 return E_NOTIMPL;
2380 static HRESULT WINAPI AudioSessionControl_SetGroupingParam(
2381 IAudioSessionControl2 *iface, const GUID *group, const GUID *session)
2383 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2385 FIXME("(%p)->(%s, %s) - stub\n", This, debugstr_guid(group),
2386 debugstr_guid(session));
2388 return E_NOTIMPL;
2391 static HRESULT WINAPI AudioSessionControl_RegisterAudioSessionNotification(
2392 IAudioSessionControl2 *iface, IAudioSessionEvents *events)
2394 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2396 FIXME("(%p)->(%p) - stub\n", This, events);
2398 return S_OK;
2401 static HRESULT WINAPI AudioSessionControl_UnregisterAudioSessionNotification(
2402 IAudioSessionControl2 *iface, IAudioSessionEvents *events)
2404 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2406 FIXME("(%p)->(%p) - stub\n", This, events);
2408 return S_OK;
2411 static HRESULT WINAPI AudioSessionControl_GetSessionIdentifier(
2412 IAudioSessionControl2 *iface, WCHAR **id)
2414 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2416 FIXME("(%p)->(%p) - stub\n", This, id);
2418 return E_NOTIMPL;
2421 static HRESULT WINAPI AudioSessionControl_GetSessionInstanceIdentifier(
2422 IAudioSessionControl2 *iface, WCHAR **id)
2424 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2426 FIXME("(%p)->(%p) - stub\n", This, id);
2428 return E_NOTIMPL;
2431 static HRESULT WINAPI AudioSessionControl_GetProcessId(
2432 IAudioSessionControl2 *iface, DWORD *pid)
2434 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2436 TRACE("(%p)->(%p)\n", This, pid);
2438 if(!pid)
2439 return E_POINTER;
2441 *pid = GetCurrentProcessId();
2443 return S_OK;
2446 static HRESULT WINAPI AudioSessionControl_IsSystemSoundsSession(
2447 IAudioSessionControl2 *iface)
2449 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2451 TRACE("(%p)\n", This);
2453 return S_FALSE;
2456 static HRESULT WINAPI AudioSessionControl_SetDuckingPreference(
2457 IAudioSessionControl2 *iface, BOOL optout)
2459 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2461 TRACE("(%p)->(%d)\n", This, optout);
2463 return S_OK;
2466 static const IAudioSessionControl2Vtbl AudioSessionControl2_Vtbl =
2468 AudioSessionControl_QueryInterface,
2469 AudioSessionControl_AddRef,
2470 AudioSessionControl_Release,
2471 AudioSessionControl_GetState,
2472 AudioSessionControl_GetDisplayName,
2473 AudioSessionControl_SetDisplayName,
2474 AudioSessionControl_GetIconPath,
2475 AudioSessionControl_SetIconPath,
2476 AudioSessionControl_GetGroupingParam,
2477 AudioSessionControl_SetGroupingParam,
2478 AudioSessionControl_RegisterAudioSessionNotification,
2479 AudioSessionControl_UnregisterAudioSessionNotification,
2480 AudioSessionControl_GetSessionIdentifier,
2481 AudioSessionControl_GetSessionInstanceIdentifier,
2482 AudioSessionControl_GetProcessId,
2483 AudioSessionControl_IsSystemSoundsSession,
2484 AudioSessionControl_SetDuckingPreference
2487 /* index == -1 means set all channels, otherwise sets only the given channel */
2488 static HRESULT ca_setvol(ACImpl *This, UINT32 index)
2490 float level;
2491 OSStatus sc;
2493 if(index == (UINT32)-1){
2494 HRESULT ret = S_OK;
2495 UINT32 i;
2496 for(i = 0; i < This->fmt->nChannels; ++i){
2497 HRESULT hr;
2498 hr = ca_setvol(This, i);
2499 if(FAILED(hr))
2500 ret = hr;
2502 return ret;
2505 if(This->session->mute)
2506 level = 0;
2507 else
2508 level = This->session->master_vol *
2509 This->session->channel_vols[index] * This->vols[index];
2511 sc = AudioQueueSetParameter(This->aqueue, kAudioQueueParam_Volume, level);
2512 if(sc != noErr)
2513 WARN("Setting _Volume property failed: %lx\n", sc);
2515 return S_OK;
2518 static HRESULT ca_session_setvol(AudioSession *session, UINT32 index)
2520 HRESULT ret = S_OK;
2521 ACImpl *client;
2523 LIST_FOR_EACH_ENTRY(client, &session->clients, ACImpl, entry){
2524 HRESULT hr;
2525 hr = ca_setvol(client, index);
2526 if(FAILED(hr))
2527 ret = hr;
2530 return ret;
2533 static HRESULT WINAPI SimpleAudioVolume_QueryInterface(
2534 ISimpleAudioVolume *iface, REFIID riid, void **ppv)
2536 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2538 if(!ppv)
2539 return E_POINTER;
2540 *ppv = NULL;
2542 if(IsEqualIID(riid, &IID_IUnknown) ||
2543 IsEqualIID(riid, &IID_ISimpleAudioVolume))
2544 *ppv = iface;
2545 if(*ppv){
2546 IUnknown_AddRef((IUnknown*)*ppv);
2547 return S_OK;
2550 WARN("Unknown interface %s\n", debugstr_guid(riid));
2551 return E_NOINTERFACE;
2554 static ULONG WINAPI SimpleAudioVolume_AddRef(ISimpleAudioVolume *iface)
2556 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2557 return AudioSessionControl_AddRef(&This->IAudioSessionControl2_iface);
2560 static ULONG WINAPI SimpleAudioVolume_Release(ISimpleAudioVolume *iface)
2562 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2563 return AudioSessionControl_Release(&This->IAudioSessionControl2_iface);
2566 static HRESULT WINAPI SimpleAudioVolume_SetMasterVolume(
2567 ISimpleAudioVolume *iface, float level, const GUID *context)
2569 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2570 AudioSession *session = This->session;
2571 HRESULT ret;
2573 TRACE("(%p)->(%f, %s)\n", session, level, wine_dbgstr_guid(context));
2575 if(level < 0.f || level > 1.f)
2576 return E_INVALIDARG;
2578 if(context)
2579 FIXME("Notifications not supported yet\n");
2581 EnterCriticalSection(&session->lock);
2583 session->master_vol = level;
2585 ret = ca_session_setvol(session, -1);
2587 LeaveCriticalSection(&session->lock);
2589 return ret;
2592 static HRESULT WINAPI SimpleAudioVolume_GetMasterVolume(
2593 ISimpleAudioVolume *iface, float *level)
2595 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2596 AudioSession *session = This->session;
2598 TRACE("(%p)->(%p)\n", session, level);
2600 if(!level)
2601 return NULL_PTR_ERR;
2603 *level = session->master_vol;
2605 return S_OK;
2608 static HRESULT WINAPI SimpleAudioVolume_SetMute(ISimpleAudioVolume *iface,
2609 BOOL mute, const GUID *context)
2611 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2612 AudioSession *session = This->session;
2614 TRACE("(%p)->(%u, %p)\n", session, mute, context);
2616 if(context)
2617 FIXME("Notifications not supported yet\n");
2619 EnterCriticalSection(&session->lock);
2621 session->mute = mute;
2623 ca_session_setvol(session, -1);
2625 LeaveCriticalSection(&session->lock);
2627 return S_OK;
2630 static HRESULT WINAPI SimpleAudioVolume_GetMute(ISimpleAudioVolume *iface,
2631 BOOL *mute)
2633 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2634 AudioSession *session = This->session;
2636 TRACE("(%p)->(%p)\n", session, mute);
2638 if(!mute)
2639 return NULL_PTR_ERR;
2641 *mute = session->mute;
2643 return S_OK;
2646 static const ISimpleAudioVolumeVtbl SimpleAudioVolume_Vtbl =
2648 SimpleAudioVolume_QueryInterface,
2649 SimpleAudioVolume_AddRef,
2650 SimpleAudioVolume_Release,
2651 SimpleAudioVolume_SetMasterVolume,
2652 SimpleAudioVolume_GetMasterVolume,
2653 SimpleAudioVolume_SetMute,
2654 SimpleAudioVolume_GetMute
2657 static HRESULT WINAPI AudioStreamVolume_QueryInterface(
2658 IAudioStreamVolume *iface, REFIID riid, void **ppv)
2660 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2662 if(!ppv)
2663 return E_POINTER;
2664 *ppv = NULL;
2666 if(IsEqualIID(riid, &IID_IUnknown) ||
2667 IsEqualIID(riid, &IID_IAudioStreamVolume))
2668 *ppv = iface;
2669 if(*ppv){
2670 IUnknown_AddRef((IUnknown*)*ppv);
2671 return S_OK;
2674 WARN("Unknown interface %s\n", debugstr_guid(riid));
2675 return E_NOINTERFACE;
2678 static ULONG WINAPI AudioStreamVolume_AddRef(IAudioStreamVolume *iface)
2680 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2681 return IAudioClient_AddRef(&This->IAudioClient_iface);
2684 static ULONG WINAPI AudioStreamVolume_Release(IAudioStreamVolume *iface)
2686 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2687 return IAudioClient_Release(&This->IAudioClient_iface);
2690 static HRESULT WINAPI AudioStreamVolume_GetChannelCount(
2691 IAudioStreamVolume *iface, UINT32 *out)
2693 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2695 TRACE("(%p)->(%p)\n", This, out);
2697 if(!out)
2698 return E_POINTER;
2700 *out = This->fmt->nChannels;
2702 return S_OK;
2705 static HRESULT WINAPI AudioStreamVolume_SetChannelVolume(
2706 IAudioStreamVolume *iface, UINT32 index, float level)
2708 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2709 HRESULT ret;
2711 TRACE("(%p)->(%d, %f)\n", This, index, level);
2713 if(level < 0.f || level > 1.f)
2714 return E_INVALIDARG;
2716 if(index >= This->fmt->nChannels)
2717 return E_INVALIDARG;
2719 OSSpinLockLock(&This->lock);
2721 This->vols[index] = level;
2723 WARN("AudioQueue doesn't support per-channel volume control\n");
2724 ret = ca_setvol(This, index);
2726 OSSpinLockUnlock(&This->lock);
2728 return ret;
2731 static HRESULT WINAPI AudioStreamVolume_GetChannelVolume(
2732 IAudioStreamVolume *iface, UINT32 index, float *level)
2734 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2736 TRACE("(%p)->(%d, %p)\n", This, index, level);
2738 if(!level)
2739 return E_POINTER;
2741 if(index >= This->fmt->nChannels)
2742 return E_INVALIDARG;
2744 *level = This->vols[index];
2746 return S_OK;
2749 static HRESULT WINAPI AudioStreamVolume_SetAllVolumes(
2750 IAudioStreamVolume *iface, UINT32 count, const float *levels)
2752 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2753 int i;
2754 HRESULT ret;
2756 TRACE("(%p)->(%d, %p)\n", This, count, levels);
2758 if(!levels)
2759 return E_POINTER;
2761 if(count != This->fmt->nChannels)
2762 return E_INVALIDARG;
2764 OSSpinLockLock(&This->lock);
2766 for(i = 0; i < count; ++i)
2767 This->vols[i] = levels[i];
2769 ret = ca_setvol(This, -1);
2771 OSSpinLockUnlock(&This->lock);
2773 return ret;
2776 static HRESULT WINAPI AudioStreamVolume_GetAllVolumes(
2777 IAudioStreamVolume *iface, UINT32 count, float *levels)
2779 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2780 int i;
2782 TRACE("(%p)->(%d, %p)\n", This, count, levels);
2784 if(!levels)
2785 return E_POINTER;
2787 if(count != This->fmt->nChannels)
2788 return E_INVALIDARG;
2790 OSSpinLockLock(&This->lock);
2792 for(i = 0; i < count; ++i)
2793 levels[i] = This->vols[i];
2795 OSSpinLockUnlock(&This->lock);
2797 return S_OK;
2800 static const IAudioStreamVolumeVtbl AudioStreamVolume_Vtbl =
2802 AudioStreamVolume_QueryInterface,
2803 AudioStreamVolume_AddRef,
2804 AudioStreamVolume_Release,
2805 AudioStreamVolume_GetChannelCount,
2806 AudioStreamVolume_SetChannelVolume,
2807 AudioStreamVolume_GetChannelVolume,
2808 AudioStreamVolume_SetAllVolumes,
2809 AudioStreamVolume_GetAllVolumes
2812 static HRESULT WINAPI ChannelAudioVolume_QueryInterface(
2813 IChannelAudioVolume *iface, REFIID riid, void **ppv)
2815 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2817 if(!ppv)
2818 return E_POINTER;
2819 *ppv = NULL;
2821 if(IsEqualIID(riid, &IID_IUnknown) ||
2822 IsEqualIID(riid, &IID_IChannelAudioVolume))
2823 *ppv = iface;
2824 if(*ppv){
2825 IUnknown_AddRef((IUnknown*)*ppv);
2826 return S_OK;
2829 WARN("Unknown interface %s\n", debugstr_guid(riid));
2830 return E_NOINTERFACE;
2833 static ULONG WINAPI ChannelAudioVolume_AddRef(IChannelAudioVolume *iface)
2835 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2836 return AudioSessionControl_AddRef(&This->IAudioSessionControl2_iface);
2839 static ULONG WINAPI ChannelAudioVolume_Release(IChannelAudioVolume *iface)
2841 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2842 return AudioSessionControl_Release(&This->IAudioSessionControl2_iface);
2845 static HRESULT WINAPI ChannelAudioVolume_GetChannelCount(
2846 IChannelAudioVolume *iface, UINT32 *out)
2848 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2849 AudioSession *session = This->session;
2851 TRACE("(%p)->(%p)\n", session, out);
2853 if(!out)
2854 return NULL_PTR_ERR;
2856 *out = session->channel_count;
2858 return S_OK;
2861 static HRESULT WINAPI ChannelAudioVolume_SetChannelVolume(
2862 IChannelAudioVolume *iface, UINT32 index, float level,
2863 const GUID *context)
2865 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2866 AudioSession *session = This->session;
2867 HRESULT ret;
2869 TRACE("(%p)->(%d, %f, %s)\n", session, index, level,
2870 wine_dbgstr_guid(context));
2872 if(level < 0.f || level > 1.f)
2873 return E_INVALIDARG;
2875 if(index >= session->channel_count)
2876 return E_INVALIDARG;
2878 if(context)
2879 FIXME("Notifications not supported yet\n");
2881 EnterCriticalSection(&session->lock);
2883 session->channel_vols[index] = level;
2885 WARN("AudioQueue doesn't support per-channel volume control\n");
2886 ret = ca_session_setvol(session, index);
2888 LeaveCriticalSection(&session->lock);
2890 return ret;
2893 static HRESULT WINAPI ChannelAudioVolume_GetChannelVolume(
2894 IChannelAudioVolume *iface, UINT32 index, float *level)
2896 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2897 AudioSession *session = This->session;
2899 TRACE("(%p)->(%d, %p)\n", session, index, level);
2901 if(!level)
2902 return NULL_PTR_ERR;
2904 if(index >= session->channel_count)
2905 return E_INVALIDARG;
2907 *level = session->channel_vols[index];
2909 return S_OK;
2912 static HRESULT WINAPI ChannelAudioVolume_SetAllVolumes(
2913 IChannelAudioVolume *iface, UINT32 count, const float *levels,
2914 const GUID *context)
2916 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2917 AudioSession *session = This->session;
2918 int i;
2919 HRESULT ret;
2921 TRACE("(%p)->(%d, %p, %s)\n", session, count, levels,
2922 wine_dbgstr_guid(context));
2924 if(!levels)
2925 return NULL_PTR_ERR;
2927 if(count != session->channel_count)
2928 return E_INVALIDARG;
2930 if(context)
2931 FIXME("Notifications not supported yet\n");
2933 EnterCriticalSection(&session->lock);
2935 for(i = 0; i < count; ++i)
2936 session->channel_vols[i] = levels[i];
2938 ret = ca_session_setvol(session, -1);
2940 LeaveCriticalSection(&session->lock);
2942 return ret;
2945 static HRESULT WINAPI ChannelAudioVolume_GetAllVolumes(
2946 IChannelAudioVolume *iface, UINT32 count, float *levels)
2948 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2949 AudioSession *session = This->session;
2950 int i;
2952 TRACE("(%p)->(%d, %p)\n", session, count, levels);
2954 if(!levels)
2955 return NULL_PTR_ERR;
2957 if(count != session->channel_count)
2958 return E_INVALIDARG;
2960 for(i = 0; i < count; ++i)
2961 levels[i] = session->channel_vols[i];
2963 return S_OK;
2966 static const IChannelAudioVolumeVtbl ChannelAudioVolume_Vtbl =
2968 ChannelAudioVolume_QueryInterface,
2969 ChannelAudioVolume_AddRef,
2970 ChannelAudioVolume_Release,
2971 ChannelAudioVolume_GetChannelCount,
2972 ChannelAudioVolume_SetChannelVolume,
2973 ChannelAudioVolume_GetChannelVolume,
2974 ChannelAudioVolume_SetAllVolumes,
2975 ChannelAudioVolume_GetAllVolumes
2978 static HRESULT WINAPI AudioSessionManager_QueryInterface(IAudioSessionManager2 *iface,
2979 REFIID riid, void **ppv)
2981 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2983 if(!ppv)
2984 return E_POINTER;
2985 *ppv = NULL;
2987 if(IsEqualIID(riid, &IID_IUnknown) ||
2988 IsEqualIID(riid, &IID_IAudioSessionManager) ||
2989 IsEqualIID(riid, &IID_IAudioSessionManager2))
2990 *ppv = iface;
2991 if(*ppv){
2992 IUnknown_AddRef((IUnknown*)*ppv);
2993 return S_OK;
2996 WARN("Unknown interface %s\n", debugstr_guid(riid));
2997 return E_NOINTERFACE;
3000 static ULONG WINAPI AudioSessionManager_AddRef(IAudioSessionManager2 *iface)
3002 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
3003 ULONG ref;
3004 ref = InterlockedIncrement(&This->ref);
3005 TRACE("(%p) Refcount now %u\n", This, ref);
3006 return ref;
3009 static ULONG WINAPI AudioSessionManager_Release(IAudioSessionManager2 *iface)
3011 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
3012 ULONG ref;
3013 ref = InterlockedDecrement(&This->ref);
3014 TRACE("(%p) Refcount now %u\n", This, ref);
3015 if(!ref)
3016 HeapFree(GetProcessHeap(), 0, This);
3017 return ref;
3020 static HRESULT WINAPI AudioSessionManager_GetAudioSessionControl(
3021 IAudioSessionManager2 *iface, const GUID *session_guid, DWORD flags,
3022 IAudioSessionControl **out)
3024 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
3025 AudioSession *session;
3026 AudioSessionWrapper *wrapper;
3027 HRESULT hr;
3029 TRACE("(%p)->(%s, %x, %p)\n", This, debugstr_guid(session_guid),
3030 flags, out);
3032 hr = get_audio_session(session_guid, This->device, 0, &session);
3033 if(FAILED(hr))
3034 return hr;
3036 wrapper = AudioSessionWrapper_Create(NULL);
3037 if(!wrapper)
3038 return E_OUTOFMEMORY;
3040 wrapper->session = session;
3042 *out = (IAudioSessionControl*)&wrapper->IAudioSessionControl2_iface;
3044 return S_OK;
3047 static HRESULT WINAPI AudioSessionManager_GetSimpleAudioVolume(
3048 IAudioSessionManager2 *iface, const GUID *session_guid, DWORD flags,
3049 ISimpleAudioVolume **out)
3051 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
3052 AudioSession *session;
3053 AudioSessionWrapper *wrapper;
3054 HRESULT hr;
3056 TRACE("(%p)->(%s, %x, %p)\n", This, debugstr_guid(session_guid),
3057 flags, out);
3059 hr = get_audio_session(session_guid, This->device, 0, &session);
3060 if(FAILED(hr))
3061 return hr;
3063 wrapper = AudioSessionWrapper_Create(NULL);
3064 if(!wrapper)
3065 return E_OUTOFMEMORY;
3067 wrapper->session = session;
3069 *out = &wrapper->ISimpleAudioVolume_iface;
3071 return S_OK;
3074 static HRESULT WINAPI AudioSessionManager_GetSessionEnumerator(
3075 IAudioSessionManager2 *iface, IAudioSessionEnumerator **out)
3077 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
3078 FIXME("(%p)->(%p) - stub\n", This, out);
3079 return E_NOTIMPL;
3082 static HRESULT WINAPI AudioSessionManager_RegisterSessionNotification(
3083 IAudioSessionManager2 *iface, IAudioSessionNotification *notification)
3085 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
3086 FIXME("(%p)->(%p) - stub\n", This, notification);
3087 return E_NOTIMPL;
3090 static HRESULT WINAPI AudioSessionManager_UnregisterSessionNotification(
3091 IAudioSessionManager2 *iface, IAudioSessionNotification *notification)
3093 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
3094 FIXME("(%p)->(%p) - stub\n", This, notification);
3095 return E_NOTIMPL;
3098 static HRESULT WINAPI AudioSessionManager_RegisterDuckNotification(
3099 IAudioSessionManager2 *iface, const WCHAR *session_id,
3100 IAudioVolumeDuckNotification *notification)
3102 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
3103 FIXME("(%p)->(%p) - stub\n", This, notification);
3104 return E_NOTIMPL;
3107 static HRESULT WINAPI AudioSessionManager_UnregisterDuckNotification(
3108 IAudioSessionManager2 *iface,
3109 IAudioVolumeDuckNotification *notification)
3111 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
3112 FIXME("(%p)->(%p) - stub\n", This, notification);
3113 return E_NOTIMPL;
3116 static const IAudioSessionManager2Vtbl AudioSessionManager2_Vtbl =
3118 AudioSessionManager_QueryInterface,
3119 AudioSessionManager_AddRef,
3120 AudioSessionManager_Release,
3121 AudioSessionManager_GetAudioSessionControl,
3122 AudioSessionManager_GetSimpleAudioVolume,
3123 AudioSessionManager_GetSessionEnumerator,
3124 AudioSessionManager_RegisterSessionNotification,
3125 AudioSessionManager_UnregisterSessionNotification,
3126 AudioSessionManager_RegisterDuckNotification,
3127 AudioSessionManager_UnregisterDuckNotification
3130 HRESULT WINAPI AUDDRV_GetAudioSessionManager(IMMDevice *device,
3131 IAudioSessionManager2 **out)
3133 SessionMgr *This;
3135 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(SessionMgr));
3136 if(!This)
3137 return E_OUTOFMEMORY;
3139 This->IAudioSessionManager2_iface.lpVtbl = &AudioSessionManager2_Vtbl;
3140 This->device = device;
3141 This->ref = 1;
3143 *out = &This->IAudioSessionManager2_iface;
3145 return S_OK;