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
35 #include <pulse/pulseaudio.h>
41 #include "wine/debug.h"
42 #include "wine/unicode.h"
43 #include "wine/list.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> */
65 Priority_Unavailable
= 0,
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
) {
107 SetThreadPriority(pulse_thread
, 0);
109 pa_context_disconnect(pulse_ctx
);
110 pa_context_unref(pulse_ctx
);
113 pa_mainloop_quit(pulse_ml
, 0);
115 WaitForSingleObject(pulse_thread
, INFINITE
);
116 CloseHandle(pulse_thread
);
122 typedef struct ACImpl ACImpl
;
124 typedef struct _AudioSession
{
131 UINT32 channel_count
;
138 typedef struct _AudioSessionWrapper
{
139 IAudioSessionControl2 IAudioSessionControl2_iface
;
140 IChannelAudioVolume IChannelAudioVolume_iface
;
141 ISimpleAudioVolume ISimpleAudioVolume_iface
;
146 AudioSession
*session
;
147 } AudioSessionWrapper
;
149 typedef struct _ACPacket
{
157 IAudioClient IAudioClient_iface
;
158 IAudioRenderClient IAudioRenderClient_iface
;
159 IAudioCaptureClient IAudioCaptureClient_iface
;
160 IAudioClock IAudioClock_iface
;
161 IAudioClock2 IAudioClock2_iface
;
162 IAudioStreamVolume IAudioStreamVolume_iface
;
166 float vol
[PA_CHANNELS_MAX
];
171 AUDCLNT_SHAREMODE share
;
174 UINT32 bufsize_frames
, bufsize_bytes
, locked
, capture_period
, pad
, started
, peek_ofs
;
175 void *locked_ptr
, *tmp_buffer
;
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
) {
265 pthread_mutex_unlock(&pulse_lock
);
266 r
= poll(ufds
, nfds
, timeout
);
267 pthread_mutex_lock(&pulse_lock
);
271 static DWORD CALLBACK
pulse_mainloop_thread(void *tmp
) {
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
);
283 static void pulse_contextcallback(pa_context
*c
, void *userdata
)
285 switch (pa_context_get_state(c
)) {
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
));
296 case PA_CONTEXT_READY
:
300 case PA_CONTEXT_FAILED
:
301 ERR("Context failed: %s\n", pa_strerror(pa_context_errno(c
)));
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
) {
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;
366 static void pulse_probe_settings(int render
, WAVEFORMATEXTENSIBLE
*fmt
) {
367 WAVEFORMATEX
*wfx
= &fmt
->Format
;
373 unsigned int length
= 0;
375 pa_channel_map_init_auto(&map
, 2, PA_CHANNEL_MAP_ALSA
);
377 ss
.format
= PA_SAMPLE_FLOAT32LE
;
378 ss
.channels
= map
.channels
;
382 attr
.minreq
= attr
.fragsize
= pa_frame_size(&ss
);
385 stream
= pa_stream_new(pulse_ctx
, "format test stream", &ss
, &map
);
387 pa_stream_set_state_callback(stream
, pulse_stream_state
, NULL
);
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
);
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
);
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
);
403 length
= pa_stream_get_buffer_attr(stream
)->minreq
;
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
)
413 pa_stream_unref(stream
);
415 pulse_def_period
[!render
] = pulse_min_period
[!render
] = pa_bytes_to_usec(10 * length
, &ss
);
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
;
431 fmt
->Samples
.wValidBitsPerSample
= 24;
432 if (ss
.format
== PA_SAMPLE_FLOAT32LE
)
433 fmt
->SubFormat
= KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
;
435 fmt
->SubFormat
= KSDATAFORMAT_SUBTYPE_PCM
;
437 fmt
->dwChannelMask
= pulse_channel_map_to_channel_mask(&map
);
440 static HRESULT
pulse_connect(void)
443 WCHAR path
[PATH_MAX
], *name
;
448 if (!(pulse_thread
= CreateThread(NULL
, 0, pulse_mainloop_thread
, NULL
, 0, NULL
)))
450 ERR("Failed to create mainloop thread.\n");
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
)))
460 pa_context_unref(pulse_ctx
);
462 GetModuleFileNameW(NULL
, path
, sizeof(path
)/sizeof(*path
));
463 name
= strrchrW(path
, '\\');
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
);
475 ERR("Failed to create context\n");
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)
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
)
492 if (state
== PA_CONTEXT_READY
)
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
));
502 pa_context_unref(pulse_ctx
);
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
)
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
519 static HRESULT
pulse_test_connect(void)
522 WCHAR path
[PATH_MAX
], *name
;
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
, '\\');
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
);
543 ERR("Failed to create context\n");
544 pa_mainloop_free(pulse_ml
);
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)
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
)
562 if (state
== PA_CONTEXT_READY
)
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
);
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
);
584 pa_mainloop_free(pulse_ml
);
590 pa_context_unref(pulse_ctx
);
592 pa_mainloop_free(pulse_ml
);
598 static HRESULT
pulse_stream_valid(ACImpl
*This
) {
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
;
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");
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
;
641 if (oldpad
== This
->pad
)
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
));
650 SetEvent(This
->event
);
653 static void pulse_underflow_callback(pa_stream
*s
, void *userdata
)
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
) {
679 LARGE_INTEGER stamp
, freq
;
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
);
685 next
= (ACPacket
*)p
->entry
.next
;
688 p
= (ACPacket
*)list_tail(&This
->packet_filled_head
);
689 assert(This
->pad
== This
->bufsize_bytes
);
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
;
699 list_remove(&p
->entry
);
700 list_add_tail(&This
->packet_filled_head
, &p
->entry
);
704 pa_stream_peek(This
->stream
, (const void**)&src
, &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
);
714 memcpy(dst
, src
, rem
);
722 pa_stream_drop(This
->stream
);
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
;
736 pa_stream_peek(This
->stream
, &src
, &src_len
);
738 assert(This
->peek_ofs
< src_len
);
739 src_len
-= This
->peek_ofs
;
740 assert(src_len
<= bytes
);
751 pa_stream_drop(This
->stream
);
753 This
->peek_ofs
+= 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
)
770 pulse_rd_loop(This
, bytes
);
772 pulse_rd_drop(This
, bytes
);
775 SetEvent(This
->event
);
778 static HRESULT
pulse_stream_connect(ACImpl
*This
, UINT32 period_bytes
) {
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
);
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
);
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
);
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
);
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
);
827 pa_stream_set_read_callback(This
->stream
, pulse_rd_callback
, This
);
831 HRESULT WINAPI
AUDDRV_GetEndpointIDs(EDataFlow flow
, const WCHAR
***ids
, GUID
**keys
,
832 UINT
*num
, UINT
*def_index
)
836 TRACE("%d %p %p %p\n", flow
, ids
, num
, def_index
);
841 *ids
= HeapAlloc(GetProcessHeap(), 0, sizeof(**ids
));
844 return E_OUTOFMEMORY
;
846 (*ids
)[0] = id
= HeapAlloc(GetProcessHeap(), 0, sizeof(defaultW
));
847 *keys
= HeapAlloc(GetProcessHeap(), 0, sizeof(**keys
));
849 HeapFree(GetProcessHeap(), 0, id
);
850 HeapFree(GetProcessHeap(), 0, *keys
);
851 HeapFree(GetProcessHeap(), 0, *ids
);
854 return E_OUTOFMEMORY
;
856 memcpy(id
, defaultW
, sizeof(defaultW
));
859 (*keys
)[0] = pulse_render_guid
;
861 (*keys
)[0] = pulse_capture_guid
;
866 int WINAPI
AUDDRV_GetPriority(void)
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
)
882 TRACE("%s %p %p\n", debugstr_guid(guid
), dev
, out
);
883 if (IsEqualGUID(guid
, &pulse_render_guid
))
885 else if (IsEqualGUID(guid
, &pulse_capture_guid
))
892 This
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*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
;
904 for (i
= 0; i
< PA_CHANNELS_MAX
; ++i
)
907 hr
= CoCreateFreeThreadedMarshaler((IUnknown
*)This
, &This
->marshal
);
909 HeapFree(GetProcessHeap(), 0, This
);
912 IMMDevice_AddRef(This
->parent
);
914 *out
= &This
->IAudioClient_iface
;
915 IAudioClient_AddRef(&This
->IAudioClient_iface
);
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
);
931 if (IsEqualIID(riid
, &IID_IUnknown
) || IsEqualIID(riid
, &IID_IAudioClient
))
934 IUnknown_AddRef((IUnknown
*)*ppv
);
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
);
949 ref
= InterlockedIncrement(&This
->ref
);
950 TRACE("(%p) Refcount now %u\n", This
, ref
);
954 static ULONG WINAPI
AudioClient_Release(IAudioClient
*iface
)
956 ACImpl
*This
= impl_from_IAudioClient(iface
);
958 ref
= InterlockedDecrement(&This
->ref
);
959 TRACE("(%p) Refcount now %u\n", This
, ref
);
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
);
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
);
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");
988 case WAVE_FORMAT_IEEE_FLOAT
:
989 TRACE("WAVE_FORMAT_IEEE_FLOAT");
991 case WAVE_FORMAT_EXTENSIBLE
:
992 TRACE("WAVE_FORMAT_EXTENSIBLE");
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
)
1020 if (fmt
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
)
1021 size
= sizeof(WAVEFORMATEXTENSIBLE
);
1023 size
= sizeof(WAVEFORMATEX
);
1025 ret
= CoTaskMemAlloc(size
);
1029 memcpy(ret
, fmt
, size
);
1031 ret
->cbSize
= size
- sizeof(WAVEFORMATEX
);
1036 static DWORD
get_channel_mask(unsigned int channels
)
1042 return KSAUDIO_SPEAKER_MONO
;
1044 return KSAUDIO_SPEAKER_STEREO
;
1046 return KSAUDIO_SPEAKER_STEREO
| SPEAKER_LOW_FREQUENCY
;
1048 return KSAUDIO_SPEAKER_QUAD
; /* not _SURROUND */
1050 return KSAUDIO_SPEAKER_QUAD
| SPEAKER_LOW_FREQUENCY
;
1052 return KSAUDIO_SPEAKER_5POINT1
; /* not 5POINT1_SURROUND */
1054 return KSAUDIO_SPEAKER_5POINT1
| SPEAKER_BACK_CENTER
;
1056 return KSAUDIO_SPEAKER_7POINT1_SURROUND
; /* Vista deprecates 7POINT1 */
1058 FIXME("Unknown speaker configuration: %u\n", channels
);
1062 static void session_init_vols(AudioSession
*session
, UINT channels
)
1064 if (session
->channel_count
< channels
) {
1067 if (session
->channel_vols
)
1068 session
->channel_vols
= HeapReAlloc(GetProcessHeap(), 0,
1069 session
->channel_vols
, sizeof(float) * channels
);
1071 session
->channel_vols
= HeapAlloc(GetProcessHeap(), 0,
1072 sizeof(float) * channels
);
1073 if (!session
->channel_vols
)
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
,
1088 ret
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(AudioSession
));
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
;
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
);
1117 return E_OUTOFMEMORY
;
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
);
1133 *out
= create_session(sessionguid
, device
, channels
);
1135 return E_OUTOFMEMORY
;
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)
1151 This
->ss
.format
= PA_SAMPLE_FLOAT32LE
;
1152 pa_channel_map_init_auto(&This
->map
, fmt
->nChannels
, PA_CHANNEL_MAP_ALSA
);
1154 case WAVE_FORMAT_PCM
:
1155 if (!fmt
->nChannels
|| fmt
->nChannels
> 2)
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
;
1162 return AUDCLNT_E_UNSUPPORTED_FORMAT
;
1163 pa_channel_map_init_auto(&This
->map
, fmt
->nChannels
, PA_CHANNEL_MAP_ALSA
);
1165 case WAVE_FORMAT_EXTENSIBLE
: {
1166 WAVEFORMATEXTENSIBLE
*wfe
= (WAVEFORMATEXTENSIBLE
*)fmt
;
1167 DWORD mask
= wfe
->dwChannelMask
;
1169 if (fmt
->cbSize
!= (sizeof(*wfe
) - sizeof(*fmt
)) && fmt
->cbSize
!= sizeof(*wfe
))
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
;
1178 valid
= fmt
->wBitsPerSample
;
1179 if (!valid
|| valid
> fmt
->wBitsPerSample
)
1181 switch (fmt
->wBitsPerSample
) {
1184 This
->ss
.format
= PA_SAMPLE_U8
;
1188 This
->ss
.format
= PA_SAMPLE_S16LE
;
1192 This
->ss
.format
= PA_SAMPLE_S24LE
;
1196 This
->ss
.format
= PA_SAMPLE_S24_32LE
;
1197 else if (valid
== 32)
1198 This
->ss
.format
= PA_SAMPLE_S32LE
;
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
);
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
);
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
;
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
);
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
));
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();
1284 pthread_mutex_unlock(&pulse_lock
);
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
);
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
1310 if (duration
< 2 * def
)
1314 if (duration
< 2 * period
)
1315 duration
= 2 * period
;
1317 /* Uh oh, really low latency requested.. */
1318 if (duration
<= 2 * period
)
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
);
1326 This
->bufsize_frames
= 2 * fmt
->nSamplesPerSec
;
1327 This
->bufsize_bytes
= This
->bufsize_frames
* pa_frame_size(&This
->ss
);
1330 This
->flags
= flags
;
1331 hr
= pulse_stream_connect(This
, period_bytes
);
1332 if (SUCCEEDED(hr
)) {
1334 const pa_buffer_attr
*attr
= pa_stream_get_buffer_attr(This
->stream
);
1335 /* Update frames according to new size */
1337 if (This
->dataflow
== eRender
)
1338 This
->bufsize_bytes
= attr
->tlength
;
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
)
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
);
1367 hr
= get_audio_session(sessionguid
, This
->parent
, fmt
->nChannels
, &This
->session
);
1369 list_add_tail(&This
->session
->clients
, &This
->entry
);
1373 HeapFree(GetProcessHeap(), 0, This
->tmp_buffer
);
1374 This
->tmp_buffer
= NULL
;
1376 pa_stream_disconnect(This
->stream
);
1377 pa_stream_unref(This
->stream
);
1378 This
->stream
= NULL
;
1381 pthread_mutex_unlock(&pulse_lock
);
1385 static HRESULT WINAPI
AudioClient_GetBufferSize(IAudioClient
*iface
,
1388 ACImpl
*This
= impl_from_IAudioClient(iface
);
1391 TRACE("(%p)->(%p)\n", This
, out
);
1396 pthread_mutex_lock(&pulse_lock
);
1397 hr
= pulse_stream_valid(This
);
1399 *out
= This
->bufsize_frames
;
1400 pthread_mutex_unlock(&pulse_lock
);
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
;
1413 TRACE("(%p)->(%p)\n", This
, latency
);
1418 pthread_mutex_lock(&pulse_lock
);
1419 hr
= pulse_stream_valid(This
);
1421 pthread_mutex_unlock(&pulse_lock
);
1424 attr
= pa_stream_get_buffer_attr(This
->stream
);
1425 if (This
->dataflow
== eRender
)
1426 lat
= attr
->minreq
/ pa_frame_size(&This
->ss
);
1428 lat
= attr
->fragsize
/ pa_frame_size(&This
->ss
);
1429 *latency
= 10000000;
1431 *latency
/= This
->ss
.rate
;
1432 pthread_mutex_unlock(&pulse_lock
);
1433 TRACE("Latency: %u ms\n", (DWORD
)(*latency
/ 10000));
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
);
1451 *out
= This
->pad
/ pa_frame_size(&This
->ss
);
1454 static HRESULT WINAPI
AudioClient_GetCurrentPadding(IAudioClient
*iface
,
1457 ACImpl
*This
= impl_from_IAudioClient(iface
);
1460 TRACE("(%p)->(%p)\n", This
, out
);
1465 pthread_mutex_lock(&pulse_lock
);
1466 hr
= pulse_stream_valid(This
);
1468 pthread_mutex_unlock(&pulse_lock
);
1472 if (This
->dataflow
== eRender
)
1473 ACImpl_GetRenderPad(This
, out
);
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
);
1482 static HRESULT WINAPI
AudioClient_IsFormatSupported(IAudioClient
*iface
,
1483 AUDCLNT_SHAREMODE mode
, const WAVEFORMATEX
*fmt
,
1486 ACImpl
*This
= impl_from_IAudioClient(iface
);
1488 WAVEFORMATEX
*closest
= NULL
;
1491 TRACE("(%p)->(%x, %p, %p)\n", This
, mode
, fmt
, out
);
1499 if (mode
== AUDCLNT_SHAREMODE_EXCLUSIVE
) {
1502 } else if (mode
== AUDCLNT_SHAREMODE_SHARED
) {
1507 return E_INVALIDARG
;
1509 if (fmt
->nChannels
== 0)
1510 return AUDCLNT_E_UNSUPPORTED_FORMAT
;
1512 closest
= clone_format(fmt
);
1514 return E_OUTOFMEMORY
;
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
) {
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
) {
1542 if (channels
!= fmt
->nChannels
|| (ext
->dwChannelMask
& ~mask
)) {
1543 hr
= AUDCLNT_E_UNSUPPORTED_FORMAT
;
1547 hr
= AUDCLNT_E_UNSUPPORTED_FORMAT
;
1552 if (IsEqualGUID(&ext
->SubFormat
, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
)) {
1553 if (fmt
->wBitsPerSample
!= 32) {
1558 if (ext
->Samples
.wValidBitsPerSample
!= fmt
->wBitsPerSample
) {
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) {
1568 if (ext
->Samples
.wValidBitsPerSample
!= fmt
->wBitsPerSample
&&
1569 !(fmt
->wBitsPerSample
== 32 &&
1570 ext
->Samples
.wValidBitsPerSample
== 24)) {
1572 ext
->Samples
.wValidBitsPerSample
= fmt
->wBitsPerSample
;
1576 hr
= AUDCLNT_E_UNSUPPORTED_FORMAT
;
1583 case WAVE_FORMAT_ALAW
:
1584 case WAVE_FORMAT_MULAW
:
1585 if (fmt
->wBitsPerSample
!= 8) {
1590 case WAVE_FORMAT_IEEE_FLOAT
:
1591 if (fmt
->wFormatTag
== WAVE_FORMAT_IEEE_FLOAT
&& fmt
->wBitsPerSample
!= 32) {
1596 case WAVE_FORMAT_PCM
:
1597 if (fmt
->wFormatTag
== WAVE_FORMAT_PCM
&&
1598 (!fmt
->wBitsPerSample
|| fmt
->wBitsPerSample
> 32 || fmt
->wBitsPerSample
% 8)) {
1603 if (fmt
->nChannels
> 2) {
1604 hr
= AUDCLNT_E_UNSUPPORTED_FORMAT
;
1608 * fmt->cbSize, fmt->nBlockAlign and fmt->nAvgBytesPerSec seem to be
1609 * ignored, invalid values are happily accepted.
1613 hr
= AUDCLNT_E_UNSUPPORTED_FORMAT
;
1617 if (exclusive
&& hr
!= S_OK
) {
1618 hr
= AUDCLNT_E_UNSUPPORTED_FORMAT
;
1619 CoTaskMemFree(closest
);
1620 } else if (hr
!= S_FALSE
)
1621 CoTaskMemFree(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
);
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
);
1646 *pwfx
= clone_format(&fmt
->Format
);
1648 return E_OUTOFMEMORY
;
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
)
1664 *defperiod
= pulse_def_period
[This
->dataflow
== eCapture
];
1666 *minperiod
= pulse_min_period
[This
->dataflow
== eCapture
];
1671 static HRESULT WINAPI
AudioClient_Start(IAudioClient
*iface
)
1673 ACImpl
*This
= impl_from_IAudioClient(iface
);
1678 TRACE("(%p)\n", This
);
1680 pthread_mutex_lock(&pulse_lock
);
1681 hr
= pulse_stream_valid(This
);
1683 pthread_mutex_unlock(&pulse_lock
);
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
);
1700 while(pa_operation_get_state(o
) == PA_OPERATION_RUNNING
)
1701 pthread_cond_wait(&pulse_cond
, &pulse_lock
);
1702 pa_operation_unref(o
);
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
);
1717 static HRESULT WINAPI
AudioClient_Stop(IAudioClient
*iface
)
1719 ACImpl
*This
= impl_from_IAudioClient(iface
);
1724 TRACE("(%p)\n", This
);
1726 pthread_mutex_lock(&pulse_lock
);
1727 hr
= pulse_stream_valid(This
);
1729 pthread_mutex_unlock(&pulse_lock
);
1733 if (!This
->started
) {
1734 pthread_mutex_unlock(&pulse_lock
);
1738 if (This
->dataflow
== eRender
) {
1739 o
= pa_stream_cork(This
->stream
, 1, pulse_op_cb
, &success
);
1741 while(pa_operation_get_state(o
) == PA_OPERATION_RUNNING
)
1742 pthread_cond_wait(&pulse_cond
, &pulse_lock
);
1743 pa_operation_unref(o
);
1749 if (SUCCEEDED(hr
)) {
1750 This
->started
= FALSE
;
1752 pthread_mutex_unlock(&pulse_lock
);
1756 static HRESULT WINAPI
AudioClient_Reset(IAudioClient
*iface
)
1758 ACImpl
*This
= impl_from_IAudioClient(iface
);
1761 TRACE("(%p)\n", This
);
1763 pthread_mutex_lock(&pulse_lock
);
1764 hr
= pulse_stream_valid(This
);
1766 pthread_mutex_unlock(&pulse_lock
);
1770 if (This
->started
) {
1771 pthread_mutex_unlock(&pulse_lock
);
1772 return AUDCLNT_E_NOT_STOPPED
;
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 */
1784 pa_operation
*o
= pa_stream_flush(This
->stream
, pulse_op_cb
, &success
);
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;
1795 This
->clock_written
+= This
->pad
;
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
);
1809 static HRESULT WINAPI
AudioClient_SetEventHandle(IAudioClient
*iface
,
1812 ACImpl
*This
= impl_from_IAudioClient(iface
);
1815 TRACE("(%p)->(%p)\n", This
, event
);
1818 return E_INVALIDARG
;
1820 pthread_mutex_lock(&pulse_lock
);
1821 hr
= pulse_stream_valid(This
);
1823 pthread_mutex_unlock(&pulse_lock
);
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
);
1832 This
->event
= event
;
1833 pthread_mutex_unlock(&pulse_lock
);
1837 static HRESULT WINAPI
AudioClient_GetService(IAudioClient
*iface
, REFIID riid
,
1840 ACImpl
*This
= impl_from_IAudioClient(iface
);
1843 TRACE("(%p)->(%s, %p)\n", This
, debugstr_guid(riid
), ppv
);
1849 pthread_mutex_lock(&pulse_lock
);
1850 hr
= pulse_stream_valid(This
);
1851 pthread_mutex_unlock(&pulse_lock
);
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
;
1884 IUnknown_AddRef((IUnknown
*)*ppv
);
1888 FIXME("stub %s\n", debugstr_guid(riid
));
1889 return E_NOINTERFACE
;
1892 static const IAudioClientVtbl AudioClient_Vtbl
=
1894 AudioClient_QueryInterface
,
1896 AudioClient_Release
,
1897 AudioClient_Initialize
,
1898 AudioClient_GetBufferSize
,
1899 AudioClient_GetStreamLatency
,
1900 AudioClient_GetCurrentPadding
,
1901 AudioClient_IsFormatSupported
,
1902 AudioClient_GetMixFormat
,
1903 AudioClient_GetDevicePeriod
,
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
);
1921 if (IsEqualIID(riid
, &IID_IUnknown
) ||
1922 IsEqualIID(riid
, &IID_IAudioRenderClient
))
1925 IUnknown_AddRef((IUnknown
*)*ppv
);
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
);
1957 TRACE("(%p)->(%u, %p)\n", This
, frames
, data
);
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
;
1970 pthread_mutex_unlock(&pulse_lock
);
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
;
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
);
1988 pa_stream_cancel_write(This
->stream
);
1989 *data
= This
->tmp_buffer
;
1990 This
->locked_ptr
= NULL
;
1992 *data
= This
->locked_ptr
;
1993 pthread_mutex_unlock(&pulse_lock
);
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
);
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
;
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
);
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
);
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
);
2062 if (IsEqualIID(riid
, &IID_IUnknown
) ||
2063 IsEqualIID(riid
, &IID_IAudioCaptureClient
))
2066 IUnknown_AddRef((IUnknown
*)*ppv
);
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
,
2093 ACImpl
*This
= impl_from_IAudioCaptureClient(iface
);
2097 TRACE("(%p)->(%p, %p, %p, %p, %p)\n", This
, data
, frames
, flags
,
2100 if (!data
|| !frames
|| !flags
)
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
);
2114 if (packet
->discont
)
2115 *flags
|= AUDCLNT_BUFFERFLAGS_DATA_DISCONTINUITY
;
2117 if (packet
->discont
)
2118 *devpos
= (This
->clock_written
+ This
->capture_period
) / pa_frame_size(&This
->ss
);
2120 *devpos
= This
->clock_written
/ pa_frame_size(&This
->ss
);
2123 *qpcpos
= packet
->qpcpos
;
2124 *data
= packet
->data
;
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
;
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
;
2156 This
->clock_written
+= This
->capture_period
;
2157 list_add_tail(&This
->packet_free_head
, &packet
->entry
);
2160 pthread_mutex_unlock(&pulse_lock
);
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
);
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
);
2179 pthread_mutex_unlock(&pulse_lock
);
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
);
2204 if (IsEqualIID(riid
, &IID_IUnknown
) || IsEqualIID(riid
, &IID_IAudioClock
))
2206 else if (IsEqualIID(riid
, &IID_IAudioClock2
))
2207 *ppv
= &This
->IAudioClock2_iface
;
2209 IUnknown_AddRef((IUnknown
*)*ppv
);
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
);
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
);
2245 *freq
= This
->ss
.rate
;
2247 pthread_mutex_unlock(&pulse_lock
);
2251 static HRESULT WINAPI
AudioClock_GetPosition(IAudioClock
*iface
, UINT64
*pos
,
2254 ACImpl
*This
= impl_from_IAudioClock(iface
);
2257 TRACE("(%p)->(%p, %p)\n", This
, pos
, qpctime
);
2262 pthread_mutex_lock(&pulse_lock
);
2263 hr
= pulse_stream_valid(This
);
2265 pthread_mutex_unlock(&pulse_lock
);
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
;
2278 This
->clock_lastpos
= *pos
;
2279 pthread_mutex_unlock(&pulse_lock
);
2281 TRACE("%p Position: %u\n", This
, (unsigned)*pos
);
2284 LARGE_INTEGER stamp
, freq
;
2285 QueryPerformanceCounter(&stamp
);
2286 QueryPerformanceFrequency(&freq
);
2287 *qpctime
= (stamp
.QuadPart
* (INT64
)10000000) / freq
.QuadPart
;
2293 static HRESULT WINAPI
AudioClock_GetCharacteristics(IAudioClock
*iface
,
2296 ACImpl
*This
= impl_from_IAudioClock(iface
);
2298 TRACE("(%p)->(%p)\n", This
, chars
);
2303 *chars
= AUDIOCLOCK_CHARACTERISTIC_FIXED_FREQ
;
2308 static const IAudioClockVtbl AudioClock_Vtbl
=
2310 AudioClock_QueryInterface
,
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
);
2347 static const IAudioClock2Vtbl AudioClock2_Vtbl
=
2349 AudioClock2_QueryInterface
,
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
);
2366 if (IsEqualIID(riid
, &IID_IUnknown
) ||
2367 IsEqualIID(riid
, &IID_IAudioStreamVolume
))
2370 IUnknown_AddRef((IUnknown
*)*ppv
);
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
);
2403 *out
= This
->ss
.channels
;
2408 struct pulse_info_cb_data
{
2413 static HRESULT WINAPI
AudioStreamVolume_SetAllVolumes(
2414 IAudioStreamVolume
*iface
, UINT32 count
, const float *levels
)
2416 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
2420 TRACE("(%p)->(%d, %p)\n", This
, count
, levels
);
2425 if (count
!= This
->ss
.channels
)
2426 return E_INVALIDARG
;
2428 pthread_mutex_lock(&pulse_lock
);
2429 hr
= pulse_stream_valid(This
);
2433 for (i
= 0; i
< count
; ++i
)
2434 This
->vol
[i
] = levels
[i
];
2437 pthread_mutex_unlock(&pulse_lock
);
2441 static HRESULT WINAPI
AudioStreamVolume_GetAllVolumes(
2442 IAudioStreamVolume
*iface
, UINT32 count
, float *levels
)
2444 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
2448 TRACE("(%p)->(%d, %p)\n", This
, count
, levels
);
2453 if (count
!= This
->ss
.channels
)
2454 return E_INVALIDARG
;
2456 pthread_mutex_lock(&pulse_lock
);
2457 hr
= pulse_stream_valid(This
);
2461 for (i
= 0; i
< count
; ++i
)
2462 levels
[i
] = This
->vol
[i
];
2465 pthread_mutex_unlock(&pulse_lock
);
2469 static HRESULT WINAPI
AudioStreamVolume_SetChannelVolume(
2470 IAudioStreamVolume
*iface
, UINT32 index
, float level
)
2472 ACImpl
*This
= impl_from_IAudioStreamVolume(iface
);
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
;
2487 hr
= AudioStreamVolume_SetAllVolumes(iface
, This
->ss
.channels
, volumes
);
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
];
2498 TRACE("(%p)->(%d, %p)\n", This
, index
, level
);
2503 if (index
>= This
->ss
.channels
)
2504 return E_INVALIDARG
;
2506 hr
= AudioStreamVolume_GetAllVolumes(iface
, This
->ss
.channels
, volumes
);
2508 *level
= volumes
[index
];
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
));
2533 ret
->IAudioSessionControl2_iface
.lpVtbl
= &AudioSessionControl2_Vtbl
;
2534 ret
->ISimpleAudioVolume_iface
.lpVtbl
= &SimpleAudioVolume_Vtbl
;
2535 ret
->IChannelAudioVolume_iface
.lpVtbl
= &ChannelAudioVolume_Vtbl
;
2539 ret
->client
= client
;
2541 ret
->session
= client
->session
;
2542 AudioClient_AddRef(&client
->IAudioClient_iface
);
2548 static HRESULT WINAPI
AudioSessionControl_QueryInterface(
2549 IAudioSessionControl2
*iface
, REFIID riid
, void **ppv
)
2551 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
2557 if (IsEqualIID(riid
, &IID_IUnknown
) ||
2558 IsEqualIID(riid
, &IID_IAudioSessionControl
) ||
2559 IsEqualIID(riid
, &IID_IAudioSessionControl2
))
2562 IUnknown_AddRef((IUnknown
*)*ppv
);
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
);
2574 ref
= InterlockedIncrement(&This
->ref
);
2575 TRACE("(%p) Refcount now %u\n", This
, ref
);
2579 static ULONG WINAPI
AudioSessionControl_Release(IAudioSessionControl2
*iface
)
2581 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2583 ref
= InterlockedDecrement(&This
->ref
);
2584 TRACE("(%p) Refcount now %u\n", This
, ref
);
2587 This
->client
->session_wrapper
= NULL
;
2588 AudioClient_Release(&This
->client
->IAudioClient_iface
);
2590 HeapFree(GetProcessHeap(), 0, This
);
2595 static HRESULT WINAPI
AudioSessionControl_GetState(IAudioSessionControl2
*iface
,
2596 AudioSessionState
*state
)
2598 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2601 TRACE("(%p)->(%p)\n", This
, state
);
2604 return NULL_PTR_ERR
;
2606 pthread_mutex_lock(&pulse_lock
);
2607 if (list_empty(&This
->session
->clients
)) {
2608 *state
= AudioSessionStateExpired
;
2611 LIST_FOR_EACH_ENTRY(client
, &This
->session
->clients
, ACImpl
, entry
) {
2612 if (client
->started
) {
2613 *state
= AudioSessionStateActive
;
2617 *state
= AudioSessionStateInactive
;
2620 pthread_mutex_unlock(&pulse_lock
);
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
);
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
));
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
);
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
));
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
);
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
));
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
);
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
);
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
);
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
);
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
);
2735 *pid
= GetCurrentProcessId();
2740 static HRESULT WINAPI
AudioSessionControl_IsSystemSoundsSession(
2741 IAudioSessionControl2
*iface
)
2743 AudioSessionWrapper
*This
= impl_from_IAudioSessionControl2(iface
);
2745 TRACE("(%p)\n", This
);
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
);
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
;
2789 static HRESULT WINAPI
AudioSessionManager_QueryInterface(IAudioSessionManager2
*iface
,
2790 REFIID riid
, void **ppv
)
2792 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppv
);
2798 if (IsEqualIID(riid
, &IID_IUnknown
) ||
2799 IsEqualIID(riid
, &IID_IAudioSessionManager
) ||
2800 IsEqualIID(riid
, &IID_IAudioSessionManager2
))
2803 IUnknown_AddRef((IUnknown
*)*ppv
);
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
);
2820 ref
= InterlockedIncrement(&This
->ref
);
2821 TRACE("(%p) Refcount now %u\n", This
, ref
);
2825 static ULONG WINAPI
AudioSessionManager_Release(IAudioSessionManager2
*iface
)
2827 SessionMgr
*This
= impl_from_IAudioSessionManager2(iface
);
2829 ref
= InterlockedDecrement(&This
->ref
);
2830 TRACE("(%p) Refcount now %u\n", This
, ref
);
2832 HeapFree(GetProcessHeap(), 0, This
);
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
;
2845 TRACE("(%p)->(%s, %x, %p)\n", This
, debugstr_guid(session_guid
),
2848 hr
= get_audio_session(session_guid
, This
->device
, 0, &session
);
2852 wrapper
= AudioSessionWrapper_Create(NULL
);
2854 return E_OUTOFMEMORY
;
2856 wrapper
->session
= session
;
2858 *out
= (IAudioSessionControl
*)&wrapper
->IAudioSessionControl2_iface
;
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
;
2872 TRACE("(%p)->(%s, %x, %p)\n", This
, debugstr_guid(session_guid
),
2875 hr
= get_audio_session(session_guid
, This
->device
, 0, &session
);
2879 wrapper
= AudioSessionWrapper_Create(NULL
);
2881 return E_OUTOFMEMORY
;
2883 wrapper
->session
= session
;
2885 *out
= &wrapper
->ISimpleAudioVolume_iface
;
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
);
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
);
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
);
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
);
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
);
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
);
2955 if (IsEqualIID(riid
, &IID_IUnknown
) ||
2956 IsEqualIID(riid
, &IID_ISimpleAudioVolume
))
2959 IUnknown_AddRef((IUnknown
*)*ppv
);
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
;
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
);
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
);
3011 return NULL_PTR_ERR
;
3013 *level
= session
->master_vol
;
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
);
3027 FIXME("Notifications not supported yet\n");
3029 session
->mute
= mute
;
3034 static HRESULT WINAPI
SimpleAudioVolume_GetMute(ISimpleAudioVolume
*iface
,
3037 AudioSessionWrapper
*This
= impl_from_ISimpleAudioVolume(iface
);
3038 AudioSession
*session
= This
->session
;
3040 TRACE("(%p)->(%p)\n", session
, mute
);
3043 return NULL_PTR_ERR
;
3045 *mute
= session
->mute
;
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
);
3070 if (IsEqualIID(riid
, &IID_IUnknown
) ||
3071 IsEqualIID(riid
, &IID_IChannelAudioVolume
))
3074 IUnknown_AddRef((IUnknown
*)*ppv
);
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
);
3103 return NULL_PTR_ERR
;
3105 *out
= session
->channel_count
;
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
;
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
);
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
);
3147 return NULL_PTR_ERR
;
3149 if (index
>= session
->channel_count
)
3150 return E_INVALIDARG
;
3152 *level
= session
->channel_vols
[index
];
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
;
3165 TRACE("(%p)->(%d, %p, %s)\n", session
, count
, levels
,
3166 wine_dbgstr_guid(context
));
3169 return NULL_PTR_ERR
;
3171 if (count
!= session
->channel_count
)
3172 return E_INVALIDARG
;
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
);
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
;
3193 TRACE("(%p)->(%d, %p)\n", session
, count
, 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
];
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
));
3225 return E_OUTOFMEMORY
;
3226 This
->IAudioSessionManager2_iface
.lpVtbl
= &AudioSessionManager2_Vtbl
;
3227 This
->device
= device
;
3229 *out
= &This
->IAudioSessionManager2_iface
;
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
)) {
3239 out
->u
.ulVal
= g_phys_speakers_mask
;
3241 return out
->u
.ulVal
? S_OK
: E_FAIL
;