Move the device lock into the backend function table
[openal-soft.git] / Alc / backends / pulseaudio.c
blobde8a40d0abb1a6e0cfa4de344a243ef470902537
1 /**
2 * OpenAL cross platform audio library
3 * Copyright (C) 2009 by Konstantinos Natsakis <konstantinos.natsakis@gmail.com>
4 * Copyright (C) 2010 by Chris Robinson <chris.kcat@gmail.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
19 * Or go to http://www.gnu.org/copyleft/lgpl.html
22 #include "config.h"
24 #include <string.h>
25 #include "alMain.h"
27 #include <pulse/pulseaudio.h>
29 #if PA_API_VERSION == 12
31 #ifndef PA_CHECK_VERSION
32 #define PA_CHECK_VERSION(major,minor,micro) \
33 ((PA_MAJOR > (major)) || \
34 (PA_MAJOR == (major) && PA_MINOR > (minor)) || \
35 (PA_MAJOR == (major) && PA_MINOR == (minor) && PA_MICRO >= (micro)))
36 #endif
38 #ifdef HAVE_DYNLOAD
39 static void *pa_handle;
40 #define MAKE_FUNC(x) static typeof(x) * p##x
41 MAKE_FUNC(pa_context_unref);
42 MAKE_FUNC(pa_sample_spec_valid);
43 MAKE_FUNC(pa_frame_size);
44 MAKE_FUNC(pa_stream_drop);
45 MAKE_FUNC(pa_strerror);
46 MAKE_FUNC(pa_context_get_state);
47 MAKE_FUNC(pa_stream_get_state);
48 MAKE_FUNC(pa_threaded_mainloop_signal);
49 MAKE_FUNC(pa_stream_peek);
50 MAKE_FUNC(pa_threaded_mainloop_wait);
51 MAKE_FUNC(pa_threaded_mainloop_unlock);
52 MAKE_FUNC(pa_threaded_mainloop_in_thread);
53 MAKE_FUNC(pa_context_new);
54 MAKE_FUNC(pa_threaded_mainloop_stop);
55 MAKE_FUNC(pa_context_disconnect);
56 MAKE_FUNC(pa_threaded_mainloop_start);
57 MAKE_FUNC(pa_threaded_mainloop_get_api);
58 MAKE_FUNC(pa_context_set_state_callback);
59 MAKE_FUNC(pa_stream_write);
60 MAKE_FUNC(pa_xfree);
61 MAKE_FUNC(pa_stream_connect_record);
62 MAKE_FUNC(pa_stream_connect_playback);
63 MAKE_FUNC(pa_stream_readable_size);
64 MAKE_FUNC(pa_stream_writable_size);
65 MAKE_FUNC(pa_stream_is_corked);
66 MAKE_FUNC(pa_stream_cork);
67 MAKE_FUNC(pa_stream_is_suspended);
68 MAKE_FUNC(pa_stream_get_device_name);
69 MAKE_FUNC(pa_stream_get_latency);
70 MAKE_FUNC(pa_path_get_filename);
71 MAKE_FUNC(pa_get_binary_name);
72 MAKE_FUNC(pa_threaded_mainloop_free);
73 MAKE_FUNC(pa_context_errno);
74 MAKE_FUNC(pa_xmalloc);
75 MAKE_FUNC(pa_stream_unref);
76 MAKE_FUNC(pa_threaded_mainloop_accept);
77 MAKE_FUNC(pa_stream_set_write_callback);
78 MAKE_FUNC(pa_threaded_mainloop_new);
79 MAKE_FUNC(pa_context_connect);
80 MAKE_FUNC(pa_stream_set_buffer_attr);
81 MAKE_FUNC(pa_stream_get_buffer_attr);
82 MAKE_FUNC(pa_stream_get_sample_spec);
83 MAKE_FUNC(pa_stream_get_time);
84 MAKE_FUNC(pa_stream_set_read_callback);
85 MAKE_FUNC(pa_stream_set_state_callback);
86 MAKE_FUNC(pa_stream_set_moved_callback);
87 MAKE_FUNC(pa_stream_set_underflow_callback);
88 MAKE_FUNC(pa_stream_new_with_proplist);
89 MAKE_FUNC(pa_stream_disconnect);
90 MAKE_FUNC(pa_threaded_mainloop_lock);
91 MAKE_FUNC(pa_channel_map_init_auto);
92 MAKE_FUNC(pa_channel_map_parse);
93 MAKE_FUNC(pa_channel_map_snprint);
94 MAKE_FUNC(pa_channel_map_equal);
95 MAKE_FUNC(pa_context_get_server_info);
96 MAKE_FUNC(pa_context_get_sink_info_by_name);
97 MAKE_FUNC(pa_context_get_sink_info_list);
98 MAKE_FUNC(pa_context_get_source_info_by_name);
99 MAKE_FUNC(pa_context_get_source_info_list);
100 MAKE_FUNC(pa_operation_get_state);
101 MAKE_FUNC(pa_operation_unref);
102 MAKE_FUNC(pa_proplist_new);
103 MAKE_FUNC(pa_proplist_free);
104 MAKE_FUNC(pa_proplist_set);
105 #if PA_CHECK_VERSION(0,9,15)
106 MAKE_FUNC(pa_channel_map_superset);
107 MAKE_FUNC(pa_stream_set_buffer_attr_callback);
108 #endif
109 #if PA_CHECK_VERSION(0,9,16)
110 MAKE_FUNC(pa_stream_begin_write);
111 #endif
112 #undef MAKE_FUNC
114 #define pa_context_unref ppa_context_unref
115 #define pa_sample_spec_valid ppa_sample_spec_valid
116 #define pa_frame_size ppa_frame_size
117 #define pa_stream_drop ppa_stream_drop
118 #define pa_strerror ppa_strerror
119 #define pa_context_get_state ppa_context_get_state
120 #define pa_stream_get_state ppa_stream_get_state
121 #define pa_threaded_mainloop_signal ppa_threaded_mainloop_signal
122 #define pa_stream_peek ppa_stream_peek
123 #define pa_threaded_mainloop_wait ppa_threaded_mainloop_wait
124 #define pa_threaded_mainloop_unlock ppa_threaded_mainloop_unlock
125 #define pa_threaded_mainloop_in_thread ppa_threaded_mainloop_in_thread
126 #define pa_context_new ppa_context_new
127 #define pa_threaded_mainloop_stop ppa_threaded_mainloop_stop
128 #define pa_context_disconnect ppa_context_disconnect
129 #define pa_threaded_mainloop_start ppa_threaded_mainloop_start
130 #define pa_threaded_mainloop_get_api ppa_threaded_mainloop_get_api
131 #define pa_context_set_state_callback ppa_context_set_state_callback
132 #define pa_stream_write ppa_stream_write
133 #define pa_xfree ppa_xfree
134 #define pa_stream_connect_record ppa_stream_connect_record
135 #define pa_stream_connect_playback ppa_stream_connect_playback
136 #define pa_stream_readable_size ppa_stream_readable_size
137 #define pa_stream_writable_size ppa_stream_writable_size
138 #define pa_stream_is_corked ppa_stream_is_corked
139 #define pa_stream_cork ppa_stream_cork
140 #define pa_stream_is_suspended ppa_stream_is_suspended
141 #define pa_stream_get_device_name ppa_stream_get_device_name
142 #define pa_stream_get_latency ppa_stream_get_latency
143 #define pa_path_get_filename ppa_path_get_filename
144 #define pa_get_binary_name ppa_get_binary_name
145 #define pa_threaded_mainloop_free ppa_threaded_mainloop_free
146 #define pa_context_errno ppa_context_errno
147 #define pa_xmalloc ppa_xmalloc
148 #define pa_stream_unref ppa_stream_unref
149 #define pa_threaded_mainloop_accept ppa_threaded_mainloop_accept
150 #define pa_stream_set_write_callback ppa_stream_set_write_callback
151 #define pa_threaded_mainloop_new ppa_threaded_mainloop_new
152 #define pa_context_connect ppa_context_connect
153 #define pa_stream_set_buffer_attr ppa_stream_set_buffer_attr
154 #define pa_stream_get_buffer_attr ppa_stream_get_buffer_attr
155 #define pa_stream_get_sample_spec ppa_stream_get_sample_spec
156 #define pa_stream_get_time ppa_stream_get_time
157 #define pa_stream_set_read_callback ppa_stream_set_read_callback
158 #define pa_stream_set_state_callback ppa_stream_set_state_callback
159 #define pa_stream_set_moved_callback ppa_stream_set_moved_callback
160 #define pa_stream_set_underflow_callback ppa_stream_set_underflow_callback
161 #define pa_stream_new_with_proplist ppa_stream_new_with_proplist
162 #define pa_stream_disconnect ppa_stream_disconnect
163 #define pa_threaded_mainloop_lock ppa_threaded_mainloop_lock
164 #define pa_channel_map_init_auto ppa_channel_map_init_auto
165 #define pa_channel_map_parse ppa_channel_map_parse
166 #define pa_channel_map_snprint ppa_channel_map_snprint
167 #define pa_channel_map_equal ppa_channel_map_equal
168 #define pa_context_get_server_info ppa_context_get_server_info
169 #define pa_context_get_sink_info_by_name ppa_context_get_sink_info_by_name
170 #define pa_context_get_sink_info_list ppa_context_get_sink_info_list
171 #define pa_context_get_source_info_by_name ppa_context_get_source_info_by_name
172 #define pa_context_get_source_info_list ppa_context_get_source_info_list
173 #define pa_operation_get_state ppa_operation_get_state
174 #define pa_operation_unref ppa_operation_unref
175 #define pa_proplist_new ppa_proplist_new
176 #define pa_proplist_free ppa_proplist_free
177 #define pa_proplist_set ppa_proplist_set
178 #if PA_CHECK_VERSION(0,9,15)
179 #define pa_channel_map_superset ppa_channel_map_superset
180 #define pa_stream_set_buffer_attr_callback ppa_stream_set_buffer_attr_callback
181 #endif
182 #if PA_CHECK_VERSION(0,9,16)
183 #define pa_stream_begin_write ppa_stream_begin_write
184 #endif
186 #endif
188 #ifndef PATH_MAX
189 #define PATH_MAX 4096
190 #endif
192 typedef struct {
193 char *device_name;
195 const void *cap_store;
196 size_t cap_len;
197 size_t cap_remain;
199 ALCuint last_readable;
201 pa_buffer_attr attr;
202 pa_sample_spec spec;
204 pa_threaded_mainloop *loop;
206 ALvoid *thread;
207 volatile ALboolean killNow;
209 pa_stream *stream;
210 pa_context *context;
211 } pulse_data;
213 typedef struct {
214 char *name;
215 char *device_name;
216 } DevMap;
219 static DevMap *allDevNameMap;
220 static ALuint numDevNames;
221 static DevMap *allCaptureDevNameMap;
222 static ALuint numCaptureDevNames;
223 static pa_context_flags_t pulse_ctx_flags;
224 static pa_proplist *prop_filter;
227 static ALCboolean pulse_load(void)
229 ALCboolean ret = ALC_TRUE;
230 #ifdef HAVE_DYNLOAD
231 if(!pa_handle)
233 #ifdef _WIN32
234 #define PALIB "libpulse-0.dll"
235 #elif defined(__APPLE__) && defined(__MACH__)
236 #define PALIB "libpulse.0.dylib"
237 #else
238 #define PALIB "libpulse.so.0"
239 #endif
240 pa_handle = LoadLib(PALIB);
241 if(!pa_handle)
242 return ALC_FALSE;
244 #define LOAD_FUNC(x) do { \
245 p##x = GetSymbol(pa_handle, #x); \
246 if(!(p##x)) { \
247 ret = ALC_FALSE; \
249 } while(0)
250 LOAD_FUNC(pa_context_unref);
251 LOAD_FUNC(pa_sample_spec_valid);
252 LOAD_FUNC(pa_stream_drop);
253 LOAD_FUNC(pa_frame_size);
254 LOAD_FUNC(pa_strerror);
255 LOAD_FUNC(pa_context_get_state);
256 LOAD_FUNC(pa_stream_get_state);
257 LOAD_FUNC(pa_threaded_mainloop_signal);
258 LOAD_FUNC(pa_stream_peek);
259 LOAD_FUNC(pa_threaded_mainloop_wait);
260 LOAD_FUNC(pa_threaded_mainloop_unlock);
261 LOAD_FUNC(pa_threaded_mainloop_in_thread);
262 LOAD_FUNC(pa_context_new);
263 LOAD_FUNC(pa_threaded_mainloop_stop);
264 LOAD_FUNC(pa_context_disconnect);
265 LOAD_FUNC(pa_threaded_mainloop_start);
266 LOAD_FUNC(pa_threaded_mainloop_get_api);
267 LOAD_FUNC(pa_context_set_state_callback);
268 LOAD_FUNC(pa_stream_write);
269 LOAD_FUNC(pa_xfree);
270 LOAD_FUNC(pa_stream_connect_record);
271 LOAD_FUNC(pa_stream_connect_playback);
272 LOAD_FUNC(pa_stream_readable_size);
273 LOAD_FUNC(pa_stream_writable_size);
274 LOAD_FUNC(pa_stream_is_corked);
275 LOAD_FUNC(pa_stream_cork);
276 LOAD_FUNC(pa_stream_is_suspended);
277 LOAD_FUNC(pa_stream_get_device_name);
278 LOAD_FUNC(pa_stream_get_latency);
279 LOAD_FUNC(pa_path_get_filename);
280 LOAD_FUNC(pa_get_binary_name);
281 LOAD_FUNC(pa_threaded_mainloop_free);
282 LOAD_FUNC(pa_context_errno);
283 LOAD_FUNC(pa_xmalloc);
284 LOAD_FUNC(pa_stream_unref);
285 LOAD_FUNC(pa_threaded_mainloop_accept);
286 LOAD_FUNC(pa_stream_set_write_callback);
287 LOAD_FUNC(pa_threaded_mainloop_new);
288 LOAD_FUNC(pa_context_connect);
289 LOAD_FUNC(pa_stream_set_buffer_attr);
290 LOAD_FUNC(pa_stream_get_buffer_attr);
291 LOAD_FUNC(pa_stream_get_sample_spec);
292 LOAD_FUNC(pa_stream_get_time);
293 LOAD_FUNC(pa_stream_set_read_callback);
294 LOAD_FUNC(pa_stream_set_state_callback);
295 LOAD_FUNC(pa_stream_set_moved_callback);
296 LOAD_FUNC(pa_stream_set_underflow_callback);
297 LOAD_FUNC(pa_stream_new_with_proplist);
298 LOAD_FUNC(pa_stream_disconnect);
299 LOAD_FUNC(pa_threaded_mainloop_lock);
300 LOAD_FUNC(pa_channel_map_init_auto);
301 LOAD_FUNC(pa_channel_map_parse);
302 LOAD_FUNC(pa_channel_map_snprint);
303 LOAD_FUNC(pa_channel_map_equal);
304 LOAD_FUNC(pa_context_get_server_info);
305 LOAD_FUNC(pa_context_get_sink_info_by_name);
306 LOAD_FUNC(pa_context_get_sink_info_list);
307 LOAD_FUNC(pa_context_get_source_info_by_name);
308 LOAD_FUNC(pa_context_get_source_info_list);
309 LOAD_FUNC(pa_operation_get_state);
310 LOAD_FUNC(pa_operation_unref);
311 LOAD_FUNC(pa_proplist_new);
312 LOAD_FUNC(pa_proplist_free);
313 LOAD_FUNC(pa_proplist_set);
314 #undef LOAD_FUNC
315 #define LOAD_OPTIONAL_FUNC(x) do { \
316 p##x = GetSymbol(pa_handle, #x); \
317 } while(0)
318 #if PA_CHECK_VERSION(0,9,15)
319 LOAD_OPTIONAL_FUNC(pa_channel_map_superset);
320 LOAD_OPTIONAL_FUNC(pa_stream_set_buffer_attr_callback);
321 #endif
322 #if PA_CHECK_VERSION(0,9,16)
323 LOAD_OPTIONAL_FUNC(pa_stream_begin_write);
324 #endif
325 #undef LOAD_OPTIONAL_FUNC
327 if(ret == ALC_FALSE)
329 CloseLib(pa_handle);
330 pa_handle = NULL;
333 #endif /* HAVE_DYNLOAD */
334 return ret;
337 /* PulseAudio Event Callbacks */
338 static void context_state_callback(pa_context *context, void *pdata)
340 pa_threaded_mainloop *loop = pdata;
341 pa_context_state_t state;
343 state = pa_context_get_state(context);
344 if(state == PA_CONTEXT_READY || !PA_CONTEXT_IS_GOOD(state))
345 pa_threaded_mainloop_signal(loop, 0);
348 static void stream_state_callback(pa_stream *stream, void *pdata)
350 pa_threaded_mainloop *loop = pdata;
351 pa_stream_state_t state;
353 state = pa_stream_get_state(stream);
354 if(state == PA_STREAM_READY || !PA_STREAM_IS_GOOD(state))
355 pa_threaded_mainloop_signal(loop, 0);
358 static void stream_buffer_attr_callback(pa_stream *stream, void *pdata)
360 ALCdevice *device = pdata;
361 pulse_data *data = device->ExtraData;
363 data->attr = *pa_stream_get_buffer_attr(stream);
364 TRACE("minreq=%d, tlength=%d, prebuf=%d\n", data->attr.minreq, data->attr.tlength, data->attr.prebuf);
367 static void context_state_callback2(pa_context *context, void *pdata)
369 ALCdevice *Device = pdata;
370 pulse_data *data = Device->ExtraData;
372 if(pa_context_get_state(context) == PA_CONTEXT_FAILED)
374 ERR("Received context failure!\n");
375 aluHandleDisconnect(Device);
377 pa_threaded_mainloop_signal(data->loop, 0);
380 static void stream_state_callback2(pa_stream *stream, void *pdata)
382 ALCdevice *Device = pdata;
383 pulse_data *data = Device->ExtraData;
385 if(pa_stream_get_state(stream) == PA_STREAM_FAILED)
387 ERR("Received stream failure!\n");
388 aluHandleDisconnect(Device);
390 pa_threaded_mainloop_signal(data->loop, 0);
393 static void stream_success_callback(pa_stream *stream, int success, void *pdata)
395 ALCdevice *Device = pdata;
396 pulse_data *data = Device->ExtraData;
397 (void)stream;
398 (void)success;
400 pa_threaded_mainloop_signal(data->loop, 0);
403 static void sink_info_callback(pa_context *context, const pa_sink_info *info, int eol, void *pdata)
405 ALCdevice *device = pdata;
406 pulse_data *data = device->ExtraData;
407 char chanmap_str[256] = "";
408 const struct {
409 const char *str;
410 enum DevFmtChannels chans;
411 } chanmaps[] = {
412 { "front-left,front-right,front-center,lfe,rear-left,rear-right,side-left,side-right",
413 DevFmtX71 },
414 { "front-left,front-right,front-center,lfe,rear-center,side-left,side-right",
415 DevFmtX61 },
416 { "front-left,front-right,front-center,lfe,rear-left,rear-right",
417 DevFmtX51 },
418 { "front-left,front-right,front-center,lfe,side-left,side-right",
419 DevFmtX51Side },
420 { "front-left,front-right,rear-left,rear-right", DevFmtQuad },
421 { "front-left,front-right", DevFmtStereo },
422 { "mono", DevFmtMono },
423 { NULL, 0 }
425 int i;
426 (void)context;
428 if(eol)
430 pa_threaded_mainloop_signal(data->loop, 0);
431 return;
434 for(i = 0;chanmaps[i].str;i++)
436 pa_channel_map map;
437 if(!pa_channel_map_parse(&map, chanmaps[i].str))
438 continue;
440 if(pa_channel_map_equal(&info->channel_map, &map)
441 #if PA_CHECK_VERSION(0,9,15)
442 || (pa_channel_map_superset &&
443 pa_channel_map_superset(&info->channel_map, &map))
444 #endif
447 device->FmtChans = chanmaps[i].chans;
448 return;
452 pa_channel_map_snprint(chanmap_str, sizeof(chanmap_str), &info->channel_map);
453 ERR("Failed to find format for channel map:\n %s\n", chanmap_str);
456 static void sink_device_callback(pa_context *context, const pa_sink_info *info, int eol, void *pdata)
458 pa_threaded_mainloop *loop = pdata;
459 void *temp;
460 ALuint i;
462 (void)context;
464 if(eol)
466 pa_threaded_mainloop_signal(loop, 0);
467 return;
470 for(i = 0;i < numDevNames;i++)
472 if(strcmp(info->name, allDevNameMap[i].device_name) == 0)
473 return;
476 TRACE("Got device \"%s\", \"%s\"\n", info->description, info->name);
478 temp = realloc(allDevNameMap, (numDevNames+1) * sizeof(*allDevNameMap));
479 if(temp)
481 allDevNameMap = temp;
482 allDevNameMap[numDevNames].name = strdup(info->description);
483 allDevNameMap[numDevNames].device_name = strdup(info->name);
484 numDevNames++;
488 static void source_device_callback(pa_context *context, const pa_source_info *info, int eol, void *pdata)
490 pa_threaded_mainloop *loop = pdata;
491 void *temp;
492 ALuint i;
494 (void)context;
496 if(eol)
498 pa_threaded_mainloop_signal(loop, 0);
499 return;
502 for(i = 0;i < numCaptureDevNames;i++)
504 if(strcmp(info->name, allCaptureDevNameMap[i].device_name) == 0)
505 return;
508 TRACE("Got device \"%s\", \"%s\"\n", info->description, info->name);
510 temp = realloc(allCaptureDevNameMap, (numCaptureDevNames+1) * sizeof(*allCaptureDevNameMap));
511 if(temp)
513 allCaptureDevNameMap = temp;
514 allCaptureDevNameMap[numCaptureDevNames].name = strdup(info->description);
515 allCaptureDevNameMap[numCaptureDevNames].device_name = strdup(info->name);
516 numCaptureDevNames++;
520 static void sink_name_callback(pa_context *context, const pa_sink_info *info, int eol, void *pdata)
522 ALCdevice *device = pdata;
523 pulse_data *data = device->ExtraData;
524 (void)context;
526 if(eol)
528 pa_threaded_mainloop_signal(data->loop, 0);
529 return;
532 free(device->DeviceName);
533 device->DeviceName = strdup(info->description);
536 static void source_name_callback(pa_context *context, const pa_source_info *info, int eol, void *pdata)
538 ALCdevice *device = pdata;
539 pulse_data *data = device->ExtraData;
540 (void)context;
542 if(eol)
544 pa_threaded_mainloop_signal(data->loop, 0);
545 return;
548 free(device->DeviceName);
549 device->DeviceName = strdup(info->description);
553 static void stream_moved_callback(pa_stream *stream, void *pdata)
555 ALCdevice *device = pdata;
556 pulse_data *data = device->ExtraData;
557 (void)stream;
559 free(data->device_name);
560 data->device_name = strdup(pa_stream_get_device_name(data->stream));
562 TRACE("Stream moved to %s\n", data->device_name);
566 static pa_context *connect_context(pa_threaded_mainloop *loop, ALboolean silent)
568 const char *name = "OpenAL Soft";
569 char path_name[PATH_MAX];
570 pa_context_state_t state;
571 pa_context *context;
572 int err;
574 if(pa_get_binary_name(path_name, sizeof(path_name)))
575 name = pa_path_get_filename(path_name);
577 context = pa_context_new(pa_threaded_mainloop_get_api(loop), name);
578 if(!context)
580 ERR("pa_context_new() failed\n");
581 return NULL;
584 pa_context_set_state_callback(context, context_state_callback, loop);
586 if((err=pa_context_connect(context, NULL, pulse_ctx_flags, NULL)) >= 0)
588 while((state=pa_context_get_state(context)) != PA_CONTEXT_READY)
590 if(!PA_CONTEXT_IS_GOOD(state))
592 err = pa_context_errno(context);
593 if(err > 0) err = -err;
594 break;
597 pa_threaded_mainloop_wait(loop);
600 pa_context_set_state_callback(context, NULL, NULL);
602 if(err < 0)
604 if(!silent)
605 ERR("Context did not connect: %s\n", pa_strerror(err));
606 pa_context_unref(context);
607 return NULL;
610 return context;
613 static pa_stream *connect_playback_stream(const char *device_name,
614 pa_threaded_mainloop *loop, pa_context *context,
615 pa_stream_flags_t flags, pa_buffer_attr *attr, pa_sample_spec *spec,
616 pa_channel_map *chanmap)
618 pa_stream_state_t state;
619 pa_stream *stream;
621 stream = pa_stream_new_with_proplist(context, "Playback Stream", spec, chanmap, prop_filter);
622 if(!stream)
624 ERR("pa_stream_new_with_proplist() failed: %s\n", pa_strerror(pa_context_errno(context)));
625 return NULL;
628 pa_stream_set_state_callback(stream, stream_state_callback, loop);
630 if(pa_stream_connect_playback(stream, device_name, attr, flags, NULL, NULL) < 0)
632 ERR("Stream did not connect: %s\n", pa_strerror(pa_context_errno(context)));
633 pa_stream_unref(stream);
634 return NULL;
637 while((state=pa_stream_get_state(stream)) != PA_STREAM_READY)
639 if(!PA_STREAM_IS_GOOD(state))
641 ERR("Stream did not get ready: %s\n", pa_strerror(pa_context_errno(context)));
642 pa_stream_unref(stream);
643 return NULL;
646 pa_threaded_mainloop_wait(loop);
648 pa_stream_set_state_callback(stream, NULL, NULL);
650 return stream;
653 static pa_stream *connect_record_stream(const char *device_name,
654 pa_threaded_mainloop *loop, pa_context *context,
655 pa_stream_flags_t flags, pa_buffer_attr *attr, pa_sample_spec *spec,
656 pa_channel_map *chanmap)
658 pa_stream_state_t state;
659 pa_stream *stream;
661 stream = pa_stream_new_with_proplist(context, "Capture Stream", spec, chanmap, prop_filter);
662 if(!stream)
664 ERR("pa_stream_new_with_proplist() failed: %s\n", pa_strerror(pa_context_errno(context)));
665 return NULL;
668 pa_stream_set_state_callback(stream, stream_state_callback, loop);
670 if(pa_stream_connect_record(stream, device_name, attr, flags) < 0)
672 ERR("Stream did not connect: %s\n", pa_strerror(pa_context_errno(context)));
673 pa_stream_unref(stream);
674 return NULL;
677 while((state=pa_stream_get_state(stream)) != PA_STREAM_READY)
679 if(!PA_STREAM_IS_GOOD(state))
681 ERR("Stream did not get ready: %s\n", pa_strerror(pa_context_errno(context)));
682 pa_stream_unref(stream);
683 return NULL;
686 pa_threaded_mainloop_wait(loop);
688 pa_stream_set_state_callback(stream, NULL, NULL);
690 return stream;
694 static void wait_for_operation(pa_operation *op, pa_threaded_mainloop *loop)
696 if(op)
698 while(pa_operation_get_state(op) == PA_OPERATION_RUNNING)
699 pa_threaded_mainloop_wait(loop);
700 pa_operation_unref(op);
705 static void probe_devices(ALboolean capture)
707 pa_threaded_mainloop *loop;
709 if(capture == AL_FALSE)
710 allDevNameMap = malloc(sizeof(DevMap) * 1);
711 else
712 allCaptureDevNameMap = malloc(sizeof(DevMap) * 1);
714 if((loop=pa_threaded_mainloop_new()) &&
715 pa_threaded_mainloop_start(loop) >= 0)
717 pa_context *context;
719 pa_threaded_mainloop_lock(loop);
720 context = connect_context(loop, AL_FALSE);
721 if(context)
723 pa_operation *o;
725 if(capture == AL_FALSE)
727 pa_stream_flags_t flags;
728 pa_sample_spec spec;
729 pa_stream *stream;
731 flags = PA_STREAM_FIX_FORMAT | PA_STREAM_FIX_RATE |
732 PA_STREAM_FIX_CHANNELS | PA_STREAM_DONT_MOVE;
734 spec.format = PA_SAMPLE_S16NE;
735 spec.rate = 44100;
736 spec.channels = 2;
738 stream = connect_playback_stream(NULL, loop, context, flags,
739 NULL, &spec, NULL);
740 if(stream)
742 o = pa_context_get_sink_info_by_name(context, pa_stream_get_device_name(stream), sink_device_callback, loop);
743 wait_for_operation(o, loop);
745 pa_stream_disconnect(stream);
746 pa_stream_unref(stream);
747 stream = NULL;
750 o = pa_context_get_sink_info_list(context, sink_device_callback, loop);
752 else
754 pa_stream_flags_t flags;
755 pa_sample_spec spec;
756 pa_stream *stream;
758 flags = PA_STREAM_FIX_FORMAT | PA_STREAM_FIX_RATE |
759 PA_STREAM_FIX_CHANNELS | PA_STREAM_DONT_MOVE;
761 spec.format = PA_SAMPLE_S16NE;
762 spec.rate = 44100;
763 spec.channels = 1;
765 stream = connect_record_stream(NULL, loop, context, flags,
766 NULL, &spec, NULL);
767 if(stream)
769 o = pa_context_get_source_info_by_name(context, pa_stream_get_device_name(stream), source_device_callback, loop);
770 wait_for_operation(o, loop);
772 pa_stream_disconnect(stream);
773 pa_stream_unref(stream);
774 stream = NULL;
777 o = pa_context_get_source_info_list(context, source_device_callback, loop);
779 wait_for_operation(o, loop);
781 pa_context_disconnect(context);
782 pa_context_unref(context);
784 pa_threaded_mainloop_unlock(loop);
785 pa_threaded_mainloop_stop(loop);
787 if(loop)
788 pa_threaded_mainloop_free(loop);
792 static ALuint PulseProc(ALvoid *param)
794 ALCdevice *Device = param;
795 pulse_data *data = Device->ExtraData;
796 ALuint buffer_size;
797 ALint update_size;
798 size_t frame_size;
799 ssize_t len;
801 SetRTPriority();
803 pa_threaded_mainloop_lock(data->loop);
804 frame_size = pa_frame_size(&data->spec);
805 update_size = Device->UpdateSize * frame_size;
807 /* Sanitize buffer metrics, in case we actually have less than what we
808 * asked for. */
809 buffer_size = minu(update_size*Device->NumUpdates, data->attr.tlength);
810 update_size = minu(update_size, buffer_size/2);
811 do {
812 len = pa_stream_writable_size(data->stream) - data->attr.tlength +
813 buffer_size;
814 if(len < update_size)
816 if(pa_stream_is_corked(data->stream) == 1)
818 pa_operation *o;
819 o = pa_stream_cork(data->stream, 0, NULL, NULL);
820 if(o) pa_operation_unref(o);
822 pa_threaded_mainloop_unlock(data->loop);
823 Sleep(1);
824 pa_threaded_mainloop_lock(data->loop);
825 continue;
827 len -= len%update_size;
829 while(len > 0)
831 size_t newlen = len;
832 void *buf;
833 pa_free_cb_t free_func = NULL;
835 #if PA_CHECK_VERSION(0,9,16)
836 if(!pa_stream_begin_write ||
837 pa_stream_begin_write(data->stream, &buf, &newlen) < 0)
838 #endif
840 buf = pa_xmalloc(newlen);
841 free_func = pa_xfree;
844 aluMixData(Device, buf, newlen/frame_size);
846 pa_stream_write(data->stream, buf, newlen, free_func, 0, PA_SEEK_RELATIVE);
847 len -= newlen;
849 } while(!data->killNow && Device->Connected);
850 pa_threaded_mainloop_unlock(data->loop);
852 return 0;
856 static ALCboolean pulse_open(ALCdevice *device)
858 pulse_data *data = pa_xmalloc(sizeof(pulse_data));
859 memset(data, 0, sizeof(*data));
861 if(!(data->loop = pa_threaded_mainloop_new()))
863 ERR("pa_threaded_mainloop_new() failed!\n");
864 goto out;
866 if(pa_threaded_mainloop_start(data->loop) < 0)
868 ERR("pa_threaded_mainloop_start() failed\n");
869 goto out;
872 pa_threaded_mainloop_lock(data->loop);
873 device->ExtraData = data;
875 data->context = connect_context(data->loop, AL_FALSE);
876 if(!data->context)
878 pa_threaded_mainloop_unlock(data->loop);
879 goto out;
881 pa_context_set_state_callback(data->context, context_state_callback2, device);
883 pa_threaded_mainloop_unlock(data->loop);
884 return ALC_TRUE;
886 out:
887 if(data->loop)
889 pa_threaded_mainloop_stop(data->loop);
890 pa_threaded_mainloop_free(data->loop);
893 device->ExtraData = NULL;
894 pa_xfree(data);
895 return ALC_FALSE;
898 static void pulse_close(ALCdevice *device)
900 pulse_data *data = device->ExtraData;
902 pa_threaded_mainloop_lock(data->loop);
904 if(data->stream)
906 pa_stream_set_moved_callback(data->stream, NULL, NULL);
907 #if PA_CHECK_VERSION(0,9,15)
908 if(pa_stream_set_buffer_attr_callback)
909 pa_stream_set_buffer_attr_callback(data->stream, NULL, NULL);
910 #endif
911 pa_stream_disconnect(data->stream);
912 pa_stream_unref(data->stream);
915 pa_context_disconnect(data->context);
916 pa_context_unref(data->context);
918 pa_threaded_mainloop_unlock(data->loop);
920 pa_threaded_mainloop_stop(data->loop);
921 pa_threaded_mainloop_free(data->loop);
923 free(data->device_name);
925 device->ExtraData = NULL;
926 pa_xfree(data);
929 /* OpenAL */
930 static ALCenum pulse_open_playback(ALCdevice *device, const ALCchar *device_name)
932 const char *pulse_name = NULL;
933 pa_stream_flags_t flags;
934 pa_sample_spec spec;
935 pulse_data *data;
936 pa_operation *o;
938 if(device_name)
940 ALuint i;
942 if(!allDevNameMap)
943 probe_devices(AL_FALSE);
945 for(i = 0;i < numDevNames;i++)
947 if(strcmp(device_name, allDevNameMap[i].name) == 0)
949 pulse_name = allDevNameMap[i].device_name;
950 break;
953 if(i == numDevNames)
954 return ALC_INVALID_VALUE;
957 if(pulse_open(device) == ALC_FALSE)
958 return ALC_INVALID_VALUE;
960 data = device->ExtraData;
961 pa_threaded_mainloop_lock(data->loop);
963 flags = PA_STREAM_FIX_FORMAT | PA_STREAM_FIX_RATE |
964 PA_STREAM_FIX_CHANNELS;
965 if(!GetConfigValueBool("pulse", "allow-moves", 0))
966 flags |= PA_STREAM_DONT_MOVE;
968 spec.format = PA_SAMPLE_S16NE;
969 spec.rate = 44100;
970 spec.channels = 2;
972 data->stream = connect_playback_stream(pulse_name, data->loop, data->context,
973 flags, NULL, &spec, NULL);
974 if(!data->stream)
976 pa_threaded_mainloop_unlock(data->loop);
977 pulse_close(device);
978 return ALC_INVALID_VALUE;
981 data->device_name = strdup(pa_stream_get_device_name(data->stream));
982 o = pa_context_get_sink_info_by_name(data->context, data->device_name,
983 sink_name_callback, device);
984 wait_for_operation(o, data->loop);
986 pa_stream_set_moved_callback(data->stream, stream_moved_callback, device);
988 pa_threaded_mainloop_unlock(data->loop);
990 return ALC_NO_ERROR;
993 static void pulse_close_playback(ALCdevice *device)
995 pulse_close(device);
998 static ALCboolean pulse_reset_playback(ALCdevice *device)
1000 pulse_data *data = device->ExtraData;
1001 pa_stream_flags_t flags = 0;
1002 pa_channel_map chanmap;
1004 pa_threaded_mainloop_lock(data->loop);
1006 if(data->stream)
1008 pa_stream_set_moved_callback(data->stream, NULL, NULL);
1009 #if PA_CHECK_VERSION(0,9,15)
1010 if(pa_stream_set_buffer_attr_callback)
1011 pa_stream_set_buffer_attr_callback(data->stream, NULL, NULL);
1012 #endif
1013 pa_stream_disconnect(data->stream);
1014 pa_stream_unref(data->stream);
1015 data->stream = NULL;
1018 if(!(device->Flags&DEVICE_CHANNELS_REQUEST))
1020 pa_operation *o;
1021 o = pa_context_get_sink_info_by_name(data->context, data->device_name, sink_info_callback, device);
1022 wait_for_operation(o, data->loop);
1024 if(!(device->Flags&DEVICE_FREQUENCY_REQUEST))
1025 flags |= PA_STREAM_FIX_RATE;
1027 flags |= PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE;
1028 flags |= PA_STREAM_ADJUST_LATENCY;
1029 flags |= PA_STREAM_START_CORKED;
1030 if(!GetConfigValueBool("pulse", "allow-moves", 0))
1031 flags |= PA_STREAM_DONT_MOVE;
1033 switch(device->FmtType)
1035 case DevFmtByte:
1036 device->FmtType = DevFmtUByte;
1037 /* fall-through */
1038 case DevFmtUByte:
1039 data->spec.format = PA_SAMPLE_U8;
1040 break;
1041 case DevFmtUShort:
1042 device->FmtType = DevFmtShort;
1043 /* fall-through */
1044 case DevFmtShort:
1045 data->spec.format = PA_SAMPLE_S16NE;
1046 break;
1047 case DevFmtUInt:
1048 device->FmtType = DevFmtInt;
1049 /* fall-through */
1050 case DevFmtInt:
1051 data->spec.format = PA_SAMPLE_S32NE;
1052 break;
1053 case DevFmtFloat:
1054 data->spec.format = PA_SAMPLE_FLOAT32NE;
1055 break;
1057 data->spec.rate = device->Frequency;
1058 data->spec.channels = ChannelsFromDevFmt(device->FmtChans);
1060 if(pa_sample_spec_valid(&data->spec) == 0)
1062 ERR("Invalid sample format\n");
1063 pa_threaded_mainloop_unlock(data->loop);
1064 return ALC_FALSE;
1067 if(!pa_channel_map_init_auto(&chanmap, data->spec.channels, PA_CHANNEL_MAP_WAVEEX))
1069 ERR("Couldn't build map for channel count (%d)!\n", data->spec.channels);
1070 pa_threaded_mainloop_unlock(data->loop);
1071 return ALC_FALSE;
1073 SetDefaultWFXChannelOrder(device);
1075 data->attr.fragsize = -1;
1076 data->attr.prebuf = 0;
1077 data->attr.minreq = device->UpdateSize * pa_frame_size(&data->spec);
1078 data->attr.tlength = data->attr.minreq * maxu(device->NumUpdates, 2);
1079 data->attr.maxlength = -1;
1081 data->stream = connect_playback_stream(data->device_name, data->loop,
1082 data->context, flags, &data->attr,
1083 &data->spec, &chanmap);
1084 if(!data->stream)
1086 pa_threaded_mainloop_unlock(data->loop);
1087 return ALC_FALSE;
1089 pa_stream_set_state_callback(data->stream, stream_state_callback2, device);
1091 data->spec = *(pa_stream_get_sample_spec(data->stream));
1092 if(device->Frequency != data->spec.rate)
1094 pa_operation *o;
1096 /* Server updated our playback rate, so modify the buffer attribs
1097 * accordingly. */
1098 data->attr.minreq = (ALuint64)device->UpdateSize * data->spec.rate /
1099 device->Frequency * pa_frame_size(&data->spec);
1100 data->attr.tlength = data->attr.minreq * maxu(device->NumUpdates, 2);
1101 data->attr.prebuf = 0;
1103 o = pa_stream_set_buffer_attr(data->stream, &data->attr,
1104 stream_success_callback, device);
1105 wait_for_operation(o, data->loop);
1107 device->Frequency = data->spec.rate;
1110 pa_stream_set_moved_callback(data->stream, stream_moved_callback, device);
1111 #if PA_CHECK_VERSION(0,9,15)
1112 if(pa_stream_set_buffer_attr_callback)
1113 pa_stream_set_buffer_attr_callback(data->stream, stream_buffer_attr_callback, device);
1114 #endif
1115 stream_buffer_attr_callback(data->stream, device);
1117 device->NumUpdates = device->UpdateSize*device->NumUpdates /
1118 (data->attr.minreq/pa_frame_size(&data->spec));
1119 device->NumUpdates = maxu(device->NumUpdates, 2);
1120 device->UpdateSize = data->attr.minreq / pa_frame_size(&data->spec);
1122 pa_threaded_mainloop_unlock(data->loop);
1123 return ALC_TRUE;
1126 static ALCboolean pulse_start_playback(ALCdevice *device)
1128 pulse_data *data = device->ExtraData;
1130 data->thread = StartThread(PulseProc, device);
1131 if(!data->thread)
1132 return ALC_FALSE;
1134 return ALC_TRUE;
1137 static void pulse_stop_playback(ALCdevice *device)
1139 pulse_data *data = device->ExtraData;
1140 pa_operation *o;
1142 if(!data->stream)
1143 return;
1145 data->killNow = AL_TRUE;
1146 if(data->thread)
1148 StopThread(data->thread);
1149 data->thread = NULL;
1151 data->killNow = AL_FALSE;
1153 pa_threaded_mainloop_lock(data->loop);
1155 o = pa_stream_cork(data->stream, 1, stream_success_callback, device);
1156 wait_for_operation(o, data->loop);
1158 pa_threaded_mainloop_unlock(data->loop);
1162 static ALCenum pulse_open_capture(ALCdevice *device, const ALCchar *device_name)
1164 const char *pulse_name = NULL;
1165 pa_stream_flags_t flags = 0;
1166 pa_channel_map chanmap;
1167 pulse_data *data;
1168 pa_operation *o;
1169 ALuint samples;
1171 if(device_name)
1173 ALuint i;
1175 if(!allCaptureDevNameMap)
1176 probe_devices(AL_TRUE);
1178 for(i = 0;i < numCaptureDevNames;i++)
1180 if(strcmp(device_name, allCaptureDevNameMap[i].name) == 0)
1182 pulse_name = allCaptureDevNameMap[i].device_name;
1183 break;
1186 if(i == numCaptureDevNames)
1187 return ALC_INVALID_VALUE;
1190 if(pulse_open(device) == ALC_FALSE)
1191 return ALC_INVALID_VALUE;
1193 data = device->ExtraData;
1194 pa_threaded_mainloop_lock(data->loop);
1196 data->spec.rate = device->Frequency;
1197 data->spec.channels = ChannelsFromDevFmt(device->FmtChans);
1199 switch(device->FmtType)
1201 case DevFmtUByte:
1202 data->spec.format = PA_SAMPLE_U8;
1203 break;
1204 case DevFmtShort:
1205 data->spec.format = PA_SAMPLE_S16NE;
1206 break;
1207 case DevFmtInt:
1208 data->spec.format = PA_SAMPLE_S32NE;
1209 break;
1210 case DevFmtFloat:
1211 data->spec.format = PA_SAMPLE_FLOAT32NE;
1212 break;
1213 case DevFmtByte:
1214 case DevFmtUShort:
1215 case DevFmtUInt:
1216 ERR("%s capture samples not supported\n", DevFmtTypeString(device->FmtType));
1217 pa_threaded_mainloop_unlock(data->loop);
1218 goto fail;
1221 if(pa_sample_spec_valid(&data->spec) == 0)
1223 ERR("Invalid sample format\n");
1224 pa_threaded_mainloop_unlock(data->loop);
1225 goto fail;
1228 if(!pa_channel_map_init_auto(&chanmap, data->spec.channels, PA_CHANNEL_MAP_WAVEEX))
1230 ERR("Couldn't build map for channel count (%d)!\n", data->spec.channels);
1231 pa_threaded_mainloop_unlock(data->loop);
1232 goto fail;
1235 samples = device->UpdateSize * device->NumUpdates;
1236 samples = maxu(samples, 100 * device->Frequency / 1000);
1238 data->attr.minreq = -1;
1239 data->attr.prebuf = -1;
1240 data->attr.maxlength = samples * pa_frame_size(&data->spec);
1241 data->attr.tlength = -1;
1242 data->attr.fragsize = minu(samples, 50*device->Frequency/1000) *
1243 pa_frame_size(&data->spec);
1245 flags |= PA_STREAM_START_CORKED|PA_STREAM_ADJUST_LATENCY;
1246 if(!GetConfigValueBool("pulse", "allow-moves", 0))
1247 flags |= PA_STREAM_DONT_MOVE;
1249 data->stream = connect_record_stream(pulse_name, data->loop, data->context,
1250 flags, &data->attr, &data->spec,
1251 &chanmap);
1252 if(!data->stream)
1254 pa_threaded_mainloop_unlock(data->loop);
1255 goto fail;
1257 pa_stream_set_state_callback(data->stream, stream_state_callback2, device);
1259 data->device_name = strdup(pa_stream_get_device_name(data->stream));
1260 o = pa_context_get_source_info_by_name(data->context, data->device_name,
1261 source_name_callback, device);
1262 wait_for_operation(o, data->loop);
1264 pa_stream_set_moved_callback(data->stream, stream_moved_callback, device);
1266 pa_threaded_mainloop_unlock(data->loop);
1267 return ALC_NO_ERROR;
1269 fail:
1270 pulse_close(device);
1271 return ALC_INVALID_VALUE;
1274 static void pulse_close_capture(ALCdevice *device)
1276 pulse_close(device);
1279 static void pulse_start_capture(ALCdevice *device)
1281 pulse_data *data = device->ExtraData;
1282 pa_operation *o;
1284 pa_threaded_mainloop_lock(data->loop);
1285 o = pa_stream_cork(data->stream, 0, stream_success_callback, device);
1286 wait_for_operation(o, data->loop);
1287 pa_threaded_mainloop_unlock(data->loop);
1290 static void pulse_stop_capture(ALCdevice *device)
1292 pulse_data *data = device->ExtraData;
1293 pa_operation *o;
1295 pa_threaded_mainloop_lock(data->loop);
1296 o = pa_stream_cork(data->stream, 1, stream_success_callback, device);
1297 wait_for_operation(o, data->loop);
1298 pa_threaded_mainloop_unlock(data->loop);
1301 static ALCenum pulse_capture_samples(ALCdevice *device, ALCvoid *buffer, ALCuint samples)
1303 pulse_data *data = device->ExtraData;
1304 ALCuint todo = samples * pa_frame_size(&data->spec);
1306 pa_threaded_mainloop_lock(data->loop);
1307 /* Capture is done in fragment-sized chunks, so we loop until we get all
1308 * that's available */
1309 data->last_readable -= todo;
1310 while(todo > 0)
1312 size_t rem = todo;
1314 if(data->cap_len == 0)
1316 pa_stream_state_t state;
1318 state = pa_stream_get_state(data->stream);
1319 if(!PA_STREAM_IS_GOOD(state))
1321 aluHandleDisconnect(device);
1322 break;
1324 if(pa_stream_peek(data->stream, &data->cap_store, &data->cap_len) < 0)
1326 ERR("pa_stream_peek() failed: %s\n",
1327 pa_strerror(pa_context_errno(data->context)));
1328 aluHandleDisconnect(device);
1329 break;
1331 data->cap_remain = data->cap_len;
1333 if(rem > data->cap_remain)
1334 rem = data->cap_remain;
1336 memcpy(buffer, data->cap_store, rem);
1338 buffer = (ALbyte*)buffer + rem;
1339 todo -= rem;
1341 data->cap_store = (ALbyte*)data->cap_store + rem;
1342 data->cap_remain -= rem;
1343 if(data->cap_remain == 0)
1345 pa_stream_drop(data->stream);
1346 data->cap_len = 0;
1349 if(todo > 0)
1350 memset(buffer, ((device->FmtType==DevFmtUByte) ? 0x80 : 0), todo);
1351 pa_threaded_mainloop_unlock(data->loop);
1353 return ALC_NO_ERROR;
1356 static ALCuint pulse_available_samples(ALCdevice *device)
1358 pulse_data *data = device->ExtraData;
1359 size_t readable = data->cap_remain;
1361 pa_threaded_mainloop_lock(data->loop);
1362 if(device->Connected)
1364 ssize_t got = pa_stream_readable_size(data->stream);
1365 if(got < 0)
1367 ERR("pa_stream_readable_size() failed: %s\n", pa_strerror(got));
1368 aluHandleDisconnect(device);
1370 else if((size_t)got > data->cap_len)
1371 readable += got - data->cap_len;
1373 pa_threaded_mainloop_unlock(data->loop);
1375 if(data->last_readable < readable)
1376 data->last_readable = readable;
1377 return data->last_readable / pa_frame_size(&data->spec);
1381 static ALint64 pulse_get_latency(ALCdevice *device)
1383 pulse_data *data = device->ExtraData;
1384 pa_usec_t latency = 0;
1385 int neg;
1387 if(pa_stream_get_latency(data->stream, &latency, &neg) == 0)
1389 if(neg)
1390 latency = 0;
1391 return (ALint64)minu64(latency, (((ALuint64)0x7fffffff << 32)|0xffffffff)/1000) * 1000;
1393 ERR("Failed to get stream latency!\n");
1394 return 0;
1398 static const BackendFuncs pulse_funcs = {
1399 pulse_open_playback,
1400 pulse_close_playback,
1401 pulse_reset_playback,
1402 pulse_start_playback,
1403 pulse_stop_playback,
1404 pulse_open_capture,
1405 pulse_close_capture,
1406 pulse_start_capture,
1407 pulse_stop_capture,
1408 pulse_capture_samples,
1409 pulse_available_samples,
1410 ALCdevice_LockDefault,
1411 ALCdevice_UnlockDefault,
1412 pulse_get_latency
1415 ALCboolean alc_pulse_init(BackendFuncs *func_list)
1417 ALCboolean ret = ALC_FALSE;
1419 if(pulse_load())
1421 pa_threaded_mainloop *loop;
1423 pulse_ctx_flags = 0;
1424 if(!GetConfigValueBool("pulse", "spawn-server", 1))
1425 pulse_ctx_flags |= PA_CONTEXT_NOAUTOSPAWN;
1427 if((loop=pa_threaded_mainloop_new()) &&
1428 pa_threaded_mainloop_start(loop) >= 0)
1430 pa_context *context;
1432 pa_threaded_mainloop_lock(loop);
1433 context = connect_context(loop, AL_TRUE);
1434 if(context)
1436 *func_list = pulse_funcs;
1437 ret = ALC_TRUE;
1439 /* Some libraries (Phonon, Qt) set some pulseaudio properties
1440 * through environment variables, which causes all streams in
1441 * the process to inherit them. This attempts to filter those
1442 * properties out by setting them to 0-length data. */
1443 prop_filter = pa_proplist_new();
1444 pa_proplist_set(prop_filter, PA_PROP_MEDIA_ROLE, NULL, 0);
1445 pa_proplist_set(prop_filter, "phonon.streamid", NULL, 0);
1447 pa_context_disconnect(context);
1448 pa_context_unref(context);
1450 pa_threaded_mainloop_unlock(loop);
1451 pa_threaded_mainloop_stop(loop);
1453 if(loop)
1454 pa_threaded_mainloop_free(loop);
1457 return ret;
1460 void alc_pulse_deinit(void)
1462 ALuint i;
1464 for(i = 0;i < numDevNames;++i)
1466 free(allDevNameMap[i].name);
1467 free(allDevNameMap[i].device_name);
1469 free(allDevNameMap);
1470 allDevNameMap = NULL;
1471 numDevNames = 0;
1473 for(i = 0;i < numCaptureDevNames;++i)
1475 free(allCaptureDevNameMap[i].name);
1476 free(allCaptureDevNameMap[i].device_name);
1478 free(allCaptureDevNameMap);
1479 allCaptureDevNameMap = NULL;
1480 numCaptureDevNames = 0;
1482 if(prop_filter)
1483 pa_proplist_free(prop_filter);
1484 prop_filter = NULL;
1486 /* PulseAudio doesn't like being CloseLib'd sometimes */
1489 void alc_pulse_probe(enum DevProbe type)
1491 ALuint i;
1493 switch(type)
1495 case ALL_DEVICE_PROBE:
1496 for(i = 0;i < numDevNames;++i)
1498 free(allDevNameMap[i].name);
1499 free(allDevNameMap[i].device_name);
1501 free(allDevNameMap);
1502 allDevNameMap = NULL;
1503 numDevNames = 0;
1505 probe_devices(AL_FALSE);
1507 for(i = 0;i < numDevNames;i++)
1508 AppendAllDevicesList(allDevNameMap[i].name);
1509 break;
1511 case CAPTURE_DEVICE_PROBE:
1512 for(i = 0;i < numCaptureDevNames;++i)
1514 free(allCaptureDevNameMap[i].name);
1515 free(allCaptureDevNameMap[i].device_name);
1517 free(allCaptureDevNameMap);
1518 allCaptureDevNameMap = NULL;
1519 numCaptureDevNames = 0;
1521 probe_devices(AL_TRUE);
1523 for(i = 0;i < numCaptureDevNames;i++)
1524 AppendCaptureDeviceList(allCaptureDevNameMap[i].name);
1525 break;
1529 #else
1531 #warning "Unsupported API version, backend will be unavailable!"
1533 ALCboolean alc_pulse_init(BackendFuncs *func_list)
1534 { return ALC_FALSE; (void)func_list; }
1536 void alc_pulse_deinit(void)
1539 void alc_pulse_probe(enum DevProbe type)
1540 { (void)type; }
1542 #endif