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
This commit is contained in:
Wim Taymans 2024-11-11 11:49:20 +01:00
parent b409f5f5d1
commit ba7a8664a1
3 changed files with 13 additions and 3 deletions

View file

@ -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. */

View file

@ -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",

View file

@ -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