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>
34 #include <pulse/util.h>
36 #include <pulsecore/source-output.h>
37 #include <pulsecore/namereg.h>
38 #include <pulsecore/core-subscribe.h>
39 #include <pulsecore/log.h>
40 #include <pulsecore/sample-util.h>
44 #define DEFAULT_MIN_LATENCY (4*PA_USEC_PER_MSEC)
46 static PA_DEFINE_CHECK_TYPE(pa_source, pa_msgobject);
48 static void source_free(pa_object *o);
50 pa_source_new_data* pa_source_new_data_init(pa_source_new_data *data) {
53 memset(data, 0, sizeof(*data));
54 data->proplist = pa_proplist_new();
59 void pa_source_new_data_set_name(pa_source_new_data *data, const char *name) {
63 data->name = pa_xstrdup(name);
66 void pa_source_new_data_set_sample_spec(pa_source_new_data *data, const pa_sample_spec *spec) {
69 if ((data->sample_spec_is_set = !!spec))
70 data->sample_spec = *spec;
73 void pa_source_new_data_set_channel_map(pa_source_new_data *data, const pa_channel_map *map) {
76 if ((data->channel_map_is_set = !!map))
77 data->channel_map = *map;
80 void pa_source_new_data_set_volume(pa_source_new_data *data, const pa_cvolume *volume) {
83 if ((data->volume_is_set = !!volume))
84 data->volume = *volume;
87 void pa_source_new_data_set_muted(pa_source_new_data *data, pa_bool_t mute) {
90 data->muted_is_set = TRUE;
94 void pa_source_new_data_done(pa_source_new_data *data) {
98 pa_proplist_free(data->proplist);
101 /* Called from main context */
102 static void reset_callbacks(pa_source *s) {
106 s->get_volume = NULL;
107 s->set_volume = NULL;
110 s->update_requested_latency = NULL;
113 /* Called from main context */
114 pa_source* pa_source_new(
116 pa_source_new_data *data,
117 pa_source_flags_t flags) {
121 char st[PA_SAMPLE_SPEC_SNPRINT_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX];
126 pa_assert(data->name);
128 s = pa_msgobject_new(pa_source);
130 if (!(name = pa_namereg_register(core, data->name, PA_NAMEREG_SOURCE, s, data->namereg_fail))) {
135 pa_source_new_data_set_name(data, name);
137 if (pa_hook_fire(&core->hooks[PA_CORE_HOOK_SOURCE_NEW], data) < 0) {
139 pa_namereg_unregister(core, name);
143 pa_return_null_if_fail(!data->driver || pa_utf8_valid(data->driver));
144 pa_return_null_if_fail(data->name && pa_utf8_valid(data->name) && data->name[0]);
146 pa_return_null_if_fail(data->sample_spec_is_set && pa_sample_spec_valid(&data->sample_spec));
148 if (!data->channel_map_is_set)
149 pa_return_null_if_fail(pa_channel_map_init_auto(&data->channel_map, data->sample_spec.channels, PA_CHANNEL_MAP_DEFAULT));
151 pa_return_null_if_fail(pa_channel_map_valid(&data->channel_map));
152 pa_return_null_if_fail(data->channel_map.channels == data->sample_spec.channels);
154 if (!data->volume_is_set)
155 pa_cvolume_reset(&data->volume, data->sample_spec.channels);
157 pa_return_null_if_fail(pa_cvolume_valid(&data->volume));
158 pa_return_null_if_fail(data->volume.channels == data->sample_spec.channels);
160 if (!data->muted_is_set)
164 pa_proplist_update(data->proplist, PA_UPDATE_MERGE, data->card->proplist);
166 if (pa_hook_fire(&core->hooks[PA_CORE_HOOK_SOURCE_FIXATE], data) < 0) {
168 pa_namereg_unregister(core, name);
172 s->parent.parent.free = source_free;
173 s->parent.process_msg = pa_source_process_msg;
176 s->state = PA_SOURCE_INIT;
178 s->name = pa_xstrdup(name);
179 s->proplist = pa_proplist_copy(data->proplist);
180 s->driver = pa_xstrdup(pa_path_get_filename(data->driver));
181 s->module = data->module;
182 s->card = data->card;
184 s->sample_spec = data->sample_spec;
185 s->channel_map = data->channel_map;
187 s->outputs = pa_idxset_new(NULL, NULL);
189 s->monitor_of = NULL;
191 s->virtual_volume = data->volume;
192 pa_cvolume_reset(&s->soft_volume, s->sample_spec.channels);
193 s->base_volume = PA_VOLUME_NORM;
194 s->n_volume_steps = PA_VOLUME_NORM+1;
195 s->muted = data->muted;
196 s->refresh_volume = s->refresh_muted = FALSE;
204 pa_silence_memchunk_get(
205 &core->silence_cache,
211 s->thread_info.outputs = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
212 s->thread_info.soft_volume = s->soft_volume;
213 s->thread_info.soft_muted = s->muted;
214 s->thread_info.state = s->state;
215 s->thread_info.max_rewind = 0;
216 s->thread_info.requested_latency_valid = FALSE;
217 s->thread_info.requested_latency = 0;
218 s->thread_info.min_latency = DEFAULT_MIN_LATENCY;
219 s->thread_info.max_latency = 0;
221 pa_assert_se(pa_idxset_put(core->sources, s, &s->index) >= 0);
224 pa_assert_se(pa_idxset_put(s->card->sources, s, NULL) >= 0);
226 pt = pa_proplist_to_string_sep(s->proplist, "\n ");
227 pa_log_info("Created source %u \"%s\" with sample spec %s and channel map %s\n %s",
230 pa_sample_spec_snprint(st, sizeof(st), &s->sample_spec),
231 pa_channel_map_snprint(cm, sizeof(cm), &s->channel_map),
238 /* Called from main context */
239 static int source_set_state(pa_source *s, pa_source_state_t state) {
241 pa_bool_t suspend_change;
242 pa_source_state_t original_state;
246 if (s->state == state)
249 original_state = s->state;
252 (original_state == PA_SOURCE_SUSPENDED && PA_SOURCE_IS_OPENED(state)) ||
253 (PA_SOURCE_IS_OPENED(original_state) && state == PA_SOURCE_SUSPENDED);
256 if ((ret = s->set_state(s, state)) < 0)
260 if ((ret = pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_SET_STATE, PA_UINT_TO_PTR(state), 0, NULL)) < 0) {
263 s->set_state(s, original_state);
270 if (suspend_change) {
274 /* We're suspending or resuming, tell everyone about it */
276 for (o = PA_SOURCE_OUTPUT(pa_idxset_first(s->outputs, &idx)); o; o = PA_SOURCE_OUTPUT(pa_idxset_next(s->outputs, &idx)))
278 o->suspend(o, state == PA_SOURCE_SUSPENDED);
281 if (state != PA_SOURCE_UNLINKED) /* if we enter UNLINKED state pa_source_unlink() will fire the apropriate events */
282 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SOURCE_STATE_CHANGED], s);
287 /* Called from main context */
288 void pa_source_put(pa_source *s) {
289 pa_source_assert_ref(s);
291 pa_assert(s->state == PA_SOURCE_INIT);
293 /* The following fields must be initialized properly when calling _put() */
294 pa_assert(s->asyncmsgq);
295 pa_assert(s->rtpoll);
296 pa_assert(!s->thread_info.min_latency || !s->thread_info.max_latency ||
297 s->thread_info.min_latency <= s->thread_info.max_latency);
299 if (!(s->flags & PA_SOURCE_HW_VOLUME_CTRL)) {
300 s->flags |= PA_SOURCE_DECIBEL_VOLUME;
302 s->thread_info.soft_volume = s->soft_volume;
303 s->thread_info.soft_muted = s->muted;
306 if (s->flags & PA_SOURCE_DECIBEL_VOLUME)
307 s->n_volume_steps = PA_VOLUME_NORM+1;
309 pa_assert_se(source_set_state(s, PA_SOURCE_IDLE) == 0);
311 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE | PA_SUBSCRIPTION_EVENT_NEW, s->index);
312 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SOURCE_PUT], s);
315 /* Called from main context */
316 void pa_source_unlink(pa_source *s) {
318 pa_source_output *o, *j = NULL;
322 /* See pa_sink_unlink() for a couple of comments how this function
325 linked = PA_SOURCE_IS_LINKED(s->state);
328 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SOURCE_UNLINK], s);
330 if (s->state != PA_SOURCE_UNLINKED)
331 pa_namereg_unregister(s->core, s->name);
332 pa_idxset_remove_by_data(s->core->sources, s, NULL);
335 pa_idxset_remove_by_data(s->card->sources, s, NULL);
337 while ((o = pa_idxset_first(s->outputs, NULL))) {
339 pa_source_output_kill(o);
344 source_set_state(s, PA_SOURCE_UNLINKED);
346 s->state = PA_SOURCE_UNLINKED;
351 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE | PA_SUBSCRIPTION_EVENT_REMOVE, s->index);
352 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SOURCE_UNLINK_POST], s);
356 /* Called from main context */
357 static void source_free(pa_object *o) {
358 pa_source_output *so;
359 pa_source *s = PA_SOURCE(o);
362 pa_assert(pa_source_refcnt(s) == 0);
364 if (PA_SOURCE_IS_LINKED(s->state))
367 pa_log_info("Freeing source %u \"%s\"", s->index, s->name);
369 pa_idxset_free(s->outputs, NULL, NULL);
371 while ((so = pa_hashmap_steal_first(s->thread_info.outputs)))
372 pa_source_output_unref(so);
374 pa_hashmap_free(s->thread_info.outputs, NULL, NULL);
376 if (s->silence.memblock)
377 pa_memblock_unref(s->silence.memblock);
383 pa_proplist_free(s->proplist);
388 /* Called from main context */
389 void pa_source_set_asyncmsgq(pa_source *s, pa_asyncmsgq *q) {
390 pa_source_assert_ref(s);
395 /* Called from main context */
396 void pa_source_set_rtpoll(pa_source *s, pa_rtpoll *p) {
397 pa_source_assert_ref(s);
402 /* Called from main context */
403 int pa_source_update_status(pa_source*s) {
404 pa_source_assert_ref(s);
405 pa_assert(PA_SOURCE_IS_LINKED(s->state));
407 if (s->state == PA_SOURCE_SUSPENDED)
410 return source_set_state(s, pa_source_used_by(s) ? PA_SOURCE_RUNNING : PA_SOURCE_IDLE);
413 /* Called from main context */
414 int pa_source_suspend(pa_source *s, pa_bool_t suspend) {
415 pa_source_assert_ref(s);
416 pa_assert(PA_SOURCE_IS_LINKED(s->state));
419 return source_set_state(s, PA_SOURCE_SUSPENDED);
421 return source_set_state(s, pa_source_used_by(s) ? PA_SOURCE_RUNNING : PA_SOURCE_IDLE);
424 /* Called from main context */
425 pa_queue *pa_source_move_all_start(pa_source *s) {
427 pa_source_output *o, *n;
430 pa_source_assert_ref(s);
431 pa_assert(PA_SOURCE_IS_LINKED(s->state));
435 for (o = PA_SOURCE_OUTPUT(pa_idxset_first(s->outputs, &idx)); o; o = n) {
436 n = PA_SOURCE_OUTPUT(pa_idxset_next(s->outputs, &idx));
438 if (pa_source_output_start_move(o) >= 0)
439 pa_queue_push(q, pa_source_output_ref(o));
445 /* Called from main context */
446 void pa_source_move_all_finish(pa_source *s, pa_queue *q, pa_bool_t save) {
449 pa_source_assert_ref(s);
450 pa_assert(PA_SOURCE_IS_LINKED(s->state));
453 while ((o = PA_SOURCE_OUTPUT(pa_queue_pop(q)))) {
454 if (pa_source_output_finish_move(o, s, save) < 0)
455 pa_source_output_unlink(o);
457 pa_source_output_unref(o);
460 pa_queue_free(q, NULL, NULL);
463 /* Called from main context */
464 void pa_source_move_all_fail(pa_queue *q) {
468 while ((o = PA_SOURCE_OUTPUT(pa_queue_pop(q)))) {
469 pa_source_output_unlink(o);
470 pa_source_output_unref(o);
473 pa_queue_free(q, NULL, NULL);
476 /* Called from IO thread context */
477 void pa_source_process_rewind(pa_source *s, size_t nbytes) {
481 pa_source_assert_ref(s);
482 pa_assert(PA_SOURCE_IS_LINKED(s->thread_info.state));
484 if (s->thread_info.state == PA_SOURCE_SUSPENDED)
490 pa_log_debug("Processing rewind...");
492 while ((o = pa_hashmap_iterate(s->thread_info.outputs, &state, NULL))) {
493 pa_source_output_assert_ref(o);
494 pa_source_output_process_rewind(o, nbytes);
498 /* Called from IO thread context */
499 void pa_source_post(pa_source*s, const pa_memchunk *chunk) {
503 pa_source_assert_ref(s);
504 pa_assert(PA_SOURCE_IS_LINKED(s->thread_info.state));
507 if (s->thread_info.state == PA_SOURCE_SUSPENDED)
510 if (s->thread_info.soft_muted || !pa_cvolume_is_norm(&s->thread_info.soft_volume)) {
511 pa_memchunk vchunk = *chunk;
513 pa_memblock_ref(vchunk.memblock);
514 pa_memchunk_make_writable(&vchunk, 0);
516 if (s->thread_info.soft_muted || pa_cvolume_is_muted(&s->thread_info.soft_volume))
517 pa_silence_memchunk(&vchunk, &s->sample_spec);
519 pa_volume_memchunk(&vchunk, &s->sample_spec, &s->thread_info.soft_volume);
521 while ((o = pa_hashmap_iterate(s->thread_info.outputs, &state, NULL))) {
522 pa_source_output_assert_ref(o);
524 if (!o->thread_info.direct_on_input)
525 pa_source_output_push(o, &vchunk);
528 pa_memblock_unref(vchunk.memblock);
531 while ((o = pa_hashmap_iterate(s->thread_info.outputs, &state, NULL))) {
532 pa_source_output_assert_ref(o);
534 if (!o->thread_info.direct_on_input)
535 pa_source_output_push(o, chunk);
540 /* Called from IO thread context */
541 void pa_source_post_direct(pa_source*s, pa_source_output *o, const pa_memchunk *chunk) {
542 pa_source_assert_ref(s);
543 pa_assert(PA_SOURCE_IS_LINKED(s->thread_info.state));
544 pa_source_output_assert_ref(o);
545 pa_assert(o->thread_info.direct_on_input);
548 if (s->thread_info.state == PA_SOURCE_SUSPENDED)
551 if (s->thread_info.soft_muted || !pa_cvolume_is_norm(&s->thread_info.soft_volume)) {
552 pa_memchunk vchunk = *chunk;
554 pa_memblock_ref(vchunk.memblock);
555 pa_memchunk_make_writable(&vchunk, 0);
557 if (s->thread_info.soft_muted || pa_cvolume_is_muted(&s->thread_info.soft_volume))
558 pa_silence_memchunk(&vchunk, &s->sample_spec);
560 pa_volume_memchunk(&vchunk, &s->sample_spec, &s->thread_info.soft_volume);
562 pa_source_output_push(o, &vchunk);
564 pa_memblock_unref(vchunk.memblock);
566 pa_source_output_push(o, chunk);
569 /* Called from main thread */
570 pa_usec_t pa_source_get_latency(pa_source *s) {
573 pa_source_assert_ref(s);
574 pa_assert(PA_SOURCE_IS_LINKED(s->state));
576 if (s->state == PA_SOURCE_SUSPENDED)
579 if (!(s->flags & PA_SOURCE_LATENCY))
582 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_GET_LATENCY, &usec, 0, NULL) == 0);
587 /* Called from main thread */
588 void pa_source_set_volume(pa_source *s, const pa_cvolume *volume) {
589 pa_cvolume old_virtual_volume;
590 pa_bool_t virtual_volume_changed;
592 pa_source_assert_ref(s);
593 pa_assert(PA_SOURCE_IS_LINKED(s->state));
595 pa_assert(pa_cvolume_valid(volume));
596 pa_assert(pa_cvolume_compatible(volume, &s->sample_spec));
598 old_virtual_volume = s->virtual_volume;
599 s->virtual_volume = *volume;
600 virtual_volume_changed = !pa_cvolume_equal(&old_virtual_volume, &s->virtual_volume);
603 pa_cvolume_reset(&s->soft_volume, s->sample_spec.channels);
606 s->soft_volume = s->virtual_volume;
608 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_SET_VOLUME, NULL, 0, NULL) == 0);
610 if (virtual_volume_changed)
611 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
614 /* Called from main thread. Only to be called by source implementor */
615 void pa_source_set_soft_volume(pa_source *s, const pa_cvolume *volume) {
616 pa_source_assert_ref(s);
619 if (PA_SOURCE_IS_LINKED(s->state))
620 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_SET_VOLUME, NULL, 0, NULL) == 0);
622 s->thread_info.soft_volume = *volume;
625 /* Called from main thread */
626 const pa_cvolume *pa_source_get_volume(pa_source *s, pa_bool_t force_refresh) {
627 pa_source_assert_ref(s);
628 pa_assert(PA_SOURCE_IS_LINKED(s->state));
630 if (s->refresh_volume || force_refresh) {
631 pa_cvolume old_virtual_volume = s->virtual_volume;
636 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_GET_VOLUME, NULL, 0, NULL) == 0);
638 if (!pa_cvolume_equal(&old_virtual_volume, &s->virtual_volume))
639 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
642 return &s->virtual_volume;
645 /* Called from main thread */
646 void pa_source_set_mute(pa_source *s, pa_bool_t mute) {
649 pa_source_assert_ref(s);
650 pa_assert(PA_SOURCE_IS_LINKED(s->state));
652 old_muted = s->muted;
658 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_SET_MUTE, NULL, 0, NULL) == 0);
660 if (old_muted != s->muted)
661 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
664 /* Called from main thread */
665 pa_bool_t pa_source_get_mute(pa_source *s, pa_bool_t force_refresh) {
667 pa_source_assert_ref(s);
668 pa_assert(PA_SOURCE_IS_LINKED(s->state));
670 if (s->refresh_muted || force_refresh) {
671 pa_bool_t old_muted = s->muted;
676 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_GET_MUTE, NULL, 0, NULL) == 0);
678 if (old_muted != s->muted)
679 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
685 /* Called from main thread */
686 pa_bool_t pa_source_update_proplist(pa_source *s, pa_update_mode_t mode, pa_proplist *p) {
687 pa_source_assert_ref(s);
690 pa_proplist_update(s->proplist, mode, p);
692 if (PA_SOURCE_IS_LINKED(s->state)) {
693 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SOURCE_PROPLIST_CHANGED], s);
694 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
700 /* Called from main thread */
701 void pa_source_set_description(pa_source *s, const char *description) {
703 pa_source_assert_ref(s);
705 if (!description && !pa_proplist_contains(s->proplist, PA_PROP_DEVICE_DESCRIPTION))
708 old = pa_proplist_gets(s->proplist, PA_PROP_DEVICE_DESCRIPTION);
710 if (old && description && !strcmp(old, description))
714 pa_proplist_sets(s->proplist, PA_PROP_DEVICE_DESCRIPTION, description);
716 pa_proplist_unset(s->proplist, PA_PROP_DEVICE_DESCRIPTION);
718 if (PA_SOURCE_IS_LINKED(s->state)) {
719 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
720 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SOURCE_PROPLIST_CHANGED], s);
724 /* Called from main thread */
725 unsigned pa_source_linked_by(pa_source *s) {
726 pa_source_assert_ref(s);
727 pa_assert(PA_SOURCE_IS_LINKED(s->state));
729 return pa_idxset_size(s->outputs);
732 /* Called from main thread */
733 unsigned pa_source_used_by(pa_source *s) {
736 pa_source_assert_ref(s);
737 pa_assert(PA_SOURCE_IS_LINKED(s->state));
739 ret = pa_idxset_size(s->outputs);
740 pa_assert(ret >= s->n_corked);
742 return ret - s->n_corked;
745 /* Called from main thread */
746 unsigned pa_source_check_suspend(pa_source *s) {
751 pa_source_assert_ref(s);
753 if (!PA_SOURCE_IS_LINKED(s->state))
758 for (o = PA_SOURCE_OUTPUT(pa_idxset_first(s->outputs, &idx)); o; o = PA_SOURCE_OUTPUT(pa_idxset_next(s->outputs, &idx))) {
759 pa_source_output_state_t st;
761 st = pa_source_output_get_state(o);
762 pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(st));
764 if (st == PA_SOURCE_OUTPUT_CORKED)
767 if (o->flags & PA_SOURCE_OUTPUT_DONT_INHIBIT_AUTO_SUSPEND)
776 /* Called from IO thread, except when it is not */
777 int pa_source_process_msg(pa_msgobject *object, int code, void *userdata, int64_t offset, pa_memchunk *chunk) {
778 pa_source *s = PA_SOURCE(object);
779 pa_source_assert_ref(s);
781 switch ((pa_source_message_t) code) {
783 case PA_SOURCE_MESSAGE_ADD_OUTPUT: {
784 pa_source_output *o = PA_SOURCE_OUTPUT(userdata);
786 pa_hashmap_put(s->thread_info.outputs, PA_UINT32_TO_PTR(o->index), pa_source_output_ref(o));
788 if (o->direct_on_input) {
789 o->thread_info.direct_on_input = o->direct_on_input;
790 pa_hashmap_put(o->thread_info.direct_on_input->thread_info.direct_outputs, PA_UINT32_TO_PTR(o->index), o);
793 pa_assert(!o->thread_info.attached);
794 o->thread_info.attached = TRUE;
799 pa_source_output_set_state_within_thread(o, o->state);
801 if (o->thread_info.requested_source_latency != (pa_usec_t) -1)
802 pa_source_output_set_requested_latency_within_thread(o, o->thread_info.requested_source_latency);
804 pa_source_output_update_max_rewind(o, s->thread_info.max_rewind);
806 /* We don't just invalidate the requested latency here,
807 * because if we are in a move we might need to fix up the
808 * requested latency. */
809 pa_source_output_set_requested_latency_within_thread(o, o->thread_info.requested_source_latency);
814 case PA_SOURCE_MESSAGE_REMOVE_OUTPUT: {
815 pa_source_output *o = PA_SOURCE_OUTPUT(userdata);
817 pa_source_output_set_state_within_thread(o, o->state);
822 pa_assert(o->thread_info.attached);
823 o->thread_info.attached = FALSE;
825 if (o->thread_info.direct_on_input) {
826 pa_hashmap_remove(o->thread_info.direct_on_input->thread_info.direct_outputs, PA_UINT32_TO_PTR(o->index));
827 o->thread_info.direct_on_input = NULL;
830 if (pa_hashmap_remove(s->thread_info.outputs, PA_UINT32_TO_PTR(o->index)))
831 pa_source_output_unref(o);
833 pa_source_invalidate_requested_latency(s);
838 case PA_SOURCE_MESSAGE_SET_VOLUME:
839 s->thread_info.soft_volume = s->soft_volume;
842 case PA_SOURCE_MESSAGE_GET_VOLUME:
845 case PA_SOURCE_MESSAGE_SET_MUTE:
846 s->thread_info.soft_muted = s->muted;
849 case PA_SOURCE_MESSAGE_GET_MUTE:
852 case PA_SOURCE_MESSAGE_SET_STATE:
853 s->thread_info.state = PA_PTR_TO_UINT(userdata);
856 case PA_SOURCE_MESSAGE_DETACH:
858 /* Detach all streams */
859 pa_source_detach_within_thread(s);
862 case PA_SOURCE_MESSAGE_ATTACH:
864 /* Reattach all streams */
865 pa_source_attach_within_thread(s);
868 case PA_SOURCE_MESSAGE_GET_REQUESTED_LATENCY: {
870 pa_usec_t *usec = userdata;
871 *usec = pa_source_get_requested_latency_within_thread(s);
873 if (*usec == (pa_usec_t) -1)
874 *usec = s->thread_info.max_latency;
879 case PA_SOURCE_MESSAGE_SET_LATENCY_RANGE: {
880 pa_usec_t *r = userdata;
882 pa_source_update_latency_range(s, r[0], r[1]);
887 case PA_SOURCE_MESSAGE_GET_LATENCY_RANGE: {
888 pa_usec_t *r = userdata;
890 r[0] = s->thread_info.min_latency;
891 r[1] = s->thread_info.max_latency;
896 case PA_SOURCE_MESSAGE_GET_MAX_REWIND:
898 *((size_t*) userdata) = s->thread_info.max_rewind;
901 case PA_SOURCE_MESSAGE_GET_LATENCY:
904 *((pa_usec_t*) userdata) = 0;
908 /* Implementors need to overwrite this implementation! */
911 case PA_SOURCE_MESSAGE_MAX:
918 /* Called from main thread */
919 int pa_source_suspend_all(pa_core *c, pa_bool_t suspend) {
924 pa_core_assert_ref(c);
926 for (source = PA_SOURCE(pa_idxset_first(c->sources, &idx)); source; source = PA_SOURCE(pa_idxset_next(c->sources, &idx)))
927 ret -= pa_source_suspend(source, suspend) < 0;
932 /* Called from main thread */
933 void pa_source_detach(pa_source *s) {
934 pa_source_assert_ref(s);
935 pa_assert(PA_SOURCE_IS_LINKED(s->state));
937 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_DETACH, NULL, 0, NULL) == 0);
940 /* Called from main thread */
941 void pa_source_attach(pa_source *s) {
942 pa_source_assert_ref(s);
943 pa_assert(PA_SOURCE_IS_LINKED(s->state));
945 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_ATTACH, NULL, 0, NULL) == 0);
948 /* Called from IO thread */
949 void pa_source_detach_within_thread(pa_source *s) {
953 pa_source_assert_ref(s);
954 pa_assert(PA_SOURCE_IS_LINKED(s->thread_info.state));
956 while ((o = pa_hashmap_iterate(s->thread_info.outputs, &state, NULL)))
961 /* Called from IO thread */
962 void pa_source_attach_within_thread(pa_source *s) {
966 pa_source_assert_ref(s);
967 pa_assert(PA_SOURCE_IS_LINKED(s->thread_info.state));
969 while ((o = pa_hashmap_iterate(s->thread_info.outputs, &state, NULL)))
974 /* Called from IO thread */
975 pa_usec_t pa_source_get_requested_latency_within_thread(pa_source *s) {
976 pa_usec_t result = (pa_usec_t) -1;
980 pa_source_assert_ref(s);
982 if (s->thread_info.requested_latency_valid)
983 return s->thread_info.requested_latency;
985 while ((o = pa_hashmap_iterate(s->thread_info.outputs, &state, NULL)))
987 if (o->thread_info.requested_source_latency != (pa_usec_t) -1 &&
988 (result == (pa_usec_t) -1 || result > o->thread_info.requested_source_latency))
989 result = o->thread_info.requested_source_latency;
991 if (result != (pa_usec_t) -1) {
992 if (s->thread_info.max_latency > 0 && result > s->thread_info.max_latency)
993 result = s->thread_info.max_latency;
995 if (s->thread_info.min_latency > 0 && result < s->thread_info.min_latency)
996 result = s->thread_info.min_latency;
999 s->thread_info.requested_latency = result;
1000 s->thread_info.requested_latency_valid = TRUE;
1005 /* Called from main thread */
1006 pa_usec_t pa_source_get_requested_latency(pa_source *s) {
1009 pa_source_assert_ref(s);
1010 pa_assert(PA_SOURCE_IS_LINKED(s->state));
1012 if (s->state == PA_SOURCE_SUSPENDED)
1015 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_GET_REQUESTED_LATENCY, &usec, 0, NULL) == 0);
1020 /* Called from IO thread */
1021 void pa_source_set_max_rewind(pa_source *s, size_t max_rewind) {
1022 pa_source_output *o;
1025 pa_source_assert_ref(s);
1027 if (max_rewind == s->thread_info.max_rewind)
1030 s->thread_info.max_rewind = max_rewind;
1032 if (PA_SOURCE_IS_LINKED(s->thread_info.state)) {
1033 while ((o = pa_hashmap_iterate(s->thread_info.outputs, &state, NULL)))
1034 pa_source_output_update_max_rewind(o, s->thread_info.max_rewind);
1038 void pa_source_invalidate_requested_latency(pa_source *s) {
1039 pa_source_output *o;
1042 pa_source_assert_ref(s);
1044 s->thread_info.requested_latency_valid = FALSE;
1046 if (s->update_requested_latency)
1047 s->update_requested_latency(s);
1049 while ((o = pa_hashmap_iterate(s->thread_info.outputs, &state, NULL)))
1050 if (o->update_source_requested_latency)
1051 o->update_source_requested_latency(o);
1054 pa_sink_invalidate_requested_latency(s->monitor_of);
1057 void pa_source_set_latency_range(pa_source *s, pa_usec_t min_latency, pa_usec_t max_latency) {
1058 pa_source_assert_ref(s);
1060 /* min_latency == 0: no limit
1061 * min_latency == (size_t) -1: default limit
1062 * min_latency anything else: specified limit
1064 * Similar for max_latency */
1066 if (min_latency == (pa_usec_t) -1)
1067 min_latency = DEFAULT_MIN_LATENCY;
1069 if (max_latency == (pa_usec_t) -1)
1070 max_latency = min_latency;
1072 pa_assert(!min_latency || !max_latency ||
1073 min_latency <= max_latency);
1075 if (PA_SOURCE_IS_LINKED(s->state)) {
1081 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_SET_LATENCY_RANGE, r, 0, NULL) == 0);
1083 s->thread_info.min_latency = min_latency;
1084 s->thread_info.max_latency = max_latency;
1086 s->thread_info.requested_latency_valid = FALSE;
1090 void pa_source_get_latency_range(pa_source *s, pa_usec_t *min_latency, pa_usec_t *max_latency) {
1091 pa_source_assert_ref(s);
1092 pa_assert(min_latency);
1093 pa_assert(max_latency);
1095 if (PA_SOURCE_IS_LINKED(s->state)) {
1096 pa_usec_t r[2] = { 0, 0 };
1098 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_GET_LATENCY_RANGE, r, 0, NULL) == 0);
1100 *min_latency = r[0];
1101 *max_latency = r[1];
1103 *min_latency = s->thread_info.min_latency;
1104 *max_latency = s->thread_info.max_latency;
1108 /* Called from IO thread */
1109 void pa_source_update_latency_range(pa_source *s, pa_usec_t min_latency, pa_usec_t max_latency) {
1110 pa_source_output *o;
1113 pa_source_assert_ref(s);
1115 s->thread_info.min_latency = min_latency;
1116 s->thread_info.max_latency = max_latency;
1118 while ((o = pa_hashmap_iterate(s->thread_info.outputs, &state, NULL)))
1119 if (o->update_source_latency_range)
1120 o->update_source_latency_range(o);
1122 pa_source_invalidate_requested_latency(s);
1125 size_t pa_source_get_max_rewind(pa_source *s) {
1127 pa_source_assert_ref(s);
1129 if (!PA_SOURCE_IS_LINKED(s->state))
1130 return s->thread_info.max_rewind;
1132 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_GET_MAX_REWIND, &r, 0, NULL) == 0);