2 This file is part of PulseAudio.
4 Copyright 2004-2008 Lennart Poettering
6 PulseAudio 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.1 of the License,
9 or (at your option) any later version.
11 PulseAudio 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 PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 #include <pulsecore/pulsecore-config.h>
27 #include <pulse/rtclock.h>
28 #include <pulse/timeval.h>
29 #include <pulse/xmalloc.h>
31 #include <pulsecore/macro.h>
32 #include <pulsecore/module.h>
33 #include <pulsecore/llist.h>
34 #include <pulsecore/sink.h>
35 #include <pulsecore/sink-input.h>
36 #include <pulsecore/memblockq.h>
37 #include <pulsecore/log.h>
38 #include <pulsecore/core-rtclock.h>
39 #include <pulsecore/core-util.h>
40 #include <pulsecore/modargs.h>
41 #include <pulsecore/namereg.h>
42 #include <pulsecore/thread.h>
43 #include <pulsecore/thread-mq.h>
44 #include <pulsecore/rtpoll.h>
45 #include <pulsecore/time-smoother.h>
46 #include <pulsecore/strlist.h>
48 #include "module-combine-sink-symdef.h"
50 PA_MODULE_AUTHOR("Lennart Poettering");
51 PA_MODULE_DESCRIPTION("Combine multiple sinks to one");
52 PA_MODULE_VERSION(PACKAGE_VERSION);
53 PA_MODULE_LOAD_ONCE(false);
55 "sink_name=<name for the sink> "
56 "sink_properties=<properties for the sink> "
57 "slaves=<slave sinks> "
58 "adjust_time=<how often to readjust rates in s> "
59 "resample_method=<method> "
60 "format=<sample format> "
62 "channels=<number of channels> "
63 "channel_map=<channel map>");
65 #define DEFAULT_SINK_NAME "combined"
67 #define MEMBLOCKQ_MAXLENGTH (1024*1024*16)
69 #define DEFAULT_ADJUST_TIME_USEC (10*PA_USEC_PER_SEC)
71 #define BLOCK_USEC (PA_USEC_PER_MSEC * 200)
73 static const char* const valid_modargs[] = {
89 SINK_MESSAGE_ADD_OUTPUT = PA_SINK_MESSAGE_MAX,
90 SINK_MESSAGE_REMOVE_OUTPUT,
92 SINK_MESSAGE_UPDATE_LATENCY,
93 SINK_MESSAGE_UPDATE_MAX_REQUEST,
94 SINK_MESSAGE_UPDATE_REQUESTED_LATENCY
98 SINK_INPUT_MESSAGE_POST = PA_SINK_INPUT_MESSAGE_MAX,
101 static void output_disable(struct output *o);
102 static void output_enable(struct output *o);
103 static void output_free(struct output *o);
104 static int output_create_sink_input(struct output *o);
105 static pa_sink_input *add_slave(struct userdata *u, pa_sink *sink);
106 static void remove_slave(struct userdata *u, pa_sink_input *i, pa_sink *s);
107 static int move_slave(struct userdata *u, pa_sink_input *i, pa_sink *s);
109 static void adjust_rates(struct userdata *u) {
111 pa_usec_t max_sink_latency = 0, min_total_latency = (pa_usec_t) -1, target_latency, avg_total_latency = 0;
117 pa_sink_assert_ref(u->sink);
119 if (pa_idxset_size(u->outputs) <= 0)
122 if (!PA_SINK_IS_OPENED(pa_sink_get_state(u->sink)))
125 PA_IDXSET_FOREACH(o, u->outputs, idx) {
126 pa_usec_t sink_latency;
128 if (!o->sink_input || !PA_SINK_IS_OPENED(pa_sink_get_state(o->sink)))
131 o->total_latency = pa_sink_input_get_latency(o->sink_input, &sink_latency);
132 o->total_latency += sink_latency;
134 if (sink_latency > max_sink_latency)
135 max_sink_latency = sink_latency;
137 if (min_total_latency == (pa_usec_t) -1 || o->total_latency < min_total_latency)
138 min_total_latency = o->total_latency;
140 avg_total_latency += o->total_latency;
143 /* pa_log_debug("[%s] total=%0.2fms sink=%0.2fms ", o->sink->name, (double) o->total_latency / PA_USEC_PER_MSEC, (double) sink_latency / PA_USEC_PER_MSEC); */
145 if (o->total_latency > 10*PA_USEC_PER_SEC)
146 pa_log_warn("[%s] Total latency of output is very high (%0.2fms), most likely the audio timing in one of your drivers is broken.", o->sink->name, (double) o->total_latency / PA_USEC_PER_MSEC);
149 if (min_total_latency == (pa_usec_t) -1)
152 avg_total_latency /= n;
154 target_latency = max_sink_latency > min_total_latency ? max_sink_latency : min_total_latency;
157 pa_log_info("[%s] avg total latency is %0.2f msec.", u->sink->name, (double) avg_total_latency / PA_USEC_PER_MSEC);
158 pa_log_info("[%s] target latency is %0.2f msec.", u->sink->name, (double) target_latency / PA_USEC_PER_MSEC);
161 base_rate = u->sink->sample_spec.rate;
163 PA_IDXSET_FOREACH(o, u->outputs, idx) {
164 uint32_t new_rate = base_rate;
165 uint32_t current_rate;
167 if (!o->sink_input || !PA_SINK_IS_OPENED(pa_sink_get_state(o->sink)))
170 current_rate = o->sink_input->sample_spec.rate;
172 if (o->total_latency != target_latency)
173 new_rate += (uint32_t) (((double) o->total_latency - (double) target_latency) / (double) u->adjust_time * (double) new_rate);
175 if (new_rate < (uint32_t) (base_rate*0.8) || new_rate > (uint32_t) (base_rate*1.25)) {
176 pa_log_warn("[%s] sample rates too different, not adjusting (%u vs. %u).", o->sink_input->sink->name, base_rate, new_rate);
177 new_rate = base_rate;
179 if (base_rate < new_rate + 20 && new_rate < base_rate + 20)
180 new_rate = base_rate;
181 /* Do the adjustment in small steps; 2‰ can be considered inaudible */
182 if (new_rate < (uint32_t) (current_rate*0.998) || new_rate > (uint32_t) (current_rate*1.002)) {
183 /* pa_log_info("[%s] new rate of %u Hz not within 2‰ of %u Hz, forcing smaller adjustment", o->sink_input->sink->name, new_rate, current_rate); */
184 new_rate = PA_CLAMP(new_rate, (uint32_t) (current_rate*0.998), (uint32_t) (current_rate*1.002));
186 pa_log_info("[%s] new rate is %u Hz; ratio is %0.3f; latency is %0.2f msec.", o->sink_input->sink->name, new_rate, (double) new_rate / base_rate, (double) o->total_latency / PA_USEC_PER_MSEC);
188 pa_sink_input_set_rate(o->sink_input, new_rate);
191 pa_asyncmsgq_send(u->sink->asyncmsgq, PA_MSGOBJECT(u->sink), SINK_MESSAGE_UPDATE_LATENCY, NULL, (int64_t) avg_total_latency, NULL);
194 static void time_callback(pa_mainloop_api *a, pa_time_event *e, const struct timeval *t, void *userdata) {
195 struct userdata *u = userdata;
199 pa_assert(u->time_event == e);
203 pa_core_rttime_restart(u->core, e, pa_rtclock_now() + u->adjust_time);
206 static void process_render_null(struct userdata *u, pa_usec_t now) {
210 /* If we are not running, we cannot produce any data */
211 if (!pa_atomic_load(&u->thread_info.running))
214 if (u->thread_info.in_null_mode)
215 u->thread_info.timestamp = now;
217 while (u->thread_info.timestamp < now + u->block_usec) {
220 pa_sink_render(u->sink, u->sink->thread_info.max_request, &chunk);
221 pa_memblock_unref(chunk.memblock);
223 u->thread_info.counter += chunk.length;
225 /* pa_log_debug("Ate %lu bytes.", (unsigned long) chunk.length); */
226 u->thread_info.timestamp += pa_bytes_to_usec(chunk.length, &u->sink->sample_spec);
230 if (ate >= u->sink->thread_info.max_request)
234 /* pa_log_debug("Ate in sum %lu bytes (of %lu)", (unsigned long) ate, (unsigned long) nbytes); */
236 pa_smoother_put(u->thread_info.smoother, now,
237 pa_bytes_to_usec(u->thread_info.counter, &u->sink->sample_spec) - (u->thread_info.timestamp - now));
240 static void thread_func(void *userdata) {
241 struct userdata *u = userdata;
245 pa_log_debug("Thread starting up");
247 if (u->core->realtime_scheduling)
248 pa_make_realtime(u->core->realtime_priority+1);
250 pa_thread_mq_install(&u->thread_mq);
252 u->thread_info.timestamp = pa_rtclock_now();
253 u->thread_info.in_null_mode = false;
258 if (PA_SINK_IS_OPENED(u->sink->thread_info.state))
259 if (u->sink->thread_info.rewind_requested)
260 pa_sink_process_rewind(u->sink, 0);
262 /* If no outputs are connected, render some data and drop it immediately. */
263 if (PA_SINK_IS_OPENED(u->sink->thread_info.state) && !u->thread_info.active_outputs) {
266 now = pa_rtclock_now();
268 if (!u->thread_info.in_null_mode || u->thread_info.timestamp <= now)
269 process_render_null(u, now);
271 pa_rtpoll_set_timer_absolute(u->rtpoll, u->thread_info.timestamp);
272 u->thread_info.in_null_mode = true;
274 pa_rtpoll_set_timer_disabled(u->rtpoll);
275 u->thread_info.in_null_mode = false;
278 /* Hmm, nothing to do. Let's sleep */
279 if ((ret = pa_rtpoll_run(u->rtpoll, true)) < 0) {
280 pa_log_info("pa_rtpoll_run() = %i", ret);
289 /* If this was no regular exit from the loop we have to continue
290 * processing messages until we received PA_MESSAGE_SHUTDOWN */
291 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
292 pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
295 pa_log_debug("Thread shutting down");
298 /* Called from I/O thread context */
299 static void render_memblock(struct userdata *u, struct output *o, size_t length) {
303 /* We are run by the sink thread, on behalf of an output (o). The
304 * output is waiting for us, hence it is safe to access its
305 * mainblockq and asyncmsgq directly. */
307 /* If we are not running, we cannot produce any data */
308 if (!pa_atomic_load(&u->thread_info.running))
311 /* Maybe there's some data in the requesting output's queue
313 while (pa_asyncmsgq_process_one(o->inq) > 0)
316 /* Ok, now let's prepare some data if we really have to */
317 while (!pa_memblockq_is_readable(o->memblockq)) {
322 pa_sink_render(u->sink, length, &chunk);
324 u->thread_info.counter += chunk.length;
326 /* OK, let's send this data to the other threads */
327 PA_LLIST_FOREACH(j, u->thread_info.active_outputs) {
331 pa_asyncmsgq_post(j->inq, PA_MSGOBJECT(j->sink_input), SINK_INPUT_MESSAGE_POST, NULL, 0, &chunk, NULL);
334 /* And place it directly into the requesting output's queue */
335 pa_memblockq_push_align(o->memblockq, &chunk);
336 pa_memblock_unref(chunk.memblock);
340 /* Called from I/O thread context */
341 static void request_memblock(struct output *o, size_t length) {
343 pa_sink_input_assert_ref(o->sink_input);
344 pa_sink_assert_ref(o->userdata->sink);
346 /* If another thread already prepared some data we received
347 * the data over the asyncmsgq, hence let's first process
349 while (pa_asyncmsgq_process_one(o->inq) > 0)
352 /* Check whether we're now readable */
353 if (pa_memblockq_is_readable(o->memblockq))
356 /* OK, we need to prepare new data, but only if the sink is actually running */
357 if (pa_atomic_load(&o->userdata->thread_info.running))
358 pa_asyncmsgq_send(o->outq, PA_MSGOBJECT(o->userdata->sink), SINK_MESSAGE_NEED, o, (int64_t) length, NULL);
361 /* Called from I/O thread context */
362 static int sink_input_pop_cb(pa_sink_input *i, size_t nbytes, pa_memchunk *chunk) {
365 pa_sink_input_assert_ref(i);
366 pa_assert_se(o = i->userdata);
368 /* If necessary, get some new data */
369 request_memblock(o, nbytes);
371 /* pa_log("%s q size is %u + %u (%u/%u)", */
373 /* pa_memblockq_get_nblocks(o->memblockq), */
374 /* pa_memblockq_get_nblocks(i->thread_info.render_memblockq), */
375 /* pa_memblockq_get_maxrewind(o->memblockq), */
376 /* pa_memblockq_get_maxrewind(i->thread_info.render_memblockq)); */
378 if (pa_memblockq_peek(o->memblockq, chunk) < 0)
381 pa_memblockq_drop(o->memblockq, chunk->length);
386 /* Called from I/O thread context */
387 static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes) {
390 pa_sink_input_assert_ref(i);
391 pa_assert_se(o = i->userdata);
393 pa_memblockq_rewind(o->memblockq, nbytes);
396 /* Called from I/O thread context */
397 static void sink_input_update_max_rewind_cb(pa_sink_input *i, size_t nbytes) {
400 pa_sink_input_assert_ref(i);
401 pa_assert_se(o = i->userdata);
403 pa_memblockq_set_maxrewind(o->memblockq, nbytes);
406 /* Called from I/O thread context */
407 static void sink_input_update_max_request_cb(pa_sink_input *i, size_t nbytes) {
410 pa_sink_input_assert_ref(i);
411 pa_assert_se(o = i->userdata);
413 if (pa_atomic_load(&o->max_request) == (int) nbytes)
416 pa_atomic_store(&o->max_request, (int) nbytes);
417 pa_asyncmsgq_post(o->outq, PA_MSGOBJECT(o->userdata->sink), SINK_MESSAGE_UPDATE_MAX_REQUEST, NULL, 0, NULL, NULL);
420 /* Called from thread context */
421 static void sink_input_update_sink_requested_latency_cb(pa_sink_input *i) {
427 pa_sink_input_assert_ref(i);
428 pa_assert_se(o = i->userdata);
430 c = pa_sink_get_requested_latency_within_thread(i->sink);
432 if (c == (pa_usec_t) -1)
433 c = i->sink->thread_info.max_latency;
435 if (pa_atomic_load(&o->requested_latency) == (int) c)
438 pa_atomic_store(&o->requested_latency, (int) c);
439 pa_asyncmsgq_post(o->outq, PA_MSGOBJECT(o->userdata->sink), SINK_MESSAGE_UPDATE_REQUESTED_LATENCY, NULL, 0, NULL, NULL);
442 /* Called from I/O thread context */
443 static void sink_input_attach_cb(pa_sink_input *i) {
447 pa_sink_input_assert_ref(i);
448 pa_assert_se(o = i->userdata);
450 /* Set up the queue from the sink thread to us */
451 pa_assert(!o->inq_rtpoll_item_read && !o->outq_rtpoll_item_write);
453 o->inq_rtpoll_item_read = pa_rtpoll_item_new_asyncmsgq_read(
454 i->sink->thread_info.rtpoll,
455 PA_RTPOLL_LATE, /* This one is not that important, since we check for data in _peek() anyway. */
458 o->outq_rtpoll_item_write = pa_rtpoll_item_new_asyncmsgq_write(
459 i->sink->thread_info.rtpoll,
463 pa_sink_input_request_rewind(i, 0, false, true, true);
465 pa_atomic_store(&o->max_request, (int) pa_sink_input_get_max_request(i));
467 c = pa_sink_get_requested_latency_within_thread(i->sink);
468 pa_atomic_store(&o->requested_latency, (int) (c == (pa_usec_t) -1 ? 0 : c));
470 pa_asyncmsgq_post(o->outq, PA_MSGOBJECT(o->userdata->sink), SINK_MESSAGE_UPDATE_MAX_REQUEST, NULL, 0, NULL, NULL);
471 pa_asyncmsgq_post(o->outq, PA_MSGOBJECT(o->userdata->sink), SINK_MESSAGE_UPDATE_REQUESTED_LATENCY, NULL, 0, NULL, NULL);
474 /* Called from I/O thread context */
475 static void sink_input_detach_cb(pa_sink_input *i) {
478 pa_sink_input_assert_ref(i);
479 pa_assert_se(o = i->userdata);
481 if (o->inq_rtpoll_item_read) {
482 pa_rtpoll_item_free(o->inq_rtpoll_item_read);
483 o->inq_rtpoll_item_read = NULL;
486 if (o->outq_rtpoll_item_write) {
487 pa_rtpoll_item_free(o->outq_rtpoll_item_write);
488 o->outq_rtpoll_item_write = NULL;
492 /* Called from main context */
493 static void sink_input_kill_cb(pa_sink_input *i) {
496 pa_sink_input_assert_ref(i);
497 pa_assert_se(o = i->userdata);
499 pa_module_unload_request(o->userdata->module, true);
503 /* Called from thread context */
504 static int sink_input_process_msg(pa_msgobject *obj, int code, void *data, int64_t offset, pa_memchunk *chunk) {
505 struct output *o = PA_SINK_INPUT(obj)->userdata;
509 case PA_SINK_INPUT_MESSAGE_GET_LATENCY: {
512 *r = pa_bytes_to_usec(pa_memblockq_get_length(o->memblockq), &o->sink_input->sample_spec);
514 /* Fall through, the default handler will add in the extra
515 * latency added by the resampler */
519 case SINK_INPUT_MESSAGE_POST:
521 if (PA_SINK_IS_OPENED(o->sink_input->sink->thread_info.state))
522 pa_memblockq_push_align(o->memblockq, chunk);
524 pa_memblockq_flush_write(o->memblockq, true);
529 return pa_sink_input_process_msg(obj, code, data, offset, chunk);
532 /* Called from main context */
533 static void suspend(struct userdata *u) {
539 /* Let's suspend by unlinking all streams */
540 PA_IDXSET_FOREACH(o, u->outputs, idx)
543 pa_log_info("Device suspended...");
546 /* Called from main context */
547 static void unsuspend(struct userdata *u) {
554 PA_IDXSET_FOREACH(o, u->outputs, idx)
557 pa_log_info("Resumed successfully...");
560 /* Called from main context */
561 static int sink_set_state(pa_sink *sink, pa_sink_state_t state) {
564 pa_sink_assert_ref(sink);
565 pa_assert_se(u = sink->userdata);
567 /* Please note that in contrast to the ALSA modules we call
568 * suspend/unsuspend from main context here! */
571 case PA_SINK_SUSPENDED:
572 pa_assert(PA_SINK_IS_OPENED(pa_sink_get_state(u->sink)));
578 case PA_SINK_RUNNING:
580 if (pa_sink_get_state(u->sink) == PA_SINK_SUSPENDED)
585 case PA_SINK_UNLINKED:
587 case PA_SINK_INVALID_STATE:
594 /* Called from IO context */
595 static void update_max_request(struct userdata *u) {
596 size_t max_request = 0;
600 pa_sink_assert_io_context(u->sink);
602 /* Collects the max_request values of all streams and sets the
603 * largest one locally */
605 PA_LLIST_FOREACH(o, u->thread_info.active_outputs) {
606 size_t mr = (size_t) pa_atomic_load(&o->max_request);
608 if (mr > max_request)
612 if (max_request <= 0)
613 max_request = pa_usec_to_bytes(u->block_usec, &u->sink->sample_spec);
615 pa_sink_set_max_request_within_thread(u->sink, max_request);
618 /* Called from IO context */
619 static void update_fixed_latency(struct userdata *u) {
620 pa_usec_t fixed_latency = 0;
624 pa_sink_assert_io_context(u->sink);
626 /* Collects the requested_latency values of all streams and sets
627 * the largest one as fixed_latency locally */
629 PA_LLIST_FOREACH(o, u->thread_info.active_outputs) {
630 pa_usec_t rl = (size_t) pa_atomic_load(&o->requested_latency);
632 if (rl > fixed_latency)
636 if (fixed_latency <= 0)
637 fixed_latency = u->block_usec;
639 pa_sink_set_fixed_latency_within_thread(u->sink, fixed_latency);
642 /* Called from thread context of the io thread */
643 static void output_add_within_thread(struct output *o) {
645 pa_sink_assert_io_context(o->sink);
647 PA_LLIST_PREPEND(struct output, o->userdata->thread_info.active_outputs, o);
649 pa_assert(!o->outq_rtpoll_item_read && !o->inq_rtpoll_item_write);
651 o->outq_rtpoll_item_read = pa_rtpoll_item_new_asyncmsgq_read(
653 PA_RTPOLL_EARLY-1, /* This item is very important */
655 o->inq_rtpoll_item_write = pa_rtpoll_item_new_asyncmsgq_write(
661 /* Called from thread context of the io thread */
662 static void output_remove_within_thread(struct output *o) {
664 pa_sink_assert_io_context(o->sink);
666 PA_LLIST_REMOVE(struct output, o->userdata->thread_info.active_outputs, o);
668 if (o->outq_rtpoll_item_read) {
669 pa_rtpoll_item_free(o->outq_rtpoll_item_read);
670 o->outq_rtpoll_item_read = NULL;
673 if (o->inq_rtpoll_item_write) {
674 pa_rtpoll_item_free(o->inq_rtpoll_item_write);
675 o->inq_rtpoll_item_write = NULL;
679 /* Called from thread context of the io thread */
680 static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
681 struct userdata *u = PA_SINK(o)->userdata;
685 case PA_SINK_MESSAGE_SET_STATE: {
686 bool running = (PA_PTR_TO_UINT(data) == PA_SINK_RUNNING);
688 pa_atomic_store(&u->thread_info.running, running);
691 pa_smoother_resume(u->thread_info.smoother, pa_rtclock_now(), true);
693 pa_smoother_pause(u->thread_info.smoother, pa_rtclock_now());
698 case PA_SINK_MESSAGE_GET_LATENCY: {
699 pa_usec_t x, y, c, *delay = data;
701 x = pa_rtclock_now();
702 y = pa_smoother_get(u->thread_info.smoother, x);
704 c = pa_bytes_to_usec(u->thread_info.counter, &u->sink->sample_spec);
714 case SINK_MESSAGE_ADD_OUTPUT:
715 output_add_within_thread(data);
716 update_max_request(u);
717 update_fixed_latency(u);
720 case SINK_MESSAGE_REMOVE_OUTPUT:
721 output_remove_within_thread(data);
722 update_max_request(u);
723 update_fixed_latency(u);
726 case SINK_MESSAGE_NEED:
727 render_memblock(u, (struct output*) data, (size_t) offset);
730 case SINK_MESSAGE_UPDATE_LATENCY: {
731 pa_usec_t x, y, latency = (pa_usec_t) offset;
733 x = pa_rtclock_now();
734 y = pa_bytes_to_usec(u->thread_info.counter, &u->sink->sample_spec);
741 pa_smoother_put(u->thread_info.smoother, x, y);
745 case SINK_MESSAGE_UPDATE_MAX_REQUEST:
746 update_max_request(u);
749 case SINK_MESSAGE_UPDATE_REQUESTED_LATENCY:
750 update_fixed_latency(u);
754 return pa_sink_process_msg(o, code, data, offset, chunk);
757 static void update_description(struct userdata *u) {
768 if (pa_idxset_isempty(u->outputs)) {
769 pa_sink_set_description(u->sink, "Simultaneous output");
773 t = pa_xstrdup("Simultaneous output to");
775 PA_IDXSET_FOREACH(o, u->outputs, idx) {
779 e = pa_sprintf_malloc("%s %s", t, pa_strnull(pa_proplist_gets(o->sink->proplist, PA_PROP_DEVICE_DESCRIPTION)));
782 e = pa_sprintf_malloc("%s, %s", t, pa_strnull(pa_proplist_gets(o->sink->proplist, PA_PROP_DEVICE_DESCRIPTION)));
788 pa_sink_set_description(u->sink, t);
792 static int output_create_sink_input(struct output *o) {
793 pa_sink_input_new_data data;
800 pa_sink_input_new_data_init(&data);
801 pa_sink_input_new_data_set_sink(&data, o->sink, false);
802 data.driver = __FILE__;
803 pa_proplist_setf(data.proplist, PA_PROP_MEDIA_NAME, "Simultaneous output on %s", pa_strnull(pa_proplist_gets(o->sink->proplist, PA_PROP_DEVICE_DESCRIPTION)));
804 pa_proplist_sets(data.proplist, PA_PROP_MEDIA_ROLE, "filter");
805 pa_sink_input_new_data_set_sample_spec(&data, &o->userdata->sink->sample_spec);
806 pa_sink_input_new_data_set_channel_map(&data, &o->userdata->sink->channel_map);
807 data.module = o->userdata->module;
808 data.resample_method = o->userdata->resample_method;
809 data.flags = PA_SINK_INPUT_VARIABLE_RATE|PA_SINK_INPUT_DONT_MOVE|PA_SINK_INPUT_NO_CREATE_ON_SUSPEND;
811 pa_sink_input_new(&o->sink_input, o->userdata->core, &data);
813 pa_sink_input_new_data_done(&data);
818 o->sink_input->parent.process_msg = sink_input_process_msg;
819 o->sink_input->pop = sink_input_pop_cb;
820 o->sink_input->process_rewind = sink_input_process_rewind_cb;
821 o->sink_input->update_max_rewind = sink_input_update_max_rewind_cb;
822 o->sink_input->update_max_request = sink_input_update_max_request_cb;
823 o->sink_input->update_sink_requested_latency = sink_input_update_sink_requested_latency_cb;
824 o->sink_input->attach = sink_input_attach_cb;
825 o->sink_input->detach = sink_input_detach_cb;
826 o->sink_input->kill = sink_input_kill_cb;
827 o->sink_input->userdata = o;
829 pa_sink_input_set_requested_latency(o->sink_input, BLOCK_USEC);
834 /* Called from main context */
835 static struct output *output_new(struct userdata *u, pa_sink *sink) {
842 o = pa_xnew0(struct output, 1);
844 o->inq = pa_asyncmsgq_new(0);
845 o->outq = pa_asyncmsgq_new(0);
847 o->memblockq = pa_memblockq_new(
848 "module-combine-sink output memblockq",
852 &u->sink->sample_spec,
858 pa_assert_se(pa_idxset_put(u->outputs, o, NULL) == 0);
859 update_description(u);
864 /* Called from main context */
865 static void output_free(struct output *o) {
870 pa_assert_se(pa_idxset_remove_by_data(o->userdata->outputs, o, NULL));
871 update_description(o->userdata);
873 if (o->inq_rtpoll_item_read)
874 pa_rtpoll_item_free(o->inq_rtpoll_item_read);
875 if (o->inq_rtpoll_item_write)
876 pa_rtpoll_item_free(o->inq_rtpoll_item_write);
878 if (o->outq_rtpoll_item_read)
879 pa_rtpoll_item_free(o->outq_rtpoll_item_read);
880 if (o->outq_rtpoll_item_write)
881 pa_rtpoll_item_free(o->outq_rtpoll_item_write);
884 pa_asyncmsgq_unref(o->inq);
887 pa_asyncmsgq_unref(o->outq);
890 pa_memblockq_free(o->memblockq);
895 /* Called from main context */
896 static void output_enable(struct output *o) {
902 /* This might cause the sink to be resumed. The state change hook
903 * of the sink might hence be called from here, which might then
904 * cause us to be called in a loop. Make sure that state changes
905 * for this output don't cause this loop by setting a flag here */
906 o->ignore_state_change = true;
908 if (output_create_sink_input(o) >= 0) {
910 if (pa_sink_get_state(o->sink) != PA_SINK_INIT) {
912 /* First we register the output. That means that the sink
913 * will start to pass data to this output. */
914 pa_asyncmsgq_send(o->userdata->sink->asyncmsgq, PA_MSGOBJECT(o->userdata->sink), SINK_MESSAGE_ADD_OUTPUT, o, 0, NULL);
916 /* Then we enable the sink input. That means that the sink
917 * is now asked for new data. */
918 pa_sink_input_put(o->sink_input);
921 /* Hmm the sink is not yet started, do things right here */
922 output_add_within_thread(o);
925 o->ignore_state_change = false;
928 /* Called from main context */
929 static void output_disable(struct output *o) {
935 /* First we disable the sink input. That means that the sink is
936 * not asked for new data anymore */
937 pa_sink_input_unlink(o->sink_input);
939 /* Then we unregister the output. That means that the sink doesn't
940 * pass any further data to this output */
941 pa_asyncmsgq_send(o->userdata->sink->asyncmsgq, PA_MSGOBJECT(o->userdata->sink), SINK_MESSAGE_REMOVE_OUTPUT, o, 0, NULL);
943 /* Now deallocate the stream */
944 pa_sink_input_unref(o->sink_input);
945 o->sink_input = NULL;
947 /* Finally, drop all queued data */
948 pa_memblockq_flush_write(o->memblockq, true);
949 pa_asyncmsgq_flush(o->inq, false);
950 pa_asyncmsgq_flush(o->outq, false);
953 /* Called from main context */
954 static void output_verify(struct output *o) {
957 if (PA_SINK_IS_OPENED(pa_sink_get_state(o->userdata->sink)))
963 /* Called from main context */
964 static bool is_suitable_sink(struct userdata *u, pa_sink *s) {
967 pa_sink_assert_ref(s);
972 if (!(s->flags & PA_SINK_HARDWARE))
975 if (!(s->flags & PA_SINK_LATENCY))
978 if ((t = pa_proplist_gets(s->proplist, PA_PROP_DEVICE_CLASS)))
979 if (!pa_streq(t, "sound"))
985 /* Called from main context */
986 static pa_hook_result_t sink_put_hook_cb(pa_core *c, pa_sink *s, struct userdata* u) {
989 pa_core_assert_ref(c);
990 pa_sink_assert_ref(s);
994 if (!is_suitable_sink(u, s))
997 /* Check if the sink is a previously unlinked slave (non-automatic mode) */
998 pa_strlist *l = u->unlinked_slaves;
1000 while (l && !pa_streq(pa_strlist_data(l), s->name))
1001 l = pa_strlist_next(l);
1004 pa_log_debug("This sink is not previously unlinked, so returning from sink put cb");
1008 u->unlinked_slaves = pa_strlist_remove(u->unlinked_slaves, s->name);
1011 pa_log_info("Configuring new sink: %s", s->name);
1012 if (!(o = output_new(u, s))) {
1013 pa_log("Failed to create sink input on sink '%s'.", s->name);
1022 /* Called from main context */
1023 static struct output* find_output(struct userdata *u, pa_sink *s) {
1033 PA_IDXSET_FOREACH(o, u->outputs, idx)
1040 /* Called from main context */
1041 static pa_hook_result_t sink_unlink_hook_cb(pa_core *c, pa_sink *s, struct userdata* u) {
1045 pa_sink_assert_ref(s);
1048 if (!(o = find_output(u, s)))
1051 pa_log_info("Unconfiguring sink: %s", s->name);
1053 if (!u->automatic && !u->no_reattach) {
1054 u->unlinked_slaves = pa_strlist_prepend(u->unlinked_slaves, s->name);
1055 pa_log_debug("Adding sink %s to the combine modules unlinked slaves list", s->name);
1063 /* Called from main context */
1064 static pa_hook_result_t sink_state_changed_hook_cb(pa_core *c, pa_sink *s, struct userdata* u) {
1067 if (!(o = find_output(u, s)))
1070 /* This state change might be triggered because we are creating a
1071 * stream here, in that case we don't want to create it a second
1072 * time here and enter a loop */
1073 if (o->ignore_state_change)
1081 int pa__init(pa_module*m) {
1083 pa_modargs *ma = NULL;
1084 const char *slaves, *rm;
1085 int resample_method = PA_RESAMPLER_TRIVIAL;
1090 pa_sink_new_data data;
1091 uint32_t adjust_time_sec;
1095 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
1096 pa_log("failed to parse module arguments");
1100 if ((rm = pa_modargs_get_value(ma, "resample_method", NULL))) {
1101 if ((resample_method = pa_parse_resample_method(rm)) < 0) {
1102 pa_log("invalid resample method '%s'", rm);
1107 m->userdata = u = pa_xnew0(struct userdata, 1);
1110 u->rtpoll = pa_rtpoll_new();
1111 pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
1112 u->resample_method = resample_method;
1113 u->outputs = pa_idxset_new(NULL, NULL);
1114 u->thread_info.smoother = pa_smoother_new(
1123 u->add_slave = add_slave;
1124 u->remove_slave = remove_slave;
1125 u->move_slave = move_slave;
1127 adjust_time_sec = DEFAULT_ADJUST_TIME_USEC / PA_USEC_PER_SEC;
1128 if (pa_modargs_get_value_u32(ma, "adjust_time", &adjust_time_sec) < 0) {
1129 pa_log("Failed to parse adjust_time value");
1133 if (adjust_time_sec != DEFAULT_ADJUST_TIME_USEC / PA_USEC_PER_SEC)
1134 u->adjust_time = adjust_time_sec * PA_USEC_PER_SEC;
1136 u->adjust_time = DEFAULT_ADJUST_TIME_USEC;
1138 slaves = pa_modargs_get_value(ma, "slaves", NULL);
1139 u->automatic = !slaves;
1141 ss = m->core->default_sample_spec;
1142 map = m->core->default_channel_map;
1144 /* Check the specified slave sinks for sample_spec and channel_map to use for the combined sink */
1145 if (!u->automatic) {
1146 const char*split_state = NULL;
1148 pa_sample_spec slaves_spec;
1149 pa_channel_map slaves_map;
1150 bool is_first_slave = true;
1152 pa_sample_spec_init(&slaves_spec);
1154 while ((n = pa_split(slaves, ",", &split_state))) {
1155 pa_sink *slave_sink;
1157 if (!(slave_sink = pa_namereg_get(m->core, n, PA_NAMEREG_SINK))) {
1158 pa_log("Invalid slave sink '%s'", n);
1165 if (is_first_slave) {
1166 slaves_spec = slave_sink->sample_spec;
1167 slaves_map = slave_sink->channel_map;
1168 is_first_slave = false;
1170 if (slaves_spec.format != slave_sink->sample_spec.format)
1171 slaves_spec.format = PA_SAMPLE_INVALID;
1173 if (slaves_spec.rate < slave_sink->sample_spec.rate)
1174 slaves_spec.rate = slave_sink->sample_spec.rate;
1176 if (!pa_channel_map_equal(&slaves_map, &slave_sink->channel_map))
1177 slaves_spec.channels = 0;
1181 if (!is_first_slave) {
1182 if (slaves_spec.format != PA_SAMPLE_INVALID)
1183 ss.format = slaves_spec.format;
1185 ss.rate = slaves_spec.rate;
1187 if (slaves_spec.channels > 0) {
1189 ss.channels = slaves_map.channels;
1194 if ((pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0)) {
1195 pa_log("Invalid sample specification.");
1199 pa_sink_new_data_init(&data);
1200 data.namereg_fail = false;
1201 data.driver = __FILE__;
1203 pa_sink_new_data_set_name(&data, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME));
1204 pa_sink_new_data_set_sample_spec(&data, &ss);
1205 pa_sink_new_data_set_channel_map(&data, &map);
1206 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_CLASS, "filter");
1209 pa_proplist_sets(data.proplist, "combine.slaves", slaves);
1211 if (pa_modargs_get_proplist(ma, "sink_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
1212 pa_log("Invalid properties");
1213 pa_sink_new_data_done(&data);
1217 /* Check proplist for a description & fill in a default value if not */
1218 u->auto_desc = false;
1219 if (NULL == pa_proplist_gets(data.proplist, PA_PROP_DEVICE_DESCRIPTION)) {
1220 u->auto_desc = true;
1221 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Simultaneous Output");
1224 u->sink = pa_sink_new(m->core, &data, PA_SINK_LATENCY);
1225 pa_sink_new_data_done(&data);
1228 pa_log("Failed to create sink");
1232 u->sink->parent.process_msg = sink_process_msg;
1233 u->sink->set_state = sink_set_state;
1234 u->sink->userdata = u;
1236 pa_sink_set_rtpoll(u->sink, u->rtpoll);
1237 pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
1239 u->block_usec = BLOCK_USEC;
1240 pa_sink_set_max_request(u->sink, pa_usec_to_bytes(u->block_usec, &u->sink->sample_spec));
1242 if (!u->automatic) {
1243 const char*split_state;
1247 /* The slaves have been specified manually */
1250 while ((n = pa_split(slaves, ",", &split_state))) {
1251 pa_sink *slave_sink;
1253 if (!(slave_sink = pa_namereg_get(m->core, n, PA_NAMEREG_SINK)) || slave_sink == u->sink) {
1254 pa_log("Invalid slave sink '%s'", n);
1261 if (!output_new(u, slave_sink)) {
1262 pa_log("Failed to create slave sink input on sink '%s'.", slave_sink->name);
1267 if (pa_idxset_size(u->outputs) < 1)
1268 pa_log_warn("No slave sinks specified.");
1270 u->sink_put_slot = NULL;
1275 /* We're in automatic mode, we add every sink that matches our needs */
1277 PA_IDXSET_FOREACH(s, m->core->sinks, idx) {
1279 if (!is_suitable_sink(u, s))
1282 if (!output_new(u, s)) {
1283 pa_log("Failed to create sink input on sink '%s'.", s->name);
1289 u->sink_put_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_PUT], PA_HOOK_LATE, (pa_hook_cb_t) sink_put_hook_cb, u);
1290 u->sink_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_UNLINK], PA_HOOK_EARLY, (pa_hook_cb_t) sink_unlink_hook_cb, u);
1291 u->sink_state_changed_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_STATE_CHANGED], PA_HOOK_NORMAL, (pa_hook_cb_t) sink_state_changed_hook_cb, u);
1293 if (!(u->thread = pa_thread_new("combine", thread_func, u))) {
1294 pa_log("Failed to create thread.");
1298 /* Activate the sink and the sink inputs */
1299 pa_sink_put(u->sink);
1301 PA_IDXSET_FOREACH(o, u->outputs, idx)
1304 if (u->adjust_time > 0)
1305 u->time_event = pa_core_rttime_new(m->core, pa_rtclock_now() + u->adjust_time, time_callback, u);
1307 pa_modargs_free(ma);
1314 pa_modargs_free(ma);
1321 void pa__done(pa_module*m) {
1327 if (!(u = m->userdata))
1330 pa_strlist_free(u->unlinked_slaves);
1332 if (u->sink_put_slot)
1333 pa_hook_slot_free(u->sink_put_slot);
1335 if (u->sink_unlink_slot)
1336 pa_hook_slot_free(u->sink_unlink_slot);
1338 if (u->sink_state_changed_slot)
1339 pa_hook_slot_free(u->sink_state_changed_slot);
1342 while ((o = pa_idxset_first(u->outputs, NULL)))
1345 pa_idxset_free(u->outputs, NULL);
1349 pa_sink_unlink(u->sink);
1352 pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
1353 pa_thread_free(u->thread);
1356 pa_thread_mq_done(&u->thread_mq);
1359 pa_sink_unref(u->sink);
1362 pa_rtpoll_free(u->rtpoll);
1365 u->core->mainloop->time_free(u->time_event);
1367 if (u->thread_info.smoother)
1368 pa_smoother_free(u->thread_info.smoother);
1373 static pa_sink_input *add_slave(struct userdata *u, pa_sink *sink) {
1379 pa_log_debug("Adding a slave to module combine");
1381 if (!(o = output_new(u, sink))) {
1382 pa_log("Failed to create slave sink input on sink '%s'.", sink->name);
1388 return o->sink_input;
1391 static void remove_slave(struct userdata *u, pa_sink_input *i, pa_sink *s) {
1400 o = find_output(u, s);
1403 pa_log_debug("Couldn't find output in module combine");
1411 static int move_slave(struct userdata *u, pa_sink_input *i, pa_sink *s) {
1421 o = find_output(u, i->sink);
1424 pa_log_debug("Could not find output with sink %s for moving", s->name);
1431 i->flags &= ~(pa_sink_input_flags_t)PA_SINK_INPUT_DONT_MOVE;
1433 sts = pa_sink_input_move_to(i, s, false);
1435 i->flags |= (pa_sink_input_flags_t)PA_SINK_INPUT_DONT_MOVE;