virtual-sink: Add a modarg for enabling volume sharing.
[profile/ivi/pulseaudio-panda.git] / src / modules / module-virtual-sink.c
1 /***
2     This file is part of PulseAudio.
3
4     Copyright 2010 Intel Corporation
5     Contributor: Pierre-Louis Bossart <pierre-louis.bossart@intel.com>
6
7     PulseAudio is free software; you can redistribute it and/or modify
8     it under the terms of the GNU Lesser General Public License as published
9     by the Free Software Foundation; either version 2.1 of the License,
10     or (at your option) any later version.
11
12     PulseAudio is distributed in the hope that it will be useful, but
13     WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15     General Public License for more details.
16
17     You should have received a copy of the GNU Lesser General Public License
18     along with PulseAudio; if not, write to the Free Software
19     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20     USA.
21 ***/
22
23 /* TODO: Some plugins cause latency, and some even report it by using a control
24    out port. We don't currently use the latency information. */
25
26 #ifdef HAVE_CONFIG_H
27 #include <config.h>
28 #endif
29
30 #include <pulse/xmalloc.h>
31 #include <pulse/i18n.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-virtual-sink-symdef.h"
47
48 PA_MODULE_AUTHOR("Pierre-Louis Bossart");
49 PA_MODULE_DESCRIPTION(_("Virtual 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           "use_volume_sharing=<yes or no> "
61         ));
62
63 #define MEMBLOCKQ_MAXLENGTH (16*1024*1024)
64
65 struct userdata {
66     pa_module *module;
67
68     pa_sink *sink;
69     pa_sink_input *sink_input;
70
71     pa_memblockq *memblockq;
72
73     pa_bool_t auto_desc;
74     unsigned channels;
75 };
76
77 static const char* const valid_modargs[] = {
78     "sink_name",
79     "sink_properties",
80     "master",
81     "format",
82     "rate",
83     "channels",
84     "channel_map",
85     "use_volume_sharing",
86     NULL
87 };
88
89 /* Called from I/O thread context */
90 static int sink_process_msg_cb(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
91     struct userdata *u = PA_SINK(o)->userdata;
92
93     switch (code) {
94
95         case PA_SINK_MESSAGE_GET_LATENCY:
96
97             /* The sink is _put() before the sink input is, so let's
98              * make sure we don't access it in that time. Also, the
99              * sink input is first shut down, the sink second. */
100             if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
101                 !PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state)) {
102                 *((pa_usec_t*) data) = 0;
103                 return 0;
104             }
105
106             *((pa_usec_t*) data) =
107
108                 /* Get the latency of the master sink */
109                 pa_sink_get_latency_within_thread(u->sink_input->sink) +
110
111                 /* Add the latency internal to our sink input on top */
112                 pa_bytes_to_usec(pa_memblockq_get_length(u->sink_input->thread_info.render_memblockq), &u->sink_input->sink->sample_spec);
113
114             return 0;
115     }
116
117     return pa_sink_process_msg(o, code, data, offset, chunk);
118 }
119
120 /* Called from main context */
121 static int sink_set_state_cb(pa_sink *s, pa_sink_state_t state) {
122     struct userdata *u;
123
124     pa_sink_assert_ref(s);
125     pa_assert_se(u = s->userdata);
126
127     if (!PA_SINK_IS_LINKED(state) ||
128         !PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(u->sink_input)))
129         return 0;
130
131     pa_sink_input_cork(u->sink_input, state == PA_SINK_SUSPENDED);
132     return 0;
133 }
134
135 /* Called from I/O thread context */
136 static void sink_request_rewind_cb(pa_sink *s) {
137     struct userdata *u;
138
139     pa_sink_assert_ref(s);
140     pa_assert_se(u = s->userdata);
141
142     if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
143         !PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state))
144         return;
145
146     /* Just hand this one over to the master sink */
147     pa_sink_input_request_rewind(u->sink_input,
148                                  s->thread_info.rewind_nbytes +
149                                  pa_memblockq_get_length(u->memblockq), TRUE, FALSE, FALSE);
150 }
151
152 /* Called from I/O thread context */
153 static void sink_update_requested_latency_cb(pa_sink *s) {
154     struct userdata *u;
155
156     pa_sink_assert_ref(s);
157     pa_assert_se(u = s->userdata);
158
159     if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
160         !PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state))
161         return;
162
163     /* Just hand this one over to the master sink */
164     pa_sink_input_set_requested_latency_within_thread(
165             u->sink_input,
166             pa_sink_get_requested_latency_within_thread(s));
167 }
168
169 /* Called from main context */
170 static void sink_set_volume_cb(pa_sink *s) {
171     struct userdata *u;
172
173     pa_sink_assert_ref(s);
174     pa_assert_se(u = s->userdata);
175
176     if (!PA_SINK_IS_LINKED(pa_sink_get_state(s)) ||
177         !PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(u->sink_input)))
178         return;
179
180     pa_sink_input_set_volume(u->sink_input, &s->real_volume, s->save_volume, TRUE);
181 }
182
183 /* Called from main context */
184 static void sink_set_mute_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_mute(u->sink_input, s->muted, s->save_muted);
195 }
196
197 /* Called from I/O thread context */
198 static int sink_input_pop_cb(pa_sink_input *i, size_t nbytes, pa_memchunk *chunk) {
199     struct userdata *u;
200     float *src, *dst;
201     size_t fs;
202     unsigned n, c;
203     pa_memchunk tchunk;
204     pa_usec_t current_latency;
205
206     pa_sink_input_assert_ref(i);
207     pa_assert(chunk);
208     pa_assert_se(u = i->userdata);
209
210     /* Hmm, process any rewind request that might be queued up */
211     pa_sink_process_rewind(u->sink, 0);
212
213     /* (1) IF YOU NEED A FIXED BLOCK SIZE USE
214      * pa_memblockq_peek_fixed_size() HERE INSTEAD. NOTE THAT FILTERS
215      * WHICH CAN DEAL WITH DYNAMIC BLOCK SIZES ARE HIGHLY
216      * PREFERRED. */
217     while (pa_memblockq_peek(u->memblockq, &tchunk) < 0) {
218         pa_memchunk nchunk;
219
220         pa_sink_render(u->sink, nbytes, &nchunk);
221         pa_memblockq_push(u->memblockq, &nchunk);
222         pa_memblock_unref(nchunk.memblock);
223     }
224
225     /* (2) IF YOU NEED A FIXED BLOCK SIZE, THIS NEXT LINE IS NOT
226      * NECESSARY */
227     tchunk.length = PA_MIN(nbytes, tchunk.length);
228     pa_assert(tchunk.length > 0);
229
230     fs = pa_frame_size(&i->sample_spec);
231     n = (unsigned) (tchunk.length / fs);
232
233     pa_assert(n > 0);
234
235     chunk->index = 0;
236     chunk->length = n*fs;
237     chunk->memblock = pa_memblock_new(i->sink->core->mempool, chunk->length);
238
239     pa_memblockq_drop(u->memblockq, chunk->length);
240
241     src = (float*) ((uint8_t*) pa_memblock_acquire(tchunk.memblock) + tchunk.index);
242     dst = (float*) pa_memblock_acquire(chunk->memblock);
243
244     /* (3) PUT YOUR CODE HERE TO DO SOMETHING WITH THE DATA */
245
246     /* As an example, copy input to output */
247     for (c = 0; c < u->channels; c++) {
248         pa_sample_clamp(PA_SAMPLE_FLOAT32NE,
249                         dst+c, u->channels * sizeof(float),
250                         src+c, u->channels * sizeof(float),
251                         n);
252     }
253
254     pa_memblock_release(tchunk.memblock);
255     pa_memblock_release(chunk->memblock);
256
257     pa_memblock_unref(tchunk.memblock);
258
259     /* (4) IF YOU NEED THE LATENCY FOR SOMETHING ACQUIRE IT LIKE THIS: */
260     current_latency =
261         /* Get the latency of the master sink */
262         pa_sink_get_latency_within_thread(i->sink) +
263
264         /* Add the latency internal to our sink input on top */
265         pa_bytes_to_usec(pa_memblockq_get_length(i->thread_info.render_memblockq), &i->sink->sample_spec);
266
267     return 0;
268 }
269
270 /* Called from I/O thread context */
271 static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes) {
272     struct userdata *u;
273     size_t amount = 0;
274
275     pa_sink_input_assert_ref(i);
276     pa_assert_se(u = i->userdata);
277
278     if (u->sink->thread_info.rewind_nbytes > 0) {
279         size_t max_rewrite;
280
281         max_rewrite = nbytes + pa_memblockq_get_length(u->memblockq);
282         amount = PA_MIN(u->sink->thread_info.rewind_nbytes, max_rewrite);
283         u->sink->thread_info.rewind_nbytes = 0;
284
285         if (amount > 0) {
286             pa_memblockq_seek(u->memblockq, - (int64_t) amount, PA_SEEK_RELATIVE, TRUE);
287
288             /* (5) PUT YOUR CODE HERE TO RESET YOUR FILTER  */
289         }
290     }
291
292     pa_sink_process_rewind(u->sink, amount);
293     pa_memblockq_rewind(u->memblockq, nbytes);
294 }
295
296 /* Called from I/O thread context */
297 static void sink_input_update_max_rewind_cb(pa_sink_input *i, size_t nbytes) {
298     struct userdata *u;
299
300     pa_sink_input_assert_ref(i);
301     pa_assert_se(u = i->userdata);
302
303     pa_memblockq_set_maxrewind(u->memblockq, nbytes);
304     pa_sink_set_max_rewind_within_thread(u->sink, nbytes);
305 }
306
307 /* Called from I/O thread context */
308 static void sink_input_update_max_request_cb(pa_sink_input *i, size_t nbytes) {
309     struct userdata *u;
310
311     pa_sink_input_assert_ref(i);
312     pa_assert_se(u = i->userdata);
313
314     /* (6) IF YOU NEED A FIXED BLOCK SIZE ROUND nbytes UP TO MULTIPLES
315      * OF IT HERE. THE PA_ROUND_UP MACRO IS USEFUL FOR THAT. */
316
317     pa_sink_set_max_request_within_thread(u->sink, nbytes);
318 }
319
320 /* Called from I/O thread context */
321 static void sink_input_update_sink_latency_range_cb(pa_sink_input *i) {
322     struct userdata *u;
323
324     pa_sink_input_assert_ref(i);
325     pa_assert_se(u = i->userdata);
326
327     pa_sink_set_latency_range_within_thread(u->sink, i->sink->thread_info.min_latency, i->sink->thread_info.max_latency);
328 }
329
330 /* Called from I/O thread context */
331 static void sink_input_update_sink_fixed_latency_cb(pa_sink_input *i) {
332     struct userdata *u;
333
334     pa_sink_input_assert_ref(i);
335     pa_assert_se(u = i->userdata);
336
337     /* (7) IF YOU NEED A FIXED BLOCK SIZE ADD THE LATENCY FOR ONE
338      * BLOCK MINUS ONE SAMPLE HERE. pa_usec_to_bytes_round_up() IS
339      * USEFUL FOR THAT. */
340
341     pa_sink_set_fixed_latency_within_thread(u->sink, i->sink->thread_info.fixed_latency);
342 }
343
344 /* Called from I/O thread context */
345 static void sink_input_detach_cb(pa_sink_input *i) {
346     struct userdata *u;
347
348     pa_sink_input_assert_ref(i);
349     pa_assert_se(u = i->userdata);
350
351     pa_sink_detach_within_thread(u->sink);
352
353     pa_sink_set_rtpoll(u->sink, NULL);
354 }
355
356 /* Called from I/O thread context */
357 static void sink_input_attach_cb(pa_sink_input *i) {
358     struct userdata *u;
359
360     pa_sink_input_assert_ref(i);
361     pa_assert_se(u = i->userdata);
362
363     pa_sink_set_rtpoll(u->sink, i->sink->thread_info.rtpoll);
364     pa_sink_set_latency_range_within_thread(u->sink, i->sink->thread_info.min_latency, i->sink->thread_info.max_latency);
365
366     /* (8.1) IF YOU NEED A FIXED BLOCK SIZE ADD THE LATENCY FOR ONE
367      * BLOCK MINUS ONE SAMPLE HERE. SEE (7) */
368     pa_sink_set_fixed_latency_within_thread(u->sink, i->sink->thread_info.fixed_latency);
369
370     /* (8.2) IF YOU NEED A FIXED BLOCK SIZE ROUND
371      * pa_sink_input_get_max_request(i) UP TO MULTIPLES OF IT
372      * HERE. SEE (6) */
373     pa_sink_set_max_request_within_thread(u->sink, pa_sink_input_get_max_request(i));
374     pa_sink_set_max_rewind_within_thread(u->sink, pa_sink_input_get_max_rewind(i));
375
376     pa_sink_attach_within_thread(u->sink);
377 }
378
379 /* Called from main context */
380 static void sink_input_kill_cb(pa_sink_input *i) {
381     struct userdata *u;
382
383     pa_sink_input_assert_ref(i);
384     pa_assert_se(u = i->userdata);
385
386     /* The order here matters! We first kill the sink input, followed
387      * by the sink. That means the sink callbacks must be protected
388      * against an unconnected sink input! */
389     pa_sink_input_unlink(u->sink_input);
390     pa_sink_unlink(u->sink);
391
392     pa_sink_input_unref(u->sink_input);
393     u->sink_input = NULL;
394
395     pa_sink_unref(u->sink);
396     u->sink = NULL;
397
398     pa_module_unload_request(u->module, TRUE);
399 }
400
401 /* Called from IO thread context */
402 static void sink_input_state_change_cb(pa_sink_input *i, pa_sink_input_state_t state) {
403     struct userdata *u;
404
405     pa_sink_input_assert_ref(i);
406     pa_assert_se(u = i->userdata);
407
408     /* If we are added for the first time, ask for a rewinding so that
409      * we are heard right-away. */
410     if (PA_SINK_INPUT_IS_LINKED(state) &&
411         i->thread_info.state == PA_SINK_INPUT_INIT) {
412         pa_log_debug("Requesting rewind due to state change.");
413         pa_sink_input_request_rewind(i, 0, FALSE, TRUE, TRUE);
414     }
415 }
416
417 /* Called from main context */
418 static pa_bool_t sink_input_may_move_to_cb(pa_sink_input *i, pa_sink *dest) {
419     struct userdata *u;
420
421     pa_sink_input_assert_ref(i);
422     pa_assert_se(u = i->userdata);
423
424     return u->sink != dest;
425 }
426
427 /* Called from main context */
428 static void sink_input_moving_cb(pa_sink_input *i, pa_sink *dest) {
429     struct userdata *u;
430
431     pa_sink_input_assert_ref(i);
432     pa_assert_se(u = i->userdata);
433
434     if (dest) {
435         pa_sink_set_asyncmsgq(u->sink, dest->asyncmsgq);
436         pa_sink_update_flags(u->sink, PA_SINK_LATENCY|PA_SINK_DYNAMIC_LATENCY, dest->flags);
437     } else
438         pa_sink_set_asyncmsgq(u->sink, NULL);
439
440     if (u->auto_desc && dest) {
441         const char *z;
442         pa_proplist *pl;
443
444         pl = pa_proplist_new();
445         z = pa_proplist_gets(dest->proplist, PA_PROP_DEVICE_DESCRIPTION);
446         pa_proplist_setf(pl, PA_PROP_DEVICE_DESCRIPTION, "Virtual Sink %s on %s",
447                          pa_proplist_gets(u->sink->proplist, "device.vsink.name"), z ? z : dest->name);
448
449         pa_sink_update_proplist(u->sink, PA_UPDATE_REPLACE, pl);
450         pa_proplist_free(pl);
451     }
452 }
453
454 /* Called from main context */
455 static void sink_input_volume_changed_cb(pa_sink_input *i) {
456     struct userdata *u;
457
458     pa_sink_input_assert_ref(i);
459     pa_assert_se(u = i->userdata);
460
461     pa_sink_volume_changed(u->sink, &i->volume);
462 }
463
464 /* Called from main context */
465 static void sink_input_mute_changed_cb(pa_sink_input *i) {
466     struct userdata *u;
467
468     pa_sink_input_assert_ref(i);
469     pa_assert_se(u = i->userdata);
470
471     pa_sink_mute_changed(u->sink, i->muted);
472 }
473
474 int pa__init(pa_module*m) {
475     struct userdata *u;
476     pa_sample_spec ss;
477     pa_channel_map map;
478     pa_modargs *ma;
479     pa_sink *master=NULL;
480     pa_sink_input_new_data sink_input_data;
481     pa_sink_new_data sink_data;
482     pa_bool_t *use_default = NULL;
483     pa_bool_t use_volume_sharing = FALSE;
484
485     pa_assert(m);
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     pa_assert(master);
498
499     ss = master->sample_spec;
500     ss.format = PA_SAMPLE_FLOAT32;
501     map = master->channel_map;
502     if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
503         pa_log("Invalid sample format specification or channel map");
504         goto fail;
505     }
506
507     if (pa_modargs_get_value_boolean(ma, "use_volume_sharing", &use_volume_sharing) < 0) {
508         pa_log("use_volume_sharing= expects a boolean argument");
509         goto fail;
510     }
511
512     u = pa_xnew0(struct userdata, 1);
513     u->module = m;
514     m->userdata = u;
515     u->channels = ss.channels;
516
517     /* Create sink */
518     pa_sink_new_data_init(&sink_data);
519     sink_data.driver = __FILE__;
520     sink_data.module = m;
521     if (!(sink_data.name = pa_xstrdup(pa_modargs_get_value(ma, "sink_name", NULL))))
522         sink_data.name = pa_sprintf_malloc("%s.vsink", master->name);
523     pa_sink_new_data_set_sample_spec(&sink_data, &ss);
524     pa_sink_new_data_set_channel_map(&sink_data, &map);
525     pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_MASTER_DEVICE, master->name);
526     pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_CLASS, "filter");
527     pa_proplist_sets(sink_data.proplist, "device.vsink.name", sink_data.name);
528
529     if (pa_modargs_get_proplist(ma, "sink_properties", sink_data.proplist, PA_UPDATE_REPLACE) < 0) {
530         pa_log("Invalid properties");
531         pa_sink_new_data_done(&sink_data);
532         goto fail;
533     }
534
535     if ((u->auto_desc = !pa_proplist_contains(sink_data.proplist, PA_PROP_DEVICE_DESCRIPTION))) {
536         const char *z;
537
538         z = pa_proplist_gets(master->proplist, PA_PROP_DEVICE_DESCRIPTION);
539         pa_proplist_setf(sink_data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Virtual Sink %s on %s", sink_data.name, z ? z : master->name);
540     }
541
542     u->sink = pa_sink_new(m->core, &sink_data, (master->flags & (PA_SINK_LATENCY|PA_SINK_DYNAMIC_LATENCY))
543                                                | (use_volume_sharing ? PA_SINK_SHARE_VOLUME_WITH_MASTER : 0));
544     pa_sink_new_data_done(&sink_data);
545
546     if (!u->sink) {
547         pa_log("Failed to create sink.");
548         goto fail;
549     }
550
551     u->sink->parent.process_msg = sink_process_msg_cb;
552     u->sink->set_state = sink_set_state_cb;
553     u->sink->update_requested_latency = sink_update_requested_latency_cb;
554     u->sink->request_rewind = sink_request_rewind_cb;
555     u->sink->set_volume = use_volume_sharing ? NULL : sink_set_volume_cb;
556     u->sink->set_mute = sink_set_mute_cb;
557     u->sink->userdata = u;
558
559     pa_sink_set_asyncmsgq(u->sink, master->asyncmsgq);
560
561     /* Create sink input */
562     pa_sink_input_new_data_init(&sink_input_data);
563     sink_input_data.driver = __FILE__;
564     sink_input_data.module = m;
565     sink_input_data.sink = master;
566     sink_input_data.origin_sink = u->sink;
567     pa_proplist_sets(sink_input_data.proplist, PA_PROP_MEDIA_NAME, "Virtual Sink Stream");
568     pa_proplist_sets(sink_input_data.proplist, PA_PROP_MEDIA_ROLE, "filter");
569     pa_sink_input_new_data_set_sample_spec(&sink_input_data, &ss);
570     pa_sink_input_new_data_set_channel_map(&sink_input_data, &map);
571
572     pa_sink_input_new(&u->sink_input, m->core, &sink_input_data);
573     pa_sink_input_new_data_done(&sink_input_data);
574
575     if (!u->sink_input)
576         goto fail;
577
578     u->sink_input->pop = sink_input_pop_cb;
579     u->sink_input->process_rewind = sink_input_process_rewind_cb;
580     u->sink_input->update_max_rewind = sink_input_update_max_rewind_cb;
581     u->sink_input->update_max_request = sink_input_update_max_request_cb;
582     u->sink_input->update_sink_latency_range = sink_input_update_sink_latency_range_cb;
583     u->sink_input->update_sink_fixed_latency = sink_input_update_sink_fixed_latency_cb;
584     u->sink_input->kill = sink_input_kill_cb;
585     u->sink_input->attach = sink_input_attach_cb;
586     u->sink_input->detach = sink_input_detach_cb;
587     u->sink_input->state_change = sink_input_state_change_cb;
588     u->sink_input->may_move_to = sink_input_may_move_to_cb;
589     u->sink_input->moving = sink_input_moving_cb;
590     u->sink_input->volume_changed = use_volume_sharing ? NULL : sink_input_volume_changed_cb;
591     u->sink_input->mute_changed = sink_input_mute_changed_cb;
592     u->sink_input->userdata = u;
593
594     u->sink->input_to_master = u->sink_input;
595
596     /* (9) IF YOU REQUIRE A FIXED BLOCK SIZE MAKE SURE TO PASS A
597      * SILENCE MEMBLOCK AS LAST PARAMETER
598      * HERE. pa_sink_input_get_silence() IS USEFUL HERE. */
599     u->memblockq = pa_memblockq_new(0, MEMBLOCKQ_MAXLENGTH, 0, pa_frame_size(&ss), 1, 1, 0, NULL);
600
601     /* (10) INITIALIZE ANYTHING ELSE YOU NEED HERE */
602
603     pa_sink_put(u->sink);
604     pa_sink_input_put(u->sink_input);
605
606     pa_modargs_free(ma);
607
608     pa_xfree(use_default);
609
610     return 0;
611
612  fail:
613     if (ma)
614         pa_modargs_free(ma);
615
616     pa_xfree(use_default);
617
618     pa__done(m);
619
620     return -1;
621 }
622
623 int pa__get_n_used(pa_module *m) {
624     struct userdata *u;
625
626     pa_assert(m);
627     pa_assert_se(u = m->userdata);
628
629     return pa_sink_linked_by(u->sink);
630 }
631
632 void pa__done(pa_module*m) {
633     struct userdata *u;
634
635     pa_assert(m);
636
637     if (!(u = m->userdata))
638         return;
639
640     /* See comments in sink_input_kill_cb() above regarding
641      * destruction order! */
642
643     if (u->sink_input)
644         pa_sink_input_unlink(u->sink_input);
645
646     if (u->sink)
647         pa_sink_unlink(u->sink);
648
649     if (u->sink_input)
650         pa_sink_input_unref(u->sink_input);
651
652     if (u->sink)
653         pa_sink_unref(u->sink);
654
655     if (u->memblockq)
656         pa_memblockq_free(u->memblockq);
657
658     pa_xfree(u);
659 }