4 This file is part of PulseAudio.
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 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
29 #include <pulse/timeval.h>
30 #include <pulse/xmalloc.h>
32 #include <pulsecore/module.h>
33 #include <pulsecore/llist.h>
34 #include <pulsecore/sink.h>
35 #include <pulsecore/sink-input.h>
36 #include <pulsecore/memblockq.h>
37 #include <pulsecore/log.h>
38 #include <pulsecore/core-util.h>
39 #include <pulsecore/modargs.h>
40 #include <pulsecore/namereg.h>
42 #include "module-combine-symdef.h"
44 PA_MODULE_AUTHOR("Lennart Poettering")
45 PA_MODULE_DESCRIPTION("Combine multiple sinks to one")
46 PA_MODULE_VERSION(PACKAGE_VERSION
)
48 "sink_name=<name for the sink> "
49 "master=<master sink> "
50 "slaves=<slave sinks> "
51 "adjust_time=<seconds> "
52 "resample_method=<method> "
53 "format=<sample format> "
54 "channels=<number of channels> "
56 "channel_map=<channel map> ")
58 #define DEFAULT_SINK_NAME "combined"
59 #define MEMBLOCKQ_MAXLENGTH (1024*170)
60 #define RENDER_SIZE (1024*10)
62 #define DEFAULT_ADJUST_TIME 20
64 static const char* const valid_modargs
[] = {
78 struct userdata
*userdata
;
79 pa_sink_input
*sink_input
;
81 pa_memblockq
*memblockq
;
82 pa_usec_t total_latency
;
83 PA_LLIST_FIELDS(struct output
);
91 struct output
*master
;
92 pa_time_event
*time_event
;
95 PA_LLIST_HEAD(struct output
, outputs
);
98 static void output_free(struct output
*o
);
99 static void clear_up(struct userdata
*u
);
101 static void update_usage(struct userdata
*u
) {
102 pa_module_set_used(u
->module
, u
->sink
? pa_sink_used_by(u
->sink
) : 0);
105 static void adjust_rates(struct userdata
*u
) {
107 pa_usec_t max_sink_latency
= 0, min_total_latency
= (pa_usec_t
) -1, target_latency
;
109 assert(u
&& u
->sink
);
111 for (o
= u
->outputs
; o
; o
= o
->next
) {
112 uint32_t sink_latency
= o
->sink_input
->sink
? pa_sink_get_latency(o
->sink_input
->sink
) : 0;
114 o
->total_latency
= sink_latency
+ pa_sink_input_get_latency(o
->sink_input
);
116 if (sink_latency
> max_sink_latency
)
117 max_sink_latency
= sink_latency
;
119 if (o
->total_latency
< min_total_latency
)
120 min_total_latency
= o
->total_latency
;
123 assert(min_total_latency
!= (pa_usec_t
) -1);
125 target_latency
= max_sink_latency
> min_total_latency
? max_sink_latency
: min_total_latency
;
127 pa_log_info(__FILE__
": [%s] target latency is %0.0f usec.", u
->sink
->name
, (float) target_latency
);
129 base_rate
= u
->sink
->sample_spec
.rate
;
131 for (o
= u
->outputs
; o
; o
= o
->next
) {
132 uint32_t r
= base_rate
;
134 if (o
->total_latency
< target_latency
)
135 r
-= (uint32_t) (((((double) target_latency
- o
->total_latency
))/u
->adjust_time
)*r
/ 1000000);
136 else if (o
->total_latency
> target_latency
)
137 r
+= (uint32_t) (((((double) o
->total_latency
- target_latency
))/u
->adjust_time
)*r
/ 1000000);
139 if (r
< (uint32_t) (base_rate
*0.9) || r
> (uint32_t) (base_rate
*1.1))
140 pa_log_warn(__FILE__
": [%s] sample rates too different, not adjusting (%u vs. %u).", o
->sink_input
->name
, base_rate
, r
);
142 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
);
143 pa_sink_input_set_rate(o
->sink_input
, r
);
148 static void request_memblock(struct userdata
*u
) {
151 assert(u
&& u
->sink
);
155 if (pa_sink_render(u
->sink
, RENDER_SIZE
, &chunk
) < 0)
158 for (o
= u
->outputs
; o
; o
= o
->next
)
159 pa_memblockq_push_align(o
->memblockq
, &chunk
);
161 pa_memblock_unref(chunk
.memblock
);
164 static void time_callback(pa_mainloop_api
*a
, pa_time_event
* e
, PA_GCC_UNUSED
const struct timeval
*tv
, void *userdata
) {
165 struct userdata
*u
= userdata
;
167 assert(u
&& a
&& u
->time_event
== e
);
172 n
.tv_sec
+= u
->adjust_time
;
173 u
->sink
->core
->mainloop
->time_restart(e
, &n
);
176 static int sink_input_peek_cb(pa_sink_input
*i
, pa_memchunk
*chunk
) {
177 struct output
*o
= i
->userdata
;
178 assert(i
&& o
&& o
->sink_input
&& chunk
);
180 if (pa_memblockq_peek(o
->memblockq
, chunk
) >= 0)
184 request_memblock(o
->userdata
);
186 return pa_memblockq_peek(o
->memblockq
, chunk
);
189 static void sink_input_drop_cb(pa_sink_input
*i
, const pa_memchunk
*chunk
, size_t length
) {
190 struct output
*o
= i
->userdata
;
191 assert(i
&& o
&& o
->sink_input
&& chunk
&& length
);
193 pa_memblockq_drop(o
->memblockq
, chunk
, length
);
194 o
->counter
+= length
;
197 static void sink_input_kill_cb(pa_sink_input
*i
) {
198 struct output
*o
= i
->userdata
;
199 assert(i
&& o
&& o
->sink_input
);
200 pa_module_unload_request(o
->userdata
->module
);
201 clear_up(o
->userdata
);
204 static pa_usec_t
sink_input_get_latency_cb(pa_sink_input
*i
) {
205 struct output
*o
= i
->userdata
;
206 assert(i
&& o
&& o
->sink_input
);
208 return pa_bytes_to_usec(pa_memblockq_get_length(o
->memblockq
), &i
->sample_spec
);
211 static pa_usec_t
sink_get_latency_cb(pa_sink
*s
) {
212 struct userdata
*u
= s
->userdata
;
213 assert(s
&& u
&& u
->sink
&& u
->master
);
216 pa_sink_input_get_latency(u
->master
->sink_input
) +
217 pa_sink_get_latency(u
->master
->sink_input
->sink
);
220 static struct output
*output_new(struct userdata
*u
, pa_sink
*sink
, int resample_method
) {
221 struct output
*o
= NULL
;
223 pa_sink_input_new_data data
;
225 assert(u
&& sink
&& u
->sink
);
227 o
= pa_xmalloc(sizeof(struct output
));
231 o
->memblockq
= pa_memblockq_new(
235 pa_frame_size(&u
->sink
->sample_spec
),
240 snprintf(t
, sizeof(t
), "%s: output #%u", u
->sink
->name
, u
->n_outputs
+1);
242 pa_sink_input_new_data_init(&data
);
244 data
.driver
= __FILE__
;
246 pa_sink_input_new_data_set_sample_spec(&data
, &u
->sink
->sample_spec
);
247 pa_sink_input_new_data_set_channel_map(&data
, &u
->sink
->channel_map
);
248 data
.module
= u
->module
;
250 if (!(o
->sink_input
= pa_sink_input_new(u
->core
, &data
, PA_SINK_INPUT_VARIABLE_RATE
)))
253 o
->sink_input
->get_latency
= sink_input_get_latency_cb
;
254 o
->sink_input
->peek
= sink_input_peek_cb
;
255 o
->sink_input
->drop
= sink_input_drop_cb
;
256 o
->sink_input
->kill
= sink_input_kill_cb
;
257 o
->sink_input
->userdata
= o
;
259 PA_LLIST_PREPEND(struct output
, u
->outputs
, o
);
267 pa_sink_input_disconnect(o
->sink_input
);
268 pa_sink_input_unref(o
->sink_input
);
272 pa_memblockq_free(o
->memblockq
);
280 static void output_free(struct output
*o
) {
282 PA_LLIST_REMOVE(struct output
, o
->userdata
->outputs
, o
);
283 o
->userdata
->n_outputs
--;
284 pa_memblockq_free(o
->memblockq
);
285 pa_sink_input_disconnect(o
->sink_input
);
286 pa_sink_input_unref(o
->sink_input
);
290 static void clear_up(struct userdata
*u
) {
295 u
->core
->mainloop
->time_free(u
->time_event
);
296 u
->time_event
= NULL
;
299 while ((o
= u
->outputs
))
305 pa_sink_disconnect(u
->sink
);
306 pa_sink_unref(u
->sink
);
311 int pa__init(pa_core
*c
, pa_module
*m
) {
313 pa_modargs
*ma
= NULL
;
314 const char *master_name
, *slaves
, *rm
;
315 pa_sink
*master_sink
;
317 const char*split_state
;
319 int resample_method
= -1;
325 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
326 pa_log(__FILE__
": failed to parse module arguments");
330 if ((rm
= pa_modargs_get_value(ma
, "resample_method", NULL
))) {
331 if ((resample_method
= pa_parse_resample_method(rm
)) < 0) {
332 pa_log(__FILE__
": invalid resample method '%s'", rm
);
337 u
= pa_xnew(struct userdata
, 1);
344 u
->time_event
= NULL
;
345 u
->adjust_time
= DEFAULT_ADJUST_TIME
;
346 PA_LLIST_HEAD_INIT(struct output
, u
->outputs
);
348 if (pa_modargs_get_value_u32(ma
, "adjust_time", &u
->adjust_time
) < 0) {
349 pa_log(__FILE__
": failed to parse adjust_time value");
353 if (!(master_name
= pa_modargs_get_value(ma
, "master", NULL
)) || !(slaves
= pa_modargs_get_value(ma
, "slaves", NULL
))) {
354 pa_log(__FILE__
": no master or slave sinks specified");
358 if (!(master_sink
= pa_namereg_get(c
, master_name
, PA_NAMEREG_SINK
, 1))) {
359 pa_log(__FILE__
": invalid master sink '%s'", master_name
);
363 ss
= master_sink
->sample_spec
;
364 if ((pa_modargs_get_sample_spec(ma
, &ss
) < 0)) {
365 pa_log(__FILE__
": invalid sample specification.");
369 if (ss
.channels
== master_sink
->sample_spec
.channels
)
370 map
= master_sink
->channel_map
;
372 pa_channel_map_init_auto(&map
, ss
.channels
, PA_CHANNEL_MAP_DEFAULT
);
374 if ((pa_modargs_get_channel_map(ma
, &map
) < 0)) {
375 pa_log(__FILE__
": invalid channel map.");
379 if (ss
.channels
!= map
.channels
) {
380 pa_log(__FILE__
": channel map and sample specification don't match.");
384 if (!(u
->sink
= pa_sink_new(c
, __FILE__
, pa_modargs_get_value(ma
, "sink_name", DEFAULT_SINK_NAME
), 0, &ss
, &map
))) {
385 pa_log(__FILE__
": failed to create sink");
389 pa_sink_set_owner(u
->sink
, m
);
390 pa_sink_set_description(u
->sink
, "Combined sink");
391 u
->sink
->get_latency
= sink_get_latency_cb
;
392 u
->sink
->userdata
= u
;
394 if (!(u
->master
= output_new(u
, master_sink
, resample_method
))) {
395 pa_log(__FILE__
": failed to create master sink input on sink '%s'.", u
->sink
->name
);
400 while ((n
= pa_split(slaves
, ",", &split_state
))) {
403 if (!(slave_sink
= pa_namereg_get(c
, n
, PA_NAMEREG_SINK
, 1))) {
404 pa_log(__FILE__
": invalid slave sink '%s'", n
);
410 if (!output_new(u
, slave_sink
, resample_method
)) {
411 pa_log(__FILE__
": failed to create slave sink input on sink '%s'.", slave_sink
->name
);
416 if (u
->n_outputs
<= 1)
417 pa_log_warn(__FILE__
": WARNING: no slave sinks specified.");
419 if (u
->adjust_time
> 0) {
420 pa_gettimeofday(&tv
);
421 tv
.tv_sec
+= u
->adjust_time
;
422 u
->time_event
= c
->mainloop
->time_new(c
->mainloop
, &tv
, time_callback
, u
);
438 void pa__done(pa_core
*c
, pa_module
*m
) {
442 if (!(u
= m
->userdata
))