get rid of svn $ keywords
[pulseaudio-mirror.git] / src / pulsecore / play-memblockq.c
blob8b3e79b9386b7b033a934b6ec75f685c486f69a9
1 /***
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 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
19 USA.
20 ***/
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
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 {
40 pa_msgobject parent;
41 pa_core *core;
42 pa_sink_input *sink_input;
43 pa_memblockq *memblockq;
44 } memblockq_stream;
46 enum {
47 MEMBLOCKQ_STREAM_MESSAGE_UNLINK,
50 PA_DECLARE_CLASS(memblockq_stream);
51 #define MEMBLOCKQ_STREAM(o) (memblockq_stream_cast(o))
52 static PA_DEFINE_CHECK_TYPE(memblockq_stream, pa_msgobject);
54 static void memblockq_stream_unlink(memblockq_stream *u) {
55 pa_assert(u);
57 if (!u->sink_input)
58 return;
60 pa_sink_input_unlink(u->sink_input);
61 pa_sink_input_unref(u->sink_input);
62 u->sink_input = NULL;
64 memblockq_stream_unref(u);
67 static void memblockq_stream_free(pa_object *o) {
68 memblockq_stream *u = MEMBLOCKQ_STREAM(o);
69 pa_assert(u);
71 if (u->memblockq)
72 pa_memblockq_free(u->memblockq);
74 pa_xfree(u);
77 static int memblockq_stream_process_msg(pa_msgobject *o, int code, void*userdata, int64_t offset, pa_memchunk *chunk) {
78 memblockq_stream *u = MEMBLOCKQ_STREAM(o);
79 memblockq_stream_assert_ref(u);
81 switch (code) {
82 case MEMBLOCKQ_STREAM_MESSAGE_UNLINK:
83 memblockq_stream_unlink(u);
84 break;
87 return 0;
90 static void sink_input_kill_cb(pa_sink_input *i) {
91 memblockq_stream *u;
93 pa_sink_input_assert_ref(i);
94 u = MEMBLOCKQ_STREAM(i->userdata);
95 memblockq_stream_assert_ref(u);
97 memblockq_stream_unlink(u);
100 /* Called from IO thread context */
101 static void sink_input_state_change_cb(pa_sink_input *i, pa_sink_input_state_t state) {
102 memblockq_stream *u;
104 pa_sink_input_assert_ref(i);
105 u = MEMBLOCKQ_STREAM(i->userdata);
106 memblockq_stream_assert_ref(u);
108 /* If we are added for the first time, ask for a rewinding so that
109 * we are heard right-away. */
110 if (PA_SINK_INPUT_IS_LINKED(state) &&
111 i->thread_info.state == PA_SINK_INPUT_INIT)
112 pa_sink_input_request_rewind(i, 0, FALSE, TRUE);
115 static int sink_input_pop_cb(pa_sink_input *i, size_t nbytes, pa_memchunk *chunk) {
116 memblockq_stream *u;
118 pa_sink_input_assert_ref(i);
119 pa_assert(chunk);
120 u = MEMBLOCKQ_STREAM(i->userdata);
121 memblockq_stream_assert_ref(u);
123 if (!u->memblockq)
124 return -1;
126 if (pa_memblockq_peek(u->memblockq, chunk) < 0) {
128 if (pa_sink_input_safe_to_remove(i)) {
130 pa_memblockq_free(u->memblockq);
131 u->memblockq = NULL;
133 pa_asyncmsgq_post(pa_thread_mq_get()->outq, PA_MSGOBJECT(u), MEMBLOCKQ_STREAM_MESSAGE_UNLINK, NULL, 0, NULL, NULL);
136 return -1;
139 chunk->length = PA_MIN(chunk->length, nbytes);
140 pa_memblockq_drop(u->memblockq, chunk->length);
142 return 0;
145 static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes) {
146 memblockq_stream *u;
148 pa_sink_input_assert_ref(i);
149 pa_assert(nbytes > 0);
150 u = MEMBLOCKQ_STREAM(i->userdata);
151 memblockq_stream_assert_ref(u);
153 if (!u->memblockq)
154 return;
156 pa_memblockq_rewind(u->memblockq, nbytes);
159 static void sink_input_update_max_rewind_cb(pa_sink_input *i, size_t nbytes) {
160 memblockq_stream *u;
162 pa_sink_input_assert_ref(i);
163 u = MEMBLOCKQ_STREAM(i->userdata);
164 memblockq_stream_assert_ref(u);
166 if (!u->memblockq)
167 return;
169 pa_memblockq_set_maxrewind(u->memblockq, nbytes);
172 pa_sink_input* pa_memblockq_sink_input_new(
173 pa_sink *sink,
174 const pa_sample_spec *ss,
175 const pa_channel_map *map,
176 pa_memblockq *q,
177 pa_cvolume *volume,
178 pa_proplist *p) {
180 memblockq_stream *u = NULL;
181 pa_sink_input_new_data data;
183 pa_assert(sink);
184 pa_assert(ss);
186 /* We allow creating this stream with no q set, so that it can be
187 * filled in later */
189 u = pa_msgobject_new(memblockq_stream);
190 u->parent.parent.free = memblockq_stream_free;
191 u->parent.process_msg = memblockq_stream_process_msg;
192 u->core = sink->core;
193 u->sink_input = NULL;
194 u->memblockq = NULL;
196 pa_sink_input_new_data_init(&data);
197 data.sink = sink;
198 data.driver = __FILE__;
199 pa_sink_input_new_data_set_sample_spec(&data, ss);
200 pa_sink_input_new_data_set_channel_map(&data, map);
201 pa_sink_input_new_data_set_volume(&data, volume);
202 pa_proplist_update(data.proplist, PA_UPDATE_REPLACE, p);
204 u->sink_input = pa_sink_input_new(sink->core, &data, 0);
205 pa_sink_input_new_data_done(&data);
207 if (!u->sink_input)
208 goto fail;
210 u->sink_input->pop = sink_input_pop_cb;
211 u->sink_input->process_rewind = sink_input_process_rewind_cb;
212 u->sink_input->update_max_rewind = sink_input_update_max_rewind_cb;
213 u->sink_input->kill = sink_input_kill_cb;
214 u->sink_input->state_change = sink_input_state_change_cb;
215 u->sink_input->userdata = u;
217 if (q)
218 pa_memblockq_sink_input_set_queue(u->sink_input, q);
220 /* The reference to u is dangling here, because we want
221 * to keep this stream around until it is fully played. */
223 /* This sink input is not "put" yet, i.e. pa_sink_input_put() has
224 * not been called! */
226 return pa_sink_input_ref(u->sink_input);
228 fail:
229 if (u)
230 memblockq_stream_unref(u);
232 return NULL;
235 int pa_play_memblockq(
236 pa_sink *sink,
237 const pa_sample_spec *ss,
238 const pa_channel_map *map,
239 pa_memblockq *q,
240 pa_cvolume *volume,
241 pa_proplist *p,
242 uint32_t *sink_input_index) {
244 pa_sink_input *i;
246 pa_assert(sink);
247 pa_assert(ss);
248 pa_assert(q);
250 if (!(i = pa_memblockq_sink_input_new(sink, ss, map, q, volume, p)))
251 return -1;
253 pa_sink_input_put(i);
255 if (sink_input_index)
256 *sink_input_index = i->index;
258 pa_sink_input_unref(i);
260 return 0;
263 void pa_memblockq_sink_input_set_queue(pa_sink_input *i, pa_memblockq *q) {
264 memblockq_stream *u;
266 pa_sink_input_assert_ref(i);
267 u = MEMBLOCKQ_STREAM(i->userdata);
268 memblockq_stream_assert_ref(u);
270 if (u->memblockq)
271 pa_memblockq_free(u->memblockq);
273 if ((u->memblockq = q)) {
274 pa_memblockq_set_prebuf(q, 0);
275 pa_memblockq_set_silence(q, NULL);
276 pa_memblockq_willneed(q);