1 /*****************************************************************************
2 * srt.c: SRT (Secure Reliable Transport) access 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 *****************************************************************************/
24 #include "srt_common.h"
27 #include <vlc_plugin.h>
28 #include <vlc_access.h>
29 #include <vlc_interrupt.h>
31 #include <vlc_network.h>
44 int i_chunks
; /* Number of chunks to allocate in the next read */
49 static void srt_wait_interrupted(void *p_data
)
51 stream_t
*p_stream
= p_data
;
52 stream_sys_t
*p_sys
= p_stream
->p_sys
;
54 vlc_mutex_lock( &p_sys
->lock
);
55 if ( p_sys
->i_poll_id
>= 0 && p_sys
->sock
!= SRT_INVALID_SOCK
)
57 p_sys
->b_interrupted
= true;
59 msg_Dbg( p_stream
, "Waking up srt_epoll_wait");
61 /* Removing all socket descriptors from the monitoring list
62 * wakes up SRT's threads. We only have one to remove. */
63 srt_epoll_remove_usock( p_sys
->i_poll_id
, p_sys
->sock
);
65 vlc_mutex_unlock( &p_sys
->lock
);
68 static int Control(stream_t
*p_stream
, int i_query
, va_list args
)
70 int i_ret
= VLC_SUCCESS
;
75 case STREAM_CAN_FASTSEEK
:
76 case STREAM_CAN_PAUSE
:
77 case STREAM_CAN_CONTROL_PACE
:
78 *va_arg( args
, bool * ) = false;
80 case STREAM_GET_PTS_DELAY
:
81 *va_arg( args
, vlc_tick_t
* ) = VLC_TICK_FROM_MS(
82 var_InheritInteger(p_stream
, "network-caching") );
92 static bool srt_schedule_reconnect(stream_t
*p_stream
)
94 vlc_object_t
*strm_obj
= (vlc_object_t
*) p_stream
;
95 int i_latency
=var_InheritInteger( p_stream
, SRT_PARAM_LATENCY
);
96 int i_payload_size
= var_InheritInteger( p_stream
, SRT_PARAM_PAYLOAD_SIZE
);
98 char *psz_passphrase
= var_InheritString( p_stream
, SRT_PARAM_PASSPHRASE
);
99 bool passphrase_needs_free
= true;
100 char *psz_streamid
= var_InheritString( p_stream
, SRT_PARAM_STREAMID
);
101 bool streamid_needs_free
= true;
104 struct addrinfo hints
= {
105 .ai_socktype
= SOCK_DGRAM
,
108 stream_sys_t
*p_sys
= p_stream
->p_sys
;
111 stat
= vlc_getaddrinfo( p_sys
->psz_host
, p_sys
->i_port
, &hints
, &res
);
114 msg_Err( p_stream
, "Cannot resolve [%s]:%d (reason: %s)",
117 gai_strerror( stat
) );
123 /* Always start with a fresh socket */
124 if (p_sys
->sock
!= SRT_INVALID_SOCK
)
126 srt_epoll_remove_usock( p_sys
->i_poll_id
, p_sys
->sock
);
127 srt_close( p_sys
->sock
);
130 p_sys
->sock
= srt_socket( res
->ai_family
, SOCK_DGRAM
, 0 );
131 if ( p_sys
->sock
== SRT_INVALID_SOCK
)
133 msg_Err( p_stream
, "Failed to open socket." );
138 if (p_stream
->psz_url
) {
139 url
= strdup( p_stream
->psz_url
);
140 if (srt_parse_url( url
, ¶ms
)) {
141 if (params
.latency
!= -1)
142 i_latency
= params
.latency
;
143 if (params
.payload_size
!= -1)
144 i_payload_size
= params
.payload_size
;
145 if (params
.passphrase
!= NULL
) {
146 free( psz_passphrase
);
147 passphrase_needs_free
= false;
148 psz_passphrase
= (char *) params
.passphrase
;
150 if (params
.streamid
!= NULL
) {
151 free( psz_streamid
);
152 streamid_needs_free
= false;
153 psz_streamid
= (char *) params
.streamid
;
158 /* Make SRT non-blocking */
159 srt_setsockopt( p_sys
->sock
, 0, SRTO_SNDSYN
,
160 &(bool) { false }, sizeof( bool ) );
161 srt_setsockopt( p_sys
->sock
, 0, SRTO_RCVSYN
,
162 &(bool) { false }, sizeof( bool ) );
164 /* Make sure TSBPD mode is enable (SRT mode) */
165 srt_setsockopt( p_sys
->sock
, 0, SRTO_TSBPDMODE
,
166 &(int) { 1 }, sizeof( int ) );
168 /* This is an access module so it is always a receiver */
169 srt_setsockopt( p_sys
->sock
, 0, SRTO_SENDER
,
170 &(int) { 0 }, sizeof( int ) );
173 srt_set_socket_option( strm_obj
, SRT_PARAM_LATENCY
, p_sys
->sock
,
174 SRTO_LATENCY
, &i_latency
, sizeof(i_latency
) );
177 if (psz_passphrase
!= NULL
&& psz_passphrase
[0] != '\0') {
178 int i_key_length
= var_InheritInteger( p_stream
, SRT_PARAM_KEY_LENGTH
);
180 srt_set_socket_option( strm_obj
, SRT_PARAM_KEY_LENGTH
, p_sys
->sock
,
181 SRTO_PBKEYLEN
, &i_key_length
, sizeof(i_key_length
) );
183 srt_set_socket_option( strm_obj
, SRT_PARAM_PASSPHRASE
, p_sys
->sock
,
184 SRTO_PASSPHRASE
, psz_passphrase
, strlen(psz_passphrase
) );
188 if (psz_streamid
!= NULL
&& psz_streamid
[0] != '\0') {
189 srt_set_socket_option( strm_obj
, SRT_PARAM_STREAMID
, p_sys
->sock
,
190 SRTO_STREAMID
, psz_streamid
, strlen(psz_streamid
) );
193 /* set maximum payload size */
194 srt_set_socket_option( strm_obj
, SRT_PARAM_PAYLOAD_SIZE
, p_sys
->sock
,
195 SRTO_PAYLOADSIZE
, &i_payload_size
, sizeof(i_payload_size
) );
198 srt_epoll_add_usock( p_sys
->i_poll_id
, p_sys
->sock
,
199 &(int) { SRT_EPOLL_ERR
| SRT_EPOLL_IN
});
201 /* Schedule a connect */
202 msg_Dbg( p_stream
, "Schedule SRT connect (dest addresss: %s, port: %d).",
203 p_sys
->psz_host
, p_sys
->i_port
);
205 stat
= srt_connect( p_sys
->sock
, res
->ai_addr
, res
->ai_addrlen
);
206 if (stat
== SRT_ERROR
) {
207 msg_Err( p_stream
, "Failed to connect to server (reason: %s)",
208 srt_getlasterror_str() );
212 /* Reset the number of chunks to allocate as the bitrate of
213 * the stream may have changed.
215 p_sys
->i_chunks
= SRT_MIN_CHUNKS_TRYREAD
;
218 if (failed
&& p_sys
->sock
!= SRT_INVALID_SOCK
)
220 srt_epoll_remove_usock( p_sys
->i_poll_id
, p_sys
->sock
);
221 srt_close(p_sys
->sock
);
222 p_sys
->sock
= SRT_INVALID_SOCK
;
225 if (passphrase_needs_free
)
226 free( psz_passphrase
);
227 if (streamid_needs_free
)
228 free( psz_streamid
);
235 static block_t
*BlockSRT(stream_t
*p_stream
, bool *restrict eof
)
237 stream_sys_t
*p_sys
= p_stream
->p_sys
;
238 int i_chunk_size
= var_InheritInteger( p_stream
, SRT_PARAM_CHUNK_SIZE
);
239 int i_poll_timeout
= var_InheritInteger( p_stream
, SRT_PARAM_POLL_TIMEOUT
);
240 /* SRT doesn't have a concept of EOF for live streams. */
245 /* We are told to stop. Stop. */
249 if ( p_sys
->i_chunks
== 0 )
250 p_sys
->i_chunks
= SRT_MIN_CHUNKS_TRYREAD
;
252 size_t i_chunk_size_actual
= ( i_chunk_size
> 0 )
253 ? i_chunk_size
: SRT_DEFAULT_CHUNK_SIZE
;
254 size_t bufsize
= i_chunk_size_actual
* p_sys
->i_chunks
;
255 block_t
*pkt
= block_Alloc( bufsize
);
256 if ( unlikely( pkt
== NULL
) )
261 vlc_interrupt_register( srt_wait_interrupted
, p_stream
);
265 while ( srt_epoll_wait( p_sys
->i_poll_id
,
266 ready
, &readycnt
, 0, 0,
267 i_poll_timeout
, NULL
, 0, NULL
, 0 ) >= 0)
269 if ( readycnt
< 0 || ready
[0] != p_sys
->sock
)
271 /* should never happen, force recovery */
272 srt_close(p_sys
->sock
);
273 p_sys
->sock
= SRT_INVALID_SOCK
;
276 switch( srt_getsockstate( p_sys
->sock
) )
284 /* Failed. Schedule recovery. */
285 if ( !srt_schedule_reconnect( p_stream
) )
286 msg_Err( p_stream
, "Failed to schedule connect" );
293 /* Try to get as much data as possible out of the lib, if there
294 * is still some left, increase the number of chunks to read so that
295 * it will read faster on the next iteration. This way the buffer will
296 * grow until it reads fast enough to keep the library empty after
300 while ( ( bufsize
- pkt
->i_buffer
) >= i_chunk_size_actual
)
302 int stat
= srt_recvmsg( p_sys
->sock
,
303 (char *)( pkt
->p_buffer
+ pkt
->i_buffer
),
304 bufsize
- pkt
->i_buffer
);
309 pkt
->i_buffer
+= (size_t)stat
;
312 msg_Dbg ( p_stream
, "Read %zu bytes out of a max of %zu"
313 " (%d chunks of %zu bytes)", pkt
->i_buffer
,
314 p_sys
->i_chunks
* i_chunk_size_actual
, p_sys
->i_chunks
,
315 i_chunk_size_actual
);
318 /* Gradually adjust number of chunks we read at a time
319 * up to a predefined maximum. The actual number we might
320 * settle on depends on stream's bit rate.
322 size_t rem
= bufsize
- pkt
->i_buffer
;
323 if ( rem
< i_chunk_size_actual
)
325 if ( p_sys
->i_chunks
< SRT_MAX_CHUNKS_TRYREAD
)
334 /* if the poll reports errors for any reason at all,
335 * including a timeout, we skip the turn.
340 if (pkt
->i_buffer
== 0) {
345 vlc_interrupt_unregister();
347 /* Re-add the socket to the poll if we were interrupted */
348 vlc_mutex_lock( &p_sys
->lock
);
349 if ( p_sys
->b_interrupted
)
351 srt_epoll_add_usock( p_sys
->i_poll_id
, p_sys
->sock
,
352 &(int) { SRT_EPOLL_ERR
| SRT_EPOLL_IN
} );
353 p_sys
->b_interrupted
= false;
355 vlc_mutex_unlock( &p_sys
->lock
);
360 static int Open(vlc_object_t
*p_this
)
362 stream_t
*p_stream
= (stream_t
*)p_this
;
363 stream_sys_t
*p_sys
= NULL
;
364 vlc_url_t parsed_url
= { 0 };
366 p_sys
= vlc_obj_calloc( p_this
, 1, sizeof( *p_sys
) );
367 if( unlikely( p_sys
== NULL
) )
372 vlc_mutex_init( &p_sys
->lock
);
374 p_stream
->p_sys
= p_sys
;
376 if ( vlc_UrlParse( &parsed_url
, p_stream
->psz_url
) == -1 )
378 msg_Err( p_stream
, "Failed to parse input URL (%s)",
383 p_sys
->psz_host
= vlc_obj_strdup( p_this
, parsed_url
.psz_host
);
384 p_sys
->i_port
= parsed_url
.i_port
;
386 vlc_UrlClean( &parsed_url
);
388 p_sys
->i_poll_id
= srt_epoll_create();
389 if ( p_sys
->i_poll_id
== -1 )
391 msg_Err( p_stream
, "Failed to create poll id for SRT socket." );
395 if ( !srt_schedule_reconnect( p_stream
) )
397 msg_Err( p_stream
, "Failed to schedule connect");
402 p_stream
->pf_block
= BlockSRT
;
403 p_stream
->pf_control
= Control
;
408 if ( p_sys
->sock
!= -1 ) srt_close( p_sys
->sock
);
409 if ( p_sys
->i_poll_id
!= -1 ) srt_epoll_release( p_sys
->i_poll_id
);
414 static void Close(vlc_object_t
*p_this
)
416 stream_t
*p_stream
= (stream_t
*)p_this
;
417 stream_sys_t
*p_sys
= p_stream
->p_sys
;
419 srt_epoll_remove_usock( p_sys
->i_poll_id
, p_sys
->sock
);
420 srt_close( p_sys
->sock
);
421 srt_epoll_release( p_sys
->i_poll_id
);
426 /* Module descriptor */
428 set_shortname( N_( "SRT" ) )
429 set_description( N_( "SRT input" ) )
430 set_category( CAT_INPUT
)
431 set_subcategory( SUBCAT_INPUT_ACCESS
)
433 add_integer( SRT_PARAM_CHUNK_SIZE
, SRT_DEFAULT_CHUNK_SIZE
,
434 N_( "SRT chunk size (bytes)" ), NULL
, true )
435 add_integer( SRT_PARAM_POLL_TIMEOUT
, SRT_DEFAULT_POLL_TIMEOUT
,
436 N_( "Return poll wait after timeout milliseconds (-1 = infinite)" ),
438 add_integer( SRT_PARAM_LATENCY
, SRT_DEFAULT_LATENCY
,
439 N_( "SRT latency (ms)" ), NULL
, true )
440 add_password( SRT_PARAM_PASSPHRASE
, "",
441 N_( "Password for stream encryption" ), NULL
)
442 add_integer( SRT_PARAM_PAYLOAD_SIZE
, SRT_DEFAULT_PAYLOAD_SIZE
,
443 N_( "SRT maximum payload size (bytes)" ), NULL
, true )
444 add_integer( SRT_PARAM_KEY_LENGTH
, SRT_DEFAULT_KEY_LENGTH
,
445 SRT_KEY_LENGTH_TEXT
, SRT_KEY_LENGTH_TEXT
, false )
446 change_integer_list( srt_key_lengths
, srt_key_length_names
)
447 add_string(SRT_PARAM_STREAMID
, "",
448 N_(" SRT Stream ID"), NULL
, false)
451 set_capability("access", 0)
454 set_callbacks(Open
, Close
)