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