From 7b7657de56a04bd852cdc201b71d425995df38e3 Mon Sep 17 00:00:00 2001 From: Andriy Utkin Date: Mon, 29 Jan 2024 15:52:04 +0000 Subject: [PATCH] Prefer TCP again This changes the behaviour of "Protocol" option of device configuration, which affects how the source RTSP stream is being consumed. Historically, TCP was preferred. Then, since 01bac09d5caf ("Refactored input device and added option to select AUTO rtp protocol") UDP with fallback to TCP became the default ("AUTO" mode). This makes picture in recordings prone to smearing, so it was decided to switch back to TCP by default. Previously, fallback to TCP was done in our code; this commit makes use of FFmpeg's "rtsp_flags=+prefer_tcp" which accomplishes suitable behaviour: try TCP, fallback to UDP. --- lib/lavf_device.cpp | 18 ++++-------------- lib/lavf_device.h | 1 - 2 files changed, 4 insertions(+), 15 deletions(-) diff --git a/lib/lavf_device.cpp b/lib/lavf_device.cpp index e70fc175..324ea465 100644 --- a/lib/lavf_device.cpp +++ b/lib/lavf_device.cpp @@ -75,7 +75,6 @@ void lavf_device::stop() int lavf_device::start() { if (ctx) return 0; - bool using_tcp = false; AVDictionary *avopt_open_input = NULL; AVInputFormat *input_fmt = NULL; @@ -88,11 +87,10 @@ int lavf_device::start() if (!strncmp(url, "rtsp://", 7)) { - if (rtp_protocol == RTP_PROTOCOL_TCP || tcp_fallback) - { - av_dict_set(&avopt_open_input, "rtsp_flags", "+prefer_tcp", 0); - tcp_fallback = false; - using_tcp = true; + switch (rtp_protocol) { + case RTP_PROTOCOL_TCP: av_dict_set(&avopt_open_input, "rtsp_transport", "tcp", 0); break; + case RTP_PROTOCOL_UDP: av_dict_set(&avopt_open_input, "rtsp_transport", "+udp+udp_multicast", 0); break; + case RTP_PROTOCOL_AUTO: av_dict_set(&avopt_open_input, "rtsp_flags", "+prefer_tcp", 0); break; } } @@ -118,14 +116,6 @@ int lavf_device::start() av_strerror(re, error_message, sizeof(error_message)); bc_log(Error, "Failed to open stream. Error: %d (%s)", re, error_message); ctx = NULL; - - if (rtp_protocol == RTP_PROTOCOL_AUTO && !using_tcp) - { - bc_log(Info, "Falling back to TCP connection"); - tcp_fallback = true; - return start(); - } - return -1; } diff --git a/lib/lavf_device.h b/lib/lavf_device.h index 4173fe8d..bc025b60 100644 --- a/lib/lavf_device.h +++ b/lib/lavf_device.h @@ -52,7 +52,6 @@ class lavf_device : public input_device private: char url[1024]; int rtp_protocol = RTP_PROTOCOL_AUTO; - bool tcp_fallback = false; char error_message[512]; AVFormatContext *ctx;