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"
56 WINE_DEFAULT_DEBUG_CHANNEL(oss);
58 #define NULL_PTR_ERR MAKE_HRESULT(SEVERITY_ERROR, FACILITY_WIN32, RPC_X_NULL_REF_POINTER)
60 static const REFERENCE_TIME DefaultPeriod = 200000;
61 static const REFERENCE_TIME MinimumPeriod = 100000;
64 typedef struct ACImpl ACImpl;
66 typedef struct _AudioSession {
76 CRITICAL_SECTION lock;
81 typedef struct _AudioSessionWrapper {
82 IAudioSessionControl2 IAudioSessionControl2_iface;
83 IChannelAudioVolume IChannelAudioVolume_iface;
84 ISimpleAudioVolume ISimpleAudioVolume_iface;
89 AudioSession *session;
90 } AudioSessionWrapper;
93 IAudioClient IAudioClient_iface;
94 IAudioRenderClient IAudioRenderClient_iface;
95 IAudioCaptureClient IAudioCaptureClient_iface;
96 IAudioClock IAudioClock_iface;
97 IAudioClock2 IAudioClock2_iface;
98 IAudioStreamVolume IAudioStreamVolume_iface;
108 AUDCLNT_SHAREMODE share;
115 BOOL initted, playing;
116 UINT64 written_frames, held_frames, tmp_buffer_frames, inbuf_frames;
117 UINT32 period_us, bufsize_frames;
118 UINT32 lcl_offs_frames; /* offs into local_buffer where valid data starts */
120 BYTE *local_buffer, *tmp_buffer;
124 CRITICAL_SECTION lock;
126 AudioSession *session;
127 AudioSessionWrapper *session_wrapper;
134 LOCKED_NORMAL, /* public buffer piece is from local_buffer */
135 LOCKED_WRAPPED /* public buffer piece is in tmp_buffer */
138 static HANDLE g_timer_q;
140 static CRITICAL_SECTION g_sessions_lock;
141 static struct list g_sessions = LIST_INIT(g_sessions);
143 static AudioSessionWrapper *AudioSessionWrapper_Create(ACImpl *client);
144 static HRESULT oss_setvol(ACImpl *This, UINT32 index);
146 static const IAudioClientVtbl AudioClient_Vtbl;
147 static const IAudioRenderClientVtbl AudioRenderClient_Vtbl;
148 static const IAudioCaptureClientVtbl AudioCaptureClient_Vtbl;
149 static const IAudioSessionControl2Vtbl AudioSessionControl2_Vtbl;
150 static const ISimpleAudioVolumeVtbl SimpleAudioVolume_Vtbl;
151 static const IAudioClockVtbl AudioClock_Vtbl;
152 static const IAudioClock2Vtbl AudioClock2_Vtbl;
153 static const IAudioStreamVolumeVtbl AudioStreamVolume_Vtbl;
154 static const IChannelAudioVolumeVtbl ChannelAudioVolume_Vtbl;
156 static inline ACImpl *impl_from_IAudioClient(IAudioClient *iface)
158 return CONTAINING_RECORD(iface, ACImpl, IAudioClient_iface);
161 static inline ACImpl *impl_from_IAudioRenderClient(IAudioRenderClient *iface)
163 return CONTAINING_RECORD(iface, ACImpl, IAudioRenderClient_iface);
166 static inline ACImpl *impl_from_IAudioCaptureClient(IAudioCaptureClient *iface)
168 return CONTAINING_RECORD(iface, ACImpl, IAudioCaptureClient_iface);
171 static inline AudioSessionWrapper *impl_from_IAudioSessionControl2(IAudioSessionControl2 *iface)
173 return CONTAINING_RECORD(iface, AudioSessionWrapper, IAudioSessionControl2_iface);
176 static inline AudioSessionWrapper *impl_from_ISimpleAudioVolume(ISimpleAudioVolume *iface)
178 return CONTAINING_RECORD(iface, AudioSessionWrapper, ISimpleAudioVolume_iface);
181 static inline AudioSessionWrapper *impl_from_IChannelAudioVolume(IChannelAudioVolume *iface)
183 return CONTAINING_RECORD(iface, AudioSessionWrapper, IChannelAudioVolume_iface);
186 static inline ACImpl *impl_from_IAudioClock(IAudioClock *iface)
188 return CONTAINING_RECORD(iface, ACImpl, IAudioClock_iface);
191 static inline ACImpl *impl_from_IAudioClock2(IAudioClock2 *iface)
193 return CONTAINING_RECORD(iface, ACImpl, IAudioClock2_iface);
196 static inline ACImpl *impl_from_IAudioStreamVolume(IAudioStreamVolume *iface)
198 return CONTAINING_RECORD(iface, ACImpl, IAudioStreamVolume_iface);
201 BOOL WINAPI DllMain(HINSTANCE dll, DWORD reason, void *reserved)
203 if(reason == DLL_PROCESS_ATTACH){
204 g_timer_q = CreateTimerQueue();
208 InitializeCriticalSection(&g_sessions_lock);
214 HRESULT WINAPI AUDDRV_GetEndpointIDs(EDataFlow flow, WCHAR ***ids, void ***keys,
215 UINT *num, UINT *def_index)
219 static int print_once = 0;
221 TRACE("%d %p %p %p\n", flow, ids, num, def_index);
223 mixer_fd = open("/dev/mixer", O_RDONLY, 0);
225 ERR("OSS /dev/mixer doesn't seem to exist\n");
226 return AUDCLNT_E_SERVICE_NOT_RUNNING;
229 if(ioctl(mixer_fd, SNDCTL_SYSINFO, &sysinfo) < 0){
233 ERR("OSS version too old, need at least OSSv4\n");
234 return AUDCLNT_E_SERVICE_NOT_RUNNING;
237 ERR("Error getting SNDCTL_SYSINFO: %d (%s)\n", errno, strerror(errno));
242 TRACE("OSS sysinfo:\n");
243 TRACE("product: %s\n", sysinfo.product);
244 TRACE("version: %s\n", sysinfo.version);
245 TRACE("versionnum: %x\n", sysinfo.versionnum);
246 TRACE("numaudios: %d\n", sysinfo.numaudios);
247 TRACE("nummixers: %d\n", sysinfo.nummixers);
248 TRACE("numcards: %d\n", sysinfo.numcards);
249 TRACE("numaudioengines: %d\n", sysinfo.numaudioengines);
253 if(sysinfo.numaudios <= 0){
254 WARN("No audio devices!\n");
256 return AUDCLNT_E_SERVICE_NOT_RUNNING;
259 *ids = HeapAlloc(GetProcessHeap(), 0, sysinfo.numaudios * sizeof(WCHAR *));
260 *keys = HeapAlloc(GetProcessHeap(), 0, sysinfo.numaudios * sizeof(char *));
264 for(i = 0; i < sysinfo.numaudios; ++i){
265 oss_audioinfo ai = {0};
268 if(ioctl(mixer_fd, SNDCTL_AUDIOINFO, &ai) < 0){
269 WARN("Error getting AUDIOINFO for dev %d: %d (%s)\n", i, errno,
274 if((flow == eCapture && (ai.caps & PCM_CAP_INPUT)) ||
275 (flow == eRender && (ai.caps & PCM_CAP_OUTPUT))){
278 (*keys)[*num] = HeapAlloc(GetProcessHeap(), 0,
279 strlen(ai.devnode) + 1);
281 for(i = 0; i < *num; ++i){
282 HeapFree(GetProcessHeap(), 0, (*ids)[i]);
283 HeapFree(GetProcessHeap(), 0, (*keys)[i]);
285 HeapFree(GetProcessHeap(), 0, *ids);
286 HeapFree(GetProcessHeap(), 0, *keys);
288 return E_OUTOFMEMORY;
290 strcpy((*keys)[*num], ai.devnode);
292 len = MultiByteToWideChar(CP_UNIXCP, 0, ai.name, -1, NULL, 0);
293 (*ids)[*num] = HeapAlloc(GetProcessHeap(), 0,
294 len * sizeof(WCHAR));
296 HeapFree(GetProcessHeap(), 0, (*keys)[*num]);
297 for(i = 0; i < *num; ++i){
298 HeapFree(GetProcessHeap(), 0, (*ids)[i]);
299 HeapFree(GetProcessHeap(), 0, (*keys)[i]);
301 HeapFree(GetProcessHeap(), 0, *ids);
302 HeapFree(GetProcessHeap(), 0, *keys);
304 return E_OUTOFMEMORY;
306 MultiByteToWideChar(CP_UNIXCP, 0, ai.name, -1,
309 if(ai.caps & PCM_CAP_DEFAULT)
324 HRESULT WINAPI AUDDRV_GetAudioEndpoint(char *devnode, IMMDevice *dev,
325 EDataFlow dataflow, IAudioClient **out)
329 TRACE("%s %p %d %p\n", devnode, dev, dataflow, out);
331 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ACImpl));
333 return E_OUTOFMEMORY;
335 if(dataflow == eRender)
336 This->fd = open(devnode, O_WRONLY, 0);
337 else if(dataflow == eCapture)
338 This->fd = open(devnode, O_RDONLY, 0);
340 HeapFree(GetProcessHeap(), 0, This);
344 ERR("Unable to open device %s: %d (%s)\n", devnode, errno,
346 HeapFree(GetProcessHeap(), 0, This);
347 return AUDCLNT_E_DEVICE_INVALIDATED;
350 This->dataflow = dataflow;
353 if(ioctl(This->fd, SNDCTL_ENGINEINFO, &This->ai) < 0){
354 ERR("Unable to get audio info for device %s: %d (%s)\n", devnode,
355 errno, strerror(errno));
357 HeapFree(GetProcessHeap(), 0, This);
361 TRACE("OSS audioinfo:\n");
362 TRACE("devnode: %s\n", This->ai.devnode);
363 TRACE("name: %s\n", This->ai.name);
364 TRACE("busy: %x\n", This->ai.busy);
365 TRACE("caps: %x\n", This->ai.caps);
366 TRACE("iformats: %x\n", This->ai.iformats);
367 TRACE("oformats: %x\n", This->ai.oformats);
368 TRACE("enabled: %d\n", This->ai.enabled);
369 TRACE("min_rate: %d\n", This->ai.min_rate);
370 TRACE("max_rate: %d\n", This->ai.max_rate);
371 TRACE("min_channels: %d\n", This->ai.min_channels);
372 TRACE("max_channels: %d\n", This->ai.max_channels);
374 This->IAudioClient_iface.lpVtbl = &AudioClient_Vtbl;
375 This->IAudioRenderClient_iface.lpVtbl = &AudioRenderClient_Vtbl;
376 This->IAudioCaptureClient_iface.lpVtbl = &AudioCaptureClient_Vtbl;
377 This->IAudioClock_iface.lpVtbl = &AudioClock_Vtbl;
378 This->IAudioClock2_iface.lpVtbl = &AudioClock2_Vtbl;
379 This->IAudioStreamVolume_iface.lpVtbl = &AudioStreamVolume_Vtbl;
381 InitializeCriticalSection(&This->lock);
384 IMMDevice_AddRef(This->parent);
386 IAudioClient_AddRef(&This->IAudioClient_iface);
388 *out = &This->IAudioClient_iface;
393 static HRESULT WINAPI AudioClient_QueryInterface(IAudioClient *iface,
394 REFIID riid, void **ppv)
396 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
401 if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IAudioClient))
404 IUnknown_AddRef((IUnknown*)*ppv);
407 WARN("Unknown interface %s\n", debugstr_guid(riid));
408 return E_NOINTERFACE;
411 static ULONG WINAPI AudioClient_AddRef(IAudioClient *iface)
413 ACImpl *This = impl_from_IAudioClient(iface);
415 ref = InterlockedIncrement(&This->ref);
416 TRACE("(%p) Refcount now %u\n", This, ref);
420 static ULONG WINAPI AudioClient_Release(IAudioClient *iface)
422 ACImpl *This = impl_from_IAudioClient(iface);
424 ref = InterlockedDecrement(&This->ref);
425 TRACE("(%p) Refcount now %u\n", This, ref);
427 IAudioClient_Stop(iface);
428 IMMDevice_Release(This->parent);
429 DeleteCriticalSection(&This->lock);
432 EnterCriticalSection(&g_sessions_lock);
433 list_remove(&This->entry);
434 if(list_empty(&This->session->clients)){
435 list_remove(&This->session->entry);
436 DeleteCriticalSection(&This->session->lock);
437 HeapFree(GetProcessHeap(), 0, This->session->channel_vols);
438 HeapFree(GetProcessHeap(), 0, This->session);
440 LeaveCriticalSection(&g_sessions_lock);
442 HeapFree(GetProcessHeap(), 0, This->vols);
443 HeapFree(GetProcessHeap(), 0, This->local_buffer);
444 HeapFree(GetProcessHeap(), 0, This->tmp_buffer);
445 CoTaskMemFree(This->fmt);
446 HeapFree(GetProcessHeap(), 0, This);
451 static void dump_fmt(const WAVEFORMATEX *fmt)
453 TRACE("wFormatTag: 0x%x (", fmt->wFormatTag);
454 switch(fmt->wFormatTag){
455 case WAVE_FORMAT_PCM:
456 TRACE("WAVE_FORMAT_PCM");
458 case WAVE_FORMAT_IEEE_FLOAT:
459 TRACE("WAVE_FORMAT_IEEE_FLOAT");
461 case WAVE_FORMAT_EXTENSIBLE:
462 TRACE("WAVE_FORMAT_EXTENSIBLE");
470 TRACE("nChannels: %u\n", fmt->nChannels);
471 TRACE("nSamplesPerSec: %u\n", fmt->nSamplesPerSec);
472 TRACE("nAvgBytesPerSec: %u\n", fmt->nAvgBytesPerSec);
473 TRACE("nBlockAlign: %u\n", fmt->nBlockAlign);
474 TRACE("wBitsPerSample: %u\n", fmt->wBitsPerSample);
475 TRACE("cbSize: %u\n", fmt->cbSize);
477 if(fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE){
478 WAVEFORMATEXTENSIBLE *fmtex = (void*)fmt;
479 TRACE("dwChannelMask: %08x\n", fmtex->dwChannelMask);
480 TRACE("Samples: %04x\n", fmtex->Samples.wReserved);
481 TRACE("SubFormat: %s\n", wine_dbgstr_guid(&fmtex->SubFormat));
485 static DWORD get_channel_mask(unsigned int channels)
491 return SPEAKER_FRONT_CENTER;
493 return SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
495 return SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT |
496 SPEAKER_LOW_FREQUENCY;
498 return SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_BACK_LEFT |
501 return SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_BACK_LEFT |
502 SPEAKER_BACK_RIGHT | SPEAKER_LOW_FREQUENCY;
504 return SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_BACK_LEFT |
505 SPEAKER_BACK_RIGHT | SPEAKER_LOW_FREQUENCY | SPEAKER_FRONT_CENTER;
507 return SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_BACK_LEFT |
508 SPEAKER_BACK_RIGHT | SPEAKER_LOW_FREQUENCY | SPEAKER_FRONT_CENTER |
511 FIXME("Unknown speaker configuration: %u\n", channels);
515 static int get_oss_format(const WAVEFORMATEX *fmt)
517 WAVEFORMATEXTENSIBLE *fmtex = (WAVEFORMATEXTENSIBLE*)fmt;
519 if(fmt->wFormatTag == WAVE_FORMAT_PCM ||
520 (fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
521 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM))){
522 switch(fmt->wBitsPerSample){
536 if(fmt->wFormatTag == WAVE_FORMAT_IEEE_FLOAT ||
537 (fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
538 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT))){
539 if(fmt->wBitsPerSample != 32)
549 static WAVEFORMATEX *clone_format(const WAVEFORMATEX *fmt)
554 if(fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
555 size = sizeof(WAVEFORMATEXTENSIBLE);
557 size = sizeof(WAVEFORMATEX);
559 ret = CoTaskMemAlloc(size);
563 memcpy(ret, fmt, size);
565 ret->cbSize = size - sizeof(WAVEFORMATEX);
570 static HRESULT setup_oss_device(ACImpl *This, const WAVEFORMATEX *fmt,
576 WAVEFORMATEXTENSIBLE *fmtex = (void*)fmt;
577 WAVEFORMATEX *closest = NULL;
579 tmp = oss_format = get_oss_format(fmt);
581 return AUDCLNT_E_UNSUPPORTED_FORMAT;
582 if(ioctl(This->fd, SNDCTL_DSP_SETFMT, &tmp) < 0){
583 WARN("SETFMT failed: %d (%s)\n", errno, strerror(errno));
586 if(tmp != oss_format){
587 TRACE("Format unsupported by this OSS version: %x\n", oss_format);
588 return AUDCLNT_E_UNSUPPORTED_FORMAT;
591 closest = clone_format(fmt);
593 tmp = fmt->nSamplesPerSec;
594 if(ioctl(This->fd, SNDCTL_DSP_SPEED, &tmp) < 0){
595 WARN("SPEED failed: %d (%s)\n", errno, strerror(errno));
596 CoTaskMemFree(closest);
599 tenth = fmt->nSamplesPerSec * 0.1;
600 if(tmp > fmt->nSamplesPerSec + tenth || tmp < fmt->nSamplesPerSec - tenth){
602 closest->nSamplesPerSec = tmp;
605 tmp = fmt->nChannels;
606 if(ioctl(This->fd, SNDCTL_DSP_CHANNELS, &tmp) < 0){
607 WARN("CHANNELS failed: %d (%s)\n", errno, strerror(errno));
608 CoTaskMemFree(closest);
611 if(tmp != fmt->nChannels){
613 closest->nChannels = tmp;
616 if(closest->wFormatTag == WAVE_FORMAT_EXTENSIBLE){
617 DWORD mask = get_channel_mask(closest->nChannels);
619 ((WAVEFORMATEXTENSIBLE*)closest)->dwChannelMask = mask;
621 if(fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
622 fmtex->dwChannelMask != mask)
626 if(ret == S_OK || !out){
627 CoTaskMemFree( closest);
631 closest->nBlockAlign =
632 closest->nChannels * closest->wBitsPerSample / 8;
633 closest->nAvgBytesPerSec =
634 closest->nBlockAlign * closest->nSamplesPerSec;
638 TRACE("returning: %08x\n", ret);
642 static AudioSession *create_session(const GUID *guid, EDataFlow flow,
647 ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(AudioSession));
651 memcpy(&ret->guid, guid, sizeof(GUID));
653 ret->dataflow = flow;
655 list_init(&ret->clients);
657 list_add_head(&g_sessions, &ret->entry);
659 InitializeCriticalSection(&ret->lock);
661 ret->channel_count = num_channels;
662 ret->channel_vols = HeapAlloc(GetProcessHeap(), 0,
663 sizeof(float) * num_channels);
664 if(!ret->channel_vols){
665 HeapFree(GetProcessHeap(), 0, ret);
669 for(; num_channels > 0; --num_channels)
670 ret->channel_vols[num_channels - 1] = 1.f;
672 ret->master_vol = 1.f;
677 static HRESULT WINAPI AudioClient_Initialize(IAudioClient *iface,
678 AUDCLNT_SHAREMODE mode, DWORD flags, REFERENCE_TIME duration,
679 REFERENCE_TIME period, const WAVEFORMATEX *fmt,
680 const GUID *sessionguid)
682 ACImpl *This = impl_from_IAudioClient(iface);
686 TRACE("(%p)->(%x, %x, %s, %s, %p, %s)\n", This, mode, flags,
687 wine_dbgstr_longlong(duration), wine_dbgstr_longlong(period), fmt, debugstr_guid(sessionguid));
694 if(mode != AUDCLNT_SHAREMODE_SHARED && mode != AUDCLNT_SHAREMODE_EXCLUSIVE)
695 return AUDCLNT_E_NOT_INITIALIZED;
697 if(flags & ~(AUDCLNT_STREAMFLAGS_CROSSPROCESS |
698 AUDCLNT_STREAMFLAGS_LOOPBACK |
699 AUDCLNT_STREAMFLAGS_EVENTCALLBACK |
700 AUDCLNT_STREAMFLAGS_NOPERSIST |
701 AUDCLNT_STREAMFLAGS_RATEADJUST |
702 AUDCLNT_SESSIONFLAGS_EXPIREWHENUNOWNED |
703 AUDCLNT_SESSIONFLAGS_DISPLAY_HIDE |
704 AUDCLNT_SESSIONFLAGS_DISPLAY_HIDEWHENEXPIRED)){
705 TRACE("Unknown flags: %08x\n", flags);
709 EnterCriticalSection(&This->lock);
712 LeaveCriticalSection(&This->lock);
713 return AUDCLNT_E_ALREADY_INITIALIZED;
716 hr = setup_oss_device(This, fmt, NULL);
718 LeaveCriticalSection(&This->lock);
719 return AUDCLNT_E_UNSUPPORTED_FORMAT;
722 LeaveCriticalSection(&This->lock);
727 if(ioctl(This->fd, SNDCTL_DSP_SETTRIGGER, &mask) < 0){
728 LeaveCriticalSection(&This->lock);
729 WARN("SETTRIGGER failed: %d (%s)\n", errno, strerror(errno));
733 mask = (100 << 8) | 100;
734 if(ioctl(This->fd, SNDCTL_DSP_SETPLAYVOL, &mask) < 0)
735 WARN("SETPLAYVOL failed: %d (%s)\n", errno, strerror(errno));
737 This->fmt = clone_format(fmt);
739 LeaveCriticalSection(&This->lock);
740 return E_OUTOFMEMORY;
744 This->period_us = period / 10;
746 This->period_us = DefaultPeriod / 10;
748 This->bufsize_frames = ceil(fmt->nSamplesPerSec * (duration / 10000000.));
749 This->local_buffer = HeapAlloc(GetProcessHeap(), 0,
750 This->bufsize_frames * fmt->nBlockAlign);
751 if(!This->local_buffer){
752 CoTaskMemFree(This->fmt);
754 LeaveCriticalSection(&This->lock);
755 return E_OUTOFMEMORY;
758 This->vols = HeapAlloc(GetProcessHeap(), 0, fmt->nChannels * sizeof(float));
760 CoTaskMemFree(This->fmt);
762 LeaveCriticalSection(&This->lock);
763 return E_OUTOFMEMORY;
766 for(i = 0; i < fmt->nChannels; ++i)
772 EnterCriticalSection(&g_sessions_lock);
774 if(!sessionguid || IsEqualGUID(sessionguid, &GUID_NULL)){
775 This->session = create_session(&GUID_NULL, This->dataflow,
778 LeaveCriticalSection(&g_sessions_lock);
779 HeapFree(GetProcessHeap(), 0, This->vols);
781 CoTaskMemFree(This->fmt);
783 LeaveCriticalSection(&This->lock);
784 return E_OUTOFMEMORY;
787 AudioSession *session;
789 LIST_FOR_EACH_ENTRY(session, &g_sessions, AudioSession, entry){
790 if(IsEqualGUID(sessionguid, &session->guid) &&
791 This->dataflow == session->dataflow){
792 if(session->channel_count != fmt->nChannels){
793 LeaveCriticalSection(&g_sessions_lock);
794 HeapFree(GetProcessHeap(), 0, This->vols);
796 CoTaskMemFree(This->fmt);
798 LeaveCriticalSection(&This->lock);
801 This->session = session;
806 This->session = create_session(sessionguid, This->dataflow,
809 LeaveCriticalSection(&g_sessions_lock);
810 HeapFree(GetProcessHeap(), 0, This->vols);
812 CoTaskMemFree(This->fmt);
814 LeaveCriticalSection(&This->lock);
815 return E_OUTOFMEMORY;
820 list_add_tail(&This->session->clients, &This->entry);
822 LeaveCriticalSection(&g_sessions_lock);
824 This->initted = TRUE;
826 oss_setvol(This, -1);
828 LeaveCriticalSection(&This->lock);
833 static HRESULT WINAPI AudioClient_GetBufferSize(IAudioClient *iface,
836 ACImpl *This = impl_from_IAudioClient(iface);
838 TRACE("(%p)->(%p)\n", This, frames);
843 EnterCriticalSection(&This->lock);
846 LeaveCriticalSection(&This->lock);
847 return AUDCLNT_E_NOT_INITIALIZED;
850 *frames = This->bufsize_frames;
852 LeaveCriticalSection(&This->lock);
857 static HRESULT WINAPI AudioClient_GetStreamLatency(IAudioClient *iface,
858 REFERENCE_TIME *latency)
860 ACImpl *This = impl_from_IAudioClient(iface);
862 TRACE("(%p)->(%p)\n", This, latency);
867 EnterCriticalSection(&This->lock);
870 LeaveCriticalSection(&This->lock);
871 return AUDCLNT_E_NOT_INITIALIZED;
874 if(This->dataflow == eRender){
878 if(ioctl(This->fd, SNDCTL_DSP_GETODELAY, &delay_bytes) < 0){
879 LeaveCriticalSection(&This->lock);
880 WARN("GETODELAY failed: %d (%s)\n", errno, strerror(errno));
884 delay_s = delay_bytes / (double)(This->fmt->nSamplesPerSec *
885 This->fmt->nBlockAlign);
887 *latency = delay_s * 10000000;
889 *latency = 10000; /* OSS doesn't provide input latency */
891 LeaveCriticalSection(&This->lock);
896 static HRESULT WINAPI AudioClient_GetCurrentPadding(IAudioClient *iface,
899 ACImpl *This = impl_from_IAudioClient(iface);
902 TRACE("(%p)->(%p)\n", This, numpad);
907 EnterCriticalSection(&This->lock);
910 LeaveCriticalSection(&This->lock);
911 return AUDCLNT_E_NOT_INITIALIZED;
914 if(This->dataflow == eRender){
915 if(ioctl(This->fd, SNDCTL_DSP_GETOSPACE, &bi) < 0){
916 LeaveCriticalSection(&This->lock);
917 WARN("GETOSPACE failed: %d (%s)\n", errno, strerror(errno));
921 *numpad = (bi.fragstotal * bi.fragsize - bi.bytes) /
922 This->fmt->nBlockAlign;
924 /* when the OSS buffer has less than one fragment of data, including
925 * no data, it often reports it as some non-zero portion of a
926 * fragment. when it has more than one fragment of data, it reports
927 * it as some multiple of that portion of the fragment size.
929 * so, we have to do some ugly workarounds to report the timing
930 * as accurately as possible */
931 if(*numpad < bi.fragsize / This->fmt->nBlockAlign){
932 *numpad = This->inbuf_frames;
933 This->inbuf_frames = 0;
935 if(*numpad < This->inbuf_frames)
936 This->inbuf_frames = *numpad;
938 *numpad = This->inbuf_frames;
940 }else if(This->dataflow == eCapture){
941 if(ioctl(This->fd, SNDCTL_DSP_GETISPACE, &bi) < 0){
942 LeaveCriticalSection(&This->lock);
943 WARN("GETISPACE failed: %d (%s)\n", errno, strerror(errno));
947 if(bi.bytes <= bi.fragsize)
950 *numpad = bi.bytes / This->fmt->nBlockAlign;
952 LeaveCriticalSection(&This->lock);
956 *numpad += This->held_frames;
958 LeaveCriticalSection(&This->lock);
963 static HRESULT WINAPI AudioClient_IsFormatSupported(IAudioClient *iface,
964 AUDCLNT_SHAREMODE mode, const WAVEFORMATEX *pwfx,
965 WAVEFORMATEX **outpwfx)
967 ACImpl *This = impl_from_IAudioClient(iface);
970 TRACE("(%p)->(%x, %p, %p)\n", This, mode, pwfx, outpwfx);
972 if(!pwfx || (mode == AUDCLNT_SHAREMODE_SHARED && !outpwfx))
975 if(mode != AUDCLNT_SHAREMODE_SHARED && mode != AUDCLNT_SHAREMODE_EXCLUSIVE)
978 if(pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
979 pwfx->cbSize < sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX))
984 EnterCriticalSection(&This->lock);
986 ret = setup_oss_device(This, pwfx, outpwfx);
988 LeaveCriticalSection(&This->lock);
993 static HRESULT WINAPI AudioClient_GetMixFormat(IAudioClient *iface,
996 ACImpl *This = impl_from_IAudioClient(iface);
997 WAVEFORMATEXTENSIBLE *fmt;
1000 TRACE("(%p)->(%p)\n", This, pwfx);
1005 *pwfx = HeapAlloc(GetProcessHeap(), 0, sizeof(WAVEFORMATEXTENSIBLE));
1007 return E_OUTOFMEMORY;
1009 fmt = (WAVEFORMATEXTENSIBLE*)*pwfx;
1011 if(This->dataflow == eRender)
1012 formats = This->ai.oformats;
1013 else if(This->dataflow == eCapture)
1014 formats = This->ai.iformats;
1016 return E_UNEXPECTED;
1018 fmt->Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
1019 if(formats & AFMT_S16_LE){
1020 fmt->Format.wBitsPerSample = 16;
1021 fmt->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
1023 }else if(formats & AFMT_FLOAT){
1024 fmt->Format.wBitsPerSample = 32;
1025 fmt->SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
1027 }else if(formats & AFMT_U8){
1028 fmt->Format.wBitsPerSample = 8;
1029 fmt->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
1030 }else if(formats & AFMT_S32_LE){
1031 fmt->Format.wBitsPerSample = 32;
1032 fmt->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
1033 }else if(formats & AFMT_S24_LE){
1034 fmt->Format.wBitsPerSample = 24;
1035 fmt->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
1037 ERR("Didn't recognize any available OSS formats: %x\n", formats);
1041 fmt->Format.nChannels = This->ai.max_channels;
1042 fmt->Format.nSamplesPerSec = This->ai.max_rate;
1043 fmt->dwChannelMask = get_channel_mask(fmt->Format.nChannels);
1045 fmt->Format.nBlockAlign = (fmt->Format.wBitsPerSample *
1046 fmt->Format.nChannels) / 8;
1047 fmt->Format.nAvgBytesPerSec = fmt->Format.nSamplesPerSec *
1048 fmt->Format.nBlockAlign;
1050 fmt->Samples.wValidBitsPerSample = fmt->Format.wBitsPerSample;
1051 fmt->Format.cbSize = sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
1058 static HRESULT WINAPI AudioClient_GetDevicePeriod(IAudioClient *iface,
1059 REFERENCE_TIME *defperiod, REFERENCE_TIME *minperiod)
1061 ACImpl *This = impl_from_IAudioClient(iface);
1063 TRACE("(%p)->(%p, %p)\n", This, defperiod, minperiod);
1065 if(!defperiod && !minperiod)
1068 EnterCriticalSection(&This->lock);
1071 *defperiod = DefaultPeriod;
1073 *minperiod = MinimumPeriod;
1075 LeaveCriticalSection(&This->lock);
1080 static void oss_write_data(ACImpl *This)
1083 UINT32 written_frames;
1086 This->local_buffer + (This->lcl_offs_frames * This->fmt->nBlockAlign);
1088 if(This->lcl_offs_frames + This->held_frames > This->bufsize_frames)
1089 to_write = This->bufsize_frames - This->lcl_offs_frames;
1091 to_write = This->held_frames;
1093 written = write(This->fd, buf, to_write * This->fmt->nBlockAlign);
1095 WARN("write failed: %d (%s)\n", errno, strerror(errno));
1098 written_frames = written / This->fmt->nBlockAlign;
1100 This->lcl_offs_frames += written_frames;
1101 This->lcl_offs_frames %= This->bufsize_frames;
1102 This->held_frames -= written_frames;
1103 This->inbuf_frames += written_frames;
1105 if(written_frames < to_write){
1106 /* OSS buffer probably full */
1110 if(This->held_frames){
1111 /* wrapped and have some data back at the start to write */
1112 written = write(This->fd, This->local_buffer,
1113 This->held_frames * This->fmt->nBlockAlign);
1115 WARN("write failed: %d (%s)\n", errno, strerror(errno));
1118 written_frames = written / This->fmt->nBlockAlign;
1120 This->lcl_offs_frames += written_frames;
1121 This->lcl_offs_frames %= This->bufsize_frames;
1122 This->held_frames -= written_frames;
1123 This->inbuf_frames += written_frames;
1127 static void oss_read_data(ACImpl *This)
1129 UINT64 pos, readable;
1133 if(ioctl(This->fd, SNDCTL_DSP_GETISPACE, &bi) < 0){
1134 WARN("GETISPACE failed: %d (%s)\n", errno, strerror(errno));
1138 pos = (This->held_frames + This->lcl_offs_frames) % This->bufsize_frames;
1139 readable = (This->bufsize_frames - pos) * This->fmt->nBlockAlign;
1141 if(bi.bytes < readable)
1142 readable = bi.bytes;
1144 nread = read(This->fd, This->local_buffer + pos * This->fmt->nBlockAlign,
1147 WARN("read failed: %d (%s)\n", errno, strerror(errno));
1151 This->held_frames += nread / This->fmt->nBlockAlign;
1153 if(This->held_frames > This->bufsize_frames){
1154 WARN("Overflow of unread data\n");
1155 This->lcl_offs_frames += This->held_frames;
1156 This->lcl_offs_frames %= This->bufsize_frames;
1157 This->held_frames = This->bufsize_frames;
1161 static void CALLBACK oss_period_callback(void *user, BOOLEAN timer)
1163 ACImpl *This = user;
1165 EnterCriticalSection(&This->lock);
1167 if(This->dataflow == eRender)
1168 oss_write_data(This);
1169 else if(This->dataflow == eCapture)
1170 oss_read_data(This);
1173 SetEvent(This->event);
1175 LeaveCriticalSection(&This->lock);
1178 static HRESULT WINAPI AudioClient_Start(IAudioClient *iface)
1180 ACImpl *This = impl_from_IAudioClient(iface);
1183 TRACE("(%p)\n", This);
1185 EnterCriticalSection(&This->lock);
1188 LeaveCriticalSection(&This->lock);
1189 return AUDCLNT_E_NOT_INITIALIZED;
1192 if((This->flags & AUDCLNT_STREAMFLAGS_EVENTCALLBACK) && !This->event){
1193 LeaveCriticalSection(&This->lock);
1194 return AUDCLNT_E_EVENTHANDLE_NOT_SET;
1198 LeaveCriticalSection(&This->lock);
1199 return AUDCLNT_E_NOT_STOPPED;
1202 if(This->dataflow == eRender)
1203 mask = PCM_ENABLE_OUTPUT;
1204 else if(This->dataflow == eCapture)
1205 mask = PCM_ENABLE_INPUT;
1207 LeaveCriticalSection(&This->lock);
1208 return E_UNEXPECTED;
1211 if(ioctl(This->fd, SNDCTL_DSP_SETTRIGGER, &mask) < 0){
1212 LeaveCriticalSection(&This->lock);
1213 WARN("SETTRIGGER failed: %d (%s)\n", errno, strerror(errno));
1217 if(!CreateTimerQueueTimer(&This->timer, g_timer_q,
1218 oss_period_callback, This, 0, This->period_us / 1000,
1219 WT_EXECUTEINTIMERTHREAD))
1220 ERR("Unable to create period timer: %u\n", GetLastError());
1222 This->playing = TRUE;
1224 LeaveCriticalSection(&This->lock);
1229 static HRESULT WINAPI AudioClient_Stop(IAudioClient *iface)
1231 ACImpl *This = impl_from_IAudioClient(iface);
1234 TRACE("(%p)\n", This);
1236 EnterCriticalSection(&This->lock);
1239 LeaveCriticalSection(&This->lock);
1240 return AUDCLNT_E_NOT_INITIALIZED;
1244 LeaveCriticalSection(&This->lock);
1248 if(This->timer && This->timer != INVALID_HANDLE_VALUE){
1249 DeleteTimerQueueTimer(g_timer_q, This->timer,
1250 INVALID_HANDLE_VALUE);
1254 if(ioctl(This->fd, SNDCTL_DSP_HALT, NULL) < 0){
1255 LeaveCriticalSection(&This->lock);
1256 WARN("HALT failed: %d (%s)\n", errno, strerror(errno));
1261 if(ioctl(This->fd, SNDCTL_DSP_SETTRIGGER, &mask) < 0){
1262 LeaveCriticalSection(&This->lock);
1263 WARN("SETTRIGGER failed: %d (%s)\n", errno, strerror(errno));
1267 This->playing = FALSE;
1269 LeaveCriticalSection(&This->lock);
1274 static HRESULT WINAPI AudioClient_Reset(IAudioClient *iface)
1276 ACImpl *This = impl_from_IAudioClient(iface);
1278 TRACE("(%p)\n", This);
1280 EnterCriticalSection(&This->lock);
1283 LeaveCriticalSection(&This->lock);
1284 return AUDCLNT_E_NOT_INITIALIZED;
1288 LeaveCriticalSection(&This->lock);
1289 return AUDCLNT_E_NOT_STOPPED;
1292 This->written_frames = 0;
1293 This->inbuf_frames = 0;
1294 This->held_frames = 0;
1296 if(ioctl(This->fd, SNDCTL_DSP_SKIP, NULL) < 0)
1297 WARN("SKIP failed: %d (%s)\n", errno, strerror(errno));
1299 LeaveCriticalSection(&This->lock);
1304 static HRESULT WINAPI AudioClient_SetEventHandle(IAudioClient *iface,
1307 ACImpl *This = impl_from_IAudioClient(iface);
1309 TRACE("(%p)->(%p)\n", This, event);
1312 return E_INVALIDARG;
1314 EnterCriticalSection(&This->lock);
1317 LeaveCriticalSection(&This->lock);
1318 return AUDCLNT_E_NOT_INITIALIZED;
1321 if(!(This->flags & AUDCLNT_STREAMFLAGS_EVENTCALLBACK)){
1322 LeaveCriticalSection(&This->lock);
1323 return AUDCLNT_E_EVENTHANDLE_NOT_EXPECTED;
1326 This->event = event;
1328 LeaveCriticalSection(&This->lock);
1333 static HRESULT WINAPI AudioClient_GetService(IAudioClient *iface, REFIID riid,
1336 ACImpl *This = impl_from_IAudioClient(iface);
1338 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
1344 EnterCriticalSection(&This->lock);
1347 LeaveCriticalSection(&This->lock);
1348 return AUDCLNT_E_NOT_INITIALIZED;
1351 if(IsEqualIID(riid, &IID_IAudioRenderClient)){
1352 if(This->dataflow != eRender){
1353 LeaveCriticalSection(&This->lock);
1354 return AUDCLNT_E_WRONG_ENDPOINT_TYPE;
1356 *ppv = &This->IAudioRenderClient_iface;
1357 }else if(IsEqualIID(riid, &IID_IAudioCaptureClient)){
1358 if(This->dataflow != eCapture){
1359 LeaveCriticalSection(&This->lock);
1360 return AUDCLNT_E_WRONG_ENDPOINT_TYPE;
1362 *ppv = &This->IAudioCaptureClient_iface;
1363 }else if(IsEqualIID(riid, &IID_IAudioClock)){
1364 *ppv = &This->IAudioClock_iface;
1365 }else if(IsEqualIID(riid, &IID_IAudioStreamVolume)){
1366 *ppv = &This->IAudioStreamVolume_iface;
1367 }else if(IsEqualIID(riid, &IID_IAudioSessionControl)){
1368 if(!This->session_wrapper){
1369 This->session_wrapper = AudioSessionWrapper_Create(This);
1370 if(!This->session_wrapper){
1371 LeaveCriticalSection(&This->lock);
1372 return E_OUTOFMEMORY;
1376 *ppv = &This->session_wrapper->IAudioSessionControl2_iface;
1377 }else if(IsEqualIID(riid, &IID_IChannelAudioVolume)){
1378 if(!This->session_wrapper){
1379 This->session_wrapper = AudioSessionWrapper_Create(This);
1380 if(!This->session_wrapper){
1381 LeaveCriticalSection(&This->lock);
1382 return E_OUTOFMEMORY;
1386 *ppv = &This->session_wrapper->IChannelAudioVolume_iface;
1387 }else if(IsEqualIID(riid, &IID_ISimpleAudioVolume)){
1388 if(!This->session_wrapper){
1389 This->session_wrapper = AudioSessionWrapper_Create(This);
1390 if(!This->session_wrapper){
1391 LeaveCriticalSection(&This->lock);
1392 return E_OUTOFMEMORY;
1396 *ppv = &This->session_wrapper->ISimpleAudioVolume_iface;
1400 IUnknown_AddRef((IUnknown*)*ppv);
1401 LeaveCriticalSection(&This->lock);
1405 LeaveCriticalSection(&This->lock);
1407 FIXME("stub %s\n", debugstr_guid(riid));
1408 return E_NOINTERFACE;
1411 static const IAudioClientVtbl AudioClient_Vtbl =
1413 AudioClient_QueryInterface,
1415 AudioClient_Release,
1416 AudioClient_Initialize,
1417 AudioClient_GetBufferSize,
1418 AudioClient_GetStreamLatency,
1419 AudioClient_GetCurrentPadding,
1420 AudioClient_IsFormatSupported,
1421 AudioClient_GetMixFormat,
1422 AudioClient_GetDevicePeriod,
1426 AudioClient_SetEventHandle,
1427 AudioClient_GetService
1430 static HRESULT WINAPI AudioRenderClient_QueryInterface(
1431 IAudioRenderClient *iface, REFIID riid, void **ppv)
1433 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
1439 if(IsEqualIID(riid, &IID_IUnknown) ||
1440 IsEqualIID(riid, &IID_IAudioRenderClient))
1443 IUnknown_AddRef((IUnknown*)*ppv);
1447 WARN("Unknown interface %s\n", debugstr_guid(riid));
1448 return E_NOINTERFACE;
1451 static ULONG WINAPI AudioRenderClient_AddRef(IAudioRenderClient *iface)
1453 ACImpl *This = impl_from_IAudioRenderClient(iface);
1454 return AudioClient_AddRef(&This->IAudioClient_iface);
1457 static ULONG WINAPI AudioRenderClient_Release(IAudioRenderClient *iface)
1459 ACImpl *This = impl_from_IAudioRenderClient(iface);
1460 return AudioClient_Release(&This->IAudioClient_iface);
1463 static HRESULT WINAPI AudioRenderClient_GetBuffer(IAudioRenderClient *iface,
1464 UINT32 frames, BYTE **data)
1466 ACImpl *This = impl_from_IAudioRenderClient(iface);
1467 UINT32 pad, write_pos;
1470 TRACE("(%p)->(%u, %p)\n", This, frames, data);
1475 EnterCriticalSection(&This->lock);
1477 if(This->buf_state != NOT_LOCKED){
1478 LeaveCriticalSection(&This->lock);
1479 return AUDCLNT_E_OUT_OF_ORDER;
1483 This->buf_state = LOCKED_NORMAL;
1484 LeaveCriticalSection(&This->lock);
1488 hr = IAudioClient_GetCurrentPadding(&This->IAudioClient_iface, &pad);
1490 LeaveCriticalSection(&This->lock);
1494 if(pad + frames > This->bufsize_frames){
1495 LeaveCriticalSection(&This->lock);
1496 return AUDCLNT_E_BUFFER_TOO_LARGE;
1500 (This->lcl_offs_frames + This->held_frames) % This->bufsize_frames;
1501 if(write_pos + frames > This->bufsize_frames){
1502 if(This->tmp_buffer_frames < frames){
1503 if(This->tmp_buffer)
1504 This->tmp_buffer = HeapReAlloc(GetProcessHeap(), 0,
1505 This->tmp_buffer, frames * This->fmt->nBlockAlign);
1507 This->tmp_buffer = HeapAlloc(GetProcessHeap(), 0,
1508 frames * This->fmt->nBlockAlign);
1509 if(!This->tmp_buffer){
1510 LeaveCriticalSection(&This->lock);
1511 return E_OUTOFMEMORY;
1513 This->tmp_buffer_frames = frames;
1515 *data = This->tmp_buffer;
1516 This->buf_state = LOCKED_WRAPPED;
1518 *data = This->local_buffer +
1519 This->lcl_offs_frames * This->fmt->nBlockAlign;
1520 This->buf_state = LOCKED_NORMAL;
1523 LeaveCriticalSection(&This->lock);
1528 static void oss_wrap_buffer(ACImpl *This, BYTE *buffer, UINT32 written_bytes)
1530 UINT32 write_offs_frames =
1531 (This->lcl_offs_frames + This->held_frames) % This->bufsize_frames;
1532 UINT32 write_offs_bytes = write_offs_frames * This->fmt->nBlockAlign;
1533 UINT32 chunk_frames = This->bufsize_frames - write_offs_frames;
1534 UINT32 chunk_bytes = chunk_frames * This->fmt->nBlockAlign;
1536 if(written_bytes < chunk_bytes){
1537 memcpy(This->local_buffer + write_offs_bytes, buffer, written_bytes);
1539 memcpy(This->local_buffer + write_offs_bytes, buffer, chunk_bytes);
1540 memcpy(This->local_buffer, buffer + chunk_bytes,
1541 written_bytes - chunk_bytes);
1545 static HRESULT WINAPI AudioRenderClient_ReleaseBuffer(
1546 IAudioRenderClient *iface, UINT32 written_frames, DWORD flags)
1548 ACImpl *This = impl_from_IAudioRenderClient(iface);
1550 UINT32 written_bytes = written_frames * This->fmt->nBlockAlign;
1552 TRACE("(%p)->(%u, %x)\n", This, written_frames, flags);
1554 EnterCriticalSection(&This->lock);
1556 if(This->buf_state == NOT_LOCKED || !written_frames){
1557 This->buf_state = NOT_LOCKED;
1558 LeaveCriticalSection(&This->lock);
1559 return written_frames ? AUDCLNT_E_OUT_OF_ORDER : S_OK;
1562 if(This->buf_state == LOCKED_NORMAL)
1563 buffer = This->local_buffer +
1564 This->lcl_offs_frames * This->fmt->nBlockAlign;
1566 buffer = This->tmp_buffer;
1568 if(flags & AUDCLNT_BUFFERFLAGS_SILENT){
1569 WAVEFORMATEXTENSIBLE *fmtex = (WAVEFORMATEXTENSIBLE*)This->fmt;
1570 if((This->fmt->wFormatTag == WAVE_FORMAT_PCM ||
1571 (This->fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
1572 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM))) &&
1573 This->fmt->wBitsPerSample == 8)
1574 memset(buffer, 128, written_frames * This->fmt->nBlockAlign);
1576 memset(buffer, 0, written_frames * This->fmt->nBlockAlign);
1579 if(This->held_frames){
1580 if(This->buf_state == LOCKED_WRAPPED)
1581 oss_wrap_buffer(This, buffer, written_bytes);
1583 This->held_frames += written_frames;
1588 w_bytes = write(This->fd, buffer,
1589 written_frames * This->fmt->nBlockAlign);
1591 LeaveCriticalSection(&This->lock);
1592 WARN("write failed: %d (%s)\n", errno, strerror(errno));
1595 w_frames = w_bytes / This->fmt->nBlockAlign;
1596 This->inbuf_frames += w_frames;
1598 if(w_frames < written_frames){
1599 if(This->buf_state == LOCKED_WRAPPED)
1600 oss_wrap_buffer(This, This->tmp_buffer + w_bytes,
1601 written_frames - w_frames);
1603 This->held_frames = written_frames - w_frames;
1607 This->written_frames += written_frames;
1608 This->buf_state = NOT_LOCKED;
1610 LeaveCriticalSection(&This->lock);
1615 static const IAudioRenderClientVtbl AudioRenderClient_Vtbl = {
1616 AudioRenderClient_QueryInterface,
1617 AudioRenderClient_AddRef,
1618 AudioRenderClient_Release,
1619 AudioRenderClient_GetBuffer,
1620 AudioRenderClient_ReleaseBuffer
1623 static HRESULT WINAPI AudioCaptureClient_QueryInterface(
1624 IAudioCaptureClient *iface, REFIID riid, void **ppv)
1626 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
1632 if(IsEqualIID(riid, &IID_IUnknown) ||
1633 IsEqualIID(riid, &IID_IAudioCaptureClient))
1636 IUnknown_AddRef((IUnknown*)*ppv);
1640 WARN("Unknown interface %s\n", debugstr_guid(riid));
1641 return E_NOINTERFACE;
1644 static ULONG WINAPI AudioCaptureClient_AddRef(IAudioCaptureClient *iface)
1646 ACImpl *This = impl_from_IAudioCaptureClient(iface);
1647 return IAudioClient_AddRef(&This->IAudioClient_iface);
1650 static ULONG WINAPI AudioCaptureClient_Release(IAudioCaptureClient *iface)
1652 ACImpl *This = impl_from_IAudioCaptureClient(iface);
1653 return IAudioClient_Release(&This->IAudioClient_iface);
1656 static HRESULT WINAPI AudioCaptureClient_GetBuffer(IAudioCaptureClient *iface,
1657 BYTE **data, UINT32 *frames, DWORD *flags, UINT64 *devpos,
1660 ACImpl *This = impl_from_IAudioCaptureClient(iface);
1663 TRACE("(%p)->(%p, %p, %p, %p, %p)\n", This, data, frames, flags,
1666 if(!data || !frames || !flags)
1669 EnterCriticalSection(&This->lock);
1671 if(This->buf_state != NOT_LOCKED){
1672 LeaveCriticalSection(&This->lock);
1673 return AUDCLNT_E_OUT_OF_ORDER;
1676 hr = IAudioCaptureClient_GetNextPacketSize(iface, frames);
1678 LeaveCriticalSection(&This->lock);
1684 if(This->lcl_offs_frames + *frames > This->bufsize_frames){
1685 UINT32 chunk_bytes, offs_bytes, frames_bytes;
1686 if(This->tmp_buffer_frames < *frames){
1687 if(This->tmp_buffer)
1688 This->tmp_buffer = HeapReAlloc(GetProcessHeap(), 0,
1689 This->tmp_buffer, *frames * This->fmt->nBlockAlign);
1691 This->tmp_buffer = HeapAlloc(GetProcessHeap(), 0,
1692 *frames * This->fmt->nBlockAlign);
1693 if(!This->tmp_buffer){
1694 LeaveCriticalSection(&This->lock);
1695 return E_OUTOFMEMORY;
1697 This->tmp_buffer_frames = *frames;
1700 *data = This->tmp_buffer;
1701 chunk_bytes = (This->bufsize_frames - This->lcl_offs_frames) *
1702 This->fmt->nBlockAlign;
1703 offs_bytes = This->lcl_offs_frames * This->fmt->nBlockAlign;
1704 frames_bytes = *frames * This->fmt->nBlockAlign;
1705 memcpy(This->tmp_buffer, This->local_buffer + offs_bytes, chunk_bytes);
1706 memcpy(This->tmp_buffer, This->local_buffer,
1707 frames_bytes - chunk_bytes);
1709 *data = This->local_buffer +
1710 This->lcl_offs_frames * This->fmt->nBlockAlign;
1712 This->buf_state = LOCKED_NORMAL;
1714 if(devpos || qpcpos)
1715 IAudioClock_GetPosition(&This->IAudioClock_iface, devpos, qpcpos);
1717 LeaveCriticalSection(&This->lock);
1719 return *frames ? S_OK : AUDCLNT_S_BUFFER_EMPTY;
1722 static HRESULT WINAPI AudioCaptureClient_ReleaseBuffer(
1723 IAudioCaptureClient *iface, UINT32 done)
1725 ACImpl *This = impl_from_IAudioCaptureClient(iface);
1727 TRACE("(%p)->(%u)\n", This, done);
1729 EnterCriticalSection(&This->lock);
1731 if(This->buf_state == NOT_LOCKED){
1732 LeaveCriticalSection(&This->lock);
1733 return AUDCLNT_E_OUT_OF_ORDER;
1736 This->held_frames -= done;
1737 This->lcl_offs_frames += done;
1738 This->lcl_offs_frames %= This->bufsize_frames;
1740 This->buf_state = NOT_LOCKED;
1742 LeaveCriticalSection(&This->lock);
1747 static HRESULT WINAPI AudioCaptureClient_GetNextPacketSize(
1748 IAudioCaptureClient *iface, UINT32 *frames)
1750 ACImpl *This = impl_from_IAudioCaptureClient(iface);
1752 TRACE("(%p)->(%p)\n", This, frames);
1754 return AudioClient_GetCurrentPadding(&This->IAudioClient_iface, frames);
1757 static const IAudioCaptureClientVtbl AudioCaptureClient_Vtbl =
1759 AudioCaptureClient_QueryInterface,
1760 AudioCaptureClient_AddRef,
1761 AudioCaptureClient_Release,
1762 AudioCaptureClient_GetBuffer,
1763 AudioCaptureClient_ReleaseBuffer,
1764 AudioCaptureClient_GetNextPacketSize
1767 static HRESULT WINAPI AudioClock_QueryInterface(IAudioClock *iface,
1768 REFIID riid, void **ppv)
1770 ACImpl *This = impl_from_IAudioClock(iface);
1772 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
1778 if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IAudioClock))
1780 else if(IsEqualIID(riid, &IID_IAudioClock2))
1781 *ppv = &This->IAudioClock2_iface;
1783 IUnknown_AddRef((IUnknown*)*ppv);
1787 WARN("Unknown interface %s\n", debugstr_guid(riid));
1788 return E_NOINTERFACE;
1791 static ULONG WINAPI AudioClock_AddRef(IAudioClock *iface)
1793 ACImpl *This = impl_from_IAudioClock(iface);
1794 return IAudioClient_AddRef(&This->IAudioClient_iface);
1797 static ULONG WINAPI AudioClock_Release(IAudioClock *iface)
1799 ACImpl *This = impl_from_IAudioClock(iface);
1800 return IAudioClient_Release(&This->IAudioClient_iface);
1803 static HRESULT WINAPI AudioClock_GetFrequency(IAudioClock *iface, UINT64 *freq)
1805 ACImpl *This = impl_from_IAudioClock(iface);
1807 TRACE("(%p)->(%p)\n", This, freq);
1809 *freq = This->fmt->nSamplesPerSec;
1814 static HRESULT WINAPI AudioClock_GetPosition(IAudioClock *iface, UINT64 *pos,
1817 ACImpl *This = impl_from_IAudioClock(iface);
1821 TRACE("(%p)->(%p, %p)\n", This, pos, qpctime);
1826 EnterCriticalSection(&This->lock);
1828 hr = IAudioClient_GetCurrentPadding(&This->IAudioClient_iface, &pad);
1830 LeaveCriticalSection(&This->lock);
1834 if(This->dataflow == eRender)
1835 *pos = This->written_frames - pad;
1836 else if(This->dataflow == eCapture)
1837 *pos = This->written_frames + pad;
1839 LeaveCriticalSection(&This->lock);
1842 LARGE_INTEGER stamp, freq;
1843 QueryPerformanceCounter(&stamp);
1844 QueryPerformanceFrequency(&freq);
1845 *qpctime = (stamp.QuadPart * (INT64)10000000) / freq.QuadPart;
1851 static HRESULT WINAPI AudioClock_GetCharacteristics(IAudioClock *iface,
1854 ACImpl *This = impl_from_IAudioClock(iface);
1856 TRACE("(%p)->(%p)\n", This, chars);
1861 *chars = AUDIOCLOCK_CHARACTERISTIC_FIXED_FREQ;
1866 static const IAudioClockVtbl AudioClock_Vtbl =
1868 AudioClock_QueryInterface,
1871 AudioClock_GetFrequency,
1872 AudioClock_GetPosition,
1873 AudioClock_GetCharacteristics
1876 static HRESULT WINAPI AudioClock2_QueryInterface(IAudioClock2 *iface,
1877 REFIID riid, void **ppv)
1879 ACImpl *This = impl_from_IAudioClock2(iface);
1880 return IAudioClock_QueryInterface(&This->IAudioClock_iface, riid, ppv);
1883 static ULONG WINAPI AudioClock2_AddRef(IAudioClock2 *iface)
1885 ACImpl *This = impl_from_IAudioClock2(iface);
1886 return IAudioClient_AddRef(&This->IAudioClient_iface);
1889 static ULONG WINAPI AudioClock2_Release(IAudioClock2 *iface)
1891 ACImpl *This = impl_from_IAudioClock2(iface);
1892 return IAudioClient_Release(&This->IAudioClient_iface);
1895 static HRESULT WINAPI AudioClock2_GetDevicePosition(IAudioClock2 *iface,
1896 UINT64 *pos, UINT64 *qpctime)
1898 ACImpl *This = impl_from_IAudioClock2(iface);
1900 FIXME("(%p)->(%p, %p)\n", This, pos, qpctime);
1905 static const IAudioClock2Vtbl AudioClock2_Vtbl =
1907 AudioClock2_QueryInterface,
1909 AudioClock2_Release,
1910 AudioClock2_GetDevicePosition
1913 static AudioSessionWrapper *AudioSessionWrapper_Create(ACImpl *client)
1915 AudioSessionWrapper *ret;
1917 ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1918 sizeof(AudioSessionWrapper));
1922 ret->IAudioSessionControl2_iface.lpVtbl = &AudioSessionControl2_Vtbl;
1923 ret->ISimpleAudioVolume_iface.lpVtbl = &SimpleAudioVolume_Vtbl;
1924 ret->IChannelAudioVolume_iface.lpVtbl = &ChannelAudioVolume_Vtbl;
1926 ret->client = client;
1927 ret->session = client->session;
1928 AudioClient_AddRef(&client->IAudioClient_iface);
1933 static HRESULT WINAPI AudioSessionControl_QueryInterface(
1934 IAudioSessionControl2 *iface, REFIID riid, void **ppv)
1936 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
1942 if(IsEqualIID(riid, &IID_IUnknown) ||
1943 IsEqualIID(riid, &IID_IAudioSessionControl) ||
1944 IsEqualIID(riid, &IID_IAudioSessionControl2))
1947 IUnknown_AddRef((IUnknown*)*ppv);
1951 WARN("Unknown interface %s\n", debugstr_guid(riid));
1952 return E_NOINTERFACE;
1955 static ULONG WINAPI AudioSessionControl_AddRef(IAudioSessionControl2 *iface)
1957 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
1959 ref = InterlockedIncrement(&This->ref);
1960 TRACE("(%p) Refcount now %u\n", This, ref);
1964 static ULONG WINAPI AudioSessionControl_Release(IAudioSessionControl2 *iface)
1966 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
1968 ref = InterlockedDecrement(&This->ref);
1969 TRACE("(%p) Refcount now %u\n", This, ref);
1971 EnterCriticalSection(&This->client->lock);
1972 This->client->session_wrapper = NULL;
1973 LeaveCriticalSection(&This->client->lock);
1974 AudioClient_Release(&This->client->IAudioClient_iface);
1975 HeapFree(GetProcessHeap(), 0, This);
1980 static HRESULT WINAPI AudioSessionControl_GetState(IAudioSessionControl2 *iface,
1981 AudioSessionState *state)
1983 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
1986 TRACE("(%p)->(%p)\n", This, state);
1989 return NULL_PTR_ERR;
1991 EnterCriticalSection(&g_sessions_lock);
1993 if(list_empty(&This->session->clients)){
1994 *state = AudioSessionStateExpired;
1995 LeaveCriticalSection(&g_sessions_lock);
1999 LIST_FOR_EACH_ENTRY(client, &This->session->clients, ACImpl, entry){
2000 EnterCriticalSection(&client->lock);
2001 if(client->playing){
2002 *state = AudioSessionStateActive;
2003 LeaveCriticalSection(&client->lock);
2004 LeaveCriticalSection(&g_sessions_lock);
2007 LeaveCriticalSection(&client->lock);
2010 LeaveCriticalSection(&g_sessions_lock);
2012 *state = AudioSessionStateInactive;
2017 static HRESULT WINAPI AudioSessionControl_GetDisplayName(
2018 IAudioSessionControl2 *iface, WCHAR **name)
2020 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2022 FIXME("(%p)->(%p) - stub\n", This, name);
2027 static HRESULT WINAPI AudioSessionControl_SetDisplayName(
2028 IAudioSessionControl2 *iface, const WCHAR *name, const GUID *session)
2030 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2032 FIXME("(%p)->(%p, %s) - stub\n", This, name, debugstr_guid(session));
2037 static HRESULT WINAPI AudioSessionControl_GetIconPath(
2038 IAudioSessionControl2 *iface, WCHAR **path)
2040 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2042 FIXME("(%p)->(%p) - stub\n", This, path);
2047 static HRESULT WINAPI AudioSessionControl_SetIconPath(
2048 IAudioSessionControl2 *iface, const WCHAR *path, const GUID *session)
2050 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2052 FIXME("(%p)->(%p, %s) - stub\n", This, path, debugstr_guid(session));
2057 static HRESULT WINAPI AudioSessionControl_GetGroupingParam(
2058 IAudioSessionControl2 *iface, GUID *group)
2060 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2062 FIXME("(%p)->(%p) - stub\n", This, group);
2067 static HRESULT WINAPI AudioSessionControl_SetGroupingParam(
2068 IAudioSessionControl2 *iface, GUID *group, const GUID *session)
2070 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2072 FIXME("(%p)->(%s, %s) - stub\n", This, debugstr_guid(group),
2073 debugstr_guid(session));
2078 static HRESULT WINAPI AudioSessionControl_RegisterAudioSessionNotification(
2079 IAudioSessionControl2 *iface, IAudioSessionEvents *events)
2081 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2083 FIXME("(%p)->(%p) - stub\n", This, events);
2088 static HRESULT WINAPI AudioSessionControl_UnregisterAudioSessionNotification(
2089 IAudioSessionControl2 *iface, IAudioSessionEvents *events)
2091 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2093 FIXME("(%p)->(%p) - stub\n", This, events);
2098 static HRESULT WINAPI AudioSessionControl_GetSessionIdentifier(
2099 IAudioSessionControl2 *iface, WCHAR **id)
2101 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2103 FIXME("(%p)->(%p) - stub\n", This, id);
2108 static HRESULT WINAPI AudioSessionControl_GetSessionInstanceIdentifier(
2109 IAudioSessionControl2 *iface, WCHAR **id)
2111 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2113 FIXME("(%p)->(%p) - stub\n", This, id);
2118 static HRESULT WINAPI AudioSessionControl_GetProcessId(
2119 IAudioSessionControl2 *iface, DWORD *pid)
2121 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2123 TRACE("(%p)->(%p)\n", This, pid);
2128 *pid = GetCurrentProcessId();
2133 static HRESULT WINAPI AudioSessionControl_IsSystemSoundsSession(
2134 IAudioSessionControl2 *iface)
2136 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2138 TRACE("(%p)\n", This);
2143 static HRESULT WINAPI AudioSessionControl_SetDuckingPreference(
2144 IAudioSessionControl2 *iface, BOOL optout)
2146 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2148 TRACE("(%p)->(%d)\n", This, optout);
2153 static const IAudioSessionControl2Vtbl AudioSessionControl2_Vtbl =
2155 AudioSessionControl_QueryInterface,
2156 AudioSessionControl_AddRef,
2157 AudioSessionControl_Release,
2158 AudioSessionControl_GetState,
2159 AudioSessionControl_GetDisplayName,
2160 AudioSessionControl_SetDisplayName,
2161 AudioSessionControl_GetIconPath,
2162 AudioSessionControl_SetIconPath,
2163 AudioSessionControl_GetGroupingParam,
2164 AudioSessionControl_SetGroupingParam,
2165 AudioSessionControl_RegisterAudioSessionNotification,
2166 AudioSessionControl_UnregisterAudioSessionNotification,
2167 AudioSessionControl_GetSessionIdentifier,
2168 AudioSessionControl_GetSessionInstanceIdentifier,
2169 AudioSessionControl_GetProcessId,
2170 AudioSessionControl_IsSystemSoundsSession,
2171 AudioSessionControl_SetDuckingPreference
2174 /* index == -1 means set all channels, otherwise sets only the given channel */
2175 static HRESULT oss_setvol(ACImpl *This, UINT32 index)
2182 if(index == (UINT32)-1){
2185 for(i = 0; i < This->fmt->nChannels; ++i){
2187 hr = oss_setvol(This, i);
2195 /* OSS doesn't support volume control past the first two channels */
2198 if(This->dataflow == eRender){
2199 setreq = SNDCTL_DSP_SETPLAYVOL;
2200 getreq = SNDCTL_DSP_GETPLAYVOL;
2201 }else if(This->dataflow == eCapture){
2202 setreq = SNDCTL_DSP_SETRECVOL;
2203 getreq = SNDCTL_DSP_GETRECVOL;
2205 return E_UNEXPECTED;
2207 if(ioctl(This->fd, getreq, &vol) < 0){
2209 /* device doesn't support this call */
2212 WARN("GET[REC|PLAY]VOL failed: %d (%s)\n", errno, strerror(errno));
2216 level = This->session->master_vol * This->session->channel_vols[index] *
2220 vol = l | (vol & 0xFF00);
2222 vol = (vol & 0xFF) | (l << 8);
2224 if(ioctl(This->fd, setreq, &vol) < 0){
2226 /* device doesn't support this call */
2229 WARN("SET[REC|PLAY]VOL failed: %d (%s)\n", errno, strerror(errno));
2236 static HRESULT oss_session_setvol(AudioSession *session, UINT32 index)
2241 LIST_FOR_EACH_ENTRY(client, &session->clients, ACImpl, entry){
2243 hr = oss_setvol(client, index);
2251 static HRESULT WINAPI SimpleAudioVolume_QueryInterface(
2252 ISimpleAudioVolume *iface, REFIID riid, void **ppv)
2254 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2260 if(IsEqualIID(riid, &IID_IUnknown) ||
2261 IsEqualIID(riid, &IID_ISimpleAudioVolume))
2264 IUnknown_AddRef((IUnknown*)*ppv);
2268 WARN("Unknown interface %s\n", debugstr_guid(riid));
2269 return E_NOINTERFACE;
2272 static ULONG WINAPI SimpleAudioVolume_AddRef(ISimpleAudioVolume *iface)
2274 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2275 return AudioSessionControl_AddRef(&This->IAudioSessionControl2_iface);
2278 static ULONG WINAPI SimpleAudioVolume_Release(ISimpleAudioVolume *iface)
2280 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2281 return AudioSessionControl_Release(&This->IAudioSessionControl2_iface);
2284 static HRESULT WINAPI SimpleAudioVolume_SetMasterVolume(
2285 ISimpleAudioVolume *iface, float level, const GUID *context)
2287 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2288 AudioSession *session = This->session;
2291 TRACE("(%p)->(%f, %s)\n", session, level, wine_dbgstr_guid(context));
2293 if(level < 0.f || level > 1.f)
2294 return E_INVALIDARG;
2297 FIXME("Notifications not supported yet\n");
2299 EnterCriticalSection(&session->lock);
2301 session->master_vol = level;
2303 ret = oss_session_setvol(session, -1);
2305 LeaveCriticalSection(&session->lock);
2310 static HRESULT WINAPI SimpleAudioVolume_GetMasterVolume(
2311 ISimpleAudioVolume *iface, float *level)
2313 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2314 AudioSession *session = This->session;
2316 TRACE("(%p)->(%p)\n", session, level);
2319 return NULL_PTR_ERR;
2321 *level = session->master_vol;
2326 static HRESULT WINAPI SimpleAudioVolume_SetMute(ISimpleAudioVolume *iface,
2327 BOOL mute, const GUID *context)
2329 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2330 AudioSession *session = This->session;
2332 FIXME("(%p)->(%u, %p) - stub\n", session, mute, context);
2337 static HRESULT WINAPI SimpleAudioVolume_GetMute(ISimpleAudioVolume *iface,
2340 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2341 AudioSession *session = This->session;
2343 FIXME("(%p)->(%p) - stub\n", session, mute);
2346 return NULL_PTR_ERR;
2351 static const ISimpleAudioVolumeVtbl SimpleAudioVolume_Vtbl =
2353 SimpleAudioVolume_QueryInterface,
2354 SimpleAudioVolume_AddRef,
2355 SimpleAudioVolume_Release,
2356 SimpleAudioVolume_SetMasterVolume,
2357 SimpleAudioVolume_GetMasterVolume,
2358 SimpleAudioVolume_SetMute,
2359 SimpleAudioVolume_GetMute
2362 static HRESULT WINAPI AudioStreamVolume_QueryInterface(
2363 IAudioStreamVolume *iface, REFIID riid, void **ppv)
2365 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2371 if(IsEqualIID(riid, &IID_IUnknown) ||
2372 IsEqualIID(riid, &IID_IAudioStreamVolume))
2375 IUnknown_AddRef((IUnknown*)*ppv);
2379 WARN("Unknown interface %s\n", debugstr_guid(riid));
2380 return E_NOINTERFACE;
2383 static ULONG WINAPI AudioStreamVolume_AddRef(IAudioStreamVolume *iface)
2385 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2386 return IAudioClient_AddRef(&This->IAudioClient_iface);
2389 static ULONG WINAPI AudioStreamVolume_Release(IAudioStreamVolume *iface)
2391 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2392 return IAudioClient_Release(&This->IAudioClient_iface);
2395 static HRESULT WINAPI AudioStreamVolume_GetChannelCount(
2396 IAudioStreamVolume *iface, UINT32 *out)
2398 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2400 TRACE("(%p)->(%p)\n", This, out);
2405 *out = This->fmt->nChannels;
2410 static HRESULT WINAPI AudioStreamVolume_SetChannelVolume(
2411 IAudioStreamVolume *iface, UINT32 index, float level)
2413 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2416 TRACE("(%p)->(%d, %f)\n", This, index, level);
2418 if(level < 0.f || level > 1.f)
2419 return E_INVALIDARG;
2421 if(index >= This->fmt->nChannels)
2422 return E_INVALIDARG;
2424 EnterCriticalSection(&This->lock);
2426 This->vols[index] = level;
2428 ret = oss_setvol(This, index);
2430 LeaveCriticalSection(&This->lock);
2435 static HRESULT WINAPI AudioStreamVolume_GetChannelVolume(
2436 IAudioStreamVolume *iface, UINT32 index, float *level)
2438 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2440 TRACE("(%p)->(%d, %p)\n", This, index, level);
2445 if(index >= This->fmt->nChannels)
2446 return E_INVALIDARG;
2448 *level = This->vols[index];
2453 static HRESULT WINAPI AudioStreamVolume_SetAllVolumes(
2454 IAudioStreamVolume *iface, UINT32 count, const float *levels)
2456 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2460 TRACE("(%p)->(%d, %p)\n", This, count, levels);
2465 if(count != This->fmt->nChannels)
2466 return E_INVALIDARG;
2468 EnterCriticalSection(&This->lock);
2470 for(i = 0; i < count; ++i)
2471 This->vols[i] = levels[i];
2473 ret = oss_setvol(This, -1);
2475 LeaveCriticalSection(&This->lock);
2480 static HRESULT WINAPI AudioStreamVolume_GetAllVolumes(
2481 IAudioStreamVolume *iface, UINT32 count, float *levels)
2483 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2486 TRACE("(%p)->(%d, %p)\n", This, count, levels);
2491 if(count != This->fmt->nChannels)
2492 return E_INVALIDARG;
2494 EnterCriticalSection(&This->lock);
2496 for(i = 0; i < count; ++i)
2497 levels[i] = This->vols[i];
2499 LeaveCriticalSection(&This->lock);
2504 static const IAudioStreamVolumeVtbl AudioStreamVolume_Vtbl =
2506 AudioStreamVolume_QueryInterface,
2507 AudioStreamVolume_AddRef,
2508 AudioStreamVolume_Release,
2509 AudioStreamVolume_GetChannelCount,
2510 AudioStreamVolume_SetChannelVolume,
2511 AudioStreamVolume_GetChannelVolume,
2512 AudioStreamVolume_SetAllVolumes,
2513 AudioStreamVolume_GetAllVolumes
2516 static HRESULT WINAPI ChannelAudioVolume_QueryInterface(
2517 IChannelAudioVolume *iface, REFIID riid, void **ppv)
2519 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2525 if(IsEqualIID(riid, &IID_IUnknown) ||
2526 IsEqualIID(riid, &IID_IChannelAudioVolume))
2529 IUnknown_AddRef((IUnknown*)*ppv);
2533 WARN("Unknown interface %s\n", debugstr_guid(riid));
2534 return E_NOINTERFACE;
2537 static ULONG WINAPI ChannelAudioVolume_AddRef(IChannelAudioVolume *iface)
2539 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2540 return AudioSessionControl_AddRef(&This->IAudioSessionControl2_iface);
2543 static ULONG WINAPI ChannelAudioVolume_Release(IChannelAudioVolume *iface)
2545 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2546 return AudioSessionControl_Release(&This->IAudioSessionControl2_iface);
2549 static HRESULT WINAPI ChannelAudioVolume_GetChannelCount(
2550 IChannelAudioVolume *iface, UINT32 *out)
2552 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2553 AudioSession *session = This->session;
2555 TRACE("(%p)->(%p)\n", session, out);
2558 return NULL_PTR_ERR;
2560 *out = session->channel_count;
2565 static HRESULT WINAPI ChannelAudioVolume_SetChannelVolume(
2566 IChannelAudioVolume *iface, UINT32 index, float level,
2567 const GUID *context)
2569 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2570 AudioSession *session = This->session;
2573 TRACE("(%p)->(%d, %f, %s)\n", session, index, level,
2574 wine_dbgstr_guid(context));
2576 if(level < 0.f || level > 1.f)
2577 return E_INVALIDARG;
2579 if(index >= session->channel_count)
2580 return E_INVALIDARG;
2583 FIXME("Notifications not supported yet\n");
2585 EnterCriticalSection(&session->lock);
2587 session->channel_vols[index] = level;
2589 ret = oss_session_setvol(session, index);
2591 LeaveCriticalSection(&session->lock);
2596 static HRESULT WINAPI ChannelAudioVolume_GetChannelVolume(
2597 IChannelAudioVolume *iface, UINT32 index, float *level)
2599 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2600 AudioSession *session = This->session;
2602 TRACE("(%p)->(%d, %p)\n", session, index, level);
2605 return NULL_PTR_ERR;
2607 if(index >= session->channel_count)
2608 return E_INVALIDARG;
2610 *level = session->channel_vols[index];
2615 static HRESULT WINAPI ChannelAudioVolume_SetAllVolumes(
2616 IChannelAudioVolume *iface, UINT32 count, const float *levels,
2617 const GUID *context)
2619 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2620 AudioSession *session = This->session;
2624 TRACE("(%p)->(%d, %p, %s)\n", session, count, levels,
2625 wine_dbgstr_guid(context));
2628 return NULL_PTR_ERR;
2630 if(count != session->channel_count)
2631 return E_INVALIDARG;
2634 FIXME("Notifications not supported yet\n");
2636 EnterCriticalSection(&session->lock);
2638 for(i = 0; i < count; ++i)
2639 session->channel_vols[i] = levels[i];
2641 ret = oss_session_setvol(session, -1);
2643 LeaveCriticalSection(&session->lock);
2648 static HRESULT WINAPI ChannelAudioVolume_GetAllVolumes(
2649 IChannelAudioVolume *iface, UINT32 count, float *levels)
2651 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
2652 AudioSession *session = This->session;
2655 TRACE("(%p)->(%d, %p)\n", session, count, levels);
2658 return NULL_PTR_ERR;
2660 if(count != session->channel_count)
2661 return E_INVALIDARG;
2663 for(i = 0; i < count; ++i)
2664 levels[i] = session->channel_vols[i];
2669 static const IChannelAudioVolumeVtbl ChannelAudioVolume_Vtbl =
2671 ChannelAudioVolume_QueryInterface,
2672 ChannelAudioVolume_AddRef,
2673 ChannelAudioVolume_Release,
2674 ChannelAudioVolume_GetChannelCount,
2675 ChannelAudioVolume_SetChannelVolume,
2676 ChannelAudioVolume_GetChannelVolume,
2677 ChannelAudioVolume_SetAllVolumes,
2678 ChannelAudioVolume_GetAllVolumes