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