• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, see <http://www.gnu.org/licenses/>.
18 ***/
19 
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23 
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <errno.h>
27 #include <unistd.h>
28 
29 #include <pulse/rtclock.h>
30 #include <pulse/timeval.h>
31 #include <pulse/util.h>
32 #include <pulse/xmalloc.h>
33 
34 #include <pulsecore/i18n.h>
35 #include <pulsecore/macro.h>
36 #include <pulsecore/sink.h>
37 #include <pulsecore/module.h>
38 #include <pulsecore/core-util.h>
39 #include <pulsecore/modargs.h>
40 #include <pulsecore/log.h>
41 #include <pulsecore/thread.h>
42 #include <pulsecore/thread-mq.h>
43 #include <pulsecore/rtpoll.h>
44 
45 PA_MODULE_AUTHOR("Lennart Poettering");
46 PA_MODULE_DESCRIPTION(_("Clocked NULL sink"));
47 PA_MODULE_VERSION(PACKAGE_VERSION);
48 PA_MODULE_LOAD_ONCE(false);
49 PA_MODULE_USAGE(
50         "sink_name=<name of sink> "
51         "sink_properties=<properties for the sink> "
52         "format=<sample format> "
53         "rate=<sample rate> "
54         "channels=<number of channels> "
55         "channel_map=<channel map>"
56         "formats=<semi-colon separated sink formats>"
57         "norewinds=<disable rewinds>");
58 
59 #define DEFAULT_SINK_NAME "null"
60 #define BLOCK_USEC (PA_USEC_PER_SEC * 2)
61 #define NOREWINDS_MAX_LATENCY_USEC (50*PA_USEC_PER_MSEC)
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     pa_idxset *formats;
76 
77     bool norewinds;
78 };
79 
80 static const char* const valid_modargs[] = {
81     "sink_name",
82     "sink_properties",
83     "format",
84     "rate",
85     "channels",
86     "channel_map",
87     "formats",
88     "norewinds",
89     NULL
90 };
91 
sink_process_msg(pa_msgobject * o,int code,void * data,int64_t offset,pa_memchunk * chunk)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_GET_LATENCY: {
103             pa_usec_t now;
104 
105             now = pa_rtclock_now();
106             *((int64_t*) data) = (int64_t)u->timestamp - (int64_t)now;
107 
108             return 0;
109         }
110     }
111 
112     return pa_sink_process_msg(o, code, data, offset, chunk);
113 }
114 
115 /* Called from the IO thread. */
sink_set_state_in_io_thread_cb(pa_sink * s,pa_sink_state_t new_state,pa_suspend_cause_t new_suspend_cause)116 static int sink_set_state_in_io_thread_cb(pa_sink *s, pa_sink_state_t new_state, pa_suspend_cause_t new_suspend_cause) {
117     struct userdata *u;
118 
119     pa_assert(s);
120     pa_assert_se(u = s->userdata);
121 
122     if (s->thread_info.state == PA_SINK_SUSPENDED || s->thread_info.state == PA_SINK_INIT) {
123         if (PA_SINK_IS_OPENED(new_state))
124             u->timestamp = pa_rtclock_now();
125     }
126 
127     return 0;
128 }
129 
sink_update_requested_latency_cb(pa_sink * s)130 static void sink_update_requested_latency_cb(pa_sink *s) {
131     struct userdata *u;
132     size_t nbytes;
133 
134     pa_sink_assert_ref(s);
135     pa_assert_se(u = s->userdata);
136 
137     u->block_usec = pa_sink_get_requested_latency_within_thread(s);
138 
139     if (u->block_usec == (pa_usec_t) -1)
140         u->block_usec = s->thread_info.max_latency;
141 
142     nbytes = pa_usec_to_bytes(u->block_usec, &s->sample_spec);
143 
144     if(u->norewinds){
145         pa_sink_set_max_rewind_within_thread(s, 0);
146     } else {
147         pa_sink_set_max_rewind_within_thread(s, nbytes);
148     }
149 
150     pa_sink_set_max_request_within_thread(s, nbytes);
151 }
152 
sink_reconfigure_cb(pa_sink * s,pa_sample_spec * spec,bool passthrough)153 static void sink_reconfigure_cb(pa_sink *s, pa_sample_spec *spec, bool passthrough) {
154     /* We don't need to do anything */
155     s->sample_spec = *spec;
156 }
157 
sink_set_formats_cb(pa_sink * s,pa_idxset * formats)158 static bool sink_set_formats_cb(pa_sink *s, pa_idxset *formats) {
159     struct userdata *u = s->userdata;
160 
161     pa_assert(u);
162 
163     pa_idxset_free(u->formats, (pa_free_cb_t) pa_format_info_free);
164     u->formats = pa_idxset_copy(formats, (pa_copy_func_t) pa_format_info_copy);
165 
166     return true;
167 }
168 
sink_get_formats_cb(pa_sink * s)169 static pa_idxset* sink_get_formats_cb(pa_sink *s) {
170     struct userdata *u = s->userdata;
171 
172     pa_assert(u);
173 
174     return pa_idxset_copy(u->formats, (pa_copy_func_t) pa_format_info_copy);
175 }
176 
process_rewind(struct userdata * u,pa_usec_t now)177 static void process_rewind(struct userdata *u, pa_usec_t now) {
178     size_t rewind_nbytes, in_buffer;
179     pa_usec_t delay;
180 
181     pa_assert(u);
182 
183     rewind_nbytes = u->sink->thread_info.rewind_nbytes;
184 
185     if (!PA_SINK_IS_OPENED(u->sink->thread_info.state) || rewind_nbytes <= 0)
186         goto do_nothing;
187 
188     pa_log_debug("Requested to rewind %lu bytes.", (unsigned long) rewind_nbytes);
189 
190     if (u->timestamp <= now)
191         goto do_nothing;
192 
193     delay = u->timestamp - now;
194     in_buffer = pa_usec_to_bytes(delay, &u->sink->sample_spec);
195 
196     if (in_buffer <= 0)
197         goto do_nothing;
198 
199     if (rewind_nbytes > in_buffer)
200         rewind_nbytes = in_buffer;
201 
202     pa_sink_process_rewind(u->sink, rewind_nbytes);
203     u->timestamp -= pa_bytes_to_usec(rewind_nbytes, &u->sink->sample_spec);
204 
205     pa_log_debug("Rewound %lu bytes.", (unsigned long) rewind_nbytes);
206     return;
207 
208 do_nothing:
209 
210     pa_sink_process_rewind(u->sink, 0);
211 }
212 
process_render(struct userdata * u,pa_usec_t now)213 static void process_render(struct userdata *u, pa_usec_t now) {
214     size_t ate = 0;
215 
216     pa_assert(u);
217 
218     /* This is the configured latency. Sink inputs connected to us
219     might not have a single frame more than the maxrequest value
220     queued. Hence: at maximum read this many bytes from the sink
221     inputs. */
222 
223     /* Fill the buffer up the latency size */
224     while (u->timestamp < now + u->block_usec) {
225         pa_memchunk chunk;
226 
227         pa_sink_render(u->sink, u->sink->thread_info.max_request, &chunk);
228         pa_memblock_unref(chunk.memblock);
229 
230 /*         pa_log_debug("Ate %lu bytes.", (unsigned long) chunk.length); */
231         u->timestamp += pa_bytes_to_usec(chunk.length, &u->sink->sample_spec);
232 
233         ate += chunk.length;
234 
235         if (ate >= u->sink->thread_info.max_request)
236             break;
237     }
238 
239 /*     pa_log_debug("Ate in sum %lu bytes (of %lu)", (unsigned long) ate, (unsigned long) nbytes); */
240 }
241 
thread_func(void * userdata)242 static void thread_func(void *userdata) {
243     struct userdata *u = userdata;
244 
245     pa_assert(u);
246 
247     pa_log_debug("Thread starting up");
248 
249     if (u->core->realtime_scheduling)
250         pa_thread_make_realtime(u->core->realtime_priority);
251 
252     pa_thread_mq_install(&u->thread_mq);
253 
254     u->timestamp = pa_rtclock_now();
255 
256     for (;;) {
257         pa_usec_t now = 0;
258         int ret;
259 
260         if (PA_SINK_IS_OPENED(u->sink->thread_info.state))
261             now = pa_rtclock_now();
262 
263         if (PA_UNLIKELY(u->sink->thread_info.rewind_requested))
264             process_rewind(u, now);
265 
266         /* Render some data and drop it immediately */
267         if (PA_SINK_IS_OPENED(u->sink->thread_info.state)) {
268             if (u->timestamp <= now)
269                 process_render(u, now);
270 
271             pa_rtpoll_set_timer_absolute(u->rtpoll, u->timestamp);
272         } else
273             pa_rtpoll_set_timer_disabled(u->rtpoll);
274 
275         /* Hmm, nothing to do. Let's sleep */
276         if ((ret = pa_rtpoll_run(u->rtpoll)) < 0)
277             goto fail;
278 
279         if (ret == 0)
280             goto finish;
281     }
282 
283 fail:
284     /* If this was no regular exit from the loop we have to continue
285      * processing messages until we received PA_MESSAGE_SHUTDOWN */
286     pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
287     pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
288 
289 finish:
290     pa_log_debug("Thread shutting down");
291 }
292 
pa__init(pa_module * m)293 int pa__init(pa_module*m) {
294     struct userdata *u = NULL;
295     pa_sample_spec ss;
296     pa_channel_map map;
297     pa_modargs *ma = NULL;
298     pa_sink_new_data data;
299     pa_format_info *format;
300     const char *formats;
301     size_t nbytes;
302 
303     pa_assert(m);
304 
305     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
306         pa_log("Failed to parse module arguments.");
307         goto fail;
308     }
309 
310     ss = m->core->default_sample_spec;
311     map = m->core->default_channel_map;
312     if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
313         pa_log("Invalid sample format specification or channel map");
314         goto fail;
315     }
316 
317     m->userdata = u = pa_xnew0(struct userdata, 1);
318     u->core = m->core;
319     u->module = m;
320     u->rtpoll = pa_rtpoll_new();
321 
322     if (pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll) < 0) {
323         pa_log("pa_thread_mq_init() failed.");
324         goto fail;
325     }
326 
327     pa_sink_new_data_init(&data);
328     data.driver = __FILE__;
329     data.module = m;
330     pa_sink_new_data_set_name(&data, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME));
331     pa_sink_new_data_set_sample_spec(&data, &ss);
332     pa_sink_new_data_set_channel_map(&data, &map);
333     pa_proplist_sets(data.proplist, PA_PROP_DEVICE_DESCRIPTION, _("Null Output"));
334     pa_proplist_sets(data.proplist, PA_PROP_DEVICE_CLASS, "abstract");
335 
336     u->formats = pa_idxset_new(NULL, NULL);
337     if ((formats = pa_modargs_get_value(ma, "formats", NULL))) {
338         char *f = NULL;
339         const char *state = NULL;
340 
341         while ((f = pa_split(formats, ";", &state))) {
342             format = pa_format_info_from_string(pa_strip(f));
343 
344             if (!format) {
345                 pa_log(_("Failed to set format: invalid format string %s"), f);
346 		pa_xfree(f);
347                 goto fail;
348             }
349             pa_xfree(f);
350 
351             pa_idxset_put(u->formats, format, NULL);
352         }
353     } else {
354         format = pa_format_info_new();
355         format->encoding = PA_ENCODING_PCM;
356         pa_idxset_put(u->formats, format, NULL);
357     }
358 
359     if (pa_modargs_get_proplist(ma, "sink_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
360         pa_log("Invalid properties");
361         pa_sink_new_data_done(&data);
362         goto fail;
363     }
364 
365     u->sink = pa_sink_new(m->core, &data, PA_SINK_LATENCY | PA_SINK_DYNAMIC_LATENCY | PA_SINK_SET_FORMATS);
366     pa_sink_new_data_done(&data);
367 
368     if (!u->sink) {
369         pa_log("Failed to create sink object.");
370         goto fail;
371     }
372 
373     u->sink->parent.process_msg = sink_process_msg;
374     u->sink->set_state_in_io_thread = sink_set_state_in_io_thread_cb;
375     u->sink->update_requested_latency = sink_update_requested_latency_cb;
376     u->sink->reconfigure = sink_reconfigure_cb;
377     u->sink->get_formats = sink_get_formats_cb;
378     u->sink->set_formats = sink_set_formats_cb;
379     u->sink->userdata = u;
380 
381     pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
382     pa_sink_set_rtpoll(u->sink, u->rtpoll);
383 
384     u->block_usec = BLOCK_USEC;
385     nbytes = pa_usec_to_bytes(u->block_usec, &u->sink->sample_spec);
386 
387     if(pa_modargs_get_value_boolean(ma, "norewinds", &u->norewinds) < 0){
388         pa_log("Invalid argument, norewinds expects a boolean value.");
389     }
390 
391     if(u->norewinds){
392         pa_sink_set_max_rewind(u->sink, 0);
393     } else {
394         pa_sink_set_max_rewind(u->sink, nbytes);
395     }
396 
397     pa_sink_set_max_request(u->sink, nbytes);
398 
399     if (!(u->thread = pa_thread_new("null-sink", thread_func, u))) {
400         pa_log("Failed to create thread.");
401         goto fail;
402     }
403 
404     if(u->norewinds){
405         pa_sink_set_latency_range(u->sink, 0, NOREWINDS_MAX_LATENCY_USEC);
406     } else {
407         pa_sink_set_latency_range(u->sink, 0, BLOCK_USEC);
408     }
409 
410     pa_sink_put(u->sink);
411 
412     pa_modargs_free(ma);
413 
414     return 0;
415 
416 fail:
417     if (ma)
418         pa_modargs_free(ma);
419 
420     pa__done(m);
421 
422     return -1;
423 }
424 
pa__get_n_used(pa_module * m)425 int pa__get_n_used(pa_module *m) {
426     struct userdata *u;
427 
428     pa_assert(m);
429     pa_assert_se(u = m->userdata);
430 
431     return pa_sink_linked_by(u->sink);
432 }
433 
pa__done(pa_module * m)434 void pa__done(pa_module*m) {
435     struct userdata *u;
436 
437     pa_assert(m);
438 
439     if (!(u = m->userdata))
440         return;
441 
442     if (u->sink)
443         pa_sink_unlink(u->sink);
444 
445     if (u->thread) {
446         pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
447         pa_thread_free(u->thread);
448     }
449 
450     pa_thread_mq_done(&u->thread_mq);
451 
452     if (u->sink)
453         pa_sink_unref(u->sink);
454 
455     if (u->rtpoll)
456         pa_rtpoll_free(u->rtpoll);
457 
458     if (u->formats)
459         pa_idxset_free(u->formats, (pa_free_cb_t) pa_format_info_free);
460 
461     pa_xfree(u);
462 }
463