pa_zero(*data);
data->proplist = pa_proplist_new();
- data->ports = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
+ data->ports = pa_hashmap_new_full(pa_idxset_string_hash_func, pa_idxset_string_compare_func, NULL, (pa_free_cb_t) pa_device_port_unref);
return data;
}
pa_proplist_free(data->proplist);
if (data->ports)
- pa_hashmap_free(data->ports, (pa_free_cb_t) pa_device_port_unref);
+ pa_hashmap_free(data->ports);
pa_xfree(data->name);
pa_xfree(data->active_port);
void *state;
pa_device_port *p;
- PA_HASHMAP_FOREACH(p, s->ports, state)
+ PA_HASHMAP_FOREACH(p, s->ports, state) {
+ if (p->available == PA_AVAILABLE_NO)
+ continue;
+
if (!s->active_port || p->priority > s->active_port->priority)
s->active_port = p;
+ }
+
+ if (!s->active_port) {
+ PA_HASHMAP_FOREACH(p, s->ports, state)
+ if (!s->active_port || p->priority > s->active_port->priority)
+ s->active_port = p;
+ }
}
if (s->active_port)
0);
s->thread_info.rtpoll = NULL;
- s->thread_info.outputs = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
+ s->thread_info.outputs = pa_hashmap_new_full(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func, NULL,
+ (pa_free_cb_t) pa_source_output_unref);
s->thread_info.soft_volume = s->soft_volume;
s->thread_info.soft_muted = s->muted;
s->thread_info.state = s->state;
pa_log_info("Freeing source %u \"%s\"", s->index, s->name);
pa_idxset_free(s->outputs, NULL);
- pa_hashmap_free(s->thread_info.outputs, (pa_free_cb_t) pa_source_output_unref);
+ pa_hashmap_free(s->thread_info.outputs);
if (s->silence.memblock)
pa_memblock_unref(s->silence.memblock);
pa_proplist_free(s->proplist);
if (s->ports)
- pa_hashmap_free(s->ports, (pa_free_cb_t) pa_device_port_unref);
+ pa_hashmap_free(s->ports);
pa_xfree(s);
}
}
/* Called from main thread */
-bool pa_source_update_rate(pa_source *s, uint32_t rate, bool passthrough) {
- bool ret = false;
+int pa_source_update_rate(pa_source *s, uint32_t rate, bool passthrough) {
+ int ret;
uint32_t desired_rate = rate;
uint32_t default_rate = s->default_sample_rate;
uint32_t alternate_rate = s->alternate_sample_rate;
- uint32_t idx;
- pa_source_output *o;
bool use_alternate = false;
- if (!s->update_rate)
- return false;
+ if (rate == s->sample_spec.rate)
+ return 0;
- if (PA_UNLIKELY(default_rate == alternate_rate)) {
- pa_log_warn("Default and alternate sample rates are the same.");
- return false;
+ if (!s->update_rate && !s->monitor_of)
+ return -1;
+
+ if (PA_UNLIKELY(default_rate == alternate_rate && !passthrough)) {
+ pa_log_debug("Default and alternate sample rates are the same.");
+ return -1;
}
if (PA_SOURCE_IS_RUNNING(s->state)) {
pa_log_info("Cannot update rate, SOURCE_IS_RUNNING, will keep using %u Hz",
s->sample_spec.rate);
- return false;
+ return -1;
}
- if (PA_UNLIKELY (desired_rate < 8000 ||
- desired_rate > PA_RATE_MAX))
- return false;
+ if (s->monitor_of) {
+ if (PA_SINK_IS_RUNNING(s->monitor_of->state)) {
+ pa_log_info("Cannot update rate, this is a monitor source and the sink is running.");
+ return -1;
+ }
+ }
+
+ if (PA_UNLIKELY(!pa_sample_rate_valid(desired_rate)))
+ return -1;
if (!passthrough) {
- pa_assert(default_rate % 4000 || default_rate % 11025);
- pa_assert(alternate_rate % 4000 || alternate_rate % 11025);
+ pa_assert((default_rate % 4000 == 0) || (default_rate % 11025 == 0));
+ pa_assert((alternate_rate % 4000 == 0) || (alternate_rate % 11025 == 0));
- if (default_rate % 4000) {
- /* default is a 11025 multiple */
+ if (default_rate % 11025 == 0) {
if ((alternate_rate % 4000 == 0) && (desired_rate % 4000 == 0))
use_alternate=true;
} else {
}
if (desired_rate == s->sample_spec.rate)
- return false;
+ return -1;
if (!passthrough && pa_source_used_by(s) > 0)
- return false;
+ return -1;
pa_log_debug("Suspending source %s due to changing the sample rate.", s->name);
pa_source_suspend(s, true, PA_SUSPEND_INTERNAL);
- if (s->update_rate(s, desired_rate) == true) {
- pa_log_info("Changed sampling rate successfully ");
+ if (s->update_rate)
+ ret = s->update_rate(s, desired_rate);
+ else {
+ /* This is a monitor source. */
+
+ /* XXX: This code is written with non-passthrough streams in mind. I
+ * have no idea whether the behaviour with passthrough streams is
+ * sensible. */
+ if (!passthrough) {
+ uint32_t old_rate = s->sample_spec.rate;
+
+ s->sample_spec.rate = desired_rate;
+ ret = pa_sink_update_rate(s->monitor_of, desired_rate, false);
+
+ if (ret < 0) {
+ /* Changing the sink rate failed, roll back the old rate for
+ * the monitor source. Why did we set the source rate before
+ * calling pa_sink_update_rate(), you may ask. The reason is
+ * that pa_sink_update_rate() tries to update the monitor
+ * source rate, but we are already in the process of updating
+ * the monitor source rate, so there's a risk of entering an
+ * infinite loop. Setting the source rate before calling
+ * pa_sink_update_rate() makes the rate == s->sample_spec.rate
+ * check in the beginning of this function return early, so we
+ * avoid looping. */
+ s->sample_spec.rate = old_rate;
+ }
+ } else
+ ret = -1;
+ }
+
+ if (ret >= 0) {
+ uint32_t idx;
+ pa_source_output *o;
PA_IDXSET_FOREACH(o, s->outputs, idx) {
if (o->state == PA_SOURCE_OUTPUT_CORKED)
pa_source_output_update_rate(o);
}
- ret = true;
+
+ pa_log_info("Changed sampling rate successfully");
}
pa_source_suspend(s, false, PA_SUSPEND_INTERNAL);
/* Let's 'push' the reference volume if necessary */
pa_cvolume_merge(&new_reference_volume, &s->reference_volume, &root_source->real_volume);
- /* If the source and it's root don't have the same number of channels, we need to remap */
+ /* If the source and its root don't have the same number of channels, we need to remap */
if (s != root_source && !pa_channel_map_equal(&s->channel_map, &root_source->channel_map))
pa_cvolume_remap(&new_reference_volume, &s->channel_map, &root_source->channel_map);
update_reference_volume(root_source, &new_reference_volume, &root_source->channel_map, save);
return 0;
}
- case PA_SOURCE_MESSAGE_DETACH:
-
- /* Detach all streams */
- pa_source_detach_within_thread(s);
- return 0;
-
- case PA_SOURCE_MESSAGE_ATTACH:
-
- /* Reattach all streams */
- pa_source_attach_within_thread(s);
- return 0;
-
case PA_SOURCE_MESSAGE_GET_REQUESTED_LATENCY: {
pa_usec_t *usec = userdata;
return ret;
}
-/* Called from main thread */
-void pa_source_detach(pa_source *s) {
- pa_source_assert_ref(s);
- pa_assert_ctl_context();
- pa_assert(PA_SOURCE_IS_LINKED(s->state));
-
- pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_DETACH, NULL, 0, NULL) == 0);
-}
-
-/* Called from main thread */
-void pa_source_attach(pa_source *s) {
- pa_source_assert_ref(s);
- pa_assert_ctl_context();
- pa_assert(PA_SOURCE_IS_LINKED(s->state));
-
- pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_ATTACH, NULL, 0, NULL) == 0);
-}
-
/* Called from IO thread */
void pa_source_detach_within_thread(pa_source *s) {
pa_source_output *o;