4 This file is part of polypaudio.
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
11 polypaudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
26 #include <sys/types.h>
27 #include <asoundlib.h>
29 #include <polyp/sample.h>
30 #include <polypcore/xmalloc.h>
31 #include <polypcore/log.h>
33 #include "alsa-util.h"
35 struct pa_alsa_fdlist
{
38 /* This is a temporary buffer used to avoid lots of mallocs */
39 struct pollfd
*work_fds
;
45 pa_defer_event
*defer
;
50 void (*cb
)(void *userdata
);
54 static void io_cb(pa_mainloop_api
*a
, pa_io_event
* e
, int fd
, pa_io_event_flags_t events
, void *userdata
) {
55 struct pa_alsa_fdlist
*fdl
= (struct pa_alsa_fdlist
*)userdata
;
57 unsigned short revents
;
59 assert(a
&& fdl
&& (fdl
->pcm
|| fdl
->mixer
) && fdl
->fds
&& fdl
->work_fds
);
66 memcpy(fdl
->work_fds
, fdl
->fds
, sizeof(struct pollfd
) * fdl
->num_fds
);
68 for (i
= 0;i
< fdl
->num_fds
;i
++) {
69 if (e
== fdl
->ios
[i
]) {
70 if (events
& PA_IO_EVENT_INPUT
)
71 fdl
->work_fds
[i
].revents
|= POLLIN
;
72 if (events
& PA_IO_EVENT_OUTPUT
)
73 fdl
->work_fds
[i
].revents
|= POLLOUT
;
74 if (events
& PA_IO_EVENT_ERROR
)
75 fdl
->work_fds
[i
].revents
|= POLLERR
;
76 if (events
& PA_IO_EVENT_HANGUP
)
77 fdl
->work_fds
[i
].revents
|= POLLHUP
;
82 assert(i
!= fdl
->num_fds
);
85 err
= snd_pcm_poll_descriptors_revents(fdl
->pcm
, fdl
->work_fds
, fdl
->num_fds
, &revents
);
87 err
= snd_mixer_poll_descriptors_revents(fdl
->mixer
, fdl
->work_fds
, fdl
->num_fds
, &revents
);
90 pa_log_error(__FILE__
": Unable to get poll revent: %s",
97 fdl
->cb(fdl
->userdata
);
99 snd_mixer_handle_events(fdl
->mixer
);
102 a
->defer_enable(fdl
->defer
, 1);
105 static void defer_cb(pa_mainloop_api
*a
, pa_defer_event
* e
, void *userdata
) {
106 struct pa_alsa_fdlist
*fdl
= (struct pa_alsa_fdlist
*)userdata
;
110 assert(a
&& fdl
&& (fdl
->pcm
|| fdl
->mixer
));
112 a
->defer_enable(fdl
->defer
, 0);
115 num_fds
= snd_pcm_poll_descriptors_count(fdl
->pcm
);
117 num_fds
= snd_mixer_poll_descriptors_count(fdl
->mixer
);
120 if (num_fds
!= fdl
->num_fds
) {
124 pa_xfree(fdl
->work_fds
);
125 fdl
->fds
= pa_xmalloc(sizeof(struct pollfd
) * num_fds
);
126 fdl
->work_fds
= pa_xmalloc(sizeof(struct pollfd
) * num_fds
);
129 memset(fdl
->work_fds
, 0, sizeof(struct pollfd
) * num_fds
);
132 err
= snd_pcm_poll_descriptors(fdl
->pcm
, fdl
->work_fds
, num_fds
);
134 err
= snd_mixer_poll_descriptors(fdl
->mixer
, fdl
->work_fds
, num_fds
);
137 pa_log_error(__FILE__
": Unable to get poll descriptors: %s",
144 if (memcmp(fdl
->fds
, fdl
->work_fds
, sizeof(struct pollfd
) * num_fds
) == 0)
148 for (i
= 0;i
< fdl
->num_fds
;i
++)
149 a
->io_free(fdl
->ios
[i
]);
150 if (num_fds
!= fdl
->num_fds
) {
152 fdl
->ios
= pa_xmalloc(sizeof(pa_io_event
*) * num_fds
);
156 fdl
->ios
= pa_xmalloc(sizeof(pa_io_event
*) * num_fds
);
161 temp
= fdl
->work_fds
;
162 fdl
->work_fds
= fdl
->fds
;
165 fdl
->num_fds
= num_fds
;
167 for (i
= 0;i
< num_fds
;i
++) {
168 fdl
->ios
[i
] = a
->io_new(a
, fdl
->fds
[i
].fd
,
169 ((fdl
->fds
[i
].events
& POLLIN
) ? PA_IO_EVENT_INPUT
: 0) |
170 ((fdl
->fds
[i
].events
& POLLOUT
) ? PA_IO_EVENT_OUTPUT
: 0),
176 struct pa_alsa_fdlist
*pa_alsa_fdlist_new(void) {
177 struct pa_alsa_fdlist
*fdl
;
179 fdl
= pa_xmalloc(sizeof(struct pa_alsa_fdlist
));
184 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
[0]);
217 pa_xfree(fdl
->work_fds
);
222 int pa_alsa_fdlist_init_pcm(struct pa_alsa_fdlist
*fdl
, snd_pcm_t
*pcm_handle
, pa_mainloop_api
* m
, void (*cb
)(void *userdata
), void *userdata
) {
223 assert(fdl
&& pcm_handle
&& m
&& !fdl
->m
&& cb
);
225 fdl
->pcm
= pcm_handle
;
228 fdl
->defer
= m
->defer_new(m
, defer_cb
, fdl
);
232 fdl
->userdata
= userdata
;
237 int pa_alsa_fdlist_init_mixer(struct pa_alsa_fdlist
*fdl
, snd_mixer_t
*mixer_handle
, pa_mainloop_api
* m
) {
238 assert(fdl
&& mixer_handle
&& m
&& !fdl
->m
);
240 fdl
->mixer
= mixer_handle
;
243 fdl
->defer
= m
->defer_new(m
, defer_cb
, fdl
);
249 /* Set the hardware parameters of the given ALSA device. Returns the
250 * selected fragment settings in *period and *period_size */
251 int pa_alsa_set_hw_params(snd_pcm_t
*pcm_handle
, const pa_sample_spec
*ss
, uint32_t *periods
, snd_pcm_uframes_t
*period_size
) {
253 snd_pcm_uframes_t buffer_size
;
254 snd_pcm_hw_params_t
*hwparams
= NULL
;
255 unsigned int r
= ss
->rate
;
257 static const snd_pcm_format_t format_trans
[] = {
258 [PA_SAMPLE_U8
] = SND_PCM_FORMAT_U8
,
259 [PA_SAMPLE_ALAW
] = SND_PCM_FORMAT_A_LAW
,
260 [PA_SAMPLE_ULAW
] = SND_PCM_FORMAT_MU_LAW
,
261 [PA_SAMPLE_S16LE
] = SND_PCM_FORMAT_S16_LE
,
262 [PA_SAMPLE_S16BE
] = SND_PCM_FORMAT_S16_BE
,
263 [PA_SAMPLE_FLOAT32LE
] = SND_PCM_FORMAT_FLOAT_LE
,
264 [PA_SAMPLE_FLOAT32BE
] = SND_PCM_FORMAT_FLOAT_BE
,
266 assert(pcm_handle
&& ss
&& periods
&& period_size
);
268 buffer_size
= *periods
* *period_size
;
270 if ((ret
= snd_pcm_hw_params_malloc(&hwparams
)) < 0 ||
271 (ret
= snd_pcm_hw_params_any(pcm_handle
, hwparams
)) < 0 ||
272 (ret
= snd_pcm_hw_params_set_access(pcm_handle
, hwparams
, SND_PCM_ACCESS_RW_INTERLEAVED
)) < 0 ||
273 (ret
= snd_pcm_hw_params_set_format(pcm_handle
, hwparams
, format_trans
[ss
->format
])) < 0 ||
274 (ret
= snd_pcm_hw_params_set_rate_near(pcm_handle
, hwparams
, &r
, NULL
)) < 0 ||
275 (ret
= snd_pcm_hw_params_set_channels(pcm_handle
, hwparams
, ss
->channels
)) < 0 ||
276 (*periods
> 0 && (ret
= snd_pcm_hw_params_set_buffer_size_near(pcm_handle
, hwparams
, &buffer_size
)) < 0) ||
277 (*period_size
> 0 && (ret
= snd_pcm_hw_params_set_period_size_near(pcm_handle
, hwparams
, period_size
, NULL
)) < 0) ||
278 (ret
= snd_pcm_hw_params(pcm_handle
, hwparams
)) < 0)
282 pa_log_info(__FILE__
": device doesn't support %u Hz, changed to %u Hz.", ss
->rate
, r
);
284 if ((ret
= snd_pcm_prepare(pcm_handle
)) < 0)
287 if ((ret
= snd_pcm_hw_params_get_buffer_size(hwparams
, &buffer_size
)) < 0 ||
288 (ret
= snd_pcm_hw_params_get_period_size(hwparams
, period_size
, NULL
)) < 0)
291 assert(buffer_size
> 0);
292 assert(*period_size
> 0);
293 *periods
= buffer_size
/ *period_size
;
294 assert(*periods
> 0);
300 snd_pcm_hw_params_free(hwparams
);
305 int pa_alsa_prepare_mixer(snd_mixer_t
*mixer
, const char *dev
) {
308 assert(mixer
&& dev
);
310 if ((err
= snd_mixer_attach(mixer
, dev
)) < 0) {
311 pa_log_warn(__FILE__
": Unable to attach to mixer %s: %s", dev
, snd_strerror(err
));
315 if ((err
= snd_mixer_selem_register(mixer
, NULL
, NULL
)) < 0) {
316 pa_log_warn(__FILE__
": Unable to register mixer: %s", snd_strerror(err
));
320 if ((err
= snd_mixer_load(mixer
)) < 0) {
321 pa_log_warn(__FILE__
": Unable to load mixer: %s", snd_strerror(err
));
328 snd_mixer_elem_t
*pa_alsa_find_elem(snd_mixer_t
*mixer
, const char *name
) {
329 snd_mixer_elem_t
*elem
;
330 snd_mixer_selem_id_t
*sid
;
331 snd_mixer_selem_id_alloca(&sid
);
333 assert(mixer
&& name
);
335 snd_mixer_selem_id_set_name(sid
, name
);
337 elem
= snd_mixer_find_selem(mixer
, sid
);
339 pa_log_warn(__FILE__
": Cannot find mixer control %s", snd_mixer_selem_id_get_name(sid
));