]> git.tuebingen.mpg.de Git - paraslash.git/blob - ao_write.c
ao_write: Join threads before returning an error from aow_post_select().
[paraslash.git] / ao_write.c
1 /*
2  * Copyright (C) 2011-2014 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         /* The mutex and the condition variable serialize access to ->btrn */
32         pthread_mutex_t mutex;
33         pthread_cond_t data_available;
34         struct btr_node *thread_btrn;
35 };
36
37 static void aow_close(struct writer_node *wn)
38 {
39         struct private_aow_data *pawd = wn->private_data;
40
41         if (!pawd)
42                 return;
43         assert(!pawd->thread_btrn);
44         ao_close(pawd->dev);
45         free(pawd);
46         wn->private_data = NULL;
47         ao_shutdown();
48 }
49
50 static void aow_pre_select(struct sched *s, struct task *t)
51 {
52         struct writer_node *wn = container_of(t, struct writer_node, task);
53         struct private_aow_data *pawd = wn->private_data;
54         int ret;
55
56         if (!pawd) { /* not yet started */
57                 assert(wn->btrn);
58                 ret = btr_node_status(wn->btrn, wn->min_iqs, BTR_NT_LEAF);
59                 if (ret != 0)
60                         goto min_delay;
61                 return; /* no data available */
62         }
63         if (!wn->btrn) { /* EOF */
64                 if (!pawd->thread_btrn) /* ready to exit */
65                         goto min_delay;
66                 /* wait for the play thread to terminate */
67                 goto timeout;
68         }
69         pthread_mutex_lock(&pawd->mutex);
70         ret = btr_node_status(wn->btrn, wn->min_iqs, BTR_NT_LEAF);
71         pthread_mutex_unlock(&pawd->mutex);
72         if (ret != 0)
73                 goto min_delay;
74         /*
75          * Even though the node status is zero, we might have data available,
76          * but the output buffer is full. If we don't set a timeout here, we
77          * are woken up only if new data arrives, which might be too late and
78          * result in a buffer underrun in the playing thread. To avoid this we
79          * never sleep longer than the (default) buffer time.
80          */
81 timeout:
82         return sched_request_timeout_ms(20, s);
83 min_delay:
84         sched_min_delay(s);
85 }
86
87 static int aow_set_sample_format(unsigned sample_rate, unsigned channels,
88                 int sample_format, ao_sample_format *result)
89 {
90         memset(result, 0, sizeof(*result));
91         switch (sample_format) {
92                 case SF_U8:
93                 case SF_U16_LE:
94                 case SF_U16_BE:
95                         return -E_AO_BAD_SAMPLE_FORMAT;
96                 case SF_S8:
97                         /* no need to set byte_format */
98                         result->bits = 8;
99                         break;
100                 case SF_S16_LE:
101                         result->bits = 16;
102                         result->byte_format = AO_FMT_LITTLE;
103                         break;
104                 case SF_S16_BE:
105                         result->bits = 16;
106                         result->byte_format = AO_FMT_BIG;
107                         break;
108                 default:
109                         PARA_EMERG_LOG("bug: invalid sample format\n");
110                         exit(EXIT_FAILURE);
111         }
112         result->channels = channels;
113         result->rate = sample_rate;
114         return 1;
115 }
116
117 static int aow_open_device(int id, ao_sample_format *asf, ao_option *options,
118                 ao_device **result)
119 {
120         const char *msg;
121         ao_device *dev = ao_open_live(id, asf, options);
122
123         if (dev) {
124                 *result = dev;
125                 return 1;
126         }
127         switch (errno) {
128                 case AO_ENODRIVER:
129                         msg = "No driver corresponds to driver_id";
130                         break;
131                 case AO_ENOTLIVE:
132                         msg = "This driver is not a live output device";
133                         break;
134                 case AO_EBADOPTION:
135                         msg = "A valid option key has an invalid value";
136                         break;
137                 case AO_EOPENDEVICE:
138                         msg = "Cannot open the device";
139                         break;
140                 case AO_EFAIL:
141                         msg = "General libao error";
142                         break;
143                 default:
144                         msg = "Unknown ao error";
145                         break;
146         }
147         PARA_ERROR_LOG("%s\n", msg);
148         return -E_AO_OPEN_LIVE;
149 }
150
151 static int aow_init(struct writer_node *wn, unsigned sample_rate,
152                 unsigned channels, int sample_format)
153 {
154         int id, ret, i;
155         ao_option *aoo = NULL;
156         ao_sample_format asf;
157         ao_info *info;
158         struct private_aow_data *pawd = para_malloc(sizeof(*pawd));
159         struct ao_write_args_info *conf = wn->conf;
160
161         ao_initialize();
162         if (conf->driver_given) {
163                 ret = -E_AO_BAD_DRIVER;
164                 id = ao_driver_id(conf->driver_arg);
165         } else {
166                 ret = -E_AO_DEFAULT_DRIVER;
167                 id = ao_default_driver_id();
168         }
169         if (id < 0)
170                 goto fail;
171         info = ao_driver_info(id);
172         assert(info && info->short_name);
173         if (info->type == AO_TYPE_FILE) {
174                 ret = -E_AO_FILE_NOT_SUPP;
175                 goto fail;
176         }
177         PARA_INFO_LOG("using %s driver\n", info->short_name);
178         for (i = 0; i < conf->ao_option_given; i++) {
179                 char *o = para_strdup(conf->ao_option_arg[i]), *value;
180
181                 ret = -E_AO_BAD_OPTION;
182                 value = strchr(o, ':');
183                 if (!value) {
184                         free(o);
185                         goto fail;
186                 }
187                 *value = '\0';
188                 value++;
189                 PARA_INFO_LOG("appending option: key=%s, value=%s\n", o, value);
190                 ret = ao_append_option(&aoo, o, value);
191                 free(o);
192                 if (ret == 0) {
193                         ret = -E_AO_APPEND_OPTION;
194                         goto fail;
195                 }
196         }
197         ret = aow_set_sample_format(sample_rate, channels, sample_format, &asf);
198         if (ret < 0)
199                 goto fail;
200         if (sample_format == SF_S8 || sample_format == SF_U8)
201                 pawd->bytes_per_frame = channels;
202         else
203                 pawd->bytes_per_frame = channels * 2;
204         ret = aow_open_device(id, &asf, aoo, &pawd->dev);
205         if (ret < 0)
206                 goto fail;
207         PARA_INFO_LOG("successfully opened %s\n", info->short_name);
208         wn->private_data = pawd;
209         return 1;
210 fail:
211         free(pawd);
212         return ret;
213 }
214
215 __noreturn static void *aow_play(void *priv)
216 {
217         struct writer_node *wn = priv;
218         struct private_aow_data *pawd = wn->private_data;
219         struct btr_node *btrn = pawd->thread_btrn;
220         size_t frames, bytes;
221         char *data;
222         int ret;
223
224         pthread_mutex_lock(&pawd->mutex);
225         for (;;) {
226                 for (;;) {
227                         ret = btr_node_status(btrn, wn->min_iqs, BTR_NT_LEAF);
228                         if (ret < 0)
229                                 goto fail;
230                         if (ret > 0) {
231                                 btr_merge(btrn, wn->min_iqs);
232                                 bytes = btr_next_buffer(btrn, &data);
233                                 frames = bytes / pawd->bytes_per_frame;
234                                 if (frames > 0)
235                                         break;
236                                 /* eof and less than a single frame available */
237                                 ret = -E_WRITE_COMMON_EOF;
238                                 goto fail;
239                         }
240                         /*
241                          * No data available, go to sleep and wait for the main
242                          * thread to wake us up. pthread_cond_wait() unlocks
243                          * the mutex while it waits and locks it again upon
244                          * return.
245                          */
246                         ret = pthread_cond_wait(&pawd->data_available,
247                                 &pawd->mutex);
248                         /* pthread_cond_wait() can never fail here */
249                         assert(ret == 0);
250                 }
251                 assert(frames > 0);
252                 bytes = frames * pawd->bytes_per_frame;
253                 pthread_mutex_unlock(&pawd->mutex);
254                 ret = ao_play(pawd->dev, data, bytes);
255                 pthread_mutex_lock(&pawd->mutex);
256                 if (ret == 0) { /* failure */
257                         ret = -E_AO_PLAY;
258                         goto fail;
259                 }
260                 btr_consume(btrn, bytes);
261         }
262 fail:
263         btr_remove_node(&pawd->thread_btrn);
264         assert(ret < 0);
265         PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
266         pthread_mutex_unlock(&pawd->mutex);
267         pthread_exit(NULL);
268 }
269
270 static int aow_create_thread(struct writer_node *wn)
271 {
272         struct private_aow_data *pawd = wn->private_data;
273         int ret;
274         const char *msg;
275
276         /* initialize with default attributes */
277         msg = "could not init mutex";
278         ret = pthread_mutex_init(&pawd->mutex, NULL);
279         if (ret < 0)
280                 goto fail;
281
282         msg = "could not initialize condition variable";
283         ret = pthread_cond_init(&pawd->data_available, NULL);
284         if (ret < 0)
285                 goto fail;
286
287         msg = "could not initialize thread attributes";
288         ret = pthread_attr_init(&pawd->attr);
289         if (ret < 0)
290                 goto fail;
291
292         /* schedule this thread under the real-time policy SCHED_FIFO */
293         msg = "could not set sched policy";
294         ret = pthread_attr_setschedpolicy(&pawd->attr, SCHED_FIFO);
295         if (ret < 0)
296                 goto fail;
297
298         msg = "could not set detach state to joinable";
299         ret = pthread_attr_setdetachstate(&pawd->attr, PTHREAD_CREATE_JOINABLE);
300         if (ret < 0)
301                 goto fail;
302
303         msg = "could not create thread";
304         ret = pthread_create(&pawd->thread, &pawd->attr, aow_play, wn);
305         if (ret < 0)
306                 goto fail;
307         return 1;
308 fail:
309         PARA_ERROR_LOG("%s (%s)\n", msg, strerror(ret));
310         return -E_AO_PTHREAD;
311 }
312
313 static int aow_post_select(__a_unused struct sched *s,
314                 struct task *t)
315 {
316         struct writer_node *wn = container_of(t, struct writer_node, task);
317         struct private_aow_data *pawd = wn->private_data;
318         int ret;
319
320         if (!pawd) {
321                 int32_t rate, ch, format;
322                 struct btr_node_description bnd;
323
324                 ret = btr_node_status(wn->btrn, wn->min_iqs, BTR_NT_LEAF);
325                 if (ret < 0)
326                         goto remove_btrn;
327                 if (ret == 0)
328                         return 0;
329                 get_btr_sample_rate(wn->btrn, &rate);
330                 get_btr_channels(wn->btrn, &ch);
331                 get_btr_sample_format(wn->btrn, &format);
332                 ret = aow_init(wn, rate, ch, format);
333                 if (ret < 0)
334                         goto remove_btrn;
335                 pawd = wn->private_data;
336
337                 /* set up thread btr node */
338                 bnd.name = "ao_thread_btrn";
339                 bnd.parent = wn->btrn;
340                 bnd.child = NULL;
341                 bnd.handler = NULL;
342                 bnd.context = pawd;
343                 pawd->thread_btrn = btr_new_node(&bnd);
344                 wn->private_data = pawd;
345
346                 ret = aow_create_thread(wn);
347                 if (ret < 0)
348                         goto remove_thread_btrn;
349                 return 0;
350         }
351         if (!wn->btrn) {
352                 if (!pawd->thread_btrn) {
353                         pthread_join(pawd->thread, NULL);
354                         return -E_AO_EOF;
355                 }
356                 PARA_INFO_LOG("waiting for play thread to terminate\n");
357                 return 0;
358         }
359         pthread_mutex_lock(&pawd->mutex);
360         ret = btr_node_status(wn->btrn, wn->min_iqs, BTR_NT_LEAF);
361         if (ret > 0) {
362                 btr_pushdown(wn->btrn);
363                 if (pthread_cond_signal(&pawd->data_available) != 0) {
364                         ret = -E_AO_PTHREAD;
365                         PARA_ERROR_LOG("pthread_cond_signal() failed\n");
366                         goto remove_thread_btrn;
367                 }
368         }
369         if (ret >= 0) {
370                 pthread_mutex_unlock(&pawd->mutex);
371                 goto out;
372         }
373         btr_remove_node(&wn->btrn);
374         pthread_cond_signal(&pawd->data_available);
375         pthread_mutex_unlock(&pawd->mutex);
376         return 0;
377 remove_thread_btrn:
378         btr_remove_node(&pawd->thread_btrn);
379 remove_btrn:
380         btr_remove_node(&wn->btrn);
381 out:
382         return ret;
383 }
384
385 __malloc static void *aow_parse_config_or_die(int argc, char **argv)
386 {
387         struct ao_write_args_info *conf = para_calloc(sizeof(*conf));
388
389         /* exits on errors */
390         ao_write_cmdline_parser(argc, argv, conf);
391         return conf;
392 }
393
394 static void aow_free_config(void *conf)
395 {
396         ao_write_cmdline_parser_free(conf);
397 }
398
399 /**
400  * The init function of the ao writer.
401  *
402  * \param w Pointer to the writer to initialize.
403  *
404  * \sa struct writer.
405  */
406 void ao_write_init(struct writer *w)
407 {
408         struct ao_write_args_info dummy;
409         int i, j, num_drivers, num_lines;
410         ao_info **driver_list;
411         char **dh; /* detailed help */
412
413         ao_write_cmdline_parser_init(&dummy);
414         w->close = aow_close;
415         w->pre_select = aow_pre_select;
416         w->post_select = aow_post_select;
417         w->parse_config_or_die = aow_parse_config_or_die;
418         w->free_config = aow_free_config;
419         w->help = (struct ggo_help)DEFINE_GGO_HELP(ao_write);
420         /* create detailed help containing all supported drivers/options */
421         for (i = 0; ao_write_args_info_detailed_help[i]; i++)
422                 ; /* nothing */
423         num_lines = i;
424         dh = para_malloc((num_lines + 3) * sizeof(char *));
425         for (i = 0; i < num_lines; i++)
426                 dh[i] = para_strdup(ao_write_args_info_detailed_help[i]);
427         dh[num_lines++] = para_strdup("libao drivers available on this host:");
428         dh[num_lines++] = para_strdup("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
429
430         ao_initialize();
431         driver_list = ao_driver_info_list(&num_drivers);
432
433         for (i = 0; i < num_drivers; i++) {
434                 ao_info *info = driver_list[i];
435                 char *keys = NULL, *tmp = NULL;
436
437                 if (info->type == AO_TYPE_FILE)
438                         continue;
439                 for (j = 0; j < info->option_count; j++) {
440                         tmp = make_message("%s%s%s", keys? keys : "",
441                                 keys? ", " : "",
442                                 info->options[j]);
443                         free(keys);
444                         keys = tmp;
445                 }
446                 dh = para_realloc(dh, (num_lines + 6) * sizeof(char *));
447                 dh[num_lines++] = make_message("%s: %s", info->short_name, info->name);
448                 dh[num_lines++] = make_message("priority: %d", info->priority);
449                 dh[num_lines++] = make_message("keys: %s", keys? keys : "[none]");
450                 dh[num_lines++] = make_message("comment: %s", info->comment?
451                         info->comment : "[none]");
452                 dh[num_lines++] = para_strdup(NULL);
453                 free(keys);
454         }
455         dh[num_lines] = NULL;
456         w->help.detailed_help = (const char **)dh;
457         ao_write_cmdline_parser_free(&dummy);
458         ao_shutdown();
459 }
460