Debian Codegraph @

addons/audio/pulseaudio.c

raw
1 /* ______ ___ ___
2 * /\ _ \ /\_ \ /\_ \
3 * \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___
4 * \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\
5 * \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \
6 * \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
7 * \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
8 * /\____/
9 * \_/__/
10 *
11 * PulseAudio sound driver.
12 *
13 * By Matthew Leverton.
14 *
15 * See readme.txt for copyright information.
16 */
17
18 #include "allegro5/allegro.h"
19 #include "allegro5/internal/aintern_audio.h"
20
21 #include <pulse/simple.h>
22 #include <pulse/error.h>
23 #include <pulse/introspect.h>
24 #include <pulse/mainloop.h>
25 #include <stdlib.h>
26
27 ALLEGRO_DEBUG_CHANNEL("PulseAudio")
28
29 enum PULSEAUDIO_VOICE_STATUS {
30 PV_IDLE,
31 PV_PLAYING,
32 PV_STOPPING,
33 PV_JOIN
34 };
35
36 typedef struct PULSEAUDIO_VOICE
37 {
38 pa_simple *s;
39 unsigned int buffer_size_in_frames;
40 unsigned int frame_size_in_bytes;
41
42 ALLEGRO_THREAD *poll_thread;
43 /* status_cond and status are protected by voice->mutex.
44 * Using another mutex introduces a deadlock if waiting for a change in
45 * status (while holding voice->mutex, acquired by a higher layer)
46 * and the background thread tries to acquire voice->mutex as well.
47 */
48 ALLEGRO_COND *status_cond;
49 enum PULSEAUDIO_VOICE_STATUS status;
50
51 // direct buffer (non-streaming):
52 ALLEGRO_MUTEX *buffer_mutex;
53 char *buffer;
54 char *buffer_end;
55 } PULSEAUDIO_VOICE;
56
57 static _AL_LIST* output_device_list;
58
59 #define DEFAULT_BUFFER_SIZE 1024
60 #define MIN_BUFFER_SIZE 128
61
62 static unsigned int get_buffer_size(const ALLEGRO_CONFIG *config)
63 {
64 if (config) {
65 const char *val = al_get_config_value(config,
66 "pulseaudio", "buffer_size");
67 if (val && val[0] != '\0') {
68 int n = atoi(val);
69 if (n < MIN_BUFFER_SIZE)
70 n = MIN_BUFFER_SIZE;
71 return n;
72 }
73 }
74
75 return DEFAULT_BUFFER_SIZE;
76 }
77
78 static void _output_device_list_dtor(void* value, void* userdata)
79 {
80 (void)userdata;
81
82 ALLEGRO_AUDIO_DEVICE* device = (ALLEGRO_AUDIO_DEVICE*)value;
83 al_free(device->name);
84 al_free(device->identifier);
85 al_free(device);
86 }
87
88 static void sink_info_cb(pa_context *c, const pa_sink_info *i, int eol,
89 void *userdata)
90 {
91 (void)c;
92 (void)eol;
93
94 if (!output_device_list) {
95 output_device_list = _al_list_create();
96 }
97
98 pa_sink_state_t *ret = userdata;
99 if (!i)
100 return;
101 *ret = i->state;
102
103 int iden_len = strlen(i->name) + 1;
104 int name_len = strlen(i->description) + 1;
105 ALLEGRO_AUDIO_DEVICE* device = (ALLEGRO_AUDIO_DEVICE*)al_malloc(sizeof(ALLEGRO_AUDIO_DEVICE));
106 device->identifier = (void*)al_malloc(iden_len);
107 device->name = (char*)al_malloc(name_len);
108
109 strcpy(device->name, i->description);
110 strcpy(device->identifier, i->name);
111
112 _al_list_push_back_ex(output_device_list, device, _output_device_list_dtor);
113 }
114
115 static int pulseaudio_open(void)
116 {
117 /* Use PA_CONTEXT_NOAUTOSPAWN to see if a PA server is running.
118 * If not, fail - we're better off using ALSA/OSS.
119 *
120 * Also check for suspended PA - again better using ALSA/OSS in
121 * that case (pa_simple_write just blocks until PA is unsuspended
122 * otherwise).
123 *
124 * TODO: Maybe we should have a force flag to the audio driver
125 * open method, which in the case of PA would spawn a server if
126 * none is running (and also unsuspend?).
127 */
128
129 pa_mainloop *mainloop = pa_mainloop_new();
130 pa_context *c = pa_context_new(pa_mainloop_get_api(mainloop),
131 al_get_app_name());
132 if (!c) {
133 pa_mainloop_free(mainloop);
134 return 1;
135 }
136
137 pa_context_connect(c, NULL, PA_CONTEXT_NOAUTOSPAWN, NULL);
138
139 while (1) {
140 /* Don't block or it will hang if there is no server to connect to. */
141 const int blocking = 0;
142 if (pa_mainloop_iterate(mainloop, blocking, NULL) < 0) {
143 ALLEGRO_ERROR("pa_mainloop_iterate failed\n");
144 pa_context_disconnect(c);
145 pa_context_unref(c);
146 pa_mainloop_free(mainloop);
147 return 1;
148 }
149 pa_context_state_t s = pa_context_get_state(c);
150 if (s == PA_CONTEXT_READY) {
151 ALLEGRO_DEBUG("PA_CONTEXT_READY\n");
152 break;
153 }
154 if (s == PA_CONTEXT_FAILED) {
155 ALLEGRO_ERROR("PA_CONTEXT_FAILED\n");
156 pa_context_disconnect(c);
157 pa_context_unref(c);
158 pa_mainloop_free(mainloop);
159 return 1;
160 }
161 }
162
163 pa_sink_state_t state = 0;
164 pa_operation *op = pa_context_get_sink_info_list(c, sink_info_cb, &state);
165 while (pa_operation_get_state(op) == PA_OPERATION_RUNNING) {
166 pa_mainloop_iterate(mainloop, 1, NULL);
167 }
168 /*if (state == PA_SINK_SUSPENDED) {
169 pa_context_disconnect(c);
170 pa_context_unref(c);
171 pa_mainloop_free(mainloop);
172 return 1;
173 }*/
174
175 pa_operation_unref(op);
176 pa_context_disconnect(c);
177 pa_context_unref(c);
178 pa_mainloop_free(mainloop);
179 return 0;
180 }
181
182 static void pulseaudio_close(void)
183 {
184 _al_list_destroy(output_device_list);
185 }
186
187 static void *pulseaudio_update(ALLEGRO_THREAD *self, void *data)
188 {
189 ALLEGRO_VOICE *voice = data;
190 PULSEAUDIO_VOICE *pv = voice->extra;
191 (void)self;
192
193 void* silence = al_malloc(pv->buffer_size_in_frames * pv->frame_size_in_bytes);
194 al_fill_silence(silence, pv->buffer_size_in_frames, voice->depth, voice->chan_conf);
195
196 for (;;) {
197 enum PULSEAUDIO_VOICE_STATUS status;
198
199 al_lock_mutex(voice->mutex);
200 while ((status = pv->status) == PV_IDLE) {
201 al_wait_cond(pv->status_cond, voice->mutex);
202 }
203 al_unlock_mutex(voice->mutex);
204
205 if (status == PV_JOIN) {
206 break;
207 }
208
209 if (status == PV_PLAYING) {
210 unsigned int frames = pv->buffer_size_in_frames;
211 if (voice->is_streaming) {
212 // streaming audio
213 const void *data = _al_voice_update(voice, voice->mutex, &frames);
214 if (!data) {
215 data = silence;
216 frames = pv->buffer_size_in_frames;
217 }
218 pa_simple_write(pv->s, data,
219 frames * pv->frame_size_in_bytes, NULL);
220 }
221 else {
222 // direct buffer audio
223 al_lock_mutex(pv->buffer_mutex);
224 const char *data = pv->buffer;
225 unsigned int len = frames * pv->frame_size_in_bytes;
226 pv->buffer += frames * pv->frame_size_in_bytes;
227 if (pv->buffer > pv->buffer_end) {
228 len = pv->buffer_end - data;
229 pv->buffer = voice->attached_stream->spl_data.buffer.ptr;
230 voice->attached_stream->pos = 0;
231 if (voice->attached_stream->loop == ALLEGRO_PLAYMODE_ONCE) {
232 al_lock_mutex(voice->mutex);
233 pv->status = PV_STOPPING;
234 al_broadcast_cond(pv->status_cond);
235 al_unlock_mutex(voice->mutex);
236 }
237 }
238 else {
239 voice->attached_stream->pos += frames;
240 }
241 al_unlock_mutex(pv->buffer_mutex);
242
243 pa_simple_write(pv->s, data, len, NULL);
244 }
245 }
246 else if (status == PV_STOPPING) {
247 pa_simple_drain(pv->s, NULL);
248 al_lock_mutex(voice->mutex);
249 pv->status = PV_IDLE;
250 al_broadcast_cond(pv->status_cond);
251 al_unlock_mutex(voice->mutex);
252 }
253 }
254 al_free(silence);
255
256 return NULL;
257 }
258
259 static int pulseaudio_allocate_voice(ALLEGRO_VOICE *voice)
260 {
261 PULSEAUDIO_VOICE *pv = al_malloc(sizeof(PULSEAUDIO_VOICE));
262 pa_sample_spec ss;
263 pa_buffer_attr ba;
264
265 ss.channels = al_get_channel_count(voice->chan_conf);
266 ss.rate = voice->frequency;
267
268 if (voice->depth == ALLEGRO_AUDIO_DEPTH_UINT8)
269 ss.format = PA_SAMPLE_U8;
270 else if (voice->depth == ALLEGRO_AUDIO_DEPTH_INT16)
271 ss.format = PA_SAMPLE_S16NE;
272 #if PA_API_VERSION > 11
273 else if (voice->depth == ALLEGRO_AUDIO_DEPTH_INT24)
274 ss.format = PA_SAMPLE_S24NE;
275 #endif
276 else if (voice->depth == ALLEGRO_AUDIO_DEPTH_FLOAT32)
277 ss.format = PA_SAMPLE_FLOAT32NE;
278 else {
279 ALLEGRO_ERROR("Unsupported PulseAudio sound format.\n");
280 al_free(pv);
281 return 1;
282 }
283
284 // These settings match what pulseaudio does by default, but with a slightly lower latency.
285 // The latency can also be controlled via PULSE_LATENCY_MSEC environment variable.
286 ba.maxlength = -1;
287 ba.tlength = pa_usec_to_bytes(50 * 1000, &ss); // 50 ms of latency by default.
288 ba.prebuf = -1;
289 ba.minreq = -1;
290 ba.fragsize = -1;
291
292 pv->s = pa_simple_new(
293 NULL, // Use the default server.
294 al_get_app_name(),
295 PA_STREAM_PLAYBACK,
296 NULL, // Use the default device.
297 "Allegro Voice",
298 &ss,
299 NULL, // Use default channel map
300 &ba,
301 NULL // Ignore error code.
302 );
303
304 if (!pv->s) {
305 al_free(pv);
306 return 1;
307 }
308
309 voice->extra = pv;
310
311 pv->buffer_size_in_frames = get_buffer_size(al_get_system_config());
312 pv->frame_size_in_bytes = ss.channels * al_get_audio_depth_size(voice->depth);
313
314 pv->status = PV_IDLE;
315 //pv->status_mutex = al_create_mutex();
316 pv->status_cond = al_create_cond();
317 pv->buffer_mutex = al_create_mutex();
318
319 pv->poll_thread = al_create_thread(pulseaudio_update, (void*)voice);
320 al_start_thread(pv->poll_thread);
321
322 return 0;
323 }
324
325 static void pulseaudio_deallocate_voice(ALLEGRO_VOICE *voice)
326 {
327 PULSEAUDIO_VOICE *pv = voice->extra;
328
329 al_lock_mutex(voice->mutex);
330 pv->status = PV_JOIN;
331 al_broadcast_cond(pv->status_cond);
332 al_unlock_mutex(voice->mutex);
333
334 /* We do NOT hold the voice mutex here, so this does NOT result in a
335 * deadlock when the thread calls _al_voice_update.
336 */
337 al_join_thread(pv->poll_thread, NULL);
338 al_destroy_thread(pv->poll_thread);
339
340 al_destroy_cond(pv->status_cond);
341 al_destroy_mutex(pv->buffer_mutex);
342
343 pa_simple_free(pv->s);
344 al_free(pv);
345 }
346
347 static int pulseaudio_load_voice(ALLEGRO_VOICE *voice, const void *data)
348 {
349 PULSEAUDIO_VOICE *pv = voice->extra;
350 (void)data;
351
352 if (voice->attached_stream->loop == ALLEGRO_PLAYMODE_BIDIR) {
353 ALLEGRO_INFO("Backwards playing not supported by the driver.\n");
354 return 1;
355 }
356
357 voice->attached_stream->pos = 0;
358
359 pv->buffer = voice->attached_stream->spl_data.buffer.ptr;
360 pv->buffer_end = pv->buffer +
361 (voice->attached_stream->spl_data.len) * pv->frame_size_in_bytes;
362
363 return 0;
364 }
365
366 static void pulseaudio_unload_voice(ALLEGRO_VOICE *voice)
367 {
368 (void) voice;
369 }
370
371 static int pulseaudio_start_voice(ALLEGRO_VOICE *voice)
372 {
373 PULSEAUDIO_VOICE *pv = voice->extra;
374 int ret;
375
376 /* We hold the voice->mutex already. */
377
378 if (pv->status == PV_IDLE) {
379 pv->status = PV_PLAYING;
380 al_broadcast_cond(pv->status_cond);
381 ret = 0;
382 }
383 else {
384 ret = 1;
385 }
386
387 return ret;
388 }
389
390 static int pulseaudio_stop_voice(ALLEGRO_VOICE *voice)
391 {
392 PULSEAUDIO_VOICE *pv = voice->extra;
393
394 /* We hold the voice->mutex already. */
395
396 if (pv->status == PV_PLAYING) {
397 pv->status = PV_STOPPING;
398 al_broadcast_cond(pv->status_cond);
399 }
400
401 while (pv->status != PV_IDLE) {
402 al_wait_cond(pv->status_cond, voice->mutex);
403 }
404
405 return 0;
406 }
407
408 static bool pulseaudio_voice_is_playing(const ALLEGRO_VOICE *voice)
409 {
410 PULSEAUDIO_VOICE *pv = voice->extra;
411 return (pv->status == PV_PLAYING);
412 }
413
414 static unsigned int pulseaudio_get_voice_position(const ALLEGRO_VOICE *voice)
415 {
416 return voice->attached_stream->pos;
417 }
418
419 static int pulseaudio_set_voice_position(ALLEGRO_VOICE *voice, unsigned int pos)
420 {
421 PULSEAUDIO_VOICE *pv = voice->extra;
422
423 pa_simple_drain(pv->s, NULL);
424
425 al_lock_mutex(pv->buffer_mutex);
426 voice->attached_stream->pos = pos;
427 pv->buffer = (char *)voice->attached_stream->spl_data.buffer.ptr +
428 pos * pv->frame_size_in_bytes;
429 al_unlock_mutex(pv->buffer_mutex);
430
431 return 0;
432 }
433
434 /* Recording */
435
436 typedef struct PULSEAUDIO_RECORDER {
437 pa_simple *s;
438 pa_sample_spec ss;
439 pa_buffer_attr ba;
440 } PULSEAUDIO_RECORDER;
441
442 static void *pulse_audio_update_recorder(ALLEGRO_THREAD *t, void *data)
443 {
444 ALLEGRO_AUDIO_RECORDER *r = (ALLEGRO_AUDIO_RECORDER *) data;
445 PULSEAUDIO_RECORDER *pa = (PULSEAUDIO_RECORDER *) r->extra;
446 ALLEGRO_EVENT user_event;
447 uint8_t *null_buffer;
448 unsigned int fragment_i = 0;
449
450 null_buffer = al_malloc(1024);
451 if (!null_buffer) {
452 ALLEGRO_ERROR("Unable to create buffer for draining PulseAudio.\n");
453 return NULL;
454 }
455
456 while (!al_get_thread_should_stop(t))
457 {
458 al_lock_mutex(r->mutex);
459 if (!r->is_recording) {
460 /* Even if not recording, we still want to read from the PA server.
461 Otherwise it will buffer everything and spit it all out whenever
462 the recording resumes. */
463 al_unlock_mutex(r->mutex);
464 pa_simple_read(pa->s, null_buffer, 1024, NULL);
465 }
466 else {
467 ALLEGRO_AUDIO_RECORDER_EVENT *e;
468 al_unlock_mutex(r->mutex);
469 if (pa_simple_read(pa->s, r->fragments[fragment_i], r->fragment_size, NULL) >= 0) {
470 user_event.user.type = ALLEGRO_EVENT_AUDIO_RECORDER_FRAGMENT;
471 e = al_get_audio_recorder_event(&user_event);
472 e->buffer = r->fragments[fragment_i];
473 e->samples = r->samples;
474 al_emit_user_event(&r->source, &user_event, NULL);
475
476 if (++fragment_i == r->fragment_count) {
477 fragment_i = 0;
478 }
479 }
480 }
481 }
482
483 al_free(null_buffer);
484 return NULL;
485 };
486
487 static int pulseaudio_allocate_recorder(ALLEGRO_AUDIO_RECORDER *r)
488 {
489 PULSEAUDIO_RECORDER *pa;
490
491 pa = al_calloc(1, sizeof(*pa));
492 if (!pa) {
493 ALLEGRO_ERROR("Unable to allocate memory for PULSEAUDIO_RECORDER.\n");
494 return 1;
495 }
496
497 pa->ss.channels = al_get_channel_count(r->chan_conf);
498 pa->ss.rate = r->frequency;
499
500 if (r->depth == ALLEGRO_AUDIO_DEPTH_UINT8)
501 pa->ss.format = PA_SAMPLE_U8;
502 else if (r->depth == ALLEGRO_AUDIO_DEPTH_INT16)
503 pa->ss.format = PA_SAMPLE_S16NE;
504 #if PA_API_VERSION > 11
505 else if (r->depth == ALLEGRO_AUDIO_DEPTH_INT24)
506 pa->ss.format = PA_SAMPLE_S24NE;
507 #endif
508 else if (r->depth == ALLEGRO_AUDIO_DEPTH_FLOAT32)
509 pa->ss.format = PA_SAMPLE_FLOAT32NE;
510 else {
511 ALLEGRO_ERROR("Unsupported PulseAudio sound format (depth).\n");
512 al_free(pa);
513 return 1;
514 }
515
516 /* maximum length of the PulseAudio buffer. -1 => let the server decide. */
517 pa->ba.maxlength = -1;
518
519 /* fragment size (bytes) controls how much data is returned back per read.
520 The documentation recommends -1 for default behavior, but that sets a
521 latency of around 2 seconds. Lower value decreases latency but increases
522 overhead.
523
524 The following attempts to set it (the base latency) to 1/8 of a second.
525 */
526 pa->ba.fragsize = (r->sample_size * r->frequency) / 8;
527
528 pa->s = pa_simple_new(NULL, al_get_app_name(), PA_STREAM_RECORD, NULL, "Allegro Audio Recorder", &pa->ss, NULL, &pa->ba, NULL);
529 if (!pa->s) {
530 ALLEGRO_ERROR("pa_simple_new() failed.\n");
531 al_free(pa);
532 return 1;
533 }
534
535 r->thread = al_create_thread(pulse_audio_update_recorder, r);
536 r->extra = pa;
537
538 return 0;
539 };
540
541 static void pulseaudio_deallocate_recorder(ALLEGRO_AUDIO_RECORDER *r)
542 {
543 PULSEAUDIO_RECORDER *pa = (PULSEAUDIO_RECORDER *) r->extra;
544
545 pa_simple_free(pa->s);
546 al_free(r->extra);
547 }
548
549 static _AL_LIST* pulseaudio_get_output_devices(void)
550 {
551 return output_device_list;
552 }
553
554 ALLEGRO_AUDIO_DRIVER _al_kcm_pulseaudio_driver =
555 {
556 "PulseAudio",
557
558 pulseaudio_open,
559 pulseaudio_close,
560
561 pulseaudio_allocate_voice,
562 pulseaudio_deallocate_voice,
563
564 pulseaudio_load_voice,
565 pulseaudio_unload_voice,
566
567 pulseaudio_start_voice,
568 pulseaudio_stop_voice,
569
570 pulseaudio_voice_is_playing,
571
572 pulseaudio_get_voice_position,
573 pulseaudio_set_voice_position,
574
575 pulseaudio_allocate_recorder,
576 pulseaudio_deallocate_recorder,
577
578 pulseaudio_get_output_devices
579 };
580
581 /* vim: set sts=3 sw=3 et: */