[pulseaudio-commits] r1491 - in /branches/lennart/src: Makefile.am modules/module-oss.c

svnmailer-noreply at 0pointer.de svnmailer-noreply at 0pointer.de
Sat Jun 23 13:05:21 PDT 2007


Author: lennart
Date: Sat Jun 23 22:05:20 2007
New Revision: 1491

URL: http://0pointer.de/cgi-bin/viewcvs.cgi?rev=3D1491&root=3Dpulseaudio&vi=
ew=3Drev
Log:
Update OSS driver for new lock-free core

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

Modified: branches/lennart/src/Makefile.am
URL: http://0pointer.de/cgi-bin/viewcvs.cgi/branches/lennart/src/Makefile.a=
m?rev=3D1491&root=3Dpulseaudio&r1=3D1490&r2=3D1491&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 Sat Jun 23 22:05:20 2007
@@ -927,12 +927,12 @@
 		module-x11-publish.la
 endif
 =

-#if HAVE_OSS
-#modlibexec_LTLIBRARIES +=3D \
-#		liboss-util.la \
-#		module-oss.la \
+if HAVE_OSS
+modlibexec_LTLIBRARIES +=3D \
+		liboss-util.la \
+		module-oss.la
 #		module-oss-mmap.la
-#endif
+endif
 =

 #if HAVE_ALSA
 #modlibexec_LTLIBRARIES +=3D \
@@ -1176,13 +1176,13 @@
 =

 # OSS
 =

-#liboss_util_la_SOURCES =3D modules/oss-util.c modules/oss-util.h
-#liboss_util_la_LDFLAGS =3D -avoid-version
-#liboss_util_la_LIBADD =3D libpulsecore.la
-
-#module_oss_la_SOURCES =3D modules/module-oss.c
-#module_oss_la_LDFLAGS =3D -module -avoid-version
-#module_oss_la_LIBADD =3D $(AM_LIBADD) libiochannel.la liboss-util.la
+liboss_util_la_SOURCES =3D modules/oss-util.c modules/oss-util.h
+liboss_util_la_LDFLAGS =3D -avoid-version
+liboss_util_la_LIBADD =3D libpulsecore.la
+
+module_oss_la_SOURCES =3D modules/module-oss.c
+module_oss_la_LDFLAGS =3D -module -avoid-version
+module_oss_la_LIBADD =3D $(AM_LIBADD) libiochannel.la liboss-util.la
 =

 #module_oss_mmap_la_SOURCES =3D modules/module-oss-mmap.c
 #module_oss_mmap_la_LDFLAGS =3D -module -avoid-version

Modified: branches/lennart/src/modules/module-oss.c
URL: http://0pointer.de/cgi-bin/viewcvs.cgi/branches/lennart/src/modules/mo=
dule-oss.c?rev=3D1491&root=3Dpulseaudio&r1=3D1490&r2=3D1491&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/modules/module-oss.c (original)
+++ branches/lennart/src/modules/module-oss.c Sat Jun 23 22:05:20 2007
@@ -22,12 +22,31 @@
   USA.
 ***/
 =

+/* General power management rules:
+ *
+ *   When SUSPENDED we close the audio device.
+ *
+ *   We make no difference between IDLE and RUNNING in our handling.
+ *
+ *   As long as we are in RUNNING/IDLE state we will *always* write data to
+ *   the device. If none is avilable from the inputs, we write silence
+ *   instead.
+ *
+ *   If power should be saved on IDLE this should be implemented in a
+ *   special suspend-on-idle module that will put us into SUSPEND mode
+ *   as soon and we're idle for too long.
+ *
+ */
+
+/* TODO: handle restoring of volume after suspend */
+
 #ifdef HAVE_CONFIG_H
 #include <config.h>
 #endif
 =

 #include <sys/soundcard.h>
 #include <sys/ioctl.h>
+#include <sys/poll.h>
 #include <stdlib.h>
 #include <sys/stat.h>
 #include <stdio.h>
@@ -42,7 +61,7 @@
 #include <pulse/util.h>
 =

 #include <pulsecore/core-error.h>
-#include <pulsecore/iochannel.h>
+#include <pulsecore/thread.h>
 #include <pulsecore/sink.h>
 #include <pulsecore/source.h>
 #include <pulsecore/module.h>
@@ -70,19 +89,33 @@
         "fragment_size=3D<fragment size> "
         "channel_map=3D<channel map>")
 =

+#define DEFAULT_DEVICE "/dev/dsp"
+
 struct userdata {
+    pa_core *core;
+    pa_module *module;
     pa_sink *sink;
     pa_source *source;
-    pa_iochannel *io;
-    pa_core *core;
-
-    pa_memchunk memchunk, silence;
+    pa_thread *thread;
+    pa_asyncmsgq *asyncmsgq;
+
+    char *device_name;
+    =

+    pa_memchunk memchunk;
 =

     uint32_t in_fragment_size, out_fragment_size, sample_size;
     int use_getospace, use_getispace;
+    int use_getodelay;
+
+    int use_pcm_volume;
+    int use_input_volume;
+
+    int sink_suspended, source_suspended;
 =

     int fd;
-    pa_module *module;
+    int mode;
+
+    int nfrags, frag_size;
 };
 =

 static const char* const valid_modargs[] =3D {
@@ -100,264 +133,500 @@
     NULL
 };
 =

