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,
235 [PA_SAMPLE_S24LE] = SND_PCM_FORMAT_S24_3LE,
236 [PA_SAMPLE_S24BE] = SND_PCM_FORMAT_S24_3BE,
237 [PA_SAMPLE_S24_32LE] = SND_PCM_FORMAT_S24_LE,
238 [PA_SAMPLE_S24_32BE] = SND_PCM_FORMAT_S24_BE,
241 static const pa_sample_format_t try_order[] = {
260 pa_assert(pcm_handle);
263 if ((ret = snd_pcm_hw_params_set_format(pcm_handle, hwparams, format_trans[*f])) >= 0)
266 if (*f == PA_SAMPLE_FLOAT32BE)
267 *f = PA_SAMPLE_FLOAT32LE;
268 else if (*f == PA_SAMPLE_FLOAT32LE)
269 *f = PA_SAMPLE_FLOAT32BE;
270 else if (*f == PA_SAMPLE_S24BE)
271 *f = PA_SAMPLE_S24LE;
272 else if (*f == PA_SAMPLE_S24LE)
273 *f = PA_SAMPLE_S24BE;
274 else if (*f == PA_SAMPLE_S24_32BE)
275 *f = PA_SAMPLE_S24_32LE;
276 else if (*f == PA_SAMPLE_S24_32LE)
277 *f = PA_SAMPLE_S24_32BE;
278 else if (*f == PA_SAMPLE_S16BE)
279 *f = PA_SAMPLE_S16LE;
280 else if (*f == PA_SAMPLE_S16LE)
281 *f = PA_SAMPLE_S16BE;
282 else if (*f == PA_SAMPLE_S32BE)
283 *f = PA_SAMPLE_S32LE;
284 else if (*f == PA_SAMPLE_S32LE)
285 *f = PA_SAMPLE_S32BE;
289 if ((ret = snd_pcm_hw_params_set_format(pcm_handle, hwparams, format_trans[*f])) >= 0)
294 for (i = 0; try_order[i] != PA_SAMPLE_INVALID; i++) {
297 if ((ret = snd_pcm_hw_params_set_format(pcm_handle, hwparams, format_trans[*f])) >= 0)
304 /* Set the hardware parameters of the given ALSA device. Returns the
305 * selected fragment settings in *period and *period_size */
306 int pa_alsa_set_hw_params(
307 snd_pcm_t *pcm_handle,
310 snd_pcm_uframes_t *period_size,
311 snd_pcm_uframes_t tsched_size,
313 pa_bool_t *use_tsched,
314 pa_bool_t require_exact_channel_number) {
317 snd_pcm_uframes_t _period_size = period_size ? *period_size : 0;
318 unsigned int _periods = periods ? *periods : 0;
319 snd_pcm_uframes_t buffer_size;
320 unsigned int r = ss->rate;
321 unsigned int c = ss->channels;
322 pa_sample_format_t f = ss->format;
323 snd_pcm_hw_params_t *hwparams;
324 pa_bool_t _use_mmap = use_mmap && *use_mmap;
325 pa_bool_t _use_tsched = use_tsched && *use_tsched;
328 pa_assert(pcm_handle);
331 snd_pcm_hw_params_alloca(&hwparams);
333 if ((ret = snd_pcm_hw_params_any(pcm_handle, hwparams)) < 0)
336 if ((ret = snd_pcm_hw_params_set_rate_resample(pcm_handle, hwparams, 0)) < 0)
340 if ((ret = snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_MMAP_INTERLEAVED)) < 0) {
342 /* mmap() didn't work, fall back to interleaved */
344 if ((ret = snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
350 } else if ((ret = snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
356 if ((ret = set_format(pcm_handle, hwparams, &f)) < 0)
359 if ((ret = snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams, &r, NULL)) < 0)
362 if (require_exact_channel_number) {
363 if ((ret = snd_pcm_hw_params_set_channels(pcm_handle, hwparams, c)) < 0)
366 if ((ret = snd_pcm_hw_params_set_channels_near(pcm_handle, hwparams, &c)) < 0)
370 if ((ret = snd_pcm_hw_params_set_periods_integer(pcm_handle, hwparams)) < 0)
373 if (_period_size && tsched_size && _periods) {
374 /* Adjust the buffer sizes, if we didn't get the rate we were asking for */
375 _period_size = (snd_pcm_uframes_t) (((uint64_t) _period_size * r) / ss->rate);
376 tsched_size = (snd_pcm_uframes_t) (((uint64_t) tsched_size * r) / ss->rate);
379 _period_size = tsched_size;
382 pa_assert_se(snd_pcm_hw_params_get_buffer_size_max(hwparams, &buffer_size) == 0);
383 pa_log_debug("Maximum hw buffer size is %u ms", (unsigned) buffer_size * 1000 / r);
386 buffer_size = _periods * _period_size;
390 /* First we pass 0 as direction to get exactly what we asked
391 * for. That this is necessary is presumably a bug in ALSA */
394 if ((ret = snd_pcm_hw_params_set_periods_near(pcm_handle, hwparams, &_periods, &dir)) < 0) {
396 if ((ret = snd_pcm_hw_params_set_periods_near(pcm_handle, hwparams, &_periods, &dir)) < 0) {
398 if ((ret = snd_pcm_hw_params_set_periods_near(pcm_handle, hwparams, &_periods, &dir)) < 0)
404 if (_period_size > 0)
405 if ((ret = snd_pcm_hw_params_set_buffer_size_near(pcm_handle, hwparams, &buffer_size)) < 0)
409 if ((ret = snd_pcm_hw_params(pcm_handle, hwparams)) < 0)
413 pa_log_info("Device %s doesn't support %u Hz, changed to %u Hz.", snd_pcm_name(pcm_handle), ss->rate, r);
415 if (ss->channels != c)
416 pa_log_info("Device %s doesn't support %u channels, changed to %u.", snd_pcm_name(pcm_handle), ss->channels, c);
419 pa_log_info("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));
421 if ((ret = snd_pcm_prepare(pcm_handle)) < 0)
424 if ((ret = snd_pcm_hw_params_get_period_size(hwparams, &_period_size, &dir)) < 0 ||
425 (ret = snd_pcm_hw_params_get_periods(hwparams, &_periods, &dir)) < 0)
428 /* If the sample rate deviates too much, we need to resample */
429 if (r < ss->rate*.95 || r > ss->rate*1.05)
431 ss->channels = (uint8_t) c;
434 pa_assert(_periods > 0);
435 pa_assert(_period_size > 0);
441 *period_size = _period_size;
444 *use_mmap = _use_mmap;
447 *use_tsched = _use_tsched;
451 snd_pcm_nonblock(pcm_handle, 1);
458 int pa_alsa_set_sw_params(snd_pcm_t *pcm, snd_pcm_uframes_t avail_min) {
459 snd_pcm_sw_params_t *swparams;
464 snd_pcm_sw_params_alloca(&swparams);
466 if ((err = snd_pcm_sw_params_current(pcm, swparams) < 0)) {
467 pa_log_warn("Unable to determine current swparams: %s\n", snd_strerror(err));
471 if ((err = snd_pcm_sw_params_set_stop_threshold(pcm, swparams, (snd_pcm_uframes_t) -1)) < 0) {
472 pa_log_warn("Unable to set stop threshold: %s\n", snd_strerror(err));
476 if ((err = snd_pcm_sw_params_set_start_threshold(pcm, swparams, (snd_pcm_uframes_t) -1)) < 0) {
477 pa_log_warn("Unable to set start threshold: %s\n", snd_strerror(err));
481 if ((err = snd_pcm_sw_params_set_avail_min(pcm, swparams, avail_min)) < 0) {
482 pa_log_error("snd_pcm_sw_params_set_avail_min() failed: %s", snd_strerror(err));
486 if ((err = snd_pcm_sw_params(pcm, swparams)) < 0) {
487 pa_log_warn("Unable to set sw params: %s\n", snd_strerror(err));
494 static const struct pa_alsa_profile_info device_table[] = {
495 {{ 1, { PA_CHANNEL_POSITION_MONO }},
501 {{ 2, { PA_CHANNEL_POSITION_LEFT, PA_CHANNEL_POSITION_RIGHT }},
507 {{ 2, { PA_CHANNEL_POSITION_LEFT, PA_CHANNEL_POSITION_RIGHT }},
509 "IEC958 Digital Stereo",
513 {{ 2, { PA_CHANNEL_POSITION_LEFT, PA_CHANNEL_POSITION_RIGHT }},
515 "HDMI Digital Stereo",
519 {{ 4, { PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT,
520 PA_CHANNEL_POSITION_REAR_LEFT, PA_CHANNEL_POSITION_REAR_RIGHT }},
522 "Analog Surround 4.0",
523 "analog-surround-40",
526 {{ 4, { PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT,
527 PA_CHANNEL_POSITION_REAR_LEFT, PA_CHANNEL_POSITION_REAR_RIGHT }},
529 "IEC958/AC3 Digital Surround 4.0",
530 "iec958-ac3-surround-40",
533 {{ 5, { PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT,
534 PA_CHANNEL_POSITION_REAR_LEFT, PA_CHANNEL_POSITION_REAR_RIGHT,
535 PA_CHANNEL_POSITION_LFE }},
537 "Analog Surround 4.1",
538 "analog-surround-41",
541 {{ 5, { PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT,
542 PA_CHANNEL_POSITION_REAR_LEFT, PA_CHANNEL_POSITION_REAR_RIGHT,
543 PA_CHANNEL_POSITION_CENTER }},
545 "Analog Surround 5.0",
546 "analog-surround-50",
549 {{ 6, { PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT,
550 PA_CHANNEL_POSITION_REAR_LEFT, PA_CHANNEL_POSITION_REAR_RIGHT,
551 PA_CHANNEL_POSITION_CENTER, PA_CHANNEL_POSITION_LFE }},
553 "Analog Surround 5.1",
554 "analog-surround-51",
557 {{ 6, { PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_CENTER,
558 PA_CHANNEL_POSITION_FRONT_RIGHT, PA_CHANNEL_POSITION_REAR_LEFT,
559 PA_CHANNEL_POSITION_REAR_RIGHT, PA_CHANNEL_POSITION_LFE}},
561 "IEC958/AC3 Digital Surround 5.1",
562 "iec958-ac3-surround-51",
565 {{ 8, { PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT,
566 PA_CHANNEL_POSITION_REAR_LEFT, PA_CHANNEL_POSITION_REAR_RIGHT,
567 PA_CHANNEL_POSITION_CENTER, PA_CHANNEL_POSITION_LFE,
568 PA_CHANNEL_POSITION_SIDE_LEFT, PA_CHANNEL_POSITION_SIDE_RIGHT }},
570 "Analog Surround 7.1",
571 "analog-surround-71",
574 {{ 0, { 0 }}, NULL, NULL, NULL, 0 }
577 snd_pcm_t *pa_alsa_open_by_device_id_auto(
584 snd_pcm_uframes_t *period_size,
585 snd_pcm_uframes_t tsched_size,
587 pa_bool_t *use_tsched,
588 const pa_alsa_profile_info **profile) {
593 snd_pcm_t *pcm_handle;
600 pa_assert(period_size);
602 /* First we try to find a device string with a superset of the
603 * requested channel map and open it without the plug: prefix. We
604 * iterate through our device table from top to bottom and take
605 * the first that matches. If we didn't find a working device that
606 * way, we iterate backwards, and check all devices that do not
607 * provide a superset of the requested channel map.*/
612 if ((direction > 0) == pa_channel_map_superset(&device_table[i].map, map)) {
613 pa_sample_spec try_ss;
615 pa_log_debug("Checking for %s (%s)", device_table[i].name, device_table[i].alsa_name);
617 d = pa_sprintf_malloc("%s:%s", device_table[i].alsa_name, dev_id);
619 try_ss.channels = device_table[i].map.channels;
620 try_ss.rate = ss->rate;
621 try_ss.format = ss->format;
623 pcm_handle = pa_alsa_open_by_device_string(
641 *map = device_table[i].map;
642 pa_assert(map->channels == ss->channels);
645 *profile = &device_table[i];
652 if (!device_table[i+1].alsa_name) {
653 /* OK, so we are at the end of our list. Let's turn
657 /* We are not at the end of the list, so let's simply
658 * try the next entry */
665 if (device_table[i+1].alsa_name &&
666 device_table[i].map.channels == device_table[i+1].map.channels) {
668 /* OK, the next entry has the same number of channels,
673 /* Hmm, so the next entry does not have the same
674 * number of channels, so let's go backwards until we
675 * find the next entry with a differnt number of
678 for (i--; i >= 0; i--)
679 if (device_table[i].map.channels != device_table[i+1].map.channels)
682 /* Hmm, there is no entry with a different number of
683 * entries, then we're done */
687 /* OK, now lets find go back as long as we have the same number of channels */
689 if (device_table[i].map.channels != device_table[i-1].map.channels)
695 /* OK, we didn't find any good device, so let's try the raw plughw: stuff */
697 d = pa_sprintf_malloc("hw:%s", dev_id);
698 pa_log_debug("Trying %s as last resort...", d);
699 pcm_handle = pa_alsa_open_by_device_string(d, dev, ss, map, mode, nfrags, period_size, tsched_size, use_mmap, use_tsched, FALSE);
702 if (pcm_handle && profile)
708 snd_pcm_t *pa_alsa_open_by_device_id_profile(
715 snd_pcm_uframes_t *period_size,
716 snd_pcm_uframes_t tsched_size,
718 pa_bool_t *use_tsched,
719 const pa_alsa_profile_info *profile) {
722 snd_pcm_t *pcm_handle;
723 pa_sample_spec try_ss;
730 pa_assert(period_size);
733 d = pa_sprintf_malloc("%s:%s", profile->alsa_name, dev_id);
735 try_ss.channels = profile->map.channels;
736 try_ss.rate = ss->rate;
737 try_ss.format = ss->format;
739 pcm_handle = pa_alsa_open_by_device_string(
759 pa_assert(map->channels == ss->channels);
764 snd_pcm_t *pa_alsa_open_by_device_string(
771 snd_pcm_uframes_t *period_size,
772 snd_pcm_uframes_t tsched_size,
774 pa_bool_t *use_tsched,
775 pa_bool_t require_exact_channel_number) {
779 snd_pcm_t *pcm_handle;
780 pa_bool_t reformat = FALSE;
786 d = pa_xstrdup(device);
789 pa_log_debug("Trying %s %s SND_PCM_NO_AUTO_FORMAT ...", d, reformat ? "without" : "with");
791 /* We don't pass SND_PCM_NONBLOCK here, since alsa-lib <=
792 * 1.0.17a would then ignore the SND_PCM_NO_xxx flags. Instead
793 * we enable nonblock mode afterwards via
794 * snd_pcm_nonblock(). Also see
795 * http://mailman.alsa-project.org/pipermail/alsa-devel/2008-August/010258.html */
797 if ((err = snd_pcm_open(&pcm_handle, d, mode,
798 /*SND_PCM_NONBLOCK|*/
799 SND_PCM_NO_AUTO_RESAMPLE|
800 SND_PCM_NO_AUTO_CHANNELS|
801 (reformat ? 0 : SND_PCM_NO_AUTO_FORMAT))) < 0) {
802 pa_log_info("Error opening PCM device %s: %s", d, snd_strerror(err));
807 if ((err = pa_alsa_set_hw_params(pcm_handle, ss, nfrags, period_size, tsched_size, use_mmap, use_tsched, require_exact_channel_number)) < 0) {
812 snd_pcm_close(pcm_handle);
816 /* Hmm, some hw is very exotic, so we retry with plug, if without it didn't work */
818 if (!pa_startswith(d, "plug:") && !pa_startswith(d, "plughw:")) {
821 t = pa_sprintf_malloc("plug:%s", d);
827 snd_pcm_close(pcm_handle);
831 pa_log_info("Failed to set hardware parameters on %s: %s", d, snd_strerror(err));
833 snd_pcm_close(pcm_handle);
842 if (ss->channels != map->channels)
843 pa_channel_map_init_extend(map, ss->channels, PA_CHANNEL_MAP_ALSA);
849 int pa_alsa_probe_profiles(
851 const pa_sample_spec *ss,
852 void (*cb)(const pa_alsa_profile_info *sink, const pa_alsa_profile_info *source, void *userdata),
855 const pa_alsa_profile_info *i;
861 /* We try each combination of playback/capture. We also try to
862 * open only for capture resp. only for sink. Don't get confused
863 * by the trailing entry in device_table we use for this! */
865 for (i = device_table; i < device_table + PA_ELEMENTSOF(device_table); i++) {
866 const pa_alsa_profile_info *j;
867 snd_pcm_t *pcm_i = NULL;
871 pa_sample_spec try_ss;
872 pa_channel_map try_map;
874 pa_log_debug("Checking for playback on %s (%s)", i->name, i->alsa_name);
875 id = pa_sprintf_malloc("%s:%s", i->alsa_name, dev_id);
878 try_ss.channels = i->map.channels;
881 pcm_i = pa_alsa_open_by_device_string(
884 SND_PCM_STREAM_PLAYBACK,
885 NULL, NULL, 0, NULL, NULL,
894 for (j = device_table; j < device_table + PA_ELEMENTSOF(device_table); j++) {
895 snd_pcm_t *pcm_j = NULL;
899 pa_sample_spec try_ss;
900 pa_channel_map try_map;
902 pa_log_debug("Checking for capture on %s (%s)", j->name, j->alsa_name);
903 jd = pa_sprintf_malloc("%s:%s", j->alsa_name, dev_id);
906 try_ss.channels = j->map.channels;
909 pcm_j = pa_alsa_open_by_device_string(
912 SND_PCM_STREAM_CAPTURE,
913 NULL, NULL, 0, NULL, NULL,
923 snd_pcm_close(pcm_j);
925 if (i->alsa_name || j->alsa_name)
926 cb(i->alsa_name ? i : NULL,
927 j->alsa_name ? j : NULL, userdata);
931 snd_pcm_close(pcm_i);
937 int pa_alsa_prepare_mixer(snd_mixer_t *mixer, const char *dev) {
943 if ((err = snd_mixer_attach(mixer, dev)) < 0) {
944 pa_log_info("Unable to attach to mixer %s: %s", dev, snd_strerror(err));
948 if ((err = snd_mixer_selem_register(mixer, NULL, NULL)) < 0) {
949 pa_log_warn("Unable to register mixer: %s", snd_strerror(err));
953 if ((err = snd_mixer_load(mixer)) < 0) {
954 pa_log_warn("Unable to load mixer: %s", snd_strerror(err));
958 pa_log_info("Successfully attached to mixer '%s'", dev);
963 static pa_bool_t elem_has_volume(snd_mixer_elem_t *elem, pa_bool_t playback) {
966 if (playback && snd_mixer_selem_has_playback_volume(elem))
969 if (!playback && snd_mixer_selem_has_capture_volume(elem))
975 static pa_bool_t elem_has_switch(snd_mixer_elem_t *elem, pa_bool_t playback) {
978 if (playback && snd_mixer_selem_has_playback_switch(elem))
981 if (!playback && snd_mixer_selem_has_capture_switch(elem))
987 snd_mixer_elem_t *pa_alsa_find_elem(snd_mixer_t *mixer, const char *name, const char *fallback, pa_bool_t playback) {
988 snd_mixer_elem_t *elem = NULL, *fallback_elem = NULL;
989 snd_mixer_selem_id_t *sid = NULL;
991 snd_mixer_selem_id_alloca(&sid);
996 snd_mixer_selem_id_set_name(sid, name);
998 if ((elem = snd_mixer_find_selem(mixer, sid))) {
1000 if (elem_has_volume(elem, playback) &&
1001 elem_has_switch(elem, playback))
1004 if (!elem_has_volume(elem, playback) &&
1005 !elem_has_switch(elem, playback))
1009 pa_log_info("Cannot find mixer control \"%s\" or mixer control is no combination of switch/volume.", snd_mixer_selem_id_get_name(sid));
1012 snd_mixer_selem_id_set_name(sid, fallback);
1014 if ((fallback_elem = snd_mixer_find_selem(mixer, sid))) {
1016 if (elem_has_volume(fallback_elem, playback) &&
1017 elem_has_switch(fallback_elem, playback)) {
1018 elem = fallback_elem;
1022 if (!elem_has_volume(fallback_elem, playback) &&
1023 !elem_has_switch(fallback_elem, playback))
1024 fallback_elem = NULL;
1027 pa_log_warn("Cannot find fallback mixer control \"%s\" or mixer control is no combination of switch/volume.", snd_mixer_selem_id_get_name(sid));
1030 if (elem && fallback_elem) {
1032 /* Hmm, so we have both elements, but neither has both mute
1033 * and volume. Let's prefer the one with the volume */
1035 if (elem_has_volume(elem, playback))
1038 if (elem_has_volume(fallback_elem, playback)) {
1039 elem = fallback_elem;
1044 if (!elem && fallback_elem)
1045 elem = fallback_elem;
1050 pa_log_info("Using mixer control \"%s\".", snd_mixer_selem_id_get_name(sid));
1055 static const snd_mixer_selem_channel_id_t alsa_channel_ids[PA_CHANNEL_POSITION_MAX] = {
1056 [PA_CHANNEL_POSITION_MONO] = SND_MIXER_SCHN_MONO, /* The ALSA name is just an alias! */
1058 [PA_CHANNEL_POSITION_FRONT_CENTER] = SND_MIXER_SCHN_FRONT_CENTER,
1059 [PA_CHANNEL_POSITION_FRONT_LEFT] = SND_MIXER_SCHN_FRONT_LEFT,
1060 [PA_CHANNEL_POSITION_FRONT_RIGHT] = SND_MIXER_SCHN_FRONT_RIGHT,
1062 [PA_CHANNEL_POSITION_REAR_CENTER] = SND_MIXER_SCHN_REAR_CENTER,
1063 [PA_CHANNEL_POSITION_REAR_LEFT] = SND_MIXER_SCHN_REAR_LEFT,
1064 [PA_CHANNEL_POSITION_REAR_RIGHT] = SND_MIXER_SCHN_REAR_RIGHT,
1066 [PA_CHANNEL_POSITION_LFE] = SND_MIXER_SCHN_WOOFER,
1068 [PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER] = SND_MIXER_SCHN_UNKNOWN,
1069 [PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER] = SND_MIXER_SCHN_UNKNOWN,
1071 [PA_CHANNEL_POSITION_SIDE_LEFT] = SND_MIXER_SCHN_SIDE_LEFT,
1072 [PA_CHANNEL_POSITION_SIDE_RIGHT] = SND_MIXER_SCHN_SIDE_RIGHT,
1074 [PA_CHANNEL_POSITION_AUX0] = SND_MIXER_SCHN_UNKNOWN,
1075 [PA_CHANNEL_POSITION_AUX1] = SND_MIXER_SCHN_UNKNOWN,
1076 [PA_CHANNEL_POSITION_AUX2] = SND_MIXER_SCHN_UNKNOWN,
1077 [PA_CHANNEL_POSITION_AUX3] = SND_MIXER_SCHN_UNKNOWN,
1078 [PA_CHANNEL_POSITION_AUX4] = SND_MIXER_SCHN_UNKNOWN,
1079 [PA_CHANNEL_POSITION_AUX5] = SND_MIXER_SCHN_UNKNOWN,
1080 [PA_CHANNEL_POSITION_AUX6] = SND_MIXER_SCHN_UNKNOWN,
1081 [PA_CHANNEL_POSITION_AUX7] = SND_MIXER_SCHN_UNKNOWN,
1082 [PA_CHANNEL_POSITION_AUX8] = SND_MIXER_SCHN_UNKNOWN,
1083 [PA_CHANNEL_POSITION_AUX9] = SND_MIXER_SCHN_UNKNOWN,
1084 [PA_CHANNEL_POSITION_AUX10] = SND_MIXER_SCHN_UNKNOWN,
1085 [PA_CHANNEL_POSITION_AUX11] = SND_MIXER_SCHN_UNKNOWN,
1086 [PA_CHANNEL_POSITION_AUX12] = SND_MIXER_SCHN_UNKNOWN,
1087 [PA_CHANNEL_POSITION_AUX13] = SND_MIXER_SCHN_UNKNOWN,
1088 [PA_CHANNEL_POSITION_AUX14] = SND_MIXER_SCHN_UNKNOWN,
1089 [PA_CHANNEL_POSITION_AUX15] = SND_MIXER_SCHN_UNKNOWN,
1090 [PA_CHANNEL_POSITION_AUX16] = SND_MIXER_SCHN_UNKNOWN,
1091 [PA_CHANNEL_POSITION_AUX17] = SND_MIXER_SCHN_UNKNOWN,
1092 [PA_CHANNEL_POSITION_AUX18] = SND_MIXER_SCHN_UNKNOWN,
1093 [PA_CHANNEL_POSITION_AUX19] = SND_MIXER_SCHN_UNKNOWN,
1094 [PA_CHANNEL_POSITION_AUX20] = SND_MIXER_SCHN_UNKNOWN,
1095 [PA_CHANNEL_POSITION_AUX21] = SND_MIXER_SCHN_UNKNOWN,
1096 [PA_CHANNEL_POSITION_AUX22] = SND_MIXER_SCHN_UNKNOWN,
1097 [PA_CHANNEL_POSITION_AUX23] = SND_MIXER_SCHN_UNKNOWN,
1098 [PA_CHANNEL_POSITION_AUX24] = SND_MIXER_SCHN_UNKNOWN,
1099 [PA_CHANNEL_POSITION_AUX25] = SND_MIXER_SCHN_UNKNOWN,
1100 [PA_CHANNEL_POSITION_AUX26] = SND_MIXER_SCHN_UNKNOWN,
1101 [PA_CHANNEL_POSITION_AUX27] = SND_MIXER_SCHN_UNKNOWN,
1102 [PA_CHANNEL_POSITION_AUX28] = SND_MIXER_SCHN_UNKNOWN,
1103 [PA_CHANNEL_POSITION_AUX29] = SND_MIXER_SCHN_UNKNOWN,
1104 [PA_CHANNEL_POSITION_AUX30] = SND_MIXER_SCHN_UNKNOWN,
1105 [PA_CHANNEL_POSITION_AUX31] = SND_MIXER_SCHN_UNKNOWN,
1107 [PA_CHANNEL_POSITION_TOP_CENTER] = SND_MIXER_SCHN_UNKNOWN,
1109 [PA_CHANNEL_POSITION_TOP_FRONT_CENTER] = SND_MIXER_SCHN_UNKNOWN,
1110 [PA_CHANNEL_POSITION_TOP_FRONT_LEFT] = SND_MIXER_SCHN_UNKNOWN,
1111 [PA_CHANNEL_POSITION_TOP_FRONT_RIGHT] = SND_MIXER_SCHN_UNKNOWN,
1113 [PA_CHANNEL_POSITION_TOP_REAR_CENTER] = SND_MIXER_SCHN_UNKNOWN,
1114 [PA_CHANNEL_POSITION_TOP_REAR_LEFT] = SND_MIXER_SCHN_UNKNOWN,
1115 [PA_CHANNEL_POSITION_TOP_REAR_RIGHT] = SND_MIXER_SCHN_UNKNOWN
1119 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) {
1121 pa_bool_t alsa_channel_used[SND_MIXER_SCHN_LAST];
1122 pa_bool_t mono_used = FALSE;
1125 pa_assert(channel_map);
1126 pa_assert(mixer_map);
1128 memset(&alsa_channel_used, 0, sizeof(alsa_channel_used));
1130 if (channel_map->channels > 1 &&
1131 ((playback && snd_mixer_selem_has_playback_volume_joined(elem)) ||
1132 (!playback && snd_mixer_selem_has_capture_volume_joined(elem)))) {
1133 pa_log_info("ALSA device lacks independant volume controls for each channel.");
1137 for (i = 0; i < channel_map->channels; i++) {
1138 snd_mixer_selem_channel_id_t id;
1141 is_mono = channel_map->map[i] == PA_CHANNEL_POSITION_MONO;
1142 id = alsa_channel_ids[channel_map->map[i]];
1144 if (!is_mono && id == SND_MIXER_SCHN_UNKNOWN) {
1145 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]));
1149 if ((is_mono && mono_used) || (!is_mono && alsa_channel_used[id])) {
1150 pa_log_info("Channel map has duplicate channel '%s', falling back to software volume control.", pa_channel_position_to_string(channel_map->map[i]));
1154 if ((playback && (!snd_mixer_selem_has_playback_channel(elem, id) || (is_mono && !snd_mixer_selem_is_playback_mono(elem)))) ||
1155 (!playback && (!snd_mixer_selem_has_capture_channel(elem, id) || (is_mono && !snd_mixer_selem_is_capture_mono(elem))))) {
1157 pa_log_info("ALSA device lacks separate volumes control for channel '%s'", pa_channel_position_to_string(channel_map->map[i]));
1162 mixer_map[i] = SND_MIXER_SCHN_MONO;
1166 alsa_channel_used[id] = TRUE;
1170 pa_log_info("All %u channels can be mapped to mixer channels.", channel_map->channels);
1175 void pa_alsa_dump(snd_pcm_t *pcm) {
1181 pa_assert_se(snd_output_buffer_open(&out) == 0);
1183 if ((err = snd_pcm_dump(pcm, out)) < 0)
1184 pa_log_debug("snd_pcm_dump(): %s", snd_strerror(err));
1187 snd_output_buffer_string(out, &s);
1188 pa_log_debug("snd_pcm_dump():\n%s", pa_strnull(s));
1191 pa_assert_se(snd_output_close(out) == 0);
1194 void pa_alsa_dump_status(snd_pcm_t *pcm) {
1197 snd_pcm_status_t *status;
1201 snd_pcm_status_alloca(&status);
1203 pa_assert_se(snd_output_buffer_open(&out) == 0);
1205 pa_assert_se(snd_pcm_status(pcm, status) == 0);
1207 if ((err = snd_pcm_status_dump(status, out)) < 0)
1208 pa_log_debug("snd_pcm_dump(): %s", snd_strerror(err));
1211 snd_output_buffer_string(out, &s);
1212 pa_log_debug("snd_pcm_dump():\n%s", pa_strnull(s));
1215 pa_assert_se(snd_output_close(out) == 0);
1218 static void alsa_error_handler(const char *file, int line, const char *function, int err, const char *fmt,...) {
1222 alsa_file = pa_sprintf_malloc("(alsa-lib)%s", file);
1226 pa_log_levelv_meta(PA_LOG_INFO, alsa_file, line, function, fmt, ap);
1230 pa_xfree(alsa_file);
1233 static pa_atomic_t n_error_handler_installed = PA_ATOMIC_INIT(0);
1235 void pa_alsa_redirect_errors_inc(void) {
1236 /* This is not really thread safe, but we do our best */
1238 if (pa_atomic_inc(&n_error_handler_installed) == 0)
1239 snd_lib_error_set_handler(alsa_error_handler);
1242 void pa_alsa_redirect_errors_dec(void) {
1245 pa_assert_se((r = pa_atomic_dec(&n_error_handler_installed)) >= 1);
1248 snd_lib_error_set_handler(NULL);
1251 void pa_alsa_init_proplist_card(pa_proplist *p, int card) {
1255 pa_assert(card >= 0);
1257 pa_proplist_setf(p, "alsa.card", "%i", card);
1259 if (snd_card_get_name(card, &cn) >= 0) {
1260 pa_proplist_sets(p, "alsa.card_name", cn);
1264 if (snd_card_get_longname(card, &lcn) >= 0) {
1265 pa_proplist_sets(p, "alsa.long_card_name", lcn);
1270 void pa_alsa_init_proplist_pcm(pa_proplist *p, snd_pcm_info_t *pcm_info) {
1272 static const char * const alsa_class_table[SND_PCM_CLASS_LAST+1] = {
1273 [SND_PCM_CLASS_GENERIC] = "generic",
1274 [SND_PCM_CLASS_MULTI] = "multi",
1275 [SND_PCM_CLASS_MODEM] = "modem",
1276 [SND_PCM_CLASS_DIGITIZER] = "digitizer"
1278 static const char * const class_table[SND_PCM_CLASS_LAST+1] = {
1279 [SND_PCM_CLASS_GENERIC] = "sound",
1280 [SND_PCM_CLASS_MULTI] = NULL,
1281 [SND_PCM_CLASS_MODEM] = "modem",
1282 [SND_PCM_CLASS_DIGITIZER] = NULL
1284 static const char * const alsa_subclass_table[SND_PCM_SUBCLASS_LAST+1] = {
1285 [SND_PCM_SUBCLASS_GENERIC_MIX] = "generic-mix",
1286 [SND_PCM_SUBCLASS_MULTI_MIX] = "multi-mix"
1289 snd_pcm_class_t class;
1290 snd_pcm_subclass_t subclass;
1291 const char *n, *id, *sdn, *cn;
1295 pa_assert(pcm_info);
1297 pa_proplist_sets(p, PA_PROP_DEVICE_API, "alsa");
1299 class = snd_pcm_info_get_class(pcm_info);
1300 if (class <= SND_PCM_CLASS_LAST) {
1301 if (class_table[class])
1302 pa_proplist_sets(p, PA_PROP_DEVICE_CLASS, class_table[class]);
1303 if (alsa_class_table[class])
1304 pa_proplist_sets(p, "alsa.class", alsa_class_table[class]);
1306 subclass = snd_pcm_info_get_subclass(pcm_info);
1307 if (subclass <= SND_PCM_SUBCLASS_LAST)
1308 if (alsa_subclass_table[subclass])
1309 pa_proplist_sets(p, "alsa.subclass", alsa_subclass_table[subclass]);
1311 if ((n = snd_pcm_info_get_name(pcm_info)))
1312 pa_proplist_sets(p, "alsa.name", n);
1314 if ((id = snd_pcm_info_get_id(pcm_info)))
1315 pa_proplist_sets(p, "alsa.id", id);
1317 pa_proplist_setf(p, "alsa.subdevice", "%u", snd_pcm_info_get_subdevice(pcm_info));
1318 if ((sdn = snd_pcm_info_get_subdevice_name(pcm_info)))
1319 pa_proplist_sets(p, "alsa.subdevice_name", sdn);
1321 pa_proplist_setf(p, "alsa.device", "%u", snd_pcm_info_get_device(pcm_info));
1323 if ((card = snd_pcm_info_get_card(pcm_info)) >= 0) {
1324 pa_alsa_init_proplist_card(p, card);
1325 cn = pa_proplist_gets(p, "alsa.card_name");
1329 pa_proplist_setf(p, PA_PROP_DEVICE_DESCRIPTION, "%s - %s", cn, n);
1331 pa_proplist_sets(p, PA_PROP_DEVICE_DESCRIPTION, cn);
1333 pa_proplist_sets(p, PA_PROP_DEVICE_DESCRIPTION, n);
1336 int pa_alsa_recover_from_poll(snd_pcm_t *pcm, int revents) {
1337 snd_pcm_state_t state;
1342 if (revents & POLLERR)
1343 pa_log_debug("Got POLLERR from ALSA");
1344 if (revents & POLLNVAL)
1345 pa_log_warn("Got POLLNVAL from ALSA");
1346 if (revents & POLLHUP)
1347 pa_log_warn("Got POLLHUP from ALSA");
1348 if (revents & POLLPRI)
1349 pa_log_warn("Got POLLPRI from ALSA");
1350 if (revents & POLLIN)
1351 pa_log_debug("Got POLLIN from ALSA");
1352 if (revents & POLLOUT)
1353 pa_log_debug("Got POLLOUT from ALSA");
1355 state = snd_pcm_state(pcm);
1356 pa_log_debug("PCM state is %s", snd_pcm_state_name(state));
1358 /* Try to recover from this error */
1362 case SND_PCM_STATE_XRUN:
1363 if ((err = snd_pcm_recover(pcm, -EPIPE, 1)) != 0) {
1364 pa_log_warn("Could not recover from POLLERR|POLLNVAL|POLLHUP and XRUN: %s", snd_strerror(err));
1369 case SND_PCM_STATE_SUSPENDED:
1370 if ((err = snd_pcm_recover(pcm, -ESTRPIPE, 1)) != 0) {
1371 pa_log_warn("Could not recover from POLLERR|POLLNVAL|POLLHUP and SUSPENDED: %s", snd_strerror(err));
1380 if ((err = snd_pcm_prepare(pcm)) < 0) {
1381 pa_log_warn("Could not recover from POLLERR|POLLNVAL|POLLHUP with snd_pcm_prepare(): %s", snd_strerror(err));
1390 pa_rtpoll_item* pa_alsa_build_pollfd(snd_pcm_t *pcm, pa_rtpoll *rtpoll) {
1392 struct pollfd *pollfd;
1393 pa_rtpoll_item *item;
1397 if ((n = snd_pcm_poll_descriptors_count(pcm)) < 0) {
1398 pa_log("snd_pcm_poll_descriptors_count() failed: %s", snd_strerror(n));
1402 item = pa_rtpoll_item_new(rtpoll, PA_RTPOLL_NEVER, (unsigned) n);
1403 pollfd = pa_rtpoll_item_get_pollfd(item, NULL);
1405 if ((err = snd_pcm_poll_descriptors(pcm, pollfd, (unsigned) n)) < 0) {
1406 pa_log("snd_pcm_poll_descriptors() failed: %s", snd_strerror(err));
1407 pa_rtpoll_item_free(item);
1414 snd_pcm_sframes_t pa_alsa_safe_avail_update(snd_pcm_t *pcm, size_t hwbuf_size, const pa_sample_spec *ss) {
1415 snd_pcm_sframes_t n;
1419 pa_assert(hwbuf_size > 0);
1422 /* Some ALSA driver expose weird bugs, let's inform the user about
1423 * what is going on */
1425 n = snd_pcm_avail_update(pcm);
1430 k = (size_t) n * pa_frame_size(ss);
1432 if (k >= hwbuf_size * 3 ||
1433 k >= pa_bytes_per_second(ss)*10)
1434 pa_log("snd_pcm_avail_update() returned a value that is exceptionally large: %lu bytes (%lu ms) "
1435 "Most likely this is an ALSA driver bug. Please report this issue to the PulseAudio developers.",
1436 (unsigned long) k, (unsigned long) (pa_bytes_to_usec(k, ss) / PA_USEC_PER_MSEC));
1441 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) {
1443 snd_pcm_uframes_t before;
1450 pa_assert(hwbuf_size > 0);
1455 r = snd_pcm_mmap_begin(pcm, areas, offset, frames);
1460 k = (size_t) *frames * pa_frame_size(ss);
1462 if (*frames > before ||
1463 k >= hwbuf_size * 3 ||
1464 k >= pa_bytes_per_second(ss)*10)
1466 pa_log("snd_pcm_mmap_begin() returned a value that is exceptionally large: %lu bytes (%lu ms) "
1467 "Most likely this is an ALSA driver bug. Please report this issue to the PulseAudio developers.",
1468 (unsigned long) k, (unsigned long) (pa_bytes_to_usec(k, ss) / PA_USEC_PER_MSEC));