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>
39 #include "module-filter-apply-symdef.h"
41 #define PA_PROP_FILTER_APPLY_MOVING "filter.apply.moving"
43 PA_MODULE_AUTHOR("Colin Guthrie");
44 PA_MODULE_DESCRIPTION("Load filter sinks automatically when needed");
45 PA_MODULE_VERSION(PACKAGE_VERSION);
46 PA_MODULE_LOAD_ONCE(TRUE);
47 PA_MODULE_USAGE(_("autoclean=<automatically unload unused filters?>"));
49 static const char* const valid_modargs[] = {
54 #define DEFAULT_AUTOCLEAN TRUE
55 #define HOUSEKEEPING_INTERVAL (10 * PA_USEC_PER_SEC)
59 uint32_t module_index;
63 pa_source *source_master;
71 *sink_input_move_finish_slot,
72 *sink_input_proplist_slot,
73 *sink_input_unlink_slot,
75 *source_output_put_slot,
76 *source_output_move_finish_slot,
77 *source_output_proplist_slot,
78 *source_output_unlink_slot,
81 pa_time_event *housekeeping_time_event;
84 static unsigned filter_hash(const void *p) {
85 const struct filter *f = p;
87 if (f->sink_master && !f->source_master)
88 return (unsigned) (f->sink_master->index + pa_idxset_string_hash_func(f->name));
89 else if (!f->sink_master && f->source_master)
90 return (unsigned) ((f->source_master->index << 16) + pa_idxset_string_hash_func(f->name));
92 pa_assert_not_reached();
95 static int filter_compare(const void *a, const void *b) {
96 const struct filter *fa = a, *fb = b;
99 if (fa->sink_master != fb->sink_master || fa->source_master != fb->source_master)
101 if ((r = strcmp(fa->name, fb->name)))
107 static struct filter *filter_new(const char *name, pa_sink *sink, pa_source *source) {
110 pa_assert(sink || source);
112 f = pa_xnew(struct filter, 1);
113 f->name = pa_xstrdup(name);
114 f->sink_master = sink;
115 f->source_master = source;
116 f->module_index = PA_INVALID_INDEX;
123 static void filter_free(struct filter *f) {
130 static const char* should_filter(pa_object *o, pa_bool_t is_sink_input) {
135 pl = PA_SINK_INPUT(o)->proplist;
137 pl = PA_SOURCE_OUTPUT(o)->proplist;
139 /* If the stream doesn't what any filter, then let it be. */
140 if ((apply = pa_proplist_gets(pl, PA_PROP_FILTER_APPLY)) && !pa_streq(apply, "")) {
141 const char* suppress = pa_proplist_gets(pl, PA_PROP_FILTER_SUPPRESS);
143 if (!suppress || !pa_streq(suppress, apply))
150 static pa_bool_t nothing_attached(struct filter *f) {
151 pa_bool_t no_si = TRUE, no_so = TRUE;
154 no_si = pa_idxset_isempty(f->sink->inputs);
156 no_so = pa_idxset_isempty(f->source->outputs);
158 return no_si && no_so;
161 static void housekeeping_time_callback(pa_mainloop_api*a, pa_time_event* e, const struct timeval *t, void *userdata) {
162 struct userdata *u = userdata;
163 struct filter *filter;
170 pa_assert(e == u->housekeeping_time_event);
171 u->core->mainloop->time_free(u->housekeeping_time_event);
172 u->housekeeping_time_event = NULL;
174 PA_HASHMAP_FOREACH(filter, u->filters, state) {
175 if (nothing_attached(filter)) {
178 pa_log_debug("Detected filter %s as no longer used. Unloading.", filter->name);
179 idx = filter->module_index;
180 pa_hashmap_remove(u->filters, filter);
182 pa_module_unload_request_by_index(u->core, idx, TRUE);
186 pa_log_info("Housekeeping Done.");
189 static void trigger_housekeeping(struct userdata *u) {
195 if (u->housekeeping_time_event)
198 u->housekeeping_time_event = pa_core_rttime_new(u->core, pa_rtclock_now() + HOUSEKEEPING_INTERVAL, housekeeping_time_callback, u);
201 static int do_move(pa_object *obj, pa_object *parent, pa_bool_t restore, pa_bool_t is_input) {
203 return pa_sink_input_move_to(PA_SINK_INPUT(obj), PA_SINK(parent), restore);
205 return pa_source_output_move_to(PA_SOURCE_OUTPUT(obj), PA_SOURCE(parent), restore);
208 static void move_object_for_filter(pa_object *o, struct filter* filter, pa_bool_t restore, pa_bool_t is_sink_input) {
217 pl = PA_SINK_INPUT(o)->proplist;
218 pa_assert_se(parent = PA_OBJECT(restore ? filter->sink_master : filter->sink));
219 name = PA_SINK(parent)->name;
221 pl = PA_SOURCE_OUTPUT(o)->proplist;
222 pa_assert_se(parent = PA_OBJECT(restore ? filter->source_master : filter->source));
223 name = PA_SOURCE(parent)->name;
226 pa_proplist_sets(pl, PA_PROP_FILTER_APPLY_MOVING, "1");
228 if (do_move(o, parent, FALSE, is_sink_input) < 0)
229 pa_log_info("Failed to move %s for \"%s\" to <%s>.", is_sink_input ? "sink-input" : "source-output",
230 pa_strnull(pa_proplist_gets(pl, PA_PROP_APPLICATION_NAME)), name);
232 pa_log_info("Successfully moved %s for \"%s\" to <%s>.", is_sink_input ? "sink-input" : "source-output",
233 pa_strnull(pa_proplist_gets(pl, PA_PROP_APPLICATION_NAME)), name);
235 pa_proplist_unset(pl, PA_PROP_FILTER_APPLY_MOVING);
238 static void find_filters_for_module(struct userdata *u, pa_module *m, const char *name) {
244 PA_IDXSET_FOREACH(sink, u->core->sinks, idx) {
245 if (sink->module == m) {
246 pa_assert(sink->input_to_master != NULL);
248 fltr = filter_new(name, sink->input_to_master->sink, NULL);
249 fltr->module_index = m->index;
252 pa_hashmap_put(u->filters, fltr, fltr);
256 PA_IDXSET_FOREACH(source, u->core->sources, idx) {
257 if (source->module == m && !source->monitor_of) {
258 pa_assert(source->output_from_master != NULL);
260 fltr = filter_new(name, NULL, source->output_from_master->source);
261 fltr->module_index = m->index;
262 fltr->source = source;
264 pa_hashmap_put(u->filters, fltr, fltr);
269 static pa_bool_t can_unload_module(struct userdata *u, uint32_t idx) {
271 struct filter *filter;
273 /* Check if any other struct filters point to the same module */
274 PA_HASHMAP_FOREACH(filter, u->filters, state) {
275 if (filter->module_index == idx && !nothing_attached(filter))
282 static pa_hook_result_t process(struct userdata *u, pa_object *o, pa_bool_t is_sink_input) {
284 pa_bool_t done_something = FALSE;
285 pa_sink *sink = NULL;
286 pa_source *source = NULL;
287 const char *sink_name = NULL, *source_name = NULL;
291 sink = PA_SINK_INPUT(o)->sink;
292 sink_name = sink->name;
293 module = sink->module;
295 source = PA_SOURCE_OUTPUT(o)->source;
296 source_name = source->name;
297 module = source->module;
300 /* If there is no sink/source yet, we can't do much */
301 if ((is_sink_input && !sink) || (!is_sink_input && !source))
304 /* If the stream doesn't what any filter, then let it be. */
305 if ((want = should_filter(o, is_sink_input))) {
307 struct filter *fltr, *filter;
309 /* We need to ensure the SI is playing on a sink of this type
310 * attached to the sink it's "officially" playing on */
315 module_name = pa_sprintf_malloc("module-%s", want);
316 if (pa_streq(module->name, module_name)) {
317 pa_log_debug("Stream appears to be playing on an appropriate sink already. Ignoring.");
318 pa_xfree(module_name);
322 fltr = filter_new(want, sink, source);
324 if (!(filter = pa_hashmap_get(u->filters, fltr))) {
328 args = pa_sprintf_malloc("autoloaded=1 %s_master=%s", is_sink_input ? "sink" : "source",
329 is_sink_input ? sink_name : source_name);
330 pa_log_debug("Loading %s with arguments '%s'", module_name, args);
332 if ((m = pa_module_load(u->core, module_name, args))) {
333 find_filters_for_module(u, m, want);
334 filter = pa_hashmap_get(u->filters, fltr);
335 done_something = TRUE;
343 pa_log("Unable to load %s for <%s>", module_name, is_sink_input ? sink_name : source_name);
344 pa_xfree(module_name);
347 pa_xfree(module_name);
349 /* We can move the stream now as we know the destination. If this
350 * isn't true, we will do it later when the sink appears. */
351 if ((is_sink_input && filter->sink) || (!is_sink_input && filter->source)) {
352 move_object_for_filter(o, filter, FALSE, is_sink_input);
353 done_something = TRUE;
357 struct filter *filter = NULL;
359 /* We do not want to filter... but are we already filtered?
360 * This can happen if an input's proplist changes */
361 PA_HASHMAP_FOREACH(filter, u->filters, state) {
362 if ((is_sink_input && sink == filter->sink) || (!is_sink_input && source == filter->source)) {
363 move_object_for_filter(o, filter, TRUE, is_sink_input);
364 done_something = TRUE;
371 trigger_housekeeping(u);
376 static pa_hook_result_t sink_input_put_cb(pa_core *core, pa_sink_input *i, struct userdata *u) {
377 pa_core_assert_ref(core);
378 pa_sink_input_assert_ref(i);
380 return process(u, PA_OBJECT(i), TRUE);
383 static pa_hook_result_t sink_input_move_finish_cb(pa_core *core, pa_sink_input *i, struct userdata *u) {
384 pa_core_assert_ref(core);
385 pa_sink_input_assert_ref(i);
387 if (pa_proplist_gets(i->proplist, PA_PROP_FILTER_APPLY_MOVING))
390 return process(u, PA_OBJECT(i), TRUE);
393 static pa_hook_result_t sink_input_proplist_cb(pa_core *core, pa_sink_input *i, struct userdata *u) {
394 pa_core_assert_ref(core);
395 pa_sink_input_assert_ref(i);
397 return process(u, PA_OBJECT(i), TRUE);
400 static pa_hook_result_t sink_input_unlink_cb(pa_core *core, pa_sink_input *i, struct userdata *u) {
401 pa_core_assert_ref(core);
402 pa_sink_input_assert_ref(i);
406 if (pa_hashmap_size(u->filters) > 0)
407 trigger_housekeeping(u);
412 static pa_hook_result_t sink_unlink_cb(pa_core *core, pa_sink *sink, struct userdata *u) {
414 struct filter *filter = NULL;
416 pa_core_assert_ref(core);
417 pa_sink_assert_ref(sink);
420 /* If either the parent or the sink we've loaded disappears,
421 * we should remove it from our hashmap */
422 PA_HASHMAP_FOREACH(filter, u->filters, state) {
423 if (filter->sink_master == sink || filter->sink == sink) {
426 /* Attempt to rescue any streams to the parent sink as this is likely
427 * the best course of action (as opposed to a generic rescue via
428 * module-rescue-streams */
429 if (filter->sink == sink) {
432 PA_IDXSET_FOREACH(i, sink->inputs, idx)
433 move_object_for_filter(PA_OBJECT(i), filter, TRUE, TRUE);
436 idx = filter->module_index;
437 pa_hashmap_remove(u->filters, filter);
440 if (can_unload_module(u, idx))
441 pa_module_unload_request_by_index(u->core, idx, TRUE);
448 static pa_hook_result_t source_output_put_cb(pa_core *core, pa_source_output *o, struct userdata *u) {
449 pa_core_assert_ref(core);
450 pa_source_output_assert_ref(o);
452 return process(u, PA_OBJECT(o), FALSE);
455 static pa_hook_result_t source_output_move_finish_cb(pa_core *core, pa_source_output *o, struct userdata *u) {
456 pa_core_assert_ref(core);
457 pa_source_output_assert_ref(o);
459 if (pa_proplist_gets(o->proplist, PA_PROP_FILTER_APPLY_MOVING))
462 return process(u, PA_OBJECT(o), FALSE);
465 static pa_hook_result_t source_output_proplist_cb(pa_core *core, pa_source_output *o, struct userdata *u) {
466 pa_core_assert_ref(core);
467 pa_source_output_assert_ref(o);
469 return process(u, PA_OBJECT(o), FALSE);
472 static pa_hook_result_t source_output_unlink_cb(pa_core *core, pa_source_output *o, struct userdata *u) {
473 pa_core_assert_ref(core);
474 pa_source_output_assert_ref(o);
478 if (pa_hashmap_size(u->filters) > 0)
479 trigger_housekeeping(u);
484 static pa_hook_result_t source_unlink_cb(pa_core *core, pa_source *source, struct userdata *u) {
486 struct filter *filter = NULL;
488 pa_core_assert_ref(core);
489 pa_source_assert_ref(source);
492 /* If either the parent or the source we've loaded disappears,
493 * we should remove it from our hashmap */
494 PA_HASHMAP_FOREACH(filter, u->filters, state) {
495 if (filter->source_master == source || filter->source == source) {
498 /* Attempt to rescue any streams to the parent source as this is likely
499 * the best course of action (as opposed to a generic rescue via
500 * module-rescue-streams */
501 if (filter->source == source) {
504 PA_IDXSET_FOREACH(o, source->outputs, idx)
505 move_object_for_filter(PA_OBJECT(o), filter, TRUE, FALSE);
508 idx = filter->module_index;
509 pa_hashmap_remove(u->filters, filter);
512 if (can_unload_module(u, idx))
513 pa_module_unload_request_by_index(u->core, idx, TRUE);
520 int pa__init(pa_module *m) {
521 pa_modargs *ma = NULL;
526 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
527 pa_log("Failed to parse module arguments");
531 m->userdata = u = pa_xnew0(struct userdata, 1);
535 u->autoclean = DEFAULT_AUTOCLEAN;
536 if (pa_modargs_get_value_boolean(ma, "autoclean", &u->autoclean) < 0) {
537 pa_log("Failed to parse autoclean value");
541 u->filters = pa_hashmap_new(filter_hash, filter_compare);
543 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);
544 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);
545 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);
546 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);
547 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);
548 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);
549 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);
550 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);
551 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);
552 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);
567 void pa__done(pa_module *m) {
572 if (!(u = m->userdata))
575 if (u->sink_input_put_slot)
576 pa_hook_slot_free(u->sink_input_put_slot);
577 if (u->sink_input_move_finish_slot)
578 pa_hook_slot_free(u->sink_input_move_finish_slot);
579 if (u->sink_input_proplist_slot)
580 pa_hook_slot_free(u->sink_input_proplist_slot);
581 if (u->sink_input_unlink_slot)
582 pa_hook_slot_free(u->sink_input_unlink_slot);
583 if (u->sink_unlink_slot)
584 pa_hook_slot_free(u->sink_unlink_slot);
585 if (u->source_output_put_slot)
586 pa_hook_slot_free(u->source_output_put_slot);
587 if (u->source_output_move_finish_slot)
588 pa_hook_slot_free(u->source_output_move_finish_slot);
589 if (u->source_output_proplist_slot)
590 pa_hook_slot_free(u->source_output_proplist_slot);
591 if (u->source_output_unlink_slot)
592 pa_hook_slot_free(u->source_output_unlink_slot);
593 if (u->source_unlink_slot)
594 pa_hook_slot_free(u->source_unlink_slot);
596 if (u->housekeeping_time_event)
597 u->core->mainloop->time_free(u->housekeeping_time_event);
602 while ((f = pa_hashmap_steal_first(u->filters))) {
603 pa_module_unload_request_by_index(u->core, f->module_index, TRUE);
607 pa_hashmap_free(u->filters, NULL, NULL);