-#define DEFAULT_DEVICE "/dev/dsp"
-
-static void update_usage(struct userdata *u) {
-   pa_module_set_used(u->module,
-                      (u->sink ? pa_sink_used_by(u->sink) : 0) +
-                      (u->source ? pa_source_used_by(u->source) : 0));
-}
-
-static void clear_up(struct userdata *u) {
-    assert(u);
-
-    if (u->sink) {
-        pa_sink_disconnect(u->sink);
-        pa_sink_unref(u->sink);
-        u->sink =3D NULL;
-    }
-
-    if (u->source) {
-        pa_source_disconnect(u->source);
-        pa_source_unref(u->source);
-        u->source =3D NULL;
-    }
-
-    if (u->io) {
-        pa_iochannel_free(u->io);
-        u->io =3D NULL;
-    }
-}
-
-static void do_write(struct userdata *u) {
-    pa_memchunk *memchunk;
-    ssize_t r;
-    size_t l;
-    int loop =3D 0;
-
-    assert(u);
-
-    if (!u->sink || !pa_iochannel_is_writable(u->io))
-        return;
-
-    update_usage(u);
-
-    l =3D u->out_fragment_size;
-
-    if (u->use_getospace) {
-        audio_buf_info info;
-
-        if (ioctl(u->fd, SNDCTL_DSP_GETOSPACE, &info) < 0)
-            u->use_getospace =3D 0;
-        else {
-            if (info.bytes/l > 0) {
-                l =3D (info.bytes/l)*l;
-                loop =3D 1;
-            }
-        }
-    }
-
-    do {
-        void *p;
-        memchunk =3D &u->memchunk;
-
-        if (!memchunk->length)
-            if (pa_sink_render(u->sink, l, memchunk) < 0)
-                memchunk =3D &u->silence;
-
-        assert(memchunk->memblock);
-        assert(memchunk->length);
-
-        p =3D pa_memblock_acquire(memchunk->memblock);
-        if ((r =3D pa_iochannel_write(u->io, (uint8_t*) p + memchunk->inde=
x, memchunk->length)) < 0) {
-            pa_memblock_release(memchunk->memblock);
-
-            if (errno !=3D EAGAIN) {
-                pa_log("write() failed: %s", pa_cstrerror(errno));
-
-                clear_up(u);
-                pa_module_unload_request(u->module);
-            }
-
-            break;
-        }
-
-        pa_memblock_release(memchunk->memblock);
-
-        if (memchunk =3D=3D &u->silence)
-            assert(r % u->sample_size =3D=3D 0);
-        else {
-            u->memchunk.index +=3D r;
-            u->memchunk.length -=3D r;
-
-            if (u->memchunk.length <=3D 0) {
-                pa_memblock_unref(u->memchunk.memblock);
-                u->memchunk.memblock =3D NULL;
-            }
-        }
-
-        l =3D l > (size_t) r ? l - r : 0;
-    } while (loop && l > 0);
-}
-
-static void do_read(struct userdata *u) {
-    pa_memchunk memchunk;
-    ssize_t r;
-    size_t l;
-    int loop =3D 0;
-    assert(u);
-
-    if (!u->source || !pa_iochannel_is_readable(u->io) || !pa_idxset_size(=
u->source->outputs))
-        return;
-
-    update_usage(u);
-
-    l =3D u->in_fragment_size;
-
-    if (u->use_getispace) {
-        audio_buf_info info;
-
-        if (ioctl(u->fd, SNDCTL_DSP_GETISPACE, &info) < 0)
-            u->use_getispace =3D 0;
-        else {
-            if (info.bytes/l > 0) {
-                l =3D (info.bytes/l)*l;
-                loop =3D 1;
-            }
-        }
-    }
-
-    do {
-        void *p;
-        memchunk.memblock =3D pa_memblock_new(u->core->mempool, l);
-
-        p =3D pa_memblock_acquire(memchunk.memblock);
-
-        if ((r =3D pa_iochannel_read(u->io, p, pa_memblock_get_length(memc=
hunk.memblock))) < 0) {
-            pa_memblock_release(memchunk.memblock);
-            pa_memblock_unref(memchunk.memblock);
-
-            if (errno !=3D EAGAIN) {
-                pa_log("read() failed: %s", pa_cstrerror(errno));
-
-                clear_up(u);
-                pa_module_unload_request(u->module);
-            }
-
-            break;
-        }
-
-        pa_memblock_release(memchunk.memblock);
-
-        assert(r <=3D (ssize_t) pa_memblock_get_length(memchunk.memblock));
-        memchunk.length =3D r;
-        memchunk.index =3D 0;
-
-        pa_source_post(u->source, &memchunk);
-        pa_memblock_unref(memchunk.memblock);
-
-        l =3D l > (size_t) r ? l - r : 0;
-    } while (loop && l > 0);
-}
-
-static void io_callback(PA_GCC_UNUSED pa_iochannel *io, void*userdata) {
-    struct userdata *u =3D userdata;
-    assert(u);
-    do_write(u);
-    do_read(u);
-}
-
-static void source_notify_cb(pa_source *s) {
-    struct userdata *u =3D s->userdata;
-    assert(u);
-    do_read(u);
-}
-
-static pa_usec_t sink_get_latency_cb(pa_sink *s) {
-    pa_usec_t r =3D 0;
-    int arg;
-    struct userdata *u =3D s->userdata;
-    assert(s && u && u->sink);
-
-    if (ioctl(u->fd, SNDCTL_DSP_GETODELAY, &arg) < 0) {
-        pa_log_info("device doesn't support SNDCTL_DSP_GETODELAY: %s", pa_=
cstrerror(errno));
-        s->get_latency =3D NULL;
-        return 0;
-    }
-
-    r +=3D pa_bytes_to_usec(arg, &s->sample_spec);
-
-    if (u->memchunk.memblock)
-        r +=3D pa_bytes_to_usec(u->memchunk.length, &s->sample_spec);
-
-    return r;
-}
-
-static pa_usec_t source_get_latency_cb(pa_source *s) {
-    struct userdata *u =3D s->userdata;
-    audio_buf_info info;
-    assert(s && u && u->source);
-
-    if (!u->use_getispace)
-        return 0;
-
-    if (ioctl(u->fd, SNDCTL_DSP_GETISPACE, &info) < 0) {
-        u->use_getispace =3D 0;
-        return 0;
-    }
-
-    if (info.bytes <=3D 0)
-        return 0;
-
-    return pa_bytes_to_usec(info.bytes, &s->sample_spec);
-}
-
-static int sink_get_hw_volume(pa_sink *s) {
-    struct userdata *u =3D s->userdata;
-
-    if (pa_oss_get_pcm_volume(u->fd, &s->sample_spec, &s->hw_volume) < 0) {
-        pa_log_info("device doesn't support reading mixer settings: %s", p=
a_cstrerror(errno));
-        s->get_hw_volume =3D NULL;
-        return -1;
-    }
-
+static int suspend(struct userdata *u) {
+    pa_assert(u);
+    pa_assert(u->fd >=3D 0);
+
+    /* Let's suspend */
+    ioctl(u->fd, SNDCTL_DSP_SYNC);
+    close(u->fd);
+    u->fd =3D -1;
+    =

     return 0;
 }
 =

-static int sink_set_hw_volume(pa_sink *s) {
-    struct userdata *u =3D s->userdata;
-
-    if (pa_oss_set_pcm_volume(u->fd, &s->sample_spec, &s->hw_volume) < 0) {
-        pa_log_info("device doesn't support writing mixer settings: %s", p=
a_cstrerror(errno));
-        s->set_hw_volume =3D NULL;
+static int unsuspend(struct userdata *u) {
+    int m;
+    pa_sample_spec ss, *ss_original;
+    int frag_size, in_frag_size, out_frag_size;
+    struct audio_buf_info info;
+
+    pa_assert(u);
+    pa_assert(u->fd < 0);
+
+    m =3D u->mode;
+
+    pa_log_debug("Trying resume...");
+
+    if ((u->fd =3D pa_oss_open(u->device_name, &m, NULL)) < 0) {
+        pa_log_warn("Resume failed, device busy (%s)", pa_cstrerror(errno)=
);
         return -1;
+
+    if (m !=3D u->mode)
+        pa_log_warn("Resume failed, couldn't open device with original acc=
ess mode.");
+        goto fail;
+    }
+
+    if (u->nfrags >=3D 2 && u->frag_size >=3D 1)
+        if (pa_oss_set_fragments(u->fd, u->nfrags, u->frag_size) < 0) {
+            pa_log_warn("Resume failed, couldn't set original fragment set=
tings.");
+            goto fail;
+        }
+
+    ss =3D *(ss_original =3D u->sink ? &u->sink->sample_spec : &u->source-=
>sample_spec);
+    if (pa_oss_auto_format(u->fd, &ss) < 0 || !pa_sample_spec_equal(&ss, s=
s_original)) {
+        pa_log_warn("Resume failed, couldn't set original sample format se=
ttings.");
+        goto fail;
+    }
+
+    if (ioctl(u->fd, SNDCTL_DSP_GETBLKSIZE, &frag_size) < 0) {
+        pa_log("SNDCTL_DSP_GETBLKSIZE: %s", pa_cstrerror(errno));
+        goto fail;
+    }
+
+    in_frag_size =3D out_frag_size =3D frag_size;
+
+    if (ioctl(u->fd, SNDCTL_DSP_GETISPACE, &info) >=3D 0)
+        in_frag_size =3D info.fragsize;
+
+    if (ioctl(u->fd, SNDCTL_DSP_GETOSPACE, &info) >=3D 0)
+        out_frag_size =3D info.fragsize;
+
+    if ((u->source && in_frag_size !=3D (int) u->in_fragment_size) || (u->=
sink && out_frag_size !=3D (int) u->out_fragment_size)) {
+        pa_log_warn("Resume failed, fragment settings don't match.");
+        goto fail;
+    }
+
+    /*
+     * Some crappy drivers do not start the recording until we read someth=
ing.
+     * Without this snippet, poll will never register the fd as ready.
+     */
+    if (u->source) {
+        uint8_t *buf =3D pa_xnew(uint8_t, u->sample_size);
+        pa_read(u->fd, buf, u->sample_size, NULL);
+        pa_xfree(buf);
     }
 =

     return 0;
+
+fail:
+    close(u->fd);
+    u->fd =3D -1;
+    return -1;
 }
 =

-static int source_get_hw_volume(pa_source *s) {
-    struct userdata *u =3D s->userdata;
-
-    if (pa_oss_get_input_volume(u->fd, &s->sample_spec, &s->hw_volume) < 0=
) {
-        pa_log_info("device doesn't support reading mixer settings: %s", p=
a_cstrerror(errno));
-        s->get_hw_volume =3D NULL;
-        return -1;
-    }
-
-    return 0;
+static int sink_process_msg(pa_msgobject *o, int code, void *data, pa_memc=
hunk *chunk) {
+    struct userdata *u =3D PA_SINK(o)->userdata;
+
+    switch (code) {
+
+        case PA_SINK_MESSAGE_GET_LATENCY: {
+            pa_usec_t r =3D 0;
+
+            if (u->fd >=3D 0) {
+                if (u->use_getodelay) {
+                    int arg;
+                    =

+                    if (ioctl(u->fd, SNDCTL_DSP_GETODELAY, &arg) < 0) {
+                        pa_log_info("Device doesn't support SNDCTL_DSP_GET=
ODELAY: %s", pa_cstrerror(errno));
+                        u->use_getodelay =3D 0;
+                    } else
+                        r =3D pa_bytes_to_usec(arg, &u->sink->sample_spec);
+                    =

+                }
+                =

+                if (!u->use_getodelay && u->use_getospace) {
+                    struct audio_buf_info info;
+                    =

+                    if (ioctl(u->fd, SNDCTL_DSP_GETOSPACE, &info) < 0) {
+                        pa_log_info("Device doesn't support SNDCTL_DSP_GET=
OSPACE: %s", pa_cstrerror(errno));
+                        u->use_getospace =3D 0;
+                    } else
+                        r =3D pa_bytes_to_usec(info.bytes, &u->sink->sampl=
e_spec);
+                }
+            }
+
+            if (u->memchunk.memblock)
+                r +=3D pa_bytes_to_usec(u->memchunk.length, &u->sink->samp=
le_spec);
+
+            *((pa_usec_t*) data) =3D r;
+
+            break;
+        }
+
+        case PA_SINK_MESSAGE_SET_STATE:
+
+            if (PA_PTR_TO_UINT(data) !=3D PA_SINK_RUNNING && u->fd >=3D 0)
+                ioctl(u->fd, SNDCTL_DSP_POST);
+
+            if (PA_PTR_TO_UINT(data) =3D=3D PA_SINK_SUSPENDED) {
+                pa_assert(u->sink->thread_info.state !=3D PA_SINK_SUSPENDE=
D);
+
+                if (u->source_suspended)
+                    if (suspend(u) < 0)
+                        return -1;
+
+                u->sink_suspended =3D 1;
+            }
+
+            if (u->sink->thread_info.state =3D=3D PA_SINK_SUSPENDED) {
+                pa_assert(PA_PTR_TO_UINT(data) !=3D PA_SINK_SUSPENDED);
+
+                if (u->source_suspended)
+                    if (unsuspend(u) < 0)
+                        return -1;
+
+                u->sink_suspended =3D 0;
+            }
+            =

+            break;
+
+        case PA_SINK_MESSAGE_SET_VOLUME:
+
+            if (u->use_pcm_volume && u->fd >=3D 0) {
+
+                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 sett=
ings: %s", pa_cstrerror(errno));
+                    u->use_pcm_volume =3D 0;
+                } else
+                    return 0;
+            }
+
+            break;
+
+        case PA_SINK_MESSAGE_GET_VOLUME:
+
+            if (u->use_pcm_volume && u->fd >=3D 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 sett=
ings: %s", pa_cstrerror(errno));
+                    u->use_pcm_volume =3D 0;
+                } else
+                    return 0;
+            }
+
+            break;
+    }
+
+    return pa_sink_process_msg(o, code, data, chunk);
 }
 =

-static int source_set_hw_volume(pa_source *s) {
-    struct userdata *u =3D s->userdata;
-
-    if (pa_oss_set_input_volume(u->fd, &s->sample_spec, &s->hw_volume) < 0=
) {
-        pa_log_info("device doesn't support writing mixer settings: %s", p=
a_cstrerror(errno));
-        s->set_hw_volume =3D NULL;
-        return -1;
-    }
-
-    return 0;
+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->use_getispace && u->fd >=3D 0) {
+                struct audio_buf_info info;
+
+                if (ioctl(u->fd, SNDCTL_DSP_GETISPACE, &info) < 0) {
+                    pa_log_info("Device doesn't support SNDCTL_DSP_GETISPA=
CE: %s", pa_cstrerror(errno));
+                    u->use_getispace =3D 0;
+                } else
+                    r =3D pa_bytes_to_usec(info.bytes, &u->sink->sample_sp=
ec);
+            }
+
+            *((pa_usec_t*) data) =3D r;
+            break;
+        }
+
+        case PA_SOURCE_MESSAGE_SET_STATE:
+
+            if (PA_PTR_TO_UINT(data) =3D=3D PA_SOURCE_SUSPENDED) {
+                pa_assert(u->source->thread_info.state !=3D PA_SOURCE_SUSP=
ENDED);
+
+                if (u->sink_suspended)
+                    if (suspend(u) < 0)
+                        return -1;
+
+                u->source_suspended =3D 1;
+            }
+
+            if (u->source->thread_info.state =3D=3D PA_SOURCE_SUSPENDED) {
+                pa_assert(PA_PTR_TO_UINT(data) !=3D PA_SOURCE_SUSPENDED);
+
+                if (u->sink_suspended)
+                    if (unsuspend(u) < 0)
+                        return -1;
+
+                u->source_suspended =3D 0;
+            }
+
+            break;
+
+        case PA_SOURCE_MESSAGE_SET_VOLUME:
+
+            if (u->use_input_volume && u->fd >=3D 0) {
+
+                if (pa_oss_set_input_volume(u->fd, &u->source->sample_spec=
, ((pa_cvolume*) data)) < 0) {
+                    pa_log_info("Device doesn't support setting mixer sett=
ings: %s", pa_cstrerror(errno));
+                    u->use_input_volume =3D 0;
+                } else
+                    return 0;
+            }
+
+            break;
+
+        case PA_SOURCE_MESSAGE_GET_VOLUME:
+
+            if (u->use_input_volume && u->fd >=3D 0) {
+
+                if (pa_oss_get_input_volume(u->fd, &u->source->sample_spec=
, ((pa_cvolume*) data)) < 0) {
+                    pa_log_info("Device doesn't support reading mixer sett=
ings: %s", pa_cstrerror(errno));
+                    u->use_input_volume =3D 0;
+                } else
+                    return 0;
+            }
+
+            break;
+    }
+
+    return pa_source_process_msg(o, code, data, chunk);
+}
+
+static void thread_func(void *userdata) {
+    enum {
+        POLLFD_ASYNCQ,
+        POLLFD_DSP,
+        POLLFD_MAX,
+    };
+
+    struct userdata *u =3D userdata;
+    struct pollfd pollfd[POLLFD_MAX];
+    int write_type =3D 0, read_type =3D 0;
+
+    pa_assert(u);
+
+    pa_log_debug("Thread starting up");
+
+    /*
+     * Some crappy drivers do not start the recording until we read someth=
ing.
+     * Without this snippet, poll will never register the fd as ready.
+     */
+    if (u->source) {
+        uint8_t *buf =3D pa_xnew(uint8_t, u->sample_size);
+        pa_read(u->fd, buf, u->sample_size, &read_type);
+        pa_xfree(buf);
+    }
+
+    memset(&pollfd, 0, sizeof(pollfd));
+
+    pollfd[POLLFD_ASYNCQ].fd =3D pa_asyncmsgq_get_fd(u->asyncmsgq);
+    pollfd[POLLFD_ASYNCQ].events =3D POLLIN;
+    pollfd[POLLFD_DSP].fd =3D u->fd;
+
+    for (;;) {
+        pa_msgobject *object;
+        int code;
+        void *data;
+        pa_memchunk chunk;
+        int r;
+
+        /* 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;
+
+            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;
+        }
+
+        /* Render some data and write it to the dsp */
+
+        if (u->sink && u->sink->thread_info.state =3D=3D PA_SINK_RUNNING &=
& (pollfd[POLLFD_DSP].revents & POLLOUT)) {
+            ssize_t l;
+            void *p;
+            int loop =3D 0;
+
+            l =3D u->out_fragment_size;
+
+            if (u->use_getospace) {
+                audio_buf_info info;
+
+                if (ioctl(u->fd, SNDCTL_DSP_GETOSPACE, &info) < 0) {
+                    pa_log_info("Device doesn't support SNDCTL_DSP_GETOSPA=
CE: %s", pa_cstrerror(errno));
+                    u->use_getospace =3D 0;
+                } else {
+                    if (info.bytes >=3D l) {
+                        l =3D (info.bytes/l)*l;
+                        loop =3D 1;
+                    }
+                }
+            }
+
+            do {
+                ssize_t t;
+
+                pa_assert(l > 0);
+
+                if (u->memchunk.length <=3D 0)
+                    pa_sink_render(u->sink, l, &u->memchunk);
+
+                pa_assert(u->memchunk.length > 0);
+
+                p =3D pa_memblock_acquire(u->memchunk.memblock);
+                t =3D pa_write(u->fd, (uint8_t*) p + u->memchunk.index, u-=
>memchunk.length, &write_type);
+                pa_memblock_release(u->memchunk.memblock);
+
+                pa_assert(t !=3D 0);
+
+                if (t < 0) {
+
+                    if (errno =3D=3D EINTR)
+                        continue;
+
+                    else if (errno =3D=3D EAGAIN) {
+
+                        pollfd[POLLFD_DSP].revents &=3D ~POLLOUT;
+                        break;
+
+                    } else {
+                        pa_log("Failed to write data to DSP: %s", pa_cstre=
rror(errno));
+                        goto fail;
+                    }
+
+                } else {
+
+                    u->memchunk.index +=3D t;
+                    u->memchunk.length -=3D t;
+
+                    if (u->memchunk.length <=3D 0) {
+                        pa_memblock_unref(u->memchunk.memblock);
+                        pa_memchunk_reset(&u->memchunk);
+                    }
+
+                    l -=3D t;
+
+                    pollfd[POLLFD_DSP].revents &=3D ~POLLOUT;
+                }
+
+            } while (loop && l > 0);
+
+            continue;
+        }
+
+        /* Try to read some data and pass it on to the source driver */
+
+        if (u->source && u->source->thread_info.state =3D=3D PA_SOURCE_RUN=
NING && ((pollfd[POLLFD_DSP].revents & POLLIN))) {
+            void *p;
+            ssize_t l;
+            pa_memchunk memchunk;
+            int loop =3D 0;
+
+            l =3D u->in_fragment_size;
+
+            if (u->use_getispace) {
+                audio_buf_info info;
+
+                if (ioctl(u->fd, SNDCTL_DSP_GETISPACE, &info) < 0) {
+                    pa_log_info("Device doesn't support SNDCTL_DSP_GETISPA=
CE: %s", pa_cstrerror(errno));
+                    u->use_getispace =3D 0;
+                } else {
+                    if (info.bytes >=3D l) {
+                        l =3D (info.bytes/l)*l;
+                        loop =3D 1;
+                    }
+                }
+            }
+
+            do {
+                ssize_t t;
+
+                pa_assert(l > 0);
+
+                memchunk.memblock =3D pa_memblock_new(u->core->mempool, l);
+
+                p =3D pa_memblock_acquire(memchunk.memblock);
+                t =3D pa_read(u->fd, p, l, &read_type);
+                pa_memblock_release(memchunk.memblock);
+
+                pa_assert(t !=3D 0); /* EOF cannot happen */
+
+                if (t < 0) {
+                    pa_memblock_unref(memchunk.memblock);
+
+                    if (errno =3D=3D EINTR)
+                        continue;
+
+                    else if (errno =3D=3D EAGAIN) {
+                        pollfd[POLLFD_DSP].revents &=3D ~POLLIN;
+                        break;
+
+                    } else {
+                        pa_log("Faile to read data from DSP: %s", pa_cstre=
rror(errno));
+                        goto fail;
+                    }
+
+                } else {
+                    memchunk.index =3D 0;
+                    memchunk.length =3D t;
+
+                    pa_source_post(u->source, &memchunk);
+                    pa_memblock_unref(memchunk.memblock);
+
+                    l -=3D t;
+
+                    pollfd[POLLFD_DSP].revents &=3D ~POLLIN;
+                }
+            } while (loop && l > 0);
+
+            continue;
+        }
+
+
+        if (u->fd >=3D 0) {
+            pollfd[POLLFD_DSP].fd =3D u->fd;
+            pollfd[POLLFD_DSP].events =3D
+                ((u->source && u->source->thread_info.state =3D=3D PA_SOUR=
CE_RUNNING) ? POLLIN : 0) |
+                ((u->sink && u->sink->thread_info.state =3D=3D PA_SINK_RUN=
NING) ? POLLOUT : 0);
+        }
+            =

+        /* Hmm, nothing to do. Let's sleep */
+
+        if (pa_asyncmsgq_before_poll(u->asyncmsgq) < 0)
+            continue;
+
+/*         pa_log("polling for %u", pollfd[POLLFD_DSP].events);  */
+        r =3D poll(pollfd, u->fd >=3D 0 ? POLLFD_MAX : POLLFD_DSP, -1);
+/*         pa_log("polling got %u", r > 0 ? pollfd[POLLFD_DSP].revents : 0=
);  */
+
+        pa_asyncmsgq_after_poll(u->asyncmsgq);
+
+        if (u->fd < 0)
+            pollfd[POLLFD_DSP].revents =3D 0;
+        =

+        if (r < 0) {
+            if (errno =3D=3D EINTR)
+                continue;
+
+            pa_log("poll() failed: %s", pa_cstrerror(errno));
+            goto fail;
+        }
+
+        if (pollfd[POLLFD_DSP].revents & ~(POLLOUT|POLLIN)) {
+            pa_log("DSP shutdown.");
+            goto fail;
+        }
+
+        pa_assert((pollfd[POLLFD_ASYNCQ].revents & ~POLLIN) =3D=3D 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_M=
ESSAGE_UNLOAD_MODULE, u->module, NULL, NULL);
+    pa_asyncmsgq_wait_for(u->asyncmsgq, PA_MESSAGE_SHUTDOWN);
+
+finish:
+    pa_log_debug("Thread shutting down");
 }
 =

 int pa__init(pa_core *c, pa_module*m) {
@@ -380,7 +649,7 @@
     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;
     }
 =

@@ -390,15 +659,15 @@
     }
 =

     if (!playback && !record) {
-        pa_log("neither playback nor record enabled for device.");
-        goto fail;
-    }
-
-    mode =3D (playback&&record) ? O_RDWR : (playback ? O_WRONLY : (record =
? O_RDONLY : 0));
+        pa_log("Neither playback nor record enabled for device.");
+        goto fail;
+    }
+
+    mode =3D (playback && record) ? O_RDWR : (playback ? O_WRONLY : (recor=
d ? O_RDONLY : 0));
 =

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

@@ -407,7 +676,7 @@
     frag_size =3D pa_bytes_per_second(&ss)/128;
 =

     if (pa_modargs_get_value_s32(ma, "fragments", &nfrags) < 0 || pa_modar=
gs_get_value_s32(ma, "fragment_size", &frag_size) < 0) {
-        pa_log("failed to parse fragments arguments");
+        pa_log("Failed to parse fragments arguments");
         goto fail;
     }
 =

@@ -415,11 +684,11 @@
         goto fail;
 =

     if (pa_oss_get_hw_description(p, hwdesc, sizeof(hwdesc)) >=3D 0)
-        pa_log_info("hardware name is '%s'.", hwdesc);
+        pa_log_info("Hardware name is '%s'.", hwdesc);
     else
         hwdesc[0] =3D 0;
 =

-    pa_log_info("device opened in %s mode.", mode =3D=3D O_WRONLY ? "O_WRO=
NLY" : (mode =3D=3D O_RDONLY ? "O_RDONLY" : "O_RDWR"));
+    pa_log_info("Device opened in %s mode.", mode =3D=3D O_WRONLY ? "O_WRO=
NLY" : (mode =3D=3D O_RDONLY ? "O_RDONLY" : "O_RDWR"));
 =

     if (nfrags >=3D 2 && frag_size >=3D 1)
         if (pa_oss_set_fragments(fd, nfrags, frag_size) < 0)
@@ -435,18 +704,27 @@
     assert(frag_size);
     in_frag_size =3D out_frag_size =3D frag_size;
 =

-    u =3D pa_xmalloc(sizeof(struct userdata));
+    u =3D pa_xnew0(struct userdata, 1);
     u->core =3D c;
+    u->module =3D m;
+    m->userdata =3D u;
     u->use_getospace =3D u->use_getispace =3D 0;
+    u->use_getodelay =3D 0;
+    u->use_input_volume =3D u->use_pcm_volume =3D 0;
+    u->mode =3D mode;
+    u->device_name =3D pa_xstrdup(p);
+    u->nfrags =3D nfrags;
+    u->frag_size =3D frag_size;
+    pa_assert_se(u->asyncmsgq =3D pa_asyncmsgq_new(0));
 =

     if (ioctl(fd, SNDCTL_DSP_GETISPACE, &info) >=3D 0) {
-        pa_log_info("input -- %u fragments of size %u.", info.fragstotal, =
info.fragsize);
+        pa_log_info("Input -- %u fragments of size %u.", info.fragstotal, =
info.fragsize);
         in_frag_size =3D info.fragsize;
         u->use_getispace =3D 1;
     }
 =

     if (ioctl(fd, SNDCTL_DSP_GETOSPACE, &info) >=3D 0) {
-        pa_log_info("output -- %u fragments of size %u.", info.fragstotal,=
 info.fragsize);
+        pa_log_info("Output -- %u fragments of size %u.", info.fragstotal,=
 info.fragsize);
         out_frag_size =3D info.fragsize;
         u->use_getospace =3D 1;
     }
@@ -462,12 +740,11 @@
         if (!(u->source =3D pa_source_new(c, __FILE__, name, namereg_fail,=
 &ss, &map)))
             goto fail;
 =

+        u->source->parent.process_msg =3D source_process_msg;
         u->source->userdata =3D u;
-        u->source->notify =3D source_notify_cb;
-        u->source->get_latency =3D source_get_latency_cb;
-        u->source->get_hw_volume =3D source_get_hw_volume;
-        u->source->set_hw_volume =3D source_set_hw_volume;
-        pa_source_set_owner(u->source, m);
+
+        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("OSS =
PCM on %s%s%s%s",
                                                                  p,
                                                                  hwdesc[0]=
 ? " (" : "",
@@ -475,6 +752,7 @@
                                                                  hwdesc[0]=
 ? ")" : ""));
         pa_xfree(t);
         u->source->is_hardware =3D 1;
+        u->source->refresh_volume =3D 1;
     } else
         u->source =3D NULL;
 =

@@ -492,11 +770,11 @@
         if (!(u->sink =3D pa_sink_new(c, __FILE__, name, namereg_fail, &ss=
, &map)))
             goto fail;
 =

-        u->sink->get_latency =3D sink_get_latency_cb;
-        u->sink->get_hw_volume =3D sink_get_hw_volume;
-        u->sink->set_hw_volume =3D sink_set_hw_volume;
+        u->sink->parent.process_msg =3D sink_process_msg;
         u->sink->userdata =3D u;
-        pa_sink_set_owner(u->sink, m);
+        =

+        pa_sink_set_module(u->sink, m);
+        pa_sink_set_asyncmsgq(u->sink, u->asyncmsgq);
         pa_sink_set_description(u->sink, t =3D pa_sprintf_malloc("OSS PCM =
on %s%s%s%s",
                                                            p,
                                                            hwdesc[0] ? " (=
" : "",
@@ -504,6 +782,7 @@
                                                            hwdesc[0] ? ")"=
 : ""));
         pa_xfree(t);
         u->sink->is_hardware =3D 1;
+        u->sink->refresh_volume =3D 1;
     } else
         u->sink =3D NULL;
 =

@@ -512,42 +791,26 @@
 =

     assert(u->source || u->sink);
 =

-    u->io =3D pa_iochannel_new(c->mainloop, u->source ? fd : -1, u->sink ?=
 fd : -1);
-    assert(u->io);
-    pa_iochannel_set_callback(u->io, io_callback, u);
     u->fd =3D fd;
 =

-    u->memchunk.memblock =3D NULL;
-    u->memchunk.length =3D 0;
+    pa_memchunk_reset(&u->memchunk);
     u->sample_size =3D pa_frame_size(&ss);
 =

     u->out_fragment_size =3D out_frag_size;
     u->in_fragment_size =3D in_frag_size;
-    u->silence.memblock =3D pa_memblock_new(u->core->mempool, u->silence.l=
ength =3D u->out_fragment_size);
-    assert(u->silence.memblock);
-    pa_silence_memblock(u->silence.memblock, &ss);
-    u->silence.index =3D 0;
-
-    u->module =3D m;
-    m->userdata =3D u;
+
+    if (!(u->thread =3D pa_thread_new(thread_func, u))) {
+        pa_log("Failed to create thread.");
+        goto fail;
+    }
 =

     pa_modargs_free(ma);
-
-    /*
-     * Some crappy drivers do not start the recording until we read someth=
ing.
-     * Without this snippet, poll will never register the fd as ready.
-     */
-    if (u->source) {
-        char *buf =3D pa_xnew(char, u->sample_size);
-        pa_read(u->fd, buf, u->sample_size, NULL);
-        pa_xfree(buf);
-    }
 =

     /* Read mixer settings */
     if (u->source)
-        source_get_hw_volume(u->source);
+        pa_asyncmsgq_post(u->asyncmsgq, PA_MSGOBJECT(u->source), PA_SOURCE=
_MESSAGE_GET_VOLUME, &u->source->volume, NULL, NULL);
     if (u->sink)
-        sink_get_hw_volume(u->sink);
+        pa_asyncmsgq_post(u->asyncmsgq, PA_MSGOBJECT(u->sink), PA_SINK_MES=
SAGE_GET_VOLUME, &u->sink->volume, NULL, NULL);
 =

     return 0;
 =

@@ -572,12 +835,33 @@
     if (!(u =3D m->userdata))
         return;
 =

-    clear_up(u);
+    if (u->sink)
+        pa_sink_disconnect(u->sink);
+
+    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->sink)
+        pa_sink_unref(u->sink);
+
+    if (u->source)
+        pa_source_unref(u->source);
 =

     if (u->memchunk.memblock)
         pa_memblock_unref(u->memchunk.memblock);
-    if (u->silence.memblock)
-        pa_memblock_unref(u->silence.memblock);
-
+
+    if (u->fd >=3D 0)
+        close(u->fd);
+
+    pa_xfree(u->device_name);
+    =

     pa_xfree(u);
 }




More information about the pulseaudio-commits mailing list