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 SILENCE_BUFFER_LENGTH (64*1024)
51 static PA_DEFINE_CHECK_TYPE(pa_sink
, pa_msgobject
);
53 static void sink_free(pa_object
*s
);
60 const pa_sample_spec
*spec
,
61 const pa_channel_map
*map
) {
72 pa_return_null_if_fail(pa_sample_spec_valid(spec
));
75 map
= pa_channel_map_init_auto(&tmap
, spec
->channels
, PA_CHANNEL_MAP_DEFAULT
);
77 pa_return_null_if_fail(map
&& pa_channel_map_valid(map
));
78 pa_return_null_if_fail(map
->channels
== spec
->channels
);
79 pa_return_null_if_fail(!driver
|| pa_utf8_valid(driver
));
80 pa_return_null_if_fail(name
&& pa_utf8_valid(name
) && *name
);
82 s
= pa_msgobject_new(pa_sink
);
84 if (!(name
= pa_namereg_register(core
, name
, PA_NAMEREG_SINK
, s
, fail
))) {
89 s
->parent
.parent
.free
= sink_free
;
90 s
->parent
.process_msg
= pa_sink_process_msg
;
93 s
->state
= PA_SINK_INIT
;
95 s
->name
= pa_xstrdup(name
);
96 s
->description
= NULL
;
97 s
->driver
= pa_xstrdup(driver
);
100 s
->sample_spec
= *spec
;
101 s
->channel_map
= *map
;
103 s
->inputs
= pa_idxset_new(NULL
, NULL
);
105 pa_cvolume_reset(&s
->volume
, spec
->channels
);
107 s
->refresh_volume
= s
->refresh_mute
= 0;
109 s
->get_latency
= NULL
;
110 s
->set_volume
= NULL
;
111 s
->get_volume
= NULL
;
121 pa_assert_se(pa_idxset_put(core
->sinks
, s
, &s
->index
) >= 0);
123 pa_sample_spec_snprint(st
, sizeof(st
), spec
);
124 pa_log_info("Created sink %u \"%s\" with sample spec \"%s\"", s
->index
, s
->name
, st
);
126 n
= pa_sprintf_malloc("%s.monitor", name
);
128 if (!(s
->monitor_source
= pa_source_new(core
, driver
, n
, 0, spec
, map
)))
129 pa_log_warn("Failed to create monitor source.");
132 s
->monitor_source
->monitor_of
= s
;
133 d
= pa_sprintf_malloc("Monitor Source of %s", s
->name
);
134 pa_source_set_description(s
->monitor_source
, d
);
140 s
->thread_info
.inputs
= pa_hashmap_new(pa_idxset_trivial_hash_func
, pa_idxset_trivial_compare_func
);
141 s
->thread_info
.soft_volume
= s
->volume
;
142 s
->thread_info
.soft_muted
= s
->muted
;
143 s
->thread_info
.state
= s
->state
;
148 void pa_sink_put(pa_sink
* s
) {
149 pa_sink_assert_ref(s
);
151 pa_assert(s
->state
== PA_SINK_INIT
);
152 pa_assert(s
->asyncmsgq
);
153 pa_assert(s
->rtpoll
);
155 s
->thread_info
.state
= s
->state
= PA_SINK_IDLE
;
157 pa_source_put(s
->monitor_source
);
159 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
| PA_SUBSCRIPTION_EVENT_NEW
, s
->index
);
160 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_NEW_POST
], s
);
163 static int sink_set_state(pa_sink
*s
, pa_sink_state_t state
) {
168 if (s
->state
== state
)
171 if (state
== PA_SINK_SUSPENDED
&& !(s
->flags
& PA_SINK_CAN_SUSPEND
))
174 if ((s
->state
== PA_SINK_SUSPENDED
&& PA_SINK_OPENED(state
)) ||
175 (PA_SINK_OPENED(s
->state
) && state
== PA_SINK_SUSPENDED
)) {
179 /* We're suspending or resuming, tell everyone about it */
181 for (i
= PA_SINK_INPUT(pa_idxset_first(s
->inputs
, &idx
)); i
; i
= PA_SINK_INPUT(pa_idxset_next(s
->inputs
, &idx
)))
183 i
->suspend(i
, state
== PA_SINK_SUSPENDED
);
187 if ((ret
= s
->set_state(s
, state
)) < 0)
190 if (pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_STATE
, PA_UINT_TO_PTR(state
), 0, NULL
) < 0)
195 if (state
!= PA_SINK_UNLINKED
) /* if we enter UNLINKED state pa_sink_unlink() will fire the apropriate events */
196 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_STATE_CHANGED
], s
);
200 void pa_sink_unlink(pa_sink
* s
) {
201 pa_sink_input
*i
, *j
= NULL
;
204 pa_assert(PA_SINK_LINKED(s
->state
));
206 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_UNLINK
], s
);
208 pa_namereg_unregister(s
->core
, s
->name
);
209 pa_idxset_remove_by_data(s
->core
->sinks
, s
, NULL
);
211 while ((i
= pa_idxset_first(s
->inputs
, NULL
))) {
213 pa_sink_input_kill(i
);
217 if (s
->monitor_source
)
218 pa_source_unlink(s
->monitor_source
);
220 sink_set_state(s
, PA_SINK_UNLINKED
);
222 s
->get_latency
= NULL
;
223 s
->get_volume
= NULL
;
224 s
->set_volume
= NULL
;
229 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
| PA_SUBSCRIPTION_EVENT_REMOVE
, s
->index
);
231 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_UNLINK_POST
], s
);
234 static void sink_free(pa_object
*o
) {
235 pa_sink
*s
= PA_SINK(o
);
239 pa_assert(pa_sink_refcnt(s
) == 0);
241 if (PA_SINK_LINKED(s
->state
))
244 pa_log_info("Freeing sink %u \"%s\"", s
->index
, s
->name
);
246 if (s
->monitor_source
) {
247 pa_source_unref(s
->monitor_source
);
248 s
->monitor_source
= NULL
;
251 pa_idxset_free(s
->inputs
, NULL
, NULL
);
253 while ((i
= pa_hashmap_steal_first(s
->thread_info
.inputs
)))
254 pa_sink_input_unref(i
);
256 pa_hashmap_free(s
->thread_info
.inputs
, NULL
, NULL
);
259 pa_memblock_unref(s
->silence
);
262 pa_xfree(s
->description
);
267 void pa_sink_set_asyncmsgq(pa_sink
*s
, pa_asyncmsgq
*q
) {
268 pa_sink_assert_ref(s
);
273 if (s
->monitor_source
)
274 pa_source_set_asyncmsgq(s
->monitor_source
, q
);
277 void pa_sink_set_rtpoll(pa_sink
*s
, pa_rtpoll
*p
) {
278 pa_sink_assert_ref(s
);
282 if (s
->monitor_source
)
283 pa_source_set_rtpoll(s
->monitor_source
, p
);
286 int pa_sink_update_status(pa_sink
*s
) {
287 pa_sink_assert_ref(s
);
288 pa_assert(PA_SINK_LINKED(s
->state
));
290 if (s
->state
== PA_SINK_SUSPENDED
)
293 return sink_set_state(s
, pa_sink_used_by(s
) ? PA_SINK_RUNNING
: PA_SINK_IDLE
);
296 int pa_sink_suspend(pa_sink
*s
, int suspend
) {
297 pa_sink_assert_ref(s
);
298 pa_assert(PA_SINK_LINKED(s
->state
));
301 return sink_set_state(s
, PA_SINK_SUSPENDED
);
303 return sink_set_state(s
, pa_sink_used_by(s
) ? PA_SINK_RUNNING
: PA_SINK_IDLE
);
306 void pa_sink_ping(pa_sink
*s
) {
307 pa_sink_assert_ref(s
);
308 pa_assert(PA_SINK_LINKED(s
->state
));
310 pa_asyncmsgq_post(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_PING
, NULL
, 0, NULL
, NULL
);
313 static unsigned fill_mix_info(pa_sink
*s
, pa_mix_info
*info
, unsigned maxinfo
) {
318 pa_sink_assert_ref(s
);
321 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)) && maxinfo
> 0) {
322 pa_sink_input_assert_ref(i
);
324 if (pa_sink_input_peek(i
, &info
->chunk
, &info
->volume
) < 0)
327 info
->userdata
= pa_sink_input_ref(i
);
329 pa_assert(info
->chunk
.memblock
);
330 pa_assert(info
->chunk
.length
> 0);
340 static void inputs_drop(pa_sink
*s
, pa_mix_info
*info
, unsigned n
, size_t length
) {
344 unsigned n_unreffed
= 0;
346 pa_sink_assert_ref(s
);
348 /* We optimize for the case where the order of the inputs has not changed */
350 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
))) {
354 pa_sink_input_assert_ref(i
);
358 /* Let's try to find the matching entry info the pa_mix_info array */
359 for (j
= 0; j
< n
; j
++) {
361 if (info
[p
].userdata
== i
) {
372 pa_sink_input_drop(i
, length
);
375 pa_sink_input_unref(m
->userdata
);
377 if (m
->chunk
.memblock
)
378 pa_memblock_unref(m
->chunk
.memblock
);
379 pa_memchunk_reset(&m
->chunk
);
385 /* Now drop references to entries that are included in the
386 * pa_mix_info array but don't exist anymore */
388 if (n_unreffed
< n
) {
389 for (; n
> 0; info
++, n
--) {
391 pa_sink_input_unref(info
->userdata
);
392 if (info
->chunk
.memblock
)
393 pa_memblock_unref(info
->chunk
.memblock
);
398 void pa_sink_render(pa_sink
*s
, size_t length
, pa_memchunk
*result
) {
399 pa_mix_info info
[MAX_MIX_CHANNELS
];
402 pa_sink_assert_ref(s
);
403 pa_assert(PA_SINK_OPENED(s
->thread_info
.state
));
409 n
= s
->thread_info
.state
== PA_SINK_RUNNING
? fill_mix_info(s
, info
, MAX_MIX_CHANNELS
) : 0;
413 if (length
> SILENCE_BUFFER_LENGTH
)
414 length
= SILENCE_BUFFER_LENGTH
;
416 if (!s
->silence
|| pa_memblock_get_length(s
->silence
) < length
) {
418 pa_memblock_unref(s
->silence
);
419 s
->silence
= pa_silence_memblock_new(s
->core
->mempool
, &s
->sample_spec
, length
);
422 result
->memblock
= pa_memblock_ref(s
->silence
);
423 result
->length
= length
;
429 *result
= info
[0].chunk
;
430 pa_memblock_ref(result
->memblock
);
432 if (result
->length
> length
)
433 result
->length
= length
;
435 pa_sw_cvolume_multiply(&volume
, &s
->thread_info
.soft_volume
, &info
[0].volume
);
437 if (s
->thread_info
.soft_muted
|| !pa_cvolume_is_norm(&volume
)) {
438 pa_memchunk_make_writable(result
, 0);
439 if (s
->thread_info
.soft_muted
|| pa_cvolume_is_muted(&volume
))
440 pa_silence_memchunk(result
, &s
->sample_spec
);
442 pa_volume_memchunk(result
, &s
->sample_spec
, &volume
);
446 result
->memblock
= pa_memblock_new(s
->core
->mempool
, length
);
448 ptr
= pa_memblock_acquire(result
->memblock
);
449 result
->length
= pa_mix(info
, n
, ptr
, length
, &s
->sample_spec
, &s
->thread_info
.soft_volume
, s
->thread_info
.soft_muted
);
450 pa_memblock_release(result
->memblock
);
455 if (s
->thread_info
.state
== PA_SINK_RUNNING
)
456 inputs_drop(s
, info
, n
, result
->length
);
458 if (s
->monitor_source
)
459 pa_source_post(s
->monitor_source
, result
);
464 void pa_sink_render_into(pa_sink
*s
, pa_memchunk
*target
) {
465 pa_mix_info info
[MAX_MIX_CHANNELS
];
468 pa_sink_assert_ref(s
);
469 pa_assert(PA_SINK_OPENED(s
->thread_info
.state
));
471 pa_assert(target
->memblock
);
472 pa_assert(target
->length
);
476 n
= s
->thread_info
.state
== PA_SINK_RUNNING
? fill_mix_info(s
, info
, MAX_MIX_CHANNELS
) : 0;
479 pa_silence_memchunk(target
, &s
->sample_spec
);
481 if (target
->length
> info
[0].chunk
.length
)
482 target
->length
= info
[0].chunk
.length
;
484 if (s
->thread_info
.soft_muted
)
485 pa_silence_memchunk(target
, &s
->sample_spec
);
490 ptr
= pa_memblock_acquire(target
->memblock
);
491 src
= pa_memblock_acquire(info
[0].chunk
.memblock
);
493 memcpy((uint8_t*) ptr
+ target
->index
,
494 (uint8_t*) src
+ info
[0].chunk
.index
,
497 pa_memblock_release(target
->memblock
);
498 pa_memblock_release(info
[0].chunk
.memblock
);
500 pa_sw_cvolume_multiply(&volume
, &s
->thread_info
.soft_volume
, &info
[0].volume
);
502 if (!pa_cvolume_is_norm(&volume
))
503 pa_volume_memchunk(target
, &s
->sample_spec
, &volume
);
509 ptr
= pa_memblock_acquire(target
->memblock
);
511 target
->length
= pa_mix(info
, n
,
512 (uint8_t*) ptr
+ target
->index
,
515 &s
->thread_info
.soft_volume
,
516 s
->thread_info
.soft_muted
);
518 pa_memblock_release(target
->memblock
);
521 if (s
->thread_info
.state
== PA_SINK_RUNNING
)
522 inputs_drop(s
, info
, n
, target
->length
);
524 if (s
->monitor_source
)
525 pa_source_post(s
->monitor_source
, target
);
530 void pa_sink_render_into_full(pa_sink
*s
, pa_memchunk
*target
) {
534 pa_sink_assert_ref(s
);
535 pa_assert(PA_SINK_OPENED(s
->thread_info
.state
));
537 pa_assert(target
->memblock
);
538 pa_assert(target
->length
);
549 pa_sink_render_into(s
, &chunk
);
558 void pa_sink_render_full(pa_sink
*s
, size_t length
, pa_memchunk
*result
) {
559 pa_sink_assert_ref(s
);
560 pa_assert(PA_SINK_OPENED(s
->thread_info
.state
));
564 /*** This needs optimization ***/
566 result
->memblock
= pa_memblock_new(s
->core
->mempool
, result
->length
= length
);
569 pa_sink_render_into_full(s
, result
);
572 void pa_sink_skip(pa_sink
*s
, size_t length
) {
576 pa_sink_assert_ref(s
);
577 pa_assert(PA_SINK_OPENED(s
->thread_info
.state
));
578 pa_assert(length
> 0);
580 if (pa_source_used_by(s
->monitor_source
)) {
583 /* If something is connected to our monitor source, we have to
584 * pass valid data to it */
587 pa_sink_render(s
, length
, &chunk
);
588 pa_memblock_unref(chunk
.memblock
);
590 pa_assert(chunk
.length
<= length
);
591 length
-= chunk
.length
;
595 /* Ok, noone cares about the rendered data, so let's not even render it */
597 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
))) {
598 pa_sink_input_assert_ref(i
);
599 pa_sink_input_drop(i
, length
);
604 pa_usec_t
pa_sink_get_latency(pa_sink
*s
) {
607 pa_sink_assert_ref(s
);
608 pa_assert(PA_SINK_LINKED(s
->state
));
610 if (!PA_SINK_OPENED(s
->state
))
614 return s
->get_latency(s
);
616 if (pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_LATENCY
, &usec
, 0, NULL
) < 0)
622 void pa_sink_set_volume(pa_sink
*s
, const pa_cvolume
*volume
) {
625 pa_sink_assert_ref(s
);
626 pa_assert(PA_SINK_LINKED(s
->state
));
629 changed
= !pa_cvolume_equal(volume
, &s
->volume
);
632 if (s
->set_volume
&& s
->set_volume(s
) < 0)
633 s
->set_volume
= NULL
;
636 pa_asyncmsgq_post(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_VOLUME
, pa_xnewdup(struct pa_cvolume
, volume
, 1), 0, NULL
, pa_xfree
);
639 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
642 const pa_cvolume
*pa_sink_get_volume(pa_sink
*s
) {
643 struct pa_cvolume old_volume
;
645 pa_sink_assert_ref(s
);
646 pa_assert(PA_SINK_LINKED(s
->state
));
648 old_volume
= s
->volume
;
650 if (s
->get_volume
&& s
->get_volume(s
) < 0)
651 s
->get_volume
= NULL
;
653 if (!s
->get_volume
&& s
->refresh_volume
)
654 pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_VOLUME
, &s
->volume
, 0, NULL
);
656 if (!pa_cvolume_equal(&old_volume
, &s
->volume
))
657 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
662 void pa_sink_set_mute(pa_sink
*s
, int mute
) {
665 pa_sink_assert_ref(s
);
666 pa_assert(PA_SINK_LINKED(s
->state
));
668 changed
= s
->muted
!= mute
;
671 if (s
->set_mute
&& s
->set_mute(s
) < 0)
675 pa_asyncmsgq_post(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_MUTE
, PA_UINT_TO_PTR(mute
), 0, NULL
, NULL
);
678 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
681 int pa_sink_get_mute(pa_sink
*s
) {
684 pa_sink_assert_ref(s
);
685 pa_assert(PA_SINK_LINKED(s
->state
));
687 old_muted
= s
->muted
;
689 if (s
->get_mute
&& s
->get_mute(s
) < 0)
692 if (!s
->get_mute
&& s
->refresh_mute
)
693 pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_MUTE
, &s
->muted
, 0, NULL
);
695 if (old_muted
!= s
->muted
)
696 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
701 void pa_sink_set_module(pa_sink
*s
, pa_module
*m
) {
702 pa_sink_assert_ref(s
);
709 if (s
->monitor_source
)
710 pa_source_set_module(s
->monitor_source
, m
);
712 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
715 void pa_sink_set_description(pa_sink
*s
, const char *description
) {
716 pa_sink_assert_ref(s
);
718 if (!description
&& !s
->description
)
721 if (description
&& s
->description
&& !strcmp(description
, s
->description
))
724 pa_xfree(s
->description
);
725 s
->description
= pa_xstrdup(description
);
727 if (s
->monitor_source
) {
730 n
= pa_sprintf_malloc("Monitor Source of %s", s
->description
? s
->description
: s
->name
);
731 pa_source_set_description(s
->monitor_source
, n
);
735 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
738 unsigned pa_sink_used_by(pa_sink
*s
) {
741 pa_sink_assert_ref(s
);
742 pa_assert(PA_SINK_LINKED(s
->state
));
744 ret
= pa_idxset_size(s
->inputs
);
746 if (s
->monitor_source
)
747 ret
+= pa_source_used_by(s
->monitor_source
);
752 int pa_sink_process_msg(pa_msgobject
*o
, int code
, void *userdata
, int64_t offset
, pa_memchunk
*chunk
) {
753 pa_sink
*s
= PA_SINK(o
);
754 pa_sink_assert_ref(s
);
755 pa_assert(PA_SINK_LINKED(s
->thread_info
.state
));
757 switch ((pa_sink_message_t
) code
) {
759 case PA_SINK_MESSAGE_ADD_INPUT
: {
760 pa_sink_input
*i
= PA_SINK_INPUT(userdata
);
761 pa_hashmap_put(s
->thread_info
.inputs
, PA_UINT32_TO_PTR(i
->index
), pa_sink_input_ref(i
));
763 /* Since the caller sleeps in pa_sink_input_put(), we can
764 * safely access data outside of thread_info even though
767 if ((i
->thread_info
.sync_prev
= i
->sync_prev
)) {
768 pa_assert(i
->sink
== i
->thread_info
.sync_prev
->sink
);
769 pa_assert(i
->sync_prev
->sync_next
== i
);
770 i
->thread_info
.sync_prev
->thread_info
.sync_next
= i
;
773 if ((i
->thread_info
.sync_next
= i
->sync_next
)) {
774 pa_assert(i
->sink
== i
->thread_info
.sync_next
->sink
);
775 pa_assert(i
->sync_next
->sync_prev
== i
);
776 i
->thread_info
.sync_next
->thread_info
.sync_prev
= i
;
785 case PA_SINK_MESSAGE_REMOVE_INPUT
: {
786 pa_sink_input
*i
= PA_SINK_INPUT(userdata
);
791 /* Since the caller sleeps in pa_sink_input_unlink(),
792 * we can safely access data outside of thread_info even
793 * though it is mutable */
795 pa_assert(!i
->thread_info
.sync_prev
);
796 pa_assert(!i
->thread_info
.sync_next
);
798 if (i
->thread_info
.sync_prev
) {
799 i
->thread_info
.sync_prev
->thread_info
.sync_next
= i
->thread_info
.sync_prev
->sync_next
;
800 i
->thread_info
.sync_prev
= NULL
;
803 if (i
->thread_info
.sync_next
) {
804 i
->thread_info
.sync_next
->thread_info
.sync_prev
= i
->thread_info
.sync_next
->sync_prev
;
805 i
->thread_info
.sync_next
= NULL
;
808 if (pa_hashmap_remove(s
->thread_info
.inputs
, PA_UINT32_TO_PTR(i
->index
)))
809 pa_sink_input_unref(i
);
814 case PA_SINK_MESSAGE_REMOVE_INPUT_AND_BUFFER
: {
815 pa_sink_input_move_info
*info
= userdata
;
818 /* We don't support moving synchronized streams. */
819 pa_assert(!info
->sink_input
->sync_prev
);
820 pa_assert(!info
->sink_input
->sync_next
);
821 pa_assert(!info
->sink_input
->thread_info
.sync_next
);
822 pa_assert(!info
->sink_input
->thread_info
.sync_prev
);
824 if (info
->ghost_sink_input
) {
825 pa_assert(info
->buffer_bytes
> 0);
826 pa_assert(info
->buffer
);
828 volume_is_norm
= pa_cvolume_is_norm(&info
->sink_input
->thread_info
.volume
);
830 pa_log_debug("Buffering %lu bytes ...", (unsigned long) info
->buffer_bytes
);
832 while (info
->buffer_bytes
> 0) {
833 pa_memchunk memchunk
;
837 if (pa_sink_input_peek(info
->sink_input
, &memchunk
, &volume
) < 0)
840 n
= memchunk
.length
> info
->buffer_bytes
? info
->buffer_bytes
: memchunk
.length
;
841 pa_sink_input_drop(info
->sink_input
, n
);
844 if (!volume_is_norm
) {
845 pa_memchunk_make_writable(&memchunk
, 0);
846 pa_volume_memchunk(&memchunk
, &s
->sample_spec
, &volume
);
849 if (pa_memblockq_push(info
->buffer
, &memchunk
) < 0) {
850 pa_memblock_unref(memchunk
.memblock
);
854 pa_memblock_unref(memchunk
.memblock
);
855 info
->buffer_bytes
-= n
;
858 /* Add the remaining already resampled chunk to the buffer */
859 if (info
->sink_input
->thread_info
.resampled_chunk
.memblock
)
860 pa_memblockq_push(info
->buffer
, &info
->sink_input
->thread_info
.resampled_chunk
);
862 pa_memblockq_sink_input_set_queue(info
->ghost_sink_input
, info
->buffer
);
864 pa_log_debug("Buffered %lu bytes ...", (unsigned long) pa_memblockq_get_length(info
->buffer
));
867 /* Let's remove the sink input ...*/
868 if (pa_hashmap_remove(s
->thread_info
.inputs
, PA_UINT32_TO_PTR(info
->sink_input
->index
)))
869 pa_sink_input_unref(info
->sink_input
);
871 /* .. and add the ghost sink input instead */
872 if (info
->ghost_sink_input
) {
873 pa_hashmap_put(s
->thread_info
.inputs
, PA_UINT32_TO_PTR(info
->ghost_sink_input
->index
), pa_sink_input_ref(info
->ghost_sink_input
));
874 info
->ghost_sink_input
->thread_info
.sync_prev
= info
->ghost_sink_input
->thread_info
.sync_next
= NULL
;
880 case PA_SINK_MESSAGE_SET_VOLUME
:
881 s
->thread_info
.soft_volume
= *((pa_cvolume
*) userdata
);
884 case PA_SINK_MESSAGE_SET_MUTE
:
885 s
->thread_info
.soft_muted
= PA_PTR_TO_UINT(userdata
);
888 case PA_SINK_MESSAGE_GET_VOLUME
:
889 *((pa_cvolume
*) userdata
) = s
->thread_info
.soft_volume
;
892 case PA_SINK_MESSAGE_GET_MUTE
:
893 *((int*) userdata
) = s
->thread_info
.soft_muted
;
896 case PA_SINK_MESSAGE_PING
:
899 case PA_SINK_MESSAGE_SET_STATE
:
901 s
->thread_info
.state
= PA_PTR_TO_UINT(userdata
);
904 case PA_SINK_MESSAGE_GET_LATENCY
:
905 case PA_SINK_MESSAGE_MAX
:
912 int pa_sink_suspend_all(pa_core
*c
, int suspend
) {
917 pa_core_assert_ref(c
);
919 for (sink
= PA_SINK(pa_idxset_first(c
->sinks
, &idx
)); sink
; sink
= PA_SINK(pa_idxset_next(c
->sinks
, &idx
)))
920 ret
-= pa_sink_suspend(sink
, suspend
) < 0;