b31fe56ea2a0df8fc8257cab0fbe7999aca0db9c
[profile/ivi/pulseaudio.git] / 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 <polyp/xmalloc.h>
30
31 #include <polypcore/module.h>
32 #include <polypcore/llist.h>
33 #include <polypcore/sink.h>
34 #include <polypcore/sink-input.h>
35 #include <polypcore/memblockq.h>
36 #include <polypcore/log.h>
37 #include <polypcore/util.h>
38 #include <polypcore/modargs.h>
39 #include <polypcore/namereg.h>
40
41 #include "module-combine-symdef.h"
42
43 PA_MODULE_AUTHOR("Lennart Poettering")
44 PA_MODULE_DESCRIPTION("Combine multiple sinks to one")
45 PA_MODULE_VERSION(PACKAGE_VERSION)
46 PA_MODULE_USAGE(
47         "sink_name=<name for the sink> "
48         "master=<master sink> "
49         "slaves=<slave sinks> "
50         "adjust_time=<seconds> "
51         "resample_method=<method> "
52         "format=<sample format> "
53         "channels=<number of channels> "
54         "rate=<sample rate> "
55         "channel_map=<channel map> ")
56
57 #define DEFAULT_SINK_NAME "combined"
58 #define MEMBLOCKQ_MAXLENGTH (1024*170)
59 #define RENDER_SIZE (1024*10)
60
61 #define DEFAULT_ADJUST_TIME 20
62
63 static const char* const valid_modargs[] = {
64     "sink_name",
65     "master",
66     "slaves",
67     "adjust_time",
68     "resample_method",
69     "format",
70     "channels",
71     "rate",
72     "channel_map",
73     NULL
74 };
75
76 struct output {
77     struct userdata *userdata;
78     pa_sink_input *sink_input;
79     size_t counter;
80     pa_memblockq *memblockq;
81     pa_usec_t total_latency;
82     PA_LLIST_FIELDS(struct output);
83 };
84
85 struct userdata {
86     pa_module *module;
87     pa_core *core;
88     pa_sink *sink;
89     unsigned n_outputs;
90     struct output *master;
91     pa_time_event *time_event;
92     uint32_t adjust_time;
93     
94     PA_LLIST_HEAD(struct output, outputs);
95 };
96
97 static void output_free(struct output *o);
98 static void clear_up(struct userdata *u);
99
100 static void update_usage(struct userdata *u) {
101     pa_module_set_used(u->module,
102                        (u->sink ? pa_idxset_size(u->sink->inputs) : 0) +
103                        (u->sink ? pa_idxset_size(u->sink->monitor_source->outputs) : 0));
104 }
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(__FILE__": [%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(__FILE__": [%s] sample rates too different, not adjusting (%u vs. %u).", o->sink_input->name, base_rate, r);
143         else {
144             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);
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 struct output *output_new(struct userdata *u, pa_sink *sink, int resample_method) {
223     struct output *o = NULL;
224     char t[256];
225     assert(u && sink && u->sink);
226     
227     o = pa_xmalloc(sizeof(struct output));
228     o->userdata = u;
229     
230     o->counter = 0;
231     o->memblockq = pa_memblockq_new(
232             0,
233             MEMBLOCKQ_MAXLENGTH,
234             MEMBLOCKQ_MAXLENGTH,
235             pa_frame_size(&u->sink->sample_spec),
236             1,
237             0,
238             NULL,
239             sink->core->memblock_stat);
240
241     snprintf(t, sizeof(t), "%s: output #%u", u->sink->name, u->n_outputs+1);
242     if (!(o->sink_input = pa_sink_input_new(sink, __FILE__, t, &u->sink->sample_spec, &u->sink->channel_map, NULL, 1, resample_method)))
243         goto fail;
244
245     o->sink_input->get_latency = sink_input_get_latency_cb;
246     o->sink_input->peek = sink_input_peek_cb;
247     o->sink_input->drop = sink_input_drop_cb;
248     o->sink_input->kill = sink_input_kill_cb;
249     o->sink_input->userdata = o;
250     o->sink_input->owner = u->module;
251     
252     PA_LLIST_PREPEND(struct output, u->outputs, o);
253     u->n_outputs++;
254     return o;
255
256 fail:
257
258     if (o) {
259         if (o->sink_input) {
260             pa_sink_input_disconnect(o->sink_input);
261             pa_sink_input_unref(o->sink_input);
262         }
263
264         if (o->memblockq)
265             pa_memblockq_free(o->memblockq);
266         
267         pa_xfree(o);
268     }
269
270     return NULL;
271 }
272
273 static void output_free(struct output *o) {
274     assert(o);
275     PA_LLIST_REMOVE(struct output, o->userdata->outputs, o);
276     o->userdata->n_outputs--;
277     pa_memblockq_free(o->memblockq);
278     pa_sink_input_disconnect(o->sink_input);
279     pa_sink_input_unref(o->sink_input);
280     pa_xfree(o);
281 }
282
283 static void clear_up(struct userdata *u) {
284     struct output *o;
285     assert(u);
286     
287     if (u->time_event) {
288         u->core->mainloop->time_free(u->time_event);
289         u->time_event = NULL;
290     }
291     
292     while ((o = u->outputs))
293         output_free(o);
294
295     u->master = NULL;
296     
297     if (u->sink) {
298         pa_sink_disconnect(u->sink);
299         pa_sink_unref(u->sink);
300         u->sink = NULL;
301     }
302 }
303
304 int pa__init(pa_core *c, pa_module*m) {
305     struct userdata *u;
306     pa_modargs *ma = NULL;
307     const char *master_name, *slaves, *rm;
308     pa_sink *master_sink;
309     char *n = NULL;
310     const char*split_state;
311     struct timeval tv;
312     int resample_method = -1;
313     pa_sample_spec ss;
314     pa_channel_map map;
315     
316     assert(c && m);
317
318     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
319         pa_log(__FILE__": failed to parse module arguments");
320         goto fail;
321     }
322
323     if ((rm = pa_modargs_get_value(ma, "resample_method", NULL))) {
324         if ((resample_method = pa_parse_resample_method(rm)) < 0) {
325             pa_log(__FILE__": invalid resample method '%s'", rm);
326             goto fail;
327         }
328     }
329     
330     u = pa_xnew(struct userdata, 1);
331     m->userdata = u;
332     u->sink = NULL;
333     u->n_outputs = 0;
334     u->master = NULL;
335     u->module = m;
336     u->core = c;
337     u->time_event = NULL;
338     u->adjust_time = DEFAULT_ADJUST_TIME;
339     PA_LLIST_HEAD_INIT(struct output, u->outputs);
340
341     if (pa_modargs_get_value_u32(ma, "adjust_time", &u->adjust_time) < 0) {
342         pa_log(__FILE__": failed to parse adjust_time value");
343         goto fail;
344     }
345     
346     if (!(master_name = pa_modargs_get_value(ma, "master", NULL)) || !(slaves = pa_modargs_get_value(ma, "slaves", NULL))) {
347         pa_log(__FILE__": no master or slave sinks specified");
348         goto fail;
349     }
350
351     if (!(master_sink = pa_namereg_get(c, master_name, PA_NAMEREG_SINK, 1))) {
352         pa_log(__FILE__": invalid master sink '%s'", master_name);
353         goto fail;
354     }
355
356     ss = master_sink->sample_spec;
357     if ((pa_modargs_get_sample_spec(ma, &ss) < 0)) {
358         pa_log(__FILE__": invalid sample specification.");
359         goto fail;
360     }
361
362     if (ss.channels == master_sink->sample_spec.channels)
363         map = master_sink->channel_map;
364     else
365         pa_channel_map_init_auto(&map, ss.channels, PA_CHANNEL_MAP_DEFAULT);
366
367     if ((pa_modargs_get_channel_map(ma, &map) < 0)) {
368         pa_log(__FILE__": invalid channel map.");
369         goto fail;
370     }
371
372     if (ss.channels != map.channels) {
373         pa_log(__FILE__": channel map and sample specification don't match.");
374         goto fail;
375     }
376     
377     if (!(u->sink = pa_sink_new(c, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, &map))) {
378         pa_log(__FILE__": failed to create sink");
379         goto fail;
380     }
381
382     pa_sink_set_owner(u->sink, m);
383     u->sink->description = pa_sprintf_malloc("Combined sink");
384     u->sink->get_latency = sink_get_latency_cb;
385     u->sink->userdata = u;
386     
387     if (!(u->master = output_new(u, master_sink, resample_method))) {
388         pa_log(__FILE__": failed to create master sink input on sink '%s'.", u->sink->name);
389         goto fail;
390     }
391     
392     split_state = NULL;
393     while ((n = pa_split(slaves, ",", &split_state))) {
394         pa_sink *slave_sink;
395         
396         if (!(slave_sink = pa_namereg_get(c, n, PA_NAMEREG_SINK, 1))) {
397             pa_log(__FILE__": invalid slave sink '%s'", n);
398             goto fail;
399         }
400
401         pa_xfree(n);
402
403         if (!output_new(u, slave_sink, resample_method)) {
404             pa_log(__FILE__": failed to create slave sink input on sink '%s'.", slave_sink->name);
405             goto fail;
406         }
407     }
408            
409     if (u->n_outputs <= 1)
410         pa_log_warn(__FILE__": WARNING: no slave sinks specified.");
411
412     if (u->adjust_time > 0) {
413         pa_gettimeofday(&tv);
414         tv.tv_sec += u->adjust_time;
415         u->time_event = c->mainloop->time_new(c->mainloop, &tv, time_callback, u);
416     }
417     
418     pa_modargs_free(ma);
419     return 0;    
420
421 fail:
422     pa_xfree(n);
423     
424     if (ma)
425         pa_modargs_free(ma);
426
427     pa__done(c, m);
428     return -1;
429 }
430
431 void pa__done(pa_core *c, pa_module*m) {
432     struct userdata *u;
433     assert(c && m);
434
435     if (!(u = m->userdata))
436         return;
437
438     clear_up(u);
439     pa_xfree(u);
440 }
441
442