From ba7a8664a12cb9ce98b0fa0dbbd51f59ce231381 Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Mon, 11 Nov 2024 11:49:20 +0100 Subject: [PATCH] module-rtp: calculate payload_size based on MTU The actual payload size depends on the MTU but should not include the IP/UDP and RTP headers. Fixes #4396 --- src/modules/module-rtp/midi.c | 5 +++-- src/modules/module-rtp/stream.c | 8 +++++++- src/modules/module-rtp/stream.h | 3 +++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/modules/module-rtp/midi.c b/src/modules/module-rtp/midi.c index 314f64f57..663be0dc0 100644 --- a/src/modules/module-rtp/midi.c +++ b/src/modules/module-rtp/midi.c @@ -354,7 +354,7 @@ static void rtp_midi_flush_packets(struct impl *impl, struct rtp_header header; struct rtp_midi_header midi_header; struct iovec iov[3]; - uint32_t len, prev_offset, base; + uint32_t len, prev_offset, base, max_size; spa_zero(header); header.v = 2; @@ -371,6 +371,7 @@ static void rtp_midi_flush_packets(struct impl *impl, iov[2].iov_len = 0; prev_offset = len = base = 0; + max_size = impl->payload_size - sizeof(midi_header); SPA_POD_SEQUENCE_FOREACH(sequence, c) { void *ev; @@ -384,7 +385,7 @@ static void rtp_midi_flush_packets(struct impl *impl, offset = c->offset * impl->rate / rate; - if (len > 0 && (len + size > impl->mtu || + if (len > 0 && (len + size > max_size || offset - base > impl->psamples)) { /* flush packet when we have one and when it's either * too large or has too much data. */ diff --git a/src/modules/module-rtp/stream.c b/src/modules/module-rtp/stream.c index 93755e388..de0019a89 100644 --- a/src/modules/module-rtp/stream.c +++ b/src/modules/module-rtp/stream.c @@ -69,6 +69,7 @@ struct impl { uint32_t ts_offset; uint32_t psamples; uint32_t mtu; + uint32_t payload_size; struct spa_ringbuffer ring; uint8_t buffer[BUFFER_SIZE]; @@ -439,6 +440,11 @@ struct rtp_stream *rtp_stream_new(struct pw_core *core, impl->payload = pw_properties_get_uint32(props, "rtp.payload", impl->payload); impl->mtu = pw_properties_get_uint32(props, "net.mtu", DEFAULT_MTU); + if (impl->mtu <= PACKET_HEADER_SIZE) { + pw_log_error("invalid MTU %d, using %d", impl->mtu, DEFAULT_MTU); + impl->mtu = DEFAULT_MTU; + } + impl->payload_size = impl->mtu - PACKET_HEADER_SIZE; impl->seq = pw_rand32(); @@ -476,7 +482,7 @@ struct rtp_stream *rtp_stream_new(struct pw_core *core, pw_log_warn("rtp.ptime doesn't match rtp.framecount. Choosing rtp.ptime"); } } else { - impl->psamples = impl->mtu / impl->stride; + impl->psamples = impl->payload_size / impl->stride; impl->psamples = SPA_CLAMP(impl->psamples, min_samples, max_samples); if (direction == PW_DIRECTION_INPUT) { pw_properties_set(props, "rtp.ptime", diff --git a/src/modules/module-rtp/stream.h b/src/modules/module-rtp/stream.h index 0221b5f1e..e4c09edbc 100644 --- a/src/modules/module-rtp/stream.h +++ b/src/modules/module-rtp/stream.h @@ -19,6 +19,9 @@ struct rtp_stream; #define ERROR_MSEC 2.0f #define DEFAULT_SESS_LATENCY 100.0f +/* 28 bytes IP/UDP, 12 bytes RTP header */ +#define PACKET_HEADER_SIZE (12+28) + #define DEFAULT_MTU 1280 #define DEFAULT_MIN_PTIME 2.0f #define DEFAULT_MAX_PTIME 20.0f