2 This file is part of PulseAudio.
4 Copyright 2011 Colin Guthrie
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
26 #include <pulse/timeval.h>
27 #include <pulse/rtclock.h>
28 #include <pulse/xmalloc.h>
30 #include <pulsecore/core.h>
31 #include <pulsecore/core-util.h>
32 #include <pulsecore/i18n.h>
33 #include <pulsecore/macro.h>
34 #include <pulsecore/hashmap.h>
35 #include <pulsecore/hook-list.h>
36 #include <pulsecore/sink-input.h>
37 #include <pulsecore/modargs.h>
38 #include <pulsecore/proplist-util.h>
40 #include "module-filter-apply-symdef.h"
42 #define PA_PROP_FILTER_APPLY_MOVING "filter.apply.moving"
44 PA_MODULE_AUTHOR("Colin Guthrie");
45 PA_MODULE_DESCRIPTION("Load filter sinks automatically when needed");
46 PA_MODULE_VERSION(PACKAGE_VERSION
);
47 PA_MODULE_LOAD_ONCE(true);
48 PA_MODULE_USAGE(_("autoclean=<automatically unload unused filters?>"));
50 static const char* const valid_modargs
[] = {
55 #define DEFAULT_AUTOCLEAN true
56 #define HOUSEKEEPING_INTERVAL (10 * PA_USEC_PER_SEC)
60 uint32_t module_index
;
64 pa_source
*source_master
;
72 *sink_input_move_finish_slot
,
73 *sink_input_proplist_slot
,
74 *sink_input_unlink_slot
,
76 *source_output_put_slot
,
77 *source_output_move_finish_slot
,
78 *source_output_proplist_slot
,
79 *source_output_unlink_slot
,
82 pa_time_event
*housekeeping_time_event
;
85 static unsigned filter_hash(const void *p
) {
86 const struct filter
*f
= p
;
88 if (f
->sink_master
&& !f
->source_master
)
89 return (unsigned) (f
->sink_master
->index
+ pa_idxset_string_hash_func(f
->name
));
90 else if (!f
->sink_master
&& f
->source_master
)
91 return (unsigned) ((f
->source_master
->index
<< 16) + pa_idxset_string_hash_func(f
->name
));
93 return (unsigned) (f
->sink_master
->index
+ (f
->source_master
->index
<< 16) + pa_idxset_string_hash_func(f
->name
));
96 static int filter_compare(const void *a
, const void *b
) {
97 const struct filter
*fa
= a
, *fb
= b
;
100 if (fa
->sink_master
!= fb
->sink_master
|| fa
->source_master
!= fb
->source_master
)
102 if ((r
= strcmp(fa
->name
, fb
->name
)))
108 static struct filter
*filter_new(const char *name
, pa_sink
*sink
, pa_source
*source
) {
111 pa_assert(sink
|| source
);
113 f
= pa_xnew(struct filter
, 1);
114 f
->name
= pa_xstrdup(name
);
115 f
->sink_master
= sink
;
116 f
->source_master
= source
;
117 f
->module_index
= PA_INVALID_INDEX
;
124 static void filter_free(struct filter
*f
) {
131 static const char* should_filter(pa_object
*o
, bool is_sink_input
) {
136 pl
= PA_SINK_INPUT(o
)->proplist
;
138 pl
= PA_SOURCE_OUTPUT(o
)->proplist
;
140 /* If the stream doesn't what any filter, then let it be. */
141 if ((apply
= pa_proplist_gets(pl
, PA_PROP_FILTER_APPLY
)) && !pa_streq(apply
, "")) {
142 const char* suppress
= pa_proplist_gets(pl
, PA_PROP_FILTER_SUPPRESS
);
144 if (!suppress
|| !pa_streq(suppress
, apply
))
151 static bool should_group_filter(struct filter
*filter
) {
152 return pa_streq(filter
->name
, "echo-cancel");
155 static char* get_group(pa_object
*o
, bool is_sink_input
) {
159 pl
= PA_SINK_INPUT(o
)->proplist
;
161 pl
= PA_SOURCE_OUTPUT(o
)->proplist
;
163 /* There's a bit of cleverness here -- the second argument ensures that we
164 * only group streams that require the same filter */
165 return pa_proplist_get_stream_group(pl
, pa_proplist_gets(pl
, PA_PROP_FILTER_APPLY
), NULL
);
168 /* For filters that apply on a source-output/sink-input pair, this finds the
169 * master sink if we know the master source, or vice versa. It does this by
170 * looking up streams that belong to the same stream group as the original
171 * object. The idea is that streams from the sam group are always routed
173 static bool find_paired_master(struct userdata
*u
, struct filter
*filter
, pa_object
*o
, bool is_sink_input
) {
176 if ((group
= get_group(o
, is_sink_input
))) {
179 char *module_name
= pa_sprintf_malloc("module-%s", filter
->name
);
182 pa_source_output
*so
;
184 PA_IDXSET_FOREACH(so
, u
->core
->source_outputs
, idx
) {
185 g
= get_group(PA_OBJECT(so
), false);
187 if (pa_streq(g
, group
)) {
188 if (pa_streq(module_name
, so
->source
->module
->name
)) {
189 /* Make sure we're not routing to another instance of
190 * the same filter. */
191 filter
->source_master
= so
->source
->output_from_master
->source
;
193 filter
->source_master
= so
->source
;
205 PA_IDXSET_FOREACH(si
, u
->core
->sink_inputs
, idx
) {
206 g
= get_group(PA_OBJECT(si
), true);
208 if (pa_streq(g
, group
)) {
209 if (pa_streq(module_name
, si
->sink
->module
->name
)) {
210 /* Make sure we're not routing to another instance of
211 * the same filter. */
212 filter
->sink_master
= si
->sink
->input_to_master
->sink
;
214 filter
->sink_master
= si
->sink
;
226 pa_xfree(module_name
);
228 if (!filter
->sink_master
|| !filter
->source_master
)
235 static bool nothing_attached(struct filter
*f
) {
236 bool no_si
= true, no_so
= true;
239 no_si
= pa_idxset_isempty(f
->sink
->inputs
);
241 no_so
= pa_idxset_isempty(f
->source
->outputs
);
243 return no_si
&& no_so
;
246 static void housekeeping_time_callback(pa_mainloop_api
*a
, pa_time_event
* e
, const struct timeval
*t
, void *userdata
) {
247 struct userdata
*u
= userdata
;
248 struct filter
*filter
;
255 pa_assert(e
== u
->housekeeping_time_event
);
256 u
->core
->mainloop
->time_free(u
->housekeeping_time_event
);
257 u
->housekeeping_time_event
= NULL
;
259 PA_HASHMAP_FOREACH(filter
, u
->filters
, state
) {
260 if (nothing_attached(filter
)) {
263 pa_log_debug("Detected filter %s as no longer used. Unloading.", filter
->name
);
264 idx
= filter
->module_index
;
265 pa_hashmap_remove(u
->filters
, filter
);
267 pa_module_unload_request_by_index(u
->core
, idx
, true);
271 pa_log_info("Housekeeping Done.");
274 static void trigger_housekeeping(struct userdata
*u
) {
280 if (u
->housekeeping_time_event
)
283 u
->housekeeping_time_event
= pa_core_rttime_new(u
->core
, pa_rtclock_now() + HOUSEKEEPING_INTERVAL
, housekeeping_time_callback
, u
);
286 static int do_move(pa_object
*obj
, pa_object
*parent
, bool restore
, bool is_input
) {
288 return pa_sink_input_move_to(PA_SINK_INPUT(obj
), PA_SINK(parent
), restore
);
290 return pa_source_output_move_to(PA_SOURCE_OUTPUT(obj
), PA_SOURCE(parent
), restore
);
293 static void move_object_for_filter(pa_object
*o
, struct filter
* filter
, bool restore
, bool is_sink_input
) {
302 pl
= PA_SINK_INPUT(o
)->proplist
;
303 parent
= PA_OBJECT(restore
? filter
->sink_master
: filter
->sink
);
306 name
= PA_SINK(parent
)->name
;
308 pl
= PA_SOURCE_OUTPUT(o
)->proplist
;
309 parent
= PA_OBJECT(restore
? filter
->source_master
: filter
->source
);
312 name
= PA_SOURCE(parent
)->name
;
315 pa_proplist_sets(pl
, PA_PROP_FILTER_APPLY_MOVING
, "1");
317 if (do_move(o
, parent
, false, is_sink_input
) < 0)
318 pa_log_info("Failed to move %s for \"%s\" to <%s>.", is_sink_input
? "sink-input" : "source-output",
319 pa_strnull(pa_proplist_gets(pl
, PA_PROP_APPLICATION_NAME
)), name
);
321 pa_log_info("Successfully moved %s for \"%s\" to <%s>.", is_sink_input
? "sink-input" : "source-output",
322 pa_strnull(pa_proplist_gets(pl
, PA_PROP_APPLICATION_NAME
)), name
);
324 pa_proplist_unset(pl
, PA_PROP_FILTER_APPLY_MOVING
);
327 static void move_objects_for_filter(struct userdata
*u
, pa_object
*o
, struct filter
* filter
, bool restore
,
328 bool is_sink_input
) {
330 if (!should_group_filter(filter
))
331 move_object_for_filter(o
, filter
, restore
, is_sink_input
);
333 pa_source_output
*so
;
338 group
= get_group(o
, is_sink_input
);
340 PA_IDXSET_FOREACH(so
, u
->core
->source_outputs
, idx
) {
341 g
= get_group(PA_OBJECT(so
), false);
343 if (pa_streq(g
, group
))
344 move_object_for_filter(PA_OBJECT(so
), filter
, restore
, false);
349 PA_IDXSET_FOREACH(si
, u
->core
->sink_inputs
, idx
) {
350 g
= get_group(PA_OBJECT(si
), true);
352 if (pa_streq(g
, group
))
353 move_object_for_filter(PA_OBJECT(si
), filter
, restore
, true);
362 /* Note that we assume a filter will provide at most one sink and at most one
363 * source (and at least one of either). */
364 static void find_filters_for_module(struct userdata
*u
, pa_module
*m
, const char *name
) {
368 struct filter
*fltr
= NULL
;
370 PA_IDXSET_FOREACH(sink
, u
->core
->sinks
, idx
) {
371 if (sink
->module
== m
) {
372 pa_assert(sink
->input_to_master
!= NULL
);
374 fltr
= filter_new(name
, sink
->input_to_master
->sink
, NULL
);
375 fltr
->module_index
= m
->index
;
382 PA_IDXSET_FOREACH(source
, u
->core
->sources
, idx
) {
383 if (source
->module
== m
&& !source
->monitor_of
) {
384 pa_assert(source
->output_from_master
!= NULL
);
387 fltr
= filter_new(name
, NULL
, source
->output_from_master
->source
);
388 fltr
->module_index
= m
->index
;
389 fltr
->source
= source
;
391 fltr
->source
= source
;
392 fltr
->source_master
= source
->output_from_master
->source
;
399 pa_hashmap_put(u
->filters
, fltr
, fltr
);
402 static bool can_unload_module(struct userdata
*u
, uint32_t idx
) {
404 struct filter
*filter
;
406 /* Check if any other struct filters point to the same module */
407 PA_HASHMAP_FOREACH(filter
, u
->filters
, state
) {
408 if (filter
->module_index
== idx
&& !nothing_attached(filter
))
415 static pa_hook_result_t
process(struct userdata
*u
, pa_object
*o
, bool is_sink_input
) {
417 bool done_something
= false;
418 pa_sink
*sink
= NULL
;
419 pa_source
*source
= NULL
;
420 pa_module
*module
= NULL
;
423 sink
= PA_SINK_INPUT(o
)->sink
;
426 module
= sink
->module
;
428 source
= PA_SOURCE_OUTPUT(o
)->source
;
431 module
= source
->module
;
434 /* If there is no sink/source yet, we can't do much */
435 if ((is_sink_input
&& !sink
) || (!is_sink_input
&& !source
))
438 /* If the stream doesn't what any filter, then let it be. */
439 if ((want
= should_filter(o
, is_sink_input
))) {
441 struct filter
*fltr
, *filter
;
443 /* We need to ensure the SI is playing on a sink of this type
444 * attached to the sink it's "officially" playing on */
449 module_name
= pa_sprintf_malloc("module-%s", want
);
450 if (pa_streq(module
->name
, module_name
)) {
451 pa_log_debug("Stream appears to be playing on an appropriate sink already. Ignoring.");
452 pa_xfree(module_name
);
456 fltr
= filter_new(want
, sink
, source
);
458 if (should_group_filter(fltr
) && !find_paired_master(u
, fltr
, o
, is_sink_input
)) {
459 pa_log_debug("Want group filtering but don't have enough streams.");
463 if (!(filter
= pa_hashmap_get(u
->filters
, fltr
))) {
467 args
= pa_sprintf_malloc("autoloaded=1 %s%s %s%s",
468 fltr
->sink_master
? "sink_master=" : "",
469 fltr
->sink_master
? fltr
->sink_master
->name
: "",
470 fltr
->source_master
? "source_master=" : "",
471 fltr
->source_master
? fltr
->source_master
->name
: "");
473 pa_log_debug("Loading %s with arguments '%s'", module_name
, args
);
475 if ((m
= pa_module_load(u
->core
, module_name
, args
))) {
476 find_filters_for_module(u
, m
, want
);
477 filter
= pa_hashmap_get(u
->filters
, fltr
);
478 done_something
= true;
486 pa_log("Unable to load %s", module_name
);
487 pa_xfree(module_name
);
490 pa_xfree(module_name
);
492 /* We can move the stream now as we know the destination. If this
493 * isn't true, we will do it later when the sink appears. */
494 if ((is_sink_input
&& filter
->sink
) || (!is_sink_input
&& filter
->source
)) {
495 move_objects_for_filter(u
, o
, filter
, false, is_sink_input
);
496 done_something
= true;
500 struct filter
*filter
= NULL
;
502 /* We do not want to filter... but are we already filtered?
503 * This can happen if an input's proplist changes */
504 PA_HASHMAP_FOREACH(filter
, u
->filters
, state
) {
505 if ((is_sink_input
&& sink
== filter
->sink
) || (!is_sink_input
&& source
== filter
->source
)) {
506 move_objects_for_filter(u
, o
, filter
, true, is_sink_input
);
507 done_something
= true;
514 trigger_housekeeping(u
);
519 static pa_hook_result_t
sink_input_put_cb(pa_core
*core
, pa_sink_input
*i
, struct userdata
*u
) {
520 pa_core_assert_ref(core
);
521 pa_sink_input_assert_ref(i
);
523 return process(u
, PA_OBJECT(i
), true);
526 static pa_hook_result_t
sink_input_move_finish_cb(pa_core
*core
, pa_sink_input
*i
, struct userdata
*u
) {
527 pa_core_assert_ref(core
);
528 pa_sink_input_assert_ref(i
);
530 if (pa_proplist_gets(i
->proplist
, PA_PROP_FILTER_APPLY_MOVING
))
533 return process(u
, PA_OBJECT(i
), true);
536 static pa_hook_result_t
sink_input_proplist_cb(pa_core
*core
, pa_sink_input
*i
, struct userdata
*u
) {
537 pa_core_assert_ref(core
);
538 pa_sink_input_assert_ref(i
);
540 return process(u
, PA_OBJECT(i
), true);
543 static pa_hook_result_t
sink_input_unlink_cb(pa_core
*core
, pa_sink_input
*i
, struct userdata
*u
) {
544 pa_core_assert_ref(core
);
545 pa_sink_input_assert_ref(i
);
549 if (pa_hashmap_size(u
->filters
) > 0)
550 trigger_housekeeping(u
);
555 static pa_hook_result_t
sink_unlink_cb(pa_core
*core
, pa_sink
*sink
, struct userdata
*u
) {
557 struct filter
*filter
= NULL
;
559 pa_core_assert_ref(core
);
560 pa_sink_assert_ref(sink
);
563 /* If either the parent or the sink we've loaded disappears,
564 * we should remove it from our hashmap */
565 PA_HASHMAP_FOREACH(filter
, u
->filters
, state
) {
566 if (filter
->sink_master
== sink
|| filter
->sink
== sink
) {
569 /* Attempt to rescue any streams to the parent sink as this is likely
570 * the best course of action (as opposed to a generic rescue via
571 * module-rescue-streams */
572 if (filter
->sink
== sink
) {
575 PA_IDXSET_FOREACH(i
, sink
->inputs
, idx
)
576 move_objects_for_filter(u
, PA_OBJECT(i
), filter
, true, true);
579 idx
= filter
->module_index
;
580 pa_hashmap_remove(u
->filters
, filter
);
583 if (can_unload_module(u
, idx
))
584 pa_module_unload_request_by_index(u
->core
, idx
, true);
591 static pa_hook_result_t
source_output_put_cb(pa_core
*core
, pa_source_output
*o
, struct userdata
*u
) {
592 pa_core_assert_ref(core
);
593 pa_source_output_assert_ref(o
);
595 return process(u
, PA_OBJECT(o
), false);
598 static pa_hook_result_t
source_output_move_finish_cb(pa_core
*core
, pa_source_output
*o
, struct userdata
*u
) {
599 pa_core_assert_ref(core
);
600 pa_source_output_assert_ref(o
);
602 if (pa_proplist_gets(o
->proplist
, PA_PROP_FILTER_APPLY_MOVING
))
605 return process(u
, PA_OBJECT(o
), false);
608 static pa_hook_result_t
source_output_proplist_cb(pa_core
*core
, pa_source_output
*o
, struct userdata
*u
) {
609 pa_core_assert_ref(core
);
610 pa_source_output_assert_ref(o
);
612 return process(u
, PA_OBJECT(o
), false);
615 static pa_hook_result_t
source_output_unlink_cb(pa_core
*core
, pa_source_output
*o
, struct userdata
*u
) {
616 pa_core_assert_ref(core
);
617 pa_source_output_assert_ref(o
);
621 if (pa_hashmap_size(u
->filters
) > 0)
622 trigger_housekeeping(u
);
627 static pa_hook_result_t
source_unlink_cb(pa_core
*core
, pa_source
*source
, struct userdata
*u
) {
629 struct filter
*filter
= NULL
;
631 pa_core_assert_ref(core
);
632 pa_source_assert_ref(source
);
635 /* If either the parent or the source we've loaded disappears,
636 * we should remove it from our hashmap */
637 PA_HASHMAP_FOREACH(filter
, u
->filters
, state
) {
638 if (filter
->source_master
== source
|| filter
->source
== source
) {
641 /* Attempt to rescue any streams to the parent source as this is likely
642 * the best course of action (as opposed to a generic rescue via
643 * module-rescue-streams */
644 if (filter
->source
== source
) {
647 PA_IDXSET_FOREACH(o
, source
->outputs
, idx
)
648 move_objects_for_filter(u
, PA_OBJECT(o
), filter
, true, false);
651 idx
= filter
->module_index
;
652 pa_hashmap_remove(u
->filters
, filter
);
655 if (can_unload_module(u
, idx
))
656 pa_module_unload_request_by_index(u
->core
, idx
, true);
663 int pa__init(pa_module
*m
) {
664 pa_modargs
*ma
= NULL
;
669 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
670 pa_log("Failed to parse module arguments");
674 m
->userdata
= u
= pa_xnew0(struct userdata
, 1);
678 u
->autoclean
= DEFAULT_AUTOCLEAN
;
679 if (pa_modargs_get_value_boolean(ma
, "autoclean", &u
->autoclean
) < 0) {
680 pa_log("Failed to parse autoclean value");
684 u
->filters
= pa_hashmap_new(filter_hash
, filter_compare
);
686 u
->sink_input_put_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SINK_INPUT_PUT
], PA_HOOK_LATE
, (pa_hook_cb_t
) sink_input_put_cb
, u
);
687 u
->sink_input_move_finish_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SINK_INPUT_MOVE_FINISH
], PA_HOOK_LATE
, (pa_hook_cb_t
) sink_input_move_finish_cb
, u
);
688 u
->sink_input_proplist_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SINK_INPUT_PROPLIST_CHANGED
], PA_HOOK_LATE
, (pa_hook_cb_t
) sink_input_proplist_cb
, u
);
689 u
->sink_input_unlink_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SINK_INPUT_UNLINK
], PA_HOOK_LATE
, (pa_hook_cb_t
) sink_input_unlink_cb
, u
);
690 u
->sink_unlink_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SINK_UNLINK
], PA_HOOK_LATE
-1, (pa_hook_cb_t
) sink_unlink_cb
, u
);
691 u
->source_output_put_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_PUT
], PA_HOOK_LATE
, (pa_hook_cb_t
) source_output_put_cb
, u
);
692 u
->source_output_move_finish_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_FINISH
], PA_HOOK_LATE
, (pa_hook_cb_t
) source_output_move_finish_cb
, u
);
693 u
->source_output_proplist_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_PROPLIST_CHANGED
], PA_HOOK_LATE
, (pa_hook_cb_t
) source_output_proplist_cb
, u
);
694 u
->source_output_unlink_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_UNLINK
], PA_HOOK_LATE
, (pa_hook_cb_t
) source_output_unlink_cb
, u
);
695 u
->source_unlink_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SOURCE_UNLINK
], PA_HOOK_LATE
-1, (pa_hook_cb_t
) source_unlink_cb
, u
);
710 void pa__done(pa_module
*m
) {
715 if (!(u
= m
->userdata
))
718 if (u
->sink_input_put_slot
)
719 pa_hook_slot_free(u
->sink_input_put_slot
);
720 if (u
->sink_input_move_finish_slot
)
721 pa_hook_slot_free(u
->sink_input_move_finish_slot
);
722 if (u
->sink_input_proplist_slot
)
723 pa_hook_slot_free(u
->sink_input_proplist_slot
);
724 if (u
->sink_input_unlink_slot
)
725 pa_hook_slot_free(u
->sink_input_unlink_slot
);
726 if (u
->sink_unlink_slot
)
727 pa_hook_slot_free(u
->sink_unlink_slot
);
728 if (u
->source_output_put_slot
)
729 pa_hook_slot_free(u
->source_output_put_slot
);
730 if (u
->source_output_move_finish_slot
)
731 pa_hook_slot_free(u
->source_output_move_finish_slot
);
732 if (u
->source_output_proplist_slot
)
733 pa_hook_slot_free(u
->source_output_proplist_slot
);
734 if (u
->source_output_unlink_slot
)
735 pa_hook_slot_free(u
->source_output_unlink_slot
);
736 if (u
->source_unlink_slot
)
737 pa_hook_slot_free(u
->source_unlink_slot
);
739 if (u
->housekeeping_time_event
)
740 u
->core
->mainloop
->time_free(u
->housekeeping_time_event
);
745 while ((f
= pa_hashmap_steal_first(u
->filters
))) {
746 pa_module_unload_request_by_index(u
->core
, f
->module_index
, true);
750 pa_hashmap_free(u
->filters
, NULL
);