2 * Copyright (C) 2005-2009 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>
18 #include <alsa/asoundlib.h>
29 #include "alsa_write.cmdline.h"
31 #include "buffer_tree.h"
33 /** always use 16 bit little endian */
34 #define FORMAT SND_PCM_FORMAT_S16_LE
36 /** Data specific to the alsa writer. */
37 struct private_alsa_write_data {
38 /** The alsa handle */
40 /** Determined and set by alsa_open(). */
42 /** The approximate maximum buffer duration in us. */
44 /* Number of frames that fit into the buffer. */
45 unsigned buffer_frames;
47 * The samplerate given by command line option or the decoder
48 * of the writer node group.
52 * The number of channels, given by command line option or the
53 * decoder of the writer node group.
58 /* Install PCM software and hardware configuration. */
59 static int alsa_init(struct private_alsa_write_data *pad,
60 struct alsa_write_args_info *conf)
62 snd_pcm_hw_params_t *hwparams;
63 snd_pcm_sw_params_t *swparams;
64 snd_pcm_uframes_t buffer_size, start_threshold, stop_threshold;
65 snd_pcm_uframes_t period_size;
68 err = snd_pcm_open(&pad->handle, conf->device_arg,
69 SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
73 snd_pcm_hw_params_alloca(&hwparams);
74 snd_pcm_sw_params_alloca(&swparams);
75 if (snd_pcm_hw_params_any(pad->handle, hwparams) < 0)
76 return -E_BROKEN_CONF;
77 if (snd_pcm_hw_params_set_access(pad->handle, hwparams,
78 SND_PCM_ACCESS_RW_INTERLEAVED) < 0)
79 return -E_ACCESS_TYPE;
80 if (snd_pcm_hw_params_set_format(pad->handle, hwparams, FORMAT) < 0)
81 return -E_SAMPLE_FORMAT;
82 if (snd_pcm_hw_params_set_channels(pad->handle, hwparams,
84 return -E_CHANNEL_COUNT;
85 if (snd_pcm_hw_params_set_rate_near(pad->handle, hwparams,
86 &pad->samplerate, NULL) < 0)
88 err = snd_pcm_hw_params_get_buffer_time_max(hwparams,
89 &pad->buffer_time, NULL);
90 if (err < 0 || !pad->buffer_time)
91 return -E_GET_BUFFER_TIME;
92 PARA_INFO_LOG("buffer time: %d\n", pad->buffer_time);
93 if (snd_pcm_hw_params_set_buffer_time_near(pad->handle, hwparams,
94 &pad->buffer_time, NULL) < 0)
95 return -E_SET_BUFFER_TIME;
96 if (snd_pcm_hw_params(pad->handle, hwparams) < 0)
98 snd_pcm_hw_params_get_period_size(hwparams, &period_size, NULL);
99 snd_pcm_hw_params_get_buffer_size(hwparams, &buffer_size);
100 PARA_INFO_LOG("buffer size: %lu, period_size: %lu\n", buffer_size,
102 if (period_size == buffer_size)
103 return -E_BAD_PERIOD;
104 snd_pcm_sw_params_current(pad->handle, swparams);
105 snd_pcm_sw_params_set_avail_min(pad->handle, swparams, period_size);
109 start_threshold = PARA_MIN(buffer_size,
110 (snd_pcm_uframes_t)pad->samplerate);
111 if (snd_pcm_sw_params_set_start_threshold(pad->handle, swparams,
112 start_threshold) < 0)
113 return -E_START_THRESHOLD;
114 stop_threshold = buffer_size;
115 if (snd_pcm_sw_params_set_stop_threshold(pad->handle, swparams,
117 return -E_STOP_THRESHOLD;
118 if (snd_pcm_sw_params(pad->handle, swparams) < 0)
119 PARA_WARNING_LOG("unable to install sw params\n");
120 pad->bytes_per_frame = snd_pcm_format_physical_width(FORMAT)
122 if (pad->bytes_per_frame <= 0)
123 return -E_PHYSICAL_WIDTH;
124 PARA_INFO_LOG("bytes per frame: %d\n", pad->bytes_per_frame);
125 if (snd_pcm_nonblock(pad->handle, 1))
126 PARA_ERROR_LOG("failed to set nonblock mode\n");
127 pad->buffer_frames = 1000 * pad->buffer_time / pad->samplerate;
128 PARA_INFO_LOG("max buffered frames: %d\n", pad->buffer_frames);
132 /* Open an instance of the alsa writer. */
133 static int alsa_open_nobtr(struct writer_node *wn)
135 struct alsa_write_args_info *conf = wn->conf;
136 struct writer_node_group *wng = wn->wng;
137 struct private_alsa_write_data *pad = para_calloc(sizeof(*pad));
139 wn->private_data = pad;
140 if (!conf->samplerate_given && wng->samplerate)
141 pad->samplerate = *wng->samplerate;
143 pad->samplerate = conf->samplerate_arg;
144 if (!conf->channels_given && wng->channels)
145 pad->channels = *wng->channels;
147 pad->channels = conf->channels_arg;
148 PARA_INFO_LOG("%d channel(s), %dHz\n", pad->channels, pad->samplerate);
152 static int alsa_open_btr(struct writer_node *wn)
154 struct alsa_write_args_info *conf = wn->conf;
155 struct private_alsa_write_data *pad = para_calloc(sizeof(*pad));
159 sprintf(wn->task.status, "alsa writer");
160 wn->private_data = pad;
163 pad->samplerate = conf->samplerate_arg;
164 pad->channels = conf->channels_arg;
166 if (!conf->samplerate_given) { /* config option trumps btr_exec */
167 /* ask parent btr nodes */
168 ret = btr_exec_up(wn->btrn, "samplerate", &buf);
171 ret = para_atoi32(buf, &rate);
172 if (ret < 0) /* should not happen */
174 pad->samplerate = rate;
179 if (!conf->channels_given) {
180 ret = btr_exec_up(wn->btrn, "channels", &buf);
183 ret = para_atoi32(buf, &ch);
190 PARA_INFO_LOG("%d channel(s), %dHz\n", pad->channels, pad->samplerate);
198 static int alsa_open(struct writer_node *wn)
200 struct alsa_write_args_info *conf = wn->conf;
202 if (conf->buffer_tree_given)
203 return alsa_open_btr(wn);
205 return alsa_open_nobtr(wn);
209 static int alsa_write_pre_select(struct sched *s, struct writer_node *wn)
211 struct alsa_write_args_info *conf = wn->conf;
212 struct private_alsa_write_data *pad = wn->private_data;
213 struct writer_node_group *wng = wn->wng;
215 snd_pcm_sframes_t avail, underrun;
219 if (conf->buffer_tree_given) {
220 size_t sz = btr_get_input_queue_size(wn->btrn);
221 //PARA_CRIT_LOG("input queue: %zu\n", sz);
222 //if (sz < pad->bytes_per_frame)
225 if (*wng->loaded - wn->written < pad->bytes_per_frame)
229 * Data is available to be written to the alsa handle. Compute number
230 * of milliseconds until next buffer underrun would occur.
232 * snd_pcm_avail_update() updates the current available count of
233 * samples for writing. It is a light method to obtain current stream
234 * position, because it does not require the user <-> kernel context
235 * switch, but the value is less accurate, because ring buffer pointers
236 * are updated in kernel drivers only when an interrupt occurs.
238 avail = snd_pcm_avail_update(pad->handle);
241 underrun = (pad->buffer_frames - avail) * pad->buffer_time
242 / pad->buffer_frames / 1000;
246 ms2tv(underrun, &tv);
247 if (tv_diff(&s->timeout, &tv, NULL) > 0)
249 //PARA_CRIT_LOG("timout: %lu\n", tv2ms(&s->timeout));
253 static void alsa_write_pre_select_btr(struct sched *s, struct task *t)
255 struct writer_node *wn = container_of(t, struct writer_node, task);
256 t->error = alsa_write_pre_select(s, wn);
259 static void xrun(snd_pcm_t *handle)
261 snd_pcm_status_t *status;
263 struct timeval tv, diff;
265 snd_pcm_status_alloca(&status);
266 ret = snd_pcm_status(handle, status);
269 if (snd_pcm_status_get_state(status) != SND_PCM_STATE_XRUN)
271 snd_pcm_status_get_trigger_tstamp(status, &tv);
272 tv_diff(now, &tv, &diff);
273 PARA_WARNING_LOG("underrun: %lums\n", tv2ms(&diff));
276 static int alsa_write_post_select(__a_unused struct sched *s,
277 struct writer_node *wn)
279 struct private_alsa_write_data *pad = wn->private_data;
280 struct writer_node_group *wng = wn->wng;
281 size_t bytes = *wng->loaded - wn->written;
282 unsigned char *data = (unsigned char*)*wng->bufp + wn->written;
283 snd_pcm_sframes_t ret, frames, avail;
285 if (*wng->input_error < 0 && (!pad->handle || bytes < pad->bytes_per_frame)) {
286 wn->written = *wng->loaded;
287 return *wng->input_error;
289 if (!bytes) /* no data available */
292 int err = alsa_init(pad, wn->conf);
296 frames = bytes / pad->bytes_per_frame;
297 if (!frames) /* less than a single frame available */
299 avail = snd_pcm_avail_update(pad->handle);
302 frames = PARA_MIN(frames, avail);
303 ret = snd_pcm_writei(pad->handle, data, frames);
305 wn->written += ret * pad->bytes_per_frame;
310 snd_pcm_prepare(pad->handle);
313 PARA_WARNING_LOG("%s\n", snd_strerror(-ret));
316 return -E_ALSA_WRITE;
319 static void alsa_write_post_select_btr(__a_unused struct sched *s,
322 struct writer_node *wn = container_of(t, struct writer_node, task);
323 struct private_alsa_write_data *pad = wn->private_data;
326 snd_pcm_sframes_t frames, avail;
330 bytes = btr_next_buffer(wn->btrn, &data);
331 //PARA_CRIT_LOG("have: %zu\n", bytes);
333 ret = -E_ALSA_ORPHAN;
334 if (btr_no_parent(wn->btrn) && (!pad->handle || bytes < pad->bytes_per_frame))
337 if (bytes == 0) /* no data available */
339 PARA_CRIT_LOG("alsa init\n");
340 ret = alsa_init(pad, wn->conf);
347 if (bytes >= pad->bytes_per_frame)
349 /* should not be possible to reach this */
350 PARA_CRIT_LOG("dropping %zu byte buffer\n", bytes);
351 btr_consume(wn->btrn, bytes);
352 bytes = btr_next_buffer(wn->btrn, &data);
354 frames = bytes / pad->bytes_per_frame;
355 avail = snd_pcm_avail_update(pad->handle);
358 frames = PARA_MIN(frames, avail);
359 //PARA_CRIT_LOG("writing %ld frames\n", frames);
360 frames = snd_pcm_writei(pad->handle, data, frames);
362 btr_consume(wn->btrn, frames * pad->bytes_per_frame);
365 if (frames == -EPIPE) {
367 snd_pcm_prepare(pad->handle);
370 PARA_WARNING_LOG("%s\n", snd_strerror(-frames));
371 if (frames == -EAGAIN)
376 btr_del_node(wn->btrn);
381 static void alsa_close(struct writer_node *wn)
383 struct private_alsa_write_data *pad = wn->private_data;
384 PARA_INFO_LOG("closing writer node %p\n", wn);
387 snd_pcm_drain(pad->handle);
388 snd_pcm_close(pad->handle);
389 snd_config_update_free_global();
394 __malloc static void *alsa_parse_config(const char *options)
397 struct alsa_write_args_info *conf
398 = para_calloc(sizeof(struct alsa_write_args_info));
400 PARA_INFO_LOG("options: %s, %zd\n", options, strcspn(options, " \t"));
401 ret = alsa_cmdline_parser_string(options, conf, "alsa_write");
404 PARA_INFO_LOG("help given: %d\n", conf->help_given);
412 * the init function of the alsa writer
414 * \param w pointer to the writer to initialize
418 void alsa_write_init(struct writer *w)
420 struct alsa_write_args_info dummy;
422 alsa_cmdline_parser_init(&dummy);
424 w->close = alsa_close;
425 w->pre_select = alsa_write_pre_select;
426 w->pre_select_btr = alsa_write_pre_select_btr;
427 w->post_select = alsa_write_post_select;
428 w->post_select_btr = alsa_write_post_select_btr;
429 w->parse_config = alsa_parse_config;
430 w->shutdown = NULL; /* nothing to do */
431 w->help = (struct ggo_help) {
432 .short_help = alsa_write_args_info_help,
433 .detailed_help = alsa_write_args_info_detailed_help
435 alsa_cmdline_parser_free(&dummy);