4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
37 #include <pulse/timeval.h>
38 #include <pulse/xmalloc.h>
40 #include <pulsecore/macro.h>
41 #include <pulsecore/sink.h>
42 #include <pulsecore/module.h>
43 #include <pulsecore/core-util.h>
44 #include <pulsecore/core-error.h>
45 #include <pulsecore/modargs.h>
46 #include <pulsecore/log.h>
47 #include <pulsecore/thread.h>
48 #include <pulsecore/thread-mq.h>
49 #include <pulsecore/rtpoll.h>
50 #include <pulsecore/rtclock.h>
52 #include "module-null-sink-symdef.h"
54 PA_MODULE_AUTHOR("Lennart Poettering")
55 PA_MODULE_DESCRIPTION("Clocked NULL sink")
56 PA_MODULE_VERSION(PACKAGE_VERSION
)
58 "format=<sample format> "
59 "channels=<number of channels> "
61 "sink_name=<name of sink>"
62 "channel_map=<channel map>"
63 "description=<description for the sink>")
65 #define DEFAULT_SINK_NAME "null"
73 pa_thread_mq thread_mq
;
78 struct timespec timestamp
;
81 static const char* const valid_modargs
[] = {
91 static int sink_process_msg(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
92 struct userdata
*u
= PA_SINK(o
)->userdata
;
95 case PA_SINK_MESSAGE_SET_STATE
:
97 if (PA_PTR_TO_UINT(data
) == PA_SINK_RUNNING
)
98 pa_rtclock_get(&u
->timestamp
);
102 case PA_SINK_MESSAGE_GET_LATENCY
: {
105 pa_rtclock_get(&now
);
107 if (pa_timespec_cmp(&u
->timestamp
, &now
) > 0)
108 *((pa_usec_t
*) data
) = 0;
110 *((pa_usec_t
*) data
) = pa_timespec_diff(&u
->timestamp
, &now
);
115 return pa_sink_process_msg(o
, code
, data
, offset
, chunk
);
118 static void thread_func(void *userdata
) {
119 struct userdata
*u
= userdata
;
123 pa_log_debug("Thread starting up");
125 pa_thread_mq_install(&u
->thread_mq
);
126 pa_rtpoll_install(u
->rtpoll
);
128 pa_rtclock_get(&u
->timestamp
);
133 /* Render some data and drop it immediately */
134 if (u
->sink
->thread_info
.state
== PA_SINK_RUNNING
) {
137 pa_rtclock_get(&now
);
139 if (pa_timespec_cmp(&u
->timestamp
, &now
) <= 0) {
140 pa_sink_skip(u
->sink
, u
->block_size
);
141 pa_timespec_add(&u
->timestamp
, pa_bytes_to_usec(u
->block_size
, &u
->sink
->sample_spec
));
144 pa_rtpoll_set_timer_absolute(u
->rtpoll
, &u
->timestamp
);
146 pa_rtpoll_set_timer_disabled(u
->rtpoll
);
148 /* Now give the sink inputs some to time to process their data */
149 if ((ret
= pa_sink_process_inputs(u
->sink
)) < 0)
154 /* Check whether there is a message for us to process */
155 if ((ret
= pa_thread_mq_process(&u
->thread_mq
) < 0))
160 /* Hmm, nothing to do. Let's sleep */
161 if (pa_rtpoll_run(u
->rtpoll
, 1) < 0) {
162 pa_log("poll() failed: %s", pa_cstrerror(errno
));
168 /* We have to continue processing messages until we receive the
169 * SHUTDOWN message */
170 pa_asyncmsgq_post(u
->thread_mq
.outq
, PA_MSGOBJECT(u
->core
), PA_CORE_MESSAGE_UNLOAD_MODULE
, u
->module
, 0, NULL
, NULL
);
171 pa_asyncmsgq_wait_for(u
->thread_mq
.inq
, PA_MESSAGE_SHUTDOWN
);
174 pa_log_debug("Thread shutting down");
177 int pa__init(pa_module
*m
) {
178 struct userdata
*u
= NULL
;
181 pa_modargs
*ma
= NULL
;
185 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
186 pa_log("Failed to parse module arguments.");
190 ss
= m
->core
->default_sample_spec
;
191 if (pa_modargs_get_sample_spec_and_channel_map(ma
, &ss
, &map
, PA_CHANNEL_MAP_DEFAULT
) < 0) {
192 pa_log("Invalid sample format specification or channel map");
196 u
= pa_xnew0(struct userdata
, 1);
200 pa_thread_mq_init(&u
->thread_mq
, m
->core
->mainloop
);
201 u
->rtpoll
= pa_rtpoll_new();
202 pa_rtpoll_item_new_asyncmsgq(u
->rtpoll
, u
->thread_mq
.inq
);
204 if (!(u
->sink
= pa_sink_new(m
->core
, __FILE__
, pa_modargs_get_value(ma
, "sink_name", DEFAULT_SINK_NAME
), 0, &ss
, &map
))) {
205 pa_log("Failed to create sink.");
209 u
->sink
->parent
.process_msg
= sink_process_msg
;
210 u
->sink
->userdata
= u
;
211 u
->sink
->flags
= PA_SINK_LATENCY
;
213 pa_sink_set_module(u
->sink
, m
);
214 pa_sink_set_asyncmsgq(u
->sink
, u
->thread_mq
.inq
);
215 pa_sink_set_rtpoll(u
->sink
, u
->rtpoll
);
216 pa_sink_set_description(u
->sink
, pa_modargs_get_value(ma
, "description", "NULL sink"));
218 u
->block_size
= pa_bytes_per_second(&ss
) / 20; /* 50 ms */
219 if (u
->block_size
<= 0)
220 u
->block_size
= pa_frame_size(&ss
);
222 if (!(u
->thread
= pa_thread_new(thread_func
, u
))) {
223 pa_log("Failed to create thread.");
227 pa_sink_put(u
->sink
);
242 void pa__done(pa_module
*m
) {
247 if (!(u
= m
->userdata
))
251 pa_sink_unlink(u
->sink
);
254 pa_asyncmsgq_send(u
->thread_mq
.inq
, NULL
, PA_MESSAGE_SHUTDOWN
, NULL
, 0, NULL
);
255 pa_thread_free(u
->thread
);
258 pa_thread_mq_done(&u
->thread_mq
);
261 pa_sink_unref(u
->sink
);
264 pa_rtpoll_free(u
->rtpoll
);