[vlc-devel] [PATCH v5 1/3] access: srt: introduce srt input module
Justin Kim
justin.kim at collabora.com
Fri Aug 25 09:17:59 CEST 2017
Secure Reliable Transport (SRT) is a proprietary transport technology
that optimizes streaming performance across unpredictable networks.
This patch is an imlementation of input module which works as a SRT
client based on SRT Library[0].
[0] https://github.com/Haivision/srt/releases
Signed-off-by: Justin Kim <justin.kim at collabora.com>
---
NEWS | 1 +
configure.ac | 5 +
modules/MODULES_LIST | 1 +
modules/access/Makefile.am | 9 ++
modules/access/srt.c | 236 +++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 252 insertions(+)
create mode 100644 modules/access/srt.c
diff --git a/NEWS b/NEWS
index 9eece2102a..9916f697ee 100644
--- a/NEWS
+++ b/NEWS
@@ -60,6 +60,7 @@ Access:
* Improvements on DVB scanning
* BluRay module can open ISO over network and has full BD-J support
* Support for DVD ISO over network
+ * 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..6e7af04367 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], [srt >= 1.2.0], [SRT input 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..2e712c84af 100644
--- a/modules/MODULES_LIST
+++ b/modules/MODULES_LIST
@@ -17,6 +17,7 @@ $Id$
* 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..e11d11a02e
--- /dev/null
+++ b/modules/access/srt.c
@@ -0,0 +1,236 @@
+/*****************************************************************************
+ * 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
+/* The default timeout is randomly chosen to avoid
+ * waiting infinately. */
+#define SRT_DEFAULT_POLL_TIMEOUT 100
+
+struct stream_sys_t
+{
+ SRTSOCKET sock;
+ int i_poll_id;
+ int i_poll_timeout;
+ 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, ready, &(int) { 2 }, 0, 0, p_sys->i_poll_timeout, 0, 0, 0, 0 ) == -1 )
+ {
+ int srt_err = srt_getlasterror( NULL );
+
+ /* Assuming that timeout error is normal when SRT socket is connected. */
+ if ( srt_err == SRT_ETIMEOUT && srt_getsockstate( p_sys->sock ) == SRTS_CONNECTED )
+ {
+ goto skip;
+ }
+
+ msg_Err( p_stream, "released poll wait (reason : %s)", srt_getlasterror_str() );
+ goto endofstream;
+ }
+
+ int stat = srt_recvmsg( p_sys->sock, (char *)pkt->p_buffer, p_sys->i_chunk_size );
+
+ if ( stat == SRT_ERROR )
+ {
+ msg_Err( p_stream, "failed to recevie SRT packet (reason: %s)", srt_getlasterror_str() );
+ goto endofstream;
+ }
+
+ pkt->i_buffer = stat;
+ return pkt;
+
+endofstream:
+ *eof = true;
+skip:
+ block_Release(pkt);
+ srt_clearlasterror();
+
+ 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;
+
+ p_sys = vlc_malloc( p_this, sizeof( *p_sys ) );
+ if( unlikely( p_sys == NULL ) )
+ return VLC_ENOMEM;
+
+ 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 );
+ goto failed;
+ }
+
+ p_sys->i_chunk_size = var_InheritInteger( p_stream, "chunk-size" );
+ p_sys->i_poll_timeout = var_InheritInteger( p_stream, "poll-timeout" );
+ 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." );
+ goto failed;
+ }
+
+ /* Make SRT non-blocking */
+ srt_setsockopt( p_sys->sock, 0, SRTO_SNDSYN, &(bool) { false }, sizeof( bool ) );
+
+ 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_IN } );
+
+ 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:
+
+ if ( parsed_url.psz_host != NULL
+ && parsed_url.psz_buffer != NULL)
+ {
+ 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 )
+ add_integer( "poll-timeout", SRT_DEFAULT_POLL_TIMEOUT, N_("Return poll wait after timeout miliseconds (-1 = infinite)"), NULL, true )
+
+ set_capability( "access", 0 )
+ add_shortcut( "srt" )
+
+ set_callbacks( Open, Close )
+vlc_module_end ()
--
2.14.1
More information about the vlc-devel
mailing list