PR #23378 opened by qwerzoid
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/23378
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/23378.patch

Looping on a non-blocking socket results in unnecessary cpu consumption.
Hence, using poll() with timeout to detect when the data is available.
This approach results in optimised cpu resources.

Signed-off-by: Aditya Banavi <[email protected]>

# Summary of changes

Briefly describe what this PR does and why.

<!--
If this PR requires new FATE test samples, attach them to the PR and
list their target paths below (relative to the fate-suite root).

Attached filenames must match the sample's filename:

```fate-samples
# e.g. vorbis/new-sample.ogg
```
-->



>From 65357fdf34986f78f87e5273551b3538ec231e79 Mon Sep 17 00:00:00 2001
From: Aditya Banavi <[email protected]>
Date: Wed, 3 Jun 2026 15:47:40 +0000
Subject: [PATCH] avformat/whip: Using poll() in ice_dtls_handshake()

Looping on a non-blocking socket results in unnecessary cpu consumption.
Hence, using poll() with timeout to detect when the data is available.
This approach results in optimised cpu resources.

Signed-off-by: Aditya Banavi <[email protected]>
---
 libavformat/whip.c | 35 +++++++++++++++++++++++------------
 1 file changed, 23 insertions(+), 12 deletions(-)

diff --git a/libavformat/whip.c b/libavformat/whip.c
index edd9136d3e..a4e368fc33 100644
--- a/libavformat/whip.c
+++ b/libavformat/whip.c
@@ -1297,10 +1297,14 @@ end:
 
 static int ice_dtls_handshake(AVFormatContext *s)
 {
-    int ret = 0, size, i;
+    int ret = 0, size, n;
     int64_t starttime = av_gettime_relative(), now;
     WHIPContext *whip = s->priv_data;
     int is_dtls_active = whip->flags & WHIP_DTLS_ACTIVE;
+    struct pollfd pfd;
+
+    pfd.fd = ffurl_get_file_handle(whip->udp);
+    pfd.events = POLLIN;
 
     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);
@@ -1340,21 +1344,28 @@ next_packet:
         }
 
         /* Read the STUN or DTLS messages from peer. */
-        for (i = 0; i < ICE_DTLS_READ_MAX_RETRY; i++) {
-            if (whip->state > WHIP_STATE_ICE_CONNECTED)
-                break;
-            ret = ffurl_read(whip->udp, whip->buf, sizeof(whip->buf));
-            if (ret > 0)
-                break;
-            if (ret == AVERROR(EAGAIN)) {
-                av_usleep(ICE_DTLS_READ_SLEEP_DURATION * WHIP_US_PER_MS);
+        n = poll(&pfd, 1, whip->handshake_timeout);
+        if (n < 0) {
+            if (errno == EINTR) {
                 continue;
             }
-            if (is_dtls_active)
-                break;
-            av_log(whip, AV_LOG_ERROR, "Failed to read message\n");
+            av_log(whip, AV_LOG_ERROR, "Poll failed\n");
+            ret = AVERROR(errno);
             goto end;
         }
+        if (n == 0) {
+            continue;
+        }
+        if (pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
+            av_log(whip, AV_LOG_ERROR, "Poll error revents = 0x%x\n", 
pfd.revents);
+            ret = AVERROR(EIO);
+            goto end;
+        }            
+        if (pfd.revents & POLLIN) {               
+            ret = ffurl_read(whip->udp, whip->buf, sizeof(whip->buf));
+            if (ret == AVERROR(EAGAIN))
+                continue;
+        }
 
         /* Handle the ICE binding response. */
         if (ice_is_binding_response(whip->buf, ret)) {
-- 
2.52.0

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

Reply via email to