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