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>
36 #include <pulsecore/sink-input.h>
37 #include <pulsecore/namereg.h>
38 #include <pulsecore/core-util.h>
39 #include <pulsecore/sample-util.h>
40 #include <pulsecore/core-subscribe.h>
41 #include <pulsecore/log.h>
42 #include <pulsecore/macro.h>
43 #include <pulsecore/play-memblockq.h>
47 #define MAX_MIX_CHANNELS 32
48 #define MIX_BUFFER_LENGTH (PA_PAGE_SIZE)
49 #define DEFAULT_MIN_LATENCY (4*PA_USEC_PER_MSEC)
51 static PA_DEFINE_CHECK_TYPE(pa_sink
, pa_msgobject
);
53 static void sink_free(pa_object
*s
);
55 pa_sink_new_data
* pa_sink_new_data_init(pa_sink_new_data
*data
) {
58 memset(data
, 0, sizeof(*data
));
59 data
->proplist
= pa_proplist_new();
64 void pa_sink_new_data_set_name(pa_sink_new_data
*data
, const char *name
) {
68 data
->name
= pa_xstrdup(name
);
71 void pa_sink_new_data_set_sample_spec(pa_sink_new_data
*data
, const pa_sample_spec
*spec
) {
74 if ((data
->sample_spec_is_set
= !!spec
))
75 data
->sample_spec
= *spec
;
78 void pa_sink_new_data_set_channel_map(pa_sink_new_data
*data
, const pa_channel_map
*map
) {
81 if ((data
->channel_map_is_set
= !!map
))
82 data
->channel_map
= *map
;
85 void pa_sink_new_data_set_volume(pa_sink_new_data
*data
, const pa_cvolume
*volume
) {
88 if ((data
->volume_is_set
= !!volume
))
89 data
->volume
= *volume
;
92 void pa_sink_new_data_set_muted(pa_sink_new_data
*data
, pa_bool_t mute
) {
95 data
->muted_is_set
= TRUE
;
99 void pa_sink_new_data_done(pa_sink_new_data
*data
) {
102 pa_xfree(data
->name
);
103 pa_proplist_free(data
->proplist
);
106 /* Called from main context */
107 static void reset_callbacks(pa_sink
*s
) {
111 s
->get_volume
= NULL
;
112 s
->set_volume
= NULL
;
115 s
->request_rewind
= NULL
;
116 s
->update_requested_latency
= NULL
;
119 /* Called from main context */
120 pa_sink
* pa_sink_new(
122 pa_sink_new_data
*data
,
123 pa_sink_flags_t flags
) {
127 char st
[PA_SAMPLE_SPEC_SNPRINT_MAX
], cm
[PA_CHANNEL_MAP_SNPRINT_MAX
];
128 pa_source_new_data source_data
;
133 pa_assert(data
->name
);
135 s
= pa_msgobject_new(pa_sink
);
137 if (!(name
= pa_namereg_register(core
, data
->name
, PA_NAMEREG_SINK
, s
, data
->namereg_fail
))) {
142 pa_sink_new_data_set_name(data
, name
);
144 if (pa_hook_fire(&core
->hooks
[PA_CORE_HOOK_SINK_NEW
], data
) < 0) {
146 pa_namereg_unregister(core
, name
);
150 pa_return_null_if_fail(!data
->driver
|| pa_utf8_valid(data
->driver
));
151 pa_return_null_if_fail(data
->name
&& pa_utf8_valid(data
->name
) && data
->name
[0]);
153 pa_return_null_if_fail(data
->sample_spec_is_set
&& pa_sample_spec_valid(&data
->sample_spec
));
155 if (!data
->channel_map_is_set
)
156 pa_return_null_if_fail(pa_channel_map_init_auto(&data
->channel_map
, data
->sample_spec
.channels
, PA_CHANNEL_MAP_DEFAULT
));
158 pa_return_null_if_fail(pa_channel_map_valid(&data
->channel_map
));
159 pa_return_null_if_fail(data
->channel_map
.channels
== data
->sample_spec
.channels
);
161 if (!data
->volume_is_set
)
162 pa_cvolume_reset(&data
->volume
, data
->sample_spec
.channels
);
164 pa_return_null_if_fail(pa_cvolume_valid(&data
->volume
));
165 pa_return_null_if_fail(data
->volume
.channels
== data
->sample_spec
.channels
);
167 if (!data
->muted_is_set
)
170 if (pa_hook_fire(&core
->hooks
[PA_CORE_HOOK_SINK_FIXATE
], data
) < 0) {
172 pa_namereg_unregister(core
, name
);
176 s
->parent
.parent
.free
= sink_free
;
177 s
->parent
.process_msg
= pa_sink_process_msg
;
180 s
->state
= PA_SINK_INIT
;
182 s
->name
= pa_xstrdup(name
);
183 s
->proplist
= pa_proplist_copy(data
->proplist
);
184 s
->driver
= pa_xstrdup(data
->driver
);
185 s
->module
= data
->module
;
187 s
->sample_spec
= data
->sample_spec
;
188 s
->channel_map
= data
->channel_map
;
190 s
->inputs
= pa_idxset_new(NULL
, NULL
);
193 s
->volume
= data
->volume
;
194 s
->base_volume
= PA_VOLUME_NORM
;
195 s
->muted
= data
->muted
;
196 s
->refresh_volume
= s
->refresh_muted
= FALSE
;
204 pa_silence_memchunk_get(
205 &core
->silence_cache
,
211 s
->thread_info
.inputs
= pa_hashmap_new(pa_idxset_trivial_hash_func
, pa_idxset_trivial_compare_func
);
212 pa_cvolume_reset(&s
->thread_info
.soft_volume
, s
->sample_spec
.channels
);
213 s
->thread_info
.soft_muted
= FALSE
;
214 s
->thread_info
.state
= s
->state
;
215 s
->thread_info
.rewind_nbytes
= 0;
216 s
->thread_info
.rewind_requested
= FALSE
;
217 s
->thread_info
.max_rewind
= 0;
218 s
->thread_info
.max_request
= 0;
219 s
->thread_info
.requested_latency_valid
= FALSE
;
220 s
->thread_info
.requested_latency
= 0;
221 s
->thread_info
.min_latency
= DEFAULT_MIN_LATENCY
;
222 s
->thread_info
.max_latency
= 0;
224 pa_assert_se(pa_idxset_put(core
->sinks
, s
, &s
->index
) >= 0);
226 pa_log_info("Created sink %u \"%s\" with sample spec %s and channel map %s",
229 pa_sample_spec_snprint(st
, sizeof(st
), &s
->sample_spec
),
230 pa_channel_map_snprint(cm
, sizeof(cm
), &s
->channel_map
));
232 pa_source_new_data_init(&source_data
);
233 pa_source_new_data_set_sample_spec(&source_data
, &s
->sample_spec
);
234 pa_source_new_data_set_channel_map(&source_data
, &s
->channel_map
);
235 source_data
.name
= pa_sprintf_malloc("%s.monitor", name
);
236 source_data
.driver
= data
->driver
;
237 source_data
.module
= data
->module
;
239 dn
= pa_proplist_gets(s
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
240 pa_proplist_setf(source_data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
, "Monitor of %s", dn
? dn
: s
->name
);
241 pa_proplist_sets(source_data
.proplist
, PA_PROP_DEVICE_CLASS
, "monitor");
243 s
->monitor_source
= pa_source_new(core
, &source_data
, 0);
245 pa_source_new_data_done(&source_data
);
247 if (!s
->monitor_source
) {
253 s
->monitor_source
->monitor_of
= s
;
255 pa_source_set_latency_range(s
->monitor_source
, s
->thread_info
.min_latency
, s
->thread_info
.max_latency
);
256 pa_source_set_max_rewind(s
->monitor_source
, s
->thread_info
.max_rewind
);
261 /* Called from main context */
262 static int sink_set_state(pa_sink
*s
, pa_sink_state_t state
) {
264 pa_bool_t suspend_change
;
265 pa_sink_state_t original_state
;
269 if (s
->state
== state
)
272 original_state
= s
->state
;
275 (original_state
== PA_SINK_SUSPENDED
&& PA_SINK_IS_OPENED(state
)) ||
276 (PA_SINK_IS_OPENED(original_state
) && state
== PA_SINK_SUSPENDED
);
279 if ((ret
= s
->set_state(s
, state
)) < 0)
283 if ((ret
= pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_STATE
, PA_UINT_TO_PTR(state
), 0, NULL
)) < 0) {
286 s
->set_state(s
, original_state
);
293 if (suspend_change
) {
297 /* We're suspending or resuming, tell everyone about it */
299 for (i
= PA_SINK_INPUT(pa_idxset_first(s
->inputs
, &idx
)); i
; i
= PA_SINK_INPUT(pa_idxset_next(s
->inputs
, &idx
)))
301 i
->suspend(i
, state
== PA_SINK_SUSPENDED
);
304 if (state
!= PA_SINK_UNLINKED
) /* if we enter UNLINKED state pa_sink_unlink() will fire the apropriate events */
305 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_STATE_CHANGED
], s
);
310 /* Called from main context */
311 void pa_sink_put(pa_sink
* s
) {
312 pa_sink_assert_ref(s
);
314 pa_assert(s
->state
== PA_SINK_INIT
);
316 /* The following fields must be initialized properly when calling _put() */
317 pa_assert(s
->asyncmsgq
);
318 pa_assert(s
->rtpoll
);
319 pa_assert(!s
->thread_info
.min_latency
|| !s
->thread_info
.max_latency
||
320 s
->thread_info
.min_latency
<= s
->thread_info
.max_latency
);
322 if (!(s
->flags
& PA_SINK_HW_VOLUME_CTRL
)) {
323 s
->flags
|= PA_SINK_DECIBEL_VOLUME
;
325 s
->thread_info
.soft_volume
= s
->volume
;
326 s
->thread_info
.soft_muted
= s
->muted
;
329 pa_assert_se(sink_set_state(s
, PA_SINK_IDLE
) == 0);
331 pa_source_put(s
->monitor_source
);
333 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
| PA_SUBSCRIPTION_EVENT_NEW
, s
->index
);
334 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_PUT
], s
);
337 /* Called from main context */
338 void pa_sink_unlink(pa_sink
* s
) {
340 pa_sink_input
*i
, *j
= NULL
;
344 /* Please note that pa_sink_unlink() does more than simply
345 * reversing pa_sink_put(). It also undoes the registrations
346 * already done in pa_sink_new()! */
348 /* All operations here shall be idempotent, i.e. pa_sink_unlink()
349 * may be called multiple times on the same sink without bad
352 linked
= PA_SINK_IS_LINKED(s
->state
);
355 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_UNLINK
], s
);
357 if (s
->state
!= PA_SINK_UNLINKED
)
358 pa_namereg_unregister(s
->core
, s
->name
);
359 pa_idxset_remove_by_data(s
->core
->sinks
, s
, NULL
);
361 while ((i
= pa_idxset_first(s
->inputs
, NULL
))) {
363 pa_sink_input_kill(i
);
368 sink_set_state(s
, PA_SINK_UNLINKED
);
370 s
->state
= PA_SINK_UNLINKED
;
374 if (s
->monitor_source
)
375 pa_source_unlink(s
->monitor_source
);
378 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
| PA_SUBSCRIPTION_EVENT_REMOVE
, s
->index
);
379 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_UNLINK_POST
], s
);
383 /* Called from main context */
384 static void sink_free(pa_object
*o
) {
385 pa_sink
*s
= PA_SINK(o
);
389 pa_assert(pa_sink_refcnt(s
) == 0);
391 if (PA_SINK_IS_LINKED(s
->state
))
394 pa_log_info("Freeing sink %u \"%s\"", s
->index
, s
->name
);
396 if (s
->monitor_source
) {
397 pa_source_unref(s
->monitor_source
);
398 s
->monitor_source
= NULL
;
401 pa_idxset_free(s
->inputs
, NULL
, NULL
);
403 while ((i
= pa_hashmap_steal_first(s
->thread_info
.inputs
)))
404 pa_sink_input_unref(i
);
406 pa_hashmap_free(s
->thread_info
.inputs
, NULL
, NULL
);
408 if (s
->silence
.memblock
)
409 pa_memblock_unref(s
->silence
.memblock
);
415 pa_proplist_free(s
->proplist
);
420 /* Called from main context */
421 void pa_sink_set_asyncmsgq(pa_sink
*s
, pa_asyncmsgq
*q
) {
422 pa_sink_assert_ref(s
);
426 if (s
->monitor_source
)
427 pa_source_set_asyncmsgq(s
->monitor_source
, q
);
430 /* Called from main context */
431 void pa_sink_set_rtpoll(pa_sink
*s
, pa_rtpoll
*p
) {
432 pa_sink_assert_ref(s
);
435 if (s
->monitor_source
)
436 pa_source_set_rtpoll(s
->monitor_source
, p
);
439 /* Called from main context */
440 int pa_sink_update_status(pa_sink
*s
) {
441 pa_sink_assert_ref(s
);
442 pa_assert(PA_SINK_IS_LINKED(s
->state
));
444 if (s
->state
== PA_SINK_SUSPENDED
)
447 return sink_set_state(s
, pa_sink_used_by(s
) ? PA_SINK_RUNNING
: PA_SINK_IDLE
);
450 /* Called from main context */
451 int pa_sink_suspend(pa_sink
*s
, pa_bool_t suspend
) {
452 pa_sink_assert_ref(s
);
453 pa_assert(PA_SINK_IS_LINKED(s
->state
));
456 return sink_set_state(s
, PA_SINK_SUSPENDED
);
458 return sink_set_state(s
, pa_sink_used_by(s
) ? PA_SINK_RUNNING
: PA_SINK_IDLE
);
461 /* Called from IO thread context */
462 void pa_sink_process_rewind(pa_sink
*s
, size_t nbytes
) {
465 pa_sink_assert_ref(s
);
466 pa_assert(PA_SINK_IS_LINKED(s
->thread_info
.state
));
468 s
->thread_info
.rewind_nbytes
= 0;
469 s
->thread_info
.rewind_requested
= FALSE
;
472 pa_log_debug("Processing rewind...");
474 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
))) {
475 pa_sink_input_assert_ref(i
);
476 pa_sink_input_process_rewind(i
, nbytes
);
480 if (s
->monitor_source
&& PA_SOURCE_IS_OPENED(s
->monitor_source
->thread_info
.state
))
481 pa_source_process_rewind(s
->monitor_source
, nbytes
);
484 /* Called from IO thread context */
485 static unsigned fill_mix_info(pa_sink
*s
, size_t *length
, pa_mix_info
*info
, unsigned maxinfo
) {
489 size_t mixlength
= *length
;
491 pa_sink_assert_ref(s
);
494 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)) && maxinfo
> 0) {
495 pa_sink_input_assert_ref(i
);
497 if (pa_sink_input_peek(i
, *length
, &info
->chunk
, &info
->volume
) < 0)
500 if (mixlength
== 0 || info
->chunk
.length
< mixlength
)
501 mixlength
= info
->chunk
.length
;
503 if (pa_memblock_is_silence(info
->chunk
.memblock
)) {
504 pa_memblock_unref(info
->chunk
.memblock
);
508 info
->userdata
= pa_sink_input_ref(i
);
510 pa_assert(info
->chunk
.memblock
);
511 pa_assert(info
->chunk
.length
> 0);
524 /* Called from IO thread context */
525 static void inputs_drop(pa_sink
*s
, pa_mix_info
*info
, unsigned n
, pa_memchunk
*result
) {
529 unsigned n_unreffed
= 0;
531 pa_sink_assert_ref(s
);
533 pa_assert(result
->memblock
);
534 pa_assert(result
->length
> 0);
536 /* We optimize for the case where the order of the inputs has not changed */
538 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
))) {
540 pa_mix_info
* m
= NULL
;
542 pa_sink_input_assert_ref(i
);
544 /* Let's try to find the matching entry info the pa_mix_info array */
545 for (j
= 0; j
< n
; j
++) {
547 if (info
[p
].userdata
== i
) {
558 pa_sink_input_drop(i
, result
->length
);
560 if (s
->monitor_source
&& PA_SOURCE_IS_OPENED(pa_source_get_state(s
->monitor_source
))) {
562 if (pa_hashmap_size(i
->thread_info
.direct_outputs
) > 0) {
567 if (m
&& m
->chunk
.memblock
) {
569 pa_memblock_ref(c
.memblock
);
570 pa_assert(result
->length
<= c
.length
);
571 c
.length
= result
->length
;
573 pa_memchunk_make_writable(&c
, 0);
574 pa_volume_memchunk(&c
, &s
->sample_spec
, &m
->volume
);
577 pa_memblock_ref(c
.memblock
);
578 pa_assert(result
->length
<= c
.length
);
579 c
.length
= result
->length
;
582 while ((o
= pa_hashmap_iterate(i
->thread_info
.direct_outputs
, &ostate
, NULL
))) {
583 pa_source_output_assert_ref(o
);
584 pa_assert(o
->direct_on_input
== i
);
585 pa_source_post_direct(s
->monitor_source
, o
, &c
);
588 pa_memblock_unref(c
.memblock
);
593 if (m
->chunk
.memblock
)
594 pa_memblock_unref(m
->chunk
.memblock
);
595 pa_memchunk_reset(&m
->chunk
);
597 pa_sink_input_unref(m
->userdata
);
604 /* Now drop references to entries that are included in the
605 * pa_mix_info array but don't exist anymore */
607 if (n_unreffed
< n
) {
608 for (; n
> 0; info
++, n
--) {
610 pa_sink_input_unref(info
->userdata
);
611 if (info
->chunk
.memblock
)
612 pa_memblock_unref(info
->chunk
.memblock
);
616 if (s
->monitor_source
&& PA_SOURCE_IS_OPENED(pa_source_get_state(s
->monitor_source
)))
617 pa_source_post(s
->monitor_source
, result
);
620 /* Called from IO thread context */
621 void pa_sink_render(pa_sink
*s
, size_t length
, pa_memchunk
*result
) {
622 pa_mix_info info
[MAX_MIX_CHANNELS
];
624 size_t block_size_max
;
626 pa_sink_assert_ref(s
);
627 pa_assert(PA_SINK_IS_OPENED(s
->thread_info
.state
));
628 pa_assert(pa_frame_aligned(length
, &s
->sample_spec
));
633 pa_assert(!s
->thread_info
.rewind_requested
);
634 pa_assert(s
->thread_info
.rewind_nbytes
== 0);
637 length
= pa_frame_align(MIX_BUFFER_LENGTH
, &s
->sample_spec
);
639 block_size_max
= pa_mempool_block_size_max(s
->core
->mempool
);
640 if (length
> block_size_max
)
641 length
= pa_frame_align(block_size_max
, &s
->sample_spec
);
643 pa_assert(length
> 0);
645 n
= s
->thread_info
.state
== PA_SINK_RUNNING
? fill_mix_info(s
, &length
, info
, MAX_MIX_CHANNELS
) : 0;
649 *result
= s
->silence
;
650 pa_memblock_ref(result
->memblock
);
652 if (result
->length
> length
)
653 result
->length
= length
;
658 *result
= info
[0].chunk
;
659 pa_memblock_ref(result
->memblock
);
661 if (result
->length
> length
)
662 result
->length
= length
;
664 pa_sw_cvolume_multiply(&volume
, &s
->thread_info
.soft_volume
, &info
[0].volume
);
666 if (s
->thread_info
.soft_muted
|| !pa_cvolume_is_norm(&volume
)) {
667 pa_memchunk_make_writable(result
, 0);
668 if (s
->thread_info
.soft_muted
|| pa_cvolume_is_muted(&volume
))
669 pa_silence_memchunk(result
, &s
->sample_spec
);
671 pa_volume_memchunk(result
, &s
->sample_spec
, &volume
);
675 result
->memblock
= pa_memblock_new(s
->core
->mempool
, length
);
677 ptr
= pa_memblock_acquire(result
->memblock
);
678 result
->length
= pa_mix(info
, n
,
681 &s
->thread_info
.soft_volume
,
682 s
->thread_info
.soft_muted
);
683 pa_memblock_release(result
->memblock
);
688 if (s
->thread_info
.state
== PA_SINK_RUNNING
)
689 inputs_drop(s
, info
, n
, result
);
694 /* Called from IO thread context */
695 void pa_sink_render_into(pa_sink
*s
, pa_memchunk
*target
) {
696 pa_mix_info info
[MAX_MIX_CHANNELS
];
698 size_t length
, block_size_max
;
700 pa_sink_assert_ref(s
);
701 pa_assert(PA_SINK_IS_OPENED(s
->thread_info
.state
));
703 pa_assert(target
->memblock
);
704 pa_assert(target
->length
> 0);
705 pa_assert(pa_frame_aligned(target
->length
, &s
->sample_spec
));
709 pa_assert(!s
->thread_info
.rewind_requested
);
710 pa_assert(s
->thread_info
.rewind_nbytes
== 0);
712 length
= target
->length
;
713 block_size_max
= pa_mempool_block_size_max(s
->core
->mempool
);
714 if (length
> block_size_max
)
715 length
= pa_frame_align(block_size_max
, &s
->sample_spec
);
717 pa_assert(length
> 0);
719 n
= s
->thread_info
.state
== PA_SINK_RUNNING
? fill_mix_info(s
, &length
, info
, MAX_MIX_CHANNELS
) : 0;
722 if (target
->length
> length
)
723 target
->length
= length
;
725 pa_silence_memchunk(target
, &s
->sample_spec
);
729 if (target
->length
> length
)
730 target
->length
= length
;
732 pa_sw_cvolume_multiply(&volume
, &s
->thread_info
.soft_volume
, &info
[0].volume
);
734 if (s
->thread_info
.soft_muted
|| pa_cvolume_is_muted(&volume
))
735 pa_silence_memchunk(target
, &s
->sample_spec
);
739 vchunk
= info
[0].chunk
;
740 pa_memblock_ref(vchunk
.memblock
);
742 if (vchunk
.length
> length
)
743 vchunk
.length
= length
;
745 if (!pa_cvolume_is_norm(&volume
)) {
746 pa_memchunk_make_writable(&vchunk
, 0);
747 pa_volume_memchunk(&vchunk
, &s
->sample_spec
, &volume
);
750 pa_memchunk_memcpy(target
, &vchunk
);
751 pa_memblock_unref(vchunk
.memblock
);
757 ptr
= pa_memblock_acquire(target
->memblock
);
759 target
->length
= pa_mix(info
, n
,
760 (uint8_t*) ptr
+ target
->index
, length
,
762 &s
->thread_info
.soft_volume
,
763 s
->thread_info
.soft_muted
);
765 pa_memblock_release(target
->memblock
);
768 if (s
->thread_info
.state
== PA_SINK_RUNNING
)
769 inputs_drop(s
, info
, n
, target
);
774 /* Called from IO thread context */
775 void pa_sink_render_into_full(pa_sink
*s
, pa_memchunk
*target
) {
779 pa_sink_assert_ref(s
);
780 pa_assert(PA_SINK_IS_OPENED(s
->thread_info
.state
));
782 pa_assert(target
->memblock
);
783 pa_assert(target
->length
> 0);
784 pa_assert(pa_frame_aligned(target
->length
, &s
->sample_spec
));
788 pa_assert(!s
->thread_info
.rewind_requested
);
789 pa_assert(s
->thread_info
.rewind_nbytes
== 0);
798 pa_sink_render_into(s
, &chunk
);
807 /* Called from IO thread context */
808 void pa_sink_render_full(pa_sink
*s
, size_t length
, pa_memchunk
*result
) {
809 pa_sink_assert_ref(s
);
810 pa_assert(PA_SINK_IS_OPENED(s
->thread_info
.state
));
811 pa_assert(length
> 0);
812 pa_assert(pa_frame_aligned(length
, &s
->sample_spec
));
815 pa_assert(!s
->thread_info
.rewind_requested
);
816 pa_assert(s
->thread_info
.rewind_nbytes
== 0);
818 /*** This needs optimization ***/
821 result
->length
= length
;
822 result
->memblock
= pa_memblock_new(s
->core
->mempool
, length
);
824 pa_sink_render_into_full(s
, result
);
827 /* Called from main thread */
828 pa_usec_t
pa_sink_get_latency(pa_sink
*s
) {
831 pa_sink_assert_ref(s
);
832 pa_assert(PA_SINK_IS_LINKED(s
->state
));
834 /* The returned value is supposed to be in the time domain of the sound card! */
836 if (!PA_SINK_IS_OPENED(s
->state
))
839 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_LATENCY
, &usec
, 0, NULL
) == 0);
844 /* Called from main thread */
845 void pa_sink_set_volume(pa_sink
*s
, const pa_cvolume
*volume
) {
847 pa_sink_set_volume_data data
;
849 pa_sink_assert_ref(s
);
850 pa_assert(PA_SINK_IS_LINKED(s
->state
));
852 pa_assert(pa_cvolume_valid(volume
));
853 pa_assert(pa_cvolume_compatible(volume
, &s
->sample_spec
));
856 data
.volume
= *volume
;
858 changed
= !pa_cvolume_equal(&data
.volume
, &s
->volume
);
861 if (pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_SET_VOLUME
], &data
) < 0)
864 changed
= !pa_cvolume_equal(&data
.volume
, &s
->volume
);
867 s
->volume
= data
.volume
;
869 if (s
->set_volume
&& s
->set_volume(s
) < 0)
870 s
->set_volume
= NULL
;
873 pa_sink_set_soft_volume(s
, volume
);
876 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
879 /* Called from main thread */
880 void pa_sink_set_soft_volume(pa_sink
*s
, const pa_cvolume
*volume
) {
881 pa_sink_assert_ref(s
);
884 if (PA_SINK_IS_LINKED(s
->state
))
885 pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_VOLUME
, volume
, 0, NULL
);
887 s
->thread_info
.soft_volume
= *volume
;
890 /* Called from main thread */
891 const pa_cvolume
*pa_sink_get_volume(pa_sink
*s
, pa_bool_t force_refresh
) {
892 pa_sink_assert_ref(s
);
893 pa_assert(PA_SINK_IS_LINKED(s
->state
));
895 if (s
->refresh_volume
|| force_refresh
) {
896 struct pa_cvolume old_volume
= s
->volume
;
898 if (s
->get_volume
&& s
->get_volume(s
) < 0)
899 s
->get_volume
= NULL
;
902 pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_VOLUME
, &s
->volume
, 0, NULL
);
904 if (!pa_cvolume_equal(&old_volume
, &s
->volume
))
905 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
911 /* Called from main thread */
912 void pa_sink_set_mute(pa_sink
*s
, pa_bool_t mute
) {
915 pa_sink_assert_ref(s
);
916 pa_assert(PA_SINK_IS_LINKED(s
->state
));
918 changed
= s
->muted
!= mute
;
921 if (s
->set_mute
&& s
->set_mute(s
) < 0)
925 pa_asyncmsgq_post(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_MUTE
, PA_UINT_TO_PTR(mute
), 0, NULL
, NULL
);
928 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
931 /* Called from main thread */
932 pa_bool_t
pa_sink_get_mute(pa_sink
*s
, pa_bool_t force_refresh
) {
934 pa_sink_assert_ref(s
);
935 pa_assert(PA_SINK_IS_LINKED(s
->state
));
937 if (s
->refresh_muted
|| force_refresh
) {
938 pa_bool_t old_muted
= s
->muted
;
940 if (s
->get_mute
&& s
->get_mute(s
) < 0)
944 pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_MUTE
, &s
->muted
, 0, NULL
);
946 if (old_muted
!= s
->muted
)
947 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
953 /* Called from main thread */
954 void pa_sink_set_description(pa_sink
*s
, const char *description
) {
956 pa_sink_assert_ref(s
);
958 if (!description
&& !pa_proplist_contains(s
->proplist
, PA_PROP_DEVICE_DESCRIPTION
))
961 old
= pa_proplist_gets(s
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
963 if (old
&& description
&& !strcmp(old
, description
))
967 pa_proplist_sets(s
->proplist
, PA_PROP_DEVICE_DESCRIPTION
, description
);
969 pa_proplist_unset(s
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
971 if (s
->monitor_source
) {
974 n
= pa_sprintf_malloc("Monitor Source of %s", description
? description
: s
->name
);
975 pa_source_set_description(s
->monitor_source
, n
);
979 if (PA_SINK_IS_LINKED(s
->state
)) {
980 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
981 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_PROPLIST_CHANGED
], s
);
985 /* Called from main thread */
986 unsigned pa_sink_linked_by(pa_sink
*s
) {
989 pa_sink_assert_ref(s
);
990 pa_assert(PA_SINK_IS_LINKED(s
->state
));
992 ret
= pa_idxset_size(s
->inputs
);
994 /* We add in the number of streams connected to us here. Please
995 * note the asymmmetry to pa_sink_used_by()! */
997 if (s
->monitor_source
)
998 ret
+= pa_source_linked_by(s
->monitor_source
);
1003 /* Called from main thread */
1004 unsigned pa_sink_used_by(pa_sink
*s
) {
1007 pa_sink_assert_ref(s
);
1008 pa_assert(PA_SINK_IS_LINKED(s
->state
));
1010 ret
= pa_idxset_size(s
->inputs
);
1011 pa_assert(ret
>= s
->n_corked
);
1013 /* Streams connected to our monitor source do not matter for
1014 * pa_sink_used_by()!.*/
1016 return ret
- s
->n_corked
;
1019 /* Called from main thread */
1020 unsigned pa_sink_check_suspend(pa_sink
*s
) {
1025 pa_sink_assert_ref(s
);
1027 if (!PA_SINK_IS_LINKED(s
->state
))
1032 for (i
= PA_SINK_INPUT(pa_idxset_first(s
->inputs
, &idx
)); i
; i
= PA_SINK_INPUT(pa_idxset_next(s
->inputs
, &idx
))) {
1033 pa_sink_input_state_t st
;
1035 st
= pa_sink_input_get_state(i
);
1036 pa_assert(PA_SINK_INPUT_IS_LINKED(st
));
1038 if (st
== PA_SINK_INPUT_CORKED
)
1041 if (i
->flags
& PA_SINK_INPUT_DONT_INHIBIT_AUTO_SUSPEND
)
1047 if (s
->monitor_source
)
1048 ret
+= pa_source_check_suspend(s
->monitor_source
);
1053 /* Called from IO thread, except when it is not */
1054 int pa_sink_process_msg(pa_msgobject
*o
, int code
, void *userdata
, int64_t offset
, pa_memchunk
*chunk
) {
1055 pa_sink
*s
= PA_SINK(o
);
1056 pa_sink_assert_ref(s
);
1058 switch ((pa_sink_message_t
) code
) {
1060 case PA_SINK_MESSAGE_ADD_INPUT
: {
1061 pa_sink_input
*i
= PA_SINK_INPUT(userdata
);
1063 /* If you change anything here, make sure to change the
1064 * sink input handling a few lines down at
1065 * PA_SINK_MESSAGE_FINISH_MOVE, too. */
1067 pa_hashmap_put(s
->thread_info
.inputs
, PA_UINT32_TO_PTR(i
->index
), pa_sink_input_ref(i
));
1069 /* Since the caller sleeps in pa_sink_input_put(), we can
1070 * safely access data outside of thread_info even though
1073 if ((i
->thread_info
.sync_prev
= i
->sync_prev
)) {
1074 pa_assert(i
->sink
== i
->thread_info
.sync_prev
->sink
);
1075 pa_assert(i
->sync_prev
->sync_next
== i
);
1076 i
->thread_info
.sync_prev
->thread_info
.sync_next
= i
;
1079 if ((i
->thread_info
.sync_next
= i
->sync_next
)) {
1080 pa_assert(i
->sink
== i
->thread_info
.sync_next
->sink
);
1081 pa_assert(i
->sync_next
->sync_prev
== i
);
1082 i
->thread_info
.sync_next
->thread_info
.sync_prev
= i
;
1085 pa_assert(!i
->thread_info
.attached
);
1086 i
->thread_info
.attached
= TRUE
;
1091 pa_sink_input_set_state_within_thread(i
, i
->state
);
1093 /* The requested latency of the sink input needs to be
1094 * fixed up and then configured on the sink */
1096 if (i
->thread_info
.requested_sink_latency
!= (pa_usec_t
) -1)
1097 pa_sink_input_set_requested_latency_within_thread(i
, i
->thread_info
.requested_sink_latency
);
1099 pa_sink_input_update_max_rewind(i
, s
->thread_info
.max_rewind
);
1100 pa_sink_input_update_max_request(i
, s
->thread_info
.max_request
);
1102 /* We don't rewind here automatically. This is left to the
1103 * sink input implementor because some sink inputs need a
1104 * slow start, i.e. need some time to buffer client
1105 * samples before beginning streaming. */
1110 case PA_SINK_MESSAGE_REMOVE_INPUT
: {
1111 pa_sink_input
*i
= PA_SINK_INPUT(userdata
);
1113 /* If you change anything here, make sure to change the
1114 * sink input handling a few lines down at
1115 * PA_SINK_MESSAGE_PREPAPRE_MOVE, too. */
1120 pa_sink_input_set_state_within_thread(i
, i
->state
);
1122 pa_assert(i
->thread_info
.attached
);
1123 i
->thread_info
.attached
= FALSE
;
1125 /* Since the caller sleeps in pa_sink_input_unlink(),
1126 * we can safely access data outside of thread_info even
1127 * though it is mutable */
1129 pa_assert(!i
->sync_prev
);
1130 pa_assert(!i
->sync_next
);
1132 if (i
->thread_info
.sync_prev
) {
1133 i
->thread_info
.sync_prev
->thread_info
.sync_next
= i
->thread_info
.sync_prev
->sync_next
;
1134 i
->thread_info
.sync_prev
= NULL
;
1137 if (i
->thread_info
.sync_next
) {
1138 i
->thread_info
.sync_next
->thread_info
.sync_prev
= i
->thread_info
.sync_next
->sync_prev
;
1139 i
->thread_info
.sync_next
= NULL
;
1142 if (pa_hashmap_remove(s
->thread_info
.inputs
, PA_UINT32_TO_PTR(i
->index
)))
1143 pa_sink_input_unref(i
);
1145 pa_sink_invalidate_requested_latency(s
);
1146 pa_sink_request_rewind(s
, (size_t) -1);
1151 case PA_SINK_MESSAGE_START_MOVE
: {
1152 pa_sink_input
*i
= PA_SINK_INPUT(userdata
);
1154 /* We don't support moving synchronized streams. */
1155 pa_assert(!i
->sync_prev
);
1156 pa_assert(!i
->sync_next
);
1157 pa_assert(!i
->thread_info
.sync_next
);
1158 pa_assert(!i
->thread_info
.sync_prev
);
1160 if (i
->thread_info
.state
!= PA_SINK_INPUT_CORKED
) {
1162 size_t sink_nbytes
, total_nbytes
;
1164 /* Get the latency of the sink */
1165 if (PA_MSGOBJECT(s
)->process_msg(PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_LATENCY
, &usec
, 0, NULL
) < 0)
1168 sink_nbytes
= pa_usec_to_bytes(usec
, &s
->sample_spec
);
1169 total_nbytes
= sink_nbytes
+ pa_memblockq_get_length(i
->thread_info
.render_memblockq
);
1171 if (total_nbytes
> 0) {
1172 i
->thread_info
.rewrite_nbytes
= i
->thread_info
.resampler
? pa_resampler_request(i
->thread_info
.resampler
, total_nbytes
) : total_nbytes
;
1173 i
->thread_info
.rewrite_flush
= TRUE
;
1174 pa_sink_input_process_rewind(i
, sink_nbytes
);
1181 pa_assert(i
->thread_info
.attached
);
1182 i
->thread_info
.attached
= FALSE
;
1184 /* Let's remove the sink input ...*/
1185 if (pa_hashmap_remove(s
->thread_info
.inputs
, PA_UINT32_TO_PTR(i
->index
)))
1186 pa_sink_input_unref(i
);
1188 pa_sink_invalidate_requested_latency(s
);
1190 pa_log_debug("Requesting rewind due to started move");
1191 pa_sink_request_rewind(s
, (size_t) -1);
1196 case PA_SINK_MESSAGE_FINISH_MOVE
: {
1197 pa_sink_input
*i
= PA_SINK_INPUT(userdata
);
1199 /* We don't support moving synchronized streams. */
1200 pa_assert(!i
->sync_prev
);
1201 pa_assert(!i
->sync_next
);
1202 pa_assert(!i
->thread_info
.sync_next
);
1203 pa_assert(!i
->thread_info
.sync_prev
);
1205 pa_hashmap_put(s
->thread_info
.inputs
, PA_UINT32_TO_PTR(i
->index
), pa_sink_input_ref(i
));
1207 pa_assert(!i
->thread_info
.attached
);
1208 i
->thread_info
.attached
= TRUE
;
1213 if (i
->thread_info
.requested_sink_latency
!= (pa_usec_t
) -1)
1214 pa_sink_input_set_requested_latency_within_thread(i
, i
->thread_info
.requested_sink_latency
);
1216 pa_sink_input_update_max_rewind(i
, s
->thread_info
.max_rewind
);
1217 pa_sink_input_update_max_request(i
, s
->thread_info
.max_request
);
1219 if (i
->thread_info
.state
!= PA_SINK_INPUT_CORKED
) {
1223 /* Get the latency of the sink */
1224 if (PA_MSGOBJECT(s
)->process_msg(PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_LATENCY
, &usec
, 0, NULL
) < 0)
1227 nbytes
= pa_usec_to_bytes(usec
, &s
->sample_spec
);
1230 pa_sink_input_drop(i
, nbytes
);
1232 pa_log_debug("Requesting rewind due to finished move");
1233 pa_sink_request_rewind(s
, nbytes
);
1239 case PA_SINK_MESSAGE_SET_VOLUME
:
1240 s
->thread_info
.soft_volume
= *((pa_cvolume
*) userdata
);
1242 pa_sink_request_rewind(s
, (size_t) -1);
1245 case PA_SINK_MESSAGE_SET_MUTE
:
1246 s
->thread_info
.soft_muted
= PA_PTR_TO_UINT(userdata
);
1248 pa_sink_request_rewind(s
, (size_t) -1);
1251 case PA_SINK_MESSAGE_GET_VOLUME
:
1252 *((pa_cvolume
*) userdata
) = s
->thread_info
.soft_volume
;
1255 case PA_SINK_MESSAGE_GET_MUTE
:
1256 *((pa_bool_t
*) userdata
) = s
->thread_info
.soft_muted
;
1259 case PA_SINK_MESSAGE_SET_STATE
:
1261 s
->thread_info
.state
= PA_PTR_TO_UINT(userdata
);
1264 case PA_SINK_MESSAGE_DETACH
:
1266 /* Detach all streams */
1267 pa_sink_detach_within_thread(s
);
1270 case PA_SINK_MESSAGE_ATTACH
:
1272 /* Reattach all streams */
1273 pa_sink_attach_within_thread(s
);
1276 case PA_SINK_MESSAGE_GET_REQUESTED_LATENCY
: {
1278 pa_usec_t
*usec
= userdata
;
1279 *usec
= pa_sink_get_requested_latency_within_thread(s
);
1281 if (*usec
== (pa_usec_t
) -1)
1282 *usec
= s
->thread_info
.max_latency
;
1287 case PA_SINK_MESSAGE_SET_LATENCY_RANGE
: {
1288 pa_usec_t
*r
= userdata
;
1290 pa_sink_update_latency_range(s
, r
[0], r
[1]);
1295 case PA_SINK_MESSAGE_GET_LATENCY_RANGE
: {
1296 pa_usec_t
*r
= userdata
;
1298 r
[0] = s
->thread_info
.min_latency
;
1299 r
[1] = s
->thread_info
.max_latency
;
1304 case PA_SINK_MESSAGE_GET_MAX_REWIND
:
1306 *((size_t*) userdata
) = s
->thread_info
.max_rewind
;
1309 case PA_SINK_MESSAGE_GET_MAX_REQUEST
:
1311 *((size_t*) userdata
) = s
->thread_info
.max_request
;
1314 case PA_SINK_MESSAGE_GET_LATENCY
:
1315 case PA_SINK_MESSAGE_MAX
:
1322 /* Called from main thread */
1323 int pa_sink_suspend_all(pa_core
*c
, pa_bool_t suspend
) {
1328 pa_core_assert_ref(c
);
1330 for (sink
= PA_SINK(pa_idxset_first(c
->sinks
, &idx
)); sink
; sink
= PA_SINK(pa_idxset_next(c
->sinks
, &idx
)))
1331 ret
-= pa_sink_suspend(sink
, suspend
) < 0;
1336 /* Called from main thread */
1337 void pa_sink_detach(pa_sink
*s
) {
1338 pa_sink_assert_ref(s
);
1339 pa_assert(PA_SINK_IS_LINKED(s
->state
));
1341 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_DETACH
, NULL
, 0, NULL
) == 0);
1344 /* Called from main thread */
1345 void pa_sink_attach(pa_sink
*s
) {
1346 pa_sink_assert_ref(s
);
1347 pa_assert(PA_SINK_IS_LINKED(s
->state
));
1349 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_ATTACH
, NULL
, 0, NULL
) == 0);
1352 /* Called from IO thread */
1353 void pa_sink_detach_within_thread(pa_sink
*s
) {
1357 pa_sink_assert_ref(s
);
1358 pa_assert(PA_SINK_IS_LINKED(s
->thread_info
.state
));
1360 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)))
1364 if (s
->monitor_source
)
1365 pa_source_detach_within_thread(s
->monitor_source
);
1368 /* Called from IO thread */
1369 void pa_sink_attach_within_thread(pa_sink
*s
) {
1373 pa_sink_assert_ref(s
);
1374 pa_assert(PA_SINK_IS_LINKED(s
->thread_info
.state
));
1376 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)))
1380 if (s
->monitor_source
)
1381 pa_source_attach_within_thread(s
->monitor_source
);
1384 /* Called from IO thread */
1385 void pa_sink_request_rewind(pa_sink
*s
, size_t nbytes
) {
1386 pa_sink_assert_ref(s
);
1387 pa_assert(PA_SINK_IS_LINKED(s
->thread_info
.state
));
1389 if (nbytes
== (size_t) -1)
1390 nbytes
= s
->thread_info
.max_rewind
;
1392 nbytes
= PA_MIN(nbytes
, s
->thread_info
.max_rewind
);
1394 if (s
->thread_info
.rewind_requested
&&
1395 nbytes
<= s
->thread_info
.rewind_nbytes
)
1398 s
->thread_info
.rewind_nbytes
= nbytes
;
1399 s
->thread_info
.rewind_requested
= TRUE
;
1401 if (s
->request_rewind
)
1402 s
->request_rewind(s
);
1405 /* Called from IO thread */
1406 pa_usec_t
pa_sink_get_requested_latency_within_thread(pa_sink
*s
) {
1407 pa_usec_t result
= (pa_usec_t
) -1;
1410 pa_usec_t monitor_latency
;
1412 pa_sink_assert_ref(s
);
1414 if (s
->thread_info
.requested_latency_valid
)
1415 return s
->thread_info
.requested_latency
;
1417 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)))
1419 if (i
->thread_info
.requested_sink_latency
!= (pa_usec_t
) -1 &&
1420 (result
== (pa_usec_t
) -1 || result
> i
->thread_info
.requested_sink_latency
))
1421 result
= i
->thread_info
.requested_sink_latency
;
1423 monitor_latency
= pa_source_get_requested_latency_within_thread(s
->monitor_source
);
1425 if (monitor_latency
!= (pa_usec_t
) -1 &&
1426 (result
== (pa_usec_t
) -1 || result
> monitor_latency
))
1427 result
= monitor_latency
;
1429 if (result
!= (pa_usec_t
) -1) {
1430 if (s
->thread_info
.max_latency
> 0 && result
> s
->thread_info
.max_latency
)
1431 result
= s
->thread_info
.max_latency
;
1433 if (s
->thread_info
.min_latency
> 0 && result
< s
->thread_info
.min_latency
)
1434 result
= s
->thread_info
.min_latency
;
1437 s
->thread_info
.requested_latency
= result
;
1438 s
->thread_info
.requested_latency_valid
= TRUE
;
1443 /* Called from main thread */
1444 pa_usec_t
pa_sink_get_requested_latency(pa_sink
*s
) {
1447 pa_sink_assert_ref(s
);
1448 pa_assert(PA_SINK_IS_LINKED(s
->state
));
1450 if (!PA_SINK_IS_OPENED(s
->state
))
1453 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_REQUESTED_LATENCY
, &usec
, 0, NULL
) == 0);
1457 /* Called from IO thread */
1458 void pa_sink_set_max_rewind(pa_sink
*s
, size_t max_rewind
) {
1462 pa_sink_assert_ref(s
);
1464 if (max_rewind
== s
->thread_info
.max_rewind
)
1467 s
->thread_info
.max_rewind
= max_rewind
;
1469 if (PA_SINK_IS_LINKED(s
->thread_info
.state
)) {
1470 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)))
1471 pa_sink_input_update_max_rewind(i
, s
->thread_info
.max_rewind
);
1474 if (s
->monitor_source
)
1475 pa_source_set_max_rewind(s
->monitor_source
, s
->thread_info
.max_rewind
);
1478 /* Called from IO thread */
1479 void pa_sink_set_max_request(pa_sink
*s
, size_t max_request
) {
1482 pa_sink_assert_ref(s
);
1484 if (max_request
== s
->thread_info
.max_request
)
1487 s
->thread_info
.max_request
= max_request
;
1489 if (PA_SINK_IS_LINKED(s
->thread_info
.state
)) {
1492 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)))
1493 pa_sink_input_update_max_request(i
, s
->thread_info
.max_request
);
1497 /* Called from IO thread */
1498 void pa_sink_invalidate_requested_latency(pa_sink
*s
) {
1502 pa_sink_assert_ref(s
);
1504 s
->thread_info
.requested_latency_valid
= FALSE
;
1506 if (s
->update_requested_latency
)
1507 s
->update_requested_latency(s
);
1509 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)))
1510 if (i
->update_sink_requested_latency
)
1511 i
->update_sink_requested_latency(i
);
1514 /* Called from main thread */
1515 void pa_sink_set_latency_range(pa_sink
*s
, pa_usec_t min_latency
, pa_usec_t max_latency
) {
1516 pa_sink_assert_ref(s
);
1518 /* min_latency == 0: no limit
1519 * min_latency == (size_t) -1: default limit
1520 * min_latency anything else: specified limit
1522 * Similar for max_latency */
1524 if (min_latency
== (pa_usec_t
) -1)
1525 min_latency
= DEFAULT_MIN_LATENCY
;
1527 if (max_latency
== (pa_usec_t
) -1)
1528 max_latency
= min_latency
;
1530 pa_assert(!min_latency
|| !max_latency
||
1531 min_latency
<= max_latency
);
1533 if (PA_SINK_IS_LINKED(s
->state
)) {
1539 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_LATENCY_RANGE
, r
, 0, NULL
) == 0);
1541 s
->thread_info
.min_latency
= min_latency
;
1542 s
->thread_info
.max_latency
= max_latency
;
1544 s
->monitor_source
->thread_info
.min_latency
= min_latency
;
1545 s
->monitor_source
->thread_info
.max_latency
= max_latency
;
1547 s
->thread_info
.requested_latency_valid
= s
->monitor_source
->thread_info
.requested_latency_valid
= FALSE
;
1551 /* Called from main thread */
1552 void pa_sink_get_latency_range(pa_sink
*s
, pa_usec_t
*min_latency
, pa_usec_t
*max_latency
) {
1553 pa_sink_assert_ref(s
);
1554 pa_assert(min_latency
);
1555 pa_assert(max_latency
);
1557 if (PA_SINK_IS_LINKED(s
->state
)) {
1558 pa_usec_t r
[2] = { 0, 0 };
1560 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_LATENCY_RANGE
, r
, 0, NULL
) == 0);
1562 *min_latency
= r
[0];
1563 *max_latency
= r
[1];
1565 *min_latency
= s
->thread_info
.min_latency
;
1566 *max_latency
= s
->thread_info
.max_latency
;
1570 /* Called from IO thread */
1571 void pa_sink_update_latency_range(pa_sink
*s
, pa_usec_t min_latency
, pa_usec_t max_latency
) {
1575 pa_sink_assert_ref(s
);
1577 s
->thread_info
.min_latency
= min_latency
;
1578 s
->thread_info
.max_latency
= max_latency
;
1580 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)))
1581 if (i
->update_sink_latency_range
)
1582 i
->update_sink_latency_range(i
);
1584 pa_sink_invalidate_requested_latency(s
);
1586 pa_source_update_latency_range(s
->monitor_source
, min_latency
, max_latency
);
1589 size_t pa_sink_get_max_rewind(pa_sink
*s
) {
1591 pa_sink_assert_ref(s
);
1593 if (!PA_SINK_IS_LINKED(s
->state
))
1594 return s
->thread_info
.max_rewind
;
1596 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_MAX_REWIND
, &r
, 0, NULL
) == 0);
1601 size_t pa_sink_get_max_request(pa_sink
*s
) {
1603 pa_sink_assert_ref(s
);
1605 if (!PA_SINK_IS_LINKED(s
->state
))
1606 return s
->thread_info
.max_request
;
1608 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_MAX_REQUEST
, &r
, 0, NULL
) == 0);