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