filter-apply: Make housekeeping optional
[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/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
41 PA_MODULE_AUTHOR("Colin Guthrie");
42 PA_MODULE_DESCRIPTION("Load filter sinks automatically when needed");
43 PA_MODULE_VERSION(PACKAGE_VERSION);
44 PA_MODULE_LOAD_ONCE(TRUE);
45 PA_MODULE_USAGE(_("autoclean=<automatically unload unused filters?>"));
46
47 static const char* const valid_modargs[] = {
48     "autoclean",
49     NULL
50 };
51
52 #define DEFAULT_AUTOCLEAN TRUE
53 #define HOUSEKEEPING_INTERVAL (10 * PA_USEC_PER_SEC)
54
55 struct filter {
56     char *name;
57     pa_sink* parent_sink;
58     uint32_t module_index;
59     pa_sink* sink;
60 };
61
62 struct userdata {
63     pa_core *core;
64     pa_hashmap *filters;
65     pa_hook_slot
66         *sink_input_put_slot,
67         *sink_input_proplist_slot,
68         *sink_input_unlink_slot,
69         *sink_unlink_slot;
70     pa_bool_t autoclean;
71     pa_time_event *housekeeping_time_event;
72 };
73
74 static unsigned filter_hash(const void *p) {
75     const struct filter *f = p;
76
77     return
78         (unsigned) f->parent_sink->index +
79         pa_idxset_string_hash_func(f->name);
80 }
81
82 static int filter_compare(const void *a, const void *b) {
83     const struct filter *fa = a, *fb = b;
84     int r;
85
86     if (fa->parent_sink != fb->parent_sink)
87         return 1;
88     if ((r = strcmp(fa->name, fb->name)))
89         return r;
90
91     return 0;
92 }
93
94 static struct filter *filter_new(const char *name, pa_sink* parent_sink) {
95     struct filter *f;
96
97     f = pa_xnew(struct filter, 1);
98     f->name = pa_xstrdup(name);
99     pa_assert_se(f->parent_sink = parent_sink);
100     f->module_index = PA_INVALID_INDEX;
101     f->sink = NULL;
102     return f;
103 }
104
105 static void filter_free(struct filter *f) {
106     pa_assert(f);
107
108     pa_xfree(f->name);
109     pa_xfree(f);
110 }
111
112 static const char* should_filter(pa_sink_input *i) {
113     const char *want;
114
115     /* If the stream doesn't what any filter, then let it be. */
116     if ((want = pa_proplist_gets(i->proplist, PA_PROP_FILTER_WANT)) && !pa_streq(want, "")) {
117         const char* suppress = pa_proplist_gets(i->proplist, PA_PROP_FILTER_SUPPRESS);
118
119         if (!suppress || !pa_streq(suppress, want))
120             return want;
121     }
122
123     return NULL;
124 }
125
126 static void housekeeping_time_callback(pa_mainloop_api*a, pa_time_event* e, const struct timeval *t, void *userdata) {
127     struct userdata *u = userdata;
128     struct filter *filter;
129     void *state;
130
131     pa_assert(a);
132     pa_assert(e);
133     pa_assert(u);
134
135     pa_assert(e == u->housekeeping_time_event);
136     u->core->mainloop->time_free(u->housekeeping_time_event);
137     u->housekeeping_time_event = NULL;
138
139     PA_HASHMAP_FOREACH(filter, u->filters, state) {
140         if (filter->sink && pa_idxset_size(filter->sink->inputs) == 0) {
141             uint32_t idx;
142
143             pa_log_debug("Detected filter %s as no longer used on sink %s. Unloading.", filter->name, filter->sink->name);
144             idx = filter->module_index;
145             pa_hashmap_remove(u->filters, filter);
146             filter_free(filter);
147             pa_module_unload_request_by_index(u->core, idx, TRUE);
148         }
149     }
150
151     pa_log_info("Housekeeping Done.");
152 }
153
154 static void trigger_housekeeping(struct userdata *u) {
155     pa_assert(u);
156
157     if (!u->autoclean)
158         return;
159
160     if (u->housekeeping_time_event)
161         return;
162
163     u->housekeeping_time_event = pa_core_rttime_new(u->core, pa_rtclock_now() + HOUSEKEEPING_INTERVAL, housekeeping_time_callback, u);
164 }
165
166 static void move_input_for_filter(pa_sink_input *i, struct filter* filter, pa_bool_t restore) {
167     pa_sink *sink;
168
169     pa_assert(i);
170     pa_assert(filter);
171
172     pa_assert_se(sink = (restore ? filter->parent_sink : filter->sink));
173
174     if (pa_sink_input_move_to(i, sink, FALSE) < 0)
175         pa_log_info("Failed to move sink input %u \"%s\" to <%s>.", i->index,
176                     pa_strnull(pa_proplist_gets(i->proplist, PA_PROP_APPLICATION_NAME)), sink->name);
177     else
178         pa_log_info("Sucessfully moved sink input %u \"%s\" to <%s>.", i->index,
179                     pa_strnull(pa_proplist_gets(i->proplist, PA_PROP_APPLICATION_NAME)), sink->name);
180 }
181
182 static pa_hook_result_t process(struct userdata *u, pa_sink_input *i) {
183     const char *want;
184     pa_bool_t done_something = FALSE;
185
186     pa_assert(u);
187     pa_sink_input_assert_ref(i);
188
189     /* If there is no sink yet, we can't do much */
190     if (!i->sink)
191         return PA_HOOK_OK;
192
193     /* If the stream doesn't what any filter, then let it be. */
194     if ((want = should_filter(i))) {
195         char *module_name;
196         struct filter *fltr, *filter;
197
198         /* We need to ensure the SI is playing on a sink of this type
199          * attached to the sink it's "officially" playing on */
200
201         if (!i->sink->module)
202             return PA_HOOK_OK;
203
204         module_name = pa_sprintf_malloc("module-%s", want);
205         if (pa_streq(i->sink->module->name, module_name)) {
206             pa_log_debug("Stream appears to be playing on an appropriate sink already. Ignoring.");
207             pa_xfree(module_name);
208             return PA_HOOK_OK;
209         }
210
211         fltr = filter_new(want, i->sink);
212
213         if (!(filter = pa_hashmap_get(u->filters, fltr))) {
214             char *args;
215             pa_module *m;
216
217             args = pa_sprintf_malloc("sink_master=%s", i->sink->name);
218             pa_log_debug("Loading %s with arguments '%s'", module_name, args);
219
220             if ((m = pa_module_load(u->core, module_name, args))) {
221                 uint32_t idx;
222                 pa_sink *sink;
223
224                 fltr->module_index = m->index;
225                 /* We cannot use the SINK_PUT hook here to detect our sink as it'll
226                  * be called during the module load so we wont yet have put the filter
227                  * in our hashmap to compare... so we have to search for it */
228                 PA_IDXSET_FOREACH(sink, u->core->sinks, idx) {
229                     if (sink->module == m) {
230                         fltr->sink = sink;
231                         break;
232                     }
233                 }
234                 pa_hashmap_put(u->filters, fltr, fltr);
235                 filter = fltr;
236                 fltr = NULL;
237                 done_something = TRUE;
238             }
239             pa_xfree(args);
240         }
241         pa_xfree(fltr);
242
243         if (!filter) {
244             pa_log("Unable to load %s for sink <%s>", module_name, i->sink->name);
245             pa_xfree(module_name);
246             return PA_HOOK_OK;
247         }
248         pa_xfree(module_name);
249
250         if (filter->sink) {
251             /* We can move the sink_input now as the know the destination.
252              * If this isn't true, we will do it later when the sink appears. */
253             move_input_for_filter(i, filter, FALSE);
254             done_something = TRUE;
255         }
256     } else {
257         void *state;
258         struct filter *filter = NULL;
259
260         /* We do not want to filter... but are we already filtered?
261          * This can happen if an input's proplist changes */
262         PA_HASHMAP_FOREACH(filter, u->filters, state) {
263             if (i->sink == filter->sink) {
264                 move_input_for_filter(i, filter, TRUE);
265                 done_something = TRUE;
266                 break;
267             }
268         }
269     }
270
271     if (done_something)
272         trigger_housekeeping(u);
273
274     return PA_HOOK_OK;
275 }
276
277 static pa_hook_result_t sink_input_put_cb(pa_core *core, pa_sink_input *i, struct userdata *u) {
278     pa_core_assert_ref(core);
279     pa_sink_input_assert_ref(i);
280
281     return process(u, i);
282 }
283
284 static pa_hook_result_t sink_input_proplist_cb(pa_core *core, pa_sink_input *i, struct userdata *u) {
285     pa_core_assert_ref(core);
286     pa_sink_input_assert_ref(i);
287
288     return process(u, i);
289 }
290
291 static pa_hook_result_t sink_input_unlink_cb(pa_core *core, pa_sink_input *i, struct userdata *u) {
292     pa_core_assert_ref(core);
293     pa_sink_input_assert_ref(i);
294
295     pa_assert(u);
296
297     if (pa_hashmap_size(u->filters) > 0)
298         trigger_housekeeping(u);
299
300     return PA_HOOK_OK;
301 }
302
303 static pa_hook_result_t sink_unlink_cb(pa_core *core, pa_sink *sink, struct userdata *u) {
304     void *state;
305     struct filter *filter = NULL;
306
307     pa_core_assert_ref(core);
308     pa_sink_assert_ref(sink);
309     pa_assert(u);
310
311     /* If either the parent or the sink we've loaded disappears,
312      * we should remove it from our hashmap */
313     PA_HASHMAP_FOREACH(filter, u->filters, state) {
314         if (filter->parent_sink == sink || filter->sink == sink) {
315             uint32_t idx;
316
317             /* Attempt to rescue any streams to the parent sink as this is likely
318              * the best course of action (as opposed to a generic rescue via
319              * module-rescue-streams */
320             if (filter->sink == sink) {
321                 pa_sink_input *i;
322
323                 PA_IDXSET_FOREACH(i, sink->inputs, idx)
324                     move_input_for_filter(i, filter, TRUE);
325             }
326
327             idx = filter->module_index;
328             pa_hashmap_remove(u->filters, filter);
329             filter_free(filter);
330             pa_module_unload_request_by_index(u->core, idx, TRUE);
331         }
332     }
333
334     return PA_HOOK_OK;
335 }
336
337
338 int pa__init(pa_module *m) {
339     pa_modargs *ma = NULL;
340     struct userdata *u;
341
342     pa_assert(m);
343
344     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
345         pa_log("Failed to parse module arguments");
346         goto fail;
347     }
348
349     m->userdata = u = pa_xnew0(struct userdata, 1);
350
351     u->core = m->core;
352
353     u->autoclean = DEFAULT_AUTOCLEAN;
354     if (pa_modargs_get_value_boolean(ma, "autoclean", &u->autoclean) < 0) {
355         pa_log("Failed to parse autoclean value");
356         goto fail;
357     }
358
359     u->filters = pa_hashmap_new(filter_hash, filter_compare);
360
361     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);
362     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);
363     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);
364     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);
365
366     pa_modargs_free(ma);
367
368     return 0;
369
370 fail:
371     pa__done(m);
372
373     if (ma)
374         pa_modargs_free(ma);
375
376     return -1;
377 }
378
379 void pa__done(pa_module *m) {
380     struct userdata* u;
381
382     pa_assert(m);
383
384     if (!(u = m->userdata))
385         return;
386
387     if (u->sink_input_put_slot)
388         pa_hook_slot_free(u->sink_input_put_slot);
389     if (u->sink_input_proplist_slot)
390         pa_hook_slot_free(u->sink_input_proplist_slot);
391     if (u->sink_input_unlink_slot)
392         pa_hook_slot_free(u->sink_input_unlink_slot);
393     if (u->sink_unlink_slot)
394         pa_hook_slot_free(u->sink_unlink_slot);
395
396     if (u->housekeeping_time_event)
397         u->core->mainloop->time_free(u->housekeeping_time_event);
398
399     if (u->filters) {
400         struct filter *f;
401
402         while ((f = pa_hashmap_steal_first(u->filters))) {
403             pa_module_unload_request_by_index(u->core, f->module_index, TRUE);
404             filter_free(f);
405         }
406
407         pa_hashmap_free(u->filters, NULL, NULL);
408     }
409
410     pa_xfree(u);
411 }