source/sink: Allow pa_{source, sink}_get_latency_within_thread() to return negative...
authorGeorg Chini <georg@chini.tk>
Mon, 17 Apr 2017 17:50:10 +0000 (19:50 +0200)
committerGeorg Chini <georg@chini.tk>
Mon, 17 Apr 2017 17:50:10 +0000 (19:50 +0200)
commitfe70b9e11aa0312f5d9508158c6cd652f66d732d
tree0331273dbd99deaa37facf5ea8e34f9aabc356fb
parenta5fccd4645729c06db2caccb67149f2e07598300
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.
38 files changed:
src/modules/alsa/alsa-sink.c
src/modules/alsa/alsa-source.c
src/modules/bluetooth/module-bluez4-device.c
src/modules/bluetooth/module-bluez5-device.c
src/modules/echo-cancel/module-echo-cancel.c
src/modules/jack/module-jack-sink.c
src/modules/jack/module-jack-source.c
src/modules/macosx/module-coreaudio-device.c
src/modules/module-combine-sink.c
src/modules/module-equalizer-sink.c
src/modules/module-esound-sink.c
src/modules/module-ladspa-sink.c
src/modules/module-loopback.c
src/modules/module-null-sink.c
src/modules/module-null-source.c
src/modules/module-pipe-sink.c
src/modules/module-pipe-source.c
src/modules/module-remap-sink.c
src/modules/module-remap-source.c
src/modules/module-sine-source.c
src/modules/module-solaris.c
src/modules/module-tunnel-sink-new.c
src/modules/module-tunnel-source-new.c
src/modules/module-tunnel.c
src/modules/module-virtual-sink.c
src/modules/module-virtual-source.c
src/modules/module-virtual-surround-sink.c
src/modules/module-waveout.c
src/modules/oss/module-oss.c
src/modules/raop/raop-sink.c
src/modules/rtp/module-rtp-recv.c
src/pulsecore/protocol-native.c
src/pulsecore/sink-input.c
src/pulsecore/sink.c
src/pulsecore/sink.h
src/pulsecore/source-output.c
src/pulsecore/source.c
src/pulsecore/source.h