module-rtp: Fix timestamp check and add discontinuity check

Until now, the timestamp check was comparing the timestamp delta against
the value of the "quantum" variable. However, the timestamps use clock
samples as units, while the "quantum" variable uses nanoseconds. The
outcome is that this check virtually never returned true. Use the
spa_io_clock duration instead of that quantum nanosecond duration to make
the check actually work.

Also, do not just rely on vast timestamp deltas to detect discontinuities;
instead, check first for the presence of the SPA_IO_CLOCK_FLAG_DISCONT
flag to detect said discontinuities.
This commit is contained in:
Carlos Rafael Giani 2025-08-03 17:58:58 +02:00 committed by Wim Taymans
parent 2a460e18e3
commit b8d98d03fe

View file

@ -521,7 +521,17 @@ static void rtp_audio_process_capture(void *data)
impl->refilling = true;
}
} else {
if (SPA_ABS((int)expected_timestamp - (int)actual_timestamp) > (int)quantum) {
if (SPA_FLAG_IS_SET(pos->clock.flags, SPA_IO_CLOCK_FLAG_DISCONT)) {
pw_log_info("IO clock reports discontinuity; resynchronizing");
impl->have_sync = false;
} else if (SPA_ABS((int64_t)expected_timestamp - (int64_t)actual_timestamp) > (int64_t)(pos->clock.duration)) {
/* Normally, expected and actual timestamp should be in sync, and deviate
* only minimally at most. If a major deviation occurs, then most likely
* the driver clock has experienced an unexpected jump. Note that the
* cycle duration in samples is used, and not the value of "quantum".
* That value is given in nanoseconds, not samples. Also, the timestamps
* themselves are not affected by rate_diff. See the documentation
* "Driver architecture and workflow" for an explanation why not. */
pw_log_warn("timestamp: expected %u != actual %u", expected_timestamp, actual_timestamp);
impl->have_sync = false;
} else if (filled + wanted > (int32_t)SPA_MIN(impl->target_buffer * 8, BUFFER_SIZE / stride)) {