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