From d679e6aa7e66aa9f485445de1bc1abe37bdc10ba Mon Sep 17 00:00:00 2001 From: Jakub Karolczyk Date: Wed, 9 Jun 2021 17:33:09 +0100 Subject: [PATCH] [CORE] Parametrize Video Buffer size Video Buffer size (max_frames) is hardcoded to the value of 10, which leads to overflow the buffer quite quickly, especially for higher resolutions or harder to encoded video scenes (e.g. high motion ones). With this change, video buffer size can be configured via video-jb-size-factor parameter in switch.conf within boundaries 10 - 100. --- src/switch_core.c | 2 ++ src/switch_rtp.c | 18 +++++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/switch_core.c b/src/switch_core.c index d4507aa49c..ae7d254a11 100644 --- a/src/switch_core.c +++ b/src/switch_core.c @@ -2393,6 +2393,8 @@ static void switch_load_core_config(const char *file) } } else if (!strcasecmp(var, "max-audio-channels") && !zstr(val)) { switch_core_max_audio_channels(atoi(val)); + } else if (!strcasecmp(var, "video-jb-size-factor") && !zstr(val)) { + switch_core_set_variable("video_jb_size_factor", val); } } } diff --git a/src/switch_rtp.c b/src/switch_rtp.c index 1b0d407dec..3ac833e757 100644 --- a/src/switch_rtp.c +++ b/src/switch_rtp.c @@ -89,6 +89,9 @@ static const switch_payload_t INVALID_PT = 255; * characters when sending, but an implementation MUST accept up to 256 * characters when receiving." */ +#define MAX_VIDEO_BUFFER_SIZE_FACTOR 100 +#define MIN_VIDEO_BUFFER_SIZE_FACTOR 10 + static switch_port_t START_PORT = RTP_START_PORT; static switch_port_t END_PORT = RTP_END_PORT; static switch_mutex_t *port_lock = NULL; @@ -4761,6 +4764,13 @@ SWITCH_DECLARE(switch_status_t) switch_rtp_get_video_buffer_size(switch_rtp_t *r SWITCH_DECLARE(switch_status_t) switch_rtp_set_video_buffer_size(switch_rtp_t *rtp_session, uint32_t frames, uint32_t max_frames) { + char *tmp; + int vb_size_factor = 0; + + if ((tmp = switch_core_get_variable("video_jb_size_factor"))) { + vb_size_factor = atoi(tmp); + } + if (!switch_rtp_ready(rtp_session)) { return SWITCH_STATUS_FALSE; } @@ -4770,7 +4780,13 @@ SWITCH_DECLARE(switch_status_t) switch_rtp_set_video_buffer_size(switch_rtp_t *r } if (!max_frames || frames >= max_frames) { - max_frames = frames * 10; + if (vb_size_factor && vb_size_factor < MAX_VIDEO_BUFFER_SIZE_FACTOR + 1 && vb_size_factor > MIN_VIDEO_BUFFER_SIZE_FACTOR) { + max_frames = frames * vb_size_factor; + } + else { + max_frames = frames * MIN_VIDEO_BUFFER_SIZE_FACTOR; + } + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG, "Set video buffer to %d\n", max_frames); } rtp_session->last_max_vb_frames = max_frames;