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>
51 #include "module-combine-symdef.h"
53 PA_MODULE_AUTHOR("Lennart Poettering");
54 PA_MODULE_DESCRIPTION("Combine multiple sinks to one");
55 PA_MODULE_VERSION(PACKAGE_VERSION);
56 PA_MODULE_LOAD_ONCE(FALSE);
58 "sink_name=<name for the sink> "
59 "sink_properties=<properties for the sink> "
60 "slaves=<slave sinks> "
61 "adjust_time=<seconds> "
62 "resample_method=<method> "
63 "format=<sample format> "
65 "channels=<number of channels> "
66 "channel_map=<channel map>");
68 #define DEFAULT_SINK_NAME "combined"
70 #define MEMBLOCKQ_MAXLENGTH (1024*1024*16)
72 #define DEFAULT_ADJUST_TIME 10
74 #define BLOCK_USEC (PA_USEC_PER_MSEC * 200)
76 static const char* const valid_modargs[] = {
90 struct userdata *userdata;
93 pa_sink_input *sink_input;
95 pa_asyncmsgq *inq, /* Message queue from the sink thread to this sink input */
96 *outq; /* Message queue from this sink input to the sink thread */
97 pa_rtpoll_item *inq_rtpoll_item_read, *inq_rtpoll_item_write;
98 pa_rtpoll_item *outq_rtpoll_item_read, *outq_rtpoll_item_write;
100 pa_memblockq *memblockq;
102 pa_usec_t total_latency;
104 pa_atomic_t max_request;
106 PA_LLIST_FIELDS(struct output);
115 pa_thread_mq thread_mq;
118 pa_time_event *time_event;
119 uint32_t adjust_time;
124 pa_hook_slot *sink_put_slot, *sink_unlink_slot, *sink_state_changed_slot;
126 pa_resample_method_t resample_method;
128 pa_usec_t block_usec;
130 pa_idxset* outputs; /* managed in main context */
133 PA_LLIST_HEAD(struct output, active_outputs); /* managed in IO thread context */
134 pa_atomic_t running; /* we cache that value here, so that every thread can query it cheaply */
136 pa_bool_t in_null_mode;
137 pa_smoother *smoother;
143 SINK_MESSAGE_ADD_OUTPUT = PA_SINK_MESSAGE_MAX,
144 SINK_MESSAGE_REMOVE_OUTPUT,
146 SINK_MESSAGE_UPDATE_LATENCY,
147 SINK_MESSAGE_UPDATE_MAX_REQUEST
151 SINK_INPUT_MESSAGE_POST = PA_SINK_INPUT_MESSAGE_MAX,
154 static void output_free(struct output *o);
155 static int output_create_sink_input(struct output *o);
157 static void adjust_rates(struct userdata *u) {
159 pa_usec_t max_sink_latency = 0, min_total_latency = (pa_usec_t) -1, target_latency, avg_total_latency = 0;
165 pa_sink_assert_ref(u->sink);
167 if (pa_idxset_size(u->outputs) <= 0)
170 if (!PA_SINK_IS_OPENED(pa_sink_get_state(u->sink)))
173 for (o = pa_idxset_first(u->outputs, &idx); o; o = pa_idxset_next(u->outputs, &idx)) {
174 pa_usec_t sink_latency;
176 if (!o->sink_input || !PA_SINK_IS_OPENED(pa_sink_get_state(o->sink)))
179 o->total_latency = pa_sink_input_get_latency(o->sink_input, &sink_latency);
180 o->total_latency += sink_latency;
182 if (sink_latency > max_sink_latency)
183 max_sink_latency = sink_latency;
185 if (min_total_latency == (pa_usec_t) -1 || o->total_latency < min_total_latency)
186 min_total_latency = o->total_latency;
188 avg_total_latency += o->total_latency;
192 if (min_total_latency == (pa_usec_t) -1)
195 avg_total_latency /= n;
197 target_latency = max_sink_latency > min_total_latency ? max_sink_latency : min_total_latency;
199 pa_log_info("[%s] avg total latency is %0.2f msec.", u->sink->name, (double) avg_total_latency / PA_USEC_PER_MSEC);
200 pa_log_info("[%s] target latency is %0.2f msec.", u->sink->name, (double) target_latency / PA_USEC_PER_MSEC);
202 base_rate = u->sink->sample_spec.rate;
204 for (o = pa_idxset_first(u->outputs, &idx); o; o = pa_idxset_next(u->outputs, &idx)) {
205 uint32_t r = base_rate;
207 if (!o->sink_input || !PA_SINK_IS_OPENED(pa_sink_get_state(o->sink)))
210 if (o->total_latency < target_latency)
211 r -= (uint32_t) ((((double) (target_latency - o->total_latency))/(double)u->adjust_time)*(double)r/PA_USEC_PER_SEC);
212 else if (o->total_latency > target_latency)
213 r += (uint32_t) ((((double) (o->total_latency - target_latency))/(double)u->adjust_time)*(double)r/PA_USEC_PER_SEC);
215 if (r < (uint32_t) (base_rate*0.9) || r > (uint32_t) (base_rate*1.1)) {
216 pa_log_warn("[%s] sample rates too different, not adjusting (%u vs. %u).", pa_proplist_gets(o->sink_input->proplist, PA_PROP_MEDIA_NAME), base_rate, r);
217 pa_sink_input_set_rate(o->sink_input, base_rate);
219 pa_log_info("[%s] new rate is %u Hz; ratio is %0.3f; latency is %0.0f usec.", pa_proplist_gets(o->sink_input->proplist, PA_PROP_MEDIA_NAME), r, (double) r / base_rate, (float) o->total_latency);
220 pa_sink_input_set_rate(o->sink_input, r);
224 pa_asyncmsgq_send(u->sink->asyncmsgq, PA_MSGOBJECT(u->sink), SINK_MESSAGE_UPDATE_LATENCY, NULL, (int64_t) avg_total_latency, NULL);
227 static void time_callback(pa_mainloop_api *a, pa_time_event *e, const struct timeval *t, void *userdata) {
228 struct userdata *u = userdata;
232 pa_assert(u->time_event == e);
236 pa_core_rttime_restart(u->core, e, pa_rtclock_now() + u->adjust_time * PA_USEC_PER_SEC);
239 static void process_render_null(struct userdata *u, pa_usec_t now) {
243 if (u->thread_info.in_null_mode)
244 u->thread_info.timestamp = now;
246 while (u->thread_info.timestamp < now + u->block_usec) {
249 pa_sink_render(u->sink, u->sink->thread_info.max_request, &chunk);
250 pa_memblock_unref(chunk.memblock);
252 u->thread_info.counter += chunk.length;
254 /* pa_log_debug("Ate %lu bytes.", (unsigned long) chunk.length); */
255 u->thread_info.timestamp += pa_bytes_to_usec(chunk.length, &u->sink->sample_spec);
259 if (ate >= u->sink->thread_info.max_request)
263 /* pa_log_debug("Ate in sum %lu bytes (of %lu)", (unsigned long) ate, (unsigned long) nbytes); */
265 pa_smoother_put(u->thread_info.smoother, now,
266 pa_bytes_to_usec(u->thread_info.counter, &u->sink->sample_spec) - (u->thread_info.timestamp - now));
269 static void thread_func(void *userdata) {
270 struct userdata *u = userdata;
274 pa_log_debug("Thread starting up");
276 if (u->core->realtime_scheduling)
277 pa_make_realtime(u->core->realtime_priority+1);
279 pa_thread_mq_install(&u->thread_mq);
281 u->thread_info.timestamp = pa_rtclock_now();
282 u->thread_info.in_null_mode = FALSE;
287 if (PA_SINK_IS_OPENED(u->sink->thread_info.state))
288 if (u->sink->thread_info.rewind_requested)
289 pa_sink_process_rewind(u->sink, 0);
291 /* If no outputs are connected, render some data and drop it immediately. */
292 if (PA_SINK_IS_OPENED(u->sink->thread_info.state) && !u->thread_info.active_outputs) {
295 now = pa_rtclock_now();
297 if (!u->thread_info.in_null_mode || u->thread_info.timestamp <= now)
298 process_render_null(u, now);
300 pa_rtpoll_set_timer_absolute(u->rtpoll, u->thread_info.timestamp);
301 u->thread_info.in_null_mode = TRUE;
303 pa_rtpoll_set_timer_disabled(u->rtpoll);
304 u->thread_info.in_null_mode = FALSE;
307 /* Hmm, nothing to do. Let's sleep */
308 if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0) {
309 pa_log_info("pa_rtpoll_run() = %i", ret);
318 /* If this was no regular exit from the loop we have to continue
319 * processing messages until we received PA_MESSAGE_SHUTDOWN */
320 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
321 pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
324 pa_log_debug("Thread shutting down");
327 /* Called from I/O thread context */
328 static void render_memblock(struct userdata *u, struct output *o, size_t length) {
332 /* We are run by the sink thread, on behalf of an output (o). The
333 * output is waiting for us, hence it is safe to access its
334 * mainblockq and asyncmsgq directly. */
336 /* If we are not running, we cannot produce any data */
337 if (!pa_atomic_load(&u->thread_info.running))
340 /* Maybe there's some data in the requesting output's queue
342 while (pa_asyncmsgq_process_one(o->inq) > 0)
345 /* Ok, now let's prepare some data if we really have to */
346 while (!pa_memblockq_is_readable(o->memblockq)) {
351 pa_sink_render(u->sink, length, &chunk);
353 u->thread_info.counter += chunk.length;
355 /* OK, let's send this data to the other threads */
356 for (j = u->thread_info.active_outputs; j; j = j->next)
358 /* Send to other outputs, which are not the requesting
362 pa_asyncmsgq_post(j->inq, PA_MSGOBJECT(j->sink_input), SINK_INPUT_MESSAGE_POST, NULL, 0, &chunk, NULL);
364 /* And place it directly into the requesting output's queue */
366 pa_memblockq_push_align(o->memblockq, &chunk);
368 pa_memblock_unref(chunk.memblock);
372 /* Called from I/O thread context */
373 static void request_memblock(struct output *o, size_t length) {
375 pa_sink_input_assert_ref(o->sink_input);
376 pa_sink_assert_ref(o->userdata->sink);
378 /* If another thread already prepared some data we received
379 * the data over the asyncmsgq, hence let's first process
381 while (pa_asyncmsgq_process_one(o->inq) > 0)
384 /* Check whether we're now readable */
385 if (pa_memblockq_is_readable(o->memblockq))
388 /* OK, we need to prepare new data, but only if the sink is actually running */
389 if (pa_atomic_load(&o->userdata->thread_info.running))
390 pa_asyncmsgq_send(o->outq, PA_MSGOBJECT(o->userdata->sink), SINK_MESSAGE_NEED, o, (int64_t) length, NULL);
393 /* Called from I/O thread context */
394 static int sink_input_pop_cb(pa_sink_input *i, size_t nbytes, pa_memchunk *chunk) {
397 pa_sink_input_assert_ref(i);
398 pa_assert_se(o = i->userdata);
400 /* If necessary, get some new data */
401 request_memblock(o, nbytes);
403 if (pa_memblockq_peek(o->memblockq, chunk) < 0)
406 pa_memblockq_drop(o->memblockq, chunk->length);
410 /* Called from I/O thread context */
411 static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes) {
414 pa_sink_input_assert_ref(i);
415 pa_assert_se(o = i->userdata);
417 pa_memblockq_rewind(o->memblockq, nbytes);
420 /* Called from I/O thread context */
421 static void sink_input_update_max_rewind_cb(pa_sink_input *i, size_t nbytes) {
424 pa_sink_input_assert_ref(i);
425 pa_assert_se(o = i->userdata);
427 pa_memblockq_set_maxrewind(o->memblockq, nbytes);
430 /* Called from I/O thread context */
431 static void sink_input_update_max_request_cb(pa_sink_input *i, size_t nbytes) {
434 pa_sink_input_assert_ref(i);
435 pa_assert_se(o = i->userdata);
437 if (pa_atomic_load(&o->max_request) == (int) nbytes)
440 pa_atomic_store(&o->max_request, (int) nbytes);
442 pa_asyncmsgq_post(o->outq, PA_MSGOBJECT(o->userdata->sink), SINK_MESSAGE_UPDATE_MAX_REQUEST, NULL, 0, NULL, NULL);
445 /* Called from I/O thread context */
446 static void sink_input_attach_cb(pa_sink_input *i) {
449 pa_sink_input_assert_ref(i);
450 pa_assert_se(o = i->userdata);
452 /* Set up the queue from the sink thread to us */
453 pa_assert(!o->inq_rtpoll_item_read && !o->outq_rtpoll_item_write);
455 o->inq_rtpoll_item_read = pa_rtpoll_item_new_asyncmsgq_read(
456 i->sink->thread_info.rtpoll,
457 PA_RTPOLL_LATE, /* This one is not that important, since we check for data in _peek() anyway. */
460 o->outq_rtpoll_item_write = pa_rtpoll_item_new_asyncmsgq_write(
461 i->sink->thread_info.rtpoll,
466 /* Called from I/O thread context */
467 static void sink_input_detach_cb(pa_sink_input *i) {
470 pa_sink_input_assert_ref(i);
471 pa_assert_se(o = i->userdata);
473 /* Shut down the queue from the sink thread to us */
474 pa_assert(o->inq_rtpoll_item_read && o->outq_rtpoll_item_write);
476 pa_rtpoll_item_free(o->inq_rtpoll_item_read);
477 o->inq_rtpoll_item_read = NULL;
479 pa_rtpoll_item_free(o->outq_rtpoll_item_write);
480 o->outq_rtpoll_item_write = NULL;
483 /* Called from main context */
484 static void sink_input_kill_cb(pa_sink_input *i) {
487 pa_sink_input_assert_ref(i);
488 pa_assert_se(o = i->userdata);
490 pa_module_unload_request(o->userdata->module, TRUE);
494 /* Called from IO thread context */
495 static void sink_input_state_change_cb(pa_sink_input *i, pa_sink_input_state_t state) {
498 pa_sink_input_assert_ref(i);
499 pa_assert_se(u = i->userdata);
501 /* If we are added for the first time, ask for a rewinding so that
502 * we are heard right-away. */
503 if (PA_SINK_INPUT_IS_LINKED(state) &&
504 i->thread_info.state == PA_SINK_INPUT_INIT)
505 pa_sink_input_request_rewind(i, 0, FALSE, TRUE, TRUE);
508 /* Called from thread context */
509 static int sink_input_process_msg(pa_msgobject *obj, int code, void *data, int64_t offset, pa_memchunk *chunk) {
510 struct output *o = PA_SINK_INPUT(obj)->userdata;
514 case PA_SINK_INPUT_MESSAGE_GET_LATENCY: {
517 *r = pa_bytes_to_usec(pa_memblockq_get_length(o->memblockq), &o->sink_input->sample_spec);
519 /* Fall through, the default handler will add in the extra
520 * latency added by the resampler */
524 case SINK_INPUT_MESSAGE_POST:
526 if (PA_SINK_IS_OPENED(o->sink_input->sink->thread_info.state))
527 pa_memblockq_push_align(o->memblockq, chunk);
529 pa_memblockq_flush_write(o->memblockq);
534 return pa_sink_input_process_msg(obj, code, data, offset, chunk);
537 /* Called from main context */
538 static void disable_output(struct output *o) {
544 pa_sink_input_unlink(o->sink_input);
545 pa_asyncmsgq_send(o->userdata->sink->asyncmsgq, PA_MSGOBJECT(o->userdata->sink), SINK_MESSAGE_REMOVE_OUTPUT, o, 0, NULL);
546 pa_sink_input_unref(o->sink_input);
547 o->sink_input = NULL;
550 /* Called from main context */
551 static void enable_output(struct output *o) {
557 if (output_create_sink_input(o) >= 0) {
559 pa_memblockq_flush_write(o->memblockq);
561 pa_sink_input_put(o->sink_input);
563 if (o->userdata->sink && PA_SINK_IS_LINKED(pa_sink_get_state(o->userdata->sink)))
564 pa_asyncmsgq_send(o->userdata->sink->asyncmsgq, PA_MSGOBJECT(o->userdata->sink), SINK_MESSAGE_ADD_OUTPUT, o, 0, NULL);
568 /* Called from main context */
569 static void suspend(struct userdata *u) {
575 /* Let's suspend by unlinking all streams */
576 for (o = pa_idxset_first(u->outputs, &idx); o; o = pa_idxset_next(u->outputs, &idx))
579 pa_log_info("Device suspended...");
582 /* Called from main context */
583 static void unsuspend(struct userdata *u) {
590 for (o = pa_idxset_first(u->outputs, &idx); o; o = pa_idxset_next(u->outputs, &idx)) {
592 pa_sink_suspend(o->sink, FALSE, PA_SUSPEND_IDLE);
594 if (PA_SINK_IS_OPENED(pa_sink_get_state(o->sink)))
598 pa_log_info("Resumed successfully...");
601 /* Called from main context */
602 static int sink_set_state(pa_sink *sink, pa_sink_state_t state) {
605 pa_sink_assert_ref(sink);
606 pa_assert_se(u = sink->userdata);
608 /* Please note that in contrast to the ALSA modules we call
609 * suspend/unsuspend from main context here! */
612 case PA_SINK_SUSPENDED:
613 pa_assert(PA_SINK_IS_OPENED(pa_sink_get_state(u->sink)));
619 case PA_SINK_RUNNING:
621 if (pa_sink_get_state(u->sink) == PA_SINK_SUSPENDED)
626 case PA_SINK_UNLINKED:
628 case PA_SINK_INVALID_STATE:
635 /* Called from IO context */
636 static void update_max_request(struct userdata *u) {
637 size_t max_request = 0;
640 for (o = u->thread_info.active_outputs; o; o = o->next) {
641 size_t mr = (size_t) pa_atomic_load(&o->max_request);
643 if (mr > max_request)
647 if (max_request <= 0)
648 max_request = pa_usec_to_bytes(u->block_usec, &u->sink->sample_spec);
650 pa_sink_set_max_request_within_thread(u->sink, max_request);
653 /* Called from thread context of the io thread */
654 static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
655 struct userdata *u = PA_SINK(o)->userdata;
659 case PA_SINK_MESSAGE_SET_STATE:
660 pa_atomic_store(&u->thread_info.running, PA_PTR_TO_UINT(data) == PA_SINK_RUNNING);
662 if (PA_PTR_TO_UINT(data) == PA_SINK_SUSPENDED)
663 pa_smoother_pause(u->thread_info.smoother, pa_rtclock_now());
665 pa_smoother_resume(u->thread_info.smoother, pa_rtclock_now(), TRUE);
669 case PA_SINK_MESSAGE_GET_LATENCY: {
670 pa_usec_t x, y, c, *delay = data;
672 x = pa_rtclock_now();
673 y = pa_smoother_get(u->thread_info.smoother, x);
675 c = pa_bytes_to_usec(u->thread_info.counter, &u->sink->sample_spec);
685 case SINK_MESSAGE_ADD_OUTPUT: {
686 struct output *op = data;
688 PA_LLIST_PREPEND(struct output, u->thread_info.active_outputs, op);
690 pa_assert(!op->outq_rtpoll_item_read && !op->inq_rtpoll_item_write);
692 op->outq_rtpoll_item_read = pa_rtpoll_item_new_asyncmsgq_read(
694 PA_RTPOLL_EARLY-1, /* This item is very important */
696 op->inq_rtpoll_item_write = pa_rtpoll_item_new_asyncmsgq_write(
701 update_max_request(u);
705 case SINK_MESSAGE_REMOVE_OUTPUT: {
706 struct output *op = data;
708 PA_LLIST_REMOVE(struct output, u->thread_info.active_outputs, op);
710 pa_assert(op->outq_rtpoll_item_read && op->inq_rtpoll_item_write);
712 pa_rtpoll_item_free(op->outq_rtpoll_item_read);
713 op->outq_rtpoll_item_read = NULL;
715 pa_rtpoll_item_free(op->inq_rtpoll_item_write);
716 op->inq_rtpoll_item_write = NULL;
718 update_max_request(u);
722 case SINK_MESSAGE_NEED:
723 render_memblock(u, (struct output*) data, (size_t) offset);
726 case SINK_MESSAGE_UPDATE_LATENCY: {
727 pa_usec_t x, y, latency = (pa_usec_t) offset;
729 x = pa_rtclock_now();
730 y = pa_bytes_to_usec(u->thread_info.counter, &u->sink->sample_spec);
737 pa_smoother_put(u->thread_info.smoother, x, y);
741 case SINK_MESSAGE_UPDATE_MAX_REQUEST:
743 update_max_request(u);
747 return pa_sink_process_msg(o, code, data, offset, chunk);
750 static void update_description(struct userdata *u) {
751 pa_bool_t first = TRUE;
761 if (pa_idxset_isempty(u->outputs)) {
762 pa_sink_set_description(u->sink, "Simultaneous output");
766 t = pa_xstrdup("Simultaneous output to");
768 for (o = pa_idxset_first(u->outputs, &idx); o; o = pa_idxset_next(u->outputs, &idx)) {
772 e = pa_sprintf_malloc("%s %s", t, pa_strnull(pa_proplist_gets(o->sink->proplist, PA_PROP_DEVICE_DESCRIPTION)));
775 e = pa_sprintf_malloc("%s, %s", t, pa_strnull(pa_proplist_gets(o->sink->proplist, PA_PROP_DEVICE_DESCRIPTION)));
781 pa_sink_set_description(u->sink, t);
785 static int output_create_sink_input(struct output *o) {
786 pa_sink_input_new_data data;
793 pa_sink_input_new_data_init(&data);
795 data.driver = __FILE__;
796 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)));
797 pa_proplist_sets(data.proplist, PA_PROP_MEDIA_ROLE, "filter");
798 pa_sink_input_new_data_set_sample_spec(&data, &o->userdata->sink->sample_spec);
799 pa_sink_input_new_data_set_channel_map(&data, &o->userdata->sink->channel_map);
800 data.module = o->userdata->module;
801 data.resample_method = o->userdata->resample_method;
803 pa_sink_input_new(&o->sink_input, o->userdata->core, &data, PA_SINK_INPUT_VARIABLE_RATE|PA_SINK_INPUT_DONT_MOVE);
805 pa_sink_input_new_data_done(&data);
810 o->sink_input->parent.process_msg = sink_input_process_msg;
811 o->sink_input->pop = sink_input_pop_cb;
812 o->sink_input->process_rewind = sink_input_process_rewind_cb;
813 o->sink_input->state_change = sink_input_state_change_cb;
814 o->sink_input->update_max_rewind = sink_input_update_max_rewind_cb;
815 o->sink_input->update_max_request = sink_input_update_max_request_cb;
816 o->sink_input->attach = sink_input_attach_cb;
817 o->sink_input->detach = sink_input_detach_cb;
818 o->sink_input->kill = sink_input_kill_cb;
819 o->sink_input->userdata = o;
821 pa_sink_input_set_requested_latency(o->sink_input, BLOCK_USEC);
826 static struct output *output_new(struct userdata *u, pa_sink *sink) {
828 pa_sink_state_t state;
834 o = pa_xnew0(struct output, 1);
836 o->inq = pa_asyncmsgq_new(0);
837 o->outq = pa_asyncmsgq_new(0);
839 o->memblockq = pa_memblockq_new(
843 pa_frame_size(&u->sink->sample_spec),
848 pa_atomic_store(&o->max_request, 0);
849 PA_LLIST_INIT(struct output, o);
851 pa_assert_se(pa_idxset_put(u->outputs, o, NULL) == 0);
853 state = pa_sink_get_state(u->sink);
855 if (state != PA_SINK_INIT)
856 pa_asyncmsgq_send(u->sink->asyncmsgq, PA_MSGOBJECT(u->sink), SINK_MESSAGE_ADD_OUTPUT, o, 0, NULL);
858 /* If the sink is not yet started, we need to do the activation ourselves */
859 PA_LLIST_PREPEND(struct output, u->thread_info.active_outputs, o);
861 o->outq_rtpoll_item_read = pa_rtpoll_item_new_asyncmsgq_read(
863 PA_RTPOLL_EARLY-1, /* This item is very important */
865 o->inq_rtpoll_item_write = pa_rtpoll_item_new_asyncmsgq_write(
871 if (PA_SINK_IS_OPENED(state) || state == PA_SINK_INIT) {
872 pa_sink_suspend(sink, FALSE, PA_SUSPEND_IDLE);
874 if (PA_SINK_IS_OPENED(pa_sink_get_state(sink)))
875 if (output_create_sink_input(o) < 0)
879 update_description(u);
886 pa_idxset_remove_by_data(u->outputs, o, NULL);
889 pa_sink_input_unlink(o->sink_input);
890 pa_sink_input_unref(o->sink_input);
894 pa_memblockq_free(o->memblockq);
897 pa_asyncmsgq_unref(o->inq);
900 pa_asyncmsgq_unref(o->outq);
908 static pa_bool_t is_suitable_sink(struct userdata *u, pa_sink *s) {
911 pa_sink_assert_ref(s);
913 if (!(s->flags & PA_SINK_HARDWARE))
919 if ((t = pa_proplist_gets(s->proplist, PA_PROP_DEVICE_CLASS)))
920 if (strcmp(t, "sound"))
926 static pa_hook_result_t sink_put_hook_cb(pa_core *c, pa_sink *s, struct userdata* u) {
929 pa_core_assert_ref(c);
930 pa_sink_assert_ref(s);
932 pa_assert(u->automatic);
934 if (!is_suitable_sink(u, s))
937 pa_log_info("Configuring new sink: %s", s->name);
939 if (!(o = output_new(u, s))) {
940 pa_log("Failed to create sink input on sink '%s'.", s->name);
945 pa_sink_input_put(o->sink_input);
950 static struct output* find_output(struct userdata *u, pa_sink *s) {
960 for (o = pa_idxset_first(u->outputs, &idx); o; o = pa_idxset_next(u->outputs, &idx))
967 static pa_hook_result_t sink_unlink_hook_cb(pa_core *c, pa_sink *s, struct userdata* u) {
971 pa_sink_assert_ref(s);
974 if (!(o = find_output(u, s)))
977 pa_log_info("Unconfiguring sink: %s", s->name);
984 static pa_hook_result_t sink_state_changed_hook_cb(pa_core *c, pa_sink *s, struct userdata* u) {
986 pa_sink_state_t state;
988 if (!(o = find_output(u, s)))
991 state = pa_sink_get_state(s);
993 if (PA_SINK_IS_OPENED(state) && PA_SINK_IS_OPENED(pa_sink_get_state(u->sink)) && !o->sink_input)
996 if (state == PA_SINK_SUSPENDED && o->sink_input)
1002 int pa__init(pa_module*m) {
1004 pa_modargs *ma = NULL;
1005 const char *slaves, *rm;
1006 int resample_method = PA_RESAMPLER_TRIVIAL;
1011 pa_sink_new_data data;
1015 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
1016 pa_log("failed to parse module arguments");
1020 if ((rm = pa_modargs_get_value(ma, "resample_method", NULL))) {
1021 if ((resample_method = pa_parse_resample_method(rm)) < 0) {
1022 pa_log("invalid resample method '%s'", rm);
1027 m->userdata = u = pa_xnew0(struct userdata, 1);
1030 u->adjust_time = DEFAULT_ADJUST_TIME;
1031 u->rtpoll = pa_rtpoll_new();
1032 pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
1033 u->resample_method = resample_method;
1034 u->outputs = pa_idxset_new(NULL, NULL);
1035 u->sink_put_slot = u->sink_unlink_slot = u->sink_state_changed_slot = NULL;
1036 PA_LLIST_HEAD_INIT(struct output, u->thread_info.active_outputs);
1037 pa_atomic_store(&u->thread_info.running, FALSE);
1038 u->thread_info.in_null_mode = FALSE;
1039 u->thread_info.counter = 0;
1040 u->thread_info.smoother = pa_smoother_new(
1049 if (pa_modargs_get_value_u32(ma, "adjust_time", &u->adjust_time) < 0) {
1050 pa_log("Failed to parse adjust_time value");
1054 slaves = pa_modargs_get_value(ma, "slaves", NULL);
1055 u->automatic = !slaves;
1057 ss = m->core->default_sample_spec;
1058 map = m->core->default_channel_map;
1059 if ((pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0)) {
1060 pa_log("Invalid sample specification.");
1064 pa_sink_new_data_init(&data);
1065 data.namereg_fail = FALSE;
1066 data.driver = __FILE__;
1068 pa_sink_new_data_set_name(&data, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME));
1069 pa_sink_new_data_set_sample_spec(&data, &ss);
1070 pa_sink_new_data_set_channel_map(&data, &map);
1071 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_CLASS, "filter");
1074 pa_proplist_sets(data.proplist, "combine.slaves", slaves);
1076 if (pa_modargs_get_proplist(ma, "sink_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
1077 pa_log("Invalid properties");
1078 pa_sink_new_data_done(&data);
1082 /* Check proplist for a description & fill in a default value if not */
1083 u->auto_desc = FALSE;
1084 if (NULL == pa_proplist_gets(data.proplist, PA_PROP_DEVICE_DESCRIPTION)) {
1085 u->auto_desc = TRUE;
1086 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Simultaneous Output");
1089 u->sink = pa_sink_new(m->core, &data, PA_SINK_LATENCY);
1090 pa_sink_new_data_done(&data);
1093 pa_log("Failed to create sink");
1097 u->sink->parent.process_msg = sink_process_msg;
1098 u->sink->set_state = sink_set_state;
1099 u->sink->userdata = u;
1101 pa_sink_set_rtpoll(u->sink, u->rtpoll);
1102 pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
1104 u->block_usec = BLOCK_USEC;
1105 pa_sink_set_max_request(u->sink, pa_usec_to_bytes(u->block_usec, &u->sink->sample_spec));
1107 if (!u->automatic) {
1108 const char*split_state;
1112 /* The slaves have been specified manually */
1115 while ((n = pa_split(slaves, ",", &split_state))) {
1116 pa_sink *slave_sink;
1118 if (!(slave_sink = pa_namereg_get(m->core, n, PA_NAMEREG_SINK)) || slave_sink == u->sink) {
1119 pa_log("Invalid slave sink '%s'", n);
1126 if (!output_new(u, slave_sink)) {
1127 pa_log("Failed to create slave sink input on sink '%s'.", slave_sink->name);
1132 if (pa_idxset_size(u->outputs) <= 1)
1133 pa_log_warn("No slave sinks specified.");
1135 u->sink_put_slot = NULL;
1140 /* We're in automatic mode, we add every sink that matches our needs */
1142 for (s = pa_idxset_first(m->core->sinks, &idx); s; s = pa_idxset_next(m->core->sinks, &idx)) {
1144 if (!is_suitable_sink(u, s))
1147 if (!output_new(u, s)) {
1148 pa_log("Failed to create sink input on sink '%s'.", s->name);
1153 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);
1156 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);
1157 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);
1159 if (!(u->thread = pa_thread_new(thread_func, u))) {
1160 pa_log("Failed to create thread.");
1164 /* Activate the sink and the sink inputs */
1165 pa_sink_put(u->sink);
1167 for (o = pa_idxset_first(u->outputs, &idx); o; o = pa_idxset_next(u->outputs, &idx))
1169 pa_sink_input_put(o->sink_input);
1171 if (u->adjust_time > 0)
1172 u->time_event = pa_core_rttime_new(m->core, pa_rtclock_now() + u->adjust_time * PA_USEC_PER_SEC, time_callback, u);
1174 pa_modargs_free(ma);
1181 pa_modargs_free(ma);
1188 static void output_free(struct output *o) {
1193 pa_assert_se(pa_idxset_remove_by_data(o->userdata->outputs, o, NULL));
1195 update_description(o->userdata);
1197 if (o->inq_rtpoll_item_read)
1198 pa_rtpoll_item_free(o->inq_rtpoll_item_read);
1199 if (o->inq_rtpoll_item_write)
1200 pa_rtpoll_item_free(o->inq_rtpoll_item_write);
1202 if (o->outq_rtpoll_item_read)
1203 pa_rtpoll_item_free(o->outq_rtpoll_item_read);
1204 if (o->outq_rtpoll_item_write)
1205 pa_rtpoll_item_free(o->outq_rtpoll_item_write);
1208 pa_asyncmsgq_unref(o->inq);
1211 pa_asyncmsgq_unref(o->outq);
1214 pa_memblockq_free(o->memblockq);
1219 void pa__done(pa_module*m) {
1225 if (!(u = m->userdata))
1228 if (u->sink_put_slot)
1229 pa_hook_slot_free(u->sink_put_slot);
1231 if (u->sink_unlink_slot)
1232 pa_hook_slot_free(u->sink_unlink_slot);
1234 if (u->sink_state_changed_slot)
1235 pa_hook_slot_free(u->sink_state_changed_slot);
1238 while ((o = pa_idxset_first(u->outputs, NULL)))
1241 pa_idxset_free(u->outputs, NULL, NULL);
1245 pa_sink_unlink(u->sink);
1248 pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
1249 pa_thread_free(u->thread);
1252 pa_thread_mq_done(&u->thread_mq);
1255 pa_sink_unref(u->sink);
1258 pa_rtpoll_free(u->rtpoll);
1261 u->core->mainloop->time_free(u->time_event);
1263 if (u->thread_info.smoother)
1264 pa_smoother_free(u->thread_info.smoother);