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