Skip to content

Commit

Permalink
Prefer TCP again
Browse files Browse the repository at this point in the history
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
01bac09 ("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.
  • Loading branch information
andrey-utkin committed Jan 29, 2024
1 parent 2079c3d commit 7b7657d
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 15 deletions.
18 changes: 4 additions & 14 deletions lib/lavf_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
}

Expand All @@ -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;
}

Expand Down
1 change: 0 additions & 1 deletion lib/lavf_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 7b7657d

Please sign in to comment.