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
29 #include <pulse/rtclock.h>
30 #include <pulse/timeval.h>
31 #include <pulse/xmalloc.h>
33 #include <pulsecore/macro.h>
34 #include <pulsecore/module.h>
35 #include <pulsecore/llist.h>
36 #include <pulsecore/sink.h>
37 #include <pulsecore/sink-input.h>
38 #include <pulsecore/memblockq.h>
39 #include <pulsecore/log.h>
40 #include <pulsecore/core-rtclock.h>
41 #include <pulsecore/core-util.h>
42 #include <pulsecore/modargs.h>
43 #include <pulsecore/namereg.h>
44 #include <pulsecore/mutex.h>
45 #include <pulsecore/thread.h>
46 #include <pulsecore/thread-mq.h>
47 #include <pulsecore/rtpoll.h>
48 #include <pulsecore/core-error.h>
49 #include <pulsecore/time-smoother.h>
50 #include <pulsecore/strlist.h>
52 #include "module-combine-symdef.h"
54 PA_MODULE_AUTHOR("Lennart Poettering");
55 PA_MODULE_DESCRIPTION("Combine multiple sinks to one");
56 PA_MODULE_VERSION(PACKAGE_VERSION);
57 PA_MODULE_LOAD_ONCE(FALSE);
59 "sink_name=<name for the sink> "
60 "sink_properties=<properties for the sink> "
61 "slaves=<slave sinks> "
62 "adjust_time=<how often to readjust rates in s> "
63 "resample_method=<method> "
64 "format=<sample format> "
66 "channels=<number of channels> "
67 "channel_map=<channel map>");
69 #define DEFAULT_SINK_NAME "combined"
71 #define MEMBLOCKQ_MAXLENGTH (1024*1024*16)
73 #define DEFAULT_ADJUST_TIME_USEC (10*PA_USEC_PER_SEC)
75 #define BLOCK_USEC (PA_USEC_PER_MSEC * 200)
77 static const char* const valid_modargs[] = {
91 struct userdata *userdata;
94 pa_sink_input *sink_input;
95 pa_bool_t ignore_state_change;
97 pa_asyncmsgq *inq, /* Message queue from the sink thread to this sink input */
98 *outq; /* Message queue from this sink input to the sink thread */
99 pa_rtpoll_item *inq_rtpoll_item_read, *inq_rtpoll_item_write;
100 pa_rtpoll_item *outq_rtpoll_item_read, *outq_rtpoll_item_write;
102 pa_memblockq *memblockq;
104 /* For communication of the stream latencies to the main thread */
105 pa_usec_t total_latency;
107 /* For coomunication of the stream parameters to the sink thread */
108 pa_atomic_t max_request;
109 pa_atomic_t requested_latency;
111 PA_LLIST_FIELDS(struct output);
120 pa_thread_mq thread_mq;
123 pa_time_event *time_event;
124 pa_usec_t adjust_time;
129 pa_strlist *unlinked_slaves;
131 pa_hook_slot *sink_put_slot, *sink_unlink_slot, *sink_state_changed_slot;
133 pa_resample_method_t resample_method;
135 pa_usec_t block_usec;
137 pa_idxset* outputs; /* managed in main context */
140 PA_LLIST_HEAD(struct output, active_outputs); /* managed in IO thread context */
141 pa_atomic_t running; /* we cache that value here, so that every thread can query it cheaply */
143 pa_bool_t in_null_mode;
144 pa_smoother *smoother;
150 SINK_MESSAGE_ADD_OUTPUT = PA_SINK_MESSAGE_MAX,
151 SINK_MESSAGE_REMOVE_OUTPUT,
153 SINK_MESSAGE_UPDATE_LATENCY,
154 SINK_MESSAGE_UPDATE_MAX_REQUEST,
155 SINK_MESSAGE_UPDATE_REQUESTED_LATENCY
159 SINK_INPUT_MESSAGE_POST = PA_SINK_INPUT_MESSAGE_MAX,
162 static void output_disable(struct output *o);
163 static void output_enable(struct output *o);
164 static void output_free(struct output *o);
165 static int output_create_sink_input(struct output *o);
167 static void adjust_rates(struct userdata *u) {
169 pa_usec_t max_sink_latency = 0, min_total_latency = (pa_usec_t) -1, target_latency, avg_total_latency = 0;
175 pa_sink_assert_ref(u->sink);
177 if (pa_idxset_size(u->outputs) <= 0)
180 if (!PA_SINK_IS_OPENED(pa_sink_get_state(u->sink)))
183 PA_IDXSET_FOREACH(o, u->outputs, idx) {
184 pa_usec_t sink_latency;
186 if (!o->sink_input || !PA_SINK_IS_OPENED(pa_sink_get_state(o->sink)))
189 o->total_latency = pa_sink_input_get_latency(o->sink_input, &sink_latency);
190 o->total_latency += sink_latency;
192 if (sink_latency > max_sink_latency)
193 max_sink_latency = sink_latency;
195 if (min_total_latency == (pa_usec_t) -1 || o->total_latency < min_total_latency)
196 min_total_latency = o->total_latency;
198 avg_total_latency += o->total_latency;
201 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);
203 if (o->total_latency > 10*PA_USEC_PER_SEC)
204 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);
207 if (min_total_latency == (pa_usec_t) -1)
210 avg_total_latency /= n;
212 target_latency = max_sink_latency > min_total_latency ? max_sink_latency : min_total_latency;
214 pa_log_info("[%s] avg total latency is %0.2f msec.", u->sink->name, (double) avg_total_latency / PA_USEC_PER_MSEC);
215 pa_log_info("[%s] target latency is %0.2f msec.", u->sink->name, (double) target_latency / PA_USEC_PER_MSEC);
217 base_rate = u->sink->sample_spec.rate;
219 PA_IDXSET_FOREACH(o, u->outputs, idx) {
220 uint32_t new_rate = base_rate;
221 uint32_t current_rate = o->sink_input->sample_spec.rate;
223 if (!o->sink_input || !PA_SINK_IS_OPENED(pa_sink_get_state(o->sink)))
226 if (o->total_latency != target_latency)
227 new_rate += (uint32_t) (((double) o->total_latency - (double) target_latency) / (double) u->adjust_time * (double) new_rate);
229 if (new_rate < (uint32_t) (base_rate*0.8) || new_rate > (uint32_t) (base_rate*1.25)) {
230 pa_log_warn("[%s] sample rates too different, not adjusting (%u vs. %u).", o->sink_input->sink->name, base_rate, new_rate);
231 new_rate = base_rate;
233 if (base_rate < new_rate + 20 && new_rate < base_rate + 20)
234 new_rate = base_rate;
235 /* Do the adjustment in small steps; 2‰ can be considered inaudible */
236 if (new_rate < (uint32_t) (current_rate*0.998) || new_rate > (uint32_t) (current_rate*1.002)) {
237 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);
238 new_rate = PA_CLAMP(new_rate, (uint32_t) (current_rate*0.998), (uint32_t) (current_rate*1.002));
240 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);
242 pa_sink_input_set_rate(o->sink_input, new_rate);
245 pa_asyncmsgq_send(u->sink->asyncmsgq, PA_MSGOBJECT(u->sink), SINK_MESSAGE_UPDATE_LATENCY, NULL, (int64_t) avg_total_latency, NULL);
248 static void time_callback(pa_mainloop_api *a, pa_time_event *e, const struct timeval *t, void *userdata) {
249 struct userdata *u = userdata;
253 pa_assert(u->time_event == e);
257 pa_core_rttime_restart(u->core, e, pa_rtclock_now() + u->adjust_time);
260 static void process_render_null(struct userdata *u, pa_usec_t now) {
264 if (u->thread_info.in_null_mode)
265 u->thread_info.timestamp = now;
267 while (u->thread_info.timestamp < now + u->block_usec) {
270 pa_sink_render(u->sink, u->sink->thread_info.max_request, &chunk);
271 pa_memblock_unref(chunk.memblock);
273 u->thread_info.counter += chunk.length;
275 /* pa_log_debug("Ate %lu bytes.", (unsigned long) chunk.length); */
276 u->thread_info.timestamp += pa_bytes_to_usec(chunk.length, &u->sink->sample_spec);
280 if (ate >= u->sink->thread_info.max_request)
284 /* pa_log_debug("Ate in sum %lu bytes (of %lu)", (unsigned long) ate, (unsigned long) nbytes); */
286 pa_smoother_put(u->thread_info.smoother, now,
287 pa_bytes_to_usec(u->thread_info.counter, &u->sink->sample_spec) - (u->thread_info.timestamp - now));
290 static void thread_func(void *userdata) {
291 struct userdata *u = userdata;
295 pa_log_debug("Thread starting up");
297 if (u->core->realtime_scheduling)
298 pa_make_realtime(u->core->realtime_priority+1);
300 pa_thread_mq_install(&u->thread_mq);
302 u->thread_info.timestamp = pa_rtclock_now();
303 u->thread_info.in_null_mode = FALSE;
308 if (PA_SINK_IS_OPENED(u->sink->thread_info.state))
309 if (u->sink->thread_info.rewind_requested)
310 pa_sink_process_rewind(u->sink, 0);
312 /* If no outputs are connected, render some data and drop it immediately. */
313 if (PA_SINK_IS_OPENED(u->sink->thread_info.state) && !u->thread_info.active_outputs) {
316 now = pa_rtclock_now();
318 if (!u->thread_info.in_null_mode || u->thread_info.timestamp <= now)
319 process_render_null(u, now);
321 pa_rtpoll_set_timer_absolute(u->rtpoll, u->thread_info.timestamp);
322 u->thread_info.in_null_mode = TRUE;
324 pa_rtpoll_set_timer_disabled(u->rtpoll);
325 u->thread_info.in_null_mode = FALSE;
328 /* Hmm, nothing to do. Let's sleep */
329 if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0) {
330 pa_log_info("pa_rtpoll_run() = %i", ret);
339 /* If this was no regular exit from the loop we have to continue
340 * processing messages until we received PA_MESSAGE_SHUTDOWN */
341 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
342 pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
345 pa_log_debug("Thread shutting down");
348 /* Called from I/O thread context */
349 static void render_memblock(struct userdata *u, struct output *o, size_t length) {
353 /* We are run by the sink thread, on behalf of an output (o). The
354 * output is waiting for us, hence it is safe to access its
355 * mainblockq and asyncmsgq directly. */
357 /* If we are not running, we cannot produce any data */
358 if (!pa_atomic_load(&u->thread_info.running))
361 /* Maybe there's some data in the requesting output's queue
363 while (pa_asyncmsgq_process_one(o->inq) > 0)
366 /* Ok, now let's prepare some data if we really have to */
367 while (!pa_memblockq_is_readable(o->memblockq)) {
372 pa_sink_render(u->sink, length, &chunk);
374 u->thread_info.counter += chunk.length;
376 /* OK, let's send this data to the other threads */
377 PA_LLIST_FOREACH(j, u->thread_info.active_outputs) {
381 pa_asyncmsgq_post(j->inq, PA_MSGOBJECT(j->sink_input), SINK_INPUT_MESSAGE_POST, NULL, 0, &chunk, NULL);
384 /* And place it directly into the requesting output's queue */
385 pa_memblockq_push_align(o->memblockq, &chunk);
386 pa_memblock_unref(chunk.memblock);
390 /* Called from I/O thread context */
391 static void request_memblock(struct output *o, size_t length) {
393 pa_sink_input_assert_ref(o->sink_input);
394 pa_sink_assert_ref(o->userdata->sink);
396 /* If another thread already prepared some data we received
397 * the data over the asyncmsgq, hence let's first process
399 while (pa_asyncmsgq_process_one(o->inq) > 0)
402 /* Check whether we're now readable */
403 if (pa_memblockq_is_readable(o->memblockq))
406 /* OK, we need to prepare new data, but only if the sink is actually running */
407 if (pa_atomic_load(&o->userdata->thread_info.running))
408 pa_asyncmsgq_send(o->outq, PA_MSGOBJECT(o->userdata->sink), SINK_MESSAGE_NEED, o, (int64_t) length, NULL);
411 /* Called from I/O thread context */
412 static int sink_input_pop_cb(pa_sink_input *i, size_t nbytes, pa_memchunk *chunk) {
415 pa_sink_input_assert_ref(i);
416 pa_assert_se(o = i->userdata);
418 /* If necessary, get some new data */
419 request_memblock(o, nbytes);
421 /* pa_log("%s q size is %u + %u (%u/%u)", */
423 /* pa_memblockq_get_nblocks(o->memblockq), */
424 /* pa_memblockq_get_nblocks(i->thread_info.render_memblockq), */
425 /* pa_memblockq_get_maxrewind(o->memblockq), */
426 /* pa_memblockq_get_maxrewind(i->thread_info.render_memblockq)); */
428 if (pa_memblockq_peek(o->memblockq, chunk) < 0)
431 pa_memblockq_drop(o->memblockq, chunk->length);
436 /* Called from I/O thread context */
437 static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes) {
440 pa_sink_input_assert_ref(i);
441 pa_assert_se(o = i->userdata);
443 pa_memblockq_rewind(o->memblockq, nbytes);
446 /* Called from I/O thread context */
447 static void sink_input_update_max_rewind_cb(pa_sink_input *i, size_t nbytes) {
450 pa_sink_input_assert_ref(i);
451 pa_assert_se(o = i->userdata);
453 pa_memblockq_set_maxrewind(o->memblockq, nbytes);
456 /* Called from I/O thread context */
457 static void sink_input_update_max_request_cb(pa_sink_input *i, size_t nbytes) {
460 pa_sink_input_assert_ref(i);
461 pa_assert_se(o = i->userdata);
463 if (pa_atomic_load(&o->max_request) == (int) nbytes)
466 pa_atomic_store(&o->max_request, (int) nbytes);
467 pa_asyncmsgq_post(o->outq, PA_MSGOBJECT(o->userdata->sink), SINK_MESSAGE_UPDATE_MAX_REQUEST, NULL, 0, NULL, NULL);
470 /* Called from thread context */
471 static void sink_input_update_sink_requested_latency_cb(pa_sink_input *i) {
477 pa_sink_input_assert_ref(i);
478 pa_assert_se(o = i->userdata);
480 c = pa_sink_get_requested_latency_within_thread(i->sink);
482 if (c == (pa_usec_t) -1)
483 c = i->sink->thread_info.max_latency;
485 if (pa_atomic_load(&o->requested_latency) == (int) c)
488 pa_atomic_store(&o->requested_latency, (int) c);
489 pa_asyncmsgq_post(o->outq, PA_MSGOBJECT(o->userdata->sink), SINK_MESSAGE_UPDATE_REQUESTED_LATENCY, NULL, 0, NULL, NULL);
492 /* Called from I/O thread context */
493 static void sink_input_attach_cb(pa_sink_input *i) {
497 pa_sink_input_assert_ref(i);
498 pa_assert_se(o = i->userdata);
500 /* Set up the queue from the sink thread to us */
501 pa_assert(!o->inq_rtpoll_item_read && !o->outq_rtpoll_item_write);
503 o->inq_rtpoll_item_read = pa_rtpoll_item_new_asyncmsgq_read(
504 i->sink->thread_info.rtpoll,
505 PA_RTPOLL_LATE, /* This one is not that important, since we check for data in _peek() anyway. */
508 o->outq_rtpoll_item_write = pa_rtpoll_item_new_asyncmsgq_write(
509 i->sink->thread_info.rtpoll,
513 pa_sink_input_request_rewind(i, 0, FALSE, TRUE, TRUE);
515 pa_atomic_store(&o->max_request, (int) pa_sink_input_get_max_request(i));
517 c = pa_sink_get_requested_latency_within_thread(i->sink);
518 pa_atomic_store(&o->requested_latency, (int) (c == (pa_usec_t) -1 ? 0 : c));
520 pa_asyncmsgq_post(o->outq, PA_MSGOBJECT(o->userdata->sink), SINK_MESSAGE_UPDATE_MAX_REQUEST, NULL, 0, NULL, NULL);
521 pa_asyncmsgq_post(o->outq, PA_MSGOBJECT(o->userdata->sink), SINK_MESSAGE_UPDATE_REQUESTED_LATENCY, NULL, 0, NULL, NULL);
524 /* Called from I/O thread context */
525 static void sink_input_detach_cb(pa_sink_input *i) {
528 pa_sink_input_assert_ref(i);
529 pa_assert_se(o = i->userdata);
531 if (o->inq_rtpoll_item_read) {
532 pa_rtpoll_item_free(o->inq_rtpoll_item_read);
533 o->inq_rtpoll_item_read = NULL;
536 if (o->outq_rtpoll_item_write) {
537 pa_rtpoll_item_free(o->outq_rtpoll_item_write);
538 o->outq_rtpoll_item_write = NULL;
542 /* Called from main context */
543 static void sink_input_kill_cb(pa_sink_input *i) {
546 pa_sink_input_assert_ref(i);
547 pa_assert_se(o = i->userdata);
549 pa_module_unload_request(o->userdata->module, TRUE);
553 /* Called from thread context */
554 static int sink_input_process_msg(pa_msgobject *obj, int code, void *data, int64_t offset, pa_memchunk *chunk) {
555 struct output *o = PA_SINK_INPUT(obj)->userdata;
559 case PA_SINK_INPUT_MESSAGE_GET_LATENCY: {
562 *r = pa_bytes_to_usec(pa_memblockq_get_length(o->memblockq), &o->sink_input->sample_spec);
564 /* Fall through, the default handler will add in the extra
565 * latency added by the resampler */
569 case SINK_INPUT_MESSAGE_POST:
571 if (PA_SINK_IS_OPENED(o->sink_input->sink->thread_info.state))
572 pa_memblockq_push_align(o->memblockq, chunk);
574 pa_memblockq_flush_write(o->memblockq, TRUE);
579 return pa_sink_input_process_msg(obj, code, data, offset, chunk);
582 /* Called from main context */
583 static void suspend(struct userdata *u) {
589 /* Let's suspend by unlinking all streams */
590 PA_IDXSET_FOREACH(o, u->outputs, idx)
593 pa_log_info("Device suspended...");
596 /* Called from main context */
597 static void unsuspend(struct userdata *u) {
604 PA_IDXSET_FOREACH(o, u->outputs, idx)
607 pa_log_info("Resumed successfully...");
610 /* Called from main context */
611 static int sink_set_state(pa_sink *sink, pa_sink_state_t state) {
614 pa_sink_assert_ref(sink);
615 pa_assert_se(u = sink->userdata);
617 /* Please note that in contrast to the ALSA modules we call
618 * suspend/unsuspend from main context here! */
621 case PA_SINK_SUSPENDED:
622 pa_assert(PA_SINK_IS_OPENED(pa_sink_get_state(u->sink)));
628 case PA_SINK_RUNNING:
630 if (pa_sink_get_state(u->sink) == PA_SINK_SUSPENDED)
635 case PA_SINK_UNLINKED:
637 case PA_SINK_INVALID_STATE:
644 /* Called from IO context */
645 static void update_max_request(struct userdata *u) {
646 size_t max_request = 0;
650 pa_sink_assert_io_context(u->sink);
652 /* Collects the max_request values of all streams and sets the
653 * largest one locally */
655 PA_LLIST_FOREACH(o, u->thread_info.active_outputs) {
656 size_t mr = (size_t) pa_atomic_load(&o->max_request);
658 if (mr > max_request)
662 if (max_request <= 0)
663 max_request = pa_usec_to_bytes(u->block_usec, &u->sink->sample_spec);
665 pa_sink_set_max_request_within_thread(u->sink, max_request);
668 /* Called from IO context */
669 static void update_fixed_latency(struct userdata *u) {
670 pa_usec_t fixed_latency = 0;
674 pa_sink_assert_io_context(u->sink);
676 /* Collects the requested_latency values of all streams and sets
677 * the largest one as fixed_latency locally */
679 PA_LLIST_FOREACH(o, u->thread_info.active_outputs) {
680 pa_usec_t rl = (size_t) pa_atomic_load(&o->requested_latency);
682 if (rl > fixed_latency)
686 if (fixed_latency <= 0)
687 fixed_latency = u->block_usec;
689 pa_sink_set_fixed_latency_within_thread(u->sink, fixed_latency);
692 /* Called from thread context of the io thread */
693 static void output_add_within_thread(struct output *o) {
695 pa_sink_assert_io_context(o->sink);
697 PA_LLIST_PREPEND(struct output, o->userdata->thread_info.active_outputs, o);
699 pa_assert(!o->outq_rtpoll_item_read && !o->inq_rtpoll_item_write);
701 o->outq_rtpoll_item_read = pa_rtpoll_item_new_asyncmsgq_read(
703 PA_RTPOLL_EARLY-1, /* This item is very important */
705 o->inq_rtpoll_item_write = pa_rtpoll_item_new_asyncmsgq_write(
711 /* Called from thread context of the io thread */
712 static void output_remove_within_thread(struct output *o) {
714 pa_sink_assert_io_context(o->sink);
716 PA_LLIST_REMOVE(struct output, o->userdata->thread_info.active_outputs, o);
718 if (o->outq_rtpoll_item_read) {
719 pa_rtpoll_item_free(o->outq_rtpoll_item_read);
720 o->outq_rtpoll_item_read = NULL;
723 if (o->inq_rtpoll_item_write) {
724 pa_rtpoll_item_free(o->inq_rtpoll_item_write);
725 o->inq_rtpoll_item_write = NULL;
729 /* Called from thread context of the io thread */
730 static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
731 struct userdata *u = PA_SINK(o)->userdata;
735 case PA_SINK_MESSAGE_SET_STATE:
736 pa_atomic_store(&u->thread_info.running, PA_PTR_TO_UINT(data) == PA_SINK_RUNNING);
738 if (PA_PTR_TO_UINT(data) == PA_SINK_SUSPENDED)
739 pa_smoother_pause(u->thread_info.smoother, pa_rtclock_now());
741 pa_smoother_resume(u->thread_info.smoother, pa_rtclock_now(), TRUE);
745 case PA_SINK_MESSAGE_GET_LATENCY: {
746 pa_usec_t x, y, c, *delay = data;
748 x = pa_rtclock_now();
749 y = pa_smoother_get(u->thread_info.smoother, x);
751 c = pa_bytes_to_usec(u->thread_info.counter, &u->sink->sample_spec);
761 case SINK_MESSAGE_ADD_OUTPUT:
762 output_add_within_thread(data);
763 update_max_request(u);
764 update_fixed_latency(u);
767 case SINK_MESSAGE_REMOVE_OUTPUT:
768 output_remove_within_thread(data);
769 update_max_request(u);
770 update_fixed_latency(u);
773 case SINK_MESSAGE_NEED:
774 render_memblock(u, (struct output*) data, (size_t) offset);
777 case SINK_MESSAGE_UPDATE_LATENCY: {
778 pa_usec_t x, y, latency = (pa_usec_t) offset;
780 x = pa_rtclock_now();
781 y = pa_bytes_to_usec(u->thread_info.counter, &u->sink->sample_spec);
788 pa_smoother_put(u->thread_info.smoother, x, y);
792 case SINK_MESSAGE_UPDATE_MAX_REQUEST:
793 update_max_request(u);
796 case SINK_MESSAGE_UPDATE_REQUESTED_LATENCY:
797 update_fixed_latency(u);
801 return pa_sink_process_msg(o, code, data, offset, chunk);
804 static void update_description(struct userdata *u) {
805 pa_bool_t first = TRUE;
815 if (pa_idxset_isempty(u->outputs)) {
816 pa_sink_set_description(u->sink, "Simultaneous output");
820 t = pa_xstrdup("Simultaneous output to");
822 PA_IDXSET_FOREACH(o, u->outputs, idx) {
826 e = pa_sprintf_malloc("%s %s", t, pa_strnull(pa_proplist_gets(o->sink->proplist, PA_PROP_DEVICE_DESCRIPTION)));
829 e = pa_sprintf_malloc("%s, %s", t, pa_strnull(pa_proplist_gets(o->sink->proplist, PA_PROP_DEVICE_DESCRIPTION)));
835 pa_sink_set_description(u->sink, t);
839 static int output_create_sink_input(struct output *o) {
840 pa_sink_input_new_data data;
847 pa_sink_input_new_data_init(&data);
849 data.driver = __FILE__;
850 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)));
851 pa_proplist_sets(data.proplist, PA_PROP_MEDIA_ROLE, "filter");
852 pa_sink_input_new_data_set_sample_spec(&data, &o->userdata->sink->sample_spec);
853 pa_sink_input_new_data_set_channel_map(&data, &o->userdata->sink->channel_map);
854 data.module = o->userdata->module;
855 data.resample_method = o->userdata->resample_method;
856 data.flags = PA_SINK_INPUT_VARIABLE_RATE|PA_SINK_INPUT_DONT_MOVE|PA_SINK_INPUT_NO_CREATE_ON_SUSPEND;
858 pa_sink_input_new(&o->sink_input, o->userdata->core, &data);
860 pa_sink_input_new_data_done(&data);
865 o->sink_input->parent.process_msg = sink_input_process_msg;
866 o->sink_input->pop = sink_input_pop_cb;
867 o->sink_input->process_rewind = sink_input_process_rewind_cb;
868 o->sink_input->update_max_rewind = sink_input_update_max_rewind_cb;
869 o->sink_input->update_max_request = sink_input_update_max_request_cb;
870 o->sink_input->update_sink_requested_latency = sink_input_update_sink_requested_latency_cb;
871 o->sink_input->attach = sink_input_attach_cb;
872 o->sink_input->detach = sink_input_detach_cb;
873 o->sink_input->kill = sink_input_kill_cb;
874 o->sink_input->userdata = o;
876 pa_sink_input_set_requested_latency(o->sink_input, BLOCK_USEC);
881 /* Called from main context */
882 static struct output *output_new(struct userdata *u, pa_sink *sink) {
889 o = pa_xnew0(struct output, 1);
891 o->inq = pa_asyncmsgq_new(0);
892 o->outq = pa_asyncmsgq_new(0);
894 o->memblockq = pa_memblockq_new(
898 pa_frame_size(&u->sink->sample_spec),
904 pa_assert_se(pa_idxset_put(u->outputs, o, NULL) == 0);
905 update_description(u);
910 /* Called from main context */
911 static void output_free(struct output *o) {
916 pa_assert_se(pa_idxset_remove_by_data(o->userdata->outputs, o, NULL));
917 update_description(o->userdata);
919 if (o->inq_rtpoll_item_read)
920 pa_rtpoll_item_free(o->inq_rtpoll_item_read);
921 if (o->inq_rtpoll_item_write)
922 pa_rtpoll_item_free(o->inq_rtpoll_item_write);
924 if (o->outq_rtpoll_item_read)
925 pa_rtpoll_item_free(o->outq_rtpoll_item_read);
926 if (o->outq_rtpoll_item_write)
927 pa_rtpoll_item_free(o->outq_rtpoll_item_write);
930 pa_asyncmsgq_unref(o->inq);
933 pa_asyncmsgq_unref(o->outq);
936 pa_memblockq_free(o->memblockq);
941 /* Called from main context */
942 static void output_enable(struct output *o) {
948 /* This might cause the sink to be resumed. The state change hook
949 * of the sink might hence be called from here, which might then
950 * cause us to be called in a loop. Make sure that state changes
951 * for this output don't cause this loop by setting a flag here */
952 o->ignore_state_change = TRUE;
954 if (output_create_sink_input(o) >= 0) {
956 if (pa_sink_get_state(o->sink) != PA_SINK_INIT) {
958 /* First we register the output. That means that the sink
959 * will start to pass data to this output. */
960 pa_asyncmsgq_send(o->userdata->sink->asyncmsgq, PA_MSGOBJECT(o->userdata->sink), SINK_MESSAGE_ADD_OUTPUT, o, 0, NULL);
962 /* Then we enable the sink input. That means that the sink
963 * is now asked for new data. */
964 pa_sink_input_put(o->sink_input);
967 /* Hmm the sink is not yet started, do things right here */
968 output_add_within_thread(o);
971 o->ignore_state_change = FALSE;
974 /* Called from main context */
975 static void output_disable(struct output *o) {
981 /* First we disable the sink input. That means that the sink is
982 * not asked for new data anymore */
983 pa_sink_input_unlink(o->sink_input);
985 /* Then we unregister the output. That means that the sink doesn't
986 * pass any further data to this output */
987 pa_asyncmsgq_send(o->userdata->sink->asyncmsgq, PA_MSGOBJECT(o->userdata->sink), SINK_MESSAGE_REMOVE_OUTPUT, o, 0, NULL);
989 /* Now dellocate the stream */
990 pa_sink_input_unref(o->sink_input);
991 o->sink_input = NULL;
993 /* Finally, drop all queued data */
994 pa_memblockq_flush_write(o->memblockq, TRUE);
995 pa_asyncmsgq_flush(o->inq, FALSE);
996 pa_asyncmsgq_flush(o->outq, FALSE);
999 /* Called from main context */
1000 static void output_verify(struct output *o) {
1003 if (PA_SINK_IS_OPENED(pa_sink_get_state(o->userdata->sink)))
1009 /* Called from main context */
1010 static pa_bool_t is_suitable_sink(struct userdata *u, pa_sink *s) {
1013 pa_sink_assert_ref(s);
1018 if (!(s->flags & PA_SINK_HARDWARE))
1021 if (!(s->flags & PA_SINK_LATENCY))
1024 if ((t = pa_proplist_gets(s->proplist, PA_PROP_DEVICE_CLASS)))
1025 if (!pa_streq(t, "sound"))
1031 /* Called from main context */
1032 static pa_hook_result_t sink_put_hook_cb(pa_core *c, pa_sink *s, struct userdata* u) {
1035 pa_core_assert_ref(c);
1036 pa_sink_assert_ref(s);
1040 if (!is_suitable_sink(u, s))
1043 /* Check if the sink is a previously unlinked slave (non-automatic mode) */
1044 pa_strlist *l = u->unlinked_slaves;
1046 while (l && !pa_streq(pa_strlist_data(l), s->name))
1047 l = pa_strlist_next(l);
1052 u->unlinked_slaves = pa_strlist_remove(u->unlinked_slaves, s->name);
1055 pa_log_info("Configuring new sink: %s", s->name);
1056 if (!(o = output_new(u, s))) {
1057 pa_log("Failed to create sink input on sink '%s'.", s->name);
1066 /* Called from main context */
1067 static struct output* find_output(struct userdata *u, pa_sink *s) {
1077 PA_IDXSET_FOREACH(o, u->outputs, idx)
1084 /* Called from main context */
1085 static pa_hook_result_t sink_unlink_hook_cb(pa_core *c, pa_sink *s, struct userdata* u) {
1089 pa_sink_assert_ref(s);
1092 if (!(o = find_output(u, s)))
1095 pa_log_info("Unconfiguring sink: %s", s->name);
1098 u->unlinked_slaves = pa_strlist_prepend(u->unlinked_slaves, s->name);
1105 /* Called from main context */
1106 static pa_hook_result_t sink_state_changed_hook_cb(pa_core *c, pa_sink *s, struct userdata* u) {
1109 if (!(o = find_output(u, s)))
1112 /* This state change might be triggered because we are creating a
1113 * stream here, in that case we don't want to create it a second
1114 * time here and enter a loop */
1115 if (o->ignore_state_change)
1123 int pa__init(pa_module*m) {
1125 pa_modargs *ma = NULL;
1126 const char *slaves, *rm;
1127 int resample_method = PA_RESAMPLER_TRIVIAL;
1132 pa_sink_new_data data;
1133 uint32_t adjust_time_sec;
1137 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
1138 pa_log("failed to parse module arguments");
1142 if ((rm = pa_modargs_get_value(ma, "resample_method", NULL))) {
1143 if ((resample_method = pa_parse_resample_method(rm)) < 0) {
1144 pa_log("invalid resample method '%s'", rm);
1149 m->userdata = u = pa_xnew0(struct userdata, 1);
1152 u->rtpoll = pa_rtpoll_new();
1153 pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
1154 u->resample_method = resample_method;
1155 u->outputs = pa_idxset_new(NULL, NULL);
1156 u->thread_info.smoother = pa_smoother_new(
1165 adjust_time_sec = DEFAULT_ADJUST_TIME_USEC / PA_USEC_PER_SEC;
1166 if (pa_modargs_get_value_u32(ma, "adjust_time", &adjust_time_sec) < 0) {
1167 pa_log("Failed to parse adjust_time value");
1171 if (adjust_time_sec != DEFAULT_ADJUST_TIME_USEC / PA_USEC_PER_SEC)
1172 u->adjust_time = adjust_time_sec * PA_USEC_PER_SEC;
1174 u->adjust_time = DEFAULT_ADJUST_TIME_USEC;
1176 slaves = pa_modargs_get_value(ma, "slaves", NULL);
1177 u->automatic = !slaves;
1179 ss = m->core->default_sample_spec;
1180 map = m->core->default_channel_map;
1182 /* Check the specified slave sinks for sample_spec and channel_map to use for the combined sink */
1183 if (!u->automatic) {
1184 const char*split_state = NULL;
1186 pa_sample_spec slaves_spec;
1187 pa_channel_map slaves_map;
1188 pa_bool_t is_first_slave = TRUE;
1190 pa_sample_spec_init(&slaves_spec);
1192 while ((n = pa_split(slaves, ",", &split_state))) {
1193 pa_sink *slave_sink;
1195 if (!(slave_sink = pa_namereg_get(m->core, n, PA_NAMEREG_SINK))) {
1196 pa_log("Invalid slave sink '%s'", n);
1203 if (is_first_slave) {
1204 slaves_spec = slave_sink->sample_spec;
1205 slaves_map = slave_sink->channel_map;
1206 is_first_slave = FALSE;
1208 if (slaves_spec.format != slave_sink->sample_spec.format)
1209 slaves_spec.format = PA_SAMPLE_INVALID;
1211 if (slaves_spec.rate < slave_sink->sample_spec.rate)
1212 slaves_spec.rate = slave_sink->sample_spec.rate;
1214 if (!pa_channel_map_equal(&slaves_map, &slave_sink->channel_map))
1215 slaves_spec.channels = 0;
1219 if (!is_first_slave) {
1220 if (slaves_spec.format != PA_SAMPLE_INVALID)
1221 ss.format = slaves_spec.format;
1223 ss.rate = slaves_spec.rate;
1225 if (slaves_spec.channels > 0) {
1227 ss.channels = slaves_map.channels;
1232 if ((pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0)) {
1233 pa_log("Invalid sample specification.");
1237 pa_sink_new_data_init(&data);
1238 data.namereg_fail = FALSE;
1239 data.driver = __FILE__;
1241 pa_sink_new_data_set_name(&data, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME));
1242 pa_sink_new_data_set_sample_spec(&data, &ss);
1243 pa_sink_new_data_set_channel_map(&data, &map);
1244 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_CLASS, "filter");
1247 pa_proplist_sets(data.proplist, "combine.slaves", slaves);
1249 if (pa_modargs_get_proplist(ma, "sink_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
1250 pa_log("Invalid properties");
1251 pa_sink_new_data_done(&data);
1255 /* Check proplist for a description & fill in a default value if not */
1256 u->auto_desc = FALSE;
1257 if (NULL == pa_proplist_gets(data.proplist, PA_PROP_DEVICE_DESCRIPTION)) {
1258 u->auto_desc = TRUE;
1259 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Simultaneous Output");
1262 u->sink = pa_sink_new(m->core, &data, PA_SINK_LATENCY);
1263 pa_sink_new_data_done(&data);
1266 pa_log("Failed to create sink");
1270 u->sink->parent.process_msg = sink_process_msg;
1271 u->sink->set_state = sink_set_state;
1272 u->sink->userdata = u;
1274 pa_sink_set_rtpoll(u->sink, u->rtpoll);
1275 pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
1277 u->block_usec = BLOCK_USEC;
1278 pa_sink_set_max_request(u->sink, pa_usec_to_bytes(u->block_usec, &u->sink->sample_spec));
1280 if (!u->automatic) {
1281 const char*split_state;
1285 /* The slaves have been specified manually */
1288 while ((n = pa_split(slaves, ",", &split_state))) {
1289 pa_sink *slave_sink;
1291 if (!(slave_sink = pa_namereg_get(m->core, n, PA_NAMEREG_SINK)) || slave_sink == u->sink) {
1292 pa_log("Invalid slave sink '%s'", n);
1299 if (!output_new(u, slave_sink)) {
1300 pa_log("Failed to create slave sink input on sink '%s'.", slave_sink->name);
1305 if (pa_idxset_size(u->outputs) <= 1)
1306 pa_log_warn("No slave sinks specified.");
1308 u->sink_put_slot = NULL;
1313 /* We're in automatic mode, we add every sink that matches our needs */
1315 PA_IDXSET_FOREACH(s, m->core->sinks, idx) {
1317 if (!is_suitable_sink(u, s))
1320 if (!output_new(u, s)) {
1321 pa_log("Failed to create sink input on sink '%s'.", s->name);
1327 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);
1328 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);
1329 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);
1331 if (!(u->thread = pa_thread_new("combine", thread_func, u))) {
1332 pa_log("Failed to create thread.");
1336 /* Activate the sink and the sink inputs */
1337 pa_sink_put(u->sink);
1339 PA_IDXSET_FOREACH(o, u->outputs, idx)
1342 if (u->adjust_time > 0)
1343 u->time_event = pa_core_rttime_new(m->core, pa_rtclock_now() + u->adjust_time, time_callback, u);
1345 pa_modargs_free(ma);
1352 pa_modargs_free(ma);
1359 void pa__done(pa_module*m) {
1365 if (!(u = m->userdata))
1368 pa_strlist_free(u->unlinked_slaves);
1370 if (u->sink_put_slot)
1371 pa_hook_slot_free(u->sink_put_slot);
1373 if (u->sink_unlink_slot)
1374 pa_hook_slot_free(u->sink_unlink_slot);
1376 if (u->sink_state_changed_slot)
1377 pa_hook_slot_free(u->sink_state_changed_slot);
1380 while ((o = pa_idxset_first(u->outputs, NULL)))
1383 pa_idxset_free(u->outputs, NULL, NULL);
1387 pa_sink_unlink(u->sink);
1390 pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
1391 pa_thread_free(u->thread);
1394 pa_thread_mq_done(&u->thread_mq);
1397 pa_sink_unref(u->sink);
1400 pa_rtpoll_free(u->rtpoll);
1403 u->core->mainloop->time_free(u->time_event);
1405 if (u->thread_info.smoother)
1406 pa_smoother_free(u->thread_info.smoother);