Add a method to deinitialize backends
[openal-soft.git] / Alc / pulseaudio.c
blobdd3621c1e6e2f1475e22a3af2b1b772edf76132c
1 /**
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
21 #include "config.h"
23 #include "alMain.h"
24 #ifdef HAVE_DLFCN_H
25 #include <dlfcn.h>
26 #endif
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
45 #endif
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);
66 MAKE_FUNC(pa_xfree);
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);
84 #undef MAKE_FUNC
86 #ifndef PATH_MAX
87 #define PATH_MAX 4096
88 #endif
90 typedef struct {
91 ALCdevice *device;
93 ALCuint samples;
94 ALCuint frame_size;
96 RingBuffer *ring;
98 pa_buffer_attr attr;
99 pa_sample_spec spec;
101 char path_name[PATH_MAX];
102 const char *context_name;
103 const char *stream_name;
105 pa_threaded_mainloop *loop;
107 pa_stream *stream;
108 pa_context *context;
109 } pulse_data;
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);
123 break;
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)));
128 break;
130 case PA_STREAM_TERMINATED:
131 AL_PRINT("%s: %s terminated!\n", data->context_name, data->stream_name);
132 break;
134 default:
135 break;
138 ppa_threaded_mainloop_signal(data->loop, 1);
139 } //}}}
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);
149 break;
151 case PA_CONTEXT_FAILED:
152 AL_PRINT("%s: Connection failed: %s\n", data->context_name,
153 ppa_strerror(ppa_context_errno(context)));
154 break;
156 case PA_CONTEXT_TERMINATED:
157 AL_PRINT("%s terminated!\n", data->context_name);
158 break;
160 default:
161 break;
164 ppa_threaded_mainloop_signal(data->loop, 1);
165 } //}}}
166 //}}}
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);
179 } //}}}
181 static void stream_read_callback(pa_stream *stream, size_t length, void *pdata) //{{{
183 ALCdevice *Device = pdata;
184 pulse_data *data = Device->ExtraData;
185 const void *buf;
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)));
191 return;
194 assert(buf);
195 assert(length);
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);
205 } //}}}
206 //}}}
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);
216 else
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");
225 goto out;
228 if(ppa_threaded_mainloop_start(data->loop) < 0)
230 AL_PRINT("pa_threaded_mainloop_start() failed\n");
231 goto out;
234 ppa_threaded_mainloop_lock(data->loop);
236 data->context = ppa_context_new(ppa_threaded_mainloop_get_api(data->loop), data->context_name);
237 if(!data->context)
239 AL_PRINT("pa_context_new() failed: %s\n",
240 ppa_strerror(ppa_context_errno(data->context)));
242 ppa_threaded_mainloop_unlock(data->loop);
243 goto out;
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);
257 goto out;
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);
271 goto out;
274 ppa_threaded_mainloop_wait(data->loop);
275 ppa_threaded_mainloop_accept(data->loop);
278 ppa_threaded_mainloop_unlock(data->loop);
279 return ALC_TRUE;
281 out:
282 if(data->loop)
284 ppa_threaded_mainloop_stop(data->loop);
285 ppa_threaded_mainloop_free(data->loop);
287 device->ExtraData = NULL;
288 device->szDeviceName = NULL;
290 ppa_xfree(data);
291 return ALC_FALSE;
292 } //}}}
294 static void pulse_close(ALCdevice *device) //{{{
296 pulse_data *data = device->ExtraData;
298 ppa_threaded_mainloop_lock(data->loop);
300 if(data->stream)
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);
318 ppa_xfree(data);
319 } //}}}
320 //}}}
322 // OpenAL {{{
323 static ALCboolean pulse_open_playback(ALCdevice *device, const ALCchar *device_name) //{{{
325 if(!pa_handle)
326 return ALC_FALSE;
328 if(device_name)
330 if(strcmp(device_name, pulse_device) != 0)
331 return ALC_FALSE;
334 return pulse_open(device, pulse_device);
335 } //}}}
337 static void pulse_close_playback(ALCdevice *device) //{{{
339 pulse_close(device);
340 } //}}}
342 static ALCboolean pulse_start_context(ALCdevice *device, ALCcontext *context) //{{{
344 pulse_data *data = device->ExtraData;
345 (void)context;
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))
367 case 1:
368 data->spec.format = PA_SAMPLE_U8;
369 break;
370 case 2:
371 data->spec.format = PA_SAMPLE_S16NE;
372 break;
373 case 4:
374 data->spec.format = PA_SAMPLE_FLOAT32NE;
375 break;
376 default:
377 AL_PRINT("Unknown format: %x\n", device->Format);
378 ppa_threaded_mainloop_unlock(data->loop);
379 return ALC_FALSE;
382 if(ppa_sample_spec_valid(&data->spec) == 0)
384 AL_PRINT("Invalid sample format\n");
385 ppa_threaded_mainloop_unlock(data->loop);
386 return ALC_FALSE;
389 data->stream = ppa_stream_new(data->context, data->stream_name, &data->spec, NULL);
390 if(!data->stream)
392 AL_PRINT("pa_stream_new() failed: %s\n",
393 ppa_strerror(ppa_context_errno(data->context)));
395 ppa_threaded_mainloop_unlock(data->loop);
396 return ALC_FALSE;
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);
408 data->stream = NULL;
410 ppa_threaded_mainloop_unlock(data->loop);
411 return ALC_FALSE;
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);
422 data->stream = NULL;
424 ppa_threaded_mainloop_unlock(data->loop);
425 return ALC_FALSE;
428 ppa_threaded_mainloop_wait(data->loop);
429 ppa_threaded_mainloop_accept(data->loop);
432 ppa_threaded_mainloop_unlock(data->loop);
433 return ALC_TRUE;
434 } //}}}
436 static void pulse_stop_context(ALCdevice *device, ALCcontext *context) //{{{
438 pulse_data *data = device->ExtraData;
439 (void)context;
441 if(!data->stream)
442 return;
444 ppa_threaded_mainloop_lock(data->loop);
446 ppa_stream_disconnect(data->stream);
447 ppa_stream_unref(data->stream);
448 data->stream = NULL;
450 ppa_threaded_mainloop_unlock(data->loop);
451 } //}}}
454 static ALCboolean pulse_open_capture(ALCdevice *device, const ALCchar *device_name) //{{{
456 pulse_data *data;
458 if(!pa_handle)
459 return ALC_FALSE;
461 if(device_name)
463 if(strcmp(device_name, pulse_capture_device) != 0)
464 return ALC_FALSE;
467 if(pulse_open(device, pulse_capture_device) == ALC_FALSE)
468 return 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);
480 pulse_close(device);
481 return ALC_FALSE;
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))
496 case 1:
497 data->spec.format = PA_SAMPLE_U8;
498 break;
499 case 2:
500 data->spec.format = PA_SAMPLE_S16NE;
501 break;
502 default:
503 AL_PRINT("Unknown format: %x\n", device->Format);
504 ppa_threaded_mainloop_unlock(data->loop);
505 pulse_close(device);
506 return ALC_FALSE;
509 if(ppa_sample_spec_valid(&data->spec) == 0)
511 AL_PRINT("Invalid sample format\n");
512 ppa_threaded_mainloop_unlock(data->loop);
513 pulse_close(device);
514 return ALC_FALSE;
517 data->stream = ppa_stream_new(data->context, data->stream_name, &data->spec, NULL);
518 if(!data->stream)
520 AL_PRINT("pa_stream_new() failed: %s\n",
521 ppa_strerror(ppa_context_errno(data->context)));
523 ppa_threaded_mainloop_unlock(data->loop);
524 pulse_close(device);
525 return ALC_FALSE;
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);
538 data->stream = NULL;
539 pulse_close(device);
540 return ALC_FALSE;
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);
553 data->stream = NULL;
554 pulse_close(device);
555 return ALC_FALSE;
558 ppa_threaded_mainloop_wait(data->loop);
559 ppa_threaded_mainloop_accept(data->loop);
562 ppa_threaded_mainloop_unlock(data->loop);
563 return ALC_TRUE;
564 } //}}}
566 static void pulse_close_capture(ALCdevice *device) //{{{
568 pulse_close(device);
569 } //}}}
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);
578 } //}}}
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);
587 } //}}}
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);
596 else
597 ReadRingBuffer(data->ring, buffer, samples);
598 } //}}}
600 static ALCuint pulse_available_samples(ALCdevice *device) //{{{
602 pulse_data *data = device->ExtraData;
603 return RingBufferSize(data->ring);
604 } //}}}
606 BackendFuncs pulse_funcs = { //{{{
607 pulse_open_playback,
608 pulse_close_playback,
609 pulse_start_context,
610 pulse_stop_context,
611 pulse_open_capture,
612 pulse_close_capture,
613 pulse_start_capture,
614 pulse_stop_capture,
615 pulse_capture_samples,
616 pulse_available_samples
617 }; //}}}
619 void alc_pulse_init(BackendFuncs *func_list) //{{{
621 *func_list = pulse_funcs;
623 #ifdef _WIN32
624 pa_handle = LoadLibrary("libpulse-0.dll");
625 #define LOAD_FUNC(x) do { \
626 p##x = GetProcAddress(pa_handle, #x); \
627 if(!(p##x)) { \
628 AL_PRINT("Could not load %s from libpulse-0.dll\n", #x); \
629 FreeLibrary(pa_handle); \
630 pa_handle = NULL; \
631 return; \
633 } while(0)
635 #elif defined (HAVE_DLFCN_H)
637 #if defined(__APPLE__) && defined(__MACH__)
638 pa_handle = dlopen("libpulse.0.dylib", RTLD_NOW);
639 #else
640 pa_handle = dlopen("libpulse.so.0", RTLD_NOW);
641 #endif
642 #define LOAD_FUNC(x) do { \
643 p##x = dlsym(pa_handle, #x); \
644 if(!(p##x)) { \
645 AL_PRINT("Could not load %s from libpulse\n", #x); \
646 dlclose(pa_handle); \
647 pa_handle = NULL; \
648 return; \
650 } while(0)
652 #else
654 pa_handle = (void*)0xDEADBEEF;
655 #define LOAD_FUNC(x) p##x = (x)
657 #endif
658 if(!pa_handle)
659 return;
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);
678 LOAD_FUNC(pa_xfree);
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);
697 #undef LOAD_FUNC
699 pulse_device = AppendDeviceList("PulseAudio Software");
700 AppendAllDeviceList(pulse_device);
702 pulse_capture_device = AppendCaptureDeviceList("PulseAudio Capture");
703 } //}}}
705 void alc_pulse_deinit(void) //{{{
707 } //}}}
708 //}}}