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