2 * Copyright (C) 2011 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. */
20 #include "buffer_tree.h"
22 #include "write_common.h"
23 #include "ao_write.cmdline.h"
26 struct private_aow_data
{
32 pthread_mutex_t mutex
;
33 pthread_cond_t data_available
;
34 struct btr_node
*thread_btrn
;
37 static void aow_close(struct writer_node
*wn
)
39 struct private_aow_data
*pawd
= wn
->private_data
;
45 wn
->private_data
= NULL
;
49 static void aow_pre_select(struct sched
*s
, struct task
*t
)
51 struct writer_node
*wn
= container_of(t
, struct writer_node
, task
);
52 int ret
= btr_node_status(wn
->btrn
, wn
->min_iqs
, BTR_NT_LEAF
);
59 static int aow_set_sample_format(unsigned sample_rate
, unsigned channels
,
60 int sample_format
, ao_sample_format
*result
)
62 memset(result
, 0, sizeof(*result
));
63 switch (sample_format
) {
67 return -E_AO_BAD_SAMPLE_FORMAT
;
69 /* no need to set byte_format */
74 result
->byte_format
= AO_FMT_LITTLE
;
78 result
->byte_format
= AO_FMT_BIG
;
81 PARA_EMERG_LOG("bug: invalid sample format\n");
84 result
->channels
= channels
;
85 result
->rate
= sample_rate
;
89 static int aow_open_device(int id
, ao_sample_format
*asf
, ao_option
*options
,
93 ao_device
*dev
= ao_open_live(id
, asf
, options
);
101 msg
= "No driver corresponds to driver_id";
104 msg
= "This driver is not a live output device";
107 msg
= "A valid option key has an invalid value";
110 msg
= "Cannot open the device";
113 msg
= "General libao error";
116 msg
= "Unknown ao error";
119 PARA_ERROR_LOG("%s\n", msg
);
120 return -E_AO_OPEN_LIVE
;
123 static int aow_init(struct writer_node
*wn
, unsigned sample_rate
,
124 unsigned channels
, int sample_format
)
127 ao_option
*aoo
= NULL
;
128 ao_sample_format asf
;
130 struct private_aow_data
*pawd
= para_malloc(sizeof(*pawd
));
131 struct ao_write_args_info
*conf
= wn
->conf
;
134 if (conf
->driver_given
) {
135 ret
= -E_AO_BAD_DRIVER
;
136 id
= ao_driver_id(conf
->driver_arg
);
138 ret
= -E_AO_DEFAULT_DRIVER
;
139 id
= ao_default_driver_id();
143 info
= ao_driver_info(id
);
144 assert(info
&& info
->short_name
);
145 if (info
->type
== AO_TYPE_FILE
) {
146 ret
= -E_AO_FILE_NOT_SUPP
;
149 PARA_INFO_LOG("using %s driver\n", info
->short_name
);
150 for (i
= 0; i
< conf
->ao_option_given
; i
++) {
151 char *o
= para_strdup(conf
->ao_option_arg
[i
]), *value
;
153 ret
= -E_AO_BAD_OPTION
;
154 value
= strchr(o
, ':');
161 PARA_INFO_LOG("appending option: key=%s, value=%s\n", o
, value
);
162 ret
= ao_append_option(&aoo
, o
, value
);
165 ret
= -E_AO_APPEND_OPTION
;
169 ret
= aow_set_sample_format(sample_rate
, channels
, sample_format
, &asf
);
172 if (sample_format
== SF_S8
|| sample_format
== SF_U8
)
173 pawd
->bytes_per_frame
= channels
;
175 pawd
->bytes_per_frame
= channels
* 2;
176 ret
= aow_open_device(id
, &asf
, aoo
, &pawd
->dev
);
179 PARA_INFO_LOG("successfully opened %s\n", info
->short_name
);
180 wn
->private_data
= pawd
;
187 __noreturn
static void *aow_play(void *priv
)
189 struct writer_node
*wn
= priv
;
190 struct private_aow_data
*pawd
= wn
->private_data
;
191 struct btr_node
*btrn
= pawd
->thread_btrn
;
192 size_t frames
, bytes
;
198 * Lock mutex and wait for signal. pthread_cond_wait() will
199 * automatically and atomically unlock mutex while it waits.
201 pthread_mutex_lock(&pawd
->mutex
);
203 ret
= btr_node_status(btrn
, wn
->min_iqs
, BTR_NT_LEAF
);
207 btr_merge(btrn
, wn
->min_iqs
);
208 bytes
= btr_next_buffer(btrn
, &data
);
209 frames
= bytes
/ pawd
->bytes_per_frame
;
212 /* eof and less than a single frame available */
213 ret
= -E_WRITE_COMMON_EOF
;
216 //PARA_CRIT_LOG("waiting for data\n");
218 //pthread_mutex_unlock(&pawd->mutex);
219 pthread_cond_wait(&pawd
->data_available
, &pawd
->mutex
);
221 pthread_mutex_unlock(&pawd
->mutex
);
223 bytes
= frames
* pawd
->bytes_per_frame
;
225 if (ao_play(pawd
->dev
, data
, bytes
) == 0) /* failure */
227 btr_consume(btrn
, bytes
);
230 pthread_mutex_unlock(&pawd
->mutex
);
233 PARA_NOTICE_LOG("%s\n", para_strerror(-ret
));
237 static int aow_create_thread(struct writer_node
*wn
)
239 struct private_aow_data
*pawd
= wn
->private_data
;
243 /* initialize with default attributes */
244 msg
= "could not init mutex";
245 ret
= pthread_mutex_init(&pawd
->mutex
, NULL
);
249 msg
= "could not initialize condition variable";
250 ret
= pthread_cond_init(&pawd
->data_available
, NULL
);
254 msg
= "could not initialize thread attributes";
255 ret
= pthread_attr_init(&pawd
->attr
);
259 /* schedule this thread under the real-time policy SCHED_FIFO */
260 msg
= "could not set sched policy";
261 ret
= pthread_attr_setschedpolicy(&pawd
->attr
, SCHED_FIFO
);
265 msg
= "could not set detach state to joinable";
266 ret
= pthread_attr_setdetachstate(&pawd
->attr
, PTHREAD_CREATE_JOINABLE
);
270 msg
= "could not create thread";
271 ret
= pthread_create(&pawd
->thread
, &pawd
->attr
, aow_play
, wn
);
276 PARA_ERROR_LOG("%s (%s)\n", msg
, strerror(ret
));
277 return -E_AO_PTHREAD
;
280 static void aow_post_select(__a_unused
struct sched
*s
,
283 struct writer_node
*wn
= container_of(t
, struct writer_node
, task
);
284 struct btr_node
*btrn
= wn
->btrn
;
285 struct private_aow_data
*pawd
= wn
->private_data
;
289 int32_t rate
, ch
, format
;
290 struct btr_node_description bnd
;
292 ret
= btr_node_status(btrn
, wn
->min_iqs
, BTR_NT_LEAF
);
297 get_btr_sample_rate(btrn
, &rate
);
298 get_btr_channels(btrn
, &ch
);
299 get_btr_sample_format(btrn
, &format
);
300 ret
= aow_init(wn
, rate
, ch
, format
);
303 pawd
= wn
->private_data
;
305 /* set up thread btr node */
306 bnd
.name
= "ao_thread_btrn";
311 pawd
->thread_btrn
= btr_new_node(&bnd
);
312 wn
->private_data
= pawd
;
314 ret
= aow_create_thread(wn
);
316 goto remove_thread_btrn
;
319 pthread_mutex_lock(&pawd
->mutex
);
320 ret
= btr_node_status(btrn
, wn
->min_iqs
, BTR_NT_LEAF
);
323 pthread_cond_signal(&pawd
->data_available
);
325 pthread_mutex_unlock(&pawd
->mutex
);
328 pthread_mutex_lock(&pawd
->mutex
);
329 btr_remove_node(btrn
);
331 PARA_INFO_LOG("waiting for thread to terminate\n");
332 pthread_cond_signal(&pawd
->data_available
);
333 pthread_mutex_unlock(&pawd
->mutex
);
334 pthread_join(pawd
->thread
, NULL
);
336 btr_remove_node(pawd
->thread_btrn
);
337 btr_free_node(pawd
->thread_btrn
);
340 btr_remove_node(btrn
);
345 __malloc
static void *aow_parse_config_or_die(const char *options
)
347 struct ao_write_args_info
*conf
= para_calloc(sizeof(*conf
));
349 /* exits on errors */
350 ao_cmdline_parser_string(options
, conf
, "ao_write");
354 static void aow_free_config(void *conf
)
356 ao_cmdline_parser_free(conf
);
360 * The init function of the ao writer.
362 * \param w Pointer to the writer to initialize.
366 void ao_write_init(struct writer
*w
)
368 struct ao_write_args_info dummy
;
369 int i
, j
, num_drivers
, num_lines
;
370 ao_info
**driver_list
;
371 char **dh
; /* detailed help */
373 ao_cmdline_parser_init(&dummy
);
374 w
->close
= aow_close
;
375 w
->pre_select
= aow_pre_select
;
376 w
->post_select
= aow_post_select
;
377 w
->parse_config_or_die
= aow_parse_config_or_die
;
378 w
->free_config
= aow_free_config
;
380 w
->help
= (struct ggo_help
) {
381 .short_help
= ao_write_args_info_help
,
383 /* create detailed help containing all supported drivers/options */
384 for (i
= 0; ao_write_args_info_detailed_help
[i
]; i
++)
387 dh
= para_malloc((num_lines
+ 3) * sizeof(char *));
388 for (i
= 0; i
< num_lines
; i
++)
389 dh
[i
] = para_strdup(ao_write_args_info_detailed_help
[i
]);
390 dh
[num_lines
++] = para_strdup("libao drivers available on this host:");
391 dh
[num_lines
++] = para_strdup("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
394 driver_list
= ao_driver_info_list(&num_drivers
);
396 for (i
= 0; i
< num_drivers
; i
++) {
397 ao_info
*info
= driver_list
[i
];
398 char *keys
= NULL
, *tmp
= NULL
;
400 if (info
->type
== AO_TYPE_FILE
)
402 for (j
= 0; j
< info
->option_count
; j
++) {
403 tmp
= make_message("%s%s%s", keys
? keys
: "",
409 dh
= para_realloc(dh
, (num_lines
+ 6) * sizeof(char *));
410 dh
[num_lines
++] = make_message("%s: %s", info
->short_name
, info
->name
);
411 dh
[num_lines
++] = make_message("priority: %d", info
->priority
);
412 dh
[num_lines
++] = make_message("keys: %s", keys
? keys
: "[none]");
413 dh
[num_lines
++] = make_message("comment: %s", info
->comment
?
414 info
->comment
: "[none]");
415 dh
[num_lines
++] = para_strdup(NULL
);
418 dh
[num_lines
] = NULL
;
419 w
->help
.detailed_help
= (const char **)dh
;
420 ao_cmdline_parser_free(&dummy
);