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/i18n.h>
30 #include <pulsecore/macro.h>
31 #include <pulsecore/hashmap.h>
32 #include <pulsecore/hook-list.h>
33 #include <pulsecore/core.h>
34 #include <pulsecore/core-util.h>
35 #include <pulsecore/sink-input.h>
36 #include <pulsecore/modargs.h>
38 #include "module-filter-apply-symdef.h"
40 #define PA_PROP_FILTER_APPLY_MOVING "filter.apply.moving"
42 PA_MODULE_AUTHOR("Colin Guthrie");
43 PA_MODULE_DESCRIPTION("Load filter sinks automatically when needed");
44 PA_MODULE_VERSION(PACKAGE_VERSION
);
45 PA_MODULE_LOAD_ONCE(TRUE
);
46 PA_MODULE_USAGE(_("autoclean=<automatically unload unused filters?>"));
48 static const char* const valid_modargs
[] = {
53 #define DEFAULT_AUTOCLEAN TRUE
54 #define HOUSEKEEPING_INTERVAL (10 * PA_USEC_PER_SEC)
58 uint32_t module_index
;
60 pa_object
*parent_obj
; /* source or sink that the filter is connected to */
61 pa_object
*obj
; /* source or sink of the filter */
69 *sink_input_move_finish_slot
,
70 *sink_input_proplist_slot
,
71 *sink_input_unlink_slot
,
73 *source_output_put_slot
,
74 *source_output_move_finish_slot
,
75 *source_output_proplist_slot
,
76 *source_output_unlink_slot
,
79 pa_time_event
*housekeeping_time_event
;
82 static unsigned filter_hash(const void *p
) {
83 const struct filter
*f
= p
;
86 return (unsigned) (PA_SINK(f
->parent_obj
)->index
+ pa_idxset_string_hash_func(f
->name
));
88 return (unsigned) ((PA_SOURCE(f
->parent_obj
)->index
<< 16) + pa_idxset_string_hash_func(f
->name
));
91 static int filter_compare(const void *a
, const void *b
) {
92 const struct filter
*fa
= a
, *fb
= b
;
95 if (fa
->parent_obj
!= fb
->parent_obj
)
97 if ((r
= strcmp(fa
->name
, fb
->name
)))
103 static struct filter
*filter_new(const char *name
, pa_object
* parent_obj
, pa_bool_t is_sink
) {
106 f
= pa_xnew(struct filter
, 1);
107 f
->name
= pa_xstrdup(name
);
108 pa_assert_se(f
->parent_obj
= parent_obj
);
109 f
->is_sink
= is_sink
;
110 f
->module_index
= PA_INVALID_INDEX
;
115 static void filter_free(struct filter
*f
) {
122 static const char* should_filter(pa_object
*o
, pa_bool_t is_sink_input
) {
127 pl
= PA_SINK_INPUT(o
)->proplist
;
129 pl
= PA_SOURCE_OUTPUT(o
)->proplist
;
131 /* If the stream doesn't what any filter, then let it be. */
132 if ((apply
= pa_proplist_gets(pl
, PA_PROP_FILTER_APPLY
)) && !pa_streq(apply
, "")) {
133 const char* suppress
= pa_proplist_gets(pl
, PA_PROP_FILTER_SUPPRESS
);
135 if (!suppress
|| !pa_streq(suppress
, apply
))
142 static pa_bool_t
nothing_attached(pa_object
*obj
, pa_bool_t is_sink
)
145 return pa_idxset_isempty(PA_SINK(obj
)->inputs
);
147 return pa_idxset_isempty(PA_SOURCE(obj
)->outputs
);
150 static void housekeeping_time_callback(pa_mainloop_api
*a
, pa_time_event
* e
, const struct timeval
*t
, void *userdata
) {
151 struct userdata
*u
= userdata
;
152 struct filter
*filter
;
159 pa_assert(e
== u
->housekeeping_time_event
);
160 u
->core
->mainloop
->time_free(u
->housekeeping_time_event
);
161 u
->housekeeping_time_event
= NULL
;
163 PA_HASHMAP_FOREACH(filter
, u
->filters
, state
) {
164 if (filter
->obj
&& nothing_attached(filter
->obj
, filter
->is_sink
)) {
167 pa_log_debug("Detected filter %s as no longer used. Unloading.", filter
->name
);
168 idx
= filter
->module_index
;
169 pa_hashmap_remove(u
->filters
, filter
);
171 pa_module_unload_request_by_index(u
->core
, idx
, TRUE
);
175 pa_log_info("Housekeeping Done.");
178 static void trigger_housekeeping(struct userdata
*u
) {
184 if (u
->housekeeping_time_event
)
187 u
->housekeeping_time_event
= pa_core_rttime_new(u
->core
, pa_rtclock_now() + HOUSEKEEPING_INTERVAL
, housekeeping_time_callback
, u
);
190 static int do_move(pa_object
*obj
, pa_object
*parent
, pa_bool_t restore
, pa_bool_t is_input
) {
192 return pa_sink_input_move_to(PA_SINK_INPUT(obj
), PA_SINK(parent
), restore
);
194 return pa_source_output_move_to(PA_SOURCE_OUTPUT(obj
), PA_SOURCE(parent
), restore
);
197 static void move_object_for_filter(pa_object
*o
, struct filter
* filter
, pa_bool_t restore
, pa_bool_t is_sink_input
) {
205 pa_assert_se(parent
= (restore
? filter
->parent_obj
: filter
->obj
));
208 pl
= PA_SINK_INPUT(o
)->proplist
;
209 name
= PA_SINK(parent
)->name
;
211 pl
= PA_SOURCE_OUTPUT(o
)->proplist
;
212 name
= PA_SOURCE(parent
)->name
;
215 pa_proplist_sets(pl
, PA_PROP_FILTER_APPLY_MOVING
, "1");
217 if (do_move(o
, parent
, FALSE
, is_sink_input
) < 0)
218 pa_log_info("Failed to move %s for \"%s\" to <%s>.", is_sink_input
? "sink-input" : "source-output",
219 pa_strnull(pa_proplist_gets(pl
, PA_PROP_APPLICATION_NAME
)), name
);
221 pa_log_info("Sucessfully moved %s for \"%s\" to <%s>.", is_sink_input
? "sink-input" : "source-output",
222 pa_strnull(pa_proplist_gets(pl
, PA_PROP_APPLICATION_NAME
)), name
);
224 pa_proplist_unset(pl
, PA_PROP_FILTER_APPLY_MOVING
);
227 static void find_filters_for_module(struct userdata
*u
, pa_module
*m
, const char *name
) {
233 PA_IDXSET_FOREACH(sink
, u
->core
->sinks
, idx
) {
234 if (sink
->module
== m
) {
235 pa_assert(sink
->input_to_master
!= NULL
);
237 fltr
= filter_new(name
, PA_OBJECT(sink
->input_to_master
->sink
), TRUE
);
238 fltr
->module_index
= m
->index
;
239 fltr
->obj
= PA_OBJECT(sink
);
241 pa_hashmap_put(u
->filters
, fltr
, fltr
);
245 PA_IDXSET_FOREACH(source
, u
->core
->sources
, idx
) {
246 if (source
->module
== m
&& !source
->monitor_of
) {
247 pa_assert(source
->output_from_master
!= NULL
);
249 fltr
= filter_new(name
, PA_OBJECT(source
->output_from_master
->source
), FALSE
);
250 fltr
->module_index
= m
->index
;
251 fltr
->obj
= PA_OBJECT(source
);
253 pa_hashmap_put(u
->filters
, fltr
, fltr
);
258 static pa_bool_t
can_unload_module(struct userdata
*u
, uint32_t idx
) {
260 struct filter
*filter
;
262 /* Check if any other struct filters point to the same module */
263 PA_HASHMAP_FOREACH(filter
, u
->filters
, state
) {
264 if (filter
->module_index
== idx
&& !nothing_attached(filter
->obj
, pa_sink_isinstance(filter
->obj
)))
271 static pa_hook_result_t
process(struct userdata
*u
, pa_object
*o
, pa_bool_t is_sink_input
) {
273 pa_bool_t done_something
= FALSE
;
275 pa_object
*parent
; /* source/sink of the given source-output/sink-input */
276 const char *parent_name
;
280 parent
= PA_OBJECT(PA_SINK_INPUT(o
)->sink
);
281 parent_name
= PA_SINK_INPUT(o
)->sink
->name
;
282 module
= PA_SINK_INPUT(o
)->sink
->module
;
284 parent
= PA_OBJECT(PA_SOURCE_OUTPUT(o
)->source
);
285 parent_name
= PA_SOURCE_OUTPUT(o
)->source
->name
;
286 module
= PA_SOURCE_OUTPUT(o
)->source
->module
;
289 /* If there is no sink yet, we can't do much */
293 /* If the stream doesn't what any filter, then let it be. */
294 if ((want
= should_filter(o
, is_sink_input
))) {
296 struct filter
*fltr
, *filter
;
298 /* We need to ensure the SI is playing on a sink of this type
299 * attached to the sink it's "officially" playing on */
304 module_name
= pa_sprintf_malloc("module-%s", want
);
305 if (pa_streq(module
->name
, module_name
)) {
306 pa_log_debug("Stream appears to be playing on an appropriate sink already. Ignoring.");
307 pa_xfree(module_name
);
311 fltr
= filter_new(want
, parent
, is_sink_input
);
313 if (!(filter
= pa_hashmap_get(u
->filters
, fltr
))) {
317 args
= pa_sprintf_malloc("autoloaded=1 %s_master=%s", is_sink_input
? "sink" : "source", parent_name
);
318 pa_log_debug("Loading %s with arguments '%s'", module_name
, args
);
320 if ((m
= pa_module_load(u
->core
, module_name
, args
))) {
321 find_filters_for_module(u
, m
, want
);
322 filter
= pa_hashmap_get(u
->filters
, fltr
);
323 done_something
= TRUE
;
331 pa_log("Unable to load %s for <%s>", module_name
, parent_name
);
332 pa_xfree(module_name
);
335 pa_xfree(module_name
);
338 /* We can move the sink_input now as the know the destination.
339 * If this isn't true, we will do it later when the sink appears. */
340 move_object_for_filter(o
, filter
, FALSE
, is_sink_input
);
341 done_something
= TRUE
;
345 struct filter
*filter
= NULL
;
347 /* We do not want to filter... but are we already filtered?
348 * This can happen if an input's proplist changes */
349 PA_HASHMAP_FOREACH(filter
, u
->filters
, state
) {
350 if (parent
== filter
->obj
) {
351 move_object_for_filter(o
, filter
, TRUE
, is_sink_input
);
352 done_something
= TRUE
;
359 trigger_housekeeping(u
);
364 static pa_hook_result_t
sink_input_put_cb(pa_core
*core
, pa_sink_input
*i
, struct userdata
*u
) {
365 pa_core_assert_ref(core
);
366 pa_sink_input_assert_ref(i
);
368 return process(u
, PA_OBJECT(i
), TRUE
);
371 static pa_hook_result_t
sink_input_move_finish_cb(pa_core
*core
, pa_sink_input
*i
, struct userdata
*u
) {
372 pa_core_assert_ref(core
);
373 pa_sink_input_assert_ref(i
);
375 if (pa_proplist_gets(i
->proplist
, PA_PROP_FILTER_APPLY_MOVING
))
378 return process(u
, PA_OBJECT(i
), TRUE
);
381 static pa_hook_result_t
sink_input_proplist_cb(pa_core
*core
, pa_sink_input
*i
, struct userdata
*u
) {
382 pa_core_assert_ref(core
);
383 pa_sink_input_assert_ref(i
);
385 return process(u
, PA_OBJECT(i
), TRUE
);
388 static pa_hook_result_t
sink_input_unlink_cb(pa_core
*core
, pa_sink_input
*i
, struct userdata
*u
) {
389 pa_core_assert_ref(core
);
390 pa_sink_input_assert_ref(i
);
394 if (pa_hashmap_size(u
->filters
) > 0)
395 trigger_housekeeping(u
);
400 static pa_hook_result_t
sink_unlink_cb(pa_core
*core
, pa_sink
*sink
, struct userdata
*u
) {
402 struct filter
*filter
= NULL
;
404 pa_core_assert_ref(core
);
405 pa_sink_assert_ref(sink
);
408 /* If either the parent or the sink we've loaded disappears,
409 * we should remove it from our hashmap */
410 PA_HASHMAP_FOREACH(filter
, u
->filters
, state
) {
411 if (filter
->parent_obj
== PA_OBJECT(sink
) || filter
->obj
== PA_OBJECT(sink
)) {
414 /* Attempt to rescue any streams to the parent sink as this is likely
415 * the best course of action (as opposed to a generic rescue via
416 * module-rescue-streams */
417 if (filter
->obj
== PA_OBJECT(sink
)) {
420 PA_IDXSET_FOREACH(i
, sink
->inputs
, idx
)
421 move_object_for_filter(PA_OBJECT(i
), filter
, TRUE
, TRUE
);
424 idx
= filter
->module_index
;
425 pa_hashmap_remove(u
->filters
, filter
);
428 if (can_unload_module(u
, idx
))
429 pa_module_unload_request_by_index(u
->core
, idx
, TRUE
);
436 static pa_hook_result_t
source_output_put_cb(pa_core
*core
, pa_source_output
*o
, struct userdata
*u
) {
437 pa_core_assert_ref(core
);
438 pa_source_output_assert_ref(o
);
440 return process(u
, PA_OBJECT(o
), FALSE
);
443 static pa_hook_result_t
source_output_move_finish_cb(pa_core
*core
, pa_source_output
*o
, struct userdata
*u
) {
444 pa_core_assert_ref(core
);
445 pa_source_output_assert_ref(o
);
447 if (pa_proplist_gets(o
->proplist
, PA_PROP_FILTER_APPLY_MOVING
))
450 return process(u
, PA_OBJECT(o
), FALSE
);
453 static pa_hook_result_t
source_output_proplist_cb(pa_core
*core
, pa_source_output
*o
, struct userdata
*u
) {
454 pa_core_assert_ref(core
);
455 pa_source_output_assert_ref(o
);
457 return process(u
, PA_OBJECT(o
), FALSE
);
460 static pa_hook_result_t
source_output_unlink_cb(pa_core
*core
, pa_source_output
*o
, struct userdata
*u
) {
461 pa_core_assert_ref(core
);
462 pa_source_output_assert_ref(o
);
466 if (pa_hashmap_size(u
->filters
) > 0)
467 trigger_housekeeping(u
);
472 static pa_hook_result_t
source_unlink_cb(pa_core
*core
, pa_source
*source
, struct userdata
*u
) {
474 struct filter
*filter
= NULL
;
476 pa_core_assert_ref(core
);
477 pa_source_assert_ref(source
);
480 /* If either the parent or the source we've loaded disappears,
481 * we should remove it from our hashmap */
482 PA_HASHMAP_FOREACH(filter
, u
->filters
, state
) {
483 if (filter
->parent_obj
== PA_OBJECT(source
) || filter
->obj
== PA_OBJECT(source
)) {
486 /* Attempt to rescue any streams to the parent source as this is likely
487 * the best course of action (as opposed to a generic rescue via
488 * module-rescue-streams */
489 if (filter
->obj
== PA_OBJECT(source
)) {
492 PA_IDXSET_FOREACH(o
, source
->outputs
, idx
)
493 move_object_for_filter(PA_OBJECT(o
), filter
, TRUE
, FALSE
);
496 idx
= filter
->module_index
;
497 pa_hashmap_remove(u
->filters
, filter
);
500 if (can_unload_module(u
, idx
))
501 pa_module_unload_request_by_index(u
->core
, idx
, TRUE
);
508 int pa__init(pa_module
*m
) {
509 pa_modargs
*ma
= NULL
;
514 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
515 pa_log("Failed to parse module arguments");
519 m
->userdata
= u
= pa_xnew0(struct userdata
, 1);
523 u
->autoclean
= DEFAULT_AUTOCLEAN
;
524 if (pa_modargs_get_value_boolean(ma
, "autoclean", &u
->autoclean
) < 0) {
525 pa_log("Failed to parse autoclean value");
529 u
->filters
= pa_hashmap_new(filter_hash
, filter_compare
);
531 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
);
532 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
);
533 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
);
534 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
);
535 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
);
536 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
);
537 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
);
538 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
);
539 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
);
540 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
);
555 void pa__done(pa_module
*m
) {
560 if (!(u
= m
->userdata
))
563 if (u
->sink_input_put_slot
)
564 pa_hook_slot_free(u
->sink_input_put_slot
);
565 if (u
->sink_input_move_finish_slot
)
566 pa_hook_slot_free(u
->sink_input_move_finish_slot
);
567 if (u
->sink_input_proplist_slot
)
568 pa_hook_slot_free(u
->sink_input_proplist_slot
);
569 if (u
->sink_input_unlink_slot
)
570 pa_hook_slot_free(u
->sink_input_unlink_slot
);
571 if (u
->sink_unlink_slot
)
572 pa_hook_slot_free(u
->sink_unlink_slot
);
573 if (u
->source_output_put_slot
)
574 pa_hook_slot_free(u
->source_output_put_slot
);
575 if (u
->source_output_move_finish_slot
)
576 pa_hook_slot_free(u
->source_output_move_finish_slot
);
577 if (u
->source_output_proplist_slot
)
578 pa_hook_slot_free(u
->source_output_proplist_slot
);
579 if (u
->source_output_unlink_slot
)
580 pa_hook_slot_free(u
->source_output_unlink_slot
);
581 if (u
->source_unlink_slot
)
582 pa_hook_slot_free(u
->source_unlink_slot
);
584 if (u
->housekeeping_time_event
)
585 u
->core
->mainloop
->time_free(u
->housekeeping_time_event
);
590 while ((f
= pa_hashmap_steal_first(u
->filters
))) {
591 pa_module_unload_request_by_index(u
->core
, f
->module_index
, TRUE
);
595 pa_hashmap_free(u
->filters
, NULL
, NULL
);