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