module-ladspa: allow moving of sink, forward fixed latency
[pulseaudio-mirror.git] / src / modules / module-ladspa-sink.c
blobe838be3e9404602d4bee3c8dac999ad9bcee0cce
1 /***
2 This file is part of PulseAudio.
4 Copyright 2004-2008 Lennart Poettering
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.
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.
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 ***/
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. */
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
29 #include <pulse/xmalloc.h>
30 #include <pulse/i18n.h>
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>
45 #include "module-ladspa-sink-symdef.h"
46 #include "ladspa.h"
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>"));
64 #define MEMBLOCKQ_MAXLENGTH (16*1024*1024)
66 struct userdata {
67 pa_module *module;
69 pa_sink *sink;
70 pa_sink_input *sink_input;
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;
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;
84 pa_memblockq *memblockq;
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
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;
105 switch (code) {
107 case PA_SINK_MESSAGE_GET_LATENCY:
109 /* The sink is _put() before the sink input is, so let's
110 * make sure we don't access it in that time. Also, the
111 * sink input is first shut down, the sink second. */
112 if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
113 !PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state)) {
114 *((pa_usec_t*) data) = 0;
115 return 0;
118 *((pa_usec_t*) data) =
120 /* Get the latency of the master sink */
121 pa_sink_get_latency_within_thread(u->sink_input->sink) +
123 /* Add the latency internal to our sink input on top */
124 pa_bytes_to_usec(pa_memblockq_get_length(u->sink_input->thread_info.render_memblockq), &u->sink_input->sink->sample_spec);
126 return 0;
129 return pa_sink_process_msg(o, code, data, offset, chunk);
132 /* Called from main context */
133 static int sink_set_state(pa_sink *s, pa_sink_state_t state) {
134 struct userdata *u;
136 pa_sink_assert_ref(s);
137 pa_assert_se(u = s->userdata);
139 if (!PA_SINK_IS_LINKED(state) ||
140 !PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(u->sink_input)))
141 return 0;
143 pa_sink_input_cork(u->sink_input, state == PA_SINK_SUSPENDED);
144 return 0;
147 /* Called from I/O thread context */
148 static void sink_request_rewind(pa_sink *s) {
149 struct userdata *u;
151 pa_sink_assert_ref(s);
152 pa_assert_se(u = s->userdata);
154 if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
155 !PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state))
156 return;
158 /* Just hand this one over to the master sink */
159 pa_sink_input_request_rewind(u->sink_input, s->thread_info.rewind_nbytes + pa_memblockq_get_length(u->memblockq), TRUE, FALSE, FALSE);
162 /* Called from I/O thread context */
163 static void sink_update_requested_latency(pa_sink *s) {
164 struct userdata *u;
166 pa_sink_assert_ref(s);
167 pa_assert_se(u = s->userdata);
169 if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
170 !PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state))
171 return;
173 /* Just hand this one over to the master sink */
174 pa_sink_input_set_requested_latency_within_thread(
175 u->sink_input,
176 pa_sink_get_requested_latency_within_thread(s));
179 /* Called from I/O thread context */
180 static int sink_input_pop_cb(pa_sink_input *i, size_t nbytes, pa_memchunk *chunk) {
181 struct userdata *u;
182 float *src, *dst;
183 size_t fs;
184 unsigned n, c;
185 pa_memchunk tchunk;
187 pa_sink_input_assert_ref(i);
188 pa_assert(chunk);
189 pa_assert_se(u = i->userdata);
191 /* Hmm, process any rewind request that might be queued up */
192 pa_sink_process_rewind(u->sink, 0);
194 while (pa_memblockq_peek(u->memblockq, &tchunk) < 0) {
195 pa_memchunk nchunk;
197 pa_sink_render(u->sink, nbytes, &nchunk);
198 pa_memblockq_push(u->memblockq, &nchunk);
199 pa_memblock_unref(nchunk.memblock);
202 tchunk.length = PA_MIN(nbytes, tchunk.length);
203 pa_assert(tchunk.length > 0);
205 fs = pa_frame_size(&i->sample_spec);
206 n = (unsigned) (PA_MIN(tchunk.length, u->block_size) / fs);
208 pa_assert(n > 0);
210 chunk->index = 0;
211 chunk->length = n*fs;
212 chunk->memblock = pa_memblock_new(i->sink->core->mempool, chunk->length);
214 pa_memblockq_drop(u->memblockq, chunk->length);
216 src = (float*) ((uint8_t*) pa_memblock_acquire(tchunk.memblock) + tchunk.index);
217 dst = (float*) pa_memblock_acquire(chunk->memblock);
219 for (c = 0; c < u->channels; c++) {
220 pa_sample_clamp(PA_SAMPLE_FLOAT32NE, u->input, sizeof(float), src+c, u->channels*sizeof(float), n);
221 u->descriptor->run(u->handle[c], n);
222 pa_sample_clamp(PA_SAMPLE_FLOAT32NE, dst+c, u->channels*sizeof(float), u->output, sizeof(float), n);
225 pa_memblock_release(tchunk.memblock);
226 pa_memblock_release(chunk->memblock);
228 pa_memblock_unref(tchunk.memblock);
230 return 0;
233 /* Called from I/O thread context */
234 static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes) {
235 struct userdata *u;
236 size_t amount = 0;
238 pa_sink_input_assert_ref(i);
239 pa_assert_se(u = i->userdata);
241 if (u->sink->thread_info.rewind_nbytes > 0) {
242 size_t max_rewrite;
244 max_rewrite = nbytes + pa_memblockq_get_length(u->memblockq);
245 amount = PA_MIN(u->sink->thread_info.rewind_nbytes, max_rewrite);
246 u->sink->thread_info.rewind_nbytes = 0;
248 if (amount > 0) {
249 unsigned c;
251 pa_memblockq_seek(u->memblockq, - (int64_t) amount, PA_SEEK_RELATIVE, TRUE);
253 pa_log_debug("Resetting plugin");
255 /* Reset the plugin */
256 if (u->descriptor->deactivate)
257 for (c = 0; c < u->channels; c++)
258 u->descriptor->deactivate(u->handle[c]);
259 if (u->descriptor->activate)
260 for (c = 0; c < u->channels; c++)
261 u->descriptor->activate(u->handle[c]);
265 pa_sink_process_rewind(u->sink, amount);
266 pa_memblockq_rewind(u->memblockq, nbytes);
269 /* Called from I/O thread context */
270 static void sink_input_update_max_rewind_cb(pa_sink_input *i, size_t nbytes) {
271 struct userdata *u;
273 pa_sink_input_assert_ref(i);
274 pa_assert_se(u = i->userdata);
276 pa_memblockq_set_maxrewind(u->memblockq, nbytes);
277 pa_sink_set_max_rewind_within_thread(u->sink, nbytes);
280 /* Called from I/O thread context */
281 static void sink_input_update_max_request_cb(pa_sink_input *i, size_t nbytes) {
282 struct userdata *u;
284 pa_sink_input_assert_ref(i);
285 pa_assert_se(u = i->userdata);
287 pa_sink_set_max_request_within_thread(u->sink, nbytes);
290 /* Called from I/O thread context */
291 static void sink_input_update_sink_latency_range_cb(pa_sink_input *i) {
292 struct userdata *u;
294 pa_sink_input_assert_ref(i);
295 pa_assert_se(u = i->userdata);
297 pa_sink_set_latency_range_within_thread(u->sink, i->sink->thread_info.min_latency, i->sink->thread_info.max_latency);
300 /* Called from I/O thread context */
301 static void sink_input_update_sink_fixed_latency_cb(pa_sink_input *i) {
302 struct userdata *u;
304 pa_sink_input_assert_ref(i);
305 pa_assert_se(u = i->userdata);
307 pa_sink_set_fixed_latency_within_thread(u->sink, i->sink->thread_info.fixed_latency);
310 /* Called from I/O thread context */
311 static void sink_input_detach_cb(pa_sink_input *i) {
312 struct userdata *u;
314 pa_sink_input_assert_ref(i);
315 pa_assert_se(u = i->userdata);
317 pa_sink_detach_within_thread(u->sink);
319 pa_sink_set_rtpoll(u->sink, NULL);
322 /* Called from I/O thread context */
323 static void sink_input_attach_cb(pa_sink_input *i) {
324 struct userdata *u;
326 pa_sink_input_assert_ref(i);
327 pa_assert_se(u = i->userdata);
329 pa_sink_set_rtpoll(u->sink, i->sink->thread_info.rtpoll);
330 pa_sink_set_latency_range_within_thread(u->sink, i->sink->thread_info.min_latency, i->sink->thread_info.max_latency);
331 pa_sink_set_fixed_latency_within_thread(u->sink, i->sink->thread_info.fixed_latency);
332 pa_sink_set_max_request_within_thread(u->sink, pa_sink_input_get_max_request(i));
333 pa_sink_set_max_rewind_within_thread(u->sink, pa_sink_input_get_max_rewind(i));
335 pa_sink_attach_within_thread(u->sink);
338 /* Called from main context */
339 static void sink_input_kill_cb(pa_sink_input *i) {
340 struct userdata *u;
342 pa_sink_input_assert_ref(i);
343 pa_assert_se(u = i->userdata);
345 /* The order here matters! We first kill the sink input, followed
346 * by the sink. That means the sink callbacks must be protected
347 * against an unconnected sink input! */
348 pa_sink_input_unlink(u->sink_input);
349 pa_sink_unlink(u->sink);
351 pa_sink_input_unref(u->sink_input);
352 u->sink_input = NULL;
354 pa_sink_unref(u->sink);
355 u->sink = NULL;
357 pa_module_unload_request(u->module, TRUE);
360 /* Called from IO thread context */
361 static void sink_input_state_change_cb(pa_sink_input *i, pa_sink_input_state_t state) {
362 struct userdata *u;
364 pa_sink_input_assert_ref(i);
365 pa_assert_se(u = i->userdata);
367 /* If we are added for the first time, ask for a rewinding so that
368 * we are heard right-away. */
369 if (PA_SINK_INPUT_IS_LINKED(state) &&
370 i->thread_info.state == PA_SINK_INPUT_INIT) {
371 pa_log_debug("Requesting rewind due to state change.");
372 pa_sink_input_request_rewind(i, 0, FALSE, TRUE, TRUE);
376 /* Called from main context */
377 static pa_bool_t sink_input_may_move_to_cb(pa_sink_input *i, pa_sink *dest) {
378 struct userdata *u;
380 pa_sink_input_assert_ref(i);
381 pa_assert_se(u = i->userdata);
383 return u->sink != dest;
386 /* Called from main context */
387 static void sink_input_moving_cb(pa_sink_input *i, pa_sink *dest) {
388 struct userdata *u;
390 pa_sink_input_assert_ref(i);
391 pa_assert_se(u = i->userdata);
393 pa_sink_set_asyncmsgq(u->sink, dest->asyncmsgq);
396 int pa__init(pa_module*m) {
397 struct userdata *u;
398 pa_sample_spec ss;
399 pa_channel_map map;
400 pa_modargs *ma;
401 char *t;
402 const char *z;
403 pa_sink *master;
404 pa_sink_input_new_data sink_input_data;
405 pa_sink_new_data sink_data;
406 const char *plugin, *label;
407 LADSPA_Descriptor_Function descriptor_func;
408 const char *e, *cdata;
409 const LADSPA_Descriptor *d;
410 unsigned long input_port, output_port, p, j, n_control;
411 unsigned c;
412 pa_bool_t *use_default = NULL;
414 pa_assert(m);
416 pa_assert_cc(sizeof(LADSPA_Data) == sizeof(float));
418 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
419 pa_log("Failed to parse module arguments.");
420 goto fail;
423 if (!(master = pa_namereg_get(m->core, pa_modargs_get_value(ma, "master", NULL), PA_NAMEREG_SINK))) {
424 pa_log("Master sink not found");
425 goto fail;
428 ss = master->sample_spec;
429 ss.format = PA_SAMPLE_FLOAT32;
430 map = master->channel_map;
431 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
432 pa_log("Invalid sample format specification or channel map");
433 goto fail;
436 if (!(plugin = pa_modargs_get_value(ma, "plugin", NULL))) {
437 pa_log("Missing LADSPA plugin name");
438 goto fail;
441 if (!(label = pa_modargs_get_value(ma, "label", NULL))) {
442 pa_log("Missing LADSPA plugin label");
443 goto fail;
446 cdata = pa_modargs_get_value(ma, "control", NULL);
448 u = pa_xnew0(struct userdata, 1);
449 u->module = m;
450 m->userdata = u;
451 u->memblockq = pa_memblockq_new(0, MEMBLOCKQ_MAXLENGTH, 0, pa_frame_size(&ss), 1, 1, 0, NULL);
453 if (!(e = getenv("LADSPA_PATH")))
454 e = LADSPA_PATH;
456 /* FIXME: This is not exactly thread safe */
457 t = pa_xstrdup(lt_dlgetsearchpath());
458 lt_dlsetsearchpath(e);
459 m->dl = lt_dlopenext(plugin);
460 lt_dlsetsearchpath(t);
461 pa_xfree(t);
463 if (!m->dl) {
464 pa_log("Failed to load LADSPA plugin: %s", lt_dlerror());
465 goto fail;
468 if (!(descriptor_func = (LADSPA_Descriptor_Function) pa_load_sym(m->dl, NULL, "ladspa_descriptor"))) {
469 pa_log("LADSPA module lacks ladspa_descriptor() symbol.");
470 goto fail;
473 for (j = 0;; j++) {
475 if (!(d = descriptor_func(j))) {
476 pa_log("Failed to find plugin label '%s' in plugin '%s'.", label, plugin);
477 goto fail;
480 if (strcmp(d->Label, label) == 0)
481 break;
484 u->descriptor = d;
486 pa_log_debug("Module: %s", plugin);
487 pa_log_debug("Label: %s", d->Label);
488 pa_log_debug("Unique ID: %lu", d->UniqueID);
489 pa_log_debug("Name: %s", d->Name);
490 pa_log_debug("Maker: %s", d->Maker);
491 pa_log_debug("Copyright: %s", d->Copyright);
493 input_port = output_port = (unsigned long) -1;
494 n_control = 0;
496 for (p = 0; p < d->PortCount; p++) {
498 if (LADSPA_IS_PORT_INPUT(d->PortDescriptors[p]) && LADSPA_IS_PORT_AUDIO(d->PortDescriptors[p])) {
500 if (strcmp(d->PortNames[p], "Input") == 0) {
501 pa_assert(input_port == (unsigned long) -1);
502 input_port = p;
503 } else {
504 pa_log("Found audio input port on plugin we cannot handle: %s", d->PortNames[p]);
505 goto fail;
508 } else if (LADSPA_IS_PORT_OUTPUT(d->PortDescriptors[p]) && LADSPA_IS_PORT_AUDIO(d->PortDescriptors[p])) {
510 if (strcmp(d->PortNames[p], "Output") == 0) {
511 pa_assert(output_port == (unsigned long) -1);
512 output_port = p;
513 } else {
514 pa_log("Found audio output port on plugin we cannot handle: %s", d->PortNames[p]);
515 goto fail;
518 } else if (LADSPA_IS_PORT_INPUT(d->PortDescriptors[p]) && LADSPA_IS_PORT_CONTROL(d->PortDescriptors[p]))
519 n_control++;
520 else {
521 pa_assert(LADSPA_IS_PORT_OUTPUT(d->PortDescriptors[p]) && LADSPA_IS_PORT_CONTROL(d->PortDescriptors[p]));
522 pa_log_debug("Ignored control output port \"%s\".", d->PortNames[p]);
526 if ((input_port == (unsigned long) -1) || (output_port == (unsigned long) -1)) {
527 pa_log("Failed to identify input and output ports. "
528 "Right now this module can only deal with plugins which provide an 'Input' and an 'Output' audio port. "
529 "Patches welcome!");
530 goto fail;
533 u->block_size = pa_frame_align(pa_mempool_block_size_max(m->core->mempool), &ss);
535 u->input = (LADSPA_Data*) pa_xnew(uint8_t, (unsigned) u->block_size);
536 if (LADSPA_IS_INPLACE_BROKEN(d->Properties))
537 u->output = (LADSPA_Data*) pa_xnew(uint8_t, (unsigned) u->block_size);
538 else
539 u->output = u->input;
541 u->channels = ss.channels;
543 for (c = 0; c < ss.channels; c++) {
544 if (!(u->handle[c] = d->instantiate(d, ss.rate))) {
545 pa_log("Failed to instantiate plugin %s with label %s for channel %i", plugin, d->Label, c);
546 goto fail;
549 d->connect_port(u->handle[c], input_port, u->input);
550 d->connect_port(u->handle[c], output_port, u->output);
553 if (!cdata && n_control > 0) {
554 pa_log("This plugin requires specification of %lu control parameters.", n_control);
555 goto fail;
558 if (n_control > 0) {
559 const char *state = NULL;
560 char *k;
561 unsigned long h;
563 u->control = pa_xnew(LADSPA_Data, (unsigned) n_control);
564 use_default = pa_xnew(pa_bool_t, (unsigned) n_control);
565 p = 0;
567 while ((k = pa_split(cdata, ",", &state)) && p < n_control) {
568 double f;
570 if (*k == 0) {
571 use_default[p++] = TRUE;
572 pa_xfree(k);
573 continue;
576 if (pa_atod(k, &f) < 0) {
577 pa_log("Failed to parse control value '%s'", k);
578 pa_xfree(k);
579 goto fail;
582 pa_xfree(k);
584 use_default[p] = FALSE;
585 u->control[p++] = (LADSPA_Data) f;
588 /* The previous loop doesn't take the last control value into account
589 if it is left empty, so we do it here. */
590 if (*cdata == 0 || cdata[strlen(cdata) - 1] == ',') {
591 if (p < n_control)
592 use_default[p] = TRUE;
593 p++;
596 if (p > n_control || k) {
597 pa_log("Too many control values passed, %lu expected.", n_control);
598 pa_xfree(k);
599 goto fail;
602 if (p < n_control) {
603 pa_log("Not enough control values passed, %lu expected, %lu passed.", n_control, p);
604 goto fail;
607 h = 0;
608 for (p = 0; p < d->PortCount; p++) {
609 LADSPA_PortRangeHintDescriptor hint = d->PortRangeHints[p].HintDescriptor;
611 if (!LADSPA_IS_PORT_CONTROL(d->PortDescriptors[p]))
612 continue;
614 if (LADSPA_IS_PORT_OUTPUT(d->PortDescriptors[p])) {
615 for (c = 0; c < ss.channels; c++)
616 d->connect_port(u->handle[c], p, &u->control_out);
617 continue;
620 pa_assert(h < n_control);
622 if (use_default[h]) {
623 LADSPA_Data lower, upper;
625 if (!LADSPA_IS_HINT_HAS_DEFAULT(hint)) {
626 pa_log("Control port value left empty but plugin defines no default.");
627 goto fail;
630 lower = d->PortRangeHints[p].LowerBound;
631 upper = d->PortRangeHints[p].UpperBound;
633 if (LADSPA_IS_HINT_SAMPLE_RATE(hint)) {
634 lower *= (LADSPA_Data) ss.rate;
635 upper *= (LADSPA_Data) ss.rate;
638 switch (hint & LADSPA_HINT_DEFAULT_MASK) {
640 case LADSPA_HINT_DEFAULT_MINIMUM:
641 u->control[h] = lower;
642 break;
644 case LADSPA_HINT_DEFAULT_MAXIMUM:
645 u->control[h] = upper;
646 break;
648 case LADSPA_HINT_DEFAULT_LOW:
649 if (LADSPA_IS_HINT_LOGARITHMIC(hint))
650 u->control[h] = (LADSPA_Data) exp(log(lower) * 0.75 + log(upper) * 0.25);
651 else
652 u->control[h] = (LADSPA_Data) (lower * 0.75 + upper * 0.25);
653 break;
655 case LADSPA_HINT_DEFAULT_MIDDLE:
656 if (LADSPA_IS_HINT_LOGARITHMIC(hint))
657 u->control[h] = (LADSPA_Data) exp(log(lower) * 0.5 + log(upper) * 0.5);
658 else
659 u->control[h] = (LADSPA_Data) (lower * 0.5 + upper * 0.5);
660 break;
662 case LADSPA_HINT_DEFAULT_HIGH:
663 if (LADSPA_IS_HINT_LOGARITHMIC(hint))
664 u->control[h] = (LADSPA_Data) exp(log(lower) * 0.25 + log(upper) * 0.75);
665 else
666 u->control[h] = (LADSPA_Data) (lower * 0.25 + upper * 0.75);
667 break;
669 case LADSPA_HINT_DEFAULT_0:
670 u->control[h] = 0;
671 break;
673 case LADSPA_HINT_DEFAULT_1:
674 u->control[h] = 1;
675 break;
677 case LADSPA_HINT_DEFAULT_100:
678 u->control[h] = 100;
679 break;
681 case LADSPA_HINT_DEFAULT_440:
682 u->control[h] = 440;
683 break;
685 default:
686 pa_assert_not_reached();
690 if (LADSPA_IS_HINT_INTEGER(hint))
691 u->control[h] = roundf(u->control[h]);
693 pa_log_debug("Binding %f to port %s", u->control[h], d->PortNames[p]);
695 for (c = 0; c < ss.channels; c++)
696 d->connect_port(u->handle[c], p, &u->control[h]);
698 h++;
701 pa_assert(h == n_control);
704 if (d->activate)
705 for (c = 0; c < u->channels; c++)
706 d->activate(u->handle[c]);
708 /* Create sink */
709 pa_sink_new_data_init(&sink_data);
710 sink_data.driver = __FILE__;
711 sink_data.module = m;
712 if (!(sink_data.name = pa_xstrdup(pa_modargs_get_value(ma, "sink_name", NULL))))
713 sink_data.name = pa_sprintf_malloc("%s.ladspa", master->name);
714 sink_data.namereg_fail = FALSE;
715 pa_sink_new_data_set_sample_spec(&sink_data, &ss);
716 pa_sink_new_data_set_channel_map(&sink_data, &map);
717 z = pa_proplist_gets(master->proplist, PA_PROP_DEVICE_DESCRIPTION);
718 pa_proplist_setf(sink_data.proplist, PA_PROP_DEVICE_DESCRIPTION, "LADSPA Plugin %s on %s", label, z ? z : master->name);
719 pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_MASTER_DEVICE, master->name);
720 pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_CLASS, "filter");
721 pa_proplist_sets(sink_data.proplist, "device.ladspa.module", plugin);
722 pa_proplist_sets(sink_data.proplist, "device.ladspa.label", d->Label);
723 pa_proplist_sets(sink_data.proplist, "device.ladspa.name", d->Name);
724 pa_proplist_sets(sink_data.proplist, "device.ladspa.maker", d->Maker);
725 pa_proplist_sets(sink_data.proplist, "device.ladspa.copyright", d->Copyright);
726 pa_proplist_setf(sink_data.proplist, "device.ladspa.unique_id", "%lu", (unsigned long) d->UniqueID);
728 if (pa_modargs_get_proplist(ma, "sink_properties", sink_data.proplist, PA_UPDATE_REPLACE) < 0) {
729 pa_log("Invalid properties");
730 pa_sink_new_data_done(&sink_data);
731 goto fail;
734 u->sink = pa_sink_new(m->core, &sink_data, master->flags & (PA_SINK_LATENCY|PA_SINK_DYNAMIC_LATENCY));
735 pa_sink_new_data_done(&sink_data);
737 if (!u->sink) {
738 pa_log("Failed to create sink.");
739 goto fail;
742 u->sink->parent.process_msg = sink_process_msg;
743 u->sink->set_state = sink_set_state;
744 u->sink->update_requested_latency = sink_update_requested_latency;
745 u->sink->request_rewind = sink_request_rewind;
746 u->sink->userdata = u;
748 pa_sink_set_asyncmsgq(u->sink, master->asyncmsgq);
750 /* Create sink input */
751 pa_sink_input_new_data_init(&sink_input_data);
752 sink_input_data.driver = __FILE__;
753 sink_input_data.module = m;
754 sink_input_data.sink = master;
755 pa_proplist_sets(sink_input_data.proplist, PA_PROP_MEDIA_NAME, "LADSPA Stream");
756 pa_proplist_sets(sink_input_data.proplist, PA_PROP_MEDIA_ROLE, "filter");
757 pa_sink_input_new_data_set_sample_spec(&sink_input_data, &ss);
758 pa_sink_input_new_data_set_channel_map(&sink_input_data, &map);
760 pa_sink_input_new(&u->sink_input, m->core, &sink_input_data, 0);
761 pa_sink_input_new_data_done(&sink_input_data);
763 if (!u->sink_input)
764 goto fail;
766 u->sink_input->pop = sink_input_pop_cb;
767 u->sink_input->process_rewind = sink_input_process_rewind_cb;
768 u->sink_input->update_max_rewind = sink_input_update_max_rewind_cb;
769 u->sink_input->update_max_request = sink_input_update_max_request_cb;
770 u->sink_input->update_sink_latency_range = sink_input_update_sink_latency_range_cb;
771 u->sink_input->update_sink_fixed_latency = sink_input_update_sink_fixed_latency_cb;
772 u->sink_input->kill = sink_input_kill_cb;
773 u->sink_input->attach = sink_input_attach_cb;
774 u->sink_input->detach = sink_input_detach_cb;
775 u->sink_input->state_change = sink_input_state_change_cb;
776 u->sink_input->may_move_to = sink_input_may_move_to_cb;
777 u->sink_input->moving = sink_input_moving_cb;
778 u->sink_input->userdata = u;
780 pa_sink_put(u->sink);
781 pa_sink_input_put(u->sink_input);
783 pa_modargs_free(ma);
785 pa_xfree(use_default);
787 return 0;
789 fail:
790 if (ma)
791 pa_modargs_free(ma);
793 pa_xfree(use_default);
795 pa__done(m);
797 return -1;
800 int pa__get_n_used(pa_module *m) {
801 struct userdata *u;
803 pa_assert(m);
804 pa_assert_se(u = m->userdata);
806 return pa_sink_linked_by(u->sink);
809 void pa__done(pa_module*m) {
810 struct userdata *u;
811 unsigned c;
813 pa_assert(m);
815 if (!(u = m->userdata))
816 return;
818 /* See comments in sink_input_kill_cb() above regarding
819 * destruction order! */
821 if (u->sink_input)
822 pa_sink_input_unlink(u->sink_input);
824 if (u->sink)
825 pa_sink_unlink(u->sink);
827 if (u->sink_input)
828 pa_sink_input_unref(u->sink_input);
830 if (u->sink)
831 pa_sink_unref(u->sink);
833 for (c = 0; c < u->channels; c++)
834 if (u->handle[c]) {
835 if (u->descriptor->deactivate)
836 u->descriptor->deactivate(u->handle[c]);
837 u->descriptor->cleanup(u->handle[c]);
840 if (u->output != u->input)
841 pa_xfree(u->output);
843 if (u->memblockq)
844 pa_memblockq_free(u->memblockq);
846 pa_xfree(u->input);
848 pa_xfree(u->control);
850 pa_xfree(u);