latency: handle negative latency correctly

In our current world, it is possible to have a negative delay. This
means that the stream should be delayed to sync with other streams.

The pulse-server sets negative delay and the Latency message can hold
those negative values so make sure we handle them in the helper
functions as well.

Do the delay calculations in pw_stream and JACK with signed values to
correctly handle negative values. Clamp JACK latency range to 0 because
negative latency is not supported in JACK.

We should also probably make sure we never end up with negative
latency, mostly in ALSA when we set a Latency offset, but that is
another detail.
This commit is contained in:
Wim Taymans 2024-10-23 10:47:58 +02:00
parent 9243ed0cbd
commit da86026b7a
5 changed files with 40 additions and 20 deletions

View file

@ -2371,7 +2371,8 @@ int pw_stream_get_time_n(struct pw_stream *stream, struct pw_time *time, size_t
time->delay += (int64_t)(((impl->latency.min_quantum + impl->latency.max_quantum) / 2.0f) * quantum);
time->delay += (impl->latency.min_rate + impl->latency.max_rate) / 2;
time->delay += ((impl->latency.min_ns + impl->latency.max_ns) / 2) * time->rate.denom / SPA_NSEC_PER_SEC;
time->delay += ((impl->latency.min_ns + impl->latency.max_ns) / 2) *
(int64_t)time->rate.denom / (int64_t)SPA_NSEC_PER_SEC;
avail_buffers = spa_ringbuffer_get_read_index(&impl->dequeued.ring, &index);
avail_buffers = SPA_CLAMP(avail_buffers, 0, (int32_t)impl->n_buffers);

View file

@ -303,6 +303,13 @@ struct pw_stream_control {
* caused by the hardware. The delay is usually quite stable and should only change when
* the topology, quantum or samplerate of the graph changes.
*
* The delay requires the application to send the stream early relative to other synchronized
* streams in order to arrive at the edge of the graph in time. This is usually done by
* delaying the other streams with the given delay.
*
* Note that the delay can be negative. A negative delay means that this stream should be
* delayed with the (positive) delay relative to other streams.
*
* pw_time.queued and pw_time.buffered is expressed in the time domain of the stream,
* or the format that is used for the buffers of this stream.
*