]> code.delx.au - pulseaudio/blob - src/modules/alsa-util.c
remove all occurences of
[pulseaudio] / src / modules / alsa-util.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
10
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <sys/types.h>
27 #include <asoundlib.h>
28
29 #include <pulse/sample.h>
30 #include <pulse/xmalloc.h>
31
32 #include <pulsecore/log.h>
33
34 #include "alsa-util.h"
35
36 struct pa_alsa_fdlist {
37 int num_fds;
38 struct pollfd *fds;
39 /* This is a temporary buffer used to avoid lots of mallocs */
40 struct pollfd *work_fds;
41
42 snd_pcm_t *pcm;
43 snd_mixer_t *mixer;
44
45 pa_mainloop_api *m;
46 pa_defer_event *defer;
47 pa_io_event **ios;
48
49 int polled;
50
51 void (*cb)(void *userdata);
52 void *userdata;
53 };
54
55 static void io_cb(pa_mainloop_api*a, pa_io_event* e, PA_GCC_UNUSED int fd, pa_io_event_flags_t events, void *userdata) {
56 struct pa_alsa_fdlist *fdl = (struct pa_alsa_fdlist*)userdata;
57 int err, i;
58 unsigned short revents;
59
60 assert(a && fdl && (fdl->pcm || fdl->mixer) && fdl->fds && fdl->work_fds);
61
62 if (fdl->polled)
63 return;
64
65 fdl->polled = 1;
66
67 memcpy(fdl->work_fds, fdl->fds, sizeof(struct pollfd) * fdl->num_fds);
68
69 for (i = 0;i < fdl->num_fds;i++) {
70 if (e == fdl->ios[i]) {
71 if (events & PA_IO_EVENT_INPUT)
72 fdl->work_fds[i].revents |= POLLIN;
73 if (events & PA_IO_EVENT_OUTPUT)
74 fdl->work_fds[i].revents |= POLLOUT;
75 if (events & PA_IO_EVENT_ERROR)
76 fdl->work_fds[i].revents |= POLLERR;
77 if (events & PA_IO_EVENT_HANGUP)
78 fdl->work_fds[i].revents |= POLLHUP;
79 break;
80 }
81 }
82
83 assert(i != fdl->num_fds);
84
85 if (fdl->pcm)
86 err = snd_pcm_poll_descriptors_revents(fdl->pcm, fdl->work_fds, fdl->num_fds, &revents);
87 else
88 err = snd_mixer_poll_descriptors_revents(fdl->mixer, fdl->work_fds, fdl->num_fds, &revents);
89
90 if (err < 0) {
91 pa_log_error("Unable to get poll revent: %s",
92 snd_strerror(err));
93 return;
94 }
95
96 a->defer_enable(fdl->defer, 1);
97
98 if (revents) {
99 if (fdl->pcm)
100 fdl->cb(fdl->userdata);
101 else
102 snd_mixer_handle_events(fdl->mixer);
103 }
104 }
105
106 static void defer_cb(pa_mainloop_api*a, PA_GCC_UNUSED pa_defer_event* e, void *userdata) {
107 struct pa_alsa_fdlist *fdl = (struct pa_alsa_fdlist*)userdata;
108 int num_fds, i, err;
109 struct pollfd *temp;
110
111 assert(a && fdl && (fdl->pcm || fdl->mixer));
112
113 a->defer_enable(fdl->defer, 0);
114
115 if (fdl->pcm)
116 num_fds = snd_pcm_poll_descriptors_count(fdl->pcm);
117 else
118 num_fds = snd_mixer_poll_descriptors_count(fdl->mixer);
119 assert(num_fds > 0);
120
121 if (num_fds != fdl->num_fds) {
122 if (fdl->fds)
123 pa_xfree(fdl->fds);
124 if (fdl->work_fds)
125 pa_xfree(fdl->work_fds);
126 fdl->fds = pa_xmalloc0(sizeof(struct pollfd) * num_fds);
127 fdl->work_fds = pa_xmalloc(sizeof(struct pollfd) * num_fds);
128 }
129
130 memset(fdl->work_fds, 0, sizeof(struct pollfd) * num_fds);
131
132 if (fdl->pcm)
133 err = snd_pcm_poll_descriptors(fdl->pcm, fdl->work_fds, num_fds);
134 else
135 err = snd_mixer_poll_descriptors(fdl->mixer, fdl->work_fds, num_fds);
136
137 if (err < 0) {
138 pa_log_error("Unable to get poll descriptors: %s",
139 snd_strerror(err));
140 return;
141 }
142
143 fdl->polled = 0;
144
145 if (memcmp(fdl->fds, fdl->work_fds, sizeof(struct pollfd) * num_fds) == 0)
146 return;
147
148 if (fdl->ios) {
149 for (i = 0;i < fdl->num_fds;i++)
150 a->io_free(fdl->ios[i]);
151 if (num_fds != fdl->num_fds) {
152 pa_xfree(fdl->ios);
153 fdl->ios = pa_xmalloc(sizeof(pa_io_event*) * num_fds);
154 assert(fdl->ios);
155 }
156 } else {
157 fdl->ios = pa_xmalloc(sizeof(pa_io_event*) * num_fds);
158 assert(fdl->ios);
159 }
160
161 /* Swap pointers */
162 temp = fdl->work_fds;
163 fdl->work_fds = fdl->fds;
164 fdl->fds = temp;
165
166 fdl->num_fds = num_fds;
167
168 for (i = 0;i < num_fds;i++) {
169 fdl->ios[i] = a->io_new(a, fdl->fds[i].fd,
170 ((fdl->fds[i].events & POLLIN) ? PA_IO_EVENT_INPUT : 0) |
171 ((fdl->fds[i].events & POLLOUT) ? PA_IO_EVENT_OUTPUT : 0),
172 io_cb, fdl);
173 assert(fdl->ios[i]);
174 }
175 }
176
177 struct pa_alsa_fdlist *pa_alsa_fdlist_new(void) {
178 struct pa_alsa_fdlist *fdl;
179
180 fdl = pa_xmalloc(sizeof(struct pa_alsa_fdlist));
181
182 fdl->num_fds = 0;
183 fdl->fds = NULL;
184 fdl->work_fds = NULL;
185
186 fdl->pcm = NULL;
187 fdl->mixer = NULL;
188
189 fdl->m = NULL;
190 fdl->defer = NULL;
191 fdl->ios = NULL;
192
193 fdl->polled = 0;
194
195 return fdl;
196 }
197
198 void pa_alsa_fdlist_free(struct pa_alsa_fdlist *fdl) {
199 assert(fdl);
200
201 if (fdl->defer) {
202 assert(fdl->m);
203 fdl->m->defer_free(fdl->defer);
204 }
205
206 if (fdl->ios) {
207 int i;
208 assert(fdl->m);
209 for (i = 0;i < fdl->num_fds;i++)
210 fdl->m->io_free(fdl->ios[i]);
211 pa_xfree(fdl->ios);
212 }
213
214 if (fdl->fds)
215 pa_xfree(fdl->fds);
216 if (fdl->work_fds)
217 pa_xfree(fdl->work_fds);
218
219 pa_xfree(fdl);
220 }
221
222 int pa_alsa_fdlist_init_pcm(struct pa_alsa_fdlist *fdl, snd_pcm_t *pcm_handle, pa_mainloop_api* m, void (*cb)(void *userdata), void *userdata) {
223 assert(fdl && pcm_handle && m && !fdl->m && cb);
224
225 fdl->pcm = pcm_handle;
226 fdl->m = m;
227
228 fdl->defer = m->defer_new(m, defer_cb, fdl);
229 assert(fdl->defer);
230
231 fdl->cb = cb;
232 fdl->userdata = userdata;
233
234 return 0;
235 }
236
237 int pa_alsa_fdlist_init_mixer(struct pa_alsa_fdlist *fdl, snd_mixer_t *mixer_handle, pa_mainloop_api* m) {
238 assert(fdl && mixer_handle && m && !fdl->m);
239
240 fdl->mixer = mixer_handle;
241 fdl->m = m;
242
243 fdl->defer = m->defer_new(m, defer_cb, fdl);
244 assert(fdl->defer);
245
246 return 0;
247 }
248
249 static int set_format(snd_pcm_t *pcm_handle, snd_pcm_hw_params_t *hwparams, pa_sample_format_t *f) {
250
251 static const snd_pcm_format_t format_trans[] = {
252 [PA_SAMPLE_U8] = SND_PCM_FORMAT_U8,
253 [PA_SAMPLE_ALAW] = SND_PCM_FORMAT_A_LAW,
254 [PA_SAMPLE_ULAW] = SND_PCM_FORMAT_MU_LAW,
255 [PA_SAMPLE_S16LE] = SND_PCM_FORMAT_S16_LE,
256 [PA_SAMPLE_S16BE] = SND_PCM_FORMAT_S16_BE,
257 [PA_SAMPLE_FLOAT32LE] = SND_PCM_FORMAT_FLOAT_LE,
258 [PA_SAMPLE_FLOAT32BE] = SND_PCM_FORMAT_FLOAT_BE,
259 };
260
261 static const pa_sample_format_t try_order[] = {
262 PA_SAMPLE_S16NE,
263 PA_SAMPLE_S16RE,
264 PA_SAMPLE_FLOAT32NE,
265 PA_SAMPLE_FLOAT32RE,
266 PA_SAMPLE_ULAW,
267 PA_SAMPLE_ALAW,
268 PA_SAMPLE_U8,
269 PA_SAMPLE_INVALID
270 };
271
272 int i, ret;
273
274 assert(pcm_handle);
275 assert(f);
276
277 if ((ret = snd_pcm_hw_params_set_format(pcm_handle, hwparams, format_trans[*f])) >= 0)
278 return ret;
279
280 if (*f == PA_SAMPLE_FLOAT32BE)
281 *f = PA_SAMPLE_FLOAT32LE;
282 else if (*f == PA_SAMPLE_FLOAT32LE)
283 *f = PA_SAMPLE_FLOAT32BE;
284 else if (*f == PA_SAMPLE_S16BE)
285 *f = PA_SAMPLE_S16LE;
286 else if (*f == PA_SAMPLE_S16LE)
287 *f = PA_SAMPLE_S16BE;
288 else
289 goto try_auto;
290
291 if ((ret = snd_pcm_hw_params_set_format(pcm_handle, hwparams, format_trans[*f])) >= 0)
292 return ret;
293
294 try_auto:
295
296 for (i = 0; try_order[i] != PA_SAMPLE_INVALID; i++) {
297 *f = try_order[i];
298
299 if ((ret = snd_pcm_hw_params_set_format(pcm_handle, hwparams, format_trans[*f])) >= 0)
300 return ret;
301 }
302
303 return -1;
304 }
305
306 /* Set the hardware parameters of the given ALSA device. Returns the
307 * selected fragment settings in *period and *period_size */
308 int pa_alsa_set_hw_params(snd_pcm_t *pcm_handle, pa_sample_spec *ss, uint32_t *periods, snd_pcm_uframes_t *period_size) {
309 int ret = -1;
310 snd_pcm_uframes_t buffer_size;
311 unsigned int r = ss->rate;
312 unsigned int c = ss->channels;
313 pa_sample_format_t f = ss->format;
314 snd_pcm_hw_params_t *hwparams;
315
316 assert(pcm_handle);
317 assert(ss);
318 assert(periods);
319 assert(period_size);
320
321 buffer_size = *periods * *period_size;
322
323 if ((ret = snd_pcm_hw_params_malloc(&hwparams)) < 0 ||
324 (ret = snd_pcm_hw_params_any(pcm_handle, hwparams)) < 0 ||
325 (ret = snd_pcm_hw_params_set_rate_resample(pcm_handle, hwparams, 0)) < 0 ||
326 (ret = snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
327 goto finish;
328
329 if ((ret = set_format(pcm_handle, hwparams, &f)) < 0)
330 goto finish;
331
332 if ((ret = snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams, &r, NULL)) < 0)
333 goto finish;
334
335 if ((ret = snd_pcm_hw_params_set_channels_near(pcm_handle, hwparams, &c)) < 0)
336 goto finish;
337
338 if ((*period_size > 0 && (ret = snd_pcm_hw_params_set_period_size_near(pcm_handle, hwparams, period_size, NULL)) < 0) ||
339 (*periods > 0 && (ret = snd_pcm_hw_params_set_buffer_size_near(pcm_handle, hwparams, &buffer_size)) < 0))
340 goto finish;
341
342 if ((ret = snd_pcm_hw_params(pcm_handle, hwparams)) < 0)
343 goto finish;
344
345 if (ss->rate != r) {
346 pa_log_warn("device doesn't support %u Hz, changed to %u Hz.", ss->rate, r);
347
348 /* If the sample rate deviates too much, we need to resample */
349 if (r < ss->rate*.95 || r > ss->rate*1.05)
350 ss->rate = r;
351 }
352
353 if (ss->channels != c) {
354 pa_log_warn("device doesn't support %u channels, changed to %u.", ss->channels, c);
355 ss->channels = c;
356 }
357
358 if (ss->format != f) {
359 pa_log_warn("device doesn't support sample format %s, changed to %s.", pa_sample_format_to_string(ss->format), pa_sample_format_to_string(f));
360 ss->format = f;
361 }
362
363 if ((ret = snd_pcm_prepare(pcm_handle)) < 0)
364 goto finish;
365
366 if ((ret = snd_pcm_hw_params_get_buffer_size(hwparams, &buffer_size)) < 0 ||
367 (ret = snd_pcm_hw_params_get_period_size(hwparams, period_size, NULL)) < 0)
368 goto finish;
369
370 assert(buffer_size > 0);
371 assert(*period_size > 0);
372 *periods = buffer_size / *period_size;
373 assert(*periods > 0);
374
375 ret = 0;
376
377 finish:
378 if (hwparams)
379 snd_pcm_hw_params_free(hwparams);
380
381 return ret;
382 }
383
384 int pa_alsa_prepare_mixer(snd_mixer_t *mixer, const char *dev) {
385 int err;
386
387 assert(mixer && dev);
388
389 if ((err = snd_mixer_attach(mixer, dev)) < 0) {
390 pa_log_warn("Unable to attach to mixer %s: %s", dev, snd_strerror(err));
391 return -1;
392 }
393
394 if ((err = snd_mixer_selem_register(mixer, NULL, NULL)) < 0) {
395 pa_log_warn("Unable to register mixer: %s", snd_strerror(err));
396 return -1;
397 }
398
399 if ((err = snd_mixer_load(mixer)) < 0) {
400 pa_log_warn("Unable to load mixer: %s", snd_strerror(err));
401 return -1;
402 }
403
404 return 0;
405 }
406
407 snd_mixer_elem_t *pa_alsa_find_elem(snd_mixer_t *mixer, const char *name, const char *fallback) {
408 snd_mixer_elem_t *elem;
409 snd_mixer_selem_id_t *sid = NULL;
410 snd_mixer_selem_id_alloca(&sid);
411
412 assert(mixer);
413 assert(name);
414
415 snd_mixer_selem_id_set_name(sid, name);
416
417 if (!(elem = snd_mixer_find_selem(mixer, sid))) {
418 pa_log_warn("Cannot find mixer control \"%s\".", snd_mixer_selem_id_get_name(sid));
419
420 if (fallback) {
421 snd_mixer_selem_id_set_name(sid, fallback);
422
423 if (!(elem = snd_mixer_find_selem(mixer, sid)))
424 pa_log_warn("Cannot find fallback mixer control \"%s\".", snd_mixer_selem_id_get_name(sid));
425 }
426 }
427
428 if (elem)
429 pa_log_info("Using mixer control \"%s\".", snd_mixer_selem_id_get_name(sid));
430
431 return elem;
432 }