From 51a970f5b7b7b7df9bc4d4a091754eb91152091a Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Mon, 13 Mar 2023 15:14:41 +0100 Subject: [PATCH] module-rtp: fix writing of audio samples Always write samples according to the current write position, only use the graph timestamp to align. --- src/modules/module-rtp/audio.c | 23 ++++++++++++----------- src/modules/module-rtp/opus.c | 19 +++++++++---------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/modules/module-rtp/audio.c b/src/modules/module-rtp/audio.c index f561d8147..642d4a54d 100644 --- a/src/modules/module-rtp/audio.c +++ b/src/modules/module-rtp/audio.c @@ -265,13 +265,21 @@ static void rtp_audio_process_capture(void *data) wanted = size / stride; filled = spa_ringbuffer_get_write_index(&impl->ring, &expected_timestamp); + if (SPA_LIKELY(impl->io_position)) { uint32_t rate = impl->io_position->clock.rate.denom; timestamp = impl->io_position->clock.position * impl->rate / rate; } else timestamp = expected_timestamp; - if (impl->have_sync) { + if (!impl->have_sync) { + pw_log_info("sync to timestamp:%u seq:%u ts_offset:%u SSRC:%u", + timestamp, impl->seq, impl->ts_offset, impl->ssrc); + impl->ring.readindex = impl->ring.writeindex = timestamp; + memset(impl->buffer, 0, BUFFER_SIZE); + impl->have_sync = true; + expected_timestamp = timestamp; + } else { if (SPA_ABS((int32_t)expected_timestamp - (int32_t)timestamp) > 32) { pw_log_warn("expected %u != timestamp %u", expected_timestamp, timestamp); impl->have_sync = false; @@ -280,21 +288,14 @@ static void rtp_audio_process_capture(void *data) impl->have_sync = false; } } - if (!impl->have_sync) { - pw_log_info("sync to timestamp:%u seq:%u ts_offset:%u SSRC:%u", - timestamp, impl->seq, impl->ts_offset, impl->ssrc); - impl->ring.readindex = impl->ring.writeindex = timestamp; - memset(impl->buffer, 0, BUFFER_SIZE); - impl->have_sync = true; - } spa_ringbuffer_write_data(&impl->ring, impl->buffer, BUFFER_SIZE, - (timestamp * stride) & BUFFER_MASK, + (expected_timestamp * stride) & BUFFER_MASK, SPA_PTROFF(d[0].data, offs, void), wanted * stride); - timestamp += wanted; - spa_ringbuffer_write_update(&impl->ring, timestamp); + expected_timestamp += wanted; + spa_ringbuffer_write_update(&impl->ring, expected_timestamp); pw_stream_queue_buffer(impl->stream, buf); diff --git a/src/modules/module-rtp/opus.c b/src/modules/module-rtp/opus.c index 5d85d4787..5b7f6b9d8 100644 --- a/src/modules/module-rtp/opus.c +++ b/src/modules/module-rtp/opus.c @@ -288,7 +288,13 @@ static void rtp_opus_process_capture(void *data) } else timestamp = expected_timestamp; - if (impl->have_sync) { + if (!impl->have_sync) { + pw_log_info("sync to timestamp:%u seq:%u ts_offset:%u SSRC:%u", + timestamp, impl->seq, impl->ts_offset, impl->ssrc); + impl->ring.readindex = impl->ring.writeindex = expected_timestamp = timestamp; + memset(impl->buffer, 0, BUFFER_SIZE); + impl->have_sync = true; + } else { if (SPA_ABS((int32_t)expected_timestamp - (int32_t)timestamp) > 32) { pw_log_warn("expected %u != timestamp %u", expected_timestamp, timestamp); impl->have_sync = false; @@ -297,21 +303,14 @@ static void rtp_opus_process_capture(void *data) impl->have_sync = false; } } - if (!impl->have_sync) { - pw_log_info("sync to timestamp:%u seq:%u ts_offset:%u SSRC:%u", - timestamp, impl->seq, impl->ts_offset, impl->ssrc); - impl->ring.readindex = impl->ring.writeindex = timestamp; - memset(impl->buffer, 0, BUFFER_SIZE); - impl->have_sync = true; - } spa_ringbuffer_write_data(&impl->ring, impl->buffer, BUFFER_SIZE, (filled * stride) & BUFFER_MASK, SPA_PTROFF(d[0].data, offs, void), wanted * stride); - timestamp += wanted; - spa_ringbuffer_write_update(&impl->ring, timestamp); + expected_timestamp += wanted; + spa_ringbuffer_write_update(&impl->ring, expected_timestamp); pw_stream_queue_buffer(impl->stream, buf);