1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2008 Lennart Poettering
5 Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
6
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.
11
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.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <errno.h>
28 #include <unistd.h>
29
30 #include <pulse/rtclock.h>
31 #include <pulse/timeval.h>
32 #include <pulse/util.h>
33 #include <pulse/xmalloc.h>
34
35 #include <pulsecore/core-util.h>
36 #include <pulsecore/log.h>
37 #include <pulsecore/macro.h>
38 #include <pulsecore/modargs.h>
39 #include <pulsecore/module.h>
40 #include <pulsecore/rtpoll.h>
41 #include <pulsecore/source.h>
42 #include <pulsecore/thread-mq.h>
43 #include <pulsecore/thread.h>
44
45 PA_MODULE_AUTHOR("Lennart Poettering & Marc-Andre Lureau");
46 PA_MODULE_DESCRIPTION("Clocked NULL source");
47 PA_MODULE_VERSION(PACKAGE_VERSION);
48 PA_MODULE_LOAD_ONCE(false);
49 PA_MODULE_USAGE(
50 "format=<sample format> "
51 "channels=<number of channels> "
52 "rate=<sample rate> "
53 "source_name=<name of source> "
54 "channel_map=<channel map> "
55 "description=<description for the source> ");
56
57 #define DEFAULT_SOURCE_NAME "source.null"
58 #define MAX_LATENCY_USEC (PA_USEC_PER_SEC * 2)
59 #define MIN_LATENCY_USEC (500)
60
61 struct userdata {
62 pa_core *core;
63 pa_module *module;
64 pa_source *source;
65
66 pa_thread *thread;
67 pa_thread_mq thread_mq;
68 pa_rtpoll *rtpoll;
69
70 size_t block_size;
71
72 pa_usec_t block_usec;
73 pa_usec_t timestamp;
74 };
75
76 static const char* const valid_modargs[] = {
77 "rate",
78 "format",
79 "channels",
80 "source_name",
81 "channel_map",
82 "description",
83 NULL
84 };
85
source_process_msg(pa_msgobject * o,int code,void * data,int64_t offset,pa_memchunk * chunk)86 static int source_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
87 struct userdata *u = PA_SOURCE(o)->userdata;
88
89 switch (code) {
90 case PA_SOURCE_MESSAGE_GET_LATENCY: {
91 pa_usec_t now;
92
93 now = pa_rtclock_now();
94 *((int64_t*) data) = (int64_t)now - (int64_t)u->timestamp;
95
96 return 0;
97 }
98 }
99
100 return pa_source_process_msg(o, code, data, offset, chunk);
101 }
102
103 /* Called from the IO thread. */
source_set_state_in_io_thread_cb(pa_source * s,pa_source_state_t new_state,pa_suspend_cause_t new_suspend_cause)104 static int source_set_state_in_io_thread_cb(pa_source *s, pa_source_state_t new_state, pa_suspend_cause_t new_suspend_cause) {
105 struct userdata *u;
106
107 pa_assert(s);
108 pa_assert_se(u = s->userdata);
109
110 if (s->thread_info.state == PA_SOURCE_SUSPENDED || s->thread_info.state == PA_SOURCE_INIT) {
111 if (PA_SOURCE_IS_OPENED(new_state))
112 u->timestamp = pa_rtclock_now();
113 }
114
115 return 0;
116 }
117
source_update_requested_latency_cb(pa_source * s)118 static void source_update_requested_latency_cb(pa_source *s) {
119 struct userdata *u;
120
121 pa_source_assert_ref(s);
122 u = s->userdata;
123 pa_assert(u);
124
125 u->block_usec = pa_source_get_requested_latency_within_thread(s);
126 if (u->block_usec == (pa_usec_t)-1)
127 u->block_usec = u->source->thread_info.max_latency;
128 }
129
thread_func(void * userdata)130 static void thread_func(void *userdata) {
131 struct userdata *u = userdata;
132 bool timer_elapsed = false;
133 size_t max_block_size;
134
135 pa_assert(u);
136
137 pa_log_debug("Thread starting up");
138
139 if (u->core->realtime_scheduling)
140 pa_thread_make_realtime(u->core->realtime_priority);
141
142 pa_thread_mq_install(&u->thread_mq);
143
144 max_block_size = pa_frame_align(pa_mempool_block_size_max(u->core->mempool), &u->source->sample_spec);
145 u->timestamp = pa_rtclock_now();
146
147 for (;;) {
148 int ret;
149
150 /* Generate some null data */
151 if (PA_SOURCE_IS_OPENED(u->source->thread_info.state)) {
152 pa_usec_t now;
153 pa_memchunk chunk;
154
155 now = pa_rtclock_now();
156
157 if (timer_elapsed && (chunk.length = pa_usec_to_bytes(now - u->timestamp, &u->source->sample_spec)) > 0) {
158
159 chunk.length = PA_MIN(max_block_size, chunk.length);
160
161 chunk.memblock = pa_memblock_new(u->core->mempool, chunk.length);
162 chunk.index = 0;
163 pa_silence_memchunk(&chunk, &u->source->sample_spec);
164 pa_source_post(u->source, &chunk);
165 pa_memblock_unref(chunk.memblock);
166
167 u->timestamp += pa_bytes_to_usec(chunk.length, &u->source->sample_spec);
168 }
169
170 pa_rtpoll_set_timer_absolute(u->rtpoll, u->timestamp + u->block_usec);
171 } else
172 pa_rtpoll_set_timer_disabled(u->rtpoll);
173
174 /* Hmm, nothing to do. Let's sleep */
175 if ((ret = pa_rtpoll_run(u->rtpoll)) < 0)
176 goto fail;
177
178 timer_elapsed = pa_rtpoll_timer_elapsed(u->rtpoll);
179
180 if (ret == 0)
181 goto finish;
182 }
183
184 fail:
185 /* If this was no regular exit from the loop we have to continue
186 * processing messages until we received PA_MESSAGE_SHUTDOWN */
187 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
188 pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
189
190 finish:
191 pa_log_debug("Thread shutting down");
192 }
193
pa__init(pa_module * m)194 int pa__init(pa_module*m) {
195 struct userdata *u = NULL;
196 pa_sample_spec ss;
197 pa_channel_map map;
198 pa_modargs *ma = NULL;
199 pa_source_new_data data;
200
201 pa_assert(m);
202
203 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
204 pa_log("Failed to parse module arguments.");
205 goto fail;
206 }
207
208 ss = m->core->default_sample_spec;
209 map = m->core->default_channel_map;
210 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
211 pa_log("Invalid sample format specification or channel map");
212 goto fail;
213 }
214
215 m->userdata = u = pa_xnew0(struct userdata, 1);
216 u->core = m->core;
217 u->module = m;
218 u->rtpoll = pa_rtpoll_new();
219
220 if (pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll) < 0) {
221 pa_log("pa_thread_mq_init() failed.");
222 goto fail;
223 }
224
225 pa_source_new_data_init(&data);
226 data.driver = __FILE__;
227 data.module = m;
228 pa_source_new_data_set_name(&data, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME));
229 pa_source_new_data_set_sample_spec(&data, &ss);
230 pa_source_new_data_set_channel_map(&data, &map);
231 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_DESCRIPTION, pa_modargs_get_value(ma, "description", "Null Input"));
232 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_CLASS, "abstract");
233
234 u->source = pa_source_new(m->core, &data, PA_SOURCE_LATENCY | PA_SOURCE_DYNAMIC_LATENCY);
235 pa_source_new_data_done(&data);
236
237 if (!u->source) {
238 pa_log("Failed to create source object.");
239 goto fail;
240 }
241
242 u->source->parent.process_msg = source_process_msg;
243 u->source->set_state_in_io_thread = source_set_state_in_io_thread_cb;
244 u->source->update_requested_latency = source_update_requested_latency_cb;
245 u->source->userdata = u;
246
247 pa_source_set_asyncmsgq(u->source, u->thread_mq.inq);
248 pa_source_set_rtpoll(u->source, u->rtpoll);
249
250 pa_source_set_latency_range(u->source, MIN_LATENCY_USEC, MAX_LATENCY_USEC);
251 u->block_usec = u->source->thread_info.max_latency;
252
253 u->source->thread_info.max_rewind =
254 pa_usec_to_bytes(u->block_usec, &u->source->sample_spec);
255
256 if (!(u->thread = pa_thread_new("null-source", thread_func, u))) {
257 pa_log("Failed to create thread.");
258 goto fail;
259 }
260
261 pa_source_put(u->source);
262
263 pa_modargs_free(ma);
264
265 return 0;
266
267 fail:
268 if (ma)
269 pa_modargs_free(ma);
270
271 pa__done(m);
272
273 return -1;
274 }
275
pa__done(pa_module * m)276 void pa__done(pa_module*m) {
277 struct userdata *u;
278
279 pa_assert(m);
280
281 if (!(u = m->userdata))
282 return;
283
284 if (u->source)
285 pa_source_unlink(u->source);
286
287 if (u->thread) {
288 pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
289 pa_thread_free(u->thread);
290 }
291
292 pa_thread_mq_done(&u->thread_mq);
293
294 if (u->source)
295 pa_source_unref(u->source);
296
297 if (u->rtpoll)
298 pa_rtpoll_free(u->rtpoll);
299
300 pa_xfree(u);
301 }
302