Clean up some loading checks
[openal-soft.git] / Alc / pulseaudio.c
blobe4f53f4c9124af43061377b95fd760052747dd0d
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_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);
103 #endif
104 #if PA_CHECK_VERSION(0,9,16)
105 MAKE_FUNC(pa_stream_begin_write);
106 #endif
107 #undef MAKE_FUNC
109 #ifndef PATH_MAX
110 #define PATH_MAX 4096
111 #endif
113 typedef struct {
114 ALCuint samples;
115 ALCuint frame_size;
117 RingBuffer *ring;
119 pa_buffer_attr attr;
120 pa_sample_spec spec;
122 char path_name[PATH_MAX];
123 const char *context_name;
124 const char *stream_name;
126 pa_threaded_mainloop *loop;
128 pa_stream *stream;
129 pa_context *context;
130 } pulse_data;
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) //{{{
139 if(load_count == 0)
141 #ifdef _WIN32
142 pa_handle = LoadLibrary("libpulse-0.dll");
143 #define LOAD_FUNC(x) do { \
144 p##x = GetProcAddress(pa_handle, #x); \
145 if(!(p##x)) { \
146 AL_PRINT("Could not load %s from libpulse-0.dll\n", #x); \
147 FreeLibrary(pa_handle); \
148 pa_handle = NULL; \
149 return NULL; \
151 } while(0)
152 #define LOAD_OPTIONAL_FUNC(x) do { \
153 p##x = GetProcAddress(pa_handle, #x); \
154 } while(0)
156 #elif defined (HAVE_DLFCN_H)
158 const char *err;
159 #if defined(__APPLE__) && defined(__MACH__)
160 pa_handle = dlopen("libpulse.0.dylib", RTLD_NOW);
161 #else
162 pa_handle = dlopen("libpulse.so.0", RTLD_NOW);
163 #endif
164 dlerror();
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); \
171 pa_handle = NULL; \
172 return NULL; \
174 } while(0)
175 #define LOAD_OPTIONAL_FUNC(x) do { \
176 p##x = dlsym(pa_handle, #x); \
177 if((err=dlerror()) != NULL) { \
178 p##x = NULL; \
180 } while(0)
182 #else
184 pa_handle = (void*)0xDEADBEEF;
185 #define LOAD_FUNC(x) p##x = (x)
186 #define LOAD_OPTIONAL_FUNC(x) p##x = (x)
188 #endif
189 if(!pa_handle)
190 return NULL;
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);
210 LOAD_FUNC(pa_xfree);
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);
239 #endif
240 #if PA_CHECK_VERSION(0,9,16)
241 LOAD_OPTIONAL_FUNC(pa_stream_begin_write);
242 #endif
244 #undef LOAD_OPTIONAL_FUNC
245 #undef LOAD_FUNC
247 ++load_count;
249 return pa_handle;
250 } //}}}
252 void pulse_unload(void) //{{{
254 if(load_count == 0 || --load_count > 0)
255 return;
257 #ifdef _WIN32
258 FreeLibrary(pa_handle);
259 #elif defined (HAVE_DLFCN_H)
260 dlclose(pa_handle);
261 #endif
262 pa_handle = NULL;
263 } //}}}
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);
279 }//}}}
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);
293 }//}}}
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);
310 }//}}}
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);
321 }//}}}
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);
332 }//}}}
334 static void stream_success_callback(pa_stream *stream, int success, void *pdata) //{{{
336 ALCdevice *Device = pdata;
337 pulse_data *data = Device->ExtraData;
338 (void)stream;
339 (void)success;
341 if(ppa_threaded_mainloop_in_thread(data->loop))
342 ppa_threaded_mainloop_signal(data->loop, 0);
343 }//}}}
344 //}}}
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;
353 if(len > 0)
355 void *buf;
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)
361 #endif
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);
370 } //}}}
371 //}}}
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);
382 else
383 data->context_name = "OpenAL Soft";
385 if(!(data->loop = ppa_threaded_mainloop_new()))
387 AL_PRINT("pa_threaded_mainloop_new() failed!\n");
388 goto out;
391 if(ppa_threaded_mainloop_start(data->loop) < 0)
393 AL_PRINT("pa_threaded_mainloop_start() failed\n");
394 goto out;
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);
401 if(!data->context)
403 AL_PRINT("pa_context_new() failed: %s\n",
404 ppa_strerror(ppa_context_errno(data->context)));
406 ppa_threaded_mainloop_unlock(data->loop);
407 goto out;
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);
421 goto out;
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);
435 goto out;
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);
445 return ALC_TRUE;
447 out:
448 if(data->loop)
450 ppa_threaded_mainloop_stop(data->loop);
451 ppa_threaded_mainloop_free(data->loop);
454 device->ExtraData = NULL;
455 ppa_xfree(data);
456 return ALC_FALSE;
457 } //}}}
459 static void pulse_close(ALCdevice *device) //{{{
461 pulse_data *data = device->ExtraData;
463 ppa_threaded_mainloop_lock(data->loop);
465 if(data->stream)
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;
482 ppa_xfree(data);
483 } //}}}
484 //}}}
486 // OpenAL {{{
487 static ALCboolean pulse_open_playback(ALCdevice *device, const ALCchar *device_name) //{{{
489 if(!device_name)
490 device_name = pulse_device;
491 else if(strcmp(device_name, pulse_device) != 0)
492 return ALC_FALSE;
494 if(!pulse_load())
495 return ALC_FALSE;
497 if(pulse_open(device, device_name) != ALC_FALSE)
498 return ALC_TRUE;
500 pulse_unload();
501 return ALC_FALSE;
502 } //}}}
504 static void pulse_close_playback(ALCdevice *device) //{{{
506 pulse_close(device);
507 pulse_unload();
508 } //}}}
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))
530 case 1:
531 data->spec.format = PA_SAMPLE_U8;
532 break;
533 case 2:
534 data->spec.format = PA_SAMPLE_S16NE;
535 break;
536 case 4:
537 data->spec.format = PA_SAMPLE_FLOAT32NE;
538 break;
539 default:
540 AL_PRINT("Unknown format: 0x%x\n", device->Format);
541 ppa_threaded_mainloop_unlock(data->loop);
542 return ALC_FALSE;
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);
551 return ALC_FALSE;
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);
558 return ALC_FALSE;
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);
566 if(!data->stream)
568 AL_PRINT("pa_stream_new() failed: %s\n",
569 ppa_strerror(ppa_context_errno(data->context)));
571 ppa_threaded_mainloop_unlock(data->loop);
572 return ALC_FALSE;
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);
583 data->stream = NULL;
585 ppa_threaded_mainloop_unlock(data->loop);
586 return ALC_FALSE;
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);
597 data->stream = NULL;
599 ppa_threaded_mainloop_unlock(data->loop);
600 return ALC_FALSE;
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)
610 pa_operation *o;
612 /* Server updated our playback rate, so modify the buffer attribs
613 * accordingly. */
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);
628 #endif
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);
634 return ALC_TRUE;
635 } //}}}
637 static void pulse_stop_playback(ALCdevice *device) //{{{
639 pulse_data *data = device->ExtraData;
641 if(!data->stream)
642 return;
644 ppa_threaded_mainloop_lock(data->loop);
646 ppa_stream_disconnect(data->stream);
647 ppa_stream_unref(data->stream);
648 data->stream = NULL;
650 ppa_threaded_mainloop_unlock(data->loop);
651 } //}}}
654 static ALCboolean pulse_open_capture(ALCdevice *device, const ALCchar *device_name) //{{{
656 pulse_data *data;
657 pa_stream_state_t state;
659 if(!device_name)
660 device_name = pulse_capture_device;
661 else if(strcmp(device_name, pulse_capture_device) != 0)
662 return ALC_FALSE;
664 if(!pulse_load())
665 return ALC_FALSE;
667 if(pulse_open(device, device_name) == ALC_FALSE)
669 pulse_unload();
670 return 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);
683 pulse_close(device);
684 pulse_unload();
685 return ALC_FALSE;
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))
701 case 1:
702 data->spec.format = PA_SAMPLE_U8;
703 break;
704 case 2:
705 data->spec.format = PA_SAMPLE_S16NE;
706 break;
707 case 4:
708 data->spec.format = PA_SAMPLE_FLOAT32NE;
709 break;
710 default:
711 AL_PRINT("Unknown format: 0x%x\n", device->Format);
712 ppa_threaded_mainloop_unlock(data->loop);
713 pulse_close(device);
714 pulse_unload();
715 return ALC_FALSE;
718 if(ppa_sample_spec_valid(&data->spec) == 0)
720 AL_PRINT("Invalid sample format\n");
721 ppa_threaded_mainloop_unlock(data->loop);
722 pulse_close(device);
723 pulse_unload();
724 return ALC_FALSE;
727 data->stream = ppa_stream_new(data->context, data->stream_name, &data->spec, NULL);
728 if(!data->stream)
730 AL_PRINT("pa_stream_new() failed: %s\n",
731 ppa_strerror(ppa_context_errno(data->context)));
733 ppa_threaded_mainloop_unlock(data->loop);
734 pulse_close(device);
735 pulse_unload();
736 return ALC_FALSE;
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);
749 data->stream = NULL;
750 pulse_close(device);
751 pulse_unload();
752 return ALC_FALSE;
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);
763 data->stream = NULL;
765 ppa_threaded_mainloop_unlock(data->loop);
767 pulse_close(device);
768 pulse_unload();
769 return ALC_FALSE;
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);
777 return ALC_TRUE;
778 } //}}}
780 static void pulse_close_capture(ALCdevice *device) //{{{
782 pulse_close(device);
783 pulse_unload();
784 } //}}}
786 static void pulse_start_capture(ALCdevice *device) //{{{
788 pulse_data *data = device->ExtraData;
789 pa_operation *o;
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);
797 } //}}}
799 static void pulse_stop_capture(ALCdevice *device) //{{{
801 pulse_data *data = device->ExtraData;
802 pa_operation *o;
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);
810 } //}}}
812 static void pulse_capture_samples(ALCdevice *device, ALCvoid *buffer, ALCuint samples) //{{{
814 pulse_data *data = device->ExtraData;
815 ALCuint available = RingBufferSize(data->ring);
816 const void *buf;
817 size_t length;
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);
827 return;
830 available = min(available, samples);
831 if(available > 0)
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 */
840 while(samples > 0)
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)));
846 break;
848 available = min(length, samples);
850 memcpy(buffer, buf, available);
851 buffer = (ALubyte*)buffer + available;
852 buf = (const ALubyte*)buf + available;
853 samples -= available;
854 length -= available;
856 /* Any unread data in the fragment will be lost, so save it */
857 length /= data->frame_size;
858 if(length > 0)
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);
868 } //}}}
870 static ALCuint pulse_available_samples(ALCdevice *device) //{{{
872 pulse_data *data = device->ExtraData;
873 ALCuint ret;
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);
880 return ret;
881 } //}}}
883 BackendFuncs pulse_funcs = { //{{{
884 pulse_open_playback,
885 pulse_close_playback,
886 pulse_reset_playback,
887 pulse_stop_playback,
888 pulse_open_capture,
889 pulse_close_capture,
890 pulse_start_capture,
891 pulse_stop_capture,
892 pulse_capture_samples,
893 pulse_available_samples
894 }; //}}}
896 void alc_pulse_init(BackendFuncs *func_list) //{{{
898 *func_list = pulse_funcs;
899 } //}}}
901 void alc_pulse_deinit(void) //{{{
903 } //}}}
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);
916 pulse_unload();
917 } //}}}
918 //}}}