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
)
57 "sink_name=<name for the sink> "
58 "master=<master sink> "
59 "slaves=<slave sinks> "
60 "adjust_time=<seconds> "
61 "resample_method=<method> "
62 "format=<sample format> "
63 "channels=<number of channels> "
65 "channel_map=<channel map>")
67 #define DEFAULT_SINK_NAME "combined"
68 #define MEMBLOCKQ_MAXLENGTH (1024*170)
70 #define DEFAULT_ADJUST_TIME 10
72 static const char* const valid_modargs
[] = {
86 struct userdata
*userdata
;
88 pa_sink_input
*sink_input
;
90 pa_asyncmsgq
*inq
, /* Message queue from the master to this sink input */
91 *outq
; /* Message queue from this sink input to the master */
92 pa_rtpoll_item
*inq_rtpoll_item
, *outq_rtpoll_item
;
94 pa_memblockq
*memblockq
;
96 pa_usec_t total_latency
;
98 PA_LLIST_FIELDS(struct output
);
107 pa_thread_mq thread_mq
;
110 struct output
*master
;
112 pa_time_event
*time_event
;
113 uint32_t adjust_time
;
118 struct timespec timestamp
;
120 pa_hook_slot
*sink_new_slot
, *sink_unlink_slot
, *sink_state_changed_slot
;
122 pa_resample_method_t resample_method
;
124 struct timespec adjust_timestamp
;
126 pa_idxset
* outputs
; /* managed in main context */
129 PA_LLIST_HEAD(struct output
, outputs
); /* managed in IO thread context */
130 struct output
*master
;
135 SINK_MESSAGE_ADD_OUTPUT
= PA_SINK_MESSAGE_MAX
,
136 SINK_MESSAGE_REMOVE_OUTPUT
,
138 SINK_MESSAGE_SET_MASTER
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 userdata
*u
, struct output
*o
);
147 static int update_master(struct userdata
*u
, struct output
*o
);
148 static int 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)
162 if (!PA_SINK_OPENED(pa_sink_get_state(u
->sink
)))
165 for (o
= pa_idxset_first(u
->outputs
, &idx
); o
; o
= pa_idxset_next(u
->outputs
, &idx
)) {
166 uint32_t sink_latency
;
168 if (!o
->sink_input
|| !PA_SINK_OPENED(pa_sink_get_state(o
->sink
)))
171 sink_latency
= o
->sink_input
->sink
? pa_sink_get_latency(o
->sink_input
->sink
) : 0;
172 o
->total_latency
= sink_latency
+ pa_sink_input_get_latency(o
->sink_input
);
174 if (sink_latency
> max_sink_latency
)
175 max_sink_latency
= sink_latency
;
177 if (o
->total_latency
< min_total_latency
)
178 min_total_latency
= o
->total_latency
;
181 if (min_total_latency
== (pa_usec_t
) -1)
184 target_latency
= max_sink_latency
> min_total_latency
? max_sink_latency
: min_total_latency
;
186 pa_log_info("[%s] target latency is %0.0f usec.", u
->sink
->name
, (float) target_latency
);
187 pa_log_info("[%s] master is %s", u
->sink
->name
, u
->master
->sink
->description
);
189 base_rate
= u
->sink
->sample_spec
.rate
;
191 for (o
= pa_idxset_first(u
->outputs
, &idx
); o
; o
= pa_idxset_next(u
->outputs
, &idx
)) {
192 uint32_t r
= base_rate
;
194 if (!o
->sink_input
|| !PA_SINK_OPENED(pa_sink_get_state(o
->sink
)))
197 if (o
->total_latency
< target_latency
)
198 r
-= (uint32_t) (((((double) target_latency
- o
->total_latency
))/u
->adjust_time
)*r
/ 1000000);
199 else if (o
->total_latency
> target_latency
)
200 r
+= (uint32_t) (((((double) o
->total_latency
- target_latency
))/u
->adjust_time
)*r
/ 1000000);
202 if (r
< (uint32_t) (base_rate
*0.9) || r
> (uint32_t) (base_rate
*1.1)) {
203 pa_log_warn("[%s] sample rates too different, not adjusting (%u vs. %u).", o
->sink_input
->name
, base_rate
, r
);
204 pa_sink_input_set_rate(o
->sink_input
, base_rate
);
206 pa_log_info("[%s] new rate is %u Hz; ratio is %0.3f; latency is %0.0f usec.", o
->sink_input
->name
, r
, (double) r
/ base_rate
, (float) o
->total_latency
);
207 pa_sink_input_set_rate(o
->sink_input
, r
);
212 static void time_callback(pa_mainloop_api
*a
, pa_time_event
* e
, const struct timeval
*tv
, void *userdata
) {
213 struct userdata
*u
= userdata
;
218 pa_assert(u
->time_event
== e
);
223 n
.tv_sec
+= u
->adjust_time
;
224 u
->sink
->core
->mainloop
->time_restart(e
, &n
);
227 static void thread_func(void *userdata
) {
228 struct userdata
*u
= userdata
;
232 pa_log_debug("Thread starting up");
234 pa_thread_mq_install(&u
->thread_mq
);
235 pa_rtpoll_install(u
->rtpoll
);
237 pa_rtclock_get(&u
->timestamp
);
239 /* This is only run when we are in NULL mode, to make sure that
240 * playback doesn't stop. In all other cases we hook our stuff
241 * into the master sink. */
246 /* Render some data and drop it immediately */
247 if (u
->sink
->thread_info
.state
== PA_SINK_RUNNING
) {
250 pa_rtclock_get(&now
);
252 if (pa_timespec_cmp(&u
->timestamp
, &now
) <= 0) {
253 pa_sink_skip(u
->sink
, u
->block_size
);
254 pa_timespec_add(&u
->timestamp
, pa_bytes_to_usec(u
->block_size
, &u
->sink
->sample_spec
));
257 pa_rtpoll_set_timer_absolute(u
->rtpoll
, &u
->timestamp
);
259 pa_rtpoll_set_timer_disabled(u
->rtpoll
);
261 /* Hmm, nothing to do. Let's sleep */
262 if ((ret
= pa_rtpoll_run(u
->rtpoll
, 1)) < 0)
270 /* If this was no regular exit from the loop we have to continue
271 * processing messages until we received PA_MESSAGE_SHUTDOWN */
272 pa_asyncmsgq_post(u
->thread_mq
.outq
, PA_MSGOBJECT(u
->core
), PA_CORE_MESSAGE_UNLOAD_MODULE
, u
->module
, 0, NULL
, NULL
);
273 pa_asyncmsgq_wait_for(u
->thread_mq
.inq
, PA_MESSAGE_SHUTDOWN
);
276 pa_log_debug("Thread shutting down");
279 /* Called from I/O thread context */
280 static void render_memblock(struct userdata
*u
, struct output
*o
, size_t length
) {
284 if (!PA_SINK_OPENED(u
->sink
->thread_info
.state
))
287 /* We are run by the master output (u->master), possibly on behalf
288 * of another output (o). The other output is waiting for us,
289 * hence it is safe to access its mainblockq directly. */
291 /* Maybe there's some data in the requesting output's queue
293 while (pa_asyncmsgq_process_one(o
->inq
) > 0)
296 /* Ok, now let's prepare some data if we really have to */
297 while (!pa_memblockq_is_readable(o
->memblockq
)) {
302 pa_sink_render(u
->sink
, length
, &chunk
);
304 /* OK, let's send this data to the other threads */
305 for (j
= o
->userdata
->thread_info
.outputs
; j
; j
= j
->next
)
307 /* Send to other outputs, which are not the requesting
308 * one, and not the master */
310 if (j
!= o
&& j
!= u
->thread_info
.master
&& j
->sink_input
)
311 pa_asyncmsgq_post(j
->inq
, PA_MSGOBJECT(j
->sink_input
), SINK_INPUT_MESSAGE_POST
, NULL
, 0, &chunk
, NULL
);
313 /* Now push it into the master queue */
314 pa_memblockq_push_align(u
->thread_info
.master
->memblockq
, &chunk
);
316 /* And into the requesting output's queue */
317 if (o
!= u
->thread_info
.master
)
318 pa_memblockq_push_align(o
->memblockq
, &chunk
);
320 pa_memblock_unref(chunk
.memblock
);
324 /* Called from I/O thread context */
325 static void request_memblock(struct output
*o
, size_t length
) {
327 pa_sink_input_assert_ref(o
->sink_input
);
328 pa_sink_assert_ref(o
->userdata
->sink
);
330 /* If another thread already prepared some data we received
331 * the data over the asyncmsgq, hence let's first process
333 while (pa_asyncmsgq_process_one(o
->inq
) > 0)
336 /* Check whether we're now readable */
337 if (pa_memblockq_is_readable(o
->memblockq
))
340 /* OK, we need to prepare new data */
342 if (o
== o
->userdata
->thread_info
.master
)
343 /* OK, we're the master, so let's render some data */
344 render_memblock(o
->userdata
, o
, length
);
347 /* We're not the master, we need to ask the master to do the
348 * rendering for us */
350 pa_asyncmsgq_send(o
->outq
, PA_MSGOBJECT(o
->userdata
->sink
), SINK_MESSAGE_NEED
, o
, length
, NULL
);
353 /* Called from I/O thread context */
354 static int sink_input_peek_cb(pa_sink_input
*i
, size_t length
, pa_memchunk
*chunk
) {
357 pa_sink_input_assert_ref(i
);
358 pa_assert_se(o
= i
->userdata
);
360 /* If necessary, get some new data */
361 request_memblock(o
, length
);
363 return pa_memblockq_peek(o
->memblockq
, chunk
);
366 /* Called from I/O thread context */
367 static void sink_input_drop_cb(pa_sink_input
*i
, size_t length
) {
370 pa_sink_input_assert_ref(i
);
371 pa_assert(length
> 0);
372 pa_assert_se(o
= i
->userdata
);
374 pa_memblockq_drop(o
->memblockq
, length
);
377 /* Called from I/O thread context for the master */
378 static void create_master_rtpolls(struct userdata
*u
) {
383 pa_assert(!u
->master
->outq_rtpoll_item
);
385 /* Set up the queues from the outputs to the master */
386 for (k
= u
->thread_info
.outputs
; k
; k
= k
->next
) {
388 pa_assert(!k
->outq_rtpoll_item
);
393 k
->outq_rtpoll_item
= pa_rtpoll_item_new_asyncmsgq(
394 u
->master
->sink
->rtpoll
,
395 PA_RTPOLL_EARLY
+1, /* This one has a slightly lower priority than the normal message handling */
400 /* Called from I/O thread context for the master */
401 static void free_master_rtpolls(struct userdata
*u
) {
404 pa_assert(!u
->master
->outq_rtpoll_item
);
406 for (k
= u
->thread_info
.outputs
; k
; k
= k
->next
) {
411 if (k
->outq_rtpoll_item
) {
412 pa_rtpoll_item_free(k
->outq_rtpoll_item
);
413 k
->outq_rtpoll_item
= NULL
;
416 pa_assert(!k
->outq_rtpoll_item
);
420 /* Called from I/O thread context */
421 static void sink_input_attach_cb(pa_sink_input
*i
) {
424 pa_sink_input_assert_ref(i
);
425 pa_assert_se(o
= i
->userdata
);
427 if (o
->userdata
->thread_info
.master
== o
) {
428 create_master_rtpolls(o
->userdata
);
430 /* Calling these two functions here is safe, because both
431 * threads that might access this sink are known to be
433 pa_sink_set_asyncmsgq(o
->userdata
->sink
, i
->sink
->asyncmsgq
);
434 pa_sink_set_rtpoll(o
->userdata
->sink
, i
->sink
->rtpoll
);
435 pa_sink_attach_within_thread(o
->userdata
->sink
);
438 /* Set up the queues from the inputs to the master */
439 pa_assert(!o
->inq_rtpoll_item
);
440 o
->inq_rtpoll_item
= pa_rtpoll_item_new_asyncmsgq(
442 PA_RTPOLL_NORMAL
, /* This one has a lower priority than the normal message handling */
446 /* Called from I/O thread context */
447 static void sink_input_detach_cb(pa_sink_input
*i
) {
450 pa_sink_input_assert_ref(i
);
451 pa_assert_se(o
= i
->userdata
);
453 pa_log("detaching %s", i
->sink
->name
);
455 pa_assert(o
->inq_rtpoll_item
);
456 pa_rtpoll_item_free(o
->inq_rtpoll_item
);
457 o
->inq_rtpoll_item
= NULL
;
459 if (o
->userdata
->thread_info
.master
== o
) {
460 pa_sink_detach_within_thread(o
->userdata
->sink
);
461 free_master_rtpolls(o
->userdata
);
465 /* Called from main context */
466 static void sink_input_kill_cb(pa_sink_input
*i
) {
469 pa_sink_input_assert_ref(i
);
473 pa_sink_input_unlink(o
->sink_input
);
474 pa_sink_input_unref(o
->sink_input
);
475 o
->sink_input
= NULL
;
477 pa_module_unload_request(o
->userdata
->module
);
480 /* Called from thread context */
481 static int sink_input_process_msg(pa_msgobject
*obj
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
482 struct output
*o
= PA_SINK_INPUT(obj
)->userdata
;
486 case PA_SINK_INPUT_MESSAGE_GET_LATENCY
: {
489 *r
= pa_bytes_to_usec(pa_memblockq_get_length(o
->memblockq
), &o
->sink_input
->sample_spec
);
491 /* Fall through, the default handler will add in the extra
492 * latency added by the resampler */
496 case SINK_INPUT_MESSAGE_POST
: {
498 if (PA_SINK_OPENED(o
->sink_input
->sink
->thread_info
.state
))
499 pa_memblockq_push_align(o
->memblockq
, chunk
);
501 pa_memblockq_flush(o
->memblockq
);
507 return pa_sink_input_process_msg(obj
, code
, data
, offset
, chunk
);
510 /* Called from main context */
511 static int suspend(struct userdata
*u
) {
517 /* Let's suspend by unlinking all streams */
519 for (o
= pa_idxset_first(u
->outputs
, &idx
); o
; o
= pa_idxset_next(u
->outputs
, &idx
)) {
522 pa_sink_input_unlink(o
->sink_input
);
523 pa_sink_input_unref(o
->sink_input
);
524 o
->sink_input
= NULL
;
528 if (pick_master(u
, NULL
) < 0)
529 pa_module_unload_request(u
->module
);
531 pa_log_info("Device suspended...");
536 /* Called from main context */
537 static int unsuspend(struct userdata
*u
) {
545 for (o
= pa_idxset_first(u
->outputs
, &idx
); o
; o
= pa_idxset_next(u
->outputs
, &idx
)) {
547 pa_sink_suspend(o
->sink
, 0);
549 if (PA_SINK_OPENED(pa_sink_get_state(o
->sink
))) {
550 if (output_create_sink_input(u
, o
) < 0)
555 if (pick_master(u
, NULL
) < 0)
556 pa_module_unload_request(u
->module
);
558 for (o
= pa_idxset_first(u
->outputs
, &idx
); o
; o
= pa_idxset_next(u
->outputs
, &idx
))
559 if (o
->sink_input
&& pa_sink_get_state(o
->sink_input
) == PA_SINK_INPUT_INIT
)
560 pa_sink_input_put(o
->sink_input
);
562 pa_log_info("Resumed successfully...");
566 /* Called from main context */
567 static int sink_set_state(pa_sink
*sink
, pa_sink_state_t state
) {
570 pa_sink_assert_ref(sink
);
571 pa_assert_se(u
= sink
->userdata
);
573 /* Please note that in contrast to the ALSA modules we call
574 * suspend/unsuspend from main context here! */
577 case PA_SINK_SUSPENDED
:
578 pa_assert(PA_SINK_OPENED(pa_sink_get_state(u
->sink
)));
586 case PA_SINK_RUNNING
:
588 if (pa_sink_get_state(u
->sink
) == PA_SINK_SUSPENDED
) {
589 if (unsuspend(u
) < 0)
595 case PA_SINK_UNLINKED
:
603 /* Called from thread context of the master */
604 static int sink_process_msg(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
605 struct userdata
*u
= PA_SINK(o
)->userdata
;
609 case PA_SINK_MESSAGE_SET_STATE
:
611 if ((pa_sink_state_t
) PA_PTR_TO_UINT(data
) == PA_SINK_RUNNING
) {
612 /* Only useful when running in NULL mode, i.e. when no
613 * master sink is attached */
614 pa_rtclock_get(&u
->timestamp
);
619 case PA_SINK_MESSAGE_GET_LATENCY
: {
622 /* This code will only be called when running in NULL
623 * mode, i.e. when no master sink is attached. See
624 * sink_get_latency_cb() below */
625 pa_rtclock_get(&now
);
627 if (pa_timespec_cmp(&u
->timestamp
, &now
) > 0)
628 *((pa_usec_t
*) data
) = 0;
630 *((pa_usec_t
*) data
) = pa_timespec_diff(&u
->timestamp
, &now
);
634 case SINK_MESSAGE_SET_MASTER
:
636 if (u
->thread_info
.master
&& data
!= u
->thread_info
.master
) {
638 if (u
->thread_info
.master
->sink_input
->thread_info
.attached
)
639 free_master_rtpolls(u
);
643 if ((u
->thread_info
.master
= data
)) {
645 /* There's now a master, and we're being executed in
646 * its thread, let's register the asyncmsgqs from other
649 if (u
->thread_info
.master
->sink_input
->thread_info
.attached
)
650 create_master_rtpolls(u
);
656 case SINK_MESSAGE_ADD_OUTPUT
: {
657 struct output
*op
= data
;
659 PA_LLIST_PREPEND(struct output
, u
->thread_info
.outputs
, op
);
661 pa_assert(!op
->outq_rtpoll_item
);
663 if (op
!= u
->thread_info
.master
) {
664 /* Create pa_asyncmsgq to master */
666 op
->outq_rtpoll_item
= pa_rtpoll_item_new_asyncmsgq(
667 u
->thread_info
.master
->sink
->rtpoll
,
668 PA_RTPOLL_EARLY
+1, /* This one has a slightly lower priority than the normal message handling */
675 case SINK_MESSAGE_REMOVE_OUTPUT
: {
676 struct output
*op
= data
;
678 PA_LLIST_REMOVE(struct output
, u
->thread_info
.outputs
, op
);
680 /* Remove the q that leads from this output to the master output */
682 if (op
->outq_rtpoll_item
) {
683 pa_rtpoll_item_free(op
->outq_rtpoll_item
);
684 op
->outq_rtpoll_item
= NULL
;
690 case SINK_MESSAGE_NEED
:
691 render_memblock(u
, data
, (size_t) offset
);
695 return pa_sink_process_msg(o
, code
, data
, offset
, chunk
);
698 /* Called from main context */
699 static pa_usec_t
sink_get_latency_cb(pa_sink
*s
) {
702 pa_sink_assert_ref(s
);
707 /* If we have a master sink, we just return the latency of it
708 * and add our own buffering on top */
710 if (!u
->master
->sink_input
)
714 pa_sink_input_get_latency(u
->master
->sink_input
) +
715 pa_sink_get_latency(u
->master
->sink_input
->sink
);
720 /* We have no master, hence let's ask our own thread which
721 * implements the NULL sink */
723 if (pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_LATENCY
, &usec
, 0, NULL
) < 0)
730 static void update_description(struct userdata
*u
) {
738 if (pa_idxset_isempty(u
->outputs
)) {
739 pa_sink_set_description(u
->sink
, "Simultaneous output");
743 t
= pa_xstrdup("Simultaneous output to");
745 for (o
= pa_idxset_first(u
->outputs
, &idx
); o
; o
= pa_idxset_next(u
->outputs
, &idx
)) {
749 e
= pa_sprintf_malloc("%s %s", t
, o
->sink
->description
);
752 e
= pa_sprintf_malloc("%s, %s", t
, o
->sink
->description
);
758 pa_sink_set_description(u
->sink
, t
);
762 static int update_master(struct userdata
*u
, struct output
*o
) {
765 /* Make sure everything is detached from the old thread before we move our stuff to a new thread */
766 if (u
->sink
&& PA_SINK_LINKED(pa_sink_get_state(u
->sink
))) {
767 pa_sink_detach(u
->sink
);
768 pa_asyncmsgq_send(u
->sink
->asyncmsgq
, PA_MSGOBJECT(u
->sink
), SINK_MESSAGE_SET_MASTER
, NULL
, 0, NULL
);
770 u
->thread_info
.master
= NULL
;
773 /* If we have a master sink we run our own sink in its thread */
775 pa_assert(o
->sink_input
);
776 pa_assert(PA_SINK_OPENED(pa_sink_get_state(o
->sink
)));
779 /* If we previously were in NULL mode, let's kill the thread */
780 pa_asyncmsgq_send(u
->thread_mq
.inq
, NULL
, PA_MESSAGE_SHUTDOWN
, NULL
, 0, NULL
);
781 pa_thread_free(u
->thread
);
784 pa_assert(u
->rtpoll
);
785 pa_rtpoll_free(u
->rtpoll
);
789 pa_sink_set_asyncmsgq(u
->sink
, o
->sink
->asyncmsgq
);
790 pa_sink_set_rtpoll(u
->sink
, o
->sink
->rtpoll
);
793 pa_log_info("Master sink is now '%s'", o
->sink_input
->sink
->name
);
797 /* We have no master sink, let's create our own thread */
799 pa_sink_set_asyncmsgq(u
->sink
, u
->thread_mq
.inq
);
803 pa_assert(!u
->rtpoll
);
805 u
->rtpoll
= pa_rtpoll_new();
806 pa_rtpoll_item_new_asyncmsgq(u
->rtpoll
, PA_RTPOLL_EARLY
, u
->thread_mq
.inq
);
808 pa_sink_set_rtpoll(u
->sink
, u
->rtpoll
);
810 if (!(u
->thread
= pa_thread_new(thread_func
, u
))) {
811 pa_log("Failed to create thread.");
816 pa_log_info("No suitable master sink found, going to NULL mode\n");
819 /* Now attach everything again */
820 if (u
->sink
&& PA_SINK_LINKED(pa_sink_get_state(u
->sink
))) {
821 pa_asyncmsgq_send(u
->sink
->asyncmsgq
, PA_MSGOBJECT(u
->sink
), SINK_MESSAGE_SET_MASTER
, u
->master
, 0, NULL
);
822 pa_sink_attach(u
->sink
);
824 u
->thread_info
.master
= u
->master
;
829 static int pick_master(struct userdata
*u
, struct output
*except
) {
834 if (u
->master
&& u
->master
!= except
&& u
->master
->sink_input
&& PA_SINK_OPENED(pa_sink_get_state(u
->master
->sink
)))
835 return update_master(u
, u
->master
);
837 for (o
= pa_idxset_first(u
->outputs
, &idx
); o
; o
= pa_idxset_next(u
->outputs
, &idx
))
838 if (o
!= except
&& o
->sink_input
&& PA_SINK_OPENED(pa_sink_get_state(o
->sink
)))
839 return update_master(u
, o
);
841 return update_master(u
, NULL
);
844 static int output_create_sink_input(struct userdata
*u
, struct output
*o
) {
845 pa_sink_input_new_data data
;
849 pa_assert(!o
->sink_input
);
851 t
= pa_sprintf_malloc("Simultaneous output on %s", o
->sink
->description
);
853 pa_sink_input_new_data_init(&data
);
855 data
.driver
= __FILE__
;
857 pa_sink_input_new_data_set_sample_spec(&data
, &u
->sink
->sample_spec
);
858 pa_sink_input_new_data_set_channel_map(&data
, &u
->sink
->channel_map
);
859 data
.module
= u
->module
;
860 data
.resample_method
= u
->resample_method
;
862 o
->sink_input
= pa_sink_input_new(u
->core
, &data
, PA_SINK_INPUT_VARIABLE_RATE
|PA_SINK_INPUT_DONT_MOVE
);
869 o
->sink_input
->parent
.process_msg
= sink_input_process_msg
;
870 o
->sink_input
->peek
= sink_input_peek_cb
;
871 o
->sink_input
->drop
= sink_input_drop_cb
;
872 o
->sink_input
->attach
= sink_input_attach_cb
;
873 o
->sink_input
->detach
= sink_input_detach_cb
;
874 o
->sink_input
->kill
= sink_input_kill_cb
;
875 o
->sink_input
->userdata
= o
;
880 static struct output
*output_new(struct userdata
*u
, pa_sink
*sink
) {
887 o
= pa_xnew(struct output
, 1);
889 o
->inq
= pa_asyncmsgq_new(0);
890 o
->outq
= pa_asyncmsgq_new(0);
891 o
->inq_rtpoll_item
= NULL
;
892 o
->outq_rtpoll_item
= NULL
;
894 o
->sink_input
= NULL
;
895 o
->memblockq
= pa_memblockq_new(
899 pa_frame_size(&u
->sink
->sample_spec
),
905 pa_assert_se(pa_idxset_put(u
->outputs
, o
, NULL
) == 0);
907 update_description(u
);
909 if (u
->sink
&& PA_SINK_LINKED(pa_sink_get_state(u
->sink
)))
910 pa_asyncmsgq_send(u
->sink
->asyncmsgq
, PA_MSGOBJECT(u
->sink
), SINK_MESSAGE_ADD_OUTPUT
, o
, 0, NULL
);
912 PA_LLIST_PREPEND(struct output
, u
->thread_info
.outputs
, o
);
914 if (PA_SINK_OPENED(pa_sink_get_state(u
->sink
)) || pa_sink_get_state(u
->sink
) == PA_SINK_INIT
) {
915 pa_sink_suspend(sink
, 0);
917 if (PA_SINK_OPENED(pa_sink_get_state(sink
)))
918 if (output_create_sink_input(u
, o
) < 0)
928 pa_sink_input_unlink(o
->sink_input
);
929 pa_sink_input_unref(o
->sink_input
);
933 pa_memblockq_free(o
->memblockq
);
936 pa_asyncmsgq_unref(o
->inq
);
939 pa_asyncmsgq_unref(o
->outq
);
947 static pa_hook_result_t
sink_new_hook_cb(pa_core
*c
, pa_sink
*s
, struct userdata
* u
) {
950 pa_core_assert_ref(c
);
951 pa_sink_assert_ref(s
);
953 pa_assert(u
->automatic
);
955 if (!(s
->flags
& PA_SINK_HARDWARE
) || s
== u
->sink
)
958 pa_log_info("Configuring new sink: %s", s
->name
);
960 if (!(o
= output_new(u
, s
))) {
961 pa_log("Failed to create sink input on sink '%s'.", s
->name
);
965 if (pick_master(u
, NULL
) < 0)
966 pa_module_unload_request(u
->module
);
969 pa_sink_input_put(o
->sink_input
);
974 static pa_hook_result_t
sink_unlink_hook_cb(pa_core
*c
, pa_sink
*s
, struct userdata
* u
) {
979 pa_sink_assert_ref(s
);
985 for (o
= pa_idxset_first(u
->outputs
, &idx
); o
; o
= pa_idxset_next(u
->outputs
, &idx
))
992 pa_log_info("Unconfiguring sink: %s", s
->name
);
999 static pa_hook_result_t
sink_state_changed_hook_cb(pa_core
*c
, pa_sink
*s
, struct userdata
* u
) {
1002 pa_sink_state_t state
;
1007 for (o
= pa_idxset_first(u
->outputs
, &idx
); o
; o
= pa_idxset_next(u
->outputs
, &idx
))
1014 state
= pa_sink_get_state(s
);
1016 if (PA_SINK_OPENED(state
) && PA_SINK_OPENED(pa_sink_get_state(u
->sink
)) && !o
->sink_input
) {
1017 output_create_sink_input(u
, o
);
1019 if (pick_master(u
, NULL
) < 0)
1020 pa_module_unload_request(u
->module
);
1023 pa_sink_input_put(o
->sink_input
);
1026 if (state
== PA_SINK_SUSPENDED
&& o
->sink_input
) {
1027 pa_sink_input_unlink(o
->sink_input
);
1028 pa_sink_input_unref(o
->sink_input
);
1029 o
->sink_input
= NULL
;
1031 pa_memblockq_flush(o
->memblockq
);
1033 if (pick_master(u
, o
) < 0)
1034 pa_module_unload_request(u
->module
);
1040 int pa__init(pa_module
*m
) {
1042 pa_modargs
*ma
= NULL
;
1043 const char *master_name
, *slaves
, *rm
;
1044 pa_sink
*master_sink
= NULL
;
1045 int resample_method
= PA_RESAMPLER_TRIVIAL
;
1053 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
1054 pa_log("failed to parse module arguments");
1058 if ((rm
= pa_modargs_get_value(ma
, "resample_method", NULL
))) {
1059 if ((resample_method
= pa_parse_resample_method(rm
)) < 0) {
1060 pa_log("invalid resample method '%s'", rm
);
1065 u
= pa_xnew(struct userdata
, 1);
1070 u
->thread_info
.master
= u
->master
= NULL
;
1071 u
->time_event
= NULL
;
1072 u
->adjust_time
= DEFAULT_ADJUST_TIME
;
1073 pa_thread_mq_init(&u
->thread_mq
, m
->core
->mainloop
);
1076 PA_LLIST_HEAD_INIT(struct output
, u
->thread_info
.outputs
);
1077 u
->resample_method
= resample_method
;
1078 u
->outputs
= pa_idxset_new(NULL
, NULL
);
1079 pa_timespec_reset(&u
->adjust_timestamp
);
1081 if (pa_modargs_get_value_u32(ma
, "adjust_time", &u
->adjust_time
) < 0) {
1082 pa_log("Failed to parse adjust_time value");
1086 master_name
= pa_modargs_get_value(ma
, "master", NULL
);
1087 slaves
= pa_modargs_get_value(ma
, "slaves", NULL
);
1088 if (!master_name
!= !slaves
) {
1089 pa_log("No master or slave sinks specified");
1094 if (!(master_sink
= pa_namereg_get(m
->core
, master_name
, PA_NAMEREG_SINK
, 1))) {
1095 pa_log("Invalid master sink '%s'", master_name
);
1099 ss
= master_sink
->sample_spec
;
1103 ss
= m
->core
->default_sample_spec
;
1107 if ((pa_modargs_get_sample_spec(ma
, &ss
) < 0)) {
1108 pa_log("Invalid sample specification.");
1112 if (master_sink
&& ss
.channels
== master_sink
->sample_spec
.channels
)
1113 map
= master_sink
->channel_map
;
1115 pa_channel_map_init_auto(&map
, ss
.channels
, PA_CHANNEL_MAP_DEFAULT
);
1117 if ((pa_modargs_get_channel_map(ma
, NULL
, &map
) < 0)) {
1118 pa_log("Invalid channel map.");
1122 if (ss
.channels
!= map
.channels
) {
1123 pa_log("Channel map and sample specification don't match.");
1127 if (!(u
->sink
= pa_sink_new(m
->core
, __FILE__
, pa_modargs_get_value(ma
, "sink_name", DEFAULT_SINK_NAME
), 0, &ss
, &map
))) {
1128 pa_log("Failed to create sink");
1132 u
->sink
->parent
.process_msg
= sink_process_msg
;
1133 u
->sink
->get_latency
= sink_get_latency_cb
;
1134 u
->sink
->set_state
= sink_set_state
;
1135 u
->sink
->userdata
= u
;
1137 u
->sink
->flags
= PA_SINK_CAN_SUSPEND
|PA_SINK_LATENCY
;
1138 pa_sink_set_module(u
->sink
, m
);
1139 pa_sink_set_description(u
->sink
, "Simultaneous output");
1141 u
->block_size
= pa_bytes_per_second(&ss
) / 20; /* 50 ms */
1142 if (u
->block_size
<= 0)
1143 u
->block_size
= pa_frame_size(&ss
);
1145 if (!u
->automatic
) {
1146 const char*split_state
;
1150 /* The master and slaves have been specified manually */
1152 if (!(u
->master
= output_new(u
, master_sink
))) {
1153 pa_log("Failed to create master sink input on sink '%s'.", master_sink
->name
);
1158 while ((n
= pa_split(slaves
, ",", &split_state
))) {
1159 pa_sink
*slave_sink
;
1161 if (!(slave_sink
= pa_namereg_get(m
->core
, n
, PA_NAMEREG_SINK
, 1)) || slave_sink
== u
->sink
) {
1162 pa_log("Invalid slave sink '%s'", n
);
1169 if (!output_new(u
, slave_sink
)) {
1170 pa_log("Failed to create slave sink input on sink '%s'.", slave_sink
->name
);
1175 if (pa_idxset_size(u
->outputs
) <= 1)
1176 pa_log_warn("No slave sinks specified.");
1178 u
->sink_new_slot
= NULL
;
1183 /* We're in automatic mode, we elect one hw sink to the master
1184 * and attach all other hw sinks as slaves to it */
1186 for (s
= pa_idxset_first(m
->core
->sinks
, &idx
); s
; s
= pa_idxset_next(m
->core
->sinks
, &idx
)) {
1188 if (!(s
->flags
& PA_SINK_HARDWARE
) || s
== u
->sink
)
1191 if (!output_new(u
, s
)) {
1192 pa_log("Failed to create sink input on sink '%s'.", s
->name
);
1197 u
->sink_new_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SINK_NEW_POST
], (pa_hook_cb_t
) sink_new_hook_cb
, u
);
1200 u
->sink_unlink_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SINK_UNLINK
], (pa_hook_cb_t
) sink_unlink_hook_cb
, u
);
1201 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
);
1203 if (pick_master(u
, NULL
) < 0)
1206 /* Activate the sink and the sink inputs */
1207 pa_sink_put(u
->sink
);
1209 for (o
= pa_idxset_first(u
->outputs
, &idx
); o
; o
= pa_idxset_next(u
->outputs
, &idx
))
1211 pa_sink_input_put(o
->sink_input
);
1213 if (u
->adjust_time
> 0) {
1215 pa_gettimeofday(&tv
);
1216 tv
.tv_sec
+= u
->adjust_time
;
1217 u
->time_event
= m
->core
->mainloop
->time_new(m
->core
->mainloop
, &tv
, time_callback
, u
);
1220 pa_modargs_free(ma
);
1227 pa_modargs_free(ma
);
1234 static void output_free(struct output
*o
) {
1237 /* Make sure the master points to a different output */
1238 if (pick_master(o
->userdata
, o
) < 0)
1239 pa_module_unload_request(o
->userdata
->module
);
1241 if (o
->userdata
->sink
&& PA_SINK_LINKED(pa_sink_get_state(o
->userdata
->sink
)))
1242 pa_asyncmsgq_send(o
->userdata
->sink
->asyncmsgq
, PA_MSGOBJECT(o
->userdata
->sink
), SINK_MESSAGE_REMOVE_OUTPUT
, o
, 0, NULL
);
1244 PA_LLIST_REMOVE(struct output
, o
->userdata
->thread_info
.outputs
, o
);
1246 pa_assert_se(pa_idxset_remove_by_data(o
->userdata
->outputs
, o
, NULL
));
1248 if (o
->sink_input
) {
1249 pa_sink_input_unlink(o
->sink_input
);
1250 pa_sink_input_unref(o
->sink_input
);
1253 update_description(o
->userdata
);
1255 if (o
->inq_rtpoll_item
)
1256 pa_rtpoll_item_free(o
->inq_rtpoll_item
);
1258 if (o
->outq_rtpoll_item
)
1259 pa_rtpoll_item_free(o
->outq_rtpoll_item
);
1262 pa_asyncmsgq_unref(o
->inq
);
1265 pa_asyncmsgq_unref(o
->outq
);
1268 pa_memblockq_free(o
->memblockq
);
1273 void pa__done(pa_module
*m
) {
1279 if (!(u
= m
->userdata
))
1282 if (u
->sink_new_slot
)
1283 pa_hook_slot_free(u
->sink_new_slot
);
1285 if (u
->sink_unlink_slot
)
1286 pa_hook_slot_free(u
->sink_unlink_slot
);
1288 if (u
->sink_state_changed_slot
)
1289 pa_hook_slot_free(u
->sink_state_changed_slot
);
1292 pa_sink_unlink(u
->sink
);
1295 while ((o
= pa_idxset_first(u
->outputs
, NULL
)))
1298 pa_idxset_free(u
->outputs
, NULL
, NULL
);
1302 pa_asyncmsgq_send(u
->thread_mq
.inq
, NULL
, PA_MESSAGE_SHUTDOWN
, NULL
, 0, NULL
);
1303 pa_thread_free(u
->thread
);
1306 pa_thread_mq_done(&u
->thread_mq
);
1309 pa_sink_unref(u
->sink
);
1312 pa_rtpoll_free(u
->rtpoll
);
1315 u
->core
->mainloop
->time_free(u
->time_event
);