module-rtp: Reorder sync checks and resynchronization code

This fixes the case when synchronization is established but actually not
valid anymore. In such a case, the code would _first_ write to the ring
buffer (at the wrong position due to the invalid sync), and _then_ detect
the bogus synchronization. Reorder the code blocks to _first_ check the
current sync, then resynchronize if neeeded (or perform initial sync if
no sync is established yet), and _then_ write to the ring buffer.
This commit is contained in:
Carlos Rafael Giani 2025-08-03 18:04:23 +02:00 committed by Wim Taymans
parent b8d98d03fe
commit e5be9cce4f

View file

@ -506,21 +506,9 @@ static void rtp_audio_process_capture(void *data)
quantum = 0;
}
if (!impl->have_sync) {
pw_log_info("sync to timestamp:%u seq:%u ts_offset:%u SSRC:%u",
actual_timestamp, impl->seq, impl->ts_offset, impl->ssrc);
impl->ring.readindex = impl->ring.writeindex = actual_timestamp;
memset(impl->buffer, 0, BUFFER_SIZE);
impl->have_sync = true;
expected_timestamp = actual_timestamp;
filled = 0;
/* First do the synchronization checks (if the sender is in sync already.) */
if (impl->separate_sender) {
/* the sender should know that the sync state has changed, and that it should
* refill the buffer */
impl->refilling = true;
}
} else {
if (impl->have_sync) {
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;
@ -542,6 +530,25 @@ static void rtp_audio_process_capture(void *data)
}
}
/* Next, (re)synchronize. If the sender was in sync, but the checks above detected
* that resynchronization is needed, then this will be done immediately below. */
if (!impl->have_sync) {
pw_log_info("(re)sync to timestamp:%u seq:%u ts_offset:%u SSRC:%u",
actual_timestamp, impl->seq, impl->ts_offset, impl->ssrc);
impl->ring.readindex = impl->ring.writeindex = actual_timestamp;
memset(impl->buffer, 0, BUFFER_SIZE);
impl->have_sync = true;
expected_timestamp = actual_timestamp;
filled = 0;
if (impl->separate_sender) {
/* the sender should know that the sync state has changed, and that it should
* refill the buffer */
impl->refilling = true;
}
}
pw_log_trace("writing %u samples at %u", wanted, expected_timestamp);
spa_ringbuffer_write_data(&impl->ring,