2 This file is part of PulseAudio.
4 Copyright 2008 Lennart Poettering
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.
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.
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
34 #include <sys/ioctl.h>
37 #include <pulse/xmalloc.h>
38 #include <pulse/timeval.h>
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>
51 #include "module-sine-source-symdef.h"
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
);
59 "source_name=<name for the source> "
60 "frequency=<frequency in Hz>");
62 #define DEFAULT_SOURCE_NAME "sine_input"
63 #define BLOCK_USEC (PA_USEC_PER_SEC * 2)
71 pa_thread_mq thread_mq
;
77 pa_usec_t block_usec
; /* how much to push at once */
78 pa_usec_t timestamp
; /* when to push next */
81 static const char* const valid_modargs
[] = {
88 static int source_process_msg(
95 struct userdata
*u
= PA_SOURCE(o
)->userdata
;
99 case PA_SOURCE_MESSAGE_SET_STATE
:
101 if (PA_PTR_TO_UINT(data
) == PA_SOURCE_RUNNING
)
102 u
->timestamp
= pa_rtclock_usec();
106 case PA_SOURCE_MESSAGE_GET_LATENCY
: {
107 pa_usec_t now
, left_to_fill
;
109 now
= pa_rtclock_usec();
110 left_to_fill
= u
->timestamp
> now
? u
->timestamp
- now
: 0ULL;
112 *((pa_usec_t
*) data
) = u
->block_usec
> left_to_fill
? u
->block_usec
- left_to_fill
: 0ULL;
118 return pa_source_process_msg(o
, code
, data
, offset
, chunk
);
121 static void source_update_requested_latency_cb(pa_source
*s
) {
124 pa_source_assert_ref(s
);
125 pa_assert_se(u
= s
->userdata
);
127 u
->block_usec
= pa_source_get_requested_latency_within_thread(s
);
129 if (u
->block_usec
== (pa_usec_t
) -1)
130 u
->block_usec
= s
->thread_info
.max_latency
;
132 pa_log_debug("new block msec = %lu", (unsigned long) (u
->block_usec
/ PA_USEC_PER_MSEC
));
135 static void process_render(struct userdata
*u
, pa_usec_t now
) {
138 while (u
->timestamp
< now
+ u
->block_usec
) {
142 k
= pa_usec_to_bytes_round_up(now
+ u
->block_usec
- u
->timestamp
, &u
->source
->sample_spec
);
145 chunk
.index
+= u
->peek_index
;
146 chunk
.length
= PA_MIN(chunk
.length
- u
->peek_index
, k
);
148 /* pa_log_debug("posting %lu", (unsigned long) chunk.length); */
149 pa_source_post(u
->source
, &chunk
);
151 u
->peek_index
+= chunk
.length
;
152 while (u
->peek_index
>= u
->memchunk
.length
)
153 u
->peek_index
-= u
->memchunk
.length
;
155 u
->timestamp
+= pa_bytes_to_usec(chunk
.length
, &u
->source
->sample_spec
);
159 static void thread_func(void *userdata
) {
160 struct userdata
*u
= userdata
;
164 pa_log_debug("Thread starting up");
166 pa_thread_mq_install(&u
->thread_mq
);
167 pa_rtpoll_install(u
->rtpoll
);
169 u
->timestamp
= pa_rtclock_usec();
174 if (PA_SOURCE_IS_OPENED(u
->source
->thread_info
.state
)) {
177 now
= pa_rtclock_usec();
179 if (u
->timestamp
<= now
)
180 process_render(u
, now
);
182 pa_rtpoll_set_timer_absolute(u
->rtpoll
, u
->timestamp
);
184 pa_rtpoll_set_timer_disabled(u
->rtpoll
);
186 /* Hmm, nothing to do. Let's sleep */
187 if ((ret
= pa_rtpoll_run(u
->rtpoll
, TRUE
)) < 0)
195 /* If this was no regular exit from the loop we have to continue
196 * processing messages until we received PA_MESSAGE_SHUTDOWN */
197 pa_asyncmsgq_post(u
->thread_mq
.outq
, PA_MSGOBJECT(u
->core
), PA_CORE_MESSAGE_UNLOAD_MODULE
, u
->module
, 0, NULL
, NULL
);
198 pa_asyncmsgq_wait_for(u
->thread_mq
.inq
, PA_MESSAGE_SHUTDOWN
);
201 pa_log_debug("Thread shutting down");
204 int pa__init(pa_module
*m
) {
207 pa_source_new_data data
;
213 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
214 pa_log("failed to parse module arguments.");
218 ss
.format
= PA_SAMPLE_FLOAT32
;
222 if (pa_modargs_get_value_u32(ma
, "rate", &ss
.rate
) < 0 || ss
.rate
<= 1) {
223 pa_log("Invalid rate specification");
228 if (pa_modargs_get_value_u32(ma
, "frequency", &frequency
) < 0 || frequency
< 1 || frequency
> ss
.rate
/2) {
229 pa_log("Invalid frequency specification");
233 m
->userdata
= u
= pa_xnew0(struct userdata
, 1);
236 u
->rtpoll
= pa_rtpoll_new();
237 pa_thread_mq_init(&u
->thread_mq
, m
->core
->mainloop
, u
->rtpoll
);
240 pa_memchunk_sine(&u
->memchunk
, m
->core
->mempool
, ss
.rate
, frequency
);
242 pa_source_new_data_init(&data
);
243 data
.driver
= __FILE__
;
245 pa_source_new_data_set_name(&data
, pa_modargs_get_value(ma
, "source_name", DEFAULT_SOURCE_NAME
));
246 pa_proplist_setf(data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
, "Sine source at %u Hz", (unsigned) frequency
);
247 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_CLASS
, "abstract");
248 pa_proplist_setf(data
.proplist
, "sine.hz", "%u", frequency
);
249 pa_source_new_data_set_sample_spec(&data
, &ss
);
251 u
->source
= pa_source_new(m
->core
, &data
, PA_SOURCE_LATENCY
);
252 pa_source_new_data_done(&data
);
255 pa_log("Failed to create source.");
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
;
263 u
->block_usec
= BLOCK_USEC
;
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
);
269 if (!(u
->thread
= pa_thread_new(thread_func
, u
))) {
270 pa_log("Failed to create thread.");
274 pa_source_put(u
->source
);
289 int pa__get_n_used(pa_module
*m
) {
293 pa_assert_se(u
= m
->userdata
);
295 return pa_source_linked_by(u
->source
);
298 void pa__done(pa_module
*m
) {
303 if (!(u
= m
->userdata
))
307 pa_source_unlink(u
->source
);
310 pa_asyncmsgq_send(u
->thread_mq
.inq
, NULL
, PA_MESSAGE_SHUTDOWN
, NULL
, 0, NULL
);
311 pa_thread_free(u
->thread
);
314 pa_thread_mq_done(&u
->thread_mq
);
317 pa_source_unref(u
->source
);
319 if (u
->memchunk
.memblock
)
320 pa_memblock_unref(u
->memchunk
.memblock
);
323 pa_rtpoll_free(u
->rtpoll
);