4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
31 #include <pulse/timeval.h>
32 #include <pulse/xmalloc.h>
34 #include <pulsecore/macro.h>
35 #include <pulsecore/module.h>
36 #include <pulsecore/llist.h>
37 #include <pulsecore/sink.h>
38 #include <pulsecore/sink-input.h>
39 #include <pulsecore/memblockq.h>
40 #include <pulsecore/log.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/rtclock.h>
49 #include <pulsecore/core-error.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 "master=<master sink> "
60 "slaves=<slave sinks> "
61 "adjust_time=<seconds> "
62 "resample_method=<method> "
63 "format=<sample format> "
64 "channels=<number of channels> "
66 "channel_map=<channel map>");
68 #define DEFAULT_SINK_NAME "combined"
69 #define MEMBLOCKQ_MAXLENGTH (1024*1024*16)
71 #define DEFAULT_ADJUST_TIME 10
73 static const char* const valid_modargs
[] = {
87 struct userdata
*userdata
;
90 pa_sink_input
*sink_input
;
92 pa_asyncmsgq
*inq
, /* Message queue from the sink thread to this sink input */
93 *outq
; /* Message queue from this sink input to the sink thread */
94 pa_rtpoll_item
*inq_rtpoll_item
, *outq_rtpoll_item
;
96 pa_memblockq
*memblockq
;
98 pa_usec_t total_latency
;
100 PA_LLIST_FIELDS(struct output
);
109 pa_thread_mq thread_mq
;
112 pa_time_event
*time_event
;
113 uint32_t adjust_time
;
118 pa_hook_slot
*sink_new_slot
, *sink_unlink_slot
, *sink_state_changed_slot
;
120 pa_resample_method_t resample_method
;
122 struct timeval adjust_timestamp
;
124 struct output
*master
;
125 pa_idxset
* outputs
; /* managed in main context */
128 PA_LLIST_HEAD(struct output
, active_outputs
); /* managed in IO thread context */
129 pa_atomic_t running
; /* we cache that value here, so that every thread can query it cheaply */
130 struct timeval timestamp
;
131 pa_bool_t in_null_mode
;
136 SINK_MESSAGE_ADD_OUTPUT
= PA_SINK_MESSAGE_MAX
,
137 SINK_MESSAGE_REMOVE_OUTPUT
,
142 SINK_INPUT_MESSAGE_POST
= PA_SINK_INPUT_MESSAGE_MAX
,
145 static void output_free(struct output
*o
);
146 static int output_create_sink_input(struct output
*o
);
147 static void update_master(struct userdata
*u
, struct output
*o
);
148 static void pick_master(struct userdata
*u
, struct output
*except
);
150 static void adjust_rates(struct userdata
*u
) {
152 pa_usec_t max_sink_latency
= 0, min_total_latency
= (pa_usec_t
) -1, target_latency
;
157 pa_sink_assert_ref(u
->sink
);
159 if (pa_idxset_size(u
->outputs
) <= 0)
165 if (!PA_SINK_OPENED(pa_sink_get_state(u
->sink
)))
168 for (o
= pa_idxset_first(u
->outputs
, &idx
); o
; o
= pa_idxset_next(u
->outputs
, &idx
)) {
169 pa_usec_t sink_latency
;
171 if (!o
->sink_input
|| !PA_SINK_OPENED(pa_sink_get_state(o
->sink
)))
174 sink_latency
= pa_sink_get_latency(o
->sink
);
175 o
->total_latency
= sink_latency
+ pa_sink_input_get_latency(o
->sink_input
);
177 if (sink_latency
> max_sink_latency
)
178 max_sink_latency
= sink_latency
;
180 if (min_total_latency
== (pa_usec_t
) -1 || o
->total_latency
< min_total_latency
)
181 min_total_latency
= o
->total_latency
;
184 if (min_total_latency
== (pa_usec_t
) -1)
187 target_latency
= max_sink_latency
> min_total_latency
? max_sink_latency
: min_total_latency
;
189 pa_log_info("[%s] target latency is %0.0f usec.", u
->sink
->name
, (float) target_latency
);
190 pa_log_info("[%s] master %s latency %0.0f usec.", u
->sink
->name
, u
->master
->sink
->name
, (float) u
->master
->total_latency
);
192 base_rate
= u
->sink
->sample_spec
.rate
;
194 for (o
= pa_idxset_first(u
->outputs
, &idx
); o
; o
= pa_idxset_next(u
->outputs
, &idx
)) {
195 uint32_t r
= base_rate
;
197 if (!o
->sink_input
|| !PA_SINK_OPENED(pa_sink_get_state(o
->sink
)))
200 if (o
->total_latency
< target_latency
)
201 r
-= (uint32_t) (((((double) target_latency
- o
->total_latency
))/u
->adjust_time
)*r
/PA_USEC_PER_SEC
);
202 else if (o
->total_latency
> target_latency
)
203 r
+= (uint32_t) (((((double) o
->total_latency
- target_latency
))/u
->adjust_time
)*r
/PA_USEC_PER_SEC
);
205 if (r
< (uint32_t) (base_rate
*0.9) || r
> (uint32_t) (base_rate
*1.1)) {
206 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
);
207 pa_sink_input_set_rate(o
->sink_input
, base_rate
);
209 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
);
210 pa_sink_input_set_rate(o
->sink_input
, r
);
215 static void time_callback(pa_mainloop_api
*a
, pa_time_event
* e
, const struct timeval
*tv
, void *userdata
) {
216 struct userdata
*u
= userdata
;
221 pa_assert(u
->time_event
== e
);
226 n
.tv_sec
+= u
->adjust_time
;
227 u
->sink
->core
->mainloop
->time_restart(e
, &n
);
230 static void thread_func(void *userdata
) {
231 struct userdata
*u
= userdata
;
235 pa_log_debug("Thread starting up");
237 if (u
->core
->realtime_scheduling
)
238 pa_make_realtime(u
->core
->realtime_priority
+1);
240 pa_thread_mq_install(&u
->thread_mq
);
241 pa_rtpoll_install(u
->rtpoll
);
243 pa_rtclock_get(&u
->thread_info
.timestamp
);
244 u
->thread_info
.in_null_mode
= FALSE
;
249 /* If no outputs are connected, render some data and drop it immediately. */
250 if (u
->sink
->thread_info
.state
== PA_SINK_RUNNING
&& !u
->thread_info
.active_outputs
) {
253 /* Just rewind if necessary, since we are in NULL mode, we
254 * don't have to pass this on */
255 pa_sink_process_rewind(u
->sink
);
257 pa_rtclock_get(&now
);
259 if (!u
->thread_info
.in_null_mode
|| pa_timeval_cmp(&u
->thread_info
.timestamp
, &now
) <= 0) {
260 pa_sink_skip(u
->sink
, u
->block_size
);
262 if (!u
->thread_info
.in_null_mode
)
263 u
->thread_info
.timestamp
= now
;
265 pa_timeval_add(&u
->thread_info
.timestamp
, pa_bytes_to_usec(u
->block_size
, &u
->sink
->sample_spec
));
268 pa_rtpoll_set_timer_absolute(u
->rtpoll
, &u
->thread_info
.timestamp
);
269 u
->thread_info
.in_null_mode
= TRUE
;
272 pa_rtpoll_set_timer_disabled(u
->rtpoll
);
273 u
->thread_info
.in_null_mode
= FALSE
;
276 /* Hmm, nothing to do. Let's sleep */
277 if ((ret
= pa_rtpoll_run(u
->rtpoll
, TRUE
)) < 0) {
278 pa_log_info("pa_rtpoll_run() = %i", ret
);
287 /* If this was no regular exit from the loop we have to continue
288 * processing messages until we received PA_MESSAGE_SHUTDOWN */
289 pa_asyncmsgq_post(u
->thread_mq
.outq
, PA_MSGOBJECT(u
->core
), PA_CORE_MESSAGE_UNLOAD_MODULE
, u
->module
, 0, NULL
, NULL
);
290 pa_asyncmsgq_wait_for(u
->thread_mq
.inq
, PA_MESSAGE_SHUTDOWN
);
293 pa_log_debug("Thread shutting down");
296 /* Called from I/O thread context */
297 static void render_memblock(struct userdata
*u
, struct output
*o
, size_t length
) {
301 /* We are run by the sink thread, on behalf of an output (o). The
302 * other output is waiting for us, hence it is safe to access its
303 * mainblockq and asyncmsgq directly. */
305 /* If we are not running, we cannot produce any data */
306 if (!pa_atomic_load(&u
->thread_info
.running
))
309 /* Maybe there's some data in the requesting output's queue
311 while (pa_asyncmsgq_process_one(o
->inq
) > 0)
314 /* Ok, now let's prepare some data if we really have to */
315 while (!pa_memblockq_is_readable(o
->memblockq
)) {
320 pa_sink_render(u
->sink
, length
, &chunk
);
322 /* OK, let's send this data to the other threads */
323 for (j
= u
->thread_info
.active_outputs
; j
; j
= j
->next
)
325 /* Send to other outputs, which are not the requesting
329 pa_asyncmsgq_post(j
->inq
, PA_MSGOBJECT(j
->sink_input
), SINK_INPUT_MESSAGE_POST
, NULL
, 0, &chunk
, NULL
);
331 /* And place it directly into the requesting output's queue */
333 pa_memblockq_push_align(o
->memblockq
, &chunk
);
335 pa_memblock_unref(chunk
.memblock
);
339 /* Called from I/O thread context */
340 static void request_memblock(struct output
*o
, size_t length
) {
342 pa_sink_input_assert_ref(o
->sink_input
);
343 pa_sink_assert_ref(o
->userdata
->sink
);
345 /* If another thread already prepared some data we received
346 * the data over the asyncmsgq, hence let's first process
348 while (pa_asyncmsgq_process_one(o
->inq
) > 0)
351 /* Check whether we're now readable */
352 if (pa_memblockq_is_readable(o
->memblockq
))
355 /* OK, we need to prepare new data, but only if the sink is actually running */
356 if (pa_atomic_load(&o
->userdata
->thread_info
.running
))
357 pa_asyncmsgq_send(o
->outq
, PA_MSGOBJECT(o
->userdata
->sink
), SINK_MESSAGE_NEED
, o
, length
, NULL
);
360 /* Called from I/O thread context */
361 static int sink_input_pop_cb(pa_sink_input
*i
, size_t nbytes
, pa_memchunk
*chunk
) {
364 pa_sink_input_assert_ref(i
);
365 pa_assert_se(o
= i
->userdata
);
367 /* If necessary, get some new data */
368 request_memblock(o
, nbytes
);
370 if (pa_memblockq_peek(o
->memblockq
, chunk
) < 0)
373 pa_memblockq_drop(o
->memblockq
, chunk
->length
);
377 /* Called from I/O thread context */
378 static void sink_input_attach_cb(pa_sink_input
*i
) {
381 pa_sink_input_assert_ref(i
);
382 pa_assert_se(o
= i
->userdata
);
384 /* Set up the queue from the sink thread to us */
385 pa_assert(!o
->inq_rtpoll_item
);
386 o
->inq_rtpoll_item
= pa_rtpoll_item_new_asyncmsgq(
388 PA_RTPOLL_LATE
, /* This one is not that important, since we check for data in _peek() anyway. */
392 /* Called from I/O thread context */
393 static void sink_input_detach_cb(pa_sink_input
*i
) {
396 pa_sink_input_assert_ref(i
);
397 pa_assert_se(o
= i
->userdata
);
399 /* Shut down the queue from the sink thread to us */
400 pa_assert(o
->inq_rtpoll_item
);
401 pa_rtpoll_item_free(o
->inq_rtpoll_item
);
402 o
->inq_rtpoll_item
= NULL
;
405 /* Called from main context */
406 static void sink_input_kill_cb(pa_sink_input
*i
) {
409 pa_sink_input_assert_ref(i
);
410 pa_assert(o
= i
->userdata
);
412 pa_module_unload_request(o
->userdata
->module
);
416 /* Called from thread context */
417 static int sink_input_process_msg(pa_msgobject
*obj
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
418 struct output
*o
= PA_SINK_INPUT(obj
)->userdata
;
422 case PA_SINK_INPUT_MESSAGE_GET_LATENCY
: {
425 *r
= pa_bytes_to_usec(pa_memblockq_get_length(o
->memblockq
), &o
->sink_input
->sample_spec
);
427 /* Fall through, the default handler will add in the extra
428 * latency added by the resampler */
432 case SINK_INPUT_MESSAGE_POST
:
434 if (PA_SINK_OPENED(o
->sink_input
->sink
->thread_info
.state
))
435 pa_memblockq_push_align(o
->memblockq
, chunk
);
437 pa_memblockq_flush(o
->memblockq
);
443 return pa_sink_input_process_msg(obj
, code
, data
, offset
, chunk
);
446 /* Called from main context */
447 static void disable_output(struct output
*o
) {
453 pa_asyncmsgq_send(o
->userdata
->sink
->asyncmsgq
, PA_MSGOBJECT(o
->userdata
->sink
), SINK_MESSAGE_REMOVE_OUTPUT
, o
, 0, NULL
);
454 pa_sink_input_unlink(o
->sink_input
);
455 pa_sink_input_unref(o
->sink_input
);
456 o
->sink_input
= NULL
;
460 /* Called from main context */
461 static void enable_output(struct output
*o
) {
467 if (output_create_sink_input(o
) >= 0) {
469 pa_memblockq_flush(o
->memblockq
);
471 pa_sink_input_put(o
->sink_input
);
473 if (o
->userdata
->sink
&& PA_SINK_LINKED(pa_sink_get_state(o
->userdata
->sink
)))
474 pa_asyncmsgq_send(o
->userdata
->sink
->asyncmsgq
, PA_MSGOBJECT(o
->userdata
->sink
), SINK_MESSAGE_ADD_OUTPUT
, o
, 0, NULL
);
478 /* Called from main context */
479 static void suspend(struct userdata
*u
) {
485 /* Let's suspend by unlinking all streams */
486 for (o
= pa_idxset_first(u
->outputs
, &idx
); o
; o
= pa_idxset_next(u
->outputs
, &idx
))
489 pick_master(u
, NULL
);
491 pa_log_info("Device suspended...");
494 /* Called from main context */
495 static void unsuspend(struct userdata
*u
) {
502 for (o
= pa_idxset_first(u
->outputs
, &idx
); o
; o
= pa_idxset_next(u
->outputs
, &idx
)) {
504 pa_sink_suspend(o
->sink
, FALSE
);
506 if (PA_SINK_OPENED(pa_sink_get_state(o
->sink
)))
510 pick_master(u
, NULL
);
512 pa_log_info("Resumed successfully...");
515 /* Called from main context */
516 static int sink_set_state(pa_sink
*sink
, pa_sink_state_t state
) {
519 pa_sink_assert_ref(sink
);
520 pa_assert_se(u
= sink
->userdata
);
522 /* Please note that in contrast to the ALSA modules we call
523 * suspend/unsuspend from main context here! */
526 case PA_SINK_SUSPENDED
:
527 pa_assert(PA_SINK_OPENED(pa_sink_get_state(u
->sink
)));
533 case PA_SINK_RUNNING
:
535 if (pa_sink_get_state(u
->sink
) == PA_SINK_SUSPENDED
)
540 case PA_SINK_UNLINKED
:
548 /* Called from thread context of the master */
549 static int sink_process_msg(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
550 struct userdata
*u
= PA_SINK(o
)->userdata
;
554 case PA_SINK_MESSAGE_SET_STATE
:
555 pa_atomic_store(&u
->thread_info
.running
, PA_PTR_TO_UINT(data
) == PA_SINK_RUNNING
);
558 case PA_SINK_MESSAGE_GET_LATENCY
:
560 /* This code will only be called when running in NULL
561 * mode, i.e. when no output is attached. See
562 * sink_get_latency_cb() below */
564 if (u
->thread_info
.in_null_mode
) {
567 if (pa_timeval_cmp(&u
->thread_info
.timestamp
, pa_rtclock_get(&now
)) > 0) {
568 *((pa_usec_t
*) data
) = pa_timeval_diff(&u
->thread_info
.timestamp
, &now
);
573 *((pa_usec_t
*) data
) = 0;
577 case SINK_MESSAGE_ADD_OUTPUT
: {
578 struct output
*op
= data
;
580 PA_LLIST_PREPEND(struct output
, u
->thread_info
.active_outputs
, op
);
582 pa_assert(!op
->outq_rtpoll_item
);
584 /* Create pa_asyncmsgq to the sink thread */
586 op
->outq_rtpoll_item
= pa_rtpoll_item_new_asyncmsgq(
588 PA_RTPOLL_EARLY
-1, /* This item is very important */
594 case SINK_MESSAGE_REMOVE_OUTPUT
: {
595 struct output
*op
= data
;
597 PA_LLIST_REMOVE(struct output
, u
->thread_info
.active_outputs
, op
);
599 /* Remove the q that leads from this output to the sink thread */
601 pa_assert(op
->outq_rtpoll_item
);
602 pa_rtpoll_item_free(op
->outq_rtpoll_item
);
603 op
->outq_rtpoll_item
= NULL
;
608 case SINK_MESSAGE_NEED
:
609 render_memblock(u
, data
, (size_t) offset
);
613 return pa_sink_process_msg(o
, code
, data
, offset
, chunk
);
616 /* Called from main context */
617 static pa_usec_t
sink_get_latency_cb(pa_sink
*s
) {
620 pa_sink_assert_ref(s
);
621 pa_assert_se(u
= s
->userdata
);
624 /* If we have a master sink, we just return the latency of it
625 * and add our own buffering on top */
627 if (!u
->master
->sink_input
)
631 pa_sink_input_get_latency(u
->master
->sink_input
) +
632 pa_sink_get_latency(u
->master
->sink
);
637 /* We have no master, hence let's ask our own thread which
638 * implements the NULL sink */
640 if (pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_LATENCY
, &usec
, 0, NULL
) < 0)
647 static void update_description(struct userdata
*u
) {
655 if (pa_idxset_isempty(u
->outputs
)) {
656 pa_sink_set_description(u
->sink
, "Simultaneous output");
660 t
= pa_xstrdup("Simultaneous output to");
662 for (o
= pa_idxset_first(u
->outputs
, &idx
); o
; o
= pa_idxset_next(u
->outputs
, &idx
)) {
666 e
= pa_sprintf_malloc("%s %s", t
, pa_strnull(pa_proplist_gets(o
->sink
->proplist
, PA_PROP_DEVICE_DESCRIPTION
)));
669 e
= pa_sprintf_malloc("%s, %s", t
, pa_strnull(pa_proplist_gets(o
->sink
->proplist
, PA_PROP_DEVICE_DESCRIPTION
)));
675 pa_sink_set_description(u
->sink
, t
);
679 static void update_master(struct userdata
*u
, struct output
*o
) {
686 pa_log_info("Master sink is now '%s'", o
->sink_input
->sink
->name
);
688 pa_log_info("No master selected, lacking suitable outputs.");
691 static void pick_master(struct userdata
*u
, struct output
*except
) {
697 u
->master
!= except
&&
698 u
->master
->sink_input
&&
699 PA_SINK_OPENED(pa_sink_get_state(u
->master
->sink
))) {
700 update_master(u
, u
->master
);
704 for (o
= pa_idxset_first(u
->outputs
, &idx
); o
; o
= pa_idxset_next(u
->outputs
, &idx
))
707 PA_SINK_OPENED(pa_sink_get_state(o
->sink
))) {
712 update_master(u
, NULL
);
715 static int output_create_sink_input(struct output
*o
) {
716 pa_sink_input_new_data data
;
724 t
= pa_sprintf_malloc("Simultaneous output on %s", pa_strnull(pa_proplist_gets(o
->sink
->proplist
, PA_PROP_DEVICE_DESCRIPTION
)));
726 pa_sink_input_new_data_init(&data
);
728 data
.driver
= __FILE__
;
729 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
, t
);
730 pa_sink_input_new_data_set_sample_spec(&data
, &o
->userdata
->sink
->sample_spec
);
731 pa_sink_input_new_data_set_channel_map(&data
, &o
->userdata
->sink
->channel_map
);
732 data
.module
= o
->userdata
->module
;
733 data
.resample_method
= o
->userdata
->resample_method
;
735 o
->sink_input
= pa_sink_input_new(o
->userdata
->core
, &data
, PA_SINK_INPUT_VARIABLE_RATE
|PA_SINK_INPUT_DONT_MOVE
);
737 pa_sink_input_new_data_done(&data
);
744 o
->sink_input
->parent
.process_msg
= sink_input_process_msg
;
745 o
->sink_input
->pop
= sink_input_pop_cb
;
746 o
->sink_input
->attach
= sink_input_attach_cb
;
747 o
->sink_input
->detach
= sink_input_detach_cb
;
748 o
->sink_input
->kill
= sink_input_kill_cb
;
749 o
->sink_input
->userdata
= o
;
755 static struct output
*output_new(struct userdata
*u
, pa_sink
*sink
) {
762 o
= pa_xnew(struct output
, 1);
764 o
->inq
= pa_asyncmsgq_new(0);
765 o
->outq
= pa_asyncmsgq_new(0);
766 o
->inq_rtpoll_item
= NULL
;
767 o
->outq_rtpoll_item
= NULL
;
769 o
->sink_input
= NULL
;
770 o
->memblockq
= pa_memblockq_new(
774 pa_frame_size(&u
->sink
->sample_spec
),
780 pa_assert_se(pa_idxset_put(u
->outputs
, o
, NULL
) == 0);
782 if (u
->sink
&& PA_SINK_LINKED(pa_sink_get_state(u
->sink
)))
783 pa_asyncmsgq_send(u
->sink
->asyncmsgq
, PA_MSGOBJECT(u
->sink
), SINK_MESSAGE_ADD_OUTPUT
, o
, 0, NULL
);
785 /* If the sink is not yet started, we need to do the activation ourselves */
786 PA_LLIST_PREPEND(struct output
, u
->thread_info
.active_outputs
, o
);
788 o
->outq_rtpoll_item
= pa_rtpoll_item_new_asyncmsgq(
790 PA_RTPOLL_EARLY
-1, /* This item is very important */
794 if (PA_SINK_OPENED(pa_sink_get_state(u
->sink
)) || pa_sink_get_state(u
->sink
) == PA_SINK_INIT
) {
795 pa_sink_suspend(sink
, FALSE
);
797 if (PA_SINK_OPENED(pa_sink_get_state(sink
)))
798 if (output_create_sink_input(o
) < 0)
803 update_description(u
);
810 pa_idxset_remove_by_data(u
->outputs
, o
, NULL
);
813 pa_sink_input_unlink(o
->sink_input
);
814 pa_sink_input_unref(o
->sink_input
);
818 pa_memblockq_free(o
->memblockq
);
821 pa_asyncmsgq_unref(o
->inq
);
824 pa_asyncmsgq_unref(o
->outq
);
832 static pa_hook_result_t
sink_new_hook_cb(pa_core
*c
, pa_sink
*s
, struct userdata
* u
) {
835 pa_core_assert_ref(c
);
836 pa_sink_assert_ref(s
);
838 pa_assert(u
->automatic
);
840 if (!(s
->flags
& PA_SINK_HARDWARE
) || s
== u
->sink
)
843 pa_log_info("Configuring new sink: %s", s
->name
);
845 if (!(o
= output_new(u
, s
))) {
846 pa_log("Failed to create sink input on sink '%s'.", s
->name
);
851 pa_sink_input_put(o
->sink_input
);
853 pick_master(u
, NULL
);
858 static pa_hook_result_t
sink_unlink_hook_cb(pa_core
*c
, pa_sink
*s
, struct userdata
* u
) {
863 pa_sink_assert_ref(s
);
869 for (o
= pa_idxset_first(u
->outputs
, &idx
); o
; o
= pa_idxset_next(u
->outputs
, &idx
))
876 pa_log_info("Unconfiguring sink: %s", s
->name
);
883 static pa_hook_result_t
sink_state_changed_hook_cb(pa_core
*c
, pa_sink
*s
, struct userdata
* u
) {
886 pa_sink_state_t state
;
891 for (o
= pa_idxset_first(u
->outputs
, &idx
); o
; o
= pa_idxset_next(u
->outputs
, &idx
))
898 state
= pa_sink_get_state(s
);
900 if (PA_SINK_OPENED(state
) && PA_SINK_OPENED(pa_sink_get_state(u
->sink
)) && !o
->sink_input
) {
902 pick_master(u
, NULL
);
905 if (state
== PA_SINK_SUSPENDED
&& o
->sink_input
) {
913 int pa__init(pa_module
*m
) {
915 pa_modargs
*ma
= NULL
;
916 const char *master_name
, *slaves
, *rm
;
917 pa_sink
*master_sink
= NULL
;
918 int resample_method
= PA_RESAMPLER_TRIVIAL
;
923 pa_sink_new_data data
;
927 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
928 pa_log("failed to parse module arguments");
932 if ((rm
= pa_modargs_get_value(ma
, "resample_method", NULL
))) {
933 if ((resample_method
= pa_parse_resample_method(rm
)) < 0) {
934 pa_log("invalid resample method '%s'", rm
);
939 u
= pa_xnew(struct userdata
, 1);
945 u
->time_event
= NULL
;
946 u
->adjust_time
= DEFAULT_ADJUST_TIME
;
947 pa_thread_mq_init(&u
->thread_mq
, m
->core
->mainloop
);
948 u
->rtpoll
= pa_rtpoll_new();
950 u
->resample_method
= resample_method
;
951 u
->outputs
= pa_idxset_new(NULL
, NULL
);
952 memset(&u
->adjust_timestamp
, 0, sizeof(u
->adjust_timestamp
));
953 u
->sink_new_slot
= u
->sink_unlink_slot
= u
->sink_state_changed_slot
= NULL
;
954 PA_LLIST_HEAD_INIT(struct output
, u
->thread_info
.active_outputs
);
955 pa_atomic_store(&u
->thread_info
.running
, FALSE
);
956 u
->thread_info
.in_null_mode
= FALSE
;
957 pa_rtpoll_item_new_asyncmsgq(u
->rtpoll
, PA_RTPOLL_EARLY
, u
->thread_mq
.inq
);
959 if (pa_modargs_get_value_u32(ma
, "adjust_time", &u
->adjust_time
) < 0) {
960 pa_log("Failed to parse adjust_time value");
964 master_name
= pa_modargs_get_value(ma
, "master", NULL
);
965 slaves
= pa_modargs_get_value(ma
, "slaves", NULL
);
966 if (!master_name
!= !slaves
) {
967 pa_log("No master or slave sinks specified");
972 if (!(master_sink
= pa_namereg_get(m
->core
, master_name
, PA_NAMEREG_SINK
, 1))) {
973 pa_log("Invalid master sink '%s'", master_name
);
977 ss
= master_sink
->sample_spec
;
978 u
->automatic
= FALSE
;
981 ss
= m
->core
->default_sample_spec
;
985 if ((pa_modargs_get_sample_spec(ma
, &ss
) < 0)) {
986 pa_log("Invalid sample specification.");
990 if (master_sink
&& ss
.channels
== master_sink
->sample_spec
.channels
)
991 map
= master_sink
->channel_map
;
993 pa_assert_se(pa_channel_map_init_auto(&map
, ss
.channels
, PA_CHANNEL_MAP_AUX
));
994 pa_channel_map_init_auto(&map
, ss
.channels
, PA_CHANNEL_MAP_DEFAULT
);
997 if ((pa_modargs_get_channel_map(ma
, NULL
, &map
) < 0)) {
998 pa_log("Invalid channel map.");
1002 if (ss
.channels
!= map
.channels
) {
1003 pa_log("Channel map and sample specification don't match.");
1007 pa_sink_new_data_init(&data
);
1008 data
.name
= pa_xstrdup(pa_modargs_get_value(ma
, "sink_name", DEFAULT_SINK_NAME
));
1009 data
.namereg_fail
= FALSE
;
1010 data
.driver
= __FILE__
;
1012 pa_sink_new_data_set_sample_spec(&data
, &ss
);
1013 pa_sink_new_data_set_channel_map(&data
, &map
);
1014 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
, "Simultaneous Output");
1016 u
->sink
= pa_sink_new(m
->core
, &data
, PA_SINK_LATENCY
);
1017 pa_sink_new_data_done(&data
);
1020 pa_log("Failed to create sink");
1024 u
->sink
->parent
.process_msg
= sink_process_msg
;
1025 u
->sink
->get_latency
= sink_get_latency_cb
;
1026 u
->sink
->set_state
= sink_set_state
;
1027 u
->sink
->userdata
= u
;
1029 pa_sink_set_rtpoll(u
->sink
, u
->rtpoll
);
1030 pa_sink_set_asyncmsgq(u
->sink
, u
->thread_mq
.inq
);
1032 u
->block_size
= pa_bytes_per_second(&ss
) / 20; /* 50 ms */
1033 if (u
->block_size
<= 0)
1034 u
->block_size
= pa_frame_size(&ss
);
1036 if (!u
->automatic
) {
1037 const char*split_state
;
1041 /* The master and slaves have been specified manually */
1043 if (!(u
->master
= output_new(u
, master_sink
))) {
1044 pa_log("Failed to create master sink input on sink '%s'.", master_sink
->name
);
1049 while ((n
= pa_split(slaves
, ",", &split_state
))) {
1050 pa_sink
*slave_sink
;
1052 if (!(slave_sink
= pa_namereg_get(m
->core
, n
, PA_NAMEREG_SINK
, 1)) || slave_sink
== u
->sink
) {
1053 pa_log("Invalid slave sink '%s'", n
);
1060 if (!output_new(u
, slave_sink
)) {
1061 pa_log("Failed to create slave sink input on sink '%s'.", slave_sink
->name
);
1066 if (pa_idxset_size(u
->outputs
) <= 1)
1067 pa_log_warn("No slave sinks specified.");
1069 u
->sink_new_slot
= NULL
;
1074 /* We're in automatic mode, we elect one hw sink to the master
1075 * and attach all other hw sinks as slaves to it */
1077 for (s
= pa_idxset_first(m
->core
->sinks
, &idx
); s
; s
= pa_idxset_next(m
->core
->sinks
, &idx
)) {
1079 if (!(s
->flags
& PA_SINK_HARDWARE
) || s
== u
->sink
)
1082 if (!output_new(u
, s
)) {
1083 pa_log("Failed to create sink input on sink '%s'.", s
->name
);
1088 u
->sink_new_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SINK_PUT
], (pa_hook_cb_t
) sink_new_hook_cb
, u
);
1091 u
->sink_unlink_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SINK_UNLINK
], (pa_hook_cb_t
) sink_unlink_hook_cb
, u
);
1092 u
->sink_state_changed_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SINK_STATE_CHANGED
], (pa_hook_cb_t
) sink_state_changed_hook_cb
, u
);
1094 pick_master(u
, NULL
);
1096 if (!(u
->thread
= pa_thread_new(thread_func
, u
))) {
1097 pa_log("Failed to create thread.");
1101 /* Activate the sink and the sink inputs */
1102 pa_sink_put(u
->sink
);
1104 for (o
= pa_idxset_first(u
->outputs
, &idx
); o
; o
= pa_idxset_next(u
->outputs
, &idx
))
1106 pa_sink_input_put(o
->sink_input
);
1108 if (u
->adjust_time
> 0) {
1110 pa_gettimeofday(&tv
);
1111 tv
.tv_sec
+= u
->adjust_time
;
1112 u
->time_event
= m
->core
->mainloop
->time_new(m
->core
->mainloop
, &tv
, time_callback
, u
);
1115 pa_modargs_free(ma
);
1122 pa_modargs_free(ma
);
1129 static void output_free(struct output
*o
) {
1132 pick_master(o
->userdata
, o
);
1136 pa_assert_se(pa_idxset_remove_by_data(o
->userdata
->outputs
, o
, NULL
));
1138 update_description(o
->userdata
);
1140 if (o
->inq_rtpoll_item
)
1141 pa_rtpoll_item_free(o
->inq_rtpoll_item
);
1143 if (o
->outq_rtpoll_item
)
1144 pa_rtpoll_item_free(o
->outq_rtpoll_item
);
1147 pa_asyncmsgq_unref(o
->inq
);
1150 pa_asyncmsgq_unref(o
->outq
);
1153 pa_memblockq_free(o
->memblockq
);
1158 void pa__done(pa_module
*m
) {
1164 if (!(u
= m
->userdata
))
1167 if (u
->sink_new_slot
)
1168 pa_hook_slot_free(u
->sink_new_slot
);
1170 if (u
->sink_unlink_slot
)
1171 pa_hook_slot_free(u
->sink_unlink_slot
);
1173 if (u
->sink_state_changed_slot
)
1174 pa_hook_slot_free(u
->sink_state_changed_slot
);
1177 while ((o
= pa_idxset_first(u
->outputs
, NULL
)))
1180 pa_idxset_free(u
->outputs
, NULL
, NULL
);
1184 pa_sink_unlink(u
->sink
);
1187 pa_asyncmsgq_send(u
->thread_mq
.inq
, NULL
, PA_MESSAGE_SHUTDOWN
, NULL
, 0, NULL
);
1188 pa_thread_free(u
->thread
);
1191 pa_thread_mq_done(&u
->thread_mq
);
1194 pa_sink_unref(u
->sink
);
1197 pa_rtpoll_free(u
->rtpoll
);
1200 u
->core
->mainloop
->time_free(u
->time_event
);