alsa: Improve documentation of struct private_alsa_write_data.
[paraslash.git] / alsa_write.c
1 /*
2 * Copyright (C) 2005-2013 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file alsa_write.c paraslash's alsa output plugin */
8
9 /*
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.
13 */
14
15 #include <regex.h>
16 #include <sys/types.h>
17 #include <alsa/asoundlib.h>
18
19 #include "para.h"
20 #include "fd.h"
21 #include "string.h"
22 #include "list.h"
23 #include "sched.h"
24 #include "ggo.h"
25 #include "buffer_tree.h"
26 #include "write.h"
27 #include "write_common.h"
28 #include "alsa_write.cmdline.h"
29 #include "error.h"
30
31 /** Data specific to the alsa writer. */
32 struct private_alsa_write_data {
33 /** The alsa handle */
34 snd_pcm_t *handle;
35 /** Determined and set by alsa_init(). */
36 int bytes_per_frame;
37 /*
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.
42 *
43 * \sa \ref btr_exec_up().
44 */
45 unsigned sample_rate;
46 /*
47 * The sample format (8/16 bit, signed/unsigned, little/big endian) is
48 * determined in the same way as the \a sample_rate.
49 */
50 snd_pcm_format_t sample_format;
51 /* The number of channels, again determined like \a sample_rate. */
52 unsigned channels;
53 struct timeval drain_barrier;
54 /* File descriptor for select(). */
55 int poll_fd;
56 };
57
58 static snd_pcm_format_t get_alsa_pcm_format(enum sample_format sf)
59 {
60 switch (sf) {
61 case SF_S8: return SND_PCM_FORMAT_S8;
62 case SF_U8: return SND_PCM_FORMAT_U8;
63 case SF_S16_LE: return SND_PCM_FORMAT_S16_LE;
64 case SF_S16_BE: return SND_PCM_FORMAT_S16_BE;
65 case SF_U16_LE: return SND_PCM_FORMAT_U16_LE;
66 case SF_U16_BE: return SND_PCM_FORMAT_U16_BE;
67 default: return SND_PCM_FORMAT_S16_LE;
68 }
69 }
70
71 /* Install PCM software and hardware configuration. */
72 static int alsa_init(struct private_alsa_write_data *pad,
73 struct alsa_write_args_info *conf)
74 {
75 snd_pcm_hw_params_t *hwparams;
76 snd_pcm_sw_params_t *swparams;
77 snd_pcm_uframes_t start_threshold, stop_threshold;
78 snd_pcm_uframes_t buffer_size, period_size;
79 snd_output_t *output_log;
80 unsigned buffer_time;
81 int ret;
82 const char *msg;
83
84 PARA_INFO_LOG("opening %s\n", conf->device_arg);
85 msg = "unable to open pcm";
86 ret = snd_pcm_open(&pad->handle, conf->device_arg,
87 SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
88 if (ret < 0)
89 goto fail;
90 snd_pcm_hw_params_alloca(&hwparams);
91 msg = "Broken alsa configuration";
92 ret = snd_pcm_hw_params_any(pad->handle, hwparams);
93 if (ret < 0)
94 goto fail;
95 msg = "access type not available";
96 ret = snd_pcm_hw_params_set_access(pad->handle, hwparams,
97 SND_PCM_ACCESS_RW_INTERLEAVED);
98 if (ret < 0)
99 goto fail;
100 msg = "sample format not available";
101 ret = snd_pcm_hw_params_set_format(pad->handle, hwparams,
102 pad->sample_format);
103 if (ret < 0)
104 goto fail;
105 msg = "channels count not available";
106 ret = snd_pcm_hw_params_set_channels(pad->handle, hwparams,
107 pad->channels);
108 if (ret < 0)
109 goto fail;
110 msg = "could not set sample rate";
111 ret = snd_pcm_hw_params_set_rate_near(pad->handle, hwparams,
112 &pad->sample_rate, NULL);
113 if (ret < 0)
114 goto fail;
115 msg = "unable to get buffer time";
116 ret = snd_pcm_hw_params_get_buffer_time_max(hwparams, &buffer_time,
117 NULL);
118 if (ret < 0 || buffer_time == 0)
119 goto fail;
120 /* buffer at most 500 milliseconds */
121 buffer_time = PARA_MIN(buffer_time, 500U * 1000U);
122 msg = "could not set buffer time";
123 ret = snd_pcm_hw_params_set_buffer_time_near(pad->handle, hwparams,
124 &buffer_time, NULL);
125 if (ret < 0)
126 goto fail;
127 msg = "unable to install hw params";
128 ret = snd_pcm_hw_params(pad->handle, hwparams);
129 if (ret < 0)
130 goto fail;
131 snd_pcm_hw_params_get_period_size(hwparams, &period_size, NULL);
132 snd_pcm_hw_params_get_buffer_size(hwparams, &buffer_size);
133 msg = "period size equals buffer size";
134 if (period_size == buffer_size)
135 goto fail;
136
137 /* software parameter setup */
138 snd_pcm_sw_params_alloca(&swparams);
139 snd_pcm_sw_params_current(pad->handle, swparams);
140 snd_pcm_sw_params_set_avail_min(pad->handle, swparams, period_size);
141 if (buffer_size < 1)
142 start_threshold = 1;
143 else
144 start_threshold = PARA_MIN(buffer_size,
145 (snd_pcm_uframes_t)pad->sample_rate);
146 msg = "could not set start threshold";
147 ret = snd_pcm_sw_params_set_start_threshold(pad->handle, swparams,
148 start_threshold);
149 if (ret < 0)
150 goto fail;
151 stop_threshold = buffer_size;
152 msg = "could not set stop threshold";
153 ret = snd_pcm_sw_params_set_stop_threshold(pad->handle, swparams,
154 stop_threshold);
155 if (ret < 0)
156 goto fail;
157 msg = "unable to install sw params";
158 ret = snd_pcm_sw_params(pad->handle, swparams);
159 if (ret < 0)
160 goto fail;
161 msg = "unable to determine bytes per frame";
162 ret = snd_pcm_format_physical_width(pad->sample_format);
163 if (ret <= 0)
164 goto fail;
165 pad->bytes_per_frame = ret * pad->channels / 8;
166 msg = "failed to set alsa handle to nonblock mode";
167 ret = snd_pcm_nonblock(pad->handle, 1);
168 if (ret < 0)
169 goto fail;
170 ret = snd_output_buffer_open(&output_log);
171 if (ret == 0) {
172 char *buf, *p;
173 size_t sz;
174 PARA_INFO_LOG("dumping alsa configuration\n");
175 snd_pcm_dump(pad->handle, output_log);
176 sz = snd_output_buffer_string(output_log, &buf);
177 for (p = buf; p < buf + sz;) {
178 char *q = memchr(p, '\n', buf + sz - p);
179 if (!q)
180 break;
181 *q = '\0';
182 PARA_INFO_LOG("%s\n", p);
183 p = q + 1;
184 }
185 snd_output_close(output_log);
186 }
187 return 1;
188 fail:
189 if (ret < 0)
190 PARA_ERROR_LOG("%s: %s\n", msg, snd_strerror(-ret));
191 else
192 PARA_ERROR_LOG("%s\n", msg);
193 return -E_ALSA;
194 }
195
196 static void alsa_write_pre_select(struct sched *s, struct task *t)
197 {
198 struct pollfd pfd;
199 struct writer_node *wn = container_of(t, struct writer_node, task);
200 struct private_alsa_write_data *pad = wn->private_data;
201 int ret = btr_node_status(wn->btrn, wn->min_iqs, BTR_NT_LEAF);
202
203 if (pad)
204 pad->poll_fd = -1;
205 if (ret == 0)
206 return;
207 if (!pad) {
208 sched_min_delay(s);
209 return;
210 }
211 if (ret < 0) {
212 sched_request_barrier_or_min_delay(&pad->drain_barrier, s);
213 return;
214 }
215 ret = snd_pcm_poll_descriptors(pad->handle, &pfd, 1);
216 if (ret < 0) {
217 PARA_ERROR_LOG("could not get alsa poll fd: %s\n",
218 snd_strerror(-ret));
219 return;
220 }
221 pad->poll_fd = pfd.fd;
222 para_fd_set(pfd.fd, &s->rfds, &s->max_fileno);
223 }
224
225 static void alsa_close(struct writer_node *wn)
226 {
227 struct private_alsa_write_data *pad = wn->private_data;
228 PARA_INFO_LOG("closing writer node %p\n", wn);
229
230 if (!pad)
231 return;
232 /*
233 * It's OK to have a blocking operation here because we already made
234 * sure that the PCM output buffer is (nearly) empty.
235 */
236 snd_pcm_nonblock(pad->handle, 0);
237 snd_pcm_drain(pad->handle);
238 snd_pcm_close(pad->handle);
239 snd_config_update_free_global();
240 free(pad);
241 }
242
243 static int alsa_write_post_select(__a_unused struct sched *s,
244 struct task *t)
245 {
246 struct writer_node *wn = container_of(t, struct writer_node, task);
247 struct private_alsa_write_data *pad = wn->private_data;
248 struct btr_node *btrn = wn->btrn;
249 char *data;
250 size_t bytes;
251 snd_pcm_sframes_t frames;
252 int ret;
253
254 ret = task_get_notification(t);
255 if (ret < 0)
256 goto err;
257 again:
258 ret = btr_node_status(btrn, wn->min_iqs, BTR_NT_LEAF);
259 if (ret == 0)
260 return 0;
261 btr_merge(btrn, wn->min_iqs);
262 bytes = btr_next_buffer(btrn, &data);
263 if (ret < 0 || bytes < wn->min_iqs) { /* eof */
264 assert(btr_no_parent(btrn));
265 ret = -E_WRITE_COMMON_EOF;
266 if (!pad)
267 goto err;
268 /* wait until pending frames are played */
269 if (pad->drain_barrier.tv_sec == 0) {
270 PARA_DEBUG_LOG("waiting for device to drain\n");
271 tv_add(now, &(struct timeval)EMBRACE(0, 200 * 1000),
272 &pad->drain_barrier);
273 return 0;
274 }
275 if (tv_diff(now, &pad->drain_barrier, NULL) > 0)
276 goto err;
277 return 0;
278 }
279 if (!pad) {
280 int32_t val;
281
282 if (bytes == 0) /* no data available */
283 return 0;
284 pad = para_calloc(sizeof(*pad));
285 get_btr_sample_rate(btrn, &val);
286 pad->sample_rate = val;
287 get_btr_channels(btrn, &val);
288 pad->channels = val;
289 get_btr_sample_format(btrn, &val);
290 pad->sample_format = get_alsa_pcm_format(val);
291
292 PARA_INFO_LOG("%d channel(s), %dHz\n", pad->channels,
293 pad->sample_rate);
294 ret = alsa_init(pad, wn->conf);
295 if (ret < 0) {
296 free(pad);
297 goto err;
298 }
299 wn->private_data = pad;
300 wn->min_iqs = pad->bytes_per_frame;
301 goto again;
302 }
303 if (pad->poll_fd < 0 || !FD_ISSET(pad->poll_fd, &s->rfds))
304 return 0;
305 frames = bytes / pad->bytes_per_frame;
306 frames = snd_pcm_writei(pad->handle, data, frames);
307 if (frames == 0 || frames == -EAGAIN) {
308 /*
309 * The alsa poll fd was ready for IO but we failed to write to
310 * the alsa handle. This means another event is pending. We
311 * don't care about that but we have to dispatch the event in
312 * order to avoid a busy loop. So we simply read from the poll
313 * fd.
314 */
315 char buf[100];
316 if (read(pad->poll_fd, buf, 100))
317 do_nothing;
318 return 0;
319 }
320 if (frames > 0) {
321 btr_consume(btrn, frames * pad->bytes_per_frame);
322 goto again;
323 }
324 if (frames == -EPIPE) {
325 PARA_WARNING_LOG("underrun (tried to write %zu bytes)\n", bytes);
326 snd_pcm_prepare(pad->handle);
327 return 0;
328 }
329 PARA_ERROR_LOG("alsa write error: %s\n", snd_strerror(-frames));
330 ret = -E_ALSA;
331 err:
332 assert(ret < 0);
333 btr_remove_node(&wn->btrn);
334 return ret;
335 }
336
337 __malloc static void *alsa_parse_config_or_die(int argc, char **argv)
338 {
339 struct alsa_write_args_info *conf = para_calloc(sizeof(*conf));
340
341 /* exits on errors */
342 alsa_write_cmdline_parser(argc, argv, conf);
343 return conf;
344 }
345
346 static void alsa_free_config(void *conf)
347 {
348 alsa_write_cmdline_parser_free(conf);
349 }
350
351 /**
352 * The init function of the alsa writer.
353 *
354 * \param w Pointer to the writer to initialize.
355 *
356 * \sa struct \ref writer.
357 */
358 void alsa_write_init(struct writer *w)
359 {
360 struct alsa_write_args_info dummy;
361
362 alsa_write_cmdline_parser_init(&dummy);
363 w->close = alsa_close;
364 w->pre_select = alsa_write_pre_select;
365 w->post_select = alsa_write_post_select;
366 w->parse_config_or_die = alsa_parse_config_or_die;
367 w->free_config = alsa_free_config;
368 w->help = (struct ggo_help)DEFINE_GGO_HELP(alsa_write);
369 alsa_write_cmdline_parser_free(&dummy);
370 }