hashmap: Add the ability to free keys
[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 #include <pulsecore/proplist-util.h>
39
40 #include "module-filter-apply-symdef.h"
41
42 #define PA_PROP_FILTER_APPLY_MOVING "filter.apply.moving"
43
44 PA_MODULE_AUTHOR("Colin Guthrie");
45 PA_MODULE_DESCRIPTION("Load filter sinks automatically when needed");
46 PA_MODULE_VERSION(PACKAGE_VERSION);
47 PA_MODULE_LOAD_ONCE(true);
48 PA_MODULE_USAGE(_("autoclean=<automatically unload unused filters?>"));
49
50 static const char* const valid_modargs[] = {
51     "autoclean",
52     NULL
53 };
54
55 #define DEFAULT_AUTOCLEAN true
56 #define HOUSEKEEPING_INTERVAL (10 * PA_USEC_PER_SEC)
57
58 struct filter {
59     char *name;
60     uint32_t module_index;
61     pa_sink *sink;
62     pa_sink *sink_master;
63     pa_source *source;
64     pa_source *source_master;
65 };
66
67 struct userdata {
68     pa_core *core;
69     pa_hashmap *filters;
70     pa_hook_slot
71         *sink_input_put_slot,
72         *sink_input_move_finish_slot,
73         *sink_input_proplist_slot,
74         *sink_input_unlink_slot,
75         *sink_unlink_slot,
76         *source_output_put_slot,
77         *source_output_move_finish_slot,
78         *source_output_proplist_slot,
79         *source_output_unlink_slot,
80         *source_unlink_slot;
81     bool autoclean;
82     pa_time_event *housekeeping_time_event;
83 };
84
85 static unsigned filter_hash(const void *p) {
86     const struct filter *f = p;
87
88     if (f->sink_master && !f->source_master)
89         return (unsigned) (f->sink_master->index + pa_idxset_string_hash_func(f->name));
90     else if (!f->sink_master && f->source_master)
91         return (unsigned) ((f->source_master->index << 16) + pa_idxset_string_hash_func(f->name));
92     else
93         return (unsigned) (f->sink_master->index + (f->source_master->index << 16) + pa_idxset_string_hash_func(f->name));
94 }
95
96 static int filter_compare(const void *a, const void *b) {
97     const struct filter *fa = a, *fb = b;
98     int r;
99
100     if (fa->sink_master != fb->sink_master || fa->source_master != fb->source_master)
101         return 1;
102     if ((r = strcmp(fa->name, fb->name)))
103         return r;
104
105     return 0;
106 }
107
108 static struct filter *filter_new(const char *name, pa_sink *sink, pa_source *source) {
109     struct filter *f;
110
111     pa_assert(sink || source);
112
113     f = pa_xnew(struct filter, 1);
114     f->name = pa_xstrdup(name);
115     f->sink_master = sink;
116     f->source_master = source;
117     f->module_index = PA_INVALID_INDEX;
118     f->sink = NULL;
119     f->source = NULL;
120
121     return f;
122 }
123
124 static void filter_free(struct filter *f) {
125     pa_assert(f);
126
127     pa_xfree(f->name);
128     pa_xfree(f);
129 }
130
131 static const char* should_filter(pa_object *o, bool is_sink_input) {
132     const char *apply;
133     pa_proplist *pl;
134
135     if (is_sink_input)
136         pl = PA_SINK_INPUT(o)->proplist;
137     else
138         pl = PA_SOURCE_OUTPUT(o)->proplist;
139
140     /* If the stream doesn't what any filter, then let it be. */
141     if ((apply = pa_proplist_gets(pl, PA_PROP_FILTER_APPLY)) && !pa_streq(apply, "")) {
142         const char* suppress = pa_proplist_gets(pl, PA_PROP_FILTER_SUPPRESS);
143
144         if (!suppress || !pa_streq(suppress, apply))
145             return apply;
146     }
147
148     return NULL;
149 }
150
151 static bool should_group_filter(struct filter *filter) {
152     return pa_streq(filter->name, "echo-cancel");
153 }
154
155 static char* get_group(pa_object *o, bool is_sink_input) {
156     pa_proplist *pl;
157
158     if (is_sink_input)
159         pl = PA_SINK_INPUT(o)->proplist;
160     else
161         pl = PA_SOURCE_OUTPUT(o)->proplist;
162
163     /* There's a bit of cleverness here -- the second argument ensures that we
164      * only group streams that require the same filter */
165     return pa_proplist_get_stream_group(pl, pa_proplist_gets(pl, PA_PROP_FILTER_APPLY), NULL);
166 }
167
168 /* For filters that apply on a source-output/sink-input pair, this finds the
169  * master sink if we know the master source, or vice versa. It does this by
170  * looking up streams that belong to the same stream group as the original
171  * object. The idea is that streams from the sam group are always routed
172  * together. */
173 static bool find_paired_master(struct userdata *u, struct filter *filter, pa_object *o, bool is_sink_input) {
174     char *group;
175
176     if ((group = get_group(o, is_sink_input))) {
177         uint32_t idx;
178         char *g;
179         char *module_name = pa_sprintf_malloc("module-%s", filter->name);
180
181         if (is_sink_input) {
182             pa_source_output *so;
183
184             PA_IDXSET_FOREACH(so, u->core->source_outputs, idx) {
185                 g = get_group(PA_OBJECT(so), false);
186
187                 if (pa_streq(g, group)) {
188                     if (pa_streq(module_name, so->source->module->name)) {
189                         /* Make sure we're not routing to another instance of
190                          * the same filter. */
191                         filter->source_master = so->source->output_from_master->source;
192                     } else {
193                         filter->source_master = so->source;
194                     }
195
196                     pa_xfree(g);
197                     break;
198                 }
199
200                 pa_xfree (g);
201             }
202         } else {
203             pa_sink_input *si;
204
205             PA_IDXSET_FOREACH(si, u->core->sink_inputs, idx) {
206                 g = get_group(PA_OBJECT(si), true);
207
208                 if (pa_streq(g, group)) {
209                     if (pa_streq(module_name, si->sink->module->name)) {
210                         /* Make sure we're not routing to another instance of
211                          * the same filter. */
212                         filter->sink_master = si->sink->input_to_master->sink;
213                     } else {
214                         filter->sink_master = si->sink;
215                     }
216
217                     pa_xfree(g);
218                     break;
219                 }
220
221                 pa_xfree(g);
222             }
223         }
224
225         pa_xfree(group);
226         pa_xfree(module_name);
227
228         if (!filter->sink_master || !filter->source_master)
229             return false;
230     }
231
232     return true;
233 }
234
235 static bool nothing_attached(struct filter *f) {
236     bool no_si = true, no_so = true;
237
238     if (f->sink)
239         no_si = pa_idxset_isempty(f->sink->inputs);
240     if (f->source)
241         no_so = pa_idxset_isempty(f->source->outputs);
242
243     return no_si && no_so;
244 }
245
246 static void housekeeping_time_callback(pa_mainloop_api*a, pa_time_event* e, const struct timeval *t, void *userdata) {
247     struct userdata *u = userdata;
248     struct filter *filter;
249     void *state;
250
251     pa_assert(a);
252     pa_assert(e);
253     pa_assert(u);
254
255     pa_assert(e == u->housekeeping_time_event);
256     u->core->mainloop->time_free(u->housekeeping_time_event);
257     u->housekeeping_time_event = NULL;
258
259     PA_HASHMAP_FOREACH(filter, u->filters, state) {
260         if (nothing_attached(filter)) {
261             uint32_t idx;
262
263             pa_log_debug("Detected filter %s as no longer used. Unloading.", filter->name);
264             idx = filter->module_index;
265             pa_hashmap_remove(u->filters, filter);
266             filter_free(filter);
267             pa_module_unload_request_by_index(u->core, idx, true);
268         }
269     }
270
271     pa_log_info("Housekeeping Done.");
272 }
273
274 static void trigger_housekeeping(struct userdata *u) {
275     pa_assert(u);
276
277     if (!u->autoclean)
278         return;
279
280     if (u->housekeeping_time_event)
281         return;
282
283     u->housekeeping_time_event = pa_core_rttime_new(u->core, pa_rtclock_now() + HOUSEKEEPING_INTERVAL, housekeeping_time_callback, u);
284 }
285
286 static int do_move(pa_object *obj, pa_object *parent, bool restore, bool is_input) {
287     if (is_input)
288         return pa_sink_input_move_to(PA_SINK_INPUT(obj), PA_SINK(parent), restore);
289     else
290         return pa_source_output_move_to(PA_SOURCE_OUTPUT(obj), PA_SOURCE(parent), restore);
291 }
292
293 static void move_object_for_filter(pa_object *o, struct filter* filter, bool restore, bool is_sink_input) {
294     pa_object *parent;
295     pa_proplist *pl;
296     const char *name;
297
298     pa_assert(o);
299     pa_assert(filter);
300
301     if (is_sink_input) {
302         pl = PA_SINK_INPUT(o)->proplist;
303         parent = PA_OBJECT(restore ? filter->sink_master : filter->sink);
304         if (!parent)
305             return;
306         name = PA_SINK(parent)->name;
307     } else {
308         pl = PA_SOURCE_OUTPUT(o)->proplist;
309         parent = PA_OBJECT(restore ? filter->source_master : filter->source);
310         if (!parent)
311             return;
312         name = PA_SOURCE(parent)->name;
313     }
314
315     pa_proplist_sets(pl, PA_PROP_FILTER_APPLY_MOVING, "1");
316
317     if (do_move(o, parent, false, is_sink_input) < 0)
318         pa_log_info("Failed to move %s for \"%s\" to <%s>.", is_sink_input ? "sink-input" : "source-output",
319                     pa_strnull(pa_proplist_gets(pl, PA_PROP_APPLICATION_NAME)), name);
320     else
321         pa_log_info("Successfully moved %s for \"%s\" to <%s>.", is_sink_input ? "sink-input" : "source-output",
322                     pa_strnull(pa_proplist_gets(pl, PA_PROP_APPLICATION_NAME)), name);
323
324     pa_proplist_unset(pl, PA_PROP_FILTER_APPLY_MOVING);
325 }
326
327 static void move_objects_for_filter(struct userdata *u, pa_object *o, struct filter* filter, bool restore,
328         bool is_sink_input) {
329
330     if (!should_group_filter(filter))
331         move_object_for_filter(o, filter, restore, is_sink_input);
332     else {
333         pa_source_output *so;
334         pa_sink_input *si;
335         char *g, *group;
336         uint32_t idx;
337
338         group = get_group(o, is_sink_input);
339
340         PA_IDXSET_FOREACH(so, u->core->source_outputs, idx) {
341             g = get_group(PA_OBJECT(so), false);
342
343             if (pa_streq(g, group))
344                 move_object_for_filter(PA_OBJECT(so), filter, restore, false);
345
346             pa_xfree(g);
347         }
348
349         PA_IDXSET_FOREACH(si, u->core->sink_inputs, idx) {
350             g = get_group(PA_OBJECT(si), true);
351
352             if (pa_streq(g, group))
353                 move_object_for_filter(PA_OBJECT(si), filter, restore, true);
354
355             pa_xfree(g);
356         }
357
358         pa_xfree(group);
359     }
360 }
361
362 /* Note that we assume a filter will provide at most one sink and at most one
363  * source (and at least one of either). */
364 static void find_filters_for_module(struct userdata *u, pa_module *m, const char *name) {
365     uint32_t idx;
366     pa_sink *sink;
367     pa_source *source;
368     struct filter *fltr = NULL;
369
370     PA_IDXSET_FOREACH(sink, u->core->sinks, idx) {
371         if (sink->module == m) {
372             pa_assert(sink->input_to_master != NULL);
373
374             fltr = filter_new(name, sink->input_to_master->sink, NULL);
375             fltr->module_index = m->index;
376             fltr->sink = sink;
377
378             break;
379         }
380     }
381
382     PA_IDXSET_FOREACH(source, u->core->sources, idx) {
383         if (source->module == m && !source->monitor_of) {
384             pa_assert(source->output_from_master != NULL);
385
386             if (!fltr) {
387                 fltr = filter_new(name, NULL, source->output_from_master->source);
388                 fltr->module_index = m->index;
389                 fltr->source = source;
390             } else {
391                 fltr->source = source;
392                 fltr->source_master = source->output_from_master->source;
393             }
394
395             break;
396         }
397     }
398
399     pa_hashmap_put(u->filters, fltr, fltr);
400 }
401
402 static bool can_unload_module(struct userdata *u, uint32_t idx) {
403     void *state;
404     struct filter *filter;
405
406     /* Check if any other struct filters point to the same module */
407     PA_HASHMAP_FOREACH(filter, u->filters, state) {
408         if (filter->module_index == idx && !nothing_attached(filter))
409             return false;
410     }
411
412     return true;
413 }
414
415 static pa_hook_result_t process(struct userdata *u, pa_object *o, bool is_sink_input) {
416     const char *want;
417     bool done_something = false;
418     pa_sink *sink = NULL;
419     pa_source *source = NULL;
420     pa_module *module = NULL;
421
422     if (is_sink_input) {
423         sink = PA_SINK_INPUT(o)->sink;
424
425         if (sink)
426             module = sink->module;
427     } else {
428         source = PA_SOURCE_OUTPUT(o)->source;
429
430         if (source)
431             module = source->module;
432     }
433
434     /* If there is no sink/source yet, we can't do much */
435     if ((is_sink_input && !sink) || (!is_sink_input && !source))
436         return PA_HOOK_OK;
437
438     /* If the stream doesn't what any filter, then let it be. */
439     if ((want = should_filter(o, is_sink_input))) {
440         char *module_name;
441         struct filter *fltr, *filter;
442
443         /* We need to ensure the SI is playing on a sink of this type
444          * attached to the sink it's "officially" playing on */
445
446         if (!module)
447             return PA_HOOK_OK;
448
449         module_name = pa_sprintf_malloc("module-%s", want);
450         if (pa_streq(module->name, module_name)) {
451             pa_log_debug("Stream appears to be playing on an appropriate sink already. Ignoring.");
452             pa_xfree(module_name);
453             return PA_HOOK_OK;
454         }
455
456         fltr = filter_new(want, sink, source);
457
458         if (should_group_filter(fltr) && !find_paired_master(u, fltr, o, is_sink_input)) {
459             pa_log_debug("Want group filtering but don't have enough streams.");
460             return PA_HOOK_OK;
461         }
462
463         if (!(filter = pa_hashmap_get(u->filters, fltr))) {
464             char *args;
465             pa_module *m;
466
467             args = pa_sprintf_malloc("autoloaded=1 %s%s %s%s",
468                     fltr->sink_master ? "sink_master=" : "",
469                     fltr->sink_master ? fltr->sink_master->name : "",
470                     fltr->source_master ? "source_master=" : "",
471                     fltr->source_master ? fltr->source_master->name : "");
472
473             pa_log_debug("Loading %s with arguments '%s'", module_name, args);
474
475             if ((m = pa_module_load(u->core, module_name, args))) {
476                 find_filters_for_module(u, m, want);
477                 filter = pa_hashmap_get(u->filters, fltr);
478                 done_something = true;
479             }
480             pa_xfree(args);
481         }
482
483         pa_xfree(fltr);
484
485         if (!filter) {
486             pa_log("Unable to load %s", module_name);
487             pa_xfree(module_name);
488             return PA_HOOK_OK;
489         }
490         pa_xfree(module_name);
491
492         /* We can move the stream now as we know the destination. If this
493          * isn't true, we will do it later when the sink appears. */
494         if ((is_sink_input && filter->sink) || (!is_sink_input && filter->source)) {
495             move_objects_for_filter(u, o, filter, false, is_sink_input);
496             done_something = true;
497         }
498     } else {
499         void *state;
500         struct filter *filter = NULL;
501
502         /* We do not want to filter... but are we already filtered?
503          * This can happen if an input's proplist changes */
504         PA_HASHMAP_FOREACH(filter, u->filters, state) {
505             if ((is_sink_input && sink == filter->sink) || (!is_sink_input && source == filter->source)) {
506                 move_objects_for_filter(u, o, filter, true, is_sink_input);
507                 done_something = true;
508                 break;
509             }
510         }
511     }
512
513     if (done_something)
514         trigger_housekeeping(u);
515
516     return PA_HOOK_OK;
517 }
518
519 static pa_hook_result_t sink_input_put_cb(pa_core *core, pa_sink_input *i, struct userdata *u) {
520     pa_core_assert_ref(core);
521     pa_sink_input_assert_ref(i);
522
523     return process(u, PA_OBJECT(i), true);
524 }
525
526 static pa_hook_result_t sink_input_move_finish_cb(pa_core *core, pa_sink_input *i, struct userdata *u) {
527     pa_core_assert_ref(core);
528     pa_sink_input_assert_ref(i);
529
530     if (pa_proplist_gets(i->proplist, PA_PROP_FILTER_APPLY_MOVING))
531         return PA_HOOK_OK;
532
533     return process(u, PA_OBJECT(i), true);
534 }
535
536 static pa_hook_result_t sink_input_proplist_cb(pa_core *core, pa_sink_input *i, struct userdata *u) {
537     pa_core_assert_ref(core);
538     pa_sink_input_assert_ref(i);
539
540     return process(u, PA_OBJECT(i), true);
541 }
542
543 static pa_hook_result_t sink_input_unlink_cb(pa_core *core, pa_sink_input *i, struct userdata *u) {
544     pa_core_assert_ref(core);
545     pa_sink_input_assert_ref(i);
546
547     pa_assert(u);
548
549     if (pa_hashmap_size(u->filters) > 0)
550         trigger_housekeeping(u);
551
552     return PA_HOOK_OK;
553 }
554
555 static pa_hook_result_t sink_unlink_cb(pa_core *core, pa_sink *sink, struct userdata *u) {
556     void *state;
557     struct filter *filter = NULL;
558
559     pa_core_assert_ref(core);
560     pa_sink_assert_ref(sink);
561     pa_assert(u);
562
563     /* If either the parent or the sink we've loaded disappears,
564      * we should remove it from our hashmap */
565     PA_HASHMAP_FOREACH(filter, u->filters, state) {
566         if (filter->sink_master == sink || filter->sink == sink) {
567             uint32_t idx;
568
569             /* Attempt to rescue any streams to the parent sink as this is likely
570              * the best course of action (as opposed to a generic rescue via
571              * module-rescue-streams */
572             if (filter->sink == sink) {
573                 pa_sink_input *i;
574
575                 PA_IDXSET_FOREACH(i, sink->inputs, idx)
576                     move_objects_for_filter(u, PA_OBJECT(i), filter, true, true);
577             }
578
579             idx = filter->module_index;
580             pa_hashmap_remove(u->filters, filter);
581             filter_free(filter);
582
583             if (can_unload_module(u, idx))
584                 pa_module_unload_request_by_index(u->core, idx, true);
585         }
586     }
587
588     return PA_HOOK_OK;
589 }
590
591 static pa_hook_result_t source_output_put_cb(pa_core *core, pa_source_output *o, struct userdata *u) {
592     pa_core_assert_ref(core);
593     pa_source_output_assert_ref(o);
594
595     return process(u, PA_OBJECT(o), false);
596 }
597
598 static pa_hook_result_t source_output_move_finish_cb(pa_core *core, pa_source_output *o, struct userdata *u) {
599     pa_core_assert_ref(core);
600     pa_source_output_assert_ref(o);
601
602     if (pa_proplist_gets(o->proplist, PA_PROP_FILTER_APPLY_MOVING))
603         return PA_HOOK_OK;
604
605     return process(u, PA_OBJECT(o), false);
606 }
607
608 static pa_hook_result_t source_output_proplist_cb(pa_core *core, pa_source_output *o, struct userdata *u) {
609     pa_core_assert_ref(core);
610     pa_source_output_assert_ref(o);
611
612     return process(u, PA_OBJECT(o), false);
613 }
614
615 static pa_hook_result_t source_output_unlink_cb(pa_core *core, pa_source_output *o, struct userdata *u) {
616     pa_core_assert_ref(core);
617     pa_source_output_assert_ref(o);
618
619     pa_assert(u);
620
621     if (pa_hashmap_size(u->filters) > 0)
622         trigger_housekeeping(u);
623
624     return PA_HOOK_OK;
625 }
626
627 static pa_hook_result_t source_unlink_cb(pa_core *core, pa_source *source, struct userdata *u) {
628     void *state;
629     struct filter *filter = NULL;
630
631     pa_core_assert_ref(core);
632     pa_source_assert_ref(source);
633     pa_assert(u);
634
635     /* If either the parent or the source we've loaded disappears,
636      * we should remove it from our hashmap */
637     PA_HASHMAP_FOREACH(filter, u->filters, state) {
638         if (filter->source_master == source || filter->source == source) {
639             uint32_t idx;
640
641             /* Attempt to rescue any streams to the parent source as this is likely
642              * the best course of action (as opposed to a generic rescue via
643              * module-rescue-streams */
644             if (filter->source == source) {
645                 pa_source_output *o;
646
647                 PA_IDXSET_FOREACH(o, source->outputs, idx)
648                     move_objects_for_filter(u, PA_OBJECT(o), filter, true, false);
649             }
650
651             idx = filter->module_index;
652             pa_hashmap_remove(u->filters, filter);
653             filter_free(filter);
654
655             if (can_unload_module(u, idx))
656                 pa_module_unload_request_by_index(u->core, idx, true);
657         }
658     }
659
660     return PA_HOOK_OK;
661 }
662
663 int pa__init(pa_module *m) {
664     pa_modargs *ma = NULL;
665     struct userdata *u;
666
667     pa_assert(m);
668
669     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
670         pa_log("Failed to parse module arguments");
671         goto fail;
672     }
673
674     m->userdata = u = pa_xnew0(struct userdata, 1);
675
676     u->core = m->core;
677
678     u->autoclean = DEFAULT_AUTOCLEAN;
679     if (pa_modargs_get_value_boolean(ma, "autoclean", &u->autoclean) < 0) {
680         pa_log("Failed to parse autoclean value");
681         goto fail;
682     }
683
684     u->filters = pa_hashmap_new(filter_hash, filter_compare);
685
686     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);
687     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);
688     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);
689     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);
690     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);
691     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);
692     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);
693     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);
694     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);
695     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);
696
697     pa_modargs_free(ma);
698
699     return 0;
700
701 fail:
702     pa__done(m);
703
704     if (ma)
705         pa_modargs_free(ma);
706
707     return -1;
708 }
709
710 void pa__done(pa_module *m) {
711     struct userdata* u;
712
713     pa_assert(m);
714
715     if (!(u = m->userdata))
716         return;
717
718     if (u->sink_input_put_slot)
719         pa_hook_slot_free(u->sink_input_put_slot);
720     if (u->sink_input_move_finish_slot)
721         pa_hook_slot_free(u->sink_input_move_finish_slot);
722     if (u->sink_input_proplist_slot)
723         pa_hook_slot_free(u->sink_input_proplist_slot);
724     if (u->sink_input_unlink_slot)
725         pa_hook_slot_free(u->sink_input_unlink_slot);
726     if (u->sink_unlink_slot)
727         pa_hook_slot_free(u->sink_unlink_slot);
728     if (u->source_output_put_slot)
729         pa_hook_slot_free(u->source_output_put_slot);
730     if (u->source_output_move_finish_slot)
731         pa_hook_slot_free(u->source_output_move_finish_slot);
732     if (u->source_output_proplist_slot)
733         pa_hook_slot_free(u->source_output_proplist_slot);
734     if (u->source_output_unlink_slot)
735         pa_hook_slot_free(u->source_output_unlink_slot);
736     if (u->source_unlink_slot)
737         pa_hook_slot_free(u->source_unlink_slot);
738
739     if (u->housekeeping_time_event)
740         u->core->mainloop->time_free(u->housekeeping_time_event);
741
742     if (u->filters) {
743         struct filter *f;
744
745         while ((f = pa_hashmap_steal_first(u->filters))) {
746             pa_module_unload_request_by_index(u->core, f->module_index, true);
747             filter_free(f);
748         }
749
750         pa_hashmap_free(u->filters);
751     }
752
753     pa_xfree(u);
754 }