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