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
->virtual_volume
= s
->volume
;
197 s
->muted
= data
->muted
;
198 s
->refresh_volume
= s
->refresh_muted
= FALSE
;
206 pa_silence_memchunk_get(
207 &core
->silence_cache
,
213 s
->thread_info
.inputs
= pa_hashmap_new(pa_idxset_trivial_hash_func
, pa_idxset_trivial_compare_func
);
214 pa_cvolume_reset(&s
->thread_info
.soft_volume
, s
->sample_spec
.channels
);
215 s
->thread_info
.soft_muted
= FALSE
;
216 s
->thread_info
.state
= s
->state
;
217 s
->thread_info
.rewind_nbytes
= 0;
218 s
->thread_info
.rewind_requested
= FALSE
;
219 s
->thread_info
.max_rewind
= 0;
220 s
->thread_info
.max_request
= 0;
221 s
->thread_info
.requested_latency_valid
= FALSE
;
222 s
->thread_info
.requested_latency
= 0;
223 s
->thread_info
.min_latency
= DEFAULT_MIN_LATENCY
;
224 s
->thread_info
.max_latency
= 0;
226 pa_assert_se(pa_idxset_put(core
->sinks
, s
, &s
->index
) >= 0);
228 pa_log_info("Created sink %u \"%s\" with sample spec %s and channel map %s",
231 pa_sample_spec_snprint(st
, sizeof(st
), &s
->sample_spec
),
232 pa_channel_map_snprint(cm
, sizeof(cm
), &s
->channel_map
));
234 pa_source_new_data_init(&source_data
);
235 pa_source_new_data_set_sample_spec(&source_data
, &s
->sample_spec
);
236 pa_source_new_data_set_channel_map(&source_data
, &s
->channel_map
);
237 source_data
.name
= pa_sprintf_malloc("%s.monitor", name
);
238 source_data
.driver
= data
->driver
;
239 source_data
.module
= data
->module
;
241 dn
= pa_proplist_gets(s
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
242 pa_proplist_setf(source_data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
, "Monitor of %s", dn
? dn
: s
->name
);
243 pa_proplist_sets(source_data
.proplist
, PA_PROP_DEVICE_CLASS
, "monitor");
245 s
->monitor_source
= pa_source_new(core
, &source_data
, PA_SOURCE_LATENCY
);
247 pa_source_new_data_done(&source_data
);
249 if (!s
->monitor_source
) {
255 s
->monitor_source
->monitor_of
= s
;
257 pa_source_set_latency_range(s
->monitor_source
, s
->thread_info
.min_latency
, s
->thread_info
.max_latency
);
258 pa_source_set_max_rewind(s
->monitor_source
, s
->thread_info
.max_rewind
);
263 /* Called from main context */
264 static int sink_set_state(pa_sink
*s
, pa_sink_state_t state
) {
266 pa_bool_t suspend_change
;
267 pa_sink_state_t original_state
;
271 if (s
->state
== state
)
274 original_state
= s
->state
;
277 (original_state
== PA_SINK_SUSPENDED
&& PA_SINK_IS_OPENED(state
)) ||
278 (PA_SINK_IS_OPENED(original_state
) && state
== PA_SINK_SUSPENDED
);
281 if ((ret
= s
->set_state(s
, state
)) < 0)
285 if ((ret
= pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_STATE
, PA_UINT_TO_PTR(state
), 0, NULL
)) < 0) {
288 s
->set_state(s
, original_state
);
295 if (suspend_change
) {
299 /* We're suspending or resuming, tell everyone about it */
301 for (i
= PA_SINK_INPUT(pa_idxset_first(s
->inputs
, &idx
)); i
; i
= PA_SINK_INPUT(pa_idxset_next(s
->inputs
, &idx
)))
303 i
->suspend(i
, state
== PA_SINK_SUSPENDED
);
306 if (state
!= PA_SINK_UNLINKED
) /* if we enter UNLINKED state pa_sink_unlink() will fire the apropriate events */
307 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_STATE_CHANGED
], s
);
312 /* Called from main context */
313 void pa_sink_put(pa_sink
* s
) {
314 pa_sink_assert_ref(s
);
316 pa_assert(s
->state
== PA_SINK_INIT
);
318 /* The following fields must be initialized properly when calling _put() */
319 pa_assert(s
->asyncmsgq
);
320 pa_assert(s
->rtpoll
);
321 pa_assert(!s
->thread_info
.min_latency
|| !s
->thread_info
.max_latency
||
322 s
->thread_info
.min_latency
<= s
->thread_info
.max_latency
);
324 if (!(s
->flags
& PA_SINK_HW_VOLUME_CTRL
)) {
325 s
->flags
|= PA_SINK_DECIBEL_VOLUME
;
327 s
->thread_info
.soft_volume
= s
->volume
;
328 s
->thread_info
.soft_muted
= s
->muted
;
331 pa_assert_se(sink_set_state(s
, PA_SINK_IDLE
) == 0);
333 pa_source_put(s
->monitor_source
);
335 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
| PA_SUBSCRIPTION_EVENT_NEW
, s
->index
);
336 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_PUT
], s
);
339 /* Called from main context */
340 void pa_sink_unlink(pa_sink
* s
) {
342 pa_sink_input
*i
, *j
= NULL
;
346 /* Please note that pa_sink_unlink() does more than simply
347 * reversing pa_sink_put(). It also undoes the registrations
348 * already done in pa_sink_new()! */
350 /* All operations here shall be idempotent, i.e. pa_sink_unlink()
351 * may be called multiple times on the same sink without bad
354 linked
= PA_SINK_IS_LINKED(s
->state
);
357 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_UNLINK
], s
);
359 if (s
->state
!= PA_SINK_UNLINKED
)
360 pa_namereg_unregister(s
->core
, s
->name
);
361 pa_idxset_remove_by_data(s
->core
->sinks
, s
, NULL
);
363 while ((i
= pa_idxset_first(s
->inputs
, NULL
))) {
365 pa_sink_input_kill(i
);
370 sink_set_state(s
, PA_SINK_UNLINKED
);
372 s
->state
= PA_SINK_UNLINKED
;
376 if (s
->monitor_source
)
377 pa_source_unlink(s
->monitor_source
);
380 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
| PA_SUBSCRIPTION_EVENT_REMOVE
, s
->index
);
381 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_UNLINK_POST
], s
);
385 /* Called from main context */
386 static void sink_free(pa_object
*o
) {
387 pa_sink
*s
= PA_SINK(o
);
391 pa_assert(pa_sink_refcnt(s
) == 0);
393 if (PA_SINK_IS_LINKED(s
->state
))
396 pa_log_info("Freeing sink %u \"%s\"", s
->index
, s
->name
);
398 if (s
->monitor_source
) {
399 pa_source_unref(s
->monitor_source
);
400 s
->monitor_source
= NULL
;
403 pa_idxset_free(s
->inputs
, NULL
, NULL
);
405 while ((i
= pa_hashmap_steal_first(s
->thread_info
.inputs
)))
406 pa_sink_input_unref(i
);
408 pa_hashmap_free(s
->thread_info
.inputs
, NULL
, NULL
);
410 if (s
->silence
.memblock
)
411 pa_memblock_unref(s
->silence
.memblock
);
417 pa_proplist_free(s
->proplist
);
422 /* Called from main context */
423 void pa_sink_set_asyncmsgq(pa_sink
*s
, pa_asyncmsgq
*q
) {
424 pa_sink_assert_ref(s
);
428 if (s
->monitor_source
)
429 pa_source_set_asyncmsgq(s
->monitor_source
, q
);
432 /* Called from main context */
433 void pa_sink_set_rtpoll(pa_sink
*s
, pa_rtpoll
*p
) {
434 pa_sink_assert_ref(s
);
437 if (s
->monitor_source
)
438 pa_source_set_rtpoll(s
->monitor_source
, p
);
441 /* Called from main context */
442 int pa_sink_update_status(pa_sink
*s
) {
443 pa_sink_assert_ref(s
);
444 pa_assert(PA_SINK_IS_LINKED(s
->state
));
446 if (s
->state
== PA_SINK_SUSPENDED
)
449 return sink_set_state(s
, pa_sink_used_by(s
) ? PA_SINK_RUNNING
: PA_SINK_IDLE
);
452 /* Called from main context */
453 int pa_sink_suspend(pa_sink
*s
, pa_bool_t suspend
) {
454 pa_sink_assert_ref(s
);
455 pa_assert(PA_SINK_IS_LINKED(s
->state
));
458 return sink_set_state(s
, PA_SINK_SUSPENDED
);
460 return sink_set_state(s
, pa_sink_used_by(s
) ? PA_SINK_RUNNING
: PA_SINK_IDLE
);
463 /* Called from IO thread context */
464 void pa_sink_process_rewind(pa_sink
*s
, size_t nbytes
) {
467 pa_sink_assert_ref(s
);
468 pa_assert(PA_SINK_IS_LINKED(s
->thread_info
.state
));
470 s
->thread_info
.rewind_nbytes
= 0;
471 s
->thread_info
.rewind_requested
= FALSE
;
474 pa_log_debug("Processing rewind...");
476 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
))) {
477 pa_sink_input_assert_ref(i
);
478 pa_sink_input_process_rewind(i
, nbytes
);
482 if (s
->monitor_source
&& PA_SOURCE_IS_OPENED(s
->monitor_source
->thread_info
.state
))
483 pa_source_process_rewind(s
->monitor_source
, nbytes
);
486 /* Called from IO thread context */
487 static unsigned fill_mix_info(pa_sink
*s
, size_t *length
, pa_mix_info
*info
, unsigned maxinfo
) {
491 size_t mixlength
= *length
;
493 pa_sink_assert_ref(s
);
496 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)) && maxinfo
> 0) {
497 pa_sink_input_assert_ref(i
);
499 if (pa_sink_input_peek(i
, *length
, &info
->chunk
, &info
->volume
) < 0)
502 if (mixlength
== 0 || info
->chunk
.length
< mixlength
)
503 mixlength
= info
->chunk
.length
;
505 if (pa_memblock_is_silence(info
->chunk
.memblock
)) {
506 pa_memblock_unref(info
->chunk
.memblock
);
510 info
->userdata
= pa_sink_input_ref(i
);
512 pa_assert(info
->chunk
.memblock
);
513 pa_assert(info
->chunk
.length
> 0);
526 /* Called from IO thread context */
527 static void inputs_drop(pa_sink
*s
, pa_mix_info
*info
, unsigned n
, pa_memchunk
*result
) {
531 unsigned n_unreffed
= 0;
533 pa_sink_assert_ref(s
);
535 pa_assert(result
->memblock
);
536 pa_assert(result
->length
> 0);
538 /* We optimize for the case where the order of the inputs has not changed */
540 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
))) {
542 pa_mix_info
* m
= NULL
;
544 pa_sink_input_assert_ref(i
);
546 /* Let's try to find the matching entry info the pa_mix_info array */
547 for (j
= 0; j
< n
; j
++) {
549 if (info
[p
].userdata
== i
) {
560 pa_sink_input_drop(i
, result
->length
);
562 if (s
->monitor_source
&& PA_SOURCE_IS_OPENED(pa_source_get_state(s
->monitor_source
))) {
564 if (pa_hashmap_size(i
->thread_info
.direct_outputs
) > 0) {
569 if (m
&& m
->chunk
.memblock
) {
571 pa_memblock_ref(c
.memblock
);
572 pa_assert(result
->length
<= c
.length
);
573 c
.length
= result
->length
;
575 pa_memchunk_make_writable(&c
, 0);
576 pa_volume_memchunk(&c
, &s
->sample_spec
, &m
->volume
);
579 pa_memblock_ref(c
.memblock
);
580 pa_assert(result
->length
<= c
.length
);
581 c
.length
= result
->length
;
584 while ((o
= pa_hashmap_iterate(i
->thread_info
.direct_outputs
, &ostate
, NULL
))) {
585 pa_source_output_assert_ref(o
);
586 pa_assert(o
->direct_on_input
== i
);
587 pa_source_post_direct(s
->monitor_source
, o
, &c
);
590 pa_memblock_unref(c
.memblock
);
595 if (m
->chunk
.memblock
)
596 pa_memblock_unref(m
->chunk
.memblock
);
597 pa_memchunk_reset(&m
->chunk
);
599 pa_sink_input_unref(m
->userdata
);
606 /* Now drop references to entries that are included in the
607 * pa_mix_info array but don't exist anymore */
609 if (n_unreffed
< n
) {
610 for (; n
> 0; info
++, n
--) {
612 pa_sink_input_unref(info
->userdata
);
613 if (info
->chunk
.memblock
)
614 pa_memblock_unref(info
->chunk
.memblock
);
618 if (s
->monitor_source
&& PA_SOURCE_IS_OPENED(pa_source_get_state(s
->monitor_source
)))
619 pa_source_post(s
->monitor_source
, result
);
622 /* Called from IO thread context */
623 void pa_sink_render(pa_sink
*s
, size_t length
, pa_memchunk
*result
) {
624 pa_mix_info info
[MAX_MIX_CHANNELS
];
626 size_t block_size_max
;
628 pa_sink_assert_ref(s
);
629 pa_assert(PA_SINK_IS_OPENED(s
->thread_info
.state
));
630 pa_assert(pa_frame_aligned(length
, &s
->sample_spec
));
635 pa_assert(!s
->thread_info
.rewind_requested
);
636 pa_assert(s
->thread_info
.rewind_nbytes
== 0);
639 length
= pa_frame_align(MIX_BUFFER_LENGTH
, &s
->sample_spec
);
641 block_size_max
= pa_mempool_block_size_max(s
->core
->mempool
);
642 if (length
> block_size_max
)
643 length
= pa_frame_align(block_size_max
, &s
->sample_spec
);
645 pa_assert(length
> 0);
647 n
= s
->thread_info
.state
== PA_SINK_RUNNING
? fill_mix_info(s
, &length
, info
, MAX_MIX_CHANNELS
) : 0;
651 *result
= s
->silence
;
652 pa_memblock_ref(result
->memblock
);
654 if (result
->length
> length
)
655 result
->length
= length
;
660 *result
= info
[0].chunk
;
661 pa_memblock_ref(result
->memblock
);
663 if (result
->length
> length
)
664 result
->length
= length
;
666 pa_sw_cvolume_multiply(&volume
, &s
->thread_info
.soft_volume
, &info
[0].volume
);
668 if (s
->thread_info
.soft_muted
|| !pa_cvolume_is_norm(&volume
)) {
669 pa_memchunk_make_writable(result
, 0);
670 if (s
->thread_info
.soft_muted
|| pa_cvolume_is_muted(&volume
))
671 pa_silence_memchunk(result
, &s
->sample_spec
);
673 pa_volume_memchunk(result
, &s
->sample_spec
, &volume
);
677 result
->memblock
= pa_memblock_new(s
->core
->mempool
, length
);
679 ptr
= pa_memblock_acquire(result
->memblock
);
680 result
->length
= pa_mix(info
, n
,
683 &s
->thread_info
.soft_volume
,
684 s
->thread_info
.soft_muted
);
685 pa_memblock_release(result
->memblock
);
690 if (s
->thread_info
.state
== PA_SINK_RUNNING
)
691 inputs_drop(s
, info
, n
, result
);
696 /* Called from IO thread context */
697 void pa_sink_render_into(pa_sink
*s
, pa_memchunk
*target
) {
698 pa_mix_info info
[MAX_MIX_CHANNELS
];
700 size_t length
, block_size_max
;
702 pa_sink_assert_ref(s
);
703 pa_assert(PA_SINK_IS_OPENED(s
->thread_info
.state
));
705 pa_assert(target
->memblock
);
706 pa_assert(target
->length
> 0);
707 pa_assert(pa_frame_aligned(target
->length
, &s
->sample_spec
));
711 pa_assert(!s
->thread_info
.rewind_requested
);
712 pa_assert(s
->thread_info
.rewind_nbytes
== 0);
714 length
= target
->length
;
715 block_size_max
= pa_mempool_block_size_max(s
->core
->mempool
);
716 if (length
> block_size_max
)
717 length
= pa_frame_align(block_size_max
, &s
->sample_spec
);
719 pa_assert(length
> 0);
721 n
= s
->thread_info
.state
== PA_SINK_RUNNING
? fill_mix_info(s
, &length
, info
, MAX_MIX_CHANNELS
) : 0;
724 if (target
->length
> length
)
725 target
->length
= length
;
727 pa_silence_memchunk(target
, &s
->sample_spec
);
731 if (target
->length
> length
)
732 target
->length
= length
;
734 pa_sw_cvolume_multiply(&volume
, &s
->thread_info
.soft_volume
, &info
[0].volume
);
736 if (s
->thread_info
.soft_muted
|| pa_cvolume_is_muted(&volume
))
737 pa_silence_memchunk(target
, &s
->sample_spec
);
741 vchunk
= info
[0].chunk
;
742 pa_memblock_ref(vchunk
.memblock
);
744 if (vchunk
.length
> length
)
745 vchunk
.length
= length
;
747 if (!pa_cvolume_is_norm(&volume
)) {
748 pa_memchunk_make_writable(&vchunk
, 0);
749 pa_volume_memchunk(&vchunk
, &s
->sample_spec
, &volume
);
752 pa_memchunk_memcpy(target
, &vchunk
);
753 pa_memblock_unref(vchunk
.memblock
);
759 ptr
= pa_memblock_acquire(target
->memblock
);
761 target
->length
= pa_mix(info
, n
,
762 (uint8_t*) ptr
+ target
->index
, length
,
764 &s
->thread_info
.soft_volume
,
765 s
->thread_info
.soft_muted
);
767 pa_memblock_release(target
->memblock
);
770 if (s
->thread_info
.state
== PA_SINK_RUNNING
)
771 inputs_drop(s
, info
, n
, target
);
776 /* Called from IO thread context */
777 void pa_sink_render_into_full(pa_sink
*s
, pa_memchunk
*target
) {
781 pa_sink_assert_ref(s
);
782 pa_assert(PA_SINK_IS_OPENED(s
->thread_info
.state
));
784 pa_assert(target
->memblock
);
785 pa_assert(target
->length
> 0);
786 pa_assert(pa_frame_aligned(target
->length
, &s
->sample_spec
));
790 pa_assert(!s
->thread_info
.rewind_requested
);
791 pa_assert(s
->thread_info
.rewind_nbytes
== 0);
800 pa_sink_render_into(s
, &chunk
);
809 /* Called from IO thread context */
810 void pa_sink_render_full(pa_sink
*s
, size_t length
, pa_memchunk
*result
) {
811 pa_sink_assert_ref(s
);
812 pa_assert(PA_SINK_IS_OPENED(s
->thread_info
.state
));
813 pa_assert(length
> 0);
814 pa_assert(pa_frame_aligned(length
, &s
->sample_spec
));
817 pa_assert(!s
->thread_info
.rewind_requested
);
818 pa_assert(s
->thread_info
.rewind_nbytes
== 0);
820 /*** This needs optimization ***/
823 result
->length
= length
;
824 result
->memblock
= pa_memblock_new(s
->core
->mempool
, length
);
826 pa_sink_render_into_full(s
, result
);
829 /* Called from main thread */
830 pa_usec_t
pa_sink_get_latency(pa_sink
*s
) {
833 pa_sink_assert_ref(s
);
834 pa_assert(PA_SINK_IS_LINKED(s
->state
));
836 /* The returned value is supposed to be in the time domain of the sound card! */
838 if (!PA_SINK_IS_OPENED(s
->state
))
841 if (!(s
->flags
& PA_SINK_LATENCY
))
844 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_LATENCY
, &usec
, 0, NULL
) == 0);
849 /* Called from main thread */
850 void pa_sink_set_volume(pa_sink
*s
, const pa_cvolume
*volume
) {
852 pa_sink_set_volume_data data
;
854 pa_sink_assert_ref(s
);
855 pa_assert(PA_SINK_IS_LINKED(s
->state
));
857 pa_assert(pa_cvolume_valid(volume
));
858 pa_assert(pa_cvolume_compatible(volume
, &s
->sample_spec
));
861 data
.virtual_volume
= data
.volume
= *volume
;
863 changed
= !pa_cvolume_equal(&data
.virtual_volume
, &s
->virtual_volume
) ||
864 !pa_cvolume_equal(&data
.volume
, &s
->volume
);
867 if (pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_SET_VOLUME
], &data
) < 0)
870 changed
= !pa_cvolume_equal(&data
.virtual_volume
, &s
->virtual_volume
); /* from client-side view */
873 s
->volume
= data
.volume
;
874 s
->virtual_volume
= data
.virtual_volume
;
876 if (s
->set_volume
&& s
->set_volume(s
) < 0)
877 s
->set_volume
= NULL
;
880 pa_sink_set_soft_volume(s
, &s
->volume
);
883 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
886 /* Called from main thread */
887 void pa_sink_set_soft_volume(pa_sink
*s
, const pa_cvolume
*volume
) {
888 pa_sink_assert_ref(s
);
891 if (PA_SINK_IS_LINKED(s
->state
))
892 pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_VOLUME
, volume
, 0, NULL
);
894 s
->thread_info
.soft_volume
= *volume
;
897 /* Called from main thread */
898 const pa_cvolume
*pa_sink_get_volume(pa_sink
*s
, pa_bool_t force_refresh
) {
899 pa_sink_assert_ref(s
);
900 pa_assert(PA_SINK_IS_LINKED(s
->state
));
902 if (s
->refresh_volume
|| force_refresh
) {
903 struct pa_cvolume old_volume
= s
->virtual_volume
;
905 if (s
->get_volume
&& s
->get_volume(s
) < 0)
906 s
->get_volume
= NULL
;
908 if (!s
->get_volume
) {
909 pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_VOLUME
, &s
->volume
, 0, NULL
);
910 s
->virtual_volume
= s
->volume
;
913 if (!pa_cvolume_equal(&old_volume
, &s
->virtual_volume
))
914 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
917 return &s
->virtual_volume
;
920 /* Called from main thread */
921 void pa_sink_set_mute(pa_sink
*s
, pa_bool_t mute
) {
924 pa_sink_assert_ref(s
);
925 pa_assert(PA_SINK_IS_LINKED(s
->state
));
927 changed
= s
->muted
!= mute
;
930 if (s
->set_mute
&& s
->set_mute(s
) < 0)
934 pa_asyncmsgq_post(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_MUTE
, PA_UINT_TO_PTR(mute
), 0, NULL
, NULL
);
937 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
940 /* Called from main thread */
941 pa_bool_t
pa_sink_get_mute(pa_sink
*s
, pa_bool_t force_refresh
) {
943 pa_sink_assert_ref(s
);
944 pa_assert(PA_SINK_IS_LINKED(s
->state
));
946 if (s
->refresh_muted
|| force_refresh
) {
947 pa_bool_t old_muted
= s
->muted
;
949 if (s
->get_mute
&& s
->get_mute(s
) < 0)
953 pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_MUTE
, &s
->muted
, 0, NULL
);
955 if (old_muted
!= s
->muted
)
956 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
962 /* Called from main thread */
963 pa_bool_t
pa_sink_update_proplist(pa_sink
*s
, pa_update_mode_t mode
, pa_proplist
*p
) {
965 pa_sink_assert_ref(s
);
967 pa_proplist_update(s
->proplist
, mode
, p
);
969 if (PA_SINK_IS_LINKED(s
->state
)) {
970 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_PROPLIST_CHANGED
], s
);
971 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
977 /* Called from main thread */
978 void pa_sink_set_description(pa_sink
*s
, const char *description
) {
980 pa_sink_assert_ref(s
);
982 if (!description
&& !pa_proplist_contains(s
->proplist
, PA_PROP_DEVICE_DESCRIPTION
))
985 old
= pa_proplist_gets(s
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
987 if (old
&& description
&& !strcmp(old
, description
))
991 pa_proplist_sets(s
->proplist
, PA_PROP_DEVICE_DESCRIPTION
, description
);
993 pa_proplist_unset(s
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
995 if (s
->monitor_source
) {
998 n
= pa_sprintf_malloc("Monitor Source of %s", description
? description
: s
->name
);
999 pa_source_set_description(s
->monitor_source
, n
);
1003 if (PA_SINK_IS_LINKED(s
->state
)) {
1004 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
1005 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_PROPLIST_CHANGED
], s
);
1009 /* Called from main thread */
1010 unsigned pa_sink_linked_by(pa_sink
*s
) {
1013 pa_sink_assert_ref(s
);
1014 pa_assert(PA_SINK_IS_LINKED(s
->state
));
1016 ret
= pa_idxset_size(s
->inputs
);
1018 /* We add in the number of streams connected to us here. Please
1019 * note the asymmmetry to pa_sink_used_by()! */
1021 if (s
->monitor_source
)
1022 ret
+= pa_source_linked_by(s
->monitor_source
);
1027 /* Called from main thread */
1028 unsigned pa_sink_used_by(pa_sink
*s
) {
1031 pa_sink_assert_ref(s
);
1032 pa_assert(PA_SINK_IS_LINKED(s
->state
));
1034 ret
= pa_idxset_size(s
->inputs
);
1035 pa_assert(ret
>= s
->n_corked
);
1037 /* Streams connected to our monitor source do not matter for
1038 * pa_sink_used_by()!.*/
1040 return ret
- s
->n_corked
;
1043 /* Called from main thread */
1044 unsigned pa_sink_check_suspend(pa_sink
*s
) {
1049 pa_sink_assert_ref(s
);
1051 if (!PA_SINK_IS_LINKED(s
->state
))
1056 for (i
= PA_SINK_INPUT(pa_idxset_first(s
->inputs
, &idx
)); i
; i
= PA_SINK_INPUT(pa_idxset_next(s
->inputs
, &idx
))) {
1057 pa_sink_input_state_t st
;
1059 st
= pa_sink_input_get_state(i
);
1060 pa_assert(PA_SINK_INPUT_IS_LINKED(st
));
1062 if (st
== PA_SINK_INPUT_CORKED
)
1065 if (i
->flags
& PA_SINK_INPUT_DONT_INHIBIT_AUTO_SUSPEND
)
1071 if (s
->monitor_source
)
1072 ret
+= pa_source_check_suspend(s
->monitor_source
);
1077 /* Called from IO thread, except when it is not */
1078 int pa_sink_process_msg(pa_msgobject
*o
, int code
, void *userdata
, int64_t offset
, pa_memchunk
*chunk
) {
1079 pa_sink
*s
= PA_SINK(o
);
1080 pa_sink_assert_ref(s
);
1082 switch ((pa_sink_message_t
) code
) {
1084 case PA_SINK_MESSAGE_ADD_INPUT
: {
1085 pa_sink_input
*i
= PA_SINK_INPUT(userdata
);
1087 /* If you change anything here, make sure to change the
1088 * sink input handling a few lines down at
1089 * PA_SINK_MESSAGE_FINISH_MOVE, too. */
1091 pa_hashmap_put(s
->thread_info
.inputs
, PA_UINT32_TO_PTR(i
->index
), pa_sink_input_ref(i
));
1093 /* Since the caller sleeps in pa_sink_input_put(), we can
1094 * safely access data outside of thread_info even though
1097 if ((i
->thread_info
.sync_prev
= i
->sync_prev
)) {
1098 pa_assert(i
->sink
== i
->thread_info
.sync_prev
->sink
);
1099 pa_assert(i
->sync_prev
->sync_next
== i
);
1100 i
->thread_info
.sync_prev
->thread_info
.sync_next
= i
;
1103 if ((i
->thread_info
.sync_next
= i
->sync_next
)) {
1104 pa_assert(i
->sink
== i
->thread_info
.sync_next
->sink
);
1105 pa_assert(i
->sync_next
->sync_prev
== i
);
1106 i
->thread_info
.sync_next
->thread_info
.sync_prev
= i
;
1109 pa_assert(!i
->thread_info
.attached
);
1110 i
->thread_info
.attached
= TRUE
;
1115 pa_sink_input_set_state_within_thread(i
, i
->state
);
1117 /* The requested latency of the sink input needs to be
1118 * fixed up and then configured on the sink */
1120 if (i
->thread_info
.requested_sink_latency
!= (pa_usec_t
) -1)
1121 pa_sink_input_set_requested_latency_within_thread(i
, i
->thread_info
.requested_sink_latency
);
1123 pa_sink_input_update_max_rewind(i
, s
->thread_info
.max_rewind
);
1124 pa_sink_input_update_max_request(i
, s
->thread_info
.max_request
);
1126 /* We don't rewind here automatically. This is left to the
1127 * sink input implementor because some sink inputs need a
1128 * slow start, i.e. need some time to buffer client
1129 * samples before beginning streaming. */
1134 case PA_SINK_MESSAGE_REMOVE_INPUT
: {
1135 pa_sink_input
*i
= PA_SINK_INPUT(userdata
);
1137 /* If you change anything here, make sure to change the
1138 * sink input handling a few lines down at
1139 * PA_SINK_MESSAGE_PREPAPRE_MOVE, too. */
1144 pa_sink_input_set_state_within_thread(i
, i
->state
);
1146 pa_assert(i
->thread_info
.attached
);
1147 i
->thread_info
.attached
= FALSE
;
1149 /* Since the caller sleeps in pa_sink_input_unlink(),
1150 * we can safely access data outside of thread_info even
1151 * though it is mutable */
1153 pa_assert(!i
->sync_prev
);
1154 pa_assert(!i
->sync_next
);
1156 if (i
->thread_info
.sync_prev
) {
1157 i
->thread_info
.sync_prev
->thread_info
.sync_next
= i
->thread_info
.sync_prev
->sync_next
;
1158 i
->thread_info
.sync_prev
= NULL
;
1161 if (i
->thread_info
.sync_next
) {
1162 i
->thread_info
.sync_next
->thread_info
.sync_prev
= i
->thread_info
.sync_next
->sync_prev
;
1163 i
->thread_info
.sync_next
= NULL
;
1166 if (pa_hashmap_remove(s
->thread_info
.inputs
, PA_UINT32_TO_PTR(i
->index
)))
1167 pa_sink_input_unref(i
);
1169 pa_sink_invalidate_requested_latency(s
);
1170 pa_sink_request_rewind(s
, (size_t) -1);
1175 case PA_SINK_MESSAGE_START_MOVE
: {
1176 pa_sink_input
*i
= PA_SINK_INPUT(userdata
);
1178 /* We don't support moving synchronized streams. */
1179 pa_assert(!i
->sync_prev
);
1180 pa_assert(!i
->sync_next
);
1181 pa_assert(!i
->thread_info
.sync_next
);
1182 pa_assert(!i
->thread_info
.sync_prev
);
1184 if (i
->thread_info
.state
!= PA_SINK_INPUT_CORKED
) {
1186 size_t sink_nbytes
, total_nbytes
;
1188 /* Get the latency of the sink */
1189 if (!(s
->flags
& PA_SINK_LATENCY
) ||
1190 PA_MSGOBJECT(s
)->process_msg(PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_LATENCY
, &usec
, 0, NULL
) < 0)
1193 sink_nbytes
= pa_usec_to_bytes(usec
, &s
->sample_spec
);
1194 total_nbytes
= sink_nbytes
+ pa_memblockq_get_length(i
->thread_info
.render_memblockq
);
1196 if (total_nbytes
> 0) {
1197 i
->thread_info
.rewrite_nbytes
= i
->thread_info
.resampler
? pa_resampler_request(i
->thread_info
.resampler
, total_nbytes
) : total_nbytes
;
1198 i
->thread_info
.rewrite_flush
= TRUE
;
1199 pa_sink_input_process_rewind(i
, sink_nbytes
);
1206 pa_assert(i
->thread_info
.attached
);
1207 i
->thread_info
.attached
= FALSE
;
1209 /* Let's remove the sink input ...*/
1210 if (pa_hashmap_remove(s
->thread_info
.inputs
, PA_UINT32_TO_PTR(i
->index
)))
1211 pa_sink_input_unref(i
);
1213 pa_sink_invalidate_requested_latency(s
);
1215 pa_log_debug("Requesting rewind due to started move");
1216 pa_sink_request_rewind(s
, (size_t) -1);
1221 case PA_SINK_MESSAGE_FINISH_MOVE
: {
1222 pa_sink_input
*i
= PA_SINK_INPUT(userdata
);
1224 /* We don't support moving synchronized streams. */
1225 pa_assert(!i
->sync_prev
);
1226 pa_assert(!i
->sync_next
);
1227 pa_assert(!i
->thread_info
.sync_next
);
1228 pa_assert(!i
->thread_info
.sync_prev
);
1230 pa_hashmap_put(s
->thread_info
.inputs
, PA_UINT32_TO_PTR(i
->index
), pa_sink_input_ref(i
));
1232 pa_assert(!i
->thread_info
.attached
);
1233 i
->thread_info
.attached
= TRUE
;
1238 if (i
->thread_info
.requested_sink_latency
!= (pa_usec_t
) -1)
1239 pa_sink_input_set_requested_latency_within_thread(i
, i
->thread_info
.requested_sink_latency
);
1241 pa_sink_input_update_max_rewind(i
, s
->thread_info
.max_rewind
);
1242 pa_sink_input_update_max_request(i
, s
->thread_info
.max_request
);
1244 if (i
->thread_info
.state
!= PA_SINK_INPUT_CORKED
) {
1248 /* Get the latency of the sink */
1249 if (!(s
->flags
& PA_SINK_LATENCY
) ||
1250 PA_MSGOBJECT(s
)->process_msg(PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_LATENCY
, &usec
, 0, NULL
) < 0)
1253 nbytes
= pa_usec_to_bytes(usec
, &s
->sample_spec
);
1256 pa_sink_input_drop(i
, nbytes
);
1258 pa_log_debug("Requesting rewind due to finished move");
1259 pa_sink_request_rewind(s
, nbytes
);
1265 case PA_SINK_MESSAGE_SET_VOLUME
:
1266 s
->thread_info
.soft_volume
= *((pa_cvolume
*) userdata
);
1268 pa_sink_request_rewind(s
, (size_t) -1);
1271 case PA_SINK_MESSAGE_SET_MUTE
:
1272 s
->thread_info
.soft_muted
= PA_PTR_TO_UINT(userdata
);
1274 pa_sink_request_rewind(s
, (size_t) -1);
1277 case PA_SINK_MESSAGE_GET_VOLUME
:
1278 *((pa_cvolume
*) userdata
) = s
->thread_info
.soft_volume
;
1281 case PA_SINK_MESSAGE_GET_MUTE
:
1282 *((pa_bool_t
*) userdata
) = s
->thread_info
.soft_muted
;
1285 case PA_SINK_MESSAGE_SET_STATE
:
1287 s
->thread_info
.state
= PA_PTR_TO_UINT(userdata
);
1290 case PA_SINK_MESSAGE_DETACH
:
1292 /* Detach all streams */
1293 pa_sink_detach_within_thread(s
);
1296 case PA_SINK_MESSAGE_ATTACH
:
1298 /* Reattach all streams */
1299 pa_sink_attach_within_thread(s
);
1302 case PA_SINK_MESSAGE_GET_REQUESTED_LATENCY
: {
1304 pa_usec_t
*usec
= userdata
;
1305 *usec
= pa_sink_get_requested_latency_within_thread(s
);
1307 if (*usec
== (pa_usec_t
) -1)
1308 *usec
= s
->thread_info
.max_latency
;
1313 case PA_SINK_MESSAGE_SET_LATENCY_RANGE
: {
1314 pa_usec_t
*r
= userdata
;
1316 pa_sink_update_latency_range(s
, r
[0], r
[1]);
1321 case PA_SINK_MESSAGE_GET_LATENCY_RANGE
: {
1322 pa_usec_t
*r
= userdata
;
1324 r
[0] = s
->thread_info
.min_latency
;
1325 r
[1] = s
->thread_info
.max_latency
;
1330 case PA_SINK_MESSAGE_GET_MAX_REWIND
:
1332 *((size_t*) userdata
) = s
->thread_info
.max_rewind
;
1335 case PA_SINK_MESSAGE_GET_MAX_REQUEST
:
1337 *((size_t*) userdata
) = s
->thread_info
.max_request
;
1340 case PA_SINK_MESSAGE_GET_LATENCY
:
1341 case PA_SINK_MESSAGE_MAX
:
1348 /* Called from main thread */
1349 int pa_sink_suspend_all(pa_core
*c
, pa_bool_t suspend
) {
1354 pa_core_assert_ref(c
);
1356 for (sink
= PA_SINK(pa_idxset_first(c
->sinks
, &idx
)); sink
; sink
= PA_SINK(pa_idxset_next(c
->sinks
, &idx
)))
1357 ret
-= pa_sink_suspend(sink
, suspend
) < 0;
1362 /* Called from main thread */
1363 void pa_sink_detach(pa_sink
*s
) {
1364 pa_sink_assert_ref(s
);
1365 pa_assert(PA_SINK_IS_LINKED(s
->state
));
1367 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_DETACH
, NULL
, 0, NULL
) == 0);
1370 /* Called from main thread */
1371 void pa_sink_attach(pa_sink
*s
) {
1372 pa_sink_assert_ref(s
);
1373 pa_assert(PA_SINK_IS_LINKED(s
->state
));
1375 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_ATTACH
, NULL
, 0, NULL
) == 0);
1378 /* Called from IO thread */
1379 void pa_sink_detach_within_thread(pa_sink
*s
) {
1383 pa_sink_assert_ref(s
);
1384 pa_assert(PA_SINK_IS_LINKED(s
->thread_info
.state
));
1386 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)))
1390 if (s
->monitor_source
)
1391 pa_source_detach_within_thread(s
->monitor_source
);
1394 /* Called from IO thread */
1395 void pa_sink_attach_within_thread(pa_sink
*s
) {
1399 pa_sink_assert_ref(s
);
1400 pa_assert(PA_SINK_IS_LINKED(s
->thread_info
.state
));
1402 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)))
1406 if (s
->monitor_source
)
1407 pa_source_attach_within_thread(s
->monitor_source
);
1410 /* Called from IO thread */
1411 void pa_sink_request_rewind(pa_sink
*s
, size_t nbytes
) {
1412 pa_sink_assert_ref(s
);
1413 pa_assert(PA_SINK_IS_LINKED(s
->thread_info
.state
));
1415 if (nbytes
== (size_t) -1)
1416 nbytes
= s
->thread_info
.max_rewind
;
1418 nbytes
= PA_MIN(nbytes
, s
->thread_info
.max_rewind
);
1420 if (s
->thread_info
.rewind_requested
&&
1421 nbytes
<= s
->thread_info
.rewind_nbytes
)
1424 s
->thread_info
.rewind_nbytes
= nbytes
;
1425 s
->thread_info
.rewind_requested
= TRUE
;
1427 if (s
->request_rewind
)
1428 s
->request_rewind(s
);
1431 /* Called from IO thread */
1432 pa_usec_t
pa_sink_get_requested_latency_within_thread(pa_sink
*s
) {
1433 pa_usec_t result
= (pa_usec_t
) -1;
1436 pa_usec_t monitor_latency
;
1438 pa_sink_assert_ref(s
);
1440 if (s
->thread_info
.requested_latency_valid
)
1441 return s
->thread_info
.requested_latency
;
1443 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)))
1445 if (i
->thread_info
.requested_sink_latency
!= (pa_usec_t
) -1 &&
1446 (result
== (pa_usec_t
) -1 || result
> i
->thread_info
.requested_sink_latency
))
1447 result
= i
->thread_info
.requested_sink_latency
;
1449 monitor_latency
= pa_source_get_requested_latency_within_thread(s
->monitor_source
);
1451 if (monitor_latency
!= (pa_usec_t
) -1 &&
1452 (result
== (pa_usec_t
) -1 || result
> monitor_latency
))
1453 result
= monitor_latency
;
1455 if (result
!= (pa_usec_t
) -1) {
1456 if (s
->thread_info
.max_latency
> 0 && result
> s
->thread_info
.max_latency
)
1457 result
= s
->thread_info
.max_latency
;
1459 if (s
->thread_info
.min_latency
> 0 && result
< s
->thread_info
.min_latency
)
1460 result
= s
->thread_info
.min_latency
;
1463 s
->thread_info
.requested_latency
= result
;
1464 s
->thread_info
.requested_latency_valid
= TRUE
;
1469 /* Called from main thread */
1470 pa_usec_t
pa_sink_get_requested_latency(pa_sink
*s
) {
1473 pa_sink_assert_ref(s
);
1474 pa_assert(PA_SINK_IS_LINKED(s
->state
));
1476 if (!PA_SINK_IS_OPENED(s
->state
))
1479 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_REQUESTED_LATENCY
, &usec
, 0, NULL
) == 0);
1483 /* Called from IO thread */
1484 void pa_sink_set_max_rewind(pa_sink
*s
, size_t max_rewind
) {
1488 pa_sink_assert_ref(s
);
1490 if (max_rewind
== s
->thread_info
.max_rewind
)
1493 s
->thread_info
.max_rewind
= max_rewind
;
1495 if (PA_SINK_IS_LINKED(s
->thread_info
.state
)) {
1496 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)))
1497 pa_sink_input_update_max_rewind(i
, s
->thread_info
.max_rewind
);
1500 if (s
->monitor_source
)
1501 pa_source_set_max_rewind(s
->monitor_source
, s
->thread_info
.max_rewind
);
1504 /* Called from IO thread */
1505 void pa_sink_set_max_request(pa_sink
*s
, size_t max_request
) {
1508 pa_sink_assert_ref(s
);
1510 if (max_request
== s
->thread_info
.max_request
)
1513 s
->thread_info
.max_request
= max_request
;
1515 if (PA_SINK_IS_LINKED(s
->thread_info
.state
)) {
1518 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)))
1519 pa_sink_input_update_max_request(i
, s
->thread_info
.max_request
);
1523 /* Called from IO thread */
1524 void pa_sink_invalidate_requested_latency(pa_sink
*s
) {
1528 pa_sink_assert_ref(s
);
1530 s
->thread_info
.requested_latency_valid
= FALSE
;
1532 if (s
->update_requested_latency
)
1533 s
->update_requested_latency(s
);
1535 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)))
1536 if (i
->update_sink_requested_latency
)
1537 i
->update_sink_requested_latency(i
);
1540 /* Called from main thread */
1541 void pa_sink_set_latency_range(pa_sink
*s
, pa_usec_t min_latency
, pa_usec_t max_latency
) {
1542 pa_sink_assert_ref(s
);
1544 /* min_latency == 0: no limit
1545 * min_latency == (size_t) -1: default limit
1546 * min_latency anything else: specified limit
1548 * Similar for max_latency */
1550 if (min_latency
== (pa_usec_t
) -1)
1551 min_latency
= DEFAULT_MIN_LATENCY
;
1553 if (max_latency
== (pa_usec_t
) -1)
1554 max_latency
= min_latency
;
1556 pa_assert(!min_latency
|| !max_latency
||
1557 min_latency
<= max_latency
);
1559 if (PA_SINK_IS_LINKED(s
->state
)) {
1565 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_LATENCY_RANGE
, r
, 0, NULL
) == 0);
1567 s
->thread_info
.min_latency
= min_latency
;
1568 s
->thread_info
.max_latency
= max_latency
;
1570 s
->monitor_source
->thread_info
.min_latency
= min_latency
;
1571 s
->monitor_source
->thread_info
.max_latency
= max_latency
;
1573 s
->thread_info
.requested_latency_valid
= s
->monitor_source
->thread_info
.requested_latency_valid
= FALSE
;
1577 /* Called from main thread */
1578 void pa_sink_get_latency_range(pa_sink
*s
, pa_usec_t
*min_latency
, pa_usec_t
*max_latency
) {
1579 pa_sink_assert_ref(s
);
1580 pa_assert(min_latency
);
1581 pa_assert(max_latency
);
1583 if (PA_SINK_IS_LINKED(s
->state
)) {
1584 pa_usec_t r
[2] = { 0, 0 };
1586 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_LATENCY_RANGE
, r
, 0, NULL
) == 0);
1588 *min_latency
= r
[0];
1589 *max_latency
= r
[1];
1591 *min_latency
= s
->thread_info
.min_latency
;
1592 *max_latency
= s
->thread_info
.max_latency
;
1596 /* Called from IO thread */
1597 void pa_sink_update_latency_range(pa_sink
*s
, pa_usec_t min_latency
, pa_usec_t max_latency
) {
1601 pa_sink_assert_ref(s
);
1603 s
->thread_info
.min_latency
= min_latency
;
1604 s
->thread_info
.max_latency
= max_latency
;
1606 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)))
1607 if (i
->update_sink_latency_range
)
1608 i
->update_sink_latency_range(i
);
1610 pa_sink_invalidate_requested_latency(s
);
1612 pa_source_update_latency_range(s
->monitor_source
, min_latency
, max_latency
);
1615 size_t pa_sink_get_max_rewind(pa_sink
*s
) {
1617 pa_sink_assert_ref(s
);
1619 if (!PA_SINK_IS_LINKED(s
->state
))
1620 return s
->thread_info
.max_rewind
;
1622 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_MAX_REWIND
, &r
, 0, NULL
) == 0);
1627 size_t pa_sink_get_max_request(pa_sink
*s
) {
1629 pa_sink_assert_ref(s
);
1631 if (!PA_SINK_IS_LINKED(s
->state
))
1632 return s
->thread_info
.max_request
;
1634 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_MAX_REQUEST
, &r
, 0, NULL
) == 0);