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