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