2 * Copyright (C) 2011-2014 Andre Noll <maan@tuebingen.mpg.de>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file ao_write.c Paraslash's libao output plugin. */
19 #include "buffer_tree.h"
21 #include "write_common.h"
22 #include "ao_write.cmdline.h"
25 struct private_aow_data {
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;
37 static void aow_close(struct writer_node *wn)
39 struct private_aow_data *pawd = wn->private_data;
43 assert(!pawd->thread_btrn);
46 wn->private_data = NULL;
49 static void aow_pre_select(struct sched *s, void *context)
51 struct writer_node *wn = context;
52 struct private_aow_data *pawd = wn->private_data;
55 if (!pawd) { /* not yet started */
57 ret = btr_node_status(wn->btrn, wn->min_iqs, BTR_NT_LEAF);
60 return; /* no data available */
62 if (!wn->btrn) { /* EOF */
63 if (!pawd->thread_btrn) /* ready to exit */
65 /* wait for the play thread to terminate */
68 pthread_mutex_lock(&pawd->mutex);
69 ret = btr_node_status(wn->btrn, wn->min_iqs, BTR_NT_LEAF);
70 pthread_mutex_unlock(&pawd->mutex);
74 * Even though the node status is zero, we might have data available,
75 * but the output buffer is full. If we don't set a timeout here, we
76 * are woken up only if new data arrives, which might be too late and
77 * result in a buffer underrun in the playing thread. To avoid this we
78 * never sleep longer than the (default) buffer time.
81 return sched_request_timeout_ms(20, s);
86 static int aow_set_sample_format(unsigned sample_rate, unsigned channels,
87 int sample_format, ao_sample_format *result)
89 memset(result, 0, sizeof(*result));
90 switch (sample_format) {
94 return -E_AO_BAD_SAMPLE_FORMAT;
96 /* no need to set byte_format */
101 result->byte_format = AO_FMT_LITTLE;
105 result->byte_format = AO_FMT_BIG;
108 PARA_EMERG_LOG("bug: invalid sample format\n");
111 result->channels = channels;
112 result->rate = sample_rate;
116 static int aow_open_device(int id, ao_sample_format *asf, ao_option *options,
120 ao_device *dev = ao_open_live(id, asf, options);
128 msg = "No driver corresponds to driver_id";
131 msg = "This driver is not a live output device";
134 msg = "A valid option key has an invalid value";
137 msg = "Cannot open the device";
140 msg = "General libao error";
143 msg = "Unknown ao error";
146 PARA_ERROR_LOG("%s\n", msg);
147 return -E_AO_OPEN_LIVE;
150 static int aow_init(struct writer_node *wn, unsigned sample_rate,
151 unsigned channels, int sample_format)
154 ao_option *aoo = NULL;
155 ao_sample_format asf;
157 struct private_aow_data *pawd = para_malloc(sizeof(*pawd));
158 struct ao_write_args_info *conf = wn->conf;
160 if (conf->driver_given) {
161 ret = -E_AO_BAD_DRIVER;
162 id = ao_driver_id(conf->driver_arg);
164 ret = -E_AO_DEFAULT_DRIVER;
165 id = ao_default_driver_id();
169 info = ao_driver_info(id);
170 assert(info && info->short_name);
171 if (info->type == AO_TYPE_FILE) {
172 ret = -E_AO_FILE_NOT_SUPP;
175 PARA_INFO_LOG("using %s driver\n", info->short_name);
176 for (i = 0; i < conf->ao_option_given; i++) {
177 char *o = para_strdup(conf->ao_option_arg[i]), *value;
179 ret = -E_AO_BAD_OPTION;
180 value = strchr(o, ':');
187 PARA_INFO_LOG("appending option: key=%s, value=%s\n", o, value);
188 ret = ao_append_option(&aoo, o, value);
191 ret = -E_AO_APPEND_OPTION;
195 ret = aow_set_sample_format(sample_rate, channels, sample_format, &asf);
198 if (sample_format == SF_S8 || sample_format == SF_U8)
199 pawd->bytes_per_frame = channels;
201 pawd->bytes_per_frame = channels * 2;
202 ret = aow_open_device(id, &asf, aoo, &pawd->dev);
205 PARA_INFO_LOG("successfully opened %s\n", info->short_name);
206 wn->private_data = pawd;
213 __noreturn static void *aow_play(void *priv)
215 struct writer_node *wn = priv;
216 struct private_aow_data *pawd = wn->private_data;
217 struct btr_node *btrn = pawd->thread_btrn;
218 size_t frames, bytes;
222 pthread_mutex_lock(&pawd->mutex);
225 ret = btr_node_status(btrn, wn->min_iqs, BTR_NT_LEAF);
229 btr_merge(btrn, wn->min_iqs);
230 bytes = btr_next_buffer(btrn, &data);
231 frames = bytes / pawd->bytes_per_frame;
234 /* eof and less than a single frame available */
235 ret = -E_WRITE_COMMON_EOF;
239 * No data available, go to sleep and wait for the main
240 * thread to wake us up. pthread_cond_wait() unlocks
241 * the mutex while it waits and locks it again upon
244 ret = pthread_cond_wait(&pawd->data_available,
246 /* pthread_cond_wait() can never fail here */
250 bytes = frames * pawd->bytes_per_frame;
251 pthread_mutex_unlock(&pawd->mutex);
252 ret = ao_play(pawd->dev, data, bytes);
253 pthread_mutex_lock(&pawd->mutex);
254 if (ret == 0) { /* failure */
258 btr_consume(btrn, bytes);
261 btr_remove_node(&pawd->thread_btrn);
263 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
264 pthread_mutex_unlock(&pawd->mutex);
268 static int aow_create_thread(struct writer_node *wn)
270 struct private_aow_data *pawd = wn->private_data;
274 /* initialize with default attributes */
275 msg = "could not init mutex";
276 ret = pthread_mutex_init(&pawd->mutex, NULL);
280 msg = "could not initialize condition variable";
281 ret = pthread_cond_init(&pawd->data_available, NULL);
285 msg = "could not initialize thread attributes";
286 ret = pthread_attr_init(&pawd->attr);
290 /* schedule this thread under the real-time policy SCHED_FIFO */
291 msg = "could not set sched policy";
292 ret = pthread_attr_setschedpolicy(&pawd->attr, SCHED_FIFO);
296 msg = "could not set detach state to joinable";
297 ret = pthread_attr_setdetachstate(&pawd->attr, PTHREAD_CREATE_JOINABLE);
301 msg = "could not create thread";
302 ret = pthread_create(&pawd->thread, &pawd->attr, aow_play, wn);
307 PARA_ERROR_LOG("%s (%s)\n", msg, strerror(ret));
308 return -E_AO_PTHREAD;
311 static int aow_post_select(__a_unused struct sched *s, void *context)
313 struct writer_node *wn = context;
314 struct private_aow_data *pawd = wn->private_data;
318 int32_t rate, ch, format;
319 struct btr_node_description bnd;
321 ret = btr_node_status(wn->btrn, wn->min_iqs, BTR_NT_LEAF);
326 get_btr_sample_rate(wn->btrn, &rate);
327 get_btr_channels(wn->btrn, &ch);
328 get_btr_sample_format(wn->btrn, &format);
329 ret = aow_init(wn, rate, ch, format);
332 pawd = wn->private_data;
334 /* set up thread btr node */
335 bnd.name = "ao_thread_btrn";
336 bnd.parent = wn->btrn;
340 pawd->thread_btrn = btr_new_node(&bnd);
341 wn->private_data = pawd;
343 ret = aow_create_thread(wn);
345 goto remove_thread_btrn;
349 if (!pawd->thread_btrn) {
350 pthread_join(pawd->thread, NULL);
353 PARA_INFO_LOG("waiting for play thread to terminate\n");
356 pthread_mutex_lock(&pawd->mutex);
357 ret = btr_node_status(wn->btrn, wn->min_iqs, BTR_NT_LEAF);
359 btr_pushdown(wn->btrn);
360 if (pthread_cond_signal(&pawd->data_available) != 0) {
362 PARA_ERROR_LOG("pthread_cond_signal() failed\n");
363 goto remove_thread_btrn;
367 pthread_mutex_unlock(&pawd->mutex);
370 btr_remove_node(&wn->btrn);
371 pthread_cond_signal(&pawd->data_available);
372 pthread_mutex_unlock(&pawd->mutex);
375 btr_remove_node(&pawd->thread_btrn);
377 btr_remove_node(&wn->btrn);
382 __malloc static void *aow_parse_config_or_die(int argc, char **argv)
384 struct ao_write_args_info *conf = para_calloc(sizeof(*conf));
386 /* exits on errors */
387 ao_write_cmdline_parser(argc, argv, conf);
391 static void aow_free_config(void *conf)
393 ao_write_cmdline_parser_free(conf);
397 * The init function of the ao writer.
399 * \param w Pointer to the writer to initialize.
403 void ao_write_init(struct writer *w)
405 struct ao_write_args_info dummy;
406 int i, j, num_drivers, num_lines;
407 ao_info **driver_list;
408 char **dh; /* detailed help */
410 ao_write_cmdline_parser_init(&dummy);
411 w->close = aow_close;
412 w->pre_select = aow_pre_select;
413 w->post_select = aow_post_select;
414 w->parse_config_or_die = aow_parse_config_or_die;
415 w->free_config = aow_free_config;
416 w->help = (struct ggo_help)DEFINE_GGO_HELP(ao_write);
417 /* create detailed help containing all supported drivers/options */
418 for (i = 0; ao_write_args_info_detailed_help[i]; i++)
421 dh = para_malloc((num_lines + 3) * sizeof(char *));
422 for (i = 0; i < num_lines; i++)
423 dh[i] = para_strdup(ao_write_args_info_detailed_help[i]);
424 dh[num_lines++] = para_strdup("libao drivers available on this host:");
425 dh[num_lines++] = para_strdup("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
428 driver_list = ao_driver_info_list(&num_drivers);
430 for (i = 0; i < num_drivers; i++) {
431 ao_info *info = driver_list[i];
432 char *keys = NULL, *tmp = NULL;
434 if (info->type == AO_TYPE_FILE)
436 for (j = 0; j < info->option_count; j++) {
437 tmp = make_message("%s%s%s", keys? keys : "",
443 dh = para_realloc(dh, (num_lines + 6) * sizeof(char *));
444 dh[num_lines++] = make_message("%s: %s", info->short_name, info->name);
445 dh[num_lines++] = make_message("priority: %d", info->priority);
446 dh[num_lines++] = make_message("keys: %s", keys? keys : "[none]");
447 dh[num_lines++] = make_message("comment: %s", info->comment?
448 info->comment : "[none]");
449 dh[num_lines++] = para_strdup(NULL);
452 dh[num_lines] = NULL;
453 w->help.detailed_help = (const char **)dh;
454 ao_write_cmdline_parser_free(&dummy);