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