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