]> code.delx.au - pulseaudio/blobdiff - src/pulsecore/play-memblockq.c
move flat volume logic into the core. while doing so add n_volume_steps field to...
[pulseaudio] / src / pulsecore / play-memblockq.c
index 2df3b9523328f1f15f3dc24c69c3d4e440a947c1..eac21de5995acbebd1e9ad4f3bbca97c910e395e 100644 (file)
@@ -1,18 +1,18 @@
-/* $Id$ */
-
 /***
   This file is part of PulseAudio.
+
+  Copyright 2006-2008 Lennart Poettering
+
   PulseAudio is free software; you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as published
   by the Free Software Foundation; either version 2 of the License,
   or (at your option) any later version.
+
   PulseAudio is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   General Public License for more details.
+
   You should have received a copy of the GNU Lesser General Public License
   along with PulseAudio; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 #endif
 
 #include <stdlib.h>
-#include <assert.h>
 #include <stdio.h>
 #include <string.h>
 
 #include <pulse/xmalloc.h>
+#include <pulse/gccmacro.h>
 
 #include <pulsecore/sink-input.h>
-#include <pulsecore/gccmacro.h>
+#include <pulsecore/thread-mq.h>
+#include <pulsecore/sample-util.h>
 
 #include "play-memblockq.h"
 
-static void sink_input_kill(pa_sink_input *i) {
-    pa_memblockq *q;
-    assert(i);
-    assert(i->userdata);
+typedef struct memblockq_stream {
+    pa_msgobject parent;
+    pa_core *core;
+    pa_sink_input *sink_input;
+    pa_memblockq *memblockq;
+} memblockq_stream;
 
-    q = i->userdata;
+enum {
+    MEMBLOCKQ_STREAM_MESSAGE_UNLINK,
+};
 
-    pa_sink_input_disconnect(i);
-    pa_sink_input_unref(i);
+PA_DECLARE_CLASS(memblockq_stream);
+#define MEMBLOCKQ_STREAM(o) (memblockq_stream_cast(o))
+static PA_DEFINE_CHECK_TYPE(memblockq_stream, pa_msgobject);
+
+static void memblockq_stream_unlink(memblockq_stream *u) {
+    pa_assert(u);
+
+    if (!u->sink_input)
+        return;
+
+    pa_sink_input_unlink(u->sink_input);
+    pa_sink_input_unref(u->sink_input);
+    u->sink_input = NULL;
+
+    memblockq_stream_unref(u);
+}
+
+static void memblockq_stream_free(pa_object *o) {
+    memblockq_stream *u = MEMBLOCKQ_STREAM(o);
+    pa_assert(u);
+
+    if (u->memblockq)
+        pa_memblockq_free(u->memblockq);
+
+    pa_xfree(u);
+}
+
+static int memblockq_stream_process_msg(pa_msgobject *o, int code, void*userdata, int64_t offset, pa_memchunk *chunk) {
+    memblockq_stream *u = MEMBLOCKQ_STREAM(o);
+    memblockq_stream_assert_ref(u);
+
+    switch (code) {
+        case MEMBLOCKQ_STREAM_MESSAGE_UNLINK:
+            memblockq_stream_unlink(u);
+            break;
+    }
 
-    pa_memblockq_free(q);
+    return 0;
 }
 
-static int sink_input_peek(pa_sink_input *i, pa_memchunk *chunk) {
-    pa_memblockq *q;
-    assert(i);
-    assert(chunk);
-    assert(i->userdata);
+static void sink_input_kill_cb(pa_sink_input *i) {
+    memblockq_stream *u;
 
-    q = i->userdata;
+    pa_sink_input_assert_ref(i);
+    u = MEMBLOCKQ_STREAM(i->userdata);
+    memblockq_stream_assert_ref(u);
 
-    return pa_memblockq_peek(q, chunk);
+    memblockq_stream_unlink(u);
 }
 
-static void si_kill(PA_GCC_UNUSED pa_mainloop_api *m, void *i) {
-    sink_input_kill(i);
+/* Called from IO thread context */
+static void sink_input_state_change_cb(pa_sink_input *i, pa_sink_input_state_t state) {
+    memblockq_stream *u;
+
+    pa_sink_input_assert_ref(i);
+    u = MEMBLOCKQ_STREAM(i->userdata);
+    memblockq_stream_assert_ref(u);
+
+    /* If we are added for the first time, ask for a rewinding so that
+     * we are heard right-away. */
+    if (PA_SINK_INPUT_IS_LINKED(state) &&
+        i->thread_info.state == PA_SINK_INPUT_INIT)
+        pa_sink_input_request_rewind(i, 0, FALSE, TRUE, TRUE);
 }
 
