2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
31 #include <pulse/introspect.h>
32 #include <pulse/utf8.h>
33 #include <pulse/xmalloc.h>
34 #include <pulse/timeval.h>
35 #include <pulse/util.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 DEFAULT_MIN_LATENCY (4*PA_USEC_PER_MSEC)
52 static PA_DEFINE_CHECK_TYPE(pa_sink
, pa_msgobject
);
54 static void sink_free(pa_object
*s
);
56 pa_sink_new_data
* pa_sink_new_data_init(pa_sink_new_data
*data
) {
59 memset(data
, 0, sizeof(*data
));
60 data
->proplist
= pa_proplist_new();
65 void pa_sink_new_data_set_name(pa_sink_new_data
*data
, const char *name
) {
69 data
->name
= pa_xstrdup(name
);
72 void pa_sink_new_data_set_sample_spec(pa_sink_new_data
*data
, const pa_sample_spec
*spec
) {
75 if ((data
->sample_spec_is_set
= !!spec
))
76 data
->sample_spec
= *spec
;
79 void pa_sink_new_data_set_channel_map(pa_sink_new_data
*data
, const pa_channel_map
*map
) {
82 if ((data
->channel_map_is_set
= !!map
))
83 data
->channel_map
= *map
;
86 void pa_sink_new_data_set_volume(pa_sink_new_data
*data
, const pa_cvolume
*volume
) {
89 if ((data
->volume_is_set
= !!volume
))
90 data
->volume
= *volume
;
93 void pa_sink_new_data_set_muted(pa_sink_new_data
*data
, pa_bool_t mute
) {
96 data
->muted_is_set
= TRUE
;
100 void pa_sink_new_data_done(pa_sink_new_data
*data
) {
103 pa_xfree(data
->name
);
104 pa_proplist_free(data
->proplist
);
107 /* Called from main context */
108 static void reset_callbacks(pa_sink
*s
) {
112 s
->get_volume
= NULL
;
113 s
->set_volume
= NULL
;
116 s
->request_rewind
= NULL
;
117 s
->update_requested_latency
= NULL
;
120 /* Called from main context */
121 pa_sink
* pa_sink_new(
123 pa_sink_new_data
*data
,
124 pa_sink_flags_t flags
) {
128 char st
[PA_SAMPLE_SPEC_SNPRINT_MAX
], cm
[PA_CHANNEL_MAP_SNPRINT_MAX
];
129 pa_source_new_data source_data
;
134 pa_assert(data
->name
);
136 s
= pa_msgobject_new(pa_sink
);
138 if (!(name
= pa_namereg_register(core
, data
->name
, PA_NAMEREG_SINK
, s
, data
->namereg_fail
))) {
143 pa_sink_new_data_set_name(data
, name
);
145 if (pa_hook_fire(&core
->hooks
[PA_CORE_HOOK_SINK_NEW
], data
) < 0) {
147 pa_namereg_unregister(core
, name
);
151 pa_return_null_if_fail(!data
->driver
|| pa_utf8_valid(data
->driver
));
152 pa_return_null_if_fail(data
->name
&& pa_utf8_valid(data
->name
) && data
->name
[0]);
154 pa_return_null_if_fail(data
->sample_spec_is_set
&& pa_sample_spec_valid(&data
->sample_spec
));
156 if (!data
->channel_map_is_set
)
157 pa_return_null_if_fail(pa_channel_map_init_auto(&data
->channel_map
, data
->sample_spec
.channels
, PA_CHANNEL_MAP_DEFAULT
));
159 pa_return_null_if_fail(pa_channel_map_valid(&data
->channel_map
));
160 pa_return_null_if_fail(data
->channel_map
.channels
== data
->sample_spec
.channels
);
162 if (!data
->volume_is_set
)
163 pa_cvolume_reset(&data
->volume
, data
->sample_spec
.channels
);
165 pa_return_null_if_fail(pa_cvolume_valid(&data
->volume
));
166 pa_return_null_if_fail(data
->volume
.channels
== data
->sample_spec
.channels
);
168 if (!data
->muted_is_set
)
172 pa_proplist_update(data
->proplist
, PA_UPDATE_MERGE
, data
->card
->proplist
);
174 if (pa_hook_fire(&core
->hooks
[PA_CORE_HOOK_SINK_FIXATE
], data
) < 0) {
176 pa_namereg_unregister(core
, name
);
180 s
->parent
.parent
.free
= sink_free
;
181 s
->parent
.process_msg
= pa_sink_process_msg
;
184 s
->state
= PA_SINK_INIT
;
186 s
->name
= pa_xstrdup(name
);
187 s
->proplist
= pa_proplist_copy(data
->proplist
);
188 s
->driver
= pa_xstrdup(pa_path_get_filename(data
->driver
));
189 s
->module
= data
->module
;
190 s
->card
= data
->card
;
192 s
->sample_spec
= data
->sample_spec
;
193 s
->channel_map
= data
->channel_map
;
195 s
->inputs
= pa_idxset_new(NULL
, NULL
);
198 s
->volume
= data
->volume
;
199 s
->base_volume
= PA_VOLUME_NORM
;
200 s
->virtual_volume
= s
->volume
;
202 s
->muted
= data
->muted
;
203 s
->refresh_volume
= s
->refresh_muted
= FALSE
;
211 pa_silence_memchunk_get(
212 &core
->silence_cache
,
218 s
->thread_info
.inputs
= pa_hashmap_new(pa_idxset_trivial_hash_func
, pa_idxset_trivial_compare_func
);
219 pa_cvolume_reset(&s
->thread_info
.soft_volume
, s
->sample_spec
.channels
);
220 s
->thread_info
.soft_muted
= FALSE
;
221 s
->thread_info
.state
= s
->state
;
222 s
->thread_info
.rewind_nbytes
= 0;
223 s
->thread_info
.rewind_requested
= FALSE
;
224 s
->thread_info
.max_rewind
= 0;
225 s
->thread_info
.max_request
= 0;
226 s
->thread_info
.requested_latency_valid
= FALSE
;
227 s
->thread_info
.requested_latency
= 0;
228 s
->thread_info
.min_latency
= DEFAULT_MIN_LATENCY
;
229 s
->thread_info
.max_latency
= 0;
231 pa_assert_se(pa_idxset_put(core
->sinks
, s
, &s
->index
) >= 0);
234 pa_assert_se(pa_idxset_put(s
->card
->sinks
, s
, NULL
) >= 0);
236 pa_log_info("Created sink %u \"%s\" with sample spec %s and channel map %s",
239 pa_sample_spec_snprint(st
, sizeof(st
), &s
->sample_spec
),
240 pa_channel_map_snprint(cm
, sizeof(cm
), &s
->channel_map
));
242 pa_source_new_data_init(&source_data
);
243 pa_source_new_data_set_sample_spec(&source_data
, &s
->sample_spec
);
244 pa_source_new_data_set_channel_map(&source_data
, &s
->channel_map
);
245 source_data
.name
= pa_sprintf_malloc("%s.monitor", name
);
246 source_data
.driver
= data
->driver
;
247 source_data
.module
= data
->module
;
248 source_data
.card
= data
->card
;
250 dn
= pa_proplist_gets(s
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
251 pa_proplist_setf(source_data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
, "Monitor of %s", dn
? dn
: s
->name
);
252 pa_proplist_sets(source_data
.proplist
, PA_PROP_DEVICE_CLASS
, "monitor");
254 s
->monitor_source
= pa_source_new(core
, &source_data
, PA_SOURCE_LATENCY
);
256 pa_source_new_data_done(&source_data
);
258 if (!s
->monitor_source
) {
264 s
->monitor_source
->monitor_of
= s
;
266 pa_source_set_latency_range(s
->monitor_source
, s
->thread_info
.min_latency
, s
->thread_info
.max_latency
);
267 pa_source_set_max_rewind(s
->monitor_source
, s
->thread_info
.max_rewind
);
272 /* Called from main context */
273 static int sink_set_state(pa_sink
*s
, pa_sink_state_t state
) {
275 pa_bool_t suspend_change
;
276 pa_sink_state_t original_state
;
280 if (s
->state
== state
)
283 original_state
= s
->state
;
286 (original_state
== PA_SINK_SUSPENDED
&& PA_SINK_IS_OPENED(state
)) ||
287 (PA_SINK_IS_OPENED(original_state
) && state
== PA_SINK_SUSPENDED
);
290 if ((ret
= s
->set_state(s
, state
)) < 0)
294 if ((ret
= pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_STATE
, PA_UINT_TO_PTR(state
), 0, NULL
)) < 0) {
297 s
->set_state(s
, original_state
);
304 if (suspend_change
) {
308 /* We're suspending or resuming, tell everyone about it */
310 for (i
= PA_SINK_INPUT(pa_idxset_first(s
->inputs
, &idx
)); i
; i
= PA_SINK_INPUT(pa_idxset_next(s
->inputs
, &idx
)))
312 i
->suspend(i
, state
== PA_SINK_SUSPENDED
);
315 if (state
!= PA_SINK_UNLINKED
) { /* if we enter UNLINKED state pa_sink_unlink() will fire the apropriate events */
316 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_STATE_CHANGED
], s
);
317 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
| PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
323 /* Called from main context */
324 void pa_sink_put(pa_sink
* s
) {
325 pa_sink_assert_ref(s
);
327 pa_assert(s
->state
== PA_SINK_INIT
);
329 /* The following fields must be initialized properly when calling _put() */
330 pa_assert(s
->asyncmsgq
);
331 pa_assert(s
->rtpoll
);
332 pa_assert(!s
->thread_info
.min_latency
|| !s
->thread_info
.max_latency
||
333 s
->thread_info
.min_latency
<= s
->thread_info
.max_latency
);
335 if (!(s
->flags
& PA_SINK_HW_VOLUME_CTRL
)) {
336 s
->flags
|= PA_SINK_DECIBEL_VOLUME
;
338 s
->thread_info
.soft_volume
= s
->volume
;
339 s
->thread_info
.soft_muted
= s
->muted
;
342 pa_assert_se(sink_set_state(s
, PA_SINK_IDLE
) == 0);
344 pa_source_put(s
->monitor_source
);
346 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
| PA_SUBSCRIPTION_EVENT_NEW
, s
->index
);
347 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_PUT
], s
);
350 /* Called from main context */
351 void pa_sink_unlink(pa_sink
* s
) {
353 pa_sink_input
*i
, *j
= NULL
;
357 /* Please note that pa_sink_unlink() does more than simply
358 * reversing pa_sink_put(). It also undoes the registrations
359 * already done in pa_sink_new()! */
361 /* All operations here shall be idempotent, i.e. pa_sink_unlink()
362 * may be called multiple times on the same sink without bad
365 linked
= PA_SINK_IS_LINKED(s
->state
);
368 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_UNLINK
], s
);
370 if (s
->state
!= PA_SINK_UNLINKED
)
371 pa_namereg_unregister(s
->core
, s
->name
);
372 pa_idxset_remove_by_data(s
->core
->sinks
, s
, NULL
);
375 pa_idxset_remove_by_data(s
->card
->sinks
, s
, NULL
);
377 while ((i
= pa_idxset_first(s
->inputs
, NULL
))) {
379 pa_sink_input_kill(i
);
384 sink_set_state(s
, PA_SINK_UNLINKED
);
386 s
->state
= PA_SINK_UNLINKED
;
390 if (s
->monitor_source
)
391 pa_source_unlink(s
->monitor_source
);
394 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
| PA_SUBSCRIPTION_EVENT_REMOVE
, s
->index
);
395 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_UNLINK_POST
], s
);
399 /* Called from main context */
400 static void sink_free(pa_object
*o
) {
401 pa_sink
*s
= PA_SINK(o
);
405 pa_assert(pa_sink_refcnt(s
) == 0);
407 if (PA_SINK_IS_LINKED(s
->state
))
410 pa_log_info("Freeing sink %u \"%s\"", s
->index
, s
->name
);
412 if (s
->monitor_source
) {
413 pa_source_unref(s
->monitor_source
);
414 s
->monitor_source
= NULL
;
417 pa_idxset_free(s
->inputs
, NULL
, NULL
);
419 while ((i
= pa_hashmap_steal_first(s
->thread_info
.inputs
)))
420 pa_sink_input_unref(i
);
422 pa_hashmap_free(s
->thread_info
.inputs
, NULL
, NULL
);
424 if (s
->silence
.memblock
)
425 pa_memblock_unref(s
->silence
.memblock
);
431 pa_proplist_free(s
->proplist
);
436 /* Called from main context */
437 void pa_sink_set_asyncmsgq(pa_sink
*s
, pa_asyncmsgq
*q
) {
438 pa_sink_assert_ref(s
);
442 if (s
->monitor_source
)
443 pa_source_set_asyncmsgq(s
->monitor_source
, q
);
446 /* Called from main context */
447 void pa_sink_set_rtpoll(pa_sink
*s
, pa_rtpoll
*p
) {
448 pa_sink_assert_ref(s
);
451 if (s
->monitor_source
)
452 pa_source_set_rtpoll(s
->monitor_source
, p
);
455 /* Called from main context */
456 int pa_sink_update_status(pa_sink
*s
) {
457 pa_sink_assert_ref(s
);
458 pa_assert(PA_SINK_IS_LINKED(s
->state
));
460 if (s
->state
== PA_SINK_SUSPENDED
)
463 return sink_set_state(s
, pa_sink_used_by(s
) ? PA_SINK_RUNNING
: PA_SINK_IDLE
);
466 /* Called from main context */
467 int pa_sink_suspend(pa_sink
*s
, pa_bool_t suspend
) {
468 pa_sink_assert_ref(s
);
469 pa_assert(PA_SINK_IS_LINKED(s
->state
));
472 return sink_set_state(s
, PA_SINK_SUSPENDED
);
474 return sink_set_state(s
, pa_sink_used_by(s
) ? PA_SINK_RUNNING
: PA_SINK_IDLE
);
477 /* Called from IO thread context */
478 void pa_sink_process_rewind(pa_sink
*s
, size_t nbytes
) {
481 pa_sink_assert_ref(s
);
482 pa_assert(PA_SINK_IS_LINKED(s
->thread_info
.state
));
484 s
->thread_info
.rewind_nbytes
= 0;
485 s
->thread_info
.rewind_requested
= FALSE
;
488 pa_log_debug("Processing rewind...");
490 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
))) {
491 pa_sink_input_assert_ref(i
);
492 pa_sink_input_process_rewind(i
, nbytes
);
496 if (s
->monitor_source
&& PA_SOURCE_IS_OPENED(s
->monitor_source
->thread_info
.state
))
497 pa_source_process_rewind(s
->monitor_source
, nbytes
);
500 /* Called from IO thread context */
501 static unsigned fill_mix_info(pa_sink
*s
, size_t *length
, pa_mix_info
*info
, unsigned maxinfo
) {
505 size_t mixlength
= *length
;
507 pa_sink_assert_ref(s
);
510 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)) && maxinfo
> 0) {
511 pa_sink_input_assert_ref(i
);
513 if (pa_sink_input_peek(i
, *length
, &info
->chunk
, &info
->volume
) < 0)
516 if (mixlength
== 0 || info
->chunk
.length
< mixlength
)
517 mixlength
= info
->chunk
.length
;
519 if (pa_memblock_is_silence(info
->chunk
.memblock
)) {
520 pa_memblock_unref(info
->chunk
.memblock
);
524 info
->userdata
= pa_sink_input_ref(i
);
526 pa_assert(info
->chunk
.memblock
);
527 pa_assert(info
->chunk
.length
> 0);
540 /* Called from IO thread context */
541 static void inputs_drop(pa_sink
*s
, pa_mix_info
*info
, unsigned n
, pa_memchunk
*result
) {
545 unsigned n_unreffed
= 0;
547 pa_sink_assert_ref(s
);
549 pa_assert(result
->memblock
);
550 pa_assert(result
->length
> 0);
552 /* We optimize for the case where the order of the inputs has not changed */
554 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
))) {
556 pa_mix_info
* m
= NULL
;
558 pa_sink_input_assert_ref(i
);
560 /* Let's try to find the matching entry info the pa_mix_info array */
561 for (j
= 0; j
< n
; j
++) {
563 if (info
[p
].userdata
== i
) {
574 pa_sink_input_drop(i
, result
->length
);
576 if (s
->monitor_source
&& PA_SOURCE_IS_OPENED(pa_source_get_state(s
->monitor_source
))) {
578 if (pa_hashmap_size(i
->thread_info
.direct_outputs
) > 0) {
583 if (m
&& m
->chunk
.memblock
) {
585 pa_memblock_ref(c
.memblock
);
586 pa_assert(result
->length
<= c
.length
);
587 c
.length
= result
->length
;
589 pa_memchunk_make_writable(&c
, 0);
590 pa_volume_memchunk(&c
, &s
->sample_spec
, &m
->volume
);
593 pa_memblock_ref(c
.memblock
);
594 pa_assert(result
->length
<= c
.length
);
595 c
.length
= result
->length
;
598 while ((o
= pa_hashmap_iterate(i
->thread_info
.direct_outputs
, &ostate
, NULL
))) {
599 pa_source_output_assert_ref(o
);
600 pa_assert(o
->direct_on_input
== i
);
601 pa_source_post_direct(s
->monitor_source
, o
, &c
);
604 pa_memblock_unref(c
.memblock
);
609 if (m
->chunk
.memblock
)
610 pa_memblock_unref(m
->chunk
.memblock
);
611 pa_memchunk_reset(&m
->chunk
);
613 pa_sink_input_unref(m
->userdata
);
620 /* Now drop references to entries that are included in the
621 * pa_mix_info array but don't exist anymore */
623 if (n_unreffed
< n
) {
624 for (; n
> 0; info
++, n
--) {
626 pa_sink_input_unref(info
->userdata
);
627 if (info
->chunk
.memblock
)
628 pa_memblock_unref(info
->chunk
.memblock
);
632 if (s
->monitor_source
&& PA_SOURCE_IS_OPENED(pa_source_get_state(s
->monitor_source
)))
633 pa_source_post(s
->monitor_source
, result
);
636 /* Called from IO thread context */
637 void pa_sink_render(pa_sink
*s
, size_t length
, pa_memchunk
*result
) {
638 pa_mix_info info
[MAX_MIX_CHANNELS
];
640 size_t block_size_max
;
642 pa_sink_assert_ref(s
);
643 pa_assert(PA_SINK_IS_OPENED(s
->thread_info
.state
));
644 pa_assert(pa_frame_aligned(length
, &s
->sample_spec
));
649 pa_assert(!s
->thread_info
.rewind_requested
);
650 pa_assert(s
->thread_info
.rewind_nbytes
== 0);
653 length
= pa_frame_align(MIX_BUFFER_LENGTH
, &s
->sample_spec
);
655 block_size_max
= pa_mempool_block_size_max(s
->core
->mempool
);
656 if (length
> block_size_max
)
657 length
= pa_frame_align(block_size_max
, &s
->sample_spec
);
659 pa_assert(length
> 0);
661 n
= fill_mix_info(s
, &length
, info
, MAX_MIX_CHANNELS
);
665 *result
= s
->silence
;
666 pa_memblock_ref(result
->memblock
);
668 if (result
->length
> length
)
669 result
->length
= length
;
674 *result
= info
[0].chunk
;
675 pa_memblock_ref(result
->memblock
);
677 if (result
->length
> length
)
678 result
->length
= length
;
680 pa_sw_cvolume_multiply(&volume
, &s
->thread_info
.soft_volume
, &info
[0].volume
);
682 if (s
->thread_info
.soft_muted
|| !pa_cvolume_is_norm(&volume
)) {
683 pa_memchunk_make_writable(result
, 0);
684 if (s
->thread_info
.soft_muted
|| pa_cvolume_is_muted(&volume
))
685 pa_silence_memchunk(result
, &s
->sample_spec
);
687 pa_volume_memchunk(result
, &s
->sample_spec
, &volume
);
691 result
->memblock
= pa_memblock_new(s
->core
->mempool
, length
);
693 ptr
= pa_memblock_acquire(result
->memblock
);
694 result
->length
= pa_mix(info
, n
,
697 &s
->thread_info
.soft_volume
,
698 s
->thread_info
.soft_muted
);
699 pa_memblock_release(result
->memblock
);
704 inputs_drop(s
, info
, n
, result
);
709 /* Called from IO thread context */
710 void pa_sink_render_into(pa_sink
*s
, pa_memchunk
*target
) {
711 pa_mix_info info
[MAX_MIX_CHANNELS
];
713 size_t length
, block_size_max
;
715 pa_sink_assert_ref(s
);
716 pa_assert(PA_SINK_IS_OPENED(s
->thread_info
.state
));
718 pa_assert(target
->memblock
);
719 pa_assert(target
->length
> 0);
720 pa_assert(pa_frame_aligned(target
->length
, &s
->sample_spec
));
724 pa_assert(!s
->thread_info
.rewind_requested
);
725 pa_assert(s
->thread_info
.rewind_nbytes
== 0);
727 length
= target
->length
;
728 block_size_max
= pa_mempool_block_size_max(s
->core
->mempool
);
729 if (length
> block_size_max
)
730 length
= pa_frame_align(block_size_max
, &s
->sample_spec
);
732 pa_assert(length
> 0);
734 n
= fill_mix_info(s
, &length
, info
, MAX_MIX_CHANNELS
);
737 if (target
->length
> length
)
738 target
->length
= length
;
740 pa_silence_memchunk(target
, &s
->sample_spec
);
744 if (target
->length
> length
)
745 target
->length
= length
;
747 pa_sw_cvolume_multiply(&volume
, &s
->thread_info
.soft_volume
, &info
[0].volume
);
749 if (s
->thread_info
.soft_muted
|| pa_cvolume_is_muted(&volume
))
750 pa_silence_memchunk(target
, &s
->sample_spec
);
754 vchunk
= info
[0].chunk
;
755 pa_memblock_ref(vchunk
.memblock
);
757 if (vchunk
.length
> length
)
758 vchunk
.length
= length
;
760 if (!pa_cvolume_is_norm(&volume
)) {
761 pa_memchunk_make_writable(&vchunk
, 0);
762 pa_volume_memchunk(&vchunk
, &s
->sample_spec
, &volume
);
765 pa_memchunk_memcpy(target
, &vchunk
);
766 pa_memblock_unref(vchunk
.memblock
);
772 ptr
= pa_memblock_acquire(target
->memblock
);
774 target
->length
= pa_mix(info
, n
,
775 (uint8_t*) ptr
+ target
->index
, length
,
777 &s
->thread_info
.soft_volume
,
778 s
->thread_info
.soft_muted
);
780 pa_memblock_release(target
->memblock
);
783 inputs_drop(s
, info
, n
, target
);
788 /* Called from IO thread context */
789 void pa_sink_render_into_full(pa_sink
*s
, pa_memchunk
*target
) {
793 pa_sink_assert_ref(s
);
794 pa_assert(PA_SINK_IS_OPENED(s
->thread_info
.state
));
796 pa_assert(target
->memblock
);
797 pa_assert(target
->length
> 0);
798 pa_assert(pa_frame_aligned(target
->length
, &s
->sample_spec
));
802 pa_assert(!s
->thread_info
.rewind_requested
);
803 pa_assert(s
->thread_info
.rewind_nbytes
== 0);
812 pa_sink_render_into(s
, &chunk
);
821 /* Called from IO thread context */
822 void pa_sink_render_full(pa_sink
*s
, size_t length
, pa_memchunk
*result
) {
823 pa_sink_assert_ref(s
);
824 pa_assert(PA_SINK_IS_OPENED(s
->thread_info
.state
));
825 pa_assert(length
> 0);
826 pa_assert(pa_frame_aligned(length
, &s
->sample_spec
));
829 pa_assert(!s
->thread_info
.rewind_requested
);
830 pa_assert(s
->thread_info
.rewind_nbytes
== 0);
832 /*** This needs optimization ***/
835 result
->length
= length
;
836 result
->memblock
= pa_memblock_new(s
->core
->mempool
, length
);
838 pa_sink_render_into_full(s
, result
);
841 /* Called from main thread */
842 pa_usec_t
pa_sink_get_latency(pa_sink
*s
) {
845 pa_sink_assert_ref(s
);
846 pa_assert(PA_SINK_IS_LINKED(s
->state
));
848 /* The returned value is supposed to be in the time domain of the sound card! */
850 if (!PA_SINK_IS_OPENED(s
->state
))
853 if (!(s
->flags
& PA_SINK_LATENCY
))
856 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_LATENCY
, &usec
, 0, NULL
) == 0);
861 /* Called from main thread */
862 void pa_sink_set_volume(pa_sink
*s
, const pa_cvolume
*volume
) {
864 pa_sink_set_volume_data data
;
866 pa_sink_assert_ref(s
);
867 pa_assert(PA_SINK_IS_LINKED(s
->state
));
869 pa_assert(pa_cvolume_valid(volume
));
870 pa_assert(pa_cvolume_compatible(volume
, &s
->sample_spec
));
873 data
.virtual_volume
= data
.volume
= *volume
;
875 changed
= !pa_cvolume_equal(&data
.virtual_volume
, &s
->virtual_volume
) ||
876 !pa_cvolume_equal(&data
.volume
, &s
->volume
);
879 if (pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_SET_VOLUME
], &data
) < 0)
882 changed
= !pa_cvolume_equal(&data
.virtual_volume
, &s
->virtual_volume
); /* from client-side view */
885 s
->volume
= data
.volume
;
886 s
->virtual_volume
= data
.virtual_volume
;
888 if (s
->set_volume
&& s
->set_volume(s
) < 0)
889 s
->set_volume
= NULL
;
892 pa_sink_set_soft_volume(s
, &s
->volume
);
895 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
898 /* Called from main thread */
899 void pa_sink_set_soft_volume(pa_sink
*s
, const pa_cvolume
*volume
) {
900 pa_sink_assert_ref(s
);
903 if (PA_SINK_IS_LINKED(s
->state
))
904 pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_VOLUME
, volume
, 0, NULL
);
906 s
->thread_info
.soft_volume
= *volume
;
909 /* Called from main thread */
910 const pa_cvolume
*pa_sink_get_volume(pa_sink
*s
, pa_bool_t force_refresh
) {
911 pa_sink_assert_ref(s
);
912 pa_assert(PA_SINK_IS_LINKED(s
->state
));
914 if (s
->refresh_volume
|| force_refresh
) {
915 struct pa_cvolume old_volume
= s
->virtual_volume
;
917 if (s
->get_volume
&& s
->get_volume(s
) < 0)
918 s
->get_volume
= NULL
;
920 if (!s
->get_volume
) {
921 pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_VOLUME
, &s
->volume
, 0, NULL
);
922 s
->virtual_volume
= s
->volume
;
925 if (!pa_cvolume_equal(&old_volume
, &s
->virtual_volume
))
926 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
929 return &s
->virtual_volume
;
932 /* Called from main thread */
933 void pa_sink_set_mute(pa_sink
*s
, pa_bool_t mute
) {
936 pa_sink_assert_ref(s
);
937 pa_assert(PA_SINK_IS_LINKED(s
->state
));
939 changed
= s
->muted
!= mute
;
942 if (s
->set_mute
&& s
->set_mute(s
) < 0)
946 pa_asyncmsgq_post(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_MUTE
, PA_UINT_TO_PTR(mute
), 0, NULL
, NULL
);
949 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
952 /* Called from main thread */
953 pa_bool_t
pa_sink_get_mute(pa_sink
*s
, pa_bool_t force_refresh
) {
955 pa_sink_assert_ref(s
);
956 pa_assert(PA_SINK_IS_LINKED(s
->state
));
958 if (s
->refresh_muted
|| force_refresh
) {
959 pa_bool_t old_muted
= s
->muted
;
961 if (s
->get_mute
&& s
->get_mute(s
) < 0)
965 pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_MUTE
, &s
->muted
, 0, NULL
);
967 if (old_muted
!= s
->muted
)
968 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
974 /* Called from main thread */
975 pa_bool_t
pa_sink_update_proplist(pa_sink
*s
, pa_update_mode_t mode
, pa_proplist
*p
) {
977 pa_sink_assert_ref(s
);
979 pa_proplist_update(s
->proplist
, mode
, p
);
981 if (PA_SINK_IS_LINKED(s
->state
)) {
982 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_PROPLIST_CHANGED
], s
);
983 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
989 /* Called from main thread */
990 void pa_sink_set_description(pa_sink
*s
, const char *description
) {
992 pa_sink_assert_ref(s
);
994 if (!description
&& !pa_proplist_contains(s
->proplist
, PA_PROP_DEVICE_DESCRIPTION
))
997 old
= pa_proplist_gets(s
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
999 if (old
&& description
&& !strcmp(old
, description
))
1003 pa_proplist_sets(s
->proplist
, PA_PROP_DEVICE_DESCRIPTION
, description
);
1005 pa_proplist_unset(s
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
1007 if (s
->monitor_source
) {
1010 n
= pa_sprintf_malloc("Monitor Source of %s", description
? description
: s
->name
);
1011 pa_source_set_description(s
->monitor_source
, n
);
1015 if (PA_SINK_IS_LINKED(s
->state
)) {
1016 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
1017 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_PROPLIST_CHANGED
], s
);
1021 /* Called from main thread */
1022 unsigned pa_sink_linked_by(pa_sink
*s
) {
1025 pa_sink_assert_ref(s
);
1026 pa_assert(PA_SINK_IS_LINKED(s
->state
));
1028 ret
= pa_idxset_size(s
->inputs
);
1030 /* We add in the number of streams connected to us here. Please
1031 * note the asymmmetry to pa_sink_used_by()! */
1033 if (s
->monitor_source
)
1034 ret
+= pa_source_linked_by(s
->monitor_source
);
1039 /* Called from main thread */
1040 unsigned pa_sink_used_by(pa_sink
*s
) {
1043 pa_sink_assert_ref(s
);
1044 pa_assert(PA_SINK_IS_LINKED(s
->state
));
1046 ret
= pa_idxset_size(s
->inputs
);
1047 pa_assert(ret
>= s
->n_corked
);
1049 /* Streams connected to our monitor source do not matter for
1050 * pa_sink_used_by()!.*/
1052 return ret
- s
->n_corked
;
1055 /* Called from main thread */
1056 unsigned pa_sink_check_suspend(pa_sink
*s
) {
1061 pa_sink_assert_ref(s
);
1063 if (!PA_SINK_IS_LINKED(s
->state
))
1068 for (i
= PA_SINK_INPUT(pa_idxset_first(s
->inputs
, &idx
)); i
; i
= PA_SINK_INPUT(pa_idxset_next(s
->inputs
, &idx
))) {
1069 pa_sink_input_state_t st
;
1071 st
= pa_sink_input_get_state(i
);
1072 pa_assert(PA_SINK_INPUT_IS_LINKED(st
));
1074 if (st
== PA_SINK_INPUT_CORKED
)
1077 if (i
->flags
& PA_SINK_INPUT_DONT_INHIBIT_AUTO_SUSPEND
)
1083 if (s
->monitor_source
)
1084 ret
+= pa_source_check_suspend(s
->monitor_source
);
1089 /* Called from IO thread, except when it is not */
1090 int pa_sink_process_msg(pa_msgobject
*o
, int code
, void *userdata
, int64_t offset
, pa_memchunk
*chunk
) {
1091 pa_sink
*s
= PA_SINK(o
);
1092 pa_sink_assert_ref(s
);
1094 switch ((pa_sink_message_t
) code
) {
1096 case PA_SINK_MESSAGE_ADD_INPUT
: {
1097 pa_sink_input
*i
= PA_SINK_INPUT(userdata
);
1099 /* If you change anything here, make sure to change the
1100 * sink input handling a few lines down at
1101 * PA_SINK_MESSAGE_FINISH_MOVE, too. */
1103 pa_hashmap_put(s
->thread_info
.inputs
, PA_UINT32_TO_PTR(i
->index
), pa_sink_input_ref(i
));
1105 /* Since the caller sleeps in pa_sink_input_put(), we can
1106 * safely access data outside of thread_info even though
1109 if ((i
->thread_info
.sync_prev
= i
->sync_prev
)) {
1110 pa_assert(i
->sink
== i
->thread_info
.sync_prev
->sink
);
1111 pa_assert(i
->sync_prev
->sync_next
== i
);
1112 i
->thread_info
.sync_prev
->thread_info
.sync_next
= i
;
1115 if ((i
->thread_info
.sync_next
= i
->sync_next
)) {
1116 pa_assert(i
->sink
== i
->thread_info
.sync_next
->sink
);
1117 pa_assert(i
->sync_next
->sync_prev
== i
);
1118 i
->thread_info
.sync_next
->thread_info
.sync_prev
= i
;
1121 pa_assert(!i
->thread_info
.attached
);
1122 i
->thread_info
.attached
= TRUE
;
1127 pa_sink_input_set_state_within_thread(i
, i
->state
);
1129 /* The requested latency of the sink input needs to be
1130 * fixed up and then configured on the sink */
1132 if (i
->thread_info
.requested_sink_latency
!= (pa_usec_t
) -1)
1133 pa_sink_input_set_requested_latency_within_thread(i
, i
->thread_info
.requested_sink_latency
);
1135 pa_sink_input_update_max_rewind(i
, s
->thread_info
.max_rewind
);
1136 pa_sink_input_update_max_request(i
, s
->thread_info
.max_request
);
1138 /* We don't rewind here automatically. This is left to the
1139 * sink input implementor because some sink inputs need a
1140 * slow start, i.e. need some time to buffer client
1141 * samples before beginning streaming. */
1146 case PA_SINK_MESSAGE_REMOVE_INPUT
: {
1147 pa_sink_input
*i
= PA_SINK_INPUT(userdata
);
1149 /* If you change anything here, make sure to change the
1150 * sink input handling a few lines down at
1151 * PA_SINK_MESSAGE_PREPAPRE_MOVE, too. */
1156 pa_sink_input_set_state_within_thread(i
, i
->state
);
1158 pa_assert(i
->thread_info
.attached
);
1159 i
->thread_info
.attached
= FALSE
;
1161 /* Since the caller sleeps in pa_sink_input_unlink(),
1162 * we can safely access data outside of thread_info even
1163 * though it is mutable */
1165 pa_assert(!i
->sync_prev
);
1166 pa_assert(!i
->sync_next
);
1168 if (i
->thread_info
.sync_prev
) {
1169 i
->thread_info
.sync_prev
->thread_info
.sync_next
= i
->thread_info
.sync_prev
->sync_next
;
1170 i
->thread_info
.sync_prev
= NULL
;
1173 if (i
->thread_info
.sync_next
) {
1174 i
->thread_info
.sync_next
->thread_info
.sync_prev
= i
->thread_info
.sync_next
->sync_prev
;
1175 i
->thread_info
.sync_next
= NULL
;
1178 if (pa_hashmap_remove(s
->thread_info
.inputs
, PA_UINT32_TO_PTR(i
->index
)))
1179 pa_sink_input_unref(i
);
1181 pa_sink_invalidate_requested_latency(s
);
1182 pa_sink_request_rewind(s
, (size_t) -1);
1187 case PA_SINK_MESSAGE_START_MOVE
: {
1188 pa_sink_input
*i
= PA_SINK_INPUT(userdata
);
1190 /* We don't support moving synchronized streams. */
1191 pa_assert(!i
->sync_prev
);
1192 pa_assert(!i
->sync_next
);
1193 pa_assert(!i
->thread_info
.sync_next
);
1194 pa_assert(!i
->thread_info
.sync_prev
);
1196 if (i
->thread_info
.state
!= PA_SINK_INPUT_CORKED
) {
1198 size_t sink_nbytes
, total_nbytes
;
1200 /* Get the latency of the sink */
1201 if (!(s
->flags
& PA_SINK_LATENCY
) ||
1202 PA_MSGOBJECT(s
)->process_msg(PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_LATENCY
, &usec
, 0, NULL
) < 0)
1205 sink_nbytes
= pa_usec_to_bytes(usec
, &s
->sample_spec
);
1206 total_nbytes
= sink_nbytes
+ pa_memblockq_get_length(i
->thread_info
.render_memblockq
);
1208 if (total_nbytes
> 0) {
1209 i
->thread_info
.rewrite_nbytes
= i
->thread_info
.resampler
? pa_resampler_request(i
->thread_info
.resampler
, total_nbytes
) : total_nbytes
;
1210 i
->thread_info
.rewrite_flush
= TRUE
;
1211 pa_sink_input_process_rewind(i
, sink_nbytes
);
1218 pa_assert(i
->thread_info
.attached
);
1219 i
->thread_info
.attached
= FALSE
;
1221 /* Let's remove the sink input ...*/
1222 if (pa_hashmap_remove(s
->thread_info
.inputs
, PA_UINT32_TO_PTR(i
->index
)))
1223 pa_sink_input_unref(i
);
1225 pa_sink_invalidate_requested_latency(s
);
1227 pa_log_debug("Requesting rewind due to started move");
1228 pa_sink_request_rewind(s
, (size_t) -1);
1233 case PA_SINK_MESSAGE_FINISH_MOVE
: {
1234 pa_sink_input
*i
= PA_SINK_INPUT(userdata
);
1236 /* We don't support moving synchronized streams. */
1237 pa_assert(!i
->sync_prev
);
1238 pa_assert(!i
->sync_next
);
1239 pa_assert(!i
->thread_info
.sync_next
);
1240 pa_assert(!i
->thread_info
.sync_prev
);
1242 pa_hashmap_put(s
->thread_info
.inputs
, PA_UINT32_TO_PTR(i
->index
), pa_sink_input_ref(i
));
1244 pa_assert(!i
->thread_info
.attached
);
1245 i
->thread_info
.attached
= TRUE
;
1250 if (i
->thread_info
.requested_sink_latency
!= (pa_usec_t
) -1)
1251 pa_sink_input_set_requested_latency_within_thread(i
, i
->thread_info
.requested_sink_latency
);
1253 pa_sink_input_update_max_rewind(i
, s
->thread_info
.max_rewind
);
1254 pa_sink_input_update_max_request(i
, s
->thread_info
.max_request
);
1256 if (i
->thread_info
.state
!= PA_SINK_INPUT_CORKED
) {
1260 /* Get the latency of the sink */
1261 if (!(s
->flags
& PA_SINK_LATENCY
) ||
1262 PA_MSGOBJECT(s
)->process_msg(PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_LATENCY
, &usec
, 0, NULL
) < 0)
1265 nbytes
= pa_usec_to_bytes(usec
, &s
->sample_spec
);
1268 pa_sink_input_drop(i
, nbytes
);
1270 pa_log_debug("Requesting rewind due to finished move");
1271 pa_sink_request_rewind(s
, nbytes
);
1277 case PA_SINK_MESSAGE_SET_VOLUME
:
1278 s
->thread_info
.soft_volume
= *((pa_cvolume
*) userdata
);
1280 pa_sink_request_rewind(s
, (size_t) -1);
1283 case PA_SINK_MESSAGE_SET_MUTE
:
1284 s
->thread_info
.soft_muted
= PA_PTR_TO_UINT(userdata
);
1286 pa_sink_request_rewind(s
, (size_t) -1);
1289 case PA_SINK_MESSAGE_GET_VOLUME
:
1290 *((pa_cvolume
*) userdata
) = s
->thread_info
.soft_volume
;
1293 case PA_SINK_MESSAGE_GET_MUTE
:
1294 *((pa_bool_t
*) userdata
) = s
->thread_info
.soft_muted
;
1297 case PA_SINK_MESSAGE_SET_STATE
:
1299 s
->thread_info
.state
= PA_PTR_TO_UINT(userdata
);
1302 case PA_SINK_MESSAGE_DETACH
:
1304 /* Detach all streams */
1305 pa_sink_detach_within_thread(s
);
1308 case PA_SINK_MESSAGE_ATTACH
:
1310 /* Reattach all streams */
1311 pa_sink_attach_within_thread(s
);
1314 case PA_SINK_MESSAGE_GET_REQUESTED_LATENCY
: {
1316 pa_usec_t
*usec
= userdata
;
1317 *usec
= pa_sink_get_requested_latency_within_thread(s
);
1319 if (*usec
== (pa_usec_t
) -1)
1320 *usec
= s
->thread_info
.max_latency
;
1325 case PA_SINK_MESSAGE_SET_LATENCY_RANGE
: {
1326 pa_usec_t
*r
= userdata
;
1328 pa_sink_update_latency_range(s
, r
[0], r
[1]);
1333 case PA_SINK_MESSAGE_GET_LATENCY_RANGE
: {
1334 pa_usec_t
*r
= userdata
;
1336 r
[0] = s
->thread_info
.min_latency
;
1337 r
[1] = s
->thread_info
.max_latency
;
1342 case PA_SINK_MESSAGE_GET_MAX_REWIND
:
1344 *((size_t*) userdata
) = s
->thread_info
.max_rewind
;
1347 case PA_SINK_MESSAGE_GET_MAX_REQUEST
:
1349 *((size_t*) userdata
) = s
->thread_info
.max_request
;
1352 case PA_SINK_MESSAGE_GET_LATENCY
:
1353 case PA_SINK_MESSAGE_MAX
:
1360 /* Called from main thread */
1361 int pa_sink_suspend_all(pa_core
*c
, pa_bool_t suspend
) {
1366 pa_core_assert_ref(c
);
1368 for (sink
= PA_SINK(pa_idxset_first(c
->sinks
, &idx
)); sink
; sink
= PA_SINK(pa_idxset_next(c
->sinks
, &idx
)))
1369 ret
-= pa_sink_suspend(sink
, suspend
) < 0;
1374 /* Called from main thread */
1375 void pa_sink_detach(pa_sink
*s
) {
1376 pa_sink_assert_ref(s
);
1377 pa_assert(PA_SINK_IS_LINKED(s
->state
));
1379 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_DETACH
, NULL
, 0, NULL
) == 0);
1382 /* Called from main thread */
1383 void pa_sink_attach(pa_sink
*s
) {
1384 pa_sink_assert_ref(s
);
1385 pa_assert(PA_SINK_IS_LINKED(s
->state
));
1387 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_ATTACH
, NULL
, 0, NULL
) == 0);
1390 /* Called from IO thread */
1391 void pa_sink_detach_within_thread(pa_sink
*s
) {
1395 pa_sink_assert_ref(s
);
1396 pa_assert(PA_SINK_IS_LINKED(s
->thread_info
.state
));
1398 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)))
1402 if (s
->monitor_source
)
1403 pa_source_detach_within_thread(s
->monitor_source
);
1406 /* Called from IO thread */
1407 void pa_sink_attach_within_thread(pa_sink
*s
) {
1411 pa_sink_assert_ref(s
);
1412 pa_assert(PA_SINK_IS_LINKED(s
->thread_info
.state
));
1414 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)))
1418 if (s
->monitor_source
)
1419 pa_source_attach_within_thread(s
->monitor_source
);
1422 /* Called from IO thread */
1423 void pa_sink_request_rewind(pa_sink
*s
, size_t nbytes
) {
1424 pa_sink_assert_ref(s
);
1425 pa_assert(PA_SINK_IS_LINKED(s
->thread_info
.state
));
1427 if (nbytes
== (size_t) -1)
1428 nbytes
= s
->thread_info
.max_rewind
;
1430 nbytes
= PA_MIN(nbytes
, s
->thread_info
.max_rewind
);
1432 if (s
->thread_info
.rewind_requested
&&
1433 nbytes
<= s
->thread_info
.rewind_nbytes
)
1436 s
->thread_info
.rewind_nbytes
= nbytes
;
1437 s
->thread_info
.rewind_requested
= TRUE
;
1439 if (s
->request_rewind
)
1440 s
->request_rewind(s
);
1443 /* Called from IO thread */
1444 pa_usec_t
pa_sink_get_requested_latency_within_thread(pa_sink
*s
) {
1445 pa_usec_t result
= (pa_usec_t
) -1;
1448 pa_usec_t monitor_latency
;
1450 pa_sink_assert_ref(s
);
1452 if (s
->thread_info
.requested_latency_valid
)
1453 return s
->thread_info
.requested_latency
;
1455 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)))
1457 if (i
->thread_info
.requested_sink_latency
!= (pa_usec_t
) -1 &&
1458 (result
== (pa_usec_t
) -1 || result
> i
->thread_info
.requested_sink_latency
))
1459 result
= i
->thread_info
.requested_sink_latency
;
1461 monitor_latency
= pa_source_get_requested_latency_within_thread(s
->monitor_source
);
1463 if (monitor_latency
!= (pa_usec_t
) -1 &&
1464 (result
== (pa_usec_t
) -1 || result
> monitor_latency
))
1465 result
= monitor_latency
;
1467 if (result
!= (pa_usec_t
) -1) {
1468 if (s
->thread_info
.max_latency
> 0 && result
> s
->thread_info
.max_latency
)
1469 result
= s
->thread_info
.max_latency
;
1471 if (s
->thread_info
.min_latency
> 0 && result
< s
->thread_info
.min_latency
)
1472 result
= s
->thread_info
.min_latency
;
1475 s
->thread_info
.requested_latency
= result
;
1476 s
->thread_info
.requested_latency_valid
= TRUE
;
1481 /* Called from main thread */
1482 pa_usec_t
pa_sink_get_requested_latency(pa_sink
*s
) {
1485 pa_sink_assert_ref(s
);
1486 pa_assert(PA_SINK_IS_LINKED(s
->state
));
1488 if (!PA_SINK_IS_OPENED(s
->state
))
1491 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_REQUESTED_LATENCY
, &usec
, 0, NULL
) == 0);
1495 /* Called from IO thread */
1496 void pa_sink_set_max_rewind(pa_sink
*s
, size_t max_rewind
) {
1500 pa_sink_assert_ref(s
);
1502 if (max_rewind
== s
->thread_info
.max_rewind
)
1505 s
->thread_info
.max_rewind
= max_rewind
;
1507 if (PA_SINK_IS_LINKED(s
->thread_info
.state
)) {
1508 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)))
1509 pa_sink_input_update_max_rewind(i
, s
->thread_info
.max_rewind
);
1512 if (s
->monitor_source
)
1513 pa_source_set_max_rewind(s
->monitor_source
, s
->thread_info
.max_rewind
);
1516 /* Called from IO thread */
1517 void pa_sink_set_max_request(pa_sink
*s
, size_t max_request
) {
1520 pa_sink_assert_ref(s
);
1522 if (max_request
== s
->thread_info
.max_request
)
1525 s
->thread_info
.max_request
= max_request
;
1527 if (PA_SINK_IS_LINKED(s
->thread_info
.state
)) {
1530 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)))
1531 pa_sink_input_update_max_request(i
, s
->thread_info
.max_request
);
1535 /* Called from IO thread */
1536 void pa_sink_invalidate_requested_latency(pa_sink
*s
) {
1540 pa_sink_assert_ref(s
);
1542 s
->thread_info
.requested_latency_valid
= FALSE
;
1544 if (s
->update_requested_latency
)
1545 s
->update_requested_latency(s
);
1547 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)))
1548 if (i
->update_sink_requested_latency
)
1549 i
->update_sink_requested_latency(i
);
1552 /* Called from main thread */
1553 void pa_sink_set_latency_range(pa_sink
*s
, pa_usec_t min_latency
, pa_usec_t max_latency
) {
1554 pa_sink_assert_ref(s
);
1556 /* min_latency == 0: no limit
1557 * min_latency == (size_t) -1: default limit
1558 * min_latency anything else: specified limit
1560 * Similar for max_latency */
1562 if (min_latency
== (pa_usec_t
) -1)
1563 min_latency
= DEFAULT_MIN_LATENCY
;
1565 if (max_latency
== (pa_usec_t
) -1)
1566 max_latency
= min_latency
;
1568 pa_assert(!min_latency
|| !max_latency
||
1569 min_latency
<= max_latency
);
1571 if (PA_SINK_IS_LINKED(s
->state
)) {
1577 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_LATENCY_RANGE
, r
, 0, NULL
) == 0);
1579 s
->thread_info
.min_latency
= min_latency
;
1580 s
->thread_info
.max_latency
= max_latency
;
1582 s
->monitor_source
->thread_info
.min_latency
= min_latency
;
1583 s
->monitor_source
->thread_info
.max_latency
= max_latency
;
1585 s
->thread_info
.requested_latency_valid
= s
->monitor_source
->thread_info
.requested_latency_valid
= FALSE
;
1589 /* Called from main thread */
1590 void pa_sink_get_latency_range(pa_sink
*s
, pa_usec_t
*min_latency
, pa_usec_t
*max_latency
) {
1591 pa_sink_assert_ref(s
);
1592 pa_assert(min_latency
);
1593 pa_assert(max_latency
);
1595 if (PA_SINK_IS_LINKED(s
->state
)) {
1596 pa_usec_t r
[2] = { 0, 0 };
1598 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_LATENCY_RANGE
, r
, 0, NULL
) == 0);
1600 *min_latency
= r
[0];
1601 *max_latency
= r
[1];
1603 *min_latency
= s
->thread_info
.min_latency
;
1604 *max_latency
= s
->thread_info
.max_latency
;
1608 /* Called from IO thread */
1609 void pa_sink_update_latency_range(pa_sink
*s
, pa_usec_t min_latency
, pa_usec_t max_latency
) {
1613 pa_sink_assert_ref(s
);
1615 s
->thread_info
.min_latency
= min_latency
;
1616 s
->thread_info
.max_latency
= max_latency
;
1618 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)))
1619 if (i
->update_sink_latency_range
)
1620 i
->update_sink_latency_range(i
);
1622 pa_sink_invalidate_requested_latency(s
);
1624 pa_source_update_latency_range(s
->monitor_source
, min_latency
, max_latency
);
1627 size_t pa_sink_get_max_rewind(pa_sink
*s
) {
1629 pa_sink_assert_ref(s
);
1631 if (!PA_SINK_IS_LINKED(s
->state
))
1632 return s
->thread_info
.max_rewind
;
1634 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_MAX_REWIND
, &r
, 0, NULL
) == 0);
1639 size_t pa_sink_get_max_request(pa_sink
*s
) {
1641 pa_sink_assert_ref(s
);
1643 if (!PA_SINK_IS_LINKED(s
->state
))
1644 return s
->thread_info
.max_request
;
1646 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_MAX_REQUEST
, &r
, 0, NULL
) == 0);