4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public License
20 along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
29 #include <sys/types.h>
30 #include <asoundlib.h>
32 #include <pulse/sample.h>
33 #include <pulse/xmalloc.h>
35 #include <pulsecore/log.h>
36 #include <pulsecore/macro.h>
37 #include <pulsecore/core-util.h>
39 #include "alsa-util.h"
41 struct pa_alsa_fdlist {
44 /* This is a temporary buffer used to avoid lots of mallocs */
45 struct pollfd *work_fds;
50 pa_defer_event *defer;
55 void (*cb)(void *userdata);
59 static void io_cb(pa_mainloop_api*a, pa_io_event* e, PA_GCC_UNUSED int fd, pa_io_event_flags_t events, void *userdata) {
61 struct pa_alsa_fdlist *fdl = userdata;
63 unsigned short revents;
67 pa_assert(fdl->mixer);
69 pa_assert(fdl->work_fds);
76 memcpy(fdl->work_fds, fdl->fds, sizeof(struct pollfd) * fdl->num_fds);
78 for (i = 0;i < fdl->num_fds; i++) {
79 if (e == fdl->ios[i]) {
80 if (events & PA_IO_EVENT_INPUT)
81 fdl->work_fds[i].revents |= POLLIN;
82 if (events & PA_IO_EVENT_OUTPUT)
83 fdl->work_fds[i].revents |= POLLOUT;
84 if (events & PA_IO_EVENT_ERROR)
85 fdl->work_fds[i].revents |= POLLERR;
86 if (events & PA_IO_EVENT_HANGUP)
87 fdl->work_fds[i].revents |= POLLHUP;
92 pa_assert(i != fdl->num_fds);
94 if ((err = snd_mixer_poll_descriptors_revents(fdl->mixer, fdl->work_fds, fdl->num_fds, &revents)) < 0) {
95 pa_log_error("Unable to get poll revent: %s", snd_strerror(err));
99 a->defer_enable(fdl->defer, 1);
102 snd_mixer_handle_events(fdl->mixer);
105 static void defer_cb(pa_mainloop_api*a, PA_GCC_UNUSED pa_defer_event* e, void *userdata) {
106 struct pa_alsa_fdlist *fdl = userdata;
112 pa_assert(fdl->mixer);
114 a->defer_enable(fdl->defer, 0);
116 num_fds = snd_mixer_poll_descriptors_count(fdl->mixer);
117 pa_assert(num_fds > 0);
119 if (num_fds != fdl->num_fds) {
123 pa_xfree(fdl->work_fds);
124 fdl->fds = pa_xnew0(struct pollfd, num_fds);
125 fdl->work_fds = pa_xnew(struct pollfd, num_fds);
128 memset(fdl->work_fds, 0, sizeof(struct pollfd) * num_fds);
130 if ((err = snd_mixer_poll_descriptors(fdl->mixer, fdl->work_fds, num_fds)) < 0) {
131 pa_log_error("Unable to get poll descriptors: %s", snd_strerror(err));
137 if (memcmp(fdl->fds, fdl->work_fds, sizeof(struct pollfd) * num_fds) == 0)
141 for (i = 0; i < fdl->num_fds; i++)
142 a->io_free(fdl->ios[i]);
144 if (num_fds != fdl->num_fds) {
151 fdl->ios = pa_xnew(pa_io_event*, num_fds);
154 temp = fdl->work_fds;
155 fdl->work_fds = fdl->fds;
158 fdl->num_fds = num_fds;
160 for (i = 0;i < num_fds;i++)
161 fdl->ios[i] = a->io_new(a, fdl->fds[i].fd,
162 ((fdl->fds[i].events & POLLIN) ? PA_IO_EVENT_INPUT : 0) |
163 ((fdl->fds[i].events & POLLOUT) ? PA_IO_EVENT_OUTPUT : 0),
167 struct pa_alsa_fdlist *pa_alsa_fdlist_new(void) {
168 struct pa_alsa_fdlist *fdl;
170 fdl = pa_xnew0(struct pa_alsa_fdlist, 1);
174 fdl->work_fds = NULL;
184 void pa_alsa_fdlist_free(struct pa_alsa_fdlist *fdl) {
189 fdl->m->defer_free(fdl->defer);
195 for (i = 0;i < fdl->num_fds;i++)
196 fdl->m->io_free(fdl->ios[i]);
203 pa_xfree(fdl->work_fds);
208 int pa_alsa_fdlist_set_mixer(struct pa_alsa_fdlist *fdl, snd_mixer_t *mixer_handle, pa_mainloop_api* m) {
210 pa_assert(mixer_handle);
214 fdl->mixer = mixer_handle;
216 fdl->defer = m->defer_new(m, defer_cb, fdl);
221 static int set_format(snd_pcm_t *pcm_handle, snd_pcm_hw_params_t *hwparams, pa_sample_format_t *f) {
223 static const snd_pcm_format_t format_trans[] = {
224 [PA_SAMPLE_U8] = SND_PCM_FORMAT_U8,
225 [PA_SAMPLE_ALAW] = SND_PCM_FORMAT_A_LAW,
226 [PA_SAMPLE_ULAW] = SND_PCM_FORMAT_MU_LAW,
227 [PA_SAMPLE_S16LE] = SND_PCM_FORMAT_S16_LE,
228 [PA_SAMPLE_S16BE] = SND_PCM_FORMAT_S16_BE,
229 [PA_SAMPLE_FLOAT32LE] = SND_PCM_FORMAT_FLOAT_LE,
230 [PA_SAMPLE_FLOAT32BE] = SND_PCM_FORMAT_FLOAT_BE,
231 [PA_SAMPLE_S32LE] = SND_PCM_FORMAT_S32_LE,
232 [PA_SAMPLE_S32BE] = SND_PCM_FORMAT_S32_BE,
235 static const pa_sample_format_t try_order[] = {
250 pa_assert(pcm_handle);
253 if ((ret = snd_pcm_hw_params_set_format(pcm_handle, hwparams, format_trans[*f])) >= 0)
256 if (*f == PA_SAMPLE_FLOAT32BE)
257 *f = PA_SAMPLE_FLOAT32LE;
258 else if (*f == PA_SAMPLE_FLOAT32LE)
259 *f = PA_SAMPLE_FLOAT32BE;
260 else if (*f == PA_SAMPLE_S16BE)
261 *f = PA_SAMPLE_S16LE;
262 else if (*f == PA_SAMPLE_S16LE)
263 *f = PA_SAMPLE_S16BE;
264 else if (*f == PA_SAMPLE_S32BE)
265 *f = PA_SAMPLE_S32LE;
266 else if (*f == PA_SAMPLE_S32LE)
267 *f = PA_SAMPLE_S32BE;
271 if ((ret = snd_pcm_hw_params_set_format(pcm_handle, hwparams, format_trans[*f])) >= 0)
276 for (i = 0; try_order[i] != PA_SAMPLE_INVALID; i++) {
279 if ((ret = snd_pcm_hw_params_set_format(pcm_handle, hwparams, format_trans[*f])) >= 0)
286 /* Set the hardware parameters of the given ALSA device. Returns the
287 * selected fragment settings in *period and *period_size */
288 int pa_alsa_set_hw_params(
289 snd_pcm_t *pcm_handle,
292 snd_pcm_uframes_t *period_size,
294 pa_bool_t require_exact_channel_number) {
297 snd_pcm_uframes_t buffer_size;
298 unsigned int r = ss->rate;
299 unsigned int c = ss->channels;
300 pa_sample_format_t f = ss->format;
301 snd_pcm_hw_params_t *hwparams;
302 pa_bool_t _use_mmap = use_mmap && *use_mmap;
304 pa_assert(pcm_handle);
307 pa_assert(period_size);
309 snd_pcm_hw_params_alloca(&hwparams);
311 buffer_size = *periods * *period_size;
313 if ((ret = snd_pcm_hw_params_any(pcm_handle, hwparams)) < 0)
316 if ((ret = snd_pcm_hw_params_set_rate_resample(pcm_handle, hwparams, 0)) < 0)
320 if ((ret = snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_MMAP_INTERLEAVED)) < 0) {
322 /* mmap() didn't work, fall back to interleaved */
324 if ((ret = snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
330 } else if ((ret = snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
333 if ((ret = set_format(pcm_handle, hwparams, &f)) < 0)
336 if ((ret = snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams, &r, NULL)) < 0)
339 if (require_exact_channel_number) {
340 if ((ret = snd_pcm_hw_params_set_channels(pcm_handle, hwparams, c)) < 0)
343 if ((ret = snd_pcm_hw_params_set_channels_near(pcm_handle, hwparams, &c)) < 0)
347 if ((*period_size > 0 && (ret = snd_pcm_hw_params_set_period_size_near(pcm_handle, hwparams, period_size, NULL)) < 0) ||
348 (*periods > 0 && (ret = snd_pcm_hw_params_set_buffer_size_near(pcm_handle, hwparams, &buffer_size)) < 0))
351 if ((ret = snd_pcm_hw_params(pcm_handle, hwparams)) < 0)
355 pa_log_warn("Device %s doesn't support %u Hz, changed to %u Hz.", snd_pcm_name(pcm_handle), ss->rate, r);
357 if (ss->channels != c)
358 pa_log_warn("Device %s doesn't support %u channels, changed to %u.", snd_pcm_name(pcm_handle), ss->channels, c);
361 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));
363 if ((ret = snd_pcm_prepare(pcm_handle)) < 0)
366 if ((ret = snd_pcm_hw_params_get_buffer_size(hwparams, &buffer_size)) < 0 ||
367 (ret = snd_pcm_hw_params_get_period_size(hwparams, period_size, NULL)) < 0)
370 /* If the sample rate deviates too much, we need to resample */
371 if (r < ss->rate*.95 || r > ss->rate*1.05)
376 pa_assert(buffer_size > 0);
377 pa_assert(*period_size > 0);
378 *periods = buffer_size / *period_size;
379 pa_assert(*periods > 0);
382 *use_mmap = _use_mmap;
391 int pa_alsa_set_sw_params(snd_pcm_t *pcm) {
392 snd_pcm_sw_params_t *swparams;
397 snd_pcm_sw_params_alloca(&swparams);
399 if ((err = snd_pcm_sw_params_current(pcm, swparams) < 0)) {
400 pa_log_warn("Unable to determine current swparams: %s\n", snd_strerror(err));
404 if ((err = snd_pcm_sw_params_set_stop_threshold(pcm, swparams, (snd_pcm_uframes_t) -1)) < 0) {
405 pa_log_warn("Unable to set stop threshold: %s\n", snd_strerror(err));
409 if ((err = snd_pcm_sw_params_set_start_threshold(pcm, swparams, (snd_pcm_uframes_t) -1)) < 0) {
410 pa_log_warn("Unable to set start threshold: %s\n", snd_strerror(err));
414 if ((err = snd_pcm_sw_params(pcm, swparams)) < 0) {
415 pa_log_warn("Unable to set sw params: %s\n", snd_strerror(err));
427 static const struct device_info device_table[] = {
428 {{ 2, { PA_CHANNEL_POSITION_LEFT, PA_CHANNEL_POSITION_RIGHT } }, "front" },
430 {{ 4, { PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT,
431 PA_CHANNEL_POSITION_REAR_LEFT, PA_CHANNEL_POSITION_REAR_RIGHT }}, "surround40" },
433 {{ 5, { PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT,
434 PA_CHANNEL_POSITION_REAR_LEFT, PA_CHANNEL_POSITION_REAR_RIGHT,
435 PA_CHANNEL_POSITION_LFE }}, "surround41" },
437 {{ 5, { PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT,
438 PA_CHANNEL_POSITION_REAR_LEFT, PA_CHANNEL_POSITION_REAR_RIGHT,
439 PA_CHANNEL_POSITION_CENTER }}, "surround50" },
441 {{ 6, { PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT,
442 PA_CHANNEL_POSITION_REAR_LEFT, PA_CHANNEL_POSITION_REAR_RIGHT,
443 PA_CHANNEL_POSITION_CENTER, PA_CHANNEL_POSITION_LFE }}, "surround51" },
445 {{ 8, { PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT,
446 PA_CHANNEL_POSITION_REAR_LEFT, PA_CHANNEL_POSITION_REAR_RIGHT,
447 PA_CHANNEL_POSITION_CENTER, PA_CHANNEL_POSITION_LFE,
448 PA_CHANNEL_POSITION_SIDE_LEFT, PA_CHANNEL_POSITION_SIDE_RIGHT }} , "surround71" },
453 static pa_bool_t channel_map_superset(const pa_channel_map *a, const pa_channel_map *b) {
454 pa_bool_t in_a[PA_CHANNEL_POSITION_MAX];
460 memset(in_a, 0, sizeof(in_a));
462 for (i = 0; i < a->channels; i++)
463 in_a[a->map[i]] = TRUE;
465 for (i = 0; i < b->channels; i++)
466 if (!in_a[b->map[i]])
472 snd_pcm_t *pa_alsa_open_by_device_id(
479 snd_pcm_uframes_t *period_size,
480 pa_bool_t *use_mmap) {
486 snd_pcm_t *pcm_handle;
493 pa_assert(period_size);
495 /* First we try to find a device string with a superset of the
496 * requested channel map and open it without the plug: prefix. We
497 * iterate through our device table from top to bottom and take
498 * the first that matches. If we didn't find a working device that
499 * way, we iterate backwards, and check all devices that do not
500 * provide a superset of the requested channel map.*/
502 for (i = 0;; i += direction) {
503 pa_sample_spec try_ss;
506 pa_assert(direction == -1);
508 /* OK, so we iterated backwards, and now are at the
509 * beginning of our list. */
513 } else if (!device_table[i].name) {
514 pa_assert(direction == 1);
516 /* OK, so we are at the end of our list. at iterated
523 if ((direction > 0) == !channel_map_superset(&device_table[i].map, map))
526 d = pa_sprintf_malloc("%s:%s", device_table[i].name, dev_id);
527 pa_log_debug("Trying %s...", d);
529 if ((err = snd_pcm_open(&pcm_handle, d, mode, SND_PCM_NONBLOCK)) < 0) {
530 pa_log_info("Couldn't open PCM device %s: %s", d, snd_strerror(err));
535 try_ss.channels = device_table[i].map.channels;
536 try_ss.rate = ss->rate;
537 try_ss.format = ss->format;
539 if ((err = pa_alsa_set_hw_params(pcm_handle, &try_ss, nfrags, period_size, use_mmap, TRUE)) < 0) {
540 pa_log_info("PCM device %s refused our hw parameters: %s", d, snd_strerror(err));
542 snd_pcm_close(pcm_handle);
547 *map = device_table[i].map;
548 pa_assert(map->channels == ss->channels);
553 /* OK, we didn't find any good device, so let's try the raw hw: stuff */
555 d = pa_sprintf_malloc("hw:%s", dev_id);
556 pa_log_debug("Trying %s as last resort...", d);
557 pcm_handle = pa_alsa_open_by_device_string(d, dev, ss, map, mode, nfrags, period_size, use_mmap);
563 snd_pcm_t *pa_alsa_open_by_device_string(
570 snd_pcm_uframes_t *period_size,
571 pa_bool_t *use_mmap) {
575 snd_pcm_t *pcm_handle;
582 pa_assert(period_size);
584 d = pa_xstrdup(device);
588 if ((err = snd_pcm_open(&pcm_handle, d, mode, SND_PCM_NONBLOCK)) < 0) {
589 pa_log("Error opening PCM device %s: %s", d, snd_strerror(err));
594 if ((err = pa_alsa_set_hw_params(pcm_handle, ss, nfrags, period_size, use_mmap, FALSE)) < 0) {
597 /* Hmm, some hw is very exotic, so we retry with plug, if without it didn't work */
599 if (pa_startswith(d, "hw:")) {
600 char *t = pa_sprintf_malloc("plughw:%s", d+3);
601 pa_log_debug("Opening the device as '%s' didn't work, retrying with '%s'.", d, t);
605 snd_pcm_close(pcm_handle);
609 pa_log("Failed to set hardware parameters on %s: %s", d, snd_strerror(err));
611 snd_pcm_close(pcm_handle);
618 if (ss->channels != map->channels) {
619 pa_assert_se(pa_channel_map_init_auto(map, ss->channels, PA_CHANNEL_MAP_AUX));
620 pa_channel_map_init_auto(map, ss->channels, PA_CHANNEL_MAP_ALSA);
627 int pa_alsa_prepare_mixer(snd_mixer_t *mixer, const char *dev) {
633 if ((err = snd_mixer_attach(mixer, dev)) < 0) {
634 pa_log_info("Unable to attach to mixer %s: %s", dev, snd_strerror(err));
638 if ((err = snd_mixer_selem_register(mixer, NULL, NULL)) < 0) {
639 pa_log_warn("Unable to register mixer: %s", snd_strerror(err));
643 if ((err = snd_mixer_load(mixer)) < 0) {
644 pa_log_warn("Unable to load mixer: %s", snd_strerror(err));
648 pa_log_info("Successfully attached to mixer '%s'", dev);
653 snd_mixer_elem_t *pa_alsa_find_elem(snd_mixer_t *mixer, const char *name, const char *fallback) {
654 snd_mixer_elem_t *elem;
655 snd_mixer_selem_id_t *sid = NULL;
657 snd_mixer_selem_id_alloca(&sid);
662 snd_mixer_selem_id_set_name(sid, name);
664 if (!(elem = snd_mixer_find_selem(mixer, sid))) {
665 pa_log_info("Cannot find mixer control \"%s\".", snd_mixer_selem_id_get_name(sid));
668 snd_mixer_selem_id_set_name(sid, fallback);
670 if (!(elem = snd_mixer_find_selem(mixer, sid)))
671 pa_log_warn("Cannot find fallback mixer control \"%s\".", snd_mixer_selem_id_get_name(sid));
676 pa_log_info("Using mixer control \"%s\".", snd_mixer_selem_id_get_name(sid));
681 static const snd_mixer_selem_channel_id_t alsa_channel_ids[PA_CHANNEL_POSITION_MAX] = {
682 [PA_CHANNEL_POSITION_MONO] = SND_MIXER_SCHN_MONO, /* The ALSA name is just an alias! */
684 [PA_CHANNEL_POSITION_FRONT_CENTER] = SND_MIXER_SCHN_FRONT_CENTER,
685 [PA_CHANNEL_POSITION_FRONT_LEFT] = SND_MIXER_SCHN_FRONT_LEFT,
686 [PA_CHANNEL_POSITION_FRONT_RIGHT] = SND_MIXER_SCHN_FRONT_RIGHT,
688 [PA_CHANNEL_POSITION_REAR_CENTER] = SND_MIXER_SCHN_REAR_CENTER,
689 [PA_CHANNEL_POSITION_REAR_LEFT] = SND_MIXER_SCHN_REAR_LEFT,
690 [PA_CHANNEL_POSITION_REAR_RIGHT] = SND_MIXER_SCHN_REAR_RIGHT,
692 [PA_CHANNEL_POSITION_LFE] = SND_MIXER_SCHN_WOOFER,
694 [PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER] = SND_MIXER_SCHN_UNKNOWN,
695 [PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER] = SND_MIXER_SCHN_UNKNOWN,
697 [PA_CHANNEL_POSITION_SIDE_LEFT] = SND_MIXER_SCHN_SIDE_LEFT,
698 [PA_CHANNEL_POSITION_SIDE_RIGHT] = SND_MIXER_SCHN_SIDE_RIGHT,
700 [PA_CHANNEL_POSITION_AUX0] = SND_MIXER_SCHN_UNKNOWN,
701 [PA_CHANNEL_POSITION_AUX1] = SND_MIXER_SCHN_UNKNOWN,
702 [PA_CHANNEL_POSITION_AUX2] = SND_MIXER_SCHN_UNKNOWN,
703 [PA_CHANNEL_POSITION_AUX3] = SND_MIXER_SCHN_UNKNOWN,
704 [PA_CHANNEL_POSITION_AUX4] = SND_MIXER_SCHN_UNKNOWN,
705 [PA_CHANNEL_POSITION_AUX5] = SND_MIXER_SCHN_UNKNOWN,
706 [PA_CHANNEL_POSITION_AUX6] = SND_MIXER_SCHN_UNKNOWN,
707 [PA_CHANNEL_POSITION_AUX7] = SND_MIXER_SCHN_UNKNOWN,
708 [PA_CHANNEL_POSITION_AUX8] = SND_MIXER_SCHN_UNKNOWN,
709 [PA_CHANNEL_POSITION_AUX9] = SND_MIXER_SCHN_UNKNOWN,
710 [PA_CHANNEL_POSITION_AUX10] = SND_MIXER_SCHN_UNKNOWN,
711 [PA_CHANNEL_POSITION_AUX11] = SND_MIXER_SCHN_UNKNOWN,
712 [PA_CHANNEL_POSITION_AUX12] = SND_MIXER_SCHN_UNKNOWN,
713 [PA_CHANNEL_POSITION_AUX13] = SND_MIXER_SCHN_UNKNOWN,
714 [PA_CHANNEL_POSITION_AUX14] = SND_MIXER_SCHN_UNKNOWN,
715 [PA_CHANNEL_POSITION_AUX15] = SND_MIXER_SCHN_UNKNOWN,
716 [PA_CHANNEL_POSITION_AUX16] = SND_MIXER_SCHN_UNKNOWN,
717 [PA_CHANNEL_POSITION_AUX17] = SND_MIXER_SCHN_UNKNOWN,
718 [PA_CHANNEL_POSITION_AUX18] = SND_MIXER_SCHN_UNKNOWN,
719 [PA_CHANNEL_POSITION_AUX19] = SND_MIXER_SCHN_UNKNOWN,
720 [PA_CHANNEL_POSITION_AUX20] = SND_MIXER_SCHN_UNKNOWN,
721 [PA_CHANNEL_POSITION_AUX21] = SND_MIXER_SCHN_UNKNOWN,
722 [PA_CHANNEL_POSITION_AUX22] = SND_MIXER_SCHN_UNKNOWN,
723 [PA_CHANNEL_POSITION_AUX23] = SND_MIXER_SCHN_UNKNOWN,
724 [PA_CHANNEL_POSITION_AUX24] = SND_MIXER_SCHN_UNKNOWN,
725 [PA_CHANNEL_POSITION_AUX25] = SND_MIXER_SCHN_UNKNOWN,
726 [PA_CHANNEL_POSITION_AUX26] = SND_MIXER_SCHN_UNKNOWN,
727 [PA_CHANNEL_POSITION_AUX27] = SND_MIXER_SCHN_UNKNOWN,
728 [PA_CHANNEL_POSITION_AUX28] = SND_MIXER_SCHN_UNKNOWN,
729 [PA_CHANNEL_POSITION_AUX29] = SND_MIXER_SCHN_UNKNOWN,
730 [PA_CHANNEL_POSITION_AUX30] = SND_MIXER_SCHN_UNKNOWN,
731 [PA_CHANNEL_POSITION_AUX31] = SND_MIXER_SCHN_UNKNOWN,
733 [PA_CHANNEL_POSITION_TOP_CENTER] = SND_MIXER_SCHN_UNKNOWN,
735 [PA_CHANNEL_POSITION_TOP_FRONT_CENTER] = SND_MIXER_SCHN_UNKNOWN,
736 [PA_CHANNEL_POSITION_TOP_FRONT_LEFT] = SND_MIXER_SCHN_UNKNOWN,
737 [PA_CHANNEL_POSITION_TOP_FRONT_RIGHT] = SND_MIXER_SCHN_UNKNOWN,
739 [PA_CHANNEL_POSITION_TOP_REAR_CENTER] = SND_MIXER_SCHN_UNKNOWN,
740 [PA_CHANNEL_POSITION_TOP_REAR_LEFT] = SND_MIXER_SCHN_UNKNOWN,
741 [PA_CHANNEL_POSITION_TOP_REAR_RIGHT] = SND_MIXER_SCHN_UNKNOWN
745 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) {
747 pa_bool_t alsa_channel_used[SND_MIXER_SCHN_LAST];
748 pa_bool_t mono_used = FALSE;
751 pa_assert(channel_map);
752 pa_assert(mixer_map);
754 memset(&alsa_channel_used, 0, sizeof(alsa_channel_used));
756 if (channel_map->channels > 1 &&
757 ((playback && snd_mixer_selem_has_playback_volume_joined(elem)) ||
758 (!playback && snd_mixer_selem_has_capture_volume_joined(elem)))) {
759 pa_log_info("ALSA device lacks independant volume controls for each channel, falling back to software volume control.");
763 for (i = 0; i < channel_map->channels; i++) {
764 snd_mixer_selem_channel_id_t id;
767 is_mono = channel_map->map[i] == PA_CHANNEL_POSITION_MONO;
768 id = alsa_channel_ids[channel_map->map[i]];
770 if (!is_mono && id == SND_MIXER_SCHN_UNKNOWN) {
771 pa_log_info("Configured channel map contains channel '%s' that is unknown to the ALSA mixer. Falling back to software volume control.", pa_channel_position_to_string(channel_map->map[i]));
775 if ((is_mono && mono_used) || (!is_mono && alsa_channel_used[id])) {
776 pa_log_info("Channel map has duplicate channel '%s', failling back to software volume control.", pa_channel_position_to_string(channel_map->map[i]));
780 if ((playback && (!snd_mixer_selem_has_playback_channel(elem, id) || (is_mono && !snd_mixer_selem_is_playback_mono(elem)))) ||
781 (!playback && (!snd_mixer_selem_has_capture_channel(elem, id) || (is_mono && !snd_mixer_selem_is_capture_mono(elem))))) {
783 pa_log_info("ALSA device lacks separate volumes control for channel '%s', falling back to software volume control.", pa_channel_position_to_string(channel_map->map[i]));
788 mixer_map[i] = SND_MIXER_SCHN_MONO;
792 alsa_channel_used[id] = TRUE;
796 pa_log_info("All %u channels can be mapped to mixer channels. Using hardware volume control.", channel_map->channels);