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