[pulseaudio-commits] r1571 - /branches/lennart/src/pulsecore/play-memchunk.c

svnmailer-noreply at 0pointer.de svnmailer-noreply at 0pointer.de
Sat Aug 4 17:07:08 PDT 2007


Author: lennart
Date: Sun Aug  5 02:07:07 2007
New Revision: 1571

URL: http://0pointer.de/cgi-bin/viewcvs.cgi?rev=3D1571&root=3Dpulseaudio&vi=
ew=3Drev
Log:
modernize play-memchunk and port it to the new core

Modified:
    branches/lennart/src/pulsecore/play-memchunk.c

Modified: branches/lennart/src/pulsecore/play-memchunk.c
URL: http://0pointer.de/cgi-bin/viewcvs.cgi/branches/lennart/src/pulsecore/=
play-memchunk.c?rev=3D1571&root=3Dpulseaudio&r1=3D1570&r2=3D1571&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/pulsecore/play-memchunk.c (original)
+++ branches/lennart/src/pulsecore/play-memchunk.c Sun Aug  5 02:07:07 2007
@@ -26,7 +26,6 @@
 #endif
 =

 #include <stdlib.h>
-#include <assert.h>
 #include <stdio.h>
 #include <string.h>
 =

@@ -37,51 +36,105 @@
 =

 #include "play-memchunk.h"
 =

