2 * Copyright (C) 2011-2014 Andre Noll <maan@systemlinux.org>
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 pthread_mutex_t mutex;
32 pthread_cond_t data_available;
33 struct btr_node *thread_btrn;
36 static void aow_close(struct writer_node *wn)
38 struct private_aow_data *pawd = wn->private_data;
44 wn->private_data = NULL;
48 static void aow_pre_select(struct sched *s, struct task *t)
50 struct writer_node *wn = container_of(t, struct writer_node, task);
51 struct private_aow_data *pawd = wn->private_data;
55 pthread_mutex_lock(&pawd->mutex);
56 ret = btr_node_status(wn->btrn, wn->min_iqs, BTR_NT_LEAF);
58 pthread_mutex_unlock(&pawd->mutex);
62 * Even though the node status is zero, we might have data
63 * available, but the output buffer is full. If we don't set a
64 * timeout here, we are woken up only if new data arrives,
65 * which might be too late and result in a buffer underrun in
66 * the playing thread. To avoid this we never sleep longer than
67 * the (default) buffer time.
69 return sched_request_timeout_ms(20, s);
74 static int aow_set_sample_format(unsigned sample_rate, unsigned channels,
75 int sample_format, ao_sample_format *result)
77 memset(result, 0, sizeof(*result));
78 switch (sample_format) {
82 return -E_AO_BAD_SAMPLE_FORMAT;
84 /* no need to set byte_format */
89 result->byte_format = AO_FMT_LITTLE;
93 result->byte_format = AO_FMT_BIG;
96 PARA_EMERG_LOG("bug: invalid sample format\n");
99 result->channels = channels;
100 result->rate = sample_rate;
104 static int aow_open_device(int id, ao_sample_format *asf, ao_option *options,
108 ao_device *dev = ao_open_live(id, asf, options);
116 msg = "No driver corresponds to driver_id";
119 msg = "This driver is not a live output device";
122 msg = "A valid option key has an invalid value";
125 msg = "Cannot open the device";
128 msg = "General libao error";
131 msg = "Unknown ao error";
134 PARA_ERROR_LOG("%s\n", msg);
135 return -E_AO_OPEN_LIVE;
138 static int aow_init(struct writer_node *wn, unsigned sample_rate,
139 unsigned channels, int sample_format)
142 ao_option *aoo = NULL;
143 ao_sample_format asf;
145 struct private_aow_data *pawd = para_malloc(sizeof(*pawd));
146 struct ao_write_args_info *conf = wn->conf;
149 if (conf->driver_given) {
150 ret = -E_AO_BAD_DRIVER;
151 id = ao_driver_id(conf->driver_arg);
153 ret = -E_AO_DEFAULT_DRIVER;
154 id = ao_default_driver_id();
158 info = ao_driver_info(id);
159 assert(info && info->short_name);
160 if (info->type == AO_TYPE_FILE) {
161 ret = -E_AO_FILE_NOT_SUPP;
164 PARA_INFO_LOG("using %s driver\n", info->short_name);
165 for (i = 0; i < conf->ao_option_given; i++) {
166 char *o = para_strdup(conf->ao_option_arg[i]), *value;
168 ret = -E_AO_BAD_OPTION;
169 value = strchr(o, ':');
176 PARA_INFO_LOG("appending option: key=%s, value=%s\n", o, value);
177 ret = ao_append_option(&aoo, o, value);
180 ret = -E_AO_APPEND_OPTION;
184 ret = aow_set_sample_format(sample_rate, channels, sample_format, &asf);
187 if (sample_format == SF_S8 || sample_format == SF_U8)
188 pawd->bytes_per_frame = channels;
190 pawd->bytes_per_frame = channels * 2;
191 ret = aow_open_device(id, &asf, aoo, &pawd->dev);
194 PARA_INFO_LOG("successfully opened %s\n", info->short_name);
195 wn->private_data = pawd;
202 __noreturn static void *aow_play(void *priv)
204 struct writer_node *wn = priv;
205 struct private_aow_data *pawd = wn->private_data;
206 struct btr_node *btrn = pawd->thread_btrn;
207 size_t frames, bytes;
213 * Lock mutex and wait for signal. pthread_cond_wait() will
214 * automatically and atomically unlock mutex while it waits.
216 pthread_mutex_lock(&pawd->mutex);
218 ret = btr_node_status(btrn, wn->min_iqs, BTR_NT_LEAF);
222 btr_merge(btrn, wn->min_iqs);
223 bytes = btr_next_buffer(btrn, &data);
224 frames = bytes / pawd->bytes_per_frame;
227 /* eof and less than a single frame available */
228 ret = -E_WRITE_COMMON_EOF;
231 //PARA_CRIT_LOG("waiting for data\n");
233 //pthread_mutex_unlock(&pawd->mutex);
234 pthread_cond_wait(&pawd->data_available, &pawd->mutex);
236 pthread_mutex_unlock(&pawd->mutex);
238 bytes = frames * pawd->bytes_per_frame;
240 if (ao_play(pawd->dev, data, bytes) == 0) /* failure */
242 pthread_mutex_lock(&pawd->mutex);
243 btr_consume(btrn, bytes);
244 pthread_mutex_unlock(&pawd->mutex);
247 pthread_mutex_unlock(&pawd->mutex);
250 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
254 static int aow_create_thread(struct writer_node *wn)
256 struct private_aow_data *pawd = wn->private_data;
260 /* initialize with default attributes */
261 msg = "could not init mutex";
262 ret = pthread_mutex_init(&pawd->mutex, NULL);
266 msg = "could not initialize condition variable";
267 ret = pthread_cond_init(&pawd->data_available, NULL);
271 msg = "could not initialize thread attributes";
272 ret = pthread_attr_init(&pawd->attr);
276 /* schedule this thread under the real-time policy SCHED_FIFO */
277 msg = "could not set sched policy";
278 ret = pthread_attr_setschedpolicy(&pawd->attr, SCHED_FIFO);
282 msg = "could not set detach state to joinable";
283 ret = pthread_attr_setdetachstate(&pawd->attr, PTHREAD_CREATE_JOINABLE);
287 msg = "could not create thread";
288 ret = pthread_create(&pawd->thread, &pawd->attr, aow_play, wn);
293 PARA_ERROR_LOG("%s (%s)\n", msg, strerror(ret));
294 return -E_AO_PTHREAD;
297 static int aow_post_select(__a_unused struct sched *s,
300 struct writer_node *wn = container_of(t, struct writer_node, task);
301 struct private_aow_data *pawd = wn->private_data;
305 int32_t rate, ch, format;
306 struct btr_node_description bnd;
308 ret = btr_node_status(wn->btrn, wn->min_iqs, BTR_NT_LEAF);
313 get_btr_sample_rate(wn->btrn, &rate);
314 get_btr_channels(wn->btrn, &ch);
315 get_btr_sample_format(wn->btrn, &format);
316 ret = aow_init(wn, rate, ch, format);
319 pawd = wn->private_data;
321 /* set up thread btr node */
322 bnd.name = "ao_thread_btrn";
323 bnd.parent = wn->btrn;
327 pawd->thread_btrn = btr_new_node(&bnd);
328 wn->private_data = pawd;
330 ret = aow_create_thread(wn);
332 goto remove_thread_btrn;
335 pthread_mutex_lock(&pawd->mutex);
336 ret = btr_node_status(wn->btrn, wn->min_iqs, BTR_NT_LEAF);
338 btr_pushdown(wn->btrn);
339 pthread_cond_signal(&pawd->data_available);
341 pthread_mutex_unlock(&pawd->mutex);
344 pthread_mutex_lock(&pawd->mutex);
345 btr_remove_node(&wn->btrn);
346 PARA_INFO_LOG("waiting for thread to terminate\n");
347 pthread_cond_signal(&pawd->data_available);
348 pthread_mutex_unlock(&pawd->mutex);
349 pthread_join(pawd->thread, NULL);
351 btr_remove_node(&pawd->thread_btrn);
353 btr_remove_node(&wn->btrn);
358 __malloc static void *aow_parse_config_or_die(int argc, char **argv)
360 struct ao_write_args_info *conf = para_calloc(sizeof(*conf));
362 /* exits on errors */
363 ao_write_cmdline_parser(argc, argv, conf);
367 static void aow_free_config(void *conf)
369 ao_write_cmdline_parser_free(conf);
373 * The init function of the ao writer.
375 * \param w Pointer to the writer to initialize.
379 void ao_write_init(struct writer *w)
381 struct ao_write_args_info dummy;
382 int i, j, num_drivers, num_lines;
383 ao_info **driver_list;
384 char **dh; /* detailed help */
386 ao_write_cmdline_parser_init(&dummy);
387 w->close = aow_close;
388 w->pre_select = aow_pre_select;
389 w->post_select = aow_post_select;
390 w->parse_config_or_die = aow_parse_config_or_die;
391 w->free_config = aow_free_config;
392 w->help = (struct ggo_help)DEFINE_GGO_HELP(ao_write);
393 /* create detailed help containing all supported drivers/options */
394 for (i = 0; ao_write_args_info_detailed_help[i]; i++)
397 dh = para_malloc((num_lines + 3) * sizeof(char *));
398 for (i = 0; i < num_lines; i++)
399 dh[i] = para_strdup(ao_write_args_info_detailed_help[i]);
400 dh[num_lines++] = para_strdup("libao drivers available on this host:");
401 dh[num_lines++] = para_strdup("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
404 driver_list = ao_driver_info_list(&num_drivers);
406 for (i = 0; i < num_drivers; i++) {
407 ao_info *info = driver_list[i];
408 char *keys = NULL, *tmp = NULL;
410 if (info->type == AO_TYPE_FILE)
412 for (j = 0; j < info->option_count; j++) {
413 tmp = make_message("%s%s%s", keys? keys : "",
419 dh = para_realloc(dh, (num_lines + 6) * sizeof(char *));
420 dh[num_lines++] = make_message("%s: %s", info->short_name, info->name);
421 dh[num_lines++] = make_message("priority: %d", info->priority);
422 dh[num_lines++] = make_message("keys: %s", keys? keys : "[none]");
423 dh[num_lines++] = make_message("comment: %s", info->comment?
424 info->comment : "[none]");
425 dh[num_lines++] = para_strdup(NULL);
428 dh[num_lines] = NULL;
429 w->help.detailed_help = (const char **)dh;
430 ao_write_cmdline_parser_free(&dummy);