2 This file is part of PulseAudio.
4 Copyright 2004-2008 Lennart Poettering
5 Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
32 #include <pulse/rtclock.h>
33 #include <pulse/timeval.h>
34 #include <pulse/xmalloc.h>
36 #include <pulsecore/core-util.h>
37 #include <pulsecore/log.h>
38 #include <pulsecore/macro.h>
39 #include <pulsecore/modargs.h>
40 #include <pulsecore/module.h>
41 #include <pulsecore/rtpoll.h>
42 #include <pulsecore/source.h>
43 #include <pulsecore/thread-mq.h>
44 #include <pulsecore/thread.h>
46 #include "module-null-source-symdef.h"
48 PA_MODULE_AUTHOR("Lennart Poettering & Marc-Andre Lureau");
49 PA_MODULE_DESCRIPTION("Clocked NULL source");
50 PA_MODULE_VERSION(PACKAGE_VERSION);
51 PA_MODULE_LOAD_ONCE(FALSE);
53 "format=<sample format> "
54 "channels=<number of channels> "
56 "source_name=<name of source> "
57 "channel_map=<channel map> "
58 "description=<description for the source> "
59 "latency_time=<latency time in ms>");
61 #define DEFAULT_SOURCE_NAME "source.null"
62 #define DEFAULT_LATENCY_TIME 20
63 #define MAX_LATENCY_USEC (PA_USEC_PER_SEC * 2)
71 pa_thread_mq thread_mq;
78 pa_usec_t latency_time;
81 static const char* const valid_modargs[] = {
92 static int source_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
93 struct userdata *u = PA_SOURCE(o)->userdata;
96 case PA_SOURCE_MESSAGE_SET_STATE:
98 if (PA_PTR_TO_UINT(data) == PA_SOURCE_RUNNING)
99 u->timestamp = pa_rtclock_now();
103 case PA_SOURCE_MESSAGE_GET_LATENCY: {
106 now = pa_rtclock_now();
107 *((pa_usec_t*) data) = u->timestamp > now ? u->timestamp - now : 0;
113 return pa_source_process_msg(o, code, data, offset, chunk);
116 static void source_update_requested_latency_cb(pa_source *s) {
119 pa_source_assert_ref(s);
123 u->block_usec = pa_source_get_requested_latency_within_thread(s);
126 static void thread_func(void *userdata) {
127 struct userdata *u = userdata;
131 pa_log_debug("Thread starting up");
133 pa_thread_mq_install(&u->thread_mq);
135 u->timestamp = pa_rtclock_now();
140 /* Generate some null data */
141 if (PA_SOURCE_IS_OPENED(u->source->thread_info.state)) {
145 now = pa_rtclock_now();
147 if ((chunk.length = pa_usec_to_bytes(now - u->timestamp, &u->source->sample_spec)) > 0) {
149 chunk.memblock = pa_memblock_new(u->core->mempool, (size_t) -1); /* or chunk.length? */
151 pa_source_post(u->source, &chunk);
152 pa_memblock_unref(chunk.memblock);
157 pa_rtpoll_set_timer_absolute(u->rtpoll, u->timestamp + u->latency_time * PA_USEC_PER_MSEC);
159 pa_rtpoll_set_timer_disabled(u->rtpoll);
161 /* Hmm, nothing to do. Let's sleep */
162 if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0)
170 /* If this was no regular exit from the loop we have to continue
171 * processing messages until we received PA_MESSAGE_SHUTDOWN */
172 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
173 pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
176 pa_log_debug("Thread shutting down");
179 int pa__init(pa_module*m) {
180 struct userdata *u = NULL;
183 pa_modargs *ma = NULL;
184 pa_source_new_data data;
185 uint32_t latency_time = DEFAULT_LATENCY_TIME;
189 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
190 pa_log("Failed to parse module arguments.");
194 ss = m->core->default_sample_spec;
195 map = m->core->default_channel_map;
196 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
197 pa_log("Invalid sample format specification or channel map");
201 m->userdata = u = pa_xnew0(struct userdata, 1);
204 u->rtpoll = pa_rtpoll_new();
205 pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
207 pa_source_new_data_init(&data);
208 data.driver = __FILE__;
210 pa_source_new_data_set_name(&data, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME));
211 pa_source_new_data_set_sample_spec(&data, &ss);
212 pa_source_new_data_set_channel_map(&data, &map);
213 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_DESCRIPTION, pa_modargs_get_value(ma, "description", "Null Input"));
214 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_CLASS, "abstract");
216 u->source = pa_source_new(m->core, &data, PA_SOURCE_LATENCY | PA_SOURCE_DYNAMIC_LATENCY);
217 pa_source_new_data_done(&data);
220 pa_log("Failed to create source object.");
224 u->latency_time = DEFAULT_LATENCY_TIME;
225 if (pa_modargs_get_value_u32(ma, "latency_time", &latency_time) < 0) {
226 pa_log("Failed to parse latency_time value.");
229 u->latency_time = latency_time;
231 u->source->parent.process_msg = source_process_msg;
232 u->source->update_requested_latency = source_update_requested_latency_cb;
233 u->source->userdata = u;
235 pa_source_set_asyncmsgq(u->source, u->thread_mq.inq);
236 pa_source_set_rtpoll(u->source, u->rtpoll);
238 pa_source_set_latency_range(u->source, 0, MAX_LATENCY_USEC);
239 u->block_usec = u->source->thread_info.max_latency;
241 u->source->thread_info.max_rewind =
242 pa_usec_to_bytes(u->block_usec, &u->source->sample_spec);
244 if (!(u->thread = pa_thread_new("null-source", thread_func, u))) {
245 pa_log("Failed to create thread.");
249 pa_source_put(u->source);
264 void pa__done(pa_module*m) {
269 if (!(u = m->userdata))
273 pa_source_unlink(u->source);
276 pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
277 pa_thread_free(u->thread);
280 pa_thread_mq_done(&u->thread_mq);
283 pa_source_unref(u->source);
286 pa_rtpoll_free(u->rtpoll);