]> code.delx.au - pulseaudio/blob - src/modules/module-filter-apply.c
filter-apply: Mark modules as being autoloaded
[pulseaudio] / src / modules / module-filter-apply.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2011 Colin Guthrie
5
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.
10
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.
15
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
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <pulse/timeval.h>
27 #include <pulse/rtclock.h>
28 #include <pulse/i18n.h>
29
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>
37
38 #include "module-filter-apply-symdef.h"
39
40 #define PA_PROP_FILTER_APPLY_MOVING "filter.apply.moving"
41
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?>"));
47
48 static const char* const valid_modargs[] = {
49 "autoclean",
50 NULL
51 };
52
53 #define DEFAULT_AUTOCLEAN TRUE
54 #define HOUSEKEEPING_INTERVAL (10 * PA_USEC_PER_SEC)
55
56 struct filter {
57 char *name;
58 uint32_t module_index;
59 pa_bool_t is_sink;
60 pa_object *parent_obj; /* source or sink that the filter is connected to */
61 pa_object *obj; /* source or sink of the filter */
62 };
63
64 struct userdata {
65 pa_core *core;
66 pa_hashmap *filters;
67 pa_hook_slot
68 *sink_input_put_slot,
69 *sink_input_move_finish_slot,
70 *sink_input_proplist_slot,
71 *sink_input_unlink_slot,
72 *sink_unlink_slot,
73 *source_output_put_slot,
74 *source_output_move_finish_slot,
75 *source_output_proplist_slot,
76 *source_output_unlink_slot,
77 *source_unlink_slot;
78 pa_bool_t autoclean;
79 pa_time_event *housekeeping_time_event;
80 };
81
82 static unsigned filter_hash(const void *p) {
83 const struct filter *f = p;
84
85 if (f->is_sink)
86 return (unsigned) (PA_SINK(f->parent_obj)->index + pa_idxset_string_hash_func(f->name));
87 else
88 return (unsigned) ((PA_SOURCE(f->parent_obj)->index << 16) + pa_idxset_string_hash_func(f->name));
89 }
90
91 static int filter_compare(const void *a, const void *b) {
92 const struct filter *fa = a, *fb = b;
93 int r;
94
95 if (fa->parent_obj != fb->parent_obj)
96 return 1;
97 if ((r = strcmp(fa->name, fb->name)))
98 return r;
99
100 return 0;
101 }
102
103 static struct filter *filter_new(const char *name, pa_object* parent_obj, pa_bool_t is_sink) {
104 struct filter *f;
105
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;
111 f->obj = NULL;
112 return f;
113 }
114
115 static void filter_free(struct filter *f) {
116 pa_assert(f);
117
118 pa_xfree(f->name);
119 pa_xfree(f);
120 }
121
122 static const char* should_filter(pa_object *o, pa_bool_t is_sink_input) {
123 const char *apply;
124 pa_proplist *pl;
125
126 if (is_sink_input)
127 pl = PA_SINK_INPUT(o)->proplist;
128 else
129 pl = PA_SOURCE_OUTPUT(o)->proplist;
130
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);
134
135 if (!suppress || !pa_streq(suppress, apply))
136 return apply;
137 }
138
139 return NULL;
140 }
141
142 static pa_bool_t nothing_attached(pa_object *obj, pa_bool_t is_sink)
143 {
144 if (is_sink)
145 return pa_idxset_isempty(PA_SINK(obj)->inputs);
146 else
147 return pa_idxset_isempty(PA_SOURCE(obj)->outputs);
148 }
149
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;
153 void *state;
154
155 pa_assert(a);
156 pa_assert(e);
157 pa_assert(u);
158
159 pa_assert(e == u->housekeeping_time_event);
160 u->core->mainloop->time_free(u->housekeeping_time_event);
161 u->housekeeping_time_event = NULL;
162
163 PA_HASHMAP_FOREACH(filter, u->filters, state) {
164 if (filter->obj && nothing_attached(filter->obj, filter->is_sink)) {
165 uint32_t idx;
166
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);
170 filter_free(filter);
171 pa_module_unload_request_by_index(u->core, idx, TRUE);
172 }
173 }
174
175 pa_log_info("Housekeeping Done.");
176 }
177
178 static void trigger_housekeeping(struct userdata *u) {
179 pa_assert(u);
180
181 if (!u->autoclean)
182 return;
183
184 if (u->housekeeping_time_event)
185 return;
186
187 u->housekeeping_time_event = pa_core_rttime_new(u->core, pa_rtclock_now() + HOUSEKEEPING_INTERVAL, housekeeping_time_callback, u);
188 }
189
190 static int do_move(pa_object *obj, pa_object *parent, pa_bool_t restore, pa_bool_t is_input) {
191 if (is_input)
192 return pa_sink_input_move_to(PA_SINK_INPUT(obj), PA_SINK(parent), restore);
193 else
194 return pa_source_output_move_to(PA_SOURCE_OUTPUT(obj), PA_SOURCE(parent), restore);
195 }
196
197 static void move_object_for_filter(pa_object *o, struct filter* filter, pa_bool_t restore, pa_bool_t is_sink_input) {
198 pa_object *parent;
199 pa_proplist *pl;
200 const char *name;
201
202 pa_assert(o);
203 pa_assert(filter);
204
205 pa_assert_se(parent = (restore ? filter->parent_obj : filter->obj));
206
207 if (is_sink_input) {
208 pl = PA_SINK_INPUT(o)->proplist;
209 name = PA_SINK(parent)->name;
210 } else {
211 pl = PA_SOURCE_OUTPUT(o)->proplist;
212 name = PA_SOURCE(parent)->name;
213 }
214
215 pa_proplist_sets(pl, PA_PROP_FILTER_APPLY_MOVING, "1");
216
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);
220 else
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);
223
224 pa_proplist_unset(pl, PA_PROP_FILTER_APPLY_MOVING);
225 }
226
227 static void find_filters_for_module(struct userdata *u, pa_module *m, const char *name) {
228 uint32_t idx;
229 pa_sink *sink;
230 pa_source *source;
231 struct filter *fltr;
232
233 PA_IDXSET_FOREACH(sink, u->core->sinks, idx) {
234 if (sink->module == m) {
235 pa_assert(sink->input_to_master != NULL);
236
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);
240
241 pa_hashmap_put(u->filters, fltr, fltr);
242 }
243 }
244
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);
248
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);
252
253 pa_hashmap_put(u->filters, fltr, fltr);
254 }
255 }
256 }
257
258 static pa_bool_t can_unload_module(struct userdata *u, uint32_t idx) {
259 void *state;
260 struct filter *filter;
261
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)))
265 return FALSE;
266 }
267
268 return TRUE;
269 }
270
271 static pa_hook_result_t process(struct userdata *u, pa_object *o, pa_bool_t is_sink_input) {
272 const char *want;
273 pa_bool_t done_something = FALSE;
274
275 pa_object *parent; /* source/sink of the given source-output/sink-input */
276 const char *parent_name;
277 pa_module *module;
278
279 if (is_sink_input) {
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;
283 } else {
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;
287 }
288
289 /* If there is no sink yet, we can't do much */
290 if (!parent)
291 return PA_HOOK_OK;
292
293 /* If the stream doesn't what any filter, then let it be. */
294 if ((want = should_filter(o, is_sink_input))) {
295 char *module_name;
296 struct filter *fltr, *filter;
297
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 */
300
301 if (!module)
302 return PA_HOOK_OK;
303
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);
308 return PA_HOOK_OK;
309 }
310
311 fltr = filter_new(want, parent, is_sink_input);
312
313 if (!(filter = pa_hashmap_get(u->filters, fltr))) {
314 char *args;
315 pa_module *m;
316
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);
319
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;
324 }
325 pa_xfree(args);
326 }
327
328 pa_xfree(fltr);
329
330 if (!filter) {
331 pa_log("Unable to load %s for <%s>", module_name, parent_name);
332 pa_xfree(module_name);
333 return PA_HOOK_OK;
334 }
335 pa_xfree(module_name);
336
337 if (filter->obj) {
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;
342 }
343 } else {
344 void *state;
345 struct filter *filter = NULL;
346
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;
353 break;
354 }
355 }
356 }
357
358 if (done_something)
359 trigger_housekeeping(u);
360
361 return PA_HOOK_OK;
362 }
363
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);
367
368 return process(u, PA_OBJECT(i), TRUE);
369 }
370
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);
374
375 if (pa_proplist_gets(i->proplist, PA_PROP_FILTER_APPLY_MOVING))
376 return PA_HOOK_OK;
377
378 return process(u, PA_OBJECT(i), TRUE);
379 }
380
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);
384
385 return process(u, PA_OBJECT(i), TRUE);
386 }
387
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);
391
392 pa_assert(u);
393
394 if (pa_hashmap_size(u->filters) > 0)
395 trigger_housekeeping(u);
396
397 return PA_HOOK_OK;
398 }
399
400 static pa_hook_result_t sink_unlink_cb(pa_core *core, pa_sink *sink, struct userdata *u) {
401 void *state;
402 struct filter *filter = NULL;
403
404 pa_core_assert_ref(core);
405 pa_sink_assert_ref(sink);
406 pa_assert(u);
407
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)) {
412 uint32_t idx;
413
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)) {
418 pa_sink_input *i;
419
420 PA_IDXSET_FOREACH(i, sink->inputs, idx)
421 move_object_for_filter(PA_OBJECT(i), filter, TRUE, TRUE);
422 }
423
424 idx = filter->module_index;
425 pa_hashmap_remove(u->filters, filter);
426 filter_free(filter);
427
428 if (can_unload_module(u, idx))
429 pa_module_unload_request_by_index(u->core, idx, TRUE);
430 }
431 }
432
433 return PA_HOOK_OK;
434 }
435
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);
439
440 return process(u, PA_OBJECT(o), FALSE);
441 }
442
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);
446
447 if (pa_proplist_gets(o->proplist, PA_PROP_FILTER_APPLY_MOVING))
448 return PA_HOOK_OK;
449
450 return process(u, PA_OBJECT(o), FALSE);
451 }
452
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);
456
457 return process(u, PA_OBJECT(o), FALSE);
458 }
459
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);
463
464 pa_assert(u);
465
466 if (pa_hashmap_size(u->filters) > 0)
467 trigger_housekeeping(u);
468
469 return PA_HOOK_OK;
470 }
471
472 static pa_hook_result_t source_unlink_cb(pa_core *core, pa_source *source, struct userdata *u) {
473 void *state;
474 struct filter *filter = NULL;
475
476 pa_core_assert_ref(core);
477 pa_source_assert_ref(source);
478 pa_assert(u);
479
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)) {
484 uint32_t idx;
485
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)) {
490 pa_source_output *o;
491
492 PA_IDXSET_FOREACH(o, source->outputs, idx)
493 move_object_for_filter(PA_OBJECT(o), filter, TRUE, FALSE);
494 }
495
496 idx = filter->module_index;
497 pa_hashmap_remove(u->filters, filter);
498 filter_free(filter);
499
500 if (can_unload_module(u, idx))
501 pa_module_unload_request_by_index(u->core, idx, TRUE);
502 }
503 }
504
505 return PA_HOOK_OK;
506 }
507
508 int pa__init(pa_module *m) {
509 pa_modargs *ma = NULL;
510 struct userdata *u;
511
512 pa_assert(m);
513
514 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
515 pa_log("Failed to parse module arguments");
516 goto fail;
517 }
518
519 m->userdata = u = pa_xnew0(struct userdata, 1);
520
521 u->core = m->core;
522
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");
526 goto fail;
527 }
528
529 u->filters = pa_hashmap_new(filter_hash, filter_compare);
530
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);
541
542 pa_modargs_free(ma);
543
544 return 0;
545
546 fail:
547 pa__done(m);
548
549 if (ma)
550 pa_modargs_free(ma);
551
552 return -1;
553 }
554
555 void pa__done(pa_module *m) {
556 struct userdata* u;
557
558 pa_assert(m);
559
560 if (!(u = m->userdata))
561 return;
562
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);
583
584 if (u->housekeeping_time_event)
585 u->core->mainloop->time_free(u->housekeeping_time_event);
586
587 if (u->filters) {
588 struct filter *f;
589
590 while ((f = pa_hashmap_steal_first(u->filters))) {
591 pa_module_unload_request_by_index(u->core, f->module_index, TRUE);
592 filter_free(f);
593 }
594
595 pa_hashmap_free(u->filters, NULL, NULL);
596 }
597
598 pa_xfree(u);
599 }