2 * Copyright 2011 Andrew Eikum for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #define NONAMELESSUNION
29 #include <sys/types.h>
31 #include <sys/ioctl.h>
35 #include <sys/soundcard.h>
41 #include "wine/debug.h"
42 #include "wine/unicode.h"
43 #include "wine/list.h"
46 #include "mmdeviceapi.h"
50 #include "endpointvolume.h"
53 #include "audiopolicy.h"
54 #include "audioclient.h"
57 /* Some implementations of OSS, such as FreeBSD older than 9.0, lack
58 SNDCTL_DSP_HALT which is just a synonym for the older SNDCTL_DSP_RESET. */
59 #ifndef SNDCTL_DSP_HALT
60 #define SNDCTL_DSP_HALT SNDCTL_DSP_RESET
63 WINE_DEFAULT_DEBUG_CHANNEL(oss);
65 #define NULL_PTR_ERR MAKE_HRESULT(SEVERITY_ERROR, FACILITY_WIN32, RPC_X_NULL_REF_POINTER)
67 static const REFERENCE_TIME DefaultPeriod = 200000;
68 static const REFERENCE_TIME MinimumPeriod = 100000;
71 typedef struct ACImpl ACImpl;
73 typedef struct _AudioSession {
83 CRITICAL_SECTION lock;
88 typedef struct _AudioSessionWrapper {
89 IAudioSessionControl2 IAudioSessionControl2_iface;
90 IChannelAudioVolume IChannelAudioVolume_iface;
91 ISimpleAudioVolume ISimpleAudioVolume_iface;
96 AudioSession *session;
97 } AudioSessionWrapper;
100 IAudioClient IAudioClient_iface;
101 IAudioRenderClient IAudioRenderClient_iface;
102 IAudioCaptureClient IAudioCaptureClient_iface;
103 IAudioClock IAudioClock_iface;
104 IAudioClock2 IAudioClock2_iface;
105 IAudioStreamVolume IAudioStreamVolume_iface;
115 AUDCLNT_SHAREMODE share;
122 BOOL initted, playing;
123 UINT64 written_frames, held_frames, tmp_buffer_frames, inbuf_frames;
124 UINT32 period_us, bufsize_frames;
125 UINT32 lcl_offs_frames; /* offs into local_buffer where valid data starts */
127 BYTE *local_buffer, *tmp_buffer;
131 CRITICAL_SECTION lock;
133 AudioSession *session;
134 AudioSessionWrapper *session_wrapper;
141 LOCKED_NORMAL, /* public buffer piece is from local_buffer */
142 LOCKED_WRAPPED /* public buffer piece is in tmp_buffer */
145 static HANDLE g_timer_q;
147 static CRITICAL_SECTION g_sessions_lock;
148 static struct list g_sessions = LIST_INIT(g_sessions);
150 static AudioSessionWrapper *AudioSessionWrapper_Create(ACImpl *client);
151 static HRESULT oss_setvol(ACImpl *This, UINT32 index);
153 static const IAudioClientVtbl AudioClient_Vtbl;
154 static const IAudioRenderClientVtbl AudioRenderClient_Vtbl;
155 static const IAudioCaptureClientVtbl AudioCaptureClient_Vtbl;
156 static const IAudioSessionControl2Vtbl AudioSessionControl2_Vtbl;
157 static const ISimpleAudioVolumeVtbl SimpleAudioVolume_Vtbl;
158 static const IAudioClockVtbl AudioClock_Vtbl;
159 static const IAudioClock2Vtbl AudioClock2_Vtbl;
160 static const IAudioStreamVolumeVtbl AudioStreamVolume_Vtbl;
161 static const IChannelAudioVolumeVtbl ChannelAudioVolume_Vtbl;
163 static inline ACImpl *impl_from_IAudioClient(IAudioClient *iface)
165 return CONTAINING_RECORD(iface, ACImpl, IAudioClient_iface);
168 static inline ACImpl *impl_from_IAudioRenderClient(IAudioRenderClient *iface)
170 return CONTAINING_RECORD(iface, ACImpl, IAudioRenderClient_iface);
173 static inline ACImpl *impl_from_IAudioCaptureClient(IAudioCaptureClient *iface)
175 return CONTAINING_RECORD(iface, ACImpl, IAudioCaptureClient_iface);
178 static inline AudioSessionWrapper *impl_from_IAudioSessionControl2(IAudioSessionControl2 *iface)
180 return CONTAINING_RECORD(iface, AudioSessionWrapper, IAudioSessionControl2_iface);
183 static inline AudioSessionWrapper *impl_from_ISimpleAudioVolume(ISimpleAudioVolume *iface)
185 return CONTAINING_RECORD(iface, AudioSessionWrapper, ISimpleAudioVolume_iface);
188 static inline AudioSessionWrapper *impl_from_IChannelAudioVolume(IChannelAudioVolume *iface)
190 return CONTAINING_RECORD(iface, AudioSessionWrapper, IChannelAudioVolume_iface);
193 static inline ACImpl *impl_from_IAudioClock(IAudioClock *iface)
195 return CONTAINING_RECORD(iface, ACImpl, IAudioClock_iface);
198 static inline ACImpl *impl_from_IAudioClock2(IAudioClock2 *iface)
200 return CONTAINING_RECORD(iface, ACImpl, IAudioClock2_iface);
203 static inline ACImpl *impl_from_IAudioStreamVolume(IAudioStreamVolume *iface)
205 return CONTAINING_RECORD(iface, ACImpl, IAudioStreamVolume_iface);
208 BOOL WINAPI DllMain(HINSTANCE dll, DWORD reason, void *reserved)
210 if(reason == DLL_PROCESS_ATTACH){
211 g_timer_q = CreateTimerQueue();
215 InitializeCriticalSection(&g_sessions_lock);
221 HRESULT WINAPI AUDDRV_GetEndpointIDs(EDataFlow flow, WCHAR ***ids, void ***keys,
222 UINT *num, UINT *def_index)
226 static int print_once = 0;
228 TRACE("%d %p %p %p\n", flow, ids, num, def_index);
230 mixer_fd = open("/dev/mixer", O_RDONLY, 0);
232 ERR("OSS /dev/mixer doesn't seem to exist\n");
233 return AUDCLNT_E_SERVICE_NOT_RUNNING;
236 if(ioctl(mixer_fd, SNDCTL_SYSINFO, &sysinfo) < 0){
240 ERR("OSS version too old, need at least OSSv4\n");
241 return AUDCLNT_E_SERVICE_NOT_RUNNING;
244 ERR("Error getting SNDCTL_SYSINFO: %d (%s)\n", errno, strerror(errno));
249 TRACE("OSS sysinfo:\n");
250 TRACE("product: %s\n", sysinfo.product);
251 TRACE("version: %s\n", sysinfo.version);
252 TRACE("versionnum: %x\n", sysinfo.versionnum);
253 TRACE("numaudios: %d\n", sysinfo.numaudios);
254 TRACE("nummixers: %d\n", sysinfo.nummixers);
255 TRACE("numcards: %d\n", sysinfo.numcards);
256 TRACE("numaudioengines: %d\n", sysinfo.numaudioengines);
260 if(sysinfo.numaudios <= 0){
261 WARN("No audio devices!\n");
263 return AUDCLNT_E_SERVICE_NOT_RUNNING;
266 *ids = HeapAlloc(GetProcessHeap(), 0, sysinfo.numaudios * sizeof(WCHAR *));
267 *keys = HeapAlloc(GetProcessHeap(), 0, sysinfo.numaudios * sizeof(char *));
271 for(i = 0; i < sysinfo.numaudios; ++i){
272 oss_audioinfo ai = {0};
275 if(ioctl(mixer_fd, SNDCTL_AUDIOINFO, &ai) < 0){
276 WARN("Error getting AUDIOINFO for dev %d: %d (%s)\n", i, errno,
281 if((flow == eCapture && (ai.caps & PCM_CAP_INPUT)) ||
282 (flow == eRender && (ai.caps & PCM_CAP_OUTPUT))){
285 (*keys)[*num] = HeapAlloc(GetProcessHeap(), 0,
286 strlen(ai.devnode) + 1);
288 for(i = 0; i < *num; ++i){
289 HeapFree(GetProcessHeap(), 0, (*ids)[i]);
290 HeapFree(GetProcessHeap(), 0, (*keys)[i]);
292 HeapFree(GetProcessHeap(), 0, *ids);
293 HeapFree(GetProcessHeap(), 0, *keys);
295 return E_OUTOFMEMORY;
297 strcpy((*keys)[*num], ai.devnode);
299 len = MultiByteToWideChar(CP_UNIXCP, 0, ai.name, -1, NULL, 0);
300 (*ids)[*num] = HeapAlloc(GetProcessHeap(), 0,
301 len * sizeof(WCHAR));
303 HeapFree(GetProcessHeap(), 0, (*keys)[*num]);
304 for(i = 0; i < *num; ++i){
305 HeapFree(GetProcessHeap(), 0, (*ids)[i]);
306 HeapFree(GetProcessHeap(), 0, (*keys)[i]);
308 HeapFree(GetProcessHeap(), 0, *ids);
309 HeapFree(GetProcessHeap(), 0, *keys);
311 return E_OUTOFMEMORY;
313 MultiByteToWideChar(CP_UNIXCP, 0, ai.name, -1,
316 if(ai.caps & PCM_CAP_DEFAULT)
331 HRESULT WINAPI AUDDRV_GetAudioEndpoint(char *devnode, IMMDevice *dev,
332 EDataFlow dataflow, IAudioClient **out)
336 TRACE("%s %p %d %p\n", devnode, dev, dataflow, out);
338 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ACImpl));
340 return E_OUTOFMEMORY;
342 if(dataflow == eRender)
343 This->fd = open(devnode, O_WRONLY, 0);
344 else if(dataflow == eCapture)
345 This->fd = open(devnode, O_RDONLY, 0);
347 HeapFree(GetProcessHeap(), 0, This);
351 ERR("Unable to open device %s: %d (%s)\n", devnode, errno,
353 HeapFree(GetProcessHeap(), 0, This);
354 return AUDCLNT_E_DEVICE_INVALIDATED;
357 This->dataflow = dataflow;
360 if(ioctl(This->fd, SNDCTL_ENGINEINFO, &This->ai) < 0){
361 ERR("Unable to get audio info for device %s: %d (%s)\n", devnode,
362 errno, strerror(errno));
364 HeapFree(GetProcessHeap(), 0, This);
368 TRACE("OSS audioinfo:\n");
369 TRACE("devnode: %s\n", This->ai.devnode);
370 TRACE("name: %s\n", This->ai.name);
371 TRACE("busy: %x\n", This->ai.busy);
372 TRACE("caps: %x\n", This->ai.caps);
373 TRACE("iformats: %x\n", This->ai.iformats);
374 TRACE("oformats: %x\n", This->ai.oformats);
375 TRACE("enabled: %d\n", This->ai.enabled);
376 TRACE("min_rate: %d\n", This->ai.min_rate);
377 TRACE("max_rate: %d\n", This->ai.max_rate);
378 TRACE("min_channels: %d\n", This->ai.min_channels);
379 TRACE("max_channels: %d\n", This->ai.max_channels);
381 This->IAudioClient_iface.lpVtbl = &AudioClient_Vtbl;
382 This->IAudioRenderClient_iface.lpVtbl = &AudioRenderClient_Vtbl;
383 This->IAudioCaptureClient_iface.lpVtbl = &AudioCaptureClient_Vtbl;
384 This->IAudioClock_iface.lpVtbl = &AudioClock_Vtbl;
385 This->IAudioClock2_iface.lpVtbl = &AudioClock2_Vtbl;
386 This->IAudioStreamVolume_iface.lpVtbl = &AudioStreamVolume_Vtbl;
388 InitializeCriticalSection(&This->lock);
391 IMMDevice_AddRef(This->parent);
393 IAudioClient_AddRef(&This->IAudioClient_iface);
395 *out = &This->IAudioClient_iface;
400 static HRESULT WINAPI AudioClient_QueryInterface(IAudioClient *iface,
401 REFIID riid, void **ppv)
403 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
408 if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IAudioClient))
411 IUnknown_AddRef((IUnknown*)*ppv);
414 WARN("Unknown interface %s\n", debugstr_guid(riid));
415 return E_NOINTERFACE;
418 static ULONG WINAPI AudioClient_AddRef(IAudioClient *iface)
420 ACImpl *This = impl_from_IAudioClient(iface);
422 ref = InterlockedIncrement(&This->ref);
423 TRACE("(%p) Refcount now %u\n", This, ref);
427 static ULONG WINAPI AudioClient_Release(IAudioClient *iface)
429 ACImpl *This = impl_from_IAudioClient(iface);
431 ref = InterlockedDecrement(&This->ref);
432 TRACE("(%p) Refcount now %u\n", This, ref);
434 IAudioClient_Stop(iface);
435 IMMDevice_Release(This->parent);
436 DeleteCriticalSection(&This->lock);
439 EnterCriticalSection(&g_sessions_lock);
440 list_remove(&This->entry);
441 if(list_empty(&This->session->clients)){
442 list_remove(&This->session->entry);
443 DeleteCriticalSection(&This->session->lock);
444 HeapFree(GetProcessHeap(), 0, This->session->channel_vols);
445 HeapFree(GetProcessHeap(), 0, This->session);
447 LeaveCriticalSection(&g_sessions_lock);
449 HeapFree(GetProcessHeap(), 0, This->vols);
450 HeapFree(GetProcessHeap(), 0, This->local_buffer);
451 HeapFree(GetProcessHeap(), 0, This->tmp_buffer);
452 CoTaskMemFree(This->fmt);
453 HeapFree(GetProcessHeap(), 0, This);
458 static void dump_fmt(const WAVEFORMATEX *fmt)
460 TRACE("wFormatTag: 0x%x (", fmt->wFormatTag);
461 switch(fmt->wFormatTag){
462 case WAVE_FORMAT_PCM:
463 TRACE("WAVE_FORMAT_PCM");
465 case WAVE_FORMAT_IEEE_FLOAT:
466 TRACE("WAVE_FORMAT_IEEE_FLOAT");
468 case WAVE_FORMAT_EXTENSIBLE:
469 TRACE("WAVE_FORMAT_EXTENSIBLE");
477 TRACE("nChannels: %u\n", fmt->nChannels);
478 TRACE("nSamplesPerSec: %u\n", fmt->nSamplesPerSec);
479 TRACE("nAvgBytesPerSec: %u\n", fmt->nAvgBytesPerSec);
480 TRACE("nBlockAlign: %u\n", fmt->nBlockAlign);
481 TRACE("wBitsPerSample: %u\n", fmt->wBitsPerSample);
482 TRACE("cbSize: %u\n", fmt->cbSize);
484 if(fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE){
485 WAVEFORMATEXTENSIBLE *fmtex = (void*)fmt;
486 TRACE("dwChannelMask: %08x\n", fmtex->dwChannelMask);
487 TRACE("Samples: %04x\n", fmtex->Samples.wReserved);
488 TRACE("SubFormat: %s\n", wine_dbgstr_guid(&fmtex->SubFormat));
492 static DWORD get_channel_mask(unsigned int channels)
498 return SPEAKER_FRONT_CENTER;
500 return SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
502 return SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT |
503 SPEAKER_LOW_FREQUENCY;
505 return SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_BACK_LEFT |
508 return SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_BACK_LEFT |
509 SPEAKER_BACK_RIGHT | SPEAKER_LOW_FREQUENCY;
511 return SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_BACK_LEFT |
512 SPEAKER_BACK_RIGHT | SPEAKER_LOW_FREQUENCY | SPEAKER_FRONT_CENTER;
514 return SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_BACK_LEFT |
515 SPEAKER_BACK_RIGHT | SPEAKER_LOW_FREQUENCY | SPEAKER_FRONT_CENTER |
518 FIXME("Unknown speaker configuration: %u\n", channels);
522 static int get_oss_format(const WAVEFORMATEX *fmt)
524 WAVEFORMATEXTENSIBLE *fmtex = (WAVEFORMATEXTENSIBLE*)fmt;
526 if(fmt->wFormatTag == WAVE_FORMAT_PCM ||
527 (fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
528 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM))){
529 switch(fmt->wBitsPerSample){
543 if(fmt->wFormatTag == WAVE_FORMAT_IEEE_FLOAT ||
544 (fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
545 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT))){
546 if(fmt->wBitsPerSample != 32)
556 static WAVEFORMATEX *clone_format(const WAVEFORMATEX *fmt)
561 if(fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
562 size = sizeof(WAVEFORMATEXTENSIBLE);
564 size = sizeof(WAVEFORMATEX);
566 ret = CoTaskMemAlloc(size);
570 memcpy(ret, fmt, size);
572 ret->cbSize = size - sizeof(WAVEFORMATEX);
577 static HRESULT setup_oss_device(ACImpl *This, const WAVEFORMATEX *fmt,
583 WAVEFORMATEXTENSIBLE *fmtex = (void*)fmt;
584 WAVEFORMATEX *closest = NULL;
586 tmp = oss_format = get_oss_format(fmt);
588 return AUDCLNT_E_UNSUPPORTED_FORMAT;
589 if(ioctl(This->fd, SNDCTL_DSP_SETFMT, &tmp) < 0){
590 WARN("SETFMT failed: %d (%s)\n", errno, strerror(errno));
593 if(tmp != oss_format){
594 TRACE("Format unsupported by this OSS version: %x\n", oss_format);
595 return AUDCLNT_E_UNSUPPORTED_FORMAT;
598 closest = clone_format(fmt);
600 tmp = fmt->nSamplesPerSec;
601 if(ioctl(This->fd, SNDCTL_DSP_SPEED, &tmp) < 0){
602 WARN("SPEED failed: %d (%s)\n", errno, strerror(errno));
603 CoTaskMemFree(closest);
606 tenth = fmt->nSamplesPerSec * 0.1;
607 if(tmp > fmt->nSamplesPerSec + tenth || tmp < fmt->nSamplesPerSec - tenth){
609 closest->nSamplesPerSec = tmp;
612 tmp = fmt->nChannels;
613 if(ioctl(This->fd, SNDCTL_DSP_CHANNELS, &tmp) < 0){
614 WARN("CHANNELS failed: %d (%s)\n", errno, strerror(errno));
615 CoTaskMemFree(closest);
618 if(tmp != fmt->nChannels){
620 closest->nChannels = tmp;
623 if(closest->wFormatTag == WAVE_FORMAT_EXTENSIBLE){
624 DWORD mask = get_channel_mask(closest->nChannels);
626 ((WAVEFORMATEXTENSIBLE*)closest)->dwChannelMask = mask;
628 if(fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
629 fmtex->dwChannelMask != mask)
633 if(ret == S_OK || !out){
634 CoTaskMemFree( closest);
638 closest->nBlockAlign =
639 closest->nChannels * closest->wBitsPerSample / 8;
640 closest->nAvgBytesPerSec =
641 closest->nBlockAlign * closest->nSamplesPerSec;
645 TRACE("returning: %08x\n", ret);
649 static AudioSession *create_session(const GUID *guid, EDataFlow flow,
654 ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(AudioSession));
658 memcpy(&ret->guid, guid, sizeof(GUID));
660 ret->dataflow = flow;
662 list_init(&ret->clients);
664 list_add_head(&g_sessions, &ret->entry);
666 InitializeCriticalSection(&ret->lock);
668 ret->channel_count = num_channels;
669 ret->channel_vols = HeapAlloc(GetProcessHeap(), 0,
670 sizeof(float) * num_channels);
671 if(!ret->channel_vols){
672 HeapFree(GetProcessHeap(), 0, ret);
676 for(; num_channels > 0; --num_channels)
677 ret->channel_vols[num_channels - 1] = 1.f;
679 ret->master_vol = 1.f;
684 static HRESULT WINAPI AudioClient_Initialize(IAudioClient *iface,
685 AUDCLNT_SHAREMODE mode, DWORD flags, REFERENCE_TIME duration,
686 REFERENCE_TIME period, const WAVEFORMATEX *fmt,
687 const GUID *sessionguid)
689 ACImpl *This = impl_from_IAudioClient(iface);
693 TRACE("(%p)->(%x, %x, %s, %s, %p, %s)\n", This, mode, flags,
694 wine_dbgstr_longlong(duration), wine_dbgstr_longlong(period), fmt, debugstr_guid(sessionguid));
701 if(mode != AUDCLNT_SHAREMODE_SHARED && mode != AUDCLNT_SHAREMODE_EXCLUSIVE)
702 return AUDCLNT_E_NOT_INITIALIZED;
704 if(flags & ~(AUDCLNT_STREAMFLAGS_CROSSPROCESS |
705 AUDCLNT_STREAMFLAGS_LOOPBACK |
706 AUDCLNT_STREAMFLAGS_EVENTCALLBACK |
707 AUDCLNT_STREAMFLAGS_NOPERSIST |
708 AUDCLNT_STREAMFLAGS_RATEADJUST |
709 AUDCLNT_SESSIONFLAGS_EXPIREWHENUNOWNED |
710 AUDCLNT_SESSIONFLAGS_DISPLAY_HIDE |
711 AUDCLNT_SESSIONFLAGS_DISPLAY_HIDEWHENEXPIRED)){
712 TRACE("Unknown flags: %08x\n", flags);
716 EnterCriticalSection(&This->lock);
719 LeaveCriticalSection(&This->lock);
720 return AUDCLNT_E_ALREADY_INITIALIZED;
723 hr = setup_oss_device(This, fmt, NULL);
725 LeaveCriticalSection(&This->lock);
726 return AUDCLNT_E_UNSUPPORTED_FORMAT;
729 LeaveCriticalSection(&This->lock);
734 if(ioctl(This->fd, SNDCTL_DSP_SETTRIGGER, &mask) < 0){
735 LeaveCriticalSection(&This->lock);
736 WARN("SETTRIGGER failed: %d (%s)\n", errno, strerror(errno));
740 mask = (100 << 8) | 100;
741 if(ioctl(This->fd, SNDCTL_DSP_SETPLAYVOL, &mask) < 0)
742 WARN("SETPLAYVOL failed: %d (%s)\n", errno, strerror(errno));
744 This->fmt = clone_format(fmt);
746 LeaveCriticalSection(&This->lock);
747 return E_OUTOFMEMORY;
751 This->period_us = period / 10;
753 This->period_us = DefaultPeriod / 10;
755 This->bufsize_frames = ceil(fmt->nSamplesPerSec * (duration / 10000000.));
756 This->local_buffer = HeapAlloc(GetProcessHeap(), 0,
757 This->bufsize_frames * fmt->nBlockAlign);
758 if(!This->local_buffer){
759 CoTaskMemFree(This->fmt);
761 LeaveCriticalSection(&This->lock);
762 return E_OUTOFMEMORY;
765 This->vols = HeapAlloc(GetProcessHeap(), 0, fmt->nChannels * sizeof(float));
767 CoTaskMemFree(This->fmt);
769 LeaveCriticalSection(&This->lock);
770 return E_OUTOFMEMORY;
773 for(i = 0; i < fmt->nChannels; ++i)
779 EnterCriticalSection(&g_sessions_lock);
781 if(!sessionguid || IsEqualGUID(sessionguid, &GUID_NULL)){
782 This->session = create_session(&GUID_NULL, This->dataflow,
785 LeaveCriticalSection(&g_sessions_lock);
786 HeapFree(GetProcessHeap(), 0, This->vols);
788 CoTaskMemFree(This->fmt);
790 LeaveCriticalSection(&This->lock);
791 return E_OUTOFMEMORY;
794 AudioSession *session;
796 LIST_FOR_EACH_ENTRY(session, &g_sessions, AudioSession, entry){
797 if(IsEqualGUID(sessionguid, &session->guid) &&
798 This->dataflow == session->dataflow){
799 if(session->channel_count != fmt->nChannels){
800 LeaveCriticalSection(&g_sessions_lock);
801 HeapFree(GetProcessHeap(), 0, This->vols);
803 CoTaskMemFree(This->fmt);
805 LeaveCriticalSection(&This->lock);
808 This->session = session;
813 This->session = create_session(sessionguid, This->dataflow,
816 LeaveCriticalSection(&g_sessions_lock);
817 HeapFree(GetProcessHeap(), 0, This->vols);
819 CoTaskMemFree(This->fmt);
821 LeaveCriticalSection(&This->lock);
822 return E_OUTOFMEMORY;
827 list_add_tail(&This->session->clients, &This->entry);
829 LeaveCriticalSection(&g_sessions_lock);
831 This->initted = TRUE;
833 oss_setvol(This, -1);
835 LeaveCriticalSection(&This->lock);
840 static HRESULT WINAPI AudioClient_GetBufferSize(IAudioClient *iface,
843 ACImpl *This = impl_from_IAudioClient(iface);
845 TRACE("(%p)->(%p)\n", This, frames);
850 EnterCriticalSection(&This->lock);
853 LeaveCriticalSection(&This->lock);
854 return AUDCLNT_E_NOT_INITIALIZED;
857 *frames = This->bufsize_frames;
859 LeaveCriticalSection(&This->lock);
864 static HRESULT WINAPI AudioClient_GetStreamLatency(IAudioClient *iface,
865 REFERENCE_TIME *latency)
867 ACImpl *This = impl_from_IAudioClient(iface);
869 TRACE("(%p)->(%p)\n", This, latency);
874 EnterCriticalSection(&This->lock);
877 LeaveCriticalSection(&This->lock);
878 return AUDCLNT_E_NOT_INITIALIZED;
881 if(This->dataflow == eRender){
885 if(ioctl(This->fd, SNDCTL_DSP_GETODELAY, &delay_bytes) < 0){
886 LeaveCriticalSection(&This->lock);
887 WARN("GETODELAY failed: %d (%s)\n", errno, strerror(errno));
891 delay_s = delay_bytes / (double)(This->fmt->nSamplesPerSec *
892 This->fmt->nBlockAlign);
894 *latency = delay_s * 10000000;
896 *latency = 10000; /* OSS doesn't provide input latency */
898 LeaveCriticalSection(&This->lock);
903 static HRESULT WINAPI AudioClient_GetCurrentPadding(IAudioClient *iface,
906 ACImpl *This = impl_from_IAudioClient(iface);
909 TRACE("(%p)->(%p)\n", This, numpad);
914 EnterCriticalSection(&This->lock);
917 LeaveCriticalSection(&This->lock);
918 return AUDCLNT_E_NOT_INITIALIZED;
921 if(This->dataflow == eRender){
922 if(ioctl(This->fd, SNDCTL_DSP_GETOSPACE, &bi) < 0){
923 LeaveCriticalSection(&This->lock);
924 WARN("GETOSPACE failed: %d (%s)\n", errno, strerror(errno));
928 *numpad = (bi.fragstotal * bi.fragsize - bi.bytes) /
929 This->fmt->nBlockAlign;
931 /* when the OSS buffer has less than one fragment of data, including
932 * no data, it often reports it as some non-zero portion of a
933 * fragment. when it has more than one fragment of data, it reports
934 * it as some multiple of that portion of the fragment size.
936 * so, we have to do some ugly workarounds to report the timing
937 * as accurately as possible */
938 if(*numpad < bi.fragsize / This->fmt->nBlockAlign){
939 *numpad = This->inbuf_frames;
940 This->inbuf_frames = 0;
942 if(*numpad < This->inbuf_frames)
943 This->inbuf_frames = *numpad;
945 *numpad = This->inbuf_frames;
947 }else if(This->dataflow == eCapture){
948 if(ioctl(This->fd, SNDCTL_DSP_GETISPACE, &bi) < 0){
949 LeaveCriticalSection(&This->lock);
950 WARN("GETISPACE failed: %d (%s)\n", errno, strerror(errno));
954 if(bi.bytes <= bi.fragsize)
957 *numpad = bi.bytes / This->fmt->nBlockAlign;
959 LeaveCriticalSection(&This->lock);
963 *numpad += This->held_frames;
965 LeaveCriticalSection(&This->lock);
970 static HRESULT WINAPI AudioClient_IsFormatSupported(IAudioClient *iface,
971 AUDCLNT_SHAREMODE mode, const WAVEFORMATEX *pwfx,
972 WAVEFORMATEX **outpwfx)
974 ACImpl *This = impl_from_IAudioClient(iface);
977 TRACE("(%p)->(%x, %p, %p)\n", This, mode, pwfx, outpwfx);
979 if(!pwfx || (mode == AUDCLNT_SHAREMODE_SHARED && !outpwfx))
982 if(mode != AUDCLNT_SHAREMODE_SHARED && mode != AUDCLNT_SHAREMODE_EXCLUSIVE)
985 if(pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
986 pwfx->cbSize < sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX))
991 EnterCriticalSection(&This->lock);
993 ret = setup_oss_device(This, pwfx, outpwfx);
995 LeaveCriticalSection(&This->lock);
1000 static HRESULT WINAPI AudioClient_GetMixFormat(IAudioClient *iface,
1001 WAVEFORMATEX **pwfx)
1003 ACImpl *This = impl_from_IAudioClient(iface);
1004 WAVEFORMATEXTENSIBLE *fmt;
1007 TRACE("(%p)->(%p)\n", This, pwfx);
1012 *pwfx = HeapAlloc(GetProcessHeap(), 0, sizeof(WAVEFORMATEXTENSIBLE));
1014 return E_OUTOFMEMORY;
1016 fmt = (WAVEFORMATEXTENSIBLE*)*pwfx;
1018 if(This->dataflow == eRender)
1019 formats = This->ai.oformats;
1020 else if(This->dataflow == eCapture)
1021 formats = This->ai.iformats;
1023 return E_UNEXPECTED;
1025 fmt->Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
1026 if(formats & AFMT_S16_LE){
1027 fmt->Format.wBitsPerSample = 16;
1028 fmt->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
1030 }else if(formats & AFMT_FLOAT){
1031 fmt->Format.wBitsPerSample = 32;
1032 fmt->SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
1034 }else if(formats & AFMT_U8){
1035 fmt->Format.wBitsPerSample = 8;
1036 fmt->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
1037 }else if(formats & AFMT_S32_LE){
1038 fmt->Format.wBitsPerSample = 32;
1039 fmt->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
1040 }else if(formats & AFMT_S24_LE){
1041 fmt->Format.wBitsPerSample = 24;
1042 fmt->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
1044 ERR("Didn't recognize any available OSS formats: %x\n", formats);
1048 fmt->Format.nChannels = This->ai.max_channels;
1049 fmt->Format.nSamplesPerSec = This->ai.max_rate;
1050 fmt->dwChannelMask = get_channel_mask(fmt->Format.nChannels);
1052 fmt->Format.nBlockAlign = (fmt->Format.wBitsPerSample *
1053 fmt->Format.nChannels) / 8;
1054 fmt->Format.nAvgBytesPerSec = fmt->Format.nSamplesPerSec *
1055 fmt->Format.nBlockAlign;
1057 fmt->Samples.wValidBitsPerSample = fmt->Format.wBitsPerSample;
1058 fmt->Format.cbSize = sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
1065 static HRESULT WINAPI AudioClient_GetDevicePeriod(IAudioClient *iface,
1066 REFERENCE_TIME *defperiod, REFERENCE_TIME *minperiod)
1068 ACImpl *This = impl_from_IAudioClient(iface);
1070 TRACE("(%p)->(%p, %p)\n", This, defperiod, minperiod);
1072 if(!defperiod && !minperiod)
1075 EnterCriticalSection(&This->lock);
1078 *defperiod = DefaultPeriod;
1080 *minperiod = MinimumPeriod;
1082 LeaveCriticalSection(&This->lock);
1087 static void oss_write_data(ACImpl *This)
1090 UINT32 written_frames;
1093 This->local_buffer + (This->lcl_offs_frames * This->fmt->nBlockAlign);
1095 if(This->lcl_offs_frames + This->held_frames > This->bufsize_frames)
1096 to_write = This->bufsize_frames - This->lcl_offs_frames;
1098 to_write = This->held_frames;
1100 written = write(This->fd, buf, to_write * This->fmt->nBlockAlign);
1102 WARN("write failed: %d (%s)\n", errno, strerror(errno));
1105 written_frames = written / This->fmt->nBlockAlign;
1107 This->lcl_offs_frames += written_frames;
1108 This->lcl_offs_frames %= This->bufsize_frames;
1109 This->held_frames -= written_frames;
1110 This->inbuf_frames += written_frames;
1112 if(written_frames < to_write){
1113 /* OSS buffer probably full */
1117 if(This->held_frames){
1118 /* wrapped and have some data back at the start to write */
1119 written = write(This->fd, This->local_buffer,
1120 This->held_frames * This->fmt->nBlockAlign);
1122 WARN("write failed: %d (%s)\n", errno, strerror(errno));
1125 written_frames = written / This->fmt->nBlockAlign;
1127 This->lcl_offs_frames += written_frames;
1128 This->lcl_offs_frames %= This->bufsize_frames;
1129 This->held_frames -= written_frames;
1130 This->inbuf_frames += written_frames;
1134 static void oss_read_data(ACImpl *This)
1136 UINT64 pos, readable;
1140 if(ioctl(This->fd, SNDCTL_DSP_GETISPACE, &bi) < 0){
1141 WARN("GETISPACE failed: %d (%s)\n", errno, strerror(errno));
1145 pos = (This->held_frames + This->lcl_offs_frames) % This->bufsize_frames;
1146 readable = (This->bufsize_frames - pos) * This->fmt->nBlockAlign;
1148 if(bi.bytes < readable)
1149 readable = bi.bytes;
1151 nread = read(This->fd, This->local_buffer + pos * This->fmt->nBlockAlign,
1154 WARN("read failed: %d (%s)\n", errno, strerror(errno));
1158 This->held_frames += nread / This->fmt->nBlockAlign;
1160 if(This->held_frames > This->bufsize_frames){
1161 WARN("Overflow of unread data\n");
1162 This->lcl_offs_frames += This->held_frames;
1163 This->lcl_offs_frames %= This->bufsize_frames;
1164 This->held_frames = This->bufsize_frames;
1168 static void CALLBACK oss_period_callback(void *user, BOOLEAN timer)
1170 ACImpl *This = user;
1172 EnterCriticalSection(&This->lock);
1174 if(This->dataflow == eRender)
1175 oss_write_data(This);
1176 else if(This->dataflow == eCapture)
1177 oss_read_data(This);
1180 SetEvent(This->event);
1182 LeaveCriticalSection(&This->lock);
1185 static HRESULT WINAPI AudioClient_Start(IAudioClient *iface)
1187 ACImpl *This = impl_from_IAudioClient(iface);
1190 TRACE("(%p)\n", This);
1192 EnterCriticalSection(&This->lock);
1195 LeaveCriticalSection(&This->lock);
1196 return AUDCLNT_E_NOT_INITIALIZED;
1199 if((This->flags & AUDCLNT_STREAMFLAGS_EVENTCALLBACK) && !This->event){
1200 LeaveCriticalSection(&This->lock);
1201 return AUDCLNT_E_EVENTHANDLE_NOT_SET;
1205 LeaveCriticalSection(&This->lock);
1206 return AUDCLNT_E_NOT_STOPPED;
1209 if(This->dataflow == eRender)
1210 mask = PCM_ENABLE_OUTPUT;
1211 else if(This->dataflow == eCapture)
1212 mask = PCM_ENABLE_INPUT;
1214 LeaveCriticalSection(&This->lock);
1215 return E_UNEXPECTED;
1218 if(ioctl(This->fd, SNDCTL_DSP_SETTRIGGER, &mask) < 0){
1219 LeaveCriticalSection(&This->lock);
1220 WARN("SETTRIGGER failed: %d (%s)\n", errno, strerror(errno));
1224 if(!CreateTimerQueueTimer(&This->timer, g_timer_q,
1225 oss_period_callback, This, 0, This->period_us / 1000,
1226 WT_EXECUTEINTIMERTHREAD))
1227 ERR("Unable to create period timer: %u\n", GetLastError());
1229 This->playing = TRUE;
1231 LeaveCriticalSection(&This->lock);
1236 static HRESULT WINAPI AudioClient_Stop(IAudioClient *iface)
1238 ACImpl *This = impl_from_IAudioClient(iface);
1241 TRACE("(%p)\n", This);
1243 EnterCriticalSection(&This->lock);
1246 LeaveCriticalSection(&This->lock);
1247 return AUDCLNT_E_NOT_INITIALIZED;
1251 LeaveCriticalSection(&This->lock);
1255 if(This->timer && This->timer != INVALID_HANDLE_VALUE){
1256 DeleteTimerQueueTimer(g_timer_q, This->timer,
1257 INVALID_HANDLE_VALUE);
1261 if(ioctl(This->fd, SNDCTL_DSP_HALT, NULL) < 0){
1262 LeaveCriticalSection(&This->lock);
1263 WARN("HALT failed: %d (%s)\n", errno, strerror(errno));
1268 if(ioctl(This->fd, SNDCTL_DSP_SETTRIGGER, &mask) < 0){
1269 LeaveCriticalSection(&This->lock);
1270 WARN("SETTRIGGER failed: %d (%s)\n", errno, strerror(errno));
1274 This->playing = FALSE;
1276 LeaveCriticalSection(&This->lock);
1281 static HRESULT WINAPI AudioClient_Reset(IAudioClient *iface)
1283 ACImpl *This = impl_from_IAudioClient(iface);
1285 TRACE("(%p)\n", This);
1287 EnterCriticalSection(&This->lock);
1290 LeaveCriticalSection(&This->lock);
1291 return AUDCLNT_E_NOT_INITIALIZED;
1295 LeaveCriticalSection(&This->lock);
1296 return AUDCLNT_E_NOT_STOPPED;
1299 This->written_frames = 0;
1300 This->inbuf_frames = 0;
1301 This->held_frames = 0;
1303 if(ioctl(This->fd, SNDCTL_DSP_SKIP, NULL) < 0)
1304 WARN("SKIP failed: %d (%s)\n", errno, strerror(errno));
1306 LeaveCriticalSection(&This->lock);
1311 static HRESULT WINAPI AudioClient_SetEventHandle(IAudioClient *iface,
1314 ACImpl *This = impl_from_IAudioClient(iface);
1316 TRACE("(%p)->(%p)\n", This, event);
1319 return E_INVALIDARG;
1321 EnterCriticalSection(&This->lock);
1324 LeaveCriticalSection(&This->lock);
1325 return AUDCLNT_E_NOT_INITIALIZED;
1328 if(!(This->flags & AUDCLNT_STREAMFLAGS_EVENTCALLBACK)){
1329 LeaveCriticalSection(&This->lock);
1330 return AUDCLNT_E_EVENTHANDLE_NOT_EXPECTED;
1333 This->event = event;
1335 LeaveCriticalSection(&This->lock);
1340 static HRESULT WINAPI AudioClient_GetService(IAudioClient *iface, REFIID riid,
1343 ACImpl *This = impl_from_IAudioClient(iface);
1345 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
1351 EnterCriticalSection(&This->lock);
1354 LeaveCriticalSection(&This->lock);
1355 return AUDCLNT_E_NOT_INITIALIZED;
1358 if(IsEqualIID(riid, &IID_IAudioRenderClient)){
1359 if(This->dataflow != eRender){
1360 LeaveCriticalSection(&This->lock);
1361 return AUDCLNT_E_WRONG_ENDPOINT_TYPE;
1363 *ppv = &This->IAudioRenderClient_iface;
1364 }else if(IsEqualIID(riid, &IID_IAudioCaptureClient)){
1365 if(This->dataflow != eCapture){
1366 LeaveCriticalSection(&This->lock);
1367 return AUDCLNT_E_WRONG_ENDPOINT_TYPE;
1369 *ppv = &This->IAudioCaptureClient_iface;
1370 }else if(IsEqualIID(riid, &IID_IAudioClock)){
1371 *ppv = &This->IAudioClock_iface;
1372 }else if(IsEqualIID(riid, &IID_IAudioStreamVolume)){
1373 *ppv = &This->IAudioStreamVolume_iface;
1374 }else if(IsEqualIID(riid, &IID_IAudioSessionControl)){
1375 if(!This->session_wrapper){
1376 This->session_wrapper = AudioSessionWrapper_Create(This);
1377 if(!This->session_wrapper){
1378 LeaveCriticalSection(&This->lock);
1379 return E_OUTOFMEMORY;
1383 *ppv = &This->session_wrapper->IAudioSessionControl2_iface;
1384 }else if(IsEqualIID(riid, &IID_IChannelAudioVolume)){
1385 if(!This->session_wrapper){
1386 This->session_wrapper = AudioSessionWrapper_Create(This);
1387 if(!This->session_wrapper){
1388 LeaveCriticalSection(&This->lock);
1389 return E_OUTOFMEMORY;
1393 *ppv = &This->session_wrapper->IChannelAudioVolume_iface;
1394 }else if(IsEqualIID(riid, &IID_ISimpleAudioVolume)){
1395 if(!This->session_wrapper){
1396 This->session_wrapper = AudioSessionWrapper_Create(This);
1397 if(!This->session_wrapper){
1398 LeaveCriticalSection(&This->lock);
1399 return E_OUTOFMEMORY;
1403 *ppv = &This->session_wrapper->ISimpleAudioVolume_iface;
1407 IUnknown_AddRef((IUnknown*)*ppv);
1408 LeaveCriticalSection(&This->lock);
1412 LeaveCriticalSection(&This->lock);
1414 FIXME("stub %s\n", debugstr_guid(riid));
1415 return E_NOINTERFACE;
1418 static const IAudioClientVtbl AudioClient_Vtbl =
1420 AudioClient_QueryInterface,
1422 AudioClient_Release,
1423 AudioClient_Initialize,
1424 AudioClient_GetBufferSize,
1425 AudioClient_GetStreamLatency,
1426 AudioClient_GetCurrentPadding,
1427 AudioClient_IsFormatSupported,
1428 AudioClient_GetMixFormat,
1429 AudioClient_GetDevicePeriod,
1433 AudioClient_SetEventHandle,
1434 AudioClient_GetService
1437 static HRESULT WINAPI AudioRenderClient_QueryInterface(
1438 IAudioRenderClient *iface, REFIID riid, void **ppv)
1440 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
1446 if(IsEqualIID(riid, &IID_IUnknown) ||
1447 IsEqualIID(riid, &IID_IAudioRenderClient))
1450 IUnknown_AddRef((IUnknown*)*ppv);
1454 WARN("Unknown interface %s\n", debugstr_guid(riid));
1455 return E_NOINTERFACE;
1458 static ULONG WINAPI AudioRenderClient_AddRef(IAudioRenderClient *iface)
1460 ACImpl *This = impl_from_IAudioRenderClient(iface);
1461 return AudioClient_AddRef(&This->IAudioClient_iface);
1464 static ULONG WINAPI AudioRenderClient_Release(IAudioRenderClient *iface)
1466 ACImpl *This = impl_from_IAudioRenderClient(iface);
1467 return AudioClient_Release(&This->IAudioClient_iface);
1470 static HRESULT WINAPI AudioRenderClient_GetBuffer(IAudioRenderClient *iface,
1471 UINT32 frames, BYTE **data)
1473 ACImpl *This = impl_from_IAudioRenderClient(iface);
1474 UINT32 pad, write_pos;
1477 TRACE("(%p)->(%u, %p)\n", This, frames, data);
1482 EnterCriticalSection(&This->lock);
1484 if(This->buf_state != NOT_LOCKED){
1485 LeaveCriticalSection(&This->lock);
1486 return AUDCLNT_E_OUT_OF_ORDER;
1490 This->buf_state = LOCKED_NORMAL;
1491 LeaveCriticalSection(&This->lock);
1495 hr = IAudioClient_GetCurrentPadding(&This->IAudioClient_iface, &pad);
1497 LeaveCriticalSection(&This->lock);
1501 if(pad + frames > This->bufsize_frames){
1502 LeaveCriticalSection(&This->lock);
1503 return AUDCLNT_E_BUFFER_TOO_LARGE;
1507 (This->lcl_offs_frames + This->held_frames) % This->bufsize_frames;
1508 if(write_pos + frames > This->bufsize_frames){
1509 if(This->tmp_buffer_frames < frames){
1510 if(This->tmp_buffer)
1511 This->tmp_buffer = HeapReAlloc(GetProcessHeap(), 0,
1512 This->tmp_buffer, frames * This->fmt->nBlockAlign);
1514 This->tmp_buffer = HeapAlloc(GetProcessHeap(), 0,
1515 frames * This->fmt->nBlockAlign);
1516 if(!This->tmp_buffer){
1517 LeaveCriticalSection(&This->lock);
1518 return E_OUTOFMEMORY;
1520 This->tmp_buffer_frames = frames;
1522 *data = This->tmp_buffer;
1523 This->buf_state = LOCKED_WRAPPED;
1525 *data = This->local_buffer +
1526 This->lcl_offs_frames * This->fmt->nBlockAlign;
1527 This->buf_state = LOCKED_NORMAL;
1530 LeaveCriticalSection(&This->lock);
1535 static void oss_wrap_buffer(ACImpl *This, BYTE *buffer, UINT32 written_bytes)
1537 UINT32 write_offs_frames =
1538 (This->lcl_offs_frames + This->held_frames) % This->bufsize_frames;
1539 UINT32 write_offs_bytes = write_offs_frames * This->fmt->nBlockAlign;
1540 UINT32 chunk_frames = This->bufsize_frames - write_offs_frames;
1541 UINT32 chunk_bytes = chunk_frames * This->fmt->nBlockAlign;
1543 if(written_bytes < chunk_bytes){
1544 memcpy(This->local_buffer + write_offs_bytes, buffer, written_bytes);
1546 memcpy(This->local_buffer + write_offs_bytes, buffer, chunk_bytes);
1547 memcpy(This->local_buffer, buffer + chunk_bytes,
1548 written_bytes - chunk_bytes);
1552 static HRESULT WINAPI AudioRenderClient_ReleaseBuffer(
1553 IAudioRenderClient *iface, UINT32 written_frames, DWORD flags)
1555 ACImpl *This = impl_from_IAudioRenderClient(iface);
1557 UINT32 written_bytes = written_frames * This->fmt->nBlockAlign;
1559 TRACE("(%p)->(%u, %x)\n", This, written_frames, flags);
1561 EnterCriticalSection(&This->lock);
1563 if(This->buf_state == NOT_LOCKED || !written_frames){
1564 This->buf_state = NOT_LOCKED;
1565 LeaveCriticalSection(&This->lock);
1566 return written_frames ? AUDCLNT_E_OUT_OF_ORDER : S_OK;
1569 if(This->buf_state == LOCKED_NORMAL)
1570 buffer = This->local_buffer +
1571 This->lcl_offs_frames * This->fmt->nBlockAlign;
1573 buffer = This->tmp_buffer;
1575 if(flags & AUDCLNT_BUFFERFLAGS_SILENT){
1576 WAVEFORMATEXTENSIBLE *fmtex = (WAVEFORMATEXTENSIBLE*)This->fmt;
1577 if((This->fmt->wFormatTag == WAVE_FORMAT_PCM ||
1578 (This->fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
1579 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM))) &&
1580 This->fmt->wBitsPerSample == 8)
1581 memset(buffer, 128, written_frames * This->fmt->nBlockAlign);
1583 memset(buffer, 0, written_frames * This->fmt->nBlockAlign);
1586 if(This->held_frames){
1587 if(This->buf_state == LOCKED_WRAPPED)
1588 oss_wrap_buffer(This, buffer, written_bytes);
1590 This->held_frames += written_frames;
1595 w_bytes = write(This->fd, buffer,
1596 written_frames * This->fmt->nBlockAlign);
1598 LeaveCriticalSection(&This->lock);
1599 WARN("write failed: %d (%s)\n", errno, strerror(errno));
1602 w_frames = w_bytes / This->fmt->nBlockAlign;
1603 This->inbuf_frames += w_frames;
1605 if(w_frames < written_frames){
1606 if(This->buf_state == LOCKED_WRAPPED)
1607 oss_wrap_buffer(This, This->tmp_buffer + w_bytes,
1608 written_frames - w_frames);
1610 This->held_frames = written_frames - w_frames;
1614 This->written_frames += written_frames;
1615 This->buf_state = NOT_LOCKED;
1617 LeaveCriticalSection(&This->lock);
1622 static const IAudioRenderClientVtbl AudioRenderClient_Vtbl = {
1623 AudioRenderClient_QueryInterface,
1624 AudioRenderClient_AddRef,
1625 AudioRenderClient_Release,
1626 AudioRenderClient_GetBuffer,
1627 AudioRenderClient_ReleaseBuffer
1630 static HRESULT WINAPI AudioCaptureClient_QueryInterface(
1631 IAudioCaptureClient *iface, REFIID riid, void **ppv)
1633 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
1639 if(IsEqualIID(riid, &IID_IUnknown) ||
1640 IsEqualIID(riid, &IID_IAudioCaptureClient))
1643 IUnknown_AddRef((IUnknown*)*ppv);
1647 WARN("Unknown interface %s\n", debugstr_guid(riid));
1648 return E_NOINTERFACE;
1651 static ULONG WINAPI AudioCaptureClient_AddRef(IAudioCaptureClient *iface)
1653 ACImpl *This = impl_from_IAudioCaptureClient(iface);
1654 return IAudioClient_AddRef(&This->IAudioClient_iface);
1657 static ULONG WINAPI AudioCaptureClient_Release(IAudioCaptureClient *iface)
1659 ACImpl *This = impl_from_IAudioCaptureClient(iface);
1660 return IAudioClient_Release(&This->IAudioClient_iface);
1663 static HRESULT WINAPI AudioCaptureClient_GetBuffer(IAudioCaptureClient *iface,
1664 BYTE **data, UINT32 *frames, DWORD *flags, UINT64 *devpos,
1667 ACImpl *This = impl_from_IAudioCaptureClient(iface);
1670 TRACE("(%p)->(%p, %p, %p, %p, %p)\n", This, data, frames, flags,
1673 if(!data || !frames || !flags)
1676 EnterCriticalSection(&This->lock);
1678 if(This->buf_state != NOT_LOCKED){
1679 LeaveCriticalSection(&This->lock);
1680 return AUDCLNT_E_OUT_OF_ORDER;
1683 hr = IAudioCaptureClient_GetNextPacketSize(iface, frames);
1685 LeaveCriticalSection(&This->lock);
1691 if(This->lcl_offs_frames + *frames > This->bufsize_frames){
1692 UINT32 chunk_bytes, offs_bytes, frames_bytes;
1693 if(This->tmp_buffer_frames < *frames){
1694 if(This->tmp_buffer)
1695 This->tmp_buffer = HeapReAlloc(GetProcessHeap(), 0,
1696 This->tmp_buffer, *frames * This->fmt->nBlockAlign);
1698 This->tmp_buffer = HeapAlloc(GetProcessHeap(), 0,
1699 *frames * This->fmt->nBlockAlign);
1700 if(!This->tmp_buffer){
1701 LeaveCriticalSection(&This->lock);
1702 return E_OUTOFMEMORY;
1704 This->tmp_buffer_frames = *frames;
1707 *data = This->tmp_buffer;
1708 chunk_bytes = (This->bufsize_frames - This->lcl_offs_frames) *
1709 This->fmt->nBlockAlign;
1710 offs_bytes = This->lcl_offs_frames * This->fmt->nBlockAlign;
1711 frames_bytes = *frames * This->fmt->nBlockAlign;
1712 memcpy(This->tmp_buffer, This->local_buffer + offs_bytes, chunk_bytes);
1713 memcpy(This->tmp_buffer, This->local_buffer,
1714 frames_bytes - chunk_bytes);
1716 *data = This->local_buffer +
1717 This->lcl_offs_frames * This->fmt->nBlockAlign;
1719 This->buf_state = LOCKED_NORMAL;
1721 if(devpos || qpcpos)
1722 IAudioClock_GetPosition(&This->IAudioClock_iface, devpos, qpcpos);
1724 LeaveCriticalSection(&This->lock);
1726 return *frames ? S_OK : AUDCLNT_S_BUFFER_EMPTY;
1729 static HRESULT WINAPI AudioCaptureClient_ReleaseBuffer(
1730 IAudioCaptureClient *iface, UINT32 done)
1732 ACImpl *This = impl_from_IAudioCaptureClient(iface);
1734 TRACE("(%p)->(%u)\n", This, done);
1736 EnterCriticalSection(&This->lock);
1738 if(This->buf_state == NOT_LOCKED){
1739 LeaveCriticalSection(&This->lock);
1740 return AUDCLNT_E_OUT_OF_ORDER;
1743 This->held_frames -= done;
1744 This->lcl_offs_frames += done;
1745 This->lcl_offs_frames %= This->bufsize_frames;
1747 This->buf_state = NOT_LOCKED;
1749 LeaveCriticalSection(&This->lock);
1754 static HRESULT WINAPI AudioCaptureClient_GetNextPacketSize(
1755 IAudioCaptureClient *iface, UINT32 *frames)
1757 ACImpl *This = impl_from_IAudioCaptureClient(iface);
1759 TRACE("(%p)->(%p)\n", This, frames);
1761 return AudioClient_GetCurrentPadding(&This->IAudioClient_iface, frames);
1764 static const IAudioCaptureClientVtbl AudioCaptureClient_Vtbl =
1766 AudioCaptureClient_QueryInterface,
1767 AudioCaptureClient_AddRef,
1768 AudioCaptureClient_Release,
1769 AudioCaptureClient_GetBuffer,
1770 AudioCaptureClient_ReleaseBuffer,
1771 AudioCaptureClient_GetNextPacketSize
1774 static HRESULT WINAPI AudioClock_QueryInterface(IAudioClock *iface,
1775 REFIID riid, void **ppv)
1777 ACImpl *This = impl_from_IAudioClock(iface);
1779 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
1785 if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IAudioClock))
1787 else if(IsEqualIID(riid, &IID_IAudioClock2))
1788 *ppv = &This->IAudioClock2_iface;
1790 IUnknown_AddRef((IUnknown*)*ppv);
1794 WARN("Unknown interface %s\n", debugstr_guid(riid));
1795 return E_NOINTERFACE;
1798 static ULONG WINAPI AudioClock_AddRef(IAudioClock *iface)
1800 ACImpl *This = impl_from_IAudioClock(iface);
1801 return IAudioClient_AddRef(&This->IAudioClient_iface);
1804 static ULONG WINAPI AudioClock_Release(IAudioClock *iface)
1806 ACImpl *This = impl_from_IAudioClock(iface);
1807 return IAudioClient_Release(&This->IAudioClient_iface);
1810 static HRESULT WINAPI AudioClock_GetFrequency(IAudioClock *iface, UINT64 *freq)
1812 ACImpl *This = impl_from_IAudioClock(iface);
1814 TRACE("(%p)->(%p)\n", This, freq);
1816 *freq = This->fmt->nSamplesPerSec;
1821 static HRESULT WINAPI AudioClock_GetPosition(IAudioClock *iface, UINT64 *pos,
1824 ACImpl *This = impl_from_IAudioClock(iface);
1828 TRACE("(%p)->(%p, %p)\n", This, pos, qpctime);
1833 EnterCriticalSection(&This->lock);
1835 hr = IAudioClient_GetCurrentPadding(&This->IAudioClient_iface, &pad);
1837 LeaveCriticalSection(&This->lock);
1841 if(This->dataflow == eRender)
1842 *pos = This->written_frames - pad;
1843 else if(This->dataflow == eCapture)
1844 *pos = This->written_frames + pad;
1846 LeaveCriticalSection(&This->lock);
1849 LARGE_INTEGER stamp, freq;
1850 QueryPerformanceCounter(&stamp);
1851 QueryPerformanceFrequency(&freq);
1852 *qpctime = (stamp.QuadPart * (INT64)10000000) / freq.QuadPart;
1858 static HRESULT WINAPI AudioClock_GetCharacteristics(IAudioClock *iface,
1861 ACImpl *This = impl_from_IAudioClock(iface);
1863 TRACE("(%p)->(%p)\n", This, chars);
1868 *chars = AUDIOCLOCK_CHARACTERISTIC_FIXED_FREQ;
1873 static const IAudioClockVtbl AudioClock_Vtbl =
1875 AudioClock_QueryInterface,
1878 AudioClock_GetFrequency,
1879 AudioClock_GetPosition,
1880 AudioClock_GetCharacteristics
1883 static HRESULT WINAPI AudioClock2_QueryInterface(IAudioClock2 *iface,
1884 REFIID riid, void **ppv)
1886 ACImpl *This = impl_from_IAudioClock2(iface);
1887 return IAudioClock_QueryInterface(&This->IAudioClock_iface, riid, ppv);
1890 static ULONG WINAPI AudioClock2_AddRef(IAudioClock2 *iface)
1892 ACImpl *This = impl_from_IAudioClock2(iface);
1893 return IAudioClient_AddRef(&This->IAudioClient_iface);
1896 static ULONG WINAPI AudioClock2_Release(IAudioClock2 *iface)
1898 ACImpl *This = impl_from_IAudioClock2(iface);
1899 return IAudioClient_Release(&This->IAudioClient_iface);
1902 static HRESULT WINAPI AudioClock2_GetDevicePosition(IAudioClock2 *iface,
1903 UINT64 *pos, UINT64 *qpctime)
1905 ACImpl *This = impl_from_IAudioClock2(iface);
1907 FIXME("(%p)->(%p, %p)\n", This, pos, qpctime);
1912 static const IAudioClock2Vtbl AudioClock2_Vtbl =
1914 AudioClock2_QueryInterface,
1916 AudioClock2_Release,
1917 AudioClock2_GetDevicePosition
1920 static AudioSessionWrapper *AudioSessionWrapper_Create(ACImpl *client)
1922 AudioSessionWrapper *ret;
1924 ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1925 sizeof(AudioSessionWrapper));
1929 ret->IAudioSessionControl2_iface.lpVtbl = &AudioSessionControl2_Vtbl;
1930 ret->ISimpleAudioVolume_iface.lpVtbl = &SimpleAudioVolume_Vtbl;
1931 ret->IChannelAudioVolume_iface.lpVtbl = &ChannelAudioVolume_Vtbl;
1933 ret->client = client;
1934 ret->session = client->session;
1935 AudioClient_AddRef(&client->IAudioClient_iface);
1940 static HRESULT WINAPI AudioSessionControl_QueryInterface(
1941 IAudioSessionControl2 *iface, REFIID riid, void **ppv)
1943 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
1949 if(IsEqualIID(riid, &IID_IUnknown) ||
1950 IsEqualIID(riid, &IID_IAudioSessionControl) ||
1951 IsEqualIID(riid, &IID_IAudioSessionControl2))
1954 IUnknown_AddRef((IUnknown*)*ppv);
1958 WARN("Unknown interface %s\n", debugstr_guid(riid));
1959 return E_NOINTERFACE;
1962 static ULONG WINAPI AudioSessionControl_AddRef(IAudioSessionControl2 *iface)
1964 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
1966 ref = InterlockedIncrement(&This->ref);
1967 TRACE("(%p) Refcount now %u\n", This, ref);
1971 static ULONG WINAPI AudioSessionControl_Release(IAudioSessionControl2 *iface)
1973 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
1975 ref = InterlockedDecrement(&This->ref);
1976 TRACE("(%p) Refcount now %u\n", This, ref);
1978 EnterCriticalSection(&This->client->lock);
1979 This->client->session_wrapper = NULL;
1980 LeaveCriticalSection(&This->client->lock);
1981 AudioClient_Release(&This->client->IAudioClient_iface);
1982 HeapFree(GetProcessHeap(), 0, This);
1987 static HRESULT WINAPI AudioSessionControl_GetState(IAudioSessionControl2 *iface,
1988 AudioSessionState *state)
1990 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
1993 TRACE("(%p)->(%p)\n", This, state);
1996 return NULL_PTR_ERR;
1998 EnterCriticalSection(&g_sessions_lock);
2000 if(list_empty(&This->session->clients)){
2001 *state = AudioSessionStateExpired;
2002 LeaveCriticalSection(&g_sessions_lock);
2006 LIST_FOR_EACH_ENTRY(client, &This->session->clients, ACImpl, entry){
2007 EnterCriticalSection(&client->lock);
2008 if(client->playing){
2009 *state = AudioSessionStateActive;
2010 LeaveCriticalSection(&client->lock);
2011 LeaveCriticalSection(&g_sessions_lock);
2014 LeaveCriticalSection(&client->lock);
2017 LeaveCriticalSection(&g_sessions_lock);
2019 *state = AudioSessionStateInactive;
2024 static HRESULT WINAPI AudioSessionControl_GetDisplayName(
2025 IAudioSessionControl2 *iface, WCHAR **name)
2027 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2029 FIXME("(%p)->(%p) - stub\n", This, name);
2034 static HRESULT WINAPI AudioSessionControl_SetDisplayName(
2035 IAudioSessionControl2 *iface, const WCHAR *name, const GUID *session)
2037 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2039 FIXME("(%p)->(%p, %s) - stub\n", This, name, debugstr_guid(session));
2044 static HRESULT WINAPI AudioSessionControl_GetIconPath(
2045 IAudioSessionControl2 *iface, WCHAR **path)
2047 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2049 FIXME("(%p)->(%p) - stub\n", This, path);
2054 static HRESULT WINAPI AudioSessionControl_SetIconPath(
2055 IAudioSessionControl2 *iface, const WCHAR *path, const GUID *session)
2057 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2059 FIXME("(%p)->(%p, %s) - stub\n", This, path, debugstr_guid(session));
2064 static HRESULT WINAPI AudioSessionControl_GetGroupingParam(
2065 IAudioSessionControl2 *iface, GUID *group)
2067 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2069 FIXME("(%p)->(%p) - stub\n", This, group);
2074 static HRESULT WINAPI AudioSessionControl_SetGroupingParam(
2075 IAudioSessionControl2 *iface, GUID *group, const GUID *session)
2077 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2079 FIXME("(%p)->(%s, %s) - stub\n", This, debugstr_guid(group),
2080 debugstr_guid(session));
2085 static HRESULT WINAPI AudioSessionControl_RegisterAudioSessionNotification(
2086 IAudioSessionControl2 *iface, IAudioSessionEvents *events)
2088 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2090 FIXME("(%p)->(%p) - stub\n", This, events);
2095 static HRESULT WINAPI AudioSessionControl_UnregisterAudioSessionNotification(
2096 IAudioSessionControl2 *iface, IAudioSessionEvents *events)
2098 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2100 FIXME("(%p)->(%p) - stub\n", This, events);
2105 static HRESULT WINAPI AudioSessionControl_GetSessionIdentifier(
2106 IAudioSessionControl2 *iface, WCHAR **id)
2108 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2110 FIXME("(%p)->(%p) - stub\n", This, id);
2115 static HRESULT WINAPI AudioSessionControl_GetSessionInstanceIdentifier(
2116 IAudioSessionControl2 *iface, WCHAR **id)
2118 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2120 FIXME("(%p)->(%p) - stub\n", This, id);
2125 static HRESULT WINAPI AudioSessionControl_GetProcessId(
2126 IAudioSessionControl2 *iface, DWORD *pid)
2128 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2130 TRACE("(%p)->(%p)\n", This, pid);
2135 *pid = GetCurrentProcessId();
2140 static HRESULT WINAPI AudioSessionControl_IsSystemSoundsSession(
2141 IAudioSessionControl2 *iface)
2143 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2145 TRACE("(%p)\n", This);
2150 static HRESULT WINAPI AudioSessionControl_SetDuckingPreference(
2151 IAudioSessionControl2 *iface, BOOL optout)
2153 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2155 TRACE("(%p)->(%d)\n", This, optout);
2160 static const IAudioSessionControl2Vtbl AudioSessionControl2_Vtbl =
2162 AudioSessionControl_QueryInterface,
2163 AudioSessionControl_AddRef,
2164 AudioSessionControl_Release,
2165 AudioSessionControl_GetState,
2166 AudioSessionControl_GetDisplayName,
2167 AudioSessionControl_SetDisplayName,
2168 AudioSessionControl_GetIconPath,
2169 AudioSessionControl_SetIconPath,
2170 AudioSessionControl_GetGroupingParam,
2171 AudioSessionControl_SetGroupingParam,
2172 AudioSessionControl_RegisterAudioSessionNotification,
2173 AudioSessionControl_UnregisterAudioSessionNotification,
2174 AudioSessionControl_GetSessionIdentifier,
2175 AudioSessionControl_GetSessionInstanceIdentifier,
2176 AudioSessionControl_GetProcessId,
2177 AudioSessionControl_IsSystemSoundsSession,
2178 AudioSessionControl_SetDuckingPreference
2181 /* index == -1 means set all channels, otherwise sets only the given channel */
2182 static HRESULT oss_setvol(ACImpl *This, UINT32 index)
2189 if(index == (UINT32)-1){
2192 for(i = 0; i < This->fmt->nChannels; ++i){
2194 hr = oss_setvol(This, i);
2202 /* OSS doesn't support volume control past the first two channels */
2205 if(This->dataflow == eRender){
2206 setreq = SNDCTL_DSP_SETPLAYVOL;
2207 getreq = SNDCTL_DSP_GETPLAYVOL;
2208 }else if(This->dataflow == eCapture){
2209 setreq = SNDCTL_DSP_SETRECVOL;
2210 getreq = SNDCTL_DSP_GETRECVOL;
2212 return E_UNEXPECTED;
2214 if(ioctl(This->fd, getreq, &vol) < 0){
2216 /* device doesn't support this call */
2219 WARN("GET[REC|PLAY]VOL failed: %d (%s)\n", errno, strerror(errno));
2223 level = This->session->master_vol * This->session->channel_vols[index] *
2227 vol = l | (vol & 0xFF00);
2229 vol = (vol & 0xFF) | (l << 8);
2231 if(ioctl(This->fd, setreq, &vol) < 0){
2233 /* device doesn't support this call */
2236 WARN("SET[REC|PLAY]VOL failed: %d (%s)\n", errno, strerror(errno));
2243 static HRESULT oss_session_setvol(AudioSession *session, UINT32 index)
2248 LIST_FOR_EACH_ENTRY(client, &session->clients, ACImpl, entry){
2250 hr = oss_setvol(client, index);
2258 static HRESULT WINAPI SimpleAudioVolume_QueryInterface(
2259 ISimpleAudioVolume *iface, REFIID riid, void **ppv)
2261 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2267 if(IsEqualIID(riid, &IID_IUnknown) ||
2268 IsEqualIID(riid, &IID_ISimpleAudioVolume))
2271 IUnknown_AddRef((IUnknown*)*ppv);
2275 WARN("Unknown interface %s\n", debugstr_guid(riid));
2276 return E_NOINTERFACE;
2279 static ULONG WINAPI SimpleAudioVolume_AddRef(ISimpleAudioVolume *iface)
2281 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2282 return AudioSessionControl_AddRef(&This->IAudioSessionControl2_iface);
2285 static ULONG WINAPI SimpleAudioVolume_Release(ISimpleAudioVolume *iface)
2287 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2288 return AudioSessionControl_Release(&This->IAudioSessionControl2_iface);
2291 static HRESULT WINAPI SimpleAudioVolume_SetMasterVolume(
2292 ISimpleAudioVolume *iface, float level, const GUID *context)
2294 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2295 AudioSession *session = This->session;
2298 TRACE("(%p)->(%f, %s)\n", session, level, wine_dbgstr_guid(context));
2300 if(level < 0.f || level > 1.f)
2301 return E_INVALIDARG;
2304 FIXME("Notifications not supported yet\n");
2306 EnterCriticalSection(&session->lock);
2308 session->master_vol = level;
2310 ret = oss_session_setvol(session, -1);
2312 LeaveCriticalSection(&session->lock);
2317 static HRESULT WINAPI SimpleAudioVolume_GetMasterVolume(
2318 ISimpleAudioVolume *iface, float *level)
2320 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2321 AudioSession *session = This->session;
2323 TRACE("(%p)->(%p)\n", session, level);
2326 return NULL_PTR_ERR;
2328 *level = session->master_vol;
2333 static HRESULT WINAPI SimpleAudioVolume_SetMute(ISimpleAudioVolume *iface,
2334 BOOL mute, const GUID *context)
2336 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2337 AudioSession *session = This->session;
2339 FIXME("(%p)->(%u, %p) - stub\n", session, mute, context);
2344 static HRESULT WINAPI SimpleAudioVolume_GetMute(ISimpleAudioVolume *iface,
2347 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2348 AudioSession *session = This->session;
2350 FIXME("(%p)->(%p) - stub\n", session, mute);
2353 return NULL_PTR_ERR;
2358 static const ISimpleAudioVolumeVtbl SimpleAudioVolume_Vtbl =
2360 SimpleAudioVolume_QueryInterface,
2361 SimpleAudioVolume_AddRef,
2362 SimpleAudioVolume_Release,
2363 SimpleAudioVolume_SetMasterVolume,
2364 SimpleAudioVolume_GetMasterVolume,
2365 SimpleAudioVolume_SetMute,
2366 SimpleAudioVolume_GetMute
2369 static HRESULT WINAPI AudioStreamVolume_QueryInterface(
2370 IAudioStreamVolume *iface, REFIID riid, void **ppv)
2372 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2378 if(IsEqualIID(riid, &IID_IUnknown) ||
2379 IsEqualIID(riid, &IID_IAudioStreamVolume))
2382 IUnknown_AddRef((IUnknown*)*ppv);
2386 WARN("Unknown interface %s\n", debugstr_guid(riid));
2387 return E_NOINTERFACE;
2390 static ULONG WINAPI AudioStreamVolume_AddRef(IAudioStreamVolume *iface)
2392 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2393 return IAudioClient_AddRef(&This->IAudioClient_iface);
2396 static ULONG WINAPI AudioStreamVolume_Release(IAudioStreamVolume *iface)
2398 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2399 return IAudioClient_Release(&This->IAudioClient_iface);
2402 static HRESULT WINAPI AudioStreamVolume_GetChannelCount(
2403 IAudioStreamVolume *iface, UINT32 *out)
2405 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2407 TRACE("(%p)->(%p)\n", This, out);
2412 *out = This->fmt->nChannels;
2417 static HRESULT WINAPI AudioStreamVolume_SetChannelVolume(
2418 IAudioStreamVolume *iface, UINT32 index, float level)
2420 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2423 TRACE("(%p)->(%d, %f)\n", This, index, level);
2425 if(level < 0.f || level > 1.f)
2426 return E_INVALIDARG;
2428 if(index >= This->fmt->nChannels)
2429 return E_INVALIDARG;
2431 EnterCriticalSection(&This->lock);
2433 This->vols[index] = level;
2435 ret = oss_setvol(This, index);
2437 LeaveCriticalSection(&This->lock);
2442 static HRESULT WINAPI AudioStreamVolume_GetChannelVolume(
2443 IAudioStreamVolume *iface, UINT32 index, float *level)
2445 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2447 TRACE("(%p)->(%d, %p)\n", This, index, level);
2452 if(index >= This->fmt->nChannels)
2453 return E_INVALIDARG;
2455 *level = This->vols[index];
2460 static HRESULT WINAPI AudioStreamVolume_SetAllVolumes(
2461 IAudioStreamVolume *iface, UINT32 count, const float *levels)
2463 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2467 TRACE("(%p)->(%d, %p)\n", This, count, levels);
2472 if(count != This->fmt->nChannels)
2473 return E_INVALIDARG;
2475 EnterCriticalSection(&This->lock);
2477 for(i = 0; i < count; ++i)
2478 This->vols[i] = levels[i];
2480 ret = oss_setvol(This, -1);
2482 LeaveCriticalSection(&This->lock);
2487 static HRESULT WINAPI AudioStreamVolume_GetAllVolumes(
2488 IAudioStreamVolume *iface, UINT32 count, float *levels)
2490 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2493 TRACE("(%p)->(%d, %p)\n", This, count, levels);
2498 if(count != This->fmt->nChannels)
2499 return E_INVALIDARG;
2501 EnterCriticalSection(&This->lock);
2503 for(i = 0; i < count; ++i)
2504 levels[i] = This->vols[i];
2506 LeaveCriticalSection(&This->lock);
2511 static const IAudioStreamVolumeVtbl AudioStreamVolume_Vtbl =
2513 AudioStreamVolume_QueryInterface,
2514 AudioStreamVolume_AddRef,
2515 AudioStreamVolume_Release,
2516 AudioStreamVolume_GetChannelCount,
2517 AudioStreamVolume_SetChannelVolume,
2518 AudioStreamVolume_GetChannelVolume,
2519 AudioStreamVolume_SetAllVolumes,
2520 AudioStreamVolume_GetAllVolumes
2523 static HRESULT WINAPI ChannelAudioVolume_QueryInterface(
2524 IChannelAudioVolume *iface, REFIID riid, void **ppv)
2526 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2532 if(IsEqualIID(riid, &IID_IUnknown) ||
2533 IsEqualIID(riid, &IID_IChannelAudioVolume))
2536 IUnknown_AddRef((IUnknown*)*ppv);
2540 WARN("Unknown interface %s\n", debugstr_guid(riid));
2541 return E_NOINTERFACE;
2544 static ULONG WINAPI ChannelAudioVolume_AddRef(IChannelAudioVolume *iface)
2546 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2547 return AudioSessionControl_AddRef(&This->IAudioSessionControl2_iface);
2550 static ULONG WINAPI ChannelAudioVolume_Release(IChannelAudioVolume *iface)
2552 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2553 return AudioSessionControl_Release(&This->IAudioSessionControl2_iface);
2556 static HRESULT WINAPI ChannelAudioVolume_GetChannelCount(
2557 IChannelAudioVolume *iface, UINT32 *out)
2559 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2560 AudioSession *session = This->session;
2562 TRACE("(%p)->(%p)\n", session, out);
2565 return NULL_PTR_ERR;
2567 *out = session->channel_count;
2572 static HRESULT WINAPI ChannelAudioVolume_SetChannelVolume(
2573 IChannelAudioVolume *iface, UINT32 index, float level,
2574 const GUID *context)
2576 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2577 AudioSession *session = This->session;
2580 TRACE("(%p)->(%d, %f, %s)\n", session, index, level,
2581 wine_dbgstr_guid(context));
2583 if(level < 0.f || level > 1.f)
2584 return E_INVALIDARG;
2586 if(index >= session->channel_count)
2587 return E_INVALIDARG;
2590 FIXME("Notifications not supported yet\n");
2592 EnterCriticalSection(&session->lock);
2594 session->channel_vols[index] = level;
2596 ret = oss_session_setvol(session, index);
2598 LeaveCriticalSection(&session->lock);
2603 static HRESULT WINAPI ChannelAudioVolume_GetChannelVolume(
2604 IChannelAudioVolume *iface, UINT32 index, float *level)
2606 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2607 AudioSession *session = This->session;
2609 TRACE("(%p)->(%d, %p)\n", session, index, level);
2612 return NULL_PTR_ERR;
2614 if(index >= session->channel_count)
2615 return E_INVALIDARG;
2617 *level = session->channel_vols[index];
2622 static HRESULT WINAPI ChannelAudioVolume_SetAllVolumes(
2623 IChannelAudioVolume *iface, UINT32 count, const float *levels,
2624 const GUID *context)
2626 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2627 AudioSession *session = This->session;
2631 TRACE("(%p)->(%d, %p, %s)\n", session, count, levels,
2632 wine_dbgstr_guid(context));
2635 return NULL_PTR_ERR;
2637 if(count != session->channel_count)
2638 return E_INVALIDARG;
2641 FIXME("Notifications not supported yet\n");
2643 EnterCriticalSection(&session->lock);
2645 for(i = 0; i < count; ++i)
2646 session->channel_vols[i] = levels[i];
2648 ret = oss_session_setvol(session, -1);
2650 LeaveCriticalSection(&session->lock);
2655 static HRESULT WINAPI ChannelAudioVolume_GetAllVolumes(
2656 IChannelAudioVolume *iface, UINT32 count, float *levels)
2658 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2659 AudioSession *session = This->session;
2662 TRACE("(%p)->(%d, %p)\n", session, count, levels);
2665 return NULL_PTR_ERR;
2667 if(count != session->channel_count)
2668 return E_INVALIDARG;
2670 for(i = 0; i < count; ++i)
2671 levels[i] = session->channel_vols[i];
2676 static const IChannelAudioVolumeVtbl ChannelAudioVolume_Vtbl =
2678 ChannelAudioVolume_QueryInterface,
2679 ChannelAudioVolume_AddRef,
2680 ChannelAudioVolume_Release,
2681 ChannelAudioVolume_GetChannelCount,
2682 ChannelAudioVolume_SetChannelVolume,
2683 ChannelAudioVolume_GetChannelVolume,
2684 ChannelAudioVolume_SetAllVolumes,
2685 ChannelAudioVolume_GetAllVolumes