chiark / gitweb /
Encourage choose tab's input focus to the search box a bit. Really we
[disorder] / server / speaker-alsa.c
CommitLineData
1c3f1e73 1/*
2 * This file is part of DisOrder
3 * Copyright (C) 2005, 2006, 2007 Richard Kettlewell
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 * USA
19 */
20/** @file server/speaker-alsa.c
21 * @brief Support for @ref BACKEND_ALSA */
22
05b75f8d 23#include "common.h"
1c3f1e73 24
146e86fb 25#if HAVE_ALSA_ASOUNDLIB_H
1c3f1e73 26
1c3f1e73 27#include <unistd.h>
28#include <poll.h>
29#include <alsa/asoundlib.h>
30
31#include "configuration.h"
32#include "syscalls.h"
33#include "log.h"
34#include "speaker-protocol.h"
35#include "speaker.h"
36
37/** @brief The current PCM handle */
38static snd_pcm_t *pcm;
39
40/** @brief Last seen buffer size */
41static snd_pcm_uframes_t last_pcm_bufsize;
42
43/** @brief ALSA backend initialization */
44static void alsa_init(void) {
45 info("selected ALSA backend");
46}
47
48/** @brief Log ALSA parameters */
49static void log_params(snd_pcm_hw_params_t *hwparams,
50 snd_pcm_sw_params_t *swparams) {
51 snd_pcm_uframes_t f;
52 unsigned u;
53
54 return; /* too verbose */
55 if(hwparams) {
56 /* TODO */
57 }
58 if(swparams) {
59 snd_pcm_sw_params_get_silence_size(swparams, &f);
60 info("sw silence_size=%lu", (unsigned long)f);
61 snd_pcm_sw_params_get_silence_threshold(swparams, &f);
62 info("sw silence_threshold=%lu", (unsigned long)f);
63 snd_pcm_sw_params_get_sleep_min(swparams, &u);
64 info("sw sleep_min=%lu", (unsigned long)u);
65 snd_pcm_sw_params_get_start_threshold(swparams, &f);
66 info("sw start_threshold=%lu", (unsigned long)f);
67 snd_pcm_sw_params_get_stop_threshold(swparams, &f);
68 info("sw stop_threshold=%lu", (unsigned long)f);
69 snd_pcm_sw_params_get_xfer_align(swparams, &f);
70 info("sw xfer_align=%lu", (unsigned long)f);
71 }
72}
73
74/** @brief ALSA deactivation */
75static void alsa_deactivate(void) {
76 if(pcm) {
77 int err;
78
79 if((err = snd_pcm_nonblock(pcm, 0)) < 0)
80 fatal(0, "error calling snd_pcm_nonblock: %d", err);
81 D(("draining pcm"));
82 snd_pcm_drain(pcm);
83 D(("closing pcm"));
84 snd_pcm_close(pcm);
85 pcm = 0;
86 device_state = device_closed;
87 D(("released audio device"));
88 }
89}
90
91/** @brief ALSA backend activation */
92static void alsa_activate(void) {
1c3f1e73 93 if(!pcm) {
94 snd_pcm_hw_params_t *hwparams;
95 snd_pcm_sw_params_t *swparams;
96 snd_pcm_uframes_t pcm_bufsize;
97 int err;
98 int sample_format = 0;
99 unsigned rate;
100
101 D(("snd_pcm_open"));
102 if((err = snd_pcm_open(&pcm,
103 config->device,
104 SND_PCM_STREAM_PLAYBACK,
105 SND_PCM_NONBLOCK))) {
106 error(0, "error from snd_pcm_open: %d", err);
107 goto error;
108 }
109 snd_pcm_hw_params_alloca(&hwparams);
110 D(("set up hw params"));
111 if((err = snd_pcm_hw_params_any(pcm, hwparams)) < 0)
112 fatal(0, "error from snd_pcm_hw_params_any: %d", err);
113 if((err = snd_pcm_hw_params_set_access(pcm, hwparams,
114 SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
115 fatal(0, "error from snd_pcm_hw_params_set_access: %d", err);
6d2d327c 116 switch(config->sample_format.bits) {
1c3f1e73 117 case 8:
118 sample_format = SND_PCM_FORMAT_S8;
119 break;
120 case 16:
6d2d327c
RK
121 switch(config->sample_format.endian) {
122 case ENDIAN_LITTLE: sample_format = SND_PCM_FORMAT_S16_LE; break;
123 case ENDIAN_BIG: sample_format = SND_PCM_FORMAT_S16_BE; break;
124 default:
125 error(0, "unrecognized byte format %d", config->sample_format.endian);
1c3f1e73 126 goto fatal;
127 }
128 break;
129 default:
6d2d327c 130 error(0, "unsupported sample size %d", config->sample_format.bits);
1c3f1e73 131 goto fatal;
132 }
133 if((err = snd_pcm_hw_params_set_format(pcm, hwparams,
134 sample_format)) < 0) {
135 error(0, "error from snd_pcm_hw_params_set_format (%d): %d",
136 sample_format, err);
137 goto fatal;
138 }
6d2d327c 139 rate = config->sample_format.rate;
1c3f1e73 140 if((err = snd_pcm_hw_params_set_rate_near(pcm, hwparams, &rate, 0)) < 0) {
141 error(0, "error from snd_pcm_hw_params_set_rate (%d): %d",
6d2d327c 142 config->sample_format.rate, err);
1c3f1e73 143 goto fatal;
144 }
6d2d327c
RK
145 if(rate != (unsigned)config->sample_format.rate)
146 info("want rate %d, got %u", config->sample_format.rate, rate);
147 if((err = snd_pcm_hw_params_set_channels
148 (pcm, hwparams, config->sample_format.channels)) < 0) {
1c3f1e73 149 error(0, "error from snd_pcm_hw_params_set_channels (%d): %d",
6d2d327c 150 config->sample_format.channels, err);
1c3f1e73 151 goto fatal;
152 }
153 pcm_bufsize = 3 * FRAMES;
154 if((err = snd_pcm_hw_params_set_buffer_size_near(pcm, hwparams,
155 &pcm_bufsize)) < 0)
156 fatal(0, "error from snd_pcm_hw_params_set_buffer_size (%d): %d",
157 3 * FRAMES, err);
158 if(pcm_bufsize != 3 * FRAMES && pcm_bufsize != last_pcm_bufsize)
159 info("asked for PCM buffer of %d frames, got %d",
160 3 * FRAMES, (int)pcm_bufsize);
161 last_pcm_bufsize = pcm_bufsize;
162 if((err = snd_pcm_hw_params(pcm, hwparams)) < 0)
163 fatal(0, "error calling snd_pcm_hw_params: %d", err);
164 D(("set up sw params"));
165 snd_pcm_sw_params_alloca(&swparams);
166 if((err = snd_pcm_sw_params_current(pcm, swparams)) < 0)
167 fatal(0, "error calling snd_pcm_sw_params_current: %d", err);
168 if((err = snd_pcm_sw_params_set_avail_min(pcm, swparams, FRAMES)) < 0)
169 fatal(0, "error calling snd_pcm_sw_params_set_avail_min %d: %d",
170 FRAMES, err);
171 if((err = snd_pcm_sw_params(pcm, swparams)) < 0)
172 fatal(0, "error calling snd_pcm_sw_params: %d", err);
1c3f1e73 173 D(("acquired audio device"));
174 log_params(hwparams, swparams);
175 device_state = device_open;
176 }
177 return;
178fatal:
179 abandon();
180error:
181 /* We assume the error is temporary and that we'll retry in a bit. */
182 if(pcm) {
183 snd_pcm_close(pcm);
184 pcm = 0;
185 device_state = device_error;
186 }
187 return;
188}
189
190/** @brief Play via ALSA */
191static size_t alsa_play(size_t frames) {
192 snd_pcm_sframes_t pcm_written_frames;
193 int err;
194
195 pcm_written_frames = snd_pcm_writei(pcm,
196 playing->buffer + playing->start,
197 frames);
198 D(("actually play %zu frames, wrote %d",
199 frames, (int)pcm_written_frames));
200 if(pcm_written_frames < 0) {
201 switch(pcm_written_frames) {
202 case -EPIPE: /* underrun */
203 error(0, "snd_pcm_writei reports underrun");
204 if((err = snd_pcm_prepare(pcm)) < 0)
205 fatal(0, "error calling snd_pcm_prepare: %d", err);
206 return 0;
207 case -EAGAIN:
208 return 0;
209 default:
210 fatal(0, "error calling snd_pcm_writei: %d",
211 (int)pcm_written_frames);
212 }
213 } else
214 return pcm_written_frames;
215}
216
217static int alsa_slots, alsa_nslots = -1;
218
219/** @brief Fill in poll fd array for ALSA */
e84fb5f0 220static void alsa_beforepoll(int attribute((unused)) *timeoutp) {
1c3f1e73 221 /* We send sample data to ALSA as fast as it can accept it, relying on
222 * the fact that it has a relatively small buffer to minimize pause
223 * latency. */
224 int retry = 3, err;
225
226 alsa_slots = fdno;
227 do {
228 retry = 0;
229 alsa_nslots = snd_pcm_poll_descriptors(pcm, &fds[fdno], NFDS - fdno);
230 if((alsa_nslots <= 0
231 || !(fds[alsa_slots].events & POLLOUT))
232 && snd_pcm_state(pcm) == SND_PCM_STATE_XRUN) {
233 error(0, "underrun detected after call to snd_pcm_poll_descriptors()");
234 if((err = snd_pcm_prepare(pcm)))
235 fatal(0, "error calling snd_pcm_prepare: %d", err);
236 } else
237 break;
238 } while(retry-- > 0);
239 if(alsa_nslots >= 0)
240 fdno += alsa_nslots;
241}
242
243/** @brief Process poll() results for ALSA */
244static int alsa_ready(void) {
245 int err;
246
247 unsigned short alsa_revents;
248
249 if((err = snd_pcm_poll_descriptors_revents(pcm,
250 &fds[alsa_slots],
251 alsa_nslots,
252 &alsa_revents)) < 0)
253 fatal(0, "error calling snd_pcm_poll_descriptors_revents: %d", err);
254 if(alsa_revents & (POLLOUT | POLLERR))
255 return 1;
256 else
257 return 0;
258}
259
260const struct speaker_backend alsa_backend = {
261 BACKEND_ALSA,
262 0,
263 alsa_init,
264 alsa_activate,
265 alsa_play,
266 alsa_deactivate,
267 alsa_beforepoll,
268 alsa_ready
269};
270
271#endif
272
273/*
274Local Variables:
275c-basic-offset:2
276comment-column:40
277fill-column:79
278indent-tabs-mode:nil
279End:
280*/