2 * Copyright (C) 2011-2012 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file ao_write.c Paraslash's libao output plugin. */
19 #include "buffer_tree.h"
21 #include "write_common.h"
22 #include "ao_write.cmdline.h"
25 struct private_aow_data
{
31 pthread_mutex_t mutex
;
32 pthread_cond_t data_available
;
33 struct btr_node
*thread_btrn
;
36 static void aow_close(struct writer_node
*wn
)
38 struct private_aow_data
*pawd
= wn
->private_data
;
44 wn
->private_data
= NULL
;
48 static void aow_pre_select(struct sched
*s
, struct task
*t
)
50 struct writer_node
*wn
= container_of(t
, struct writer_node
, task
);
51 int ret
= btr_node_status(wn
->btrn
, wn
->min_iqs
, BTR_NT_LEAF
);
58 static int aow_set_sample_format(unsigned sample_rate
, unsigned channels
,
59 int sample_format
, ao_sample_format
*result
)
61 memset(result
, 0, sizeof(*result
));
62 switch (sample_format
) {
66 return -E_AO_BAD_SAMPLE_FORMAT
;
68 /* no need to set byte_format */
73 result
->byte_format
= AO_FMT_LITTLE
;
77 result
->byte_format
= AO_FMT_BIG
;
80 PARA_EMERG_LOG("bug: invalid sample format\n");
83 result
->channels
= channels
;
84 result
->rate
= sample_rate
;
88 static int aow_open_device(int id
, ao_sample_format
*asf
, ao_option
*options
,
92 ao_device
*dev
= ao_open_live(id
, asf
, options
);
100 msg
= "No driver corresponds to driver_id";
103 msg
= "This driver is not a live output device";
106 msg
= "A valid option key has an invalid value";
109 msg
= "Cannot open the device";
112 msg
= "General libao error";
115 msg
= "Unknown ao error";
118 PARA_ERROR_LOG("%s\n", msg
);
119 return -E_AO_OPEN_LIVE
;
122 static int aow_init(struct writer_node
*wn
, unsigned sample_rate
,
123 unsigned channels
, int sample_format
)
126 ao_option
*aoo
= NULL
;
127 ao_sample_format asf
;
129 struct private_aow_data
*pawd
= para_malloc(sizeof(*pawd
));
130 struct ao_write_args_info
*conf
= wn
->conf
;
133 if (conf
->driver_given
) {
134 ret
= -E_AO_BAD_DRIVER
;
135 id
= ao_driver_id(conf
->driver_arg
);
137 ret
= -E_AO_DEFAULT_DRIVER
;
138 id
= ao_default_driver_id();
142 info
= ao_driver_info(id
);
143 assert(info
&& info
->short_name
);
144 if (info
->type
== AO_TYPE_FILE
) {
145 ret
= -E_AO_FILE_NOT_SUPP
;
148 PARA_INFO_LOG("using %s driver\n", info
->short_name
);
149 for (i
= 0; i
< conf
->ao_option_given
; i
++) {
150 char *o
= para_strdup(conf
->ao_option_arg
[i
]), *value
;
152 ret
= -E_AO_BAD_OPTION
;
153 value
= strchr(o
, ':');
160 PARA_INFO_LOG("appending option: key=%s, value=%s\n", o
, value
);
161 ret
= ao_append_option(&aoo
, o
, value
);
164 ret
= -E_AO_APPEND_OPTION
;
168 ret
= aow_set_sample_format(sample_rate
, channels
, sample_format
, &asf
);
171 if (sample_format
== SF_S8
|| sample_format
== SF_U8
)
172 pawd
->bytes_per_frame
= channels
;
174 pawd
->bytes_per_frame
= channels
* 2;
175 ret
= aow_open_device(id
, &asf
, aoo
, &pawd
->dev
);
178 PARA_INFO_LOG("successfully opened %s\n", info
->short_name
);
179 wn
->private_data
= pawd
;
186 __noreturn
static void *aow_play(void *priv
)
188 struct writer_node
*wn
= priv
;
189 struct private_aow_data
*pawd
= wn
->private_data
;
190 struct btr_node
*btrn
= pawd
->thread_btrn
;
191 size_t frames
, bytes
;
197 * Lock mutex and wait for signal. pthread_cond_wait() will
198 * automatically and atomically unlock mutex while it waits.
200 pthread_mutex_lock(&pawd
->mutex
);
202 ret
= btr_node_status(btrn
, wn
->min_iqs
, BTR_NT_LEAF
);
206 btr_merge(btrn
, wn
->min_iqs
);
207 bytes
= btr_next_buffer(btrn
, &data
);
208 frames
= bytes
/ pawd
->bytes_per_frame
;
211 /* eof and less than a single frame available */
212 ret
= -E_WRITE_COMMON_EOF
;
215 //PARA_CRIT_LOG("waiting for data\n");
217 //pthread_mutex_unlock(&pawd->mutex);
218 pthread_cond_wait(&pawd
->data_available
, &pawd
->mutex
);
220 pthread_mutex_unlock(&pawd
->mutex
);
222 bytes
= frames
* pawd
->bytes_per_frame
;
224 if (ao_play(pawd
->dev
, data
, bytes
) == 0) /* failure */
226 btr_consume(btrn
, bytes
);
229 pthread_mutex_unlock(&pawd
->mutex
);
232 PARA_NOTICE_LOG("%s\n", para_strerror(-ret
));
236 static int aow_create_thread(struct writer_node
*wn
)
238 struct private_aow_data
*pawd
= wn
->private_data
;
242 /* initialize with default attributes */
243 msg
= "could not init mutex";
244 ret
= pthread_mutex_init(&pawd
->mutex
, NULL
);
248 msg
= "could not initialize condition variable";
249 ret
= pthread_cond_init(&pawd
->data_available
, NULL
);
253 msg
= "could not initialize thread attributes";
254 ret
= pthread_attr_init(&pawd
->attr
);
258 /* schedule this thread under the real-time policy SCHED_FIFO */
259 msg
= "could not set sched policy";
260 ret
= pthread_attr_setschedpolicy(&pawd
->attr
, SCHED_FIFO
);
264 msg
= "could not set detach state to joinable";
265 ret
= pthread_attr_setdetachstate(&pawd
->attr
, PTHREAD_CREATE_JOINABLE
);
269 msg
= "could not create thread";
270 ret
= pthread_create(&pawd
->thread
, &pawd
->attr
, aow_play
, wn
);
275 PARA_ERROR_LOG("%s (%s)\n", msg
, strerror(ret
));
276 return -E_AO_PTHREAD
;
279 static void aow_post_select(__a_unused
struct sched
*s
,
282 struct writer_node
*wn
= container_of(t
, struct writer_node
, task
);
283 struct btr_node
*btrn
= wn
->btrn
;
284 struct private_aow_data
*pawd
= wn
->private_data
;
288 int32_t rate
, ch
, format
;
289 struct btr_node_description bnd
;
291 ret
= btr_node_status(btrn
, wn
->min_iqs
, BTR_NT_LEAF
);
296 get_btr_sample_rate(btrn
, &rate
);
297 get_btr_channels(btrn
, &ch
);
298 get_btr_sample_format(btrn
, &format
);
299 ret
= aow_init(wn
, rate
, ch
, format
);
302 pawd
= wn
->private_data
;
304 /* set up thread btr node */
305 bnd
.name
= "ao_thread_btrn";
310 pawd
->thread_btrn
= btr_new_node(&bnd
);
311 wn
->private_data
= pawd
;
313 ret
= aow_create_thread(wn
);
315 goto remove_thread_btrn
;
318 pthread_mutex_lock(&pawd
->mutex
);
319 ret
= btr_node_status(btrn
, wn
->min_iqs
, BTR_NT_LEAF
);
322 pthread_cond_signal(&pawd
->data_available
);
324 pthread_mutex_unlock(&pawd
->mutex
);
327 pthread_mutex_lock(&pawd
->mutex
);
328 btr_remove_node(btrn
);
330 PARA_INFO_LOG("waiting for thread to terminate\n");
331 pthread_cond_signal(&pawd
->data_available
);
332 pthread_mutex_unlock(&pawd
->mutex
);
333 pthread_join(pawd
->thread
, NULL
);
335 btr_remove_node(pawd
->thread_btrn
);
336 btr_free_node(pawd
->thread_btrn
);
339 btr_remove_node(btrn
);
344 __malloc
static void *aow_parse_config_or_die(const char *options
)
346 struct ao_write_args_info
*conf
= para_calloc(sizeof(*conf
));
348 /* exits on errors */
349 ao_cmdline_parser_string(options
, conf
, "ao_write");
353 static void aow_free_config(void *conf
)
355 ao_cmdline_parser_free(conf
);
359 * The init function of the ao writer.
361 * \param w Pointer to the writer to initialize.
365 void ao_write_init(struct writer
*w
)
367 struct ao_write_args_info dummy
;
368 int i
, j
, num_drivers
, num_lines
;
369 ao_info
**driver_list
;
370 char **dh
; /* detailed help */
372 ao_cmdline_parser_init(&dummy
);
373 w
->close
= aow_close
;
374 w
->pre_select
= aow_pre_select
;
375 w
->post_select
= aow_post_select
;
376 w
->parse_config_or_die
= aow_parse_config_or_die
;
377 w
->free_config
= aow_free_config
;
379 w
->help
= (struct ggo_help
) {
380 .short_help
= ao_write_args_info_help
,
382 /* create detailed help containing all supported drivers/options */
383 for (i
= 0; ao_write_args_info_detailed_help
[i
]; i
++)
386 dh
= para_malloc((num_lines
+ 3) * sizeof(char *));
387 for (i
= 0; i
< num_lines
; i
++)
388 dh
[i
] = para_strdup(ao_write_args_info_detailed_help
[i
]);
389 dh
[num_lines
++] = para_strdup("libao drivers available on this host:");
390 dh
[num_lines
++] = para_strdup("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
393 driver_list
= ao_driver_info_list(&num_drivers
);
395 for (i
= 0; i
< num_drivers
; i
++) {
396 ao_info
*info
= driver_list
[i
];
397 char *keys
= NULL
, *tmp
= NULL
;
399 if (info
->type
== AO_TYPE_FILE
)
401 for (j
= 0; j
< info
->option_count
; j
++) {
402 tmp
= make_message("%s%s%s", keys
? keys
: "",
408 dh
= para_realloc(dh
, (num_lines
+ 6) * sizeof(char *));
409 dh
[num_lines
++] = make_message("%s: %s", info
->short_name
, info
->name
);
410 dh
[num_lines
++] = make_message("priority: %d", info
->priority
);
411 dh
[num_lines
++] = make_message("keys: %s", keys
? keys
: "[none]");
412 dh
[num_lines
++] = make_message("comment: %s", info
->comment
?
413 info
->comment
: "[none]");
414 dh
[num_lines
++] = para_strdup(NULL
);
417 dh
[num_lines
] = NULL
;
418 w
->help
.detailed_help
= (const char **)dh
;
419 ao_cmdline_parser_free(&dummy
);