2 This file is part of PulseAudio.
4 Copyright 2004-2009 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>
34 #include <pulse/util.h>
35 #include <pulse/i18n.h>
37 #include <pulsecore/log.h>
38 #include <pulsecore/macro.h>
39 #include <pulsecore/core-util.h>
40 #include <pulsecore/atomic.h>
41 #include <pulsecore/core-error.h>
42 #include <pulsecore/once.h>
44 #include "alsa-util.h"
50 struct pa_alsa_fdlist
{
53 /* This is a temporary buffer used to avoid lots of mallocs */
54 struct pollfd
*work_fds
;
59 pa_defer_event
*defer
;
64 void (*cb
)(void *userdata
);
68 static void io_cb(pa_mainloop_api
*a
, pa_io_event
* e
, int fd
, pa_io_event_flags_t events
, void *userdata
) {
70 struct pa_alsa_fdlist
*fdl
= userdata
;
73 unsigned short revents
;
77 pa_assert(fdl
->mixer
);
79 pa_assert(fdl
->work_fds
);
86 memcpy(fdl
->work_fds
, fdl
->fds
, sizeof(struct pollfd
) * fdl
->num_fds
);
88 for (i
= 0; i
< fdl
->num_fds
; i
++) {
89 if (e
== fdl
->ios
[i
]) {
90 if (events
& PA_IO_EVENT_INPUT
)
91 fdl
->work_fds
[i
].revents
|= POLLIN
;
92 if (events
& PA_IO_EVENT_OUTPUT
)
93 fdl
->work_fds
[i
].revents
|= POLLOUT
;
94 if (events
& PA_IO_EVENT_ERROR
)
95 fdl
->work_fds
[i
].revents
|= POLLERR
;
96 if (events
& PA_IO_EVENT_HANGUP
)
97 fdl
->work_fds
[i
].revents
|= POLLHUP
;
102 pa_assert(i
!= fdl
->num_fds
);
104 if ((err
= snd_mixer_poll_descriptors_revents(fdl
->mixer
, fdl
->work_fds
, fdl
->num_fds
, &revents
)) < 0) {
105 pa_log_error("Unable to get poll revent: %s", snd_strerror(err
));
109 a
->defer_enable(fdl
->defer
, 1);
112 snd_mixer_handle_events(fdl
->mixer
);
115 static void defer_cb(pa_mainloop_api
*a
, pa_defer_event
* e
, void *userdata
) {
116 struct pa_alsa_fdlist
*fdl
= userdata
;
123 pa_assert(fdl
->mixer
);
125 a
->defer_enable(fdl
->defer
, 0);
127 if ((n
= snd_mixer_poll_descriptors_count(fdl
->mixer
)) < 0) {
128 pa_log("snd_mixer_poll_descriptors_count() failed: %s", snd_strerror(n
));
131 num_fds
= (unsigned) n
;
133 if (num_fds
!= fdl
->num_fds
) {
137 pa_xfree(fdl
->work_fds
);
138 fdl
->fds
= pa_xnew0(struct pollfd
, num_fds
);
139 fdl
->work_fds
= pa_xnew(struct pollfd
, num_fds
);
142 memset(fdl
->work_fds
, 0, sizeof(struct pollfd
) * num_fds
);
144 if ((err
= snd_mixer_poll_descriptors(fdl
->mixer
, fdl
->work_fds
, num_fds
)) < 0) {
145 pa_log_error("Unable to get poll descriptors: %s", snd_strerror(err
));
151 if (memcmp(fdl
->fds
, fdl
->work_fds
, sizeof(struct pollfd
) * num_fds
) == 0)
155 for (i
= 0; i
< fdl
->num_fds
; i
++)
156 a
->io_free(fdl
->ios
[i
]);
158 if (num_fds
!= fdl
->num_fds
) {
165 fdl
->ios
= pa_xnew(pa_io_event
*, num_fds
);
168 temp
= fdl
->work_fds
;
169 fdl
->work_fds
= fdl
->fds
;
172 fdl
->num_fds
= num_fds
;
174 for (i
= 0;i
< num_fds
;i
++)
175 fdl
->ios
[i
] = a
->io_new(a
, fdl
->fds
[i
].fd
,
176 ((fdl
->fds
[i
].events
& POLLIN
) ? PA_IO_EVENT_INPUT
: 0) |
177 ((fdl
->fds
[i
].events
& POLLOUT
) ? PA_IO_EVENT_OUTPUT
: 0),
181 struct pa_alsa_fdlist
*pa_alsa_fdlist_new(void) {
182 struct pa_alsa_fdlist
*fdl
;
184 fdl
= pa_xnew0(struct pa_alsa_fdlist
, 1);
188 fdl
->work_fds
= NULL
;
198 void pa_alsa_fdlist_free(struct pa_alsa_fdlist
*fdl
) {
203 fdl
->m
->defer_free(fdl
->defer
);
209 for (i
= 0; i
< fdl
->num_fds
; i
++)
210 fdl
->m
->io_free(fdl
->ios
[i
]);
217 pa_xfree(fdl
->work_fds
);
222 int pa_alsa_fdlist_set_mixer(struct pa_alsa_fdlist
*fdl
, snd_mixer_t
*mixer_handle
, pa_mainloop_api
* m
) {
224 pa_assert(mixer_handle
);
228 fdl
->mixer
= mixer_handle
;
230 fdl
->defer
= m
->defer_new(m
, defer_cb
, fdl
);
235 static int set_format(snd_pcm_t
*pcm_handle
, snd_pcm_hw_params_t
*hwparams
, pa_sample_format_t
*f
) {
237 static const snd_pcm_format_t format_trans
[] = {
238 [PA_SAMPLE_U8
] = SND_PCM_FORMAT_U8
,
239 [PA_SAMPLE_ALAW
] = SND_PCM_FORMAT_A_LAW
,
240 [PA_SAMPLE_ULAW
] = SND_PCM_FORMAT_MU_LAW
,
241 [PA_SAMPLE_S16LE
] = SND_PCM_FORMAT_S16_LE
,
242 [PA_SAMPLE_S16BE
] = SND_PCM_FORMAT_S16_BE
,
243 [PA_SAMPLE_FLOAT32LE
] = SND_PCM_FORMAT_FLOAT_LE
,
244 [PA_SAMPLE_FLOAT32BE
] = SND_PCM_FORMAT_FLOAT_BE
,
245 [PA_SAMPLE_S32LE
] = SND_PCM_FORMAT_S32_LE
,
246 [PA_SAMPLE_S32BE
] = SND_PCM_FORMAT_S32_BE
,
247 [PA_SAMPLE_S24LE
] = SND_PCM_FORMAT_S24_3LE
,
248 [PA_SAMPLE_S24BE
] = SND_PCM_FORMAT_S24_3BE
,
249 [PA_SAMPLE_S24_32LE
] = SND_PCM_FORMAT_S24_LE
,
250 [PA_SAMPLE_S24_32BE
] = SND_PCM_FORMAT_S24_BE
,
253 static const pa_sample_format_t try_order
[] = {
272 pa_assert(pcm_handle
);
275 if ((ret
= snd_pcm_hw_params_set_format(pcm_handle
, hwparams
, format_trans
[*f
])) >= 0)
278 if (*f
== PA_SAMPLE_FLOAT32BE
)
279 *f
= PA_SAMPLE_FLOAT32LE
;
280 else if (*f
== PA_SAMPLE_FLOAT32LE
)
281 *f
= PA_SAMPLE_FLOAT32BE
;
282 else if (*f
== PA_SAMPLE_S24BE
)
283 *f
= PA_SAMPLE_S24LE
;
284 else if (*f
== PA_SAMPLE_S24LE
)
285 *f
= PA_SAMPLE_S24BE
;
286 else if (*f
== PA_SAMPLE_S24_32BE
)
287 *f
= PA_SAMPLE_S24_32LE
;
288 else if (*f
== PA_SAMPLE_S24_32LE
)
289 *f
= PA_SAMPLE_S24_32BE
;
290 else if (*f
== PA_SAMPLE_S16BE
)
291 *f
= PA_SAMPLE_S16LE
;
292 else if (*f
== PA_SAMPLE_S16LE
)
293 *f
= PA_SAMPLE_S16BE
;
294 else if (*f
== PA_SAMPLE_S32BE
)
295 *f
= PA_SAMPLE_S32LE
;
296 else if (*f
== PA_SAMPLE_S32LE
)
297 *f
= PA_SAMPLE_S32BE
;
301 if ((ret
= snd_pcm_hw_params_set_format(pcm_handle
, hwparams
, format_trans
[*f
])) >= 0)
306 for (i
= 0; try_order
[i
] != PA_SAMPLE_INVALID
; i
++) {
309 if ((ret
= snd_pcm_hw_params_set_format(pcm_handle
, hwparams
, format_trans
[*f
])) >= 0)
316 /* Set the hardware parameters of the given ALSA device. Returns the
317 * selected fragment settings in *period and *period_size */
318 int pa_alsa_set_hw_params(
319 snd_pcm_t
*pcm_handle
,
322 snd_pcm_uframes_t
*period_size
,
323 snd_pcm_uframes_t tsched_size
,
325 pa_bool_t
*use_tsched
,
326 pa_bool_t require_exact_channel_number
) {
329 snd_pcm_uframes_t _period_size
= period_size
? *period_size
: 0;
330 unsigned int _periods
= periods
? *periods
: 0;
331 snd_pcm_uframes_t buffer_size
;
332 unsigned int r
= ss
->rate
;
333 unsigned int c
= ss
->channels
;
334 pa_sample_format_t f
= ss
->format
;
335 snd_pcm_hw_params_t
*hwparams
;
336 pa_bool_t _use_mmap
= use_mmap
&& *use_mmap
;
337 pa_bool_t _use_tsched
= use_tsched
&& *use_tsched
;
340 pa_assert(pcm_handle
);
343 snd_pcm_hw_params_alloca(&hwparams
);
345 if ((ret
= snd_pcm_hw_params_any(pcm_handle
, hwparams
)) < 0)
348 if ((ret
= snd_pcm_hw_params_set_rate_resample(pcm_handle
, hwparams
, 0)) < 0)
353 if (snd_pcm_hw_params_set_access(pcm_handle
, hwparams
, SND_PCM_ACCESS_MMAP_INTERLEAVED
) < 0) {
355 /* mmap() didn't work, fall back to interleaved */
357 if ((ret
= snd_pcm_hw_params_set_access(pcm_handle
, hwparams
, SND_PCM_ACCESS_RW_INTERLEAVED
)) < 0)
363 } else if ((ret
= snd_pcm_hw_params_set_access(pcm_handle
, hwparams
, SND_PCM_ACCESS_RW_INTERLEAVED
)) < 0)
369 if ((ret
= set_format(pcm_handle
, hwparams
, &f
)) < 0)
372 if ((ret
= snd_pcm_hw_params_set_rate_near(pcm_handle
, hwparams
, &r
, NULL
)) < 0)
375 if (require_exact_channel_number
) {
376 if ((ret
= snd_pcm_hw_params_set_channels(pcm_handle
, hwparams
, c
)) < 0)
379 if ((ret
= snd_pcm_hw_params_set_channels_near(pcm_handle
, hwparams
, &c
)) < 0)
383 if ((ret
= snd_pcm_hw_params_set_periods_integer(pcm_handle
, hwparams
)) < 0)
386 if (_period_size
&& tsched_size
&& _periods
) {
387 /* Adjust the buffer sizes, if we didn't get the rate we were asking for */
388 _period_size
= (snd_pcm_uframes_t
) (((uint64_t) _period_size
* r
) / ss
->rate
);
389 tsched_size
= (snd_pcm_uframes_t
) (((uint64_t) tsched_size
* r
) / ss
->rate
);
392 _period_size
= tsched_size
;
395 pa_assert_se(snd_pcm_hw_params_get_buffer_size_max(hwparams
, &buffer_size
) == 0);
396 pa_log_debug("Maximum hw buffer size is %u ms", (unsigned) buffer_size
* 1000 / r
);
399 buffer_size
= _periods
* _period_size
;
403 /* First we pass 0 as direction to get exactly what we asked
404 * for. That this is necessary is presumably a bug in ALSA */
407 if ((ret
= snd_pcm_hw_params_set_periods_near(pcm_handle
, hwparams
, &_periods
, &dir
)) < 0) {
409 if ((ret
= snd_pcm_hw_params_set_periods_near(pcm_handle
, hwparams
, &_periods
, &dir
)) < 0) {
411 if ((ret
= snd_pcm_hw_params_set_periods_near(pcm_handle
, hwparams
, &_periods
, &dir
)) < 0)
417 if (_period_size
> 0)
418 if ((ret
= snd_pcm_hw_params_set_buffer_size_near(pcm_handle
, hwparams
, &buffer_size
)) < 0)
422 if ((ret
= snd_pcm_hw_params(pcm_handle
, hwparams
)) < 0)
426 pa_log_info("Device %s doesn't support %u Hz, changed to %u Hz.", snd_pcm_name(pcm_handle
), ss
->rate
, r
);
428 if (ss
->channels
!= c
)
429 pa_log_info("Device %s doesn't support %u channels, changed to %u.", snd_pcm_name(pcm_handle
), ss
->channels
, c
);
432 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
));
434 if ((ret
= snd_pcm_prepare(pcm_handle
)) < 0)
437 if ((ret
= snd_pcm_hw_params_get_period_size(hwparams
, &_period_size
, &dir
)) < 0 ||
438 (ret
= snd_pcm_hw_params_get_periods(hwparams
, &_periods
, &dir
)) < 0)
441 /* If the sample rate deviates too much, we need to resample */
442 if (r
< ss
->rate
*.95 || r
> ss
->rate
*1.05)
444 ss
->channels
= (uint8_t) c
;
447 pa_assert(_periods
> 0);
448 pa_assert(_period_size
> 0);
454 *period_size
= _period_size
;
457 *use_mmap
= _use_mmap
;
460 *use_tsched
= _use_tsched
;
464 snd_pcm_nonblock(pcm_handle
, 1);
471 int pa_alsa_set_sw_params(snd_pcm_t
*pcm
, snd_pcm_uframes_t avail_min
) {
472 snd_pcm_sw_params_t
*swparams
;
473 snd_pcm_uframes_t boundary
;
478 snd_pcm_sw_params_alloca(&swparams
);
480 if ((err
= snd_pcm_sw_params_current(pcm
, swparams
) < 0)) {
481 pa_log_warn("Unable to determine current swparams: %s\n", snd_strerror(err
));
485 if ((err
= snd_pcm_sw_params_set_period_event(pcm
, swparams
, 0)) < 0) {
486 pa_log_warn("Unable to disable period event: %s\n", snd_strerror(err
));
490 if ((err
= snd_pcm_sw_params_set_tstamp_mode(pcm
, swparams
, SND_PCM_TSTAMP_ENABLE
)) < 0) {
491 pa_log_warn("Unable to enable time stamping: %s\n", snd_strerror(err
));
495 if ((err
= snd_pcm_sw_params_get_boundary(swparams
, &boundary
)) < 0) {
496 pa_log_warn("Unable to get boundary: %s\n", snd_strerror(err
));
500 if ((err
= snd_pcm_sw_params_set_stop_threshold(pcm
, swparams
, boundary
)) < 0) {
501 pa_log_warn("Unable to set stop threshold: %s\n", snd_strerror(err
));
505 if ((err
= snd_pcm_sw_params_set_start_threshold(pcm
, swparams
, (snd_pcm_uframes_t
) -1)) < 0) {
506 pa_log_warn("Unable to set start threshold: %s\n", snd_strerror(err
));
510 if ((err
= snd_pcm_sw_params_set_avail_min(pcm
, swparams
, avail_min
)) < 0) {
511 pa_log_error("snd_pcm_sw_params_set_avail_min() failed: %s", snd_strerror(err
));
515 if ((err
= snd_pcm_sw_params(pcm
, swparams
)) < 0) {
516 pa_log_warn("Unable to set sw params: %s\n", snd_strerror(err
));
523 static const struct pa_alsa_profile_info device_table
[] = {
524 {{ 1, { PA_CHANNEL_POSITION_MONO
}},
530 {{ 2, { PA_CHANNEL_POSITION_LEFT
, PA_CHANNEL_POSITION_RIGHT
}},
536 {{ 2, { PA_CHANNEL_POSITION_LEFT
, PA_CHANNEL_POSITION_RIGHT
}},
538 N_("Digital Stereo (IEC958)"),
542 {{ 2, { PA_CHANNEL_POSITION_LEFT
, PA_CHANNEL_POSITION_RIGHT
}},
544 N_("Digital Stereo (HDMI)"),
548 {{ 4, { PA_CHANNEL_POSITION_FRONT_LEFT
, PA_CHANNEL_POSITION_FRONT_RIGHT
,
549 PA_CHANNEL_POSITION_REAR_LEFT
, PA_CHANNEL_POSITION_REAR_RIGHT
}},
551 N_("Analog Surround 4.0"),
552 "analog-surround-40",
555 {{ 4, { PA_CHANNEL_POSITION_FRONT_LEFT
, PA_CHANNEL_POSITION_FRONT_RIGHT
,
556 PA_CHANNEL_POSITION_REAR_LEFT
, PA_CHANNEL_POSITION_REAR_RIGHT
}},
558 N_("Digital Surround 4.0 (IEC958/AC3)"),
559 "iec958-ac3-surround-40",
562 {{ 5, { PA_CHANNEL_POSITION_FRONT_LEFT
, PA_CHANNEL_POSITION_FRONT_RIGHT
,
563 PA_CHANNEL_POSITION_REAR_LEFT
, PA_CHANNEL_POSITION_REAR_RIGHT
,
564 PA_CHANNEL_POSITION_LFE
}},
566 N_("Analog Surround 4.1"),
567 "analog-surround-41",
570 {{ 5, { PA_CHANNEL_POSITION_FRONT_LEFT
, PA_CHANNEL_POSITION_FRONT_RIGHT
,
571 PA_CHANNEL_POSITION_REAR_LEFT
, PA_CHANNEL_POSITION_REAR_RIGHT
,
572 PA_CHANNEL_POSITION_CENTER
}},
574 N_("Analog Surround 5.0"),
575 "analog-surround-50",
578 {{ 6, { PA_CHANNEL_POSITION_FRONT_LEFT
, PA_CHANNEL_POSITION_FRONT_RIGHT
,
579 PA_CHANNEL_POSITION_REAR_LEFT
, PA_CHANNEL_POSITION_REAR_RIGHT
,
580 PA_CHANNEL_POSITION_CENTER
, PA_CHANNEL_POSITION_LFE
}},
582 N_("Analog Surround 5.1"),
583 "analog-surround-51",
586 {{ 6, { PA_CHANNEL_POSITION_FRONT_LEFT
, PA_CHANNEL_POSITION_FRONT_CENTER
,
587 PA_CHANNEL_POSITION_FRONT_RIGHT
, PA_CHANNEL_POSITION_REAR_LEFT
,
588 PA_CHANNEL_POSITION_REAR_RIGHT
, PA_CHANNEL_POSITION_LFE
}},
590 N_("Digital Surround 5.1 (IEC958/AC3)"),
591 "iec958-ac3-surround-51",
594 {{ 8, { PA_CHANNEL_POSITION_FRONT_LEFT
, PA_CHANNEL_POSITION_FRONT_RIGHT
,
595 PA_CHANNEL_POSITION_REAR_LEFT
, PA_CHANNEL_POSITION_REAR_RIGHT
,
596 PA_CHANNEL_POSITION_CENTER
, PA_CHANNEL_POSITION_LFE
,
597 PA_CHANNEL_POSITION_SIDE_LEFT
, PA_CHANNEL_POSITION_SIDE_RIGHT
}},
599 N_("Analog Surround 7.1"),
600 "analog-surround-71",
603 {{ 0, { 0 }}, NULL
, NULL
, NULL
, 0 }
606 snd_pcm_t
*pa_alsa_open_by_device_id_auto(
613 snd_pcm_uframes_t
*period_size
,
614 snd_pcm_uframes_t tsched_size
,
616 pa_bool_t
*use_tsched
,
617 const pa_alsa_profile_info
**profile
) {
622 snd_pcm_t
*pcm_handle
;
629 pa_assert(period_size
);
631 /* First we try to find a device string with a superset of the
632 * requested channel map and open it without the plug: prefix. We
633 * iterate through our device table from top to bottom and take
634 * the first that matches. If we didn't find a working device that
635 * way, we iterate backwards, and check all devices that do not
636 * provide a superset of the requested channel map.*/
641 if ((direction
> 0) == pa_channel_map_superset(&device_table
[i
].map
, map
)) {
642 pa_sample_spec try_ss
;
644 pa_log_debug("Checking for %s (%s)", device_table
[i
].name
, device_table
[i
].alsa_name
);
646 d
= pa_sprintf_malloc("%s:%s", device_table
[i
].alsa_name
, dev_id
);
648 try_ss
.channels
= device_table
[i
].map
.channels
;
649 try_ss
.rate
= ss
->rate
;
650 try_ss
.format
= ss
->format
;
652 pcm_handle
= pa_alsa_open_by_device_string(
670 *map
= device_table
[i
].map
;
671 pa_assert(map
->channels
== ss
->channels
);
674 *profile
= &device_table
[i
];
681 if (!device_table
[i
+1].alsa_name
) {
682 /* OK, so we are at the end of our list. Let's turn
686 /* We are not at the end of the list, so let's simply
687 * try the next entry */
694 if (device_table
[i
+1].alsa_name
&&
695 device_table
[i
].map
.channels
== device_table
[i
+1].map
.channels
) {
697 /* OK, the next entry has the same number of channels,
702 /* Hmm, so the next entry does not have the same
703 * number of channels, so let's go backwards until we
704 * find the next entry with a differnt number of
707 for (i
--; i
>= 0; i
--)
708 if (device_table
[i
].map
.channels
!= device_table
[i
+1].map
.channels
)
711 /* Hmm, there is no entry with a different number of
712 * entries, then we're done */
716 /* OK, now lets find go back as long as we have the same number of channels */
718 if (device_table
[i
].map
.channels
!= device_table
[i
-1].map
.channels
)
724 /* OK, we didn't find any good device, so let's try the raw plughw: stuff */
726 d
= pa_sprintf_malloc("hw:%s", dev_id
);
727 pa_log_debug("Trying %s as last resort...", d
);
728 pcm_handle
= pa_alsa_open_by_device_string(d
, dev
, ss
, map
, mode
, nfrags
, period_size
, tsched_size
, use_mmap
, use_tsched
, FALSE
);
731 if (pcm_handle
&& profile
)
737 snd_pcm_t
*pa_alsa_open_by_device_id_profile(
744 snd_pcm_uframes_t
*period_size
,
745 snd_pcm_uframes_t tsched_size
,
747 pa_bool_t
*use_tsched
,
748 const pa_alsa_profile_info
*profile
) {
751 snd_pcm_t
*pcm_handle
;
752 pa_sample_spec try_ss
;
759 pa_assert(period_size
);
762 d
= pa_sprintf_malloc("%s:%s", profile
->alsa_name
, dev_id
);
764 try_ss
.channels
= profile
->map
.channels
;
765 try_ss
.rate
= ss
->rate
;
766 try_ss
.format
= ss
->format
;
768 pcm_handle
= pa_alsa_open_by_device_string(
788 pa_assert(map
->channels
== ss
->channels
);
793 snd_pcm_t
*pa_alsa_open_by_device_string(
800 snd_pcm_uframes_t
*period_size
,
801 snd_pcm_uframes_t tsched_size
,
803 pa_bool_t
*use_tsched
,
804 pa_bool_t require_exact_channel_number
) {
808 snd_pcm_t
*pcm_handle
;
809 pa_bool_t reformat
= FALSE
;
815 d
= pa_xstrdup(device
);
818 pa_log_debug("Trying %s %s SND_PCM_NO_AUTO_FORMAT ...", d
, reformat
? "without" : "with");
820 /* We don't pass SND_PCM_NONBLOCK here, since alsa-lib <=
821 * 1.0.17a would then ignore the SND_PCM_NO_xxx flags. Instead
822 * we enable nonblock mode afterwards via
823 * snd_pcm_nonblock(). Also see
824 * http://mailman.alsa-project.org/pipermail/alsa-devel/2008-August/010258.html */
826 if ((err
= snd_pcm_open(&pcm_handle
, d
, mode
,
827 /*SND_PCM_NONBLOCK|*/
828 SND_PCM_NO_AUTO_RESAMPLE
|
829 SND_PCM_NO_AUTO_CHANNELS
|
830 (reformat
? 0 : SND_PCM_NO_AUTO_FORMAT
))) < 0) {
831 pa_log_info("Error opening PCM device %s: %s", d
, snd_strerror(err
));
835 if ((err
= pa_alsa_set_hw_params(pcm_handle
, ss
, nfrags
, period_size
, tsched_size
, use_mmap
, use_tsched
, require_exact_channel_number
)) < 0) {
840 snd_pcm_close(pcm_handle
);
844 /* Hmm, some hw is very exotic, so we retry with plug, if without it didn't work */
846 if (!pa_startswith(d
, "plug:") && !pa_startswith(d
, "plughw:")) {
849 t
= pa_sprintf_malloc("plug:%s", d
);
855 snd_pcm_close(pcm_handle
);
859 pa_log_info("Failed to set hardware parameters on %s: %s", d
, snd_strerror(err
));
860 snd_pcm_close(pcm_handle
);
870 if (ss
->channels
!= map
->channels
)
871 pa_channel_map_init_extend(map
, ss
->channels
, PA_CHANNEL_MAP_ALSA
);
882 int pa_alsa_probe_profiles(
884 const pa_sample_spec
*ss
,
885 void (*cb
)(const pa_alsa_profile_info
*sink
, const pa_alsa_profile_info
*source
, void *userdata
),
888 const pa_alsa_profile_info
*i
;
894 /* We try each combination of playback/capture. We also try to
895 * open only for capture resp. only for sink. Don't get confused
896 * by the trailing entry in device_table we use for this! */
898 for (i
= device_table
; i
< device_table
+ PA_ELEMENTSOF(device_table
); i
++) {
899 const pa_alsa_profile_info
*j
;
900 snd_pcm_t
*pcm_i
= NULL
;
904 pa_sample_spec try_ss
;
905 pa_channel_map try_map
;
907 pa_log_debug("Checking for playback on %s (%s)", i
->name
, i
->alsa_name
);
908 id
= pa_sprintf_malloc("%s:%s", i
->alsa_name
, dev_id
);
911 try_ss
.channels
= i
->map
.channels
;
914 pcm_i
= pa_alsa_open_by_device_string(
917 SND_PCM_STREAM_PLAYBACK
,
918 NULL
, NULL
, 0, NULL
, NULL
,
927 for (j
= device_table
; j
< device_table
+ PA_ELEMENTSOF(device_table
); j
++) {
928 snd_pcm_t
*pcm_j
= NULL
;
932 pa_sample_spec try_ss
;
933 pa_channel_map try_map
;
935 pa_log_debug("Checking for capture on %s (%s)", j
->name
, j
->alsa_name
);
936 jd
= pa_sprintf_malloc("%s:%s", j
->alsa_name
, dev_id
);
939 try_ss
.channels
= j
->map
.channels
;
942 pcm_j
= pa_alsa_open_by_device_string(
945 SND_PCM_STREAM_CAPTURE
,
946 NULL
, NULL
, 0, NULL
, NULL
,
956 snd_pcm_close(pcm_j
);
958 if (i
->alsa_name
|| j
->alsa_name
)
959 cb(i
->alsa_name
? i
: NULL
,
960 j
->alsa_name
? j
: NULL
, userdata
);
964 snd_pcm_close(pcm_i
);
970 int pa_alsa_prepare_mixer(snd_mixer_t
*mixer
, const char *dev
) {
976 if ((err
= snd_mixer_attach(mixer
, dev
)) < 0) {
977 pa_log_info("Unable to attach to mixer %s: %s", dev
, snd_strerror(err
));
981 if ((err
= snd_mixer_selem_register(mixer
, NULL
, NULL
)) < 0) {
982 pa_log_warn("Unable to register mixer: %s", snd_strerror(err
));
986 if ((err
= snd_mixer_load(mixer
)) < 0) {
987 pa_log_warn("Unable to load mixer: %s", snd_strerror(err
));
991 pa_log_info("Successfully attached to mixer '%s'", dev
);
996 static pa_bool_t
elem_has_volume(snd_mixer_elem_t
*elem
, pa_bool_t playback
) {
999 if (playback
&& snd_mixer_selem_has_playback_volume(elem
))
1002 if (!playback
&& snd_mixer_selem_has_capture_volume(elem
))
1008 static pa_bool_t
elem_has_switch(snd_mixer_elem_t
*elem
, pa_bool_t playback
) {
1011 if (playback
&& snd_mixer_selem_has_playback_switch(elem
))
1014 if (!playback
&& snd_mixer_selem_has_capture_switch(elem
))
1020 snd_mixer_elem_t
*pa_alsa_find_elem(snd_mixer_t
*mixer
, const char *name
, const char *fallback
, pa_bool_t playback
) {
1021 snd_mixer_elem_t
*elem
= NULL
, *fallback_elem
= NULL
;
1022 snd_mixer_selem_id_t
*sid
= NULL
;
1024 snd_mixer_selem_id_alloca(&sid
);
1029 snd_mixer_selem_id_set_name(sid
, name
);
1030 snd_mixer_selem_id_set_index(sid
, 0);
1032 if ((elem
= snd_mixer_find_selem(mixer
, sid
))) {
1034 if (elem_has_volume(elem
, playback
) &&
1035 elem_has_switch(elem
, playback
))
1038 if (!elem_has_volume(elem
, playback
) &&
1039 !elem_has_switch(elem
, playback
))
1043 pa_log_info("Cannot find mixer control \"%s\" or mixer control is no combination of switch/volume.", snd_mixer_selem_id_get_name(sid
));
1046 snd_mixer_selem_id_set_name(sid
, fallback
);
1047 snd_mixer_selem_id_set_index(sid
, 0);
1049 if ((fallback_elem
= snd_mixer_find_selem(mixer
, sid
))) {
1051 if (elem_has_volume(fallback_elem
, playback
) &&
1052 elem_has_switch(fallback_elem
, playback
)) {
1053 elem
= fallback_elem
;
1057 if (!elem_has_volume(fallback_elem
, playback
) &&
1058 !elem_has_switch(fallback_elem
, playback
))
1059 fallback_elem
= NULL
;
1062 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
));
1065 if (elem
&& fallback_elem
) {
1067 /* Hmm, so we have both elements, but neither has both mute
1068 * and volume. Let's prefer the one with the volume */
1070 if (elem_has_volume(elem
, playback
))
1073 if (elem_has_volume(fallback_elem
, playback
)) {
1074 elem
= fallback_elem
;
1079 if (!elem
&& fallback_elem
)
1080 elem
= fallback_elem
;
1085 pa_log_info("Using mixer control \"%s\".", snd_mixer_selem_id_get_name(sid
));
1090 int pa_alsa_find_mixer_and_elem(
1093 snd_mixer_elem_t
**_e
) {
1097 snd_mixer_elem_t
*e
;
1098 pa_bool_t found
= FALSE
;
1105 if ((err
= snd_mixer_open(&m
, 0)) < 0) {
1106 pa_log("Error opening mixer: %s", snd_strerror(err
));
1110 /* First, try by name */
1111 if ((dev
= snd_pcm_name(pcm
)))
1112 if (pa_alsa_prepare_mixer(m
, dev
) >= 0)
1115 /* Then, try by card index */
1117 snd_pcm_info_t
* info
;
1118 snd_pcm_info_alloca(&info
);
1120 if (snd_pcm_info(pcm
, info
) >= 0) {
1124 if ((card_idx
= snd_pcm_info_get_card(info
)) >= 0) {
1126 md
= pa_sprintf_malloc("hw:%i", card_idx
);
1128 if (!dev
|| !pa_streq(dev
, md
))
1129 if (pa_alsa_prepare_mixer(m
, md
) >= 0)
1142 switch (snd_pcm_stream(pcm
)) {
1144 case SND_PCM_STREAM_PLAYBACK
:
1145 e
= pa_alsa_find_elem(m
, "Master", "PCM", TRUE
);
1148 case SND_PCM_STREAM_CAPTURE
:
1149 e
= pa_alsa_find_elem(m
, "Capture", "Mic", FALSE
);
1153 pa_assert_not_reached();
1169 static const snd_mixer_selem_channel_id_t alsa_channel_ids
[PA_CHANNEL_POSITION_MAX
] = {
1170 [PA_CHANNEL_POSITION_MONO
] = SND_MIXER_SCHN_MONO
, /* The ALSA name is just an alias! */
1172 [PA_CHANNEL_POSITION_FRONT_CENTER
] = SND_MIXER_SCHN_FRONT_CENTER
,
1173 [PA_CHANNEL_POSITION_FRONT_LEFT
] = SND_MIXER_SCHN_FRONT_LEFT
,
1174 [PA_CHANNEL_POSITION_FRONT_RIGHT
] = SND_MIXER_SCHN_FRONT_RIGHT
,
1176 [PA_CHANNEL_POSITION_REAR_CENTER
] = SND_MIXER_SCHN_REAR_CENTER
,
1177 [PA_CHANNEL_POSITION_REAR_LEFT
] = SND_MIXER_SCHN_REAR_LEFT
,
1178 [PA_CHANNEL_POSITION_REAR_RIGHT
] = SND_MIXER_SCHN_REAR_RIGHT
,
1180 [PA_CHANNEL_POSITION_LFE
] = SND_MIXER_SCHN_WOOFER
,
1182 [PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER
] = SND_MIXER_SCHN_UNKNOWN
,
1183 [PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER
] = SND_MIXER_SCHN_UNKNOWN
,
1185 [PA_CHANNEL_POSITION_SIDE_LEFT
] = SND_MIXER_SCHN_SIDE_LEFT
,
1186 [PA_CHANNEL_POSITION_SIDE_RIGHT
] = SND_MIXER_SCHN_SIDE_RIGHT
,
1188 [PA_CHANNEL_POSITION_AUX0
] = SND_MIXER_SCHN_UNKNOWN
,
1189 [PA_CHANNEL_POSITION_AUX1
] = SND_MIXER_SCHN_UNKNOWN
,
1190 [PA_CHANNEL_POSITION_AUX2
] = SND_MIXER_SCHN_UNKNOWN
,
1191 [PA_CHANNEL_POSITION_AUX3
] = SND_MIXER_SCHN_UNKNOWN
,
1192 [PA_CHANNEL_POSITION_AUX4
] = SND_MIXER_SCHN_UNKNOWN
,
1193 [PA_CHANNEL_POSITION_AUX5
] = SND_MIXER_SCHN_UNKNOWN
,
1194 [PA_CHANNEL_POSITION_AUX6
] = SND_MIXER_SCHN_UNKNOWN
,
1195 [PA_CHANNEL_POSITION_AUX7
] = SND_MIXER_SCHN_UNKNOWN
,
1196 [PA_CHANNEL_POSITION_AUX8
] = SND_MIXER_SCHN_UNKNOWN
,
1197 [PA_CHANNEL_POSITION_AUX9
] = SND_MIXER_SCHN_UNKNOWN
,
1198 [PA_CHANNEL_POSITION_AUX10
] = SND_MIXER_SCHN_UNKNOWN
,
1199 [PA_CHANNEL_POSITION_AUX11
] = SND_MIXER_SCHN_UNKNOWN
,
1200 [PA_CHANNEL_POSITION_AUX12
] = SND_MIXER_SCHN_UNKNOWN
,
1201 [PA_CHANNEL_POSITION_AUX13
] = SND_MIXER_SCHN_UNKNOWN
,
1202 [PA_CHANNEL_POSITION_AUX14
] = SND_MIXER_SCHN_UNKNOWN
,
1203 [PA_CHANNEL_POSITION_AUX15
] = SND_MIXER_SCHN_UNKNOWN
,
1204 [PA_CHANNEL_POSITION_AUX16
] = SND_MIXER_SCHN_UNKNOWN
,
1205 [PA_CHANNEL_POSITION_AUX17
] = SND_MIXER_SCHN_UNKNOWN
,
1206 [PA_CHANNEL_POSITION_AUX18
] = SND_MIXER_SCHN_UNKNOWN
,
1207 [PA_CHANNEL_POSITION_AUX19
] = SND_MIXER_SCHN_UNKNOWN
,
1208 [PA_CHANNEL_POSITION_AUX20
] = SND_MIXER_SCHN_UNKNOWN
,
1209 [PA_CHANNEL_POSITION_AUX21
] = SND_MIXER_SCHN_UNKNOWN
,
1210 [PA_CHANNEL_POSITION_AUX22
] = SND_MIXER_SCHN_UNKNOWN
,
1211 [PA_CHANNEL_POSITION_AUX23
] = SND_MIXER_SCHN_UNKNOWN
,
1212 [PA_CHANNEL_POSITION_AUX24
] = SND_MIXER_SCHN_UNKNOWN
,
1213 [PA_CHANNEL_POSITION_AUX25
] = SND_MIXER_SCHN_UNKNOWN
,
1214 [PA_CHANNEL_POSITION_AUX26
] = SND_MIXER_SCHN_UNKNOWN
,
1215 [PA_CHANNEL_POSITION_AUX27
] = SND_MIXER_SCHN_UNKNOWN
,
1216 [PA_CHANNEL_POSITION_AUX28
] = SND_MIXER_SCHN_UNKNOWN
,
1217 [PA_CHANNEL_POSITION_AUX29
] = SND_MIXER_SCHN_UNKNOWN
,
1218 [PA_CHANNEL_POSITION_AUX30
] = SND_MIXER_SCHN_UNKNOWN
,
1219 [PA_CHANNEL_POSITION_AUX31
] = SND_MIXER_SCHN_UNKNOWN
,
1221 [PA_CHANNEL_POSITION_TOP_CENTER
] = SND_MIXER_SCHN_UNKNOWN
,
1223 [PA_CHANNEL_POSITION_TOP_FRONT_CENTER
] = SND_MIXER_SCHN_UNKNOWN
,
1224 [PA_CHANNEL_POSITION_TOP_FRONT_LEFT
] = SND_MIXER_SCHN_UNKNOWN
,
1225 [PA_CHANNEL_POSITION_TOP_FRONT_RIGHT
] = SND_MIXER_SCHN_UNKNOWN
,
1227 [PA_CHANNEL_POSITION_TOP_REAR_CENTER
] = SND_MIXER_SCHN_UNKNOWN
,
1228 [PA_CHANNEL_POSITION_TOP_REAR_LEFT
] = SND_MIXER_SCHN_UNKNOWN
,
1229 [PA_CHANNEL_POSITION_TOP_REAR_RIGHT
] = SND_MIXER_SCHN_UNKNOWN
1233 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
) {
1235 pa_bool_t alsa_channel_used
[SND_MIXER_SCHN_LAST
];
1236 pa_bool_t mono_used
= FALSE
;
1239 pa_assert(channel_map
);
1240 pa_assert(mixer_map
);
1242 memset(&alsa_channel_used
, 0, sizeof(alsa_channel_used
));
1244 if (channel_map
->channels
> 1 &&
1245 ((playback
&& snd_mixer_selem_has_playback_volume_joined(elem
)) ||
1246 (!playback
&& snd_mixer_selem_has_capture_volume_joined(elem
)))) {
1247 pa_log_info("ALSA device lacks independant volume controls for each channel.");
1251 for (i
= 0; i
< channel_map
->channels
; i
++) {
1252 snd_mixer_selem_channel_id_t id
;
1255 is_mono
= channel_map
->map
[i
] == PA_CHANNEL_POSITION_MONO
;
1256 id
= alsa_channel_ids
[channel_map
->map
[i
]];
1258 if (!is_mono
&& id
== SND_MIXER_SCHN_UNKNOWN
) {
1259 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
]));
1263 if ((is_mono
&& mono_used
) || (!is_mono
&& alsa_channel_used
[id
])) {
1264 pa_log_info("Channel map has duplicate channel '%s', falling back to software volume control.", pa_channel_position_to_string(channel_map
->map
[i
]));
1268 if ((playback
&& (!snd_mixer_selem_has_playback_channel(elem
, id
) || (is_mono
&& !snd_mixer_selem_is_playback_mono(elem
)))) ||
1269 (!playback
&& (!snd_mixer_selem_has_capture_channel(elem
, id
) || (is_mono
&& !snd_mixer_selem_is_capture_mono(elem
))))) {
1271 pa_log_info("ALSA device lacks separate volumes control for channel '%s'", pa_channel_position_to_string(channel_map
->map
[i
]));
1276 mixer_map
[i
] = SND_MIXER_SCHN_MONO
;
1280 alsa_channel_used
[id
] = TRUE
;
1284 pa_log_info("All %u channels can be mapped to mixer channels.", channel_map
->channels
);
1289 void pa_alsa_dump(snd_pcm_t
*pcm
) {
1295 pa_assert_se(snd_output_buffer_open(&out
) == 0);
1297 if ((err
= snd_pcm_dump(pcm
, out
)) < 0)
1298 pa_log_debug("snd_pcm_dump(): %s", snd_strerror(err
));
1301 snd_output_buffer_string(out
, &s
);
1302 pa_log_debug("snd_pcm_dump():\n%s", pa_strnull(s
));
1305 pa_assert_se(snd_output_close(out
) == 0);
1308 void pa_alsa_dump_status(snd_pcm_t
*pcm
) {
1311 snd_pcm_status_t
*status
;
1315 snd_pcm_status_alloca(&status
);
1317 pa_assert_se(snd_output_buffer_open(&out
) == 0);
1319 pa_assert_se(snd_pcm_status(pcm
, status
) == 0);
1321 if ((err
= snd_pcm_status_dump(status
, out
)) < 0)
1322 pa_log_debug("snd_pcm_dump(): %s", snd_strerror(err
));
1325 snd_output_buffer_string(out
, &s
);
1326 pa_log_debug("snd_pcm_dump():\n%s", pa_strnull(s
));
1329 pa_assert_se(snd_output_close(out
) == 0);
1332 static void alsa_error_handler(const char *file
, int line
, const char *function
, int err
, const char *fmt
,...) {
1336 alsa_file
= pa_sprintf_malloc("(alsa-lib)%s", file
);
1340 pa_log_levelv_meta(PA_LOG_INFO
, alsa_file
, line
, function
, fmt
, ap
);
1344 pa_xfree(alsa_file
);
1347 static pa_atomic_t n_error_handler_installed
= PA_ATOMIC_INIT(0);
1349 void pa_alsa_redirect_errors_inc(void) {
1350 /* This is not really thread safe, but we do our best */
1352 if (pa_atomic_inc(&n_error_handler_installed
) == 0)
1353 snd_lib_error_set_handler(alsa_error_handler
);
1356 void pa_alsa_redirect_errors_dec(void) {
1359 pa_assert_se((r
= pa_atomic_dec(&n_error_handler_installed
)) >= 1);
1362 snd_lib_error_set_handler(NULL
);
1365 void pa_alsa_init_proplist_card(pa_core
*c
, pa_proplist
*p
, int card
) {
1366 char *cn
, *lcn
, *dn
;
1369 pa_assert(card
>= 0);
1371 pa_proplist_setf(p
, "alsa.card", "%i", card
);
1373 if (snd_card_get_name(card
, &cn
) >= 0) {
1374 pa_proplist_sets(p
, "alsa.card_name", cn
);
1378 if (snd_card_get_longname(card
, &lcn
) >= 0) {
1379 pa_proplist_sets(p
, "alsa.long_card_name", lcn
);
1383 if ((dn
= pa_alsa_get_driver_name(card
))) {
1384 pa_proplist_sets(p
, "alsa.driver_name", dn
);
1389 pa_hal_get_info(c
, p
, card
);
1393 void pa_alsa_init_proplist_pcm_info(pa_core
*c
, pa_proplist
*p
, snd_pcm_info_t
*pcm_info
) {
1395 static const char * const alsa_class_table
[SND_PCM_CLASS_LAST
+1] = {
1396 [SND_PCM_CLASS_GENERIC
] = "generic",
1397 [SND_PCM_CLASS_MULTI
] = "multi",
1398 [SND_PCM_CLASS_MODEM
] = "modem",
1399 [SND_PCM_CLASS_DIGITIZER
] = "digitizer"
1401 static const char * const class_table
[SND_PCM_CLASS_LAST
+1] = {
1402 [SND_PCM_CLASS_GENERIC
] = "sound",
1403 [SND_PCM_CLASS_MULTI
] = NULL
,
1404 [SND_PCM_CLASS_MODEM
] = "modem",
1405 [SND_PCM_CLASS_DIGITIZER
] = NULL
1407 static const char * const alsa_subclass_table
[SND_PCM_SUBCLASS_LAST
+1] = {
1408 [SND_PCM_SUBCLASS_GENERIC_MIX
] = "generic-mix",
1409 [SND_PCM_SUBCLASS_MULTI_MIX
] = "multi-mix"
1412 snd_pcm_class_t
class;
1413 snd_pcm_subclass_t subclass
;
1414 const char *n
, *id
, *sdn
, *cn
= NULL
;
1418 pa_assert(pcm_info
);
1420 pa_proplist_sets(p
, PA_PROP_DEVICE_API
, "alsa");
1422 class = snd_pcm_info_get_class(pcm_info
);
1423 if (class <= SND_PCM_CLASS_LAST
) {
1424 if (class_table
[class])
1425 pa_proplist_sets(p
, PA_PROP_DEVICE_CLASS
, class_table
[class]);
1426 if (alsa_class_table
[class])
1427 pa_proplist_sets(p
, "alsa.class", alsa_class_table
[class]);
1429 subclass
= snd_pcm_info_get_subclass(pcm_info
);
1430 if (subclass
<= SND_PCM_SUBCLASS_LAST
)
1431 if (alsa_subclass_table
[subclass
])
1432 pa_proplist_sets(p
, "alsa.subclass", alsa_subclass_table
[subclass
]);
1434 if ((n
= snd_pcm_info_get_name(pcm_info
)))
1435 pa_proplist_sets(p
, "alsa.name", n
);
1437 if ((id
= snd_pcm_info_get_id(pcm_info
)))
1438 pa_proplist_sets(p
, "alsa.id", id
);
1440 pa_proplist_setf(p
, "alsa.subdevice", "%u", snd_pcm_info_get_subdevice(pcm_info
));
1441 if ((sdn
= snd_pcm_info_get_subdevice_name(pcm_info
)))
1442 pa_proplist_sets(p
, "alsa.subdevice_name", sdn
);
1444 pa_proplist_setf(p
, "alsa.device", "%u", snd_pcm_info_get_device(pcm_info
));
1446 if ((card
= snd_pcm_info_get_card(pcm_info
)) >= 0) {
1447 pa_alsa_init_proplist_card(c
, p
, card
);
1448 cn
= pa_proplist_gets(p
, "alsa.card_name");
1451 if (cn
&& n
&& !strstr(cn
, n
) && !strstr(n
, cn
))
1452 pa_proplist_setf(p
, PA_PROP_DEVICE_DESCRIPTION
, "%s, %s", cn
, n
);
1453 else if (cn
&& (!n
|| strstr(cn
, n
)))
1454 pa_proplist_sets(p
, PA_PROP_DEVICE_DESCRIPTION
, cn
);
1456 pa_proplist_sets(p
, PA_PROP_DEVICE_DESCRIPTION
, n
);
1459 void pa_alsa_init_proplist_pcm(pa_core
*c
, pa_proplist
*p
, snd_pcm_t
*pcm
) {
1460 snd_pcm_hw_params_t
*hwparams
;
1461 snd_pcm_info_t
*info
;
1464 snd_pcm_hw_params_alloca(&hwparams
);
1465 snd_pcm_info_alloca(&info
);
1467 if ((err
= snd_pcm_hw_params_current(pcm
, hwparams
)) < 0)
1468 pa_log_warn("Error fetching hardware parameter info: %s", snd_strerror(err
));
1471 if ((bits
= snd_pcm_hw_params_get_sbits(hwparams
)) >= 0)
1472 pa_proplist_setf(p
, "alsa.resolution_bits", "%i", bits
);
1475 if ((err
= snd_pcm_info(pcm
, info
)) < 0)
1476 pa_log_warn("Error fetching PCM info: %s", snd_strerror(err
));
1478 pa_alsa_init_proplist_pcm_info(c
, p
, info
);
1481 int pa_alsa_recover_from_poll(snd_pcm_t
*pcm
, int revents
) {
1482 snd_pcm_state_t state
;
1487 if (revents
& POLLERR
)
1488 pa_log_debug("Got POLLERR from ALSA");
1489 if (revents
& POLLNVAL
)
1490 pa_log_warn("Got POLLNVAL from ALSA");
1491 if (revents
& POLLHUP
)
1492 pa_log_warn("Got POLLHUP from ALSA");
1493 if (revents
& POLLPRI
)
1494 pa_log_warn("Got POLLPRI from ALSA");
1495 if (revents
& POLLIN
)
1496 pa_log_debug("Got POLLIN from ALSA");
1497 if (revents
& POLLOUT
)
1498 pa_log_debug("Got POLLOUT from ALSA");
1500 state
= snd_pcm_state(pcm
);
1501 pa_log_debug("PCM state is %s", snd_pcm_state_name(state
));
1503 /* Try to recover from this error */
1507 case SND_PCM_STATE_XRUN
:
1508 if ((err
= snd_pcm_recover(pcm
, -EPIPE
, 1)) != 0) {
1509 pa_log_warn("Could not recover from POLLERR|POLLNVAL|POLLHUP and XRUN: %s", snd_strerror(err
));
1514 case SND_PCM_STATE_SUSPENDED
:
1515 if ((err
= snd_pcm_recover(pcm
, -ESTRPIPE
, 1)) != 0) {
1516 pa_log_warn("Could not recover from POLLERR|POLLNVAL|POLLHUP and SUSPENDED: %s", snd_strerror(err
));
1525 if ((err
= snd_pcm_prepare(pcm
)) < 0) {
1526 pa_log_warn("Could not recover from POLLERR|POLLNVAL|POLLHUP with snd_pcm_prepare(): %s", snd_strerror(err
));
1535 pa_rtpoll_item
* pa_alsa_build_pollfd(snd_pcm_t
*pcm
, pa_rtpoll
*rtpoll
) {
1537 struct pollfd
*pollfd
;
1538 pa_rtpoll_item
*item
;
1542 if ((n
= snd_pcm_poll_descriptors_count(pcm
)) < 0) {
1543 pa_log("snd_pcm_poll_descriptors_count() failed: %s", snd_strerror(n
));
1547 item
= pa_rtpoll_item_new(rtpoll
, PA_RTPOLL_NEVER
, (unsigned) n
);
1548 pollfd
= pa_rtpoll_item_get_pollfd(item
, NULL
);
1550 if ((err
= snd_pcm_poll_descriptors(pcm
, pollfd
, (unsigned) n
)) < 0) {
1551 pa_log("snd_pcm_poll_descriptors() failed: %s", snd_strerror(err
));
1552 pa_rtpoll_item_free(item
);
1559 snd_pcm_sframes_t
pa_alsa_safe_avail(snd_pcm_t
*pcm
, size_t hwbuf_size
, const pa_sample_spec
*ss
) {
1560 snd_pcm_sframes_t n
;
1564 pa_assert(hwbuf_size
> 0);
1567 /* Some ALSA driver expose weird bugs, let's inform the user about
1568 * what is going on */
1570 n
= snd_pcm_avail(pcm
);
1575 k
= (size_t) n
* pa_frame_size(ss
);
1577 if (k
>= hwbuf_size
* 5 ||
1578 k
>= pa_bytes_per_second(ss
)*10) {
1581 char *dn
= pa_alsa_get_driver_name_by_pcm(pcm
);
1582 pa_log(_("snd_pcm_avail() returned a value that is exceptionally large: %lu bytes (%lu ms).\n"
1583 "Most likely this is a bug in the ALSA driver '%s'. Please report this issue to the ALSA developers."),
1585 (unsigned long) (pa_bytes_to_usec(k
, ss
) / PA_USEC_PER_MSEC
),
1590 /* Mhmm, let's try not to fail completely */
1591 n
= (snd_pcm_sframes_t
) (hwbuf_size
/ pa_frame_size(ss
));
1597 int pa_alsa_safe_delay(snd_pcm_t
*pcm
, snd_pcm_sframes_t
*delay
, size_t hwbuf_size
, const pa_sample_spec
*ss
) {
1604 pa_assert(hwbuf_size
> 0);
1607 /* Some ALSA driver expose weird bugs, let's inform the user about
1608 * what is going on */
1610 if ((r
= snd_pcm_delay(pcm
, delay
)) < 0)
1613 k
= (ssize_t
) *delay
* (ssize_t
) pa_frame_size(ss
);
1615 abs_k
= k
>= 0 ? (size_t) k
: (size_t) -k
;
1617 if (abs_k
>= hwbuf_size
* 5 ||
1618 abs_k
>= pa_bytes_per_second(ss
)*10) {
1621 char *dn
= pa_alsa_get_driver_name_by_pcm(pcm
);
1622 pa_log(_("snd_pcm_delay() returned a value that is exceptionally large: %li bytes (%s%lu ms).\n"
1623 "Most likely this is a bug in the ALSA driver '%s'. Please report this issue to the ALSA developers."),
1626 (unsigned long) (pa_bytes_to_usec(abs_k
, ss
) / PA_USEC_PER_MSEC
),
1631 /* Mhmm, let's try not to fail completely */
1633 *delay
= -(snd_pcm_sframes_t
) (hwbuf_size
/ pa_frame_size(ss
));
1635 *delay
= (snd_pcm_sframes_t
) (hwbuf_size
/ pa_frame_size(ss
));
1641 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
) {
1643 snd_pcm_uframes_t before
;
1650 pa_assert(hwbuf_size
> 0);
1655 r
= snd_pcm_mmap_begin(pcm
, areas
, offset
, frames
);
1660 k
= (size_t) *frames
* pa_frame_size(ss
);
1662 if (*frames
> before
||
1663 k
>= hwbuf_size
* 3 ||
1664 k
>= pa_bytes_per_second(ss
)*10)
1667 char *dn
= pa_alsa_get_driver_name_by_pcm(pcm
);
1668 pa_log(_("snd_pcm_mmap_begin() returned a value that is exceptionally large: %lu bytes (%lu ms).\n"
1669 "Most likely this is a bug in the ALSA driver '%s'. Please report this issue to the ALSA developers."),
1671 (unsigned long) (pa_bytes_to_usec(k
, ss
) / PA_USEC_PER_MSEC
),
1679 char *pa_alsa_get_driver_name(int card
) {
1682 pa_assert(card
>= 0);
1684 t
= pa_sprintf_malloc("/sys/class/sound/card%i/device/driver/module", card
);
1691 n
= pa_xstrdup(pa_path_get_filename(m
));
1697 char *pa_alsa_get_driver_name_by_pcm(snd_pcm_t
*pcm
) {
1700 snd_pcm_info_t
* info
;
1701 snd_pcm_info_alloca(&info
);
1703 if (snd_pcm_info(pcm
, info
) < 0)
1706 if ((card
= snd_pcm_info_get_card(info
)) < 0)
1709 return pa_alsa_get_driver_name(card
);
1712 char *pa_alsa_get_reserve_name(const char *device
) {
1718 if ((t
= strchr(device
, ':')))
1721 if ((i
= snd_card_get_index(device
)) < 0) {
1724 if (pa_atoi(device
, &k
) < 0)
1730 return pa_sprintf_malloc("Audio%i", i
);