2 This file is part of PulseAudio.
4 Copyright 2004-2006 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>
35 #include <pulsecore/log.h>
36 #include <pulsecore/macro.h>
37 #include <pulsecore/core-util.h>
38 #include <pulsecore/atomic.h>
40 #include "alsa-util.h"
42 struct pa_alsa_fdlist
{
45 /* This is a temporary buffer used to avoid lots of mallocs */
46 struct pollfd
*work_fds
;
51 pa_defer_event
*defer
;
56 void (*cb
)(void *userdata
);
60 static void io_cb(pa_mainloop_api
*a
, pa_io_event
* e
, int fd
, pa_io_event_flags_t events
, void *userdata
) {
62 struct pa_alsa_fdlist
*fdl
= userdata
;
65 unsigned short revents
;
69 pa_assert(fdl
->mixer
);
71 pa_assert(fdl
->work_fds
);
78 memcpy(fdl
->work_fds
, fdl
->fds
, sizeof(struct pollfd
) * fdl
->num_fds
);
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
;
94 pa_assert(i
!= fdl
->num_fds
);
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
));
101 a
->defer_enable(fdl
->defer
, 1);
104 snd_mixer_handle_events(fdl
->mixer
);
107 static void defer_cb(pa_mainloop_api
*a
, pa_defer_event
* e
, void *userdata
) {
108 struct pa_alsa_fdlist
*fdl
= userdata
;
115 pa_assert(fdl
->mixer
);
117 a
->defer_enable(fdl
->defer
, 0);
119 num_fds
= (unsigned) snd_mixer_poll_descriptors_count(fdl
->mixer
);
121 if (num_fds
!= fdl
->num_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
);
130 memset(fdl
->work_fds
, 0, sizeof(struct pollfd
) * num_fds
);
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
));
139 if (memcmp(fdl
->fds
, fdl
->work_fds
, sizeof(struct pollfd
) * num_fds
) == 0)
143 for (i
= 0; i
< fdl
->num_fds
; i
++)
144 a
->io_free(fdl
->ios
[i
]);
146 if (num_fds
!= fdl
->num_fds
) {
153 fdl
->ios
= pa_xnew(pa_io_event
*, num_fds
);
156 temp
= fdl
->work_fds
;
157 fdl
->work_fds
= fdl
->fds
;
160 fdl
->num_fds
= num_fds
;
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),
169 struct pa_alsa_fdlist
*pa_alsa_fdlist_new(void) {
170 struct pa_alsa_fdlist
*fdl
;
172 fdl
= pa_xnew0(struct pa_alsa_fdlist
, 1);
176 fdl
->work_fds
= NULL
;
186 void pa_alsa_fdlist_free(struct pa_alsa_fdlist
*fdl
) {
191 fdl
->m
->defer_free(fdl
->defer
);
197 for (i
= 0; i
< fdl
->num_fds
; i
++)
198 fdl
->m
->io_free(fdl
->ios
[i
]);
205 pa_xfree(fdl
->work_fds
);
210 int pa_alsa_fdlist_set_mixer(struct pa_alsa_fdlist
*fdl
, snd_mixer_t
*mixer_handle
, pa_mainloop_api
* m
) {
212 pa_assert(mixer_handle
);
216 fdl
->mixer
= mixer_handle
;
218 fdl
->defer
= m
->defer_new(m
, defer_cb
, fdl
);
223 static int set_format(snd_pcm_t
*pcm_handle
, snd_pcm_hw_params_t
*hwparams
, pa_sample_format_t
*f
) {
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
,
241 static const pa_sample_format_t try_order
[] = {
260 pa_assert(pcm_handle
);
263 if ((ret
= snd_pcm_hw_params_set_format(pcm_handle
, hwparams
, format_trans
[*f
])) >= 0)
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
;
289 if ((ret
= snd_pcm_hw_params_set_format(pcm_handle
, hwparams
, format_trans
[*f
])) >= 0)
294 for (i
= 0; try_order
[i
] != PA_SAMPLE_INVALID
; i
++) {
297 if ((ret
= snd_pcm_hw_params_set_format(pcm_handle
, hwparams
, format_trans
[*f
])) >= 0)
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
,
310 snd_pcm_uframes_t
*period_size
,
311 snd_pcm_uframes_t tsched_size
,
313 pa_bool_t
*use_tsched
,
314 pa_bool_t require_exact_channel_number
) {
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
;
328 pa_assert(pcm_handle
);
331 snd_pcm_hw_params_alloca(&hwparams
);
333 if ((ret
= snd_pcm_hw_params_any(pcm_handle
, hwparams
)) < 0)
336 if ((ret
= snd_pcm_hw_params_set_rate_resample(pcm_handle
, hwparams
, 0)) < 0)
340 if ((ret
= snd_pcm_hw_params_set_access(pcm_handle
, hwparams
, SND_PCM_ACCESS_MMAP_INTERLEAVED
)) < 0) {
342 /* mmap() didn't work, fall back to interleaved */
344 if ((ret
= snd_pcm_hw_params_set_access(pcm_handle
, hwparams
, SND_PCM_ACCESS_RW_INTERLEAVED
)) < 0)
350 } else if ((ret
= snd_pcm_hw_params_set_access(pcm_handle
, hwparams
, SND_PCM_ACCESS_RW_INTERLEAVED
)) < 0)
356 if ((ret
= set_format(pcm_handle
, hwparams
, &f
)) < 0)
359 if ((ret
= snd_pcm_hw_params_set_rate_near(pcm_handle
, hwparams
, &r
, NULL
)) < 0)
362 if (require_exact_channel_number
) {
363 if ((ret
= snd_pcm_hw_params_set_channels(pcm_handle
, hwparams
, c
)) < 0)
366 if ((ret
= snd_pcm_hw_params_set_channels_near(pcm_handle
, hwparams
, &c
)) < 0)
370 if ((ret
= snd_pcm_hw_params_set_periods_integer(pcm_handle
, hwparams
)) < 0)
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
);
379 _period_size
= tsched_size
;
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
);
386 buffer_size
= _periods
* _period_size
;
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 */
394 if ((ret
= snd_pcm_hw_params_set_periods_near(pcm_handle
, hwparams
, &_periods
, &dir
)) < 0) {
396 if ((ret
= snd_pcm_hw_params_set_periods_near(pcm_handle
, hwparams
, &_periods
, &dir
)) < 0) {
398 if ((ret
= snd_pcm_hw_params_set_periods_near(pcm_handle
, hwparams
, &_periods
, &dir
)) < 0)
404 if (_period_size
> 0)
405 if ((ret
= snd_pcm_hw_params_set_buffer_size_near(pcm_handle
, hwparams
, &buffer_size
)) < 0)
409 if ((ret
= snd_pcm_hw_params(pcm_handle
, hwparams
)) < 0)
413 pa_log_info("Device %s doesn't support %u Hz, changed to %u Hz.", snd_pcm_name(pcm_handle
), ss
->rate
, r
);
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
);
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
));
421 if ((ret
= snd_pcm_prepare(pcm_handle
)) < 0)
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)
428 /* If the sample rate deviates too much, we need to resample */
429 if (r
< ss
->rate
*.95 || r
> ss
->rate
*1.05)
431 ss
->channels
= (uint8_t) c
;
434 pa_assert(_periods
> 0);
435 pa_assert(_period_size
> 0);
441 *period_size
= _period_size
;
444 *use_mmap
= _use_mmap
;
447 *use_tsched
= _use_tsched
;
451 snd_pcm_nonblock(pcm_handle
, 1);
458 int pa_alsa_set_sw_params(snd_pcm_t
*pcm
, snd_pcm_uframes_t avail_min
) {
459 snd_pcm_sw_params_t
*swparams
;
464 snd_pcm_sw_params_alloca(&swparams
);
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
));
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
));
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
));
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
));
486 if ((err
= snd_pcm_sw_params(pcm
, swparams
)) < 0) {
487 pa_log_warn("Unable to set sw params: %s\n", snd_strerror(err
));
494 static const struct pa_alsa_profile_info device_table
[] = {
495 {{ 1, { PA_CHANNEL_POSITION_MONO
}},
500 {{ 2, { PA_CHANNEL_POSITION_LEFT
, PA_CHANNEL_POSITION_RIGHT
}},
505 {{ 2, { PA_CHANNEL_POSITION_LEFT
, PA_CHANNEL_POSITION_RIGHT
}},
507 "IEC958 Digital Stereo",
510 {{ 2, { PA_CHANNEL_POSITION_LEFT
, PA_CHANNEL_POSITION_RIGHT
}},
512 "HDMI Digital Stereo",
515 {{ 4, { PA_CHANNEL_POSITION_FRONT_LEFT
, PA_CHANNEL_POSITION_FRONT_RIGHT
,
516 PA_CHANNEL_POSITION_REAR_LEFT
, PA_CHANNEL_POSITION_REAR_RIGHT
}},
518 "Analog Surround 4.0",
519 "analog-surround-40" },
521 {{ 4, { PA_CHANNEL_POSITION_FRONT_LEFT
, PA_CHANNEL_POSITION_FRONT_RIGHT
,
522 PA_CHANNEL_POSITION_REAR_LEFT
, PA_CHANNEL_POSITION_REAR_RIGHT
}},
524 "IEC958/AC3 Digital Surround 4.0",
525 "iec958-ac3-surround-40" },
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
}},
531 "Analog Surround 4.1",
532 "analog-surround-41"},
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
}},
538 "Analog Surround 5.0",
539 "analog-surround-50" },
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
}},
545 "Analog Surround 5.1",
546 "analog-surround-51" },
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
}},
552 "IEC958/AC3 Digital Surround 5.1",
553 "iec958-ac3-surround-51" },
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
}},
560 "Analog Surround 7.1",
561 "analog-surround-71" },
563 {{ 0, { 0 }}, NULL
, NULL
, NULL
}
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
];
573 memset(in_a
, 0, sizeof(in_a
));
575 for (i
= 0; i
< a
->channels
; i
++)
576 in_a
[a
->map
[i
]] = TRUE
;
578 for (i
= 0; i
< b
->channels
; i
++)
579 if (!in_a
[b
->map
[i
]])
585 snd_pcm_t
*pa_alsa_open_by_device_id(
592 snd_pcm_uframes_t
*period_size
,
593 snd_pcm_uframes_t tsched_size
,
595 pa_bool_t
*use_tsched
,
596 const char**config_description
,
597 const char **config_name
) {
602 snd_pcm_t
*pcm_handle
;
609 pa_assert(period_size
);
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.*/
621 if ((direction
> 0) == channel_map_superset(&device_table
[i
].map
, map
)) {
622 pa_sample_spec try_ss
;
624 pa_log_debug("Checking for %s (%s)", device_table
[i
].name
, device_table
[i
].alsa_name
);
626 d
= pa_sprintf_malloc("%s:%s", device_table
[i
].alsa_name
, dev_id
);
628 try_ss
.channels
= device_table
[i
].map
.channels
;
629 try_ss
.rate
= ss
->rate
;
630 try_ss
.format
= ss
->format
;
632 pcm_handle
= pa_alsa_open_by_device_string(
650 *map
= device_table
[i
].map
;
651 pa_assert(map
->channels
== ss
->channels
);
653 if (config_description
)
654 *config_description
= device_table
[i
].description
;
656 *config_name
= device_table
[i
].name
;
664 if (!device_table
[i
+1].alsa_name
) {
665 /* OK, so we are at the end of our list. Let's turn
669 /* We are not at the end of the list, so let's simply
670 * try the next entry */
677 if (device_table
[i
+1].alsa_name
&&
678 device_table
[i
].map
.channels
== device_table
[i
+1].map
.channels
) {
680 /* OK, the next entry has the same number of channels,
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
690 for (i
--; i
>= 0; i
--)
691 if (device_table
[i
].map
.channels
!= device_table
[i
+1].map
.channels
)
694 /* Hmm, there is no entry with a different number of
695 * entries, then we're done */
699 /* OK, now lets find go back as long as we have the same number of channels */
701 if (device_table
[i
].map
.channels
!= device_table
[i
-1].map
.channels
)
707 /* OK, we didn't find any good device, so let's try the raw plughw: stuff */
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
);
715 *config_description
= NULL
;
722 snd_pcm_t
*pa_alsa_open_by_device_string(
729 snd_pcm_uframes_t
*period_size
,
730 snd_pcm_uframes_t tsched_size
,
732 pa_bool_t
*use_tsched
,
733 pa_bool_t require_exact_channel_number
) {
737 snd_pcm_t
*pcm_handle
;
738 pa_bool_t reformat
= FALSE
;
744 d
= pa_xstrdup(device
);
747 pa_log_debug("Trying %s %s SND_PCM_NO_AUTO_FORMAT ...", d
, reformat
? "without" : "with");
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 */
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
));
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) {
770 snd_pcm_close(pcm_handle
);
774 /* Hmm, some hw is very exotic, so we retry with plug, if without it didn't work */
776 if (!pa_startswith(d
, "plug:") && !pa_startswith(d
, "plughw:")) {
779 t
= pa_sprintf_malloc("plug:%s", d
);
785 snd_pcm_close(pcm_handle
);
789 pa_log_info("Failed to set hardware parameters on %s: %s", d
, snd_strerror(err
));
791 snd_pcm_close(pcm_handle
);
798 if (ss
->channels
!= map
->channels
)
799 pa_channel_map_init_extend(map
, ss
->channels
, PA_CHANNEL_MAP_ALSA
);
805 int pa_alsa_probe_profiles(
807 const pa_sample_spec
*ss
,
808 void (*cb
)(const pa_alsa_profile_info
*sink
, const pa_alsa_profile_info
*source
, void *userdata
),
811 const pa_alsa_profile_info
*i
;
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! */
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
;
827 pa_sample_spec try_ss
;
828 pa_channel_map try_map
;
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
);
834 try_ss
.channels
= i
->map
.channels
;
837 pcm_i
= pa_alsa_open_by_device_string(
840 SND_PCM_STREAM_PLAYBACK
,
841 NULL
, NULL
, 0, NULL
, NULL
,
850 for (j
= device_table
; j
< device_table
+ PA_ELEMENTSOF(device_table
); j
++) {
851 snd_pcm_t
*pcm_j
= NULL
;
855 pa_sample_spec try_ss
;
856 pa_channel_map try_map
;
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
);
862 try_ss
.channels
= j
->map
.channels
;
865 pcm_j
= pa_alsa_open_by_device_string(
868 SND_PCM_STREAM_CAPTURE
,
869 NULL
, NULL
, 0, NULL
, NULL
,
879 snd_pcm_close(pcm_j
);
881 if (i
->alsa_name
|| j
->alsa_name
)
882 cb(i
->alsa_name
? i
: NULL
,
883 j
->alsa_name
? j
: NULL
, userdata
);
887 snd_pcm_close(pcm_i
);
893 int pa_alsa_prepare_mixer(snd_mixer_t
*mixer
, const char *dev
) {
899 if ((err
= snd_mixer_attach(mixer
, dev
)) < 0) {
900 pa_log_info("Unable to attach to mixer %s: %s", dev
, snd_strerror(err
));
904 if ((err
= snd_mixer_selem_register(mixer
, NULL
, NULL
)) < 0) {
905 pa_log_warn("Unable to register mixer: %s", snd_strerror(err
));
909 if ((err
= snd_mixer_load(mixer
)) < 0) {
910 pa_log_warn("Unable to load mixer: %s", snd_strerror(err
));
914 pa_log_info("Successfully attached to mixer '%s'", dev
);
919 static pa_bool_t
elem_has_volume(snd_mixer_elem_t
*elem
, pa_bool_t playback
) {
922 if (playback
&& snd_mixer_selem_has_playback_volume(elem
))
925 if (!playback
&& snd_mixer_selem_has_capture_volume(elem
))
931 static pa_bool_t
elem_has_switch(snd_mixer_elem_t
*elem
, pa_bool_t playback
) {
934 if (playback
&& snd_mixer_selem_has_playback_switch(elem
))
937 if (!playback
&& snd_mixer_selem_has_capture_switch(elem
))
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
;
947 snd_mixer_selem_id_alloca(&sid
);
952 snd_mixer_selem_id_set_name(sid
, name
);
954 if ((elem
= snd_mixer_find_selem(mixer
, sid
))) {
956 if (elem_has_volume(elem
, playback
) &&
957 elem_has_switch(elem
, playback
))
960 if (!elem_has_volume(elem
, playback
) &&
961 !elem_has_switch(elem
, playback
))
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
));
968 snd_mixer_selem_id_set_name(sid
, fallback
);
970 if ((fallback_elem
= snd_mixer_find_selem(mixer
, sid
))) {
972 if (elem_has_volume(fallback_elem
, playback
) &&
973 elem_has_switch(fallback_elem
, playback
)) {
974 elem
= fallback_elem
;
978 if (!elem_has_volume(fallback_elem
, playback
) &&
979 !elem_has_switch(fallback_elem
, playback
))
980 fallback_elem
= NULL
;
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
));
986 if (elem
&& fallback_elem
) {
988 /* Hmm, so we have both elements, but neither has both mute
989 * and volume. Let's prefer the one with the volume */
991 if (elem_has_volume(elem
, playback
))
994 if (elem_has_volume(fallback_elem
, playback
)) {
995 elem
= fallback_elem
;
1000 if (!elem
&& fallback_elem
)
1001 elem
= fallback_elem
;
1006 pa_log_info("Using mixer control \"%s\".", snd_mixer_selem_id_get_name(sid
));
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! */
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
,
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
,
1022 [PA_CHANNEL_POSITION_LFE
] = SND_MIXER_SCHN_WOOFER
,
1024 [PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER
] = SND_MIXER_SCHN_UNKNOWN
,
1025 [PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER
] = SND_MIXER_SCHN_UNKNOWN
,
1027 [PA_CHANNEL_POSITION_SIDE_LEFT
] = SND_MIXER_SCHN_SIDE_LEFT
,
1028 [PA_CHANNEL_POSITION_SIDE_RIGHT
] = SND_MIXER_SCHN_SIDE_RIGHT
,
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
,
1063 [PA_CHANNEL_POSITION_TOP_CENTER
] = SND_MIXER_SCHN_UNKNOWN
,
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
,
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
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
) {
1077 pa_bool_t alsa_channel_used
[SND_MIXER_SCHN_LAST
];
1078 pa_bool_t mono_used
= FALSE
;
1081 pa_assert(channel_map
);
1082 pa_assert(mixer_map
);
1084 memset(&alsa_channel_used
, 0, sizeof(alsa_channel_used
));
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.");
1093 for (i
= 0; i
< channel_map
->channels
; i
++) {
1094 snd_mixer_selem_channel_id_t id
;
1097 is_mono
= channel_map
->map
[i
] == PA_CHANNEL_POSITION_MONO
;
1098 id
= alsa_channel_ids
[channel_map
->map
[i
]];
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
]));
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
]));
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
))))) {
1113 pa_log_info("ALSA device lacks separate volumes control for channel '%s'", pa_channel_position_to_string(channel_map
->map
[i
]));
1118 mixer_map
[i
] = SND_MIXER_SCHN_MONO
;
1122 alsa_channel_used
[id
] = TRUE
;
1126 pa_log_info("All %u channels can be mapped to mixer channels.", channel_map
->channels
);
1131 void pa_alsa_dump(snd_pcm_t
*pcm
) {
1137 pa_assert_se(snd_output_buffer_open(&out
) == 0);
1139 if ((err
= snd_pcm_dump(pcm
, out
)) < 0)
1140 pa_log_debug("snd_pcm_dump(): %s", snd_strerror(err
));
1143 snd_output_buffer_string(out
, &s
);
1144 pa_log_debug("snd_pcm_dump():\n%s", pa_strnull(s
));
1147 pa_assert_se(snd_output_close(out
) == 0);
1150 void pa_alsa_dump_status(snd_pcm_t
*pcm
) {
1153 snd_pcm_status_t
*status
;
1157 snd_pcm_status_alloca(&status
);
1159 pa_assert_se(snd_output_buffer_open(&out
) == 0);
1161 pa_assert_se(snd_pcm_status(pcm
, status
) == 0);
1163 if ((err
= snd_pcm_status_dump(status
, out
)) < 0)
1164 pa_log_debug("snd_pcm_dump(): %s", snd_strerror(err
));
1167 snd_output_buffer_string(out
, &s
);
1168 pa_log_debug("snd_pcm_dump():\n%s", pa_strnull(s
));
1171 pa_assert_se(snd_output_close(out
) == 0);
1174 static void alsa_error_handler(const char *file
, int line
, const char *function
, int err
, const char *fmt
,...) {
1178 alsa_file
= pa_sprintf_malloc("(alsa-lib)%s", file
);
1182 pa_log_levelv_meta(PA_LOG_INFO
, alsa_file
, line
, function
, fmt
, ap
);
1186 pa_xfree(alsa_file
);
1189 static pa_atomic_t n_error_handler_installed
= PA_ATOMIC_INIT(0);
1191 void pa_alsa_redirect_errors_inc(void) {
1192 /* This is not really thread safe, but we do our best */
1194 if (pa_atomic_inc(&n_error_handler_installed
) == 0)
1195 snd_lib_error_set_handler(alsa_error_handler
);
1198 void pa_alsa_redirect_errors_dec(void) {
1201 pa_assert_se((r
= pa_atomic_dec(&n_error_handler_installed
)) >= 1);
1204 snd_lib_error_set_handler(NULL
);
1207 void pa_alsa_init_proplist_card(pa_proplist
*p
, int card
) {
1211 pa_assert(card
>= 0);
1213 pa_proplist_setf(p
, "alsa.card", "%i", card
);
1215 if (snd_card_get_name(card
, &cn
) >= 0) {
1216 pa_proplist_sets(p
, "alsa.card_name", cn
);
1220 if (snd_card_get_longname(card
, &lcn
) >= 0) {
1221 pa_proplist_sets(p
, "alsa.long_card_name", lcn
);
1226 void pa_alsa_init_proplist_pcm(pa_proplist
*p
, snd_pcm_info_t
*pcm_info
) {
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"
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
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"
1245 snd_pcm_class_t
class;
1246 snd_pcm_subclass_t subclass
;
1247 const char *n
, *id
, *sdn
, *cn
;
1251 pa_assert(pcm_info
);
1253 pa_proplist_sets(p
, PA_PROP_DEVICE_API
, "alsa");
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]);
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
]);
1267 if ((n
= snd_pcm_info_get_name(pcm_info
)))
1268 pa_proplist_sets(p
, "alsa.name", n
);
1270 if ((id
= snd_pcm_info_get_id(pcm_info
)))
1271 pa_proplist_sets(p
, "alsa.id", id
);
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
);
1277 pa_proplist_setf(p
, "alsa.device", "%u", snd_pcm_info_get_device(pcm_info
));
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");
1285 pa_proplist_setf(p
, PA_PROP_DEVICE_DESCRIPTION
, "%s - %s", cn
, n
);
1287 pa_proplist_sets(p
, PA_PROP_DEVICE_DESCRIPTION
, cn
);
1289 pa_proplist_sets(p
, PA_PROP_DEVICE_DESCRIPTION
, n
);
1292 int pa_alsa_recover_from_poll(snd_pcm_t
*pcm
, int revents
) {
1293 snd_pcm_state_t state
;
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");
1311 state
= snd_pcm_state(pcm
);
1312 pa_log_debug("PCM state is %s", snd_pcm_state_name(state
));
1314 /* Try to recover from this error */
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
));
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
));
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
));
1346 pa_rtpoll_item
* pa_alsa_build_pollfd(snd_pcm_t
*pcm
, pa_rtpoll
*rtpoll
) {
1348 struct pollfd
*pollfd
;
1349 pa_rtpoll_item
*item
;
1353 if ((n
= snd_pcm_poll_descriptors_count(pcm
)) < 0) {
1354 pa_log("snd_pcm_poll_descriptors_count() failed: %s", snd_strerror(n
));
1358 item
= pa_rtpoll_item_new(rtpoll
, PA_RTPOLL_NEVER
, (unsigned) n
);
1359 pollfd
= pa_rtpoll_item_get_pollfd(item
, NULL
);
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
);
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
;
1375 pa_assert(hwbuf_size
> 0);
1378 /* Some ALSA driver expose weird bugs, let's inform the user about
1379 * what is going on */
1381 n
= snd_pcm_avail_update(pcm
);
1386 k
= (size_t) n
* pa_frame_size(ss
);
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
));
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
) {
1399 snd_pcm_uframes_t before
;
1406 pa_assert(hwbuf_size
> 0);
1411 r
= snd_pcm_mmap_begin(pcm
, areas
, offset
, frames
);
1416 k
= (size_t) *frames
* pa_frame_size(ss
);
1418 if (*frames
> before
||
1419 k
>= hwbuf_size
* 3 ||
1420 k
>= pa_bytes_per_second(ss
)*10)
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
));