4 This file is part of PulseAudio.
6 Copyright 2006-2008 Lennart Poettering
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
32 #include <pulse/xmalloc.h>
33 #include <pulse/gccmacro.h>
35 #include <pulsecore/sink-input.h>
36 #include <pulsecore/thread-mq.h>
37 #include <pulsecore/sample-util.h>
39 #include "play-memblockq.h"
41 typedef struct memblockq_stream {
44 pa_sink_input *sink_input;
45 pa_memblockq *memblockq;
49 MEMBLOCKQ_STREAM_MESSAGE_UNLINK,
52 PA_DECLARE_CLASS(memblockq_stream);
53 #define MEMBLOCKQ_STREAM(o) (memblockq_stream_cast(o))
54 static PA_DEFINE_CHECK_TYPE(memblockq_stream, pa_msgobject);
56 static void memblockq_stream_unlink(memblockq_stream *u) {
62 pa_sink_input_unlink(u->sink_input);
63 pa_sink_input_unref(u->sink_input);
66 memblockq_stream_unref(u);
69 static void memblockq_stream_free(pa_object *o) {
70 memblockq_stream *u = MEMBLOCKQ_STREAM(o);
74 pa_memblockq_free(u->memblockq);
79 static int memblockq_stream_process_msg(pa_msgobject *o, int code, void*userdata, int64_t offset, pa_memchunk *chunk) {
80 memblockq_stream *u = MEMBLOCKQ_STREAM(o);
81 memblockq_stream_assert_ref(u);
84 case MEMBLOCKQ_STREAM_MESSAGE_UNLINK:
85 memblockq_stream_unlink(u);
92 static void sink_input_kill_cb(pa_sink_input *i) {
95 pa_sink_input_assert_ref(i);
96 u = MEMBLOCKQ_STREAM(i->userdata);
97 memblockq_stream_assert_ref(u);
99 memblockq_stream_unlink(u);
102 /* Called from IO thread context */
103 static void sink_input_state_change_cb(pa_sink_input *i, pa_sink_input_state_t state) {
106 pa_sink_input_assert_ref(i);
107 u = MEMBLOCKQ_STREAM(i->userdata);
108 memblockq_stream_assert_ref(u);
110 /* If we are added for the first time, ask for a rewinding so that
111 * we are heard right-away. */
112 if (PA_SINK_INPUT_IS_LINKED(state) &&
113 i->thread_info.state == PA_SINK_INPUT_INIT)
114 pa_sink_input_request_rewind(i, 0, FALSE, TRUE);
117 static int sink_input_pop_cb(pa_sink_input *i, size_t nbytes, pa_memchunk *chunk) {
120 pa_sink_input_assert_ref(i);
122 u = MEMBLOCKQ_STREAM(i->userdata);
123 memblockq_stream_assert_ref(u);
128 if (pa_memblockq_peek(u->memblockq, chunk) < 0) {
130 if (pa_sink_input_safe_to_remove(i)) {
132 pa_memblockq_free(u->memblockq);
135 pa_asyncmsgq_post(pa_thread_mq_get()->outq, PA_MSGOBJECT(u), MEMBLOCKQ_STREAM_MESSAGE_UNLINK, NULL, 0, NULL, NULL);
141 pa_memblockq_drop(u->memblockq, chunk->length);
146 static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes) {
149 pa_sink_input_assert_ref(i);
150 pa_assert(nbytes > 0);
151 u = MEMBLOCKQ_STREAM(i->userdata);
152 memblockq_stream_assert_ref(u);
157 pa_memblockq_rewind(u->memblockq, nbytes);
160 static void sink_input_update_max_rewind_cb(pa_sink_input *i, size_t nbytes) {
163 pa_sink_input_assert_ref(i);
164 u = MEMBLOCKQ_STREAM(i->userdata);
165 memblockq_stream_assert_ref(u);
170 pa_memblockq_set_maxrewind(u->memblockq, nbytes);
173 pa_sink_input* pa_memblockq_sink_input_new(
175 const pa_sample_spec *ss,
176 const pa_channel_map *map,
181 memblockq_stream *u = NULL;
182 pa_sink_input_new_data data;
187 /* We allow creating this stream with no q set, so that it can be
190 u = pa_msgobject_new(memblockq_stream);
191 u->parent.parent.free = memblockq_stream_free;
192 u->parent.process_msg = memblockq_stream_process_msg;
193 u->core = sink->core;
194 u->sink_input = NULL;
197 pa_sink_input_new_data_init(&data);
199 data.driver = __FILE__;
200 pa_sink_input_new_data_set_sample_spec(&data, ss);
201 pa_sink_input_new_data_set_channel_map(&data, map);
202 pa_sink_input_new_data_set_volume(&data, volume);
203 pa_proplist_update(data.proplist, PA_UPDATE_REPLACE, p);
205 u->sink_input = pa_sink_input_new(sink->core, &data, 0);
206 pa_sink_input_new_data_done(&data);
211 u->sink_input->pop = sink_input_pop_cb;
212 u->sink_input->process_rewind = sink_input_process_rewind_cb;
213 u->sink_input->update_max_rewind = sink_input_update_max_rewind_cb;
214 u->sink_input->kill = sink_input_kill_cb;
215 u->sink_input->state_change = sink_input_state_change_cb;
216 u->sink_input->userdata = u;
219 pa_memblockq_sink_input_set_queue(u->sink_input, q);
221 /* The reference to u is dangling here, because we want
222 * to keep this stream around until it is fully played. */
224 /* This sink input is not "put" yet, i.e. pa_sink_input_put() has
225 * not been called! */
227 return pa_sink_input_ref(u->sink_input);
231 memblockq_stream_unref(u);
236 int pa_play_memblockq(
238 const pa_sample_spec *ss,
239 const pa_channel_map *map,
243 uint32_t *sink_input_index) {
251 if (!(i = pa_memblockq_sink_input_new(sink, ss, map, q, volume, p)))
254 pa_sink_input_put(i);
256 if (sink_input_index)
257 *sink_input_index = i->index;
259 pa_sink_input_unref(i);
264 void pa_memblockq_sink_input_set_queue(pa_sink_input *i, pa_memblockq *q) {
267 pa_sink_input_assert_ref(i);
268 u = MEMBLOCKQ_STREAM(i->userdata);
269 memblockq_stream_assert_ref(u);
272 pa_memblockq_free(u->memblockq);
274 if ((u->memblockq = q)) {
275 pa_memblockq_set_prebuf(q, 0);
276 pa_memblockq_set_silence(q, NULL);
277 pa_memblockq_willneed(q);