port module-alsa-sink to new lock-free core. also add mmmap'ing support while doing so.

git-svn-id: file:///home/lennart/svn/public/pulseaudio/branches/lennart@1551 fefdeb5f-60dc-0310-8127-8f9354f1896f
This commit is contained in:
Lennart Poettering 2007-07-28 17:24:28 +00:00
parent c7df4ba6c3
commit 787f93533c
5 changed files with 745 additions and 601 deletions

View file

@ -934,12 +934,12 @@ modlibexec_LTLIBRARIES += \
module-oss.la module-oss.la
endif endif
#if HAVE_ALSA if HAVE_ALSA
#modlibexec_LTLIBRARIES += \ modlibexec_LTLIBRARIES += \
# libalsa-util.la \ libalsa-util.la \
# module-alsa-sink.la \ module-alsa-sink.la
# module-alsa-source.la # module-alsa-source.la
#endif endif
if HAVE_SOLARIS if HAVE_SOLARIS
modlibexec_LTLIBRARIES += \ modlibexec_LTLIBRARIES += \
@ -1185,15 +1185,15 @@ module_oss_la_LIBADD = $(AM_LIBADD) libiochannel.la liboss-util.la
# ALSA # ALSA
#libalsa_util_la_SOURCES = modules/alsa-util.c modules/alsa-util.h libalsa_util_la_SOURCES = modules/alsa-util.c modules/alsa-util.h
#libalsa_util_la_LDFLAGS = -avoid-version libalsa_util_la_LDFLAGS = -avoid-version
#libalsa_util_la_LIBADD = $(AM_LIBADD) $(ASOUNDLIB_LIBS) libpulsecore.la libalsa_util_la_LIBADD = $(AM_LIBADD) $(ASOUNDLIB_LIBS) libpulsecore.la
#libalsa_util_la_CFLAGS = $(AM_CFLAGS) $(ASOUNDLIB_CFLAGS) libalsa_util_la_CFLAGS = $(AM_CFLAGS) $(ASOUNDLIB_CFLAGS)
#module_alsa_sink_la_SOURCES = modules/module-alsa-sink.c module_alsa_sink_la_SOURCES = modules/module-alsa-sink.c
#module_alsa_sink_la_LDFLAGS = -module -avoid-version module_alsa_sink_la_LDFLAGS = -module -avoid-version
#module_alsa_sink_la_LIBADD = $(AM_LIBADD) $(ASOUNDLIB_LIBS) libalsa-util.la libpulsecore.la module_alsa_sink_la_LIBADD = $(AM_LIBADD) $(ASOUNDLIB_LIBS) libalsa-util.la libpulsecore.la
#module_alsa_sink_la_CFLAGS = $(AM_CFLAGS) $(ASOUNDLIB_CFLAGS) module_alsa_sink_la_CFLAGS = $(AM_CFLAGS) $(ASOUNDLIB_CFLAGS)
#module_alsa_source_la_SOURCES = modules/module-alsa-source.c #module_alsa_source_la_SOURCES = modules/module-alsa-source.c
#module_alsa_source_la_LDFLAGS = -module -avoid-version #module_alsa_source_la_LDFLAGS = -module -avoid-version

View file

