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 static void *pa_handle
;
48 #define MAKE_FUNC(x) static typeof(x) * p##x
49 MAKE_FUNC(pa_context_unref
);
50 MAKE_FUNC(pa_sample_spec_valid
);
51 MAKE_FUNC(pa_stream_drop
);
52 MAKE_FUNC(pa_strerror
);
53 MAKE_FUNC(pa_context_get_state
);
54 MAKE_FUNC(pa_stream_get_state
);
55 MAKE_FUNC(pa_threaded_mainloop_signal
);
56 MAKE_FUNC(pa_stream_peek
);
57 MAKE_FUNC(pa_threaded_mainloop_wait
);
58 MAKE_FUNC(pa_threaded_mainloop_unlock
);
59 MAKE_FUNC(pa_context_new
);
60 MAKE_FUNC(pa_threaded_mainloop_stop
);
61 MAKE_FUNC(pa_context_disconnect
);
62 MAKE_FUNC(pa_threaded_mainloop_start
);
63 MAKE_FUNC(pa_threaded_mainloop_get_api
);
64 MAKE_FUNC(pa_context_set_state_callback
);
65 MAKE_FUNC(pa_stream_write
);
67 MAKE_FUNC(pa_stream_connect_record
);
68 MAKE_FUNC(pa_stream_connect_playback
);
69 MAKE_FUNC(pa_path_get_filename
);
70 MAKE_FUNC(pa_get_binary_name
);
71 MAKE_FUNC(pa_threaded_mainloop_free
);
72 MAKE_FUNC(pa_context_errno
);
73 MAKE_FUNC(pa_xmalloc0
);
74 MAKE_FUNC(pa_stream_unref
);
75 MAKE_FUNC(pa_threaded_mainloop_accept
);
76 MAKE_FUNC(pa_stream_set_write_callback
);
77 MAKE_FUNC(pa_threaded_mainloop_new
);
78 MAKE_FUNC(pa_context_connect
);
79 MAKE_FUNC(pa_stream_set_read_callback
);
80 MAKE_FUNC(pa_stream_set_state_callback
);
81 MAKE_FUNC(pa_stream_new
);
82 MAKE_FUNC(pa_stream_disconnect
);
83 MAKE_FUNC(pa_threaded_mainloop_lock
);
101 char path_name
[PATH_MAX
];
102 const char *context_name
;
103 const char *stream_name
;
105 pa_threaded_mainloop
*loop
;
111 static char *pulse_device
;
112 static char *pulse_capture_device
;
114 // PulseAudio Event Callbacks {{{
115 static void stream_state_callback(pa_stream
*stream
, void *pdata
) //{{{
117 pulse_data
*data
= pdata
;
119 switch(ppa_stream_get_state(stream
))
121 case PA_STREAM_READY
:
122 AL_PRINT("%s: %s ready!\n", data
->context_name
, data
->stream_name
);
125 case PA_STREAM_FAILED
:
126 AL_PRINT("%s: %s: Connection failed: %s\n", data
->context_name
,
127 data
->stream_name
, ppa_strerror(ppa_context_errno(data
->context
)));
130 case PA_STREAM_TERMINATED
:
131 AL_PRINT("%s: %s terminated!\n", data
->context_name
, data
->stream_name
);
138 ppa_threaded_mainloop_signal(data
->loop
, 1);
141 static void context_state_callback(pa_context
*context
, void *pdata
) //{{{
143 pulse_data
*data
= pdata
;
145 switch(ppa_context_get_state(context
))
147 case PA_CONTEXT_READY
:
148 AL_PRINT("%s ready!\n", data
->context_name
);
151 case PA_CONTEXT_FAILED
:
152 AL_PRINT("%s: Connection failed: %s\n", data
->context_name
,
153 ppa_strerror(ppa_context_errno(context
)));
156 case PA_CONTEXT_TERMINATED
:
157 AL_PRINT("%s terminated!\n", data
->context_name
);
164 ppa_threaded_mainloop_signal(data
->loop
, 1);
168 // PulseAudio I/O Callbacks //{{{
169 static void stream_write_callback(pa_stream
*stream
, size_t len
, void *pdata
) //{{{
171 ALCdevice
*Device
= pdata
;
172 void *buf
= ppa_xmalloc0(len
);
174 SuspendContext(NULL
);
175 aluMixData(Device
->Context
, buf
, len
, Device
->Format
);
176 ProcessContext(NULL
);
178 ppa_stream_write(stream
, buf
, len
, ppa_xfree
, 0, PA_SEEK_RELATIVE
);
181 static void stream_read_callback(pa_stream
*stream
, size_t length
, void *pdata
) //{{{
183 ALCdevice
*Device
= pdata
;
184 pulse_data
*data
= Device
->ExtraData
;
187 if(ppa_stream_peek(stream
, &buf
, &length
) < 0)
189 AL_PRINT("pa_stream_peek() failed: %s\n",
190 ppa_strerror(ppa_context_errno(data
->context
)));
197 length
/= data
->frame_size
;
199 if(data
->samples
< length
)
200 AL_PRINT("stream_read_callback: buffer overflow!\n");
202 WriteRingBuffer(data
->ring
, buf
, (length
<data
->samples
) ? length
: data
->samples
);
204 ppa_stream_drop(stream
);
208 static ALCboolean
pulse_open(ALCdevice
*device
, ALCchar
*device_name
) //{{{
210 pulse_data
*data
= ppa_xmalloc0(sizeof(pulse_data
));
212 data
->device
= device
;
214 if(ppa_get_binary_name(data
->path_name
, sizeof(data
->path_name
)))
215 data
->context_name
= ppa_path_get_filename(data
->path_name
);
217 data
->context_name
= "OpenAL Soft";
219 device
->ExtraData
= data
;
220 device
->szDeviceName
= device_name
;
222 if(!(data
->loop
= ppa_threaded_mainloop_new()))
224 AL_PRINT("pa_threaded_mainloop_new() failed!\n");
228 if(ppa_threaded_mainloop_start(data
->loop
) < 0)
230 AL_PRINT("pa_threaded_mainloop_start() failed\n");
234 ppa_threaded_mainloop_lock(data
->loop
);
236 data
->context
= ppa_context_new(ppa_threaded_mainloop_get_api(data
->loop
), data
->context_name
);
239 AL_PRINT("pa_context_new() failed: %s\n",
240 ppa_strerror(ppa_context_errno(data
->context
)));
242 ppa_threaded_mainloop_unlock(data
->loop
);
246 ppa_context_set_state_callback(data
->context
, context_state_callback
, data
);
248 if(ppa_context_connect(data
->context
, NULL
, PA_CONTEXT_NOAUTOSPAWN
, NULL
) < 0)
250 AL_PRINT("Context did not connect: %s\n",
251 ppa_strerror(ppa_context_errno(data
->context
)));
253 ppa_context_unref(data
->context
);
254 data
->context
= NULL
;
256 ppa_threaded_mainloop_unlock(data
->loop
);
260 while(ppa_context_get_state(data
->context
) != PA_CONTEXT_READY
)
262 if(!PA_CONTEXT_IS_GOOD(ppa_context_get_state(data
->context
)))
264 AL_PRINT("Context did not get ready: %s\n",
265 ppa_strerror(ppa_context_errno(data
->context
)));
267 ppa_context_unref(data
->context
);
268 data
->context
= NULL
;
270 ppa_threaded_mainloop_unlock(data
->loop
);
274 ppa_threaded_mainloop_wait(data
->loop
);
275 ppa_threaded_mainloop_accept(data
->loop
);
278 ppa_threaded_mainloop_unlock(data
->loop
);
284 ppa_threaded_mainloop_stop(data
->loop
);
285 ppa_threaded_mainloop_free(data
->loop
);
287 device
->ExtraData
= NULL
;
288 device
->szDeviceName
= NULL
;
294 static void pulse_close(ALCdevice
*device
) //{{{
296 pulse_data
*data
= device
->ExtraData
;
298 ppa_threaded_mainloop_lock(data
->loop
);
302 ppa_stream_disconnect(data
->stream
);
303 ppa_stream_unref(data
->stream
);
306 ppa_context_disconnect(data
->context
);
307 ppa_context_unref(data
->context
);
309 ppa_threaded_mainloop_unlock(data
->loop
);
311 ppa_threaded_mainloop_stop(data
->loop
);
312 ppa_threaded_mainloop_free(data
->loop
);
314 device
->ExtraData
= NULL
;
315 device
->szDeviceName
= NULL
;
316 DestroyRingBuffer(data
->ring
);
323 static ALCboolean
pulse_open_playback(ALCdevice
*device
, const ALCchar
*device_name
) //{{{
330 if(strcmp(device_name
, pulse_device
) != 0)
334 return pulse_open(device
, pulse_device
);
337 static void pulse_close_playback(ALCdevice
*device
) //{{{
342 static ALCboolean
pulse_start_context(ALCdevice
*device
, ALCcontext
*context
) //{{{
344 pulse_data
*data
= device
->ExtraData
;
347 ppa_threaded_mainloop_lock(data
->loop
);
349 data
->samples
= device
->BufferSize
;
350 data
->frame_size
= aluBytesFromFormat(device
->Format
) *
351 aluChannelsFromFormat(device
->Format
);
353 device
->UpdateSize
= device
->BufferSize
/ 4;
355 data
->attr
.minreq
= -1;
356 data
->attr
.prebuf
= -1;
357 data
->attr
.maxlength
= -1;
358 data
->attr
.fragsize
= data
->frame_size
* device
->UpdateSize
;
359 data
->attr
.tlength
= data
->attr
.fragsize
* 4;
360 data
->stream_name
= "Playback Stream";
362 data
->spec
.rate
= device
->Frequency
;
363 data
->spec
.channels
= aluChannelsFromFormat(device
->Format
);
365 switch(aluBytesFromFormat(device
->Format
))
368 data
->spec
.format
= PA_SAMPLE_U8
;
371 data
->spec
.format
= PA_SAMPLE_S16NE
;
374 data
->spec
.format
= PA_SAMPLE_FLOAT32NE
;
377 AL_PRINT("Unknown format: %x\n", device
->Format
);
378 ppa_threaded_mainloop_unlock(data
->loop
);
382 if(ppa_sample_spec_valid(&data
->spec
) == 0)
384 AL_PRINT("Invalid sample format\n");
385 ppa_threaded_mainloop_unlock(data
->loop
);
389 data
->stream
= ppa_stream_new(data
->context
, data
->stream_name
, &data
->spec
, NULL
);
392 AL_PRINT("pa_stream_new() failed: %s\n",
393 ppa_strerror(ppa_context_errno(data
->context
)));
395 ppa_threaded_mainloop_unlock(data
->loop
);
399 ppa_stream_set_state_callback(data
->stream
, stream_state_callback
, data
);
400 ppa_stream_set_write_callback(data
->stream
, stream_write_callback
, device
);
402 if(ppa_stream_connect_playback(data
->stream
, NULL
, &data
->attr
, PA_STREAM_ADJUST_LATENCY
, NULL
, NULL
) < 0)
404 AL_PRINT("Stream did not connect: %s\n",
405 ppa_strerror(ppa_context_errno(data
->context
)));
407 ppa_stream_unref(data
->stream
);
410 ppa_threaded_mainloop_unlock(data
->loop
);
414 while(ppa_stream_get_state(data
->stream
) != PA_STREAM_READY
)
416 if(!PA_STREAM_IS_GOOD(ppa_stream_get_state(data
->stream
)))
418 AL_PRINT("Stream did not get ready: %s\n",
419 ppa_strerror(ppa_context_errno(data
->context
)));
421 ppa_stream_unref(data
->stream
);
424 ppa_threaded_mainloop_unlock(data
->loop
);
428 ppa_threaded_mainloop_wait(data
->loop
);
429 ppa_threaded_mainloop_accept(data
->loop
);
432 ppa_threaded_mainloop_unlock(data
->loop
);
436 static void pulse_stop_context(ALCdevice
*device
, ALCcontext
*context
) //{{{
438 pulse_data
*data
= device
->ExtraData
;
444 ppa_threaded_mainloop_lock(data
->loop
);
446 ppa_stream_disconnect(data
->stream
);
447 ppa_stream_unref(data
->stream
);
450 ppa_threaded_mainloop_unlock(data
->loop
);
454 static ALCboolean
pulse_open_capture(ALCdevice
*device
, const ALCchar
*device_name
) //{{{
463 if(strcmp(device_name
, pulse_capture_device
) != 0)
467 if(pulse_open(device
, pulse_capture_device
) == ALC_FALSE
)
470 data
= device
->ExtraData
;
471 ppa_threaded_mainloop_lock(data
->loop
);
473 data
->samples
= device
->BufferSize
;
474 data
->frame_size
= aluBytesFromFormat(device
->Format
) *
475 aluChannelsFromFormat(device
->Format
);
477 if(!(data
->ring
= CreateRingBuffer(data
->frame_size
, data
->samples
)))
479 ppa_threaded_mainloop_unlock(data
->loop
);
484 data
->attr
.minreq
= -1;
485 data
->attr
.prebuf
= -1;
486 data
->attr
.maxlength
= -1;
487 data
->attr
.tlength
= -1;
488 data
->attr
.fragsize
= data
->frame_size
* data
->samples
/ 2;
489 data
->stream_name
= "Capture Stream";
491 data
->spec
.rate
= device
->Frequency
;
492 data
->spec
.channels
= aluChannelsFromFormat(device
->Format
);
494 switch(aluBytesFromFormat(device
->Format
))
497 data
->spec
.format
= PA_SAMPLE_U8
;
500 data
->spec
.format
= PA_SAMPLE_S16NE
;
503 AL_PRINT("Unknown format: %x\n", device
->Format
);
504 ppa_threaded_mainloop_unlock(data
->loop
);
509 if(ppa_sample_spec_valid(&data
->spec
) == 0)
511 AL_PRINT("Invalid sample format\n");
512 ppa_threaded_mainloop_unlock(data
->loop
);
517 data
->stream
= ppa_stream_new(data
->context
, data
->stream_name
, &data
->spec
, NULL
);
520 AL_PRINT("pa_stream_new() failed: %s\n",
521 ppa_strerror(ppa_context_errno(data
->context
)));
523 ppa_threaded_mainloop_unlock(data
->loop
);
528 ppa_stream_set_state_callback(data
->stream
, stream_state_callback
, data
);
530 if(ppa_stream_connect_record(data
->stream
, NULL
, &data
->attr
, PA_STREAM_ADJUST_LATENCY
) < 0)
532 AL_PRINT("Stream did not connect: %s\n",
533 ppa_strerror(ppa_context_errno(data
->context
)));
535 ppa_stream_unref(data
->stream
);
536 ppa_threaded_mainloop_unlock(data
->loop
);
543 while(ppa_stream_get_state(data
->stream
) != PA_STREAM_READY
)
545 if(!PA_STREAM_IS_GOOD(ppa_stream_get_state(data
->stream
)))
547 AL_PRINT("Stream did not get ready: %s\n",
548 ppa_strerror(ppa_context_errno(data
->context
)));
550 ppa_stream_unref(data
->stream
);
551 ppa_threaded_mainloop_unlock(data
->loop
);
558 ppa_threaded_mainloop_wait(data
->loop
);
559 ppa_threaded_mainloop_accept(data
->loop
);
562 ppa_threaded_mainloop_unlock(data
->loop
);
566 static void pulse_close_capture(ALCdevice
*device
) //{{{
571 static void pulse_start_capture(ALCdevice
*device
) //{{{
573 pulse_data
*data
= device
->ExtraData
;
575 ppa_threaded_mainloop_lock(data
->loop
);
576 ppa_stream_set_read_callback(data
->stream
, stream_read_callback
, device
);
577 ppa_threaded_mainloop_unlock(data
->loop
);
580 static void pulse_stop_capture(ALCdevice
*device
) //{{{
582 pulse_data
*data
= device
->ExtraData
;
584 ppa_threaded_mainloop_lock(data
->loop
);
585 ppa_stream_set_read_callback(data
->stream
, NULL
, NULL
);
586 ppa_threaded_mainloop_unlock(data
->loop
);
589 static void pulse_capture_samples(ALCdevice
*device
, ALCvoid
*buffer
, ALCuint samples
) //{{{
591 pulse_data
*data
= device
->ExtraData
;
592 ALCuint available
= RingBufferSize(data
->ring
);
594 if(available
< samples
)
595 SetALCError(ALC_INVALID_VALUE
);
597 ReadRingBuffer(data
->ring
, buffer
, samples
);
600 static ALCuint
pulse_available_samples(ALCdevice
*device
) //{{{
602 pulse_data
*data
= device
->ExtraData
;
603 return RingBufferSize(data
->ring
);
606 BackendFuncs pulse_funcs
= { //{{{
608 pulse_close_playback
,
615 pulse_capture_samples
,
616 pulse_available_samples
619 void alc_pulse_init(BackendFuncs
*func_list
) //{{{
621 *func_list
= pulse_funcs
;
624 pa_handle
= LoadLibrary("libpulse-0.dll");
625 #define LOAD_FUNC(x) do { \
626 p##x = GetProcAddress(pa_handle, #x); \
628 AL_PRINT("Could not load %s from libpulse-0.dll\n", #x); \
629 FreeLibrary(pa_handle); \
635 #elif defined (HAVE_DLFCN_H)
637 #if defined(__APPLE__) && defined(__MACH__)
638 pa_handle
= dlopen("libpulse.0.dylib", RTLD_NOW
);
640 pa_handle
= dlopen("libpulse.so.0", RTLD_NOW
);
642 #define LOAD_FUNC(x) do { \
643 p##x = dlsym(pa_handle, #x); \
645 AL_PRINT("Could not load %s from libpulse\n", #x); \
646 dlclose(pa_handle); \
654 pa_handle
= (void*)0xDEADBEEF;
655 #define LOAD_FUNC(x) p##x = (x)
661 LOAD_FUNC(pa_context_unref
);
662 LOAD_FUNC(pa_sample_spec_valid
);
663 LOAD_FUNC(pa_stream_drop
);
664 LOAD_FUNC(pa_strerror
);
665 LOAD_FUNC(pa_context_get_state
);
666 LOAD_FUNC(pa_stream_get_state
);
667 LOAD_FUNC(pa_threaded_mainloop_signal
);
668 LOAD_FUNC(pa_stream_peek
);
669 LOAD_FUNC(pa_threaded_mainloop_wait
);
670 LOAD_FUNC(pa_threaded_mainloop_unlock
);
671 LOAD_FUNC(pa_context_new
);
672 LOAD_FUNC(pa_threaded_mainloop_stop
);
673 LOAD_FUNC(pa_context_disconnect
);
674 LOAD_FUNC(pa_threaded_mainloop_start
);
675 LOAD_FUNC(pa_threaded_mainloop_get_api
);
676 LOAD_FUNC(pa_context_set_state_callback
);
677 LOAD_FUNC(pa_stream_write
);
679 LOAD_FUNC(pa_stream_connect_record
);
680 LOAD_FUNC(pa_stream_connect_playback
);
681 LOAD_FUNC(pa_path_get_filename
);
682 LOAD_FUNC(pa_get_binary_name
);
683 LOAD_FUNC(pa_threaded_mainloop_free
);
684 LOAD_FUNC(pa_context_errno
);
685 LOAD_FUNC(pa_xmalloc0
);
686 LOAD_FUNC(pa_stream_unref
);
687 LOAD_FUNC(pa_threaded_mainloop_accept
);
688 LOAD_FUNC(pa_stream_set_write_callback
);
689 LOAD_FUNC(pa_threaded_mainloop_new
);
690 LOAD_FUNC(pa_context_connect
);
691 LOAD_FUNC(pa_stream_set_read_callback
);
692 LOAD_FUNC(pa_stream_set_state_callback
);
693 LOAD_FUNC(pa_stream_new
);
694 LOAD_FUNC(pa_stream_disconnect
);
695 LOAD_FUNC(pa_threaded_mainloop_lock
);
699 pulse_device
= AppendDeviceList("PulseAudio Software");
700 AppendAllDeviceList(pulse_device
);
702 pulse_capture_device
= AppendCaptureDeviceList("PulseAudio Capture");
705 void alc_pulse_deinit(void) //{{{