]> code.delx.au - pulseaudio/blob - src/modules/module-null-sink.c
make sure to call process_rewind() under all circumstances before we do the next...
[pulseaudio] / src / modules / module-null-sink.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-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 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 <sys/stat.h>
28 #include <stdio.h>
29 #include <errno.h>
30 #include <string.h>
31 #include <fcntl.h>
32 #include <unistd.h>
33 #include <limits.h>
34
35 #include <pulse/timeval.h>
36 #include <pulse/xmalloc.h>
37
38 #include <pulsecore/macro.h>
39 #include <pulsecore/sink.h>
40 #include <pulsecore/module.h>
41 #include <pulsecore/core-util.h>
42 #include <pulsecore/core-error.h>
43 #include <pulsecore/modargs.h>
44 #include <pulsecore/log.h>
45 #include <pulsecore/thread.h>
46 #include <pulsecore/thread-mq.h>
47 #include <pulsecore/rtpoll.h>
48 #include <pulsecore/rtclock.h>
49
50 #include "module-null-sink-symdef.h"
51
52 PA_MODULE_AUTHOR("Lennart Poettering");
53 PA_MODULE_DESCRIPTION("Clocked NULL sink");
54 PA_MODULE_VERSION(PACKAGE_VERSION);
55 PA_MODULE_LOAD_ONCE(FALSE);
56 PA_MODULE_USAGE(
57 "format=<sample format> "
58 "channels=<number of channels> "
59 "rate=<sample rate> "
60 "sink_name=<name of sink> "
61 "channel_map=<channel map> "
62 "description=<description for the sink>");
63
64 #define DEFAULT_SINK_NAME "null"
65 #define MAX_LATENCY_USEC (PA_USEC_PER_SEC * 2)
66
67 struct userdata {
68 pa_core *core;
69 pa_module *module;
70 pa_sink *sink;
71
72 pa_thread *thread;
73 pa_thread_mq thread_mq;
74 pa_rtpoll *rtpoll;
75
76 size_t block_size;
77
78 pa_usec_t block_usec;
79 pa_usec_t timestamp;
80 };
81
82 static const char* const valid_modargs[] = {
83 "rate",
84 "format",
85 "channels",
86 "sink_name",
87 "channel_map",
88 "description",
89 NULL
90 };
91
92 static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
93 struct userdata *u = PA_SINK(o)->userdata;
94
95 switch (code) {
96 case PA_SINK_MESSAGE_SET_STATE:
97
98 if (PA_PTR_TO_UINT(data) == PA_SINK_RUNNING)
99 u->timestamp = pa_rtclock_usec();
100
101 break;
102
103 case PA_SINK_MESSAGE_GET_LATENCY: {
104 pa_usec_t now;
105
106 now = pa_rtclock_usec();
107 *((pa_usec_t*) data) = u->timestamp > now ? u->timestamp - now : 0;
108
109 return 0;
110 }
111 }
112
113 return pa_sink_process_msg(o, code, data, offset, chunk);
114 }
115
116 static void sink_update_requested_latency_cb(pa_sink *s) {
117 struct userdata *u;
118
119 pa_sink_assert_ref(s);
120 u = s->userdata;
121 pa_assert(u);
122
123 u->block_usec = pa_sink_get_requested_latency_within_thread(s);
124 }
125
126 static void process_rewind(struct userdata *u, pa_usec_t now) {
127 size_t rewind_nbytes, in_buffer;
128 pa_usec_t delay;
129
130 pa_assert(u);
131
132 /* Figure out how much we shall rewind and reset the counter */
133 rewind_nbytes = u->sink->thread_info.rewind_nbytes;
134 u->sink->thread_info.rewind_nbytes = 0;
135
136 pa_assert(rewind_nbytes > 0);
137 pa_log_debug("Requested to rewind %lu bytes.", (unsigned long) rewind_nbytes);
138
139 if (u->timestamp <= now)
140 return;
141
142 delay = u->timestamp - now;
143 in_buffer = pa_usec_to_bytes(delay, &u->sink->sample_spec);
144
145 if (in_buffer <= 0)
146 return;
147
148 if (rewind_nbytes > in_buffer)
149 rewind_nbytes = in_buffer;
150
151 pa_sink_process_rewind(u->sink, rewind_nbytes);
152 u->timestamp -= pa_bytes_to_usec(rewind_nbytes, &u->sink->sample_spec);
153
154 pa_log_debug("Rewound %lu bytes.", (unsigned long) rewind_nbytes);
155 }
156
157 static void process_render(struct userdata *u, pa_usec_t now) {
158 size_t ate = 0;
159
160 pa_assert(u);
161
162 /* This is the configured latency. Sink inputs connected to us
163 might not have a single frame more than the maxrequest value
164 queed. Hence: at maximum read this many bytes from the sink
165 inputs. */
166
167 /* Fill the buffer up the the latency size */
168 while (u->timestamp < now + u->block_usec) {
169 pa_memchunk chunk;
170
171 pa_sink_render(u->sink, u->sink->thread_info.max_request, &chunk);
172 pa_memblock_unref(chunk.memblock);
173
174 /* pa_log_debug("Ate %lu bytes.", (unsigned long) chunk.length); */
175 u->timestamp += pa_bytes_to_usec(chunk.length, &u->sink->sample_spec);
176
177 ate += chunk.length;
178
179 if (ate >= u->sink->thread_info.max_request)
180 break;
181 }
182
183 /* pa_log_debug("Ate in sum %lu bytes (of %lu)", (unsigned long) ate, (unsigned long) nbytes); */
184 }
185
186 static void thread_func(void *userdata) {
187 struct userdata *u = userdata;
188
189 pa_assert(u);
190
191 pa_log_debug("Thread starting up");
192
193 pa_thread_mq_install(&u->thread_mq);
194 pa_rtpoll_install(u->rtpoll);
195
196 u->timestamp = pa_rtclock_usec();
197
198 for (;;) {
199 int ret;
200
201 /* Render some data and drop it immediately */
202 if (PA_SINK_IS_OPENED(u->sink->thread_info.state)) {
203 pa_usec_t now;
204
205 now = pa_rtclock_usec();
206
207 if (u->sink->thread_info.rewind_requested) {
208 if (u->sink->thread_info.rewind_nbytes > 0)
209 process_rewind(u, now);
210 else
211 pa_sink_process_rewind(u->sink, 0);
212 }
213
214 if (u->timestamp <= now)
215 process_render(u, now);
216
217 pa_rtpoll_set_timer_absolute(u->rtpoll, u->timestamp);
218 } else
219 pa_rtpoll_set_timer_disabled(u->rtpoll);
220
221 /* Hmm, nothing to do. Let's sleep */
222 if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0)
223 goto fail;
224
225 if (ret == 0)
226 goto finish;
227 }
228
229 fail:
230 /* If this was no regular exit from the loop we have to continue
231 * processing messages until we received PA_MESSAGE_SHUTDOWN */
232 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
233 pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
234
235 finish:
236 pa_log_debug("Thread shutting down");
237 }
238
239 int pa__init(pa_module*m) {
240 struct userdata *u = NULL;
241 pa_sample_spec ss;
242 pa_channel_map map;
243 pa_modargs *ma = NULL;
244 pa_sink_new_data data;
245
246 pa_assert(m);
247
248 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
249 pa_log("Failed to parse module arguments.");
250 goto fail;
251 }
252
253 ss = m->core->default_sample_spec;
254 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
255 pa_log("Invalid sample format specification or channel map");
256 goto fail;
257 }
258
259 m->userdata = u = pa_xnew0(struct userdata, 1);
260 u->core = m->core;
261 u->module = m;
262 u->rtpoll = pa_rtpoll_new();
263 pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
264
265 pa_sink_new_data_init(&data);
266 data.driver = __FILE__;
267 data.module = m;
268 pa_sink_new_data_set_name(&data, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME));
269 pa_sink_new_data_set_sample_spec(&data, &ss);
270 pa_sink_new_data_set_channel_map(&data, &map);
271 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_DESCRIPTION, pa_modargs_get_value(ma, "description", "Null Output"));
272 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_CLASS, "abstract");
273
274 u->sink = pa_sink_new(m->core, &data, PA_SINK_LATENCY);
275 pa_sink_new_data_done(&data);
276
277 if (!u->sink) {
278 pa_log("Failed to create sink object.");
279 goto fail;
280 }
281
282 u->sink->parent.process_msg = sink_process_msg;
283 u->sink->update_requested_latency = sink_update_requested_latency_cb;
284 u->sink->userdata = u;
285
286 pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
287 pa_sink_set_rtpoll(u->sink, u->rtpoll);
288
289 pa_sink_set_latency_range(u->sink, (pa_usec_t) -1, MAX_LATENCY_USEC);
290 u->block_usec = u->sink->thread_info.max_latency;
291
292 u->sink->thread_info.max_rewind =
293 u->sink->thread_info.max_request =
294 pa_usec_to_bytes(u->block_usec, &u->sink->sample_spec);
295
296 if (!(u->thread = pa_thread_new(thread_func, u))) {
297 pa_log("Failed to create thread.");
298 goto fail;
299 }
300
301 pa_sink_put(u->sink);
302
303 pa_modargs_free(ma);
304
305 return 0;
306
307 fail:
308 if (ma)
309 pa_modargs_free(ma);
310
311 pa__done(m);
312
313 return -1;
314 }
315
316 void pa__done(pa_module*m) {
317 struct userdata *u;
318
319 pa_assert(m);
320
321 if (!(u = m->userdata))
322 return;
323
324 if (u->sink)
325 pa_sink_unlink(u->sink);
326
327 if (u->thread) {
328 pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
329 pa_thread_free(u->thread);
330 }
331
332 pa_thread_mq_done(&u->thread_mq);
333
334 if (u->sink)
335 pa_sink_unref(u->sink);
336
337 if (u->rtpoll)
338 pa_rtpoll_free(u->rtpoll);
339
340 pa_xfree(u);
341 }