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
25 #include <pulse/pulseaudio.h>
27 #if PA_API_VERSION == 11
28 #define PA_STREAM_ADJUST_LATENCY 0x2000U
29 static inline int PA_STREAM_IS_GOOD(pa_stream_state_t x
)
31 return (x
== PA_STREAM_CREATING
|| x
== PA_STREAM_READY
);
33 static inline int PA_CONTEXT_IS_GOOD(pa_context_state_t x
)
35 return (x
== PA_CONTEXT_CONNECTING
|| x
== PA_CONTEXT_AUTHORIZING
||
36 x
== PA_CONTEXT_SETTING_NAME
|| x
== PA_CONTEXT_READY
);
38 #define PA_STREAM_IS_GOOD PA_STREAM_IS_GOOD
39 #define PA_CONTEXT_IS_GOOD PA_CONTEXT_IS_GOOD
40 #elif PA_API_VERSION != 12
41 #error Invalid PulseAudio API version
57 char path_name
[PATH_MAX
];
58 const char *context_name
;
59 const char *stream_name
;
61 pa_threaded_mainloop
*loop
;
67 static char *pulse_device
;
68 static char *pulse_capture_device
;
70 // PulseAudio Event Callbacks {{{
71 static void stream_state_callback(pa_stream
*stream
, void *pdata
) //{{{
73 pulse_data
*data
= pdata
;
75 switch(pa_stream_get_state(stream
))
78 AL_PRINT("%s: %s ready!\n", data
->context_name
, data
->stream_name
);
81 case PA_STREAM_FAILED
:
82 AL_PRINT("%s: %s: Connection failed: %s\n", data
->context_name
,
83 data
->stream_name
, pa_strerror(pa_context_errno(data
->context
)));
86 case PA_STREAM_TERMINATED
:
87 AL_PRINT("%s: %s terminated!\n", data
->context_name
, data
->stream_name
);
94 pa_threaded_mainloop_signal(data
->loop
, 1);
97 static void context_state_callback(pa_context
*context
, void *pdata
) //{{{
99 pulse_data
*data
= pdata
;
101 switch(pa_context_get_state(context
))
103 case PA_CONTEXT_READY
:
104 AL_PRINT("%s ready!\n", data
->context_name
);
107 case PA_CONTEXT_FAILED
:
108 AL_PRINT("%s: Connection failed: %s\n", data
->context_name
,
109 pa_strerror(pa_context_errno(context
)));
112 case PA_CONTEXT_TERMINATED
:
113 AL_PRINT("%s terminated!\n", data
->context_name
);
120 pa_threaded_mainloop_signal(data
->loop
, 1);
124 // PulseAudio I/O Callbacks //{{{
125 static void stream_write_callback(pa_stream
*stream
, size_t len
, void *pdata
) //{{{
127 ALCdevice
*Device
= pdata
;
128 void *buf
= pa_xmalloc0(len
);
130 SuspendContext(NULL
);
131 aluMixData(Device
->Context
, buf
, len
, Device
->Format
);
132 ProcessContext(NULL
);
134 pa_stream_write(stream
, buf
, len
, pa_xfree
, 0, PA_SEEK_RELATIVE
);
137 static void stream_read_callback(pa_stream
*stream
, size_t length
, void *pdata
) //{{{
139 ALCdevice
*Device
= pdata
;
140 pulse_data
*data
= Device
->ExtraData
;
143 if(pa_stream_peek(stream
, &buf
, &length
) < 0)
145 AL_PRINT("pa_stream_peek() failed: %s\n",
146 pa_strerror(pa_context_errno(data
->context
)));
153 length
/= data
->frame_size
;
155 if(data
->samples
< length
)
156 AL_PRINT("stream_read_callback: buffer overflow!\n");
158 WriteRingBuffer(data
->ring
, buf
, (length
<data
->samples
) ? length
: data
->samples
);
160 pa_stream_drop(stream
);
164 static ALCboolean
pulse_open(ALCdevice
*device
, ALCchar
*device_name
, ALCenum format
, ALCuint samples
, ALCuint frequency
) //{{{
166 pulse_data
*data
= pa_xmalloc0(sizeof(pulse_data
));
168 data
->device
= device
;
169 data
->format
= format
;
170 data
->samples
= samples
;
171 data
->frequency
= frequency
;
172 data
->frame_size
= aluBytesFromFormat(format
) * aluChannelsFromFormat(format
);
174 if(pa_get_binary_name(data
->path_name
, sizeof(data
->path_name
)))
175 data
->context_name
= pa_path_get_filename(data
->path_name
);
177 data
->context_name
= "OpenAL Soft";
179 if(!(data
->ring
= CreateRingBuffer(data
->frame_size
, data
->samples
)))
185 device
->ExtraData
= data
;
186 device
->szDeviceName
= device_name
;
188 data
->attr
.minreq
= -1;
189 data
->attr
.prebuf
= -1;
190 data
->attr
.maxlength
= -1;
192 if(device
->IsCaptureDevice
)
194 data
->attr
.tlength
= -1;
195 data
->attr
.fragsize
= data
->frame_size
* data
->samples
/ 2;
196 data
->stream_name
= "Capture Stream";
200 data
->attr
.tlength
= data
->frame_size
* (device
->UpdateSize
&~3);
201 data
->attr
.fragsize
= -1;
202 data
->stream_name
= "Playback Stream";
205 data
->spec
.rate
= data
->frequency
;
206 data
->spec
.channels
= aluChannelsFromFormat(data
->format
);
208 switch(aluBytesFromFormat(data
->format
))
211 data
->spec
.format
= PA_SAMPLE_U8
;
214 data
->spec
.format
= PA_SAMPLE_S16NE
;
217 AL_PRINT("Unknown format: %x\n", data
->format
);
221 if(pa_sample_spec_valid(&data
->spec
) == 0)
223 AL_PRINT("Invalid sample format\n");
227 if(!(data
->loop
= pa_threaded_mainloop_new()))
229 AL_PRINT("pa_threaded_mainloop_new() failed!\n");
233 if(pa_threaded_mainloop_start(data
->loop
) < 0)
235 AL_PRINT("pa_threaded_mainloop_start() failed\n");
239 pa_threaded_mainloop_lock(data
->loop
);
241 data
->context
= pa_context_new(pa_threaded_mainloop_get_api(data
->loop
), data
->context_name
);
244 AL_PRINT("pa_context_new() failed: %s\n",
245 pa_strerror(pa_context_errno(data
->context
)));
247 pa_threaded_mainloop_unlock(data
->loop
);
251 pa_context_set_state_callback(data
->context
, context_state_callback
, data
);
253 if(pa_context_connect(data
->context
, NULL
, 0, NULL
) < 0)
255 AL_PRINT("Context did not connect: %s\n",
256 pa_strerror(pa_context_errno(data
->context
)));
258 pa_context_unref(data
->context
);
259 pa_threaded_mainloop_unlock(data
->loop
);
261 data
->context
= NULL
;
265 while(pa_context_get_state(data
->context
) != PA_CONTEXT_READY
)
267 if(!PA_CONTEXT_IS_GOOD(pa_context_get_state(data
->context
)))
269 pa_context_unref(data
->context
);
270 pa_threaded_mainloop_unlock(data
->loop
);
272 data
->context
= NULL
;
276 pa_threaded_mainloop_wait(data
->loop
);
277 pa_threaded_mainloop_accept(data
->loop
);
280 data
->stream
= pa_stream_new(data
->context
, data
->stream_name
, &data
->spec
, NULL
);
283 AL_PRINT("pa_stream_new() failed: %s\n",
284 pa_strerror(pa_context_errno(data
->context
)));
286 pa_threaded_mainloop_unlock(data
->loop
);
290 pa_stream_set_state_callback(data
->stream
, stream_state_callback
, data
);
292 if(device
->IsCaptureDevice
)
294 if(pa_stream_connect_record(data
->stream
, NULL
, &data
->attr
, PA_STREAM_ADJUST_LATENCY
) < 0)
296 AL_PRINT("Stream did not connect: %s\n",
297 pa_strerror(pa_context_errno(data
->context
)));
299 pa_stream_unref(data
->stream
);
300 pa_threaded_mainloop_unlock(data
->loop
);
308 pa_stream_set_write_callback(data
->stream
, stream_write_callback
, device
);
310 if(pa_stream_connect_playback(data
->stream
, NULL
, &data
->attr
, PA_STREAM_ADJUST_LATENCY
, NULL
, NULL
) < 0)
312 AL_PRINT("Stream did not connect: %s\n",
313 pa_strerror(pa_context_errno(data
->context
)));
315 pa_stream_unref(data
->stream
);
316 pa_threaded_mainloop_unlock(data
->loop
);
323 while(pa_stream_get_state(data
->stream
) != PA_STREAM_READY
)
325 if(!PA_STREAM_IS_GOOD(pa_stream_get_state(data
->stream
)))
327 pa_stream_unref(data
->stream
);
328 pa_threaded_mainloop_unlock(data
->loop
);
334 pa_threaded_mainloop_wait(data
->loop
);
335 pa_threaded_mainloop_accept(data
->loop
);
338 device
->UpdateSize
/= 4;
339 pa_threaded_mainloop_unlock(data
->loop
);
344 pa_threaded_mainloop_lock(data
->loop
);
346 pa_context_disconnect(data
->context
);
347 pa_context_unref(data
->context
);
349 pa_threaded_mainloop_unlock(data
->loop
);
351 pa_threaded_mainloop_stop(data
->loop
);
352 pa_threaded_mainloop_free(data
->loop
);
354 device
->ExtraData
= NULL
;
355 device
->szDeviceName
= NULL
;
356 DestroyRingBuffer(data
->ring
);
362 static void pulse_close(ALCdevice
*device
) //{{{
364 pulse_data
*data
= device
->ExtraData
;
366 pa_threaded_mainloop_lock(data
->loop
);
368 pa_stream_disconnect(data
->stream
);
369 pa_stream_unref(data
->stream
);
371 pa_context_disconnect(data
->context
);
372 pa_context_unref(data
->context
);
374 pa_threaded_mainloop_unlock(data
->loop
);
376 pa_threaded_mainloop_stop(data
->loop
);
377 pa_threaded_mainloop_free(data
->loop
);
379 device
->ExtraData
= NULL
;
380 device
->szDeviceName
= NULL
;
381 DestroyRingBuffer(data
->ring
);
388 static ALCboolean
pulse_open_playback(ALCdevice
*device
, const ALCchar
*device_name
) //{{{
392 if(strcmp(device_name
, pulse_device
) != 0)
396 return pulse_open(device
, pulse_device
, device
->Format
, 0, device
->Frequency
);
399 static void pulse_close_playback(ALCdevice
*device
) //{{{
404 static ALCboolean
pulse_open_capture(ALCdevice
*device
, const ALCchar
*device_name
, ALCuint frequency
, ALCenum format
, ALCsizei samples
) //{{{
408 if(strcmp(device_name
, pulse_capture_device
) != 0)
412 return pulse_open(device
, pulse_capture_device
, format
, samples
, frequency
);
415 static void pulse_close_capture(ALCdevice
*device
) //{{{
420 static void pulse_start_capture(ALCdevice
*device
) //{{{
422 pulse_data
*data
= device
->ExtraData
;
424 pa_threaded_mainloop_lock(data
->loop
);
425 pa_stream_set_read_callback(data
->stream
, stream_read_callback
, device
);
426 pa_threaded_mainloop_unlock(data
->loop
);
429 static void pulse_stop_capture(ALCdevice
*device
) //{{{
431 pulse_data
*data
= device
->ExtraData
;
433 pa_threaded_mainloop_lock(data
->loop
);
434 pa_stream_set_read_callback(data
->stream
, NULL
, NULL
);
435 pa_threaded_mainloop_unlock(data
->loop
);
438 static void pulse_capture_samples(ALCdevice
*device
, ALCvoid
*buffer
, ALCuint samples
) //{{{
440 pulse_data
*data
= device
->ExtraData
;
441 ALCuint available
= RingBufferSize(data
->ring
);
443 if(available
< samples
)
444 SetALCError(ALC_INVALID_VALUE
);
446 ReadRingBuffer(data
->ring
, buffer
, samples
);
449 static ALCuint
pulse_available_samples(ALCdevice
*device
) //{{{
451 pulse_data
*data
= device
->ExtraData
;
452 return RingBufferSize(data
->ring
);
455 BackendFuncs pulse_funcs
= { //{{{
457 pulse_close_playback
,
462 pulse_capture_samples
,
463 pulse_available_samples
466 void alc_pulse_init(BackendFuncs
*func_list
) //{{{
468 *func_list
= pulse_funcs
;
470 pulse_device
= AppendDeviceList("PulseAudio Software");
471 AppendAllDeviceList(pulse_device
);
473 pulse_capture_device
= AppendCaptureDeviceList("PulseAudio Capture");