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