2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
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.
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.
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
27 #include <sys/types.h>
29 #include <asoundlib.h>
31 #include <pulse/sample.h>
32 #include <pulse/xmalloc.h>
33 #include <pulse/timeval.h>
35 #include <pulsecore/log.h>
36 #include <pulsecore/macro.h>
37 #include <pulsecore/core-util.h>
38 #include <pulsecore/atomic.h>
40 #include "alsa-util.h"
42 struct pa_alsa_fdlist {
45 /* This is a temporary buffer used to avoid lots of mallocs */
46 struct pollfd *work_fds;
51 pa_defer_event *defer;
56 void (*cb)(void *userdata);
60 static void io_cb(pa_mainloop_api*a, pa_io_event* e, int fd, pa_io_event_flags_t events, void *userdata) {
62 struct pa_alsa_fdlist *fdl = userdata;
65 unsigned short revents;
69 pa_assert(fdl->mixer);
71 pa_assert(fdl->work_fds);
78 memcpy(fdl->work_fds, fdl->fds, sizeof(struct pollfd) * fdl->num_fds);
80 for (i = 0; i < fdl->num_fds; i++) {
81 if (e == fdl->ios[i]) {
82 if (events & PA_IO_EVENT_INPUT)
83 fdl->work_fds[i].revents |= POLLIN;
84 if (events & PA_IO_EVENT_OUTPUT)
85 fdl->work_fds[i].revents |= POLLOUT;
86 if (events & PA_IO_EVENT_ERROR)
87 fdl->work_fds[i].revents |= POLLERR;
88 if (events & PA_IO_EVENT_HANGUP)
89 fdl->work_fds[i].revents |= POLLHUP;
94 pa_assert(i != fdl->num_fds);
96 if ((err = snd_mixer_poll_descriptors_revents(fdl->mixer, fdl->work_fds, fdl->num_fds, &revents)) < 0) {
97 pa_log_error("Unable to get poll revent: %s", snd_strerror(err));
101 a->defer_enable(fdl->defer, 1);
104 snd_mixer_handle_events(fdl->mixer);
107 static void defer_cb(pa_mainloop_api*a, pa_defer_event* e, void *userdata) {
108 struct pa_alsa_fdlist *fdl = userdata;
115 pa_assert(fdl->mixer);
117 a->defer_enable(fdl->defer, 0);
119 num_fds = (unsigned) snd_mixer_poll_descriptors_count(fdl->mixer);
121 if (num_fds != fdl->num_fds) {
125 pa_xfree(fdl->work_fds);
126 fdl->fds = pa_xnew0(struct pollfd, num_fds);
127 fdl->work_fds = pa_xnew(struct pollfd, num_fds);
130 memset(fdl->work_fds, 0, sizeof(struct pollfd) * num_fds);
132 if ((err = snd_mixer_poll_descriptors(fdl->mixer, fdl->work_fds, num_fds)) < 0) {
133 pa_log_error("Unable to get poll descriptors: %s", snd_strerror(err));
139 if (memcmp(fdl->fds, fdl->work_fds, sizeof(struct pollfd) * num_fds) == 0)
143 for (i = 0; i < fdl->num_fds; i++)
144 a->io_free(fdl->ios[i]);
146 if (num_fds != fdl->num_fds) {
153 fdl->ios = pa_xnew(pa_io_event*, num_fds);
156 temp = fdl->work_fds;
157 fdl->work_fds = fdl->fds;
160 fdl->num_fds = num_fds;
162 for (i = 0;i < num_fds;i++)
163 fdl->ios[i] = a->io_new(a, fdl->fds[i].fd,
164 ((fdl->fds[i].events & POLLIN) ? PA_IO_EVENT_INPUT : 0) |
165 ((fdl->fds[i].events & POLLOUT) ? PA_IO_EVENT_OUTPUT : 0),
169 struct pa_alsa_fdlist *pa_alsa_fdlist_new(void) {
170 struct pa_alsa_fdlist *fdl;
172 fdl = pa_xnew0(struct pa_alsa_fdlist, 1);
176 fdl->work_fds = NULL;
186 void pa_alsa_fdlist_free(struct pa_alsa_fdlist *fdl) {
191 fdl->m->defer_free(fdl->defer);
197 for (i = 0; i < fdl->num_fds; i++)
198 fdl->m->io_free(fdl->ios[i]);
205 pa_xfree(fdl->work_fds);
210 int pa_alsa_fdlist_set_mixer(struct pa_alsa_fdlist *fdl, snd_mixer_t *mixer_handle, pa_mainloop_api* m) {
212 pa_assert(mixer_handle);
216 fdl->mixer = mixer_handle;
218 fdl->defer = m->defer_new(m, defer_cb, fdl);
223 static int set_format(snd_pcm_t *pcm_handle, snd_pcm_hw_params_t *hwparams, pa_sample_format_t *f) {
225 static const snd_pcm_format_t format_trans[] = {
226 [PA_SAMPLE_U8] = SND_PCM_FORMAT_U8,
227 [PA_SAMPLE_ALAW] = SND_PCM_FORMAT_A_LAW,
228 [PA_SAMPLE_ULAW] = SND_PCM_FORMAT_MU_LAW,
229 [PA_SAMPLE_S16LE] = SND_PCM_FORMAT_S16_LE,
230 [PA_SAMPLE_S16BE] = SND_PCM_FORMAT_S16_BE,
231 [PA_SAMPLE_FLOAT32LE] = SND_PCM_FORMAT_FLOAT_LE,
232 [PA_SAMPLE_FLOAT32BE] = SND_PCM_FORMAT_FLOAT_BE,
233 [PA_SAMPLE_S32LE] = SND_PCM_FORMAT_S32_LE,
234 [PA_SAMPLE_S32BE] = SND_PCM_FORMAT_S32_BE,
237 static const pa_sample_format_t try_order[] = {
252 pa_assert(pcm_handle);
255 if ((ret = snd_pcm_hw_params_set_format(pcm_handle, hwparams, format_trans[*f])) >= 0)
258 if (*f == PA_SAMPLE_FLOAT32BE)
259 *f = PA_SAMPLE_FLOAT32LE;
260 else if (*f == PA_SAMPLE_FLOAT32LE)
261 *f = PA_SAMPLE_FLOAT32BE;
262 else if (*f == PA_SAMPLE_S16BE)
263 *f = PA_SAMPLE_S16LE;
264 else if (*f == PA_SAMPLE_S16LE)
265 *f = PA_SAMPLE_S16BE;
266 else if (*f == PA_SAMPLE_S32BE)
267 *f = PA_SAMPLE_S32LE;
268 else if (*f == PA_SAMPLE_S32LE)
269 *f = PA_SAMPLE_S32BE;
273 if ((ret = snd_pcm_hw_params_set_format(pcm_handle, hwparams, format_trans[*f])) >= 0)
278 for (i = 0; try_order[i] != PA_SAMPLE_INVALID; i++) {
281 if ((ret = snd_pcm_hw_params_set_format(pcm_handle, hwparams, format_trans[*f])) >= 0)
288 /* Set the hardware parameters of the given ALSA device. Returns the
289 * selected fragment settings in *period and *period_size */
290 int pa_alsa_set_hw_params(
291 snd_pcm_t *pcm_handle,
294 snd_pcm_uframes_t *period_size,
295 snd_pcm_uframes_t tsched_size,
297 pa_bool_t *use_tsched,
298 pa_bool_t require_exact_channel_number) {
301 snd_pcm_uframes_t _period_size = *period_size;
302 unsigned int _periods = *periods;
303 snd_pcm_uframes_t buffer_size;
304 unsigned int r = ss->rate;
305 unsigned int c = ss->channels;
306 pa_sample_format_t f = ss->format;
307 snd_pcm_hw_params_t *hwparams;
308 pa_bool_t _use_mmap = use_mmap && *use_mmap;
309 pa_bool_t _use_tsched = use_tsched && *use_tsched;
312 pa_assert(pcm_handle);
315 pa_assert(period_size);
317 snd_pcm_hw_params_alloca(&hwparams);
319 if ((ret = snd_pcm_hw_params_any(pcm_handle, hwparams)) < 0)
322 if ((ret = snd_pcm_hw_params_set_rate_resample(pcm_handle, hwparams, 0)) < 0)
326 if ((ret = snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_MMAP_INTERLEAVED)) < 0) {
328 /* mmap() didn't work, fall back to interleaved */
330 if ((ret = snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
336 } else if ((ret = snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
342 if ((ret = set_format(pcm_handle, hwparams, &f)) < 0)
345 if ((ret = snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams, &r, NULL)) < 0)
348 /* Adjust the buffer sizes, if we didn't get the rate we were asking for */
349 _period_size = (snd_pcm_uframes_t) (((uint64_t) _period_size * r) / ss->rate);
350 tsched_size = (snd_pcm_uframes_t) (((uint64_t) tsched_size * r) / ss->rate);
352 if (require_exact_channel_number) {
353 if ((ret = snd_pcm_hw_params_set_channels(pcm_handle, hwparams, c)) < 0)
356 if ((ret = snd_pcm_hw_params_set_channels_near(pcm_handle, hwparams, &c)) < 0)
361 _period_size = tsched_size;
364 pa_assert_se(snd_pcm_hw_params_get_buffer_size_max(hwparams, &buffer_size) == 0);
365 pa_log_debug("Maximum hw buffer size is %u ms", (unsigned) buffer_size * 1000 / r);
368 buffer_size = _periods * _period_size;
370 if ((ret = snd_pcm_hw_params_set_periods_integer(pcm_handle, hwparams)) < 0)
375 /* First we pass 0 as direction to get exactly what we asked
376 * for. That this is necessary is presumably a bug in ALSA */
379 if ((ret = snd_pcm_hw_params_set_periods_near(pcm_handle, hwparams, &_periods, &dir)) < 0) {
381 if ((ret = snd_pcm_hw_params_set_periods_near(pcm_handle, hwparams, &_periods, &dir)) < 0) {
383 if ((ret = snd_pcm_hw_params_set_periods_near(pcm_handle, hwparams, &_periods, &dir)) < 0)
389 if (_period_size > 0)
390 if ((ret = snd_pcm_hw_params_set_buffer_size_near(pcm_handle, hwparams, &buffer_size)) < 0)
393 if ((ret = snd_pcm_hw_params(pcm_handle, hwparams)) < 0)
397 pa_log_warn("Device %s doesn't support %u Hz, changed to %u Hz.", snd_pcm_name(pcm_handle), ss->rate, r);
399 if (ss->channels != c)
400 pa_log_warn("Device %s doesn't support %u channels, changed to %u.", snd_pcm_name(pcm_handle), ss->channels, c);
403 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));
405 if ((ret = snd_pcm_prepare(pcm_handle)) < 0)
408 if ((ret = snd_pcm_hw_params_get_period_size(hwparams, &_period_size, &dir)) < 0 ||
409 (ret = snd_pcm_hw_params_get_periods(hwparams, &_periods, &dir)) < 0)
412 /* If the sample rate deviates too much, we need to resample */
413 if (r < ss->rate*.95 || r > ss->rate*1.05)
415 ss->channels = (uint8_t) c;
418 pa_assert(_periods > 0);
419 pa_assert(_period_size > 0);
422 *period_size = _period_size;
425 *use_mmap = _use_mmap;
428 *use_tsched = _use_tsched;
432 snd_pcm_nonblock(pcm_handle, 1);
439 int pa_alsa_set_sw_params(snd_pcm_t *pcm, snd_pcm_uframes_t avail_min) {
440 snd_pcm_sw_params_t *swparams;
445 snd_pcm_sw_params_alloca(&swparams);
447 if ((err = snd_pcm_sw_params_current(pcm, swparams) < 0)) {
448 pa_log_warn("Unable to determine current swparams: %s\n", snd_strerror(err));
452 if ((err = snd_pcm_sw_params_set_stop_threshold(pcm, swparams, (snd_pcm_uframes_t) -1)) < 0) {
453 pa_log_warn("Unable to set stop threshold: %s\n", snd_strerror(err));
457 if ((err = snd_pcm_sw_params_set_start_threshold(pcm, swparams, (snd_pcm_uframes_t) -1)) < 0) {
458 pa_log_warn("Unable to set start threshold: %s\n", snd_strerror(err));
462 if ((err = snd_pcm_sw_params_set_avail_min(pcm, swparams, avail_min)) < 0) {
463 pa_log_error("snd_pcm_sw_params_set_avail_min() failed: %s", snd_strerror(err));
467 if ((err = snd_pcm_sw_params(pcm, swparams)) < 0) {
468 pa_log_warn("Unable to set sw params: %s\n", snd_strerror(err));
480 static const struct device_info device_table[] = {
481 {{ 2, { PA_CHANNEL_POSITION_LEFT, PA_CHANNEL_POSITION_RIGHT } }, "front" },
483 {{ 4, { PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT,
484 PA_CHANNEL_POSITION_REAR_LEFT, PA_CHANNEL_POSITION_REAR_RIGHT }}, "surround40" },
486 {{ 5, { PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT,
487 PA_CHANNEL_POSITION_REAR_LEFT, PA_CHANNEL_POSITION_REAR_RIGHT,
488 PA_CHANNEL_POSITION_LFE }}, "surround41" },
490 {{ 5, { PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT,
491 PA_CHANNEL_POSITION_REAR_LEFT, PA_CHANNEL_POSITION_REAR_RIGHT,
492 PA_CHANNEL_POSITION_CENTER }}, "surround50" },
494 {{ 6, { PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT,
495 PA_CHANNEL_POSITION_REAR_LEFT, PA_CHANNEL_POSITION_REAR_RIGHT,
496 PA_CHANNEL_POSITION_CENTER, PA_CHANNEL_POSITION_LFE }}, "surround51" },
498 {{ 8, { PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT,
499 PA_CHANNEL_POSITION_REAR_LEFT, PA_CHANNEL_POSITION_REAR_RIGHT,
500 PA_CHANNEL_POSITION_CENTER, PA_CHANNEL_POSITION_LFE,
501 PA_CHANNEL_POSITION_SIDE_LEFT, PA_CHANNEL_POSITION_SIDE_RIGHT }} , "surround71" },
506 static pa_bool_t channel_map_superset(const pa_channel_map *a, const pa_channel_map *b) {
507 pa_bool_t in_a[PA_CHANNEL_POSITION_MAX];
513 memset(in_a, 0, sizeof(in_a));
515 for (i = 0; i < a->channels; i++)
516 in_a[a->map[i]] = TRUE;
518 for (i = 0; i < b->channels; i++)
519 if (!in_a[b->map[i]])
525 snd_pcm_t *pa_alsa_open_by_device_id(
532 snd_pcm_uframes_t *period_size,
533 snd_pcm_uframes_t tsched_size,
535 pa_bool_t *use_tsched) {
541 snd_pcm_t *pcm_handle;
548 pa_assert(period_size);
550 /* First we try to find a device string with a superset of the
551 * requested channel map and open it without the plug: prefix. We
552 * iterate through our device table from top to bottom and take
553 * the first that matches. If we didn't find a working device that
554 * way, we iterate backwards, and check all devices that do not
555 * provide a superset of the requested channel map.*/
557 for (i = 0;; i += direction) {
558 pa_sample_spec try_ss;
562 pa_assert(direction == -1);
564 /* OK, so we iterated backwards, and now are at the
565 * beginning of our list. */
569 } else if (!device_table[i].name) {
570 pa_assert(direction == 1);
572 /* OK, so we are at the end of our list. at iterated
579 if ((direction > 0) == !channel_map_superset(&device_table[i].map, map))
582 d = pa_sprintf_malloc("%s:%s", device_table[i].name, dev_id);
586 pa_log_debug("Trying %s %s SND_PCM_NO_AUTO_FORMAT ...", d, reformat ? "without" : "with");
588 /* We don't pass SND_PCM_NONBLOCK here, since alsa-lib <=
589 * 1.0.17a would then ignore the SND_PCM_NO_xxx
590 * flags. Instead we enable nonblock mode afterwards via
591 * snd_pcm_nonblock(). Also see
592 * http://mailman.alsa-project.org/pipermail/alsa-devel/2008-August/010258.html */
594 if ((err = snd_pcm_open(&pcm_handle, d, mode,
595 /* SND_PCM_NONBLOCK| */
596 SND_PCM_NO_AUTO_RESAMPLE|
597 SND_PCM_NO_AUTO_CHANNELS|
598 (reformat ? 0 : SND_PCM_NO_AUTO_FORMAT))) < 0) {
599 pa_log_info("Couldn't open PCM device %s: %s", d, snd_strerror(err));
603 try_ss.channels = device_table[i].map.channels;
604 try_ss.rate = ss->rate;
605 try_ss.format = ss->format;
607 if ((err = pa_alsa_set_hw_params(pcm_handle, &try_ss, nfrags, period_size, tsched_size, use_mmap, use_tsched, TRUE)) < 0) {
611 snd_pcm_close(pcm_handle);
615 if (!pa_startswith(d, "plug:") && !pa_startswith(d, "plughw:")) {
618 t = pa_sprintf_malloc("plug:%s", d);
624 snd_pcm_close(pcm_handle);
628 pa_log_info("PCM device %s refused our hw parameters: %s", d, snd_strerror(err));
629 snd_pcm_close(pcm_handle);
634 *map = device_table[i].map;
635 pa_assert(map->channels == ss->channels);
643 /* OK, we didn't find any good device, so let's try the raw plughw: stuff */
645 d = pa_sprintf_malloc("hw:%s", dev_id);
646 pa_log_debug("Trying %s as last resort...", d);
647 pcm_handle = pa_alsa_open_by_device_string(d, dev, ss, map, mode, nfrags, period_size, tsched_size, use_mmap, use_tsched);
653 snd_pcm_t *pa_alsa_open_by_device_string(
660 snd_pcm_uframes_t *period_size,
661 snd_pcm_uframes_t tsched_size,
663 pa_bool_t *use_tsched) {
667 snd_pcm_t *pcm_handle;
668 pa_bool_t reformat = FALSE;
675 pa_assert(period_size);
677 d = pa_xstrdup(device);
680 pa_log_debug("Trying %s %s SND_PCM_NO_AUTO_FORMAT ...", d, reformat ? "without" : "with");
682 /* We don't pass SND_PCM_NONBLOCK here, since alsa-lib <=
683 * 1.0.17a would then ignore the SND_PCM_NO_xxx flags. Instead
684 * we enable nonblock mode afterwards via
685 * snd_pcm_nonblock(). Also see
686 * http://mailman.alsa-project.org/pipermail/alsa-devel/2008-August/010258.html */
688 if ((err = snd_pcm_open(&pcm_handle, d, mode,
689 /*SND_PCM_NONBLOCK|*/
690 SND_PCM_NO_AUTO_RESAMPLE|
691 SND_PCM_NO_AUTO_CHANNELS|
692 (reformat ? 0 : SND_PCM_NO_AUTO_FORMAT))) < 0) {
693 pa_log("Error opening PCM device %s: %s", d, snd_strerror(err));
698 if ((err = pa_alsa_set_hw_params(pcm_handle, ss, nfrags, period_size, tsched_size, use_mmap, use_tsched, FALSE)) < 0) {
703 snd_pcm_close(pcm_handle);
707 /* Hmm, some hw is very exotic, so we retry with plug, if without it didn't work */
709 if (!pa_startswith(d, "plug:") && !pa_startswith(d, "plughw:")) {
712 t = pa_sprintf_malloc("plug:%s", d);
718 snd_pcm_close(pcm_handle);
722 pa_log("Failed to set hardware parameters on %s: %s", d, snd_strerror(err));
724 snd_pcm_close(pcm_handle);
730 if (ss->channels != map->channels)
731 pa_channel_map_init_extend(map, ss->channels, PA_CHANNEL_MAP_ALSA);
737 int pa_alsa_prepare_mixer(snd_mixer_t *mixer, const char *dev) {
743 if ((err = snd_mixer_attach(mixer, dev)) < 0) {
744 pa_log_info("Unable to attach to mixer %s: %s", dev, snd_strerror(err));
748 if ((err = snd_mixer_selem_register(mixer, NULL, NULL)) < 0) {
749 pa_log_warn("Unable to register mixer: %s", snd_strerror(err));
753 if ((err = snd_mixer_load(mixer)) < 0) {
754 pa_log_warn("Unable to load mixer: %s", snd_strerror(err));
758 pa_log_info("Successfully attached to mixer '%s'", dev);
763 snd_mixer_elem_t *pa_alsa_find_elem(snd_mixer_t *mixer, const char *name, const char *fallback) {
764 snd_mixer_elem_t *elem;
765 snd_mixer_selem_id_t *sid = NULL;
767 snd_mixer_selem_id_alloca(&sid);
772 snd_mixer_selem_id_set_name(sid, name);
774 if (!(elem = snd_mixer_find_selem(mixer, sid))) {
775 pa_log_info("Cannot find mixer control \"%s\".", snd_mixer_selem_id_get_name(sid));
778 snd_mixer_selem_id_set_name(sid, fallback);
780 if (!(elem = snd_mixer_find_selem(mixer, sid)))
781 pa_log_warn("Cannot find fallback mixer control \"%s\".", snd_mixer_selem_id_get_name(sid));
786 pa_log_info("Using mixer control \"%s\".", snd_mixer_selem_id_get_name(sid));
791 static const snd_mixer_selem_channel_id_t alsa_channel_ids[PA_CHANNEL_POSITION_MAX] = {
792 [PA_CHANNEL_POSITION_MONO] = SND_MIXER_SCHN_MONO, /* The ALSA name is just an alias! */
794 [PA_CHANNEL_POSITION_FRONT_CENTER] = SND_MIXER_SCHN_FRONT_CENTER,
795 [PA_CHANNEL_POSITION_FRONT_LEFT] = SND_MIXER_SCHN_FRONT_LEFT,
796 [PA_CHANNEL_POSITION_FRONT_RIGHT] = SND_MIXER_SCHN_FRONT_RIGHT,
798 [PA_CHANNEL_POSITION_REAR_CENTER] = SND_MIXER_SCHN_REAR_CENTER,
799 [PA_CHANNEL_POSITION_REAR_LEFT] = SND_MIXER_SCHN_REAR_LEFT,
800 [PA_CHANNEL_POSITION_REAR_RIGHT] = SND_MIXER_SCHN_REAR_RIGHT,
802 [PA_CHANNEL_POSITION_LFE] = SND_MIXER_SCHN_WOOFER,
804 [PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER] = SND_MIXER_SCHN_UNKNOWN,
805 [PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER] = SND_MIXER_SCHN_UNKNOWN,
807 [PA_CHANNEL_POSITION_SIDE_LEFT] = SND_MIXER_SCHN_SIDE_LEFT,
808 [PA_CHANNEL_POSITION_SIDE_RIGHT] = SND_MIXER_SCHN_SIDE_RIGHT,
810 [PA_CHANNEL_POSITION_AUX0] = SND_MIXER_SCHN_UNKNOWN,
811 [PA_CHANNEL_POSITION_AUX1] = SND_MIXER_SCHN_UNKNOWN,
812 [PA_CHANNEL_POSITION_AUX2] = SND_MIXER_SCHN_UNKNOWN,
813 [PA_CHANNEL_POSITION_AUX3] = SND_MIXER_SCHN_UNKNOWN,
814 [PA_CHANNEL_POSITION_AUX4] = SND_MIXER_SCHN_UNKNOWN,
815 [PA_CHANNEL_POSITION_AUX5] = SND_MIXER_SCHN_UNKNOWN,
816 [PA_CHANNEL_POSITION_AUX6] = SND_MIXER_SCHN_UNKNOWN,
817 [PA_CHANNEL_POSITION_AUX7] = SND_MIXER_SCHN_UNKNOWN,
818 [PA_CHANNEL_POSITION_AUX8] = SND_MIXER_SCHN_UNKNOWN,
819 [PA_CHANNEL_POSITION_AUX9] = SND_MIXER_SCHN_UNKNOWN,
820 [PA_CHANNEL_POSITION_AUX10] = SND_MIXER_SCHN_UNKNOWN,
821 [PA_CHANNEL_POSITION_AUX11] = SND_MIXER_SCHN_UNKNOWN,
822 [PA_CHANNEL_POSITION_AUX12] = SND_MIXER_SCHN_UNKNOWN,
823 [PA_CHANNEL_POSITION_AUX13] = SND_MIXER_SCHN_UNKNOWN,
824 [PA_CHANNEL_POSITION_AUX14] = SND_MIXER_SCHN_UNKNOWN,
825 [PA_CHANNEL_POSITION_AUX15] = SND_MIXER_SCHN_UNKNOWN,
826 [PA_CHANNEL_POSITION_AUX16] = SND_MIXER_SCHN_UNKNOWN,
827 [PA_CHANNEL_POSITION_AUX17] = SND_MIXER_SCHN_UNKNOWN,
828 [PA_CHANNEL_POSITION_AUX18] = SND_MIXER_SCHN_UNKNOWN,
829 [PA_CHANNEL_POSITION_AUX19] = SND_MIXER_SCHN_UNKNOWN,
830 [PA_CHANNEL_POSITION_AUX20] = SND_MIXER_SCHN_UNKNOWN,
831 [PA_CHANNEL_POSITION_AUX21] = SND_MIXER_SCHN_UNKNOWN,
832 [PA_CHANNEL_POSITION_AUX22] = SND_MIXER_SCHN_UNKNOWN,
833 [PA_CHANNEL_POSITION_AUX23] = SND_MIXER_SCHN_UNKNOWN,
834 [PA_CHANNEL_POSITION_AUX24] = SND_MIXER_SCHN_UNKNOWN,
835 [PA_CHANNEL_POSITION_AUX25] = SND_MIXER_SCHN_UNKNOWN,
836 [PA_CHANNEL_POSITION_AUX26] = SND_MIXER_SCHN_UNKNOWN,
837 [PA_CHANNEL_POSITION_AUX27] = SND_MIXER_SCHN_UNKNOWN,
838 [PA_CHANNEL_POSITION_AUX28] = SND_MIXER_SCHN_UNKNOWN,
839 [PA_CHANNEL_POSITION_AUX29] = SND_MIXER_SCHN_UNKNOWN,
840 [PA_CHANNEL_POSITION_AUX30] = SND_MIXER_SCHN_UNKNOWN,
841 [PA_CHANNEL_POSITION_AUX31] = SND_MIXER_SCHN_UNKNOWN,
843 [PA_CHANNEL_POSITION_TOP_CENTER] = SND_MIXER_SCHN_UNKNOWN,
845 [PA_CHANNEL_POSITION_TOP_FRONT_CENTER] = SND_MIXER_SCHN_UNKNOWN,
846 [PA_CHANNEL_POSITION_TOP_FRONT_LEFT] = SND_MIXER_SCHN_UNKNOWN,
847 [PA_CHANNEL_POSITION_TOP_FRONT_RIGHT] = SND_MIXER_SCHN_UNKNOWN,
849 [PA_CHANNEL_POSITION_TOP_REAR_CENTER] = SND_MIXER_SCHN_UNKNOWN,
850 [PA_CHANNEL_POSITION_TOP_REAR_LEFT] = SND_MIXER_SCHN_UNKNOWN,
851 [PA_CHANNEL_POSITION_TOP_REAR_RIGHT] = SND_MIXER_SCHN_UNKNOWN
855 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) {
857 pa_bool_t alsa_channel_used[SND_MIXER_SCHN_LAST];
858 pa_bool_t mono_used = FALSE;
861 pa_assert(channel_map);
862 pa_assert(mixer_map);
864 memset(&alsa_channel_used, 0, sizeof(alsa_channel_used));
866 if (channel_map->channels > 1 &&
867 ((playback && snd_mixer_selem_has_playback_volume_joined(elem)) ||
868 (!playback && snd_mixer_selem_has_capture_volume_joined(elem)))) {
869 pa_log_info("ALSA device lacks independant volume controls for each channel.");
873 for (i = 0; i < channel_map->channels; i++) {
874 snd_mixer_selem_channel_id_t id;
877 is_mono = channel_map->map[i] == PA_CHANNEL_POSITION_MONO;
878 id = alsa_channel_ids[channel_map->map[i]];
880 if (!is_mono && id == SND_MIXER_SCHN_UNKNOWN) {
881 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]));
885 if ((is_mono && mono_used) || (!is_mono && alsa_channel_used[id])) {
886 pa_log_info("Channel map has duplicate channel '%s', falling back to software volume control.", pa_channel_position_to_string(channel_map->map[i]));
890 if ((playback && (!snd_mixer_selem_has_playback_channel(elem, id) || (is_mono && !snd_mixer_selem_is_playback_mono(elem)))) ||
891 (!playback && (!snd_mixer_selem_has_capture_channel(elem, id) || (is_mono && !snd_mixer_selem_is_capture_mono(elem))))) {
893 pa_log_info("ALSA device lacks separate volumes control for channel '%s'", pa_channel_position_to_string(channel_map->map[i]));
898 mixer_map[i] = SND_MIXER_SCHN_MONO;
902 alsa_channel_used[id] = TRUE;
906 pa_log_info("All %u channels can be mapped to mixer channels.", channel_map->channels);
911 void pa_alsa_dump(snd_pcm_t *pcm) {
917 pa_assert_se(snd_output_buffer_open(&out) == 0);
919 if ((err = snd_pcm_dump(pcm, out)) < 0)
920 pa_log_debug("snd_pcm_dump(): %s", snd_strerror(err));
923 snd_output_buffer_string(out, &s);
924 pa_log_debug("snd_pcm_dump():\n%s", pa_strnull(s));
927 pa_assert_se(snd_output_close(out) == 0);
930 void pa_alsa_dump_status(snd_pcm_t *pcm) {
933 snd_pcm_status_t *status;
937 snd_pcm_status_alloca(&status);
939 pa_assert_se(snd_output_buffer_open(&out) == 0);
941 pa_assert_se(snd_pcm_status(pcm, status) == 0);
943 if ((err = snd_pcm_status_dump(status, out)) < 0)
944 pa_log_debug("snd_pcm_dump(): %s", snd_strerror(err));
947 snd_output_buffer_string(out, &s);
948 pa_log_debug("snd_pcm_dump():\n%s", pa_strnull(s));
951 pa_assert_se(snd_output_close(out) == 0);
954 static void alsa_error_handler(const char *file, int line, const char *function, int err, const char *fmt,...) {
958 alsa_file = pa_sprintf_malloc("(alsa-lib)%s", file);
962 pa_log_levelv_meta(PA_LOG_INFO, alsa_file, line, function, fmt, ap);
969 static pa_atomic_t n_error_handler_installed = PA_ATOMIC_INIT(0);
971 void pa_alsa_redirect_errors_inc(void) {
972 /* This is not really thread safe, but we do our best */
974 if (pa_atomic_inc(&n_error_handler_installed) == 0)
975 snd_lib_error_set_handler(alsa_error_handler);
978 void pa_alsa_redirect_errors_dec(void) {
981 pa_assert_se((r = pa_atomic_dec(&n_error_handler_installed)) >= 1);
984 snd_lib_error_set_handler(NULL);
987 void pa_alsa_init_proplist(pa_proplist *p, snd_pcm_info_t *pcm_info) {
989 static const char * const alsa_class_table[SND_PCM_CLASS_LAST+1] = {
990 [SND_PCM_CLASS_GENERIC] = "generic",
991 [SND_PCM_CLASS_MULTI] = "multi",
992 [SND_PCM_CLASS_MODEM] = "modem",
993 [SND_PCM_CLASS_DIGITIZER] = "digitizer"
995 static const char * const class_table[SND_PCM_CLASS_LAST+1] = {
996 [SND_PCM_CLASS_GENERIC] = "sound",
997 [SND_PCM_CLASS_MULTI] = NULL,
998 [SND_PCM_CLASS_MODEM] = "modem",
999 [SND_PCM_CLASS_DIGITIZER] = NULL
1001 static const char * const alsa_subclass_table[SND_PCM_SUBCLASS_LAST+1] = {
1002 [SND_PCM_SUBCLASS_GENERIC_MIX] = "generic-mix",
1003 [SND_PCM_SUBCLASS_MULTI_MIX] = "multi-mix"
1006 snd_pcm_class_t class;
1007 snd_pcm_subclass_t subclass;
1008 const char *n, *id, *sdn;
1009 char *cn = NULL, *lcn = NULL;
1013 pa_assert(pcm_info);
1015 pa_proplist_sets(p, PA_PROP_DEVICE_API, "alsa");
1017 class = snd_pcm_info_get_class(pcm_info);
1018 if (class <= SND_PCM_CLASS_LAST) {
1019 if (class_table[class])
1020 pa_proplist_sets(p, PA_PROP_DEVICE_CLASS, class_table[class]);
1021 if (alsa_class_table[class])
1022 pa_proplist_sets(p, "alsa.class", alsa_class_table[class]);
1024 subclass = snd_pcm_info_get_subclass(pcm_info);
1025 if (subclass <= SND_PCM_SUBCLASS_LAST)
1026 if (alsa_subclass_table[subclass])
1027 pa_proplist_sets(p, "alsa.subclass", alsa_subclass_table[subclass]);
1029 if ((n = snd_pcm_info_get_name(pcm_info)))
1030 pa_proplist_sets(p, "alsa.name", n);
1032 if ((id = snd_pcm_info_get_id(pcm_info)))
1033 pa_proplist_sets(p, "alsa.id", id);
1035 pa_proplist_setf(p, "alsa.subdevice", "%u", snd_pcm_info_get_subdevice(pcm_info));
1036 if ((sdn = snd_pcm_info_get_subdevice_name(pcm_info)))
1037 pa_proplist_sets(p, "alsa.subdevice_name", sdn);
1039 pa_proplist_setf(p, "alsa.device", "%u", snd_pcm_info_get_device(pcm_info));
1041 if ((card = snd_pcm_info_get_card(pcm_info)) >= 0) {
1042 pa_proplist_setf(p, "alsa.card", "%i", card);
1044 if (snd_card_get_name(card, &cn) >= 0)
1045 pa_proplist_sets(p, "alsa.card_name", cn);
1047 if (snd_card_get_longname(card, &lcn) >= 0)
1048 pa_proplist_sets(p, "alsa.long_card_name", lcn);
1052 pa_proplist_setf(p, PA_PROP_DEVICE_DESCRIPTION, "%s - %s", cn, n);
1054 pa_proplist_sets(p, PA_PROP_DEVICE_DESCRIPTION, cn);
1056 pa_proplist_sets(p, PA_PROP_DEVICE_DESCRIPTION, n);
1062 int pa_alsa_recover_from_poll(snd_pcm_t *pcm, int revents) {
1063 snd_pcm_state_t state;
1068 if (revents & POLLERR)
1069 pa_log_debug("Got POLLERR from ALSA");
1070 if (revents & POLLNVAL)
1071 pa_log_warn("Got POLLNVAL from ALSA");
1072 if (revents & POLLHUP)
1073 pa_log_warn("Got POLLHUP from ALSA");
1074 if (revents & POLLPRI)
1075 pa_log_warn("Got POLLPRI from ALSA");
1076 if (revents & POLLIN)
1077 pa_log_debug("Got POLLIN from ALSA");
1078 if (revents & POLLOUT)
1079 pa_log_debug("Got POLLOUT from ALSA");
1081 state = snd_pcm_state(pcm);
1082 pa_log_debug("PCM state is %s", snd_pcm_state_name(state));
1084 /* Try to recover from this error */
1088 case SND_PCM_STATE_XRUN:
1089 if ((err = snd_pcm_recover(pcm, -EPIPE, 1)) != 0) {
1090 pa_log_warn("Could not recover from POLLERR|POLLNVAL|POLLHUP and XRUN: %s", snd_strerror(err));
1095 case SND_PCM_STATE_SUSPENDED:
1096 if ((err = snd_pcm_recover(pcm, -ESTRPIPE, 1)) != 0) {
1097 pa_log_warn("Could not recover from POLLERR|POLLNVAL|POLLHUP and SUSPENDED: %s", snd_strerror(err));
1106 if ((err = snd_pcm_prepare(pcm)) < 0) {
1107 pa_log_warn("Could not recover from POLLERR|POLLNVAL|POLLHUP with snd_pcm_prepare(): %s", snd_strerror(err));
1116 pa_rtpoll_item* pa_alsa_build_pollfd(snd_pcm_t *pcm, pa_rtpoll *rtpoll) {
1118 struct pollfd *pollfd;
1119 pa_rtpoll_item *item;
1123 if ((n = snd_pcm_poll_descriptors_count(pcm)) < 0) {
1124 pa_log("snd_pcm_poll_descriptors_count() failed: %s", snd_strerror(n));
1128 item = pa_rtpoll_item_new(rtpoll, PA_RTPOLL_NEVER, (unsigned) n);
1129 pollfd = pa_rtpoll_item_get_pollfd(item, NULL);
1131 if ((err = snd_pcm_poll_descriptors(pcm, pollfd, (unsigned) n)) < 0) {
1132 pa_log("snd_pcm_poll_descriptors() failed: %s", snd_strerror(err));
1133 pa_rtpoll_item_free(item);
1140 snd_pcm_sframes_t pa_alsa_safe_avail_update(snd_pcm_t *pcm, size_t hwbuf_size, const pa_sample_spec *ss) {
1141 snd_pcm_sframes_t n;
1145 pa_assert(hwbuf_size > 0);
1148 /* Some ALSA driver expose weird bugs, let's inform the user about
1149 * what is going on */
1151 n = snd_pcm_avail_update(pcm);
1156 k = (size_t) n * pa_frame_size(ss);
1158 if (k >= hwbuf_size * 3 ||
1159 k >= pa_bytes_per_second(ss)*10)
1160 pa_log("snd_pcm_avail_update() returned a value that is exceptionally large: %lu bytes (%lu ms) "
1161 "Most likely this is an ALSA driver bug. Please report this issue to the PulseAudio developers.",
1162 (unsigned long) k, (unsigned long) (pa_bytes_to_usec(k, ss) / PA_USEC_PER_MSEC));
1167 int pa_alsa_safe_mmap_begin(snd_pcm_t *pcm, const snd_pcm_channel_area_t **areas, snd_pcm_uframes_t *offset, snd_pcm_uframes_t *frames, size_t hwbuf_size, const pa_sample_spec *ss) {
1169 snd_pcm_uframes_t before;
1176 pa_assert(hwbuf_size > 0);
1181 r = snd_pcm_mmap_begin(pcm, areas, offset, frames);
1186 k = (size_t) *frames * pa_frame_size(ss);
1188 if (*frames > before ||
1189 k >= hwbuf_size * 3 ||
1190 k >= pa_bytes_per_second(ss)*10)
1192 pa_log("snd_pcm_mmap_begin() returned a value that is exceptionally large: %lu bytes (%lu ms) "
1193 "Most likely this is an ALSA driver bug. Please report this issue to the PulseAudio developers.",
1194 (unsigned long) k, (unsigned long) (pa_bytes_to_usec(k, ss) / PA_USEC_PER_MSEC));