4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public License
20 along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
33 #include <pulse/introspect.h>
34 #include <pulse/utf8.h>
35 #include <pulse/xmalloc.h>
37 #include <pulsecore/sink-input.h>
38 #include <pulsecore/namereg.h>
39 #include <pulsecore/core-util.h>
40 #include <pulsecore/sample-util.h>
41 #include <pulsecore/core-subscribe.h>
42 #include <pulsecore/log.h>
43 #include <pulsecore/macro.h>
44 #include <pulsecore/play-memblockq.h>
48 #define MAX_MIX_CHANNELS 32
49 #define MIX_BUFFER_LENGTH (PA_PAGE_SIZE)
50 #define SILENCE_BUFFER_LENGTH (PA_PAGE_SIZE*12)
52 static PA_DEFINE_CHECK_TYPE(pa_sink
, pa_msgobject
);
54 static void sink_free(pa_object
*s
);
61 const pa_sample_spec
*spec
,
62 const pa_channel_map
*map
) {
73 pa_return_null_if_fail(pa_sample_spec_valid(spec
));
76 map
= pa_channel_map_init_auto(&tmap
, spec
->channels
, PA_CHANNEL_MAP_DEFAULT
);
78 pa_return_null_if_fail(map
&& pa_channel_map_valid(map
));
79 pa_return_null_if_fail(map
->channels
== spec
->channels
);
80 pa_return_null_if_fail(!driver
|| pa_utf8_valid(driver
));
81 pa_return_null_if_fail(name
&& pa_utf8_valid(name
) && *name
);
83 s
= pa_msgobject_new(pa_sink
);
85 if (!(name
= pa_namereg_register(core
, name
, PA_NAMEREG_SINK
, s
, fail
))) {
90 s
->parent
.parent
.free
= sink_free
;
91 s
->parent
.process_msg
= pa_sink_process_msg
;
94 s
->state
= PA_SINK_INIT
;
96 s
->name
= pa_xstrdup(name
);
97 s
->description
= NULL
;
98 s
->driver
= pa_xstrdup(driver
);
101 s
->sample_spec
= *spec
;
102 s
->channel_map
= *map
;
104 s
->inputs
= pa_idxset_new(NULL
, NULL
);
107 pa_cvolume_reset(&s
->volume
, spec
->channels
);
109 s
->refresh_volume
= s
->refresh_mute
= FALSE
;
111 s
->get_latency
= NULL
;
112 s
->set_volume
= NULL
;
113 s
->get_volume
= NULL
;
123 pa_assert_se(pa_idxset_put(core
->sinks
, s
, &s
->index
) >= 0);
125 pa_sample_spec_snprint(st
, sizeof(st
), spec
);
126 pa_log_info("Created sink %u \"%s\" with sample spec \"%s\"", s
->index
, s
->name
, st
);
128 n
= pa_sprintf_malloc("%s.monitor", name
);
130 if (!(s
->monitor_source
= pa_source_new(core
, driver
, n
, 0, spec
, map
)))
131 pa_log_warn("Failed to create monitor source.");
134 s
->monitor_source
->monitor_of
= s
;
135 d
= pa_sprintf_malloc("Monitor Source of %s", s
->name
);
136 pa_source_set_description(s
->monitor_source
, d
);
142 s
->thread_info
.inputs
= pa_hashmap_new(pa_idxset_trivial_hash_func
, pa_idxset_trivial_compare_func
);
143 s
->thread_info
.soft_volume
= s
->volume
;
144 s
->thread_info
.soft_muted
= s
->muted
;
145 s
->thread_info
.state
= s
->state
;
150 void pa_sink_put(pa_sink
* s
) {
151 pa_sink_assert_ref(s
);
153 pa_assert(s
->state
== PA_SINK_INIT
);
154 pa_assert(s
->asyncmsgq
);
155 pa_assert(s
->rtpoll
);
157 s
->thread_info
.state
= s
->state
= PA_SINK_IDLE
;
159 pa_source_put(s
->monitor_source
);
161 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
| PA_SUBSCRIPTION_EVENT_NEW
, s
->index
);
162 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_NEW_POST
], s
);
165 static int sink_set_state(pa_sink
*s
, pa_sink_state_t state
) {
170 if (s
->state
== state
)
173 if (state
== PA_SINK_SUSPENDED
&& !(s
->flags
& PA_SINK_CAN_SUSPEND
))
176 if ((s
->state
== PA_SINK_SUSPENDED
&& PA_SINK_OPENED(state
)) ||
177 (PA_SINK_OPENED(s
->state
) && state
== PA_SINK_SUSPENDED
)) {
181 /* We're suspending or resuming, tell everyone about it */
183 for (i
= PA_SINK_INPUT(pa_idxset_first(s
->inputs
, &idx
)); i
; i
= PA_SINK_INPUT(pa_idxset_next(s
->inputs
, &idx
)))
185 i
->suspend(i
, state
== PA_SINK_SUSPENDED
);
189 if ((ret
= s
->set_state(s
, state
)) < 0)
192 if (pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_STATE
, PA_UINT_TO_PTR(state
), 0, NULL
) < 0)
197 if (state
!= PA_SINK_UNLINKED
) /* if we enter UNLINKED state pa_sink_unlink() will fire the apropriate events */
198 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_STATE_CHANGED
], s
);
202 void pa_sink_unlink(pa_sink
* s
) {
203 pa_sink_input
*i
, *j
= NULL
;
206 pa_assert(PA_SINK_LINKED(s
->state
));
208 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_UNLINK
], s
);
210 pa_namereg_unregister(s
->core
, s
->name
);
211 pa_idxset_remove_by_data(s
->core
->sinks
, s
, NULL
);
213 while ((i
= pa_idxset_first(s
->inputs
, NULL
))) {
215 pa_sink_input_kill(i
);
219 sink_set_state(s
, PA_SINK_UNLINKED
);
221 if (s
->monitor_source
)
222 pa_source_unlink(s
->monitor_source
);
224 s
->get_latency
= NULL
;
225 s
->get_volume
= NULL
;
226 s
->set_volume
= NULL
;
231 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
| PA_SUBSCRIPTION_EVENT_REMOVE
, s
->index
);
233 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_UNLINK_POST
], s
);
236 static void sink_free(pa_object
*o
) {
237 pa_sink
*s
= PA_SINK(o
);
241 pa_assert(pa_sink_refcnt(s
) == 0);
243 if (PA_SINK_LINKED(s
->state
))
246 pa_log_info("Freeing sink %u \"%s\"", s
->index
, s
->name
);
248 if (s
->monitor_source
) {
249 pa_source_unref(s
->monitor_source
);
250 s
->monitor_source
= NULL
;
253 pa_idxset_free(s
->inputs
, NULL
, NULL
);
255 while ((i
= pa_hashmap_steal_first(s
->thread_info
.inputs
)))
256 pa_sink_input_unref(i
);
258 pa_hashmap_free(s
->thread_info
.inputs
, NULL
, NULL
);
261 pa_memblock_unref(s
->silence
);
264 pa_xfree(s
->description
);
269 void pa_sink_set_asyncmsgq(pa_sink
*s
, pa_asyncmsgq
*q
) {
270 pa_sink_assert_ref(s
);
275 if (s
->monitor_source
)
276 pa_source_set_asyncmsgq(s
->monitor_source
, q
);
279 void pa_sink_set_rtpoll(pa_sink
*s
, pa_rtpoll
*p
) {
280 pa_sink_assert_ref(s
);
284 if (s
->monitor_source
)
285 pa_source_set_rtpoll(s
->monitor_source
, p
);
288 int pa_sink_update_status(pa_sink
*s
) {
289 pa_sink_assert_ref(s
);
290 pa_assert(PA_SINK_LINKED(s
->state
));
292 if (s
->state
== PA_SINK_SUSPENDED
)
295 return sink_set_state(s
, pa_sink_used_by(s
) ? PA_SINK_RUNNING
: PA_SINK_IDLE
);
298 int pa_sink_suspend(pa_sink
*s
, pa_bool_t suspend
) {
299 pa_sink_assert_ref(s
);
300 pa_assert(PA_SINK_LINKED(s
->state
));
303 return sink_set_state(s
, PA_SINK_SUSPENDED
);
305 return sink_set_state(s
, pa_sink_used_by(s
) ? PA_SINK_RUNNING
: PA_SINK_IDLE
);
308 void pa_sink_ping(pa_sink
*s
) {
309 pa_sink_assert_ref(s
);
310 pa_assert(PA_SINK_LINKED(s
->state
));
312 pa_asyncmsgq_post(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_PING
, NULL
, 0, NULL
, NULL
);
315 static unsigned fill_mix_info(pa_sink
*s
, size_t length
, pa_mix_info
*info
, unsigned maxinfo
) {
320 pa_sink_assert_ref(s
);
323 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)) && maxinfo
> 0) {
324 pa_sink_input_assert_ref(i
);
326 if (pa_sink_input_peek(i
, length
, &info
->chunk
, &info
->volume
) < 0)
329 info
->userdata
= pa_sink_input_ref(i
);
331 pa_assert(info
->chunk
.memblock
);
332 pa_assert(info
->chunk
.length
> 0);
342 static void inputs_drop(pa_sink
*s
, pa_mix_info
*info
, unsigned n
, size_t length
) {
346 unsigned n_unreffed
= 0;
348 pa_sink_assert_ref(s
);
350 /* We optimize for the case where the order of the inputs has not changed */
352 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
))) {
356 pa_sink_input_assert_ref(i
);
360 /* Let's try to find the matching entry info the pa_mix_info array */
361 for (j
= 0; j
< n
; j
++) {
363 if (info
[p
].userdata
== i
) {
374 pa_sink_input_drop(i
, length
);
377 pa_sink_input_unref(m
->userdata
);
379 if (m
->chunk
.memblock
)
380 pa_memblock_unref(m
->chunk
.memblock
);
381 pa_memchunk_reset(&m
->chunk
);
387 /* Now drop references to entries that are included in the
388 * pa_mix_info array but don't exist anymore */
390 if (n_unreffed
< n
) {
391 for (; n
> 0; info
++, n
--) {
393 pa_sink_input_unref(info
->userdata
);
394 if (info
->chunk
.memblock
)
395 pa_memblock_unref(info
->chunk
.memblock
);
400 void pa_sink_render(pa_sink
*s
, size_t length
, pa_memchunk
*result
) {
401 pa_mix_info info
[MAX_MIX_CHANNELS
];
403 size_t block_size_max
;
405 pa_sink_assert_ref(s
);
406 pa_assert(PA_SINK_OPENED(s
->thread_info
.state
));
407 pa_assert(pa_frame_aligned(length
, &s
->sample_spec
));
413 length
= pa_frame_align(MIX_BUFFER_LENGTH
, &s
->sample_spec
);
415 block_size_max
= pa_mempool_block_size_max(s
->core
->mempool
);
416 if (length
> block_size_max
)
417 length
= pa_frame_align(block_size_max
, &s
->sample_spec
);
419 pa_assert(length
> 0);
421 n
= s
->thread_info
.state
== PA_SINK_RUNNING
? fill_mix_info(s
, length
, info
, MAX_MIX_CHANNELS
) : 0;
425 if (length
> SILENCE_BUFFER_LENGTH
)
426 length
= pa_frame_align(SILENCE_BUFFER_LENGTH
, &s
->sample_spec
);
428 pa_assert(length
> 0);
430 if (!s
->silence
|| pa_memblock_get_length(s
->silence
) < length
) {
432 pa_memblock_unref(s
->silence
);
433 s
->silence
= pa_silence_memblock_new(s
->core
->mempool
, &s
->sample_spec
, length
);
436 result
->memblock
= pa_memblock_ref(s
->silence
);
437 result
->length
= length
;
443 *result
= info
[0].chunk
;
444 pa_memblock_ref(result
->memblock
);
446 if (result
->length
> length
)
447 result
->length
= length
;
449 pa_sw_cvolume_multiply(&volume
, &s
->thread_info
.soft_volume
, &info
[0].volume
);
451 if (s
->thread_info
.soft_muted
|| !pa_cvolume_is_norm(&volume
)) {
452 pa_memchunk_make_writable(result
, 0);
453 if (s
->thread_info
.soft_muted
|| pa_cvolume_is_muted(&volume
))
454 pa_silence_memchunk(result
, &s
->sample_spec
);
456 pa_volume_memchunk(result
, &s
->sample_spec
, &volume
);
460 result
->memblock
= pa_memblock_new(s
->core
->mempool
, length
);
462 ptr
= pa_memblock_acquire(result
->memblock
);
463 result
->length
= pa_mix(info
, n
, ptr
, length
, &s
->sample_spec
, &s
->thread_info
.soft_volume
, s
->thread_info
.soft_muted
);
464 pa_memblock_release(result
->memblock
);
469 if (s
->thread_info
.state
== PA_SINK_RUNNING
)
470 inputs_drop(s
, info
, n
, result
->length
);
472 if (s
->monitor_source
)
473 pa_source_post(s
->monitor_source
, result
);
478 void pa_sink_render_into(pa_sink
*s
, pa_memchunk
*target
) {
479 pa_mix_info info
[MAX_MIX_CHANNELS
];
482 pa_sink_assert_ref(s
);
483 pa_assert(PA_SINK_OPENED(s
->thread_info
.state
));
485 pa_assert(target
->memblock
);
486 pa_assert(target
->length
> 0);
487 pa_assert(pa_frame_aligned(target
->length
, &s
->sample_spec
));
491 n
= s
->thread_info
.state
== PA_SINK_RUNNING
? fill_mix_info(s
, target
->length
, info
, MAX_MIX_CHANNELS
) : 0;
494 pa_silence_memchunk(target
, &s
->sample_spec
);
496 if (target
->length
> info
[0].chunk
.length
)
497 target
->length
= info
[0].chunk
.length
;
499 if (s
->thread_info
.soft_muted
)
500 pa_silence_memchunk(target
, &s
->sample_spec
);
505 ptr
= pa_memblock_acquire(target
->memblock
);
506 src
= pa_memblock_acquire(info
[0].chunk
.memblock
);
508 memcpy((uint8_t*) ptr
+ target
->index
,
509 (uint8_t*) src
+ info
[0].chunk
.index
,
512 pa_memblock_release(target
->memblock
);
513 pa_memblock_release(info
[0].chunk
.memblock
);
515 pa_sw_cvolume_multiply(&volume
, &s
->thread_info
.soft_volume
, &info
[0].volume
);
517 if (!pa_cvolume_is_norm(&volume
))
518 pa_volume_memchunk(target
, &s
->sample_spec
, &volume
);
524 ptr
= pa_memblock_acquire(target
->memblock
);
526 target
->length
= pa_mix(info
, n
,
527 (uint8_t*) ptr
+ target
->index
,
530 &s
->thread_info
.soft_volume
,
531 s
->thread_info
.soft_muted
);
533 pa_memblock_release(target
->memblock
);
536 if (s
->thread_info
.state
== PA_SINK_RUNNING
)
537 inputs_drop(s
, info
, n
, target
->length
);
539 if (s
->monitor_source
)
540 pa_source_post(s
->monitor_source
, target
);
545 void pa_sink_render_into_full(pa_sink
*s
, pa_memchunk
*target
) {
549 pa_sink_assert_ref(s
);
550 pa_assert(PA_SINK_OPENED(s
->thread_info
.state
));
552 pa_assert(target
->memblock
);
553 pa_assert(target
->length
> 0);
554 pa_assert(pa_frame_aligned(target
->length
, &s
->sample_spec
));
565 pa_sink_render_into(s
, &chunk
);
574 void pa_sink_render_full(pa_sink
*s
, size_t length
, pa_memchunk
*result
) {
575 pa_sink_assert_ref(s
);
576 pa_assert(PA_SINK_OPENED(s
->thread_info
.state
));
577 pa_assert(length
> 0);
578 pa_assert(pa_frame_aligned(length
, &s
->sample_spec
));
581 /*** This needs optimization ***/
584 result
->length
= length
;
585 result
->memblock
= pa_memblock_new(s
->core
->mempool
, length
);
587 pa_sink_render_into_full(s
, result
);
590 void pa_sink_skip(pa_sink
*s
, size_t length
) {
594 pa_sink_assert_ref(s
);
595 pa_assert(PA_SINK_OPENED(s
->thread_info
.state
));
596 pa_assert(length
> 0);
597 pa_assert(pa_frame_aligned(length
, &s
->sample_spec
));
599 if (pa_source_used_by(s
->monitor_source
)) {
602 /* If something is connected to our monitor source, we have to
603 * pass valid data to it */
606 pa_sink_render(s
, length
, &chunk
);
607 pa_memblock_unref(chunk
.memblock
);
609 pa_assert(chunk
.length
<= length
);
610 length
-= chunk
.length
;
614 /* Ok, noone cares about the rendered data, so let's not even render it */
616 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
))) {
617 pa_sink_input_assert_ref(i
);
618 pa_sink_input_drop(i
, length
);
623 pa_usec_t
pa_sink_get_latency(pa_sink
*s
) {
626 pa_sink_assert_ref(s
);
627 pa_assert(PA_SINK_LINKED(s
->state
));
629 if (!PA_SINK_OPENED(s
->state
))
633 return s
->get_latency(s
);
635 if (pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_LATENCY
, &usec
, 0, NULL
) < 0)
641 void pa_sink_set_volume(pa_sink
*s
, const pa_cvolume
*volume
) {
644 pa_sink_assert_ref(s
);
645 pa_assert(PA_SINK_LINKED(s
->state
));
648 changed
= !pa_cvolume_equal(volume
, &s
->volume
);
651 if (s
->set_volume
&& s
->set_volume(s
) < 0)
652 s
->set_volume
= NULL
;
655 pa_asyncmsgq_post(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_VOLUME
, pa_xnewdup(struct pa_cvolume
, volume
, 1), 0, NULL
, pa_xfree
);
658 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
661 const pa_cvolume
*pa_sink_get_volume(pa_sink
*s
) {
662 struct pa_cvolume old_volume
;
664 pa_sink_assert_ref(s
);
665 pa_assert(PA_SINK_LINKED(s
->state
));
667 old_volume
= s
->volume
;
669 if (s
->get_volume
&& s
->get_volume(s
) < 0)
670 s
->get_volume
= NULL
;
672 if (!s
->get_volume
&& s
->refresh_volume
)
673 pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_VOLUME
, &s
->volume
, 0, NULL
);
675 if (!pa_cvolume_equal(&old_volume
, &s
->volume
))
676 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
681 void pa_sink_set_mute(pa_sink
*s
, pa_bool_t mute
) {
684 pa_sink_assert_ref(s
);
685 pa_assert(PA_SINK_LINKED(s
->state
));
687 changed
= s
->muted
!= mute
;
690 if (s
->set_mute
&& s
->set_mute(s
) < 0)
694 pa_asyncmsgq_post(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_MUTE
, PA_UINT_TO_PTR(mute
), 0, NULL
, NULL
);
697 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
700 pa_bool_t
pa_sink_get_mute(pa_sink
*s
) {
703 pa_sink_assert_ref(s
);
704 pa_assert(PA_SINK_LINKED(s
->state
));
706 old_muted
= s
->muted
;
708 if (s
->get_mute
&& s
->get_mute(s
) < 0)
711 if (!s
->get_mute
&& s
->refresh_mute
)
712 pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_MUTE
, &s
->muted
, 0, NULL
);
714 if (old_muted
!= s
->muted
)
715 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
720 void pa_sink_set_module(pa_sink
*s
, pa_module
*m
) {
721 pa_sink_assert_ref(s
);
728 if (s
->monitor_source
)
729 pa_source_set_module(s
->monitor_source
, m
);
731 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
734 void pa_sink_set_description(pa_sink
*s
, const char *description
) {
735 pa_sink_assert_ref(s
);
737 if (!description
&& !s
->description
)
740 if (description
&& s
->description
&& !strcmp(description
, s
->description
))
743 pa_xfree(s
->description
);
744 s
->description
= pa_xstrdup(description
);
746 if (s
->monitor_source
) {
749 n
= pa_sprintf_malloc("Monitor Source of %s", s
->description
? s
->description
: s
->name
);
750 pa_source_set_description(s
->monitor_source
, n
);
754 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
757 unsigned pa_sink_linked_by(pa_sink
*s
) {
760 pa_sink_assert_ref(s
);
761 pa_assert(PA_SINK_LINKED(s
->state
));
763 ret
= pa_idxset_size(s
->inputs
);
765 if (s
->monitor_source
)
766 ret
+= pa_source_used_by(s
->monitor_source
);
771 unsigned pa_sink_used_by(pa_sink
*s
) {
774 pa_sink_assert_ref(s
);
775 pa_assert(PA_SINK_LINKED(s
->state
));
777 ret
= pa_idxset_size(s
->inputs
);
779 pa_assert(ret
>= s
->n_corked
);
783 if (s
->monitor_source
)
784 ret
+= pa_source_used_by(s
->monitor_source
);
789 int pa_sink_process_msg(pa_msgobject
*o
, int code
, void *userdata
, int64_t offset
, pa_memchunk
*chunk
) {
790 pa_sink
*s
= PA_SINK(o
);
791 pa_sink_assert_ref(s
);
792 pa_assert(PA_SINK_LINKED(s
->thread_info
.state
));
794 switch ((pa_sink_message_t
) code
) {
796 case PA_SINK_MESSAGE_ADD_INPUT
: {
797 pa_sink_input
*i
= PA_SINK_INPUT(userdata
);
798 pa_hashmap_put(s
->thread_info
.inputs
, PA_UINT32_TO_PTR(i
->index
), pa_sink_input_ref(i
));
800 /* Since the caller sleeps in pa_sink_input_put(), we can
801 * safely access data outside of thread_info even though
804 if ((i
->thread_info
.sync_prev
= i
->sync_prev
)) {
805 pa_assert(i
->sink
== i
->thread_info
.sync_prev
->sink
);
806 pa_assert(i
->sync_prev
->sync_next
== i
);
807 i
->thread_info
.sync_prev
->thread_info
.sync_next
= i
;
810 if ((i
->thread_info
.sync_next
= i
->sync_next
)) {
811 pa_assert(i
->sink
== i
->thread_info
.sync_next
->sink
);
812 pa_assert(i
->sync_next
->sync_prev
== i
);
813 i
->thread_info
.sync_next
->thread_info
.sync_prev
= i
;
816 pa_assert(!i
->thread_info
.attached
);
817 i
->thread_info
.attached
= TRUE
;
825 case PA_SINK_MESSAGE_REMOVE_INPUT
: {
826 pa_sink_input
*i
= PA_SINK_INPUT(userdata
);
831 pa_assert(i
->thread_info
.attached
);
832 i
->thread_info
.attached
= FALSE
;
834 /* Since the caller sleeps in pa_sink_input_unlink(),
835 * we can safely access data outside of thread_info even
836 * though it is mutable */
838 pa_assert(!i
->thread_info
.sync_prev
);
839 pa_assert(!i
->thread_info
.sync_next
);
841 if (i
->thread_info
.sync_prev
) {
842 i
->thread_info
.sync_prev
->thread_info
.sync_next
= i
->thread_info
.sync_prev
->sync_next
;
843 i
->thread_info
.sync_prev
= NULL
;
846 if (i
->thread_info
.sync_next
) {
847 i
->thread_info
.sync_next
->thread_info
.sync_prev
= i
->thread_info
.sync_next
->sync_prev
;
848 i
->thread_info
.sync_next
= NULL
;
851 if (pa_hashmap_remove(s
->thread_info
.inputs
, PA_UINT32_TO_PTR(i
->index
)))
852 pa_sink_input_unref(i
);
857 case PA_SINK_MESSAGE_REMOVE_INPUT_AND_BUFFER
: {
858 pa_sink_input_move_info
*info
= userdata
;
861 /* We don't support moving synchronized streams. */
862 pa_assert(!info
->sink_input
->sync_prev
);
863 pa_assert(!info
->sink_input
->sync_next
);
864 pa_assert(!info
->sink_input
->thread_info
.sync_next
);
865 pa_assert(!info
->sink_input
->thread_info
.sync_prev
);
867 if (info
->ghost_sink_input
) {
868 pa_assert(info
->buffer_bytes
> 0);
869 pa_assert(info
->buffer
);
871 volume_is_norm
= pa_cvolume_is_norm(&info
->sink_input
->thread_info
.volume
);
873 pa_log_debug("Buffering %lu bytes ...", (unsigned long) info
->buffer_bytes
);
875 while (info
->buffer_bytes
> 0) {
876 pa_memchunk memchunk
;
880 if (pa_sink_input_peek(info
->sink_input
, info
->buffer_bytes
, &memchunk
, &volume
) < 0)
883 n
= memchunk
.length
> info
->buffer_bytes
? info
->buffer_bytes
: memchunk
.length
;
884 pa_sink_input_drop(info
->sink_input
, n
);
887 if (!volume_is_norm
) {
888 pa_memchunk_make_writable(&memchunk
, 0);
889 pa_volume_memchunk(&memchunk
, &s
->sample_spec
, &volume
);
892 if (pa_memblockq_push(info
->buffer
, &memchunk
) < 0) {
893 pa_memblock_unref(memchunk
.memblock
);
897 pa_memblock_unref(memchunk
.memblock
);
898 info
->buffer_bytes
-= n
;
901 /* Add the remaining already resampled chunk to the buffer */
902 if (info
->sink_input
->thread_info
.resampled_chunk
.memblock
)
903 pa_memblockq_push(info
->buffer
, &info
->sink_input
->thread_info
.resampled_chunk
);
905 pa_memblockq_sink_input_set_queue(info
->ghost_sink_input
, info
->buffer
);
907 pa_log_debug("Buffered %lu bytes ...", (unsigned long) pa_memblockq_get_length(info
->buffer
));
910 /* Let's remove the sink input ...*/
911 if (pa_hashmap_remove(s
->thread_info
.inputs
, PA_UINT32_TO_PTR(info
->sink_input
->index
)))
912 pa_sink_input_unref(info
->sink_input
);
914 /* .. and add the ghost sink input instead */
915 if (info
->ghost_sink_input
) {
916 pa_hashmap_put(s
->thread_info
.inputs
, PA_UINT32_TO_PTR(info
->ghost_sink_input
->index
), pa_sink_input_ref(info
->ghost_sink_input
));
917 info
->ghost_sink_input
->thread_info
.sync_prev
= info
->ghost_sink_input
->thread_info
.sync_next
= NULL
;
923 case PA_SINK_MESSAGE_SET_VOLUME
:
924 s
->thread_info
.soft_volume
= *((pa_cvolume
*) userdata
);
927 case PA_SINK_MESSAGE_SET_MUTE
:
928 s
->thread_info
.soft_muted
= PA_PTR_TO_UINT(userdata
);
931 case PA_SINK_MESSAGE_GET_VOLUME
:
932 *((pa_cvolume
*) userdata
) = s
->thread_info
.soft_volume
;
935 case PA_SINK_MESSAGE_GET_MUTE
:
936 *((pa_bool_t
*) userdata
) = s
->thread_info
.soft_muted
;
939 case PA_SINK_MESSAGE_PING
:
942 case PA_SINK_MESSAGE_SET_STATE
:
944 s
->thread_info
.state
= PA_PTR_TO_UINT(userdata
);
947 case PA_SINK_MESSAGE_DETACH
:
949 /* We're detaching all our input streams so that the
950 * asyncmsgq and rtpoll fields can be changed without
952 pa_sink_detach_within_thread(s
);
955 case PA_SINK_MESSAGE_ATTACH
:
957 /* Reattach all streams */
958 pa_sink_attach_within_thread(s
);
961 case PA_SINK_MESSAGE_GET_LATENCY
:
962 case PA_SINK_MESSAGE_MAX
:
969 int pa_sink_suspend_all(pa_core
*c
, pa_bool_t suspend
) {
974 pa_core_assert_ref(c
);
976 for (sink
= PA_SINK(pa_idxset_first(c
->sinks
, &idx
)); sink
; sink
= PA_SINK(pa_idxset_next(c
->sinks
, &idx
)))
977 ret
-= pa_sink_suspend(sink
, suspend
) < 0;
982 void pa_sink_detach(pa_sink
*s
) {
983 pa_sink_assert_ref(s
);
984 pa_assert(PA_SINK_LINKED(s
->state
));
986 pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_DETACH
, NULL
, 0, NULL
);
989 void pa_sink_attach(pa_sink
*s
) {
990 pa_sink_assert_ref(s
);
991 pa_assert(PA_SINK_LINKED(s
->state
));
993 pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_ATTACH
, NULL
, 0, NULL
);
996 void pa_sink_detach_within_thread(pa_sink
*s
) {
1000 pa_sink_assert_ref(s
);
1001 pa_assert(PA_SINK_LINKED(s
->thread_info
.state
));
1003 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)))
1007 if (s
->monitor_source
)
1008 pa_source_detach_within_thread(s
->monitor_source
);
1011 void pa_sink_attach_within_thread(pa_sink
*s
) {
1015 pa_sink_assert_ref(s
);
1016 pa_assert(PA_SINK_LINKED(s
->thread_info
.state
));
1018 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)))
1022 if (s
->monitor_source
)
1023 pa_source_attach_within_thread(s
->monitor_source
);