I guess -1 isn't allowed for the output
[openal-soft.git] / Alc / backends / pulseaudio.c
blob8c5469b27eed5415c7c00e6fe393dffeefce3cf4
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 al_string missing_funcs = AL_STRING_INIT_STATIC();
187 #ifdef _WIN32
188 #define PALIB "libpulse-0.dll"
189 #elif defined(__APPLE__) && defined(__MACH__)
190 #define PALIB "libpulse.0.dylib"
191 #else
192 #define PALIB "libpulse.so.0"
193 #endif
194 pa_handle = LoadLib(PALIB);
195 if(!pa_handle)
197 WARN("Failed to load %s\n", PALIB);
198 return ALC_FALSE;
201 #define LOAD_FUNC(x) do { \
202 p##x = GetSymbol(pa_handle, #x); \
203 if(!(p##x)) { \
204 ret = ALC_FALSE; \
205 alstr_append_cstr(&missing_funcs, "\n" #x); \
207 } while(0)
208 LOAD_FUNC(pa_context_unref);
209 LOAD_FUNC(pa_sample_spec_valid);
210 LOAD_FUNC(pa_stream_drop);
211 LOAD_FUNC(pa_frame_size);
212 LOAD_FUNC(pa_strerror);
213 LOAD_FUNC(pa_context_get_state);
214 LOAD_FUNC(pa_stream_get_state);
215 LOAD_FUNC(pa_threaded_mainloop_signal);
216 LOAD_FUNC(pa_stream_peek);
217 LOAD_FUNC(pa_threaded_mainloop_wait);
218 LOAD_FUNC(pa_threaded_mainloop_unlock);
219 LOAD_FUNC(pa_threaded_mainloop_in_thread);
220 LOAD_FUNC(pa_context_new);
221 LOAD_FUNC(pa_threaded_mainloop_stop);
222 LOAD_FUNC(pa_context_disconnect);
223 LOAD_FUNC(pa_threaded_mainloop_start);
224 LOAD_FUNC(pa_threaded_mainloop_get_api);
225 LOAD_FUNC(pa_context_set_state_callback);
226 LOAD_FUNC(pa_stream_write);
227 LOAD_FUNC(pa_xfree);
228 LOAD_FUNC(pa_stream_connect_record);
229 LOAD_FUNC(pa_stream_connect_playback);
230 LOAD_FUNC(pa_stream_readable_size);
231 LOAD_FUNC(pa_stream_writable_size);
232 LOAD_FUNC(pa_stream_is_corked);
233 LOAD_FUNC(pa_stream_cork);
234 LOAD_FUNC(pa_stream_is_suspended);
235 LOAD_FUNC(pa_stream_get_device_name);
236 LOAD_FUNC(pa_stream_get_latency);
237 LOAD_FUNC(pa_path_get_filename);
238 LOAD_FUNC(pa_get_binary_name);
239 LOAD_FUNC(pa_threaded_mainloop_free);
240 LOAD_FUNC(pa_context_errno);
241 LOAD_FUNC(pa_xmalloc);
242 LOAD_FUNC(pa_stream_unref);
243 LOAD_FUNC(pa_threaded_mainloop_accept);
244 LOAD_FUNC(pa_stream_set_write_callback);
245 LOAD_FUNC(pa_threaded_mainloop_new);
246 LOAD_FUNC(pa_context_connect);
247 LOAD_FUNC(pa_stream_set_buffer_attr);
248 LOAD_FUNC(pa_stream_get_buffer_attr);
249 LOAD_FUNC(pa_stream_get_sample_spec);
250 LOAD_FUNC(pa_stream_get_time);
251 LOAD_FUNC(pa_stream_set_read_callback);
252 LOAD_FUNC(pa_stream_set_state_callback);
253 LOAD_FUNC(pa_stream_set_moved_callback);
254 LOAD_FUNC(pa_stream_set_underflow_callback);
255 LOAD_FUNC(pa_stream_new_with_proplist);
256 LOAD_FUNC(pa_stream_disconnect);
257 LOAD_FUNC(pa_threaded_mainloop_lock);
258 LOAD_FUNC(pa_channel_map_init_auto);
259 LOAD_FUNC(pa_channel_map_parse);
260 LOAD_FUNC(pa_channel_map_snprint);
261 LOAD_FUNC(pa_channel_map_equal);
262 LOAD_FUNC(pa_context_get_server_info);
263 LOAD_FUNC(pa_context_get_sink_info_by_name);
264 LOAD_FUNC(pa_context_get_sink_info_list);
265 LOAD_FUNC(pa_context_get_source_info_by_name);
266 LOAD_FUNC(pa_context_get_source_info_list);
267 LOAD_FUNC(pa_operation_get_state);
268 LOAD_FUNC(pa_operation_unref);
269 LOAD_FUNC(pa_proplist_new);
270 LOAD_FUNC(pa_proplist_free);
271 LOAD_FUNC(pa_proplist_set);
272 LOAD_FUNC(pa_channel_map_superset);
273 LOAD_FUNC(pa_stream_set_buffer_attr_callback);
274 LOAD_FUNC(pa_stream_begin_write);
275 #undef LOAD_FUNC
277 if(ret == ALC_FALSE)
279 WARN("Missing expected functions:%s\n", alstr_get_cstr(missing_funcs));
280 CloseLib(pa_handle);
281 pa_handle = NULL;
283 alstr_reset(&missing_funcs);
285 #endif /* HAVE_DYNLOAD */
286 return ret;
290 /* Global flags and properties */
291 static pa_context_flags_t pulse_ctx_flags;
292 static pa_proplist *prop_filter;
295 /* PulseAudio Event Callbacks */
296 static void context_state_callback(pa_context *context, void *pdata)
298 pa_threaded_mainloop *loop = pdata;
299 pa_context_state_t state;
301 state = pa_context_get_state(context);
302 if(state == PA_CONTEXT_READY || !PA_CONTEXT_IS_GOOD(state))
303 pa_threaded_mainloop_signal(loop, 0);
306 static void stream_state_callback(pa_stream *stream, void *pdata)
308 pa_threaded_mainloop *loop = pdata;
309 pa_stream_state_t state;
311 state = pa_stream_get_state(stream);
312 if(state == PA_STREAM_READY || !PA_STREAM_IS_GOOD(state))
313 pa_threaded_mainloop_signal(loop, 0);
316 static void stream_success_callback(pa_stream *UNUSED(stream), int UNUSED(success), void *pdata)
318 pa_threaded_mainloop *loop = pdata;
319 pa_threaded_mainloop_signal(loop, 0);
322 static void wait_for_operation(pa_operation *op, pa_threaded_mainloop *loop)
324 if(op)
326 while(pa_operation_get_state(op) == PA_OPERATION_RUNNING)
327 pa_threaded_mainloop_wait(loop);
328 pa_operation_unref(op);
333 static pa_context *connect_context(pa_threaded_mainloop *loop, ALboolean silent)
335 const char *name = "OpenAL Soft";
336 char path_name[PATH_MAX];
337 pa_context_state_t state;
338 pa_context *context;
339 int err;
341 if(pa_get_binary_name(path_name, sizeof(path_name)))
342 name = pa_path_get_filename(path_name);
344 context = pa_context_new(pa_threaded_mainloop_get_api(loop), name);
345 if(!context)
347 ERR("pa_context_new() failed\n");
348 return NULL;
351 pa_context_set_state_callback(context, context_state_callback, loop);
353 if((err=pa_context_connect(context, NULL, pulse_ctx_flags, NULL)) >= 0)
355 while((state=pa_context_get_state(context)) != PA_CONTEXT_READY)
357 if(!PA_CONTEXT_IS_GOOD(state))
359 err = pa_context_errno(context);
360 if(err > 0) err = -err;
361 break;
364 pa_threaded_mainloop_wait(loop);
367 pa_context_set_state_callback(context, NULL, NULL);
369 if(err < 0)
371 if(!silent)
372 ERR("Context did not connect: %s\n", pa_strerror(err));
373 pa_context_unref(context);
374 return NULL;
377 return context;
381 static ALCboolean pulse_open(pa_threaded_mainloop **loop, pa_context **context,
382 void(*state_cb)(pa_context*,void*), void *ptr)
384 if(!(*loop = pa_threaded_mainloop_new()))
386 ERR("pa_threaded_mainloop_new() failed!\n");
387 return ALC_FALSE;
389 if(pa_threaded_mainloop_start(*loop) < 0)
391 ERR("pa_threaded_mainloop_start() failed\n");
392 goto error;
395 pa_threaded_mainloop_lock(*loop);
397 *context = connect_context(*loop, AL_FALSE);
398 if(!*context)
400 pa_threaded_mainloop_unlock(*loop);
401 pa_threaded_mainloop_stop(*loop);
402 goto error;
404 pa_context_set_state_callback(*context, state_cb, ptr);
406 pa_threaded_mainloop_unlock(*loop);
407 return ALC_TRUE;
409 error:
410 pa_threaded_mainloop_free(*loop);
411 *loop = NULL;
413 return ALC_FALSE;
416 static void pulse_close(pa_threaded_mainloop *loop, pa_context *context, pa_stream *stream)
418 pa_threaded_mainloop_lock(loop);
420 if(stream)
422 pa_stream_set_state_callback(stream, NULL, NULL);
423 pa_stream_set_moved_callback(stream, NULL, NULL);
424 pa_stream_set_write_callback(stream, NULL, NULL);
425 pa_stream_set_buffer_attr_callback(stream, NULL, NULL);
426 pa_stream_disconnect(stream);
427 pa_stream_unref(stream);
430 pa_context_disconnect(context);
431 pa_context_unref(context);
433 pa_threaded_mainloop_unlock(loop);
435 pa_threaded_mainloop_stop(loop);
436 pa_threaded_mainloop_free(loop);
440 typedef struct {
441 al_string name;
442 al_string device_name;
443 } DevMap;
444 TYPEDEF_VECTOR(DevMap, vector_DevMap)
446 static vector_DevMap PlaybackDevices;
447 static vector_DevMap CaptureDevices;
449 static void clear_devlist(vector_DevMap *list)
451 #define DEINIT_STRS(i) (AL_STRING_DEINIT((i)->name),AL_STRING_DEINIT((i)->device_name))
452 VECTOR_FOR_EACH(DevMap, *list, DEINIT_STRS);
453 #undef DEINIT_STRS
454 VECTOR_RESIZE(*list, 0, 0);
458 typedef struct ALCpulsePlayback {
459 DERIVE_FROM_TYPE(ALCbackend);
461 al_string device_name;
463 pa_buffer_attr attr;
464 pa_sample_spec spec;
466 pa_threaded_mainloop *loop;
468 pa_stream *stream;
469 pa_context *context;
471 volatile ALboolean killNow;
472 althrd_t thread;
473 } ALCpulsePlayback;
475 static void ALCpulsePlayback_deviceCallback(pa_context *context, const pa_sink_info *info, int eol, void *pdata);
476 static void ALCpulsePlayback_probeDevices(void);
478 static void ALCpulsePlayback_bufferAttrCallback(pa_stream *stream, void *pdata);
479 static void ALCpulsePlayback_contextStateCallback(pa_context *context, void *pdata);
480 static void ALCpulsePlayback_streamStateCallback(pa_stream *stream, void *pdata);
481 static void ALCpulsePlayback_streamWriteCallback(pa_stream *p, size_t nbytes, void *userdata);
482 static void ALCpulsePlayback_sinkInfoCallback(pa_context *context, const pa_sink_info *info, int eol, void *pdata);
483 static void ALCpulsePlayback_sinkNameCallback(pa_context *context, const pa_sink_info *info, int eol, void *pdata);
484 static void ALCpulsePlayback_streamMovedCallback(pa_stream *stream, void *pdata);
485 static pa_stream *ALCpulsePlayback_connectStream(const char *device_name, pa_threaded_mainloop *loop,
486 pa_context *context, pa_stream_flags_t flags,
487 pa_buffer_attr *attr, pa_sample_spec *spec,
488 pa_channel_map *chanmap);
489 static int ALCpulsePlayback_mixerProc(void *ptr);
491 static void ALCpulsePlayback_Construct(ALCpulsePlayback *self, ALCdevice *device);
492 static void ALCpulsePlayback_Destruct(ALCpulsePlayback *self);
493 static ALCenum ALCpulsePlayback_open(ALCpulsePlayback *self, const ALCchar *name);
494 static void ALCpulsePlayback_close(ALCpulsePlayback *self);
495 static ALCboolean ALCpulsePlayback_reset(ALCpulsePlayback *self);
496 static ALCboolean ALCpulsePlayback_start(ALCpulsePlayback *self);
497 static void ALCpulsePlayback_stop(ALCpulsePlayback *self);
498 static DECLARE_FORWARD2(ALCpulsePlayback, ALCbackend, ALCenum, captureSamples, ALCvoid*, ALCuint)
499 static DECLARE_FORWARD(ALCpulsePlayback, ALCbackend, ALCuint, availableSamples)
500 static ClockLatency ALCpulsePlayback_getClockLatency(ALCpulsePlayback *self);
501 static void ALCpulsePlayback_lock(ALCpulsePlayback *self);
502 static void ALCpulsePlayback_unlock(ALCpulsePlayback *self);
503 DECLARE_DEFAULT_ALLOCATORS(ALCpulsePlayback)
505 DEFINE_ALCBACKEND_VTABLE(ALCpulsePlayback);
508 static void ALCpulsePlayback_Construct(ALCpulsePlayback *self, ALCdevice *device)
510 ALCbackend_Construct(STATIC_CAST(ALCbackend, self), device);
511 SET_VTABLE2(ALCpulsePlayback, ALCbackend, self);
513 AL_STRING_INIT(self->device_name);
516 static void ALCpulsePlayback_Destruct(ALCpulsePlayback *self)
518 AL_STRING_DEINIT(self->device_name);
519 ALCbackend_Destruct(STATIC_CAST(ALCbackend, self));
523 static void ALCpulsePlayback_deviceCallback(pa_context *UNUSED(context), const pa_sink_info *info, int eol, void *pdata)
525 pa_threaded_mainloop *loop = pdata;
526 const DevMap *iter;
527 DevMap entry;
528 int count;
530 if(eol)
532 pa_threaded_mainloop_signal(loop, 0);
533 return;
536 #define MATCH_INFO_NAME(iter) (alstr_cmp_cstr((iter)->device_name, info->name) == 0)
537 VECTOR_FIND_IF(iter, const DevMap, PlaybackDevices, MATCH_INFO_NAME);
538 if(iter != VECTOR_END(PlaybackDevices)) return;
539 #undef MATCH_INFO_NAME
541 AL_STRING_INIT(entry.name);
542 AL_STRING_INIT(entry.device_name);
544 alstr_copy_cstr(&entry.device_name, info->name);
546 count = 0;
547 while(1)
549 alstr_copy_cstr(&entry.name, info->description);
550 if(count != 0)
552 char str[64];
553 snprintf(str, sizeof(str), " #%d", count+1);
554 alstr_append_cstr(&entry.name, str);
557 #define MATCH_ENTRY(i) (alstr_cmp(entry.name, (i)->name) == 0)
558 VECTOR_FIND_IF(iter, const DevMap, PlaybackDevices, MATCH_ENTRY);
559 if(iter == VECTOR_END(PlaybackDevices)) break;
560 #undef MATCH_ENTRY
561 count++;
564 TRACE("Got device \"%s\", \"%s\"\n", alstr_get_cstr(entry.name), alstr_get_cstr(entry.device_name));
566 VECTOR_PUSH_BACK(PlaybackDevices, entry);
569 static void ALCpulsePlayback_probeDevices(void)
571 pa_threaded_mainloop *loop;
573 clear_devlist(&PlaybackDevices);
575 if((loop=pa_threaded_mainloop_new()) &&
576 pa_threaded_mainloop_start(loop) >= 0)
578 pa_context *context;
580 pa_threaded_mainloop_lock(loop);
581 context = connect_context(loop, AL_FALSE);
582 if(context)
584 pa_operation *o;
585 pa_stream_flags_t flags;
586 pa_sample_spec spec;
587 pa_stream *stream;
589 flags = PA_STREAM_FIX_FORMAT | PA_STREAM_FIX_RATE |
590 PA_STREAM_FIX_CHANNELS | PA_STREAM_DONT_MOVE;
592 spec.format = PA_SAMPLE_S16NE;
593 spec.rate = 44100;
594 spec.channels = 2;
596 stream = ALCpulsePlayback_connectStream(NULL, loop, context, flags,
597 NULL, &spec, NULL);
598 if(stream)
600 o = pa_context_get_sink_info_by_name(context, pa_stream_get_device_name(stream),
601 ALCpulsePlayback_deviceCallback, loop);
602 wait_for_operation(o, loop);
604 pa_stream_disconnect(stream);
605 pa_stream_unref(stream);
606 stream = NULL;
609 o = pa_context_get_sink_info_list(context, ALCpulsePlayback_deviceCallback, loop);
610 wait_for_operation(o, loop);
612 pa_context_disconnect(context);
613 pa_context_unref(context);
615 pa_threaded_mainloop_unlock(loop);
616 pa_threaded_mainloop_stop(loop);
618 if(loop)
619 pa_threaded_mainloop_free(loop);
623 static void ALCpulsePlayback_bufferAttrCallback(pa_stream *stream, void *pdata)
625 ALCpulsePlayback *self = pdata;
627 self->attr = *pa_stream_get_buffer_attr(stream);
628 TRACE("minreq=%d, tlength=%d, prebuf=%d\n", self->attr.minreq, self->attr.tlength, self->attr.prebuf);
629 /* FIXME: Update the device's UpdateSize (and/or NumUpdates) using the new
630 * buffer attributes? Changing UpdateSize will change the ALC_REFRESH
631 * property, which probably shouldn't change between device resets. But
632 * leaving it alone means ALC_REFRESH will be off.
636 static void ALCpulsePlayback_contextStateCallback(pa_context *context, void *pdata)
638 ALCpulsePlayback *self = pdata;
639 if(pa_context_get_state(context) == PA_CONTEXT_FAILED)
641 ERR("Received context failure!\n");
642 aluHandleDisconnect(STATIC_CAST(ALCbackend,self)->mDevice);
644 pa_threaded_mainloop_signal(self->loop, 0);
647 static void ALCpulsePlayback_streamStateCallback(pa_stream *stream, void *pdata)
649 ALCpulsePlayback *self = pdata;
650 if(pa_stream_get_state(stream) == PA_STREAM_FAILED)
652 ERR("Received stream failure!\n");
653 aluHandleDisconnect(STATIC_CAST(ALCbackend,self)->mDevice);
655 pa_threaded_mainloop_signal(self->loop, 0);
658 static void ALCpulsePlayback_streamWriteCallback(pa_stream* UNUSED(p), size_t UNUSED(nbytes), void *pdata)
660 ALCpulsePlayback *self = pdata;
661 pa_threaded_mainloop_signal(self->loop, 0);
664 static void ALCpulsePlayback_sinkInfoCallback(pa_context *UNUSED(context), const pa_sink_info *info, int eol, void *pdata)
666 static const struct {
667 enum DevFmtChannels chans;
668 pa_channel_map map;
669 } chanmaps[] = {
670 { DevFmtX71, { 8, {
671 PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT,
672 PA_CHANNEL_POSITION_FRONT_CENTER, PA_CHANNEL_POSITION_LFE,
673 PA_CHANNEL_POSITION_REAR_LEFT, PA_CHANNEL_POSITION_REAR_RIGHT,
674 PA_CHANNEL_POSITION_SIDE_LEFT, PA_CHANNEL_POSITION_SIDE_RIGHT
675 } } },
676 { DevFmtX61, { 7, {
677 PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT,
678 PA_CHANNEL_POSITION_FRONT_CENTER, PA_CHANNEL_POSITION_LFE,
679 PA_CHANNEL_POSITION_REAR_CENTER,
680 PA_CHANNEL_POSITION_SIDE_LEFT, PA_CHANNEL_POSITION_SIDE_RIGHT
681 } } },
682 { DevFmtX51, { 6, {
683 PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT,
684 PA_CHANNEL_POSITION_FRONT_CENTER, PA_CHANNEL_POSITION_LFE,
685 PA_CHANNEL_POSITION_SIDE_LEFT, PA_CHANNEL_POSITION_SIDE_RIGHT
686 } } },
687 { DevFmtX51Rear, { 6, {
688 PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT,
689 PA_CHANNEL_POSITION_FRONT_CENTER, PA_CHANNEL_POSITION_LFE,
690 PA_CHANNEL_POSITION_REAR_LEFT, PA_CHANNEL_POSITION_REAR_RIGHT
691 } } },
692 { DevFmtQuad, { 4, {
693 PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT,
694 PA_CHANNEL_POSITION_REAR_LEFT, PA_CHANNEL_POSITION_REAR_RIGHT
695 } } },
696 { DevFmtStereo, { 2, {
697 PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT
698 } } },
699 { DevFmtMono, { 1, {PA_CHANNEL_POSITION_MONO} } }
701 ALCpulsePlayback *self = pdata;
702 ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
703 size_t i;
705 if(eol)
707 pa_threaded_mainloop_signal(self->loop, 0);
708 return;
711 for(i = 0;i < COUNTOF(chanmaps);i++)
713 if(pa_channel_map_superset(&info->channel_map, &chanmaps[i].map))
715 if(!(device->Flags&DEVICE_CHANNELS_REQUEST))
716 device->FmtChans = chanmaps[i].chans;
717 break;
720 if(i == COUNTOF(chanmaps))
722 char chanmap_str[PA_CHANNEL_MAP_SNPRINT_MAX] = "";
723 pa_channel_map_snprint(chanmap_str, sizeof(chanmap_str), &info->channel_map);
724 WARN("Failed to find format for channel map:\n %s\n", chanmap_str);
727 if(info->active_port)
728 TRACE("Active port: %s (%s)\n", info->active_port->name, info->active_port->description);
729 device->IsHeadphones = (info->active_port &&
730 strcmp(info->active_port->name, "analog-output-headphones") == 0 &&
731 device->FmtChans == DevFmtStereo);
734 static void ALCpulsePlayback_sinkNameCallback(pa_context *UNUSED(context), const pa_sink_info *info, int eol, void *pdata)
736 ALCpulsePlayback *self = pdata;
737 ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
739 if(eol)
741 pa_threaded_mainloop_signal(self->loop, 0);
742 return;
745 alstr_copy_cstr(&device->DeviceName, info->description);
749 static void ALCpulsePlayback_streamMovedCallback(pa_stream *stream, void *pdata)
751 ALCpulsePlayback *self = pdata;
753 alstr_copy_cstr(&self->device_name, pa_stream_get_device_name(stream));
755 TRACE("Stream moved to %s\n", alstr_get_cstr(self->device_name));
759 static pa_stream *ALCpulsePlayback_connectStream(const char *device_name,
760 pa_threaded_mainloop *loop, pa_context *context,
761 pa_stream_flags_t flags, pa_buffer_attr *attr, pa_sample_spec *spec,
762 pa_channel_map *chanmap)
764 pa_stream_state_t state;
765 pa_stream *stream;
767 if(!device_name)
769 device_name = getenv("ALSOFT_PULSE_DEFAULT");
770 if(device_name && !device_name[0])
771 device_name = NULL;
774 stream = pa_stream_new_with_proplist(context, "Playback Stream", spec, chanmap, prop_filter);
775 if(!stream)
777 ERR("pa_stream_new_with_proplist() failed: %s\n", pa_strerror(pa_context_errno(context)));
778 return NULL;
781 pa_stream_set_state_callback(stream, stream_state_callback, loop);
783 if(pa_stream_connect_playback(stream, device_name, attr, flags, NULL, NULL) < 0)
785 ERR("Stream did not connect: %s\n", pa_strerror(pa_context_errno(context)));
786 pa_stream_unref(stream);
787 return NULL;
790 while((state=pa_stream_get_state(stream)) != PA_STREAM_READY)
792 if(!PA_STREAM_IS_GOOD(state))
794 ERR("Stream did not get ready: %s\n", pa_strerror(pa_context_errno(context)));
795 pa_stream_unref(stream);
796 return NULL;
799 pa_threaded_mainloop_wait(loop);
801 pa_stream_set_state_callback(stream, NULL, NULL);
803 return stream;
807 static int ALCpulsePlayback_mixerProc(void *ptr)
809 ALCpulsePlayback *self = ptr;
810 ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
811 ALuint buffer_size;
812 size_t frame_size;
813 ssize_t len;
815 SetRTPriority();
816 althrd_setname(althrd_current(), MIXER_THREAD_NAME);
818 pa_threaded_mainloop_lock(self->loop);
819 frame_size = pa_frame_size(&self->spec);
821 while(!self->killNow && device->Connected)
823 len = pa_stream_writable_size(self->stream);
824 if(len < 0)
826 ERR("Failed to get writable size: %ld", (long)len);
827 aluHandleDisconnect(device);
828 break;
831 /* Make sure we're going to write at least 2 'periods' (minreqs), in
832 * case the server increased it since starting playback. Also round up
833 * the number of writable periods if it's not an integer count.
835 buffer_size = maxu((self->attr.tlength + self->attr.minreq/2) / self->attr.minreq, 2) *
836 self->attr.minreq;
838 /* NOTE: This assumes pa_stream_writable_size returns between 0 and
839 * tlength, else there will be more latency than intended.
841 len = mini(len - (ssize_t)self->attr.tlength, 0) + buffer_size;
842 if(len < (int32_t)self->attr.minreq)
844 if(pa_stream_is_corked(self->stream))
846 pa_operation *o;
847 o = pa_stream_cork(self->stream, 0, NULL, NULL);
848 if(o) pa_operation_unref(o);
850 pa_threaded_mainloop_wait(self->loop);
851 continue;
853 len -= len%self->attr.minreq;
855 while(len > 0)
857 size_t newlen = len;
858 int ret;
859 void *buf;
860 pa_free_cb_t free_func = NULL;
862 if(pa_stream_begin_write(self->stream, &buf, &newlen) < 0)
864 buf = pa_xmalloc(newlen);
865 free_func = pa_xfree;
868 aluMixData(device, buf, newlen/frame_size);
870 ret = pa_stream_write(self->stream, buf, newlen, free_func, 0, PA_SEEK_RELATIVE);
871 if(ret != PA_OK)
873 ERR("Failed to write to stream: %d, %s\n", ret, pa_strerror(ret));
874 break;
876 len -= newlen;
879 pa_threaded_mainloop_unlock(self->loop);
881 return 0;
885 static ALCenum ALCpulsePlayback_open(ALCpulsePlayback *self, const ALCchar *name)
887 const_al_string dev_name = AL_STRING_INIT_STATIC();
888 const char *pulse_name = NULL;
889 pa_stream_flags_t flags;
890 pa_sample_spec spec;
892 if(name)
894 const DevMap *iter;
896 if(VECTOR_SIZE(PlaybackDevices) == 0)
897 ALCpulsePlayback_probeDevices();
899 #define MATCH_NAME(iter) (alstr_cmp_cstr((iter)->name, name) == 0)
900 VECTOR_FIND_IF(iter, const DevMap, PlaybackDevices, MATCH_NAME);
901 #undef MATCH_NAME
902 if(iter == VECTOR_END(PlaybackDevices))
903 return ALC_INVALID_VALUE;
904 pulse_name = alstr_get_cstr(iter->device_name);
905 dev_name = iter->name;
908 if(!pulse_open(&self->loop, &self->context, ALCpulsePlayback_contextStateCallback, self))
909 return ALC_INVALID_VALUE;
911 pa_threaded_mainloop_lock(self->loop);
913 flags = PA_STREAM_FIX_FORMAT | PA_STREAM_FIX_RATE |
914 PA_STREAM_FIX_CHANNELS;
915 if(!GetConfigValueBool(NULL, "pulse", "allow-moves", 0))
916 flags |= PA_STREAM_DONT_MOVE;
918 spec.format = PA_SAMPLE_S16NE;
919 spec.rate = 44100;
920 spec.channels = 2;
922 TRACE("Connecting to \"%s\"\n", pulse_name ? pulse_name : "(default)");
923 self->stream = ALCpulsePlayback_connectStream(pulse_name, self->loop, self->context,
924 flags, NULL, &spec, NULL);
925 if(!self->stream)
927 pa_threaded_mainloop_unlock(self->loop);
928 pulse_close(self->loop, self->context, self->stream);
929 self->loop = NULL;
930 self->context = NULL;
931 return ALC_INVALID_VALUE;
933 pa_stream_set_moved_callback(self->stream, ALCpulsePlayback_streamMovedCallback, self);
935 alstr_copy_cstr(&self->device_name, pa_stream_get_device_name(self->stream));
936 if(alstr_empty(dev_name))
938 pa_operation *o = pa_context_get_sink_info_by_name(
939 self->context, alstr_get_cstr(self->device_name),
940 ALCpulsePlayback_sinkNameCallback, self
942 wait_for_operation(o, self->loop);
944 else
946 ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
947 alstr_copy(&device->DeviceName, dev_name);
950 pa_threaded_mainloop_unlock(self->loop);
952 return ALC_NO_ERROR;
955 static void ALCpulsePlayback_close(ALCpulsePlayback *self)
957 pulse_close(self->loop, self->context, self->stream);
958 self->loop = NULL;
959 self->context = NULL;
960 self->stream = NULL;
962 alstr_clear(&self->device_name);
965 static ALCboolean ALCpulsePlayback_reset(ALCpulsePlayback *self)
967 ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
968 pa_stream_flags_t flags = 0;
969 const char *mapname = NULL;
970 pa_channel_map chanmap;
971 pa_operation *o;
973 pa_threaded_mainloop_lock(self->loop);
975 if(self->stream)
977 pa_stream_set_state_callback(self->stream, NULL, NULL);
978 pa_stream_set_moved_callback(self->stream, NULL, NULL);
979 pa_stream_set_write_callback(self->stream, NULL, NULL);
980 pa_stream_set_buffer_attr_callback(self->stream, NULL, NULL);
981 pa_stream_disconnect(self->stream);
982 pa_stream_unref(self->stream);
983 self->stream = NULL;
986 o = pa_context_get_sink_info_by_name(self->context, alstr_get_cstr(self->device_name),
987 ALCpulsePlayback_sinkInfoCallback, self);
988 wait_for_operation(o, self->loop);
990 if(GetConfigValueBool(alstr_get_cstr(device->DeviceName), "pulse", "fix-rate", 0) ||
991 !(device->Flags&DEVICE_FREQUENCY_REQUEST))
992 flags |= PA_STREAM_FIX_RATE;
993 flags |= PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE;
994 flags |= PA_STREAM_ADJUST_LATENCY;
995 flags |= PA_STREAM_START_CORKED;
996 if(!GetConfigValueBool(NULL, "pulse", "allow-moves", 0))
997 flags |= PA_STREAM_DONT_MOVE;
999 switch(device->FmtType)
1001 case DevFmtByte:
1002 device->FmtType = DevFmtUByte;
1003 /* fall-through */
1004 case DevFmtUByte:
1005 self->spec.format = PA_SAMPLE_U8;
1006 break;
1007 case DevFmtUShort:
1008 device->FmtType = DevFmtShort;
1009 /* fall-through */
1010 case DevFmtShort:
1011 self->spec.format = PA_SAMPLE_S16NE;
1012 break;
1013 case DevFmtUInt:
1014 device->FmtType = DevFmtInt;
1015 /* fall-through */
1016 case DevFmtInt:
1017 self->spec.format = PA_SAMPLE_S32NE;
1018 break;
1019 case DevFmtFloat:
1020 self->spec.format = PA_SAMPLE_FLOAT32NE;
1021 break;
1023 self->spec.rate = device->Frequency;
1024 self->spec.channels = ChannelsFromDevFmt(device->FmtChans, device->AmbiOrder);
1026 if(pa_sample_spec_valid(&self->spec) == 0)
1028 ERR("Invalid sample format\n");
1029 pa_threaded_mainloop_unlock(self->loop);
1030 return ALC_FALSE;
1033 switch(device->FmtChans)
1035 case DevFmtMono:
1036 mapname = "mono";
1037 break;
1038 case DevFmtAmbi3D:
1039 device->FmtChans = DevFmtStereo;
1040 /*fall-through*/
1041 case DevFmtStereo:
1042 mapname = "front-left,front-right";
1043 break;
1044 case DevFmtQuad:
1045 mapname = "front-left,front-right,rear-left,rear-right";
1046 break;
1047 case DevFmtX51:
1048 mapname = "front-left,front-right,front-center,lfe,side-left,side-right";
1049 break;
1050 case DevFmtX51Rear:
1051 mapname = "front-left,front-right,front-center,lfe,rear-left,rear-right";
1052 break;
1053 case DevFmtX61:
1054 mapname = "front-left,front-right,front-center,lfe,rear-center,side-left,side-right";
1055 break;
1056 case DevFmtX71:
1057 mapname = "front-left,front-right,front-center,lfe,rear-left,rear-right,side-left,side-right";
1058 break;
1060 if(!pa_channel_map_parse(&chanmap, mapname))
1062 ERR("Failed to build channel map for %s\n", DevFmtChannelsString(device->FmtChans));
1063 pa_threaded_mainloop_unlock(self->loop);
1064 return ALC_FALSE;
1066 SetDefaultWFXChannelOrder(device);
1068 self->attr.fragsize = -1;
1069 self->attr.prebuf = 0;
1070 self->attr.minreq = device->UpdateSize * pa_frame_size(&self->spec);
1071 self->attr.tlength = self->attr.minreq * maxu(device->NumUpdates, 2);
1072 self->attr.maxlength = -1;
1074 self->stream = ALCpulsePlayback_connectStream(alstr_get_cstr(self->device_name),
1075 self->loop, self->context, flags, &self->attr, &self->spec, &chanmap
1077 if(!self->stream)
1079 pa_threaded_mainloop_unlock(self->loop);
1080 return ALC_FALSE;
1082 pa_stream_set_state_callback(self->stream, ALCpulsePlayback_streamStateCallback, self);
1083 pa_stream_set_moved_callback(self->stream, ALCpulsePlayback_streamMovedCallback, self);
1084 pa_stream_set_write_callback(self->stream, ALCpulsePlayback_streamWriteCallback, self);
1086 self->spec = *(pa_stream_get_sample_spec(self->stream));
1087 if(device->Frequency != self->spec.rate)
1089 /* Server updated our playback rate, so modify the buffer attribs
1090 * accordingly. */
1091 device->NumUpdates = (ALuint)clampd(
1092 (ALdouble)device->NumUpdates/device->Frequency*self->spec.rate + 0.5, 2.0, 16.0
1095 self->attr.minreq = device->UpdateSize * pa_frame_size(&self->spec);
1096 self->attr.tlength = self->attr.minreq * device->NumUpdates;
1097 self->attr.maxlength = -1;
1098 self->attr.prebuf = 0;
1100 o = pa_stream_set_buffer_attr(self->stream, &self->attr,
1101 stream_success_callback, self->loop);
1102 wait_for_operation(o, self->loop);
1104 device->Frequency = self->spec.rate;
1107 pa_stream_set_buffer_attr_callback(self->stream, ALCpulsePlayback_bufferAttrCallback, self);
1108 ALCpulsePlayback_bufferAttrCallback(self->stream, self);
1110 device->NumUpdates = (ALuint)clampu64(
1111 (self->attr.tlength + self->attr.minreq/2) / self->attr.minreq, 2, 16
1113 device->UpdateSize = self->attr.minreq / pa_frame_size(&self->spec);
1115 /* HACK: prebuf should be 0 as that's what we set it to. However on some
1116 * systems it comes back as non-0, so we have to make sure the device will
1117 * write enough audio to start playback. The lack of manual start control
1118 * may have unintended consequences, but it's better than not starting at
1119 * all.
1121 if(self->attr.prebuf != 0)
1123 ALuint len = self->attr.prebuf / pa_frame_size(&self->spec);
1124 if(len <= device->UpdateSize*device->NumUpdates)
1125 ERR("Non-0 prebuf, %u samples (%u bytes), device has %u samples\n",
1126 len, self->attr.prebuf, device->UpdateSize*device->NumUpdates);
1127 else
1129 ERR("Large prebuf, %u samples (%u bytes), increasing device from %u samples",
1130 len, self->attr.prebuf, device->UpdateSize*device->NumUpdates);
1131 device->NumUpdates = (len+device->UpdateSize-1) / device->UpdateSize;
1135 pa_threaded_mainloop_unlock(self->loop);
1136 return ALC_TRUE;
1139 static ALCboolean ALCpulsePlayback_start(ALCpulsePlayback *self)
1141 self->killNow = AL_FALSE;
1142 if(althrd_create(&self->thread, ALCpulsePlayback_mixerProc, self) != althrd_success)
1143 return ALC_FALSE;
1144 return ALC_TRUE;
1147 static void ALCpulsePlayback_stop(ALCpulsePlayback *self)
1149 pa_operation *o;
1150 int res;
1152 if(!self->stream || self->killNow)
1153 return;
1155 self->killNow = AL_TRUE;
1156 /* Signal the main loop in case PulseAudio isn't sending us audio requests
1157 * (e.g. if the device is suspended). We need to lock the mainloop in case
1158 * the mixer is between checking the killNow flag but before waiting for
1159 * the signal.
1161 pa_threaded_mainloop_lock(self->loop);
1162 pa_threaded_mainloop_unlock(self->loop);
1163 pa_threaded_mainloop_signal(self->loop, 0);
1164 althrd_join(self->thread, &res);
1166 pa_threaded_mainloop_lock(self->loop);
1168 o = pa_stream_cork(self->stream, 1, stream_success_callback, self->loop);
1169 wait_for_operation(o, self->loop);
1171 pa_threaded_mainloop_unlock(self->loop);
1175 static ClockLatency ALCpulsePlayback_getClockLatency(ALCpulsePlayback *self)
1177 pa_usec_t latency = 0;
1178 ClockLatency ret;
1179 int neg, err;
1181 pa_threaded_mainloop_lock(self->loop);
1182 ret.ClockTime = GetDeviceClockTime(STATIC_CAST(ALCbackend,self)->mDevice);
1183 if((err=pa_stream_get_latency(self->stream, &latency, &neg)) != 0)
1185 /* FIXME: if err = -PA_ERR_NODATA, it means we were called too soon
1186 * after starting the stream and no timing info has been received from
1187 * the server yet. Should we wait, possibly stalling the app, or give a
1188 * dummy value? Either way, it shouldn't be 0. */
1189 if(err != -PA_ERR_NODATA)
1190 ERR("Failed to get stream latency: 0x%x\n", err);
1191 latency = 0;
1192 neg = 0;
1194 if(neg) latency = 0;
1195 ret.Latency = minu64(latency, U64(0xffffffffffffffff)/1000) * 1000;
1196 pa_threaded_mainloop_unlock(self->loop);
1198 return ret;
1202 static void ALCpulsePlayback_lock(ALCpulsePlayback *self)
1204 pa_threaded_mainloop_lock(self->loop);
1207 static void ALCpulsePlayback_unlock(ALCpulsePlayback *self)
1209 pa_threaded_mainloop_unlock(self->loop);
1213 typedef struct ALCpulseCapture {
1214 DERIVE_FROM_TYPE(ALCbackend);
1216 al_string device_name;
1218 const void *cap_store;
1219 size_t cap_len;
1220 size_t cap_remain;
1222 ALCuint last_readable;
1224 pa_buffer_attr attr;
1225 pa_sample_spec spec;
1227 pa_threaded_mainloop *loop;
1229 pa_stream *stream;
1230 pa_context *context;
1231 } ALCpulseCapture;
1233 static void ALCpulseCapture_deviceCallback(pa_context *context, const pa_source_info *info, int eol, void *pdata);
1234 static void ALCpulseCapture_probeDevices(void);
1236 static void ALCpulseCapture_contextStateCallback(pa_context *context, void *pdata);
1237 static void ALCpulseCapture_streamStateCallback(pa_stream *stream, void *pdata);
1238 static void ALCpulseCapture_sourceNameCallback(pa_context *context, const pa_source_info *info, int eol, void *pdata);
1239 static void ALCpulseCapture_streamMovedCallback(pa_stream *stream, void *pdata);
1240 static pa_stream *ALCpulseCapture_connectStream(const char *device_name,
1241 pa_threaded_mainloop *loop, pa_context *context,
1242 pa_stream_flags_t flags, pa_buffer_attr *attr,
1243 pa_sample_spec *spec, pa_channel_map *chanmap);
1245 static void ALCpulseCapture_Construct(ALCpulseCapture *self, ALCdevice *device);
1246 static void ALCpulseCapture_Destruct(ALCpulseCapture *self);
1247 static ALCenum ALCpulseCapture_open(ALCpulseCapture *self, const ALCchar *name);
1248 static void ALCpulseCapture_close(ALCpulseCapture *self);
1249 static DECLARE_FORWARD(ALCpulseCapture, ALCbackend, ALCboolean, reset)
1250 static ALCboolean ALCpulseCapture_start(ALCpulseCapture *self);
1251 static void ALCpulseCapture_stop(ALCpulseCapture *self);
1252 static ALCenum ALCpulseCapture_captureSamples(ALCpulseCapture *self, ALCvoid *buffer, ALCuint samples);
1253 static ALCuint ALCpulseCapture_availableSamples(ALCpulseCapture *self);
1254 static ClockLatency ALCpulseCapture_getClockLatency(ALCpulseCapture *self);
1255 static void ALCpulseCapture_lock(ALCpulseCapture *self);
1256 static void ALCpulseCapture_unlock(ALCpulseCapture *self);
1257 DECLARE_DEFAULT_ALLOCATORS(ALCpulseCapture)
1259 DEFINE_ALCBACKEND_VTABLE(ALCpulseCapture);
1262 static void ALCpulseCapture_Construct(ALCpulseCapture *self, ALCdevice *device)
1264 ALCbackend_Construct(STATIC_CAST(ALCbackend, self), device);
1265 SET_VTABLE2(ALCpulseCapture, ALCbackend, self);
1267 AL_STRING_INIT(self->device_name);
1270 static void ALCpulseCapture_Destruct(ALCpulseCapture *self)
1272 AL_STRING_DEINIT(self->device_name);
1273 ALCbackend_Destruct(STATIC_CAST(ALCbackend, self));
1277 static void ALCpulseCapture_deviceCallback(pa_context *UNUSED(context), const pa_source_info *info, int eol, void *pdata)
1279 pa_threaded_mainloop *loop = pdata;
1280 const DevMap *iter;
1281 DevMap entry;
1282 int count;
1284 if(eol)
1286 pa_threaded_mainloop_signal(loop, 0);
1287 return;
1290 #define MATCH_INFO_NAME(iter) (alstr_cmp_cstr((iter)->device_name, info->name) == 0)
1291 VECTOR_FIND_IF(iter, const DevMap, CaptureDevices, MATCH_INFO_NAME);
1292 if(iter != VECTOR_END(CaptureDevices)) return;
1293 #undef MATCH_INFO_NAME
1295 AL_STRING_INIT(entry.name);
1296 AL_STRING_INIT(entry.device_name);
1298 alstr_copy_cstr(&entry.device_name, info->name);
1300 count = 0;
1301 while(1)
1303 alstr_copy_cstr(&entry.name, info->description);
1304 if(count != 0)
1306 char str[64];
1307 snprintf(str, sizeof(str), " #%d", count+1);
1308 alstr_append_cstr(&entry.name, str);
1311 #define MATCH_ENTRY(i) (alstr_cmp(entry.name, (i)->name) == 0)
1312 VECTOR_FIND_IF(iter, const DevMap, CaptureDevices, MATCH_ENTRY);
1313 if(iter == VECTOR_END(CaptureDevices)) break;
1314 #undef MATCH_ENTRY
1315 count++;
1318 TRACE("Got device \"%s\", \"%s\"\n", alstr_get_cstr(entry.name), alstr_get_cstr(entry.device_name));
1320 VECTOR_PUSH_BACK(CaptureDevices, entry);
1323 static void ALCpulseCapture_probeDevices(void)
1325 pa_threaded_mainloop *loop;
1327 clear_devlist(&CaptureDevices);
1329 if((loop=pa_threaded_mainloop_new()) &&
1330 pa_threaded_mainloop_start(loop) >= 0)
1332 pa_context *context;
1334 pa_threaded_mainloop_lock(loop);
1335 context = connect_context(loop, AL_FALSE);
1336 if(context)
1338 pa_operation *o;
1339 pa_stream_flags_t flags;
1340 pa_sample_spec spec;
1341 pa_stream *stream;
1343 flags = PA_STREAM_FIX_FORMAT | PA_STREAM_FIX_RATE |
1344 PA_STREAM_FIX_CHANNELS | PA_STREAM_DONT_MOVE;
1346 spec.format = PA_SAMPLE_S16NE;
1347 spec.rate = 44100;
1348 spec.channels = 1;
1350 stream = ALCpulseCapture_connectStream(NULL, loop, context, flags,
1351 NULL, &spec, NULL);
1352 if(stream)
1354 o = pa_context_get_source_info_by_name(context, pa_stream_get_device_name(stream),
1355 ALCpulseCapture_deviceCallback, loop);
1356 wait_for_operation(o, loop);
1358 pa_stream_disconnect(stream);
1359 pa_stream_unref(stream);
1360 stream = NULL;
1363 o = pa_context_get_source_info_list(context, ALCpulseCapture_deviceCallback, loop);
1364 wait_for_operation(o, loop);
1366 pa_context_disconnect(context);
1367 pa_context_unref(context);
1369 pa_threaded_mainloop_unlock(loop);
1370 pa_threaded_mainloop_stop(loop);
1372 if(loop)
1373 pa_threaded_mainloop_free(loop);
1377 static void ALCpulseCapture_contextStateCallback(pa_context *context, void *pdata)
1379 ALCpulseCapture *self = pdata;
1380 if(pa_context_get_state(context) == PA_CONTEXT_FAILED)
1382 ERR("Received context failure!\n");
1383 aluHandleDisconnect(STATIC_CAST(ALCbackend,self)->mDevice);
1385 pa_threaded_mainloop_signal(self->loop, 0);
1388 static void ALCpulseCapture_streamStateCallback(pa_stream *stream, void *pdata)
1390 ALCpulseCapture *self = pdata;
1391 if(pa_stream_get_state(stream) == PA_STREAM_FAILED)
1393 ERR("Received stream failure!\n");
1394 aluHandleDisconnect(STATIC_CAST(ALCbackend,self)->mDevice);
1396 pa_threaded_mainloop_signal(self->loop, 0);
1400 static void ALCpulseCapture_sourceNameCallback(pa_context *UNUSED(context), const pa_source_info *info, int eol, void *pdata)
1402 ALCpulseCapture *self = pdata;
1403 ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
1405 if(eol)
1407 pa_threaded_mainloop_signal(self->loop, 0);
1408 return;
1411 alstr_copy_cstr(&device->DeviceName, info->description);
1415 static void ALCpulseCapture_streamMovedCallback(pa_stream *stream, void *pdata)
1417 ALCpulseCapture *self = pdata;
1419 alstr_copy_cstr(&self->device_name, pa_stream_get_device_name(stream));
1421 TRACE("Stream moved to %s\n", alstr_get_cstr(self->device_name));
1425 static pa_stream *ALCpulseCapture_connectStream(const char *device_name,
1426 pa_threaded_mainloop *loop, pa_context *context,
1427 pa_stream_flags_t flags, pa_buffer_attr *attr, pa_sample_spec *spec,
1428 pa_channel_map *chanmap)
1430 pa_stream_state_t state;
1431 pa_stream *stream;
1433 stream = pa_stream_new_with_proplist(context, "Capture Stream", spec, chanmap, prop_filter);
1434 if(!stream)
1436 ERR("pa_stream_new_with_proplist() failed: %s\n", pa_strerror(pa_context_errno(context)));
1437 return NULL;
1440 pa_stream_set_state_callback(stream, stream_state_callback, loop);
1442 if(pa_stream_connect_record(stream, device_name, attr, flags) < 0)
1444 ERR("Stream did not connect: %s\n", pa_strerror(pa_context_errno(context)));
1445 pa_stream_unref(stream);
1446 return NULL;
1449 while((state=pa_stream_get_state(stream)) != PA_STREAM_READY)
1451 if(!PA_STREAM_IS_GOOD(state))
1453 ERR("Stream did not get ready: %s\n", pa_strerror(pa_context_errno(context)));
1454 pa_stream_unref(stream);
1455 return NULL;
1458 pa_threaded_mainloop_wait(loop);
1460 pa_stream_set_state_callback(stream, NULL, NULL);
1462 return stream;
1466 static ALCenum ALCpulseCapture_open(ALCpulseCapture *self, const ALCchar *name)
1468 ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
1469 const char *pulse_name = NULL;
1470 pa_stream_flags_t flags = 0;
1471 const char *mapname = NULL;
1472 pa_channel_map chanmap;
1473 ALuint samples;
1475 if(name)
1477 const DevMap *iter;
1479 if(VECTOR_SIZE(CaptureDevices) == 0)
1480 ALCpulseCapture_probeDevices();
1482 #define MATCH_NAME(iter) (alstr_cmp_cstr((iter)->name, name) == 0)
1483 VECTOR_FIND_IF(iter, const DevMap, CaptureDevices, MATCH_NAME);
1484 #undef MATCH_NAME
1485 if(iter == VECTOR_END(CaptureDevices))
1486 return ALC_INVALID_VALUE;
1487 pulse_name = alstr_get_cstr(iter->device_name);
1488 alstr_copy(&device->DeviceName, iter->name);
1491 if(!pulse_open(&self->loop, &self->context, ALCpulseCapture_contextStateCallback, self))
1492 return ALC_INVALID_VALUE;
1494 pa_threaded_mainloop_lock(self->loop);
1496 switch(device->FmtType)
1498 case DevFmtUByte:
1499 self->spec.format = PA_SAMPLE_U8;
1500 break;
1501 case DevFmtShort:
1502 self->spec.format = PA_SAMPLE_S16NE;
1503 break;
1504 case DevFmtInt:
1505 self->spec.format = PA_SAMPLE_S32NE;
1506 break;
1507 case DevFmtFloat:
1508 self->spec.format = PA_SAMPLE_FLOAT32NE;
1509 break;
1510 case DevFmtByte:
1511 case DevFmtUShort:
1512 case DevFmtUInt:
1513 ERR("%s capture samples not supported\n", DevFmtTypeString(device->FmtType));
1514 pa_threaded_mainloop_unlock(self->loop);
1515 goto fail;
1518 switch(device->FmtChans)
1520 case DevFmtMono:
1521 mapname = "mono";
1522 break;
1523 case DevFmtStereo:
1524 mapname = "front-left,front-right";
1525 break;
1526 case DevFmtQuad:
1527 mapname = "front-left,front-right,rear-left,rear-right";
1528 break;
1529 case DevFmtX51:
1530 mapname = "front-left,front-right,front-center,lfe,side-left,side-right";
1531 break;
1532 case DevFmtX51Rear:
1533 mapname = "front-left,front-right,front-center,lfe,rear-left,rear-right";
1534 break;
1535 case DevFmtX61:
1536 mapname = "front-left,front-right,front-center,lfe,rear-center,side-left,side-right";
1537 break;
1538 case DevFmtX71:
1539 mapname = "front-left,front-right,front-center,lfe,rear-left,rear-right,side-left,side-right";
1540 break;
1541 case DevFmtAmbi3D:
1542 ERR("%s capture samples not supported\n", DevFmtChannelsString(device->FmtChans));
1543 pa_threaded_mainloop_unlock(self->loop);
1544 goto fail;
1546 if(!pa_channel_map_parse(&chanmap, mapname))
1548 ERR("Failed to build channel map for %s\n", DevFmtChannelsString(device->FmtChans));
1549 pa_threaded_mainloop_unlock(self->loop);
1550 return ALC_FALSE;
1553 self->spec.rate = device->Frequency;
1554 self->spec.channels = ChannelsFromDevFmt(device->FmtChans, device->AmbiOrder);
1556 if(pa_sample_spec_valid(&self->spec) == 0)
1558 ERR("Invalid sample format\n");
1559 pa_threaded_mainloop_unlock(self->loop);
1560 goto fail;
1563 if(!pa_channel_map_init_auto(&chanmap, self->spec.channels, PA_CHANNEL_MAP_WAVEEX))
1565 ERR("Couldn't build map for channel count (%d)!\n", self->spec.channels);
1566 pa_threaded_mainloop_unlock(self->loop);
1567 goto fail;
1570 samples = device->UpdateSize * device->NumUpdates;
1571 samples = maxu(samples, 100 * device->Frequency / 1000);
1573 self->attr.minreq = -1;
1574 self->attr.prebuf = -1;
1575 self->attr.maxlength = samples * pa_frame_size(&self->spec);
1576 self->attr.tlength = -1;
1577 self->attr.fragsize = minu(samples, 50*device->Frequency/1000) *
1578 pa_frame_size(&self->spec);
1580 flags |= PA_STREAM_START_CORKED|PA_STREAM_ADJUST_LATENCY;
1581 if(!GetConfigValueBool(NULL, "pulse", "allow-moves", 0))
1582 flags |= PA_STREAM_DONT_MOVE;
1584 TRACE("Connecting to \"%s\"\n", pulse_name ? pulse_name : "(default)");
1585 self->stream = ALCpulseCapture_connectStream(pulse_name,
1586 self->loop, self->context, flags, &self->attr, &self->spec, &chanmap
1588 if(!self->stream)
1590 pa_threaded_mainloop_unlock(self->loop);
1591 goto fail;
1593 pa_stream_set_moved_callback(self->stream, ALCpulseCapture_streamMovedCallback, self);
1594 pa_stream_set_state_callback(self->stream, ALCpulseCapture_streamStateCallback, self);
1596 alstr_copy_cstr(&self->device_name, pa_stream_get_device_name(self->stream));
1597 if(alstr_empty(device->DeviceName))
1599 pa_operation *o = pa_context_get_source_info_by_name(
1600 self->context, alstr_get_cstr(self->device_name),
1601 ALCpulseCapture_sourceNameCallback, self
1603 wait_for_operation(o, self->loop);
1606 pa_threaded_mainloop_unlock(self->loop);
1607 return ALC_NO_ERROR;
1609 fail:
1610 pulse_close(self->loop, self->context, self->stream);
1611 self->loop = NULL;
1612 self->context = NULL;
1613 self->stream = NULL;
1615 return ALC_INVALID_VALUE;
1618 static void ALCpulseCapture_close(ALCpulseCapture *self)
1620 pulse_close(self->loop, self->context, self->stream);
1621 self->loop = NULL;
1622 self->context = NULL;
1623 self->stream = NULL;
1625 alstr_clear(&self->device_name);
1628 static ALCboolean ALCpulseCapture_start(ALCpulseCapture *self)
1630 pa_operation *o;
1631 pa_threaded_mainloop_lock(self->loop);
1632 o = pa_stream_cork(self->stream, 0, stream_success_callback, self->loop);
1633 wait_for_operation(o, self->loop);
1634 pa_threaded_mainloop_unlock(self->loop);
1635 return ALC_TRUE;
1638 static void ALCpulseCapture_stop(ALCpulseCapture *self)
1640 pa_operation *o;
1641 pa_threaded_mainloop_lock(self->loop);
1642 o = pa_stream_cork(self->stream, 1, stream_success_callback, self->loop);
1643 wait_for_operation(o, self->loop);
1644 pa_threaded_mainloop_unlock(self->loop);
1647 static ALCenum ALCpulseCapture_captureSamples(ALCpulseCapture *self, ALCvoid *buffer, ALCuint samples)
1649 ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
1650 ALCuint todo = samples * pa_frame_size(&self->spec);
1652 /* Capture is done in fragment-sized chunks, so we loop until we get all
1653 * that's available */
1654 self->last_readable -= todo;
1655 pa_threaded_mainloop_lock(self->loop);
1656 while(todo > 0)
1658 size_t rem = todo;
1660 if(self->cap_len == 0)
1662 pa_stream_state_t state;
1664 state = pa_stream_get_state(self->stream);
1665 if(!PA_STREAM_IS_GOOD(state))
1667 aluHandleDisconnect(device);
1668 break;
1670 if(pa_stream_peek(self->stream, &self->cap_store, &self->cap_len) < 0)
1672 ERR("pa_stream_peek() failed: %s\n",
1673 pa_strerror(pa_context_errno(self->context)));
1674 aluHandleDisconnect(device);
1675 break;
1677 self->cap_remain = self->cap_len;
1679 if(rem > self->cap_remain)
1680 rem = self->cap_remain;
1682 memcpy(buffer, self->cap_store, rem);
1684 buffer = (ALbyte*)buffer + rem;
1685 todo -= rem;
1687 self->cap_store = (ALbyte*)self->cap_store + rem;
1688 self->cap_remain -= rem;
1689 if(self->cap_remain == 0)
1691 pa_stream_drop(self->stream);
1692 self->cap_len = 0;
1695 pa_threaded_mainloop_unlock(self->loop);
1696 if(todo > 0)
1697 memset(buffer, ((device->FmtType==DevFmtUByte) ? 0x80 : 0), todo);
1699 return ALC_NO_ERROR;
1702 static ALCuint ALCpulseCapture_availableSamples(ALCpulseCapture *self)
1704 ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
1705 size_t readable = self->cap_remain;
1707 if(device->Connected)
1709 ssize_t got;
1710 pa_threaded_mainloop_lock(self->loop);
1711 got = pa_stream_readable_size(self->stream);
1712 if(got < 0)
1714 ERR("pa_stream_readable_size() failed: %s\n", pa_strerror(got));
1715 aluHandleDisconnect(device);
1717 else if((size_t)got > self->cap_len)
1718 readable += got - self->cap_len;
1719 pa_threaded_mainloop_unlock(self->loop);
1722 if(self->last_readable < readable)
1723 self->last_readable = readable;
1724 return self->last_readable / pa_frame_size(&self->spec);
1728 static ClockLatency ALCpulseCapture_getClockLatency(ALCpulseCapture *self)
1730 pa_usec_t latency = 0;
1731 ClockLatency ret;
1732 int neg, err;
1734 pa_threaded_mainloop_lock(self->loop);
1735 ret.ClockTime = GetDeviceClockTime(STATIC_CAST(ALCbackend,self)->mDevice);
1736 if((err=pa_stream_get_latency(self->stream, &latency, &neg)) != 0)
1738 ERR("Failed to get stream latency: 0x%x\n", err);
1739 latency = 0;
1740 neg = 0;
1742 if(neg) latency = 0;
1743 ret.Latency = minu64(latency, U64(0xffffffffffffffff)/1000) * 1000;
1744 pa_threaded_mainloop_unlock(self->loop);
1746 return ret;
1750 static void ALCpulseCapture_lock(ALCpulseCapture *self)
1752 pa_threaded_mainloop_lock(self->loop);
1755 static void ALCpulseCapture_unlock(ALCpulseCapture *self)
1757 pa_threaded_mainloop_unlock(self->loop);
1761 typedef struct ALCpulseBackendFactory {
1762 DERIVE_FROM_TYPE(ALCbackendFactory);
1763 } ALCpulseBackendFactory;
1764 #define ALCPULSEBACKENDFACTORY_INITIALIZER { { GET_VTABLE2(ALCpulseBackendFactory, ALCbackendFactory) } }
1766 static ALCboolean ALCpulseBackendFactory_init(ALCpulseBackendFactory *self);
1767 static void ALCpulseBackendFactory_deinit(ALCpulseBackendFactory *self);
1768 static ALCboolean ALCpulseBackendFactory_querySupport(ALCpulseBackendFactory *self, ALCbackend_Type type);
1769 static void ALCpulseBackendFactory_probe(ALCpulseBackendFactory *self, enum DevProbe type);
1770 static ALCbackend* ALCpulseBackendFactory_createBackend(ALCpulseBackendFactory *self, ALCdevice *device, ALCbackend_Type type);
1772 DEFINE_ALCBACKENDFACTORY_VTABLE(ALCpulseBackendFactory);
1775 static ALCboolean ALCpulseBackendFactory_init(ALCpulseBackendFactory* UNUSED(self))
1777 ALCboolean ret = ALC_FALSE;
1779 VECTOR_INIT(PlaybackDevices);
1780 VECTOR_INIT(CaptureDevices);
1782 if(pulse_load())
1784 pa_threaded_mainloop *loop;
1786 pulse_ctx_flags = 0;
1787 if(!GetConfigValueBool(NULL, "pulse", "spawn-server", 1))
1788 pulse_ctx_flags |= PA_CONTEXT_NOAUTOSPAWN;
1790 if((loop=pa_threaded_mainloop_new()) &&
1791 pa_threaded_mainloop_start(loop) >= 0)
1793 pa_context *context;
1795 pa_threaded_mainloop_lock(loop);
1796 context = connect_context(loop, AL_TRUE);
1797 if(context)
1799 ret = ALC_TRUE;
1801 /* Some libraries (Phonon, Qt) set some pulseaudio properties
1802 * through environment variables, which causes all streams in
1803 * the process to inherit them. This attempts to filter those
1804 * properties out by setting them to 0-length data. */
1805 prop_filter = pa_proplist_new();
1806 pa_proplist_set(prop_filter, PA_PROP_MEDIA_ROLE, NULL, 0);
1807 pa_proplist_set(prop_filter, "phonon.streamid", NULL, 0);
1809 pa_context_disconnect(context);
1810 pa_context_unref(context);
1812 pa_threaded_mainloop_unlock(loop);
1813 pa_threaded_mainloop_stop(loop);
1815 if(loop)
1816 pa_threaded_mainloop_free(loop);
1819 return ret;
1822 static void ALCpulseBackendFactory_deinit(ALCpulseBackendFactory* UNUSED(self))
1824 clear_devlist(&PlaybackDevices);
1825 VECTOR_DEINIT(PlaybackDevices);
1827 clear_devlist(&CaptureDevices);
1828 VECTOR_DEINIT(CaptureDevices);
1830 if(prop_filter)
1831 pa_proplist_free(prop_filter);
1832 prop_filter = NULL;
1834 /* PulseAudio doesn't like being CloseLib'd sometimes */
1837 static ALCboolean ALCpulseBackendFactory_querySupport(ALCpulseBackendFactory* UNUSED(self), ALCbackend_Type type)
1839 if(type == ALCbackend_Playback || type == ALCbackend_Capture)
1840 return ALC_TRUE;
1841 return ALC_FALSE;
1844 static void ALCpulseBackendFactory_probe(ALCpulseBackendFactory* UNUSED(self), enum DevProbe type)
1846 switch(type)
1848 case ALL_DEVICE_PROBE:
1849 ALCpulsePlayback_probeDevices();
1850 #define APPEND_ALL_DEVICES_LIST(e) AppendAllDevicesList(alstr_get_cstr((e)->name))
1851 VECTOR_FOR_EACH(const DevMap, PlaybackDevices, APPEND_ALL_DEVICES_LIST);
1852 #undef APPEND_ALL_DEVICES_LIST
1853 break;
1855 case CAPTURE_DEVICE_PROBE:
1856 ALCpulseCapture_probeDevices();
1857 #define APPEND_CAPTURE_DEVICE_LIST(e) AppendCaptureDeviceList(alstr_get_cstr((e)->name))
1858 VECTOR_FOR_EACH(const DevMap, CaptureDevices, APPEND_CAPTURE_DEVICE_LIST);
1859 #undef APPEND_CAPTURE_DEVICE_LIST
1860 break;
1864 static ALCbackend* ALCpulseBackendFactory_createBackend(ALCpulseBackendFactory* UNUSED(self), ALCdevice *device, ALCbackend_Type type)
1866 if(type == ALCbackend_Playback)
1868 ALCpulsePlayback *backend;
1869 NEW_OBJ(backend, ALCpulsePlayback)(device);
1870 if(!backend) return NULL;
1871 return STATIC_CAST(ALCbackend, backend);
1873 if(type == ALCbackend_Capture)
1875 ALCpulseCapture *backend;
1876 NEW_OBJ(backend, ALCpulseCapture)(device);
1877 if(!backend) return NULL;
1878 return STATIC_CAST(ALCbackend, backend);
1881 return NULL;
1885 #else /* PA_API_VERSION == 12 */
1887 #warning "Unsupported API version, backend will be unavailable!"
1889 typedef struct ALCpulseBackendFactory {
1890 DERIVE_FROM_TYPE(ALCbackendFactory);
1891 } ALCpulseBackendFactory;
1892 #define ALCPULSEBACKENDFACTORY_INITIALIZER { { GET_VTABLE2(ALCpulseBackendFactory, ALCbackendFactory) } }
1894 static ALCboolean ALCpulseBackendFactory_init(ALCpulseBackendFactory* UNUSED(self))
1896 return ALC_FALSE;
1899 static void ALCpulseBackendFactory_deinit(ALCpulseBackendFactory* UNUSED(self))
1903 static ALCboolean ALCpulseBackendFactory_querySupport(ALCpulseBackendFactory* UNUSED(self), ALCbackend_Type UNUSED(type))
1905 return ALC_FALSE;
1908 static void ALCpulseBackendFactory_probe(ALCpulseBackendFactory* UNUSED(self), enum DevProbe UNUSED(type))
1912 static ALCbackend* ALCpulseBackendFactory_createBackend(ALCpulseBackendFactory* UNUSED(self), ALCdevice* UNUSED(device), ALCbackend_Type UNUSED(type))
1914 return NULL;
1917 DEFINE_ALCBACKENDFACTORY_VTABLE(ALCpulseBackendFactory);
1919 #endif /* PA_API_VERSION == 12 */
1921 ALCbackendFactory *ALCpulseBackendFactory_getFactory(void)
1923 static ALCpulseBackendFactory factory = ALCPULSEBACKENDFACTORY_INITIALIZER;
1924 return STATIC_CAST(ALCbackendFactory, &factory);