]> code.delx.au - pulseaudio/blob - src/modules/module-sine-source.c
rtclock: fix issues found by Lennart
[pulseaudio] / src / modules / module-sine-source.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 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 <sys/stat.h>
28 #include <stdio.h>
29 #include <errno.h>
30 #include <string.h>
31 #include <fcntl.h>
32 #include <unistd.h>
33 #include <limits.h>
34 #include <sys/ioctl.h>
35 #include <sys/poll.h>
36
37 #include <pulse/rtclock.h>
38 #include <pulse/timeval.h>
39 #include <pulse/xmalloc.h>
40
41 #include <pulsecore/core-error.h>
42 #include <pulsecore/source.h>
43 #include <pulsecore/module.h>
44 #include <pulsecore/core-rtclock.h>
45 #include <pulsecore/core-util.h>
46 #include <pulsecore/modargs.h>
47 #include <pulsecore/log.h>
48 #include <pulsecore/thread.h>
49 #include <pulsecore/thread-mq.h>
50 #include <pulsecore/rtpoll.h>
51
52 #include "module-sine-source-symdef.h"
53
54 PA_MODULE_AUTHOR("Lennart Poettering");
55 PA_MODULE_DESCRIPTION("Sine wave generator source");
56 PA_MODULE_VERSION(PACKAGE_VERSION);
57 PA_MODULE_LOAD_ONCE(FALSE);
58 PA_MODULE_USAGE(
59 "source_name=<name for the source> "
60 "source_properties=<properties for the source> "
61 "rate=<sample rate> "
62 "frequency=<frequency in Hz>");
63
64 #define DEFAULT_SOURCE_NAME "sine_input"
65 #define BLOCK_USEC (PA_USEC_PER_SEC * 2)
66
67 struct userdata {
68 pa_core *core;
69 pa_module *module;
70 pa_source *source;
71
72 pa_thread *thread;
73 pa_thread_mq thread_mq;
74 pa_rtpoll *rtpoll;
75
76 pa_memchunk memchunk;
77 size_t peek_index;
78
79 pa_usec_t block_usec; /* how much to push at once */
80 pa_usec_t timestamp; /* when to push next */
81 };
82
83 static const char* const valid_modargs[] = {
84 "source_name",
85 "source_properties",
86 "rate",
87 "frequency",
88 NULL
89 };
90
91 static int source_process_msg(
92 pa_msgobject *o,
93 int code,
94 void *data,
95 int64_t offset,
96 pa_memchunk *chunk) {
97
98 struct userdata *u = PA_SOURCE(o)->userdata;
99
100 switch (code) {
101
102 case PA_SOURCE_MESSAGE_SET_STATE:
103
104 if (PA_PTR_TO_UINT(data) == PA_SOURCE_RUNNING)
105 u->timestamp = pa_rtclock_now();
106
107 break;
108
109 case PA_SOURCE_MESSAGE_GET_LATENCY: {
110 pa_usec_t now, left_to_fill;
111
112 now = pa_rtclock_now();
113 left_to_fill = u->timestamp > now ? u->timestamp - now : 0ULL;
114
115 *((pa_usec_t*) data) = u->block_usec > left_to_fill ? u->block_usec - left_to_fill : 0ULL;
116
117 return 0;
118 }
119 }
120
121 return pa_source_process_msg(o, code, data, offset, chunk);
122 }
123
124 static void source_update_requested_latency_cb(pa_source *s) {
125 struct userdata *u;
126
127 pa_source_assert_ref(s);
128 pa_assert_se(u = s->userdata);
129
130 u->block_usec = pa_source_get_requested_latency_within_thread(s);
131
132 if (u->block_usec == (pa_usec_t) -1)
133 u->block_usec = s->thread_info.max_latency;
134
135 pa_log_debug("new block msec = %lu", (unsigned long) (u->block_usec / PA_USEC_PER_MSEC));
136 }
137
138 static void process_render(struct userdata *u, pa_usec_t now) {
139 pa_assert(u);
140
141 while (u->timestamp < now + u->block_usec) {
142 pa_memchunk chunk;
143 size_t k;
144
145 k = pa_usec_to_bytes_round_up(now + u->block_usec - u->timestamp, &u->source->sample_spec);
146
147 chunk = u->memchunk;
148 chunk.index += u->peek_index;
149 chunk.length = PA_MIN(chunk.length - u->peek_index, k);
150
151 /* pa_log_debug("posting %lu", (unsigned long) chunk.length); */
152 pa_source_post(u->source, &chunk);
153
154 u->peek_index += chunk.length;
155 while (u->peek_index >= u->memchunk.length)
156 u->peek_index -= u->memchunk.length;
157
158 u->timestamp += pa_bytes_to_usec(chunk.length, &u->source->sample_spec);
159 }
160 }
161
162 static void thread_func(void *userdata) {
163 struct userdata *u = userdata;
164
165 pa_assert(u);
166
167 pa_log_debug("Thread starting up");
168
169 pa_thread_mq_install(&u->thread_mq);
170 pa_rtpoll_install(u->rtpoll);
171
172 u->timestamp = pa_rtclock_now();
173
174 for (;;) {
175 int ret;
176
177 if (PA_SOURCE_IS_OPENED(u->source->thread_info.state)) {
178 pa_usec_t now;
179
180 now = pa_rtclock_now();
181
182 if (u->timestamp <= now)
183 process_render(u, now);
184
185 pa_rtpoll_set_timer_absolute(u->rtpoll, u->timestamp);
186 } else
187 pa_rtpoll_set_timer_disabled(u->rtpoll);
188
189 /* Hmm, nothing to do. Let's sleep */
190 if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0)
191 goto fail;
192
193 if (ret == 0)
194 goto finish;
195 }
196
197 fail:
198 /* If this was no regular exit from the loop we have to continue
199 * processing messages until we received PA_MESSAGE_SHUTDOWN */
200 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
201 pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
202
203 finish:
204 pa_log_debug("Thread shutting down");
205 }
206
207 int pa__init(pa_module*m) {
208 struct userdata *u;
209 pa_modargs *ma;
210 pa_source_new_data data;
211 uint32_t frequency;
212 pa_sample_spec ss;
213
214 pa_assert(m);
215
216 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
217 pa_log("failed to parse module arguments.");
218 goto fail;
219 }
220
221 ss.format = PA_SAMPLE_FLOAT32;
222 ss.channels = 1;
223 ss.rate = 44100;
224
225 if (pa_modargs_get_value_u32(ma, "rate", &ss.rate) < 0 || ss.rate <= 1) {
226 pa_log("Invalid rate specification");
227 goto fail;
228 }
229
230 frequency = 440;
231 if (pa_modargs_get_value_u32(ma, "frequency", &frequency) < 0 || frequency < 1 || frequency > ss.rate/2) {
232 pa_log("Invalid frequency specification");
233 goto fail;
234 }
235
236 m->userdata = u = pa_xnew0(struct userdata, 1);
237 u->core = m->core;
238 u->module = m;
239 u->rtpoll = pa_rtpoll_new();
240 pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
241
242 u->peek_index = 0;
243 pa_memchunk_sine(&u->memchunk, m->core->mempool, ss.rate, frequency);
244
245 pa_source_new_data_init(&data);
246 data.driver = __FILE__;
247 data.module = m;
248 pa_source_new_data_set_name(&data, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME));
249 pa_proplist_setf(data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Sine source at %u Hz", (unsigned) frequency);
250 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_CLASS, "abstract");
251 pa_proplist_setf(data.proplist, "sine.hz", "%u", frequency);
252 pa_source_new_data_set_sample_spec(&data, &ss);
253
254 if (pa_modargs_get_proplist(ma, "source_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
255 pa_log("Invalid properties");
256 pa_source_new_data_done(&data);
257 goto fail;
258 }
259
260 u->source = pa_source_new(m->core, &data, PA_SOURCE_LATENCY);
261 pa_source_new_data_done(&data);
262
263 if (!u->source) {
264 pa_log("Failed to create source.");
265 goto fail;
266 }
267
268 u->source->parent.process_msg = source_process_msg;
269 u->source->update_requested_latency = source_update_requested_latency_cb;
270 u->source->userdata = u;
271
272 u->block_usec = BLOCK_USEC;
273
274 pa_source_set_asyncmsgq(u->source, u->thread_mq.inq);
275 pa_source_set_rtpoll(u->source, u->rtpoll);
276 pa_source_set_fixed_latency(u->source, u->block_usec);
277
278 if (!(u->thread = pa_thread_new(thread_func, u))) {
279 pa_log("Failed to create thread.");
280 goto fail;
281 }
282
283 pa_source_put(u->source);
284
285 pa_modargs_free(ma);
286
287 return 0;
288
289 fail:
290 if (ma)
291 pa_modargs_free(ma);
292
293 pa__done(m);
294
295 return -1;
296 }
297
298 int pa__get_n_used(pa_module *m) {
299 struct userdata *u;
300
301 pa_assert(m);
302 pa_assert_se(u = m->userdata);
303
304 return pa_source_linked_by(u->source);
305 }
306
307 void pa__done(pa_module*m) {
308 struct userdata *u;
309
310 pa_assert(m);
311
312 if (!(u = m->userdata))
313 return;
314
315 if (u->source)
316 pa_source_unlink(u->source);
317
318 if (u->thread) {
319 pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
320 pa_thread_free(u->thread);
321 }
322
323 pa_thread_mq_done(&u->thread_mq);
324
325 if (u->source)
326 pa_source_unref(u->source);
327
328 if (u->memchunk.memblock)
329 pa_memblock_unref(u->memchunk.memblock);
330
331 if (u->rtpoll)
332 pa_rtpoll_free(u->rtpoll);
333
334 pa_xfree(u);
335 }