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

Rémi Denis-Courmont remi at remlab.net
Tue Aug 15 16:39:44 CEST 2017


Le perjantaina 11. elokuuta 2017, 1.12.26 EEST Justin Kim a écrit :
> 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>
> ---
>  NEWS                              |   1 +
>  configure.ac                      |   5 +
>  modules/MODULES_LIST              |   2 +
>  modules/access/Makefile.am        |   9 +
>  modules/access/srt.c              | 215 +++++++++++++++++++
>  modules/access_output/Makefile.am |   8 +
>  modules/access_output/srt.c       | 434
> ++++++++++++++++++++++++++++++++++++++ 7 files changed, 674 insertions(+)
>  create mode 100644 modules/access/srt.c
>  create mode 100644 modules/access_output/srt.c

I´d split in/out in separate patches...

> 
> diff --git a/NEWS b/NEWS
> index ef31c19a83..c88261802e 100644
> --- a/NEWS
> +++ b/NEWS
> @@ -56,6 +56,7 @@ Access:
>   * New SAT>IP access module, to receive DVB-S via IP networks
>   * Improvements on DVB scanning
>   * BluRay module can open ISO over network and has full BD-J support
> + * New SRT access module using libsrt
> 
>  Decoder:
>   * OMX GPU-zerocopy support for decoding and display on Android using
> OpenMax IL diff --git a/configure.ac b/configure.ac
> index 8487fea895..0c3981e8cc 100644
> --- a/configure.ac
> +++ b/configure.ac
> @@ -3859,6 +3859,11 @@ AS_IF([test "${enable_lirc}" = "yes"], [
>  ])
>  AM_CONDITIONAL([HAVE_LIRC], [test "${have_lirc}" = "yes"])
> 
> +dnl
> +dnl  SRT plugin
> +dnl
> +PKG_ENABLE_MODULES_VLC([SRT], [access_srt access_output_srt], [srt >=
> 1.2.0], [SRT input/output plugin], [auto]) +
>  EXTEND_HELP_STRING([Visualisations and Video filter plugins:])
>  dnl
>  dnl  goom visualization plugin
> diff --git a/modules/MODULES_LIST b/modules/MODULES_LIST
> index 9509eba71d..8bf1a8ea00 100644
> --- a/modules/MODULES_LIST
> +++ b/modules/MODULES_LIST
> @@ -14,9 +14,11 @@ $Id$
>   * access_output_http: HTTP Network access module
>   * access_output_livehttp: Live HTTP stream output
>   * access_output_shout: Shoutcast access output
> + * access_output_srt: SRT (Secure Reliable Transport) access_output module
>   * access_output_udp: UDP Network access_output module
>   * access_qtsound: Quicktime Audio Capture
>   * access_realrtsp: Real RTSP access
> + * access_srt: SRT(Secure Reliable Transport) access module
>   * access_wasapi: WASAPI audio input
>   * accesstweaks: access control tweaking module (dev tool)
>   * adaptive: Unified adaptive streaming module (DASH/HLS)
> diff --git a/modules/access/Makefile.am b/modules/access/Makefile.am
> index dd485a2767..51ac1157c5 100644
> --- a/modules/access/Makefile.am
> +++ b/modules/access/Makefile.am
> @@ -417,3 +417,12 @@ libaccess_mtp_plugin_la_LIBADD = $(MTP_LIBS)
>  libaccess_mtp_plugin_la_LDFLAGS = $(AM_LDFLAGS) -rpath '$(accessdir)'
>  access_LTLIBRARIES += $(LTLIBaccess_mtp)
>  EXTRA_LTLIBRARIES += libaccess_mtp_plugin.la
> +
> +### SRT ###
> +
> +libaccess_srt_plugin_la_SOURCES = access/srt.c
> +libaccess_srt_plugin_la_CPPFLAGS = $(AM_CPPFLAGS) $(SRT_CPPFLAGS)
> +libaccess_srt_plugin_la_LIBADD = $(SRT_LIBS)
> +libaccess_srt_plugin_la_LDFLAGS = $(AM_LDFLAGS) -rpath '$(accessdir)'
> +access_LTLIBRARIES += $(LTLIBaccess_srt)
> +EXTRA_LTLIBRARIES += libaccess_srt_plugin.la
> diff --git a/modules/access/srt.c b/modules/access/srt.c
> new file mode 100644
> index 0000000000..9c0caf615b
> --- /dev/null
> +++ b/modules/access/srt.c
> @@ -0,0 +1,215 @@
> +/**************************************************************************
> *** + * 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_network.h>
> +#include <vlc_url.h>
> +
> +#include <srt/srt.h>
> +
> +/* libsrt defines default packet size as 1316 internally
> + * so srt module takes same value. */
> +#define SRT_DEFAULT_CHUNK_SIZE 1316
> +
> +struct stream_sys_t
> +{
> +    SRTSOCKET sock;
> +    int i_poll_id;
> +    size_t i_chunk_size;
> +};
> +
> +static int Control(stream_t *p_stream, int i_query, va_list args)
> +{
> +    VLC_UNUSED( p_stream );
> +
> +    switch( i_query )
> +    {
> +        case STREAM_CAN_SEEK:
> +        case STREAM_CAN_FASTSEEK:
> +        case STREAM_CAN_PAUSE:
> +        case STREAM_CAN_CONTROL_PACE:
> +            *va_arg( args, bool * ) = false;
> +            break;
> +        case STREAM_GET_PTS_DELAY:
> +            *va_arg( args, int64_t * ) = INT64_C(1000)
> +                   * var_InheritInteger(p_stream, "network-caching");
> +            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;
> +}
> +
> +static int Open(vlc_object_t *p_this)
> +{
> +    stream_t     *p_stream = (stream_t*)p_this;
> +    stream_sys_t *p_sys = NULL;
> +    vlc_url_t     parsed_url = { 0 };
> +    struct addrinfo hints = {
> +        .ai_socktype = SOCK_DGRAM,
> +    }, *res = NULL;
> +    int stat;
> +
> +    if ( vlc_UrlParse( &parsed_url, p_stream->psz_url ) == -1 )
> +    {
> +      msg_Err( p_stream, "Failed to parse a given URL (%s)",
> p_stream->psz_url );
> +      return VLC_EGENERIC;
> +    }
> +
> +    p_sys = vlc_malloc( p_this, sizeof( *p_sys ) );
> +    if( unlikely( p_sys == NULL ) )

Leaking the parsed URL here.

> +        return VLC_ENOMEM;
> +
> +    p_sys->i_chunk_size = var_InheritInteger( p_stream, "chunk-size" );
> +    p_sys->i_poll_id = -1;
> +    p_stream->p_sys = p_sys;
> +    p_stream->pf_block = BlockSRT;
> +    p_stream->pf_control = Control;
> +
> +    stat = vlc_getaddrinfo( parsed_url.psz_host, parsed_url.i_port, &hints,
> &res ); +    if ( stat )
> +    {
> +      msg_Err( p_stream, "Cannot resolve [%s]:%d (reason: %s)",
> +               parsed_url.psz_host,
> +               parsed_url.i_port,
> +               gai_strerror( stat ) );
> +
> +      goto failed;
> +    }
> +
> +    p_sys->sock = srt_socket(res->ai_family, 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." );
> +        goto failed;
> +    }
> +    srt_epoll_add_usock( p_sys->i_poll_id, p_sys->sock, &(int) {
> SRT_EPOLL_OUT } ); +
> +    stat = srt_connect( p_sys->sock, res->ai_addr, sizeof (struct
> sockaddr)); +
> +    if ( stat == SRT_ERROR )
> +    {
> +        msg_Err( p_stream, "Failed to connect to server." );
> +        goto failed;
> +    }
> +
> +    vlc_UrlClean( &parsed_url );
> +    freeaddrinfo( res );
> +
> +    return VLC_SUCCESS;
> +
> +failed:
> +    vlc_UrlClean( &parsed_url );
> +    if ( res != NULL )
> +    {
> +        freeaddrinfo( res );
> +    }
> +
> +    if ( p_sys->i_poll_id != -1 )
> +    {
> +        srt_epoll_release( p_sys->i_poll_id );
> +        p_sys->i_poll_id = -1;
> +    }
> +    srt_close( p_sys->sock );
> +
> +    if ( p_sys != NULL )
> +    {
> +        free( p_sys );
> +        p_stream->p_sys = NULL;
> +    }
> +    return VLC_EGENERIC;
> +}
> +
> +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;
> +
> +    srt_epoll_release( p_sys->i_poll_id );
> +    msg_Dbg( p_stream, "closing server" );
> +    srt_close( p_sys->sock );
> +}
> +
> +/* Module descriptor */
> +vlc_module_begin ()
> +    set_shortname( N_("SRT") )
> +    set_description( N_("SRT input") )
> +    set_category( CAT_INPUT )
> +    set_subcategory( SUBCAT_INPUT_ACCESS )
> +
> +    add_integer( "chunk-size", SRT_DEFAULT_CHUNK_SIZE, N_("SRT chunk size
> (bytes)"), NULL, true ) +
> +    set_capability( "access", 0 )
> +    add_shortcut( "srt" )
> +
> +    set_callbacks( Open, Close )
> +vlc_module_end ()
> diff --git a/modules/access_output/Makefile.am
> b/modules/access_output/Makefile.am index 7c9311646c..bc954ee03b 100644
> --- a/modules/access_output/Makefile.am
> +++ b/modules/access_output/Makefile.am
> @@ -26,3 +26,11 @@ 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_CPPFLAGS = $(AM_CPPFLAGS) $(SRT_CPPFLAGS)
> +libaccess_output_srt_plugin_la_LIBADD = $(SRT_LIBS) $(LIBPTHREAD)
> +libaccess_output_srt_plugin_la_LDFLAGS = $(AM_LDFLAGS) -rpath
> '$(access_outdir)' +access_out_LTLIBRARIES += $(LTLIBaccess_output_srt)
> +EXTRA_LTLIBRARIES += libaccess_output_srt_plugin.la
> diff --git a/modules/access_output/srt.c b/modules/access_output/srt.c
> new file mode 100644
> index 0000000000..def063120f
> --- /dev/null
> +++ b/modules/access_output/srt.c
> @@ -0,0 +1,434 @@
> +/**************************************************************************
> *** + * 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_network.h>
> +
> +#include <srt/srt.h>
> +
> +#define MAX_EMPTY_BLOCKS 200
> +/* libsrt defines default packet size as 1316 internally
> + * so srt module takes same value. */
> +#define SRT_DEFAULT_CHUNK_SIZE 1316
> +/* libsrt tutorial uses 9000 as a default binding port */
> +#define SRT_DEFAULT_PORT 9000
> +
> +#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 to help reducing the
> scheduling load.") +
> +struct sout_access_out_sys_t
> +{
> +    SRTSOCKET     sock;
> +    int           i_poll_id;
> +    size_t        i_chunk_size;
> +
> +    mtime_t       i_caching;
> +    block_fifo_t *p_fifo;

> +    block_fifo_t *p_empty_blocks;

That optimization is very questionable nowadays, and probably totally 
irrelevant to SRT.

> +    block_t      *p_buffer;
> +
> +    vlc_thread_t  thread;
> +};
> +
> +static const char *const ppsz_sout_options[] = {
> +    "caching",
> +    NULL
> +};
> +
> +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;
> +        }

^^^
This whole timing dance is meant to reduce jitter over raw UDP. I don´t know 
how SRT works at lower layers, but it´s probably not relevant or even not 
working.


> +
> +        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();

This assumes that the two SRT functions above are safe for synchronous thread 
cancellation. I doubt that were the case, and even if it is, I doubt that 
would be concious and guaranteed by the SRT developers.

You might not need a thread at all here.

> +
> +        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;
> +}
> +
> +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 )

