]> code.delx.au - pulseaudio/blob - src/modules/module-combine.c
drop a couple of WARNING prefixes in log messages, since we have pa_log_warn anyway...
[pulseaudio] / src / modules / module-combine.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
12
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdio.h>
29 #include <errno.h>
30
31 #include <pulse/timeval.h>
32 #include <pulse/xmalloc.h>
33
34 #include <pulsecore/macro.h>
35 #include <pulsecore/module.h>
36 #include <pulsecore/llist.h>
37 #include <pulsecore/sink.h>
38 #include <pulsecore/sink-input.h>
39 #include <pulsecore/memblockq.h>
40 #include <pulsecore/log.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/rtclock.h>
49 #include <pulsecore/core-error.h>
50
51 #include "module-combine-symdef.h"
52
53 PA_MODULE_AUTHOR("Lennart Poettering")
54 PA_MODULE_DESCRIPTION("Combine multiple sinks to one")
55 PA_MODULE_VERSION(PACKAGE_VERSION)
56 PA_MODULE_USAGE(
57 "sink_name=<name for the sink> "
58 "master=<master sink> "
59 "slaves=<slave sinks> "
60 "adjust_time=<seconds> "
61 "resample_method=<method> "
62 "format=<sample format> "
63 "channels=<number of channels> "
64 "rate=<sample rate> "
65 "channel_map=<channel map>")
66
67 #define DEFAULT_SINK_NAME "combined"
68 #define MEMBLOCKQ_MAXLENGTH (1024*170)
69
70 #define DEFAULT_ADJUST_TIME 10
71
72 static const char* const valid_modargs[] = {
73 "sink_name",
74 "master",
75 "slaves",
76 "adjust_time",
77 "resample_method",
78 "format",
79 "channels",
80 "rate",
81 "channel_map",
82 NULL
83 };
84
85 struct output {
86 struct userdata *userdata;
87 pa_sink *sink;
88 pa_sink_input *sink_input;
89
90 pa_asyncmsgq *asyncmsgq;
91 pa_rtpoll_item *rtpoll_item;
92
93 pa_memblockq *memblockq;
94
95 pa_usec_t total_latency;
96
97 PA_LLIST_FIELDS(struct output);
98 };
99
100 struct userdata {
101 pa_core *core;
102 pa_module *module;
103 pa_sink *sink;
104
105 pa_thread *thread;
106 pa_thread_mq thread_mq;
107 pa_rtpoll *rtpoll;
108
109 pa_mutex *mutex;
110
111 struct output *master;
112
113 pa_time_event *time_event;
114 uint32_t adjust_time;
115
116 int automatic;
117 size_t block_size;
118
119 struct timespec timestamp;
120
121 pa_hook_slot *sink_new_slot, *sink_unlink_slot, *sink_state_changed_slot;
122
123 pa_resample_method_t resample_method;
124
125 struct timespec adjust_timestamp;
126
127 pa_idxset* outputs; /* managed in main context */
128
129 struct {
130 PA_LLIST_HEAD(struct output, outputs); /* managed in IO thread context */
131 struct output *master;
132 } thread_info;
133 };
134
135 enum {
136 SINK_MESSAGE_ADD_OUTPUT = PA_SINK_MESSAGE_MAX,
137 SINK_MESSAGE_REMOVE_OUTPUT
138 };
139
140 enum {
141 SINK_INPUT_MESSAGE_POST = PA_SINK_INPUT_MESSAGE_MAX
142 };
143
144 static void output_free(struct output *o);
145 static int output_create_sink_input(struct userdata *u, struct output *o);
146 static int update_master(struct userdata *u, struct output *o);
147 static int pick_master(struct userdata *u);
148
149 static void adjust_rates(struct userdata *u) {
150 struct output *o;
151 pa_usec_t max_sink_latency = 0, min_total_latency = (pa_usec_t) -1, target_latency;
152 uint32_t base_rate;
153 uint32_t idx;
154
155 pa_assert(u);
156 pa_sink_assert_ref(u->sink);
157
158 if (pa_idxset_size(u->outputs) <= 0)
159 return;
160
161 if (!PA_SINK_OPENED(pa_sink_get_state(u->sink)))
162 return;
163
164 for (o = pa_idxset_first(u->outputs, &idx); o; o = pa_idxset_next(u->outputs, &idx)) {
165 uint32_t sink_latency;
166
167 if (!o->sink_input || !PA_SINK_OPENED(pa_sink_get_state(o->sink)))
168 continue;
169
170 sink_latency = o->sink_input->sink ? pa_sink_get_latency(o->sink_input->sink) : 0;
171 o->total_latency = sink_latency + pa_sink_input_get_latency(o->sink_input);
172
173 if (sink_latency > max_sink_latency)
174 max_sink_latency = sink_latency;
175
176 if (o->total_latency < min_total_latency)
177 min_total_latency = o->total_latency;
178 }
179
180 if (min_total_latency == (pa_usec_t) -1)
181 return;
182
183 target_latency = max_sink_latency > min_total_latency ? max_sink_latency : min_total_latency;
184
185 pa_log_info("[%s] target latency is %0.0f usec.", u->sink->name, (float) target_latency);
186 pa_log_info("[%s] master is %s", u->sink->name, u->master->sink->description);
187
188 base_rate = u->sink->sample_spec.rate;
189
190 for (o = pa_idxset_first(u->outputs, &idx); o; o = pa_idxset_next(u->outputs, &idx)) {
191 uint32_t r = base_rate;
192
193 if (!o->sink_input || !PA_SINK_OPENED(pa_sink_get_state(o->sink)))
194 continue;
195
196 if (o->total_latency < target_latency)
197 r -= (uint32_t) (((((double) target_latency - o->total_latency))/u->adjust_time)*r/ 1000000);
198 else if (o->total_latency > target_latency)
199 r += (uint32_t) (((((double) o->total_latency - target_latency))/u->adjust_time)*r/ 1000000);
200
201 if (r < (uint32_t) (base_rate*0.9) || r > (uint32_t) (base_rate*1.1)) {
202 pa_log_warn("[%s] sample rates too different, not adjusting (%u vs. %u).", o->sink_input->name, base_rate, r);
203 pa_sink_input_set_rate(o->sink_input, base_rate);
204 } else {
205 pa_log_info("[%s] new rate is %u Hz; ratio is %0.3f; latency is %0.0f usec.", o->sink_input->name, r, (double) r / base_rate, (float) o->total_latency);
206 pa_sink_input_set_rate(o->sink_input, r);
207 }
208 }
209 }
210
211 static void time_callback(pa_mainloop_api*a, pa_time_event* e, const struct timeval *tv, void *userdata) {
212 struct userdata *u = userdata;
213 struct timeval n;
214
215 pa_assert(u);
216 pa_assert(a);
217 pa_assert(u->time_event == e);
218
219 adjust_rates(u);
220
221 pa_gettimeofday(&n);
222 n.tv_sec += u->adjust_time;
223 u->sink->core->mainloop->time_restart(e, &n);
224 }
225
226 static void thread_func(void *userdata) {
227 struct userdata *u = userdata;
228
229 pa_assert(u);
230
231 pa_log_debug("Thread starting up");
232
233 pa_thread_mq_install(&u->thread_mq);
234 pa_rtpoll_install(u->rtpoll);
235
236 pa_rtclock_get(&u->timestamp);
237
238 /* This is only run when we are in NULL mode, to make sure that
239 * playback doesn't stop. In all other cases we hook our stuff
240 * into the master sink. */
241
242 for (;;) {
243 int ret;
244
245 /* Render some data and drop it immediately */
246 if (u->sink->thread_info.state == PA_SINK_RUNNING) {
247 struct timespec now;
248
249 pa_rtclock_get(&now);
250
251 if (pa_timespec_cmp(&u->timestamp, &now) <= 0) {
252 pa_sink_skip(u->sink, u->block_size);
253 pa_timespec_add(&u->timestamp, pa_bytes_to_usec(u->block_size, &u->sink->sample_spec));
254 }
255
256 pa_rtpoll_set_timer_absolute(u->rtpoll, &u->timestamp);
257 } else
258 pa_rtpoll_set_timer_disabled(u->rtpoll);
259
260 /* Hmm, nothing to do. Let's sleep */
261 if ((ret = pa_rtpoll_run(u->rtpoll, 1)) < 0)
262 goto fail;
263
264 if (ret == 0)
265 goto finish;
266 }
267
268 fail:
269 /* If this was no regular exit from the loop we have to continue
270 * processing messages until we received PA_MESSAGE_SHUTDOWN */
271 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
272 pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
273
274 finish:
275 pa_log_debug("Thread shutting down");
276 }
277
278 static void request_memblock(struct output *o, size_t length) {
279 pa_memchunk chunk;
280
281 pa_assert(o);
282 pa_sink_input_assert_ref(o->sink_input);
283 pa_sink_assert_ref(o->userdata->sink);
284
285 /* If another thread already prepared some data we received
286 * the data over the asyncmsgq, hence let's first process
287 * it. */
288 while (pa_asyncmsgq_process_one(o->asyncmsgq) > 0)
289 ;
290
291 /* Check whether we're now readable */
292 if (pa_memblockq_is_readable(o->memblockq))
293 return;
294
295 /* OK, we need to prepare new data */
296 pa_mutex_lock(o->userdata->mutex);
297
298 if (PA_SINK_OPENED(o->userdata->sink->thread_info.state)) {
299
300 /* Maybe there's some data now? */
301 while (pa_asyncmsgq_process_one(o->asyncmsgq) > 0)
302 ;
303
304 /* Ok, now let's prepare some data if we really have to */
305 while (!pa_memblockq_is_readable(o->memblockq)) {
306 struct output *j;
307
308 /* Do it! */
309 pa_sink_render(o->userdata->sink, length, &chunk);
310
311 /* OK, let's send this data to the other threads */
312 for (j = o->userdata->thread_info.outputs; j; j = j->next)
313 if (j != o && j->sink_input)
314 pa_asyncmsgq_post(j->asyncmsgq, PA_MSGOBJECT(j->sink_input), SINK_INPUT_MESSAGE_POST, NULL, 0, &chunk, NULL);
315
316 /* And push it into our own queue */
317 pa_memblockq_push_align(o->memblockq, &chunk);
318 pa_memblock_unref(chunk.memblock);
319 }
320 }
321
322 pa_mutex_unlock(o->userdata->mutex);
323 }
324
325 /* Called from I/O thread context */
326 static int sink_input_peek_cb(pa_sink_input *i, size_t length, pa_memchunk *chunk) {
327 struct output *o;
328
329 pa_sink_input_assert_ref(i);
330 o = i->userdata;
331 pa_assert(o);
332
333 /* If necessary, get some new data */
334 request_memblock(o, length);
335
336 return pa_memblockq_peek(o->memblockq, chunk);
337 }
338
339 /* Called from I/O thread context */
340 static void sink_input_drop_cb(pa_sink_input *i, size_t length) {
341 struct output *o;
342
343 pa_sink_input_assert_ref(i);
344 pa_assert(length > 0);
345 o = i->userdata;
346 pa_assert(o);
347
348 pa_memblockq_drop(o->memblockq, length);
349 }
350
351 /* Called from I/O thread context */
352 static void sink_input_attach_cb(pa_sink_input *i) {
353 struct output *o;
354
355 pa_sink_input_assert_ref(i);
356 o = i->userdata;
357 pa_assert(o);
358
359 if (o->userdata->master == o) {
360 /* Calling these two functions here is safe, because both
361 * threads that might access this sink input are known to be
362 * waiting for us. */
363 pa_sink_set_asyncmsgq(o->userdata->sink, i->sink->asyncmsgq);
364 pa_sink_set_rtpoll(o->userdata->sink, i->sink->rtpoll);
365 pa_sink_attach_within_thread(o->userdata->sink);
366 }
367
368 pa_assert(!o->rtpoll_item);
369 o->rtpoll_item = pa_rtpoll_item_new_asyncmsgq(
370 i->sink->rtpoll,
371 PA_RTPOLL_NORMAL, /* This one has a lower priority than the normal message handling */
372 o->asyncmsgq);
373 }
374
375 /* Called from I/O thread context */
376 static void sink_input_detach_cb(pa_sink_input *i) {
377 struct output *o;
378
379 pa_sink_input_assert_ref(i);
380 o = i->userdata;
381 pa_assert(o);
382
383 pa_assert(o->rtpoll_item);
384 pa_rtpoll_item_free(o->rtpoll_item);
385 o->rtpoll_item = NULL;
386
387 if (o->userdata->master == o)
388 pa_sink_detach_within_thread(o->userdata->sink);
389 }
390
391 /* Called from main context */
392 static void sink_input_kill_cb(pa_sink_input *i) {
393 struct output *o;
394
395 pa_sink_input_assert_ref(i);
396 o = i->userdata;
397 pa_assert(o);
398
399 pa_sink_input_unlink(o->sink_input);
400 pa_sink_input_unref(o->sink_input);
401 o->sink_input = NULL;
402
403 pa_module_unload_request(o->userdata->module);
404 }
405
406 /* Called from thread context */
407 static int sink_input_process_msg(pa_msgobject *obj, int code, void *data, int64_t offset, pa_memchunk *chunk) {
408 struct output *o = PA_SINK_INPUT(obj)->userdata;
409
410 switch (code) {
411
412 case PA_SINK_INPUT_MESSAGE_GET_LATENCY: {
413 pa_usec_t *r = data;
414
415 *r = pa_bytes_to_usec(pa_memblockq_get_length(o->memblockq), &o->sink_input->sample_spec);
416
417 /* Fall through, the default handler will add in the extra
418 * latency added by the resampler */
419 break;
420 }
421
422 case SINK_INPUT_MESSAGE_POST: {
423
424 if (PA_SINK_OPENED(o->sink_input->sink->thread_info.state))
425 pa_memblockq_push_align(o->memblockq, chunk);
426 else
427 pa_memblockq_flush(o->memblockq);
428
429 break;
430 }
431 }
432
433 return pa_sink_input_process_msg(obj, code, data, offset, chunk);
434 }
435
436 static int suspend(struct userdata *u) {
437 struct output *o;
438 uint32_t idx;
439
440 pa_assert(u);
441
442 /* Let's suspend by unlinking all streams */
443
444 for (o = pa_idxset_first(u->outputs, &idx); o; o = pa_idxset_next(u->outputs, &idx)) {
445
446 if (o->sink_input) {
447 pa_sink_input_unlink(o->sink_input);
448 pa_sink_input_unref(o->sink_input);
449 o->sink_input = NULL;
450 }
451 }
452
453 if (pick_master(u) < 0)
454 pa_module_unload_request(u->module);
455
456 pa_log_info("Device suspended...");
457
458 return 0;
459 }
460
461 static int unsuspend(struct userdata *u) {
462 struct output *o;
463 uint32_t idx;
464
465 pa_assert(u);
466
467 /* Let's resume */
468
469 for (o = pa_idxset_first(u->outputs, &idx); o; o = pa_idxset_next(u->outputs, &idx)) {
470
471 pa_sink_suspend(o->sink, 0);
472
473 if (PA_SINK_OPENED(pa_sink_get_state(o->sink))) {
474 if (output_create_sink_input(u, o) < 0)
475 output_free(o);
476 else
477 pa_sink_input_put(o->sink_input);
478 }
479 }
480
481 if (pick_master(u) < 0)
482 pa_module_unload_request(u->module);
483
484 pa_log_info("Resumed successfully...");
485 return 0;
486 }
487
488 static int sink_set_state(pa_sink *sink, pa_sink_state_t state) {
489 struct userdata *u;
490
491 pa_sink_assert_ref(sink);
492 u = sink->userdata;
493 pa_assert(u);
494
495 /* Please note that in contrast to the ALSA modules we call
496 * suspend/unsuspend from main context here! */
497
498 switch (state) {
499 case PA_SINK_SUSPENDED:
500 pa_assert(PA_SINK_OPENED(pa_sink_get_state(u->sink)));
501
502 if (suspend(u) < 0)
503 return -1;
504
505 break;
506
507 case PA_SINK_IDLE:
508 case PA_SINK_RUNNING:
509
510 if (pa_sink_get_state(u->sink) == PA_SINK_SUSPENDED) {
511 if (unsuspend(u) < 0)
512 return -1;
513 }
514
515 break;
516
517 case PA_SINK_UNLINKED:
518 case PA_SINK_INIT:
519 ;
520 }
521
522 return 0;
523 }
524
525 /* Called from thread context of the master */
526 static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
527 struct userdata *u = PA_SINK(o)->userdata;
528
529 switch (code) {
530
531 case PA_SINK_MESSAGE_SET_STATE:
532
533 if ((pa_sink_state_t) PA_PTR_TO_UINT(data) == PA_SINK_RUNNING) {
534 /* Only useful when running in NULL mode, i.e. when no
535 * master sink is attached */
536 pa_rtclock_get(&u->timestamp);
537 }
538
539 break;
540
541 case PA_SINK_MESSAGE_GET_LATENCY: {
542 struct timespec now;
543
544 /* This code will only be called when running in NULL
545 * mode, i.e. when no master sink is attached. See
546 * sink_get_latency_cb() below */
547 pa_rtclock_get(&now);
548
549 if (pa_timespec_cmp(&u->timestamp, &now) > 0)
550 *((pa_usec_t*) data) = 0;
551 else
552 *((pa_usec_t*) data) = pa_timespec_diff(&u->timestamp, &now);
553 break;
554 }
555
556 case PA_SINK_MESSAGE_DETACH:
557
558 /* We're detaching all our input streams artificially, so
559 * that we can drive our sink from a different sink */
560
561 u->thread_info.master = NULL;
562 break;
563
564 case PA_SINK_MESSAGE_ATTACH:
565
566 /* We're attached all our input streams artificially again */
567
568 u->thread_info.master = data;
569 break;
570
571 case SINK_MESSAGE_ADD_OUTPUT:
572 PA_LLIST_PREPEND(struct output, u->thread_info.outputs, (struct output*) data);
573 break;
574
575 case SINK_MESSAGE_REMOVE_OUTPUT:
576 PA_LLIST_REMOVE(struct output, u->thread_info.outputs, (struct output*) data);
577 break;
578 }
579
580 return pa_sink_process_msg(o, code, data, offset, chunk);
581 }
582
583 /* Called from main context */
584 static pa_usec_t sink_get_latency_cb(pa_sink *s) {
585 struct userdata *u;
586
587 pa_sink_assert_ref(s);
588 u = s->userdata;
589 pa_assert(u);
590
591 if (u->master) {
592 /* If we have a master sink, we just return the latency of it
593 * and add our own buffering on top */
594
595 if (!u->master->sink_input)
596 return 0;
597
598 return
599 pa_sink_input_get_latency(u->master->sink_input) +
600 pa_sink_get_latency(u->master->sink_input->sink);
601
602 } else {
603 pa_usec_t usec;
604
605 /* We have no master, hence let's ask our own thread which
606 * implements the NULL sink */
607
608 if (pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_LATENCY, &usec, 0, NULL) < 0)
609 return 0;
610
611 return usec;
612 }
613 }
614
615 static void update_description(struct userdata *u) {
616 int first = 1;
617 char *t;
618 struct output *o;
619 uint32_t idx;
620
621 pa_assert(u);
622
623 if (pa_idxset_isempty(u->outputs)) {
624 pa_sink_set_description(u->sink, "Simultaneous output");
625 return;
626 }
627
628 t = pa_xstrdup("Simultaneous output to");
629
630 for (o = pa_idxset_first(u->outputs, &idx); o; o = pa_idxset_next(u->outputs, &idx)) {
631 char *e;
632
633 if (first) {
634 e = pa_sprintf_malloc("%s %s", t, o->sink->description);
635 first = 0;
636 } else
637 e = pa_sprintf_malloc("%s, %s", t, o->sink->description);
638
639 pa_xfree(t);
640 t = e;
641 }
642
643 pa_sink_set_description(u->sink, t);
644 pa_xfree(t);
645 }
646
647 static int update_master(struct userdata *u, struct output *o) {
648 pa_assert(u);
649
650 /* Make sure everything is detached from the old thread before we move our stuff to a new thread */
651 if (u->sink && PA_SINK_LINKED(pa_sink_get_state(u->sink)))
652 pa_sink_detach(u->sink);
653
654 if (o) {
655 /* If we have a master sink we run our own sink in its thread */
656
657 pa_assert(o->sink_input);
658 pa_assert(PA_SINK_OPENED(pa_sink_get_state(o->sink)));
659
660 if (u->thread) {
661 /* If we previously were in NULL mode, let's kill the thread */
662 pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
663 pa_thread_free(u->thread);
664 u->thread = NULL;
665
666 pa_assert(u->rtpoll);
667 pa_rtpoll_free(u->rtpoll);
668 u->rtpoll = NULL;
669 }
670
671 pa_sink_set_asyncmsgq(u->sink, o->sink->asyncmsgq);
672 pa_sink_set_rtpoll(u->sink, o->sink->rtpoll);
673 u->master = o;
674
675 pa_log_info("Master sink is now '%s'", o->sink_input->sink->name);
676
677 } else {
678
679 /* We have no master sink, let's create our own thread */
680
681 pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
682 u->master = NULL;
683
684 if (!u->thread) {
685 pa_assert(!u->rtpoll);
686
687 u->rtpoll = pa_rtpoll_new();
688 pa_rtpoll_item_new_asyncmsgq(u->rtpoll, PA_RTPOLL_EARLY, u->thread_mq.inq);
689
690 pa_sink_set_rtpoll(u->sink, u->rtpoll);
691
692 if (!(u->thread = pa_thread_new(thread_func, u))) {
693 pa_log("Failed to create thread.");
694 return -1;
695 }
696 }
697
698 pa_log_info("No suitable master sink found, going to NULL mode\n");
699 }
700
701 /* Now attach everything again */
702 if (u->sink && PA_SINK_LINKED(pa_sink_get_state(u->sink)))
703 pa_sink_attach(u->sink);
704
705 return 0;
706 }
707
708 static int pick_master(struct userdata *u) {
709 struct output *o;
710 uint32_t idx;
711 pa_assert(u);
712
713 if (u->master && u->master->sink_input && PA_SINK_OPENED(pa_sink_get_state(u->master->sink)))
714 return update_master(u, u->master);
715
716 for (o = pa_idxset_first(u->outputs, &idx); o; o = pa_idxset_next(u->outputs, &idx))
717 if (o->sink_input && PA_SINK_OPENED(pa_sink_get_state(o->sink)))
718 return update_master(u, o);
719
720 return update_master(u, NULL);
721 }
722
723 static int output_create_sink_input(struct userdata *u, struct output *o) {
724 pa_sink_input_new_data data;
725 char *t;
726
727 pa_assert(u);
728 pa_assert(!o->sink_input);
729
730 t = pa_sprintf_malloc("Simultaneous output on %s", o->sink->description);
731
732 pa_sink_input_new_data_init(&data);
733 data.sink = o->sink;
734 data.driver = __FILE__;
735 data.name = t;
736 pa_sink_input_new_data_set_sample_spec(&data, &u->sink->sample_spec);
737 pa_sink_input_new_data_set_channel_map(&data, &u->sink->channel_map);
738 data.module = u->module;
739 data.resample_method = u->resample_method;
740
741 o->sink_input = pa_sink_input_new(u->core, &data, PA_SINK_INPUT_VARIABLE_RATE|PA_SINK_INPUT_DONT_MOVE);
742
743 pa_xfree(t);
744
745 if (!o->sink_input)
746 return -1;
747
748 o->sink_input->parent.process_msg = sink_input_process_msg;
749 o->sink_input->peek = sink_input_peek_cb;
750 o->sink_input->drop = sink_input_drop_cb;
751 o->sink_input->attach = sink_input_attach_cb;
752 o->sink_input->detach = sink_input_detach_cb;
753 o->sink_input->kill = sink_input_kill_cb;
754 o->sink_input->userdata = o;
755
756 return 0;
757 }
758
759 static struct output *output_new(struct userdata *u, pa_sink *sink) {
760 struct output *o;
761
762 pa_assert(u);
763 pa_assert(sink);
764 pa_assert(u->sink);
765
766 o = pa_xnew(struct output, 1);
767 o->userdata = u;
768 o->asyncmsgq = pa_asyncmsgq_new(0);
769 o->rtpoll_item = NULL;
770 o->sink = sink;
771 o->sink_input = NULL;
772 o->memblockq = pa_memblockq_new(
773 0,
774 MEMBLOCKQ_MAXLENGTH,
775 MEMBLOCKQ_MAXLENGTH,
776 pa_frame_size(&u->sink->sample_spec),
777 1,
778 0,
779 NULL);
780
781
782 pa_assert_se(pa_idxset_put(u->outputs, o, NULL) == 0);
783
784 update_description(u);
785
786 if (u->sink && PA_SINK_LINKED(pa_sink_get_state(u->sink)))
787 pa_asyncmsgq_send(u->sink->asyncmsgq, PA_MSGOBJECT(u->sink), SINK_MESSAGE_ADD_OUTPUT, o, 0, NULL);
788 else
789 PA_LLIST_PREPEND(struct output, u->thread_info.outputs, o);
790
791 if (PA_SINK_OPENED(pa_sink_get_state(u->sink)) || pa_sink_get_state(u->sink) == PA_SINK_INIT) {
792 pa_sink_suspend(sink, 0);
793
794 if (PA_SINK_OPENED(pa_sink_get_state(sink)))
795 if (output_create_sink_input(u, o) < 0)
796 goto fail;
797 }
798
799 return o;
800
801 fail:
802
803 if (o) {
804 if (o->sink_input) {
805 pa_sink_input_unlink(o->sink_input);
806 pa_sink_input_unref(o->sink_input);
807 }
808
809 if (o->memblockq)
810 pa_memblockq_free(o->memblockq);
811
812 if (o->asyncmsgq)
813 pa_asyncmsgq_unref(o->asyncmsgq);
814
815 pa_xfree(o);
816 }
817
818 return NULL;
819 }
820
821 static pa_hook_result_t sink_new_hook_cb(pa_core *c, pa_sink *s, struct userdata* u) {
822 struct output *o;
823
824 pa_core_assert_ref(c);
825 pa_sink_assert_ref(s);
826 pa_assert(u);
827 pa_assert(u->automatic);
828
829 if (!(s->flags & PA_SINK_HARDWARE) || s == u->sink)
830 return PA_HOOK_OK;
831
832 pa_log_info("Configuring new sink: %s", s->name);
833
834 if (!(o = output_new(u, s))) {
835 pa_log("Failed to create sink input on sink '%s'.", s->name);
836 return PA_HOOK_OK;
837 }
838
839 if (pick_master(u) < 0)
840 pa_module_unload_request(u->module);
841
842 if (o->sink_input)
843 pa_sink_input_put(o->sink_input);
844
845 return PA_HOOK_OK;
846 }
847
848 static pa_hook_result_t sink_unlink_hook_cb(pa_core *c, pa_sink *s, struct userdata* u) {
849 struct output *o;
850 uint32_t idx;
851
852 pa_assert(c);
853 pa_sink_assert_ref(s);
854 pa_assert(u);
855
856 if (s == u->sink)
857 return PA_HOOK_OK;
858
859 for (o = pa_idxset_first(u->outputs, &idx); o; o = pa_idxset_next(u->outputs, &idx))
860 if (o->sink == s)
861 break;
862
863 if (!o)
864 return PA_HOOK_OK;
865
866 pa_log_info("Unconfiguring sink: %s", s->name);
867
868 output_free(o);
869
870 if (pick_master(u) < 0)
871 pa_module_unload_request(u->module);
872
873 return PA_HOOK_OK;
874 }
875
876 static pa_hook_result_t sink_state_changed_hook_cb(pa_core *c, pa_sink *s, struct userdata* u) {
877 struct output *o;
878 uint32_t idx;
879 pa_sink_state_t state;
880
881 if (s == u->sink)
882 return PA_HOOK_OK;
883
884 for (o = pa_idxset_first(u->outputs, &idx); o; o = pa_idxset_next(u->outputs, &idx))
885 if (o->sink == s)
886 break;
887
888 if (!o)
889 return PA_HOOK_OK;
890
891 state = pa_sink_get_state(s);
892
893 if (PA_SINK_OPENED(state) && PA_SINK_OPENED(pa_sink_get_state(u->sink)) && !o->sink_input) {
894 output_create_sink_input(u, o);
895
896 if (pick_master(u) < 0)
897 pa_module_unload_request(u->module);
898
899 if (o->sink_input)
900 pa_sink_input_put(o->sink_input);
901 }
902
903 if (state == PA_SINK_SUSPENDED && o->sink_input) {
904 pa_sink_input_unlink(o->sink_input);
905 pa_sink_input_unref(o->sink_input);
906 o->sink_input = NULL;
907
908 pa_memblockq_flush(o->memblockq);
909
910 if (pick_master(u) < 0)
911 pa_module_unload_request(u->module);
912 }
913
914 return PA_HOOK_OK;
915 }
916
917 int pa__init(pa_module*m) {
918 struct userdata *u;
919 pa_modargs *ma = NULL;
920 const char *master_name, *slaves, *rm;
921 pa_sink *master_sink = NULL;
922 int resample_method = PA_RESAMPLER_TRIVIAL;
923 pa_sample_spec ss;
924 pa_channel_map map;
925 struct output *o;
926 uint32_t idx;
927
928 pa_assert(m);
929
930 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
931 pa_log("failed to parse module arguments");
932 goto fail;
933 }
934
935 if ((rm = pa_modargs_get_value(ma, "resample_method", NULL))) {
936 if ((resample_method = pa_parse_resample_method(rm)) < 0) {
937 pa_log("invalid resample method '%s'", rm);
938 goto fail;
939 }
940 }
941
942 u = pa_xnew(struct userdata, 1);
943 u->core = m->core;
944 u->module = m;
945 m->userdata = u;
946 u->sink = NULL;
947 u->thread_info.master = u->master = NULL;
948 u->time_event = NULL;
949 u->adjust_time = DEFAULT_ADJUST_TIME;
950 u->mutex = pa_mutex_new(FALSE, TRUE);
951 pa_thread_mq_init(&u->thread_mq, m->core->mainloop);
952 u->rtpoll = NULL;
953 u->thread = NULL;
954 PA_LLIST_HEAD_INIT(struct output, u->thread_info.outputs);
955 u->resample_method = resample_method;
956 u->outputs = pa_idxset_new(NULL, NULL);
957 pa_timespec_reset(&u->adjust_timestamp);
958
959 if (pa_modargs_get_value_u32(ma, "adjust_time", &u->adjust_time) < 0) {
960 pa_log("Failed to parse adjust_time value");
961 goto fail;
962 }
963
964 master_name = pa_modargs_get_value(ma, "master", NULL);
965 slaves = pa_modargs_get_value(ma, "slaves", NULL);
966 if (!master_name != !slaves) {
967 pa_log("No master or slave sinks specified");
968 goto fail;
969 }
970
971 if (master_name) {
972 if (!(master_sink = pa_namereg_get(m->core, master_name, PA_NAMEREG_SINK, 1))) {
973 pa_log("Invalid master sink '%s'", master_name);
974 goto fail;
975 }
976
977 ss = master_sink->sample_spec;
978 u->automatic = 0;
979 } else {
980 master_sink = NULL;
981 ss = m->core->default_sample_spec;
982 u->automatic = 1;
983 }
984
985 if ((pa_modargs_get_sample_spec(ma, &ss) < 0)) {
986 pa_log("Invalid sample specification.");
987 goto fail;
988 }
989
990 if (master_sink && ss.channels == master_sink->sample_spec.channels)
991 map = master_sink->channel_map;
992 else
993 pa_channel_map_init_auto(&map, ss.channels, PA_CHANNEL_MAP_DEFAULT);
994
995 if ((pa_modargs_get_channel_map(ma, NULL, &map) < 0)) {
996 pa_log("Invalid channel map.");
997 goto fail;
998 }
999
1000 if (ss.channels != map.channels) {
1001 pa_log("Channel map and sample specification don't match.");
1002 goto fail;
1003 }
1004
1005 if (!(u->sink = pa_sink_new(m->core, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, &map))) {
1006 pa_log("Failed to create sink");
1007 goto fail;
1008 }
1009
1010 u->sink->parent.process_msg = sink_process_msg;
1011 u->sink->get_latency = sink_get_latency_cb;
1012 u->sink->set_state = sink_set_state;
1013 u->sink->userdata = u;
1014
1015 u->sink->flags = PA_SINK_CAN_SUSPEND|PA_SINK_LATENCY;
1016 pa_sink_set_module(u->sink, m);
1017 pa_sink_set_description(u->sink, "Simultaneous output");
1018
1019 u->block_size = pa_bytes_per_second(&ss) / 20; /* 50 ms */
1020 if (u->block_size <= 0)
1021 u->block_size = pa_frame_size(&ss);
1022
1023 if (!u->automatic) {
1024 const char*split_state;
1025 char *n = NULL;
1026 pa_assert(slaves);
1027
1028 /* The master and slaves have been specified manually */
1029
1030 if (!(u->master = output_new(u, master_sink))) {
1031 pa_log("Failed to create master sink input on sink '%s'.", master_sink->name);
1032 goto fail;
1033 }
1034
1035 split_state = NULL;
1036 while ((n = pa_split(slaves, ",", &split_state))) {
1037 pa_sink *slave_sink;
1038
1039 if (!(slave_sink = pa_namereg_get(m->core, n, PA_NAMEREG_SINK, 1)) || slave_sink == u->sink) {
1040 pa_log("Invalid slave sink '%s'", n);
1041 pa_xfree(n);
1042 goto fail;
1043 }
1044
1045 pa_xfree(n);
1046
1047 if (!output_new(u, slave_sink)) {
1048 pa_log("Failed to create slave sink input on sink '%s'.", slave_sink->name);
1049 goto fail;
1050 }
1051 }
1052
1053 if (pa_idxset_size(u->outputs) <= 1)
1054 pa_log_warn("No slave sinks specified.");
1055
1056 u->sink_new_slot = NULL;
1057
1058 } else {
1059 pa_sink *s;
1060
1061 /* We're in automatic mode, we elect one hw sink to the master
1062 * and attach all other hw sinks as slaves to it */
1063
1064 for (s = pa_idxset_first(m->core->sinks, &idx); s; s = pa_idxset_next(m->core->sinks, &idx)) {
1065
1066 if (!(s->flags & PA_SINK_HARDWARE) || s == u->sink)
1067 continue;
1068
1069 if (!output_new(u, s)) {
1070 pa_log("Failed to create sink input on sink '%s'.", s->name);
1071 goto fail;
1072 }
1073 }
1074
1075 u->sink_new_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_NEW_POST], (pa_hook_cb_t) sink_new_hook_cb, u);
1076 }
1077
1078 u->sink_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_UNLINK], (pa_hook_cb_t) sink_unlink_hook_cb, u);
1079 u->sink_state_changed_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_STATE_CHANGED], (pa_hook_cb_t) sink_state_changed_hook_cb, u);
1080
1081 if (pick_master(u) < 0)
1082 goto fail;
1083
1084 /* Activate the sink and the sink inputs */
1085 pa_sink_put(u->sink);
1086
1087 for (o = pa_idxset_first(u->outputs, &idx); o; o = pa_idxset_next(u->outputs, &idx))
1088 if (o->sink_input)
1089 pa_sink_input_put(o->sink_input);
1090
1091 if (u->adjust_time > 0) {
1092 struct timeval tv;
1093 pa_gettimeofday(&tv);
1094 tv.tv_sec += u->adjust_time;
1095 u->time_event = m->core->mainloop->time_new(m->core->mainloop, &tv, time_callback, u);
1096 }
1097
1098 pa_modargs_free(ma);
1099
1100 return 0;
1101
1102 fail:
1103
1104 if (ma)
1105 pa_modargs_free(ma);
1106
1107 pa__done(m);
1108
1109 return -1;
1110 }
1111
1112 static void output_free(struct output *o) {
1113 pa_assert(o);
1114
1115 if (o->userdata) {
1116 if (o->userdata->sink && PA_SINK_LINKED(pa_sink_get_state(o->userdata->sink)))
1117 pa_asyncmsgq_send(o->userdata->sink->asyncmsgq, PA_MSGOBJECT(o->userdata->sink), SINK_MESSAGE_REMOVE_OUTPUT, o, 0, NULL);
1118 else
1119 PA_LLIST_REMOVE(struct output, o->userdata->thread_info.outputs, o);
1120 }
1121
1122 pa_assert_se(pa_idxset_remove_by_data(o->userdata->outputs, o, NULL));
1123
1124 if (o->userdata->master == o) {
1125 /* Make sure the master points to a different output */
1126 o->userdata->master = NULL;
1127 pick_master(o->userdata);
1128 }
1129
1130 update_description(o->userdata);
1131
1132 if (o->sink_input) {
1133 pa_sink_input_unlink(o->sink_input);
1134 pa_sink_input_unref(o->sink_input);
1135 }
1136
1137 if (o->rtpoll_item)
1138 pa_rtpoll_item_free(o->rtpoll_item);
1139
1140 if (o->memblockq)
1141 pa_memblockq_free(o->memblockq);
1142
1143 if (o->asyncmsgq)
1144 pa_asyncmsgq_unref(o->asyncmsgq);
1145
1146 pa_xfree(o);
1147 }
1148
1149 void pa__done(pa_module*m) {
1150 struct userdata *u;
1151 struct output *o;
1152
1153 pa_assert(m);
1154
1155 if (!(u = m->userdata))
1156 return;
1157
1158 if (u->sink_new_slot)
1159 pa_hook_slot_free(u->sink_new_slot);
1160
1161 if (u->sink_unlink_slot)
1162 pa_hook_slot_free(u->sink_unlink_slot);
1163
1164 if (u->sink_state_changed_slot)
1165 pa_hook_slot_free(u->sink_state_changed_slot);
1166
1167 if (u->sink)
1168 pa_sink_unlink(u->sink);
1169
1170 if (u->outputs) {
1171 while ((o = pa_idxset_first(u->outputs, NULL)))
1172 output_free(o);
1173
1174 pa_idxset_free(u->outputs, NULL, NULL);
1175 }
1176
1177 if (u->thread) {
1178 pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
1179 pa_thread_free(u->thread);
1180 }
1181
1182 pa_thread_mq_done(&u->thread_mq);
1183
1184 if (u->sink)
1185 pa_sink_unref(u->sink);
1186
1187 if (u->rtpoll)
1188 pa_rtpoll_free(u->rtpoll);
1189
1190 if (u->time_event)
1191 u->core->mainloop->time_free(u->time_event);
1192
1193 pa_mutex_free(u->mutex);
1194
1195 pa_xfree(u);
1196 }
1197
1198