]> git.tuebingen.mpg.de Git - paraslash.git/blob - ao_write.c
audiod: Fix error message on status errors.
[paraslash.git] / ao_write.c
1 /*
2  * Copyright (C) 2011-2013 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file ao_write.c Paraslash's libao output plugin. */
8
9 #include <pthread.h>
10 #include <ao/ao.h>
11 #include <regex.h>
12
13 #include "para.h"
14 #include "fd.h"
15 #include "string.h"
16 #include "list.h"
17 #include "sched.h"
18 #include "ggo.h"
19 #include "buffer_tree.h"
20 #include "write.h"
21 #include "write_common.h"
22 #include "ao_write.cmdline.h"
23 #include "error.h"
24
25 struct private_aow_data {
26         ao_device *dev;
27         int bytes_per_frame;
28
29         pthread_t thread;
30         pthread_attr_t attr;
31         pthread_mutex_t mutex;
32         pthread_cond_t data_available;
33         struct btr_node *thread_btrn;
34 };
35
36 static void aow_close(struct writer_node *wn)
37 {
38         struct private_aow_data *pawd = wn->private_data;
39
40         if (!pawd)
41                 return;
42         ao_close(pawd->dev);
43         free(pawd);
44         wn->private_data = NULL;
45         ao_shutdown();
46 }
47
48 static void aow_pre_select(struct sched *s, struct task *t)
49 {
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);
52
53         if (ret == 0)
54                 return;
55         sched_min_delay(s);
56 }
57
58 static int aow_set_sample_format(unsigned sample_rate, unsigned channels,
59                 int sample_format, ao_sample_format *result)
60 {
61         memset(result, 0, sizeof(*result));
62         switch (sample_format) {
63                 case SF_U8:
64                 case SF_U16_LE:
65                 case SF_U16_BE:
66                         return -E_AO_BAD_SAMPLE_FORMAT;
67                 case SF_S8:
68                         /* no need to set byte_format */
69                         result->bits = 8;
70                         break;
71                 case SF_S16_LE:
72                         result->bits = 16;
73                         result->byte_format = AO_FMT_LITTLE;
74                         break;
75                 case SF_S16_BE:
76                         result->bits = 16;
77                         result->byte_format = AO_FMT_BIG;
78                         break;
79                 default:
80                         PARA_EMERG_LOG("bug: invalid sample format\n");
81                         exit(EXIT_FAILURE);
82         }
83         result->channels = channels;
84         result->rate = sample_rate;
85         return 1;
86 }
87
88 static int aow_open_device(int id, ao_sample_format *asf, ao_option *options,
89                 ao_device **result)
90 {
91         const char *msg;
92         ao_device *dev = ao_open_live(id, asf, options);
93
94         if (dev) {
95                 *result = dev;
96                 return 1;
97         }
98         switch (errno) {
99                 case AO_ENODRIVER:
100                         msg = "No driver corresponds to driver_id";
101                         break;
102                 case AO_ENOTLIVE:
103                         msg = "This driver is not a live output device";
104                         break;
105                 case AO_EBADOPTION:
106                         msg = "A valid option key has an invalid value";
107                         break;
108                 case AO_EOPENDEVICE:
109                         msg = "Cannot open the device";
110                         break;
111                 case AO_EFAIL:
112                         msg = "General libao error";
113                         break;
114                 default:
115                         msg = "Unknown ao error";
116                         break;
117         }
118         PARA_ERROR_LOG("%s\n", msg);
119         return -E_AO_OPEN_LIVE;
120 }
121
122 static int aow_init(struct writer_node *wn, unsigned sample_rate,
123                 unsigned channels, int sample_format)
124 {
125         int id, ret, i;
126         ao_option *aoo = NULL;
127         ao_sample_format asf;
128         ao_info *info;
129         struct private_aow_data *pawd = para_malloc(sizeof(*pawd));
130         struct ao_write_args_info *conf = wn->conf;
131
132         ao_initialize();
133         if (conf->driver_given) {
134                 ret = -E_AO_BAD_DRIVER;
135                 id = ao_driver_id(conf->driver_arg);
136         } else {
137                 ret = -E_AO_DEFAULT_DRIVER;
138                 id = ao_default_driver_id();
139         }
140         if (id < 0)
141                 goto fail;
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;
146                 goto fail;
147         }
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;
151
152                 ret = -E_AO_BAD_OPTION;
153                 value = strchr(o, ':');
154                 if (!value) {
155                         free(o);
156                         goto fail;
157                 }
158                 *value = '\0';
159                 value++;
160                 PARA_INFO_LOG("appending option: key=%s, value=%s\n", o, value);
161                 ret = ao_append_option(&aoo, o, value);
162                 free(o);
163                 if (ret == 0) {
164                         ret = -E_AO_APPEND_OPTION;
165                         goto fail;
166                 }
167         }
168         ret = aow_set_sample_format(sample_rate, channels, sample_format, &asf);
169         if (ret < 0)
170                 goto fail;
171         if (sample_format == SF_S8 || sample_format == SF_U8)
172                 pawd->bytes_per_frame = channels;
173         else
174                 pawd->bytes_per_frame = channels * 2;
175         ret = aow_open_device(id, &asf, aoo, &pawd->dev);
176         if (ret < 0)
177                 goto fail;
178         PARA_INFO_LOG("successfully opened %s\n", info->short_name);
179         wn->private_data = pawd;
180         return 1;
181 fail:
182         free(pawd);
183         return ret;
184 }
185
186 __noreturn static void *aow_play(void *priv)
187 {
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;
192         char *data;
193         int ret;
194
195         for (;;) {
196                 /*
197                  * Lock mutex and wait for signal. pthread_cond_wait() will
198                  * automatically and atomically unlock mutex while it waits.
199                  */
200                 pthread_mutex_lock(&pawd->mutex);
201                 for (;;) {
202                         ret = btr_node_status(btrn, wn->min_iqs, BTR_NT_LEAF);
203                         if (ret < 0)
204                                 goto unlock;
205                         if (ret > 0) {
206                                 btr_merge(btrn, wn->min_iqs);
207                                 bytes = btr_next_buffer(btrn, &data);
208                                 frames = bytes / pawd->bytes_per_frame;
209                                 if (frames > 0)
210                                         break;
211                                 /* eof and less than a single frame available */
212                                 ret = -E_WRITE_COMMON_EOF;
213                                 goto unlock;
214                         }
215                         //PARA_CRIT_LOG("waiting for data\n");
216                         //usleep(1000);
217                         //pthread_mutex_unlock(&pawd->mutex);
218                         pthread_cond_wait(&pawd->data_available, &pawd->mutex);
219                 }
220                 pthread_mutex_unlock(&pawd->mutex);
221                 assert(frames > 0);
222                 bytes = frames * pawd->bytes_per_frame;
223                 ret = -E_AO_PLAY;
224                 if (ao_play(pawd->dev, data, bytes) == 0) /* failure */
225                         goto out;
226                 btr_consume(btrn, bytes);
227         }
228 unlock:
229         pthread_mutex_unlock(&pawd->mutex);
230 out:
231         assert(ret < 0);
232         PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
233         pthread_exit(NULL);
234 }
235
236 static int aow_create_thread(struct writer_node *wn)
237 {
238         struct private_aow_data *pawd = wn->private_data;
239         int ret;
240         const char *msg;
241
242         /* initialize with default attributes */
243         msg = "could not init mutex";
244         ret = pthread_mutex_init(&pawd->mutex, NULL);
245         if (ret < 0)
246                 goto fail;
247
248         msg = "could not initialize condition variable";
249         ret = pthread_cond_init(&pawd->data_available, NULL);
250         if (ret < 0)
251                 goto fail;
252
253         msg = "could not initialize thread attributes";
254         ret = pthread_attr_init(&pawd->attr);
255         if (ret < 0)
256                 goto fail;
257
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);
261         if (ret < 0)
262                 goto fail;
263
264         msg = "could not set detach state to joinable";
265         ret = pthread_attr_setdetachstate(&pawd->attr, PTHREAD_CREATE_JOINABLE);
266         if (ret < 0)
267                 goto fail;
268
269         msg = "could not create thread";
270         ret = pthread_create(&pawd->thread, &pawd->attr, aow_play, wn);
271         if (ret < 0)
272                 goto fail;
273         return 1;
274 fail:
275         PARA_ERROR_LOG("%s (%s)\n", msg, strerror(ret));
276         return -E_AO_PTHREAD;
277 }
278
279 static int aow_post_select(__a_unused struct sched *s,
280                 struct task *t)
281 {
282         struct writer_node *wn = container_of(t, struct writer_node, task);
283         struct private_aow_data *pawd = wn->private_data;
284         int ret;
285
286         if (!pawd) {
287                 int32_t rate, ch, format;
288                 struct btr_node_description bnd;
289
290                 ret = btr_node_status(wn->btrn, wn->min_iqs, BTR_NT_LEAF);
291                 if (ret < 0)
292                         goto remove_btrn;
293                 if (ret == 0)
294                         return 0;
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);
299                 if (ret < 0)
300                         goto remove_btrn;
301                 pawd = wn->private_data;
302
303                 /* set up thread btr node */
304                 bnd.name = "ao_thread_btrn";
305                 bnd.parent = wn->btrn;
306                 bnd.child = NULL;
307                 bnd.handler = NULL;
308                 bnd.context = pawd;
309                 pawd->thread_btrn = btr_new_node(&bnd);
310                 wn->private_data = pawd;
311
312                 ret = aow_create_thread(wn);
313                 if (ret < 0)
314                         goto remove_thread_btrn;
315                 return 0;
316         }
317         pthread_mutex_lock(&pawd->mutex);
318         ret = btr_node_status(wn->btrn, wn->min_iqs, BTR_NT_LEAF);
319         if (ret > 0) {
320                 btr_pushdown(wn->btrn);
321                 pthread_cond_signal(&pawd->data_available);
322         }
323         pthread_mutex_unlock(&pawd->mutex);
324         if (ret >= 0)
325                 goto out;
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);
332 remove_thread_btrn:
333         btr_remove_node(&pawd->thread_btrn);
334 remove_btrn:
335         btr_remove_node(&wn->btrn);
336 out:
337         return ret;
338 }
339
340 __malloc static void *aow_parse_config_or_die(int argc, char **argv)
341 {
342         struct ao_write_args_info *conf = para_calloc(sizeof(*conf));
343
344         /* exits on errors */
345         ao_write_cmdline_parser(argc, argv, conf);
346         return conf;
347 }
348
349 static void aow_free_config(void *conf)
350 {
351         ao_write_cmdline_parser_free(conf);
352 }
353
354 /**
355  * The init function of the ao writer.
356  *
357  * \param w Pointer to the writer to initialize.
358  *
359  * \sa struct writer.
360  */
361 void ao_write_init(struct writer *w)
362 {
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 */
367
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)DEFINE_GGO_HELP(ao_write);
375         /* create detailed help containing all supported drivers/options */
376         for (i = 0; ao_write_args_info_detailed_help[i]; i++)
377                 ; /* nothing */
378         num_lines = i;
379         dh = para_malloc((num_lines + 3) * sizeof(char *));
380         for (i = 0; i < num_lines; i++)
381                 dh[i] = para_strdup(ao_write_args_info_detailed_help[i]);
382         dh[num_lines++] = para_strdup("libao drivers available on this host:");
383         dh[num_lines++] = para_strdup("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
384
385         ao_initialize();
386         driver_list = ao_driver_info_list(&num_drivers);
387
388         for (i = 0; i < num_drivers; i++) {
389                 ao_info *info = driver_list[i];
390                 char *keys = NULL, *tmp = NULL;
391
392                 if (info->type == AO_TYPE_FILE)
393                         continue;
394                 for (j = 0; j < info->option_count; j++) {
395                         tmp = make_message("%s%s%s", keys? keys : "",
396                                 keys? ", " : "",
397                                 info->options[j]);
398                         free(keys);
399                         keys = tmp;
400                 }
401                 dh = para_realloc(dh, (num_lines + 6) * sizeof(char *));
402                 dh[num_lines++] = make_message("%s: %s", info->short_name, info->name);
403                 dh[num_lines++] = make_message("priority: %d", info->priority);
404                 dh[num_lines++] = make_message("keys: %s", keys? keys : "[none]");
405                 dh[num_lines++] = make_message("comment: %s", info->comment?
406                         info->comment : "[none]");
407                 dh[num_lines++] = para_strdup(NULL);
408                 free(keys);
409         }
410         dh[num_lines] = NULL;
411         w->help.detailed_help = (const char **)dh;
412         ao_write_cmdline_parser_free(&dummy);
413         ao_shutdown();
414 }
415