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