Merge commit 'origin/master-tx'
[platform/upstream/pulseaudio.git] / src / modules / module-ladspa-sink.c
1 /***
2   This file is part of PulseAudio.
3
4   Copyright 2004-2008 Lennart Poettering
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 /* TODO: Some plugins cause latency, and some even report it by using a control
23    out port. We don't currently use the latency information. */
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <pulse/xmalloc.h>
30 #include <pulse/i18n.h>
31
32 #include <pulsecore/core-error.h>
33 #include <pulsecore/namereg.h>
34 #include <pulsecore/sink.h>
35 #include <pulsecore/module.h>
36 #include <pulsecore/core-util.h>
37 #include <pulsecore/modargs.h>
38 #include <pulsecore/log.h>
39 #include <pulsecore/thread.h>
40 #include <pulsecore/thread-mq.h>
41 #include <pulsecore/rtpoll.h>
42 #include <pulsecore/sample-util.h>
43 #include <pulsecore/ltdl-helper.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           "sink_properties=<properties for the sink> "
55           "master=<name of sink to filter> "
56           "format=<sample format> "
57           "rate=<sample rate> "
58           "channels=<number of channels> "
59           "channel_map=<channel map> "
60           "plugin=<ladspa plugin name> "
61           "label=<ladspa plugin label> "
62           "control=<comma seperated list of input control values>"));
63
64 #define MEMBLOCKQ_MAXLENGTH (16*1024*1024)
65
66 struct userdata {
67     pa_core *core;
68     pa_module *module;
69
70     pa_sink *sink, *master;
71     pa_sink_input *sink_input;
72
73     const LADSPA_Descriptor *descriptor;
74     unsigned channels;
75     LADSPA_Handle handle[PA_CHANNELS_MAX];
76     LADSPA_Data *input, *output;
77     size_t block_size;
78     unsigned long input_port, output_port;
79     LADSPA_Data *control;
80
81     /* This is a dummy buffer. Every port must be connected, but we don't care
82        about control out ports. We connect them all to this single buffer. */
83     LADSPA_Data control_out;
84
85     pa_memblockq *memblockq;
86 };
87
88 static const char* const valid_modargs[] = {
89     "sink_name",
90     "sink_properties",
91     "master",
92     "format",
93     "rate",
94     "channels",
95     "channel_map",
96     "plugin",
97     "label",
98     "control",
99     NULL
100 };
101
102 /* Called from I/O thread context */
103 static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
104     struct userdata *u = PA_SINK(o)->userdata;
105
106     switch (code) {
107
108         case PA_SINK_MESSAGE_GET_LATENCY: {
109             pa_usec_t usec = 0;
110
111             /* Get the latency of the master sink */
112             if (PA_MSGOBJECT(u->master)->process_msg(PA_MSGOBJECT(u->master), PA_SINK_MESSAGE_GET_LATENCY, &usec, 0, NULL) < 0)
113                 usec = 0;
114
115             /* Add the latency internal to our sink input on top */
116             usec += pa_bytes_to_usec(pa_memblockq_get_length(u->sink_input->thread_info.render_memblockq), &u->master->sample_spec);
117
118             *((pa_usec_t*) data) = usec;
119             return 0;
120         }
121     }
122
123     return pa_sink_process_msg(o, code, data, offset, chunk);
124 }
125
126 /* Called from main context */
127 static int sink_set_state(pa_sink *s, pa_sink_state_t state) {
128     struct userdata *u;
129
130     pa_sink_assert_ref(s);
131     pa_assert_se(u = s->userdata);
132
133     if (PA_SINK_IS_LINKED(state) &&
134         u->sink_input &&
135         PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(u->sink_input)))
136
137         pa_sink_input_cork(u->sink_input, state == PA_SINK_SUSPENDED);
138
139     return 0;
140 }
141
142 /* Called from I/O thread context */
143 static void sink_request_rewind(pa_sink *s) {
144     struct userdata *u;
145
146     pa_sink_assert_ref(s);
147     pa_assert_se(u = s->userdata);
148
149     /* Just hand this one over to the master sink */
150     pa_sink_input_request_rewind(u->sink_input, s->thread_info.rewind_nbytes + pa_memblockq_get_length(u->memblockq), TRUE, FALSE, FALSE);
151 }
152
153 /* Called from I/O thread context */
154 static void sink_update_requested_latency(pa_sink *s) {
155     struct userdata *u;
156
157     pa_sink_assert_ref(s);
158     pa_assert_se(u = s->userdata);
159
160     /* Just hand this one over to the master sink */
161     pa_sink_input_set_requested_latency_within_thread(
162             u->sink_input,
163             pa_sink_get_requested_latency_within_thread(s));
164 }
165
166 /* Called from I/O thread context */
167 static int sink_input_pop_cb(pa_sink_input *i, size_t nbytes, pa_memchunk *chunk) {
168     struct userdata *u;
169     float *src, *dst;
170     size_t fs;
171     unsigned n, c;
172     pa_memchunk tchunk;
173
174     pa_sink_input_assert_ref(i);
175     pa_assert(chunk);
176     pa_assert_se(u = i->userdata);
177
178     if (!u->sink || !PA_SINK_IS_OPENED(u->sink->thread_info.state))
179         return -1;
180
181     while (pa_memblockq_peek(u->memblockq, &tchunk) < 0) {
182         pa_memchunk nchunk;
183
184         pa_sink_render(u->sink, nbytes, &nchunk);
185         pa_memblockq_push(u->memblockq, &nchunk);
186         pa_memblock_unref(nchunk.memblock);
187     }
188
189     tchunk.length = PA_MIN(nbytes, tchunk.length);
190     pa_assert(tchunk.length > 0);
191
192     fs = pa_frame_size(&i->sample_spec);
193     n = (unsigned) (PA_MIN(tchunk.length, u->block_size) / fs);
194
195     pa_assert(n > 0);
196
197     chunk->index = 0;
198     chunk->length = n*fs;
199     chunk->memblock = pa_memblock_new(i->sink->core->mempool, chunk->length);
200
201     pa_memblockq_drop(u->memblockq, chunk->length);
202
203     src = (float*) ((uint8_t*) pa_memblock_acquire(tchunk.memblock) + tchunk.index);
204     dst = (float*) pa_memblock_acquire(chunk->memblock);
205
206     for (c = 0; c < u->channels; c++) {
207         pa_sample_clamp(PA_SAMPLE_FLOAT32NE, u->input, sizeof(float), src+c, u->channels*sizeof(float), n);
208         u->descriptor->run(u->handle[c], n);
209         pa_sample_clamp(PA_SAMPLE_FLOAT32NE, dst+c, u->channels*sizeof(float), u->output, sizeof(float), n);
210     }
211
212     pa_memblock_release(tchunk.memblock);
213     pa_memblock_release(chunk->memblock);
214
215     pa_memblock_unref(tchunk.memblock);
216
217     return 0;
218 }
219
220 /* Called from I/O thread context */
221 static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes) {
222     struct userdata *u;
223     size_t amount = 0;
224
225     pa_sink_input_assert_ref(i);
226     pa_assert_se(u = i->userdata);
227
228     if (!u->sink || !PA_SINK_IS_OPENED(u->sink->thread_info.state))
229         return;
230
231     if (u->sink->thread_info.rewind_nbytes > 0) {
232         size_t max_rewrite;
233
234         max_rewrite = nbytes + pa_memblockq_get_length(u->memblockq);
235         amount = PA_MIN(u->sink->thread_info.rewind_nbytes, max_rewrite);
236         u->sink->thread_info.rewind_nbytes = 0;
237
238         if (amount > 0) {
239             unsigned c;
240
241             pa_memblockq_seek(u->memblockq, - (int64_t) amount, PA_SEEK_RELATIVE, TRUE);
242
243             pa_log_debug("Resetting plugin");
244
245             /* Reset the plugin */
246             if (u->descriptor->deactivate)
247                 for (c = 0; c < u->channels; c++)
248                     u->descriptor->deactivate(u->handle[c]);
249             if (u->descriptor->activate)
250                 for (c = 0; c < u->channels; c++)
251                     u->descriptor->activate(u->handle[c]);
252         }
253     }
254
255     pa_sink_process_rewind(u->sink, amount);
256     pa_memblockq_rewind(u->memblockq, nbytes);
257 }
258
259 /* Called from I/O thread context */
260 static void sink_input_update_max_rewind_cb(pa_sink_input *i, size_t nbytes) {
261     struct userdata *u;
262
263     pa_sink_input_assert_ref(i);
264     pa_assert_se(u = i->userdata);
265
266     if (!u->sink || !PA_SINK_IS_LINKED(u->sink->thread_info.state))
267         return;
268
269     pa_memblockq_set_maxrewind(u->memblockq, nbytes);
270     pa_sink_set_max_rewind_within_thread(u->sink, nbytes);
271 }
272
273 /* Called from I/O thread context */
274 static void sink_input_update_max_request_cb(pa_sink_input *i, size_t nbytes) {
275     struct userdata *u;
276
277     pa_sink_input_assert_ref(i);
278     pa_assert_se(u = i->userdata);
279
280     if (!u->sink || !PA_SINK_IS_LINKED(u->sink->thread_info.state))
281         return;
282
283     pa_sink_set_max_request_within_thread(u->sink, nbytes);
284 }
285
286 /* Called from I/O thread context */
287 static void sink_input_update_sink_latency_range_cb(pa_sink_input *i) {
288     struct userdata *u;
289
290     pa_sink_input_assert_ref(i);
291     pa_assert_se(u = i->userdata);
292
293     if (!u->sink || !PA_SINK_IS_LINKED(u->sink->thread_info.state))
294         return;
295
296     pa_sink_set_latency_range_within_thread(u->sink, i->sink->thread_info.min_latency, i->sink->thread_info.max_latency);
297 }
298
299 /* Called from I/O thread context */
300 static void sink_input_detach_cb(pa_sink_input *i) {
301     struct userdata *u;
302
303     pa_sink_input_assert_ref(i);
304     pa_assert_se(u = i->userdata);
305
306     if (!u->sink || !PA_SINK_IS_LINKED(u->sink->thread_info.state))
307         return;
308
309     pa_sink_detach_within_thread(u->sink);
310     pa_sink_set_asyncmsgq(u->sink, NULL);
311     pa_sink_set_rtpoll(u->sink, NULL);
312 }
313
314 /* Called from I/O thread context */
315 static void sink_input_attach_cb(pa_sink_input *i) {
316     struct userdata *u;
317
318     pa_sink_input_assert_ref(i);
319     pa_assert_se(u = i->userdata);
320
321     if (!u->sink || !PA_SINK_IS_LINKED(u->sink->thread_info.state))
322         return;
323
324     pa_sink_set_asyncmsgq(u->sink, i->sink->asyncmsgq);
325     pa_sink_set_rtpoll(u->sink, i->sink->rtpoll);
326     pa_sink_attach_within_thread(u->sink);
327
328     pa_sink_set_latency_range_within_thread(u->sink, u->master->thread_info.min_latency, u->master->thread_info.max_latency);
329 }
330
331 /* Called from main context */
332 static void sink_input_kill_cb(pa_sink_input *i) {
333     struct userdata *u;
334
335     pa_sink_input_assert_ref(i);
336     pa_assert_se(u = i->userdata);
337
338     pa_sink_unlink(u->sink);
339     pa_sink_input_unlink(u->sink_input);
340
341     pa_sink_unref(u->sink);
342     u->sink = NULL;
343     pa_sink_input_unref(u->sink_input);
344     u->sink_input = NULL;
345
346     pa_module_unload_request(u->module, TRUE);
347 }
348
349 /* Called from IO thread context */
350 static void sink_input_state_change_cb(pa_sink_input *i, pa_sink_input_state_t state) {
351     struct userdata *u;
352
353     pa_sink_input_assert_ref(i);
354     pa_assert_se(u = i->userdata);
355
356     /* If we are added for the first time, ask for a rewinding so that
357      * we are heard right-away. */
358     if (PA_SINK_INPUT_IS_LINKED(state) &&
359         i->thread_info.state == PA_SINK_INPUT_INIT) {
360         pa_log_debug("Requesting rewind due to state change.");
361         pa_sink_input_request_rewind(i, 0, FALSE, TRUE, TRUE);
362     }
363 }
364
365 /* Called from main context */
366 static pa_bool_t sink_input_may_move_to_cb(pa_sink_input *i, pa_sink *dest) {
367     struct userdata *u;
368
369     pa_sink_input_assert_ref(i);
370     pa_assert_se(u = i->userdata);
371
372     return u->sink != dest;
373 }
374
375 int pa__init(pa_module*m) {
376     struct userdata *u;
377     pa_sample_spec ss;
378     pa_channel_map map;
379     pa_modargs *ma;
380     char *t;
381     const char *z;
382     pa_sink *master;
383     pa_sink_input_new_data sink_input_data;
384     pa_sink_new_data sink_data;
385     const char *plugin, *label;
386     LADSPA_Descriptor_Function descriptor_func;
387     const char *e, *cdata;
388     const LADSPA_Descriptor *d;
389     unsigned long input_port, output_port, p, j, n_control;
390     unsigned c;
391     pa_bool_t *use_default = NULL;
392
393     pa_assert(m);
394
395     pa_assert(sizeof(LADSPA_Data) == sizeof(float));
396
397     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
398         pa_log("Failed to parse module arguments.");
399         goto fail;
400     }
401
402     if (!(master = pa_namereg_get(m->core, pa_modargs_get_value(ma, "master", NULL), PA_NAMEREG_SINK))) {
403         pa_log("Master sink not found");
404         goto fail;
405     }
406
407     ss = master->sample_spec;
408     ss.format = PA_SAMPLE_FLOAT32;
409     map = master->channel_map;
410     if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
411         pa_log("Invalid sample format specification or channel map");
412         goto fail;
413     }
414
415     if (!(plugin = pa_modargs_get_value(ma, "plugin", NULL))) {
416         pa_log("Missing LADSPA plugin name");
417         goto fail;
418     }
419
420     if (!(label = pa_modargs_get_value(ma, "label", NULL))) {
421         pa_log("Missing LADSPA plugin label");
422         goto fail;
423     }
424
425     cdata = pa_modargs_get_value(ma, "control", NULL);
426
427     u = pa_xnew0(struct userdata, 1);
428     u->core = m->core;
429     u->module = m;
430     m->userdata = u;
431     u->master = master;
432     u->sink = NULL;
433     u->sink_input = NULL;
434     u->memblockq = pa_memblockq_new(0, MEMBLOCKQ_MAXLENGTH, 0, pa_frame_size(&ss), 1, 1, 0, NULL);
435
436     if (!(e = getenv("LADSPA_PATH")))
437         e = LADSPA_PATH;
438
439     /* FIXME: This is not exactly thread safe */
440     t = pa_xstrdup(lt_dlgetsearchpath());
441     lt_dlsetsearchpath(e);
442     m->dl = lt_dlopenext(plugin);
443     lt_dlsetsearchpath(t);
444     pa_xfree(t);
445
446     if (!m->dl) {
447         pa_log("Failed to load LADSPA plugin: %s", lt_dlerror());
448         goto fail;
449     }
450
451     if (!(descriptor_func = (LADSPA_Descriptor_Function) pa_load_sym(m->dl, NULL, "ladspa_descriptor"))) {
452         pa_log("LADSPA module lacks ladspa_descriptor() symbol.");
453         goto fail;
454     }
455
456     for (j = 0;; j++) {
457
458         if (!(d = descriptor_func(j))) {
459             pa_log("Failed to find plugin label '%s' in plugin '%s'.", label, plugin);
460             goto fail;
461         }
462
463         if (strcmp(d->Label, label) == 0)
464             break;
465     }
466
467     u->descriptor = d;
468
469     pa_log_debug("Module: %s", plugin);
470     pa_log_debug("Label: %s", d->Label);
471     pa_log_debug("Unique ID: %lu", d->UniqueID);
472     pa_log_debug("Name: %s", d->Name);
473     pa_log_debug("Maker: %s", d->Maker);
474     pa_log_debug("Copyright: %s", d->Copyright);
475
476     input_port = output_port = (unsigned long) -1;
477     n_control = 0;
478
479     for (p = 0; p < d->PortCount; p++) {
480
481         if (LADSPA_IS_PORT_INPUT(d->PortDescriptors[p]) && LADSPA_IS_PORT_AUDIO(d->PortDescriptors[p])) {
482
483             if (strcmp(d->PortNames[p], "Input") == 0) {
484                 pa_assert(input_port == (unsigned long) -1);
485                 input_port = p;
486             } else {
487                 pa_log("Found audio input port on plugin we cannot handle: %s", d->PortNames[p]);
488                 goto fail;
489             }
490
491         } else if (LADSPA_IS_PORT_OUTPUT(d->PortDescriptors[p]) && LADSPA_IS_PORT_AUDIO(d->PortDescriptors[p])) {
492
493             if (strcmp(d->PortNames[p], "Output") == 0) {
494                 pa_assert(output_port == (unsigned long) -1);
495                 output_port = p;
496             } else {
497                 pa_log("Found audio output port on plugin we cannot handle: %s", d->PortNames[p]);
498                 goto fail;
499             }
500
501         } else if (LADSPA_IS_PORT_INPUT(d->PortDescriptors[p]) && LADSPA_IS_PORT_CONTROL(d->PortDescriptors[p]))
502             n_control++;
503         else {
504             pa_assert(LADSPA_IS_PORT_OUTPUT(d->PortDescriptors[p]) && LADSPA_IS_PORT_CONTROL(d->PortDescriptors[p]));
505             pa_log_debug("Ignored control output port \"%s\".", d->PortNames[p]);
506         }
507     }
508
509     if ((input_port == (unsigned long) -1) || (output_port == (unsigned long) -1)) {
510         pa_log("Failed to identify input and output ports. "
511                "Right now this module can only deal with plugins which provide an 'Input' and an 'Output' audio port. "
512                "Patches welcome!");
513         goto fail;
514     }
515
516     u->block_size = pa_frame_align(pa_mempool_block_size_max(m->core->mempool), &ss);
517
518     u->input = (LADSPA_Data*) pa_xnew(uint8_t, (unsigned) u->block_size);
519     if (LADSPA_IS_INPLACE_BROKEN(d->Properties))
520         u->output = (LADSPA_Data*) pa_xnew(uint8_t, (unsigned) u->block_size);
521     else
522         u->output = u->input;
523
524     u->channels = ss.channels;
525
526     for (c = 0; c < ss.channels; c++) {
527         if (!(u->handle[c] = d->instantiate(d, ss.rate))) {
528             pa_log("Failed to instantiate plugin %s with label %s for channel %i", plugin, d->Label, c);
529             goto fail;
530         }
531
532         d->connect_port(u->handle[c], input_port, u->input);
533         d->connect_port(u->handle[c], output_port, u->output);
534     }
535
536     if (!cdata && n_control > 0) {
537         pa_log("This plugin requires specification of %lu control parameters.", n_control);
538         goto fail;
539     }
540
541     if (n_control > 0) {
542         const char *state = NULL;
543         char *k;
544         unsigned long h;
545
546         u->control = pa_xnew(LADSPA_Data, (unsigned) n_control);
547         use_default = pa_xnew(pa_bool_t, (unsigned) n_control);
548         p = 0;
549
550         while ((k = pa_split(cdata, ",", &state)) && p < n_control) {
551             double f;
552
553             if (*k == 0) {
554                 use_default[p++] = TRUE;
555                 pa_xfree(k);
556                 continue;
557             }
558
559             if (pa_atod(k, &f) < 0) {
560                 pa_log("Failed to parse control value '%s'", k);
561                 pa_xfree(k);
562                 goto fail;
563             }
564
565             pa_xfree(k);
566
567             use_default[p] = FALSE;
568             u->control[p++] = (LADSPA_Data) f;
569         }
570
571         /* The previous loop doesn't take the last control value into account
572            if it is left empty, so we do it here. */
573         if (*cdata == 0 || cdata[strlen(cdata) - 1] == ',') {
574             if (p < n_control)
575                 use_default[p] = TRUE;
576             p++;
577         }
578
579         if (p > n_control || k) {
580             pa_log("Too many control values passed, %lu expected.", n_control);
581             pa_xfree(k);
582             goto fail;
583         }
584
585         if (p < n_control) {
586             pa_log("Not enough control values passed, %lu expected, %lu passed.", n_control, p);
587             goto fail;
588         }
589
590         h = 0;
591         for (p = 0; p < d->PortCount; p++) {
592             LADSPA_PortRangeHintDescriptor hint = d->PortRangeHints[p].HintDescriptor;
593
594             if (!LADSPA_IS_PORT_CONTROL(d->PortDescriptors[p]))
595                 continue;
596
597             if (LADSPA_IS_PORT_OUTPUT(d->PortDescriptors[p])) {
598                 for (c = 0; c < ss.channels; c++)
599                     d->connect_port(u->handle[c], p, &u->control_out);
600                 continue;
601             }
602
603             pa_assert(h < n_control);
604
605             if (use_default[h]) {
606                 LADSPA_Data lower, upper;
607
608                 if (!LADSPA_IS_HINT_HAS_DEFAULT(hint)) {
609                     pa_log("Control port value left empty but plugin defines no default.");
610                     goto fail;
611                 }
612
613                 lower = d->PortRangeHints[p].LowerBound;
614                 upper = d->PortRangeHints[p].UpperBound;
615
616                 if (LADSPA_IS_HINT_SAMPLE_RATE(hint)) {
617                     lower *= (LADSPA_Data) ss.rate;
618                     upper *= (LADSPA_Data) ss.rate;
619                 }
620
621                 switch (hint & LADSPA_HINT_DEFAULT_MASK) {
622
623                     case LADSPA_HINT_DEFAULT_MINIMUM:
624                         u->control[h] = lower;
625                         break;
626
627                     case LADSPA_HINT_DEFAULT_MAXIMUM:
628                         u->control[h] = upper;
629                         break;
630
631                     case LADSPA_HINT_DEFAULT_LOW:
632                         if (LADSPA_IS_HINT_LOGARITHMIC(hint))
633                             u->control[h] = (LADSPA_Data) exp(log(lower) * 0.75 + log(upper) * 0.25);
634                         else
635                             u->control[h] = (LADSPA_Data) (lower * 0.75 + upper * 0.25);
636                         break;
637
638                     case LADSPA_HINT_DEFAULT_MIDDLE:
639                         if (LADSPA_IS_HINT_LOGARITHMIC(hint))
640                             u->control[h] = (LADSPA_Data) exp(log(lower) * 0.5 + log(upper) * 0.5);
641                         else
642                             u->control[h] = (LADSPA_Data) (lower * 0.5 + upper * 0.5);
643                         break;
644
645                     case LADSPA_HINT_DEFAULT_HIGH:
646                         if (LADSPA_IS_HINT_LOGARITHMIC(hint))
647                             u->control[h] = (LADSPA_Data) exp(log(lower) * 0.25 + log(upper) * 0.75);
648                         else
649                             u->control[h] = (LADSPA_Data) (lower * 0.25 + upper * 0.75);
650                         break;
651
652                     case LADSPA_HINT_DEFAULT_0:
653                         u->control[h] = 0;
654                         break;
655
656                     case LADSPA_HINT_DEFAULT_1:
657                         u->control[h] = 1;
658                         break;
659
660                     case LADSPA_HINT_DEFAULT_100:
661                         u->control[h] = 100;
662                         break;
663
664                     case LADSPA_HINT_DEFAULT_440:
665                         u->control[h] = 440;
666                         break;
667
668                     default:
669                         pa_assert_not_reached();
670                 }
671             }
672
673             if (LADSPA_IS_HINT_INTEGER(hint))
674                 u->control[h] = roundf(u->control[h]);
675
676             pa_log_debug("Binding %f to port %s", u->control[h], d->PortNames[p]);
677
678             for (c = 0; c < ss.channels; c++)
679                 d->connect_port(u->handle[c], p, &u->control[h]);
680
681             h++;
682         }
683
684         pa_assert(h == n_control);
685     }
686
687     if (d->activate)
688         for (c = 0; c < u->channels; c++)
689             d->activate(u->handle[c]);
690
691     /* Create sink */
692     pa_sink_new_data_init(&sink_data);
693     sink_data.driver = __FILE__;
694     sink_data.module = m;
695     if (!(sink_data.name = pa_xstrdup(pa_modargs_get_value(ma, "sink_name", NULL))))
696         sink_data.name = pa_sprintf_malloc("%s.ladspa", master->name);
697     sink_data.namereg_fail = FALSE;
698     pa_sink_new_data_set_sample_spec(&sink_data, &ss);
699     pa_sink_new_data_set_channel_map(&sink_data, &map);
700     z = pa_proplist_gets(master->proplist, PA_PROP_DEVICE_DESCRIPTION);
701     pa_proplist_setf(sink_data.proplist, PA_PROP_DEVICE_DESCRIPTION, "LADSPA Plugin %s on %s", label, z ? z : master->name);
702     pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_MASTER_DEVICE, master->name);
703     pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_CLASS, "filter");
704     pa_proplist_sets(sink_data.proplist, "device.ladspa.module", plugin);
705     pa_proplist_sets(sink_data.proplist, "device.ladspa.label", d->Label);
706     pa_proplist_sets(sink_data.proplist, "device.ladspa.name", d->Name);
707     pa_proplist_sets(sink_data.proplist, "device.ladspa.maker", d->Maker);
708     pa_proplist_sets(sink_data.proplist, "device.ladspa.copyright", d->Copyright);
709     pa_proplist_setf(sink_data.proplist, "device.ladspa.unique_id", "%lu", (unsigned long) d->UniqueID);
710
711     if (pa_modargs_get_proplist(ma, "sink_properties", sink_data.proplist, PA_UPDATE_REPLACE) < 0) {
712         pa_log("Invalid properties");
713         pa_sink_new_data_done(&sink_data);
714         goto fail;
715     }
716
717     u->sink = pa_sink_new(m->core, &sink_data, PA_SINK_LATENCY|PA_SINK_DYNAMIC_LATENCY);
718     pa_sink_new_data_done(&sink_data);
719
720     if (!u->sink) {
721         pa_log("Failed to create sink.");
722         goto fail;
723     }
724
725     u->sink->parent.process_msg = sink_process_msg;
726     u->sink->set_state = sink_set_state;
727     u->sink->update_requested_latency = sink_update_requested_latency;
728     u->sink->request_rewind = sink_request_rewind;
729     u->sink->userdata = u;
730
731     pa_sink_set_asyncmsgq(u->sink, master->asyncmsgq);
732     pa_sink_set_rtpoll(u->sink, master->rtpoll);
733
734     /* Create sink input */
735     pa_sink_input_new_data_init(&sink_input_data);
736     sink_input_data.driver = __FILE__;
737     sink_input_data.module = m;
738     sink_input_data.sink = u->master;
739     pa_proplist_sets(sink_input_data.proplist, PA_PROP_MEDIA_NAME, "LADSPA Stream");
740     pa_proplist_sets(sink_input_data.proplist, PA_PROP_MEDIA_ROLE, "filter");
741     pa_sink_input_new_data_set_sample_spec(&sink_input_data, &ss);
742     pa_sink_input_new_data_set_channel_map(&sink_input_data, &map);
743
744     pa_sink_input_new(&u->sink_input, m->core, &sink_input_data, PA_SINK_INPUT_DONT_MOVE);
745     pa_sink_input_new_data_done(&sink_input_data);
746
747     if (!u->sink_input)
748         goto fail;
749
750     u->sink_input->pop = sink_input_pop_cb;
751     u->sink_input->process_rewind = sink_input_process_rewind_cb;
752     u->sink_input->update_max_rewind = sink_input_update_max_rewind_cb;
753     u->sink_input->update_max_request = sink_input_update_max_request_cb;
754     u->sink_input->update_sink_latency_range = sink_input_update_sink_latency_range_cb;
755     u->sink_input->kill = sink_input_kill_cb;
756     u->sink_input->attach = sink_input_attach_cb;
757     u->sink_input->detach = sink_input_detach_cb;
758     u->sink_input->state_change = sink_input_state_change_cb;
759     u->sink_input->may_move_to = sink_input_may_move_to_cb;
760     u->sink_input->userdata = u;
761
762     pa_sink_put(u->sink);
763     pa_sink_input_put(u->sink_input);
764
765     pa_modargs_free(ma);
766
767     pa_xfree(use_default);
768
769     return 0;
770
771 fail:
772     if (ma)
773         pa_modargs_free(ma);
774
775     pa_xfree(use_default);
776
777     pa__done(m);
778
779     return -1;
780 }
781
782 int pa__get_n_used(pa_module *m) {
783     struct userdata *u;
784
785     pa_assert(m);
786     pa_assert_se(u = m->userdata);
787
788     return pa_sink_linked_by(u->sink);
789 }
790
791 void pa__done(pa_module*m) {
792     struct userdata *u;
793     unsigned c;
794
795     pa_assert(m);
796
797     if (!(u = m->userdata))
798         return;
799
800     if (u->sink) {
801         pa_sink_unlink(u->sink);
802         pa_sink_unref(u->sink);
803     }
804
805     if (u->sink_input) {
806         pa_sink_input_unlink(u->sink_input);
807         pa_sink_input_unref(u->sink_input);
808     }
809
810     for (c = 0; c < u->channels; c++)
811         if (u->handle[c]) {
812             if (u->descriptor->deactivate)
813                 u->descriptor->deactivate(u->handle[c]);
814             u->descriptor->cleanup(u->handle[c]);
815         }
816
817     if (u->output != u->input)
818         pa_xfree(u->output);
819
820     if (u->memblockq)
821         pa_memblockq_free(u->memblockq);
822
823     pa_xfree(u->input);
824
825     pa_xfree(u->control);
826
827     pa_xfree(u);
828 }