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