Set mixer sample pos and frac when needed
[openal-soft.git] / Alc / pulseaudio.c
blob46ac446a5cd29ef1b29e411b6f5eb0ab3c168abc
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 "alMain.h"
25 #ifdef HAVE_DLFCN_H
26 #include <dlfcn.h>
27 #endif
29 #include <pulse/pulseaudio.h>
31 #if PA_API_VERSION == 11
32 #define PA_STREAM_ADJUST_LATENCY 0x2000U
33 static __inline int PA_STREAM_IS_GOOD(pa_stream_state_t x)
35 return (x == PA_STREAM_CREATING || x == PA_STREAM_READY);
37 static __inline int PA_CONTEXT_IS_GOOD(pa_context_state_t x)
39 return (x == PA_CONTEXT_CONNECTING || x == PA_CONTEXT_AUTHORIZING ||
40 x == PA_CONTEXT_SETTING_NAME || x == PA_CONTEXT_READY);
42 #define PA_STREAM_IS_GOOD PA_STREAM_IS_GOOD
43 #define PA_CONTEXT_IS_GOOD PA_CONTEXT_IS_GOOD
44 #elif PA_API_VERSION != 12
45 #error Invalid PulseAudio API version
46 #endif
48 #ifndef PA_CHECK_VERSION
49 #define PA_CHECK_VERSION(major,minor,micro) \
50 ((PA_MAJOR > (major)) || \
51 (PA_MAJOR == (major) && PA_MINOR > (minor)) || \
52 (PA_MAJOR == (major) && PA_MINOR == (minor) && PA_MICRO >= (micro)))
53 #endif
55 static void *pa_handle;
56 #define MAKE_FUNC(x) static typeof(x) * p##x
57 MAKE_FUNC(pa_context_unref);
58 MAKE_FUNC(pa_sample_spec_valid);
59 MAKE_FUNC(pa_stream_drop);
60 MAKE_FUNC(pa_strerror);
61 MAKE_FUNC(pa_context_get_state);
62 MAKE_FUNC(pa_stream_get_state);
63 MAKE_FUNC(pa_threaded_mainloop_signal);
64 MAKE_FUNC(pa_stream_peek);
65 MAKE_FUNC(pa_threaded_mainloop_wait);
66 MAKE_FUNC(pa_threaded_mainloop_unlock);
67 MAKE_FUNC(pa_threaded_mainloop_in_thread);
68 MAKE_FUNC(pa_context_new);
69 MAKE_FUNC(pa_threaded_mainloop_stop);
70 MAKE_FUNC(pa_context_disconnect);
71 MAKE_FUNC(pa_threaded_mainloop_start);
72 MAKE_FUNC(pa_threaded_mainloop_get_api);
73 MAKE_FUNC(pa_context_set_state_callback);
74 MAKE_FUNC(pa_stream_write);
75 MAKE_FUNC(pa_xfree);
76 MAKE_FUNC(pa_stream_connect_record);
77 MAKE_FUNC(pa_stream_connect_playback);
78 MAKE_FUNC(pa_stream_readable_size);
79 MAKE_FUNC(pa_stream_writable_size);
80 MAKE_FUNC(pa_stream_cork);
81 MAKE_FUNC(pa_stream_is_suspended);
82 MAKE_FUNC(pa_stream_get_device_name);
83 MAKE_FUNC(pa_path_get_filename);
84 MAKE_FUNC(pa_get_binary_name);
85 MAKE_FUNC(pa_threaded_mainloop_free);
86 MAKE_FUNC(pa_context_errno);
87 MAKE_FUNC(pa_xmalloc);
88 MAKE_FUNC(pa_stream_unref);
89 MAKE_FUNC(pa_threaded_mainloop_accept);
90 MAKE_FUNC(pa_stream_set_write_callback);
91 MAKE_FUNC(pa_threaded_mainloop_new);
92 MAKE_FUNC(pa_context_connect);
93 MAKE_FUNC(pa_stream_set_buffer_attr);
94 MAKE_FUNC(pa_stream_get_buffer_attr);
95 MAKE_FUNC(pa_stream_get_sample_spec);
96 MAKE_FUNC(pa_stream_get_time);
97 MAKE_FUNC(pa_stream_set_read_callback);
98 MAKE_FUNC(pa_stream_set_state_callback);
99 MAKE_FUNC(pa_stream_set_moved_callback);
100 MAKE_FUNC(pa_stream_set_underflow_callback);
101 MAKE_FUNC(pa_stream_new);
102 MAKE_FUNC(pa_stream_disconnect);
103 MAKE_FUNC(pa_threaded_mainloop_lock);
104 MAKE_FUNC(pa_channel_map_init_auto);
105 MAKE_FUNC(pa_channel_map_parse);
106 MAKE_FUNC(pa_channel_map_snprint);
107 MAKE_FUNC(pa_channel_map_equal);
108 MAKE_FUNC(pa_context_get_server_info);
109 MAKE_FUNC(pa_context_get_sink_info_by_name);
110 MAKE_FUNC(pa_context_get_sink_info_list);
111 MAKE_FUNC(pa_context_get_source_info_list);
112 MAKE_FUNC(pa_operation_get_state);
113 MAKE_FUNC(pa_operation_unref);
114 #if PA_CHECK_VERSION(0,9,15)
115 MAKE_FUNC(pa_channel_map_superset);
116 MAKE_FUNC(pa_stream_set_buffer_attr_callback);
117 #endif
118 #if PA_CHECK_VERSION(0,9,16)
119 MAKE_FUNC(pa_stream_begin_write);
120 #endif
121 #undef MAKE_FUNC
123 #ifndef PATH_MAX
124 #define PATH_MAX 4096
125 #endif
127 typedef struct {
128 char *device_name;
130 ALCuint samples;
131 ALCuint frame_size;
133 RingBuffer *ring;
135 pa_buffer_attr attr;
136 pa_sample_spec spec;
138 pa_threaded_mainloop *loop;
140 ALvoid *thread;
141 volatile ALboolean killNow;
143 pa_stream *stream;
144 pa_context *context;
145 } pulse_data;
147 typedef struct {
148 char *name;
149 char *device_name;
150 } DevMap;
153 static const ALCchar pulse_device[] = "PulseAudio Default";
154 static DevMap *allDevNameMap;
155 static ALuint numDevNames;
156 static DevMap *allCaptureDevNameMap;
157 static ALuint numCaptureDevNames;
158 static pa_context_flags_t pulse_ctx_flags;
161 void *pulse_load(void) //{{{
163 if(!pa_handle)
165 #ifdef _WIN32
166 pa_handle = LoadLibrary("libpulse-0.dll");
167 #define LOAD_FUNC(x) do { \
168 p##x = (typeof(p##x))GetProcAddress(pa_handle, #x); \
169 if(!(p##x)) { \
170 AL_PRINT("Could not load %s from libpulse-0.dll\n", #x); \
171 FreeLibrary(pa_handle); \
172 pa_handle = NULL; \
173 return NULL; \
175 } while(0)
176 #define LOAD_OPTIONAL_FUNC(x) do { \
177 p##x = (typeof(p##x))GetProcAddress(pa_handle, #x); \
178 } while(0)
180 #elif defined (HAVE_DLFCN_H)
182 const char *err;
183 #if defined(__APPLE__) && defined(__MACH__)
184 pa_handle = dlopen("libpulse.0.dylib", RTLD_NOW);
185 #else
186 pa_handle = dlopen("libpulse.so.0", RTLD_NOW);
187 #endif
188 dlerror();
190 #define LOAD_FUNC(x) do { \
191 p##x = dlsym(pa_handle, #x); \
192 if((err=dlerror()) != NULL) { \
193 AL_PRINT("Could not load %s from libpulse: %s\n", #x, err); \
194 dlclose(pa_handle); \
195 pa_handle = NULL; \
196 return NULL; \
198 } while(0)
199 #define LOAD_OPTIONAL_FUNC(x) do { \
200 p##x = dlsym(pa_handle, #x); \
201 if((err=dlerror()) != NULL) { \
202 p##x = NULL; \
204 } while(0)
206 #else
208 pa_handle = (void*)0xDEADBEEF;
209 #define LOAD_FUNC(x) p##x = (x)
210 #define LOAD_OPTIONAL_FUNC(x) p##x = (x)
212 #endif
213 if(!pa_handle)
214 return NULL;
216 LOAD_FUNC(pa_context_unref);
217 LOAD_FUNC(pa_sample_spec_valid);
218 LOAD_FUNC(pa_stream_drop);
219 LOAD_FUNC(pa_strerror);
220 LOAD_FUNC(pa_context_get_state);
221 LOAD_FUNC(pa_stream_get_state);
222 LOAD_FUNC(pa_threaded_mainloop_signal);
223 LOAD_FUNC(pa_stream_peek);
224 LOAD_FUNC(pa_threaded_mainloop_wait);
225 LOAD_FUNC(pa_threaded_mainloop_unlock);
226 LOAD_FUNC(pa_threaded_mainloop_in_thread);
227 LOAD_FUNC(pa_context_new);
228 LOAD_FUNC(pa_threaded_mainloop_stop);
229 LOAD_FUNC(pa_context_disconnect);
230 LOAD_FUNC(pa_threaded_mainloop_start);
231 LOAD_FUNC(pa_threaded_mainloop_get_api);
232 LOAD_FUNC(pa_context_set_state_callback);
233 LOAD_FUNC(pa_stream_write);
234 LOAD_FUNC(pa_xfree);
235 LOAD_FUNC(pa_stream_connect_record);
236 LOAD_FUNC(pa_stream_connect_playback);
237 LOAD_FUNC(pa_stream_readable_size);
238 LOAD_FUNC(pa_stream_writable_size);
239 LOAD_FUNC(pa_stream_cork);
240 LOAD_FUNC(pa_stream_is_suspended);
241 LOAD_FUNC(pa_stream_get_device_name);
242 LOAD_FUNC(pa_path_get_filename);
243 LOAD_FUNC(pa_get_binary_name);
244 LOAD_FUNC(pa_threaded_mainloop_free);
245 LOAD_FUNC(pa_context_errno);
246 LOAD_FUNC(pa_xmalloc);
247 LOAD_FUNC(pa_stream_unref);
248 LOAD_FUNC(pa_threaded_mainloop_accept);
249 LOAD_FUNC(pa_stream_set_write_callback);
250 LOAD_FUNC(pa_threaded_mainloop_new);
251 LOAD_FUNC(pa_context_connect);
252 LOAD_FUNC(pa_stream_set_buffer_attr);
253 LOAD_FUNC(pa_stream_get_buffer_attr);
254 LOAD_FUNC(pa_stream_get_sample_spec);
255 LOAD_FUNC(pa_stream_get_time);
256 LOAD_FUNC(pa_stream_set_read_callback);
257 LOAD_FUNC(pa_stream_set_state_callback);
258 LOAD_FUNC(pa_stream_set_moved_callback);
259 LOAD_FUNC(pa_stream_set_underflow_callback);
260 LOAD_FUNC(pa_stream_new);
261 LOAD_FUNC(pa_stream_disconnect);
262 LOAD_FUNC(pa_threaded_mainloop_lock);
263 LOAD_FUNC(pa_channel_map_init_auto);
264 LOAD_FUNC(pa_channel_map_parse);
265 LOAD_FUNC(pa_channel_map_snprint);
266 LOAD_FUNC(pa_channel_map_equal);
267 LOAD_FUNC(pa_context_get_server_info);
268 LOAD_FUNC(pa_context_get_sink_info_by_name);
269 LOAD_FUNC(pa_context_get_sink_info_list);
270 LOAD_FUNC(pa_context_get_source_info_list);
271 LOAD_FUNC(pa_operation_get_state);
272 LOAD_FUNC(pa_operation_unref);
273 #if PA_CHECK_VERSION(0,9,15)
274 LOAD_OPTIONAL_FUNC(pa_channel_map_superset);
275 LOAD_OPTIONAL_FUNC(pa_stream_set_buffer_attr_callback);
276 #endif
277 #if PA_CHECK_VERSION(0,9,16)
278 LOAD_OPTIONAL_FUNC(pa_stream_begin_write);
279 #endif
281 #undef LOAD_OPTIONAL_FUNC
282 #undef LOAD_FUNC
284 return pa_handle;
285 } //}}}
287 // PulseAudio Event Callbacks //{{{
288 static void context_state_callback(pa_context *context, void *pdata) //{{{
290 pa_threaded_mainloop *loop = pdata;
291 pa_context_state_t state;
293 state = ppa_context_get_state(context);
294 if(state == PA_CONTEXT_READY || !PA_CONTEXT_IS_GOOD(state))
295 ppa_threaded_mainloop_signal(loop, 0);
296 }//}}}
298 static void stream_state_callback(pa_stream *stream, void *pdata) //{{{
300 pa_threaded_mainloop *loop = pdata;
301 pa_stream_state_t state;
303 state = ppa_stream_get_state(stream);
304 if(state == PA_STREAM_READY || !PA_STREAM_IS_GOOD(state))
305 ppa_threaded_mainloop_signal(loop, 0);
306 }//}}}
308 static void stream_signal_callback(pa_stream *stream, void *pdata) //{{{
310 ALCdevice *Device = pdata;
311 pulse_data *data = Device->ExtraData;
312 (void)stream;
314 ppa_threaded_mainloop_signal(data->loop, 0);
315 }//}}}
317 static void stream_buffer_attr_callback(pa_stream *stream, void *pdata) //{{{
319 ALCdevice *Device = pdata;
320 pulse_data *data = Device->ExtraData;
322 SuspendContext(NULL);
324 data->attr = *(ppa_stream_get_buffer_attr(stream));
325 Device->UpdateSize = data->attr.minreq / data->frame_size;
326 Device->NumUpdates = (data->attr.tlength/data->frame_size) / Device->UpdateSize;
327 if(Device->NumUpdates <= 1)
329 Device->NumUpdates = 1;
330 AL_PRINT("PulseAudio returned minreq > tlength/2; expect break up\n");
333 ProcessContext(NULL);
334 }//}}}
336 static void stream_device_callback(pa_stream *stream, void *pdata) //{{{
338 ALCdevice *Device = pdata;
339 pulse_data *data = Device->ExtraData;
341 free(data->device_name);
342 data->device_name = strdup(ppa_stream_get_device_name(stream));
343 }//}}}
345 static void context_state_callback2(pa_context *context, void *pdata) //{{{
347 ALCdevice *Device = pdata;
348 pulse_data *data = Device->ExtraData;
350 if(ppa_context_get_state(context) == PA_CONTEXT_FAILED)
352 AL_PRINT("Received context failure!\n");
353 aluHandleDisconnect(Device);
355 ppa_threaded_mainloop_signal(data->loop, 0);
356 }//}}}
358 static void stream_state_callback2(pa_stream *stream, void *pdata) //{{{
360 ALCdevice *Device = pdata;
361 pulse_data *data = Device->ExtraData;
363 if(ppa_stream_get_state(stream) == PA_STREAM_FAILED)
365 AL_PRINT("Received stream failure!\n");
366 aluHandleDisconnect(Device);
368 ppa_threaded_mainloop_signal(data->loop, 0);
369 }//}}}
371 static void stream_success_callback(pa_stream *stream, int success, void *pdata) //{{{
373 ALCdevice *Device = pdata;
374 pulse_data *data = Device->ExtraData;
375 (void)stream;
376 (void)success;
378 ppa_threaded_mainloop_signal(data->loop, 0);
379 }//}}}
381 static void sink_info_callback(pa_context *context, const pa_sink_info *info, int eol, void *pdata) //{{{
383 ALCdevice *device = pdata;
384 pulse_data *data = device->ExtraData;
385 char chanmap_str[256] = "";
386 const struct {
387 const char *str;
388 ALenum format;
389 } chanmaps[] = {
390 { "front-left,front-right,front-center,lfe,rear-left,rear-right,side-left,side-right",
391 AL_FORMAT_71CHN32 },
392 { "front-left,front-right,front-center,lfe,rear-center,side-left,side-right",
393 AL_FORMAT_61CHN32 },
394 { "front-left,front-right,front-center,lfe,rear-left,rear-right",
395 AL_FORMAT_51CHN32 },
396 { "front-left,front-right,rear-left,rear-right", AL_FORMAT_QUAD32 },
397 { "front-left,front-right", AL_FORMAT_STEREO_FLOAT32 },
398 { "mono", AL_FORMAT_MONO_FLOAT32 },
399 { NULL, 0 }
401 int i;
402 (void)context;
404 if(eol)
406 ppa_threaded_mainloop_signal(data->loop, 0);
407 return;
410 for(i = 0;chanmaps[i].str;i++)
412 pa_channel_map map;
413 if(!ppa_channel_map_parse(&map, chanmaps[i].str))
414 continue;
416 if(ppa_channel_map_equal(&info->channel_map, &map)
417 #if PA_CHECK_VERSION(0,9,15)
418 || (ppa_channel_map_superset &&
419 ppa_channel_map_superset(&info->channel_map, &map))
420 #endif
423 device->Format = chanmaps[i].format;
424 return;
428 ppa_channel_map_snprint(chanmap_str, sizeof(chanmap_str), &info->channel_map);
429 AL_PRINT("Failed to find format for channel map:\n %s\n", chanmap_str);
430 }//}}}
432 static void sink_device_callback(pa_context *context, const pa_sink_info *info, int eol, void *pdata) //{{{
434 pa_threaded_mainloop *loop = pdata;
435 char str[1024];
436 void *temp;
437 int count;
438 ALuint i;
440 (void)context;
442 if(eol)
444 ppa_threaded_mainloop_signal(loop, 0);
445 return;
448 count = 0;
449 do {
450 if(count == 0)
451 snprintf(str, sizeof(str), "%s via PulseAudio", info->description);
452 else
453 snprintf(str, sizeof(str), "%s #%d via PulseAudio", info->description, count+1);
454 count++;
456 for(i = 0;i < numDevNames;i++)
458 if(strcmp(str, allDevNameMap[i].name) == 0)
459 break;
461 } while(i != numDevNames);
463 temp = realloc(allDevNameMap, (numDevNames+1) * sizeof(*allDevNameMap));
464 if(temp)
466 allDevNameMap = temp;
467 allDevNameMap[numDevNames].name = strdup(str);
468 allDevNameMap[numDevNames].device_name = strdup(info->name);
469 numDevNames++;
471 }//}}}
473 static void source_device_callback(pa_context *context, const pa_source_info *info, int eol, void *pdata) //{{{
475 pa_threaded_mainloop *loop = pdata;
476 char str[1024];
477 void *temp;
478 int count;
479 ALuint i;
481 (void)context;
483 if(eol)
485 ppa_threaded_mainloop_signal(loop, 0);
486 return;
489 count = 0;
490 do {
491 if(count == 0)
492 snprintf(str, sizeof(str), "%s via PulseAudio", info->description);
493 else
494 snprintf(str, sizeof(str), "%s #%d via PulseAudio", info->description, count+1);
495 count++;
497 for(i = 0;i < numCaptureDevNames;i++)
499 if(strcmp(str, allCaptureDevNameMap[i].name) == 0)
500 break;
502 } while(i != numCaptureDevNames);
504 temp = realloc(allCaptureDevNameMap, (numCaptureDevNames+1) * sizeof(*allCaptureDevNameMap));
505 if(temp)
507 allCaptureDevNameMap = temp;
508 allCaptureDevNameMap[numCaptureDevNames].name = strdup(str);
509 allCaptureDevNameMap[numCaptureDevNames].device_name = strdup(info->name);
510 numCaptureDevNames++;
512 }//}}}
513 //}}}
515 // PulseAudio I/O Callbacks //{{{
516 static void stream_write_callback(pa_stream *stream, size_t len, void *pdata) //{{{
518 ALCdevice *Device = pdata;
519 pulse_data *data = Device->ExtraData;
520 (void)stream;
521 (void)len;
523 ppa_threaded_mainloop_signal(data->loop, 0);
524 } //}}}
525 //}}}
527 static ALuint PulseProc(ALvoid *param)
529 ALCdevice *Device = param;
530 pulse_data *data = Device->ExtraData;
531 ssize_t len;
533 SetRTPriority();
535 ppa_threaded_mainloop_lock(data->loop);
536 do {
537 len = (Device->Connected ? ppa_stream_writable_size(data->stream) : 0);
538 len -= len%(Device->UpdateSize*data->frame_size);
539 if(len == 0)
541 ppa_threaded_mainloop_wait(data->loop);
542 continue;
545 while(len > 0)
547 size_t newlen = len;
548 void *buf;
549 pa_free_cb_t free_func = NULL;
551 #if PA_CHECK_VERSION(0,9,16)
552 if(!ppa_stream_begin_write ||
553 ppa_stream_begin_write(data->stream, &buf, &newlen) < 0)
554 #endif
556 buf = ppa_xmalloc(newlen);
557 free_func = ppa_xfree;
559 ppa_threaded_mainloop_unlock(data->loop);
561 aluMixData(Device, buf, newlen/data->frame_size);
563 ppa_threaded_mainloop_lock(data->loop);
564 ppa_stream_write(data->stream, buf, newlen, free_func, 0, PA_SEEK_RELATIVE);
565 len -= newlen;
567 } while(Device->Connected && !data->killNow);
568 ppa_threaded_mainloop_unlock(data->loop);
570 return 0;
573 static pa_context *connect_context(pa_threaded_mainloop *loop)
575 const char *name = "OpenAL Soft";
576 char path_name[PATH_MAX];
577 pa_context_state_t state;
578 pa_context *context;
579 int err;
581 if(ppa_get_binary_name(path_name, sizeof(path_name)))
582 name = ppa_path_get_filename(path_name);
584 context = ppa_context_new(ppa_threaded_mainloop_get_api(loop), name);
585 if(!context)
587 AL_PRINT("pa_context_new() failed\n");
588 return NULL;
591 ppa_context_set_state_callback(context, context_state_callback, loop);
593 if((err=ppa_context_connect(context, NULL, pulse_ctx_flags, NULL)) >= 0)
595 while((state=ppa_context_get_state(context)) != PA_CONTEXT_READY)
597 if(!PA_CONTEXT_IS_GOOD(state))
599 err = ppa_context_errno(context);
600 if(err > 0) err = -err;
601 break;
604 ppa_threaded_mainloop_wait(loop);
607 ppa_context_set_state_callback(context, NULL, NULL);
609 if(err < 0)
611 AL_PRINT("Context did not connect: %s\n", ppa_strerror(err));
612 ppa_context_unref(context);
613 return NULL;
616 return context;
619 static pa_stream *connect_playback_stream(ALCdevice *device,
620 pa_stream_flags_t flags, pa_buffer_attr *attr, pa_sample_spec *spec,
621 pa_channel_map *chanmap)
623 pulse_data *data = device->ExtraData;
624 pa_stream_state_t state;
625 pa_stream *stream;
627 stream = ppa_stream_new(data->context, "Playback Stream", spec, chanmap);
628 if(!stream)
630 AL_PRINT("pa_stream_new() failed: %s\n",
631 ppa_strerror(ppa_context_errno(data->context)));
632 return NULL;
635 ppa_stream_set_state_callback(stream, stream_state_callback, data->loop);
637 if(ppa_stream_connect_playback(stream, data->device_name, attr, flags, NULL, NULL) < 0)
639 AL_PRINT("Stream did not connect: %s\n",
640 ppa_strerror(ppa_context_errno(data->context)));
641 ppa_stream_unref(stream);
642 return NULL;
645 while((state=ppa_stream_get_state(stream)) != PA_STREAM_READY)
647 if(!PA_STREAM_IS_GOOD(state))
649 AL_PRINT("Stream did not get ready: %s\n",
650 ppa_strerror(ppa_context_errno(data->context)));
651 ppa_stream_unref(stream);
652 return NULL;
655 ppa_threaded_mainloop_wait(data->loop);
657 ppa_stream_set_state_callback(stream, NULL, NULL);
659 return stream;
662 static void probe_devices(ALboolean capture)
664 pa_threaded_mainloop *loop;
666 if(capture == AL_FALSE)
667 allDevNameMap = malloc(sizeof(DevMap) * 1);
668 else
669 allCaptureDevNameMap = malloc(sizeof(DevMap) * 1);
671 if((loop=ppa_threaded_mainloop_new()) &&
672 ppa_threaded_mainloop_start(loop) >= 0)
674 pa_context *context;
676 ppa_threaded_mainloop_lock(loop);
677 context = connect_context(loop);
678 if(context)
680 pa_operation *o;
682 if(capture == AL_FALSE)
684 allDevNameMap[0].name = strdup(pulse_device);
685 allDevNameMap[0].device_name = NULL;
686 numDevNames = 1;
688 o = ppa_context_get_sink_info_list(context, sink_device_callback, loop);
690 else
692 allCaptureDevNameMap[0].name = strdup(pulse_device);
693 allCaptureDevNameMap[0].device_name = NULL;
694 numCaptureDevNames = 1;
696 o = ppa_context_get_source_info_list(context, source_device_callback, loop);
698 while(ppa_operation_get_state(o) == PA_OPERATION_RUNNING)
699 ppa_threaded_mainloop_wait(loop);
700 ppa_operation_unref(o);
702 ppa_context_disconnect(context);
703 ppa_context_unref(context);
705 ppa_threaded_mainloop_unlock(loop);
706 ppa_threaded_mainloop_stop(loop);
708 if(loop)
709 ppa_threaded_mainloop_free(loop);
713 static ALCboolean pulse_open(ALCdevice *device, const ALCchar *device_name) //{{{
715 pulse_data *data = ppa_xmalloc(sizeof(pulse_data));
716 memset(data, 0, sizeof(*data));
718 if(!(data->loop = ppa_threaded_mainloop_new()))
720 AL_PRINT("pa_threaded_mainloop_new() failed!\n");
721 goto out;
723 if(ppa_threaded_mainloop_start(data->loop) < 0)
725 AL_PRINT("pa_threaded_mainloop_start() failed\n");
726 goto out;
729 ppa_threaded_mainloop_lock(data->loop);
730 device->ExtraData = data;
732 data->context = connect_context(data->loop);
733 if(!data->context)
735 ppa_threaded_mainloop_unlock(data->loop);
736 goto out;
738 ppa_context_set_state_callback(data->context, context_state_callback2, device);
740 device->szDeviceName = strdup(device_name);
742 ppa_threaded_mainloop_unlock(data->loop);
743 return ALC_TRUE;
745 out:
746 if(data->loop)
748 ppa_threaded_mainloop_stop(data->loop);
749 ppa_threaded_mainloop_free(data->loop);
752 device->ExtraData = NULL;
753 ppa_xfree(data);
754 return ALC_FALSE;
755 } //}}}
757 static void pulse_close(ALCdevice *device) //{{{
759 pulse_data *data = device->ExtraData;
761 ppa_threaded_mainloop_lock(data->loop);
763 if(data->stream)
765 ppa_stream_disconnect(data->stream);
766 ppa_stream_unref(data->stream);
769 ppa_context_disconnect(data->context);
770 ppa_context_unref(data->context);
772 ppa_threaded_mainloop_unlock(data->loop);
774 ppa_threaded_mainloop_stop(data->loop);
775 ppa_threaded_mainloop_free(data->loop);
777 DestroyRingBuffer(data->ring);
778 free(data->device_name);
780 device->ExtraData = NULL;
781 ppa_xfree(data);
782 } //}}}
783 //}}}
785 // OpenAL {{{
786 static ALCboolean pulse_open_playback(ALCdevice *device, const ALCchar *device_name) //{{{
788 char *pulse_name = NULL;
789 pa_sample_spec spec;
790 pulse_data *data;
792 if(!pulse_load())
793 return ALC_FALSE;
795 if(!allDevNameMap)
796 probe_devices(AL_FALSE);
798 if(!device_name && numDevNames > 0)
799 device_name = allDevNameMap[0].name;
800 else
802 ALuint i;
804 for(i = 0;i < numDevNames;i++)
806 if(strcmp(device_name, allDevNameMap[i].name) == 0)
808 pulse_name = allDevNameMap[i].device_name;
809 break;
812 if(i == numDevNames)
813 return ALC_FALSE;
816 if(pulse_open(device, device_name) == ALC_FALSE)
817 return ALC_FALSE;
819 data = device->ExtraData;
821 ppa_threaded_mainloop_lock(data->loop);
823 spec.format = PA_SAMPLE_S16NE;
824 spec.rate = 44100;
825 spec.channels = 2;
827 data->device_name = pulse_name;
828 pa_stream *stream = connect_playback_stream(device, 0, NULL, &spec, NULL);
829 if(!stream)
831 ppa_threaded_mainloop_unlock(data->loop);
832 goto fail;
835 if(ppa_stream_is_suspended(stream))
837 AL_PRINT("Device is suspended\n");
838 ppa_stream_disconnect(stream);
839 ppa_stream_unref(stream);
840 ppa_threaded_mainloop_unlock(data->loop);
841 goto fail;
843 data->device_name = strdup(ppa_stream_get_device_name(stream));
845 ppa_stream_disconnect(stream);
846 ppa_stream_unref(stream);
848 ppa_threaded_mainloop_unlock(data->loop);
850 return ALC_TRUE;
852 fail:
853 pulse_close(device);
854 return ALC_FALSE;
855 } //}}}
857 static void pulse_close_playback(ALCdevice *device) //{{{
859 pulse_close(device);
860 } //}}}
862 static ALCboolean pulse_reset_playback(ALCdevice *device) //{{{
864 pulse_data *data = device->ExtraData;
865 pa_stream_flags_t flags = 0;
866 pa_channel_map chanmap;
868 ppa_threaded_mainloop_lock(data->loop);
870 if(!ConfigValueExists(NULL, "format"))
872 pa_operation *o;
873 o = ppa_context_get_sink_info_by_name(data->context, data->device_name, sink_info_callback, device);
874 while(ppa_operation_get_state(o) == PA_OPERATION_RUNNING)
875 ppa_threaded_mainloop_wait(data->loop);
876 ppa_operation_unref(o);
878 if(!ConfigValueExists(NULL, "frequency"))
879 flags |= PA_STREAM_FIX_RATE;
881 data->frame_size = aluFrameSizeFromFormat(device->Format);
882 data->attr.prebuf = -1;
883 data->attr.fragsize = -1;
884 data->attr.minreq = device->UpdateSize * data->frame_size;
885 data->attr.tlength = data->attr.minreq * device->NumUpdates;
886 if(data->attr.tlength < data->attr.minreq*2)
887 data->attr.tlength = data->attr.minreq*2;
888 data->attr.maxlength = data->attr.tlength;
889 flags |= PA_STREAM_EARLY_REQUESTS;
890 flags |= PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE;
892 switch(aluBytesFromFormat(device->Format))
894 case 1:
895 data->spec.format = PA_SAMPLE_U8;
896 break;
897 case 2:
898 data->spec.format = PA_SAMPLE_S16NE;
899 break;
900 case 4:
901 data->spec.format = PA_SAMPLE_FLOAT32NE;
902 break;
903 default:
904 AL_PRINT("Unknown format: 0x%x\n", device->Format);
905 ppa_threaded_mainloop_unlock(data->loop);
906 return ALC_FALSE;
908 data->spec.rate = device->Frequency;
909 data->spec.channels = aluChannelsFromFormat(device->Format);
911 if(ppa_sample_spec_valid(&data->spec) == 0)
913 AL_PRINT("Invalid sample format\n");
914 ppa_threaded_mainloop_unlock(data->loop);
915 return ALC_FALSE;
918 if(!ppa_channel_map_init_auto(&chanmap, data->spec.channels, PA_CHANNEL_MAP_WAVEEX))
920 AL_PRINT("Couldn't build map for channel count (%d)!\n", data->spec.channels);
921 ppa_threaded_mainloop_unlock(data->loop);
922 return ALC_FALSE;
924 SetDefaultWFXChannelOrder(device);
926 data->stream = connect_playback_stream(device, flags, &data->attr, &data->spec, &chanmap);
927 if(!data->stream)
929 ppa_threaded_mainloop_unlock(data->loop);
930 return ALC_FALSE;
933 ppa_stream_set_state_callback(data->stream, stream_state_callback2, device);
935 data->spec = *(ppa_stream_get_sample_spec(data->stream));
936 if(device->Frequency != data->spec.rate)
938 pa_operation *o;
940 /* Server updated our playback rate, so modify the buffer attribs
941 * accordingly. */
942 data->attr.minreq = (ALuint64)(data->attr.minreq/data->frame_size) *
943 data->spec.rate / device->Frequency * data->frame_size;
944 data->attr.tlength = data->attr.minreq * device->NumUpdates;
945 data->attr.maxlength = data->attr.tlength;
947 o = ppa_stream_set_buffer_attr(data->stream, &data->attr,
948 stream_success_callback, device);
949 while(ppa_operation_get_state(o) == PA_OPERATION_RUNNING)
950 ppa_threaded_mainloop_wait(data->loop);
951 ppa_operation_unref(o);
953 device->Frequency = data->spec.rate;
956 stream_buffer_attr_callback(data->stream, device);
957 #if PA_CHECK_VERSION(0,9,15)
958 if(ppa_stream_set_buffer_attr_callback)
959 ppa_stream_set_buffer_attr_callback(data->stream, stream_buffer_attr_callback, device);
960 #endif
961 ppa_stream_set_moved_callback(data->stream, stream_device_callback, device);
962 ppa_stream_set_write_callback(data->stream, stream_write_callback, device);
963 ppa_stream_set_underflow_callback(data->stream, stream_signal_callback, device);
965 data->thread = StartThread(PulseProc, device);
966 if(!data->thread)
968 #if PA_CHECK_VERSION(0,9,15)
969 if(ppa_stream_set_buffer_attr_callback)
970 ppa_stream_set_buffer_attr_callback(data->stream, NULL, NULL);
971 #endif
972 ppa_stream_set_moved_callback(data->stream, NULL, NULL);
973 ppa_stream_set_write_callback(data->stream, NULL, NULL);
974 ppa_stream_set_underflow_callback(data->stream, NULL, NULL);
975 ppa_stream_disconnect(data->stream);
976 ppa_stream_unref(data->stream);
977 data->stream = NULL;
979 ppa_threaded_mainloop_unlock(data->loop);
980 return ALC_FALSE;
983 ppa_threaded_mainloop_unlock(data->loop);
984 return ALC_TRUE;
985 } //}}}
987 static void pulse_stop_playback(ALCdevice *device) //{{{
989 pulse_data *data = device->ExtraData;
991 if(!data->stream)
992 return;
994 data->killNow = AL_TRUE;
995 if(data->thread)
997 ppa_threaded_mainloop_signal(data->loop, 0);
998 StopThread(data->thread);
999 data->thread = NULL;
1001 data->killNow = AL_FALSE;
1003 ppa_threaded_mainloop_lock(data->loop);
1005 #if PA_CHECK_VERSION(0,9,15)
1006 if(ppa_stream_set_buffer_attr_callback)
1007 ppa_stream_set_buffer_attr_callback(data->stream, NULL, NULL);
1008 #endif
1009 ppa_stream_set_moved_callback(data->stream, NULL, NULL);
1010 ppa_stream_set_write_callback(data->stream, NULL, NULL);
1011 ppa_stream_set_underflow_callback(data->stream, NULL, NULL);
1012 ppa_stream_disconnect(data->stream);
1013 ppa_stream_unref(data->stream);
1014 data->stream = NULL;
1016 ppa_threaded_mainloop_unlock(data->loop);
1017 } //}}}
1020 static ALCboolean pulse_open_capture(ALCdevice *device, const ALCchar *device_name) //{{{
1022 char *pulse_name = NULL;
1023 pulse_data *data;
1024 pa_stream_flags_t flags = 0;
1025 pa_stream_state_t state;
1026 pa_channel_map chanmap;
1028 if(!pulse_load())
1029 return ALC_FALSE;
1031 if(!allCaptureDevNameMap)
1032 probe_devices(AL_TRUE);
1034 if(!device_name && numCaptureDevNames > 0)
1035 device_name = allCaptureDevNameMap[0].name;
1036 else
1038 ALuint i;
1040 for(i = 0;i < numCaptureDevNames;i++)
1042 if(strcmp(device_name, allCaptureDevNameMap[i].name) == 0)
1044 pulse_name = allCaptureDevNameMap[i].device_name;
1045 break;
1048 if(i == numCaptureDevNames)
1049 return ALC_FALSE;
1052 if(pulse_open(device, device_name) == ALC_FALSE)
1053 return ALC_FALSE;
1055 data = device->ExtraData;
1056 ppa_threaded_mainloop_lock(data->loop);
1058 data->samples = device->UpdateSize * device->NumUpdates;
1059 data->frame_size = aluFrameSizeFromFormat(device->Format);
1060 if(data->samples < 100 * device->Frequency / 1000)
1061 data->samples = 100 * device->Frequency / 1000;
1063 if(!(data->ring = CreateRingBuffer(data->frame_size, data->samples)))
1065 ppa_threaded_mainloop_unlock(data->loop);
1066 goto fail;
1069 data->attr.minreq = -1;
1070 data->attr.prebuf = -1;
1071 data->attr.maxlength = data->samples * data->frame_size;
1072 data->attr.tlength = -1;
1073 data->attr.fragsize = min(data->samples, 50 * device->Frequency / 1000) *
1074 data->frame_size;
1076 data->spec.rate = device->Frequency;
1077 data->spec.channels = aluChannelsFromFormat(device->Format);
1079 switch(aluBytesFromFormat(device->Format))
1081 case 1:
1082 data->spec.format = PA_SAMPLE_U8;
1083 break;
1084 case 2:
1085 data->spec.format = PA_SAMPLE_S16NE;
1086 break;
1087 case 4:
1088 data->spec.format = PA_SAMPLE_FLOAT32NE;
1089 break;
1090 default:
1091 AL_PRINT("Unknown format: 0x%x\n", device->Format);
1092 ppa_threaded_mainloop_unlock(data->loop);
1093 goto fail;
1096 if(ppa_sample_spec_valid(&data->spec) == 0)
1098 AL_PRINT("Invalid sample format\n");
1099 ppa_threaded_mainloop_unlock(data->loop);
1100 goto fail;
1103 if(!ppa_channel_map_init_auto(&chanmap, data->spec.channels, PA_CHANNEL_MAP_WAVEEX))
1105 AL_PRINT("Couldn't build map for channel count (%d)!\n", data->spec.channels);
1106 ppa_threaded_mainloop_unlock(data->loop);
1107 goto fail;
1110 data->stream = ppa_stream_new(data->context, "Capture Stream", &data->spec, &chanmap);
1111 if(!data->stream)
1113 AL_PRINT("pa_stream_new() failed: %s\n",
1114 ppa_strerror(ppa_context_errno(data->context)));
1116 ppa_threaded_mainloop_unlock(data->loop);
1117 goto fail;
1120 ppa_stream_set_state_callback(data->stream, stream_state_callback, data->loop);
1122 flags |= PA_STREAM_START_CORKED|PA_STREAM_ADJUST_LATENCY;
1123 if(ppa_stream_connect_record(data->stream, pulse_name, &data->attr, flags) < 0)
1125 AL_PRINT("Stream did not connect: %s\n",
1126 ppa_strerror(ppa_context_errno(data->context)));
1128 ppa_stream_unref(data->stream);
1129 data->stream = NULL;
1131 ppa_threaded_mainloop_unlock(data->loop);
1132 goto fail;
1135 while((state=ppa_stream_get_state(data->stream)) != PA_STREAM_READY)
1137 if(!PA_STREAM_IS_GOOD(state))
1139 AL_PRINT("Stream did not get ready: %s\n",
1140 ppa_strerror(ppa_context_errno(data->context)));
1142 ppa_stream_unref(data->stream);
1143 data->stream = NULL;
1145 ppa_threaded_mainloop_unlock(data->loop);
1146 goto fail;
1149 ppa_threaded_mainloop_wait(data->loop);
1151 ppa_stream_set_state_callback(data->stream, stream_state_callback2, device);
1153 ppa_threaded_mainloop_unlock(data->loop);
1154 return ALC_TRUE;
1156 fail:
1157 pulse_close(device);
1158 return ALC_FALSE;
1159 } //}}}
1161 static void pulse_close_capture(ALCdevice *device) //{{{
1163 pulse_close(device);
1164 } //}}}
1166 static void pulse_start_capture(ALCdevice *device) //{{{
1168 pulse_data *data = device->ExtraData;
1169 pa_operation *o;
1171 ppa_threaded_mainloop_lock(data->loop);
1172 o = ppa_stream_cork(data->stream, 0, stream_success_callback, device);
1173 while(ppa_operation_get_state(o) == PA_OPERATION_RUNNING)
1174 ppa_threaded_mainloop_wait(data->loop);
1175 ppa_operation_unref(o);
1176 ppa_threaded_mainloop_unlock(data->loop);
1177 } //}}}
1179 static void pulse_stop_capture(ALCdevice *device) //{{{
1181 pulse_data *data = device->ExtraData;
1182 pa_operation *o;
1184 ppa_threaded_mainloop_lock(data->loop);
1185 o = ppa_stream_cork(data->stream, 1, stream_success_callback, device);
1186 while(ppa_operation_get_state(o) == PA_OPERATION_RUNNING)
1187 ppa_threaded_mainloop_wait(data->loop);
1188 ppa_operation_unref(o);
1189 ppa_threaded_mainloop_unlock(data->loop);
1190 } //}}}
1192 static ALCuint pulse_available_samples(ALCdevice *device) //{{{
1194 pulse_data *data = device->ExtraData;
1195 size_t samples;
1197 ppa_threaded_mainloop_lock(data->loop);
1198 /* Capture is done in fragment-sized chunks, so we loop until we get all
1199 * that's available */
1200 samples = (device->Connected ? ppa_stream_readable_size(data->stream) : 0);
1201 while(samples > 0)
1203 const void *buf;
1204 size_t length;
1206 if(ppa_stream_peek(data->stream, &buf, &length) < 0)
1208 AL_PRINT("pa_stream_peek() failed: %s\n",
1209 ppa_strerror(ppa_context_errno(data->context)));
1210 break;
1213 WriteRingBuffer(data->ring, buf, length/data->frame_size);
1214 samples -= length;
1216 ppa_stream_drop(data->stream);
1218 ppa_threaded_mainloop_unlock(data->loop);
1220 return RingBufferSize(data->ring);
1221 } //}}}
1223 static void pulse_capture_samples(ALCdevice *device, ALCvoid *buffer, ALCuint samples) //{{{
1225 pulse_data *data = device->ExtraData;
1227 if(pulse_available_samples(device) >= samples)
1228 ReadRingBuffer(data->ring, buffer, samples);
1229 else
1230 alcSetError(device, ALC_INVALID_VALUE);
1231 } //}}}
1234 BackendFuncs pulse_funcs = { //{{{
1235 pulse_open_playback,
1236 pulse_close_playback,
1237 pulse_reset_playback,
1238 pulse_stop_playback,
1239 pulse_open_capture,
1240 pulse_close_capture,
1241 pulse_start_capture,
1242 pulse_stop_capture,
1243 pulse_capture_samples,
1244 pulse_available_samples
1245 }; //}}}
1247 void alc_pulse_init(BackendFuncs *func_list) //{{{
1249 *func_list = pulse_funcs;
1251 pulse_ctx_flags = 0;
1252 if(!GetConfigValueBool("pulse", "spawn-server", 0))
1253 pulse_ctx_flags |= PA_CONTEXT_NOAUTOSPAWN;
1254 } //}}}
1256 void alc_pulse_deinit(void) //{{{
1258 ALuint i;
1260 for(i = 0;i < numDevNames;++i)
1262 free(allDevNameMap[i].name);
1263 free(allDevNameMap[i].device_name);
1265 free(allDevNameMap);
1266 allDevNameMap = NULL;
1267 numDevNames = 0;
1269 for(i = 0;i < numCaptureDevNames;++i)
1271 free(allCaptureDevNameMap[i].name);
1272 free(allCaptureDevNameMap[i].device_name);
1274 free(allCaptureDevNameMap);
1275 allCaptureDevNameMap = NULL;
1276 numCaptureDevNames = 0;
1278 if(pa_handle)
1280 #ifdef _WIN32
1281 FreeLibrary(pa_handle);
1282 #elif defined (HAVE_DLFCN_H)
1283 dlclose(pa_handle);
1284 #endif
1285 pa_handle = NULL;
1287 } //}}}
1289 void alc_pulse_probe(int type) //{{{
1291 if(!pulse_load()) return;
1293 if(type == DEVICE_PROBE)
1295 pa_threaded_mainloop *loop;
1297 if((loop=ppa_threaded_mainloop_new()) &&
1298 ppa_threaded_mainloop_start(loop) >= 0)
1300 pa_context *context;
1302 ppa_threaded_mainloop_lock(loop);
1303 context = connect_context(loop);
1304 if(context)
1306 AppendDeviceList(pulse_device);
1308 ppa_context_disconnect(context);
1309 ppa_context_unref(context);
1311 ppa_threaded_mainloop_unlock(loop);
1312 ppa_threaded_mainloop_stop(loop);
1314 if(loop)
1315 ppa_threaded_mainloop_free(loop);
1317 else if(type == ALL_DEVICE_PROBE)
1319 ALuint i;
1321 for(i = 0;i < numDevNames;++i)
1323 free(allDevNameMap[i].name);
1324 free(allDevNameMap[i].device_name);
1326 free(allDevNameMap);
1327 allDevNameMap = NULL;
1328 numDevNames = 0;
1330 probe_devices(AL_FALSE);
1332 for(i = 0;i < numDevNames;i++)
1333 AppendAllDeviceList(allDevNameMap[i].name);
1335 else if(type == CAPTURE_DEVICE_PROBE)
1337 ALuint i;
1339 for(i = 0;i < numCaptureDevNames;++i)
1341 free(allCaptureDevNameMap[i].name);
1342 free(allCaptureDevNameMap[i].device_name);
1344 free(allCaptureDevNameMap);
1345 allCaptureDevNameMap = NULL;
1346 numCaptureDevNames = 0;
1348 probe_devices(AL_TRUE);
1350 for(i = 0;i < numCaptureDevNames;i++)
1351 AppendCaptureDeviceList(allCaptureDevNameMap[i].name);
1353 } //}}}
1354 //}}}