wma: Trivial whitespace fix.
[paraslash.git] / ao_write.c
1 /*
2  * Copyright (C) 2011-2012 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 void 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 btr_node *btrn = wn->btrn;
284         struct private_aow_data *pawd = wn->private_data;
285         int ret;
286
287         if (!pawd) {
288                 int32_t rate, ch, format;
289                 struct btr_node_description bnd;
290
291                 ret = btr_node_status(btrn, wn->min_iqs, BTR_NT_LEAF);
292                 if (ret < 0)
293                         goto remove_btrn;
294                 if (ret == 0)
295                         return;
296                 get_btr_sample_rate(btrn, &rate);
297                 get_btr_channels(btrn, &ch);
298                 get_btr_sample_format(btrn, &format);
299                 ret = aow_init(wn, rate, ch, format);
300                 if (ret < 0)
301                         goto remove_btrn;
302                 pawd = wn->private_data;
303
304                 /* set up thread btr node */
305                 bnd.name = "ao_thread_btrn";
306                 bnd.parent = btrn;
307                 bnd.child = NULL;
308                 bnd.handler = NULL;
309                 bnd.context = pawd;
310                 pawd->thread_btrn = btr_new_node(&bnd);
311                 wn->private_data = pawd;
312
313                 ret = aow_create_thread(wn);
314                 if (ret < 0)
315                         goto remove_thread_btrn;
316                 return;
317         }
318         pthread_mutex_lock(&pawd->mutex);
319         ret = btr_node_status(btrn, wn->min_iqs, BTR_NT_LEAF);
320         if (ret > 0) {
321                 btr_pushdown(btrn);
322                 pthread_cond_signal(&pawd->data_available);
323         }
324         pthread_mutex_unlock(&pawd->mutex);
325         if (ret >= 0)
326                 goto out;
327         pthread_mutex_lock(&pawd->mutex);
328         btr_remove_node(btrn);
329         btrn = NULL;
330         PARA_INFO_LOG("waiting for thread to terminate\n");
331         pthread_cond_signal(&pawd->data_available);
332         pthread_mutex_unlock(&pawd->mutex);
333         pthread_join(pawd->thread, NULL);
334 remove_thread_btrn:
335         btr_remove_node(pawd->thread_btrn);
336         btr_free_node(pawd->thread_btrn);
337 remove_btrn:
338         if (btrn)
339                 btr_remove_node(btrn);
340 out:
341         t->error = ret;
342 }
343
344 __malloc static void *aow_parse_config_or_die(const char *options)
345 {
346         struct ao_write_args_info *conf = para_calloc(sizeof(*conf));
347
348         /* exits on errors */
349         ao_cmdline_parser_string(options, conf, "ao_write");
350         return conf;
351 }
352
353 static void aow_free_config(void *conf)
354 {
355         ao_cmdline_parser_free(conf);
356 }
357
358 /**
359  * The init function of the ao writer.
360  *
361  * \param w Pointer to the writer to initialize.
362  *
363  * \sa struct writer.
364  */
365 void ao_write_init(struct writer *w)
366 {
367         struct ao_write_args_info dummy;
368         int i, j, num_drivers, num_lines;
369         ao_info **driver_list;
370         char **dh; /* detailed help */
371
372         ao_cmdline_parser_init(&dummy);
373         w->close = aow_close;
374         w->pre_select = aow_pre_select;
375         w->post_select = aow_post_select;
376         w->parse_config_or_die = aow_parse_config_or_die;
377         w->free_config = aow_free_config;
378         w->shutdown = NULL;
379         w->help = (struct ggo_help) {
380                 .short_help = ao_write_args_info_help,
381         };
382         /* create detailed help containing all supported drivers/options */
383         for (i = 0; ao_write_args_info_detailed_help[i]; i++)
384                 ; /* nothing */
385         num_lines = i;
386         dh = para_malloc((num_lines + 3) * sizeof(char *));
387         for (i = 0; i < num_lines; i++)
388                 dh[i] = para_strdup(ao_write_args_info_detailed_help[i]);
389         dh[num_lines++] = para_strdup("libao drivers available on this host:");
390         dh[num_lines++] = para_strdup("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
391
392         ao_initialize();
393         driver_list = ao_driver_info_list(&num_drivers);
394
395         for (i = 0; i < num_drivers; i++) {
396                 ao_info *info = driver_list[i];
397                 char *keys = NULL, *tmp = NULL;
398
399                 if (info->type == AO_TYPE_FILE)
400                         continue;
401                 for (j = 0; j < info->option_count; j++) {
402                         tmp = make_message("%s%s%s", keys? keys : "",
403                                 keys? ", " : "",
404                                 info->options[j]);
405                         free(keys);
406                         keys = tmp;
407                 }
408                 dh = para_realloc(dh, (num_lines + 6) * sizeof(char *));
409                 dh[num_lines++] = make_message("%s: %s", info->short_name, info->name);
410                 dh[num_lines++] = make_message("priority: %d", info->priority);
411                 dh[num_lines++] = make_message("keys: %s", keys? keys : "[none]");
412                 dh[num_lines++] = make_message("comment: %s", info->comment?
413                         info->comment : "[none]");
414                 dh[num_lines++] = para_strdup(NULL);
415                 free(keys);
416         }
417         dh[num_lines] = NULL;
418         w->help.detailed_help = (const char **)dh;
419         ao_cmdline_parser_free(&dummy);
420         ao_shutdown();
421 }
422