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 pa_return_null_if_fail((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 static int sink_set_state(pa_sink
*s
, pa_sink_state_t state
) {
152 pa_bool_t suspend_change
;
156 if (s
->state
== state
)
160 (s
->state
== PA_SINK_SUSPENDED
&& PA_SINK_OPENED(state
)) ||
161 (PA_SINK_OPENED(s
->state
) && state
== PA_SINK_SUSPENDED
);
164 if ((ret
= s
->set_state(s
, state
)) < 0)
167 if (pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_STATE
, PA_UINT_TO_PTR(state
), 0, NULL
) < 0)
172 if (suspend_change
) {
176 /* We're suspending or resuming, tell everyone about it */
178 for (i
= PA_SINK_INPUT(pa_idxset_first(s
->inputs
, &idx
)); i
; i
= PA_SINK_INPUT(pa_idxset_next(s
->inputs
, &idx
)))
180 i
->suspend(i
, state
== PA_SINK_SUSPENDED
);
183 if (state
!= PA_SINK_UNLINKED
) /* if we enter UNLINKED state pa_sink_unlink() will fire the apropriate events */
184 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_STATE_CHANGED
], s
);
189 void pa_sink_put(pa_sink
* s
) {
190 pa_sink_assert_ref(s
);
192 pa_assert(s
->state
== PA_SINK_INIT
);
193 pa_assert(s
->asyncmsgq
);
194 pa_assert(s
->rtpoll
);
196 pa_assert_se(sink_set_state(s
, PA_SINK_IDLE
) == 0);
198 pa_source_put(s
->monitor_source
);
200 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
| PA_SUBSCRIPTION_EVENT_NEW
, s
->index
);
201 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_NEW_POST
], s
);
204 void pa_sink_unlink(pa_sink
* s
) {
206 pa_sink_input
*i
, *j
= NULL
;
210 /* Please note that pa_sink_unlink() does more than simply
211 * reversing pa_sink_put(). It also undoes the registrations
212 * already done in pa_sink_new()! */
214 /* All operations here shall be idempotent, i.e. pa_sink_unlink()
215 * may be called multiple times on the same sink without bad
218 linked
= PA_SINK_LINKED(s
->state
);
221 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_UNLINK
], s
);
223 if (s
->state
!= PA_SINK_UNLINKED
)
224 pa_namereg_unregister(s
->core
, s
->name
);
225 pa_idxset_remove_by_data(s
->core
->sinks
, s
, NULL
);
227 while ((i
= pa_idxset_first(s
->inputs
, NULL
))) {
229 pa_sink_input_kill(i
);
234 sink_set_state(s
, PA_SINK_UNLINKED
);
236 s
->state
= PA_SINK_UNLINKED
;
238 s
->get_latency
= NULL
;
239 s
->get_volume
= NULL
;
240 s
->set_volume
= NULL
;
245 if (s
->monitor_source
)
246 pa_source_unlink(s
->monitor_source
);
249 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
| PA_SUBSCRIPTION_EVENT_REMOVE
, s
->index
);
250 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_UNLINK_POST
], s
);
254 static void sink_free(pa_object
*o
) {
255 pa_sink
*s
= PA_SINK(o
);
259 pa_assert(pa_sink_refcnt(s
) == 0);
261 if (PA_SINK_LINKED(s
->state
))
264 pa_log_info("Freeing sink %u \"%s\"", s
->index
, s
->name
);
266 if (s
->monitor_source
) {
267 pa_source_unref(s
->monitor_source
);
268 s
->monitor_source
= NULL
;
271 pa_idxset_free(s
->inputs
, NULL
, NULL
);
273 while ((i
= pa_hashmap_steal_first(s
->thread_info
.inputs
)))
274 pa_sink_input_unref(i
);
276 pa_hashmap_free(s
->thread_info
.inputs
, NULL
, NULL
);
279 pa_memblock_unref(s
->silence
);
282 pa_xfree(s
->description
);
287 void pa_sink_set_asyncmsgq(pa_sink
*s
, pa_asyncmsgq
*q
) {
288 pa_sink_assert_ref(s
);
293 if (s
->monitor_source
)
294 pa_source_set_asyncmsgq(s
->monitor_source
, q
);
297 void pa_sink_set_rtpoll(pa_sink
*s
, pa_rtpoll
*p
) {
298 pa_sink_assert_ref(s
);
302 if (s
->monitor_source
)
303 pa_source_set_rtpoll(s
->monitor_source
, p
);
306 int pa_sink_update_status(pa_sink
*s
) {
307 pa_sink_assert_ref(s
);
308 pa_assert(PA_SINK_LINKED(s
->state
));
310 if (s
->state
== PA_SINK_SUSPENDED
)
313 return sink_set_state(s
, pa_sink_used_by(s
) ? PA_SINK_RUNNING
: PA_SINK_IDLE
);
316 int pa_sink_suspend(pa_sink
*s
, pa_bool_t suspend
) {
317 pa_sink_assert_ref(s
);
318 pa_assert(PA_SINK_LINKED(s
->state
));
321 return sink_set_state(s
, PA_SINK_SUSPENDED
);
323 return sink_set_state(s
, pa_sink_used_by(s
) ? PA_SINK_RUNNING
: PA_SINK_IDLE
);
326 void pa_sink_ping(pa_sink
*s
) {
327 pa_sink_assert_ref(s
);
328 pa_assert(PA_SINK_LINKED(s
->state
));
330 pa_asyncmsgq_post(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_PING
, NULL
, 0, NULL
, NULL
);
333 static unsigned fill_mix_info(pa_sink
*s
, size_t length
, pa_mix_info
*info
, unsigned maxinfo
) {
338 pa_sink_assert_ref(s
);
341 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)) && maxinfo
> 0) {
342 pa_sink_input_assert_ref(i
);
344 if (pa_sink_input_peek(i
, length
, &info
->chunk
, &info
->volume
) < 0)
347 info
->userdata
= pa_sink_input_ref(i
);
349 pa_assert(info
->chunk
.memblock
);
350 pa_assert(info
->chunk
.length
> 0);
360 static void inputs_drop(pa_sink
*s
, pa_mix_info
*info
, unsigned n
, size_t length
) {
364 unsigned n_unreffed
= 0;
366 pa_sink_assert_ref(s
);
368 /* We optimize for the case where the order of the inputs has not changed */
370 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
))) {
374 pa_sink_input_assert_ref(i
);
378 /* Let's try to find the matching entry info the pa_mix_info array */
379 for (j
= 0; j
< n
; j
++) {
381 if (info
[p
].userdata
== i
) {
392 pa_sink_input_drop(i
, length
);
395 pa_sink_input_unref(m
->userdata
);
397 if (m
->chunk
.memblock
)
398 pa_memblock_unref(m
->chunk
.memblock
);
399 pa_memchunk_reset(&m
->chunk
);
405 /* Now drop references to entries that are included in the
406 * pa_mix_info array but don't exist anymore */
408 if (n_unreffed
< n
) {
409 for (; n
> 0; info
++, n
--) {
411 pa_sink_input_unref(info
->userdata
);
412 if (info
->chunk
.memblock
)
413 pa_memblock_unref(info
->chunk
.memblock
);
418 void pa_sink_render(pa_sink
*s
, size_t length
, pa_memchunk
*result
) {
419 pa_mix_info info
[MAX_MIX_CHANNELS
];
421 size_t block_size_max
;
423 pa_sink_assert_ref(s
);
424 pa_assert(PA_SINK_OPENED(s
->thread_info
.state
));
425 pa_assert(pa_frame_aligned(length
, &s
->sample_spec
));
431 length
= pa_frame_align(MIX_BUFFER_LENGTH
, &s
->sample_spec
);
433 block_size_max
= pa_mempool_block_size_max(s
->core
->mempool
);
434 if (length
> block_size_max
)
435 length
= pa_frame_align(block_size_max
, &s
->sample_spec
);
437 pa_assert(length
> 0);
439 n
= s
->thread_info
.state
== PA_SINK_RUNNING
? fill_mix_info(s
, length
, info
, MAX_MIX_CHANNELS
) : 0;
443 if (length
> SILENCE_BUFFER_LENGTH
)
444 length
= pa_frame_align(SILENCE_BUFFER_LENGTH
, &s
->sample_spec
);
446 pa_assert(length
> 0);
448 if (!s
->silence
|| pa_memblock_get_length(s
->silence
) < length
) {
450 pa_memblock_unref(s
->silence
);
451 s
->silence
= pa_silence_memblock_new(s
->core
->mempool
, &s
->sample_spec
, length
);
454 result
->memblock
= pa_memblock_ref(s
->silence
);
455 result
->length
= length
;
461 *result
= info
[0].chunk
;
462 pa_memblock_ref(result
->memblock
);
464 if (result
->length
> length
)
465 result
->length
= length
;
467 pa_sw_cvolume_multiply(&volume
, &s
->thread_info
.soft_volume
, &info
[0].volume
);
469 if (s
->thread_info
.soft_muted
|| !pa_cvolume_is_norm(&volume
)) {
470 pa_memchunk_make_writable(result
, 0);
471 if (s
->thread_info
.soft_muted
|| pa_cvolume_is_muted(&volume
))
472 pa_silence_memchunk(result
, &s
->sample_spec
);
474 pa_volume_memchunk(result
, &s
->sample_spec
, &volume
);
478 result
->memblock
= pa_memblock_new(s
->core
->mempool
, length
);
480 ptr
= pa_memblock_acquire(result
->memblock
);
481 result
->length
= pa_mix(info
, n
, ptr
, length
, &s
->sample_spec
, &s
->thread_info
.soft_volume
, s
->thread_info
.soft_muted
);
482 pa_memblock_release(result
->memblock
);
487 if (s
->thread_info
.state
== PA_SINK_RUNNING
)
488 inputs_drop(s
, info
, n
, result
->length
);
490 if (s
->monitor_source
&& PA_SOURCE_OPENED(pa_source_get_state(s
->monitor_source
)))
491 pa_source_post(s
->monitor_source
, result
);
496 void pa_sink_render_into(pa_sink
*s
, pa_memchunk
*target
) {
497 pa_mix_info info
[MAX_MIX_CHANNELS
];
500 pa_sink_assert_ref(s
);
501 pa_assert(PA_SINK_OPENED(s
->thread_info
.state
));
503 pa_assert(target
->memblock
);
504 pa_assert(target
->length
> 0);
505 pa_assert(pa_frame_aligned(target
->length
, &s
->sample_spec
));
509 n
= s
->thread_info
.state
== PA_SINK_RUNNING
? fill_mix_info(s
, target
->length
, info
, MAX_MIX_CHANNELS
) : 0;
512 pa_silence_memchunk(target
, &s
->sample_spec
);
514 if (target
->length
> info
[0].chunk
.length
)
515 target
->length
= info
[0].chunk
.length
;
517 if (s
->thread_info
.soft_muted
)
518 pa_silence_memchunk(target
, &s
->sample_spec
);
523 ptr
= pa_memblock_acquire(target
->memblock
);
524 src
= pa_memblock_acquire(info
[0].chunk
.memblock
);
526 memcpy((uint8_t*) ptr
+ target
->index
,
527 (uint8_t*) src
+ info
[0].chunk
.index
,
530 pa_memblock_release(target
->memblock
);
531 pa_memblock_release(info
[0].chunk
.memblock
);
533 pa_sw_cvolume_multiply(&volume
, &s
->thread_info
.soft_volume
, &info
[0].volume
);
535 if (!pa_cvolume_is_norm(&volume
))
536 pa_volume_memchunk(target
, &s
->sample_spec
, &volume
);
542 ptr
= pa_memblock_acquire(target
->memblock
);
544 target
->length
= pa_mix(info
, n
,
545 (uint8_t*) ptr
+ target
->index
,
548 &s
->thread_info
.soft_volume
,
549 s
->thread_info
.soft_muted
);
551 pa_memblock_release(target
->memblock
);
554 if (s
->thread_info
.state
== PA_SINK_RUNNING
)
555 inputs_drop(s
, info
, n
, target
->length
);
557 if (s
->monitor_source
&& PA_SOURCE_OPENED(pa_source_get_state(s
->monitor_source
)))
558 pa_source_post(s
->monitor_source
, target
);
563 void pa_sink_render_into_full(pa_sink
*s
, pa_memchunk
*target
) {
567 pa_sink_assert_ref(s
);
568 pa_assert(PA_SINK_OPENED(s
->thread_info
.state
));
570 pa_assert(target
->memblock
);
571 pa_assert(target
->length
> 0);
572 pa_assert(pa_frame_aligned(target
->length
, &s
->sample_spec
));
583 pa_sink_render_into(s
, &chunk
);
592 void pa_sink_render_full(pa_sink
*s
, size_t length
, pa_memchunk
*result
) {
593 pa_sink_assert_ref(s
);
594 pa_assert(PA_SINK_OPENED(s
->thread_info
.state
));
595 pa_assert(length
> 0);
596 pa_assert(pa_frame_aligned(length
, &s
->sample_spec
));
599 /*** This needs optimization ***/
602 result
->length
= length
;
603 result
->memblock
= pa_memblock_new(s
->core
->mempool
, length
);
605 pa_sink_render_into_full(s
, result
);
608 void pa_sink_skip(pa_sink
*s
, size_t length
) {
612 pa_sink_assert_ref(s
);
613 pa_assert(PA_SINK_OPENED(s
->thread_info
.state
));
614 pa_assert(length
> 0);
615 pa_assert(pa_frame_aligned(length
, &s
->sample_spec
));
617 if (pa_source_used_by(s
->monitor_source
)) {
620 /* If something is connected to our monitor source, we have to
621 * pass valid data to it */
624 pa_sink_render(s
, length
, &chunk
);
625 pa_memblock_unref(chunk
.memblock
);
627 pa_assert(chunk
.length
<= length
);
628 length
-= chunk
.length
;
632 /* Ok, noone cares about the rendered data, so let's not even render it */
634 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
))) {
635 pa_sink_input_assert_ref(i
);
636 pa_sink_input_drop(i
, length
);
641 pa_usec_t
pa_sink_get_latency(pa_sink
*s
) {
644 pa_sink_assert_ref(s
);
645 pa_assert(PA_SINK_LINKED(s
->state
));
647 if (!PA_SINK_OPENED(s
->state
))
651 return s
->get_latency(s
);
653 if (pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_LATENCY
, &usec
, 0, NULL
) < 0)
659 void pa_sink_set_volume(pa_sink
*s
, const pa_cvolume
*volume
) {
662 pa_sink_assert_ref(s
);
663 pa_assert(PA_SINK_LINKED(s
->state
));
666 changed
= !pa_cvolume_equal(volume
, &s
->volume
);
669 if (s
->set_volume
&& s
->set_volume(s
) < 0)
670 s
->set_volume
= NULL
;
673 pa_asyncmsgq_post(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_VOLUME
, pa_xnewdup(struct pa_cvolume
, volume
, 1), 0, NULL
, pa_xfree
);
676 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
679 const pa_cvolume
*pa_sink_get_volume(pa_sink
*s
) {
680 struct pa_cvolume old_volume
;
682 pa_sink_assert_ref(s
);
683 pa_assert(PA_SINK_LINKED(s
->state
));
685 old_volume
= s
->volume
;
687 if (s
->get_volume
&& s
->get_volume(s
) < 0)
688 s
->get_volume
= NULL
;
690 if (!s
->get_volume
&& s
->refresh_volume
)
691 pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_VOLUME
, &s
->volume
, 0, NULL
);
693 if (!pa_cvolume_equal(&old_volume
, &s
->volume
))
694 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
699 void pa_sink_set_mute(pa_sink
*s
, pa_bool_t mute
) {
702 pa_sink_assert_ref(s
);
703 pa_assert(PA_SINK_LINKED(s
->state
));
705 changed
= s
->muted
!= mute
;
708 if (s
->set_mute
&& s
->set_mute(s
) < 0)
712 pa_asyncmsgq_post(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_MUTE
, PA_UINT_TO_PTR(mute
), 0, NULL
, NULL
);
715 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
718 pa_bool_t
pa_sink_get_mute(pa_sink
*s
) {
721 pa_sink_assert_ref(s
);
722 pa_assert(PA_SINK_LINKED(s
->state
));
724 old_muted
= s
->muted
;
726 if (s
->get_mute
&& s
->get_mute(s
) < 0)
729 if (!s
->get_mute
&& s
->refresh_mute
)
730 pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_MUTE
, &s
->muted
, 0, NULL
);
732 if (old_muted
!= s
->muted
)
733 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
738 void pa_sink_set_module(pa_sink
*s
, pa_module
*m
) {
739 pa_sink_assert_ref(s
);
746 if (s
->monitor_source
)
747 pa_source_set_module(s
->monitor_source
, m
);
749 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
752 void pa_sink_set_description(pa_sink
*s
, const char *description
) {
753 pa_sink_assert_ref(s
);
755 if (!description
&& !s
->description
)
758 if (description
&& s
->description
&& !strcmp(description
, s
->description
))
761 pa_xfree(s
->description
);
762 s
->description
= pa_xstrdup(description
);
764 if (s
->monitor_source
) {
767 n
= pa_sprintf_malloc("Monitor Source of %s", s
->description
? s
->description
: s
->name
);
768 pa_source_set_description(s
->monitor_source
, n
);
772 if (PA_SINK_LINKED(s
->state
)) {
773 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
774 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_DESCRIPTION_CHANGED
], s
);
778 unsigned pa_sink_linked_by(pa_sink
*s
) {
781 pa_sink_assert_ref(s
);
782 pa_assert(PA_SINK_LINKED(s
->state
));
784 ret
= pa_idxset_size(s
->inputs
);
786 /* We add in the number of streams connected to us here. Please
787 * not the asymmmetry to pa_sink_used_by()! */
789 if (s
->monitor_source
)
790 ret
+= pa_source_linked_by(s
->monitor_source
);
795 unsigned pa_sink_used_by(pa_sink
*s
) {
798 pa_sink_assert_ref(s
);
799 pa_assert(PA_SINK_LINKED(s
->state
));
801 ret
= pa_idxset_size(s
->inputs
);
802 pa_assert(ret
>= s
->n_corked
);
805 /* Streams connected to our monitor source do not matter for
806 * pa_sink_used_by()!.*/
811 int pa_sink_process_msg(pa_msgobject
*o
, int code
, void *userdata
, int64_t offset
, pa_memchunk
*chunk
) {
812 pa_sink
*s
= PA_SINK(o
);
813 pa_sink_assert_ref(s
);
814 pa_assert(s
->thread_info
.state
!= PA_SINK_UNLINKED
);
816 switch ((pa_sink_message_t
) code
) {
818 case PA_SINK_MESSAGE_ADD_INPUT
: {
819 pa_sink_input
*i
= PA_SINK_INPUT(userdata
);
820 pa_hashmap_put(s
->thread_info
.inputs
, PA_UINT32_TO_PTR(i
->index
), pa_sink_input_ref(i
));
822 /* Since the caller sleeps in pa_sink_input_put(), we can
823 * safely access data outside of thread_info even though
826 if ((i
->thread_info
.sync_prev
= i
->sync_prev
)) {
827 pa_assert(i
->sink
== i
->thread_info
.sync_prev
->sink
);
828 pa_assert(i
->sync_prev
->sync_next
== i
);
829 i
->thread_info
.sync_prev
->thread_info
.sync_next
= i
;
832 if ((i
->thread_info
.sync_next
= i
->sync_next
)) {
833 pa_assert(i
->sink
== i
->thread_info
.sync_next
->sink
);
834 pa_assert(i
->sync_next
->sync_prev
== i
);
835 i
->thread_info
.sync_next
->thread_info
.sync_prev
= i
;
838 pa_assert(!i
->thread_info
.attached
);
839 i
->thread_info
.attached
= TRUE
;
844 /* If you change anything here, make sure to change the
845 * ghost sink input handling a few lines down at
846 * PA_SINK_MESSAGE_REMOVE_INPUT_AND_BUFFER, too. */
851 case PA_SINK_MESSAGE_REMOVE_INPUT
: {
852 pa_sink_input
*i
= PA_SINK_INPUT(userdata
);
854 /* If you change anything here, make sure to change the
855 * sink input handling a few lines down at
856 * PA_SINK_MESSAGE_REMOVE_INPUT_AND_BUFFER, too. */
861 pa_assert(i
->thread_info
.attached
);
862 i
->thread_info
.attached
= FALSE
;
864 /* Since the caller sleeps in pa_sink_input_unlink(),
865 * we can safely access data outside of thread_info even
866 * though it is mutable */
868 pa_assert(!i
->thread_info
.sync_prev
);
869 pa_assert(!i
->thread_info
.sync_next
);
871 if (i
->thread_info
.sync_prev
) {
872 i
->thread_info
.sync_prev
->thread_info
.sync_next
= i
->thread_info
.sync_prev
->sync_next
;
873 i
->thread_info
.sync_prev
= NULL
;
876 if (i
->thread_info
.sync_next
) {
877 i
->thread_info
.sync_next
->thread_info
.sync_prev
= i
->thread_info
.sync_next
->sync_prev
;
878 i
->thread_info
.sync_next
= NULL
;
881 if (pa_hashmap_remove(s
->thread_info
.inputs
, PA_UINT32_TO_PTR(i
->index
)))
882 pa_sink_input_unref(i
);
887 case PA_SINK_MESSAGE_REMOVE_INPUT_AND_BUFFER
: {
888 pa_sink_input_move_info
*info
= userdata
;
891 /* We don't support moving synchronized streams. */
892 pa_assert(!info
->sink_input
->sync_prev
);
893 pa_assert(!info
->sink_input
->sync_next
);
894 pa_assert(!info
->sink_input
->thread_info
.sync_next
);
895 pa_assert(!info
->sink_input
->thread_info
.sync_prev
);
897 if (info
->sink_input
->detach
)
898 info
->sink_input
->detach(info
->sink_input
);
900 pa_assert(info
->sink_input
->thread_info
.attached
);
901 info
->sink_input
->thread_info
.attached
= FALSE
;
903 if (info
->ghost_sink_input
) {
904 pa_assert(info
->buffer_bytes
> 0);
905 pa_assert(info
->buffer
);
907 volume_is_norm
= pa_cvolume_is_norm(&info
->sink_input
->thread_info
.volume
);
909 pa_log_debug("Buffering %lu bytes ...", (unsigned long) info
->buffer_bytes
);
911 while (info
->buffer_bytes
> 0) {
912 pa_memchunk memchunk
;
916 if (pa_sink_input_peek(info
->sink_input
, info
->buffer_bytes
, &memchunk
, &volume
) < 0)
919 n
= memchunk
.length
> info
->buffer_bytes
? info
->buffer_bytes
: memchunk
.length
;
920 pa_sink_input_drop(info
->sink_input
, n
);
923 if (!volume_is_norm
) {
924 pa_memchunk_make_writable(&memchunk
, 0);
925 pa_volume_memchunk(&memchunk
, &s
->sample_spec
, &volume
);
928 if (pa_memblockq_push(info
->buffer
, &memchunk
) < 0) {
929 pa_memblock_unref(memchunk
.memblock
);
933 pa_memblock_unref(memchunk
.memblock
);
934 info
->buffer_bytes
-= n
;
937 /* Add the remaining already resampled chunk to the buffer */
938 if (info
->sink_input
->thread_info
.resampled_chunk
.memblock
)
939 pa_memblockq_push(info
->buffer
, &info
->sink_input
->thread_info
.resampled_chunk
);
941 pa_memblockq_sink_input_set_queue(info
->ghost_sink_input
, info
->buffer
);
943 pa_log_debug("Buffered %lu bytes ...", (unsigned long) pa_memblockq_get_length(info
->buffer
));
946 /* Let's remove the sink input ...*/
947 if (pa_hashmap_remove(s
->thread_info
.inputs
, PA_UINT32_TO_PTR(info
->sink_input
->index
)))
948 pa_sink_input_unref(info
->sink_input
);
950 /* .. and add the ghost sink input instead */
951 if (info
->ghost_sink_input
) {
952 pa_hashmap_put(s
->thread_info
.inputs
, PA_UINT32_TO_PTR(info
->ghost_sink_input
->index
), pa_sink_input_ref(info
->ghost_sink_input
));
953 info
->ghost_sink_input
->thread_info
.sync_prev
= info
->ghost_sink_input
->thread_info
.sync_next
= NULL
;
955 pa_assert(!info
->ghost_sink_input
->thread_info
.attached
);
956 info
->ghost_sink_input
->thread_info
.attached
= TRUE
;
958 if (info
->ghost_sink_input
->attach
)
959 info
->ghost_sink_input
->attach(info
->ghost_sink_input
);
965 case PA_SINK_MESSAGE_SET_VOLUME
:
966 s
->thread_info
.soft_volume
= *((pa_cvolume
*) userdata
);
969 case PA_SINK_MESSAGE_SET_MUTE
:
970 s
->thread_info
.soft_muted
= PA_PTR_TO_UINT(userdata
);
973 case PA_SINK_MESSAGE_GET_VOLUME
:
974 *((pa_cvolume
*) userdata
) = s
->thread_info
.soft_volume
;
977 case PA_SINK_MESSAGE_GET_MUTE
:
978 *((pa_bool_t
*) userdata
) = s
->thread_info
.soft_muted
;
981 case PA_SINK_MESSAGE_PING
:
984 case PA_SINK_MESSAGE_SET_STATE
:
986 s
->thread_info
.state
= PA_PTR_TO_UINT(userdata
);
989 case PA_SINK_MESSAGE_DETACH
:
991 /* We're detaching all our input streams so that the
992 * asyncmsgq and rtpoll fields can be changed without
994 pa_sink_detach_within_thread(s
);
997 case PA_SINK_MESSAGE_ATTACH
:
999 /* Reattach all streams */
1000 pa_sink_attach_within_thread(s
);
1003 case PA_SINK_MESSAGE_GET_LATENCY
:
1004 case PA_SINK_MESSAGE_MAX
:
1011 int pa_sink_suspend_all(pa_core
*c
, pa_bool_t suspend
) {
1016 pa_core_assert_ref(c
);
1018 for (sink
= PA_SINK(pa_idxset_first(c
->sinks
, &idx
)); sink
; sink
= PA_SINK(pa_idxset_next(c
->sinks
, &idx
)))
1019 ret
-= pa_sink_suspend(sink
, suspend
) < 0;
1024 void pa_sink_detach(pa_sink
*s
) {
1025 pa_sink_assert_ref(s
);
1026 pa_assert(PA_SINK_LINKED(s
->state
));
1028 pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_DETACH
, NULL
, 0, NULL
);
1031 void pa_sink_attach(pa_sink
*s
) {
1032 pa_sink_assert_ref(s
);
1033 pa_assert(PA_SINK_LINKED(s
->state
));
1035 pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_ATTACH
, NULL
, 0, NULL
);
1038 void pa_sink_detach_within_thread(pa_sink
*s
) {
1042 pa_sink_assert_ref(s
);
1043 pa_assert(PA_SINK_LINKED(s
->thread_info
.state
));
1045 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)))
1049 if (s
->monitor_source
)
1050 pa_source_detach_within_thread(s
->monitor_source
);
1053 void pa_sink_attach_within_thread(pa_sink
*s
) {
1057 pa_sink_assert_ref(s
);
1058 pa_assert(PA_SINK_LINKED(s
->thread_info
.state
));
1060 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)))
1064 if (s
->monitor_source
)
1065 pa_source_attach_within_thread(s
->monitor_source
);