2 * OpenAL cross platform audio library
3 * Copyright (C) 2009 by Konstantinos Natsakis <konstantinos.natsakis@gmail.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
28 #include <pulse/pulseaudio.h>
30 #if PA_API_VERSION == 11
31 #define PA_STREAM_ADJUST_LATENCY 0x2000U
32 static inline int PA_STREAM_IS_GOOD(pa_stream_state_t x
)
34 return (x
== PA_STREAM_CREATING
|| x
== PA_STREAM_READY
);
36 static inline int PA_CONTEXT_IS_GOOD(pa_context_state_t x
)
38 return (x
== PA_CONTEXT_CONNECTING
|| x
== PA_CONTEXT_AUTHORIZING
||
39 x
== PA_CONTEXT_SETTING_NAME
|| x
== PA_CONTEXT_READY
);
41 #define PA_STREAM_IS_GOOD PA_STREAM_IS_GOOD
42 #define PA_CONTEXT_IS_GOOD PA_CONTEXT_IS_GOOD
43 #elif PA_API_VERSION != 12
44 #error Invalid PulseAudio API version
47 #ifndef PA_CHECK_VERSION
48 #define PA_CHECK_VERSION(major,minor,micro) \
49 ((PA_MAJOR > (major)) || \
50 (PA_MAJOR == (major) && PA_MINOR > (minor)) || \
51 (PA_MAJOR == (major) && PA_MINOR == (minor) && PA_MICRO >= (micro)))
54 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_cork
);
79 MAKE_FUNC(pa_path_get_filename
);
80 MAKE_FUNC(pa_get_binary_name
);
81 MAKE_FUNC(pa_threaded_mainloop_free
);
82 MAKE_FUNC(pa_context_errno
);
83 MAKE_FUNC(pa_xmalloc
);
84 MAKE_FUNC(pa_stream_unref
);
85 MAKE_FUNC(pa_threaded_mainloop_accept
);
86 MAKE_FUNC(pa_stream_set_write_callback
);
87 MAKE_FUNC(pa_threaded_mainloop_new
);
88 MAKE_FUNC(pa_context_connect
);
89 MAKE_FUNC(pa_stream_set_buffer_attr
);
90 MAKE_FUNC(pa_stream_get_buffer_attr
);
91 MAKE_FUNC(pa_stream_get_sample_spec
);
92 MAKE_FUNC(pa_stream_set_read_callback
);
93 MAKE_FUNC(pa_stream_set_state_callback
);
94 MAKE_FUNC(pa_stream_new
);
95 MAKE_FUNC(pa_stream_disconnect
);
96 MAKE_FUNC(pa_threaded_mainloop_lock
);
97 MAKE_FUNC(pa_channel_map_init_auto
);
98 MAKE_FUNC(pa_channel_map_parse
);
99 MAKE_FUNC(pa_operation_get_state
);
100 MAKE_FUNC(pa_operation_unref
);
101 #if PA_CHECK_VERSION(0,9,15)
102 MAKE_FUNC(pa_stream_set_buffer_attr_callback
);
104 #if PA_CHECK_VERSION(0,9,16)
105 MAKE_FUNC(pa_stream_begin_write
);
110 #define PATH_MAX 4096
122 char path_name
[PATH_MAX
];
123 const char *context_name
;
124 const char *stream_name
;
126 pa_threaded_mainloop
*loop
;
132 static const ALCchar pulse_device
[] = "PulseAudio Software";
133 static const ALCchar pulse_capture_device
[] = "PulseAudio Capture";
134 static volatile ALuint load_count
;
137 void *pulse_load(void) //{{{
142 pa_handle
= LoadLibrary("libpulse-0.dll");
143 #define LOAD_FUNC(x) do { \
144 p##x = GetProcAddress(pa_handle, #x); \
146 AL_PRINT("Could not load %s from libpulse-0.dll\n", #x); \
147 FreeLibrary(pa_handle); \
152 #define LOAD_OPTIONAL_FUNC(x) do { \
153 p##x = GetProcAddress(pa_handle, #x); \
156 #elif defined (HAVE_DLFCN_H)
159 #if defined(__APPLE__) && defined(__MACH__)
160 pa_handle
= dlopen("libpulse.0.dylib", RTLD_NOW
);
162 pa_handle
= dlopen("libpulse.so.0", RTLD_NOW
);
166 #define LOAD_FUNC(x) do { \
167 p##x = dlsym(pa_handle, #x); \
168 if((err=dlerror()) != NULL) { \
169 AL_PRINT("Could not load %s from libpulse: %s\n", #x, err); \
170 dlclose(pa_handle); \
175 #define LOAD_OPTIONAL_FUNC(x) do { \
176 p##x = dlsym(pa_handle, #x); \
177 if((err=dlerror()) != NULL) { \
184 pa_handle
= (void*)0xDEADBEEF;
185 #define LOAD_FUNC(x) p##x = (x)
186 #define LOAD_OPTIONAL_FUNC(x) p##x = (x)
192 LOAD_FUNC(pa_context_unref
);
193 LOAD_FUNC(pa_sample_spec_valid
);
194 LOAD_FUNC(pa_stream_drop
);
195 LOAD_FUNC(pa_strerror
);
196 LOAD_FUNC(pa_context_get_state
);
197 LOAD_FUNC(pa_stream_get_state
);
198 LOAD_FUNC(pa_threaded_mainloop_signal
);
199 LOAD_FUNC(pa_stream_peek
);
200 LOAD_FUNC(pa_threaded_mainloop_wait
);
201 LOAD_FUNC(pa_threaded_mainloop_unlock
);
202 LOAD_FUNC(pa_threaded_mainloop_in_thread
);
203 LOAD_FUNC(pa_context_new
);
204 LOAD_FUNC(pa_threaded_mainloop_stop
);
205 LOAD_FUNC(pa_context_disconnect
);
206 LOAD_FUNC(pa_threaded_mainloop_start
);
207 LOAD_FUNC(pa_threaded_mainloop_get_api
);
208 LOAD_FUNC(pa_context_set_state_callback
);
209 LOAD_FUNC(pa_stream_write
);
211 LOAD_FUNC(pa_stream_connect_record
);
212 LOAD_FUNC(pa_stream_connect_playback
);
213 LOAD_FUNC(pa_stream_readable_size
);
214 LOAD_FUNC(pa_stream_cork
);
215 LOAD_FUNC(pa_path_get_filename
);
216 LOAD_FUNC(pa_get_binary_name
);
217 LOAD_FUNC(pa_threaded_mainloop_free
);
218 LOAD_FUNC(pa_context_errno
);
219 LOAD_FUNC(pa_xmalloc
);
220 LOAD_FUNC(pa_stream_unref
);
221 LOAD_FUNC(pa_threaded_mainloop_accept
);
222 LOAD_FUNC(pa_stream_set_write_callback
);
223 LOAD_FUNC(pa_threaded_mainloop_new
);
224 LOAD_FUNC(pa_context_connect
);
225 LOAD_FUNC(pa_stream_set_buffer_attr
);
226 LOAD_FUNC(pa_stream_get_buffer_attr
);
227 LOAD_FUNC(pa_stream_get_sample_spec
);
228 LOAD_FUNC(pa_stream_set_read_callback
);
229 LOAD_FUNC(pa_stream_set_state_callback
);
230 LOAD_FUNC(pa_stream_new
);
231 LOAD_FUNC(pa_stream_disconnect
);
232 LOAD_FUNC(pa_threaded_mainloop_lock
);
233 LOAD_FUNC(pa_channel_map_init_auto
);
234 LOAD_FUNC(pa_channel_map_parse
);
235 LOAD_FUNC(pa_operation_get_state
);
236 LOAD_FUNC(pa_operation_unref
);
237 #if PA_CHECK_VERSION(0,9,15)
238 LOAD_OPTIONAL_FUNC(pa_stream_set_buffer_attr_callback
);
240 #if PA_CHECK_VERSION(0,9,16)
241 LOAD_OPTIONAL_FUNC(pa_stream_begin_write
);
244 #undef LOAD_OPTIONAL_FUNC
252 void pulse_unload(void) //{{{
254 if(load_count
== 0 || --load_count
> 0)
258 FreeLibrary(pa_handle
);
259 #elif defined (HAVE_DLFCN_H)
266 // PulseAudio Event Callbacks //{{{
267 static void context_state_callback(pa_context
*context
, void *pdata
) //{{{
269 ALCdevice
*Device
= pdata
;
270 pulse_data
*data
= Device
->ExtraData
;
271 pa_context_state_t state
;
273 state
= ppa_context_get_state(context
);
274 if(state
== PA_CONTEXT_READY
|| !PA_CONTEXT_IS_GOOD(state
))
276 if(ppa_threaded_mainloop_in_thread(data
->loop
))
277 ppa_threaded_mainloop_signal(data
->loop
, 0);
281 static void stream_state_callback(pa_stream
*stream
, void *pdata
) //{{{
283 ALCdevice
*Device
= pdata
;
284 pulse_data
*data
= Device
->ExtraData
;
285 pa_stream_state_t state
;
287 state
= ppa_stream_get_state(stream
);
288 if(state
== PA_STREAM_READY
|| !PA_STREAM_IS_GOOD(state
))
290 if(ppa_threaded_mainloop_in_thread(data
->loop
))
291 ppa_threaded_mainloop_signal(data
->loop
, 0);
295 static void stream_buffer_attr_callback(pa_stream
*stream
, void *pdata
) //{{{
297 ALCdevice
*Device
= pdata
;
298 pulse_data
*data
= Device
->ExtraData
;
300 SuspendContext(NULL
);
302 data
->attr
= *(ppa_stream_get_buffer_attr(stream
));
303 if(data
->attr
.tlength
< data
->attr
.minreq
*2)
304 AL_PRINT("new tlength (%d) is smaller than two periods (%d x 2)!\n",
305 data
->attr
.tlength
, data
->attr
.minreq
);
306 Device
->UpdateSize
= data
->attr
.minreq
/ data
->frame_size
;
307 Device
->NumUpdates
= data
->attr
.tlength
/data
->attr
.minreq
;
309 ProcessContext(NULL
);
312 static void context_state_callback2(pa_context
*context
, void *pdata
) //{{{
314 ALCdevice
*Device
= pdata
;
316 if(ppa_context_get_state(context
) == PA_CONTEXT_FAILED
)
318 AL_PRINT("Received context failure!\n");
319 aluHandleDisconnect(Device
);
323 static void stream_state_callback2(pa_stream
*stream
, void *pdata
) //{{{
325 ALCdevice
*Device
= pdata
;
327 if(ppa_stream_get_state(stream
) == PA_STREAM_FAILED
)
329 AL_PRINT("Received stream failure!\n");
330 aluHandleDisconnect(Device
);
334 static void stream_success_callback(pa_stream
*stream
, int success
, void *pdata
) //{{{
336 ALCdevice
*Device
= pdata
;
337 pulse_data
*data
= Device
->ExtraData
;
341 if(ppa_threaded_mainloop_in_thread(data
->loop
))
342 ppa_threaded_mainloop_signal(data
->loop
, 0);
346 // PulseAudio I/O Callbacks //{{{
347 static void stream_write_callback(pa_stream
*stream
, size_t len
, void *pdata
) //{{{
349 ALCdevice
*Device
= pdata
;
350 pulse_data
*data
= Device
->ExtraData
;
352 len
-= len
%data
->attr
.minreq
;
356 pa_free_cb_t free_func
= NULL
;
358 #if PA_CHECK_VERSION(0,9,16)
359 if(!ppa_stream_begin_write
||
360 ppa_stream_begin_write(stream
, &buf
, &len
) < 0)
363 buf
= ppa_xmalloc(len
);
364 free_func
= ppa_xfree
;
367 aluMixData(Device
, buf
, len
/data
->frame_size
);
368 ppa_stream_write(stream
, buf
, len
, free_func
, 0, PA_SEEK_RELATIVE
);
373 static ALCboolean
pulse_open(ALCdevice
*device
, const ALCchar
*device_name
) //{{{
375 pulse_data
*data
= ppa_xmalloc(sizeof(pulse_data
));
376 pa_context_state_t state
;
378 memset(data
, 0, sizeof(*data
));
380 if(ppa_get_binary_name(data
->path_name
, sizeof(data
->path_name
)))
381 data
->context_name
= ppa_path_get_filename(data
->path_name
);
383 data
->context_name
= "OpenAL Soft";
385 if(!(data
->loop
= ppa_threaded_mainloop_new()))
387 AL_PRINT("pa_threaded_mainloop_new() failed!\n");
391 if(ppa_threaded_mainloop_start(data
->loop
) < 0)
393 AL_PRINT("pa_threaded_mainloop_start() failed\n");
397 ppa_threaded_mainloop_lock(data
->loop
);
398 device
->ExtraData
= data
;
400 data
->context
= ppa_context_new(ppa_threaded_mainloop_get_api(data
->loop
), data
->context_name
);
403 AL_PRINT("pa_context_new() failed: %s\n",
404 ppa_strerror(ppa_context_errno(data
->context
)));
406 ppa_threaded_mainloop_unlock(data
->loop
);
410 ppa_context_set_state_callback(data
->context
, context_state_callback
, device
);
412 if(ppa_context_connect(data
->context
, NULL
, PA_CONTEXT_NOAUTOSPAWN
, NULL
) < 0)
414 AL_PRINT("Context did not connect: %s\n",
415 ppa_strerror(ppa_context_errno(data
->context
)));
417 ppa_context_unref(data
->context
);
418 data
->context
= NULL
;
420 ppa_threaded_mainloop_unlock(data
->loop
);
424 while((state
=ppa_context_get_state(data
->context
)) != PA_CONTEXT_READY
)
426 if(!PA_CONTEXT_IS_GOOD(state
))
428 AL_PRINT("Context did not get ready: %s\n",
429 ppa_strerror(ppa_context_errno(data
->context
)));
431 ppa_context_unref(data
->context
);
432 data
->context
= NULL
;
434 ppa_threaded_mainloop_unlock(data
->loop
);
438 ppa_threaded_mainloop_wait(data
->loop
);
440 ppa_context_set_state_callback(data
->context
, context_state_callback2
, device
);
442 device
->szDeviceName
= strdup(device_name
);
444 ppa_threaded_mainloop_unlock(data
->loop
);
450 ppa_threaded_mainloop_stop(data
->loop
);
451 ppa_threaded_mainloop_free(data
->loop
);
454 device
->ExtraData
= NULL
;
459 static void pulse_close(ALCdevice
*device
) //{{{
461 pulse_data
*data
= device
->ExtraData
;
463 ppa_threaded_mainloop_lock(data
->loop
);
467 ppa_stream_disconnect(data
->stream
);
468 ppa_stream_unref(data
->stream
);
471 ppa_context_disconnect(data
->context
);
472 ppa_context_unref(data
->context
);
474 ppa_threaded_mainloop_unlock(data
->loop
);
476 ppa_threaded_mainloop_stop(data
->loop
);
477 ppa_threaded_mainloop_free(data
->loop
);
479 DestroyRingBuffer(data
->ring
);
481 device
->ExtraData
= NULL
;
487 static ALCboolean
pulse_open_playback(ALCdevice
*device
, const ALCchar
*device_name
) //{{{
490 device_name
= pulse_device
;
491 else if(strcmp(device_name
, pulse_device
) != 0)
497 if(pulse_open(device
, device_name
) != ALC_FALSE
)
504 static void pulse_close_playback(ALCdevice
*device
) //{{{
510 static ALCboolean
pulse_reset_playback(ALCdevice
*device
) //{{{
512 pulse_data
*data
= device
->ExtraData
;
513 pa_stream_flags_t flags
= 0;
514 pa_stream_state_t state
;
515 pa_channel_map chanmap
;
517 ppa_threaded_mainloop_lock(data
->loop
);
519 data
->frame_size
= aluBytesFromFormat(device
->Format
) *
520 aluChannelsFromFormat(device
->Format
);
521 data
->attr
.minreq
= data
->frame_size
* device
->UpdateSize
;
522 data
->attr
.prebuf
= -1;
523 data
->attr
.maxlength
= -1;
524 data
->attr
.fragsize
= -1;
525 data
->attr
.tlength
= data
->attr
.minreq
* device
->NumUpdates
;
526 data
->stream_name
= "Playback Stream";
528 switch(aluBytesFromFormat(device
->Format
))
531 data
->spec
.format
= PA_SAMPLE_U8
;
534 data
->spec
.format
= PA_SAMPLE_S16NE
;
537 data
->spec
.format
= PA_SAMPLE_FLOAT32NE
;
540 AL_PRINT("Unknown format: 0x%x\n", device
->Format
);
541 ppa_threaded_mainloop_unlock(data
->loop
);
544 data
->spec
.rate
= device
->Frequency
;
545 data
->spec
.channels
= aluChannelsFromFormat(device
->Format
);
547 if(ppa_sample_spec_valid(&data
->spec
) == 0)
549 AL_PRINT("Invalid sample format\n");
550 ppa_threaded_mainloop_unlock(data
->loop
);
554 if(!ppa_channel_map_init_auto(&chanmap
, data
->spec
.channels
, PA_CHANNEL_MAP_WAVEEX
))
556 AL_PRINT("Couldn't build map for channel count (%d)!\n", data
->spec
.channels
);
557 ppa_threaded_mainloop_unlock(data
->loop
);
560 SetDefaultWFXChannelOrder(device
);
562 if(*(GetConfigValue(NULL
, "frequency", "")) == '\0')
563 flags
|= PA_STREAM_FIX_RATE
;
565 data
->stream
= ppa_stream_new(data
->context
, data
->stream_name
, &data
->spec
, &chanmap
);
568 AL_PRINT("pa_stream_new() failed: %s\n",
569 ppa_strerror(ppa_context_errno(data
->context
)));
571 ppa_threaded_mainloop_unlock(data
->loop
);
575 ppa_stream_set_state_callback(data
->stream
, stream_state_callback
, device
);
577 if(ppa_stream_connect_playback(data
->stream
, NULL
, &data
->attr
, flags
, NULL
, NULL
) < 0)
579 AL_PRINT("Stream did not connect: %s\n",
580 ppa_strerror(ppa_context_errno(data
->context
)));
582 ppa_stream_unref(data
->stream
);
585 ppa_threaded_mainloop_unlock(data
->loop
);
589 while((state
=ppa_stream_get_state(data
->stream
)) != PA_STREAM_READY
)
591 if(!PA_STREAM_IS_GOOD(state
))
593 AL_PRINT("Stream did not get ready: %s\n",
594 ppa_strerror(ppa_context_errno(data
->context
)));
596 ppa_stream_unref(data
->stream
);
599 ppa_threaded_mainloop_unlock(data
->loop
);
603 ppa_threaded_mainloop_wait(data
->loop
);
605 ppa_stream_set_state_callback(data
->stream
, stream_state_callback2
, device
);
607 data
->spec
= *(ppa_stream_get_sample_spec(data
->stream
));
608 if(device
->Frequency
!= data
->spec
.rate
)
612 /* Server updated our playback rate, so modify the buffer attribs
614 data
->attr
.minreq
= (ALuint64
)(data
->attr
.minreq
/data
->frame_size
) *
615 data
->spec
.rate
/ device
->Frequency
* data
->frame_size
;
616 data
->attr
.tlength
= data
->attr
.minreq
* device
->NumUpdates
;
618 o
= ppa_stream_set_buffer_attr(data
->stream
, &data
->attr
, NULL
, NULL
);
619 ppa_operation_unref(o
);
621 device
->Frequency
= data
->spec
.rate
;
624 stream_buffer_attr_callback(data
->stream
, device
);
625 #if PA_CHECK_VERSION(0,9,15)
626 if(ppa_stream_set_buffer_attr_callback
)
627 ppa_stream_set_buffer_attr_callback(data
->stream
, stream_buffer_attr_callback
, device
);
630 stream_write_callback(data
->stream
, data
->attr
.tlength
, device
);
631 ppa_stream_set_write_callback(data
->stream
, stream_write_callback
, device
);
633 ppa_threaded_mainloop_unlock(data
->loop
);
637 static void pulse_stop_playback(ALCdevice
*device
) //{{{
639 pulse_data
*data
= device
->ExtraData
;
644 ppa_threaded_mainloop_lock(data
->loop
);
646 ppa_stream_disconnect(data
->stream
);
647 ppa_stream_unref(data
->stream
);
650 ppa_threaded_mainloop_unlock(data
->loop
);
654 static ALCboolean
pulse_open_capture(ALCdevice
*device
, const ALCchar
*device_name
) //{{{
657 pa_stream_state_t state
;
660 device_name
= pulse_capture_device
;
661 else if(strcmp(device_name
, pulse_capture_device
) != 0)
667 if(pulse_open(device
, device_name
) == ALC_FALSE
)
673 data
= device
->ExtraData
;
674 ppa_threaded_mainloop_lock(data
->loop
);
676 data
->samples
= device
->UpdateSize
* device
->NumUpdates
;
677 data
->frame_size
= aluBytesFromFormat(device
->Format
) *
678 aluChannelsFromFormat(device
->Format
);
680 if(!(data
->ring
= CreateRingBuffer(data
->frame_size
, data
->samples
)))
682 ppa_threaded_mainloop_unlock(data
->loop
);
688 data
->attr
.minreq
= -1;
689 data
->attr
.prebuf
= -1;
690 data
->attr
.maxlength
= data
->frame_size
* data
->samples
;
691 data
->attr
.tlength
= -1;
692 data
->attr
.fragsize
= min(data
->frame_size
* data
->samples
,
693 10 * device
->Frequency
/ 1000);
694 data
->stream_name
= "Capture Stream";
696 data
->spec
.rate
= device
->Frequency
;
697 data
->spec
.channels
= aluChannelsFromFormat(device
->Format
);
699 switch(aluBytesFromFormat(device
->Format
))
702 data
->spec
.format
= PA_SAMPLE_U8
;
705 data
->spec
.format
= PA_SAMPLE_S16NE
;
708 data
->spec
.format
= PA_SAMPLE_FLOAT32NE
;
711 AL_PRINT("Unknown format: 0x%x\n", device
->Format
);
712 ppa_threaded_mainloop_unlock(data
->loop
);
718 if(ppa_sample_spec_valid(&data
->spec
) == 0)
720 AL_PRINT("Invalid sample format\n");
721 ppa_threaded_mainloop_unlock(data
->loop
);
727 data
->stream
= ppa_stream_new(data
->context
, data
->stream_name
, &data
->spec
, NULL
);
730 AL_PRINT("pa_stream_new() failed: %s\n",
731 ppa_strerror(ppa_context_errno(data
->context
)));
733 ppa_threaded_mainloop_unlock(data
->loop
);
739 ppa_stream_set_state_callback(data
->stream
, stream_state_callback
, device
);
741 if(ppa_stream_connect_record(data
->stream
, NULL
, &data
->attr
, PA_STREAM_START_CORKED
) < 0)
743 AL_PRINT("Stream did not connect: %s\n",
744 ppa_strerror(ppa_context_errno(data
->context
)));
746 ppa_stream_unref(data
->stream
);
747 ppa_threaded_mainloop_unlock(data
->loop
);
755 while((state
=ppa_stream_get_state(data
->stream
)) != PA_STREAM_READY
)
757 if(!PA_STREAM_IS_GOOD(state
))
759 AL_PRINT("Stream did not get ready: %s\n",
760 ppa_strerror(ppa_context_errno(data
->context
)));
762 ppa_stream_unref(data
->stream
);
765 ppa_threaded_mainloop_unlock(data
->loop
);
772 ppa_threaded_mainloop_wait(data
->loop
);
774 ppa_stream_set_state_callback(data
->stream
, stream_state_callback2
, device
);
776 ppa_threaded_mainloop_unlock(data
->loop
);
780 static void pulse_close_capture(ALCdevice
*device
) //{{{
786 static void pulse_start_capture(ALCdevice
*device
) //{{{
788 pulse_data
*data
= device
->ExtraData
;
791 ppa_threaded_mainloop_lock(data
->loop
);
792 o
= ppa_stream_cork(data
->stream
, 0, stream_success_callback
, device
);
793 while(ppa_operation_get_state(o
) == PA_OPERATION_RUNNING
)
794 ppa_threaded_mainloop_wait(data
->loop
);
795 ppa_operation_unref(o
);
796 ppa_threaded_mainloop_unlock(data
->loop
);
799 static void pulse_stop_capture(ALCdevice
*device
) //{{{
801 pulse_data
*data
= device
->ExtraData
;
804 ppa_threaded_mainloop_lock(data
->loop
);
805 o
= ppa_stream_cork(data
->stream
, 1, stream_success_callback
, device
);
806 while(ppa_operation_get_state(o
) == PA_OPERATION_RUNNING
)
807 ppa_threaded_mainloop_wait(data
->loop
);
808 ppa_operation_unref(o
);
809 ppa_threaded_mainloop_unlock(data
->loop
);
812 static void pulse_capture_samples(ALCdevice
*device
, ALCvoid
*buffer
, ALCuint samples
) //{{{
814 pulse_data
*data
= device
->ExtraData
;
815 ALCuint available
= RingBufferSize(data
->ring
);
819 available
*= data
->frame_size
;
820 samples
*= data
->frame_size
;
822 ppa_threaded_mainloop_lock(data
->loop
);
823 if(available
+ppa_stream_readable_size(data
->stream
) < samples
)
825 ppa_threaded_mainloop_unlock(data
->loop
);
826 alcSetError(ALC_INVALID_VALUE
);
830 available
= min(available
, samples
);
833 ReadRingBuffer(data
->ring
, buffer
, available
/data
->frame_size
);
834 buffer
= (ALubyte
*)buffer
+ available
;
835 samples
-= available
;
838 /* Capture is done in fragment-sized chunks, so we loop until we get all
839 * that's requested */
842 if(ppa_stream_peek(data
->stream
, &buf
, &length
) < 0)
844 AL_PRINT("pa_stream_peek() failed: %s\n",
845 ppa_strerror(ppa_context_errno(data
->context
)));
848 available
= min(length
, samples
);
850 memcpy(buffer
, buf
, available
);
851 buffer
= (ALubyte
*)buffer
+ available
;
852 buf
= (const ALubyte
*)buf
+ available
;
853 samples
-= available
;
856 /* Any unread data in the fragment will be lost, so save it */
857 length
/= data
->frame_size
;
860 if(length
> data
->samples
)
861 length
= data
->samples
;
862 WriteRingBuffer(data
->ring
, buf
, length
);
865 ppa_stream_drop(data
->stream
);
867 ppa_threaded_mainloop_unlock(data
->loop
);
870 static ALCuint
pulse_available_samples(ALCdevice
*device
) //{{{
872 pulse_data
*data
= device
->ExtraData
;
875 ppa_threaded_mainloop_lock(data
->loop
);
876 ret
= RingBufferSize(data
->ring
);
877 ret
+= ppa_stream_readable_size(data
->stream
)/data
->frame_size
;
878 ppa_threaded_mainloop_unlock(data
->loop
);
883 BackendFuncs pulse_funcs
= { //{{{
885 pulse_close_playback
,
886 pulse_reset_playback
,
892 pulse_capture_samples
,
893 pulse_available_samples
896 void alc_pulse_init(BackendFuncs
*func_list
) //{{{
898 *func_list
= pulse_funcs
;
901 void alc_pulse_deinit(void) //{{{
905 void alc_pulse_probe(int type
) //{{{
907 if(!pulse_load()) return;
909 if(type
== DEVICE_PROBE
)
910 AppendDeviceList(pulse_device
);
911 else if(type
== ALL_DEVICE_PROBE
)
912 AppendAllDeviceList(pulse_device
);
913 else if(type
== CAPTURE_DEVICE_PROBE
)
914 AppendCaptureDeviceList(pulse_capture_device
);