diff options
Diffstat (limited to 'src/pulsecore/sink.c')
-rw-r--r-- | src/pulsecore/sink.c | 158 |
1 files changed, 114 insertions, 44 deletions
diff --git a/src/pulsecore/sink.c b/src/pulsecore/sink.c index a0f0ea7e..93800d14 100644 --- a/src/pulsecore/sink.c +++ b/src/pulsecore/sink.c @@ -50,6 +50,7 @@ #define MIX_BUFFER_LENGTH (PA_PAGE_SIZE) #define ABSOLUTE_MIN_LATENCY (500) #define ABSOLUTE_MAX_LATENCY (10*PA_USEC_PER_SEC) +#define DEFAULT_FIXED_LATENCY (250*PA_USEC_PER_MSEC) static PA_DEFINE_CHECK_TYPE(pa_sink, pa_msgobject); @@ -208,6 +209,8 @@ pa_sink* pa_sink_new( s->muted = data->muted; s->refresh_volume = s->refresh_muted = FALSE; + s->fixed_latency = flags & PA_SINK_DYNAMIC_LATENCY ? 0 : DEFAULT_FIXED_LATENCY; + reset_callbacks(s); s->userdata = NULL; @@ -363,8 +366,13 @@ void pa_sink_put(pa_sink* s) { if (s->flags & PA_SINK_LATENCY) s->monitor_source->flags |= PA_SOURCE_LATENCY; - if (s->flags & PA_SINK_DYNAMIC_LATENCY) + if (s->flags & PA_SINK_DYNAMIC_LATENCY) { s->monitor_source->flags |= PA_SOURCE_DYNAMIC_LATENCY; + s->fixed_latency = 0; + } else if (s->fixed_latency <= 0) + s->fixed_latency = DEFAULT_FIXED_LATENCY; + + s->monitor_source->fixed_latency = s->fixed_latency; pa_assert_se(sink_set_state(s, PA_SINK_IDLE) == 0); @@ -515,8 +523,12 @@ pa_queue *pa_sink_move_all_start(pa_sink *s) { for (i = PA_SINK_INPUT(pa_idxset_first(s->inputs, &idx)); i; i = n) { n = PA_SINK_INPUT(pa_idxset_next(s->inputs, &idx)); + pa_sink_input_ref(i); + if (pa_sink_input_start_move(i) >= 0) - pa_queue_push(q, pa_sink_input_ref(i)); + pa_queue_push(q, i); + else + pa_sink_input_unref(i); } return q; @@ -984,6 +996,46 @@ pa_usec_t pa_sink_get_latency_within_thread(pa_sink *s) { return usec; } +static void compute_new_soft_volume(pa_sink_input *i, const pa_cvolume *new_volume) { + unsigned c; + + pa_sink_input_assert_ref(i); + pa_assert(new_volume->channels == i->sample_spec.channels); + + /* + * This basically calculates: + * + * i->relative_volume := i->virtual_volume / new_volume + * i->soft_volume := i->relative_volume * i->volume_factor + */ + + /* The new sink volume passed in here must already be remapped to + * the sink input's channel map! */ + + i->soft_volume.channels = i->sample_spec.channels; + + for (c = 0; c < i->sample_spec.channels; c++) + + if (new_volume->values[c] <= PA_VOLUME_MUTED) + /* We leave i->relative_volume untouched */ + i->soft_volume.values[c] = PA_VOLUME_MUTED; + else { + i->relative_volume[c] = + pa_sw_volume_to_linear(i->virtual_volume.values[c]) / + pa_sw_volume_to_linear(new_volume->values[c]); + + i->soft_volume.values[c] = pa_sw_volume_from_linear( + i->relative_volume[c] * + pa_sw_volume_to_linear(i->volume_factor.values[c])); + } + + /* Hooks have the ability to play games with i->soft_volume */ + pa_hook_fire(&i->core->hooks[PA_CORE_HOOK_SINK_INPUT_SET_VOLUME], i); + + /* We don't copy the soft_volume to the thread_info data + * here. That must be done by the caller */ +} + /* Called from main thread */ void pa_sink_update_flat_volume(pa_sink *s, pa_cvolume *new_volume) { pa_sink_input *i; @@ -998,7 +1050,7 @@ void pa_sink_update_flat_volume(pa_sink *s, pa_cvolume *new_volume) { * might need to fix up the sink volume accordingly. Please note * that we don't actually update the sinks volume here, we only * return how it needs to be updated. The caller should then call - * pa_sink_set_flat_volume().*/ + * pa_sink_set_volume().*/ if (pa_idxset_isempty(s->inputs)) { /* In the special case that we have no sink input we leave the @@ -1030,26 +1082,22 @@ void pa_sink_update_flat_volume(pa_sink *s, pa_cvolume *new_volume) { remapped_new_volume = *new_volume; pa_cvolume_remap(&remapped_new_volume, &s->channel_map, &i->channel_map); - pa_sw_cvolume_divide(&i->soft_volume, &i->virtual_volume, &remapped_new_volume); - pa_sw_cvolume_multiply(&i->soft_volume, &i->soft_volume, &i->volume_factor); - - /* Hooks have the ability to play games with i->soft_volume */ - pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SINK_INPUT_SET_VOLUME], i); + compute_new_soft_volume(i, &remapped_new_volume); - /* We don't issue PA_SINK_INPUT_MESSAGE_SET_VOLUME because - * we want the update to have atomically with the sink - * volume update, hence we do it within the - * pa_sink_set_flat_volume() call below*/ + /* We don't copy soft_volume to the thread_info data here + * (i.e. issue PA_SINK_INPUT_MESSAGE_SET_VOLUME) because we + * want the update to be atomically with the sink volume + * update, hence we do it within the pa_sink_set_volume() call + * below */ } } /* Called from main thread */ -void pa_sink_propagate_flat_volume(pa_sink *s, const pa_cvolume *old_volume) { +void pa_sink_propagate_flat_volume(pa_sink *s) { pa_sink_input *i; uint32_t idx; pa_sink_assert_ref(s); - pa_assert(old_volume); pa_assert(PA_SINK_IS_LINKED(s->state)); pa_assert(s->flags & PA_SINK_FLAT_VOLUME); @@ -1058,39 +1106,43 @@ void pa_sink_propagate_flat_volume(pa_sink *s, const pa_cvolume *old_volume) { * sink input volumes accordingly */ for (i = PA_SINK_INPUT(pa_idxset_first(s->inputs, &idx)); i; i = PA_SINK_INPUT(pa_idxset_next(s->inputs, &idx))) { - pa_cvolume remapped_old_volume, remapped_new_volume, fixed_volume; + pa_cvolume sink_volume, new_virtual_volume; unsigned c; - remapped_new_volume = s->virtual_volume; - pa_cvolume_remap(&remapped_new_volume, &s->channel_map, &i->channel_map); + /* This basically calculates i->virtual_volume := i->relative_volume * s->virtual_volume */ - remapped_old_volume = *old_volume; - pa_cvolume_remap(&remapped_old_volume, &s->channel_map, &i->channel_map); + sink_volume = s->virtual_volume; + pa_cvolume_remap(&sink_volume, &s->channel_map, &i->channel_map); for (c = 0; c < i->sample_spec.channels; c++) + new_virtual_volume.values[c] = pa_sw_volume_from_linear( + i->relative_volume[c] * + pa_sw_volume_to_linear(sink_volume.values[c])); - if (remapped_old_volume.values[c] == PA_VOLUME_MUTED) - fixed_volume.values[c] = PA_VOLUME_MUTED; - else - fixed_volume.values[c] = (pa_volume_t) - ((uint64_t) i->virtual_volume.values[c] * - (uint64_t) remapped_new_volume.values[c] / - (uint64_t) remapped_old_volume.values[c]); + new_virtual_volume.channels = i->sample_spec.channels; - fixed_volume.channels = i->virtual_volume.channels; + if (!pa_cvolume_equal(&new_virtual_volume, &i->virtual_volume)) { + i->virtual_volume = new_virtual_volume; - if (!pa_cvolume_equal(&fixed_volume, &i->virtual_volume)) { - i->virtual_volume = fixed_volume; + /* Hmm, the soft volume might no longer actually match + * what has been chosen as new virtual volume here, + * especially when the old volume was + * PA_VOLUME_MUTED. Hence let's recalculate the soft + * volumes here. */ + compute_new_soft_volume(i, &sink_volume); /* The virtual volume changed, let's tell people so */ pa_subscription_post(i->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index); } } + + /* If the soft_volume of any of the sink inputs got changed, let's + * make sure the thread copies are synced up. */ + pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SYNC_VOLUMES, NULL, 0, NULL) == 0); } /* Called from main thread */ void pa_sink_set_volume(pa_sink *s, const pa_cvolume *volume, pa_bool_t propagate, pa_bool_t sendmsg) { - pa_cvolume old_virtual_volume; pa_bool_t virtual_volume_changed; pa_sink_assert_ref(s); @@ -1099,14 +1151,13 @@ void pa_sink_set_volume(pa_sink *s, const pa_cvolume *volume, pa_bool_t propagat pa_assert(pa_cvolume_valid(volume)); pa_assert(pa_cvolume_compatible(volume, &s->sample_spec)); - old_virtual_volume = s->virtual_volume; + virtual_volume_changed = !pa_cvolume_equal(volume, &s->virtual_volume); s->virtual_volume = *volume; - virtual_volume_changed = !pa_cvolume_equal(&old_virtual_volume, &s->virtual_volume); /* Propagate this volume change back to the inputs */ if (virtual_volume_changed) if (propagate && (s->flags & PA_SINK_FLAT_VOLUME)) - pa_sink_propagate_flat_volume(s, &old_virtual_volume); + pa_sink_propagate_flat_volume(s); if (s->set_volume) { /* If we have a function set_volume(), then we do not apply a @@ -1157,7 +1208,7 @@ const pa_cvolume *pa_sink_get_volume(pa_sink *s, pa_bool_t force_refresh) { if (!pa_cvolume_equal(&old_virtual_volume, &s->virtual_volume)) { if (s->flags & PA_SINK_FLAT_VOLUME) - pa_sink_propagate_flat_volume(s, &old_virtual_volume); + pa_sink_propagate_flat_volume(s); pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index); } @@ -1565,9 +1616,13 @@ int pa_sink_process_msg(pa_msgobject *o, int code, void *userdata, int64_t offse pa_sink_request_rewind(s, (size_t) -1); } - if (s->flags & PA_SINK_FLAT_VOLUME) - sync_input_volumes_within_thread(s); + if (!(s->flags & PA_SINK_FLAT_VOLUME)) + return 0; + + /* Fall through ... */ + case PA_SINK_MESSAGE_SYNC_VOLUMES: + sync_input_volumes_within_thread(s); return 0; case PA_SINK_MESSAGE_GET_VOLUME: @@ -1585,7 +1640,11 @@ int pa_sink_process_msg(pa_msgobject *o, int code, void *userdata, int64_t offse case PA_SINK_MESSAGE_GET_MUTE: return 0; - case PA_SINK_MESSAGE_SET_STATE: + case PA_SINK_MESSAGE_SET_STATE: { + + pa_bool_t suspend_change = + (s->thread_info.state == PA_SINK_SUSPENDED && PA_SINK_IS_OPENED(PA_PTR_TO_UINT(userdata))) || + (PA_SINK_IS_OPENED(s->thread_info.state) && PA_PTR_TO_UINT(userdata) == PA_SINK_SUSPENDED); s->thread_info.state = PA_PTR_TO_UINT(userdata); @@ -1594,7 +1653,17 @@ int pa_sink_process_msg(pa_msgobject *o, int code, void *userdata, int64_t offse s->thread_info.rewind_requested = FALSE; } + if (suspend_change) { + pa_sink_input *i; + void *state = NULL; + + while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL))) + if (i->suspend_within_thread) + i->suspend_within_thread(i, s->thread_info.state == PA_SINK_SUSPENDED); + } + return 0; + } case PA_SINK_MESSAGE_DETACH: @@ -1763,6 +1832,9 @@ pa_usec_t pa_sink_get_requested_latency_within_thread(pa_sink *s) { pa_sink_assert_ref(s); + if (!(s->flags & PA_SINK_DYNAMIC_LATENCY)) + return PA_CLAMP(s->fixed_latency, s->thread_info.min_latency, s->thread_info.max_latency); + if (s->thread_info.requested_latency_valid) return s->thread_info.requested_latency; @@ -1778,13 +1850,8 @@ pa_usec_t pa_sink_get_requested_latency_within_thread(pa_sink *s) { (result == (pa_usec_t) -1 || result > monitor_latency)) result = monitor_latency; - if (result != (pa_usec_t) -1) { - if (result > s->thread_info.max_latency) - result = s->thread_info.max_latency; - - if (result < s->thread_info.min_latency) - result = s->thread_info.min_latency; - } + if (result != (pa_usec_t) -1) + result = PA_CLAMP(result, s->thread_info.min_latency, s->thread_info.max_latency); s->thread_info.requested_latency = result; s->thread_info.requested_latency_valid = TRUE; @@ -1873,6 +1940,9 @@ void pa_sink_invalidate_requested_latency(pa_sink *s) { pa_sink_assert_ref(s); + if (!(s->flags & PA_SINK_DYNAMIC_LATENCY)) + return; + s->thread_info.requested_latency_valid = FALSE; if (PA_SINK_IS_LINKED(s->thread_info.state)) { |