[pulseaudio-commits] r1561 - in /branches/lennart/src: Makefile.am modules/module-alsa-source.c

svnmailer-noreply at 0pointer.de svnmailer-noreply at 0pointer.de
Sat Jul 28 16:50:21 PDT 2007


Author: lennart
Date: Sun Jul 29 01:50:20 2007
New Revision: 1561

URL: http://0pointer.de/cgi-bin/viewcvs.cgi?rev=3D1561&root=3Dpulseaudio&vi=
ew=3Drev
Log:
port module-alsa-source to new lock-free core

Modified:
    branches/lennart/src/Makefile.am
    branches/lennart/src/modules/module-alsa-source.c

Modified: branches/lennart/src/Makefile.am
URL: http://0pointer.de/cgi-bin/viewcvs.cgi/branches/lennart/src/Makefile.a=
m?rev=3D1561&root=3Dpulseaudio&r1=3D1560&r2=3D1561&view=3Ddiff
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
--- branches/lennart/src/Makefile.am (original)
+++ branches/lennart/src/Makefile.am Sun Jul 29 01:50:20 2007
@@ -937,8 +937,8 @@
 if HAVE_ALSA
 modlibexec_LTLIBRARIES +=3D \
 		libalsa-util.la \
-		module-alsa-sink.la
-#		module-alsa-source.la
+		module-alsa-sink.la \
+		module-alsa-source.la
 endif
 =

 if HAVE_SOLARIS
@@ -1195,10 +1195,10 @@
 module_alsa_sink_la_LIBADD =3D $(AM_LIBADD) $(ASOUNDLIB_LIBS) libalsa-util=
.la libpulsecore.la
 module_alsa_sink_la_CFLAGS =3D $(AM_CFLAGS) $(ASOUNDLIB_CFLAGS)
 =

-#module_alsa_source_la_SOURCES =3D modules/module-alsa-source.c
-#module_alsa_source_la_LDFLAGS =3D -module -avoid-version
-#module_alsa_source_la_LIBADD =3D $(AM_LIBADD) $(ASOUNDLIB_LIBS) libalsa-u=
til.la libpulsecore.la
-#module_alsa_source_la_CFLAGS =3D $(AM_CFLAGS) $(ASOUNDLIB_CFLAGS)
+module_alsa_source_la_SOURCES =3D modules/module-alsa-source.c
+module_alsa_source_la_LDFLAGS =3D -module -avoid-version
+module_alsa_source_la_LIBADD =3D $(AM_LIBADD) $(ASOUNDLIB_LIBS) libalsa-ut=
il.la libpulsecore.la
+module_alsa_source_la_CFLAGS =3D $(AM_CFLAGS) $(ASOUNDLIB_CFLAGS)
 =

 # Solaris
 =


Modified: branches/lennart/src/modules/module-alsa-source.c
URL: http://0pointer.de/cgi-bin/viewcvs.cgi/branches/lennart/src/modules/mo=
dule-alsa-source.c?rev=3D1561&root=3Dpulseaudio&r1=3D1560&r2=3D1561&view=3D=
diff
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
--- branches/lennart/src/modules/module-alsa-source.c (original)
+++ branches/lennart/src/modules/module-alsa-source.c Sun Jul 29 01:50:20 2=
007
@@ -48,6 +48,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-source-symdef.h"
@@ -63,20 +66,33 @@
         "rate=3D<sample rate> "
         "fragments=3D<number of fragments> "
         "fragment_size=3D<fragment size> "
-        "channel_map=3D<channel map>")
+        "channel_map=3D<channel map> "
+        "mmap=3D<enable memory mapping?>")
+
+#define DEFAULT_DEVICE "default"
+#define DEFAULT_NFRAGS 4
+#define DEFAULT_FRAGSIZE_MSEC 25
 =

 struct userdata {
+    pa_core *core;
+    pa_module *module;
+    pa_source *source;
+    pa_thread *thread;
+    pa_asyncmsgq *asyncmsgq;
+
     snd_pcm_t *pcm_handle;
+
+    pa_alsa_fdlist *mixer_fdl;
     snd_mixer_t *mixer_handle;
     snd_mixer_elem_t *mixer_elem;
-    pa_source *source;
-    struct pa_alsa_fdlist *pcm_fdl;
-    struct pa_alsa_fdlist *mixer_fdl;
     long hw_volume_max, hw_volume_min;
 =

-    size_t frame_size, fragment_size;
-    pa_memchunk memchunk;
-    pa_module *module;
+    size_t frame_size, fragment_size, hwbuf_size;
+    unsigned nfragments;
+
+    char *device_name;
+
+    int use_mmap;
 };
 =

 static const char* const valid_modargs[] =3D {
@@ -88,317 +104,555 @@
     "fragments",
     "fragment_size",
     "channel_map",
+    "mmap",
     NULL
 };
 =

