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
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
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)))
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
);
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
);
118 #if PA_CHECK_VERSION(0,9,16)
119 MAKE_FUNC(pa_stream_begin_write
);
124 #define PATH_MAX 4096
138 pa_threaded_mainloop
*loop
;
141 volatile ALboolean killNow
;
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) //{{{
166 pa_handle
= LoadLibrary("libpulse-0.dll");
167 #define LOAD_FUNC(x) do { \
168 p##x = (typeof(p##x))GetProcAddress(pa_handle, #x); \
170 AL_PRINT("Could not load %s from libpulse-0.dll\n", #x); \
171 FreeLibrary(pa_handle); \
176 #define LOAD_OPTIONAL_FUNC(x) do { \
177 p##x = (typeof(p##x))GetProcAddress(pa_handle, #x); \
180 #elif defined (HAVE_DLFCN_H)
183 #if defined(__APPLE__) && defined(__MACH__)
184 pa_handle
= dlopen("libpulse.0.dylib", RTLD_NOW
);
186 pa_handle
= dlopen("libpulse.so.0", RTLD_NOW
);
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); \
199 #define LOAD_OPTIONAL_FUNC(x) do { \
200 p##x = dlsym(pa_handle, #x); \
201 if((err=dlerror()) != NULL) { \
208 pa_handle
= (void*)0xDEADBEEF;
209 #define LOAD_FUNC(x) p##x = (x)
210 #define LOAD_OPTIONAL_FUNC(x) p##x = (x)
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
);
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
);
277 #if PA_CHECK_VERSION(0,9,16)
278 LOAD_OPTIONAL_FUNC(pa_stream_begin_write
);
281 #undef LOAD_OPTIONAL_FUNC
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);
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);
308 static void stream_signal_callback(pa_stream
*stream
, void *pdata
) //{{{
310 ALCdevice
*Device
= pdata
;
311 pulse_data
*data
= Device
->ExtraData
;
314 ppa_threaded_mainloop_signal(data
->loop
, 0);
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
);
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
));
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);
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);
371 static void stream_success_callback(pa_stream
*stream
, int success
, void *pdata
) //{{{
373 ALCdevice
*Device
= pdata
;
374 pulse_data
*data
= Device
->ExtraData
;
378 ppa_threaded_mainloop_signal(data
->loop
, 0);
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] = "";
390 { "front-left,front-right,front-center,lfe,rear-left,rear-right,side-left,side-right",
392 { "front-left,front-right,front-center,lfe,rear-center,side-left,side-right",
394 { "front-left,front-right,front-center,lfe,rear-left,rear-right",
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
},
406 ppa_threaded_mainloop_signal(data
->loop
, 0);
410 for(i
= 0;chanmaps
[i
].str
;i
++)
413 if(!ppa_channel_map_parse(&map
, chanmaps
[i
].str
))
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
))
423 device
->Format
= chanmaps
[i
].format
;
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
);
432 static void sink_device_callback(pa_context
*context
, const pa_sink_info
*info
, int eol
, void *pdata
) //{{{
434 pa_threaded_mainloop
*loop
= pdata
;
444 ppa_threaded_mainloop_signal(loop
, 0);
451 snprintf(str
, sizeof(str
), "%s via PulseAudio", info
->description
);
453 snprintf(str
, sizeof(str
), "%s #%d via PulseAudio", info
->description
, count
+1);
456 for(i
= 0;i
< numDevNames
;i
++)
458 if(strcmp(str
, allDevNameMap
[i
].name
) == 0)
461 } while(i
!= numDevNames
);
463 temp
= realloc(allDevNameMap
, (numDevNames
+1) * sizeof(*allDevNameMap
));
466 allDevNameMap
= temp
;
467 allDevNameMap
[numDevNames
].name
= strdup(str
);
468 allDevNameMap
[numDevNames
].device_name
= strdup(info
->name
);
473 static void source_device_callback(pa_context
*context
, const pa_source_info
*info
, int eol
, void *pdata
) //{{{
475 pa_threaded_mainloop
*loop
= pdata
;
485 ppa_threaded_mainloop_signal(loop
, 0);
492 snprintf(str
, sizeof(str
), "%s via PulseAudio", info
->description
);
494 snprintf(str
, sizeof(str
), "%s #%d via PulseAudio", info
->description
, count
+1);
497 for(i
= 0;i
< numCaptureDevNames
;i
++)
499 if(strcmp(str
, allCaptureDevNameMap
[i
].name
) == 0)
502 } while(i
!= numCaptureDevNames
);
504 temp
= realloc(allCaptureDevNameMap
, (numCaptureDevNames
+1) * sizeof(*allCaptureDevNameMap
));
507 allCaptureDevNameMap
= temp
;
508 allCaptureDevNameMap
[numCaptureDevNames
].name
= strdup(str
);
509 allCaptureDevNameMap
[numCaptureDevNames
].device_name
= strdup(info
->name
);
510 numCaptureDevNames
++;
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
;
523 ppa_threaded_mainloop_signal(data
->loop
, 0);
527 static ALuint
PulseProc(ALvoid
*param
)
529 ALCdevice
*Device
= param
;
530 pulse_data
*data
= Device
->ExtraData
;
535 ppa_threaded_mainloop_lock(data
->loop
);
537 len
= (Device
->Connected
? ppa_stream_writable_size(data
->stream
) : 0);
538 len
-= len
%(Device
->UpdateSize
*data
->frame_size
);
541 ppa_threaded_mainloop_wait(data
->loop
);
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)
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
);
567 } while(Device
->Connected
&& !data
->killNow
);
568 ppa_threaded_mainloop_unlock(data
->loop
);
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
;
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
);
587 AL_PRINT("pa_context_new() failed\n");
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
;
604 ppa_threaded_mainloop_wait(loop
);
607 ppa_context_set_state_callback(context
, NULL
, NULL
);
611 AL_PRINT("Context did not connect: %s\n", ppa_strerror(err
));
612 ppa_context_unref(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
;
627 stream
= ppa_stream_new(data
->context
, "Playback Stream", spec
, chanmap
);
630 AL_PRINT("pa_stream_new() failed: %s\n",
631 ppa_strerror(ppa_context_errno(data
->context
)));
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
);
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
);
655 ppa_threaded_mainloop_wait(data
->loop
);
657 ppa_stream_set_state_callback(stream
, NULL
, NULL
);
662 static void probe_devices(ALboolean capture
)
664 pa_threaded_mainloop
*loop
;
666 if(capture
== AL_FALSE
)
667 allDevNameMap
= malloc(sizeof(DevMap
) * 1);
669 allCaptureDevNameMap
= malloc(sizeof(DevMap
) * 1);
671 if((loop
=ppa_threaded_mainloop_new()) &&
672 ppa_threaded_mainloop_start(loop
) >= 0)
676 ppa_threaded_mainloop_lock(loop
);
677 context
= connect_context(loop
);
682 if(capture
== AL_FALSE
)
684 allDevNameMap
[0].name
= strdup(pulse_device
);
685 allDevNameMap
[0].device_name
= NULL
;
688 o
= ppa_context_get_sink_info_list(context
, sink_device_callback
, loop
);
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
);
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");
723 if(ppa_threaded_mainloop_start(data
->loop
) < 0)
725 AL_PRINT("pa_threaded_mainloop_start() failed\n");
729 ppa_threaded_mainloop_lock(data
->loop
);
730 device
->ExtraData
= data
;
732 data
->context
= connect_context(data
->loop
);
735 ppa_threaded_mainloop_unlock(data
->loop
);
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
);
748 ppa_threaded_mainloop_stop(data
->loop
);
749 ppa_threaded_mainloop_free(data
->loop
);
752 device
->ExtraData
= NULL
;
757 static void pulse_close(ALCdevice
*device
) //{{{
759 pulse_data
*data
= device
->ExtraData
;
761 ppa_threaded_mainloop_lock(data
->loop
);
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
;
786 static ALCboolean
pulse_open_playback(ALCdevice
*device
, const ALCchar
*device_name
) //{{{
788 char *pulse_name
= NULL
;
796 probe_devices(AL_FALSE
);
798 if(!device_name
&& numDevNames
> 0)
799 device_name
= allDevNameMap
[0].name
;
804 for(i
= 0;i
< numDevNames
;i
++)
806 if(strcmp(device_name
, allDevNameMap
[i
].name
) == 0)
808 pulse_name
= allDevNameMap
[i
].device_name
;
816 if(pulse_open(device
, device_name
) == ALC_FALSE
)
819 data
= device
->ExtraData
;
821 ppa_threaded_mainloop_lock(data
->loop
);
823 spec
.format
= PA_SAMPLE_S16NE
;
827 data
->device_name
= pulse_name
;
828 pa_stream
*stream
= connect_playback_stream(device
, 0, NULL
, &spec
, NULL
);
831 ppa_threaded_mainloop_unlock(data
->loop
);
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
);
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
);
857 static void pulse_close_playback(ALCdevice
*device
) //{{{
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"))
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
))
895 data
->spec
.format
= PA_SAMPLE_U8
;
898 data
->spec
.format
= PA_SAMPLE_S16NE
;
901 data
->spec
.format
= PA_SAMPLE_FLOAT32NE
;
904 AL_PRINT("Unknown format: 0x%x\n", device
->Format
);
905 ppa_threaded_mainloop_unlock(data
->loop
);
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
);
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
);
924 SetDefaultWFXChannelOrder(device
);
926 data
->stream
= connect_playback_stream(device
, flags
, &data
->attr
, &data
->spec
, &chanmap
);
929 ppa_threaded_mainloop_unlock(data
->loop
);
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
)
940 /* Server updated our playback rate, so modify the buffer attribs
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
);
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
);
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
);
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
);
979 ppa_threaded_mainloop_unlock(data
->loop
);
983 ppa_threaded_mainloop_unlock(data
->loop
);
987 static void pulse_stop_playback(ALCdevice
*device
) //{{{
989 pulse_data
*data
= device
->ExtraData
;
994 data
->killNow
= AL_TRUE
;
997 ppa_threaded_mainloop_signal(data
->loop
, 0);
998 StopThread(data
->thread
);
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
);
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
);
1020 static ALCboolean
pulse_open_capture(ALCdevice
*device
, const ALCchar
*device_name
) //{{{
1022 char *pulse_name
= NULL
;
1024 pa_stream_flags_t flags
= 0;
1025 pa_stream_state_t state
;
1026 pa_channel_map chanmap
;
1031 if(!allCaptureDevNameMap
)
1032 probe_devices(AL_TRUE
);
1034 if(!device_name
&& numCaptureDevNames
> 0)
1035 device_name
= allCaptureDevNameMap
[0].name
;
1040 for(i
= 0;i
< numCaptureDevNames
;i
++)
1042 if(strcmp(device_name
, allCaptureDevNameMap
[i
].name
) == 0)
1044 pulse_name
= allCaptureDevNameMap
[i
].device_name
;
1048 if(i
== numCaptureDevNames
)
1052 if(pulse_open(device
, device_name
) == 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
);
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) *
1076 data
->spec
.rate
= device
->Frequency
;
1077 data
->spec
.channels
= aluChannelsFromFormat(device
->Format
);
1079 switch(aluBytesFromFormat(device
->Format
))
1082 data
->spec
.format
= PA_SAMPLE_U8
;
1085 data
->spec
.format
= PA_SAMPLE_S16NE
;
1088 data
->spec
.format
= PA_SAMPLE_FLOAT32NE
;
1091 AL_PRINT("Unknown format: 0x%x\n", device
->Format
);
1092 ppa_threaded_mainloop_unlock(data
->loop
);
1096 if(ppa_sample_spec_valid(&data
->spec
) == 0)
1098 AL_PRINT("Invalid sample format\n");
1099 ppa_threaded_mainloop_unlock(data
->loop
);
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
);
1110 data
->stream
= ppa_stream_new(data
->context
, "Capture Stream", &data
->spec
, &chanmap
);
1113 AL_PRINT("pa_stream_new() failed: %s\n",
1114 ppa_strerror(ppa_context_errno(data
->context
)));
1116 ppa_threaded_mainloop_unlock(data
->loop
);
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
);
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
);
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
);
1157 pulse_close(device
);
1161 static void pulse_close_capture(ALCdevice
*device
) //{{{
1163 pulse_close(device
);
1166 static void pulse_start_capture(ALCdevice
*device
) //{{{
1168 pulse_data
*data
= device
->ExtraData
;
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
);
1179 static void pulse_stop_capture(ALCdevice
*device
) //{{{
1181 pulse_data
*data
= device
->ExtraData
;
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
);
1192 static ALCuint
pulse_available_samples(ALCdevice
*device
) //{{{
1194 pulse_data
*data
= device
->ExtraData
;
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);
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
)));
1213 WriteRingBuffer(data
->ring
, buf
, length
/data
->frame_size
);
1216 ppa_stream_drop(data
->stream
);
1218 ppa_threaded_mainloop_unlock(data
->loop
);
1220 return RingBufferSize(data
->ring
);
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
);
1230 alcSetError(device
, ALC_INVALID_VALUE
);
1234 BackendFuncs pulse_funcs
= { //{{{
1235 pulse_open_playback
,
1236 pulse_close_playback
,
1237 pulse_reset_playback
,
1238 pulse_stop_playback
,
1240 pulse_close_capture
,
1241 pulse_start_capture
,
1243 pulse_capture_samples
,
1244 pulse_available_samples
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
;
1256 void alc_pulse_deinit(void) //{{{
1260 for(i
= 0;i
< numDevNames
;++i
)
1262 free(allDevNameMap
[i
].name
);
1263 free(allDevNameMap
[i
].device_name
);
1265 free(allDevNameMap
);
1266 allDevNameMap
= NULL
;
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;
1281 FreeLibrary(pa_handle
);
1282 #elif defined (HAVE_DLFCN_H)
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
);
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
);
1315 ppa_threaded_mainloop_free(loop
);
1317 else if(type
== ALL_DEVICE_PROBE
)
1321 for(i
= 0;i
< numDevNames
;++i
)
1323 free(allDevNameMap
[i
].name
);
1324 free(allDevNameMap
[i
].device_name
);
1326 free(allDevNameMap
);
1327 allDevNameMap
= NULL
;
1330 probe_devices(AL_FALSE
);
1332 for(i
= 0;i
< numDevNames
;i
++)
1333 AppendAllDeviceList(allDevNameMap
[i
].name
);
1335 else if(type
== CAPTURE_DEVICE_PROBE
)
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
);