From f1c167496e41cabc2bd1b890b149e4b34648cad6 Mon Sep 17 00:00:00 2001 From: Olivier Langlois Date: Tue, 8 Apr 2014 23:21:52 -0400 Subject: [PATCH] Support broadcast destination for udp protocol Use the required socket option SO_BROADCAST to be able to stream to a broadcast address. Prior to the patch, trying to stream to a broadcast address was resulting to the following error: av_interleaved_write_frame(): Permission denied The patch has been tested with: ffmpeg -f v4l2 -framerate 30 -input_format yuyv422 -video_size 640x480 -i /dev/video0 \ -c:v libx264 -profile:v high -preset ultrafast -tune zerolatency -b:v 500k -pix_fmt yuv420p \ -f mpegts udp://192.168.1.255:5004?broadcast=1 I have added an option to let the user explicitly request broadcast in order to avoid ffmpeg to broadcast unintentionally. Signed-off-by: Olivier Langlois Signed-off-by: Michael Niedermayer --- libavformat/udp.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/libavformat/udp.c b/libavformat/udp.c index 65c7a5549d..91b411e679 100644 --- a/libavformat/udp.c +++ b/libavformat/udp.c @@ -62,6 +62,7 @@ typedef struct { int ttl; int buffer_size; int is_multicast; + int is_broadcast; int local_port; int reuse_socket; int overrun_nonfatal; @@ -96,6 +97,7 @@ static const AVOption options[] = { {"localaddr", "choose local IP address", OFFSET(local_addr), AV_OPT_TYPE_STRING, {.str = ""}, 0, 0, D|E }, {"pkt_size", "set size of UDP packets", OFFSET(packet_size), AV_OPT_TYPE_INT, {.i64 = 1472}, 0, INT_MAX, D|E }, {"reuse", "explicitly allow or disallow reusing UDP sockets", OFFSET(reuse_socket), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, D|E }, +{"broadcast", "explicitly allow or disallow broadcast destination", OFFSET(is_broadcast), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, E }, {"ttl", "set the time to live value (for multicast only)", OFFSET(ttl), AV_OPT_TYPE_INT, {.i64 = 16}, 0, INT_MAX, E }, {"connect", "set if connect() should be called on socket", OFFSET(is_connected), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, D|E }, /* TODO 'sources', 'block' option */ @@ -602,6 +604,8 @@ static int udp_open(URLContext *h, const char *uri, int flags) } if (!is_output && av_find_info_tag(buf, sizeof(buf), "timeout", p)) s->timeout = strtol(buf, NULL, 10); + if (is_output && av_find_info_tag(buf, sizeof(buf), "broadcast", p)) + s->is_broadcast = strtol(buf, NULL, 10); } /* handling needed to support options picking from both AVOption and URL */ s->circular_buffer_size *= 188; @@ -642,6 +646,11 @@ static int udp_open(URLContext *h, const char *uri, int flags) goto fail; } + if (s->is_broadcast) { + if (setsockopt (udp_fd, SOL_SOCKET, SO_BROADCAST, &(s->is_broadcast), sizeof(s->is_broadcast)) != 0) + goto fail; + } + /* If multicast, try binding the multicast address first, to avoid * receiving UDP packets from other sources aimed at the same UDP * port. This fails on windows. This makes sending to the same address