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

Justin Kim justin.kim at collabora.com
Wed Aug 9 20:13:54 CEST 2017


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                      |   6 +
 modules/MODULES_LIST              |   2 +
 modules/access/Makefile.am        |   8 +
 modules/access/srt.c              | 216 +++++++++++++++++++
 modules/access_output/Makefile.am |   7 +
 modules/access_output/srt.c       | 437 ++++++++++++++++++++++++++++++++++++++
 7 files changed, 677 insertions(+)
 create mode 100644 modules/access/srt.c
 create mode 100644 modules/access_output/srt.c

diff --git a/NEWS b/NEWS
index ef31c19a83..369b7e6c51 100644
--- a/NEWS
+++ b/NEWS
@@ -29,6 +29,7 @@ Core:
    es category single only or multiple es behavior
 
 Access:
+ * New SRT access module using libsrt
  * New NFS access module using libnfs
  * New SMB access module using libdsm
  * Rewrite MPEG-DASH (Dynamic Adaptive Streaming over HTTP) support, including
diff --git a/configure.ac b/configure.ac
index 8487fea895..920acb3dbc 100644
--- a/configure.ac
+++ b/configure.ac
@@ -3859,6 +3859,12 @@ AS_IF([test "${enable_lirc}" = "yes"], [
 ])
 AM_CONDITIONAL([HAVE_LIRC], [test "${have_lirc}" = "yes"])
 
+dnl
+dnl  SRT plugin
+dnl
+PKG_ENABLE_MODULES_VLC([SRT], [], [srt >= 1.2.0], [srt input/output plugin], [auto])
+AM_CONDITIONAL([HAVE_SRT], [test "${enable_srt}" = "yes"])
+
 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..98d3e968a3 100644
--- a/modules/MODULES_LIST
+++ b/modules/MODULES_LIST
@@ -14,6 +14,7 @@ $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
@@ -363,6 +364,7 @@ $Id$
  * speex: a speex audio decoder/packetizer using the libspeex library
  * speex_resampler: audio resampler using the libspeexdsp library
  * spudec: RLE DVD subtitles decoder
+ * srt: SRT(Secure Reliable Transport) input
  * stats: Stats encoder function
  * stereo_widen: Enhances stereo effect
  * stl: EBU STL decoder
diff --git a/modules/access/Makefile.am b/modules/access/Makefile.am
index dd485a2767..97d6a617aa 100644
--- a/modules/access/Makefile.am
+++ b/modules/access/Makefile.am
@@ -417,3 +417,11 @@ 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 ###
+
+libsrt_plugin_la_SOURCES = access/srt.c
+libsrt_plugin_la_LIBADD = $(SRT_LIBS)
+if HAVE_SRT
+access_LTLIBRARIES += libsrt_plugin.la
+endif
diff --git a/modules/access/srt.c b/modules/access/srt.c
new file mode 100644
index 0000000000..fb9118c720
--- /dev/null
+++ b/modules/access/srt.c
@@ -0,0 +1,216 @@
+/*****************************************************************************
+ * 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 <sys/socket.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;
+    vlc_url_t     parsed_url = { 0 };
+    struct addrinfo hints = {
+        .ai_socktype = SOCK_DGRAM,
+    }, *res = NULL;
+    int stat;
+
+    p_sys = vlc_malloc( p_this, sizeof( *p_sys ) );
+    if( unlikely( p_sys == NULL ) )
+        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;
+
+    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;
+    }
+
+    msg_Dbg( p_stream, "Parsed URL (scheme: %s, host:port=%s:%u)",
+             parsed_url.psz_protocol,
+             parsed_url.psz_host,
+             parsed_url.i_port );
+
+    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 );
+    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..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..22fb45a05d
--- /dev/null
+++ b/modules/access_output/srt.c
@@ -0,0 +1,437 @@
+/*****************************************************************************
+ * 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. You can choose the number " \
+                          "of packets that will be sent at a time. It " \
+                          "helps reducing the scheduling load on " \
+                          "heavily-loaded systems." )
+
+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;
+    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;
+        }
+
+        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;
+}
+
+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 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 ()
-- 
2.14.0



More information about the vlc-devel mailing list