Skip to content

Commit

Permalink
Ignore RTP packets when adjusting UDPTL media port
Browse files Browse the repository at this point in the history
When auto-adjusting UDPTL media port at the start of a call, ignore any
RTP packets that is received which can arrive after FreeSWITCH has
switched and come from a different port on the remote side.
  • Loading branch information
pfournier committed Sep 24, 2024
1 parent c402ce1 commit cd060ab
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/include/switch_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,7 @@ typedef enum {
SWITCH_RTP_FLAG_VAD,
SWITCH_RTP_FLAG_BREAK,
SWITCH_RTP_FLAG_UDPTL,
SWITCH_RTP_FLAG_UDPTL_INIT,
SWITCH_RTP_FLAG_DATAWAIT,
SWITCH_RTP_FLAG_BYTESWAP,
SWITCH_RTP_FLAG_PASS_RFC2833,
Expand Down
16 changes: 15 additions & 1 deletion src/switch_rtp.c
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,8 @@ struct switch_rtp {
uint32_t rtcp_autoadj_threshold;
uint32_t rtcp_autoadj_tally;

uint32_t udptl_autoadj_threshold;

srtp_ctx_t *send_ctx[2];
srtp_ctx_t *recv_ctx[2];

Expand Down Expand Up @@ -3107,6 +3109,9 @@ SWITCH_DECLARE(switch_status_t) switch_rtp_udptl_mode(switch_rtp_t *rtp_session)
}
}

if (!rtp_session->flags[SWITCH_RTP_FLAG_UDPTL]) {
switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_UDPTL_INIT);
}
switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_UDPTL);
switch_rtp_set_flag(rtp_session, SWITCH_RTP_FLAG_PROXY_MEDIA);
switch_socket_opt_set(rtp_session->sock_input, SWITCH_SO_NONBLOCK, FALSE);
Expand Down Expand Up @@ -5388,6 +5393,7 @@ SWITCH_DECLARE(void) switch_rtp_set_flag(switch_rtp_t *rtp_session, switch_rtp_f
rtp_session->autoadj_window = 20;
rtp_session->autoadj_threshold = 10;
rtp_session->autoadj_tally = 0;
rtp_session->udptl_autoadj_threshold = 1;

switch_mutex_lock(rtp_session->flag_mutex);
rtp_session->flags[SWITCH_RTP_FLAG_RTCP_AUTOADJ] = 1;
Expand Down Expand Up @@ -7642,9 +7648,16 @@ static int rtp_common_read(switch_rtp_t *rtp_session, switch_payload_t *payload_
}
}

/* ignore RTP packets when looking for UDPTL media */
if (rtp_session->flags[SWITCH_RTP_FLAG_AUTOADJ] && rtp_session->flags[SWITCH_RTP_FLAG_UDPTL_INIT]) {
if (bytes && rtp_session->last_seq > 100) {
goto recvfrom;
}
}

if (bytes && rtp_session->flags[SWITCH_RTP_FLAG_AUTOADJ] && switch_sockaddr_get_port(rtp_session->rtp_from_addr)) {
if (!switch_cmp_addr(rtp_session->rtp_from_addr, rtp_session->remote_addr, SWITCH_FALSE)) {
if (++rtp_session->autoadj_tally >= rtp_session->autoadj_threshold) {
if (++rtp_session->autoadj_tally >= (rtp_session->flags[SWITCH_RTP_FLAG_UDPTL] ? rtp_session->udptl_autoadj_threshold : rtp_session->autoadj_threshold)) {
const char *err;
uint32_t old = rtp_session->remote_port;
const char *tx_host;
Expand Down Expand Up @@ -7705,6 +7718,7 @@ static int rtp_common_read(switch_rtp_t *rtp_session, switch_payload_t *payload_
if (bytes && !(rtp_session->rtp_bugs & RTP_BUG_ALWAYS_AUTO_ADJUST) && rtp_session->autoadj_window) {
if (--rtp_session->autoadj_window == 0) {
switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_AUTOADJ);
switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_UDPTL_INIT);
}
}

Expand Down

0 comments on commit cd060ab

Please sign in to comment.