-static void sink_input_drop(pa_sink_input *i, const pa_memchunk*chunk, size_t length) {
-    pa_memblockq *q;
-    
-    assert(i);
-    assert(length > 0);
-    assert( i->userdata);
-    
-    q = i->userdata;
+static int sink_input_pop_cb(pa_sink_input *i, size_t nbytes, pa_memchunk *chunk) {
+    memblockq_stream *u;
+
+    pa_sink_input_assert_ref(i);
+    pa_assert(chunk);
+    u = MEMBLOCKQ_STREAM(i->userdata);
+    memblockq_stream_assert_ref(u);
+
+    if (!u->memblockq)
+        return -1;
+
+    if (pa_memblockq_peek(u->memblockq, chunk) < 0) {
+
+        if (pa_sink_input_safe_to_remove(i)) {
+
+            pa_memblockq_free(u->memblockq);
+            u->memblockq = NULL;
 
-    pa_memblockq_drop(q, chunk, length);
+            pa_asyncmsgq_post(pa_thread_mq_get()->outq, PA_MSGOBJECT(u), MEMBLOCKQ_STREAM_MESSAGE_UNLINK, NULL, 0, NULL, NULL);
+        }
 
-    if (pa_memblockq_get_length(q) <= 0)
-        pa_mainloop_api_once(i->sink->core->mainloop, si_kill, i);
+        return -1;
+    }
+
+    chunk->length = PA_MIN(chunk->length, nbytes);
+    pa_memblockq_drop(u->memblockq, chunk->length);
+
+    return 0;
+}
+
+static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes) {
+    memblockq_stream *u;
+
+    pa_sink_input_assert_ref(i);
+    u = MEMBLOCKQ_STREAM(i->userdata);
+    memblockq_stream_assert_ref(u);
+
+    if (!u->memblockq)
+        return;
+
+    pa_memblockq_rewind(u->memblockq, nbytes);
+}
+
+static void sink_input_update_max_rewind_cb(pa_sink_input *i, size_t nbytes) {
+    memblockq_stream *u;
+
+    pa_sink_input_assert_ref(i);
+    u = MEMBLOCKQ_STREAM(i->userdata);
+    memblockq_stream_assert_ref(u);
+
+    if (!u->memblockq)
+        return;
+
+    pa_memblockq_set_maxrewind(u->memblockq, nbytes);
+}
+
+pa_sink_input* pa_memblockq_sink_input_new(
+        pa_sink *sink,
+        const pa_sample_spec *ss,
+        const pa_channel_map *map,
+        pa_memblockq *q,
+        pa_cvolume *volume,
+        pa_proplist *p) {
+
+    memblockq_stream *u = NULL;
+    pa_sink_input_new_data data;
+
+    pa_assert(sink);
+    pa_assert(ss);
+
+    /* We allow creating this stream with no q set, so that it can be
+     * filled in later */
+
+    u = pa_msgobject_new(memblockq_stream);
+    u->parent.parent.free = memblockq_stream_free;
+    u->parent.process_msg = memblockq_stream_process_msg;
+    u->core = sink->core;
+    u->sink_input = NULL;
+    u->memblockq = NULL;
+
+    pa_sink_input_new_data_init(&data);
+    data.sink = sink;
+    data.driver = __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_virtual_volume(&data, volume);
+    pa_proplist_update(data.proplist, PA_UPDATE_REPLACE, p);
+
+    u->sink_input = pa_sink_input_new(sink->core, &data, 0);
+    pa_sink_input_new_data_done(&data);
+
+    if (!u->sink_input)
+        goto fail;
+
+    u->sink_input->pop = sink_input_pop_cb;
+    u->sink_input->process_rewind = sink_input_process_rewind_cb;
+    u->sink_input->update_max_rewind = sink_input_update_max_rewind_cb;
+    u->sink_input->kill = sink_input_kill_cb;
+    u->sink_input->state_change = sink_input_state_change_cb;
+    u->sink_input->userdata = u;
+
+    if (q)
+        pa_memblockq_sink_input_set_queue(u->sink_input, q);
+
+    /* The reference to u is dangling here, because we want
+     * to keep this stream around until it is fully played. */
+
+    /* This sink input is not "put" yet, i.e. pa_sink_input_put() has
+     * not been called! */
+
+    return pa_sink_input_ref(u->sink_input);
+
+fail:
+    if (u)
+        memblockq_stream_unref(u);
+
+    return NULL;
 }
 
 int pa_play_memblockq(
-    pa_sink *sink,
-    const char *name,
-    const pa_sample_spec *ss,
-    const pa_channel_map *map,
-    pa_memblockq *q,
-    pa_cvolume *cvolume) {
-    
-    pa_sink_input *si;
-
-    assert(sink);
-    assert(ss);
-    assert(q);
-
-    if (pa_memblockq_get_length(q) <= 0)
-        return 0;
-
-    if (cvolume && pa_cvolume_is_muted(cvolume))
-        return 0;
-
-    if (!(si = pa_sink_input_new(sink, name, __FILE__, ss, map, cvolume, 0, PA_RESAMPLER_INVALID)))
+        pa_sink *sink,
+        const pa_sample_spec *ss,
+        const pa_channel_map *map,
+        pa_memblockq *q,
+        pa_cvolume *volume,
+        pa_proplist *p,
+        uint32_t *sink_input_index) {
+
+    pa_sink_input *i;
+
+    pa_assert(sink);
+    pa_assert(ss);
+    pa_assert(q);
+
+    if (!(i = pa_memblockq_sink_input_new(sink, ss, map, q, volume, p)))
         return -1;
 
-    si->peek = sink_input_peek;
-    si->drop = sink_input_drop;
-    si->kill = sink_input_kill;
-    
-    si->userdata = q;
+    pa_sink_input_put(i);
+
+    if (sink_input_index)
+        *sink_input_index = i->index;
+
+    pa_sink_input_unref(i);
 
-    pa_sink_notify(sink);
-    
     return 0;
 }
+
+void pa_memblockq_sink_input_set_queue(pa_sink_input *i, pa_memblockq *q) {
+    memblockq_stream *u;
+
+    pa_sink_input_assert_ref(i);
+    u = MEMBLOCKQ_STREAM(i->userdata);
+    memblockq_stream_assert_ref(u);
+
+    if (u->memblockq)
+        pa_memblockq_free(u->memblockq);
+
+    if ((u->memblockq = q)) {
+        pa_memblockq_set_prebuf(q, 0);
+        pa_memblockq_set_silence(q, NULL);
+        pa_memblockq_willneed(q);
+    }
+}