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