Trace PulseAudio's active port
[openal-soft.git] / Alc / backends / pulseaudio.c
blobdcb1a508358d8ecb32bf19ce2793f0ab2eba4467
1 /**
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
22 #include "config.h"
24 #include <string.h>
26 #include "alMain.h"
27 #include "alu.h"
28 #include "threads.h"
29 #include "compat.h"
31 #include "backends/base.h"
33 #include <pulse/pulseaudio.h>
35 #if PA_API_VERSION == 12
37 #ifndef PA_CHECK_VERSION
38 #define PA_CHECK_VERSION(major,minor,micro) \
39 ((PA_MAJOR > (major)) || \
40 (PA_MAJOR == (major) && PA_MINOR > (minor)) || \
41 (PA_MAJOR == (major) && PA_MINOR == (minor) && PA_MICRO >= (micro)))
42 #endif
44 #ifdef HAVE_DYNLOAD
45 static void *pa_handle;
46 #define MAKE_FUNC(x) static __typeof(x) * p##x
47 MAKE_FUNC(pa_context_unref);
48 MAKE_FUNC(pa_sample_spec_valid);
49 MAKE_FUNC(pa_frame_size);
50 MAKE_FUNC(pa_stream_drop);
51 MAKE_FUNC(pa_strerror);
52 MAKE_FUNC(pa_context_get_state);
53 MAKE_FUNC(pa_stream_get_state);
54 MAKE_FUNC(pa_threaded_mainloop_signal);
55 MAKE_FUNC(pa_stream_peek);
56 MAKE_FUNC(pa_threaded_mainloop_wait);
57 MAKE_FUNC(pa_threaded_mainloop_unlock);
58 MAKE_FUNC(pa_threaded_mainloop_in_thread);
59 MAKE_FUNC(pa_context_new);
60 MAKE_FUNC(pa_threaded_mainloop_stop);
61 MAKE_FUNC(pa_context_disconnect);
62 MAKE_FUNC(pa_threaded_mainloop_start);
63 MAKE_FUNC(pa_threaded_mainloop_get_api);
64 MAKE_FUNC(pa_context_set_state_callback);
65 MAKE_FUNC(pa_stream_write);
66 MAKE_FUNC(pa_xfree);
67 MAKE_FUNC(pa_stream_connect_record);
68 MAKE_FUNC(pa_stream_connect_playback);
69 MAKE_FUNC(pa_stream_readable_size);
70 MAKE_FUNC(pa_stream_writable_size);
71 MAKE_FUNC(pa_stream_is_corked);
72 MAKE_FUNC(pa_stream_cork);
73 MAKE_FUNC(pa_stream_is_suspended);
74 MAKE_FUNC(pa_stream_get_device_name);
75 MAKE_FUNC(pa_stream_get_latency);
76 MAKE_FUNC(pa_path_get_filename);
77 MAKE_FUNC(pa_get_binary_name);
78 MAKE_FUNC(pa_threaded_mainloop_free);
79 MAKE_FUNC(pa_context_errno);
80 MAKE_FUNC(pa_xmalloc);
81 MAKE_FUNC(pa_stream_unref);
82 MAKE_FUNC(pa_threaded_mainloop_accept);
83 MAKE_FUNC(pa_stream_set_write_callback);
84 MAKE_FUNC(pa_threaded_mainloop_new);
85 MAKE_FUNC(pa_context_connect);
86 MAKE_FUNC(pa_stream_set_buffer_attr);
87 MAKE_FUNC(pa_stream_get_buffer_attr);
88 MAKE_FUNC(pa_stream_get_sample_spec);
89 MAKE_FUNC(pa_stream_get_time);
90 MAKE_FUNC(pa_stream_set_read_callback);
91 MAKE_FUNC(pa_stream_set_state_callback);
92 MAKE_FUNC(pa_stream_set_moved_callback);
93 MAKE_FUNC(pa_stream_set_underflow_callback);
94 MAKE_FUNC(pa_stream_new_with_proplist);
95 MAKE_FUNC(pa_stream_disconnect);
96 MAKE_FUNC(pa_threaded_mainloop_lock);
97 MAKE_FUNC(pa_channel_map_init_auto);
98 MAKE_FUNC(pa_channel_map_parse);
99 MAKE_FUNC(pa_channel_map_snprint);
100 MAKE_FUNC(pa_channel_map_equal);
101 MAKE_FUNC(pa_context_get_server_info);
102 MAKE_FUNC(pa_context_get_sink_info_by_name);
103 MAKE_FUNC(pa_context_get_sink_info_list);
104 MAKE_FUNC(pa_context_get_source_info_by_name);
105 MAKE_FUNC(pa_context_get_source_info_list);
106 MAKE_FUNC(pa_operation_get_state);
107 MAKE_FUNC(pa_operation_unref);
108 MAKE_FUNC(pa_proplist_new);
109 MAKE_FUNC(pa_proplist_free);
110 MAKE_FUNC(pa_proplist_set);
111 #if PA_CHECK_VERSION(0,9,15)
112 MAKE_FUNC(pa_channel_map_superset);
113 MAKE_FUNC(pa_stream_set_buffer_attr_callback);
114 #endif
115 #if PA_CHECK_VERSION(0,9,16)
116 MAKE_FUNC(pa_stream_begin_write);
117 #endif
118 #undef MAKE_FUNC
120 #define pa_context_unref ppa_context_unref
121 #define pa_sample_spec_valid ppa_sample_spec_valid
122 #define pa_frame_size ppa_frame_size
123 #define pa_stream_drop ppa_stream_drop
124 #define pa_strerror ppa_strerror
125 #define pa_context_get_state ppa_context_get_state
126 #define pa_stream_get_state ppa_stream_get_state
127 #define pa_threaded_mainloop_signal ppa_threaded_mainloop_signal
128 #define pa_stream_peek ppa_stream_peek
129 #define pa_threaded_mainloop_wait ppa_threaded_mainloop_wait
130 #define pa_threaded_mainloop_unlock ppa_threaded_mainloop_unlock
131 #define pa_threaded_mainloop_in_thread ppa_threaded_mainloop_in_thread
132 #define pa_context_new ppa_context_new
133 #define pa_threaded_mainloop_stop ppa_threaded_mainloop_stop
134 #define pa_context_disconnect ppa_context_disconnect
135 #define pa_threaded_mainloop_start ppa_threaded_mainloop_start
136 #define pa_threaded_mainloop_get_api ppa_threaded_mainloop_get_api
137 #define pa_context_set_state_callback ppa_context_set_state_callback
138 #define pa_stream_write ppa_stream_write
139 #define pa_xfree ppa_xfree
140 #define pa_stream_connect_record ppa_stream_connect_record
141 #define pa_stream_connect_playback ppa_stream_connect_playback
142 #define pa_stream_readable_size ppa_stream_readable_size
143 #define pa_stream_writable_size ppa_stream_writable_size
144 #define pa_stream_is_corked ppa_stream_is_corked
145 #define pa_stream_cork ppa_stream_cork
146 #define pa_stream_is_suspended ppa_stream_is_suspended
147 #define pa_stream_get_device_name ppa_stream_get_device_name
148 #define pa_stream_get_latency ppa_stream_get_latency
149 #define pa_path_get_filename ppa_path_get_filename
150 #define pa_get_binary_name ppa_get_binary_name
151 #define pa_threaded_mainloop_free ppa_threaded_mainloop_free
152 #define pa_context_errno ppa_context_errno
153 #define pa_xmalloc ppa_xmalloc
154 #define pa_stream_unref ppa_stream_unref
155 #define pa_threaded_mainloop_accept ppa_threaded_mainloop_accept
156 #define pa_stream_set_write_callback ppa_stream_set_write_callback
157 #define pa_threaded_mainloop_new ppa_threaded_mainloop_new
158 #define pa_context_connect ppa_context_connect
159 #define pa_stream_set_buffer_attr ppa_stream_set_buffer_attr
160 #define pa_stream_get_buffer_attr ppa_stream_get_buffer_attr
161 #define pa_stream_get_sample_spec ppa_stream_get_sample_spec
162 #define pa_stream_get_time ppa_stream_get_time
163 #define pa_stream_set_read_callback ppa_stream_set_read_callback
164 #define pa_stream_set_state_callback ppa_stream_set_state_callback
165 #define pa_stream_set_moved_callback ppa_stream_set_moved_callback
166 #define pa_stream_set_underflow_callback ppa_stream_set_underflow_callback
167 #define pa_stream_new_with_proplist ppa_stream_new_with_proplist
168 #define pa_stream_disconnect ppa_stream_disconnect
169 #define pa_threaded_mainloop_lock ppa_threaded_mainloop_lock
170 #define pa_channel_map_init_auto ppa_channel_map_init_auto
171 #define pa_channel_map_parse ppa_channel_map_parse
172 #define pa_channel_map_snprint ppa_channel_map_snprint
173 #define pa_channel_map_equal ppa_channel_map_equal
174 #define pa_context_get_server_info ppa_context_get_server_info
175 #define pa_context_get_sink_info_by_name ppa_context_get_sink_info_by_name
176 #define pa_context_get_sink_info_list ppa_context_get_sink_info_list
177 #define pa_context_get_source_info_by_name ppa_context_get_source_info_by_name
178 #define pa_context_get_source_info_list ppa_context_get_source_info_list
179 #define pa_operation_get_state ppa_operation_get_state
180 #define pa_operation_unref ppa_operation_unref
181 #define pa_proplist_new ppa_proplist_new
182 #define pa_proplist_free ppa_proplist_free
183 #define pa_proplist_set ppa_proplist_set
184 #if PA_CHECK_VERSION(0,9,15)
185 #define pa_channel_map_superset ppa_channel_map_superset
186 #define pa_stream_set_buffer_attr_callback ppa_stream_set_buffer_attr_callback
187 #endif
188 #if PA_CHECK_VERSION(0,9,16)
189 #define pa_stream_begin_write ppa_stream_begin_write
190 #endif
192 #endif
194 static ALCboolean pulse_load(void)
196 ALCboolean ret = ALC_TRUE;
197 #ifdef HAVE_DYNLOAD
198 if(!pa_handle)
200 #ifdef _WIN32
201 #define PALIB "libpulse-0.dll"
202 #elif defined(__APPLE__) && defined(__MACH__)
203 #define PALIB "libpulse.0.dylib"
204 #else
205 #define PALIB "libpulse.so.0"
206 #endif
207 pa_handle = LoadLib(PALIB);
208 if(!pa_handle)
209 return ALC_FALSE;
211 #define LOAD_FUNC(x) do { \
212 p##x = GetSymbol(pa_handle, #x); \
213 if(!(p##x)) { \
214 ret = ALC_FALSE; \
216 } while(0)
217 LOAD_FUNC(pa_context_unref);
218 LOAD_FUNC(pa_sample_spec_valid);
219 LOAD_FUNC(pa_stream_drop);
220 LOAD_FUNC(pa_frame_size);
221 LOAD_FUNC(pa_strerror);
222 LOAD_FUNC(pa_context_get_state);
223 LOAD_FUNC(pa_stream_get_state);
224 LOAD_FUNC(pa_threaded_mainloop_signal);
225 LOAD_FUNC(pa_stream_peek);
226 LOAD_FUNC(pa_threaded_mainloop_wait);
227 LOAD_FUNC(pa_threaded_mainloop_unlock);
228 LOAD_FUNC(pa_threaded_mainloop_in_thread);
229 LOAD_FUNC(pa_context_new);
230 LOAD_FUNC(pa_threaded_mainloop_stop);
231 LOAD_FUNC(pa_context_disconnect);
232 LOAD_FUNC(pa_threaded_mainloop_start);
233 LOAD_FUNC(pa_threaded_mainloop_get_api);
234 LOAD_FUNC(pa_context_set_state_callback);
235 LOAD_FUNC(pa_stream_write);
236 LOAD_FUNC(pa_xfree);
237 LOAD_FUNC(pa_stream_connect_record);
238 LOAD_FUNC(pa_stream_connect_playback);
239 LOAD_FUNC(pa_stream_readable_size);
240 LOAD_FUNC(pa_stream_writable_size);
241 LOAD_FUNC(pa_stream_is_corked);
242 LOAD_FUNC(pa_stream_cork);
243 LOAD_FUNC(pa_stream_is_suspended);
244 LOAD_FUNC(pa_stream_get_device_name);
245 LOAD_FUNC(pa_stream_get_latency);
246 LOAD_FUNC(pa_path_get_filename);
247 LOAD_FUNC(pa_get_binary_name);
248 LOAD_FUNC(pa_threaded_mainloop_free);
249 LOAD_FUNC(pa_context_errno);
250 LOAD_FUNC(pa_xmalloc);
251 LOAD_FUNC(pa_stream_unref);
252 LOAD_FUNC(pa_threaded_mainloop_accept);
253 LOAD_FUNC(pa_stream_set_write_callback);
254 LOAD_FUNC(pa_threaded_mainloop_new);
255 LOAD_FUNC(pa_context_connect);
256 LOAD_FUNC(pa_stream_set_buffer_attr);
257 LOAD_FUNC(pa_stream_get_buffer_attr);
258 LOAD_FUNC(pa_stream_get_sample_spec);
259 LOAD_FUNC(pa_stream_get_time);
260 LOAD_FUNC(pa_stream_set_read_callback);
261 LOAD_FUNC(pa_stream_set_state_callback);
262 LOAD_FUNC(pa_stream_set_moved_callback);
263 LOAD_FUNC(pa_stream_set_underflow_callback);
264 LOAD_FUNC(pa_stream_new_with_proplist);
265 LOAD_FUNC(pa_stream_disconnect);
266 LOAD_FUNC(pa_threaded_mainloop_lock);
267 LOAD_FUNC(pa_channel_map_init_auto);
268 LOAD_FUNC(pa_channel_map_parse);
269 LOAD_FUNC(pa_channel_map_snprint);
270 LOAD_FUNC(pa_channel_map_equal);
271 LOAD_FUNC(pa_context_get_server_info);
272 LOAD_FUNC(pa_context_get_sink_info_by_name);
273 LOAD_FUNC(pa_context_get_sink_info_list);
274 LOAD_FUNC(pa_context_get_source_info_by_name);
275 LOAD_FUNC(pa_context_get_source_info_list);
276 LOAD_FUNC(pa_operation_get_state);
277 LOAD_FUNC(pa_operation_unref);
278 LOAD_FUNC(pa_proplist_new);
279 LOAD_FUNC(pa_proplist_free);
280 LOAD_FUNC(pa_proplist_set);
281 #undef LOAD_FUNC
282 #define LOAD_OPTIONAL_FUNC(x) do { \
283 p##x = GetSymbol(pa_handle, #x); \
284 } while(0)
285 #if PA_CHECK_VERSION(0,9,15)
286 LOAD_OPTIONAL_FUNC(pa_channel_map_superset);
287 LOAD_OPTIONAL_FUNC(pa_stream_set_buffer_attr_callback);
288 #endif
289 #if PA_CHECK_VERSION(0,9,16)
290 LOAD_OPTIONAL_FUNC(pa_stream_begin_write);
291 #endif
292 #undef LOAD_OPTIONAL_FUNC
294 if(ret == ALC_FALSE)
296 CloseLib(pa_handle);
297 pa_handle = NULL;
300 #endif /* HAVE_DYNLOAD */
301 return ret;
305 /* Global flags and properties */
306 static pa_context_flags_t pulse_ctx_flags;
307 static pa_proplist *prop_filter;
310 /* PulseAudio Event Callbacks */
311 static void context_state_callback(pa_context *context, void *pdata)
313 pa_threaded_mainloop *loop = pdata;
314 pa_context_state_t state;
316 state = pa_context_get_state(context);
317 if(state == PA_CONTEXT_READY || !PA_CONTEXT_IS_GOOD(state))
318 pa_threaded_mainloop_signal(loop, 0);
321 static void stream_state_callback(pa_stream *stream, void *pdata)
323 pa_threaded_mainloop *loop = pdata;
324 pa_stream_state_t state;
326 state = pa_stream_get_state(stream);
327 if(state == PA_STREAM_READY || !PA_STREAM_IS_GOOD(state))
328 pa_threaded_mainloop_signal(loop, 0);
331 static void stream_success_callback(pa_stream *UNUSED(stream), int UNUSED(success), void *pdata)
333 pa_threaded_mainloop *loop = pdata;
334 pa_threaded_mainloop_signal(loop, 0);
337 static void wait_for_operation(pa_operation *op, pa_threaded_mainloop *loop)
339 if(op)
341 while(pa_operation_get_state(op) == PA_OPERATION_RUNNING)
342 pa_threaded_mainloop_wait(loop);
343 pa_operation_unref(op);
348 static pa_context *connect_context(pa_threaded_mainloop *loop, ALboolean silent)
350 const char *name = "OpenAL Soft";
351 char path_name[PATH_MAX];
352 pa_context_state_t state;
353 pa_context *context;
354 int err;
356 if(pa_get_binary_name(path_name, sizeof(path_name)))
357 name = pa_path_get_filename(path_name);
359 context = pa_context_new(pa_threaded_mainloop_get_api(loop), name);
360 if(!context)
362 ERR("pa_context_new() failed\n");
363 return NULL;
366 pa_context_set_state_callback(context, context_state_callback, loop);
368 if((err=pa_context_connect(context, NULL, pulse_ctx_flags, NULL)) >= 0)
370 while((state=pa_context_get_state(context)) != PA_CONTEXT_READY)
372 if(!PA_CONTEXT_IS_GOOD(state))
374 err = pa_context_errno(context);
375 if(err > 0) err = -err;
376 break;
379 pa_threaded_mainloop_wait(loop);
382 pa_context_set_state_callback(context, NULL, NULL);
384 if(err < 0)
386 if(!silent)
387 ERR("Context did not connect: %s\n", pa_strerror(err));
388 pa_context_unref(context);
389 return NULL;
392 return context;
396 static ALCboolean pulse_open(pa_threaded_mainloop **loop, pa_context **context,
397 void(*state_cb)(pa_context*,void*), void *ptr)
399 if(!(*loop = pa_threaded_mainloop_new()))
401 ERR("pa_threaded_mainloop_new() failed!\n");
402 return ALC_FALSE;
404 if(pa_threaded_mainloop_start(*loop) < 0)
406 ERR("pa_threaded_mainloop_start() failed\n");
407 goto error;
410 pa_threaded_mainloop_lock(*loop);
412 *context = connect_context(*loop, AL_FALSE);
413 if(!*context)
415 pa_threaded_mainloop_unlock(*loop);
416 pa_threaded_mainloop_stop(*loop);
417 goto error;
419 pa_context_set_state_callback(*context, state_cb, ptr);
421 pa_threaded_mainloop_unlock(*loop);
422 return ALC_TRUE;
424 error:
425 pa_threaded_mainloop_free(*loop);
426 *loop = NULL;
428 return ALC_FALSE;
431 static void pulse_close(pa_threaded_mainloop *loop, pa_context *context,
432 pa_stream *stream)
434 pa_threaded_mainloop_lock(loop);
436 if(stream)
438 pa_stream_set_state_callback(stream, NULL, NULL);
439 pa_stream_set_moved_callback(stream, NULL, NULL);
440 pa_stream_set_write_callback(stream, NULL, NULL);
441 #if PA_CHECK_VERSION(0,9,15)
442 if(pa_stream_set_buffer_attr_callback)
443 pa_stream_set_buffer_attr_callback(stream, NULL, NULL);
444 #endif
445 pa_stream_disconnect(stream);
446 pa_stream_unref(stream);
449 pa_context_disconnect(context);
450 pa_context_unref(context);
452 pa_threaded_mainloop_unlock(loop);
454 pa_threaded_mainloop_stop(loop);
455 pa_threaded_mainloop_free(loop);
459 typedef struct {
460 al_string name;
461 al_string device_name;
462 } DevMap;
463 TYPEDEF_VECTOR(DevMap, vector_DevMap)
465 static vector_DevMap PlaybackDevices;
466 static vector_DevMap CaptureDevices;
468 static void clear_devlist(vector_DevMap *list)
470 #define DEINIT_STRS(i) (AL_STRING_DEINIT((i)->name),AL_STRING_DEINIT((i)->device_name))
471 VECTOR_FOR_EACH(DevMap, *list, DEINIT_STRS);
472 #undef DEINIT_STRS
473 VECTOR_RESIZE(*list, 0);
477 typedef struct ALCpulsePlayback {
478 DERIVE_FROM_TYPE(ALCbackend);
480 al_string device_name;
482 pa_buffer_attr attr;
483 pa_sample_spec spec;
485 pa_threaded_mainloop *loop;
487 pa_stream *stream;
488 pa_context *context;
490 volatile ALboolean killNow;
491 althrd_t thread;
492 } ALCpulsePlayback;
494 static void ALCpulsePlayback_deviceCallback(pa_context *context, const pa_sink_info *info, int eol, void *pdata);
495 static void ALCpulsePlayback_probeDevices(void);
497 static void ALCpulsePlayback_bufferAttrCallback(pa_stream *stream, void *pdata);
498 static void ALCpulsePlayback_contextStateCallback(pa_context *context, void *pdata);
499 static void ALCpulsePlayback_streamStateCallback(pa_stream *stream, void *pdata);
500 static void ALCpulsePlayback_streamWriteCallback(pa_stream *p, size_t nbytes, void *userdata);
501 static void ALCpulsePlayback_sinkInfoCallback(pa_context *context, const pa_sink_info *info, int eol, void *pdata);
502 static void ALCpulsePlayback_sinkNameCallback(pa_context *context, const pa_sink_info *info, int eol, void *pdata);
503 static void ALCpulsePlayback_streamMovedCallback(pa_stream *stream, void *pdata);
504 static pa_stream *ALCpulsePlayback_connectStream(const char *device_name, pa_threaded_mainloop *loop,
505 pa_context *context, pa_stream_flags_t flags,
506 pa_buffer_attr *attr, pa_sample_spec *spec,
507 pa_channel_map *chanmap);
508 static int ALCpulsePlayback_mixerProc(void *ptr);
510 static void ALCpulsePlayback_Construct(ALCpulsePlayback *self, ALCdevice *device);
511 static void ALCpulsePlayback_Destruct(ALCpulsePlayback *self);
512 static ALCenum ALCpulsePlayback_open(ALCpulsePlayback *self, const ALCchar *name);
513 static void ALCpulsePlayback_close(ALCpulsePlayback *self);
514 static ALCboolean ALCpulsePlayback_reset(ALCpulsePlayback *self);
515 static ALCboolean ALCpulsePlayback_start(ALCpulsePlayback *self);
516 static void ALCpulsePlayback_stop(ALCpulsePlayback *self);
517 static DECLARE_FORWARD2(ALCpulsePlayback, ALCbackend, ALCenum, captureSamples, ALCvoid*, ALCuint)
518 static DECLARE_FORWARD(ALCpulsePlayback, ALCbackend, ALCuint, availableSamples)
519 static ALint64 ALCpulsePlayback_getLatency(ALCpulsePlayback *self);
520 static void ALCpulsePlayback_lock(ALCpulsePlayback *self);
521 static void ALCpulsePlayback_unlock(ALCpulsePlayback *self);
522 DECLARE_DEFAULT_ALLOCATORS(ALCpulsePlayback)
524 DEFINE_ALCBACKEND_VTABLE(ALCpulsePlayback);
527 static void ALCpulsePlayback_Construct(ALCpulsePlayback *self, ALCdevice *device)
529 ALCbackend_Construct(STATIC_CAST(ALCbackend, self), device);
530 SET_VTABLE2(ALCpulsePlayback, ALCbackend, self);
532 AL_STRING_INIT(self->device_name);
535 static void ALCpulsePlayback_Destruct(ALCpulsePlayback *self)
537 AL_STRING_DEINIT(self->device_name);
538 ALCbackend_Destruct(STATIC_CAST(ALCbackend, self));
542 static void ALCpulsePlayback_deviceCallback(pa_context *UNUSED(context), const pa_sink_info *info, int eol, void *pdata)
544 pa_threaded_mainloop *loop = pdata;
545 const DevMap *iter;
546 DevMap entry;
548 if(eol)
550 pa_threaded_mainloop_signal(loop, 0);
551 return;
554 #define MATCH_INFO_NAME(iter) (al_string_cmp_cstr((iter)->device_name, info->name) == 0)
555 VECTOR_FIND_IF(iter, const DevMap, PlaybackDevices, MATCH_INFO_NAME);
556 #undef MATCH_INFO_NAME
557 if(iter != VECTOR_ITER_END(PlaybackDevices))
558 return;
560 TRACE("Got device \"%s\", \"%s\"\n", info->description, info->name);
562 AL_STRING_INIT(entry.name);
563 AL_STRING_INIT(entry.device_name);
565 al_string_copy_cstr(&entry.name, info->description);
566 al_string_copy_cstr(&entry.device_name, info->name);
568 VECTOR_PUSH_BACK(PlaybackDevices, entry);
571 static void ALCpulsePlayback_probeDevices(void)
573 pa_threaded_mainloop *loop;
575 clear_devlist(&PlaybackDevices);
577 if((loop=pa_threaded_mainloop_new()) &&
578 pa_threaded_mainloop_start(loop) >= 0)
580 pa_context *context;
582 pa_threaded_mainloop_lock(loop);
583 context = connect_context(loop, AL_FALSE);
584 if(context)
586 pa_operation *o;
587 pa_stream_flags_t flags;
588 pa_sample_spec spec;
589 pa_stream *stream;
591 flags = PA_STREAM_FIX_FORMAT | PA_STREAM_FIX_RATE |
592 PA_STREAM_FIX_CHANNELS | PA_STREAM_DONT_MOVE;
594 spec.format = PA_SAMPLE_S16NE;
595 spec.rate = 44100;
596 spec.channels = 2;
598 stream = ALCpulsePlayback_connectStream(NULL, loop, context, flags,
599 NULL, &spec, NULL);
600 if(stream)
602 o = pa_context_get_sink_info_by_name(context, pa_stream_get_device_name(stream),
603 ALCpulsePlayback_deviceCallback, loop);
604 wait_for_operation(o, loop);
606 pa_stream_disconnect(stream);
607 pa_stream_unref(stream);
608 stream = NULL;
611 o = pa_context_get_sink_info_list(context, ALCpulsePlayback_deviceCallback, loop);
612 wait_for_operation(o, loop);
614 pa_context_disconnect(context);
615 pa_context_unref(context);
617 pa_threaded_mainloop_unlock(loop);
618 pa_threaded_mainloop_stop(loop);
620 if(loop)
621 pa_threaded_mainloop_free(loop);
625 static void ALCpulsePlayback_bufferAttrCallback(pa_stream *stream, void *pdata)
627 ALCpulsePlayback *self = pdata;
629 self->attr = *pa_stream_get_buffer_attr(stream);
630 TRACE("minreq=%d, tlength=%d, prebuf=%d\n", self->attr.minreq, self->attr.tlength, self->attr.prebuf);
633 static void ALCpulsePlayback_contextStateCallback(pa_context *context, void *pdata)
635 ALCpulsePlayback *self = pdata;
636 if(pa_context_get_state(context) == PA_CONTEXT_FAILED)
638 ERR("Received context failure!\n");
639 aluHandleDisconnect(STATIC_CAST(ALCbackend,self)->mDevice);
641 pa_threaded_mainloop_signal(self->loop, 0);
644 static void ALCpulsePlayback_streamStateCallback(pa_stream *stream, void *pdata)
646 ALCpulsePlayback *self = pdata;
647 if(pa_stream_get_state(stream) == PA_STREAM_FAILED)
649 ERR("Received stream failure!\n");
650 aluHandleDisconnect(STATIC_CAST(ALCbackend,self)->mDevice);
652 pa_threaded_mainloop_signal(self->loop, 0);
655 static void ALCpulsePlayback_streamWriteCallback(pa_stream* UNUSED(p), size_t UNUSED(nbytes), void *pdata)
657 ALCpulsePlayback *self = pdata;
658 pa_threaded_mainloop_signal(self->loop, 0);
661 static void ALCpulsePlayback_sinkInfoCallback(pa_context *UNUSED(context), const pa_sink_info *info, int eol, void *pdata)
663 ALCpulsePlayback *self = pdata;
664 ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
665 const struct {
666 const char *str;
667 enum DevFmtChannels chans;
668 } chanmaps[] = {
669 { "front-left,front-right,front-center,lfe,rear-left,rear-right,side-left,side-right",
670 DevFmtX71 },
671 { "front-left,front-right,front-center,lfe,rear-center,side-left,side-right",
672 DevFmtX61 },
673 { "front-left,front-right,front-center,lfe,side-left,side-right",
674 DevFmtX51 },
675 { "front-left,front-right,front-center,lfe,rear-left,rear-right",
676 DevFmtX51Rear },
677 { "front-left,front-right,rear-left,rear-right", DevFmtQuad },
678 { "front-left,front-right", DevFmtStereo },
679 { "mono", DevFmtMono },
680 { NULL, 0 }
682 int i;
684 if(eol)
686 pa_threaded_mainloop_signal(self->loop, 0);
687 return;
690 for(i = 0;chanmaps[i].str;i++)
692 pa_channel_map map;
693 if(!pa_channel_map_parse(&map, chanmaps[i].str))
694 continue;
696 if(pa_channel_map_equal(&info->channel_map, &map)
697 #if PA_CHECK_VERSION(0,9,15)
698 || (pa_channel_map_superset &&
699 pa_channel_map_superset(&info->channel_map, &map))
700 #endif
703 if(!(device->Flags&DEVICE_CHANNELS_REQUEST))
704 device->FmtChans = chanmaps[i].chans;
705 break;
709 if(info->active_port)
710 TRACE("Active port: %s (%s)\n", info->active_port->name, info->active_port->description);
711 device->IsHeadphones = (info->active_port &&
712 strcmp(info->active_port->name, "analog-output-headphones") == 0 &&
713 device->FmtChans == DevFmtStereo);
715 if(!chanmaps[i].str)
717 char chanmap_str[PA_CHANNEL_MAP_SNPRINT_MAX] = "";
718 pa_channel_map_snprint(chanmap_str, sizeof(chanmap_str), &info->channel_map);
719 WARN("Failed to find format for channel map:\n %s\n", chanmap_str);
723 static void ALCpulsePlayback_sinkNameCallback(pa_context *UNUSED(context), const pa_sink_info *info, int eol, void *pdata)
725 ALCpulsePlayback *self = pdata;
726 ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
728 if(eol)
730 pa_threaded_mainloop_signal(self->loop, 0);
731 return;
734 al_string_copy_cstr(&device->DeviceName, info->description);
738 static void ALCpulsePlayback_streamMovedCallback(pa_stream *stream, void *pdata)
740 ALCpulsePlayback *self = pdata;
742 al_string_copy_cstr(&self->device_name, pa_stream_get_device_name(stream));
744 TRACE("Stream moved to %s\n", al_string_get_cstr(self->device_name));
748 static pa_stream *ALCpulsePlayback_connectStream(const char *device_name,
749 pa_threaded_mainloop *loop, pa_context *context,
750 pa_stream_flags_t flags, pa_buffer_attr *attr, pa_sample_spec *spec,
751 pa_channel_map *chanmap)
753 pa_stream_state_t state;
754 pa_stream *stream;
756 stream = pa_stream_new_with_proplist(context, "Playback Stream", spec, chanmap, prop_filter);
757 if(!stream)
759 ERR("pa_stream_new_with_proplist() failed: %s\n", pa_strerror(pa_context_errno(context)));
760 return NULL;
763 pa_stream_set_state_callback(stream, stream_state_callback, loop);
765 if(pa_stream_connect_playback(stream, device_name, attr, flags, NULL, NULL) < 0)
767 ERR("Stream did not connect: %s\n", pa_strerror(pa_context_errno(context)));
768 pa_stream_unref(stream);
769 return NULL;
772 while((state=pa_stream_get_state(stream)) != PA_STREAM_READY)
774 if(!PA_STREAM_IS_GOOD(state))
776 ERR("Stream did not get ready: %s\n", pa_strerror(pa_context_errno(context)));
777 pa_stream_unref(stream);
778 return NULL;
781 pa_threaded_mainloop_wait(loop);
783 pa_stream_set_state_callback(stream, NULL, NULL);
785 return stream;
789 static int ALCpulsePlayback_mixerProc(void *ptr)
791 ALCpulsePlayback *self = ptr;
792 ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
793 ALuint buffer_size;
794 ALint update_size;
795 size_t frame_size;
796 ssize_t len;
798 SetRTPriority();
799 althrd_setname(althrd_current(), MIXER_THREAD_NAME);
801 pa_threaded_mainloop_lock(self->loop);
802 frame_size = pa_frame_size(&self->spec);
803 update_size = device->UpdateSize * frame_size;
805 /* Sanitize buffer metrics, in case we actually have less than what we
806 * asked for. */
807 buffer_size = minu(update_size*device->NumUpdates, self->attr.tlength);
808 update_size = minu(update_size, buffer_size/2);
809 do {
810 len = pa_stream_writable_size(self->stream) - self->attr.tlength +
811 buffer_size;
812 if(len < update_size)
814 if(pa_stream_is_corked(self->stream) == 1)
816 pa_operation *o;
817 o = pa_stream_cork(self->stream, 0, NULL, NULL);
818 if(o) pa_operation_unref(o);
820 pa_threaded_mainloop_wait(self->loop);
821 continue;
823 len -= len%update_size;
825 while(len > 0)
827 size_t newlen = len;
828 void *buf;
829 pa_free_cb_t free_func = NULL;
831 #if PA_CHECK_VERSION(0,9,16)
832 if(!pa_stream_begin_write ||
833 pa_stream_begin_write(self->stream, &buf, &newlen) < 0)
834 #endif
836 buf = pa_xmalloc(newlen);
837 free_func = pa_xfree;
840 aluMixData(device, buf, newlen/frame_size);
842 pa_stream_write(self->stream, buf, newlen, free_func, 0, PA_SEEK_RELATIVE);
843 len -= newlen;
845 } while(!self->killNow && device->Connected);
846 pa_threaded_mainloop_unlock(self->loop);
848 return 0;
852 static ALCenum ALCpulsePlayback_open(ALCpulsePlayback *self, const ALCchar *name)
854 const char *pulse_name = NULL;
855 pa_stream_flags_t flags;
856 pa_sample_spec spec;
857 pa_operation *o;
859 if(name)
861 const DevMap *iter;
863 if(VECTOR_SIZE(PlaybackDevices) == 0)
864 ALCpulsePlayback_probeDevices();
866 #define MATCH_NAME(iter) (al_string_cmp_cstr((iter)->name, name) == 0)
867 VECTOR_FIND_IF(iter, const DevMap, PlaybackDevices, MATCH_NAME);
868 #undef MATCH_NAME
869 if(iter == VECTOR_ITER_END(PlaybackDevices))
870 return ALC_INVALID_VALUE;
871 pulse_name = al_string_get_cstr(iter->device_name);
874 if(!pulse_open(&self->loop, &self->context, ALCpulsePlayback_contextStateCallback, self))
875 return ALC_INVALID_VALUE;
877 pa_threaded_mainloop_lock(self->loop);
879 flags = PA_STREAM_FIX_FORMAT | PA_STREAM_FIX_RATE |
880 PA_STREAM_FIX_CHANNELS;
881 if(!GetConfigValueBool("pulse", "allow-moves", 0))
882 flags |= PA_STREAM_DONT_MOVE;
884 spec.format = PA_SAMPLE_S16NE;
885 spec.rate = 44100;
886 spec.channels = 2;
888 TRACE("Connecting to \"%s\"\n", pulse_name ? pulse_name : "(default)");
889 self->stream = ALCpulsePlayback_connectStream(pulse_name, self->loop, self->context,
890 flags, NULL, &spec, NULL);
891 if(!self->stream)
893 pa_threaded_mainloop_unlock(self->loop);
894 pulse_close(self->loop, self->context, self->stream);
895 self->loop = NULL;
896 self->context = NULL;
897 return ALC_INVALID_VALUE;
899 pa_stream_set_moved_callback(self->stream, ALCpulsePlayback_streamMovedCallback, self);
901 al_string_copy_cstr(&self->device_name, pa_stream_get_device_name(self->stream));
902 o = pa_context_get_sink_info_by_name(self->context,
903 al_string_get_cstr(self->device_name),
904 ALCpulsePlayback_sinkNameCallback, self);
905 wait_for_operation(o, self->loop);
907 pa_threaded_mainloop_unlock(self->loop);
909 return ALC_NO_ERROR;
912 static void ALCpulsePlayback_close(ALCpulsePlayback *self)
914 pulse_close(self->loop, self->context, self->stream);
915 self->loop = NULL;
916 self->context = NULL;
917 self->stream = NULL;
919 al_string_clear(&self->device_name);
922 static ALCboolean ALCpulsePlayback_reset(ALCpulsePlayback *self)
924 ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
925 pa_stream_flags_t flags = 0;
926 const char *mapname = NULL;
927 pa_channel_map chanmap;
928 pa_operation *o;
929 ALuint len;
931 pa_threaded_mainloop_lock(self->loop);
933 if(self->stream)
935 pa_stream_set_moved_callback(self->stream, NULL, NULL);
936 #if PA_CHECK_VERSION(0,9,15)
937 if(pa_stream_set_buffer_attr_callback)
938 pa_stream_set_buffer_attr_callback(self->stream, NULL, NULL);
939 #endif
940 pa_stream_disconnect(self->stream);
941 pa_stream_unref(self->stream);
942 self->stream = NULL;
945 o = pa_context_get_sink_info_by_name(self->context, al_string_get_cstr(self->device_name),
946 ALCpulsePlayback_sinkInfoCallback, self);
947 wait_for_operation(o, self->loop);
949 if(!(device->Flags&DEVICE_FREQUENCY_REQUEST))
950 flags |= PA_STREAM_FIX_RATE;
951 flags |= PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE;
952 flags |= PA_STREAM_ADJUST_LATENCY;
953 flags |= PA_STREAM_START_CORKED;
954 if(!GetConfigValueBool("pulse", "allow-moves", 0))
955 flags |= PA_STREAM_DONT_MOVE;
957 switch(device->FmtType)
959 case DevFmtByte:
960 device->FmtType = DevFmtUByte;
961 /* fall-through */
962 case DevFmtUByte:
963 self->spec.format = PA_SAMPLE_U8;
964 break;
965 case DevFmtUShort:
966 device->FmtType = DevFmtShort;
967 /* fall-through */
968 case DevFmtShort:
969 self->spec.format = PA_SAMPLE_S16NE;
970 break;
971 case DevFmtUInt:
972 device->FmtType = DevFmtInt;
973 /* fall-through */
974 case DevFmtInt:
975 self->spec.format = PA_SAMPLE_S32NE;
976 break;
977 case DevFmtFloat:
978 self->spec.format = PA_SAMPLE_FLOAT32NE;
979 break;
981 self->spec.rate = device->Frequency;
982 self->spec.channels = ChannelsFromDevFmt(device->FmtChans);
984 if(pa_sample_spec_valid(&self->spec) == 0)
986 ERR("Invalid sample format\n");
987 pa_threaded_mainloop_unlock(self->loop);
988 return ALC_FALSE;
991 switch(device->FmtChans)
993 case DevFmtMono:
994 mapname = "mono";
995 break;
996 case DevFmtStereo:
997 mapname = "front-left,front-right";
998 break;
999 case DevFmtQuad:
1000 mapname = "front-left,front-right,rear-left,rear-right";
1001 break;
1002 case DevFmtX51:
1003 mapname = "front-left,front-right,front-center,lfe,side-left,side-right";
1004 break;
1005 case DevFmtX51Rear:
1006 mapname = "front-left,front-right,front-center,lfe,rear-left,rear-right";
1007 break;
1008 case DevFmtX61:
1009 mapname = "front-left,front-right,front-center,lfe,rear-center,side-left,side-right";
1010 break;
1011 case DevFmtX71:
1012 mapname = "front-left,front-right,front-center,lfe,rear-left,rear-right,side-left,side-right";
1013 break;
1015 if(!pa_channel_map_parse(&chanmap, mapname))
1017 ERR("Failed to build channel map for %s\n", DevFmtChannelsString(device->FmtChans));
1018 pa_threaded_mainloop_unlock(self->loop);
1019 return ALC_FALSE;
1021 SetDefaultWFXChannelOrder(device);
1023 self->attr.fragsize = -1;
1024 self->attr.prebuf = 0;
1025 self->attr.minreq = device->UpdateSize * pa_frame_size(&self->spec);
1026 self->attr.tlength = self->attr.minreq * maxu(device->NumUpdates, 2);
1027 self->attr.maxlength = -1;
1029 self->stream = ALCpulsePlayback_connectStream(al_string_get_cstr(self->device_name),
1030 self->loop, self->context, flags,
1031 &self->attr, &self->spec, &chanmap);
1032 if(!self->stream)
1034 pa_threaded_mainloop_unlock(self->loop);
1035 return ALC_FALSE;
1037 pa_stream_set_state_callback(self->stream, ALCpulsePlayback_streamStateCallback, self);
1038 pa_stream_set_moved_callback(self->stream, ALCpulsePlayback_streamMovedCallback, self);
1039 pa_stream_set_write_callback(self->stream, ALCpulsePlayback_streamWriteCallback, self);
1041 self->spec = *(pa_stream_get_sample_spec(self->stream));
1042 if(device->Frequency != self->spec.rate)
1044 /* Server updated our playback rate, so modify the buffer attribs
1045 * accordingly. */
1046 device->NumUpdates = (ALuint)((ALdouble)device->NumUpdates / device->Frequency *
1047 self->spec.rate + 0.5);
1048 self->attr.minreq = device->UpdateSize * pa_frame_size(&self->spec);
1049 self->attr.tlength = self->attr.minreq * clampu(device->NumUpdates, 2, 16);
1050 self->attr.maxlength = -1;
1051 self->attr.prebuf = 0;
1053 o = pa_stream_set_buffer_attr(self->stream, &self->attr,
1054 stream_success_callback, self->loop);
1055 wait_for_operation(o, self->loop);
1057 device->Frequency = self->spec.rate;
1060 #if PA_CHECK_VERSION(0,9,15)
1061 if(pa_stream_set_buffer_attr_callback)
1062 pa_stream_set_buffer_attr_callback(self->stream, ALCpulsePlayback_bufferAttrCallback, self);
1063 #endif
1064 ALCpulsePlayback_bufferAttrCallback(self->stream, self);
1066 len = self->attr.minreq / pa_frame_size(&self->spec);
1067 device->NumUpdates = (ALuint)((ALdouble)device->NumUpdates/len*device->UpdateSize + 0.5);
1068 device->NumUpdates = clampu(device->NumUpdates, 2, 16);
1069 device->UpdateSize = len;
1071 pa_threaded_mainloop_unlock(self->loop);
1072 return ALC_TRUE;
1075 static ALCboolean ALCpulsePlayback_start(ALCpulsePlayback *self)
1077 self->killNow = AL_FALSE;
1078 if(althrd_create(&self->thread, ALCpulsePlayback_mixerProc, self) != althrd_success)
1079 return ALC_FALSE;
1080 return ALC_TRUE;
1083 static void ALCpulsePlayback_stop(ALCpulsePlayback *self)
1085 pa_operation *o;
1086 int res;
1088 if(!self->stream)
1089 return;
1091 self->killNow = AL_TRUE;
1092 althrd_join(self->thread, &res);
1094 pa_threaded_mainloop_lock(self->loop);
1096 o = pa_stream_cork(self->stream, 1, stream_success_callback, self->loop);
1097 wait_for_operation(o, self->loop);
1099 pa_threaded_mainloop_unlock(self->loop);
1103 static ALint64 ALCpulsePlayback_getLatency(ALCpulsePlayback *self)
1105 pa_usec_t latency = 0;
1106 int neg, err;
1108 if((err=pa_stream_get_latency(self->stream, &latency, &neg)) != 0)
1110 /* FIXME: if err = -PA_ERR_NODATA, it means we were called too soon
1111 * after starting the stream and no timing info has been received from
1112 * the server yet. Should we wait, possibly stalling the app, or give a
1113 * dummy value? Either way, it shouldn't be 0. */
1114 if(err != -PA_ERR_NODATA)
1115 ERR("Failed to get stream latency: 0x%x\n", err);
1116 return 0;
1119 if(neg) latency = 0;
1120 return (ALint64)minu64(latency, U64(0x7fffffffffffffff)/1000) * 1000;
1124 static void ALCpulsePlayback_lock(ALCpulsePlayback *self)
1126 pa_threaded_mainloop_lock(self->loop);
1129 static void ALCpulsePlayback_unlock(ALCpulsePlayback *self)
1131 pa_threaded_mainloop_unlock(self->loop);
1135 typedef struct ALCpulseCapture {
1136 DERIVE_FROM_TYPE(ALCbackend);
1138 al_string device_name;
1140 const void *cap_store;
1141 size_t cap_len;
1142 size_t cap_remain;
1144 ALCuint last_readable;
1146 pa_buffer_attr attr;
1147 pa_sample_spec spec;
1149 pa_threaded_mainloop *loop;
1151 pa_stream *stream;
1152 pa_context *context;
1153 } ALCpulseCapture;
1155 static void ALCpulseCapture_deviceCallback(pa_context *context, const pa_source_info *info, int eol, void *pdata);
1156 static void ALCpulseCapture_probeDevices(void);
1158 static void ALCpulseCapture_contextStateCallback(pa_context *context, void *pdata);
1159 static void ALCpulseCapture_streamStateCallback(pa_stream *stream, void *pdata);
1160 static void ALCpulseCapture_sourceNameCallback(pa_context *context, const pa_source_info *info, int eol, void *pdata);
1161 static void ALCpulseCapture_streamMovedCallback(pa_stream *stream, void *pdata);
1162 static pa_stream *ALCpulseCapture_connectStream(const char *device_name,
1163 pa_threaded_mainloop *loop, pa_context *context,
1164 pa_stream_flags_t flags, pa_buffer_attr *attr,
1165 pa_sample_spec *spec, pa_channel_map *chanmap);
1167 static void ALCpulseCapture_Construct(ALCpulseCapture *self, ALCdevice *device);
1168 static void ALCpulseCapture_Destruct(ALCpulseCapture *self);
1169 static ALCenum ALCpulseCapture_open(ALCpulseCapture *self, const ALCchar *name);
1170 static void ALCpulseCapture_close(ALCpulseCapture *self);
1171 static DECLARE_FORWARD(ALCpulseCapture, ALCbackend, ALCboolean, reset)
1172 static ALCboolean ALCpulseCapture_start(ALCpulseCapture *self);
1173 static void ALCpulseCapture_stop(ALCpulseCapture *self);
1174 static ALCenum ALCpulseCapture_captureSamples(ALCpulseCapture *self, ALCvoid *buffer, ALCuint samples);
1175 static ALCuint ALCpulseCapture_availableSamples(ALCpulseCapture *self);
1176 static ALint64 ALCpulseCapture_getLatency(ALCpulseCapture *self);
1177 static void ALCpulseCapture_lock(ALCpulseCapture *self);
1178 static void ALCpulseCapture_unlock(ALCpulseCapture *self);
1179 DECLARE_DEFAULT_ALLOCATORS(ALCpulseCapture)
1181 DEFINE_ALCBACKEND_VTABLE(ALCpulseCapture);
1184 static void ALCpulseCapture_Construct(ALCpulseCapture *self, ALCdevice *device)
1186 ALCbackend_Construct(STATIC_CAST(ALCbackend, self), device);
1187 SET_VTABLE2(ALCpulseCapture, ALCbackend, self);
1189 AL_STRING_INIT(self->device_name);
1192 static void ALCpulseCapture_Destruct(ALCpulseCapture *self)
1194 AL_STRING_DEINIT(self->device_name);
1195 ALCbackend_Destruct(STATIC_CAST(ALCbackend, self));
1199 static void ALCpulseCapture_deviceCallback(pa_context *UNUSED(context), const pa_source_info *info, int eol, void *pdata)
1201 pa_threaded_mainloop *loop = pdata;
1202 const DevMap *iter;
1203 DevMap entry;
1205 if(eol)
1207 pa_threaded_mainloop_signal(loop, 0);
1208 return;
1211 #define MATCH_INFO_NAME(iter) (al_string_cmp_cstr((iter)->device_name, info->name) == 0)
1212 VECTOR_FIND_IF(iter, const DevMap, CaptureDevices, MATCH_INFO_NAME);
1213 #undef MATCH_INFO_NAME
1214 if(iter != VECTOR_ITER_END(CaptureDevices))
1215 return;
1217 TRACE("Got device \"%s\", \"%s\"\n", info->description, info->name);
1219 AL_STRING_INIT(entry.name);
1220 AL_STRING_INIT(entry.device_name);
1222 al_string_copy_cstr(&entry.name, info->description);
1223 al_string_copy_cstr(&entry.device_name, info->name);
1225 VECTOR_PUSH_BACK(CaptureDevices, entry);
1228 static void ALCpulseCapture_probeDevices(void)
1230 pa_threaded_mainloop *loop;
1232 clear_devlist(&CaptureDevices);
1234 if((loop=pa_threaded_mainloop_new()) &&
1235 pa_threaded_mainloop_start(loop) >= 0)
1237 pa_context *context;
1239 pa_threaded_mainloop_lock(loop);
1240 context = connect_context(loop, AL_FALSE);
1241 if(context)
1243 pa_operation *o;
1244 pa_stream_flags_t flags;
1245 pa_sample_spec spec;
1246 pa_stream *stream;
1248 flags = PA_STREAM_FIX_FORMAT | PA_STREAM_FIX_RATE |
1249 PA_STREAM_FIX_CHANNELS | PA_STREAM_DONT_MOVE;
1251 spec.format = PA_SAMPLE_S16NE;
1252 spec.rate = 44100;
1253 spec.channels = 1;
1255 stream = ALCpulseCapture_connectStream(NULL, loop, context, flags,
1256 NULL, &spec, NULL);
1257 if(stream)
1259 o = pa_context_get_source_info_by_name(context, pa_stream_get_device_name(stream),
1260 ALCpulseCapture_deviceCallback, loop);
1261 wait_for_operation(o, loop);
1263 pa_stream_disconnect(stream);
1264 pa_stream_unref(stream);
1265 stream = NULL;
1268 o = pa_context_get_source_info_list(context, ALCpulseCapture_deviceCallback, loop);
1269 wait_for_operation(o, loop);
1271 pa_context_disconnect(context);
1272 pa_context_unref(context);
1274 pa_threaded_mainloop_unlock(loop);
1275 pa_threaded_mainloop_stop(loop);
1277 if(loop)
1278 pa_threaded_mainloop_free(loop);
1282 static void ALCpulseCapture_contextStateCallback(pa_context *context, void *pdata)
1284 ALCpulseCapture *self = pdata;
1285 if(pa_context_get_state(context) == PA_CONTEXT_FAILED)
1287 ERR("Received context failure!\n");
1288 aluHandleDisconnect(STATIC_CAST(ALCbackend,self)->mDevice);
1290 pa_threaded_mainloop_signal(self->loop, 0);
1293 static void ALCpulseCapture_streamStateCallback(pa_stream *stream, void *pdata)
1295 ALCpulseCapture *self = pdata;
1296 if(pa_stream_get_state(stream) == PA_STREAM_FAILED)
1298 ERR("Received stream failure!\n");
1299 aluHandleDisconnect(STATIC_CAST(ALCbackend,self)->mDevice);
1301 pa_threaded_mainloop_signal(self->loop, 0);
1305 static void ALCpulseCapture_sourceNameCallback(pa_context *UNUSED(context), const pa_source_info *info, int eol, void *pdata)
1307 ALCpulseCapture *self = pdata;
1308 ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
1310 if(eol)
1312 pa_threaded_mainloop_signal(self->loop, 0);
1313 return;
1316 al_string_copy_cstr(&device->DeviceName, info->description);
1320 static void ALCpulseCapture_streamMovedCallback(pa_stream *stream, void *pdata)
1322 ALCpulseCapture *self = pdata;
1324 al_string_copy_cstr(&self->device_name, pa_stream_get_device_name(stream));
1326 TRACE("Stream moved to %s\n", al_string_get_cstr(self->device_name));
1330 static pa_stream *ALCpulseCapture_connectStream(const char *device_name,
1331 pa_threaded_mainloop *loop, pa_context *context,
1332 pa_stream_flags_t flags, pa_buffer_attr *attr, pa_sample_spec *spec,
1333 pa_channel_map *chanmap)
1335 pa_stream_state_t state;
1336 pa_stream *stream;
1338 stream = pa_stream_new_with_proplist(context, "Capture Stream", spec, chanmap, prop_filter);
1339 if(!stream)
1341 ERR("pa_stream_new_with_proplist() failed: %s\n", pa_strerror(pa_context_errno(context)));
1342 return NULL;
1345 pa_stream_set_state_callback(stream, stream_state_callback, loop);
1347 if(pa_stream_connect_record(stream, device_name, attr, flags) < 0)
1349 ERR("Stream did not connect: %s\n", pa_strerror(pa_context_errno(context)));
1350 pa_stream_unref(stream);
1351 return NULL;
1354 while((state=pa_stream_get_state(stream)) != PA_STREAM_READY)
1356 if(!PA_STREAM_IS_GOOD(state))
1358 ERR("Stream did not get ready: %s\n", pa_strerror(pa_context_errno(context)));
1359 pa_stream_unref(stream);
1360 return NULL;
1363 pa_threaded_mainloop_wait(loop);
1365 pa_stream_set_state_callback(stream, NULL, NULL);
1367 return stream;
1371 static ALCenum ALCpulseCapture_open(ALCpulseCapture *self, const ALCchar *name)
1373 ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
1374 const char *pulse_name = NULL;
1375 pa_stream_flags_t flags = 0;
1376 pa_channel_map chanmap;
1377 pa_operation *o;
1378 ALuint samples;
1380 if(name)
1382 const DevMap *iter;
1384 if(VECTOR_SIZE(CaptureDevices) == 0)
1385 ALCpulseCapture_probeDevices();
1387 #define MATCH_NAME(iter) (al_string_cmp_cstr((iter)->name, name) == 0)
1388 VECTOR_FIND_IF(iter, const DevMap, CaptureDevices, MATCH_NAME);
1389 #undef MATCH_NAME
1390 if(iter == VECTOR_ITER_END(CaptureDevices))
1391 return ALC_INVALID_VALUE;
1392 pulse_name = al_string_get_cstr(iter->device_name);
1395 if(!pulse_open(&self->loop, &self->context, ALCpulseCapture_contextStateCallback, self))
1396 return ALC_INVALID_VALUE;
1398 pa_threaded_mainloop_lock(self->loop);
1400 self->spec.rate = device->Frequency;
1401 self->spec.channels = ChannelsFromDevFmt(device->FmtChans);
1403 switch(device->FmtType)
1405 case DevFmtUByte:
1406 self->spec.format = PA_SAMPLE_U8;
1407 break;
1408 case DevFmtShort:
1409 self->spec.format = PA_SAMPLE_S16NE;
1410 break;
1411 case DevFmtInt:
1412 self->spec.format = PA_SAMPLE_S32NE;
1413 break;
1414 case DevFmtFloat:
1415 self->spec.format = PA_SAMPLE_FLOAT32NE;
1416 break;
1417 case DevFmtByte:
1418 case DevFmtUShort:
1419 case DevFmtUInt:
1420 ERR("%s capture samples not supported\n", DevFmtTypeString(device->FmtType));
1421 pa_threaded_mainloop_unlock(self->loop);
1422 goto fail;
1425 if(pa_sample_spec_valid(&self->spec) == 0)
1427 ERR("Invalid sample format\n");
1428 pa_threaded_mainloop_unlock(self->loop);
1429 goto fail;
1432 if(!pa_channel_map_init_auto(&chanmap, self->spec.channels, PA_CHANNEL_MAP_WAVEEX))
1434 ERR("Couldn't build map for channel count (%d)!\n", self->spec.channels);
1435 pa_threaded_mainloop_unlock(self->loop);
1436 goto fail;
1439 samples = device->UpdateSize * device->NumUpdates;
1440 samples = maxu(samples, 100 * device->Frequency / 1000);
1442 self->attr.minreq = -1;
1443 self->attr.prebuf = -1;
1444 self->attr.maxlength = samples * pa_frame_size(&self->spec);
1445 self->attr.tlength = -1;
1446 self->attr.fragsize = minu(samples, 50*device->Frequency/1000) *
1447 pa_frame_size(&self->spec);
1449 flags |= PA_STREAM_START_CORKED|PA_STREAM_ADJUST_LATENCY;
1450 if(!GetConfigValueBool("pulse", "allow-moves", 0))
1451 flags |= PA_STREAM_DONT_MOVE;
1453 TRACE("Connecting to \"%s\"\n", pulse_name ? pulse_name : "(default)");
1454 self->stream = ALCpulseCapture_connectStream(pulse_name, self->loop, self->context,
1455 flags, &self->attr, &self->spec,
1456 &chanmap);
1457 if(!self->stream)
1459 pa_threaded_mainloop_unlock(self->loop);
1460 goto fail;
1462 pa_stream_set_moved_callback(self->stream, ALCpulseCapture_streamMovedCallback, self);
1463 pa_stream_set_state_callback(self->stream, ALCpulseCapture_streamStateCallback, self);
1465 al_string_copy_cstr(&self->device_name, pa_stream_get_device_name(self->stream));
1466 o = pa_context_get_source_info_by_name(self->context,
1467 al_string_get_cstr(self->device_name),
1468 ALCpulseCapture_sourceNameCallback, self);
1469 wait_for_operation(o, self->loop);
1471 pa_threaded_mainloop_unlock(self->loop);
1472 return ALC_NO_ERROR;
1474 fail:
1475 pulse_close(self->loop, self->context, self->stream);
1476 self->loop = NULL;
1477 self->context = NULL;
1478 self->stream = NULL;
1480 return ALC_INVALID_VALUE;
1483 static void ALCpulseCapture_close(ALCpulseCapture *self)
1485 pulse_close(self->loop, self->context, self->stream);
1486 self->loop = NULL;
1487 self->context = NULL;
1488 self->stream = NULL;
1490 al_string_clear(&self->device_name);
1493 static ALCboolean ALCpulseCapture_start(ALCpulseCapture *self)
1495 pa_operation *o;
1496 o = pa_stream_cork(self->stream, 0, stream_success_callback, self->loop);
1497 wait_for_operation(o, self->loop);
1499 return ALC_TRUE;
1502 static void ALCpulseCapture_stop(ALCpulseCapture *self)
1504 pa_operation *o;
1505 o = pa_stream_cork(self->stream, 1, stream_success_callback, self->loop);
1506 wait_for_operation(o, self->loop);
1509 static ALCenum ALCpulseCapture_captureSamples(ALCpulseCapture *self, ALCvoid *buffer, ALCuint samples)
1511 ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
1512 ALCuint todo = samples * pa_frame_size(&self->spec);
1514 /* Capture is done in fragment-sized chunks, so we loop until we get all
1515 * that's available */
1516 self->last_readable -= todo;
1517 while(todo > 0)
1519 size_t rem = todo;
1521 if(self->cap_len == 0)
1523 pa_stream_state_t state;
1525 state = pa_stream_get_state(self->stream);
1526 if(!PA_STREAM_IS_GOOD(state))
1528 aluHandleDisconnect(device);
1529 break;
1531 if(pa_stream_peek(self->stream, &self->cap_store, &self->cap_len) < 0)
1533 ERR("pa_stream_peek() failed: %s\n",
1534 pa_strerror(pa_context_errno(self->context)));
1535 aluHandleDisconnect(device);
1536 break;
1538 self->cap_remain = self->cap_len;
1540 if(rem > self->cap_remain)
1541 rem = self->cap_remain;
1543 memcpy(buffer, self->cap_store, rem);
1545 buffer = (ALbyte*)buffer + rem;
1546 todo -= rem;
1548 self->cap_store = (ALbyte*)self->cap_store + rem;
1549 self->cap_remain -= rem;
1550 if(self->cap_remain == 0)
1552 pa_stream_drop(self->stream);
1553 self->cap_len = 0;
1556 if(todo > 0)
1557 memset(buffer, ((device->FmtType==DevFmtUByte) ? 0x80 : 0), todo);
1559 return ALC_NO_ERROR;
1562 static ALCuint ALCpulseCapture_availableSamples(ALCpulseCapture *self)
1564 ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
1565 size_t readable = self->cap_remain;
1567 if(device->Connected)
1569 ssize_t got = pa_stream_readable_size(self->stream);
1570 if(got < 0)
1572 ERR("pa_stream_readable_size() failed: %s\n", pa_strerror(got));
1573 aluHandleDisconnect(device);
1575 else if((size_t)got > self->cap_len)
1576 readable += got - self->cap_len;
1579 if(self->last_readable < readable)
1580 self->last_readable = readable;
1581 return self->last_readable / pa_frame_size(&self->spec);
1585 static ALint64 ALCpulseCapture_getLatency(ALCpulseCapture *self)
1587 pa_usec_t latency = 0;
1588 int neg;
1590 if(pa_stream_get_latency(self->stream, &latency, &neg) != 0)
1592 ERR("Failed to get stream latency!\n");
1593 return 0;
1596 if(neg) latency = 0;
1597 return (ALint64)minu64(latency, U64(0x7fffffffffffffff)/1000) * 1000;
1601 static void ALCpulseCapture_lock(ALCpulseCapture *self)
1603 pa_threaded_mainloop_lock(self->loop);
1606 static void ALCpulseCapture_unlock(ALCpulseCapture *self)
1608 pa_threaded_mainloop_unlock(self->loop);
1612 static inline void AppendAllDevicesList2(const DevMap *entry)
1613 { AppendAllDevicesList(al_string_get_cstr(entry->name)); }
1614 static inline void AppendCaptureDeviceList2(const DevMap *entry)
1615 { AppendCaptureDeviceList(al_string_get_cstr(entry->name)); }
1617 typedef struct ALCpulseBackendFactory {
1618 DERIVE_FROM_TYPE(ALCbackendFactory);
1619 } ALCpulseBackendFactory;
1620 #define ALCPULSEBACKENDFACTORY_INITIALIZER { { GET_VTABLE2(ALCpulseBackendFactory, ALCbackendFactory) } }
1622 static ALCboolean ALCpulseBackendFactory_init(ALCpulseBackendFactory *self);
1623 static void ALCpulseBackendFactory_deinit(ALCpulseBackendFactory *self);
1624 static ALCboolean ALCpulseBackendFactory_querySupport(ALCpulseBackendFactory *self, ALCbackend_Type type);
1625 static void ALCpulseBackendFactory_probe(ALCpulseBackendFactory *self, enum DevProbe type);
1626 static ALCbackend* ALCpulseBackendFactory_createBackend(ALCpulseBackendFactory *self, ALCdevice *device, ALCbackend_Type type);
1628 DEFINE_ALCBACKENDFACTORY_VTABLE(ALCpulseBackendFactory);
1631 static ALCboolean ALCpulseBackendFactory_init(ALCpulseBackendFactory* UNUSED(self))
1633 ALCboolean ret = ALC_FALSE;
1635 VECTOR_INIT(PlaybackDevices);
1636 VECTOR_INIT(CaptureDevices);
1638 if(pulse_load())
1640 pa_threaded_mainloop *loop;
1642 pulse_ctx_flags = 0;
1643 if(!GetConfigValueBool("pulse", "spawn-server", 1))
1644 pulse_ctx_flags |= PA_CONTEXT_NOAUTOSPAWN;
1646 if((loop=pa_threaded_mainloop_new()) &&
1647 pa_threaded_mainloop_start(loop) >= 0)
1649 pa_context *context;
1651 pa_threaded_mainloop_lock(loop);
1652 context = connect_context(loop, AL_TRUE);
1653 if(context)
1655 ret = ALC_TRUE;
1657 /* Some libraries (Phonon, Qt) set some pulseaudio properties
1658 * through environment variables, which causes all streams in
1659 * the process to inherit them. This attempts to filter those
1660 * properties out by setting them to 0-length data. */
1661 prop_filter = pa_proplist_new();
1662 pa_proplist_set(prop_filter, PA_PROP_MEDIA_ROLE, NULL, 0);
1663 pa_proplist_set(prop_filter, "phonon.streamid", NULL, 0);
1665 pa_context_disconnect(context);
1666 pa_context_unref(context);
1668 pa_threaded_mainloop_unlock(loop);
1669 pa_threaded_mainloop_stop(loop);
1671 if(loop)
1672 pa_threaded_mainloop_free(loop);
1675 return ret;
1678 static void ALCpulseBackendFactory_deinit(ALCpulseBackendFactory* UNUSED(self))
1680 clear_devlist(&PlaybackDevices);
1681 VECTOR_DEINIT(PlaybackDevices);
1683 clear_devlist(&CaptureDevices);
1684 VECTOR_DEINIT(CaptureDevices);
1686 if(prop_filter)
1687 pa_proplist_free(prop_filter);
1688 prop_filter = NULL;
1690 /* PulseAudio doesn't like being CloseLib'd sometimes */
1693 static ALCboolean ALCpulseBackendFactory_querySupport(ALCpulseBackendFactory* UNUSED(self), ALCbackend_Type type)
1695 if(type == ALCbackend_Playback || type == ALCbackend_Capture)
1696 return ALC_TRUE;
1697 return ALC_FALSE;
1700 static void ALCpulseBackendFactory_probe(ALCpulseBackendFactory* UNUSED(self), enum DevProbe type)
1702 switch(type)
1704 case ALL_DEVICE_PROBE:
1705 ALCpulsePlayback_probeDevices();
1706 VECTOR_FOR_EACH(const DevMap, PlaybackDevices, AppendAllDevicesList2);
1707 break;
1709 case CAPTURE_DEVICE_PROBE:
1710 ALCpulseCapture_probeDevices();
1711 VECTOR_FOR_EACH(const DevMap, CaptureDevices, AppendCaptureDeviceList2);
1712 break;
1716 static ALCbackend* ALCpulseBackendFactory_createBackend(ALCpulseBackendFactory* UNUSED(self), ALCdevice *device, ALCbackend_Type type)
1718 if(type == ALCbackend_Playback)
1720 ALCpulsePlayback *backend;
1722 backend = ALCpulsePlayback_New(sizeof(*backend));
1723 if(!backend) return NULL;
1724 memset(backend, 0, sizeof(*backend));
1726 ALCpulsePlayback_Construct(backend, device);
1728 return STATIC_CAST(ALCbackend, backend);
1730 if(type == ALCbackend_Capture)
1732 ALCpulseCapture *backend;
1734 backend = ALCpulseCapture_New(sizeof(*backend));
1735 if(!backend) return NULL;
1736 memset(backend, 0, sizeof(*backend));
1738 ALCpulseCapture_Construct(backend, device);
1740 return STATIC_CAST(ALCbackend, backend);
1743 return NULL;
1747 #else /* PA_API_VERSION == 12 */
1749 #warning "Unsupported API version, backend will be unavailable!"
1751 typedef struct ALCpulseBackendFactory {
1752 DERIVE_FROM_TYPE(ALCbackendFactory);
1753 } ALCpulseBackendFactory;
1754 #define ALCPULSEBACKENDFACTORY_INITIALIZER { { GET_VTABLE2(ALCpulseBackendFactory, ALCbackendFactory) } }
1756 static ALCboolean ALCpulseBackendFactory_init(ALCpulseBackendFactory* UNUSED(self))
1758 return ALC_FALSE;
1761 static void ALCpulseBackendFactory_deinit(ALCpulseBackendFactory* UNUSED(self))
1765 static ALCboolean ALCpulseBackendFactory_querySupport(ALCpulseBackendFactory* UNUSED(self), ALCbackend_Type UNUSED(type))
1767 return ALC_FALSE;
1770 static void ALCpulseBackendFactory_probe(ALCpulseBackendFactory* UNUSED(self), enum DevProbe UNUSED(type))
1774 static ALCbackend* ALCpulseBackendFactory_createBackend(ALCpulseBackendFactory* UNUSED(self), ALCdevice* UNUSED(device), ALCbackend_Type UNUSED(type))
1776 return NULL;
1779 DEFINE_ALCBACKENDFACTORY_VTABLE(ALCpulseBackendFactory);
1781 #endif /* PA_API_VERSION == 12 */
1783 ALCbackendFactory *ALCpulseBackendFactory_getFactory(void)
1785 static ALCpulseBackendFactory factory = ALCPULSEBACKENDFACTORY_INITIALIZER;
1786 return STATIC_CAST(ALCbackendFactory, &factory);