2 * Copyright (C) 2011-2013 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 int ret = btr_node_status(wn->btrn, wn->min_iqs, BTR_NT_LEAF);
58 static int aow_set_sample_format(unsigned sample_rate, unsigned channels,
59 int sample_format, ao_sample_format *result)
61 memset(result, 0, sizeof(*result));
62 switch (sample_format) {
66 return -E_AO_BAD_SAMPLE_FORMAT;
68 /* no need to set byte_format */
73 result->byte_format = AO_FMT_LITTLE;
77 result->byte_format = AO_FMT_BIG;
80 PARA_EMERG_LOG("bug: invalid sample format\n");
83 result->channels = channels;
84 result->rate = sample_rate;
88 static int aow_open_device(int id, ao_sample_format *asf, ao_option *options,
92 ao_device *dev = ao_open_live(id, asf, options);
100 msg = "No driver corresponds to driver_id";
103 msg = "This driver is not a live output device";
106 msg = "A valid option key has an invalid value";
109 msg = "Cannot open the device";
112 msg = "General libao error";
115 msg = "Unknown ao error";
118 PARA_ERROR_LOG("%s\n", msg);
119 return -E_AO_OPEN_LIVE;
122 static int aow_init(struct writer_node *wn, unsigned sample_rate,
123 unsigned channels, int sample_format)
126 ao_option *aoo = NULL;
127 ao_sample_format asf;
129 struct private_aow_data *pawd = para_malloc(sizeof(*pawd));
130 struct ao_write_args_info *conf = wn->conf;
133 if (conf->driver_given) {
134 ret = -E_AO_BAD_DRIVER;
135 id = ao_driver_id(conf->driver_arg);
137 ret = -E_AO_DEFAULT_DRIVER;
138 id = ao_default_driver_id();
142 info = ao_driver_info(id);
143 assert(info && info->short_name);
144 if (info->type == AO_TYPE_FILE) {
145 ret = -E_AO_FILE_NOT_SUPP;
148 PARA_INFO_LOG("using %s driver\n", info->short_name);
149 for (i = 0; i < conf->ao_option_given; i++) {
150 char *o = para_strdup(conf->ao_option_arg[i]), *value;
152 ret = -E_AO_BAD_OPTION;
153 value = strchr(o, ':');
160 PARA_INFO_LOG("appending option: key=%s, value=%s\n", o, value);
161 ret = ao_append_option(&aoo, o, value);
164 ret = -E_AO_APPEND_OPTION;
168 ret = aow_set_sample_format(sample_rate, channels, sample_format, &asf);
171 if (sample_format == SF_S8 || sample_format == SF_U8)
172 pawd->bytes_per_frame = channels;
174 pawd->bytes_per_frame = channels * 2;
175 ret = aow_open_device(id, &asf, aoo, &pawd->dev);
178 PARA_INFO_LOG("successfully opened %s\n", info->short_name);
179 wn->private_data = pawd;
186 __noreturn static void *aow_play(void *priv)
188 struct writer_node *wn = priv;
189 struct private_aow_data *pawd = wn->private_data;
190 struct btr_node *btrn = pawd->thread_btrn;
191 size_t frames, bytes;
197 * Lock mutex and wait for signal. pthread_cond_wait() will
198 * automatically and atomically unlock mutex while it waits.
200 pthread_mutex_lock(&pawd->mutex);
202 ret = btr_node_status(btrn, wn->min_iqs, BTR_NT_LEAF);
206 btr_merge(btrn, wn->min_iqs);
207 bytes = btr_next_buffer(btrn, &data);
208 frames = bytes / pawd->bytes_per_frame;
211 /* eof and less than a single frame available */
212 ret = -E_WRITE_COMMON_EOF;
215 //PARA_CRIT_LOG("waiting for data\n");
217 //pthread_mutex_unlock(&pawd->mutex);
218 pthread_cond_wait(&pawd->data_available, &pawd->mutex);
220 pthread_mutex_unlock(&pawd->mutex);
222 bytes = frames * pawd->bytes_per_frame;
224 if (ao_play(pawd->dev, data, bytes) == 0) /* failure */
226 btr_consume(btrn, bytes);
229 pthread_mutex_unlock(&pawd->mutex);
232 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
236 static int aow_create_thread(struct writer_node *wn)
238 struct private_aow_data *pawd = wn->private_data;
242 /* initialize with default attributes */
243 msg = "could not init mutex";
244 ret = pthread_mutex_init(&pawd->mutex, NULL);
248 msg = "could not initialize condition variable";
249 ret = pthread_cond_init(&pawd->data_available, NULL);
253 msg = "could not initialize thread attributes";
254 ret = pthread_attr_init(&pawd->attr);
258 /* schedule this thread under the real-time policy SCHED_FIFO */
259 msg = "could not set sched policy";
260 ret = pthread_attr_setschedpolicy(&pawd->attr, SCHED_FIFO);
264 msg = "could not set detach state to joinable";
265 ret = pthread_attr_setdetachstate(&pawd->attr, PTHREAD_CREATE_JOINABLE);
269 msg = "could not create thread";
270 ret = pthread_create(&pawd->thread, &pawd->attr, aow_play, wn);
275 PARA_ERROR_LOG("%s (%s)\n", msg, strerror(ret));
276 return -E_AO_PTHREAD;
279 static int aow_post_select(__a_unused struct sched *s,
282 struct writer_node *wn = container_of(t, struct writer_node, task);
283 struct private_aow_data *pawd = wn->private_data;
287 int32_t rate, ch, format;
288 struct btr_node_description bnd;
290 ret = btr_node_status(wn->btrn, wn->min_iqs, BTR_NT_LEAF);
295 get_btr_sample_rate(wn->btrn, &rate);
296 get_btr_channels(wn->btrn, &ch);
297 get_btr_sample_format(wn->btrn, &format);
298 ret = aow_init(wn, rate, ch, format);
301 pawd = wn->private_data;
303 /* set up thread btr node */
304 bnd.name = "ao_thread_btrn";
305 bnd.parent = wn->btrn;
309 pawd->thread_btrn = btr_new_node(&bnd);
310 wn->private_data = pawd;
312 ret = aow_create_thread(wn);
314 goto remove_thread_btrn;
317 pthread_mutex_lock(&pawd->mutex);
318 ret = btr_node_status(wn->btrn, wn->min_iqs, BTR_NT_LEAF);
320 btr_pushdown(wn->btrn);
321 pthread_cond_signal(&pawd->data_available);
323 pthread_mutex_unlock(&pawd->mutex);
326 pthread_mutex_lock(&pawd->mutex);
327 btr_remove_node(&wn->btrn);
328 PARA_INFO_LOG("waiting for thread to terminate\n");
329 pthread_cond_signal(&pawd->data_available);
330 pthread_mutex_unlock(&pawd->mutex);
331 pthread_join(pawd->thread, NULL);
333 btr_remove_node(&pawd->thread_btrn);
335 btr_remove_node(&wn->btrn);
340 __malloc static void *aow_parse_config_or_die(int argc, char **argv)
342 struct ao_write_args_info *conf = para_calloc(sizeof(*conf));
344 /* exits on errors */
345 ao_write_cmdline_parser(argc, argv, conf);
349 static void aow_free_config(void *conf)
351 ao_write_cmdline_parser_free(conf);
355 * The init function of the ao writer.
357 * \param w Pointer to the writer to initialize.
361 void ao_write_init(struct writer *w)
363 struct ao_write_args_info dummy;
364 int i, j, num_drivers, num_lines;
365 ao_info **driver_list;
366 char **dh; /* detailed help */
368 ao_write_cmdline_parser_init(&dummy);
369 w->close = aow_close;
370 w->pre_select = aow_pre_select;
371 w->post_select = aow_post_select;
372 w->parse_config_or_die = aow_parse_config_or_die;
373 w->free_config = aow_free_config;
374 w->help = (struct ggo_help) {
375 .short_help = ao_write_args_info_help,
377 /* create detailed help containing all supported drivers/options */
378 for (i = 0; ao_write_args_info_detailed_help[i]; i++)
381 dh = para_malloc((num_lines + 3) * sizeof(char *));
382 for (i = 0; i < num_lines; i++)
383 dh[i] = para_strdup(ao_write_args_info_detailed_help[i]);
384 dh[num_lines++] = para_strdup("libao drivers available on this host:");
385 dh[num_lines++] = para_strdup("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
388 driver_list = ao_driver_info_list(&num_drivers);
390 for (i = 0; i < num_drivers; i++) {
391 ao_info *info = driver_list[i];
392 char *keys = NULL, *tmp = NULL;
394 if (info->type == AO_TYPE_FILE)
396 for (j = 0; j < info->option_count; j++) {
397 tmp = make_message("%s%s%s", keys? keys : "",
403 dh = para_realloc(dh, (num_lines + 6) * sizeof(char *));
404 dh[num_lines++] = make_message("%s: %s", info->short_name, info->name);
405 dh[num_lines++] = make_message("priority: %d", info->priority);
406 dh[num_lines++] = make_message("keys: %s", keys? keys : "[none]");
407 dh[num_lines++] = make_message("comment: %s", info->comment?
408 info->comment : "[none]");
409 dh[num_lines++] = para_strdup(NULL);
412 dh[num_lines] = NULL;
413 w->help.detailed_help = (const char **)dh;
414 ao_write_cmdline_parser_free(&dummy);