[FFmpeg-devel] [PATCH 2/4] libavformat/tcp: Added an option to reuse sockets

Hendrik Leppkes h.leppkes at gmail.com
Fri Nov 3 10:38:55 EET 2017


On Fri, Nov 3, 2017 at 9:27 AM, Karthick J <kjeyapal at akamai.com> wrote:
> ---
>  doc/protocols.texi  |   4 ++
>  libavformat/tcp.c   | 150 ++++++++++++++++++++++++++++++++++++++++++++++++++++
>  libavformat/tcp.h   |  27 ++++++++++
>  libavformat/utils.c |   2 +
>  4 files changed, 183 insertions(+)
>  create mode 100644 libavformat/tcp.h
>
> diff --git a/doc/protocols.texi b/doc/protocols.texi
> index a7968ff..62d317d 100644
> --- a/doc/protocols.texi
> +++ b/doc/protocols.texi
> @@ -1242,6 +1242,10 @@ Set receive buffer size, expressed bytes.
>
>  @item send_buffer_size=@var{bytes}
>  Set send buffer size, expressed bytes.
> +
> + at item reuse_sockets=@var{1|0}
> +Reuse sockets instead of opening a new socket each time.
> +Default value is 0.
>  @end table
>
>  The following example shows how to setup a listening TCP connection
> diff --git a/libavformat/tcp.c b/libavformat/tcp.c
> index 06368ff..8bca628 100644
> --- a/libavformat/tcp.c
> +++ b/libavformat/tcp.c
> @@ -1,6 +1,7 @@
>  /*
>   * TCP protocol
>   * Copyright (c) 2002 Fabrice Bellard
> + * Copyright (c) 2017 Akamai Technologies, Inc
>   *
>   * This file is part of FFmpeg.
>   *
> @@ -19,6 +20,8 @@
>   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
>   */
>  #include "avformat.h"
> +#include "tcp.h"
> +#include "libavcodec/internal.h"
>  #include "libavutil/avassert.h"
>  #include "libavutil/parseutils.h"
>  #include "libavutil/opt.h"
> @@ -38,6 +41,7 @@ typedef struct TCPOptions {
>      int listen_timeout;
>      int recv_buffer_size;
>      int send_buffer_size;
> +    int reuse_sockets;
>  } TCPOptions;
>
>  typedef struct TCPContext {
> @@ -47,6 +51,16 @@ typedef struct TCPContext {
>      TCPOptions options;
>  } TCPContext;
>
> +typedef struct TCPSocket {
> +    char *hostname;
> +    int  port;
> +    int in_use;
> +    int64_t last_close_time;
> +    int  fd;
> +    TCPOptions options;
> +    struct TCPSocket *next;
> +} TCPSocket;
> +
>  #define OFFSET(x) (offsetof(TCPContext, options) + offsetof(TCPOptions, x))
>  #define D AV_OPT_FLAG_DECODING_PARAM
>  #define E AV_OPT_FLAG_ENCODING_PARAM
> @@ -56,6 +70,7 @@ static const AVOption options[] = {
>      { "listen_timeout",  "Connection awaiting timeout (in milliseconds)",      OFFSET(listen_timeout), AV_OPT_TYPE_INT, { .i64 = -1 },         -1, INT_MAX, .flags = D|E },
>      { "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 },
>      { "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 },
> +    { "reuse_sockets", "Reuse sockets instead of opening a new socket each time", OFFSET(reuse_sockets), AV_OPT_TYPE_BOOL, { .i64 = 0 },         0, 1, .flags = D|E },
>      { NULL }
>  };
>
> @@ -66,6 +81,116 @@ static const AVClass tcp_class = {
>      .version    = LIBAVUTIL_VERSION_INT,
>  };
>
> +static TCPSocket *first_socket = NULL;

We're not very fond of global state, especially in new code. A better
way would be to have a socket pool structure that a caller can manage,
instead of having it globally.
(Additionally, we're also trying to really get rid of the global
avformat lock, which this is using).


More information about the ffmpeg-devel mailing list