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