hashmap: Use pa_free_cb_t instead of pa_free2_cb_t
[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     pa_bool_t 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, pa_bool_t 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 pa_bool_t should_group_filter(struct filter *filter) {
152     return pa_streq(filter->name, "echo-cancel");
153 }
154
155 static char* get_group(pa_object *o, pa_bool_t 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 pa_bool_t find_paired_master(struct userdata *u, struct filter *filter, pa_object *o, pa_bool_t 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 pa_bool_t nothing_attached(struct filter *f) {
236     pa_bool_t 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, pa_bool_t restore, pa_bool_t 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, pa_bool_t restore, pa_bool_t 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, pa_bool_t restore,
328         pa_bool_t 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 pa_bool_t 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, pa_bool_t is_sink_input) {
416     const char *want;
417     pa_bool_t done_something = FALSE;
418     pa_sink *sink = NULL;
419     pa_source *source = NULL;
420     pa_module *module;
421
422     if (is_sink_input) {
423         sink = PA_SINK_INPUT(o)->sink;
424         module = sink->module;
425     } else {
426         source = PA_SOURCE_OUTPUT(o)->source;
427         module = source->module;
428     }
429
430     /* If there is no sink/source yet, we can't do much */
431     if ((is_sink_input && !sink) || (!is_sink_input && !source))
432         return PA_HOOK_OK;
433
434     /* If the stream doesn't what any filter, then let it be. */
435     if ((want = should_filter(o, is_sink_input))) {
436         char *module_name;
437         struct filter *fltr, *filter;
438
439         /* We need to ensure the SI is playing on a sink of this type
440          * attached to the sink it's "officially" playing on */
441
442         if (!module)
443             return PA_HOOK_OK;
444
445         module_name = pa_sprintf_malloc("module-%s", want);
446         if (pa_streq(module->name, module_name)) {
447             pa_log_debug("Stream appears to be playing on an appropriate sink already. Ignoring.");
448             pa_xfree(module_name);
449             return PA_HOOK_OK;
450         }
451
452         fltr = filter_new(want, sink, source);
453
454         if (should_group_filter(fltr) && !find_paired_master(u, fltr, o, is_sink_input)) {
455             pa_log_debug("Want group filtering but don't have enough streams.");
456             return PA_HOOK_OK;
457         }
458
459         if (!(filter = pa_hashmap_get(u->filters, fltr))) {
460             char *args;
461             pa_module *m;
462
463             args = pa_sprintf_malloc("autoloaded=1 %s%s %s%s",
464                     fltr->sink_master ? "sink_master=" : "",
465                     fltr->sink_master ? fltr->sink_master->name : "",
466                     fltr->source_master ? "source_master=" : "",
467                     fltr->source_master ? fltr->source_master->name : "");
468
469             pa_log_debug("Loading %s with arguments '%s'", module_name, args);
470
471             if ((m = pa_module_load(u->core, module_name, args))) {
472                 find_filters_for_module(u, m, want);
473                 filter = pa_hashmap_get(u->filters, fltr);
474                 done_something = TRUE;
475             }
476             pa_xfree(args);
477         }
478
479         pa_xfree(fltr);
480
481         if (!filter) {
482             pa_log("Unable to load %s", module_name);
483             pa_xfree(module_name);
484             return PA_HOOK_OK;
485         }
486         pa_xfree(module_name);
487
488         /* We can move the stream now as we know the destination. If this
489          * isn't true, we will do it later when the sink appears. */
490         if ((is_sink_input && filter->sink) || (!is_sink_input && filter->source)) {
491             move_objects_for_filter(u, o, filter, FALSE, is_sink_input);
492             done_something = TRUE;
493         }
494     } else {
495         void *state;
496         struct filter *filter = NULL;
497
498         /* We do not want to filter... but are we already filtered?
499          * This can happen if an input's proplist changes */
500         PA_HASHMAP_FOREACH(filter, u->filters, state) {
501             if ((is_sink_input && sink == filter->sink) || (!is_sink_input && source == filter->source)) {
502                 move_objects_for_filter(u, o, filter, TRUE, is_sink_input);
503                 done_something = TRUE;
504                 break;
505             }
506         }
507     }
508
509     if (done_something)
510         trigger_housekeeping(u);
511
512     return PA_HOOK_OK;
513 }
514
515 static pa_hook_result_t sink_input_put_cb(pa_core *core, pa_sink_input *i, struct userdata *u) {
516     pa_core_assert_ref(core);
517     pa_sink_input_assert_ref(i);
518
519     return process(u, PA_OBJECT(i), TRUE);
520 }
521
522 static pa_hook_result_t sink_input_move_finish_cb(pa_core *core, pa_sink_input *i, struct userdata *u) {
523     pa_core_assert_ref(core);
524     pa_sink_input_assert_ref(i);
525
526     if (pa_proplist_gets(i->proplist, PA_PROP_FILTER_APPLY_MOVING))
527         return PA_HOOK_OK;
528
529     return process(u, PA_OBJECT(i), TRUE);
530 }
531
532 static pa_hook_result_t sink_input_proplist_cb(pa_core *core, pa_sink_input *i, struct userdata *u) {
533     pa_core_assert_ref(core);
534     pa_sink_input_assert_ref(i);
535
536     return process(u, PA_OBJECT(i), TRUE);
537 }
538
539 static pa_hook_result_t sink_input_unlink_cb(pa_core *core, pa_sink_input *i, struct userdata *u) {
540     pa_core_assert_ref(core);
541     pa_sink_input_assert_ref(i);
542
543     pa_assert(u);
544
545     if (pa_hashmap_size(u->filters) > 0)
546         trigger_housekeeping(u);
547
548     return PA_HOOK_OK;
549 }
550
551 static pa_hook_result_t sink_unlink_cb(pa_core *core, pa_sink *sink, struct userdata *u) {
552     void *state;
553     struct filter *filter = NULL;
554
555     pa_core_assert_ref(core);
556     pa_sink_assert_ref(sink);
557     pa_assert(u);
558
559     /* If either the parent or the sink we've loaded disappears,
560      * we should remove it from our hashmap */
561     PA_HASHMAP_FOREACH(filter, u->filters, state) {
562         if (filter->sink_master == sink || filter->sink == sink) {
563             uint32_t idx;
564
565             /* Attempt to rescue any streams to the parent sink as this is likely
566              * the best course of action (as opposed to a generic rescue via
567              * module-rescue-streams */
568             if (filter->sink == sink) {
569                 pa_sink_input *i;
570
571                 PA_IDXSET_FOREACH(i, sink->inputs, idx)
572                     move_objects_for_filter(u, PA_OBJECT(i), filter, TRUE, TRUE);
573             }
574
575             idx = filter->module_index;
576             pa_hashmap_remove(u->filters, filter);
577             filter_free(filter);
578
579             if (can_unload_module(u, idx))
580                 pa_module_unload_request_by_index(u->core, idx, TRUE);
581         }
582     }
583
584     return PA_HOOK_OK;
585 }
586
587 static pa_hook_result_t source_output_put_cb(pa_core *core, pa_source_output *o, struct userdata *u) {
588     pa_core_assert_ref(core);
589     pa_source_output_assert_ref(o);
590
591     return process(u, PA_OBJECT(o), FALSE);
592 }
593
594 static pa_hook_result_t source_output_move_finish_cb(pa_core *core, pa_source_output *o, struct userdata *u) {
595     pa_core_assert_ref(core);
596     pa_source_output_assert_ref(o);
597
598     if (pa_proplist_gets(o->proplist, PA_PROP_FILTER_APPLY_MOVING))
599         return PA_HOOK_OK;
600
601     return process(u, PA_OBJECT(o), FALSE);
602 }
603
604 static pa_hook_result_t source_output_proplist_cb(pa_core *core, pa_source_output *o, struct userdata *u) {
605     pa_core_assert_ref(core);
606     pa_source_output_assert_ref(o);
607
608     return process(u, PA_OBJECT(o), FALSE);
609 }
610
611 static pa_hook_result_t source_output_unlink_cb(pa_core *core, pa_source_output *o, struct userdata *u) {
612     pa_core_assert_ref(core);
613     pa_source_output_assert_ref(o);
614
615     pa_assert(u);
616
617     if (pa_hashmap_size(u->filters) > 0)
618         trigger_housekeeping(u);
619
620     return PA_HOOK_OK;
621 }
622
623 static pa_hook_result_t source_unlink_cb(pa_core *core, pa_source *source, struct userdata *u) {
624     void *state;
625     struct filter *filter = NULL;
626
627     pa_core_assert_ref(core);
628     pa_source_assert_ref(source);
629     pa_assert(u);
630
631     /* If either the parent or the source we've loaded disappears,
632      * we should remove it from our hashmap */
633     PA_HASHMAP_FOREACH(filter, u->filters, state) {
634         if (filter->source_master == source || filter->source == source) {
635             uint32_t idx;
636
637             /* Attempt to rescue any streams to the parent source as this is likely
638              * the best course of action (as opposed to a generic rescue via
639              * module-rescue-streams */
640             if (filter->source == source) {
641                 pa_source_output *o;
642
643                 PA_IDXSET_FOREACH(o, source->outputs, idx)
644                     move_objects_for_filter(u, PA_OBJECT(o), filter, TRUE, FALSE);
645             }
646
647             idx = filter->module_index;
648             pa_hashmap_remove(u->filters, filter);
649             filter_free(filter);
650
651             if (can_unload_module(u, idx))
652                 pa_module_unload_request_by_index(u->core, idx, TRUE);
653         }
654     }
655
656     return PA_HOOK_OK;
657 }
658
659 int pa__init(pa_module *m) {
660     pa_modargs *ma = NULL;
661     struct userdata *u;
662
663     pa_assert(m);
664
665     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
666         pa_log("Failed to parse module arguments");
667         goto fail;
668     }
669
670     m->userdata = u = pa_xnew0(struct userdata, 1);
671
672     u->core = m->core;
673
674     u->autoclean = DEFAULT_AUTOCLEAN;
675     if (pa_modargs_get_value_boolean(ma, "autoclean", &u->autoclean) < 0) {
676         pa_log("Failed to parse autoclean value");
677         goto fail;
678     }
679
680     u->filters = pa_hashmap_new(filter_hash, filter_compare);
681
682     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);
683     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);
684     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);
685     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);
686     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);
687     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);
688     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);
689     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);
690     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);
691     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);
692
693     pa_modargs_free(ma);
694
695     return 0;
696
697 fail:
698     pa__done(m);
699
700     if (ma)
701         pa_modargs_free(ma);
702
703     return -1;
704 }
705
706 void pa__done(pa_module *m) {
707     struct userdata* u;
708
709     pa_assert(m);
710
711     if (!(u = m->userdata))
712         return;
713
714     if (u->sink_input_put_slot)
715         pa_hook_slot_free(u->sink_input_put_slot);
716     if (u->sink_input_move_finish_slot)
717         pa_hook_slot_free(u->sink_input_move_finish_slot);
718     if (u->sink_input_proplist_slot)
719         pa_hook_slot_free(u->sink_input_proplist_slot);
720     if (u->sink_input_unlink_slot)
721         pa_hook_slot_free(u->sink_input_unlink_slot);
722     if (u->sink_unlink_slot)
723         pa_hook_slot_free(u->sink_unlink_slot);
724     if (u->source_output_put_slot)
725         pa_hook_slot_free(u->source_output_put_slot);
726     if (u->source_output_move_finish_slot)
727         pa_hook_slot_free(u->source_output_move_finish_slot);
728     if (u->source_output_proplist_slot)
729         pa_hook_slot_free(u->source_output_proplist_slot);
730     if (u->source_output_unlink_slot)
731         pa_hook_slot_free(u->source_output_unlink_slot);
732     if (u->source_unlink_slot)
733         pa_hook_slot_free(u->source_unlink_slot);
734
735     if (u->housekeeping_time_event)
736         u->core->mainloop->time_free(u->housekeeping_time_event);
737
738     if (u->filters) {
739         struct filter *f;
740
741         while ((f = pa_hashmap_steal_first(u->filters))) {
742             pa_module_unload_request_by_index(u->core, f->module_index, TRUE);
743             filter_free(f);
744         }
745
746         pa_hashmap_free(u->filters, NULL);
747     }
748
749     pa_xfree(u);
750 }