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