af6882420f02377e9a48c695b72af6fdb20b43e8
[paraslash.git] / ao_write.c
1 /*
2 * Copyright (C) 2011-2014 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file ao_write.c Paraslash's libao output plugin. */
8
9 #include <pthread.h>
10 #include <ao/ao.h>
11 #include <regex.h>
12
13 #include "para.h"
14 #include "fd.h"
15 #include "string.h"
16 #include "list.h"
17 #include "sched.h"
18 #include "ggo.h"
19 #include "buffer_tree.h"
20 #include "write.h"
21 #include "write_common.h"
22 #include "ao_write.cmdline.h"
23 #include "error.h"
24
25 struct private_aow_data {
26 ao_device *dev;
27 int bytes_per_frame;
28
29 pthread_t thread;
30 pthread_attr_t attr;
31 /* The mutex and the condition variable serialize access to ->btrn */
32 pthread_mutex_t mutex;
33 pthread_cond_t data_available;
34 struct btr_node *thread_btrn;
35 };
36
37 static void aow_close(struct writer_node *wn)
38 {
39 struct private_aow_data *pawd = wn->private_data;
40
41 if (!pawd)
42 return;
43 if (pawd->thread_btrn) {
44 pthread_cancel(pawd->thread);
45 pthread_join(pawd->thread, NULL);
46 }
47 ao_close(pawd->dev);
48 free(pawd);
49 wn->private_data = NULL;
50 ao_shutdown();
51 }
52
53 static void aow_pre_select(struct sched *s, struct task *t)
54 {
55 struct writer_node *wn = container_of(t, struct writer_node, task);
56 struct private_aow_data *pawd = wn->private_data;
57 int ret;
58
59 if (!pawd) { /* not yet started */
60 assert(wn->btrn);
61 ret = btr_node_status(wn->btrn, wn->min_iqs, BTR_NT_LEAF);
62 if (ret != 0)
63 goto min_delay;
64 return; /* no data available */
65 }
66 if (!wn->btrn) { /* EOF */
67 if (!pawd->thread_btrn) /* ready to exit */
68 goto min_delay;
69 /* wait for the play thread to terminate */
70 goto timeout;
71 }
72 pthread_mutex_lock(&pawd->mutex);
73 ret = btr_node_status(wn->btrn, wn->min_iqs, BTR_NT_LEAF);
74 pthread_mutex_unlock(&pawd->mutex);
75 if (ret != 0)
76 goto min_delay;
77 /*
78 * Even though the node status is zero, we might have data available,
79 * but the output buffer is full. If we don't set a timeout here, we
80 * are woken up only if new data arrives, which might be too late and
81 * result in a buffer underrun in the playing thread. To avoid this we
82 * never sleep longer than the (default) buffer time.
83 */
84 timeout:
85 return sched_request_timeout_ms(20, s);
86 min_delay:
87 sched_min_delay(s);
88 }
89
90 static int aow_set_sample_format(unsigned sample_rate, unsigned channels,
91 int sample_format, ao_sample_format *result)
92 {
93 memset(result, 0, sizeof(*result));
94 switch (sample_format) {
95 case SF_U8:
96 case SF_U16_LE:
97 case SF_U16_BE:
98 return -E_AO_BAD_SAMPLE_FORMAT;
99 case SF_S8:
100 /* no need to set byte_format */
101 result->bits = 8;
102 break;
103 case SF_S16_LE:
104 result->bits = 16;
105 result->byte_format = AO_FMT_LITTLE;
106 break;
107 case SF_S16_BE:
108 result->bits = 16;
109 result->byte_format = AO_FMT_BIG;
110 break;
111 default:
112 PARA_EMERG_LOG("bug: invalid sample format\n");
113 exit(EXIT_FAILURE);
114 }
115 result->channels = channels;
116 result->rate = sample_rate;
117 return 1;
118 }
119
120 static int aow_open_device(int id, ao_sample_format *asf, ao_option *options,
121 ao_device **result)
122 {
123 const char *msg;
124 ao_device *dev = ao_open_live(id, asf, options);
125
126 if (dev) {
127 *result = dev;
128 return 1;
129 }
130 switch (errno) {
131 case AO_ENODRIVER:
132 msg = "No driver corresponds to driver_id";
133 break;
134 case AO_ENOTLIVE:
135 msg = "This driver is not a live output device";
136 break;
137 case AO_EBADOPTION:
138 msg = "A valid option key has an invalid value";
139 break;
140 case AO_EOPENDEVICE:
141 msg = "Cannot open the device";
142 break;
143 case AO_EFAIL:
144 msg = "General libao error";
145 break;
146 default:
147 msg = "Unknown ao error";
148 break;
149 }
150 PARA_ERROR_LOG("%s\n", msg);
151 return -E_AO_OPEN_LIVE;
152 }
153
154 static int aow_init(struct writer_node *wn, unsigned sample_rate,
155 unsigned channels, int sample_format)
156 {
157 int id, ret, i;
158 ao_option *aoo = NULL;
159 ao_sample_format asf;
160 ao_info *info;
161 struct private_aow_data *pawd = para_malloc(sizeof(*pawd));
162 struct ao_write_args_info *conf = wn->conf;
163
164 ao_initialize();
165 if (conf->driver_given) {
166 ret = -E_AO_BAD_DRIVER;
167 id = ao_driver_id(conf->driver_arg);
168 } else {
169 ret = -E_AO_DEFAULT_DRIVER;
170 id = ao_default_driver_id();
171 }
172 if (id < 0)
173 goto fail;
174 info = ao_driver_info(id);
175 assert(info && info->short_name);
176 if (info->type == AO_TYPE_FILE) {
177 ret = -E_AO_FILE_NOT_SUPP;
178 goto fail;
179 }
180 PARA_INFO_LOG("using %s driver\n", info->short_name);
181 for (i = 0; i < conf->ao_option_given; i++) {
182 char *o = para_strdup(conf->ao_option_arg[i]), *value;
183
184 ret = -E_AO_BAD_OPTION;
185 value = strchr(o, ':');
186 if (!value) {
187 free(o);
188 goto fail;
189 }
190 *value = '\0';
191 value++;
192 PARA_INFO_LOG("appending option: key=%s, value=%s\n", o, value);
193 ret = ao_append_option(&aoo, o, value);
194 free(o);
195 if (ret == 0) {
196 ret = -E_AO_APPEND_OPTION;
197 goto fail;
198 }
199 }
200 ret = aow_set_sample_format(sample_rate, channels, sample_format, &asf);
201 if (ret < 0)
202 goto fail;
203 if (sample_format == SF_S8 || sample_format == SF_U8)
204 pawd->bytes_per_frame = channels;
205 else
206 pawd->bytes_per_frame = channels * 2;
207 ret = aow_open_device(id, &asf, aoo, &pawd->dev);
208 if (ret < 0)
209 goto fail;
210 PARA_INFO_LOG("successfully opened %s\n", info->short_name);
211 wn->private_data = pawd;
212 return 1;
213 fail:
214 free(pawd);
215 return ret;
216 }
217
218 __noreturn static void *aow_play(void *priv)
219 {
220 struct writer_node *wn = priv;
221 struct private_aow_data *pawd = wn->private_data;
222 struct btr_node *btrn = pawd->thread_btrn;
223 size_t frames, bytes;
224 char *data;
225 int ret;
226
227 pthread_mutex_lock(&pawd->mutex);
228 for (;;) {
229 for (;;) {
230 ret = btr_node_status(btrn, wn->min_iqs, BTR_NT_LEAF);
231 if (ret < 0)
232 goto fail;
233 if (ret > 0) {
234 btr_merge(btrn, wn->min_iqs);
235 bytes = btr_next_buffer(btrn, &data);
236 frames = bytes / pawd->bytes_per_frame;
237 if (frames > 0)
238 break;
239 /* eof and less than a single frame available */
240 ret = -E_WRITE_COMMON_EOF;
241 goto fail;
242 }
243 /*
244 * No data available, go to sleep and wait for the main
245 * thread to wake us up. pthread_cond_wait() unlocks
246 * the mutex while it waits and locks it again upon
247 * return.
248 */
249 ret = pthread_cond_wait(&pawd->data_available,
250 &pawd->mutex);
251 /* pthread_cond_wait() can never fail here */
252 assert(ret == 0);
253 }
254 assert(frames > 0);
255 bytes = frames * pawd->bytes_per_frame;
256 pthread_mutex_unlock(&pawd->mutex);
257 ret = ao_play(pawd->dev, data, bytes);
258 pthread_mutex_lock(&pawd->mutex);
259 if (ret == 0) { /* failure */
260 ret = -E_AO_PLAY;
261 goto fail;
262 }
263 btr_consume(btrn, bytes);
264 }
265 fail:
266 btr_remove_node(&pawd->thread_btrn);
267 assert(ret < 0);
268 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
269 pthread_mutex_unlock(&pawd->mutex);
270 pthread_exit(NULL);
271 }
272
273 static int aow_create_thread(struct writer_node *wn)
274 {
275 struct private_aow_data *pawd = wn->private_data;
276 int ret;
277 const char *msg;
278
279 /* initialize with default attributes */
280 msg = "could not init mutex";
281 ret = pthread_mutex_init(&pawd->mutex, NULL);
282 if (ret < 0)
283 goto fail;
284
285 msg = "could not initialize condition variable";
286 ret = pthread_cond_init(&pawd->data_available, NULL);
287 if (ret < 0)
288 goto fail;
289
290 msg = "could not initialize thread attributes";
291 ret = pthread_attr_init(&pawd->attr);
292 if (ret < 0)
293 goto fail;
294
295 /* schedule this thread under the real-time policy SCHED_FIFO */
296 msg = "could not set sched policy";
297 ret = pthread_attr_setschedpolicy(&pawd->attr, SCHED_FIFO);
298 if (ret < 0)
299 goto fail;
300
301 msg = "could not set detach state to joinable";
302 ret = pthread_attr_setdetachstate(&pawd->attr, PTHREAD_CREATE_JOINABLE);
303 if (ret < 0)
304 goto fail;
305
306 msg = "could not create thread";
307 ret = pthread_create(&pawd->thread, &pawd->attr, aow_play, wn);
308 if (ret < 0)
309 goto fail;
310 return 1;
311 fail:
312 PARA_ERROR_LOG("%s (%s)\n", msg, strerror(ret));
313 return -E_AO_PTHREAD;
314 }
315
316 static int aow_post_select(__a_unused struct sched *s,
317 struct task *t)
318 {
319 struct writer_node *wn = container_of(t, struct writer_node, task);
320 struct private_aow_data *pawd = wn->private_data;
321 int ret;
322
323 if (!pawd) {
324 int32_t rate, ch, format;
325 struct btr_node_description bnd;
326
327 ret = btr_node_status(wn->btrn, wn->min_iqs, BTR_NT_LEAF);
328 if (ret < 0)
329 goto remove_btrn;
330 if (ret == 0)
331 return 0;
332 get_btr_sample_rate(wn->btrn, &rate);
333 get_btr_channels(wn->btrn, &ch);
334 get_btr_sample_format(wn->btrn, &format);
335 ret = aow_init(wn, rate, ch, format);
336 if (ret < 0)
337 goto remove_btrn;
338 pawd = wn->private_data;
339
340 /* set up thread btr node */
341 bnd.name = "ao_thread_btrn";
342 bnd.parent = wn->btrn;
343 bnd.child = NULL;
344 bnd.handler = NULL;
345 bnd.context = pawd;
346 pawd->thread_btrn = btr_new_node(&bnd);
347 wn->private_data = pawd;
348
349 ret = aow_create_thread(wn);
350 if (ret < 0)
351 goto remove_thread_btrn;
352 return 0;
353 }
354 if (!wn->btrn) {
355 if (!pawd->thread_btrn)
356 return -E_AO_EOF;
357 PARA_INFO_LOG("waiting for play thread to terminate\n");
358 return 0;
359 }
360 pthread_mutex_lock(&pawd->mutex);
361 ret = btr_node_status(wn->btrn, wn->min_iqs, BTR_NT_LEAF);
362 if (ret > 0) {
363 btr_pushdown(wn->btrn);
364 if (pthread_cond_signal(&pawd->data_available) != 0) {
365 ret = -E_AO_PTHREAD;
366 PARA_ERROR_LOG("pthread_cond_signal() failed\n");
367 goto remove_thread_btrn;
368 }
369 }
370 if (ret >= 0) {
371 pthread_mutex_unlock(&pawd->mutex);
372 goto out;
373 }
374 btr_remove_node(&wn->btrn);
375 pthread_cond_signal(&pawd->data_available);
376 pthread_mutex_unlock(&pawd->mutex);
377 return 0;
378 remove_thread_btrn:
379 btr_remove_node(&pawd->thread_btrn);
380 remove_btrn:
381 btr_remove_node(&wn->btrn);
382 out:
383 return ret;
384 }
385
386 __malloc static void *aow_parse_config_or_die(int argc, char **argv)
387 {
388 struct ao_write_args_info *conf = para_calloc(sizeof(*conf));
389
390 /* exits on errors */
391 ao_write_cmdline_parser(argc, argv, conf);
392 return conf;
393 }
394
395 static void aow_free_config(void *conf)
396 {
397 ao_write_cmdline_parser_free(conf);
398 }
399
400 /**
401 * The init function of the ao writer.
402 *
403 * \param w Pointer to the writer to initialize.
404 *
405 * \sa struct writer.
406 */
407 void ao_write_init(struct writer *w)
408 {
409 struct ao_write_args_info dummy;
410 int i, j, num_drivers, num_lines;
411 ao_info **driver_list;
412 char **dh; /* detailed help */
413
414 ao_write_cmdline_parser_init(&dummy);
415 w->close = aow_close;
416 w->pre_select = aow_pre_select;
417 w->post_select = aow_post_select;
418 w->parse_config_or_die = aow_parse_config_or_die;
419 w->free_config = aow_free_config;
420 w->help = (struct ggo_help)DEFINE_GGO_HELP(ao_write);
421 /* create detailed help containing all supported drivers/options */
422 for (i = 0; ao_write_args_info_detailed_help[i]; i++)
423 ; /* nothing */
424 num_lines = i;
425 dh = para_malloc((num_lines + 3) * sizeof(char *));
426 for (i = 0; i < num_lines; i++)
427 dh[i] = para_strdup(ao_write_args_info_detailed_help[i]);
428 dh[num_lines++] = para_strdup("libao drivers available on this host:");
429 dh[num_lines++] = para_strdup("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
430
431 ao_initialize();
432 driver_list = ao_driver_info_list(&num_drivers);
433
434 for (i = 0; i < num_drivers; i++) {
435 ao_info *info = driver_list[i];
436 char *keys = NULL, *tmp = NULL;
437
438 if (info->type == AO_TYPE_FILE)
439 continue;
440 for (j = 0; j < info->option_count; j++) {
441 tmp = make_message("%s%s%s", keys? keys : "",
442 keys? ", " : "",
443 info->options[j]);
444 free(keys);
445 keys = tmp;
446 }
447 dh = para_realloc(dh, (num_lines + 6) * sizeof(char *));
448 dh[num_lines++] = make_message("%s: %s", info->short_name, info->name);
449 dh[num_lines++] = make_message("priority: %d", info->priority);
450 dh[num_lines++] = make_message("keys: %s", keys? keys : "[none]");
451 dh[num_lines++] = make_message("comment: %s", info->comment?
452 info->comment : "[none]");
453 dh[num_lines++] = para_strdup(NULL);
454 free(keys);
455 }
456 dh[num_lines] = NULL;
457 w->help.detailed_help = (const char **)dh;
458 ao_write_cmdline_parser_free(&dummy);
459 ao_shutdown();
460 }
461