summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2007-07-28 17:24:28 +0000
committerLennart Poettering <lennart@poettering.net>2007-07-28 17:24:28 +0000
commit787f93533c6f0974d83185e2df1f8071dcce48ee (patch)
tree21f4dfc3c28aa1871d00440a1fdc052aa6811fa2
parentc7df4ba6c3f9b0f63e45505c53879cab11c0d696 (diff)
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
-rw-r--r--src/Makefile.am26
-rw-r--r--src/modules/alsa-util.c228
-rw-r--r--src/modules/alsa-util.h10
-rw-r--r--src/modules/module-alsa-sink.c967
-rw-r--r--src/modules/module-oss.c61
5 files changed, 718 insertions, 574 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
index be0e584e..ec1bbd02 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -934,12 +934,12 @@ modlibexec_LTLIBRARIES += \
module-oss.la
endif
-#if HAVE_ALSA
-#modlibexec_LTLIBRARIES += \
-# libalsa-util.la \
-# module-alsa-sink.la \
+if HAVE_ALSA
+modlibexec_LTLIBRARIES += \
+ libalsa-util.la \
+ module-alsa-sink.la
# module-alsa-source.la
-#endif
+endif
if HAVE_SOLARIS
modlibexec_LTLIBRARIES += \
@@ -1185,15 +1185,15 @@ module_oss_la_LIBADD = $(AM_LIBADD) libiochannel.la liboss-util.la
# ALSA
-#libalsa_util_la_SOURCES = modules/alsa-util.c modules/alsa-util.h
-#libalsa_util_la_LDFLAGS = -avoid-version
-#libalsa_util_la_LIBADD = $(AM_LIBADD) $(ASOUNDLIB_LIBS) libpulsecore.la
-#libalsa_util_la_CFLAGS = $(AM_CFLAGS) $(ASOUNDLIB_CFLAGS)
+libalsa_util_la_SOURCES = modules/alsa-util.c modules/alsa-util.h
+libalsa_util_la_LDFLAGS = -avoid-version
+libalsa_util_la_LIBADD = $(AM_LIBADD) $(ASOUNDLIB_LIBS) libpulsecore.la
+libalsa_util_la_CFLAGS = $(AM_CFLAGS) $(ASOUNDLIB_CFLAGS)
-#module_alsa_sink_la_SOURCES = modules/module-alsa-sink.c
-#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_CFLAGS = $(AM_CFLAGS) $(ASOUNDLIB_CFLAGS)
+module_alsa_sink_la_SOURCES = modules/module-alsa-sink.c
+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_CFLAGS = $(AM_CFLAGS) $(ASOUNDLIB_CFLAGS)
#module_alsa_source_la_SOURCES = modules/module-alsa-source.c
#module_alsa_source_la_LDFLAGS = -module -avoid-version
diff --git a/src/modules/alsa-util.c b/src/modules/alsa-util.c
index 0a518025..520215fa 100644
--- a/src/modules/alsa-util.c
+++ b/src/modules/alsa-util.c
@@ -36,219 +36,6 @@
#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 const snd_pcm_format_t format_trans[] = {
@@ -308,7 +95,7 @@ try_auto:
/* Set the hardware parameters of the given ALSA device. Returns the
* 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;
snd_pcm_uframes_t buffer_size;
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 ||
(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_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
+ (ret = snd_pcm_hw_params_set_rate_resample(pcm_handle, hwparams, 0)) < 0)
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)
goto finish;
diff --git a/src/modules/alsa-util.h b/src/modules/alsa-util.h
index ea6c7e1d..0cde087f 100644
--- a/src/modules/alsa-util.h
+++ b/src/modules/alsa-util.h
@@ -32,15 +32,7 @@
#include <pulse/channelmap.h>
-struct pa_alsa_fdlist;
-
-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_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 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);
diff --git a/src/modules/module-alsa-sink.c b/src/modules/module-alsa-sink.c
index f9c4efd4..4fc2f3a2 100644
--- a/src/modules/module-alsa-sink.c
+++ b/src/modules/module-alsa-sink.c
@@ -26,7 +26,6 @@
#include <config.h>
#endif
-#include <assert.h>
#include <stdio.h>
#ifdef HAVE_SYS_POLL_H
@@ -38,6 +37,7 @@
#include <asoundlib.h>
#include <pulse/xmalloc.h>
+#include <pulse/util.h>
#include <pulsecore/core.h>
#include <pulsecore/module.h>
@@ -47,6 +47,9 @@
#include <pulsecore/core-util.h>
#include <pulsecore/sample-util.h>
#include <pulsecore/log.h>
+#include <pulsecore/macro.h>
+#include <pulsecore/thread.h>
+#include <pulsecore/core-error.h>
#include "alsa-util.h"
#include "module-alsa-sink-symdef.h"
@@ -62,20 +65,36 @@ PA_MODULE_USAGE(
"rate=<sample rate> "
"fragments=<number of fragments> "
"fragment_size=<fragment size> "
- "channel_map=<channel map>")
+ "channel_map=<channel map> "
+ "mmap=<enable memory mapping?>")
+
+#define DEFAULT_DEVICE "default"
+
+#define DEFAULT_NFRAGS 4
+#define DEFAULT_FRAGSIZE_MSEC 25
struct userdata {
- snd_pcm_t *pcm_handle;
- snd_mixer_t *mixer_handle;
- snd_mixer_elem_t *mixer_elem;
+ pa_core *core;
+ pa_module *module;
pa_sink *sink;
- struct pa_alsa_fdlist *pcm_fdl;
- struct pa_alsa_fdlist *mixer_fdl;
- long hw_volume_max, hw_volume_min;
+ pa_thread *thread;
+ pa_asyncmsgq *asyncmsgq;
+
+ snd_pcm_t *pcm_handle;
- size_t frame_size, fragment_size;
- pa_memchunk memchunk, silence;
- pa_module *module;
+/* snd_mixer_t *mixer_handle; */
+/* snd_mixer_elem_t *mixer_elem; */
+/* long hw_volume_max, hw_volume_min; */
+
+ size_t frame_size, fragment_size, hwbuf_size;
+ unsigned nfragments;
+ pa_memchunk memchunk;
+
+ char *device_name;
+
+ int use_mmap;
+
+ int first;
};
static const char* const valid_modargs[] = {
@@ -87,327 +106,601 @@ static const char* const valid_modargs[] = {
"fragments",
"fragment_size",
"channel_map",
+ "mmap",
NULL
};
-#define DEFAULT_DEVICE "default"
+static int mmap_write(struct userdata *u) {
+ snd_pcm_sframes_t n;
+ int err;
+ const snd_pcm_channel_area_t *areas;
+ snd_pcm_uframes_t offset, frames;
+
+ pa_assert(u);
+ pa_assert(u->sink);
-static void update_usage(struct userdata *u) {
- pa_module_set_used(u->module, u->sink ? pa_sink_used_by(u->sink) : 0);
-}
+ for (;;) {
+ pa_memchunk chunk;
+ void *p;
+
+ if ((n = snd_pcm_avail_update(u->pcm_handle)) < 0) {
-static void clear_up(struct userdata *u) {
- assert(u);
+ if (n == -EPIPE) {
+ pa_log_debug("snd_pcm_avail_update: Buffer underrun!");
+ u->first = 1;
+ }
+
+ if ((err = snd_pcm_recover(u->pcm_handle, n, 1)) == 0)
+ continue;
- if (u->sink) {
- pa_sink_disconnect(u->sink);
- pa_sink_unref(u->sink);
- u->sink = NULL;
- }
+ if (err == EAGAIN)
+ return 0;
+
+ pa_log("snd_pcm_avail_update: %s", snd_strerror(n));
+ return -1;
+ }
- if (u->pcm_fdl)
- pa_alsa_fdlist_free(u->pcm_fdl);
- if (u->mixer_fdl)
- pa_alsa_fdlist_free(u->mixer_fdl);
+/* pa_log("Got request for %i samples", (int) n); */
+
+ if (n <= 0)
+ return 0;
+
+ frames = n;
+
+ if ((err = snd_pcm_mmap_begin(u->pcm_handle, &areas, &offset, &frames)) < 0) {
+
+ if (err == -EPIPE) {
+ pa_log_debug("snd_pcm_mmap_begin: Buffer underrun!");
+ u->first = 1;
+ }
- u->pcm_fdl = u->mixer_fdl = NULL;
+ if ((err = snd_pcm_recover(u->pcm_handle, err, 1)) == 0)
+ continue;
- if (u->mixer_handle) {
- snd_mixer_close(u->mixer_handle);
- u->mixer_handle = NULL;
- }
+ if (err == EAGAIN)
+ return 0;
- if (u->pcm_handle) {
- snd_pcm_drop(u->pcm_handle);
- snd_pcm_close(u->pcm_handle);
- u->pcm_handle = NULL;
+ pa_log("Failed to write data to DSP: %s", snd_strerror(err));
+ return -1;
+ }
+
+ /* Check these are multiples of 8 bit */
+ pa_assert((areas[0].first & 7) == 0);
+ pa_assert((areas[0].step & 7)== 0);
+
+ /* We assume a single interleaved memory buffer */
+ pa_assert((areas[0].first >> 3) == 0);
+ pa_assert((areas[0].step >> 3) == u->frame_size);
+
+ p = (uint8_t*) areas[0].addr + (offset * u->frame_size);
+
+ chunk.memblock = pa_memblock_new_fixed(u->core->mempool, p, frames * u->frame_size, 1);
+ chunk.length = pa_memblock_get_length(chunk.memblock);
+ chunk.index = 0;
+
+ pa_sink_render_into_full(u->sink, &chunk);
+
+ /* FIXME: Maybe we can do something to keep this memory block
+ * a little bit longer around? */
+ pa_memblock_unref_fixed(chunk.memblock);
+
+ if ((err = snd_pcm_mmap_commit(u->pcm_handle, offset, frames)) < 0) {
+
+ if (err == -EPIPE) {
+ pa_log_debug("snd_pcm_mmap_commit: Buffer underrun!");
+ u->first = 1;
+ }
+
+ if ((err = snd_pcm_recover(u->pcm_handle, err, 1)) == 0)
+ continue;
+
+ if (err == EAGAIN)
+ return 0;
+
+ pa_log("Failed to write data to DSP: %s", snd_strerror(err));
+ return -1;
+ }
+
+/* pa_log("wrote %i samples", (int) frames); */
}
}
-static int xrun_recovery(struct userdata *u) {
- int ret;
- assert(u);
-
- pa_log_info("*** ALSA-XRUN (playback) ***");
+static pa_usec_t sink_get_latency(struct userdata *u) {
+ pa_usec_t r = 0;
+ snd_pcm_sframes_t frames = 0;
+ int err;
+
+ pa_assert(u);
- if ((ret = snd_pcm_prepare(u->pcm_handle)) < 0) {
- pa_log("snd_pcm_prepare() failed: %s", snd_strerror(-ret));
+ snd_pcm_avail_update(u->pcm_handle);
- clear_up(u);
- pa_module_unload_request(u->module);
- return -1;
+ if ((err = snd_pcm_delay(u->pcm_handle, &frames)) < 0) {
+ pa_log("Failed to get delay: %s", snd_strerror(err));
+ return 0;
}
- return ret;
+ if (frames > 0)
+ r = pa_bytes_to_usec(frames * u->frame_size, &u->sink->sample_spec);
+
+ if (u->memchunk.memblock)
+ r += pa_bytes_to_usec(u->memchunk.length, &u->sink->sample_spec);
+
+ return r;
}
-static int suspend_recovery(struct userdata *u) {
- int ret;
- assert(u);
+static int suspend(struct userdata *u) {
+ pa_assert(u);
+ pa_assert(u->pcm_handle);
- pa_log_info("*** ALSA-SUSPEND (playback) ***");
+ snd_pcm_drain(u->pcm_handle); /* Let's suspend */
+ snd_pcm_close(u->pcm_handle);
+ u->pcm_handle = NULL;
- if ((ret = snd_pcm_resume(u->pcm_handle)) < 0) {
- if (ret == -EAGAIN)
- return -1;
+ pa_log_debug("Device suspended...");
+
+ return 0;
+}
- if (ret != -ENOSYS)
- pa_log("snd_pcm_resume() failed: %s", snd_strerror(-ret));
- else {
- if ((ret = snd_pcm_prepare(u->pcm_handle)) < 0)
- pa_log("snd_pcm_prepare() failed: %s", snd_strerror(-ret));
- }
+static int unsuspend(struct userdata *u) {
+ pa_sample_spec ss;
+ int err, b;
+ unsigned nfrags;
+ snd_pcm_uframes_t period_size;
- if (ret < 0) {
- clear_up(u);
- pa_module_unload_request(u->module);
- return -1;
- }
+ pa_assert(u);
+ pa_assert(!u->pcm_handle);
+
+ pa_log_debug("Trying resume...");
+
+ snd_config_update_free_global();
+ if ((err = snd_pcm_open(&u->pcm_handle, u->device_name, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK)) < 0) {
+ pa_log("Error opening PCM device %s: %s", u->device_name, snd_strerror(err));
+ goto fail;
}
- return ret;
-}
+ ss = u->sink->sample_spec;
+ nfrags = u->nfragments;
+ period_size = u->fragment_size / u->frame_size;
+ b = u->use_mmap;
+
+ if ((err = pa_alsa_set_hw_params(u->pcm_handle, &ss, &nfrags, &period_size, &b)) < 0) {
+ pa_log("Failed to set hardware parameters: %s", snd_strerror(err));
+ goto fail;
+ }
-static void do_write(struct userdata *u) {
- assert(u);
+ if (b != u->use_mmap) {
+ pa_log_warn("Resume failed, couldn't get original access mode.");
+ goto fail;
+ }
- update_usage(u);
+ if (!pa_sample_spec_equal(&ss, &u->sink->sample_spec)) {
+ pa_log_warn("Resume failed, couldn't restore original sample settings.");
+ goto fail;
+ }
- for (;;) {
- void *p;
- pa_memchunk *memchunk = NULL;
- snd_pcm_sframes_t frames;
-
- if (u->memchunk.memblock)
- memchunk = &u->memchunk;
- else {
- if (pa_sink_render(u->sink, u->fragment_size, &u->memchunk) < 0)
- memchunk = &u->silence;
- else
- memchunk = &u->memchunk;
- }
- assert(memchunk->memblock);
- assert(memchunk->length);
- assert((memchunk->length % u->frame_size) == 0);
+ if (!nfrags != u->nfragments || period_size*u->frame_size != u->fragment_size) {
+ pa_log_warn("Resume failed, couldn't restore original fragment settings.");
+ goto fail;
+ }
- p = pa_memblock_acquire(memchunk->memblock);
+ u->first = 1;
+
+ pa_log_debug("Resumed successfully...");
- if ((frames = snd_pcm_writei(u->pcm_handle, (uint8_t*) p + memchunk->index, memchunk->length / u->frame_size)) < 0) {
- pa_memblock_release(memchunk->memblock);
+ return 0;
- if (frames == -EAGAIN)
- return;
+fail:
+ snd_pcm_close(u->pcm_handle);
+ u->pcm_handle = NULL;
- if (frames == -EPIPE) {
- if (xrun_recovery(u) < 0)
- return;
+ return -1;
+}
- continue;
- }
+static int sink_process_msg(pa_msgobject *o, int code, void *data, pa_memchunk *chunk) {
+ struct userdata *u = PA_SINK(o)->userdata;
- if (frames == -ESTRPIPE) {
- if (suspend_recovery(u) < 0)
- return;
+ switch (code) {
- continue;
- }
+ case PA_SINK_MESSAGE_GET_LATENCY: {
+ pa_usec_t r = 0;
+
+ if (u->pcm_handle)
+ r = sink_get_latency(u);
- pa_log("snd_pcm_writei() failed: %s", snd_strerror(-frames));
+ *((pa_usec_t*) data) = r;
- clear_up(u);
- pa_module_unload_request(u->module);
- return;
+ break;
}
- pa_memblock_release(memchunk->memblock);
+ case PA_SINK_MESSAGE_SET_STATE:
+
+ if (PA_PTR_TO_UINT(data) == PA_SINK_SUSPENDED) {
+ pa_assert(u->sink->thread_info.state != PA_SINK_SUSPENDED);
- if (memchunk == &u->memchunk) {
- size_t l = frames * u->frame_size;
- memchunk->index += l;
- memchunk->length -= l;
+ if (suspend(u) < 0)
+ return -1;
+
+ } else if (u->sink->thread_info.state == PA_SINK_SUSPENDED) {
+ pa_assert(PA_PTR_TO_UINT(data) != PA_SINK_SUSPENDED);
- if (memchunk->length == 0) {
- pa_memblock_unref(memchunk->memblock);
- memchunk->memblock = NULL;
- memchunk->index = memchunk->length = 0;
+ if (unsuspend(u) < 0)
+ return -1;
}
- }
+
+ break;
+
+/* case PA_SINK_MESSAGE_SET_VOLUME: */
+
+/* if (u->use_pcm_volume && u->fd >= 0) { */
- break;
+/* if (pa_oss_set_pcm_volume(u->fd, &u->sink->sample_spec, ((pa_cvolume*) data)) < 0) { */
+/* pa_log_info("Device doesn't support setting mixer settings: %s", pa_cstrerror(errno)); */
+/* u->use_pcm_volume = 0; */
+/* } else */
+/* return 0; */
+/* } */
+
+/* break; */
+
+/* case PA_SINK_MESSAGE_GET_VOLUME: */
+
+/* if (u->use_pcm_volume && u->fd >= 0) { */
+
+/* if (pa_oss_get_pcm_volume(u->fd, &u->sink->sample_spec, ((pa_cvolume*) data)) < 0) { */
+/* pa_log_info("Device doesn't support reading mixer settings: %s", pa_cstrerror(errno)); */
+/* u->use_pcm_volume = 0; */
+/* } else */
+/* return 0; */
+/* } */
+
+/* break; */
}
+
+ return pa_sink_process_msg(o, code, data, chunk);
}
-static void fdl_callback(void *userdata) {
- struct userdata *u = userdata;
- assert(u);
- if (snd_pcm_state(u->pcm_handle) == SND_PCM_STATE_XRUN)
- if (xrun_recovery(u) < 0)
- return;
+/* static int mixer_callback(snd_mixer_elem_t *elem, unsigned int mask) { */
+/* struct userdata *u = snd_mixer_elem_get_callback_private(elem); */
- if (snd_pcm_state(u->pcm_handle) == SND_PCM_STATE_SUSPENDED)
- if (suspend_recovery(u) < 0)
- return;
+/* pa_assert(u && u->mixer_handle); */
- do_write(u);
-}
+/* if (mask == SND_CTL_EVENT_MASK_REMOVE) */
+/* return 0; */
-static int mixer_callback(snd_mixer_elem_t *elem, unsigned int mask) {
- struct userdata *u = snd_mixer_elem_get_callback_private(elem);
+/* if (mask & SND_CTL_EVENT_MASK_VALUE) { */
+/* if (u->sink->get_hw_volume) */
+/* u->sink->get_hw_volume(u->sink); */
+/* if (u->sink->get_hw_mute) */
+/* u->sink->get_hw_mute(u->sink); */
+/* pa_subscription_post(u->sink->core, */
+/* PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, */
+/* u->sink->index); */
+/* } */
- assert(u && u->mixer_handle);
+/* return 0; */
+/* } */
- if (mask == SND_CTL_EVENT_MASK_REMOVE)
- return 0;
+/* static int sink_get_hw_volume_cb(pa_sink *s) { */
+/* struct userdata *u = s->userdata; */
+/* int err; */
+/* int i; */
- if (mask & SND_CTL_EVENT_MASK_VALUE) {
- if (u->sink->get_hw_volume)
- u->sink->get_hw_volume(u->sink);
- if (u->sink->get_hw_mute)
- u->sink->get_hw_mute(u->sink);
- pa_subscription_post(u->sink->core,
- PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE,
- u->sink->index);
- }
+/* pa_assert(u); */
+/* pa_assert(u->mixer_elem); */
- return 0;
-}
+/* for (i = 0; i < s->hw_volume.channels; i++) { */
+/* long set_vol, vol; */
-static pa_usec_t sink_get_latency_cb(pa_sink *s) {
- pa_usec_t r = 0;
- struct userdata *u = s->userdata;
- snd_pcm_sframes_t frames;
- int err;
+/* pa_assert(snd_mixer_selem_has_playback_channel(u->mixer_elem, i)); */
- assert(s && u && u->sink);
+/* if ((err = snd_mixer_selem_get_playback_volume(u->mixer_elem, i, &vol)) < 0) */
+/* goto fail; */
- if ((err = snd_pcm_delay(u->pcm_handle, &frames)) < 0) {
- pa_log("failed to get delay: %s", snd_strerror(err));
- s->get_latency = NULL;
- return 0;
- }
+/* set_vol = (long) roundf(((float) s->hw_volume.values[i] * (u->hw_volume_max - u->hw_volume_min)) / PA_VOLUME_NORM) + u->hw_volume_min; */
- if (frames < 0)
- frames = 0;
+/* /\* Try to avoid superfluous volume changes *\/ */
+/* if (set_vol != vol) */
+/* s->hw_volume.values[i] = (pa_volume_t) roundf(((float) (vol - u->hw_volume_min) * PA_VOLUME_NORM) / (u->hw_volume_max - u->hw_volume_min)); */
+/* } */
- r += pa_bytes_to_usec(frames * u->frame_size, &s->sample_spec);
+/* return 0; */
- if (u->memchunk.memblock)
- r += pa_bytes_to_usec(u->memchunk.length, &s->sample_spec);
+/* fail: */
+/* pa_log_error("Unable to read volume: %s", snd_strerror(err)); */
+/* s->get_hw_volume = NULL; */
+/* s->set_hw_volume = NULL; */
+/* return -1; */
+/* } */
- return r;
-}
+/* static int sink_set_hw_volume_cb(pa_sink *s) { */
+/* struct userdata *u = s->userdata; */
+/* int err; */
+/* int i; */
+/* pa_volume_t vol; */
-static int sink_get_hw_volume_cb(pa_sink *s) {
- struct userdata *u = s->userdata;
- int err;
- int i;
+/* pa_assert(u); */
+/* pa_assert(u->mixer_elem); */
- assert(u);
- assert(u->mixer_elem);
+/* for (i = 0; i < s->hw_volume.channels; i++) { */
+/* long alsa_vol; */
- for (i = 0; i < s->hw_volume.channels; i++) {
- long set_vol, vol;
+/* pa_assert(snd_mixer_selem_has_playback_channel(u->mixer_elem, i)); */
- assert(snd_mixer_selem_has_playback_channel(u->mixer_elem, i));
+/* vol = s->hw_volume.values[i]; */
- if ((err = snd_mixer_selem_get_playback_volume(u->mixer_elem, i, &vol)) < 0)
- goto fail;
+/* if (vol > PA_VOLUME_NORM) */
+/* vol = PA_VOLUME_NORM; */
- set_vol = (long) roundf(((float) s->hw_volume.values[i] * (u->hw_volume_max - u->hw_volume_min)) / PA_VOLUME_NORM) + u->hw_volume_min;
+/* alsa_vol = (long) roundf(((float) vol * (u->hw_volume_max - u->hw_volume_min)) / PA_VOLUME_NORM) + u->hw_volume_min; */
- /* Try to avoid superfluous volume changes */
- if (set_vol != vol)
- s->hw_volume.values[i] = (pa_volume_t) roundf(((float) (vol - u->hw_volume_min) * PA_VOLUME_NORM) / (u->hw_volume_max - u->hw_volume_min));
- }
+/* if ((err = snd_mixer_selem_set_playback_volume(u->mixer_elem, i, alsa_vol)) < 0) */
+/* goto fail; */
+/* } */
- return 0;
+/* return 0; */
-fail:
- pa_log_error("Unable to read volume: %s", snd_strerror(err));
- s->get_hw_volume = NULL;
- s->set_hw_volume = NULL;
- return -1;
-}
+/* fail: */
+/* pa_log_error("Unable to set volume: %s", snd_strerror(err)); */
+/* s->get_hw_volume = NULL; */
+/* s->set_hw_volume = NULL; */
+/* return -1; */
+/* } */
-static int sink_set_hw_volume_cb(pa_sink *s) {
- struct userdata *u = s->userdata;
- int err;
- int i;
- pa_volume_t vol;
+/* static int sink_get_hw_mute_cb(pa_sink *s) { */
+/* struct userdata *u = s->userdata; */
+/* int err, sw; */
- assert(u);
- assert(u->mixer_elem);
+/* pa_assert(u && u->mixer_elem); */
- for (i = 0; i < s->hw_volume.channels; i++) {
- long alsa_vol;
+/* err = snd_mixer_selem_get_playback_switch(u->mixer_elem, 0, &sw); */
+/* if (err) { */
+/* pa_log_error("Unable to get switch: %s", snd_strerror(err)); */
+/* s->get_hw_mute = NULL; */
+/* s->set_hw_mute = NULL; */
+/* return -1; */
+/* } */
- assert(snd_mixer_selem_has_playback_channel(u->mixer_elem, i));
+/* s->hw_muted = !sw; */
- vol = s->hw_volume.values[i];
+/* return 0; */
+/* } */
- if (vol > PA_VOLUME_NORM)
- vol = PA_VOLUME_NORM;
+/* static int sink_set_hw_mute_cb(pa_sink *s) { */
+/* struct userdata *u = s->userdata; */
+/* int err; */
- alsa_vol = (long) roundf(((float) vol * (u->hw_volume_max - u->hw_volume_min)) / PA_VOLUME_NORM) + u->hw_volume_min;
+/* pa_assert(u && u->mixer_elem); */
- if ((err = snd_mixer_selem_set_playback_volume(u->mixer_elem, i, alsa_vol)) < 0)
- goto fail;
- }
+/* err = snd_mixer_selem_set_playback_switch_all(u->mixer_elem, !s->hw_muted); */
+/* if (err) { */
+/* pa_log_error("Unable to set switch: %s", snd_strerror(err)); */
+/* s->get_hw_mute = NULL; */
+/* s->set_hw_mute = NULL; */
+/* return -1; */
+/* } */
- return 0;
+/* return 0; */
+/* } */
-fail:
- pa_log_error("Unable to set volume: %s", snd_strerror(err));
- s->get_hw_volume = NULL;
- s->set_hw_volume = NULL;
- return -1;
-}
+static void thread_func(void *userdata) {
+ enum {
+ POLLFD_ASYNCQ,
+ POLLFD_ALSA_BASE
+ };
+
+ struct userdata *u = userdata;
+ struct pollfd *pollfd = NULL;
+ int n_alsa_fds, err;
+ unsigned short revents = 0;
+ snd_pcm_status_t *status;
-static int sink_get_hw_mute_cb(pa_sink *s) {
- struct userdata *u = s->userdata;
- int err, sw;
+ pa_assert(u);
+ snd_pcm_status_alloca(&status);
- assert(u && u->mixer_elem);
+ pa_log_debug("Thread starting up");
- err = snd_mixer_selem_get_playback_switch(u->mixer_elem, 0, &sw);
- if (err) {
- pa_log_error("Unable to get switch: %s", snd_strerror(err));
- s->get_hw_mute = NULL;
- s->set_hw_mute = NULL;
- return -1;
+ if ((n_alsa_fds = snd_pcm_poll_descriptors_count(u->pcm_handle)) < 0) {
+ pa_log("snd_pcm_poll_descriptors_count() failed: %s", snd_strerror(n_alsa_fds));
+ goto fail;
}
- s->hw_muted = !sw;
+ pollfd = pa_xnew0(struct pollfd, POLLFD_ALSA_BASE + n_alsa_fds);
- return 0;
-}
+ pollfd[POLLFD_ASYNCQ].fd = pa_asyncmsgq_get_fd(u->asyncmsgq);
+ pollfd[POLLFD_ASYNCQ].events = POLLIN;
-static int sink_set_hw_mute_cb(pa_sink *s) {
- struct userdata *u = s->userdata;
- int err;
+ if ((err = snd_pcm_poll_descriptors(u->pcm_handle, pollfd+POLLFD_ALSA_BASE, n_alsa_fds)) < 0) {
+ pa_log("snd_pcm_poll_descriptors() failed: %s", snd_strerror(err));
+ goto fail;
+ }
+
+ for (;;) {
+ pa_msgobject *object;
+ int code;
+ void *data;
+ pa_memchunk chunk;
+ int r;
+
+/* pa_log("loop"); */
+
+ /* Check whether there is a message for us to process */
+ if (pa_asyncmsgq_get(u->asyncmsgq, &object, &code, &data, &chunk, 0) == 0) {
+ int ret;
+
+/* pa_log("processing msg"); */
+
+ if (!object && code == PA_MESSAGE_SHUTDOWN) {
+ pa_asyncmsgq_done(u->asyncmsgq, 0);
+ goto finish;
+ }
+
+ ret = pa_asyncmsgq_dispatch(object, code, data, &chunk);
+ pa_asyncmsgq_done(u->asyncmsgq, ret);
+ continue;
+ }
+
+/* pa_log("loop2"); */
+
+ /* Render some data and write it to the dsp */
+
+ if (u->sink->thread_info.state != PA_SINK_SUSPENDED && ((revents & POLLOUT) || u->first == 1)) {
+
+ if (u->use_mmap) {
+ int ret;
+
+ if ((ret = mmap_write(u)) < 0)
+ goto fail;
+
+ revents &= ~POLLOUT;
+
+ if (ret > 0)
+ continue;
+
+ } else {
+ ssize_t l;
+
+ snd_pcm_hwsync(u->pcm_handle);
+ if ((err = snd_pcm_status(u->pcm_handle, status)) >= 0)
+ l = snd_pcm_status_get_avail(status) * u->frame_size;
+ else
+ l = u->fragment_size;
+
+ do {
+ void *p;
+ snd_pcm_sframes_t t;
+
+ pa_assert(l > 0);
+
+ if (u->memchunk.length <= 0)
+ pa_sink_render(u->sink, l, &u->memchunk);
+
+ pa_assert(u->memchunk.length > 0);
+
+ p = pa_memblock_acquire(u->memchunk.memblock);
+ t = snd_pcm_writei(u->pcm_handle, (uint8_t*) p + u->memchunk.index, u->memchunk.length / u->frame_size);
+ pa_memblock_release(u->memchunk.memblock);
+
+/* pa_log("wrote %i bytes of %u", t, l); */
+
+ pa_assert(t != 0);
+
+ if (t < 0) {
+
+ if (t == -EPIPE) {
+ pa_log_debug("Buffer underrun!");
+ u->first = 1;
+ }
+
+ if ((t = snd_pcm_recover(u->pcm_handle, t, 1)) == 0)
+ continue;
+
+ if (t == EAGAIN) {
+ pa_log_debug("EAGAIN");
+
+ revents &= ~POLLOUT;
+ break;
+
+ } else {
+ pa_log("Failed to write data to DSP: %s", snd_strerror(t));
+ goto fail;
+ }
+
+ } else {
+
+ u->memchunk.index += t * u->frame_size;
+ u->memchunk.length -= t * u->frame_size;
+
+ if (u->memchunk.length <= 0) {
+ pa_memblock_unref(u->memchunk.memblock);
+ pa_memchunk_reset(&u->memchunk);
+ }
+
+ l -= t * u->frame_size;
+
+ revents &= ~POLLOUT;
+ }
+
+ } while (l > 0);
+
+ continue;
+ }
+
+ if (u->first) {
+ pa_log_info("Starting playback.");
+
+ if ((err = snd_pcm_start(u->pcm_handle)) < 0) {
+ pa_log("Failed to start PCM playback: %s", snd_strerror(err));
+ goto fail;
+ }
+
+ u->first = 0;
+ }
+ }
- assert(u && u->mixer_elem);
+ /* Hmm, nothing to do. Let's sleep */
+ if (pa_asyncmsgq_before_poll(u->asyncmsgq) < 0)
+ continue;
- err = snd_mixer_selem_set_playback_switch_all(u->mixer_elem, !s->hw_muted);
- if (err) {
- pa_log_error("Unable to set switch: %s", snd_strerror(err));
- s->get_hw_mute = NULL;
- s->set_hw_mute = NULL;
- return -1;
+/* pa_log("polling for %i", POLLFD_ALSA_BASE + (u->sink->thread_info.state != PA_SINK_SUSPENDED ? n_alsa_fds : 0)); */
+ r = poll(pollfd, POLLFD_ALSA_BASE + (u->sink->thread_info.state != PA_SINK_SUSPENDED ? n_alsa_fds : 0), -1);
+ /*pa_log("polling got dsp=%i amq=%i (%i)", r > 0 ? pollfd[POLLFD_DSP].revents : 0, r > 0 ? pollfd[POLLFD_ASYNCQ].revents : 0, r); */
+/* pa_log("poll end"); */
+
+ pa_asyncmsgq_after_poll(u->asyncmsgq);
+
+ if (r < 0) {
+ if (errno == EINTR) {
+ pollfd[POLLFD_ASYNCQ].revents = 0;
+ revents = 0;
+ continue;
+ }
+
+ pa_log("poll() failed: %s", pa_cstrerror(errno));
+ goto fail;
+ }
+
+ pa_assert(r > 0);
+
+ if (u->sink->thread_info.state != PA_SINK_SUSPENDED) {
+ if ((err = snd_pcm_poll_descriptors_revents(u->pcm_handle, pollfd + POLLFD_ALSA_BASE, n_alsa_fds, &revents)) < 0) {
+ pa_log("snd_pcm_poll_descriptors_revents() failed: %s", snd_strerror(err));
+ goto fail;
+ }
+
+/* pa_log("got alsa event"); */
+ } else
+ revents = 0;
+
+ if (revents & ~(POLLOUT|POLLERR)) {
+ pa_log("DSP shutdown.");
+ goto fail;
+ }
+
+ pa_assert((pollfd[POLLFD_ASYNCQ].revents & ~POLLIN) == 0);
}
- return 0;
+fail:
+ /* We have to continue processing messages until we receive the
+ * SHUTDOWN message */
+ pa_asyncmsgq_post(u->core->asyncmsgq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, NULL, NULL);
+ pa_asyncmsgq_wait_for(u->asyncmsgq, PA_MESSAGE_SHUTDOWN);
+
+finish:
+ pa_log_debug("Thread shutting down");
+
+ pa_xfree(pollfd);
}
int pa__init(pa_core *c, pa_module*m) {
+
pa_modargs *ma = NULL;
int ret = -1;
struct userdata *u = NULL;
const char *dev;
pa_sample_spec ss;
pa_channel_map map;
- uint32_t periods, fragsize;
+ uint32_t nfrags, frag_size;
snd_pcm_uframes_t period_size;
size_t frame_size;
snd_pcm_info_t *pcm_info = NULL;
@@ -416,6 +709,10 @@ int pa__init(pa_core *c, pa_module*m) {
const char *name;
char *name_buf = NULL;
int namereg_fail;
+ int use_mmap = 1, b;
+
+ pa_assert(c);
+ pa_assert(m);
if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
pa_log("failed to parse module arguments");
@@ -430,19 +727,29 @@ int pa__init(pa_core *c, pa_module*m) {
frame_size = pa_frame_size(&ss);
- /* Fix latency to 100ms */
- periods = 8;
- fragsize = pa_bytes_per_second(&ss)/128;
+ nfrags = DEFAULT_NFRAGS;
+ frag_size = pa_usec_to_bytes(DEFAULT_FRAGSIZE_MSEC*1000, &ss);
+ if (frag_size <= 0)
+ frag_size = frame_size;
- if (pa_modargs_get_value_u32(ma, "fragments", &periods) < 0 || pa_modargs_get_value_u32(ma, "fragment_size", &fragsize) < 0) {
+ if (pa_modargs_get_value_u32(ma, "fragments", &nfrags) < 0 || pa_modargs_get_value_u32(ma, "fragment_size", &frag_size) < 0) {
pa_log("failed to parse buffer metrics");
goto fail;
}
- period_size = fragsize/frame_size;
+ period_size = frag_size/frame_size;
+ if (pa_modargs_get_value_boolean(ma, "mmap", &use_mmap) < 0) {
+ pa_log("Failed to parse mmap argument.");
+ goto fail;
+ }
+
u = pa_xnew0(struct userdata, 1);
- m->userdata = u;
+ u->core = c;
u->module = m;
+ m->userdata = u;
+ u->use_mmap = use_mmap;
+ u->first = 1;
+ pa_assert_se(u->asyncmsgq = pa_asyncmsgq_new(0));
snd_config_update_free_global();
if ((err = snd_pcm_open(&u->pcm_handle, dev = pa_modargs_get_value(ma, "device", DEFAULT_DEVICE), SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK)) < 0) {
@@ -450,17 +757,25 @@ int pa__init(pa_core *c, pa_module*m) {
goto fail;
}
+ u->device_name = pa_xstrdup(dev);
+
if ((err = snd_pcm_info_malloc(&pcm_info)) < 0 ||
(err = snd_pcm_info(u->pcm_handle, pcm_info)) < 0) {
pa_log("Error fetching PCM info: %s", snd_strerror(err));
goto fail;
}
- if ((err = pa_alsa_set_hw_params(u->pcm_handle, &ss, &periods, &period_size)) < 0) {
+ b = use_mmap;
+ if ((err = pa_alsa_set_hw_params(u->pcm_handle, &ss, &nfrags, &period_size, &b)) < 0) {
pa_log("Failed to set hardware parameters: %s", snd_strerror(err));
goto fail;
}
+ if (use_mmap && !b) {
+ pa_log_info("Device doesn't support mmap(), falling back to UNIX read/write mode.");
+ u->use_mmap = use_mmap = b;
+ }
+
/* ALSA might tweak the sample spec, so recalculate the frame size */
frame_size = pa_frame_size(&ss);
@@ -468,16 +783,16 @@ int pa__init(pa_core *c, pa_module*m) {
/* Seems ALSA didn't like the channel number, so let's fix the channel map */
pa_channel_map_init_auto(&map, ss.channels, PA_CHANNEL_MAP_ALSA);
- if ((err = snd_mixer_open(&u->mixer_handle, 0)) < 0) {
- pa_log("Error opening mixer: %s", snd_strerror(err));
- goto fail;
- }
+/* if ((err = snd_mixer_open(&u->mixer_handle, 0)) < 0) */
+/* pa_log_warn("Error opening mixer: %s", snd_strerror(err)); */
+/* else { */
- if ((pa_alsa_prepare_mixer(u->mixer_handle, dev) < 0) ||
- !(u->mixer_elem = pa_alsa_find_elem(u->mixer_handle, "PCM", "Master"))) {
- snd_mixer_close(u->mixer_handle);
- u->mixer_handle = NULL;
- }
+/* if ((pa_alsa_prepare_mixer(u->mixer_handle, dev) < 0) || */
+/* !(u->mixer_elem = pa_alsa_find_elem(u->mixer_handle, "PCM", "Master"))) { */
+/* snd_mixer_close(u->mixer_handle); */
+/* u->mixer_handle = NULL; */
+/* } */
+/* } */
if ((name = pa_modargs_get_value(ma, "sink_name", NULL)))
namereg_fail = 1;
@@ -486,84 +801,86 @@ int pa__init(pa_core *c, pa_module*m) {
namereg_fail = 0;
}
- if (!(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);
+
+ if (!u->sink) {
pa_log("Failed to create sink object");
goto fail;
}
- u->sink->is_hardware = 1;
- u->sink->get_latency = sink_get_latency_cb;
- if (u->mixer_handle) {
- assert(u->mixer_elem);
- if (snd_mixer_selem_has_playback_volume(u->mixer_elem)) {
- int i;
-
- for (i = 0;i < ss.channels;i++) {
- if (!snd_mixer_selem_has_playback_channel(u->mixer_elem, i))
- break;
- }
-
- if (i == ss.channels) {
- u->sink->get_hw_volume = sink_get_hw_volume_cb;
- u->sink->set_hw_volume = sink_set_hw_volume_cb;
- snd_mixer_selem_get_playback_volume_range(
- u->mixer_elem, &u->hw_volume_min, &u->hw_volume_max);
- }
- }
- if (snd_mixer_selem_has_playback_switch(u->mixer_elem)) {
- u->sink->get_hw_mute = sink_get_hw_mute_cb;
- u->sink->set_hw_mute = sink_set_hw_mute_cb;
- }
- }
+ u->sink->parent.process_msg = sink_process_msg;
u->sink->userdata = u;
- pa_sink_set_owner(u->sink, m);
- pa_sink_set_description(u->sink, t = pa_sprintf_malloc("ALSA PCM on %s (%s)", dev, snd_pcm_info_get_name(pcm_info)));
- pa_xfree(t);
- u->pcm_fdl = pa_alsa_fdlist_new();
- assert(u->pcm_fdl);
- if (pa_alsa_fdlist_init_pcm(u->pcm_fdl, u->pcm_handle, c->mainloop, fdl_callback, u) < 0) {
- pa_log("failed to initialise file descriptor monitoring");
- goto fail;
- }
-
- if (u->mixer_handle) {
- u->mixer_fdl = pa_alsa_fdlist_new();
- assert(u->mixer_fdl);
- if (pa_alsa_fdlist_init_mixer(u->mixer_fdl, u->mixer_handle, c->mainloop) < 0) {
- pa_log("failed to initialise file descriptor monitoring");
- goto fail;
- }
- snd_mixer_elem_set_callback(u->mixer_elem, mixer_callback);
- snd_mixer_elem_set_callback_private(u->mixer_elem, u);
- } else
- u->mixer_fdl = NULL;
+ pa_sink_set_module(u->sink, m);
+ pa_sink_set_asyncmsgq(u->sink, u->asyncmsgq);
+ pa_sink_set_description(u->sink, t = pa_sprintf_malloc(
+ "ALSA PCM on %s (%s)",
+ dev,
+ snd_pcm_info_get_name(pcm_info)));
+ pa_xfree(t);
+
+ u->sink->is_hardware = 1;
+
+/* if (u->mixer_handle) { */
+/* pa_assert(u->mixer_elem); */
+/* if (snd_mixer_selem_has_playback_volume(u->mixer_elem)) { */
+/* int i; */
+
+/* for (i = 0;i < ss.channels;i++) { */
+/* if (!snd_mixer_selem_has_playback_channel(u->mixer_elem, i)) */
+/* break; */
+/* } */
+
+/* if (i == ss.channels) { */
+/* u->sink->get_hw_volume = sink_get_hw_volume_cb; */
+/* u->sink->set_hw_volume = sink_set_hw_volume_cb; */
+/* snd_mixer_selem_get_playback_volume_range( */
+/* u->mixer_elem, &u->hw_volume_min, &u->hw_volume_max); */
+/* } */
+/* } */
+/* if (snd_mixer_selem_has_playback_switch(u->mixer_elem)) { */
+/* u->sink->get_hw_mute = sink_get_hw_mute_cb; */
+/* u->sink->set_hw_mute = sink_set_hw_mute_cb; */
+/* } */
+/* } */
+
+/* if (u->mixer_handle) { */
+/* u->mixer_fdl = pa_alsa_fdlist_new(); */
+/* pa_assert(u->mixer_fdl); */
+/* if (pa_alsa_fdlist_init_mixer(u->mixer_fdl, u->mixer_handle, c->mainloop) < 0) { */
+/* pa_log("failed to initialise file descriptor monitoring"); */
+/* goto fail; */
+/* } */
+/* snd_mixer_elem_set_callback(u->mixer_elem, mixer_callback); */
+/* snd_mixer_elem_set_callback_private(u->mixer_elem, u); */
+/* } else */
+/* u->mixer_fdl = NULL; */
u->frame_size = frame_size;
u->fragment_size = period_size * frame_size;
+ u->nfragments = nfrags;
+ u->hwbuf_size = u->fragment_size * nfrags;
- pa_log_info("using %u fragments of size %lu bytes.", periods, (long unsigned)u->fragment_size);
+ pa_log_info("Using %u fragments of size %lu bytes.", nfrags, (long unsigned) u->fragment_size);
- u->silence.memblock = pa_memblock_new(c->mempool, u->silence.length = u->fragment_size);
- assert(u->silence.memblock);
- pa_silence_memblock(u->silence.memblock, &ss);
- u->silence.index = 0;
+ pa_memchunk_reset(&u->memchunk);
- u->memchunk.memblock = NULL;
- u->memchunk.index = u->memchunk.length = 0;
+ if (!(u->thread = pa_thread_new(thread_func, u))) {
+ pa_log("Failed to create thread.");
+ goto fail;
+ }
+
+ /* Get initial mixer settings */
+ if (u->sink->get_volume)
+ u->sink->get_volume(u->sink);
+ if (u->sink->get_mute)
+ u->sink->get_mute(u->sink);
ret = 0;
- /* Get initial mixer settings */
- if (u->sink->get_hw_volume)
- u->sink->get_hw_volume(u->sink);
- if (u->sink->get_hw_mute)
- u->sink->get_hw_mute(u->sink);
-
finish:
- pa_xfree(name_buf);
-
if (ma)
pa_modargs_free(ma);
@@ -582,18 +899,42 @@ fail:
void pa__done(pa_core *c, pa_module*m) {
struct userdata *u;
- assert(c && m);
+
+ pa_assert(c);
+ pa_assert(m);
if (!(u = m->userdata))
return;
- clear_up(u);
+ if (u->sink)
+ pa_sink_disconnect(u->sink);
+
+ if (u->thread) {
+ pa_asyncmsgq_send(u->asyncmsgq, NULL, PA_MESSAGE_SHUTDOWN, NULL, NULL);
+ pa_thread_free(u->thread);
+ }
+
+ if (u->asyncmsgq)
+ pa_asyncmsgq_free(u->asyncmsgq);
+ if (u->sink)
+ pa_sink_unref(u->sink);
+
if (u->memchunk.memblock)
pa_memblock_unref(u->memchunk.memblock);
- if (u->silence.memblock)
- pa_memblock_unref(u->silence.memblock);
+
+/* if (u->mixer_handle) */
+/* snd_mixer_close(u->mixer_handle); */
+ if (u->pcm_handle) {
+ snd_pcm_drop(u->pcm_handle);
+ snd_pcm_close(u->pcm_handle);
+ }
+
+ pa_xfree(u->device_name);
+
+ snd_config_update_free_global();
+
pa_xfree(u);
}
diff --git a/src/modules/module-oss.c b/src/modules/module-oss.c
index 96e0c3ce..4db1ed02 100644
--- a/src/modules/module-oss.c
+++ b/src/modules/module-oss.c
@@ -48,9 +48,14 @@
#include <sys/mman.h>
#endif
+#ifdef HAVE_SYS_POLL_H
+#include <sys/poll.h>
+#else
+#include "poll.h"
+#endif
+
#include <sys/soundcard.h>
#include <sys/ioctl.h>
-#include <sys/poll.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <stdio.h>
@@ -73,6 +78,7 @@
#include <pulsecore/core-util.h>
#include <pulsecore/modargs.h>
#include <pulsecore/log.h>
+#include <pulsecore/macro.h>
#include "oss-util.h"
#include "module-oss-symdef.h"
@@ -805,7 +811,6 @@ static void thread_func(void *userdata) {
} else {
ssize_t l;
- void *p;
int loop = 0;
l = u->out_fragment_size;
@@ -825,6 +830,7 @@ static void thread_func(void *userdata) {
}
do {
+ void *p;
ssize_t t;
pa_assert(l > 0);
@@ -1019,9 +1025,10 @@ finish:
}
int pa__init(pa_core *c, pa_module*m) {
+
struct audio_buf_info info;
struct userdata *u = NULL;
- const char *p;
+ const char *dev;
int fd = -1;
int nfrags, frag_size;
int mode, caps;
@@ -1074,7 +1081,7 @@ int pa__init(pa_core *c, pa_module*m) {
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;
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;
}
- 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);
else
hwdesc[0] = 0;
@@ -1111,11 +1118,12 @@ int pa__init(pa_core *c, pa_module*m) {
u->core = c;
u->module = m;
m->userdata = u;
+ u->fd = fd;
u->use_getospace = u->use_getispace = 1;
u->use_getodelay = 1;
u->use_input_volume = u->use_pcm_volume = 1;
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->out_fragment_size = u->in_fragment_size = u->frag_size = frag_size;
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)))
namereg_fail = 1;
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;
}
u->source = pa_source_new(c, __FILE__, name, namereg_fail, &ss, &map);
pa_xfree(name_buf);
- if (!u->source)
+ if (!u->source) {
+ pa_log("Failed to create source object");
goto fail;
+ }
u->source->parent.process_msg = source_process_msg;
u->source->userdata = u;
pa_source_set_module(u->source, m);
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",
- p,
- hwdesc[0] ? " (" : "",
- hwdesc[0] ? hwdesc : "",
- hwdesc[0] ? ")" : ""));
+ pa_source_set_description(u->source, t = pa_sprintf_malloc(
+ "OSS PCM on %s%s%s%s",
+ dev,
+ hwdesc[0] ? " (" : "",
+ hwdesc[0] ? hwdesc : "",
+ hwdesc[0] ? ")" : ""));
pa_xfree(t);
u->source->is_hardware = 1;
u->source->refresh_volume = 1;
@@ -1210,25 +1221,28 @@ try_write:
if ((name = pa_modargs_get_value(ma, "sink_name", NULL)))
namereg_fail = 1;
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;
}
u->sink = pa_sink_new(c, __FILE__, name, namereg_fail, &ss, &map);
pa_xfree(name_buf);
- if (!u->sink)
+ if (!u->sink) {
+ pa_log("Failed to create sink object");
goto fail;
+ }
u->sink->parent.process_msg = sink_process_msg;
u->sink->userdata = u;
pa_sink_set_module(u->sink, m);
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",
- p,
- hwdesc[0] ? " (" : "",
- hwdesc[0] ? hwdesc : "",
- hwdesc[0] ? ")" : ""));
+ pa_sink_set_description(u->sink, t = pa_sprintf_malloc(
+ "OSS PCM on %s%s%s%s",
+ dev,
+ hwdesc[0] ? " (" : "",
+ hwdesc[0] ? hwdesc : "",
+ hwdesc[0] ? ")" : ""));
pa_xfree(t);
u->sink->is_hardware = 1;
u->sink->refresh_volume = 1;
@@ -1241,8 +1255,6 @@ go_on:
pa_assert(u->source || u->sink);
- u->fd = fd;
-
pa_memchunk_reset(&u->memchunk);
if (!(u->thread = pa_thread_new(thread_func, u))) {
@@ -1261,7 +1273,10 @@ go_on:
return 0;
fail:
- if (fd >= 0)
+
+ if (u)
+ pa__done(c, m);
+ else if (fd >= 0)
close(fd);
if (ma)