]> code.delx.au - pulseaudio/blob - src/modules/module-ladspa-sink.c
never hand out more data from a sink input than requested. Otherwise the resampler...
[pulseaudio] / src / modules / module-ladspa-sink.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2008 Lennart Poettering
7
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
12
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 USA.
22 ***/
23
24 /* TODO: Some plugins cause latency, and some even report it by using a control
25 out port. We don't currently use the latency information. */
26
27 #ifdef HAVE_CONFIG_H
28 #include <config.h>
29 #endif
30
31 #include <pulse/xmalloc.h>
32
33 #include <pulsecore/core-error.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/thread.h>
41 #include <pulsecore/thread-mq.h>
42 #include <pulsecore/rtpoll.h>
43 #include <pulsecore/sample-util.h>
44 #include <pulsecore/ltdl-helper.h>
45
46 #include "module-ladspa-sink-symdef.h"
47 #include "ladspa.h"
48
49 PA_MODULE_AUTHOR("Lennart Poettering");
50 PA_MODULE_DESCRIPTION("Virtual LADSPA sink");
51 PA_MODULE_VERSION(PACKAGE_VERSION);
52 PA_MODULE_LOAD_ONCE(FALSE);
53 PA_MODULE_USAGE(
54 "sink_name=<name for the sink> "
55 "master=<name of sink to remap> "
56 "format=<sample format> "
57 "channels=<number of channels> "
58 "rate=<sample rate> "
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 "master",
91 "format",
92 "channels",
93 "rate",
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(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 pa_usec_t usec = 0;
109
110 /* Get the latency of the master sink */
111 if (PA_MSGOBJECT(u->master)->process_msg(PA_MSGOBJECT(u->master), PA_SINK_MESSAGE_GET_LATENCY, &usec, 0, NULL) < 0)
112 usec = 0;
113
114 /* Add the latency internal to our sink input on top */
115 usec += pa_bytes_to_usec(pa_memblockq_get_length(u->sink_input->thread_info.render_memblockq), &u->master->sample_spec);
116
117 *((pa_usec_t*) data) = usec;
118 return 0;
119 }
120 }
121
122 return pa_sink_process_msg(o, code, data, offset, chunk);
123 }
124
125 /* Called from main context */
126 static int sink_set_state(pa_sink *s, pa_sink_state_t state) {
127 struct userdata *u;
128
129 pa_sink_assert_ref(s);
130 pa_assert_se(u = s->userdata);
131
132 if (PA_SINK_IS_LINKED(state) &&
133 u->sink_input &&
134 PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(u->sink_input)))
135
136 pa_sink_input_cork(u->sink_input, state == PA_SINK_SUSPENDED);
137
138 return 0;
139 }
140
141 /* Called from I/O thread context */
142 static void sink_request_rewind(pa_sink *s) {
143 struct userdata *u;
144
145 pa_sink_assert_ref(s);
146 pa_assert_se(u = s->userdata);
147
148 /* Just hand this one over to the master sink */
149 pa_sink_input_request_rewind(u->sink_input, s->thread_info.rewind_nbytes + pa_memblockq_get_length(u->memblockq), TRUE, FALSE);
150 }
151
152 /* Called from I/O thread context */
153 static void sink_update_requested_latency(pa_sink *s) {
154 struct userdata *u;
155
156 pa_sink_assert_ref(s);
157 pa_assert_se(u = s->userdata);
158
159 /* Just hand this one over to the master sink */
160 pa_sink_input_set_requested_latency_within_thread(
161 u->sink_input,
162 pa_sink_get_requested_latency_within_thread(s));
163 }
164
165 /* Called from I/O thread context */
166 static int sink_input_pop_cb(pa_sink_input *i, size_t nbytes, pa_memchunk *chunk) {
167 struct userdata *u;
168 float *src, *dst;
169 size_t fs;
170 unsigned n, c;
171 pa_memchunk tchunk;
172
173 pa_sink_input_assert_ref(i);
174 pa_assert(chunk);
175 pa_assert_se(u = i->userdata);
176
177 if (!u->sink || !PA_SINK_IS_OPENED(u->sink->thread_info.state))
178 return -1;
179
180 while (pa_memblockq_peek(u->memblockq, &tchunk) < 0) {
181 pa_memchunk nchunk;
182
183 pa_sink_render(u->sink, nbytes, &nchunk);
184 pa_memblockq_push(u->memblockq, &nchunk);
185 pa_memblock_unref(nchunk.memblock);
186 }
187
188 tchunk.length = PA_MIN(nbytes, tchunk.length);
189 pa_assert(tchunk.length > 0);
190
191 fs = pa_frame_size(&i->sample_spec);
192 n = PA_MIN(tchunk.length, u->block_size) / fs;
193
194 pa_assert(n > 0);
195
196 chunk->index = 0;
197 chunk->length = n*fs;
198 chunk->memblock = pa_memblock_new(i->sink->core->mempool, chunk->length);
199
200 pa_memblockq_drop(u->memblockq, chunk->length);
201
202 src = (float*) ((uint8_t*) pa_memblock_acquire(tchunk.memblock) + tchunk.index);
203 dst = (float*) pa_memblock_acquire(chunk->memblock);
204
205 for (c = 0; c < u->channels; c++) {
206 pa_sample_clamp(PA_SAMPLE_FLOAT32NE, u->input, sizeof(float), src+c, u->channels*sizeof(float), n);
207 u->descriptor->run(u->handle[c], n);
208 pa_sample_clamp(PA_SAMPLE_FLOAT32NE, dst+c, u->channels*sizeof(float), u->output, sizeof(float), n);
209 }
210
211 pa_memblock_release(tchunk.memblock);
212 pa_memblock_release(chunk->memblock);
213
214 pa_memblock_unref(tchunk.memblock);
215
216 return 0;
217 }
218
219 /* Called from I/O thread context */
220 static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes) {
221 struct userdata *u;
222
223 pa_sink_input_assert_ref(i);
224 pa_assert_se(u = i->userdata);
225 pa_assert(nbytes > 0);
226
227 if (!u->sink || !PA_SINK_IS_OPENED(u->sink->thread_info.state))
228 return;
229
230 if (u->sink->thread_info.rewind_nbytes > 0) {
231 size_t max_rewrite, amount;
232
233 max_rewrite = nbytes + pa_memblockq_get_length(u->memblockq);
234 amount = PA_MIN(u->sink->thread_info.rewind_nbytes, max_rewrite);
235 u->sink->thread_info.rewind_nbytes = 0;
236
237 if (amount > 0) {
238 unsigned c;
239
240 pa_memblockq_seek(u->memblockq, - (int64_t) amount, PA_SEEK_RELATIVE);
241 pa_sink_process_rewind(u->sink, amount);
242
243 pa_log_debug("Resetting plugin");
244
245 /* Reset the plugin */
246 if (u->descriptor->deactivate)
247 for (c = 0; c < u->channels; c++)
248 u->descriptor->deactivate(u->handle[c]);
249 if (u->descriptor->activate)
250 for (c = 0; c < u->channels; c++)
251 u->descriptor->activate(u->handle[c]);
252 }
253 }
254
255 pa_memblockq_rewind(u->memblockq, nbytes);
256 }
257
258 /* Called from I/O thread context */
259 static void sink_input_update_max_rewind_cb(pa_sink_input *i, size_t nbytes) {
260 struct userdata *u;
261
262 pa_sink_input_assert_ref(i);
263 pa_assert_se(u = i->userdata);
264
265 if (!u->sink || !PA_SINK_IS_OPENED(u->sink->thread_info.state))
266 return;
267
268 pa_memblockq_set_maxrewind(u->memblockq, nbytes);
269 pa_sink_set_max_rewind(u->sink, nbytes);
270 }
271
272 /* Called from I/O thread context */
273 static void sink_input_detach_cb(pa_sink_input *i) {
274 struct userdata *u;
275
276 pa_sink_input_assert_ref(i);
277 pa_assert_se(u = i->userdata);
278
279 if (!u->sink || !PA_SINK_IS_OPENED(u->sink->thread_info.state))
280 return;
281
282 pa_sink_detach_within_thread(u->sink);
283 pa_sink_set_asyncmsgq(u->sink, NULL);
284 pa_sink_set_rtpoll(u->sink, NULL);
285 }
286
287 /* Called from I/O thread context */
288 static void sink_input_attach_cb(pa_sink_input *i) {
289 struct userdata *u;
290
291 pa_sink_input_assert_ref(i);
292 pa_assert_se(u = i->userdata);
293
294 if (!u->sink || !PA_SINK_IS_OPENED(u->sink->thread_info.state))
295 return;
296
297 pa_sink_set_asyncmsgq(u->sink, i->sink->asyncmsgq);
298 pa_sink_set_rtpoll(u->sink, i->sink->rtpoll);
299 pa_sink_attach_within_thread(u->sink);
300
301 pa_sink_set_latency_range(u->sink, u->master->min_latency, u->master->max_latency);
302 }
303
304 /* Called from main context */
305 static void sink_input_kill_cb(pa_sink_input *i) {
306 struct userdata *u;
307
308 pa_sink_input_assert_ref(i);
309 pa_assert_se(u = i->userdata);
310
311 pa_sink_unlink(u->sink);
312 pa_sink_input_unlink(u->sink_input);
313
314 pa_sink_unref(u->sink);
315 u->sink = NULL;
316 pa_sink_input_unref(u->sink_input);
317 u->sink_input = NULL;
318
319 pa_module_unload_request(u->module);
320 }
321
322 /* Called from IO thread context */
323 static void sink_input_state_change_cb(pa_sink_input *i, pa_sink_input_state_t state) {
324 struct userdata *u;
325
326 pa_sink_input_assert_ref(i);
327 pa_assert_se(u = i->userdata);
328
329 /* If we are added for the first time, ask for a rewinding so that
330 * we are heard right-away. */
331 if (PA_SINK_INPUT_IS_LINKED(state) &&
332 i->thread_info.state == PA_SINK_INPUT_INIT) {
333 pa_log_debug("Requesting rewind due to state change.");
334 pa_sink_input_request_rewind(i, 0, FALSE, TRUE);
335 }
336 }
337
338 int pa__init(pa_module*m) {
339 struct userdata *u;
340 pa_sample_spec ss;
341 pa_channel_map map;
342 pa_modargs *ma;
343 char *t;
344 const char *z;
345 pa_sink *master;
346 pa_sink_input_new_data sink_input_data;
347 pa_sink_new_data sink_data;
348 const char *plugin, *label;
349 LADSPA_Descriptor_Function descriptor_func;
350 const char *e, *cdata;
351 const LADSPA_Descriptor *d;
352 unsigned long input_port, output_port, p, j, n_control;
353 unsigned c;
354 pa_bool_t *use_default = NULL;
355
356 pa_assert(m);
357
358 pa_assert(sizeof(LADSPA_Data) == sizeof(float));
359
360 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
361 pa_log("Failed to parse module arguments.");
362 goto fail;
363 }
364
365 if (!(master = pa_namereg_get(m->core, pa_modargs_get_value(ma, "master", NULL), PA_NAMEREG_SINK, 1))) {
366 pa_log("Master sink not found");
367 goto fail;
368 }
369
370 ss = master->sample_spec;
371 ss.format = PA_SAMPLE_FLOAT32;
372 map = master->channel_map;
373 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
374 pa_log("Invalid sample format specification or channel map");
375 goto fail;
376 }
377
378 if (!(plugin = pa_modargs_get_value(ma, "plugin", NULL))) {
379 pa_log("Missing LADSPA plugin name");
380 goto fail;
381 }
382
383 if (!(label = pa_modargs_get_value(ma, "label", NULL))) {
384 pa_log("Missing LADSPA plugin label");
385 goto fail;
386 }
387
388 cdata = pa_modargs_get_value(ma, "control", NULL);
389
390 u = pa_xnew0(struct userdata, 1);
391 u->core = m->core;
392 u->module = m;
393 m->userdata = u;
394 u->master = master;
395 u->sink = NULL;
396 u->sink_input = NULL;
397 u->memblockq = pa_memblockq_new(0, MEMBLOCKQ_MAXLENGTH, 0, pa_frame_size(&ss), 1, 1, 0, NULL);
398
399 if (!(e = getenv("LADSPA_PATH")))
400 e = LADSPA_PATH;
401
402 /* FIXME: This is not exactly thread safe */
403 t = pa_xstrdup(lt_dlgetsearchpath());
404 lt_dlsetsearchpath(e);
405 m->dl = lt_dlopenext(plugin);
406 lt_dlsetsearchpath(t);
407 pa_xfree(t);
408
409 if (!m->dl) {
410 pa_log("Failed to load LADSPA plugin: %s", lt_dlerror());
411 goto fail;
412 }
413
414 if (!(descriptor_func = (LADSPA_Descriptor_Function) pa_load_sym(m->dl, NULL, "ladspa_descriptor"))) {
415 pa_log("LADSPA module lacks ladspa_descriptor() symbol.");
416 goto fail;
417 }
418
419 for (j = 0;; j++) {
420
421 if (!(d = descriptor_func(j))) {
422 pa_log("Failed to find plugin label '%s' in plugin '%s'.", label, plugin);
423 goto fail;
424 }
425
426 if (strcmp(d->Label, label) == 0)
427 break;
428 }
429
430 u->descriptor = d;
431
432 pa_log_debug("Module: %s", plugin);
433 pa_log_debug("Label: %s", d->Label);
434 pa_log_debug("Unique ID: %lu", d->UniqueID);
435 pa_log_debug("Name: %s", d->Name);
436 pa_log_debug("Maker: %s", d->Maker);
437 pa_log_debug("Copyright: %s", d->Copyright);
438
439 input_port = output_port = (unsigned long) -1;
440 n_control = 0;
441
442 for (p = 0; p < d->PortCount; p++) {
443
444 if (LADSPA_IS_PORT_INPUT(d->PortDescriptors[p]) && LADSPA_IS_PORT_AUDIO(d->PortDescriptors[p])) {
445
446 if (strcmp(d->PortNames[p], "Input") == 0) {
447 pa_assert(input_port == (unsigned long) -1);
448 input_port = p;
449 } else {
450 pa_log("Found audio input port on plugin we cannot handle: %s", d->PortNames[p]);
451 goto fail;
452 }
453
454 } else if (LADSPA_IS_PORT_OUTPUT(d->PortDescriptors[p]) && LADSPA_IS_PORT_AUDIO(d->PortDescriptors[p])) {
455
456 if (strcmp(d->PortNames[p], "Output") == 0) {
457 pa_assert(output_port == (unsigned long) -1);
458 output_port = p;
459 } else {
460 pa_log("Found audio output port on plugin we cannot handle: %s", d->PortNames[p]);
461 goto fail;
462 }
463
464 } else if (LADSPA_IS_PORT_INPUT(d->PortDescriptors[p]) && LADSPA_IS_PORT_CONTROL(d->PortDescriptors[p]))
465 n_control++;
466 else {
467 pa_assert(LADSPA_IS_PORT_OUTPUT(d->PortDescriptors[p]) && LADSPA_IS_PORT_CONTROL(d->PortDescriptors[p]));
468 pa_log_debug("Ignored control output port \"%s\".", d->PortNames[p]);
469 }
470 }
471
472 if ((input_port == (unsigned long) -1) || (output_port == (unsigned long) -1)) {
473 pa_log("Failed to identify input and output ports. "
474 "Right now this module can only deal with plugins which provide an 'Input' and an 'Output' audio port. "
475 "Patches welcome!");
476 goto fail;
477 }
478
479 u->block_size = pa_frame_align(pa_mempool_block_size_max(m->core->mempool), &ss);
480
481 u->input = (LADSPA_Data*) pa_xnew(uint8_t, u->block_size);
482 if (LADSPA_IS_INPLACE_BROKEN(d->Properties))
483 u->output = (LADSPA_Data*) pa_xnew(uint8_t, u->block_size);
484 else
485 u->output = u->input;
486
487 u->channels = ss.channels;
488
489 for (c = 0; c < ss.channels; c++) {
490 if (!(u->handle[c] = d->instantiate(d, ss.rate))) {
491 pa_log("Failed to instantiate plugin %s with label %s for channel %i", plugin, d->Label, c);
492 goto fail;
493 }
494
495 d->connect_port(u->handle[c], input_port, u->input);
496 d->connect_port(u->handle[c], output_port, u->output);
497 }
498
499 if (!cdata && n_control > 0) {
500 pa_log("This plugin requires specification of %lu control parameters.", n_control);
501 goto fail;
502 }
503
504 if (n_control > 0) {
505 const char *state = NULL;
506 char *k;
507 unsigned long h;
508
509 u->control = pa_xnew(LADSPA_Data, n_control);
510 use_default = pa_xnew(pa_bool_t, n_control);
511 p = 0;
512
513 while ((k = pa_split(cdata, ",", &state)) && p < n_control) {
514 float f;
515
516 if (*k == 0) {
517 use_default[p++] = TRUE;
518 pa_xfree(k);
519 continue;
520 }
521
522 if (pa_atof(k, &f) < 0) {
523 pa_log("Failed to parse control value '%s'", k);
524 pa_xfree(k);
525 goto fail;
526 }
527
528 pa_xfree(k);
529
530 use_default[p] = FALSE;
531 u->control[p++] = f;
532 }
533
534 /* The previous loop doesn't take the last control value into account
535 if it is left empty, so we do it here. */
536 if (*cdata == 0 || cdata[strlen(cdata) - 1] == ',') {
537 if (p < n_control)
538 use_default[p] = TRUE;
539 p++;
540 }
541
542 if (p > n_control || k) {
543 pa_log("Too many control values passed, %lu expected.", n_control);
544 pa_xfree(k);
545 goto fail;
546 }
547
548 if (p < n_control) {
549 pa_log("Not enough control values passed, %lu expected, %lu passed.", n_control, p);
550 goto fail;
551 }
552
553 h = 0;
554 for (p = 0; p < d->PortCount; p++) {
555 LADSPA_PortRangeHintDescriptor hint = d->PortRangeHints[p].HintDescriptor;
556
557 if (!LADSPA_IS_PORT_CONTROL(d->PortDescriptors[p]))
558 continue;
559
560 if (LADSPA_IS_PORT_OUTPUT(d->PortDescriptors[p])) {
561 for (c = 0; c < ss.channels; c++)
562 d->connect_port(u->handle[c], p, &u->control_out);
563 continue;
564 }
565
566 pa_assert(h < n_control);
567
568 if (use_default[h]) {
569 LADSPA_Data lower, upper;
570
571 if (!LADSPA_IS_HINT_HAS_DEFAULT(hint)) {
572 pa_log("Control port value left empty but plugin defines no default.");
573 goto fail;
574 }
575
576 lower = d->PortRangeHints[p].LowerBound;
577 upper = d->PortRangeHints[p].UpperBound;
578
579 if (LADSPA_IS_HINT_SAMPLE_RATE(hint)) {
580 lower *= ss.rate;
581 upper *= ss.rate;
582 }
583
584 switch (hint & LADSPA_HINT_DEFAULT_MASK) {
585
586 case LADSPA_HINT_DEFAULT_MINIMUM:
587 u->control[h] = lower;
588 break;
589
590 case LADSPA_HINT_DEFAULT_MAXIMUM:
591 u->control[h] = upper;
592 break;
593
594 case LADSPA_HINT_DEFAULT_LOW:
595 if (LADSPA_IS_HINT_LOGARITHMIC(hint))
596 u->control[h] = exp(log(lower) * 0.75 + log(upper) * 0.25);
597 else
598 u->control[h] = lower * 0.75 + upper * 0.25;
599 break;
600
601 case LADSPA_HINT_DEFAULT_MIDDLE:
602 if (LADSPA_IS_HINT_LOGARITHMIC(hint))
603 u->control[h] = exp(log(lower) * 0.5 + log(upper) * 0.5);
604 else
605 u->control[h] = lower * 0.5 + upper * 0.5;
606 break;
607
608 case LADSPA_HINT_DEFAULT_HIGH:
609 if (LADSPA_IS_HINT_LOGARITHMIC(hint))
610 u->control[h] = exp(log(lower) * 0.25 + log(upper) * 0.75);
611 else
612 u->control[h] = lower * 0.25 + upper * 0.75;
613 break;
614
615 case LADSPA_HINT_DEFAULT_0:
616 u->control[h] = 0;
617 break;
618
619 case LADSPA_HINT_DEFAULT_1:
620 u->control[h] = 1;
621 break;
622
623 case LADSPA_HINT_DEFAULT_100:
624 u->control[h] = 100;
625 break;
626
627 case LADSPA_HINT_DEFAULT_440:
628 u->control[h] = 440;
629 break;
630
631 default:
632 pa_assert_not_reached();
633 }
634 }
635
636 if (LADSPA_IS_HINT_INTEGER(hint))
637 u->control[h] = roundf(u->control[h]);
638
639 pa_log_debug("Binding %f to port %s", u->control[h], d->PortNames[p]);
640
641 for (c = 0; c < ss.channels; c++)
642 d->connect_port(u->handle[c], p, &u->control[h]);
643
644 h++;
645 }
646
647 pa_assert(h == n_control);
648 }
649
650 if (d->activate)
651 for (c = 0; c < u->channels; c++)
652 d->activate(u->handle[c]);
653
654 /* Create sink */
655 pa_sink_new_data_init(&sink_data);
656 sink_data.driver = __FILE__;
657 sink_data.module = m;
658 if (!(sink_data.name = pa_xstrdup(pa_modargs_get_value(ma, "sink_name", NULL))))
659 sink_data.name = pa_sprintf_malloc("%s.ladspa", master->name);
660 sink_data.namereg_fail = FALSE;
661 pa_sink_new_data_set_sample_spec(&sink_data, &ss);
662 pa_sink_new_data_set_channel_map(&sink_data, &map);
663 z = pa_proplist_gets(master->proplist, PA_PROP_DEVICE_DESCRIPTION);
664 pa_proplist_setf(sink_data.proplist, PA_PROP_DEVICE_DESCRIPTION, "LADSPA Plugin %s on %s", label, z ? z : master->name);
665 pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_MASTER_DEVICE, master->name);
666 pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_CLASS, "filter");
667 pa_proplist_sets(sink_data.proplist, "device.ladspa.module", plugin);
668 pa_proplist_sets(sink_data.proplist, "device.ladspa.label", d->Label);
669 pa_proplist_sets(sink_data.proplist, "device.ladspa.name", d->Name);
670 pa_proplist_sets(sink_data.proplist, "device.ladspa.maker", d->Maker);
671 pa_proplist_sets(sink_data.proplist, "device.ladspa.copyright", d->Copyright);
672 pa_proplist_setf(sink_data.proplist, "device.ladspa.unique_id", "%lu", (unsigned long) d->UniqueID);
673
674 u->sink = pa_sink_new(m->core, &sink_data, PA_SINK_LATENCY);
675 pa_sink_new_data_done(&sink_data);
676
677 if (!u->sink) {
678 pa_log("Failed to create sink.");
679 goto fail;
680 }
681
682 u->sink->parent.process_msg = sink_process_msg;
683 u->sink->set_state = sink_set_state;
684 u->sink->update_requested_latency = sink_update_requested_latency;
685 u->sink->request_rewind = sink_request_rewind;
686 u->sink->userdata = u;
687
688 pa_sink_set_asyncmsgq(u->sink, master->asyncmsgq);
689 pa_sink_set_rtpoll(u->sink, master->rtpoll);
690
691 /* Create sink input */
692 pa_sink_input_new_data_init(&sink_input_data);
693 sink_input_data.driver = __FILE__;
694 sink_input_data.module = m;
695 sink_input_data.sink = u->master;
696 pa_proplist_sets(sink_input_data.proplist, PA_PROP_MEDIA_NAME, "LADSPA Stream");
697 pa_proplist_sets(sink_input_data.proplist, PA_PROP_MEDIA_ROLE, "filter");
698 pa_sink_input_new_data_set_sample_spec(&sink_input_data, &ss);
699 pa_sink_input_new_data_set_channel_map(&sink_input_data, &map);
700
701 u->sink_input = pa_sink_input_new(m->core, &sink_input_data, PA_SINK_INPUT_DONT_MOVE);
702 pa_sink_input_new_data_done(&sink_input_data);
703
704 if (!u->sink_input)
705 goto fail;
706
707 u->sink_input->pop = sink_input_pop_cb;
708 u->sink_input->process_rewind = sink_input_process_rewind_cb;
709 u->sink_input->update_max_rewind = sink_input_update_max_rewind_cb;
710 u->sink_input->kill = sink_input_kill_cb;
711 u->sink_input->attach = sink_input_attach_cb;
712 u->sink_input->detach = sink_input_detach_cb;
713 u->sink_input->state_change = sink_input_state_change_cb;
714 u->sink_input->userdata = u;
715
716 pa_sink_put(u->sink);
717 pa_sink_input_put(u->sink_input);
718
719 pa_modargs_free(ma);
720
721 pa_xfree(use_default);
722
723 return 0;
724
725 fail:
726 if (ma)
727 pa_modargs_free(ma);
728
729 pa_xfree(use_default);
730
731 pa__done(m);
732
733 return -1;
734 }
735
736 void pa__done(pa_module*m) {
737 struct userdata *u;
738 unsigned c;
739
740 pa_assert(m);
741
742 if (!(u = m->userdata))
743 return;
744
745 if (u->sink) {
746 pa_sink_unlink(u->sink);
747 pa_sink_unref(u->sink);
748 }
749
750 if (u->sink_input) {
751 pa_sink_input_unlink(u->sink_input);
752 pa_sink_input_unref(u->sink_input);
753 }
754
755 for (c = 0; c < u->channels; c++)
756 if (u->handle[c]) {
757 if (u->descriptor->deactivate)
758 u->descriptor->deactivate(u->handle[c]);
759 u->descriptor->cleanup(u->handle[c]);
760 }
761
762 if (u->output != u->input)
763 pa_xfree(u->output);
764
765 if (u->memblockq)
766 pa_memblockq_free(u->memblockq);
767
768 pa_xfree(u->input);
769
770 pa_xfree(u->control);
771
772 pa_xfree(u);
773 }