]> code.delx.au - pulseaudio/blob - src/modules/alsa/alsa-util.c
652217648ccb704409ed382db97836a7990df550
[pulseaudio] / src / modules / alsa / alsa-util.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
6
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA.
21 ***/
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <sys/types.h>
28 #include <limits.h>
29 #include <asoundlib.h>
30
31 #include <pulse/sample.h>
32 #include <pulse/xmalloc.h>
33 #include <pulse/timeval.h>
34
35 #include <pulsecore/log.h>
36 #include <pulsecore/macro.h>
37 #include <pulsecore/core-util.h>
38 #include <pulsecore/atomic.h>
39
40 #include "alsa-util.h"
41
42 struct pa_alsa_fdlist {
43 unsigned num_fds;
44 struct pollfd *fds;
45 /* This is a temporary buffer used to avoid lots of mallocs */
46 struct pollfd *work_fds;
47
48 snd_mixer_t *mixer;
49
50 pa_mainloop_api *m;
51 pa_defer_event *defer;
52 pa_io_event **ios;
53
54 pa_bool_t polled;
55
56 void (*cb)(void *userdata);
57 void *userdata;
58 };
59
60 static void io_cb(pa_mainloop_api*a, pa_io_event* e, int fd, pa_io_event_flags_t events, void *userdata) {
61
62 struct pa_alsa_fdlist *fdl = userdata;
63 int err;
64 unsigned i;
65 unsigned short revents;
66
67 pa_assert(a);
68 pa_assert(fdl);
69 pa_assert(fdl->mixer);
70 pa_assert(fdl->fds);
71 pa_assert(fdl->work_fds);
72
73 if (fdl->polled)
74 return;
75
76 fdl->polled = TRUE;
77
78 memcpy(fdl->work_fds, fdl->fds, sizeof(struct pollfd) * fdl->num_fds);
79
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;
90 break;
91 }
92 }
93
94 pa_assert(i != fdl->num_fds);
95
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));
98 return;
99 }
100
101 a->defer_enable(fdl->defer, 1);
102
103 if (revents)
104 snd_mixer_handle_events(fdl->mixer);
105 }
106
107 static void defer_cb(pa_mainloop_api*a, pa_defer_event* e, void *userdata) {
108 struct pa_alsa_fdlist *fdl = userdata;
109 unsigned num_fds, i;
110 int err;
111 struct pollfd *temp;
112
113 pa_assert(a);
114 pa_assert(fdl);
115 pa_assert(fdl->mixer);
116
117 a->defer_enable(fdl->defer, 0);
118
119 num_fds = (unsigned) snd_mixer_poll_descriptors_count(fdl->mixer);
120
121 if (num_fds != fdl->num_fds) {
122 if (fdl->fds)
123 pa_xfree(fdl->fds);
124 if (fdl->work_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);
128 }
129
130 memset(fdl->work_fds, 0, sizeof(struct pollfd) * num_fds);
131
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));
134 return;
135 }
136
137 fdl->polled = FALSE;
138
139 if (memcmp(fdl->fds, fdl->work_fds, sizeof(struct pollfd) * num_fds) == 0)
140 return;
141
142 if (fdl->ios) {
143 for (i = 0; i < fdl->num_fds; i++)
144 a->io_free(fdl->ios[i]);
145
146 if (num_fds != fdl->num_fds) {
147 pa_xfree(fdl->ios);
148 fdl->ios = NULL;
149 }
150 }
151
152 if (!fdl->ios)
153 fdl->ios = pa_xnew(pa_io_event*, num_fds);
154
155 /* Swap pointers */
156 temp = fdl->work_fds;
157 fdl->work_fds = fdl->fds;
158 fdl->fds = temp;
159
160 fdl->num_fds = num_fds;
161
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),
166 io_cb, fdl);
167 }
168
169 struct pa_alsa_fdlist *pa_alsa_fdlist_new(void) {
170 struct pa_alsa_fdlist *fdl;
171
172 fdl = pa_xnew0(struct pa_alsa_fdlist, 1);
173
174 fdl->num_fds = 0;
175 fdl->fds = NULL;
176 fdl->work_fds = NULL;
177 fdl->mixer = NULL;
178 fdl->m = NULL;
179 fdl->defer = NULL;
180 fdl->ios = NULL;
181 fdl->polled = FALSE;
182
183 return fdl;
184 }
185
186 void pa_alsa_fdlist_free(struct pa_alsa_fdlist *fdl) {
187 pa_assert(fdl);
188
189 if (fdl->defer) {
190 pa_assert(fdl->m);
191 fdl->m->defer_free(fdl->defer);
192 }
193
194 if (fdl->ios) {
195 unsigned i;
196 pa_assert(fdl->m);
197 for (i = 0; i < fdl->num_fds; i++)
198 fdl->m->io_free(fdl->ios[i]);
199 pa_xfree(fdl->ios);
200 }
201
202 if (fdl->fds)
203 pa_xfree(fdl->fds);
204 if (fdl->work_fds)
205 pa_xfree(fdl->work_fds);
206
207 pa_xfree(fdl);
208 }
209
210 int pa_alsa_fdlist_set_mixer(struct pa_alsa_fdlist *fdl, snd_mixer_t *mixer_handle, pa_mainloop_api* m) {
211 pa_assert(fdl);
212 pa_assert(mixer_handle);
213 pa_assert(m);
214 pa_assert(!fdl->m);
215
216 fdl->mixer = mixer_handle;
217 fdl->m = m;
218 fdl->defer = m->defer_new(m, defer_cb, fdl);
219
220 return 0;
221 }
222
223 static int set_format(snd_pcm_t *pcm_handle, snd_pcm_hw_params_t *hwparams, pa_sample_format_t *f) {
224
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,
239 };
240
241 static const pa_sample_format_t try_order[] = {
242 PA_SAMPLE_FLOAT32NE,
243 PA_SAMPLE_FLOAT32RE,
244 PA_SAMPLE_S32NE,
245 PA_SAMPLE_S32RE,
246 PA_SAMPLE_S24_32NE,
247 PA_SAMPLE_S24_32RE,
248 PA_SAMPLE_S24NE,
249 PA_SAMPLE_S24RE,
250 PA_SAMPLE_S16NE,
251 PA_SAMPLE_S16RE,
252 PA_SAMPLE_ALAW,
253 PA_SAMPLE_ULAW,
254 PA_SAMPLE_U8,
255 PA_SAMPLE_INVALID
256 };
257
258 int i, ret;
259
260 pa_assert(pcm_handle);
261 pa_assert(f);
262
263 if ((ret = snd_pcm_hw_params_set_format(pcm_handle, hwparams, format_trans[*f])) >= 0)
264 return ret;
265
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;
286 else
287 goto try_auto;
288
289 if ((ret = snd_pcm_hw_params_set_format(pcm_handle, hwparams, format_trans[*f])) >= 0)
290 return ret;
291
292 try_auto:
293
294 for (i = 0; try_order[i] != PA_SAMPLE_INVALID; i++) {
295 *f = try_order[i];
296
297 if ((ret = snd_pcm_hw_params_set_format(pcm_handle, hwparams, format_trans[*f])) >= 0)
298 return ret;
299 }
300
301 return -1;
302 }
303
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,
308 pa_sample_spec *ss,
309 uint32_t *periods,
310 snd_pcm_uframes_t *period_size,
311 snd_pcm_uframes_t tsched_size,
312 pa_bool_t *use_mmap,
313 pa_bool_t *use_tsched,
314 pa_bool_t require_exact_channel_number) {
315
316 int ret = -1;
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;
326 int dir;
327
328 pa_assert(pcm_handle);
329 pa_assert(ss);
330
331 snd_pcm_hw_params_alloca(&hwparams);
332
333 if ((ret = snd_pcm_hw_params_any(pcm_handle, hwparams)) < 0)
334 goto finish;
335
336 if ((ret = snd_pcm_hw_params_set_rate_resample(pcm_handle, hwparams, 0)) < 0)
337 goto finish;
338
339 if (_use_mmap) {
340 if ((ret = snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_MMAP_INTERLEAVED)) < 0) {
341
342 /* mmap() didn't work, fall back to interleaved */
343
344 if ((ret = snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
345 goto finish;
346
347 _use_mmap = FALSE;
348 }
349
350 } else if ((ret = snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
351 goto finish;
352
353 if (!_use_mmap)
354 _use_tsched = FALSE;
355
356 if ((ret = set_format(pcm_handle, hwparams, &f)) < 0)
357 goto finish;
358
359 if ((ret = snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams, &r, NULL)) < 0)
360 goto finish;
361
362 if (require_exact_channel_number) {
363 if ((ret = snd_pcm_hw_params_set_channels(pcm_handle, hwparams, c)) < 0)
364 goto finish;
365 } else {
366 if ((ret = snd_pcm_hw_params_set_channels_near(pcm_handle, hwparams, &c)) < 0)
367 goto finish;
368 }
369
370 if ((ret = snd_pcm_hw_params_set_periods_integer(pcm_handle, hwparams)) < 0)
371 goto finish;
372
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);
377
378 if (_use_tsched) {
379 _period_size = tsched_size;
380 _periods = 1;
381
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);
384 }
385
386 buffer_size = _periods * _period_size;
387
388 if (_periods > 0) {
389
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 */
392
393 dir = 0;
394 if ((ret = snd_pcm_hw_params_set_periods_near(pcm_handle, hwparams, &_periods, &dir)) < 0) {
395 dir = 1;
396 if ((ret = snd_pcm_hw_params_set_periods_near(pcm_handle, hwparams, &_periods, &dir)) < 0) {
397 dir = -1;
398 if ((ret = snd_pcm_hw_params_set_periods_near(pcm_handle, hwparams, &_periods, &dir)) < 0)
399 goto finish;
400 }
401 }
402 }
403
404 if (_period_size > 0)
405 if ((ret = snd_pcm_hw_params_set_buffer_size_near(pcm_handle, hwparams, &buffer_size)) < 0)
406 goto finish;
407 }
408
409 if ((ret = snd_pcm_hw_params(pcm_handle, hwparams)) < 0)
410 goto finish;
411
412 if (ss->rate != r)
413 pa_log_info("Device %s doesn't support %u Hz, changed to %u Hz.", snd_pcm_name(pcm_handle), ss->rate, r);
414
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);
417
418 if (ss->format != f)
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));
420
421 if ((ret = snd_pcm_prepare(pcm_handle)) < 0)
422 goto finish;
423
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)
426 goto finish;
427
428 /* If the sample rate deviates too much, we need to resample */
429 if (r < ss->rate*.95 || r > ss->rate*1.05)
430 ss->rate = r;
431 ss->channels = (uint8_t) c;
432 ss->format = f;
433
434 pa_assert(_periods > 0);
435 pa_assert(_period_size > 0);
436
437 if (periods)
438 *periods = _periods;
439
440 if (period_size)
441 *period_size = _period_size;
442
443 if (use_mmap)
444 *use_mmap = _use_mmap;
445
446 if (use_tsched)
447 *use_tsched = _use_tsched;
448
449 ret = 0;
450
451 snd_pcm_nonblock(pcm_handle, 1);
452
453 finish:
454
455 return ret;
456 }
457
458 int pa_alsa_set_sw_params(snd_pcm_t *pcm, snd_pcm_uframes_t avail_min) {
459 snd_pcm_sw_params_t *swparams;
460 int err;
461
462 pa_assert(pcm);
463
464 snd_pcm_sw_params_alloca(&swparams);
465
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));
468 return err;
469 }
470
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));
473 return err;
474 }
475
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));
478 return err;
479 }
480
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));
483 return err;
484 }
485
486 if ((err = snd_pcm_sw_params(pcm, swparams)) < 0) {
487 pa_log_warn("Unable to set sw params: %s\n", snd_strerror(err));
488 return err;
489 }
490
491 return 0;
492 }
493
494 static const struct pa_alsa_profile_info device_table[] = {
495 {{ 1, { PA_CHANNEL_POSITION_MONO }},
496 "hw",
497 "Analog Mono",
498 "analog-mono" },
499
500 {{ 2, { PA_CHANNEL_POSITION_LEFT, PA_CHANNEL_POSITION_RIGHT }},
501 "front",
502 "Analog Stereo",
503 "analog-stereo" },
504
505 {{ 2, { PA_CHANNEL_POSITION_LEFT, PA_CHANNEL_POSITION_RIGHT }},
506 "iec958",
507 "IEC958 Digital Stereo",
508 "iec958-stereo" },
509
510 {{ 2, { PA_CHANNEL_POSITION_LEFT, PA_CHANNEL_POSITION_RIGHT }},
511 "hdmi",
512 "HDMI Digital Stereo",
513 "hdmi-stereo"},
514
515 {{ 4, { PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT,
516 PA_CHANNEL_POSITION_REAR_LEFT, PA_CHANNEL_POSITION_REAR_RIGHT }},
517 "surround40",
518 "Analog Surround 4.0",
519 "analog-surround-40" },
520
521 {{ 4, { PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT,
522 PA_CHANNEL_POSITION_REAR_LEFT, PA_CHANNEL_POSITION_REAR_RIGHT }},
523 "a52",
524 "IEC958/AC3 Digital Surround 4.0",
525 "iec958-ac3-surround-40" },
526
527 {{ 5, { PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT,
528 PA_CHANNEL_POSITION_REAR_LEFT, PA_CHANNEL_POSITION_REAR_RIGHT,
529 PA_CHANNEL_POSITION_LFE }},
530 "surround41",
531 "Analog Surround 4.1",
532 "analog-surround-41"},
533
534 {{ 5, { PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT,
535 PA_CHANNEL_POSITION_REAR_LEFT, PA_CHANNEL_POSITION_REAR_RIGHT,
536 PA_CHANNEL_POSITION_CENTER }},
537 "surround50",
538 "Analog Surround 5.0",
539 "analog-surround-50" },
540
541 {{ 6, { 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, PA_CHANNEL_POSITION_LFE }},
544 "surround51",
545 "Analog Surround 5.1",
546 "analog-surround-51" },
547
548 {{ 6, { PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_CENTER,
549 PA_CHANNEL_POSITION_FRONT_RIGHT, PA_CHANNEL_POSITION_REAR_LEFT,
550 PA_CHANNEL_POSITION_REAR_RIGHT, PA_CHANNEL_POSITION_LFE}},
551 "a52",
552 "IEC958/AC3 Digital Surround 5.1",
553 "iec958-ac3-surround-51" },
554
555 {{ 8, { PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT,
556 PA_CHANNEL_POSITION_REAR_LEFT, PA_CHANNEL_POSITION_REAR_RIGHT,
557 PA_CHANNEL_POSITION_CENTER, PA_CHANNEL_POSITION_LFE,
558 PA_CHANNEL_POSITION_SIDE_LEFT, PA_CHANNEL_POSITION_SIDE_RIGHT }},
559 "surround71",
560 "Analog Surround 7.1",
561 "analog-surround-71" },
562
563 {{ 0, { 0 }}, NULL, NULL, NULL }
564 };
565
566 static pa_bool_t channel_map_superset(const pa_channel_map *a, const pa_channel_map *b) {
567 pa_bool_t in_a[PA_CHANNEL_POSITION_MAX];
568 unsigned i;
569
570 pa_assert(a);
571 pa_assert(b);
572
573 memset(in_a, 0, sizeof(in_a));
574
575 for (i = 0; i < a->channels; i++)
576 in_a[a->map[i]] = TRUE;
577
578 for (i = 0; i < b->channels; i++)
579 if (!in_a[b->map[i]])
580 return FALSE;
581
582 return TRUE;
583 }
584
585 snd_pcm_t *pa_alsa_open_by_device_id(
586 const char *dev_id,
587 char **dev,
588 pa_sample_spec *ss,
589 pa_channel_map* map,
590 int mode,
591 uint32_t *nfrags,
592 snd_pcm_uframes_t *period_size,
593 snd_pcm_uframes_t tsched_size,
594 pa_bool_t *use_mmap,
595 pa_bool_t *use_tsched,
596 const char**config_description,
597 const char **config_name) {
598
599 int i;
600 int direction = 1;
601 char *d;
602 snd_pcm_t *pcm_handle;
603
604 pa_assert(dev_id);
605 pa_assert(dev);
606 pa_assert(ss);
607 pa_assert(map);
608 pa_assert(nfrags);
609 pa_assert(period_size);
610
611 /* First we try to find a device string with a superset of the
612 * requested channel map and open it without the plug: prefix. We
613 * iterate through our device table from top to bottom and take
614 * the first that matches. If we didn't find a working device that
615 * way, we iterate backwards, and check all devices that do not
616 * provide a superset of the requested channel map.*/
617
618 i = 0;
619 for (;;) {
620
621 if ((direction > 0) == channel_map_superset(&device_table[i].map, map)) {
622 pa_sample_spec try_ss;
623
624 pa_log_debug("Checking for %s (%s)", device_table[i].name, device_table[i].alsa_name);
625
626 d = pa_sprintf_malloc("%s:%s", device_table[i].alsa_name, dev_id);
627
628 try_ss.channels = device_table[i].map.channels;
629 try_ss.rate = ss->rate;
630 try_ss.format = ss->format;
631
632 pcm_handle = pa_alsa_open_by_device_string(
633 d,
634 dev,
635 &try_ss,
636 map,
637 mode,
638 nfrags,
639 period_size,
640 tsched_size,
641 use_mmap,
642 use_tsched,
643 TRUE);
644
645 pa_xfree(d);
646
647 if (pcm_handle) {
648
649 *ss = try_ss;
650 *map = device_table[i].map;
651 pa_assert(map->channels == ss->channels);
652
653 if (config_description)
654 *config_description = device_table[i].description;
655 if (config_name)
656 *config_name = device_table[i].name;
657
658
659 return pcm_handle;
660 }
661 }
662
663 if (direction > 0) {
664 if (!device_table[i+1].alsa_name) {
665 /* OK, so we are at the end of our list. Let's turn
666 * back. */
667 direction = -1;
668 } else {
669 /* We are not at the end of the list, so let's simply
670 * try the next entry */
671 i++;
672 }
673 }
674
675 if (direction < 0) {
676
677 if (device_table[i+1].alsa_name &&
678 device_table[i].map.channels == device_table[i+1].map.channels) {
679
680 /* OK, the next entry has the same number of channels,
681 * let's try it */
682 i++;
683
684 } else {
685 /* Hmm, so the next entry does not have the same
686 * number of channels, so let's go backwards until we
687 * find the next entry with a differnt number of
688 * channels */
689
690 for (i--; i >= 0; i--)
691 if (device_table[i].map.channels != device_table[i+1].map.channels)
692 break;
693
694 /* Hmm, there is no entry with a different number of
695 * entries, then we're done */
696 if (i < 0)
697 break;
698
699 /* OK, now lets find go back as long as we have the same number of channels */
700 for (; i > 0; i--)
701 if (device_table[i].map.channels != device_table[i-1].map.channels)
702 break;
703 }
704 }
705 }
706
707 /* OK, we didn't find any good device, so let's try the raw plughw: stuff */
708
709 d = pa_sprintf_malloc("hw:%s", dev_id);
710 pa_log_debug("Trying %s as last resort...", d);
711 pcm_handle = pa_alsa_open_by_device_string(d, dev, ss, map, mode, nfrags, period_size, tsched_size, use_mmap, use_tsched, FALSE);
712 pa_xfree(d);
713
714 if (pcm_handle) {
715 *config_description = NULL;
716 *config_name = NULL;
717 }
718
719 return pcm_handle;
720 }
721
722 snd_pcm_t *pa_alsa_open_by_device_string(
723 const char *device,
724 char **dev,
725 pa_sample_spec *ss,
726 pa_channel_map* map,
727 int mode,
728 uint32_t *nfrags,
729 snd_pcm_uframes_t *period_size,
730 snd_pcm_uframes_t tsched_size,
731 pa_bool_t *use_mmap,
732 pa_bool_t *use_tsched,
733 pa_bool_t require_exact_channel_number) {
734
735 int err;
736 char *d;
737 snd_pcm_t *pcm_handle;
738 pa_bool_t reformat = FALSE;
739
740 pa_assert(device);
741 pa_assert(ss);
742 pa_assert(map);
743
744 d = pa_xstrdup(device);
745
746 for (;;) {
747 pa_log_debug("Trying %s %s SND_PCM_NO_AUTO_FORMAT ...", d, reformat ? "without" : "with");
748
749 /* We don't pass SND_PCM_NONBLOCK here, since alsa-lib <=
750 * 1.0.17a would then ignore the SND_PCM_NO_xxx flags. Instead
751 * we enable nonblock mode afterwards via
752 * snd_pcm_nonblock(). Also see
753 * http://mailman.alsa-project.org/pipermail/alsa-devel/2008-August/010258.html */
754
755 if ((err = snd_pcm_open(&pcm_handle, d, mode,
756 /*SND_PCM_NONBLOCK|*/
757 SND_PCM_NO_AUTO_RESAMPLE|
758 SND_PCM_NO_AUTO_CHANNELS|
759 (reformat ? 0 : SND_PCM_NO_AUTO_FORMAT))) < 0) {
760 pa_log_info("Error opening PCM device %s: %s", d, snd_strerror(err));
761 pa_xfree(d);
762 return NULL;
763 }
764
765 if ((err = pa_alsa_set_hw_params(pcm_handle, ss, nfrags, period_size, tsched_size, use_mmap, use_tsched, require_exact_channel_number)) < 0) {
766
767 if (!reformat) {
768 reformat = TRUE;
769
770 snd_pcm_close(pcm_handle);
771 continue;
772 }
773
774 /* Hmm, some hw is very exotic, so we retry with plug, if without it didn't work */
775
776 if (!pa_startswith(d, "plug:") && !pa_startswith(d, "plughw:")) {
777 char *t;
778
779 t = pa_sprintf_malloc("plug:%s", d);
780 pa_xfree(d);
781 d = t;
782
783 reformat = FALSE;
784
785 snd_pcm_close(pcm_handle);
786 continue;
787 }
788
789 pa_log_info("Failed to set hardware parameters on %s: %s", d, snd_strerror(err));
790 pa_xfree(d);
791 snd_pcm_close(pcm_handle);
792 return NULL;
793 }
794
795 if (dev)
796 *dev = d;
797
798 if (ss->channels != map->channels)
799 pa_channel_map_init_extend(map, ss->channels, PA_CHANNEL_MAP_ALSA);
800
801 return pcm_handle;
802 }
803 }
804
805 int pa_alsa_probe_profiles(
806 const char *dev_id,
807 const pa_sample_spec *ss,
808 void (*cb)(const pa_alsa_profile_info *sink, const pa_alsa_profile_info *source, void *userdata),
809 void *userdata) {
810
811 const pa_alsa_profile_info *i;
812
813 pa_assert(dev_id);
814 pa_assert(ss);
815 pa_assert(cb);
816
817 /* We try each combination of playback/capture. We also try to
818 * open only for capture resp. only for sink. Don't get confused
819 * by the trailing entry in device_table we use for this! */
820
821 for (i = device_table; i < device_table + PA_ELEMENTSOF(device_table); i++) {
822 const pa_alsa_profile_info *j;
823 snd_pcm_t *pcm_i = NULL;
824
825 if (i->alsa_name) {
826 char *id;
827 pa_sample_spec try_ss;
828 pa_channel_map try_map;
829
830 pa_log_debug("Checking for playback on %s (%s)", i->name, i->alsa_name);
831 id = pa_sprintf_malloc("%s:%s", i->alsa_name, dev_id);
832
833 try_ss = *ss;
834 try_ss.channels = i->map.channels;
835 try_map = i->map;
836
837 pcm_i = pa_alsa_open_by_device_string(
838 id, NULL,
839 &try_ss, &try_map,
840 SND_PCM_STREAM_PLAYBACK,
841 NULL, NULL, 0, NULL, NULL,
842 TRUE);
843
844 pa_xfree(id);
845
846 if (!pcm_i)
847 continue;
848 }
849
850 for (j = device_table; j < device_table + PA_ELEMENTSOF(device_table); j++) {
851 snd_pcm_t *pcm_j = NULL;
852
853 if (j->alsa_name) {
854 char *jd;
855 pa_sample_spec try_ss;
856 pa_channel_map try_map;
857
858 pa_log_debug("Checking for capture on %s (%s)", j->name, j->alsa_name);
859 jd = pa_sprintf_malloc("%s:%s", j->alsa_name, dev_id);
860
861 try_ss = *ss;
862 try_ss.channels = j->map.channels;
863 try_map = j->map;
864
865 pcm_j = pa_alsa_open_by_device_string(
866 jd, NULL,
867 &try_ss, &try_map,
868 SND_PCM_STREAM_CAPTURE,
869 NULL, NULL, 0, NULL, NULL,
870 TRUE);
871
872 pa_xfree(jd);
873
874 if (!pcm_j)
875 continue;
876 }
877
878 if (pcm_j)
879 snd_pcm_close(pcm_j);
880
881 if (i->alsa_name || j->alsa_name)
882 cb(i->alsa_name ? i : NULL,
883 j->alsa_name ? j : NULL, userdata);
884 }
885
886 if (pcm_i)
887 snd_pcm_close(pcm_i);
888 }
889
890 return TRUE;
891 }
892
893 int pa_alsa_prepare_mixer(snd_mixer_t *mixer, const char *dev) {
894 int err;
895
896 pa_assert(mixer);
897 pa_assert(dev);
898
899 if ((err = snd_mixer_attach(mixer, dev)) < 0) {
900 pa_log_info("Unable to attach to mixer %s: %s", dev, snd_strerror(err));
901 return -1;
902 }
903
904 if ((err = snd_mixer_selem_register(mixer, NULL, NULL)) < 0) {
905 pa_log_warn("Unable to register mixer: %s", snd_strerror(err));
906 return -1;
907 }
908
909 if ((err = snd_mixer_load(mixer)) < 0) {
910 pa_log_warn("Unable to load mixer: %s", snd_strerror(err));
911 return -1;
912 }
913
914 pa_log_info("Successfully attached to mixer '%s'", dev);
915
916 return 0;
917 }
918
919 static pa_bool_t elem_has_volume(snd_mixer_elem_t *elem, pa_bool_t playback) {
920 pa_assert(elem);
921
922 if (playback && snd_mixer_selem_has_playback_volume(elem))
923 return TRUE;
924
925 if (!playback && snd_mixer_selem_has_capture_volume(elem))
926 return TRUE;
927
928 return FALSE;
929 }
930
931 static pa_bool_t elem_has_switch(snd_mixer_elem_t *elem, pa_bool_t playback) {
932 pa_assert(elem);
933
934 if (playback && snd_mixer_selem_has_playback_switch(elem))
935 return TRUE;
936
937 if (!playback && snd_mixer_selem_has_capture_switch(elem))
938 return TRUE;
939
940 return FALSE;
941 }
942
943 snd_mixer_elem_t *pa_alsa_find_elem(snd_mixer_t *mixer, const char *name, const char *fallback, pa_bool_t playback) {
944 snd_mixer_elem_t *elem = NULL, *fallback_elem = NULL;
945 snd_mixer_selem_id_t *sid = NULL;
946
947 snd_mixer_selem_id_alloca(&sid);
948
949 pa_assert(mixer);
950 pa_assert(name);
951
952 snd_mixer_selem_id_set_name(sid, name);
953
954 if ((elem = snd_mixer_find_selem(mixer, sid))) {
955
956 if (elem_has_volume(elem, playback) &&
957 elem_has_switch(elem, playback))
958 goto success;
959
960 if (!elem_has_volume(elem, playback) &&
961 !elem_has_switch(elem, playback))
962 elem = NULL;
963 }
964
965 pa_log_info("Cannot find mixer control \"%s\" or mixer control is no combination of switch/volume.", snd_mixer_selem_id_get_name(sid));
966
967 if (fallback) {
968 snd_mixer_selem_id_set_name(sid, fallback);
969
970 if ((fallback_elem = snd_mixer_find_selem(mixer, sid))) {
971
972 if (elem_has_volume(fallback_elem, playback) &&
973 elem_has_switch(fallback_elem, playback)) {
974 elem = fallback_elem;
975 goto success;
976 }
977
978 if (!elem_has_volume(fallback_elem, playback) &&
979 !elem_has_switch(fallback_elem, playback))
980 fallback_elem = NULL;
981 }
982
983 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));
984 }
985
986 if (elem && fallback_elem) {
987
988 /* Hmm, so we have both elements, but neither has both mute
989 * and volume. Let's prefer the one with the volume */
990
991 if (elem_has_volume(elem, playback))
992 goto success;
993
994 if (elem_has_volume(fallback_elem, playback)) {
995 elem = fallback_elem;
996 goto success;
997 }
998 }
999
1000 if (!elem && fallback_elem)
1001 elem = fallback_elem;
1002
1003 success:
1004
1005 if (elem)
1006 pa_log_info("Using mixer control \"%s\".", snd_mixer_selem_id_get_name(sid));
1007
1008 return elem;
1009 }
1010
1011 static const snd_mixer_selem_channel_id_t alsa_channel_ids[PA_CHANNEL_POSITION_MAX] = {
1012 [PA_CHANNEL_POSITION_MONO] = SND_MIXER_SCHN_MONO, /* The ALSA name is just an alias! */
1013
1014 [PA_CHANNEL_POSITION_FRONT_CENTER] = SND_MIXER_SCHN_FRONT_CENTER,
1015 [PA_CHANNEL_POSITION_FRONT_LEFT] = SND_MIXER_SCHN_FRONT_LEFT,
1016 [PA_CHANNEL_POSITION_FRONT_RIGHT] = SND_MIXER_SCHN_FRONT_RIGHT,
1017
1018 [PA_CHANNEL_POSITION_REAR_CENTER] = SND_MIXER_SCHN_REAR_CENTER,
1019 [PA_CHANNEL_POSITION_REAR_LEFT] = SND_MIXER_SCHN_REAR_LEFT,
1020 [PA_CHANNEL_POSITION_REAR_RIGHT] = SND_MIXER_SCHN_REAR_RIGHT,
1021
1022 [PA_CHANNEL_POSITION_LFE] = SND_MIXER_SCHN_WOOFER,
1023
1024 [PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER] = SND_MIXER_SCHN_UNKNOWN,
1025 [PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER] = SND_MIXER_SCHN_UNKNOWN,
1026
1027 [PA_CHANNEL_POSITION_SIDE_LEFT] = SND_MIXER_SCHN_SIDE_LEFT,
1028 [PA_CHANNEL_POSITION_SIDE_RIGHT] = SND_MIXER_SCHN_SIDE_RIGHT,
1029
1030 [PA_CHANNEL_POSITION_AUX0] = SND_MIXER_SCHN_UNKNOWN,
1031 [PA_CHANNEL_POSITION_AUX1] = SND_MIXER_SCHN_UNKNOWN,
1032 [PA_CHANNEL_POSITION_AUX2] = SND_MIXER_SCHN_UNKNOWN,
1033 [PA_CHANNEL_POSITION_AUX3] = SND_MIXER_SCHN_UNKNOWN,
1034 [PA_CHANNEL_POSITION_AUX4] = SND_MIXER_SCHN_UNKNOWN,
1035 [PA_CHANNEL_POSITION_AUX5] = SND_MIXER_SCHN_UNKNOWN,
1036 [PA_CHANNEL_POSITION_AUX6] = SND_MIXER_SCHN_UNKNOWN,
1037 [PA_CHANNEL_POSITION_AUX7] = SND_MIXER_SCHN_UNKNOWN,
1038 [PA_CHANNEL_POSITION_AUX8] = SND_MIXER_SCHN_UNKNOWN,
1039 [PA_CHANNEL_POSITION_AUX9] = SND_MIXER_SCHN_UNKNOWN,
1040 [PA_CHANNEL_POSITION_AUX10] = SND_MIXER_SCHN_UNKNOWN,
1041 [PA_CHANNEL_POSITION_AUX11] = SND_MIXER_SCHN_UNKNOWN,
1042 [PA_CHANNEL_POSITION_AUX12] = SND_MIXER_SCHN_UNKNOWN,
1043 [PA_CHANNEL_POSITION_AUX13] = SND_MIXER_SCHN_UNKNOWN,
1044 [PA_CHANNEL_POSITION_AUX14] = SND_MIXER_SCHN_UNKNOWN,
1045 [PA_CHANNEL_POSITION_AUX15] = SND_MIXER_SCHN_UNKNOWN,
1046 [PA_CHANNEL_POSITION_AUX16] = SND_MIXER_SCHN_UNKNOWN,
1047 [PA_CHANNEL_POSITION_AUX17] = SND_MIXER_SCHN_UNKNOWN,
1048 [PA_CHANNEL_POSITION_AUX18] = SND_MIXER_SCHN_UNKNOWN,
1049 [PA_CHANNEL_POSITION_AUX19] = SND_MIXER_SCHN_UNKNOWN,
1050 [PA_CHANNEL_POSITION_AUX20] = SND_MIXER_SCHN_UNKNOWN,
1051 [PA_CHANNEL_POSITION_AUX21] = SND_MIXER_SCHN_UNKNOWN,
1052 [PA_CHANNEL_POSITION_AUX22] = SND_MIXER_SCHN_UNKNOWN,
1053 [PA_CHANNEL_POSITION_AUX23] = SND_MIXER_SCHN_UNKNOWN,
1054 [PA_CHANNEL_POSITION_AUX24] = SND_MIXER_SCHN_UNKNOWN,
1055 [PA_CHANNEL_POSITION_AUX25] = SND_MIXER_SCHN_UNKNOWN,
1056 [PA_CHANNEL_POSITION_AUX26] = SND_MIXER_SCHN_UNKNOWN,
1057 [PA_CHANNEL_POSITION_AUX27] = SND_MIXER_SCHN_UNKNOWN,
1058 [PA_CHANNEL_POSITION_AUX28] = SND_MIXER_SCHN_UNKNOWN,
1059 [PA_CHANNEL_POSITION_AUX29] = SND_MIXER_SCHN_UNKNOWN,
1060 [PA_CHANNEL_POSITION_AUX30] = SND_MIXER_SCHN_UNKNOWN,
1061 [PA_CHANNEL_POSITION_AUX31] = SND_MIXER_SCHN_UNKNOWN,
1062
1063 [PA_CHANNEL_POSITION_TOP_CENTER] = SND_MIXER_SCHN_UNKNOWN,
1064
1065 [PA_CHANNEL_POSITION_TOP_FRONT_CENTER] = SND_MIXER_SCHN_UNKNOWN,
1066 [PA_CHANNEL_POSITION_TOP_FRONT_LEFT] = SND_MIXER_SCHN_UNKNOWN,
1067 [PA_CHANNEL_POSITION_TOP_FRONT_RIGHT] = SND_MIXER_SCHN_UNKNOWN,
1068
1069 [PA_CHANNEL_POSITION_TOP_REAR_CENTER] = SND_MIXER_SCHN_UNKNOWN,
1070 [PA_CHANNEL_POSITION_TOP_REAR_LEFT] = SND_MIXER_SCHN_UNKNOWN,
1071 [PA_CHANNEL_POSITION_TOP_REAR_RIGHT] = SND_MIXER_SCHN_UNKNOWN
1072 };
1073
1074
1075 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) {
1076 unsigned i;
1077 pa_bool_t alsa_channel_used[SND_MIXER_SCHN_LAST];
1078 pa_bool_t mono_used = FALSE;
1079
1080 pa_assert(elem);
1081 pa_assert(channel_map);
1082 pa_assert(mixer_map);
1083
1084 memset(&alsa_channel_used, 0, sizeof(alsa_channel_used));
1085
1086 if (channel_map->channels > 1 &&
1087 ((playback && snd_mixer_selem_has_playback_volume_joined(elem)) ||
1088 (!playback && snd_mixer_selem_has_capture_volume_joined(elem)))) {
1089 pa_log_info("ALSA device lacks independant volume controls for each channel.");
1090 return -1;
1091 }
1092
1093 for (i = 0; i < channel_map->channels; i++) {
1094 snd_mixer_selem_channel_id_t id;
1095 pa_bool_t is_mono;
1096
1097 is_mono = channel_map->map[i] == PA_CHANNEL_POSITION_MONO;
1098 id = alsa_channel_ids[channel_map->map[i]];
1099
1100 if (!is_mono && id == SND_MIXER_SCHN_UNKNOWN) {
1101 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]));
1102 return -1;
1103 }
1104
1105 if ((is_mono && mono_used) || (!is_mono && alsa_channel_used[id])) {
1106 pa_log_info("Channel map has duplicate channel '%s', falling back to software volume control.", pa_channel_position_to_string(channel_map->map[i]));
1107 return -1;
1108 }
1109
1110 if ((playback && (!snd_mixer_selem_has_playback_channel(elem, id) || (is_mono && !snd_mixer_selem_is_playback_mono(elem)))) ||
1111 (!playback && (!snd_mixer_selem_has_capture_channel(elem, id) || (is_mono && !snd_mixer_selem_is_capture_mono(elem))))) {
1112
1113 pa_log_info("ALSA device lacks separate volumes control for channel '%s'", pa_channel_position_to_string(channel_map->map[i]));
1114 return -1;
1115 }
1116
1117 if (is_mono) {
1118 mixer_map[i] = SND_MIXER_SCHN_MONO;
1119 mono_used = TRUE;
1120 } else {
1121 mixer_map[i] = id;
1122 alsa_channel_used[id] = TRUE;
1123 }
1124 }
1125
1126 pa_log_info("All %u channels can be mapped to mixer channels.", channel_map->channels);
1127
1128 return 0;
1129 }
1130
1131 void pa_alsa_dump(snd_pcm_t *pcm) {
1132 int err;
1133 snd_output_t *out;
1134
1135 pa_assert(pcm);
1136
1137 pa_assert_se(snd_output_buffer_open(&out) == 0);
1138
1139 if ((err = snd_pcm_dump(pcm, out)) < 0)
1140 pa_log_debug("snd_pcm_dump(): %s", snd_strerror(err));
1141 else {
1142 char *s = NULL;
1143 snd_output_buffer_string(out, &s);
1144 pa_log_debug("snd_pcm_dump():\n%s", pa_strnull(s));
1145 }
1146
1147 pa_assert_se(snd_output_close(out) == 0);
1148 }
1149
1150 void pa_alsa_dump_status(snd_pcm_t *pcm) {
1151 int err;
1152 snd_output_t *out;
1153 snd_pcm_status_t *status;
1154
1155 pa_assert(pcm);
1156
1157 snd_pcm_status_alloca(&status);
1158
1159 pa_assert_se(snd_output_buffer_open(&out) == 0);
1160
1161 pa_assert_se(snd_pcm_status(pcm, status) == 0);
1162
1163 if ((err = snd_pcm_status_dump(status, out)) < 0)
1164 pa_log_debug("snd_pcm_dump(): %s", snd_strerror(err));
1165 else {
1166 char *s = NULL;
1167 snd_output_buffer_string(out, &s);
1168 pa_log_debug("snd_pcm_dump():\n%s", pa_strnull(s));
1169 }
1170
1171 pa_assert_se(snd_output_close(out) == 0);
1172 }
1173
1174 static void alsa_error_handler(const char *file, int line, const char *function, int err, const char *fmt,...) {
1175 va_list ap;
1176 char *alsa_file;
1177
1178 alsa_file = pa_sprintf_malloc("(alsa-lib)%s", file);
1179
1180 va_start(ap, fmt);
1181
1182 pa_log_levelv_meta(PA_LOG_INFO, alsa_file, line, function, fmt, ap);
1183
1184 va_end(ap);
1185
1186 pa_xfree(alsa_file);
1187 }
1188
1189 static pa_atomic_t n_error_handler_installed = PA_ATOMIC_INIT(0);
1190
1191 void pa_alsa_redirect_errors_inc(void) {
1192 /* This is not really thread safe, but we do our best */
1193
1194 if (pa_atomic_inc(&n_error_handler_installed) == 0)
1195 snd_lib_error_set_handler(alsa_error_handler);
1196 }
1197
1198 void pa_alsa_redirect_errors_dec(void) {
1199 int r;
1200
1201 pa_assert_se((r = pa_atomic_dec(&n_error_handler_installed)) >= 1);
1202
1203 if (r == 1)
1204 snd_lib_error_set_handler(NULL);
1205 }
1206
1207 void pa_alsa_init_proplist_card(pa_proplist *p, int card) {
1208 char *cn, *lcn;
1209
1210 pa_assert(p);
1211 pa_assert(card >= 0);
1212
1213 pa_proplist_setf(p, "alsa.card", "%i", card);
1214
1215 if (snd_card_get_name(card, &cn) >= 0) {
1216 pa_proplist_sets(p, "alsa.card_name", cn);
1217 free(cn);
1218 }
1219
1220 if (snd_card_get_longname(card, &lcn) >= 0) {
1221 pa_proplist_sets(p, "alsa.long_card_name", lcn);
1222 free(lcn);
1223 }
1224 }
1225
1226 void pa_alsa_init_proplist_pcm(pa_proplist *p, snd_pcm_info_t *pcm_info) {
1227
1228 static const char * const alsa_class_table[SND_PCM_CLASS_LAST+1] = {
1229 [SND_PCM_CLASS_GENERIC] = "generic",
1230 [SND_PCM_CLASS_MULTI] = "multi",
1231 [SND_PCM_CLASS_MODEM] = "modem",
1232 [SND_PCM_CLASS_DIGITIZER] = "digitizer"
1233 };
1234 static const char * const class_table[SND_PCM_CLASS_LAST+1] = {
1235 [SND_PCM_CLASS_GENERIC] = "sound",
1236 [SND_PCM_CLASS_MULTI] = NULL,
1237 [SND_PCM_CLASS_MODEM] = "modem",
1238 [SND_PCM_CLASS_DIGITIZER] = NULL
1239 };
1240 static const char * const alsa_subclass_table[SND_PCM_SUBCLASS_LAST+1] = {
1241 [SND_PCM_SUBCLASS_GENERIC_MIX] = "generic-mix",
1242 [SND_PCM_SUBCLASS_MULTI_MIX] = "multi-mix"
1243 };
1244
1245 snd_pcm_class_t class;
1246 snd_pcm_subclass_t subclass;
1247 const char *n, *id, *sdn, *cn;
1248 int card;
1249
1250 pa_assert(p);
1251 pa_assert(pcm_info);
1252
1253 pa_proplist_sets(p, PA_PROP_DEVICE_API, "alsa");
1254
1255 class = snd_pcm_info_get_class(pcm_info);
1256 if (class <= SND_PCM_CLASS_LAST) {
1257 if (class_table[class])
1258 pa_proplist_sets(p, PA_PROP_DEVICE_CLASS, class_table[class]);
1259 if (alsa_class_table[class])
1260 pa_proplist_sets(p, "alsa.class", alsa_class_table[class]);
1261 }
1262 subclass = snd_pcm_info_get_subclass(pcm_info);
1263 if (subclass <= SND_PCM_SUBCLASS_LAST)
1264 if (alsa_subclass_table[subclass])
1265 pa_proplist_sets(p, "alsa.subclass", alsa_subclass_table[subclass]);
1266
1267 if ((n = snd_pcm_info_get_name(pcm_info)))
1268 pa_proplist_sets(p, "alsa.name", n);
1269
1270 if ((id = snd_pcm_info_get_id(pcm_info)))
1271 pa_proplist_sets(p, "alsa.id", id);
1272
1273 pa_proplist_setf(p, "alsa.subdevice", "%u", snd_pcm_info_get_subdevice(pcm_info));
1274 if ((sdn = snd_pcm_info_get_subdevice_name(pcm_info)))
1275 pa_proplist_sets(p, "alsa.subdevice_name", sdn);
1276
1277 pa_proplist_setf(p, "alsa.device", "%u", snd_pcm_info_get_device(pcm_info));
1278
1279 if ((card = snd_pcm_info_get_card(pcm_info)) >= 0) {
1280 pa_alsa_init_proplist_card(p, card);
1281 cn = pa_proplist_gets(p, "alsa.card_name");
1282 }
1283
1284 if (cn && n)
1285 pa_proplist_setf(p, PA_PROP_DEVICE_DESCRIPTION, "%s - %s", cn, n);
1286 else if (cn)
1287 pa_proplist_sets(p, PA_PROP_DEVICE_DESCRIPTION, cn);
1288 else if (n)
1289 pa_proplist_sets(p, PA_PROP_DEVICE_DESCRIPTION, n);
1290 }
1291
1292 int pa_alsa_recover_from_poll(snd_pcm_t *pcm, int revents) {
1293 snd_pcm_state_t state;
1294 int err;
1295
1296 pa_assert(pcm);
1297
1298 if (revents & POLLERR)
1299 pa_log_debug("Got POLLERR from ALSA");
1300 if (revents & POLLNVAL)
1301 pa_log_warn("Got POLLNVAL from ALSA");
1302 if (revents & POLLHUP)
1303 pa_log_warn("Got POLLHUP from ALSA");
1304 if (revents & POLLPRI)
1305 pa_log_warn("Got POLLPRI from ALSA");
1306 if (revents & POLLIN)
1307 pa_log_debug("Got POLLIN from ALSA");
1308 if (revents & POLLOUT)
1309 pa_log_debug("Got POLLOUT from ALSA");
1310
1311 state = snd_pcm_state(pcm);
1312 pa_log_debug("PCM state is %s", snd_pcm_state_name(state));
1313
1314 /* Try to recover from this error */
1315
1316 switch (state) {
1317
1318 case SND_PCM_STATE_XRUN:
1319 if ((err = snd_pcm_recover(pcm, -EPIPE, 1)) != 0) {
1320 pa_log_warn("Could not recover from POLLERR|POLLNVAL|POLLHUP and XRUN: %s", snd_strerror(err));
1321 return -1;
1322 }
1323 break;
1324
1325 case SND_PCM_STATE_SUSPENDED:
1326 if ((err = snd_pcm_recover(pcm, -ESTRPIPE, 1)) != 0) {
1327 pa_log_warn("Could not recover from POLLERR|POLLNVAL|POLLHUP and SUSPENDED: %s", snd_strerror(err));
1328 return -1;
1329 }
1330 break;
1331
1332 default:
1333
1334 snd_pcm_drop(pcm);
1335
1336 if ((err = snd_pcm_prepare(pcm)) < 0) {
1337 pa_log_warn("Could not recover from POLLERR|POLLNVAL|POLLHUP with snd_pcm_prepare(): %s", snd_strerror(err));
1338 return -1;
1339 }
1340 break;
1341 }
1342
1343 return 0;
1344 }
1345
1346 pa_rtpoll_item* pa_alsa_build_pollfd(snd_pcm_t *pcm, pa_rtpoll *rtpoll) {
1347 int n, err;
1348 struct pollfd *pollfd;
1349 pa_rtpoll_item *item;
1350
1351 pa_assert(pcm);
1352
1353 if ((n = snd_pcm_poll_descriptors_count(pcm)) < 0) {
1354 pa_log("snd_pcm_poll_descriptors_count() failed: %s", snd_strerror(n));
1355 return NULL;
1356 }
1357
1358 item = pa_rtpoll_item_new(rtpoll, PA_RTPOLL_NEVER, (unsigned) n);
1359 pollfd = pa_rtpoll_item_get_pollfd(item, NULL);
1360
1361 if ((err = snd_pcm_poll_descriptors(pcm, pollfd, (unsigned) n)) < 0) {
1362 pa_log("snd_pcm_poll_descriptors() failed: %s", snd_strerror(err));
1363 pa_rtpoll_item_free(item);
1364 return NULL;
1365 }
1366
1367 return item;
1368 }
1369
1370 snd_pcm_sframes_t pa_alsa_safe_avail_update(snd_pcm_t *pcm, size_t hwbuf_size, const pa_sample_spec *ss) {
1371 snd_pcm_sframes_t n;
1372 size_t k;
1373
1374 pa_assert(pcm);
1375 pa_assert(hwbuf_size > 0);
1376 pa_assert(ss);
1377
1378 /* Some ALSA driver expose weird bugs, let's inform the user about
1379 * what is going on */
1380
1381 n = snd_pcm_avail_update(pcm);
1382
1383 if (n <= 0)
1384 return n;
1385
1386 k = (size_t) n * pa_frame_size(ss);
1387
1388 if (k >= hwbuf_size * 3 ||
1389 k >= pa_bytes_per_second(ss)*10)
1390 pa_log("snd_pcm_avail_update() returned a value that is exceptionally large: %lu bytes (%lu ms) "
1391 "Most likely this is an ALSA driver bug. Please report this issue to the PulseAudio developers.",
1392 (unsigned long) k, (unsigned long) (pa_bytes_to_usec(k, ss) / PA_USEC_PER_MSEC));
1393
1394 return n;
1395 }
1396
1397 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) {
1398 int r;
1399 snd_pcm_uframes_t before;
1400 size_t k;
1401
1402 pa_assert(pcm);
1403 pa_assert(areas);
1404 pa_assert(offset);
1405 pa_assert(frames);
1406 pa_assert(hwbuf_size > 0);
1407 pa_assert(ss);
1408
1409 before = *frames;
1410
1411 r = snd_pcm_mmap_begin(pcm, areas, offset, frames);
1412
1413 if (r < 0)
1414 return r;
1415
1416 k = (size_t) *frames * pa_frame_size(ss);
1417
1418 if (*frames > before ||
1419 k >= hwbuf_size * 3 ||
1420 k >= pa_bytes_per_second(ss)*10)
1421
1422 pa_log("snd_pcm_mmap_begin() returned a value that is exceptionally large: %lu bytes (%lu ms) "
1423 "Most likely this is an ALSA driver bug. Please report this issue to the PulseAudio developers.",
1424 (unsigned long) k, (unsigned long) (pa_bytes_to_usec(k, ss) / PA_USEC_PER_MSEC));
1425
1426 return r;
1427 }