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
)
162 pa_proplist_update(data
->proplist
, PA_UPDATE_MERGE
, data
->card
->proplist
);
164 if (pa_hook_fire(&core
->hooks
[PA_CORE_HOOK_SOURCE_FIXATE
], data
) < 0) {
166 pa_namereg_unregister(core
, name
);
170 s
->parent
.parent
.free
= source_free
;
171 s
->parent
.process_msg
= pa_source_process_msg
;
174 s
->state
= PA_SOURCE_INIT
;
176 s
->name
= pa_xstrdup(name
);
177 s
->proplist
= pa_proplist_copy(data
->proplist
);
178 s
->driver
= pa_xstrdup(data
->driver
);
179 s
->module
= data
->module
;
180 s
->card
= data
->card
;
182 s
->sample_spec
= data
->sample_spec
;
183 s
->channel_map
= data
->channel_map
;
185 s
->outputs
= pa_idxset_new(NULL
, NULL
);
187 s
->monitor_of
= NULL
;
189 s
->volume
= data
->volume
;
190 s
->muted
= data
->muted
;
191 s
->refresh_volume
= s
->refresh_muted
= FALSE
;
192 s
->base_volume
= PA_VOLUME_NORM
;
200 pa_silence_memchunk_get(
201 &core
->silence_cache
,
207 s
->thread_info
.outputs
= pa_hashmap_new(pa_idxset_trivial_hash_func
, pa_idxset_trivial_compare_func
);
208 pa_cvolume_reset(&s
->thread_info
.soft_volume
, s
->sample_spec
.channels
);
209 s
->thread_info
.soft_muted
= FALSE
;
210 s
->thread_info
.state
= s
->state
;
211 s
->thread_info
.max_rewind
= 0;
212 s
->thread_info
.requested_latency_valid
= FALSE
;
213 s
->thread_info
.requested_latency
= 0;
214 s
->thread_info
.min_latency
= DEFAULT_MIN_LATENCY
;
215 s
->thread_info
.max_latency
= 0;
217 pa_assert_se(pa_idxset_put(core
->sources
, s
, &s
->index
) >= 0);
220 pa_assert_se(pa_idxset_put(s
->card
->sources
, s
, NULL
) >= 0);
222 pa_log_info("Created source %u \"%s\" with sample spec %s and channel map %s",
225 pa_sample_spec_snprint(st
, sizeof(st
), &s
->sample_spec
),
226 pa_channel_map_snprint(cm
, sizeof(cm
), &s
->channel_map
));
231 /* Called from main context */
232 static int source_set_state(pa_source
*s
, pa_source_state_t state
) {
234 pa_bool_t suspend_change
;
235 pa_source_state_t original_state
;
239 if (s
->state
== state
)
242 original_state
= s
->state
;
245 (original_state
== PA_SOURCE_SUSPENDED
&& PA_SOURCE_IS_OPENED(state
)) ||
246 (PA_SOURCE_IS_OPENED(original_state
) && state
== PA_SOURCE_SUSPENDED
);
249 if ((ret
= s
->set_state(s
, state
)) < 0)
253 if ((ret
= pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SOURCE_MESSAGE_SET_STATE
, PA_UINT_TO_PTR(state
), 0, NULL
)) < 0) {
256 s
->set_state(s
, original_state
);
263 if (suspend_change
) {
267 /* We're suspending or resuming, tell everyone about it */
269 for (o
= PA_SOURCE_OUTPUT(pa_idxset_first(s
->outputs
, &idx
)); o
; o
= PA_SOURCE_OUTPUT(pa_idxset_next(s
->outputs
, &idx
)))
271 o
->suspend(o
, state
== PA_SOURCE_SUSPENDED
);
274 if (state
!= PA_SOURCE_UNLINKED
) /* if we enter UNLINKED state pa_source_unlink() will fire the apropriate events */
275 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SOURCE_STATE_CHANGED
], s
);
280 /* Called from main context */
281 void pa_source_put(pa_source
*s
) {
282 pa_source_assert_ref(s
);
284 pa_assert(s
->state
== PA_SOURCE_INIT
);
286 /* The following fields must be initialized properly when calling _put() */
287 pa_assert(s
->asyncmsgq
);
288 pa_assert(s
->rtpoll
);
289 pa_assert(!s
->thread_info
.min_latency
|| !s
->thread_info
.max_latency
||
290 s
->thread_info
.min_latency
<= s
->thread_info
.max_latency
);
292 if (!(s
->flags
& PA_SOURCE_HW_VOLUME_CTRL
)) {
293 s
->flags
|= PA_SOURCE_DECIBEL_VOLUME
;
295 s
->thread_info
.soft_volume
= s
->volume
;
296 s
->thread_info
.soft_muted
= s
->muted
;
299 pa_assert_se(source_set_state(s
, PA_SOURCE_IDLE
) == 0);
301 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SOURCE
| PA_SUBSCRIPTION_EVENT_NEW
, s
->index
);
302 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SOURCE_PUT
], s
);
305 /* Called from main context */
306 void pa_source_unlink(pa_source
*s
) {
308 pa_source_output
*o
, *j
= NULL
;
312 /* See pa_sink_unlink() for a couple of comments how this function
315 linked
= PA_SOURCE_IS_LINKED(s
->state
);
318 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SOURCE_UNLINK
], s
);
320 if (s
->state
!= PA_SOURCE_UNLINKED
)
321 pa_namereg_unregister(s
->core
, s
->name
);
322 pa_idxset_remove_by_data(s
->core
->sources
, s
, NULL
);
325 pa_idxset_remove_by_data(s
->card
->sinks
, s
, NULL
);
327 while ((o
= pa_idxset_first(s
->outputs
, NULL
))) {
329 pa_source_output_kill(o
);
334 source_set_state(s
, PA_SOURCE_UNLINKED
);
336 s
->state
= PA_SOURCE_UNLINKED
;
341 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SOURCE
| PA_SUBSCRIPTION_EVENT_REMOVE
, s
->index
);
342 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SOURCE_UNLINK_POST
], s
);
346 /* Called from main context */
347 static void source_free(pa_object
*o
) {
348 pa_source_output
*so
;
349 pa_source
*s
= PA_SOURCE(o
);
352 pa_assert(pa_source_refcnt(s
) == 0);
354 if (PA_SOURCE_IS_LINKED(s
->state
))
357 pa_log_info("Freeing source %u \"%s\"", s
->index
, s
->name
);
359 pa_idxset_free(s
->outputs
, NULL
, NULL
);
361 while ((so
= pa_hashmap_steal_first(s
->thread_info
.outputs
)))
362 pa_source_output_unref(so
);
364 pa_hashmap_free(s
->thread_info
.outputs
, NULL
, NULL
);
366 if (s
->silence
.memblock
)
367 pa_memblock_unref(s
->silence
.memblock
);
373 pa_proplist_free(s
->proplist
);
378 /* Called from main context */
379 void pa_source_set_asyncmsgq(pa_source
*s
, pa_asyncmsgq
*q
) {
380 pa_source_assert_ref(s
);
385 /* Called from main context */
386 void pa_source_set_rtpoll(pa_source
*s
, pa_rtpoll
*p
) {
387 pa_source_assert_ref(s
);
392 /* Called from main context */
393 int pa_source_update_status(pa_source
*s
) {
394 pa_source_assert_ref(s
);
395 pa_assert(PA_SOURCE_IS_LINKED(s
->state
));
397 if (s
->state
== PA_SOURCE_SUSPENDED
)
400 return source_set_state(s
, pa_source_used_by(s
) ? PA_SOURCE_RUNNING
: PA_SOURCE_IDLE
);
403 /* Called from main context */
404 int pa_source_suspend(pa_source
*s
, pa_bool_t suspend
) {
405 pa_source_assert_ref(s
);
406 pa_assert(PA_SOURCE_IS_LINKED(s
->state
));
409 return source_set_state(s
, PA_SOURCE_SUSPENDED
);
411 return source_set_state(s
, pa_source_used_by(s
) ? PA_SOURCE_RUNNING
: PA_SOURCE_IDLE
);
414 /* Called from IO thread context */
415 void pa_source_process_rewind(pa_source
*s
, size_t nbytes
) {
419 pa_source_assert_ref(s
);
420 pa_assert(PA_SOURCE_IS_OPENED(s
->thread_info
.state
));
425 pa_log_debug("Processing rewind...");
427 while ((o
= pa_hashmap_iterate(s
->thread_info
.outputs
, &state
, NULL
))) {
428 pa_source_output_assert_ref(o
);
429 pa_source_output_process_rewind(o
, nbytes
);
433 /* Called from IO thread context */
434 void pa_source_post(pa_source
*s
, const pa_memchunk
*chunk
) {
438 pa_source_assert_ref(s
);
439 pa_assert(PA_SOURCE_IS_OPENED(s
->thread_info
.state
));
442 if (s
->thread_info
.soft_muted
|| !pa_cvolume_is_norm(&s
->thread_info
.soft_volume
)) {
443 pa_memchunk vchunk
= *chunk
;
445 pa_memblock_ref(vchunk
.memblock
);
446 pa_memchunk_make_writable(&vchunk
, 0);
448 if (s
->thread_info
.soft_muted
|| pa_cvolume_is_muted(&s
->thread_info
.soft_volume
))
449 pa_silence_memchunk(&vchunk
, &s
->sample_spec
);
451 pa_volume_memchunk(&vchunk
, &s
->sample_spec
, &s
->thread_info
.soft_volume
);
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
, &vchunk
);
460 pa_memblock_unref(vchunk
.memblock
);
463 while ((o
= pa_hashmap_iterate(s
->thread_info
.outputs
, &state
, NULL
))) {
464 pa_source_output_assert_ref(o
);
466 if (!o
->thread_info
.direct_on_input
)
467 pa_source_output_push(o
, chunk
);
472 /* Called from IO thread context */
473 void pa_source_post_direct(pa_source
*s
, pa_source_output
*o
, const pa_memchunk
*chunk
) {
474 pa_source_assert_ref(s
);
475 pa_assert(PA_SOURCE_IS_OPENED(s
->thread_info
.state
));
476 pa_source_output_assert_ref(o
);
477 pa_assert(o
->thread_info
.direct_on_input
);
480 if (s
->thread_info
.soft_muted
|| !pa_cvolume_is_norm(&s
->thread_info
.soft_volume
)) {
481 pa_memchunk vchunk
= *chunk
;
483 pa_memblock_ref(vchunk
.memblock
);
484 pa_memchunk_make_writable(&vchunk
, 0);
486 if (s
->thread_info
.soft_muted
|| pa_cvolume_is_muted(&s
->thread_info
.soft_volume
))
487 pa_silence_memchunk(&vchunk
, &s
->sample_spec
);
489 pa_volume_memchunk(&vchunk
, &s
->sample_spec
, &s
->thread_info
.soft_volume
);
491 pa_source_output_push(o
, &vchunk
);
493 pa_memblock_unref(vchunk
.memblock
);
495 pa_source_output_push(o
, chunk
);
498 /* Called from main thread */
499 pa_usec_t
pa_source_get_latency(pa_source
*s
) {
502 pa_source_assert_ref(s
);
503 pa_assert(PA_SOURCE_IS_LINKED(s
->state
));
505 if (!PA_SOURCE_IS_OPENED(s
->state
))
508 if (!(s
->flags
& PA_SOURCE_LATENCY
))
511 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SOURCE_MESSAGE_GET_LATENCY
, &usec
, 0, NULL
) == 0);
516 /* Called from main thread */
517 void pa_source_set_volume(pa_source
*s
, const pa_cvolume
*volume
) {
520 pa_source_assert_ref(s
);
521 pa_assert(PA_SOURCE_IS_LINKED(s
->state
));
524 changed
= !pa_cvolume_equal(volume
, &s
->volume
);
527 if (s
->set_volume
&& s
->set_volume(s
) < 0)
528 s
->set_volume
= NULL
;
531 pa_source_set_soft_volume(s
, volume
);
534 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SOURCE
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
537 /* Called from main thread */
538 void pa_source_set_soft_volume(pa_source
*s
, const pa_cvolume
*volume
) {
539 pa_source_assert_ref(s
);
542 if (PA_SOURCE_IS_LINKED(s
->state
))
543 pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SOURCE_MESSAGE_SET_VOLUME
, volume
, 0, NULL
);
545 s
->thread_info
.soft_volume
= *volume
;
548 /* Called from main thread */
549 const pa_cvolume
*pa_source_get_volume(pa_source
*s
, pa_bool_t force_refresh
) {
550 pa_source_assert_ref(s
);
551 pa_assert(PA_SOURCE_IS_LINKED(s
->state
));
553 if (s
->refresh_volume
|| force_refresh
) {
554 pa_cvolume old_volume
= s
->volume
;
556 if (s
->get_volume
&& s
->get_volume(s
) < 0)
557 s
->get_volume
= NULL
;
560 pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SOURCE_MESSAGE_GET_VOLUME
, &s
->volume
, 0, NULL
);
562 if (!pa_cvolume_equal(&old_volume
, &s
->volume
))
563 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SOURCE
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
569 /* Called from main thread */
570 void pa_source_set_mute(pa_source
*s
, pa_bool_t mute
) {
573 pa_source_assert_ref(s
);
574 pa_assert(PA_SOURCE_IS_LINKED(s
->state
));
576 changed
= s
->muted
!= mute
;
579 if (s
->set_mute
&& s
->set_mute(s
) < 0)
583 pa_asyncmsgq_post(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SOURCE_MESSAGE_SET_MUTE
, PA_UINT_TO_PTR(mute
), 0, NULL
, NULL
);
586 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SOURCE
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
589 /* Called from main thread */
590 pa_bool_t
pa_source_get_mute(pa_source
*s
, pa_bool_t force_refresh
) {
592 pa_source_assert_ref(s
);
593 pa_assert(PA_SOURCE_IS_LINKED(s
->state
));
595 if (s
->refresh_muted
|| force_refresh
) {
596 pa_bool_t old_muted
= s
->muted
;
598 if (s
->get_mute
&& s
->get_mute(s
) < 0)
602 pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SOURCE_MESSAGE_GET_MUTE
, &s
->muted
, 0, NULL
);
604 if (old_muted
!= s
->muted
)
605 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SOURCE
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
611 pa_bool_t
pa_source_update_proplist(pa_source
*s
, pa_update_mode_t mode
, pa_proplist
*p
) {
613 pa_source_assert_ref(s
);
615 pa_proplist_update(s
->proplist
, mode
, p
);
617 if (PA_SOURCE_IS_LINKED(s
->state
)) {
618 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SOURCE_PROPLIST_CHANGED
], s
);
619 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SOURCE
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
625 /* Called from main thread */
626 void pa_source_set_description(pa_source
*s
, const char *description
) {
628 pa_source_assert_ref(s
);
630 if (!description
&& !pa_proplist_contains(s
->proplist
, PA_PROP_DEVICE_DESCRIPTION
))
633 old
= pa_proplist_gets(s
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
635 if (old
&& description
&& !strcmp(old
, description
))
639 pa_proplist_sets(s
->proplist
, PA_PROP_DEVICE_DESCRIPTION
, description
);
641 pa_proplist_unset(s
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
643 if (PA_SOURCE_IS_LINKED(s
->state
)) {
644 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SOURCE
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
645 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SOURCE_PROPLIST_CHANGED
], s
);
649 /* Called from main thread */
650 unsigned pa_source_linked_by(pa_source
*s
) {
651 pa_source_assert_ref(s
);
652 pa_assert(PA_SOURCE_IS_LINKED(s
->state
));
654 return pa_idxset_size(s
->outputs
);
657 /* Called from main thread */
658 unsigned pa_source_used_by(pa_source
*s
) {
661 pa_source_assert_ref(s
);
662 pa_assert(PA_SOURCE_IS_LINKED(s
->state
));
664 ret
= pa_idxset_size(s
->outputs
);
665 pa_assert(ret
>= s
->n_corked
);
667 return ret
- s
->n_corked
;
670 /* Called from main thread */
671 unsigned pa_source_check_suspend(pa_source
*s
) {
676 pa_source_assert_ref(s
);
678 if (!PA_SOURCE_IS_LINKED(s
->state
))
683 for (o
= PA_SOURCE_OUTPUT(pa_idxset_first(s
->outputs
, &idx
)); o
; o
= PA_SOURCE_OUTPUT(pa_idxset_next(s
->outputs
, &idx
))) {
684 pa_source_output_state_t st
;
686 st
= pa_source_output_get_state(o
);
687 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(st
));
689 if (st
== PA_SOURCE_OUTPUT_CORKED
)
692 if (o
->flags
& PA_SOURCE_OUTPUT_DONT_INHIBIT_AUTO_SUSPEND
)
701 /* Called from IO thread, except when it is not */
702 int pa_source_process_msg(pa_msgobject
*object
, int code
, void *userdata
, int64_t offset
, pa_memchunk
*chunk
) {
703 pa_source
*s
= PA_SOURCE(object
);
704 pa_source_assert_ref(s
);
706 switch ((pa_source_message_t
) code
) {
708 case PA_SOURCE_MESSAGE_ADD_OUTPUT
: {
709 pa_source_output
*o
= PA_SOURCE_OUTPUT(userdata
);
711 pa_hashmap_put(s
->thread_info
.outputs
, PA_UINT32_TO_PTR(o
->index
), pa_source_output_ref(o
));
713 if (o
->direct_on_input
) {
714 o
->thread_info
.direct_on_input
= o
->direct_on_input
;
715 pa_hashmap_put(o
->thread_info
.direct_on_input
->thread_info
.direct_outputs
, PA_UINT32_TO_PTR(o
->index
), o
);
718 pa_assert(!o
->thread_info
.attached
);
719 o
->thread_info
.attached
= TRUE
;
724 pa_source_output_set_state_within_thread(o
, o
->state
);
726 if (o
->thread_info
.requested_source_latency
!= (pa_usec_t
) -1)
727 pa_source_output_set_requested_latency_within_thread(o
, o
->thread_info
.requested_source_latency
);
729 pa_source_output_update_max_rewind(o
, s
->thread_info
.max_rewind
);
731 /* We don't just invalidate the requested latency here,
732 * because if we are in a move we might need to fix up the
733 * requested latency. */
734 pa_source_output_set_requested_latency_within_thread(o
, o
->thread_info
.requested_source_latency
);
739 case PA_SOURCE_MESSAGE_REMOVE_OUTPUT
: {
740 pa_source_output
*o
= PA_SOURCE_OUTPUT(userdata
);
742 pa_source_output_set_state_within_thread(o
, o
->state
);
747 pa_assert(o
->thread_info
.attached
);
748 o
->thread_info
.attached
= FALSE
;
750 if (o
->thread_info
.direct_on_input
) {
751 pa_hashmap_remove(o
->thread_info
.direct_on_input
->thread_info
.direct_outputs
, PA_UINT32_TO_PTR(o
->index
));
752 o
->thread_info
.direct_on_input
= NULL
;
755 if (pa_hashmap_remove(s
->thread_info
.outputs
, PA_UINT32_TO_PTR(o
->index
)))
756 pa_source_output_unref(o
);
758 pa_source_invalidate_requested_latency(s
);
763 case PA_SOURCE_MESSAGE_SET_VOLUME
:
764 s
->thread_info
.soft_volume
= *((pa_cvolume
*) userdata
);
767 case PA_SOURCE_MESSAGE_SET_MUTE
:
768 s
->thread_info
.soft_muted
= PA_PTR_TO_UINT(userdata
);
771 case PA_SOURCE_MESSAGE_GET_VOLUME
:
772 *((pa_cvolume
*) userdata
) = s
->thread_info
.soft_volume
;
775 case PA_SOURCE_MESSAGE_GET_MUTE
:
776 *((pa_bool_t
*) userdata
) = s
->thread_info
.soft_muted
;
779 case PA_SOURCE_MESSAGE_SET_STATE
:
780 s
->thread_info
.state
= PA_PTR_TO_UINT(userdata
);
783 case PA_SOURCE_MESSAGE_DETACH
:
785 /* Detach all streams */
786 pa_source_detach_within_thread(s
);
789 case PA_SOURCE_MESSAGE_ATTACH
:
791 /* Reattach all streams */
792 pa_source_attach_within_thread(s
);
795 case PA_SOURCE_MESSAGE_GET_REQUESTED_LATENCY
: {
797 pa_usec_t
*usec
= userdata
;
798 *usec
= pa_source_get_requested_latency_within_thread(s
);
800 if (*usec
== (pa_usec_t
) -1)
801 *usec
= s
->thread_info
.max_latency
;
806 case PA_SOURCE_MESSAGE_SET_LATENCY_RANGE
: {
807 pa_usec_t
*r
= userdata
;
809 pa_source_update_latency_range(s
, r
[0], r
[1]);
814 case PA_SOURCE_MESSAGE_GET_LATENCY_RANGE
: {
815 pa_usec_t
*r
= userdata
;
817 r
[0] = s
->thread_info
.min_latency
;
818 r
[1] = s
->thread_info
.max_latency
;
823 case PA_SOURCE_MESSAGE_GET_MAX_REWIND
:
825 *((size_t*) userdata
) = s
->thread_info
.max_rewind
;
828 case PA_SOURCE_MESSAGE_GET_LATENCY
:
831 *((pa_usec_t
*) userdata
) = 0;
835 /* Implementors need to overwrite this implementation! */
838 case PA_SOURCE_MESSAGE_MAX
:
845 /* Called from main thread */
846 int pa_source_suspend_all(pa_core
*c
, pa_bool_t suspend
) {
851 pa_core_assert_ref(c
);
853 for (source
= PA_SOURCE(pa_idxset_first(c
->sources
, &idx
)); source
; source
= PA_SOURCE(pa_idxset_next(c
->sources
, &idx
)))
854 ret
-= pa_source_suspend(source
, suspend
) < 0;
859 /* Called from main thread */
860 void pa_source_detach(pa_source
*s
) {
861 pa_source_assert_ref(s
);
862 pa_assert(PA_SOURCE_IS_LINKED(s
->state
));
864 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SOURCE_MESSAGE_DETACH
, NULL
, 0, NULL
) == 0);
867 /* Called from main thread */
868 void pa_source_attach(pa_source
*s
) {
869 pa_source_assert_ref(s
);
870 pa_assert(PA_SOURCE_IS_LINKED(s
->state
));
872 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SOURCE_MESSAGE_ATTACH
, NULL
, 0, NULL
) == 0);
875 /* Called from IO thread */
876 void pa_source_detach_within_thread(pa_source
*s
) {
880 pa_source_assert_ref(s
);
881 pa_assert(PA_SOURCE_IS_LINKED(s
->thread_info
.state
));
883 while ((o
= pa_hashmap_iterate(s
->thread_info
.outputs
, &state
, NULL
)))
888 /* Called from IO thread */
889 void pa_source_attach_within_thread(pa_source
*s
) {
893 pa_source_assert_ref(s
);
894 pa_assert(PA_SOURCE_IS_LINKED(s
->thread_info
.state
));
896 while ((o
= pa_hashmap_iterate(s
->thread_info
.outputs
, &state
, NULL
)))
901 /* Called from IO thread */
902 pa_usec_t
pa_source_get_requested_latency_within_thread(pa_source
*s
) {
903 pa_usec_t result
= (pa_usec_t
) -1;
907 pa_source_assert_ref(s
);
909 if (s
->thread_info
.requested_latency_valid
)
910 return s
->thread_info
.requested_latency
;
912 while ((o
= pa_hashmap_iterate(s
->thread_info
.outputs
, &state
, NULL
)))
914 if (o
->thread_info
.requested_source_latency
!= (pa_usec_t
) -1 &&
915 (result
== (pa_usec_t
) -1 || result
> o
->thread_info
.requested_source_latency
))
916 result
= o
->thread_info
.requested_source_latency
;
918 if (result
!= (pa_usec_t
) -1) {
919 if (s
->thread_info
.max_latency
> 0 && result
> s
->thread_info
.max_latency
)
920 result
= s
->thread_info
.max_latency
;
922 if (s
->thread_info
.min_latency
> 0 && result
< s
->thread_info
.min_latency
)
923 result
= s
->thread_info
.min_latency
;
926 s
->thread_info
.requested_latency
= result
;
927 s
->thread_info
.requested_latency_valid
= TRUE
;
932 /* Called from main thread */
933 pa_usec_t
pa_source_get_requested_latency(pa_source
*s
) {
936 pa_source_assert_ref(s
);
937 pa_assert(PA_SOURCE_IS_LINKED(s
->state
));
939 if (!PA_SOURCE_IS_OPENED(s
->state
))
942 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SOURCE_MESSAGE_GET_REQUESTED_LATENCY
, &usec
, 0, NULL
) == 0);
947 /* Called from IO thread */
948 void pa_source_set_max_rewind(pa_source
*s
, size_t max_rewind
) {
952 pa_source_assert_ref(s
);
954 if (max_rewind
== s
->thread_info
.max_rewind
)
957 s
->thread_info
.max_rewind
= max_rewind
;
959 if (PA_SOURCE_IS_LINKED(s
->thread_info
.state
)) {
960 while ((o
= pa_hashmap_iterate(s
->thread_info
.outputs
, &state
, NULL
)))
961 pa_source_output_update_max_rewind(o
, s
->thread_info
.max_rewind
);
965 void pa_source_invalidate_requested_latency(pa_source
*s
) {
969 pa_source_assert_ref(s
);
971 s
->thread_info
.requested_latency_valid
= FALSE
;
973 if (s
->update_requested_latency
)
974 s
->update_requested_latency(s
);
976 while ((o
= pa_hashmap_iterate(s
->thread_info
.outputs
, &state
, NULL
)))
977 if (o
->update_source_requested_latency
)
978 o
->update_source_requested_latency(o
);
981 pa_sink_invalidate_requested_latency(s
->monitor_of
);
984 void pa_source_set_latency_range(pa_source
*s
, pa_usec_t min_latency
, pa_usec_t max_latency
) {
985 pa_source_assert_ref(s
);
987 /* min_latency == 0: no limit
988 * min_latency == (size_t) -1: default limit
989 * min_latency anything else: specified limit
991 * Similar for max_latency */
993 if (min_latency
== (pa_usec_t
) -1)
994 min_latency
= DEFAULT_MIN_LATENCY
;
996 if (max_latency
== (pa_usec_t
) -1)
997 max_latency
= min_latency
;
999 pa_assert(!min_latency
|| !max_latency
||
1000 min_latency
<= max_latency
);
1002 if (PA_SOURCE_IS_LINKED(s
->state
)) {
1008 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SOURCE_MESSAGE_SET_LATENCY_RANGE
, r
, 0, NULL
) == 0);
1010 s
->thread_info
.min_latency
= min_latency
;
1011 s
->thread_info
.max_latency
= max_latency
;
1013 s
->thread_info
.requested_latency_valid
= FALSE
;
1017 void pa_source_get_latency_range(pa_source
*s
, pa_usec_t
*min_latency
, pa_usec_t
*max_latency
) {
1018 pa_source_assert_ref(s
);
1019 pa_assert(min_latency
);
1020 pa_assert(max_latency
);
1022 if (PA_SOURCE_IS_LINKED(s
->state
)) {
1023 pa_usec_t r
[2] = { 0, 0 };
1025 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SOURCE_MESSAGE_GET_LATENCY_RANGE
, r
, 0, NULL
) == 0);
1027 *min_latency
= r
[0];
1028 *max_latency
= r
[1];
1030 *min_latency
= s
->thread_info
.min_latency
;
1031 *max_latency
= s
->thread_info
.max_latency
;
1035 /* Called from IO thread */
1036 void pa_source_update_latency_range(pa_source
*s
, pa_usec_t min_latency
, pa_usec_t max_latency
) {
1037 pa_source_output
*o
;
1040 pa_source_assert_ref(s
);
1042 s
->thread_info
.min_latency
= min_latency
;
1043 s
->thread_info
.max_latency
= max_latency
;
1045 while ((o
= pa_hashmap_iterate(s
->thread_info
.outputs
, &state
, NULL
)))
1046 if (o
->update_source_latency_range
)
1047 o
->update_source_latency_range(o
);
1049 pa_source_invalidate_requested_latency(s
);
1052 size_t pa_source_get_max_rewind(pa_source
*s
) {
1054 pa_source_assert_ref(s
);
1056 if (!PA_SOURCE_IS_LINKED(s
->state
))
1057 return s
->thread_info
.max_rewind
;
1059 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SOURCE_MESSAGE_GET_MAX_REWIND
, &r
, 0, NULL
) == 0);