]> code.delx.au - pulseaudio/blob - src/pulsecore/play-memblockq.c
Big pile of dependant changes:
[pulseaudio] / src / pulsecore / play-memblockq.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2006 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 static int sink_input_pop_cb(pa_sink_input *i, size_t nbytes, pa_memchunk *chunk) {
103 memblockq_stream *u;
104
105 pa_sink_input_assert_ref(i);
106 pa_assert(chunk);
107 u = MEMBLOCKQ_STREAM(i->userdata);
108 memblockq_stream_assert_ref(u);
109
110 if (!u->memblockq)
111 return -1;
112
113 if (pa_memblockq_peek(u->memblockq, chunk) < 0) {
114
115 if (pa_sink_input_safe_to_remove(i)) {
116
117 pa_memblockq_free(u->memblockq);
118 u->memblockq = NULL;
119 pa_asyncmsgq_post(pa_thread_mq_get()->outq, PA_MSGOBJECT(u), MEMBLOCKQ_STREAM_MESSAGE_UNLINK, NULL, 0, NULL, NULL);
120 }
121
122 return -1;
123 }
124
125 pa_memblockq_drop(u->memblockq, chunk->length);
126
127 return 0;
128 }
129
130 static void sink_input_rewind_cb(pa_sink_input *i, size_t nbytes) {
131 memblockq_stream *u;
132
133 pa_sink_input_assert_ref(i);
134 pa_assert(nbytes > 0);
135 u = MEMBLOCKQ_STREAM(i->userdata);
136 memblockq_stream_assert_ref(u);
137
138 if (!u->memblockq)
139 return;
140
141 pa_memblockq_rewind(u->memblockq, nbytes);
142 }
143
144 static void sink_input_set_max_rewind(pa_sink_input *i, size_t nbytes) {
145 memblockq_stream *u;
146
147 pa_sink_input_assert_ref(i);
148 u = MEMBLOCKQ_STREAM(i->userdata);
149 memblockq_stream_assert_ref(u);
150
151 if (!u->memblockq)
152 return;
153
154 pa_memblockq_set_maxrewind(u->memblockq, nbytes);
155 }
156
157 pa_sink_input* pa_memblockq_sink_input_new(
158 pa_sink *sink,
159 const pa_sample_spec *ss,
160 const pa_channel_map *map,
161 pa_memblockq *q,
162 pa_cvolume *volume,
163 pa_proplist *p) {
164
165 memblockq_stream *u = NULL;
166 pa_sink_input_new_data data;
167
168 pa_assert(sink);
169 pa_assert(ss);
170
171 /* We allow creating this stream with no q set, so that it can be
172 * filled in later */
173
174 u = pa_msgobject_new(memblockq_stream);
175 u->parent.parent.free = memblockq_stream_free;
176 u->parent.process_msg = memblockq_stream_process_msg;
177 u->core = sink->core;
178 u->sink_input = NULL;
179 u->memblockq = NULL;
180
181 pa_sink_input_new_data_init(&data);
182 data.sink = sink;
183 data.driver = __FILE__;
184 pa_sink_input_new_data_set_sample_spec(&data, ss);
185 pa_sink_input_new_data_set_channel_map(&data, map);
186 pa_sink_input_new_data_set_volume(&data, volume);
187 pa_proplist_update(data.proplist, PA_UPDATE_REPLACE, p);
188
189 u->sink_input = pa_sink_input_new(sink->core, &data, 0);
190 pa_sink_input_new_data_done(&data);
191
192 if (!u->sink_input)
193 goto fail;
194
195 u->sink_input->pop = sink_input_pop_cb;
196 u->sink_input->rewind = sink_input_rewind_cb;
197 u->sink_input->set_max_rewind = sink_input_set_max_rewind;
198 u->sink_input->kill = sink_input_kill_cb;
199 u->sink_input->userdata = u;
200
201 if (q)
202 pa_memblockq_sink_input_set_queue(u->sink_input, q);
203
204 /* The reference to u is dangling here, because we want
205 * to keep this stream around until it is fully played. */
206
207 /* This sink input is not "put" yet, i.e. pa_sink_input_put() has
208 * not been called! */
209
210 return pa_sink_input_ref(u->sink_input);
211
212 fail:
213 if (u)
214 memblockq_stream_unref(u);
215
216 return NULL;
217 }
218
219 int pa_play_memblockq(
220 pa_sink *sink,
221 const pa_sample_spec *ss,
222 const pa_channel_map *map,
223 pa_memblockq *q,
224 pa_cvolume *volume,
225 pa_proplist *p,
226 uint32_t *sink_input_index) {
227
228 pa_sink_input *i;
229
230 pa_assert(sink);
231 pa_assert(ss);
232 pa_assert(q);
233
234 if (!(i = pa_memblockq_sink_input_new(sink, ss, map, q, volume, p)))
235 return -1;
236
237 pa_sink_input_put(i);
238
239 if (sink_input_index)
240 *sink_input_index = i->index;
241
242 pa_sink_input_unref(i);
243
244 return 0;
245 }
246
247 void pa_memblockq_sink_input_set_queue(pa_sink_input *i, pa_memblockq *q) {
248 memblockq_stream *u;
249
250 pa_sink_input_assert_ref(i);
251 u = MEMBLOCKQ_STREAM(i->userdata);
252 memblockq_stream_assert_ref(u);
253
254 if (u->memblockq)
255 pa_memblockq_free(u->memblockq);
256
257 if ((u->memblockq = q)) {
258 pa_memchunk silence;
259
260 pa_memblockq_set_prebuf(q, 0);
261
262 pa_sink_input_get_silence(i, &silence);
263 pa_memblockq_set_silence(q, &silence);
264 pa_memblock_unref(silence.memblock);
265
266 pa_memblockq_willneed(q);
267 }
268 }