2 * This file is part of DisOrder
3 * Copyright (C) 2005, 2006, 2007 Richard Kettlewell
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.
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.
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
20 /** @file server/speaker-alsa.c
21 * @brief Support for @ref BACKEND_ALSA */
25 #if HAVE_ALSA_ASOUNDLIB_H
29 #include <alsa/asoundlib.h>
31 #include "configuration.h"
34 #include "speaker-protocol.h"
37 /** @brief The current PCM handle */
38 static snd_pcm_t
*pcm
;
40 /** @brief Last seen buffer size */
41 static snd_pcm_uframes_t last_pcm_bufsize
;
43 /** @brief ALSA backend initialization */
44 static void alsa_init(void) {
45 info("selected ALSA backend");
48 /** @brief Log ALSA parameters */
49 static void log_params(snd_pcm_hw_params_t
*hwparams
,
50 snd_pcm_sw_params_t
*swparams
) {
53 return; /* too verbose */
58 snd_pcm_sw_params_get_silence_size(swparams
, &f
);
59 info("sw silence_size=%lu", (unsigned long)f
);
60 snd_pcm_sw_params_get_silence_threshold(swparams
, &f
);
61 info("sw silence_threshold=%lu", (unsigned long)f
);
62 #if HAVE_SND_PCM_SW_PARAMS_GET_SLEEP_MIN
66 snd_pcm_sw_params_get_sleep_min(swparams
, &u
);
67 info("sw sleep_min=%lu", (unsigned long)u
);
70 snd_pcm_sw_params_get_start_threshold(swparams
, &f
);
71 info("sw start_threshold=%lu", (unsigned long)f
);
72 snd_pcm_sw_params_get_stop_threshold(swparams
, &f
);
73 info("sw stop_threshold=%lu", (unsigned long)f
);
74 #if HAVE_SND_PCM_SW_PARAMS_GET_XFER_ALIGN
75 snd_pcm_sw_params_get_xfer_align(swparams
, &f
);
76 info("sw xfer_align=%lu", (unsigned long)f
);
81 /** @brief ALSA deactivation */
82 static void alsa_deactivate(void) {
86 if((err
= snd_pcm_nonblock(pcm
, 0)) < 0)
87 fatal(0, "error calling snd_pcm_nonblock: %d", err
);
93 device_state
= device_closed
;
94 D(("released audio device"));
98 /** @brief ALSA backend activation */
99 static void alsa_activate(void) {
101 snd_pcm_hw_params_t
*hwparams
;
102 snd_pcm_sw_params_t
*swparams
;
103 snd_pcm_uframes_t pcm_bufsize
;
105 int sample_format
= 0;
109 if((err
= snd_pcm_open(&pcm
,
111 SND_PCM_STREAM_PLAYBACK
,
112 SND_PCM_NONBLOCK
))) {
113 error(0, "error from snd_pcm_open: %d", err
);
116 snd_pcm_hw_params_alloca(&hwparams
);
117 D(("set up hw params"));
118 if((err
= snd_pcm_hw_params_any(pcm
, hwparams
)) < 0)
119 fatal(0, "error from snd_pcm_hw_params_any: %d", err
);
120 if((err
= snd_pcm_hw_params_set_access(pcm
, hwparams
,
121 SND_PCM_ACCESS_RW_INTERLEAVED
)) < 0)
122 fatal(0, "error from snd_pcm_hw_params_set_access: %d", err
);
123 switch(config
->sample_format
.bits
) {
125 sample_format
= SND_PCM_FORMAT_S8
;
128 switch(config
->sample_format
.endian
) {
129 case ENDIAN_LITTLE
: sample_format
= SND_PCM_FORMAT_S16_LE
; break;
130 case ENDIAN_BIG
: sample_format
= SND_PCM_FORMAT_S16_BE
; break;
132 error(0, "unrecognized byte format %d", config
->sample_format
.endian
);
137 error(0, "unsupported sample size %d", config
->sample_format
.bits
);
140 if((err
= snd_pcm_hw_params_set_format(pcm
, hwparams
,
141 sample_format
)) < 0) {
142 error(0, "error from snd_pcm_hw_params_set_format (%d): %d",
146 rate
= config
->sample_format
.rate
;
147 if((err
= snd_pcm_hw_params_set_rate_near(pcm
, hwparams
, &rate
, 0)) < 0) {
148 error(0, "error from snd_pcm_hw_params_set_rate (%d): %d",
149 config
->sample_format
.rate
, err
);
152 if(rate
!= (unsigned)config
->sample_format
.rate
)
153 info("want rate %d, got %u", config
->sample_format
.rate
, rate
);
154 if((err
= snd_pcm_hw_params_set_channels
155 (pcm
, hwparams
, config
->sample_format
.channels
)) < 0) {
156 error(0, "error from snd_pcm_hw_params_set_channels (%d): %d",
157 config
->sample_format
.channels
, err
);
160 pcm_bufsize
= 3 * FRAMES
;
161 if((err
= snd_pcm_hw_params_set_buffer_size_near(pcm
, hwparams
,
163 fatal(0, "error from snd_pcm_hw_params_set_buffer_size (%d): %d",
165 if(pcm_bufsize
!= 3 * FRAMES
&& pcm_bufsize
!= last_pcm_bufsize
)
166 info("asked for PCM buffer of %d frames, got %d",
167 3 * FRAMES
, (int)pcm_bufsize
);
168 last_pcm_bufsize
= pcm_bufsize
;
169 if((err
= snd_pcm_hw_params(pcm
, hwparams
)) < 0)
170 fatal(0, "error calling snd_pcm_hw_params: %d", err
);
171 D(("set up sw params"));
172 snd_pcm_sw_params_alloca(&swparams
);
173 if((err
= snd_pcm_sw_params_current(pcm
, swparams
)) < 0)
174 fatal(0, "error calling snd_pcm_sw_params_current: %d", err
);
175 if((err
= snd_pcm_sw_params_set_avail_min(pcm
, swparams
, FRAMES
)) < 0)
176 fatal(0, "error calling snd_pcm_sw_params_set_avail_min %d: %d",
178 if((err
= snd_pcm_sw_params(pcm
, swparams
)) < 0)
179 fatal(0, "error calling snd_pcm_sw_params: %d", err
);
180 D(("acquired audio device"));
181 log_params(hwparams
, swparams
);
182 device_state
= device_open
;
188 /* We assume the error is temporary and that we'll retry in a bit. */
192 device_state
= device_error
;
197 /** @brief Play via ALSA */
198 static size_t alsa_play(size_t frames
) {
199 snd_pcm_sframes_t pcm_written_frames
;
202 pcm_written_frames
= snd_pcm_writei(pcm
,
203 playing
->buffer
+ playing
->start
,
205 D(("actually play %zu frames, wrote %d",
206 frames
, (int)pcm_written_frames
));
207 if(pcm_written_frames
< 0) {
208 switch(pcm_written_frames
) {
209 case -EPIPE
: /* underrun */
210 error(0, "snd_pcm_writei reports underrun");
211 if((err
= snd_pcm_prepare(pcm
)) < 0)
212 fatal(0, "error calling snd_pcm_prepare: %d", err
);
217 fatal(0, "error calling snd_pcm_writei: %d",
218 (int)pcm_written_frames
);
221 return pcm_written_frames
;
224 static int alsa_slots
, alsa_nslots
= -1;
226 /** @brief Fill in poll fd array for ALSA */
227 static void alsa_beforepoll(int attribute((unused
)) *timeoutp
) {
228 /* We send sample data to ALSA as fast as it can accept it, relying on
229 * the fact that it has a relatively small buffer to minimize pause
236 alsa_nslots
= snd_pcm_poll_descriptors(pcm
, &fds
[fdno
], NFDS
- fdno
);
238 || !(fds
[alsa_slots
].events
& POLLOUT
))
239 && snd_pcm_state(pcm
) == SND_PCM_STATE_XRUN
) {
240 error(0, "underrun detected after call to snd_pcm_poll_descriptors()");
241 if((err
= snd_pcm_prepare(pcm
)))
242 fatal(0, "error calling snd_pcm_prepare: %d", err
);
245 } while(retry
-- > 0);
250 /** @brief Process poll() results for ALSA */
251 static int alsa_ready(void) {
254 unsigned short alsa_revents
;
256 if((err
= snd_pcm_poll_descriptors_revents(pcm
,
260 fatal(0, "error calling snd_pcm_poll_descriptors_revents: %d", err
);
261 if(alsa_revents
& (POLLOUT
| POLLERR
))
267 const struct speaker_backend alsa_backend
= {