[vlc-devel] [PATCH 1/2] access{_out}:srt: introduce SRT input/ouput

Jean-Baptiste Kempf jb at videolan.org
Tue Aug 8 21:52:31 CEST 2017


Hello Justin,

On Tue, 8 Aug 2017, at 20:49, Justin Kim wrote:
> Secure Reliable Transport (SRT) is a proprietary transport technology
> that optimizes streaming performance across unpredictable networks.
> 
> Signed-off-by: Justin Kim <justin.kim at collabora.com>
> ---
>  configure.ac                      |  29 +++
>  modules/access/Makefile.am        |   8 +
>  modules/access/srt.c              | 237 +++++++++++++++++++++
>  modules/access_output/Makefile.am |   7 +
>  modules/access_output/srt.c       | 428
>  ++++++++++++++++++++++++++++++++++++++
>  5 files changed, 709 insertions(+)
>  create mode 100644 modules/access/srt.c
>  create mode 100644 modules/access_output/srt.c

This is missing a NEWS entry, and a modules/MODULES_LIST entry (use the
script)

> +dnl
> +dnl  SRT plugin
> +dnl
> +AC_ARG_ENABLE(srt,
> +[AS_HELP_STRING([--disable-srt],[srt input/output plugin (default
> auto)])])
> +have_srt="no"
> +AS_IF([test "${enable_srt}" != "no"], [
> +    PKG_CHECK_MODULES(SRT, [srt >= 1.2.0], [
> +        have_srt="yes"
> +        AC_CHECK_HEADER([srt/srt.h], [
> +            VLC_ADD_PLUGIN([srt])
> +            VLC_ADD_CXXFLAGS([srt], [$SRT_CFLAGS])
> +            VLC_ADD_CFLAGS([srt], [$SRT_CFLAGS])
> +            VLC_ADD_LIBS([srt], [$SRT_LIBS])
> +        ], [
> +            AS_IF([test -n "${enable_srt}"],
> +                [AC_MSG_ERROR([${SRT_PKG_ERRORS} (required for srt).])],
> +                [AC_MSG_WARN([${SRT_PKG_ERRORS} (required for srt).])]
> +                )
> +        ])
> +    ], [
> +        AS_IF([test "x${enable_srt}" = "xyes"],
> +            [AC_MSG_ERROR([${SRT_PKG_ERRORS} (required for srt).])],
> +            [AC_MSG_WARN([${SRT_PKG_ERRORS} (required for srt).])]
> +            )
> +    ])
> +])
> +AM_CONDITIONAL([HAVE_SRT], [test "${have_srt}" = "yes"])

Why not using the PKG_VLC macro ? Is that not possible ?

> diff --git a/modules/access/srt.c b/modules/access/srt.c
> new file mode 100644
> index 0000000000..8f37232bec
> --- /dev/null
> +++ b/modules/access/srt.c
> @@ -0,0 +1,237 @@
> +/*****************************************************************************
> + * srt.c: SRT (Secure Reliable Transport) input module
> +
> *****************************************************************************
> + * Copyright (C) 2017, Collabora Ltd.
> + *
> + * Authors: Justin Kim <justin.kim at collabora.com>
> + *
> + * This program is free software; you can redistribute it and/or modify
> it
> + * under the terms of the GNU Lesser General Public License as published
> by
> + * the Free Software Foundation; either version 2.1 of the License, or
> + * (at your option) any later version.
> + *
> + * This program is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
> + * GNU Lesser General Public License for more details.
> + *
> + * You should have received a copy of the GNU Lesser General Public
> License
> + * along with this program; if not, write to the Free Software
> Foundation,
> + * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
> +
> *****************************************************************************/
> +
> +#ifdef HAVE_CONFIG_H
> +# include "config.h"
> +#endif
> +
> +#include <errno.h>
> +#include <vlc_common.h>
> +#include <vlc_plugin.h>
> +#include <vlc_access.h>
> +#include <vlc_interface.h>

Interface?

> +#include <sys/socket.h>
> +#include <netinet/in.h>

This requires ifdefs, else it won't compile for Windows. (Consider
including vlc_network.h maybe)

> +#include <srt/srt.h>
> +
> +#define SRT_DEFAULT_CHUNK_SIZE 1316

Any explanation about this number?