-#define DEFAULT_DEVICE "default"
-
-static void update_usage(struct userdata *u) {
-   pa_module_set_used(u->module, u->source ? pa_source_used_by(u->source) =
: 0);
-}
-
-static void clear_up(struct userdata *u) {
-    assert(u);
-
-    if (u->source) {
-        pa_source_disconnect(u->source);
-        pa_source_unref(u->source);
-        u->source =3D NULL;
-    }
-
-    if (u->pcm_fdl)
-        pa_alsa_fdlist_free(u->pcm_fdl);
-    if (u->mixer_fdl)
-        pa_alsa_fdlist_free(u->mixer_fdl);
-
-    u->pcm_fdl =3D u->mixer_fdl =3D NULL;
-
-    if (u->mixer_handle) {
-        snd_mixer_close(u->mixer_handle);
-        u->mixer_handle =3D NULL;
-    }
-
-    if (u->pcm_handle) {
-        snd_pcm_drop(u->pcm_handle);
-        snd_pcm_close(u->pcm_handle);
-        u->pcm_handle =3D NULL;
-    }
-}
-
-static int xrun_recovery(struct userdata *u) {
-    int ret;
-    assert(u);
-
-    pa_log_info("*** ALSA-XRUN (capture) ***");
-
-    if ((ret =3D snd_pcm_prepare(u->pcm_handle)) < 0) {
-        pa_log("snd_pcm_prepare() failed: %s", snd_strerror(-ret));
-
-        clear_up(u);
-        pa_module_unload_request(u->module);
-
+static int mmap_read(struct userdata *u) {
+    snd_pcm_sframes_t n;
+    int err;
+    const snd_pcm_channel_area_t *areas;
+    snd_pcm_uframes_t offset, frames;
+    int work_done =3D 0;
+    =

+    pa_assert(u);
+    pa_assert(u->source);
+
+    for (;;) {
+        pa_memchunk chunk;
+        void *p;
+        =

+        if ((n =3D snd_pcm_avail_update(u->pcm_handle)) < 0) {
+
+            if (n =3D=3D -EPIPE)
+                pa_log_debug("snd_pcm_avail_update: Buffer underrun!");
+            =

+            if ((err =3D snd_pcm_recover(u->pcm_handle, n, 1)) =3D=3D 0)
+                continue;
+
+            if (err =3D=3D -EAGAIN)
+                return work_done;
+            =

+            pa_log("snd_pcm_avail_update: %s", snd_strerror(n));
+            return -1;
+        }
+
+/*         pa_log("Got request for %i samples", (int) n); */
+        =

+        if (n <=3D 0)
+            return work_done;
+
+        frames =3D n;
+        =

+        if ((err =3D snd_pcm_mmap_begin(u->pcm_handle, &areas, &offset, &f=
rames)) < 0) {
+            =

+            if (err =3D=3D -EPIPE)
+                pa_log_debug("snd_pcm_mmap_begin: Buffer underrun!");
+
+            if ((err =3D snd_pcm_recover(u->pcm_handle, err, 1)) =3D=3D 0)
+                continue;
+
+            if (err =3D=3D -EAGAIN)
+                return work_done;
+
+            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) =3D=3D 0);
+        pa_assert((areas[0].step & 7)=3D=3D 0);
+
+        /* We assume a single interleaved memory buffer */
+        pa_assert((areas[0].first >> 3) =3D=3D 0);
+        pa_assert((areas[0].step >> 3) =3D=3D u->frame_size);
+
+        p =3D (uint8_t*) areas[0].addr + (offset * u->frame_size);
+        =

+        chunk.memblock =3D pa_memblock_new_fixed(u->core->mempool, p, fram=
es * u->frame_size, 1);
+        chunk.length =3D pa_memblock_get_length(chunk.memblock);
+        chunk.index =3D 0;
+
+        pa_source_post(u->source, &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 =3D snd_pcm_mmap_commit(u->pcm_handle, offset, frames)) <=
 0) {
+
+            if (err =3D=3D -EPIPE)
+                pa_log_debug("snd_pcm_mmap_commit: Buffer underrun!");
+            =

+            if ((err =3D snd_pcm_recover(u->pcm_handle, err, 1)) =3D=3D 0)
+                continue;
+            =

+            if (err =3D=3D -EAGAIN)
+                return work_done;
+            =

+            pa_log("Failed to write data to DSP: %s", snd_strerror(err));
+            return -1;
+        }
+
+        work_done =3D 1;
+
+/*         pa_log("wrote %i samples", (int) frames); */
+    }
+}
+
+static pa_usec_t source_get_latency(struct userdata *u) {
+    pa_usec_t r =3D 0;
+    snd_pcm_sframes_t frames =3D 0;
+    int err;
+    =

+    pa_assert(u);
+
+    snd_pcm_avail_update(u->pcm_handle);
+
+    if ((err =3D snd_pcm_delay(u->pcm_handle, &frames)) < 0) {
+        pa_log("Failed to get delay: %s", snd_strerror(err));
+        return 0;
+    }
+
+    if (frames > 0)
+        r =3D pa_bytes_to_usec(frames * u->frame_size, &u->source->sample_=
spec);
+
+    return r;
+}
+
+static int suspend(struct userdata *u) {
+    pa_assert(u);
+    pa_assert(u->pcm_handle);
+
+    /* Let's suspend */
+    snd_pcm_close(u->pcm_handle);
+    u->pcm_handle =3D NULL;
+
+    pa_log_debug("Device suspended...");
+    =

+    return 0;
+}
+
+static int unsuspend(struct userdata *u) {
+    pa_sample_spec ss;
+    int err, b;
+    unsigned nfrags;
+    snd_pcm_uframes_t period_size;
+
+    pa_assert(u);
+    pa_assert(!u->pcm_handle);
+
+    pa_log_debug("Trying resume...");
+
+    snd_config_update_free_global();
+    if ((err =3D snd_pcm_open(&u->pcm_handle, u->device_name, SND_PCM_STRE=
AM_CAPTURE, SND_PCM_NONBLOCK)) < 0) {
+        pa_log("Error opening PCM device %s: %s", u->device_name, snd_stre=
rror(err));
+        goto fail;
+    }
+
+    ss =3D u->source->sample_spec;
+    nfrags =3D u->nfragments;
+    period_size =3D u->fragment_size / u->frame_size;
+    b =3D u->use_mmap;
+    =

+    if ((err =3D pa_alsa_set_hw_params(u->pcm_handle, &ss, &nfrags, &perio=
d_size, &b)) < 0) {
+        pa_log("Failed to set hardware parameters: %s", snd_strerror(err));
+        goto fail;
+    }
+
+    if (b !=3D u->use_mmap) {
+        pa_log_warn("Resume failed, couldn't get original access mode.");
+        goto fail;
+    }
+
+    if (!pa_sample_spec_equal(&ss, &u->source->sample_spec)) {
+        pa_log_warn("Resume failed, couldn't restore original sample setti=
ngs.");
+        goto fail;
+    }
+
+    if (nfrags !=3D u->nfragments || period_size*u->frame_size !=3D u->fra=
gment_size) {
+        pa_log_warn("Resume failed, couldn't restore original fragment set=
tings.");
+        goto fail;
+    }
+
+    snd_pcm_start(u->pcm_handle);
+    =

+    /* FIXME: We need to reload the volume somehow */
+                =

+    pa_log_debug("Resumed successfully...");
+
+    return 0;
+
+fail:
+    snd_pcm_close(u->pcm_handle);
+    u->pcm_handle =3D NULL;
+
+    return -1;
+}
+
+static int source_process_msg(pa_msgobject *o, int code, void *data, pa_me=
mchunk *chunk) {
+    struct userdata *u =3D PA_SOURCE(o)->userdata;
+
+    switch (code) {
+
+        case PA_SOURCE_MESSAGE_GET_LATENCY: {
+            pa_usec_t r =3D 0;
+
+            if (u->pcm_handle)
+                r =3D source_get_latency(u);
+
+            *((pa_usec_t*) data) =3D r;
+
+            break;
+        }
+
+        case PA_SOURCE_MESSAGE_SET_STATE:
+
+            switch ((pa_source_state_t) PA_PTR_TO_UINT(data)) {
+                =

+                case PA_SOURCE_SUSPENDED:
+                    pa_assert(PA_SOURCE_OPENED(u->source->thread_info.stat=
e));
+
+                    if (suspend(u) < 0)
+                        return -1;
+                    =

+                    break;
+
+                case PA_SOURCE_IDLE:
+                case PA_SOURCE_RUNNING:
+
+                    if (u->source->thread_info.state =3D=3D PA_SOURCE_SUSP=
ENDED) {
+                        if (unsuspend(u) < 0)
+                            return -1;
+                    }
+                    =

+                    break;
+
+                case PA_SOURCE_DISCONNECTED:
+                    ;
+            }
+            =

+            break;
+    }
+
+    return pa_source_process_msg(o, code, data, chunk);
+}
+
+static int mixer_callback(snd_mixer_elem_t *elem, unsigned int mask) {
+    struct userdata *u =3D snd_mixer_elem_get_callback_private(elem);
+
+    pa_assert(u);
+    pa_assert(u->mixer_handle);
+
+    if (mask =3D=3D SND_CTL_EVENT_MASK_REMOVE)
+        return 0;
+
+    if (mask & SND_CTL_EVENT_MASK_VALUE) {
+        pa_source_get_volume(u->source);
+        pa_source_get_mute(u->source);
+    }
+
+    return 0;
+}
+
+static int source_get_volume_cb(pa_source *s) {
+    struct userdata *u =3D s->userdata;
+    int err;
+    int i;
+
+    pa_assert(u);
+    pa_assert(u->mixer_elem);
+
+    for (i =3D 0; i < s->sample_spec.channels; i++) {
+        long set_vol, vol;
+
+        pa_assert(snd_mixer_selem_has_capture_channel(u->mixer_elem, i));
+
+        if ((err =3D snd_mixer_selem_get_capture_volume(u->mixer_elem, i, =
&vol)) < 0)
+            goto fail;
+
+        set_vol =3D (long) roundf(((float) s->volume.values[i] * (u->hw_vo=
lume_max - u->hw_volume_min)) / PA_VOLUME_NORM) + u->hw_volume_min;
+
+        /* Try to avoid superfluous volume changes */
+        if (set_vol !=3D vol)
+            s->volume.values[i] =3D (pa_volume_t) roundf(((float) (vol - u=
->hw_volume_min) * PA_VOLUME_NORM) / (u->hw_volume_max - u->hw_volume_min));
+    }
+
+    return 0;
+
+fail:
+    pa_log_error("Unable to read volume: %s", snd_strerror(err));
+    =

+    s->get_volume =3D NULL;
+    s->set_volume =3D NULL;
+    return -1;
+}
+
+static int source_set_volume_cb(pa_source *s) {
+    struct userdata *u =3D s->userdata;
+    int err;
+    int i;
+
+    pa_assert(u);
+    pa_assert(u->mixer_elem);
+
+    for (i =3D 0; i < s->sample_spec.channels; i++) {
+        long alsa_vol;
+        pa_volume_t vol;
+
+        pa_assert(snd_mixer_selem_has_capture_channel(u->mixer_elem, i));
+
+        vol =3D s->volume.values[i];
+
+        if (vol > PA_VOLUME_NORM)
+            vol =3D PA_VOLUME_NORM;
+
+        alsa_vol =3D (long) roundf(((float) vol * (u->hw_volume_max - u->h=
w_volume_min)) / PA_VOLUME_NORM) + u->hw_volume_min;
+
+        if ((err =3D snd_mixer_selem_set_capture_volume(u->mixer_elem, i, =
alsa_vol)) < 0)
+            goto fail;
+    }
+
+    return 0;
+
+fail:
+    pa_log_error("Unable to set volume: %s", snd_strerror(err));
+    =

+    s->get_volume =3D NULL;
+    s->set_volume =3D NULL;
+    return -1;
+}
+
+static int source_get_mute_cb(pa_source *s) {
+    struct userdata *u =3D s->userdata;
+    int err, sw;
+
+    pa_assert(u);
+    pa_assert(u->mixer_elem);
+
+    if ((err =3D snd_mixer_selem_get_capture_switch(u->mixer_elem, 0, &sw)=
) < 0) {
+        pa_log_error("Unable to get switch: %s", snd_strerror(err));
+
+        s->get_mute =3D NULL;
+        s->set_mute =3D NULL;
         return -1;
     }
 =

+    s->muted =3D !sw;
+
     return 0;
 }
 =

-
-static int suspend_recovery(struct userdata *u) {
-    int ret;
-    assert(u);
-
-    pa_log_info("*** ALSA-SUSPEND (capture) ***");
-
-    if ((ret =3D snd_pcm_resume(u->pcm_handle)) < 0) {
-        if (ret =3D=3D -EAGAIN)
-            return -1;
-
-        if (ret !=3D -ENOSYS)
-            pa_log("snd_pcm_resume() failed: %s", snd_strerror(-ret));
-        else {
-            if ((ret =3D snd_pcm_prepare(u->pcm_handle)) < 0)
-                pa_log("snd_pcm_prepare() failed: %s", snd_strerror(-ret));
-        }
-
-        if (ret < 0) {
-            clear_up(u);
-            pa_module_unload_request(u->module);
-            return -1;
-        }
-    }
-
-    return ret;
-}
-
-static void do_read(struct userdata *u) {
-    assert(u);
-
-    update_usage(u);
+static int source_set_mute_cb(pa_source *s) {
+    struct userdata *u =3D s->userdata;
+    int err;
+
+    pa_assert(u);
+    pa_assert(u->mixer_elem);
+
+    if ((err =3D snd_mixer_selem_set_capture_switch_all(u->mixer_elem, !s-=
>muted)) < 0) {
+        pa_log_error("Unable to set switch: %s", snd_strerror(err));
+        =

+        s->get_mute =3D NULL;
+        s->set_mute =3D NULL;
+        return -1;
+    }
+
+    return 0;
+}
+
+static void thread_func(void *userdata) {
+    enum {
+        POLLFD_ASYNCQ,
+        POLLFD_ALSA_BASE
+    };
+
+    struct userdata *u =3D userdata;
+    struct pollfd *pollfd =3D NULL;
+    int n_alsa_fds, err;
+    unsigned short revents =3D 0;
+    snd_pcm_status_t *status;
+
+    pa_assert(u);
+    snd_pcm_status_alloca(&status);
+
+    pa_log_debug("Thread starting up");
+
+    if ((n_alsa_fds =3D 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;
+    }
+
+    pollfd =3D pa_xnew0(struct pollfd, POLLFD_ALSA_BASE + n_alsa_fds);
+
+    pollfd[POLLFD_ASYNCQ].fd =3D pa_asyncmsgq_get_fd(u->asyncmsgq);
+    pollfd[POLLFD_ASYNCQ].events =3D POLLIN;
+
+    if ((err =3D snd_pcm_poll_descriptors(u->pcm_handle, pollfd+POLLFD_ALS=
A_BASE, n_alsa_fds)) < 0) {
+        pa_log("snd_pcm_poll_descriptors() failed: %s", snd_strerror(err));
+        goto fail;
+    }
 =

     for (;;) {
-        pa_memchunk post_memchunk;
-        snd_pcm_sframes_t frames;
-        size_t l;
-        void *p;
-
-        if (!u->memchunk.memblock) {
-            u->memchunk.memblock =3D pa_memblock_new(u->source->core->memp=
ool, u->memchunk.length =3D u->fragment_size);
-            u->memchunk.index =3D 0;
-        }
-
-        assert(u->memchunk.memblock);
-        assert(u->memchunk.length);
-        assert(u->memchunk.length % u->frame_size =3D=3D 0);
-
-        p =3D pa_memblock_acquire(u->memchunk.memblock);
-
-        if ((frames =3D snd_pcm_readi(u->pcm_handle, (uint8_t*) p + u->mem=
chunk.index, u->memchunk.length / u->frame_size)) < 0) {
-            pa_memblock_release(u->memchunk.memblock);
-
-            if (frames =3D=3D -EAGAIN)
-                return;
-
-            if (frames =3D=3D -EPIPE) {
-                if (xrun_recovery(u) < 0)
-                    return;
-
+        pa_msgobject *object;
+        int code;
+        void *data;
+        int r;
+        pa_memchunk chunk;
+
+/*         pa_log("loop");     */
+        =

+        /* Check whether there is a message for us to process */
+        if (pa_asyncmsgq_get(u->asyncmsgq, &object, &code, &data, &chunk, =
0) =3D=3D 0) {
+            int ret;
+
+/*             pa_log("processing msg"); */
+
+            if (!object && code =3D=3D PA_MESSAGE_SHUTDOWN) {
+                pa_asyncmsgq_done(u->asyncmsgq, 0);
+                goto finish;
+            }
+
+            ret =3D 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 (PA_SOURCE_OPENED(u->source->thread_info.state) && (revents & P=
OLLIN)) {
+            int work_done =3D 0;
+            pa_assert(u->pcm_handle);
+
+            if (u->use_mmap) {
+
+                if ((work_done =3D mmap_read(u)) < 0)
+                    goto fail;
+
+            } else {
+                ssize_t l;
+
+                snd_pcm_hwsync(u->pcm_handle);
+                if ((err =3D snd_pcm_status(u->pcm_handle, status)) >=3D 0)
+                    l =3D snd_pcm_status_get_avail(status) * u->frame_size;
+                else
+                    l =3D u->fragment_size;
+
+                while (l > 0) {
+                    void *p;
+                    snd_pcm_sframes_t t;
+
+                    pa_assert(l > 0);
+
+                    chunk.memblock =3D pa_memblock_new(u->core->mempool, l=
);
+
+                    p =3D pa_memblock_acquire(chunk.memblock);
+                    t =3D snd_pcm_readi(u->pcm_handle, (uint8_t*) p, l / u=
->frame_size);
+                    pa_memblock_release(chunk.memblock);
+                    =

+/*                     pa_log("wrote %i bytes of %u (%u)", t*u->frame_size=
, u->memchunk.length, l);   */
+                    =

+                    pa_assert(t !=3D 0);
+                    =

+                    if (t < 0) {
+                        pa_memblock_unref(chunk.memblock);
+
+                        if (t =3D=3D -EPIPE)
+                            pa_log_debug("Buffer underrun!");
+                        =

+                        if ((t =3D snd_pcm_recover(u->pcm_handle, t, 1)) =
=3D=3D 0)
+                            continue;
+                        =

+                        if (t =3D=3D -EAGAIN) {
+                            pa_log_debug("EAGAIN");
+                            break;
+                        } else {
+                            pa_log("Failed to read data from DSP: %s", snd=
_strerror(t));
+                            goto fail;
+                        }
+                        =

+                    } else {
+                        =

+                        chunk.index =3D 0;
+                        chunk.length =3D t * u->frame_size;
+
+                        pa_source_post(u->source, &chunk);
+                        pa_memblock_unref(chunk.memblock);
+                        =

+                        l -=3D t * u->frame_size;
+
+                        work_done =3D 1;
+                    }
+                } =

+            }
+
+            revents &=3D ~POLLIN;
+            =

+            if (work_done)
+                continue;
+        }
+
+        /* Hmm, nothing to do. Let's sleep */
+        if (pa_asyncmsgq_before_poll(u->asyncmsgq) < 0)
+            continue;
+
+/*         pa_log("polling for %i", POLLFD_ALSA_BASE + (PA_SOURCE_OPENED(u=
->source->thread_info.state) ? n_alsa_fds : 0));   */
+        r =3D poll(pollfd, POLLFD_ALSA_BASE + (PA_SOURCE_OPENED(u->source-=
>thread_info.state) ? n_alsa_fds : 0), -1);
+        /*pa_log("polling got dsp=3D%i amq=3D%i (%i)", r > 0 ? pollfd[POLL=
FD_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 =3D=3D EINTR) {
+                pollfd[POLLFD_ASYNCQ].revents =3D 0;
+                revents =3D 0;
                 continue;
             }
 =

-            if (frames =3D=3D -ESTRPIPE) {
-                if (suspend_recovery(u) < 0)
-                    return;
-
-                continue;
+            pa_log("poll() failed: %s", pa_cstrerror(errno));
+            goto fail;
+        }
+
+        pa_assert(r > 0);
+
+        if (PA_SOURCE_OPENED(u->source->thread_info.state)) {
+            if ((err =3D snd_pcm_poll_descriptors_revents(u->pcm_handle, p=
ollfd + POLLFD_ALSA_BASE, n_alsa_fds, &revents)) < 0) {
+                pa_log("snd_pcm_poll_descriptors_revents() failed: %s", sn=
d_strerror(err));
+                goto fail;
             }
 =

-            pa_log("snd_pcm_readi() failed: %s", snd_strerror(-frames));
-
-            clear_up(u);
-            pa_module_unload_request(u->module);
-            return;
-        }
-        pa_memblock_release(u->memchunk.memblock);
-
-        l =3D frames * u->frame_size;
-
-        post_memchunk =3D u->memchunk;
-        post_memchunk.length =3D l;
-
-        pa_source_post(u->source, &post_memchunk);
-
-        u->memchunk.index +=3D l;
-        u->memchunk.length -=3D l;
-
-        if (u->memchunk.length =3D=3D 0) {
-            pa_memblock_unref(u->memchunk.memblock);
-            u->memchunk.memblock =3D NULL;
-            u->memchunk.index =3D u->memchunk.length =3D 0;
-        }
-
-        break;
-    }
-}
-
-static void fdl_callback(void *userdata) {
-    struct userdata *u =3D userdata;
-    assert(u);
-
-    if (snd_pcm_state(u->pcm_handle) =3D=3D SND_PCM_STATE_XRUN)
-        if (xrun_recovery(u) < 0)
-            return;
-
-    if (snd_pcm_state(u->pcm_handle) =3D=3D SND_PCM_STATE_SUSPENDED)
-        if (suspend_recovery(u) < 0)
-            return;
-
-    do_read(u);
-}
-
-static int mixer_callback(snd_mixer_elem_t *elem, unsigned int mask) {
-    struct userdata *u =3D snd_mixer_elem_get_callback_private(elem);
-
-    assert(u && u->mixer_handle);
-
-    if (mask =3D=3D SND_CTL_EVENT_MASK_REMOVE)
-        return 0;
-
-    if (mask & SND_CTL_EVENT_MASK_VALUE) {
-        if (u->source->get_hw_volume)
-            u->source->get_hw_volume(u->source);
-        if (u->source->get_hw_mute)
-            u->source->get_hw_mute(u->source);
-
-        pa_subscription_post(u->source->core,
-            PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE,
-            u->source->index);
-    }
-
-    return 0;
-}
-
-static pa_usec_t source_get_latency_cb(pa_source *s) {
-    struct userdata *u =3D s->userdata;
-    snd_pcm_sframes_t frames;
-    assert(s && u && u->source);
-
-    if (snd_pcm_delay(u->pcm_handle, &frames) < 0) {
-        pa_log("failed to get delay");
-        s->get_latency =3D NULL;
-        return 0;
-    }
-
-    return pa_bytes_to_usec(frames * u->frame_size, &s->sample_spec);
-}
-
-static int source_get_hw_volume_cb(pa_source *s) {
-    struct userdata *u =3D s->userdata;
-    long vol;
-    int err;
-    int i;
-
-    assert(u && u->mixer_elem);
-
-    for (i =3D 0;i < s->hw_volume.channels;i++) {
-        long set_vol;
-
-        assert(snd_mixer_selem_has_capture_channel(u->mixer_elem, i));
-
-        if ((err =3D snd_mixer_selem_get_capture_volume(u->mixer_elem, i, =
&vol)) < 0)
-            goto fail;
-
-        set_vol =3D (long) roundf(((float) s->hw_volume.values[i] * (u->hw=
_volume_max - u->hw_volume_min)) / PA_VOLUME_NORM) + u->hw_volume_min;
-
-        /* Try to avoid superfluous volume changes */
-        if (set_vol !=3D vol)
-            s->hw_volume.values[i] =3D (pa_volume_t) roundf(((float) (vol =
- u->hw_volume_min) * PA_VOLUME_NORM) / (u->hw_volume_max - u->hw_volume_mi=
n));
-    }
-
-    return 0;
+/*             pa_log("got alsa event"); */
+        } else
+            revents =3D 0;
+        =

+        pa_assert((pollfd[POLLFD_ASYNCQ].revents & ~POLLIN) =3D=3D 0);
+    }
 =

 fail:
-    pa_log_error("Unable to read volume: %s", snd_strerror(err));
-    s->get_hw_volume =3D NULL;
-    s->set_hw_volume =3D NULL;
-    return -1;
-}
-
-static int source_set_hw_volume_cb(pa_source *s) {
-    struct userdata *u =3D s->userdata;
-    int err;
-    pa_volume_t vol;
-    int i;
-
-    assert(u && u->mixer_elem);
-
-    for (i =3D 0;i < s->hw_volume.channels;i++) {
-        assert(snd_mixer_selem_has_capture_channel(u->mixer_elem, i));
-
-        vol =3D s->hw_volume.values[i];
-
-        if (vol > PA_VOLUME_NORM)
-            vol =3D PA_VOLUME_NORM;
-
-        vol =3D (long) roundf(((float) vol * (u->hw_volume_max - u->hw_vol=
ume_min)) / PA_VOLUME_NORM) + u->hw_volume_min;
-
-        if ((err =3D snd_mixer_selem_set_capture_volume(u->mixer_elem, i, =
vol)) < 0)
-            goto fail;
-    }
-
-    return 0;
-
-fail:
-    pa_log_error("Unable to set volume: %s", snd_strerror(err));
-    s->get_hw_volume =3D NULL;
-    s->set_hw_volume =3D NULL;
-    return -1;
-}
-
-static int source_get_hw_mute_cb(pa_source *s) {
-    struct userdata *u =3D s->userdata;
-    int err, sw;
-
-    assert(u && u->mixer_elem);
-
-    err =3D snd_mixer_selem_get_capture_switch(u->mixer_elem, 0, &sw);
-    if (err) {
-        pa_log_error("Unable to get switch: %s", snd_strerror(err));
-        s->get_hw_mute =3D NULL;
-        s->set_hw_mute =3D NULL;
-        return -1;
-    }
-
-    s->hw_muted =3D !sw;
-
-    return 0;
-}
-
-static int source_set_hw_mute_cb(pa_source *s) {
-    struct userdata *u =3D s->userdata;
-    int err;
-
-    assert(u && u->mixer_elem);
-
-    err =3D snd_mixer_selem_set_capture_switch_all(u->mixer_elem, !s->hw_m=
uted);
-    if (err) {
-        pa_log_error("Unable to set switch: %s", snd_strerror(err));
-        s->get_hw_mute =3D NULL;
-        s->set_hw_mute =3D NULL;
-        return -1;
-    }
-
-    return 0;
-}
+    /* We have to continue processing messages until we receive the
+     * SHUTDOWN message */
+    pa_asyncmsgq_post(u->core->asyncmsgq, PA_MSGOBJECT(u->core), PA_CORE_M=
ESSAGE_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 =3D NULL;
     int ret =3D -1;
     struct userdata *u =3D NULL;
     const char *dev;
     pa_sample_spec ss;
     pa_channel_map map;
-    unsigned periods, fragsize;
+    unsigned nfrags, frag_size;
     snd_pcm_uframes_t period_size;
     size_t frame_size;
     snd_pcm_info_t *pcm_info =3D NULL;
@@ -407,33 +661,47 @@
     const char *name;
     char *name_buf =3D NULL;
     int namereg_fail;
-
+    int use_mmap =3D 1, b;
+
+    pa_assert(c);
+    pa_assert(m);
+    =

     if (!(ma =3D pa_modargs_new(m->argument, valid_modargs))) {
-        pa_log("failed to parse module arguments");
+        pa_log("Failed to parse module arguments");
         goto fail;
     }
 =

     ss =3D c->default_sample_spec;
     if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANN=
EL_MAP_ALSA) < 0) {
-        pa_log("failed to parse sample specification");
+        pa_log("Failed to parse sample specification");
         goto fail;
     }
 =

     frame_size =3D pa_frame_size(&ss);
 =

     /* Fix latency to 100ms */
-    periods =3D 12;
-    fragsize =3D pa_bytes_per_second(&ss)/128;
-
-    if (pa_modargs_get_value_u32(ma, "fragments", &periods) < 0 || pa_moda=
rgs_get_value_u32(ma, "fragment_size", &fragsize) < 0) {
-        pa_log("failed to parse buffer metrics");
-        goto fail;
-    }
-    period_size =3D fragsize/frame_size;
-
+    nfrags =3D DEFAULT_NFRAGS;
+    frag_size =3D pa_usec_to_bytes(DEFAULT_FRAGSIZE_MSEC*1000, &ss);
+    if (frag_size <=3D 0)
+        frag_size =3D frame_size;
+
+    if (pa_modargs_get_value_u32(ma, "fragments", &nfrags) < 0 || pa_modar=
gs_get_value_u32(ma, "fragment_size", &frag_size) < 0) {
+        pa_log("Failed to parse buffer metrics");
+        goto fail;
+    }
+    period_size =3D 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 =3D pa_xnew0(struct userdata, 1);
+    u->core =3D c;
+    u->module =3D m;
     m->userdata =3D u;
-    u->module =3D m;
+    u->use_mmap =3D use_mmap;
+    pa_assert_se(u->asyncmsgq =3D pa_asyncmsgq_new(0));
 =

     snd_config_update_free_global();
     if ((err =3D snd_pcm_open(&u->pcm_handle, dev =3D pa_modargs_get_value=
(ma, "device", DEFAULT_DEVICE), SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK)) =
< 0) {
@@ -441,30 +709,44 @@
         goto fail;
     }
 =

+    u->device_name =3D pa_xstrdup(dev);
+
     if ((err =3D snd_pcm_info_malloc(&pcm_info)) < 0 ||
         (err =3D snd_pcm_info(u->pcm_handle, pcm_info)) < 0) {
         pa_log("Error fetching PCM info: %s", snd_strerror(err));
         goto fail;
     }
 =

-    if ((err =3D pa_alsa_set_hw_params(u->pcm_handle, &ss, &periods, &peri=
od_size)) < 0) {
+    b =3D use_mmap;
+    if ((err =3D pa_alsa_set_hw_params(u->pcm_handle, &ss, &nfrags, &perio=
d_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 r=
ead/write mode.");
+        u->use_mmap =3D use_mmap =3D b;
+    }
+
+    if (u->use_mmap)
+        pa_log_info("Successfully enabled mmap() mode.");
+    =

+    /* ALSA might tweak the sample spec, so recalculate the frame size */
+    frame_size =3D pa_frame_size(&ss);
 =

     if (ss.channels !=3D map.channels)
         /* Seems ALSA didn't like the channel number, so let's fix the cha=
nnel map */
         pa_channel_map_init_auto(&map, ss.channels, PA_CHANNEL_MAP_ALSA);
 =

-    if ((err =3D snd_mixer_open(&u->mixer_handle, 0)) < 0) {
+    if ((err =3D snd_mixer_open(&u->mixer_handle, 0)) < 0)
         pa_log("Error opening mixer: %s", snd_strerror(err));
-        goto fail;
-    }
-
-    if ((pa_alsa_prepare_mixer(u->mixer_handle, dev) < 0) ||
-        !(u->mixer_elem =3D pa_alsa_find_elem(u->mixer_handle, "Capture", =
"Mic"))) {
-        snd_mixer_close(u->mixer_handle);
-        u->mixer_handle =3D NULL;
+    else {
+        =

+        if ((pa_alsa_prepare_mixer(u->mixer_handle, dev) < 0) ||
+            !(u->mixer_elem =3D pa_alsa_find_elem(u->mixer_handle, "Captur=
e", "Mic"))) {
+            snd_mixer_close(u->mixer_handle);
+            u->mixer_handle =3D NULL;
+        }
     }
 =

     if ((name =3D pa_modargs_get_value(ma, "source_name", NULL)))
@@ -474,16 +756,37 @@
         namereg_fail =3D 0;
     }
 =

-    if (!(u->source =3D pa_source_new(c, __FILE__, name, namereg_fail, &ss=
, &map))) {
+    u->source =3D pa_source_new(c, __FILE__, name, namereg_fail, &ss, &map=
);
+    pa_xfree(name_buf);
+    =

+    if (!u->source) {
         pa_log("Failed to create source object");
         goto fail;
     }
 =

+    u->source->parent.process_msg =3D source_process_msg;
+    u->source->userdata =3D u;
+
+    pa_source_set_module(u->source, m);
+    pa_source_set_asyncmsgq(u->source, u->asyncmsgq);
+    pa_source_set_description(u->source, t =3D pa_sprintf_malloc(
+                                      "ALSA PCM on %s (%s)",
+                                      dev,
+                                      snd_pcm_info_get_name(pcm_info)));
+    pa_xfree(t);
+
     u->source->is_hardware =3D 1;
-    u->source->userdata =3D u;
-    u->source->get_latency =3D source_get_latency_cb;
+
+    u->frame_size =3D frame_size;
+    u->fragment_size =3D frag_size =3D period_size * frame_size;
+    u->nfragments =3D nfrags;
+    u->hwbuf_size =3D u->fragment_size * nfrags;
+
+    pa_log_info("Using %u fragments of size %lu bytes.", nfrags, (long uns=
igned) u->fragment_size);
+
     if (u->mixer_handle) {
         assert(u->mixer_elem);
+        =

         if (snd_mixer_selem_has_capture_volume(u->mixer_elem)) {
             int i;
 =

@@ -493,64 +796,48 @@
             }
 =

             if (i =3D=3D ss.channels) {
-                u->source->get_hw_volume =3D source_get_hw_volume_cb;
-                u->source->set_hw_volume =3D source_set_hw_volume_cb;
-                snd_mixer_selem_get_capture_volume_range(
-                    u->mixer_elem, &u->hw_volume_min, &u->hw_volume_max);
+                u->source->get_volume =3D source_get_volume_cb;
+                u->source->set_volume =3D source_set_volume_cb;
+                snd_mixer_selem_get_capture_volume_range(u->mixer_elem, &u=
->hw_volume_min, &u->hw_volume_max);
             }
         }
+        =

         if (snd_mixer_selem_has_capture_switch(u->mixer_elem)) {
-            u->source->get_hw_mute =3D source_get_hw_mute_cb;
-            u->source->set_hw_mute =3D source_set_hw_mute_cb;
-        }
-    }
-    pa_source_set_owner(u->source, m);
-    pa_source_set_description(u->source, t =3D pa_sprintf_malloc("ALSA PCM=
 on %s (%s)", dev, snd_pcm_info_get_name(pcm_info)));
-    pa_xfree(t);
-
-    u->pcm_fdl =3D pa_alsa_fdlist_new();
-    assert(u->pcm_fdl);
-    if (pa_alsa_fdlist_init_pcm(u->pcm_fdl, u->pcm_handle, c->mainloop, fd=
l_callback, u) < 0) {
-        pa_log("failed to initialise file descriptor monitoring");
-        goto fail;
-    }
-
-    if (u->mixer_handle) {
+            u->source->get_mute =3D source_get_mute_cb;
+            u->source->set_mute =3D source_set_mute_cb;
+        }
+
         u->mixer_fdl =3D pa_alsa_fdlist_new();
-        assert(u->mixer_fdl);
-        if (pa_alsa_fdlist_init_mixer(u->mixer_fdl, u->mixer_handle, c->ma=
inloop) < 0) {
+        =

+        if (pa_alsa_fdlist_set_mixer(u->mixer_fdl, u->mixer_handle, c->mai=
nloop) < 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 =3D NULL;
 =

-    u->frame_size =3D frame_size;
-    u->fragment_size =3D period_size * frame_size;
-
-    pa_log_info("using %u fragments of size %lu bytes.", periods, (long un=
signed) u->fragment_size);
-
-    u->memchunk.memblock =3D NULL;
-    u->memchunk.index =3D u->memchunk.length =3D 0;
+    if (!(u->thread =3D pa_thread_new(thread_func, u))) {
+        pa_log("Failed to create thread.");
+        goto fail;
+    }
+    /* Get initial mixer settings */
+    if (u->source->get_volume)
+        u->source->get_volume(u->source);
+    if (u->source->get_mute)
+        u->source->get_mute(u->source);
 =

     snd_pcm_start(u->pcm_handle);
-
+    =

     ret =3D 0;
 =

-    /* Get initial mixer settings */
-    if (u->source->get_hw_volume)
-        u->source->get_hw_volume(u->source);
-    if (u->source->get_hw_mute)
-        u->source->get_hw_mute(u->source);
-
 finish:
-    pa_xfree(name_buf);
 =

     if (ma)
-         pa_modargs_free(ma);
-
+        pa_modargs_free(ma);
+    =

     if (pcm_info)
         snd_pcm_info_free(pcm_info);
 =

@@ -566,16 +853,41 @@
 =

 void pa__done(pa_core *c, pa_module*m) {
     struct userdata *u;
-    assert(c && m);
+    =

+    pa_assert(c);
+    pa_assert(m);
 =

     if (!(u =3D m->userdata))
         return;
 =

-    clear_up(u);
-
-    if (u->memchunk.memblock)
-        pa_memblock_unref(u->memchunk.memblock);
-
+    if (u->source)
+        pa_source_disconnect(u->source);
+
+    if (u->thread) {
+        pa_asyncmsgq_send(u->asyncmsgq, NULL, PA_MESSAGE_SHUTDOWN, NULL, N=
ULL);
+        pa_thread_free(u->thread);
+    }
+
+    if (u->asyncmsgq)
+        pa_asyncmsgq_free(u->asyncmsgq);
+
+    if (u->source)
+        pa_source_unref(u->source);
+
+    if (u->mixer_fdl)
+        pa_alsa_fdlist_free(u->mixer_fdl);
+
+    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);
     pa_xfree(u);
-}
-
+
+    snd_config_update_free_global();
+}
+




More information about the pulseaudio-commits mailing list