Debian Codegraph @

src/libout123/modules/alsa.c

raw
1 /*
2 alsa: sound output with Advanced Linux Sound Architecture 1.x API
3
4 copyright 2006-2020 by the mpg123 project - free software under the terms of the LGPL 2.1
5 see COPYING and AUTHORS files in distribution or http://mpg123.org
6
7 initially written by Clemens Ladisch <[email protected]>
8 */
9
10 /* ALSA headers define struct timeval if no POSIX macro is set,
11 nicely in conflict with definitions in system headers. They had
12 a discussion about that a long time ago:
13 http://mailman.alsa-project.org/pipermail/alsa-devel/2007-June/001684.html
14 ... seems like the conclusion was not carried through.
15 */
16 #define _POSIX_SOURCE
17 /* Things are still missing if _DEFAULT_SOURCE is not defined (for recent
18 glibc, I presume. */
19 #define _DEFAULT_SOURCE
20 #include "../out123_int.h"
21 #include <errno.h>
22
23 /* make ALSA 0.9.x compatible to the 1.0.x API */
24 #define ALSA_PCM_NEW_HW_PARAMS_API
25 #define ALSA_PCM_NEW_SW_PARAMS_API
26
27 #include <alloca.h> /* GCC complains about missing declaration of alloca. */
28 #include <alsa/asoundlib.h>
29
30 #include "../../common/debug.h"
31
32 /* Total buffer size in seconds, 0.2 is more true to what ALSA maximally uses
33 here (8192 samples). The earlier default of 0.5 was never true. */
34 #define BUFFER_LENGTH (ao->device_buffer > 0. ? ao->device_buffer : 0.2)
35
36 static const struct {
37 snd_pcm_format_t alsa;
38 int mpg123;
39 } format_map[] = {
40 { SND_PCM_FORMAT_S16, MPG123_ENC_SIGNED_16 },
41 { SND_PCM_FORMAT_U16, MPG123_ENC_UNSIGNED_16 },
42 { SND_PCM_FORMAT_U8, MPG123_ENC_UNSIGNED_8 },
43 { SND_PCM_FORMAT_S8, MPG123_ENC_SIGNED_8 },
44 { SND_PCM_FORMAT_A_LAW, MPG123_ENC_ALAW_8 },
45 { SND_PCM_FORMAT_MU_LAW, MPG123_ENC_ULAW_8 },
46 { SND_PCM_FORMAT_S32, MPG123_ENC_SIGNED_32 },
47 { SND_PCM_FORMAT_U32, MPG123_ENC_UNSIGNED_32 },
48 #ifdef WORDS_BIGENDIAN
49 { SND_PCM_FORMAT_S24_3BE, MPG123_ENC_SIGNED_24 },
50 { SND_PCM_FORMAT_U24_3BE, MPG123_ENC_UNSIGNED_24 },
51 #else
52 { SND_PCM_FORMAT_S24_3LE, MPG123_ENC_SIGNED_24 },
53 { SND_PCM_FORMAT_U24_3LE, MPG123_ENC_UNSIGNED_24 },
54 #endif
55 { SND_PCM_FORMAT_FLOAT, MPG123_ENC_FLOAT_32 },
56 { SND_PCM_FORMAT_FLOAT64, MPG123_ENC_FLOAT_64 }
57 };
58 #define NUM_FORMATS (sizeof format_map / sizeof format_map[0])
59
60
61 static int rates_match(long int desired, unsigned int actual)
62 {
63 return actual * 100 > desired * (100 - AUDIO_RATE_TOLERANCE) &&
64 actual * 100 < desired * (100 + AUDIO_RATE_TOLERANCE);
65 }
66
67 static int initialize_device(out123_handle *ao)
68 {
69 snd_pcm_hw_params_t *hw=NULL;
70 snd_pcm_sw_params_t *sw=NULL;
71 snd_pcm_uframes_t buffer_size;
72 snd_pcm_uframes_t period_size;
73 snd_pcm_format_t format;
74 snd_pcm_t *pcm=(snd_pcm_t*)ao->userptr;
75 unsigned int rate;
76 int i;
77
78 snd_pcm_hw_params_alloca(&hw); /* Ignore GCC warning here... alsa-lib>=1.0.16 doesn't trigger that anymore, too. */
79 if (snd_pcm_hw_params_any(pcm, hw) < 0) {
80 if(!AOQUIET) error("initialize_device(): no configuration available");
81 return -1;
82 }
83 if (snd_pcm_hw_params_set_access(pcm, hw, SND_PCM_ACCESS_RW_INTERLEAVED) < 0) {
84 if(!AOQUIET) error("initialize_device(): device does not support interleaved access");
85 return -1;
86 }
87 format = SND_PCM_FORMAT_UNKNOWN;
88 for (i = 0; i < NUM_FORMATS; ++i) {
89 if (ao->format == format_map[i].mpg123) {
90 format = format_map[i].alsa;
91 break;
92 }
93 }
94 if (format == SND_PCM_FORMAT_UNKNOWN) {
95 if(!AOQUIET) error1("initialize_device(): invalid sample format %d", ao->format);
96 errno = EINVAL;
97 return -1;
98 }
99 if (snd_pcm_hw_params_set_format(pcm, hw, format) < 0) {
100 if(!AOQUIET) error1("initialize_device(): cannot set format %s", snd_pcm_format_name(format));
101 return -1;
102 }
103 if (snd_pcm_hw_params_set_channels(pcm, hw, ao->channels) < 0) {
104 if(!AOQUIET) error1("initialize_device(): cannot set %d channels", ao->channels);
105 return -1;
106 }
107 rate = ao->rate;
108 if (snd_pcm_hw_params_set_rate_near(pcm, hw, &rate, NULL) < 0) {
109 if(!AOQUIET) error1("initialize_device(): cannot set rate %u", rate);
110 return -1;
111 }
112 if (!rates_match(ao->rate, rate)) {
113 if(!AOQUIET) error2("initialize_device(): rate %ld not available, using %u", ao->rate, rate);
114 /* return -1; */
115 }
116 buffer_size = rate * BUFFER_LENGTH;
117 if (snd_pcm_hw_params_set_buffer_size_near(pcm, hw, &buffer_size) < 0) {
118 if(!AOQUIET) error("initialize_device(): cannot set buffer size");
119 return -1;
120 }
121 debug1("buffer_size=%lu", (unsigned long)buffer_size);
122 period_size = buffer_size / 3; /* 3 periods is so much more common. */
123 if (snd_pcm_hw_params_set_period_size_near(pcm, hw, &period_size, NULL) < 0) {
124 if(!AOQUIET) error("initialize_device(): cannot set period size");
125 return -1;
126 }
127 debug1("period_size=%lu", (unsigned long)period_size);
128 if (snd_pcm_hw_params(pcm, hw) < 0) {
129 if(!AOQUIET) error("initialize_device(): cannot set hw params");
130 return -1;
131 }
132
133 snd_pcm_sw_params_alloca(&sw);
134 if (snd_pcm_sw_params_current(pcm, sw) < 0) {
135 if(!AOQUIET) error("initialize_device(): cannot get sw params");
136 return -1;
137 }
138 /* Start playing once we got at least full period ... this is not default? */
139 if (snd_pcm_sw_params_set_start_threshold(pcm, sw, buffer_size/2) < 0) {
140 if(!AOQUIET) error("initialize_device(): cannot set start threshold");
141 return -1;
142 }
143 /* wake up on every interrupt */
144 if (snd_pcm_sw_params_set_avail_min(pcm, sw, 1) < 0) {
145 if(!AOQUIET) error("initialize_device(): cannot set min available");
146 return -1;
147 }
148 #if SND_LIB_VERSION < ((1<<16)|16)
149 /* Always write as many frames as possible (deprecated since alsa-lib 1.0.16) */
150 if (snd_pcm_sw_params_set_xfer_align(pcm, sw, 1) < 0) {
151 if(!AOQUIET) error("initialize_device(): cannot set transfer alignment");
152 return -1;
153 }
154 #endif
155 if (snd_pcm_sw_params(pcm, sw) < 0) {
156 if(!AOQUIET) error("initialize_device(): cannot set sw params");
157 return -1;
158 }
159 return 0;
160 }
161
162 #ifndef DEBUG
163 static void error_ignorer(const char *file, int line, const char *function, int err, const char *fmt,...)
164 {
165 /* I can make ALSA silent. */
166 }
167 #endif
168
169 static int open_alsa(out123_handle *ao)
170 {
171 const char *pcm_name;
172 snd_pcm_t *pcm=NULL;
173 debug1("open_alsa with %p", ao->userptr);
174
175 #ifndef DEBUG
176 if(AOQUIET) snd_lib_error_set_handler(error_ignorer);
177 #endif
178
179 pcm_name = ao->device ? ao->device : "default";
180 if (snd_pcm_open(&pcm, pcm_name, SND_PCM_STREAM_PLAYBACK, 0) < 0) {
181 if(!AOQUIET) error1("cannot open device %s", pcm_name);
182 return -1;
183 }
184 ao->userptr = pcm;
185 if (ao->format != -1) {
186 /* we're going to play: initalize sample format */
187 return initialize_device(ao);
188 } else {
189 /* query mode; sample format will be set for each query */
190 return 0;
191 }
192 }
193
194
195 static int get_formats_alsa(out123_handle *ao)
196 {
197 snd_pcm_t *pcm=(snd_pcm_t*)ao->userptr;
198 snd_pcm_hw_params_t *hw;
199 unsigned int rate;
200 int supported_formats, i;
201
202 snd_pcm_hw_params_alloca(&hw);
203 if (snd_pcm_hw_params_any(pcm, hw) < 0) {
204 if(!AOQUIET) error("get_formats_alsa(): no configuration available");
205 return -1;
206 }
207 if (snd_pcm_hw_params_set_access(pcm, hw, SND_PCM_ACCESS_RW_INTERLEAVED) < 0)
208 return -1;
209 if (snd_pcm_hw_params_set_channels(pcm, hw, ao->channels) < 0)
210 return 0;
211 rate = ao->rate;
212 if (snd_pcm_hw_params_set_rate_near(pcm, hw, &rate, NULL) < 0)
213 return -1;
214 if (!rates_match(ao->rate, rate))
215 return 0;
216 supported_formats = 0;
217 for (i = 0; i < NUM_FORMATS; ++i) {
218 if (snd_pcm_hw_params_test_format(pcm, hw, format_map[i].alsa) == 0)
219 supported_formats |= format_map[i].mpg123;
220 }
221 return supported_formats;
222 }
223
224 static int write_alsa(out123_handle *ao, unsigned char *buf, int bytes)
225 {
226 snd_pcm_t *pcm=(snd_pcm_t*)ao->userptr;
227 snd_pcm_uframes_t frames;
228 snd_pcm_sframes_t written;
229
230 frames = snd_pcm_bytes_to_frames(pcm, bytes);
231 while
232 ( /* Try to write, recover if error, try again if recovery successful. */
233 (written = snd_pcm_writei(pcm, buf, frames)) < 0
234 && snd_pcm_recover(pcm, (int)written, 0) == 0
235 )
236 {
237 debug2("recovered from alsa issue %i while trying to write %lu frames", (int)written, (unsigned long)frames);
238 }
239 if(written < 0)
240 {
241 if(!AOQUIET)
242 error1("Fatal problem with alsa output, error %i.", (int)written);
243 return -1;
244 }
245 else return snd_pcm_frames_to_bytes(pcm, written);
246 }
247
248 static void flush_alsa(out123_handle *ao)
249 {
250 snd_pcm_t *pcm=(snd_pcm_t*)ao->userptr;
251
252 /* is this the optimal solution? - we should figure out what we really whant from this function */
253
254 debug("alsa drop");
255 snd_pcm_drop(pcm);
256 debug("alsa prepare");
257 snd_pcm_prepare(pcm);
258 debug("alsa flush done");
259 }
260
261 static void drain_alsa(out123_handle *ao)
262 {
263 snd_pcm_t *pcm=(snd_pcm_t*)ao->userptr;
264 debug1("drain_alsa with %p", ao->userptr);
265 snd_pcm_drain(pcm);
266 }
267
268 static int close_alsa(out123_handle *ao)
269 {
270 snd_pcm_t *pcm=(snd_pcm_t*)ao->userptr;
271 debug1("close_alsa with %p", ao->userptr);
272 if(pcm != NULL) /* be really generous for being called without any device opening */
273 {
274 ao->userptr = NULL; /* Should alsa do this or the module wrapper? */
275 return snd_pcm_close(pcm);
276 }
277 else return 0;
278 }
279
280 static int enumerate_alsa( out123_handle *ao, int (*store_device)(void *devlist
281 , const char *name, const char *description), void *devlist )
282 {
283 void ** hints;
284 int ret = snd_device_name_hint(-1, "pcm", &hints);
285 if(ret)
286 {
287 if(!AOQUIET)
288 merror("ALSA device listing failed with code %d.", ret);
289 return -1;
290 }
291 void ** hint = hints;
292 while(!ret && *hint)
293 {
294 char *io = snd_device_name_get_hint(*hint, "IOID");
295 if((io == NULL || !strcmp("Output", io)))
296 {
297 char *name = snd_device_name_get_hint(*hint, "NAME");
298 char *desc = snd_device_name_get_hint(*hint, "DESC");
299 ret = store_device(devlist, name, desc);
300 free(name);
301 free(desc);
302 }
303 free(io);
304 ++hint;
305 }
306 snd_device_name_free_hint(hints);
307 return 0;
308 }
309
310 static int init_alsa(out123_handle* ao)
311 {
312 if (ao==NULL) return -1;
313
314 /* Set callbacks */
315 ao->open = open_alsa;
316 ao->flush = flush_alsa;
317 ao->drain = drain_alsa;
318 ao->write = write_alsa;
319 ao->get_formats = get_formats_alsa;
320 ao->close = close_alsa;
321 ao->enumerate = enumerate_alsa;
322
323 /* Success */
324 return 0;
325 }
326 /*
327 Module information data structure
328 */
329 mpg123_module_t mpg123_output_module_info = {
330 /* api_version */ MPG123_MODULE_API_VERSION,
331 /* name */ "alsa",
332 /* description */ "Output audio using Advanced Linux Sound Architecture (ALSA).",
333 /* revision */ "$Rev:$",
334 /* handle */ NULL,
335
336 /* init_output */ init_alsa,
337 };
338