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