1 /*****************************************************************************
2 * srt.c: SRT (Secure Reliable Transport) output module
3 *****************************************************************************
4 * Copyright (C) 2017, Collabora Ltd.
6 * Authors: Justin Kim <justin.kim@collabora.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
28 #include <sys/eventfd.h>
29 #include <sys/epoll.h>
31 #include <vlc_common.h>
32 #include <vlc_interrupt.h>
33 #include <vlc_plugin.h>
35 #include <vlc_block.h>
36 #include <vlc_network.h>
40 /* libsrt defines default packet size as 1316 internally
41 * so srt module takes same value. */
42 #define SRT_DEFAULT_CHUNK_SIZE 1316
43 /* libsrt tutorial uses 9000 as a default binding port */
44 #define SRT_DEFAULT_PORT 9000
45 /* The default timeout is -1 (infinite) */
46 #define SRT_DEFAULT_POLL_TIMEOUT 100
47 /* The default latency is 125
48 * which uses srt library internally */
49 #define SRT_DEFAULT_LATENCY 125
51 struct sout_access_out_sys_t
61 static void srt_wait_interrupted(void *p_data)
63 sout_access_out_t *p_access = p_data;
64 sout_access_out_sys_t *p_sys = p_access->p_sys;
65 msg_Dbg( p_access, "Waking up srt_epoll_wait");
66 if ( write( p_sys->i_event_fd, &( bool ) { true }, sizeof( bool ) ) < 0 )
68 msg_Err( p_access, "Failed to send data to event fd");
72 static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer )
74 sout_access_out_sys_t *p_sys = p_access->p_sys;
77 vlc_interrupt_register( srt_wait_interrupted, p_access);
83 i_len += p_buffer->i_buffer;
85 while( p_buffer->i_buffer )
87 size_t i_write = __MIN( p_buffer->i_buffer, p_sys->i_chunk_size );
90 struct epoll_event event[1] = { 0 };
92 if ( srt_epoll_wait( p_sys->i_poll_id,
93 0, 0, ready, &(int){ 2 }, p_sys->i_poll_timeout,
94 &(int) { p_sys->i_event_fd }, &(int) { 1 }, 0, 0 ) == -1 )
96 /* Assuming that timeout error is normal when SRT socket is connected. */
97 if ( srt_getlasterror( NULL ) == SRT_ETIMEOUT &&
98 srt_getsockstate( p_sys->sock ) == SRTS_CONNECTED )
100 srt_clearlasterror();
104 i_len = VLC_EGENERIC;
108 if ( event[0].events & EPOLLIN )
111 int ret = read( event[0].data.fd, &cancel, sizeof( bool ) );
119 msg_Dbg( p_access, "Cancelled running" );
125 if ( srt_sendmsg2( p_sys->sock, (char *)p_buffer->p_buffer, i_write, 0 ) == SRT_ERROR )
126 msg_Warn( p_access, "send error: %s", srt_getlasterror_str() );
128 p_buffer->p_buffer += i_write;
129 p_buffer->i_buffer -= i_write;
132 p_next = p_buffer->p_next;
133 block_Release( p_buffer );
138 vlc_interrupt_unregister();
139 if ( i_len <= 0 ) block_ChainRelease( p_buffer );
143 static int Control( sout_access_out_t *p_access, int i_query, va_list args )
145 VLC_UNUSED (p_access);
147 int i_ret = VLC_SUCCESS;
151 case ACCESS_OUT_CONTROLS_PACE:
152 *va_arg( args, bool * ) = false;
156 i_ret = VLC_EGENERIC;
163 static int Open( vlc_object_t *p_this )
165 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
166 sout_access_out_sys_t *p_sys = NULL;
168 char *psz_dst_addr = NULL;
172 struct addrinfo hints = {
173 .ai_socktype = SOCK_DGRAM,
176 if (var_Create ( p_access, "dst-port", VLC_VAR_INTEGER )
177 || var_Create ( p_access, "src-port", VLC_VAR_INTEGER )
178 || var_Create ( p_access, "dst-addr", VLC_VAR_STRING )
179 || var_Create ( p_access, "src-addr", VLC_VAR_STRING ) )
181 msg_Err( p_access, "Valid network information is required." );
185 if( !( p_sys = malloc ( sizeof( *p_sys ) ) ) )
188 p_sys->i_chunk_size = var_InheritInteger( p_access, "chunk-size" );
189 p_sys->i_poll_timeout = var_InheritInteger( p_access, "poll-timeout" );
190 p_sys->i_latency = var_InheritInteger( p_access, "latency" );
191 p_sys->i_poll_id = -1;
192 p_sys->i_event_fd = -1;
194 p_access->p_sys = p_sys;
196 i_dst_port = SRT_DEFAULT_PORT;
197 char *psz_parser = psz_dst_addr = strdup( p_access->psz_path );
204 if (psz_parser[0] == '[')
205 psz_parser = strchr (psz_parser, ']');
207 psz_parser = strchr (psz_parser ? psz_parser : psz_dst_addr, ':');
208 if (psz_parser != NULL)
210 *psz_parser++ = '\0';
211 i_dst_port = atoi (psz_parser);
214 msg_Dbg( p_access, "Setting SRT socket (dest addresss: %s, port: %d).",
215 psz_dst_addr, i_dst_port );
217 stat = vlc_getaddrinfo( psz_dst_addr, i_dst_port, &hints, &res );
220 msg_Err( p_access, "Cannot resolve [%s]:%d (reason: %s)",
223 gai_strerror( stat ) );
228 p_sys->sock = srt_socket( res->ai_family, SOCK_DGRAM, 0 );
229 if ( p_sys->sock == SRT_ERROR )
231 msg_Err( p_access, "Failed to open socket." );
235 /* Make SRT non-blocking */
236 srt_setsockopt( p_sys->sock, 0, SRTO_SNDSYN, &(bool) { false }, sizeof( bool ) );
238 /* Make sure TSBPD mode is enable (SRT mode) */
239 srt_setsockopt( p_sys->sock, 0, SRTO_TSBPDMODE, &(int) { 1 }, sizeof( int ) );
241 /* This is an access_out so it is always a sender */
242 srt_setsockopt( p_sys->sock, 0, SRTO_SENDER, &(int) { 1 }, sizeof( int ) );
245 srt_setsockopt( p_sys->sock, 0, SRTO_TSBPDDELAY, &p_sys->i_latency, sizeof( int ) );
247 p_sys->i_poll_id = srt_epoll_create();
248 if ( p_sys->i_poll_id == -1 )
250 msg_Err( p_access, "Failed to create poll id for SRT socket (reason: %s)",
251 srt_getlasterror_str() );
256 srt_epoll_add_usock( p_sys->i_poll_id, p_sys->sock, &(int) { SRT_EPOLL_OUT });
257 srt_setsockopt( p_sys->sock, 0, SRTO_SENDER, &(int) { 1 }, sizeof(int) );
259 p_sys->i_event_fd = eventfd( 0, EFD_NONBLOCK | EFD_CLOEXEC );
260 srt_epoll_add_ssock( p_sys->i_poll_id, p_sys->i_event_fd, &(int) { EPOLLIN } );
262 stat = srt_connect( p_sys->sock, res->ai_addr, sizeof (struct sockaddr));
263 if ( stat == SRT_ERROR )
265 msg_Err( p_access, "Failed to connect to server (reason: %s)",
266 srt_getlasterror_str() );
270 p_access->pf_write = Write;
271 p_access->pf_control = Control;
273 free( psz_dst_addr );
279 if ( psz_dst_addr != NULL)
280 free( psz_dst_addr );
287 if ( p_sys->i_poll_id != -1 ) srt_epoll_release( p_sys->i_poll_id );
288 if ( p_sys->sock != -1 ) srt_close( p_sys->sock );
289 if ( p_sys->i_event_fd != -1 ) close( p_sys->i_event_fd );
297 static void Close( vlc_object_t * p_this )
299 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
300 sout_access_out_sys_t *p_sys = p_access->p_sys;
302 srt_epoll_release( p_sys->i_poll_id );
303 srt_close( p_sys->sock );
305 if ( p_sys->i_event_fd != -1 )
307 close( p_sys->i_event_fd );
308 p_sys->i_event_fd = -1;
314 /* Module descriptor */
316 set_shortname( N_("SRT") )
317 set_description( N_("SRT stream output") )
318 set_category( CAT_SOUT )
319 set_subcategory( SUBCAT_SOUT_ACO )
321 add_integer( "chunk-size", SRT_DEFAULT_CHUNK_SIZE,
322 N_("SRT chunk size (bytes)"), NULL, true )
323 add_integer( "poll-timeout", SRT_DEFAULT_POLL_TIMEOUT,
324 N_("Return poll wait after timeout miliseconds (-1 = infinite)"), NULL, true )
325 add_integer( "latency", SRT_DEFAULT_LATENCY, N_("SRT latency (ms)"), NULL, true )
327 set_capability( "sout access", 0 )
328 add_shortcut( "srt" )
330 set_callbacks( Open, Close )