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