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
->muted
= data
->muted
;
195 s
->refresh_volume
= s
->refresh_muted
= FALSE
;
203 pa_silence_memchunk_get(
204 &core
->silence_cache
,
210 s
->thread_info
.inputs
= pa_hashmap_new(pa_idxset_trivial_hash_func
, pa_idxset_trivial_compare_func
);
211 pa_cvolume_reset(&s
->thread_info
.soft_volume
, s
->sample_spec
.channels
);
212 s
->thread_info
.soft_muted
= FALSE
;
213 s
->thread_info
.state
= s
->state
;
214 s
->thread_info
.rewind_nbytes
= 0;
215 s
->thread_info
.rewind_requested
= FALSE
;
216 s
->thread_info
.max_rewind
= 0;
217 s
->thread_info
.max_request
= 0;
218 s
->thread_info
.requested_latency_valid
= FALSE
;
219 s
->thread_info
.requested_latency
= 0;
220 s
->thread_info
.min_latency
= DEFAULT_MIN_LATENCY
;
221 s
->thread_info
.max_latency
= 0;
223 pa_assert_se(pa_idxset_put(core
->sinks
, s
, &s
->index
) >= 0);
225 pa_log_info("Created sink %u \"%s\" with sample spec %s and channel map %s",
228 pa_sample_spec_snprint(st
, sizeof(st
), &s
->sample_spec
),
229 pa_channel_map_snprint(cm
, sizeof(cm
), &s
->channel_map
));
231 pa_source_new_data_init(&source_data
);
232 pa_source_new_data_set_sample_spec(&source_data
, &s
->sample_spec
);
233 pa_source_new_data_set_channel_map(&source_data
, &s
->channel_map
);
234 source_data
.name
= pa_sprintf_malloc("%s.monitor", name
);
235 source_data
.driver
= data
->driver
;
236 source_data
.module
= data
->module
;
238 dn
= pa_proplist_gets(s
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
239 pa_proplist_setf(source_data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
, "Monitor of %s", dn
? dn
: s
->name
);
240 pa_proplist_sets(source_data
.proplist
, PA_PROP_DEVICE_CLASS
, "monitor");
242 s
->monitor_source
= pa_source_new(core
, &source_data
, 0);
244 pa_source_new_data_done(&source_data
);
246 if (!s
->monitor_source
) {
252 s
->monitor_source
->monitor_of
= s
;
254 pa_source_set_latency_range(s
->monitor_source
, s
->thread_info
.min_latency
, s
->thread_info
.max_latency
);
255 pa_source_set_max_rewind(s
->monitor_source
, s
->thread_info
.max_rewind
);
260 /* Called from main context */
261 static int sink_set_state(pa_sink
*s
, pa_sink_state_t state
) {
263 pa_bool_t suspend_change
;
264 pa_sink_state_t original_state
;
268 if (s
->state
== state
)
271 original_state
= s
->state
;
274 (original_state
== PA_SINK_SUSPENDED
&& PA_SINK_IS_OPENED(state
)) ||
275 (PA_SINK_IS_OPENED(original_state
) && state
== PA_SINK_SUSPENDED
);
278 if ((ret
= s
->set_state(s
, state
)) < 0)
282 if ((ret
= pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_STATE
, PA_UINT_TO_PTR(state
), 0, NULL
)) < 0) {
285 s
->set_state(s
, original_state
);
292 if (suspend_change
) {
296 /* We're suspending or resuming, tell everyone about it */
298 for (i
= PA_SINK_INPUT(pa_idxset_first(s
->inputs
, &idx
)); i
; i
= PA_SINK_INPUT(pa_idxset_next(s
->inputs
, &idx
)))
300 i
->suspend(i
, state
== PA_SINK_SUSPENDED
);
303 if (state
!= PA_SINK_UNLINKED
) /* if we enter UNLINKED state pa_sink_unlink() will fire the apropriate events */
304 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_STATE_CHANGED
], s
);
309 /* Called from main context */
310 void pa_sink_put(pa_sink
* s
) {
311 pa_sink_assert_ref(s
);
313 pa_assert(s
->state
== PA_SINK_INIT
);
315 /* The following fields must be initialized properly when calling _put() */
316 pa_assert(s
->asyncmsgq
);
317 pa_assert(s
->rtpoll
);
318 pa_assert(!s
->thread_info
.min_latency
|| !s
->thread_info
.max_latency
||
319 s
->thread_info
.min_latency
<= s
->thread_info
.max_latency
);
321 if (!(s
->flags
& PA_SINK_HW_VOLUME_CTRL
)) {
322 s
->flags
|= PA_SINK_DECIBEL_VOLUME
;
324 s
->thread_info
.soft_volume
= s
->volume
;
325 s
->thread_info
.soft_muted
= s
->muted
;
328 pa_assert_se(sink_set_state(s
, PA_SINK_IDLE
) == 0);
330 pa_source_put(s
->monitor_source
);
332 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
| PA_SUBSCRIPTION_EVENT_NEW
, s
->index
);
333 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_PUT
], s
);
336 /* Called from main context */
337 void pa_sink_unlink(pa_sink
* s
) {
339 pa_sink_input
*i
, *j
= NULL
;
343 /* Please note that pa_sink_unlink() does more than simply
344 * reversing pa_sink_put(). It also undoes the registrations
345 * already done in pa_sink_new()! */
347 /* All operations here shall be idempotent, i.e. pa_sink_unlink()
348 * may be called multiple times on the same sink without bad
351 linked
= PA_SINK_IS_LINKED(s
->state
);
354 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_UNLINK
], s
);
356 if (s
->state
!= PA_SINK_UNLINKED
)
357 pa_namereg_unregister(s
->core
, s
->name
);
358 pa_idxset_remove_by_data(s
->core
->sinks
, s
, NULL
);
360 while ((i
= pa_idxset_first(s
->inputs
, NULL
))) {
362 pa_sink_input_kill(i
);
367 sink_set_state(s
, PA_SINK_UNLINKED
);
369 s
->state
= PA_SINK_UNLINKED
;
373 if (s
->monitor_source
)
374 pa_source_unlink(s
->monitor_source
);
377 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
| PA_SUBSCRIPTION_EVENT_REMOVE
, s
->index
);
378 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_UNLINK_POST
], s
);
382 /* Called from main context */
383 static void sink_free(pa_object
*o
) {
384 pa_sink
*s
= PA_SINK(o
);
388 pa_assert(pa_sink_refcnt(s
) == 0);
390 if (PA_SINK_IS_LINKED(s
->state
))
393 pa_log_info("Freeing sink %u \"%s\"", s
->index
, s
->name
);
395 if (s
->monitor_source
) {
396 pa_source_unref(s
->monitor_source
);
397 s
->monitor_source
= NULL
;
400 pa_idxset_free(s
->inputs
, NULL
, NULL
);
402 while ((i
= pa_hashmap_steal_first(s
->thread_info
.inputs
)))
403 pa_sink_input_unref(i
);
405 pa_hashmap_free(s
->thread_info
.inputs
, NULL
, NULL
);
407 if (s
->silence
.memblock
)
408 pa_memblock_unref(s
->silence
.memblock
);
414 pa_proplist_free(s
->proplist
);
419 /* Called from main context */
420 void pa_sink_set_asyncmsgq(pa_sink
*s
, pa_asyncmsgq
*q
) {
421 pa_sink_assert_ref(s
);
425 if (s
->monitor_source
)
426 pa_source_set_asyncmsgq(s
->monitor_source
, q
);
429 /* Called from main context */
430 void pa_sink_set_rtpoll(pa_sink
*s
, pa_rtpoll
*p
) {
431 pa_sink_assert_ref(s
);
434 if (s
->monitor_source
)
435 pa_source_set_rtpoll(s
->monitor_source
, p
);
438 /* Called from main context */
439 int pa_sink_update_status(pa_sink
*s
) {
440 pa_sink_assert_ref(s
);
441 pa_assert(PA_SINK_IS_LINKED(s
->state
));
443 if (s
->state
== PA_SINK_SUSPENDED
)
446 return sink_set_state(s
, pa_sink_used_by(s
) ? PA_SINK_RUNNING
: PA_SINK_IDLE
);
449 /* Called from main context */
450 int pa_sink_suspend(pa_sink
*s
, pa_bool_t suspend
) {
451 pa_sink_assert_ref(s
);
452 pa_assert(PA_SINK_IS_LINKED(s
->state
));
455 return sink_set_state(s
, PA_SINK_SUSPENDED
);
457 return sink_set_state(s
, pa_sink_used_by(s
) ? PA_SINK_RUNNING
: PA_SINK_IDLE
);
460 /* Called from IO thread context */
461 void pa_sink_process_rewind(pa_sink
*s
, size_t nbytes
) {
464 pa_sink_assert_ref(s
);
465 pa_assert(PA_SINK_IS_LINKED(s
->thread_info
.state
));
467 s
->thread_info
.rewind_nbytes
= 0;
468 s
->thread_info
.rewind_requested
= FALSE
;
471 pa_log_debug("Processing rewind...");
473 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
))) {
474 pa_sink_input_assert_ref(i
);
475 pa_sink_input_process_rewind(i
, nbytes
);
479 if (s
->monitor_source
&& PA_SOURCE_IS_OPENED(s
->monitor_source
->thread_info
.state
))
480 pa_source_process_rewind(s
->monitor_source
, nbytes
);
483 /* Called from IO thread context */
484 static unsigned fill_mix_info(pa_sink
*s
, size_t *length
, pa_mix_info
*info
, unsigned maxinfo
) {
488 size_t mixlength
= *length
;
490 pa_sink_assert_ref(s
);
493 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)) && maxinfo
> 0) {
494 pa_sink_input_assert_ref(i
);
496 if (pa_sink_input_peek(i
, *length
, &info
->chunk
, &info
->volume
) < 0)
499 if (mixlength
== 0 || info
->chunk
.length
< mixlength
)
500 mixlength
= info
->chunk
.length
;
502 if (pa_memblock_is_silence(info
->chunk
.memblock
)) {
503 pa_memblock_unref(info
->chunk
.memblock
);
507 info
->userdata
= pa_sink_input_ref(i
);
509 pa_assert(info
->chunk
.memblock
);
510 pa_assert(info
->chunk
.length
> 0);
523 /* Called from IO thread context */
524 static void inputs_drop(pa_sink
*s
, pa_mix_info
*info
, unsigned n
, pa_memchunk
*result
) {
528 unsigned n_unreffed
= 0;
530 pa_sink_assert_ref(s
);
532 pa_assert(result
->memblock
);
533 pa_assert(result
->length
> 0);
535 /* We optimize for the case where the order of the inputs has not changed */
537 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
))) {
539 pa_mix_info
* m
= NULL
;
541 pa_sink_input_assert_ref(i
);
543 /* Let's try to find the matching entry info the pa_mix_info array */
544 for (j
= 0; j
< n
; j
++) {
546 if (info
[p
].userdata
== i
) {
557 pa_sink_input_drop(i
, result
->length
);
559 if (s
->monitor_source
&& PA_SOURCE_IS_OPENED(pa_source_get_state(s
->monitor_source
))) {
561 if (pa_hashmap_size(i
->thread_info
.direct_outputs
) > 0) {
566 if (m
&& m
->chunk
.memblock
) {
568 pa_memblock_ref(c
.memblock
);
569 pa_assert(result
->length
<= c
.length
);
570 c
.length
= result
->length
;
572 pa_memchunk_make_writable(&c
, 0);
573 pa_volume_memchunk(&c
, &s
->sample_spec
, &m
->volume
);
576 pa_memblock_ref(c
.memblock
);
577 pa_assert(result
->length
<= c
.length
);
578 c
.length
= result
->length
;
581 while ((o
= pa_hashmap_iterate(i
->thread_info
.direct_outputs
, &ostate
, NULL
))) {
582 pa_source_output_assert_ref(o
);
583 pa_assert(o
->direct_on_input
== i
);
584 pa_source_post_direct(s
->monitor_source
, o
, &c
);
587 pa_memblock_unref(c
.memblock
);
592 if (m
->chunk
.memblock
)
593 pa_memblock_unref(m
->chunk
.memblock
);
594 pa_memchunk_reset(&m
->chunk
);
596 pa_sink_input_unref(m
->userdata
);
603 /* Now drop references to entries that are included in the
604 * pa_mix_info array but don't exist anymore */
606 if (n_unreffed
< n
) {
607 for (; n
> 0; info
++, n
--) {
609 pa_sink_input_unref(info
->userdata
);
610 if (info
->chunk
.memblock
)
611 pa_memblock_unref(info
->chunk
.memblock
);
615 if (s
->monitor_source
&& PA_SOURCE_IS_OPENED(pa_source_get_state(s
->monitor_source
)))
616 pa_source_post(s
->monitor_source
, result
);
619 /* Called from IO thread context */
620 void pa_sink_render(pa_sink
*s
, size_t length
, pa_memchunk
*result
) {
621 pa_mix_info info
[MAX_MIX_CHANNELS
];
623 size_t block_size_max
;
625 pa_sink_assert_ref(s
);
626 pa_assert(PA_SINK_IS_OPENED(s
->thread_info
.state
));
627 pa_assert(pa_frame_aligned(length
, &s
->sample_spec
));
632 pa_assert(!s
->thread_info
.rewind_requested
);
633 pa_assert(s
->thread_info
.rewind_nbytes
== 0);
636 length
= pa_frame_align(MIX_BUFFER_LENGTH
, &s
->sample_spec
);
638 block_size_max
= pa_mempool_block_size_max(s
->core
->mempool
);
639 if (length
> block_size_max
)
640 length
= pa_frame_align(block_size_max
, &s
->sample_spec
);
642 pa_assert(length
> 0);
644 n
= s
->thread_info
.state
== PA_SINK_RUNNING
? fill_mix_info(s
, &length
, info
, MAX_MIX_CHANNELS
) : 0;
648 *result
= s
->silence
;
649 pa_memblock_ref(result
->memblock
);
651 if (result
->length
> length
)
652 result
->length
= length
;
657 *result
= info
[0].chunk
;
658 pa_memblock_ref(result
->memblock
);
660 if (result
->length
> length
)
661 result
->length
= length
;
663 pa_sw_cvolume_multiply(&volume
, &s
->thread_info
.soft_volume
, &info
[0].volume
);
665 if (s
->thread_info
.soft_muted
|| !pa_cvolume_is_norm(&volume
)) {
666 pa_memchunk_make_writable(result
, 0);
667 if (s
->thread_info
.soft_muted
|| pa_cvolume_is_muted(&volume
))
668 pa_silence_memchunk(result
, &s
->sample_spec
);
670 pa_volume_memchunk(result
, &s
->sample_spec
, &volume
);
674 result
->memblock
= pa_memblock_new(s
->core
->mempool
, length
);
676 ptr
= pa_memblock_acquire(result
->memblock
);
677 result
->length
= pa_mix(info
, n
,
680 &s
->thread_info
.soft_volume
,
681 s
->thread_info
.soft_muted
);
682 pa_memblock_release(result
->memblock
);
687 if (s
->thread_info
.state
== PA_SINK_RUNNING
)
688 inputs_drop(s
, info
, n
, result
);
693 /* Called from IO thread context */
694 void pa_sink_render_into(pa_sink
*s
, pa_memchunk
*target
) {
695 pa_mix_info info
[MAX_MIX_CHANNELS
];
697 size_t length
, block_size_max
;
699 pa_sink_assert_ref(s
);
700 pa_assert(PA_SINK_IS_OPENED(s
->thread_info
.state
));
702 pa_assert(target
->memblock
);
703 pa_assert(target
->length
> 0);
704 pa_assert(pa_frame_aligned(target
->length
, &s
->sample_spec
));
708 pa_assert(!s
->thread_info
.rewind_requested
);
709 pa_assert(s
->thread_info
.rewind_nbytes
== 0);
711 length
= target
->length
;
712 block_size_max
= pa_mempool_block_size_max(s
->core
->mempool
);
713 if (length
> block_size_max
)
714 length
= pa_frame_align(block_size_max
, &s
->sample_spec
);
716 pa_assert(length
> 0);
718 n
= s
->thread_info
.state
== PA_SINK_RUNNING
? fill_mix_info(s
, &length
, info
, MAX_MIX_CHANNELS
) : 0;
721 if (target
->length
> length
)
722 target
->length
= length
;
724 pa_silence_memchunk(target
, &s
->sample_spec
);
728 if (target
->length
> length
)
729 target
->length
= length
;
731 pa_sw_cvolume_multiply(&volume
, &s
->thread_info
.soft_volume
, &info
[0].volume
);
733 if (s
->thread_info
.soft_muted
|| pa_cvolume_is_muted(&volume
))
734 pa_silence_memchunk(target
, &s
->sample_spec
);
738 vchunk
= info
[0].chunk
;
739 pa_memblock_ref(vchunk
.memblock
);
741 if (vchunk
.length
> length
)
742 vchunk
.length
= length
;
744 if (!pa_cvolume_is_norm(&volume
)) {
745 pa_memchunk_make_writable(&vchunk
, 0);
746 pa_volume_memchunk(&vchunk
, &s
->sample_spec
, &volume
);
749 pa_memchunk_memcpy(target
, &vchunk
);
750 pa_memblock_unref(vchunk
.memblock
);
756 ptr
= pa_memblock_acquire(target
->memblock
);
758 target
->length
= pa_mix(info
, n
,
759 (uint8_t*) ptr
+ target
->index
, length
,
761 &s
->thread_info
.soft_volume
,
762 s
->thread_info
.soft_muted
);
764 pa_memblock_release(target
->memblock
);
767 if (s
->thread_info
.state
== PA_SINK_RUNNING
)
768 inputs_drop(s
, info
, n
, target
);
773 /* Called from IO thread context */
774 void pa_sink_render_into_full(pa_sink
*s
, pa_memchunk
*target
) {
778 pa_sink_assert_ref(s
);
779 pa_assert(PA_SINK_IS_OPENED(s
->thread_info
.state
));
781 pa_assert(target
->memblock
);
782 pa_assert(target
->length
> 0);
783 pa_assert(pa_frame_aligned(target
->length
, &s
->sample_spec
));
787 pa_assert(!s
->thread_info
.rewind_requested
);
788 pa_assert(s
->thread_info
.rewind_nbytes
== 0);
797 pa_sink_render_into(s
, &chunk
);
806 /* Called from IO thread context */
807 void pa_sink_render_full(pa_sink
*s
, size_t length
, pa_memchunk
*result
) {
808 pa_sink_assert_ref(s
);
809 pa_assert(PA_SINK_IS_OPENED(s
->thread_info
.state
));
810 pa_assert(length
> 0);
811 pa_assert(pa_frame_aligned(length
, &s
->sample_spec
));
814 pa_assert(!s
->thread_info
.rewind_requested
);
815 pa_assert(s
->thread_info
.rewind_nbytes
== 0);
817 /*** This needs optimization ***/
820 result
->length
= length
;
821 result
->memblock
= pa_memblock_new(s
->core
->mempool
, length
);
823 pa_sink_render_into_full(s
, result
);
826 /* Called from main thread */
827 pa_usec_t
pa_sink_get_latency(pa_sink
*s
) {
830 pa_sink_assert_ref(s
);
831 pa_assert(PA_SINK_IS_LINKED(s
->state
));
833 /* The returned value is supposed to be in the time domain of the sound card! */
835 if (!PA_SINK_IS_OPENED(s
->state
))
838 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_LATENCY
, &usec
, 0, NULL
) == 0);
843 /* Called from main thread */
844 void pa_sink_set_volume(pa_sink
*s
, const pa_cvolume
*volume
) {
847 pa_sink_assert_ref(s
);
848 pa_assert(PA_SINK_IS_LINKED(s
->state
));
851 changed
= !pa_cvolume_equal(volume
, &s
->volume
);
854 if (s
->set_volume
&& s
->set_volume(s
) < 0)
855 s
->set_volume
= NULL
;
858 pa_sink_set_soft_volume(s
, volume
);
861 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
864 /* Called from main thread */
865 void pa_sink_set_soft_volume(pa_sink
*s
, const pa_cvolume
*volume
) {
866 pa_sink_assert_ref(s
);
869 if (PA_SINK_IS_LINKED(s
->state
))
870 pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_VOLUME
, volume
, 0, NULL
);
872 s
->thread_info
.soft_volume
= *volume
;
875 /* Called from main thread */
876 const pa_cvolume
*pa_sink_get_volume(pa_sink
*s
, pa_bool_t force_refresh
) {
877 pa_sink_assert_ref(s
);
878 pa_assert(PA_SINK_IS_LINKED(s
->state
));
880 if (s
->refresh_volume
|| force_refresh
) {
881 struct pa_cvolume old_volume
= s
->volume
;
883 if (s
->get_volume
&& s
->get_volume(s
) < 0)
884 s
->get_volume
= NULL
;
887 pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_VOLUME
, &s
->volume
, 0, NULL
);
889 if (!pa_cvolume_equal(&old_volume
, &s
->volume
))
890 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
896 /* Called from main thread */
897 void pa_sink_set_mute(pa_sink
*s
, pa_bool_t mute
) {
900 pa_sink_assert_ref(s
);
901 pa_assert(PA_SINK_IS_LINKED(s
->state
));
903 changed
= s
->muted
!= mute
;
906 if (s
->set_mute
&& s
->set_mute(s
) < 0)
910 pa_asyncmsgq_post(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_MUTE
, PA_UINT_TO_PTR(mute
), 0, NULL
, NULL
);
913 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
916 /* Called from main thread */
917 pa_bool_t
pa_sink_get_mute(pa_sink
*s
, pa_bool_t force_refresh
) {
919 pa_sink_assert_ref(s
);
920 pa_assert(PA_SINK_IS_LINKED(s
->state
));
922 if (s
->refresh_muted
|| force_refresh
) {
923 pa_bool_t old_muted
= s
->muted
;
925 if (s
->get_mute
&& s
->get_mute(s
) < 0)
929 pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_MUTE
, &s
->muted
, 0, NULL
);
931 if (old_muted
!= s
->muted
)
932 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
938 /* Called from main thread */
939 void pa_sink_set_description(pa_sink
*s
, const char *description
) {
941 pa_sink_assert_ref(s
);
943 if (!description
&& !pa_proplist_contains(s
->proplist
, PA_PROP_DEVICE_DESCRIPTION
))
946 old
= pa_proplist_gets(s
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
948 if (old
&& description
&& !strcmp(old
, description
))
952 pa_proplist_sets(s
->proplist
, PA_PROP_DEVICE_DESCRIPTION
, description
);
954 pa_proplist_unset(s
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
956 if (s
->monitor_source
) {
959 n
= pa_sprintf_malloc("Monitor Source of %s", description
? description
: s
->name
);
960 pa_source_set_description(s
->monitor_source
, n
);
964 if (PA_SINK_IS_LINKED(s
->state
)) {
965 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
966 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_PROPLIST_CHANGED
], s
);
970 /* Called from main thread */
971 unsigned pa_sink_linked_by(pa_sink
*s
) {
974 pa_sink_assert_ref(s
);
975 pa_assert(PA_SINK_IS_LINKED(s
->state
));
977 ret
= pa_idxset_size(s
->inputs
);
979 /* We add in the number of streams connected to us here. Please
980 * not the asymmmetry to pa_sink_used_by()! */
982 if (s
->monitor_source
)
983 ret
+= pa_source_linked_by(s
->monitor_source
);
988 /* Called from main thread */
989 unsigned pa_sink_used_by(pa_sink
*s
) {
992 pa_sink_assert_ref(s
);
993 pa_assert(PA_SINK_IS_LINKED(s
->state
));
995 ret
= pa_idxset_size(s
->inputs
);
996 pa_assert(ret
>= s
->n_corked
);
998 /* Streams connected to our monitor source do not matter for
999 * pa_sink_used_by()!.*/
1001 return ret
- s
->n_corked
;
1004 /* Called from IO thread, except when it is not */
1005 int pa_sink_process_msg(pa_msgobject
*o
, int code
, void *userdata
, int64_t offset
, pa_memchunk
*chunk
) {
1006 pa_sink
*s
= PA_SINK(o
);
1007 pa_sink_assert_ref(s
);
1009 switch ((pa_sink_message_t
) code
) {
1011 case PA_SINK_MESSAGE_ADD_INPUT
: {
1012 pa_sink_input
*i
= PA_SINK_INPUT(userdata
);
1014 /* If you change anything here, make sure to change the
1015 * sink input handling a few lines down at
1016 * PA_SINK_MESSAGE_FINISH_MOVE, too. */
1018 pa_hashmap_put(s
->thread_info
.inputs
, PA_UINT32_TO_PTR(i
->index
), pa_sink_input_ref(i
));
1020 /* Since the caller sleeps in pa_sink_input_put(), we can
1021 * safely access data outside of thread_info even though
1024 if ((i
->thread_info
.sync_prev
= i
->sync_prev
)) {
1025 pa_assert(i
->sink
== i
->thread_info
.sync_prev
->sink
);
1026 pa_assert(i
->sync_prev
->sync_next
== i
);
1027 i
->thread_info
.sync_prev
->thread_info
.sync_next
= i
;
1030 if ((i
->thread_info
.sync_next
= i
->sync_next
)) {
1031 pa_assert(i
->sink
== i
->thread_info
.sync_next
->sink
);
1032 pa_assert(i
->sync_next
->sync_prev
== i
);
1033 i
->thread_info
.sync_next
->thread_info
.sync_prev
= i
;
1036 pa_assert(!i
->thread_info
.attached
);
1037 i
->thread_info
.attached
= TRUE
;
1042 pa_sink_input_set_state_within_thread(i
, i
->state
);
1044 /* The requested latency of the sink input needs to be
1045 * fixed up and then configured on the sink */
1047 if (i
->thread_info
.requested_sink_latency
!= (pa_usec_t
) -1)
1048 pa_sink_input_set_requested_latency_within_thread(i
, i
->thread_info
.requested_sink_latency
);
1050 pa_sink_input_update_max_rewind(i
, s
->thread_info
.max_rewind
);
1051 pa_sink_input_update_max_request(i
, s
->thread_info
.max_request
);
1053 /* We don't rewind here automatically. This is left to the
1054 * sink input implementor because some sink inputs need a
1055 * slow start, i.e. need some time to buffer client
1056 * samples before beginning streaming. */
1061 case PA_SINK_MESSAGE_REMOVE_INPUT
: {
1062 pa_sink_input
*i
= PA_SINK_INPUT(userdata
);
1064 /* If you change anything here, make sure to change the
1065 * sink input handling a few lines down at
1066 * PA_SINK_MESSAGE_PREPAPRE_MOVE, too. */
1071 pa_sink_input_set_state_within_thread(i
, i
->state
);
1073 pa_assert(i
->thread_info
.attached
);
1074 i
->thread_info
.attached
= FALSE
;
1076 /* Since the caller sleeps in pa_sink_input_unlink(),
1077 * we can safely access data outside of thread_info even
1078 * though it is mutable */
1080 pa_assert(!i
->sync_prev
);
1081 pa_assert(!i
->sync_next
);
1083 if (i
->thread_info
.sync_prev
) {
1084 i
->thread_info
.sync_prev
->thread_info
.sync_next
= i
->thread_info
.sync_prev
->sync_next
;
1085 i
->thread_info
.sync_prev
= NULL
;
1088 if (i
->thread_info
.sync_next
) {
1089 i
->thread_info
.sync_next
->thread_info
.sync_prev
= i
->thread_info
.sync_next
->sync_prev
;
1090 i
->thread_info
.sync_next
= NULL
;
1093 if (pa_hashmap_remove(s
->thread_info
.inputs
, PA_UINT32_TO_PTR(i
->index
)))
1094 pa_sink_input_unref(i
);
1096 pa_sink_invalidate_requested_latency(s
);
1097 pa_sink_request_rewind(s
, (size_t) -1);
1102 case PA_SINK_MESSAGE_START_MOVE
: {
1103 pa_sink_input
*i
= PA_SINK_INPUT(userdata
);
1105 /* We don't support moving synchronized streams. */
1106 pa_assert(!i
->sync_prev
);
1107 pa_assert(!i
->sync_next
);
1108 pa_assert(!i
->thread_info
.sync_next
);
1109 pa_assert(!i
->thread_info
.sync_prev
);
1111 if (i
->thread_info
.state
!= PA_SINK_INPUT_CORKED
) {
1113 size_t sink_nbytes
, total_nbytes
;
1115 /* Get the latency of the sink */
1116 if (PA_MSGOBJECT(s
)->process_msg(PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_LATENCY
, &usec
, 0, NULL
) < 0)
1119 sink_nbytes
= pa_usec_to_bytes(usec
, &s
->sample_spec
);
1120 total_nbytes
= sink_nbytes
+ pa_memblockq_get_length(i
->thread_info
.render_memblockq
);
1122 if (total_nbytes
> 0) {
1123 i
->thread_info
.rewrite_nbytes
= i
->thread_info
.resampler
? pa_resampler_request(i
->thread_info
.resampler
, total_nbytes
) : total_nbytes
;
1124 i
->thread_info
.rewrite_flush
= TRUE
;
1125 pa_sink_input_process_rewind(i
, sink_nbytes
);
1132 pa_assert(i
->thread_info
.attached
);
1133 i
->thread_info
.attached
= FALSE
;
1135 /* Let's remove the sink input ...*/
1136 if (pa_hashmap_remove(s
->thread_info
.inputs
, PA_UINT32_TO_PTR(i
->index
)))
1137 pa_sink_input_unref(i
);
1139 pa_sink_invalidate_requested_latency(s
);
1141 pa_log_debug("Requesting rewind due to started move");
1142 pa_sink_request_rewind(s
, (size_t) -1);
1147 case PA_SINK_MESSAGE_FINISH_MOVE
: {
1148 pa_sink_input
*i
= PA_SINK_INPUT(userdata
);
1150 /* We don't support moving synchronized streams. */
1151 pa_assert(!i
->sync_prev
);
1152 pa_assert(!i
->sync_next
);
1153 pa_assert(!i
->thread_info
.sync_next
);
1154 pa_assert(!i
->thread_info
.sync_prev
);
1156 pa_hashmap_put(s
->thread_info
.inputs
, PA_UINT32_TO_PTR(i
->index
), pa_sink_input_ref(i
));
1158 pa_assert(!i
->thread_info
.attached
);
1159 i
->thread_info
.attached
= TRUE
;
1164 if (i
->thread_info
.requested_sink_latency
!= (pa_usec_t
) -1)
1165 pa_sink_input_set_requested_latency_within_thread(i
, i
->thread_info
.requested_sink_latency
);
1167 pa_sink_input_update_max_rewind(i
, s
->thread_info
.max_rewind
);
1168 pa_sink_input_update_max_request(i
, s
->thread_info
.max_request
);
1170 if (i
->thread_info
.state
!= PA_SINK_INPUT_CORKED
) {
1174 /* Get the latency of the sink */
1175 if (PA_MSGOBJECT(s
)->process_msg(PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_LATENCY
, &usec
, 0, NULL
) < 0)
1178 nbytes
= pa_usec_to_bytes(usec
, &s
->sample_spec
);
1181 pa_sink_input_drop(i
, nbytes
);
1183 pa_log_debug("Requesting rewind due to finished move");
1184 pa_sink_request_rewind(s
, nbytes
);
1190 case PA_SINK_MESSAGE_SET_VOLUME
:
1191 s
->thread_info
.soft_volume
= *((pa_cvolume
*) userdata
);
1193 pa_sink_request_rewind(s
, (size_t) -1);
1196 case PA_SINK_MESSAGE_SET_MUTE
:
1197 s
->thread_info
.soft_muted
= PA_PTR_TO_UINT(userdata
);
1199 pa_sink_request_rewind(s
, (size_t) -1);
1202 case PA_SINK_MESSAGE_GET_VOLUME
:
1203 *((pa_cvolume
*) userdata
) = s
->thread_info
.soft_volume
;
1206 case PA_SINK_MESSAGE_GET_MUTE
:
1207 *((pa_bool_t
*) userdata
) = s
->thread_info
.soft_muted
;
1210 case PA_SINK_MESSAGE_SET_STATE
:
1212 s
->thread_info
.state
= PA_PTR_TO_UINT(userdata
);
1215 case PA_SINK_MESSAGE_DETACH
:
1217 /* Detach all streams */
1218 pa_sink_detach_within_thread(s
);
1221 case PA_SINK_MESSAGE_ATTACH
:
1223 /* Reattach all streams */
1224 pa_sink_attach_within_thread(s
);
1227 case PA_SINK_MESSAGE_GET_REQUESTED_LATENCY
: {
1229 pa_usec_t
*usec
= userdata
;
1230 *usec
= pa_sink_get_requested_latency_within_thread(s
);
1232 if (*usec
== (pa_usec_t
) -1)
1233 *usec
= s
->thread_info
.max_latency
;
1238 case PA_SINK_MESSAGE_SET_LATENCY_RANGE
: {
1239 pa_usec_t
*r
= userdata
;
1241 pa_sink_update_latency_range(s
, r
[0], r
[1]);
1246 case PA_SINK_MESSAGE_GET_LATENCY_RANGE
: {
1247 pa_usec_t
*r
= userdata
;
1249 r
[0] = s
->thread_info
.min_latency
;
1250 r
[1] = s
->thread_info
.max_latency
;
1255 case PA_SINK_MESSAGE_GET_MAX_REWIND
:
1257 *((size_t*) userdata
) = s
->thread_info
.max_rewind
;
1260 case PA_SINK_MESSAGE_GET_MAX_REQUEST
:
1262 *((size_t*) userdata
) = s
->thread_info
.max_request
;
1265 case PA_SINK_MESSAGE_GET_LATENCY
:
1266 case PA_SINK_MESSAGE_MAX
:
1273 /* Called from main thread */
1274 int pa_sink_suspend_all(pa_core
*c
, pa_bool_t suspend
) {
1279 pa_core_assert_ref(c
);
1281 for (sink
= PA_SINK(pa_idxset_first(c
->sinks
, &idx
)); sink
; sink
= PA_SINK(pa_idxset_next(c
->sinks
, &idx
)))
1282 ret
-= pa_sink_suspend(sink
, suspend
) < 0;
1287 /* Called from main thread */
1288 void pa_sink_detach(pa_sink
*s
) {
1289 pa_sink_assert_ref(s
);
1290 pa_assert(PA_SINK_IS_LINKED(s
->state
));
1292 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_DETACH
, NULL
, 0, NULL
) == 0);
1295 /* Called from main thread */
1296 void pa_sink_attach(pa_sink
*s
) {
1297 pa_sink_assert_ref(s
);
1298 pa_assert(PA_SINK_IS_LINKED(s
->state
));
1300 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_ATTACH
, NULL
, 0, NULL
) == 0);
1303 /* Called from IO thread */
1304 void pa_sink_detach_within_thread(pa_sink
*s
) {
1308 pa_sink_assert_ref(s
);
1309 pa_assert(PA_SINK_IS_LINKED(s
->thread_info
.state
));
1311 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)))
1315 if (s
->monitor_source
)
1316 pa_source_detach_within_thread(s
->monitor_source
);
1319 /* Called from IO thread */
1320 void pa_sink_attach_within_thread(pa_sink
*s
) {
1324 pa_sink_assert_ref(s
);
1325 pa_assert(PA_SINK_IS_LINKED(s
->thread_info
.state
));
1327 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)))
1331 if (s
->monitor_source
)
1332 pa_source_attach_within_thread(s
->monitor_source
);
1335 /* Called from IO thread */
1336 void pa_sink_request_rewind(pa_sink
*s
, size_t nbytes
) {
1337 pa_sink_assert_ref(s
);
1338 pa_assert(PA_SINK_IS_LINKED(s
->thread_info
.state
));
1340 if (nbytes
== (size_t) -1)
1341 nbytes
= s
->thread_info
.max_rewind
;
1343 nbytes
= PA_MIN(nbytes
, s
->thread_info
.max_rewind
);
1345 if (s
->thread_info
.rewind_requested
&&
1346 nbytes
<= s
->thread_info
.rewind_nbytes
)
1349 s
->thread_info
.rewind_nbytes
= nbytes
;
1350 s
->thread_info
.rewind_requested
= TRUE
;
1352 if (s
->request_rewind
)
1353 s
->request_rewind(s
);
1356 /* Called from IO thread */
1357 pa_usec_t
pa_sink_get_requested_latency_within_thread(pa_sink
*s
) {
1358 pa_usec_t result
= (pa_usec_t
) -1;
1361 pa_usec_t monitor_latency
;
1363 pa_sink_assert_ref(s
);
1365 if (s
->thread_info
.requested_latency_valid
)
1366 return s
->thread_info
.requested_latency
;
1368 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)))
1370 if (i
->thread_info
.requested_sink_latency
!= (pa_usec_t
) -1 &&
1371 (result
== (pa_usec_t
) -1 || result
> i
->thread_info
.requested_sink_latency
))
1372 result
= i
->thread_info
.requested_sink_latency
;
1374 monitor_latency
= pa_source_get_requested_latency_within_thread(s
->monitor_source
);
1376 if (monitor_latency
!= (pa_usec_t
) -1 &&
1377 (result
== (pa_usec_t
) -1 || result
> monitor_latency
))
1378 result
= monitor_latency
;
1380 if (result
!= (pa_usec_t
) -1) {
1381 if (s
->thread_info
.max_latency
> 0 && result
> s
->thread_info
.max_latency
)
1382 result
= s
->thread_info
.max_latency
;
1384 if (s
->thread_info
.min_latency
> 0 && result
< s
->thread_info
.min_latency
)
1385 result
= s
->thread_info
.min_latency
;
1388 s
->thread_info
.requested_latency
= result
;
1389 s
->thread_info
.requested_latency_valid
= TRUE
;
1394 /* Called from main thread */
1395 pa_usec_t
pa_sink_get_requested_latency(pa_sink
*s
) {
1398 pa_sink_assert_ref(s
);
1399 pa_assert(PA_SINK_IS_LINKED(s
->state
));
1401 if (!PA_SINK_IS_OPENED(s
->state
))
1404 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_REQUESTED_LATENCY
, &usec
, 0, NULL
) == 0);
1408 /* Called from IO thread */
1409 void pa_sink_set_max_rewind(pa_sink
*s
, size_t max_rewind
) {
1413 pa_sink_assert_ref(s
);
1415 if (max_rewind
== s
->thread_info
.max_rewind
)
1418 s
->thread_info
.max_rewind
= max_rewind
;
1420 if (PA_SINK_IS_LINKED(s
->thread_info
.state
)) {
1421 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)))
1422 pa_sink_input_update_max_rewind(i
, s
->thread_info
.max_rewind
);
1425 if (s
->monitor_source
)
1426 pa_source_set_max_rewind(s
->monitor_source
, s
->thread_info
.max_rewind
);
1429 /* Called from IO thread */
1430 void pa_sink_set_max_request(pa_sink
*s
, size_t max_request
) {
1433 pa_sink_assert_ref(s
);
1435 if (max_request
== s
->thread_info
.max_request
)
1438 s
->thread_info
.max_request
= max_request
;
1440 if (PA_SINK_IS_LINKED(s
->thread_info
.state
)) {
1443 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)))
1444 pa_sink_input_update_max_request(i
, s
->thread_info
.max_request
);
1448 /* Called from IO thread */
1449 void pa_sink_invalidate_requested_latency(pa_sink
*s
) {
1453 pa_sink_assert_ref(s
);
1455 s
->thread_info
.requested_latency_valid
= FALSE
;
1457 if (s
->update_requested_latency
)
1458 s
->update_requested_latency(s
);
1460 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)))
1461 if (i
->update_sink_requested_latency
)
1462 i
->update_sink_requested_latency(i
);
1465 /* Called from main thread */
1466 void pa_sink_set_latency_range(pa_sink
*s
, pa_usec_t min_latency
, pa_usec_t max_latency
) {
1467 pa_sink_assert_ref(s
);
1469 /* min_latency == 0: no limit
1470 * min_latency == (size_t) -1: default limit
1471 * min_latency anything else: specified limit
1473 * Similar for max_latency */
1475 if (min_latency
== (pa_usec_t
) -1)
1476 min_latency
= DEFAULT_MIN_LATENCY
;
1478 if (max_latency
== (pa_usec_t
) -1)
1479 max_latency
= min_latency
;
1481 pa_assert(!min_latency
|| !max_latency
||
1482 min_latency
<= max_latency
);
1484 if (PA_SINK_IS_LINKED(s
->state
)) {
1490 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_LATENCY_RANGE
, r
, 0, NULL
) == 0);
1492 s
->thread_info
.min_latency
= min_latency
;
1493 s
->thread_info
.max_latency
= max_latency
;
1495 s
->monitor_source
->thread_info
.min_latency
= min_latency
;
1496 s
->monitor_source
->thread_info
.max_latency
= max_latency
;
1498 s
->thread_info
.requested_latency_valid
= s
->monitor_source
->thread_info
.requested_latency_valid
= FALSE
;
1502 /* Called from main thread */
1503 void pa_sink_get_latency_range(pa_sink
*s
, pa_usec_t
*min_latency
, pa_usec_t
*max_latency
) {
1504 pa_sink_assert_ref(s
);
1505 pa_assert(min_latency
);
1506 pa_assert(max_latency
);
1508 if (PA_SINK_IS_LINKED(s
->state
)) {
1509 pa_usec_t r
[2] = { 0, 0 };
1511 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_LATENCY_RANGE
, r
, 0, NULL
) == 0);
1513 *min_latency
= r
[0];
1514 *max_latency
= r
[1];
1516 *min_latency
= s
->thread_info
.min_latency
;
1517 *max_latency
= s
->thread_info
.max_latency
;
1521 /* Called from IO thread */
1522 void pa_sink_update_latency_range(pa_sink
*s
, pa_usec_t min_latency
, pa_usec_t max_latency
) {
1526 pa_sink_assert_ref(s
);
1528 s
->thread_info
.min_latency
= min_latency
;
1529 s
->thread_info
.max_latency
= max_latency
;
1531 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)))
1532 if (i
->update_sink_latency_range
)
1533 i
->update_sink_latency_range(i
);
1535 pa_sink_invalidate_requested_latency(s
);
1537 pa_source_update_latency_range(s
->monitor_source
, min_latency
, max_latency
);
1540 size_t pa_sink_get_max_rewind(pa_sink
*s
) {
1542 pa_sink_assert_ref(s
);
1544 if (!PA_SINK_IS_LINKED(s
->state
))
1545 return s
->thread_info
.max_rewind
;
1547 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_MAX_REWIND
, &r
, 0, NULL
) == 0);
1552 size_t pa_sink_get_max_request(pa_sink
*s
) {
1554 pa_sink_assert_ref(s
);
1556 if (!PA_SINK_IS_LINKED(s
->state
))
1557 return s
->thread_info
.max_request
;
1559 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_MAX_REQUEST
, &r
, 0, NULL
) == 0);