Browse code

libavformat/tcp.c : add send_buffer_size and recv_buffer_size options

adds two new options that may be set via the dictionary:

- send_buffer_size
- recv_buffer_size

When present, setsockopt() is used with SO_SNDBUF and SO_RCVBUF to set
socket buffer sizes. I chose to make send and receive independent
because buffering requirements are often asymmetric.

Errors in setting the buffer size mean the socket will use its
default, so they are ignored.

There is no sanity checking on values, as the kernel/socket layers
already impose reasonable limits if asked for something crazy.

Rationale for enlarging receive buffers is to reduce susceptibility
to intermittent network delays/congestion. I added setting the send
buffer for symmetry.

Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>

Perette Barella authored on 2016/01/20 03:22:13
Showing 2 changed files
... ...
@@ -1147,6 +1147,12 @@ than this time interval, raise error.
1147 1147
 
1148 1148
 @item listen_timeout=@var{milliseconds}
1149 1149
 Set listen timeout, expressed in milliseconds.
1150
+
1151
+@item recv_buffer_size=@var{bytes}
1152
+Set receive buffer size, expressed bytes.
1153
+
1154
+@item send_buffer_size=@var{bytes}
1155
+Set send buffer size, expressed bytes.
1150 1156
 @end table
1151 1157
 
1152 1158
 The following example shows how to setup a listening TCP connection
... ...
@@ -39,6 +39,8 @@ typedef struct TCPContext {
39 39
     int open_timeout;
40 40
     int rw_timeout;
41 41
     int listen_timeout;
42
+    int recv_buffer_size;
43
+    int send_buffer_size;
42 44
 } TCPContext;
43 45
 
44 46
 #define OFFSET(x) offsetof(TCPContext, x)
... ...
@@ -48,6 +50,8 @@ static const AVOption options[] = {
48 48
     { "listen",          "Listen for incoming connections",  OFFSET(listen),         AV_OPT_TYPE_INT, { .i64 = 0 },     0,       2,       .flags = D|E },
49 49
     { "timeout",     "set timeout (in microseconds) of socket I/O operations", OFFSET(rw_timeout),     AV_OPT_TYPE_INT, { .i64 = -1 },         -1, INT_MAX, .flags = D|E },
50 50
     { "listen_timeout",  "Connection awaiting timeout (in milliseconds)",      OFFSET(listen_timeout), AV_OPT_TYPE_INT, { .i64 = -1 },         -1, INT_MAX, .flags = D|E },
51
+    { "send_buffer_size", "Socket send buffer size (in bytes)",                OFFSET(send_buffer_size), AV_OPT_TYPE_INT, { .i64 = -1 },         -1, INT_MAX, .flags = D|E },
52
+    { "recv_buffer_size", "Socket receive buffer size (in bytes)",             OFFSET(recv_buffer_size), AV_OPT_TYPE_INT, { .i64 = -1 },         -1, INT_MAX, .flags = D|E },
51 53
     { NULL }
52 54
 };
53 55
 
... ...
@@ -150,6 +154,15 @@ static int tcp_open(URLContext *h, const char *uri, int flags)
150 150
 
151 151
     h->is_streamed = 1;
152 152
     s->fd = fd;
153
+    /* Set the socket's send or receive buffer sizes, if specified.
154
+       If unspecified or setting fails, system default is used. */
155
+    if (s->recv_buffer_size > 0) {
156
+        setsockopt (fd, SOL_SOCKET, SO_RCVBUF, &s->recv_buffer_size, sizeof (s->recv_buffer_size));
157
+    }
158
+    if (s->send_buffer_size > 0) {
159
+        setsockopt (fd, SOL_SOCKET, SO_SNDBUF, &s->send_buffer_size, sizeof (s->send_buffer_size));
160
+    }
161
+
153 162
     freeaddrinfo(ai);
154 163
     return 0;
155 164