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