4 This file is part of polypaudio.
6 polypaudio 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 of the License,
9 or (at your option) any later version.
11 polypaudio 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 polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
29 #include <polypcore/module.h>
30 #include <polypcore/llist.h>
31 #include <polypcore/sink.h>
32 #include <polypcore/sink-input.h>
33 #include <polypcore/memblockq.h>
34 #include <polypcore/log.h>
35 #include <polypcore/util.h>
36 #include <polypcore/xmalloc.h>
37 #include <polypcore/modargs.h>
38 #include <polypcore/namereg.h>
40 #include "module-combine-symdef.h"
42 PA_MODULE_AUTHOR("Lennart Poettering")
43 PA_MODULE_DESCRIPTION("Combine multiple sinks to one")
44 PA_MODULE_VERSION(PACKAGE_VERSION
)
45 PA_MODULE_USAGE("sink_name=<name for the sink> master=<master sink> slaves=<slave sinks> adjust_time=<seconds> resample_method=<method>")
47 #define DEFAULT_SINK_NAME "combined"
48 #define MEMBLOCKQ_MAXLENGTH (1024*170)
49 #define RENDER_SIZE (1024*10)
51 #define DEFAULT_ADJUST_TIME 20
53 static const char* const valid_modargs
[] = {
63 struct userdata
*userdata
;
64 pa_sink_input
*sink_input
;
66 pa_memblockq
*memblockq
;
67 pa_usec_t total_latency
;
68 PA_LLIST_FIELDS(struct output
);
76 struct output
*master
;
77 pa_time_event
*time_event
;
80 PA_LLIST_HEAD(struct output
, outputs
);
83 static void output_free(struct output
*o
);
84 static void clear_up(struct userdata
*u
);
86 static void update_usage(struct userdata
*u
) {
87 pa_module_set_used(u
->module
,
88 (u
->sink
? pa_idxset_size(u
->sink
->inputs
) : 0) +
89 (u
->sink
? pa_idxset_size(u
->sink
->monitor_source
->outputs
) : 0));
93 static void adjust_rates(struct userdata
*u
) {
95 pa_usec_t max_sink_latency
= 0, min_total_latency
= (pa_usec_t
) -1, target_latency
;
99 for (o
= u
->outputs
; o
; o
= o
->next
) {
100 uint32_t sink_latency
= o
->sink_input
->sink
? pa_sink_get_latency(o
->sink_input
->sink
) : 0;
102 o
->total_latency
= sink_latency
+ pa_sink_input_get_latency(o
->sink_input
);
104 if (sink_latency
> max_sink_latency
)
105 max_sink_latency
= sink_latency
;
107 if (o
->total_latency
< min_total_latency
)
108 min_total_latency
= o
->total_latency
;
111 assert(min_total_latency
!= (pa_usec_t
) -1);
113 target_latency
= max_sink_latency
> min_total_latency
? max_sink_latency
: min_total_latency
;
115 pa_log_info(__FILE__
": [%s] target latency is %0.0f usec.", u
->sink
->name
, (float) target_latency
);
117 base_rate
= u
->sink
->sample_spec
.rate
;
119 for (o
= u
->outputs
; o
; o
= o
->next
) {
120 uint32_t r
= base_rate
;
122 if (o
->total_latency
< target_latency
)
123 r
-= (uint32_t) (((((double) target_latency
- o
->total_latency
))/u
->adjust_time
)*r
/ 1000000);
124 else if (o
->total_latency
> target_latency
)
125 r
+= (uint32_t) (((((double) o
->total_latency
- target_latency
))/u
->adjust_time
)*r
/ 1000000);
127 if (r
< (uint32_t) (base_rate
*0.9) || r
> (uint32_t) (base_rate
*1.1))
128 pa_log_warn(__FILE__
": [%s] sample rates too different, not adjusting (%u vs. %u).", o
->sink_input
->name
, base_rate
, r
);
130 pa_log_info(__FILE__
": [%s] new rate is %u Hz; ratio is %0.3f; latency is %0.0f usec.", o
->sink_input
->name
, r
, (double) r
/ base_rate
, (float) o
->total_latency
);
131 pa_sink_input_set_rate(o
->sink_input
, r
);
136 static void request_memblock(struct userdata
*u
) {
139 assert(u
&& u
->sink
);
143 if (pa_sink_render(u
->sink
, RENDER_SIZE
, &chunk
) < 0)
146 for (o
= u
->outputs
; o
; o
= o
->next
)
147 pa_memblockq_push_align(o
->memblockq
, &chunk
);
149 pa_memblock_unref(chunk
.memblock
);
152 static void time_callback(pa_mainloop_api
*a
, pa_time_event
* e
, PA_GCC_UNUSED
const struct timeval
*tv
, void *userdata
) {
153 struct userdata
*u
= userdata
;
155 assert(u
&& a
&& u
->time_event
== e
);
160 n
.tv_sec
+= u
->adjust_time
;
161 u
->sink
->core
->mainloop
->time_restart(e
, &n
);
164 static int sink_input_peek_cb(pa_sink_input
*i
, pa_memchunk
*chunk
) {
165 struct output
*o
= i
->userdata
;
166 assert(i
&& o
&& o
->sink_input
&& chunk
);
168 if (pa_memblockq_peek(o
->memblockq
, chunk
) >= 0)
172 request_memblock(o
->userdata
);
174 return pa_memblockq_peek(o
->memblockq
, chunk
);
177 static void sink_input_drop_cb(pa_sink_input
*i
, const pa_memchunk
*chunk
, size_t length
) {
178 struct output
*o
= i
->userdata
;
179 assert(i
&& o
&& o
->sink_input
&& chunk
&& length
);
181 pa_memblockq_drop(o
->memblockq
, chunk
, length
);
182 o
->counter
+= length
;
185 static void sink_input_kill_cb(pa_sink_input
*i
) {
186 struct output
*o
= i
->userdata
;
187 assert(i
&& o
&& o
->sink_input
);
188 pa_module_unload_request(o
->userdata
->module
);
189 clear_up(o
->userdata
);
192 static pa_usec_t
sink_input_get_latency_cb(pa_sink_input
*i
) {
193 struct output
*o
= i
->userdata
;
194 assert(i
&& o
&& o
->sink_input
);
196 return pa_bytes_to_usec(pa_memblockq_get_length(o
->memblockq
), &i
->sample_spec
);
199 static pa_usec_t
sink_get_latency_cb(pa_sink
*s
) {
200 struct userdata
*u
= s
->userdata
;
201 assert(s
&& u
&& u
->sink
&& u
->master
);
204 pa_sink_input_get_latency(u
->master
->sink_input
) +
205 pa_sink_get_latency(u
->master
->sink_input
->sink
);
208 static struct output
*output_new(struct userdata
*u
, pa_sink
*sink
, int resample_method
) {
209 struct output
*o
= NULL
;
211 assert(u
&& sink
&& u
->sink
);
213 o
= pa_xmalloc(sizeof(struct output
));
217 o
->memblockq
= pa_memblockq_new(
221 pa_frame_size(&u
->sink
->sample_spec
),
225 sink
->core
->memblock_stat
);
227 snprintf(t
, sizeof(t
), "%s: output #%u", u
->sink
->name
, u
->n_outputs
+1);
228 if (!(o
->sink_input
= pa_sink_input_new(sink
, __FILE__
, t
, &u
->sink
->sample_spec
, &u
->sink
->channel_map
, 1, resample_method
)))
231 o
->sink_input
->get_latency
= sink_input_get_latency_cb
;
232 o
->sink_input
->peek
= sink_input_peek_cb
;
233 o
->sink_input
->drop
= sink_input_drop_cb
;
234 o
->sink_input
->kill
= sink_input_kill_cb
;
235 o
->sink_input
->userdata
= o
;
236 o
->sink_input
->owner
= u
->module
;
238 PA_LLIST_PREPEND(struct output
, u
->outputs
, o
);
246 pa_sink_input_disconnect(o
->sink_input
);
247 pa_sink_input_unref(o
->sink_input
);
251 pa_memblockq_free(o
->memblockq
);
259 static void output_free(struct output
*o
) {
261 PA_LLIST_REMOVE(struct output
, o
->userdata
->outputs
, o
);
262 o
->userdata
->n_outputs
--;
263 pa_memblockq_free(o
->memblockq
);
264 pa_sink_input_disconnect(o
->sink_input
);
265 pa_sink_input_unref(o
->sink_input
);
269 static void clear_up(struct userdata
*u
) {
274 u
->core
->mainloop
->time_free(u
->time_event
);
275 u
->time_event
= NULL
;
278 while ((o
= u
->outputs
))
284 pa_sink_disconnect(u
->sink
);
285 pa_sink_unref(u
->sink
);
290 int pa__init(pa_core
*c
, pa_module
*m
) {
292 pa_modargs
*ma
= NULL
;
293 const char *master_name
, *slaves
, *rm
;
294 pa_sink
*master_sink
;
296 const char*split_state
;
298 int resample_method
= -1;
301 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
302 pa_log(__FILE__
": failed to parse module arguments");
306 if ((rm
= pa_modargs_get_value(ma
, "resample_method", NULL
))) {
307 if ((resample_method
= pa_parse_resample_method(rm
)) < 0) {
308 pa_log(__FILE__
": invalid resample method '%s'", rm
);
313 u
= pa_xmalloc(sizeof(struct userdata
));
320 u
->time_event
= NULL
;
321 u
->adjust_time
= DEFAULT_ADJUST_TIME
;
322 PA_LLIST_HEAD_INIT(struct output
, u
->outputs
);
324 if (pa_modargs_get_value_u32(ma
, "adjust_time", &u
->adjust_time
) < 0) {
325 pa_log(__FILE__
": failed to parse adjust_time value");
329 if (!(master_name
= pa_modargs_get_value(ma
, "master", NULL
)) || !(slaves
= pa_modargs_get_value(ma
, "slaves", NULL
))) {
330 pa_log(__FILE__
": no master or slave sinks specified");
334 if (!(master_sink
= pa_namereg_get(c
, master_name
, PA_NAMEREG_SINK
, 1))) {
335 pa_log(__FILE__
": invalid master sink '%s'", master_name
);
339 if (!(u
->sink
= pa_sink_new(c
, __FILE__
, pa_modargs_get_value(ma
, "sink_name", DEFAULT_SINK_NAME
), 0, &master_sink
->sample_spec
, &master_sink
->channel_map
))) {
340 pa_log(__FILE__
": failed to create sink");
344 pa_sink_set_owner(u
->sink
, m
);
345 u
->sink
->description
= pa_sprintf_malloc("Combined sink");
346 u
->sink
->get_latency
= sink_get_latency_cb
;
347 u
->sink
->userdata
= u
;
349 if (!(u
->master
= output_new(u
, master_sink
, resample_method
))) {
350 pa_log(__FILE__
": failed to create master sink input on sink '%s'.", u
->sink
->name
);
355 while ((n
= pa_split(slaves
, ",", &split_state
))) {
358 if (!(slave_sink
= pa_namereg_get(c
, n
, PA_NAMEREG_SINK
, 1))) {
359 pa_log(__FILE__
": invalid slave sink '%s'", n
);
365 if (!output_new(u
, slave_sink
, resample_method
)) {
366 pa_log(__FILE__
": failed to create slave sink input on sink '%s'.", slave_sink
->name
);
371 if (u
->n_outputs
<= 1)
372 pa_log_warn(__FILE__
": WARNING: no slave sinks specified.");
374 if (u
->adjust_time
> 0) {
375 pa_gettimeofday(&tv
);
376 tv
.tv_sec
+= u
->adjust_time
;
377 u
->time_event
= c
->mainloop
->time_new(c
->mainloop
, &tv
, time_callback
, u
);
393 void pa__done(pa_core
*c
, pa_module
*m
) {
397 if (!(u
= m
->userdata
))