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 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 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
32 #include "sink-input.h"
33 #include "memblockq.h"
40 PA_MODULE_AUTHOR("Lennart Poettering")
41 PA_MODULE_DESCRIPTION("Combine multiple sinks to one")
42 PA_MODULE_VERSION(PACKAGE_VERSION
)
43 PA_MODULE_USAGE("sink_name=<name for the sink> master=<master sink> slave=<slave sinks> adjust_time=<seconds>")
45 #define DEFAULT_SINK_NAME "combined"
46 #define MEMBLOCKQ_MAXLENGTH (1024*170)
47 #define RENDER_SIZE (1024*10)
49 #define DEFAULT_ADJUST_TIME 20
51 static const char* const valid_modargs
[] = {
60 struct userdata
*userdata
;
61 struct pa_sink_input
*sink_input
;
63 struct pa_memblockq
*memblockq
;
64 pa_usec_t total_latency
;
65 PA_LLIST_FIELDS(struct output
);
69 struct pa_module
*module
;
73 struct output
*master
;
74 struct pa_time_event
*time_event
;
77 PA_LLIST_HEAD(struct output
, outputs
);
80 static void output_free(struct output
*o
);
81 static void clear_up(struct userdata
*u
);
83 static void update_usage(struct userdata
*u
) {
84 pa_module_set_used(u
->module
,
85 (u
->sink
? pa_idxset_ncontents(u
->sink
->inputs
) : 0) +
86 (u
->sink
? pa_idxset_ncontents(u
->sink
->monitor_source
->outputs
) : 0));
90 static void adjust_rates(struct userdata
*u
) {
92 pa_usec_t max_sink_latency
= 0, min_total_latency
= (pa_usec_t
) -1, target_latency
;
96 for (o
= u
->outputs
; o
; o
= o
->next
) {
97 uint32_t sink_latency
= o
->sink_input
->sink
? pa_sink_get_latency(o
->sink_input
->sink
) : 0;
99 o
->total_latency
= sink_latency
+ pa_sink_input_get_latency(o
->sink_input
);
101 if (sink_latency
> max_sink_latency
)
102 max_sink_latency
= sink_latency
;
104 if (o
->total_latency
< min_total_latency
)
105 min_total_latency
= o
->total_latency
;
108 assert(max_sink_latency
> 0 && min_total_latency
!= (pa_usec_t
) -1);
110 target_latency
= max_sink_latency
> min_total_latency
? max_sink_latency
: min_total_latency
;
112 pa_log(__FILE__
": [%s] target latency is %0.0f usec.\n", u
->sink
->name
, (float) target_latency
);
114 base_rate
= u
->sink
->sample_spec
.rate
;
116 for (o
= u
->outputs
; o
; o
= o
->next
) {
117 uint32_t r
= base_rate
;
119 if (o
->total_latency
< target_latency
)
120 r
-= (uint32_t) (((((double) target_latency
- o
->total_latency
))/u
->adjust_time
)*r
/ 1000000);
121 else if (o
->total_latency
> target_latency
)
122 r
+= (uint32_t) (((((double) o
->total_latency
- target_latency
))/u
->adjust_time
)*r
/ 1000000);
124 if (r
< (uint32_t) (base_rate
*0.9) || r
> (uint32_t) (base_rate
*1.1))
125 pa_log(__FILE__
": [%s] sample rates too different, not adjusting (%u vs. %u).\n", o
->sink_input
->name
, base_rate
, r
);
127 pa_log(__FILE__
": [%s] new rate is %u Hz; ratio is %0.3f; latency is %0.0f usec.\n", o
->sink_input
->name
, r
, (double) r
/ base_rate
, (float) o
->total_latency
);
128 pa_sink_input_set_rate(o
->sink_input
, r
);
133 static void request_memblock(struct userdata
*u
) {
134 struct pa_memchunk chunk
;
136 assert(u
&& u
->sink
);
140 if (pa_sink_render(u
->sink
, RENDER_SIZE
, &chunk
) < 0)
143 for (o
= u
->outputs
; o
; o
= o
->next
)
144 pa_memblockq_push_align(o
->memblockq
, &chunk
, 0);
146 pa_memblock_unref(chunk
.memblock
);
149 static void time_callback(struct pa_mainloop_api
*a
, struct pa_time_event
* e
, const struct timeval
*tv
, void *userdata
) {
150 struct userdata
*u
= userdata
;
152 assert(u
&& a
&& u
->time_event
== e
);
156 gettimeofday(&n
, NULL
);
157 n
.tv_sec
+= u
->adjust_time
;
158 u
->sink
->core
->mainloop
->time_restart(e
, &n
);
161 static int sink_input_peek_cb(struct pa_sink_input
*i
, struct pa_memchunk
*chunk
) {
162 struct output
*o
= i
->userdata
;
163 assert(i
&& o
&& o
->sink_input
&& chunk
);
165 if (pa_memblockq_peek(o
->memblockq
, chunk
) >= 0)
169 request_memblock(o
->userdata
);
171 return pa_memblockq_peek(o
->memblockq
, chunk
);
174 static void sink_input_drop_cb(struct pa_sink_input
*i
, const struct pa_memchunk
*chunk
, size_t length
) {
175 struct output
*o
= i
->userdata
;
176 assert(i
&& o
&& o
->sink_input
&& chunk
&& length
);
178 pa_memblockq_drop(o
->memblockq
, chunk
, length
);
179 o
->counter
+= length
;
182 static void sink_input_kill_cb(struct pa_sink_input
*i
) {
183 struct output
*o
= i
->userdata
;
184 assert(i
&& o
&& o
->sink_input
);
185 pa_module_unload_request(o
->userdata
->module
);
186 clear_up(o
->userdata
);
189 static pa_usec_t
sink_input_get_latency_cb(struct pa_sink_input
*i
) {
190 struct output
*o
= i
->userdata
;
191 assert(i
&& o
&& o
->sink_input
);
193 return pa_bytes_to_usec(pa_memblockq_get_length(o
->memblockq
), &i
->sample_spec
);
196 static pa_usec_t
sink_get_latency_cb(struct pa_sink
*s
) {
197 struct userdata
*u
= s
->userdata
;
198 assert(s
&& u
&& u
->sink
&& u
->master
);
200 return pa_sink_input_get_latency(u
->master
->sink_input
);
203 static struct output
*output_new(struct userdata
*u
, struct pa_sink
*sink
) {
204 struct output
*o
= NULL
;
206 assert(u
&& sink
&& u
->sink
);
208 o
= pa_xmalloc(sizeof(struct output
));
212 o
->memblockq
= pa_memblockq_new(MEMBLOCKQ_MAXLENGTH
, MEMBLOCKQ_MAXLENGTH
, pa_frame_size(&u
->sink
->sample_spec
), 0, 0, sink
->core
->memblock_stat
);
214 snprintf(t
, sizeof(t
), "%s: output #%u", u
->sink
->name
, u
->n_outputs
+1);
215 if (!(o
->sink_input
= pa_sink_input_new(sink
, t
, &u
->sink
->sample_spec
, 1)))
218 o
->sink_input
->get_latency
= sink_input_get_latency_cb
;
219 o
->sink_input
->peek
= sink_input_peek_cb
;
220 o
->sink_input
->drop
= sink_input_drop_cb
;
221 o
->sink_input
->kill
= sink_input_kill_cb
;
222 o
->sink_input
->userdata
= o
;
223 o
->sink_input
->owner
= u
->module
;
225 PA_LLIST_PREPEND(struct output
, u
->outputs
, o
);
233 pa_sink_input_disconnect(o
->sink_input
);
234 pa_sink_input_unref(o
->sink_input
);
238 pa_memblockq_free(o
->memblockq
);
246 static void output_free(struct output
*o
) {
248 PA_LLIST_REMOVE(struct output
, o
->userdata
->outputs
, o
);
249 o
->userdata
->n_outputs
--;
250 pa_memblockq_free(o
->memblockq
);
251 pa_sink_input_disconnect(o
->sink_input
);
252 pa_sink_input_unref(o
->sink_input
);
256 static void clear_up(struct userdata
*u
) {
261 u
->core
->mainloop
->time_free(u
->time_event
);
262 u
->time_event
= NULL
;
265 while ((o
= u
->outputs
))
271 pa_sink_disconnect(u
->sink
);
272 pa_sink_unref(u
->sink
);
277 int pa__init(struct pa_core
*c
, struct pa_module
*m
) {
279 struct pa_modargs
*ma
= NULL
;
280 const char *master_name
, *slaves
;
281 struct pa_sink
*master_sink
;
283 const char*split_state
;
287 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
288 pa_log(__FILE__
": failed to parse module arguments\n");
292 u
= pa_xmalloc(sizeof(struct userdata
));
299 u
->time_event
= NULL
;
300 u
->adjust_time
= DEFAULT_ADJUST_TIME
;
301 PA_LLIST_HEAD_INIT(struct output
, u
->outputs
);
303 if (pa_modargs_get_value_u32(ma
, "adjust_time", &u
->adjust_time
) < 0) {
304 pa_log(__FILE__
": failed to parse adjust_time value\n");
308 if (!(master_name
= pa_modargs_get_value(ma
, "master", NULL
)) || !(slaves
= pa_modargs_get_value(ma
, "slaves", NULL
))) {
309 pa_log(__FILE__
": no master or slave sinks specified\n");
313 if (!(master_sink
= pa_namereg_get(c
, master_name
, PA_NAMEREG_SINK
, 1))) {
314 pa_log(__FILE__
": invalid master sink '%s'\n", master_name
);
318 if (!(u
->sink
= pa_sink_new(c
, pa_modargs_get_value(ma
, "sink_name", DEFAULT_SINK_NAME
), 0, &master_sink
->sample_spec
))) {
319 pa_log(__FILE__
": failed to create sink\n");
323 pa_sink_set_owner(u
->sink
, m
);
324 u
->sink
->description
= pa_sprintf_malloc("Combined sink");
325 u
->sink
->get_latency
= sink_get_latency_cb
;
326 u
->sink
->userdata
= u
;
328 if (!(u
->master
= output_new(u
, master_sink
))) {
329 pa_log(__FILE__
": failed to create master sink input on sink '%s'.\n", u
->sink
->name
);
334 while ((n
= pa_split(slaves
, ",", &split_state
))) {
335 struct pa_sink
*slave_sink
;
337 if (!(slave_sink
= pa_namereg_get(c
, n
, PA_NAMEREG_SINK
, 1))) {
338 pa_log(__FILE__
": invalid slave sink '%s'\n", n
);
344 if (!output_new(u
, slave_sink
)) {
345 pa_log(__FILE__
": failed to create slave sink input on sink '%s'.\n", slave_sink
->name
);
350 if (u
->n_outputs
<= 1)
351 pa_log(__FILE__
": WARNING: no slave sinks specified.\n");
353 if (u
->adjust_time
> 0) {
354 gettimeofday(&tv
, NULL
);
355 tv
.tv_sec
+= u
->adjust_time
;
356 u
->time_event
= c
->mainloop
->time_new(c
->mainloop
, &tv
, time_callback
, u
);
372 void pa__done(struct pa_core
*c
, struct pa_module
*m
) {
376 if (!(u
= m
->userdata
))