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