> +static int Open(vlc_object_t *);
> +static void Close(vlc_object_t *);
> +
> +static block_t *BlockSRT( stream_t *, bool * );
> +static int Control( stream_t *, int, va_list );
> +
> +/* Module descriptor */
> +vlc_module_begin()
> +    set_shortname(N_("SRT"))
> +    set_description(N_("SRT input"))
> +    set_category(CAT_INPUT)
> +    set_subcategory( SUBCAT_INPUT_ACCESS )
> +
> +    set_capability("access", 0)
> +    add_shortcut( "srt" )
> +
> +    set_callbacks(Open, Close)
> +vlc_module_end ()
> +
> +struct stream_sys_t
> +{
> +    SRTSOCKET sock;
> +    int i_poll_id;
> +    int i_chunk_size;
> +};
> +
> +static int Open(vlc_object_t *p_this)
> +{
> +    stream_t     *p_stream = (stream_t*)p_this;
> +    stream_sys_t *p_sys;
> +
> +    p_sys = vlc_malloc( p_this, sizeof( *p_sys ) );
> +    if( unlikely( p_sys == NULL ) )
> +        return VLC_ENOMEM;
> +
> +    p_sys->i_chunk_size = SRT_DEFAULT_CHUNK_SIZE;
> +    p_stream->p_sys = p_sys;
> +    p_stream->pf_block = BlockSRT;
> +    p_stream->pf_control = Control;
> +
> +    p_sys->sock = srt_socket(AF_INET, SOCK_DGRAM, 0);
> +    if ( p_sys->sock == SRT_ERROR )
> +    {
> +        msg_Err( p_stream, "Failed to open socket." );
> +        return VLC_EGENERIC;
> +    }
> +
> +    p_sys->i_poll_id = srt_epoll_create();
> +    if ( p_sys->i_poll_id == -1 )
> +    {
> +        msg_Err( p_stream, "Failed to create poll id for SRT socket." );
> +        srt_close ( p_sys->sock );
> +        return VLC_EGENERIC;
> +    }
> +    int modes = SRT_EPOLL_OUT;
> +    srt_epoll_add_usock( p_sys->i_poll_id, p_sys->sock, &modes );
> +
> +    char *psz_name = strdup( p_stream->psz_location );
> +    char *psz_parser;
> +    const char *psz_server_addr, *psz_bind_addr = "";
> +    int  i_bind_port = 1234, i_server_port = 0;

Is that normal to be non-configurable?

> +    if( unlikely(psz_name == NULL) )
> +        return VLC_ENOMEM;
> +
> +    /* Parse psz_name syntax (Copied from udp.c):
> +     * [serveraddr[:serverport]][@[bindaddr]:[bindport]] */
> +    psz_parser = strchr( psz_name, '@' );
> +    if( psz_parser != NULL )
> +    {
> +        /* Found bind address and/or bind port */
> +        *psz_parser++ = '\0';
> +        psz_bind_addr = psz_parser;
> +
> +        if( psz_bind_addr[0] == '[' )
> +            /* skips bracket'd IPv6 address */
> +            psz_parser = strchr( psz_parser, ']' );
> +
> +        if( psz_parser != NULL )
> +        {
> +            psz_parser = strchr( psz_parser, ':' );
> +            if( psz_parser != NULL )
> +            {
> +                *psz_parser++ = '\0';
> +                i_bind_port = atoi( psz_parser );
> +            }
> +        }
> +    }
> +
> +    psz_server_addr = psz_name;
> +    psz_parser = ( psz_server_addr[0] == '[' )
> +        ? strchr( psz_name, ']' ) /* skips bracket'd IPv6 address */
> +        : psz_name;
> +
> +    if( psz_parser != NULL )
> +    {
> +        psz_parser = strchr( psz_parser, ':' );
> +        if( psz_parser != NULL )
> +        {
> +            *psz_parser++ = '\0';
> +            i_server_port = atoi( psz_parser );
> +        }
> +    }
> +
> +    msg_Dbg( p_stream, "opening server=%s:%d local=%s:%d",
> +             psz_server_addr, i_server_port, psz_bind_addr, i_bind_port
> );
> +
> +    struct sockaddr_in sa;
> +    memset( &sa, 0, sizeof sa );
> +    sa.sin_family = AF_INET;
> +    sa.sin_port = htons( i_server_port );
> +
> +    if ( !inet_pton( sa.sin_family, psz_server_addr, &sa.sin_addr ) )
> +    {
> +        msg_Err( p_stream, "Failed to convert server address(%s).",
> psz_server_addr );
> +        srt_close ( p_sys->sock );
> +        return VLC_EGENERIC;
> +    }
> +
> +    free( psz_name );
> +
> +    int stat;
> +    struct sockaddr *p_sa = (struct sockaddr*)&sa;
> +    stat = srt_connect( p_sys->sock, p_sa, sizeof sa);
> +
> +    if ( stat == SRT_ERROR )
> +    {
> +        msg_Err( p_stream, "Failed to connect to server." );
> +        srt_close ( p_sys->sock );
> +        return VLC_EGENERIC;
> +    } 
> +
> +    return VLC_SUCCESS;
> +}
> +
> +static void Close(vlc_object_t *p_this)
> +{
> +    stream_t     *p_stream = (stream_t*)p_this;
> +    stream_sys_t *p_sys = p_stream->p_sys;
> +
> +    msg_Dbg( p_stream, "closing server" );
> +    srt_close ( p_sys->sock );
> +}
> +
> +static int Control(stream_t *p_stream, int i_query, va_list args)
> +{
> +    VLC_UNUSED (p_stream);
> +    bool    *pb_bool;
> +
> +    switch( i_query )
> +    {
> +        case STREAM_CAN_SEEK:
> +        case STREAM_CAN_FASTSEEK:
> +        case STREAM_CAN_PAUSE:
> +        case STREAM_CAN_CONTROL_PACE:
> +            pb_bool = va_arg( args, bool * );
> +            *pb_bool = false;
> +            break;
> +        default:
> +            return VLC_EGENERIC;
> +    }
> +    return VLC_SUCCESS;
> +}
> +
> +static block_t *BlockSRT(stream_t *p_stream, bool *restrict eof)
> +{
> +    stream_sys_t *p_sys = p_stream->p_sys;
> +
> +    block_t *pkt = block_Alloc( p_sys->i_chunk_size );
> +
> +    if (unlikely(pkt == NULL))
> +    {   
> +        return NULL;
> +    }
> +
> +    SRTSOCKET ready[2];
> +    if ( srt_epoll_wait( p_sys->i_poll_id, 0, 0, ready, &(int) { 2 },
> -1, 0, 0, 0, 0 ) == -1 )
> +    {
> +        msg_Err( p_stream, "%s", srt_getlasterror_str() );
> +        goto skip;
> +    }
> +
> +    int stat = srt_recvmsg( p_sys->sock, (char *)pkt->p_buffer,
> p_sys->i_chunk_size );
> +
> +    if ( stat == SRT_ERROR )
> +    {
> +        msg_Err( p_stream, "%s", srt_getlasterror_str() );
> +        goto skip;
> +    }
> +
> +    pkt->i_buffer = stat;
> +    return pkt;
> +
> +skip:
> +   *eof = true;
> +    block_Release(pkt);
> +    return NULL;
> +}
> diff --git a/modules/access_output/Makefile.am
> b/modules/access_output/Makefile.am
> index 7c9311646c..7eddb58814 100644
> --- a/modules/access_output/Makefile.am
> +++ b/modules/access_output/Makefile.am
> @@ -26,3 +26,10 @@ libaccess_output_shout_plugin_la_LDFLAGS =
> $(AM_LDFLAGS) -rpath '$(access_outdir
>  libaccess_output_shout_plugin_la_LIBADD = $(SHOUT_LIBS) $(SOCKET_LIBS)
>  access_out_LTLIBRARIES += $(LTLIBaccess_output_shout)
>  EXTRA_LTLIBRARIES += libaccess_output_shout_plugin.la
> +
> +### SRT ###
> +libaccess_output_srt_plugin_la_SOURCES = access_output/srt.c
> +libaccess_output_srt_plugin_la_LIBADD = $(SRT_LIBS) $(LIBPTHREAD)
> +if HAVE_SRT
> +access_out_LTLIBRARIES += libaccess_output_srt_plugin.la
> +endif
> diff --git a/modules/access_output/srt.c b/modules/access_output/srt.c
> new file mode 100644
> index 0000000000..9c28a7efeb
> --- /dev/null
> +++ b/modules/access_output/srt.c
> @@ -0,0 +1,428 @@
> +/*****************************************************************************
> + * srt.c: SRT (Secure Reliable Transport) output module
> +
> *****************************************************************************
> + * Copyright (C) 2017, Collabora Ltd.
> + *
> + * Authors: Justin Kim <justin.kim at collabora.com>
> + *
> + * This program is free software; you can redistribute it and/or modify
> it
> + * under the terms of the GNU Lesser General Public License as published
> by
> + * the Free Software Foundation; either version 2.1 of the License, or
> + * (at your option) any later version.
> + *
> + * This program is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
> + * GNU Lesser General Public License for more details.
> + *
> + * You should have received a copy of the GNU Lesser General Public
> License
> + * along with this program; if not, write to the Free Software
> Foundation,
> + * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
> +
> *****************************************************************************/
> +
> +#ifdef HAVE_CONFIG_H
> +# include "config.h"
> +#endif
> +
> +#include <errno.h>
> +#include <vlc_common.h>
> +#include <vlc_plugin.h>
> +#include <vlc_sout.h>
> +#include <vlc_block.h>
> +#include <vlc_interface.h>
> +
> +#include <sys/socket.h>
> +#include <netinet/in.h>
> +
> +#include <srt/srt.h>
> +
> +#define MAX_EMPTY_BLOCKS 200
> +#define SRT_DEFAULT_CHUNK_SIZE 1316
> +#define SRT_DEFAULT_PORT 9000

