1 /* Copyright (C) 2011 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
3 /** \file ao_write.c Paraslash's libao output plugin. */
10 #include "write_cmd.lsg.h"
16 #include "buffer_tree.h"
20 struct private_aow_data
{
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
;
32 static void aow_close(struct writer_node
*wn
)
34 struct private_aow_data
*pawd
= wn
->private_data
;
38 assert(!pawd
->thread_btrn
);
41 wn
->private_data
= NULL
;
45 static void aow_pre_monitor(struct sched
*s
, void *context
)
47 struct writer_node
*wn
= context
;
48 struct private_aow_data
*pawd
= wn
->private_data
;
51 if (!pawd
) { /* not yet started */
53 ret
= btr_node_status(wn
->btrn
, wn
->min_iqs
, BTR_NT_LEAF
);
56 return; /* no data available */
58 if (!wn
->btrn
) { /* EOF */
59 if (!pawd
->thread_btrn
) /* ready to exit */
61 /* wait for the play thread to terminate */
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
);
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.
77 return sched_request_timeout_ms(20, s
);
82 static int aow_set_sample_format(unsigned sample_rate
, unsigned channels
,
83 int sample_format
, ao_sample_format
*result
)
85 memset(result
, 0, sizeof(*result
));
86 switch (sample_format
) {
90 return -E_BAD_SAMPLE_FORMAT
;
92 /* no need to set byte_format */
97 result
->byte_format
= AO_FMT_LITTLE
;
101 result
->byte_format
= AO_FMT_BIG
;
104 PARA_EMERG_LOG("bug: invalid sample format\n");
107 result
->channels
= channels
;
108 result
->rate
= sample_rate
;
112 static int aow_open_device(int id
, ao_sample_format
*asf
, ao_option
*options
,
116 ao_device
*dev
= ao_open_live(id
, asf
, options
);
124 msg
= "No driver corresponds to driver_id";
127 msg
= "This driver is not a live output device";
130 msg
= "A valid option key has an invalid value";
133 msg
= "Cannot open the device";
136 msg
= "General libao error";
139 msg
= "Unknown ao error";
142 PARA_ERROR_LOG("%s\n", msg
);
143 return -E_AO_OPEN_LIVE
;
146 static void aow_show_drivers(void)
148 int i
, j
, num_drivers
;
149 ao_info
**driver_list
;
151 PARA_DEBUG_LOG("libao drivers available on this host:\n");
152 PARA_DEBUG_LOG("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
154 driver_list
= ao_driver_info_list(&num_drivers
);
156 for (i
= 0; i
< num_drivers
; i
++) {
157 ao_info
*info
= driver_list
[i
];
158 char *keys
= NULL
, *tmp
= NULL
;
160 if (info
->type
== AO_TYPE_FILE
)
162 PARA_DEBUG_LOG("name: %s: %s\n", info
->short_name
, info
->name
);
163 PARA_DEBUG_LOG("priority: %d\n", info
->priority
);
164 for (j
= 0; j
< info
->option_count
; j
++) {
165 tmp
= make_message("%s%s%s", keys
? keys
: "",
171 PARA_DEBUG_LOG("keys: %s\n", keys
? keys
: "[none]");
173 PARA_DEBUG_LOG("comment: %s\n", info
->comment
?
174 info
->comment
: "[none]");
178 static int aow_init(struct writer_node
*wn
, unsigned sample_rate
,
179 unsigned channels
, int sample_format
)
182 ao_option
*aoo
= NULL
;
183 ao_sample_format asf
;
185 const struct lls_opt_result
*r
;
187 struct private_aow_data
*pawd
= alloc(sizeof(*pawd
));
191 if (WRITE_CMD_OPT_GIVEN(AO
, DRIVER
, wn
->lpr
)) {
192 ret
= -E_AO_BAD_DRIVER
;
193 id
= ao_driver_id(WRITE_CMD_OPT_STRING_VAL(AO
, DRIVER
, wn
->lpr
));
195 ret
= -E_AO_DEFAULT_DRIVER
;
196 id
= ao_default_driver_id();
200 info
= ao_driver_info(id
);
201 assert(info
&& info
->short_name
);
202 if (info
->type
== AO_TYPE_FILE
) {
203 ret
= -E_AO_FILE_NOT_SUPP
;
206 PARA_INFO_LOG("using %s driver\n", info
->short_name
);
207 r
= WRITE_CMD_OPT_RESULT(AO
, AO_OPTION
, wn
->lpr
);
208 n
= lls_opt_given(r
);
209 for (i
= 0; i
< n
; i
++) {
210 char *o
= para_strdup(lls_string_val(i
, r
));
213 ret
= -E_AO_BAD_OPTION
;
214 value
= strchr(o
, ':');
221 PARA_INFO_LOG("appending option: key=%s, value=%s\n", o
, value
);
222 ret
= ao_append_option(&aoo
, o
, value
);
225 ret
= -E_AO_APPEND_OPTION
;
229 ret
= aow_set_sample_format(sample_rate
, channels
, sample_format
, &asf
);
232 if (sample_format
== SF_S8
|| sample_format
== SF_U8
)
233 pawd
->bytes_per_frame
= channels
;
235 pawd
->bytes_per_frame
= channels
* 2;
236 ret
= aow_open_device(id
, &asf
, aoo
, &pawd
->dev
);
239 PARA_INFO_LOG("successfully opened %s\n", info
->short_name
);
240 wn
->private_data
= pawd
;
247 static void *aow_play(void *priv
)
249 struct writer_node
*wn
= priv
;
250 struct private_aow_data
*pawd
= wn
->private_data
;
251 struct btr_node
*btrn
= pawd
->thread_btrn
;
252 size_t frames
, bytes
;
256 pthread_mutex_lock(&pawd
->mutex
);
259 ret
= btr_node_status(btrn
, wn
->min_iqs
, BTR_NT_LEAF
);
263 btr_merge(btrn
, wn
->min_iqs
);
264 bytes
= btr_next_buffer(btrn
, &data
);
265 frames
= bytes
/ pawd
->bytes_per_frame
;
268 /* eof and less than a single frame available */
269 ret
= -E_WRITE_COMMON_EOF
;
273 * No data available, go to sleep and wait for the main
274 * thread to wake us up. pthread_cond_wait() unlocks
275 * the mutex while it waits and locks it again upon
278 ret
= pthread_cond_wait(&pawd
->data_available
,
280 /* pthread_cond_wait() can never fail here */
284 bytes
= frames
* pawd
->bytes_per_frame
;
285 pthread_mutex_unlock(&pawd
->mutex
);
286 ret
= ao_play(pawd
->dev
, data
, bytes
);
287 pthread_mutex_lock(&pawd
->mutex
);
288 if (ret
== 0) { /* failure */
292 btr_consume(btrn
, bytes
);
295 btr_remove_node(&pawd
->thread_btrn
);
297 PARA_NOTICE_LOG("%s\n", para_strerror(-ret
));
298 pthread_mutex_unlock(&pawd
->mutex
);
302 static int aow_create_thread(struct writer_node
*wn
)
304 struct private_aow_data
*pawd
= wn
->private_data
;
308 /* initialize with default attributes */
309 msg
= "could not init mutex";
310 ret
= pthread_mutex_init(&pawd
->mutex
, NULL
);
314 msg
= "could not initialize condition variable";
315 ret
= pthread_cond_init(&pawd
->data_available
, NULL
);
319 msg
= "could not initialize thread attributes";
320 ret
= pthread_attr_init(&pawd
->attr
);
324 /* schedule this thread under the real-time policy SCHED_FIFO */
325 msg
= "could not set sched policy";
326 ret
= pthread_attr_setschedpolicy(&pawd
->attr
, SCHED_FIFO
);
330 msg
= "could not set detach state to joinable";
331 ret
= pthread_attr_setdetachstate(&pawd
->attr
, PTHREAD_CREATE_JOINABLE
);
335 msg
= "could not create thread";
336 ret
= pthread_create(&pawd
->thread
, &pawd
->attr
, aow_play
, wn
);
341 PARA_ERROR_LOG("%s (%s)\n", msg
, strerror(ret
));
342 return -E_AO_PTHREAD
;
345 static int aow_post_monitor(__a_unused
struct sched
*s
, void *context
)
347 struct writer_node
*wn
= context
;
348 struct private_aow_data
*pawd
= wn
->private_data
;
352 int32_t rate
, ch
, format
;
353 struct btr_node_description bnd
;
355 ret
= btr_node_status(wn
->btrn
, wn
->min_iqs
, BTR_NT_LEAF
);
360 ret
= get_btr_sample_rate(wn
->btrn
, &rate
);
363 ret
= get_btr_channels(wn
->btrn
, &ch
);
366 ret
= get_btr_sample_format(wn
->btrn
, &format
);
369 ret
= aow_init(wn
, rate
, ch
, format
);
372 pawd
= wn
->private_data
;
374 /* set up thread btr node */
375 bnd
.name
= "ao_thread_btrn";
376 bnd
.parent
= wn
->btrn
;
380 pawd
->thread_btrn
= btr_new_node(&bnd
);
381 wn
->private_data
= pawd
;
383 ret
= aow_create_thread(wn
);
385 goto remove_thread_btrn
;
389 if (!pawd
->thread_btrn
) {
390 pthread_join(pawd
->thread
, NULL
);
393 PARA_INFO_LOG("waiting for play thread to terminate\n");
396 pthread_mutex_lock(&pawd
->mutex
);
397 ret
= btr_node_status(wn
->btrn
, wn
->min_iqs
, BTR_NT_LEAF
);
399 btr_pushdown(wn
->btrn
);
400 if (pthread_cond_signal(&pawd
->data_available
) != 0) {
402 PARA_ERROR_LOG("pthread_cond_signal() failed\n");
403 goto remove_thread_btrn
;
407 pthread_mutex_unlock(&pawd
->mutex
);
410 btr_remove_node(&wn
->btrn
);
411 pthread_cond_signal(&pawd
->data_available
);
412 pthread_mutex_unlock(&pawd
->mutex
);
415 btr_remove_node(&pawd
->thread_btrn
);
417 btr_remove_node(&wn
->btrn
);
422 struct writer lsg_write_cmd_com_ao_user_data
= {
424 .pre_monitor
= aow_pre_monitor
,
425 .post_monitor
= aow_post_monitor
,