[FFmpeg-devel] [PATCH] tcp: add TCP keepalive tuning options

2025-11-21 Thread Practice2001 via ffmpeg-devel
Addition of tcp_keepalive, tcp_keepidle, tcp_keepintvl, and tcp_keepcnt
support to the TCP protocol. Exposeing these options to the HTTP protocol
so they can be used for HTTP(S) connections. Updated documentation.
Tested with: ./configure && make && make fate

Fixes ticket #11671.

Signed-off-by: Practice2001 
---
 doc/protocols.texi | 26 ++-
 libavformat/http.c | 36 +
 libavformat/tcp.c  | 80 ++
 3 files changed, 141 insertions(+), 1 deletion(-)

diff --git a/doc/protocols.texi b/doc/protocols.texi
index b74383122a..848f4820e2 100644
--- a/doc/protocols.texi
+++ b/doc/protocols.texi
@@ -989,7 +989,31 @@ Set TCP_NODELAY to disable Nagle's algorithm. Default 
value is 0.
 @item tcp_keepalive=@var{1|0}
 Enable the TCP keepalive mechanism to detect dead peers and help maintain 
long-lived idle connections. Default value is 0.
 
-Only the basic keepalive option (SO_KEEPALIVE) can be enabled or disabled. 
Platform-specific tuning parameters such as TCP_KEEPIDLE, TCP_KEEPINTVL, or 
TCP_KEEPCNT are not configurable and will use the operating system's default 
values.
+The basic keepalive option (SO_KEEPALIVE) can be enabled or disabled. 
Platform-specific tuning parameters such as TCP_KEEPIDLE, TCP_KEEPINTVL, or 
TCP_KEEPCNT are configurable.
+
+@item tcp_keepidle=@var{seconds}
+Set the TCP keepalive idle time (in seconds).
+
+This controls how long the connection must remain idle before the first
+keepalive probe is sent.
+
+Default is 0 (uses system default).
+
+@item tcp_keepintvl=@var{seconds}
+Set the interval (in seconds) between individual TCP keepalive probes.
+
+Default is 0 (uses system default).
+
+@item tcp_keepcnt=@var{count}
+Set the number of unacknowledged keepalive probes that must occur before
+the connection is considered dead.
+
+Default is 0 (uses system default).
+
+@emph{Note:}
+These platform-specific parameters are available on Linux and BSD-derived
+systems. On Windows, only basic keepalive configuration is supported and
+the underlying system will use its own values for probe timing and count.
 
 @end table
 
diff --git a/libavformat/http.c b/libavformat/http.c
index c4e6292a95..1c264d845c 100644
--- a/libavformat/http.c
+++ b/libavformat/http.c
@@ -146,6 +146,11 @@ typedef struct HTTPContext {
 unsigned int retry_after;
 int reconnect_max_retries;
 int reconnect_delay_total_max;
+/* TCP keepalive forwarding */
+int tcp_keepalive;/* -1 = unset, 0 = off, 1 = on */
+int tcp_keepidle; /* seconds, 0 = unset */
+int tcp_keepintvl;/* seconds, 0 = unset */
+int tcp_keepcnt;  /* probe count, 0 = unset */
 } HTTPContext;
 
 #define OFFSET(x) offsetof(HTTPContext, x)
@@ -191,6 +196,10 @@ static const AVOption options[] = {
 { "resource", "The resource requested by a client", OFFSET(resource), 
AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, E },
 { "reply_code", "The http status code to return to a client", 
OFFSET(reply_code), AV_OPT_TYPE_INT, { .i64 = 200}, INT_MIN, 599, E},
 { "short_seek_size", "Threshold to favor readahead over seek.", 
OFFSET(short_seek_size), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, D },
+{ "tcp_keepalive", "Enable SO_KEEPALIVE on underlying TCP socket", 
OFFSET(tcp_keepalive), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, D | E },
+{ "tcp_keepidle", "TCP keepalive idle time (seconds) for underlying TCP 
socket", OFFSET(tcp_keepidle), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, D | E 
},
+{ "tcp_keepintvl", "TCP keepalive interval (seconds) for underlying TCP 
socket", OFFSET(tcp_keepintvl), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, D | 
E },
+{ "tcp_keepcnt", "TCP keepalive probe count for underlying TCP socket", 
OFFSET(tcp_keepcnt), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, D | E },
 { NULL }
 };
 
