1 /* Copyright (C) 2011 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
3 /** \file ao_write.c Paraslash's libao output plugin. */
10 #include "write_cmd.lsg.h"
16 #include "buffer_tree.h"
20 struct private_aow_data {
26 /* The mutex and the condition variable serialize access to ->btrn */
27 pthread_mutex_t mutex;
28 pthread_cond_t data_available;
29 struct btr_node *thread_btrn;
32 static void aow_close(struct writer_node *wn)
34 struct private_aow_data *pawd = wn->private_data;
38 assert(!pawd->thread_btrn);
41 wn->private_data = NULL;
45 static void aow_pre_monitor(struct sched *s, void *context)
47 struct writer_node *wn = context;
48 struct private_aow_data *pawd = wn->private_data;
51 if (!pawd) { /* not yet started */
53 ret = btr_node_status(wn->btrn, wn->min_iqs, BTR_NT_LEAF);
56 return; /* no data available */
58 if (!wn->btrn) { /* EOF */
59 if (!pawd->thread_btrn) /* ready to exit */
61 /* wait for the play thread to terminate */
64 pthread_mutex_lock(&pawd->mutex);
65 ret = btr_node_status(wn->btrn, wn->min_iqs, BTR_NT_LEAF);
66 pthread_mutex_unlock(&pawd->mutex);
70 * Even though the node status is zero, we might have data available,
71 * but the output buffer is full. If we don't set a timeout here, we
72 * are woken up only if new data arrives, which might be too late and
73 * result in a buffer underrun in the playing thread. To avoid this we
74 * never sleep longer than the (default) buffer time.
77 return sched_request_timeout_ms(20, s);
82 static int aow_set_sample_format(unsigned sample_rate, unsigned channels,
83 int sample_format, ao_sample_format *result)
85 memset(result, 0, sizeof(*result));
86 switch (sample_format) {
92 return -E_BAD_SAMPLE_FORMAT;
94 /* no need to set byte_format */
99 result->byte_format = AO_FMT_LITTLE;
103 result->byte_format = AO_FMT_BIG;
106 PARA_EMERG_LOG("bug: invalid sample format\n");
109 result->channels = channels;
110 result->rate = sample_rate;
114 static int aow_open_device(int id, ao_sample_format *asf, ao_option *options,
118 ao_device *dev = ao_open_live(id, asf, options);
126 msg = "No driver corresponds to driver_id";
129 msg = "This driver is not a live output device";
132 msg = "A valid option key has an invalid value";
135 msg = "Cannot open the device";
138 msg = "General libao error";
141 msg = "Unknown ao error";
144 PARA_ERROR_LOG("%s\n", msg);
145 return -E_AO_OPEN_LIVE;
148 static void aow_show_drivers(void)
150 int i, j, num_drivers;
151 ao_info **driver_list;
153 PARA_DEBUG_LOG("libao drivers available on this host:\n");
154 PARA_DEBUG_LOG("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
156 driver_list = ao_driver_info_list(&num_drivers);
158 for (i = 0; i < num_drivers; i++) {
159 ao_info *info = driver_list[i];
160 char *keys = NULL, *tmp = NULL;
162 if (info->type == AO_TYPE_FILE)
164 PARA_DEBUG_LOG("name: %s: %s\n", info->short_name, info->name);
165 PARA_DEBUG_LOG("priority: %d\n", info->priority);
166 for (j = 0; j < info->option_count; j++) {
167 tmp = make_message("%s%s%s", keys? keys : "",
173 PARA_DEBUG_LOG("keys: %s\n", keys? keys : "[none]");
175 PARA_DEBUG_LOG("comment: %s\n", info->comment?
176 info->comment : "[none]");
180 static int aow_init(struct writer_node *wn, unsigned sample_rate,
181 unsigned channels, int sample_format)
184 ao_option *aoo = NULL;
185 ao_sample_format asf;
187 const struct lls_opt_result *r;
189 struct private_aow_data *pawd = alloc(sizeof(*pawd));
193 if (WRITE_CMD_OPT_GIVEN(AO, DRIVER, wn->lpr)) {
194 ret = -E_AO_BAD_DRIVER;
195 id = ao_driver_id(WRITE_CMD_OPT_STRING_VAL(AO, DRIVER, wn->lpr));
197 ret = -E_AO_DEFAULT_DRIVER;
198 id = ao_default_driver_id();
202 info = ao_driver_info(id);
203 assert(info && info->short_name);
204 if (info->type == AO_TYPE_FILE) {
205 ret = -E_AO_FILE_NOT_SUPP;
208 PARA_INFO_LOG("using %s driver\n", info->short_name);
209 r = WRITE_CMD_OPT_RESULT(AO, AO_OPTION, wn->lpr);
210 n = lls_opt_given(r);
211 for (i = 0; i < n; i++) {
212 char *o = para_strdup(lls_string_val(i, r));
215 ret = -E_AO_BAD_OPTION;
216 value = strchr(o, ':');
223 PARA_INFO_LOG("appending option: key=%s, value=%s\n", o, value);
224 ret = ao_append_option(&aoo, o, value);
227 ret = -E_AO_APPEND_OPTION;
231 ret = aow_set_sample_format(sample_rate, channels, sample_format, &asf);
234 if (sample_format == SF_S8 || sample_format == SF_U8)
235 pawd->bytes_per_frame = channels;
237 pawd->bytes_per_frame = channels * 2;
238 ret = aow_open_device(id, &asf, aoo, &pawd->dev);
241 PARA_INFO_LOG("successfully opened %s\n", info->short_name);
242 wn->private_data = pawd;
249 static void *aow_play(void *priv)
251 struct writer_node *wn = priv;
252 struct private_aow_data *pawd = wn->private_data;
253 struct btr_node *btrn = pawd->thread_btrn;
254 size_t frames, bytes;
258 pthread_mutex_lock(&pawd->mutex);
261 ret = btr_node_status(btrn, wn->min_iqs, BTR_NT_LEAF);
265 btr_merge(btrn, wn->min_iqs);
266 bytes = btr_next_buffer(btrn, &data);
267 frames = bytes / pawd->bytes_per_frame;
270 /* eof and less than a single frame available */
275 * No data available, go to sleep and wait for the main
276 * thread to wake us up. pthread_cond_wait() unlocks
277 * the mutex while it waits and locks it again upon
280 ret = pthread_cond_wait(&pawd->data_available,
282 /* pthread_cond_wait() can never fail here */
286 bytes = frames * pawd->bytes_per_frame;
287 pthread_mutex_unlock(&pawd->mutex);
288 ret = ao_play(pawd->dev, data, bytes);
289 pthread_mutex_lock(&pawd->mutex);
290 if (ret == 0) { /* failure */
294 btr_consume(btrn, bytes);
297 btr_remove_node(&pawd->thread_btrn);
299 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
300 pthread_mutex_unlock(&pawd->mutex);
304 static int aow_create_thread(struct writer_node *wn)
306 struct private_aow_data *pawd = wn->private_data;
310 /* initialize with default attributes */
311 msg = "could not init mutex";
312 ret = pthread_mutex_init(&pawd->mutex, NULL);
316 msg = "could not initialize condition variable";
317 ret = pthread_cond_init(&pawd->data_available, NULL);
321 msg = "could not initialize thread attributes";
322 ret = pthread_attr_init(&pawd->attr);
326 /* schedule this thread under the real-time policy SCHED_FIFO */
327 msg = "could not set sched policy";
328 ret = pthread_attr_setschedpolicy(&pawd->attr, SCHED_FIFO);
332 msg = "could not set detach state to joinable";
333 ret = pthread_attr_setdetachstate(&pawd->attr, PTHREAD_CREATE_JOINABLE);
337 msg = "could not create thread";
338 ret = pthread_create(&pawd->thread, &pawd->attr, aow_play, wn);
343 PARA_ERROR_LOG("%s (%s)\n", msg, strerror(ret));
344 return -E_AO_PTHREAD;
347 static int aow_post_monitor(__a_unused struct sched *s, void *context)
349 struct writer_node *wn = context;
350 struct private_aow_data *pawd = wn->private_data;
354 int32_t rate, ch, format;
355 struct btr_node_description bnd;
357 ret = btr_node_status(wn->btrn, wn->min_iqs, BTR_NT_LEAF);
362 ret = get_btr_sample_rate(wn->btrn, &rate);
365 ret = get_btr_channels(wn->btrn, &ch);
368 ret = get_btr_sample_format(wn->btrn, &format);
371 ret = aow_init(wn, rate, ch, format);
374 pawd = wn->private_data;
376 /* set up thread btr node */
377 bnd.name = "ao_thread_btrn";
378 bnd.parent = wn->btrn;
382 pawd->thread_btrn = btr_new_node(&bnd);
383 wn->private_data = pawd;
385 ret = aow_create_thread(wn);
387 goto remove_thread_btrn;
391 if (!pawd->thread_btrn) {
392 pthread_join(pawd->thread, NULL);
395 PARA_INFO_LOG("waiting for play thread to terminate\n");
398 pthread_mutex_lock(&pawd->mutex);
399 ret = btr_node_status(wn->btrn, wn->min_iqs, BTR_NT_LEAF);
401 btr_pushdown(wn->btrn);
402 if (pthread_cond_signal(&pawd->data_available) != 0) {
404 PARA_ERROR_LOG("pthread_cond_signal() failed\n");
405 goto remove_thread_btrn;
409 pthread_mutex_unlock(&pawd->mutex);
412 btr_remove_node(&wn->btrn);
413 pthread_cond_signal(&pawd->data_available);
414 pthread_mutex_unlock(&pawd->mutex);
417 btr_remove_node(&pawd->thread_btrn);
419 btr_remove_node(&wn->btrn);
424 struct writer lsg_write_cmd_com_ao_user_data = {
426 .pre_monitor = aow_pre_monitor,
427 .post_monitor = aow_post_monitor,