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