This function is deprecated and should raise a warning.

> +    {
> +        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;
> +            }
> +        }

Does it make any sense to gather TS packets before passing them onto the SRT 
library? Otherwise, this only adds latency and complexity.


> +
> +        p_next = p_buffer->p_next;
> +        block_Release( p_buffer );
> +        p_buffer = p_next;
> +    }
> +
> +    return i_len;
> +}
> +
> +static int Control( sout_access_out_t *p_access, int i_query, va_list args
> ) +{
> +    VLC_UNUSED( p_access );
> +
> +    switch( i_query )
> +    {
> +        case ACCESS_OUT_CONTROLS_PACE:
> +            *va_arg( args, bool * ) = false;
> +            break;
> +
> +        default:
> +            return VLC_EGENERIC;
> +    }
> +    return VLC_SUCCESS;
> +}
> +
> +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 = NULL;
> +
> +    char                    *psz_dst_addr = NULL;
> +    int                      i_dst_port;
> +    int                      stat;
> +
> +    struct addrinfo hints = {
> +        .ai_socktype = SOCK_DGRAM,
> +    }, *res = NULL;
> +
> +    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_sys->i_caching = UINT64_C(1000)
> +                     * var_GetInteger( p_access, SOUT_CFG_PREFIX
> "caching"); +    p_sys->i_chunk_size = var_InheritInteger( p_access,
> "chunk-size" ); +    p_sys->i_poll_id = -1;
> +
> +    p_access->p_sys = p_sys;
> +
> +    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 );
> +
> +    stat = vlc_getaddrinfo( psz_dst_addr, i_dst_port, &hints, &res );
> +    if ( stat )
> +    {
> +        msg_Err( p_access, "Cannot resolve [%s]:%d (reason: %s)",
> +                 psz_dst_addr,
> +                 i_dst_port,
> +                 gai_strerror( stat ) );
> +
> +        goto failed;
> +    }
> +
> +    p_sys->sock = srt_socket( res->ai_family, SOCK_DGRAM, 0 );
> +    if ( p_sys->sock == SRT_ERROR )
> +    {
> +        msg_Err( p_access, "Failed to open socket." );
> +        goto failed;
> +    }
> +
> +    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
> (reason: %s)", +                 srt_getlasterror_str() );
> +
> +        goto failed;
> +    }
> +
> +    srt_epoll_add_usock( p_sys->i_poll_id, p_sys->sock, &(int) {
> SRT_EPOLL_OUT }); +    srt_setsockopt( p_sys->sock, 0, SRTO_SENDER, &(int)
> { 1 }, sizeof(int) ); +
> +    stat = srt_connect( p_sys->sock, res->ai_addr, sizeof (struct
> sockaddr)); +    if ( stat == SRT_ERROR )
> +    {
> +        msg_Err( p_access, "Failed to connect to server (reason: %s)",
> +                 srt_getlasterror_str() );
> +        goto failed;
> +    }
> +
> +    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 );
> +        goto failed;
> +    }
> +
> +    p_access->pf_write = Write;
> +    p_access->pf_control = Control;
> +
> +    free( psz_dst_addr );
> +    freeaddrinfo( res );
> +
> +    return VLC_SUCCESS;
> +
> +failed:
> +    if ( psz_dst_addr != NULL)
> +        free( psz_dst_addr );
> +
> +    if ( res != NULL )
> +        freeaddrinfo( res );
> +
> +    if ( p_sys != NULL )
> +    {
> +        if ( p_sys->i_poll_id != -1 ) srt_epoll_release( p_sys->i_poll_id
> ); +        if ( p_sys->sock != -1 ) srt_close( p_sys->sock );
> +        free( p_sys );
> +    }
> +
> +    return VLC_EGENERIC;
> +}
> +
> +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_epoll_release( p_sys->i_poll_id );
> +    srt_close( p_sys->sock );
> +
> +    free( p_sys );
> +}
> +
> +/* 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( "chunk-size", SRT_DEFAULT_CHUNK_SIZE, N_("SRT chunk size
> (bytes)"), NULL, true ) +    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 ()


-- 
雷米‧德尼-库尔蒙
https://www.remlab.net/



More information about the vlc-devel mailing list