Set headphones when pulse reports using the headphones port
[openal-soft.git] / Alc / backends / pulseaudio.c
blob861bb01e2c5ea3dec991986e55d39931fd73c048
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 device->IsHeadphones = (device->FmtChans == DevFmtStereo && info->active_port &&
710 strcmp(info->active_port->name, "analog-output-headphones") == 0);
712 if(!chanmaps[i].str)
714 char chanmap_str[PA_CHANNEL_MAP_SNPRINT_MAX] = "";
715 pa_channel_map_snprint(chanmap_str, sizeof(chanmap_str), &info->channel_map);
716 WARN("Failed to find format for channel map:\n %s\n", chanmap_str);
720 static void ALCpulsePlayback_sinkNameCallback(pa_context *UNUSED(context), const pa_sink_info *info, int eol, void *pdata)
722 ALCpulsePlayback *self = pdata;
723 ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
725 if(eol)
727 pa_threaded_mainloop_signal(self->loop, 0);
728 return;
731 al_string_copy_cstr(&device->DeviceName, info->description);
735 static void ALCpulsePlayback_streamMovedCallback(pa_stream *stream, void *pdata)
737 ALCpulsePlayback *self = pdata;
739 al_string_copy_cstr(&self->device_name, pa_stream_get_device_name(stream));
741 TRACE("Stream moved to %s\n", al_string_get_cstr(self->device_name));
745 static pa_stream *ALCpulsePlayback_connectStream(const char *device_name,
746 pa_threaded_mainloop *loop, pa_context *context,
747 pa_stream_flags_t flags, pa_buffer_attr *attr, pa_sample_spec *spec,
748 pa_channel_map *chanmap)
750 pa_stream_state_t state;
751 pa_stream *stream;
753 stream = pa_stream_new_with_proplist(context, "Playback Stream", spec, chanmap, prop_filter);
754 if(!stream)
756 ERR("pa_stream_new_with_proplist() failed: %s\n", pa_strerror(pa_context_errno(context)));
757 return NULL;
760 pa_stream_set_state_callback(stream, stream_state_callback, loop);
762 if(pa_stream_connect_playback(stream, device_name, attr, flags, NULL, NULL) < 0)
764 ERR("Stream did not connect: %s\n", pa_strerror(pa_context_errno(context)));
765 pa_stream_unref(stream);
766 return NULL;
769 while((state=pa_stream_get_state(stream)) != PA_STREAM_READY)
771 if(!PA_STREAM_IS_GOOD(state))
773 ERR("Stream did not get ready: %s\n", pa_strerror(pa_context_errno(context)));
774 pa_stream_unref(stream);
775 return NULL;
778 pa_threaded_mainloop_wait(loop);
780 pa_stream_set_state_callback(stream, NULL, NULL);
782 return stream;
786 static int ALCpulsePlayback_mixerProc(void *ptr)
788 ALCpulsePlayback *self = ptr;
789 ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
790 ALuint buffer_size;
791 ALint update_size;
792 size_t frame_size;
793 ssize_t len;
795 SetRTPriority();
796 althrd_setname(althrd_current(), MIXER_THREAD_NAME);
798 pa_threaded_mainloop_lock(self->loop);
799 frame_size = pa_frame_size(&self->spec);
800 update_size = device->UpdateSize * frame_size;
802 /* Sanitize buffer metrics, in case we actually have less than what we
803 * asked for. */
804 buffer_size = minu(update_size*device->NumUpdates, self->attr.tlength);
805 update_size = minu(update_size, buffer_size/2);
806 do {
807 len = pa_stream_writable_size(self->stream) - self->attr.tlength +
808 buffer_size;
809 if(len < update_size)
811 if(pa_stream_is_corked(self->stream) == 1)
813 pa_operation *o;
814 o = pa_stream_cork(self->stream, 0, NULL, NULL);
815 if(o) pa_operation_unref(o);
817 pa_threaded_mainloop_wait(self->loop);
818 continue;
820 len -= len%update_size;
822 while(len > 0)
824 size_t newlen = len;
825 void *buf;
826 pa_free_cb_t free_func = NULL;
828 #if PA_CHECK_VERSION(0,9,16)
829 if(!pa_stream_begin_write ||
830 pa_stream_begin_write(self->stream, &buf, &newlen) < 0)
831 #endif
833 buf = pa_xmalloc(newlen);
834 free_func = pa_xfree;
837 aluMixData(device, buf, newlen/frame_size);
839 pa_stream_write(self->stream, buf, newlen, free_func, 0, PA_SEEK_RELATIVE);
840 len -= newlen;
842 } while(!self->killNow && device->Connected);
843 pa_threaded_mainloop_unlock(self->loop);
845 return 0;
849 static ALCenum ALCpulsePlayback_open(ALCpulsePlayback *self, const ALCchar *name)
851 const char *pulse_name = NULL;
852 pa_stream_flags_t flags;
853 pa_sample_spec spec;
854 pa_operation *o;
856 if(name)
858 const DevMap *iter;
860 if(VECTOR_SIZE(PlaybackDevices) == 0)
861 ALCpulsePlayback_probeDevices();
863 #define MATCH_NAME(iter) (al_string_cmp_cstr((iter)->name, name) == 0)
864 VECTOR_FIND_IF(iter, const DevMap, PlaybackDevices, MATCH_NAME);
865 #undef MATCH_NAME
866 if(iter == VECTOR_ITER_END(PlaybackDevices))
867 return ALC_INVALID_VALUE;
868 pulse_name = al_string_get_cstr(iter->device_name);
871 if(!pulse_open(&self->loop, &self->context, ALCpulsePlayback_contextStateCallback, self))
872 return ALC_INVALID_VALUE;
874 pa_threaded_mainloop_lock(self->loop);
876 flags = PA_STREAM_FIX_FORMAT | PA_STREAM_FIX_RATE |
877 PA_STREAM_FIX_CHANNELS;
878 if(!GetConfigValueBool("pulse", "allow-moves", 0))
879 flags |= PA_STREAM_DONT_MOVE;
881 spec.format = PA_SAMPLE_S16NE;
882 spec.rate = 44100;
883 spec.channels = 2;
885 TRACE("Connecting to \"%s\"\n", pulse_name ? pulse_name : "(default)");
886 self->stream = ALCpulsePlayback_connectStream(pulse_name, self->loop, self->context,
887 flags, NULL, &spec, NULL);
888 if(!self->stream)
890 pa_threaded_mainloop_unlock(self->loop);
891 pulse_close(self->loop, self->context, self->stream);
892 self->loop = NULL;
893 self->context = NULL;
894 return ALC_INVALID_VALUE;
896 pa_stream_set_moved_callback(self->stream, ALCpulsePlayback_streamMovedCallback, self);
898 al_string_copy_cstr(&self->device_name, pa_stream_get_device_name(self->stream));
899 o = pa_context_get_sink_info_by_name(self->context,
900 al_string_get_cstr(self->device_name),
901 ALCpulsePlayback_sinkNameCallback, self);
902 wait_for_operation(o, self->loop);
904 pa_threaded_mainloop_unlock(self->loop);
906 return ALC_NO_ERROR;
909 static void ALCpulsePlayback_close(ALCpulsePlayback *self)
911 pulse_close(self->loop, self->context, self->stream);
912 self->loop = NULL;
913 self->context = NULL;
914 self->stream = NULL;
916 al_string_clear(&self->device_name);
919 static ALCboolean ALCpulsePlayback_reset(ALCpulsePlayback *self)
921 ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
922 pa_stream_flags_t flags = 0;
923 const char *mapname = NULL;
924 pa_channel_map chanmap;
925 pa_operation *o;
926 ALuint len;
928 pa_threaded_mainloop_lock(self->loop);
930 if(self->stream)
932 pa_stream_set_moved_callback(self->stream, NULL, NULL);
933 #if PA_CHECK_VERSION(0,9,15)
934 if(pa_stream_set_buffer_attr_callback)
935 pa_stream_set_buffer_attr_callback(self->stream, NULL, NULL);
936 #endif
937 pa_stream_disconnect(self->stream);
938 pa_stream_unref(self->stream);
939 self->stream = NULL;
942 o = pa_context_get_sink_info_by_name(self->context, al_string_get_cstr(self->device_name),
943 ALCpulsePlayback_sinkInfoCallback, self);
944 wait_for_operation(o, self->loop);
946 if(!(device->Flags&DEVICE_FREQUENCY_REQUEST))
947 flags |= PA_STREAM_FIX_RATE;
948 flags |= PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE;
949 flags |= PA_STREAM_ADJUST_LATENCY;
950 flags |= PA_STREAM_START_CORKED;
951 if(!GetConfigValueBool("pulse", "allow-moves", 0))
952 flags |= PA_STREAM_DONT_MOVE;
954 switch(device->FmtType)
956 case DevFmtByte:
957 device->FmtType = DevFmtUByte;
958 /* fall-through */
959 case DevFmtUByte:
960 self->spec.format = PA_SAMPLE_U8;
961 break;
962 case DevFmtUShort:
963 device->FmtType = DevFmtShort;
964 /* fall-through */
965 case DevFmtShort:
966 self->spec.format = PA_SAMPLE_S16NE;
967 break;
968 case DevFmtUInt:
969 device->FmtType = DevFmtInt;
970 /* fall-through */
971 case DevFmtInt:
972 self->spec.format = PA_SAMPLE_S32NE;
973 break;
974 case DevFmtFloat:
975 self->spec.format = PA_SAMPLE_FLOAT32NE;
976 break;
978 self->spec.rate = device->Frequency;
979 self->spec.channels = ChannelsFromDevFmt(device->FmtChans);
981 if(pa_sample_spec_valid(&self->spec) == 0)
983 ERR("Invalid sample format\n");
984 pa_threaded_mainloop_unlock(self->loop);
985 return ALC_FALSE;
988 switch(device->FmtChans)
990 case DevFmtMono:
991 mapname = "mono";
992 break;
993 case DevFmtStereo:
994 mapname = "front-left,front-right";
995 break;
996 case DevFmtQuad:
997 mapname = "front-left,front-right,rear-left,rear-right";
998 break;
999 case DevFmtX51:
1000 mapname = "front-left,front-right,front-center,lfe,side-left,side-right";
1001 break;
1002 case DevFmtX51Rear:
1003 mapname = "front-left,front-right,front-center,lfe,rear-left,rear-right";
1004 break;
1005 case DevFmtX61:
1006 mapname = "front-left,front-right,front-center,lfe,rear-center,side-left,side-right";
1007 break;
1008 case DevFmtX71:
1009 mapname = "front-left,front-right,front-center,lfe,rear-left,rear-right,side-left,side-right";
1010 break;
1012 if(!pa_channel_map_parse(&chanmap, mapname))
1014 ERR("Failed to build channel map for %s\n", DevFmtChannelsString(device->FmtChans));
1015 pa_threaded_mainloop_unlock(self->loop);
1016 return ALC_FALSE;
1018 SetDefaultWFXChannelOrder(device);
1020 self->attr.fragsize = -1;
1021 self->attr.prebuf = 0;
1022 self->attr.minreq = device->UpdateSize * pa_frame_size(&self->spec);
1023 self->attr.tlength = self->attr.minreq * maxu(device->NumUpdates, 2);
1024 self->attr.maxlength = -1;
1026 self->stream = ALCpulsePlayback_connectStream(al_string_get_cstr(self->device_name),
1027 self->loop, self->context, flags,
1028 &self->attr, &self->spec, &chanmap);
1029 if(!self->stream)
1031 pa_threaded_mainloop_unlock(self->loop);
1032 return ALC_FALSE;
1034 pa_stream_set_state_callback(self->stream, ALCpulsePlayback_streamStateCallback, self);
1035 pa_stream_set_moved_callback(self->stream, ALCpulsePlayback_streamMovedCallback, self);
1036 pa_stream_set_write_callback(self->stream, ALCpulsePlayback_streamWriteCallback, self);
1038 self->spec = *(pa_stream_get_sample_spec(self->stream));
1039 if(device->Frequency != self->spec.rate)
1041 /* Server updated our playback rate, so modify the buffer attribs
1042 * accordingly. */
1043 device->NumUpdates = (ALuint)((ALdouble)device->NumUpdates / device->Frequency *
1044 self->spec.rate + 0.5);
1045 self->attr.minreq = device->UpdateSize * pa_frame_size(&self->spec);
1046 self->attr.tlength = self->attr.minreq * clampu(device->NumUpdates, 2, 16);
1047 self->attr.maxlength = -1;
1048 self->attr.prebuf = 0;
1050 o = pa_stream_set_buffer_attr(self->stream, &self->attr,
1051 stream_success_callback, self->loop);
1052 wait_for_operation(o, self->loop);
1054 device->Frequency = self->spec.rate;
1057 #if PA_CHECK_VERSION(0,9,15)
1058 if(pa_stream_set_buffer_attr_callback)
1059 pa_stream_set_buffer_attr_callback(self->stream, ALCpulsePlayback_bufferAttrCallback, self);
1060 #endif
1061 ALCpulsePlayback_bufferAttrCallback(self->stream, self);
1063 len = self->attr.minreq / pa_frame_size(&self->spec);
1064 device->NumUpdates = (ALuint)((ALdouble)device->NumUpdates/len*device->UpdateSize + 0.5);
1065 device->NumUpdates = clampu(device->NumUpdates, 2, 16);
1066 device->UpdateSize = len;
1068 pa_threaded_mainloop_unlock(self->loop);
1069 return ALC_TRUE;
1072 static ALCboolean ALCpulsePlayback_start(ALCpulsePlayback *self)
1074 self->killNow = AL_FALSE;
1075 if(althrd_create(&self->thread, ALCpulsePlayback_mixerProc, self) != althrd_success)
1076 return ALC_FALSE;
1077 return ALC_TRUE;
1080 static void ALCpulsePlayback_stop(ALCpulsePlayback *self)
1082 pa_operation *o;
1083 int res;
1085 if(!self->stream)
1086 return;
1088 self->killNow = AL_TRUE;
1089 althrd_join(self->thread, &res);
1091 pa_threaded_mainloop_lock(self->loop);
1093 o = pa_stream_cork(self->stream, 1, stream_success_callback, self->loop);
1094 wait_for_operation(o, self->loop);
1096 pa_threaded_mainloop_unlock(self->loop);
1100 static ALint64 ALCpulsePlayback_getLatency(ALCpulsePlayback *self)
1102 pa_usec_t latency = 0;
1103 int neg, err;
1105 if((err=pa_stream_get_latency(self->stream, &latency, &neg)) != 0)
1107 /* FIXME: if err = -PA_ERR_NODATA, it means we were called too soon
1108 * after starting the stream and no timing info has been received from
1109 * the server yet. Should we wait, possibly stalling the app, or give a
1110 * dummy value? Either way, it shouldn't be 0. */
1111 if(err != -PA_ERR_NODATA)
1112 ERR("Failed to get stream latency: 0x%x\n", err);
1113 return 0;
1116 if(neg) latency = 0;
1117 return (ALint64)minu64(latency, U64(0x7fffffffffffffff)/1000) * 1000;
1121 static void ALCpulsePlayback_lock(ALCpulsePlayback *self)
1123 pa_threaded_mainloop_lock(self->loop);
1126 static void ALCpulsePlayback_unlock(ALCpulsePlayback *self)
1128 pa_threaded_mainloop_unlock(self->loop);
1132 typedef struct ALCpulseCapture {
1133 DERIVE_FROM_TYPE(ALCbackend);
1135 al_string device_name;
1137 const void *cap_store;
1138 size_t cap_len;
1139 size_t cap_remain;
1141 ALCuint last_readable;
1143 pa_buffer_attr attr;
1144 pa_sample_spec spec;
1146 pa_threaded_mainloop *loop;
1148 pa_stream *stream;
1149 pa_context *context;
1150 } ALCpulseCapture;
1152 static void ALCpulseCapture_deviceCallback(pa_context *context, const pa_source_info *info, int eol, void *pdata);
1153 static void ALCpulseCapture_probeDevices(void);
1155 static void ALCpulseCapture_contextStateCallback(pa_context *context, void *pdata);
1156 static void ALCpulseCapture_streamStateCallback(pa_stream *stream, void *pdata);
1157 static void ALCpulseCapture_sourceNameCallback(pa_context *context, const pa_source_info *info, int eol, void *pdata);
1158 static void ALCpulseCapture_streamMovedCallback(pa_stream *stream, void *pdata);
1159 static pa_stream *ALCpulseCapture_connectStream(const char *device_name,
1160 pa_threaded_mainloop *loop, pa_context *context,
1161 pa_stream_flags_t flags, pa_buffer_attr *attr,
1162 pa_sample_spec *spec, pa_channel_map *chanmap);
1164 static void ALCpulseCapture_Construct(ALCpulseCapture *self, ALCdevice *device);
1165 static void ALCpulseCapture_Destruct(ALCpulseCapture *self);
1166 static ALCenum ALCpulseCapture_open(ALCpulseCapture *self, const ALCchar *name);
1167 static void ALCpulseCapture_close(ALCpulseCapture *self);
1168 static DECLARE_FORWARD(ALCpulseCapture, ALCbackend, ALCboolean, reset)
1169 static ALCboolean ALCpulseCapture_start(ALCpulseCapture *self);
1170 static void ALCpulseCapture_stop(ALCpulseCapture *self);
1171 static ALCenum ALCpulseCapture_captureSamples(ALCpulseCapture *self, ALCvoid *buffer, ALCuint samples);
1172 static ALCuint ALCpulseCapture_availableSamples(ALCpulseCapture *self);
1173 static ALint64 ALCpulseCapture_getLatency(ALCpulseCapture *self);
1174 static void ALCpulseCapture_lock(ALCpulseCapture *self);
1175 static void ALCpulseCapture_unlock(ALCpulseCapture *self);
1176 DECLARE_DEFAULT_ALLOCATORS(ALCpulseCapture)
1178 DEFINE_ALCBACKEND_VTABLE(ALCpulseCapture);
1181 static void ALCpulseCapture_Construct(ALCpulseCapture *self, ALCdevice *device)
1183 ALCbackend_Construct(STATIC_CAST(ALCbackend, self), device);
1184 SET_VTABLE2(ALCpulseCapture, ALCbackend, self);
1186 AL_STRING_INIT(self->device_name);
1189 static void ALCpulseCapture_Destruct(ALCpulseCapture *self)
1191 AL_STRING_DEINIT(self->device_name);
1192 ALCbackend_Destruct(STATIC_CAST(ALCbackend, self));
1196 static void ALCpulseCapture_deviceCallback(pa_context *UNUSED(context), const pa_source_info *info, int eol, void *pdata)
1198 pa_threaded_mainloop *loop = pdata;
1199 const DevMap *iter;
1200 DevMap entry;
1202 if(eol)
1204 pa_threaded_mainloop_signal(loop, 0);
1205 return;
1208 #define MATCH_INFO_NAME(iter) (al_string_cmp_cstr((iter)->device_name, info->name) == 0)
1209 VECTOR_FIND_IF(iter, const DevMap, CaptureDevices, MATCH_INFO_NAME);
1210 #undef MATCH_INFO_NAME
1211 if(iter != VECTOR_ITER_END(CaptureDevices))
1212 return;
1214 TRACE("Got device \"%s\", \"%s\"\n", info->description, info->name);
1216 AL_STRING_INIT(entry.name);
1217 AL_STRING_INIT(entry.device_name);
1219 al_string_copy_cstr(&entry.name, info->description);
1220 al_string_copy_cstr(&entry.device_name, info->name);
1222 VECTOR_PUSH_BACK(CaptureDevices, entry);
1225 static void ALCpulseCapture_probeDevices(void)
1227 pa_threaded_mainloop *loop;
1229 clear_devlist(&CaptureDevices);
1231 if((loop=pa_threaded_mainloop_new()) &&
1232 pa_threaded_mainloop_start(loop) >= 0)
1234 pa_context *context;
1236 pa_threaded_mainloop_lock(loop);
1237 context = connect_context(loop, AL_FALSE);
1238 if(context)
1240 pa_operation *o;
1241 pa_stream_flags_t flags;
1242 pa_sample_spec spec;
1243 pa_stream *stream;
1245 flags = PA_STREAM_FIX_FORMAT | PA_STREAM_FIX_RATE |
1246 PA_STREAM_FIX_CHANNELS | PA_STREAM_DONT_MOVE;
1248 spec.format = PA_SAMPLE_S16NE;
1249 spec.rate = 44100;
1250 spec.channels = 1;
1252 stream = ALCpulseCapture_connectStream(NULL, loop, context, flags,
1253 NULL, &spec, NULL);
1254 if(stream)
1256 o = pa_context_get_source_info_by_name(context, pa_stream_get_device_name(stream),
1257 ALCpulseCapture_deviceCallback, loop);
1258 wait_for_operation(o, loop);
1260 pa_stream_disconnect(stream);
1261 pa_stream_unref(stream);
1262 stream = NULL;
1265 o = pa_context_get_source_info_list(context, ALCpulseCapture_deviceCallback, loop);
1266 wait_for_operation(o, loop);
1268 pa_context_disconnect(context);
1269 pa_context_unref(context);
1271 pa_threaded_mainloop_unlock(loop);
1272 pa_threaded_mainloop_stop(loop);
1274 if(loop)
1275 pa_threaded_mainloop_free(loop);
1279 static void ALCpulseCapture_contextStateCallback(pa_context *context, void *pdata)
1281 ALCpulseCapture *self = pdata;
1282 if(pa_context_get_state(context) == PA_CONTEXT_FAILED)
1284 ERR("Received context failure!\n");
1285 aluHandleDisconnect(STATIC_CAST(ALCbackend,self)->mDevice);
1287 pa_threaded_mainloop_signal(self->loop, 0);
1290 static void ALCpulseCapture_streamStateCallback(pa_stream *stream, void *pdata)
1292 ALCpulseCapture *self = pdata;
1293 if(pa_stream_get_state(stream) == PA_STREAM_FAILED)
1295 ERR("Received stream failure!\n");
1296 aluHandleDisconnect(STATIC_CAST(ALCbackend,self)->mDevice);
1298 pa_threaded_mainloop_signal(self->loop, 0);
1302 static void ALCpulseCapture_sourceNameCallback(pa_context *UNUSED(context), const pa_source_info *info, int eol, void *pdata)
1304 ALCpulseCapture *self = pdata;
1305 ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
1307 if(eol)
1309 pa_threaded_mainloop_signal(self->loop, 0);
1310 return;
1313 al_string_copy_cstr(&device->DeviceName, info->description);
1317 static void ALCpulseCapture_streamMovedCallback(pa_stream *stream, void *pdata)
1319 ALCpulseCapture *self = pdata;
1321 al_string_copy_cstr(&self->device_name, pa_stream_get_device_name(stream));
1323 TRACE("Stream moved to %s\n", al_string_get_cstr(self->device_name));
1327 static pa_stream *ALCpulseCapture_connectStream(const char *device_name,
1328 pa_threaded_mainloop *loop, pa_context *context,
1329 pa_stream_flags_t flags, pa_buffer_attr *attr, pa_sample_spec *spec,
1330 pa_channel_map *chanmap)
1332 pa_stream_state_t state;
1333 pa_stream *stream;
1335 stream = pa_stream_new_with_proplist(context, "Capture Stream", spec, chanmap, prop_filter);
1336 if(!stream)
1338 ERR("pa_stream_new_with_proplist() failed: %s\n", pa_strerror(pa_context_errno(context)));
1339 return NULL;
1342 pa_stream_set_state_callback(stream, stream_state_callback, loop);
1344 if(pa_stream_connect_record(stream, device_name, attr, flags) < 0)
1346 ERR("Stream did not connect: %s\n", pa_strerror(pa_context_errno(context)));
1347 pa_stream_unref(stream);
1348 return NULL;
1351 while((state=pa_stream_get_state(stream)) != PA_STREAM_READY)
1353 if(!PA_STREAM_IS_GOOD(state))
1355 ERR("Stream did not get ready: %s\n", pa_strerror(pa_context_errno(context)));
1356 pa_stream_unref(stream);
1357 return NULL;
1360 pa_threaded_mainloop_wait(loop);
1362 pa_stream_set_state_callback(stream, NULL, NULL);
1364 return stream;
1368 static ALCenum ALCpulseCapture_open(ALCpulseCapture *self, const ALCchar *name)
1370 ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
1371 const char *pulse_name = NULL;
1372 pa_stream_flags_t flags = 0;
1373 pa_channel_map chanmap;
1374 pa_operation *o;
1375 ALuint samples;
1377 if(name)
1379 const DevMap *iter;
1381 if(VECTOR_SIZE(CaptureDevices) == 0)
1382 ALCpulseCapture_probeDevices();
1384 #define MATCH_NAME(iter) (al_string_cmp_cstr((iter)->name, name) == 0)
1385 VECTOR_FIND_IF(iter, const DevMap, CaptureDevices, MATCH_NAME);
1386 #undef MATCH_NAME
1387 if(iter == VECTOR_ITER_END(CaptureDevices))
1388 return ALC_INVALID_VALUE;
1389 pulse_name = al_string_get_cstr(iter->device_name);
1392 if(!pulse_open(&self->loop, &self->context, ALCpulseCapture_contextStateCallback, self))
1393 return ALC_INVALID_VALUE;
1395 pa_threaded_mainloop_lock(self->loop);
1397 self->spec.rate = device->Frequency;
1398 self->spec.channels = ChannelsFromDevFmt(device->FmtChans);
1400 switch(device->FmtType)
1402 case DevFmtUByte:
1403 self->spec.format = PA_SAMPLE_U8;
1404 break;
1405 case DevFmtShort:
1406 self->spec.format = PA_SAMPLE_S16NE;
1407 break;
1408 case DevFmtInt:
1409 self->spec.format = PA_SAMPLE_S32NE;
1410 break;
1411 case DevFmtFloat:
1412 self->spec.format = PA_SAMPLE_FLOAT32NE;
1413 break;
1414 case DevFmtByte:
1415 case DevFmtUShort:
1416 case DevFmtUInt:
1417 ERR("%s capture samples not supported\n", DevFmtTypeString(device->FmtType));
1418 pa_threaded_mainloop_unlock(self->loop);
1419 goto fail;
1422 if(pa_sample_spec_valid(&self->spec) == 0)
1424 ERR("Invalid sample format\n");
1425 pa_threaded_mainloop_unlock(self->loop);
1426 goto fail;
1429 if(!pa_channel_map_init_auto(&chanmap, self->spec.channels, PA_CHANNEL_MAP_WAVEEX))
1431 ERR("Couldn't build map for channel count (%d)!\n", self->spec.channels);
1432 pa_threaded_mainloop_unlock(self->loop);
1433 goto fail;
1436 samples = device->UpdateSize * device->NumUpdates;
1437 samples = maxu(samples, 100 * device->Frequency / 1000);
1439 self->attr.minreq = -1;
1440 self->attr.prebuf = -1;
1441 self->attr.maxlength = samples * pa_frame_size(&self->spec);
1442 self->attr.tlength = -1;
1443 self->attr.fragsize = minu(samples, 50*device->Frequency/1000) *
1444 pa_frame_size(&self->spec);
1446 flags |= PA_STREAM_START_CORKED|PA_STREAM_ADJUST_LATENCY;
1447 if(!GetConfigValueBool("pulse", "allow-moves", 0))
1448 flags |= PA_STREAM_DONT_MOVE;
1450 TRACE("Connecting to \"%s\"\n", pulse_name ? pulse_name : "(default)");
1451 self->stream = ALCpulseCapture_connectStream(pulse_name, self->loop, self->context,
1452 flags, &self->attr, &self->spec,
1453 &chanmap);
1454 if(!self->stream)
1456 pa_threaded_mainloop_unlock(self->loop);
1457 goto fail;
1459 pa_stream_set_moved_callback(self->stream, ALCpulseCapture_streamMovedCallback, self);
1460 pa_stream_set_state_callback(self->stream, ALCpulseCapture_streamStateCallback, self);
1462 al_string_copy_cstr(&self->device_name, pa_stream_get_device_name(self->stream));
1463 o = pa_context_get_source_info_by_name(self->context,
1464 al_string_get_cstr(self->device_name),
1465 ALCpulseCapture_sourceNameCallback, self);
1466 wait_for_operation(o, self->loop);
1468 pa_threaded_mainloop_unlock(self->loop);
1469 return ALC_NO_ERROR;
1471 fail:
1472 pulse_close(self->loop, self->context, self->stream);
1473 self->loop = NULL;
1474 self->context = NULL;
1475 self->stream = NULL;
1477 return ALC_INVALID_VALUE;
1480 static void ALCpulseCapture_close(ALCpulseCapture *self)
1482 pulse_close(self->loop, self->context, self->stream);
1483 self->loop = NULL;
1484 self->context = NULL;
1485 self->stream = NULL;
1487 al_string_clear(&self->device_name);
1490 static ALCboolean ALCpulseCapture_start(ALCpulseCapture *self)
1492 pa_operation *o;
1493 o = pa_stream_cork(self->stream, 0, stream_success_callback, self->loop);
1494 wait_for_operation(o, self->loop);
1496 return ALC_TRUE;
1499 static void ALCpulseCapture_stop(ALCpulseCapture *self)
1501 pa_operation *o;
1502 o = pa_stream_cork(self->stream, 1, stream_success_callback, self->loop);
1503 wait_for_operation(o, self->loop);
1506 static ALCenum ALCpulseCapture_captureSamples(ALCpulseCapture *self, ALCvoid *buffer, ALCuint samples)
1508 ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
1509 ALCuint todo = samples * pa_frame_size(&self->spec);
1511 /* Capture is done in fragment-sized chunks, so we loop until we get all
1512 * that's available */
1513 self->last_readable -= todo;
1514 while(todo > 0)
1516 size_t rem = todo;
1518 if(self->cap_len == 0)
1520 pa_stream_state_t state;
1522 state = pa_stream_get_state(self->stream);
1523 if(!PA_STREAM_IS_GOOD(state))
1525 aluHandleDisconnect(device);
1526 break;
1528 if(pa_stream_peek(self->stream, &self->cap_store, &self->cap_len) < 0)
1530 ERR("pa_stream_peek() failed: %s\n",
1531 pa_strerror(pa_context_errno(self->context)));
1532 aluHandleDisconnect(device);
1533 break;
1535 self->cap_remain = self->cap_len;
1537 if(rem > self->cap_remain)
1538 rem = self->cap_remain;
1540 memcpy(buffer, self->cap_store, rem);
1542 buffer = (ALbyte*)buffer + rem;
1543 todo -= rem;
1545 self->cap_store = (ALbyte*)self->cap_store + rem;
1546 self->cap_remain -= rem;
1547 if(self->cap_remain == 0)
1549 pa_stream_drop(self->stream);
1550 self->cap_len = 0;
1553 if(todo > 0)
1554 memset(buffer, ((device->FmtType==DevFmtUByte) ? 0x80 : 0), todo);
1556 return ALC_NO_ERROR;
1559 static ALCuint ALCpulseCapture_availableSamples(ALCpulseCapture *self)
1561 ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
1562 size_t readable = self->cap_remain;
1564 if(device->Connected)
1566 ssize_t got = pa_stream_readable_size(self->stream);
1567 if(got < 0)
1569 ERR("pa_stream_readable_size() failed: %s\n", pa_strerror(got));
1570 aluHandleDisconnect(device);
1572 else if((size_t)got > self->cap_len)
1573 readable += got - self->cap_len;
1576 if(self->last_readable < readable)
1577 self->last_readable = readable;
1578 return self->last_readable / pa_frame_size(&self->spec);
1582 static ALint64 ALCpulseCapture_getLatency(ALCpulseCapture *self)
1584 pa_usec_t latency = 0;
1585 int neg;
1587 if(pa_stream_get_latency(self->stream, &latency, &neg) != 0)
1589 ERR("Failed to get stream latency!\n");
1590 return 0;
1593 if(neg) latency = 0;
1594 return (ALint64)minu64(latency, U64(0x7fffffffffffffff)/1000) * 1000;
1598 static void ALCpulseCapture_lock(ALCpulseCapture *self)
1600 pa_threaded_mainloop_lock(self->loop);
1603 static void ALCpulseCapture_unlock(ALCpulseCapture *self)
1605 pa_threaded_mainloop_unlock(self->loop);
1609 static inline void AppendAllDevicesList2(const DevMap *entry)
1610 { AppendAllDevicesList(al_string_get_cstr(entry->name)); }
1611 static inline void AppendCaptureDeviceList2(const DevMap *entry)
1612 { AppendCaptureDeviceList(al_string_get_cstr(entry->name)); }
1614 typedef struct ALCpulseBackendFactory {
1615 DERIVE_FROM_TYPE(ALCbackendFactory);
1616 } ALCpulseBackendFactory;
1617 #define ALCPULSEBACKENDFACTORY_INITIALIZER { { GET_VTABLE2(ALCpulseBackendFactory, ALCbackendFactory) } }
1619 static ALCboolean ALCpulseBackendFactory_init(ALCpulseBackendFactory *self);
1620 static void ALCpulseBackendFactory_deinit(ALCpulseBackendFactory *self);
1621 static ALCboolean ALCpulseBackendFactory_querySupport(ALCpulseBackendFactory *self, ALCbackend_Type type);
1622 static void ALCpulseBackendFactory_probe(ALCpulseBackendFactory *self, enum DevProbe type);
1623 static ALCbackend* ALCpulseBackendFactory_createBackend(ALCpulseBackendFactory *self, ALCdevice *device, ALCbackend_Type type);
1625 DEFINE_ALCBACKENDFACTORY_VTABLE(ALCpulseBackendFactory);
1628 static ALCboolean ALCpulseBackendFactory_init(ALCpulseBackendFactory* UNUSED(self))
1630 ALCboolean ret = ALC_FALSE;
1632 VECTOR_INIT(PlaybackDevices);
1633 VECTOR_INIT(CaptureDevices);
1635 if(pulse_load())
1637 pa_threaded_mainloop *loop;
1639 pulse_ctx_flags = 0;
1640 if(!GetConfigValueBool("pulse", "spawn-server", 1))
1641 pulse_ctx_flags |= PA_CONTEXT_NOAUTOSPAWN;
1643 if((loop=pa_threaded_mainloop_new()) &&
1644 pa_threaded_mainloop_start(loop) >= 0)
1646 pa_context *context;
1648 pa_threaded_mainloop_lock(loop);
1649 context = connect_context(loop, AL_TRUE);
1650 if(context)
1652 ret = ALC_TRUE;
1654 /* Some libraries (Phonon, Qt) set some pulseaudio properties
1655 * through environment variables, which causes all streams in
1656 * the process to inherit them. This attempts to filter those
1657 * properties out by setting them to 0-length data. */
1658 prop_filter = pa_proplist_new();
1659 pa_proplist_set(prop_filter, PA_PROP_MEDIA_ROLE, NULL, 0);
1660 pa_proplist_set(prop_filter, "phonon.streamid", NULL, 0);
1662 pa_context_disconnect(context);
1663 pa_context_unref(context);
1665 pa_threaded_mainloop_unlock(loop);
1666 pa_threaded_mainloop_stop(loop);
1668 if(loop)
1669 pa_threaded_mainloop_free(loop);
1672 return ret;
1675 static void ALCpulseBackendFactory_deinit(ALCpulseBackendFactory* UNUSED(self))
1677 clear_devlist(&PlaybackDevices);
1678 VECTOR_DEINIT(PlaybackDevices);
1680 clear_devlist(&CaptureDevices);
1681 VECTOR_DEINIT(CaptureDevices);
1683 if(prop_filter)
1684 pa_proplist_free(prop_filter);
1685 prop_filter = NULL;
1687 /* PulseAudio doesn't like being CloseLib'd sometimes */
1690 static ALCboolean ALCpulseBackendFactory_querySupport(ALCpulseBackendFactory* UNUSED(self), ALCbackend_Type type)
1692 if(type == ALCbackend_Playback || type == ALCbackend_Capture)
1693 return ALC_TRUE;
1694 return ALC_FALSE;
1697 static void ALCpulseBackendFactory_probe(ALCpulseBackendFactory* UNUSED(self), enum DevProbe type)
1699 switch(type)
1701 case ALL_DEVICE_PROBE:
1702 ALCpulsePlayback_probeDevices();
1703 VECTOR_FOR_EACH(const DevMap, PlaybackDevices, AppendAllDevicesList2);
1704 break;
1706 case CAPTURE_DEVICE_PROBE:
1707 ALCpulseCapture_probeDevices();
1708 VECTOR_FOR_EACH(const DevMap, CaptureDevices, AppendCaptureDeviceList2);
1709 break;
1713 static ALCbackend* ALCpulseBackendFactory_createBackend(ALCpulseBackendFactory* UNUSED(self), ALCdevice *device, ALCbackend_Type type)
1715 if(type == ALCbackend_Playback)
1717 ALCpulsePlayback *backend;
1719 backend = ALCpulsePlayback_New(sizeof(*backend));
1720 if(!backend) return NULL;
1721 memset(backend, 0, sizeof(*backend));
1723 ALCpulsePlayback_Construct(backend, device);
1725 return STATIC_CAST(ALCbackend, backend);
1727 if(type == ALCbackend_Capture)
1729 ALCpulseCapture *backend;
1731 backend = ALCpulseCapture_New(sizeof(*backend));
1732 if(!backend) return NULL;
1733 memset(backend, 0, sizeof(*backend));
1735 ALCpulseCapture_Construct(backend, device);
1737 return STATIC_CAST(ALCbackend, backend);
1740 return NULL;
1744 #else /* PA_API_VERSION == 12 */
1746 #warning "Unsupported API version, backend will be unavailable!"
1748 typedef struct ALCpulseBackendFactory {
1749 DERIVE_FROM_TYPE(ALCbackendFactory);
1750 } ALCpulseBackendFactory;
1751 #define ALCPULSEBACKENDFACTORY_INITIALIZER { { GET_VTABLE2(ALCpulseBackendFactory, ALCbackendFactory) } }
1753 static ALCboolean ALCpulseBackendFactory_init(ALCpulseBackendFactory* UNUSED(self))
1755 return ALC_FALSE;
1758 static void ALCpulseBackendFactory_deinit(ALCpulseBackendFactory* UNUSED(self))
1762 static ALCboolean ALCpulseBackendFactory_querySupport(ALCpulseBackendFactory* UNUSED(self), ALCbackend_Type UNUSED(type))
1764 return ALC_FALSE;
1767 static void ALCpulseBackendFactory_probe(ALCpulseBackendFactory* UNUSED(self), enum DevProbe UNUSED(type))
1771 static ALCbackend* ALCpulseBackendFactory_createBackend(ALCpulseBackendFactory* UNUSED(self), ALCdevice* UNUSED(device), ALCbackend_Type UNUSED(type))
1773 return NULL;
1776 DEFINE_ALCBACKENDFACTORY_VTABLE(ALCpulseBackendFactory);
1778 #endif /* PA_API_VERSION == 12 */
1780 ALCbackendFactory *ALCpulseBackendFactory_getFactory(void)
1782 static ALCpulseBackendFactory factory = ALCPULSEBACKENDFACTORY_INITIALIZER;
1783 return STATIC_CAST(ALCbackendFactory, &factory);