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
26 #include <pulse/pulseaudio.h>
28 #if PA_API_VERSION == 11
29 #define PA_STREAM_ADJUST_LATENCY 0x2000U
30 #define PA_STREAM_EARLY_REQUESTS 0x4000U
31 static __inline
int PA_STREAM_IS_GOOD(pa_stream_state_t x
)
33 return (x
== PA_STREAM_CREATING
|| x
== PA_STREAM_READY
);
35 static __inline
int PA_CONTEXT_IS_GOOD(pa_context_state_t x
)
37 return (x
== PA_CONTEXT_CONNECTING
|| x
== PA_CONTEXT_AUTHORIZING
||
38 x
== PA_CONTEXT_SETTING_NAME
|| x
== PA_CONTEXT_READY
);
40 #define PA_STREAM_IS_GOOD PA_STREAM_IS_GOOD
41 #define PA_CONTEXT_IS_GOOD PA_CONTEXT_IS_GOOD
42 #elif PA_API_VERSION != 12
43 #error Invalid PulseAudio API version
46 #ifndef PA_CHECK_VERSION
47 #define PA_CHECK_VERSION(major,minor,micro) \
48 ((PA_MAJOR > (major)) || \
49 (PA_MAJOR == (major) && PA_MINOR > (minor)) || \
50 (PA_MAJOR == (major) && PA_MINOR == (minor) && PA_MICRO >= (micro)))
53 static void *pa_handle
;
55 #define MAKE_FUNC(x) static typeof(x) * p##x
56 MAKE_FUNC(pa_context_unref
);
57 MAKE_FUNC(pa_sample_spec_valid
);
58 MAKE_FUNC(pa_stream_drop
);
59 MAKE_FUNC(pa_strerror
);
60 MAKE_FUNC(pa_context_get_state
);
61 MAKE_FUNC(pa_stream_get_state
);
62 MAKE_FUNC(pa_threaded_mainloop_signal
);
63 MAKE_FUNC(pa_stream_peek
);
64 MAKE_FUNC(pa_threaded_mainloop_wait
);
65 MAKE_FUNC(pa_threaded_mainloop_unlock
);
66 MAKE_FUNC(pa_threaded_mainloop_in_thread
);
67 MAKE_FUNC(pa_context_new
);
68 MAKE_FUNC(pa_threaded_mainloop_stop
);
69 MAKE_FUNC(pa_context_disconnect
);
70 MAKE_FUNC(pa_threaded_mainloop_start
);
71 MAKE_FUNC(pa_threaded_mainloop_get_api
);
72 MAKE_FUNC(pa_context_set_state_callback
);
73 MAKE_FUNC(pa_stream_write
);
75 MAKE_FUNC(pa_stream_connect_record
);
76 MAKE_FUNC(pa_stream_connect_playback
);
77 MAKE_FUNC(pa_stream_readable_size
);
78 MAKE_FUNC(pa_stream_writable_size
);
79 MAKE_FUNC(pa_stream_cork
);
80 MAKE_FUNC(pa_stream_is_suspended
);
81 MAKE_FUNC(pa_stream_get_device_name
);
82 MAKE_FUNC(pa_path_get_filename
);
83 MAKE_FUNC(pa_get_binary_name
);
84 MAKE_FUNC(pa_threaded_mainloop_free
);
85 MAKE_FUNC(pa_context_errno
);
86 MAKE_FUNC(pa_xmalloc
);
87 MAKE_FUNC(pa_stream_unref
);
88 MAKE_FUNC(pa_threaded_mainloop_accept
);
89 MAKE_FUNC(pa_stream_set_write_callback
);
90 MAKE_FUNC(pa_threaded_mainloop_new
);
91 MAKE_FUNC(pa_context_connect
);
92 MAKE_FUNC(pa_stream_set_buffer_attr
);
93 MAKE_FUNC(pa_stream_get_buffer_attr
);
94 MAKE_FUNC(pa_stream_get_sample_spec
);
95 MAKE_FUNC(pa_stream_get_time
);
96 MAKE_FUNC(pa_stream_set_read_callback
);
97 MAKE_FUNC(pa_stream_set_state_callback
);
98 MAKE_FUNC(pa_stream_set_moved_callback
);
99 MAKE_FUNC(pa_stream_set_underflow_callback
);
100 MAKE_FUNC(pa_stream_new
);
101 MAKE_FUNC(pa_stream_disconnect
);
102 MAKE_FUNC(pa_threaded_mainloop_lock
);
103 MAKE_FUNC(pa_channel_map_init_auto
);
104 MAKE_FUNC(pa_channel_map_parse
);
105 MAKE_FUNC(pa_channel_map_snprint
);
106 MAKE_FUNC(pa_channel_map_equal
);
107 MAKE_FUNC(pa_context_get_server_info
);
108 MAKE_FUNC(pa_context_get_sink_info_by_name
);
109 MAKE_FUNC(pa_context_get_sink_info_list
);
110 MAKE_FUNC(pa_context_get_source_info_list
);
111 MAKE_FUNC(pa_operation_get_state
);
112 MAKE_FUNC(pa_operation_unref
);
113 #if PA_CHECK_VERSION(0,9,15)
114 MAKE_FUNC(pa_channel_map_superset
);
115 MAKE_FUNC(pa_stream_set_buffer_attr_callback
);
117 #if PA_CHECK_VERSION(0,9,16)
118 MAKE_FUNC(pa_stream_begin_write
);
122 #define pa_context_unref ppa_context_unref
123 #define pa_sample_spec_valid ppa_sample_spec_valid
124 #define pa_stream_drop ppa_stream_drop
125 #define pa_strerror ppa_strerror
126 #define pa_context_get_state ppa_context_get_state
127 #define pa_stream_get_state ppa_stream_get_state
128 #define pa_threaded_mainloop_signal ppa_threaded_mainloop_signal
129 #define pa_stream_peek ppa_stream_peek
130 #define pa_threaded_mainloop_wait ppa_threaded_mainloop_wait
131 #define pa_threaded_mainloop_unlock ppa_threaded_mainloop_unlock
132 #define pa_threaded_mainloop_in_thread ppa_threaded_mainloop_in_thread
133 #define pa_context_new ppa_context_new
134 #define pa_threaded_mainloop_stop ppa_threaded_mainloop_stop
135 #define pa_context_disconnect ppa_context_disconnect
136 #define pa_threaded_mainloop_start ppa_threaded_mainloop_start
137 #define pa_threaded_mainloop_get_api ppa_threaded_mainloop_get_api
138 #define pa_context_set_state_callback ppa_context_set_state_callback
139 #define pa_stream_write ppa_stream_write
140 #define pa_xfree ppa_xfree
141 #define pa_stream_connect_record ppa_stream_connect_record
142 #define pa_stream_connect_playback ppa_stream_connect_playback
143 #define pa_stream_readable_size ppa_stream_readable_size
144 #define pa_stream_writable_size ppa_stream_writable_size
145 #define pa_stream_cork ppa_stream_cork
146 #define pa_stream_is_suspended ppa_stream_is_suspended
147 #define pa_stream_get_device_name ppa_stream_get_device_name
148 #define pa_path_get_filename ppa_path_get_filename
149 #define pa_get_binary_name ppa_get_binary_name
150 #define pa_threaded_mainloop_free ppa_threaded_mainloop_free
151 #define pa_context_errno ppa_context_errno
152 #define pa_xmalloc ppa_xmalloc
153 #define pa_stream_unref ppa_stream_unref
154 #define pa_threaded_mainloop_accept ppa_threaded_mainloop_accept
155 #define pa_stream_set_write_callback ppa_stream_set_write_callback
156 #define pa_threaded_mainloop_new ppa_threaded_mainloop_new
157 #define pa_context_connect ppa_context_connect
158 #define pa_stream_set_buffer_attr ppa_stream_set_buffer_attr
159 #define pa_stream_get_buffer_attr ppa_stream_get_buffer_attr
160 #define pa_stream_get_sample_spec ppa_stream_get_sample_spec
161 #define pa_stream_get_time ppa_stream_get_time
162 #define pa_stream_set_read_callback ppa_stream_set_read_callback
163 #define pa_stream_set_state_callback ppa_stream_set_state_callback
164 #define pa_stream_set_moved_callback ppa_stream_set_moved_callback
165 #define pa_stream_set_underflow_callback ppa_stream_set_underflow_callback
166 #define pa_stream_new ppa_stream_new
167 #define pa_stream_disconnect ppa_stream_disconnect
168 #define pa_threaded_mainloop_lock ppa_threaded_mainloop_lock
169 #define pa_channel_map_init_auto ppa_channel_map_init_auto
170 #define pa_channel_map_parse ppa_channel_map_parse
171 #define pa_channel_map_snprint ppa_channel_map_snprint
172 #define pa_channel_map_equal ppa_channel_map_equal
173 #define pa_context_get_server_info ppa_context_get_server_info
174 #define pa_context_get_sink_info_by_name ppa_context_get_sink_info_by_name
175 #define pa_context_get_sink_info_list ppa_context_get_sink_info_list
176 #define pa_context_get_source_info_list ppa_context_get_source_info_list
177 #define pa_operation_get_state ppa_operation_get_state
178 #define pa_operation_unref ppa_operation_unref
179 #if PA_CHECK_VERSION(0,9,15)
180 #define pa_channel_map_superset ppa_channel_map_superset
181 #define pa_stream_set_buffer_attr_callback ppa_stream_set_buffer_attr_callback
183 #if PA_CHECK_VERSION(0,9,16)
184 #define pa_stream_begin_write ppa_stream_begin_write
190 #define PATH_MAX 4096
204 pa_threaded_mainloop
*loop
;
207 volatile ALboolean killNow
;
219 static const ALCchar pulse_device
[] = "PulseAudio Default";
220 static DevMap
*allDevNameMap
;
221 static ALuint numDevNames
;
222 static DevMap
*allCaptureDevNameMap
;
223 static ALuint numCaptureDevNames
;
224 static pa_context_flags_t pulse_ctx_flags
;
227 static void context_state_callback(pa_context
*context
, void *pdata
)
229 pa_threaded_mainloop
*loop
= pdata
;
230 pa_context_state_t state
;
232 state
= pa_context_get_state(context
);
233 if(state
== PA_CONTEXT_READY
|| !PA_CONTEXT_IS_GOOD(state
))
234 pa_threaded_mainloop_signal(loop
, 0);
237 static pa_context
*connect_context(pa_threaded_mainloop
*loop
, ALboolean silent
)
239 const char *name
= "OpenAL Soft";
240 char path_name
[PATH_MAX
];
241 pa_context_state_t state
;
245 if(pa_get_binary_name(path_name
, sizeof(path_name
)))
246 name
= pa_path_get_filename(path_name
);
248 context
= pa_context_new(pa_threaded_mainloop_get_api(loop
), name
);
251 ERR("pa_context_new() failed\n");
255 pa_context_set_state_callback(context
, context_state_callback
, loop
);
257 if((err
=pa_context_connect(context
, NULL
, pulse_ctx_flags
, NULL
)) >= 0)
259 while((state
=pa_context_get_state(context
)) != PA_CONTEXT_READY
)
261 if(!PA_CONTEXT_IS_GOOD(state
))
263 err
= pa_context_errno(context
);
264 if(err
> 0) err
= -err
;
268 pa_threaded_mainloop_wait(loop
);
271 pa_context_set_state_callback(context
, NULL
, NULL
);
276 ERR("Context did not connect: %s\n", pa_strerror(err
));
277 pa_context_unref(context
);
285 static ALCboolean
pulse_load(void) //{{{
287 ALCboolean ret
= ALC_TRUE
;
293 #define PALIB "libpulse-0.dll"
294 #elif defined(__APPLE__) && defined(__MACH__)
295 #define PALIB "libpulse.0.dylib"
297 #define PALIB "libpulse.so.0"
299 pa_handle
= LoadLib(PALIB
);
303 #define LOAD_FUNC(x) do { \
304 p##x = GetSymbol(pa_handle, #x); \
309 LOAD_FUNC(pa_context_unref
);
310 LOAD_FUNC(pa_sample_spec_valid
);
311 LOAD_FUNC(pa_stream_drop
);
312 LOAD_FUNC(pa_strerror
);
313 LOAD_FUNC(pa_context_get_state
);
314 LOAD_FUNC(pa_stream_get_state
);
315 LOAD_FUNC(pa_threaded_mainloop_signal
);
316 LOAD_FUNC(pa_stream_peek
);
317 LOAD_FUNC(pa_threaded_mainloop_wait
);
318 LOAD_FUNC(pa_threaded_mainloop_unlock
);
319 LOAD_FUNC(pa_threaded_mainloop_in_thread
);
320 LOAD_FUNC(pa_context_new
);
321 LOAD_FUNC(pa_threaded_mainloop_stop
);
322 LOAD_FUNC(pa_context_disconnect
);
323 LOAD_FUNC(pa_threaded_mainloop_start
);
324 LOAD_FUNC(pa_threaded_mainloop_get_api
);
325 LOAD_FUNC(pa_context_set_state_callback
);
326 LOAD_FUNC(pa_stream_write
);
328 LOAD_FUNC(pa_stream_connect_record
);
329 LOAD_FUNC(pa_stream_connect_playback
);
330 LOAD_FUNC(pa_stream_readable_size
);
331 LOAD_FUNC(pa_stream_writable_size
);
332 LOAD_FUNC(pa_stream_cork
);
333 LOAD_FUNC(pa_stream_is_suspended
);
334 LOAD_FUNC(pa_stream_get_device_name
);
335 LOAD_FUNC(pa_path_get_filename
);
336 LOAD_FUNC(pa_get_binary_name
);
337 LOAD_FUNC(pa_threaded_mainloop_free
);
338 LOAD_FUNC(pa_context_errno
);
339 LOAD_FUNC(pa_xmalloc
);
340 LOAD_FUNC(pa_stream_unref
);
341 LOAD_FUNC(pa_threaded_mainloop_accept
);
342 LOAD_FUNC(pa_stream_set_write_callback
);
343 LOAD_FUNC(pa_threaded_mainloop_new
);
344 LOAD_FUNC(pa_context_connect
);
345 LOAD_FUNC(pa_stream_set_buffer_attr
);
346 LOAD_FUNC(pa_stream_get_buffer_attr
);
347 LOAD_FUNC(pa_stream_get_sample_spec
);
348 LOAD_FUNC(pa_stream_get_time
);
349 LOAD_FUNC(pa_stream_set_read_callback
);
350 LOAD_FUNC(pa_stream_set_state_callback
);
351 LOAD_FUNC(pa_stream_set_moved_callback
);
352 LOAD_FUNC(pa_stream_set_underflow_callback
);
353 LOAD_FUNC(pa_stream_new
);
354 LOAD_FUNC(pa_stream_disconnect
);
355 LOAD_FUNC(pa_threaded_mainloop_lock
);
356 LOAD_FUNC(pa_channel_map_init_auto
);
357 LOAD_FUNC(pa_channel_map_parse
);
358 LOAD_FUNC(pa_channel_map_snprint
);
359 LOAD_FUNC(pa_channel_map_equal
);
360 LOAD_FUNC(pa_context_get_server_info
);
361 LOAD_FUNC(pa_context_get_sink_info_by_name
);
362 LOAD_FUNC(pa_context_get_sink_info_list
);
363 LOAD_FUNC(pa_context_get_source_info_list
);
364 LOAD_FUNC(pa_operation_get_state
);
365 LOAD_FUNC(pa_operation_unref
);
367 #define LOAD_OPTIONAL_FUNC(x) do { \
368 p##x = GetSymbol(pa_handle, #x); \
370 #if PA_CHECK_VERSION(0,9,15)
371 LOAD_OPTIONAL_FUNC(pa_channel_map_superset
);
372 LOAD_OPTIONAL_FUNC(pa_stream_set_buffer_attr_callback
);
374 #if PA_CHECK_VERSION(0,9,16)
375 LOAD_OPTIONAL_FUNC(pa_stream_begin_write
);
377 #undef LOAD_OPTIONAL_FUNC
379 #else /* HAVE_DYNLOAD */
380 pa_handle
= (void*)0xDEADBEEF;
394 // PulseAudio Event Callbacks //{{{
395 static void stream_state_callback(pa_stream
*stream
, void *pdata
) //{{{
397 pa_threaded_mainloop
*loop
= pdata
;
398 pa_stream_state_t state
;
400 state
= pa_stream_get_state(stream
);
401 if(state
== PA_STREAM_READY
|| !PA_STREAM_IS_GOOD(state
))
402 pa_threaded_mainloop_signal(loop
, 0);
405 static void stream_signal_callback(pa_stream
*stream
, void *pdata
) //{{{
407 ALCdevice
*Device
= pdata
;
408 pulse_data
*data
= Device
->ExtraData
;
411 pa_threaded_mainloop_signal(data
->loop
, 0);
414 static void stream_buffer_attr_callback(pa_stream
*stream
, void *pdata
) //{{{
416 const pa_buffer_attr
*attr
;
419 attr
= pa_stream_get_buffer_attr(stream
);
420 WARN("PulseAudio modified buffer length: minreq=%d, tlength=%d\n", attr
->minreq
, attr
->tlength
);
423 static void stream_device_callback(pa_stream
*stream
, void *pdata
) //{{{
425 ALCdevice
*Device
= pdata
;
426 pulse_data
*data
= Device
->ExtraData
;
428 free(data
->device_name
);
429 data
->device_name
= strdup(pa_stream_get_device_name(stream
));
432 static void context_state_callback2(pa_context
*context
, void *pdata
) //{{{
434 ALCdevice
*Device
= pdata
;
435 pulse_data
*data
= Device
->ExtraData
;
437 if(pa_context_get_state(context
) == PA_CONTEXT_FAILED
)
439 ERR("Received context failure!\n");
440 aluHandleDisconnect(Device
);
442 pa_threaded_mainloop_signal(data
->loop
, 0);
445 static void stream_state_callback2(pa_stream
*stream
, void *pdata
) //{{{
447 ALCdevice
*Device
= pdata
;
448 pulse_data
*data
= Device
->ExtraData
;
450 if(pa_stream_get_state(stream
) == PA_STREAM_FAILED
)
452 ERR("Received stream failure!\n");
453 aluHandleDisconnect(Device
);
455 pa_threaded_mainloop_signal(data
->loop
, 0);
458 static void stream_success_callback(pa_stream
*stream
, int success
, void *pdata
) //{{{
460 ALCdevice
*Device
= pdata
;
461 pulse_data
*data
= Device
->ExtraData
;
465 pa_threaded_mainloop_signal(data
->loop
, 0);
468 static void sink_info_callback(pa_context
*context
, const pa_sink_info
*info
, int eol
, void *pdata
) //{{{
470 ALCdevice
*device
= pdata
;
471 pulse_data
*data
= device
->ExtraData
;
472 char chanmap_str
[256] = "";
475 enum DevFmtChannels chans
;
477 { "front-left,front-right,front-center,lfe,rear-left,rear-right,side-left,side-right",
479 { "front-left,front-right,front-center,lfe,rear-center,side-left,side-right",
481 { "front-left,front-right,front-center,lfe,rear-left,rear-right",
483 { "front-left,front-right,front-center,lfe,side-left,side-right",
485 { "front-left,front-right,rear-left,rear-right", DevFmtQuad
},
486 { "front-left,front-right", DevFmtStereo
},
487 { "mono", DevFmtMono
},
495 pa_threaded_mainloop_signal(data
->loop
, 0);
499 for(i
= 0;chanmaps
[i
].str
;i
++)
502 if(!pa_channel_map_parse(&map
, chanmaps
[i
].str
))
505 if(pa_channel_map_equal(&info
->channel_map
, &map
)
506 #if PA_CHECK_VERSION(0,9,15)
507 || (pa_channel_map_superset
&&
508 pa_channel_map_superset(&info
->channel_map
, &map
))
512 device
->FmtChans
= chanmaps
[i
].chans
;
517 pa_channel_map_snprint(chanmap_str
, sizeof(chanmap_str
), &info
->channel_map
);
518 ERR("Failed to find format for channel map:\n %s\n", chanmap_str
);
521 static void sink_device_callback(pa_context
*context
, const pa_sink_info
*info
, int eol
, void *pdata
) //{{{
523 pa_threaded_mainloop
*loop
= pdata
;
533 pa_threaded_mainloop_signal(loop
, 0);
540 snprintf(str
, sizeof(str
), "%s", info
->description
);
542 snprintf(str
, sizeof(str
), "%s #%d", info
->description
, count
+1);
545 for(i
= 0;i
< numDevNames
;i
++)
547 if(strcmp(str
, allDevNameMap
[i
].name
) == 0)
550 } while(i
!= numDevNames
);
552 temp
= realloc(allDevNameMap
, (numDevNames
+1) * sizeof(*allDevNameMap
));
555 allDevNameMap
= temp
;
556 allDevNameMap
[numDevNames
].name
= strdup(str
);
557 allDevNameMap
[numDevNames
].device_name
= strdup(info
->name
);
562 static void source_device_callback(pa_context
*context
, const pa_source_info
*info
, int eol
, void *pdata
) //{{{
564 pa_threaded_mainloop
*loop
= pdata
;
574 pa_threaded_mainloop_signal(loop
, 0);
581 snprintf(str
, sizeof(str
), "%s", info
->description
);
583 snprintf(str
, sizeof(str
), "%s #%d", info
->description
, count
+1);
586 for(i
= 0;i
< numCaptureDevNames
;i
++)
588 if(strcmp(str
, allCaptureDevNameMap
[i
].name
) == 0)
591 } while(i
!= numCaptureDevNames
);
593 temp
= realloc(allCaptureDevNameMap
, (numCaptureDevNames
+1) * sizeof(*allCaptureDevNameMap
));
596 allCaptureDevNameMap
= temp
;
597 allCaptureDevNameMap
[numCaptureDevNames
].name
= strdup(str
);
598 allCaptureDevNameMap
[numCaptureDevNames
].device_name
= strdup(info
->name
);
599 numCaptureDevNames
++;
604 // PulseAudio I/O Callbacks //{{{
605 static void stream_write_callback(pa_stream
*stream
, size_t len
, void *pdata
) //{{{
607 ALCdevice
*Device
= pdata
;
608 pulse_data
*data
= Device
->ExtraData
;
612 pa_threaded_mainloop_signal(data
->loop
, 0);
616 static ALuint
PulseProc(ALvoid
*param
)
618 ALCdevice
*Device
= param
;
619 pulse_data
*data
= Device
->ExtraData
;
624 pa_threaded_mainloop_lock(data
->loop
);
626 len
= (Device
->Connected
? pa_stream_writable_size(data
->stream
) : 0);
627 len
-= len
%(Device
->UpdateSize
*data
->frame_size
);
630 pa_threaded_mainloop_wait(data
->loop
);
638 pa_free_cb_t free_func
= NULL
;
640 #if PA_CHECK_VERSION(0,9,16)
641 if(!pa_stream_begin_write
||
642 pa_stream_begin_write(data
->stream
, &buf
, &newlen
) < 0)
645 buf
= pa_xmalloc(newlen
);
646 free_func
= pa_xfree
;
648 pa_threaded_mainloop_unlock(data
->loop
);
650 aluMixData(Device
, buf
, newlen
/data
->frame_size
);
652 pa_threaded_mainloop_lock(data
->loop
);
653 pa_stream_write(data
->stream
, buf
, newlen
, free_func
, 0, PA_SEEK_RELATIVE
);
656 } while(Device
->Connected
&& !data
->killNow
);
657 pa_threaded_mainloop_unlock(data
->loop
);
662 static pa_stream
*connect_playback_stream(ALCdevice
*device
,
663 pa_stream_flags_t flags
, pa_buffer_attr
*attr
, pa_sample_spec
*spec
,
664 pa_channel_map
*chanmap
)
666 pulse_data
*data
= device
->ExtraData
;
667 pa_stream_state_t state
;
670 stream
= pa_stream_new(data
->context
, "Playback Stream", spec
, chanmap
);
673 ERR("pa_stream_new() failed: %s\n",
674 pa_strerror(pa_context_errno(data
->context
)));
678 pa_stream_set_state_callback(stream
, stream_state_callback
, data
->loop
);
680 if(pa_stream_connect_playback(stream
, data
->device_name
, attr
, flags
, NULL
, NULL
) < 0)
682 ERR("Stream did not connect: %s\n",
683 pa_strerror(pa_context_errno(data
->context
)));
684 pa_stream_unref(stream
);
688 while((state
=pa_stream_get_state(stream
)) != PA_STREAM_READY
)
690 if(!PA_STREAM_IS_GOOD(state
))
692 ERR("Stream did not get ready: %s\n",
693 pa_strerror(pa_context_errno(data
->context
)));
694 pa_stream_unref(stream
);
698 pa_threaded_mainloop_wait(data
->loop
);
700 pa_stream_set_state_callback(stream
, NULL
, NULL
);
705 static void probe_devices(ALboolean capture
)
707 pa_threaded_mainloop
*loop
;
709 if(capture
== AL_FALSE
)
710 allDevNameMap
= malloc(sizeof(DevMap
) * 1);
712 allCaptureDevNameMap
= malloc(sizeof(DevMap
) * 1);
714 if((loop
=pa_threaded_mainloop_new()) &&
715 pa_threaded_mainloop_start(loop
) >= 0)
719 pa_threaded_mainloop_lock(loop
);
720 context
= connect_context(loop
, AL_FALSE
);
725 if(capture
== AL_FALSE
)
726 o
= pa_context_get_sink_info_list(context
, sink_device_callback
, loop
);
728 o
= pa_context_get_source_info_list(context
, source_device_callback
, loop
);
729 while(pa_operation_get_state(o
) == PA_OPERATION_RUNNING
)
730 pa_threaded_mainloop_wait(loop
);
731 pa_operation_unref(o
);
733 pa_context_disconnect(context
);
734 pa_context_unref(context
);
736 pa_threaded_mainloop_unlock(loop
);
737 pa_threaded_mainloop_stop(loop
);
740 pa_threaded_mainloop_free(loop
);
744 static ALCboolean
pulse_open(ALCdevice
*device
, const ALCchar
*device_name
) //{{{
746 pulse_data
*data
= pa_xmalloc(sizeof(pulse_data
));
747 memset(data
, 0, sizeof(*data
));
749 if(!(data
->loop
= pa_threaded_mainloop_new()))
751 ERR("pa_threaded_mainloop_new() failed!\n");
754 if(pa_threaded_mainloop_start(data
->loop
) < 0)
756 ERR("pa_threaded_mainloop_start() failed\n");
760 pa_threaded_mainloop_lock(data
->loop
);
761 device
->ExtraData
= data
;
763 data
->context
= connect_context(data
->loop
, AL_FALSE
);
766 pa_threaded_mainloop_unlock(data
->loop
);
769 pa_context_set_state_callback(data
->context
, context_state_callback2
, device
);
771 device
->szDeviceName
= strdup(device_name
);
773 pa_threaded_mainloop_unlock(data
->loop
);
779 pa_threaded_mainloop_stop(data
->loop
);
780 pa_threaded_mainloop_free(data
->loop
);
783 device
->ExtraData
= NULL
;
788 static void pulse_close(ALCdevice
*device
) //{{{
790 pulse_data
*data
= device
->ExtraData
;
792 pa_threaded_mainloop_lock(data
->loop
);
796 pa_stream_disconnect(data
->stream
);
797 pa_stream_unref(data
->stream
);
800 pa_context_disconnect(data
->context
);
801 pa_context_unref(data
->context
);
803 pa_threaded_mainloop_unlock(data
->loop
);
805 pa_threaded_mainloop_stop(data
->loop
);
806 pa_threaded_mainloop_free(data
->loop
);
808 DestroyRingBuffer(data
->ring
);
809 free(data
->device_name
);
811 device
->ExtraData
= NULL
;
817 static ALCenum
pulse_open_playback(ALCdevice
*device
, const ALCchar
*device_name
) //{{{
819 char *pulse_name
= NULL
;
824 probe_devices(AL_FALSE
);
827 device_name
= pulse_device
;
828 else if(strcmp(device_name
, pulse_device
) != 0)
832 for(i
= 0;i
< numDevNames
;i
++)
834 if(strcmp(device_name
, allDevNameMap
[i
].name
) == 0)
836 pulse_name
= allDevNameMap
[i
].device_name
;
841 return ALC_INVALID_VALUE
;
844 if(pulse_open(device
, device_name
) == ALC_FALSE
)
845 return ALC_INVALID_VALUE
;
847 data
= device
->ExtraData
;
849 pa_threaded_mainloop_lock(data
->loop
);
851 spec
.format
= PA_SAMPLE_S16NE
;
855 data
->device_name
= pulse_name
;
856 pa_stream
*stream
= connect_playback_stream(device
, 0, NULL
, &spec
, NULL
);
859 pa_threaded_mainloop_unlock(data
->loop
);
863 if(pa_stream_is_suspended(stream
))
865 ERR("Device is suspended\n");
866 pa_stream_disconnect(stream
);
867 pa_stream_unref(stream
);
868 pa_threaded_mainloop_unlock(data
->loop
);
871 data
->device_name
= strdup(pa_stream_get_device_name(stream
));
873 pa_stream_disconnect(stream
);
874 pa_stream_unref(stream
);
876 pa_threaded_mainloop_unlock(data
->loop
);
882 return ALC_INVALID_VALUE
;
885 static void pulse_close_playback(ALCdevice
*device
) //{{{
890 static ALCboolean
pulse_reset_playback(ALCdevice
*device
) //{{{
892 pulse_data
*data
= device
->ExtraData
;
893 pa_stream_flags_t flags
= 0;
894 pa_channel_map chanmap
;
896 pa_threaded_mainloop_lock(data
->loop
);
898 if(!(device
->Flags
&DEVICE_CHANNELS_REQUEST
))
901 o
= pa_context_get_sink_info_by_name(data
->context
, data
->device_name
, sink_info_callback
, device
);
902 while(pa_operation_get_state(o
) == PA_OPERATION_RUNNING
)
903 pa_threaded_mainloop_wait(data
->loop
);
904 pa_operation_unref(o
);
906 if(!(device
->Flags
&DEVICE_FREQUENCY_REQUEST
))
907 flags
|= PA_STREAM_FIX_RATE
;
909 data
->frame_size
= FrameSizeFromDevFmt(device
->FmtChans
, device
->FmtType
);
910 data
->attr
.prebuf
= -1;
911 data
->attr
.fragsize
= -1;
912 data
->attr
.minreq
= device
->UpdateSize
* data
->frame_size
;
913 data
->attr
.tlength
= data
->attr
.minreq
* maxu(device
->NumUpdates
, 2);
914 data
->attr
.maxlength
= -1;
915 flags
|= PA_STREAM_EARLY_REQUESTS
;
916 flags
|= PA_STREAM_INTERPOLATE_TIMING
| PA_STREAM_AUTO_TIMING_UPDATE
;
918 switch(device
->FmtType
)
921 device
->FmtType
= DevFmtUByte
;
924 data
->spec
.format
= PA_SAMPLE_U8
;
927 device
->FmtType
= DevFmtShort
;
930 data
->spec
.format
= PA_SAMPLE_S16NE
;
933 device
->FmtType
= DevFmtInt
;
936 data
->spec
.format
= PA_SAMPLE_S32NE
;
939 data
->spec
.format
= PA_SAMPLE_FLOAT32NE
;
942 data
->spec
.rate
= device
->Frequency
;
943 data
->spec
.channels
= ChannelsFromDevFmt(device
->FmtChans
);
945 if(pa_sample_spec_valid(&data
->spec
) == 0)
947 ERR("Invalid sample format\n");
948 pa_threaded_mainloop_unlock(data
->loop
);
952 if(!pa_channel_map_init_auto(&chanmap
, data
->spec
.channels
, PA_CHANNEL_MAP_WAVEEX
))
954 ERR("Couldn't build map for channel count (%d)!\n", data
->spec
.channels
);
955 pa_threaded_mainloop_unlock(data
->loop
);
958 SetDefaultWFXChannelOrder(device
);
960 data
->stream
= connect_playback_stream(device
, flags
, &data
->attr
, &data
->spec
, &chanmap
);
963 pa_threaded_mainloop_unlock(data
->loop
);
967 pa_stream_set_state_callback(data
->stream
, stream_state_callback2
, device
);
969 data
->spec
= *(pa_stream_get_sample_spec(data
->stream
));
970 if(device
->Frequency
!= data
->spec
.rate
)
974 /* Server updated our playback rate, so modify the buffer attribs
976 data
->attr
.minreq
= (ALuint64
)(data
->attr
.minreq
/data
->frame_size
) *
977 data
->spec
.rate
/ device
->Frequency
* data
->frame_size
;
978 data
->attr
.tlength
= data
->attr
.minreq
* maxu(device
->NumUpdates
, 2);
980 o
= pa_stream_set_buffer_attr(data
->stream
, &data
->attr
,
981 stream_success_callback
, device
);
982 while(pa_operation_get_state(o
) == PA_OPERATION_RUNNING
)
983 pa_threaded_mainloop_wait(data
->loop
);
984 pa_operation_unref(o
);
986 device
->Frequency
= data
->spec
.rate
;
989 #if PA_CHECK_VERSION(0,9,15)
990 if(pa_stream_set_buffer_attr_callback
)
991 pa_stream_set_buffer_attr_callback(data
->stream
, stream_buffer_attr_callback
, device
);
993 pa_stream_set_moved_callback(data
->stream
, stream_device_callback
, device
);
994 pa_stream_set_write_callback(data
->stream
, stream_write_callback
, device
);
995 pa_stream_set_underflow_callback(data
->stream
, stream_signal_callback
, device
);
997 data
->attr
= *(pa_stream_get_buffer_attr(data
->stream
));
998 device
->UpdateSize
= data
->attr
.minreq
/ data
->frame_size
;
999 device
->NumUpdates
= (data
->attr
.tlength
/data
->frame_size
) / device
->UpdateSize
;
1000 if(device
->NumUpdates
<= 1)
1004 ERR("PulseAudio returned minreq=%d, tlength=%d; expect lag or break up\n", data
->attr
.minreq
, data
->attr
.tlength
);
1006 /* Server gave a comparatively large minreq, so modify the tlength. */
1007 device
->NumUpdates
= 2;
1008 data
->attr
.tlength
= data
->attr
.minreq
* device
->NumUpdates
;
1010 o
= pa_stream_set_buffer_attr(data
->stream
, &data
->attr
,
1011 stream_success_callback
, device
);
1012 while(pa_operation_get_state(o
) == PA_OPERATION_RUNNING
)
1013 pa_threaded_mainloop_wait(data
->loop
);
1014 pa_operation_unref(o
);
1017 data
->thread
= StartThread(PulseProc
, device
);
1020 #if PA_CHECK_VERSION(0,9,15)
1021 if(pa_stream_set_buffer_attr_callback
)
1022 pa_stream_set_buffer_attr_callback(data
->stream
, NULL
, NULL
);
1024 pa_stream_set_moved_callback(data
->stream
, NULL
, NULL
);
1025 pa_stream_set_write_callback(data
->stream
, NULL
, NULL
);
1026 pa_stream_set_underflow_callback(data
->stream
, NULL
, NULL
);
1027 pa_stream_disconnect(data
->stream
);
1028 pa_stream_unref(data
->stream
);
1029 data
->stream
= NULL
;
1031 pa_threaded_mainloop_unlock(data
->loop
);
1035 pa_threaded_mainloop_unlock(data
->loop
);
1039 static void pulse_stop_playback(ALCdevice
*device
) //{{{
1041 pulse_data
*data
= device
->ExtraData
;
1046 data
->killNow
= AL_TRUE
;
1049 pa_threaded_mainloop_signal(data
->loop
, 0);
1050 StopThread(data
->thread
);
1051 data
->thread
= NULL
;
1053 data
->killNow
= AL_FALSE
;
1055 pa_threaded_mainloop_lock(data
->loop
);
1057 #if PA_CHECK_VERSION(0,9,15)
1058 if(pa_stream_set_buffer_attr_callback
)
1059 pa_stream_set_buffer_attr_callback(data
->stream
, NULL
, NULL
);
1061 pa_stream_set_moved_callback(data
->stream
, NULL
, NULL
);
1062 pa_stream_set_write_callback(data
->stream
, NULL
, NULL
);
1063 pa_stream_set_underflow_callback(data
->stream
, NULL
, NULL
);
1064 pa_stream_disconnect(data
->stream
);
1065 pa_stream_unref(data
->stream
);
1066 data
->stream
= NULL
;
1068 pa_threaded_mainloop_unlock(data
->loop
);
1072 static ALCenum
pulse_open_capture(ALCdevice
*device
, const ALCchar
*device_name
) //{{{
1074 char *pulse_name
= NULL
;
1076 pa_stream_flags_t flags
= 0;
1077 pa_stream_state_t state
;
1078 pa_channel_map chanmap
;
1080 if(!allCaptureDevNameMap
)
1081 probe_devices(AL_TRUE
);
1083 if(!device_name
&& numCaptureDevNames
> 0)
1085 device_name
= allCaptureDevNameMap
[0].name
;
1086 pulse_name
= allCaptureDevNameMap
[0].device_name
;
1091 for(i
= 0;i
< numCaptureDevNames
;i
++)
1093 if(strcmp(device_name
, allCaptureDevNameMap
[i
].name
) == 0)
1095 pulse_name
= allCaptureDevNameMap
[i
].device_name
;
1099 if(i
== numCaptureDevNames
)
1100 return ALC_INVALID_VALUE
;
1103 if(pulse_open(device
, device_name
) == ALC_FALSE
)
1104 return ALC_INVALID_VALUE
;
1106 data
= device
->ExtraData
;
1107 pa_threaded_mainloop_lock(data
->loop
);
1109 data
->samples
= device
->UpdateSize
* device
->NumUpdates
;
1110 data
->frame_size
= FrameSizeFromDevFmt(device
->FmtChans
, device
->FmtType
);
1111 data
->samples
= maxu(data
->samples
, 100 * device
->Frequency
/ 1000);
1113 if(!(data
->ring
= CreateRingBuffer(data
->frame_size
, data
->samples
)))
1115 pa_threaded_mainloop_unlock(data
->loop
);
1119 data
->attr
.minreq
= -1;
1120 data
->attr
.prebuf
= -1;
1121 data
->attr
.maxlength
= data
->samples
* data
->frame_size
;
1122 data
->attr
.tlength
= -1;
1123 data
->attr
.fragsize
= minu(data
->samples
, 50*device
->Frequency
/1000) *
1126 data
->spec
.rate
= device
->Frequency
;
1127 data
->spec
.channels
= ChannelsFromDevFmt(device
->FmtChans
);
1129 switch(device
->FmtType
)
1132 data
->spec
.format
= PA_SAMPLE_U8
;
1135 data
->spec
.format
= PA_SAMPLE_S16NE
;
1138 data
->spec
.format
= PA_SAMPLE_S32NE
;
1141 data
->spec
.format
= PA_SAMPLE_FLOAT32NE
;
1146 ERR("%s capture samples not supported\n", DevFmtTypeString(device
->FmtType
));
1147 pa_threaded_mainloop_unlock(data
->loop
);
1151 if(pa_sample_spec_valid(&data
->spec
) == 0)
1153 ERR("Invalid sample format\n");
1154 pa_threaded_mainloop_unlock(data
->loop
);
1158 if(!pa_channel_map_init_auto(&chanmap
, data
->spec
.channels
, PA_CHANNEL_MAP_WAVEEX
))
1160 ERR("Couldn't build map for channel count (%d)!\n", data
->spec
.channels
);
1161 pa_threaded_mainloop_unlock(data
->loop
);
1165 data
->stream
= pa_stream_new(data
->context
, "Capture Stream", &data
->spec
, &chanmap
);
1168 ERR("pa_stream_new() failed: %s\n",
1169 pa_strerror(pa_context_errno(data
->context
)));
1171 pa_threaded_mainloop_unlock(data
->loop
);
1175 pa_stream_set_state_callback(data
->stream
, stream_state_callback
, data
->loop
);
1177 flags
|= PA_STREAM_START_CORKED
|PA_STREAM_ADJUST_LATENCY
;
1178 if(pa_stream_connect_record(data
->stream
, pulse_name
, &data
->attr
, flags
) < 0)
1180 ERR("Stream did not connect: %s\n",
1181 pa_strerror(pa_context_errno(data
->context
)));
1183 pa_stream_unref(data
->stream
);
1184 data
->stream
= NULL
;
1186 pa_threaded_mainloop_unlock(data
->loop
);
1190 while((state
=pa_stream_get_state(data
->stream
)) != PA_STREAM_READY
)
1192 if(!PA_STREAM_IS_GOOD(state
))
1194 ERR("Stream did not get ready: %s\n",
1195 pa_strerror(pa_context_errno(data
->context
)));
1197 pa_stream_unref(data
->stream
);
1198 data
->stream
= NULL
;
1200 pa_threaded_mainloop_unlock(data
->loop
);
1204 pa_threaded_mainloop_wait(data
->loop
);
1206 pa_stream_set_state_callback(data
->stream
, stream_state_callback2
, device
);
1208 pa_threaded_mainloop_unlock(data
->loop
);
1209 return ALC_NO_ERROR
;
1212 pulse_close(device
);
1213 return ALC_INVALID_VALUE
;
1216 static void pulse_close_capture(ALCdevice
*device
) //{{{
1218 pulse_close(device
);
1221 static void pulse_start_capture(ALCdevice
*device
) //{{{
1223 pulse_data
*data
= device
->ExtraData
;
1226 pa_threaded_mainloop_lock(data
->loop
);
1227 o
= pa_stream_cork(data
->stream
, 0, stream_success_callback
, device
);
1228 while(pa_operation_get_state(o
) == PA_OPERATION_RUNNING
)
1229 pa_threaded_mainloop_wait(data
->loop
);
1230 pa_operation_unref(o
);
1231 pa_threaded_mainloop_unlock(data
->loop
);
1234 static void pulse_stop_capture(ALCdevice
*device
) //{{{
1236 pulse_data
*data
= device
->ExtraData
;
1239 pa_threaded_mainloop_lock(data
->loop
);
1240 o
= pa_stream_cork(data
->stream
, 1, stream_success_callback
, device
);
1241 while(pa_operation_get_state(o
) == PA_OPERATION_RUNNING
)
1242 pa_threaded_mainloop_wait(data
->loop
);
1243 pa_operation_unref(o
);
1244 pa_threaded_mainloop_unlock(data
->loop
);
1247 static ALCenum
pulse_capture_samples(ALCdevice
*device
, ALCvoid
*buffer
, ALCuint samples
) //{{{
1249 pulse_data
*data
= device
->ExtraData
;
1250 ReadRingBuffer(data
->ring
, buffer
, samples
);
1251 return ALC_NO_ERROR
;
1254 static ALCuint
pulse_available_samples(ALCdevice
*device
) //{{{
1256 pulse_data
*data
= device
->ExtraData
;
1259 pa_threaded_mainloop_lock(data
->loop
);
1260 /* Capture is done in fragment-sized chunks, so we loop until we get all
1261 * that's available */
1262 samples
= (device
->Connected
? pa_stream_readable_size(data
->stream
) : 0);
1268 if(pa_stream_peek(data
->stream
, &buf
, &length
) < 0)
1270 ERR("pa_stream_peek() failed: %s\n",
1271 pa_strerror(pa_context_errno(data
->context
)));
1275 WriteRingBuffer(data
->ring
, buf
, length
/data
->frame_size
);
1278 pa_stream_drop(data
->stream
);
1280 pa_threaded_mainloop_unlock(data
->loop
);
1282 return RingBufferSize(data
->ring
);
1286 static const BackendFuncs pulse_funcs
= { //{{{
1287 pulse_open_playback
,
1288 pulse_close_playback
,
1289 pulse_reset_playback
,
1290 pulse_stop_playback
,
1292 pulse_close_capture
,
1293 pulse_start_capture
,
1295 pulse_capture_samples
,
1296 pulse_available_samples
1299 ALCboolean
alc_pulse_init(BackendFuncs
*func_list
) //{{{
1301 ALCboolean ret
= ALC_FALSE
;
1305 pa_threaded_mainloop
*loop
;
1307 pulse_ctx_flags
= 0;
1308 if(!GetConfigValueBool("pulse", "spawn-server", 0))
1309 pulse_ctx_flags
|= PA_CONTEXT_NOAUTOSPAWN
;
1311 if((loop
=pa_threaded_mainloop_new()) &&
1312 pa_threaded_mainloop_start(loop
) >= 0)
1314 pa_context
*context
;
1316 pa_threaded_mainloop_lock(loop
);
1317 context
= connect_context(loop
, AL_TRUE
);
1322 pa_context_disconnect(context
);
1323 pa_context_unref(context
);
1325 pa_threaded_mainloop_unlock(loop
);
1326 pa_threaded_mainloop_stop(loop
);
1329 pa_threaded_mainloop_free(loop
);
1332 if(ret
!= ALC_FALSE
)
1333 *func_list
= pulse_funcs
;
1337 void alc_pulse_deinit(void) //{{{
1341 for(i
= 0;i
< numDevNames
;++i
)
1343 free(allDevNameMap
[i
].name
);
1344 free(allDevNameMap
[i
].device_name
);
1346 free(allDevNameMap
);
1347 allDevNameMap
= NULL
;
1350 for(i
= 0;i
< numCaptureDevNames
;++i
)
1352 free(allCaptureDevNameMap
[i
].name
);
1353 free(allCaptureDevNameMap
[i
].device_name
);
1355 free(allCaptureDevNameMap
);
1356 allCaptureDevNameMap
= NULL
;
1357 numCaptureDevNames
= 0;
1361 CloseLib(pa_handle
);
1366 void alc_pulse_probe(enum DevProbe type
) //{{{
1368 pa_threaded_mainloop
*loop
;
1374 if((loop
=pa_threaded_mainloop_new()) &&
1375 pa_threaded_mainloop_start(loop
) >= 0)
1377 pa_context
*context
;
1379 pa_threaded_mainloop_lock(loop
);
1380 context
= connect_context(loop
, AL_FALSE
);
1383 AppendDeviceList(pulse_device
);
1385 pa_context_disconnect(context
);
1386 pa_context_unref(context
);
1388 pa_threaded_mainloop_unlock(loop
);
1389 pa_threaded_mainloop_stop(loop
);
1392 pa_threaded_mainloop_free(loop
);
1395 case ALL_DEVICE_PROBE
:
1396 for(i
= 0;i
< numDevNames
;++i
)
1398 free(allDevNameMap
[i
].name
);
1399 free(allDevNameMap
[i
].device_name
);
1401 free(allDevNameMap
);
1402 allDevNameMap
= NULL
;
1405 probe_devices(AL_FALSE
);
1407 for(i
= 0;i
< numDevNames
;i
++)
1408 AppendAllDeviceList(allDevNameMap
[i
].name
);
1411 case CAPTURE_DEVICE_PROBE
:
1412 for(i
= 0;i
< numCaptureDevNames
;++i
)
1414 free(allCaptureDevNameMap
[i
].name
);
1415 free(allCaptureDevNameMap
[i
].device_name
);
1417 free(allCaptureDevNameMap
);
1418 allCaptureDevNameMap
= NULL
;
1419 numCaptureDevNames
= 0;
1421 probe_devices(AL_TRUE
);
1423 for(i
= 0;i
< numCaptureDevNames
;i
++)
1424 AppendCaptureDeviceList(allCaptureDevNameMap
[i
].name
);