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