]> code.delx.au - pulseaudio/blob - src/modules/module-ladspa-sink.c
commit glitch-free work
[pulseaudio] / src / modules / module-ladspa-sink.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
12
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 USA.
22 ***/
23
24 /* TODO: Some plugins cause latency, and some even report it by using a control
25 out port. We don't currently use the latency information. */
26
27 #ifdef HAVE_CONFIG_H
28 #include <config.h>
29 #endif
30
31 #include <pulse/xmalloc.h>
32
33 #include <pulsecore/core-error.h>
34 #include <pulsecore/namereg.h>
35 #include <pulsecore/sink.h>
36 #include <pulsecore/module.h>
37 #include <pulsecore/core-util.h>
38 #include <pulsecore/modargs.h>
39 #include <pulsecore/log.h>
40 #include <pulsecore/thread.h>
41 #include <pulsecore/thread-mq.h>
42 #include <pulsecore/rtpoll.h>
43 #include <pulsecore/sample-util.h>
44
45 #include "module-ladspa-sink-symdef.h"
46 #include "ladspa.h"
47
48 PA_MODULE_AUTHOR("Lennart Poettering");
49 PA_MODULE_DESCRIPTION("Virtual LADSPA sink");
50 PA_MODULE_VERSION(PACKAGE_VERSION);
51 PA_MODULE_LOAD_ONCE(FALSE);
52 PA_MODULE_USAGE(
53 "sink_name=<name for the sink> "
54 "master=<name of sink to remap> "
55 "format=<sample format> "
56 "channels=<number of channels> "
57 "rate=<sample rate> "
58 "channel_map=<channel map> "
59 "plugin=<ladspa plugin name> "
60 "label=<ladspa plugin label> "
61 "control=<comma seperated list of input control values>");
62
63 struct userdata {
64 pa_core *core;
65 pa_module *module;
66
67 pa_sink *sink, *master;
68 pa_sink_input *sink_input;
69
70 const LADSPA_Descriptor *descriptor;
71 unsigned channels;
72 LADSPA_Handle handle[PA_CHANNELS_MAX];
73 LADSPA_Data *input, *output;
74 size_t block_size;
75 unsigned long input_port, output_port;
76 LADSPA_Data *control;
77
78 /* This is a dummy buffer. Every port must be connected, but we don't care
79 about control out ports. We connect them all to this single buffer. */
80 LADSPA_Data control_out;
81 };
82
83 static const char* const valid_modargs[] = {
84 "sink_name",
85 "master",
86 "format",
87 "channels",
88 "rate",
89 "channel_map",
90 "plugin",
91 "label",
92 "control",
93 NULL
94 };
95
96 /* Called from I/O thread context */
97 static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
98 struct userdata *u = PA_SINK(o)->userdata;
99
100 switch (code) {
101
102 case PA_SINK_MESSAGE_GET_LATENCY: {
103 pa_usec_t usec = 0;
104
105 if (PA_MSGOBJECT(u->master)->process_msg(PA_MSGOBJECT(u->master), PA_SINK_MESSAGE_GET_LATENCY, &usec, 0, NULL) < 0)
106 usec = 0;
107
108 *((pa_usec_t*) data) = usec /* + pa_bytes_to_usec(u->memchunk.length, &u->sink->sample_spec) */;
109 return 0;
110 }
111 }
112
113 return pa_sink_process_msg(o, code, data, offset, chunk);
114 }
115
116 /* Called from main context */
117 static int sink_set_state(pa_sink *s, pa_sink_state_t state) {
118 struct userdata *u;
119
120 pa_sink_assert_ref(s);
121 pa_assert_se(u = s->userdata);
122
123 if (PA_SINK_LINKED(state) && u->sink_input && PA_SINK_INPUT_LINKED(pa_sink_input_get_state(u->sink_input)))
124 pa_sink_input_cork(u->sink_input, state == PA_SINK_SUSPENDED);
125
126 return 0;
127 }
128
129 /* Called from I/O thread context */
130 static void sink_request_rewind(pa_sink *s) {
131 struct userdata *u;
132
133 pa_sink_assert_ref(s);
134 pa_assert_se(u = s->userdata);
135
136 /* Just hand this one over to the master sink */
137 pa_sink_input_request_rewrite(u->sink_input, s->thread_info.rewind_nbytes);
138 }
139
140 /* Called from I/O thread context */
141 static void sink_update_requested_latency(pa_sink *s) {
142 struct userdata *u;
143
144 pa_sink_assert_ref(s);
145 pa_assert_se(u = s->userdata);
146
147 /* Just hand this one over to the master sink */
148 u->sink_input->thread_info.requested_sink_latency = pa_sink_get_requested_latency(s);
149 pa_sink_invalidate_requested_latency(u->master);
150 }
151
152 /* Called from I/O thread context */
153 static int sink_input_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
154 struct userdata *u = PA_SINK_INPUT(o)->userdata;
155
156 switch (code) {
157 case PA_SINK_INPUT_MESSAGE_GET_LATENCY:
158 *((pa_usec_t*) data) = 0 /*pa_bytes_to_usec(u->memchunk.length, &u->sink_input->sample_spec)*/;
159
160 /* Fall through, the default handler will add in the extra
161 * latency added by the resampler */
162 break;
163 }
164
165 return pa_sink_input_process_msg(o, code, data, offset, chunk);
166 }
167
168 /* Called from I/O thread context */
169 static int sink_input_pop_cb(pa_sink_input *i, size_t nbytes, pa_memchunk *chunk) {
170 struct userdata *u;
171 float *src, *dst;
172 size_t fs;
173 unsigned n, c;
174 pa_memchunk tchunk;
175
176 pa_sink_input_assert_ref(i);
177 pa_assert(chunk);
178 pa_assert_se(u = i->userdata);
179
180 pa_sink_render(u->sink, nbytes, &tchunk);
181
182 fs = pa_frame_size(&i->sample_spec);
183 n = tchunk.length / fs;
184
185 pa_assert(n > 0);
186
187 chunk->memblock = pa_memblock_new(i->sink->core->mempool, tchunk.length);
188 chunk->index = 0;
189 chunk->length = tchunk.length;
190
191 src = (float*) ((uint8_t*) pa_memblock_acquire(tchunk.memblock) + tchunk.index);
192 dst = (float*) pa_memblock_acquire(chunk->memblock);
193
194 for (c = 0; c < u->channels; c++) {
195 unsigned j;
196 float *p, *q;
197
198 p = src + c;
199 q = u->input;
200 for (j = 0; j < n; j++, p += u->channels, q++)
201 *q = PA_CLAMP_UNLIKELY(*p, -1.0, 1.0);
202
203 u->descriptor->run(u->handle[c], n);
204
205 q = u->output;
206 p = dst + c;
207 for (j = 0; j < n; j++, q++, p += u->channels)
208 *p = PA_CLAMP_UNLIKELY(*q, -1.0, 1.0);
209 }
210
211 pa_memblock_release(tchunk.memblock);
212 pa_memblock_release(chunk->memblock);
213
214 pa_memblock_unref(tchunk.memblock);
215
216 return 0;
217 }
218
219 /* Called from I/O thread context */
220 static void sink_input_rewind_cb(pa_sink_input *i, size_t nbytes) {
221 struct userdata *u;
222
223 pa_sink_input_assert_ref(i);
224 pa_assert_se(u = i->userdata);
225 pa_assert(nbytes > 0);
226
227 u->sink->thread_info.rewind_nbytes = nbytes;
228 pa_sink_process_rewind(u->sink);
229 }
230
231 /* Called from I/O thread context */
232 static void sink_input_set_max_rewind_cb(pa_sink_input *i, size_t nbytes) {
233 struct userdata *u;
234
235 pa_sink_input_assert_ref(i);
236 pa_assert_se(u = i->userdata);
237
238 pa_sink_set_max_rewind(u->sink, nbytes);
239 }
240
241 /* Called from I/O thread context */
242 static void sink_input_detach_cb(pa_sink_input *i) {
243 struct userdata *u;
244
245 pa_sink_input_assert_ref(i);
246 pa_assert_se(u = i->userdata);
247
248 pa_sink_detach_within_thread(u->sink);
249 }
250
251 /* Called from I/O thread context */
252 static void sink_input_attach_cb(pa_sink_input *i) {
253 struct userdata *u;
254
255 pa_sink_input_assert_ref(i);
256 pa_assert_se(u = i->userdata);
257
258 pa_sink_set_asyncmsgq(u->sink, i->sink->asyncmsgq);
259 pa_sink_set_rtpoll(u->sink, i->sink->rtpoll);
260
261 pa_sink_attach_within_thread(u->sink);
262 }
263
264 /* Called from main context */
265 static void sink_input_kill_cb(pa_sink_input *i) {
266 struct userdata *u;
267
268 pa_sink_input_assert_ref(i);
269 pa_assert_se(u = i->userdata);
270
271 pa_sink_input_unlink(u->sink_input);
272 pa_sink_input_unref(u->sink_input);
273 u->sink_input = NULL;
274
275 pa_sink_unlink(u->sink);
276 pa_sink_unref(u->sink);
277 u->sink = NULL;
278
279 pa_module_unload_request(u->module);
280 }
281
282 int pa__init(pa_module*m) {
283 struct userdata *u;
284 pa_sample_spec ss;
285 pa_channel_map map;
286 pa_modargs *ma;
287 char *t;
288 const char *z;
289 pa_sink *master;
290 pa_sink_input_new_data sink_input_data;
291 pa_sink_new_data sink_data;
292 const char *plugin, *label;
293 LADSPA_Descriptor_Function descriptor_func;
294 const char *e, *cdata;
295 const LADSPA_Descriptor *d;
296 unsigned long input_port, output_port, p, j, n_control;
297 unsigned c;
298 pa_bool_t *use_default = NULL;
299
300 pa_assert(m);
301
302 pa_assert(sizeof(LADSPA_Data) == sizeof(float));
303
304 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
305 pa_log("Failed to parse module arguments.");
306 goto fail;
307 }
308
309 if (!(master = pa_namereg_get(m->core, pa_modargs_get_value(ma, "master", NULL), PA_NAMEREG_SINK, 1))) {
310 pa_log("Master sink not found");
311 goto fail;
312 }
313
314 ss = master->sample_spec;
315 ss.format = PA_SAMPLE_FLOAT32;
316 map = master->channel_map;
317 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
318 pa_log("Invalid sample format specification or channel map");
319 goto fail;
320 }
321
322 if (!(plugin = pa_modargs_get_value(ma, "plugin", NULL))) {
323 pa_log("Missing LADSPA plugin name");
324 goto fail;
325 }
326
327 if (!(label = pa_modargs_get_value(ma, "label", NULL))) {
328 pa_log("Missing LADSPA plugin label");
329 goto fail;
330 }
331
332 cdata = pa_modargs_get_value(ma, "control", NULL);
333
334 u = pa_xnew0(struct userdata, 1);
335 u->core = m->core;
336 u->module = m;
337 m->userdata = u;
338 u->master = master;
339 u->sink = NULL;
340 u->sink_input = NULL;
341
342 if (!(e = getenv("LADSPA_PATH")))
343 e = LADSPA_PATH;
344
345 /* FIXME: This is not exactly thread safe */
346 t = pa_xstrdup(lt_dlgetsearchpath());
347 lt_dlsetsearchpath(e);
348 m->dl = lt_dlopenext(plugin);
349 lt_dlsetsearchpath(t);
350 pa_xfree(t);
351
352 if (!m->dl) {
353 pa_log("Failed to load LADSPA plugin: %s", lt_dlerror());
354 goto fail;
355 }
356
357 if (!(descriptor_func = (LADSPA_Descriptor_Function) lt_dlsym(m->dl, "ladspa_descriptor"))) {
358 pa_log("LADSPA module lacks ladspa_descriptor() symbol.");
359 goto fail;
360 }
361
362 for (j = 0;; j++) {
363
364 if (!(d = descriptor_func(j))) {
365 pa_log("Failed to find plugin label '%s' in plugin '%s'.", plugin, label);
366 goto fail;
367 }
368
369 if (strcmp(d->Label, label) == 0)
370 break;
371 }
372
373 u->descriptor = d;
374
375 pa_log_debug("Module: %s", plugin);
376 pa_log_debug("Label: %s", d->Label);
377 pa_log_debug("Unique ID: %lu", d->UniqueID);
378 pa_log_debug("Name: %s", d->Name);
379 pa_log_debug("Maker: %s", d->Maker);
380 pa_log_debug("Copyright: %s", d->Copyright);
381
382 input_port = output_port = (unsigned long) -1;
383 n_control = 0;
384
385 for (p = 0; p < d->PortCount; p++) {
386
387 if (LADSPA_IS_PORT_INPUT(d->PortDescriptors[p]) && LADSPA_IS_PORT_AUDIO(d->PortDescriptors[p])) {
388
389 if (strcmp(d->PortNames[p], "Input") == 0) {
390 pa_assert(input_port == (unsigned long) -1);
391 input_port = p;
392 } else {
393 pa_log("Found audio input port on plugin we cannot handle: %s", d->PortNames[p]);
394 goto fail;
395 }
396
397 } else if (LADSPA_IS_PORT_OUTPUT(d->PortDescriptors[p]) && LADSPA_IS_PORT_AUDIO(d->PortDescriptors[p])) {
398
399 if (strcmp(d->PortNames[p], "Output") == 0) {
400 pa_assert(output_port == (unsigned long) -1);
401 output_port = p;
402 } else {
403 pa_log("Found audio output port on plugin we cannot handle: %s", d->PortNames[p]);
404 goto fail;
405 }
406
407 } else if (LADSPA_IS_PORT_INPUT(d->PortDescriptors[p]) && LADSPA_IS_PORT_CONTROL(d->PortDescriptors[p]))
408 n_control++;
409 else {
410 pa_assert(LADSPA_IS_PORT_OUTPUT(d->PortDescriptors[p]) && LADSPA_IS_PORT_CONTROL(d->PortDescriptors[p]));
411 pa_log_debug("Ignored control output port \"%s\".", d->PortNames[p]);
412 }
413 }
414
415 if ((input_port == (unsigned long) -1) || (output_port == (unsigned long) -1)) {
416 pa_log("Failed to identify input and output ports. "
417 "Right now this module can only deal with plugins which provide an 'Input' and an 'Output' audio port. "
418 "Patches welcome!");
419 goto fail;
420 }
421
422 u->block_size = pa_frame_align(pa_mempool_block_size_max(m->core->mempool), &ss);
423
424 u->input = (LADSPA_Data*) pa_xnew(uint8_t, u->block_size);
425 if (LADSPA_IS_INPLACE_BROKEN(d->Properties))
426 u->output = (LADSPA_Data*) pa_xnew(uint8_t, u->block_size);
427 else
428 u->output = u->input;
429
430 u->channels = ss.channels;
431
432 for (c = 0; c < ss.channels; c++) {
433 if (!(u->handle[c] = d->instantiate(d, ss.rate))) {
434 pa_log("Failed to instantiate plugin %s with label %s for channel %i", plugin, d->Label, c);
435 goto fail;
436 }
437
438 d->connect_port(u->handle[c], input_port, u->input);
439 d->connect_port(u->handle[c], output_port, u->output);
440 }
441
442 if (!cdata && n_control > 0) {
443 pa_log("This plugin requires specification of %lu control parameters.", n_control);
444 goto fail;
445 }
446
447 if (n_control > 0) {
448 const char *state = NULL;
449 char *k;
450 unsigned long h;
451
452 u->control = pa_xnew(LADSPA_Data, n_control);
453 use_default = pa_xnew(pa_bool_t, n_control);
454 p = 0;
455
456 while ((k = pa_split(cdata, ",", &state)) && p < n_control) {
457 float f;
458
459 if (*k == 0) {
460 use_default[p++] = TRUE;
461 pa_xfree(k);
462 continue;
463 }
464
465 if (pa_atof(k, &f) < 0) {
466 pa_log("Failed to parse control value '%s'", k);
467 pa_xfree(k);
468 goto fail;
469 }
470
471 pa_xfree(k);
472
473 use_default[p] = FALSE;
474 u->control[p++] = f;
475 }
476
477 /* The previous loop doesn't take the last control value into account
478 if it is left empty, so we do it here. */
479 if (*cdata == 0 || cdata[strlen(cdata) - 1] == ',') {
480 if (p < n_control)
481 use_default[p] = TRUE;
482 p++;
483 }
484
485 if (p > n_control || k) {
486 pa_log("Too many control values passed, %lu expected.", n_control);
487 if (k)
488 pa_xfree(k);
489 goto fail;
490 }
491
492 if (p < n_control) {
493 pa_log("Not enough control values passed, %lu expected, %lu passed.", n_control, p);
494 goto fail;
495 }
496
497 h = 0;
498 for (p = 0; p < d->PortCount; p++) {
499 LADSPA_PortRangeHintDescriptor hint = d->PortRangeHints[p].HintDescriptor;
500
501 if (!LADSPA_IS_PORT_CONTROL(d->PortDescriptors[p]))
502 continue;
503
504 if (LADSPA_IS_PORT_OUTPUT(d->PortDescriptors[p])) {
505 for (c = 0; c < ss.channels; c++)
506 d->connect_port(u->handle[c], p, &u->control_out);
507 continue;
508 }
509
510 pa_assert(h < n_control);
511
512 if (use_default[h]) {
513 LADSPA_Data lower, upper;
514
515 if (!LADSPA_IS_HINT_HAS_DEFAULT(hint)) {
516 pa_log("Control port value left empty but plugin defines no default.");
517 goto fail;
518 }
519
520 lower = d->PortRangeHints[p].LowerBound;
521 upper = d->PortRangeHints[p].UpperBound;
522
523 if (LADSPA_IS_HINT_SAMPLE_RATE(hint)) {
524 lower *= ss.rate;
525 upper *= ss.rate;
526 }
527
528 switch (hint & LADSPA_HINT_DEFAULT_MASK) {
529
530 case LADSPA_HINT_DEFAULT_MINIMUM:
531 u->control[h] = lower;
532 break;
533
534 case LADSPA_HINT_DEFAULT_MAXIMUM:
535 u->control[h] = upper;
536 break;
537
538 case LADSPA_HINT_DEFAULT_LOW:
539 if (LADSPA_IS_HINT_LOGARITHMIC(hint))
540 u->control[h] = exp(log(lower) * 0.75 + log(upper) * 0.25);
541 else
542 u->control[h] = lower * 0.75 + upper * 0.25;
543 break;
544
545 case LADSPA_HINT_DEFAULT_MIDDLE:
546 if (LADSPA_IS_HINT_LOGARITHMIC(hint))
547 u->control[h] = exp(log(lower) * 0.5 + log(upper) * 0.5);
548 else
549 u->control[h] = lower * 0.5 + upper * 0.5;
550 break;
551
552 case LADSPA_HINT_DEFAULT_HIGH:
553 if (LADSPA_IS_HINT_LOGARITHMIC(hint))
554 u->control[h] = exp(log(lower) * 0.25 + log(upper) * 0.75);
555 else
556 u->control[h] = lower * 0.25 + upper * 0.75;
557 break;
558
559 case LADSPA_HINT_DEFAULT_0:
560 u->control[h] = 0;
561 break;
562
563 case LADSPA_HINT_DEFAULT_1:
564 u->control[h] = 1;
565 break;
566
567 case LADSPA_HINT_DEFAULT_100:
568 u->control[h] = 100;
569 break;
570
571 case LADSPA_HINT_DEFAULT_440:
572 u->control[h] = 440;
573 break;
574
575 default:
576 pa_assert_not_reached();
577 }
578 }
579
580 if (LADSPA_IS_HINT_INTEGER(hint))
581 u->control[h] = roundf(u->control[h]);
582
583 pa_log_debug("Binding %f to port %s", u->control[h], d->PortNames[p]);
584
585 for (c = 0; c < ss.channels; c++)
586 d->connect_port(u->handle[c], p, &u->control[h]);
587
588 h++;
589 }
590
591 pa_assert(h == n_control);
592 }
593
594 if (d->activate)
595 for (c = 0; c < u->channels; c++)
596 d->activate(u->handle[c]);
597
598 /* Create sink */
599 pa_sink_new_data_init(&sink_data);
600 sink_data.driver = __FILE__;
601 sink_data.module = m;
602 if (!(sink_data.name = pa_xstrdup(pa_modargs_get_value(ma, "sink_name", NULL))))
603 sink_data.name = pa_sprintf_malloc("%s.ladspa", master->name);
604 sink_data.namereg_fail = FALSE;
605 pa_sink_new_data_set_sample_spec(&sink_data, &ss);
606 pa_sink_new_data_set_channel_map(&sink_data, &map);
607 z = pa_proplist_gets(master->proplist, PA_PROP_DEVICE_DESCRIPTION);
608 pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_DESCRIPTION, t = pa_sprintf_malloc("LADSPA Plugin %s on %s", label, z ? z : master->name));
609 pa_xfree(t);
610 pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_MASTER_DEVICE, master->name);
611 pa_proplist_sets(sink_data.proplist, "device.ladspa.module", plugin);
612 pa_proplist_sets(sink_data.proplist, "device.ladspa.label", d->Label);
613 pa_proplist_sets(sink_data.proplist, "device.ladspa.name", d->Name);
614 pa_proplist_sets(sink_data.proplist, "device.ladspa.maker", d->Maker);
615 pa_proplist_sets(sink_data.proplist, "device.ladspa.copyright", d->Copyright);
616 pa_proplist_sets(sink_data.proplist, "device.ladspa.unique_id", t = pa_sprintf_malloc("%lu", (unsigned long) d->UniqueID));
617 pa_xfree(t);
618
619 u->sink = pa_sink_new(m->core, &sink_data, 0);
620 pa_sink_new_data_done(&sink_data);
621
622 if (!u->sink) {
623 pa_log("Failed to create sink.");
624 goto fail;
625 }
626
627 u->sink->parent.process_msg = sink_process_msg;
628 u->sink->set_state = sink_set_state;
629 u->sink->update_requested_latency = sink_update_requested_latency;
630 u->sink->request_rewind = sink_request_rewind;
631 u->sink->userdata = u;
632 u->sink->flags = PA_SINK_LATENCY;
633
634 pa_sink_set_asyncmsgq(u->sink, master->asyncmsgq);
635 pa_sink_set_rtpoll(u->sink, master->rtpoll);
636
637 /* Create sink input */
638 pa_sink_input_new_data_init(&sink_input_data);
639 sink_input_data.driver = __FILE__;
640 sink_input_data.module = m;
641 sink_input_data.sink = u->master;
642 pa_proplist_sets(sink_input_data.proplist, PA_PROP_MEDIA_NAME, "LADSPA Stream");
643 pa_proplist_sets(sink_input_data.proplist, PA_PROP_MEDIA_ROLE, "routing");
644 pa_sink_input_new_data_set_sample_spec(&sink_input_data, &ss);
645 pa_sink_input_new_data_set_channel_map(&sink_input_data, &map);
646
647 u->sink_input = pa_sink_input_new(m->core, &sink_input_data, PA_SINK_INPUT_DONT_MOVE);
648 pa_sink_input_new_data_done(&sink_input_data);
649
650 if (!u->sink_input)
651 goto fail;
652
653 u->sink_input->parent.process_msg = sink_input_process_msg;
654 u->sink_input->pop = sink_input_pop_cb;
655 u->sink_input->rewind = sink_input_rewind_cb;
656 u->sink_input->set_max_rewind = sink_input_set_max_rewind_cb;
657 u->sink_input->kill = sink_input_kill_cb;
658 u->sink_input->attach = sink_input_attach_cb;
659 u->sink_input->detach = sink_input_detach_cb;
660 u->sink_input->userdata = u;
661
662 pa_sink_put(u->sink);
663 pa_sink_input_put(u->sink_input);
664
665 pa_modargs_free(ma);
666
667 pa_xfree(use_default);
668
669 return 0;
670
671 fail:
672 if (ma)
673 pa_modargs_free(ma);
674
675 pa_xfree(use_default);
676
677 pa__done(m);
678
679 return -1;
680 }
681
682 void pa__done(pa_module*m) {
683 struct userdata *u;
684 unsigned c;
685
686 pa_assert(m);
687
688 if (!(u = m->userdata))
689 return;
690
691 if (u->sink_input) {
692 pa_sink_input_unlink(u->sink_input);
693 pa_sink_input_unref(u->sink_input);
694 }
695
696 if (u->sink) {
697 pa_sink_unlink(u->sink);
698 pa_sink_unref(u->sink);
699 }
700
701 for (c = 0; c < u->channels; c++)
702 if (u->handle[c]) {
703 if (u->descriptor->deactivate)
704 u->descriptor->deactivate(u->handle[c]);
705 u->descriptor->cleanup(u->handle[c]);
706 }
707
708 if (u->output != u->input)
709 pa_xfree(u->output);
710
711 pa_xfree(u->input);
712
713 pa_xfree(u->control);
714
715 pa_xfree(u);
716 }