@ -36,219 +36,6 @@
#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;
snd_mixer_t *mixer;
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, PA_GCC_UNUSED 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 || fdl->mixer) && fdl->fds && fdl->work_fds);
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);
if (fdl->pcm)
err = snd_pcm_poll_descriptors_revents(fdl->pcm, fdl->work_fds, fdl->num_fds, &revents);
else
err = snd_mixer_poll_descriptors_revents(fdl->mixer, fdl->work_fds, fdl->num_fds, &revents);
if (err < 0) {
pa_log_error("Unable to get poll revent: %s",
snd_strerror(err));
return;
}
a->defer_enable(fdl->defer, 1);
if (revents) {
if (fdl->pcm)
fdl->cb(fdl->userdata);
else
snd_mixer_handle_events(fdl->mixer);
}
}
static void defer_cb(pa_mainloop_api*a, PA_GCC_UNUSED 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 || fdl->mixer));
a->defer_enable(fdl->defer, 0);
if (fdl->pcm)
num_fds = snd_pcm_poll_descriptors_count(fdl->pcm);
else
num_fds = snd_mixer_poll_descriptors_count(fdl->mixer);
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_xmalloc0(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);
if (fdl->pcm)
err = snd_pcm_poll_descriptors(fdl->pcm, fdl->work_fds, num_fds);
else
err = snd_mixer_poll_descriptors(fdl->mixer, fdl->work_fds, num_fds);
if (err < 0) {
pa_log_error("Unable to get poll descriptors: %s",
snd_strerror(err));
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));
fdl->num_fds = 0;
fdl->fds = NULL;
fdl->work_fds = NULL;
fdl->pcm = NULL;
fdl->mixer = 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[i]);
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;
}
int pa_alsa_fdlist_init_mixer(struct pa_alsa_fdlist *fdl, snd_mixer_t *mixer_handle, pa_mainloop_api* m) {
assert(fdl && mixer_handle && m && !fdl->m);
fdl->mixer = mixer_handle;
fdl->m = m;
fdl->defer = m->defer_new(m, defer_cb, fdl);
assert(fdl->defer);
return 0;
}
static int set_format(snd_pcm_t *pcm_handle, snd_pcm_hw_params_t *hwparams, pa_sample_format_t *f) { static int set_format(snd_pcm_t *pcm_handle, snd_pcm_hw_params_t *hwparams, pa_sample_format_t *f) {
static const snd_pcm_format_t format_trans[] = { static const snd_pcm_format_t format_trans[] = {
@ -308,7 +95,7 @@ try_auto:
/* 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, pa_sample_spec *ss, uint32_t *periods, snd_pcm_uframes_t *period_size) { int pa_alsa_set_hw_params(snd_pcm_t *pcm_handle, pa_sample_spec *ss, uint32_t *periods, snd_pcm_uframes_t *period_size, int *use_mmap) {
int ret = -1; int ret = -1;
snd_pcm_uframes_t buffer_size; snd_pcm_uframes_t buffer_size;
unsigned int r = ss->rate; unsigned int r = ss->rate;
@ -325,10 +112,19 @@ int pa_alsa_set_hw_params(snd_pcm_t *pcm_handle, pa_sample_spec *ss, uint32_t *p
if ((ret = snd_pcm_hw_params_malloc(&hwparams)) < 0 || if ((ret = snd_pcm_hw_params_malloc(&hwparams)) < 0 ||
(ret = snd_pcm_hw_params_any(pcm_handle, hwparams)) < 0 || (ret = snd_pcm_hw_params_any(pcm_handle, hwparams)) < 0 ||
(ret = snd_pcm_hw_params_set_rate_resample(pcm_handle, hwparams, 0)) < 0 || (ret = snd_pcm_hw_params_set_rate_resample(pcm_handle, hwparams, 0)) < 0)
(ret = snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
goto finish; goto finish;
if (use_mmap && *use_mmap) {
if ((ret = snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_MMAP_INTERLEAVED)) < 0)
if ((ret = snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
goto finish;
} else if ((ret = snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
goto finish;
else if (*use_mmap)
*use_mmap = 0;
if ((ret = set_format(pcm_handle, hwparams, &f)) < 0) if ((ret = set_format(pcm_handle, hwparams, &f)) < 0)
goto finish; goto finish;

View file

@ -32,15 +32,7 @@
#include <pulse/channelmap.h> #include <pulse/channelmap.h>
struct pa_alsa_fdlist; int pa_alsa_set_hw_params(snd_pcm_t *pcm_handle, pa_sample_spec *ss, uint32_t *periods, snd_pcm_uframes_t *period_size, int *use_mmap);
struct pa_alsa_fdlist *pa_alsa_fdlist_new(void);
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_fdlist_init_mixer(struct pa_alsa_fdlist *fdl, snd_mixer_t *mixer_handle, pa_mainloop_api* m);
int pa_alsa_set_hw_params(snd_pcm_t *pcm_handle, 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, const char *fallback); snd_mixer_elem_t *pa_alsa_find_elem(snd_mixer_t *mixer, const char *name, const char *fallback);

File diff suppressed because it is too large Load diff

View file

@ -48,9 +48,14 @@
#include <sys/mman.h> #include <sys/mman.h>
#endif #endif
#ifdef HAVE_SYS_POLL_H
#include <sys/poll.h>
#else
#include "poll.h"
#endif
#include <sys/soundcard.h> #include <sys/soundcard.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/poll.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <stdio.h> #include <stdio.h>
@ -73,6 +78,7 @@
#include <pulsecore/core-util.h> #include <pulsecore/core-util.h>
#include <pulsecore/modargs.h> #include <pulsecore/modargs.h>
#include <pulsecore/log.h> #include <pulsecore/log.h>
#include <pulsecore/macro.h>
#include "oss-util.h" #include "oss-util.h"
#include "module-oss-symdef.h" #include "module-oss-symdef.h"
@ -805,7 +811,6 @@ static void thread_func(void *userdata) {
} else { } else {
ssize_t l; ssize_t l;
void *p;
int loop = 0; int loop = 0;
l = u->out_fragment_size; l = u->out_fragment_size;
@ -825,6 +830,7 @@ static void thread_func(void *userdata) {
} }
do { do {
void *p;
ssize_t t; ssize_t t;
pa_assert(l > 0); pa_assert(l > 0);
@ -1019,9 +1025,10 @@ finish:
} }
int pa__init(pa_core *c, pa_module*m) { int pa__init(pa_core *c, pa_module*m) {
struct audio_buf_info info; struct audio_buf_info info;
struct userdata *u = NULL; struct userdata *u = NULL;
const char *p; const char *dev;
int fd = -1; int fd = -1;
int nfrags, frag_size; int nfrags, frag_size;
int mode, caps; int mode, caps;
@ -1074,7 +1081,7 @@ int pa__init(pa_core *c, pa_module*m) {
goto fail; goto fail;
} }
if ((fd = pa_oss_open(p = pa_modargs_get_value(ma, "device", DEFAULT_DEVICE), &mode, &caps)) < 0) if ((fd = pa_oss_open(dev = pa_modargs_get_value(ma, "device", DEFAULT_DEVICE), &mode, &caps)) < 0)
goto fail; goto fail;
if (use_mmap && (!(caps & DSP_CAP_MMAP) || !(caps & DSP_CAP_TRIGGER))) { if (use_mmap && (!(caps & DSP_CAP_MMAP) || !(caps & DSP_CAP_TRIGGER))) {
@ -1087,7 +1094,7 @@ int pa__init(pa_core *c, pa_module*m) {
use_mmap = 0; use_mmap = 0;
} }
if (pa_oss_get_hw_description(p, hwdesc, sizeof(hwdesc)) >= 0) if (pa_oss_get_hw_description(dev, hwdesc, sizeof(hwdesc)) >= 0)
pa_log_info("Hardware name is '%s'.", hwdesc); pa_log_info("Hardware name is '%s'.", hwdesc);
else else
hwdesc[0] = 0; hwdesc[0] = 0;
@ -1111,11 +1118,12 @@ int pa__init(pa_core *c, pa_module*m) {
u->core = c; u->core = c;
u->module = m; u->module = m;
m->userdata = u; m->userdata = u;
u->fd = fd;
u->use_getospace = u->use_getispace = 1; u->use_getospace = u->use_getispace = 1;
u->use_getodelay = 1; u->use_getodelay = 1;
u->use_input_volume = u->use_pcm_volume = 1; u->use_input_volume = u->use_pcm_volume = 1;
u->mode = mode; u->mode = mode;
u->device_name = pa_xstrdup(p); u->device_name = pa_xstrdup(dev);
u->in_nfrags = u->out_nfrags = u->nfrags = nfrags; u->in_nfrags = u->out_nfrags = u->nfrags = nfrags;
u->out_fragment_size = u->in_fragment_size = u->frag_size = frag_size; u->out_fragment_size = u->in_fragment_size = u->frag_size = frag_size;
u->use_mmap = use_mmap; u->use_mmap = use_mmap;
@ -1159,25 +1167,28 @@ int pa__init(pa_core *c, pa_module*m) {
if ((name = pa_modargs_get_value(ma, "source_name", NULL))) if ((name = pa_modargs_get_value(ma, "source_name", NULL)))
namereg_fail = 1; namereg_fail = 1;
else { else {
name = name_buf = pa_sprintf_malloc("oss_input.%s", pa_path_get_filename(p)); name = name_buf = pa_sprintf_malloc("oss_input.%s", pa_path_get_filename(dev));
namereg_fail = 0; namereg_fail = 0;
} }
u->source = pa_source_new(c, __FILE__, name, namereg_fail, &ss, &map); u->source = pa_source_new(c, __FILE__, name, namereg_fail, &ss, &map);
pa_xfree(name_buf); pa_xfree(name_buf);
if (!u->source) if (!u->source) {
pa_log("Failed to create source object");
goto fail; goto fail;
}
u->source->parent.process_msg = source_process_msg; u->source->parent.process_msg = source_process_msg;
u->source->userdata = u; u->source->userdata = u;
pa_source_set_module(u->source, m); pa_source_set_module(u->source, m);
pa_source_set_asyncmsgq(u->source, u->asyncmsgq); pa_source_set_asyncmsgq(u->source, u->asyncmsgq);
pa_source_set_description(u->source, t = pa_sprintf_malloc("OSS PCM on %s%s%s%s", pa_source_set_description(u->source, t = pa_sprintf_malloc(
p, "OSS PCM on %s%s%s%s",
hwdesc[0] ? " (" : "", dev,
hwdesc[0] ? hwdesc : "", hwdesc[0] ? " (" : "",
hwdesc[0] ? ")" : "")); hwdesc[0] ? hwdesc : "",
hwdesc[0] ? ")" : ""));
pa_xfree(t); pa_xfree(t);
u->source->is_hardware = 1; u->source->is_hardware = 1;
u->source->refresh_volume = 1; u->source->refresh_volume = 1;
@ -1210,25 +1221,28 @@ try_write:
if ((name = pa_modargs_get_value(ma, "sink_name", NULL))) if ((name = pa_modargs_get_value(ma, "sink_name", NULL)))
namereg_fail = 1; namereg_fail = 1;
else { else {
name = name_buf = pa_sprintf_malloc("oss_output.%s", pa_path_get_filename(p)); name = name_buf = pa_sprintf_malloc("oss_output.%s", pa_path_get_filename(dev));
namereg_fail = 0; namereg_fail = 0;
} }
u->sink = pa_sink_new(c, __FILE__, name, namereg_fail, &ss, &map); u->sink = pa_sink_new(c, __FILE__, name, namereg_fail, &ss, &map);
pa_xfree(name_buf); pa_xfree(name_buf);
if (!u->sink) if (!u->sink) {
pa_log("Failed to create sink object");
goto fail; goto fail;
}
u->sink->parent.process_msg = sink_process_msg; u->sink->parent.process_msg = sink_process_msg;
u->sink->userdata = u; u->sink->userdata = u;
pa_sink_set_module(u->sink, m); pa_sink_set_module(u->sink, m);
pa_sink_set_asyncmsgq(u->sink, u->asyncmsgq); pa_sink_set_asyncmsgq(u->sink, u->asyncmsgq);
pa_sink_set_description(u->sink, t = pa_sprintf_malloc("OSS PCM on %s%s%s%s", pa_sink_set_description(u->sink, t = pa_sprintf_malloc(
p, "OSS PCM on %s%s%s%s",
hwdesc[0] ? " (" : "", dev,
hwdesc[0] ? hwdesc : "", hwdesc[0] ? " (" : "",
hwdesc[0] ? ")" : "")); hwdesc[0] ? hwdesc : "",
hwdesc[0] ? ")" : ""));
pa_xfree(t); pa_xfree(t);
u->sink->is_hardware = 1; u->sink->is_hardware = 1;
u->sink->refresh_volume = 1; u->sink->refresh_volume = 1;
@ -1241,8 +1255,6 @@ go_on:
pa_assert(u->source || u->sink); pa_assert(u->source || u->sink);
u->fd = fd;
pa_memchunk_reset(&u->memchunk); pa_memchunk_reset(&u->memchunk);
if (!(u->thread = pa_thread_new(thread_func, u))) { if (!(u->thread = pa_thread_new(thread_func, u))) {
@ -1261,7 +1273,10 @@ go_on:
return 0; return 0;
fail: fail:
if (fd >= 0)
if (u)
pa__done(c, m);
else if (fd >= 0)
close(fd); close(fd);
if (ma) if (ma)