winepulse: Prefer PulseAudio driver.
[wine.git] / dlls / winepulse.drv / mmdevdrv.c
blobb2f83c8bea32433b73c022a07efe86fcb3b6fb6c
1 /*
2 * Copyright 2011-2012 Maarten Lankhorst
3 * Copyright 2010-2011 Maarten Lankhorst for CodeWeavers
4 * Copyright 2011 Andrew Eikum for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #define NONAMELESSUNION
22 #define COBJMACROS
23 #define _GNU_SOURCE
25 #include "config.h"
26 #include <poll.h>
27 #include <pthread.h>
29 #include <stdarg.h>
30 #include <unistd.h>
31 #include <math.h>
32 #include <stdio.h>
33 #include <errno.h>
35 #include <pulse/pulseaudio.h>
37 #include "windef.h"
38 #include "winbase.h"
39 #include "winnls.h"
40 #include "winreg.h"
41 #include "wine/debug.h"
42 #include "wine/unicode.h"
43 #include "wine/list.h"
45 #include "ole2.h"
46 #include "dshow.h"
47 #include "dsound.h"
48 #include "propsys.h"
50 #include "initguid.h"
51 #include "ks.h"
52 #include "ksmedia.h"
53 #include "propkey.h"
54 #include "mmdeviceapi.h"
55 #include "audioclient.h"
56 #include "endpointvolume.h"
57 #include "audiopolicy.h"
59 WINE_DEFAULT_DEBUG_CHANNEL(pulse);
61 #define NULL_PTR_ERR MAKE_HRESULT(SEVERITY_ERROR, FACILITY_WIN32, RPC_X_NULL_REF_POINTER)
63 /* From <dlls/mmdevapi/mmdevapi.h> */
64 enum DriverPriority {
65 Priority_Unavailable = 0,
66 Priority_Low,
67 Priority_Neutral,
68 Priority_Preferred
71 static const REFERENCE_TIME MinimumPeriod = 30000;
72 static const REFERENCE_TIME DefaultPeriod = 100000;
74 static pa_context *pulse_ctx;
75 static pa_mainloop *pulse_ml;
77 static HANDLE pulse_thread;
78 static pthread_mutex_t pulse_lock;
79 static pthread_cond_t pulse_cond = PTHREAD_COND_INITIALIZER;
80 static struct list g_sessions = LIST_INIT(g_sessions);
82 static UINT g_phys_speakers_mask = 0;
84 /* Mixer format + period times */
85 static WAVEFORMATEXTENSIBLE pulse_fmt[2];
86 static REFERENCE_TIME pulse_min_period[2], pulse_def_period[2];
88 static GUID pulse_render_guid =
89 { 0xfd47d9cc, 0x4218, 0x4135, { 0x9c, 0xe2, 0x0c, 0x19, 0x5c, 0x87, 0x40, 0x5b } };
90 static GUID pulse_capture_guid =
91 { 0x25da76d0, 0x033c, 0x4235, { 0x90, 0x02, 0x19, 0xf4, 0x88, 0x94, 0xac, 0x6f } };
93 BOOL WINAPI DllMain(HINSTANCE dll, DWORD reason, void *reserved)
95 if (reason == DLL_PROCESS_ATTACH) {
96 pthread_mutexattr_t attr;
98 DisableThreadLibraryCalls(dll);
100 pthread_mutexattr_init(&attr);
101 pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT);
103 if (pthread_mutex_init(&pulse_lock, &attr) != 0)
104 pthread_mutex_init(&pulse_lock, NULL);
105 } else if (reason == DLL_PROCESS_DETACH) {
106 if (pulse_thread)
107 SetThreadPriority(pulse_thread, 0);
108 if (pulse_ctx) {
109 pa_context_disconnect(pulse_ctx);
110 pa_context_unref(pulse_ctx);
112 if (pulse_ml)
113 pa_mainloop_quit(pulse_ml, 0);
114 if (pulse_thread) {
115 WaitForSingleObject(pulse_thread, INFINITE);
116 CloseHandle(pulse_thread);
119 return TRUE;
122 typedef struct ACImpl ACImpl;
124 typedef struct _AudioSession {
125 GUID guid;
126 struct list clients;
128 IMMDevice *device;
130 float master_vol;
131 UINT32 channel_count;
132 float *channel_vols;
133 BOOL mute;
135 struct list entry;
136 } AudioSession;
138 typedef struct _AudioSessionWrapper {
139 IAudioSessionControl2 IAudioSessionControl2_iface;
140 IChannelAudioVolume IChannelAudioVolume_iface;
141 ISimpleAudioVolume ISimpleAudioVolume_iface;
143 LONG ref;
145 ACImpl *client;
146 AudioSession *session;
147 } AudioSessionWrapper;
149 typedef struct _ACPacket {
150 struct list entry;
151 UINT64 qpcpos;
152 BYTE *data;
153 UINT32 discont;
154 } ACPacket;
156 struct ACImpl {
157 IAudioClient IAudioClient_iface;
158 IAudioRenderClient IAudioRenderClient_iface;
159 IAudioCaptureClient IAudioCaptureClient_iface;
160 IAudioClock IAudioClock_iface;
161 IAudioClock2 IAudioClock2_iface;
162 IAudioStreamVolume IAudioStreamVolume_iface;
163 IUnknown *marshal;
164 IMMDevice *parent;
165 struct list entry;
166 float vol[PA_CHANNELS_MAX];
168 LONG ref;
169 EDataFlow dataflow;
170 DWORD flags;
171 AUDCLNT_SHAREMODE share;
172 HANDLE event;
174 UINT32 bufsize_frames, bufsize_bytes, locked, capture_period, pad, started, peek_ofs;
175 void *locked_ptr, *tmp_buffer;
177 pa_stream *stream;
178 pa_sample_spec ss;
179 pa_channel_map map;
181 INT64 clock_lastpos, clock_written;
183 AudioSession *session;
184 AudioSessionWrapper *session_wrapper;
185 struct list packet_free_head;
186 struct list packet_filled_head;
189 static const WCHAR defaultW[] = {'P','u','l','s','e','a','u','d','i','o',0};
191 static const IAudioClientVtbl AudioClient_Vtbl;
192 static const IAudioRenderClientVtbl AudioRenderClient_Vtbl;
193 static const IAudioCaptureClientVtbl AudioCaptureClient_Vtbl;
194 static const IAudioSessionControl2Vtbl AudioSessionControl2_Vtbl;
195 static const ISimpleAudioVolumeVtbl SimpleAudioVolume_Vtbl;
196 static const IChannelAudioVolumeVtbl ChannelAudioVolume_Vtbl;
197 static const IAudioClockVtbl AudioClock_Vtbl;
198 static const IAudioClock2Vtbl AudioClock2_Vtbl;
199 static const IAudioStreamVolumeVtbl AudioStreamVolume_Vtbl;
201 static AudioSessionWrapper *AudioSessionWrapper_Create(ACImpl *client);
203 static inline ACImpl *impl_from_IAudioClient(IAudioClient *iface)
205 return CONTAINING_RECORD(iface, ACImpl, IAudioClient_iface);
208 static inline ACImpl *impl_from_IAudioRenderClient(IAudioRenderClient *iface)
210 return CONTAINING_RECORD(iface, ACImpl, IAudioRenderClient_iface);
213 static inline ACImpl *impl_from_IAudioCaptureClient(IAudioCaptureClient *iface)
215 return CONTAINING_RECORD(iface, ACImpl, IAudioCaptureClient_iface);
218 static inline AudioSessionWrapper *impl_from_IAudioSessionControl2(IAudioSessionControl2 *iface)
220 return CONTAINING_RECORD(iface, AudioSessionWrapper, IAudioSessionControl2_iface);
223 static inline AudioSessionWrapper *impl_from_ISimpleAudioVolume(ISimpleAudioVolume *iface)
225 return CONTAINING_RECORD(iface, AudioSessionWrapper, ISimpleAudioVolume_iface);
228 static inline AudioSessionWrapper *impl_from_IChannelAudioVolume(IChannelAudioVolume *iface)
230 return CONTAINING_RECORD(iface, AudioSessionWrapper, IChannelAudioVolume_iface);
233 static inline ACImpl *impl_from_IAudioClock(IAudioClock *iface)
235 return CONTAINING_RECORD(iface, ACImpl, IAudioClock_iface);
238 static inline ACImpl *impl_from_IAudioClock2(IAudioClock2 *iface)
240 return CONTAINING_RECORD(iface, ACImpl, IAudioClock2_iface);
243 static inline ACImpl *impl_from_IAudioStreamVolume(IAudioStreamVolume *iface)
245 return CONTAINING_RECORD(iface, ACImpl, IAudioStreamVolume_iface);
248 /* Following pulseaudio design here, mainloop has the lock taken whenever
249 * it is handling something for pulse, and the lock is required whenever
250 * doing any pa_* call that can affect the state in any way
252 * pa_cond_wait is used when waiting on results, because the mainloop needs
253 * the same lock taken to affect the state
255 * This is basically the same as the pa_threaded_mainloop implementation,
256 * but that cannot be used because it uses pthread_create directly
258 * pa_threaded_mainloop_(un)lock -> pthread_mutex_(un)lock
259 * pa_threaded_mainloop_signal -> pthread_cond_signal
260 * pa_threaded_mainloop_wait -> pthread_cond_wait
263 static int pulse_poll_func(struct pollfd *ufds, unsigned long nfds, int timeout, void *userdata) {
264 int r;
265 pthread_mutex_unlock(&pulse_lock);
266 r = poll(ufds, nfds, timeout);
267 pthread_mutex_lock(&pulse_lock);
268 return r;
271 static DWORD CALLBACK pulse_mainloop_thread(void *tmp) {
272 int ret;
273 pulse_ml = pa_mainloop_new();
274 pa_mainloop_set_poll_func(pulse_ml, pulse_poll_func, NULL);
275 pthread_mutex_lock(&pulse_lock);
276 pthread_cond_signal(&pulse_cond);
277 pa_mainloop_run(pulse_ml, &ret);
278 pthread_mutex_unlock(&pulse_lock);
279 pa_mainloop_free(pulse_ml);
280 return ret;
283 static void pulse_contextcallback(pa_context *c, void *userdata)
285 switch (pa_context_get_state(c)) {
286 default:
287 FIXME("Unhandled state: %i\n", pa_context_get_state(c));
288 case PA_CONTEXT_CONNECTING:
289 case PA_CONTEXT_UNCONNECTED:
290 case PA_CONTEXT_AUTHORIZING:
291 case PA_CONTEXT_SETTING_NAME:
292 case PA_CONTEXT_TERMINATED:
293 TRACE("State change to %i\n", pa_context_get_state(c));
294 return;
296 case PA_CONTEXT_READY:
297 TRACE("Ready\n");
298 break;
300 case PA_CONTEXT_FAILED:
301 ERR("Context failed: %s\n", pa_strerror(pa_context_errno(c)));
302 break;
304 pthread_cond_signal(&pulse_cond);
307 static void pulse_stream_state(pa_stream *s, void *user)
309 pa_stream_state_t state = pa_stream_get_state(s);
310 TRACE("Stream state changed to %i\n", state);
311 pthread_cond_signal(&pulse_cond);
314 static const enum pa_channel_position pulse_pos_from_wfx[] = {
315 PA_CHANNEL_POSITION_FRONT_LEFT,
316 PA_CHANNEL_POSITION_FRONT_RIGHT,
317 PA_CHANNEL_POSITION_FRONT_CENTER,
318 PA_CHANNEL_POSITION_LFE,
319 PA_CHANNEL_POSITION_REAR_LEFT,
320 PA_CHANNEL_POSITION_REAR_RIGHT,
321 PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER,
322 PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER,
323 PA_CHANNEL_POSITION_REAR_CENTER,
324 PA_CHANNEL_POSITION_SIDE_LEFT,
325 PA_CHANNEL_POSITION_SIDE_RIGHT,
326 PA_CHANNEL_POSITION_TOP_CENTER,
327 PA_CHANNEL_POSITION_TOP_FRONT_LEFT,
328 PA_CHANNEL_POSITION_TOP_FRONT_CENTER,
329 PA_CHANNEL_POSITION_TOP_FRONT_RIGHT,
330 PA_CHANNEL_POSITION_TOP_REAR_LEFT,
331 PA_CHANNEL_POSITION_TOP_REAR_CENTER,
332 PA_CHANNEL_POSITION_TOP_REAR_RIGHT
335 static DWORD pulse_channel_map_to_channel_mask(const pa_channel_map *map) {
336 int i;
337 DWORD mask = 0;
339 for (i = 0; i < map->channels; ++i)
340 switch (map->map[i]) {
341 default: FIXME("Unhandled channel %s\n", pa_channel_position_to_string(map->map[i])); break;
342 case PA_CHANNEL_POSITION_FRONT_LEFT: mask |= SPEAKER_FRONT_LEFT; break;
343 case PA_CHANNEL_POSITION_MONO:
344 case PA_CHANNEL_POSITION_FRONT_CENTER: mask |= SPEAKER_FRONT_CENTER; break;
345 case PA_CHANNEL_POSITION_FRONT_RIGHT: mask |= SPEAKER_FRONT_RIGHT; break;
346 case PA_CHANNEL_POSITION_REAR_LEFT: mask |= SPEAKER_BACK_LEFT; break;
347 case PA_CHANNEL_POSITION_REAR_CENTER: mask |= SPEAKER_BACK_CENTER; break;
348 case PA_CHANNEL_POSITION_REAR_RIGHT: mask |= SPEAKER_BACK_RIGHT; break;
349 case PA_CHANNEL_POSITION_LFE: mask |= SPEAKER_LOW_FREQUENCY; break;
350 case PA_CHANNEL_POSITION_SIDE_LEFT: mask |= SPEAKER_SIDE_LEFT; break;
351 case PA_CHANNEL_POSITION_SIDE_RIGHT: mask |= SPEAKER_SIDE_RIGHT; break;
352 case PA_CHANNEL_POSITION_TOP_CENTER: mask |= SPEAKER_TOP_CENTER; break;
353 case PA_CHANNEL_POSITION_TOP_FRONT_LEFT: mask |= SPEAKER_TOP_FRONT_LEFT; break;
354 case PA_CHANNEL_POSITION_TOP_FRONT_CENTER: mask |= SPEAKER_TOP_FRONT_CENTER; break;
355 case PA_CHANNEL_POSITION_TOP_FRONT_RIGHT: mask |= SPEAKER_TOP_FRONT_RIGHT; break;
356 case PA_CHANNEL_POSITION_TOP_REAR_LEFT: mask |= SPEAKER_TOP_BACK_LEFT; break;
357 case PA_CHANNEL_POSITION_TOP_REAR_CENTER: mask |= SPEAKER_TOP_BACK_CENTER; break;
358 case PA_CHANNEL_POSITION_TOP_REAR_RIGHT: mask |= SPEAKER_TOP_BACK_RIGHT; break;
359 case PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER: mask |= SPEAKER_FRONT_LEFT_OF_CENTER; break;
360 case PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER: mask |= SPEAKER_FRONT_RIGHT_OF_CENTER; break;
363 return mask;
366 static void pulse_probe_settings(int render, WAVEFORMATEXTENSIBLE *fmt) {
367 WAVEFORMATEX *wfx = &fmt->Format;
368 pa_stream *stream;
369 pa_channel_map map;
370 pa_sample_spec ss;
371 pa_buffer_attr attr;
372 int ret;
373 unsigned int length = 0;
375 pa_channel_map_init_auto(&map, 2, PA_CHANNEL_MAP_ALSA);
376 ss.rate = 48000;
377 ss.format = PA_SAMPLE_FLOAT32LE;
378 ss.channels = map.channels;
380 attr.maxlength = -1;
381 attr.tlength = -1;
382 attr.minreq = attr.fragsize = pa_frame_size(&ss);
383 attr.prebuf = 0;
385 stream = pa_stream_new(pulse_ctx, "format test stream", &ss, &map);
386 if (stream)
387 pa_stream_set_state_callback(stream, pulse_stream_state, NULL);
388 if (!stream)
389 ret = -1;
390 else if (render)
391 ret = pa_stream_connect_playback(stream, NULL, &attr,
392 PA_STREAM_START_CORKED|PA_STREAM_FIX_RATE|PA_STREAM_FIX_CHANNELS|PA_STREAM_EARLY_REQUESTS, NULL, NULL);
393 else
394 ret = pa_stream_connect_record(stream, NULL, &attr, PA_STREAM_START_CORKED|PA_STREAM_FIX_RATE|PA_STREAM_FIX_CHANNELS|PA_STREAM_EARLY_REQUESTS);
395 if (ret >= 0) {
396 while (pa_mainloop_iterate(pulse_ml, 1, &ret) >= 0 &&
397 pa_stream_get_state(stream) == PA_STREAM_CREATING)
399 if (pa_stream_get_state(stream) == PA_STREAM_READY) {
400 ss = *pa_stream_get_sample_spec(stream);
401 map = *pa_stream_get_channel_map(stream);
402 if (render)
403 length = pa_stream_get_buffer_attr(stream)->minreq;
404 else
405 length = pa_stream_get_buffer_attr(stream)->fragsize;
406 pa_stream_disconnect(stream);
407 while (pa_mainloop_iterate(pulse_ml, 1, &ret) >= 0 &&
408 pa_stream_get_state(stream) == PA_STREAM_READY)
412 if (stream)
413 pa_stream_unref(stream);
414 if (length)
415 pulse_def_period[!render] = pulse_min_period[!render] = pa_bytes_to_usec(10 * length, &ss);
416 else
417 pulse_min_period[!render] = MinimumPeriod;
418 if (pulse_def_period[!render] <= DefaultPeriod)
419 pulse_def_period[!render] = DefaultPeriod;
421 wfx->wFormatTag = WAVE_FORMAT_EXTENSIBLE;
422 wfx->cbSize = sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
423 wfx->nChannels = ss.channels;
424 wfx->wBitsPerSample = 8 * pa_sample_size_of_format(ss.format);
425 wfx->nSamplesPerSec = ss.rate;
426 wfx->nBlockAlign = pa_frame_size(&ss);
427 wfx->nAvgBytesPerSec = wfx->nSamplesPerSec * wfx->nBlockAlign;
428 if (ss.format != PA_SAMPLE_S24_32LE)
429 fmt->Samples.wValidBitsPerSample = wfx->wBitsPerSample;
430 else
431 fmt->Samples.wValidBitsPerSample = 24;
432 if (ss.format == PA_SAMPLE_FLOAT32LE)
433 fmt->SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
434 else
435 fmt->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
437 fmt->dwChannelMask = pulse_channel_map_to_channel_mask(&map);
440 static HRESULT pulse_connect(void)
442 int len;
443 WCHAR path[PATH_MAX], *name;
444 char *str;
446 if (!pulse_thread)
448 if (!(pulse_thread = CreateThread(NULL, 0, pulse_mainloop_thread, NULL, 0, NULL)))
450 ERR("Failed to create mainloop thread.\n");
451 return E_FAIL;
453 SetThreadPriority(pulse_thread, THREAD_PRIORITY_TIME_CRITICAL);
454 pthread_cond_wait(&pulse_cond, &pulse_lock);
457 if (pulse_ctx && PA_CONTEXT_IS_GOOD(pa_context_get_state(pulse_ctx)))
458 return S_OK;
459 if (pulse_ctx)
460 pa_context_unref(pulse_ctx);
462 GetModuleFileNameW(NULL, path, sizeof(path)/sizeof(*path));
463 name = strrchrW(path, '\\');
464 if (!name)
465 name = path;
466 else
467 name++;
468 len = WideCharToMultiByte(CP_UNIXCP, 0, name, -1, NULL, 0, NULL, NULL);
469 str = pa_xmalloc(len);
470 WideCharToMultiByte(CP_UNIXCP, 0, name, -1, str, len, NULL, NULL);
471 TRACE("Name: %s\n", str);
472 pulse_ctx = pa_context_new(pa_mainloop_get_api(pulse_ml), str);
473 pa_xfree(str);
474 if (!pulse_ctx) {
475 ERR("Failed to create context\n");
476 return E_FAIL;
479 pa_context_set_state_callback(pulse_ctx, pulse_contextcallback, NULL);
481 TRACE("libpulse protocol version: %u. API Version %u\n", pa_context_get_protocol_version(pulse_ctx), PA_API_VERSION);
482 if (pa_context_connect(pulse_ctx, NULL, 0, NULL) < 0)
483 goto fail;
485 /* Wait for connection */
486 while (pthread_cond_wait(&pulse_cond, &pulse_lock)) {
487 pa_context_state_t state = pa_context_get_state(pulse_ctx);
489 if (state == PA_CONTEXT_FAILED || state == PA_CONTEXT_TERMINATED)
490 goto fail;
492 if (state == PA_CONTEXT_READY)
493 break;
496 TRACE("Connected to server %s with protocol version: %i.\n",
497 pa_context_get_server(pulse_ctx),
498 pa_context_get_server_protocol_version(pulse_ctx));
499 return S_OK;
501 fail:
502 pa_context_unref(pulse_ctx);
503 pulse_ctx = NULL;
504 return E_FAIL;
507 /* For default Pulseaudio render device, OR together all of the
508 * PKEY_AudioEndpoint_PhysicalSpeakers values of the sinks. */
509 static void pulse_phys_speakers_cb(pa_context *c, const pa_sink_info *i, int eol, void *userdata)
511 if (i)
512 g_phys_speakers_mask |= pulse_channel_map_to_channel_mask(&i->channel_map);
515 /* some poorly-behaved applications call audio functions during DllMain, so we
516 * have to do as much as possible without creating a new thread. this function
517 * sets up a synchronous connection to verify the server is running and query
518 * static data. */
519 static HRESULT pulse_test_connect(void)
521 int len, ret;
522 WCHAR path[PATH_MAX], *name;
523 char *str;
524 pa_operation *o;
526 pulse_ml = pa_mainloop_new();
528 pa_mainloop_set_poll_func(pulse_ml, pulse_poll_func, NULL);
530 GetModuleFileNameW(NULL, path, sizeof(path)/sizeof(*path));
531 name = strrchrW(path, '\\');
532 if (!name)
533 name = path;
534 else
535 name++;
536 len = WideCharToMultiByte(CP_UNIXCP, 0, name, -1, NULL, 0, NULL, NULL);
537 str = pa_xmalloc(len);
538 WideCharToMultiByte(CP_UNIXCP, 0, name, -1, str, len, NULL, NULL);
539 TRACE("Name: %s\n", str);
540 pulse_ctx = pa_context_new(pa_mainloop_get_api(pulse_ml), str);
541 pa_xfree(str);
542 if (!pulse_ctx) {
543 ERR("Failed to create context\n");
544 pa_mainloop_free(pulse_ml);
545 pulse_ml = NULL;
546 return E_FAIL;
549 pa_context_set_state_callback(pulse_ctx, pulse_contextcallback, NULL);
551 TRACE("libpulse protocol version: %u. API Version %u\n", pa_context_get_protocol_version(pulse_ctx), PA_API_VERSION);
552 if (pa_context_connect(pulse_ctx, NULL, 0, NULL) < 0)
553 goto fail;
555 /* Wait for connection */
556 while (pa_mainloop_iterate(pulse_ml, 1, &ret) >= 0) {
557 pa_context_state_t state = pa_context_get_state(pulse_ctx);
559 if (state == PA_CONTEXT_FAILED || state == PA_CONTEXT_TERMINATED)
560 goto fail;
562 if (state == PA_CONTEXT_READY)
563 break;
566 TRACE("Test-connected to server %s with protocol version: %i.\n",
567 pa_context_get_server(pulse_ctx),
568 pa_context_get_server_protocol_version(pulse_ctx));
570 pulse_probe_settings(1, &pulse_fmt[0]);
571 pulse_probe_settings(0, &pulse_fmt[1]);
573 g_phys_speakers_mask = 0;
574 o = pa_context_get_sink_info_list(pulse_ctx, &pulse_phys_speakers_cb, NULL);
575 if (o) {
576 while (pa_mainloop_iterate(pulse_ml, 1, &ret) >= 0 &&
577 pa_operation_get_state(o) == PA_OPERATION_RUNNING)
579 pa_operation_unref(o);
582 pa_context_unref(pulse_ctx);
583 pulse_ctx = NULL;
584 pa_mainloop_free(pulse_ml);
585 pulse_ml = NULL;
587 return S_OK;
589 fail:
590 pa_context_unref(pulse_ctx);
591 pulse_ctx = NULL;
592 pa_mainloop_free(pulse_ml);
593 pulse_ml = NULL;
595 return E_FAIL;
598 static HRESULT pulse_stream_valid(ACImpl *This) {
599 if (!This->stream)
600 return AUDCLNT_E_NOT_INITIALIZED;
601 if (!This->stream || pa_stream_get_state(This->stream) != PA_STREAM_READY)
602 return AUDCLNT_E_DEVICE_INVALIDATED;
603 return S_OK;
606 static void silence_buffer(pa_sample_format_t format, BYTE *buffer, UINT32 bytes)
608 memset(buffer, format == PA_SAMPLE_U8 ? 0x80 : 0, bytes);
611 static void dump_attr(const pa_buffer_attr *attr) {
612 TRACE("maxlength: %u\n", attr->maxlength);
613 TRACE("minreq: %u\n", attr->minreq);
614 TRACE("fragsize: %u\n", attr->fragsize);
615 TRACE("tlength: %u\n", attr->tlength);
616 TRACE("prebuf: %u\n", attr->prebuf);
619 static void pulse_op_cb(pa_stream *s, int success, void *user) {
620 TRACE("Success: %i\n", success);
621 *(int*)user = success;
622 pthread_cond_signal(&pulse_cond);
625 static void pulse_attr_update(pa_stream *s, void *user) {
626 const pa_buffer_attr *attr = pa_stream_get_buffer_attr(s);
627 TRACE("New attributes or device moved:\n");
628 dump_attr(attr);
631 static void pulse_wr_callback(pa_stream *s, size_t bytes, void *userdata)
633 ACImpl *This = userdata;
634 UINT32 oldpad = This->pad;
636 if (bytes < This->bufsize_bytes)
637 This->pad = This->bufsize_bytes - bytes;
638 else
639 This->pad = 0;
641 if (oldpad == This->pad)
642 return;
644 assert(oldpad > This->pad);
646 This->clock_written += oldpad - This->pad;
647 TRACE("New pad: %zu (-%zu)\n", This->pad / pa_frame_size(&This->ss), (oldpad - This->pad) / pa_frame_size(&This->ss));
649 if (This->event)
650 SetEvent(This->event);
653 static void pulse_underflow_callback(pa_stream *s, void *userdata)
655 WARN("Underflow\n");
658 /* Latency is periodically updated even when nothing is played,
659 * because of PA_STREAM_AUTO_TIMING_UPDATE so use it as timer
661 * Perfect for passing all tests :)
663 static void pulse_latency_callback(pa_stream *s, void *userdata)
665 ACImpl *This = userdata;
666 if (!This->pad && This->event)
667 SetEvent(This->event);
670 static void pulse_started_callback(pa_stream *s, void *userdata)
672 TRACE("(Re)started playing\n");
675 static void pulse_rd_loop(ACImpl *This, size_t bytes)
677 while (bytes >= This->capture_period) {
678 ACPacket *p, *next;
679 LARGE_INTEGER stamp, freq;
680 BYTE *dst, *src;
681 size_t src_len, copy, rem = This->capture_period;
682 if (!(p = (ACPacket*)list_head(&This->packet_free_head))) {
683 p = (ACPacket*)list_head(&This->packet_filled_head);
684 if (!p->discont) {
685 next = (ACPacket*)p->entry.next;
686 next->discont = 1;
687 } else
688 p = (ACPacket*)list_tail(&This->packet_filled_head);
689 assert(This->pad == This->bufsize_bytes);
690 } else {
691 assert(This->pad < This->bufsize_bytes);
692 This->pad += This->capture_period;
693 assert(This->pad <= This->bufsize_bytes);
695 QueryPerformanceCounter(&stamp);
696 QueryPerformanceFrequency(&freq);
697 p->qpcpos = (stamp.QuadPart * (INT64)10000000) / freq.QuadPart;
698 p->discont = 0;
699 list_remove(&p->entry);
700 list_add_tail(&This->packet_filled_head, &p->entry);
702 dst = p->data;
703 while (rem) {
704 pa_stream_peek(This->stream, (const void**)&src, &src_len);
705 assert(src_len);
706 assert(This->peek_ofs < src_len);
707 src += This->peek_ofs;
708 src_len -= This->peek_ofs;
709 assert(src_len <= bytes);
711 copy = rem;
712 if (copy > src_len)
713 copy = src_len;
714 memcpy(dst, src, rem);
715 src += copy;
716 src_len -= copy;
717 dst += copy;
718 rem -= copy;
720 if (!src_len) {
721 This->peek_ofs = 0;
722 pa_stream_drop(This->stream);
723 } else
724 This->peek_ofs += copy;
726 bytes -= This->capture_period;
730 static void pulse_rd_drop(ACImpl *This, size_t bytes)
732 while (bytes >= This->capture_period) {
733 size_t src_len, copy, rem = This->capture_period;
734 while (rem) {
735 const void *src;
736 pa_stream_peek(This->stream, &src, &src_len);
737 assert(src_len);
738 assert(This->peek_ofs < src_len);
739 src_len -= This->peek_ofs;
740 assert(src_len <= bytes);
742 copy = rem;
743 if (copy > src_len)
744 copy = src_len;
746 src_len -= copy;
747 rem -= copy;
749 if (!src_len) {
750 This->peek_ofs = 0;
751 pa_stream_drop(This->stream);
752 } else
753 This->peek_ofs += copy;
754 bytes -= copy;
759 static void pulse_rd_callback(pa_stream *s, size_t bytes, void *userdata)
761 ACImpl *This = userdata;
763 TRACE("Readable total: %zu, fragsize: %u\n", bytes, pa_stream_get_buffer_attr(s)->fragsize);
764 assert(bytes >= This->peek_ofs);
765 bytes -= This->peek_ofs;
766 if (bytes < This->capture_period)
767 return;
769 if (This->started)
770 pulse_rd_loop(This, bytes);
771 else
772 pulse_rd_drop(This, bytes);
774 if (This->event)
775 SetEvent(This->event);
778 static HRESULT pulse_stream_connect(ACImpl *This, UINT32 period_bytes) {
779 int ret;
780 char buffer[64];
781 static LONG number;
782 pa_buffer_attr attr;
783 if (This->stream) {
784 pa_stream_disconnect(This->stream);
785 while (pa_stream_get_state(This->stream) == PA_STREAM_READY)
786 pthread_cond_wait(&pulse_cond, &pulse_lock);
787 pa_stream_unref(This->stream);
789 ret = InterlockedIncrement(&number);
790 sprintf(buffer, "audio stream #%i", ret);
791 This->stream = pa_stream_new(pulse_ctx, buffer, &This->ss, &This->map);
793 if (!This->stream) {
794 WARN("pa_stream_new returned error %i\n", pa_context_errno(pulse_ctx));
795 return AUDCLNT_E_ENDPOINT_CREATE_FAILED;
798 pa_stream_set_state_callback(This->stream, pulse_stream_state, This);
799 pa_stream_set_buffer_attr_callback(This->stream, pulse_attr_update, This);
800 pa_stream_set_moved_callback(This->stream, pulse_attr_update, This);
802 /* Pulseaudio will fill in correct values */
803 attr.minreq = attr.fragsize = period_bytes;
804 attr.maxlength = attr.tlength = This->bufsize_bytes;
805 attr.prebuf = pa_frame_size(&This->ss);
806 dump_attr(&attr);
807 if (This->dataflow == eRender)
808 ret = pa_stream_connect_playback(This->stream, NULL, &attr,
809 PA_STREAM_START_CORKED|PA_STREAM_START_UNMUTED|PA_STREAM_AUTO_TIMING_UPDATE|PA_STREAM_INTERPOLATE_TIMING|PA_STREAM_EARLY_REQUESTS, NULL, NULL);
810 else
811 ret = pa_stream_connect_record(This->stream, NULL, &attr,
812 PA_STREAM_START_CORKED|PA_STREAM_START_UNMUTED|PA_STREAM_AUTO_TIMING_UPDATE|PA_STREAM_INTERPOLATE_TIMING|PA_STREAM_EARLY_REQUESTS);
813 if (ret < 0) {
814 WARN("Returns %i\n", ret);
815 return AUDCLNT_E_ENDPOINT_CREATE_FAILED;
817 while (pa_stream_get_state(This->stream) == PA_STREAM_CREATING)
818 pthread_cond_wait(&pulse_cond, &pulse_lock);
819 if (pa_stream_get_state(This->stream) != PA_STREAM_READY)
820 return AUDCLNT_E_ENDPOINT_CREATE_FAILED;
822 if (This->dataflow == eRender) {
823 pa_stream_set_write_callback(This->stream, pulse_wr_callback, This);
824 pa_stream_set_underflow_callback(This->stream, pulse_underflow_callback, This);
825 pa_stream_set_started_callback(This->stream, pulse_started_callback, This);
826 } else
827 pa_stream_set_read_callback(This->stream, pulse_rd_callback, This);
828 return S_OK;
831 HRESULT WINAPI AUDDRV_GetEndpointIDs(EDataFlow flow, const WCHAR ***ids, GUID **keys,
832 UINT *num, UINT *def_index)
834 WCHAR *id;
836 TRACE("%d %p %p %p\n", flow, ids, num, def_index);
838 *num = 1;
839 *def_index = 0;
841 *ids = HeapAlloc(GetProcessHeap(), 0, sizeof(**ids));
842 *keys = NULL;
843 if (!*ids)
844 return E_OUTOFMEMORY;
846 (*ids)[0] = id = HeapAlloc(GetProcessHeap(), 0, sizeof(defaultW));
847 *keys = HeapAlloc(GetProcessHeap(), 0, sizeof(**keys));
848 if (!*keys || !id) {
849 HeapFree(GetProcessHeap(), 0, id);
850 HeapFree(GetProcessHeap(), 0, *keys);
851 HeapFree(GetProcessHeap(), 0, *ids);
852 *ids = NULL;
853 *keys = NULL;
854 return E_OUTOFMEMORY;
856 memcpy(id, defaultW, sizeof(defaultW));
858 if (flow == eRender)
859 (*keys)[0] = pulse_render_guid;
860 else
861 (*keys)[0] = pulse_capture_guid;
863 return S_OK;
866 int WINAPI AUDDRV_GetPriority(void)
868 HRESULT hr;
869 pthread_mutex_lock(&pulse_lock);
870 hr = pulse_test_connect();
871 pthread_mutex_unlock(&pulse_lock);
872 return SUCCEEDED(hr) ? Priority_Preferred : Priority_Unavailable;
875 HRESULT WINAPI AUDDRV_GetAudioEndpoint(GUID *guid, IMMDevice *dev, IAudioClient **out)
877 ACImpl *This;
878 int i;
879 EDataFlow dataflow;
880 HRESULT hr;
882 TRACE("%s %p %p\n", debugstr_guid(guid), dev, out);
883 if (IsEqualGUID(guid, &pulse_render_guid))
884 dataflow = eRender;
885 else if (IsEqualGUID(guid, &pulse_capture_guid))
886 dataflow = eCapture;
887 else
888 return E_UNEXPECTED;
890 *out = NULL;
892 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*This));
893 if (!This)
894 return E_OUTOFMEMORY;
896 This->IAudioClient_iface.lpVtbl = &AudioClient_Vtbl;
897 This->IAudioRenderClient_iface.lpVtbl = &AudioRenderClient_Vtbl;
898 This->IAudioCaptureClient_iface.lpVtbl = &AudioCaptureClient_Vtbl;
899 This->IAudioClock_iface.lpVtbl = &AudioClock_Vtbl;
900 This->IAudioClock2_iface.lpVtbl = &AudioClock2_Vtbl;
901 This->IAudioStreamVolume_iface.lpVtbl = &AudioStreamVolume_Vtbl;
902 This->dataflow = dataflow;
903 This->parent = dev;
904 for (i = 0; i < PA_CHANNELS_MAX; ++i)
905 This->vol[i] = 1.f;
907 hr = CoCreateFreeThreadedMarshaler((IUnknown*)This, &This->marshal);
908 if (hr) {
909 HeapFree(GetProcessHeap(), 0, This);
910 return hr;
912 IMMDevice_AddRef(This->parent);
914 *out = &This->IAudioClient_iface;
915 IAudioClient_AddRef(&This->IAudioClient_iface);
917 return S_OK;
920 static HRESULT WINAPI AudioClient_QueryInterface(IAudioClient *iface,
921 REFIID riid, void **ppv)
923 ACImpl *This = impl_from_IAudioClient(iface);
925 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
927 if (!ppv)
928 return E_POINTER;
930 *ppv = NULL;
931 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IAudioClient))
932 *ppv = iface;
933 if (*ppv) {
934 IUnknown_AddRef((IUnknown*)*ppv);
935 return S_OK;
938 if (IsEqualIID(riid, &IID_IMarshal))
939 return IUnknown_QueryInterface(This->marshal, riid, ppv);
941 WARN("Unknown interface %s\n", debugstr_guid(riid));
942 return E_NOINTERFACE;
945 static ULONG WINAPI AudioClient_AddRef(IAudioClient *iface)
947 ACImpl *This = impl_from_IAudioClient(iface);
948 ULONG ref;
949 ref = InterlockedIncrement(&This->ref);
950 TRACE("(%p) Refcount now %u\n", This, ref);
951 return ref;
954 static ULONG WINAPI AudioClient_Release(IAudioClient *iface)
956 ACImpl *This = impl_from_IAudioClient(iface);
957 ULONG ref;
958 ref = InterlockedDecrement(&This->ref);
959 TRACE("(%p) Refcount now %u\n", This, ref);
960 if (!ref) {
961 if (This->stream) {
962 pthread_mutex_lock(&pulse_lock);
963 if (PA_STREAM_IS_GOOD(pa_stream_get_state(This->stream))) {
964 pa_stream_disconnect(This->stream);
965 while (PA_STREAM_IS_GOOD(pa_stream_get_state(This->stream)))
966 pthread_cond_wait(&pulse_cond, &pulse_lock);
968 pa_stream_unref(This->stream);
969 This->stream = NULL;
970 list_remove(&This->entry);
971 pthread_mutex_unlock(&pulse_lock);
973 IUnknown_Release(This->marshal);
974 IMMDevice_Release(This->parent);
975 HeapFree(GetProcessHeap(), 0, This->tmp_buffer);
976 HeapFree(GetProcessHeap(), 0, This);
978 return ref;
981 static void dump_fmt(const WAVEFORMATEX *fmt)
983 TRACE("wFormatTag: 0x%x (", fmt->wFormatTag);
984 switch(fmt->wFormatTag) {
985 case WAVE_FORMAT_PCM:
986 TRACE("WAVE_FORMAT_PCM");
987 break;
988 case WAVE_FORMAT_IEEE_FLOAT:
989 TRACE("WAVE_FORMAT_IEEE_FLOAT");
990 break;
991 case WAVE_FORMAT_EXTENSIBLE:
992 TRACE("WAVE_FORMAT_EXTENSIBLE");
993 break;
994 default:
995 TRACE("Unknown");
996 break;
998 TRACE(")\n");
1000 TRACE("nChannels: %u\n", fmt->nChannels);
1001 TRACE("nSamplesPerSec: %u\n", fmt->nSamplesPerSec);
1002 TRACE("nAvgBytesPerSec: %u\n", fmt->nAvgBytesPerSec);
1003 TRACE("nBlockAlign: %u\n", fmt->nBlockAlign);
1004 TRACE("wBitsPerSample: %u\n", fmt->wBitsPerSample);
1005 TRACE("cbSize: %u\n", fmt->cbSize);
1007 if (fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE) {
1008 WAVEFORMATEXTENSIBLE *fmtex = (void*)fmt;
1009 TRACE("dwChannelMask: %08x\n", fmtex->dwChannelMask);
1010 TRACE("Samples: %04x\n", fmtex->Samples.wReserved);
1011 TRACE("SubFormat: %s\n", wine_dbgstr_guid(&fmtex->SubFormat));
1015 static WAVEFORMATEX *clone_format(const WAVEFORMATEX *fmt)
1017 WAVEFORMATEX *ret;
1018 size_t size;
1020 if (fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
1021 size = sizeof(WAVEFORMATEXTENSIBLE);
1022 else
1023 size = sizeof(WAVEFORMATEX);
1025 ret = CoTaskMemAlloc(size);
1026 if (!ret)
1027 return NULL;
1029 memcpy(ret, fmt, size);
1031 ret->cbSize = size - sizeof(WAVEFORMATEX);
1033 return ret;
1036 static DWORD get_channel_mask(unsigned int channels)
1038 switch(channels) {
1039 case 0:
1040 return 0;
1041 case 1:
1042 return KSAUDIO_SPEAKER_MONO;
1043 case 2:
1044 return KSAUDIO_SPEAKER_STEREO;
1045 case 3:
1046 return KSAUDIO_SPEAKER_STEREO | SPEAKER_LOW_FREQUENCY;
1047 case 4:
1048 return KSAUDIO_SPEAKER_QUAD; /* not _SURROUND */
1049 case 5:
1050 return KSAUDIO_SPEAKER_QUAD | SPEAKER_LOW_FREQUENCY;
1051 case 6:
1052 return KSAUDIO_SPEAKER_5POINT1; /* not 5POINT1_SURROUND */
1053 case 7:
1054 return KSAUDIO_SPEAKER_5POINT1 | SPEAKER_BACK_CENTER;
1055 case 8:
1056 return KSAUDIO_SPEAKER_7POINT1_SURROUND; /* Vista deprecates 7POINT1 */
1058 FIXME("Unknown speaker configuration: %u\n", channels);
1059 return 0;
1062 static void session_init_vols(AudioSession *session, UINT channels)
1064 if (session->channel_count < channels) {
1065 UINT i;
1067 if (session->channel_vols)
1068 session->channel_vols = HeapReAlloc(GetProcessHeap(), 0,
1069 session->channel_vols, sizeof(float) * channels);
1070 else
1071 session->channel_vols = HeapAlloc(GetProcessHeap(), 0,
1072 sizeof(float) * channels);
1073 if (!session->channel_vols)
1074 return;
1076 for(i = session->channel_count; i < channels; ++i)
1077 session->channel_vols[i] = 1.f;
1079 session->channel_count = channels;
1083 static AudioSession *create_session(const GUID *guid, IMMDevice *device,
1084 UINT num_channels)
1086 AudioSession *ret;
1088 ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(AudioSession));
1089 if (!ret)
1090 return NULL;
1092 memcpy(&ret->guid, guid, sizeof(GUID));
1094 ret->device = device;
1096 list_init(&ret->clients);
1098 list_add_head(&g_sessions, &ret->entry);
1100 session_init_vols(ret, num_channels);
1102 ret->master_vol = 1.f;
1104 return ret;
1107 /* if channels == 0, then this will return or create a session with
1108 * matching dataflow and GUID. otherwise, channels must also match */
1109 static HRESULT get_audio_session(const GUID *sessionguid,
1110 IMMDevice *device, UINT channels, AudioSession **out)
1112 AudioSession *session;
1114 if (!sessionguid || IsEqualGUID(sessionguid, &GUID_NULL)) {
1115 *out = create_session(&GUID_NULL, device, channels);
1116 if (!*out)
1117 return E_OUTOFMEMORY;
1119 return S_OK;
1122 *out = NULL;
1123 LIST_FOR_EACH_ENTRY(session, &g_sessions, AudioSession, entry) {
1124 if (session->device == device &&
1125 IsEqualGUID(sessionguid, &session->guid)) {
1126 session_init_vols(session, channels);
1127 *out = session;
1128 break;
1132 if (!*out) {
1133 *out = create_session(sessionguid, device, channels);
1134 if (!*out)
1135 return E_OUTOFMEMORY;
1138 return S_OK;
1141 static HRESULT pulse_spec_from_waveformat(ACImpl *This, const WAVEFORMATEX *fmt)
1143 pa_channel_map_init(&This->map);
1144 This->ss.rate = fmt->nSamplesPerSec;
1145 This->ss.format = PA_SAMPLE_INVALID;
1147 switch(fmt->wFormatTag) {
1148 case WAVE_FORMAT_IEEE_FLOAT:
1149 if (!fmt->nChannels || fmt->nChannels > 2 || fmt->wBitsPerSample != 32)
1150 break;
1151 This->ss.format = PA_SAMPLE_FLOAT32LE;
1152 pa_channel_map_init_auto(&This->map, fmt->nChannels, PA_CHANNEL_MAP_ALSA);
1153 break;
1154 case WAVE_FORMAT_PCM:
1155 if (!fmt->nChannels || fmt->nChannels > 2)
1156 break;
1157 if (fmt->wBitsPerSample == 8)
1158 This->ss.format = PA_SAMPLE_U8;
1159 else if (fmt->wBitsPerSample == 16)
1160 This->ss.format = PA_SAMPLE_S16LE;
1161 else
1162 return AUDCLNT_E_UNSUPPORTED_FORMAT;
1163 pa_channel_map_init_auto(&This->map, fmt->nChannels, PA_CHANNEL_MAP_ALSA);
1164 break;
1165 case WAVE_FORMAT_EXTENSIBLE: {
1166 WAVEFORMATEXTENSIBLE *wfe = (WAVEFORMATEXTENSIBLE*)fmt;
1167 DWORD mask = wfe->dwChannelMask;
1168 DWORD i = 0, j;
1169 if (fmt->cbSize != (sizeof(*wfe) - sizeof(*fmt)) && fmt->cbSize != sizeof(*wfe))
1170 break;
1171 if (IsEqualGUID(&wfe->SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT) &&
1172 (!wfe->Samples.wValidBitsPerSample || wfe->Samples.wValidBitsPerSample == 32) &&
1173 fmt->wBitsPerSample == 32)
1174 This->ss.format = PA_SAMPLE_FLOAT32LE;
1175 else if (IsEqualGUID(&wfe->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM)) {
1176 DWORD valid = wfe->Samples.wValidBitsPerSample;
1177 if (!valid)
1178 valid = fmt->wBitsPerSample;
1179 if (!valid || valid > fmt->wBitsPerSample)
1180 break;
1181 switch (fmt->wBitsPerSample) {
1182 case 8:
1183 if (valid == 8)
1184 This->ss.format = PA_SAMPLE_U8;
1185 break;
1186 case 16:
1187 if (valid == 16)
1188 This->ss.format = PA_SAMPLE_S16LE;
1189 break;
1190 case 24:
1191 if (valid == 24)
1192 This->ss.format = PA_SAMPLE_S24LE;
1193 break;
1194 case 32:
1195 if (valid == 24)
1196 This->ss.format = PA_SAMPLE_S24_32LE;
1197 else if (valid == 32)
1198 This->ss.format = PA_SAMPLE_S32LE;
1199 break;
1200 default:
1201 return AUDCLNT_E_UNSUPPORTED_FORMAT;
1204 This->map.channels = fmt->nChannels;
1205 if (!mask || (mask & (SPEAKER_ALL|SPEAKER_RESERVED)))
1206 mask = get_channel_mask(fmt->nChannels);
1207 for (j = 0; j < sizeof(pulse_pos_from_wfx)/sizeof(*pulse_pos_from_wfx) && i < fmt->nChannels; ++j) {
1208 if (mask & (1 << j))
1209 This->map.map[i++] = pulse_pos_from_wfx[j];
1212 /* Special case for mono since pulse appears to map it differently */
1213 if (mask == SPEAKER_FRONT_CENTER)
1214 This->map.map[0] = PA_CHANNEL_POSITION_MONO;
1216 if (i < fmt->nChannels || (mask & SPEAKER_RESERVED)) {
1217 This->map.channels = 0;
1218 ERR("Invalid channel mask: %i/%i and %x(%x)\n", i, fmt->nChannels, mask, wfe->dwChannelMask);
1219 break;
1221 break;
1223 case WAVE_FORMAT_ALAW:
1224 case WAVE_FORMAT_MULAW:
1225 if (fmt->wBitsPerSample != 8) {
1226 FIXME("Unsupported bpp %u for LAW\n", fmt->wBitsPerSample);
1227 return AUDCLNT_E_UNSUPPORTED_FORMAT;
1229 if (fmt->nChannels != 1 && fmt->nChannels != 2) {
1230 FIXME("Unsupported channels %u for LAW\n", fmt->nChannels);
1231 return AUDCLNT_E_UNSUPPORTED_FORMAT;
1233 This->ss.format = fmt->wFormatTag == WAVE_FORMAT_MULAW ? PA_SAMPLE_ULAW : PA_SAMPLE_ALAW;
1234 pa_channel_map_init_auto(&This->map, fmt->nChannels, PA_CHANNEL_MAP_ALSA);
1235 break;
1236 default:
1237 WARN("Unhandled tag %x\n", fmt->wFormatTag);
1238 return AUDCLNT_E_UNSUPPORTED_FORMAT;
1240 This->ss.channels = This->map.channels;
1241 if (!pa_channel_map_valid(&This->map) || This->ss.format == PA_SAMPLE_INVALID) {
1242 ERR("Invalid format! Channel spec valid: %i, format: %i\n", pa_channel_map_valid(&This->map), This->ss.format);
1243 return AUDCLNT_E_UNSUPPORTED_FORMAT;
1245 return S_OK;
1248 static HRESULT WINAPI AudioClient_Initialize(IAudioClient *iface,
1249 AUDCLNT_SHAREMODE mode, DWORD flags, REFERENCE_TIME duration,
1250 REFERENCE_TIME period, const WAVEFORMATEX *fmt,
1251 const GUID *sessionguid)
1253 ACImpl *This = impl_from_IAudioClient(iface);
1254 HRESULT hr = S_OK;
1255 UINT period_bytes;
1257 TRACE("(%p)->(%x, %x, %s, %s, %p, %s)\n", This, mode, flags,
1258 wine_dbgstr_longlong(duration), wine_dbgstr_longlong(period), fmt, debugstr_guid(sessionguid));
1260 if (!fmt)
1261 return E_POINTER;
1263 if (mode != AUDCLNT_SHAREMODE_SHARED && mode != AUDCLNT_SHAREMODE_EXCLUSIVE)
1264 return AUDCLNT_E_NOT_INITIALIZED;
1265 if (mode == AUDCLNT_SHAREMODE_EXCLUSIVE)
1266 return AUDCLNT_E_EXCLUSIVE_MODE_NOT_ALLOWED;
1268 if (flags & ~(AUDCLNT_STREAMFLAGS_CROSSPROCESS |
1269 AUDCLNT_STREAMFLAGS_LOOPBACK |
1270 AUDCLNT_STREAMFLAGS_EVENTCALLBACK |
1271 AUDCLNT_STREAMFLAGS_NOPERSIST |
1272 AUDCLNT_STREAMFLAGS_RATEADJUST |
1273 AUDCLNT_SESSIONFLAGS_EXPIREWHENUNOWNED |
1274 AUDCLNT_SESSIONFLAGS_DISPLAY_HIDE |
1275 AUDCLNT_SESSIONFLAGS_DISPLAY_HIDEWHENEXPIRED)) {
1276 TRACE("Unknown flags: %08x\n", flags);
1277 return E_INVALIDARG;
1280 pthread_mutex_lock(&pulse_lock);
1282 hr = pulse_connect();
1283 if (FAILED(hr)) {
1284 pthread_mutex_unlock(&pulse_lock);
1285 return hr;
1288 if (This->stream) {
1289 pthread_mutex_unlock(&pulse_lock);
1290 return AUDCLNT_E_ALREADY_INITIALIZED;
1293 hr = pulse_spec_from_waveformat(This, fmt);
1294 TRACE("Obtaining format returns %08x\n", hr);
1295 dump_fmt(fmt);
1297 if (FAILED(hr))
1298 goto exit;
1300 if (mode == AUDCLNT_SHAREMODE_SHARED) {
1301 REFERENCE_TIME def = pulse_def_period[This->dataflow == eCapture];
1302 REFERENCE_TIME min = pulse_min_period[This->dataflow == eCapture];
1304 /* Switch to low latency mode if below 2 default periods,
1305 * which is 20 ms by default, this will increase the amount
1306 * of interrupts but allows very low latency. In dsound I
1307 * managed to get a total latency of ~8ms, which is well below
1308 * default
1310 if (duration < 2 * def)
1311 period = min;
1312 else
1313 period = def;
1314 if (duration < 2 * period)
1315 duration = 2 * period;
1317 /* Uh oh, really low latency requested.. */
1318 if (duration <= 2 * period)
1319 period /= 2;
1321 period_bytes = pa_frame_size(&This->ss) * MulDiv(period, This->ss.rate, 10000000);
1323 if (duration < 20000000)
1324 This->bufsize_frames = ceil((duration / 10000000.) * fmt->nSamplesPerSec);
1325 else
1326 This->bufsize_frames = 2 * fmt->nSamplesPerSec;
1327 This->bufsize_bytes = This->bufsize_frames * pa_frame_size(&This->ss);
1329 This->share = mode;
1330 This->flags = flags;
1331 hr = pulse_stream_connect(This, period_bytes);
1332 if (SUCCEEDED(hr)) {
1333 UINT32 unalign;
1334 const pa_buffer_attr *attr = pa_stream_get_buffer_attr(This->stream);
1335 /* Update frames according to new size */
1336 dump_attr(attr);
1337 if (This->dataflow == eRender)
1338 This->bufsize_bytes = attr->tlength;
1339 else {
1340 This->capture_period = period_bytes = attr->fragsize;
1341 if ((unalign = This->bufsize_bytes % period_bytes))
1342 This->bufsize_bytes += period_bytes - unalign;
1344 This->bufsize_frames = This->bufsize_bytes / pa_frame_size(&This->ss);
1346 if (SUCCEEDED(hr)) {
1347 UINT32 i, capture_packets = This->capture_period ? This->bufsize_bytes / This->capture_period : 0;
1348 This->tmp_buffer = HeapAlloc(GetProcessHeap(), 0, This->bufsize_bytes + capture_packets * sizeof(ACPacket));
1349 if (!This->tmp_buffer)
1350 hr = E_OUTOFMEMORY;
1351 else {
1352 ACPacket *cur_packet = (ACPacket*)((char*)This->tmp_buffer + This->bufsize_bytes);
1353 BYTE *data = This->tmp_buffer;
1354 silence_buffer(This->ss.format, This->tmp_buffer, This->bufsize_bytes);
1355 list_init(&This->packet_free_head);
1356 list_init(&This->packet_filled_head);
1357 for (i = 0; i < capture_packets; ++i, ++cur_packet) {
1358 list_add_tail(&This->packet_free_head, &cur_packet->entry);
1359 cur_packet->data = data;
1360 data += This->capture_period;
1362 assert(!This->capture_period || This->bufsize_bytes == This->capture_period * capture_packets);
1363 assert(!capture_packets || data - This->bufsize_bytes == This->tmp_buffer);
1366 if (SUCCEEDED(hr))
1367 hr = get_audio_session(sessionguid, This->parent, fmt->nChannels, &This->session);
1368 if (SUCCEEDED(hr))
1369 list_add_tail(&This->session->clients, &This->entry);
1371 exit:
1372 if (FAILED(hr)) {
1373 HeapFree(GetProcessHeap(), 0, This->tmp_buffer);
1374 This->tmp_buffer = NULL;
1375 if (This->stream) {
1376 pa_stream_disconnect(This->stream);
1377 pa_stream_unref(This->stream);
1378 This->stream = NULL;
1381 pthread_mutex_unlock(&pulse_lock);
1382 return hr;
1385 static HRESULT WINAPI AudioClient_GetBufferSize(IAudioClient *iface,
1386 UINT32 *out)
1388 ACImpl *This = impl_from_IAudioClient(iface);
1389 HRESULT hr;
1391 TRACE("(%p)->(%p)\n", This, out);
1393 if (!out)
1394 return E_POINTER;
1396 pthread_mutex_lock(&pulse_lock);
1397 hr = pulse_stream_valid(This);
1398 if (SUCCEEDED(hr))
1399 *out = This->bufsize_frames;
1400 pthread_mutex_unlock(&pulse_lock);
1402 return hr;
1405 static HRESULT WINAPI AudioClient_GetStreamLatency(IAudioClient *iface,
1406 REFERENCE_TIME *latency)
1408 ACImpl *This = impl_from_IAudioClient(iface);
1409 const pa_buffer_attr *attr;
1410 REFERENCE_TIME lat;
1411 HRESULT hr;
1413 TRACE("(%p)->(%p)\n", This, latency);
1415 if (!latency)
1416 return E_POINTER;
1418 pthread_mutex_lock(&pulse_lock);
1419 hr = pulse_stream_valid(This);
1420 if (FAILED(hr)) {
1421 pthread_mutex_unlock(&pulse_lock);
1422 return hr;
1424 attr = pa_stream_get_buffer_attr(This->stream);
1425 if (This->dataflow == eRender)
1426 lat = attr->minreq / pa_frame_size(&This->ss);
1427 else
1428 lat = attr->fragsize / pa_frame_size(&This->ss);
1429 *latency = 10000000;
1430 *latency *= lat;
1431 *latency /= This->ss.rate;
1432 pthread_mutex_unlock(&pulse_lock);
1433 TRACE("Latency: %u ms\n", (DWORD)(*latency / 10000));
1434 return S_OK;
1437 static void ACImpl_GetRenderPad(ACImpl *This, UINT32 *out)
1439 *out = This->pad / pa_frame_size(&This->ss);
1442 static void ACImpl_GetCapturePad(ACImpl *This, UINT32 *out)
1444 ACPacket *packet = This->locked_ptr;
1445 if (!packet && !list_empty(&This->packet_filled_head)) {
1446 packet = (ACPacket*)list_head(&This->packet_filled_head);
1447 This->locked_ptr = packet;
1448 list_remove(&packet->entry);
1450 if (out)
1451 *out = This->pad / pa_frame_size(&This->ss);
1454 static HRESULT WINAPI AudioClient_GetCurrentPadding(IAudioClient *iface,
1455 UINT32 *out)
1457 ACImpl *This = impl_from_IAudioClient(iface);
1458 HRESULT hr;
1460 TRACE("(%p)->(%p)\n", This, out);
1462 if (!out)
1463 return E_POINTER;
1465 pthread_mutex_lock(&pulse_lock);
1466 hr = pulse_stream_valid(This);
1467 if (FAILED(hr)) {
1468 pthread_mutex_unlock(&pulse_lock);
1469 return hr;
1472 if (This->dataflow == eRender)
1473 ACImpl_GetRenderPad(This, out);
1474 else
1475 ACImpl_GetCapturePad(This, out);
1476 pthread_mutex_unlock(&pulse_lock);
1478 TRACE("%p Pad: %u ms (%u)\n", This, MulDiv(*out, 1000, This->ss.rate), *out);
1479 return S_OK;
1482 static HRESULT WINAPI AudioClient_IsFormatSupported(IAudioClient *iface,
1483 AUDCLNT_SHAREMODE mode, const WAVEFORMATEX *fmt,
1484 WAVEFORMATEX **out)
1486 ACImpl *This = impl_from_IAudioClient(iface);
1487 HRESULT hr = S_OK;
1488 WAVEFORMATEX *closest = NULL;
1489 BOOL exclusive;
1491 TRACE("(%p)->(%x, %p, %p)\n", This, mode, fmt, out);
1493 if (!fmt)
1494 return E_POINTER;
1496 if (out)
1497 *out = NULL;
1499 if (mode == AUDCLNT_SHAREMODE_EXCLUSIVE) {
1500 exclusive = 1;
1501 out = NULL;
1502 } else if (mode == AUDCLNT_SHAREMODE_SHARED) {
1503 exclusive = 0;
1504 if (!out)
1505 return E_POINTER;
1506 } else
1507 return E_INVALIDARG;
1509 if (fmt->nChannels == 0)
1510 return AUDCLNT_E_UNSUPPORTED_FORMAT;
1512 closest = clone_format(fmt);
1513 if (!closest)
1514 return E_OUTOFMEMORY;
1516 dump_fmt(fmt);
1518 switch (fmt->wFormatTag) {
1519 case WAVE_FORMAT_EXTENSIBLE: {
1520 WAVEFORMATEXTENSIBLE *ext = (WAVEFORMATEXTENSIBLE*)closest;
1522 if ((fmt->cbSize != sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX) &&
1523 fmt->cbSize != sizeof(WAVEFORMATEXTENSIBLE)) ||
1524 fmt->nBlockAlign != fmt->wBitsPerSample / 8 * fmt->nChannels ||
1525 ext->Samples.wValidBitsPerSample > fmt->wBitsPerSample ||
1526 fmt->nAvgBytesPerSec != fmt->nBlockAlign * fmt->nSamplesPerSec) {
1527 hr = E_INVALIDARG;
1528 break;
1531 if (exclusive) {
1532 UINT32 mask = 0, i, channels = 0;
1534 if (!(ext->dwChannelMask & (SPEAKER_ALL | SPEAKER_RESERVED))) {
1535 for (i = 1; !(i & SPEAKER_RESERVED); i <<= 1) {
1536 if (i & ext->dwChannelMask) {
1537 mask |= i;
1538 channels++;
1542 if (channels != fmt->nChannels || (ext->dwChannelMask & ~mask)) {
1543 hr = AUDCLNT_E_UNSUPPORTED_FORMAT;
1544 break;
1546 } else {
1547 hr = AUDCLNT_E_UNSUPPORTED_FORMAT;
1548 break;
1552 if (IsEqualGUID(&ext->SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT)) {
1553 if (fmt->wBitsPerSample != 32) {
1554 hr = E_INVALIDARG;
1555 break;
1558 if (ext->Samples.wValidBitsPerSample != fmt->wBitsPerSample) {
1559 hr = S_FALSE;
1560 ext->Samples.wValidBitsPerSample = fmt->wBitsPerSample;
1562 } else if (IsEqualGUID(&ext->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM)) {
1563 if (!fmt->wBitsPerSample || fmt->wBitsPerSample > 32 || fmt->wBitsPerSample % 8) {
1564 hr = E_INVALIDARG;
1565 break;
1568 if (ext->Samples.wValidBitsPerSample != fmt->wBitsPerSample &&
1569 !(fmt->wBitsPerSample == 32 &&
1570 ext->Samples.wValidBitsPerSample == 24)) {
1571 hr = S_FALSE;
1572 ext->Samples.wValidBitsPerSample = fmt->wBitsPerSample;
1573 break;
1575 } else {
1576 hr = AUDCLNT_E_UNSUPPORTED_FORMAT;
1577 break;
1580 break;
1583 case WAVE_FORMAT_ALAW:
1584 case WAVE_FORMAT_MULAW:
1585 if (fmt->wBitsPerSample != 8) {
1586 hr = E_INVALIDARG;
1587 break;
1589 /* Fall-through */
1590 case WAVE_FORMAT_IEEE_FLOAT:
1591 if (fmt->wFormatTag == WAVE_FORMAT_IEEE_FLOAT && fmt->wBitsPerSample != 32) {
1592 hr = E_INVALIDARG;
1593 break;
1595 /* Fall-through */
1596 case WAVE_FORMAT_PCM:
1597 if (fmt->wFormatTag == WAVE_FORMAT_PCM &&
1598 (!fmt->wBitsPerSample || fmt->wBitsPerSample > 32 || fmt->wBitsPerSample % 8)) {
1599 hr = E_INVALIDARG;
1600 break;
1603 if (fmt->nChannels > 2) {
1604 hr = AUDCLNT_E_UNSUPPORTED_FORMAT;
1605 break;
1608 * fmt->cbSize, fmt->nBlockAlign and fmt->nAvgBytesPerSec seem to be
1609 * ignored, invalid values are happily accepted.
1611 break;
1612 default:
1613 hr = AUDCLNT_E_UNSUPPORTED_FORMAT;
1614 break;
1617 if (exclusive && hr != S_OK) {
1618 hr = AUDCLNT_E_UNSUPPORTED_FORMAT;
1619 CoTaskMemFree(closest);
1620 } else if (hr != S_FALSE)
1621 CoTaskMemFree(closest);
1622 else
1623 *out = closest;
1625 /* Winepulse does not currently support exclusive mode, if you know of an
1626 * application that uses it, I will correct this..
1628 if (hr == S_OK && exclusive)
1629 return This->dataflow == eCapture ? AUDCLNT_E_UNSUPPORTED_FORMAT : AUDCLNT_E_EXCLUSIVE_MODE_NOT_ALLOWED;
1631 TRACE("returning: %08x %p\n", hr, out ? *out : NULL);
1632 return hr;
1635 static HRESULT WINAPI AudioClient_GetMixFormat(IAudioClient *iface,
1636 WAVEFORMATEX **pwfx)
1638 ACImpl *This = impl_from_IAudioClient(iface);
1639 WAVEFORMATEXTENSIBLE *fmt = &pulse_fmt[This->dataflow == eCapture];
1641 TRACE("(%p)->(%p)\n", This, pwfx);
1643 if (!pwfx)
1644 return E_POINTER;
1646 *pwfx = clone_format(&fmt->Format);
1647 if (!*pwfx)
1648 return E_OUTOFMEMORY;
1649 dump_fmt(*pwfx);
1650 return S_OK;
1653 static HRESULT WINAPI AudioClient_GetDevicePeriod(IAudioClient *iface,
1654 REFERENCE_TIME *defperiod, REFERENCE_TIME *minperiod)
1656 ACImpl *This = impl_from_IAudioClient(iface);
1658 TRACE("(%p)->(%p, %p)\n", This, defperiod, minperiod);
1660 if (!defperiod && !minperiod)
1661 return E_POINTER;
1663 if (defperiod)
1664 *defperiod = pulse_def_period[This->dataflow == eCapture];
1665 if (minperiod)
1666 *minperiod = pulse_min_period[This->dataflow == eCapture];
1668 return S_OK;
1671 static HRESULT WINAPI AudioClient_Start(IAudioClient *iface)
1673 ACImpl *This = impl_from_IAudioClient(iface);
1674 HRESULT hr = S_OK;
1675 int success;
1676 pa_operation *o;
1678 TRACE("(%p)\n", This);
1680 pthread_mutex_lock(&pulse_lock);
1681 hr = pulse_stream_valid(This);
1682 if (FAILED(hr)) {
1683 pthread_mutex_unlock(&pulse_lock);
1684 return hr;
1687 if ((This->flags & AUDCLNT_STREAMFLAGS_EVENTCALLBACK) && !This->event) {
1688 pthread_mutex_unlock(&pulse_lock);
1689 return AUDCLNT_E_EVENTHANDLE_NOT_SET;
1692 if (This->started) {
1693 pthread_mutex_unlock(&pulse_lock);
1694 return AUDCLNT_E_NOT_STOPPED;
1697 if (pa_stream_is_corked(This->stream)) {
1698 o = pa_stream_cork(This->stream, 0, pulse_op_cb, &success);
1699 if (o) {
1700 while(pa_operation_get_state(o) == PA_OPERATION_RUNNING)
1701 pthread_cond_wait(&pulse_cond, &pulse_lock);
1702 pa_operation_unref(o);
1703 } else
1704 success = 0;
1705 if (!success)
1706 hr = E_FAIL;
1708 if (SUCCEEDED(hr)) {
1709 This->started = TRUE;
1710 if (This->dataflow == eRender && This->event)
1711 pa_stream_set_latency_update_callback(This->stream, pulse_latency_callback, This);
1713 pthread_mutex_unlock(&pulse_lock);
1714 return hr;
1717 static HRESULT WINAPI AudioClient_Stop(IAudioClient *iface)
1719 ACImpl *This = impl_from_IAudioClient(iface);
1720 HRESULT hr = S_OK;
1721 pa_operation *o;
1722 int success;
1724 TRACE("(%p)\n", This);
1726 pthread_mutex_lock(&pulse_lock);
1727 hr = pulse_stream_valid(This);
1728 if (FAILED(hr)) {
1729 pthread_mutex_unlock(&pulse_lock);
1730 return hr;
1733 if (!This->started) {
1734 pthread_mutex_unlock(&pulse_lock);
1735 return S_FALSE;
1738 if (This->dataflow == eRender) {
1739 o = pa_stream_cork(This->stream, 1, pulse_op_cb, &success);
1740 if (o) {
1741 while(pa_operation_get_state(o) == PA_OPERATION_RUNNING)
1742 pthread_cond_wait(&pulse_cond, &pulse_lock);
1743 pa_operation_unref(o);
1744 } else
1745 success = 0;
1746 if (!success)
1747 hr = E_FAIL;
1749 if (SUCCEEDED(hr)) {
1750 This->started = FALSE;
1752 pthread_mutex_unlock(&pulse_lock);
1753 return hr;
1756 static HRESULT WINAPI AudioClient_Reset(IAudioClient *iface)
1758 ACImpl *This = impl_from_IAudioClient(iface);
1759 HRESULT hr = S_OK;
1761 TRACE("(%p)\n", This);
1763 pthread_mutex_lock(&pulse_lock);
1764 hr = pulse_stream_valid(This);
1765 if (FAILED(hr)) {
1766 pthread_mutex_unlock(&pulse_lock);
1767 return hr;
1770 if (This->started) {
1771 pthread_mutex_unlock(&pulse_lock);
1772 return AUDCLNT_E_NOT_STOPPED;
1775 if (This->locked) {
1776 pthread_mutex_unlock(&pulse_lock);
1777 return AUDCLNT_E_BUFFER_OPERATION_PENDING;
1780 if (This->dataflow == eRender) {
1781 /* If there is still data in the render buffer it needs to be removed from the server */
1782 int success = 0;
1783 if (This->pad) {
1784 pa_operation *o = pa_stream_flush(This->stream, pulse_op_cb, &success);
1785 if (o) {
1786 while(pa_operation_get_state(o) == PA_OPERATION_RUNNING)
1787 pthread_cond_wait(&pulse_cond, &pulse_lock);
1788 pa_operation_unref(o);
1791 if (success || !This->pad)
1792 This->clock_lastpos = This->clock_written = This->pad = 0;
1793 } else {
1794 ACPacket *p;
1795 This->clock_written += This->pad;
1796 This->pad = 0;
1798 if ((p = This->locked_ptr)) {
1799 This->locked_ptr = NULL;
1800 list_add_tail(&This->packet_free_head, &p->entry);
1802 list_move_tail(&This->packet_free_head, &This->packet_filled_head);
1804 pthread_mutex_unlock(&pulse_lock);
1806 return hr;
1809 static HRESULT WINAPI AudioClient_SetEventHandle(IAudioClient *iface,
1810 HANDLE event)
1812 ACImpl *This = impl_from_IAudioClient(iface);
1813 HRESULT hr;
1815 TRACE("(%p)->(%p)\n", This, event);
1817 if (!event)
1818 return E_INVALIDARG;
1820 pthread_mutex_lock(&pulse_lock);
1821 hr = pulse_stream_valid(This);
1822 if (FAILED(hr)) {
1823 pthread_mutex_unlock(&pulse_lock);
1824 return hr;
1827 if (!(This->flags & AUDCLNT_STREAMFLAGS_EVENTCALLBACK))
1828 hr = AUDCLNT_E_EVENTHANDLE_NOT_EXPECTED;
1829 else if (This->event)
1830 hr = HRESULT_FROM_WIN32(ERROR_INVALID_NAME);
1831 else
1832 This->event = event;
1833 pthread_mutex_unlock(&pulse_lock);
1834 return hr;
1837 static HRESULT WINAPI AudioClient_GetService(IAudioClient *iface, REFIID riid,
1838 void **ppv)
1840 ACImpl *This = impl_from_IAudioClient(iface);
1841 HRESULT hr;
1843 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
1845 if (!ppv)
1846 return E_POINTER;
1847 *ppv = NULL;
1849 pthread_mutex_lock(&pulse_lock);
1850 hr = pulse_stream_valid(This);
1851 pthread_mutex_unlock(&pulse_lock);
1852 if (FAILED(hr))
1853 return hr;
1855 if (IsEqualIID(riid, &IID_IAudioRenderClient)) {
1856 if (This->dataflow != eRender)
1857 return AUDCLNT_E_WRONG_ENDPOINT_TYPE;
1858 *ppv = &This->IAudioRenderClient_iface;
1859 } else if (IsEqualIID(riid, &IID_IAudioCaptureClient)) {
1860 if (This->dataflow != eCapture)
1861 return AUDCLNT_E_WRONG_ENDPOINT_TYPE;
1862 *ppv = &This->IAudioCaptureClient_iface;
1863 } else if (IsEqualIID(riid, &IID_IAudioClock)) {
1864 *ppv = &This->IAudioClock_iface;
1865 } else if (IsEqualIID(riid, &IID_IAudioStreamVolume)) {
1866 *ppv = &This->IAudioStreamVolume_iface;
1867 } else if (IsEqualIID(riid, &IID_IAudioSessionControl) ||
1868 IsEqualIID(riid, &IID_IChannelAudioVolume) ||
1869 IsEqualIID(riid, &IID_ISimpleAudioVolume)) {
1870 if (!This->session_wrapper) {
1871 This->session_wrapper = AudioSessionWrapper_Create(This);
1872 if (!This->session_wrapper)
1873 return E_OUTOFMEMORY;
1875 if (IsEqualIID(riid, &IID_IAudioSessionControl))
1876 *ppv = &This->session_wrapper->IAudioSessionControl2_iface;
1877 else if (IsEqualIID(riid, &IID_IChannelAudioVolume))
1878 *ppv = &This->session_wrapper->IChannelAudioVolume_iface;
1879 else if (IsEqualIID(riid, &IID_ISimpleAudioVolume))
1880 *ppv = &This->session_wrapper->ISimpleAudioVolume_iface;
1883 if (*ppv) {
1884 IUnknown_AddRef((IUnknown*)*ppv);
1885 return S_OK;
1888 FIXME("stub %s\n", debugstr_guid(riid));
1889 return E_NOINTERFACE;
1892 static const IAudioClientVtbl AudioClient_Vtbl =
1894 AudioClient_QueryInterface,
1895 AudioClient_AddRef,
1896 AudioClient_Release,
1897 AudioClient_Initialize,
1898 AudioClient_GetBufferSize,
1899 AudioClient_GetStreamLatency,
1900 AudioClient_GetCurrentPadding,
1901 AudioClient_IsFormatSupported,
1902 AudioClient_GetMixFormat,
1903 AudioClient_GetDevicePeriod,
1904 AudioClient_Start,
1905 AudioClient_Stop,
1906 AudioClient_Reset,
1907 AudioClient_SetEventHandle,
1908 AudioClient_GetService
1911 static HRESULT WINAPI AudioRenderClient_QueryInterface(
1912 IAudioRenderClient *iface, REFIID riid, void **ppv)
1914 ACImpl *This = impl_from_IAudioRenderClient(iface);
1915 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
1917 if (!ppv)
1918 return E_POINTER;
1919 *ppv = NULL;
1921 if (IsEqualIID(riid, &IID_IUnknown) ||
1922 IsEqualIID(riid, &IID_IAudioRenderClient))
1923 *ppv = iface;
1924 if (*ppv) {
1925 IUnknown_AddRef((IUnknown*)*ppv);
1926 return S_OK;
1929 if (IsEqualIID(riid, &IID_IMarshal))
1930 return IUnknown_QueryInterface(This->marshal, riid, ppv);
1932 WARN("Unknown interface %s\n", debugstr_guid(riid));
1933 return E_NOINTERFACE;
1936 static ULONG WINAPI AudioRenderClient_AddRef(IAudioRenderClient *iface)
1938 ACImpl *This = impl_from_IAudioRenderClient(iface);
1939 return AudioClient_AddRef(&This->IAudioClient_iface);
1942 static ULONG WINAPI AudioRenderClient_Release(IAudioRenderClient *iface)
1944 ACImpl *This = impl_from_IAudioRenderClient(iface);
1945 return AudioClient_Release(&This->IAudioClient_iface);
1948 static HRESULT WINAPI AudioRenderClient_GetBuffer(IAudioRenderClient *iface,
1949 UINT32 frames, BYTE **data)
1951 ACImpl *This = impl_from_IAudioRenderClient(iface);
1952 size_t avail, req, bytes = frames * pa_frame_size(&This->ss);
1953 UINT32 pad;
1954 HRESULT hr = S_OK;
1955 int ret = -1;
1957 TRACE("(%p)->(%u, %p)\n", This, frames, data);
1959 if (!data)
1960 return E_POINTER;
1961 *data = NULL;
1963 pthread_mutex_lock(&pulse_lock);
1964 hr = pulse_stream_valid(This);
1965 if (FAILED(hr) || This->locked) {
1966 pthread_mutex_unlock(&pulse_lock);
1967 return FAILED(hr) ? hr : AUDCLNT_E_OUT_OF_ORDER;
1969 if (!frames) {
1970 pthread_mutex_unlock(&pulse_lock);
1971 return S_OK;
1974 ACImpl_GetRenderPad(This, &pad);
1975 avail = This->bufsize_frames - pad;
1976 if (avail < frames || bytes > This->bufsize_bytes) {
1977 pthread_mutex_unlock(&pulse_lock);
1978 WARN("Wanted to write %u, but only %zu available\n", frames, avail);
1979 return AUDCLNT_E_BUFFER_TOO_LARGE;
1982 This->locked = frames;
1983 req = bytes;
1984 ret = pa_stream_begin_write(This->stream, &This->locked_ptr, &req);
1985 if (ret < 0 || req < bytes) {
1986 FIXME("%p Not using pulse locked data: %i %zu/%u %u/%u\n", This, ret, req/pa_frame_size(&This->ss), frames, pad, This->bufsize_frames);
1987 if (ret >= 0)
1988 pa_stream_cancel_write(This->stream);
1989 *data = This->tmp_buffer;
1990 This->locked_ptr = NULL;
1991 } else
1992 *data = This->locked_ptr;
1993 pthread_mutex_unlock(&pulse_lock);
1994 return hr;
1997 static void pulse_free_noop(void *buf)
2001 static HRESULT WINAPI AudioRenderClient_ReleaseBuffer(
2002 IAudioRenderClient *iface, UINT32 written_frames, DWORD flags)
2004 ACImpl *This = impl_from_IAudioRenderClient(iface);
2005 UINT32 written_bytes = written_frames * pa_frame_size(&This->ss);
2007 TRACE("(%p)->(%u, %x)\n", This, written_frames, flags);
2009 pthread_mutex_lock(&pulse_lock);
2010 if (!This->locked || !written_frames) {
2011 if (This->locked_ptr)
2012 pa_stream_cancel_write(This->stream);
2013 This->locked = 0;
2014 This->locked_ptr = NULL;
2015 pthread_mutex_unlock(&pulse_lock);
2016 return written_frames ? AUDCLNT_E_OUT_OF_ORDER : S_OK;
2019 if (This->locked < written_frames) {
2020 pthread_mutex_unlock(&pulse_lock);
2021 return AUDCLNT_E_INVALID_SIZE;
2024 This->locked = 0;
2025 if (This->locked_ptr) {
2026 if (flags & AUDCLNT_BUFFERFLAGS_SILENT)
2027 silence_buffer(This->ss.format, This->locked_ptr, written_bytes);
2028 pa_stream_write(This->stream, This->locked_ptr, written_bytes, NULL, 0, PA_SEEK_RELATIVE);
2029 } else {
2030 if (flags & AUDCLNT_BUFFERFLAGS_SILENT)
2031 silence_buffer(This->ss.format, This->tmp_buffer, written_bytes);
2032 pa_stream_write(This->stream, This->tmp_buffer, written_bytes, pulse_free_noop, 0, PA_SEEK_RELATIVE);
2035 This->pad += written_bytes;
2036 This->locked_ptr = NULL;
2037 TRACE("Released %u, pad %zu\n", written_frames, This->pad / pa_frame_size(&This->ss));
2038 assert(This->pad <= This->bufsize_bytes);
2040 pthread_mutex_unlock(&pulse_lock);
2041 return S_OK;
2044 static const IAudioRenderClientVtbl AudioRenderClient_Vtbl = {
2045 AudioRenderClient_QueryInterface,
2046 AudioRenderClient_AddRef,
2047 AudioRenderClient_Release,
2048 AudioRenderClient_GetBuffer,
2049 AudioRenderClient_ReleaseBuffer
2052 static HRESULT WINAPI AudioCaptureClient_QueryInterface(
2053 IAudioCaptureClient *iface, REFIID riid, void **ppv)
2055 ACImpl *This = impl_from_IAudioCaptureClient(iface);
2056 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2058 if (!ppv)
2059 return E_POINTER;
2060 *ppv = NULL;
2062 if (IsEqualIID(riid, &IID_IUnknown) ||
2063 IsEqualIID(riid, &IID_IAudioCaptureClient))
2064 *ppv = iface;
2065 if (*ppv) {
2066 IUnknown_AddRef((IUnknown*)*ppv);
2067 return S_OK;
2070 if (IsEqualIID(riid, &IID_IMarshal))
2071 return IUnknown_QueryInterface(This->marshal, riid, ppv);
2073 WARN("Unknown interface %s\n", debugstr_guid(riid));
2074 return E_NOINTERFACE;
2077 static ULONG WINAPI AudioCaptureClient_AddRef(IAudioCaptureClient *iface)
2079 ACImpl *This = impl_from_IAudioCaptureClient(iface);
2080 return IAudioClient_AddRef(&This->IAudioClient_iface);
2083 static ULONG WINAPI AudioCaptureClient_Release(IAudioCaptureClient *iface)
2085 ACImpl *This = impl_from_IAudioCaptureClient(iface);
2086 return IAudioClient_Release(&This->IAudioClient_iface);
2089 static HRESULT WINAPI AudioCaptureClient_GetBuffer(IAudioCaptureClient *iface,
2090 BYTE **data, UINT32 *frames, DWORD *flags, UINT64 *devpos,
2091 UINT64 *qpcpos)
2093 ACImpl *This = impl_from_IAudioCaptureClient(iface);
2094 HRESULT hr;
2095 ACPacket *packet;
2097 TRACE("(%p)->(%p, %p, %p, %p, %p)\n", This, data, frames, flags,
2098 devpos, qpcpos);
2100 if (!data || !frames || !flags)
2101 return E_POINTER;
2103 pthread_mutex_lock(&pulse_lock);
2104 hr = pulse_stream_valid(This);
2105 if (FAILED(hr) || This->locked) {
2106 pthread_mutex_unlock(&pulse_lock);
2107 return FAILED(hr) ? hr : AUDCLNT_E_OUT_OF_ORDER;
2110 ACImpl_GetCapturePad(This, NULL);
2111 if ((packet = This->locked_ptr)) {
2112 *frames = This->capture_period / pa_frame_size(&This->ss);
2113 *flags = 0;
2114 if (packet->discont)
2115 *flags |= AUDCLNT_BUFFERFLAGS_DATA_DISCONTINUITY;
2116 if (devpos) {
2117 if (packet->discont)
2118 *devpos = (This->clock_written + This->capture_period) / pa_frame_size(&This->ss);
2119 else
2120 *devpos = This->clock_written / pa_frame_size(&This->ss);
2122 if (qpcpos)
2123 *qpcpos = packet->qpcpos;
2124 *data = packet->data;
2126 else
2127 *frames = 0;
2128 This->locked = *frames;
2129 pthread_mutex_unlock(&pulse_lock);
2130 return *frames ? S_OK : AUDCLNT_S_BUFFER_EMPTY;
2133 static HRESULT WINAPI AudioCaptureClient_ReleaseBuffer(
2134 IAudioCaptureClient *iface, UINT32 done)
2136 ACImpl *This = impl_from_IAudioCaptureClient(iface);
2138 TRACE("(%p)->(%u)\n", This, done);
2140 pthread_mutex_lock(&pulse_lock);
2141 if (!This->locked && done) {
2142 pthread_mutex_unlock(&pulse_lock);
2143 return AUDCLNT_E_OUT_OF_ORDER;
2145 if (done && This->locked != done) {
2146 pthread_mutex_unlock(&pulse_lock);
2147 return AUDCLNT_E_INVALID_SIZE;
2149 if (done) {
2150 ACPacket *packet = This->locked_ptr;
2151 This->locked_ptr = NULL;
2152 This->pad -= This->capture_period;
2153 if (packet->discont)
2154 This->clock_written += 2 * This->capture_period;
2155 else
2156 This->clock_written += This->capture_period;
2157 list_add_tail(&This->packet_free_head, &packet->entry);
2159 This->locked = 0;
2160 pthread_mutex_unlock(&pulse_lock);
2161 return S_OK;
2164 static HRESULT WINAPI AudioCaptureClient_GetNextPacketSize(
2165 IAudioCaptureClient *iface, UINT32 *frames)
2167 ACImpl *This = impl_from_IAudioCaptureClient(iface);
2169 TRACE("(%p)->(%p)\n", This, frames);
2170 if (!frames)
2171 return E_POINTER;
2173 pthread_mutex_lock(&pulse_lock);
2174 ACImpl_GetCapturePad(This, NULL);
2175 if (This->locked_ptr)
2176 *frames = This->capture_period / pa_frame_size(&This->ss);
2177 else
2178 *frames = 0;
2179 pthread_mutex_unlock(&pulse_lock);
2180 return S_OK;
2183 static const IAudioCaptureClientVtbl AudioCaptureClient_Vtbl =
2185 AudioCaptureClient_QueryInterface,
2186 AudioCaptureClient_AddRef,
2187 AudioCaptureClient_Release,
2188 AudioCaptureClient_GetBuffer,
2189 AudioCaptureClient_ReleaseBuffer,
2190 AudioCaptureClient_GetNextPacketSize
2193 static HRESULT WINAPI AudioClock_QueryInterface(IAudioClock *iface,
2194 REFIID riid, void **ppv)
2196 ACImpl *This = impl_from_IAudioClock(iface);
2198 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2200 if (!ppv)
2201 return E_POINTER;
2202 *ppv = NULL;
2204 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IAudioClock))
2205 *ppv = iface;
2206 else if (IsEqualIID(riid, &IID_IAudioClock2))
2207 *ppv = &This->IAudioClock2_iface;
2208 if (*ppv) {
2209 IUnknown_AddRef((IUnknown*)*ppv);
2210 return S_OK;
2213 if (IsEqualIID(riid, &IID_IMarshal))
2214 return IUnknown_QueryInterface(This->marshal, riid, ppv);
2216 WARN("Unknown interface %s\n", debugstr_guid(riid));
2217 return E_NOINTERFACE;
2220 static ULONG WINAPI AudioClock_AddRef(IAudioClock *iface)
2222 ACImpl *This = impl_from_IAudioClock(iface);
2223 return IAudioClient_AddRef(&This->IAudioClient_iface);
2226 static ULONG WINAPI AudioClock_Release(IAudioClock *iface)
2228 ACImpl *This = impl_from_IAudioClock(iface);
2229 return IAudioClient_Release(&This->IAudioClient_iface);
2232 static HRESULT WINAPI AudioClock_GetFrequency(IAudioClock *iface, UINT64 *freq)
2234 ACImpl *This = impl_from_IAudioClock(iface);
2235 HRESULT hr;
2237 TRACE("(%p)->(%p)\n", This, freq);
2239 pthread_mutex_lock(&pulse_lock);
2240 hr = pulse_stream_valid(This);
2241 if (SUCCEEDED(hr)) {
2242 if (This->share == AUDCLNT_SHAREMODE_SHARED)
2243 *freq = This->ss.rate * pa_frame_size(&This->ss);
2244 else
2245 *freq = This->ss.rate;
2247 pthread_mutex_unlock(&pulse_lock);
2248 return hr;
2251 static HRESULT WINAPI AudioClock_GetPosition(IAudioClock *iface, UINT64 *pos,
2252 UINT64 *qpctime)
2254 ACImpl *This = impl_from_IAudioClock(iface);
2255 HRESULT hr;
2257 TRACE("(%p)->(%p, %p)\n", This, pos, qpctime);
2259 if (!pos)
2260 return E_POINTER;
2262 pthread_mutex_lock(&pulse_lock);
2263 hr = pulse_stream_valid(This);
2264 if (FAILED(hr)) {
2265 pthread_mutex_unlock(&pulse_lock);
2266 return hr;
2269 *pos = This->clock_written;
2271 if (This->share == AUDCLNT_SHAREMODE_EXCLUSIVE)
2272 *pos /= pa_frame_size(&This->ss);
2274 /* Make time never go backwards */
2275 if (*pos < This->clock_lastpos)
2276 *pos = This->clock_lastpos;
2277 else
2278 This->clock_lastpos = *pos;
2279 pthread_mutex_unlock(&pulse_lock);
2281 TRACE("%p Position: %u\n", This, (unsigned)*pos);
2283 if (qpctime) {
2284 LARGE_INTEGER stamp, freq;
2285 QueryPerformanceCounter(&stamp);
2286 QueryPerformanceFrequency(&freq);
2287 *qpctime = (stamp.QuadPart * (INT64)10000000) / freq.QuadPart;
2290 return S_OK;
2293 static HRESULT WINAPI AudioClock_GetCharacteristics(IAudioClock *iface,
2294 DWORD *chars)
2296 ACImpl *This = impl_from_IAudioClock(iface);
2298 TRACE("(%p)->(%p)\n", This, chars);
2300 if (!chars)
2301 return E_POINTER;
2303 *chars = AUDIOCLOCK_CHARACTERISTIC_FIXED_FREQ;
2305 return S_OK;
2308 static const IAudioClockVtbl AudioClock_Vtbl =
2310 AudioClock_QueryInterface,
2311 AudioClock_AddRef,
2312 AudioClock_Release,
2313 AudioClock_GetFrequency,
2314 AudioClock_GetPosition,
2315 AudioClock_GetCharacteristics
2318 static HRESULT WINAPI AudioClock2_QueryInterface(IAudioClock2 *iface,
2319 REFIID riid, void **ppv)
2321 ACImpl *This = impl_from_IAudioClock2(iface);
2322 return IAudioClock_QueryInterface(&This->IAudioClock_iface, riid, ppv);
2325 static ULONG WINAPI AudioClock2_AddRef(IAudioClock2 *iface)
2327 ACImpl *This = impl_from_IAudioClock2(iface);
2328 return IAudioClient_AddRef(&This->IAudioClient_iface);
2331 static ULONG WINAPI AudioClock2_Release(IAudioClock2 *iface)
2333 ACImpl *This = impl_from_IAudioClock2(iface);
2334 return IAudioClient_Release(&This->IAudioClient_iface);
2337 static HRESULT WINAPI AudioClock2_GetDevicePosition(IAudioClock2 *iface,
2338 UINT64 *pos, UINT64 *qpctime)
2340 ACImpl *This = impl_from_IAudioClock2(iface);
2341 HRESULT hr = AudioClock_GetPosition(&This->IAudioClock_iface, pos, qpctime);
2342 if (SUCCEEDED(hr) && This->share == AUDCLNT_SHAREMODE_SHARED)
2343 *pos /= pa_frame_size(&This->ss);
2344 return hr;
2347 static const IAudioClock2Vtbl AudioClock2_Vtbl =
2349 AudioClock2_QueryInterface,
2350 AudioClock2_AddRef,
2351 AudioClock2_Release,
2352 AudioClock2_GetDevicePosition
2355 static HRESULT WINAPI AudioStreamVolume_QueryInterface(
2356 IAudioStreamVolume *iface, REFIID riid, void **ppv)
2358 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2360 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2362 if (!ppv)
2363 return E_POINTER;
2364 *ppv = NULL;
2366 if (IsEqualIID(riid, &IID_IUnknown) ||
2367 IsEqualIID(riid, &IID_IAudioStreamVolume))
2368 *ppv = iface;
2369 if (*ppv) {
2370 IUnknown_AddRef((IUnknown*)*ppv);
2371 return S_OK;
2374 if (IsEqualIID(riid, &IID_IMarshal))
2375 return IUnknown_QueryInterface(This->marshal, riid, ppv);
2377 WARN("Unknown interface %s\n", debugstr_guid(riid));
2378 return E_NOINTERFACE;
2381 static ULONG WINAPI AudioStreamVolume_AddRef(IAudioStreamVolume *iface)
2383 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2384 return IAudioClient_AddRef(&This->IAudioClient_iface);
2387 static ULONG WINAPI AudioStreamVolume_Release(IAudioStreamVolume *iface)
2389 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2390 return IAudioClient_Release(&This->IAudioClient_iface);
2393 static HRESULT WINAPI AudioStreamVolume_GetChannelCount(
2394 IAudioStreamVolume *iface, UINT32 *out)
2396 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2398 TRACE("(%p)->(%p)\n", This, out);
2400 if (!out)
2401 return E_POINTER;
2403 *out = This->ss.channels;
2405 return S_OK;
2408 struct pulse_info_cb_data {
2409 UINT32 n;
2410 float *levels;
2413 static HRESULT WINAPI AudioStreamVolume_SetAllVolumes(
2414 IAudioStreamVolume *iface, UINT32 count, const float *levels)
2416 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2417 HRESULT hr;
2418 int i;
2420 TRACE("(%p)->(%d, %p)\n", This, count, levels);
2422 if (!levels)
2423 return E_POINTER;
2425 if (count != This->ss.channels)
2426 return E_INVALIDARG;
2428 pthread_mutex_lock(&pulse_lock);
2429 hr = pulse_stream_valid(This);
2430 if (FAILED(hr))
2431 goto out;
2433 for (i = 0; i < count; ++i)
2434 This->vol[i] = levels[i];
2436 out:
2437 pthread_mutex_unlock(&pulse_lock);
2438 return hr;
2441 static HRESULT WINAPI AudioStreamVolume_GetAllVolumes(
2442 IAudioStreamVolume *iface, UINT32 count, float *levels)
2444 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2445 HRESULT hr;
2446 int i;
2448 TRACE("(%p)->(%d, %p)\n", This, count, levels);
2450 if (!levels)
2451 return E_POINTER;
2453 if (count != This->ss.channels)
2454 return E_INVALIDARG;
2456 pthread_mutex_lock(&pulse_lock);
2457 hr = pulse_stream_valid(This);
2458 if (FAILED(hr))
2459 goto out;
2461 for (i = 0; i < count; ++i)
2462 levels[i] = This->vol[i];
2464 out:
2465 pthread_mutex_unlock(&pulse_lock);
2466 return hr;
2469 static HRESULT WINAPI AudioStreamVolume_SetChannelVolume(
2470 IAudioStreamVolume *iface, UINT32 index, float level)
2472 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2473 HRESULT hr;
2474 float volumes[PA_CHANNELS_MAX];
2476 TRACE("(%p)->(%d, %f)\n", This, index, level);
2478 if (level < 0.f || level > 1.f)
2479 return E_INVALIDARG;
2481 if (index >= This->ss.channels)
2482 return E_INVALIDARG;
2484 hr = AudioStreamVolume_GetAllVolumes(iface, This->ss.channels, volumes);
2485 volumes[index] = level;
2486 if (SUCCEEDED(hr))
2487 hr = AudioStreamVolume_SetAllVolumes(iface, This->ss.channels, volumes);
2488 return hr;
2491 static HRESULT WINAPI AudioStreamVolume_GetChannelVolume(
2492 IAudioStreamVolume *iface, UINT32 index, float *level)
2494 ACImpl *This = impl_from_IAudioStreamVolume(iface);
2495 float volumes[PA_CHANNELS_MAX];
2496 HRESULT hr;
2498 TRACE("(%p)->(%d, %p)\n", This, index, level);
2500 if (!level)
2501 return E_POINTER;
2503 if (index >= This->ss.channels)
2504 return E_INVALIDARG;
2506 hr = AudioStreamVolume_GetAllVolumes(iface, This->ss.channels, volumes);
2507 if (SUCCEEDED(hr))
2508 *level = volumes[index];
2509 return hr;
2512 static const IAudioStreamVolumeVtbl AudioStreamVolume_Vtbl =
2514 AudioStreamVolume_QueryInterface,
2515 AudioStreamVolume_AddRef,
2516 AudioStreamVolume_Release,
2517 AudioStreamVolume_GetChannelCount,
2518 AudioStreamVolume_SetChannelVolume,
2519 AudioStreamVolume_GetChannelVolume,
2520 AudioStreamVolume_SetAllVolumes,
2521 AudioStreamVolume_GetAllVolumes
2524 static AudioSessionWrapper *AudioSessionWrapper_Create(ACImpl *client)
2526 AudioSessionWrapper *ret;
2528 ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
2529 sizeof(AudioSessionWrapper));
2530 if (!ret)
2531 return NULL;
2533 ret->IAudioSessionControl2_iface.lpVtbl = &AudioSessionControl2_Vtbl;
2534 ret->ISimpleAudioVolume_iface.lpVtbl = &SimpleAudioVolume_Vtbl;
2535 ret->IChannelAudioVolume_iface.lpVtbl = &ChannelAudioVolume_Vtbl;
2537 ret->ref = !client;
2539 ret->client = client;
2540 if (client) {
2541 ret->session = client->session;
2542 AudioClient_AddRef(&client->IAudioClient_iface);
2545 return ret;
2548 static HRESULT WINAPI AudioSessionControl_QueryInterface(
2549 IAudioSessionControl2 *iface, REFIID riid, void **ppv)
2551 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2553 if (!ppv)
2554 return E_POINTER;
2555 *ppv = NULL;
2557 if (IsEqualIID(riid, &IID_IUnknown) ||
2558 IsEqualIID(riid, &IID_IAudioSessionControl) ||
2559 IsEqualIID(riid, &IID_IAudioSessionControl2))
2560 *ppv = iface;
2561 if (*ppv) {
2562 IUnknown_AddRef((IUnknown*)*ppv);
2563 return S_OK;
2566 WARN("Unknown interface %s\n", debugstr_guid(riid));
2567 return E_NOINTERFACE;
2570 static ULONG WINAPI AudioSessionControl_AddRef(IAudioSessionControl2 *iface)
2572 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2573 ULONG ref;
2574 ref = InterlockedIncrement(&This->ref);
2575 TRACE("(%p) Refcount now %u\n", This, ref);
2576 return ref;
2579 static ULONG WINAPI AudioSessionControl_Release(IAudioSessionControl2 *iface)
2581 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2582 ULONG ref;
2583 ref = InterlockedDecrement(&This->ref);
2584 TRACE("(%p) Refcount now %u\n", This, ref);
2585 if (!ref) {
2586 if (This->client) {
2587 This->client->session_wrapper = NULL;
2588 AudioClient_Release(&This->client->IAudioClient_iface);
2590 HeapFree(GetProcessHeap(), 0, This);
2592 return ref;
2595 static HRESULT WINAPI AudioSessionControl_GetState(IAudioSessionControl2 *iface,
2596 AudioSessionState *state)
2598 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2599 ACImpl *client;
2601 TRACE("(%p)->(%p)\n", This, state);
2603 if (!state)
2604 return NULL_PTR_ERR;
2606 pthread_mutex_lock(&pulse_lock);
2607 if (list_empty(&This->session->clients)) {
2608 *state = AudioSessionStateExpired;
2609 goto out;
2611 LIST_FOR_EACH_ENTRY(client, &This->session->clients, ACImpl, entry) {
2612 if (client->started) {
2613 *state = AudioSessionStateActive;
2614 goto out;
2617 *state = AudioSessionStateInactive;
2619 out:
2620 pthread_mutex_unlock(&pulse_lock);
2621 return S_OK;
2624 static HRESULT WINAPI AudioSessionControl_GetDisplayName(
2625 IAudioSessionControl2 *iface, WCHAR **name)
2627 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2629 FIXME("(%p)->(%p) - stub\n", This, name);
2631 return E_NOTIMPL;
2634 static HRESULT WINAPI AudioSessionControl_SetDisplayName(
2635 IAudioSessionControl2 *iface, const WCHAR *name, const GUID *session)
2637 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2639 FIXME("(%p)->(%p, %s) - stub\n", This, name, debugstr_guid(session));
2641 return E_NOTIMPL;
2644 static HRESULT WINAPI AudioSessionControl_GetIconPath(
2645 IAudioSessionControl2 *iface, WCHAR **path)
2647 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2649 FIXME("(%p)->(%p) - stub\n", This, path);
2651 return E_NOTIMPL;
2654 static HRESULT WINAPI AudioSessionControl_SetIconPath(
2655 IAudioSessionControl2 *iface, const WCHAR *path, const GUID *session)
2657 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2659 FIXME("(%p)->(%p, %s) - stub\n", This, path, debugstr_guid(session));
2661 return E_NOTIMPL;
2664 static HRESULT WINAPI AudioSessionControl_GetGroupingParam(
2665 IAudioSessionControl2 *iface, GUID *group)
2667 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2669 FIXME("(%p)->(%p) - stub\n", This, group);
2671 return E_NOTIMPL;
2674 static HRESULT WINAPI AudioSessionControl_SetGroupingParam(
2675 IAudioSessionControl2 *iface, const GUID *group, const GUID *session)
2677 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2679 FIXME("(%p)->(%s, %s) - stub\n", This, debugstr_guid(group),
2680 debugstr_guid(session));
2682 return E_NOTIMPL;
2685 static HRESULT WINAPI AudioSessionControl_RegisterAudioSessionNotification(
2686 IAudioSessionControl2 *iface, IAudioSessionEvents *events)
2688 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2690 FIXME("(%p)->(%p) - stub\n", This, events);
2692 return S_OK;
2695 static HRESULT WINAPI AudioSessionControl_UnregisterAudioSessionNotification(
2696 IAudioSessionControl2 *iface, IAudioSessionEvents *events)
2698 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2700 FIXME("(%p)->(%p) - stub\n", This, events);
2702 return S_OK;
2705 static HRESULT WINAPI AudioSessionControl_GetSessionIdentifier(
2706 IAudioSessionControl2 *iface, WCHAR **id)
2708 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2710 FIXME("(%p)->(%p) - stub\n", This, id);
2712 return E_NOTIMPL;
2715 static HRESULT WINAPI AudioSessionControl_GetSessionInstanceIdentifier(
2716 IAudioSessionControl2 *iface, WCHAR **id)
2718 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2720 FIXME("(%p)->(%p) - stub\n", This, id);
2722 return E_NOTIMPL;
2725 static HRESULT WINAPI AudioSessionControl_GetProcessId(
2726 IAudioSessionControl2 *iface, DWORD *pid)
2728 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2730 TRACE("(%p)->(%p)\n", This, pid);
2732 if (!pid)
2733 return E_POINTER;
2735 *pid = GetCurrentProcessId();
2737 return S_OK;
2740 static HRESULT WINAPI AudioSessionControl_IsSystemSoundsSession(
2741 IAudioSessionControl2 *iface)
2743 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2745 TRACE("(%p)\n", This);
2747 return S_FALSE;
2750 static HRESULT WINAPI AudioSessionControl_SetDuckingPreference(
2751 IAudioSessionControl2 *iface, BOOL optout)
2753 AudioSessionWrapper *This = impl_from_IAudioSessionControl2(iface);
2755 TRACE("(%p)->(%d)\n", This, optout);
2757 return S_OK;
2760 static const IAudioSessionControl2Vtbl AudioSessionControl2_Vtbl =
2762 AudioSessionControl_QueryInterface,
2763 AudioSessionControl_AddRef,
2764 AudioSessionControl_Release,
2765 AudioSessionControl_GetState,
2766 AudioSessionControl_GetDisplayName,
2767 AudioSessionControl_SetDisplayName,
2768 AudioSessionControl_GetIconPath,
2769 AudioSessionControl_SetIconPath,
2770 AudioSessionControl_GetGroupingParam,
2771 AudioSessionControl_SetGroupingParam,
2772 AudioSessionControl_RegisterAudioSessionNotification,
2773 AudioSessionControl_UnregisterAudioSessionNotification,
2774 AudioSessionControl_GetSessionIdentifier,
2775 AudioSessionControl_GetSessionInstanceIdentifier,
2776 AudioSessionControl_GetProcessId,
2777 AudioSessionControl_IsSystemSoundsSession,
2778 AudioSessionControl_SetDuckingPreference
2781 typedef struct _SessionMgr {
2782 IAudioSessionManager2 IAudioSessionManager2_iface;
2784 LONG ref;
2786 IMMDevice *device;
2787 } SessionMgr;
2789 static HRESULT WINAPI AudioSessionManager_QueryInterface(IAudioSessionManager2 *iface,
2790 REFIID riid, void **ppv)
2792 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2794 if (!ppv)
2795 return E_POINTER;
2796 *ppv = NULL;
2798 if (IsEqualIID(riid, &IID_IUnknown) ||
2799 IsEqualIID(riid, &IID_IAudioSessionManager) ||
2800 IsEqualIID(riid, &IID_IAudioSessionManager2))
2801 *ppv = iface;
2802 if (*ppv) {
2803 IUnknown_AddRef((IUnknown*)*ppv);
2804 return S_OK;
2807 WARN("Unknown interface %s\n", debugstr_guid(riid));
2808 return E_NOINTERFACE;
2811 static inline SessionMgr *impl_from_IAudioSessionManager2(IAudioSessionManager2 *iface)
2813 return CONTAINING_RECORD(iface, SessionMgr, IAudioSessionManager2_iface);
2816 static ULONG WINAPI AudioSessionManager_AddRef(IAudioSessionManager2 *iface)
2818 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
2819 ULONG ref;
2820 ref = InterlockedIncrement(&This->ref);
2821 TRACE("(%p) Refcount now %u\n", This, ref);
2822 return ref;
2825 static ULONG WINAPI AudioSessionManager_Release(IAudioSessionManager2 *iface)
2827 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
2828 ULONG ref;
2829 ref = InterlockedDecrement(&This->ref);
2830 TRACE("(%p) Refcount now %u\n", This, ref);
2831 if (!ref)
2832 HeapFree(GetProcessHeap(), 0, This);
2833 return ref;
2836 static HRESULT WINAPI AudioSessionManager_GetAudioSessionControl(
2837 IAudioSessionManager2 *iface, const GUID *session_guid, DWORD flags,
2838 IAudioSessionControl **out)
2840 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
2841 AudioSession *session;
2842 AudioSessionWrapper *wrapper;
2843 HRESULT hr;
2845 TRACE("(%p)->(%s, %x, %p)\n", This, debugstr_guid(session_guid),
2846 flags, out);
2848 hr = get_audio_session(session_guid, This->device, 0, &session);
2849 if (FAILED(hr))
2850 return hr;
2852 wrapper = AudioSessionWrapper_Create(NULL);
2853 if (!wrapper)
2854 return E_OUTOFMEMORY;
2856 wrapper->session = session;
2858 *out = (IAudioSessionControl*)&wrapper->IAudioSessionControl2_iface;
2860 return S_OK;
2863 static HRESULT WINAPI AudioSessionManager_GetSimpleAudioVolume(
2864 IAudioSessionManager2 *iface, const GUID *session_guid, DWORD flags,
2865 ISimpleAudioVolume **out)
2867 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
2868 AudioSession *session;
2869 AudioSessionWrapper *wrapper;
2870 HRESULT hr;
2872 TRACE("(%p)->(%s, %x, %p)\n", This, debugstr_guid(session_guid),
2873 flags, out);
2875 hr = get_audio_session(session_guid, This->device, 0, &session);
2876 if (FAILED(hr))
2877 return hr;
2879 wrapper = AudioSessionWrapper_Create(NULL);
2880 if (!wrapper)
2881 return E_OUTOFMEMORY;
2883 wrapper->session = session;
2885 *out = &wrapper->ISimpleAudioVolume_iface;
2887 return S_OK;
2890 static HRESULT WINAPI AudioSessionManager_GetSessionEnumerator(
2891 IAudioSessionManager2 *iface, IAudioSessionEnumerator **out)
2893 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
2894 FIXME("(%p)->(%p) - stub\n", This, out);
2895 return E_NOTIMPL;
2898 static HRESULT WINAPI AudioSessionManager_RegisterSessionNotification(
2899 IAudioSessionManager2 *iface, IAudioSessionNotification *notification)
2901 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
2902 FIXME("(%p)->(%p) - stub\n", This, notification);
2903 return E_NOTIMPL;
2906 static HRESULT WINAPI AudioSessionManager_UnregisterSessionNotification(
2907 IAudioSessionManager2 *iface, IAudioSessionNotification *notification)
2909 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
2910 FIXME("(%p)->(%p) - stub\n", This, notification);
2911 return E_NOTIMPL;
2914 static HRESULT WINAPI AudioSessionManager_RegisterDuckNotification(
2915 IAudioSessionManager2 *iface, const WCHAR *session_id,
2916 IAudioVolumeDuckNotification *notification)
2918 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
2919 FIXME("(%p)->(%p) - stub\n", This, notification);
2920 return E_NOTIMPL;
2923 static HRESULT WINAPI AudioSessionManager_UnregisterDuckNotification(
2924 IAudioSessionManager2 *iface,
2925 IAudioVolumeDuckNotification *notification)
2927 SessionMgr *This = impl_from_IAudioSessionManager2(iface);
2928 FIXME("(%p)->(%p) - stub\n", This, notification);
2929 return E_NOTIMPL;
2932 static const IAudioSessionManager2Vtbl AudioSessionManager2_Vtbl =
2934 AudioSessionManager_QueryInterface,
2935 AudioSessionManager_AddRef,
2936 AudioSessionManager_Release,
2937 AudioSessionManager_GetAudioSessionControl,
2938 AudioSessionManager_GetSimpleAudioVolume,
2939 AudioSessionManager_GetSessionEnumerator,
2940 AudioSessionManager_RegisterSessionNotification,
2941 AudioSessionManager_UnregisterSessionNotification,
2942 AudioSessionManager_RegisterDuckNotification,
2943 AudioSessionManager_UnregisterDuckNotification
2946 static HRESULT WINAPI SimpleAudioVolume_QueryInterface(
2947 ISimpleAudioVolume *iface, REFIID riid, void **ppv)
2949 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
2951 if (!ppv)
2952 return E_POINTER;
2953 *ppv = NULL;
2955 if (IsEqualIID(riid, &IID_IUnknown) ||
2956 IsEqualIID(riid, &IID_ISimpleAudioVolume))
2957 *ppv = iface;
2958 if (*ppv) {
2959 IUnknown_AddRef((IUnknown*)*ppv);
2960 return S_OK;
2963 WARN("Unknown interface %s\n", debugstr_guid(riid));
2964 return E_NOINTERFACE;
2967 static ULONG WINAPI SimpleAudioVolume_AddRef(ISimpleAudioVolume *iface)
2969 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2970 return AudioSessionControl_AddRef(&This->IAudioSessionControl2_iface);
2973 static ULONG WINAPI SimpleAudioVolume_Release(ISimpleAudioVolume *iface)
2975 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2976 return AudioSessionControl_Release(&This->IAudioSessionControl2_iface);
2979 static HRESULT WINAPI SimpleAudioVolume_SetMasterVolume(
2980 ISimpleAudioVolume *iface, float level, const GUID *context)
2982 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
2983 AudioSession *session = This->session;
2985 TRACE("(%p)->(%f, %s)\n", session, level, wine_dbgstr_guid(context));
2987 if (level < 0.f || level > 1.f)
2988 return E_INVALIDARG;
2990 if (context)
2991 FIXME("Notifications not supported yet\n");
2993 TRACE("Pulseaudio does not support session volume control\n");
2995 pthread_mutex_lock(&pulse_lock);
2996 session->master_vol = level;
2997 pthread_mutex_unlock(&pulse_lock);
2999 return S_OK;
3002 static HRESULT WINAPI SimpleAudioVolume_GetMasterVolume(
3003 ISimpleAudioVolume *iface, float *level)
3005 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
3006 AudioSession *session = This->session;
3008 TRACE("(%p)->(%p)\n", session, level);
3010 if (!level)
3011 return NULL_PTR_ERR;
3013 *level = session->master_vol;
3015 return S_OK;
3018 static HRESULT WINAPI SimpleAudioVolume_SetMute(ISimpleAudioVolume *iface,
3019 BOOL mute, const GUID *context)
3021 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
3022 AudioSession *session = This->session;
3024 TRACE("(%p)->(%u, %p)\n", session, mute, context);
3026 if (context)
3027 FIXME("Notifications not supported yet\n");
3029 session->mute = mute;
3031 return S_OK;
3034 static HRESULT WINAPI SimpleAudioVolume_GetMute(ISimpleAudioVolume *iface,
3035 BOOL *mute)
3037 AudioSessionWrapper *This = impl_from_ISimpleAudioVolume(iface);
3038 AudioSession *session = This->session;
3040 TRACE("(%p)->(%p)\n", session, mute);
3042 if (!mute)
3043 return NULL_PTR_ERR;
3045 *mute = session->mute;
3047 return S_OK;
3050 static const ISimpleAudioVolumeVtbl SimpleAudioVolume_Vtbl =
3052 SimpleAudioVolume_QueryInterface,
3053 SimpleAudioVolume_AddRef,
3054 SimpleAudioVolume_Release,
3055 SimpleAudioVolume_SetMasterVolume,
3056 SimpleAudioVolume_GetMasterVolume,
3057 SimpleAudioVolume_SetMute,
3058 SimpleAudioVolume_GetMute
3061 static HRESULT WINAPI ChannelAudioVolume_QueryInterface(
3062 IChannelAudioVolume *iface, REFIID riid, void **ppv)
3064 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
3066 if (!ppv)
3067 return E_POINTER;
3068 *ppv = NULL;
3070 if (IsEqualIID(riid, &IID_IUnknown) ||
3071 IsEqualIID(riid, &IID_IChannelAudioVolume))
3072 *ppv = iface;
3073 if (*ppv) {
3074 IUnknown_AddRef((IUnknown*)*ppv);
3075 return S_OK;
3078 WARN("Unknown interface %s\n", debugstr_guid(riid));
3079 return E_NOINTERFACE;
3082 static ULONG WINAPI ChannelAudioVolume_AddRef(IChannelAudioVolume *iface)
3084 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
3085 return AudioSessionControl_AddRef(&This->IAudioSessionControl2_iface);
3088 static ULONG WINAPI ChannelAudioVolume_Release(IChannelAudioVolume *iface)
3090 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
3091 return AudioSessionControl_Release(&This->IAudioSessionControl2_iface);
3094 static HRESULT WINAPI ChannelAudioVolume_GetChannelCount(
3095 IChannelAudioVolume *iface, UINT32 *out)
3097 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
3098 AudioSession *session = This->session;
3100 TRACE("(%p)->(%p)\n", session, out);
3102 if (!out)
3103 return NULL_PTR_ERR;
3105 *out = session->channel_count;
3107 return S_OK;
3110 static HRESULT WINAPI ChannelAudioVolume_SetChannelVolume(
3111 IChannelAudioVolume *iface, UINT32 index, float level,
3112 const GUID *context)
3114 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
3115 AudioSession *session = This->session;
3117 TRACE("(%p)->(%d, %f, %s)\n", session, index, level,
3118 wine_dbgstr_guid(context));
3120 if (level < 0.f || level > 1.f)
3121 return E_INVALIDARG;
3123 if (index >= session->channel_count)
3124 return E_INVALIDARG;
3126 if (context)
3127 FIXME("Notifications not supported yet\n");
3129 TRACE("Pulseaudio does not support session volume control\n");
3131 pthread_mutex_lock(&pulse_lock);
3132 session->channel_vols[index] = level;
3133 pthread_mutex_unlock(&pulse_lock);
3135 return S_OK;
3138 static HRESULT WINAPI ChannelAudioVolume_GetChannelVolume(
3139 IChannelAudioVolume *iface, UINT32 index, float *level)
3141 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
3142 AudioSession *session = This->session;
3144 TRACE("(%p)->(%d, %p)\n", session, index, level);
3146 if (!level)
3147 return NULL_PTR_ERR;
3149 if (index >= session->channel_count)
3150 return E_INVALIDARG;
3152 *level = session->channel_vols[index];
3154 return S_OK;
3157 static HRESULT WINAPI ChannelAudioVolume_SetAllVolumes(
3158 IChannelAudioVolume *iface, UINT32 count, const float *levels,
3159 const GUID *context)
3161 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
3162 AudioSession *session = This->session;
3163 int i;
3165 TRACE("(%p)->(%d, %p, %s)\n", session, count, levels,
3166 wine_dbgstr_guid(context));
3168 if (!levels)
3169 return NULL_PTR_ERR;
3171 if (count != session->channel_count)
3172 return E_INVALIDARG;
3174 if (context)
3175 FIXME("Notifications not supported yet\n");
3177 TRACE("Pulseaudio does not support session volume control\n");
3179 pthread_mutex_lock(&pulse_lock);
3180 for(i = 0; i < count; ++i)
3181 session->channel_vols[i] = levels[i];
3182 pthread_mutex_unlock(&pulse_lock);
3183 return S_OK;
3186 static HRESULT WINAPI ChannelAudioVolume_GetAllVolumes(
3187 IChannelAudioVolume *iface, UINT32 count, float *levels)
3189 AudioSessionWrapper *This = impl_from_IChannelAudioVolume(iface);
3190 AudioSession *session = This->session;
3191 int i;
3193 TRACE("(%p)->(%d, %p)\n", session, count, levels);
3195 if (!levels)
3196 return NULL_PTR_ERR;
3198 if (count != session->channel_count)
3199 return E_INVALIDARG;
3201 for(i = 0; i < count; ++i)
3202 levels[i] = session->channel_vols[i];
3204 return S_OK;
3207 static const IChannelAudioVolumeVtbl ChannelAudioVolume_Vtbl =
3209 ChannelAudioVolume_QueryInterface,
3210 ChannelAudioVolume_AddRef,
3211 ChannelAudioVolume_Release,
3212 ChannelAudioVolume_GetChannelCount,
3213 ChannelAudioVolume_SetChannelVolume,
3214 ChannelAudioVolume_GetChannelVolume,
3215 ChannelAudioVolume_SetAllVolumes,
3216 ChannelAudioVolume_GetAllVolumes
3219 HRESULT WINAPI AUDDRV_GetAudioSessionManager(IMMDevice *device,
3220 IAudioSessionManager2 **out)
3222 SessionMgr *This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(SessionMgr));
3223 *out = NULL;
3224 if (!This)
3225 return E_OUTOFMEMORY;
3226 This->IAudioSessionManager2_iface.lpVtbl = &AudioSessionManager2_Vtbl;
3227 This->device = device;
3228 This->ref = 1;
3229 *out = &This->IAudioSessionManager2_iface;
3230 return S_OK;
3233 HRESULT WINAPI AUDDRV_GetPropValue(GUID *guid, const PROPERTYKEY *prop, PROPVARIANT *out)
3235 TRACE("%s, (%s,%u), %p\n", wine_dbgstr_guid(guid), wine_dbgstr_guid(&prop->fmtid), prop->pid, out);
3237 if (IsEqualGUID(guid, &pulse_render_guid) && IsEqualPropertyKey(*prop, PKEY_AudioEndpoint_PhysicalSpeakers)) {
3238 out->vt = VT_UI4;
3239 out->u.ulVal = g_phys_speakers_mask;
3241 return out->u.ulVal ? S_OK : E_FAIL;
3244 return E_NOTIMPL;