[FFmpeg-devel] [PATCH 13/18] avformat/udp: add function to set remote address directly

Timo Rothenpieler timo at rothenpieler.org
Wed Jul 2 19:56:41 EEST 2025


---
 libavformat/network.h |  1 +
 libavformat/udp.c     | 30 ++++++++++++++++++++++++++++++
 2 files changed, 31 insertions(+)

diff --git a/libavformat/network.h b/libavformat/network.h
index 48bb75a758..5734b664cd 100644
--- a/libavformat/network.h
+++ b/libavformat/network.h
@@ -339,5 +339,6 @@ int ff_connect_parallel(struct addrinfo *addrs, int timeout_ms_per_address,
                         int (*customize_fd)(void *, int, int), void *customize_ctx);
 
 void ff_udp_get_last_recv_addr(URLContext *h, struct sockaddr_storage *addr);
+int ff_udp_set_remote_addr(URLContext *h, const struct sockaddr *dest_addr, socklen_t dest_addr_len, int do_connect);
 
 #endif /* AVFORMAT_NETWORK_H */
diff --git a/libavformat/udp.c b/libavformat/udp.c
index e02eff0f33..3657acb985 100644
--- a/libavformat/udp.c
+++ b/libavformat/udp.c
@@ -458,6 +458,36 @@ int ff_udp_set_remote_url(URLContext *h, const char *uri)
     return 0;
 }
 
+/**
+ * This function is identical to ff_udp_set_remote_url, except that it takes a sockaddr directly.
+ */
+int ff_udp_set_remote_addr(URLContext *h, const struct sockaddr *dest_addr, socklen_t dest_addr_len, int do_connect)
+{
+    UDPContext *s = h->priv_data;
+
+    /* set the destination address */
+    if (dest_addr_len < 0 || dest_addr_len > sizeof(s->dest_addr))
+        return AVERROR(EIO);
+    s->dest_addr_len = dest_addr_len;
+    memcpy(&s->dest_addr, dest_addr, dest_addr_len);
+
+    s->is_multicast = ff_is_multicast_address((struct sockaddr*) &s->dest_addr);
+    if (do_connect >= 0) {
+        int was_connected = s->is_connected;
+        s->is_connected = do_connect;
+        if (s->is_connected && !was_connected) {
+            if (connect(s->udp_fd, (struct sockaddr *) &s->dest_addr,
+                        s->dest_addr_len)) {
+                s->is_connected = 0;
+                ff_log_net_error(h, AV_LOG_ERROR, "connect");
+                return AVERROR(EIO);
+            }
+        }
+    }
+
+    return 0;
+}
+
 /**
  * Return the local port used by the UDP connection
  * @param h media file context
-- 
2.49.0



More information about the ffmpeg-devel mailing list