Avoid calling alDelete* from alGen*
[openal-soft.git] / Alc / pulseaudio.c
blobd91d2e164851801375e83e74e1c29efdccf87a53
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 #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)))
52 #endif
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);
74 MAKE_FUNC(pa_xfree);
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_channel_map_snprint);
100 MAKE_FUNC(pa_channel_map_equal);
101 MAKE_FUNC(pa_context_get_server_info);
102 MAKE_FUNC(pa_context_get_sink_info_by_name);
103 MAKE_FUNC(pa_operation_get_state);
104 MAKE_FUNC(pa_operation_unref);
105 #if PA_CHECK_VERSION(0,9,15)
106 MAKE_FUNC(pa_channel_map_superset);
107 MAKE_FUNC(pa_stream_set_buffer_attr_callback);
108 #endif
109 #if PA_CHECK_VERSION(0,9,16)
110 MAKE_FUNC(pa_stream_begin_write);
111 #endif
112 #undef MAKE_FUNC
114 #ifndef PATH_MAX
115 #define PATH_MAX 4096
116 #endif
118 typedef struct {
119 ALCuint samples;
120 ALCuint frame_size;
122 RingBuffer *ring;
124 pa_buffer_attr attr;
125 pa_sample_spec spec;
127 char path_name[PATH_MAX];
128 const char *context_name;
129 const char *stream_name;
131 pa_threaded_mainloop *loop;
133 pa_stream *stream;
134 pa_context *context;
135 } pulse_data;
137 static const ALCchar pulse_device[] = "PulseAudio Software";
138 static const ALCchar pulse_capture_device[] = "PulseAudio Capture";
139 static pa_context_flags_t pulse_ctx_flags;
140 static volatile ALuint load_count;
143 void *pulse_load(void) //{{{
145 if(load_count == 0)
147 #ifdef _WIN32
148 pa_handle = LoadLibrary("libpulse-0.dll");
149 #define LOAD_FUNC(x) do { \
150 p##x = (typeof(p##x))GetProcAddress(pa_handle, #x); \
151 if(!(p##x)) { \
152 AL_PRINT("Could not load %s from libpulse-0.dll\n", #x); \
153 FreeLibrary(pa_handle); \
154 pa_handle = NULL; \
155 return NULL; \
157 } while(0)
158 #define LOAD_OPTIONAL_FUNC(x) do { \
159 p##x = (typeof(p##x))GetProcAddress(pa_handle, #x); \
160 } while(0)
162 #elif defined (HAVE_DLFCN_H)
164 const char *err;
165 #if defined(__APPLE__) && defined(__MACH__)
166 pa_handle = dlopen("libpulse.0.dylib", RTLD_NOW);
167 #else
168 pa_handle = dlopen("libpulse.so.0", RTLD_NOW);
169 #endif
170 dlerror();
172 #define LOAD_FUNC(x) do { \
173 p##x = dlsym(pa_handle, #x); \
174 if((err=dlerror()) != NULL) { \
175 AL_PRINT("Could not load %s from libpulse: %s\n", #x, err); \
176 dlclose(pa_handle); \
177 pa_handle = NULL; \
178 return NULL; \
180 } while(0)
181 #define LOAD_OPTIONAL_FUNC(x) do { \
182 p##x = dlsym(pa_handle, #x); \
183 if((err=dlerror()) != NULL) { \
184 p##x = NULL; \
186 } while(0)
188 #else
190 pa_handle = (void*)0xDEADBEEF;
191 #define LOAD_FUNC(x) p##x = (x)
192 #define LOAD_OPTIONAL_FUNC(x) p##x = (x)
194 #endif
195 if(!pa_handle)
196 return NULL;
198 LOAD_FUNC(pa_context_unref);
199 LOAD_FUNC(pa_sample_spec_valid);
200 LOAD_FUNC(pa_stream_drop);
201 LOAD_FUNC(pa_strerror);
202 LOAD_FUNC(pa_context_get_state);
203 LOAD_FUNC(pa_stream_get_state);
204 LOAD_FUNC(pa_threaded_mainloop_signal);
205 LOAD_FUNC(pa_stream_peek);
206 LOAD_FUNC(pa_threaded_mainloop_wait);
207 LOAD_FUNC(pa_threaded_mainloop_unlock);
208 LOAD_FUNC(pa_threaded_mainloop_in_thread);
209 LOAD_FUNC(pa_context_new);
210 LOAD_FUNC(pa_threaded_mainloop_stop);
211 LOAD_FUNC(pa_context_disconnect);
212 LOAD_FUNC(pa_threaded_mainloop_start);
213 LOAD_FUNC(pa_threaded_mainloop_get_api);
214 LOAD_FUNC(pa_context_set_state_callback);
215 LOAD_FUNC(pa_stream_write);
216 LOAD_FUNC(pa_xfree);
217 LOAD_FUNC(pa_stream_connect_record);
218 LOAD_FUNC(pa_stream_connect_playback);
219 LOAD_FUNC(pa_stream_readable_size);
220 LOAD_FUNC(pa_stream_cork);
221 LOAD_FUNC(pa_path_get_filename);
222 LOAD_FUNC(pa_get_binary_name);
223 LOAD_FUNC(pa_threaded_mainloop_free);
224 LOAD_FUNC(pa_context_errno);
225 LOAD_FUNC(pa_xmalloc);
226 LOAD_FUNC(pa_stream_unref);
227 LOAD_FUNC(pa_threaded_mainloop_accept);
228 LOAD_FUNC(pa_stream_set_write_callback);
229 LOAD_FUNC(pa_threaded_mainloop_new);
230 LOAD_FUNC(pa_context_connect);
231 LOAD_FUNC(pa_stream_set_buffer_attr);
232 LOAD_FUNC(pa_stream_get_buffer_attr);
233 LOAD_FUNC(pa_stream_get_sample_spec);
234 LOAD_FUNC(pa_stream_set_read_callback);
235 LOAD_FUNC(pa_stream_set_state_callback);
236 LOAD_FUNC(pa_stream_new);
237 LOAD_FUNC(pa_stream_disconnect);
238 LOAD_FUNC(pa_threaded_mainloop_lock);
239 LOAD_FUNC(pa_channel_map_init_auto);
240 LOAD_FUNC(pa_channel_map_parse);
241 LOAD_FUNC(pa_channel_map_snprint);
242 LOAD_FUNC(pa_channel_map_equal);
243 LOAD_FUNC(pa_context_get_server_info);
244 LOAD_FUNC(pa_context_get_sink_info_by_name);
245 LOAD_FUNC(pa_operation_get_state);
246 LOAD_FUNC(pa_operation_unref);
247 #if PA_CHECK_VERSION(0,9,15)
248 LOAD_OPTIONAL_FUNC(pa_channel_map_superset);
249 LOAD_OPTIONAL_FUNC(pa_stream_set_buffer_attr_callback);
250 #endif
251 #if PA_CHECK_VERSION(0,9,16)
252 LOAD_OPTIONAL_FUNC(pa_stream_begin_write);
253 #endif
255 #undef LOAD_OPTIONAL_FUNC
256 #undef LOAD_FUNC
258 ++load_count;
260 return pa_handle;
261 } //}}}
263 void pulse_unload(void) //{{{
265 if(load_count == 0 || --load_count > 0)
266 return;
268 #ifdef _WIN32
269 FreeLibrary(pa_handle);
270 #elif defined (HAVE_DLFCN_H)
271 dlclose(pa_handle);
272 #endif
273 pa_handle = NULL;
274 } //}}}
277 // PulseAudio Event Callbacks //{{{
278 static void context_state_callback(pa_context *context, void *pdata) //{{{
280 ALCdevice *Device = pdata;
281 pulse_data *data = Device->ExtraData;
282 pa_context_state_t state;
284 state = ppa_context_get_state(context);
285 if(state == PA_CONTEXT_READY || !PA_CONTEXT_IS_GOOD(state))
286 ppa_threaded_mainloop_signal(data->loop, 0);
287 }//}}}
289 static void stream_state_callback(pa_stream *stream, void *pdata) //{{{
291 ALCdevice *Device = pdata;
292 pulse_data *data = Device->ExtraData;
293 pa_stream_state_t state;
295 state = ppa_stream_get_state(stream);
296 if(state == PA_STREAM_READY || !PA_STREAM_IS_GOOD(state))
297 ppa_threaded_mainloop_signal(data->loop, 0);
298 }//}}}
300 static void stream_buffer_attr_callback(pa_stream *stream, void *pdata) //{{{
302 ALCdevice *Device = pdata;
303 pulse_data *data = Device->ExtraData;
305 SuspendContext(NULL);
307 data->attr = *(ppa_stream_get_buffer_attr(stream));
308 Device->UpdateSize = 20 * Device->Frequency / 1000;
309 Device->NumUpdates = data->attr.tlength/data->frame_size / Device->UpdateSize;
310 if(Device->NumUpdates == 0)
311 Device->NumUpdates = 1;
313 ProcessContext(NULL);
314 }//}}}
316 static void context_state_callback2(pa_context *context, void *pdata) //{{{
318 ALCdevice *Device = pdata;
319 pulse_data *data = Device->ExtraData;
321 if(ppa_context_get_state(context) == PA_CONTEXT_FAILED)
323 AL_PRINT("Received context failure!\n");
324 aluHandleDisconnect(Device);
326 ppa_threaded_mainloop_signal(data->loop, 0);
327 }//}}}
329 static void stream_state_callback2(pa_stream *stream, void *pdata) //{{{
331 ALCdevice *Device = pdata;
332 pulse_data *data = Device->ExtraData;
334 if(ppa_stream_get_state(stream) == PA_STREAM_FAILED)
336 AL_PRINT("Received stream failure!\n");
337 aluHandleDisconnect(Device);
339 ppa_threaded_mainloop_signal(data->loop, 0);
340 }//}}}
342 static void stream_success_callback(pa_stream *stream, int success, void *pdata) //{{{
344 ALCdevice *Device = pdata;
345 pulse_data *data = Device->ExtraData;
346 (void)stream;
347 (void)success;
349 ppa_threaded_mainloop_signal(data->loop, 0);
350 }//}}}
352 static void server_info_callback(pa_context *context, const pa_server_info *info, void *pdata) //{{{
354 struct {
355 pa_threaded_mainloop *loop;
356 char *name;
357 } *data = pdata;
358 (void)context;
360 data->name = strdup(info->default_sink_name);
361 ppa_threaded_mainloop_signal(data->loop, 0);
362 }//}}}
364 static void sink_info_callback(pa_context *context, const pa_sink_info *info, int eol, void *pdata) //{{{
366 ALCdevice *device = pdata;
367 pulse_data *data = device->ExtraData;
368 char chanmap_str[256] = "";
369 const struct {
370 const char *str;
371 ALenum format;
372 } chanmaps[] = {
373 { "front-left,front-right,front-center,lfe,rear-left,rear-right,side-left,side-right",
374 AL_FORMAT_71CHN32 },
375 { "front-left,front-right,front-center,lfe,rear-center,side-left,side-right",
376 AL_FORMAT_61CHN32 },
377 { "front-left,front-right,front-center,lfe,rear-left,rear-right",
378 AL_FORMAT_51CHN32 },
379 { "front-left,front-right,rear-left,rear-right", AL_FORMAT_QUAD32 },
380 { "front-left,front-right", AL_FORMAT_STEREO_FLOAT32 },
381 { "mono", AL_FORMAT_MONO_FLOAT32 },
382 { NULL, 0 }
384 int i;
385 (void)context;
387 if(eol)
389 ppa_threaded_mainloop_signal(data->loop, 0);
390 return;
393 for(i = 0;chanmaps[i].str;i++)
395 pa_channel_map map;
396 if(!ppa_channel_map_parse(&map, chanmaps[i].str))
397 continue;
399 if(ppa_channel_map_equal(&info->channel_map, &map)
400 #if PA_CHECK_VERSION(0,9,15)
401 || (ppa_channel_map_superset &&
402 ppa_channel_map_superset(&info->channel_map, &map))
403 #endif
406 device->Format = chanmaps[i].format;
407 return;
411 ppa_channel_map_snprint(chanmap_str, sizeof(chanmap_str), &info->channel_map);
412 AL_PRINT("Failed to find format for channel map:\n %s\n", chanmap_str);
413 }//}}}
414 //}}}
416 // PulseAudio I/O Callbacks //{{{
417 static void stream_write_callback(pa_stream *stream, size_t len, void *pdata) //{{{
419 ALCdevice *Device = pdata;
420 pulse_data *data = Device->ExtraData;
422 while(len > 0)
424 size_t newlen = len;
425 void *buf;
426 pa_free_cb_t free_func = NULL;
428 #if PA_CHECK_VERSION(0,9,16)
429 if(!ppa_stream_begin_write ||
430 ppa_stream_begin_write(stream, &buf, &newlen) < 0)
431 #endif
433 buf = ppa_xmalloc(newlen);
434 free_func = ppa_xfree;
437 aluMixData(Device, buf, newlen/data->frame_size);
438 ppa_stream_write(stream, buf, newlen, free_func, 0, PA_SEEK_RELATIVE);
439 len -= newlen;
441 } //}}}
442 //}}}
444 static ALCboolean pulse_open(ALCdevice *device, const ALCchar *device_name) //{{{
446 pulse_data *data = ppa_xmalloc(sizeof(pulse_data));
447 pa_context_state_t state;
449 memset(data, 0, sizeof(*data));
451 if(ppa_get_binary_name(data->path_name, sizeof(data->path_name)))
452 data->context_name = ppa_path_get_filename(data->path_name);
453 else
454 data->context_name = "OpenAL Soft";
456 if(!(data->loop = ppa_threaded_mainloop_new()))
458 AL_PRINT("pa_threaded_mainloop_new() failed!\n");
459 goto out;
462 if(ppa_threaded_mainloop_start(data->loop) < 0)
464 AL_PRINT("pa_threaded_mainloop_start() failed\n");
465 goto out;
468 ppa_threaded_mainloop_lock(data->loop);
469 device->ExtraData = data;
471 data->context = ppa_context_new(ppa_threaded_mainloop_get_api(data->loop), data->context_name);
472 if(!data->context)
474 AL_PRINT("pa_context_new() failed\n");
476 ppa_threaded_mainloop_unlock(data->loop);
477 goto out;
480 ppa_context_set_state_callback(data->context, context_state_callback, device);
482 if(ppa_context_connect(data->context, NULL, pulse_ctx_flags, NULL) < 0)
484 AL_PRINT("Context did not connect: %s\n",
485 ppa_strerror(ppa_context_errno(data->context)));
487 ppa_context_unref(data->context);
488 data->context = NULL;
490 ppa_threaded_mainloop_unlock(data->loop);
491 goto out;
494 while((state=ppa_context_get_state(data->context)) != PA_CONTEXT_READY)
496 if(!PA_CONTEXT_IS_GOOD(state))
498 int err = ppa_context_errno(data->context);
499 if(err != PA_ERR_CONNECTIONREFUSED)
500 AL_PRINT("Context did not get ready: %s\n", ppa_strerror(err));
502 ppa_context_unref(data->context);
503 data->context = NULL;
505 ppa_threaded_mainloop_unlock(data->loop);
506 goto out;
509 ppa_threaded_mainloop_wait(data->loop);
511 ppa_context_set_state_callback(data->context, context_state_callback2, device);
513 device->szDeviceName = strdup(device_name);
515 ppa_threaded_mainloop_unlock(data->loop);
516 return ALC_TRUE;
518 out:
519 if(data->loop)
521 ppa_threaded_mainloop_stop(data->loop);
522 ppa_threaded_mainloop_free(data->loop);
525 device->ExtraData = NULL;
526 ppa_xfree(data);
527 return ALC_FALSE;
528 } //}}}
530 static void pulse_close(ALCdevice *device) //{{{
532 pulse_data *data = device->ExtraData;
534 ppa_threaded_mainloop_lock(data->loop);
536 if(data->stream)
538 ppa_stream_disconnect(data->stream);
539 ppa_stream_unref(data->stream);
542 ppa_context_disconnect(data->context);
543 ppa_context_unref(data->context);
545 ppa_threaded_mainloop_unlock(data->loop);
547 ppa_threaded_mainloop_stop(data->loop);
548 ppa_threaded_mainloop_free(data->loop);
550 DestroyRingBuffer(data->ring);
552 device->ExtraData = NULL;
553 ppa_xfree(data);
554 } //}}}
555 //}}}
557 // OpenAL {{{
558 static ALCboolean pulse_open_playback(ALCdevice *device, const ALCchar *device_name) //{{{
560 if(!device_name)
561 device_name = pulse_device;
562 else if(strcmp(device_name, pulse_device) != 0)
563 return ALC_FALSE;
565 if(!pulse_load())
566 return ALC_FALSE;
568 if(pulse_open(device, device_name) != ALC_FALSE)
570 ALuint len = GetConfigValueInt("pulse", "buffer-length", 2048);
571 if(len != 0)
573 device->UpdateSize = len;
574 device->NumUpdates = 1;
576 return ALC_TRUE;
579 pulse_unload();
580 return ALC_FALSE;
581 } //}}}
583 static void pulse_close_playback(ALCdevice *device) //{{{
585 pulse_close(device);
586 pulse_unload();
587 } //}}}
589 static ALCboolean pulse_reset_playback(ALCdevice *device) //{{{
591 pulse_data *data = device->ExtraData;
592 pa_stream_flags_t flags = 0;
593 pa_stream_state_t state;
594 pa_channel_map chanmap;
596 ppa_threaded_mainloop_lock(data->loop);
598 if(!ConfigValueExists(NULL, "format"))
600 pa_operation *o;
601 struct {
602 pa_threaded_mainloop *loop;
603 char *name;
604 } server_data;
605 server_data.loop = data->loop;
606 server_data.name = NULL;
608 o = ppa_context_get_server_info(data->context, server_info_callback, &server_data);
609 while(ppa_operation_get_state(o) == PA_OPERATION_RUNNING)
610 ppa_threaded_mainloop_wait(data->loop);
611 ppa_operation_unref(o);
612 if(server_data.name)
614 o = ppa_context_get_sink_info_by_name(data->context, server_data.name, sink_info_callback, device);
615 while(ppa_operation_get_state(o) == PA_OPERATION_RUNNING)
616 ppa_threaded_mainloop_wait(data->loop);
617 ppa_operation_unref(o);
618 free(server_data.name);
621 if(!ConfigValueExists(NULL, "frequency"))
622 flags |= PA_STREAM_FIX_RATE;
624 data->frame_size = aluBytesFromFormat(device->Format) *
625 aluChannelsFromFormat(device->Format);
626 data->stream_name = "Playback Stream";
627 data->attr.minreq = -1;
628 data->attr.prebuf = -1;
629 data->attr.fragsize = -1;
630 data->attr.tlength = device->UpdateSize * device->NumUpdates *
631 data->frame_size;
632 data->attr.maxlength = data->attr.tlength;
634 switch(aluBytesFromFormat(device->Format))
636 case 1:
637 data->spec.format = PA_SAMPLE_U8;
638 break;
639 case 2:
640 data->spec.format = PA_SAMPLE_S16NE;
641 break;
642 case 4:
643 data->spec.format = PA_SAMPLE_FLOAT32NE;
644 break;
645 default:
646 AL_PRINT("Unknown format: 0x%x\n", device->Format);
647 ppa_threaded_mainloop_unlock(data->loop);
648 return ALC_FALSE;
650 data->spec.rate = device->Frequency;
651 data->spec.channels = aluChannelsFromFormat(device->Format);
653 if(ppa_sample_spec_valid(&data->spec) == 0)
655 AL_PRINT("Invalid sample format\n");
656 ppa_threaded_mainloop_unlock(data->loop);
657 return ALC_FALSE;
660 if(!ppa_channel_map_init_auto(&chanmap, data->spec.channels, PA_CHANNEL_MAP_WAVEEX))
662 AL_PRINT("Couldn't build map for channel count (%d)!\n", data->spec.channels);
663 ppa_threaded_mainloop_unlock(data->loop);
664 return ALC_FALSE;
666 SetDefaultWFXChannelOrder(device);
668 data->stream = ppa_stream_new(data->context, data->stream_name, &data->spec, &chanmap);
669 if(!data->stream)
671 AL_PRINT("pa_stream_new() failed: %s\n",
672 ppa_strerror(ppa_context_errno(data->context)));
674 ppa_threaded_mainloop_unlock(data->loop);
675 return ALC_FALSE;
678 ppa_stream_set_state_callback(data->stream, stream_state_callback, device);
680 if(ppa_stream_connect_playback(data->stream, NULL, &data->attr, flags, NULL, NULL) < 0)
682 AL_PRINT("Stream did not connect: %s\n",
683 ppa_strerror(ppa_context_errno(data->context)));
685 ppa_stream_unref(data->stream);
686 data->stream = NULL;
688 ppa_threaded_mainloop_unlock(data->loop);
689 return ALC_FALSE;
692 while((state=ppa_stream_get_state(data->stream)) != PA_STREAM_READY)
694 if(!PA_STREAM_IS_GOOD(state))
696 AL_PRINT("Stream did not get ready: %s\n",
697 ppa_strerror(ppa_context_errno(data->context)));
699 ppa_stream_unref(data->stream);
700 data->stream = NULL;
702 ppa_threaded_mainloop_unlock(data->loop);
703 return ALC_FALSE;
706 ppa_threaded_mainloop_wait(data->loop);
708 ppa_stream_set_state_callback(data->stream, stream_state_callback2, device);
710 data->spec = *(ppa_stream_get_sample_spec(data->stream));
711 if(device->Frequency != data->spec.rate)
713 pa_operation *o;
715 /* Server updated our playback rate, so modify the buffer attribs
716 * accordingly. */
717 data->attr.tlength = (ALuint64)(data->attr.tlength/data->frame_size) *
718 data->spec.rate / device->Frequency * data->frame_size;
719 data->attr.maxlength = data->attr.tlength;
721 o = ppa_stream_set_buffer_attr(data->stream, &data->attr,
722 stream_success_callback, device);
723 while(ppa_operation_get_state(o) == PA_OPERATION_RUNNING)
724 ppa_threaded_mainloop_wait(data->loop);
725 ppa_operation_unref(o);
727 device->Frequency = data->spec.rate;
730 stream_buffer_attr_callback(data->stream, device);
731 #if PA_CHECK_VERSION(0,9,15)
732 if(ppa_stream_set_buffer_attr_callback)
733 ppa_stream_set_buffer_attr_callback(data->stream, stream_buffer_attr_callback, device);
734 #endif
736 stream_write_callback(data->stream, data->attr.tlength, device);
737 ppa_stream_set_write_callback(data->stream, stream_write_callback, device);
739 ppa_threaded_mainloop_unlock(data->loop);
740 return ALC_TRUE;
741 } //}}}
743 static void pulse_stop_playback(ALCdevice *device) //{{{
745 pulse_data *data = device->ExtraData;
747 if(!data->stream)
748 return;
750 ppa_threaded_mainloop_lock(data->loop);
752 ppa_stream_disconnect(data->stream);
753 ppa_stream_unref(data->stream);
754 data->stream = NULL;
756 ppa_threaded_mainloop_unlock(data->loop);
757 } //}}}
760 static ALCboolean pulse_open_capture(ALCdevice *device, const ALCchar *device_name) //{{{
762 pulse_data *data;
763 pa_stream_flags_t flags = 0;
764 pa_stream_state_t state;
765 pa_channel_map chanmap;
767 if(!device_name)
768 device_name = pulse_capture_device;
769 else if(strcmp(device_name, pulse_capture_device) != 0)
770 return ALC_FALSE;
772 if(!pulse_load())
773 return ALC_FALSE;
775 if(pulse_open(device, device_name) == ALC_FALSE)
777 pulse_unload();
778 return ALC_FALSE;
781 data = device->ExtraData;
782 ppa_threaded_mainloop_lock(data->loop);
784 data->samples = device->UpdateSize * device->NumUpdates;
785 data->frame_size = aluBytesFromFormat(device->Format) *
786 aluChannelsFromFormat(device->Format);
788 if(!(data->ring = CreateRingBuffer(data->frame_size, data->samples)))
790 ppa_threaded_mainloop_unlock(data->loop);
791 goto fail;
794 data->attr.minreq = -1;
795 data->attr.prebuf = -1;
796 data->attr.maxlength = data->frame_size * data->samples;
797 data->attr.tlength = -1;
798 data->attr.fragsize = min(data->frame_size * data->samples,
799 10 * device->Frequency / 1000);
800 data->stream_name = "Capture Stream";
802 data->spec.rate = device->Frequency;
803 data->spec.channels = aluChannelsFromFormat(device->Format);
805 switch(aluBytesFromFormat(device->Format))
807 case 1:
808 data->spec.format = PA_SAMPLE_U8;
809 break;
810 case 2:
811 data->spec.format = PA_SAMPLE_S16NE;
812 break;
813 case 4:
814 data->spec.format = PA_SAMPLE_FLOAT32NE;
815 break;
816 default:
817 AL_PRINT("Unknown format: 0x%x\n", device->Format);
818 ppa_threaded_mainloop_unlock(data->loop);
819 goto fail;
822 if(ppa_sample_spec_valid(&data->spec) == 0)
824 AL_PRINT("Invalid sample format\n");
825 ppa_threaded_mainloop_unlock(data->loop);
826 goto fail;
829 if(!ppa_channel_map_init_auto(&chanmap, data->spec.channels, PA_CHANNEL_MAP_WAVEEX))
831 AL_PRINT("Couldn't build map for channel count (%d)!\n", data->spec.channels);
832 ppa_threaded_mainloop_unlock(data->loop);
833 goto fail;
836 data->stream = ppa_stream_new(data->context, data->stream_name, &data->spec, &chanmap);
837 if(!data->stream)
839 AL_PRINT("pa_stream_new() failed: %s\n",
840 ppa_strerror(ppa_context_errno(data->context)));
842 ppa_threaded_mainloop_unlock(data->loop);
843 goto fail;
846 ppa_stream_set_state_callback(data->stream, stream_state_callback, device);
848 flags |= PA_STREAM_START_CORKED|PA_STREAM_ADJUST_LATENCY;
849 if(ppa_stream_connect_record(data->stream, NULL, &data->attr, flags) < 0)
851 AL_PRINT("Stream did not connect: %s\n",
852 ppa_strerror(ppa_context_errno(data->context)));
854 ppa_stream_unref(data->stream);
855 data->stream = NULL;
857 ppa_threaded_mainloop_unlock(data->loop);
858 goto fail;
861 while((state=ppa_stream_get_state(data->stream)) != PA_STREAM_READY)
863 if(!PA_STREAM_IS_GOOD(state))
865 AL_PRINT("Stream did not get ready: %s\n",
866 ppa_strerror(ppa_context_errno(data->context)));
868 ppa_stream_unref(data->stream);
869 data->stream = NULL;
871 ppa_threaded_mainloop_unlock(data->loop);
872 goto fail;
875 ppa_threaded_mainloop_wait(data->loop);
877 ppa_stream_set_state_callback(data->stream, stream_state_callback2, device);
879 ppa_threaded_mainloop_unlock(data->loop);
880 return ALC_TRUE;
882 fail:
883 pulse_close(device);
884 pulse_unload();
885 return ALC_FALSE;
886 } //}}}
888 static void pulse_close_capture(ALCdevice *device) //{{{
890 pulse_close(device);
891 pulse_unload();
892 } //}}}
894 static void pulse_start_capture(ALCdevice *device) //{{{
896 pulse_data *data = device->ExtraData;
897 pa_operation *o;
899 ppa_threaded_mainloop_lock(data->loop);
900 o = ppa_stream_cork(data->stream, 0, stream_success_callback, device);
901 while(ppa_operation_get_state(o) == PA_OPERATION_RUNNING)
902 ppa_threaded_mainloop_wait(data->loop);
903 ppa_operation_unref(o);
904 ppa_threaded_mainloop_unlock(data->loop);
905 } //}}}
907 static void pulse_stop_capture(ALCdevice *device) //{{{
909 pulse_data *data = device->ExtraData;
910 pa_operation *o;
912 ppa_threaded_mainloop_lock(data->loop);
913 o = ppa_stream_cork(data->stream, 1, stream_success_callback, device);
914 while(ppa_operation_get_state(o) == PA_OPERATION_RUNNING)
915 ppa_threaded_mainloop_wait(data->loop);
916 ppa_operation_unref(o);
917 ppa_threaded_mainloop_unlock(data->loop);
918 } //}}}
920 static void pulse_capture_samples(ALCdevice *device, ALCvoid *buffer, ALCuint samples) //{{{
922 pulse_data *data = device->ExtraData;
923 ALCuint available = RingBufferSize(data->ring);
924 const void *buf;
925 size_t length;
927 available *= data->frame_size;
928 samples *= data->frame_size;
930 ppa_threaded_mainloop_lock(data->loop);
931 if(available+ppa_stream_readable_size(data->stream) < samples)
933 ppa_threaded_mainloop_unlock(data->loop);
934 alcSetError(device, ALC_INVALID_VALUE);
935 return;
938 available = min(available, samples);
939 if(available > 0)
941 ReadRingBuffer(data->ring, buffer, available/data->frame_size);
942 buffer = (ALubyte*)buffer + available;
943 samples -= available;
946 /* Capture is done in fragment-sized chunks, so we loop until we get all
947 * that's requested */
948 while(samples > 0)
950 if(ppa_stream_peek(data->stream, &buf, &length) < 0)
952 AL_PRINT("pa_stream_peek() failed: %s\n",
953 ppa_strerror(ppa_context_errno(data->context)));
954 break;
956 available = min(length, samples);
958 memcpy(buffer, buf, available);
959 buffer = (ALubyte*)buffer + available;
960 buf = (const ALubyte*)buf + available;
961 samples -= available;
962 length -= available;
964 /* Any unread data in the fragment will be lost, so save it */
965 length /= data->frame_size;
966 if(length > 0)
968 if(length > data->samples)
969 length = data->samples;
970 WriteRingBuffer(data->ring, buf, length);
973 ppa_stream_drop(data->stream);
975 ppa_threaded_mainloop_unlock(data->loop);
976 } //}}}
978 static ALCuint pulse_available_samples(ALCdevice *device) //{{{
980 pulse_data *data = device->ExtraData;
981 ALCuint ret;
983 ppa_threaded_mainloop_lock(data->loop);
984 ret = RingBufferSize(data->ring);
985 ret += ppa_stream_readable_size(data->stream)/data->frame_size;
986 ppa_threaded_mainloop_unlock(data->loop);
988 return ret;
989 } //}}}
991 BackendFuncs pulse_funcs = { //{{{
992 pulse_open_playback,
993 pulse_close_playback,
994 pulse_reset_playback,
995 pulse_stop_playback,
996 pulse_open_capture,
997 pulse_close_capture,
998 pulse_start_capture,
999 pulse_stop_capture,
1000 pulse_capture_samples,
1001 pulse_available_samples
1002 }; //}}}
1004 void alc_pulse_init(BackendFuncs *func_list) //{{{
1006 *func_list = pulse_funcs;
1008 pulse_ctx_flags = 0;
1009 if(!GetConfigValueBool("pulse", "spawn-server", 0))
1010 pulse_ctx_flags |= PA_CONTEXT_NOAUTOSPAWN;
1011 } //}}}
1013 void alc_pulse_deinit(void) //{{{
1015 } //}}}
1017 void alc_pulse_probe(int type) //{{{
1019 if(!pulse_load()) return;
1021 if(type == DEVICE_PROBE)
1022 AppendDeviceList(pulse_device);
1023 else if(type == ALL_DEVICE_PROBE)
1024 AppendAllDeviceList(pulse_device);
1025 else if(type == CAPTURE_DEVICE_PROBE)
1026 AppendCaptureDeviceList(pulse_capture_device);
1028 pulse_unload();
1029 } //}}}
1030 //}}}