Remove unnecessary #includes
[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 #include <pulse/xmalloc.h>
30
31 #include <pulsecore/macro.h>
32 #include <pulsecore/hashmap.h>
33 #include <pulsecore/hook-list.h>
34 #include <pulsecore/core.h>
35 #include <pulsecore/core-util.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_bool_t is_sink;
61     pa_object *parent_obj;      /* source or sink that the filter is connected to */
62     pa_object *obj;             /* source or sink of the filter */
63 };
64
65 struct userdata {
66     pa_core *core;
67     pa_hashmap *filters;
68     pa_hook_slot
69         *sink_input_put_slot,
70         *sink_input_move_finish_slot,
71         *sink_input_proplist_slot,
72         *sink_input_unlink_slot,
73         *sink_unlink_slot,
74         *source_output_put_slot,
75         *source_output_move_finish_slot,
76         *source_output_proplist_slot,
77         *source_output_unlink_slot,
78         *source_unlink_slot;
79     pa_bool_t autoclean;
80     pa_time_event *housekeeping_time_event;
81 };
82
83 static unsigned filter_hash(const void *p) {
84     const struct filter *f = p;
85
86     if (f->is_sink)
87         return (unsigned) (PA_SINK(f->parent_obj)->index + pa_idxset_string_hash_func(f->name));
88     else
89         return (unsigned) ((PA_SOURCE(f->parent_obj)->index << 16) + pa_idxset_string_hash_func(f->name));
90 }
91
92 static int filter_compare(const void *a, const void *b) {
93     const struct filter *fa = a, *fb = b;
94     int r;
95
96     if (fa->parent_obj != fb->parent_obj)
97         return 1;
98     if ((r = strcmp(fa->name, fb->name)))
99         return r;
100
101     return 0;
102 }
103
104 static struct filter *filter_new(const char *name, pa_object* parent_obj, pa_bool_t is_sink) {
105     struct filter *f;
106
107     f = pa_xnew(struct filter, 1);
108     f->name = pa_xstrdup(name);
109     pa_assert_se(f->parent_obj = parent_obj);
110     f->is_sink = is_sink;
111     f->module_index = PA_INVALID_INDEX;
112     f->obj = NULL;
113     return f;
114 }
115
116 static void filter_free(struct filter *f) {
117     pa_assert(f);
118
119     pa_xfree(f->name);
120     pa_xfree(f);
121 }
122
123 static const char* should_filter(pa_object *o, pa_bool_t is_sink_input) {
124     const char *apply;
125     pa_proplist *pl;
126
127     if (is_sink_input)
128         pl = PA_SINK_INPUT(o)->proplist;
129     else
130         pl = PA_SOURCE_OUTPUT(o)->proplist;
131
132     /* If the stream doesn't what any filter, then let it be. */
133     if ((apply = pa_proplist_gets(pl, PA_PROP_FILTER_APPLY)) && !pa_streq(apply, "")) {
134         const char* suppress = pa_proplist_gets(pl, PA_PROP_FILTER_SUPPRESS);
135
136         if (!suppress || !pa_streq(suppress, apply))
137             return apply;
138     }
139
140     return NULL;
141 }
142
143 static pa_bool_t nothing_attached(pa_object *obj, pa_bool_t is_sink)
144 {
145     if (is_sink)
146         return pa_idxset_isempty(PA_SINK(obj)->inputs);
147     else
148         return pa_idxset_isempty(PA_SOURCE(obj)->outputs);
149 }
150
151 static void housekeeping_time_callback(pa_mainloop_api*a, pa_time_event* e, const struct timeval *t, void *userdata) {
152     struct userdata *u = userdata;
153     struct filter *filter;
154     void *state;
155
156     pa_assert(a);
157     pa_assert(e);
158     pa_assert(u);
159
160     pa_assert(e == u->housekeeping_time_event);
161     u->core->mainloop->time_free(u->housekeeping_time_event);
162     u->housekeeping_time_event = NULL;
163
164     PA_HASHMAP_FOREACH(filter, u->filters, state) {
165         if (filter->obj && nothing_attached(filter->obj, filter->is_sink)) {
166             uint32_t idx;
167
168             pa_log_debug("Detected filter %s as no longer used. Unloading.", filter->name);
169             idx = filter->module_index;
170             pa_hashmap_remove(u->filters, filter);
171             filter_free(filter);
172             pa_module_unload_request_by_index(u->core, idx, TRUE);
173         }
174     }
175
176     pa_log_info("Housekeeping Done.");
177 }
178
179 static void trigger_housekeeping(struct userdata *u) {
180     pa_assert(u);
181
182     if (!u->autoclean)
183         return;
184
185     if (u->housekeeping_time_event)
186         return;
187
188     u->housekeeping_time_event = pa_core_rttime_new(u->core, pa_rtclock_now() + HOUSEKEEPING_INTERVAL, housekeeping_time_callback, u);
189 }
190
191 static int do_move(pa_object *obj, pa_object *parent, pa_bool_t restore, pa_bool_t is_input) {
192     if (is_input)
193         return pa_sink_input_move_to(PA_SINK_INPUT(obj), PA_SINK(parent), restore);
194     else
195         return pa_source_output_move_to(PA_SOURCE_OUTPUT(obj), PA_SOURCE(parent), restore);
196 }
197
198 static void move_object_for_filter(pa_object *o, struct filter* filter, pa_bool_t restore, pa_bool_t is_sink_input) {
199     pa_object *parent;
200     pa_proplist *pl;
201     const char *name;
202
203     pa_assert(o);
204     pa_assert(filter);
205
206     pa_assert_se(parent = (restore ? filter->parent_obj : filter->obj));
207
208     if (is_sink_input) {
209         pl = PA_SINK_INPUT(o)->proplist;
210         name = PA_SINK(parent)->name;
211     } else {
212         pl = PA_SOURCE_OUTPUT(o)->proplist;
213         name = PA_SOURCE(parent)->name;
214     }
215
216     pa_proplist_sets(pl, PA_PROP_FILTER_APPLY_MOVING, "1");
217
218     if (do_move(o, parent, FALSE, is_sink_input) < 0)
219         pa_log_info("Failed to move %s for \"%s\" to <%s>.", is_sink_input ? "sink-input" : "source-output",
220                     pa_strnull(pa_proplist_gets(pl, PA_PROP_APPLICATION_NAME)), name);
221     else
222         pa_log_info("Sucessfully moved %s for \"%s\" to <%s>.", is_sink_input ? "sink-input" : "source-output",
223                     pa_strnull(pa_proplist_gets(pl, PA_PROP_APPLICATION_NAME)), name);
224
225     pa_proplist_unset(pl, PA_PROP_FILTER_APPLY_MOVING);
226 }
227
228 static void find_filters_for_module(struct userdata *u, pa_module *m, const char *name) {
229     uint32_t idx;
230     pa_sink *sink;
231     pa_source *source;
232     struct filter *fltr;
233
234     PA_IDXSET_FOREACH(sink, u->core->sinks, idx) {
235         if (sink->module == m) {
236             pa_assert(sink->input_to_master != NULL);
237
238             fltr = filter_new(name, PA_OBJECT(sink->input_to_master->sink), TRUE);
239             fltr->module_index = m->index;
240             fltr->obj = PA_OBJECT(sink);
241
242             pa_hashmap_put(u->filters, fltr, fltr);
243         }
244     }
245
246     PA_IDXSET_FOREACH(source, u->core->sources, idx) {
247         if (source->module == m && !source->monitor_of) {
248             pa_assert(source->output_from_master != NULL);
249
250             fltr = filter_new(name, PA_OBJECT(source->output_from_master->source), FALSE);
251             fltr->module_index = m->index;
252             fltr->obj = PA_OBJECT(source);
253
254             pa_hashmap_put(u->filters, fltr, fltr);
255         }
256     }
257 }
258
259 static pa_bool_t can_unload_module(struct userdata *u, uint32_t idx) {
260     void *state;
261     struct filter *filter;
262
263     /* Check if any other struct filters point to the same module */
264     PA_HASHMAP_FOREACH(filter, u->filters, state) {
265         if (filter->module_index == idx && !nothing_attached(filter->obj, pa_sink_isinstance(filter->obj)))
266             return FALSE;
267     }
268
269     return TRUE;
270 }
271
272 static pa_hook_result_t process(struct userdata *u, pa_object *o, pa_bool_t is_sink_input) {
273     const char *want;
274     pa_bool_t done_something = FALSE;
275
276     pa_object *parent; /* source/sink of the given source-output/sink-input */
277     const char *parent_name;
278     pa_module *module;
279
280     if (is_sink_input) {
281         parent = PA_OBJECT(PA_SINK_INPUT(o)->sink);
282         parent_name = PA_SINK_INPUT(o)->sink->name;
283         module = PA_SINK_INPUT(o)->sink->module;
284     } else {
285         parent = PA_OBJECT(PA_SOURCE_OUTPUT(o)->source);
286         parent_name = PA_SOURCE_OUTPUT(o)->source->name;
287         module = PA_SOURCE_OUTPUT(o)->source->module;
288     }
289
290     /* If there is no sink yet, we can't do much */
291     if (!parent)
292         return PA_HOOK_OK;
293
294     /* If the stream doesn't what any filter, then let it be. */
295     if ((want = should_filter(o, is_sink_input))) {
296         char *module_name;
297         struct filter *fltr, *filter;
298
299         /* We need to ensure the SI is playing on a sink of this type
300          * attached to the sink it's "officially" playing on */
301
302         if (!module)
303             return PA_HOOK_OK;
304
305         module_name = pa_sprintf_malloc("module-%s", want);
306         if (pa_streq(module->name, module_name)) {
307             pa_log_debug("Stream appears to be playing on an appropriate sink already. Ignoring.");
308             pa_xfree(module_name);
309             return PA_HOOK_OK;
310         }
311
312         fltr = filter_new(want, parent, is_sink_input);
313
314         if (!(filter = pa_hashmap_get(u->filters, fltr))) {
315             char *args;
316             pa_module *m;
317
318             args = pa_sprintf_malloc("autoloaded=1 %s_master=%s", is_sink_input ? "sink" : "source", parent_name);
319             pa_log_debug("Loading %s with arguments '%s'", module_name, args);
320
321             if ((m = pa_module_load(u->core, module_name, args))) {
322                 find_filters_for_module(u, m, want);
323                 filter = pa_hashmap_get(u->filters, fltr);
324                 done_something = TRUE;
325             }
326             pa_xfree(args);
327         }
328
329         pa_xfree(fltr);
330
331         if (!filter) {
332             pa_log("Unable to load %s for <%s>", module_name, parent_name);
333             pa_xfree(module_name);
334             return PA_HOOK_OK;
335         }
336         pa_xfree(module_name);
337
338         if (filter->obj) {
339             /* We can move the sink_input now as the know the destination.
340              * If this isn't true, we will do it later when the sink appears. */
341             move_object_for_filter(o, filter, FALSE, is_sink_input);
342             done_something = TRUE;
343         }
344     } else {
345         void *state;
346         struct filter *filter = NULL;
347
348         /* We do not want to filter... but are we already filtered?
349          * This can happen if an input's proplist changes */
350         PA_HASHMAP_FOREACH(filter, u->filters, state) {
351             if (parent == filter->obj) {
352                 move_object_for_filter(o, filter, TRUE, is_sink_input);
353                 done_something = TRUE;
354                 break;
355             }
356         }
357     }
358
359     if (done_something)
360         trigger_housekeeping(u);
361
362     return PA_HOOK_OK;
363 }
364
365 static pa_hook_result_t sink_input_put_cb(pa_core *core, pa_sink_input *i, struct userdata *u) {
366     pa_core_assert_ref(core);
367     pa_sink_input_assert_ref(i);
368
369     return process(u, PA_OBJECT(i), TRUE);
370 }
371
372 static pa_hook_result_t sink_input_move_finish_cb(pa_core *core, pa_sink_input *i, struct userdata *u) {
373     pa_core_assert_ref(core);
374     pa_sink_input_assert_ref(i);
375
376     if (pa_proplist_gets(i->proplist, PA_PROP_FILTER_APPLY_MOVING))
377         return PA_HOOK_OK;
378
379     return process(u, PA_OBJECT(i), TRUE);
380 }
381
382 static pa_hook_result_t sink_input_proplist_cb(pa_core *core, pa_sink_input *i, struct userdata *u) {
383     pa_core_assert_ref(core);
384     pa_sink_input_assert_ref(i);
385
386     return process(u, PA_OBJECT(i), TRUE);
387 }
388
389 static pa_hook_result_t sink_input_unlink_cb(pa_core *core, pa_sink_input *i, struct userdata *u) {
390     pa_core_assert_ref(core);
391     pa_sink_input_assert_ref(i);
392
393     pa_assert(u);
394
395     if (pa_hashmap_size(u->filters) > 0)
396         trigger_housekeeping(u);
397
398     return PA_HOOK_OK;
399 }
400
401 static pa_hook_result_t sink_unlink_cb(pa_core *core, pa_sink *sink, struct userdata *u) {
402     void *state;
403     struct filter *filter = NULL;
404
405     pa_core_assert_ref(core);
406     pa_sink_assert_ref(sink);
407     pa_assert(u);
408
409     /* If either the parent or the sink we've loaded disappears,
410      * we should remove it from our hashmap */
411     PA_HASHMAP_FOREACH(filter, u->filters, state) {
412         if (filter->parent_obj == PA_OBJECT(sink) || filter->obj == PA_OBJECT(sink)) {
413             uint32_t idx;
414
415             /* Attempt to rescue any streams to the parent sink as this is likely
416              * the best course of action (as opposed to a generic rescue via
417              * module-rescue-streams */
418             if (filter->obj == PA_OBJECT(sink)) {
419                 pa_sink_input *i;
420
421                 PA_IDXSET_FOREACH(i, sink->inputs, idx)
422                     move_object_for_filter(PA_OBJECT(i), filter, TRUE, TRUE);
423             }
424
425             idx = filter->module_index;
426             pa_hashmap_remove(u->filters, filter);
427             filter_free(filter);
428
429             if (can_unload_module(u, idx))
430                 pa_module_unload_request_by_index(u->core, idx, TRUE);
431         }
432     }
433
434     return PA_HOOK_OK;
435 }
436
437 static pa_hook_result_t source_output_put_cb(pa_core *core, pa_source_output *o, struct userdata *u) {
438     pa_core_assert_ref(core);
439     pa_source_output_assert_ref(o);
440
441     return process(u, PA_OBJECT(o), FALSE);
442 }
443
444 static pa_hook_result_t source_output_move_finish_cb(pa_core *core, pa_source_output *o, struct userdata *u) {
445     pa_core_assert_ref(core);
446     pa_source_output_assert_ref(o);
447
448     if (pa_proplist_gets(o->proplist, PA_PROP_FILTER_APPLY_MOVING))
449         return PA_HOOK_OK;
450
451     return process(u, PA_OBJECT(o), FALSE);
452 }
453
454 static pa_hook_result_t source_output_proplist_cb(pa_core *core, pa_source_output *o, struct userdata *u) {
455     pa_core_assert_ref(core);
456     pa_source_output_assert_ref(o);
457
458     return process(u, PA_OBJECT(o), FALSE);
459 }
460
461 static pa_hook_result_t source_output_unlink_cb(pa_core *core, pa_source_output *o, struct userdata *u) {
462     pa_core_assert_ref(core);
463     pa_source_output_assert_ref(o);
464
465     pa_assert(u);
466
467     if (pa_hashmap_size(u->filters) > 0)
468         trigger_housekeeping(u);
469
470     return PA_HOOK_OK;
471 }
472
473 static pa_hook_result_t source_unlink_cb(pa_core *core, pa_source *source, struct userdata *u) {
474     void *state;
475     struct filter *filter = NULL;
476
477     pa_core_assert_ref(core);
478     pa_source_assert_ref(source);
479     pa_assert(u);
480
481     /* If either the parent or the source we've loaded disappears,
482      * we should remove it from our hashmap */
483     PA_HASHMAP_FOREACH(filter, u->filters, state) {
484         if (filter->parent_obj == PA_OBJECT(source) || filter->obj == PA_OBJECT(source)) {
485             uint32_t idx;
486
487             /* Attempt to rescue any streams to the parent source as this is likely
488              * the best course of action (as opposed to a generic rescue via
489              * module-rescue-streams */
490             if (filter->obj == PA_OBJECT(source)) {
491                 pa_source_output *o;
492
493                 PA_IDXSET_FOREACH(o, source->outputs, idx)
494                     move_object_for_filter(PA_OBJECT(o), filter, TRUE, FALSE);
495             }
496
497             idx = filter->module_index;
498             pa_hashmap_remove(u->filters, filter);
499             filter_free(filter);
500
501             if (can_unload_module(u, idx))
502                 pa_module_unload_request_by_index(u->core, idx, TRUE);
503         }
504     }
505
506     return PA_HOOK_OK;
507 }
508
509 int pa__init(pa_module *m) {
510     pa_modargs *ma = NULL;
511     struct userdata *u;
512
513     pa_assert(m);
514
515     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
516         pa_log("Failed to parse module arguments");
517         goto fail;
518     }
519
520     m->userdata = u = pa_xnew0(struct userdata, 1);
521
522     u->core = m->core;
523
524     u->autoclean = DEFAULT_AUTOCLEAN;
525     if (pa_modargs_get_value_boolean(ma, "autoclean", &u->autoclean) < 0) {
526         pa_log("Failed to parse autoclean value");
527         goto fail;
528     }
529
530     u->filters = pa_hashmap_new(filter_hash, filter_compare);
531
532     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);
533     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);
534     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);
535     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);
536     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);
537     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);
538     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);
539     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);
540     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);
541     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);
542
543     pa_modargs_free(ma);
544
545     return 0;
546
547 fail:
548     pa__done(m);
549
550     if (ma)
551         pa_modargs_free(ma);
552
553     return -1;
554 }
555
556 void pa__done(pa_module *m) {
557     struct userdata* u;
558
559     pa_assert(m);
560
561     if (!(u = m->userdata))
562         return;
563
564     if (u->sink_input_put_slot)
565         pa_hook_slot_free(u->sink_input_put_slot);
566     if (u->sink_input_move_finish_slot)
567         pa_hook_slot_free(u->sink_input_move_finish_slot);
568     if (u->sink_input_proplist_slot)
569         pa_hook_slot_free(u->sink_input_proplist_slot);
570     if (u->sink_input_unlink_slot)
571         pa_hook_slot_free(u->sink_input_unlink_slot);
572     if (u->sink_unlink_slot)
573         pa_hook_slot_free(u->sink_unlink_slot);
574     if (u->source_output_put_slot)
575         pa_hook_slot_free(u->source_output_put_slot);
576     if (u->source_output_move_finish_slot)
577         pa_hook_slot_free(u->source_output_move_finish_slot);
578     if (u->source_output_proplist_slot)
579         pa_hook_slot_free(u->source_output_proplist_slot);
580     if (u->source_output_unlink_slot)
581         pa_hook_slot_free(u->source_output_unlink_slot);
582     if (u->source_unlink_slot)
583         pa_hook_slot_free(u->source_unlink_slot);
584
585     if (u->housekeeping_time_event)
586         u->core->mainloop->time_free(u->housekeeping_time_event);
587
588     if (u->filters) {
589         struct filter *f;
590
591         while ((f = pa_hashmap_steal_first(u->filters))) {
592             pa_module_unload_request_by_index(u->core, f->module_index, TRUE);
593             filter_free(f);
594         }
595
596         pa_hashmap_free(u->filters, NULL, NULL);
597     }
598
599     pa_xfree(u);
600 }