1 /*****************************************************************************
2 * srt.c: SRT (Secure Reliable Transport) input module
3 *****************************************************************************
4 * Copyright (C) 2017-2018, Collabora Ltd.
5 * Copyright (C) 2018, Haivision Systems Inc.
7 * Authors: Justin Kim <justin.kim@collabora.com>
8 * Roman Diouskine <rdiouskine@haivision.com>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
29 #include <vlc_common.h>
30 #include <vlc_interrupt.h>
32 #include <vlc_plugin.h>
33 #include <vlc_access.h>
35 #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 /* The default timeout is -1 (infinite) */
44 #define SRT_DEFAULT_POLL_TIMEOUT -1
45 /* The default latency is 125
46 * which uses srt library internally */
47 #define SRT_DEFAULT_LATENCY 125
48 /* Crypto key length in bytes. */
49 #define SRT_KEY_LENGTH_TEXT N_("Crypto key length in bytes")
50 #define SRT_DEFAULT_KEY_LENGTH 16
51 static const int srt_key_lengths
[] = {
55 static const char *const srt_key_length_names
[] = {
56 N_("16 bytes"), N_("24 bytes"), N_("32 bytes"),
69 static void srt_wait_interrupted(void *p_data
)
71 stream_t
*p_stream
= p_data
;
72 stream_sys_t
*p_sys
= p_stream
->p_sys
;
74 vlc_mutex_lock( &p_sys
->lock
);
75 if ( p_sys
->i_poll_id
>= 0 && p_sys
->sock
!= SRT_INVALID_SOCK
)
77 p_sys
->b_interrupted
= true;
79 msg_Dbg( p_stream
, "Waking up srt_epoll_wait");
81 /* Removing all socket descriptors from the monitoring list
82 * wakes up SRT's threads. We only have one to remove. */
83 srt_epoll_remove_usock( p_sys
->i_poll_id
, p_sys
->sock
);
85 vlc_mutex_unlock( &p_sys
->lock
);
88 static int Control(stream_t
*p_stream
, int i_query
, va_list args
)
90 int i_ret
= VLC_SUCCESS
;
95 case STREAM_CAN_FASTSEEK
:
96 case STREAM_CAN_PAUSE
:
97 case STREAM_CAN_CONTROL_PACE
:
98 *va_arg( args
, bool * ) = false;
100 case STREAM_GET_PTS_DELAY
:
101 *va_arg( args
, int64_t * ) = INT64_C(1000)
102 * var_InheritInteger(p_stream
, "network-caching");
105 i_ret
= VLC_EGENERIC
;
112 static bool srt_schedule_reconnect(stream_t
*p_stream
)
116 char *psz_passphrase
= NULL
;
118 struct addrinfo hints
= {
119 .ai_socktype
= SOCK_DGRAM
,
122 stream_sys_t
*p_sys
= p_stream
->p_sys
;
125 stat
= vlc_getaddrinfo( p_sys
->psz_host
, p_sys
->i_port
, &hints
, &res
);
128 msg_Err( p_stream
, "Cannot resolve [%s]:%d (reason: %s)",
131 gai_strerror( stat
) );
137 /* Always start with a fresh socket */
138 if (p_sys
->sock
!= SRT_INVALID_SOCK
)
140 srt_epoll_remove_usock( p_sys
->i_poll_id
, p_sys
->sock
);
141 srt_close( p_sys
->sock
);
144 p_sys
->sock
= srt_socket( res
->ai_family
, SOCK_DGRAM
, 0 );
145 if ( p_sys
->sock
== SRT_INVALID_SOCK
)
147 msg_Err( p_stream
, "Failed to open socket." );
152 /* Make SRT non-blocking */
153 srt_setsockopt( p_sys
->sock
, 0, SRTO_SNDSYN
,
154 &(bool) { false }, sizeof( bool ) );
155 srt_setsockopt( p_sys
->sock
, 0, SRTO_RCVSYN
,
156 &(bool) { false }, sizeof( bool ) );
158 /* Make sure TSBPD mode is enable (SRT mode) */
159 srt_setsockopt( p_sys
->sock
, 0, SRTO_TSBPDMODE
,
160 &(int) { 1 }, sizeof( int ) );
162 /* This is an access module so it is always a receiver */
163 srt_setsockopt( p_sys
->sock
, 0, SRTO_SENDER
,
164 &(int) { 0 }, sizeof( int ) );
167 i_latency
= var_InheritInteger( p_stream
, "latency" );
168 srt_setsockopt( p_sys
->sock
, 0, SRTO_TSBPDDELAY
,
169 &i_latency
, sizeof( int ) );
171 psz_passphrase
= var_InheritString( p_stream
, "passphrase" );
172 if ( psz_passphrase
!= NULL
&& psz_passphrase
[0] != '\0')
174 int i_key_length
= var_InheritInteger( p_stream
, "key-length" );
175 srt_setsockopt( p_sys
->sock
, 0, SRTO_PASSPHRASE
,
176 psz_passphrase
, strlen( psz_passphrase
) );
177 srt_setsockopt( p_sys
->sock
, 0, SRTO_PBKEYLEN
,
178 &i_key_length
, sizeof( int ) );
181 srt_epoll_add_usock( p_sys
->i_poll_id
, p_sys
->sock
,
182 &(int) { SRT_EPOLL_ERR
| SRT_EPOLL_IN
});
184 /* Schedule a connect */
185 msg_Dbg( p_stream
, "Schedule SRT connect (dest addresss: %s, port: %d).",
186 p_sys
->psz_host
, p_sys
->i_port
);
188 stat
= srt_connect( p_sys
->sock
, res
->ai_addr
, res
->ai_addrlen
);
189 if ( stat
== SRT_ERROR
)
191 msg_Err( p_stream
, "Failed to connect to server (reason: %s)",
192 srt_getlasterror_str() );
197 if (failed
&& p_sys
->sock
!= SRT_INVALID_SOCK
)
199 srt_epoll_remove_usock( p_sys
->i_poll_id
, p_sys
->sock
);
200 srt_close(p_sys
->sock
);
201 p_sys
->sock
= SRT_INVALID_SOCK
;
205 free( psz_passphrase
);
210 static block_t
*BlockSRT(stream_t
*p_stream
, bool *restrict eof
)
212 stream_sys_t
*p_sys
= p_stream
->p_sys
;
213 int i_chunk_size
= var_InheritInteger( p_stream
, "chunk-size" );
214 int i_poll_timeout
= var_InheritInteger( p_stream
, "poll-timeout" );
218 /* We are told to stop. Stop. */
222 block_t
*pkt
= block_Alloc( i_chunk_size
);
223 if ( unlikely( pkt
== NULL
) )
228 vlc_interrupt_register( srt_wait_interrupted
, p_stream
);
232 while ( srt_epoll_wait( p_sys
->i_poll_id
,
233 ready
, &readycnt
, 0, 0,
234 i_poll_timeout
, NULL
, 0, NULL
, 0 ) >= 0)
236 if ( readycnt
< 0 || ready
[0] != p_sys
->sock
)
238 /* should never happen, force recovery */
239 srt_close(p_sys
->sock
);
240 p_sys
->sock
= SRT_INVALID_SOCK
;
243 switch( srt_getsockstate( p_sys
->sock
) )
251 /* Failed. Schedule recovery. */
252 if ( !srt_schedule_reconnect( p_stream
) )
253 msg_Err( p_stream
, "Failed to schedule connect" );
260 int stat
= srt_recvmsg( p_sys
->sock
,
261 (char *)pkt
->p_buffer
, i_chunk_size
);
264 pkt
->i_buffer
= stat
;
268 msg_Err( p_stream
, "failed to receive packet, set EOS (reason: %s)",
269 srt_getlasterror_str() );
274 /* if the poll reports errors for any reason at all
275 * including a timeout, or there is a read error,
283 vlc_interrupt_unregister();
285 /* Re-add the socket to the poll if we were interrupted */
286 vlc_mutex_lock( &p_sys
->lock
);
287 if ( p_sys
->b_interrupted
)
289 srt_epoll_add_usock( p_sys
->i_poll_id
, p_sys
->sock
,
290 &(int) { SRT_EPOLL_ERR
| SRT_EPOLL_IN
} );
291 p_sys
->b_interrupted
= false;
293 vlc_mutex_unlock( &p_sys
->lock
);
298 static int Open(vlc_object_t
*p_this
)
300 stream_t
*p_stream
= (stream_t
*)p_this
;
301 stream_sys_t
*p_sys
= NULL
;
302 vlc_url_t parsed_url
= { 0 };
304 p_sys
= vlc_obj_calloc( p_this
, 1, sizeof( *p_sys
) );
305 if( unlikely( p_sys
== NULL
) )
310 vlc_mutex_init( &p_sys
->lock
);
312 p_stream
->p_sys
= p_sys
;
314 if ( vlc_UrlParse( &parsed_url
, p_stream
->psz_url
) == -1 )
316 msg_Err( p_stream
, "Failed to parse input URL (%s)",
321 p_sys
->psz_host
= strdup( parsed_url
.psz_host
);
322 p_sys
->i_port
= parsed_url
.i_port
;
324 vlc_UrlClean( &parsed_url
);
326 p_sys
->i_poll_id
= srt_epoll_create();
327 if ( p_sys
->i_poll_id
== -1 )
329 msg_Err( p_stream
, "Failed to create poll id for SRT socket." );
333 if ( !srt_schedule_reconnect( p_stream
) )
335 msg_Err( p_stream
, "Failed to schedule connect");
340 p_stream
->pf_block
= BlockSRT
;
341 p_stream
->pf_control
= Control
;
346 vlc_mutex_destroy( &p_sys
->lock
);
350 if ( p_sys
->sock
!= -1 ) srt_close( p_sys
->sock
);
351 if ( p_sys
->i_poll_id
!= -1 ) srt_epoll_release( p_sys
->i_poll_id
);
353 free( p_sys
->psz_host
);
355 vlc_obj_free( p_this
, p_sys
);
356 p_stream
->p_sys
= NULL
;
362 static void Close(vlc_object_t
*p_this
)
364 stream_t
*p_stream
= (stream_t
*)p_this
;
365 stream_sys_t
*p_sys
= p_stream
->p_sys
;
369 vlc_mutex_destroy( &p_sys
->lock
);
371 srt_epoll_remove_usock( p_sys
->i_poll_id
, p_sys
->sock
);
372 srt_close( p_sys
->sock
);
373 srt_epoll_release( p_sys
->i_poll_id
);
375 free( p_sys
->psz_host
);
377 vlc_obj_free( p_this
, p_sys
);
378 p_stream
->p_sys
= NULL
;
384 /* Module descriptor */
386 set_shortname( N_("SRT") )
387 set_description( N_("SRT input") )
388 set_category( CAT_INPUT
)
389 set_subcategory( SUBCAT_INPUT_ACCESS
)
391 add_integer( "chunk-size", SRT_DEFAULT_CHUNK_SIZE
,
392 N_("SRT chunk size (bytes)"), NULL
, true )
393 add_integer( "poll-timeout", SRT_DEFAULT_POLL_TIMEOUT
,
394 N_("Return poll wait after timeout milliseconds (-1 = infinite)"), NULL
, true )
395 add_integer( "latency", SRT_DEFAULT_LATENCY
, N_("SRT latency (ms)"), NULL
, true )
396 add_password("passphrase", "", N_("Password for stream encryption"), NULL
)
397 add_integer( "key-length", SRT_DEFAULT_KEY_LENGTH
,
398 SRT_KEY_LENGTH_TEXT
, SRT_KEY_LENGTH_TEXT
, false )
399 change_integer_list( srt_key_lengths
, srt_key_length_names
)
401 set_capability( "access", 0 )
402 add_shortcut( "srt" )
404 set_callbacks( Open
, Close
)