[FFmpeg-cvslog] avformat/udp: add function to set remote address directly
Timo Rothenpieler
git at videolan.org
Fri Jul 11 19:04:13 EEST 2025
ffmpeg | branch: master | Timo Rothenpieler <timo at rothenpieler.org> | Sat Jun 28 20:58:20 2025 +0200| [2604c86c1f8e6a07840054ad51cb26a99d06b640] | committer: Timo Rothenpieler
avformat/udp: add function to set remote address directly
> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=2604c86c1f8e6a07840054ad51cb26a99d06b640
---
libavformat/network.h | 1 +
libavformat/udp.c | 30 ++++++++++++++++++++++++++++++
2 files changed, 31 insertions(+)
diff --git a/libavformat/network.h b/libavformat/network.h
index a6ce2fa225..7c72c902aa 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, socklen_t *addr_len);
+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 2ca02cad0c..0fde3548e7 100644
--- a/libavformat/udp.c
+++ b/libavformat/udp.c
@@ -465,6 +465,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
More information about the ffmpeg-cvslog
mailing list