From 045c1d602dcba57868845ba3270510593c39480f Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 15 May 2008 23:34:41 +0000 Subject: merge glitch-free branch back into trunk git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@2445 fefdeb5f-60dc-0310-8127-8f9354f1896f --- src/pulsecore/play-memblockq.c | 117 ++++++++++++++++++++++++++++------------- 1 file changed, 80 insertions(+), 37 deletions(-) (limited to 'src/pulsecore/play-memblockq.c') diff --git a/src/pulsecore/play-memblockq.c b/src/pulsecore/play-memblockq.c index 5d3c2d39..2688f923 100644 --- a/src/pulsecore/play-memblockq.c +++ b/src/pulsecore/play-memblockq.c @@ -3,7 +3,7 @@ /*** This file is part of PulseAudio. - Copyright 2006 Lennart Poettering + 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 @@ -30,10 +30,11 @@ #include #include +#include #include -#include #include +#include #include "play-memblockq.h" @@ -59,7 +60,6 @@ static void memblockq_stream_unlink(memblockq_stream *u) { return; pa_sink_input_unlink(u->sink_input); - pa_sink_input_unref(u->sink_input); u->sink_input = NULL; @@ -70,8 +70,6 @@ static void memblockq_stream_free(pa_object *o) { memblockq_stream *u = MEMBLOCKQ_STREAM(o); pa_assert(u); - memblockq_stream_unlink(u); - if (u->memblockq) pa_memblockq_free(u->memblockq); @@ -92,15 +90,34 @@ static int memblockq_stream_process_msg(pa_msgobject *o, int code, void*userdata } static void sink_input_kill_cb(pa_sink_input *i) { + memblockq_stream *u; + pa_sink_input_assert_ref(i); + u = MEMBLOCKQ_STREAM(i->userdata); + memblockq_stream_assert_ref(u); - memblockq_stream_unlink(MEMBLOCKQ_STREAM(i->userdata)); + memblockq_stream_unlink(u); } -static int sink_input_peek_cb(pa_sink_input *i, size_t length, pa_memchunk *chunk) { +/* 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_assert(i); + 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); +} + +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); @@ -109,36 +126,57 @@ static int sink_input_peek_cb(pa_sink_input *i, size_t length, pa_memchunk *chun return -1; if (pa_memblockq_peek(u->memblockq, chunk) < 0) { - pa_memblockq_free(u->memblockq); - u->memblockq = NULL; - pa_asyncmsgq_post(pa_thread_mq_get()->outq, PA_MSGOBJECT(u), MEMBLOCKQ_STREAM_MESSAGE_UNLINK, NULL, 0, NULL, NULL); + + if (pa_sink_input_safe_to_remove(i)) { + + pa_memblockq_free(u->memblockq); + u->memblockq = NULL; + + pa_asyncmsgq_post(pa_thread_mq_get()->outq, PA_MSGOBJECT(u), MEMBLOCKQ_STREAM_MESSAGE_UNLINK, NULL, 0, NULL, NULL); + } + return -1; } + pa_memblockq_drop(u->memblockq, chunk->length); + return 0; } -static void sink_input_drop_cb(pa_sink_input *i, size_t length) { +static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes) { memblockq_stream *u; - pa_assert(i); - pa_assert(length > 0); + pa_sink_input_assert_ref(i); + pa_assert(nbytes > 0); + 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_drop(u->memblockq, length); + pa_memblockq_set_maxrewind(u->memblockq, nbytes); } pa_sink_input* pa_memblockq_sink_input_new( pa_sink *sink, - const char *name, const pa_sample_spec *ss, const pa_channel_map *map, pa_memblockq *q, - pa_cvolume *volume) { + pa_cvolume *volume, + pa_proplist *p) { memblockq_stream *u = NULL; pa_sink_input_new_data data; @@ -149,41 +187,36 @@ pa_sink_input* pa_memblockq_sink_input_new( /* We allow creating this stream with no q set, so that it can be * filled in later */ - if (q && pa_memblockq_get_length(q) <= 0) { - pa_memblockq_free(q); - return NULL; - } - - if (volume && pa_cvolume_is_muted(volume)) { - pa_memblockq_free(q); - return NULL; - } - 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 = q; + u->memblockq = NULL; pa_sink_input_new_data_init(&data); data.sink = sink; - data.name = name; 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_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 = pa_sink_input_new(sink->core, &data, 0))) + if (!u->sink_input) goto fail; - u->sink_input->peek = sink_input_peek_cb; - u->sink_input->drop = sink_input_drop_cb; + 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_prebuf_disable(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. */ @@ -202,11 +235,12 @@ fail: 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 *volume) { + pa_cvolume *volume, + pa_proplist *p, + uint32_t *sink_input_index) { pa_sink_input *i; @@ -214,10 +248,14 @@ int pa_play_memblockq( pa_assert(ss); pa_assert(q); - if (!(i = pa_memblockq_sink_input_new(sink, name, ss, map, q, volume))) + if (!(i = pa_memblockq_sink_input_new(sink, ss, map, q, volume, p))) return -1; pa_sink_input_put(i); + + if (sink_input_index) + *sink_input_index = i->index; + pa_sink_input_unref(i); return 0; @@ -232,5 +270,10 @@ void pa_memblockq_sink_input_set_queue(pa_sink_input *i, pa_memblockq *q) { if (u->memblockq) pa_memblockq_free(u->memblockq); - u->memblockq = q; + + if ((u->memblockq = q)) { + pa_memblockq_set_prebuf(q, 0); + pa_memblockq_set_silence(q, NULL); + pa_memblockq_willneed(q); + } } -- cgit