-static void sink_input_kill_cb(pa_sink_input *i) {
-    pa_memchunk *c;
-    assert(i && i->userdata);
-    c =3D i->userdata;
+typedef struct memchunk_stream {
+    pa_msgobject parent;
+    pa_core *core;
+    pa_sink_input *sink_input;
+    pa_memchunk memchunk;
+} memchunk_stream;
 =

-    pa_sink_input_disconnect(i);
-    pa_sink_input_unref(i);
+enum {
+    MEMCHUNK_STREAM_MESSAGE_UNLINK,
+};
 =

-    pa_memblock_unref(c->memblock);
-    pa_xfree(c);
+PA_DECLARE_CLASS(memchunk_stream);
+#define MEMCHUNK_STREAM(o) (memchunk_stream_cast(o))
+static PA_DEFINE_CHECK_TYPE(memchunk_stream, pa_msgobject);
+
+static void memchunk_stream_unlink(memchunk_stream *u) {
+    pa_assert(u);
+
+    if (!u->sink_input)
+        return;
+
+    pa_sink_input_disconnect(u->sink_input);
+    =

+    pa_sink_input_unref(u->sink_input);
+    u->sink_input =3D NULL;
+    =

+    /* Make sure we don't decrease the ref count twice. */
+    memchunk_stream_unref(u);
 }
 =

-static int sink_input_peek_cb(pa_sink_input *i, pa_memchunk *chunk) {
-    pa_memchunk *c;
-    assert(i && chunk && i->userdata);
-    c =3D i->userdata;
+static void memchunk_stream_free(pa_object *o) {
+    memchunk_stream *u =3D MEMCHUNK_STREAM(o);
+    pa_assert(u);
 =

-    if (c->length <=3D 0)
-        return -1;
+    memchunk_stream_unlink(u);
+    =

+    if (u->memchunk.memblock)
+        pa_memblock_unref(u->memchunk.memblock);
 =

-    assert(c->memblock);
-    *chunk =3D *c;
-    pa_memblock_ref(c->memblock);
+    pa_xfree(u);
+}
+
+static int memchunk_stream_process_msg(pa_msgobject *o, int code, void*use=
rdata, int64_t offset, pa_memchunk *chunk) {
+    memchunk_stream *u =3D MEMCHUNK_STREAM(o);
+    memchunk_stream_assert_ref(u);
+    =

+    switch (code) {
+        case MEMCHUNK_STREAM_MESSAGE_UNLINK:
+            memchunk_stream_unlink(u);
+            break;
+    }
 =

     return 0;
 }
 =

-static void si_kill_cb(PA_GCC_UNUSED pa_mainloop_api *m, void *i) {
-    sink_input_kill_cb(i);
+static void sink_input_kill_cb(pa_sink_input *i) {
+    pa_sink_input_assert_ref(i);
+
+    memchunk_stream_unlink(MEMCHUNK_STREAM(i->userdata));
+}
+
+static int sink_input_peek_cb(pa_sink_input *i, pa_memchunk *chunk) {
+    memchunk_stream *u;
+
+    pa_assert(i);
+    pa_assert(chunk);
+    u =3D MEMCHUNK_STREAM(i->userdata);
+    memchunk_stream_assert_ref(u);
+
+    if (!u->memchunk.memblock)
+        return -1;
+    =

+    if (u->memchunk.length <=3D 0) {
+        pa_memblock_unref(u->memchunk.memblock);
+        u->memchunk.memblock =3D NULL;
+        pa_asyncmsgq_post(u->core->asyncmsgq, PA_MSGOBJECT(u), MEMCHUNK_ST=
REAM_MESSAGE_UNLINK, NULL, 0, NULL, NULL);
+        return -1;
+    }
+
+    pa_assert(u->memchunk.memblock);
+    *chunk =3D u->memchunk;
+    pa_memblock_ref(chunk->memblock);
+
+    return 0;
 }
 =

 static void sink_input_drop_cb(pa_sink_input *i, size_t length) {
-    pa_memchunk *c;
-    assert(i && length && i->userdata);
-    c =3D i->userdata;
+    memchunk_stream *u;
 =

-    if (length >=3D c->length) {
-        c->length -=3D length;
-        c->index +=3D length;
-    } else {
+    pa_assert(i);
+    pa_assert(length > 0);
+    u =3D MEMCHUNK_STREAM(i->userdata);
+    memchunk_stream_assert_ref(u);
 =

-        c->length =3D 0;
-
-        pa_mainloop_api_once(i->sink->core->mainloop, si_kill_cb, i);
-    }
+    if (length >=3D u->memchunk.length) {
+        u->memchunk.length -=3D length;
+        u->memchunk.index +=3D length;
+    } else
+        u->memchunk.length =3D 0;
 }
 =

 int pa_play_memchunk(
@@ -92,36 +145,51 @@
         const pa_memchunk *chunk,
         pa_cvolume *volume) {
 =

-    pa_sink_input *si;
-    pa_memchunk *nchunk;
+    memchunk_stream *u =3D NULL;
     pa_sink_input_new_data data;
 =

-    assert(sink);
-    assert(ss);
-    assert(chunk);
+    pa_assert(sink);
+    pa_assert(ss);
+    pa_assert(chunk);
 =

     if (volume && pa_cvolume_is_muted(volume))
         return 0;
 =

+    u =3D pa_msgobject_new(memchunk_stream);
+    u->parent.parent.free =3D memchunk_stream_free;
+    u->parent.process_msg =3D memchunk_stream_process_msg;
+    u->core =3D sink->core;
+    u->sink_input =3D NULL;
+    u->memchunk =3D *chunk;
+    pa_memblock_ref(u->memchunk.memblock);
+
     pa_sink_input_new_data_init(&data);
     data.sink =3D sink;
+    data.driver =3D __FILE__;
     data.name =3D name;
-    data.driver =3D __FILE__;
     pa_sink_input_new_data_set_sample_spec(&data, ss);
     pa_sink_input_new_data_set_channel_map(&data, map);
     pa_sink_input_new_data_set_volume(&data, volume);
 =

-    if (!(si =3D pa_sink_input_new(sink->core, &data, 0)))
-        return -1;
+    if (!(u->sink_input =3D pa_sink_input_new(sink->core, &data, 0)))
+        goto fail;
+    =

+    u->sink_input->peek =3D sink_input_peek_cb;
+    u->sink_input->drop =3D sink_input_drop_cb;
+    u->sink_input->kill =3D sink_input_kill_cb;
+    u->sink_input->userdata =3D u;
 =

-    si->peek =3D sink_input_peek_cb;
-    si->drop =3D sink_input_drop_cb;
-    si->kill =3D sink_input_kill_cb;
+    pa_sink_input_put(u->sink_input);
 =

-    si->userdata =3D nchunk =3D pa_xnew(pa_memchunk, 1);
-    *nchunk =3D *chunk;
+    /* The reference to u is dangling here, because we want to keep
+     * this stream around until it is fully played. */
+    =

+    return 0;
 =

-    pa_memblock_ref(chunk->memblock);
+fail:
+    if (u)
+        memchunk_stream_unref(u);
 =

-    return 0;
+    return -1;
 }
+




More information about the pulseaudio-commits mailing list