[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 <olivier@trillion01.com> --- libavformat/udp.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/libavformat/udp.c b/libavformat/udp.c index d9a18d5..7d204c9 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 -- 1.9.1
On Tue, Apr 08, 2014 at 11:21:52PM -0400, Olivier Langlois wrote:
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 <olivier@trillion01.com>
patch applied please add entry to our udp documentation for this as well thx [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB The real ebay dictionary, page 2 "100% positive feedback" - "All either got their money back or didnt complain" "Best seller ever, very honest" - "Seller refunded buyer after failed scam"
On Tue, 2014-05-13 at 16:00 +0200, Michael Niedermayer wrote:
On Tue, Apr 08, 2014 at 11:21:52PM -0400, Olivier Langlois wrote:
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 <olivier@trillion01.com>
patch applied
please add entry to our udp documentation for this as well
thx Thank you Michael. I will update and offer a patch for the udp documentation soon.
As a sidenote and possibly worthwhile to include as a warning in the udp documentation. It is that I have discovered a network HW "feature" that I wasn't aware of. It is that some Ethernet switches have what they call a "Broadcast storm protection". If a broadcasted stream go through such a switch, a lot of packets will be missing on the receiving side. Linksys sd216 was not impacted by this but the Netgear GS605 had such a protection. I finally ended up upgrading my switch to a Netgear ProSafe M4100-D12G Managed Switch where you can turn on/off the broadcast storm protection. I am sorry to sound like an infotainment for Ethernet switch products but if raising the awareness on this feature to others can spare some pain to them, this is hopefully worthwhile to share. greetings,
participants (2)
-
Michael Niedermayer -
Olivier Langlois