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);
65 static int aow_set_sample_format(unsigned sample_rate, unsigned channels,
66 int sample_format, ao_sample_format *result)
68 memset(result, 0, sizeof(*result));
69 switch (sample_format) {
73 return -E_AO_BAD_SAMPLE_FORMAT;
75 /* no need to set byte_format */
80 result->byte_format = AO_FMT_LITTLE;
84 result->byte_format = AO_FMT_BIG;
87 PARA_EMERG_LOG("bug: invalid sample format\n");
90 result->channels = channels;
91 result->rate = sample_rate;
95 static int aow_open_device(int id, ao_sample_format *asf, ao_option *options,
99 ao_device *dev = ao_open_live(id, asf, options);
107 msg = "No driver corresponds to driver_id";
110 msg = "This driver is not a live output device";
113 msg = "A valid option key has an invalid value";
116 msg = "Cannot open the device";
119 msg = "General libao error";
122 msg = "Unknown ao error";
125 PARA_ERROR_LOG("%s\n", msg);
126 return -E_AO_OPEN_LIVE;
129 static int aow_init(struct writer_node *wn, unsigned sample_rate,
130 unsigned channels, int sample_format)
133 ao_option *aoo = NULL;
134 ao_sample_format asf;
136 struct private_aow_data *pawd = para_malloc(sizeof(*pawd));
137 struct ao_write_args_info *conf = wn->conf;
140 if (conf->driver_given) {
141 ret = -E_AO_BAD_DRIVER;
142 id = ao_driver_id(conf->driver_arg);
144 ret = -E_AO_DEFAULT_DRIVER;
145 id = ao_default_driver_id();
149 info = ao_driver_info(id);
150 assert(info && info->short_name);
151 if (info->type == AO_TYPE_FILE) {
152 ret = -E_AO_FILE_NOT_SUPP;
155 PARA_INFO_LOG("using %s driver\n", info->short_name);
156 for (i = 0; i < conf->ao_option_given; i++) {
157 char *o = para_strdup(conf->ao_option_arg[i]), *value;
159 ret = -E_AO_BAD_OPTION;
160 value = strchr(o, ':');
167 PARA_INFO_LOG("appending option: key=%s, value=%s\n", o, value);
168 ret = ao_append_option(&aoo, o, value);
171 ret = -E_AO_APPEND_OPTION;
175 ret = aow_set_sample_format(sample_rate, channels, sample_format, &asf);
178 if (sample_format == SF_S8 || sample_format == SF_U8)
179 pawd->bytes_per_frame = channels;
181 pawd->bytes_per_frame = channels * 2;
182 ret = aow_open_device(id, &asf, aoo, &pawd->dev);
185 PARA_INFO_LOG("successfully opened %s\n", info->short_name);
186 wn->private_data = pawd;
193 __noreturn static void *aow_play(void *priv)
195 struct writer_node *wn = priv;
196 struct private_aow_data *pawd = wn->private_data;
197 struct btr_node *btrn = pawd->thread_btrn;
198 size_t frames, bytes;
204 * Lock mutex and wait for signal. pthread_cond_wait() will
205 * automatically and atomically unlock mutex while it waits.
207 pthread_mutex_lock(&pawd->mutex);
209 ret = btr_node_status(btrn, wn->min_iqs, BTR_NT_LEAF);
213 btr_merge(btrn, wn->min_iqs);
214 bytes = btr_next_buffer(btrn, &data);
215 frames = bytes / pawd->bytes_per_frame;
218 /* eof and less than a single frame available */
219 ret = -E_WRITE_COMMON_EOF;
222 //PARA_CRIT_LOG("waiting for data\n");
224 //pthread_mutex_unlock(&pawd->mutex);
225 pthread_cond_wait(&pawd->data_available, &pawd->mutex);
227 pthread_mutex_unlock(&pawd->mutex);
229 bytes = frames * pawd->bytes_per_frame;
231 if (ao_play(pawd->dev, data, bytes) == 0) /* failure */
233 pthread_mutex_lock(&pawd->mutex);
234 btr_consume(btrn, bytes);
235 pthread_mutex_unlock(&pawd->mutex);
238 pthread_mutex_unlock(&pawd->mutex);
241 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
245 static int aow_create_thread(struct writer_node *wn)
247 struct private_aow_data *pawd = wn->private_data;
251 /* initialize with default attributes */
252 msg = "could not init mutex";
253 ret = pthread_mutex_init(&pawd->mutex, NULL);
257 msg = "could not initialize condition variable";
258 ret = pthread_cond_init(&pawd->data_available, NULL);
262 msg = "could not initialize thread attributes";
263 ret = pthread_attr_init(&pawd->attr);
267 /* schedule this thread under the real-time policy SCHED_FIFO */
268 msg = "could not set sched policy";
269 ret = pthread_attr_setschedpolicy(&pawd->attr, SCHED_FIFO);
273 msg = "could not set detach state to joinable";
274 ret = pthread_attr_setdetachstate(&pawd->attr, PTHREAD_CREATE_JOINABLE);
278 msg = "could not create thread";
279 ret = pthread_create(&pawd->thread, &pawd->attr, aow_play, wn);
284 PARA_ERROR_LOG("%s (%s)\n", msg, strerror(ret));
285 return -E_AO_PTHREAD;
288 static int aow_post_select(__a_unused struct sched *s,
291 struct writer_node *wn = container_of(t, struct writer_node, task);
292 struct private_aow_data *pawd = wn->private_data;
296 int32_t rate, ch, format;
297 struct btr_node_description bnd;
299 ret = btr_node_status(wn->btrn, wn->min_iqs, BTR_NT_LEAF);
304 get_btr_sample_rate(wn->btrn, &rate);
305 get_btr_channels(wn->btrn, &ch);
306 get_btr_sample_format(wn->btrn, &format);
307 ret = aow_init(wn, rate, ch, format);
310 pawd = wn->private_data;
312 /* set up thread btr node */
313 bnd.name = "ao_thread_btrn";
314 bnd.parent = wn->btrn;
318 pawd->thread_btrn = btr_new_node(&bnd);
319 wn->private_data = pawd;
321 ret = aow_create_thread(wn);
323 goto remove_thread_btrn;
326 pthread_mutex_lock(&pawd->mutex);
327 ret = btr_node_status(wn->btrn, wn->min_iqs, BTR_NT_LEAF);
329 btr_pushdown(wn->btrn);
330 pthread_cond_signal(&pawd->data_available);
332 pthread_mutex_unlock(&pawd->mutex);
335 pthread_mutex_lock(&pawd->mutex);
336 btr_remove_node(&wn->btrn);
337 PARA_INFO_LOG("waiting for thread to terminate\n");
338 pthread_cond_signal(&pawd->data_available);
339 pthread_mutex_unlock(&pawd->mutex);
340 pthread_join(pawd->thread, NULL);
342 btr_remove_node(&pawd->thread_btrn);
344 btr_remove_node(&wn->btrn);
349 __malloc static void *aow_parse_config_or_die(int argc, char **argv)
351 struct ao_write_args_info *conf = para_calloc(sizeof(*conf));
353 /* exits on errors */
354 ao_write_cmdline_parser(argc, argv, conf);
358 static void aow_free_config(void *conf)
360 ao_write_cmdline_parser_free(conf);
364 * The init function of the ao writer.
366 * \param w Pointer to the writer to initialize.
370 void ao_write_init(struct writer *w)
372 struct ao_write_args_info dummy;
373 int i, j, num_drivers, num_lines;
374 ao_info **driver_list;
375 char **dh; /* detailed help */
377 ao_write_cmdline_parser_init(&dummy);
378 w->close = aow_close;
379 w->pre_select = aow_pre_select;
380 w->post_select = aow_post_select;
381 w->parse_config_or_die = aow_parse_config_or_die;
382 w->free_config = aow_free_config;
383 w->help = (struct ggo_help)DEFINE_GGO_HELP(ao_write);
384 /* create detailed help containing all supported drivers/options */
385 for (i = 0; ao_write_args_info_detailed_help[i]; i++)
388 dh = para_malloc((num_lines + 3) * sizeof(char *));
389 for (i = 0; i < num_lines; i++)
390 dh[i] = para_strdup(ao_write_args_info_detailed_help[i]);
391 dh[num_lines++] = para_strdup("libao drivers available on this host:");
392 dh[num_lines++] = para_strdup("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
395 driver_list = ao_driver_info_list(&num_drivers);
397 for (i = 0; i < num_drivers; i++) {
398 ao_info *info = driver_list[i];
399 char *keys = NULL, *tmp = NULL;
401 if (info->type == AO_TYPE_FILE)
403 for (j = 0; j < info->option_count; j++) {
404 tmp = make_message("%s%s%s", keys? keys : "",
410 dh = para_realloc(dh, (num_lines + 6) * sizeof(char *));
411 dh[num_lines++] = make_message("%s: %s", info->short_name, info->name);
412 dh[num_lines++] = make_message("priority: %d", info->priority);
413 dh[num_lines++] = make_message("keys: %s", keys? keys : "[none]");
414 dh[num_lines++] = make_message("comment: %s", info->comment?
415 info->comment : "[none]");
416 dh[num_lines++] = para_strdup(NULL);
419 dh[num_lines] = NULL;
420 w->help.detailed_help = (const char **)dh;
421 ao_write_cmdline_parser_free(&dummy);