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