mirror of
https://gitlab.freedesktop.org/pulseaudio/pulseaudio.git
synced 2025-11-04 13:29:59 -05:00
Handle ALSA file descriptors more correctly. This means a bit more overhead,
but following their API properly should avoid problems in the future. git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@606 fefdeb5f-60dc-0310-8127-8f9354f1896f
This commit is contained in:
parent
c119996c73
commit
ae07d5abd5
4 changed files with 207 additions and 62 deletions
|
|
@ -32,6 +32,188 @@
|
||||||
|
|
||||||
#include "alsa-util.h"
|
#include "alsa-util.h"
|
||||||
|
|
||||||
|
struct pa_alsa_fdlist {
|
||||||
|
int num_fds;
|
||||||
|
struct pollfd *fds;
|
||||||
|
/* This is a temporary buffer used to avoid lots of mallocs */
|
||||||
|
struct pollfd *work_fds;
|
||||||
|
|
||||||
|
snd_pcm_t *pcm;
|
||||||
|
|
||||||
|
pa_mainloop_api *m;
|
||||||
|
pa_defer_event *defer;
|
||||||
|
pa_io_event **ios;
|
||||||
|
|
||||||
|
int polled;
|
||||||
|
|
||||||
|
void (*cb)(void *userdata);
|
||||||
|
void *userdata;
|
||||||
|
};
|
||||||
|
|
||||||
|
static void io_cb(pa_mainloop_api*a, pa_io_event* e, int fd, pa_io_event_flags_t events, void *userdata) {
|
||||||
|
struct pa_alsa_fdlist *fdl = (struct pa_alsa_fdlist*)userdata;
|
||||||
|
int err, i;
|
||||||
|
unsigned short revents;
|
||||||
|
|
||||||
|
assert(a && fdl && fdl->pcm);
|
||||||
|
|
||||||
|
if (fdl->polled)
|
||||||
|
return;
|
||||||
|
|
||||||
|
fdl->polled = 1;
|
||||||
|
|
||||||
|
memcpy(fdl->work_fds, fdl->fds, sizeof(struct pollfd) * fdl->num_fds);
|
||||||
|
|
||||||
|
for (i = 0;i < fdl->num_fds;i++) {
|
||||||
|
if (e == fdl->ios[i]) {
|
||||||
|
if (events & PA_IO_EVENT_INPUT)
|
||||||
|
fdl->work_fds[i].revents |= POLLIN;
|
||||||
|
if (events & PA_IO_EVENT_OUTPUT)
|
||||||
|
fdl->work_fds[i].revents |= POLLOUT;
|
||||||
|
if (events & PA_IO_EVENT_ERROR)
|
||||||
|
fdl->work_fds[i].revents |= POLLERR;
|
||||||
|
if (events & PA_IO_EVENT_HANGUP)
|
||||||
|
fdl->work_fds[i].revents |= POLLHUP;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(i != fdl->num_fds);
|
||||||
|
|
||||||
|
err = snd_pcm_poll_descriptors_revents(fdl->pcm, fdl->work_fds, fdl->num_fds, &revents);
|
||||||
|
if (err < 0) {
|
||||||
|
pa_log_error(__FILE__": Unable to get poll revent: %s",
|
||||||
|
snd_strerror(err));
|
||||||
|
a->defer_enable(fdl->defer, 0);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (revents)
|
||||||
|
fdl->cb(fdl->userdata);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void defer_cb(pa_mainloop_api*a, pa_defer_event* e, void *userdata) {
|
||||||
|
struct pa_alsa_fdlist *fdl = (struct pa_alsa_fdlist*)userdata;
|
||||||
|
int num_fds, i, err;
|
||||||
|
struct pollfd *temp;
|
||||||
|
|
||||||
|
assert(a && fdl && fdl->pcm);
|
||||||
|
|
||||||
|
num_fds = snd_pcm_poll_descriptors_count(fdl->pcm);
|
||||||
|
assert(num_fds > 0);
|
||||||
|
|
||||||
|
if (num_fds != fdl->num_fds) {
|
||||||
|
if (fdl->fds)
|
||||||
|
pa_xfree(fdl->fds);
|
||||||
|
if (fdl->work_fds)
|
||||||
|
pa_xfree(fdl->work_fds);
|
||||||
|
fdl->fds = pa_xmalloc(sizeof(struct pollfd) * num_fds);
|
||||||
|
fdl->work_fds = pa_xmalloc(sizeof(struct pollfd) * num_fds);
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(fdl->work_fds, 0, sizeof(struct pollfd) * num_fds);
|
||||||
|
err = snd_pcm_poll_descriptors(fdl->pcm, fdl->work_fds, num_fds);
|
||||||
|
if (err < 0) {
|
||||||
|
pa_log_error(__FILE__": Unable to get poll descriptors: %s",
|
||||||
|
snd_strerror(err));
|
||||||
|
a->defer_enable(fdl->defer, 0);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
fdl->polled = 0;
|
||||||
|
|
||||||
|
if (memcmp(fdl->fds, fdl->work_fds, sizeof(struct pollfd) * num_fds) == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (fdl->ios) {
|
||||||
|
for (i = 0;i < fdl->num_fds;i++)
|
||||||
|
a->io_free(fdl->ios[i]);
|
||||||
|
if (num_fds != fdl->num_fds) {
|
||||||
|
pa_xfree(fdl->ios);
|
||||||
|
fdl->ios = pa_xmalloc(sizeof(pa_io_event*) * num_fds);
|
||||||
|
assert(fdl->ios);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
fdl->ios = pa_xmalloc(sizeof(pa_io_event*) * num_fds);
|
||||||
|
assert(fdl->ios);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Swap pointers */
|
||||||
|
temp = fdl->work_fds;
|
||||||
|
fdl->work_fds = fdl->fds;
|
||||||
|
fdl->fds = temp;
|
||||||
|
|
||||||
|
fdl->num_fds = num_fds;
|
||||||
|
|
||||||
|
for (i = 0;i < num_fds;i++) {
|
||||||
|
fdl->ios[i] = a->io_new(a, fdl->fds[i].fd,
|
||||||
|
((fdl->fds[i].events & POLLIN) ? PA_IO_EVENT_INPUT : 0) |
|
||||||
|
((fdl->fds[i].events & POLLOUT) ? PA_IO_EVENT_OUTPUT : 0),
|
||||||
|
io_cb, fdl);
|
||||||
|
assert(fdl->ios[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct pa_alsa_fdlist *pa_alsa_fdlist_new(void) {
|
||||||
|
struct pa_alsa_fdlist *fdl;
|
||||||
|
|
||||||
|
fdl = pa_xmalloc(sizeof(struct pa_alsa_fdlist));
|
||||||
|
assert(fdl);
|
||||||
|
|
||||||
|
fdl->num_fds = 0;
|
||||||
|
fdl->fds = NULL;
|
||||||
|
fdl->work_fds = NULL;
|
||||||
|
|
||||||
|
fdl->pcm = NULL;
|
||||||
|
|
||||||
|
fdl->m = NULL;
|
||||||
|
fdl->defer = NULL;
|
||||||
|
fdl->ios = NULL;
|
||||||
|
|
||||||
|
fdl->polled = 0;
|
||||||
|
|
||||||
|
return fdl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void pa_alsa_fdlist_free(struct pa_alsa_fdlist *fdl) {
|
||||||
|
assert(fdl);
|
||||||
|
|
||||||
|
if (fdl->defer) {
|
||||||
|
assert(fdl->m);
|
||||||
|
fdl->m->defer_free(fdl->defer);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fdl->ios) {
|
||||||
|
int i;
|
||||||
|
assert(fdl->m);
|
||||||
|
for (i = 0;i < fdl->num_fds;i++)
|
||||||
|
fdl->m->io_free(fdl->ios[0]);
|
||||||
|
pa_xfree(fdl->ios);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fdl->fds)
|
||||||
|
pa_xfree(fdl->fds);
|
||||||
|
if (fdl->work_fds)
|
||||||
|
pa_xfree(fdl->work_fds);
|
||||||
|
|
||||||
|
pa_xfree(fdl);
|
||||||
|
}
|
||||||
|
|
||||||
|
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) {
|
||||||
|
assert(fdl && pcm_handle && m && !fdl->m && cb);
|
||||||
|
|
||||||
|
fdl->pcm = pcm_handle;
|
||||||
|
fdl->m = m;
|
||||||
|
|
||||||
|
fdl->defer = m->defer_new(m, defer_cb, fdl);
|
||||||
|
assert(fdl->defer);
|
||||||
|
|
||||||
|
fdl->cb = cb;
|
||||||
|
fdl->userdata = userdata;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
/* Set the hardware parameters of the given ALSA device. Returns the
|
/* Set the hardware parameters of the given ALSA device. Returns the
|
||||||
* selected fragment settings in *period and *period_size */
|
* selected fragment settings in *period and *period_size */
|
||||||
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) {
|
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) {
|
||||||
|
|
@ -86,49 +268,6 @@ finish:
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Allocate an IO event for every ALSA poll descriptor for the
|
|
||||||
* specified ALSA device. Return a pointer to such an array in
|
|
||||||
* *io_events. Store the length of that array in *n_io_events. Use the
|
|
||||||
* specified callback function and userdata. The array has to be freed
|
|
||||||
* with pa_free_io_events(). */
|
|
||||||
int pa_create_io_events(snd_pcm_t *pcm_handle, pa_mainloop_api* m, pa_io_event ***io_events, unsigned *n_io_events, void (*cb)(pa_mainloop_api*a, pa_io_event *e, int fd, pa_io_event_flags_t events, void *userdata), void *userdata) {
|
|
||||||
unsigned i;
|
|
||||||
struct pollfd *pfds, *ppfd;
|
|
||||||
pa_io_event **ios;
|
|
||||||
assert(pcm_handle && m && io_events && n_io_events && cb);
|
|
||||||
|
|
||||||
*n_io_events = snd_pcm_poll_descriptors_count(pcm_handle);
|
|
||||||
|
|
||||||
pfds = pa_xmalloc(sizeof(struct pollfd) * *n_io_events);
|
|
||||||
if (snd_pcm_poll_descriptors(pcm_handle, pfds, *n_io_events) < 0) {
|
|
||||||
pa_xfree(pfds);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
*io_events = pa_xmalloc(sizeof(void*) * *n_io_events);
|
|
||||||
|
|
||||||
for (i = 0, ios = *io_events, ppfd = pfds; i < *n_io_events; i++, ios++, ppfd++) {
|
|
||||||
*ios = m->io_new(m, ppfd->fd,
|
|
||||||
((ppfd->events & POLLIN) ? PA_IO_EVENT_INPUT : 0) |
|
|
||||||
((ppfd->events & POLLOUT) ? PA_IO_EVENT_OUTPUT : 0), cb, userdata);
|
|
||||||
assert(*ios);
|
|
||||||
}
|
|
||||||
|
|
||||||
pa_xfree(pfds);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Free the memory allocated by pa_create_io_events() */
|
|
||||||
void pa_free_io_events(pa_mainloop_api* m, pa_io_event **io_events, unsigned n_io_events) {
|
|
||||||
unsigned i;
|
|
||||||
pa_io_event **ios;
|
|
||||||
assert(m && io_events);
|
|
||||||
|
|
||||||
for (ios = io_events, i = 0; i < n_io_events; i++, ios++)
|
|
||||||
m->io_free(*ios);
|
|
||||||
pa_xfree(io_events);
|
|
||||||
}
|
|
||||||
|
|
||||||
int pa_alsa_prepare_mixer(snd_mixer_t *mixer, const char *dev) {
|
int pa_alsa_prepare_mixer(snd_mixer_t *mixer, const char *dev) {
|
||||||
int err;
|
int err;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -27,10 +27,14 @@
|
||||||
#include <polyp/sample.h>
|
#include <polyp/sample.h>
|
||||||
#include <polyp/mainloop-api.h>
|
#include <polyp/mainloop-api.h>
|
||||||
|
|
||||||
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);
|
struct pa_alsa_fdlist;
|
||||||
|
|
||||||
int pa_create_io_events(snd_pcm_t *pcm_handle, pa_mainloop_api *m, pa_io_event ***io_events, unsigned *n_io_events, void (*cb)(pa_mainloop_api*a, pa_io_event *e, int fd, pa_io_event_flags_t events, void *userdata), void *userdata);
|
struct pa_alsa_fdlist *pa_alsa_fdlist_new(void);
|
||||||
void pa_free_io_events(pa_mainloop_api* m, pa_io_event **io_sources, unsigned n_io_sources);
|
void pa_alsa_fdlist_free(struct pa_alsa_fdlist *fdl);
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
int pa_alsa_prepare_mixer(snd_mixer_t *mixer, const char *dev);
|
int pa_alsa_prepare_mixer(snd_mixer_t *mixer, const char *dev);
|
||||||
snd_mixer_elem_t *pa_alsa_find_elem(snd_mixer_t *mixer, const char *name);
|
snd_mixer_elem_t *pa_alsa_find_elem(snd_mixer_t *mixer, const char *name);
|
||||||
|
|
|
||||||
|
|
@ -57,8 +57,7 @@ struct userdata {
|
||||||
snd_mixer_t *mixer_handle;
|
snd_mixer_t *mixer_handle;
|
||||||
snd_mixer_elem_t *mixer_elem;
|
snd_mixer_elem_t *mixer_elem;
|
||||||
pa_sink *sink;
|
pa_sink *sink;
|
||||||
pa_io_event **io_events;
|
struct pa_alsa_fdlist *fdl;
|
||||||
unsigned n_io_events;
|
|
||||||
long hw_volume_max, hw_volume_min;
|
long hw_volume_max, hw_volume_min;
|
||||||
|
|
||||||
size_t frame_size, fragment_size;
|
size_t frame_size, fragment_size;
|
||||||
|
|
@ -144,9 +143,9 @@ static void do_write(struct userdata *u) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void io_callback(pa_mainloop_api*a, pa_io_event *e, PA_GCC_UNUSED int fd, PA_GCC_UNUSED pa_io_event_flags_t f, void *userdata) {
|
static void fdl_callback(void *userdata) {
|
||||||
struct userdata *u = userdata;
|
struct userdata *u = userdata;
|
||||||
assert(u && a && e);
|
assert(u);
|
||||||
|
|
||||||
if (snd_pcm_state(u->pcm_handle) == SND_PCM_STATE_XRUN)
|
if (snd_pcm_state(u->pcm_handle) == SND_PCM_STATE_XRUN)
|
||||||
xrun_recovery(u);
|
xrun_recovery(u);
|
||||||
|
|
@ -359,8 +358,10 @@ int pa__init(pa_core *c, pa_module*m) {
|
||||||
pa_sink_set_owner(u->sink, m);
|
pa_sink_set_owner(u->sink, m);
|
||||||
u->sink->description = pa_sprintf_malloc("Advanced Linux Sound Architecture PCM on '%s'", dev);
|
u->sink->description = pa_sprintf_malloc("Advanced Linux Sound Architecture PCM on '%s'", dev);
|
||||||
|
|
||||||
if (pa_create_io_events(u->pcm_handle, c->mainloop, &u->io_events, &u->n_io_events, io_callback, u) < 0) {
|
u->fdl = pa_alsa_fdlist_new();
|
||||||
pa_log(__FILE__": failed to obtain file descriptors");
|
assert(u->fdl);
|
||||||
|
if (pa_alsa_fdlist_init_pcm(u->fdl, u->pcm_handle, c->mainloop, fdl_callback, u) < 0) {
|
||||||
|
pa_log(__FILE__": failed to initialise file descriptor monitoring");
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -411,8 +412,8 @@ void pa__done(pa_core *c, pa_module*m) {
|
||||||
pa_sink_unref(u->sink);
|
pa_sink_unref(u->sink);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (u->io_events)
|
if (u->fdl)
|
||||||
pa_free_io_events(c->mainloop, u->io_events, u->n_io_events);
|
pa_alsa_fdlist_free(u->fdl);
|
||||||
|
|
||||||
if (u->mixer_handle)
|
if (u->mixer_handle)
|
||||||
snd_mixer_close(u->mixer_handle);
|
snd_mixer_close(u->mixer_handle);
|
||||||
|
|
|
||||||
|
|
@ -57,8 +57,7 @@ struct userdata {
|
||||||
snd_mixer_t *mixer_handle;
|
snd_mixer_t *mixer_handle;
|
||||||
snd_mixer_elem_t *mixer_elem;
|
snd_mixer_elem_t *mixer_elem;
|
||||||
pa_source *source;
|
pa_source *source;
|
||||||
pa_io_event **io_events;
|
struct pa_alsa_fdlist *fdl;
|
||||||
unsigned n_io_events;
|
|
||||||
long hw_volume_max, hw_volume_min;
|
long hw_volume_max, hw_volume_min;
|
||||||
|
|
||||||
size_t frame_size, fragment_size;
|
size_t frame_size, fragment_size;
|
||||||
|
|
@ -144,9 +143,9 @@ static void do_read(struct userdata *u) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void io_callback(pa_mainloop_api*a, pa_io_event *e, PA_GCC_UNUSED int fd, PA_GCC_UNUSED pa_io_event_flags_t f, void *userdata) {
|
static void fdl_callback(void *userdata) {
|
||||||
struct userdata *u = userdata;
|
struct userdata *u = userdata;
|
||||||
assert(u && a && e);
|
assert(u);
|
||||||
|
|
||||||
if (snd_pcm_state(u->pcm_handle) == SND_PCM_STATE_XRUN)
|
if (snd_pcm_state(u->pcm_handle) == SND_PCM_STATE_XRUN)
|
||||||
xrun_recovery(u);
|
xrun_recovery(u);
|
||||||
|
|
@ -350,8 +349,10 @@ int pa__init(pa_core *c, pa_module*m) {
|
||||||
pa_source_set_owner(u->source, m);
|
pa_source_set_owner(u->source, m);
|
||||||
u->source->description = pa_sprintf_malloc("Advanced Linux Sound Architecture PCM on '%s'", dev);
|
u->source->description = pa_sprintf_malloc("Advanced Linux Sound Architecture PCM on '%s'", dev);
|
||||||
|
|
||||||
if (pa_create_io_events(u->pcm_handle, c->mainloop, &u->io_events, &u->n_io_events, io_callback, u) < 0) {
|
u->fdl = pa_alsa_fdlist_new();
|
||||||
pa_log(__FILE__": failed to obtain file descriptors");
|
assert(u->fdl);
|
||||||
|
if (pa_alsa_fdlist_init_pcm(u->fdl, u->pcm_handle, c->mainloop, fdl_callback, u) < 0) {
|
||||||
|
pa_log(__FILE__": failed to initialise file descriptor monitoring");
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -399,8 +400,8 @@ void pa__done(pa_core *c, pa_module*m) {
|
||||||
pa_source_unref(u->source);
|
pa_source_unref(u->source);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (u->io_events)
|
if (u->fdl)
|
||||||
pa_free_io_events(c->mainloop, u->io_events, u->n_io_events);
|
pa_alsa_fdlist_free(u->fdl);
|
||||||
|
|
||||||
if (u->mixer_handle)
|
if (u->mixer_handle)
|
||||||
snd_mixer_close(u->mixer_handle);
|
snd_mixer_close(u->mixer_handle);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue