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