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, pa_bool_t 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 pa_bool_t should_group_filter(struct filter *filter) {
152 return pa_streq(filter->name, "echo-cancel");
155 static char* get_group(pa_object *o, pa_bool_t 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 pa_bool_t find_paired_master(struct userdata *u, struct filter *filter, pa_object *o, pa_bool_t 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 pa_bool_t nothing_attached(struct filter *f) {
236 pa_bool_t 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, pa_bool_t restore, pa_bool_t 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, pa_bool_t restore, pa_bool_t 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, pa_bool_t restore,
328 pa_bool_t 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 pa_bool_t 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, pa_bool_t is_sink_input) {
417 pa_bool_t done_something = FALSE;
418 pa_sink *sink = NULL;
419 pa_source *source = NULL;
423 sink = PA_SINK_INPUT(o)->sink;
424 module = sink->module;
426 source = PA_SOURCE_OUTPUT(o)->source;
427 module = source->module;
430 /* If there is no sink/source yet, we can't do much */
431 if ((is_sink_input && !sink) || (!is_sink_input && !source))
434 /* If the stream doesn't what any filter, then let it be. */
435 if ((want = should_filter(o, is_sink_input))) {
437 struct filter *fltr, *filter;
439 /* We need to ensure the SI is playing on a sink of this type
440 * attached to the sink it's "officially" playing on */
445 module_name = pa_sprintf_malloc("module-%s", want);
446 if (pa_streq(module->name, module_name)) {
447 pa_log_debug("Stream appears to be playing on an appropriate sink already. Ignoring.");
448 pa_xfree(module_name);
452 fltr = filter_new(want, sink, source);
454 if (should_group_filter(fltr) && !find_paired_master(u, fltr, o, is_sink_input)) {
455 pa_log_debug("Want group filtering but don't have enough streams.");
459 if (!(filter = pa_hashmap_get(u->filters, fltr))) {
463 args = pa_sprintf_malloc("autoloaded=1 %s%s %s%s",
464 fltr->sink_master ? "sink_master=" : "",
465 fltr->sink_master ? fltr->sink_master->name : "",
466 fltr->source_master ? "source_master=" : "",
467 fltr->source_master ? fltr->source_master->name : "");
469 pa_log_debug("Loading %s with arguments '%s'", module_name, args);
471 if ((m = pa_module_load(u->core, module_name, args))) {
472 find_filters_for_module(u, m, want);
473 filter = pa_hashmap_get(u->filters, fltr);
474 done_something = TRUE;
482 pa_log("Unable to load %s", module_name);
483 pa_xfree(module_name);
486 pa_xfree(module_name);
488 /* We can move the stream now as we know the destination. If this
489 * isn't true, we will do it later when the sink appears. */
490 if ((is_sink_input && filter->sink) || (!is_sink_input && filter->source)) {
491 move_objects_for_filter(u, o, filter, FALSE, is_sink_input);
492 done_something = TRUE;
496 struct filter *filter = NULL;
498 /* We do not want to filter... but are we already filtered?
499 * This can happen if an input's proplist changes */
500 PA_HASHMAP_FOREACH(filter, u->filters, state) {
501 if ((is_sink_input && sink == filter->sink) || (!is_sink_input && source == filter->source)) {
502 move_objects_for_filter(u, o, filter, TRUE, is_sink_input);
503 done_something = TRUE;
510 trigger_housekeeping(u);
515 static pa_hook_result_t sink_input_put_cb(pa_core *core, pa_sink_input *i, struct userdata *u) {
516 pa_core_assert_ref(core);
517 pa_sink_input_assert_ref(i);
519 return process(u, PA_OBJECT(i), TRUE);
522 static pa_hook_result_t sink_input_move_finish_cb(pa_core *core, pa_sink_input *i, struct userdata *u) {
523 pa_core_assert_ref(core);
524 pa_sink_input_assert_ref(i);
526 if (pa_proplist_gets(i->proplist, PA_PROP_FILTER_APPLY_MOVING))
529 return process(u, PA_OBJECT(i), TRUE);
532 static pa_hook_result_t sink_input_proplist_cb(pa_core *core, pa_sink_input *i, struct userdata *u) {
533 pa_core_assert_ref(core);
534 pa_sink_input_assert_ref(i);
536 return process(u, PA_OBJECT(i), TRUE);
539 static pa_hook_result_t sink_input_unlink_cb(pa_core *core, pa_sink_input *i, struct userdata *u) {
540 pa_core_assert_ref(core);
541 pa_sink_input_assert_ref(i);
545 if (pa_hashmap_size(u->filters) > 0)
546 trigger_housekeeping(u);
551 static pa_hook_result_t sink_unlink_cb(pa_core *core, pa_sink *sink, struct userdata *u) {
553 struct filter *filter = NULL;
555 pa_core_assert_ref(core);
556 pa_sink_assert_ref(sink);
559 /* If either the parent or the sink we've loaded disappears,
560 * we should remove it from our hashmap */
561 PA_HASHMAP_FOREACH(filter, u->filters, state) {
562 if (filter->sink_master == sink || filter->sink == sink) {
565 /* Attempt to rescue any streams to the parent sink as this is likely
566 * the best course of action (as opposed to a generic rescue via
567 * module-rescue-streams */
568 if (filter->sink == sink) {
571 PA_IDXSET_FOREACH(i, sink->inputs, idx)
572 move_objects_for_filter(u, PA_OBJECT(i), filter, TRUE, TRUE);
575 idx = filter->module_index;
576 pa_hashmap_remove(u->filters, filter);
579 if (can_unload_module(u, idx))
580 pa_module_unload_request_by_index(u->core, idx, TRUE);
587 static pa_hook_result_t source_output_put_cb(pa_core *core, pa_source_output *o, struct userdata *u) {
588 pa_core_assert_ref(core);
589 pa_source_output_assert_ref(o);
591 return process(u, PA_OBJECT(o), FALSE);
594 static pa_hook_result_t source_output_move_finish_cb(pa_core *core, pa_source_output *o, struct userdata *u) {
595 pa_core_assert_ref(core);
596 pa_source_output_assert_ref(o);
598 if (pa_proplist_gets(o->proplist, PA_PROP_FILTER_APPLY_MOVING))
601 return process(u, PA_OBJECT(o), FALSE);
604 static pa_hook_result_t source_output_proplist_cb(pa_core *core, pa_source_output *o, struct userdata *u) {
605 pa_core_assert_ref(core);
606 pa_source_output_assert_ref(o);
608 return process(u, PA_OBJECT(o), FALSE);
611 static pa_hook_result_t source_output_unlink_cb(pa_core *core, pa_source_output *o, struct userdata *u) {
612 pa_core_assert_ref(core);
613 pa_source_output_assert_ref(o);
617 if (pa_hashmap_size(u->filters) > 0)
618 trigger_housekeeping(u);
623 static pa_hook_result_t source_unlink_cb(pa_core *core, pa_source *source, struct userdata *u) {
625 struct filter *filter = NULL;
627 pa_core_assert_ref(core);
628 pa_source_assert_ref(source);
631 /* If either the parent or the source we've loaded disappears,
632 * we should remove it from our hashmap */
633 PA_HASHMAP_FOREACH(filter, u->filters, state) {
634 if (filter->source_master == source || filter->source == source) {
637 /* Attempt to rescue any streams to the parent source as this is likely
638 * the best course of action (as opposed to a generic rescue via
639 * module-rescue-streams */
640 if (filter->source == source) {
643 PA_IDXSET_FOREACH(o, source->outputs, idx)
644 move_objects_for_filter(u, PA_OBJECT(o), filter, TRUE, FALSE);
647 idx = filter->module_index;
648 pa_hashmap_remove(u->filters, filter);
651 if (can_unload_module(u, idx))
652 pa_module_unload_request_by_index(u->core, idx, TRUE);
659 int pa__init(pa_module *m) {
660 pa_modargs *ma = NULL;
665 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
666 pa_log("Failed to parse module arguments");
670 m->userdata = u = pa_xnew0(struct userdata, 1);
674 u->autoclean = DEFAULT_AUTOCLEAN;
675 if (pa_modargs_get_value_boolean(ma, "autoclean", &u->autoclean) < 0) {
676 pa_log("Failed to parse autoclean value");
680 u->filters = pa_hashmap_new(filter_hash, filter_compare);
682 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);
683 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);
684 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);
685 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);
686 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);
687 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);
688 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);
689 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);
690 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);
691 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);
706 void pa__done(pa_module *m) {
711 if (!(u = m->userdata))
714 if (u->sink_input_put_slot)
715 pa_hook_slot_free(u->sink_input_put_slot);
716 if (u->sink_input_move_finish_slot)
717 pa_hook_slot_free(u->sink_input_move_finish_slot);
718 if (u->sink_input_proplist_slot)
719 pa_hook_slot_free(u->sink_input_proplist_slot);
720 if (u->sink_input_unlink_slot)
721 pa_hook_slot_free(u->sink_input_unlink_slot);
722 if (u->sink_unlink_slot)
723 pa_hook_slot_free(u->sink_unlink_slot);
724 if (u->source_output_put_slot)
725 pa_hook_slot_free(u->source_output_put_slot);
726 if (u->source_output_move_finish_slot)
727 pa_hook_slot_free(u->source_output_move_finish_slot);
728 if (u->source_output_proplist_slot)
729 pa_hook_slot_free(u->source_output_proplist_slot);
730 if (u->source_output_unlink_slot)
731 pa_hook_slot_free(u->source_output_unlink_slot);
732 if (u->source_unlink_slot)
733 pa_hook_slot_free(u->source_unlink_slot);
735 if (u->housekeeping_time_event)
736 u->core->mainloop->time_free(u->housekeeping_time_event);
741 while ((f = pa_hashmap_steal_first(u->filters))) {
742 pa_module_unload_request_by_index(u->core, f->module_index, TRUE);
746 pa_hashmap_free(u->filters, NULL, NULL);