4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public License
20 along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
33 #include <pulse/introspect.h>
34 #include <pulse/utf8.h>
35 #include <pulse/xmalloc.h>
36 #include <pulse/timeval.h>
38 #include <pulsecore/sink-input.h>
39 #include <pulsecore/namereg.h>
40 #include <pulsecore/core-util.h>
41 #include <pulsecore/sample-util.h>
42 #include <pulsecore/core-subscribe.h>
43 #include <pulsecore/log.h>
44 #include <pulsecore/macro.h>
45 #include <pulsecore/play-memblockq.h>
49 #define MAX_MIX_CHANNELS 32
50 #define MIX_BUFFER_LENGTH (PA_PAGE_SIZE)
51 #define DEFAULT_MIN_LATENCY (4*PA_USEC_PER_MSEC)
53 static PA_DEFINE_CHECK_TYPE(pa_sink
, pa_msgobject
);
55 static void sink_free(pa_object
*s
);
57 pa_sink_new_data
* pa_sink_new_data_init(pa_sink_new_data
*data
) {
60 memset(data
, 0, sizeof(*data
));
61 data
->proplist
= pa_proplist_new();
66 void pa_sink_new_data_set_name(pa_sink_new_data
*data
, const char *name
) {
70 data
->name
= pa_xstrdup(name
);
73 void pa_sink_new_data_set_sample_spec(pa_sink_new_data
*data
, const pa_sample_spec
*spec
) {
76 if ((data
->sample_spec_is_set
= !!spec
))
77 data
->sample_spec
= *spec
;
80 void pa_sink_new_data_set_channel_map(pa_sink_new_data
*data
, const pa_channel_map
*map
) {
83 if ((data
->channel_map_is_set
= !!map
))
84 data
->channel_map
= *map
;
87 void pa_sink_new_data_set_volume(pa_sink_new_data
*data
, const pa_cvolume
*volume
) {
90 if ((data
->volume_is_set
= !!volume
))
91 data
->volume
= *volume
;
94 void pa_sink_new_data_set_muted(pa_sink_new_data
*data
, pa_bool_t mute
) {
97 data
->muted_is_set
= TRUE
;
101 void pa_sink_new_data_done(pa_sink_new_data
*data
) {
104 pa_xfree(data
->name
);
105 pa_proplist_free(data
->proplist
);
108 static void reset_callbacks(pa_sink
*s
) {
112 s
->get_volume
= NULL
;
113 s
->set_volume
= NULL
;
116 s
->get_latency
= NULL
;
117 s
->request_rewind
= NULL
;
118 s
->update_requested_latency
= NULL
;
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
)
171 if (pa_hook_fire(&core
->hooks
[PA_CORE_HOOK_SINK_FIXATE
], data
) < 0) {
173 pa_namereg_unregister(core
, name
);
177 s
->parent
.parent
.free
= sink_free
;
178 s
->parent
.process_msg
= pa_sink_process_msg
;
181 s
->state
= PA_SINK_INIT
;
183 s
->name
= pa_xstrdup(name
);
184 s
->proplist
= pa_proplist_copy(data
->proplist
);
185 s
->driver
= pa_xstrdup(data
->driver
);
186 s
->module
= data
->module
;
188 s
->sample_spec
= data
->sample_spec
;
189 s
->channel_map
= data
->channel_map
;
191 s
->inputs
= pa_idxset_new(NULL
, NULL
);
194 s
->volume
= data
->volume
;
195 s
->muted
= data
->muted
;
196 s
->refresh_volume
= s
->refresh_mute
= FALSE
;
204 pa_silence_memchunk_get(
205 &core
->silence_cache
,
211 s
->min_latency
= DEFAULT_MIN_LATENCY
;
212 s
->max_latency
= s
->min_latency
;
214 s
->thread_info
.inputs
= pa_hashmap_new(pa_idxset_trivial_hash_func
, pa_idxset_trivial_compare_func
);
215 s
->thread_info
.soft_volume
= s
->volume
;
216 s
->thread_info
.soft_muted
= s
->muted
;
217 s
->thread_info
.state
= s
->state
;
218 s
->thread_info
.rewind_nbytes
= 0;
219 s
->thread_info
.max_rewind
= 0;
220 s
->thread_info
.requested_latency_valid
= FALSE
;
221 s
->thread_info
.requested_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
;
253 pa_source_set_max_rewind(s
->monitor_source
, s
->thread_info
.max_rewind
);
258 static int sink_set_state(pa_sink
*s
, pa_sink_state_t state
) {
260 pa_bool_t suspend_change
;
264 if (s
->state
== state
)
268 (s
->state
== PA_SINK_SUSPENDED
&& PA_SINK_IS_OPENED(state
)) ||
269 (PA_SINK_IS_OPENED(s
->state
) && state
== PA_SINK_SUSPENDED
);
272 if ((ret
= s
->set_state(s
, state
)) < 0)
275 if (pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_STATE
, PA_UINT_TO_PTR(state
), 0, NULL
) < 0)
280 if (suspend_change
) {
284 /* We're suspending or resuming, tell everyone about it */
286 for (i
= PA_SINK_INPUT(pa_idxset_first(s
->inputs
, &idx
)); i
; i
= PA_SINK_INPUT(pa_idxset_next(s
->inputs
, &idx
)))
288 i
->suspend(i
, state
== PA_SINK_SUSPENDED
);
291 if (state
!= PA_SINK_UNLINKED
) /* if we enter UNLINKED state pa_sink_unlink() will fire the apropriate events */
292 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_STATE_CHANGED
], s
);
297 void pa_sink_put(pa_sink
* s
) {
298 pa_sink_assert_ref(s
);
300 pa_assert(s
->state
== PA_SINK_INIT
);
301 pa_assert(s
->asyncmsgq
);
302 pa_assert(s
->rtpoll
);
304 pa_assert(!s
->min_latency
|| !s
->max_latency
|| s
->min_latency
<= s
->max_latency
);
306 if (!(s
->flags
& PA_SINK_HW_VOLUME_CTRL
))
307 s
->flags
|= PA_SINK_DECIBEL_VOLUME
;
309 pa_assert_se(sink_set_state(s
, PA_SINK_IDLE
) == 0);
311 pa_source_put(s
->monitor_source
);
313 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
| PA_SUBSCRIPTION_EVENT_NEW
, s
->index
);
314 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_PUT
], s
);
317 void pa_sink_unlink(pa_sink
* s
) {
319 pa_sink_input
*i
, *j
= NULL
;
323 /* Please note that pa_sink_unlink() does more than simply
324 * reversing pa_sink_put(). It also undoes the registrations
325 * already done in pa_sink_new()! */
327 /* All operations here shall be idempotent, i.e. pa_sink_unlink()
328 * may be called multiple times on the same sink without bad
331 linked
= PA_SINK_IS_LINKED(s
->state
);
334 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_UNLINK
], s
);
336 if (s
->state
!= PA_SINK_UNLINKED
)
337 pa_namereg_unregister(s
->core
, s
->name
);
338 pa_idxset_remove_by_data(s
->core
->sinks
, s
, NULL
);
340 while ((i
= pa_idxset_first(s
->inputs
, NULL
))) {
342 pa_sink_input_kill(i
);
347 sink_set_state(s
, PA_SINK_UNLINKED
);
349 s
->state
= PA_SINK_UNLINKED
;
353 if (s
->monitor_source
)
354 pa_source_unlink(s
->monitor_source
);
357 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
| PA_SUBSCRIPTION_EVENT_REMOVE
, s
->index
);
358 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_UNLINK_POST
], s
);
362 static void sink_free(pa_object
*o
) {
363 pa_sink
*s
= PA_SINK(o
);
367 pa_assert(pa_sink_refcnt(s
) == 0);
369 if (PA_SINK_IS_LINKED(s
->state
))
372 pa_log_info("Freeing sink %u \"%s\"", s
->index
, s
->name
);
374 if (s
->monitor_source
) {
375 pa_source_unref(s
->monitor_source
);
376 s
->monitor_source
= NULL
;
379 pa_idxset_free(s
->inputs
, NULL
, NULL
);
381 while ((i
= pa_hashmap_steal_first(s
->thread_info
.inputs
)))
382 pa_sink_input_unref(i
);
384 pa_hashmap_free(s
->thread_info
.inputs
, NULL
, NULL
);
386 if (s
->silence
.memblock
)
387 pa_memblock_unref(s
->silence
.memblock
);
393 pa_proplist_free(s
->proplist
);
398 void pa_sink_set_asyncmsgq(pa_sink
*s
, pa_asyncmsgq
*q
) {
399 pa_sink_assert_ref(s
);
403 if (s
->monitor_source
)
404 pa_source_set_asyncmsgq(s
->monitor_source
, q
);
407 void pa_sink_set_rtpoll(pa_sink
*s
, pa_rtpoll
*p
) {
408 pa_sink_assert_ref(s
);
411 if (s
->monitor_source
)
412 pa_source_set_rtpoll(s
->monitor_source
, p
);
415 int pa_sink_update_status(pa_sink
*s
) {
416 pa_sink_assert_ref(s
);
417 pa_assert(PA_SINK_IS_LINKED(s
->state
));
419 if (s
->state
== PA_SINK_SUSPENDED
)
422 return sink_set_state(s
, pa_sink_used_by(s
) ? PA_SINK_RUNNING
: PA_SINK_IDLE
);
425 int pa_sink_suspend(pa_sink
*s
, pa_bool_t suspend
) {
426 pa_sink_assert_ref(s
);
427 pa_assert(PA_SINK_IS_LINKED(s
->state
));
430 return sink_set_state(s
, PA_SINK_SUSPENDED
);
432 return sink_set_state(s
, pa_sink_used_by(s
) ? PA_SINK_RUNNING
: PA_SINK_IDLE
);
435 void pa_sink_process_rewind(pa_sink
*s
, size_t nbytes
) {
438 pa_sink_assert_ref(s
);
439 pa_assert(PA_SINK_IS_LINKED(s
->state
));
441 /* Make sure the sink code already reset the counter! */
442 pa_assert(s
->thread_info
.rewind_nbytes
<= 0);
447 pa_log_debug("Processing rewind...");
449 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
))) {
450 pa_sink_input_assert_ref(i
);
451 pa_sink_input_process_rewind(i
, nbytes
);
454 if (s
->monitor_source
&& PA_SOURCE_IS_OPENED(pa_source_get_state(s
->monitor_source
)))
455 pa_source_process_rewind(s
->monitor_source
, nbytes
);
459 static unsigned fill_mix_info(pa_sink
*s
, size_t *length
, pa_mix_info
*info
, unsigned maxinfo
) {
463 size_t mixlength
= *length
;
465 pa_sink_assert_ref(s
);
468 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)) && maxinfo
> 0) {
469 pa_sink_input_assert_ref(i
);
471 if (pa_sink_input_peek(i
, *length
, &info
->chunk
, &info
->volume
) < 0)
474 if (mixlength
== 0 || info
->chunk
.length
< mixlength
)
475 mixlength
= info
->chunk
.length
;
477 if (pa_memblock_is_silence(info
->chunk
.memblock
)) {
478 pa_memblock_unref(info
->chunk
.memblock
);
482 info
->userdata
= pa_sink_input_ref(i
);
484 pa_assert(info
->chunk
.memblock
);
485 pa_assert(info
->chunk
.length
> 0);
498 static void inputs_drop(pa_sink
*s
, pa_mix_info
*info
, unsigned n
, size_t length
) {
502 unsigned n_unreffed
= 0;
504 pa_sink_assert_ref(s
);
506 /* We optimize for the case where the order of the inputs has not changed */
508 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
))) {
512 pa_sink_input_assert_ref(i
);
516 /* Let's try to find the matching entry info the pa_mix_info array */
517 for (j
= 0; j
< n
; j
++) {
519 if (info
[p
].userdata
== i
) {
530 pa_sink_input_drop(i
, length
);
533 pa_sink_input_unref(m
->userdata
);
535 if (m
->chunk
.memblock
)
536 pa_memblock_unref(m
->chunk
.memblock
);
537 pa_memchunk_reset(&m
->chunk
);
543 /* Now drop references to entries that are included in the
544 * pa_mix_info array but don't exist anymore */
546 if (n_unreffed
< n
) {
547 for (; n
> 0; info
++, n
--) {
549 pa_sink_input_unref(info
->userdata
);
550 if (info
->chunk
.memblock
)
551 pa_memblock_unref(info
->chunk
.memblock
);
556 void pa_sink_render(pa_sink
*s
, size_t length
, pa_memchunk
*result
) {
557 pa_mix_info info
[MAX_MIX_CHANNELS
];
559 size_t block_size_max
;
561 pa_sink_assert_ref(s
);
562 pa_assert(PA_SINK_IS_OPENED(s
->thread_info
.state
));
563 pa_assert(pa_frame_aligned(length
, &s
->sample_spec
));
568 s
->thread_info
.rewind_nbytes
= 0;
571 length
= pa_frame_align(MIX_BUFFER_LENGTH
, &s
->sample_spec
);
573 block_size_max
= pa_mempool_block_size_max(s
->core
->mempool
);
574 if (length
> block_size_max
)
575 length
= pa_frame_align(block_size_max
, &s
->sample_spec
);
577 pa_assert(length
> 0);
579 n
= s
->thread_info
.state
== PA_SINK_RUNNING
? fill_mix_info(s
, &length
, info
, MAX_MIX_CHANNELS
) : 0;
583 *result
= s
->silence
;
584 pa_memblock_ref(result
->memblock
);
586 if (result
->length
> length
)
587 result
->length
= length
;
592 *result
= info
[0].chunk
;
593 pa_memblock_ref(result
->memblock
);
595 if (result
->length
> length
)
596 result
->length
= length
;
598 pa_sw_cvolume_multiply(&volume
, &s
->thread_info
.soft_volume
, &info
[0].volume
);
600 if (s
->thread_info
.soft_muted
|| !pa_cvolume_is_norm(&volume
)) {
601 pa_log("adjusting volume ");
602 pa_memchunk_make_writable(result
, 0);
603 if (s
->thread_info
.soft_muted
|| pa_cvolume_is_muted(&volume
))
604 pa_silence_memchunk(result
, &s
->sample_spec
);
606 pa_volume_memchunk(result
, &s
->sample_spec
, &volume
);
610 result
->memblock
= pa_memblock_new(s
->core
->mempool
, length
);
612 ptr
= pa_memblock_acquire(result
->memblock
);
613 result
->length
= pa_mix(info
, n
,
616 &s
->thread_info
.soft_volume
,
617 s
->thread_info
.soft_muted
);
618 pa_memblock_release(result
->memblock
);
623 if (s
->thread_info
.state
== PA_SINK_RUNNING
)
624 inputs_drop(s
, info
, n
, result
->length
);
626 if (s
->monitor_source
&& PA_SOURCE_IS_OPENED(pa_source_get_state(s
->monitor_source
)))
627 pa_source_post(s
->monitor_source
, result
);
632 void pa_sink_render_into(pa_sink
*s
, pa_memchunk
*target
) {
633 pa_mix_info info
[MAX_MIX_CHANNELS
];
635 size_t length
, block_size_max
;
637 pa_sink_assert_ref(s
);
638 pa_assert(PA_SINK_IS_OPENED(s
->thread_info
.state
));
640 pa_assert(target
->memblock
);
641 pa_assert(target
->length
> 0);
642 pa_assert(pa_frame_aligned(target
->length
, &s
->sample_spec
));
646 s
->thread_info
.rewind_nbytes
= 0;
648 length
= target
->length
;
649 block_size_max
= pa_mempool_block_size_max(s
->core
->mempool
);
650 if (length
> block_size_max
)
651 length
= pa_frame_align(block_size_max
, &s
->sample_spec
);
653 n
= s
->thread_info
.state
== PA_SINK_RUNNING
? fill_mix_info(s
, &length
, info
, MAX_MIX_CHANNELS
) : 0;
656 if (target
->length
> length
)
657 target
->length
= length
;
659 pa_silence_memchunk(target
, &s
->sample_spec
);
663 if (target
->length
> length
)
664 target
->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_muted(&volume
))
669 pa_silence_memchunk(target
, &s
->sample_spec
);
673 vchunk
= info
[0].chunk
;
674 pa_memblock_ref(vchunk
.memblock
);
676 if (vchunk
.length
> target
->length
)
677 vchunk
.length
= target
->length
;
679 if (!pa_cvolume_is_norm(&volume
)) {
680 pa_memchunk_make_writable(&vchunk
, 0);
681 pa_volume_memchunk(&vchunk
, &s
->sample_spec
, &volume
);
684 pa_memchunk_memcpy(target
, &vchunk
);
685 pa_memblock_unref(vchunk
.memblock
);
691 ptr
= pa_memblock_acquire(target
->memblock
);
693 target
->length
= pa_mix(info
, n
,
694 (uint8_t*) ptr
+ target
->index
, length
,
696 &s
->thread_info
.soft_volume
,
697 s
->thread_info
.soft_muted
);
699 pa_memblock_release(target
->memblock
);
702 if (s
->thread_info
.state
== PA_SINK_RUNNING
)
703 inputs_drop(s
, info
, n
, target
->length
);
705 if (s
->monitor_source
&& PA_SOURCE_IS_OPENED(pa_source_get_state(s
->monitor_source
)))
706 pa_source_post(s
->monitor_source
, target
);
711 void pa_sink_render_into_full(pa_sink
*s
, pa_memchunk
*target
) {
715 pa_sink_assert_ref(s
);
716 pa_assert(PA_SINK_IS_OPENED(s
->thread_info
.state
));
718 pa_assert(target
->memblock
);
719 pa_assert(target
->length
> 0);
720 pa_assert(pa_frame_aligned(target
->length
, &s
->sample_spec
));
724 s
->thread_info
.rewind_nbytes
= 0;
733 pa_sink_render_into(s
, &chunk
);
742 void pa_sink_render_full(pa_sink
*s
, size_t length
, pa_memchunk
*result
) {
743 pa_sink_assert_ref(s
);
744 pa_assert(PA_SINK_IS_OPENED(s
->thread_info
.state
));
745 pa_assert(length
> 0);
746 pa_assert(pa_frame_aligned(length
, &s
->sample_spec
));
749 s
->thread_info
.rewind_nbytes
= 0;
751 /*** This needs optimization ***/
754 result
->length
= length
;
755 result
->memblock
= pa_memblock_new(s
->core
->mempool
, length
);
757 pa_sink_render_into_full(s
, result
);
760 pa_usec_t
pa_sink_get_latency(pa_sink
*s
) {
763 pa_sink_assert_ref(s
);
764 pa_assert(PA_SINK_IS_LINKED(s
->state
));
766 /* The returned value is supposed to be in the time domain of the sound card! */
768 if (!PA_SINK_IS_OPENED(s
->state
))
772 return s
->get_latency(s
);
774 if (pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_LATENCY
, &usec
, 0, NULL
) < 0)
780 void pa_sink_set_volume(pa_sink
*s
, const pa_cvolume
*volume
) {
783 pa_sink_assert_ref(s
);
784 pa_assert(PA_SINK_IS_LINKED(s
->state
));
787 changed
= !pa_cvolume_equal(volume
, &s
->volume
);
790 if (s
->set_volume
&& s
->set_volume(s
) < 0)
791 s
->set_volume
= NULL
;
794 pa_asyncmsgq_post(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_VOLUME
, pa_xnewdup(struct pa_cvolume
, volume
, 1), 0, NULL
, pa_xfree
);
797 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
800 const pa_cvolume
*pa_sink_get_volume(pa_sink
*s
) {
801 struct pa_cvolume old_volume
;
803 pa_sink_assert_ref(s
);
804 pa_assert(PA_SINK_IS_LINKED(s
->state
));
806 old_volume
= s
->volume
;
808 if (s
->get_volume
&& s
->get_volume(s
) < 0)
809 s
->get_volume
= NULL
;
811 if (!s
->get_volume
&& s
->refresh_volume
)
812 pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_VOLUME
, &s
->volume
, 0, NULL
);
814 if (!pa_cvolume_equal(&old_volume
, &s
->volume
))
815 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
820 void pa_sink_set_mute(pa_sink
*s
, pa_bool_t mute
) {
823 pa_sink_assert_ref(s
);
824 pa_assert(PA_SINK_IS_LINKED(s
->state
));
826 changed
= s
->muted
!= mute
;
829 if (s
->set_mute
&& s
->set_mute(s
) < 0)
833 pa_asyncmsgq_post(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_MUTE
, PA_UINT_TO_PTR(mute
), 0, NULL
, NULL
);
836 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
839 pa_bool_t
pa_sink_get_mute(pa_sink
*s
) {
842 pa_sink_assert_ref(s
);
843 pa_assert(PA_SINK_IS_LINKED(s
->state
));
845 old_muted
= s
->muted
;
847 if (s
->get_mute
&& s
->get_mute(s
) < 0)
850 if (!s
->get_mute
&& s
->refresh_mute
)
851 pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_MUTE
, &s
->muted
, 0, NULL
);
853 if (old_muted
!= s
->muted
)
854 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
859 void pa_sink_set_description(pa_sink
*s
, const char *description
) {
861 pa_sink_assert_ref(s
);
863 if (!description
&& !pa_proplist_contains(s
->proplist
, PA_PROP_DEVICE_DESCRIPTION
))
866 old
= pa_proplist_gets(s
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
868 if (old
&& description
&& !strcmp(old
, description
))
872 pa_proplist_sets(s
->proplist
, PA_PROP_DEVICE_DESCRIPTION
, description
);
874 pa_proplist_unset(s
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
876 if (s
->monitor_source
) {
879 n
= pa_sprintf_malloc("Monitor Source of %s", description
? description
: s
->name
);
880 pa_source_set_description(s
->monitor_source
, n
);
884 if (PA_SINK_IS_LINKED(s
->state
)) {
885 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
886 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_PROPLIST_CHANGED
], s
);
890 unsigned pa_sink_linked_by(pa_sink
*s
) {
893 pa_sink_assert_ref(s
);
894 pa_assert(PA_SINK_IS_LINKED(s
->state
));
896 ret
= pa_idxset_size(s
->inputs
);
898 /* We add in the number of streams connected to us here. Please
899 * not the asymmmetry to pa_sink_used_by()! */
901 if (s
->monitor_source
)
902 ret
+= pa_source_linked_by(s
->monitor_source
);
907 unsigned pa_sink_used_by(pa_sink
*s
) {
910 pa_sink_assert_ref(s
);
911 pa_assert(PA_SINK_IS_LINKED(s
->state
));
913 ret
= pa_idxset_size(s
->inputs
);
914 pa_assert(ret
>= s
->n_corked
);
916 /* Streams connected to our monitor source do not matter for
917 * pa_sink_used_by()!.*/
919 return ret
- s
->n_corked
;
922 int pa_sink_process_msg(pa_msgobject
*o
, int code
, void *userdata
, int64_t offset
, pa_memchunk
*chunk
) {
923 pa_sink
*s
= PA_SINK(o
);
924 pa_sink_assert_ref(s
);
925 pa_assert(s
->thread_info
.state
!= PA_SINK_UNLINKED
);
927 switch ((pa_sink_message_t
) code
) {
929 case PA_SINK_MESSAGE_ADD_INPUT
: {
930 pa_sink_input
*i
= PA_SINK_INPUT(userdata
);
932 pa_hashmap_put(s
->thread_info
.inputs
, PA_UINT32_TO_PTR(i
->index
), pa_sink_input_ref(i
));
934 /* Since the caller sleeps in pa_sink_input_put(), we can
935 * safely access data outside of thread_info even though
938 if ((i
->thread_info
.sync_prev
= i
->sync_prev
)) {
939 pa_assert(i
->sink
== i
->thread_info
.sync_prev
->sink
);
940 pa_assert(i
->sync_prev
->sync_next
== i
);
941 i
->thread_info
.sync_prev
->thread_info
.sync_next
= i
;
944 if ((i
->thread_info
.sync_next
= i
->sync_next
)) {
945 pa_assert(i
->sink
== i
->thread_info
.sync_next
->sink
);
946 pa_assert(i
->sync_next
->sync_prev
== i
);
947 i
->thread_info
.sync_next
->thread_info
.sync_prev
= i
;
950 pa_assert(!i
->thread_info
.attached
);
951 i
->thread_info
.attached
= TRUE
;
956 pa_sink_input_set_state_within_thread(i
, i
->state
);
958 pa_sink_input_update_max_rewind(i
, s
->thread_info
.max_rewind
);
960 pa_sink_invalidate_requested_latency(s
);
962 /* We don't rewind here automatically. This is left to the
963 * sink input implementor because some sink inputs need a
964 * slow start, i.e. need some time to buffer client
965 * samples before beginning streaming. */
967 /* If you change anything here, make sure to change the
968 * ghost sink input handling a few lines down at
969 * PA_SINK_MESSAGE_REMOVE_INPUT_AND_BUFFER, too. */
974 case PA_SINK_MESSAGE_REMOVE_INPUT
: {
975 pa_sink_input
*i
= PA_SINK_INPUT(userdata
);
977 /* If you change anything here, make sure to change the
978 * sink input handling a few lines down at
979 * PA_SINK_MESSAGE_REMOVE_INPUT_AND_BUFFER, too. */
981 pa_sink_input_set_state_within_thread(i
, i
->state
);
986 pa_assert(i
->thread_info
.attached
);
987 i
->thread_info
.attached
= FALSE
;
989 /* Since the caller sleeps in pa_sink_input_unlink(),
990 * we can safely access data outside of thread_info even
991 * though it is mutable */
993 pa_assert(!i
->thread_info
.sync_prev
);
994 pa_assert(!i
->thread_info
.sync_next
);
996 if (i
->thread_info
.sync_prev
) {
997 i
->thread_info
.sync_prev
->thread_info
.sync_next
= i
->thread_info
.sync_prev
->sync_next
;
998 i
->thread_info
.sync_prev
= NULL
;
1001 if (i
->thread_info
.sync_next
) {
1002 i
->thread_info
.sync_next
->thread_info
.sync_prev
= i
->thread_info
.sync_next
->sync_prev
;
1003 i
->thread_info
.sync_next
= NULL
;
1006 if (pa_hashmap_remove(s
->thread_info
.inputs
, PA_UINT32_TO_PTR(i
->index
)))
1007 pa_sink_input_unref(i
);
1009 pa_sink_invalidate_requested_latency(s
);
1010 pa_sink_request_rewind(s
, 0);
1015 case PA_SINK_MESSAGE_REMOVE_INPUT_AND_BUFFER
: {
1016 pa_sink_input_move_info
*info
= userdata
;
1019 /* We don't support moving synchronized streams. */
1020 pa_assert(!info
->sink_input
->sync_prev
);
1021 pa_assert(!info
->sink_input
->sync_next
);
1022 pa_assert(!info
->sink_input
->thread_info
.sync_next
);
1023 pa_assert(!info
->sink_input
->thread_info
.sync_prev
);
1025 if (info
->sink_input
->detach
)
1026 info
->sink_input
->detach(info
->sink_input
);
1028 pa_assert(info
->sink_input
->thread_info
.attached
);
1029 info
->sink_input
->thread_info
.attached
= FALSE
;
1030 pa_sink_invalidate_requested_latency(info
->sink_input
->sink
);
1032 if (info
->ghost_sink_input
) {
1033 pa_assert(info
->buffer_bytes
> 0);
1034 pa_assert(info
->buffer
);
1036 volume_is_norm
= pa_cvolume_is_norm(&info
->sink_input
->thread_info
.volume
);
1038 pa_log_debug("Buffering %lu bytes ...", (unsigned long) info
->buffer_bytes
);
1040 while (info
->buffer_bytes
> 0) {
1041 pa_memchunk memchunk
;
1045 if (pa_sink_input_peek(info
->sink_input
, info
->buffer_bytes
, &memchunk
, &volume
) < 0)
1048 n
= memchunk
.length
> info
->buffer_bytes
? info
->buffer_bytes
: memchunk
.length
;
1049 pa_sink_input_drop(info
->sink_input
, n
);
1050 memchunk
.length
= n
;
1052 if (!volume_is_norm
) {
1053 pa_memchunk_make_writable(&memchunk
, 0);
1054 pa_volume_memchunk(&memchunk
, &s
->sample_spec
, &volume
);
1057 if (pa_memblockq_push(info
->buffer
, &memchunk
) < 0) {
1058 pa_memblock_unref(memchunk
.memblock
);
1062 pa_memblock_unref(memchunk
.memblock
);
1063 info
->buffer_bytes
-= n
;
1066 /* Add the remaining already resampled chunks to the buffer */
1067 pa_memblockq_splice(info
->buffer
, info
->sink_input
->thread_info
.render_memblockq
);
1069 pa_memblockq_sink_input_set_queue(info
->ghost_sink_input
, info
->buffer
);
1071 pa_log_debug("Buffered %lu bytes ...", (unsigned long) pa_memblockq_get_length(info
->buffer
));
1074 /* Let's remove the sink input ...*/
1075 if (pa_hashmap_remove(s
->thread_info
.inputs
, PA_UINT32_TO_PTR(info
->sink_input
->index
)))
1076 pa_sink_input_unref(info
->sink_input
);
1078 /* .. and add the ghost sink input instead */
1079 if (info
->ghost_sink_input
) {
1080 pa_hashmap_put(s
->thread_info
.inputs
, PA_UINT32_TO_PTR(info
->ghost_sink_input
->index
), pa_sink_input_ref(info
->ghost_sink_input
));
1081 info
->ghost_sink_input
->thread_info
.sync_prev
= info
->ghost_sink_input
->thread_info
.sync_next
= NULL
;
1083 pa_sink_input_update_max_rewind(info
->ghost_sink_input
, s
->thread_info
.max_rewind
);
1085 pa_assert(!info
->ghost_sink_input
->thread_info
.attached
);
1086 info
->ghost_sink_input
->thread_info
.attached
= TRUE
;
1088 if (info
->ghost_sink_input
->attach
)
1089 info
->ghost_sink_input
->attach(info
->ghost_sink_input
);
1092 pa_sink_invalidate_requested_latency(s
);
1093 pa_sink_request_rewind(s
, 0);
1098 case PA_SINK_MESSAGE_SET_VOLUME
:
1099 s
->thread_info
.soft_volume
= *((pa_cvolume
*) userdata
);
1101 pa_sink_request_rewind(s
, 0);
1104 case PA_SINK_MESSAGE_SET_MUTE
:
1105 s
->thread_info
.soft_muted
= PA_PTR_TO_UINT(userdata
);
1107 pa_sink_request_rewind(s
, 0);
1110 case PA_SINK_MESSAGE_GET_VOLUME
:
1111 *((pa_cvolume
*) userdata
) = s
->thread_info
.soft_volume
;
1114 case PA_SINK_MESSAGE_GET_MUTE
:
1115 *((pa_bool_t
*) userdata
) = s
->thread_info
.soft_muted
;
1118 case PA_SINK_MESSAGE_SET_STATE
:
1120 s
->thread_info
.state
= PA_PTR_TO_UINT(userdata
);
1123 case PA_SINK_MESSAGE_DETACH
:
1125 /* We're detaching all our input streams so that the
1126 * asyncmsgq and rtpoll fields can be changed without
1128 pa_sink_detach_within_thread(s
);
1131 case PA_SINK_MESSAGE_ATTACH
:
1133 /* Reattach all streams */
1134 pa_sink_attach_within_thread(s
);
1137 case PA_SINK_MESSAGE_GET_REQUESTED_LATENCY
: {
1139 pa_usec_t
*usec
= userdata
;
1140 *usec
= pa_sink_get_requested_latency_within_thread(s
);
1144 case PA_SINK_MESSAGE_GET_LATENCY
:
1145 case PA_SINK_MESSAGE_MAX
:
1152 int pa_sink_suspend_all(pa_core
*c
, pa_bool_t suspend
) {
1157 pa_core_assert_ref(c
);
1159 for (sink
= PA_SINK(pa_idxset_first(c
->sinks
, &idx
)); sink
; sink
= PA_SINK(pa_idxset_next(c
->sinks
, &idx
)))
1160 ret
-= pa_sink_suspend(sink
, suspend
) < 0;
1165 void pa_sink_detach(pa_sink
*s
) {
1166 pa_sink_assert_ref(s
);
1167 pa_assert(PA_SINK_IS_LINKED(s
->state
));
1169 pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_DETACH
, NULL
, 0, NULL
);
1172 void pa_sink_attach(pa_sink
*s
) {
1173 pa_sink_assert_ref(s
);
1174 pa_assert(PA_SINK_IS_LINKED(s
->state
));
1176 pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_ATTACH
, NULL
, 0, NULL
);
1179 void pa_sink_detach_within_thread(pa_sink
*s
) {
1183 pa_sink_assert_ref(s
);
1184 pa_assert(PA_SINK_IS_LINKED(s
->thread_info
.state
));
1186 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)))
1190 if (s
->monitor_source
)
1191 pa_source_detach_within_thread(s
->monitor_source
);
1194 void pa_sink_attach_within_thread(pa_sink
*s
) {
1198 pa_sink_assert_ref(s
);
1199 pa_assert(PA_SINK_IS_LINKED(s
->thread_info
.state
));
1201 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)))
1205 if (s
->monitor_source
)
1206 pa_source_attach_within_thread(s
->monitor_source
);
1209 void pa_sink_request_rewind(pa_sink
*s
, size_t nbytes
) {
1210 pa_sink_assert_ref(s
);
1211 pa_assert(PA_SINK_IS_LINKED(s
->thread_info
.state
));
1214 nbytes
= s
->thread_info
.max_rewind
;
1216 nbytes
= PA_MIN(nbytes
, s
->thread_info
.max_rewind
);
1218 if (nbytes
<= s
->thread_info
.rewind_nbytes
)
1221 s
->thread_info
.rewind_nbytes
= nbytes
;
1223 if (s
->request_rewind
)
1224 s
->request_rewind(s
);
1227 pa_usec_t
pa_sink_get_requested_latency_within_thread(pa_sink
*s
) {
1228 pa_usec_t result
= (pa_usec_t
) -1;
1232 pa_sink_assert_ref(s
);
1234 if (s
->thread_info
.requested_latency_valid
)
1235 return s
->thread_info
.requested_latency
;
1237 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)))
1239 if (i
->thread_info
.requested_sink_latency
!= (pa_usec_t
) -1 &&
1240 (result
== (pa_usec_t
) -1 || result
> i
->thread_info
.requested_sink_latency
))
1241 result
= i
->thread_info
.requested_sink_latency
;
1243 if (result
!= (pa_usec_t
) -1) {
1244 if (s
->max_latency
> 0 && result
> s
->max_latency
)
1245 result
= s
->max_latency
;
1247 if (s
->min_latency
> 0 && result
< s
->min_latency
)
1248 result
= s
->min_latency
;
1251 s
->thread_info
.requested_latency
= result
;
1252 s
->thread_info
.requested_latency_valid
= TRUE
;
1257 pa_usec_t
pa_sink_get_requested_latency(pa_sink
*s
) {
1260 pa_sink_assert_ref(s
);
1261 pa_assert(PA_SINK_IS_LINKED(s
->state
));
1263 if (!PA_SINK_IS_OPENED(s
->state
))
1266 if (pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_REQUESTED_LATENCY
, &usec
, 0, NULL
) < 0)
1269 if (usec
== (pa_usec_t
) -1)
1270 usec
= s
->max_latency
;
1275 void pa_sink_set_max_rewind(pa_sink
*s
, size_t max_rewind
) {
1279 pa_sink_assert_ref(s
);
1281 if (max_rewind
== s
->thread_info
.max_rewind
)
1284 s
->thread_info
.max_rewind
= max_rewind
;
1286 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)))
1287 pa_sink_input_update_max_rewind(i
, s
->thread_info
.max_rewind
);
1289 if (s
->monitor_source
)
1290 pa_source_set_max_rewind(s
->monitor_source
, s
->thread_info
.max_rewind
);
1293 void pa_sink_invalidate_requested_latency(pa_sink
*s
) {
1295 pa_sink_assert_ref(s
);
1296 pa_assert(PA_SINK_IS_LINKED(s
->thread_info
.state
));
1298 if (!s
->thread_info
.requested_latency_valid
)
1301 s
->thread_info
.requested_latency_valid
= FALSE
;
1303 if (s
->update_requested_latency
)
1304 s
->update_requested_latency(s
);