]> code.delx.au - pulseaudio/blob - polyp/module-combine.c
5df3f1589bd80d79ec8c7289595ca701641d02dc
[pulseaudio] / polyp / 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 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 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 "module.h"
30 #include "llist.h"
31 #include "sink.h"
32 #include "sink-input.h"
33 #include "memblockq.h"
34 #include "log.h"
35 #include "util.h"
36 #include "xmalloc.h"
37 #include "modargs.h"
38 #include "namereg.h"
39 #include "module-combine-symdef.h"
40
41 PA_MODULE_AUTHOR("Lennart Poettering")
42 PA_MODULE_DESCRIPTION("Combine multiple sinks to one")
43 PA_MODULE_VERSION(PACKAGE_VERSION)
44 PA_MODULE_USAGE("sink_name=<name for the sink> master=<master sink> slaves=<slave sinks> adjust_time=<seconds> resample_method=<method>")
45
46 #define DEFAULT_SINK_NAME "combined"
47 #define MEMBLOCKQ_MAXLENGTH (1024*170)
48 #define RENDER_SIZE (1024*10)
49
50 #define DEFAULT_ADJUST_TIME 20
51
52 static const char* const valid_modargs[] = {
53 "sink_name",
54 "master",
55 "slaves",
56 "adjust_time",
57 "resample_method",
58 NULL
59 };
60
61 struct output {
62 struct userdata *userdata;
63 struct pa_sink_input *sink_input;
64 size_t counter;
65 struct pa_memblockq *memblockq;
66 pa_usec_t total_latency;
67 PA_LLIST_FIELDS(struct output);
68 };
69
70 struct userdata {
71 struct pa_module *module;
72 struct pa_core *core;
73 struct pa_sink *sink;
74 unsigned n_outputs;
75 struct output *master;
76 struct pa_time_event *time_event;
77 uint32_t adjust_time;
78
79 PA_LLIST_HEAD(struct output, outputs);
80 };
81
82 static void output_free(struct output *o);
83 static void clear_up(struct userdata *u);
84
85 static void update_usage(struct userdata *u) {
86 pa_module_set_used(u->module,
87 (u->sink ? pa_idxset_ncontents(u->sink->inputs) : 0) +
88 (u->sink ? pa_idxset_ncontents(u->sink->monitor_source->outputs) : 0));
89 }
90
91
92 static void adjust_rates(struct userdata *u) {
93 struct output *o;
94 pa_usec_t max_sink_latency = 0, min_total_latency = (pa_usec_t) -1, target_latency;
95 uint32_t base_rate;
96 assert(u && u->sink);
97
98 for (o = u->outputs; o; o = o->next) {
99 uint32_t sink_latency = o->sink_input->sink ? pa_sink_get_latency(o->sink_input->sink) : 0;
100
101 o->total_latency = sink_latency + pa_sink_input_get_latency(o->sink_input);
102
103 if (sink_latency > max_sink_latency)
104 max_sink_latency = sink_latency;
105
106 if (o->total_latency < min_total_latency)
107 min_total_latency = o->total_latency;
108 }
109
110 assert(min_total_latency != (pa_usec_t) -1);
111
112 target_latency = max_sink_latency > min_total_latency ? max_sink_latency : min_total_latency;
113
114 pa_log(__FILE__": [%s] target latency is %0.0f usec.\n", u->sink->name, (float) target_latency);
115
116 base_rate = u->sink->sample_spec.rate;
117
118 for (o = u->outputs; o; o = o->next) {
119 uint32_t r = base_rate;
120
121 if (o->total_latency < target_latency)
122 r -= (uint32_t) (((((double) target_latency - o->total_latency))/u->adjust_time)*r/ 1000000);
123 else if (o->total_latency > target_latency)
124 r += (uint32_t) (((((double) o->total_latency - target_latency))/u->adjust_time)*r/ 1000000);
125
126 if (r < (uint32_t) (base_rate*0.9) || r > (uint32_t) (base_rate*1.1))
127 pa_log(__FILE__": [%s] sample rates too different, not adjusting (%u vs. %u).\n", o->sink_input->name, base_rate, r);
128 else {
129 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);
130 pa_sink_input_set_rate(o->sink_input, r);
131 }
132 }
133 }
134
135 static void request_memblock(struct userdata *u) {
136 struct pa_memchunk chunk;
137 struct output *o;
138 assert(u && u->sink);
139
140 update_usage(u);
141
142 if (pa_sink_render(u->sink, RENDER_SIZE, &chunk) < 0)
143 return;
144
145 for (o = u->outputs; o; o = o->next)
146 pa_memblockq_push_align(o->memblockq, &chunk, 0);
147
148 pa_memblock_unref(chunk.memblock);
149 }
150
151 static void time_callback(struct pa_mainloop_api*a, struct pa_time_event* e, const struct timeval *tv, void *userdata) {
152 struct userdata *u = userdata;
153 struct timeval n;
154 assert(u && a && u->time_event == e);
155
156 adjust_rates(u);
157
158 gettimeofday(&n, NULL);
159 n.tv_sec += u->adjust_time;
160 u->sink->core->mainloop->time_restart(e, &n);
161 }
162
163 static int sink_input_peek_cb(struct pa_sink_input *i, struct pa_memchunk *chunk) {
164 struct output *o = i->userdata;
165 assert(i && o && o->sink_input && chunk);
166
167 if (pa_memblockq_peek(o->memblockq, chunk) >= 0)
168 return 0;
169
170 /* Try harder */
171 request_memblock(o->userdata);
172
173 return pa_memblockq_peek(o->memblockq, chunk);
174 }
175
176 static void sink_input_drop_cb(struct pa_sink_input *i, const struct pa_memchunk *chunk, size_t length) {
177 struct output *o = i->userdata;
178 assert(i && o && o->sink_input && chunk && length);
179
180 pa_memblockq_drop(o->memblockq, chunk, length);
181 o->counter += length;
182 }
183
184 static void sink_input_kill_cb(struct pa_sink_input *i) {
185 struct output *o = i->userdata;
186 assert(i && o && o->sink_input);
187 pa_module_unload_request(o->userdata->module);
188 clear_up(o->userdata);
189 }
190
191 static pa_usec_t sink_input_get_latency_cb(struct pa_sink_input *i) {
192 struct output *o = i->userdata;
193 assert(i && o && o->sink_input);
194
195 return pa_bytes_to_usec(pa_memblockq_get_length(o->memblockq), &i->sample_spec);
196 }
197
198 static pa_usec_t sink_get_latency_cb(struct pa_sink *s) {
199 struct userdata *u = s->userdata;
200 assert(s && u && u->sink && u->master);
201
202 return pa_sink_input_get_latency(u->master->sink_input);
203 }
204
205 static struct output *output_new(struct userdata *u, struct pa_sink *sink, int resample_method) {
206 struct output *o = NULL;
207 char t[256];
208 assert(u && sink && u->sink);
209
210 o = pa_xmalloc(sizeof(struct output));
211 o->userdata = u;
212
213 o->counter = 0;
214 o->memblockq = pa_memblockq_new(MEMBLOCKQ_MAXLENGTH, MEMBLOCKQ_MAXLENGTH, pa_frame_size(&u->sink->sample_spec), 0, 0, sink->core->memblock_stat);
215
216 snprintf(t, sizeof(t), "%s: output #%u", u->sink->name, u->n_outputs+1);
217 if (!(o->sink_input = pa_sink_input_new(sink, t, &u->sink->sample_spec, 1, resample_method)))
218 goto fail;
219
220 o->sink_input->get_latency = sink_input_get_latency_cb;
221 o->sink_input->peek = sink_input_peek_cb;
222 o->sink_input->drop = sink_input_drop_cb;
223 o->sink_input->kill = sink_input_kill_cb;
224 o->sink_input->userdata = o;
225 o->sink_input->owner = u->module;
226
227 PA_LLIST_PREPEND(struct output, u->outputs, o);
228 u->n_outputs++;
229 return o;
230
231 fail:
232
233 if (o) {
234 if (o->sink_input) {
235 pa_sink_input_disconnect(o->sink_input);
236 pa_sink_input_unref(o->sink_input);
237 }
238
239 if (o->memblockq)
240 pa_memblockq_free(o->memblockq);
241
242 pa_xfree(o);
243 }
244
245 return NULL;
246 }
247
248 static void output_free(struct output *o) {
249 assert(o);
250 PA_LLIST_REMOVE(struct output, o->userdata->outputs, o);
251 o->userdata->n_outputs--;
252 pa_memblockq_free(o->memblockq);
253 pa_sink_input_disconnect(o->sink_input);
254 pa_sink_input_unref(o->sink_input);
255 pa_xfree(o);
256 }
257
258 static void clear_up(struct userdata *u) {
259 struct output *o;
260 assert(u);
261
262 if (u->time_event) {
263 u->core->mainloop->time_free(u->time_event);
264 u->time_event = NULL;
265 }
266
267 while ((o = u->outputs))
268 output_free(o);
269
270 u->master = NULL;
271
272 if (u->sink) {
273 pa_sink_disconnect(u->sink);
274 pa_sink_unref(u->sink);
275 u->sink = NULL;
276 }
277 }
278
279 int pa__init(struct pa_core *c, struct pa_module*m) {
280 struct userdata *u;
281 struct pa_modargs *ma = NULL;
282 const char *master_name, *slaves, *rm;
283 struct pa_sink *master_sink;
284 char *n = NULL;
285 const char*split_state;
286 struct timeval tv;
287 int resample_method = -1;
288 assert(c && m);
289
290 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
291 pa_log(__FILE__": failed to parse module arguments\n");
292 goto fail;
293 }
294
295 if ((rm = pa_modargs_get_value(ma, "resample_method", NULL))) {
296 if ((resample_method = pa_parse_resample_method(rm)) < 0) {
297 pa_log(__FILE__": invalid resample method '%s'\n", rm);
298 goto fail;
299 }
300 }
301
302 u = pa_xmalloc(sizeof(struct userdata));
303 m->userdata = u;
304 u->sink = NULL;
305 u->n_outputs = 0;
306 u->master = NULL;
307 u->module = m;
308 u->core = c;
309 u->time_event = NULL;
310 u->adjust_time = DEFAULT_ADJUST_TIME;
311 PA_LLIST_HEAD_INIT(struct output, u->outputs);
312
313 if (pa_modargs_get_value_u32(ma, "adjust_time", &u->adjust_time) < 0) {
314 pa_log(__FILE__": failed to parse adjust_time value\n");
315 goto fail;
316 }
317
318 if (!(master_name = pa_modargs_get_value(ma, "master", NULL)) || !(slaves = pa_modargs_get_value(ma, "slaves", NULL))) {
319 pa_log(__FILE__": no master or slave sinks specified\n");
320 goto fail;
321 }
322
323 if (!(master_sink = pa_namereg_get(c, master_name, PA_NAMEREG_SINK, 1))) {
324 pa_log(__FILE__": invalid master sink '%s'\n", master_name);
325 goto fail;
326 }
327
328 if (!(u->sink = pa_sink_new(c, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &master_sink->sample_spec))) {
329 pa_log(__FILE__": failed to create sink\n");
330 goto fail;
331 }
332
333 pa_sink_set_owner(u->sink, m);
334 u->sink->description = pa_sprintf_malloc("Combined sink");
335 u->sink->get_latency = sink_get_latency_cb;
336 u->sink->userdata = u;
337
338 if (!(u->master = output_new(u, master_sink, resample_method))) {
339 pa_log(__FILE__": failed to create master sink input on sink '%s'.\n", u->sink->name);
340 goto fail;
341 }
342
343 split_state = NULL;
344 while ((n = pa_split(slaves, ",", &split_state))) {
345 struct pa_sink *slave_sink;
346
347 if (!(slave_sink = pa_namereg_get(c, n, PA_NAMEREG_SINK, 1))) {
348 pa_log(__FILE__": invalid slave sink '%s'\n", n);
349 goto fail;
350 }
351
352 pa_xfree(n);
353
354 if (!output_new(u, slave_sink, resample_method)) {
355 pa_log(__FILE__": failed to create slave sink input on sink '%s'.\n", slave_sink->name);
356 goto fail;
357 }
358 }
359
360 if (u->n_outputs <= 1)
361 pa_log(__FILE__": WARNING: no slave sinks specified.\n");
362
363 if (u->adjust_time > 0) {
364 gettimeofday(&tv, NULL);
365 tv.tv_sec += u->adjust_time;
366 u->time_event = c->mainloop->time_new(c->mainloop, &tv, time_callback, u);
367 }
368
369 pa_modargs_free(ma);
370 return 0;
371
372 fail:
373 pa_xfree(n);
374
375 if (ma)
376 pa_modargs_free(ma);
377
378 pa__done(c, m);
379 return -1;
380 }
381
382 void pa__done(struct pa_core *c, struct pa_module*m) {
383 struct userdata *u;
384 assert(c && m);
385
386 if (!(u = m->userdata))
387 return;
388
389 clear_up(u);
390 pa_xfree(u);
391 }
392
393