Avoid tracing wide-char strings
[openal-soft.git] / Alc / backends / pulseaudio.c
blobf8761c9142446308d00744d25334ec63f1d70cb0
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 #ifdef HAVE_DYNLOAD
38 static void *pa_handle;
39 #define MAKE_FUNC(x) static __typeof(x) * p##x
40 MAKE_FUNC(pa_context_unref);
41 MAKE_FUNC(pa_sample_spec_valid);
42 MAKE_FUNC(pa_frame_size);
43 MAKE_FUNC(pa_stream_drop);
44 MAKE_FUNC(pa_strerror);
45 MAKE_FUNC(pa_context_get_state);
46 MAKE_FUNC(pa_stream_get_state);
47 MAKE_FUNC(pa_threaded_mainloop_signal);
48 MAKE_FUNC(pa_stream_peek);
49 MAKE_FUNC(pa_threaded_mainloop_wait);
50 MAKE_FUNC(pa_threaded_mainloop_unlock);
51 MAKE_FUNC(pa_threaded_mainloop_in_thread);
52 MAKE_FUNC(pa_context_new);
53 MAKE_FUNC(pa_threaded_mainloop_stop);
54 MAKE_FUNC(pa_context_disconnect);
55 MAKE_FUNC(pa_threaded_mainloop_start);
56 MAKE_FUNC(pa_threaded_mainloop_get_api);
57 MAKE_FUNC(pa_context_set_state_callback);
58 MAKE_FUNC(pa_stream_write);
59 MAKE_FUNC(pa_xfree);
60 MAKE_FUNC(pa_stream_connect_record);
61 MAKE_FUNC(pa_stream_connect_playback);
62 MAKE_FUNC(pa_stream_readable_size);
63 MAKE_FUNC(pa_stream_writable_size);
64 MAKE_FUNC(pa_stream_is_corked);
65 MAKE_FUNC(pa_stream_cork);
66 MAKE_FUNC(pa_stream_is_suspended);
67 MAKE_FUNC(pa_stream_get_device_name);
68 MAKE_FUNC(pa_stream_get_latency);
69 MAKE_FUNC(pa_path_get_filename);
70 MAKE_FUNC(pa_get_binary_name);
71 MAKE_FUNC(pa_threaded_mainloop_free);
72 MAKE_FUNC(pa_context_errno);
73 MAKE_FUNC(pa_xmalloc);
74 MAKE_FUNC(pa_stream_unref);
75 MAKE_FUNC(pa_threaded_mainloop_accept);
76 MAKE_FUNC(pa_stream_set_write_callback);
77 MAKE_FUNC(pa_threaded_mainloop_new);
78 MAKE_FUNC(pa_context_connect);
79 MAKE_FUNC(pa_stream_set_buffer_attr);
80 MAKE_FUNC(pa_stream_get_buffer_attr);
81 MAKE_FUNC(pa_stream_get_sample_spec);
82 MAKE_FUNC(pa_stream_get_time);
83 MAKE_FUNC(pa_stream_set_read_callback);
84 MAKE_FUNC(pa_stream_set_state_callback);
85 MAKE_FUNC(pa_stream_set_moved_callback);
86 MAKE_FUNC(pa_stream_set_underflow_callback);
87 MAKE_FUNC(pa_stream_new_with_proplist);
88 MAKE_FUNC(pa_stream_disconnect);
89 MAKE_FUNC(pa_threaded_mainloop_lock);
90 MAKE_FUNC(pa_channel_map_init_auto);
91 MAKE_FUNC(pa_channel_map_parse);
92 MAKE_FUNC(pa_channel_map_snprint);
93 MAKE_FUNC(pa_channel_map_equal);
94 MAKE_FUNC(pa_context_get_server_info);
95 MAKE_FUNC(pa_context_get_sink_info_by_name);
96 MAKE_FUNC(pa_context_get_sink_info_list);
97 MAKE_FUNC(pa_context_get_source_info_by_name);
98 MAKE_FUNC(pa_context_get_source_info_list);
99 MAKE_FUNC(pa_operation_get_state);
100 MAKE_FUNC(pa_operation_unref);
101 MAKE_FUNC(pa_proplist_new);
102 MAKE_FUNC(pa_proplist_free);
103 MAKE_FUNC(pa_proplist_set);
104 MAKE_FUNC(pa_channel_map_superset);
105 MAKE_FUNC(pa_stream_set_buffer_attr_callback);
106 MAKE_FUNC(pa_stream_begin_write);
107 #undef MAKE_FUNC
109 #define pa_context_unref ppa_context_unref
110 #define pa_sample_spec_valid ppa_sample_spec_valid
111 #define pa_frame_size ppa_frame_size
112 #define pa_stream_drop ppa_stream_drop
113 #define pa_strerror ppa_strerror
114 #define pa_context_get_state ppa_context_get_state
115 #define pa_stream_get_state ppa_stream_get_state
116 #define pa_threaded_mainloop_signal ppa_threaded_mainloop_signal
117 #define pa_stream_peek ppa_stream_peek
118 #define pa_threaded_mainloop_wait ppa_threaded_mainloop_wait
119 #define pa_threaded_mainloop_unlock ppa_threaded_mainloop_unlock
120 #define pa_threaded_mainloop_in_thread ppa_threaded_mainloop_in_thread
121 #define pa_context_new ppa_context_new
122 #define pa_threaded_mainloop_stop ppa_threaded_mainloop_stop
123 #define pa_context_disconnect ppa_context_disconnect
124 #define pa_threaded_mainloop_start ppa_threaded_mainloop_start
125 #define pa_threaded_mainloop_get_api ppa_threaded_mainloop_get_api
126 #define pa_context_set_state_callback ppa_context_set_state_callback
127 #define pa_stream_write ppa_stream_write
128 #define pa_xfree ppa_xfree
129 #define pa_stream_connect_record ppa_stream_connect_record
130 #define pa_stream_connect_playback ppa_stream_connect_playback
131 #define pa_stream_readable_size ppa_stream_readable_size
132 #define pa_stream_writable_size ppa_stream_writable_size
133 #define pa_stream_is_corked ppa_stream_is_corked
134 #define pa_stream_cork ppa_stream_cork
135 #define pa_stream_is_suspended ppa_stream_is_suspended
136 #define pa_stream_get_device_name ppa_stream_get_device_name
137 #define pa_stream_get_latency ppa_stream_get_latency
138 #define pa_path_get_filename ppa_path_get_filename
139 #define pa_get_binary_name ppa_get_binary_name
140 #define pa_threaded_mainloop_free ppa_threaded_mainloop_free
141 #define pa_context_errno ppa_context_errno
142 #define pa_xmalloc ppa_xmalloc
143 #define pa_stream_unref ppa_stream_unref
144 #define pa_threaded_mainloop_accept ppa_threaded_mainloop_accept
145 #define pa_stream_set_write_callback ppa_stream_set_write_callback
146 #define pa_threaded_mainloop_new ppa_threaded_mainloop_new
147 #define pa_context_connect ppa_context_connect
148 #define pa_stream_set_buffer_attr ppa_stream_set_buffer_attr
149 #define pa_stream_get_buffer_attr ppa_stream_get_buffer_attr
150 #define pa_stream_get_sample_spec ppa_stream_get_sample_spec
151 #define pa_stream_get_time ppa_stream_get_time
152 #define pa_stream_set_read_callback ppa_stream_set_read_callback
153 #define pa_stream_set_state_callback ppa_stream_set_state_callback
154 #define pa_stream_set_moved_callback ppa_stream_set_moved_callback
155 #define pa_stream_set_underflow_callback ppa_stream_set_underflow_callback
156 #define pa_stream_new_with_proplist ppa_stream_new_with_proplist
157 #define pa_stream_disconnect ppa_stream_disconnect
158 #define pa_threaded_mainloop_lock ppa_threaded_mainloop_lock
159 #define pa_channel_map_init_auto ppa_channel_map_init_auto
160 #define pa_channel_map_parse ppa_channel_map_parse
161 #define pa_channel_map_snprint ppa_channel_map_snprint
162 #define pa_channel_map_equal ppa_channel_map_equal
163 #define pa_context_get_server_info ppa_context_get_server_info
164 #define pa_context_get_sink_info_by_name ppa_context_get_sink_info_by_name
165 #define pa_context_get_sink_info_list ppa_context_get_sink_info_list
166 #define pa_context_get_source_info_by_name ppa_context_get_source_info_by_name
167 #define pa_context_get_source_info_list ppa_context_get_source_info_list
168 #define pa_operation_get_state ppa_operation_get_state
169 #define pa_operation_unref ppa_operation_unref
170 #define pa_proplist_new ppa_proplist_new
171 #define pa_proplist_free ppa_proplist_free
172 #define pa_proplist_set ppa_proplist_set
173 #define pa_channel_map_superset ppa_channel_map_superset
174 #define pa_stream_set_buffer_attr_callback ppa_stream_set_buffer_attr_callback
175 #define pa_stream_begin_write ppa_stream_begin_write
177 #endif
179 static ALCboolean pulse_load(void)
181 ALCboolean ret = ALC_TRUE;
182 #ifdef HAVE_DYNLOAD
183 if(!pa_handle)
185 #ifdef _WIN32
186 #define PALIB "libpulse-0.dll"
187 #elif defined(__APPLE__) && defined(__MACH__)
188 #define PALIB "libpulse.0.dylib"
189 #else
190 #define PALIB "libpulse.so.0"
191 #endif
192 pa_handle = LoadLib(PALIB);
193 if(!pa_handle)
194 return ALC_FALSE;
196 #define LOAD_FUNC(x) do { \
197 p##x = GetSymbol(pa_handle, #x); \
198 if(!(p##x)) { \
199 ret = ALC_FALSE; \
201 } while(0)
202 LOAD_FUNC(pa_context_unref);
203 LOAD_FUNC(pa_sample_spec_valid);
204 LOAD_FUNC(pa_stream_drop);
205 LOAD_FUNC(pa_frame_size);
206 LOAD_FUNC(pa_strerror);
207 LOAD_FUNC(pa_context_get_state);
208 LOAD_FUNC(pa_stream_get_state);
209 LOAD_FUNC(pa_threaded_mainloop_signal);
210 LOAD_FUNC(pa_stream_peek);
211 LOAD_FUNC(pa_threaded_mainloop_wait);
212 LOAD_FUNC(pa_threaded_mainloop_unlock);
213 LOAD_FUNC(pa_threaded_mainloop_in_thread);
214 LOAD_FUNC(pa_context_new);
215 LOAD_FUNC(pa_threaded_mainloop_stop);
216 LOAD_FUNC(pa_context_disconnect);
217 LOAD_FUNC(pa_threaded_mainloop_start);
218 LOAD_FUNC(pa_threaded_mainloop_get_api);
219 LOAD_FUNC(pa_context_set_state_callback);
220 LOAD_FUNC(pa_stream_write);
221 LOAD_FUNC(pa_xfree);
222 LOAD_FUNC(pa_stream_connect_record);
223 LOAD_FUNC(pa_stream_connect_playback);
224 LOAD_FUNC(pa_stream_readable_size);
225 LOAD_FUNC(pa_stream_writable_size);
226 LOAD_FUNC(pa_stream_is_corked);
227 LOAD_FUNC(pa_stream_cork);
228 LOAD_FUNC(pa_stream_is_suspended);
229 LOAD_FUNC(pa_stream_get_device_name);
230 LOAD_FUNC(pa_stream_get_latency);
231 LOAD_FUNC(pa_path_get_filename);
232 LOAD_FUNC(pa_get_binary_name);
233 LOAD_FUNC(pa_threaded_mainloop_free);
234 LOAD_FUNC(pa_context_errno);
235 LOAD_FUNC(pa_xmalloc);
236 LOAD_FUNC(pa_stream_unref);
237 LOAD_FUNC(pa_threaded_mainloop_accept);
238 LOAD_FUNC(pa_stream_set_write_callback);
239 LOAD_FUNC(pa_threaded_mainloop_new);
240 LOAD_FUNC(pa_context_connect);
241 LOAD_FUNC(pa_stream_set_buffer_attr);
242 LOAD_FUNC(pa_stream_get_buffer_attr);
243 LOAD_FUNC(pa_stream_get_sample_spec);
244 LOAD_FUNC(pa_stream_get_time);
245 LOAD_FUNC(pa_stream_set_read_callback);
246 LOAD_FUNC(pa_stream_set_state_callback);
247 LOAD_FUNC(pa_stream_set_moved_callback);
248 LOAD_FUNC(pa_stream_set_underflow_callback);
249 LOAD_FUNC(pa_stream_new_with_proplist);
250 LOAD_FUNC(pa_stream_disconnect);
251 LOAD_FUNC(pa_threaded_mainloop_lock);
252 LOAD_FUNC(pa_channel_map_init_auto);
253 LOAD_FUNC(pa_channel_map_parse);
254 LOAD_FUNC(pa_channel_map_snprint);
255 LOAD_FUNC(pa_channel_map_equal);
256 LOAD_FUNC(pa_context_get_server_info);
257 LOAD_FUNC(pa_context_get_sink_info_by_name);
258 LOAD_FUNC(pa_context_get_sink_info_list);
259 LOAD_FUNC(pa_context_get_source_info_by_name);
260 LOAD_FUNC(pa_context_get_source_info_list);
261 LOAD_FUNC(pa_operation_get_state);
262 LOAD_FUNC(pa_operation_unref);
263 LOAD_FUNC(pa_proplist_new);
264 LOAD_FUNC(pa_proplist_free);
265 LOAD_FUNC(pa_proplist_set);
266 LOAD_FUNC(pa_channel_map_superset);
267 LOAD_FUNC(pa_stream_set_buffer_attr_callback);
268 LOAD_FUNC(pa_stream_begin_write);
269 #undef LOAD_FUNC
271 if(ret == ALC_FALSE)
273 CloseLib(pa_handle);
274 pa_handle = NULL;
277 #endif /* HAVE_DYNLOAD */
278 return ret;
282 /* Global flags and properties */
283 static pa_context_flags_t pulse_ctx_flags;
284 static pa_proplist *prop_filter;
287 /* PulseAudio Event Callbacks */
288 static void context_state_callback(pa_context *context, void *pdata)
290 pa_threaded_mainloop *loop = pdata;
291 pa_context_state_t state;
293 state = pa_context_get_state(context);
294 if(state == PA_CONTEXT_READY || !PA_CONTEXT_IS_GOOD(state))
295 pa_threaded_mainloop_signal(loop, 0);
298 static void stream_state_callback(pa_stream *stream, void *pdata)
300 pa_threaded_mainloop *loop = pdata;
301 pa_stream_state_t state;
303 state = pa_stream_get_state(stream);
304 if(state == PA_STREAM_READY || !PA_STREAM_IS_GOOD(state))
305 pa_threaded_mainloop_signal(loop, 0);
308 static void stream_success_callback(pa_stream *UNUSED(stream), int UNUSED(success), void *pdata)
310 pa_threaded_mainloop *loop = pdata;
311 pa_threaded_mainloop_signal(loop, 0);
314 static void wait_for_operation(pa_operation *op, pa_threaded_mainloop *loop)
316 if(op)
318 while(pa_operation_get_state(op) == PA_OPERATION_RUNNING)
319 pa_threaded_mainloop_wait(loop);
320 pa_operation_unref(op);
325 static pa_context *connect_context(pa_threaded_mainloop *loop, ALboolean silent)
327 const char *name = "OpenAL Soft";
328 char path_name[PATH_MAX];
329 pa_context_state_t state;
330 pa_context *context;
331 int err;
333 if(pa_get_binary_name(path_name, sizeof(path_name)))
334 name = pa_path_get_filename(path_name);
336 context = pa_context_new(pa_threaded_mainloop_get_api(loop), name);
337 if(!context)
339 ERR("pa_context_new() failed\n");
340 return NULL;
343 pa_context_set_state_callback(context, context_state_callback, loop);
345 if((err=pa_context_connect(context, NULL, pulse_ctx_flags, NULL)) >= 0)
347 while((state=pa_context_get_state(context)) != PA_CONTEXT_READY)
349 if(!PA_CONTEXT_IS_GOOD(state))
351 err = pa_context_errno(context);
352 if(err > 0) err = -err;
353 break;
356 pa_threaded_mainloop_wait(loop);
359 pa_context_set_state_callback(context, NULL, NULL);
361 if(err < 0)
363 if(!silent)
364 ERR("Context did not connect: %s\n", pa_strerror(err));
365 pa_context_unref(context);
366 return NULL;
369 return context;
373 static ALCboolean pulse_open(pa_threaded_mainloop **loop, pa_context **context,
374 void(*state_cb)(pa_context*,void*), void *ptr)
376 if(!(*loop = pa_threaded_mainloop_new()))
378 ERR("pa_threaded_mainloop_new() failed!\n");
379 return ALC_FALSE;
381 if(pa_threaded_mainloop_start(*loop) < 0)
383 ERR("pa_threaded_mainloop_start() failed\n");
384 goto error;
387 pa_threaded_mainloop_lock(*loop);
389 *context = connect_context(*loop, AL_FALSE);
390 if(!*context)
392 pa_threaded_mainloop_unlock(*loop);
393 pa_threaded_mainloop_stop(*loop);
394 goto error;
396 pa_context_set_state_callback(*context, state_cb, ptr);
398 pa_threaded_mainloop_unlock(*loop);
399 return ALC_TRUE;
401 error:
402 pa_threaded_mainloop_free(*loop);
403 *loop = NULL;
405 return ALC_FALSE;
408 static void pulse_close(pa_threaded_mainloop *loop, pa_context *context, pa_stream *stream)
410 pa_threaded_mainloop_lock(loop);
412 if(stream)
414 pa_stream_set_state_callback(stream, NULL, NULL);
415 pa_stream_set_moved_callback(stream, NULL, NULL);
416 pa_stream_set_write_callback(stream, NULL, NULL);
417 pa_stream_set_buffer_attr_callback(stream, NULL, NULL);
418 pa_stream_disconnect(stream);
419 pa_stream_unref(stream);
422 pa_context_disconnect(context);
423 pa_context_unref(context);
425 pa_threaded_mainloop_unlock(loop);
427 pa_threaded_mainloop_stop(loop);
428 pa_threaded_mainloop_free(loop);
432 typedef struct {
433 al_string name;
434 al_string device_name;
435 } DevMap;
436 TYPEDEF_VECTOR(DevMap, vector_DevMap)
438 static vector_DevMap PlaybackDevices;
439 static vector_DevMap CaptureDevices;
441 static void clear_devlist(vector_DevMap *list)
443 #define DEINIT_STRS(i) (AL_STRING_DEINIT((i)->name),AL_STRING_DEINIT((i)->device_name))
444 VECTOR_FOR_EACH(DevMap, *list, DEINIT_STRS);
445 #undef DEINIT_STRS
446 VECTOR_RESIZE(*list, 0);
450 typedef struct ALCpulsePlayback {
451 DERIVE_FROM_TYPE(ALCbackend);
453 al_string device_name;
455 pa_buffer_attr attr;
456 pa_sample_spec spec;
458 pa_threaded_mainloop *loop;
460 pa_stream *stream;
461 pa_context *context;
463 volatile ALboolean killNow;
464 althrd_t thread;
465 } ALCpulsePlayback;
467 static void ALCpulsePlayback_deviceCallback(pa_context *context, const pa_sink_info *info, int eol, void *pdata);
468 static void ALCpulsePlayback_probeDevices(void);
470 static void ALCpulsePlayback_bufferAttrCallback(pa_stream *stream, void *pdata);
471 static void ALCpulsePlayback_contextStateCallback(pa_context *context, void *pdata);
472 static void ALCpulsePlayback_streamStateCallback(pa_stream *stream, void *pdata);
473 static void ALCpulsePlayback_streamWriteCallback(pa_stream *p, size_t nbytes, void *userdata);
474 static void ALCpulsePlayback_sinkInfoCallback(pa_context *context, const pa_sink_info *info, int eol, void *pdata);
475 static void ALCpulsePlayback_sinkNameCallback(pa_context *context, const pa_sink_info *info, int eol, void *pdata);
476 static void ALCpulsePlayback_streamMovedCallback(pa_stream *stream, void *pdata);
477 static pa_stream *ALCpulsePlayback_connectStream(const char *device_name, pa_threaded_mainloop *loop,
478 pa_context *context, pa_stream_flags_t flags,
479 pa_buffer_attr *attr, pa_sample_spec *spec,
480 pa_channel_map *chanmap);
481 static int ALCpulsePlayback_mixerProc(void *ptr);
483 static void ALCpulsePlayback_Construct(ALCpulsePlayback *self, ALCdevice *device);
484 static void ALCpulsePlayback_Destruct(ALCpulsePlayback *self);
485 static ALCenum ALCpulsePlayback_open(ALCpulsePlayback *self, const ALCchar *name);
486 static void ALCpulsePlayback_close(ALCpulsePlayback *self);
487 static ALCboolean ALCpulsePlayback_reset(ALCpulsePlayback *self);
488 static ALCboolean ALCpulsePlayback_start(ALCpulsePlayback *self);
489 static void ALCpulsePlayback_stop(ALCpulsePlayback *self);
490 static DECLARE_FORWARD2(ALCpulsePlayback, ALCbackend, ALCenum, captureSamples, ALCvoid*, ALCuint)
491 static DECLARE_FORWARD(ALCpulsePlayback, ALCbackend, ALCuint, availableSamples)
492 static ALint64 ALCpulsePlayback_getLatency(ALCpulsePlayback *self);
493 static void ALCpulsePlayback_lock(ALCpulsePlayback *self);
494 static void ALCpulsePlayback_unlock(ALCpulsePlayback *self);
495 DECLARE_DEFAULT_ALLOCATORS(ALCpulsePlayback)
497 DEFINE_ALCBACKEND_VTABLE(ALCpulsePlayback);
500 static void ALCpulsePlayback_Construct(ALCpulsePlayback *self, ALCdevice *device)
502 ALCbackend_Construct(STATIC_CAST(ALCbackend, self), device);
503 SET_VTABLE2(ALCpulsePlayback, ALCbackend, self);
505 AL_STRING_INIT(self->device_name);
508 static void ALCpulsePlayback_Destruct(ALCpulsePlayback *self)
510 AL_STRING_DEINIT(self->device_name);
511 ALCbackend_Destruct(STATIC_CAST(ALCbackend, self));
515 static void ALCpulsePlayback_deviceCallback(pa_context *UNUSED(context), const pa_sink_info *info, int eol, void *pdata)
517 pa_threaded_mainloop *loop = pdata;
518 const DevMap *iter;
519 DevMap entry;
521 if(eol)
523 pa_threaded_mainloop_signal(loop, 0);
524 return;
527 #define MATCH_INFO_NAME(iter) (al_string_cmp_cstr((iter)->device_name, info->name) == 0)
528 VECTOR_FIND_IF(iter, const DevMap, PlaybackDevices, MATCH_INFO_NAME);
529 #undef MATCH_INFO_NAME
530 if(iter != VECTOR_ITER_END(PlaybackDevices))
531 return;
533 TRACE("Got device \"%s\", \"%s\"\n", info->description, info->name);
535 AL_STRING_INIT(entry.name);
536 AL_STRING_INIT(entry.device_name);
538 al_string_copy_cstr(&entry.name, info->description);
539 al_string_copy_cstr(&entry.device_name, info->name);
541 VECTOR_PUSH_BACK(PlaybackDevices, entry);
544 static void ALCpulsePlayback_probeDevices(void)
546 pa_threaded_mainloop *loop;
548 clear_devlist(&PlaybackDevices);
550 if((loop=pa_threaded_mainloop_new()) &&
551 pa_threaded_mainloop_start(loop) >= 0)
553 pa_context *context;
555 pa_threaded_mainloop_lock(loop);
556 context = connect_context(loop, AL_FALSE);
557 if(context)
559 pa_operation *o;
560 pa_stream_flags_t flags;
561 pa_sample_spec spec;
562 pa_stream *stream;
564 flags = PA_STREAM_FIX_FORMAT | PA_STREAM_FIX_RATE |
565 PA_STREAM_FIX_CHANNELS | PA_STREAM_DONT_MOVE;
567 spec.format = PA_SAMPLE_S16NE;
568 spec.rate = 44100;
569 spec.channels = 2;
571 stream = ALCpulsePlayback_connectStream(NULL, loop, context, flags,
572 NULL, &spec, NULL);
573 if(stream)
575 o = pa_context_get_sink_info_by_name(context, pa_stream_get_device_name(stream),
576 ALCpulsePlayback_deviceCallback, loop);
577 wait_for_operation(o, loop);
579 pa_stream_disconnect(stream);
580 pa_stream_unref(stream);
581 stream = NULL;
584 o = pa_context_get_sink_info_list(context, ALCpulsePlayback_deviceCallback, loop);
585 wait_for_operation(o, loop);
587 pa_context_disconnect(context);
588 pa_context_unref(context);
590 pa_threaded_mainloop_unlock(loop);
591 pa_threaded_mainloop_stop(loop);
593 if(loop)
594 pa_threaded_mainloop_free(loop);
598 static void ALCpulsePlayback_bufferAttrCallback(pa_stream *stream, void *pdata)
600 ALCpulsePlayback *self = pdata;
602 self->attr = *pa_stream_get_buffer_attr(stream);
603 TRACE("minreq=%d, tlength=%d, prebuf=%d\n", self->attr.minreq, self->attr.tlength, self->attr.prebuf);
606 static void ALCpulsePlayback_contextStateCallback(pa_context *context, void *pdata)
608 ALCpulsePlayback *self = pdata;
609 if(pa_context_get_state(context) == PA_CONTEXT_FAILED)
611 ERR("Received context failure!\n");
612 aluHandleDisconnect(STATIC_CAST(ALCbackend,self)->mDevice);
614 pa_threaded_mainloop_signal(self->loop, 0);
617 static void ALCpulsePlayback_streamStateCallback(pa_stream *stream, void *pdata)
619 ALCpulsePlayback *self = pdata;
620 if(pa_stream_get_state(stream) == PA_STREAM_FAILED)
622 ERR("Received stream failure!\n");
623 aluHandleDisconnect(STATIC_CAST(ALCbackend,self)->mDevice);
625 pa_threaded_mainloop_signal(self->loop, 0);
628 static void ALCpulsePlayback_streamWriteCallback(pa_stream* UNUSED(p), size_t UNUSED(nbytes), void *pdata)
630 ALCpulsePlayback *self = pdata;
631 pa_threaded_mainloop_signal(self->loop, 0);
634 static void ALCpulsePlayback_sinkInfoCallback(pa_context *UNUSED(context), const pa_sink_info *info, int eol, void *pdata)
636 ALCpulsePlayback *self = pdata;
637 ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
638 const struct {
639 const char *str;
640 enum DevFmtChannels chans;
641 } chanmaps[] = {
642 { "front-left,front-right,front-center,lfe,rear-left,rear-right,side-left,side-right",
643 DevFmtX71 },
644 { "front-left,front-right,front-center,lfe,rear-center,side-left,side-right",
645 DevFmtX61 },
646 { "front-left,front-right,front-center,lfe,side-left,side-right",
647 DevFmtX51 },
648 { "front-left,front-right,front-center,lfe,rear-left,rear-right",
649 DevFmtX51Rear },
650 { "front-left,front-right,rear-left,rear-right", DevFmtQuad },
651 { "front-left,front-right", DevFmtStereo },
652 { "mono", DevFmtMono },
653 { NULL, 0 }
655 int i;
657 if(eol)
659 pa_threaded_mainloop_signal(self->loop, 0);
660 return;
663 for(i = 0;chanmaps[i].str;i++)
665 pa_channel_map map;
666 if(!pa_channel_map_parse(&map, chanmaps[i].str))
667 continue;
669 if(pa_channel_map_superset(&info->channel_map, &map))
671 if(!(device->Flags&DEVICE_CHANNELS_REQUEST))
672 device->FmtChans = chanmaps[i].chans;
673 break;
677 if(info->active_port)
678 TRACE("Active port: %s (%s)\n", info->active_port->name, info->active_port->description);
679 device->IsHeadphones = (info->active_port &&
680 strcmp(info->active_port->name, "analog-output-headphones") == 0 &&
681 device->FmtChans == DevFmtStereo);
683 if(!chanmaps[i].str)
685 char chanmap_str[PA_CHANNEL_MAP_SNPRINT_MAX] = "";
686 pa_channel_map_snprint(chanmap_str, sizeof(chanmap_str), &info->channel_map);
687 WARN("Failed to find format for channel map:\n %s\n", chanmap_str);
691 static void ALCpulsePlayback_sinkNameCallback(pa_context *UNUSED(context), const pa_sink_info *info, int eol, void *pdata)
693 ALCpulsePlayback *self = pdata;
694 ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
696 if(eol)
698 pa_threaded_mainloop_signal(self->loop, 0);
699 return;
702 al_string_copy_cstr(&device->DeviceName, info->description);
706 static void ALCpulsePlayback_streamMovedCallback(pa_stream *stream, void *pdata)
708 ALCpulsePlayback *self = pdata;
710 al_string_copy_cstr(&self->device_name, pa_stream_get_device_name(stream));
712 TRACE("Stream moved to %s\n", al_string_get_cstr(self->device_name));
716 static pa_stream *ALCpulsePlayback_connectStream(const char *device_name,
717 pa_threaded_mainloop *loop, pa_context *context,
718 pa_stream_flags_t flags, pa_buffer_attr *attr, pa_sample_spec *spec,
719 pa_channel_map *chanmap)
721 pa_stream_state_t state;
722 pa_stream *stream;
724 stream = pa_stream_new_with_proplist(context, "Playback Stream", spec, chanmap, prop_filter);
725 if(!stream)
727 ERR("pa_stream_new_with_proplist() failed: %s\n", pa_strerror(pa_context_errno(context)));
728 return NULL;
731 pa_stream_set_state_callback(stream, stream_state_callback, loop);
733 if(pa_stream_connect_playback(stream, device_name, attr, flags, NULL, NULL) < 0)
735 ERR("Stream did not connect: %s\n", pa_strerror(pa_context_errno(context)));
736 pa_stream_unref(stream);
737 return NULL;
740 while((state=pa_stream_get_state(stream)) != PA_STREAM_READY)
742 if(!PA_STREAM_IS_GOOD(state))
744 ERR("Stream did not get ready: %s\n", pa_strerror(pa_context_errno(context)));
745 pa_stream_unref(stream);
746 return NULL;
749 pa_threaded_mainloop_wait(loop);
751 pa_stream_set_state_callback(stream, NULL, NULL);
753 return stream;
757 static int ALCpulsePlayback_mixerProc(void *ptr)
759 ALCpulsePlayback *self = ptr;
760 ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
761 ALuint buffer_size;
762 ALint update_size;
763 size_t frame_size;
764 ssize_t len;
766 SetRTPriority();
767 althrd_setname(althrd_current(), MIXER_THREAD_NAME);
769 pa_threaded_mainloop_lock(self->loop);
770 frame_size = pa_frame_size(&self->spec);
771 update_size = device->UpdateSize * frame_size;
773 /* Sanitize buffer metrics, in case we actually have less than what we
774 * asked for. */
775 buffer_size = minu(update_size*device->NumUpdates, self->attr.tlength);
776 update_size = minu(update_size, buffer_size/2);
777 do {
778 len = pa_stream_writable_size(self->stream) - self->attr.tlength +
779 buffer_size;
780 if(len < update_size)
782 if(pa_stream_is_corked(self->stream) == 1)
784 pa_operation *o;
785 o = pa_stream_cork(self->stream, 0, NULL, NULL);
786 if(o) pa_operation_unref(o);
788 pa_threaded_mainloop_wait(self->loop);
789 continue;
791 len -= len%update_size;
793 while(len > 0)
795 size_t newlen = len;
796 void *buf;
797 pa_free_cb_t free_func = NULL;
799 if(pa_stream_begin_write(self->stream, &buf, &newlen) < 0)
801 buf = pa_xmalloc(newlen);
802 free_func = pa_xfree;
805 aluMixData(device, buf, newlen/frame_size);
807 pa_stream_write(self->stream, buf, newlen, free_func, 0, PA_SEEK_RELATIVE);
808 len -= newlen;
810 } while(!self->killNow && device->Connected);
811 pa_threaded_mainloop_unlock(self->loop);
813 return 0;
817 static ALCenum ALCpulsePlayback_open(ALCpulsePlayback *self, const ALCchar *name)
819 const char *pulse_name = NULL;
820 pa_stream_flags_t flags;
821 pa_sample_spec spec;
822 pa_operation *o;
824 if(name)
826 const DevMap *iter;
828 if(VECTOR_SIZE(PlaybackDevices) == 0)
829 ALCpulsePlayback_probeDevices();
831 #define MATCH_NAME(iter) (al_string_cmp_cstr((iter)->name, name) == 0)
832 VECTOR_FIND_IF(iter, const DevMap, PlaybackDevices, MATCH_NAME);
833 #undef MATCH_NAME
834 if(iter == VECTOR_ITER_END(PlaybackDevices))
835 return ALC_INVALID_VALUE;
836 pulse_name = al_string_get_cstr(iter->device_name);
839 if(!pulse_open(&self->loop, &self->context, ALCpulsePlayback_contextStateCallback, self))
840 return ALC_INVALID_VALUE;
842 pa_threaded_mainloop_lock(self->loop);
844 flags = PA_STREAM_FIX_FORMAT | PA_STREAM_FIX_RATE |
845 PA_STREAM_FIX_CHANNELS;
846 if(!GetConfigValueBool("pulse", "allow-moves", 0))
847 flags |= PA_STREAM_DONT_MOVE;
849 spec.format = PA_SAMPLE_S16NE;
850 spec.rate = 44100;
851 spec.channels = 2;
853 TRACE("Connecting to \"%s\"\n", pulse_name ? pulse_name : "(default)");
854 self->stream = ALCpulsePlayback_connectStream(pulse_name, self->loop, self->context,
855 flags, NULL, &spec, NULL);
856 if(!self->stream)
858 pa_threaded_mainloop_unlock(self->loop);
859 pulse_close(self->loop, self->context, self->stream);
860 self->loop = NULL;
861 self->context = NULL;
862 return ALC_INVALID_VALUE;
864 pa_stream_set_moved_callback(self->stream, ALCpulsePlayback_streamMovedCallback, self);
866 al_string_copy_cstr(&self->device_name, pa_stream_get_device_name(self->stream));
867 o = pa_context_get_sink_info_by_name(self->context,
868 al_string_get_cstr(self->device_name),
869 ALCpulsePlayback_sinkNameCallback, self);
870 wait_for_operation(o, self->loop);
872 pa_threaded_mainloop_unlock(self->loop);
874 return ALC_NO_ERROR;
877 static void ALCpulsePlayback_close(ALCpulsePlayback *self)
879 pulse_close(self->loop, self->context, self->stream);
880 self->loop = NULL;
881 self->context = NULL;
882 self->stream = NULL;
884 al_string_clear(&self->device_name);
887 static ALCboolean ALCpulsePlayback_reset(ALCpulsePlayback *self)
889 ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
890 pa_stream_flags_t flags = 0;
891 const char *mapname = NULL;
892 pa_channel_map chanmap;
893 pa_operation *o;
894 ALuint len;
896 pa_threaded_mainloop_lock(self->loop);
898 if(self->stream)
900 pa_stream_set_state_callback(self->stream, NULL, NULL);
901 pa_stream_set_moved_callback(self->stream, NULL, NULL);
902 pa_stream_set_write_callback(self->stream, NULL, NULL);
903 pa_stream_set_buffer_attr_callback(self->stream, NULL, NULL);
904 pa_stream_disconnect(self->stream);
905 pa_stream_unref(self->stream);
906 self->stream = NULL;
909 o = pa_context_get_sink_info_by_name(self->context, al_string_get_cstr(self->device_name),
910 ALCpulsePlayback_sinkInfoCallback, self);
911 wait_for_operation(o, self->loop);
913 if(!(device->Flags&DEVICE_FREQUENCY_REQUEST))
914 flags |= PA_STREAM_FIX_RATE;
915 flags |= PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE;
916 flags |= PA_STREAM_ADJUST_LATENCY;
917 flags |= PA_STREAM_START_CORKED;
918 if(!GetConfigValueBool("pulse", "allow-moves", 0))
919 flags |= PA_STREAM_DONT_MOVE;
921 switch(device->FmtType)
923 case DevFmtByte:
924 device->FmtType = DevFmtUByte;
925 /* fall-through */
926 case DevFmtUByte:
927 self->spec.format = PA_SAMPLE_U8;
928 break;
929 case DevFmtUShort:
930 device->FmtType = DevFmtShort;
931 /* fall-through */
932 case DevFmtShort:
933 self->spec.format = PA_SAMPLE_S16NE;
934 break;
935 case DevFmtUInt:
936 device->FmtType = DevFmtInt;
937 /* fall-through */
938 case DevFmtInt:
939 self->spec.format = PA_SAMPLE_S32NE;
940 break;
941 case DevFmtFloat:
942 self->spec.format = PA_SAMPLE_FLOAT32NE;
943 break;
945 self->spec.rate = device->Frequency;
946 self->spec.channels = ChannelsFromDevFmt(device->FmtChans);
948 if(pa_sample_spec_valid(&self->spec) == 0)
950 ERR("Invalid sample format\n");
951 pa_threaded_mainloop_unlock(self->loop);
952 return ALC_FALSE;
955 switch(device->FmtChans)
957 case DevFmtMono:
958 mapname = "mono";
959 break;
960 case DevFmtBFormat3D:
961 device->FmtChans = DevFmtStereo;
962 /*fall-through*/
963 case DevFmtStereo:
964 mapname = "front-left,front-right";
965 break;
966 case DevFmtQuad:
967 mapname = "front-left,front-right,rear-left,rear-right";
968 break;
969 case DevFmtX51:
970 mapname = "front-left,front-right,front-center,lfe,side-left,side-right";
971 break;
972 case DevFmtX51Rear:
973 mapname = "front-left,front-right,front-center,lfe,rear-left,rear-right";
974 break;
975 case DevFmtX61:
976 mapname = "front-left,front-right,front-center,lfe,rear-center,side-left,side-right";
977 break;
978 case DevFmtX71:
979 mapname = "front-left,front-right,front-center,lfe,rear-left,rear-right,side-left,side-right";
980 break;
982 if(!pa_channel_map_parse(&chanmap, mapname))
984 ERR("Failed to build channel map for %s\n", DevFmtChannelsString(device->FmtChans));
985 pa_threaded_mainloop_unlock(self->loop);
986 return ALC_FALSE;
988 SetDefaultWFXChannelOrder(device);
990 self->attr.fragsize = -1;
991 self->attr.prebuf = 0;
992 self->attr.minreq = device->UpdateSize * pa_frame_size(&self->spec);
993 self->attr.tlength = self->attr.minreq * maxu(device->NumUpdates, 2);
994 self->attr.maxlength = -1;
996 self->stream = ALCpulsePlayback_connectStream(al_string_get_cstr(self->device_name),
997 self->loop, self->context, flags,
998 &self->attr, &self->spec, &chanmap);
999 if(!self->stream)
1001 pa_threaded_mainloop_unlock(self->loop);
1002 return ALC_FALSE;
1004 pa_stream_set_state_callback(self->stream, ALCpulsePlayback_streamStateCallback, self);
1005 pa_stream_set_moved_callback(self->stream, ALCpulsePlayback_streamMovedCallback, self);
1006 pa_stream_set_write_callback(self->stream, ALCpulsePlayback_streamWriteCallback, self);
1008 self->spec = *(pa_stream_get_sample_spec(self->stream));
1009 if(device->Frequency != self->spec.rate)
1011 /* Server updated our playback rate, so modify the buffer attribs
1012 * accordingly. */
1013 device->NumUpdates = (ALuint)((ALdouble)device->NumUpdates / device->Frequency *
1014 self->spec.rate + 0.5);
1015 self->attr.minreq = device->UpdateSize * pa_frame_size(&self->spec);
1016 self->attr.tlength = self->attr.minreq * clampu(device->NumUpdates, 2, 16);
1017 self->attr.maxlength = -1;
1018 self->attr.prebuf = 0;
1020 o = pa_stream_set_buffer_attr(self->stream, &self->attr,
1021 stream_success_callback, self->loop);
1022 wait_for_operation(o, self->loop);
1024 device->Frequency = self->spec.rate;
1027 pa_stream_set_buffer_attr_callback(self->stream, ALCpulsePlayback_bufferAttrCallback, self);
1028 ALCpulsePlayback_bufferAttrCallback(self->stream, self);
1030 len = self->attr.minreq / pa_frame_size(&self->spec);
1031 device->NumUpdates = (ALuint)((ALdouble)device->NumUpdates/len*device->UpdateSize + 0.5);
1032 device->NumUpdates = clampu(device->NumUpdates, 2, 16);
1033 device->UpdateSize = len;
1035 pa_threaded_mainloop_unlock(self->loop);
1036 return ALC_TRUE;
1039 static ALCboolean ALCpulsePlayback_start(ALCpulsePlayback *self)
1041 self->killNow = AL_FALSE;
1042 if(althrd_create(&self->thread, ALCpulsePlayback_mixerProc, self) != althrd_success)
1043 return ALC_FALSE;
1044 return ALC_TRUE;
1047 static void ALCpulsePlayback_stop(ALCpulsePlayback *self)
1049 pa_operation *o;
1050 int res;
1052 if(!self->stream || self->killNow)
1053 return;
1055 self->killNow = AL_TRUE;
1056 /* Signal the main loop in case PulseAudio isn't sending us audio requests
1057 * (e.g. if the device is suspended). We need to lock the mainloop in case
1058 * the mixer is between checking the killNow flag but before waiting for
1059 * the signal.
1061 pa_threaded_mainloop_lock(self->loop);
1062 pa_threaded_mainloop_unlock(self->loop);
1063 pa_threaded_mainloop_signal(self->loop, 0);
1064 althrd_join(self->thread, &res);
1066 pa_threaded_mainloop_lock(self->loop);
1068 o = pa_stream_cork(self->stream, 1, stream_success_callback, self->loop);
1069 wait_for_operation(o, self->loop);
1071 pa_threaded_mainloop_unlock(self->loop);
1075 static ALint64 ALCpulsePlayback_getLatency(ALCpulsePlayback *self)
1077 pa_usec_t latency = 0;
1078 int neg, err;
1080 if((err=pa_stream_get_latency(self->stream, &latency, &neg)) != 0)
1082 /* FIXME: if err = -PA_ERR_NODATA, it means we were called too soon
1083 * after starting the stream and no timing info has been received from
1084 * the server yet. Should we wait, possibly stalling the app, or give a
1085 * dummy value? Either way, it shouldn't be 0. */
1086 if(err != -PA_ERR_NODATA)
1087 ERR("Failed to get stream latency: 0x%x\n", err);
1088 return 0;
1091 if(neg) latency = 0;
1092 return (ALint64)minu64(latency, U64(0x7fffffffffffffff)/1000) * 1000;
1096 static void ALCpulsePlayback_lock(ALCpulsePlayback *self)
1098 pa_threaded_mainloop_lock(self->loop);
1101 static void ALCpulsePlayback_unlock(ALCpulsePlayback *self)
1103 pa_threaded_mainloop_unlock(self->loop);
1107 typedef struct ALCpulseCapture {
1108 DERIVE_FROM_TYPE(ALCbackend);
1110 al_string device_name;
1112 const void *cap_store;
1113 size_t cap_len;
1114 size_t cap_remain;
1116 ALCuint last_readable;
1118 pa_buffer_attr attr;
1119 pa_sample_spec spec;
1121 pa_threaded_mainloop *loop;
1123 pa_stream *stream;
1124 pa_context *context;
1125 } ALCpulseCapture;
1127 static void ALCpulseCapture_deviceCallback(pa_context *context, const pa_source_info *info, int eol, void *pdata);
1128 static void ALCpulseCapture_probeDevices(void);
1130 static void ALCpulseCapture_contextStateCallback(pa_context *context, void *pdata);
1131 static void ALCpulseCapture_streamStateCallback(pa_stream *stream, void *pdata);
1132 static void ALCpulseCapture_sourceNameCallback(pa_context *context, const pa_source_info *info, int eol, void *pdata);
1133 static void ALCpulseCapture_streamMovedCallback(pa_stream *stream, void *pdata);
1134 static pa_stream *ALCpulseCapture_connectStream(const char *device_name,
1135 pa_threaded_mainloop *loop, pa_context *context,
1136 pa_stream_flags_t flags, pa_buffer_attr *attr,
1137 pa_sample_spec *spec, pa_channel_map *chanmap);
1139 static void ALCpulseCapture_Construct(ALCpulseCapture *self, ALCdevice *device);
1140 static void ALCpulseCapture_Destruct(ALCpulseCapture *self);
1141 static ALCenum ALCpulseCapture_open(ALCpulseCapture *self, const ALCchar *name);
1142 static void ALCpulseCapture_close(ALCpulseCapture *self);
1143 static DECLARE_FORWARD(ALCpulseCapture, ALCbackend, ALCboolean, reset)
1144 static ALCboolean ALCpulseCapture_start(ALCpulseCapture *self);
1145 static void ALCpulseCapture_stop(ALCpulseCapture *self);
1146 static ALCenum ALCpulseCapture_captureSamples(ALCpulseCapture *self, ALCvoid *buffer, ALCuint samples);
1147 static ALCuint ALCpulseCapture_availableSamples(ALCpulseCapture *self);
1148 static ALint64 ALCpulseCapture_getLatency(ALCpulseCapture *self);
1149 static void ALCpulseCapture_lock(ALCpulseCapture *self);
1150 static void ALCpulseCapture_unlock(ALCpulseCapture *self);
1151 DECLARE_DEFAULT_ALLOCATORS(ALCpulseCapture)
1153 DEFINE_ALCBACKEND_VTABLE(ALCpulseCapture);
1156 static void ALCpulseCapture_Construct(ALCpulseCapture *self, ALCdevice *device)
1158 ALCbackend_Construct(STATIC_CAST(ALCbackend, self), device);
1159 SET_VTABLE2(ALCpulseCapture, ALCbackend, self);
1161 AL_STRING_INIT(self->device_name);
1164 static void ALCpulseCapture_Destruct(ALCpulseCapture *self)
1166 AL_STRING_DEINIT(self->device_name);
1167 ALCbackend_Destruct(STATIC_CAST(ALCbackend, self));
1171 static void ALCpulseCapture_deviceCallback(pa_context *UNUSED(context), const pa_source_info *info, int eol, void *pdata)
1173 pa_threaded_mainloop *loop = pdata;
1174 const DevMap *iter;
1175 DevMap entry;
1177 if(eol)
1179 pa_threaded_mainloop_signal(loop, 0);
1180 return;
1183 #define MATCH_INFO_NAME(iter) (al_string_cmp_cstr((iter)->device_name, info->name) == 0)
1184 VECTOR_FIND_IF(iter, const DevMap, CaptureDevices, MATCH_INFO_NAME);
1185 #undef MATCH_INFO_NAME
1186 if(iter != VECTOR_ITER_END(CaptureDevices))
1187 return;
1189 TRACE("Got device \"%s\", \"%s\"\n", info->description, info->name);
1191 AL_STRING_INIT(entry.name);
1192 AL_STRING_INIT(entry.device_name);
1194 al_string_copy_cstr(&entry.name, info->description);
1195 al_string_copy_cstr(&entry.device_name, info->name);
1197 VECTOR_PUSH_BACK(CaptureDevices, entry);
1200 static void ALCpulseCapture_probeDevices(void)
1202 pa_threaded_mainloop *loop;
1204 clear_devlist(&CaptureDevices);
1206 if((loop=pa_threaded_mainloop_new()) &&
1207 pa_threaded_mainloop_start(loop) >= 0)
1209 pa_context *context;
1211 pa_threaded_mainloop_lock(loop);
1212 context = connect_context(loop, AL_FALSE);
1213 if(context)
1215 pa_operation *o;
1216 pa_stream_flags_t flags;
1217 pa_sample_spec spec;
1218 pa_stream *stream;
1220 flags = PA_STREAM_FIX_FORMAT | PA_STREAM_FIX_RATE |
1221 PA_STREAM_FIX_CHANNELS | PA_STREAM_DONT_MOVE;
1223 spec.format = PA_SAMPLE_S16NE;
1224 spec.rate = 44100;
1225 spec.channels = 1;
1227 stream = ALCpulseCapture_connectStream(NULL, loop, context, flags,
1228 NULL, &spec, NULL);
1229 if(stream)
1231 o = pa_context_get_source_info_by_name(context, pa_stream_get_device_name(stream),
1232 ALCpulseCapture_deviceCallback, loop);
1233 wait_for_operation(o, loop);
1235 pa_stream_disconnect(stream);
1236 pa_stream_unref(stream);
1237 stream = NULL;
1240 o = pa_context_get_source_info_list(context, ALCpulseCapture_deviceCallback, loop);
1241 wait_for_operation(o, loop);
1243 pa_context_disconnect(context);
1244 pa_context_unref(context);
1246 pa_threaded_mainloop_unlock(loop);
1247 pa_threaded_mainloop_stop(loop);
1249 if(loop)
1250 pa_threaded_mainloop_free(loop);
1254 static void ALCpulseCapture_contextStateCallback(pa_context *context, void *pdata)
1256 ALCpulseCapture *self = pdata;
1257 if(pa_context_get_state(context) == PA_CONTEXT_FAILED)
1259 ERR("Received context failure!\n");
1260 aluHandleDisconnect(STATIC_CAST(ALCbackend,self)->mDevice);
1262 pa_threaded_mainloop_signal(self->loop, 0);
1265 static void ALCpulseCapture_streamStateCallback(pa_stream *stream, void *pdata)
1267 ALCpulseCapture *self = pdata;
1268 if(pa_stream_get_state(stream) == PA_STREAM_FAILED)
1270 ERR("Received stream failure!\n");
1271 aluHandleDisconnect(STATIC_CAST(ALCbackend,self)->mDevice);
1273 pa_threaded_mainloop_signal(self->loop, 0);
1277 static void ALCpulseCapture_sourceNameCallback(pa_context *UNUSED(context), const pa_source_info *info, int eol, void *pdata)
1279 ALCpulseCapture *self = pdata;
1280 ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
1282 if(eol)
1284 pa_threaded_mainloop_signal(self->loop, 0);
1285 return;
1288 al_string_copy_cstr(&device->DeviceName, info->description);
1292 static void ALCpulseCapture_streamMovedCallback(pa_stream *stream, void *pdata)
1294 ALCpulseCapture *self = pdata;
1296 al_string_copy_cstr(&self->device_name, pa_stream_get_device_name(stream));
1298 TRACE("Stream moved to %s\n", al_string_get_cstr(self->device_name));
1302 static pa_stream *ALCpulseCapture_connectStream(const char *device_name,
1303 pa_threaded_mainloop *loop, pa_context *context,
1304 pa_stream_flags_t flags, pa_buffer_attr *attr, pa_sample_spec *spec,
1305 pa_channel_map *chanmap)
1307 pa_stream_state_t state;
1308 pa_stream *stream;
1310 stream = pa_stream_new_with_proplist(context, "Capture Stream", spec, chanmap, prop_filter);
1311 if(!stream)
1313 ERR("pa_stream_new_with_proplist() failed: %s\n", pa_strerror(pa_context_errno(context)));
1314 return NULL;
1317 pa_stream_set_state_callback(stream, stream_state_callback, loop);
1319 if(pa_stream_connect_record(stream, device_name, attr, flags) < 0)
1321 ERR("Stream did not connect: %s\n", pa_strerror(pa_context_errno(context)));
1322 pa_stream_unref(stream);
1323 return NULL;
1326 while((state=pa_stream_get_state(stream)) != PA_STREAM_READY)
1328 if(!PA_STREAM_IS_GOOD(state))
1330 ERR("Stream did not get ready: %s\n", pa_strerror(pa_context_errno(context)));
1331 pa_stream_unref(stream);
1332 return NULL;
1335 pa_threaded_mainloop_wait(loop);
1337 pa_stream_set_state_callback(stream, NULL, NULL);
1339 return stream;
1343 static ALCenum ALCpulseCapture_open(ALCpulseCapture *self, const ALCchar *name)
1345 ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
1346 const char *pulse_name = NULL;
1347 pa_stream_flags_t flags = 0;
1348 pa_channel_map chanmap;
1349 pa_operation *o;
1350 ALuint samples;
1352 if(name)
1354 const DevMap *iter;
1356 if(VECTOR_SIZE(CaptureDevices) == 0)
1357 ALCpulseCapture_probeDevices();
1359 #define MATCH_NAME(iter) (al_string_cmp_cstr((iter)->name, name) == 0)
1360 VECTOR_FIND_IF(iter, const DevMap, CaptureDevices, MATCH_NAME);
1361 #undef MATCH_NAME
1362 if(iter == VECTOR_ITER_END(CaptureDevices))
1363 return ALC_INVALID_VALUE;
1364 pulse_name = al_string_get_cstr(iter->device_name);
1367 if(!pulse_open(&self->loop, &self->context, ALCpulseCapture_contextStateCallback, self))
1368 return ALC_INVALID_VALUE;
1370 pa_threaded_mainloop_lock(self->loop);
1372 self->spec.rate = device->Frequency;
1373 self->spec.channels = ChannelsFromDevFmt(device->FmtChans);
1375 switch(device->FmtType)
1377 case DevFmtUByte:
1378 self->spec.format = PA_SAMPLE_U8;
1379 break;
1380 case DevFmtShort:
1381 self->spec.format = PA_SAMPLE_S16NE;
1382 break;
1383 case DevFmtInt:
1384 self->spec.format = PA_SAMPLE_S32NE;
1385 break;
1386 case DevFmtFloat:
1387 self->spec.format = PA_SAMPLE_FLOAT32NE;
1388 break;
1389 case DevFmtByte:
1390 case DevFmtUShort:
1391 case DevFmtUInt:
1392 ERR("%s capture samples not supported\n", DevFmtTypeString(device->FmtType));
1393 pa_threaded_mainloop_unlock(self->loop);
1394 goto fail;
1397 if(pa_sample_spec_valid(&self->spec) == 0)
1399 ERR("Invalid sample format\n");
1400 pa_threaded_mainloop_unlock(self->loop);
1401 goto fail;
1404 if(!pa_channel_map_init_auto(&chanmap, self->spec.channels, PA_CHANNEL_MAP_WAVEEX))
1406 ERR("Couldn't build map for channel count (%d)!\n", self->spec.channels);
1407 pa_threaded_mainloop_unlock(self->loop);
1408 goto fail;
1411 samples = device->UpdateSize * device->NumUpdates;
1412 samples = maxu(samples, 100 * device->Frequency / 1000);
1414 self->attr.minreq = -1;
1415 self->attr.prebuf = -1;
1416 self->attr.maxlength = samples * pa_frame_size(&self->spec);
1417 self->attr.tlength = -1;
1418 self->attr.fragsize = minu(samples, 50*device->Frequency/1000) *
1419 pa_frame_size(&self->spec);
1421 flags |= PA_STREAM_START_CORKED|PA_STREAM_ADJUST_LATENCY;
1422 if(!GetConfigValueBool("pulse", "allow-moves", 0))
1423 flags |= PA_STREAM_DONT_MOVE;
1425 TRACE("Connecting to \"%s\"\n", pulse_name ? pulse_name : "(default)");
1426 self->stream = ALCpulseCapture_connectStream(pulse_name, self->loop, self->context,
1427 flags, &self->attr, &self->spec,
1428 &chanmap);
1429 if(!self->stream)
1431 pa_threaded_mainloop_unlock(self->loop);
1432 goto fail;
1434 pa_stream_set_moved_callback(self->stream, ALCpulseCapture_streamMovedCallback, self);
1435 pa_stream_set_state_callback(self->stream, ALCpulseCapture_streamStateCallback, self);
1437 al_string_copy_cstr(&self->device_name, pa_stream_get_device_name(self->stream));
1438 o = pa_context_get_source_info_by_name(self->context,
1439 al_string_get_cstr(self->device_name),
1440 ALCpulseCapture_sourceNameCallback, self);
1441 wait_for_operation(o, self->loop);
1443 pa_threaded_mainloop_unlock(self->loop);
1444 return ALC_NO_ERROR;
1446 fail:
1447 pulse_close(self->loop, self->context, self->stream);
1448 self->loop = NULL;
1449 self->context = NULL;
1450 self->stream = NULL;
1452 return ALC_INVALID_VALUE;
1455 static void ALCpulseCapture_close(ALCpulseCapture *self)
1457 pulse_close(self->loop, self->context, self->stream);
1458 self->loop = NULL;
1459 self->context = NULL;
1460 self->stream = NULL;
1462 al_string_clear(&self->device_name);
1465 static ALCboolean ALCpulseCapture_start(ALCpulseCapture *self)
1467 pa_operation *o;
1468 o = pa_stream_cork(self->stream, 0, stream_success_callback, self->loop);
1469 wait_for_operation(o, self->loop);
1471 return ALC_TRUE;
1474 static void ALCpulseCapture_stop(ALCpulseCapture *self)
1476 pa_operation *o;
1477 o = pa_stream_cork(self->stream, 1, stream_success_callback, self->loop);
1478 wait_for_operation(o, self->loop);
1481 static ALCenum ALCpulseCapture_captureSamples(ALCpulseCapture *self, ALCvoid *buffer, ALCuint samples)
1483 ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
1484 ALCuint todo = samples * pa_frame_size(&self->spec);
1486 /* Capture is done in fragment-sized chunks, so we loop until we get all
1487 * that's available */
1488 self->last_readable -= todo;
1489 while(todo > 0)
1491 size_t rem = todo;
1493 if(self->cap_len == 0)
1495 pa_stream_state_t state;
1497 state = pa_stream_get_state(self->stream);
1498 if(!PA_STREAM_IS_GOOD(state))
1500 aluHandleDisconnect(device);
1501 break;
1503 if(pa_stream_peek(self->stream, &self->cap_store, &self->cap_len) < 0)
1505 ERR("pa_stream_peek() failed: %s\n",
1506 pa_strerror(pa_context_errno(self->context)));
1507 aluHandleDisconnect(device);
1508 break;
1510 self->cap_remain = self->cap_len;
1512 if(rem > self->cap_remain)
1513 rem = self->cap_remain;
1515 memcpy(buffer, self->cap_store, rem);
1517 buffer = (ALbyte*)buffer + rem;
1518 todo -= rem;
1520 self->cap_store = (ALbyte*)self->cap_store + rem;
1521 self->cap_remain -= rem;
1522 if(self->cap_remain == 0)
1524 pa_stream_drop(self->stream);
1525 self->cap_len = 0;
1528 if(todo > 0)
1529 memset(buffer, ((device->FmtType==DevFmtUByte) ? 0x80 : 0), todo);
1531 return ALC_NO_ERROR;
1534 static ALCuint ALCpulseCapture_availableSamples(ALCpulseCapture *self)
1536 ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
1537 size_t readable = self->cap_remain;
1539 if(device->Connected)
1541 ssize_t got = pa_stream_readable_size(self->stream);
1542 if(got < 0)
1544 ERR("pa_stream_readable_size() failed: %s\n", pa_strerror(got));
1545 aluHandleDisconnect(device);
1547 else if((size_t)got > self->cap_len)
1548 readable += got - self->cap_len;
1551 if(self->last_readable < readable)
1552 self->last_readable = readable;
1553 return self->last_readable / pa_frame_size(&self->spec);
1557 static ALint64 ALCpulseCapture_getLatency(ALCpulseCapture *self)
1559 pa_usec_t latency = 0;
1560 int neg;
1562 if(pa_stream_get_latency(self->stream, &latency, &neg) != 0)
1564 ERR("Failed to get stream latency!\n");
1565 return 0;
1568 if(neg) latency = 0;
1569 return (ALint64)minu64(latency, U64(0x7fffffffffffffff)/1000) * 1000;
1573 static void ALCpulseCapture_lock(ALCpulseCapture *self)
1575 pa_threaded_mainloop_lock(self->loop);
1578 static void ALCpulseCapture_unlock(ALCpulseCapture *self)
1580 pa_threaded_mainloop_unlock(self->loop);
1584 typedef struct ALCpulseBackendFactory {
1585 DERIVE_FROM_TYPE(ALCbackendFactory);
1586 } ALCpulseBackendFactory;
1587 #define ALCPULSEBACKENDFACTORY_INITIALIZER { { GET_VTABLE2(ALCpulseBackendFactory, ALCbackendFactory) } }
1589 static ALCboolean ALCpulseBackendFactory_init(ALCpulseBackendFactory *self);
1590 static void ALCpulseBackendFactory_deinit(ALCpulseBackendFactory *self);
1591 static ALCboolean ALCpulseBackendFactory_querySupport(ALCpulseBackendFactory *self, ALCbackend_Type type);
1592 static void ALCpulseBackendFactory_probe(ALCpulseBackendFactory *self, enum DevProbe type);
1593 static ALCbackend* ALCpulseBackendFactory_createBackend(ALCpulseBackendFactory *self, ALCdevice *device, ALCbackend_Type type);
1595 DEFINE_ALCBACKENDFACTORY_VTABLE(ALCpulseBackendFactory);
1598 static ALCboolean ALCpulseBackendFactory_init(ALCpulseBackendFactory* UNUSED(self))
1600 ALCboolean ret = ALC_FALSE;
1602 VECTOR_INIT(PlaybackDevices);
1603 VECTOR_INIT(CaptureDevices);
1605 if(pulse_load())
1607 pa_threaded_mainloop *loop;
1609 pulse_ctx_flags = 0;
1610 if(!GetConfigValueBool("pulse", "spawn-server", 1))
1611 pulse_ctx_flags |= PA_CONTEXT_NOAUTOSPAWN;
1613 if((loop=pa_threaded_mainloop_new()) &&
1614 pa_threaded_mainloop_start(loop) >= 0)
1616 pa_context *context;
1618 pa_threaded_mainloop_lock(loop);
1619 context = connect_context(loop, AL_TRUE);
1620 if(context)
1622 ret = ALC_TRUE;
1624 /* Some libraries (Phonon, Qt) set some pulseaudio properties
1625 * through environment variables, which causes all streams in
1626 * the process to inherit them. This attempts to filter those
1627 * properties out by setting them to 0-length data. */
1628 prop_filter = pa_proplist_new();
1629 pa_proplist_set(prop_filter, PA_PROP_MEDIA_ROLE, NULL, 0);
1630 pa_proplist_set(prop_filter, "phonon.streamid", NULL, 0);
1632 pa_context_disconnect(context);
1633 pa_context_unref(context);
1635 pa_threaded_mainloop_unlock(loop);
1636 pa_threaded_mainloop_stop(loop);
1638 if(loop)
1639 pa_threaded_mainloop_free(loop);
1642 return ret;
1645 static void ALCpulseBackendFactory_deinit(ALCpulseBackendFactory* UNUSED(self))
1647 clear_devlist(&PlaybackDevices);
1648 VECTOR_DEINIT(PlaybackDevices);
1650 clear_devlist(&CaptureDevices);
1651 VECTOR_DEINIT(CaptureDevices);
1653 if(prop_filter)
1654 pa_proplist_free(prop_filter);
1655 prop_filter = NULL;
1657 /* PulseAudio doesn't like being CloseLib'd sometimes */
1660 static ALCboolean ALCpulseBackendFactory_querySupport(ALCpulseBackendFactory* UNUSED(self), ALCbackend_Type type)
1662 if(type == ALCbackend_Playback || type == ALCbackend_Capture)
1663 return ALC_TRUE;
1664 return ALC_FALSE;
1667 static void ALCpulseBackendFactory_probe(ALCpulseBackendFactory* UNUSED(self), enum DevProbe type)
1669 switch(type)
1671 case ALL_DEVICE_PROBE:
1672 ALCpulsePlayback_probeDevices();
1673 #define APPEND_ALL_DEVICES_LIST(e) AppendAllDevicesList(al_string_get_cstr((e)->name))
1674 VECTOR_FOR_EACH(const DevMap, PlaybackDevices, APPEND_ALL_DEVICES_LIST);
1675 #undef APPEND_ALL_DEVICES_LIST
1676 break;
1678 case CAPTURE_DEVICE_PROBE:
1679 ALCpulseCapture_probeDevices();
1680 #define APPEND_CAPTURE_DEVICE_LIST(e) AppendCaptureDeviceList(al_string_get_cstr((e)->name))
1681 VECTOR_FOR_EACH(const DevMap, CaptureDevices, APPEND_CAPTURE_DEVICE_LIST);
1682 #undef APPEND_CAPTURE_DEVICE_LIST
1683 break;
1687 static ALCbackend* ALCpulseBackendFactory_createBackend(ALCpulseBackendFactory* UNUSED(self), ALCdevice *device, ALCbackend_Type type)
1689 if(type == ALCbackend_Playback)
1691 ALCpulsePlayback *backend;
1693 backend = ALCpulsePlayback_New(sizeof(*backend));
1694 if(!backend) return NULL;
1695 memset(backend, 0, sizeof(*backend));
1697 ALCpulsePlayback_Construct(backend, device);
1699 return STATIC_CAST(ALCbackend, backend);
1701 if(type == ALCbackend_Capture)
1703 ALCpulseCapture *backend;
1705 backend = ALCpulseCapture_New(sizeof(*backend));
1706 if(!backend) return NULL;
1707 memset(backend, 0, sizeof(*backend));
1709 ALCpulseCapture_Construct(backend, device);
1711 return STATIC_CAST(ALCbackend, backend);
1714 return NULL;
1718 #else /* PA_API_VERSION == 12 */
1720 #warning "Unsupported API version, backend will be unavailable!"
1722 typedef struct ALCpulseBackendFactory {
1723 DERIVE_FROM_TYPE(ALCbackendFactory);
1724 } ALCpulseBackendFactory;
1725 #define ALCPULSEBACKENDFACTORY_INITIALIZER { { GET_VTABLE2(ALCpulseBackendFactory, ALCbackendFactory) } }
1727 static ALCboolean ALCpulseBackendFactory_init(ALCpulseBackendFactory* UNUSED(self))
1729 return ALC_FALSE;
1732 static void ALCpulseBackendFactory_deinit(ALCpulseBackendFactory* UNUSED(self))
1736 static ALCboolean ALCpulseBackendFactory_querySupport(ALCpulseBackendFactory* UNUSED(self), ALCbackend_Type UNUSED(type))
1738 return ALC_FALSE;
1741 static void ALCpulseBackendFactory_probe(ALCpulseBackendFactory* UNUSED(self), enum DevProbe UNUSED(type))
1745 static ALCbackend* ALCpulseBackendFactory_createBackend(ALCpulseBackendFactory* UNUSED(self), ALCdevice* UNUSED(device), ALCbackend_Type UNUSED(type))
1747 return NULL;
1750 DEFINE_ALCBACKENDFACTORY_VTABLE(ALCpulseBackendFactory);
1752 #endif /* PA_API_VERSION == 12 */
1754 ALCbackendFactory *ALCpulseBackendFactory_getFactory(void)
1756 static ALCpulseBackendFactory factory = ALCPULSEBACKENDFACTORY_INITIALIZER;
1757 return STATIC_CAST(ALCbackendFactory, &factory);