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
;
61 pa_object
*parent_obj
; /* source or sink that the filter is connected to */
62 pa_object
*obj
; /* source or sink of the filter */
70 *sink_input_move_finish_slot
,
71 *sink_input_proplist_slot
,
72 *sink_input_unlink_slot
,
74 *source_output_put_slot
,
75 *source_output_move_finish_slot
,
76 *source_output_proplist_slot
,
77 *source_output_unlink_slot
,
80 pa_time_event
*housekeeping_time_event
;
83 static unsigned filter_hash(const void *p
) {
84 const struct filter
*f
= p
;
87 return (unsigned) (PA_SINK(f
->parent_obj
)->index
+ pa_idxset_string_hash_func(f
->name
));
89 return (unsigned) ((PA_SOURCE(f
->parent_obj
)->index
<< 16) + pa_idxset_string_hash_func(f
->name
));
92 static int filter_compare(const void *a
, const void *b
) {
93 const struct filter
*fa
= a
, *fb
= b
;
96 if (fa
->parent_obj
!= fb
->parent_obj
)
98 if ((r
= strcmp(fa
->name
, fb
->name
)))
104 static struct filter
*filter_new(const char *name
, pa_object
* parent_obj
, pa_bool_t is_sink
) {
107 f
= pa_xnew(struct filter
, 1);
108 f
->name
= pa_xstrdup(name
);
109 pa_assert_se(f
->parent_obj
= parent_obj
);
110 f
->is_sink
= is_sink
;
111 f
->module_index
= PA_INVALID_INDEX
;
116 static void filter_free(struct filter
*f
) {
123 static const char* should_filter(pa_object
*o
, pa_bool_t is_sink_input
) {
128 pl
= PA_SINK_INPUT(o
)->proplist
;
130 pl
= PA_SOURCE_OUTPUT(o
)->proplist
;
132 /* If the stream doesn't what any filter, then let it be. */
133 if ((apply
= pa_proplist_gets(pl
, PA_PROP_FILTER_APPLY
)) && !pa_streq(apply
, "")) {
134 const char* suppress
= pa_proplist_gets(pl
, PA_PROP_FILTER_SUPPRESS
);
136 if (!suppress
|| !pa_streq(suppress
, apply
))
143 static pa_bool_t
nothing_attached(pa_object
*obj
, pa_bool_t is_sink
)
146 return pa_idxset_isempty(PA_SINK(obj
)->inputs
);
148 return pa_idxset_isempty(PA_SOURCE(obj
)->outputs
);
151 static void housekeeping_time_callback(pa_mainloop_api
*a
, pa_time_event
* e
, const struct timeval
*t
, void *userdata
) {
152 struct userdata
*u
= userdata
;
153 struct filter
*filter
;
160 pa_assert(e
== u
->housekeeping_time_event
);
161 u
->core
->mainloop
->time_free(u
->housekeeping_time_event
);
162 u
->housekeeping_time_event
= NULL
;
164 PA_HASHMAP_FOREACH(filter
, u
->filters
, state
) {
165 if (filter
->obj
&& nothing_attached(filter
->obj
, filter
->is_sink
)) {
168 pa_log_debug("Detected filter %s as no longer used. Unloading.", filter
->name
);
169 idx
= filter
->module_index
;
170 pa_hashmap_remove(u
->filters
, filter
);
172 pa_module_unload_request_by_index(u
->core
, idx
, TRUE
);
176 pa_log_info("Housekeeping Done.");
179 static void trigger_housekeeping(struct userdata
*u
) {
185 if (u
->housekeeping_time_event
)
188 u
->housekeeping_time_event
= pa_core_rttime_new(u
->core
, pa_rtclock_now() + HOUSEKEEPING_INTERVAL
, housekeeping_time_callback
, u
);
191 static int do_move(pa_object
*obj
, pa_object
*parent
, pa_bool_t restore
, pa_bool_t is_input
) {
193 return pa_sink_input_move_to(PA_SINK_INPUT(obj
), PA_SINK(parent
), restore
);
195 return pa_source_output_move_to(PA_SOURCE_OUTPUT(obj
), PA_SOURCE(parent
), restore
);
198 static void move_object_for_filter(pa_object
*o
, struct filter
* filter
, pa_bool_t restore
, pa_bool_t is_sink_input
) {
206 pa_assert_se(parent
= (restore
? filter
->parent_obj
: filter
->obj
));
209 pl
= PA_SINK_INPUT(o
)->proplist
;
210 name
= PA_SINK(parent
)->name
;
212 pl
= PA_SOURCE_OUTPUT(o
)->proplist
;
213 name
= PA_SOURCE(parent
)->name
;
216 pa_proplist_sets(pl
, PA_PROP_FILTER_APPLY_MOVING
, "1");
218 if (do_move(o
, parent
, FALSE
, is_sink_input
) < 0)
219 pa_log_info("Failed to move %s for \"%s\" to <%s>.", is_sink_input
? "sink-input" : "source-output",
220 pa_strnull(pa_proplist_gets(pl
, PA_PROP_APPLICATION_NAME
)), name
);
222 pa_log_info("Successfully moved %s for \"%s\" to <%s>.", is_sink_input
? "sink-input" : "source-output",
223 pa_strnull(pa_proplist_gets(pl
, PA_PROP_APPLICATION_NAME
)), name
);
225 pa_proplist_unset(pl
, PA_PROP_FILTER_APPLY_MOVING
);
228 static void find_filters_for_module(struct userdata
*u
, pa_module
*m
, const char *name
) {
234 PA_IDXSET_FOREACH(sink
, u
->core
->sinks
, idx
) {
235 if (sink
->module
== m
) {
236 pa_assert(sink
->input_to_master
!= NULL
);
238 fltr
= filter_new(name
, PA_OBJECT(sink
->input_to_master
->sink
), TRUE
);
239 fltr
->module_index
= m
->index
;
240 fltr
->obj
= PA_OBJECT(sink
);
242 pa_hashmap_put(u
->filters
, fltr
, fltr
);
246 PA_IDXSET_FOREACH(source
, u
->core
->sources
, idx
) {
247 if (source
->module
== m
&& !source
->monitor_of
) {
248 pa_assert(source
->output_from_master
!= NULL
);
250 fltr
= filter_new(name
, PA_OBJECT(source
->output_from_master
->source
), FALSE
);
251 fltr
->module_index
= m
->index
;
252 fltr
->obj
= PA_OBJECT(source
);
254 pa_hashmap_put(u
->filters
, fltr
, fltr
);
259 static pa_bool_t
can_unload_module(struct userdata
*u
, uint32_t idx
) {
261 struct filter
*filter
;
263 /* Check if any other struct filters point to the same module */
264 PA_HASHMAP_FOREACH(filter
, u
->filters
, state
) {
265 if (filter
->module_index
== idx
&& !nothing_attached(filter
->obj
, pa_sink_isinstance(filter
->obj
)))
272 static pa_hook_result_t
process(struct userdata
*u
, pa_object
*o
, pa_bool_t is_sink_input
) {
274 pa_bool_t done_something
= FALSE
;
276 pa_object
*parent
; /* source/sink of the given source-output/sink-input */
277 const char *parent_name
;
281 parent
= PA_OBJECT(PA_SINK_INPUT(o
)->sink
);
282 parent_name
= PA_SINK_INPUT(o
)->sink
->name
;
283 module
= PA_SINK_INPUT(o
)->sink
->module
;
285 parent
= PA_OBJECT(PA_SOURCE_OUTPUT(o
)->source
);
286 parent_name
= PA_SOURCE_OUTPUT(o
)->source
->name
;
287 module
= PA_SOURCE_OUTPUT(o
)->source
->module
;
290 /* If there is no sink yet, we can't do much */
294 /* If the stream doesn't what any filter, then let it be. */
295 if ((want
= should_filter(o
, is_sink_input
))) {
297 struct filter
*fltr
, *filter
;
299 /* We need to ensure the SI is playing on a sink of this type
300 * attached to the sink it's "officially" playing on */
305 module_name
= pa_sprintf_malloc("module-%s", want
);
306 if (pa_streq(module
->name
, module_name
)) {
307 pa_log_debug("Stream appears to be playing on an appropriate sink already. Ignoring.");
308 pa_xfree(module_name
);
312 fltr
= filter_new(want
, parent
, is_sink_input
);
314 if (!(filter
= pa_hashmap_get(u
->filters
, fltr
))) {
318 args
= pa_sprintf_malloc("autoloaded=1 %s_master=%s", is_sink_input
? "sink" : "source", parent_name
);
319 pa_log_debug("Loading %s with arguments '%s'", module_name
, args
);
321 if ((m
= pa_module_load(u
->core
, module_name
, args
))) {
322 find_filters_for_module(u
, m
, want
);
323 filter
= pa_hashmap_get(u
->filters
, fltr
);
324 done_something
= TRUE
;
332 pa_log("Unable to load %s for <%s>", module_name
, parent_name
);
333 pa_xfree(module_name
);
336 pa_xfree(module_name
);
339 /* We can move the sink_input now as the know the destination.
340 * If this isn't true, we will do it later when the sink appears. */
341 move_object_for_filter(o
, filter
, FALSE
, is_sink_input
);
342 done_something
= TRUE
;
346 struct filter
*filter
= NULL
;
348 /* We do not want to filter... but are we already filtered?
349 * This can happen if an input's proplist changes */
350 PA_HASHMAP_FOREACH(filter
, u
->filters
, state
) {
351 if (parent
== filter
->obj
) {
352 move_object_for_filter(o
, filter
, TRUE
, is_sink_input
);
353 done_something
= TRUE
;
360 trigger_housekeeping(u
);
365 static pa_hook_result_t
sink_input_put_cb(pa_core
*core
, pa_sink_input
*i
, struct userdata
*u
) {
366 pa_core_assert_ref(core
);
367 pa_sink_input_assert_ref(i
);
369 return process(u
, PA_OBJECT(i
), TRUE
);
372 static pa_hook_result_t
sink_input_move_finish_cb(pa_core
*core
, pa_sink_input
*i
, struct userdata
*u
) {
373 pa_core_assert_ref(core
);
374 pa_sink_input_assert_ref(i
);
376 if (pa_proplist_gets(i
->proplist
, PA_PROP_FILTER_APPLY_MOVING
))
379 return process(u
, PA_OBJECT(i
), TRUE
);
382 static pa_hook_result_t
sink_input_proplist_cb(pa_core
*core
, pa_sink_input
*i
, struct userdata
*u
) {
383 pa_core_assert_ref(core
);
384 pa_sink_input_assert_ref(i
);
386 return process(u
, PA_OBJECT(i
), TRUE
);
389 static pa_hook_result_t
sink_input_unlink_cb(pa_core
*core
, pa_sink_input
*i
, struct userdata
*u
) {
390 pa_core_assert_ref(core
);
391 pa_sink_input_assert_ref(i
);
395 if (pa_hashmap_size(u
->filters
) > 0)
396 trigger_housekeeping(u
);
401 static pa_hook_result_t
sink_unlink_cb(pa_core
*core
, pa_sink
*sink
, struct userdata
*u
) {
403 struct filter
*filter
= NULL
;
405 pa_core_assert_ref(core
);
406 pa_sink_assert_ref(sink
);
409 /* If either the parent or the sink we've loaded disappears,
410 * we should remove it from our hashmap */
411 PA_HASHMAP_FOREACH(filter
, u
->filters
, state
) {
412 if (filter
->parent_obj
== PA_OBJECT(sink
) || filter
->obj
== PA_OBJECT(sink
)) {
415 /* Attempt to rescue any streams to the parent sink as this is likely
416 * the best course of action (as opposed to a generic rescue via
417 * module-rescue-streams */
418 if (filter
->obj
== PA_OBJECT(sink
)) {
421 PA_IDXSET_FOREACH(i
, sink
->inputs
, idx
)
422 move_object_for_filter(PA_OBJECT(i
), filter
, TRUE
, TRUE
);
425 idx
= filter
->module_index
;
426 pa_hashmap_remove(u
->filters
, filter
);
429 if (can_unload_module(u
, idx
))
430 pa_module_unload_request_by_index(u
->core
, idx
, TRUE
);
437 static pa_hook_result_t
source_output_put_cb(pa_core
*core
, pa_source_output
*o
, struct userdata
*u
) {
438 pa_core_assert_ref(core
);
439 pa_source_output_assert_ref(o
);
441 return process(u
, PA_OBJECT(o
), FALSE
);
444 static pa_hook_result_t
source_output_move_finish_cb(pa_core
*core
, pa_source_output
*o
, struct userdata
*u
) {
445 pa_core_assert_ref(core
);
446 pa_source_output_assert_ref(o
);
448 if (pa_proplist_gets(o
->proplist
, PA_PROP_FILTER_APPLY_MOVING
))
451 return process(u
, PA_OBJECT(o
), FALSE
);
454 static pa_hook_result_t
source_output_proplist_cb(pa_core
*core
, pa_source_output
*o
, struct userdata
*u
) {
455 pa_core_assert_ref(core
);
456 pa_source_output_assert_ref(o
);
458 return process(u
, PA_OBJECT(o
), FALSE
);
461 static pa_hook_result_t
source_output_unlink_cb(pa_core
*core
, pa_source_output
*o
, struct userdata
*u
) {
462 pa_core_assert_ref(core
);
463 pa_source_output_assert_ref(o
);
467 if (pa_hashmap_size(u
->filters
) > 0)
468 trigger_housekeeping(u
);
473 static pa_hook_result_t
source_unlink_cb(pa_core
*core
, pa_source
*source
, struct userdata
*u
) {
475 struct filter
*filter
= NULL
;
477 pa_core_assert_ref(core
);
478 pa_source_assert_ref(source
);
481 /* If either the parent or the source we've loaded disappears,
482 * we should remove it from our hashmap */
483 PA_HASHMAP_FOREACH(filter
, u
->filters
, state
) {
484 if (filter
->parent_obj
== PA_OBJECT(source
) || filter
->obj
== PA_OBJECT(source
)) {
487 /* Attempt to rescue any streams to the parent source as this is likely
488 * the best course of action (as opposed to a generic rescue via
489 * module-rescue-streams */
490 if (filter
->obj
== PA_OBJECT(source
)) {
493 PA_IDXSET_FOREACH(o
, source
->outputs
, idx
)
494 move_object_for_filter(PA_OBJECT(o
), filter
, TRUE
, FALSE
);
497 idx
= filter
->module_index
;
498 pa_hashmap_remove(u
->filters
, filter
);
501 if (can_unload_module(u
, idx
))
502 pa_module_unload_request_by_index(u
->core
, idx
, TRUE
);
509 int pa__init(pa_module
*m
) {
510 pa_modargs
*ma
= NULL
;
515 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
516 pa_log("Failed to parse module arguments");
520 m
->userdata
= u
= pa_xnew0(struct userdata
, 1);
524 u
->autoclean
= DEFAULT_AUTOCLEAN
;
525 if (pa_modargs_get_value_boolean(ma
, "autoclean", &u
->autoclean
) < 0) {
526 pa_log("Failed to parse autoclean value");
530 u
->filters
= pa_hashmap_new(filter_hash
, filter_compare
);
532 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
);
533 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
);
534 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
);
535 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
);
536 u
->sink_unlink_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SINK_UNLINK
], PA_HOOK_LATE
, (pa_hook_cb_t
) sink_unlink_cb
, u
);
537 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
);
538 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
);
539 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
);
540 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
);
541 u
->source_unlink_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SOURCE_UNLINK
], PA_HOOK_LATE
, (pa_hook_cb_t
) source_unlink_cb
, u
);
556 void pa__done(pa_module
*m
) {
561 if (!(u
= m
->userdata
))
564 if (u
->sink_input_put_slot
)
565 pa_hook_slot_free(u
->sink_input_put_slot
);
566 if (u
->sink_input_move_finish_slot
)
567 pa_hook_slot_free(u
->sink_input_move_finish_slot
);
568 if (u
->sink_input_proplist_slot
)
569 pa_hook_slot_free(u
->sink_input_proplist_slot
);
570 if (u
->sink_input_unlink_slot
)
571 pa_hook_slot_free(u
->sink_input_unlink_slot
);
572 if (u
->sink_unlink_slot
)
573 pa_hook_slot_free(u
->sink_unlink_slot
);
574 if (u
->source_output_put_slot
)
575 pa_hook_slot_free(u
->source_output_put_slot
);
576 if (u
->source_output_move_finish_slot
)
577 pa_hook_slot_free(u
->source_output_move_finish_slot
);
578 if (u
->source_output_proplist_slot
)
579 pa_hook_slot_free(u
->source_output_proplist_slot
);
580 if (u
->source_output_unlink_slot
)
581 pa_hook_slot_free(u
->source_output_unlink_slot
);
582 if (u
->source_unlink_slot
)
583 pa_hook_slot_free(u
->source_unlink_slot
);
585 if (u
->housekeeping_time_event
)
586 u
->core
->mainloop
->time_free(u
->housekeeping_time_event
);
591 while ((f
= pa_hashmap_steal_first(u
->filters
))) {
592 pa_module_unload_request_by_index(u
->core
, f
->module_index
, TRUE
);
596 pa_hashmap_free(u
->filters
, NULL
, NULL
);