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