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/utf8.h>
32 #include <pulse/xmalloc.h>
33 #include <pulse/timeval.h>
35 #include <pulsecore/source-output.h>
36 #include <pulsecore/namereg.h>
37 #include <pulsecore/core-subscribe.h>
38 #include <pulsecore/log.h>
39 #include <pulsecore/sample-util.h>
43 #define DEFAULT_MIN_LATENCY (4*PA_USEC_PER_MSEC)
45 static PA_DEFINE_CHECK_TYPE(pa_source
, pa_msgobject
);
47 static void source_free(pa_object
*o
);
49 pa_source_new_data
* pa_source_new_data_init(pa_source_new_data
*data
) {
52 memset(data
, 0, sizeof(*data
));
53 data
->proplist
= pa_proplist_new();
58 void pa_source_new_data_set_name(pa_source_new_data
*data
, const char *name
) {
62 data
->name
= pa_xstrdup(name
);
65 void pa_source_new_data_set_sample_spec(pa_source_new_data
*data
, const pa_sample_spec
*spec
) {
68 if ((data
->sample_spec_is_set
= !!spec
))
69 data
->sample_spec
= *spec
;
72 void pa_source_new_data_set_channel_map(pa_source_new_data
*data
, const pa_channel_map
*map
) {
75 if ((data
->channel_map_is_set
= !!map
))
76 data
->channel_map
= *map
;
79 void pa_source_new_data_set_volume(pa_source_new_data
*data
, const pa_cvolume
*volume
) {
82 if ((data
->volume_is_set
= !!volume
))
83 data
->volume
= *volume
;
86 void pa_source_new_data_set_muted(pa_source_new_data
*data
, pa_bool_t mute
) {
89 data
->muted_is_set
= TRUE
;
93 void pa_source_new_data_done(pa_source_new_data
*data
) {
97 pa_proplist_free(data
->proplist
);
100 /* Called from main context */
101 static void reset_callbacks(pa_source
*s
) {
105 s
->get_volume
= NULL
;
106 s
->set_volume
= NULL
;
109 s
->update_requested_latency
= NULL
;
112 /* Called from main context */
113 pa_source
* pa_source_new(
115 pa_source_new_data
*data
,
116 pa_source_flags_t flags
) {
120 char st
[PA_SAMPLE_SPEC_SNPRINT_MAX
], cm
[PA_CHANNEL_MAP_SNPRINT_MAX
];
124 pa_assert(data
->name
);
126 s
= pa_msgobject_new(pa_source
);
128 if (!(name
= pa_namereg_register(core
, data
->name
, PA_NAMEREG_SOURCE
, s
, data
->namereg_fail
))) {
133 pa_source_new_data_set_name(data
, name
);
135 if (pa_hook_fire(&core
->hooks
[PA_CORE_HOOK_SOURCE_NEW
], data
) < 0) {
137 pa_namereg_unregister(core
, name
);
141 pa_return_null_if_fail(!data
->driver
|| pa_utf8_valid(data
->driver
));
142 pa_return_null_if_fail(data
->name
&& pa_utf8_valid(data
->name
) && data
->name
[0]);
144 pa_return_null_if_fail(data
->sample_spec_is_set
&& pa_sample_spec_valid(&data
->sample_spec
));
146 if (!data
->channel_map_is_set
)
147 pa_return_null_if_fail(pa_channel_map_init_auto(&data
->channel_map
, data
->sample_spec
.channels
, PA_CHANNEL_MAP_DEFAULT
));
149 pa_return_null_if_fail(pa_channel_map_valid(&data
->channel_map
));
150 pa_return_null_if_fail(data
->channel_map
.channels
== data
->sample_spec
.channels
);
152 if (!data
->volume_is_set
)
153 pa_cvolume_reset(&data
->volume
, data
->sample_spec
.channels
);
155 pa_return_null_if_fail(pa_cvolume_valid(&data
->volume
));
156 pa_return_null_if_fail(data
->volume
.channels
== data
->sample_spec
.channels
);
158 if (!data
->muted_is_set
)
161 if (pa_hook_fire(&core
->hooks
[PA_CORE_HOOK_SOURCE_FIXATE
], data
) < 0) {
163 pa_namereg_unregister(core
, name
);
167 s
->parent
.parent
.free
= source_free
;
168 s
->parent
.process_msg
= pa_source_process_msg
;
171 s
->state
= PA_SOURCE_INIT
;
173 s
->name
= pa_xstrdup(name
);
174 s
->proplist
= pa_proplist_copy(data
->proplist
);
175 s
->driver
= pa_xstrdup(data
->driver
);
176 s
->module
= data
->module
;
178 s
->sample_spec
= data
->sample_spec
;
179 s
->channel_map
= data
->channel_map
;
181 s
->outputs
= pa_idxset_new(NULL
, NULL
);
183 s
->monitor_of
= NULL
;
185 s
->volume
= data
->volume
;
186 s
->muted
= data
->muted
;
187 s
->refresh_volume
= s
->refresh_muted
= FALSE
;
188 s
->base_volume
= PA_VOLUME_NORM
;
196 pa_silence_memchunk_get(
197 &core
->silence_cache
,
203 s
->thread_info
.outputs
= pa_hashmap_new(pa_idxset_trivial_hash_func
, pa_idxset_trivial_compare_func
);
204 pa_cvolume_reset(&s
->thread_info
.soft_volume
, s
->sample_spec
.channels
);
205 s
->thread_info
.soft_muted
= FALSE
;
206 s
->thread_info
.state
= s
->state
;
207 s
->thread_info
.max_rewind
= 0;
208 s
->thread_info
.requested_latency_valid
= FALSE
;
209 s
->thread_info
.requested_latency
= 0;
210 s
->thread_info
.min_latency
= DEFAULT_MIN_LATENCY
;
211 s
->thread_info
.max_latency
= 0;
213 pa_assert_se(pa_idxset_put(core
->sources
, s
, &s
->index
) >= 0);
215 pa_log_info("Created source %u \"%s\" with sample spec %s and channel map %s",
218 pa_sample_spec_snprint(st
, sizeof(st
), &s
->sample_spec
),
219 pa_channel_map_snprint(cm
, sizeof(cm
), &s
->channel_map
));
224 /* Called from main context */
225 static int source_set_state(pa_source
*s
, pa_source_state_t state
) {
227 pa_bool_t suspend_change
;
228 pa_source_state_t original_state
;
232 if (s
->state
== state
)
235 original_state
= s
->state
;
238 (original_state
== PA_SOURCE_SUSPENDED
&& PA_SOURCE_IS_OPENED(state
)) ||
239 (PA_SOURCE_IS_OPENED(original_state
) && state
== PA_SOURCE_SUSPENDED
);
242 if ((ret
= s
->set_state(s
, state
)) < 0)
246 if ((ret
= pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SOURCE_MESSAGE_SET_STATE
, PA_UINT_TO_PTR(state
), 0, NULL
)) < 0) {
249 s
->set_state(s
, original_state
);
256 if (suspend_change
) {
260 /* We're suspending or resuming, tell everyone about it */
262 for (o
= PA_SOURCE_OUTPUT(pa_idxset_first(s
->outputs
, &idx
)); o
; o
= PA_SOURCE_OUTPUT(pa_idxset_next(s
->outputs
, &idx
)))
264 o
->suspend(o
, state
== PA_SOURCE_SUSPENDED
);
267 if (state
!= PA_SOURCE_UNLINKED
) /* if we enter UNLINKED state pa_source_unlink() will fire the apropriate events */
268 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SOURCE_STATE_CHANGED
], s
);
273 /* Called from main context */
274 void pa_source_put(pa_source
*s
) {
275 pa_source_assert_ref(s
);
277 pa_assert(s
->state
== PA_SOURCE_INIT
);
279 /* The following fields must be initialized properly when calling _put() */
280 pa_assert(s
->asyncmsgq
);
281 pa_assert(s
->rtpoll
);
282 pa_assert(!s
->thread_info
.min_latency
|| !s
->thread_info
.max_latency
||
283 s
->thread_info
.min_latency
<= s
->thread_info
.max_latency
);
285 if (!(s
->flags
& PA_SOURCE_HW_VOLUME_CTRL
)) {
286 s
->flags
|= PA_SOURCE_DECIBEL_VOLUME
;
288 s
->thread_info
.soft_volume
= s
->volume
;
289 s
->thread_info
.soft_muted
= s
->muted
;
292 pa_assert_se(source_set_state(s
, PA_SOURCE_IDLE
) == 0);
294 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SOURCE
| PA_SUBSCRIPTION_EVENT_NEW
, s
->index
);
295 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SOURCE_PUT
], s
);
298 /* Called from main context */
299 void pa_source_unlink(pa_source
*s
) {
301 pa_source_output
*o
, *j
= NULL
;
305 /* See pa_sink_unlink() for a couple of comments how this function
308 linked
= PA_SOURCE_IS_LINKED(s
->state
);
311 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SOURCE_UNLINK
], s
);
313 if (s
->state
!= PA_SOURCE_UNLINKED
)
314 pa_namereg_unregister(s
->core
, s
->name
);
315 pa_idxset_remove_by_data(s
->core
->sources
, s
, NULL
);
317 while ((o
= pa_idxset_first(s
->outputs
, NULL
))) {
319 pa_source_output_kill(o
);
324 source_set_state(s
, PA_SOURCE_UNLINKED
);
326 s
->state
= PA_SOURCE_UNLINKED
;
331 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SOURCE
| PA_SUBSCRIPTION_EVENT_REMOVE
, s
->index
);
332 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SOURCE_UNLINK_POST
], s
);
336 /* Called from main context */
337 static void source_free(pa_object
*o
) {
338 pa_source_output
*so
;
339 pa_source
*s
= PA_SOURCE(o
);
342 pa_assert(pa_source_refcnt(s
) == 0);
344 if (PA_SOURCE_IS_LINKED(s
->state
))
347 pa_log_info("Freeing source %u \"%s\"", s
->index
, s
->name
);
349 pa_idxset_free(s
->outputs
, NULL
, NULL
);
351 while ((so
= pa_hashmap_steal_first(s
->thread_info
.outputs
)))
352 pa_source_output_unref(so
);
354 pa_hashmap_free(s
->thread_info
.outputs
, NULL
, NULL
);
356 if (s
->silence
.memblock
)
357 pa_memblock_unref(s
->silence
.memblock
);
363 pa_proplist_free(s
->proplist
);
368 /* Called from main context */
369 void pa_source_set_asyncmsgq(pa_source
*s
, pa_asyncmsgq
*q
) {
370 pa_source_assert_ref(s
);
375 /* Called from main context */
376 void pa_source_set_rtpoll(pa_source
*s
, pa_rtpoll
*p
) {
377 pa_source_assert_ref(s
);
382 /* Called from main context */
383 int pa_source_update_status(pa_source
*s
) {
384 pa_source_assert_ref(s
);
385 pa_assert(PA_SOURCE_IS_LINKED(s
->state
));
387 if (s
->state
== PA_SOURCE_SUSPENDED
)
390 return source_set_state(s
, pa_source_used_by(s
) ? PA_SOURCE_RUNNING
: PA_SOURCE_IDLE
);
393 /* Called from main context */
394 int pa_source_suspend(pa_source
*s
, pa_bool_t suspend
) {
395 pa_source_assert_ref(s
);
396 pa_assert(PA_SOURCE_IS_LINKED(s
->state
));
399 return source_set_state(s
, PA_SOURCE_SUSPENDED
);
401 return source_set_state(s
, pa_source_used_by(s
) ? PA_SOURCE_RUNNING
: PA_SOURCE_IDLE
);
404 /* Called from IO thread context */
405 void pa_source_process_rewind(pa_source
*s
, size_t nbytes
) {
409 pa_source_assert_ref(s
);
410 pa_assert(PA_SOURCE_IS_OPENED(s
->thread_info
.state
));
415 pa_log_debug("Processing rewind...");
417 while ((o
= pa_hashmap_iterate(s
->thread_info
.outputs
, &state
, NULL
))) {
418 pa_source_output_assert_ref(o
);
419 pa_source_output_process_rewind(o
, nbytes
);
423 /* Called from IO thread context */
424 void pa_source_post(pa_source
*s
, const pa_memchunk
*chunk
) {
428 pa_source_assert_ref(s
);
429 pa_assert(PA_SOURCE_IS_OPENED(s
->thread_info
.state
));
432 if (s
->thread_info
.soft_muted
|| !pa_cvolume_is_norm(&s
->thread_info
.soft_volume
)) {
433 pa_memchunk vchunk
= *chunk
;
435 pa_memblock_ref(vchunk
.memblock
);
436 pa_memchunk_make_writable(&vchunk
, 0);
438 if (s
->thread_info
.soft_muted
|| pa_cvolume_is_muted(&s
->thread_info
.soft_volume
))
439 pa_silence_memchunk(&vchunk
, &s
->sample_spec
);
441 pa_volume_memchunk(&vchunk
, &s
->sample_spec
, &s
->thread_info
.soft_volume
);
443 while ((o
= pa_hashmap_iterate(s
->thread_info
.outputs
, &state
, NULL
))) {
444 pa_source_output_assert_ref(o
);
446 if (!o
->thread_info
.direct_on_input
)
447 pa_source_output_push(o
, &vchunk
);
450 pa_memblock_unref(vchunk
.memblock
);
453 while ((o
= pa_hashmap_iterate(s
->thread_info
.outputs
, &state
, NULL
))) {
454 pa_source_output_assert_ref(o
);
456 if (!o
->thread_info
.direct_on_input
)
457 pa_source_output_push(o
, chunk
);
462 /* Called from IO thread context */
463 void pa_source_post_direct(pa_source
*s
, pa_source_output
*o
, const pa_memchunk
*chunk
) {
464 pa_source_assert_ref(s
);
465 pa_assert(PA_SOURCE_IS_OPENED(s
->thread_info
.state
));
466 pa_source_output_assert_ref(o
);
467 pa_assert(o
->thread_info
.direct_on_input
);
470 if (s
->thread_info
.soft_muted
|| !pa_cvolume_is_norm(&s
->thread_info
.soft_volume
)) {
471 pa_memchunk vchunk
= *chunk
;
473 pa_memblock_ref(vchunk
.memblock
);
474 pa_memchunk_make_writable(&vchunk
, 0);
476 if (s
->thread_info
.soft_muted
|| pa_cvolume_is_muted(&s
->thread_info
.soft_volume
))
477 pa_silence_memchunk(&vchunk
, &s
->sample_spec
);
479 pa_volume_memchunk(&vchunk
, &s
->sample_spec
, &s
->thread_info
.soft_volume
);
481 pa_source_output_push(o
, &vchunk
);
483 pa_memblock_unref(vchunk
.memblock
);
485 pa_source_output_push(o
, chunk
);
488 /* Called from main thread */
489 pa_usec_t
pa_source_get_latency(pa_source
*s
) {
492 pa_source_assert_ref(s
);
493 pa_assert(PA_SOURCE_IS_LINKED(s
->state
));
495 if (!PA_SOURCE_IS_OPENED(s
->state
))
498 if (!(s
->flags
& PA_SOURCE_LATENCY
))
501 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SOURCE_MESSAGE_GET_LATENCY
, &usec
, 0, NULL
) == 0);
506 /* Called from main thread */
507 void pa_source_set_volume(pa_source
*s
, const pa_cvolume
*volume
) {
510 pa_source_assert_ref(s
);
511 pa_assert(PA_SOURCE_IS_LINKED(s
->state
));
514 changed
= !pa_cvolume_equal(volume
, &s
->volume
);
517 if (s
->set_volume
&& s
->set_volume(s
) < 0)
518 s
->set_volume
= NULL
;
521 pa_source_set_soft_volume(s
, volume
);
524 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SOURCE
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
527 /* Called from main thread */
528 void pa_source_set_soft_volume(pa_source
*s
, const pa_cvolume
*volume
) {
529 pa_source_assert_ref(s
);
532 if (PA_SOURCE_IS_LINKED(s
->state
))
533 pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SOURCE_MESSAGE_SET_VOLUME
, volume
, 0, NULL
);
535 s
->thread_info
.soft_volume
= *volume
;
538 /* Called from main thread */
539 const pa_cvolume
*pa_source_get_volume(pa_source
*s
, pa_bool_t force_refresh
) {
540 pa_source_assert_ref(s
);
541 pa_assert(PA_SOURCE_IS_LINKED(s
->state
));
543 if (s
->refresh_volume
|| force_refresh
) {
544 pa_cvolume old_volume
= s
->volume
;
546 if (s
->get_volume
&& s
->get_volume(s
) < 0)
547 s
->get_volume
= NULL
;
550 pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SOURCE_MESSAGE_GET_VOLUME
, &s
->volume
, 0, NULL
);
552 if (!pa_cvolume_equal(&old_volume
, &s
->volume
))
553 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SOURCE
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
559 /* Called from main thread */
560 void pa_source_set_mute(pa_source
*s
, pa_bool_t mute
) {
563 pa_source_assert_ref(s
);
564 pa_assert(PA_SOURCE_IS_LINKED(s
->state
));
566 changed
= s
->muted
!= mute
;
569 if (s
->set_mute
&& s
->set_mute(s
) < 0)
573 pa_asyncmsgq_post(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SOURCE_MESSAGE_SET_MUTE
, PA_UINT_TO_PTR(mute
), 0, NULL
, NULL
);
576 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SOURCE
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
579 /* Called from main thread */
580 pa_bool_t
pa_source_get_mute(pa_source
*s
, pa_bool_t force_refresh
) {
582 pa_source_assert_ref(s
);
583 pa_assert(PA_SOURCE_IS_LINKED(s
->state
));
585 if (s
->refresh_muted
|| force_refresh
) {
586 pa_bool_t old_muted
= s
->muted
;
588 if (s
->get_mute
&& s
->get_mute(s
) < 0)
592 pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SOURCE_MESSAGE_GET_MUTE
, &s
->muted
, 0, NULL
);
594 if (old_muted
!= s
->muted
)
595 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SOURCE
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
601 pa_bool_t
pa_source_update_proplist(pa_source
*s
, pa_update_mode_t mode
, pa_proplist
*p
) {
603 pa_source_assert_ref(s
);
605 pa_proplist_update(s
->proplist
, mode
, p
);
607 if (PA_SOURCE_IS_LINKED(s
->state
)) {
608 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SOURCE_PROPLIST_CHANGED
], s
);
609 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SOURCE
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
615 /* Called from main thread */
616 void pa_source_set_description(pa_source
*s
, const char *description
) {
618 pa_source_assert_ref(s
);
620 if (!description
&& !pa_proplist_contains(s
->proplist
, PA_PROP_DEVICE_DESCRIPTION
))
623 old
= pa_proplist_gets(s
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
625 if (old
&& description
&& !strcmp(old
, description
))
629 pa_proplist_sets(s
->proplist
, PA_PROP_DEVICE_DESCRIPTION
, description
);
631 pa_proplist_unset(s
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
633 if (PA_SOURCE_IS_LINKED(s
->state
)) {
634 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SOURCE
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
635 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SOURCE_PROPLIST_CHANGED
], s
);
639 /* Called from main thread */
640 unsigned pa_source_linked_by(pa_source
*s
) {
641 pa_source_assert_ref(s
);
642 pa_assert(PA_SOURCE_IS_LINKED(s
->state
));
644 return pa_idxset_size(s
->outputs
);
647 /* Called from main thread */
648 unsigned pa_source_used_by(pa_source
*s
) {
651 pa_source_assert_ref(s
);
652 pa_assert(PA_SOURCE_IS_LINKED(s
->state
));
654 ret
= pa_idxset_size(s
->outputs
);
655 pa_assert(ret
>= s
->n_corked
);
657 return ret
- s
->n_corked
;
660 /* Called from main thread */
661 unsigned pa_source_check_suspend(pa_source
*s
) {
666 pa_source_assert_ref(s
);
668 if (!PA_SOURCE_IS_LINKED(s
->state
))
673 for (o
= PA_SOURCE_OUTPUT(pa_idxset_first(s
->outputs
, &idx
)); o
; o
= PA_SOURCE_OUTPUT(pa_idxset_next(s
->outputs
, &idx
))) {
674 pa_source_output_state_t st
;
676 st
= pa_source_output_get_state(o
);
677 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(st
));
679 if (st
== PA_SOURCE_OUTPUT_CORKED
)
682 if (o
->flags
& PA_SOURCE_OUTPUT_DONT_INHIBIT_AUTO_SUSPEND
)
691 /* Called from IO thread, except when it is not */
692 int pa_source_process_msg(pa_msgobject
*object
, int code
, void *userdata
, int64_t offset
, pa_memchunk
*chunk
) {
693 pa_source
*s
= PA_SOURCE(object
);
694 pa_source_assert_ref(s
);
696 switch ((pa_source_message_t
) code
) {
698 case PA_SOURCE_MESSAGE_ADD_OUTPUT
: {
699 pa_source_output
*o
= PA_SOURCE_OUTPUT(userdata
);
701 pa_hashmap_put(s
->thread_info
.outputs
, PA_UINT32_TO_PTR(o
->index
), pa_source_output_ref(o
));
703 if (o
->direct_on_input
) {
704 o
->thread_info
.direct_on_input
= o
->direct_on_input
;
705 pa_hashmap_put(o
->thread_info
.direct_on_input
->thread_info
.direct_outputs
, PA_UINT32_TO_PTR(o
->index
), o
);
708 pa_assert(!o
->thread_info
.attached
);
709 o
->thread_info
.attached
= TRUE
;
714 pa_source_output_set_state_within_thread(o
, o
->state
);
716 if (o
->thread_info
.requested_source_latency
!= (pa_usec_t
) -1)
717 pa_source_output_set_requested_latency_within_thread(o
, o
->thread_info
.requested_source_latency
);
719 pa_source_output_update_max_rewind(o
, s
->thread_info
.max_rewind
);
721 /* We don't just invalidate the requested latency here,
722 * because if we are in a move we might need to fix up the
723 * requested latency. */
724 pa_source_output_set_requested_latency_within_thread(o
, o
->thread_info
.requested_source_latency
);
729 case PA_SOURCE_MESSAGE_REMOVE_OUTPUT
: {
730 pa_source_output
*o
= PA_SOURCE_OUTPUT(userdata
);
732 pa_source_output_set_state_within_thread(o
, o
->state
);
737 pa_assert(o
->thread_info
.attached
);
738 o
->thread_info
.attached
= FALSE
;
740 if (o
->thread_info
.direct_on_input
) {
741 pa_hashmap_remove(o
->thread_info
.direct_on_input
->thread_info
.direct_outputs
, PA_UINT32_TO_PTR(o
->index
));
742 o
->thread_info
.direct_on_input
= NULL
;
745 if (pa_hashmap_remove(s
->thread_info
.outputs
, PA_UINT32_TO_PTR(o
->index
)))
746 pa_source_output_unref(o
);
748 pa_source_invalidate_requested_latency(s
);
753 case PA_SOURCE_MESSAGE_SET_VOLUME
:
754 s
->thread_info
.soft_volume
= *((pa_cvolume
*) userdata
);
757 case PA_SOURCE_MESSAGE_SET_MUTE
:
758 s
->thread_info
.soft_muted
= PA_PTR_TO_UINT(userdata
);
761 case PA_SOURCE_MESSAGE_GET_VOLUME
:
762 *((pa_cvolume
*) userdata
) = s
->thread_info
.soft_volume
;
765 case PA_SOURCE_MESSAGE_GET_MUTE
:
766 *((pa_bool_t
*) userdata
) = s
->thread_info
.soft_muted
;
769 case PA_SOURCE_MESSAGE_SET_STATE
:
770 s
->thread_info
.state
= PA_PTR_TO_UINT(userdata
);
773 case PA_SOURCE_MESSAGE_DETACH
:
775 /* Detach all streams */
776 pa_source_detach_within_thread(s
);
779 case PA_SOURCE_MESSAGE_ATTACH
:
781 /* Reattach all streams */
782 pa_source_attach_within_thread(s
);
785 case PA_SOURCE_MESSAGE_GET_REQUESTED_LATENCY
: {
787 pa_usec_t
*usec
= userdata
;
788 *usec
= pa_source_get_requested_latency_within_thread(s
);
790 if (*usec
== (pa_usec_t
) -1)
791 *usec
= s
->thread_info
.max_latency
;
796 case PA_SOURCE_MESSAGE_SET_LATENCY_RANGE
: {
797 pa_usec_t
*r
= userdata
;
799 pa_source_update_latency_range(s
, r
[0], r
[1]);
804 case PA_SOURCE_MESSAGE_GET_LATENCY_RANGE
: {
805 pa_usec_t
*r
= userdata
;
807 r
[0] = s
->thread_info
.min_latency
;
808 r
[1] = s
->thread_info
.max_latency
;
813 case PA_SOURCE_MESSAGE_GET_MAX_REWIND
:
815 *((size_t*) userdata
) = s
->thread_info
.max_rewind
;
818 case PA_SOURCE_MESSAGE_GET_LATENCY
:
821 *((pa_usec_t
*) userdata
) = 0;
825 /* Implementors need to overwrite this implementation! */
828 case PA_SOURCE_MESSAGE_MAX
:
835 /* Called from main thread */
836 int pa_source_suspend_all(pa_core
*c
, pa_bool_t suspend
) {
841 pa_core_assert_ref(c
);
843 for (source
= PA_SOURCE(pa_idxset_first(c
->sources
, &idx
)); source
; source
= PA_SOURCE(pa_idxset_next(c
->sources
, &idx
)))
844 ret
-= pa_source_suspend(source
, suspend
) < 0;
849 /* Called from main thread */
850 void pa_source_detach(pa_source
*s
) {
851 pa_source_assert_ref(s
);
852 pa_assert(PA_SOURCE_IS_LINKED(s
->state
));
854 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SOURCE_MESSAGE_DETACH
, NULL
, 0, NULL
) == 0);
857 /* Called from main thread */
858 void pa_source_attach(pa_source
*s
) {
859 pa_source_assert_ref(s
);
860 pa_assert(PA_SOURCE_IS_LINKED(s
->state
));
862 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SOURCE_MESSAGE_ATTACH
, NULL
, 0, NULL
) == 0);
865 /* Called from IO thread */
866 void pa_source_detach_within_thread(pa_source
*s
) {
870 pa_source_assert_ref(s
);
871 pa_assert(PA_SOURCE_IS_LINKED(s
->thread_info
.state
));
873 while ((o
= pa_hashmap_iterate(s
->thread_info
.outputs
, &state
, NULL
)))
878 /* Called from IO thread */
879 void pa_source_attach_within_thread(pa_source
*s
) {
883 pa_source_assert_ref(s
);
884 pa_assert(PA_SOURCE_IS_LINKED(s
->thread_info
.state
));
886 while ((o
= pa_hashmap_iterate(s
->thread_info
.outputs
, &state
, NULL
)))
891 /* Called from IO thread */
892 pa_usec_t
pa_source_get_requested_latency_within_thread(pa_source
*s
) {
893 pa_usec_t result
= (pa_usec_t
) -1;
897 pa_source_assert_ref(s
);
899 if (s
->thread_info
.requested_latency_valid
)
900 return s
->thread_info
.requested_latency
;
902 while ((o
= pa_hashmap_iterate(s
->thread_info
.outputs
, &state
, NULL
)))
904 if (o
->thread_info
.requested_source_latency
!= (pa_usec_t
) -1 &&
905 (result
== (pa_usec_t
) -1 || result
> o
->thread_info
.requested_source_latency
))
906 result
= o
->thread_info
.requested_source_latency
;
908 if (result
!= (pa_usec_t
) -1) {
909 if (s
->thread_info
.max_latency
> 0 && result
> s
->thread_info
.max_latency
)
910 result
= s
->thread_info
.max_latency
;
912 if (s
->thread_info
.min_latency
> 0 && result
< s
->thread_info
.min_latency
)
913 result
= s
->thread_info
.min_latency
;
916 s
->thread_info
.requested_latency
= result
;
917 s
->thread_info
.requested_latency_valid
= TRUE
;
922 /* Called from main thread */
923 pa_usec_t
pa_source_get_requested_latency(pa_source
*s
) {
926 pa_source_assert_ref(s
);
927 pa_assert(PA_SOURCE_IS_LINKED(s
->state
));
929 if (!PA_SOURCE_IS_OPENED(s
->state
))
932 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SOURCE_MESSAGE_GET_REQUESTED_LATENCY
, &usec
, 0, NULL
) == 0);
937 /* Called from IO thread */
938 void pa_source_set_max_rewind(pa_source
*s
, size_t max_rewind
) {
942 pa_source_assert_ref(s
);
944 if (max_rewind
== s
->thread_info
.max_rewind
)
947 s
->thread_info
.max_rewind
= max_rewind
;
949 if (PA_SOURCE_IS_LINKED(s
->thread_info
.state
)) {
950 while ((o
= pa_hashmap_iterate(s
->thread_info
.outputs
, &state
, NULL
)))
951 pa_source_output_update_max_rewind(o
, s
->thread_info
.max_rewind
);
955 void pa_source_invalidate_requested_latency(pa_source
*s
) {
959 pa_source_assert_ref(s
);
961 s
->thread_info
.requested_latency_valid
= FALSE
;
963 if (s
->update_requested_latency
)
964 s
->update_requested_latency(s
);
966 while ((o
= pa_hashmap_iterate(s
->thread_info
.outputs
, &state
, NULL
)))
967 if (o
->update_source_requested_latency
)
968 o
->update_source_requested_latency(o
);
971 pa_sink_invalidate_requested_latency(s
->monitor_of
);
974 void pa_source_set_latency_range(pa_source
*s
, pa_usec_t min_latency
, pa_usec_t max_latency
) {
975 pa_source_assert_ref(s
);
977 /* min_latency == 0: no limit
978 * min_latency == (size_t) -1: default limit
979 * min_latency anything else: specified limit
981 * Similar for max_latency */
983 if (min_latency
== (pa_usec_t
) -1)
984 min_latency
= DEFAULT_MIN_LATENCY
;
986 if (max_latency
== (pa_usec_t
) -1)
987 max_latency
= min_latency
;
989 pa_assert(!min_latency
|| !max_latency
||
990 min_latency
<= max_latency
);
992 if (PA_SOURCE_IS_LINKED(s
->state
)) {
998 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SOURCE_MESSAGE_SET_LATENCY_RANGE
, r
, 0, NULL
) == 0);
1000 s
->thread_info
.min_latency
= min_latency
;
1001 s
->thread_info
.max_latency
= max_latency
;
1003 s
->thread_info
.requested_latency_valid
= FALSE
;
1007 void pa_source_get_latency_range(pa_source
*s
, pa_usec_t
*min_latency
, pa_usec_t
*max_latency
) {
1008 pa_source_assert_ref(s
);
1009 pa_assert(min_latency
);
1010 pa_assert(max_latency
);
1012 if (PA_SOURCE_IS_LINKED(s
->state
)) {
1013 pa_usec_t r
[2] = { 0, 0 };
1015 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SOURCE_MESSAGE_GET_LATENCY_RANGE
, r
, 0, NULL
) == 0);
1017 *min_latency
= r
[0];
1018 *max_latency
= r
[1];
1020 *min_latency
= s
->thread_info
.min_latency
;
1021 *max_latency
= s
->thread_info
.max_latency
;
1025 /* Called from IO thread */
1026 void pa_source_update_latency_range(pa_source
*s
, pa_usec_t min_latency
, pa_usec_t max_latency
) {
1027 pa_source_output
*o
;
1030 pa_source_assert_ref(s
);
1032 s
->thread_info
.min_latency
= min_latency
;
1033 s
->thread_info
.max_latency
= max_latency
;
1035 while ((o
= pa_hashmap_iterate(s
->thread_info
.outputs
, &state
, NULL
)))
1036 if (o
->update_source_latency_range
)
1037 o
->update_source_latency_range(o
);
1039 pa_source_invalidate_requested_latency(s
);
1042 size_t pa_source_get_max_rewind(pa_source
*s
) {
1044 pa_source_assert_ref(s
);
1046 if (!PA_SOURCE_IS_LINKED(s
->state
))
1047 return s
->thread_info
.max_rewind
;
1049 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SOURCE_MESSAGE_GET_MAX_REWIND
, &r
, 0, NULL
) == 0);