2 This file is part of PulseAudio.
4 Copyright 2004-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
31 #include <pulse/rtclock.h>
32 #include <pulse/timeval.h>
33 #include <pulse/xmalloc.h>
35 #include <pulsecore/i18n.h>
36 #include <pulsecore/macro.h>
37 #include <pulsecore/sink.h>
38 #include <pulsecore/module.h>
39 #include <pulsecore/core-util.h>
40 #include <pulsecore/modargs.h>
41 #include <pulsecore/log.h>
42 #include <pulsecore/thread.h>
43 #include <pulsecore/thread-mq.h>
44 #include <pulsecore/rtpoll.h>
46 #include "module-null-sink-symdef.h"
48 PA_MODULE_AUTHOR("Lennart Poettering");
49 PA_MODULE_DESCRIPTION(_("Clocked NULL sink"));
50 PA_MODULE_VERSION(PACKAGE_VERSION
);
51 PA_MODULE_LOAD_ONCE(FALSE
);
53 "sink_name=<name of sink> "
54 "sink_properties=<properties for the sink> "
55 "format=<sample format> "
57 "channels=<number of channels> "
58 "channel_map=<channel map>");
60 #define DEFAULT_SINK_NAME "null"
61 #define BLOCK_USEC (PA_USEC_PER_SEC * 2)
69 pa_thread_mq thread_mq
;
76 static const char* const valid_modargs
[] = {
86 static int sink_process_msg(
93 struct userdata
*u
= PA_SINK(o
)->userdata
;
96 case PA_SINK_MESSAGE_SET_STATE
:
98 if (PA_PTR_TO_UINT(data
) == PA_SINK_RUNNING
)
99 u
->timestamp
= pa_rtclock_now();
103 case PA_SINK_MESSAGE_GET_LATENCY
: {
106 now
= pa_rtclock_now();
107 *((pa_usec_t
*) data
) = u
->timestamp
> now
? u
->timestamp
- now
: 0ULL;
113 return pa_sink_process_msg(o
, code
, data
, offset
, chunk
);
116 static void sink_update_requested_latency_cb(pa_sink
*s
) {
120 pa_sink_assert_ref(s
);
121 pa_assert_se(u
= s
->userdata
);
123 u
->block_usec
= pa_sink_get_requested_latency_within_thread(s
);
125 if (u
->block_usec
== (pa_usec_t
) -1)
126 u
->block_usec
= s
->thread_info
.max_latency
;
128 nbytes
= pa_usec_to_bytes(u
->block_usec
, &s
->sample_spec
);
129 pa_sink_set_max_rewind_within_thread(s
, nbytes
);
130 pa_sink_set_max_request_within_thread(s
, nbytes
);
133 static void process_rewind(struct userdata
*u
, pa_usec_t now
) {
134 size_t rewind_nbytes
, in_buffer
;
139 /* Figure out how much we shall rewind and reset the counter */
140 rewind_nbytes
= u
->sink
->thread_info
.rewind_nbytes
;
141 u
->sink
->thread_info
.rewind_nbytes
= 0;
143 pa_assert(rewind_nbytes
> 0);
144 pa_log_debug("Requested to rewind %lu bytes.", (unsigned long) rewind_nbytes
);
146 if (u
->timestamp
<= now
)
149 delay
= u
->timestamp
- now
;
150 in_buffer
= pa_usec_to_bytes(delay
, &u
->sink
->sample_spec
);
155 if (rewind_nbytes
> in_buffer
)
156 rewind_nbytes
= in_buffer
;
158 pa_sink_process_rewind(u
->sink
, rewind_nbytes
);
159 u
->timestamp
-= pa_bytes_to_usec(rewind_nbytes
, &u
->sink
->sample_spec
);
161 pa_log_debug("Rewound %lu bytes.", (unsigned long) rewind_nbytes
);
166 pa_sink_process_rewind(u
->sink
, 0);
169 static void process_render(struct userdata
*u
, pa_usec_t now
) {
174 /* This is the configured latency. Sink inputs connected to us
175 might not have a single frame more than the maxrequest value
176 queued. Hence: at maximum read this many bytes from the sink
179 /* Fill the buffer up the latency size */
180 while (u
->timestamp
< now
+ u
->block_usec
) {
183 pa_sink_render(u
->sink
, u
->sink
->thread_info
.max_request
, &chunk
);
184 pa_memblock_unref(chunk
.memblock
);
186 /* pa_log_debug("Ate %lu bytes.", (unsigned long) chunk.length); */
187 u
->timestamp
+= pa_bytes_to_usec(chunk
.length
, &u
->sink
->sample_spec
);
191 if (ate
>= u
->sink
->thread_info
.max_request
)
195 /* pa_log_debug("Ate in sum %lu bytes (of %lu)", (unsigned long) ate, (unsigned long) nbytes); */
198 static void thread_func(void *userdata
) {
199 struct userdata
*u
= userdata
;
203 pa_log_debug("Thread starting up");
205 pa_thread_mq_install(&u
->thread_mq
);
207 u
->timestamp
= pa_rtclock_now();
212 /* Render some data and drop it immediately */
213 if (PA_SINK_IS_OPENED(u
->sink
->thread_info
.state
)) {
216 now
= pa_rtclock_now();
218 if (u
->sink
->thread_info
.rewind_requested
) {
219 if (u
->sink
->thread_info
.rewind_nbytes
> 0)
220 process_rewind(u
, now
);
222 pa_sink_process_rewind(u
->sink
, 0);
225 if (u
->timestamp
<= now
)
226 process_render(u
, now
);
228 pa_rtpoll_set_timer_absolute(u
->rtpoll
, u
->timestamp
);
230 pa_rtpoll_set_timer_disabled(u
->rtpoll
);
232 /* Hmm, nothing to do. Let's sleep */
233 if ((ret
= pa_rtpoll_run(u
->rtpoll
, TRUE
)) < 0)
241 /* If this was no regular exit from the loop we have to continue
242 * processing messages until we received PA_MESSAGE_SHUTDOWN */
243 pa_asyncmsgq_post(u
->thread_mq
.outq
, PA_MSGOBJECT(u
->core
), PA_CORE_MESSAGE_UNLOAD_MODULE
, u
->module
, 0, NULL
, NULL
);
244 pa_asyncmsgq_wait_for(u
->thread_mq
.inq
, PA_MESSAGE_SHUTDOWN
);
247 pa_log_debug("Thread shutting down");
250 int pa__init(pa_module
*m
) {
251 struct userdata
*u
= NULL
;
254 pa_modargs
*ma
= NULL
;
255 pa_sink_new_data data
;
260 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
261 pa_log("Failed to parse module arguments.");
265 ss
= m
->core
->default_sample_spec
;
266 map
= m
->core
->default_channel_map
;
267 if (pa_modargs_get_sample_spec_and_channel_map(ma
, &ss
, &map
, PA_CHANNEL_MAP_DEFAULT
) < 0) {
268 pa_log("Invalid sample format specification or channel map");
272 m
->userdata
= u
= pa_xnew0(struct userdata
, 1);
275 u
->rtpoll
= pa_rtpoll_new();
276 pa_thread_mq_init(&u
->thread_mq
, m
->core
->mainloop
, u
->rtpoll
);
278 pa_sink_new_data_init(&data
);
279 data
.driver
= __FILE__
;
281 pa_sink_new_data_set_name(&data
, pa_modargs_get_value(ma
, "sink_name", DEFAULT_SINK_NAME
));
282 pa_sink_new_data_set_sample_spec(&data
, &ss
);
283 pa_sink_new_data_set_channel_map(&data
, &map
);
284 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
, _("Null Output"));
285 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_CLASS
, "abstract");
287 if (pa_modargs_get_proplist(ma
, "sink_properties", data
.proplist
, PA_UPDATE_REPLACE
) < 0) {
288 pa_log("Invalid properties");
289 pa_sink_new_data_done(&data
);
293 u
->sink
= pa_sink_new(m
->core
, &data
, PA_SINK_LATENCY
|PA_SINK_DYNAMIC_LATENCY
);
294 pa_sink_new_data_done(&data
);
297 pa_log("Failed to create sink object.");
301 u
->sink
->parent
.process_msg
= sink_process_msg
;
302 u
->sink
->update_requested_latency
= sink_update_requested_latency_cb
;
303 u
->sink
->userdata
= u
;
305 pa_sink_set_asyncmsgq(u
->sink
, u
->thread_mq
.inq
);
306 pa_sink_set_rtpoll(u
->sink
, u
->rtpoll
);
308 u
->block_usec
= BLOCK_USEC
;
309 nbytes
= pa_usec_to_bytes(u
->block_usec
, &u
->sink
->sample_spec
);
310 pa_sink_set_max_rewind(u
->sink
, nbytes
);
311 pa_sink_set_max_request(u
->sink
, nbytes
);
313 if (!(u
->thread
= pa_thread_new("null-sink", thread_func
, u
))) {
314 pa_log("Failed to create thread.");
318 pa_sink_set_latency_range(u
->sink
, 0, BLOCK_USEC
);
320 pa_sink_put(u
->sink
);
335 int pa__get_n_used(pa_module
*m
) {
339 pa_assert_se(u
= m
->userdata
);
341 return pa_sink_linked_by(u
->sink
);
344 void pa__done(pa_module
*m
) {
349 if (!(u
= m
->userdata
))
353 pa_sink_unlink(u
->sink
);
356 pa_asyncmsgq_send(u
->thread_mq
.inq
, NULL
, PA_MESSAGE_SHUTDOWN
, NULL
, 0, NULL
);
357 pa_thread_free(u
->thread
);
360 pa_thread_mq_done(&u
->thread_mq
);
363 pa_sink_unref(u
->sink
);
366 pa_rtpoll_free(u
->rtpoll
);