4 This file is part of polypaudio.
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.
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.
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
29 #include <polyp/xmalloc.h>
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>
41 #include "module-combine-symdef.h"
43 PA_MODULE_AUTHOR("Lennart Poettering")
44 PA_MODULE_DESCRIPTION("Combine multiple sinks to one")
45 PA_MODULE_VERSION(PACKAGE_VERSION)
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> "
55 "channel_map=<channel map> ")
57 #define DEFAULT_SINK_NAME "combined"
58 #define MEMBLOCKQ_MAXLENGTH (1024*170)
59 #define RENDER_SIZE (1024*10)
61 #define DEFAULT_ADJUST_TIME 20
63 static const char* const valid_modargs[] = {
77 struct userdata *userdata;
78 pa_sink_input *sink_input;
80 pa_memblockq *memblockq;
81 pa_usec_t total_latency;
82 PA_LLIST_FIELDS(struct output);
90 struct output *master;
91 pa_time_event *time_event;
94 PA_LLIST_HEAD(struct output, outputs);
97 static void output_free(struct output *o);
98 static void clear_up(struct userdata *u);
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));
107 static void adjust_rates(struct userdata *u) {
109 pa_usec_t max_sink_latency = 0, min_total_latency = (pa_usec_t) -1, target_latency;
111 assert(u && u->sink);
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;
116 o->total_latency = sink_latency + pa_sink_input_get_latency(o->sink_input);
118 if (sink_latency > max_sink_latency)
119 max_sink_latency = sink_latency;
121 if (o->total_latency < min_total_latency)
122 min_total_latency = o->total_latency;
125 assert(min_total_latency != (pa_usec_t) -1);
127 target_latency = max_sink_latency > min_total_latency ? max_sink_latency : min_total_latency;
129 pa_log_info(__FILE__": [%s] target latency is %0.0f usec.", u->sink->name, (float) target_latency);
131 base_rate = u->sink->sample_spec.rate;
133 for (o = u->outputs; o; o = o->next) {
134 uint32_t r = base_rate;
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);
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);
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);
150 static void request_memblock(struct userdata *u) {
153 assert(u && u->sink);
157 if (pa_sink_render(u->sink, RENDER_SIZE, &chunk) < 0)
160 for (o = u->outputs; o; o = o->next)
161 pa_memblockq_push_align(o->memblockq, &chunk);
163 pa_memblock_unref(chunk.memblock);
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;
169 assert(u && a && u->time_event == e);
174 n.tv_sec += u->adjust_time;
175 u->sink->core->mainloop->time_restart(e, &n);
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);
182 if (pa_memblockq_peek(o->memblockq, chunk) >= 0)
186 request_memblock(o->userdata);
188 return pa_memblockq_peek(o->memblockq, chunk);
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);
195 pa_memblockq_drop(o->memblockq, chunk, length);
196 o->counter += length;
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);
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);
210 return pa_bytes_to_usec(pa_memblockq_get_length(o->memblockq), &i->sample_spec);
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);
218 pa_sink_input_get_latency(u->master->sink_input) +
219 pa_sink_get_latency(u->master->sink_input->sink);
222 static struct output *output_new(struct userdata *u, pa_sink *sink, int resample_method) {
223 struct output *o = NULL;
225 assert(u && sink && u->sink);
227 o = pa_xmalloc(sizeof(struct output));
231 o->memblockq = pa_memblockq_new(
235 pa_frame_size(&u->sink->sample_spec),
239 sink->core->memblock_stat);
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)))
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;
252 PA_LLIST_PREPEND(struct output, u->outputs, o);
260 pa_sink_input_disconnect(o->sink_input);
261 pa_sink_input_unref(o->sink_input);
265 pa_memblockq_free(o->memblockq);
273 static void output_free(struct output *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);
283 static void clear_up(struct userdata *u) {
288 u->core->mainloop->time_free(u->time_event);
289 u->time_event = NULL;
292 while ((o = u->outputs))
298 pa_sink_disconnect(u->sink);
299 pa_sink_unref(u->sink);
304 int pa__init(pa_core *c, pa_module*m) {
306 pa_modargs *ma = NULL;
307 const char *master_name, *slaves, *rm;
308 pa_sink *master_sink;
310 const char*split_state;
312 int resample_method = -1;
318 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
319 pa_log(__FILE__": failed to parse module arguments");
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);
330 u = pa_xnew(struct userdata, 1);
337 u->time_event = NULL;
338 u->adjust_time = DEFAULT_ADJUST_TIME;
339 PA_LLIST_HEAD_INIT(struct output, u->outputs);
341 if (pa_modargs_get_value_u32(ma, "adjust_time", &u->adjust_time) < 0) {
342 pa_log(__FILE__": failed to parse adjust_time value");
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");
351 if (!(master_sink = pa_namereg_get(c, master_name, PA_NAMEREG_SINK, 1))) {
352 pa_log(__FILE__": invalid master sink '%s'", master_name);
356 ss = master_sink->sample_spec;
357 if ((pa_modargs_get_sample_spec(ma, &ss) < 0)) {
358 pa_log(__FILE__": invalid sample specification.");
362 if (ss.channels == master_sink->sample_spec.channels)
363 map = master_sink->channel_map;
365 pa_channel_map_init_auto(&map, ss.channels, PA_CHANNEL_MAP_DEFAULT);
367 if ((pa_modargs_get_channel_map(ma, &map) < 0)) {
368 pa_log(__FILE__": invalid channel map.");
372 if (ss.channels != map.channels) {
373 pa_log(__FILE__": channel map and sample specification don't match.");
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");
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;
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);
393 while ((n = pa_split(slaves, ",", &split_state))) {
396 if (!(slave_sink = pa_namereg_get(c, n, PA_NAMEREG_SINK, 1))) {
397 pa_log(__FILE__": invalid slave sink '%s'", n);
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);
409 if (u->n_outputs <= 1)
410 pa_log_warn(__FILE__": WARNING: no slave sinks specified.");
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);
431 void pa__done(pa_core *c, pa_module*m) {
435 if (!(u = m->userdata))