Use a macro for the record thread name
[openal-soft.git] / Alc / backends / pulseaudio.c
blobae7e4e8509d2bbbda2cfd9c1a57949869e84bdb4
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 althrd_join(self->thread, &res);
1058 pa_threaded_mainloop_lock(self->loop);
1060 o = pa_stream_cork(self->stream, 1, stream_success_callback, self->loop);
1061 wait_for_operation(o, self->loop);
1063 pa_threaded_mainloop_unlock(self->loop);
1067 static ALint64 ALCpulsePlayback_getLatency(ALCpulsePlayback *self)
1069 pa_usec_t latency = 0;
1070 int neg, err;
1072 if((err=pa_stream_get_latency(self->stream, &latency, &neg)) != 0)
1074 /* FIXME: if err = -PA_ERR_NODATA, it means we were called too soon
1075 * after starting the stream and no timing info has been received from
1076 * the server yet. Should we wait, possibly stalling the app, or give a
1077 * dummy value? Either way, it shouldn't be 0. */
1078 if(err != -PA_ERR_NODATA)
1079 ERR("Failed to get stream latency: 0x%x\n", err);
1080 return 0;
1083 if(neg) latency = 0;
1084 return (ALint64)minu64(latency, U64(0x7fffffffffffffff)/1000) * 1000;
1088 static void ALCpulsePlayback_lock(ALCpulsePlayback *self)
1090 pa_threaded_mainloop_lock(self->loop);
1093 static void ALCpulsePlayback_unlock(ALCpulsePlayback *self)
1095 pa_threaded_mainloop_unlock(self->loop);
1099 typedef struct ALCpulseCapture {
1100 DERIVE_FROM_TYPE(ALCbackend);
1102 al_string device_name;
1104 const void *cap_store;
1105 size_t cap_len;
1106 size_t cap_remain;
1108 ALCuint last_readable;
1110 pa_buffer_attr attr;
1111 pa_sample_spec spec;
1113 pa_threaded_mainloop *loop;
1115 pa_stream *stream;
1116 pa_context *context;
1117 } ALCpulseCapture;
1119 static void ALCpulseCapture_deviceCallback(pa_context *context, const pa_source_info *info, int eol, void *pdata);
1120 static void ALCpulseCapture_probeDevices(void);
1122 static void ALCpulseCapture_contextStateCallback(pa_context *context, void *pdata);
1123 static void ALCpulseCapture_streamStateCallback(pa_stream *stream, void *pdata);
1124 static void ALCpulseCapture_sourceNameCallback(pa_context *context, const pa_source_info *info, int eol, void *pdata);
1125 static void ALCpulseCapture_streamMovedCallback(pa_stream *stream, void *pdata);
1126 static pa_stream *ALCpulseCapture_connectStream(const char *device_name,
1127 pa_threaded_mainloop *loop, pa_context *context,
1128 pa_stream_flags_t flags, pa_buffer_attr *attr,
1129 pa_sample_spec *spec, pa_channel_map *chanmap);
1131 static void ALCpulseCapture_Construct(ALCpulseCapture *self, ALCdevice *device);
1132 static void ALCpulseCapture_Destruct(ALCpulseCapture *self);
1133 static ALCenum ALCpulseCapture_open(ALCpulseCapture *self, const ALCchar *name);
1134 static void ALCpulseCapture_close(ALCpulseCapture *self);
1135 static DECLARE_FORWARD(ALCpulseCapture, ALCbackend, ALCboolean, reset)
1136 static ALCboolean ALCpulseCapture_start(ALCpulseCapture *self);
1137 static void ALCpulseCapture_stop(ALCpulseCapture *self);
1138 static ALCenum ALCpulseCapture_captureSamples(ALCpulseCapture *self, ALCvoid *buffer, ALCuint samples);
1139 static ALCuint ALCpulseCapture_availableSamples(ALCpulseCapture *self);
1140 static ALint64 ALCpulseCapture_getLatency(ALCpulseCapture *self);
1141 static void ALCpulseCapture_lock(ALCpulseCapture *self);
1142 static void ALCpulseCapture_unlock(ALCpulseCapture *self);
1143 DECLARE_DEFAULT_ALLOCATORS(ALCpulseCapture)
1145 DEFINE_ALCBACKEND_VTABLE(ALCpulseCapture);
1148 static void ALCpulseCapture_Construct(ALCpulseCapture *self, ALCdevice *device)
1150 ALCbackend_Construct(STATIC_CAST(ALCbackend, self), device);
1151 SET_VTABLE2(ALCpulseCapture, ALCbackend, self);
1153 AL_STRING_INIT(self->device_name);
1156 static void ALCpulseCapture_Destruct(ALCpulseCapture *self)
1158 AL_STRING_DEINIT(self->device_name);
1159 ALCbackend_Destruct(STATIC_CAST(ALCbackend, self));
1163 static void ALCpulseCapture_deviceCallback(pa_context *UNUSED(context), const pa_source_info *info, int eol, void *pdata)
1165 pa_threaded_mainloop *loop = pdata;
1166 const DevMap *iter;
1167 DevMap entry;
1169 if(eol)
1171 pa_threaded_mainloop_signal(loop, 0);
1172 return;
1175 #define MATCH_INFO_NAME(iter) (al_string_cmp_cstr((iter)->device_name, info->name) == 0)
1176 VECTOR_FIND_IF(iter, const DevMap, CaptureDevices, MATCH_INFO_NAME);
1177 #undef MATCH_INFO_NAME
1178 if(iter != VECTOR_ITER_END(CaptureDevices))
1179 return;
1181 TRACE("Got device \"%s\", \"%s\"\n", info->description, info->name);
1183 AL_STRING_INIT(entry.name);
1184 AL_STRING_INIT(entry.device_name);
1186 al_string_copy_cstr(&entry.name, info->description);
1187 al_string_copy_cstr(&entry.device_name, info->name);
1189 VECTOR_PUSH_BACK(CaptureDevices, entry);
1192 static void ALCpulseCapture_probeDevices(void)
1194 pa_threaded_mainloop *loop;
1196 clear_devlist(&CaptureDevices);
1198 if((loop=pa_threaded_mainloop_new()) &&
1199 pa_threaded_mainloop_start(loop) >= 0)
1201 pa_context *context;
1203 pa_threaded_mainloop_lock(loop);
1204 context = connect_context(loop, AL_FALSE);
1205 if(context)
1207 pa_operation *o;
1208 pa_stream_flags_t flags;
1209 pa_sample_spec spec;
1210 pa_stream *stream;
1212 flags = PA_STREAM_FIX_FORMAT | PA_STREAM_FIX_RATE |
1213 PA_STREAM_FIX_CHANNELS | PA_STREAM_DONT_MOVE;
1215 spec.format = PA_SAMPLE_S16NE;
1216 spec.rate = 44100;
1217 spec.channels = 1;
1219 stream = ALCpulseCapture_connectStream(NULL, loop, context, flags,
1220 NULL, &spec, NULL);
1221 if(stream)
1223 o = pa_context_get_source_info_by_name(context, pa_stream_get_device_name(stream),
1224 ALCpulseCapture_deviceCallback, loop);
1225 wait_for_operation(o, loop);
1227 pa_stream_disconnect(stream);
1228 pa_stream_unref(stream);
1229 stream = NULL;
1232 o = pa_context_get_source_info_list(context, ALCpulseCapture_deviceCallback, loop);
1233 wait_for_operation(o, loop);
1235 pa_context_disconnect(context);
1236 pa_context_unref(context);
1238 pa_threaded_mainloop_unlock(loop);
1239 pa_threaded_mainloop_stop(loop);
1241 if(loop)
1242 pa_threaded_mainloop_free(loop);
1246 static void ALCpulseCapture_contextStateCallback(pa_context *context, void *pdata)
1248 ALCpulseCapture *self = pdata;
1249 if(pa_context_get_state(context) == PA_CONTEXT_FAILED)
1251 ERR("Received context failure!\n");
1252 aluHandleDisconnect(STATIC_CAST(ALCbackend,self)->mDevice);
1254 pa_threaded_mainloop_signal(self->loop, 0);
1257 static void ALCpulseCapture_streamStateCallback(pa_stream *stream, void *pdata)
1259 ALCpulseCapture *self = pdata;
1260 if(pa_stream_get_state(stream) == PA_STREAM_FAILED)
1262 ERR("Received stream failure!\n");
1263 aluHandleDisconnect(STATIC_CAST(ALCbackend,self)->mDevice);
1265 pa_threaded_mainloop_signal(self->loop, 0);
1269 static void ALCpulseCapture_sourceNameCallback(pa_context *UNUSED(context), const pa_source_info *info, int eol, void *pdata)
1271 ALCpulseCapture *self = pdata;
1272 ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
1274 if(eol)
1276 pa_threaded_mainloop_signal(self->loop, 0);
1277 return;
1280 al_string_copy_cstr(&device->DeviceName, info->description);
1284 static void ALCpulseCapture_streamMovedCallback(pa_stream *stream, void *pdata)
1286 ALCpulseCapture *self = pdata;
1288 al_string_copy_cstr(&self->device_name, pa_stream_get_device_name(stream));
1290 TRACE("Stream moved to %s\n", al_string_get_cstr(self->device_name));
1294 static pa_stream *ALCpulseCapture_connectStream(const char *device_name,
1295 pa_threaded_mainloop *loop, pa_context *context,
1296 pa_stream_flags_t flags, pa_buffer_attr *attr, pa_sample_spec *spec,
1297 pa_channel_map *chanmap)
1299 pa_stream_state_t state;
1300 pa_stream *stream;
1302 stream = pa_stream_new_with_proplist(context, "Capture Stream", spec, chanmap, prop_filter);
1303 if(!stream)
1305 ERR("pa_stream_new_with_proplist() failed: %s\n", pa_strerror(pa_context_errno(context)));
1306 return NULL;
1309 pa_stream_set_state_callback(stream, stream_state_callback, loop);
1311 if(pa_stream_connect_record(stream, device_name, attr, flags) < 0)
1313 ERR("Stream did not connect: %s\n", pa_strerror(pa_context_errno(context)));
1314 pa_stream_unref(stream);
1315 return NULL;
1318 while((state=pa_stream_get_state(stream)) != PA_STREAM_READY)
1320 if(!PA_STREAM_IS_GOOD(state))
1322 ERR("Stream did not get ready: %s\n", pa_strerror(pa_context_errno(context)));
1323 pa_stream_unref(stream);
1324 return NULL;
1327 pa_threaded_mainloop_wait(loop);
1329 pa_stream_set_state_callback(stream, NULL, NULL);
1331 return stream;
1335 static ALCenum ALCpulseCapture_open(ALCpulseCapture *self, const ALCchar *name)
1337 ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
1338 const char *pulse_name = NULL;
1339 pa_stream_flags_t flags = 0;
1340 pa_channel_map chanmap;
1341 pa_operation *o;
1342 ALuint samples;
1344 if(name)
1346 const DevMap *iter;
1348 if(VECTOR_SIZE(CaptureDevices) == 0)
1349 ALCpulseCapture_probeDevices();
1351 #define MATCH_NAME(iter) (al_string_cmp_cstr((iter)->name, name) == 0)
1352 VECTOR_FIND_IF(iter, const DevMap, CaptureDevices, MATCH_NAME);
1353 #undef MATCH_NAME
1354 if(iter == VECTOR_ITER_END(CaptureDevices))
1355 return ALC_INVALID_VALUE;
1356 pulse_name = al_string_get_cstr(iter->device_name);
1359 if(!pulse_open(&self->loop, &self->context, ALCpulseCapture_contextStateCallback, self))
1360 return ALC_INVALID_VALUE;
1362 pa_threaded_mainloop_lock(self->loop);
1364 self->spec.rate = device->Frequency;
1365 self->spec.channels = ChannelsFromDevFmt(device->FmtChans);
1367 switch(device->FmtType)
1369 case DevFmtUByte:
1370 self->spec.format = PA_SAMPLE_U8;
1371 break;
1372 case DevFmtShort:
1373 self->spec.format = PA_SAMPLE_S16NE;
1374 break;
1375 case DevFmtInt:
1376 self->spec.format = PA_SAMPLE_S32NE;
1377 break;
1378 case DevFmtFloat:
1379 self->spec.format = PA_SAMPLE_FLOAT32NE;
1380 break;
1381 case DevFmtByte:
1382 case DevFmtUShort:
1383 case DevFmtUInt:
1384 ERR("%s capture samples not supported\n", DevFmtTypeString(device->FmtType));
1385 pa_threaded_mainloop_unlock(self->loop);
1386 goto fail;
1389 if(pa_sample_spec_valid(&self->spec) == 0)
1391 ERR("Invalid sample format\n");
1392 pa_threaded_mainloop_unlock(self->loop);
1393 goto fail;
1396 if(!pa_channel_map_init_auto(&chanmap, self->spec.channels, PA_CHANNEL_MAP_WAVEEX))
1398 ERR("Couldn't build map for channel count (%d)!\n", self->spec.channels);
1399 pa_threaded_mainloop_unlock(self->loop);
1400 goto fail;
1403 samples = device->UpdateSize * device->NumUpdates;
1404 samples = maxu(samples, 100 * device->Frequency / 1000);
1406 self->attr.minreq = -1;
1407 self->attr.prebuf = -1;
1408 self->attr.maxlength = samples * pa_frame_size(&self->spec);
1409 self->attr.tlength = -1;
1410 self->attr.fragsize = minu(samples, 50*device->Frequency/1000) *
1411 pa_frame_size(&self->spec);
1413 flags |= PA_STREAM_START_CORKED|PA_STREAM_ADJUST_LATENCY;
1414 if(!GetConfigValueBool("pulse", "allow-moves", 0))
1415 flags |= PA_STREAM_DONT_MOVE;
1417 TRACE("Connecting to \"%s\"\n", pulse_name ? pulse_name : "(default)");
1418 self->stream = ALCpulseCapture_connectStream(pulse_name, self->loop, self->context,
1419 flags, &self->attr, &self->spec,
1420 &chanmap);
1421 if(!self->stream)
1423 pa_threaded_mainloop_unlock(self->loop);
1424 goto fail;
1426 pa_stream_set_moved_callback(self->stream, ALCpulseCapture_streamMovedCallback, self);
1427 pa_stream_set_state_callback(self->stream, ALCpulseCapture_streamStateCallback, self);
1429 al_string_copy_cstr(&self->device_name, pa_stream_get_device_name(self->stream));
1430 o = pa_context_get_source_info_by_name(self->context,
1431 al_string_get_cstr(self->device_name),
1432 ALCpulseCapture_sourceNameCallback, self);
1433 wait_for_operation(o, self->loop);
1435 pa_threaded_mainloop_unlock(self->loop);
1436 return ALC_NO_ERROR;
1438 fail:
1439 pulse_close(self->loop, self->context, self->stream);
1440 self->loop = NULL;
1441 self->context = NULL;
1442 self->stream = NULL;
1444 return ALC_INVALID_VALUE;
1447 static void ALCpulseCapture_close(ALCpulseCapture *self)
1449 pulse_close(self->loop, self->context, self->stream);
1450 self->loop = NULL;
1451 self->context = NULL;
1452 self->stream = NULL;
1454 al_string_clear(&self->device_name);
1457 static ALCboolean ALCpulseCapture_start(ALCpulseCapture *self)
1459 pa_operation *o;
1460 o = pa_stream_cork(self->stream, 0, stream_success_callback, self->loop);
1461 wait_for_operation(o, self->loop);
1463 return ALC_TRUE;
1466 static void ALCpulseCapture_stop(ALCpulseCapture *self)
1468 pa_operation *o;
1469 o = pa_stream_cork(self->stream, 1, stream_success_callback, self->loop);
1470 wait_for_operation(o, self->loop);
1473 static ALCenum ALCpulseCapture_captureSamples(ALCpulseCapture *self, ALCvoid *buffer, ALCuint samples)
1475 ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
1476 ALCuint todo = samples * pa_frame_size(&self->spec);
1478 /* Capture is done in fragment-sized chunks, so we loop until we get all
1479 * that's available */
1480 self->last_readable -= todo;
1481 while(todo > 0)
1483 size_t rem = todo;
1485 if(self->cap_len == 0)
1487 pa_stream_state_t state;
1489 state = pa_stream_get_state(self->stream);
1490 if(!PA_STREAM_IS_GOOD(state))
1492 aluHandleDisconnect(device);
1493 break;
1495 if(pa_stream_peek(self->stream, &self->cap_store, &self->cap_len) < 0)
1497 ERR("pa_stream_peek() failed: %s\n",
1498 pa_strerror(pa_context_errno(self->context)));
1499 aluHandleDisconnect(device);
1500 break;
1502 self->cap_remain = self->cap_len;
1504 if(rem > self->cap_remain)
1505 rem = self->cap_remain;
1507 memcpy(buffer, self->cap_store, rem);
1509 buffer = (ALbyte*)buffer + rem;
1510 todo -= rem;
1512 self->cap_store = (ALbyte*)self->cap_store + rem;
1513 self->cap_remain -= rem;
1514 if(self->cap_remain == 0)
1516 pa_stream_drop(self->stream);
1517 self->cap_len = 0;
1520 if(todo > 0)
1521 memset(buffer, ((device->FmtType==DevFmtUByte) ? 0x80 : 0), todo);
1523 return ALC_NO_ERROR;
1526 static ALCuint ALCpulseCapture_availableSamples(ALCpulseCapture *self)
1528 ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
1529 size_t readable = self->cap_remain;
1531 if(device->Connected)
1533 ssize_t got = pa_stream_readable_size(self->stream);
1534 if(got < 0)
1536 ERR("pa_stream_readable_size() failed: %s\n", pa_strerror(got));
1537 aluHandleDisconnect(device);
1539 else if((size_t)got > self->cap_len)
1540 readable += got - self->cap_len;
1543 if(self->last_readable < readable)
1544 self->last_readable = readable;
1545 return self->last_readable / pa_frame_size(&self->spec);
1549 static ALint64 ALCpulseCapture_getLatency(ALCpulseCapture *self)
1551 pa_usec_t latency = 0;
1552 int neg;
1554 if(pa_stream_get_latency(self->stream, &latency, &neg) != 0)
1556 ERR("Failed to get stream latency!\n");
1557 return 0;
1560 if(neg) latency = 0;
1561 return (ALint64)minu64(latency, U64(0x7fffffffffffffff)/1000) * 1000;
1565 static void ALCpulseCapture_lock(ALCpulseCapture *self)
1567 pa_threaded_mainloop_lock(self->loop);
1570 static void ALCpulseCapture_unlock(ALCpulseCapture *self)
1572 pa_threaded_mainloop_unlock(self->loop);
1576 typedef struct ALCpulseBackendFactory {
1577 DERIVE_FROM_TYPE(ALCbackendFactory);
1578 } ALCpulseBackendFactory;
1579 #define ALCPULSEBACKENDFACTORY_INITIALIZER { { GET_VTABLE2(ALCpulseBackendFactory, ALCbackendFactory) } }
1581 static ALCboolean ALCpulseBackendFactory_init(ALCpulseBackendFactory *self);
1582 static void ALCpulseBackendFactory_deinit(ALCpulseBackendFactory *self);
1583 static ALCboolean ALCpulseBackendFactory_querySupport(ALCpulseBackendFactory *self, ALCbackend_Type type);
1584 static void ALCpulseBackendFactory_probe(ALCpulseBackendFactory *self, enum DevProbe type);
1585 static ALCbackend* ALCpulseBackendFactory_createBackend(ALCpulseBackendFactory *self, ALCdevice *device, ALCbackend_Type type);
1587 DEFINE_ALCBACKENDFACTORY_VTABLE(ALCpulseBackendFactory);
1590 static ALCboolean ALCpulseBackendFactory_init(ALCpulseBackendFactory* UNUSED(self))
1592 ALCboolean ret = ALC_FALSE;
1594 VECTOR_INIT(PlaybackDevices);
1595 VECTOR_INIT(CaptureDevices);
1597 if(pulse_load())
1599 pa_threaded_mainloop *loop;
1601 pulse_ctx_flags = 0;
1602 if(!GetConfigValueBool("pulse", "spawn-server", 1))
1603 pulse_ctx_flags |= PA_CONTEXT_NOAUTOSPAWN;
1605 if((loop=pa_threaded_mainloop_new()) &&
1606 pa_threaded_mainloop_start(loop) >= 0)
1608 pa_context *context;
1610 pa_threaded_mainloop_lock(loop);
1611 context = connect_context(loop, AL_TRUE);
1612 if(context)
1614 ret = ALC_TRUE;
1616 /* Some libraries (Phonon, Qt) set some pulseaudio properties
1617 * through environment variables, which causes all streams in
1618 * the process to inherit them. This attempts to filter those
1619 * properties out by setting them to 0-length data. */
1620 prop_filter = pa_proplist_new();
1621 pa_proplist_set(prop_filter, PA_PROP_MEDIA_ROLE, NULL, 0);
1622 pa_proplist_set(prop_filter, "phonon.streamid", NULL, 0);
1624 pa_context_disconnect(context);
1625 pa_context_unref(context);
1627 pa_threaded_mainloop_unlock(loop);
1628 pa_threaded_mainloop_stop(loop);
1630 if(loop)
1631 pa_threaded_mainloop_free(loop);
1634 return ret;
1637 static void ALCpulseBackendFactory_deinit(ALCpulseBackendFactory* UNUSED(self))
1639 clear_devlist(&PlaybackDevices);
1640 VECTOR_DEINIT(PlaybackDevices);
1642 clear_devlist(&CaptureDevices);
1643 VECTOR_DEINIT(CaptureDevices);
1645 if(prop_filter)
1646 pa_proplist_free(prop_filter);
1647 prop_filter = NULL;
1649 /* PulseAudio doesn't like being CloseLib'd sometimes */
1652 static ALCboolean ALCpulseBackendFactory_querySupport(ALCpulseBackendFactory* UNUSED(self), ALCbackend_Type type)
1654 if(type == ALCbackend_Playback || type == ALCbackend_Capture)
1655 return ALC_TRUE;
1656 return ALC_FALSE;
1659 static void ALCpulseBackendFactory_probe(ALCpulseBackendFactory* UNUSED(self), enum DevProbe type)
1661 switch(type)
1663 case ALL_DEVICE_PROBE:
1664 ALCpulsePlayback_probeDevices();
1665 #define APPEND_ALL_DEVICES_LIST(e) AppendAllDevicesList(al_string_get_cstr((e)->name))
1666 VECTOR_FOR_EACH(const DevMap, PlaybackDevices, APPEND_ALL_DEVICES_LIST);
1667 #undef APPEND_ALL_DEVICES_LIST
1668 break;
1670 case CAPTURE_DEVICE_PROBE:
1671 ALCpulseCapture_probeDevices();
1672 #define APPEND_CAPTURE_DEVICE_LIST(e) AppendCaptureDeviceList(al_string_get_cstr((e)->name))
1673 VECTOR_FOR_EACH(const DevMap, CaptureDevices, APPEND_CAPTURE_DEVICE_LIST);
1674 #undef APPEND_CAPTURE_DEVICE_LIST
1675 break;
1679 static ALCbackend* ALCpulseBackendFactory_createBackend(ALCpulseBackendFactory* UNUSED(self), ALCdevice *device, ALCbackend_Type type)
1681 if(type == ALCbackend_Playback)
1683 ALCpulsePlayback *backend;
1685 backend = ALCpulsePlayback_New(sizeof(*backend));
1686 if(!backend) return NULL;
1687 memset(backend, 0, sizeof(*backend));
1689 ALCpulsePlayback_Construct(backend, device);
1691 return STATIC_CAST(ALCbackend, backend);
1693 if(type == ALCbackend_Capture)
1695 ALCpulseCapture *backend;
1697 backend = ALCpulseCapture_New(sizeof(*backend));
1698 if(!backend) return NULL;
1699 memset(backend, 0, sizeof(*backend));
1701 ALCpulseCapture_Construct(backend, device);
1703 return STATIC_CAST(ALCbackend, backend);
1706 return NULL;
1710 #else /* PA_API_VERSION == 12 */
1712 #warning "Unsupported API version, backend will be unavailable!"
1714 typedef struct ALCpulseBackendFactory {
1715 DERIVE_FROM_TYPE(ALCbackendFactory);
1716 } ALCpulseBackendFactory;
1717 #define ALCPULSEBACKENDFACTORY_INITIALIZER { { GET_VTABLE2(ALCpulseBackendFactory, ALCbackendFactory) } }
1719 static ALCboolean ALCpulseBackendFactory_init(ALCpulseBackendFactory* UNUSED(self))
1721 return ALC_FALSE;
1724 static void ALCpulseBackendFactory_deinit(ALCpulseBackendFactory* UNUSED(self))
1728 static ALCboolean ALCpulseBackendFactory_querySupport(ALCpulseBackendFactory* UNUSED(self), ALCbackend_Type UNUSED(type))
1730 return ALC_FALSE;
1733 static void ALCpulseBackendFactory_probe(ALCpulseBackendFactory* UNUSED(self), enum DevProbe UNUSED(type))
1737 static ALCbackend* ALCpulseBackendFactory_createBackend(ALCpulseBackendFactory* UNUSED(self), ALCdevice* UNUSED(device), ALCbackend_Type UNUSED(type))
1739 return NULL;
1742 DEFINE_ALCBACKENDFACTORY_VTABLE(ALCpulseBackendFactory);
1744 #endif /* PA_API_VERSION == 12 */
1746 ALCbackendFactory *ALCpulseBackendFactory_getFactory(void)
1748 static ALCpulseBackendFactory factory = ALCPULSEBACKENDFACTORY_INITIALIZER;
1749 return STATIC_CAST(ALCbackendFactory, &factory);