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