2006-07-28 22:52:28 +00:00
|
|
|
/***
|
|
|
|
|
This file is part of PulseAudio.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
Copyright 2006-2008 Lennart Poettering
|
2007-02-13 15:35:19 +00:00
|
|
|
|
2006-07-28 22:52:28 +00:00
|
|
|
PulseAudio is free software; you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU Lesser General Public License as published
|
2009-03-03 20:23:02 +00:00
|
|
|
by the Free Software Foundation; either version 2.1 of the License,
|
2006-07-28 22:52:28 +00:00
|
|
|
or (at your option) any later version.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-07-28 22:52:28 +00:00
|
|
|
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.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-07-28 22:52:28 +00:00
|
|
|
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
|
|
|
|
|
USA.
|
|
|
|
|
***/
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
#include <pulse/xmalloc.h>
|
2008-05-15 23:34:41 +00:00
|
|
|
#include <pulse/gccmacro.h>
|
2006-07-28 22:52:28 +00:00
|
|
|
|
|
|
|
|
#include <pulsecore/sink-input.h>
|
2007-10-28 19:13:50 +00:00
|
|
|
#include <pulsecore/thread-mq.h>
|
2008-05-15 23:34:41 +00:00
|
|
|
#include <pulsecore/sample-util.h>
|
2006-07-28 22:52:28 +00:00
|
|
|
|
|
|
|
|
#include "play-memblockq.h"
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
typedef struct memblockq_stream {
|
|
|
|
|
pa_msgobject parent;
|
|
|
|
|
pa_core *core;
|
|
|
|
|
pa_sink_input *sink_input;
|
|
|
|
|
pa_memblockq *memblockq;
|
|
|
|
|
} memblockq_stream;
|
2006-07-28 22:52:28 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
enum {
|
|
|
|
|
MEMBLOCKQ_STREAM_MESSAGE_UNLINK,
|
|
|
|
|
};
|
2006-07-28 22:52:28 +00:00
|
|
|
|
2009-08-21 21:27:44 +02:00
|
|
|
PA_DEFINE_PRIVATE_CLASS(memblockq_stream, pa_msgobject);
|
2007-10-28 19:13:50 +00:00
|
|
|
#define MEMBLOCKQ_STREAM(o) (memblockq_stream_cast(o))
|
|
|
|
|
|
|
|
|
|
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);
|
2006-07-28 22:52:28 +00:00
|
|
|
}
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
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);
|
2006-07-28 22:52:28 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_xfree(u);
|
2006-07-28 22:52:28 +00:00
|
|
|
}
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void sink_input_kill_cb(pa_sink_input *i) {
|
2008-05-15 23:34:41 +00:00
|
|
|
memblockq_stream *u;
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_sink_input_assert_ref(i);
|
2008-05-15 23:34:41 +00:00
|
|
|
u = MEMBLOCKQ_STREAM(i->userdata);
|
|
|
|
|
memblockq_stream_assert_ref(u);
|
2007-10-28 19:13:50 +00:00
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
memblockq_stream_unlink(u);
|
2006-07-28 22:52:28 +00:00
|
|
|
}
|
|
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
/* Called from IO thread context */
|
|
|
|
|
static void sink_input_state_change_cb(pa_sink_input *i, pa_sink_input_state_t state) {
|
2007-10-28 19:13:50 +00:00
|
|
|
memblockq_stream *u;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
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)
|
2009-01-15 00:40:06 +01:00
|
|
|
pa_sink_input_request_rewind(i, 0, FALSE, TRUE, TRUE);
|
2008-05-15 23:34:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(chunk);
|
|
|
|
|
u = MEMBLOCKQ_STREAM(i->userdata);
|
|
|
|
|
memblockq_stream_assert_ref(u);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
if (!u->memblockq)
|
|
|
|
|
return -1;
|
2006-07-28 22:52:28 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
if (pa_memblockq_peek(u->memblockq, chunk) < 0) {
|
2008-05-15 23:34:41 +00:00
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
2006-07-28 22:52:28 +00:00
|
|
|
|
2011-04-20 15:56:29 +03:00
|
|
|
/* If there's no memblock, there's going to be data in the memblockq after
|
|
|
|
|
* a gap with length chunk->length. Drop the the gap and peek the actual
|
|
|
|
|
* data. There should always be some data coming - hence the assert. The
|
|
|
|
|
* gap will occur if the memblockq is rewound beyond index 0.*/
|
|
|
|
|
if (!chunk->memblock) {
|
|
|
|
|
pa_memblockq_drop(u->memblockq, chunk->length);
|
|
|
|
|
pa_assert_se(pa_memblockq_peek(u->memblockq, chunk) >= 0);
|
|
|
|
|
}
|
virtual-sink: Fix a crash when moving the sink to a new master right after setup.
If the virtual sink is moved to a new master right after it has been created,
then the virtual sink input's memblockq can be rewound to a negative read
index. The data written prior to the move starts from index zero, so after the
rewind there's a bit of silence. If the memblockq doesn't have a silence
memchunk set, then pa_memblockq_peek() will return zero in such case, and the
returned memchunk's memblock pointer will be NULL.
That scenario wasn't taken into account in the implementation of
sink_input_pop_cb. Setting a silence memchunk for the memblockq solves this
problem, because pa_memblock_peek() will now return a valid memblock if the
read index happens to point to a hole in the memblockq.
I believe this isn't the best possible solution, though. It doesn't really make
sense to rewind the sink input's memblockq beyond index 0 in the first place,
because now when the stream starts to play to the new master sink, there's some
unnecessary silence before the actual data starts. This is a small problem,
though, and I don't grok the rewinding system well enough to know how to fix
this issue properly.
I went through all files that call pa_memblockq_peek() to see if there are more
similar bugs. play-memblockq.c was the only one that looked to me like it might
be broken in the same way. I didn't try reproducing the bug with
play-memblockq.c, though, so I just added a FIXME comment there.
2011-02-24 16:16:43 +02:00
|
|
|
|
2008-05-27 22:08:42 +00:00
|
|
|
chunk->length = PA_MIN(chunk->length, nbytes);
|
2008-05-15 23:34:41 +00:00
|
|
|
pa_memblockq_drop(u->memblockq, chunk->length);
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
return 0;
|
2006-07-28 22:52:28 +00:00
|
|
|
}
|
|
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes) {
|
2007-10-28 19:13:50 +00:00
|
|
|
memblockq_stream *u;
|
|
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
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);
|
2007-10-28 19:13:50 +00:00
|
|
|
u = MEMBLOCKQ_STREAM(i->userdata);
|
|
|
|
|
memblockq_stream_assert_ref(u);
|
|
|
|
|
|
|
|
|
|
if (!u->memblockq)
|
|
|
|
|
return;
|
|
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
pa_memblockq_set_maxrewind(u->memblockq, nbytes);
|
2007-10-28 19:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pa_sink_input* pa_memblockq_sink_input_new(
|
2006-08-13 16:19:56 +00:00
|
|
|
pa_sink *sink,
|
|
|
|
|
const pa_sample_spec *ss,
|
|
|
|
|
const pa_channel_map *map,
|
|
|
|
|
pa_memblockq *q,
|
2008-05-15 23:34:41 +00:00
|
|
|
pa_cvolume *volume,
|
2010-02-21 17:35:05 +01:00
|
|
|
pa_proplist *p,
|
|
|
|
|
pa_sink_input_flags_t flags) {
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
memblockq_stream *u = NULL;
|
2006-08-13 16:19:56 +00:00
|
|
|
pa_sink_input_new_data data;
|
2006-07-28 22:52:28 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(sink);
|
|
|
|
|
pa_assert(ss);
|
2006-07-28 22:52:28 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
/* 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;
|
2008-05-15 23:34:41 +00:00
|
|
|
u->memblockq = NULL;
|
2007-10-28 19:13:50 +00:00
|
|
|
|
2006-08-13 16:19:56 +00:00
|
|
|
pa_sink_input_new_data_init(&data);
|
2011-02-28 13:23:23 +05:30
|
|
|
pa_sink_input_new_data_set_sink(&data, sink, FALSE);
|
2006-08-13 16:19:56 +00:00
|
|
|
data.driver = __FILE__;
|
|
|
|
|
pa_sink_input_new_data_set_sample_spec(&data, ss);
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_sink_input_new_data_set_channel_map(&data, map);
|
2009-02-04 18:34:08 +01:00
|
|
|
pa_sink_input_new_data_set_volume(&data, volume);
|
2008-05-15 23:34:41 +00:00
|
|
|
pa_proplist_update(data.proplist, PA_UPDATE_REPLACE, p);
|
2010-02-21 17:35:05 +01:00
|
|
|
data.flags |= flags;
|
2008-05-15 23:34:41 +00:00
|
|
|
|
2009-08-28 23:24:09 +02:00
|
|
|
pa_sink_input_new(&u->sink_input, sink->core, &data);
|
2008-05-15 23:34:41 +00:00
|
|
|
pa_sink_input_new_data_done(&data);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
if (!u->sink_input)
|
2007-10-28 19:13:50 +00:00
|
|
|
goto fail;
|
|
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
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;
|
2007-10-28 19:13:50 +00:00
|
|
|
u->sink_input->kill = sink_input_kill_cb;
|
2008-05-15 23:34:41 +00:00
|
|
|
u->sink_input->state_change = sink_input_state_change_cb;
|
2007-10-28 19:13:50 +00:00
|
|
|
u->sink_input->userdata = u;
|
|
|
|
|
|
|
|
|
|
if (q)
|
2008-05-15 23:34:41 +00:00
|
|
|
pa_memblockq_sink_input_set_queue(u->sink_input, q);
|
2006-07-28 22:52:28 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
/* The reference to u is dangling here, because we want
|
|
|
|
|
* to keep this stream around until it is fully played. */
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
/* This sink input is not "put" yet, i.e. pa_sink_input_put() has
|
|
|
|
|
* not been called! */
|
2006-07-28 22:52:28 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
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 pa_sample_spec *ss,
|
|
|
|
|
const pa_channel_map *map,
|
|
|
|
|
pa_memblockq *q,
|
2008-05-15 23:34:41 +00:00
|
|
|
pa_cvolume *volume,
|
|
|
|
|
pa_proplist *p,
|
2010-02-21 17:35:05 +01:00
|
|
|
pa_sink_input_flags_t flags,
|
2008-05-15 23:34:41 +00:00
|
|
|
uint32_t *sink_input_index) {
|
2007-10-28 19:13:50 +00:00
|
|
|
|
|
|
|
|
pa_sink_input *i;
|
|
|
|
|
|
|
|
|
|
pa_assert(sink);
|
|
|
|
|
pa_assert(ss);
|
|
|
|
|
pa_assert(q);
|
|
|
|
|
|
2010-02-21 17:35:05 +01:00
|
|
|
if (!(i = pa_memblockq_sink_input_new(sink, ss, map, q, volume, p, flags)))
|
2007-10-28 19:13:50 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
pa_sink_input_put(i);
|
2008-05-15 23:34:41 +00:00
|
|
|
|
|
|
|
|
if (sink_input_index)
|
|
|
|
|
*sink_input_index = i->index;
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_sink_input_unref(i);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-07-28 22:52:28 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
2007-10-28 19:13:50 +00:00
|
|
|
|
|
|
|
|
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);
|
2008-05-15 23:34:41 +00:00
|
|
|
|
|
|
|
|
if ((u->memblockq = q)) {
|
|
|
|
|
pa_memblockq_set_prebuf(q, 0);
|
|
|
|
|
pa_memblockq_set_silence(q, NULL);
|
|
|
|
|
pa_memblockq_willneed(q);
|
|
|
|
|
}
|
2007-10-28 19:13:50 +00:00
|
|
|
}
|