]> code.delx.au - pulseaudio/blob - src/modules/module-combine.c
fix latency calculations of module-combine
[pulseaudio] / src / modules / module-combine.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
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.
10
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.
15
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
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <assert.h>
27 #include <stdio.h>
28
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>
39
40 #include "module-combine-symdef.h"
41
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>")
46
47 #define DEFAULT_SINK_NAME "combined"
48 #define MEMBLOCKQ_MAXLENGTH (1024*170)
49 #define RENDER_SIZE (1024*10)
50
51 #define DEFAULT_ADJUST_TIME 20
52
53 static const char* const valid_modargs[] = {
54 "sink_name",
55 "master",
56 "slaves",
57 "adjust_time",
58 "resample_method",
59 NULL
60 };
61
62 struct output {
63 struct userdata *userdata;
64 pa_sink_input *sink_input;
65 size_t counter;
66 pa_memblockq *memblockq;
67 pa_usec_t total_latency;
68 PA_LLIST_FIELDS(struct output);
69 };
70
71 struct userdata {
72 pa_module *module;
73 pa_core *core;
74 pa_sink *sink;
75 unsigned n_outputs;
76 struct output *master;
77 pa_time_event *time_event;
78 uint32_t adjust_time;
79
80 PA_LLIST_HEAD(struct output, outputs);
81 };
82
83 static void output_free(struct output *o);
84 static void clear_up(struct userdata *u);
85
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));
90 }
91
92
93 static void adjust_rates(struct userdata *u) {
94 struct output *o;
95 pa_usec_t max_sink_latency = 0, min_total_latency = (pa_usec_t) -1, target_latency;
96 uint32_t base_rate;
97 assert(u && u->sink);
98
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;
101
102 o->total_latency = sink_latency + pa_sink_input_get_latency(o->sink_input);
103
104 if (sink_latency > max_sink_latency)
105 max_sink_latency = sink_latency;
106
107 if (o->total_latency < min_total_latency)
108 min_total_latency = o->total_latency;
109 }
110
111 assert(min_total_latency != (pa_usec_t) -1);
112
113 target_latency = max_sink_latency > min_total_latency ? max_sink_latency : min_total_latency;
114
115 pa_log_info(__FILE__": [%s] target latency is %0.0f usec.", u->sink->name, (float) target_latency);
116
117 base_rate = u->sink->sample_spec.rate;
118
119 for (o = u->outputs; o; o = o->next) {
120 uint32_t r = base_rate;
121
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);
126
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);
129 else {
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);
132 }
133 }
134 }
135
136 static void request_memblock(struct userdata *u) {
137 pa_memchunk chunk;
138 struct output *o;
139 assert(u && u->sink);
140
141 update_usage(u);
142
143 if (pa_sink_render(u->sink, RENDER_SIZE, &chunk) < 0)
144 return;
145
146 for (o = u->outputs; o; o = o->next)
147 pa_memblockq_push_align(o->memblockq, &chunk);
148
149 pa_memblock_unref(chunk.memblock);
150 }
151
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;
154 struct timeval n;
155 assert(u && a && u->time_event == e);
156
157 adjust_rates(u);
158
159 pa_gettimeofday(&n);
160 n.tv_sec += u->adjust_time;
161 u->sink->core->mainloop->time_restart(e, &n);
162 }
163
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);
167
168 if (pa_memblockq_peek(o->memblockq, chunk) >= 0)
169 return 0;
170
171 /* Try harder */
172 request_memblock(o->userdata);
173
174 return pa_memblockq_peek(o->memblockq, chunk);
175 }
176
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);
180
181 pa_memblockq_drop(o->memblockq, chunk, length);
182 o->counter += length;
183 }
184
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);
190 }
191
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);
195
196 return pa_bytes_to_usec(pa_memblockq_get_length(o->memblockq), &i->sample_spec);
197 }
198
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);
202
203 return
204 pa_sink_input_get_latency(u->master->sink_input) +
205 pa_sink_get_latency(u->master->sink_input->sink);
206 }
207
208 static struct output *output_new(struct userdata *u, pa_sink *sink, int resample_method) {
209 struct output *o = NULL;
210 char t[256];
211 assert(u && sink && u->sink);
212
213 o = pa_xmalloc(sizeof(struct output));
214 o->userdata = u;
215
216 o->counter = 0;
217 o->memblockq = pa_memblockq_new(
218 0,
219 MEMBLOCKQ_MAXLENGTH,
220 MEMBLOCKQ_MAXLENGTH,
221 pa_frame_size(&u->sink->sample_spec),
222 1,
223 0,
224 NULL,
225 sink->core->memblock_stat);
226
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)))
229 goto fail;
230
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;
237
238 PA_LLIST_PREPEND(struct output, u->outputs, o);
239 u->n_outputs++;
240 return o;
241
242 fail:
243
244 if (o) {
245 if (o->sink_input) {
246 pa_sink_input_disconnect(o->sink_input);
247 pa_sink_input_unref(o->sink_input);
248 }
249
250 if (o->memblockq)
251 pa_memblockq_free(o->memblockq);
252
253 pa_xfree(o);
254 }
255
256 return NULL;
257 }
258
259 static void output_free(struct output *o) {
260 assert(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);
266 pa_xfree(o);
267 }
268
269 static void clear_up(struct userdata *u) {
270 struct output *o;
271 assert(u);
272
273 if (u->time_event) {
274 u->core->mainloop->time_free(u->time_event);
275 u->time_event = NULL;
276 }
277
278 while ((o = u->outputs))
279 output_free(o);
280
281 u->master = NULL;
282
283 if (u->sink) {
284 pa_sink_disconnect(u->sink);
285 pa_sink_unref(u->sink);
286 u->sink = NULL;
287 }
288 }
289
290 int pa__init(pa_core *c, pa_module*m) {
291 struct userdata *u;
292 pa_modargs *ma = NULL;
293 const char *master_name, *slaves, *rm;
294 pa_sink *master_sink;
295 char *n = NULL;
296 const char*split_state;
297 struct timeval tv;
298 int resample_method = -1;
299 assert(c && m);
300
301 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
302 pa_log(__FILE__": failed to parse module arguments");
303 goto fail;
304 }
305
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);
309 goto fail;
310 }
311 }
312
313 u = pa_xmalloc(sizeof(struct userdata));
314 m->userdata = u;
315 u->sink = NULL;
316 u->n_outputs = 0;
317 u->master = NULL;
318 u->module = m;
319 u->core = c;
320 u->time_event = NULL;
321 u->adjust_time = DEFAULT_ADJUST_TIME;
322 PA_LLIST_HEAD_INIT(struct output, u->outputs);
323
324 if (pa_modargs_get_value_u32(ma, "adjust_time", &u->adjust_time) < 0) {
325 pa_log(__FILE__": failed to parse adjust_time value");
326 goto fail;
327 }
328
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");
331 goto fail;
332 }
333
334 if (!(master_sink = pa_namereg_get(c, master_name, PA_NAMEREG_SINK, 1))) {
335 pa_log(__FILE__": invalid master sink '%s'", master_name);
336 goto fail;
337 }
338
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");
341 goto fail;
342 }
343
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;
348
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);
351 goto fail;
352 }
353
354 split_state = NULL;
355 while ((n = pa_split(slaves, ",", &split_state))) {
356 pa_sink *slave_sink;
357
358 if (!(slave_sink = pa_namereg_get(c, n, PA_NAMEREG_SINK, 1))) {
359 pa_log(__FILE__": invalid slave sink '%s'", n);
360 goto fail;
361 }
362
363 pa_xfree(n);
364
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);
367 goto fail;
368 }
369 }
370
371 if (u->n_outputs <= 1)
372 pa_log_warn(__FILE__": WARNING: no slave sinks specified.");
373
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);
378 }
379
380 pa_modargs_free(ma);
381 return 0;
382
383 fail:
384 pa_xfree(n);
385
386 if (ma)
387 pa_modargs_free(ma);
388
389 pa__done(c, m);
390 return -1;
391 }
392
393 void pa__done(pa_core *c, pa_module*m) {
394 struct userdata *u;
395 assert(c && m);
396
397 if (!(u = m->userdata))
398 return;
399
400 clear_up(u);
401 pa_xfree(u);
402 }
403
404