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