filters: Allow a filter to have both sink and source
[platform/upstream/pulseaudio.git] / 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/xmalloc.h>
29
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
39 #include "module-filter-apply-symdef.h"
40
41 #define PA_PROP_FILTER_APPLY_MOVING "filter.apply.moving"
42
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?>"));
48
49 static const char* const valid_modargs[] = {
50     "autoclean",
51     NULL
52 };
53
54 #define DEFAULT_AUTOCLEAN TRUE
55 #define HOUSEKEEPING_INTERVAL (10 * PA_USEC_PER_SEC)
56
57 struct filter {
58     char *name;
59     uint32_t module_index;
60     pa_sink *sink;
61     pa_sink *sink_master;
62     pa_source *source;
63     pa_source *source_master;
64 };
65
66 struct userdata {
67     pa_core *core;
68     pa_hashmap *filters;
69     pa_hook_slot
70         *sink_input_put_slot,
71         *sink_input_move_finish_slot,
72         *sink_input_proplist_slot,
73         *sink_input_unlink_slot,
74         *sink_unlink_slot,
75         *source_output_put_slot,
76         *source_output_move_finish_slot,
77         *source_output_proplist_slot,
78         *source_output_unlink_slot,
79         *source_unlink_slot;
80     pa_bool_t autoclean;
81     pa_time_event *housekeeping_time_event;
82 };
83
84 static unsigned filter_hash(const void *p) {
85     const struct filter *f = p;
86
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));
91     else
92         pa_assert_not_reached();
93 }
94
95 static int filter_compare(const void *a, const void *b) {
96     const struct filter *fa = a, *fb = b;
97     int r;
98
99     if (fa->sink_master != fb->sink_master || fa->source_master != fb->source_master)
100         return 1;
101     if ((r = strcmp(fa->name, fb->name)))
102         return r;
103
104     return 0;
105 }
106
107 static struct filter *filter_new(const char *name, pa_sink *sink, pa_source *source) {
108     struct filter *f;
109
110     pa_assert(sink || source);
111
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;
117     f->sink = NULL;
118     f->source = NULL;
119
120     return f;
121 }
122
123 static void filter_free(struct filter *f) {
124     pa_assert(f);
125
126     pa_xfree(f->name);
127     pa_xfree(f);
128 }
129
130 static const char* should_filter(pa_object *o, pa_bool_t is_sink_input) {
131     const char *apply;
132     pa_proplist *pl;
133
134     if (is_sink_input)
135         pl = PA_SINK_INPUT(o)->proplist;
136     else
137         pl = PA_SOURCE_OUTPUT(o)->proplist;
138
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);
142
143         if (!suppress || !pa_streq(suppress, apply))
144             return apply;
145     }
146
147     return NULL;
148 }
149
150 static pa_bool_t nothing_attached(struct filter *f) {
151     pa_bool_t no_si = TRUE, no_so = TRUE;
152
153     if (f->sink)
154         no_si = pa_idxset_isempty(f->sink->inputs);
155     else if (f->source)
156         no_so = pa_idxset_isempty(f->source->outputs);
157
158     return no_si && no_so;
159 }
160
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;
164     void *state;
165
166     pa_assert(a);
167     pa_assert(e);
168     pa_assert(u);
169
170     pa_assert(e == u->housekeeping_time_event);
171     u->core->mainloop->time_free(u->housekeeping_time_event);
172     u->housekeeping_time_event = NULL;
173
174     PA_HASHMAP_FOREACH(filter, u->filters, state) {
175         if (nothing_attached(filter)) {
176             uint32_t idx;
177
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);
181             filter_free(filter);
182             pa_module_unload_request_by_index(u->core, idx, TRUE);
183         }
184     }
185
186     pa_log_info("Housekeeping Done.");
187 }
188
189 static void trigger_housekeeping(struct userdata *u) {
190     pa_assert(u);
191
192     if (!u->autoclean)
193         return;
194
195     if (u->housekeeping_time_event)
196         return;
197
198     u->housekeeping_time_event = pa_core_rttime_new(u->core, pa_rtclock_now() + HOUSEKEEPING_INTERVAL, housekeeping_time_callback, u);
199 }
200
201 static int do_move(pa_object *obj, pa_object *parent, pa_bool_t restore, pa_bool_t is_input) {
202     if (is_input)
203         return pa_sink_input_move_to(PA_SINK_INPUT(obj), PA_SINK(parent), restore);
204     else
205         return pa_source_output_move_to(PA_SOURCE_OUTPUT(obj), PA_SOURCE(parent), restore);
206 }
207
208 static void move_object_for_filter(pa_object *o, struct filter* filter, pa_bool_t restore, pa_bool_t is_sink_input) {
209     pa_object *parent;
210     pa_proplist *pl;
211     const char *name;
212
213     pa_assert(o);
214     pa_assert(filter);
215
216     if (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;
220     } else {
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;
224     }
225
226     pa_proplist_sets(pl, PA_PROP_FILTER_APPLY_MOVING, "1");
227
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);
231     else
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);
234
235     pa_proplist_unset(pl, PA_PROP_FILTER_APPLY_MOVING);
236 }
237
238 static void find_filters_for_module(struct userdata *u, pa_module *m, const char *name) {
239     uint32_t idx;
240     pa_sink *sink;
241     pa_source *source;
242     struct filter *fltr;
243
244     PA_IDXSET_FOREACH(sink, u->core->sinks, idx) {
245         if (sink->module == m) {
246             pa_assert(sink->input_to_master != NULL);
247
248             fltr = filter_new(name, sink->input_to_master->sink, NULL);
249             fltr->module_index = m->index;
250             fltr->sink = sink;
251
252             pa_hashmap_put(u->filters, fltr, fltr);
253         }
254     }
255
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);
259
260             fltr = filter_new(name, NULL, source->output_from_master->source);
261             fltr->module_index = m->index;
262             fltr->source = source;
263
264             pa_hashmap_put(u->filters, fltr, fltr);
265         }
266     }
267 }
268
269 static pa_bool_t can_unload_module(struct userdata *u, uint32_t idx) {
270     void *state;
271     struct filter *filter;
272
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))
276             return FALSE;
277     }
278
279     return TRUE;
280 }
281
282 static pa_hook_result_t process(struct userdata *u, pa_object *o, pa_bool_t is_sink_input) {
283     const char *want;
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;
288     pa_module *module;
289
290     if (is_sink_input) {
291         sink = PA_SINK_INPUT(o)->sink;
292         sink_name = sink->name;
293         module = sink->module;
294     } else {
295         source = PA_SOURCE_OUTPUT(o)->source;
296         source_name = source->name;
297         module = source->module;
298     }
299
300     /* If there is no sink/source yet, we can't do much */
301     if ((is_sink_input && !sink) || (!is_sink_input && !source))
302         return PA_HOOK_OK;
303
304     /* If the stream doesn't what any filter, then let it be. */
305     if ((want = should_filter(o, is_sink_input))) {
306         char *module_name;
307         struct filter *fltr, *filter;
308
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 */
311
312         if (!module)
313             return PA_HOOK_OK;
314
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);
319             return PA_HOOK_OK;
320         }
321
322         fltr = filter_new(want, sink, source);
323
324         if (!(filter = pa_hashmap_get(u->filters, fltr))) {
325             char *args;
326             pa_module *m;
327
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);
331
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;
336             }
337             pa_xfree(args);
338         }
339
340         pa_xfree(fltr);
341
342         if (!filter) {
343             pa_log("Unable to load %s for <%s>", module_name, is_sink_input ? sink_name : source_name);
344             pa_xfree(module_name);
345             return PA_HOOK_OK;
346         }
347         pa_xfree(module_name);
348
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;
354         }
355     } else {
356         void *state;
357         struct filter *filter = NULL;
358
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;
365                 break;
366             }
367         }
368     }
369
370     if (done_something)
371         trigger_housekeeping(u);
372
373     return PA_HOOK_OK;
374 }
375
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);
379
380     return process(u, PA_OBJECT(i), TRUE);
381 }
382
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);
386
387     if (pa_proplist_gets(i->proplist, PA_PROP_FILTER_APPLY_MOVING))
388         return PA_HOOK_OK;
389
390     return process(u, PA_OBJECT(i), TRUE);
391 }
392
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);
396
397     return process(u, PA_OBJECT(i), TRUE);
398 }
399
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);
403
404     pa_assert(u);
405
406     if (pa_hashmap_size(u->filters) > 0)
407         trigger_housekeeping(u);
408
409     return PA_HOOK_OK;
410 }
411
412 static pa_hook_result_t sink_unlink_cb(pa_core *core, pa_sink *sink, struct userdata *u) {
413     void *state;
414     struct filter *filter = NULL;
415
416     pa_core_assert_ref(core);
417     pa_sink_assert_ref(sink);
418     pa_assert(u);
419
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) {
424             uint32_t idx;
425
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) {
430                 pa_sink_input *i;
431
432                 PA_IDXSET_FOREACH(i, sink->inputs, idx)
433                     move_object_for_filter(PA_OBJECT(i), filter, TRUE, TRUE);
434             }
435
436             idx = filter->module_index;
437             pa_hashmap_remove(u->filters, filter);
438             filter_free(filter);
439
440             if (can_unload_module(u, idx))
441                 pa_module_unload_request_by_index(u->core, idx, TRUE);
442         }
443     }
444
445     return PA_HOOK_OK;
446 }
447
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);
451
452     return process(u, PA_OBJECT(o), FALSE);
453 }
454
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);
458
459     if (pa_proplist_gets(o->proplist, PA_PROP_FILTER_APPLY_MOVING))
460         return PA_HOOK_OK;
461
462     return process(u, PA_OBJECT(o), FALSE);
463 }
464
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);
468
469     return process(u, PA_OBJECT(o), FALSE);
470 }
471
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);
475
476     pa_assert(u);
477
478     if (pa_hashmap_size(u->filters) > 0)
479         trigger_housekeeping(u);
480
481     return PA_HOOK_OK;
482 }
483
484 static pa_hook_result_t source_unlink_cb(pa_core *core, pa_source *source, struct userdata *u) {
485     void *state;
486     struct filter *filter = NULL;
487
488     pa_core_assert_ref(core);
489     pa_source_assert_ref(source);
490     pa_assert(u);
491
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) {
496             uint32_t idx;
497
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) {
502                 pa_source_output *o;
503
504                 PA_IDXSET_FOREACH(o, source->outputs, idx)
505                     move_object_for_filter(PA_OBJECT(o), filter, TRUE, FALSE);
506             }
507
508             idx = filter->module_index;
509             pa_hashmap_remove(u->filters, filter);
510             filter_free(filter);
511
512             if (can_unload_module(u, idx))
513                 pa_module_unload_request_by_index(u->core, idx, TRUE);
514         }
515     }
516
517     return PA_HOOK_OK;
518 }
519
520 int pa__init(pa_module *m) {
521     pa_modargs *ma = NULL;
522     struct userdata *u;
523
524     pa_assert(m);
525
526     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
527         pa_log("Failed to parse module arguments");
528         goto fail;
529     }
530
531     m->userdata = u = pa_xnew0(struct userdata, 1);
532
533     u->core = m->core;
534
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");
538         goto fail;
539     }
540
541     u->filters = pa_hashmap_new(filter_hash, filter_compare);
542
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);
553
554     pa_modargs_free(ma);
555
556     return 0;
557
558 fail:
559     pa__done(m);
560
561     if (ma)
562         pa_modargs_free(ma);
563
564     return -1;
565 }
566
567 void pa__done(pa_module *m) {
568     struct userdata* u;
569
570     pa_assert(m);
571
572     if (!(u = m->userdata))
573         return;
574
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);
595
596     if (u->housekeeping_time_event)
597         u->core->mainloop->time_free(u->housekeeping_time_event);
598
599     if (u->filters) {
600         struct filter *f;
601
602         while ((f = pa_hashmap_steal_first(u->filters))) {
603             pa_module_unload_request_by_index(u->core, f->module_index, TRUE);
604             filter_free(f);
605         }
606
607         pa_hashmap_free(u->filters, NULL, NULL);
608     }
609
610     pa_xfree(u);
611 }