]> code.delx.au - pulseaudio/blob - src/modules/module-combine.c
01d7a4ecb756a9153c3ffe2a3ec5e6a6a5d8eb0c
[pulseaudio] / src / modules / module-combine.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 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <errno.h>
28
29 #include <pulse/rtclock.h>
30 #include <pulse/timeval.h>
31 #include <pulse/xmalloc.h>
32
33 #include <pulsecore/macro.h>
34 #include <pulsecore/module.h>
35 #include <pulsecore/llist.h>
36 #include <pulsecore/sink.h>
37 #include <pulsecore/sink-input.h>
38 #include <pulsecore/memblockq.h>
39 #include <pulsecore/log.h>
40 #include <pulsecore/core-rtclock.h>
41 #include <pulsecore/core-util.h>
42 #include <pulsecore/modargs.h>
43 #include <pulsecore/namereg.h>
44 #include <pulsecore/mutex.h>
45 #include <pulsecore/thread.h>
46 #include <pulsecore/thread-mq.h>
47 #include <pulsecore/rtpoll.h>
48 #include <pulsecore/core-error.h>
49 #include <pulsecore/time-smoother.h>
50 #include <pulsecore/strlist.h>
51
52 #include "module-combine-symdef.h"
53
54 PA_MODULE_AUTHOR("Lennart Poettering");
55 PA_MODULE_DESCRIPTION("Combine multiple sinks to one");
56 PA_MODULE_VERSION(PACKAGE_VERSION);
57 PA_MODULE_LOAD_ONCE(FALSE);
58 PA_MODULE_USAGE(
59 "sink_name=<name for the sink> "
60 "sink_properties=<properties for the sink> "
61 "slaves=<slave sinks> "
62 "adjust_time=<how often to readjust rates in s> "
63 "resample_method=<method> "
64 "format=<sample format> "
65 "rate=<sample rate> "
66 "channels=<number of channels> "
67 "channel_map=<channel map>");
68
69 #define DEFAULT_SINK_NAME "combined"
70
71 #define MEMBLOCKQ_MAXLENGTH (1024*1024*16)
72
73 #define DEFAULT_ADJUST_TIME_USEC (10*PA_USEC_PER_SEC)
74
75 #define BLOCK_USEC (PA_USEC_PER_MSEC * 200)
76
77 static const char* const valid_modargs[] = {
78 "sink_name",
79 "sink_properties",
80 "slaves",
81 "adjust_time",
82 "resample_method",
83 "format",
84 "rate",
85 "channels",
86 "channel_map",
87 NULL
88 };
89
90 struct output {
91 struct userdata *userdata;
92
93 pa_sink *sink;
94 pa_sink_input *sink_input;
95 pa_bool_t ignore_state_change;
96
97 pa_asyncmsgq *inq, /* Message queue from the sink thread to this sink input */
98 *outq; /* Message queue from this sink input to the sink thread */
99 pa_rtpoll_item *inq_rtpoll_item_read, *inq_rtpoll_item_write;
100 pa_rtpoll_item *outq_rtpoll_item_read, *outq_rtpoll_item_write;
101
102 pa_memblockq *memblockq;
103
104 /* For communication of the stream latencies to the main thread */
105 pa_usec_t total_latency;
106
107 /* For coomunication of the stream parameters to the sink thread */
108 pa_atomic_t max_request;
109 pa_atomic_t requested_latency;
110
111 PA_LLIST_FIELDS(struct output);
112 };
113
114 struct userdata {
115 pa_core *core;
116 pa_module *module;
117 pa_sink *sink;
118
119 pa_thread *thread;
120 pa_thread_mq thread_mq;
121 pa_rtpoll *rtpoll;
122
123 pa_time_event *time_event;
124 pa_usec_t adjust_time;
125
126 pa_bool_t automatic;
127 pa_bool_t auto_desc;
128
129 pa_strlist *unlinked_slaves;
130
131 pa_hook_slot *sink_put_slot, *sink_unlink_slot, *sink_state_changed_slot;
132
133 pa_resample_method_t resample_method;
134
135 pa_usec_t block_usec;
136
137 pa_idxset* outputs; /* managed in main context */
138
139 struct {
140 PA_LLIST_HEAD(struct output, active_outputs); /* managed in IO thread context */
141 pa_atomic_t running; /* we cache that value here, so that every thread can query it cheaply */
142 pa_usec_t timestamp;
143 pa_bool_t in_null_mode;
144 pa_smoother *smoother;
145 uint64_t counter;
146 } thread_info;
147 };
148
149 enum {
150 SINK_MESSAGE_ADD_OUTPUT = PA_SINK_MESSAGE_MAX,
151 SINK_MESSAGE_REMOVE_OUTPUT,
152 SINK_MESSAGE_NEED,
153 SINK_MESSAGE_UPDATE_LATENCY,
154 SINK_MESSAGE_UPDATE_MAX_REQUEST,
155 SINK_MESSAGE_UPDATE_REQUESTED_LATENCY
156 };
157
158 enum {
159 SINK_INPUT_MESSAGE_POST = PA_SINK_INPUT_MESSAGE_MAX,
160 };
161
162 static void output_disable(struct output *o);
163 static void output_enable(struct output *o);
164 static void output_free(struct output *o);
165 static int output_create_sink_input(struct output *o);
166
167 static void adjust_rates(struct userdata *u) {
168 struct output *o;
169 pa_usec_t max_sink_latency = 0, min_total_latency = (pa_usec_t) -1, target_latency, avg_total_latency = 0;
170 uint32_t base_rate;
171 uint32_t idx;
172 unsigned n = 0;
173
174 pa_assert(u);
175 pa_sink_assert_ref(u->sink);
176
177 if (pa_idxset_size(u->outputs) <= 0)
178 return;
179
180 if (!PA_SINK_IS_OPENED(pa_sink_get_state(u->sink)))
181 return;
182
183 PA_IDXSET_FOREACH(o, u->outputs, idx) {
184 pa_usec_t sink_latency;
185
186 if (!o->sink_input || !PA_SINK_IS_OPENED(pa_sink_get_state(o->sink)))
187 continue;
188
189 o->total_latency = pa_sink_input_get_latency(o->sink_input, &sink_latency);
190 o->total_latency += sink_latency;
191
192 if (sink_latency > max_sink_latency)
193 max_sink_latency = sink_latency;
194
195 if (min_total_latency == (pa_usec_t) -1 || o->total_latency < min_total_latency)
196 min_total_latency = o->total_latency;
197
198 avg_total_latency += o->total_latency;
199 n++;
200
201 pa_log_debug("[%s] total=%0.2fms sink=%0.2fms ", o->sink->name, (double) o->total_latency / PA_USEC_PER_MSEC, (double) sink_latency / PA_USEC_PER_MSEC);
202
203 if (o->total_latency > 10*PA_USEC_PER_SEC)
204 pa_log_warn("[%s] Total latency of output is very high (%0.2fms), most likely the audio timing in one of your drivers is broken.", o->sink->name, (double) o->total_latency / PA_USEC_PER_MSEC);
205 }
206
207 if (min_total_latency == (pa_usec_t) -1)
208 return;
209
210 avg_total_latency /= n;
211
212 target_latency = max_sink_latency > min_total_latency ? max_sink_latency : min_total_latency;
213
214 pa_log_info("[%s] avg total latency is %0.2f msec.", u->sink->name, (double) avg_total_latency / PA_USEC_PER_MSEC);
215 pa_log_info("[%s] target latency is %0.2f msec.", u->sink->name, (double) target_latency / PA_USEC_PER_MSEC);
216
217 base_rate = u->sink->sample_spec.rate;
218
219 PA_IDXSET_FOREACH(o, u->outputs, idx) {
220 uint32_t r = base_rate;
221
222 if (!o->sink_input || !PA_SINK_IS_OPENED(pa_sink_get_state(o->sink)))
223 continue;
224
225 if (o->total_latency < target_latency)
226 r -= (uint32_t) ((((double) (target_latency - o->total_latency))/(double)u->adjust_time)*(double)r);
227 else if (o->total_latency > target_latency)
228 r += (uint32_t) ((((double) (o->total_latency - target_latency))/(double)u->adjust_time)*(double)r);
229
230 if (r < (uint32_t) (base_rate*0.9) || r > (uint32_t) (base_rate*1.1)) {
231 pa_log_warn("[%s] sample rates too different, not adjusting (%u vs. %u).", o->sink_input->sink->name, base_rate, r);
232 pa_sink_input_set_rate(o->sink_input, base_rate);
233 } else {
234 pa_log_info("[%s] new rate is %u Hz; ratio is %0.3f; latency is %0.0f usec.", o->sink_input->sink->name, r, (double) r / base_rate, (float) o->total_latency);
235 pa_sink_input_set_rate(o->sink_input, r);
236 }
237 }
238
239 pa_asyncmsgq_send(u->sink->asyncmsgq, PA_MSGOBJECT(u->sink), SINK_MESSAGE_UPDATE_LATENCY, NULL, (int64_t) avg_total_latency, NULL);
240 }
241
242 static void time_callback(pa_mainloop_api *a, pa_time_event *e, const struct timeval *t, void *userdata) {
243 struct userdata *u = userdata;
244
245 pa_assert(u);
246 pa_assert(a);
247 pa_assert(u->time_event == e);
248
249 adjust_rates(u);
250
251 pa_core_rttime_restart(u->core, e, pa_rtclock_now() + u->adjust_time);
252 }
253
254 static void process_render_null(struct userdata *u, pa_usec_t now) {
255 size_t ate = 0;
256 pa_assert(u);
257
258 if (u->thread_info.in_null_mode)
259 u->thread_info.timestamp = now;
260
261 while (u->thread_info.timestamp < now + u->block_usec) {
262 pa_memchunk chunk;
263
264 pa_sink_render(u->sink, u->sink->thread_info.max_request, &chunk);
265 pa_memblock_unref(chunk.memblock);
266
267 u->thread_info.counter += chunk.length;
268
269 /* pa_log_debug("Ate %lu bytes.", (unsigned long) chunk.length); */
270 u->thread_info.timestamp += pa_bytes_to_usec(chunk.length, &u->sink->sample_spec);
271
272 ate += chunk.length;
273
274 if (ate >= u->sink->thread_info.max_request)
275 break;
276 }
277
278 /* pa_log_debug("Ate in sum %lu bytes (of %lu)", (unsigned long) ate, (unsigned long) nbytes); */
279
280 pa_smoother_put(u->thread_info.smoother, now,
281 pa_bytes_to_usec(u->thread_info.counter, &u->sink->sample_spec) - (u->thread_info.timestamp - now));
282 }
283
284 static void thread_func(void *userdata) {
285 struct userdata *u = userdata;
286
287 pa_assert(u);
288
289 pa_log_debug("Thread starting up");
290
291 if (u->core->realtime_scheduling)
292 pa_make_realtime(u->core->realtime_priority+1);
293
294 pa_thread_mq_install(&u->thread_mq);
295
296 u->thread_info.timestamp = pa_rtclock_now();
297 u->thread_info.in_null_mode = FALSE;
298
299 for (;;) {
300 int ret;
301
302 if (PA_SINK_IS_OPENED(u->sink->thread_info.state))
303 if (u->sink->thread_info.rewind_requested)
304 pa_sink_process_rewind(u->sink, 0);
305
306 /* If no outputs are connected, render some data and drop it immediately. */
307 if (PA_SINK_IS_OPENED(u->sink->thread_info.state) && !u->thread_info.active_outputs) {
308 pa_usec_t now;
309
310 now = pa_rtclock_now();
311
312 if (!u->thread_info.in_null_mode || u->thread_info.timestamp <= now)
313 process_render_null(u, now);
314
315 pa_rtpoll_set_timer_absolute(u->rtpoll, u->thread_info.timestamp);
316 u->thread_info.in_null_mode = TRUE;
317 } else {
318 pa_rtpoll_set_timer_disabled(u->rtpoll);
319 u->thread_info.in_null_mode = FALSE;
320 }
321
322 /* Hmm, nothing to do. Let's sleep */
323 if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0) {
324 pa_log_info("pa_rtpoll_run() = %i", ret);
325 goto fail;
326 }
327
328 if (ret == 0)
329 goto finish;
330 }
331
332 fail:
333 /* If this was no regular exit from the loop we have to continue
334 * processing messages until we received PA_MESSAGE_SHUTDOWN */
335 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
336 pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
337
338 finish:
339 pa_log_debug("Thread shutting down");
340 }
341
342 /* Called from I/O thread context */
343 static void render_memblock(struct userdata *u, struct output *o, size_t length) {
344 pa_assert(u);
345 pa_assert(o);
346
347 /* We are run by the sink thread, on behalf of an output (o). The
348 * output is waiting for us, hence it is safe to access its
349 * mainblockq and asyncmsgq directly. */
350
351 /* If we are not running, we cannot produce any data */
352 if (!pa_atomic_load(&u->thread_info.running))
353 return;
354
355 /* Maybe there's some data in the requesting output's queue
356 * now? */
357 while (pa_asyncmsgq_process_one(o->inq) > 0)
358 ;
359
360 /* Ok, now let's prepare some data if we really have to */
361 while (!pa_memblockq_is_readable(o->memblockq)) {
362 struct output *j;
363 pa_memchunk chunk;
364
365 /* Render data! */
366 pa_sink_render(u->sink, length, &chunk);
367
368 u->thread_info.counter += chunk.length;
369
370 /* OK, let's send this data to the other threads */
371 PA_LLIST_FOREACH(j, u->thread_info.active_outputs) {
372 if (j == o)
373 continue;
374
375 pa_asyncmsgq_post(j->inq, PA_MSGOBJECT(j->sink_input), SINK_INPUT_MESSAGE_POST, NULL, 0, &chunk, NULL);
376 }
377
378 /* And place it directly into the requesting output's queue */
379 pa_memblockq_push_align(o->memblockq, &chunk);
380 pa_memblock_unref(chunk.memblock);
381 }
382 }
383
384 /* Called from I/O thread context */
385 static void request_memblock(struct output *o, size_t length) {
386 pa_assert(o);
387 pa_sink_input_assert_ref(o->sink_input);
388 pa_sink_assert_ref(o->userdata->sink);
389
390 /* If another thread already prepared some data we received
391 * the data over the asyncmsgq, hence let's first process
392 * it. */
393 while (pa_asyncmsgq_process_one(o->inq) > 0)
394 ;
395
396 /* Check whether we're now readable */
397 if (pa_memblockq_is_readable(o->memblockq))
398 return;
399
400 /* OK, we need to prepare new data, but only if the sink is actually running */
401 if (pa_atomic_load(&o->userdata->thread_info.running))
402 pa_asyncmsgq_send(o->outq, PA_MSGOBJECT(o->userdata->sink), SINK_MESSAGE_NEED, o, (int64_t) length, NULL);
403 }
404
405 /* Called from I/O thread context */
406 static int sink_input_pop_cb(pa_sink_input *i, size_t nbytes, pa_memchunk *chunk) {
407 struct output *o;
408
409 pa_sink_input_assert_ref(i);
410 pa_assert_se(o = i->userdata);
411
412 /* If necessary, get some new data */
413 request_memblock(o, nbytes);
414
415 /* pa_log("%s q size is %u + %u (%u/%u)", */
416 /* i->sink->name, */
417 /* pa_memblockq_get_nblocks(o->memblockq), */
418 /* pa_memblockq_get_nblocks(i->thread_info.render_memblockq), */
419 /* pa_memblockq_get_maxrewind(o->memblockq), */
420 /* pa_memblockq_get_maxrewind(i->thread_info.render_memblockq)); */
421
422 if (pa_memblockq_peek(o->memblockq, chunk) < 0)
423 return -1;
424
425 pa_memblockq_drop(o->memblockq, chunk->length);
426
427 return 0;
428 }
429
430 /* Called from I/O thread context */
431 static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes) {
432 struct output *o;
433
434 pa_sink_input_assert_ref(i);
435 pa_assert_se(o = i->userdata);
436
437 pa_memblockq_rewind(o->memblockq, nbytes);
438 }
439
440 /* Called from I/O thread context */
441 static void sink_input_update_max_rewind_cb(pa_sink_input *i, size_t nbytes) {
442 struct output *o;
443
444 pa_sink_input_assert_ref(i);
445 pa_assert_se(o = i->userdata);
446
447 pa_memblockq_set_maxrewind(o->memblockq, nbytes);
448 }
449
450 /* Called from I/O thread context */
451 static void sink_input_update_max_request_cb(pa_sink_input *i, size_t nbytes) {
452 struct output *o;
453
454 pa_sink_input_assert_ref(i);
455 pa_assert_se(o = i->userdata);
456
457 if (pa_atomic_load(&o->max_request) == (int) nbytes)
458 return;
459
460 pa_atomic_store(&o->max_request, (int) nbytes);
461 pa_asyncmsgq_post(o->outq, PA_MSGOBJECT(o->userdata->sink), SINK_MESSAGE_UPDATE_MAX_REQUEST, NULL, 0, NULL, NULL);
462 }
463
464 /* Called from thread context */
465 static void sink_input_update_sink_requested_latency_cb(pa_sink_input *i) {
466 struct output *o;
467 pa_usec_t c;
468
469 pa_assert(i);
470
471 pa_sink_input_assert_ref(i);
472 pa_assert_se(o = i->userdata);
473
474 c = pa_sink_get_requested_latency_within_thread(i->sink);
475
476 if (c == (pa_usec_t) -1)
477 c = i->sink->thread_info.max_latency;
478
479 if (pa_atomic_load(&o->requested_latency) == (int) c)
480 return;
481
482 pa_atomic_store(&o->requested_latency, (int) c);
483 pa_asyncmsgq_post(o->outq, PA_MSGOBJECT(o->userdata->sink), SINK_MESSAGE_UPDATE_REQUESTED_LATENCY, NULL, 0, NULL, NULL);
484 }
485
486 /* Called from I/O thread context */
487 static void sink_input_attach_cb(pa_sink_input *i) {
488 struct output *o;
489 pa_usec_t c;
490
491 pa_sink_input_assert_ref(i);
492 pa_assert_se(o = i->userdata);
493
494 /* Set up the queue from the sink thread to us */
495 pa_assert(!o->inq_rtpoll_item_read && !o->outq_rtpoll_item_write);
496
497 o->inq_rtpoll_item_read = pa_rtpoll_item_new_asyncmsgq_read(
498 i->sink->thread_info.rtpoll,
499 PA_RTPOLL_LATE, /* This one is not that important, since we check for data in _peek() anyway. */
500 o->inq);
501
502 o->outq_rtpoll_item_write = pa_rtpoll_item_new_asyncmsgq_write(
503 i->sink->thread_info.rtpoll,
504 PA_RTPOLL_EARLY,
505 o->outq);
506
507 pa_sink_input_request_rewind(i, 0, FALSE, TRUE, TRUE);
508
509 pa_atomic_store(&o->max_request, (int) pa_sink_input_get_max_request(i));
510
511 c = pa_sink_get_requested_latency_within_thread(i->sink);
512 pa_atomic_store(&o->requested_latency, (int) (c == (pa_usec_t) -1 ? 0 : c));
513
514 pa_asyncmsgq_post(o->outq, PA_MSGOBJECT(o->userdata->sink), SINK_MESSAGE_UPDATE_MAX_REQUEST, NULL, 0, NULL, NULL);
515 pa_asyncmsgq_post(o->outq, PA_MSGOBJECT(o->userdata->sink), SINK_MESSAGE_UPDATE_REQUESTED_LATENCY, NULL, 0, NULL, NULL);
516 }
517
518 /* Called from I/O thread context */
519 static void sink_input_detach_cb(pa_sink_input *i) {
520 struct output *o;
521
522 pa_sink_input_assert_ref(i);
523 pa_assert_se(o = i->userdata);
524
525 if (o->inq_rtpoll_item_read) {
526 pa_rtpoll_item_free(o->inq_rtpoll_item_read);
527 o->inq_rtpoll_item_read = NULL;
528 }
529
530 if (o->outq_rtpoll_item_write) {
531 pa_rtpoll_item_free(o->outq_rtpoll_item_write);
532 o->outq_rtpoll_item_write = NULL;
533 }
534 }
535
536 /* Called from main context */
537 static void sink_input_kill_cb(pa_sink_input *i) {
538 struct output *o;
539
540 pa_sink_input_assert_ref(i);
541 pa_assert_se(o = i->userdata);
542
543 pa_module_unload_request(o->userdata->module, TRUE);
544 output_free(o);
545 }
546
547 /* Called from thread context */
548 static int sink_input_process_msg(pa_msgobject *obj, int code, void *data, int64_t offset, pa_memchunk *chunk) {
549 struct output *o = PA_SINK_INPUT(obj)->userdata;
550
551 switch (code) {
552
553 case PA_SINK_INPUT_MESSAGE_GET_LATENCY: {
554 pa_usec_t *r = data;
555
556 *r = pa_bytes_to_usec(pa_memblockq_get_length(o->memblockq), &o->sink_input->sample_spec);
557
558 /* Fall through, the default handler will add in the extra
559 * latency added by the resampler */
560 break;
561 }
562
563 case SINK_INPUT_MESSAGE_POST:
564
565 if (PA_SINK_IS_OPENED(o->sink_input->sink->thread_info.state))
566 pa_memblockq_push_align(o->memblockq, chunk);
567 else
568 pa_memblockq_flush_write(o->memblockq, TRUE);
569
570 return 0;
571 }
572
573 return pa_sink_input_process_msg(obj, code, data, offset, chunk);
574 }
575
576 /* Called from main context */
577 static void suspend(struct userdata *u) {
578 struct output *o;
579 uint32_t idx;
580
581 pa_assert(u);
582
583 /* Let's suspend by unlinking all streams */
584 PA_IDXSET_FOREACH(o, u->outputs, idx)
585 output_disable(o);
586
587 pa_log_info("Device suspended...");
588 }
589
590 /* Called from main context */
591 static void unsuspend(struct userdata *u) {
592 struct output *o;
593 uint32_t idx;
594
595 pa_assert(u);
596
597 /* Let's resume */
598 PA_IDXSET_FOREACH(o, u->outputs, idx)
599 output_enable(o);
600
601 pa_log_info("Resumed successfully...");
602 }
603
604 /* Called from main context */
605 static int sink_set_state(pa_sink *sink, pa_sink_state_t state) {
606 struct userdata *u;
607
608 pa_sink_assert_ref(sink);
609 pa_assert_se(u = sink->userdata);
610
611 /* Please note that in contrast to the ALSA modules we call
612 * suspend/unsuspend from main context here! */
613
614 switch (state) {
615 case PA_SINK_SUSPENDED:
616 pa_assert(PA_SINK_IS_OPENED(pa_sink_get_state(u->sink)));
617
618 suspend(u);
619 break;
620
621 case PA_SINK_IDLE:
622 case PA_SINK_RUNNING:
623
624 if (pa_sink_get_state(u->sink) == PA_SINK_SUSPENDED)
625 unsuspend(u);
626
627 break;
628
629 case PA_SINK_UNLINKED:
630 case PA_SINK_INIT:
631 case PA_SINK_INVALID_STATE:
632 ;
633 }
634
635 return 0;
636 }
637
638 /* Called from IO context */
639 static void update_max_request(struct userdata *u) {
640 size_t max_request = 0;
641 struct output *o;
642
643 pa_assert(u);
644 pa_sink_assert_io_context(u->sink);
645
646 /* Collects the max_request values of all streams and sets the
647 * largest one locally */
648
649 PA_LLIST_FOREACH(o, u->thread_info.active_outputs) {
650 size_t mr = (size_t) pa_atomic_load(&o->max_request);
651
652 if (mr > max_request)
653 max_request = mr;
654 }
655
656 if (max_request <= 0)
657 max_request = pa_usec_to_bytes(u->block_usec, &u->sink->sample_spec);
658
659 pa_sink_set_max_request_within_thread(u->sink, max_request);
660 }
661
662 /* Called from IO context */
663 static void update_fixed_latency(struct userdata *u) {
664 pa_usec_t fixed_latency = 0;
665 struct output *o;
666
667 pa_assert(u);
668 pa_sink_assert_io_context(u->sink);
669
670 /* Collects the requested_latency values of all streams and sets
671 * the largest one as fixed_latency locally */
672
673 PA_LLIST_FOREACH(o, u->thread_info.active_outputs) {
674 pa_usec_t rl = (size_t) pa_atomic_load(&o->requested_latency);
675
676 if (rl > fixed_latency)
677 fixed_latency = rl;
678 }
679
680 if (fixed_latency <= 0)
681 fixed_latency = u->block_usec;
682
683 pa_sink_set_fixed_latency_within_thread(u->sink, fixed_latency);
684 }
685
686 /* Called from thread context of the io thread */
687 static void output_add_within_thread(struct output *o) {
688 pa_assert(o);
689 pa_sink_assert_io_context(o->sink);
690
691 PA_LLIST_PREPEND(struct output, o->userdata->thread_info.active_outputs, o);
692
693 pa_assert(!o->outq_rtpoll_item_read && !o->inq_rtpoll_item_write);
694
695 o->outq_rtpoll_item_read = pa_rtpoll_item_new_asyncmsgq_read(
696 o->userdata->rtpoll,
697 PA_RTPOLL_EARLY-1, /* This item is very important */
698 o->outq);
699 o->inq_rtpoll_item_write = pa_rtpoll_item_new_asyncmsgq_write(
700 o->userdata->rtpoll,
701 PA_RTPOLL_EARLY,
702 o->inq);
703 }
704
705 /* Called from thread context of the io thread */
706 static void output_remove_within_thread(struct output *o) {
707 pa_assert(o);
708 pa_sink_assert_io_context(o->sink);
709
710 PA_LLIST_REMOVE(struct output, o->userdata->thread_info.active_outputs, o);
711
712 if (o->outq_rtpoll_item_read) {
713 pa_rtpoll_item_free(o->outq_rtpoll_item_read);
714 o->outq_rtpoll_item_read = NULL;
715 }
716
717 if (o->inq_rtpoll_item_write) {
718 pa_rtpoll_item_free(o->inq_rtpoll_item_write);
719 o->inq_rtpoll_item_write = NULL;
720 }
721 }
722
723 /* Called from thread context of the io thread */
724 static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
725 struct userdata *u = PA_SINK(o)->userdata;
726
727 switch (code) {
728
729 case PA_SINK_MESSAGE_SET_STATE:
730 pa_atomic_store(&u->thread_info.running, PA_PTR_TO_UINT(data) == PA_SINK_RUNNING);
731
732 if (PA_PTR_TO_UINT(data) == PA_SINK_SUSPENDED)
733 pa_smoother_pause(u->thread_info.smoother, pa_rtclock_now());
734 else
735 pa_smoother_resume(u->thread_info.smoother, pa_rtclock_now(), TRUE);
736
737 break;
738
739 case PA_SINK_MESSAGE_GET_LATENCY: {
740 pa_usec_t x, y, c, *delay = data;
741
742 x = pa_rtclock_now();
743 y = pa_smoother_get(u->thread_info.smoother, x);
744
745 c = pa_bytes_to_usec(u->thread_info.counter, &u->sink->sample_spec);
746
747 if (y < c)
748 *delay = c - y;
749 else
750 *delay = 0;
751
752 return 0;
753 }
754
755 case SINK_MESSAGE_ADD_OUTPUT:
756 output_add_within_thread(data);
757 update_max_request(u);
758 update_fixed_latency(u);
759 return 0;
760
761 case SINK_MESSAGE_REMOVE_OUTPUT:
762 output_remove_within_thread(data);
763 update_max_request(u);
764 update_fixed_latency(u);
765 return 0;
766
767 case SINK_MESSAGE_NEED:
768 render_memblock(u, (struct output*) data, (size_t) offset);
769 return 0;
770
771 case SINK_MESSAGE_UPDATE_LATENCY: {
772 pa_usec_t x, y, latency = (pa_usec_t) offset;
773
774 x = pa_rtclock_now();
775 y = pa_bytes_to_usec(u->thread_info.counter, &u->sink->sample_spec);
776
777 if (y > latency)
778 y -= latency;
779 else
780 y = 0;
781
782 pa_smoother_put(u->thread_info.smoother, x, y);
783 return 0;
784 }
785
786 case SINK_MESSAGE_UPDATE_MAX_REQUEST:
787 update_max_request(u);
788 break;
789
790 case SINK_MESSAGE_UPDATE_REQUESTED_LATENCY:
791 update_fixed_latency(u);
792 break;
793 }
794
795 return pa_sink_process_msg(o, code, data, offset, chunk);
796 }
797
798 static void update_description(struct userdata *u) {
799 pa_bool_t first = TRUE;
800 char *t;
801 struct output *o;
802 uint32_t idx;
803
804 pa_assert(u);
805
806 if (!u->auto_desc)
807 return;
808
809 if (pa_idxset_isempty(u->outputs)) {
810 pa_sink_set_description(u->sink, "Simultaneous output");
811 return;
812 }
813
814 t = pa_xstrdup("Simultaneous output to");
815
816 PA_IDXSET_FOREACH(o, u->outputs, idx) {
817 char *e;
818
819 if (first) {
820 e = pa_sprintf_malloc("%s %s", t, pa_strnull(pa_proplist_gets(o->sink->proplist, PA_PROP_DEVICE_DESCRIPTION)));
821 first = FALSE;
822 } else
823 e = pa_sprintf_malloc("%s, %s", t, pa_strnull(pa_proplist_gets(o->sink->proplist, PA_PROP_DEVICE_DESCRIPTION)));
824
825 pa_xfree(t);
826 t = e;
827 }
828
829 pa_sink_set_description(u->sink, t);
830 pa_xfree(t);
831 }
832
833 static int output_create_sink_input(struct output *o) {
834 pa_sink_input_new_data data;
835
836 pa_assert(o);
837
838 if (o->sink_input)
839 return 0;
840
841 pa_sink_input_new_data_init(&data);
842 data.sink = o->sink;
843 data.driver = __FILE__;
844 pa_proplist_setf(data.proplist, PA_PROP_MEDIA_NAME, "Simultaneous output on %s", pa_strnull(pa_proplist_gets(o->sink->proplist, PA_PROP_DEVICE_DESCRIPTION)));
845 pa_proplist_sets(data.proplist, PA_PROP_MEDIA_ROLE, "filter");
846 pa_sink_input_new_data_set_sample_spec(&data, &o->userdata->sink->sample_spec);
847 pa_sink_input_new_data_set_channel_map(&data, &o->userdata->sink->channel_map);
848 data.module = o->userdata->module;
849 data.resample_method = o->userdata->resample_method;
850 data.flags = PA_SINK_INPUT_VARIABLE_RATE|PA_SINK_INPUT_DONT_MOVE|PA_SINK_INPUT_NO_CREATE_ON_SUSPEND;
851
852 pa_sink_input_new(&o->sink_input, o->userdata->core, &data);
853
854 pa_sink_input_new_data_done(&data);
855
856 if (!o->sink_input)
857 return -1;
858
859 o->sink_input->parent.process_msg = sink_input_process_msg;
860 o->sink_input->pop = sink_input_pop_cb;
861 o->sink_input->process_rewind = sink_input_process_rewind_cb;
862 o->sink_input->update_max_rewind = sink_input_update_max_rewind_cb;
863 o->sink_input->update_max_request = sink_input_update_max_request_cb;
864 o->sink_input->update_sink_requested_latency = sink_input_update_sink_requested_latency_cb;
865 o->sink_input->attach = sink_input_attach_cb;
866 o->sink_input->detach = sink_input_detach_cb;
867 o->sink_input->kill = sink_input_kill_cb;
868 o->sink_input->userdata = o;
869
870 pa_sink_input_set_requested_latency(o->sink_input, BLOCK_USEC);
871
872 return 0;
873 }
874
875 /* Called from main context */
876 static struct output *output_new(struct userdata *u, pa_sink *sink) {
877 struct output *o;
878
879 pa_assert(u);
880 pa_assert(sink);
881 pa_assert(u->sink);
882
883 o = pa_xnew0(struct output, 1);
884 o->userdata = u;
885 o->inq = pa_asyncmsgq_new(0);
886 o->outq = pa_asyncmsgq_new(0);
887 o->sink = sink;
888 o->memblockq = pa_memblockq_new(
889 0,
890 MEMBLOCKQ_MAXLENGTH,
891 MEMBLOCKQ_MAXLENGTH,
892 pa_frame_size(&u->sink->sample_spec),
893 1,
894 0,
895 0,
896 &u->sink->silence);
897
898 pa_assert_se(pa_idxset_put(u->outputs, o, NULL) == 0);
899 update_description(u);
900
901 return o;
902 }
903
904 /* Called from main context */
905 static void output_free(struct output *o) {
906 pa_assert(o);
907
908 output_disable(o);
909
910 pa_assert_se(pa_idxset_remove_by_data(o->userdata->outputs, o, NULL));
911 update_description(o->userdata);
912
913 if (o->inq_rtpoll_item_read)
914 pa_rtpoll_item_free(o->inq_rtpoll_item_read);
915 if (o->inq_rtpoll_item_write)
916 pa_rtpoll_item_free(o->inq_rtpoll_item_write);
917
918 if (o->outq_rtpoll_item_read)
919 pa_rtpoll_item_free(o->outq_rtpoll_item_read);
920 if (o->outq_rtpoll_item_write)
921 pa_rtpoll_item_free(o->outq_rtpoll_item_write);
922
923 if (o->inq)
924 pa_asyncmsgq_unref(o->inq);
925
926 if (o->outq)
927 pa_asyncmsgq_unref(o->outq);
928
929 if (o->memblockq)
930 pa_memblockq_free(o->memblockq);
931
932 pa_xfree(o);
933 }
934
935 /* Called from main context */
936 static void output_enable(struct output *o) {
937 pa_assert(o);
938
939 if (o->sink_input)
940 return;
941
942 /* This might cause the sink to be resumed. The state change hook
943 * of the sink might hence be called from here, which might then
944 * cause us to be called in a loop. Make sure that state changes
945 * for this output don't cause this loop by setting a flag here */
946 o->ignore_state_change = TRUE;
947
948 if (output_create_sink_input(o) >= 0) {
949
950 if (pa_sink_get_state(o->sink) != PA_SINK_INIT) {
951
952 /* First we register the output. That means that the sink
953 * will start to pass data to this output. */
954 pa_asyncmsgq_send(o->userdata->sink->asyncmsgq, PA_MSGOBJECT(o->userdata->sink), SINK_MESSAGE_ADD_OUTPUT, o, 0, NULL);
955
956 /* Then we enable the sink input. That means that the sink
957 * is now asked for new data. */
958 pa_sink_input_put(o->sink_input);
959
960 } else
961 /* Hmm the sink is not yet started, do things right here */
962 output_add_within_thread(o);
963 }
964
965 o->ignore_state_change = FALSE;
966 }
967
968 /* Called from main context */
969 static void output_disable(struct output *o) {
970 pa_assert(o);
971
972 if (!o->sink_input)
973 return;
974
975 /* First we disable the sink input. That means that the sink is
976 * not asked for new data anymore */
977 pa_sink_input_unlink(o->sink_input);
978
979 /* Then we unregister the output. That means that the sink doesn't
980 * pass any further data to this output */
981 pa_asyncmsgq_send(o->userdata->sink->asyncmsgq, PA_MSGOBJECT(o->userdata->sink), SINK_MESSAGE_REMOVE_OUTPUT, o, 0, NULL);
982
983 /* Now dellocate the stream */
984 pa_sink_input_unref(o->sink_input);
985 o->sink_input = NULL;
986
987 /* Finally, drop all queued data */
988 pa_memblockq_flush_write(o->memblockq, TRUE);
989 pa_asyncmsgq_flush(o->inq, FALSE);
990 pa_asyncmsgq_flush(o->outq, FALSE);
991 }
992
993 /* Called from main context */
994 static void output_verify(struct output *o) {
995 pa_assert(o);
996
997 if (PA_SINK_IS_OPENED(pa_sink_get_state(o->userdata->sink)))
998 output_enable(o);
999 else
1000 output_disable(o);
1001 }
1002
1003 /* Called from main context */
1004 static pa_bool_t is_suitable_sink(struct userdata *u, pa_sink *s) {
1005 const char *t;
1006
1007 pa_sink_assert_ref(s);
1008
1009 if (s == u->sink)
1010 return FALSE;
1011
1012 if (!(s->flags & PA_SINK_HARDWARE))
1013 return FALSE;
1014
1015 if (!(s->flags & PA_SINK_LATENCY))
1016 return FALSE;
1017
1018 if ((t = pa_proplist_gets(s->proplist, PA_PROP_DEVICE_CLASS)))
1019 if (!pa_streq(t, "sound"))
1020 return FALSE;
1021
1022 return TRUE;
1023 }
1024
1025 /* Called from main context */
1026 static pa_hook_result_t sink_put_hook_cb(pa_core *c, pa_sink *s, struct userdata* u) {
1027 struct output *o;
1028
1029 pa_core_assert_ref(c);
1030 pa_sink_assert_ref(s);
1031 pa_assert(u);
1032
1033 if (!is_suitable_sink(u, s))
1034 return PA_HOOK_OK;
1035
1036 /* Check if the sink is a previously unlinked slave (non-automatic mode) */
1037 if (!u->automatic) {
1038 pa_strlist *l = u->unlinked_slaves;
1039
1040 while (l && !pa_streq(pa_strlist_data(l), s->name))
1041 l = pa_strlist_next(l);
1042
1043 if (l)
1044 u->unlinked_slaves = pa_strlist_remove(u->unlinked_slaves, s->name);
1045 else
1046 return PA_HOOK_OK;
1047 }
1048
1049 pa_log_info("Configuring new sink: %s", s->name);
1050 if (!(o = output_new(u, s))) {
1051 pa_log("Failed to create sink input on sink '%s'.", s->name);
1052 return PA_HOOK_OK;
1053 }
1054
1055 output_verify(o);
1056
1057 return PA_HOOK_OK;
1058 }
1059
1060 /* Called from main context */
1061 static struct output* find_output(struct userdata *u, pa_sink *s) {
1062 struct output *o;
1063 uint32_t idx;
1064
1065 pa_assert(u);
1066 pa_assert(s);
1067
1068 if (u->sink == s)
1069 return NULL;
1070
1071 PA_IDXSET_FOREACH(o, u->outputs, idx)
1072 if (o->sink == s)
1073 return o;
1074
1075 return NULL;
1076 }
1077
1078 /* Called from main context */
1079 static pa_hook_result_t sink_unlink_hook_cb(pa_core *c, pa_sink *s, struct userdata* u) {
1080 struct output *o;
1081
1082 pa_assert(c);
1083 pa_sink_assert_ref(s);
1084 pa_assert(u);
1085
1086 if (!(o = find_output(u, s)))
1087 return PA_HOOK_OK;
1088
1089 pa_log_info("Unconfiguring sink: %s", s->name);
1090
1091 if (!u->automatic)
1092 u->unlinked_slaves = pa_strlist_prepend(u->unlinked_slaves, s->name);
1093
1094 output_free(o);
1095
1096 return PA_HOOK_OK;
1097 }
1098
1099 /* Called from main context */
1100 static pa_hook_result_t sink_state_changed_hook_cb(pa_core *c, pa_sink *s, struct userdata* u) {
1101 struct output *o;
1102
1103 if (!(o = find_output(u, s)))
1104 return PA_HOOK_OK;
1105
1106 /* This state change might be triggered because we are creating a
1107 * stream here, in that case we don't want to create it a second
1108 * time here and enter a loop */
1109 if (o->ignore_state_change)
1110 return PA_HOOK_OK;
1111
1112 output_verify(o);
1113
1114 return PA_HOOK_OK;
1115 }
1116
1117 int pa__init(pa_module*m) {
1118 struct userdata *u;
1119 pa_modargs *ma = NULL;
1120 const char *slaves, *rm;
1121 int resample_method = PA_RESAMPLER_TRIVIAL;
1122 pa_sample_spec ss;
1123 pa_channel_map map;
1124 struct output *o;
1125 uint32_t idx;
1126 pa_sink_new_data data;
1127 uint32_t adjust_time_sec;
1128
1129 pa_assert(m);
1130
1131 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
1132 pa_log("failed to parse module arguments");
1133 goto fail;
1134 }
1135
1136 if ((rm = pa_modargs_get_value(ma, "resample_method", NULL))) {
1137 if ((resample_method = pa_parse_resample_method(rm)) < 0) {
1138 pa_log("invalid resample method '%s'", rm);
1139 goto fail;
1140 }
1141 }
1142
1143 m->userdata = u = pa_xnew0(struct userdata, 1);
1144 u->core = m->core;
1145 u->module = m;
1146 u->rtpoll = pa_rtpoll_new();
1147 pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
1148 u->resample_method = resample_method;
1149 u->outputs = pa_idxset_new(NULL, NULL);
1150 u->thread_info.smoother = pa_smoother_new(
1151 PA_USEC_PER_SEC,
1152 PA_USEC_PER_SEC*2,
1153 TRUE,
1154 TRUE,
1155 10,
1156 0,
1157 FALSE);
1158
1159 adjust_time_sec = DEFAULT_ADJUST_TIME_USEC / PA_USEC_PER_SEC;
1160 if (pa_modargs_get_value_u32(ma, "adjust_time", &adjust_time_sec) < 0) {
1161 pa_log("Failed to parse adjust_time value");
1162 goto fail;
1163 }
1164
1165 if (adjust_time_sec != DEFAULT_ADJUST_TIME_USEC / PA_USEC_PER_SEC)
1166 u->adjust_time = adjust_time_sec * PA_USEC_PER_SEC;
1167 else
1168 u->adjust_time = DEFAULT_ADJUST_TIME_USEC;
1169
1170 slaves = pa_modargs_get_value(ma, "slaves", NULL);
1171 u->automatic = !slaves;
1172
1173 ss = m->core->default_sample_spec;
1174 map = m->core->default_channel_map;
1175
1176 /* Check the specified slave sinks for sample_spec and channel_map to use for the combined sink */
1177 if (!u->automatic) {
1178 const char*split_state = NULL;
1179 char *n = NULL;
1180 pa_sample_spec slaves_spec;
1181 pa_channel_map slaves_map;
1182 pa_bool_t is_first_slave = TRUE;
1183
1184 pa_sample_spec_init(&slaves_spec);
1185
1186 while ((n = pa_split(slaves, ",", &split_state))) {
1187 pa_sink *slave_sink;
1188
1189 if (!(slave_sink = pa_namereg_get(m->core, n, PA_NAMEREG_SINK))) {
1190 pa_log("Invalid slave sink '%s'", n);
1191 pa_xfree(n);
1192 goto fail;
1193 }
1194
1195 pa_xfree(n);
1196
1197 if (is_first_slave) {
1198 slaves_spec = slave_sink->sample_spec;
1199 slaves_map = slave_sink->channel_map;
1200 is_first_slave = FALSE;
1201 } else {
1202 if (slaves_spec.format != slave_sink->sample_spec.format)
1203 slaves_spec.format = PA_SAMPLE_INVALID;
1204
1205 if (slaves_spec.rate < slave_sink->sample_spec.rate)
1206 slaves_spec.rate = slave_sink->sample_spec.rate;
1207
1208 if (!pa_channel_map_equal(&slaves_map, &slave_sink->channel_map))
1209 slaves_spec.channels = 0;
1210 }
1211 }
1212
1213 if (!is_first_slave) {
1214 if (slaves_spec.format != PA_SAMPLE_INVALID)
1215 ss.format = slaves_spec.format;
1216
1217 ss.rate = slaves_spec.rate;
1218
1219 if (slaves_spec.channels > 0) {
1220 map = slaves_map;
1221 ss.channels = slaves_map.channels;
1222 }
1223 }
1224 }
1225
1226 if ((pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0)) {
1227 pa_log("Invalid sample specification.");
1228 goto fail;
1229 }
1230
1231 pa_sink_new_data_init(&data);
1232 data.namereg_fail = FALSE;
1233 data.driver = __FILE__;
1234 data.module = m;
1235 pa_sink_new_data_set_name(&data, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME));
1236 pa_sink_new_data_set_sample_spec(&data, &ss);
1237 pa_sink_new_data_set_channel_map(&data, &map);
1238 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_CLASS, "filter");
1239
1240 if (slaves)
1241 pa_proplist_sets(data.proplist, "combine.slaves", slaves);
1242
1243 if (pa_modargs_get_proplist(ma, "sink_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
1244 pa_log("Invalid properties");
1245 pa_sink_new_data_done(&data);
1246 goto fail;
1247 }
1248
1249 /* Check proplist for a description & fill in a default value if not */
1250 u->auto_desc = FALSE;
1251 if (NULL == pa_proplist_gets(data.proplist, PA_PROP_DEVICE_DESCRIPTION)) {
1252 u->auto_desc = TRUE;
1253 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Simultaneous Output");
1254 }
1255
1256 u->sink = pa_sink_new(m->core, &data, PA_SINK_LATENCY);
1257 pa_sink_new_data_done(&data);
1258
1259 if (!u->sink) {
1260 pa_log("Failed to create sink");
1261 goto fail;
1262 }
1263
1264 u->sink->parent.process_msg = sink_process_msg;
1265 u->sink->set_state = sink_set_state;
1266 u->sink->userdata = u;
1267
1268 pa_sink_set_rtpoll(u->sink, u->rtpoll);
1269 pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
1270
1271 u->block_usec = BLOCK_USEC;
1272 pa_sink_set_max_request(u->sink, pa_usec_to_bytes(u->block_usec, &u->sink->sample_spec));
1273
1274 if (!u->automatic) {
1275 const char*split_state;
1276 char *n = NULL;
1277 pa_assert(slaves);
1278
1279 /* The slaves have been specified manually */
1280
1281 split_state = NULL;
1282 while ((n = pa_split(slaves, ",", &split_state))) {
1283 pa_sink *slave_sink;
1284
1285 if (!(slave_sink = pa_namereg_get(m->core, n, PA_NAMEREG_SINK)) || slave_sink == u->sink) {
1286 pa_log("Invalid slave sink '%s'", n);
1287 pa_xfree(n);
1288 goto fail;
1289 }
1290
1291 pa_xfree(n);
1292
1293 if (!output_new(u, slave_sink)) {
1294 pa_log("Failed to create slave sink input on sink '%s'.", slave_sink->name);
1295 goto fail;
1296 }
1297 }
1298
1299 if (pa_idxset_size(u->outputs) <= 1)
1300 pa_log_warn("No slave sinks specified.");
1301
1302 u->sink_put_slot = NULL;
1303
1304 } else {
1305 pa_sink *s;
1306
1307 /* We're in automatic mode, we add every sink that matches our needs */
1308
1309 PA_IDXSET_FOREACH(s, m->core->sinks, idx) {
1310
1311 if (!is_suitable_sink(u, s))
1312 continue;
1313
1314 if (!output_new(u, s)) {
1315 pa_log("Failed to create sink input on sink '%s'.", s->name);
1316 goto fail;
1317 }
1318 }
1319 }
1320
1321 u->sink_put_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_PUT], PA_HOOK_LATE, (pa_hook_cb_t) sink_put_hook_cb, u);
1322 u->sink_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_UNLINK], PA_HOOK_EARLY, (pa_hook_cb_t) sink_unlink_hook_cb, u);
1323 u->sink_state_changed_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_STATE_CHANGED], PA_HOOK_NORMAL, (pa_hook_cb_t) sink_state_changed_hook_cb, u);
1324
1325 if (!(u->thread = pa_thread_new("combine", thread_func, u))) {
1326 pa_log("Failed to create thread.");
1327 goto fail;
1328 }
1329
1330 /* Activate the sink and the sink inputs */
1331 pa_sink_put(u->sink);
1332
1333 PA_IDXSET_FOREACH(o, u->outputs, idx)
1334 output_verify(o);
1335
1336 if (u->adjust_time > 0)
1337 u->time_event = pa_core_rttime_new(m->core, pa_rtclock_now() + u->adjust_time, time_callback, u);
1338
1339 pa_modargs_free(ma);
1340
1341 return 0;
1342
1343 fail:
1344
1345 if (ma)
1346 pa_modargs_free(ma);
1347
1348 pa__done(m);
1349
1350 return -1;
1351 }
1352
1353 void pa__done(pa_module*m) {
1354 struct userdata *u;
1355 struct output *o;
1356
1357 pa_assert(m);
1358
1359 if (!(u = m->userdata))
1360 return;
1361
1362 pa_strlist_free(u->unlinked_slaves);
1363
1364 if (u->sink_put_slot)
1365 pa_hook_slot_free(u->sink_put_slot);
1366
1367 if (u->sink_unlink_slot)
1368 pa_hook_slot_free(u->sink_unlink_slot);
1369
1370 if (u->sink_state_changed_slot)
1371 pa_hook_slot_free(u->sink_state_changed_slot);
1372
1373 if (u->outputs) {
1374 while ((o = pa_idxset_first(u->outputs, NULL)))
1375 output_free(o);
1376
1377 pa_idxset_free(u->outputs, NULL, NULL);
1378 }
1379
1380 if (u->sink)
1381 pa_sink_unlink(u->sink);
1382
1383 if (u->thread) {
1384 pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
1385 pa_thread_free(u->thread);
1386 }
1387
1388 pa_thread_mq_done(&u->thread_mq);
1389
1390 if (u->sink)
1391 pa_sink_unref(u->sink);
1392
1393 if (u->rtpoll)
1394 pa_rtpoll_free(u->rtpoll);
1395
1396 if (u->time_event)
1397 u->core->mainloop->time_free(u->time_event);
1398
1399 if (u->thread_info.smoother)
1400 pa_smoother_free(u->thread_info.smoother);
1401
1402 pa_xfree(u);
1403 }