Simplify setting a fontsound link
[openal-soft.git] / Alc / backends / pulseaudio.c
blob582522407b9dd52e7d97c455d6e8cefe096826e5
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., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, 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_moved_callback(stream, NULL, NULL);
439 #if PA_CHECK_VERSION(0,9,15)
440 if(pa_stream_set_buffer_attr_callback)
441 pa_stream_set_buffer_attr_callback(stream, NULL, NULL);
442 #endif
443 pa_stream_disconnect(stream);
444 pa_stream_unref(stream);
447 pa_context_disconnect(context);
448 pa_context_unref(context);
450 pa_threaded_mainloop_unlock(loop);
452 pa_threaded_mainloop_stop(loop);
453 pa_threaded_mainloop_free(loop);
457 typedef struct {
458 al_string name;
459 al_string device_name;
460 } DevMap;
461 DECL_VECTOR(DevMap)
463 static vector_DevMap PlaybackDevices;
464 static vector_DevMap CaptureDevices;
466 static void clear_devlist(vector_DevMap *list)
468 DevMap *iter, *end;
470 iter = VECTOR_ITER_BEGIN(*list);
471 end = VECTOR_ITER_END(*list);
472 for(;iter != end;iter++)
474 AL_STRING_DEINIT(iter->name);
475 AL_STRING_DEINIT(iter->device_name);
477 VECTOR_RESIZE(*list, 0);
481 typedef struct ALCpulsePlayback {
482 DERIVE_FROM_TYPE(ALCbackend);
484 al_string device_name;
486 pa_buffer_attr attr;
487 pa_sample_spec spec;
489 pa_threaded_mainloop *loop;
491 pa_stream *stream;
492 pa_context *context;
494 volatile ALboolean killNow;
495 althrd_t thread;
496 } ALCpulsePlayback;
498 static void ALCpulsePlayback_deviceCallback(pa_context *context, const pa_sink_info *info, int eol, void *pdata);
499 static void ALCpulsePlayback_probeDevices(void);
501 static void ALCpulsePlayback_bufferAttrCallback(pa_stream *stream, void *pdata);
502 static void ALCpulsePlayback_contextStateCallback(pa_context *context, void *pdata);
503 static void ALCpulsePlayback_streamStateCallback(pa_stream *stream, void *pdata);
504 static void ALCpulsePlayback_sinkInfoCallback(pa_context *context, const pa_sink_info *info, int eol, void *pdata);
505 static void ALCpulsePlayback_sinkNameCallback(pa_context *context, const pa_sink_info *info, int eol, void *pdata);
506 static void ALCpulsePlayback_streamMovedCallback(pa_stream *stream, void *pdata);
507 static pa_stream *ALCpulsePlayback_connectStream(const char *device_name, pa_threaded_mainloop *loop,
508 pa_context *context, pa_stream_flags_t flags,
509 pa_buffer_attr *attr, pa_sample_spec *spec,
510 pa_channel_map *chanmap);
511 static int ALCpulsePlayback_mixerProc(void *ptr);
513 static void ALCpulsePlayback_Construct(ALCpulsePlayback *self, ALCdevice *device);
514 static void ALCpulsePlayback_Destruct(ALCpulsePlayback *self);
515 static ALCenum ALCpulsePlayback_open(ALCpulsePlayback *self, const ALCchar *name);
516 static void ALCpulsePlayback_close(ALCpulsePlayback *self);
517 static ALCboolean ALCpulsePlayback_reset(ALCpulsePlayback *self);
518 static ALCboolean ALCpulsePlayback_start(ALCpulsePlayback *self);
519 static void ALCpulsePlayback_stop(ALCpulsePlayback *self);
520 static DECLARE_FORWARD2(ALCpulsePlayback, ALCbackend, ALCenum, captureSamples, ALCvoid*, ALCuint)
521 static DECLARE_FORWARD(ALCpulsePlayback, ALCbackend, ALCuint, availableSamples)
522 static ALint64 ALCpulsePlayback_getLatency(ALCpulsePlayback *self);
523 static void ALCpulsePlayback_lock(ALCpulsePlayback *self);
524 static void ALCpulsePlayback_unlock(ALCpulsePlayback *self);
525 DECLARE_DEFAULT_ALLOCATORS(ALCpulsePlayback)
527 DEFINE_ALCBACKEND_VTABLE(ALCpulsePlayback);
530 static void ALCpulsePlayback_Construct(ALCpulsePlayback *self, ALCdevice *device)
532 ALCbackend_Construct(STATIC_CAST(ALCbackend, self), device);
533 SET_VTABLE2(ALCpulsePlayback, ALCbackend, self);
535 AL_STRING_INIT(self->device_name);
538 static void ALCpulsePlayback_Destruct(ALCpulsePlayback *self)
540 AL_STRING_DEINIT(self->device_name);
541 ALCbackend_Destruct(STATIC_CAST(ALCbackend, self));
545 static void ALCpulsePlayback_deviceCallback(pa_context *UNUSED(context), const pa_sink_info *info, int eol, void *pdata)
547 pa_threaded_mainloop *loop = pdata;
548 const DevMap *iter, *end;
549 DevMap entry;
551 if(eol)
553 pa_threaded_mainloop_signal(loop, 0);
554 return;
557 iter = VECTOR_ITER_BEGIN(PlaybackDevices);
558 end = VECTOR_ITER_END(PlaybackDevices);
559 for(;iter != end;iter++)
561 if(al_string_cmp_cstr(iter->device_name, info->name) == 0)
562 return;
565 TRACE("Got device \"%s\", \"%s\"\n", info->description, info->name);
567 AL_STRING_INIT(entry.name);
568 AL_STRING_INIT(entry.device_name);
570 al_string_copy_cstr(&entry.name, info->description);
571 al_string_copy_cstr(&entry.device_name, info->name);
573 VECTOR_PUSH_BACK(PlaybackDevices, entry);
576 static void ALCpulsePlayback_probeDevices(void)
578 pa_threaded_mainloop *loop;
580 clear_devlist(&PlaybackDevices);
582 if((loop=pa_threaded_mainloop_new()) &&
583 pa_threaded_mainloop_start(loop) >= 0)
585 pa_context *context;
587 pa_threaded_mainloop_lock(loop);
588 context = connect_context(loop, AL_FALSE);
589 if(context)
591 pa_operation *o;
592 pa_stream_flags_t flags;
593 pa_sample_spec spec;
594 pa_stream *stream;
596 flags = PA_STREAM_FIX_FORMAT | PA_STREAM_FIX_RATE |
597 PA_STREAM_FIX_CHANNELS | PA_STREAM_DONT_MOVE;
599 spec.format = PA_SAMPLE_S16NE;
600 spec.rate = 44100;
601 spec.channels = 2;
603 stream = ALCpulsePlayback_connectStream(NULL, loop, context, flags,
604 NULL, &spec, NULL);
605 if(stream)
607 o = pa_context_get_sink_info_by_name(context, pa_stream_get_device_name(stream),
608 ALCpulsePlayback_deviceCallback, loop);
609 wait_for_operation(o, loop);
611 pa_stream_disconnect(stream);
612 pa_stream_unref(stream);
613 stream = NULL;
616 o = pa_context_get_sink_info_list(context, ALCpulsePlayback_deviceCallback, loop);
617 wait_for_operation(o, loop);
619 pa_context_disconnect(context);
620 pa_context_unref(context);
622 pa_threaded_mainloop_unlock(loop);
623 pa_threaded_mainloop_stop(loop);
625 if(loop)
626 pa_threaded_mainloop_free(loop);
630 static void ALCpulsePlayback_bufferAttrCallback(pa_stream *stream, void *pdata)
632 ALCpulsePlayback *self = pdata;
634 self->attr = *pa_stream_get_buffer_attr(stream);
635 TRACE("minreq=%d, tlength=%d, prebuf=%d\n", self->attr.minreq, self->attr.tlength, self->attr.prebuf);
638 static void ALCpulsePlayback_contextStateCallback(pa_context *context, void *pdata)
640 ALCpulsePlayback *self = pdata;
641 if(pa_context_get_state(context) == PA_CONTEXT_FAILED)
643 ERR("Received context failure!\n");
644 aluHandleDisconnect(STATIC_CAST(ALCbackend,self)->mDevice);
646 pa_threaded_mainloop_signal(self->loop, 0);
649 static void ALCpulsePlayback_streamStateCallback(pa_stream *stream, void *pdata)
651 ALCpulsePlayback *self = pdata;
652 if(pa_stream_get_state(stream) == PA_STREAM_FAILED)
654 ERR("Received stream failure!\n");
655 aluHandleDisconnect(STATIC_CAST(ALCbackend,self)->mDevice);
657 pa_threaded_mainloop_signal(self->loop, 0);
660 static void ALCpulsePlayback_sinkInfoCallback(pa_context *UNUSED(context), const pa_sink_info *info, int eol, void *pdata)
662 ALCpulsePlayback *self = pdata;
663 ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
664 char chanmap_str[256] = "";
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,rear-left,rear-right",
674 DevFmtX51 },
675 { "front-left,front-right,front-center,lfe,side-left,side-right",
676 DevFmtX51Side },
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 device->FmtChans = chanmaps[i].chans;
704 return;
708 pa_channel_map_snprint(chanmap_str, sizeof(chanmap_str), &info->channel_map);
709 ERR("Failed to find format for channel map:\n %s\n", chanmap_str);
712 static void ALCpulsePlayback_sinkNameCallback(pa_context *UNUSED(context), const pa_sink_info *info, int eol, void *pdata)
714 ALCpulsePlayback *self = pdata;
715 ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
717 if(eol)
719 pa_threaded_mainloop_signal(self->loop, 0);
720 return;
723 al_string_copy_cstr(&device->DeviceName, info->description);
727 static void ALCpulsePlayback_streamMovedCallback(pa_stream *stream, void *pdata)
729 ALCpulsePlayback *self = pdata;
731 al_string_copy_cstr(&self->device_name, pa_stream_get_device_name(stream));
733 TRACE("Stream moved to %s\n", al_string_get_cstr(self->device_name));
737 static pa_stream *ALCpulsePlayback_connectStream(const char *device_name,
738 pa_threaded_mainloop *loop, pa_context *context,
739 pa_stream_flags_t flags, pa_buffer_attr *attr, pa_sample_spec *spec,
740 pa_channel_map *chanmap)
742 pa_stream_state_t state;
743 pa_stream *stream;
745 stream = pa_stream_new_with_proplist(context, "Playback Stream", spec, chanmap, prop_filter);
746 if(!stream)
748 ERR("pa_stream_new_with_proplist() failed: %s\n", pa_strerror(pa_context_errno(context)));
749 return NULL;
752 pa_stream_set_state_callback(stream, stream_state_callback, loop);
754 if(pa_stream_connect_playback(stream, device_name, attr, flags, NULL, NULL) < 0)
756 ERR("Stream did not connect: %s\n", pa_strerror(pa_context_errno(context)));
757 pa_stream_unref(stream);
758 return NULL;
761 while((state=pa_stream_get_state(stream)) != PA_STREAM_READY)
763 if(!PA_STREAM_IS_GOOD(state))
765 ERR("Stream did not get ready: %s\n", pa_strerror(pa_context_errno(context)));
766 pa_stream_unref(stream);
767 return NULL;
770 pa_threaded_mainloop_wait(loop);
772 pa_stream_set_state_callback(stream, NULL, NULL);
774 return stream;
778 static int ALCpulsePlayback_mixerProc(void *ptr)
780 ALCpulsePlayback *self = ptr;
781 ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
782 ALuint buffer_size;
783 ALint update_size;
784 size_t frame_size;
785 ssize_t len;
787 SetRTPriority();
788 althrd_setname(althrd_current(), MIXER_THREAD_NAME);
790 pa_threaded_mainloop_lock(self->loop);
791 frame_size = pa_frame_size(&self->spec);
792 update_size = device->UpdateSize * frame_size;
794 /* Sanitize buffer metrics, in case we actually have less than what we
795 * asked for. */
796 buffer_size = minu(update_size*device->NumUpdates, self->attr.tlength);
797 update_size = minu(update_size, buffer_size/2);
798 do {
799 len = pa_stream_writable_size(self->stream) - self->attr.tlength +
800 buffer_size;
801 if(len < update_size)
803 if(pa_stream_is_corked(self->stream) == 1)
805 pa_operation *o;
806 o = pa_stream_cork(self->stream, 0, NULL, NULL);
807 if(o) pa_operation_unref(o);
809 pa_threaded_mainloop_unlock(self->loop);
810 al_nssleep(0, 1000000);
811 pa_threaded_mainloop_lock(self->loop);
812 continue;
814 len -= len%update_size;
816 while(len > 0)
818 size_t newlen = len;
819 void *buf;
820 pa_free_cb_t free_func = NULL;
822 #if PA_CHECK_VERSION(0,9,16)
823 if(!pa_stream_begin_write ||
824 pa_stream_begin_write(self->stream, &buf, &newlen) < 0)
825 #endif
827 buf = pa_xmalloc(newlen);
828 free_func = pa_xfree;
831 aluMixData(device, buf, newlen/frame_size);
833 pa_stream_write(self->stream, buf, newlen, free_func, 0, PA_SEEK_RELATIVE);
834 len -= newlen;
836 } while(!self->killNow && device->Connected);
837 pa_threaded_mainloop_unlock(self->loop);
839 return 0;
843 static ALCenum ALCpulsePlayback_open(ALCpulsePlayback *self, const ALCchar *name)
845 const char *pulse_name = NULL;
846 pa_stream_flags_t flags;
847 pa_sample_spec spec;
848 pa_operation *o;
850 if(name)
852 const DevMap *iter, *end;
854 if(VECTOR_SIZE(PlaybackDevices) == 0)
855 ALCpulsePlayback_probeDevices();
857 iter = VECTOR_ITER_BEGIN(PlaybackDevices);
858 end = VECTOR_ITER_END(PlaybackDevices);
859 for(;iter != end;iter++)
861 if(al_string_cmp_cstr(iter->name, name) == 0)
863 pulse_name = al_string_get_cstr(iter->device_name);
864 break;
867 if(iter == end)
868 return ALC_INVALID_VALUE;
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 ALuint len;
927 pa_threaded_mainloop_lock(self->loop);
929 if(self->stream)
931 pa_stream_set_moved_callback(self->stream, NULL, NULL);
932 #if PA_CHECK_VERSION(0,9,15)
933 if(pa_stream_set_buffer_attr_callback)
934 pa_stream_set_buffer_attr_callback(self->stream, NULL, NULL);
935 #endif
936 pa_stream_disconnect(self->stream);
937 pa_stream_unref(self->stream);
938 self->stream = NULL;
941 if(!(device->Flags&DEVICE_CHANNELS_REQUEST))
943 pa_operation *o;
944 o = pa_context_get_sink_info_by_name(self->context,
945 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;
952 flags |= PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE;
953 flags |= PA_STREAM_ADJUST_LATENCY;
954 flags |= PA_STREAM_START_CORKED;
955 if(!GetConfigValueBool("pulse", "allow-moves", 0))
956 flags |= PA_STREAM_DONT_MOVE;
958 switch(device->FmtType)
960 case DevFmtByte:
961 device->FmtType = DevFmtUByte;
962 /* fall-through */
963 case DevFmtUByte:
964 self->spec.format = PA_SAMPLE_U8;
965 break;
966 case DevFmtUShort:
967 device->FmtType = DevFmtShort;
968 /* fall-through */
969 case DevFmtShort:
970 self->spec.format = PA_SAMPLE_S16NE;
971 break;
972 case DevFmtUInt:
973 device->FmtType = DevFmtInt;
974 /* fall-through */
975 case DevFmtInt:
976 self->spec.format = PA_SAMPLE_S32NE;
977 break;
978 case DevFmtFloat:
979 self->spec.format = PA_SAMPLE_FLOAT32NE;
980 break;
982 self->spec.rate = device->Frequency;
983 self->spec.channels = ChannelsFromDevFmt(device->FmtChans);
985 if(pa_sample_spec_valid(&self->spec) == 0)
987 ERR("Invalid sample format\n");
988 pa_threaded_mainloop_unlock(self->loop);
989 return ALC_FALSE;
992 switch(device->FmtChans)
994 case DevFmtMono:
995 mapname = "mono";
996 break;
997 case DevFmtStereo:
998 mapname = "front-left,front-right";
999 break;
1000 case DevFmtQuad:
1001 mapname = "front-left,front-right,rear-left,rear-right";
1002 break;
1003 case DevFmtX51:
1004 mapname = "front-left,front-right,front-center,lfe,rear-left,rear-right";
1005 break;
1006 case DevFmtX51Side:
1007 mapname = "front-left,front-right,front-center,lfe,side-left,side-right";
1008 break;
1009 case DevFmtX61:
1010 mapname = "front-left,front-right,front-center,lfe,rear-center,side-left,side-right";
1011 break;
1012 case DevFmtX71:
1013 mapname = "front-left,front-right,front-center,lfe,rear-left,rear-right,side-left,side-right";
1014 break;
1016 if(!pa_channel_map_parse(&chanmap, mapname))
1018 ERR("Failed to build channel map for %s\n", DevFmtChannelsString(device->FmtChans));
1019 pa_threaded_mainloop_unlock(self->loop);
1020 return ALC_FALSE;
1022 SetDefaultWFXChannelOrder(device);
1024 self->attr.fragsize = -1;
1025 self->attr.prebuf = 0;
1026 self->attr.minreq = device->UpdateSize * pa_frame_size(&self->spec);
1027 self->attr.tlength = self->attr.minreq * maxu(device->NumUpdates, 2);
1028 self->attr.maxlength = -1;
1030 self->stream = ALCpulsePlayback_connectStream(al_string_get_cstr(self->device_name),
1031 self->loop, self->context, flags,
1032 &self->attr, &self->spec, &chanmap);
1033 if(!self->stream)
1035 pa_threaded_mainloop_unlock(self->loop);
1036 return ALC_FALSE;
1038 pa_stream_set_state_callback(self->stream, ALCpulsePlayback_streamStateCallback, self);
1039 pa_stream_set_moved_callback(self->stream, ALCpulsePlayback_streamMovedCallback, self);
1041 self->spec = *(pa_stream_get_sample_spec(self->stream));
1042 if(device->Frequency != self->spec.rate)
1044 pa_operation *o;
1046 /* Server updated our playback rate, so modify the buffer attribs
1047 * accordingly. */
1048 device->NumUpdates = (ALuint)((ALdouble)device->NumUpdates / device->Frequency *
1049 self->spec.rate + 0.5);
1050 self->attr.minreq = device->UpdateSize * pa_frame_size(&self->spec);
1051 self->attr.tlength = self->attr.minreq * clampu(device->NumUpdates, 2, 16);
1052 self->attr.maxlength = -1;
1053 self->attr.prebuf = 0;
1055 o = pa_stream_set_buffer_attr(self->stream, &self->attr,
1056 stream_success_callback, self->loop);
1057 wait_for_operation(o, self->loop);
1059 device->Frequency = self->spec.rate;
1062 #if PA_CHECK_VERSION(0,9,15)
1063 if(pa_stream_set_buffer_attr_callback)
1064 pa_stream_set_buffer_attr_callback(self->stream, ALCpulsePlayback_bufferAttrCallback, self);
1065 #endif
1066 ALCpulsePlayback_bufferAttrCallback(self->stream, self);
1068 len = self->attr.minreq / pa_frame_size(&self->spec);
1069 if((CPUCapFlags&CPU_CAP_SSE))
1070 len = (len+3)&~3;
1071 device->NumUpdates = (ALuint)((ALdouble)device->NumUpdates/len*device->UpdateSize + 0.5);
1072 device->NumUpdates = clampu(device->NumUpdates, 2, 16);
1073 device->UpdateSize = len;
1075 pa_threaded_mainloop_unlock(self->loop);
1076 return ALC_TRUE;
1079 static ALCboolean ALCpulsePlayback_start(ALCpulsePlayback *self)
1081 self->killNow = AL_FALSE;
1082 if(althrd_create(&self->thread, ALCpulsePlayback_mixerProc, self) != althrd_success)
1083 return ALC_FALSE;
1084 return ALC_TRUE;
1087 static void ALCpulsePlayback_stop(ALCpulsePlayback *self)
1089 pa_operation *o;
1090 int res;
1092 if(!self->stream)
1093 return;
1095 self->killNow = AL_TRUE;
1096 althrd_join(self->thread, &res);
1098 pa_threaded_mainloop_lock(self->loop);
1100 o = pa_stream_cork(self->stream, 1, stream_success_callback, self->loop);
1101 wait_for_operation(o, self->loop);
1103 pa_threaded_mainloop_unlock(self->loop);
1107 static ALint64 ALCpulsePlayback_getLatency(ALCpulsePlayback *self)
1109 pa_usec_t latency = 0;
1110 int neg, err;
1112 if((err=pa_stream_get_latency(self->stream, &latency, &neg)) != 0)
1114 /* FIXME: if err = -PA_ERR_NODATA, it means we were called too soon
1115 * after starting the stream and no timing info has been received from
1116 * the server yet. Should we wait, possibly stalling the app, or give a
1117 * dummy value? Either way, it shouldn't be 0. */
1118 if(err != -PA_ERR_NODATA)
1119 ERR("Failed to get stream latency: 0x%x\n", err);
1120 return 0;
1123 if(neg) latency = 0;
1124 return (ALint64)minu64(latency, U64(0x7fffffffffffffff)/1000) * 1000;
1128 static void ALCpulsePlayback_lock(ALCpulsePlayback *self)
1130 pa_threaded_mainloop_lock(self->loop);
1133 static void ALCpulsePlayback_unlock(ALCpulsePlayback *self)
1135 pa_threaded_mainloop_unlock(self->loop);
1139 typedef struct ALCpulseCapture {
1140 DERIVE_FROM_TYPE(ALCbackend);
1142 al_string device_name;
1144 const void *cap_store;
1145 size_t cap_len;
1146 size_t cap_remain;
1148 ALCuint last_readable;
1150 pa_buffer_attr attr;
1151 pa_sample_spec spec;
1153 pa_threaded_mainloop *loop;
1155 pa_stream *stream;
1156 pa_context *context;
1157 } ALCpulseCapture;
1159 static void ALCpulseCapture_deviceCallback(pa_context *context, const pa_source_info *info, int eol, void *pdata);
1160 static void ALCpulseCapture_probeDevices(void);
1162 static void ALCpulseCapture_contextStateCallback(pa_context *context, void *pdata);
1163 static void ALCpulseCapture_streamStateCallback(pa_stream *stream, void *pdata);
1164 static void ALCpulseCapture_sourceNameCallback(pa_context *context, const pa_source_info *info, int eol, void *pdata);
1165 static void ALCpulseCapture_streamMovedCallback(pa_stream *stream, void *pdata);
1166 static pa_stream *ALCpulseCapture_connectStream(const char *device_name,
1167 pa_threaded_mainloop *loop, pa_context *context,
1168 pa_stream_flags_t flags, pa_buffer_attr *attr,
1169 pa_sample_spec *spec, pa_channel_map *chanmap);
1171 static void ALCpulseCapture_Construct(ALCpulseCapture *self, ALCdevice *device);
1172 static void ALCpulseCapture_Destruct(ALCpulseCapture *self);
1173 static ALCenum ALCpulseCapture_open(ALCpulseCapture *self, const ALCchar *name);
1174 static void ALCpulseCapture_close(ALCpulseCapture *self);
1175 static DECLARE_FORWARD(ALCpulseCapture, ALCbackend, ALCboolean, reset)
1176 static ALCboolean ALCpulseCapture_start(ALCpulseCapture *self);
1177 static void ALCpulseCapture_stop(ALCpulseCapture *self);
1178 static ALCenum ALCpulseCapture_captureSamples(ALCpulseCapture *self, ALCvoid *buffer, ALCuint samples);
1179 static ALCuint ALCpulseCapture_availableSamples(ALCpulseCapture *self);
1180 static ALint64 ALCpulseCapture_getLatency(ALCpulseCapture *self);
1181 static void ALCpulseCapture_lock(ALCpulseCapture *self);
1182 static void ALCpulseCapture_unlock(ALCpulseCapture *self);
1183 DECLARE_DEFAULT_ALLOCATORS(ALCpulseCapture)
1185 DEFINE_ALCBACKEND_VTABLE(ALCpulseCapture);
1188 static void ALCpulseCapture_Construct(ALCpulseCapture *self, ALCdevice *device)
1190 ALCbackend_Construct(STATIC_CAST(ALCbackend, self), device);
1191 SET_VTABLE2(ALCpulseCapture, ALCbackend, self);
1193 AL_STRING_INIT(self->device_name);
1196 static void ALCpulseCapture_Destruct(ALCpulseCapture *self)
1198 AL_STRING_DEINIT(self->device_name);
1199 ALCbackend_Destruct(STATIC_CAST(ALCbackend, self));
1203 static void ALCpulseCapture_deviceCallback(pa_context *UNUSED(context), const pa_source_info *info, int eol, void *pdata)
1205 pa_threaded_mainloop *loop = pdata;
1206 const DevMap *iter, *end;
1207 DevMap entry;
1209 if(eol)
1211 pa_threaded_mainloop_signal(loop, 0);
1212 return;
1215 iter = VECTOR_ITER_BEGIN(CaptureDevices);
1216 end = VECTOR_ITER_END(CaptureDevices);
1217 for(;iter != end;iter++)
1219 if(al_string_cmp_cstr(iter->device_name, info->name) == 0)
1220 return;
1223 TRACE("Got device \"%s\", \"%s\"\n", info->description, info->name);
1225 AL_STRING_INIT(entry.name);
1226 AL_STRING_INIT(entry.device_name);
1228 al_string_copy_cstr(&entry.name, info->description);
1229 al_string_copy_cstr(&entry.device_name, info->name);
1231 VECTOR_PUSH_BACK(CaptureDevices, entry);
1234 static void ALCpulseCapture_probeDevices(void)
1236 pa_threaded_mainloop *loop;
1238 clear_devlist(&CaptureDevices);
1240 if((loop=pa_threaded_mainloop_new()) &&
1241 pa_threaded_mainloop_start(loop) >= 0)
1243 pa_context *context;
1245 pa_threaded_mainloop_lock(loop);
1246 context = connect_context(loop, AL_FALSE);
1247 if(context)
1249 pa_operation *o;
1250 pa_stream_flags_t flags;
1251 pa_sample_spec spec;
1252 pa_stream *stream;
1254 flags = PA_STREAM_FIX_FORMAT | PA_STREAM_FIX_RATE |
1255 PA_STREAM_FIX_CHANNELS | PA_STREAM_DONT_MOVE;
1257 spec.format = PA_SAMPLE_S16NE;
1258 spec.rate = 44100;
1259 spec.channels = 1;
1261 stream = ALCpulseCapture_connectStream(NULL, loop, context, flags,
1262 NULL, &spec, NULL);
1263 if(stream)
1265 o = pa_context_get_source_info_by_name(context, pa_stream_get_device_name(stream),
1266 ALCpulseCapture_deviceCallback, loop);
1267 wait_for_operation(o, loop);
1269 pa_stream_disconnect(stream);
1270 pa_stream_unref(stream);
1271 stream = NULL;
1274 o = pa_context_get_source_info_list(context, ALCpulseCapture_deviceCallback, loop);
1275 wait_for_operation(o, loop);
1277 pa_context_disconnect(context);
1278 pa_context_unref(context);
1280 pa_threaded_mainloop_unlock(loop);
1281 pa_threaded_mainloop_stop(loop);
1283 if(loop)
1284 pa_threaded_mainloop_free(loop);
1288 static void ALCpulseCapture_contextStateCallback(pa_context *context, void *pdata)
1290 ALCpulseCapture *self = pdata;
1291 if(pa_context_get_state(context) == PA_CONTEXT_FAILED)
1293 ERR("Received context failure!\n");
1294 aluHandleDisconnect(STATIC_CAST(ALCbackend,self)->mDevice);
1296 pa_threaded_mainloop_signal(self->loop, 0);
1299 static void ALCpulseCapture_streamStateCallback(pa_stream *stream, void *pdata)
1301 ALCpulseCapture *self = pdata;
1302 if(pa_stream_get_state(stream) == PA_STREAM_FAILED)
1304 ERR("Received stream failure!\n");
1305 aluHandleDisconnect(STATIC_CAST(ALCbackend,self)->mDevice);
1307 pa_threaded_mainloop_signal(self->loop, 0);
1311 static void ALCpulseCapture_sourceNameCallback(pa_context *UNUSED(context), const pa_source_info *info, int eol, void *pdata)
1313 ALCpulseCapture *self = pdata;
1314 ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
1316 if(eol)
1318 pa_threaded_mainloop_signal(self->loop, 0);
1319 return;
1322 al_string_copy_cstr(&device->DeviceName, info->description);
1326 static void ALCpulseCapture_streamMovedCallback(pa_stream *stream, void *pdata)
1328 ALCpulseCapture *self = pdata;
1330 al_string_copy_cstr(&self->device_name, pa_stream_get_device_name(stream));
1332 TRACE("Stream moved to %s\n", al_string_get_cstr(self->device_name));
1336 static pa_stream *ALCpulseCapture_connectStream(const char *device_name,
1337 pa_threaded_mainloop *loop, pa_context *context,
1338 pa_stream_flags_t flags, pa_buffer_attr *attr, pa_sample_spec *spec,
1339 pa_channel_map *chanmap)
1341 pa_stream_state_t state;
1342 pa_stream *stream;
1344 stream = pa_stream_new_with_proplist(context, "Capture Stream", spec, chanmap, prop_filter);
1345 if(!stream)
1347 ERR("pa_stream_new_with_proplist() failed: %s\n", pa_strerror(pa_context_errno(context)));
1348 return NULL;
1351 pa_stream_set_state_callback(stream, stream_state_callback, loop);
1353 if(pa_stream_connect_record(stream, device_name, attr, flags) < 0)
1355 ERR("Stream did not connect: %s\n", pa_strerror(pa_context_errno(context)));
1356 pa_stream_unref(stream);
1357 return NULL;
1360 while((state=pa_stream_get_state(stream)) != PA_STREAM_READY)
1362 if(!PA_STREAM_IS_GOOD(state))
1364 ERR("Stream did not get ready: %s\n", pa_strerror(pa_context_errno(context)));
1365 pa_stream_unref(stream);
1366 return NULL;
1369 pa_threaded_mainloop_wait(loop);
1371 pa_stream_set_state_callback(stream, NULL, NULL);
1373 return stream;
1377 static ALCenum ALCpulseCapture_open(ALCpulseCapture *self, const ALCchar *name)
1379 ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
1380 const char *pulse_name = NULL;
1381 pa_stream_flags_t flags = 0;
1382 pa_channel_map chanmap;
1383 pa_operation *o;
1384 ALuint samples;
1386 if(name)
1388 const DevMap *iter, *end;
1390 if(VECTOR_SIZE(CaptureDevices) == 0)
1391 ALCpulseCapture_probeDevices();
1393 iter = VECTOR_ITER_BEGIN(CaptureDevices);
1394 end = VECTOR_ITER_END(CaptureDevices);
1395 for(;iter != end;iter++)
1397 if(al_string_cmp_cstr(iter->name, name) == 0)
1399 pulse_name = al_string_get_cstr(iter->device_name);
1400 break;
1403 if(iter == end)
1404 return ALC_INVALID_VALUE;
1407 if(!pulse_open(&self->loop, &self->context, ALCpulseCapture_contextStateCallback, self))
1408 return ALC_INVALID_VALUE;
1410 pa_threaded_mainloop_lock(self->loop);
1412 self->spec.rate = device->Frequency;
1413 self->spec.channels = ChannelsFromDevFmt(device->FmtChans);
1415 switch(device->FmtType)
1417 case DevFmtUByte:
1418 self->spec.format = PA_SAMPLE_U8;
1419 break;
1420 case DevFmtShort:
1421 self->spec.format = PA_SAMPLE_S16NE;
1422 break;
1423 case DevFmtInt:
1424 self->spec.format = PA_SAMPLE_S32NE;
1425 break;
1426 case DevFmtFloat:
1427 self->spec.format = PA_SAMPLE_FLOAT32NE;
1428 break;
1429 case DevFmtByte:
1430 case DevFmtUShort:
1431 case DevFmtUInt:
1432 ERR("%s capture samples not supported\n", DevFmtTypeString(device->FmtType));
1433 pa_threaded_mainloop_unlock(self->loop);
1434 goto fail;
1437 if(pa_sample_spec_valid(&self->spec) == 0)
1439 ERR("Invalid sample format\n");
1440 pa_threaded_mainloop_unlock(self->loop);
1441 goto fail;
1444 if(!pa_channel_map_init_auto(&chanmap, self->spec.channels, PA_CHANNEL_MAP_WAVEEX))
1446 ERR("Couldn't build map for channel count (%d)!\n", self->spec.channels);
1447 pa_threaded_mainloop_unlock(self->loop);
1448 goto fail;
1451 samples = device->UpdateSize * device->NumUpdates;
1452 samples = maxu(samples, 100 * device->Frequency / 1000);
1454 self->attr.minreq = -1;
1455 self->attr.prebuf = -1;
1456 self->attr.maxlength = samples * pa_frame_size(&self->spec);
1457 self->attr.tlength = -1;
1458 self->attr.fragsize = minu(samples, 50*device->Frequency/1000) *
1459 pa_frame_size(&self->spec);
1461 flags |= PA_STREAM_START_CORKED|PA_STREAM_ADJUST_LATENCY;
1462 if(!GetConfigValueBool("pulse", "allow-moves", 0))
1463 flags |= PA_STREAM_DONT_MOVE;
1465 TRACE("Connecting to \"%s\"\n", pulse_name ? pulse_name : "(default)");
1466 self->stream = ALCpulseCapture_connectStream(pulse_name, self->loop, self->context,
1467 flags, &self->attr, &self->spec,
1468 &chanmap);
1469 if(!self->stream)
1471 pa_threaded_mainloop_unlock(self->loop);
1472 goto fail;
1474 pa_stream_set_moved_callback(self->stream, ALCpulseCapture_streamMovedCallback, self);
1475 pa_stream_set_state_callback(self->stream, ALCpulseCapture_streamStateCallback, self);
1477 al_string_copy_cstr(&self->device_name, pa_stream_get_device_name(self->stream));
1478 o = pa_context_get_source_info_by_name(self->context,
1479 al_string_get_cstr(self->device_name),
1480 ALCpulseCapture_sourceNameCallback, self);
1481 wait_for_operation(o, self->loop);
1483 pa_threaded_mainloop_unlock(self->loop);
1484 return ALC_NO_ERROR;
1486 fail:
1487 pulse_close(self->loop, self->context, self->stream);
1488 self->loop = NULL;
1489 self->context = NULL;
1490 self->stream = NULL;
1492 return ALC_INVALID_VALUE;
1495 static void ALCpulseCapture_close(ALCpulseCapture *self)
1497 pulse_close(self->loop, self->context, self->stream);
1498 self->loop = NULL;
1499 self->context = NULL;
1500 self->stream = NULL;
1502 al_string_clear(&self->device_name);
1505 static ALCboolean ALCpulseCapture_start(ALCpulseCapture *self)
1507 pa_operation *o;
1508 o = pa_stream_cork(self->stream, 0, stream_success_callback, self->loop);
1509 wait_for_operation(o, self->loop);
1511 return ALC_TRUE;
1514 static void ALCpulseCapture_stop(ALCpulseCapture *self)
1516 pa_operation *o;
1517 o = pa_stream_cork(self->stream, 1, stream_success_callback, self->loop);
1518 wait_for_operation(o, self->loop);
1521 static ALCenum ALCpulseCapture_captureSamples(ALCpulseCapture *self, ALCvoid *buffer, ALCuint samples)
1523 ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
1524 ALCuint todo = samples * pa_frame_size(&self->spec);
1526 /* Capture is done in fragment-sized chunks, so we loop until we get all
1527 * that's available */
1528 self->last_readable -= todo;
1529 while(todo > 0)
1531 size_t rem = todo;
1533 if(self->cap_len == 0)
1535 pa_stream_state_t state;
1537 state = pa_stream_get_state(self->stream);
1538 if(!PA_STREAM_IS_GOOD(state))
1540 aluHandleDisconnect(device);
1541 break;
1543 if(pa_stream_peek(self->stream, &self->cap_store, &self->cap_len) < 0)
1545 ERR("pa_stream_peek() failed: %s\n",
1546 pa_strerror(pa_context_errno(self->context)));
1547 aluHandleDisconnect(device);
1548 break;
1550 self->cap_remain = self->cap_len;
1552 if(rem > self->cap_remain)
1553 rem = self->cap_remain;
1555 memcpy(buffer, self->cap_store, rem);
1557 buffer = (ALbyte*)buffer + rem;
1558 todo -= rem;
1560 self->cap_store = (ALbyte*)self->cap_store + rem;
1561 self->cap_remain -= rem;
1562 if(self->cap_remain == 0)
1564 pa_stream_drop(self->stream);
1565 self->cap_len = 0;
1568 if(todo > 0)
1569 memset(buffer, ((device->FmtType==DevFmtUByte) ? 0x80 : 0), todo);
1571 return ALC_NO_ERROR;
1574 static ALCuint ALCpulseCapture_availableSamples(ALCpulseCapture *self)
1576 ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
1577 size_t readable = self->cap_remain;
1579 if(device->Connected)
1581 ssize_t got = pa_stream_readable_size(self->stream);
1582 if(got < 0)
1584 ERR("pa_stream_readable_size() failed: %s\n", pa_strerror(got));
1585 aluHandleDisconnect(device);
1587 else if((size_t)got > self->cap_len)
1588 readable += got - self->cap_len;
1591 if(self->last_readable < readable)
1592 self->last_readable = readable;
1593 return self->last_readable / pa_frame_size(&self->spec);
1597 static ALint64 ALCpulseCapture_getLatency(ALCpulseCapture *self)
1599 pa_usec_t latency = 0;
1600 int neg;
1602 if(pa_stream_get_latency(self->stream, &latency, &neg) != 0)
1604 ERR("Failed to get stream latency!\n");
1605 return 0;
1608 if(neg) latency = 0;
1609 return (ALint64)minu64(latency, U64(0x7fffffffffffffff)/1000) * 1000;
1613 static void ALCpulseCapture_lock(ALCpulseCapture *self)
1615 pa_threaded_mainloop_lock(self->loop);
1618 static void ALCpulseCapture_unlock(ALCpulseCapture *self)
1620 pa_threaded_mainloop_unlock(self->loop);
1624 static inline void AppendAllDevicesList2(const DevMap *entry)
1625 { AppendAllDevicesList(al_string_get_cstr(entry->name)); }
1626 static inline void AppendCaptureDeviceList2(const DevMap *entry)
1627 { AppendCaptureDeviceList(al_string_get_cstr(entry->name)); }
1629 typedef struct ALCpulseBackendFactory {
1630 DERIVE_FROM_TYPE(ALCbackendFactory);
1631 } ALCpulseBackendFactory;
1632 #define ALCPULSEBACKENDFACTORY_INITIALIZER { { GET_VTABLE2(ALCpulseBackendFactory, ALCbackendFactory) } }
1634 static ALCboolean ALCpulseBackendFactory_init(ALCpulseBackendFactory *self);
1635 static void ALCpulseBackendFactory_deinit(ALCpulseBackendFactory *self);
1636 static ALCboolean ALCpulseBackendFactory_querySupport(ALCpulseBackendFactory *self, ALCbackend_Type type);
1637 static void ALCpulseBackendFactory_probe(ALCpulseBackendFactory *self, enum DevProbe type);
1638 static ALCbackend* ALCpulseBackendFactory_createBackend(ALCpulseBackendFactory *self, ALCdevice *device, ALCbackend_Type type);
1640 DEFINE_ALCBACKENDFACTORY_VTABLE(ALCpulseBackendFactory);
1643 static ALCboolean ALCpulseBackendFactory_init(ALCpulseBackendFactory* UNUSED(self))
1645 ALCboolean ret = ALC_FALSE;
1647 VECTOR_INIT(PlaybackDevices);
1648 VECTOR_INIT(CaptureDevices);
1650 if(pulse_load())
1652 pa_threaded_mainloop *loop;
1654 pulse_ctx_flags = 0;
1655 if(!GetConfigValueBool("pulse", "spawn-server", 1))
1656 pulse_ctx_flags |= PA_CONTEXT_NOAUTOSPAWN;
1658 if((loop=pa_threaded_mainloop_new()) &&
1659 pa_threaded_mainloop_start(loop) >= 0)
1661 pa_context *context;
1663 pa_threaded_mainloop_lock(loop);
1664 context = connect_context(loop, AL_TRUE);
1665 if(context)
1667 ret = ALC_TRUE;
1669 /* Some libraries (Phonon, Qt) set some pulseaudio properties
1670 * through environment variables, which causes all streams in
1671 * the process to inherit them. This attempts to filter those
1672 * properties out by setting them to 0-length data. */
1673 prop_filter = pa_proplist_new();
1674 pa_proplist_set(prop_filter, PA_PROP_MEDIA_ROLE, NULL, 0);
1675 pa_proplist_set(prop_filter, "phonon.streamid", NULL, 0);
1677 pa_context_disconnect(context);
1678 pa_context_unref(context);
1680 pa_threaded_mainloop_unlock(loop);
1681 pa_threaded_mainloop_stop(loop);
1683 if(loop)
1684 pa_threaded_mainloop_free(loop);
1687 return ret;
1690 static void ALCpulseBackendFactory_deinit(ALCpulseBackendFactory* UNUSED(self))
1692 clear_devlist(&PlaybackDevices);
1693 VECTOR_DEINIT(PlaybackDevices);
1695 clear_devlist(&CaptureDevices);
1696 VECTOR_DEINIT(CaptureDevices);
1698 if(prop_filter)
1699 pa_proplist_free(prop_filter);
1700 prop_filter = NULL;
1702 /* PulseAudio doesn't like being CloseLib'd sometimes */
1705 static ALCboolean ALCpulseBackendFactory_querySupport(ALCpulseBackendFactory* UNUSED(self), ALCbackend_Type type)
1707 if(type == ALCbackend_Playback || type == ALCbackend_Capture)
1708 return ALC_TRUE;
1709 return ALC_FALSE;
1712 static void ALCpulseBackendFactory_probe(ALCpulseBackendFactory* UNUSED(self), enum DevProbe type)
1714 switch(type)
1716 case ALL_DEVICE_PROBE:
1717 ALCpulsePlayback_probeDevices();
1718 VECTOR_FOR_EACH(const DevMap, PlaybackDevices, AppendAllDevicesList2);
1719 break;
1721 case CAPTURE_DEVICE_PROBE:
1722 ALCpulseCapture_probeDevices();
1723 VECTOR_FOR_EACH(const DevMap, CaptureDevices, AppendCaptureDeviceList2);
1724 break;
1728 static ALCbackend* ALCpulseBackendFactory_createBackend(ALCpulseBackendFactory* UNUSED(self), ALCdevice *device, ALCbackend_Type type)
1730 if(type == ALCbackend_Playback)
1732 ALCpulsePlayback *backend;
1734 backend = ALCpulsePlayback_New(sizeof(*backend));
1735 if(!backend) return NULL;
1736 memset(backend, 0, sizeof(*backend));
1738 ALCpulsePlayback_Construct(backend, device);
1740 return STATIC_CAST(ALCbackend, backend);
1742 if(type == ALCbackend_Capture)
1744 ALCpulseCapture *backend;
1746 backend = ALCpulseCapture_New(sizeof(*backend));
1747 if(!backend) return NULL;
1748 memset(backend, 0, sizeof(*backend));
1750 ALCpulseCapture_Construct(backend, device);
1752 return STATIC_CAST(ALCbackend, backend);
1755 return NULL;
1759 #else /* PA_API_VERSION == 12 */
1761 #warning "Unsupported API version, backend will be unavailable!"
1763 typedef struct ALCpulseBackendFactory {
1764 DERIVE_FROM_TYPE(ALCbackendFactory);
1765 } ALCpulseBackendFactory;
1766 #define ALCPULSEBACKENDFACTORY_INITIALIZER { { GET_VTABLE2(ALCpulseBackendFactory, ALCbackendFactory) } }
1768 static ALCboolean ALCpulseBackendFactory_init(ALCpulseBackendFactory* UNUSED(self))
1770 return ALC_FALSE;
1773 static void ALCpulseBackendFactory_deinit(ALCpulseBackendFactory* UNUSED(self))
1777 static ALCboolean ALCpulseBackendFactory_querySupport(ALCpulseBackendFactory* UNUSED(self), ALCbackend_Type UNUSED(type))
1779 return ALC_FALSE;
1782 static void ALCpulseBackendFactory_probe(ALCpulseBackendFactory* UNUSED(self), enum DevProbe UNUSED(type))
1786 static ALCbackend* ALCpulseBackendFactory_createBackend(ALCpulseBackendFactory* UNUSED(self), ALCdevice* UNUSED(device), ALCbackend_Type UNUSED(type))
1788 return NULL;
1791 DEFINE_ALCBACKENDFACTORY_VTABLE(ALCpulseBackendFactory);
1793 #endif /* PA_API_VERSION == 12 */
1795 ALCbackendFactory *ALCpulseBackendFactory_getFactory(void)
1797 static ALCpulseBackendFactory factory = ALCPULSEBACKENDFACTORY_INITIALIZER;
1798 return STATIC_CAST(ALCbackendFactory, &factory);