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
*asyncmsgq
;
91 pa_rtpoll_item
*rtpoll_item
;
93 pa_memblockq
*memblockq
;
95 pa_usec_t total_latency
;
97 PA_LLIST_FIELDS(struct output
);
106 pa_thread_mq thread_mq
;
111 struct output
*master
;
113 pa_time_event
*time_event
;
114 uint32_t adjust_time
;
119 struct timespec timestamp
;
121 pa_hook_slot
*sink_new_slot
, *sink_unlink_slot
, *sink_state_changed_slot
;
123 pa_resample_method_t resample_method
;
125 struct timespec adjust_timestamp
;
127 pa_idxset
* outputs
; /* managed in main context */
130 PA_LLIST_HEAD(struct output
, outputs
); /* managed in IO thread context */
131 struct output
*master
;
136 SINK_MESSAGE_ADD_OUTPUT
= PA_SINK_MESSAGE_MAX
,
137 SINK_MESSAGE_REMOVE_OUTPUT
141 SINK_INPUT_MESSAGE_POST
= PA_SINK_INPUT_MESSAGE_MAX
144 static void output_free(struct output
*o
);
145 static int output_create_sink_input(struct userdata
*u
, struct output
*o
);
146 static int update_master(struct userdata
*u
, struct output
*o
);
147 static int pick_master(struct userdata
*u
);
149 static void adjust_rates(struct userdata
*u
) {
151 pa_usec_t max_sink_latency
= 0, min_total_latency
= (pa_usec_t
) -1, target_latency
;
156 pa_sink_assert_ref(u
->sink
);
158 if (pa_idxset_size(u
->outputs
) <= 0)
161 if (!PA_SINK_OPENED(pa_sink_get_state(u
->sink
)))
164 for (o
= pa_idxset_first(u
->outputs
, &idx
); o
; o
= pa_idxset_next(u
->outputs
, &idx
)) {
165 uint32_t sink_latency
;
167 if (!o
->sink_input
|| !PA_SINK_OPENED(pa_sink_get_state(o
->sink
)))
170 sink_latency
= o
->sink_input
->sink
? pa_sink_get_latency(o
->sink_input
->sink
) : 0;
171 o
->total_latency
= sink_latency
+ pa_sink_input_get_latency(o
->sink_input
);
173 if (sink_latency
> max_sink_latency
)
174 max_sink_latency
= sink_latency
;
176 if (o
->total_latency
< min_total_latency
)
177 min_total_latency
= o
->total_latency
;
180 if (min_total_latency
== (pa_usec_t
) -1)
183 target_latency
= max_sink_latency
> min_total_latency
? max_sink_latency
: min_total_latency
;
185 pa_log_info("[%s] target latency is %0.0f usec.", u
->sink
->name
, (float) target_latency
);
186 pa_log_info("[%s] master is %s", u
->sink
->name
, u
->master
->sink
->description
);
188 base_rate
= u
->sink
->sample_spec
.rate
;
190 for (o
= pa_idxset_first(u
->outputs
, &idx
); o
; o
= pa_idxset_next(u
->outputs
, &idx
)) {
191 uint32_t r
= base_rate
;
193 if (!o
->sink_input
|| !PA_SINK_OPENED(pa_sink_get_state(o
->sink
)))
196 if (o
->total_latency
< target_latency
)
197 r
-= (uint32_t) (((((double) target_latency
- o
->total_latency
))/u
->adjust_time
)*r
/ 1000000);
198 else if (o
->total_latency
> target_latency
)
199 r
+= (uint32_t) (((((double) o
->total_latency
- target_latency
))/u
->adjust_time
)*r
/ 1000000);
201 if (r
< (uint32_t) (base_rate
*0.9) || r
> (uint32_t) (base_rate
*1.1)) {
202 pa_log_warn("[%s] sample rates too different, not adjusting (%u vs. %u).", o
->sink_input
->name
, base_rate
, r
);
203 pa_sink_input_set_rate(o
->sink_input
, base_rate
);
205 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
);
206 pa_sink_input_set_rate(o
->sink_input
, r
);
211 static void time_callback(pa_mainloop_api
*a
, pa_time_event
* e
, const struct timeval
*tv
, void *userdata
) {
212 struct userdata
*u
= userdata
;
217 pa_assert(u
->time_event
== e
);
222 n
.tv_sec
+= u
->adjust_time
;
223 u
->sink
->core
->mainloop
->time_restart(e
, &n
);
226 static void thread_func(void *userdata
) {
227 struct userdata
*u
= userdata
;
231 pa_log_debug("Thread starting up");
233 pa_thread_mq_install(&u
->thread_mq
);
234 pa_rtpoll_install(u
->rtpoll
);
236 pa_rtclock_get(&u
->timestamp
);
238 /* This is only run when we are in NULL mode, to make sure that
239 * playback doesn't stop. In all other cases we hook our stuff
240 * into the master sink. */
245 /* Render some data and drop it immediately */
246 if (u
->sink
->thread_info
.state
== PA_SINK_RUNNING
) {
249 pa_rtclock_get(&now
);
251 if (pa_timespec_cmp(&u
->timestamp
, &now
) <= 0) {
252 pa_sink_skip(u
->sink
, u
->block_size
);
253 pa_timespec_add(&u
->timestamp
, pa_bytes_to_usec(u
->block_size
, &u
->sink
->sample_spec
));
256 pa_rtpoll_set_timer_absolute(u
->rtpoll
, &u
->timestamp
);
258 pa_rtpoll_set_timer_disabled(u
->rtpoll
);
260 /* Hmm, nothing to do. Let's sleep */
261 if ((ret
= pa_rtpoll_run(u
->rtpoll
, 1)) < 0)
269 /* If this was no regular exit from the loop we have to continue
270 * processing messages until we received PA_MESSAGE_SHUTDOWN */
271 pa_asyncmsgq_post(u
->thread_mq
.outq
, PA_MSGOBJECT(u
->core
), PA_CORE_MESSAGE_UNLOAD_MODULE
, u
->module
, 0, NULL
, NULL
);
272 pa_asyncmsgq_wait_for(u
->thread_mq
.inq
, PA_MESSAGE_SHUTDOWN
);
275 pa_log_debug("Thread shutting down");
278 static void request_memblock(struct output
*o
, size_t length
) {
282 pa_sink_input_assert_ref(o
->sink_input
);
283 pa_sink_assert_ref(o
->userdata
->sink
);
285 /* If another thread already prepared some data we received
286 * the data over the asyncmsgq, hence let's first process
288 while (pa_asyncmsgq_process_one(o
->asyncmsgq
) > 0)
291 /* Check whether we're now readable */
292 if (pa_memblockq_is_readable(o
->memblockq
))
295 /* OK, we need to prepare new data */
296 pa_mutex_lock(o
->userdata
->mutex
);
298 if (PA_SINK_OPENED(o
->userdata
->sink
->thread_info
.state
)) {
300 /* Maybe there's some data now? */
301 while (pa_asyncmsgq_process_one(o
->asyncmsgq
) > 0)
304 /* Ok, now let's prepare some data if we really have to */
305 while (!pa_memblockq_is_readable(o
->memblockq
)) {
309 pa_sink_render(o
->userdata
->sink
, length
, &chunk
);
311 /* OK, let's send this data to the other threads */
312 for (j
= o
->userdata
->thread_info
.outputs
; j
; j
= j
->next
)
313 if (j
!= o
&& j
->sink_input
)
314 pa_asyncmsgq_post(j
->asyncmsgq
, PA_MSGOBJECT(j
->sink_input
), SINK_INPUT_MESSAGE_POST
, NULL
, 0, &chunk
, NULL
);
316 /* And push it into our own queue */
317 pa_memblockq_push_align(o
->memblockq
, &chunk
);
318 pa_memblock_unref(chunk
.memblock
);
322 pa_mutex_unlock(o
->userdata
->mutex
);
325 /* Called from I/O thread context */
326 static int sink_input_peek_cb(pa_sink_input
*i
, size_t length
, pa_memchunk
*chunk
) {
329 pa_sink_input_assert_ref(i
);
333 /* If necessary, get some new data */
334 request_memblock(o
, length
);
336 return pa_memblockq_peek(o
->memblockq
, chunk
);
339 /* Called from I/O thread context */
340 static void sink_input_drop_cb(pa_sink_input
*i
, size_t length
) {
343 pa_sink_input_assert_ref(i
);
344 pa_assert(length
> 0);
348 pa_memblockq_drop(o
->memblockq
, length
);
351 /* Called from I/O thread context */
352 static void sink_input_attach_cb(pa_sink_input
*i
) {
355 pa_sink_input_assert_ref(i
);
359 if (o
->userdata
->master
== o
) {
360 /* Calling these two functions here is safe, because both
361 * threads that might access this sink input are known to be
363 pa_sink_set_asyncmsgq(o
->userdata
->sink
, i
->sink
->asyncmsgq
);
364 pa_sink_set_rtpoll(o
->userdata
->sink
, i
->sink
->rtpoll
);
365 pa_sink_attach_within_thread(o
->userdata
->sink
);
368 pa_assert(!o
->rtpoll_item
);
369 o
->rtpoll_item
= pa_rtpoll_item_new_asyncmsgq(
371 PA_RTPOLL_NORMAL
, /* This one has a lower priority than the normal message handling */
375 /* Called from I/O thread context */
376 static void sink_input_detach_cb(pa_sink_input
*i
) {
379 pa_sink_input_assert_ref(i
);
383 pa_assert(o
->rtpoll_item
);
384 pa_rtpoll_item_free(o
->rtpoll_item
);
385 o
->rtpoll_item
= NULL
;
387 if (o
->userdata
->master
== o
)
388 pa_sink_detach_within_thread(o
->userdata
->sink
);
391 /* Called from main context */
392 static void sink_input_kill_cb(pa_sink_input
*i
) {
395 pa_sink_input_assert_ref(i
);
399 pa_sink_input_unlink(o
->sink_input
);
400 pa_sink_input_unref(o
->sink_input
);
401 o
->sink_input
= NULL
;
403 pa_module_unload_request(o
->userdata
->module
);
406 /* Called from thread context */
407 static int sink_input_process_msg(pa_msgobject
*obj
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
408 struct output
*o
= PA_SINK_INPUT(obj
)->userdata
;
412 case PA_SINK_INPUT_MESSAGE_GET_LATENCY
: {
415 *r
= pa_bytes_to_usec(pa_memblockq_get_length(o
->memblockq
), &o
->sink_input
->sample_spec
);
417 /* Fall through, the default handler will add in the extra
418 * latency added by the resampler */
422 case SINK_INPUT_MESSAGE_POST
: {
424 if (PA_SINK_OPENED(o
->sink_input
->sink
->thread_info
.state
))
425 pa_memblockq_push_align(o
->memblockq
, chunk
);
427 pa_memblockq_flush(o
->memblockq
);
433 return pa_sink_input_process_msg(obj
, code
, data
, offset
, chunk
);
436 static int suspend(struct userdata
*u
) {
442 /* Let's suspend by unlinking all streams */
444 for (o
= pa_idxset_first(u
->outputs
, &idx
); o
; o
= pa_idxset_next(u
->outputs
, &idx
)) {
447 pa_sink_input_unlink(o
->sink_input
);
448 pa_sink_input_unref(o
->sink_input
);
449 o
->sink_input
= NULL
;
453 if (pick_master(u
) < 0)
454 pa_module_unload_request(u
->module
);
456 pa_log_info("Device suspended...");
461 static int unsuspend(struct userdata
*u
) {
469 for (o
= pa_idxset_first(u
->outputs
, &idx
); o
; o
= pa_idxset_next(u
->outputs
, &idx
)) {
471 pa_sink_suspend(o
->sink
, 0);
473 if (PA_SINK_OPENED(pa_sink_get_state(o
->sink
))) {
474 if (output_create_sink_input(u
, o
) < 0)
477 pa_sink_input_put(o
->sink_input
);
481 if (pick_master(u
) < 0)
482 pa_module_unload_request(u
->module
);
484 pa_log_info("Resumed successfully...");
488 static int sink_set_state(pa_sink
*sink
, pa_sink_state_t state
) {
491 pa_sink_assert_ref(sink
);
495 /* Please note that in contrast to the ALSA modules we call
496 * suspend/unsuspend from main context here! */
499 case PA_SINK_SUSPENDED
:
500 pa_assert(PA_SINK_OPENED(pa_sink_get_state(u
->sink
)));
508 case PA_SINK_RUNNING
:
510 if (pa_sink_get_state(u
->sink
) == PA_SINK_SUSPENDED
) {
511 if (unsuspend(u
) < 0)
517 case PA_SINK_UNLINKED
:
525 /* Called from thread context of the master */
526 static int sink_process_msg(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
527 struct userdata
*u
= PA_SINK(o
)->userdata
;
531 case PA_SINK_MESSAGE_SET_STATE
:
533 if ((pa_sink_state_t
) PA_PTR_TO_UINT(data
) == PA_SINK_RUNNING
) {
534 /* Only useful when running in NULL mode, i.e. when no
535 * master sink is attached */
536 pa_rtclock_get(&u
->timestamp
);
541 case PA_SINK_MESSAGE_GET_LATENCY
: {
544 /* This code will only be called when running in NULL
545 * mode, i.e. when no master sink is attached. See
546 * sink_get_latency_cb() below */
547 pa_rtclock_get(&now
);
549 if (pa_timespec_cmp(&u
->timestamp
, &now
) > 0)
550 *((pa_usec_t
*) data
) = 0;
552 *((pa_usec_t
*) data
) = pa_timespec_diff(&u
->timestamp
, &now
);
556 case PA_SINK_MESSAGE_DETACH
:
558 /* We're detaching all our input streams artificially, so
559 * that we can drive our sink from a different sink */
561 u
->thread_info
.master
= NULL
;
564 case PA_SINK_MESSAGE_ATTACH
:
566 /* We're attached all our input streams artificially again */
568 u
->thread_info
.master
= data
;
571 case SINK_MESSAGE_ADD_OUTPUT
:
572 PA_LLIST_PREPEND(struct output
, u
->thread_info
.outputs
, (struct output
*) data
);
575 case SINK_MESSAGE_REMOVE_OUTPUT
:
576 PA_LLIST_REMOVE(struct output
, u
->thread_info
.outputs
, (struct output
*) data
);
580 return pa_sink_process_msg(o
, code
, data
, offset
, chunk
);
583 /* Called from main context */
584 static pa_usec_t
sink_get_latency_cb(pa_sink
*s
) {
587 pa_sink_assert_ref(s
);
592 /* If we have a master sink, we just return the latency of it
593 * and add our own buffering on top */
595 if (!u
->master
->sink_input
)
599 pa_sink_input_get_latency(u
->master
->sink_input
) +
600 pa_sink_get_latency(u
->master
->sink_input
->sink
);
605 /* We have no master, hence let's ask our own thread which
606 * implements the NULL sink */
608 if (pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_LATENCY
, &usec
, 0, NULL
) < 0)
615 static void update_description(struct userdata
*u
) {
623 if (pa_idxset_isempty(u
->outputs
)) {
624 pa_sink_set_description(u
->sink
, "Simultaneous output");
628 t
= pa_xstrdup("Simultaneous output to");
630 for (o
= pa_idxset_first(u
->outputs
, &idx
); o
; o
= pa_idxset_next(u
->outputs
, &idx
)) {
634 e
= pa_sprintf_malloc("%s %s", t
, o
->sink
->description
);
637 e
= pa_sprintf_malloc("%s, %s", t
, o
->sink
->description
);
643 pa_sink_set_description(u
->sink
, t
);
647 static int update_master(struct userdata
*u
, struct output
*o
) {
650 /* Make sure everything is detached from the old thread before we move our stuff to a new thread */
651 if (u
->sink
&& PA_SINK_LINKED(pa_sink_get_state(u
->sink
)))
652 pa_sink_detach(u
->sink
);
655 /* If we have a master sink we run our own sink in its thread */
657 pa_assert(o
->sink_input
);
658 pa_assert(PA_SINK_OPENED(pa_sink_get_state(o
->sink
)));
661 /* If we previously were in NULL mode, let's kill the thread */
662 pa_asyncmsgq_send(u
->thread_mq
.inq
, NULL
, PA_MESSAGE_SHUTDOWN
, NULL
, 0, NULL
);
663 pa_thread_free(u
->thread
);
666 pa_assert(u
->rtpoll
);
667 pa_rtpoll_free(u
->rtpoll
);
671 pa_sink_set_asyncmsgq(u
->sink
, o
->sink
->asyncmsgq
);
672 pa_sink_set_rtpoll(u
->sink
, o
->sink
->rtpoll
);
675 pa_log_info("Master sink is now '%s'", o
->sink_input
->sink
->name
);
679 /* We have no master sink, let's create our own thread */
681 pa_sink_set_asyncmsgq(u
->sink
, u
->thread_mq
.inq
);
685 pa_assert(!u
->rtpoll
);
687 u
->rtpoll
= pa_rtpoll_new();
688 pa_rtpoll_item_new_asyncmsgq(u
->rtpoll
, PA_RTPOLL_EARLY
, u
->thread_mq
.inq
);
690 pa_sink_set_rtpoll(u
->sink
, u
->rtpoll
);
692 if (!(u
->thread
= pa_thread_new(thread_func
, u
))) {
693 pa_log("Failed to create thread.");
698 pa_log_info("No suitable master sink found, going to NULL mode\n");
701 /* Now attach everything again */
702 if (u
->sink
&& PA_SINK_LINKED(pa_sink_get_state(u
->sink
)))
703 pa_sink_attach(u
->sink
);
708 static int pick_master(struct userdata
*u
) {
713 if (u
->master
&& u
->master
->sink_input
&& PA_SINK_OPENED(pa_sink_get_state(u
->master
->sink
)))
714 return update_master(u
, u
->master
);
716 for (o
= pa_idxset_first(u
->outputs
, &idx
); o
; o
= pa_idxset_next(u
->outputs
, &idx
))
717 if (o
->sink_input
&& PA_SINK_OPENED(pa_sink_get_state(o
->sink
)))
718 return update_master(u
, o
);
720 return update_master(u
, NULL
);
723 static int output_create_sink_input(struct userdata
*u
, struct output
*o
) {
724 pa_sink_input_new_data data
;
728 pa_assert(!o
->sink_input
);
730 t
= pa_sprintf_malloc("Simultaneous output on %s", o
->sink
->description
);
732 pa_sink_input_new_data_init(&data
);
734 data
.driver
= __FILE__
;
736 pa_sink_input_new_data_set_sample_spec(&data
, &u
->sink
->sample_spec
);
737 pa_sink_input_new_data_set_channel_map(&data
, &u
->sink
->channel_map
);
738 data
.module
= u
->module
;
739 data
.resample_method
= u
->resample_method
;
741 o
->sink_input
= pa_sink_input_new(u
->core
, &data
, PA_SINK_INPUT_VARIABLE_RATE
|PA_SINK_INPUT_DONT_MOVE
);
748 o
->sink_input
->parent
.process_msg
= sink_input_process_msg
;
749 o
->sink_input
->peek
= sink_input_peek_cb
;
750 o
->sink_input
->drop
= sink_input_drop_cb
;
751 o
->sink_input
->attach
= sink_input_attach_cb
;
752 o
->sink_input
->detach
= sink_input_detach_cb
;
753 o
->sink_input
->kill
= sink_input_kill_cb
;
754 o
->sink_input
->userdata
= o
;
759 static struct output
*output_new(struct userdata
*u
, pa_sink
*sink
) {
766 o
= pa_xnew(struct output
, 1);
768 o
->asyncmsgq
= pa_asyncmsgq_new(0);
769 o
->rtpoll_item
= NULL
;
771 o
->sink_input
= NULL
;
772 o
->memblockq
= pa_memblockq_new(
776 pa_frame_size(&u
->sink
->sample_spec
),
782 pa_assert_se(pa_idxset_put(u
->outputs
, o
, NULL
) == 0);
784 update_description(u
);
786 if (u
->sink
&& PA_SINK_LINKED(pa_sink_get_state(u
->sink
)))
787 pa_asyncmsgq_send(u
->sink
->asyncmsgq
, PA_MSGOBJECT(u
->sink
), SINK_MESSAGE_ADD_OUTPUT
, o
, 0, NULL
);
789 PA_LLIST_PREPEND(struct output
, u
->thread_info
.outputs
, o
);
791 if (PA_SINK_OPENED(pa_sink_get_state(u
->sink
)) || pa_sink_get_state(u
->sink
) == PA_SINK_INIT
) {
792 pa_sink_suspend(sink
, 0);
794 if (PA_SINK_OPENED(pa_sink_get_state(sink
)))
795 if (output_create_sink_input(u
, o
) < 0)
805 pa_sink_input_unlink(o
->sink_input
);
806 pa_sink_input_unref(o
->sink_input
);
810 pa_memblockq_free(o
->memblockq
);
813 pa_asyncmsgq_unref(o
->asyncmsgq
);
821 static pa_hook_result_t
sink_new_hook_cb(pa_core
*c
, pa_sink
*s
, struct userdata
* u
) {
824 pa_core_assert_ref(c
);
825 pa_sink_assert_ref(s
);
827 pa_assert(u
->automatic
);
829 if (!(s
->flags
& PA_SINK_HARDWARE
) || s
== u
->sink
)
832 pa_log_info("Configuring new sink: %s", s
->name
);
834 if (!(o
= output_new(u
, s
))) {
835 pa_log("Failed to create sink input on sink '%s'.", s
->name
);
839 if (pick_master(u
) < 0)
840 pa_module_unload_request(u
->module
);
843 pa_sink_input_put(o
->sink_input
);
848 static pa_hook_result_t
sink_unlink_hook_cb(pa_core
*c
, pa_sink
*s
, struct userdata
* u
) {
853 pa_sink_assert_ref(s
);
859 for (o
= pa_idxset_first(u
->outputs
, &idx
); o
; o
= pa_idxset_next(u
->outputs
, &idx
))
866 pa_log_info("Unconfiguring sink: %s", s
->name
);
870 if (pick_master(u
) < 0)
871 pa_module_unload_request(u
->module
);
876 static pa_hook_result_t
sink_state_changed_hook_cb(pa_core
*c
, pa_sink
*s
, struct userdata
* u
) {
879 pa_sink_state_t state
;
884 for (o
= pa_idxset_first(u
->outputs
, &idx
); o
; o
= pa_idxset_next(u
->outputs
, &idx
))
891 state
= pa_sink_get_state(s
);
893 if (PA_SINK_OPENED(state
) && PA_SINK_OPENED(pa_sink_get_state(u
->sink
)) && !o
->sink_input
) {
894 output_create_sink_input(u
, o
);
896 if (pick_master(u
) < 0)
897 pa_module_unload_request(u
->module
);
900 pa_sink_input_put(o
->sink_input
);
903 if (state
== PA_SINK_SUSPENDED
&& o
->sink_input
) {
904 pa_sink_input_unlink(o
->sink_input
);
905 pa_sink_input_unref(o
->sink_input
);
906 o
->sink_input
= NULL
;
908 pa_memblockq_flush(o
->memblockq
);
910 if (pick_master(u
) < 0)
911 pa_module_unload_request(u
->module
);
917 int pa__init(pa_module
*m
) {
919 pa_modargs
*ma
= NULL
;
920 const char *master_name
, *slaves
, *rm
;
921 pa_sink
*master_sink
= NULL
;
922 int resample_method
= PA_RESAMPLER_TRIVIAL
;
930 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
931 pa_log("failed to parse module arguments");
935 if ((rm
= pa_modargs_get_value(ma
, "resample_method", NULL
))) {
936 if ((resample_method
= pa_parse_resample_method(rm
)) < 0) {
937 pa_log("invalid resample method '%s'", rm
);
942 u
= pa_xnew(struct userdata
, 1);
947 u
->thread_info
.master
= u
->master
= NULL
;
948 u
->time_event
= NULL
;
949 u
->adjust_time
= DEFAULT_ADJUST_TIME
;
950 u
->mutex
= pa_mutex_new(FALSE
, TRUE
);
951 pa_thread_mq_init(&u
->thread_mq
, m
->core
->mainloop
);
954 PA_LLIST_HEAD_INIT(struct output
, u
->thread_info
.outputs
);
955 u
->resample_method
= resample_method
;
956 u
->outputs
= pa_idxset_new(NULL
, NULL
);
957 pa_timespec_reset(&u
->adjust_timestamp
);
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
;
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_channel_map_init_auto(&map
, ss
.channels
, PA_CHANNEL_MAP_DEFAULT
);
995 if ((pa_modargs_get_channel_map(ma
, NULL
, &map
) < 0)) {
996 pa_log("Invalid channel map.");
1000 if (ss
.channels
!= map
.channels
) {
1001 pa_log("Channel map and sample specification don't match.");
1005 if (!(u
->sink
= pa_sink_new(m
->core
, __FILE__
, pa_modargs_get_value(ma
, "sink_name", DEFAULT_SINK_NAME
), 0, &ss
, &map
))) {
1006 pa_log("Failed to create sink");
1010 u
->sink
->parent
.process_msg
= sink_process_msg
;
1011 u
->sink
->get_latency
= sink_get_latency_cb
;
1012 u
->sink
->set_state
= sink_set_state
;
1013 u
->sink
->userdata
= u
;
1015 u
->sink
->flags
= PA_SINK_CAN_SUSPEND
|PA_SINK_LATENCY
;
1016 pa_sink_set_module(u
->sink
, m
);
1017 pa_sink_set_description(u
->sink
, "Simultaneous output");
1019 u
->block_size
= pa_bytes_per_second(&ss
) / 20; /* 50 ms */
1020 if (u
->block_size
<= 0)
1021 u
->block_size
= pa_frame_size(&ss
);
1023 if (!u
->automatic
) {
1024 const char*split_state
;
1028 /* The master and slaves have been specified manually */
1030 if (!(u
->master
= output_new(u
, master_sink
))) {
1031 pa_log("Failed to create master sink input on sink '%s'.", master_sink
->name
);
1036 while ((n
= pa_split(slaves
, ",", &split_state
))) {
1037 pa_sink
*slave_sink
;
1039 if (!(slave_sink
= pa_namereg_get(m
->core
, n
, PA_NAMEREG_SINK
, 1)) || slave_sink
== u
->sink
) {
1040 pa_log("Invalid slave sink '%s'", n
);
1047 if (!output_new(u
, slave_sink
)) {
1048 pa_log("Failed to create slave sink input on sink '%s'.", slave_sink
->name
);
1053 if (pa_idxset_size(u
->outputs
) <= 1)
1054 pa_log_warn("No slave sinks specified.");
1056 u
->sink_new_slot
= NULL
;
1061 /* We're in automatic mode, we elect one hw sink to the master
1062 * and attach all other hw sinks as slaves to it */
1064 for (s
= pa_idxset_first(m
->core
->sinks
, &idx
); s
; s
= pa_idxset_next(m
->core
->sinks
, &idx
)) {
1066 if (!(s
->flags
& PA_SINK_HARDWARE
) || s
== u
->sink
)
1069 if (!output_new(u
, s
)) {
1070 pa_log("Failed to create sink input on sink '%s'.", s
->name
);
1075 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
);
1078 u
->sink_unlink_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SINK_UNLINK
], (pa_hook_cb_t
) sink_unlink_hook_cb
, u
);
1079 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
);
1081 if (pick_master(u
) < 0)
1084 /* Activate the sink and the sink inputs */
1085 pa_sink_put(u
->sink
);
1087 for (o
= pa_idxset_first(u
->outputs
, &idx
); o
; o
= pa_idxset_next(u
->outputs
, &idx
))
1089 pa_sink_input_put(o
->sink_input
);
1091 if (u
->adjust_time
> 0) {
1093 pa_gettimeofday(&tv
);
1094 tv
.tv_sec
+= u
->adjust_time
;
1095 u
->time_event
= m
->core
->mainloop
->time_new(m
->core
->mainloop
, &tv
, time_callback
, u
);
1098 pa_modargs_free(ma
);
1105 pa_modargs_free(ma
);
1112 static void output_free(struct output
*o
) {
1116 if (o
->userdata
->sink
&& PA_SINK_LINKED(pa_sink_get_state(o
->userdata
->sink
)))
1117 pa_asyncmsgq_send(o
->userdata
->sink
->asyncmsgq
, PA_MSGOBJECT(o
->userdata
->sink
), SINK_MESSAGE_REMOVE_OUTPUT
, o
, 0, NULL
);
1119 PA_LLIST_REMOVE(struct output
, o
->userdata
->thread_info
.outputs
, o
);
1122 pa_assert_se(pa_idxset_remove_by_data(o
->userdata
->outputs
, o
, NULL
));
1124 if (o
->userdata
->master
== o
) {
1125 /* Make sure the master points to a different output */
1126 o
->userdata
->master
= NULL
;
1127 pick_master(o
->userdata
);
1130 update_description(o
->userdata
);
1132 if (o
->sink_input
) {
1133 pa_sink_input_unlink(o
->sink_input
);
1134 pa_sink_input_unref(o
->sink_input
);
1138 pa_rtpoll_item_free(o
->rtpoll_item
);
1141 pa_memblockq_free(o
->memblockq
);
1144 pa_asyncmsgq_unref(o
->asyncmsgq
);
1149 void pa__done(pa_module
*m
) {
1155 if (!(u
= m
->userdata
))
1158 if (u
->sink_new_slot
)
1159 pa_hook_slot_free(u
->sink_new_slot
);
1161 if (u
->sink_unlink_slot
)
1162 pa_hook_slot_free(u
->sink_unlink_slot
);
1164 if (u
->sink_state_changed_slot
)
1165 pa_hook_slot_free(u
->sink_state_changed_slot
);
1168 pa_sink_unlink(u
->sink
);
1171 while ((o
= pa_idxset_first(u
->outputs
, NULL
)))
1174 pa_idxset_free(u
->outputs
, NULL
, NULL
);
1178 pa_asyncmsgq_send(u
->thread_mq
.inq
, NULL
, PA_MESSAGE_SHUTDOWN
, NULL
, 0, NULL
);
1179 pa_thread_free(u
->thread
);
1182 pa_thread_mq_done(&u
->thread_mq
);
1185 pa_sink_unref(u
->sink
);
1188 pa_rtpoll_free(u
->rtpoll
);
1191 u
->core
->mainloop
->time_free(u
->time_event
);
1193 pa_mutex_free(u
->mutex
);