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 static int sink_set_state(pa_sink
*s
, pa_sink_state_t state
) {
155 if (s
->state
== state
)
158 if ((s
->state
== PA_SINK_SUSPENDED
&& PA_SINK_OPENED(state
)) ||
159 (PA_SINK_OPENED(s
->state
) && state
== PA_SINK_SUSPENDED
)) {
163 /* We're suspending or resuming, tell everyone about it */
165 for (i
= PA_SINK_INPUT(pa_idxset_first(s
->inputs
, &idx
)); i
; i
= PA_SINK_INPUT(pa_idxset_next(s
->inputs
, &idx
)))
167 i
->suspend(i
, state
== PA_SINK_SUSPENDED
);
171 if ((ret
= s
->set_state(s
, state
)) < 0)
174 if (pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_STATE
, PA_UINT_TO_PTR(state
), 0, NULL
) < 0)
179 if (state
!= PA_SINK_UNLINKED
) /* if we enter UNLINKED state pa_sink_unlink() will fire the apropriate events */
180 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_STATE_CHANGED
], s
);
184 void pa_sink_put(pa_sink
* s
) {
185 pa_sink_assert_ref(s
);
187 pa_assert(s
->state
== PA_SINK_INIT
);
188 pa_assert(s
->asyncmsgq
);
189 pa_assert(s
->rtpoll
);
191 pa_assert_se(sink_set_state(s
, PA_SINK_IDLE
) == 0);
193 pa_source_put(s
->monitor_source
);
195 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
| PA_SUBSCRIPTION_EVENT_NEW
, s
->index
);
196 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_NEW_POST
], s
);
199 void pa_sink_unlink(pa_sink
* s
) {
201 pa_sink_input
*i
, *j
= NULL
;
205 /* Please note that pa_sink_unlink() does more than simply
206 * reversing pa_sink_put(). It also undoes the registrations
207 * already done in pa_sink_new()! */
209 /* All operations here shall be idempotent, i.e. pa_sink_unlink()
210 * may be called multiple times on the same sink without bad
213 linked
= PA_SINK_LINKED(s
->state
);
216 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_UNLINK
], s
);
218 if (s
->state
!= PA_SINK_UNLINKED
)
219 pa_namereg_unregister(s
->core
, s
->name
);
220 pa_idxset_remove_by_data(s
->core
->sinks
, s
, NULL
);
222 while ((i
= pa_idxset_first(s
->inputs
, NULL
))) {
224 pa_sink_input_kill(i
);
229 sink_set_state(s
, PA_SINK_UNLINKED
);
231 s
->state
= PA_SINK_UNLINKED
;
233 s
->get_latency
= NULL
;
234 s
->get_volume
= NULL
;
235 s
->set_volume
= NULL
;
240 if (s
->monitor_source
)
241 pa_source_unlink(s
->monitor_source
);
244 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
| PA_SUBSCRIPTION_EVENT_REMOVE
, s
->index
);
245 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_UNLINK_POST
], s
);
249 static void sink_free(pa_object
*o
) {
250 pa_sink
*s
= PA_SINK(o
);
254 pa_assert(pa_sink_refcnt(s
) == 0);
256 if (PA_SINK_LINKED(s
->state
))
259 pa_log_info("Freeing sink %u \"%s\"", s
->index
, s
->name
);
261 if (s
->monitor_source
) {
262 pa_source_unref(s
->monitor_source
);
263 s
->monitor_source
= NULL
;
266 pa_idxset_free(s
->inputs
, NULL
, NULL
);
268 while ((i
= pa_hashmap_steal_first(s
->thread_info
.inputs
)))
269 pa_sink_input_unref(i
);
271 pa_hashmap_free(s
->thread_info
.inputs
, NULL
, NULL
);
274 pa_memblock_unref(s
->silence
);
277 pa_xfree(s
->description
);
282 void pa_sink_set_asyncmsgq(pa_sink
*s
, pa_asyncmsgq
*q
) {
283 pa_sink_assert_ref(s
);
288 if (s
->monitor_source
)
289 pa_source_set_asyncmsgq(s
->monitor_source
, q
);
292 void pa_sink_set_rtpoll(pa_sink
*s
, pa_rtpoll
*p
) {
293 pa_sink_assert_ref(s
);
297 if (s
->monitor_source
)
298 pa_source_set_rtpoll(s
->monitor_source
, p
);
301 int pa_sink_update_status(pa_sink
*s
) {
302 pa_sink_assert_ref(s
);
303 pa_assert(PA_SINK_LINKED(s
->state
));
305 if (s
->state
== PA_SINK_SUSPENDED
)
308 return sink_set_state(s
, pa_sink_used_by(s
) ? PA_SINK_RUNNING
: PA_SINK_IDLE
);
311 int pa_sink_suspend(pa_sink
*s
, pa_bool_t suspend
) {
312 pa_sink_assert_ref(s
);
313 pa_assert(PA_SINK_LINKED(s
->state
));
316 return sink_set_state(s
, PA_SINK_SUSPENDED
);
318 return sink_set_state(s
, pa_sink_used_by(s
) ? PA_SINK_RUNNING
: PA_SINK_IDLE
);
321 void pa_sink_ping(pa_sink
*s
) {
322 pa_sink_assert_ref(s
);
323 pa_assert(PA_SINK_LINKED(s
->state
));
325 pa_asyncmsgq_post(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_PING
, NULL
, 0, NULL
, NULL
);
328 static unsigned fill_mix_info(pa_sink
*s
, size_t length
, pa_mix_info
*info
, unsigned maxinfo
) {
333 pa_sink_assert_ref(s
);
336 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)) && maxinfo
> 0) {
337 pa_sink_input_assert_ref(i
);
339 if (pa_sink_input_peek(i
, length
, &info
->chunk
, &info
->volume
) < 0)
342 info
->userdata
= pa_sink_input_ref(i
);
344 pa_assert(info
->chunk
.memblock
);
345 pa_assert(info
->chunk
.length
> 0);
355 static void inputs_drop(pa_sink
*s
, pa_mix_info
*info
, unsigned n
, size_t length
) {
359 unsigned n_unreffed
= 0;
361 pa_sink_assert_ref(s
);
363 /* We optimize for the case where the order of the inputs has not changed */
365 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
))) {
369 pa_sink_input_assert_ref(i
);
373 /* Let's try to find the matching entry info the pa_mix_info array */
374 for (j
= 0; j
< n
; j
++) {
376 if (info
[p
].userdata
== i
) {
387 pa_sink_input_drop(i
, length
);
390 pa_sink_input_unref(m
->userdata
);
392 if (m
->chunk
.memblock
)
393 pa_memblock_unref(m
->chunk
.memblock
);
394 pa_memchunk_reset(&m
->chunk
);
400 /* Now drop references to entries that are included in the
401 * pa_mix_info array but don't exist anymore */
403 if (n_unreffed
< n
) {
404 for (; n
> 0; info
++, n
--) {
406 pa_sink_input_unref(info
->userdata
);
407 if (info
->chunk
.memblock
)
408 pa_memblock_unref(info
->chunk
.memblock
);
413 void pa_sink_render(pa_sink
*s
, size_t length
, pa_memchunk
*result
) {
414 pa_mix_info info
[MAX_MIX_CHANNELS
];
416 size_t block_size_max
;
418 pa_sink_assert_ref(s
);
419 pa_assert(PA_SINK_OPENED(s
->thread_info
.state
));
420 pa_assert(pa_frame_aligned(length
, &s
->sample_spec
));
426 length
= pa_frame_align(MIX_BUFFER_LENGTH
, &s
->sample_spec
);
428 block_size_max
= pa_mempool_block_size_max(s
->core
->mempool
);
429 if (length
> block_size_max
)
430 length
= pa_frame_align(block_size_max
, &s
->sample_spec
);
432 pa_assert(length
> 0);
434 n
= s
->thread_info
.state
== PA_SINK_RUNNING
? fill_mix_info(s
, length
, info
, MAX_MIX_CHANNELS
) : 0;
438 if (length
> SILENCE_BUFFER_LENGTH
)
439 length
= pa_frame_align(SILENCE_BUFFER_LENGTH
, &s
->sample_spec
);
441 pa_assert(length
> 0);
443 if (!s
->silence
|| pa_memblock_get_length(s
->silence
) < length
) {
445 pa_memblock_unref(s
->silence
);
446 s
->silence
= pa_silence_memblock_new(s
->core
->mempool
, &s
->sample_spec
, length
);
449 result
->memblock
= pa_memblock_ref(s
->silence
);
450 result
->length
= length
;
456 *result
= info
[0].chunk
;
457 pa_memblock_ref(result
->memblock
);
459 if (result
->length
> length
)
460 result
->length
= length
;
462 pa_sw_cvolume_multiply(&volume
, &s
->thread_info
.soft_volume
, &info
[0].volume
);
464 if (s
->thread_info
.soft_muted
|| !pa_cvolume_is_norm(&volume
)) {
465 pa_memchunk_make_writable(result
, 0);
466 if (s
->thread_info
.soft_muted
|| pa_cvolume_is_muted(&volume
))
467 pa_silence_memchunk(result
, &s
->sample_spec
);
469 pa_volume_memchunk(result
, &s
->sample_spec
, &volume
);
473 result
->memblock
= pa_memblock_new(s
->core
->mempool
, length
);
475 ptr
= pa_memblock_acquire(result
->memblock
);
476 result
->length
= pa_mix(info
, n
, ptr
, length
, &s
->sample_spec
, &s
->thread_info
.soft_volume
, s
->thread_info
.soft_muted
);
477 pa_memblock_release(result
->memblock
);
482 if (s
->thread_info
.state
== PA_SINK_RUNNING
)
483 inputs_drop(s
, info
, n
, result
->length
);
485 if (s
->monitor_source
&& PA_SOURCE_OPENED(pa_source_get_state(s
->monitor_source
)))
486 pa_source_post(s
->monitor_source
, result
);
491 void pa_sink_render_into(pa_sink
*s
, pa_memchunk
*target
) {
492 pa_mix_info info
[MAX_MIX_CHANNELS
];
495 pa_sink_assert_ref(s
);
496 pa_assert(PA_SINK_OPENED(s
->thread_info
.state
));
498 pa_assert(target
->memblock
);
499 pa_assert(target
->length
> 0);
500 pa_assert(pa_frame_aligned(target
->length
, &s
->sample_spec
));
504 n
= s
->thread_info
.state
== PA_SINK_RUNNING
? fill_mix_info(s
, target
->length
, info
, MAX_MIX_CHANNELS
) : 0;
507 pa_silence_memchunk(target
, &s
->sample_spec
);
509 if (target
->length
> info
[0].chunk
.length
)
510 target
->length
= info
[0].chunk
.length
;
512 if (s
->thread_info
.soft_muted
)
513 pa_silence_memchunk(target
, &s
->sample_spec
);
518 ptr
= pa_memblock_acquire(target
->memblock
);
519 src
= pa_memblock_acquire(info
[0].chunk
.memblock
);
521 memcpy((uint8_t*) ptr
+ target
->index
,
522 (uint8_t*) src
+ info
[0].chunk
.index
,
525 pa_memblock_release(target
->memblock
);
526 pa_memblock_release(info
[0].chunk
.memblock
);
528 pa_sw_cvolume_multiply(&volume
, &s
->thread_info
.soft_volume
, &info
[0].volume
);
530 if (!pa_cvolume_is_norm(&volume
))
531 pa_volume_memchunk(target
, &s
->sample_spec
, &volume
);
537 ptr
= pa_memblock_acquire(target
->memblock
);
539 target
->length
= pa_mix(info
, n
,
540 (uint8_t*) ptr
+ target
->index
,
543 &s
->thread_info
.soft_volume
,
544 s
->thread_info
.soft_muted
);
546 pa_memblock_release(target
->memblock
);
549 if (s
->thread_info
.state
== PA_SINK_RUNNING
)
550 inputs_drop(s
, info
, n
, target
->length
);
552 if (s
->monitor_source
&& PA_SOURCE_OPENED(pa_source_get_state(s
->monitor_source
)))
553 pa_source_post(s
->monitor_source
, target
);
558 void pa_sink_render_into_full(pa_sink
*s
, pa_memchunk
*target
) {
562 pa_sink_assert_ref(s
);
563 pa_assert(PA_SINK_OPENED(s
->thread_info
.state
));
565 pa_assert(target
->memblock
);
566 pa_assert(target
->length
> 0);
567 pa_assert(pa_frame_aligned(target
->length
, &s
->sample_spec
));
578 pa_sink_render_into(s
, &chunk
);
587 void pa_sink_render_full(pa_sink
*s
, size_t length
, pa_memchunk
*result
) {
588 pa_sink_assert_ref(s
);
589 pa_assert(PA_SINK_OPENED(s
->thread_info
.state
));
590 pa_assert(length
> 0);
591 pa_assert(pa_frame_aligned(length
, &s
->sample_spec
));
594 /*** This needs optimization ***/
597 result
->length
= length
;
598 result
->memblock
= pa_memblock_new(s
->core
->mempool
, length
);
600 pa_sink_render_into_full(s
, result
);
603 void pa_sink_skip(pa_sink
*s
, size_t length
) {
607 pa_sink_assert_ref(s
);
608 pa_assert(PA_SINK_OPENED(s
->thread_info
.state
));
609 pa_assert(length
> 0);
610 pa_assert(pa_frame_aligned(length
, &s
->sample_spec
));
612 if (pa_source_used_by(s
->monitor_source
)) {
615 /* If something is connected to our monitor source, we have to
616 * pass valid data to it */
619 pa_sink_render(s
, length
, &chunk
);
620 pa_memblock_unref(chunk
.memblock
);
622 pa_assert(chunk
.length
<= length
);
623 length
-= chunk
.length
;
627 /* Ok, noone cares about the rendered data, so let's not even render it */
629 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
))) {
630 pa_sink_input_assert_ref(i
);
631 pa_sink_input_drop(i
, length
);
636 pa_usec_t
pa_sink_get_latency(pa_sink
*s
) {
639 pa_sink_assert_ref(s
);
640 pa_assert(PA_SINK_LINKED(s
->state
));
642 if (!PA_SINK_OPENED(s
->state
))
646 return s
->get_latency(s
);
648 if (pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_LATENCY
, &usec
, 0, NULL
) < 0)
654 void pa_sink_set_volume(pa_sink
*s
, const pa_cvolume
*volume
) {
657 pa_sink_assert_ref(s
);
658 pa_assert(PA_SINK_LINKED(s
->state
));
661 changed
= !pa_cvolume_equal(volume
, &s
->volume
);
664 if (s
->set_volume
&& s
->set_volume(s
) < 0)
665 s
->set_volume
= NULL
;
668 pa_asyncmsgq_post(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_VOLUME
, pa_xnewdup(struct pa_cvolume
, volume
, 1), 0, NULL
, pa_xfree
);
671 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
674 const pa_cvolume
*pa_sink_get_volume(pa_sink
*s
) {
675 struct pa_cvolume old_volume
;
677 pa_sink_assert_ref(s
);
678 pa_assert(PA_SINK_LINKED(s
->state
));
680 old_volume
= s
->volume
;
682 if (s
->get_volume
&& s
->get_volume(s
) < 0)
683 s
->get_volume
= NULL
;
685 if (!s
->get_volume
&& s
->refresh_volume
)
686 pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_VOLUME
, &s
->volume
, 0, NULL
);
688 if (!pa_cvolume_equal(&old_volume
, &s
->volume
))
689 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
694 void pa_sink_set_mute(pa_sink
*s
, pa_bool_t mute
) {
697 pa_sink_assert_ref(s
);
698 pa_assert(PA_SINK_LINKED(s
->state
));
700 changed
= s
->muted
!= mute
;
703 if (s
->set_mute
&& s
->set_mute(s
) < 0)
707 pa_asyncmsgq_post(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_MUTE
, PA_UINT_TO_PTR(mute
), 0, NULL
, NULL
);
710 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
713 pa_bool_t
pa_sink_get_mute(pa_sink
*s
) {
716 pa_sink_assert_ref(s
);
717 pa_assert(PA_SINK_LINKED(s
->state
));
719 old_muted
= s
->muted
;
721 if (s
->get_mute
&& s
->get_mute(s
) < 0)
724 if (!s
->get_mute
&& s
->refresh_mute
)
725 pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_MUTE
, &s
->muted
, 0, NULL
);
727 if (old_muted
!= s
->muted
)
728 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
733 void pa_sink_set_module(pa_sink
*s
, pa_module
*m
) {
734 pa_sink_assert_ref(s
);
741 if (s
->monitor_source
)
742 pa_source_set_module(s
->monitor_source
, m
);
744 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
747 void pa_sink_set_description(pa_sink
*s
, const char *description
) {
748 pa_sink_assert_ref(s
);
750 if (!description
&& !s
->description
)
753 if (description
&& s
->description
&& !strcmp(description
, s
->description
))
756 pa_xfree(s
->description
);
757 s
->description
= pa_xstrdup(description
);
759 if (s
->monitor_source
) {
762 n
= pa_sprintf_malloc("Monitor Source of %s", s
->description
? s
->description
: s
->name
);
763 pa_source_set_description(s
->monitor_source
, n
);
767 if (PA_SINK_LINKED(s
->state
)) {
768 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
769 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_DESCRIPTION_CHANGED
], s
);
773 unsigned pa_sink_linked_by(pa_sink
*s
) {
776 pa_sink_assert_ref(s
);
777 pa_assert(PA_SINK_LINKED(s
->state
));
779 ret
= pa_idxset_size(s
->inputs
);
781 /* We add in the number of streams connected to us here. Please
782 * not the asymmmetry to pa_sink_used_by()! */
784 if (s
->monitor_source
)
785 ret
+= pa_source_linked_by(s
->monitor_source
);
790 unsigned pa_sink_used_by(pa_sink
*s
) {
793 pa_sink_assert_ref(s
);
794 pa_assert(PA_SINK_LINKED(s
->state
));
796 ret
= pa_idxset_size(s
->inputs
);
797 pa_assert(ret
>= s
->n_corked
);
800 /* Streams connected to our monitor source do not matter for
801 * pa_sink_used_by()!.*/
806 int pa_sink_process_msg(pa_msgobject
*o
, int code
, void *userdata
, int64_t offset
, pa_memchunk
*chunk
) {
807 pa_sink
*s
= PA_SINK(o
);
808 pa_sink_assert_ref(s
);
809 pa_assert(s
->thread_info
.state
!= PA_SINK_UNLINKED
);
811 switch ((pa_sink_message_t
) code
) {
813 case PA_SINK_MESSAGE_ADD_INPUT
: {
814 pa_sink_input
*i
= PA_SINK_INPUT(userdata
);
815 pa_hashmap_put(s
->thread_info
.inputs
, PA_UINT32_TO_PTR(i
->index
), pa_sink_input_ref(i
));
817 /* Since the caller sleeps in pa_sink_input_put(), we can
818 * safely access data outside of thread_info even though
821 if ((i
->thread_info
.sync_prev
= i
->sync_prev
)) {
822 pa_assert(i
->sink
== i
->thread_info
.sync_prev
->sink
);
823 pa_assert(i
->sync_prev
->sync_next
== i
);
824 i
->thread_info
.sync_prev
->thread_info
.sync_next
= i
;
827 if ((i
->thread_info
.sync_next
= i
->sync_next
)) {
828 pa_assert(i
->sink
== i
->thread_info
.sync_next
->sink
);
829 pa_assert(i
->sync_next
->sync_prev
== i
);
830 i
->thread_info
.sync_next
->thread_info
.sync_prev
= i
;
833 pa_assert(!i
->thread_info
.attached
);
834 i
->thread_info
.attached
= TRUE
;
839 /* If you change anything here, make sure to change the
840 * ghost sink input handling a few lines down at
841 * PA_SINK_MESSAGE_REMOVE_INPUT_AND_BUFFER, too. */
846 case PA_SINK_MESSAGE_REMOVE_INPUT
: {
847 pa_sink_input
*i
= PA_SINK_INPUT(userdata
);
849 /* If you change anything here, make sure to change the
850 * sink input handling a few lines down at
851 * PA_SINK_MESSAGE_REMOVE_INPUT_AND_BUFFER, too. */
856 pa_assert(i
->thread_info
.attached
);
857 i
->thread_info
.attached
= FALSE
;
859 /* Since the caller sleeps in pa_sink_input_unlink(),
860 * we can safely access data outside of thread_info even
861 * though it is mutable */
863 pa_assert(!i
->thread_info
.sync_prev
);
864 pa_assert(!i
->thread_info
.sync_next
);
866 if (i
->thread_info
.sync_prev
) {
867 i
->thread_info
.sync_prev
->thread_info
.sync_next
= i
->thread_info
.sync_prev
->sync_next
;
868 i
->thread_info
.sync_prev
= NULL
;
871 if (i
->thread_info
.sync_next
) {
872 i
->thread_info
.sync_next
->thread_info
.sync_prev
= i
->thread_info
.sync_next
->sync_prev
;
873 i
->thread_info
.sync_next
= NULL
;
876 if (pa_hashmap_remove(s
->thread_info
.inputs
, PA_UINT32_TO_PTR(i
->index
)))
877 pa_sink_input_unref(i
);
882 case PA_SINK_MESSAGE_REMOVE_INPUT_AND_BUFFER
: {
883 pa_sink_input_move_info
*info
= userdata
;
886 /* We don't support moving synchronized streams. */
887 pa_assert(!info
->sink_input
->sync_prev
);
888 pa_assert(!info
->sink_input
->sync_next
);
889 pa_assert(!info
->sink_input
->thread_info
.sync_next
);
890 pa_assert(!info
->sink_input
->thread_info
.sync_prev
);
892 if (info
->sink_input
->detach
)
893 info
->sink_input
->detach(info
->sink_input
);
895 pa_assert(info
->sink_input
->thread_info
.attached
);
896 info
->sink_input
->thread_info
.attached
= FALSE
;
898 if (info
->ghost_sink_input
) {
899 pa_assert(info
->buffer_bytes
> 0);
900 pa_assert(info
->buffer
);
902 volume_is_norm
= pa_cvolume_is_norm(&info
->sink_input
->thread_info
.volume
);
904 pa_log_debug("Buffering %lu bytes ...", (unsigned long) info
->buffer_bytes
);
906 while (info
->buffer_bytes
> 0) {
907 pa_memchunk memchunk
;
911 if (pa_sink_input_peek(info
->sink_input
, info
->buffer_bytes
, &memchunk
, &volume
) < 0)
914 n
= memchunk
.length
> info
->buffer_bytes
? info
->buffer_bytes
: memchunk
.length
;
915 pa_sink_input_drop(info
->sink_input
, n
);
918 if (!volume_is_norm
) {
919 pa_memchunk_make_writable(&memchunk
, 0);
920 pa_volume_memchunk(&memchunk
, &s
->sample_spec
, &volume
);
923 if (pa_memblockq_push(info
->buffer
, &memchunk
) < 0) {
924 pa_memblock_unref(memchunk
.memblock
);
928 pa_memblock_unref(memchunk
.memblock
);
929 info
->buffer_bytes
-= n
;
932 /* Add the remaining already resampled chunk to the buffer */
933 if (info
->sink_input
->thread_info
.resampled_chunk
.memblock
)
934 pa_memblockq_push(info
->buffer
, &info
->sink_input
->thread_info
.resampled_chunk
);
936 pa_memblockq_sink_input_set_queue(info
->ghost_sink_input
, info
->buffer
);
938 pa_log_debug("Buffered %lu bytes ...", (unsigned long) pa_memblockq_get_length(info
->buffer
));
941 /* Let's remove the sink input ...*/
942 if (pa_hashmap_remove(s
->thread_info
.inputs
, PA_UINT32_TO_PTR(info
->sink_input
->index
)))
943 pa_sink_input_unref(info
->sink_input
);
945 /* .. and add the ghost sink input instead */
946 if (info
->ghost_sink_input
) {
947 pa_hashmap_put(s
->thread_info
.inputs
, PA_UINT32_TO_PTR(info
->ghost_sink_input
->index
), pa_sink_input_ref(info
->ghost_sink_input
));
948 info
->ghost_sink_input
->thread_info
.sync_prev
= info
->ghost_sink_input
->thread_info
.sync_next
= NULL
;
950 pa_assert(!info
->ghost_sink_input
->thread_info
.attached
);
951 info
->ghost_sink_input
->thread_info
.attached
= TRUE
;
953 if (info
->ghost_sink_input
->attach
)
954 info
->ghost_sink_input
->attach(info
->ghost_sink_input
);
960 case PA_SINK_MESSAGE_SET_VOLUME
:
961 s
->thread_info
.soft_volume
= *((pa_cvolume
*) userdata
);
964 case PA_SINK_MESSAGE_SET_MUTE
:
965 s
->thread_info
.soft_muted
= PA_PTR_TO_UINT(userdata
);
968 case PA_SINK_MESSAGE_GET_VOLUME
:
969 *((pa_cvolume
*) userdata
) = s
->thread_info
.soft_volume
;
972 case PA_SINK_MESSAGE_GET_MUTE
:
973 *((pa_bool_t
*) userdata
) = s
->thread_info
.soft_muted
;
976 case PA_SINK_MESSAGE_PING
:
979 case PA_SINK_MESSAGE_SET_STATE
:
981 s
->thread_info
.state
= PA_PTR_TO_UINT(userdata
);
984 case PA_SINK_MESSAGE_DETACH
:
986 /* We're detaching all our input streams so that the
987 * asyncmsgq and rtpoll fields can be changed without
989 pa_sink_detach_within_thread(s
);
992 case PA_SINK_MESSAGE_ATTACH
:
994 /* Reattach all streams */
995 pa_sink_attach_within_thread(s
);
998 case PA_SINK_MESSAGE_GET_LATENCY
:
999 case PA_SINK_MESSAGE_MAX
:
1006 int pa_sink_suspend_all(pa_core
*c
, pa_bool_t suspend
) {
1011 pa_core_assert_ref(c
);
1013 for (sink
= PA_SINK(pa_idxset_first(c
->sinks
, &idx
)); sink
; sink
= PA_SINK(pa_idxset_next(c
->sinks
, &idx
)))
1014 ret
-= pa_sink_suspend(sink
, suspend
) < 0;
1019 void pa_sink_detach(pa_sink
*s
) {
1020 pa_sink_assert_ref(s
);
1021 pa_assert(PA_SINK_LINKED(s
->state
));
1023 pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_DETACH
, NULL
, 0, NULL
);
1026 void pa_sink_attach(pa_sink
*s
) {
1027 pa_sink_assert_ref(s
);
1028 pa_assert(PA_SINK_LINKED(s
->state
));
1030 pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_ATTACH
, NULL
, 0, NULL
);
1033 void pa_sink_detach_within_thread(pa_sink
*s
) {
1037 pa_sink_assert_ref(s
);
1038 pa_assert(PA_SINK_LINKED(s
->thread_info
.state
));
1040 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)))
1044 if (s
->monitor_source
)
1045 pa_source_detach_within_thread(s
->monitor_source
);
1048 void pa_sink_attach_within_thread(pa_sink
*s
) {
1052 pa_sink_assert_ref(s
);
1053 pa_assert(PA_SINK_LINKED(s
->thread_info
.state
));
1055 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)))
1059 if (s
->monitor_source
)
1060 pa_source_attach_within_thread(s
->monitor_source
);