[vlc-devel] [PATCH v6 1/3] access: srt: introduce srt input module
Rémi Denis-Courmont
remi at remlab.net
Mon Sep 25 20:54:00 CEST 2017
Le torstaina 14. syyskuuta 2017, 18.43.38 EEST Justin Kim a écrit :
> 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 | 312
> +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 328
> insertions(+)
> create mode 100644 modules/access/srt.c
>
> diff --git a/NEWS b/NEWS
> index cab32bfd5a..31c61baab1 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 5b7437b8df..0014629c92 100644
> --- a/configure.ac
> +++ b/configure.ac
> @@ -3881,6 +3881,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 28dac0289f..0e117115a7 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..ca38d3d6d6
> --- /dev/null
> +++ b/modules/access/srt.c
> @@ -0,0 +1,312 @@
> +/**************************************************************************
> *** + * 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 <sys/eventfd.h>
> +#include <sys/epoll.h>
> +
> +#include <vlc_common.h>
> +#include <vlc_interrupt.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 -1 (infinite) */
> +#define SRT_DEFAULT_POLL_TIMEOUT -1
> +
> +struct stream_sys_t
> +{
> + SRTSOCKET sock;
> + int i_poll_id;
> + int i_poll_timeout;
> + size_t i_chunk_size;
> + int i_event_fd;
> + vlc_mutex_t lock;
> +};
> +
> +static void srt_wait_interrupted(void *p_data)
> +{
> + stream_t *p_stream = p_data;
> + stream_sys_t *p_sys = p_stream->p_sys;
> + vlc_mutex_lock( &p_sys->lock );
> + msg_Dbg( p_stream, "Waking up srt_epoll_wait");
> + if ( write( p_sys->i_event_fd, &( bool ) { true }, sizeof( bool ) ) < 0
> ) + {
> + msg_Err( p_stream, "Failed to send data to event fd");
> + }
> + vlc_mutex_unlock( &p_sys->lock );
Not sure what the lock is about, TBH. AFAIK, reading or writing a Linux event
FDs is atomic.
> +}
> +
> +static int Control(stream_t *p_stream, int i_query, va_list args)
> +{
> + stream_sys_t *p_sys = p_stream->p_sys;
> + int i_ret = VLC_SUCCESS;
> +
> + vlc_mutex_lock( &p_sys->lock );
> +
> + 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:
> + i_ret = VLC_EGENERIC;
> + break;
> + }
> +
> + vlc_mutex_unlock( &p_sys->lock );
I don´t exactly see what the lock protects here either.
> +
> + return i_ret;
> +}
> +
> +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;
> + }
> +
> + vlc_interrupt_register( srt_wait_interrupted, p_stream);
> + vlc_mutex_lock( &p_sys->lock );
> +
> + SRTSOCKET ready[2];
> + struct epoll_event event[1] = { 0 };
> +
> + if ( srt_epoll_wait( p_sys->i_poll_id,
> + ready, &(int) { 2 }, 0, 0, p_sys->i_poll_timeout,
> + &(int) { p_sys->i_event_fd }, &(int) { 1 }, 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;
> + }
> +
> + if ( event[0].events & EPOLLIN ) {
> + bool cancel = 0;
> + int ret = read( event[0].data.fd, &cancel, sizeof( bool ) );
> + if ( ret < 0 )
> + {
> + goto skip;
> + }
> +
> + if ( cancel )
> + {
> + msg_Dbg( p_stream, "Cancelled running" );
> + 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;
> + vlc_mutex_unlock( &p_sys->lock );
> + vlc_interrupt_unregister();
> + return pkt;
> +
> +endofstream:
> + msg_Dbg( p_stream, "EOS");
> + *eof = true;
> +skip:
> + block_Release(pkt);
> + srt_clearlasterror();
> + vlc_mutex_unlock( &p_sys->lock );
> + vlc_interrupt_unregister();
> +
> + 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_sys->i_event_fd = -1;
> + p_stream->p_sys = p_sys;
> + p_stream->pf_block = BlockSRT;
> + p_stream->pf_control = Control;
> +
> + vlc_mutex_init( &p_sys->lock );
> +
> + 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 } ); +
> + p_sys->i_event_fd = eventfd( 0, EFD_NONBLOCK );
All file descriptors in VLC must be created atomically with close-on-exec
flag.
> + srt_epoll_add_ssock( p_sys->i_poll_id, p_sys->i_event_fd, &(int) {
> EPOLLIN } ); +
> + 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_event_fd != -1 )
> + {
> + close( p_sys->i_event_fd );
> + p_sys->i_event_fd = -1;
> + }
> +
> + 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;
> +
> + if ( p_sys->i_poll_id != -1 )
> + {
> + srt_epoll_release( p_sys->i_poll_id );
> + p_sys->i_poll_id = -1;
> + }
> + msg_Dbg( p_stream, "closing server" );
> + srt_close( p_sys->sock );
> +
> + if ( p_sys->i_event_fd != -1 )
> + {
> + close( p_sys->i_event_fd );
> + p_sys->i_event_fd = -1;
> + }
> +
> + vlc_mutex_destroy( &p_sys->lock );
> +}
> +
> +/* 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 ()
--
雷米‧德尼-库尔蒙
https://www.remlab.net/
More information about the vlc-devel
mailing list