]> code.delx.au - pulseaudio/blob - src/modules/alsa-util.c
Merge commit 'coling/master'
[pulseaudio] / src / modules / alsa-util.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
6
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA.
21 ***/
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <sys/types.h>
28 #include <limits.h>
29 #include <asoundlib.h>
30
31 #include <pulse/sample.h>
32 #include <pulse/xmalloc.h>
33
34 #include <pulsecore/log.h>
35 #include <pulsecore/macro.h>
36 #include <pulsecore/core-util.h>
37 #include <pulsecore/atomic.h>
38
39 #include "alsa-util.h"
40
41 struct pa_alsa_fdlist {
42 unsigned num_fds;
43 struct pollfd *fds;
44 /* This is a temporary buffer used to avoid lots of mallocs */
45 struct pollfd *work_fds;
46
47 snd_mixer_t *mixer;
48
49 pa_mainloop_api *m;
50 pa_defer_event *defer;
51 pa_io_event **ios;
52
53 pa_bool_t polled;
54
55 void (*cb)(void *userdata);
56 void *userdata;
57 };
58
59 static void io_cb(pa_mainloop_api*a, pa_io_event* e, int fd, pa_io_event_flags_t events, void *userdata) {
60
61 struct pa_alsa_fdlist *fdl = userdata;
62 int err;
63 unsigned i;
64 unsigned short revents;
65
66 pa_assert(a);
67 pa_assert(fdl);
68 pa_assert(fdl->mixer);
69 pa_assert(fdl->fds);
70 pa_assert(fdl->work_fds);
71
72 if (fdl->polled)
73 return;
74
75 fdl->polled = TRUE;
76
77 memcpy(fdl->work_fds, fdl->fds, sizeof(struct pollfd) * fdl->num_fds);
78
79 for (i = 0; i < fdl->num_fds; i++) {
80 if (e == fdl->ios[i]) {
81 if (events & PA_IO_EVENT_INPUT)
82 fdl->work_fds[i].revents |= POLLIN;
83 if (events & PA_IO_EVENT_OUTPUT)
84 fdl->work_fds[i].revents |= POLLOUT;
85 if (events & PA_IO_EVENT_ERROR)
86 fdl->work_fds[i].revents |= POLLERR;
87 if (events & PA_IO_EVENT_HANGUP)
88 fdl->work_fds[i].revents |= POLLHUP;
89 break;
90 }
91 }
92
93 pa_assert(i != fdl->num_fds);
94
95 if ((err = snd_mixer_poll_descriptors_revents(fdl->mixer, fdl->work_fds, fdl->num_fds, &revents)) < 0) {
96 pa_log_error("Unable to get poll revent: %s", snd_strerror(err));
97 return;
98 }
99
100 a->defer_enable(fdl->defer, 1);
101
102 if (revents)
103 snd_mixer_handle_events(fdl->mixer);
104 }
105
106 static void defer_cb(pa_mainloop_api*a, pa_defer_event* e, void *userdata) {
107 struct pa_alsa_fdlist *fdl = userdata;
108 unsigned num_fds, i;
109 int err;
110 struct pollfd *temp;
111
112 pa_assert(a);
113 pa_assert(fdl);
114 pa_assert(fdl->mixer);
115
116 a->defer_enable(fdl->defer, 0);
117
118 num_fds = (unsigned) snd_mixer_poll_descriptors_count(fdl->mixer);
119
120 if (num_fds != fdl->num_fds) {
121 if (fdl->fds)
122 pa_xfree(fdl->fds);
123 if (fdl->work_fds)
124 pa_xfree(fdl->work_fds);
125 fdl->fds = pa_xnew0(struct pollfd, num_fds);
126 fdl->work_fds = pa_xnew(struct pollfd, num_fds);
127 }
128
129 memset(fdl->work_fds, 0, sizeof(struct pollfd) * num_fds);
130
131 if ((err = snd_mixer_poll_descriptors(fdl->mixer, fdl->work_fds, num_fds)) < 0) {
132 pa_log_error("Unable to get poll descriptors: %s", snd_strerror(err));
133 return;
134 }
135
136 fdl->polled = FALSE;
137
138 if (memcmp(fdl->fds, fdl->work_fds, sizeof(struct pollfd) * num_fds) == 0)
139 return;
140
141 if (fdl->ios) {
142 for (i = 0; i < fdl->num_fds; i++)
143 a->io_free(fdl->ios[i]);
144
145 if (num_fds != fdl->num_fds) {
146 pa_xfree(fdl->ios);
147 fdl->ios = NULL;
148 }
149 }
150
151 if (!fdl->ios)
152 fdl->ios = pa_xnew(pa_io_event*, num_fds);
153
154 /* Swap pointers */
155 temp = fdl->work_fds;
156 fdl->work_fds = fdl->fds;
157 fdl->fds = temp;
158
159 fdl->num_fds = num_fds;
160
161 for (i = 0;i < num_fds;i++)
162 fdl->ios[i] = a->io_new(a, fdl->fds[i].fd,
163 ((fdl->fds[i].events & POLLIN) ? PA_IO_EVENT_INPUT : 0) |
164 ((fdl->fds[i].events & POLLOUT) ? PA_IO_EVENT_OUTPUT : 0),
165 io_cb, fdl);
166 }
167
168 struct pa_alsa_fdlist *pa_alsa_fdlist_new(void) {
169 struct pa_alsa_fdlist *fdl;
170
171 fdl = pa_xnew0(struct pa_alsa_fdlist, 1);
172
173 fdl->num_fds = 0;
174 fdl->fds = NULL;
175 fdl->work_fds = NULL;
176 fdl->mixer = NULL;
177 fdl->m = NULL;
178 fdl->defer = NULL;
179 fdl->ios = NULL;
180 fdl->polled = FALSE;
181
182 return fdl;
183 }
184
185 void pa_alsa_fdlist_free(struct pa_alsa_fdlist *fdl) {
186 pa_assert(fdl);
187
188 if (fdl->defer) {
189 pa_assert(fdl->m);
190 fdl->m->defer_free(fdl->defer);
191 }
192
193 if (fdl->ios) {
194 unsigned i;
195 pa_assert(fdl->m);
196 for (i = 0; i < fdl->num_fds; i++)
197 fdl->m->io_free(fdl->ios[i]);
198 pa_xfree(fdl->ios);
199 }
200
201 if (fdl->fds)
202 pa_xfree(fdl->fds);
203 if (fdl->work_fds)
204 pa_xfree(fdl->work_fds);
205
206 pa_xfree(fdl);
207 }
208
209 int pa_alsa_fdlist_set_mixer(struct pa_alsa_fdlist *fdl, snd_mixer_t *mixer_handle, pa_mainloop_api* m) {
210 pa_assert(fdl);
211 pa_assert(mixer_handle);
212 pa_assert(m);
213 pa_assert(!fdl->m);
214
215 fdl->mixer = mixer_handle;
216 fdl->m = m;
217 fdl->defer = m->defer_new(m, defer_cb, fdl);
218
219 return 0;
220 }
221
222 static int set_format(snd_pcm_t *pcm_handle, snd_pcm_hw_params_t *hwparams, pa_sample_format_t *f) {
223
224 static const snd_pcm_format_t format_trans[] = {
225 [PA_SAMPLE_U8] = SND_PCM_FORMAT_U8,
226 [PA_SAMPLE_ALAW] = SND_PCM_FORMAT_A_LAW,
227 [PA_SAMPLE_ULAW] = SND_PCM_FORMAT_MU_LAW,
228 [PA_SAMPLE_S16LE] = SND_PCM_FORMAT_S16_LE,
229 [PA_SAMPLE_S16BE] = SND_PCM_FORMAT_S16_BE,
230 [PA_SAMPLE_FLOAT32LE] = SND_PCM_FORMAT_FLOAT_LE,
231 [PA_SAMPLE_FLOAT32BE] = SND_PCM_FORMAT_FLOAT_BE,
232 [PA_SAMPLE_S32LE] = SND_PCM_FORMAT_S32_LE,
233 [PA_SAMPLE_S32BE] = SND_PCM_FORMAT_S32_BE,
234 };
235
236 static const pa_sample_format_t try_order[] = {
237 PA_SAMPLE_FLOAT32NE,
238 PA_SAMPLE_FLOAT32RE,
239 PA_SAMPLE_S32NE,
240 PA_SAMPLE_S32RE,
241 PA_SAMPLE_S16NE,
242 PA_SAMPLE_S16RE,
243 PA_SAMPLE_ALAW,
244 PA_SAMPLE_ULAW,
245 PA_SAMPLE_U8,
246 PA_SAMPLE_INVALID
247 };
248
249 int i, ret;
250
251 pa_assert(pcm_handle);
252 pa_assert(f);
253
254 if ((ret = snd_pcm_hw_params_set_format(pcm_handle, hwparams, format_trans[*f])) >= 0)
255 return ret;
256
257 if (*f == PA_SAMPLE_FLOAT32BE)
258 *f = PA_SAMPLE_FLOAT32LE;
259 else if (*f == PA_SAMPLE_FLOAT32LE)
260 *f = PA_SAMPLE_FLOAT32BE;
261 else if (*f == PA_SAMPLE_S16BE)
262 *f = PA_SAMPLE_S16LE;
263 else if (*f == PA_SAMPLE_S16LE)
264 *f = PA_SAMPLE_S16BE;
265 else if (*f == PA_SAMPLE_S32BE)
266 *f = PA_SAMPLE_S32LE;
267 else if (*f == PA_SAMPLE_S32LE)
268 *f = PA_SAMPLE_S32BE;
269 else
270 goto try_auto;
271
272 if ((ret = snd_pcm_hw_params_set_format(pcm_handle, hwparams, format_trans[*f])) >= 0)
273 return ret;
274
275 try_auto:
276
277 for (i = 0; try_order[i] != PA_SAMPLE_INVALID; i++) {
278 *f = try_order[i];
279
280 if ((ret = snd_pcm_hw_params_set_format(pcm_handle, hwparams, format_trans[*f])) >= 0)
281 return ret;
282 }
283
284 return -1;
285 }
286
287 /* Set the hardware parameters of the given ALSA device. Returns the
288 * selected fragment settings in *period and *period_size */
289 int pa_alsa_set_hw_params(
290 snd_pcm_t *pcm_handle,
291 pa_sample_spec *ss,
292 uint32_t *periods,
293 snd_pcm_uframes_t *period_size,
294 snd_pcm_uframes_t tsched_size,
295 pa_bool_t *use_mmap,
296 pa_bool_t *use_tsched,
297 pa_bool_t require_exact_channel_number) {
298
299 int ret = -1;
300 snd_pcm_uframes_t _period_size = *period_size;
301 unsigned int _periods = *periods;
302 snd_pcm_uframes_t buffer_size;
303 unsigned int r = ss->rate;
304 unsigned int c = ss->channels;
305 pa_sample_format_t f = ss->format;
306 snd_pcm_hw_params_t *hwparams;
307 pa_bool_t _use_mmap = use_mmap && *use_mmap;
308 pa_bool_t _use_tsched = use_tsched && *use_tsched;
309 int dir;
310
311 pa_assert(pcm_handle);
312 pa_assert(ss);
313 pa_assert(periods);
314 pa_assert(period_size);
315
316 snd_pcm_hw_params_alloca(&hwparams);
317
318 if ((ret = snd_pcm_hw_params_any(pcm_handle, hwparams)) < 0)
319 goto finish;
320
321 if ((ret = snd_pcm_hw_params_set_rate_resample(pcm_handle, hwparams, 0)) < 0)
322 goto finish;
323
324 if (_use_mmap) {
325 if ((ret = snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_MMAP_INTERLEAVED)) < 0) {
326
327 /* mmap() didn't work, fall back to interleaved */
328
329 if ((ret = snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
330 goto finish;
331
332 _use_mmap = FALSE;
333 }
334
335 } else if ((ret = snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
336 goto finish;
337
338 if (!_use_mmap)
339 _use_tsched = FALSE;
340
341 if ((ret = set_format(pcm_handle, hwparams, &f)) < 0)
342 goto finish;
343
344 if ((ret = snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams, &r, NULL)) < 0)
345 goto finish;
346
347 /* Adjust the buffer sizes, if we didn't get the rate we were asking for */
348 _period_size = (snd_pcm_uframes_t) (((uint64_t) _period_size * r) / ss->rate);
349 tsched_size = (snd_pcm_uframes_t) (((uint64_t) tsched_size * r) / ss->rate);
350
351 if (require_exact_channel_number) {
352 if ((ret = snd_pcm_hw_params_set_channels(pcm_handle, hwparams, c)) < 0)
353 goto finish;
354 } else {
355 if ((ret = snd_pcm_hw_params_set_channels_near(pcm_handle, hwparams, &c)) < 0)
356 goto finish;
357 }
358
359 if (_use_tsched) {
360 _period_size = tsched_size;
361 _periods = 1;
362
363 pa_assert_se(snd_pcm_hw_params_get_buffer_size_max(hwparams, &buffer_size) == 0);
364 pa_log_debug("Maximum hw buffer size is %u ms", (unsigned) buffer_size * 1000 / r);
365 }
366
367 buffer_size = _periods * _period_size;
368
369 if ((ret = snd_pcm_hw_params_set_periods_integer(pcm_handle, hwparams)) < 0)
370 goto finish;
371
372 if (_periods > 0) {
373
374 /* First we pass 0 as direction to get exactly what we asked
375 * for. That this is necessary is presumably a bug in ALSA */
376
377 dir = 0;
378 if ((ret = snd_pcm_hw_params_set_periods_near(pcm_handle, hwparams, &_periods, &dir)) < 0) {
379 dir = 1;
380 if ((ret = snd_pcm_hw_params_set_periods_near(pcm_handle, hwparams, &_periods, &dir)) < 0) {
381 dir = -1;
382 if ((ret = snd_pcm_hw_params_set_periods_near(pcm_handle, hwparams, &_periods, &dir)) < 0)
383 goto finish;
384 }
385 }
386 }
387
388 if (_period_size > 0)
389 if ((ret = snd_pcm_hw_params_set_buffer_size_near(pcm_handle, hwparams, &buffer_size)) < 0)
390 goto finish;
391
392 if ((ret = snd_pcm_hw_params(pcm_handle, hwparams)) < 0)
393 goto finish;
394
395 if (ss->rate != r)
396 pa_log_warn("Device %s doesn't support %u Hz, changed to %u Hz.", snd_pcm_name(pcm_handle), ss->rate, r);
397
398 if (ss->channels != c)
399 pa_log_warn("Device %s doesn't support %u channels, changed to %u.", snd_pcm_name(pcm_handle), ss->channels, c);
400
401 if (ss->format != f)
402 pa_log_warn("Device %s doesn't support sample format %s, changed to %s.", snd_pcm_name(pcm_handle), pa_sample_format_to_string(ss->format), pa_sample_format_to_string(f));
403
404 if ((ret = snd_pcm_prepare(pcm_handle)) < 0)
405 goto finish;
406
407 if ((ret = snd_pcm_hw_params_get_period_size(hwparams, &_period_size, &dir)) < 0 ||
408 (ret = snd_pcm_hw_params_get_periods(hwparams, &_periods, &dir)) < 0)
409 goto finish;
410
411 /* If the sample rate deviates too much, we need to resample */
412 if (r < ss->rate*.95 || r > ss->rate*1.05)
413 ss->rate = r;
414 ss->channels = (uint8_t) c;
415 ss->format = f;
416
417 pa_assert(_periods > 0);
418 pa_assert(_period_size > 0);
419
420 *periods = _periods;
421 *period_size = _period_size;
422
423 if (use_mmap)
424 *use_mmap = _use_mmap;
425
426 if (use_tsched)
427 *use_tsched = _use_tsched;
428
429 ret = 0;
430
431 snd_pcm_nonblock(pcm_handle, 1);
432
433 finish:
434
435 return ret;
436 }
437
438 int pa_alsa_set_sw_params(snd_pcm_t *pcm, snd_pcm_uframes_t avail_min) {
439 snd_pcm_sw_params_t *swparams;
440 int err;
441
442 pa_assert(pcm);
443
444 snd_pcm_sw_params_alloca(&swparams);
445
446 if ((err = snd_pcm_sw_params_current(pcm, swparams) < 0)) {
447 pa_log_warn("Unable to determine current swparams: %s\n", snd_strerror(err));
448 return err;
449 }
450
451 if ((err = snd_pcm_sw_params_set_stop_threshold(pcm, swparams, (snd_pcm_uframes_t) -1)) < 0) {
452 pa_log_warn("Unable to set stop threshold: %s\n", snd_strerror(err));
453 return err;
454 }
455
456 if ((err = snd_pcm_sw_params_set_start_threshold(pcm, swparams, (snd_pcm_uframes_t) -1)) < 0) {
457 pa_log_warn("Unable to set start threshold: %s\n", snd_strerror(err));
458 return err;
459 }
460
461 if ((err = snd_pcm_sw_params_set_avail_min(pcm, swparams, avail_min)) < 0) {
462 pa_log_error("snd_pcm_sw_params_set_avail_min() failed: %s", snd_strerror(err));
463 return err;
464 }
465
466 if ((err = snd_pcm_sw_params(pcm, swparams)) < 0) {
467 pa_log_warn("Unable to set sw params: %s\n", snd_strerror(err));
468 return err;
469 }
470
471 return 0;
472 }
473
474 struct device_info {
475 pa_channel_map map;
476 const char *name;
477 };
478
479 static const struct device_info device_table[] = {
480 {{ 2, { PA_CHANNEL_POSITION_LEFT, PA_CHANNEL_POSITION_RIGHT } }, "front" },
481
482 {{ 4, { PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT,
483 PA_CHANNEL_POSITION_REAR_LEFT, PA_CHANNEL_POSITION_REAR_RIGHT }}, "surround40" },
484
485 {{ 5, { PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT,
486 PA_CHANNEL_POSITION_REAR_LEFT, PA_CHANNEL_POSITION_REAR_RIGHT,
487 PA_CHANNEL_POSITION_LFE }}, "surround41" },
488
489 {{ 5, { PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT,
490 PA_CHANNEL_POSITION_REAR_LEFT, PA_CHANNEL_POSITION_REAR_RIGHT,
491 PA_CHANNEL_POSITION_CENTER }}, "surround50" },
492
493 {{ 6, { PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT,
494 PA_CHANNEL_POSITION_REAR_LEFT, PA_CHANNEL_POSITION_REAR_RIGHT,
495 PA_CHANNEL_POSITION_CENTER, PA_CHANNEL_POSITION_LFE }}, "surround51" },
496
497 {{ 8, { PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT,
498 PA_CHANNEL_POSITION_REAR_LEFT, PA_CHANNEL_POSITION_REAR_RIGHT,
499 PA_CHANNEL_POSITION_CENTER, PA_CHANNEL_POSITION_LFE,
500 PA_CHANNEL_POSITION_SIDE_LEFT, PA_CHANNEL_POSITION_SIDE_RIGHT }} , "surround71" },
501
502 {{ 0, { 0 }}, NULL }
503 };
504
505 static pa_bool_t channel_map_superset(const pa_channel_map *a, const pa_channel_map *b) {
506 pa_bool_t in_a[PA_CHANNEL_POSITION_MAX];
507 unsigned i;
508
509 pa_assert(a);
510 pa_assert(b);
511
512 memset(in_a, 0, sizeof(in_a));
513
514 for (i = 0; i < a->channels; i++)
515 in_a[a->map[i]] = TRUE;
516
517 for (i = 0; i < b->channels; i++)
518 if (!in_a[b->map[i]])
519 return FALSE;
520
521 return TRUE;
522 }
523
524 snd_pcm_t *pa_alsa_open_by_device_id(
525 const char *dev_id,
526 char **dev,
527 pa_sample_spec *ss,
528 pa_channel_map* map,
529 int mode,
530 uint32_t *nfrags,
531 snd_pcm_uframes_t *period_size,
532 snd_pcm_uframes_t tsched_size,
533 pa_bool_t *use_mmap,
534 pa_bool_t *use_tsched) {
535
536 int i;
537 int direction = 1;
538 int err;
539 char *d;
540 snd_pcm_t *pcm_handle;
541
542 pa_assert(dev_id);
543 pa_assert(dev);
544 pa_assert(ss);
545 pa_assert(map);
546 pa_assert(nfrags);
547 pa_assert(period_size);
548
549 /* First we try to find a device string with a superset of the
550 * requested channel map and open it without the plug: prefix. We
551 * iterate through our device table from top to bottom and take
552 * the first that matches. If we didn't find a working device that
553 * way, we iterate backwards, and check all devices that do not
554 * provide a superset of the requested channel map.*/
555
556 for (i = 0;; i += direction) {
557 pa_sample_spec try_ss;
558
559 if (i < 0) {
560 pa_assert(direction == -1);
561
562 /* OK, so we iterated backwards, and now are at the
563 * beginning of our list. */
564
565 break;
566
567 } else if (!device_table[i].name) {
568 pa_assert(direction == 1);
569
570 /* OK, so we are at the end of our list. at iterated
571 * forwards. */
572
573 i--;
574 direction = -1;
575 }
576
577 if ((direction > 0) == !channel_map_superset(&device_table[i].map, map))
578 continue;
579
580 d = pa_sprintf_malloc("%s:%s", device_table[i].name, dev_id);
581
582 for (;;) {
583 pa_log_debug("Trying %s...", d);
584
585 /* We don't pass SND_PCM_NONBLOCK here, since alsa-lib <=
586 * 1.0.17a would then ignore the SND_PCM_NO_xxx
587 * flags. Instead we enable nonblock mode afterwards via
588 * snd_pcm_nonblock(). Also see
589 * http://mailman.alsa-project.org/pipermail/alsa-devel/2008-August/010258.html */
590
591 if ((err = snd_pcm_open(&pcm_handle, d, mode,
592 /* SND_PCM_NONBLOCK| */
593 SND_PCM_NO_AUTO_RESAMPLE|
594 SND_PCM_NO_AUTO_CHANNELS|
595 SND_PCM_NO_AUTO_FORMAT)) < 0) {
596 pa_log_info("Couldn't open PCM device %s: %s", d, snd_strerror(err));
597 break;
598 }
599
600 try_ss.channels = device_table[i].map.channels;
601 try_ss.rate = ss->rate;
602 try_ss.format = ss->format;
603
604 if ((err = pa_alsa_set_hw_params(pcm_handle, &try_ss, nfrags, period_size, tsched_size, use_mmap, use_tsched, TRUE)) < 0) {
605
606 if (!pa_startswith(d, "plug:") && !pa_startswith(d, "plughw:")) {
607 char *t;
608
609 t = pa_sprintf_malloc("plug:%s", d);
610 pa_xfree(d);
611 d = t;
612
613 snd_pcm_close(pcm_handle);
614 continue;
615 }
616
617 pa_log_info("PCM device %s refused our hw parameters: %s", d, snd_strerror(err));
618 snd_pcm_close(pcm_handle);
619 break;
620 }
621
622 *ss = try_ss;
623 *map = device_table[i].map;
624 pa_assert(map->channels == ss->channels);
625 *dev = d;
626 return pcm_handle;
627 }
628
629 pa_xfree(d);
630 }
631
632 /* OK, we didn't find any good device, so let's try the raw plughw: stuff */
633
634 d = pa_sprintf_malloc("hw:%s", dev_id);
635 pa_log_debug("Trying %s as last resort...", d);
636 pcm_handle = pa_alsa_open_by_device_string(d, dev, ss, map, mode, nfrags, period_size, tsched_size, use_mmap, use_tsched);
637 pa_xfree(d);
638
639 return pcm_handle;
640 }
641
642 snd_pcm_t *pa_alsa_open_by_device_string(
643 const char *device,
644 char **dev,
645 pa_sample_spec *ss,
646 pa_channel_map* map,
647 int mode,
648 uint32_t *nfrags,
649 snd_pcm_uframes_t *period_size,
650 snd_pcm_uframes_t tsched_size,
651 pa_bool_t *use_mmap,
652 pa_bool_t *use_tsched) {
653
654 int err;
655 char *d;
656 snd_pcm_t *pcm_handle;
657
658 pa_assert(device);
659 pa_assert(dev);
660 pa_assert(ss);
661 pa_assert(map);
662 pa_assert(nfrags);
663 pa_assert(period_size);
664
665 d = pa_xstrdup(device);
666
667 for (;;) {
668 pa_log_debug("Trying %s...", d);
669
670 /* We don't pass SND_PCM_NONBLOCK here, since alsa-lib <=
671 * 1.0.17a would then ignore the SND_PCM_NO_xxx flags. Instead
672 * we enable nonblock mode afterwards via
673 * snd_pcm_nonblock(). Also see
674 * http://mailman.alsa-project.org/pipermail/alsa-devel/2008-August/010258.html */
675
676 if ((err = snd_pcm_open(&pcm_handle, d, mode,
677 /*SND_PCM_NONBLOCK|*/
678 SND_PCM_NO_AUTO_RESAMPLE|
679 SND_PCM_NO_AUTO_CHANNELS|
680 SND_PCM_NO_AUTO_FORMAT)) < 0) {
681 pa_log("Error opening PCM device %s: %s", d, snd_strerror(err));
682 pa_xfree(d);
683 return NULL;
684 }
685
686 if ((err = pa_alsa_set_hw_params(pcm_handle, ss, nfrags, period_size, tsched_size, use_mmap, use_tsched, FALSE)) < 0) {
687
688 /* Hmm, some hw is very exotic, so we retry with plug, if without it didn't work */
689
690 if (!pa_startswith(d, "plug:") && !pa_startswith(d, "plughw:")) {
691 char *t;
692
693 t = pa_sprintf_malloc("plug:%s", d);
694 pa_xfree(d);
695 d = t;
696
697 snd_pcm_close(pcm_handle);
698 continue;
699 }
700
701 pa_log("Failed to set hardware parameters on %s: %s", d, snd_strerror(err));
702 pa_xfree(d);
703 snd_pcm_close(pcm_handle);
704 return NULL;
705 }
706
707 *dev = d;
708
709 if (ss->channels != map->channels)
710 pa_channel_map_init_extend(map, ss->channels, PA_CHANNEL_MAP_ALSA);
711
712 return pcm_handle;
713 }
714 }
715
716 int pa_alsa_prepare_mixer(snd_mixer_t *mixer, const char *dev) {
717 int err;
718
719 pa_assert(mixer);
720 pa_assert(dev);
721
722 if ((err = snd_mixer_attach(mixer, dev)) < 0) {
723 pa_log_info("Unable to attach to mixer %s: %s", dev, snd_strerror(err));
724 return -1;
725 }
726
727 if ((err = snd_mixer_selem_register(mixer, NULL, NULL)) < 0) {
728 pa_log_warn("Unable to register mixer: %s", snd_strerror(err));
729 return -1;
730 }
731
732 if ((err = snd_mixer_load(mixer)) < 0) {
733 pa_log_warn("Unable to load mixer: %s", snd_strerror(err));
734 return -1;
735 }
736
737 pa_log_info("Successfully attached to mixer '%s'", dev);
738
739 return 0;
740 }
741
742 snd_mixer_elem_t *pa_alsa_find_elem(snd_mixer_t *mixer, const char *name, const char *fallback) {
743 snd_mixer_elem_t *elem;
744 snd_mixer_selem_id_t *sid = NULL;
745
746 snd_mixer_selem_id_alloca(&sid);
747
748 pa_assert(mixer);
749 pa_assert(name);
750
751 snd_mixer_selem_id_set_name(sid, name);
752
753 if (!(elem = snd_mixer_find_selem(mixer, sid))) {
754 pa_log_info("Cannot find mixer control \"%s\".", snd_mixer_selem_id_get_name(sid));
755
756 if (fallback) {
757 snd_mixer_selem_id_set_name(sid, fallback);
758
759 if (!(elem = snd_mixer_find_selem(mixer, sid)))
760 pa_log_warn("Cannot find fallback mixer control \"%s\".", snd_mixer_selem_id_get_name(sid));
761 }
762 }
763
764 if (elem)
765 pa_log_info("Using mixer control \"%s\".", snd_mixer_selem_id_get_name(sid));
766
767 return elem;
768 }
769
770 static const snd_mixer_selem_channel_id_t alsa_channel_ids[PA_CHANNEL_POSITION_MAX] = {
771 [PA_CHANNEL_POSITION_MONO] = SND_MIXER_SCHN_MONO, /* The ALSA name is just an alias! */
772
773 [PA_CHANNEL_POSITION_FRONT_CENTER] = SND_MIXER_SCHN_FRONT_CENTER,
774 [PA_CHANNEL_POSITION_FRONT_LEFT] = SND_MIXER_SCHN_FRONT_LEFT,
775 [PA_CHANNEL_POSITION_FRONT_RIGHT] = SND_MIXER_SCHN_FRONT_RIGHT,
776
777 [PA_CHANNEL_POSITION_REAR_CENTER] = SND_MIXER_SCHN_REAR_CENTER,
778 [PA_CHANNEL_POSITION_REAR_LEFT] = SND_MIXER_SCHN_REAR_LEFT,
779 [PA_CHANNEL_POSITION_REAR_RIGHT] = SND_MIXER_SCHN_REAR_RIGHT,
780
781 [PA_CHANNEL_POSITION_LFE] = SND_MIXER_SCHN_WOOFER,
782
783 [PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER] = SND_MIXER_SCHN_UNKNOWN,
784 [PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER] = SND_MIXER_SCHN_UNKNOWN,
785
786 [PA_CHANNEL_POSITION_SIDE_LEFT] = SND_MIXER_SCHN_SIDE_LEFT,
787 [PA_CHANNEL_POSITION_SIDE_RIGHT] = SND_MIXER_SCHN_SIDE_RIGHT,
788
789 [PA_CHANNEL_POSITION_AUX0] = SND_MIXER_SCHN_UNKNOWN,
790 [PA_CHANNEL_POSITION_AUX1] = SND_MIXER_SCHN_UNKNOWN,
791 [PA_CHANNEL_POSITION_AUX2] = SND_MIXER_SCHN_UNKNOWN,
792 [PA_CHANNEL_POSITION_AUX3] = SND_MIXER_SCHN_UNKNOWN,
793 [PA_CHANNEL_POSITION_AUX4] = SND_MIXER_SCHN_UNKNOWN,
794 [PA_CHANNEL_POSITION_AUX5] = SND_MIXER_SCHN_UNKNOWN,
795 [PA_CHANNEL_POSITION_AUX6] = SND_MIXER_SCHN_UNKNOWN,
796 [PA_CHANNEL_POSITION_AUX7] = SND_MIXER_SCHN_UNKNOWN,
797 [PA_CHANNEL_POSITION_AUX8] = SND_MIXER_SCHN_UNKNOWN,
798 [PA_CHANNEL_POSITION_AUX9] = SND_MIXER_SCHN_UNKNOWN,
799 [PA_CHANNEL_POSITION_AUX10] = SND_MIXER_SCHN_UNKNOWN,
800 [PA_CHANNEL_POSITION_AUX11] = SND_MIXER_SCHN_UNKNOWN,
801 [PA_CHANNEL_POSITION_AUX12] = SND_MIXER_SCHN_UNKNOWN,
802 [PA_CHANNEL_POSITION_AUX13] = SND_MIXER_SCHN_UNKNOWN,
803 [PA_CHANNEL_POSITION_AUX14] = SND_MIXER_SCHN_UNKNOWN,
804 [PA_CHANNEL_POSITION_AUX15] = SND_MIXER_SCHN_UNKNOWN,
805 [PA_CHANNEL_POSITION_AUX16] = SND_MIXER_SCHN_UNKNOWN,
806 [PA_CHANNEL_POSITION_AUX17] = SND_MIXER_SCHN_UNKNOWN,
807 [PA_CHANNEL_POSITION_AUX18] = SND_MIXER_SCHN_UNKNOWN,
808 [PA_CHANNEL_POSITION_AUX19] = SND_MIXER_SCHN_UNKNOWN,
809 [PA_CHANNEL_POSITION_AUX20] = SND_MIXER_SCHN_UNKNOWN,
810 [PA_CHANNEL_POSITION_AUX21] = SND_MIXER_SCHN_UNKNOWN,
811 [PA_CHANNEL_POSITION_AUX22] = SND_MIXER_SCHN_UNKNOWN,
812 [PA_CHANNEL_POSITION_AUX23] = SND_MIXER_SCHN_UNKNOWN,
813 [PA_CHANNEL_POSITION_AUX24] = SND_MIXER_SCHN_UNKNOWN,
814 [PA_CHANNEL_POSITION_AUX25] = SND_MIXER_SCHN_UNKNOWN,
815 [PA_CHANNEL_POSITION_AUX26] = SND_MIXER_SCHN_UNKNOWN,
816 [PA_CHANNEL_POSITION_AUX27] = SND_MIXER_SCHN_UNKNOWN,
817 [PA_CHANNEL_POSITION_AUX28] = SND_MIXER_SCHN_UNKNOWN,
818 [PA_CHANNEL_POSITION_AUX29] = SND_MIXER_SCHN_UNKNOWN,
819 [PA_CHANNEL_POSITION_AUX30] = SND_MIXER_SCHN_UNKNOWN,
820 [PA_CHANNEL_POSITION_AUX31] = SND_MIXER_SCHN_UNKNOWN,
821
822 [PA_CHANNEL_POSITION_TOP_CENTER] = SND_MIXER_SCHN_UNKNOWN,
823
824 [PA_CHANNEL_POSITION_TOP_FRONT_CENTER] = SND_MIXER_SCHN_UNKNOWN,
825 [PA_CHANNEL_POSITION_TOP_FRONT_LEFT] = SND_MIXER_SCHN_UNKNOWN,
826 [PA_CHANNEL_POSITION_TOP_FRONT_RIGHT] = SND_MIXER_SCHN_UNKNOWN,
827
828 [PA_CHANNEL_POSITION_TOP_REAR_CENTER] = SND_MIXER_SCHN_UNKNOWN,
829 [PA_CHANNEL_POSITION_TOP_REAR_LEFT] = SND_MIXER_SCHN_UNKNOWN,
830 [PA_CHANNEL_POSITION_TOP_REAR_RIGHT] = SND_MIXER_SCHN_UNKNOWN
831 };
832
833
834 int pa_alsa_calc_mixer_map(snd_mixer_elem_t *elem, const pa_channel_map *channel_map, snd_mixer_selem_channel_id_t mixer_map[], pa_bool_t playback) {
835 unsigned i;
836 pa_bool_t alsa_channel_used[SND_MIXER_SCHN_LAST];
837 pa_bool_t mono_used = FALSE;
838
839 pa_assert(elem);
840 pa_assert(channel_map);
841 pa_assert(mixer_map);
842
843 memset(&alsa_channel_used, 0, sizeof(alsa_channel_used));
844
845 if (channel_map->channels > 1 &&
846 ((playback && snd_mixer_selem_has_playback_volume_joined(elem)) ||
847 (!playback && snd_mixer_selem_has_capture_volume_joined(elem)))) {
848 pa_log_info("ALSA device lacks independant volume controls for each channel.");
849 return -1;
850 }
851
852 for (i = 0; i < channel_map->channels; i++) {
853 snd_mixer_selem_channel_id_t id;
854 pa_bool_t is_mono;
855
856 is_mono = channel_map->map[i] == PA_CHANNEL_POSITION_MONO;
857 id = alsa_channel_ids[channel_map->map[i]];
858
859 if (!is_mono && id == SND_MIXER_SCHN_UNKNOWN) {
860 pa_log_info("Configured channel map contains channel '%s' that is unknown to the ALSA mixer.", pa_channel_position_to_string(channel_map->map[i]));
861 return -1;
862 }
863
864 if ((is_mono && mono_used) || (!is_mono && alsa_channel_used[id])) {
865 pa_log_info("Channel map has duplicate channel '%s', falling back to software volume control.", pa_channel_position_to_string(channel_map->map[i]));
866 return -1;
867 }
868
869 if ((playback && (!snd_mixer_selem_has_playback_channel(elem, id) || (is_mono && !snd_mixer_selem_is_playback_mono(elem)))) ||
870 (!playback && (!snd_mixer_selem_has_capture_channel(elem, id) || (is_mono && !snd_mixer_selem_is_capture_mono(elem))))) {
871
872 pa_log_info("ALSA device lacks separate volumes control for channel '%s'", pa_channel_position_to_string(channel_map->map[i]));
873 return -1;
874 }
875
876 if (is_mono) {
877 mixer_map[i] = SND_MIXER_SCHN_MONO;
878 mono_used = TRUE;
879 } else {
880 mixer_map[i] = id;
881 alsa_channel_used[id] = TRUE;
882 }
883 }
884
885 pa_log_info("All %u channels can be mapped to mixer channels.", channel_map->channels);
886
887 return 0;
888 }
889
890 void pa_alsa_dump(snd_pcm_t *pcm) {
891 int err;
892 snd_output_t *out;
893
894 pa_assert(pcm);
895
896 pa_assert_se(snd_output_buffer_open(&out) == 0);
897
898 if ((err = snd_pcm_dump(pcm, out)) < 0)
899 pa_log_debug("snd_pcm_dump(): %s", snd_strerror(err));
900 else {
901 char *s = NULL;
902 snd_output_buffer_string(out, &s);
903 pa_log_debug("snd_pcm_dump():\n%s", pa_strnull(s));
904 }
905
906 pa_assert_se(snd_output_close(out) == 0);
907 }
908
909 void pa_alsa_dump_status(snd_pcm_t *pcm) {
910 int err;
911 snd_output_t *out;
912 snd_pcm_status_t *status;
913
914 pa_assert(pcm);
915
916 snd_pcm_status_alloca(&status);
917
918 pa_assert_se(snd_output_buffer_open(&out) == 0);
919
920 pa_assert_se(snd_pcm_status(pcm, status) == 0);
921
922 if ((err = snd_pcm_status_dump(status, out)) < 0)
923 pa_log_debug("snd_pcm_dump(): %s", snd_strerror(err));
924 else {
925 char *s = NULL;
926 snd_output_buffer_string(out, &s);
927 pa_log_debug("snd_pcm_dump():\n%s", pa_strnull(s));
928 }
929
930 pa_assert_se(snd_output_close(out) == 0);
931 }
932
933 static void alsa_error_handler(const char *file, int line, const char *function, int err, const char *fmt,...) {
934 va_list ap;
935 char *alsa_file;
936
937 alsa_file = pa_sprintf_malloc("(alsa-lib)%s", file);
938
939 va_start(ap, fmt);
940
941 pa_log_levelv_meta(PA_LOG_INFO, alsa_file, line, function, fmt, ap);
942
943 va_end(ap);
944
945 pa_xfree(alsa_file);
946 }
947
948 static pa_atomic_t n_error_handler_installed = PA_ATOMIC_INIT(0);
949
950 void pa_alsa_redirect_errors_inc(void) {
951 /* This is not really thread safe, but we do our best */
952
953 if (pa_atomic_inc(&n_error_handler_installed) == 0)
954 snd_lib_error_set_handler(alsa_error_handler);
955 }
956
957 void pa_alsa_redirect_errors_dec(void) {
958 int r;
959
960 pa_assert_se((r = pa_atomic_dec(&n_error_handler_installed)) >= 1);
961
962 if (r == 1)
963 snd_lib_error_set_handler(NULL);
964 }
965
966 void pa_alsa_init_proplist(pa_proplist *p, snd_pcm_info_t *pcm_info) {
967
968 static const char * const alsa_class_table[SND_PCM_CLASS_LAST+1] = {
969 [SND_PCM_CLASS_GENERIC] = "generic",
970 [SND_PCM_CLASS_MULTI] = "multi",
971 [SND_PCM_CLASS_MODEM] = "modem",
972 [SND_PCM_CLASS_DIGITIZER] = "digitizer"
973 };
974 static const char * const class_table[SND_PCM_CLASS_LAST+1] = {
975 [SND_PCM_CLASS_GENERIC] = "sound",
976 [SND_PCM_CLASS_MULTI] = NULL,
977 [SND_PCM_CLASS_MODEM] = "modem",
978 [SND_PCM_CLASS_DIGITIZER] = NULL
979 };
980 static const char * const alsa_subclass_table[SND_PCM_SUBCLASS_LAST+1] = {
981 [SND_PCM_SUBCLASS_GENERIC_MIX] = "generic-mix",
982 [SND_PCM_SUBCLASS_MULTI_MIX] = "multi-mix"
983 };
984
985 snd_pcm_class_t class;
986 snd_pcm_subclass_t subclass;
987 const char *n, *id, *sdn;
988 char *cn = NULL, *lcn = NULL;
989 int card;
990
991 pa_assert(p);
992 pa_assert(pcm_info);
993
994 pa_proplist_sets(p, PA_PROP_DEVICE_API, "alsa");
995
996 class = snd_pcm_info_get_class(pcm_info);
997 if (class <= SND_PCM_CLASS_LAST) {
998 if (class_table[class])
999 pa_proplist_sets(p, PA_PROP_DEVICE_CLASS, class_table[class]);
1000 if (alsa_class_table[class])
1001 pa_proplist_sets(p, "alsa.class", alsa_class_table[class]);
1002 }
1003 subclass = snd_pcm_info_get_subclass(pcm_info);
1004 if (subclass <= SND_PCM_SUBCLASS_LAST)
1005 if (alsa_subclass_table[subclass])
1006 pa_proplist_sets(p, "alsa.subclass", alsa_subclass_table[subclass]);
1007
1008 if ((n = snd_pcm_info_get_name(pcm_info)))
1009 pa_proplist_sets(p, "alsa.name", n);
1010
1011 if ((id = snd_pcm_info_get_id(pcm_info)))
1012 pa_proplist_sets(p, "alsa.id", id);
1013
1014 pa_proplist_setf(p, "alsa.subdevice", "%u", snd_pcm_info_get_subdevice(pcm_info));
1015 if ((sdn = snd_pcm_info_get_subdevice_name(pcm_info)))
1016 pa_proplist_sets(p, "alsa.subdevice_name", sdn);
1017
1018 pa_proplist_setf(p, "alsa.device", "%u", snd_pcm_info_get_device(pcm_info));
1019
1020 if ((card = snd_pcm_info_get_card(pcm_info)) >= 0) {
1021 pa_proplist_setf(p, "alsa.card", "%i", card);
1022
1023 if (snd_card_get_name(card, &cn) >= 0)
1024 pa_proplist_sets(p, "alsa.card_name", cn);
1025
1026 if (snd_card_get_longname(card, &lcn) >= 0)
1027 pa_proplist_sets(p, "alsa.long_card_name", lcn);
1028 }
1029
1030 if (cn && n)
1031 pa_proplist_setf(p, PA_PROP_DEVICE_DESCRIPTION, "%s - %s", cn, n);
1032 else if (cn)
1033 pa_proplist_sets(p, PA_PROP_DEVICE_DESCRIPTION, cn);
1034 else if (n)
1035 pa_proplist_sets(p, PA_PROP_DEVICE_DESCRIPTION, n);
1036
1037 free(lcn);
1038 free(cn);
1039 }
1040
1041 int pa_alsa_recover_from_poll(snd_pcm_t *pcm, int revents) {
1042 snd_pcm_state_t state;
1043 int err;
1044
1045 pa_assert(pcm);
1046
1047 if (revents & POLLERR)
1048 pa_log_warn("Got POLLERR from ALSA");
1049 if (revents & POLLNVAL)
1050 pa_log_warn("Got POLLNVAL from ALSA");
1051 if (revents & POLLHUP)
1052 pa_log_warn("Got POLLHUP from ALSA");
1053
1054 state = snd_pcm_state(pcm);
1055 pa_log_warn("PCM state is %s", snd_pcm_state_name(state));
1056
1057 /* Try to recover from this error */
1058
1059 switch (state) {
1060
1061 case SND_PCM_STATE_XRUN:
1062 if ((err = snd_pcm_recover(pcm, -EPIPE, 1)) != 0) {
1063 pa_log_warn("Could not recover from POLLERR|POLLNVAL|POLLHUP and XRUN: %s", snd_strerror(err));
1064 return -1;
1065 }
1066 break;
1067
1068 case SND_PCM_STATE_SUSPENDED:
1069 if ((err = snd_pcm_recover(pcm, -ESTRPIPE, 1)) != 0) {
1070 pa_log_warn("Could not recover from POLLERR|POLLNVAL|POLLHUP and SUSPENDED: %s", snd_strerror(err));
1071 return -1;
1072 }
1073 break;
1074
1075 default:
1076
1077 snd_pcm_drop(pcm);
1078
1079 if ((err = snd_pcm_prepare(pcm)) < 0) {
1080 pa_log_warn("Could not recover from POLLERR|POLLNVAL|POLLHUP with snd_pcm_prepare(): %s", snd_strerror(err));
1081 return -1;
1082 }
1083 break;
1084 }
1085
1086 return 0;
1087 }
1088
1089 pa_rtpoll_item* pa_alsa_build_pollfd(snd_pcm_t *pcm, pa_rtpoll *rtpoll) {
1090 int n, err;
1091 struct pollfd *pollfd;
1092 pa_rtpoll_item *item;
1093
1094 pa_assert(pcm);
1095
1096 if ((n = snd_pcm_poll_descriptors_count(pcm)) < 0) {
1097 pa_log("snd_pcm_poll_descriptors_count() failed: %s", snd_strerror(n));
1098 return NULL;
1099 }
1100
1101 item = pa_rtpoll_item_new(rtpoll, PA_RTPOLL_NEVER, (unsigned) n);
1102 pollfd = pa_rtpoll_item_get_pollfd(item, NULL);
1103
1104 if ((err = snd_pcm_poll_descriptors(pcm, pollfd, (unsigned) n)) < 0) {
1105 pa_log("snd_pcm_poll_descriptors() failed: %s", snd_strerror(err));
1106 pa_rtpoll_item_free(item);
1107 return NULL;
1108 }
1109
1110 return item;
1111 }
1112
1113 pa_cvolume *pa_alsa_volume_divide(pa_cvolume *r, const pa_cvolume *t) {
1114 unsigned i;
1115
1116 pa_assert(r);
1117 pa_assert(t);
1118 pa_assert(r->channels == t->channels);
1119
1120 for (i = 0; i < r->channels; i++) {
1121 double a, b, c;
1122
1123 a = pa_sw_volume_to_linear(r->values[i]); /* the hw volume */
1124 b = pa_sw_volume_to_linear(t->values[i]); /* the intended volume */
1125
1126 if (a <= 0)
1127 c = 0;
1128 else
1129 c = b / a;
1130
1131 r->values[i] = pa_sw_volume_from_linear(c);
1132 }
1133
1134 return r;
1135 }