source/sink: Allow pa_{source, sink}_get_latency_within_thread() to return negative values

The reported latency of source or sink is based on measured initial conditions.
If the conditions contain an error, the estimated latency values may become negative.
This does not indicate that the latency is indeed negative but can be considered
merely an offset error. The current get_latency_in_thread() calls and the
implementations of the PA_{SINK,SOURCE}_MESSAGE_GET_LATENCY messages truncate negative
latencies because they do not make sense from a physical point of view. In fact,
the values are truncated twice, once in the message handler and a second time in
the pa_{source,sink}_get_latency_within_thread() call itself.
This leads to two problems for the latency controller within module-loopback:

- Truncating leads to discontinuities in the latency reports which then trigger
  unwanted end to end latency corrections.
- If a large negative port latency offsets is set, the reported latency is always 0,
  making it impossible to control the end to end latency at all.

This patch is a pre-condition for solving these problems.
It adds a new flag to pa_{sink,source}_get_latency_within_thread() to allow
negative return values. Truncating is also removed in all implementations of the
PA_{SINK,SOURCE}_MESSAGE_GET_LATENCY message handlers. The allow_negative flag
is set to false for all calls of pa_{sink,source}_get_latency_within_thread()
except when used within PA_{SINK,SOURCE}_MESSAGE_GET_LATENCY. This means that the
original behavior is not altered in most cases. Only if a positive latency offset
is set and the message returns a negative value, the reported latency is smaller
because the values are not truncated twice.

Additionally let PA_SOURCE_MESSAGE_GET_LATENCY return -pa_sink_get_latency_within_thread()
for monitor sources because the source gets the data before it is played.
This commit is contained in:
Georg Chini 2017-04-17 19:50:10 +02:00
parent a5fccd4645
commit fe70b9e11a
38 changed files with 137 additions and 139 deletions

View file

@ -402,29 +402,29 @@ static int source_process_msg_cb(pa_msgobject *o, int code, void *data, int64_t
pa_usec_t remote_latency;
if (!PA_SOURCE_IS_LINKED(u->source->thread_info.state)) {
*((pa_usec_t*) data) = 0;
*((int64_t*) data) = 0;
return 0;
}
if (!u->stream) {
*((pa_usec_t*) data) = 0;
*((int64_t*) data) = 0;
return 0;
}
if (pa_stream_get_state(u->stream) != PA_STREAM_READY) {
*((pa_usec_t*) data) = 0;
*((int64_t*) data) = 0;
return 0;
}
if (pa_stream_get_latency(u->stream, &remote_latency, &negative) < 0) {
*((pa_usec_t*) data) = 0;
*((int64_t*) data) = 0;
return 0;
}
if (negative)
*((pa_usec_t*) data) = 0;
*((int64_t*) data) = - (int64_t)remote_latency;
else
*((pa_usec_t*) data) = remote_latency;
*((int64_t*) data) = remote_latency;
return 0;
}