"Fossies" - the Fresh Open Source Software Archive

Member "pulseaudio-14.2/src/modules/module-ladspa-sink.c" (11 Jan 2021, 48482 Bytes) of package /linux/misc/pulseaudio-14.2.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "module-ladspa-sink.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 14.0_vs_14.2.

    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, see <http://www.gnu.org/licenses/>.
   18 ***/
   19 
   20 /* TODO: Some plugins cause latency, and some even report it by using a control
   21    out port. We don't currently use the latency information. */
   22 
   23 #ifdef HAVE_CONFIG_H
   24 #include <config.h>
   25 #endif
   26 
   27 #include <math.h>
   28 
   29 #include <pulse/xmalloc.h>
   30 
   31 #include <pulsecore/i18n.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/rtpoll.h>
   39 #include <pulsecore/sample-util.h>
   40 #include <pulsecore/ltdl-helper.h>
   41 
   42 #ifdef HAVE_DBUS
   43 #include <pulsecore/protocol-dbus.h>
   44 #include <pulsecore/dbus-util.h>
   45 #endif
   46 
   47 #include "ladspa.h"
   48 
   49 PA_MODULE_AUTHOR("Lennart Poettering");
   50 PA_MODULE_DESCRIPTION(_("Virtual LADSPA sink"));
   51 PA_MODULE_VERSION(PACKAGE_VERSION);
   52 PA_MODULE_LOAD_ONCE(false);
   53 PA_MODULE_USAGE(
   54     _("sink_name=<name for the sink> "
   55       "sink_properties=<properties for the sink> "
   56       "sink_input_properties=<properties for the sink input> "
   57       "master=<name of sink to filter> "
   58       "sink_master=<name of sink to filter> "
   59       "format=<sample format> "
   60       "rate=<sample rate> "
   61       "channels=<number of channels> "
   62       "channel_map=<input channel map> "
   63       "plugin=<ladspa plugin name> "
   64       "label=<ladspa plugin label> "
   65       "control=<comma separated list of input control values> "
   66       "input_ladspaport_map=<comma separated list of input LADSPA port names> "
   67       "output_ladspaport_map=<comma separated list of output LADSPA port names> "
   68       "autoloaded=<set if this module is being loaded automatically> "));
   69 
   70 #define MEMBLOCKQ_MAXLENGTH (16*1024*1024)
   71 #define DEFAULT_AUTOLOADED false
   72 
   73 /* PLEASE NOTICE: The PortAudio ports and the LADSPA ports are two different concepts.
   74 They are not related and where possible the names of the LADSPA port variables contains "ladspa" to avoid confusion */
   75 
   76 struct userdata {
   77     pa_module *module;
   78 
   79     pa_sink *sink;
   80     pa_sink_input *sink_input;
   81 
   82     const LADSPA_Descriptor *descriptor;
   83     LADSPA_Handle handle[PA_CHANNELS_MAX];
   84     unsigned long max_ladspaport_count, input_count, output_count, channels;
   85     LADSPA_Data **input, **output;
   86     size_t block_size;
   87     LADSPA_Data *control;
   88     long unsigned n_control;
   89 
   90     /* This is a dummy buffer. Every port must be connected, but we don't care
   91     about control out ports. We connect them all to this single buffer. */
   92     LADSPA_Data control_out;
   93 
   94     pa_memblockq *memblockq;
   95 
   96     bool *use_default;
   97     pa_sample_spec ss;
   98 
   99 #ifdef HAVE_DBUS
  100     pa_dbus_protocol *dbus_protocol;
  101     char *dbus_path;
  102 #endif
  103 
  104     bool auto_desc;
  105     bool autoloaded;
  106 };
  107 
  108 static const char* const valid_modargs[] = {
  109     "sink_name",
  110     "sink_properties",
  111     "sink_input_properties",
  112     "master",  /* Will be deprecated. */
  113     "sink_master",
  114     "format",
  115     "rate",
  116     "channels",
  117     "channel_map",
  118     "plugin",
  119     "label",
  120     "control",
  121     "input_ladspaport_map",
  122     "output_ladspaport_map",
  123     "autoloaded",
  124     NULL
  125 };
  126 
  127 /* The PA_SINK_MESSAGE types that extend the predefined messages. */
  128 enum {
  129    LADSPA_SINK_MESSAGE_UPDATE_PARAMETERS = PA_SINK_MESSAGE_MAX
  130 };
  131 
  132 static int write_control_parameters(struct userdata *u, double *control_values, bool *use_default);
  133 static void connect_control_ports(struct userdata *u);
  134 
  135 #ifdef HAVE_DBUS
  136 
  137 #define LADSPA_IFACE "org.PulseAudio.Ext.Ladspa1"
  138 #define LADSPA_ALGORITHM_PARAMETERS "AlgorithmParameters"
  139 
  140 /* TODO: add a PropertyChanged signal to tell that the algorithm parameters have been changed */
  141 
  142 enum ladspa_handler_index {
  143     LADSPA_HANDLER_ALGORITHM_PARAMETERS,
  144     LADSPA_HANDLER_MAX
  145 };
  146 
  147 static void get_algorithm_parameters(DBusConnection *conn, DBusMessage *msg, void *_u) {
  148     struct userdata *u;
  149     DBusMessage *reply = NULL;
  150     DBusMessageIter msg_iter, struct_iter;
  151     unsigned long i;
  152     double *control;
  153     dbus_bool_t *use_default;
  154 
  155     pa_assert(conn);
  156     pa_assert(msg);
  157     pa_assert_se(u = _u);
  158 
  159     pa_assert_se((reply = dbus_message_new_method_return(msg)));
  160     dbus_message_iter_init_append(reply, &msg_iter);
  161 
  162     dbus_message_iter_open_container(&msg_iter, DBUS_TYPE_STRUCT, NULL, &struct_iter);
  163 
  164     /* copying because of the D-Bus type mapping */
  165     control = pa_xnew(double, u->n_control);
  166     use_default = pa_xnew(dbus_bool_t, u->n_control);
  167 
  168     for (i = 0; i < u->n_control; i++) {
  169         control[i] = (double) u->control[i];
  170         use_default[i] = u->use_default[i];
  171     }
  172 
  173     pa_dbus_append_basic_array(&struct_iter, DBUS_TYPE_DOUBLE, control, u->n_control);
  174     pa_dbus_append_basic_array(&struct_iter, DBUS_TYPE_BOOLEAN, use_default, u->n_control);
  175 
  176     pa_assert_se(dbus_message_iter_close_container(&msg_iter, &struct_iter));
  177 
  178     pa_assert_se(dbus_connection_send(conn, reply, NULL));
  179 
  180     dbus_message_unref(reply);
  181     pa_xfree(control);
  182     pa_xfree(use_default);
  183 }
  184 
  185 static void set_algorithm_parameters(DBusConnection *conn, DBusMessage *msg, DBusMessageIter *iter, void *_u) {
  186     struct userdata *u;
  187     DBusMessageIter array_iter, struct_iter;
  188     int n_control = 0, n_use_default;
  189     unsigned n_dbus_control, n_dbus_use_default;
  190     double *read_values = NULL;
  191     dbus_bool_t *read_defaults = NULL;
  192     bool *use_defaults = NULL;
  193     unsigned long i;
  194 
  195     pa_assert(conn);
  196     pa_assert(msg);
  197     pa_assert_se(u = _u);
  198 
  199     /* The property we are expecting has signature (adab), meaning that it's a
  200        struct of two arrays, the first containing doubles and the second containing
  201        booleans. The first array has the algorithm configuration values and the
  202        second array has booleans indicating whether the matching algorithm
  203        configuration value should use (or try to use) the default value provided by
  204        the algorithm module. The PulseAudio D-Bus infrastructure will take care of
  205        checking the argument types for us. */
  206 
  207     dbus_message_iter_recurse(iter, &struct_iter);
  208 
  209     dbus_message_iter_recurse(&struct_iter, &array_iter);
  210     dbus_message_iter_get_fixed_array(&array_iter, &read_values, &n_control);
  211 
  212     dbus_message_iter_next(&struct_iter);
  213     dbus_message_iter_recurse(&struct_iter, &array_iter);
  214     dbus_message_iter_get_fixed_array(&array_iter, &read_defaults, &n_use_default);
  215 
  216     n_dbus_control = n_control; /* handle the unsignedness */
  217     n_dbus_use_default = n_use_default;
  218 
  219     if (n_dbus_control != u->n_control || n_dbus_use_default != u->n_control) {
  220         pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "Wrong number of array values (expected %lu)", u->n_control);
  221         return;
  222     }
  223 
  224     use_defaults = pa_xnew(bool, n_control);
  225     for (i = 0; i < u->n_control; i++)
  226         use_defaults[i] = read_defaults[i];
  227 
  228     if (write_control_parameters(u, read_values, use_defaults) < 0) {
  229         pa_log_warn("Failed writing control parameters");
  230         goto error;
  231     }
  232 
  233     pa_asyncmsgq_send(u->sink->asyncmsgq, PA_MSGOBJECT(u->sink), LADSPA_SINK_MESSAGE_UPDATE_PARAMETERS, NULL, 0, NULL);
  234 
  235     pa_dbus_send_empty_reply(conn, msg);
  236 
  237     pa_xfree(use_defaults);
  238     return;
  239 
  240 error:
  241     pa_xfree(use_defaults);
  242     pa_dbus_send_error(conn, msg, DBUS_ERROR_FAILED, "Internal error");
  243 }
  244 
  245 static pa_dbus_property_handler ladspa_property_handlers[LADSPA_HANDLER_MAX] = {
  246     [LADSPA_HANDLER_ALGORITHM_PARAMETERS] = {
  247         .property_name = LADSPA_ALGORITHM_PARAMETERS,
  248         .type = "(adab)",
  249         .get_cb = get_algorithm_parameters,
  250         .set_cb = set_algorithm_parameters
  251     }
  252 };
  253 
  254 static void ladspa_get_all(DBusConnection *conn, DBusMessage *msg, void *_u) {
  255     struct userdata *u;
  256     DBusMessage *reply = NULL;
  257     DBusMessageIter msg_iter, dict_iter, dict_entry_iter, variant_iter, struct_iter;
  258     const char *key = LADSPA_ALGORITHM_PARAMETERS;
  259     double *control;
  260     dbus_bool_t *use_default;
  261     long unsigned i;
  262 
  263     pa_assert(conn);
  264     pa_assert(msg);
  265     pa_assert_se(u = _u);
  266 
  267     pa_assert_se((reply = dbus_message_new_method_return(msg)));
  268 
  269     /* Currently, on this interface, only a single property is returned. */
  270 
  271     dbus_message_iter_init_append(reply, &msg_iter);
  272     pa_assert_se(dbus_message_iter_open_container(&msg_iter, DBUS_TYPE_ARRAY, "{sv}", &dict_iter));
  273     pa_assert_se(dbus_message_iter_open_container(&dict_iter, DBUS_TYPE_DICT_ENTRY, NULL, &dict_entry_iter));
  274     pa_assert_se(dbus_message_iter_append_basic(&dict_entry_iter, DBUS_TYPE_STRING, &key));
  275 
  276     pa_assert_se(dbus_message_iter_open_container(&dict_entry_iter, DBUS_TYPE_VARIANT, "(adab)", &variant_iter));
  277     pa_assert_se(dbus_message_iter_open_container(&variant_iter, DBUS_TYPE_STRUCT, NULL, &struct_iter));
  278 
  279     control = pa_xnew(double, u->n_control);
  280     use_default = pa_xnew(dbus_bool_t, u->n_control);
  281 
  282     for (i = 0; i < u->n_control; i++) {
  283         control[i] = (double) u->control[i];
  284         use_default[i] = u->use_default[i];
  285     }
  286 
  287     pa_dbus_append_basic_array(&struct_iter, DBUS_TYPE_DOUBLE, control, u->n_control);
  288     pa_dbus_append_basic_array(&struct_iter, DBUS_TYPE_BOOLEAN, use_default, u->n_control);
  289 
  290     pa_assert_se(dbus_message_iter_close_container(&variant_iter, &struct_iter));
  291     pa_assert_se(dbus_message_iter_close_container(&dict_entry_iter, &variant_iter));
  292     pa_assert_se(dbus_message_iter_close_container(&dict_iter, &dict_entry_iter));
  293     pa_assert_se(dbus_message_iter_close_container(&msg_iter, &dict_iter));
  294 
  295     pa_assert_se(dbus_connection_send(conn, reply, NULL));
  296     dbus_message_unref(reply);
  297     pa_xfree(control);
  298     pa_xfree(use_default);
  299 }
  300 
  301 static pa_dbus_interface_info ladspa_info = {
  302     .name = LADSPA_IFACE,
  303     .method_handlers = NULL,
  304     .n_method_handlers = 0,
  305     .property_handlers = ladspa_property_handlers,
  306     .n_property_handlers = LADSPA_HANDLER_MAX,
  307     .get_all_properties_cb = ladspa_get_all,
  308     .signals = NULL,
  309     .n_signals = 0
  310 };
  311 
  312 static void dbus_init(struct userdata *u) {
  313     pa_assert_se(u);
  314 
  315     u->dbus_protocol = pa_dbus_protocol_get(u->sink->core);
  316     u->dbus_path = pa_sprintf_malloc("/org/pulseaudio/core1/sink%d", u->sink->index);
  317 
  318     pa_dbus_protocol_add_interface(u->dbus_protocol, u->dbus_path, &ladspa_info, u);
  319 }
  320 
  321 static void dbus_done(struct userdata *u) {
  322     pa_assert_se(u);
  323 
  324     if (!u->dbus_protocol) {
  325         pa_assert(!u->dbus_path);
  326         return;
  327     }
  328 
  329     pa_dbus_protocol_remove_interface(u->dbus_protocol, u->dbus_path, ladspa_info.name);
  330     pa_xfree(u->dbus_path);
  331     pa_dbus_protocol_unref(u->dbus_protocol);
  332 
  333     u->dbus_path = NULL;
  334     u->dbus_protocol = NULL;
  335 }
  336 
  337 #endif /* HAVE_DBUS */
  338 
  339 /* Called from I/O thread context */
  340 static int sink_process_msg_cb(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
  341     struct userdata *u = PA_SINK(o)->userdata;
  342 
  343     switch (code) {
  344 
  345     case PA_SINK_MESSAGE_GET_LATENCY:
  346 
  347         /* The sink is _put() before the sink input is, so let's
  348          * make sure we don't access it in that time. Also, the
  349          * sink input is first shut down, the sink second. */
  350         if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
  351             !PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state)) {
  352             *((int64_t*) data) = 0;
  353             return 0;
  354         }
  355 
  356         *((int64_t*) data) =
  357 
  358             /* Get the latency of the master sink */
  359             pa_sink_get_latency_within_thread(u->sink_input->sink, true) +
  360 
  361             /* Add the latency internal to our sink input on top */
  362             pa_bytes_to_usec(pa_memblockq_get_length(u->sink_input->thread_info.render_memblockq), &u->sink_input->sink->sample_spec);
  363 
  364         return 0;
  365 
  366     case LADSPA_SINK_MESSAGE_UPDATE_PARAMETERS:
  367 
  368         /* rewind the stream to throw away the previously rendered data */
  369 
  370         pa_log_debug("Requesting rewind due to parameter update.");
  371         pa_sink_request_rewind(u->sink, -1);
  372 
  373         /* change the sink parameters */
  374         connect_control_ports(u);
  375 
  376         return 0;
  377     }
  378 
  379     return pa_sink_process_msg(o, code, data, offset, chunk);
  380 }
  381 
  382 /* Called from main context */
  383 static int sink_set_state_in_main_thread_cb(pa_sink *s, pa_sink_state_t state, pa_suspend_cause_t suspend_cause) {
  384     struct userdata *u;
  385 
  386     pa_sink_assert_ref(s);
  387     pa_assert_se(u = s->userdata);
  388 
  389     if (!PA_SINK_IS_LINKED(state) ||
  390             !PA_SINK_INPUT_IS_LINKED(u->sink_input->state))
  391         return 0;
  392 
  393     pa_sink_input_cork(u->sink_input, state == PA_SINK_SUSPENDED);
  394     return 0;
  395 }
  396 
  397 /* Called from the IO thread. */
  398 static int sink_set_state_in_io_thread_cb(pa_sink *s, pa_sink_state_t new_state, pa_suspend_cause_t new_suspend_cause) {
  399     struct userdata *u;
  400 
  401     pa_assert(s);
  402     pa_assert_se(u = s->userdata);
  403 
  404     /* When set to running or idle for the first time, request a rewind
  405      * of the master sink to make sure we are heard immediately */
  406     if (PA_SINK_IS_OPENED(new_state) && s->thread_info.state == PA_SINK_INIT) {
  407         pa_log_debug("Requesting rewind due to state change.");
  408         pa_sink_input_request_rewind(u->sink_input, 0, false, true, true);
  409     }
  410 
  411     return 0;
  412 }
  413 
  414 /* Called from I/O thread context */
  415 static void sink_request_rewind_cb(pa_sink *s) {
  416     struct userdata *u;
  417 
  418     pa_sink_assert_ref(s);
  419     pa_assert_se(u = s->userdata);
  420 
  421     if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
  422             !PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state))
  423         return;
  424 
  425     /* Just hand this one over to the master sink */
  426     pa_sink_input_request_rewind(u->sink_input,
  427                                  s->thread_info.rewind_nbytes +
  428                                  pa_memblockq_get_length(u->memblockq), true, false, false);
  429 }
  430 
  431 /* Called from I/O thread context */
  432 static void sink_update_requested_latency_cb(pa_sink *s) {
  433     struct userdata *u;
  434 
  435     pa_sink_assert_ref(s);
  436     pa_assert_se(u = s->userdata);
  437 
  438     if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
  439             !PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state))
  440         return;
  441 
  442     /* Just hand this one over to the master sink */
  443     pa_sink_input_set_requested_latency_within_thread(
  444         u->sink_input,
  445         pa_sink_get_requested_latency_within_thread(s));
  446 }
  447 
  448 /* Called from main context */
  449 static void sink_set_mute_cb(pa_sink *s) {
  450     struct userdata *u;
  451 
  452     pa_sink_assert_ref(s);
  453     pa_assert_se(u = s->userdata);
  454 
  455     if (!PA_SINK_IS_LINKED(s->state) ||
  456             !PA_SINK_INPUT_IS_LINKED(u->sink_input->state))
  457         return;
  458 
  459     pa_sink_input_set_mute(u->sink_input, s->muted, s->save_muted);
  460 }
  461 
  462 /* Called from I/O thread context */
  463 static int sink_input_pop_cb(pa_sink_input *i, size_t nbytes, pa_memchunk *chunk) {
  464     struct userdata *u;
  465     float *src, *dst;
  466     size_t fs;
  467     unsigned n, h, c;
  468     pa_memchunk tchunk;
  469 
  470     pa_sink_input_assert_ref(i);
  471     pa_assert(chunk);
  472     pa_assert_se(u = i->userdata);
  473 
  474     if (!PA_SINK_IS_LINKED(u->sink->thread_info.state))
  475         return -1;
  476 
  477     /* Hmm, process any rewind request that might be queued up */
  478     pa_sink_process_rewind(u->sink, 0);
  479 
  480     while (pa_memblockq_peek(u->memblockq, &tchunk) < 0) {
  481         pa_memchunk nchunk;
  482 
  483         pa_sink_render(u->sink, nbytes, &nchunk);
  484         pa_memblockq_push(u->memblockq, &nchunk);
  485         pa_memblock_unref(nchunk.memblock);
  486     }
  487 
  488     tchunk.length = PA_MIN(nbytes, tchunk.length);
  489     pa_assert(tchunk.length > 0);
  490 
  491     fs = pa_frame_size(&i->sample_spec);
  492     n = (unsigned) (PA_MIN(tchunk.length, u->block_size) / fs);
  493 
  494     pa_assert(n > 0);
  495 
  496     chunk->index = 0;
  497     chunk->length = n*fs;
  498     chunk->memblock = pa_memblock_new(i->sink->core->mempool, chunk->length);
  499 
  500     pa_memblockq_drop(u->memblockq, chunk->length);
  501 
  502     src = pa_memblock_acquire_chunk(&tchunk);
  503     dst = pa_memblock_acquire(chunk->memblock);
  504 
  505     for (h = 0; h < (u->channels / u->max_ladspaport_count); h++) {
  506         for (c = 0; c < u->input_count; c++)
  507             pa_sample_clamp(PA_SAMPLE_FLOAT32NE, u->input[c], sizeof(float), src+ h*u->max_ladspaport_count + c, u->channels*sizeof(float), n);
  508         u->descriptor->run(u->handle[h], n);
  509         for (c = 0; c < u->output_count; c++)
  510             pa_sample_clamp(PA_SAMPLE_FLOAT32NE, dst + h*u->max_ladspaport_count + c, u->channels*sizeof(float), u->output[c], sizeof(float), n);
  511     }
  512 
  513     pa_memblock_release(tchunk.memblock);
  514     pa_memblock_release(chunk->memblock);
  515 
  516     pa_memblock_unref(tchunk.memblock);
  517 
  518     return 0;
  519 }
  520 
  521 /* Called from I/O thread context */
  522 static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes) {
  523     struct userdata *u;
  524     size_t amount = 0;
  525 
  526     pa_sink_input_assert_ref(i);
  527     pa_assert_se(u = i->userdata);
  528 
  529     /* If the sink is not yet linked, there is nothing to rewind */
  530     if (!PA_SINK_IS_LINKED(u->sink->thread_info.state))
  531         return;
  532 
  533     if (u->sink->thread_info.rewind_nbytes > 0) {
  534         size_t max_rewrite;
  535 
  536         max_rewrite = nbytes + pa_memblockq_get_length(u->memblockq);
  537         amount = PA_MIN(u->sink->thread_info.rewind_nbytes, max_rewrite);
  538         u->sink->thread_info.rewind_nbytes = 0;
  539 
  540         if (amount > 0) {
  541             unsigned c;
  542 
  543             pa_memblockq_seek(u->memblockq, - (int64_t) amount, PA_SEEK_RELATIVE, true);
  544 
  545             pa_log_debug("Resetting plugin");
  546 
  547             /* Reset the plugin */
  548             if (u->descriptor->deactivate)
  549                 for (c = 0; c < (u->channels / u->max_ladspaport_count); c++)
  550                     u->descriptor->deactivate(u->handle[c]);
  551             if (u->descriptor->activate)
  552                 for (c = 0; c < (u->channels / u->max_ladspaport_count); c++)
  553                     u->descriptor->activate(u->handle[c]);
  554         }
  555     }
  556 
  557     pa_sink_process_rewind(u->sink, amount);
  558     pa_memblockq_rewind(u->memblockq, nbytes);
  559 }
  560 
  561 /* Called from I/O thread context */
  562 static void sink_input_update_max_rewind_cb(pa_sink_input *i, size_t nbytes) {
  563     struct userdata *u;
  564 
  565     pa_sink_input_assert_ref(i);
  566     pa_assert_se(u = i->userdata);
  567 
  568     /* FIXME: Too small max_rewind:
  569      * https://bugs.freedesktop.org/show_bug.cgi?id=53709 */
  570     pa_memblockq_set_maxrewind(u->memblockq, nbytes);
  571     pa_sink_set_max_rewind_within_thread(u->sink, nbytes);
  572 }
  573 
  574 /* Called from I/O thread context */
  575 static void sink_input_update_max_request_cb(pa_sink_input *i, size_t nbytes) {
  576     struct userdata *u;
  577 
  578     pa_sink_input_assert_ref(i);
  579     pa_assert_se(u = i->userdata);
  580 
  581     pa_sink_set_max_request_within_thread(u->sink, nbytes);
  582 }
  583 
  584 /* Called from I/O thread context */
  585 static void sink_input_update_sink_latency_range_cb(pa_sink_input *i) {
  586     struct userdata *u;
  587 
  588     pa_sink_input_assert_ref(i);
  589     pa_assert_se(u = i->userdata);
  590 
  591     pa_sink_set_latency_range_within_thread(u->sink, i->sink->thread_info.min_latency, i->sink->thread_info.max_latency);
  592 }
  593 
  594 /* Called from I/O thread context */
  595 static void sink_input_update_sink_fixed_latency_cb(pa_sink_input *i) {
  596     struct userdata *u;
  597 
  598     pa_sink_input_assert_ref(i);
  599     pa_assert_se(u = i->userdata);
  600 
  601     pa_sink_set_fixed_latency_within_thread(u->sink, i->sink->thread_info.fixed_latency);
  602 }
  603 
  604 /* Called from I/O thread context */
  605 static void sink_input_detach_cb(pa_sink_input *i) {
  606     struct userdata *u;
  607 
  608     pa_sink_input_assert_ref(i);
  609     pa_assert_se(u = i->userdata);
  610 
  611     if (PA_SINK_IS_LINKED(u->sink->thread_info.state))
  612         pa_sink_detach_within_thread(u->sink);
  613 
  614     pa_sink_set_rtpoll(u->sink, NULL);
  615 }
  616 
  617 /* Called from I/O thread context */
  618 static void sink_input_attach_cb(pa_sink_input *i) {
  619     struct userdata *u;
  620 
  621     pa_sink_input_assert_ref(i);
  622     pa_assert_se(u = i->userdata);
  623 
  624     pa_sink_set_rtpoll(u->sink, i->sink->thread_info.rtpoll);
  625     pa_sink_set_latency_range_within_thread(u->sink, i->sink->thread_info.min_latency, i->sink->thread_info.max_latency);
  626     pa_sink_set_fixed_latency_within_thread(u->sink, i->sink->thread_info.fixed_latency);
  627     pa_sink_set_max_request_within_thread(u->sink, pa_sink_input_get_max_request(i));
  628 
  629     /* FIXME: Too small max_rewind:
  630      * https://bugs.freedesktop.org/show_bug.cgi?id=53709 */
  631     pa_sink_set_max_rewind_within_thread(u->sink, pa_sink_input_get_max_rewind(i));
  632 
  633     if (PA_SINK_IS_LINKED(u->sink->thread_info.state))
  634         pa_sink_attach_within_thread(u->sink);
  635 }
  636 
  637 /* Called from main context */
  638 static void sink_input_kill_cb(pa_sink_input *i) {
  639     struct userdata *u;
  640 
  641     pa_sink_input_assert_ref(i);
  642     pa_assert_se(u = i->userdata);
  643 
  644     /* The order here matters! We first kill the sink so that streams
  645      * can properly be moved away while the sink input is still connected
  646      * to the master. */
  647     pa_sink_input_cork(u->sink_input, true);
  648     pa_sink_unlink(u->sink);
  649     pa_sink_input_unlink(u->sink_input);
  650 
  651     pa_sink_input_unref(u->sink_input);
  652     u->sink_input = NULL;
  653 
  654     pa_sink_unref(u->sink);
  655     u->sink = NULL;
  656 
  657     pa_module_unload_request(u->module, true);
  658 }
  659 
  660 /* Called from main context */
  661 static bool sink_input_may_move_to_cb(pa_sink_input *i, pa_sink *dest) {
  662     struct userdata *u;
  663 
  664     pa_sink_input_assert_ref(i);
  665     pa_assert_se(u = i->userdata);
  666 
  667     if (u->autoloaded)
  668         return false;
  669 
  670     return u->sink != dest;
  671 }
  672 
  673 /* Called from main context */
  674 static void sink_input_moving_cb(pa_sink_input *i, pa_sink *dest) {
  675     struct userdata *u;
  676 
  677     pa_sink_input_assert_ref(i);
  678     pa_assert_se(u = i->userdata);
  679 
  680     if (dest) {
  681         pa_sink_set_asyncmsgq(u->sink, dest->asyncmsgq);
  682         pa_sink_update_flags(u->sink, PA_SINK_LATENCY|PA_SINK_DYNAMIC_LATENCY, dest->flags);
  683     } else
  684         pa_sink_set_asyncmsgq(u->sink, NULL);
  685 
  686     if (u->auto_desc && dest) {
  687         const char *z;
  688         pa_proplist *pl;
  689 
  690         pl = pa_proplist_new();
  691         z = pa_proplist_gets(dest->proplist, PA_PROP_DEVICE_DESCRIPTION);
  692         pa_proplist_setf(pl, PA_PROP_DEVICE_DESCRIPTION, "LADSPA Plugin %s on %s",
  693                          pa_proplist_gets(u->sink->proplist, "device.ladspa.name"), z ? z : dest->name);
  694 
  695         pa_sink_update_proplist(u->sink, PA_UPDATE_REPLACE, pl);
  696         pa_proplist_free(pl);
  697     }
  698 }
  699 
  700 /* Called from main context */
  701 static void sink_input_mute_changed_cb(pa_sink_input *i) {
  702     struct userdata *u;
  703 
  704     pa_sink_input_assert_ref(i);
  705     pa_assert_se(u = i->userdata);
  706 
  707     pa_sink_mute_changed(u->sink, i->muted);
  708 }
  709 
  710 /* Called from main context */
  711 static void sink_input_suspend_cb(pa_sink_input *i, pa_sink_state_t old_state, pa_suspend_cause_t old_suspend_cause) {
  712     struct userdata *u;
  713 
  714     pa_sink_input_assert_ref(i);
  715     pa_assert_se(u = i->userdata);
  716 
  717     if (!PA_SINK_IS_LINKED(u->sink->state))
  718         return;
  719 
  720     if (i->sink->state != PA_SINK_SUSPENDED || i->sink->suspend_cause == PA_SUSPEND_IDLE)
  721         pa_sink_suspend(u->sink, false, PA_SUSPEND_UNAVAILABLE);
  722     else
  723         pa_sink_suspend(u->sink, true, PA_SUSPEND_UNAVAILABLE);
  724 }
  725 
  726 static int parse_control_parameters(struct userdata *u, const char *cdata, double *read_values, bool *use_default) {
  727     unsigned long p = 0;
  728     const char *state = NULL;
  729     char *k;
  730 
  731     pa_assert(read_values);
  732     pa_assert(use_default);
  733     pa_assert(u);
  734 
  735     pa_log_debug("Trying to read %lu control values", u->n_control);
  736 
  737     if (!cdata || u->n_control == 0)
  738         return -1;
  739 
  740     pa_log_debug("cdata: '%s'", cdata);
  741 
  742     while ((k = pa_split(cdata, ",", &state)) && p < u->n_control) {
  743         double f;
  744 
  745         if (*k == 0) {
  746             pa_log_debug("Read empty config value (p=%lu)", p);
  747             use_default[p++] = true;
  748             pa_xfree(k);
  749             continue;
  750         }
  751 
  752         if (pa_atod(k, &f) < 0) {
  753             pa_log_debug("Failed to parse control value '%s' (p=%lu)", k, p);
  754             pa_xfree(k);
  755             goto fail;
  756         }
  757 
  758         pa_xfree(k);
  759 
  760         pa_log_debug("Read config value %f (p=%lu)", f, p);
  761 
  762         use_default[p] = false;
  763         read_values[p++] = f;
  764     }
  765 
  766     /* The previous loop doesn't take the last control value into account
  767        if it is left empty, so we do it here. */
  768     if (*cdata == 0 || cdata[strlen(cdata) - 1] == ',') {
  769         if (p < u->n_control)
  770             use_default[p] = true;
  771         p++;
  772     }
  773 
  774     if (p > u->n_control || k) {
  775         pa_log("Too many control values passed, %lu expected.", u->n_control);
  776         pa_xfree(k);
  777         goto fail;
  778     }
  779 
  780     if (p < u->n_control) {
  781         pa_log("Not enough control values passed, %lu expected, %lu passed.", u->n_control, p);
  782         goto fail;
  783     }
  784 
  785     return 0;
  786 
  787 fail:
  788     return -1;
  789 }
  790 
  791 static void connect_control_ports(struct userdata *u) {
  792     unsigned long p = 0, h = 0, c;
  793     const LADSPA_Descriptor *d;
  794 
  795     pa_assert(u);
  796     pa_assert_se(d = u->descriptor);
  797 
  798     for (p = 0; p < d->PortCount; p++) {
  799         if (!LADSPA_IS_PORT_CONTROL(d->PortDescriptors[p]))
  800             continue;
  801 
  802         if (LADSPA_IS_PORT_OUTPUT(d->PortDescriptors[p])) {
  803             for (c = 0; c < (u->channels / u->max_ladspaport_count); c++)
  804                 d->connect_port(u->handle[c], p, &u->control_out);
  805             continue;
  806         }
  807 
  808         /* input control port */
  809 
  810         pa_log_debug("Binding %f to port %s", u->control[h], d->PortNames[p]);
  811 
  812         for (c = 0; c < (u->channels / u->max_ladspaport_count); c++)
  813             d->connect_port(u->handle[c], p, &u->control[h]);
  814 
  815         h++;
  816     }
  817 }
  818 
  819 static int validate_control_parameters(struct userdata *u, double *control_values, bool *use_default) {
  820     unsigned long p = 0, h = 0;
  821     const LADSPA_Descriptor *d;
  822     pa_sample_spec ss;
  823 
  824     pa_assert(control_values);
  825     pa_assert(use_default);
  826     pa_assert(u);
  827     pa_assert_se(d = u->descriptor);
  828 
  829     ss = u->ss;
  830 
  831     /* Iterate over all ports. Check for every control port that 1) it
  832      * supports default values if a default value is provided and 2) the
  833      * provided value is within the limits specified in the plugin. */
  834 
  835     for (p = 0; p < d->PortCount; p++) {
  836         LADSPA_PortRangeHintDescriptor hint = d->PortRangeHints[p].HintDescriptor;
  837 
  838         if (!LADSPA_IS_PORT_CONTROL(d->PortDescriptors[p]))
  839             continue;
  840 
  841         if (LADSPA_IS_PORT_OUTPUT(d->PortDescriptors[p]))
  842             continue;
  843 
  844         if (use_default[h]) {
  845             /* User wants to use default value. Check if the plugin
  846              * provides it. */
  847             if (!LADSPA_IS_HINT_HAS_DEFAULT(hint)) {
  848                 pa_log_warn("Control port value left empty but plugin defines no default.");
  849                 return -1;
  850             }
  851         }
  852         else {
  853             /* Check if the user-provided value is within the bounds. */
  854             LADSPA_Data lower = d->PortRangeHints[p].LowerBound;
  855             LADSPA_Data upper = d->PortRangeHints[p].UpperBound;
  856 
  857             if (LADSPA_IS_HINT_SAMPLE_RATE(hint)) {
  858                 upper *= (LADSPA_Data) ss.rate;
  859                 lower *= (LADSPA_Data) ss.rate;
  860             }
  861 
  862             if (LADSPA_IS_HINT_BOUNDED_ABOVE(hint)) {
  863                 if (control_values[h] > upper) {
  864                     pa_log_warn("Control value %lu over upper bound: %f (upper bound: %f)", h, control_values[h], upper);
  865                     return -1;
  866                 }
  867             }
  868             if (LADSPA_IS_HINT_BOUNDED_BELOW(hint)) {
  869                 if (control_values[h] < lower) {
  870                     pa_log_warn("Control value %lu below lower bound: %f (lower bound: %f)", h, control_values[h], lower);
  871                     return -1;
  872                 }
  873             }
  874         }
  875 
  876         h++;
  877     }
  878 
  879     return 0;
  880 }
  881 
  882 static int write_control_parameters(struct userdata *u, double *control_values, bool *use_default) {
  883     unsigned long p = 0, h = 0, c;
  884     const LADSPA_Descriptor *d;
  885     pa_sample_spec ss;
  886 
  887     pa_assert(control_values);
  888     pa_assert(use_default);
  889     pa_assert(u);
  890     pa_assert_se(d = u->descriptor);
  891 
  892     ss = u->ss;
  893 
  894     if (validate_control_parameters(u, control_values, use_default) < 0)
  895         return -1;
  896 
  897     /* p iterates over all ports, h is the control port iterator */
  898 
  899     for (p = 0; p < d->PortCount; p++) {
  900         LADSPA_PortRangeHintDescriptor hint = d->PortRangeHints[p].HintDescriptor;
  901 
  902         if (!LADSPA_IS_PORT_CONTROL(d->PortDescriptors[p]))
  903             continue;
  904 
  905         if (LADSPA_IS_PORT_OUTPUT(d->PortDescriptors[p])) {
  906             for (c = 0; c < (u->channels / u->max_ladspaport_count); c++)
  907                 d->connect_port(u->handle[c], p, &u->control_out);
  908             continue;
  909         }
  910 
  911         if (use_default[h]) {
  912 
  913             LADSPA_Data lower, upper;
  914 
  915             lower = d->PortRangeHints[p].LowerBound;
  916             upper = d->PortRangeHints[p].UpperBound;
  917 
  918             if (LADSPA_IS_HINT_SAMPLE_RATE(hint)) {
  919                 lower *= (LADSPA_Data) ss.rate;
  920                 upper *= (LADSPA_Data) ss.rate;
  921             }
  922 
  923             switch (hint & LADSPA_HINT_DEFAULT_MASK) {
  924 
  925             case LADSPA_HINT_DEFAULT_MINIMUM:
  926                 u->control[h] = lower;
  927                 break;
  928 
  929             case LADSPA_HINT_DEFAULT_MAXIMUM:
  930                 u->control[h] = upper;
  931                 break;
  932 
  933             case LADSPA_HINT_DEFAULT_LOW:
  934                 if (LADSPA_IS_HINT_LOGARITHMIC(hint))
  935                     u->control[h] = (LADSPA_Data) exp(log(lower) * 0.75 + log(upper) * 0.25);
  936                 else
  937                     u->control[h] = (LADSPA_Data) (lower * 0.75 + upper * 0.25);
  938                 break;
  939 
  940             case LADSPA_HINT_DEFAULT_MIDDLE:
  941                 if (LADSPA_IS_HINT_LOGARITHMIC(hint))
  942                     u->control[h] = (LADSPA_Data) exp(log(lower) * 0.5 + log(upper) * 0.5);
  943                 else
  944                     u->control[h] = (LADSPA_Data) (lower * 0.5 + upper * 0.5);
  945                 break;
  946 
  947             case LADSPA_HINT_DEFAULT_HIGH:
  948                 if (LADSPA_IS_HINT_LOGARITHMIC(hint))
  949                     u->control[h] = (LADSPA_Data) exp(log(lower) * 0.25 + log(upper) * 0.75);
  950                 else
  951                     u->control[h] = (LADSPA_Data) (lower * 0.25 + upper * 0.75);
  952                 break;
  953 
  954             case LADSPA_HINT_DEFAULT_0:
  955                 u->control[h] = 0;
  956                 break;
  957 
  958             case LADSPA_HINT_DEFAULT_1:
  959                 u->control[h] = 1;
  960                 break;
  961 
  962             case LADSPA_HINT_DEFAULT_100:
  963                 u->control[h] = 100;
  964                 break;
  965 
  966             case LADSPA_HINT_DEFAULT_440:
  967                 u->control[h] = 440;
  968                 break;
  969 
  970             default:
  971                 pa_assert_not_reached();
  972             }
  973         }
  974         else {
  975             if (LADSPA_IS_HINT_INTEGER(hint)) {
  976                 u->control[h] = roundf(control_values[h]);
  977             }
  978             else {
  979                 u->control[h] = control_values[h];
  980             }
  981         }
  982 
  983         h++;
  984     }
  985 
  986     /* set the use_default array to the user data */
  987     memcpy(u->use_default, use_default, u->n_control * sizeof(u->use_default[0]));
  988 
  989     return 0;
  990 }
  991 
  992 int pa__init(pa_module*m) {
  993     struct userdata *u;
  994     pa_sample_spec ss;
  995     pa_channel_map map;
  996     pa_modargs *ma;
  997     char *t;
  998     const char *master_name;
  999     pa_sink *master;
 1000     pa_sink_input_new_data sink_input_data;
 1001     pa_sink_new_data sink_data;
 1002     const char *plugin, *label, *input_ladspaport_map, *output_ladspaport_map;
 1003     LADSPA_Descriptor_Function descriptor_func;
 1004     unsigned long input_ladspaport[PA_CHANNELS_MAX], output_ladspaport[PA_CHANNELS_MAX];
 1005     const char *e, *cdata;
 1006     const LADSPA_Descriptor *d;
 1007     unsigned long p, h, j, n_control, c;
 1008     pa_memchunk silence;
 1009 
 1010     pa_assert(m);
 1011 
 1012     pa_assert_cc(sizeof(LADSPA_Data) == sizeof(float));
 1013 
 1014     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
 1015         pa_log("Failed to parse module arguments.");
 1016         goto fail;
 1017     }
 1018 
 1019     master_name = pa_modargs_get_value(ma, "sink_master", NULL);
 1020     if (!master_name) {
 1021         master_name = pa_modargs_get_value(ma, "master", NULL);
 1022         if (master_name)
 1023             pa_log_warn("The 'master' module argument is deprecated and may be removed in the future, "
 1024                         "please use the 'sink_master' argument instead.");
 1025     }
 1026 
 1027     master = pa_namereg_get(m->core, master_name, PA_NAMEREG_SINK);
 1028     if (!master) {
 1029         pa_log("Master sink not found.");
 1030         goto fail;
 1031     }
 1032 
 1033     ss = master->sample_spec;
 1034     ss.format = PA_SAMPLE_FLOAT32;
 1035     map = master->channel_map;
 1036     if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
 1037         pa_log("Invalid sample format specification or channel map");
 1038         goto fail;
 1039     }
 1040 
 1041     if (ss.format != PA_SAMPLE_FLOAT32) {
 1042         pa_log("LADSPA accepts float format only");
 1043         goto fail;
 1044     }
 1045 
 1046     if (!(plugin = pa_modargs_get_value(ma, "plugin", NULL))) {
 1047         pa_log("Missing LADSPA plugin name");
 1048         goto fail;
 1049     }
 1050 
 1051     if (!(label = pa_modargs_get_value(ma, "label", NULL))) {
 1052         pa_log("Missing LADSPA plugin label");
 1053         goto fail;
 1054     }
 1055 
 1056     if (!(input_ladspaport_map = pa_modargs_get_value(ma, "input_ladspaport_map", NULL)))
 1057         pa_log_debug("Using default input ladspa port mapping");
 1058 
 1059     if (!(output_ladspaport_map = pa_modargs_get_value(ma, "output_ladspaport_map", NULL)))
 1060         pa_log_debug("Using default output ladspa port mapping");
 1061 
 1062     cdata = pa_modargs_get_value(ma, "control", NULL);
 1063 
 1064     u = pa_xnew0(struct userdata, 1);
 1065     u->module = m;
 1066     m->userdata = u;
 1067     u->max_ladspaport_count = 1; /*to avoid division by zero etc. in pa__done when failing before this value has been set*/
 1068     u->channels = 0;
 1069     u->input = NULL;
 1070     u->output = NULL;
 1071     u->ss = ss;
 1072 
 1073     if (!(e = getenv("LADSPA_PATH")))
 1074         /* The LADSPA_PATH preprocessor macro isn't a string literal (i.e. it
 1075          * doesn't contain quotes), because otherwise the build system would
 1076          * have an extra burden of getting the escaping right (Windows paths
 1077          * are especially tricky). PA_EXPAND_AND_STRINGIZE does the necessary
 1078          * escaping. */
 1079         e = PA_EXPAND_AND_STRINGIZE(LADSPA_PATH);
 1080 
 1081     /* FIXME: This is not exactly thread safe */
 1082     t = pa_xstrdup(lt_dlgetsearchpath());
 1083     lt_dlsetsearchpath(e);
 1084     m->dl = lt_dlopenext(plugin);
 1085     lt_dlsetsearchpath(t);
 1086     pa_xfree(t);
 1087 
 1088     if (!m->dl) {
 1089         pa_log("Failed to load LADSPA plugin: %s", lt_dlerror());
 1090         goto fail;
 1091     }
 1092 
 1093     if (!(descriptor_func = (LADSPA_Descriptor_Function) pa_load_sym(m->dl, NULL, "ladspa_descriptor"))) {
 1094         pa_log("LADSPA module lacks ladspa_descriptor() symbol.");
 1095         goto fail;
 1096     }
 1097 
 1098     for (j = 0;; j++) {
 1099 
 1100         if (!(d = descriptor_func(j))) {
 1101             pa_log("Failed to find plugin label '%s' in plugin '%s'.", label, plugin);
 1102             goto fail;
 1103         }
 1104 
 1105         if (pa_streq(d->Label, label))
 1106             break;
 1107     }
 1108 
 1109     u->descriptor = d;
 1110 
 1111     pa_log_debug("Module: %s", plugin);
 1112     pa_log_debug("Label: %s", d->Label);
 1113     pa_log_debug("Unique ID: %lu", d->UniqueID);
 1114     pa_log_debug("Name: %s", d->Name);
 1115     pa_log_debug("Maker: %s", d->Maker);
 1116     pa_log_debug("Copyright: %s", d->Copyright);
 1117 
 1118     n_control = 0;
 1119     u->channels = ss.channels;
 1120 
 1121     /*
 1122     * Enumerate ladspa ports
 1123     * Default mapping is in order given by the plugin
 1124     */
 1125     for (p = 0; p < d->PortCount; p++) {
 1126         if (LADSPA_IS_PORT_AUDIO(d->PortDescriptors[p])) {
 1127             if (LADSPA_IS_PORT_INPUT(d->PortDescriptors[p])) {
 1128                 pa_log_debug("Port %lu is input: %s", p, d->PortNames[p]);
 1129                 input_ladspaport[u->input_count] = p;
 1130                 u->input_count++;
 1131             } else if (LADSPA_IS_PORT_OUTPUT(d->PortDescriptors[p])) {
 1132                 pa_log_debug("Port %lu is output: %s", p, d->PortNames[p]);
 1133                 output_ladspaport[u->output_count] = p;
 1134                 u->output_count++;
 1135             }
 1136         } else if (LADSPA_IS_PORT_CONTROL(d->PortDescriptors[p]) && LADSPA_IS_PORT_INPUT(d->PortDescriptors[p])) {
 1137             pa_log_debug("Port %lu is control: %s", p, d->PortNames[p]);
 1138             n_control++;
 1139         } else
 1140             pa_log_debug("Ignored port %s", d->PortNames[p]);
 1141         /* XXX: Has anyone ever seen an in-place plugin with non-equal number of input and output ports? */
 1142         /* Could be if the plugin is for up-mixing stereo to 5.1 channels */
 1143         /* Or if the plugin is down-mixing 5.1 to two channel stereo or binaural encoded signal */
 1144         if (u->input_count > u->max_ladspaport_count)
 1145             u->max_ladspaport_count = u->input_count;
 1146         else
 1147             u->max_ladspaport_count = u->output_count;
 1148     }
 1149 
 1150     if (u->channels % u->max_ladspaport_count) {
 1151         pa_log("Cannot handle non-integral number of plugins required for given number of channels");
 1152         goto fail;
 1153     }
 1154 
 1155     pa_log_debug("Will run %lu plugin instances", u->channels / u->max_ladspaport_count);
 1156 
 1157     /* Parse data for input ladspa port map */
 1158     if (input_ladspaport_map) {
 1159         const char *state = NULL;
 1160         char *pname;
 1161         c = 0;
 1162         while ((pname = pa_split(input_ladspaport_map, ",", &state))) {
 1163             if (c == u->input_count) {
 1164                 pa_log("Too many ports in input ladspa port map");
 1165                 pa_xfree(pname);
 1166                 goto fail;
 1167             }
 1168 
 1169             for (p = 0; p < d->PortCount; p++) {
 1170                 if (pa_streq(d->PortNames[p], pname)) {
 1171                     if (LADSPA_IS_PORT_AUDIO(d->PortDescriptors[p]) && LADSPA_IS_PORT_INPUT(d->PortDescriptors[p])) {
 1172                         input_ladspaport[c] = p;
 1173                     } else {
 1174                         pa_log("Port %s is not an audio input ladspa port", pname);
 1175                         pa_xfree(pname);
 1176                         goto fail;
 1177                     }
 1178                 }
 1179             }
 1180             c++;
 1181             pa_xfree(pname);
 1182         }
 1183     }
 1184 
 1185     /* Parse data for output port map */
 1186     if (output_ladspaport_map) {
 1187         const char *state = NULL;
 1188         char *pname;
 1189         c = 0;
 1190         while ((pname = pa_split(output_ladspaport_map, ",", &state))) {
 1191             if (c == u->output_count) {
 1192                 pa_log("Too many ports in output ladspa port map");
 1193                 pa_xfree(pname);
 1194                 goto fail;
 1195             }
 1196             for (p = 0; p < d->PortCount; p++) {
 1197                 if (pa_streq(d->PortNames[p], pname)) {
 1198                     if (LADSPA_IS_PORT_AUDIO(d->PortDescriptors[p]) && LADSPA_IS_PORT_OUTPUT(d->PortDescriptors[p])) {
 1199                         output_ladspaport[c] = p;
 1200                     } else {
 1201                         pa_log("Port %s is not an output ladspa port", pname);
 1202                         pa_xfree(pname);
 1203                         goto fail;
 1204                     }
 1205                 }
 1206             }
 1207             c++;
 1208             pa_xfree(pname);
 1209         }
 1210     }
 1211 
 1212     u->block_size = pa_frame_align(pa_mempool_block_size_max(m->core->mempool), &ss);
 1213 
 1214     /* Create buffers */
 1215     if (LADSPA_IS_INPLACE_BROKEN(d->Properties)) {
 1216         u->input = (LADSPA_Data**) pa_xnew(LADSPA_Data*, (unsigned) u->input_count);
 1217         for (c = 0; c < u->input_count; c++)
 1218             u->input[c] = (LADSPA_Data*) pa_xnew(uint8_t, (unsigned) u->block_size);
 1219         u->output = (LADSPA_Data**) pa_xnew(LADSPA_Data*, (unsigned) u->output_count);
 1220         for (c = 0; c < u->output_count; c++)
 1221             u->output[c] = (LADSPA_Data*) pa_xnew(uint8_t, (unsigned) u->block_size);
 1222     } else {
 1223         u->input = (LADSPA_Data**) pa_xnew(LADSPA_Data*, (unsigned) u->max_ladspaport_count);
 1224         for (c = 0; c < u->max_ladspaport_count; c++)
 1225             u->input[c] = (LADSPA_Data*) pa_xnew(uint8_t, (unsigned) u->block_size);
 1226         u->output = u->input;
 1227     }
 1228     /* Initialize plugin instances */
 1229     for (h = 0; h < (u->channels / u->max_ladspaport_count); h++) {
 1230         if (!(u->handle[h] = d->instantiate(d, ss.rate))) {
 1231             pa_log("Failed to instantiate plugin %s with label %s", plugin, d->Label);
 1232             goto fail;
 1233         }
 1234 
 1235         for (c = 0; c < u->input_count; c++)
 1236             d->connect_port(u->handle[h], input_ladspaport[c], u->input[c]);
 1237         for (c = 0; c < u->output_count; c++)
 1238             d->connect_port(u->handle[h], output_ladspaport[c], u->output[c]);
 1239     }
 1240 
 1241     u->n_control = n_control;
 1242 
 1243     if (u->n_control > 0) {
 1244         double *control_values;
 1245         bool *use_default;
 1246 
 1247         /* temporary storage for parser */
 1248         control_values = pa_xnew(double, (unsigned) u->n_control);
 1249         use_default = pa_xnew(bool, (unsigned) u->n_control);
 1250 
 1251         /* real storage */
 1252         u->control = pa_xnew(LADSPA_Data, (unsigned) u->n_control);
 1253         u->use_default = pa_xnew(bool, (unsigned) u->n_control);
 1254 
 1255         if ((parse_control_parameters(u, cdata, control_values, use_default) < 0) ||
 1256             (write_control_parameters(u, control_values, use_default) < 0)) {
 1257             pa_xfree(control_values);
 1258             pa_xfree(use_default);
 1259 
 1260             pa_log("Failed to parse, validate or set control parameters");
 1261 
 1262             goto fail;
 1263         }
 1264         connect_control_ports(u);
 1265         pa_xfree(control_values);
 1266         pa_xfree(use_default);
 1267     }
 1268 
 1269     if (d->activate)
 1270         for (c = 0; c < (u->channels / u->max_ladspaport_count); c++)
 1271             d->activate(u->handle[c]);
 1272 
 1273     /* Create sink */
 1274     pa_sink_new_data_init(&sink_data);
 1275     sink_data.driver = __FILE__;
 1276     sink_data.module = m;
 1277     if (!(sink_data.name = pa_xstrdup(pa_modargs_get_value(ma, "sink_name", NULL))))
 1278         sink_data.name = pa_sprintf_malloc("%s.ladspa", master->name);
 1279     pa_sink_new_data_set_sample_spec(&sink_data, &ss);
 1280     pa_sink_new_data_set_channel_map(&sink_data, &map);
 1281     pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_MASTER_DEVICE, master->name);
 1282     pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_CLASS, "filter");
 1283     pa_proplist_sets(sink_data.proplist, "device.ladspa.module", plugin);
 1284     pa_proplist_sets(sink_data.proplist, "device.ladspa.label", d->Label);
 1285     pa_proplist_sets(sink_data.proplist, "device.ladspa.name", d->Name);
 1286     pa_proplist_sets(sink_data.proplist, "device.ladspa.maker", d->Maker);
 1287     pa_proplist_sets(sink_data.proplist, "device.ladspa.copyright", d->Copyright);
 1288     pa_proplist_setf(sink_data.proplist, "device.ladspa.unique_id", "%lu", (unsigned long) d->UniqueID);
 1289 
 1290     if (pa_modargs_get_proplist(ma, "sink_properties", sink_data.proplist, PA_UPDATE_REPLACE) < 0) {
 1291         pa_log("Invalid properties");
 1292         pa_sink_new_data_done(&sink_data);
 1293         goto fail;
 1294     }
 1295 
 1296     u->autoloaded = DEFAULT_AUTOLOADED;
 1297     if (pa_modargs_get_value_boolean(ma, "autoloaded", &u->autoloaded) < 0) {
 1298         pa_log("Failed to parse autoloaded value");
 1299         goto fail;
 1300     }
 1301 
 1302     if ((u->auto_desc = !pa_proplist_contains(sink_data.proplist, PA_PROP_DEVICE_DESCRIPTION))) {
 1303         const char *z;
 1304 
 1305         z = pa_proplist_gets(master->proplist, PA_PROP_DEVICE_DESCRIPTION);
 1306         pa_proplist_setf(sink_data.proplist, PA_PROP_DEVICE_DESCRIPTION, "LADSPA Plugin %s on %s", d->Name, z ? z : master->name);
 1307     }
 1308 
 1309     u->sink = pa_sink_new(m->core, &sink_data,
 1310                           (master->flags & (PA_SINK_LATENCY|PA_SINK_DYNAMIC_LATENCY)) | PA_SINK_SHARE_VOLUME_WITH_MASTER);
 1311     pa_sink_new_data_done(&sink_data);
 1312 
 1313     if (!u->sink) {
 1314         pa_log("Failed to create sink.");
 1315         goto fail;
 1316     }
 1317 
 1318     u->sink->parent.process_msg = sink_process_msg_cb;
 1319     u->sink->set_state_in_main_thread = sink_set_state_in_main_thread_cb;
 1320     u->sink->set_state_in_io_thread = sink_set_state_in_io_thread_cb;
 1321     u->sink->update_requested_latency = sink_update_requested_latency_cb;
 1322     u->sink->request_rewind = sink_request_rewind_cb;
 1323     pa_sink_set_set_mute_callback(u->sink, sink_set_mute_cb);
 1324     u->sink->userdata = u;
 1325 
 1326     pa_sink_set_asyncmsgq(u->sink, master->asyncmsgq);
 1327 
 1328     /* Create sink input */
 1329     pa_sink_input_new_data_init(&sink_input_data);
 1330     sink_input_data.driver = __FILE__;
 1331     sink_input_data.module = m;
 1332     pa_sink_input_new_data_set_sink(&sink_input_data, master, false, true);
 1333     sink_input_data.origin_sink = u->sink;
 1334     pa_proplist_sets(sink_input_data.proplist, PA_PROP_MEDIA_NAME, "LADSPA Stream");
 1335     pa_proplist_sets(sink_input_data.proplist, PA_PROP_MEDIA_ROLE, "filter");
 1336 
 1337     if (pa_modargs_get_proplist(ma, "sink_input_properties", sink_input_data.proplist, PA_UPDATE_REPLACE) < 0) {
 1338         pa_log("Invalid properties");
 1339         pa_sink_input_new_data_done(&sink_input_data);
 1340         goto fail;
 1341     }
 1342 
 1343     pa_sink_input_new_data_set_sample_spec(&sink_input_data, &ss);
 1344     pa_sink_input_new_data_set_channel_map(&sink_input_data, &map);
 1345     sink_input_data.flags |= PA_SINK_INPUT_START_CORKED;
 1346 
 1347     pa_sink_input_new(&u->sink_input, m->core, &sink_input_data);
 1348     pa_sink_input_new_data_done(&sink_input_data);
 1349 
 1350     if (!u->sink_input)
 1351         goto fail;
 1352 
 1353     u->sink_input->pop = sink_input_pop_cb;
 1354     u->sink_input->process_rewind = sink_input_process_rewind_cb;
 1355     u->sink_input->update_max_rewind = sink_input_update_max_rewind_cb;
 1356     u->sink_input->update_max_request = sink_input_update_max_request_cb;
 1357     u->sink_input->update_sink_latency_range = sink_input_update_sink_latency_range_cb;
 1358     u->sink_input->update_sink_fixed_latency = sink_input_update_sink_fixed_latency_cb;
 1359     u->sink_input->kill = sink_input_kill_cb;
 1360     u->sink_input->attach = sink_input_attach_cb;
 1361     u->sink_input->detach = sink_input_detach_cb;
 1362     u->sink_input->may_move_to = sink_input_may_move_to_cb;
 1363     u->sink_input->moving = sink_input_moving_cb;
 1364     u->sink_input->mute_changed = sink_input_mute_changed_cb;
 1365     u->sink_input->suspend = sink_input_suspend_cb;
 1366     u->sink_input->userdata = u;
 1367 
 1368     u->sink->input_to_master = u->sink_input;
 1369 
 1370     pa_sink_input_get_silence(u->sink_input, &silence);
 1371     u->memblockq = pa_memblockq_new("module-ladspa-sink memblockq", 0, MEMBLOCKQ_MAXLENGTH, 0, &ss, 1, 1, 0, &silence);
 1372     pa_memblock_unref(silence.memblock);
 1373 
 1374     /* The order here is important. The input must be put first,
 1375      * otherwise streams might attach to the sink before the sink
 1376      * input is attached to the master. */
 1377     pa_sink_input_put(u->sink_input);
 1378     pa_sink_put(u->sink);
 1379     pa_sink_input_cork(u->sink_input, false);
 1380 
 1381 #ifdef HAVE_DBUS
 1382     dbus_init(u);
 1383 #endif
 1384 
 1385     pa_modargs_free(ma);
 1386 
 1387     return 0;
 1388 
 1389 fail:
 1390     if (ma)
 1391         pa_modargs_free(ma);
 1392 
 1393     pa__done(m);
 1394 
 1395     return -1;
 1396 }
 1397 
 1398 int pa__get_n_used(pa_module *m) {
 1399     struct userdata *u;
 1400 
 1401     pa_assert(m);
 1402     pa_assert_se(u = m->userdata);
 1403 
 1404     return pa_sink_linked_by(u->sink);
 1405 }
 1406 
 1407 void pa__done(pa_module*m) {
 1408     struct userdata *u;
 1409     unsigned c;
 1410 
 1411     pa_assert(m);
 1412 
 1413     if (!(u = m->userdata))
 1414         return;
 1415 
 1416     /* See comments in sink_input_kill_cb() above regarding
 1417     * destruction order! */
 1418 
 1419 #ifdef HAVE_DBUS
 1420     dbus_done(u);
 1421 #endif
 1422 
 1423     if (u->sink_input)
 1424         pa_sink_input_cork(u->sink_input, true);
 1425 
 1426     if (u->sink)
 1427         pa_sink_unlink(u->sink);
 1428 
 1429     if (u->sink_input) {
 1430         pa_sink_input_unlink(u->sink_input);
 1431         pa_sink_input_unref(u->sink_input);
 1432     }
 1433 
 1434     if (u->sink)
 1435         pa_sink_unref(u->sink);
 1436 
 1437     for (c = 0; c < (u->channels / u->max_ladspaport_count); c++) {
 1438         if (u->handle[c]) {
 1439             if (u->descriptor->deactivate)
 1440                 u->descriptor->deactivate(u->handle[c]);
 1441             u->descriptor->cleanup(u->handle[c]);
 1442         }
 1443     }
 1444 
 1445     if (u->output == u->input) {
 1446         if (u->input != NULL) {
 1447             for (c = 0; c < u->max_ladspaport_count; c++)
 1448                 pa_xfree(u->input[c]);
 1449             pa_xfree(u->input);
 1450         }
 1451     } else {
 1452         if (u->input != NULL) {
 1453             for (c = 0; c < u->input_count; c++)
 1454                 pa_xfree(u->input[c]);
 1455             pa_xfree(u->input);
 1456         }
 1457         if (u->output != NULL) {
 1458             for (c = 0; c < u->output_count; c++)
 1459                 pa_xfree(u->output[c]);
 1460             pa_xfree(u->output);
 1461         }
 1462     }
 1463 
 1464     if (u->memblockq)
 1465         pa_memblockq_free(u->memblockq);
 1466 
 1467     pa_xfree(u->control);
 1468     pa_xfree(u->use_default);
 1469     pa_xfree(u);
 1470 }