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>
36 #include <pulse/timeval.h>
38 #include <pulsecore/sink-input.h>
39 #include <pulsecore/namereg.h>
40 #include <pulsecore/core-util.h>
41 #include <pulsecore/sample-util.h>
42 #include <pulsecore/core-subscribe.h>
43 #include <pulsecore/log.h>
44 #include <pulsecore/macro.h>
45 #include <pulsecore/play-memblockq.h>
49 #define MAX_MIX_CHANNELS 32
50 #define MIX_BUFFER_LENGTH (PA_PAGE_SIZE)
51 #define DEFAULT_MIN_LATENCY (4*PA_USEC_PER_MSEC)
53 static PA_DEFINE_CHECK_TYPE(pa_sink
, pa_msgobject
);
55 static void sink_free(pa_object
*s
);
57 pa_sink_new_data
* pa_sink_new_data_init(pa_sink_new_data
*data
) {
60 memset(data
, 0, sizeof(*data
));
61 data
->proplist
= pa_proplist_new();
66 void pa_sink_new_data_set_name(pa_sink_new_data
*data
, const char *name
) {
70 data
->name
= pa_xstrdup(name
);
73 void pa_sink_new_data_set_sample_spec(pa_sink_new_data
*data
, const pa_sample_spec
*spec
) {
76 if ((data
->sample_spec_is_set
= !!spec
))
77 data
->sample_spec
= *spec
;
80 void pa_sink_new_data_set_channel_map(pa_sink_new_data
*data
, const pa_channel_map
*map
) {
83 if ((data
->channel_map_is_set
= !!map
))
84 data
->channel_map
= *map
;
87 void pa_sink_new_data_set_volume(pa_sink_new_data
*data
, const pa_cvolume
*volume
) {
90 if ((data
->volume_is_set
= !!volume
))
91 data
->volume
= *volume
;
94 void pa_sink_new_data_set_muted(pa_sink_new_data
*data
, pa_bool_t mute
) {
97 data
->muted_is_set
= TRUE
;
101 void pa_sink_new_data_done(pa_sink_new_data
*data
) {
104 pa_xfree(data
->name
);
105 pa_proplist_free(data
->proplist
);
108 pa_sink
* pa_sink_new(
110 pa_sink_new_data
*data
,
111 pa_sink_flags_t flags
) {
116 char st
[PA_SAMPLE_SPEC_SNPRINT_MAX
], cm
[PA_CHANNEL_MAP_SNPRINT_MAX
];
117 pa_source_new_data source_data
;
122 if (pa_hook_fire(&core
->hooks
[PA_CORE_HOOK_SINK_NEW
], data
) < 0)
125 pa_return_null_if_fail(!data
->driver
|| pa_utf8_valid(data
->driver
));
126 pa_return_null_if_fail(data
->name
&& pa_utf8_valid(data
->name
) && data
->name
[0]);
128 pa_return_null_if_fail(data
->sample_spec_is_set
&& pa_sample_spec_valid(&data
->sample_spec
));
130 if (!data
->channel_map_is_set
)
131 pa_return_null_if_fail(pa_channel_map_init_auto(&data
->channel_map
, data
->sample_spec
.channels
, PA_CHANNEL_MAP_DEFAULT
));
133 pa_return_null_if_fail(pa_channel_map_valid(&data
->channel_map
));
134 pa_return_null_if_fail(data
->channel_map
.channels
== data
->sample_spec
.channels
);
136 if (!data
->volume_is_set
)
137 pa_cvolume_reset(&data
->volume
, data
->sample_spec
.channels
);
139 pa_return_null_if_fail(pa_cvolume_valid(&data
->volume
));
140 pa_return_null_if_fail(data
->volume
.channels
== data
->sample_spec
.channels
);
142 if (!data
->muted_is_set
)
145 if (pa_hook_fire(&core
->hooks
[PA_CORE_HOOK_SINK_FIXATE
], data
) < 0)
148 s
= pa_msgobject_new(pa_sink
);
150 if (!(name
= pa_namereg_register(core
, data
->name
, PA_NAMEREG_SINK
, s
, data
->namereg_fail
))) {
155 s
->parent
.parent
.free
= sink_free
;
156 s
->parent
.process_msg
= pa_sink_process_msg
;
159 s
->state
= PA_SINK_INIT
;
161 s
->name
= pa_xstrdup(name
);
162 s
->proplist
= pa_proplist_copy(data
->proplist
);
163 s
->driver
= pa_xstrdup(data
->driver
);
164 s
->module
= data
->module
;
166 s
->sample_spec
= data
->sample_spec
;
167 s
->channel_map
= data
->channel_map
;
169 s
->inputs
= pa_idxset_new(NULL
, NULL
);
172 s
->volume
= data
->volume
;
173 s
->muted
= data
->muted
;
174 s
->refresh_volume
= s
->refresh_mute
= FALSE
;
176 s
->get_latency
= NULL
;
177 s
->set_volume
= NULL
;
178 s
->get_volume
= NULL
;
182 s
->request_rewind
= NULL
;
183 s
->update_requested_latency
= NULL
;
188 s
->silence
= pa_silence_memblock_new(core
->mempool
, &s
->sample_spec
, 0);
190 s
->min_latency
= DEFAULT_MIN_LATENCY
;
192 s
->thread_info
.inputs
= pa_hashmap_new(pa_idxset_trivial_hash_func
, pa_idxset_trivial_compare_func
);
193 s
->thread_info
.soft_volume
= s
->volume
;
194 s
->thread_info
.soft_muted
= s
->muted
;
195 s
->thread_info
.state
= s
->state
;
196 s
->thread_info
.rewind_nbytes
= 0;
197 s
->thread_info
.max_rewind
= 0;
198 s
->thread_info
.requested_latency_valid
= TRUE
;
199 s
->thread_info
.requested_latency
= 0;
201 pa_assert_se(pa_idxset_put(core
->sinks
, s
, &s
->index
) >= 0);
203 pa_log_info("Created sink %u \"%s\" with sample spec %s and channel map %s",
206 pa_sample_spec_snprint(st
, sizeof(st
), &s
->sample_spec
),
207 pa_channel_map_snprint(cm
, sizeof(cm
), &s
->channel_map
));
209 pa_source_new_data_init(&source_data
);
210 pa_source_new_data_set_sample_spec(&source_data
, &s
->sample_spec
);
211 pa_source_new_data_set_channel_map(&source_data
, &s
->channel_map
);
212 source_data
.name
= pa_sprintf_malloc("%s.monitor", name
);
213 source_data
.driver
= data
->driver
;
215 d
= pa_sprintf_malloc("Monitor Source of %s", s
->name
);
216 pa_proplist_sets(data
->proplist
, PA_PROP_DEVICE_DESCRIPTION
, d
);
218 pa_proplist_sets(data
->proplist
, PA_PROP_DEVICE_CLASS
, "monitor");
220 s
->monitor_source
= pa_source_new(core
, &source_data
, 0);
222 pa_source_new_data_done(&source_data
);
224 if (!s
->monitor_source
) {
230 s
->monitor_source
->monitor_of
= s
;
235 static int sink_set_state(pa_sink
*s
, pa_sink_state_t state
) {
237 pa_bool_t suspend_change
;
241 if (s
->state
== state
)
245 (s
->state
== PA_SINK_SUSPENDED
&& PA_SINK_OPENED(state
)) ||
246 (PA_SINK_OPENED(s
->state
) && state
== PA_SINK_SUSPENDED
);
249 if ((ret
= s
->set_state(s
, state
)) < 0)
252 if (pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_STATE
, PA_UINT_TO_PTR(state
), 0, NULL
) < 0)
257 if (suspend_change
) {
261 /* We're suspending or resuming, tell everyone about it */
263 for (i
= PA_SINK_INPUT(pa_idxset_first(s
->inputs
, &idx
)); i
; i
= PA_SINK_INPUT(pa_idxset_next(s
->inputs
, &idx
)))
265 i
->suspend(i
, state
== PA_SINK_SUSPENDED
);
268 if (state
!= PA_SINK_UNLINKED
) /* if we enter UNLINKED state pa_sink_unlink() will fire the apropriate events */
269 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_STATE_CHANGED
], s
);
274 void pa_sink_put(pa_sink
* s
) {
275 pa_sink_assert_ref(s
);
277 pa_assert(s
->state
== PA_SINK_INIT
);
278 pa_assert(s
->asyncmsgq
);
279 pa_assert(s
->rtpoll
);
281 if (s
->get_volume
&& s
->set_volume
)
282 s
->flags
|= PA_SINK_HW_VOLUME_CTRL
;
284 s
->flags
= (s
->flags
& ~PA_SINK_HW_VOLUME_CTRL
) | PA_SINK_DECIBEL_VOLUME
;
286 if (s
->get_mute
&& s
->set_mute
)
287 s
->flags
|= PA_SINK_HW_MUTE_CTRL
;
289 s
->flags
&= ~PA_SINK_HW_MUTE_CTRL
;
291 pa_assert_se(sink_set_state(s
, PA_SINK_IDLE
) == 0);
293 pa_source_put(s
->monitor_source
);
295 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
| PA_SUBSCRIPTION_EVENT_NEW
, s
->index
);
296 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_PUT
], s
);
299 void pa_sink_unlink(pa_sink
* s
) {
301 pa_sink_input
*i
, *j
= NULL
;
305 /* Please note that pa_sink_unlink() does more than simply
306 * reversing pa_sink_put(). It also undoes the registrations
307 * already done in pa_sink_new()! */
309 /* All operations here shall be idempotent, i.e. pa_sink_unlink()
310 * may be called multiple times on the same sink without bad
313 linked
= PA_SINK_LINKED(s
->state
);
316 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_UNLINK
], s
);
318 if (s
->state
!= PA_SINK_UNLINKED
)
319 pa_namereg_unregister(s
->core
, s
->name
);
320 pa_idxset_remove_by_data(s
->core
->sinks
, s
, NULL
);
322 while ((i
= pa_idxset_first(s
->inputs
, NULL
))) {
324 pa_sink_input_kill(i
);
329 sink_set_state(s
, PA_SINK_UNLINKED
);
331 s
->state
= PA_SINK_UNLINKED
;
333 s
->get_latency
= NULL
;
334 s
->get_volume
= NULL
;
335 s
->set_volume
= NULL
;
339 s
->request_rewind
= NULL
;
340 s
->update_requested_latency
= NULL
;
342 if (s
->monitor_source
)
343 pa_source_unlink(s
->monitor_source
);
346 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
| PA_SUBSCRIPTION_EVENT_REMOVE
, s
->index
);
347 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_UNLINK_POST
], s
);
351 static void sink_free(pa_object
*o
) {
352 pa_sink
*s
= PA_SINK(o
);
356 pa_assert(pa_sink_refcnt(s
) == 0);
358 if (PA_SINK_LINKED(s
->state
))
361 pa_log_info("Freeing sink %u \"%s\"", s
->index
, s
->name
);
363 if (s
->monitor_source
) {
364 pa_source_unref(s
->monitor_source
);
365 s
->monitor_source
= NULL
;
368 pa_idxset_free(s
->inputs
, NULL
, NULL
);
370 while ((i
= pa_hashmap_steal_first(s
->thread_info
.inputs
)))
371 pa_sink_input_unref(i
);
373 pa_hashmap_free(s
->thread_info
.inputs
, NULL
, NULL
);
376 pa_memblock_unref(s
->silence
);
382 pa_proplist_free(s
->proplist
);
387 void pa_sink_set_asyncmsgq(pa_sink
*s
, pa_asyncmsgq
*q
) {
388 pa_sink_assert_ref(s
);
393 if (s
->monitor_source
)
394 pa_source_set_asyncmsgq(s
->monitor_source
, q
);
397 void pa_sink_set_rtpoll(pa_sink
*s
, pa_rtpoll
*p
) {
398 pa_sink_assert_ref(s
);
402 if (s
->monitor_source
)
403 pa_source_set_rtpoll(s
->monitor_source
, p
);
406 int pa_sink_update_status(pa_sink
*s
) {
407 pa_sink_assert_ref(s
);
408 pa_assert(PA_SINK_LINKED(s
->state
));
410 if (s
->state
== PA_SINK_SUSPENDED
)
413 return sink_set_state(s
, pa_sink_used_by(s
) ? PA_SINK_RUNNING
: PA_SINK_IDLE
);
416 int pa_sink_suspend(pa_sink
*s
, pa_bool_t suspend
) {
417 pa_sink_assert_ref(s
);
418 pa_assert(PA_SINK_LINKED(s
->state
));
421 return sink_set_state(s
, PA_SINK_SUSPENDED
);
423 return sink_set_state(s
, pa_sink_used_by(s
) ? PA_SINK_RUNNING
: PA_SINK_IDLE
);
426 void pa_sink_ping(pa_sink
*s
) {
427 pa_sink_assert_ref(s
);
428 pa_assert(PA_SINK_LINKED(s
->state
));
430 pa_asyncmsgq_post(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_PING
, NULL
, 0, NULL
, NULL
);
433 void pa_sink_process_rewind(pa_sink
*s
) {
436 pa_sink_assert_ref(s
);
437 pa_assert(PA_SINK_LINKED(s
->state
));
439 if (s
->thread_info
.rewind_nbytes
<= 0)
442 pa_log_debug("Processing rewind...");
444 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
))) {
445 pa_sink_input_assert_ref(i
);
447 pa_sink_input_rewind(i
, s
->thread_info
.rewind_nbytes
);
450 s
->thread_info
.rewind_nbytes
= 0;
453 static unsigned fill_mix_info(pa_sink
*s
, size_t length
, pa_mix_info
*info
, unsigned maxinfo
) {
458 pa_sink_assert_ref(s
);
461 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)) && maxinfo
> 0) {
462 pa_sink_input_assert_ref(i
);
464 if (pa_sink_input_peek(i
, length
, &info
->chunk
, &info
->volume
) < 0)
467 if (pa_memblock_is_silence(info
->chunk
.memblock
)) {
468 pa_memblock_unref(info
->chunk
.memblock
);
472 info
->userdata
= pa_sink_input_ref(i
);
474 pa_assert(info
->chunk
.memblock
);
475 pa_assert(info
->chunk
.length
> 0);
485 static void inputs_drop(pa_sink
*s
, pa_mix_info
*info
, unsigned n
, size_t length
) {
489 unsigned n_unreffed
= 0;
491 pa_sink_assert_ref(s
);
493 /* We optimize for the case where the order of the inputs has not changed */
495 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
))) {
499 pa_sink_input_assert_ref(i
);
503 /* Let's try to find the matching entry info the pa_mix_info array */
504 for (j
= 0; j
< n
; j
++) {
506 if (info
[p
].userdata
== i
) {
517 pa_sink_input_drop(i
, length
);
520 pa_sink_input_unref(m
->userdata
);
522 if (m
->chunk
.memblock
)
523 pa_memblock_unref(m
->chunk
.memblock
);
524 pa_memchunk_reset(&m
->chunk
);
530 /* Now drop references to entries that are included in the
531 * pa_mix_info array but don't exist anymore */
533 if (n_unreffed
< n
) {
534 for (; n
> 0; info
++, n
--) {
536 pa_sink_input_unref(info
->userdata
);
537 if (info
->chunk
.memblock
)
538 pa_memblock_unref(info
->chunk
.memblock
);
543 void pa_sink_render(pa_sink
*s
, size_t length
, pa_memchunk
*result
) {
544 pa_mix_info info
[MAX_MIX_CHANNELS
];
546 size_t block_size_max
;
548 pa_sink_assert_ref(s
);
549 pa_assert(PA_SINK_OPENED(s
->thread_info
.state
));
550 pa_assert(pa_frame_aligned(length
, &s
->sample_spec
));
555 s
->thread_info
.rewind_nbytes
= 0;
558 length
= pa_frame_align(MIX_BUFFER_LENGTH
, &s
->sample_spec
);
560 block_size_max
= pa_mempool_block_size_max(s
->core
->mempool
);
561 if (length
> block_size_max
)
562 length
= pa_frame_align(block_size_max
, &s
->sample_spec
);
564 pa_assert(length
> 0);
566 n
= s
->thread_info
.state
== PA_SINK_RUNNING
? fill_mix_info(s
, length
, info
, MAX_MIX_CHANNELS
) : 0;
570 result
->memblock
= pa_memblock_ref(s
->silence
);
571 result
->length
= PA_MIN(pa_memblock_get_length(s
->silence
), length
);
577 *result
= info
[0].chunk
;
578 pa_memblock_ref(result
->memblock
);
580 if (result
->length
> length
)
581 result
->length
= length
;
583 pa_sw_cvolume_multiply(&volume
, &s
->thread_info
.soft_volume
, &info
[0].volume
);
585 if (s
->thread_info
.soft_muted
|| !pa_cvolume_is_norm(&volume
)) {
586 pa_memchunk_make_writable(result
, 0);
587 if (s
->thread_info
.soft_muted
|| pa_cvolume_is_muted(&volume
))
588 pa_silence_memchunk(result
, &s
->sample_spec
);
590 pa_volume_memchunk(result
, &s
->sample_spec
, &volume
);
594 result
->memblock
= pa_memblock_new(s
->core
->mempool
, length
);
596 ptr
= pa_memblock_acquire(result
->memblock
);
597 result
->length
= pa_mix(info
, n
, ptr
, length
, &s
->sample_spec
, &s
->thread_info
.soft_volume
, s
->thread_info
.soft_muted
);
598 pa_memblock_release(result
->memblock
);
603 if (s
->thread_info
.state
== PA_SINK_RUNNING
)
604 inputs_drop(s
, info
, n
, result
->length
);
606 if (s
->monitor_source
&& PA_SOURCE_OPENED(pa_source_get_state(s
->monitor_source
)))
607 pa_source_post(s
->monitor_source
, result
);
612 void pa_sink_render_into(pa_sink
*s
, pa_memchunk
*target
) {
613 pa_mix_info info
[MAX_MIX_CHANNELS
];
616 pa_sink_assert_ref(s
);
617 pa_assert(PA_SINK_OPENED(s
->thread_info
.state
));
619 pa_assert(target
->memblock
);
620 pa_assert(target
->length
> 0);
621 pa_assert(pa_frame_aligned(target
->length
, &s
->sample_spec
));
625 s
->thread_info
.rewind_nbytes
= 0;
627 n
= s
->thread_info
.state
== PA_SINK_RUNNING
? fill_mix_info(s
, target
->length
, info
, MAX_MIX_CHANNELS
) : 0;
630 pa_silence_memchunk(target
, &s
->sample_spec
);
632 if (target
->length
> info
[0].chunk
.length
)
633 target
->length
= info
[0].chunk
.length
;
635 if (s
->thread_info
.soft_muted
)
636 pa_silence_memchunk(target
, &s
->sample_spec
);
641 ptr
= pa_memblock_acquire(target
->memblock
);
642 src
= pa_memblock_acquire(info
[0].chunk
.memblock
);
644 memcpy((uint8_t*) ptr
+ target
->index
,
645 (uint8_t*) src
+ info
[0].chunk
.index
,
648 pa_memblock_release(target
->memblock
);
649 pa_memblock_release(info
[0].chunk
.memblock
);
651 pa_sw_cvolume_multiply(&volume
, &s
->thread_info
.soft_volume
, &info
[0].volume
);
653 if (!pa_cvolume_is_norm(&volume
))
654 pa_volume_memchunk(target
, &s
->sample_spec
, &volume
);
660 ptr
= pa_memblock_acquire(target
->memblock
);
662 target
->length
= pa_mix(info
, n
,
663 (uint8_t*) ptr
+ target
->index
,
666 &s
->thread_info
.soft_volume
,
667 s
->thread_info
.soft_muted
);
669 pa_memblock_release(target
->memblock
);
672 if (s
->thread_info
.state
== PA_SINK_RUNNING
)
673 inputs_drop(s
, info
, n
, target
->length
);
675 if (s
->monitor_source
&& PA_SOURCE_OPENED(pa_source_get_state(s
->monitor_source
)))
676 pa_source_post(s
->monitor_source
, target
);
681 void pa_sink_render_into_full(pa_sink
*s
, pa_memchunk
*target
) {
685 pa_sink_assert_ref(s
);
686 pa_assert(PA_SINK_OPENED(s
->thread_info
.state
));
688 pa_assert(target
->memblock
);
689 pa_assert(target
->length
> 0);
690 pa_assert(pa_frame_aligned(target
->length
, &s
->sample_spec
));
694 s
->thread_info
.rewind_nbytes
= 0;
703 pa_sink_render_into(s
, &chunk
);
712 void pa_sink_render_full(pa_sink
*s
, size_t length
, pa_memchunk
*result
) {
713 pa_sink_assert_ref(s
);
714 pa_assert(PA_SINK_OPENED(s
->thread_info
.state
));
715 pa_assert(length
> 0);
716 pa_assert(pa_frame_aligned(length
, &s
->sample_spec
));
719 s
->thread_info
.rewind_nbytes
= 0;
721 /*** This needs optimization ***/
724 result
->length
= length
;
725 result
->memblock
= pa_memblock_new(s
->core
->mempool
, length
);
727 pa_sink_render_into_full(s
, result
);
730 void pa_sink_skip(pa_sink
*s
, size_t length
) {
734 pa_sink_assert_ref(s
);
735 pa_assert(PA_SINK_OPENED(s
->thread_info
.state
));
736 pa_assert(length
> 0);
737 pa_assert(pa_frame_aligned(length
, &s
->sample_spec
));
739 s
->thread_info
.rewind_nbytes
= 0;
741 if (pa_source_used_by(s
->monitor_source
)) {
744 /* If something is connected to our monitor source, we have to
745 * pass valid data to it */
748 pa_sink_render(s
, length
, &chunk
);
749 pa_memblock_unref(chunk
.memblock
);
751 pa_assert(chunk
.length
<= length
);
752 length
-= chunk
.length
;
756 /* Ok, noone cares about the rendered data, so let's not even render it */
758 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
))) {
759 pa_sink_input_assert_ref(i
);
760 pa_sink_input_drop(i
, length
);
765 pa_usec_t
pa_sink_get_latency(pa_sink
*s
) {
768 pa_sink_assert_ref(s
);
769 pa_assert(PA_SINK_LINKED(s
->state
));
771 /* The returned value is supposed to be in the time domain of the sound card! */
773 if (!PA_SINK_OPENED(s
->state
))
777 return s
->get_latency(s
);
779 if (pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_LATENCY
, &usec
, 0, NULL
) < 0)
785 void pa_sink_set_volume(pa_sink
*s
, const pa_cvolume
*volume
) {
788 pa_sink_assert_ref(s
);
789 pa_assert(PA_SINK_LINKED(s
->state
));
792 changed
= !pa_cvolume_equal(volume
, &s
->volume
);
795 if (s
->set_volume
&& s
->set_volume(s
) < 0)
796 s
->set_volume
= NULL
;
799 pa_asyncmsgq_post(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_VOLUME
, pa_xnewdup(struct pa_cvolume
, volume
, 1), 0, NULL
, pa_xfree
);
802 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
805 const pa_cvolume
*pa_sink_get_volume(pa_sink
*s
) {
806 struct pa_cvolume old_volume
;
808 pa_sink_assert_ref(s
);
809 pa_assert(PA_SINK_LINKED(s
->state
));
811 old_volume
= s
->volume
;
813 if (s
->get_volume
&& s
->get_volume(s
) < 0)
814 s
->get_volume
= NULL
;
816 if (!s
->get_volume
&& s
->refresh_volume
)
817 pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_VOLUME
, &s
->volume
, 0, NULL
);
819 if (!pa_cvolume_equal(&old_volume
, &s
->volume
))
820 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
825 void pa_sink_set_mute(pa_sink
*s
, pa_bool_t mute
) {
828 pa_sink_assert_ref(s
);
829 pa_assert(PA_SINK_LINKED(s
->state
));
831 changed
= s
->muted
!= mute
;
834 if (s
->set_mute
&& s
->set_mute(s
) < 0)
838 pa_asyncmsgq_post(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_MUTE
, PA_UINT_TO_PTR(mute
), 0, NULL
, NULL
);
841 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
844 pa_bool_t
pa_sink_get_mute(pa_sink
*s
) {
847 pa_sink_assert_ref(s
);
848 pa_assert(PA_SINK_LINKED(s
->state
));
850 old_muted
= s
->muted
;
852 if (s
->get_mute
&& s
->get_mute(s
) < 0)
855 if (!s
->get_mute
&& s
->refresh_mute
)
856 pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_MUTE
, &s
->muted
, 0, NULL
);
858 if (old_muted
!= s
->muted
)
859 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
864 void pa_sink_set_description(pa_sink
*s
, const char *description
) {
866 pa_sink_assert_ref(s
);
868 if (!description
&& !pa_proplist_contains(s
->proplist
, PA_PROP_DEVICE_DESCRIPTION
))
871 old
= pa_proplist_gets(s
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
873 if (old
&& description
&& !strcmp(old
, description
))
877 pa_proplist_sets(s
->proplist
, PA_PROP_DEVICE_DESCRIPTION
, description
);
879 pa_proplist_unset(s
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
881 if (s
->monitor_source
) {
884 n
= pa_sprintf_malloc("Monitor Source of %s", description
? description
: s
->name
);
885 pa_source_set_description(s
->monitor_source
, n
);
889 if (PA_SINK_LINKED(s
->state
)) {
890 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
891 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_PROPLIST_CHANGED
], s
);
895 unsigned pa_sink_linked_by(pa_sink
*s
) {
898 pa_sink_assert_ref(s
);
899 pa_assert(PA_SINK_LINKED(s
->state
));
901 ret
= pa_idxset_size(s
->inputs
);
903 /* We add in the number of streams connected to us here. Please
904 * not the asymmmetry to pa_sink_used_by()! */
906 if (s
->monitor_source
)
907 ret
+= pa_source_linked_by(s
->monitor_source
);
912 unsigned pa_sink_used_by(pa_sink
*s
) {
915 pa_sink_assert_ref(s
);
916 pa_assert(PA_SINK_LINKED(s
->state
));
918 ret
= pa_idxset_size(s
->inputs
);
919 pa_assert(ret
>= s
->n_corked
);
922 /* Streams connected to our monitor source do not matter for
923 * pa_sink_used_by()!.*/
928 int pa_sink_process_msg(pa_msgobject
*o
, int code
, void *userdata
, int64_t offset
, pa_memchunk
*chunk
) {
929 pa_sink
*s
= PA_SINK(o
);
930 pa_sink_assert_ref(s
);
931 pa_assert(s
->thread_info
.state
!= PA_SINK_UNLINKED
);
933 switch ((pa_sink_message_t
) code
) {
935 case PA_SINK_MESSAGE_ADD_INPUT
: {
936 pa_sink_input
*i
= PA_SINK_INPUT(userdata
);
938 pa_hashmap_put(s
->thread_info
.inputs
, PA_UINT32_TO_PTR(i
->index
), pa_sink_input_ref(i
));
940 /* Since the caller sleeps in pa_sink_input_put(), we can
941 * safely access data outside of thread_info even though
944 if ((i
->thread_info
.sync_prev
= i
->sync_prev
)) {
945 pa_assert(i
->sink
== i
->thread_info
.sync_prev
->sink
);
946 pa_assert(i
->sync_prev
->sync_next
== i
);
947 i
->thread_info
.sync_prev
->thread_info
.sync_next
= i
;
950 if ((i
->thread_info
.sync_next
= i
->sync_next
)) {
951 pa_assert(i
->sink
== i
->thread_info
.sync_next
->sink
);
952 pa_assert(i
->sync_next
->sync_prev
== i
);
953 i
->thread_info
.sync_next
->thread_info
.sync_prev
= i
;
956 pa_sink_input_set_max_rewind(i
, s
->thread_info
.max_rewind
);
958 pa_assert(!i
->thread_info
.attached
);
959 i
->thread_info
.attached
= TRUE
;
964 /* If you change anything here, make sure to change the
965 * ghost sink input handling a few lines down at
966 * PA_SINK_MESSAGE_REMOVE_INPUT_AND_BUFFER, too. */
968 pa_sink_invalidate_requested_latency(s
);
970 /* i->thread_info.ignore_rewind = TRUE; */
971 /* pa_sink_request_rewind(s, 0); */
976 case PA_SINK_MESSAGE_REMOVE_INPUT
: {
977 pa_sink_input
*i
= PA_SINK_INPUT(userdata
);
979 /* If you change anything here, make sure to change the
980 * sink input handling a few lines down at
981 * PA_SINK_MESSAGE_REMOVE_INPUT_AND_BUFFER, too. */
986 pa_assert(i
->thread_info
.attached
);
987 i
->thread_info
.attached
= FALSE
;
989 /* Since the caller sleeps in pa_sink_input_unlink(),
990 * we can safely access data outside of thread_info even
991 * though it is mutable */
993 pa_assert(!i
->thread_info
.sync_prev
);
994 pa_assert(!i
->thread_info
.sync_next
);
996 if (i
->thread_info
.sync_prev
) {
997 i
->thread_info
.sync_prev
->thread_info
.sync_next
= i
->thread_info
.sync_prev
->sync_next
;
998 i
->thread_info
.sync_prev
= NULL
;
1001 if (i
->thread_info
.sync_next
) {
1002 i
->thread_info
.sync_next
->thread_info
.sync_prev
= i
->thread_info
.sync_next
->sync_prev
;
1003 i
->thread_info
.sync_next
= NULL
;
1006 if (pa_hashmap_remove(s
->thread_info
.inputs
, PA_UINT32_TO_PTR(i
->index
)))
1007 pa_sink_input_unref(i
);
1009 pa_sink_invalidate_requested_latency(s
);
1011 /* pa_sink_request_rewind(s, 0); */
1016 case PA_SINK_MESSAGE_REMOVE_INPUT_AND_BUFFER
: {
1017 pa_sink_input_move_info
*info
= userdata
;
1020 /* We don't support moving synchronized streams. */
1021 pa_assert(!info
->sink_input
->sync_prev
);
1022 pa_assert(!info
->sink_input
->sync_next
);
1023 pa_assert(!info
->sink_input
->thread_info
.sync_next
);
1024 pa_assert(!info
->sink_input
->thread_info
.sync_prev
);
1026 if (info
->sink_input
->detach
)
1027 info
->sink_input
->detach(info
->sink_input
);
1029 pa_assert(info
->sink_input
->thread_info
.attached
);
1030 info
->sink_input
->thread_info
.attached
= FALSE
;
1031 pa_sink_invalidate_requested_latency(info
->sink_input
->sink
);
1033 if (info
->ghost_sink_input
) {
1034 pa_assert(info
->buffer_bytes
> 0);
1035 pa_assert(info
->buffer
);
1037 volume_is_norm
= pa_cvolume_is_norm(&info
->sink_input
->thread_info
.volume
);
1039 pa_log_debug("Buffering %lu bytes ...", (unsigned long) info
->buffer_bytes
);
1041 while (info
->buffer_bytes
> 0) {
1042 pa_memchunk memchunk
;
1046 if (pa_sink_input_peek(info
->sink_input
, info
->buffer_bytes
, &memchunk
, &volume
) < 0)
1049 n
= memchunk
.length
> info
->buffer_bytes
? info
->buffer_bytes
: memchunk
.length
;
1050 pa_sink_input_drop(info
->sink_input
, n
);
1051 memchunk
.length
= n
;
1053 if (!volume_is_norm
) {
1054 pa_memchunk_make_writable(&memchunk
, 0);
1055 pa_volume_memchunk(&memchunk
, &s
->sample_spec
, &volume
);
1058 if (pa_memblockq_push(info
->buffer
, &memchunk
) < 0) {
1059 pa_memblock_unref(memchunk
.memblock
);
1063 pa_memblock_unref(memchunk
.memblock
);
1064 info
->buffer_bytes
-= n
;
1067 /* Add the remaining already resampled chunks to the buffer */
1068 pa_memblockq_splice(info
->buffer
, info
->sink_input
->thread_info
.render_memblockq
);
1070 pa_memblockq_sink_input_set_queue(info
->ghost_sink_input
, info
->buffer
);
1072 pa_log_debug("Buffered %lu bytes ...", (unsigned long) pa_memblockq_get_length(info
->buffer
));
1075 /* Let's remove the sink input ...*/
1076 if (pa_hashmap_remove(s
->thread_info
.inputs
, PA_UINT32_TO_PTR(info
->sink_input
->index
)))
1077 pa_sink_input_unref(info
->sink_input
);
1079 /* .. and add the ghost sink input instead */
1080 if (info
->ghost_sink_input
) {
1081 pa_hashmap_put(s
->thread_info
.inputs
, PA_UINT32_TO_PTR(info
->ghost_sink_input
->index
), pa_sink_input_ref(info
->ghost_sink_input
));
1082 info
->ghost_sink_input
->thread_info
.sync_prev
= info
->ghost_sink_input
->thread_info
.sync_next
= NULL
;
1084 pa_sink_input_set_max_rewind(info
->ghost_sink_input
, s
->thread_info
.max_rewind
);
1086 pa_assert(!info
->ghost_sink_input
->thread_info
.attached
);
1087 info
->ghost_sink_input
->thread_info
.attached
= TRUE
;
1089 if (info
->ghost_sink_input
->attach
)
1090 info
->ghost_sink_input
->attach(info
->ghost_sink_input
);
1094 pa_sink_invalidate_requested_latency(s
);
1096 pa_sink_request_rewind(s
, 0);
1101 case PA_SINK_MESSAGE_SET_VOLUME
:
1102 s
->thread_info
.soft_volume
= *((pa_cvolume
*) userdata
);
1104 pa_sink_request_rewind(s
, 0);
1107 case PA_SINK_MESSAGE_SET_MUTE
:
1108 s
->thread_info
.soft_muted
= PA_PTR_TO_UINT(userdata
);
1110 pa_sink_request_rewind(s
, 0);
1113 case PA_SINK_MESSAGE_GET_VOLUME
:
1114 *((pa_cvolume
*) userdata
) = s
->thread_info
.soft_volume
;
1117 case PA_SINK_MESSAGE_GET_MUTE
:
1118 *((pa_bool_t
*) userdata
) = s
->thread_info
.soft_muted
;
1121 case PA_SINK_MESSAGE_PING
:
1124 case PA_SINK_MESSAGE_SET_STATE
:
1126 s
->thread_info
.state
= PA_PTR_TO_UINT(userdata
);
1129 case PA_SINK_MESSAGE_DETACH
:
1131 /* We're detaching all our input streams so that the
1132 * asyncmsgq and rtpoll fields can be changed without
1134 pa_sink_detach_within_thread(s
);
1137 case PA_SINK_MESSAGE_ATTACH
:
1139 /* Reattach all streams */
1140 pa_sink_attach_within_thread(s
);
1143 case PA_SINK_MESSAGE_GET_LATENCY
:
1144 case PA_SINK_MESSAGE_MAX
:
1151 int pa_sink_suspend_all(pa_core
*c
, pa_bool_t suspend
) {
1156 pa_core_assert_ref(c
);
1158 for (sink
= PA_SINK(pa_idxset_first(c
->sinks
, &idx
)); sink
; sink
= PA_SINK(pa_idxset_next(c
->sinks
, &idx
)))
1159 ret
-= pa_sink_suspend(sink
, suspend
) < 0;
1164 void pa_sink_detach(pa_sink
*s
) {
1165 pa_sink_assert_ref(s
);
1166 pa_assert(PA_SINK_LINKED(s
->state
));
1168 pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_DETACH
, NULL
, 0, NULL
);
1171 void pa_sink_attach(pa_sink
*s
) {
1172 pa_sink_assert_ref(s
);
1173 pa_assert(PA_SINK_LINKED(s
->state
));
1175 pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_ATTACH
, NULL
, 0, NULL
);
1178 void pa_sink_detach_within_thread(pa_sink
*s
) {
1182 pa_sink_assert_ref(s
);
1183 pa_assert(PA_SINK_LINKED(s
->thread_info
.state
));
1185 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)))
1189 if (s
->monitor_source
)
1190 pa_source_detach_within_thread(s
->monitor_source
);
1193 void pa_sink_attach_within_thread(pa_sink
*s
) {
1197 pa_sink_assert_ref(s
);
1198 pa_assert(PA_SINK_LINKED(s
->thread_info
.state
));
1200 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)))
1204 if (s
->monitor_source
)
1205 pa_source_attach_within_thread(s
->monitor_source
);
1208 void pa_sink_request_rewind(pa_sink
*s
, size_t nbytes
) {
1209 pa_sink_assert_ref(s
);
1210 pa_assert(PA_SINK_LINKED(s
->thread_info
.state
));
1213 nbytes
= s
->thread_info
.max_rewind
;
1215 nbytes
= PA_MIN(nbytes
, s
->thread_info
.max_rewind
);
1217 if (nbytes
<= s
->thread_info
.rewind_nbytes
)
1220 s
->thread_info
.rewind_nbytes
= nbytes
;
1222 if (s
->request_rewind
)
1223 s
->request_rewind(s
);
1226 pa_usec_t
pa_sink_get_requested_latency(pa_sink
*s
) {
1227 pa_usec_t result
= 0;
1231 pa_sink_assert_ref(s
);
1233 if (s
->thread_info
.requested_latency_valid
)
1234 return s
->thread_info
.requested_latency
;
1236 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)))
1238 if (i
->thread_info
.requested_sink_latency
> 0 &&
1239 (!result
|| result
> i
->thread_info
.requested_sink_latency
))
1240 result
= i
->thread_info
.requested_sink_latency
;
1242 s
->thread_info
.requested_latency
= result
;
1243 s
->thread_info
.requested_latency_valid
= TRUE
;
1248 void pa_sink_set_max_rewind(pa_sink
*s
, size_t max_rewind
) {
1252 pa_sink_assert_ref(s
);
1253 pa_assert(PA_SINK_LINKED(s
->thread_info
.state
));
1255 if (max_rewind
== s
->thread_info
.max_rewind
)
1258 s
->thread_info
.max_rewind
= max_rewind
;
1260 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)))
1261 pa_sink_input_set_max_rewind(i
, s
->thread_info
.max_rewind
);
1264 void pa_sink_invalidate_requested_latency(pa_sink
*s
) {
1266 pa_sink_assert_ref(s
);
1267 pa_assert(PA_SINK_LINKED(s
->thread_info
.state
));
1269 if (!s
->thread_info
.requested_latency_valid
)
1272 s
->thread_info
.requested_latency_valid
= FALSE
;
1274 if (s
->update_requested_latency
)
1275 s
->update_requested_latency(s
);