]> code.delx.au - pulseaudio/blob - src/pulsecore/play-memblockq.c
never hand out more data from a sink input than requested. Otherwise the resampler...
[pulseaudio] / src / pulsecore / play-memblockq.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2006-2008 Lennart Poettering
7
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.
12
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.
17
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
21 USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31
32 #include <pulse/xmalloc.h>
33 #include <pulse/gccmacro.h>
34
35 #include <pulsecore/sink-input.h>
36 #include <pulsecore/thread-mq.h>
37 #include <pulsecore/sample-util.h>
38
39 #include "play-memblockq.h"
40
41 typedef struct memblockq_stream {
42 pa_msgobject parent;
43 pa_core *core;
44 pa_sink_input *sink_input;
45 pa_memblockq *memblockq;
46 } memblockq_stream;
47
48 enum {
49 MEMBLOCKQ_STREAM_MESSAGE_UNLINK,
50 };
51
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);
55
56 static void memblockq_stream_unlink(memblockq_stream *u) {
57 pa_assert(u);
58
59 if (!u->sink_input)
60 return;
61
62 pa_sink_input_unlink(u->sink_input);
63 pa_sink_input_unref(u->sink_input);
64 u->sink_input = NULL;
65
66 memblockq_stream_unref(u);
67 }
68
69 static void memblockq_stream_free(pa_object *o) {
70 memblockq_stream *u = MEMBLOCKQ_STREAM(o);
71 pa_assert(u);
72
73 if (u->memblockq)
74 pa_memblockq_free(u->memblockq);
75
76 pa_xfree(u);
77 }
78
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);
82
83 switch (code) {
84 case MEMBLOCKQ_STREAM_MESSAGE_UNLINK:
85 memblockq_stream_unlink(u);
86 break;
87 }
88
89 return 0;
90 }
91
92 static void sink_input_kill_cb(pa_sink_input *i) {
93 memblockq_stream *u;
94
95 pa_sink_input_assert_ref(i);
96 u = MEMBLOCKQ_STREAM(i->userdata);
97 memblockq_stream_assert_ref(u);
98
99 memblockq_stream_unlink(u);
100 }
101
102 /* Called from IO thread context */
103 static void sink_input_state_change_cb(pa_sink_input *i, pa_sink_input_state_t state) {
104 memblockq_stream *u;
105
106 pa_sink_input_assert_ref(i);
107 u = MEMBLOCKQ_STREAM(i->userdata);
108 memblockq_stream_assert_ref(u);
109
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);
115 }
116
117 static int sink_input_pop_cb(pa_sink_input *i, size_t nbytes, pa_memchunk *chunk) {
118 memblockq_stream *u;
119
120 pa_sink_input_assert_ref(i);
121 pa_assert(chunk);
122 u = MEMBLOCKQ_STREAM(i->userdata);
123 memblockq_stream_assert_ref(u);
124
125 if (!u->memblockq)
126 return -1;
127
128 if (pa_memblockq_peek(u->memblockq, chunk) < 0) {
129
130 if (pa_sink_input_safe_to_remove(i)) {
131
132 pa_memblockq_free(u->memblockq);
133 u->memblockq = NULL;
134
135 pa_asyncmsgq_post(pa_thread_mq_get()->outq, PA_MSGOBJECT(u), MEMBLOCKQ_STREAM_MESSAGE_UNLINK, NULL, 0, NULL, NULL);
136 }
137
138 return -1;
139 }
140
141 chunk->length = PA_MIN(chunk->length, nbytes);
142 pa_memblockq_drop(u->memblockq, chunk->length);
143
144 return 0;
145 }
146
147 static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes) {
148 memblockq_stream *u;
149
150 pa_sink_input_assert_ref(i);
151 pa_assert(nbytes > 0);
152 u = MEMBLOCKQ_STREAM(i->userdata);
153 memblockq_stream_assert_ref(u);
154
155 if (!u->memblockq)
156 return;
157
158 pa_memblockq_rewind(u->memblockq, nbytes);
159 }
160
161 static void sink_input_update_max_rewind_cb(pa_sink_input *i, size_t nbytes) {
162 memblockq_stream *u;
163
164 pa_sink_input_assert_ref(i);
165 u = MEMBLOCKQ_STREAM(i->userdata);
166 memblockq_stream_assert_ref(u);
167
168 if (!u->memblockq)
169 return;
170
171 pa_memblockq_set_maxrewind(u->memblockq, nbytes);
172 }
173
174 pa_sink_input* pa_memblockq_sink_input_new(
175 pa_sink *sink,
176 const pa_sample_spec *ss,
177 const pa_channel_map *map,
178 pa_memblockq *q,
179 pa_cvolume *volume,
180 pa_proplist *p) {
181
182 memblockq_stream *u = NULL;
183 pa_sink_input_new_data data;
184
185 pa_assert(sink);
186 pa_assert(ss);
187
188 /* We allow creating this stream with no q set, so that it can be
189 * filled in later */
190
191 u = pa_msgobject_new(memblockq_stream);
192 u->parent.parent.free = memblockq_stream_free;
193 u->parent.process_msg = memblockq_stream_process_msg;
194 u->core = sink->core;
195 u->sink_input = NULL;
196 u->memblockq = NULL;
197
198 pa_sink_input_new_data_init(&data);
199 data.sink = sink;
200 data.driver = __FILE__;
201 pa_sink_input_new_data_set_sample_spec(&data, ss);
202 pa_sink_input_new_data_set_channel_map(&data, map);
203 pa_sink_input_new_data_set_volume(&data, volume);
204 pa_proplist_update(data.proplist, PA_UPDATE_REPLACE, p);
205
206 u->sink_input = pa_sink_input_new(sink->core, &data, 0);
207 pa_sink_input_new_data_done(&data);
208
209 if (!u->sink_input)
210 goto fail;
211
212 u->sink_input->pop = sink_input_pop_cb;
213 u->sink_input->process_rewind = sink_input_process_rewind_cb;
214 u->sink_input->update_max_rewind = sink_input_update_max_rewind_cb;
215 u->sink_input->kill = sink_input_kill_cb;
216 u->sink_input->state_change = sink_input_state_change_cb;
217 u->sink_input->userdata = u;
218
219 if (q)
220 pa_memblockq_sink_input_set_queue(u->sink_input, q);
221
222 /* The reference to u is dangling here, because we want
223 * to keep this stream around until it is fully played. */
224
225 /* This sink input is not "put" yet, i.e. pa_sink_input_put() has
226 * not been called! */
227
228 return pa_sink_input_ref(u->sink_input);
229
230 fail:
231 if (u)
232 memblockq_stream_unref(u);
233
234 return NULL;
235 }
236
237 int pa_play_memblockq(
238 pa_sink *sink,
239 const pa_sample_spec *ss,
240 const pa_channel_map *map,
241 pa_memblockq *q,
242 pa_cvolume *volume,
243 pa_proplist *p,
244 uint32_t *sink_input_index) {
245
246 pa_sink_input *i;
247
248 pa_assert(sink);
249 pa_assert(ss);
250 pa_assert(q);
251
252 if (!(i = pa_memblockq_sink_input_new(sink, ss, map, q, volume, p)))
253 return -1;
254
255 pa_sink_input_put(i);
256
257 if (sink_input_index)
258 *sink_input_index = i->index;
259
260 pa_sink_input_unref(i);
261
262 return 0;
263 }
264
265 void pa_memblockq_sink_input_set_queue(pa_sink_input *i, pa_memblockq *q) {
266 memblockq_stream *u;
267
268 pa_sink_input_assert_ref(i);
269 u = MEMBLOCKQ_STREAM(i->userdata);
270 memblockq_stream_assert_ref(u);
271
272 if (u->memblockq)
273 pa_memblockq_free(u->memblockq);
274
275 if ((u->memblockq = q)) {
276 pa_memblockq_set_prebuf(q, 0);
277 pa_memblockq_set_silence(q, NULL);
278 pa_memblockq_willneed(q);
279 }
280 }