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