2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
31 #include <pulse/introspect.h>
32 #include <pulse/utf8.h>
33 #include <pulse/xmalloc.h>
34 #include <pulse/timeval.h>
35 #include <pulse/util.h>
37 #include <pulsecore/sink-input.h>
38 #include <pulsecore/namereg.h>
39 #include <pulsecore/core-util.h>
40 #include <pulsecore/sample-util.h>
41 #include <pulsecore/core-subscribe.h>
42 #include <pulsecore/log.h>
43 #include <pulsecore/macro.h>
44 #include <pulsecore/play-memblockq.h>
48 #define MAX_MIX_CHANNELS 32
49 #define MIX_BUFFER_LENGTH (PA_PAGE_SIZE)
50 #define DEFAULT_MIN_LATENCY (4*PA_USEC_PER_MSEC)
52 static PA_DEFINE_CHECK_TYPE(pa_sink
, pa_msgobject
);
54 static void sink_free(pa_object
*s
);
56 pa_sink_new_data
* pa_sink_new_data_init(pa_sink_new_data
*data
) {
59 memset(data
, 0, sizeof(*data
));
60 data
->proplist
= pa_proplist_new();
65 void pa_sink_new_data_set_name(pa_sink_new_data
*data
, const char *name
) {
69 data
->name
= pa_xstrdup(name
);
72 void pa_sink_new_data_set_sample_spec(pa_sink_new_data
*data
, const pa_sample_spec
*spec
) {
75 if ((data
->sample_spec_is_set
= !!spec
))
76 data
->sample_spec
= *spec
;
79 void pa_sink_new_data_set_channel_map(pa_sink_new_data
*data
, const pa_channel_map
*map
) {
82 if ((data
->channel_map_is_set
= !!map
))
83 data
->channel_map
= *map
;
86 void pa_sink_new_data_set_volume(pa_sink_new_data
*data
, const pa_cvolume
*volume
) {
89 if ((data
->volume_is_set
= !!volume
))
90 data
->volume
= *volume
;
93 void pa_sink_new_data_set_muted(pa_sink_new_data
*data
, pa_bool_t mute
) {
96 data
->muted_is_set
= TRUE
;
100 void pa_sink_new_data_done(pa_sink_new_data
*data
) {
103 pa_xfree(data
->name
);
104 pa_proplist_free(data
->proplist
);
107 /* Called from main context */
108 static void reset_callbacks(pa_sink
*s
) {
112 s
->get_volume
= NULL
;
113 s
->set_volume
= NULL
;
116 s
->request_rewind
= NULL
;
117 s
->update_requested_latency
= NULL
;
120 /* Called from main context */
121 pa_sink
* pa_sink_new(
123 pa_sink_new_data
*data
,
124 pa_sink_flags_t flags
) {
128 char st
[PA_SAMPLE_SPEC_SNPRINT_MAX
], cm
[PA_CHANNEL_MAP_SNPRINT_MAX
];
129 pa_source_new_data source_data
;
134 pa_assert(data
->name
);
136 s
= pa_msgobject_new(pa_sink
);
138 if (!(name
= pa_namereg_register(core
, data
->name
, PA_NAMEREG_SINK
, s
, data
->namereg_fail
))) {
143 pa_sink_new_data_set_name(data
, name
);
145 if (pa_hook_fire(&core
->hooks
[PA_CORE_HOOK_SINK_NEW
], data
) < 0) {
147 pa_namereg_unregister(core
, name
);
151 pa_return_null_if_fail(!data
->driver
|| pa_utf8_valid(data
->driver
));
152 pa_return_null_if_fail(data
->name
&& pa_utf8_valid(data
->name
) && data
->name
[0]);
154 pa_return_null_if_fail(data
->sample_spec_is_set
&& pa_sample_spec_valid(&data
->sample_spec
));
156 if (!data
->channel_map_is_set
)
157 pa_return_null_if_fail(pa_channel_map_init_auto(&data
->channel_map
, data
->sample_spec
.channels
, PA_CHANNEL_MAP_DEFAULT
));
159 pa_return_null_if_fail(pa_channel_map_valid(&data
->channel_map
));
160 pa_return_null_if_fail(data
->channel_map
.channels
== data
->sample_spec
.channels
);
162 if (!data
->volume_is_set
)
163 pa_cvolume_reset(&data
->volume
, data
->sample_spec
.channels
);
165 pa_return_null_if_fail(pa_cvolume_valid(&data
->volume
));
166 pa_return_null_if_fail(data
->volume
.channels
== data
->sample_spec
.channels
);
168 if (!data
->muted_is_set
)
172 pa_proplist_update(data
->proplist
, PA_UPDATE_MERGE
, data
->card
->proplist
);
174 if (pa_hook_fire(&core
->hooks
[PA_CORE_HOOK_SINK_FIXATE
], data
) < 0) {
176 pa_namereg_unregister(core
, name
);
180 s
->parent
.parent
.free
= sink_free
;
181 s
->parent
.process_msg
= pa_sink_process_msg
;
184 s
->state
= PA_SINK_INIT
;
186 s
->name
= pa_xstrdup(name
);
187 s
->proplist
= pa_proplist_copy(data
->proplist
);
188 s
->driver
= pa_xstrdup(pa_path_get_filename(data
->driver
));
189 s
->module
= data
->module
;
190 s
->card
= data
->card
;
192 s
->sample_spec
= data
->sample_spec
;
193 s
->channel_map
= data
->channel_map
;
195 s
->inputs
= pa_idxset_new(NULL
, NULL
);
198 s
->volume
= data
->volume
;
199 s
->base_volume
= PA_VOLUME_NORM
;
200 s
->virtual_volume
= s
->volume
;
202 s
->muted
= data
->muted
;
203 s
->refresh_volume
= s
->refresh_muted
= FALSE
;
211 pa_silence_memchunk_get(
212 &core
->silence_cache
,
218 s
->thread_info
.inputs
= pa_hashmap_new(pa_idxset_trivial_hash_func
, pa_idxset_trivial_compare_func
);
219 pa_cvolume_reset(&s
->thread_info
.soft_volume
, s
->sample_spec
.channels
);
220 s
->thread_info
.soft_muted
= FALSE
;
221 s
->thread_info
.state
= s
->state
;
222 s
->thread_info
.rewind_nbytes
= 0;
223 s
->thread_info
.rewind_requested
= FALSE
;
224 s
->thread_info
.max_rewind
= 0;
225 s
->thread_info
.max_request
= 0;
226 s
->thread_info
.requested_latency_valid
= FALSE
;
227 s
->thread_info
.requested_latency
= 0;
228 s
->thread_info
.min_latency
= DEFAULT_MIN_LATENCY
;
229 s
->thread_info
.max_latency
= 0;
231 pa_assert_se(pa_idxset_put(core
->sinks
, s
, &s
->index
) >= 0);
234 pa_assert_se(pa_idxset_put(s
->card
->sinks
, s
, NULL
) >= 0);
236 pa_log_info("Created sink %u \"%s\" with sample spec %s and channel map %s",
239 pa_sample_spec_snprint(st
, sizeof(st
), &s
->sample_spec
),
240 pa_channel_map_snprint(cm
, sizeof(cm
), &s
->channel_map
));
242 pa_source_new_data_init(&source_data
);
243 pa_source_new_data_set_sample_spec(&source_data
, &s
->sample_spec
);
244 pa_source_new_data_set_channel_map(&source_data
, &s
->channel_map
);
245 source_data
.name
= pa_sprintf_malloc("%s.monitor", name
);
246 source_data
.driver
= data
->driver
;
247 source_data
.module
= data
->module
;
248 source_data
.card
= data
->card
;
250 dn
= pa_proplist_gets(s
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
251 pa_proplist_setf(source_data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
, "Monitor of %s", dn
? dn
: s
->name
);
252 pa_proplist_sets(source_data
.proplist
, PA_PROP_DEVICE_CLASS
, "monitor");
254 s
->monitor_source
= pa_source_new(core
, &source_data
, PA_SOURCE_LATENCY
);
256 pa_source_new_data_done(&source_data
);
258 if (!s
->monitor_source
) {
264 s
->monitor_source
->monitor_of
= s
;
266 pa_source_set_latency_range(s
->monitor_source
, s
->thread_info
.min_latency
, s
->thread_info
.max_latency
);
267 pa_source_set_max_rewind(s
->monitor_source
, s
->thread_info
.max_rewind
);
272 /* Called from main context */
273 static int sink_set_state(pa_sink
*s
, pa_sink_state_t state
) {
275 pa_bool_t suspend_change
;
276 pa_sink_state_t original_state
;
280 if (s
->state
== state
)
283 original_state
= s
->state
;
286 (original_state
== PA_SINK_SUSPENDED
&& PA_SINK_IS_OPENED(state
)) ||
287 (PA_SINK_IS_OPENED(original_state
) && state
== PA_SINK_SUSPENDED
);
290 if ((ret
= s
->set_state(s
, state
)) < 0)
294 if ((ret
= pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_STATE
, PA_UINT_TO_PTR(state
), 0, NULL
)) < 0) {
297 s
->set_state(s
, original_state
);
304 if (suspend_change
) {
308 /* We're suspending or resuming, tell everyone about it */
310 for (i
= PA_SINK_INPUT(pa_idxset_first(s
->inputs
, &idx
)); i
; i
= PA_SINK_INPUT(pa_idxset_next(s
->inputs
, &idx
)))
312 i
->suspend(i
, state
== PA_SINK_SUSPENDED
);
315 if (state
!= PA_SINK_UNLINKED
) { /* if we enter UNLINKED state pa_sink_unlink() will fire the apropriate events */
316 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_STATE_CHANGED
], s
);
317 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
| PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
323 /* Called from main context */
324 void pa_sink_put(pa_sink
* s
) {
325 pa_sink_assert_ref(s
);
327 pa_assert(s
->state
== PA_SINK_INIT
);
329 /* The following fields must be initialized properly when calling _put() */
330 pa_assert(s
->asyncmsgq
);
331 pa_assert(s
->rtpoll
);
332 pa_assert(!s
->thread_info
.min_latency
|| !s
->thread_info
.max_latency
||
333 s
->thread_info
.min_latency
<= s
->thread_info
.max_latency
);
335 if (!(s
->flags
& PA_SINK_HW_VOLUME_CTRL
)) {
336 s
->flags
|= PA_SINK_DECIBEL_VOLUME
;
338 s
->thread_info
.soft_volume
= s
->volume
;
339 s
->thread_info
.soft_muted
= s
->muted
;
342 pa_assert_se(sink_set_state(s
, PA_SINK_IDLE
) == 0);
344 pa_source_put(s
->monitor_source
);
346 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
| PA_SUBSCRIPTION_EVENT_NEW
, s
->index
);
347 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_PUT
], s
);
350 /* Called from main context */
351 void pa_sink_unlink(pa_sink
* s
) {
353 pa_sink_input
*i
, *j
= NULL
;
357 /* Please note that pa_sink_unlink() does more than simply
358 * reversing pa_sink_put(). It also undoes the registrations
359 * already done in pa_sink_new()! */
361 /* All operations here shall be idempotent, i.e. pa_sink_unlink()
362 * may be called multiple times on the same sink without bad
365 linked
= PA_SINK_IS_LINKED(s
->state
);
368 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_UNLINK
], s
);
370 if (s
->state
!= PA_SINK_UNLINKED
)
371 pa_namereg_unregister(s
->core
, s
->name
);
372 pa_idxset_remove_by_data(s
->core
->sinks
, s
, NULL
);
375 pa_idxset_remove_by_data(s
->card
->sinks
, s
, NULL
);
377 while ((i
= pa_idxset_first(s
->inputs
, NULL
))) {
379 pa_sink_input_kill(i
);
384 sink_set_state(s
, PA_SINK_UNLINKED
);
386 s
->state
= PA_SINK_UNLINKED
;
390 if (s
->monitor_source
)
391 pa_source_unlink(s
->monitor_source
);
394 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
| PA_SUBSCRIPTION_EVENT_REMOVE
, s
->index
);
395 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_UNLINK_POST
], s
);
399 /* Called from main context */
400 static void sink_free(pa_object
*o
) {
401 pa_sink
*s
= PA_SINK(o
);
405 pa_assert(pa_sink_refcnt(s
) == 0);
407 if (PA_SINK_IS_LINKED(s
->state
))
410 pa_log_info("Freeing sink %u \"%s\"", s
->index
, s
->name
);
412 if (s
->monitor_source
) {
413 pa_source_unref(s
->monitor_source
);
414 s
->monitor_source
= NULL
;
417 pa_idxset_free(s
->inputs
, NULL
, NULL
);
419 while ((i
= pa_hashmap_steal_first(s
->thread_info
.inputs
)))
420 pa_sink_input_unref(i
);
422 pa_hashmap_free(s
->thread_info
.inputs
, NULL
, NULL
);
424 if (s
->silence
.memblock
)
425 pa_memblock_unref(s
->silence
.memblock
);
431 pa_proplist_free(s
->proplist
);
436 /* Called from main context */
437 void pa_sink_set_asyncmsgq(pa_sink
*s
, pa_asyncmsgq
*q
) {
438 pa_sink_assert_ref(s
);
442 if (s
->monitor_source
)
443 pa_source_set_asyncmsgq(s
->monitor_source
, q
);
446 /* Called from main context */
447 void pa_sink_set_rtpoll(pa_sink
*s
, pa_rtpoll
*p
) {
448 pa_sink_assert_ref(s
);
451 if (s
->monitor_source
)
452 pa_source_set_rtpoll(s
->monitor_source
, p
);
455 /* Called from main context */
456 int pa_sink_update_status(pa_sink
*s
) {
457 pa_sink_assert_ref(s
);
458 pa_assert(PA_SINK_IS_LINKED(s
->state
));
460 if (s
->state
== PA_SINK_SUSPENDED
)
463 return sink_set_state(s
, pa_sink_used_by(s
) ? PA_SINK_RUNNING
: PA_SINK_IDLE
);
466 /* Called from main context */
467 int pa_sink_suspend(pa_sink
*s
, pa_bool_t suspend
) {
468 pa_sink_assert_ref(s
);
469 pa_assert(PA_SINK_IS_LINKED(s
->state
));
472 return sink_set_state(s
, PA_SINK_SUSPENDED
);
474 return sink_set_state(s
, pa_sink_used_by(s
) ? PA_SINK_RUNNING
: PA_SINK_IDLE
);
477 /* Called from main context */
478 pa_queue
*pa_sink_move_all_start(pa_sink
*s
) {
480 pa_sink_input
*i
, *n
;
483 pa_sink_assert_ref(s
);
484 pa_assert(PA_SINK_IS_LINKED(s
->state
));
488 for (i
= PA_SINK_INPUT(pa_idxset_first(s
->inputs
, &idx
)); i
; i
= n
) {
489 n
= PA_SINK_INPUT(pa_idxset_next(s
->inputs
, &idx
));
491 if (pa_sink_input_start_move(i
) >= 0)
492 pa_queue_push(q
, pa_sink_input_ref(i
));
498 /* Called from main context */
499 void pa_sink_move_all_finish(pa_sink
*s
, pa_queue
*q
) {
502 pa_sink_assert_ref(s
);
503 pa_assert(PA_SINK_IS_LINKED(s
->state
));
506 while ((i
= PA_SINK_INPUT(pa_queue_pop(q
)))) {
507 if (pa_sink_input_finish_move(i
, s
) < 0)
508 pa_sink_input_unlink(i
);
510 pa_sink_input_unref(i
);
513 pa_queue_free(q
, NULL
, NULL
);
516 /* Called from main context */
517 void pa_sink_move_all_fail(pa_queue
*q
) {
521 while ((i
= PA_SINK_INPUT(pa_queue_pop(q
)))) {
522 pa_sink_input_unlink(i
);
523 pa_sink_input_unref(i
);
526 pa_queue_free(q
, NULL
, NULL
);
529 /* Called from IO thread context */
530 void pa_sink_process_rewind(pa_sink
*s
, size_t nbytes
) {
533 pa_sink_assert_ref(s
);
534 pa_assert(PA_SINK_IS_LINKED(s
->thread_info
.state
));
536 s
->thread_info
.rewind_nbytes
= 0;
537 s
->thread_info
.rewind_requested
= FALSE
;
540 pa_log_debug("Processing rewind...");
542 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
))) {
543 pa_sink_input_assert_ref(i
);
544 pa_sink_input_process_rewind(i
, nbytes
);
548 if (s
->monitor_source
&& PA_SOURCE_IS_OPENED(s
->monitor_source
->thread_info
.state
))
549 pa_source_process_rewind(s
->monitor_source
, nbytes
);
552 /* Called from IO thread context */
553 static unsigned fill_mix_info(pa_sink
*s
, size_t *length
, pa_mix_info
*info
, unsigned maxinfo
) {
557 size_t mixlength
= *length
;
559 pa_sink_assert_ref(s
);
562 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)) && maxinfo
> 0) {
563 pa_sink_input_assert_ref(i
);
565 if (pa_sink_input_peek(i
, *length
, &info
->chunk
, &info
->volume
) < 0)
568 if (mixlength
== 0 || info
->chunk
.length
< mixlength
)
569 mixlength
= info
->chunk
.length
;
571 if (pa_memblock_is_silence(info
->chunk
.memblock
)) {
572 pa_memblock_unref(info
->chunk
.memblock
);
576 info
->userdata
= pa_sink_input_ref(i
);
578 pa_assert(info
->chunk
.memblock
);
579 pa_assert(info
->chunk
.length
> 0);
592 /* Called from IO thread context */
593 static void inputs_drop(pa_sink
*s
, pa_mix_info
*info
, unsigned n
, pa_memchunk
*result
) {
597 unsigned n_unreffed
= 0;
599 pa_sink_assert_ref(s
);
601 pa_assert(result
->memblock
);
602 pa_assert(result
->length
> 0);
604 /* We optimize for the case where the order of the inputs has not changed */
606 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
))) {
608 pa_mix_info
* m
= NULL
;
610 pa_sink_input_assert_ref(i
);
612 /* Let's try to find the matching entry info the pa_mix_info array */
613 for (j
= 0; j
< n
; j
++) {
615 if (info
[p
].userdata
== i
) {
626 pa_sink_input_drop(i
, result
->length
);
628 if (s
->monitor_source
&& PA_SOURCE_IS_OPENED(pa_source_get_state(s
->monitor_source
))) {
630 if (pa_hashmap_size(i
->thread_info
.direct_outputs
) > 0) {
635 if (m
&& m
->chunk
.memblock
) {
637 pa_memblock_ref(c
.memblock
);
638 pa_assert(result
->length
<= c
.length
);
639 c
.length
= result
->length
;
641 pa_memchunk_make_writable(&c
, 0);
642 pa_volume_memchunk(&c
, &s
->sample_spec
, &m
->volume
);
645 pa_memblock_ref(c
.memblock
);
646 pa_assert(result
->length
<= c
.length
);
647 c
.length
= result
->length
;
650 while ((o
= pa_hashmap_iterate(i
->thread_info
.direct_outputs
, &ostate
, NULL
))) {
651 pa_source_output_assert_ref(o
);
652 pa_assert(o
->direct_on_input
== i
);
653 pa_source_post_direct(s
->monitor_source
, o
, &c
);
656 pa_memblock_unref(c
.memblock
);
661 if (m
->chunk
.memblock
)
662 pa_memblock_unref(m
->chunk
.memblock
);
663 pa_memchunk_reset(&m
->chunk
);
665 pa_sink_input_unref(m
->userdata
);
672 /* Now drop references to entries that are included in the
673 * pa_mix_info array but don't exist anymore */
675 if (n_unreffed
< n
) {
676 for (; n
> 0; info
++, n
--) {
678 pa_sink_input_unref(info
->userdata
);
679 if (info
->chunk
.memblock
)
680 pa_memblock_unref(info
->chunk
.memblock
);
684 if (s
->monitor_source
&& PA_SOURCE_IS_OPENED(pa_source_get_state(s
->monitor_source
)))
685 pa_source_post(s
->monitor_source
, result
);
688 /* Called from IO thread context */
689 void pa_sink_render(pa_sink
*s
, size_t length
, pa_memchunk
*result
) {
690 pa_mix_info info
[MAX_MIX_CHANNELS
];
692 size_t block_size_max
;
694 pa_sink_assert_ref(s
);
695 pa_assert(PA_SINK_IS_OPENED(s
->thread_info
.state
));
696 pa_assert(pa_frame_aligned(length
, &s
->sample_spec
));
701 pa_assert(!s
->thread_info
.rewind_requested
);
702 pa_assert(s
->thread_info
.rewind_nbytes
== 0);
705 length
= pa_frame_align(MIX_BUFFER_LENGTH
, &s
->sample_spec
);
707 block_size_max
= pa_mempool_block_size_max(s
->core
->mempool
);
708 if (length
> block_size_max
)
709 length
= pa_frame_align(block_size_max
, &s
->sample_spec
);
711 pa_assert(length
> 0);
713 n
= fill_mix_info(s
, &length
, info
, MAX_MIX_CHANNELS
);
717 *result
= s
->silence
;
718 pa_memblock_ref(result
->memblock
);
720 if (result
->length
> length
)
721 result
->length
= length
;
726 *result
= info
[0].chunk
;
727 pa_memblock_ref(result
->memblock
);
729 if (result
->length
> length
)
730 result
->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_norm(&volume
)) {
735 pa_memchunk_make_writable(result
, 0);
736 if (s
->thread_info
.soft_muted
|| pa_cvolume_is_muted(&volume
))
737 pa_silence_memchunk(result
, &s
->sample_spec
);
739 pa_volume_memchunk(result
, &s
->sample_spec
, &volume
);
743 result
->memblock
= pa_memblock_new(s
->core
->mempool
, length
);
745 ptr
= pa_memblock_acquire(result
->memblock
);
746 result
->length
= pa_mix(info
, n
,
749 &s
->thread_info
.soft_volume
,
750 s
->thread_info
.soft_muted
);
751 pa_memblock_release(result
->memblock
);
756 inputs_drop(s
, info
, n
, result
);
761 /* Called from IO thread context */
762 void pa_sink_render_into(pa_sink
*s
, pa_memchunk
*target
) {
763 pa_mix_info info
[MAX_MIX_CHANNELS
];
765 size_t length
, block_size_max
;
767 pa_sink_assert_ref(s
);
768 pa_assert(PA_SINK_IS_OPENED(s
->thread_info
.state
));
770 pa_assert(target
->memblock
);
771 pa_assert(target
->length
> 0);
772 pa_assert(pa_frame_aligned(target
->length
, &s
->sample_spec
));
776 pa_assert(!s
->thread_info
.rewind_requested
);
777 pa_assert(s
->thread_info
.rewind_nbytes
== 0);
779 length
= target
->length
;
780 block_size_max
= pa_mempool_block_size_max(s
->core
->mempool
);
781 if (length
> block_size_max
)
782 length
= pa_frame_align(block_size_max
, &s
->sample_spec
);
784 pa_assert(length
> 0);
786 n
= fill_mix_info(s
, &length
, info
, MAX_MIX_CHANNELS
);
789 if (target
->length
> length
)
790 target
->length
= length
;
792 pa_silence_memchunk(target
, &s
->sample_spec
);
796 if (target
->length
> length
)
797 target
->length
= length
;
799 pa_sw_cvolume_multiply(&volume
, &s
->thread_info
.soft_volume
, &info
[0].volume
);
801 if (s
->thread_info
.soft_muted
|| pa_cvolume_is_muted(&volume
))
802 pa_silence_memchunk(target
, &s
->sample_spec
);
806 vchunk
= info
[0].chunk
;
807 pa_memblock_ref(vchunk
.memblock
);
809 if (vchunk
.length
> length
)
810 vchunk
.length
= length
;
812 if (!pa_cvolume_is_norm(&volume
)) {
813 pa_memchunk_make_writable(&vchunk
, 0);
814 pa_volume_memchunk(&vchunk
, &s
->sample_spec
, &volume
);
817 pa_memchunk_memcpy(target
, &vchunk
);
818 pa_memblock_unref(vchunk
.memblock
);
824 ptr
= pa_memblock_acquire(target
->memblock
);
826 target
->length
= pa_mix(info
, n
,
827 (uint8_t*) ptr
+ target
->index
, length
,
829 &s
->thread_info
.soft_volume
,
830 s
->thread_info
.soft_muted
);
832 pa_memblock_release(target
->memblock
);
835 inputs_drop(s
, info
, n
, target
);
840 /* Called from IO thread context */
841 void pa_sink_render_into_full(pa_sink
*s
, pa_memchunk
*target
) {
845 pa_sink_assert_ref(s
);
846 pa_assert(PA_SINK_IS_OPENED(s
->thread_info
.state
));
848 pa_assert(target
->memblock
);
849 pa_assert(target
->length
> 0);
850 pa_assert(pa_frame_aligned(target
->length
, &s
->sample_spec
));
854 pa_assert(!s
->thread_info
.rewind_requested
);
855 pa_assert(s
->thread_info
.rewind_nbytes
== 0);
864 pa_sink_render_into(s
, &chunk
);
873 /* Called from IO thread context */
874 void pa_sink_render_full(pa_sink
*s
, size_t length
, pa_memchunk
*result
) {
875 pa_sink_assert_ref(s
);
876 pa_assert(PA_SINK_IS_OPENED(s
->thread_info
.state
));
877 pa_assert(length
> 0);
878 pa_assert(pa_frame_aligned(length
, &s
->sample_spec
));
881 pa_assert(!s
->thread_info
.rewind_requested
);
882 pa_assert(s
->thread_info
.rewind_nbytes
== 0);
884 /*** This needs optimization ***/
887 result
->length
= length
;
888 result
->memblock
= pa_memblock_new(s
->core
->mempool
, length
);
890 pa_sink_render_into_full(s
, result
);
893 /* Called from main thread */
894 pa_usec_t
pa_sink_get_latency(pa_sink
*s
) {
897 pa_sink_assert_ref(s
);
898 pa_assert(PA_SINK_IS_LINKED(s
->state
));
900 /* The returned value is supposed to be in the time domain of the sound card! */
902 if (!PA_SINK_IS_OPENED(s
->state
))
905 if (!(s
->flags
& PA_SINK_LATENCY
))
908 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_LATENCY
, &usec
, 0, NULL
) == 0);
913 /* Called from main thread */
914 void pa_sink_set_volume(pa_sink
*s
, const pa_cvolume
*volume
) {
916 pa_sink_set_volume_data data
;
918 pa_sink_assert_ref(s
);
919 pa_assert(PA_SINK_IS_LINKED(s
->state
));
921 pa_assert(pa_cvolume_valid(volume
));
922 pa_assert(pa_cvolume_compatible(volume
, &s
->sample_spec
));
925 data
.virtual_volume
= data
.volume
= *volume
;
927 changed
= !pa_cvolume_equal(&data
.virtual_volume
, &s
->virtual_volume
) ||
928 !pa_cvolume_equal(&data
.volume
, &s
->volume
);
931 if (pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_SET_VOLUME
], &data
) < 0)
934 changed
= !pa_cvolume_equal(&data
.virtual_volume
, &s
->virtual_volume
); /* from client-side view */
937 s
->volume
= data
.volume
;
938 s
->virtual_volume
= data
.virtual_volume
;
940 if (s
->set_volume
&& s
->set_volume(s
) < 0)
941 s
->set_volume
= NULL
;
944 pa_sink_set_soft_volume(s
, &s
->volume
);
947 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
950 /* Called from main thread */
951 void pa_sink_set_soft_volume(pa_sink
*s
, const pa_cvolume
*volume
) {
952 pa_sink_assert_ref(s
);
955 if (PA_SINK_IS_LINKED(s
->state
))
956 pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_VOLUME
, volume
, 0, NULL
);
958 s
->thread_info
.soft_volume
= *volume
;
961 /* Called from main thread */
962 const pa_cvolume
*pa_sink_get_volume(pa_sink
*s
, pa_bool_t force_refresh
) {
963 pa_sink_assert_ref(s
);
964 pa_assert(PA_SINK_IS_LINKED(s
->state
));
966 if (s
->refresh_volume
|| force_refresh
) {
967 struct pa_cvolume old_volume
= s
->virtual_volume
;
969 if (s
->get_volume
&& s
->get_volume(s
) < 0)
970 s
->get_volume
= NULL
;
972 if (!s
->get_volume
) {
973 pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_VOLUME
, &s
->volume
, 0, NULL
);
974 s
->virtual_volume
= s
->volume
;
977 if (!pa_cvolume_equal(&old_volume
, &s
->virtual_volume
))
978 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
981 return &s
->virtual_volume
;
984 /* Called from main thread */
985 void pa_sink_set_mute(pa_sink
*s
, pa_bool_t mute
) {
988 pa_sink_assert_ref(s
);
989 pa_assert(PA_SINK_IS_LINKED(s
->state
));
991 changed
= s
->muted
!= mute
;
994 if (s
->set_mute
&& s
->set_mute(s
) < 0)
998 pa_asyncmsgq_post(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_MUTE
, PA_UINT_TO_PTR(mute
), 0, NULL
, NULL
);
1001 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
1004 /* Called from main thread */
1005 pa_bool_t
pa_sink_get_mute(pa_sink
*s
, pa_bool_t force_refresh
) {
1007 pa_sink_assert_ref(s
);
1008 pa_assert(PA_SINK_IS_LINKED(s
->state
));
1010 if (s
->refresh_muted
|| force_refresh
) {
1011 pa_bool_t old_muted
= s
->muted
;
1013 if (s
->get_mute
&& s
->get_mute(s
) < 0)
1017 pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_MUTE
, &s
->muted
, 0, NULL
);
1019 if (old_muted
!= s
->muted
)
1020 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
1026 /* Called from main thread */
1027 pa_bool_t
pa_sink_update_proplist(pa_sink
*s
, pa_update_mode_t mode
, pa_proplist
*p
) {
1029 pa_sink_assert_ref(s
);
1031 pa_proplist_update(s
->proplist
, mode
, p
);
1033 if (PA_SINK_IS_LINKED(s
->state
)) {
1034 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_PROPLIST_CHANGED
], s
);
1035 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
1041 /* Called from main thread */
1042 void pa_sink_set_description(pa_sink
*s
, const char *description
) {
1044 pa_sink_assert_ref(s
);
1046 if (!description
&& !pa_proplist_contains(s
->proplist
, PA_PROP_DEVICE_DESCRIPTION
))
1049 old
= pa_proplist_gets(s
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
1051 if (old
&& description
&& !strcmp(old
, description
))
1055 pa_proplist_sets(s
->proplist
, PA_PROP_DEVICE_DESCRIPTION
, description
);
1057 pa_proplist_unset(s
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
1059 if (s
->monitor_source
) {
1062 n
= pa_sprintf_malloc("Monitor Source of %s", description
? description
: s
->name
);
1063 pa_source_set_description(s
->monitor_source
, n
);
1067 if (PA_SINK_IS_LINKED(s
->state
)) {
1068 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
1069 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_PROPLIST_CHANGED
], s
);
1073 /* Called from main thread */
1074 unsigned pa_sink_linked_by(pa_sink
*s
) {
1077 pa_sink_assert_ref(s
);
1078 pa_assert(PA_SINK_IS_LINKED(s
->state
));
1080 ret
= pa_idxset_size(s
->inputs
);
1082 /* We add in the number of streams connected to us here. Please
1083 * note the asymmmetry to pa_sink_used_by()! */
1085 if (s
->monitor_source
)
1086 ret
+= pa_source_linked_by(s
->monitor_source
);
1091 /* Called from main thread */
1092 unsigned pa_sink_used_by(pa_sink
*s
) {
1095 pa_sink_assert_ref(s
);
1096 pa_assert(PA_SINK_IS_LINKED(s
->state
));
1098 ret
= pa_idxset_size(s
->inputs
);
1099 pa_assert(ret
>= s
->n_corked
);
1101 /* Streams connected to our monitor source do not matter for
1102 * pa_sink_used_by()!.*/
1104 return ret
- s
->n_corked
;
1107 /* Called from main thread */
1108 unsigned pa_sink_check_suspend(pa_sink
*s
) {
1113 pa_sink_assert_ref(s
);
1115 if (!PA_SINK_IS_LINKED(s
->state
))
1120 for (i
= PA_SINK_INPUT(pa_idxset_first(s
->inputs
, &idx
)); i
; i
= PA_SINK_INPUT(pa_idxset_next(s
->inputs
, &idx
))) {
1121 pa_sink_input_state_t st
;
1123 st
= pa_sink_input_get_state(i
);
1124 pa_assert(PA_SINK_INPUT_IS_LINKED(st
));
1126 if (st
== PA_SINK_INPUT_CORKED
)
1129 if (i
->flags
& PA_SINK_INPUT_DONT_INHIBIT_AUTO_SUSPEND
)
1135 if (s
->monitor_source
)
1136 ret
+= pa_source_check_suspend(s
->monitor_source
);
1141 /* Called from IO thread, except when it is not */
1142 int pa_sink_process_msg(pa_msgobject
*o
, int code
, void *userdata
, int64_t offset
, pa_memchunk
*chunk
) {
1143 pa_sink
*s
= PA_SINK(o
);
1144 pa_sink_assert_ref(s
);
1146 switch ((pa_sink_message_t
) code
) {
1148 case PA_SINK_MESSAGE_ADD_INPUT
: {
1149 pa_sink_input
*i
= PA_SINK_INPUT(userdata
);
1151 /* If you change anything here, make sure to change the
1152 * sink input handling a few lines down at
1153 * PA_SINK_MESSAGE_FINISH_MOVE, too. */
1155 pa_hashmap_put(s
->thread_info
.inputs
, PA_UINT32_TO_PTR(i
->index
), pa_sink_input_ref(i
));
1157 /* Since the caller sleeps in pa_sink_input_put(), we can
1158 * safely access data outside of thread_info even though
1161 if ((i
->thread_info
.sync_prev
= i
->sync_prev
)) {
1162 pa_assert(i
->sink
== i
->thread_info
.sync_prev
->sink
);
1163 pa_assert(i
->sync_prev
->sync_next
== i
);
1164 i
->thread_info
.sync_prev
->thread_info
.sync_next
= i
;
1167 if ((i
->thread_info
.sync_next
= i
->sync_next
)) {
1168 pa_assert(i
->sink
== i
->thread_info
.sync_next
->sink
);
1169 pa_assert(i
->sync_next
->sync_prev
== i
);
1170 i
->thread_info
.sync_next
->thread_info
.sync_prev
= i
;
1173 pa_assert(!i
->thread_info
.attached
);
1174 i
->thread_info
.attached
= TRUE
;
1179 pa_sink_input_set_state_within_thread(i
, i
->state
);
1181 /* The requested latency of the sink input needs to be
1182 * fixed up and then configured on the sink */
1184 if (i
->thread_info
.requested_sink_latency
!= (pa_usec_t
) -1)
1185 pa_sink_input_set_requested_latency_within_thread(i
, i
->thread_info
.requested_sink_latency
);
1187 pa_sink_input_update_max_rewind(i
, s
->thread_info
.max_rewind
);
1188 pa_sink_input_update_max_request(i
, s
->thread_info
.max_request
);
1190 /* We don't rewind here automatically. This is left to the
1191 * sink input implementor because some sink inputs need a
1192 * slow start, i.e. need some time to buffer client
1193 * samples before beginning streaming. */
1198 case PA_SINK_MESSAGE_REMOVE_INPUT
: {
1199 pa_sink_input
*i
= PA_SINK_INPUT(userdata
);
1201 /* If you change anything here, make sure to change the
1202 * sink input handling a few lines down at
1203 * PA_SINK_MESSAGE_PREPAPRE_MOVE, too. */
1208 pa_sink_input_set_state_within_thread(i
, i
->state
);
1210 pa_assert(i
->thread_info
.attached
);
1211 i
->thread_info
.attached
= FALSE
;
1213 /* Since the caller sleeps in pa_sink_input_unlink(),
1214 * we can safely access data outside of thread_info even
1215 * though it is mutable */
1217 pa_assert(!i
->sync_prev
);
1218 pa_assert(!i
->sync_next
);
1220 if (i
->thread_info
.sync_prev
) {
1221 i
->thread_info
.sync_prev
->thread_info
.sync_next
= i
->thread_info
.sync_prev
->sync_next
;
1222 i
->thread_info
.sync_prev
= NULL
;
1225 if (i
->thread_info
.sync_next
) {
1226 i
->thread_info
.sync_next
->thread_info
.sync_prev
= i
->thread_info
.sync_next
->sync_prev
;
1227 i
->thread_info
.sync_next
= NULL
;
1230 if (pa_hashmap_remove(s
->thread_info
.inputs
, PA_UINT32_TO_PTR(i
->index
)))
1231 pa_sink_input_unref(i
);
1233 pa_sink_invalidate_requested_latency(s
);
1234 pa_sink_request_rewind(s
, (size_t) -1);
1239 case PA_SINK_MESSAGE_START_MOVE
: {
1240 pa_sink_input
*i
= PA_SINK_INPUT(userdata
);
1242 /* We don't support moving synchronized streams. */
1243 pa_assert(!i
->sync_prev
);
1244 pa_assert(!i
->sync_next
);
1245 pa_assert(!i
->thread_info
.sync_next
);
1246 pa_assert(!i
->thread_info
.sync_prev
);
1248 if (i
->thread_info
.state
!= PA_SINK_INPUT_CORKED
) {
1250 size_t sink_nbytes
, total_nbytes
;
1252 /* Get the latency of the sink */
1253 if (!(s
->flags
& PA_SINK_LATENCY
) ||
1254 PA_MSGOBJECT(s
)->process_msg(PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_LATENCY
, &usec
, 0, NULL
) < 0)
1257 sink_nbytes
= pa_usec_to_bytes(usec
, &s
->sample_spec
);
1258 total_nbytes
= sink_nbytes
+ pa_memblockq_get_length(i
->thread_info
.render_memblockq
);
1260 if (total_nbytes
> 0) {
1261 i
->thread_info
.rewrite_nbytes
= i
->thread_info
.resampler
? pa_resampler_request(i
->thread_info
.resampler
, total_nbytes
) : total_nbytes
;
1262 i
->thread_info
.rewrite_flush
= TRUE
;
1263 pa_sink_input_process_rewind(i
, sink_nbytes
);
1270 pa_assert(i
->thread_info
.attached
);
1271 i
->thread_info
.attached
= FALSE
;
1273 /* Let's remove the sink input ...*/
1274 if (pa_hashmap_remove(s
->thread_info
.inputs
, PA_UINT32_TO_PTR(i
->index
)))
1275 pa_sink_input_unref(i
);
1277 pa_sink_invalidate_requested_latency(s
);
1279 pa_log_debug("Requesting rewind due to started move");
1280 pa_sink_request_rewind(s
, (size_t) -1);
1285 case PA_SINK_MESSAGE_FINISH_MOVE
: {
1286 pa_sink_input
*i
= PA_SINK_INPUT(userdata
);
1288 /* We don't support moving synchronized streams. */
1289 pa_assert(!i
->sync_prev
);
1290 pa_assert(!i
->sync_next
);
1291 pa_assert(!i
->thread_info
.sync_next
);
1292 pa_assert(!i
->thread_info
.sync_prev
);
1294 pa_hashmap_put(s
->thread_info
.inputs
, PA_UINT32_TO_PTR(i
->index
), pa_sink_input_ref(i
));
1296 pa_assert(!i
->thread_info
.attached
);
1297 i
->thread_info
.attached
= TRUE
;
1302 if (i
->thread_info
.requested_sink_latency
!= (pa_usec_t
) -1)
1303 pa_sink_input_set_requested_latency_within_thread(i
, i
->thread_info
.requested_sink_latency
);
1305 pa_sink_input_update_max_rewind(i
, s
->thread_info
.max_rewind
);
1306 pa_sink_input_update_max_request(i
, s
->thread_info
.max_request
);
1308 if (i
->thread_info
.state
!= PA_SINK_INPUT_CORKED
) {
1312 /* Get the latency of the sink */
1313 if (!(s
->flags
& PA_SINK_LATENCY
) ||
1314 PA_MSGOBJECT(s
)->process_msg(PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_LATENCY
, &usec
, 0, NULL
) < 0)
1317 nbytes
= pa_usec_to_bytes(usec
, &s
->sample_spec
);
1320 pa_sink_input_drop(i
, nbytes
);
1322 pa_log_debug("Requesting rewind due to finished move");
1323 pa_sink_request_rewind(s
, nbytes
);
1329 case PA_SINK_MESSAGE_SET_VOLUME
:
1330 s
->thread_info
.soft_volume
= *((pa_cvolume
*) userdata
);
1332 pa_sink_request_rewind(s
, (size_t) -1);
1335 case PA_SINK_MESSAGE_SET_MUTE
:
1336 s
->thread_info
.soft_muted
= PA_PTR_TO_UINT(userdata
);
1338 pa_sink_request_rewind(s
, (size_t) -1);
1341 case PA_SINK_MESSAGE_GET_VOLUME
:
1342 *((pa_cvolume
*) userdata
) = s
->thread_info
.soft_volume
;
1345 case PA_SINK_MESSAGE_GET_MUTE
:
1346 *((pa_bool_t
*) userdata
) = s
->thread_info
.soft_muted
;
1349 case PA_SINK_MESSAGE_SET_STATE
:
1351 s
->thread_info
.state
= PA_PTR_TO_UINT(userdata
);
1354 case PA_SINK_MESSAGE_DETACH
:
1356 /* Detach all streams */
1357 pa_sink_detach_within_thread(s
);
1360 case PA_SINK_MESSAGE_ATTACH
:
1362 /* Reattach all streams */
1363 pa_sink_attach_within_thread(s
);
1366 case PA_SINK_MESSAGE_GET_REQUESTED_LATENCY
: {
1368 pa_usec_t
*usec
= userdata
;
1369 *usec
= pa_sink_get_requested_latency_within_thread(s
);
1371 if (*usec
== (pa_usec_t
) -1)
1372 *usec
= s
->thread_info
.max_latency
;
1377 case PA_SINK_MESSAGE_SET_LATENCY_RANGE
: {
1378 pa_usec_t
*r
= userdata
;
1380 pa_sink_update_latency_range(s
, r
[0], r
[1]);
1385 case PA_SINK_MESSAGE_GET_LATENCY_RANGE
: {
1386 pa_usec_t
*r
= userdata
;
1388 r
[0] = s
->thread_info
.min_latency
;
1389 r
[1] = s
->thread_info
.max_latency
;
1394 case PA_SINK_MESSAGE_GET_MAX_REWIND
:
1396 *((size_t*) userdata
) = s
->thread_info
.max_rewind
;
1399 case PA_SINK_MESSAGE_GET_MAX_REQUEST
:
1401 *((size_t*) userdata
) = s
->thread_info
.max_request
;
1404 case PA_SINK_MESSAGE_GET_LATENCY
:
1405 case PA_SINK_MESSAGE_MAX
:
1412 /* Called from main thread */
1413 int pa_sink_suspend_all(pa_core
*c
, pa_bool_t suspend
) {
1418 pa_core_assert_ref(c
);
1420 for (sink
= PA_SINK(pa_idxset_first(c
->sinks
, &idx
)); sink
; sink
= PA_SINK(pa_idxset_next(c
->sinks
, &idx
)))
1421 ret
-= pa_sink_suspend(sink
, suspend
) < 0;
1426 /* Called from main thread */
1427 void pa_sink_detach(pa_sink
*s
) {
1428 pa_sink_assert_ref(s
);
1429 pa_assert(PA_SINK_IS_LINKED(s
->state
));
1431 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_DETACH
, NULL
, 0, NULL
) == 0);
1434 /* Called from main thread */
1435 void pa_sink_attach(pa_sink
*s
) {
1436 pa_sink_assert_ref(s
);
1437 pa_assert(PA_SINK_IS_LINKED(s
->state
));
1439 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_ATTACH
, NULL
, 0, NULL
) == 0);
1442 /* Called from IO thread */
1443 void pa_sink_detach_within_thread(pa_sink
*s
) {
1447 pa_sink_assert_ref(s
);
1448 pa_assert(PA_SINK_IS_LINKED(s
->thread_info
.state
));
1450 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)))
1454 if (s
->monitor_source
)
1455 pa_source_detach_within_thread(s
->monitor_source
);
1458 /* Called from IO thread */
1459 void pa_sink_attach_within_thread(pa_sink
*s
) {
1463 pa_sink_assert_ref(s
);
1464 pa_assert(PA_SINK_IS_LINKED(s
->thread_info
.state
));
1466 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)))
1470 if (s
->monitor_source
)
1471 pa_source_attach_within_thread(s
->monitor_source
);
1474 /* Called from IO thread */
1475 void pa_sink_request_rewind(pa_sink
*s
, size_t nbytes
) {
1476 pa_sink_assert_ref(s
);
1477 pa_assert(PA_SINK_IS_LINKED(s
->thread_info
.state
));
1479 if (nbytes
== (size_t) -1)
1480 nbytes
= s
->thread_info
.max_rewind
;
1482 nbytes
= PA_MIN(nbytes
, s
->thread_info
.max_rewind
);
1484 if (s
->thread_info
.rewind_requested
&&
1485 nbytes
<= s
->thread_info
.rewind_nbytes
)
1488 s
->thread_info
.rewind_nbytes
= nbytes
;
1489 s
->thread_info
.rewind_requested
= TRUE
;
1491 if (s
->request_rewind
)
1492 s
->request_rewind(s
);
1495 /* Called from IO thread */
1496 pa_usec_t
pa_sink_get_requested_latency_within_thread(pa_sink
*s
) {
1497 pa_usec_t result
= (pa_usec_t
) -1;
1500 pa_usec_t monitor_latency
;
1502 pa_sink_assert_ref(s
);
1504 if (s
->thread_info
.requested_latency_valid
)
1505 return s
->thread_info
.requested_latency
;
1507 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)))
1509 if (i
->thread_info
.requested_sink_latency
!= (pa_usec_t
) -1 &&
1510 (result
== (pa_usec_t
) -1 || result
> i
->thread_info
.requested_sink_latency
))
1511 result
= i
->thread_info
.requested_sink_latency
;
1513 monitor_latency
= pa_source_get_requested_latency_within_thread(s
->monitor_source
);
1515 if (monitor_latency
!= (pa_usec_t
) -1 &&
1516 (result
== (pa_usec_t
) -1 || result
> monitor_latency
))
1517 result
= monitor_latency
;
1519 if (result
!= (pa_usec_t
) -1) {
1520 if (s
->thread_info
.max_latency
> 0 && result
> s
->thread_info
.max_latency
)
1521 result
= s
->thread_info
.max_latency
;
1523 if (s
->thread_info
.min_latency
> 0 && result
< s
->thread_info
.min_latency
)
1524 result
= s
->thread_info
.min_latency
;
1527 s
->thread_info
.requested_latency
= result
;
1528 s
->thread_info
.requested_latency_valid
= TRUE
;
1533 /* Called from main thread */
1534 pa_usec_t
pa_sink_get_requested_latency(pa_sink
*s
) {
1537 pa_sink_assert_ref(s
);
1538 pa_assert(PA_SINK_IS_LINKED(s
->state
));
1540 if (!PA_SINK_IS_OPENED(s
->state
))
1543 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_REQUESTED_LATENCY
, &usec
, 0, NULL
) == 0);
1547 /* Called from IO thread */
1548 void pa_sink_set_max_rewind(pa_sink
*s
, size_t max_rewind
) {
1552 pa_sink_assert_ref(s
);
1554 if (max_rewind
== s
->thread_info
.max_rewind
)
1557 s
->thread_info
.max_rewind
= max_rewind
;
1559 if (PA_SINK_IS_LINKED(s
->thread_info
.state
)) {
1560 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)))
1561 pa_sink_input_update_max_rewind(i
, s
->thread_info
.max_rewind
);
1564 if (s
->monitor_source
)
1565 pa_source_set_max_rewind(s
->monitor_source
, s
->thread_info
.max_rewind
);
1568 /* Called from IO thread */
1569 void pa_sink_set_max_request(pa_sink
*s
, size_t max_request
) {
1572 pa_sink_assert_ref(s
);
1574 if (max_request
== s
->thread_info
.max_request
)
1577 s
->thread_info
.max_request
= max_request
;
1579 if (PA_SINK_IS_LINKED(s
->thread_info
.state
)) {
1582 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)))
1583 pa_sink_input_update_max_request(i
, s
->thread_info
.max_request
);
1587 /* Called from IO thread */
1588 void pa_sink_invalidate_requested_latency(pa_sink
*s
) {
1592 pa_sink_assert_ref(s
);
1594 s
->thread_info
.requested_latency_valid
= FALSE
;
1596 if (s
->update_requested_latency
)
1597 s
->update_requested_latency(s
);
1599 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)))
1600 if (i
->update_sink_requested_latency
)
1601 i
->update_sink_requested_latency(i
);
1604 /* Called from main thread */
1605 void pa_sink_set_latency_range(pa_sink
*s
, pa_usec_t min_latency
, pa_usec_t max_latency
) {
1606 pa_sink_assert_ref(s
);
1608 /* min_latency == 0: no limit
1609 * min_latency == (size_t) -1: default limit
1610 * min_latency anything else: specified limit
1612 * Similar for max_latency */
1614 if (min_latency
== (pa_usec_t
) -1)
1615 min_latency
= DEFAULT_MIN_LATENCY
;
1617 if (max_latency
== (pa_usec_t
) -1)
1618 max_latency
= min_latency
;
1620 pa_assert(!min_latency
|| !max_latency
||
1621 min_latency
<= max_latency
);
1623 if (PA_SINK_IS_LINKED(s
->state
)) {
1629 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_LATENCY_RANGE
, r
, 0, NULL
) == 0);
1631 s
->thread_info
.min_latency
= min_latency
;
1632 s
->thread_info
.max_latency
= max_latency
;
1634 s
->monitor_source
->thread_info
.min_latency
= min_latency
;
1635 s
->monitor_source
->thread_info
.max_latency
= max_latency
;
1637 s
->thread_info
.requested_latency_valid
= s
->monitor_source
->thread_info
.requested_latency_valid
= FALSE
;
1641 /* Called from main thread */
1642 void pa_sink_get_latency_range(pa_sink
*s
, pa_usec_t
*min_latency
, pa_usec_t
*max_latency
) {
1643 pa_sink_assert_ref(s
);
1644 pa_assert(min_latency
);
1645 pa_assert(max_latency
);
1647 if (PA_SINK_IS_LINKED(s
->state
)) {
1648 pa_usec_t r
[2] = { 0, 0 };
1650 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_LATENCY_RANGE
, r
, 0, NULL
) == 0);
1652 *min_latency
= r
[0];
1653 *max_latency
= r
[1];
1655 *min_latency
= s
->thread_info
.min_latency
;
1656 *max_latency
= s
->thread_info
.max_latency
;
1660 /* Called from IO thread */
1661 void pa_sink_update_latency_range(pa_sink
*s
, pa_usec_t min_latency
, pa_usec_t max_latency
) {
1665 pa_sink_assert_ref(s
);
1667 s
->thread_info
.min_latency
= min_latency
;
1668 s
->thread_info
.max_latency
= max_latency
;
1670 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)))
1671 if (i
->update_sink_latency_range
)
1672 i
->update_sink_latency_range(i
);
1674 pa_sink_invalidate_requested_latency(s
);
1676 pa_source_update_latency_range(s
->monitor_source
, min_latency
, max_latency
);
1679 size_t pa_sink_get_max_rewind(pa_sink
*s
) {
1681 pa_sink_assert_ref(s
);
1683 if (!PA_SINK_IS_LINKED(s
->state
))
1684 return s
->thread_info
.max_rewind
;
1686 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_MAX_REWIND
, &r
, 0, NULL
) == 0);
1691 size_t pa_sink_get_max_request(pa_sink
*s
) {
1693 pa_sink_assert_ref(s
);
1695 if (!PA_SINK_IS_LINKED(s
->state
))
1696 return s
->thread_info
.max_request
;
1698 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_MAX_REQUEST
, &r
, 0, NULL
) == 0);