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>
43 #include "alsa-util.h"
49 struct pa_alsa_fdlist
{
52 /* This is a temporary buffer used to avoid lots of mallocs */
53 struct pollfd
*work_fds
;
58 pa_defer_event
*defer
;
63 void (*cb
)(void *userdata
);
67 static void io_cb(pa_mainloop_api
*a
, pa_io_event
* e
, int fd
, pa_io_event_flags_t events
, void *userdata
) {
69 struct pa_alsa_fdlist
*fdl
= userdata
;
72 unsigned short revents
;
76 pa_assert(fdl
->mixer
);
78 pa_assert(fdl
->work_fds
);
85 memcpy(fdl
->work_fds
, fdl
->fds
, sizeof(struct pollfd
) * fdl
->num_fds
);
87 for (i
= 0; i
< fdl
->num_fds
; i
++) {
88 if (e
== fdl
->ios
[i
]) {
89 if (events
& PA_IO_EVENT_INPUT
)
90 fdl
->work_fds
[i
].revents
|= POLLIN
;
91 if (events
& PA_IO_EVENT_OUTPUT
)
92 fdl
->work_fds
[i
].revents
|= POLLOUT
;
93 if (events
& PA_IO_EVENT_ERROR
)
94 fdl
->work_fds
[i
].revents
|= POLLERR
;
95 if (events
& PA_IO_EVENT_HANGUP
)
96 fdl
->work_fds
[i
].revents
|= POLLHUP
;
101 pa_assert(i
!= fdl
->num_fds
);
103 if ((err
= snd_mixer_poll_descriptors_revents(fdl
->mixer
, fdl
->work_fds
, fdl
->num_fds
, &revents
)) < 0) {
104 pa_log_error("Unable to get poll revent: %s", snd_strerror(err
));
108 a
->defer_enable(fdl
->defer
, 1);
111 snd_mixer_handle_events(fdl
->mixer
);
114 static void defer_cb(pa_mainloop_api
*a
, pa_defer_event
* e
, void *userdata
) {
115 struct pa_alsa_fdlist
*fdl
= userdata
;
122 pa_assert(fdl
->mixer
);
124 a
->defer_enable(fdl
->defer
, 0);
126 if ((n
= snd_mixer_poll_descriptors_count(fdl
->mixer
)) < 0) {
127 pa_log("snd_mixer_poll_descriptors_count() failed: %s", snd_strerror(n
));
130 num_fds
= (unsigned) n
;
132 if (num_fds
!= fdl
->num_fds
) {
136 pa_xfree(fdl
->work_fds
);
137 fdl
->fds
= pa_xnew0(struct pollfd
, num_fds
);
138 fdl
->work_fds
= pa_xnew(struct pollfd
, num_fds
);
141 memset(fdl
->work_fds
, 0, sizeof(struct pollfd
) * num_fds
);
143 if ((err
= snd_mixer_poll_descriptors(fdl
->mixer
, fdl
->work_fds
, num_fds
)) < 0) {
144 pa_log_error("Unable to get poll descriptors: %s", snd_strerror(err
));
150 if (memcmp(fdl
->fds
, fdl
->work_fds
, sizeof(struct pollfd
) * num_fds
) == 0)
154 for (i
= 0; i
< fdl
->num_fds
; i
++)
155 a
->io_free(fdl
->ios
[i
]);
157 if (num_fds
!= fdl
->num_fds
) {
164 fdl
->ios
= pa_xnew(pa_io_event
*, num_fds
);
167 temp
= fdl
->work_fds
;
168 fdl
->work_fds
= fdl
->fds
;
171 fdl
->num_fds
= num_fds
;
173 for (i
= 0;i
< num_fds
;i
++)
174 fdl
->ios
[i
] = a
->io_new(a
, fdl
->fds
[i
].fd
,
175 ((fdl
->fds
[i
].events
& POLLIN
) ? PA_IO_EVENT_INPUT
: 0) |
176 ((fdl
->fds
[i
].events
& POLLOUT
) ? PA_IO_EVENT_OUTPUT
: 0),
180 struct pa_alsa_fdlist
*pa_alsa_fdlist_new(void) {
181 struct pa_alsa_fdlist
*fdl
;
183 fdl
= pa_xnew0(struct pa_alsa_fdlist
, 1);
187 fdl
->work_fds
= NULL
;
197 void pa_alsa_fdlist_free(struct pa_alsa_fdlist
*fdl
) {
202 fdl
->m
->defer_free(fdl
->defer
);
208 for (i
= 0; i
< fdl
->num_fds
; i
++)
209 fdl
->m
->io_free(fdl
->ios
[i
]);
216 pa_xfree(fdl
->work_fds
);
221 int pa_alsa_fdlist_set_mixer(struct pa_alsa_fdlist
*fdl
, snd_mixer_t
*mixer_handle
, pa_mainloop_api
* m
) {
223 pa_assert(mixer_handle
);
227 fdl
->mixer
= mixer_handle
;
229 fdl
->defer
= m
->defer_new(m
, defer_cb
, fdl
);
234 static int set_format(snd_pcm_t
*pcm_handle
, snd_pcm_hw_params_t
*hwparams
, pa_sample_format_t
*f
) {
236 static const snd_pcm_format_t format_trans
[] = {
237 [PA_SAMPLE_U8
] = SND_PCM_FORMAT_U8
,
238 [PA_SAMPLE_ALAW
] = SND_PCM_FORMAT_A_LAW
,
239 [PA_SAMPLE_ULAW
] = SND_PCM_FORMAT_MU_LAW
,
240 [PA_SAMPLE_S16LE
] = SND_PCM_FORMAT_S16_LE
,
241 [PA_SAMPLE_S16BE
] = SND_PCM_FORMAT_S16_BE
,
242 [PA_SAMPLE_FLOAT32LE
] = SND_PCM_FORMAT_FLOAT_LE
,
243 [PA_SAMPLE_FLOAT32BE
] = SND_PCM_FORMAT_FLOAT_BE
,
244 [PA_SAMPLE_S32LE
] = SND_PCM_FORMAT_S32_LE
,
245 [PA_SAMPLE_S32BE
] = SND_PCM_FORMAT_S32_BE
,
246 [PA_SAMPLE_S24LE
] = SND_PCM_FORMAT_S24_3LE
,
247 [PA_SAMPLE_S24BE
] = SND_PCM_FORMAT_S24_3BE
,
248 [PA_SAMPLE_S24_32LE
] = SND_PCM_FORMAT_S24_LE
,
249 [PA_SAMPLE_S24_32BE
] = SND_PCM_FORMAT_S24_BE
,
252 static const pa_sample_format_t try_order
[] = {
271 pa_assert(pcm_handle
);
274 if ((ret
= snd_pcm_hw_params_set_format(pcm_handle
, hwparams
, format_trans
[*f
])) >= 0)
277 if (*f
== PA_SAMPLE_FLOAT32BE
)
278 *f
= PA_SAMPLE_FLOAT32LE
;
279 else if (*f
== PA_SAMPLE_FLOAT32LE
)
280 *f
= PA_SAMPLE_FLOAT32BE
;
281 else if (*f
== PA_SAMPLE_S24BE
)
282 *f
= PA_SAMPLE_S24LE
;
283 else if (*f
== PA_SAMPLE_S24LE
)
284 *f
= PA_SAMPLE_S24BE
;
285 else if (*f
== PA_SAMPLE_S24_32BE
)
286 *f
= PA_SAMPLE_S24_32LE
;
287 else if (*f
== PA_SAMPLE_S24_32LE
)
288 *f
= PA_SAMPLE_S24_32BE
;
289 else if (*f
== PA_SAMPLE_S16BE
)
290 *f
= PA_SAMPLE_S16LE
;
291 else if (*f
== PA_SAMPLE_S16LE
)
292 *f
= PA_SAMPLE_S16BE
;
293 else if (*f
== PA_SAMPLE_S32BE
)
294 *f
= PA_SAMPLE_S32LE
;
295 else if (*f
== PA_SAMPLE_S32LE
)
296 *f
= PA_SAMPLE_S32BE
;
300 if ((ret
= snd_pcm_hw_params_set_format(pcm_handle
, hwparams
, format_trans
[*f
])) >= 0)
305 for (i
= 0; try_order
[i
] != PA_SAMPLE_INVALID
; i
++) {
308 if ((ret
= snd_pcm_hw_params_set_format(pcm_handle
, hwparams
, format_trans
[*f
])) >= 0)
315 /* Set the hardware parameters of the given ALSA device. Returns the
316 * selected fragment settings in *period and *period_size */
317 int pa_alsa_set_hw_params(
318 snd_pcm_t
*pcm_handle
,
321 snd_pcm_uframes_t
*period_size
,
322 snd_pcm_uframes_t tsched_size
,
324 pa_bool_t
*use_tsched
,
325 pa_bool_t require_exact_channel_number
) {
328 snd_pcm_uframes_t _period_size
= period_size
? *period_size
: 0;
329 unsigned int _periods
= periods
? *periods
: 0;
330 snd_pcm_uframes_t buffer_size
;
331 unsigned int r
= ss
->rate
;
332 unsigned int c
= ss
->channels
;
333 pa_sample_format_t f
= ss
->format
;
334 snd_pcm_hw_params_t
*hwparams
;
335 pa_bool_t _use_mmap
= use_mmap
&& *use_mmap
;
336 pa_bool_t _use_tsched
= use_tsched
&& *use_tsched
;
339 pa_assert(pcm_handle
);
342 snd_pcm_hw_params_alloca(&hwparams
);
344 if ((ret
= snd_pcm_hw_params_any(pcm_handle
, hwparams
)) < 0)
347 if ((ret
= snd_pcm_hw_params_set_rate_resample(pcm_handle
, hwparams
, 0)) < 0)
352 if (snd_pcm_hw_params_set_access(pcm_handle
, hwparams
, SND_PCM_ACCESS_MMAP_INTERLEAVED
) < 0) {
354 /* mmap() didn't work, fall back to interleaved */
356 if ((ret
= snd_pcm_hw_params_set_access(pcm_handle
, hwparams
, SND_PCM_ACCESS_RW_INTERLEAVED
)) < 0)
362 } else if ((ret
= snd_pcm_hw_params_set_access(pcm_handle
, hwparams
, SND_PCM_ACCESS_RW_INTERLEAVED
)) < 0)
368 if ((ret
= set_format(pcm_handle
, hwparams
, &f
)) < 0)
371 if ((ret
= snd_pcm_hw_params_set_rate_near(pcm_handle
, hwparams
, &r
, NULL
)) < 0)
374 if (require_exact_channel_number
) {
375 if ((ret
= snd_pcm_hw_params_set_channels(pcm_handle
, hwparams
, c
)) < 0)
378 if ((ret
= snd_pcm_hw_params_set_channels_near(pcm_handle
, hwparams
, &c
)) < 0)
382 if ((ret
= snd_pcm_hw_params_set_periods_integer(pcm_handle
, hwparams
)) < 0)
385 if (_period_size
&& tsched_size
&& _periods
) {
386 /* Adjust the buffer sizes, if we didn't get the rate we were asking for */
387 _period_size
= (snd_pcm_uframes_t
) (((uint64_t) _period_size
* r
) / ss
->rate
);
388 tsched_size
= (snd_pcm_uframes_t
) (((uint64_t) tsched_size
* r
) / ss
->rate
);
391 _period_size
= tsched_size
;
394 pa_assert_se(snd_pcm_hw_params_get_buffer_size_max(hwparams
, &buffer_size
) == 0);
395 pa_log_debug("Maximum hw buffer size is %u ms", (unsigned) buffer_size
* 1000 / r
);
398 buffer_size
= _periods
* _period_size
;
402 /* First we pass 0 as direction to get exactly what we asked
403 * for. That this is necessary is presumably a bug in ALSA */
406 if ((ret
= snd_pcm_hw_params_set_periods_near(pcm_handle
, hwparams
, &_periods
, &dir
)) < 0) {
408 if ((ret
= snd_pcm_hw_params_set_periods_near(pcm_handle
, hwparams
, &_periods
, &dir
)) < 0) {
410 if ((ret
= snd_pcm_hw_params_set_periods_near(pcm_handle
, hwparams
, &_periods
, &dir
)) < 0)
416 if (_period_size
> 0)
417 if ((ret
= snd_pcm_hw_params_set_buffer_size_near(pcm_handle
, hwparams
, &buffer_size
)) < 0)
421 if ((ret
= snd_pcm_hw_params(pcm_handle
, hwparams
)) < 0)
425 pa_log_info("Device %s doesn't support %u Hz, changed to %u Hz.", snd_pcm_name(pcm_handle
), ss
->rate
, r
);
427 if (ss
->channels
!= c
)
428 pa_log_info("Device %s doesn't support %u channels, changed to %u.", snd_pcm_name(pcm_handle
), ss
->channels
, c
);
431 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
));
433 if ((ret
= snd_pcm_prepare(pcm_handle
)) < 0)
436 if ((ret
= snd_pcm_hw_params_get_period_size(hwparams
, &_period_size
, &dir
)) < 0 ||
437 (ret
= snd_pcm_hw_params_get_periods(hwparams
, &_periods
, &dir
)) < 0)
440 /* If the sample rate deviates too much, we need to resample */
441 if (r
< ss
->rate
*.95 || r
> ss
->rate
*1.05)
443 ss
->channels
= (uint8_t) c
;
446 pa_assert(_periods
> 0);
447 pa_assert(_period_size
> 0);
453 *period_size
= _period_size
;
456 *use_mmap
= _use_mmap
;
459 *use_tsched
= _use_tsched
;
463 snd_pcm_nonblock(pcm_handle
, 1);
470 int pa_alsa_set_sw_params(snd_pcm_t
*pcm
, snd_pcm_uframes_t avail_min
) {
471 snd_pcm_sw_params_t
*swparams
;
472 snd_pcm_uframes_t boundary
;
477 snd_pcm_sw_params_alloca(&swparams
);
479 if ((err
= snd_pcm_sw_params_current(pcm
, swparams
) < 0)) {
480 pa_log_warn("Unable to determine current swparams: %s\n", snd_strerror(err
));
484 if ((err
= snd_pcm_sw_params_set_period_event(pcm
, swparams
, 0)) < 0) {
485 pa_log_warn("Unable to disable period event: %s\n", snd_strerror(err
));
489 if ((err
= snd_pcm_sw_params_set_tstamp_mode(pcm
, swparams
, SND_PCM_TSTAMP_ENABLE
)) < 0) {
490 pa_log_warn("Unable to enable time stamping: %s\n", snd_strerror(err
));
494 if ((err
= snd_pcm_sw_params_get_boundary(swparams
, &boundary
)) < 0) {
495 pa_log_warn("Unable to get boundary: %s\n", snd_strerror(err
));
499 if ((err
= snd_pcm_sw_params_set_stop_threshold(pcm
, swparams
, boundary
)) < 0) {
500 pa_log_warn("Unable to set stop threshold: %s\n", snd_strerror(err
));
504 if ((err
= snd_pcm_sw_params_set_start_threshold(pcm
, swparams
, (snd_pcm_uframes_t
) -1)) < 0) {
505 pa_log_warn("Unable to set start threshold: %s\n", snd_strerror(err
));
509 if ((err
= snd_pcm_sw_params_set_avail_min(pcm
, swparams
, avail_min
)) < 0) {
510 pa_log_error("snd_pcm_sw_params_set_avail_min() failed: %s", snd_strerror(err
));
514 if ((err
= snd_pcm_sw_params(pcm
, swparams
)) < 0) {
515 pa_log_warn("Unable to set sw params: %s\n", snd_strerror(err
));
522 static const struct pa_alsa_profile_info device_table
[] = {
523 {{ 1, { PA_CHANNEL_POSITION_MONO
}},
529 {{ 2, { PA_CHANNEL_POSITION_LEFT
, PA_CHANNEL_POSITION_RIGHT
}},
535 {{ 2, { PA_CHANNEL_POSITION_LEFT
, PA_CHANNEL_POSITION_RIGHT
}},
537 N_("Digital Stereo (IEC958)"),
541 {{ 2, { PA_CHANNEL_POSITION_LEFT
, PA_CHANNEL_POSITION_RIGHT
}},
543 N_("Digital Stereo (HDMI)"),
547 {{ 4, { PA_CHANNEL_POSITION_FRONT_LEFT
, PA_CHANNEL_POSITION_FRONT_RIGHT
,
548 PA_CHANNEL_POSITION_REAR_LEFT
, PA_CHANNEL_POSITION_REAR_RIGHT
}},
550 N_("Analog Surround 4.0"),
551 "analog-surround-40",
554 {{ 4, { PA_CHANNEL_POSITION_FRONT_LEFT
, PA_CHANNEL_POSITION_FRONT_RIGHT
,
555 PA_CHANNEL_POSITION_REAR_LEFT
, PA_CHANNEL_POSITION_REAR_RIGHT
}},
557 N_("Digital Surround 4.0 (IEC958/AC3)"),
558 "iec958-ac3-surround-40",
561 {{ 5, { PA_CHANNEL_POSITION_FRONT_LEFT
, PA_CHANNEL_POSITION_FRONT_RIGHT
,
562 PA_CHANNEL_POSITION_REAR_LEFT
, PA_CHANNEL_POSITION_REAR_RIGHT
,
563 PA_CHANNEL_POSITION_LFE
}},
565 N_("Analog Surround 4.1"),
566 "analog-surround-41",
569 {{ 5, { PA_CHANNEL_POSITION_FRONT_LEFT
, PA_CHANNEL_POSITION_FRONT_RIGHT
,
570 PA_CHANNEL_POSITION_REAR_LEFT
, PA_CHANNEL_POSITION_REAR_RIGHT
,
571 PA_CHANNEL_POSITION_CENTER
}},
573 N_("Analog Surround 5.0"),
574 "analog-surround-50",
577 {{ 6, { PA_CHANNEL_POSITION_FRONT_LEFT
, PA_CHANNEL_POSITION_FRONT_RIGHT
,
578 PA_CHANNEL_POSITION_REAR_LEFT
, PA_CHANNEL_POSITION_REAR_RIGHT
,
579 PA_CHANNEL_POSITION_CENTER
, PA_CHANNEL_POSITION_LFE
}},
581 N_("Analog Surround 5.1"),
582 "analog-surround-51",
585 {{ 6, { PA_CHANNEL_POSITION_FRONT_LEFT
, PA_CHANNEL_POSITION_FRONT_CENTER
,
586 PA_CHANNEL_POSITION_FRONT_RIGHT
, PA_CHANNEL_POSITION_REAR_LEFT
,
587 PA_CHANNEL_POSITION_REAR_RIGHT
, PA_CHANNEL_POSITION_LFE
}},
589 N_("Digital Surround 5.1 (IEC958/AC3)"),
590 "iec958-ac3-surround-51",
593 {{ 8, { PA_CHANNEL_POSITION_FRONT_LEFT
, PA_CHANNEL_POSITION_FRONT_RIGHT
,
594 PA_CHANNEL_POSITION_REAR_LEFT
, PA_CHANNEL_POSITION_REAR_RIGHT
,
595 PA_CHANNEL_POSITION_CENTER
, PA_CHANNEL_POSITION_LFE
,
596 PA_CHANNEL_POSITION_SIDE_LEFT
, PA_CHANNEL_POSITION_SIDE_RIGHT
}},
598 N_("Analog Surround 7.1"),
599 "analog-surround-71",
602 {{ 0, { 0 }}, NULL
, NULL
, NULL
, 0 }
605 snd_pcm_t
*pa_alsa_open_by_device_id_auto(
612 snd_pcm_uframes_t
*period_size
,
613 snd_pcm_uframes_t tsched_size
,
615 pa_bool_t
*use_tsched
,
616 const pa_alsa_profile_info
**profile
) {
621 snd_pcm_t
*pcm_handle
;
628 pa_assert(period_size
);
630 /* First we try to find a device string with a superset of the
631 * requested channel map and open it without the plug: prefix. We
632 * iterate through our device table from top to bottom and take
633 * the first that matches. If we didn't find a working device that
634 * way, we iterate backwards, and check all devices that do not
635 * provide a superset of the requested channel map.*/
640 if ((direction
> 0) == pa_channel_map_superset(&device_table
[i
].map
, map
)) {
641 pa_sample_spec try_ss
;
643 pa_log_debug("Checking for %s (%s)", device_table
[i
].name
, device_table
[i
].alsa_name
);
645 d
= pa_sprintf_malloc("%s:%s", device_table
[i
].alsa_name
, dev_id
);
647 try_ss
.channels
= device_table
[i
].map
.channels
;
648 try_ss
.rate
= ss
->rate
;
649 try_ss
.format
= ss
->format
;
651 pcm_handle
= pa_alsa_open_by_device_string(
669 *map
= device_table
[i
].map
;
670 pa_assert(map
->channels
== ss
->channels
);
673 *profile
= &device_table
[i
];
680 if (!device_table
[i
+1].alsa_name
) {
681 /* OK, so we are at the end of our list. Let's turn
685 /* We are not at the end of the list, so let's simply
686 * try the next entry */
693 if (device_table
[i
+1].alsa_name
&&
694 device_table
[i
].map
.channels
== device_table
[i
+1].map
.channels
) {
696 /* OK, the next entry has the same number of channels,
701 /* Hmm, so the next entry does not have the same
702 * number of channels, so let's go backwards until we
703 * find the next entry with a differnt number of
706 for (i
--; i
>= 0; i
--)
707 if (device_table
[i
].map
.channels
!= device_table
[i
+1].map
.channels
)
710 /* Hmm, there is no entry with a different number of
711 * entries, then we're done */
715 /* OK, now lets find go back as long as we have the same number of channels */
717 if (device_table
[i
].map
.channels
!= device_table
[i
-1].map
.channels
)
723 /* OK, we didn't find any good device, so let's try the raw plughw: stuff */
725 d
= pa_sprintf_malloc("hw:%s", dev_id
);
726 pa_log_debug("Trying %s as last resort...", d
);
727 pcm_handle
= pa_alsa_open_by_device_string(d
, dev
, ss
, map
, mode
, nfrags
, period_size
, tsched_size
, use_mmap
, use_tsched
, FALSE
);
730 if (pcm_handle
&& profile
)
736 snd_pcm_t
*pa_alsa_open_by_device_id_profile(
743 snd_pcm_uframes_t
*period_size
,
744 snd_pcm_uframes_t tsched_size
,
746 pa_bool_t
*use_tsched
,
747 const pa_alsa_profile_info
*profile
) {
750 snd_pcm_t
*pcm_handle
;
751 pa_sample_spec try_ss
;
758 pa_assert(period_size
);
761 d
= pa_sprintf_malloc("%s:%s", profile
->alsa_name
, dev_id
);
763 try_ss
.channels
= profile
->map
.channels
;
764 try_ss
.rate
= ss
->rate
;
765 try_ss
.format
= ss
->format
;
767 pcm_handle
= pa_alsa_open_by_device_string(
787 pa_assert(map
->channels
== ss
->channels
);
792 snd_pcm_t
*pa_alsa_open_by_device_string(
799 snd_pcm_uframes_t
*period_size
,
800 snd_pcm_uframes_t tsched_size
,
802 pa_bool_t
*use_tsched
,
803 pa_bool_t require_exact_channel_number
) {
807 snd_pcm_t
*pcm_handle
;
808 pa_bool_t reformat
= FALSE
;
814 d
= pa_xstrdup(device
);
817 pa_log_debug("Trying %s %s SND_PCM_NO_AUTO_FORMAT ...", d
, reformat
? "without" : "with");
819 /* We don't pass SND_PCM_NONBLOCK here, since alsa-lib <=
820 * 1.0.17a would then ignore the SND_PCM_NO_xxx flags. Instead
821 * we enable nonblock mode afterwards via
822 * snd_pcm_nonblock(). Also see
823 * http://mailman.alsa-project.org/pipermail/alsa-devel/2008-August/010258.html */
825 if ((err
= snd_pcm_open(&pcm_handle
, d
, mode
,
826 /*SND_PCM_NONBLOCK|*/
827 SND_PCM_NO_AUTO_RESAMPLE
|
828 SND_PCM_NO_AUTO_CHANNELS
|
829 (reformat
? 0 : SND_PCM_NO_AUTO_FORMAT
))) < 0) {
830 pa_log_info("Error opening PCM device %s: %s", d
, snd_strerror(err
));
834 if ((err
= pa_alsa_set_hw_params(pcm_handle
, ss
, nfrags
, period_size
, tsched_size
, use_mmap
, use_tsched
, require_exact_channel_number
)) < 0) {
839 snd_pcm_close(pcm_handle
);
843 /* Hmm, some hw is very exotic, so we retry with plug, if without it didn't work */
845 if (!pa_startswith(d
, "plug:") && !pa_startswith(d
, "plughw:")) {
848 t
= pa_sprintf_malloc("plug:%s", d
);
854 snd_pcm_close(pcm_handle
);
858 pa_log_info("Failed to set hardware parameters on %s: %s", d
, snd_strerror(err
));
859 snd_pcm_close(pcm_handle
);
869 if (ss
->channels
!= map
->channels
)
870 pa_channel_map_init_extend(map
, ss
->channels
, PA_CHANNEL_MAP_ALSA
);
881 int pa_alsa_probe_profiles(
883 const pa_sample_spec
*ss
,
884 void (*cb
)(const pa_alsa_profile_info
*sink
, const pa_alsa_profile_info
*source
, void *userdata
),
887 const pa_alsa_profile_info
*i
;
893 /* We try each combination of playback/capture. We also try to
894 * open only for capture resp. only for sink. Don't get confused
895 * by the trailing entry in device_table we use for this! */
897 for (i
= device_table
; i
< device_table
+ PA_ELEMENTSOF(device_table
); i
++) {
898 const pa_alsa_profile_info
*j
;
899 snd_pcm_t
*pcm_i
= NULL
;
903 pa_sample_spec try_ss
;
904 pa_channel_map try_map
;
906 pa_log_debug("Checking for playback on %s (%s)", i
->name
, i
->alsa_name
);
907 id
= pa_sprintf_malloc("%s:%s", i
->alsa_name
, dev_id
);
910 try_ss
.channels
= i
->map
.channels
;
913 pcm_i
= pa_alsa_open_by_device_string(
916 SND_PCM_STREAM_PLAYBACK
,
917 NULL
, NULL
, 0, NULL
, NULL
,
926 for (j
= device_table
; j
< device_table
+ PA_ELEMENTSOF(device_table
); j
++) {
927 snd_pcm_t
*pcm_j
= NULL
;
931 pa_sample_spec try_ss
;
932 pa_channel_map try_map
;
934 pa_log_debug("Checking for capture on %s (%s)", j
->name
, j
->alsa_name
);
935 jd
= pa_sprintf_malloc("%s:%s", j
->alsa_name
, dev_id
);
938 try_ss
.channels
= j
->map
.channels
;
941 pcm_j
= pa_alsa_open_by_device_string(
944 SND_PCM_STREAM_CAPTURE
,
945 NULL
, NULL
, 0, NULL
, NULL
,
955 snd_pcm_close(pcm_j
);
957 if (i
->alsa_name
|| j
->alsa_name
)
958 cb(i
->alsa_name
? i
: NULL
,
959 j
->alsa_name
? j
: NULL
, userdata
);
963 snd_pcm_close(pcm_i
);
969 int pa_alsa_prepare_mixer(snd_mixer_t
*mixer
, const char *dev
) {
975 if ((err
= snd_mixer_attach(mixer
, dev
)) < 0) {
976 pa_log_info("Unable to attach to mixer %s: %s", dev
, snd_strerror(err
));
980 if ((err
= snd_mixer_selem_register(mixer
, NULL
, NULL
)) < 0) {
981 pa_log_warn("Unable to register mixer: %s", snd_strerror(err
));
985 if ((err
= snd_mixer_load(mixer
)) < 0) {
986 pa_log_warn("Unable to load mixer: %s", snd_strerror(err
));
990 pa_log_info("Successfully attached to mixer '%s'", dev
);
995 static pa_bool_t
elem_has_volume(snd_mixer_elem_t
*elem
, pa_bool_t playback
) {
998 if (playback
&& snd_mixer_selem_has_playback_volume(elem
))
1001 if (!playback
&& snd_mixer_selem_has_capture_volume(elem
))
1007 static pa_bool_t
elem_has_switch(snd_mixer_elem_t
*elem
, pa_bool_t playback
) {
1010 if (playback
&& snd_mixer_selem_has_playback_switch(elem
))
1013 if (!playback
&& snd_mixer_selem_has_capture_switch(elem
))
1019 snd_mixer_elem_t
*pa_alsa_find_elem(snd_mixer_t
*mixer
, const char *name
, const char *fallback
, pa_bool_t playback
) {
1020 snd_mixer_elem_t
*elem
= NULL
, *fallback_elem
= NULL
;
1021 snd_mixer_selem_id_t
*sid
= NULL
;
1023 snd_mixer_selem_id_alloca(&sid
);
1028 snd_mixer_selem_id_set_name(sid
, name
);
1030 if ((elem
= snd_mixer_find_selem(mixer
, sid
))) {
1032 if (elem_has_volume(elem
, playback
) &&
1033 elem_has_switch(elem
, playback
))
1036 if (!elem_has_volume(elem
, playback
) &&
1037 !elem_has_switch(elem
, playback
))
1041 pa_log_info("Cannot find mixer control \"%s\" or mixer control is no combination of switch/volume.", snd_mixer_selem_id_get_name(sid
));
1044 snd_mixer_selem_id_set_name(sid
, fallback
);
1046 if ((fallback_elem
= snd_mixer_find_selem(mixer
, sid
))) {
1048 if (elem_has_volume(fallback_elem
, playback
) &&
1049 elem_has_switch(fallback_elem
, playback
)) {
1050 elem
= fallback_elem
;
1054 if (!elem_has_volume(fallback_elem
, playback
) &&
1055 !elem_has_switch(fallback_elem
, playback
))
1056 fallback_elem
= NULL
;
1059 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
));
1062 if (elem
&& fallback_elem
) {
1064 /* Hmm, so we have both elements, but neither has both mute
1065 * and volume. Let's prefer the one with the volume */
1067 if (elem_has_volume(elem
, playback
))
1070 if (elem_has_volume(fallback_elem
, playback
)) {
1071 elem
= fallback_elem
;
1076 if (!elem
&& fallback_elem
)
1077 elem
= fallback_elem
;
1082 pa_log_info("Using mixer control \"%s\".", snd_mixer_selem_id_get_name(sid
));
1088 int pa_alsa_find_mixer_and_elem(
1091 snd_mixer_elem_t
**_e
) {
1095 snd_mixer_elem_t
*e
;
1096 pa_bool_t found
= FALSE
;
1103 if ((err
= snd_mixer_open(&m
, 0)) < 0) {
1104 pa_log("Error opening mixer: %s", snd_strerror(err
));
1108 /* First, try by name */
1109 if ((dev
= snd_pcm_name(pcm
)))
1110 if (pa_alsa_prepare_mixer(m
, dev
) >= 0)
1113 /* Then, try by card index */
1115 snd_pcm_info_t
* info
;
1116 snd_pcm_info_alloca(&info
);
1118 if (snd_pcm_info(pcm
, info
) >= 0) {
1122 if ((card_idx
= snd_pcm_info_get_card(info
)) >= 0) {
1124 md
= pa_sprintf_malloc("hw:%i", card_idx
);
1126 if (!dev
|| !pa_streq(dev
, md
))
1127 if (pa_alsa_prepare_mixer(m
, md
) >= 0)
1140 switch (snd_pcm_stream(pcm
)) {
1142 case SND_PCM_STREAM_PLAYBACK
:
1143 e
= pa_alsa_find_elem(m
, "Master", "PCM", TRUE
);
1146 case SND_PCM_STREAM_CAPTURE
:
1147 e
= pa_alsa_find_elem(m
, "Capture", "Mic", FALSE
);
1151 pa_assert_not_reached();
1167 static const snd_mixer_selem_channel_id_t alsa_channel_ids
[PA_CHANNEL_POSITION_MAX
] = {
1168 [PA_CHANNEL_POSITION_MONO
] = SND_MIXER_SCHN_MONO
, /* The ALSA name is just an alias! */
1170 [PA_CHANNEL_POSITION_FRONT_CENTER
] = SND_MIXER_SCHN_FRONT_CENTER
,
1171 [PA_CHANNEL_POSITION_FRONT_LEFT
] = SND_MIXER_SCHN_FRONT_LEFT
,
1172 [PA_CHANNEL_POSITION_FRONT_RIGHT
] = SND_MIXER_SCHN_FRONT_RIGHT
,
1174 [PA_CHANNEL_POSITION_REAR_CENTER
] = SND_MIXER_SCHN_REAR_CENTER
,
1175 [PA_CHANNEL_POSITION_REAR_LEFT
] = SND_MIXER_SCHN_REAR_LEFT
,
1176 [PA_CHANNEL_POSITION_REAR_RIGHT
] = SND_MIXER_SCHN_REAR_RIGHT
,
1178 [PA_CHANNEL_POSITION_LFE
] = SND_MIXER_SCHN_WOOFER
,
1180 [PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER
] = SND_MIXER_SCHN_UNKNOWN
,
1181 [PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER
] = SND_MIXER_SCHN_UNKNOWN
,
1183 [PA_CHANNEL_POSITION_SIDE_LEFT
] = SND_MIXER_SCHN_SIDE_LEFT
,
1184 [PA_CHANNEL_POSITION_SIDE_RIGHT
] = SND_MIXER_SCHN_SIDE_RIGHT
,
1186 [PA_CHANNEL_POSITION_AUX0
] = SND_MIXER_SCHN_UNKNOWN
,
1187 [PA_CHANNEL_POSITION_AUX1
] = SND_MIXER_SCHN_UNKNOWN
,
1188 [PA_CHANNEL_POSITION_AUX2
] = SND_MIXER_SCHN_UNKNOWN
,
1189 [PA_CHANNEL_POSITION_AUX3
] = SND_MIXER_SCHN_UNKNOWN
,
1190 [PA_CHANNEL_POSITION_AUX4
] = SND_MIXER_SCHN_UNKNOWN
,
1191 [PA_CHANNEL_POSITION_AUX5
] = SND_MIXER_SCHN_UNKNOWN
,
1192 [PA_CHANNEL_POSITION_AUX6
] = SND_MIXER_SCHN_UNKNOWN
,
1193 [PA_CHANNEL_POSITION_AUX7
] = SND_MIXER_SCHN_UNKNOWN
,
1194 [PA_CHANNEL_POSITION_AUX8
] = SND_MIXER_SCHN_UNKNOWN
,
1195 [PA_CHANNEL_POSITION_AUX9
] = SND_MIXER_SCHN_UNKNOWN
,
1196 [PA_CHANNEL_POSITION_AUX10
] = SND_MIXER_SCHN_UNKNOWN
,
1197 [PA_CHANNEL_POSITION_AUX11
] = SND_MIXER_SCHN_UNKNOWN
,
1198 [PA_CHANNEL_POSITION_AUX12
] = SND_MIXER_SCHN_UNKNOWN
,
1199 [PA_CHANNEL_POSITION_AUX13
] = SND_MIXER_SCHN_UNKNOWN
,
1200 [PA_CHANNEL_POSITION_AUX14
] = SND_MIXER_SCHN_UNKNOWN
,
1201 [PA_CHANNEL_POSITION_AUX15
] = SND_MIXER_SCHN_UNKNOWN
,
1202 [PA_CHANNEL_POSITION_AUX16
] = SND_MIXER_SCHN_UNKNOWN
,
1203 [PA_CHANNEL_POSITION_AUX17
] = SND_MIXER_SCHN_UNKNOWN
,
1204 [PA_CHANNEL_POSITION_AUX18
] = SND_MIXER_SCHN_UNKNOWN
,
1205 [PA_CHANNEL_POSITION_AUX19
] = SND_MIXER_SCHN_UNKNOWN
,
1206 [PA_CHANNEL_POSITION_AUX20
] = SND_MIXER_SCHN_UNKNOWN
,
1207 [PA_CHANNEL_POSITION_AUX21
] = SND_MIXER_SCHN_UNKNOWN
,
1208 [PA_CHANNEL_POSITION_AUX22
] = SND_MIXER_SCHN_UNKNOWN
,
1209 [PA_CHANNEL_POSITION_AUX23
] = SND_MIXER_SCHN_UNKNOWN
,
1210 [PA_CHANNEL_POSITION_AUX24
] = SND_MIXER_SCHN_UNKNOWN
,
1211 [PA_CHANNEL_POSITION_AUX25
] = SND_MIXER_SCHN_UNKNOWN
,
1212 [PA_CHANNEL_POSITION_AUX26
] = SND_MIXER_SCHN_UNKNOWN
,
1213 [PA_CHANNEL_POSITION_AUX27
] = SND_MIXER_SCHN_UNKNOWN
,
1214 [PA_CHANNEL_POSITION_AUX28
] = SND_MIXER_SCHN_UNKNOWN
,
1215 [PA_CHANNEL_POSITION_AUX29
] = SND_MIXER_SCHN_UNKNOWN
,
1216 [PA_CHANNEL_POSITION_AUX30
] = SND_MIXER_SCHN_UNKNOWN
,
1217 [PA_CHANNEL_POSITION_AUX31
] = SND_MIXER_SCHN_UNKNOWN
,
1219 [PA_CHANNEL_POSITION_TOP_CENTER
] = SND_MIXER_SCHN_UNKNOWN
,
1221 [PA_CHANNEL_POSITION_TOP_FRONT_CENTER
] = SND_MIXER_SCHN_UNKNOWN
,
1222 [PA_CHANNEL_POSITION_TOP_FRONT_LEFT
] = SND_MIXER_SCHN_UNKNOWN
,
1223 [PA_CHANNEL_POSITION_TOP_FRONT_RIGHT
] = SND_MIXER_SCHN_UNKNOWN
,
1225 [PA_CHANNEL_POSITION_TOP_REAR_CENTER
] = SND_MIXER_SCHN_UNKNOWN
,
1226 [PA_CHANNEL_POSITION_TOP_REAR_LEFT
] = SND_MIXER_SCHN_UNKNOWN
,
1227 [PA_CHANNEL_POSITION_TOP_REAR_RIGHT
] = SND_MIXER_SCHN_UNKNOWN
1231 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
) {
1233 pa_bool_t alsa_channel_used
[SND_MIXER_SCHN_LAST
];
1234 pa_bool_t mono_used
= FALSE
;
1237 pa_assert(channel_map
);
1238 pa_assert(mixer_map
);
1240 memset(&alsa_channel_used
, 0, sizeof(alsa_channel_used
));
1242 if (channel_map
->channels
> 1 &&
1243 ((playback
&& snd_mixer_selem_has_playback_volume_joined(elem
)) ||
1244 (!playback
&& snd_mixer_selem_has_capture_volume_joined(elem
)))) {
1245 pa_log_info("ALSA device lacks independant volume controls for each channel.");
1249 for (i
= 0; i
< channel_map
->channels
; i
++) {
1250 snd_mixer_selem_channel_id_t id
;
1253 is_mono
= channel_map
->map
[i
] == PA_CHANNEL_POSITION_MONO
;
1254 id
= alsa_channel_ids
[channel_map
->map
[i
]];
1256 if (!is_mono
&& id
== SND_MIXER_SCHN_UNKNOWN
) {
1257 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
]));
1261 if ((is_mono
&& mono_used
) || (!is_mono
&& alsa_channel_used
[id
])) {
1262 pa_log_info("Channel map has duplicate channel '%s', falling back to software volume control.", pa_channel_position_to_string(channel_map
->map
[i
]));
1266 if ((playback
&& (!snd_mixer_selem_has_playback_channel(elem
, id
) || (is_mono
&& !snd_mixer_selem_is_playback_mono(elem
)))) ||
1267 (!playback
&& (!snd_mixer_selem_has_capture_channel(elem
, id
) || (is_mono
&& !snd_mixer_selem_is_capture_mono(elem
))))) {
1269 pa_log_info("ALSA device lacks separate volumes control for channel '%s'", pa_channel_position_to_string(channel_map
->map
[i
]));
1274 mixer_map
[i
] = SND_MIXER_SCHN_MONO
;
1278 alsa_channel_used
[id
] = TRUE
;
1282 pa_log_info("All %u channels can be mapped to mixer channels.", channel_map
->channels
);
1287 void pa_alsa_dump(snd_pcm_t
*pcm
) {
1293 pa_assert_se(snd_output_buffer_open(&out
) == 0);
1295 if ((err
= snd_pcm_dump(pcm
, out
)) < 0)
1296 pa_log_debug("snd_pcm_dump(): %s", snd_strerror(err
));
1299 snd_output_buffer_string(out
, &s
);
1300 pa_log_debug("snd_pcm_dump():\n%s", pa_strnull(s
));
1303 pa_assert_se(snd_output_close(out
) == 0);
1306 void pa_alsa_dump_status(snd_pcm_t
*pcm
) {
1309 snd_pcm_status_t
*status
;
1313 snd_pcm_status_alloca(&status
);
1315 pa_assert_se(snd_output_buffer_open(&out
) == 0);
1317 pa_assert_se(snd_pcm_status(pcm
, status
) == 0);
1319 if ((err
= snd_pcm_status_dump(status
, out
)) < 0)
1320 pa_log_debug("snd_pcm_dump(): %s", snd_strerror(err
));
1323 snd_output_buffer_string(out
, &s
);
1324 pa_log_debug("snd_pcm_dump():\n%s", pa_strnull(s
));
1327 pa_assert_se(snd_output_close(out
) == 0);
1330 static void alsa_error_handler(const char *file
, int line
, const char *function
, int err
, const char *fmt
,...) {
1334 alsa_file
= pa_sprintf_malloc("(alsa-lib)%s", file
);
1338 pa_log_levelv_meta(PA_LOG_INFO
, alsa_file
, line
, function
, fmt
, ap
);
1342 pa_xfree(alsa_file
);
1345 static pa_atomic_t n_error_handler_installed
= PA_ATOMIC_INIT(0);
1347 void pa_alsa_redirect_errors_inc(void) {
1348 /* This is not really thread safe, but we do our best */
1350 if (pa_atomic_inc(&n_error_handler_installed
) == 0)
1351 snd_lib_error_set_handler(alsa_error_handler
);
1354 void pa_alsa_redirect_errors_dec(void) {
1357 pa_assert_se((r
= pa_atomic_dec(&n_error_handler_installed
)) >= 1);
1360 snd_lib_error_set_handler(NULL
);
1363 void pa_alsa_init_proplist_card(pa_core
*c
, pa_proplist
*p
, int card
) {
1364 char *cn
, *lcn
, *dn
;
1367 pa_assert(card
>= 0);
1369 pa_proplist_setf(p
, "alsa.card", "%i", card
);
1371 if (snd_card_get_name(card
, &cn
) >= 0) {
1372 pa_proplist_sets(p
, "alsa.card_name", cn
);
1376 if (snd_card_get_longname(card
, &lcn
) >= 0) {
1377 pa_proplist_sets(p
, "alsa.long_card_name", lcn
);
1381 if ((dn
= pa_alsa_get_driver_name(card
))) {
1382 pa_proplist_sets(p
, "alsa.driver_name", dn
);
1387 pa_hal_get_info(c
, p
, card
);
1391 void pa_alsa_init_proplist_pcm_info(pa_core
*c
, pa_proplist
*p
, snd_pcm_info_t
*pcm_info
) {
1393 static const char * const alsa_class_table
[SND_PCM_CLASS_LAST
+1] = {
1394 [SND_PCM_CLASS_GENERIC
] = "generic",
1395 [SND_PCM_CLASS_MULTI
] = "multi",
1396 [SND_PCM_CLASS_MODEM
] = "modem",
1397 [SND_PCM_CLASS_DIGITIZER
] = "digitizer"
1399 static const char * const class_table
[SND_PCM_CLASS_LAST
+1] = {
1400 [SND_PCM_CLASS_GENERIC
] = "sound",
1401 [SND_PCM_CLASS_MULTI
] = NULL
,
1402 [SND_PCM_CLASS_MODEM
] = "modem",
1403 [SND_PCM_CLASS_DIGITIZER
] = NULL
1405 static const char * const alsa_subclass_table
[SND_PCM_SUBCLASS_LAST
+1] = {
1406 [SND_PCM_SUBCLASS_GENERIC_MIX
] = "generic-mix",
1407 [SND_PCM_SUBCLASS_MULTI_MIX
] = "multi-mix"
1410 snd_pcm_class_t
class;
1411 snd_pcm_subclass_t subclass
;
1412 const char *n
, *id
, *sdn
, *cn
= NULL
;
1416 pa_assert(pcm_info
);
1418 pa_proplist_sets(p
, PA_PROP_DEVICE_API
, "alsa");
1420 class = snd_pcm_info_get_class(pcm_info
);
1421 if (class <= SND_PCM_CLASS_LAST
) {
1422 if (class_table
[class])
1423 pa_proplist_sets(p
, PA_PROP_DEVICE_CLASS
, class_table
[class]);
1424 if (alsa_class_table
[class])
1425 pa_proplist_sets(p
, "alsa.class", alsa_class_table
[class]);
1427 subclass
= snd_pcm_info_get_subclass(pcm_info
);
1428 if (subclass
<= SND_PCM_SUBCLASS_LAST
)
1429 if (alsa_subclass_table
[subclass
])
1430 pa_proplist_sets(p
, "alsa.subclass", alsa_subclass_table
[subclass
]);
1432 if ((n
= snd_pcm_info_get_name(pcm_info
)))
1433 pa_proplist_sets(p
, "alsa.name", n
);
1435 if ((id
= snd_pcm_info_get_id(pcm_info
)))
1436 pa_proplist_sets(p
, "alsa.id", id
);
1438 pa_proplist_setf(p
, "alsa.subdevice", "%u", snd_pcm_info_get_subdevice(pcm_info
));
1439 if ((sdn
= snd_pcm_info_get_subdevice_name(pcm_info
)))
1440 pa_proplist_sets(p
, "alsa.subdevice_name", sdn
);
1442 pa_proplist_setf(p
, "alsa.device", "%u", snd_pcm_info_get_device(pcm_info
));
1444 if ((card
= snd_pcm_info_get_card(pcm_info
)) >= 0) {
1445 pa_alsa_init_proplist_card(c
, p
, card
);
1446 cn
= pa_proplist_gets(p
, "alsa.card_name");
1450 pa_proplist_setf(p
, PA_PROP_DEVICE_DESCRIPTION
, "%s - %s", cn
, n
);
1452 pa_proplist_sets(p
, PA_PROP_DEVICE_DESCRIPTION
, cn
);
1454 pa_proplist_sets(p
, PA_PROP_DEVICE_DESCRIPTION
, n
);
1457 void pa_alsa_init_proplist_pcm(pa_core
*c
, pa_proplist
*p
, snd_pcm_t
*pcm
) {
1458 snd_pcm_hw_params_t
*hwparams
;
1459 snd_pcm_info_t
*info
;
1462 snd_pcm_hw_params_alloca(&hwparams
);
1463 snd_pcm_info_alloca(&info
);
1465 if ((err
= snd_pcm_hw_params_current(pcm
, hwparams
)) < 0)
1466 pa_log_warn("Error fetching hardware parameter info: %s", snd_strerror(err
));
1469 if ((bits
= snd_pcm_hw_params_get_sbits(hwparams
)) >= 0)
1470 pa_proplist_setf(p
, "alsa.resolution_bits", "%i", bits
);
1473 if ((err
= snd_pcm_info(pcm
, info
)) < 0)
1474 pa_log_warn("Error fetching PCM info: %s", snd_strerror(err
));
1476 pa_alsa_init_proplist_pcm_info(c
, p
, info
);
1479 int pa_alsa_recover_from_poll(snd_pcm_t
*pcm
, int revents
) {
1480 snd_pcm_state_t state
;
1485 if (revents
& POLLERR
)
1486 pa_log_debug("Got POLLERR from ALSA");
1487 if (revents
& POLLNVAL
)
1488 pa_log_warn("Got POLLNVAL from ALSA");
1489 if (revents
& POLLHUP
)
1490 pa_log_warn("Got POLLHUP from ALSA");
1491 if (revents
& POLLPRI
)
1492 pa_log_warn("Got POLLPRI from ALSA");
1493 if (revents
& POLLIN
)
1494 pa_log_debug("Got POLLIN from ALSA");
1495 if (revents
& POLLOUT
)
1496 pa_log_debug("Got POLLOUT from ALSA");
1498 state
= snd_pcm_state(pcm
);
1499 pa_log_debug("PCM state is %s", snd_pcm_state_name(state
));
1501 /* Try to recover from this error */
1505 case SND_PCM_STATE_XRUN
:
1506 if ((err
= snd_pcm_recover(pcm
, -EPIPE
, 1)) != 0) {
1507 pa_log_warn("Could not recover from POLLERR|POLLNVAL|POLLHUP and XRUN: %s", snd_strerror(err
));
1512 case SND_PCM_STATE_SUSPENDED
:
1513 if ((err
= snd_pcm_recover(pcm
, -ESTRPIPE
, 1)) != 0) {
1514 pa_log_warn("Could not recover from POLLERR|POLLNVAL|POLLHUP and SUSPENDED: %s", snd_strerror(err
));
1523 if ((err
= snd_pcm_prepare(pcm
)) < 0) {
1524 pa_log_warn("Could not recover from POLLERR|POLLNVAL|POLLHUP with snd_pcm_prepare(): %s", snd_strerror(err
));
1533 pa_rtpoll_item
* pa_alsa_build_pollfd(snd_pcm_t
*pcm
, pa_rtpoll
*rtpoll
) {
1535 struct pollfd
*pollfd
;
1536 pa_rtpoll_item
*item
;
1540 if ((n
= snd_pcm_poll_descriptors_count(pcm
)) < 0) {
1541 pa_log("snd_pcm_poll_descriptors_count() failed: %s", snd_strerror(n
));
1545 item
= pa_rtpoll_item_new(rtpoll
, PA_RTPOLL_NEVER
, (unsigned) n
);
1546 pollfd
= pa_rtpoll_item_get_pollfd(item
, NULL
);
1548 if ((err
= snd_pcm_poll_descriptors(pcm
, pollfd
, (unsigned) n
)) < 0) {
1549 pa_log("snd_pcm_poll_descriptors() failed: %s", snd_strerror(err
));
1550 pa_rtpoll_item_free(item
);
1557 snd_pcm_sframes_t
pa_alsa_safe_avail(snd_pcm_t
*pcm
, size_t hwbuf_size
, const pa_sample_spec
*ss
) {
1558 snd_pcm_sframes_t n
;
1562 pa_assert(hwbuf_size
> 0);
1565 /* Some ALSA driver expose weird bugs, let's inform the user about
1566 * what is going on */
1568 n
= snd_pcm_avail(pcm
);
1573 k
= (size_t) n
* pa_frame_size(ss
);
1575 if (k
>= hwbuf_size
* 3 ||
1576 k
>= pa_bytes_per_second(ss
)*10)
1577 pa_log(_("snd_pcm_avail_update() returned a value that is exceptionally large: %lu bytes (%lu ms). "
1578 "Most likely this is an ALSA driver bug. Please report this issue to the ALSA developers."),
1579 (unsigned long) k
, (unsigned long) (pa_bytes_to_usec(k
, ss
) / PA_USEC_PER_MSEC
));
1584 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
) {
1586 snd_pcm_uframes_t before
;
1593 pa_assert(hwbuf_size
> 0);
1598 r
= snd_pcm_mmap_begin(pcm
, areas
, offset
, frames
);
1603 k
= (size_t) *frames
* pa_frame_size(ss
);
1605 if (*frames
> before
||
1606 k
>= hwbuf_size
* 3 ||
1607 k
>= pa_bytes_per_second(ss
)*10)
1609 pa_log(_("snd_pcm_mmap_begin() returned a value that is exceptionally large: %lu bytes (%lu ms). "
1610 "Most likely this is an ALSA driver bug. Please report this issue to the ALSA developers."),
1611 (unsigned long) k
, (unsigned long) (pa_bytes_to_usec(k
, ss
) / PA_USEC_PER_MSEC
));
1616 char *pa_alsa_get_driver_name(int card
) {
1619 pa_assert(card
>= 0);
1621 t
= pa_sprintf_malloc("/sys/class/sound/card%i/device/driver/module", card
);
1628 n
= pa_xstrdup(pa_path_get_filename(m
));