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
) {
204 pa_sink_input
*i
, *j
= NULL
;
208 /* Please note that pa_sink_unlink() does more than simply
209 * reversing pa_sink_put(). It also undoes the registrations
210 * already done in pa_sink_new()! */
212 /* All operations here shall be idempotent, i.e. pa_sink_unlink()
213 * may be called multiple times on the same sink without bad
216 linked
= PA_SINK_LINKED(s
->state
);
219 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_UNLINK
], s
);
221 if (s
->state
!= PA_SINK_UNLINKED
)
222 pa_namereg_unregister(s
->core
, s
->name
);
223 pa_idxset_remove_by_data(s
->core
->sinks
, s
, NULL
);
225 while ((i
= pa_idxset_first(s
->inputs
, NULL
))) {
227 pa_sink_input_kill(i
);
232 sink_set_state(s
, PA_SINK_UNLINKED
);
234 s
->state
= PA_SINK_UNLINKED
;
236 s
->get_latency
= NULL
;
237 s
->get_volume
= NULL
;
238 s
->set_volume
= NULL
;
243 if (s
->monitor_source
)
244 pa_source_unlink(s
->monitor_source
);
247 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
| PA_SUBSCRIPTION_EVENT_REMOVE
, s
->index
);
248 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_UNLINK_POST
], s
);
252 static void sink_free(pa_object
*o
) {
253 pa_sink
*s
= PA_SINK(o
);
257 pa_assert(pa_sink_refcnt(s
) == 0);
259 if (PA_SINK_LINKED(s
->state
))
262 pa_log_info("Freeing sink %u \"%s\"", s
->index
, s
->name
);
264 if (s
->monitor_source
) {
265 pa_source_unref(s
->monitor_source
);
266 s
->monitor_source
= NULL
;
269 pa_idxset_free(s
->inputs
, NULL
, NULL
);
271 while ((i
= pa_hashmap_steal_first(s
->thread_info
.inputs
)))
272 pa_sink_input_unref(i
);
274 pa_hashmap_free(s
->thread_info
.inputs
, NULL
, NULL
);
277 pa_memblock_unref(s
->silence
);
280 pa_xfree(s
->description
);
285 void pa_sink_set_asyncmsgq(pa_sink
*s
, pa_asyncmsgq
*q
) {
286 pa_sink_assert_ref(s
);
291 if (s
->monitor_source
)
292 pa_source_set_asyncmsgq(s
->monitor_source
, q
);
295 void pa_sink_set_rtpoll(pa_sink
*s
, pa_rtpoll
*p
) {
296 pa_sink_assert_ref(s
);
300 if (s
->monitor_source
)
301 pa_source_set_rtpoll(s
->monitor_source
, p
);
304 int pa_sink_update_status(pa_sink
*s
) {
305 pa_sink_assert_ref(s
);
306 pa_assert(PA_SINK_LINKED(s
->state
));
308 if (s
->state
== PA_SINK_SUSPENDED
)
311 return sink_set_state(s
, pa_sink_used_by(s
) ? PA_SINK_RUNNING
: PA_SINK_IDLE
);
314 int pa_sink_suspend(pa_sink
*s
, pa_bool_t suspend
) {
315 pa_sink_assert_ref(s
);
316 pa_assert(PA_SINK_LINKED(s
->state
));
319 return sink_set_state(s
, PA_SINK_SUSPENDED
);
321 return sink_set_state(s
, pa_sink_used_by(s
) ? PA_SINK_RUNNING
: PA_SINK_IDLE
);
324 void pa_sink_ping(pa_sink
*s
) {
325 pa_sink_assert_ref(s
);
326 pa_assert(PA_SINK_LINKED(s
->state
));
328 pa_asyncmsgq_post(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_PING
, NULL
, 0, NULL
, NULL
);
331 static unsigned fill_mix_info(pa_sink
*s
, size_t length
, pa_mix_info
*info
, unsigned maxinfo
) {
336 pa_sink_assert_ref(s
);
339 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)) && maxinfo
> 0) {
340 pa_sink_input_assert_ref(i
);
342 if (pa_sink_input_peek(i
, length
, &info
->chunk
, &info
->volume
) < 0)
345 info
->userdata
= pa_sink_input_ref(i
);
347 pa_assert(info
->chunk
.memblock
);
348 pa_assert(info
->chunk
.length
> 0);
358 static void inputs_drop(pa_sink
*s
, pa_mix_info
*info
, unsigned n
, size_t length
) {
362 unsigned n_unreffed
= 0;
364 pa_sink_assert_ref(s
);
366 /* We optimize for the case where the order of the inputs has not changed */
368 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
))) {
372 pa_sink_input_assert_ref(i
);
376 /* Let's try to find the matching entry info the pa_mix_info array */
377 for (j
= 0; j
< n
; j
++) {
379 if (info
[p
].userdata
== i
) {
390 pa_sink_input_drop(i
, length
);
393 pa_sink_input_unref(m
->userdata
);
395 if (m
->chunk
.memblock
)
396 pa_memblock_unref(m
->chunk
.memblock
);
397 pa_memchunk_reset(&m
->chunk
);
403 /* Now drop references to entries that are included in the
404 * pa_mix_info array but don't exist anymore */
406 if (n_unreffed
< n
) {
407 for (; n
> 0; info
++, n
--) {
409 pa_sink_input_unref(info
->userdata
);
410 if (info
->chunk
.memblock
)
411 pa_memblock_unref(info
->chunk
.memblock
);
416 void pa_sink_render(pa_sink
*s
, size_t length
, pa_memchunk
*result
) {
417 pa_mix_info info
[MAX_MIX_CHANNELS
];
419 size_t block_size_max
;
421 pa_sink_assert_ref(s
);
422 pa_assert(PA_SINK_OPENED(s
->thread_info
.state
));
423 pa_assert(pa_frame_aligned(length
, &s
->sample_spec
));
429 length
= pa_frame_align(MIX_BUFFER_LENGTH
, &s
->sample_spec
);
431 block_size_max
= pa_mempool_block_size_max(s
->core
->mempool
);
432 if (length
> block_size_max
)
433 length
= pa_frame_align(block_size_max
, &s
->sample_spec
);
435 pa_assert(length
> 0);
437 n
= s
->thread_info
.state
== PA_SINK_RUNNING
? fill_mix_info(s
, length
, info
, MAX_MIX_CHANNELS
) : 0;
441 if (length
> SILENCE_BUFFER_LENGTH
)
442 length
= pa_frame_align(SILENCE_BUFFER_LENGTH
, &s
->sample_spec
);
444 pa_assert(length
> 0);
446 if (!s
->silence
|| pa_memblock_get_length(s
->silence
) < length
) {
448 pa_memblock_unref(s
->silence
);
449 s
->silence
= pa_silence_memblock_new(s
->core
->mempool
, &s
->sample_spec
, length
);
452 result
->memblock
= pa_memblock_ref(s
->silence
);
453 result
->length
= length
;
459 *result
= info
[0].chunk
;
460 pa_memblock_ref(result
->memblock
);
462 if (result
->length
> length
)
463 result
->length
= length
;
465 pa_sw_cvolume_multiply(&volume
, &s
->thread_info
.soft_volume
, &info
[0].volume
);
467 if (s
->thread_info
.soft_muted
|| !pa_cvolume_is_norm(&volume
)) {
468 pa_memchunk_make_writable(result
, 0);
469 if (s
->thread_info
.soft_muted
|| pa_cvolume_is_muted(&volume
))
470 pa_silence_memchunk(result
, &s
->sample_spec
);
472 pa_volume_memchunk(result
, &s
->sample_spec
, &volume
);
476 result
->memblock
= pa_memblock_new(s
->core
->mempool
, length
);
478 ptr
= pa_memblock_acquire(result
->memblock
);
479 result
->length
= pa_mix(info
, n
, ptr
, length
, &s
->sample_spec
, &s
->thread_info
.soft_volume
, s
->thread_info
.soft_muted
);
480 pa_memblock_release(result
->memblock
);
485 if (s
->thread_info
.state
== PA_SINK_RUNNING
)
486 inputs_drop(s
, info
, n
, result
->length
);
488 if (s
->monitor_source
)
489 pa_source_post(s
->monitor_source
, result
);
494 void pa_sink_render_into(pa_sink
*s
, pa_memchunk
*target
) {
495 pa_mix_info info
[MAX_MIX_CHANNELS
];
498 pa_sink_assert_ref(s
);
499 pa_assert(PA_SINK_OPENED(s
->thread_info
.state
));
501 pa_assert(target
->memblock
);
502 pa_assert(target
->length
> 0);
503 pa_assert(pa_frame_aligned(target
->length
, &s
->sample_spec
));
507 n
= s
->thread_info
.state
== PA_SINK_RUNNING
? fill_mix_info(s
, target
->length
, info
, MAX_MIX_CHANNELS
) : 0;
510 pa_silence_memchunk(target
, &s
->sample_spec
);
512 if (target
->length
> info
[0].chunk
.length
)
513 target
->length
= info
[0].chunk
.length
;
515 if (s
->thread_info
.soft_muted
)
516 pa_silence_memchunk(target
, &s
->sample_spec
);
521 ptr
= pa_memblock_acquire(target
->memblock
);
522 src
= pa_memblock_acquire(info
[0].chunk
.memblock
);
524 memcpy((uint8_t*) ptr
+ target
->index
,
525 (uint8_t*) src
+ info
[0].chunk
.index
,
528 pa_memblock_release(target
->memblock
);
529 pa_memblock_release(info
[0].chunk
.memblock
);
531 pa_sw_cvolume_multiply(&volume
, &s
->thread_info
.soft_volume
, &info
[0].volume
);
533 if (!pa_cvolume_is_norm(&volume
))
534 pa_volume_memchunk(target
, &s
->sample_spec
, &volume
);
540 ptr
= pa_memblock_acquire(target
->memblock
);
542 target
->length
= pa_mix(info
, n
,
543 (uint8_t*) ptr
+ target
->index
,
546 &s
->thread_info
.soft_volume
,
547 s
->thread_info
.soft_muted
);
549 pa_memblock_release(target
->memblock
);
552 if (s
->thread_info
.state
== PA_SINK_RUNNING
)
553 inputs_drop(s
, info
, n
, target
->length
);
555 if (s
->monitor_source
)
556 pa_source_post(s
->monitor_source
, target
);
561 void pa_sink_render_into_full(pa_sink
*s
, pa_memchunk
*target
) {
565 pa_sink_assert_ref(s
);
566 pa_assert(PA_SINK_OPENED(s
->thread_info
.state
));
568 pa_assert(target
->memblock
);
569 pa_assert(target
->length
> 0);
570 pa_assert(pa_frame_aligned(target
->length
, &s
->sample_spec
));
581 pa_sink_render_into(s
, &chunk
);
590 void pa_sink_render_full(pa_sink
*s
, size_t length
, pa_memchunk
*result
) {
591 pa_sink_assert_ref(s
);
592 pa_assert(PA_SINK_OPENED(s
->thread_info
.state
));
593 pa_assert(length
> 0);
594 pa_assert(pa_frame_aligned(length
, &s
->sample_spec
));
597 /*** This needs optimization ***/
600 result
->length
= length
;
601 result
->memblock
= pa_memblock_new(s
->core
->mempool
, length
);
603 pa_sink_render_into_full(s
, result
);
606 void pa_sink_skip(pa_sink
*s
, size_t length
) {
610 pa_sink_assert_ref(s
);
611 pa_assert(PA_SINK_OPENED(s
->thread_info
.state
));
612 pa_assert(length
> 0);
613 pa_assert(pa_frame_aligned(length
, &s
->sample_spec
));
615 if (pa_source_used_by(s
->monitor_source
)) {
618 /* If something is connected to our monitor source, we have to
619 * pass valid data to it */
622 pa_sink_render(s
, length
, &chunk
);
623 pa_memblock_unref(chunk
.memblock
);
625 pa_assert(chunk
.length
<= length
);
626 length
-= chunk
.length
;
630 /* Ok, noone cares about the rendered data, so let's not even render it */
632 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
))) {
633 pa_sink_input_assert_ref(i
);
634 pa_sink_input_drop(i
, length
);
639 pa_usec_t
pa_sink_get_latency(pa_sink
*s
) {
642 pa_sink_assert_ref(s
);
643 pa_assert(PA_SINK_LINKED(s
->state
));
645 if (!PA_SINK_OPENED(s
->state
))
649 return s
->get_latency(s
);
651 if (pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_LATENCY
, &usec
, 0, NULL
) < 0)
657 void pa_sink_set_volume(pa_sink
*s
, const pa_cvolume
*volume
) {
660 pa_sink_assert_ref(s
);
661 pa_assert(PA_SINK_LINKED(s
->state
));
664 changed
= !pa_cvolume_equal(volume
, &s
->volume
);
667 if (s
->set_volume
&& s
->set_volume(s
) < 0)
668 s
->set_volume
= NULL
;
671 pa_asyncmsgq_post(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_VOLUME
, pa_xnewdup(struct pa_cvolume
, volume
, 1), 0, NULL
, pa_xfree
);
674 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
677 const pa_cvolume
*pa_sink_get_volume(pa_sink
*s
) {
678 struct pa_cvolume old_volume
;
680 pa_sink_assert_ref(s
);
681 pa_assert(PA_SINK_LINKED(s
->state
));
683 old_volume
= s
->volume
;
685 if (s
->get_volume
&& s
->get_volume(s
) < 0)
686 s
->get_volume
= NULL
;
688 if (!s
->get_volume
&& s
->refresh_volume
)
689 pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_VOLUME
, &s
->volume
, 0, NULL
);
691 if (!pa_cvolume_equal(&old_volume
, &s
->volume
))
692 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
697 void pa_sink_set_mute(pa_sink
*s
, pa_bool_t mute
) {
700 pa_sink_assert_ref(s
);
701 pa_assert(PA_SINK_LINKED(s
->state
));
703 changed
= s
->muted
!= mute
;
706 if (s
->set_mute
&& s
->set_mute(s
) < 0)
710 pa_asyncmsgq_post(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_MUTE
, PA_UINT_TO_PTR(mute
), 0, NULL
, NULL
);
713 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
716 pa_bool_t
pa_sink_get_mute(pa_sink
*s
) {
719 pa_sink_assert_ref(s
);
720 pa_assert(PA_SINK_LINKED(s
->state
));
722 old_muted
= s
->muted
;
724 if (s
->get_mute
&& s
->get_mute(s
) < 0)
727 if (!s
->get_mute
&& s
->refresh_mute
)
728 pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_MUTE
, &s
->muted
, 0, NULL
);
730 if (old_muted
!= s
->muted
)
731 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
736 void pa_sink_set_module(pa_sink
*s
, pa_module
*m
) {
737 pa_sink_assert_ref(s
);
744 if (s
->monitor_source
)
745 pa_source_set_module(s
->monitor_source
, m
);
747 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
750 void pa_sink_set_description(pa_sink
*s
, const char *description
) {
751 pa_sink_assert_ref(s
);
753 if (!description
&& !s
->description
)
756 if (description
&& s
->description
&& !strcmp(description
, s
->description
))
759 pa_xfree(s
->description
);
760 s
->description
= pa_xstrdup(description
);
762 if (s
->monitor_source
) {
765 n
= pa_sprintf_malloc("Monitor Source of %s", s
->description
? s
->description
: s
->name
);
766 pa_source_set_description(s
->monitor_source
, n
);
770 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
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 if (s
->monitor_source
)
782 ret
+= pa_source_used_by(s
->monitor_source
);
787 unsigned pa_sink_used_by(pa_sink
*s
) {
790 pa_sink_assert_ref(s
);
791 pa_assert(PA_SINK_LINKED(s
->state
));
793 ret
= pa_idxset_size(s
->inputs
);
795 pa_assert(ret
>= s
->n_corked
);
799 if (s
->monitor_source
)
800 ret
+= pa_source_used_by(s
->monitor_source
);
805 int pa_sink_process_msg(pa_msgobject
*o
, int code
, void *userdata
, int64_t offset
, pa_memchunk
*chunk
) {
806 pa_sink
*s
= PA_SINK(o
);
807 pa_sink_assert_ref(s
);
808 pa_assert(PA_SINK_LINKED(s
->thread_info
.state
));
810 switch ((pa_sink_message_t
) code
) {
812 case PA_SINK_MESSAGE_ADD_INPUT
: {
813 pa_sink_input
*i
= PA_SINK_INPUT(userdata
);
814 pa_hashmap_put(s
->thread_info
.inputs
, PA_UINT32_TO_PTR(i
->index
), pa_sink_input_ref(i
));
816 /* Since the caller sleeps in pa_sink_input_put(), we can
817 * safely access data outside of thread_info even though
820 if ((i
->thread_info
.sync_prev
= i
->sync_prev
)) {
821 pa_assert(i
->sink
== i
->thread_info
.sync_prev
->sink
);
822 pa_assert(i
->sync_prev
->sync_next
== i
);
823 i
->thread_info
.sync_prev
->thread_info
.sync_next
= i
;
826 if ((i
->thread_info
.sync_next
= i
->sync_next
)) {
827 pa_assert(i
->sink
== i
->thread_info
.sync_next
->sink
);
828 pa_assert(i
->sync_next
->sync_prev
== i
);
829 i
->thread_info
.sync_next
->thread_info
.sync_prev
= i
;
832 pa_assert(!i
->thread_info
.attached
);
833 i
->thread_info
.attached
= TRUE
;
838 /* If you change anything here, make sure to change the
839 * ghost sink input handling a few lines down at
840 * PA_SINK_MESSAGE_REMOVE_INPUT_AND_BUFFER, too. */
845 case PA_SINK_MESSAGE_REMOVE_INPUT
: {
846 pa_sink_input
*i
= PA_SINK_INPUT(userdata
);
848 /* If you change anything here, make sure to change the
849 * sink input handling a few lines down at
850 * PA_SINK_MESSAGE_REMOVE_INPUT_AND_BUFFER, too. */
855 pa_assert(i
->thread_info
.attached
);
856 i
->thread_info
.attached
= FALSE
;
858 /* Since the caller sleeps in pa_sink_input_unlink(),
859 * we can safely access data outside of thread_info even
860 * though it is mutable */
862 pa_assert(!i
->thread_info
.sync_prev
);
863 pa_assert(!i
->thread_info
.sync_next
);
865 if (i
->thread_info
.sync_prev
) {
866 i
->thread_info
.sync_prev
->thread_info
.sync_next
= i
->thread_info
.sync_prev
->sync_next
;
867 i
->thread_info
.sync_prev
= NULL
;
870 if (i
->thread_info
.sync_next
) {
871 i
->thread_info
.sync_next
->thread_info
.sync_prev
= i
->thread_info
.sync_next
->sync_prev
;
872 i
->thread_info
.sync_next
= NULL
;
875 if (pa_hashmap_remove(s
->thread_info
.inputs
, PA_UINT32_TO_PTR(i
->index
)))
876 pa_sink_input_unref(i
);
881 case PA_SINK_MESSAGE_REMOVE_INPUT_AND_BUFFER
: {
882 pa_sink_input_move_info
*info
= userdata
;
885 /* We don't support moving synchronized streams. */
886 pa_assert(!info
->sink_input
->sync_prev
);
887 pa_assert(!info
->sink_input
->sync_next
);
888 pa_assert(!info
->sink_input
->thread_info
.sync_next
);
889 pa_assert(!info
->sink_input
->thread_info
.sync_prev
);
891 if (info
->sink_input
->detach
)
892 info
->sink_input
->detach(info
->sink_input
);
894 pa_assert(info
->sink_input
->thread_info
.attached
);
895 info
->sink_input
->thread_info
.attached
= FALSE
;
897 if (info
->ghost_sink_input
) {
898 pa_assert(info
->buffer_bytes
> 0);
899 pa_assert(info
->buffer
);
901 volume_is_norm
= pa_cvolume_is_norm(&info
->sink_input
->thread_info
.volume
);
903 pa_log_debug("Buffering %lu bytes ...", (unsigned long) info
->buffer_bytes
);
905 while (info
->buffer_bytes
> 0) {
906 pa_memchunk memchunk
;
910 if (pa_sink_input_peek(info
->sink_input
, info
->buffer_bytes
, &memchunk
, &volume
) < 0)
913 n
= memchunk
.length
> info
->buffer_bytes
? info
->buffer_bytes
: memchunk
.length
;
914 pa_sink_input_drop(info
->sink_input
, n
);
917 if (!volume_is_norm
) {
918 pa_memchunk_make_writable(&memchunk
, 0);
919 pa_volume_memchunk(&memchunk
, &s
->sample_spec
, &volume
);
922 if (pa_memblockq_push(info
->buffer
, &memchunk
) < 0) {
923 pa_memblock_unref(memchunk
.memblock
);
927 pa_memblock_unref(memchunk
.memblock
);
928 info
->buffer_bytes
-= n
;
931 /* Add the remaining already resampled chunk to the buffer */
932 if (info
->sink_input
->thread_info
.resampled_chunk
.memblock
)
933 pa_memblockq_push(info
->buffer
, &info
->sink_input
->thread_info
.resampled_chunk
);
935 pa_memblockq_sink_input_set_queue(info
->ghost_sink_input
, info
->buffer
);
937 pa_log_debug("Buffered %lu bytes ...", (unsigned long) pa_memblockq_get_length(info
->buffer
));
940 /* Let's remove the sink input ...*/
941 if (pa_hashmap_remove(s
->thread_info
.inputs
, PA_UINT32_TO_PTR(info
->sink_input
->index
)))
942 pa_sink_input_unref(info
->sink_input
);
944 /* .. and add the ghost sink input instead */
945 if (info
->ghost_sink_input
) {
946 pa_hashmap_put(s
->thread_info
.inputs
, PA_UINT32_TO_PTR(info
->ghost_sink_input
->index
), pa_sink_input_ref(info
->ghost_sink_input
));
947 info
->ghost_sink_input
->thread_info
.sync_prev
= info
->ghost_sink_input
->thread_info
.sync_next
= NULL
;
949 pa_assert(!info
->ghost_sink_input
->thread_info
.attached
);
950 info
->ghost_sink_input
->thread_info
.attached
= TRUE
;
952 if (info
->ghost_sink_input
->attach
)
953 info
->ghost_sink_input
->attach(info
->ghost_sink_input
);
959 case PA_SINK_MESSAGE_SET_VOLUME
:
960 s
->thread_info
.soft_volume
= *((pa_cvolume
*) userdata
);
963 case PA_SINK_MESSAGE_SET_MUTE
:
964 s
->thread_info
.soft_muted
= PA_PTR_TO_UINT(userdata
);
967 case PA_SINK_MESSAGE_GET_VOLUME
:
968 *((pa_cvolume
*) userdata
) = s
->thread_info
.soft_volume
;
971 case PA_SINK_MESSAGE_GET_MUTE
:
972 *((pa_bool_t
*) userdata
) = s
->thread_info
.soft_muted
;
975 case PA_SINK_MESSAGE_PING
:
978 case PA_SINK_MESSAGE_SET_STATE
:
980 s
->thread_info
.state
= PA_PTR_TO_UINT(userdata
);
983 case PA_SINK_MESSAGE_DETACH
:
985 /* We're detaching all our input streams so that the
986 * asyncmsgq and rtpoll fields can be changed without
988 pa_sink_detach_within_thread(s
);
991 case PA_SINK_MESSAGE_ATTACH
:
993 /* Reattach all streams */
994 pa_sink_attach_within_thread(s
);
997 case PA_SINK_MESSAGE_GET_LATENCY
:
998 case PA_SINK_MESSAGE_MAX
:
1005 int pa_sink_suspend_all(pa_core
*c
, pa_bool_t suspend
) {
1010 pa_core_assert_ref(c
);
1012 for (sink
= PA_SINK(pa_idxset_first(c
->sinks
, &idx
)); sink
; sink
= PA_SINK(pa_idxset_next(c
->sinks
, &idx
)))
1013 ret
-= pa_sink_suspend(sink
, suspend
) < 0;
1018 void pa_sink_detach(pa_sink
*s
) {
1019 pa_sink_assert_ref(s
);
1020 pa_assert(PA_SINK_LINKED(s
->state
));
1022 pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_DETACH
, NULL
, 0, NULL
);
1025 void pa_sink_attach(pa_sink
*s
) {
1026 pa_sink_assert_ref(s
);
1027 pa_assert(PA_SINK_LINKED(s
->state
));
1029 pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_ATTACH
, NULL
, 0, NULL
);
1032 void pa_sink_detach_within_thread(pa_sink
*s
) {
1036 pa_sink_assert_ref(s
);
1037 pa_assert(PA_SINK_LINKED(s
->thread_info
.state
));
1039 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)))
1043 if (s
->monitor_source
)
1044 pa_source_detach_within_thread(s
->monitor_source
);
1047 void pa_sink_attach_within_thread(pa_sink
*s
) {
1051 pa_sink_assert_ref(s
);
1052 pa_assert(PA_SINK_LINKED(s
->thread_info
.state
));
1054 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)))
1058 if (s
->monitor_source
)
1059 pa_source_attach_within_thread(s
->monitor_source
);