Same remarks as above.

> +static int  Open ( vlc_object_t * );
> +static void Close( vlc_object_t * );
> +
> +#define SOUT_CFG_PREFIX "sout-srt-"
> +
> +#define CACHING_TEXT N_("Caching value (ms)")
> +#define CACHING_LONGTEXT N_( \
> +    "Default caching value for outbound SRT streams. This " \
> +    "value should be set in milliseconds." )
> +
> +#define GROUP_TEXT N_("Group packets")
> +#define GROUP_LONGTEXT N_("Packets can be sent one by one at the right
> time " \
> +                          "or by groups. You can choose the number " \
> +                          "of packets that will be sent at a time. It "
> \
> +                          "helps reducing the scheduling load on " \
> +                          "heavily-loaded systems." )
> +
> +/* Module descriptor */
> +vlc_module_begin()
> +    set_shortname(N_("SRT"))
> +    set_description(N_("SRT stream output"))
> +    set_category( CAT_SOUT )
> +    set_subcategory( SUBCAT_SOUT_ACO )
> +
> +    add_integer( SOUT_CFG_PREFIX "caching", DEFAULT_PTS_DELAY / 1000,
> CACHING_TEXT, CACHING_LONGTEXT, true )
> +    add_integer( SOUT_CFG_PREFIX "group", 1, GROUP_TEXT, GROUP_LONGTEXT,
> +                                 true )
> +
> +    set_capability("sout access", 0)
> +    add_shortcut( "srt" )
> +
> +    set_callbacks(Open, Close)
> +vlc_module_end ()
> +
> +struct sout_access_out_sys_t
> +{
> +    SRTSOCKET     sock;
> +    int           i_poll_id;
> +    int           i_chunk_size;
> +
> +    mtime_t       i_caching;
> +    block_fifo_t *p_fifo;
> +    block_fifo_t *p_empty_blocks;
> +    block_t      *p_buffer;
> +
> +    vlc_thread_t  thread;
> +};
> +
> +static const char *const ppsz_sout_options[] = {
> +    "caching",
> +    NULL
> +};
> +
> +static ssize_t Write            ( sout_access_out_t *, block_t * );
> +static int Control              ( sout_access_out_t *, int, va_list );
> +
> +static void* ThreadWrite        ( void * );
> +static block_t *NewSRTPacket    ( sout_access_out_t *, mtime_t );
> +
> +static int Open( vlc_object_t *p_this )
> +{
> +    sout_access_out_t       *p_access = (sout_access_out_t*)p_this;
> +    sout_access_out_sys_t   *p_sys;
> +
> +    char                *psz_dst_addr = NULL;
> +    int                 i_dst_port;
> +
> +    config_ChainParse( p_access, SOUT_CFG_PREFIX,
> +                       ppsz_sout_options, p_access->p_cfg );
> +
> +    if (var_Create (p_access, "dst-port", VLC_VAR_INTEGER)
> +     || var_Create (p_access, "src-port", VLC_VAR_INTEGER)
> +     || var_Create (p_access, "dst-addr", VLC_VAR_STRING)
> +     || var_Create (p_access, "src-addr", VLC_VAR_STRING))
> +    {
> +         msg_Err( p_access, "Valid network information is required." );
> +        return VLC_ENOMEM;
> +    }
> +
> +    if( !( p_sys = malloc ( sizeof( *p_sys ) ) ) )
> +        return VLC_ENOMEM;
> +    p_access->p_sys = p_sys;
> +
> +    p_sys->sock = srt_socket(AF_INET, SOCK_DGRAM, 0);
> +    if ( p_sys->sock == SRT_ERROR )
> +    {
> +        msg_Err( p_access, "Failed to open socket." );
> +        return VLC_EGENERIC;
> +    }
> +
> +    p_sys->i_poll_id = srt_epoll_create();
> +    if ( p_sys->i_poll_id == -1 )
> +    {
> +        msg_Err( p_access, "Failed to create poll id for SRT socket." );
> +        srt_close( p_sys->sock );
> +        return VLC_EGENERIC;
> +    }
> +
> +    srt_epoll_add_usock( p_sys->i_poll_id, p_sys->sock, &(int) {
> SRT_EPOLL_OUT });
> +
> +    i_dst_port = SRT_DEFAULT_PORT;
> +    char *psz_parser = psz_dst_addr = strdup( p_access->psz_path );
> +    if( !psz_dst_addr )
> +    {
> +        free( p_sys );
> +        return VLC_ENOMEM;
> +    }
> +
> +    if (psz_parser[0] == '[')
> +        psz_parser = strchr (psz_parser, ']');
> +
> +    psz_parser = strchr (psz_parser ? psz_parser : psz_dst_addr, ':');
> +    if (psz_parser != NULL)
> +    {
> +        *psz_parser++ = '\0';
> +        i_dst_port = atoi (psz_parser);
> +    }
> +
> +    msg_Dbg( p_access, "Setting SRT socket (dest addresss: %s, port:
> %d).",
> +             psz_dst_addr, i_dst_port );
> +
> +    struct sockaddr_in sa;
> +    memset( &sa, 0, sizeof sa );
> +    sa.sin_family = AF_INET;
> +    sa.sin_port = htons( i_dst_port );
> +
> +    int stat;
> +    struct sockaddr *p_sa = (struct sockaddr*)&sa;
> +
> +    if ( !inet_pton( sa.sin_family, psz_dst_addr, &sa.sin_addr ) )
> +    {
> +        msg_Err( p_access, "Failed to convert destination address(%s).",
> psz_dst_addr );
> +        free( psz_dst_addr );
> +        srt_close( p_sys->sock );
> +        return VLC_EGENERIC;
> +    }
> +
> +    free( psz_dst_addr );
> +
> +    srt_setsockopt( p_sys->sock, 0, SRTO_SENDER, &(int) { 1 },
> sizeof(int) );
> +
> +    stat = srt_connect( p_sys->sock, p_sa, sizeof sa);
> +
> +    if ( stat == SRT_ERROR )
> +    {
> +        msg_Err( p_access, "Failed to connect to server." );
> +        srt_close( p_sys->sock );
> +        return VLC_EGENERIC;
> +    } 
> +
> +    p_sys->i_caching = UINT64_C(1000)
> +                     * var_GetInteger( p_access, SOUT_CFG_PREFIX
> "caching");
> +    p_sys->i_chunk_size = SRT_DEFAULT_CHUNK_SIZE;
> +    p_sys->p_fifo = block_FifoNew();
> +    p_sys->p_empty_blocks = block_FifoNew();
> +    p_sys->p_buffer = NULL;
> + 
> +    if( vlc_clone( &p_sys->thread, ThreadWrite, p_access,
> +                           VLC_THREAD_PRIORITY_HIGHEST ) )
> +    {
> +        msg_Err( p_access, "cannot spawn sout access thread" );
> +        block_FifoRelease( p_sys->p_fifo );
> +        block_FifoRelease( p_sys->p_empty_blocks );
> +        srt_close( p_sys->sock );
> +        free( p_sys );
> +        return VLC_EGENERIC;
> +    }
> +
> +    p_access->pf_write = Write;
> +    p_access->pf_control = Control;
> +
> +    return VLC_SUCCESS;
> +}
> +
> +static void Close( vlc_object_t * p_this )
> +{
> +    sout_access_out_t     *p_access = (sout_access_out_t*)p_this;
> +    sout_access_out_sys_t *p_sys = p_access->p_sys;
> +
> +    vlc_cancel( p_sys->thread );
> +    vlc_join( p_sys->thread, NULL );
> +    block_FifoRelease( p_sys->p_fifo );
> +    block_FifoRelease( p_sys->p_empty_blocks );
> +
> +    if( p_sys->p_buffer ) block_Release( p_sys->p_buffer );
> +
> +    srt_close( p_sys->sock );
> +
> +    free( p_sys );
> +}
> +
> +static int Control( sout_access_out_t *p_access, int i_query, va_list
> args )
> +{
> +    (void)p_access;
> +
> +    switch( i_query )
> +    {
> +        case ACCESS_OUT_CONTROLS_PACE:
> +            *va_arg( args, bool * ) = false;
> +            break;
> +
> +        default:
> +            return VLC_EGENERIC;
> +    }
> +    return VLC_SUCCESS;
> +}
> +
> +static block_t *NewSRTPacket( sout_access_out_t *p_access, mtime_t
> i_dts)
> +{
> +    sout_access_out_sys_t *p_sys = p_access->p_sys;
> +    block_t *p_buffer;
> +
> +    while ( vlc_fifo_GetCount( p_sys->p_empty_blocks ) >
> MAX_EMPTY_BLOCKS )
> +    {
> +        p_buffer = block_FifoGet( p_sys->p_empty_blocks );
> +        block_Release( p_buffer );
> +    }
> +
> +    if( vlc_fifo_GetCount( p_sys->p_empty_blocks ) == 0 )
> +    {
> +        p_buffer = block_Alloc( p_sys->i_chunk_size );
> +    }
> +    else
> +    {
> +        p_buffer = block_FifoGet( p_sys->p_empty_blocks );
> +        p_buffer->i_flags = 0;
> +        p_buffer = block_Realloc( p_buffer, 0, p_sys->i_chunk_size );
> +    }
> +
> +    p_buffer->i_dts = i_dts;
> +    p_buffer->i_buffer = 0;
> +
> +    return p_buffer;
> +}
> +
> +static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer )
> +{
> +    sout_access_out_sys_t *p_sys = p_access->p_sys;
> +    int i_len = 0;
> +
> +    /* The below codes are copied from udp.c */
> +    while( p_buffer )
> +    {
> +        block_t *p_next;
> +        int i_packets = 0;
> +        mtime_t now = mdate();
> +
> +        /* Check if there is enough space in the buffer */
> +        if( p_sys->p_buffer &&
> +            p_sys->p_buffer->i_buffer + p_buffer->i_buffer >
> (size_t)p_sys->i_chunk_size )
> +        {
> +            if( p_sys->p_buffer->i_dts + p_sys->i_caching < now )
> +            {
> +                msg_Dbg( p_access, "late packet for SRT input (%"PRId64
> ")",
> +                         now - p_sys->p_buffer->i_dts
> +                          - p_sys->i_caching );
> +            }
> +            block_FifoPut( p_sys->p_fifo, p_sys->p_buffer );
> +            p_sys->p_buffer = NULL;
> +        }
> +
> +        i_len += p_buffer->i_buffer;
> +
> +        while( p_buffer->i_buffer )
> +        {
> +            size_t i_payload_size = p_sys->i_chunk_size;
> +            size_t i_write = __MIN( p_buffer->i_buffer, i_payload_size
> );
> +
> +            i_packets++;
> +
> +            if( !p_sys->p_buffer )
> +            {
> +                p_sys->p_buffer = NewSRTPacket( p_access,
> p_buffer->i_dts );
> +                if( !p_sys->p_buffer ) break;
> +            }
> +
> +            memcpy( p_sys->p_buffer->p_buffer +
> p_sys->p_buffer->i_buffer,
> +                    p_buffer->p_buffer, i_write );
> +
> +            p_sys->p_buffer->i_buffer += i_write;
> +            p_buffer->p_buffer += i_write;
> +            p_buffer->i_buffer -= i_write;
> +            if ( p_buffer->i_flags & BLOCK_FLAG_CLOCK )
> +            {
> +                if ( p_sys->p_buffer->i_flags & BLOCK_FLAG_CLOCK )
> +                    msg_Warn( p_access, "putting two PCRs at once" );
> +                p_sys->p_buffer->i_flags |= BLOCK_FLAG_CLOCK;
> +            }
> +
> +            if( p_sys->p_buffer->i_buffer == (size_t)p_sys->i_chunk_size
> || i_packets > 1 )
> +            {
> +                /* Flush */
> +                if( p_sys->p_buffer->i_dts + p_sys->i_caching < now )
> +                {
> +                    msg_Dbg( p_access, "late packet for SRT input
> (%"PRId64 ")",
> +                             mdate() - p_sys->p_buffer->i_dts
> +                              - p_sys->i_caching );
> +                }
> +                block_FifoPut( p_sys->p_fifo, p_sys->p_buffer );
> +                p_sys->p_buffer = NULL;
> +            }
> +        }
> +
> +        p_next = p_buffer->p_next;
> +        block_Release( p_buffer );
> +        p_buffer = p_next;
> +    }
> +
> +    return i_len;
> +}
> +
> +static void* ThreadWrite( void *data )
> +{
> +    sout_access_out_t *p_access = data;
> +    sout_access_out_sys_t *p_sys = p_access->p_sys;
> +    mtime_t i_date_last = -1;
> +    const unsigned i_group = var_GetInteger( p_access,
> +                                             SOUT_CFG_PREFIX "group" );
> +    mtime_t i_to_send = i_group;
> +    unsigned i_dropped_packets = 0;
> +
> +    for (;;)
> +    {
> +        block_t *p_pk = block_FifoGet( p_sys->p_fifo );
> +        mtime_t       i_date, i_sent;
> +
> +        i_date = p_sys->i_caching + p_pk->i_dts;
> +        if( i_date_last > 0 )
> +        {
> +            if( i_date - i_date_last > 2000000 )
> +            {
> +                if( !i_dropped_packets )
> +                    msg_Dbg( p_access, "mmh, hole (%"PRId64" > 2s) ->
> drop",
> +                             i_date - i_date_last );
> +
> +                block_FifoPut( p_sys->p_empty_blocks, p_pk );
> +
> +                i_date_last = i_date;
> +                i_dropped_packets++;
> +                continue;
> +            }
> +            else if( i_date - i_date_last < -1000 )
> +            {
> +                if( !i_dropped_packets )
> +                    msg_Dbg( p_access, "mmh, packets in the past
> (%"PRId64")",
> +                             i_date_last - i_date );
> +            }
> +        }
> +
> +        block_cleanup_push( p_pk );
> +        i_to_send--;
> +        if( !i_to_send || (p_pk->i_flags & BLOCK_FLAG_CLOCK) )
> +        {
> +            mwait( i_date );
> +            i_to_send = i_group;
> +        }
> +
> +        SRTSOCKET ready[2];
> +        if ( srt_epoll_wait( p_sys->i_poll_id, 0, 0, ready, &(int){ 2 },
> -1, 0, 0, 0, 0 ) == -1 )
> +            msg_Err( p_access, "%s", srt_getlasterror_str() );
> +
> +        if ( srt_sendmsg2( p_sys->sock, (char *)p_pk->p_buffer,
> p_pk->i_buffer, 0 ) == SRT_ERROR )
> +            msg_Warn( p_access, "send error: %s", srt_getlasterror_str()
> );
> +        
> +        vlc_cleanup_pop();
> +
> +        if( i_dropped_packets )
> +        {
> +            msg_Dbg( p_access, "dropped %i packets", i_dropped_packets
> );
> +            i_dropped_packets = 0;
> +        }
> +
> +        i_sent = mdate();
> +        if ( i_sent > i_date + 20000 )
> +        {
> +            msg_Dbg( p_access, "packet has been sent too late (%"PRId64
> ")",
> +                     i_sent - i_date );
> +        }
> +
> +        block_FifoPut( p_sys->p_empty_blocks, p_pk );
> +
> +        i_date_last = i_date;
> +    }
> +    return NULL;
> +
> +}


That seems of quite good quality, tbh, for a first patch :)

-- 
Jean-Baptiste Kempf -  President
+33 672 704 734


More information about the vlc-devel mailing list