Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: Jack Lau via ffmpeg-devel <ffmpeg-devel@ffmpeg.org>
To: ffmpeg-devel@ffmpeg.org
Cc: Jack Lau <code@ffmpeg.org>
Subject: [FFmpeg-devel] [PATCH] avformat/whip: several improves in ice_dtls_handshake (PR #20765)
Date: Mon, 27 Oct 2025 12:45:31 -0000
Message-ID: <176156913160.81.14411345504692332918@7d278768979e> (raw)

PR #20765 opened by Jack Lau (JackLau)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/20765
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/20765.patch


>From 94f217adf0b4b91bcabb7cd9747f7b58c9c125e3 Mon Sep 17 00:00:00 2001
From: Jack Lau <jacklau1222@qq.com>
Date: Mon, 27 Oct 2025 16:50:30 +0800
Subject: [PATCH 1/4] avformat/whip: correct the ice time log

The ICE handshake actually finished after recieve
and handle binding request from peer when the peer
is not ice lite mode.

Log the ice status when it really done.

Signed-off-by: Jack Lau <jacklau1222@qq.com>
---
 libavformat/whip.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/libavformat/whip.c b/libavformat/whip.c
index a11fffb9c6..626ebb8303 100644
--- a/libavformat/whip.c
+++ b/libavformat/whip.c
@@ -1321,10 +1321,6 @@ next_packet:
             if (whip->state < WHIP_STATE_ICE_CONNECTED) {
                 if (whip->is_peer_ice_lite)
                     whip->state = WHIP_STATE_ICE_CONNECTED;
-                whip->whip_ice_time = av_gettime_relative();
-                av_log(whip, AV_LOG_VERBOSE, "ICE STUN ok, state=%d, url=udp://%s:%d, location=%s, username=%s:%s, res=%dB, elapsed=%.2fms\n",
-                    whip->state, whip->ice_host, whip->ice_port, whip->whip_resource_url ? whip->whip_resource_url : "",
-                    whip->ice_ufrag_remote, whip->ice_ufrag_local, ret, ELAPSED(whip->whip_starttime, av_gettime_relative()));
 
                 ff_url_join(buf, sizeof(buf), "dtls", NULL, whip->ice_host, whip->ice_port, NULL);
                 av_dict_set_int(&opts, "mtu", whip->pkt_size, 0);
@@ -1360,9 +1356,13 @@ next_packet:
 
         /* Handle DTLS handshake */
         if (is_dtls_packet(whip->buf, ret) || is_dtls_active) {
+            whip->whip_ice_time = av_gettime_relative();
             /* Start consent timer when ICE selected */
-            whip->whip_last_consent_tx_time = whip->whip_last_consent_rx_time = av_gettime_relative();
+            whip->whip_last_consent_tx_time = whip->whip_last_consent_rx_time = whip->whip_ice_time;
             whip->state = WHIP_STATE_ICE_CONNECTED;
+            av_log(whip, AV_LOG_VERBOSE, "ICE STUN ok, state=%d, url=udp://%s:%d, location=%s, username=%s:%s, res=%dB, elapsed=%.2fms\n",
+                whip->state, whip->ice_host, whip->ice_port, whip->whip_resource_url ? whip->whip_resource_url : "",
+                whip->ice_ufrag_remote, whip->ice_ufrag_local, ret, ELAPSED(whip->whip_starttime, whip->whip_ice_time));
             ret = ffurl_handshake(whip->dtls_uc);
             if (ret < 0) {
                 whip->state = WHIP_STATE_FAILED;
-- 
2.49.1


>From e69481bc7cd7528dd2596a2b2eccd9a51997fb9c Mon Sep 17 00:00:00 2001
From: Jack Lau <jacklau1222@qq.com>
Date: Mon, 27 Oct 2025 19:58:05 +0800
Subject: [PATCH 2/4] avformat/whip: put dtls init code into dtls_initialize

Signed-off-by: Jack Lau <jacklau1222@qq.com>
---
 libavformat/whip.c | 50 ++++++++++++++++++++++++----------------------
 1 file changed, 26 insertions(+), 24 deletions(-)

diff --git a/libavformat/whip.c b/libavformat/whip.c
index 626ebb8303..719072bf0e 100644
--- a/libavformat/whip.c
+++ b/libavformat/whip.c
@@ -381,15 +381,39 @@ static av_cold int certificate_key_init(AVFormatContext *s)
 
 static av_cold int dtls_initialize(AVFormatContext *s)
 {
+    int ret = 0;
     WHIPContext *whip = s->priv_data;
+    int is_dtls_active = whip->flags & WHIP_DTLS_ACTIVE;
+    AVDictionary *opts = NULL;
+    char buf[256];
+
+    ff_url_join(buf, sizeof(buf), "dtls", NULL, whip->ice_host, whip->ice_port, NULL);
+    av_dict_set_int(&opts, "mtu", whip->pkt_size, 0);
+    if (whip->cert_file) {
+        av_dict_set(&opts, "cert_file", whip->cert_file, 0);
+    } else
+        av_dict_set(&opts, "cert_pem", whip->cert_buf, 0);
+
+    if (whip->key_file) {
+        av_dict_set(&opts, "key_file", whip->key_file, 0);
+    } else
+        av_dict_set(&opts, "key_pem", whip->key_buf, 0);
+    av_dict_set_int(&opts, "external_sock", 1, 0);
+    av_dict_set_int(&opts, "use_srtp", 1, 0);
+    av_dict_set_int(&opts, "listen", is_dtls_active ? 0 : 1, 0);
+    ret = ffurl_open_whitelist(&whip->dtls_uc, buf, AVIO_FLAG_READ_WRITE, &s->interrupt_callback,
+        &opts, s->protocol_whitelist, s->protocol_blacklist, NULL);
+    av_dict_free(&opts);
+    if (ret < 0)
+        goto end;
     /* reuse the udp created by whip */
     ff_tls_set_external_socket(whip->dtls_uc, whip->udp);
 
     /* Make the socket non-blocking */
     ff_socket_nonblock(ffurl_get_file_handle(whip->dtls_uc), 1);
     whip->dtls_uc->flags |= AVIO_FLAG_NONBLOCK;
-
-    return 0;
+end:
+    return ret;
 }
 
 /**
@@ -1259,8 +1283,6 @@ static int ice_dtls_handshake(AVFormatContext *s)
     int64_t starttime = av_gettime_relative(), now;
     WHIPContext *whip = s->priv_data;
     int is_dtls_active = whip->flags & WHIP_DTLS_ACTIVE;
-    AVDictionary *opts = NULL;
-    char buf[256];
 
     if (whip->state < WHIP_STATE_UDP_CONNECTED || !whip->udp) {
         av_log(whip, AV_LOG_ERROR, "UDP not connected, state=%d, udp=%p\n", whip->state, whip->udp);
@@ -1322,26 +1344,6 @@ next_packet:
                 if (whip->is_peer_ice_lite)
                     whip->state = WHIP_STATE_ICE_CONNECTED;
 
-                ff_url_join(buf, sizeof(buf), "dtls", NULL, whip->ice_host, whip->ice_port, NULL);
-                av_dict_set_int(&opts, "mtu", whip->pkt_size, 0);
-                if (whip->cert_file) {
-                    av_dict_set(&opts, "cert_file", whip->cert_file, 0);
-                } else
-                    av_dict_set(&opts, "cert_pem", whip->cert_buf, 0);
-
-                if (whip->key_file) {
-                    av_dict_set(&opts, "key_file", whip->key_file, 0);
-                } else
-                    av_dict_set(&opts, "key_pem", whip->key_buf, 0);
-                av_dict_set_int(&opts, "external_sock", 1, 0);
-                av_dict_set_int(&opts, "use_srtp", 1, 0);
-                av_dict_set_int(&opts, "listen", is_dtls_active ? 0 : 1, 0);
-                /* If got the first binding response, start DTLS handshake. */
-                ret = ffurl_open_whitelist(&whip->dtls_uc, buf, AVIO_FLAG_READ_WRITE, &s->interrupt_callback,
-                    &opts, s->protocol_whitelist, s->protocol_blacklist, NULL);
-                av_dict_free(&opts);
-                if (ret < 0)
-                    goto end;
                 dtls_initialize(s);
             }
             goto next_packet;
-- 
2.49.1


>From 485bb672756d4844daca6157046c59356cbebda8 Mon Sep 17 00:00:00 2001
From: Jack Lau <jacklau1222@qq.com>
Date: Mon, 27 Oct 2025 20:06:23 +0800
Subject: [PATCH 3/4] avformat/whip: handle the error of dtls_initialize

Correct the log level to AV_LOG_ERROR when dtls fails

Signed-off-by: Jack Lau <jacklau1222@qq.com>
---
 libavformat/whip.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/libavformat/whip.c b/libavformat/whip.c
index 719072bf0e..4baf9dcdec 100644
--- a/libavformat/whip.c
+++ b/libavformat/whip.c
@@ -404,8 +404,10 @@ static av_cold int dtls_initialize(AVFormatContext *s)
     ret = ffurl_open_whitelist(&whip->dtls_uc, buf, AVIO_FLAG_READ_WRITE, &s->interrupt_callback,
         &opts, s->protocol_whitelist, s->protocol_blacklist, NULL);
     av_dict_free(&opts);
-    if (ret < 0)
+    if (ret < 0) {
+        av_log(whip, AV_LOG_ERROR, "Failed to open DTLS url:%s\n", buf);
         goto end;
+    }
     /* reuse the udp created by whip */
     ff_tls_set_external_socket(whip->dtls_uc, whip->udp);
 
@@ -1343,8 +1345,6 @@ next_packet:
             if (whip->state < WHIP_STATE_ICE_CONNECTED) {
                 if (whip->is_peer_ice_lite)
                     whip->state = WHIP_STATE_ICE_CONNECTED;
-
-                dtls_initialize(s);
             }
             goto next_packet;
         }
@@ -1365,10 +1365,14 @@ next_packet:
             av_log(whip, AV_LOG_VERBOSE, "ICE STUN ok, state=%d, url=udp://%s:%d, location=%s, username=%s:%s, res=%dB, elapsed=%.2fms\n",
                 whip->state, whip->ice_host, whip->ice_port, whip->whip_resource_url ? whip->whip_resource_url : "",
                 whip->ice_ufrag_remote, whip->ice_ufrag_local, ret, ELAPSED(whip->whip_starttime, whip->whip_ice_time));
+
+            ret = dtls_initialize(s);
+            if (ret < 0)
+                goto end;
             ret = ffurl_handshake(whip->dtls_uc);
             if (ret < 0) {
                 whip->state = WHIP_STATE_FAILED;
-                av_log(whip, AV_LOG_VERBOSE, "DTLS session failed\n");
+                av_log(whip, AV_LOG_ERROR, "DTLS session failed\n");
                 goto end;
             }
             if (!ret) {
-- 
2.49.1


>From b2a48bc0156711d740db793dbc586d05cefe22cd Mon Sep 17 00:00:00 2001
From: Jack Lau <jacklau1222@qq.com>
Date: Mon, 27 Oct 2025 20:30:24 +0800
Subject: [PATCH 4/4] avformat/whip: remove redundant NONBLOCK set for dtls

DTLS handshake already force set BLOCK mode.

Signed-off-by: Jack Lau <jacklau1222@qq.com>
---
 libavformat/whip.c | 4 ----
 1 file changed, 4 deletions(-)

diff --git a/libavformat/whip.c b/libavformat/whip.c
index 4baf9dcdec..9a996b4e0c 100644
--- a/libavformat/whip.c
+++ b/libavformat/whip.c
@@ -410,10 +410,6 @@ static av_cold int dtls_initialize(AVFormatContext *s)
     }
     /* reuse the udp created by whip */
     ff_tls_set_external_socket(whip->dtls_uc, whip->udp);
-
-    /* Make the socket non-blocking */
-    ff_socket_nonblock(ffurl_get_file_handle(whip->dtls_uc), 1);
-    whip->dtls_uc->flags |= AVIO_FLAG_NONBLOCK;
 end:
     return ret;
 }
-- 
2.49.1

_______________________________________________
ffmpeg-devel mailing list -- ffmpeg-devel@ffmpeg.org
To unsubscribe send an email to ffmpeg-devel-leave@ffmpeg.org

                 reply	other threads:[~2025-10-27 12:46 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=176156913160.81.14411345504692332918@7d278768979e \
    --to=ffmpeg-devel@ffmpeg.org \
    --cc=code@ffmpeg.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git