Rework ALSA mixer channel detection code. This time we actually care about the channe...
[profile/ivi/pulseaudio-panda.git] / src / modules / alsa-util.c
1 /* $Id$ */
2
3 /***
4   This file is part of PulseAudio.
5
6   Copyright 2004-2006 Lennart Poettering
7   Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
8
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.
13
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.
18
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
22   USA.
23 ***/
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <sys/types.h>
30 #include <asoundlib.h>
31
32 #include <pulse/sample.h>
33 #include <pulse/xmalloc.h>
34
35 #include <pulsecore/log.h>
36 #include <pulsecore/macro.h>
37 #include <pulsecore/core-util.h>
38
39 #include "alsa-util.h"
40
41 struct pa_alsa_fdlist {
42     int 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     int 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, PA_GCC_UNUSED int fd, pa_io_event_flags_t events, void *userdata) {
60
61     struct pa_alsa_fdlist *fdl = userdata;
62     int err, i;
63     unsigned short revents;
64
65     pa_assert(a);
66     pa_assert(fdl);
67     pa_assert(fdl->mixer);
68     pa_assert(fdl->fds);
69     pa_assert(fdl->work_fds);
70
71     if (fdl->polled)
72         return;
73
74     fdl->polled = 1;
75
76     memcpy(fdl->work_fds, fdl->fds, sizeof(struct pollfd) * fdl->num_fds);
77
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;
88             break;
89         }
90     }
91
92     pa_assert(i != fdl->num_fds);
93
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));
96         return;
97     }
98
99     a->defer_enable(fdl->defer, 1);
100
101     if (revents)
102         snd_mixer_handle_events(fdl->mixer);
103 }
104
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;
107     int num_fds, i, err;
108     struct pollfd *temp;
109
110     pa_assert(a);
111     pa_assert(fdl);
112     pa_assert(fdl->mixer);
113
114     a->defer_enable(fdl->defer, 0);
115
116     num_fds = snd_mixer_poll_descriptors_count(fdl->mixer);
117     pa_assert(num_fds > 0);
118
119     if (num_fds != fdl->num_fds) {
120         if (fdl->fds)
121             pa_xfree(fdl->fds);
122         if (fdl->work_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);
126     }
127
128     memset(fdl->work_fds, 0, sizeof(struct pollfd) * num_fds);
129
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));
132         return;
133     }
134
135     fdl->polled = 0;
136
137     if (memcmp(fdl->fds, fdl->work_fds, sizeof(struct pollfd) * num_fds) == 0)
138         return;
139
140     if (fdl->ios) {
141         for (i = 0; i < fdl->num_fds; i++)
142             a->io_free(fdl->ios[i]);
143
144         if (num_fds != fdl->num_fds) {
145             pa_xfree(fdl->ios);
146             fdl->ios = NULL;
147         }
148     }
149
150     if (!fdl->ios)
151         fdl->ios = pa_xnew(pa_io_event*, num_fds);
152
153     /* Swap pointers */
154     temp = fdl->work_fds;
155     fdl->work_fds = fdl->fds;
156     fdl->fds = temp;
157
158     fdl->num_fds = num_fds;
159
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),
164             io_cb, fdl);
165 }
166
167 struct pa_alsa_fdlist *pa_alsa_fdlist_new(void) {
168     struct pa_alsa_fdlist *fdl;
169
170     fdl = pa_xnew0(struct pa_alsa_fdlist, 1);
171
172     fdl->num_fds = 0;
173     fdl->fds = NULL;
174     fdl->work_fds = NULL;
175     fdl->mixer = NULL;
176     fdl->m = NULL;
177     fdl->defer = NULL;
178     fdl->ios = NULL;
179     fdl->polled = 0;
180
181     return fdl;
182 }
183
184 void pa_alsa_fdlist_free(struct pa_alsa_fdlist *fdl) {
185     pa_assert(fdl);
186
187     if (fdl->defer) {
188         pa_assert(fdl->m);
189         fdl->m->defer_free(fdl->defer);
190     }
191
192     if (fdl->ios) {
193         int i;
194         pa_assert(fdl->m);
195         for (i = 0;i < fdl->num_fds;i++)
196             fdl->m->io_free(fdl->ios[i]);
197         pa_xfree(fdl->ios);
198     }
199
200     if (fdl->fds)
201         pa_xfree(fdl->fds);
202     if (fdl->work_fds)
203         pa_xfree(fdl->work_fds);
204
205     pa_xfree(fdl);
206 }
207
208 int pa_alsa_fdlist_set_mixer(struct pa_alsa_fdlist *fdl, snd_mixer_t *mixer_handle, pa_mainloop_api* m) {
209     pa_assert(fdl);
210     pa_assert(mixer_handle);
211     pa_assert(m);
212     pa_assert(!fdl->m);
213
214     fdl->mixer = mixer_handle;
215     fdl->m = m;
216     fdl->defer = m->defer_new(m, defer_cb, fdl);
217
218     return 0;
219 }
220
221 static int set_format(snd_pcm_t *pcm_handle, snd_pcm_hw_params_t *hwparams, pa_sample_format_t *f) {
222
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,
233     };
234
235     static const pa_sample_format_t try_order[] = {
236         PA_SAMPLE_FLOAT32NE,
237         PA_SAMPLE_FLOAT32RE,
238         PA_SAMPLE_S32NE,
239         PA_SAMPLE_S32RE,
240         PA_SAMPLE_S16NE,
241         PA_SAMPLE_S16RE,
242         PA_SAMPLE_ALAW,
243         PA_SAMPLE_ULAW,
244         PA_SAMPLE_U8,
245         PA_SAMPLE_INVALID
246     };
247
248     int i, ret;
249
250     pa_assert(pcm_handle);
251     pa_assert(f);
252
253     if ((ret = snd_pcm_hw_params_set_format(pcm_handle, hwparams, format_trans[*f])) >= 0)
254         return ret;
255
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;
268     else
269         goto try_auto;
270
271     if ((ret = snd_pcm_hw_params_set_format(pcm_handle, hwparams, format_trans[*f])) >= 0)
272         return ret;
273
274 try_auto:
275
276     for (i = 0; try_order[i] != PA_SAMPLE_INVALID; i++) {
277         *f = try_order[i];
278
279         if ((ret = snd_pcm_hw_params_set_format(pcm_handle, hwparams, format_trans[*f])) >= 0)
280             return ret;
281     }
282
283     return -1;
284 }
285
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,
290         pa_sample_spec *ss,
291         uint32_t *periods,
292         snd_pcm_uframes_t *period_size,
293         pa_bool_t *use_mmap,
294         pa_bool_t require_exact_channel_number) {
295
296     int ret = -1;
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;
303
304     pa_assert(pcm_handle);
305     pa_assert(ss);
306     pa_assert(periods);
307     pa_assert(period_size);
308
309     snd_pcm_hw_params_alloca(&hwparams);
310
311     buffer_size = *periods * *period_size;
312
313     if ((ret = snd_pcm_hw_params_any(pcm_handle, hwparams)) < 0)
314         goto finish;
315
316     if ((ret = snd_pcm_hw_params_set_rate_resample(pcm_handle, hwparams, 0)) < 0)
317         goto finish;
318
319     if (_use_mmap) {
320         if ((ret = snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_MMAP_INTERLEAVED)) < 0) {
321
322             /* mmap() didn't work, fall back to interleaved */
323
324             if ((ret = snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
325                 goto finish;
326
327             _use_mmap = FALSE;
328         }
329
330     } else if ((ret = snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
331         goto finish;
332
333     if ((ret = set_format(pcm_handle, hwparams, &f)) < 0)
334         goto finish;
335
336     if ((ret = snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams, &r, NULL)) < 0)
337         goto finish;
338
339     if (require_exact_channel_number) {
340         if ((ret = snd_pcm_hw_params_set_channels(pcm_handle, hwparams, c)) < 0)
341             goto finish;
342     } else {
343         if ((ret = snd_pcm_hw_params_set_channels_near(pcm_handle, hwparams, &c)) < 0)
344             goto finish;
345     }
346
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))
349         goto finish;
350
351     if  ((ret = snd_pcm_hw_params(pcm_handle, hwparams)) < 0)
352         goto finish;
353
354     if (ss->rate != r)
355         pa_log_warn("Device %s doesn't support %u Hz, changed to %u Hz.", snd_pcm_name(pcm_handle), ss->rate, r);
356
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);
359
360     if (ss->format != f)
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));
362
363     if ((ret = snd_pcm_prepare(pcm_handle)) < 0)
364         goto finish;
365
366     if ((ret = snd_pcm_hw_params_get_buffer_size(hwparams, &buffer_size)) < 0 ||
367         (ret = snd_pcm_hw_params_get_period_size(hwparams, period_size, NULL)) < 0)
368         goto finish;
369
370     /* If the sample rate deviates too much, we need to resample */
371     if (r < ss->rate*.95 || r > ss->rate*1.05)
372         ss->rate = r;
373     ss->channels = c;
374     ss->format = f;
375
376     pa_assert(buffer_size > 0);
377     pa_assert(*period_size > 0);
378     *periods = buffer_size / *period_size;
379     pa_assert(*periods > 0);
380
381     if (use_mmap)
382         *use_mmap = _use_mmap;
383
384     ret = 0;
385
386 finish:
387
388     return ret;
389 }
390
391 int pa_alsa_set_sw_params(snd_pcm_t *pcm) {
392     snd_pcm_sw_params_t *swparams;
393     int err;
394
395     pa_assert(pcm);
396
397     snd_pcm_sw_params_alloca(&swparams);
398
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));
401         return err;
402     }
403
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));
406         return err;
407     }
408
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));
411         return err;
412     }
413
414     if ((err = snd_pcm_sw_params(pcm, swparams)) < 0) {
415         pa_log_warn("Unable to set sw params: %s\n", snd_strerror(err));
416         return err;
417     }
418
419     return 0;
420 }
421
422 struct device_info {
423     pa_channel_map map;
424     const char *name;
425 };
426
427 static const struct device_info device_table[] = {
428     {{ 2, { PA_CHANNEL_POSITION_LEFT, PA_CHANNEL_POSITION_RIGHT } }, "front" },
429
430     {{ 4, { PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT,
431             PA_CHANNEL_POSITION_REAR_LEFT, PA_CHANNEL_POSITION_REAR_RIGHT }}, "surround40" },
432
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" },
436
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" },
440
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" },
444
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" },
449
450     {{ 0, { 0 }}, NULL }
451 };
452
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];
455     unsigned i;
456
457     pa_assert(a);
458     pa_assert(b);
459
460     memset(in_a, 0, sizeof(in_a));
461
462     for (i = 0; i < a->channels; i++)
463         in_a[a->map[i]] = TRUE;
464
465     for (i = 0; i < b->channels; i++)
466         if (!in_a[b->map[i]])
467             return FALSE;
468
469     return TRUE;
470 }
471
472 snd_pcm_t *pa_alsa_open_by_device_id(
473         const char *dev_id,
474         char **dev,
475         pa_sample_spec *ss,
476         pa_channel_map* map,
477         int mode,
478         uint32_t *nfrags,
479         snd_pcm_uframes_t *period_size,
480         pa_bool_t *use_mmap) {
481
482     int i;
483     int direction = 1;
484     int err;
485     char *d;
486     snd_pcm_t *pcm_handle;
487
488     pa_assert(dev_id);
489     pa_assert(dev);
490     pa_assert(ss);
491     pa_assert(map);
492     pa_assert(nfrags);
493     pa_assert(period_size);
494
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.*/
501
502     for (i = 0;; i += direction) {
503         pa_sample_spec try_ss;
504
505         if (i < 0) {
506             pa_assert(direction == -1);
507
508             /* OK, so we iterated backwards, and now are at the
509              * beginning of our list. */
510
511             break;
512
513         } else if (!device_table[i].name) {
514             pa_assert(direction == 1);
515
516             /* OK, so we are at the end of our list. at iterated
517              * forwards. */
518
519             i--;
520             direction = -1;
521         }
522
523         if ((direction > 0) == !channel_map_superset(&device_table[i].map, map))
524             continue;
525
526         d = pa_sprintf_malloc("%s:%s", device_table[i].name, dev_id);
527         pa_log_debug("Trying %s...", d);
528
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));
531             pa_xfree(d);
532             continue;
533         }
534
535         try_ss.channels = device_table[i].map.channels;
536         try_ss.rate = ss->rate;
537         try_ss.format = ss->format;
538
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));
541             pa_xfree(d);
542             snd_pcm_close(pcm_handle);
543             continue;
544         }
545
546         *ss = try_ss;
547         *map = device_table[i].map;
548         pa_assert(map->channels == ss->channels);
549         *dev = d;
550         return pcm_handle;
551     }
552
553     /* OK, we didn't find any good device, so let's try the raw hw: stuff */
554
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);
558     pa_xfree(d);
559
560     return pcm_handle;
561 }
562
563 snd_pcm_t *pa_alsa_open_by_device_string(
564         const char *device,
565         char **dev,
566         pa_sample_spec *ss,
567         pa_channel_map* map,
568         int mode,
569         uint32_t *nfrags,
570         snd_pcm_uframes_t *period_size,
571         pa_bool_t *use_mmap) {
572
573     int err;
574     char *d;
575     snd_pcm_t *pcm_handle;
576
577     pa_assert(device);
578     pa_assert(dev);
579     pa_assert(ss);
580     pa_assert(map);
581     pa_assert(nfrags);
582     pa_assert(period_size);
583
584     d = pa_xstrdup(device);
585
586     for (;;) {
587
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));
590             pa_xfree(d);
591             return NULL;
592         }
593
594         if ((err = pa_alsa_set_hw_params(pcm_handle, ss, nfrags, period_size, use_mmap, FALSE)) < 0) {
595
596             if (err == -EPERM) {
597                 /* Hmm, some hw is very exotic, so we retry with plug, if without it didn't work */
598
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);
602                     pa_xfree(d);
603                     d = t;
604
605                     snd_pcm_close(pcm_handle);
606                     continue;
607                 }
608
609                 pa_log("Failed to set hardware parameters on %s: %s", d, snd_strerror(err));
610                 pa_xfree(d);
611                 snd_pcm_close(pcm_handle);
612                 return NULL;
613             }
614         }
615
616         *dev = d;
617
618         if (ss->channels != map->channels)
619             pa_channel_map_init_auto(map, ss->channels, PA_CHANNEL_MAP_ALSA);
620
621         return pcm_handle;
622     }
623 }
624
625 int pa_alsa_prepare_mixer(snd_mixer_t *mixer, const char *dev) {
626     int err;
627
628     pa_assert(mixer);
629     pa_assert(dev);
630
631     if ((err = snd_mixer_attach(mixer, dev)) < 0) {
632         pa_log_info("Unable to attach to mixer %s: %s", dev, snd_strerror(err));
633         return -1;
634     }
635
636     if ((err = snd_mixer_selem_register(mixer, NULL, NULL)) < 0) {
637         pa_log_warn("Unable to register mixer: %s", snd_strerror(err));
638         return -1;
639     }
640
641     if ((err = snd_mixer_load(mixer)) < 0) {
642         pa_log_warn("Unable to load mixer: %s", snd_strerror(err));
643         return -1;
644     }
645
646     pa_log_info("Successfully attached to mixer '%s'", dev);
647
648     return 0;
649 }
650
651 snd_mixer_elem_t *pa_alsa_find_elem(snd_mixer_t *mixer, const char *name, const char *fallback) {
652     snd_mixer_elem_t *elem;
653     snd_mixer_selem_id_t *sid = NULL;
654
655     snd_mixer_selem_id_alloca(&sid);
656
657     pa_assert(mixer);
658     pa_assert(name);
659
660     snd_mixer_selem_id_set_name(sid, name);
661
662     if (!(elem = snd_mixer_find_selem(mixer, sid))) {
663         pa_log_info("Cannot find mixer control \"%s\".", snd_mixer_selem_id_get_name(sid));
664
665         if (fallback) {
666             snd_mixer_selem_id_set_name(sid, fallback);
667
668             if (!(elem = snd_mixer_find_selem(mixer, sid)))
669                 pa_log_warn("Cannot find fallback mixer control \"%s\".", snd_mixer_selem_id_get_name(sid));
670         }
671     }
672
673     if (elem)
674         pa_log_info("Using mixer control \"%s\".", snd_mixer_selem_id_get_name(sid));
675
676     return elem;
677 }
678
679 static const snd_mixer_selem_channel_id_t alsa_channel_ids[PA_CHANNEL_POSITION_MAX] = {
680     [PA_CHANNEL_POSITION_MONO] = SND_MIXER_SCHN_MONO, /* The ALSA name is just an alias! */
681
682     [PA_CHANNEL_POSITION_FRONT_CENTER] = SND_MIXER_SCHN_FRONT_CENTER,
683     [PA_CHANNEL_POSITION_FRONT_LEFT] = SND_MIXER_SCHN_FRONT_LEFT,
684     [PA_CHANNEL_POSITION_FRONT_RIGHT] = SND_MIXER_SCHN_FRONT_RIGHT,
685
686     [PA_CHANNEL_POSITION_REAR_CENTER] = SND_MIXER_SCHN_REAR_CENTER,
687     [PA_CHANNEL_POSITION_REAR_LEFT] = SND_MIXER_SCHN_REAR_LEFT,
688     [PA_CHANNEL_POSITION_REAR_RIGHT] = SND_MIXER_SCHN_REAR_RIGHT,
689
690     [PA_CHANNEL_POSITION_LFE] = SND_MIXER_SCHN_WOOFER,
691
692     [PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER] = SND_MIXER_SCHN_UNKNOWN,
693     [PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER] = SND_MIXER_SCHN_UNKNOWN,
694
695     [PA_CHANNEL_POSITION_SIDE_LEFT] = SND_MIXER_SCHN_SIDE_LEFT,
696     [PA_CHANNEL_POSITION_SIDE_RIGHT] = SND_MIXER_SCHN_SIDE_RIGHT,
697
698     [PA_CHANNEL_POSITION_AUX0] = SND_MIXER_SCHN_UNKNOWN,
699     [PA_CHANNEL_POSITION_AUX1] = SND_MIXER_SCHN_UNKNOWN,
700     [PA_CHANNEL_POSITION_AUX2] = SND_MIXER_SCHN_UNKNOWN,
701     [PA_CHANNEL_POSITION_AUX3] = SND_MIXER_SCHN_UNKNOWN,
702     [PA_CHANNEL_POSITION_AUX4] = SND_MIXER_SCHN_UNKNOWN,
703     [PA_CHANNEL_POSITION_AUX5] = SND_MIXER_SCHN_UNKNOWN,
704     [PA_CHANNEL_POSITION_AUX6] = SND_MIXER_SCHN_UNKNOWN,
705     [PA_CHANNEL_POSITION_AUX7] = SND_MIXER_SCHN_UNKNOWN,
706     [PA_CHANNEL_POSITION_AUX8] = SND_MIXER_SCHN_UNKNOWN,
707     [PA_CHANNEL_POSITION_AUX9] =  SND_MIXER_SCHN_UNKNOWN,
708     [PA_CHANNEL_POSITION_AUX10] = SND_MIXER_SCHN_UNKNOWN,
709     [PA_CHANNEL_POSITION_AUX11] = SND_MIXER_SCHN_UNKNOWN,
710     [PA_CHANNEL_POSITION_AUX12] = SND_MIXER_SCHN_UNKNOWN,
711     [PA_CHANNEL_POSITION_AUX13] = SND_MIXER_SCHN_UNKNOWN,
712     [PA_CHANNEL_POSITION_AUX14] = SND_MIXER_SCHN_UNKNOWN,
713     [PA_CHANNEL_POSITION_AUX15] = SND_MIXER_SCHN_UNKNOWN,
714     [PA_CHANNEL_POSITION_AUX16] = SND_MIXER_SCHN_UNKNOWN,
715     [PA_CHANNEL_POSITION_AUX17] = SND_MIXER_SCHN_UNKNOWN,
716     [PA_CHANNEL_POSITION_AUX18] = SND_MIXER_SCHN_UNKNOWN,
717     [PA_CHANNEL_POSITION_AUX19] = SND_MIXER_SCHN_UNKNOWN,
718     [PA_CHANNEL_POSITION_AUX20] = SND_MIXER_SCHN_UNKNOWN,
719     [PA_CHANNEL_POSITION_AUX21] = SND_MIXER_SCHN_UNKNOWN,
720     [PA_CHANNEL_POSITION_AUX22] = SND_MIXER_SCHN_UNKNOWN,
721     [PA_CHANNEL_POSITION_AUX23] = SND_MIXER_SCHN_UNKNOWN,
722     [PA_CHANNEL_POSITION_AUX24] = SND_MIXER_SCHN_UNKNOWN,
723     [PA_CHANNEL_POSITION_AUX25] = SND_MIXER_SCHN_UNKNOWN,
724     [PA_CHANNEL_POSITION_AUX26] = SND_MIXER_SCHN_UNKNOWN,
725     [PA_CHANNEL_POSITION_AUX27] = SND_MIXER_SCHN_UNKNOWN,
726     [PA_CHANNEL_POSITION_AUX28] = SND_MIXER_SCHN_UNKNOWN,
727     [PA_CHANNEL_POSITION_AUX29] = SND_MIXER_SCHN_UNKNOWN,
728     [PA_CHANNEL_POSITION_AUX30] = SND_MIXER_SCHN_UNKNOWN,
729     [PA_CHANNEL_POSITION_AUX31] = SND_MIXER_SCHN_UNKNOWN,
730
731     [PA_CHANNEL_POSITION_TOP_CENTER] = SND_MIXER_SCHN_UNKNOWN,
732
733     [PA_CHANNEL_POSITION_TOP_FRONT_CENTER] = SND_MIXER_SCHN_UNKNOWN,
734     [PA_CHANNEL_POSITION_TOP_FRONT_LEFT] = SND_MIXER_SCHN_UNKNOWN,
735     [PA_CHANNEL_POSITION_TOP_FRONT_RIGHT] = SND_MIXER_SCHN_UNKNOWN,
736
737     [PA_CHANNEL_POSITION_TOP_REAR_CENTER] = SND_MIXER_SCHN_UNKNOWN,
738     [PA_CHANNEL_POSITION_TOP_REAR_LEFT] = SND_MIXER_SCHN_UNKNOWN,
739     [PA_CHANNEL_POSITION_TOP_REAR_RIGHT] = SND_MIXER_SCHN_UNKNOWN
740 };
741
742
743 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) {
744     unsigned i;
745     pa_bool_t alsa_channel_used[SND_MIXER_SCHN_LAST];
746     pa_bool_t mono_used = FALSE;
747
748     pa_assert(elem);
749     pa_assert(channel_map);
750     pa_assert(mixer_map);
751
752     memset(&alsa_channel_used, 0, sizeof(alsa_channel_used));
753
754     if (channel_map->channels > 1 &&
755         ((playback && snd_mixer_selem_has_playback_volume_joined(elem)) ||
756          (!playback && snd_mixer_selem_has_capture_volume_joined(elem)))) {
757         pa_log_info("ALSA device lacks independant volume controls for each channel, falling back to software volume control.");
758         return -1;
759     }
760
761     for (i = 0; i < channel_map->channels; i++) {
762         snd_mixer_selem_channel_id_t id;
763         pa_bool_t is_mono;
764
765         is_mono = channel_map->map[i] == PA_CHANNEL_POSITION_MONO;
766         id = alsa_channel_ids[channel_map->map[i]];
767
768         if (!is_mono && id == SND_MIXER_SCHN_UNKNOWN) {
769             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]));
770             return -1;
771         }
772
773         if ((is_mono && mono_used) || (!is_mono && alsa_channel_used[id])) {
774             pa_log_info("Channel map has duplicate channel '%s', failling back to software volume control.", pa_channel_position_to_string(channel_map->map[i]));
775             return -1;
776         }
777
778         if ((playback && (!snd_mixer_selem_has_playback_channel(elem, id) || (is_mono && !snd_mixer_selem_is_playback_mono(elem)))) ||
779             (!playback && (!snd_mixer_selem_has_capture_channel(elem, id) || (is_mono && !snd_mixer_selem_is_capture_mono(elem))))) {
780
781             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]));
782             return -1;
783         }
784
785         if (is_mono) {
786             mixer_map[i] = SND_MIXER_SCHN_MONO;
787             mono_used = TRUE;
788         } else {
789             mixer_map[i] = id;
790             alsa_channel_used[id] = TRUE;
791         }
792     }
793
794     pa_log_info("All %u channels can be mapped to mixer channels. Using hardware volume control.", channel_map->channels);
795
796     return 0;
797 }