chiark / gitweb /
Don't use deprecated ALSA calls (since they generate warnings, which
[disorder] / server / speaker-alsa.c
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
23 #include "common.h"
24
25 #if HAVE_ALSA_ASOUNDLIB_H
26
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 */
38 static snd_pcm_t *pcm;
39
40 /** @brief Last seen buffer size */
41 static snd_pcm_uframes_t last_pcm_bufsize;
42
43 /** @brief ALSA backend initialization */
44 static void alsa_init(void) {
45   info("selected ALSA backend");
46 }
47
48 /** @brief Log ALSA parameters */
49 static 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 #if HAVE_SND_PCM_SW_PARAMS_GET_SLEEP_MIN
64     snd_pcm_sw_params_get_sleep_min(swparams, &u);
65     info("sw sleep_min=%lu", (unsigned long)u);
66 #endif
67     snd_pcm_sw_params_get_start_threshold(swparams, &f);
68     info("sw start_threshold=%lu", (unsigned long)f);
69     snd_pcm_sw_params_get_stop_threshold(swparams, &f);
70     info("sw stop_threshold=%lu", (unsigned long)f);
71 #if HAVE_SND_PCM_SW_PARAMS_GET_XFER_ALIGN
72     snd_pcm_sw_params_get_xfer_align(swparams, &f);
73     info("sw xfer_align=%lu", (unsigned long)f);
74 #endif
75   }
76 }
77
78 /** @brief ALSA deactivation */
79 static void alsa_deactivate(void) {
80   if(pcm) {
81     int err;
82     
83     if((err = snd_pcm_nonblock(pcm, 0)) < 0)
84       fatal(0, "error calling snd_pcm_nonblock: %d", err);
85     D(("draining pcm"));
86     snd_pcm_drain(pcm);
87     D(("closing pcm"));
88     snd_pcm_close(pcm);
89     pcm = 0;
90     device_state = device_closed;
91     D(("released audio device"));
92   }
93 }
94
95 /** @brief ALSA backend activation */
96 static void alsa_activate(void) {
97   if(!pcm) {
98     snd_pcm_hw_params_t *hwparams;
99     snd_pcm_sw_params_t *swparams;
100     snd_pcm_uframes_t pcm_bufsize;
101     int err;
102     int sample_format = 0;
103     unsigned rate;
104
105     D(("snd_pcm_open"));
106     if((err = snd_pcm_open(&pcm,
107                            config->device,
108                            SND_PCM_STREAM_PLAYBACK,
109                            SND_PCM_NONBLOCK))) {
110       error(0, "error from snd_pcm_open: %d", err);
111       goto error;
112     }
113     snd_pcm_hw_params_alloca(&hwparams);
114     D(("set up hw params"));
115     if((err = snd_pcm_hw_params_any(pcm, hwparams)) < 0)
116       fatal(0, "error from snd_pcm_hw_params_any: %d", err);
117     if((err = snd_pcm_hw_params_set_access(pcm, hwparams,
118                                            SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
119       fatal(0, "error from snd_pcm_hw_params_set_access: %d", err);
120     switch(config->sample_format.bits) {
121     case 8:
122       sample_format = SND_PCM_FORMAT_S8;
123       break;
124     case 16:
125       switch(config->sample_format.endian) {
126       case ENDIAN_LITTLE: sample_format = SND_PCM_FORMAT_S16_LE; break;
127       case ENDIAN_BIG: sample_format = SND_PCM_FORMAT_S16_BE; break;
128       default:
129         error(0, "unrecognized byte format %d", config->sample_format.endian);
130         goto fatal;
131       }
132       break;
133     default:
134       error(0, "unsupported sample size %d", config->sample_format.bits);
135       goto fatal;
136     }
137     if((err = snd_pcm_hw_params_set_format(pcm, hwparams,
138                                            sample_format)) < 0) {
139       error(0, "error from snd_pcm_hw_params_set_format (%d): %d",
140             sample_format, err);
141       goto fatal;
142     }
143     rate = config->sample_format.rate;
144     if((err = snd_pcm_hw_params_set_rate_near(pcm, hwparams, &rate, 0)) < 0) {
145       error(0, "error from snd_pcm_hw_params_set_rate (%d): %d",
146             config->sample_format.rate, err);
147       goto fatal;
148     }
149     if(rate != (unsigned)config->sample_format.rate)
150       info("want rate %d, got %u", config->sample_format.rate, rate);
151     if((err = snd_pcm_hw_params_set_channels
152         (pcm, hwparams, config->sample_format.channels)) < 0) {
153       error(0, "error from snd_pcm_hw_params_set_channels (%d): %d",
154             config->sample_format.channels, err);
155       goto fatal;
156     }
157     pcm_bufsize = 3 * FRAMES;
158     if((err = snd_pcm_hw_params_set_buffer_size_near(pcm, hwparams,
159                                                      &pcm_bufsize)) < 0)
160       fatal(0, "error from snd_pcm_hw_params_set_buffer_size (%d): %d",
161             3 * FRAMES, err);
162     if(pcm_bufsize != 3 * FRAMES && pcm_bufsize != last_pcm_bufsize)
163       info("asked for PCM buffer of %d frames, got %d",
164            3 * FRAMES, (int)pcm_bufsize);
165     last_pcm_bufsize = pcm_bufsize;
166     if((err = snd_pcm_hw_params(pcm, hwparams)) < 0)
167       fatal(0, "error calling snd_pcm_hw_params: %d", err);
168     D(("set up sw params"));
169     snd_pcm_sw_params_alloca(&swparams);
170     if((err = snd_pcm_sw_params_current(pcm, swparams)) < 0)
171       fatal(0, "error calling snd_pcm_sw_params_current: %d", err);
172     if((err = snd_pcm_sw_params_set_avail_min(pcm, swparams, FRAMES)) < 0)
173       fatal(0, "error calling snd_pcm_sw_params_set_avail_min %d: %d",
174             FRAMES, err);
175     if((err = snd_pcm_sw_params(pcm, swparams)) < 0)
176       fatal(0, "error calling snd_pcm_sw_params: %d", err);
177     D(("acquired audio device"));
178     log_params(hwparams, swparams);
179     device_state = device_open;
180   }
181   return;
182 fatal:
183   abandon();
184 error:
185   /* We assume the error is temporary and that we'll retry in a bit. */
186   if(pcm) {
187     snd_pcm_close(pcm);
188     pcm = 0;
189     device_state = device_error;
190   }
191   return;
192 }
193
194 /** @brief Play via ALSA */
195 static size_t alsa_play(size_t frames) {
196   snd_pcm_sframes_t pcm_written_frames;
197   int err;
198   
199   pcm_written_frames = snd_pcm_writei(pcm,
200                                       playing->buffer + playing->start,
201                                       frames);
202   D(("actually play %zu frames, wrote %d",
203      frames, (int)pcm_written_frames));
204   if(pcm_written_frames < 0) {
205     switch(pcm_written_frames) {
206     case -EPIPE:                        /* underrun */
207       error(0, "snd_pcm_writei reports underrun");
208       if((err = snd_pcm_prepare(pcm)) < 0)
209         fatal(0, "error calling snd_pcm_prepare: %d", err);
210       return 0;
211     case -EAGAIN:
212       return 0;
213     default:
214       fatal(0, "error calling snd_pcm_writei: %d",
215             (int)pcm_written_frames);
216     }
217   } else
218     return pcm_written_frames;
219 }
220
221 static int alsa_slots, alsa_nslots = -1;
222
223 /** @brief Fill in poll fd array for ALSA */
224 static void alsa_beforepoll(int attribute((unused)) *timeoutp) {
225   /* We send sample data to ALSA as fast as it can accept it, relying on
226    * the fact that it has a relatively small buffer to minimize pause
227    * latency. */
228   int retry = 3, err;
229   
230   alsa_slots = fdno;
231   do {
232     retry = 0;
233     alsa_nslots = snd_pcm_poll_descriptors(pcm, &fds[fdno], NFDS - fdno);
234     if((alsa_nslots <= 0
235         || !(fds[alsa_slots].events & POLLOUT))
236        && snd_pcm_state(pcm) == SND_PCM_STATE_XRUN) {
237       error(0, "underrun detected after call to snd_pcm_poll_descriptors()");
238       if((err = snd_pcm_prepare(pcm)))
239         fatal(0, "error calling snd_pcm_prepare: %d", err);
240     } else
241       break;
242   } while(retry-- > 0);
243   if(alsa_nslots >= 0)
244     fdno += alsa_nslots;
245 }
246
247 /** @brief Process poll() results for ALSA */
248 static int alsa_ready(void) {
249   int err;
250
251   unsigned short alsa_revents;
252   
253   if((err = snd_pcm_poll_descriptors_revents(pcm,
254                                              &fds[alsa_slots],
255                                              alsa_nslots,
256                                              &alsa_revents)) < 0)
257     fatal(0, "error calling snd_pcm_poll_descriptors_revents: %d", err);
258   if(alsa_revents & (POLLOUT | POLLERR))
259     return 1;
260   else
261     return 0;
262 }
263
264 const struct speaker_backend alsa_backend = {
265   BACKEND_ALSA,
266   0,
267   alsa_init,
268   alsa_activate,
269   alsa_play,
270   alsa_deactivate,
271   alsa_beforepoll,
272   alsa_ready
273 };
274
275 #endif
276
277 /*
278 Local Variables:
279 c-basic-offset:2
280 comment-column:40
281 fill-column:79
282 indent-tabs-mode:nil
283 End:
284 */