]> code.delx.au - pulseaudio/blob - src/modules/module-ladspa-sink.c
modules: Remove obsolete may_move_to callbacks
[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 pa_bool_t *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 pa_bool_t 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, pa_bool_t *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 pa_bool_t *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(pa_bool_t, 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_volume_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_volume(u->sink_input, &s->real_volume, s->save_volume, TRUE);
438 }
439
440 /* Called from main context */
441 static void sink_set_mute_cb(pa_sink *s) {
442 struct userdata *u;
443
444 pa_sink_assert_ref(s);
445 pa_assert_se(u = s->userdata);
446
447 if (!PA_SINK_IS_LINKED(pa_sink_get_state(s)) ||
448 !PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(u->sink_input)))
449 return;
450
451 pa_sink_input_set_mute(u->sink_input, s->muted, s->save_muted);
452 }
453
454 /* Called from I/O thread context */
455 static int sink_input_pop_cb(pa_sink_input *i, size_t nbytes, pa_memchunk *chunk) {
456 struct userdata *u;
457 float *src, *dst;
458 size_t fs;
459 unsigned n, h, c;
460 pa_memchunk tchunk;
461
462 pa_sink_input_assert_ref(i);
463 pa_assert(chunk);
464 pa_assert_se(u = i->userdata);
465
466 /* Hmm, process any rewind request that might be queued up */
467 pa_sink_process_rewind(u->sink, 0);
468
469 while (pa_memblockq_peek(u->memblockq, &tchunk) < 0) {
470 pa_memchunk nchunk;
471
472 pa_sink_render(u->sink, nbytes, &nchunk);
473 pa_memblockq_push(u->memblockq, &nchunk);
474 pa_memblock_unref(nchunk.memblock);
475 }
476
477 tchunk.length = PA_MIN(nbytes, tchunk.length);
478 pa_assert(tchunk.length > 0);
479
480 fs = pa_frame_size(&i->sample_spec);
481 n = (unsigned) (PA_MIN(tchunk.length, u->block_size) / fs);
482
483 pa_assert(n > 0);
484
485 chunk->index = 0;
486 chunk->length = n*fs;
487 chunk->memblock = pa_memblock_new(i->sink->core->mempool, chunk->length);
488
489 pa_memblockq_drop(u->memblockq, chunk->length);
490
491 src = pa_memblock_acquire_chunk(&tchunk);
492 dst = pa_memblock_acquire(chunk->memblock);
493
494 for (h = 0; h < (u->channels / u->max_ladspaport_count); h++) {
495 for (c = 0; c < u->input_count; c++)
496 pa_sample_clamp(PA_SAMPLE_FLOAT32NE, u->input[c], sizeof(float), src+ h*u->max_ladspaport_count + c, u->channels*sizeof(float), n);
497 u->descriptor->run(u->handle[h], n);
498 for (c = 0; c < u->output_count; c++)
499 pa_sample_clamp(PA_SAMPLE_FLOAT32NE, dst + h*u->max_ladspaport_count + c, u->channels*sizeof(float), u->output[c], sizeof(float), n);
500 }
501
502 pa_memblock_release(tchunk.memblock);
503 pa_memblock_release(chunk->memblock);
504
505 pa_memblock_unref(tchunk.memblock);
506
507 return 0;
508 }
509
510 /* Called from I/O thread context */
511 static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes) {
512 struct userdata *u;
513 size_t amount = 0;
514
515 pa_sink_input_assert_ref(i);
516 pa_assert_se(u = i->userdata);
517
518 if (u->sink->thread_info.rewind_nbytes > 0) {
519 size_t max_rewrite;
520
521 max_rewrite = nbytes + pa_memblockq_get_length(u->memblockq);
522 amount = PA_MIN(u->sink->thread_info.rewind_nbytes, max_rewrite);
523 u->sink->thread_info.rewind_nbytes = 0;
524
525 if (amount > 0) {
526 unsigned c;
527
528 pa_memblockq_seek(u->memblockq, - (int64_t) amount, PA_SEEK_RELATIVE, TRUE);
529
530 pa_log_debug("Resetting plugin");
531
532 /* Reset the plugin */
533 if (u->descriptor->deactivate)
534 for (c = 0; c < (u->channels / u->max_ladspaport_count); c++)
535 u->descriptor->deactivate(u->handle[c]);
536 if (u->descriptor->activate)
537 for (c = 0; c < (u->channels / u->max_ladspaport_count); c++)
538 u->descriptor->activate(u->handle[c]);
539 }
540 }
541
542 pa_sink_process_rewind(u->sink, amount);
543 pa_memblockq_rewind(u->memblockq, nbytes);
544 }
545
546 /* Called from I/O thread context */
547 static void sink_input_update_max_rewind_cb(pa_sink_input *i, size_t nbytes) {
548 struct userdata *u;
549
550 pa_sink_input_assert_ref(i);
551 pa_assert_se(u = i->userdata);
552
553 /* FIXME: Too small max_rewind:
554 * https://bugs.freedesktop.org/show_bug.cgi?id=53709 */
555 pa_memblockq_set_maxrewind(u->memblockq, nbytes);
556 pa_sink_set_max_rewind_within_thread(u->sink, nbytes);
557 }
558
559 /* Called from I/O thread context */
560 static void sink_input_update_max_request_cb(pa_sink_input *i, size_t nbytes) {
561 struct userdata *u;
562
563 pa_sink_input_assert_ref(i);
564 pa_assert_se(u = i->userdata);
565
566 pa_sink_set_max_request_within_thread(u->sink, nbytes);
567 }
568
569 /* Called from I/O thread context */
570 static void sink_input_update_sink_latency_range_cb(pa_sink_input *i) {
571 struct userdata *u;
572
573 pa_sink_input_assert_ref(i);
574 pa_assert_se(u = i->userdata);
575
576 pa_sink_set_latency_range_within_thread(u->sink, i->sink->thread_info.min_latency, i->sink->thread_info.max_latency);
577 }
578
579 /* Called from I/O thread context */
580 static void sink_input_update_sink_fixed_latency_cb(pa_sink_input *i) {
581 struct userdata *u;
582
583 pa_sink_input_assert_ref(i);
584 pa_assert_se(u = i->userdata);
585
586 pa_sink_set_fixed_latency_within_thread(u->sink, i->sink->thread_info.fixed_latency);
587 }
588
589 /* Called from I/O thread context */
590 static void sink_input_detach_cb(pa_sink_input *i) {
591 struct userdata *u;
592
593 pa_sink_input_assert_ref(i);
594 pa_assert_se(u = i->userdata);
595
596 pa_sink_detach_within_thread(u->sink);
597
598 pa_sink_set_rtpoll(u->sink, NULL);
599 }
600
601 /* Called from I/O thread context */
602 static void sink_input_attach_cb(pa_sink_input *i) {
603 struct userdata *u;
604
605 pa_sink_input_assert_ref(i);
606 pa_assert_se(u = i->userdata);
607
608 pa_sink_set_rtpoll(u->sink, i->sink->thread_info.rtpoll);
609 pa_sink_set_latency_range_within_thread(u->sink, i->sink->thread_info.min_latency, i->sink->thread_info.max_latency);
610 pa_sink_set_fixed_latency_within_thread(u->sink, i->sink->thread_info.fixed_latency);
611 pa_sink_set_max_request_within_thread(u->sink, pa_sink_input_get_max_request(i));
612
613 /* FIXME: Too small max_rewind:
614 * https://bugs.freedesktop.org/show_bug.cgi?id=53709 */
615 pa_sink_set_max_rewind_within_thread(u->sink, pa_sink_input_get_max_rewind(i));
616
617 pa_sink_attach_within_thread(u->sink);
618 }
619
620 /* Called from main context */
621 static void sink_input_kill_cb(pa_sink_input *i) {
622 struct userdata *u;
623
624 pa_sink_input_assert_ref(i);
625 pa_assert_se(u = i->userdata);
626
627 /* The order here matters! We first kill the sink input, followed
628 * by the sink. That means the sink callbacks must be protected
629 * against an unconnected sink input! */
630 pa_sink_input_unlink(u->sink_input);
631 pa_sink_unlink(u->sink);
632
633 pa_sink_input_unref(u->sink_input);
634 u->sink_input = NULL;
635
636 pa_sink_unref(u->sink);
637 u->sink = NULL;
638
639 pa_module_unload_request(u->module, TRUE);
640 }
641
642 /* Called from IO thread context */
643 static void sink_input_state_change_cb(pa_sink_input *i, pa_sink_input_state_t state) {
644 struct userdata *u;
645
646 pa_sink_input_assert_ref(i);
647 pa_assert_se(u = i->userdata);
648
649 /* If we are added for the first time, ask for a rewinding so that
650 * we are heard right-away. */
651 if (PA_SINK_INPUT_IS_LINKED(state) &&
652 i->thread_info.state == PA_SINK_INPUT_INIT) {
653 pa_log_debug("Requesting rewind due to state change.");
654 pa_sink_input_request_rewind(i, 0, FALSE, TRUE, TRUE);
655 }
656 }
657
658 /* Called from main context */
659 static void sink_input_moving_cb(pa_sink_input *i, pa_sink *dest) {
660 struct userdata *u;
661
662 pa_sink_input_assert_ref(i);
663 pa_assert_se(u = i->userdata);
664
665 if (dest) {
666 pa_sink_set_asyncmsgq(u->sink, dest->asyncmsgq);
667 pa_sink_update_flags(u->sink, PA_SINK_LATENCY|PA_SINK_DYNAMIC_LATENCY, dest->flags);
668 } else
669 pa_sink_set_asyncmsgq(u->sink, NULL);
670
671 if (u->auto_desc && dest) {
672 const char *z;
673 pa_proplist *pl;
674
675 pl = pa_proplist_new();
676 z = pa_proplist_gets(dest->proplist, PA_PROP_DEVICE_DESCRIPTION);
677 pa_proplist_setf(pl, PA_PROP_DEVICE_DESCRIPTION, "LADSPA Plugin %s on %s",
678 pa_proplist_gets(u->sink->proplist, "device.ladspa.name"), z ? z : dest->name);
679
680 pa_sink_update_proplist(u->sink, PA_UPDATE_REPLACE, pl);
681 pa_proplist_free(pl);
682 }
683 }
684
685 /* Called from main context */
686 static void sink_input_volume_changed_cb(pa_sink_input *i) {
687 struct userdata *u;
688
689 pa_sink_input_assert_ref(i);
690 pa_assert_se(u = i->userdata);
691
692 pa_sink_volume_changed(u->sink, &i->volume);
693 }
694
695 /* Called from main context */
696 static void sink_input_mute_changed_cb(pa_sink_input *i) {
697 struct userdata *u;
698
699 pa_sink_input_assert_ref(i);
700 pa_assert_se(u = i->userdata);
701
702 pa_sink_mute_changed(u->sink, i->muted);
703 }
704
705 static int parse_control_parameters(struct userdata *u, const char *cdata, double *read_values, pa_bool_t *use_default) {
706 unsigned long p = 0;
707 const char *state = NULL;
708 char *k;
709
710 pa_assert(read_values);
711 pa_assert(use_default);
712 pa_assert(u);
713
714 pa_log_debug("Trying to read %lu control values", u->n_control);
715
716 if (!cdata && u->n_control > 0)
717 return -1;
718
719 pa_log_debug("cdata: '%s'", cdata);
720
721 while ((k = pa_split(cdata, ",", &state)) && p < u->n_control) {
722 double f;
723
724 if (*k == 0) {
725 pa_log_debug("Read empty config value (p=%lu)", p);
726 use_default[p++] = TRUE;
727 pa_xfree(k);
728 continue;
729 }
730
731 if (pa_atod(k, &f) < 0) {
732 pa_log_debug("Failed to parse control value '%s' (p=%lu)", k, p);
733 pa_xfree(k);
734 goto fail;
735 }
736
737 pa_xfree(k);
738
739 pa_log_debug("Read config value %f (p=%lu)", f, p);
740
741 use_default[p] = FALSE;
742 read_values[p++] = f;
743 }
744
745 /* The previous loop doesn't take the last control value into account
746 if it is left empty, so we do it here. */
747 if (*cdata == 0 || cdata[strlen(cdata) - 1] == ',') {
748 if (p < u->n_control)
749 use_default[p] = TRUE;
750 p++;
751 }
752
753 if (p > u->n_control || k) {
754 pa_log("Too many control values passed, %lu expected.", u->n_control);
755 pa_xfree(k);
756 goto fail;
757 }
758
759 if (p < u->n_control) {
760 pa_log("Not enough control values passed, %lu expected, %lu passed.", u->n_control, p);
761 goto fail;
762 }
763
764 return 0;
765
766 fail:
767 return -1;
768 }
769
770 static void connect_control_ports(struct userdata *u) {
771 unsigned long p = 0, h = 0, c;
772 const LADSPA_Descriptor *d;
773
774 pa_assert(u);
775 pa_assert_se(d = u->descriptor);
776
777 for (p = 0; p < d->PortCount; p++) {
778 if (!LADSPA_IS_PORT_CONTROL(d->PortDescriptors[p]))
779 continue;
780
781 if (LADSPA_IS_PORT_OUTPUT(d->PortDescriptors[p])) {
782 for (c = 0; c < (u->channels / u->max_ladspaport_count); c++)
783 d->connect_port(u->handle[c], p, &u->control_out);
784 continue;
785 }
786
787 /* input control port */
788
789 pa_log_debug("Binding %f to port %s", u->control[h], d->PortNames[p]);
790
791 for (c = 0; c < (u->channels / u->max_ladspaport_count); c++)
792 d->connect_port(u->handle[c], p, &u->control[h]);
793
794 h++;
795 }
796 }
797
798 static int validate_control_parameters(struct userdata *u, double *control_values, pa_bool_t *use_default) {
799 unsigned long p = 0, h = 0;
800 const LADSPA_Descriptor *d;
801 pa_sample_spec ss;
802
803 pa_assert(control_values);
804 pa_assert(use_default);
805 pa_assert(u);
806 pa_assert_se(d = u->descriptor);
807
808 ss = u->ss;
809
810 /* Iterate over all ports. Check for every control port that 1) it
811 * supports default values if a default value is provided and 2) the
812 * provided value is within the limits specified in the plugin. */
813
814 for (p = 0; p < d->PortCount; p++) {
815 LADSPA_PortRangeHintDescriptor hint = d->PortRangeHints[p].HintDescriptor;
816
817 if (!LADSPA_IS_PORT_CONTROL(d->PortDescriptors[p]))
818 continue;
819
820 if (LADSPA_IS_PORT_OUTPUT(d->PortDescriptors[p]))
821 continue;
822
823 if (use_default[h]) {
824 /* User wants to use default value. Check if the plugin
825 * provides it. */
826 if (!LADSPA_IS_HINT_HAS_DEFAULT(hint)) {
827 pa_log_warn("Control port value left empty but plugin defines no default.");
828 return -1;
829 }
830 }
831 else {
832 /* Check if the user-provided value is within the bounds. */
833 LADSPA_Data lower = d->PortRangeHints[p].LowerBound;
834 LADSPA_Data upper = d->PortRangeHints[p].UpperBound;
835
836 if (LADSPA_IS_HINT_SAMPLE_RATE(hint)) {
837 upper *= (LADSPA_Data) ss.rate;
838 lower *= (LADSPA_Data) ss.rate;
839 }
840
841 if (LADSPA_IS_HINT_BOUNDED_ABOVE(hint)) {
842 if (control_values[h] > upper) {
843 pa_log_warn("Control value %lu over upper bound: %f (upper bound: %f)", h, control_values[h], upper);
844 return -1;
845 }
846 }
847 if (LADSPA_IS_HINT_BOUNDED_BELOW(hint)) {
848 if (control_values[h] < lower) {
849 pa_log_warn("Control value %lu below lower bound: %f (lower bound: %f)", h, control_values[h], lower);
850 return -1;
851 }
852 }
853 }
854
855 h++;
856 }
857
858 return 0;
859 }
860
861 static int write_control_parameters(struct userdata *u, double *control_values, pa_bool_t *use_default) {
862 unsigned long p = 0, h = 0, c;
863 const LADSPA_Descriptor *d;
864 pa_sample_spec ss;
865
866 pa_assert(control_values);
867 pa_assert(use_default);
868 pa_assert(u);
869 pa_assert_se(d = u->descriptor);
870
871 ss = u->ss;
872
873 if (validate_control_parameters(u, control_values, use_default) < 0)
874 return -1;
875
876 /* p iterates over all ports, h is the control port iterator */
877
878 for (p = 0; p < d->PortCount; p++) {
879 LADSPA_PortRangeHintDescriptor hint = d->PortRangeHints[p].HintDescriptor;
880
881 if (!LADSPA_IS_PORT_CONTROL(d->PortDescriptors[p]))
882 continue;
883
884 if (LADSPA_IS_PORT_OUTPUT(d->PortDescriptors[p])) {
885 for (c = 0; c < (u->channels / u->max_ladspaport_count); c++)
886 d->connect_port(u->handle[c], p, &u->control_out);
887 continue;
888 }
889
890 if (use_default[h]) {
891
892 LADSPA_Data lower, upper;
893
894 lower = d->PortRangeHints[p].LowerBound;
895 upper = d->PortRangeHints[p].UpperBound;
896
897 if (LADSPA_IS_HINT_SAMPLE_RATE(hint)) {
898 lower *= (LADSPA_Data) ss.rate;
899 upper *= (LADSPA_Data) ss.rate;
900 }
901
902 switch (hint & LADSPA_HINT_DEFAULT_MASK) {
903
904 case LADSPA_HINT_DEFAULT_MINIMUM:
905 u->control[h] = lower;
906 break;
907
908 case LADSPA_HINT_DEFAULT_MAXIMUM:
909 u->control[h] = upper;
910 break;
911
912 case LADSPA_HINT_DEFAULT_LOW:
913 if (LADSPA_IS_HINT_LOGARITHMIC(hint))
914 u->control[h] = (LADSPA_Data) exp(log(lower) * 0.75 + log(upper) * 0.25);
915 else
916 u->control[h] = (LADSPA_Data) (lower * 0.75 + upper * 0.25);
917 break;
918
919 case LADSPA_HINT_DEFAULT_MIDDLE:
920 if (LADSPA_IS_HINT_LOGARITHMIC(hint))
921 u->control[h] = (LADSPA_Data) exp(log(lower) * 0.5 + log(upper) * 0.5);
922 else
923 u->control[h] = (LADSPA_Data) (lower * 0.5 + upper * 0.5);
924 break;
925
926 case LADSPA_HINT_DEFAULT_HIGH:
927 if (LADSPA_IS_HINT_LOGARITHMIC(hint))
928 u->control[h] = (LADSPA_Data) exp(log(lower) * 0.25 + log(upper) * 0.75);
929 else
930 u->control[h] = (LADSPA_Data) (lower * 0.25 + upper * 0.75);
931 break;
932
933 case LADSPA_HINT_DEFAULT_0:
934 u->control[h] = 0;
935 break;
936
937 case LADSPA_HINT_DEFAULT_1:
938 u->control[h] = 1;
939 break;
940
941 case LADSPA_HINT_DEFAULT_100:
942 u->control[h] = 100;
943 break;
944
945 case LADSPA_HINT_DEFAULT_440:
946 u->control[h] = 440;
947 break;
948
949 default:
950 pa_assert_not_reached();
951 }
952 }
953 else {
954 if (LADSPA_IS_HINT_INTEGER(hint)) {
955 u->control[h] = roundf(control_values[h]);
956 }
957 else {
958 u->control[h] = control_values[h];
959 }
960 }
961
962 h++;
963 }
964
965 /* set the use_default array to the user data */
966 memcpy(u->use_default, use_default, u->n_control * sizeof(u->use_default[0]));
967
968 return 0;
969 }
970
971
972 int pa__init(pa_module*m) {
973 struct userdata *u;
974 pa_sample_spec ss;
975 pa_channel_map map;
976 pa_modargs *ma;
977 char *t;
978 pa_sink *master;
979 pa_sink_input_new_data sink_input_data;
980 pa_sink_new_data sink_data;
981 const char *plugin, *label, *input_ladspaport_map, *output_ladspaport_map;
982 LADSPA_Descriptor_Function descriptor_func;
983 unsigned long input_ladspaport[PA_CHANNELS_MAX], output_ladspaport[PA_CHANNELS_MAX];
984 const char *e, *cdata;
985 const LADSPA_Descriptor *d;
986 unsigned long p, h, j, n_control, c;
987
988 pa_assert(m);
989
990 pa_assert_cc(sizeof(LADSPA_Data) == sizeof(float));
991
992 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
993 pa_log("Failed to parse module arguments.");
994 goto fail;
995 }
996
997 if (!(master = pa_namereg_get(m->core, pa_modargs_get_value(ma, "master", NULL), PA_NAMEREG_SINK))) {
998 pa_log("Master sink not found");
999 goto fail;
1000 }
1001
1002 ss = master->sample_spec;
1003 ss.format = PA_SAMPLE_FLOAT32;
1004 map = master->channel_map;
1005 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
1006 pa_log("Invalid sample format specification or channel map");
1007 goto fail;
1008 }
1009
1010 if (!(plugin = pa_modargs_get_value(ma, "plugin", NULL))) {
1011 pa_log("Missing LADSPA plugin name");
1012 goto fail;
1013 }
1014
1015 if (!(label = pa_modargs_get_value(ma, "label", NULL))) {
1016 pa_log("Missing LADSPA plugin label");
1017 goto fail;
1018 }
1019
1020 if (!(input_ladspaport_map = pa_modargs_get_value(ma, "input_ladspaport_map", NULL)))
1021 pa_log_debug("Using default input ladspa port mapping");
1022
1023 if (!(output_ladspaport_map = pa_modargs_get_value(ma, "output_ladspaport_map", NULL)))
1024 pa_log_debug("Using default output ladspa port mapping");
1025
1026 cdata = pa_modargs_get_value(ma, "control", NULL);
1027
1028 u = pa_xnew0(struct userdata, 1);
1029 u->module = m;
1030 m->userdata = u;
1031 u->memblockq = pa_memblockq_new("module-ladspa-sink memblockq", 0, MEMBLOCKQ_MAXLENGTH, 0, &ss, 1, 1, 0, NULL);
1032 u->max_ladspaport_count = 1; /*to avoid division by zero etc. in pa__done when failing before this value has been set*/
1033 u->channels = 0;
1034 u->input = NULL;
1035 u->output = NULL;
1036 u->ss = ss;
1037
1038 if (!(e = getenv("LADSPA_PATH")))
1039 e = LADSPA_PATH;
1040
1041 /* FIXME: This is not exactly thread safe */
1042 t = pa_xstrdup(lt_dlgetsearchpath());
1043 lt_dlsetsearchpath(e);
1044 m->dl = lt_dlopenext(plugin);
1045 lt_dlsetsearchpath(t);
1046 pa_xfree(t);
1047
1048 if (!m->dl) {
1049 pa_log("Failed to load LADSPA plugin: %s", lt_dlerror());
1050 goto fail;
1051 }
1052
1053 if (!(descriptor_func = (LADSPA_Descriptor_Function) pa_load_sym(m->dl, NULL, "ladspa_descriptor"))) {
1054 pa_log("LADSPA module lacks ladspa_descriptor() symbol.");
1055 goto fail;
1056 }
1057
1058 for (j = 0;; j++) {
1059
1060 if (!(d = descriptor_func(j))) {
1061 pa_log("Failed to find plugin label '%s' in plugin '%s'.", label, plugin);
1062 goto fail;
1063 }
1064
1065 if (pa_streq(d->Label, label))
1066 break;
1067 }
1068
1069 u->descriptor = d;
1070
1071 pa_log_debug("Module: %s", plugin);
1072 pa_log_debug("Label: %s", d->Label);
1073 pa_log_debug("Unique ID: %lu", d->UniqueID);
1074 pa_log_debug("Name: %s", d->Name);
1075 pa_log_debug("Maker: %s", d->Maker);
1076 pa_log_debug("Copyright: %s", d->Copyright);
1077
1078 n_control = 0;
1079 u->channels = ss.channels;
1080
1081 /*
1082 * Enumerate ladspa ports
1083 * Default mapping is in order given by the plugin
1084 */
1085 for (p = 0; p < d->PortCount; p++) {
1086 if (LADSPA_IS_PORT_AUDIO(d->PortDescriptors[p])) {
1087 if (LADSPA_IS_PORT_INPUT(d->PortDescriptors[p])) {
1088 pa_log_debug("Port %lu is input: %s", p, d->PortNames[p]);
1089 input_ladspaport[u->input_count] = p;
1090 u->input_count++;
1091 } else if (LADSPA_IS_PORT_OUTPUT(d->PortDescriptors[p])) {
1092 pa_log_debug("Port %lu is output: %s", p, d->PortNames[p]);
1093 output_ladspaport[u->output_count] = p;
1094 u->output_count++;
1095 }
1096 } else if (LADSPA_IS_PORT_CONTROL(d->PortDescriptors[p]) && LADSPA_IS_PORT_INPUT(d->PortDescriptors[p])) {
1097 pa_log_debug("Port %lu is control: %s", p, d->PortNames[p]);
1098 n_control++;
1099 } else
1100 pa_log_debug("Ignored port %s", d->PortNames[p]);
1101 /* XXX: Has anyone ever seen an in-place plugin with non-equal number of input and output ports? */
1102 /* Could be if the plugin is for up-mixing stereo to 5.1 channels */
1103 /* Or if the plugin is down-mixing 5.1 to two channel stereo or binaural encoded signal */
1104 if (u->input_count > u->max_ladspaport_count)
1105 u->max_ladspaport_count = u->input_count;
1106 else
1107 u->max_ladspaport_count = u->output_count;
1108 }
1109
1110 if (u->channels % u->max_ladspaport_count) {
1111 pa_log("Cannot handle non-integral number of plugins required for given number of channels");
1112 goto fail;
1113 }
1114
1115 pa_log_debug("Will run %lu plugin instances", u->channels / u->max_ladspaport_count);
1116
1117 /* Parse data for input ladspa port map */
1118 if (input_ladspaport_map) {
1119 const char *state = NULL;
1120 char *pname;
1121 c = 0;
1122 while ((pname = pa_split(input_ladspaport_map, ",", &state))) {
1123 if (c == u->input_count) {
1124 pa_log("Too many ports in input ladspa port map");
1125 pa_xfree(pname);
1126 goto fail;
1127 }
1128
1129 for (p = 0; p < d->PortCount; p++) {
1130 if (pa_streq(d->PortNames[p], pname)) {
1131 if (LADSPA_IS_PORT_AUDIO(d->PortDescriptors[p]) && LADSPA_IS_PORT_INPUT(d->PortDescriptors[p])) {
1132 input_ladspaport[c] = p;
1133 } else {
1134 pa_log("Port %s is not an audio input ladspa port", pname);
1135 pa_xfree(pname);
1136 goto fail;
1137 }
1138 }
1139 }
1140 c++;
1141 pa_xfree(pname);
1142 }
1143 }
1144
1145 /* Parse data for output port map */
1146 if (output_ladspaport_map) {
1147 const char *state = NULL;
1148 char *pname;
1149 c = 0;
1150 while ((pname = pa_split(output_ladspaport_map, ",", &state))) {
1151 if (c == u->output_count) {
1152 pa_log("Too many ports in output ladspa port map");
1153 pa_xfree(pname);
1154 goto fail;
1155 }
1156 for (p = 0; p < d->PortCount; p++) {
1157 if (pa_streq(d->PortNames[p], pname)) {
1158 if (LADSPA_IS_PORT_AUDIO(d->PortDescriptors[p]) && LADSPA_IS_PORT_OUTPUT(d->PortDescriptors[p])) {
1159 output_ladspaport[c] = p;
1160 } else {
1161 pa_log("Port %s is not an output ladspa port", pname);
1162 pa_xfree(pname);
1163 goto fail;
1164 }
1165 }
1166 }
1167 c++;
1168 pa_xfree(pname);
1169 }
1170 }
1171
1172
1173 u->block_size = pa_frame_align(pa_mempool_block_size_max(m->core->mempool), &ss);
1174
1175 /* Create buffers */
1176 if (LADSPA_IS_INPLACE_BROKEN(d->Properties)) {
1177 u->input = (LADSPA_Data**) pa_xnew(LADSPA_Data*, (unsigned) u->input_count);
1178 for (c = 0; c < u->input_count; c++)
1179 u->input[c] = (LADSPA_Data*) pa_xnew(uint8_t, (unsigned) u->block_size);
1180 u->output = (LADSPA_Data**) pa_xnew(LADSPA_Data*, (unsigned) u->output_count);
1181 for (c = 0; c < u->output_count; c++)
1182 u->output[c] = (LADSPA_Data*) pa_xnew(uint8_t, (unsigned) u->block_size);
1183 } else {
1184 u->input = (LADSPA_Data**) pa_xnew(LADSPA_Data*, (unsigned) u->max_ladspaport_count);
1185 for (c = 0; c < u->max_ladspaport_count; c++)
1186 u->input[c] = (LADSPA_Data*) pa_xnew(uint8_t, (unsigned) u->block_size);
1187 u->output = u->input;
1188 }
1189 /* Initialize plugin instances */
1190 for (h = 0; h < (u->channels / u->max_ladspaport_count); h++) {
1191 if (!(u->handle[h] = d->instantiate(d, ss.rate))) {
1192 pa_log("Failed to instantiate plugin %s with label %s", plugin, d->Label);
1193 goto fail;
1194 }
1195
1196 for (c = 0; c < u->input_count; c++)
1197 d->connect_port(u->handle[h], input_ladspaport[c], u->input[c]);
1198 for (c = 0; c < u->output_count; c++)
1199 d->connect_port(u->handle[h], output_ladspaport[c], u->output[c]);
1200 }
1201
1202 u->n_control = n_control;
1203
1204 if (u->n_control > 0) {
1205 double *control_values;
1206 pa_bool_t *use_default;
1207
1208 /* temporary storage for parser */
1209 control_values = pa_xnew(double, (unsigned) u->n_control);
1210 use_default = pa_xnew(pa_bool_t, (unsigned) u->n_control);
1211
1212 /* real storage */
1213 u->control = pa_xnew(LADSPA_Data, (unsigned) u->n_control);
1214 u->use_default = pa_xnew(pa_bool_t, (unsigned) u->n_control);
1215
1216 if ((parse_control_parameters(u, cdata, control_values, use_default) < 0) ||
1217 (write_control_parameters(u, control_values, use_default) < 0)) {
1218 pa_xfree(control_values);
1219 pa_xfree(use_default);
1220
1221 pa_log("Failed to parse, validate or set control parameters");
1222
1223 goto fail;
1224 }
1225 connect_control_ports(u);
1226 pa_xfree(control_values);
1227 pa_xfree(use_default);
1228 }
1229
1230 if (d->activate)
1231 for (c = 0; c < (u->channels / u->max_ladspaport_count); c++)
1232 d->activate(u->handle[c]);
1233
1234 /* Create sink */
1235 pa_sink_new_data_init(&sink_data);
1236 sink_data.driver = __FILE__;
1237 sink_data.module = m;
1238 if (!(sink_data.name = pa_xstrdup(pa_modargs_get_value(ma, "sink_name", NULL))))
1239 sink_data.name = pa_sprintf_malloc("%s.ladspa", master->name);
1240 pa_sink_new_data_set_sample_spec(&sink_data, &ss);
1241 pa_sink_new_data_set_channel_map(&sink_data, &map);
1242 pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_MASTER_DEVICE, master->name);
1243 pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_CLASS, "filter");
1244 pa_proplist_sets(sink_data.proplist, "device.ladspa.module", plugin);
1245 pa_proplist_sets(sink_data.proplist, "device.ladspa.label", d->Label);
1246 pa_proplist_sets(sink_data.proplist, "device.ladspa.name", d->Name);
1247 pa_proplist_sets(sink_data.proplist, "device.ladspa.maker", d->Maker);
1248 pa_proplist_sets(sink_data.proplist, "device.ladspa.copyright", d->Copyright);
1249 pa_proplist_setf(sink_data.proplist, "device.ladspa.unique_id", "%lu", (unsigned long) d->UniqueID);
1250
1251 if (pa_modargs_get_proplist(ma, "sink_properties", sink_data.proplist, PA_UPDATE_REPLACE) < 0) {
1252 pa_log("Invalid properties");
1253 pa_sink_new_data_done(&sink_data);
1254 goto fail;
1255 }
1256
1257 if ((u->auto_desc = !pa_proplist_contains(sink_data.proplist, PA_PROP_DEVICE_DESCRIPTION))) {
1258 const char *z;
1259
1260 z = pa_proplist_gets(master->proplist, PA_PROP_DEVICE_DESCRIPTION);
1261 pa_proplist_setf(sink_data.proplist, PA_PROP_DEVICE_DESCRIPTION, "LADSPA Plugin %s on %s", d->Name, z ? z : master->name);
1262 }
1263
1264 u->sink = pa_sink_new(m->core, &sink_data,
1265 (master->flags & (PA_SINK_LATENCY|PA_SINK_DYNAMIC_LATENCY)));
1266 pa_sink_new_data_done(&sink_data);
1267
1268 if (!u->sink) {
1269 pa_log("Failed to create sink.");
1270 goto fail;
1271 }
1272
1273 u->sink->parent.process_msg = sink_process_msg_cb;
1274 u->sink->set_state = sink_set_state_cb;
1275 u->sink->update_requested_latency = sink_update_requested_latency_cb;
1276 u->sink->request_rewind = sink_request_rewind_cb;
1277 pa_sink_enable_decibel_volume(u->sink, TRUE);
1278 pa_sink_set_set_volume_callback(u->sink, sink_set_volume_cb);
1279 pa_sink_set_set_mute_callback(u->sink, sink_set_mute_cb);
1280 u->sink->userdata = u;
1281
1282 pa_sink_set_asyncmsgq(u->sink, master->asyncmsgq);
1283
1284 /* Create sink input */
1285 pa_sink_input_new_data_init(&sink_input_data);
1286 sink_input_data.driver = __FILE__;
1287 sink_input_data.module = m;
1288 pa_sink_input_new_data_set_sink(&sink_input_data, master, FALSE);
1289 sink_input_data.origin_sink = u->sink;
1290 pa_proplist_sets(sink_input_data.proplist, PA_PROP_MEDIA_NAME, "LADSPA Stream");
1291 pa_proplist_sets(sink_input_data.proplist, PA_PROP_MEDIA_ROLE, "filter");
1292 pa_sink_input_new_data_set_sample_spec(&sink_input_data, &ss);
1293 pa_sink_input_new_data_set_channel_map(&sink_input_data, &map);
1294
1295 pa_sink_input_new(&u->sink_input, m->core, &sink_input_data);
1296 pa_sink_input_new_data_done(&sink_input_data);
1297
1298 if (!u->sink_input)
1299 goto fail;
1300
1301 u->sink_input->pop = sink_input_pop_cb;
1302 u->sink_input->process_rewind = sink_input_process_rewind_cb;
1303 u->sink_input->update_max_rewind = sink_input_update_max_rewind_cb;
1304 u->sink_input->update_max_request = sink_input_update_max_request_cb;
1305 u->sink_input->update_sink_latency_range = sink_input_update_sink_latency_range_cb;
1306 u->sink_input->update_sink_fixed_latency = sink_input_update_sink_fixed_latency_cb;
1307 u->sink_input->kill = sink_input_kill_cb;
1308 u->sink_input->attach = sink_input_attach_cb;
1309 u->sink_input->detach = sink_input_detach_cb;
1310 u->sink_input->state_change = sink_input_state_change_cb;
1311 u->sink_input->moving = sink_input_moving_cb;
1312 u->sink_input->volume_changed = sink_input_volume_changed_cb;
1313 u->sink_input->mute_changed = sink_input_mute_changed_cb;
1314 u->sink_input->userdata = u;
1315
1316 u->sink->input_to_master = u->sink_input;
1317
1318 pa_sink_put(u->sink);
1319 pa_sink_input_put(u->sink_input);
1320
1321 #ifdef HAVE_DBUS
1322 dbus_init(u);
1323 #endif
1324
1325 pa_modargs_free(ma);
1326
1327 return 0;
1328
1329 fail:
1330 if (ma)
1331 pa_modargs_free(ma);
1332
1333 pa__done(m);
1334
1335 return -1;
1336 }
1337
1338 int pa__get_n_used(pa_module *m) {
1339 struct userdata *u;
1340
1341 pa_assert(m);
1342 pa_assert_se(u = m->userdata);
1343
1344 return pa_sink_linked_by(u->sink);
1345 }
1346
1347 void pa__done(pa_module*m) {
1348 struct userdata *u;
1349 unsigned c;
1350
1351 pa_assert(m);
1352
1353 if (!(u = m->userdata))
1354 return;
1355
1356 /* See comments in sink_input_kill_cb() above regarding
1357 * destruction order! */
1358
1359 #ifdef HAVE_DBUS
1360 dbus_done(u);
1361 #endif
1362
1363 if (u->sink_input)
1364 pa_sink_input_unlink(u->sink_input);
1365
1366 if (u->sink)
1367 pa_sink_unlink(u->sink);
1368
1369 if (u->sink_input)
1370 pa_sink_input_unref(u->sink_input);
1371
1372 if (u->sink)
1373 pa_sink_unref(u->sink);
1374
1375 for (c = 0; c < (u->channels / u->max_ladspaport_count); c++) {
1376 if (u->handle[c]) {
1377 if (u->descriptor->deactivate)
1378 u->descriptor->deactivate(u->handle[c]);
1379 u->descriptor->cleanup(u->handle[c]);
1380 }
1381 }
1382
1383 if (u->output == u->input) {
1384 if (u->input != NULL) {
1385 for (c = 0; c < u->max_ladspaport_count; c++)
1386 pa_xfree(u->input[c]);
1387 pa_xfree(u->input);
1388 }
1389 } else {
1390 if (u->input != NULL) {
1391 for (c = 0; c < u->input_count; c++)
1392 pa_xfree(u->input[c]);
1393 pa_xfree(u->input);
1394 }
1395 if (u->output != NULL) {
1396 for (c = 0; c < u->output_count; c++)
1397 pa_xfree(u->output[c]);
1398 pa_xfree(u->output);
1399 }
1400 }
1401
1402 if (u->memblockq)
1403 pa_memblockq_free(u->memblockq);
1404
1405 pa_xfree(u->control);
1406 pa_xfree(u->use_default);
1407 pa_xfree(u);
1408 }