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