2 * OpenAL cross platform audio library
3 * Copyright (C) 2009 by Konstantinos Natsakis <konstantinos.natsakis@gmail.com>
4 * Copyright (C) 2010 by Chris Robinson <chris.kcat@gmail.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 * Or go to http://www.gnu.org/copyleft/lgpl.html
31 #include "backends/base.h"
33 #include <pulse/pulseaudio.h>
35 #if PA_API_VERSION == 12
38 static void *pa_handle
;
39 #define MAKE_FUNC(x) static __typeof(x) * p##x
40 MAKE_FUNC(pa_context_unref
);
41 MAKE_FUNC(pa_sample_spec_valid
);
42 MAKE_FUNC(pa_frame_size
);
43 MAKE_FUNC(pa_stream_drop
);
44 MAKE_FUNC(pa_strerror
);
45 MAKE_FUNC(pa_context_get_state
);
46 MAKE_FUNC(pa_stream_get_state
);
47 MAKE_FUNC(pa_threaded_mainloop_signal
);
48 MAKE_FUNC(pa_stream_peek
);
49 MAKE_FUNC(pa_threaded_mainloop_wait
);
50 MAKE_FUNC(pa_threaded_mainloop_unlock
);
51 MAKE_FUNC(pa_threaded_mainloop_in_thread
);
52 MAKE_FUNC(pa_context_new
);
53 MAKE_FUNC(pa_threaded_mainloop_stop
);
54 MAKE_FUNC(pa_context_disconnect
);
55 MAKE_FUNC(pa_threaded_mainloop_start
);
56 MAKE_FUNC(pa_threaded_mainloop_get_api
);
57 MAKE_FUNC(pa_context_set_state_callback
);
58 MAKE_FUNC(pa_stream_write
);
60 MAKE_FUNC(pa_stream_connect_record
);
61 MAKE_FUNC(pa_stream_connect_playback
);
62 MAKE_FUNC(pa_stream_readable_size
);
63 MAKE_FUNC(pa_stream_writable_size
);
64 MAKE_FUNC(pa_stream_is_corked
);
65 MAKE_FUNC(pa_stream_cork
);
66 MAKE_FUNC(pa_stream_is_suspended
);
67 MAKE_FUNC(pa_stream_get_device_name
);
68 MAKE_FUNC(pa_stream_get_latency
);
69 MAKE_FUNC(pa_path_get_filename
);
70 MAKE_FUNC(pa_get_binary_name
);
71 MAKE_FUNC(pa_threaded_mainloop_free
);
72 MAKE_FUNC(pa_context_errno
);
73 MAKE_FUNC(pa_xmalloc
);
74 MAKE_FUNC(pa_stream_unref
);
75 MAKE_FUNC(pa_threaded_mainloop_accept
);
76 MAKE_FUNC(pa_stream_set_write_callback
);
77 MAKE_FUNC(pa_threaded_mainloop_new
);
78 MAKE_FUNC(pa_context_connect
);
79 MAKE_FUNC(pa_stream_set_buffer_attr
);
80 MAKE_FUNC(pa_stream_get_buffer_attr
);
81 MAKE_FUNC(pa_stream_get_sample_spec
);
82 MAKE_FUNC(pa_stream_get_time
);
83 MAKE_FUNC(pa_stream_set_read_callback
);
84 MAKE_FUNC(pa_stream_set_state_callback
);
85 MAKE_FUNC(pa_stream_set_moved_callback
);
86 MAKE_FUNC(pa_stream_set_underflow_callback
);
87 MAKE_FUNC(pa_stream_new_with_proplist
);
88 MAKE_FUNC(pa_stream_disconnect
);
89 MAKE_FUNC(pa_threaded_mainloop_lock
);
90 MAKE_FUNC(pa_channel_map_init_auto
);
91 MAKE_FUNC(pa_channel_map_parse
);
92 MAKE_FUNC(pa_channel_map_snprint
);
93 MAKE_FUNC(pa_channel_map_equal
);
94 MAKE_FUNC(pa_context_get_server_info
);
95 MAKE_FUNC(pa_context_get_sink_info_by_name
);
96 MAKE_FUNC(pa_context_get_sink_info_list
);
97 MAKE_FUNC(pa_context_get_source_info_by_name
);
98 MAKE_FUNC(pa_context_get_source_info_list
);
99 MAKE_FUNC(pa_operation_get_state
);
100 MAKE_FUNC(pa_operation_unref
);
101 MAKE_FUNC(pa_proplist_new
);
102 MAKE_FUNC(pa_proplist_free
);
103 MAKE_FUNC(pa_proplist_set
);
104 MAKE_FUNC(pa_channel_map_superset
);
105 MAKE_FUNC(pa_stream_set_buffer_attr_callback
);
106 MAKE_FUNC(pa_stream_begin_write
);
109 #define pa_context_unref ppa_context_unref
110 #define pa_sample_spec_valid ppa_sample_spec_valid
111 #define pa_frame_size ppa_frame_size
112 #define pa_stream_drop ppa_stream_drop
113 #define pa_strerror ppa_strerror
114 #define pa_context_get_state ppa_context_get_state
115 #define pa_stream_get_state ppa_stream_get_state
116 #define pa_threaded_mainloop_signal ppa_threaded_mainloop_signal
117 #define pa_stream_peek ppa_stream_peek
118 #define pa_threaded_mainloop_wait ppa_threaded_mainloop_wait
119 #define pa_threaded_mainloop_unlock ppa_threaded_mainloop_unlock
120 #define pa_threaded_mainloop_in_thread ppa_threaded_mainloop_in_thread
121 #define pa_context_new ppa_context_new
122 #define pa_threaded_mainloop_stop ppa_threaded_mainloop_stop
123 #define pa_context_disconnect ppa_context_disconnect
124 #define pa_threaded_mainloop_start ppa_threaded_mainloop_start
125 #define pa_threaded_mainloop_get_api ppa_threaded_mainloop_get_api
126 #define pa_context_set_state_callback ppa_context_set_state_callback
127 #define pa_stream_write ppa_stream_write
128 #define pa_xfree ppa_xfree
129 #define pa_stream_connect_record ppa_stream_connect_record
130 #define pa_stream_connect_playback ppa_stream_connect_playback
131 #define pa_stream_readable_size ppa_stream_readable_size
132 #define pa_stream_writable_size ppa_stream_writable_size
133 #define pa_stream_is_corked ppa_stream_is_corked
134 #define pa_stream_cork ppa_stream_cork
135 #define pa_stream_is_suspended ppa_stream_is_suspended
136 #define pa_stream_get_device_name ppa_stream_get_device_name
137 #define pa_stream_get_latency ppa_stream_get_latency
138 #define pa_path_get_filename ppa_path_get_filename
139 #define pa_get_binary_name ppa_get_binary_name
140 #define pa_threaded_mainloop_free ppa_threaded_mainloop_free
141 #define pa_context_errno ppa_context_errno
142 #define pa_xmalloc ppa_xmalloc
143 #define pa_stream_unref ppa_stream_unref
144 #define pa_threaded_mainloop_accept ppa_threaded_mainloop_accept
145 #define pa_stream_set_write_callback ppa_stream_set_write_callback
146 #define pa_threaded_mainloop_new ppa_threaded_mainloop_new
147 #define pa_context_connect ppa_context_connect
148 #define pa_stream_set_buffer_attr ppa_stream_set_buffer_attr
149 #define pa_stream_get_buffer_attr ppa_stream_get_buffer_attr
150 #define pa_stream_get_sample_spec ppa_stream_get_sample_spec
151 #define pa_stream_get_time ppa_stream_get_time
152 #define pa_stream_set_read_callback ppa_stream_set_read_callback
153 #define pa_stream_set_state_callback ppa_stream_set_state_callback
154 #define pa_stream_set_moved_callback ppa_stream_set_moved_callback
155 #define pa_stream_set_underflow_callback ppa_stream_set_underflow_callback
156 #define pa_stream_new_with_proplist ppa_stream_new_with_proplist
157 #define pa_stream_disconnect ppa_stream_disconnect
158 #define pa_threaded_mainloop_lock ppa_threaded_mainloop_lock
159 #define pa_channel_map_init_auto ppa_channel_map_init_auto
160 #define pa_channel_map_parse ppa_channel_map_parse
161 #define pa_channel_map_snprint ppa_channel_map_snprint
162 #define pa_channel_map_equal ppa_channel_map_equal
163 #define pa_context_get_server_info ppa_context_get_server_info
164 #define pa_context_get_sink_info_by_name ppa_context_get_sink_info_by_name
165 #define pa_context_get_sink_info_list ppa_context_get_sink_info_list
166 #define pa_context_get_source_info_by_name ppa_context_get_source_info_by_name
167 #define pa_context_get_source_info_list ppa_context_get_source_info_list
168 #define pa_operation_get_state ppa_operation_get_state
169 #define pa_operation_unref ppa_operation_unref
170 #define pa_proplist_new ppa_proplist_new
171 #define pa_proplist_free ppa_proplist_free
172 #define pa_proplist_set ppa_proplist_set
173 #define pa_channel_map_superset ppa_channel_map_superset
174 #define pa_stream_set_buffer_attr_callback ppa_stream_set_buffer_attr_callback
175 #define pa_stream_begin_write ppa_stream_begin_write
179 static ALCboolean
pulse_load(void)
181 ALCboolean ret
= ALC_TRUE
;
186 #define PALIB "libpulse-0.dll"
187 #elif defined(__APPLE__) && defined(__MACH__)
188 #define PALIB "libpulse.0.dylib"
190 #define PALIB "libpulse.so.0"
192 pa_handle
= LoadLib(PALIB
);
196 #define LOAD_FUNC(x) do { \
197 p##x = GetSymbol(pa_handle, #x); \
202 LOAD_FUNC(pa_context_unref
);
203 LOAD_FUNC(pa_sample_spec_valid
);
204 LOAD_FUNC(pa_stream_drop
);
205 LOAD_FUNC(pa_frame_size
);
206 LOAD_FUNC(pa_strerror
);
207 LOAD_FUNC(pa_context_get_state
);
208 LOAD_FUNC(pa_stream_get_state
);
209 LOAD_FUNC(pa_threaded_mainloop_signal
);
210 LOAD_FUNC(pa_stream_peek
);
211 LOAD_FUNC(pa_threaded_mainloop_wait
);
212 LOAD_FUNC(pa_threaded_mainloop_unlock
);
213 LOAD_FUNC(pa_threaded_mainloop_in_thread
);
214 LOAD_FUNC(pa_context_new
);
215 LOAD_FUNC(pa_threaded_mainloop_stop
);
216 LOAD_FUNC(pa_context_disconnect
);
217 LOAD_FUNC(pa_threaded_mainloop_start
);
218 LOAD_FUNC(pa_threaded_mainloop_get_api
);
219 LOAD_FUNC(pa_context_set_state_callback
);
220 LOAD_FUNC(pa_stream_write
);
222 LOAD_FUNC(pa_stream_connect_record
);
223 LOAD_FUNC(pa_stream_connect_playback
);
224 LOAD_FUNC(pa_stream_readable_size
);
225 LOAD_FUNC(pa_stream_writable_size
);
226 LOAD_FUNC(pa_stream_is_corked
);
227 LOAD_FUNC(pa_stream_cork
);
228 LOAD_FUNC(pa_stream_is_suspended
);
229 LOAD_FUNC(pa_stream_get_device_name
);
230 LOAD_FUNC(pa_stream_get_latency
);
231 LOAD_FUNC(pa_path_get_filename
);
232 LOAD_FUNC(pa_get_binary_name
);
233 LOAD_FUNC(pa_threaded_mainloop_free
);
234 LOAD_FUNC(pa_context_errno
);
235 LOAD_FUNC(pa_xmalloc
);
236 LOAD_FUNC(pa_stream_unref
);
237 LOAD_FUNC(pa_threaded_mainloop_accept
);
238 LOAD_FUNC(pa_stream_set_write_callback
);
239 LOAD_FUNC(pa_threaded_mainloop_new
);
240 LOAD_FUNC(pa_context_connect
);
241 LOAD_FUNC(pa_stream_set_buffer_attr
);
242 LOAD_FUNC(pa_stream_get_buffer_attr
);
243 LOAD_FUNC(pa_stream_get_sample_spec
);
244 LOAD_FUNC(pa_stream_get_time
);
245 LOAD_FUNC(pa_stream_set_read_callback
);
246 LOAD_FUNC(pa_stream_set_state_callback
);
247 LOAD_FUNC(pa_stream_set_moved_callback
);
248 LOAD_FUNC(pa_stream_set_underflow_callback
);
249 LOAD_FUNC(pa_stream_new_with_proplist
);
250 LOAD_FUNC(pa_stream_disconnect
);
251 LOAD_FUNC(pa_threaded_mainloop_lock
);
252 LOAD_FUNC(pa_channel_map_init_auto
);
253 LOAD_FUNC(pa_channel_map_parse
);
254 LOAD_FUNC(pa_channel_map_snprint
);
255 LOAD_FUNC(pa_channel_map_equal
);
256 LOAD_FUNC(pa_context_get_server_info
);
257 LOAD_FUNC(pa_context_get_sink_info_by_name
);
258 LOAD_FUNC(pa_context_get_sink_info_list
);
259 LOAD_FUNC(pa_context_get_source_info_by_name
);
260 LOAD_FUNC(pa_context_get_source_info_list
);
261 LOAD_FUNC(pa_operation_get_state
);
262 LOAD_FUNC(pa_operation_unref
);
263 LOAD_FUNC(pa_proplist_new
);
264 LOAD_FUNC(pa_proplist_free
);
265 LOAD_FUNC(pa_proplist_set
);
266 LOAD_FUNC(pa_channel_map_superset
);
267 LOAD_FUNC(pa_stream_set_buffer_attr_callback
);
268 LOAD_FUNC(pa_stream_begin_write
);
277 #endif /* HAVE_DYNLOAD */
282 /* Global flags and properties */
283 static pa_context_flags_t pulse_ctx_flags
;
284 static pa_proplist
*prop_filter
;
287 /* PulseAudio Event Callbacks */
288 static void context_state_callback(pa_context
*context
, void *pdata
)
290 pa_threaded_mainloop
*loop
= pdata
;
291 pa_context_state_t state
;
293 state
= pa_context_get_state(context
);
294 if(state
== PA_CONTEXT_READY
|| !PA_CONTEXT_IS_GOOD(state
))
295 pa_threaded_mainloop_signal(loop
, 0);
298 static void stream_state_callback(pa_stream
*stream
, void *pdata
)
300 pa_threaded_mainloop
*loop
= pdata
;
301 pa_stream_state_t state
;
303 state
= pa_stream_get_state(stream
);
304 if(state
== PA_STREAM_READY
|| !PA_STREAM_IS_GOOD(state
))
305 pa_threaded_mainloop_signal(loop
, 0);
308 static void stream_success_callback(pa_stream
*UNUSED(stream
), int UNUSED(success
), void *pdata
)
310 pa_threaded_mainloop
*loop
= pdata
;
311 pa_threaded_mainloop_signal(loop
, 0);
314 static void wait_for_operation(pa_operation
*op
, pa_threaded_mainloop
*loop
)
318 while(pa_operation_get_state(op
) == PA_OPERATION_RUNNING
)
319 pa_threaded_mainloop_wait(loop
);
320 pa_operation_unref(op
);
325 static pa_context
*connect_context(pa_threaded_mainloop
*loop
, ALboolean silent
)
327 const char *name
= "OpenAL Soft";
328 char path_name
[PATH_MAX
];
329 pa_context_state_t state
;
333 if(pa_get_binary_name(path_name
, sizeof(path_name
)))
334 name
= pa_path_get_filename(path_name
);
336 context
= pa_context_new(pa_threaded_mainloop_get_api(loop
), name
);
339 ERR("pa_context_new() failed\n");
343 pa_context_set_state_callback(context
, context_state_callback
, loop
);
345 if((err
=pa_context_connect(context
, NULL
, pulse_ctx_flags
, NULL
)) >= 0)
347 while((state
=pa_context_get_state(context
)) != PA_CONTEXT_READY
)
349 if(!PA_CONTEXT_IS_GOOD(state
))
351 err
= pa_context_errno(context
);
352 if(err
> 0) err
= -err
;
356 pa_threaded_mainloop_wait(loop
);
359 pa_context_set_state_callback(context
, NULL
, NULL
);
364 ERR("Context did not connect: %s\n", pa_strerror(err
));
365 pa_context_unref(context
);
373 static ALCboolean
pulse_open(pa_threaded_mainloop
**loop
, pa_context
**context
,
374 void(*state_cb
)(pa_context
*,void*), void *ptr
)
376 if(!(*loop
= pa_threaded_mainloop_new()))
378 ERR("pa_threaded_mainloop_new() failed!\n");
381 if(pa_threaded_mainloop_start(*loop
) < 0)
383 ERR("pa_threaded_mainloop_start() failed\n");
387 pa_threaded_mainloop_lock(*loop
);
389 *context
= connect_context(*loop
, AL_FALSE
);
392 pa_threaded_mainloop_unlock(*loop
);
393 pa_threaded_mainloop_stop(*loop
);
396 pa_context_set_state_callback(*context
, state_cb
, ptr
);
398 pa_threaded_mainloop_unlock(*loop
);
402 pa_threaded_mainloop_free(*loop
);
408 static void pulse_close(pa_threaded_mainloop
*loop
, pa_context
*context
, pa_stream
*stream
)
410 pa_threaded_mainloop_lock(loop
);
414 pa_stream_set_state_callback(stream
, NULL
, NULL
);
415 pa_stream_set_moved_callback(stream
, NULL
, NULL
);
416 pa_stream_set_write_callback(stream
, NULL
, NULL
);
417 pa_stream_set_buffer_attr_callback(stream
, NULL
, NULL
);
418 pa_stream_disconnect(stream
);
419 pa_stream_unref(stream
);
422 pa_context_disconnect(context
);
423 pa_context_unref(context
);
425 pa_threaded_mainloop_unlock(loop
);
427 pa_threaded_mainloop_stop(loop
);
428 pa_threaded_mainloop_free(loop
);
434 al_string device_name
;
436 TYPEDEF_VECTOR(DevMap
, vector_DevMap
)
438 static vector_DevMap PlaybackDevices
;
439 static vector_DevMap CaptureDevices
;
441 static void clear_devlist(vector_DevMap
*list
)
443 #define DEINIT_STRS(i) (AL_STRING_DEINIT((i)->name),AL_STRING_DEINIT((i)->device_name))
444 VECTOR_FOR_EACH(DevMap
, *list
, DEINIT_STRS
);
446 VECTOR_RESIZE(*list
, 0, 0);
450 typedef struct ALCpulsePlayback
{
451 DERIVE_FROM_TYPE(ALCbackend
);
453 al_string device_name
;
458 pa_threaded_mainloop
*loop
;
463 volatile ALboolean killNow
;
467 static void ALCpulsePlayback_deviceCallback(pa_context
*context
, const pa_sink_info
*info
, int eol
, void *pdata
);
468 static void ALCpulsePlayback_probeDevices(void);
470 static void ALCpulsePlayback_bufferAttrCallback(pa_stream
*stream
, void *pdata
);
471 static void ALCpulsePlayback_contextStateCallback(pa_context
*context
, void *pdata
);
472 static void ALCpulsePlayback_streamStateCallback(pa_stream
*stream
, void *pdata
);
473 static void ALCpulsePlayback_streamWriteCallback(pa_stream
*p
, size_t nbytes
, void *userdata
);
474 static void ALCpulsePlayback_sinkInfoCallback(pa_context
*context
, const pa_sink_info
*info
, int eol
, void *pdata
);
475 static void ALCpulsePlayback_sinkNameCallback(pa_context
*context
, const pa_sink_info
*info
, int eol
, void *pdata
);
476 static void ALCpulsePlayback_streamMovedCallback(pa_stream
*stream
, void *pdata
);
477 static pa_stream
*ALCpulsePlayback_connectStream(const char *device_name
, pa_threaded_mainloop
*loop
,
478 pa_context
*context
, pa_stream_flags_t flags
,
479 pa_buffer_attr
*attr
, pa_sample_spec
*spec
,
480 pa_channel_map
*chanmap
);
481 static int ALCpulsePlayback_mixerProc(void *ptr
);
483 static void ALCpulsePlayback_Construct(ALCpulsePlayback
*self
, ALCdevice
*device
);
484 static void ALCpulsePlayback_Destruct(ALCpulsePlayback
*self
);
485 static ALCenum
ALCpulsePlayback_open(ALCpulsePlayback
*self
, const ALCchar
*name
);
486 static void ALCpulsePlayback_close(ALCpulsePlayback
*self
);
487 static ALCboolean
ALCpulsePlayback_reset(ALCpulsePlayback
*self
);
488 static ALCboolean
ALCpulsePlayback_start(ALCpulsePlayback
*self
);
489 static void ALCpulsePlayback_stop(ALCpulsePlayback
*self
);
490 static DECLARE_FORWARD2(ALCpulsePlayback
, ALCbackend
, ALCenum
, captureSamples
, ALCvoid
*, ALCuint
)
491 static DECLARE_FORWARD(ALCpulsePlayback
, ALCbackend
, ALCuint
, availableSamples
)
492 static ClockLatency
ALCpulsePlayback_getClockLatency(ALCpulsePlayback
*self
);
493 static void ALCpulsePlayback_lock(ALCpulsePlayback
*self
);
494 static void ALCpulsePlayback_unlock(ALCpulsePlayback
*self
);
495 DECLARE_DEFAULT_ALLOCATORS(ALCpulsePlayback
)
497 DEFINE_ALCBACKEND_VTABLE(ALCpulsePlayback
);
500 static void ALCpulsePlayback_Construct(ALCpulsePlayback
*self
, ALCdevice
*device
)
502 ALCbackend_Construct(STATIC_CAST(ALCbackend
, self
), device
);
503 SET_VTABLE2(ALCpulsePlayback
, ALCbackend
, self
);
505 AL_STRING_INIT(self
->device_name
);
508 static void ALCpulsePlayback_Destruct(ALCpulsePlayback
*self
)
510 AL_STRING_DEINIT(self
->device_name
);
511 ALCbackend_Destruct(STATIC_CAST(ALCbackend
, self
));
515 static void ALCpulsePlayback_deviceCallback(pa_context
*UNUSED(context
), const pa_sink_info
*info
, int eol
, void *pdata
)
517 pa_threaded_mainloop
*loop
= pdata
;
524 pa_threaded_mainloop_signal(loop
, 0);
528 #define MATCH_INFO_NAME(iter) (al_string_cmp_cstr((iter)->device_name, info->name) == 0)
529 VECTOR_FIND_IF(iter
, const DevMap
, PlaybackDevices
, MATCH_INFO_NAME
);
530 if(iter
!= VECTOR_END(PlaybackDevices
)) return;
531 #undef MATCH_INFO_NAME
533 AL_STRING_INIT(entry
.name
);
534 AL_STRING_INIT(entry
.device_name
);
536 al_string_copy_cstr(&entry
.device_name
, info
->name
);
541 al_string_copy_cstr(&entry
.name
, info
->description
);
545 snprintf(str
, sizeof(str
), " #%d", count
+1);
546 al_string_append_cstr(&entry
.name
, str
);
549 #define MATCH_ENTRY(i) (al_string_cmp(entry.name, (i)->name) == 0)
550 VECTOR_FIND_IF(iter
, const DevMap
, PlaybackDevices
, MATCH_ENTRY
);
551 if(iter
== VECTOR_END(PlaybackDevices
)) break;
556 TRACE("Got device \"%s\", \"%s\"\n", al_string_get_cstr(entry
.name
), al_string_get_cstr(entry
.device_name
));
558 VECTOR_PUSH_BACK(PlaybackDevices
, entry
);
561 static void ALCpulsePlayback_probeDevices(void)
563 pa_threaded_mainloop
*loop
;
565 clear_devlist(&PlaybackDevices
);
567 if((loop
=pa_threaded_mainloop_new()) &&
568 pa_threaded_mainloop_start(loop
) >= 0)
572 pa_threaded_mainloop_lock(loop
);
573 context
= connect_context(loop
, AL_FALSE
);
577 pa_stream_flags_t flags
;
581 flags
= PA_STREAM_FIX_FORMAT
| PA_STREAM_FIX_RATE
|
582 PA_STREAM_FIX_CHANNELS
| PA_STREAM_DONT_MOVE
;
584 spec
.format
= PA_SAMPLE_S16NE
;
588 stream
= ALCpulsePlayback_connectStream(NULL
, loop
, context
, flags
,
592 o
= pa_context_get_sink_info_by_name(context
, pa_stream_get_device_name(stream
),
593 ALCpulsePlayback_deviceCallback
, loop
);
594 wait_for_operation(o
, loop
);
596 pa_stream_disconnect(stream
);
597 pa_stream_unref(stream
);
601 o
= pa_context_get_sink_info_list(context
, ALCpulsePlayback_deviceCallback
, loop
);
602 wait_for_operation(o
, loop
);
604 pa_context_disconnect(context
);
605 pa_context_unref(context
);
607 pa_threaded_mainloop_unlock(loop
);
608 pa_threaded_mainloop_stop(loop
);
611 pa_threaded_mainloop_free(loop
);
615 static void ALCpulsePlayback_bufferAttrCallback(pa_stream
*stream
, void *pdata
)
617 ALCpulsePlayback
*self
= pdata
;
619 self
->attr
= *pa_stream_get_buffer_attr(stream
);
620 TRACE("minreq=%d, tlength=%d, prebuf=%d\n", self
->attr
.minreq
, self
->attr
.tlength
, self
->attr
.prebuf
);
623 static void ALCpulsePlayback_contextStateCallback(pa_context
*context
, void *pdata
)
625 ALCpulsePlayback
*self
= pdata
;
626 if(pa_context_get_state(context
) == PA_CONTEXT_FAILED
)
628 ERR("Received context failure!\n");
629 aluHandleDisconnect(STATIC_CAST(ALCbackend
,self
)->mDevice
);
631 pa_threaded_mainloop_signal(self
->loop
, 0);
634 static void ALCpulsePlayback_streamStateCallback(pa_stream
*stream
, void *pdata
)
636 ALCpulsePlayback
*self
= pdata
;
637 if(pa_stream_get_state(stream
) == PA_STREAM_FAILED
)
639 ERR("Received stream failure!\n");
640 aluHandleDisconnect(STATIC_CAST(ALCbackend
,self
)->mDevice
);
642 pa_threaded_mainloop_signal(self
->loop
, 0);
645 static void ALCpulsePlayback_streamWriteCallback(pa_stream
* UNUSED(p
), size_t UNUSED(nbytes
), void *pdata
)
647 ALCpulsePlayback
*self
= pdata
;
648 pa_threaded_mainloop_signal(self
->loop
, 0);
651 static void ALCpulsePlayback_sinkInfoCallback(pa_context
*UNUSED(context
), const pa_sink_info
*info
, int eol
, void *pdata
)
653 static const struct {
654 enum DevFmtChannels chans
;
658 PA_CHANNEL_POSITION_FRONT_LEFT
, PA_CHANNEL_POSITION_FRONT_RIGHT
,
659 PA_CHANNEL_POSITION_FRONT_CENTER
, PA_CHANNEL_POSITION_LFE
,
660 PA_CHANNEL_POSITION_REAR_LEFT
, PA_CHANNEL_POSITION_REAR_RIGHT
,
661 PA_CHANNEL_POSITION_SIDE_LEFT
, PA_CHANNEL_POSITION_SIDE_RIGHT
664 PA_CHANNEL_POSITION_FRONT_LEFT
, PA_CHANNEL_POSITION_FRONT_RIGHT
,
665 PA_CHANNEL_POSITION_FRONT_CENTER
, PA_CHANNEL_POSITION_LFE
,
666 PA_CHANNEL_POSITION_REAR_CENTER
,
667 PA_CHANNEL_POSITION_SIDE_LEFT
, PA_CHANNEL_POSITION_SIDE_RIGHT
670 PA_CHANNEL_POSITION_FRONT_LEFT
, PA_CHANNEL_POSITION_FRONT_RIGHT
,
671 PA_CHANNEL_POSITION_FRONT_CENTER
, PA_CHANNEL_POSITION_LFE
,
672 PA_CHANNEL_POSITION_SIDE_LEFT
, PA_CHANNEL_POSITION_SIDE_RIGHT
674 { DevFmtX51Rear
, { 6, {
675 PA_CHANNEL_POSITION_FRONT_LEFT
, PA_CHANNEL_POSITION_FRONT_RIGHT
,
676 PA_CHANNEL_POSITION_FRONT_CENTER
, PA_CHANNEL_POSITION_LFE
,
677 PA_CHANNEL_POSITION_REAR_LEFT
, PA_CHANNEL_POSITION_REAR_RIGHT
680 PA_CHANNEL_POSITION_FRONT_LEFT
, PA_CHANNEL_POSITION_FRONT_RIGHT
,
681 PA_CHANNEL_POSITION_REAR_LEFT
, PA_CHANNEL_POSITION_REAR_RIGHT
683 { DevFmtStereo
, { 2, {
684 PA_CHANNEL_POSITION_FRONT_LEFT
, PA_CHANNEL_POSITION_FRONT_RIGHT
686 { DevFmtMono
, { 1, {PA_CHANNEL_POSITION_MONO
} } }
688 ALCpulsePlayback
*self
= pdata
;
689 ALCdevice
*device
= STATIC_CAST(ALCbackend
,self
)->mDevice
;
694 pa_threaded_mainloop_signal(self
->loop
, 0);
698 for(i
= 0;i
< COUNTOF(chanmaps
);i
++)
700 if(pa_channel_map_superset(&info
->channel_map
, &chanmaps
[i
].map
))
702 if(!(device
->Flags
&DEVICE_CHANNELS_REQUEST
))
703 device
->FmtChans
= chanmaps
[i
].chans
;
707 if(i
== COUNTOF(chanmaps
))
709 char chanmap_str
[PA_CHANNEL_MAP_SNPRINT_MAX
] = "";
710 pa_channel_map_snprint(chanmap_str
, sizeof(chanmap_str
), &info
->channel_map
);
711 WARN("Failed to find format for channel map:\n %s\n", chanmap_str
);
714 if(info
->active_port
)
715 TRACE("Active port: %s (%s)\n", info
->active_port
->name
, info
->active_port
->description
);
716 device
->IsHeadphones
= (info
->active_port
&&
717 strcmp(info
->active_port
->name
, "analog-output-headphones") == 0 &&
718 device
->FmtChans
== DevFmtStereo
);
721 static void ALCpulsePlayback_sinkNameCallback(pa_context
*UNUSED(context
), const pa_sink_info
*info
, int eol
, void *pdata
)
723 ALCpulsePlayback
*self
= pdata
;
724 ALCdevice
*device
= STATIC_CAST(ALCbackend
,self
)->mDevice
;
728 pa_threaded_mainloop_signal(self
->loop
, 0);
732 al_string_copy_cstr(&device
->DeviceName
, info
->description
);
736 static void ALCpulsePlayback_streamMovedCallback(pa_stream
*stream
, void *pdata
)
738 ALCpulsePlayback
*self
= pdata
;
740 al_string_copy_cstr(&self
->device_name
, pa_stream_get_device_name(stream
));
742 TRACE("Stream moved to %s\n", al_string_get_cstr(self
->device_name
));
746 static pa_stream
*ALCpulsePlayback_connectStream(const char *device_name
,
747 pa_threaded_mainloop
*loop
, pa_context
*context
,
748 pa_stream_flags_t flags
, pa_buffer_attr
*attr
, pa_sample_spec
*spec
,
749 pa_channel_map
*chanmap
)
751 pa_stream_state_t state
;
754 stream
= pa_stream_new_with_proplist(context
, "Playback Stream", spec
, chanmap
, prop_filter
);
757 ERR("pa_stream_new_with_proplist() failed: %s\n", pa_strerror(pa_context_errno(context
)));
761 pa_stream_set_state_callback(stream
, stream_state_callback
, loop
);
763 if(pa_stream_connect_playback(stream
, device_name
, attr
, flags
, NULL
, NULL
) < 0)
765 ERR("Stream did not connect: %s\n", pa_strerror(pa_context_errno(context
)));
766 pa_stream_unref(stream
);
770 while((state
=pa_stream_get_state(stream
)) != PA_STREAM_READY
)
772 if(!PA_STREAM_IS_GOOD(state
))
774 ERR("Stream did not get ready: %s\n", pa_strerror(pa_context_errno(context
)));
775 pa_stream_unref(stream
);
779 pa_threaded_mainloop_wait(loop
);
781 pa_stream_set_state_callback(stream
, NULL
, NULL
);
787 static int ALCpulsePlayback_mixerProc(void *ptr
)
789 ALCpulsePlayback
*self
= ptr
;
790 ALCdevice
*device
= STATIC_CAST(ALCbackend
,self
)->mDevice
;
797 althrd_setname(althrd_current(), MIXER_THREAD_NAME
);
799 pa_threaded_mainloop_lock(self
->loop
);
800 frame_size
= pa_frame_size(&self
->spec
);
801 update_size
= device
->UpdateSize
* frame_size
;
803 /* Sanitize buffer metrics, in case we actually have less than what we
805 buffer_size
= minu(update_size
*device
->NumUpdates
, self
->attr
.tlength
);
806 update_size
= minu(update_size
, buffer_size
/2);
808 len
= pa_stream_writable_size(self
->stream
) - self
->attr
.tlength
+
810 if(len
< update_size
)
812 if(pa_stream_is_corked(self
->stream
) == 1)
815 o
= pa_stream_cork(self
->stream
, 0, NULL
, NULL
);
816 if(o
) pa_operation_unref(o
);
818 pa_threaded_mainloop_wait(self
->loop
);
821 len
-= len
%update_size
;
827 pa_free_cb_t free_func
= NULL
;
829 if(pa_stream_begin_write(self
->stream
, &buf
, &newlen
) < 0)
831 buf
= pa_xmalloc(newlen
);
832 free_func
= pa_xfree
;
835 aluMixData(device
, buf
, newlen
/frame_size
);
837 pa_stream_write(self
->stream
, buf
, newlen
, free_func
, 0, PA_SEEK_RELATIVE
);
840 } while(!self
->killNow
&& device
->Connected
);
841 pa_threaded_mainloop_unlock(self
->loop
);
847 static ALCenum
ALCpulsePlayback_open(ALCpulsePlayback
*self
, const ALCchar
*name
)
849 const_al_string dev_name
= AL_STRING_INIT_STATIC();
850 const char *pulse_name
= NULL
;
851 pa_stream_flags_t flags
;
858 if(VECTOR_SIZE(PlaybackDevices
) == 0)
859 ALCpulsePlayback_probeDevices();
861 #define MATCH_NAME(iter) (al_string_cmp_cstr((iter)->name, name) == 0)
862 VECTOR_FIND_IF(iter
, const DevMap
, PlaybackDevices
, MATCH_NAME
);
864 if(iter
== VECTOR_END(PlaybackDevices
))
865 return ALC_INVALID_VALUE
;
866 pulse_name
= al_string_get_cstr(iter
->device_name
);
867 dev_name
= iter
->name
;
870 if(!pulse_open(&self
->loop
, &self
->context
, ALCpulsePlayback_contextStateCallback
, self
))
871 return ALC_INVALID_VALUE
;
873 pa_threaded_mainloop_lock(self
->loop
);
875 flags
= PA_STREAM_FIX_FORMAT
| PA_STREAM_FIX_RATE
|
876 PA_STREAM_FIX_CHANNELS
;
877 if(!GetConfigValueBool(NULL
, "pulse", "allow-moves", 0))
878 flags
|= PA_STREAM_DONT_MOVE
;
880 spec
.format
= PA_SAMPLE_S16NE
;
884 TRACE("Connecting to \"%s\"\n", pulse_name
? pulse_name
: "(default)");
885 self
->stream
= ALCpulsePlayback_connectStream(pulse_name
, self
->loop
, self
->context
,
886 flags
, NULL
, &spec
, NULL
);
889 pa_threaded_mainloop_unlock(self
->loop
);
890 pulse_close(self
->loop
, self
->context
, self
->stream
);
892 self
->context
= NULL
;
893 return ALC_INVALID_VALUE
;
895 pa_stream_set_moved_callback(self
->stream
, ALCpulsePlayback_streamMovedCallback
, self
);
897 al_string_copy_cstr(&self
->device_name
, pa_stream_get_device_name(self
->stream
));
898 if(al_string_empty(dev_name
))
900 pa_operation
*o
= pa_context_get_sink_info_by_name(
901 self
->context
, al_string_get_cstr(self
->device_name
),
902 ALCpulsePlayback_sinkNameCallback
, self
904 wait_for_operation(o
, self
->loop
);
908 ALCdevice
*device
= STATIC_CAST(ALCbackend
,self
)->mDevice
;
909 al_string_copy(&device
->DeviceName
, dev_name
);
912 pa_threaded_mainloop_unlock(self
->loop
);
917 static void ALCpulsePlayback_close(ALCpulsePlayback
*self
)
919 pulse_close(self
->loop
, self
->context
, self
->stream
);
921 self
->context
= NULL
;
924 al_string_clear(&self
->device_name
);
927 static ALCboolean
ALCpulsePlayback_reset(ALCpulsePlayback
*self
)
929 ALCdevice
*device
= STATIC_CAST(ALCbackend
,self
)->mDevice
;
930 pa_stream_flags_t flags
= 0;
931 const char *mapname
= NULL
;
932 pa_channel_map chanmap
;
936 pa_threaded_mainloop_lock(self
->loop
);
940 pa_stream_set_state_callback(self
->stream
, NULL
, NULL
);
941 pa_stream_set_moved_callback(self
->stream
, NULL
, NULL
);
942 pa_stream_set_write_callback(self
->stream
, NULL
, NULL
);
943 pa_stream_set_buffer_attr_callback(self
->stream
, NULL
, NULL
);
944 pa_stream_disconnect(self
->stream
);
945 pa_stream_unref(self
->stream
);
949 o
= pa_context_get_sink_info_by_name(self
->context
, al_string_get_cstr(self
->device_name
),
950 ALCpulsePlayback_sinkInfoCallback
, self
);
951 wait_for_operation(o
, self
->loop
);
953 if(GetConfigValueBool(al_string_get_cstr(device
->DeviceName
), "pulse", "fix-rate", 0) ||
954 !(device
->Flags
&DEVICE_FREQUENCY_REQUEST
))
955 flags
|= PA_STREAM_FIX_RATE
;
956 flags
|= PA_STREAM_INTERPOLATE_TIMING
| PA_STREAM_AUTO_TIMING_UPDATE
;
957 flags
|= PA_STREAM_ADJUST_LATENCY
;
958 flags
|= PA_STREAM_START_CORKED
;
959 if(!GetConfigValueBool(NULL
, "pulse", "allow-moves", 0))
960 flags
|= PA_STREAM_DONT_MOVE
;
962 switch(device
->FmtType
)
965 device
->FmtType
= DevFmtUByte
;
968 self
->spec
.format
= PA_SAMPLE_U8
;
971 device
->FmtType
= DevFmtShort
;
974 self
->spec
.format
= PA_SAMPLE_S16NE
;
977 device
->FmtType
= DevFmtInt
;
980 self
->spec
.format
= PA_SAMPLE_S32NE
;
983 self
->spec
.format
= PA_SAMPLE_FLOAT32NE
;
986 self
->spec
.rate
= device
->Frequency
;
987 self
->spec
.channels
= ChannelsFromDevFmt(device
->FmtChans
);
989 if(pa_sample_spec_valid(&self
->spec
) == 0)
991 ERR("Invalid sample format\n");
992 pa_threaded_mainloop_unlock(self
->loop
);
996 switch(device
->FmtChans
)
1004 device
->FmtChans
= DevFmtStereo
;
1007 mapname
= "front-left,front-right";
1010 mapname
= "front-left,front-right,rear-left,rear-right";
1013 mapname
= "front-left,front-right,front-center,lfe,side-left,side-right";
1016 mapname
= "front-left,front-right,front-center,lfe,rear-left,rear-right";
1019 mapname
= "front-left,front-right,front-center,lfe,rear-center,side-left,side-right";
1022 mapname
= "front-left,front-right,front-center,lfe,rear-left,rear-right,side-left,side-right";
1025 if(!pa_channel_map_parse(&chanmap
, mapname
))
1027 ERR("Failed to build channel map for %s\n", DevFmtChannelsString(device
->FmtChans
));
1028 pa_threaded_mainloop_unlock(self
->loop
);
1031 SetDefaultWFXChannelOrder(device
);
1033 self
->attr
.fragsize
= -1;
1034 self
->attr
.prebuf
= 0;
1035 self
->attr
.minreq
= device
->UpdateSize
* pa_frame_size(&self
->spec
);
1036 self
->attr
.tlength
= self
->attr
.minreq
* maxu(device
->NumUpdates
, 2);
1037 self
->attr
.maxlength
= -1;
1039 self
->stream
= ALCpulsePlayback_connectStream(al_string_get_cstr(self
->device_name
),
1040 self
->loop
, self
->context
, flags
,
1041 &self
->attr
, &self
->spec
, &chanmap
);
1044 pa_threaded_mainloop_unlock(self
->loop
);
1047 pa_stream_set_state_callback(self
->stream
, ALCpulsePlayback_streamStateCallback
, self
);
1048 pa_stream_set_moved_callback(self
->stream
, ALCpulsePlayback_streamMovedCallback
, self
);
1049 pa_stream_set_write_callback(self
->stream
, ALCpulsePlayback_streamWriteCallback
, self
);
1051 self
->spec
= *(pa_stream_get_sample_spec(self
->stream
));
1052 if(device
->Frequency
!= self
->spec
.rate
)
1054 /* Server updated our playback rate, so modify the buffer attribs
1056 device
->NumUpdates
= (ALuint
)clampd(
1057 (ALdouble
)device
->NumUpdates
/device
->Frequency
*self
->spec
.rate
+ 0.5, 2.0, 16.0
1060 self
->attr
.minreq
= device
->UpdateSize
* pa_frame_size(&self
->spec
);
1061 self
->attr
.tlength
= self
->attr
.minreq
* device
->NumUpdates
;
1062 self
->attr
.maxlength
= -1;
1063 self
->attr
.prebuf
= 0;
1065 o
= pa_stream_set_buffer_attr(self
->stream
, &self
->attr
,
1066 stream_success_callback
, self
->loop
);
1067 wait_for_operation(o
, self
->loop
);
1069 device
->Frequency
= self
->spec
.rate
;
1072 pa_stream_set_buffer_attr_callback(self
->stream
, ALCpulsePlayback_bufferAttrCallback
, self
);
1073 ALCpulsePlayback_bufferAttrCallback(self
->stream
, self
);
1075 len
= self
->attr
.minreq
/ pa_frame_size(&self
->spec
);
1076 device
->NumUpdates
= (ALuint
)clampd(
1077 (ALdouble
)device
->NumUpdates
/len
*device
->UpdateSize
+ 0.5, 2.0, 16.0
1079 device
->UpdateSize
= len
;
1081 /* HACK: prebuf should be 0 as that's what we set it to. However on some
1082 * systems it comes back as non-0, so we have to make sure the device will
1083 * write enough audio to start playback. The lack of manual start control
1084 * may have unintended consequences, but it's better than not starting at
1087 if(self
->attr
.prebuf
!= 0)
1089 len
= self
->attr
.prebuf
/ pa_frame_size(&self
->spec
);
1090 if(len
<= device
->UpdateSize
*device
->NumUpdates
)
1091 ERR("Non-0 prebuf, %u samples (%u bytes), device has %u samples\n",
1092 len
, self
->attr
.prebuf
, device
->UpdateSize
*device
->NumUpdates
);
1095 ERR("Large prebuf, %u samples (%u bytes), increasing device from %u samples",
1096 len
, self
->attr
.prebuf
, device
->UpdateSize
*device
->NumUpdates
);
1097 device
->NumUpdates
= (len
+device
->UpdateSize
-1) / device
->UpdateSize
;
1101 pa_threaded_mainloop_unlock(self
->loop
);
1105 static ALCboolean
ALCpulsePlayback_start(ALCpulsePlayback
*self
)
1107 self
->killNow
= AL_FALSE
;
1108 if(althrd_create(&self
->thread
, ALCpulsePlayback_mixerProc
, self
) != althrd_success
)
1113 static void ALCpulsePlayback_stop(ALCpulsePlayback
*self
)
1118 if(!self
->stream
|| self
->killNow
)
1121 self
->killNow
= AL_TRUE
;
1122 /* Signal the main loop in case PulseAudio isn't sending us audio requests
1123 * (e.g. if the device is suspended). We need to lock the mainloop in case
1124 * the mixer is between checking the killNow flag but before waiting for
1127 pa_threaded_mainloop_lock(self
->loop
);
1128 pa_threaded_mainloop_unlock(self
->loop
);
1129 pa_threaded_mainloop_signal(self
->loop
, 0);
1130 althrd_join(self
->thread
, &res
);
1132 pa_threaded_mainloop_lock(self
->loop
);
1134 o
= pa_stream_cork(self
->stream
, 1, stream_success_callback
, self
->loop
);
1135 wait_for_operation(o
, self
->loop
);
1137 pa_threaded_mainloop_unlock(self
->loop
);
1141 static ClockLatency
ALCpulsePlayback_getClockLatency(ALCpulsePlayback
*self
)
1143 pa_usec_t latency
= 0;
1147 pa_threaded_mainloop_lock(self
->loop
);
1148 ret
.ClockTime
= GetDeviceClockTime(STATIC_CAST(ALCbackend
,self
)->mDevice
);
1149 if((err
=pa_stream_get_latency(self
->stream
, &latency
, &neg
)) != 0)
1151 /* FIXME: if err = -PA_ERR_NODATA, it means we were called too soon
1152 * after starting the stream and no timing info has been received from
1153 * the server yet. Should we wait, possibly stalling the app, or give a
1154 * dummy value? Either way, it shouldn't be 0. */
1155 if(err
!= -PA_ERR_NODATA
)
1156 ERR("Failed to get stream latency: 0x%x\n", err
);
1160 if(neg
) latency
= 0;
1161 ret
.Latency
= minu64(latency
, U64(0xffffffffffffffff)/1000) * 1000;
1162 pa_threaded_mainloop_unlock(self
->loop
);
1168 static void ALCpulsePlayback_lock(ALCpulsePlayback
*self
)
1170 pa_threaded_mainloop_lock(self
->loop
);
1173 static void ALCpulsePlayback_unlock(ALCpulsePlayback
*self
)
1175 pa_threaded_mainloop_unlock(self
->loop
);
1179 typedef struct ALCpulseCapture
{
1180 DERIVE_FROM_TYPE(ALCbackend
);
1182 al_string device_name
;
1184 const void *cap_store
;
1188 ALCuint last_readable
;
1190 pa_buffer_attr attr
;
1191 pa_sample_spec spec
;
1193 pa_threaded_mainloop
*loop
;
1196 pa_context
*context
;
1199 static void ALCpulseCapture_deviceCallback(pa_context
*context
, const pa_source_info
*info
, int eol
, void *pdata
);
1200 static void ALCpulseCapture_probeDevices(void);
1202 static void ALCpulseCapture_contextStateCallback(pa_context
*context
, void *pdata
);
1203 static void ALCpulseCapture_streamStateCallback(pa_stream
*stream
, void *pdata
);
1204 static void ALCpulseCapture_sourceNameCallback(pa_context
*context
, const pa_source_info
*info
, int eol
, void *pdata
);
1205 static void ALCpulseCapture_streamMovedCallback(pa_stream
*stream
, void *pdata
);
1206 static pa_stream
*ALCpulseCapture_connectStream(const char *device_name
,
1207 pa_threaded_mainloop
*loop
, pa_context
*context
,
1208 pa_stream_flags_t flags
, pa_buffer_attr
*attr
,
1209 pa_sample_spec
*spec
, pa_channel_map
*chanmap
);
1211 static void ALCpulseCapture_Construct(ALCpulseCapture
*self
, ALCdevice
*device
);
1212 static void ALCpulseCapture_Destruct(ALCpulseCapture
*self
);
1213 static ALCenum
ALCpulseCapture_open(ALCpulseCapture
*self
, const ALCchar
*name
);
1214 static void ALCpulseCapture_close(ALCpulseCapture
*self
);
1215 static DECLARE_FORWARD(ALCpulseCapture
, ALCbackend
, ALCboolean
, reset
)
1216 static ALCboolean
ALCpulseCapture_start(ALCpulseCapture
*self
);
1217 static void ALCpulseCapture_stop(ALCpulseCapture
*self
);
1218 static ALCenum
ALCpulseCapture_captureSamples(ALCpulseCapture
*self
, ALCvoid
*buffer
, ALCuint samples
);
1219 static ALCuint
ALCpulseCapture_availableSamples(ALCpulseCapture
*self
);
1220 static ClockLatency
ALCpulseCapture_getClockLatency(ALCpulseCapture
*self
);
1221 static void ALCpulseCapture_lock(ALCpulseCapture
*self
);
1222 static void ALCpulseCapture_unlock(ALCpulseCapture
*self
);
1223 DECLARE_DEFAULT_ALLOCATORS(ALCpulseCapture
)
1225 DEFINE_ALCBACKEND_VTABLE(ALCpulseCapture
);
1228 static void ALCpulseCapture_Construct(ALCpulseCapture
*self
, ALCdevice
*device
)
1230 ALCbackend_Construct(STATIC_CAST(ALCbackend
, self
), device
);
1231 SET_VTABLE2(ALCpulseCapture
, ALCbackend
, self
);
1233 AL_STRING_INIT(self
->device_name
);
1236 static void ALCpulseCapture_Destruct(ALCpulseCapture
*self
)
1238 AL_STRING_DEINIT(self
->device_name
);
1239 ALCbackend_Destruct(STATIC_CAST(ALCbackend
, self
));
1243 static void ALCpulseCapture_deviceCallback(pa_context
*UNUSED(context
), const pa_source_info
*info
, int eol
, void *pdata
)
1245 pa_threaded_mainloop
*loop
= pdata
;
1252 pa_threaded_mainloop_signal(loop
, 0);
1256 #define MATCH_INFO_NAME(iter) (al_string_cmp_cstr((iter)->device_name, info->name) == 0)
1257 VECTOR_FIND_IF(iter
, const DevMap
, CaptureDevices
, MATCH_INFO_NAME
);
1258 if(iter
!= VECTOR_END(CaptureDevices
)) return;
1259 #undef MATCH_INFO_NAME
1261 AL_STRING_INIT(entry
.name
);
1262 AL_STRING_INIT(entry
.device_name
);
1264 al_string_copy_cstr(&entry
.device_name
, info
->name
);
1269 al_string_copy_cstr(&entry
.name
, info
->description
);
1273 snprintf(str
, sizeof(str
), " #%d", count
+1);
1274 al_string_append_cstr(&entry
.name
, str
);
1277 #define MATCH_ENTRY(i) (al_string_cmp(entry.name, (i)->name) == 0)
1278 VECTOR_FIND_IF(iter
, const DevMap
, CaptureDevices
, MATCH_ENTRY
);
1279 if(iter
== VECTOR_END(CaptureDevices
)) break;
1284 TRACE("Got device \"%s\", \"%s\"\n", al_string_get_cstr(entry
.name
), al_string_get_cstr(entry
.device_name
));
1286 VECTOR_PUSH_BACK(CaptureDevices
, entry
);
1289 static void ALCpulseCapture_probeDevices(void)
1291 pa_threaded_mainloop
*loop
;
1293 clear_devlist(&CaptureDevices
);
1295 if((loop
=pa_threaded_mainloop_new()) &&
1296 pa_threaded_mainloop_start(loop
) >= 0)
1298 pa_context
*context
;
1300 pa_threaded_mainloop_lock(loop
);
1301 context
= connect_context(loop
, AL_FALSE
);
1305 pa_stream_flags_t flags
;
1306 pa_sample_spec spec
;
1309 flags
= PA_STREAM_FIX_FORMAT
| PA_STREAM_FIX_RATE
|
1310 PA_STREAM_FIX_CHANNELS
| PA_STREAM_DONT_MOVE
;
1312 spec
.format
= PA_SAMPLE_S16NE
;
1316 stream
= ALCpulseCapture_connectStream(NULL
, loop
, context
, flags
,
1320 o
= pa_context_get_source_info_by_name(context
, pa_stream_get_device_name(stream
),
1321 ALCpulseCapture_deviceCallback
, loop
);
1322 wait_for_operation(o
, loop
);
1324 pa_stream_disconnect(stream
);
1325 pa_stream_unref(stream
);
1329 o
= pa_context_get_source_info_list(context
, ALCpulseCapture_deviceCallback
, loop
);
1330 wait_for_operation(o
, loop
);
1332 pa_context_disconnect(context
);
1333 pa_context_unref(context
);
1335 pa_threaded_mainloop_unlock(loop
);
1336 pa_threaded_mainloop_stop(loop
);
1339 pa_threaded_mainloop_free(loop
);
1343 static void ALCpulseCapture_contextStateCallback(pa_context
*context
, void *pdata
)
1345 ALCpulseCapture
*self
= pdata
;
1346 if(pa_context_get_state(context
) == PA_CONTEXT_FAILED
)
1348 ERR("Received context failure!\n");
1349 aluHandleDisconnect(STATIC_CAST(ALCbackend
,self
)->mDevice
);
1351 pa_threaded_mainloop_signal(self
->loop
, 0);
1354 static void ALCpulseCapture_streamStateCallback(pa_stream
*stream
, void *pdata
)
1356 ALCpulseCapture
*self
= pdata
;
1357 if(pa_stream_get_state(stream
) == PA_STREAM_FAILED
)
1359 ERR("Received stream failure!\n");
1360 aluHandleDisconnect(STATIC_CAST(ALCbackend
,self
)->mDevice
);
1362 pa_threaded_mainloop_signal(self
->loop
, 0);
1366 static void ALCpulseCapture_sourceNameCallback(pa_context
*UNUSED(context
), const pa_source_info
*info
, int eol
, void *pdata
)
1368 ALCpulseCapture
*self
= pdata
;
1369 ALCdevice
*device
= STATIC_CAST(ALCbackend
,self
)->mDevice
;
1373 pa_threaded_mainloop_signal(self
->loop
, 0);
1377 al_string_copy_cstr(&device
->DeviceName
, info
->description
);
1381 static void ALCpulseCapture_streamMovedCallback(pa_stream
*stream
, void *pdata
)
1383 ALCpulseCapture
*self
= pdata
;
1385 al_string_copy_cstr(&self
->device_name
, pa_stream_get_device_name(stream
));
1387 TRACE("Stream moved to %s\n", al_string_get_cstr(self
->device_name
));
1391 static pa_stream
*ALCpulseCapture_connectStream(const char *device_name
,
1392 pa_threaded_mainloop
*loop
, pa_context
*context
,
1393 pa_stream_flags_t flags
, pa_buffer_attr
*attr
, pa_sample_spec
*spec
,
1394 pa_channel_map
*chanmap
)
1396 pa_stream_state_t state
;
1399 stream
= pa_stream_new_with_proplist(context
, "Capture Stream", spec
, chanmap
, prop_filter
);
1402 ERR("pa_stream_new_with_proplist() failed: %s\n", pa_strerror(pa_context_errno(context
)));
1406 pa_stream_set_state_callback(stream
, stream_state_callback
, loop
);
1408 if(pa_stream_connect_record(stream
, device_name
, attr
, flags
) < 0)
1410 ERR("Stream did not connect: %s\n", pa_strerror(pa_context_errno(context
)));
1411 pa_stream_unref(stream
);
1415 while((state
=pa_stream_get_state(stream
)) != PA_STREAM_READY
)
1417 if(!PA_STREAM_IS_GOOD(state
))
1419 ERR("Stream did not get ready: %s\n", pa_strerror(pa_context_errno(context
)));
1420 pa_stream_unref(stream
);
1424 pa_threaded_mainloop_wait(loop
);
1426 pa_stream_set_state_callback(stream
, NULL
, NULL
);
1432 static ALCenum
ALCpulseCapture_open(ALCpulseCapture
*self
, const ALCchar
*name
)
1434 ALCdevice
*device
= STATIC_CAST(ALCbackend
,self
)->mDevice
;
1435 const char *pulse_name
= NULL
;
1436 pa_stream_flags_t flags
= 0;
1437 pa_channel_map chanmap
;
1444 if(VECTOR_SIZE(CaptureDevices
) == 0)
1445 ALCpulseCapture_probeDevices();
1447 #define MATCH_NAME(iter) (al_string_cmp_cstr((iter)->name, name) == 0)
1448 VECTOR_FIND_IF(iter
, const DevMap
, CaptureDevices
, MATCH_NAME
);
1450 if(iter
== VECTOR_END(CaptureDevices
))
1451 return ALC_INVALID_VALUE
;
1452 pulse_name
= al_string_get_cstr(iter
->device_name
);
1453 al_string_copy(&device
->DeviceName
, iter
->name
);
1456 if(!pulse_open(&self
->loop
, &self
->context
, ALCpulseCapture_contextStateCallback
, self
))
1457 return ALC_INVALID_VALUE
;
1459 pa_threaded_mainloop_lock(self
->loop
);
1461 self
->spec
.rate
= device
->Frequency
;
1462 self
->spec
.channels
= ChannelsFromDevFmt(device
->FmtChans
);
1464 switch(device
->FmtType
)
1467 self
->spec
.format
= PA_SAMPLE_U8
;
1470 self
->spec
.format
= PA_SAMPLE_S16NE
;
1473 self
->spec
.format
= PA_SAMPLE_S32NE
;
1476 self
->spec
.format
= PA_SAMPLE_FLOAT32NE
;
1481 ERR("%s capture samples not supported\n", DevFmtTypeString(device
->FmtType
));
1482 pa_threaded_mainloop_unlock(self
->loop
);
1486 if(pa_sample_spec_valid(&self
->spec
) == 0)
1488 ERR("Invalid sample format\n");
1489 pa_threaded_mainloop_unlock(self
->loop
);
1493 if(!pa_channel_map_init_auto(&chanmap
, self
->spec
.channels
, PA_CHANNEL_MAP_WAVEEX
))
1495 ERR("Couldn't build map for channel count (%d)!\n", self
->spec
.channels
);
1496 pa_threaded_mainloop_unlock(self
->loop
);
1500 samples
= device
->UpdateSize
* device
->NumUpdates
;
1501 samples
= maxu(samples
, 100 * device
->Frequency
/ 1000);
1503 self
->attr
.minreq
= -1;
1504 self
->attr
.prebuf
= -1;
1505 self
->attr
.maxlength
= samples
* pa_frame_size(&self
->spec
);
1506 self
->attr
.tlength
= -1;
1507 self
->attr
.fragsize
= minu(samples
, 50*device
->Frequency
/1000) *
1508 pa_frame_size(&self
->spec
);
1510 flags
|= PA_STREAM_START_CORKED
|PA_STREAM_ADJUST_LATENCY
;
1511 if(!GetConfigValueBool(NULL
, "pulse", "allow-moves", 0))
1512 flags
|= PA_STREAM_DONT_MOVE
;
1514 TRACE("Connecting to \"%s\"\n", pulse_name
? pulse_name
: "(default)");
1515 self
->stream
= ALCpulseCapture_connectStream(pulse_name
, self
->loop
, self
->context
,
1516 flags
, &self
->attr
, &self
->spec
,
1520 pa_threaded_mainloop_unlock(self
->loop
);
1523 pa_stream_set_moved_callback(self
->stream
, ALCpulseCapture_streamMovedCallback
, self
);
1524 pa_stream_set_state_callback(self
->stream
, ALCpulseCapture_streamStateCallback
, self
);
1526 al_string_copy_cstr(&self
->device_name
, pa_stream_get_device_name(self
->stream
));
1527 if(al_string_empty(device
->DeviceName
))
1529 pa_operation
*o
= pa_context_get_source_info_by_name(
1530 self
->context
, al_string_get_cstr(self
->device_name
),
1531 ALCpulseCapture_sourceNameCallback
, self
1533 wait_for_operation(o
, self
->loop
);
1536 pa_threaded_mainloop_unlock(self
->loop
);
1537 return ALC_NO_ERROR
;
1540 pulse_close(self
->loop
, self
->context
, self
->stream
);
1542 self
->context
= NULL
;
1543 self
->stream
= NULL
;
1545 return ALC_INVALID_VALUE
;
1548 static void ALCpulseCapture_close(ALCpulseCapture
*self
)
1550 pulse_close(self
->loop
, self
->context
, self
->stream
);
1552 self
->context
= NULL
;
1553 self
->stream
= NULL
;
1555 al_string_clear(&self
->device_name
);
1558 static ALCboolean
ALCpulseCapture_start(ALCpulseCapture
*self
)
1561 o
= pa_stream_cork(self
->stream
, 0, stream_success_callback
, self
->loop
);
1562 wait_for_operation(o
, self
->loop
);
1567 static void ALCpulseCapture_stop(ALCpulseCapture
*self
)
1570 o
= pa_stream_cork(self
->stream
, 1, stream_success_callback
, self
->loop
);
1571 wait_for_operation(o
, self
->loop
);
1574 static ALCenum
ALCpulseCapture_captureSamples(ALCpulseCapture
*self
, ALCvoid
*buffer
, ALCuint samples
)
1576 ALCdevice
*device
= STATIC_CAST(ALCbackend
,self
)->mDevice
;
1577 ALCuint todo
= samples
* pa_frame_size(&self
->spec
);
1579 /* Capture is done in fragment-sized chunks, so we loop until we get all
1580 * that's available */
1581 self
->last_readable
-= todo
;
1586 if(self
->cap_len
== 0)
1588 pa_stream_state_t state
;
1590 state
= pa_stream_get_state(self
->stream
);
1591 if(!PA_STREAM_IS_GOOD(state
))
1593 aluHandleDisconnect(device
);
1596 if(pa_stream_peek(self
->stream
, &self
->cap_store
, &self
->cap_len
) < 0)
1598 ERR("pa_stream_peek() failed: %s\n",
1599 pa_strerror(pa_context_errno(self
->context
)));
1600 aluHandleDisconnect(device
);
1603 self
->cap_remain
= self
->cap_len
;
1605 if(rem
> self
->cap_remain
)
1606 rem
= self
->cap_remain
;
1608 memcpy(buffer
, self
->cap_store
, rem
);
1610 buffer
= (ALbyte
*)buffer
+ rem
;
1613 self
->cap_store
= (ALbyte
*)self
->cap_store
+ rem
;
1614 self
->cap_remain
-= rem
;
1615 if(self
->cap_remain
== 0)
1617 pa_stream_drop(self
->stream
);
1622 memset(buffer
, ((device
->FmtType
==DevFmtUByte
) ? 0x80 : 0), todo
);
1624 return ALC_NO_ERROR
;
1627 static ALCuint
ALCpulseCapture_availableSamples(ALCpulseCapture
*self
)
1629 ALCdevice
*device
= STATIC_CAST(ALCbackend
,self
)->mDevice
;
1630 size_t readable
= self
->cap_remain
;
1632 if(device
->Connected
)
1634 ssize_t got
= pa_stream_readable_size(self
->stream
);
1637 ERR("pa_stream_readable_size() failed: %s\n", pa_strerror(got
));
1638 aluHandleDisconnect(device
);
1640 else if((size_t)got
> self
->cap_len
)
1641 readable
+= got
- self
->cap_len
;
1644 if(self
->last_readable
< readable
)
1645 self
->last_readable
= readable
;
1646 return self
->last_readable
/ pa_frame_size(&self
->spec
);
1650 static ClockLatency
ALCpulseCapture_getClockLatency(ALCpulseCapture
*self
)
1652 pa_usec_t latency
= 0;
1656 pa_threaded_mainloop_lock(self
->loop
);
1657 ret
.ClockTime
= GetDeviceClockTime(STATIC_CAST(ALCbackend
,self
)->mDevice
);
1658 if((err
=pa_stream_get_latency(self
->stream
, &latency
, &neg
)) != 0)
1660 ERR("Failed to get stream latency: 0x%x\n", err
);
1664 if(neg
) latency
= 0;
1665 ret
.Latency
= minu64(latency
, U64(0xffffffffffffffff)/1000) * 1000;
1666 pa_threaded_mainloop_unlock(self
->loop
);
1672 static void ALCpulseCapture_lock(ALCpulseCapture
*self
)
1674 pa_threaded_mainloop_lock(self
->loop
);
1677 static void ALCpulseCapture_unlock(ALCpulseCapture
*self
)
1679 pa_threaded_mainloop_unlock(self
->loop
);
1683 typedef struct ALCpulseBackendFactory
{
1684 DERIVE_FROM_TYPE(ALCbackendFactory
);
1685 } ALCpulseBackendFactory
;
1686 #define ALCPULSEBACKENDFACTORY_INITIALIZER { { GET_VTABLE2(ALCpulseBackendFactory, ALCbackendFactory) } }
1688 static ALCboolean
ALCpulseBackendFactory_init(ALCpulseBackendFactory
*self
);
1689 static void ALCpulseBackendFactory_deinit(ALCpulseBackendFactory
*self
);
1690 static ALCboolean
ALCpulseBackendFactory_querySupport(ALCpulseBackendFactory
*self
, ALCbackend_Type type
);
1691 static void ALCpulseBackendFactory_probe(ALCpulseBackendFactory
*self
, enum DevProbe type
);
1692 static ALCbackend
* ALCpulseBackendFactory_createBackend(ALCpulseBackendFactory
*self
, ALCdevice
*device
, ALCbackend_Type type
);
1694 DEFINE_ALCBACKENDFACTORY_VTABLE(ALCpulseBackendFactory
);
1697 static ALCboolean
ALCpulseBackendFactory_init(ALCpulseBackendFactory
* UNUSED(self
))
1699 ALCboolean ret
= ALC_FALSE
;
1701 VECTOR_INIT(PlaybackDevices
);
1702 VECTOR_INIT(CaptureDevices
);
1706 pa_threaded_mainloop
*loop
;
1708 pulse_ctx_flags
= 0;
1709 if(!GetConfigValueBool(NULL
, "pulse", "spawn-server", 1))
1710 pulse_ctx_flags
|= PA_CONTEXT_NOAUTOSPAWN
;
1712 if((loop
=pa_threaded_mainloop_new()) &&
1713 pa_threaded_mainloop_start(loop
) >= 0)
1715 pa_context
*context
;
1717 pa_threaded_mainloop_lock(loop
);
1718 context
= connect_context(loop
, AL_TRUE
);
1723 /* Some libraries (Phonon, Qt) set some pulseaudio properties
1724 * through environment variables, which causes all streams in
1725 * the process to inherit them. This attempts to filter those
1726 * properties out by setting them to 0-length data. */
1727 prop_filter
= pa_proplist_new();
1728 pa_proplist_set(prop_filter
, PA_PROP_MEDIA_ROLE
, NULL
, 0);
1729 pa_proplist_set(prop_filter
, "phonon.streamid", NULL
, 0);
1731 pa_context_disconnect(context
);
1732 pa_context_unref(context
);
1734 pa_threaded_mainloop_unlock(loop
);
1735 pa_threaded_mainloop_stop(loop
);
1738 pa_threaded_mainloop_free(loop
);
1744 static void ALCpulseBackendFactory_deinit(ALCpulseBackendFactory
* UNUSED(self
))
1746 clear_devlist(&PlaybackDevices
);
1747 VECTOR_DEINIT(PlaybackDevices
);
1749 clear_devlist(&CaptureDevices
);
1750 VECTOR_DEINIT(CaptureDevices
);
1753 pa_proplist_free(prop_filter
);
1756 /* PulseAudio doesn't like being CloseLib'd sometimes */
1759 static ALCboolean
ALCpulseBackendFactory_querySupport(ALCpulseBackendFactory
* UNUSED(self
), ALCbackend_Type type
)
1761 if(type
== ALCbackend_Playback
|| type
== ALCbackend_Capture
)
1766 static void ALCpulseBackendFactory_probe(ALCpulseBackendFactory
* UNUSED(self
), enum DevProbe type
)
1770 case ALL_DEVICE_PROBE
:
1771 ALCpulsePlayback_probeDevices();
1772 #define APPEND_ALL_DEVICES_LIST(e) AppendAllDevicesList(al_string_get_cstr((e)->name))
1773 VECTOR_FOR_EACH(const DevMap
, PlaybackDevices
, APPEND_ALL_DEVICES_LIST
);
1774 #undef APPEND_ALL_DEVICES_LIST
1777 case CAPTURE_DEVICE_PROBE
:
1778 ALCpulseCapture_probeDevices();
1779 #define APPEND_CAPTURE_DEVICE_LIST(e) AppendCaptureDeviceList(al_string_get_cstr((e)->name))
1780 VECTOR_FOR_EACH(const DevMap
, CaptureDevices
, APPEND_CAPTURE_DEVICE_LIST
);
1781 #undef APPEND_CAPTURE_DEVICE_LIST
1786 static ALCbackend
* ALCpulseBackendFactory_createBackend(ALCpulseBackendFactory
* UNUSED(self
), ALCdevice
*device
, ALCbackend_Type type
)
1788 if(type
== ALCbackend_Playback
)
1790 ALCpulsePlayback
*backend
;
1791 NEW_OBJ(backend
, ALCpulsePlayback
)(device
);
1792 if(!backend
) return NULL
;
1793 return STATIC_CAST(ALCbackend
, backend
);
1795 if(type
== ALCbackend_Capture
)
1797 ALCpulseCapture
*backend
;
1798 NEW_OBJ(backend
, ALCpulseCapture
)(device
);
1799 if(!backend
) return NULL
;
1800 return STATIC_CAST(ALCbackend
, backend
);
1807 #else /* PA_API_VERSION == 12 */
1809 #warning "Unsupported API version, backend will be unavailable!"
1811 typedef struct ALCpulseBackendFactory
{
1812 DERIVE_FROM_TYPE(ALCbackendFactory
);
1813 } ALCpulseBackendFactory
;
1814 #define ALCPULSEBACKENDFACTORY_INITIALIZER { { GET_VTABLE2(ALCpulseBackendFactory, ALCbackendFactory) } }
1816 static ALCboolean
ALCpulseBackendFactory_init(ALCpulseBackendFactory
* UNUSED(self
))
1821 static void ALCpulseBackendFactory_deinit(ALCpulseBackendFactory
* UNUSED(self
))
1825 static ALCboolean
ALCpulseBackendFactory_querySupport(ALCpulseBackendFactory
* UNUSED(self
), ALCbackend_Type
UNUSED(type
))
1830 static void ALCpulseBackendFactory_probe(ALCpulseBackendFactory
* UNUSED(self
), enum DevProbe
UNUSED(type
))
1834 static ALCbackend
* ALCpulseBackendFactory_createBackend(ALCpulseBackendFactory
* UNUSED(self
), ALCdevice
* UNUSED(device
), ALCbackend_Type
UNUSED(type
))
1839 DEFINE_ALCBACKENDFACTORY_VTABLE(ALCpulseBackendFactory
);
1841 #endif /* PA_API_VERSION == 12 */
1843 ALCbackendFactory
*ALCpulseBackendFactory_getFactory(void)
1845 static ALCpulseBackendFactory factory
= ALCPULSEBACKENDFACTORY_INITIALIZER
;
1846 return STATIC_CAST(ALCbackendFactory
, &factory
);