From fe8be0cdb6ed76a67283bb1000585af79b492d4d Mon Sep 17 00:00:00 2001
From: Cary Tetrick <cary.tetrick@gmail.com>
Date: Thu, 26 Feb 2015 15:18:47 -0600
Subject: [PATCH]  Topic: Option to control no_delay flag in librtmp

 This adds another option to a change authored by Brian Brice <bbrice@gmail.com> 2015-01-19
 "librtmp: Allow changing the socket send buffer size"
 (Brian is aware of this change).

 Unlike a previous patch, this has no dependency on rtmpdump.

 Options to the ffmpeg commandline in support of now arguments in librtmp.
     rtmp_buffer_size - sets rtmp socket send buffer size in bytes.
         this is the same as the patch it replaces.
     rtmp_nodelay - on or off. defaults to on.
         allows users to control whether nagel is active.

 See this thread on Zeranoe forum:
 http://ffmpeg.zeranoe.com/forum/viewtopic.php?f=7&t=657&p=7823#p7823

 I believe this addresses case 1604 on the ffmpeg tracker.
 "Summary of the bug: Rtmp output to justin.tv fps continually drops, ..."
 - note the memory problem mentioned had been fixed. This fix only applies if
 librtmp is enabled.
 https://trac.ffmpeg.org/ticket/1604

 Defualt behavior leaves things unchanged. Users must employ options intentionally.
---
 libavformat/librtmp.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/libavformat/librtmp.c b/libavformat/librtmp.c
index bfa9a71..64ae59d 100644
--- a/libavformat/librtmp.c
+++ b/libavformat/librtmp.c
@@ -52,6 +52,7 @@ typedef struct LibRTMPContext {
     int live;
     char *temp_filename;
     int buffer_size;
+    int tcp_nodelay;
 } LibRTMPContext;
 
 static void rtmp_log(int level, const char *fmt, va_list args)
@@ -239,6 +240,11 @@ static int rtmp_open(URLContext *s, const char *uri, int flags)
         int tmp = ctx->buffer_size;
         setsockopt(r->m_sb.sb_socket, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp));
     }
+
+    if (flags & AVIO_FLAG_WRITE) {
+        int tmp = ctx->tcp_nodelay;
+        setsockopt(r->m_sb.sb_socket, IPPROTO_TCP, TCP_NODELAY, &tmp, sizeof(tmp));
+    }
 #endif
 
     s->is_streamed = 1;
@@ -323,7 +329,10 @@ static const AVOption options[] = {
     {"rtmp_swfverify", "URL to player swf file, compute hash/size automatically. (unimplemented)", OFFSET(swfverify), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC},
     {"rtmp_tcurl", "URL of the target stream. Defaults to proto://host[:port]/app.", OFFSET(tcurl), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC|ENC},
 #if CONFIG_NETWORK
-    {"rtmp_buffer_size", "set buffer size in bytes", OFFSET(buffer_size), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, DEC|ENC },
+    {"rtmp_buffer_size", "Set buffer size in bytes. Otherwise, use system default.", OFFSET(buffer_size), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, DEC|ENC },
+    {"rtmp_nodelay", "Use TCP_NODELAY", OFFSET(tcp_nodelay), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, ENC, "rtmp_nodelay"},
+    {"off", "Nagel algorithm active", 0, AV_OPT_TYPE_CONST, {.i64 = 0}, 0, 0, ENC, "rtmp_nodelay"},
+    {"on", "Nagel algorithm disabled. Disabled by default.", 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, 0, 0, ENC, "rtmp_nodelay"},
 #endif
     { NULL },
 };
-- 
1.9.4.msysgit.2

