From 79c5b07e2cd7502d1b33fae4b918be6ce6962285 Mon Sep 17 00:00:00 2001 From: Ryan Colobong Date: Fri, 6 Dec 2024 03:31:29 +0800 Subject: [PATCH 1/2] ENGDESK-36728: Support configuring the RTP Sequence Range --- src/include/switch_rtp.h | 20 ++++++++++++++++++++ src/switch_core.c | 4 ++++ src/switch_rtp.c | 27 ++++++++++++++++++++++++++- 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/include/switch_rtp.h b/src/include/switch_rtp.h index 62021d1564..098d842c35 100644 --- a/src/include/switch_rtp.h +++ b/src/include/switch_rtp.h @@ -328,6 +328,26 @@ SWITCH_DECLARE(switch_status_t) switch_rtp_set_remote_ssrc(switch_rtp_t *rtp_ses */ SWITCH_DECLARE(switch_port_t) switch_rtp_set_end_port(switch_port_t port); +/*! + \brief Set/Get RTP start sequence + \param port new value (if > 0) + \return the current RTP start sequence +*/ +SWITCH_DECLARE(uint16_t) switch_rtp_set_start_sequence(uint16_t sequence); + +/*! + \brief Set/Get RTP end sequence + \param port new value (if > 0) + \return the current RTP end sequence +*/ +SWITCH_DECLARE(uint16_t) switch_rtp_set_end_sequence(uint16_t sequence); + +/*! + \brief Request a new start sequence to be used for RTP packet + \return the new random sequence +*/ +SWITCH_DECLARE(uint16_t) switch_rtp_request_sequence(); + /*! \brief Set/Get RTP packet penalty for packet loss \param port new value (if > 0) diff --git a/src/switch_core.c b/src/switch_core.c index 80d3031056..9699c2450f 100644 --- a/src/switch_core.c +++ b/src/switch_core.c @@ -2388,6 +2388,10 @@ static void switch_load_core_config(const char *file) switch_rtp_set_start_port((switch_port_t) atoi(val)); } else if (!strcasecmp(var, "rtp-end-port") && !zstr(val)) { switch_rtp_set_end_port((switch_port_t) atoi(val)); + } else if (!strcasecmp(var, "rtp-start-seq") && !zstr(val)) { + switch_rtp_set_start_sequence(atoi(val)); + } else if (!strcasecmp(var, "rtp-end-seq") && !zstr(val)) { + switch_rtp_set_end_sequence(atoi(val)); } else if (!strcasecmp(var, "rtp-mos-packet-loss-penalty") && !zstr(val)) { switch_rtp_set_mos_packet_loss_penalty(atof(val)); } else if (!strcasecmp(var, "rtp-mos-jitter-penalty") && !zstr(val)) { diff --git a/src/switch_rtp.c b/src/switch_rtp.c index 9f154d3f4c..a795259198 100644 --- a/src/switch_rtp.c +++ b/src/switch_rtp.c @@ -79,6 +79,8 @@ #define rtp_header_len 12 #define RTP_START_PORT 16384 #define RTP_END_PORT 32768 +#define RTP_START_SEQUENCE 0 +#define RTP_END_SEQUENCE 64000 #define MASTER_KEY_LEN 30 #define RTP_MAGIC_NUMBER 42 #define WARN_SRTP_ERRS 10 @@ -102,6 +104,8 @@ static const switch_payload_t INVALID_PT = 255; static switch_port_t START_PORT = RTP_START_PORT; static switch_port_t END_PORT = RTP_END_PORT; +static uint16_t START_SEQUENCE = RTP_START_SEQUENCE; +static uint16_t END_SEQUENCE = RTP_END_SEQUENCE; static switch_mutex_t *port_lock = NULL; static switch_size_t do_flush(switch_rtp_t *rtp_session, int force, switch_size_t bytes_in); static rtp_create_probe_func create_probe = 0; @@ -2920,6 +2924,27 @@ SWITCH_DECLARE(switch_port_t) switch_rtp_set_end_port(switch_port_t port) return END_PORT; } +SWITCH_DECLARE(uint16_t) switch_rtp_set_start_sequence(uint16_t sequence) +{ + if (sequence) { + START_SEQUENCE = sequence; + } + return START_SEQUENCE; +} + +SWITCH_DECLARE(uint16_t) switch_rtp_set_end_sequence(uint16_t sequence) +{ + if (sequence) { + END_SEQUENCE = sequence; + } + return END_SEQUENCE; +} + +SWITCH_DECLARE(uint16_t) switch_rtp_request_sequence() +{ + return (((uint16_t)rand()) % (END_SEQUENCE - START_SEQUENCE + 1) + START_SEQUENCE); +} + SWITCH_DECLARE(double) switch_rtp_set_mos_packet_loss_penalty(double penalty) { if (penalty) { @@ -5098,7 +5123,7 @@ SWITCH_DECLARE(switch_status_t) switch_rtp_create(switch_rtp_t **new_rtp_session if (rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]) { switch_sockaddr_create(&rtp_session->rtcp_from_addr, pool); } - rtp_session->seq = (uint16_t) rand(); + rtp_session->seq = switch_rtp_request_sequence(); rtp_session->ssrc = (uint32_t) ((intptr_t) rtp_session + (uint32_t) switch_epoch_time_now(NULL)); #ifdef DEBUG_TS_ROLLOVER rtp_session->last_write_ts = TS_ROLLOVER_START; From ac77f0bb598fc4f998dd9e5f938968ef8764ca58 Mon Sep 17 00:00:00 2001 From: Ryan Colobong Date: Fri, 6 Dec 2024 04:06:03 +0800 Subject: [PATCH 2/2] Minor fix in the comment --- src/include/switch_rtp.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/include/switch_rtp.h b/src/include/switch_rtp.h index 098d842c35..30389876a2 100644 --- a/src/include/switch_rtp.h +++ b/src/include/switch_rtp.h @@ -330,14 +330,14 @@ SWITCH_DECLARE(switch_port_t) switch_rtp_set_end_port(switch_port_t port); /*! \brief Set/Get RTP start sequence - \param port new value (if > 0) + \param sequence new value (if > 0) \return the current RTP start sequence */ SWITCH_DECLARE(uint16_t) switch_rtp_set_start_sequence(uint16_t sequence); /*! \brief Set/Get RTP end sequence - \param port new value (if > 0) + \param sequence new value (if > 0) \return the current RTP end sequence */ SWITCH_DECLARE(uint16_t) switch_rtp_set_end_sequence(uint16_t sequence);