2 This file is part of PulseAudio.
4 Copyright 2006-2008 Lennart Poettering
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2.1 of the License,
9 or (at your option) any later version.
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
30 #include <pulse/xmalloc.h>
31 #include <pulse/gccmacro.h>
33 #include <pulsecore/sink-input.h>
34 #include <pulsecore/thread-mq.h>
35 #include <pulsecore/sample-util.h>
37 #include "play-memblockq.h"
39 typedef struct memblockq_stream {
42 pa_sink_input *sink_input;
43 pa_memblockq *memblockq;
47 MEMBLOCKQ_STREAM_MESSAGE_UNLINK,
50 PA_DEFINE_PRIVATE_CLASS(memblockq_stream, pa_msgobject);
51 #define MEMBLOCKQ_STREAM(o) (memblockq_stream_cast(o))
53 static void memblockq_stream_unlink(memblockq_stream *u) {
59 pa_sink_input_unlink(u->sink_input);
60 pa_sink_input_unref(u->sink_input);
63 memblockq_stream_unref(u);
66 static void memblockq_stream_free(pa_object *o) {
67 memblockq_stream *u = MEMBLOCKQ_STREAM(o);
71 pa_memblockq_free(u->memblockq);
76 static int memblockq_stream_process_msg(pa_msgobject *o, int code, void*userdata, int64_t offset, pa_memchunk *chunk) {
77 memblockq_stream *u = MEMBLOCKQ_STREAM(o);
78 memblockq_stream_assert_ref(u);
81 case MEMBLOCKQ_STREAM_MESSAGE_UNLINK:
82 memblockq_stream_unlink(u);
89 static void sink_input_kill_cb(pa_sink_input *i) {
92 pa_sink_input_assert_ref(i);
93 u = MEMBLOCKQ_STREAM(i->userdata);
94 memblockq_stream_assert_ref(u);
96 memblockq_stream_unlink(u);
99 /* Called from IO thread context */
100 static void sink_input_state_change_cb(pa_sink_input *i, pa_sink_input_state_t state) {
103 pa_sink_input_assert_ref(i);
104 u = MEMBLOCKQ_STREAM(i->userdata);
105 memblockq_stream_assert_ref(u);
107 /* If we are added for the first time, ask for a rewinding so that
108 * we are heard right-away. */
109 if (PA_SINK_INPUT_IS_LINKED(state) &&
110 i->thread_info.state == PA_SINK_INPUT_INIT)
111 pa_sink_input_request_rewind(i, 0, FALSE, TRUE, TRUE);
114 static int sink_input_pop_cb(pa_sink_input *i, size_t nbytes, pa_memchunk *chunk) {
117 pa_sink_input_assert_ref(i);
119 u = MEMBLOCKQ_STREAM(i->userdata);
120 memblockq_stream_assert_ref(u);
125 if (pa_memblockq_peek(u->memblockq, chunk) < 0) {
127 if (pa_sink_input_safe_to_remove(i)) {
129 pa_memblockq_free(u->memblockq);
132 pa_asyncmsgq_post(pa_thread_mq_get()->outq, PA_MSGOBJECT(u), MEMBLOCKQ_STREAM_MESSAGE_UNLINK, NULL, 0, NULL, NULL);
138 chunk->length = PA_MIN(chunk->length, nbytes);
139 pa_memblockq_drop(u->memblockq, chunk->length);
144 static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes) {
147 pa_sink_input_assert_ref(i);
148 u = MEMBLOCKQ_STREAM(i->userdata);
149 memblockq_stream_assert_ref(u);
154 pa_memblockq_rewind(u->memblockq, nbytes);
157 static void sink_input_update_max_rewind_cb(pa_sink_input *i, size_t nbytes) {
160 pa_sink_input_assert_ref(i);
161 u = MEMBLOCKQ_STREAM(i->userdata);
162 memblockq_stream_assert_ref(u);
167 pa_memblockq_set_maxrewind(u->memblockq, nbytes);
170 pa_sink_input* pa_memblockq_sink_input_new(
172 const pa_sample_spec *ss,
173 const pa_channel_map *map,
178 memblockq_stream *u = NULL;
179 pa_sink_input_new_data data;
184 /* We allow creating this stream with no q set, so that it can be
187 u = pa_msgobject_new(memblockq_stream);
188 u->parent.parent.free = memblockq_stream_free;
189 u->parent.process_msg = memblockq_stream_process_msg;
190 u->core = sink->core;
191 u->sink_input = NULL;
194 pa_sink_input_new_data_init(&data);
196 data.driver = __FILE__;
197 pa_sink_input_new_data_set_sample_spec(&data, ss);
198 pa_sink_input_new_data_set_channel_map(&data, map);
199 pa_sink_input_new_data_set_volume(&data, volume);
200 pa_proplist_update(data.proplist, PA_UPDATE_REPLACE, p);
202 pa_sink_input_new(&u->sink_input, sink->core, &data);
203 pa_sink_input_new_data_done(&data);
208 u->sink_input->pop = sink_input_pop_cb;
209 u->sink_input->process_rewind = sink_input_process_rewind_cb;
210 u->sink_input->update_max_rewind = sink_input_update_max_rewind_cb;
211 u->sink_input->kill = sink_input_kill_cb;
212 u->sink_input->state_change = sink_input_state_change_cb;
213 u->sink_input->userdata = u;
216 pa_memblockq_sink_input_set_queue(u->sink_input, q);
218 /* The reference to u is dangling here, because we want
219 * to keep this stream around until it is fully played. */
221 /* This sink input is not "put" yet, i.e. pa_sink_input_put() has
222 * not been called! */
224 return pa_sink_input_ref(u->sink_input);
228 memblockq_stream_unref(u);
233 int pa_play_memblockq(
235 const pa_sample_spec *ss,
236 const pa_channel_map *map,
240 uint32_t *sink_input_index) {
248 if (!(i = pa_memblockq_sink_input_new(sink, ss, map, q, volume, p)))
251 pa_sink_input_put(i);
253 if (sink_input_index)
254 *sink_input_index = i->index;
256 pa_sink_input_unref(i);
261 void pa_memblockq_sink_input_set_queue(pa_sink_input *i, pa_memblockq *q) {
264 pa_sink_input_assert_ref(i);
265 u = MEMBLOCKQ_STREAM(i->userdata);
266 memblockq_stream_assert_ref(u);
269 pa_memblockq_free(u->memblockq);
271 if ((u->memblockq = q)) {
272 pa_memblockq_set_prebuf(q, 0);
273 pa_memblockq_set_silence(q, NULL);
274 pa_memblockq_willneed(q);