@@ -282,6 +291,33 @@ static int http_open_cnx_internal(URLContext *h, 
AVDictionary **options)
 
 ff_url_join(buf, sizeof(buf), lower_proto, NULL, hostname, port, NULL);
 
+
+/* Forward TCP keepalive options to underlying protocol via options dict.
+* Prefer using AVDictionary forwarding to modifying the URL (safer). */
+if (options) {
+int err = 0;
+if (s->tcp_keepalive != -1) {
+err = av_dict_set_int(options, "tcp_keepalive", s->tcp_keepalive ? 
1 : 0, 0);
+if (err < 0)
+goto end; /* existing cleanup label in this function */
+}
+if (s->tcp_keepidle > 0) {
+err = av_dict_set_int(options, "tcp_keepidle", s->tcp_keepidle, 0);
+if (err < 0)
+goto end;
+}
+if (s->tcp_keepintvl > 0) {
+err = av_dict_set_int(options, "tcp_keepintvl", s->tcp_keepintvl, 
0);
+if (err < 0)
+goto end;
+}
+if (s->tcp_keepcnt > 0) {
+err = av_dict_set_int(options, "tcp_keepcnt", s->tcp_keepcnt, 0);
+if (err < 0)
+goto end

[FFmpeg-devel] [PATCH] tcp: add TCP keepalive tuning options

2025-11-23 Thread Practice2001 via ffmpeg-devel
Addition of tcp_keepalive, tcp_keepidle, tcp_keepintvl, and tcp_keepcnt
support to the TCP protocol. Exposeing these options to the HTTP protocol
so they can be used for HTTP(S) connections. Updated documentation.
Tested with: ./configure && make && make fate

Fixes ticket #11671.

Signed-off-by: Practice2001 
---
 doc/protocols.texi | 26 ++-
 libavformat/http.c | 36 +
 libavformat/tcp.c  | 80 ++
 3 files changed, 141 insertions(+), 1 deletion(-)

diff --git a/doc/protocols.texi b/doc/protocols.texi
index b74383122a..848f4820e2 100644
--- a/doc/protocols.texi
+++ b/doc/protocols.texi
@@ -989,7 +989,31 @@ Set TCP_NODELAY to disable Nagle's algorithm. Default 
value is 0.
 @item tcp_keepalive=@var{1|0}
 Enable the TCP keepalive mechanism to detect dead peers and help maintain 
long-lived idle connections. Default value is 0.
 
-Only the basic keepalive option (SO_KEEPALIVE) can be enabled or disabled. 
Platform-specific tuning parameters such as TCP_KEEPIDLE, TCP_KEEPINTVL, or 
TCP_KEEPCNT are not configurable and will use the operating system's default 
values.
+The basic keepalive option (SO_KEEPALIVE) can be enabled or disabled. 
Platform-specific tuning parameters such as TCP_KEEPIDLE, TCP_KEEPINTVL, or 
TCP_KEEPCNT are configurable.
+
+@item tcp_keepidle=@var{seconds}
+Set the TCP keepalive idle time (in seconds).
+
+This controls how long the connection must remain idle before the first
+keepalive probe is sent.
+
+Default is 0 (uses system default).
+
+@item tcp_keepintvl=@var{seconds}
+Set the interval (in seconds) between individual TCP keepalive probes.
+
+Default is 0 (uses system default).
+
+@item tcp_keepcnt=@var{count}
+Set the number of unacknowledged keepalive probes that must occur before
+the connection is considered dead.
+
+Default is 0 (uses system default).
+
+@emph{Note:}
+These platform-specific parameters are available on Linux and BSD-derived
+systems. On Windows, only basic keepalive configuration is supported and
+the underlying system will use its own values for probe timing and count.
 
 @end table
 
diff --git a/libavformat/http.c b/libavformat/http.c
index c4e6292a95..1c264d845c 100644
--- a/libavformat/http.c
+++ b/libavformat/http.c
@@ -146,6 +146,11 @@ typedef struct HTTPContext {
 unsigned int retry_after;
 int reconnect_max_retries;
 int reconnect_delay_total_max;
+/* TCP keepalive forwarding */
+int tcp_keepalive;/* -1 = unset, 0 = off, 1 = on */
+int tcp_keepidle; /* seconds, 0 = unset */
+int tcp_keepintvl;/* seconds, 0 = unset */
+int tcp_keepcnt;  /* probe count, 0 = unset */
 } HTTPContext;
 
 #define OFFSET(x) offsetof(HTTPContext, x)
@@ -191,6 +196,10 @@ static const AVOption options[] = {
 { "resource", "The resource requested by a client", OFFSET(resource), 
AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, E },
 { "reply_code", "The http status code to return to a client", 
OFFSET(reply_code), AV_OPT_TYPE_INT, { .i64 = 200}, INT_MIN, 599, E},
 { "short_seek_size", "Threshold to favor readahead over seek.", 
OFFSET(short_seek_size), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, D },
+{ "tcp_keepalive", "Enable SO_KEEPALIVE on underlying TCP socket", 
OFFSET(tcp_keepalive), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, D | E },
+{ "tcp_keepidle", "TCP keepalive idle time (seconds) for underlying TCP 
socket", OFFSET(tcp_keepidle), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, D | E 
},
+{ "tcp_keepintvl", "TCP keepalive interval (seconds) for underlying TCP 
socket", OFFSET(tcp_keepintvl), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, D | 
E },
+{ "tcp_keepcnt", "TCP keepalive probe count for underlying TCP socket", 
OFFSET(tcp_keepcnt), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, D | E },
 { NULL }
 };
 
@@ -282,6 +291,33 @@ static int http_open_cnx_internal(URLContext *h, 
AVDictionary **options)
 
 ff_url_join(buf, sizeof(buf), lower_proto, NULL, hostname, port, NULL);
 
+
+/* Forward TCP keepalive options to underlying protocol via options dict.
+* Prefer using AVDictionary forwarding to modifying the URL (safer). */
+if (options) {
+int err = 0;
+if (s->tcp_keepalive != -1) {
+err = av_dict_set_int(options, "tcp_keepalive", s->tcp_keepalive ? 
1 : 0, 0);
+if (err < 0)
+goto end; /* existing cleanup label in this function */
+}
+if (s->tcp_keepidle > 0) {
+err = av_dict_set_int(options, "tcp_keepidle", s->tcp_keepidle, 0);
+if (err < 0)
+goto end;
+}
+if (s->tcp_keepintvl > 0) {
+err = av_dict_set_int(options, "tcp_keepintvl", s->tcp_keepintvl, 
0);
+if (err < 0)
+goto end;
+}
+if (s->tcp_keepcnt > 0) {
+err = av_dict_set_int(options, "tcp_keepcnt", s->tcp_keepcnt, 0);
+if (err < 0)
+goto end

[FFmpeg-devel] [PATCH] configure: fix static library suffix for MSVC builds

2025-11-22 Thread Practice2001 via ffmpeg-devel
MSVC static builds currently always use ".a" for static library suffixes
(e.g. libavcodec.a, libavformat.a). This causes failures when linking
with Visual Studio, which expects ".lib" static libraries.

This patch adjusts configure so that when --toolchain=msvc is used,
LIBSUF is correctly set to ".lib" instead of the Unix default ".a".

This fixes linking with MSVC and Visual Studio-based build systems.

---

Environment:
 - Windows 10
 - MSYS2 (mingw64)
 - Visual Studio 2019 / 2022
 - NASM installed
 - MSVC toolchain selected

Commands:

./configure --toolchain=msvc --enable-static --disable-shared
make

Resulting static libraries incorrectly have ".a" suffix, e.g.:

libavcodec.a
libavformat.a
libavutil.a

Linking test program with MSVC:

cl test_ffmpeg.c /I C:\ffmpeg-test-install\include ^
/link /LIBPATH:C:\ffmpeg-test-install\lib avcodec.lib avformat.lib 
avutil.lib

Error:

LINK : fatal error LNK1181: cannot open input file 'avcodec.lib'

---

Verification of the fix:

After applying this patch and rebuilding:

Generated static libraries:

avcodec.lib
avformat.lib
avutil.lib

Test program build script (build_test.bat):

cl test_ffmpeg.c ^
/I C:\ffmpeg-test-install\include ^
/link /LIBPATH:C:\ffmpeg-test-install\lib ^
avcodec.lib avformat.lib avutil.lib

Output:
D:\ffmpeg\temp>.\buid_test.bat
Microsoft (R) C/C++ Optimizing Compiler Version 19.50.35718 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

test_ffmpeg.c
Microsoft (R) Incremental Linker Version 14.50.35718.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:test_ffmpeg.exe
/LIBPATH:C:\ffmpeg-test-install\lib
avcodec.lib
avformat.lib
avutil.lib
/OUT:test.exe
test_ffmpeg.obj

=== BUILD SUCCESS ===
Running test...
FFmpeg avcodec version: 4068196
FFmpeg avformat version: 4064871
FFmpeg avutil version: 3936868

This confirms MSVC accepts the .lib suffix and the libraries function
correctly.

Closes: #11242
---
 configure | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/configure b/configure
index 47f9b31439..596d66ce53 100755
--- a/configure
+++ b/configure
@@ -6067,7 +6067,7 @@ case $target_os in
 win32|win64)
 disable symver
 LIBPREF=""
-   LIBSUF=".lib"
+LIBSUF=".lib"
 if enabled shared; then
 # Cannot build both shared and static libs with MSVC or icl.
 disable static
-- 
2.34.1

___
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]


[FFmpeg-devel] [PATCH] configure: fix static library suffix for MSVC builds

2025-11-22 Thread Practice2001 via ffmpeg-devel
MSVC static builds currently always use ".a" for static library suffixes
(e.g. libavcodec.a, libavformat.a). This causes failures when linking
with Visual Studio, which expects ".lib" static libraries.

This patch adjusts configure so that when --toolchain=msvc is used,
LIBSUF is correctly set to ".lib" instead of the Unix default ".a".

This fixes linking with MSVC and Visual Studio-based build systems.

---

Environment:
 - Windows 10
 - MSYS2 (mingw64)
 - Visual Studio 2019 / 2022
 - NASM installed
 - MSVC toolchain selected

Commands:

./configure --toolchain=msvc --enable-static --disable-shared
make

Resulting static libraries incorrectly have ".a" suffix, e.g.:

libavcodec.a
libavformat.a
libavutil.a

Linking test program with MSVC:

cl test_ffmpeg.c /I C:\ffmpeg-test-install\include ^
/link /LIBPATH:C:\ffmpeg-test-install\lib avcodec.lib avformat.lib 
avutil.lib

Error:

LINK : fatal error LNK1181: cannot open input file 'avcodec.lib'

---

Verification of the fix:

After applying this patch and rebuilding:

Generated static libraries:

avcodec.lib
avformat.lib
avutil.lib

Test program build script (build_test.bat):

cl test_ffmpeg.c ^
/I C:\ffmpeg-test-install\include ^
/link /LIBPATH:C:\ffmpeg-test-install\lib ^
avcodec.lib avformat.lib avutil.lib

Output:
D:\ffmpeg\temp>.\buid_test.bat
Microsoft (R) C/C++ Optimizing Compiler Version 19.50.35718 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

test_ffmpeg.c
Microsoft (R) Incremental Linker Version 14.50.35718.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:test_ffmpeg.exe
/LIBPATH:C:\ffmpeg-test-install\lib
avcodec.lib
avformat.lib
avutil.lib
/OUT:test.exe
test_ffmpeg.obj

=== BUILD SUCCESS ===
Running test...
FFmpeg avcodec version: 4068196
FFmpeg avformat version: 4064871
FFmpeg avutil version: 3936868

This confirms MSVC accepts the .lib suffix and the libraries function
correctly.

---
 configure | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/configure b/configure
index 47f9b31439..596d66ce53 100755
--- a/configure
+++ b/configure
@@ -6067,7 +6067,7 @@ case $target_os in
 win32|win64)
 disable symver
 LIBPREF=""
-   LIBSUF=".lib"
+LIBSUF=".lib"
 if enabled shared; then
 # Cannot build both shared and static libs with MSVC or icl.
 disable static
-- 
2.34.1

___
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]