2 * Copyright (C) 2005-2014 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file alsa_write.c paraslash's alsa output plugin */
10 * Based in parts on aplay.c from the alsa-utils-1.0.8 package,
11 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>, which is
12 * based on the vplay program by Michael Beck.
16 #include <sys/types.h>
17 #include <alsa/asoundlib.h>
25 #include "buffer_tree.h"
27 #include "write_common.h"
28 #include "alsa_write.cmdline.h"
31 /** Data specific to the alsa writer. */
32 struct private_alsa_write_data
{
33 /** The alsa handle */
35 /** Determined and set by alsa_init(). */
38 * If the sample rate is not given at the command line and no wav
39 * header was detected, the btr exec mechanism is employed to query the
40 * ancestor buffer tree nodes for this information. In a typical setup
41 * the decoder passes the sample rate back to the alsa writer.
43 * \sa \ref btr_exec_up().
47 * The sample format (8/16 bit, signed/unsigned, little/big endian) is
48 * determined in the same way as the \a sample_rate.
50 snd_pcm_format_t sample_format
;
51 /* The number of channels, again determined like \a sample_rate. */
53 /* time until buffer underrun occurs, in milliseconds */
55 struct timeval drain_barrier
;
56 /* File descriptor for select(). */
60 static snd_pcm_format_t
get_alsa_pcm_format(enum sample_format sf
)
63 case SF_S8
: return SND_PCM_FORMAT_S8
;
64 case SF_U8
: return SND_PCM_FORMAT_U8
;
65 case SF_S16_LE
: return SND_PCM_FORMAT_S16_LE
;
66 case SF_S16_BE
: return SND_PCM_FORMAT_S16_BE
;
67 case SF_U16_LE
: return SND_PCM_FORMAT_U16_LE
;
68 case SF_U16_BE
: return SND_PCM_FORMAT_U16_BE
;
69 default: return SND_PCM_FORMAT_S16_LE
;
73 /* Install PCM software and hardware configuration. */
74 static int alsa_init(struct private_alsa_write_data
*pad
,
75 struct alsa_write_args_info
*conf
)
77 snd_pcm_hw_params_t
*hwparams
;
78 snd_pcm_sw_params_t
*swparams
;
79 snd_pcm_uframes_t start_threshold
, stop_threshold
;
80 snd_pcm_uframes_t buffer_size
, period_size
;
81 snd_output_t
*output_log
;
86 PARA_INFO_LOG("opening %s\n", conf
->device_arg
);
87 msg
= "unable to open pcm";
88 ret
= snd_pcm_open(&pad
->handle
, conf
->device_arg
,
89 SND_PCM_STREAM_PLAYBACK
, SND_PCM_NONBLOCK
);
92 snd_pcm_hw_params_alloca(&hwparams
);
93 msg
= "Broken alsa configuration";
94 ret
= snd_pcm_hw_params_any(pad
->handle
, hwparams
);
97 msg
= "access type not available";
98 ret
= snd_pcm_hw_params_set_access(pad
->handle
, hwparams
,
99 SND_PCM_ACCESS_RW_INTERLEAVED
);
102 msg
= "sample format not available";
103 ret
= snd_pcm_hw_params_set_format(pad
->handle
, hwparams
,
107 msg
= "channels count not available";
108 ret
= snd_pcm_hw_params_set_channels(pad
->handle
, hwparams
,
112 msg
= "could not set sample rate";
113 ret
= snd_pcm_hw_params_set_rate_near(pad
->handle
, hwparams
,
114 &pad
->sample_rate
, NULL
);
117 /* alsa wants microseconds */
118 pad
->buffer_time
= conf
->buffer_time_arg
* 1000;
119 msg
= "could not set buffer time";
120 ret
= snd_pcm_hw_params_set_buffer_time_near(pad
->handle
, hwparams
,
121 &pad
->buffer_time
, NULL
);
124 pad
->buffer_time
/= 1000; /* we prefer milliseconds */
125 period_time
= pad
->buffer_time
* 250; /* buffer time / 4 */
126 msg
= "could not set period time";
127 ret
= snd_pcm_hw_params_set_period_time_near(pad
->handle
, hwparams
,
132 msg
= "unable to install hw params";
133 ret
= snd_pcm_hw_params(pad
->handle
, hwparams
);
136 snd_pcm_hw_params_get_period_size(hwparams
, &period_size
, NULL
);
137 snd_pcm_hw_params_get_buffer_size(hwparams
, &buffer_size
);
138 msg
= "period size equals buffer size";
139 if (period_size
== buffer_size
)
142 /* software parameter setup */
143 snd_pcm_sw_params_alloca(&swparams
);
144 snd_pcm_sw_params_current(pad
->handle
, swparams
);
145 snd_pcm_sw_params_set_avail_min(pad
->handle
, swparams
, period_size
);
149 start_threshold
= PARA_MIN(buffer_size
,
150 (snd_pcm_uframes_t
)pad
->sample_rate
);
151 msg
= "could not set start threshold";
152 ret
= snd_pcm_sw_params_set_start_threshold(pad
->handle
, swparams
,
156 stop_threshold
= buffer_size
;
157 msg
= "could not set stop threshold";
158 ret
= snd_pcm_sw_params_set_stop_threshold(pad
->handle
, swparams
,
162 msg
= "unable to install sw params";
163 ret
= snd_pcm_sw_params(pad
->handle
, swparams
);
166 msg
= "unable to determine bytes per frame";
167 ret
= snd_pcm_format_physical_width(pad
->sample_format
);
170 pad
->bytes_per_frame
= ret
* pad
->channels
/ 8;
171 msg
= "failed to set alsa handle to nonblock mode";
172 ret
= snd_pcm_nonblock(pad
->handle
, 1);
175 ret
= snd_output_buffer_open(&output_log
);
179 PARA_DEBUG_LOG("dumping alsa configuration\n");
180 snd_pcm_dump(pad
->handle
, output_log
);
181 snd_pcm_hw_params_dump(hwparams
, output_log
);
182 sz
= snd_output_buffer_string(output_log
, &buf
);
183 for (p
= buf
; p
< buf
+ sz
;) {
184 char *q
= memchr(p
, '\n', buf
+ sz
- p
);
188 PARA_DEBUG_LOG("%s\n", p
);
191 snd_output_close(output_log
);
196 PARA_ERROR_LOG("%s: %s\n", msg
, snd_strerror(-ret
));
198 PARA_ERROR_LOG("%s\n", msg
);
202 static void alsa_write_pre_select(struct sched
*s
, void *context
)
205 struct writer_node
*wn
= context
;
206 struct private_alsa_write_data
*pad
= wn
->private_data
;
207 int ret
= btr_node_status(wn
->btrn
, wn
->min_iqs
, BTR_NT_LEAF
);
218 sched_request_barrier_or_min_delay(&pad
->drain_barrier
, s
);
221 /* wait at most 50% of the buffer time */
222 sched_request_timeout_ms(pad
->buffer_time
/ 2, s
);
223 ret
= snd_pcm_poll_descriptors(pad
->handle
, &pfd
, 1);
225 PARA_ERROR_LOG("could not get alsa poll fd: %s\n",
229 pad
->poll_fd
= pfd
.fd
;
230 para_fd_set(pfd
.fd
, &s
->rfds
, &s
->max_fileno
);
233 static void alsa_close(struct writer_node
*wn
)
235 struct private_alsa_write_data
*pad
= wn
->private_data
;
236 PARA_INFO_LOG("closing writer node %p\n", wn
);
241 * It's OK to have a blocking operation here because we already made
242 * sure that the PCM output buffer is (nearly) empty.
244 snd_pcm_nonblock(pad
->handle
, 0);
245 snd_pcm_drain(pad
->handle
);
246 snd_pcm_close(pad
->handle
);
247 snd_config_update_free_global();
251 static int alsa_write_post_select(__a_unused
struct sched
*s
, void *context
)
253 struct writer_node
*wn
= context
;
254 struct private_alsa_write_data
*pad
= wn
->private_data
;
255 struct btr_node
*btrn
= wn
->btrn
;
258 snd_pcm_sframes_t frames
;
261 ret
= task_get_notification(wn
->task
);
265 ret
= btr_node_status(btrn
, wn
->min_iqs
, BTR_NT_LEAF
);
268 btr_merge(btrn
, wn
->min_iqs
);
269 bytes
= btr_next_buffer(btrn
, &data
);
270 if (ret
< 0 || bytes
< wn
->min_iqs
) { /* eof */
271 assert(btr_no_parent(btrn
));
272 ret
= -E_WRITE_COMMON_EOF
;
275 /* wait until pending frames are played */
276 if (pad
->drain_barrier
.tv_sec
== 0) {
277 PARA_DEBUG_LOG("waiting for device to drain\n");
278 tv_add(now
, &(struct timeval
)EMBRACE(0, 200 * 1000),
279 &pad
->drain_barrier
);
282 if (tv_diff(now
, &pad
->drain_barrier
, NULL
) > 0)
289 if (bytes
== 0) /* no data available */
291 pad
= para_calloc(sizeof(*pad
));
292 get_btr_sample_rate(btrn
, &val
);
293 pad
->sample_rate
= val
;
294 get_btr_channels(btrn
, &val
);
296 get_btr_sample_format(btrn
, &val
);
297 pad
->sample_format
= get_alsa_pcm_format(val
);
299 PARA_INFO_LOG("%d channel(s), %dHz\n", pad
->channels
,
301 ret
= alsa_init(pad
, wn
->conf
);
306 wn
->private_data
= pad
;
307 wn
->min_iqs
= pad
->bytes_per_frame
;
310 frames
= bytes
/ pad
->bytes_per_frame
;
311 frames
= snd_pcm_writei(pad
->handle
, data
, frames
);
312 if (frames
== 0 || frames
== -EAGAIN
) {
314 if (pad
->poll_fd
>= 0 && FD_ISSET(pad
->poll_fd
, &s
->rfds
))
315 if (read(pad
->poll_fd
, buf
, 100))
320 btr_consume(btrn
, frames
* pad
->bytes_per_frame
);
323 if (frames
== -EPIPE
) {
324 PARA_WARNING_LOG("underrun (tried to write %zu bytes)\n", bytes
);
325 snd_pcm_prepare(pad
->handle
);
328 PARA_ERROR_LOG("alsa write error: %s\n", snd_strerror(-frames
));
332 btr_remove_node(&wn
->btrn
);
336 __malloc
static void *alsa_parse_config_or_die(int argc
, char **argv
)
338 struct alsa_write_args_info
*conf
= para_calloc(sizeof(*conf
));
340 /* exits on errors */
341 alsa_write_cmdline_parser(argc
, argv
, conf
);
345 static void alsa_free_config(void *conf
)
347 alsa_write_cmdline_parser_free(conf
);
351 * The init function of the alsa writer.
353 * \param w Pointer to the writer to initialize.
355 * \sa struct \ref writer.
357 void alsa_write_init(struct writer
*w
)
359 struct alsa_write_args_info dummy
;
361 alsa_write_cmdline_parser_init(&dummy
);
362 w
->close
= alsa_close
;
363 w
->pre_select
= alsa_write_pre_select
;
364 w
->post_select
= alsa_write_post_select
;
365 w
->parse_config_or_die
= alsa_parse_config_or_die
;
366 w
->free_config
= alsa_free_config
;
367 w
->help
= (struct ggo_help
)DEFINE_GGO_HELP(alsa_write
);
368 alsa_write_cmdline_parser_free(&dummy
);