1 /*****************************************************************************
2 * srt.c: SRT (Secure Reliable Transport) input 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 *****************************************************************************/
30 #include <vlc_common.h>
31 #include <vlc_interrupt.h>
33 #include <vlc_plugin.h>
34 #include <vlc_access.h>
36 #include <vlc_network.h>
41 /* libsrt defines default packet size as 1316 internally
42 * so srt module takes same value. */
43 #define SRT_DEFAULT_CHUNK_SIZE 1316
44 /* The default timeout is -1 (infinite) */
45 #define SRT_DEFAULT_POLL_TIMEOUT -1
46 /* The default latency is 125
47 * which uses srt library internally */
48 #define SRT_DEFAULT_LATENCY 125
49 /* Crypto key length in bytes. */
50 #define SRT_KEY_LENGTH_TEXT N_("Crypto key length in bytes")
51 #define SRT_DEFAULT_KEY_LENGTH 16
52 static const int srt_key_lengths
[] = {
56 static const char *const srt_key_length_names
[] = {
57 N_("16 bytes"), N_("24 bytes"), N_("32 bytes"),
70 static void srt_wait_interrupted(void *p_data
)
72 stream_t
*p_stream
= p_data
;
73 stream_sys_t
*p_sys
= p_stream
->p_sys
;
74 msg_Dbg( p_stream
, "Waking up srt_epoll_wait");
75 if ( write( p_sys
->i_pipe_fds
[1], &( bool ) { true }, sizeof( bool ) ) < 0 )
77 msg_Err( p_stream
, "Failed to send data to pipe");
81 static int Control(stream_t
*p_stream
, int i_query
, va_list args
)
83 int i_ret
= VLC_SUCCESS
;
88 case STREAM_CAN_FASTSEEK
:
89 case STREAM_CAN_PAUSE
:
90 case STREAM_CAN_CONTROL_PACE
:
91 *va_arg( args
, bool * ) = false;
93 case STREAM_GET_PTS_DELAY
:
94 *va_arg( args
, int64_t * ) = INT64_C(1000)
95 * var_InheritInteger(p_stream
, "network-caching");
105 static block_t
*BlockSRT(stream_t
*p_stream
, bool *restrict eof
)
107 stream_sys_t
*p_sys
= p_stream
->p_sys
;
109 block_t
*pkt
= block_Alloc( p_sys
->i_chunk_size
);
111 if ( unlikely( pkt
== NULL
) )
116 vlc_interrupt_register( srt_wait_interrupted
, p_stream
);
120 if ( srt_epoll_wait( p_sys
->i_poll_id
,
121 ready
, &(int) { 2 }, NULL
, 0, p_sys
->i_poll_timeout
,
122 &(int) { p_sys
->i_pipe_fds
[0] }, &(int) { 1 }, NULL
, 0 ) == -1 )
124 int srt_err
= srt_getlasterror( NULL
);
126 /* Assuming that timeout error is normal when SRT socket is connected. */
127 if ( srt_err
== SRT_ETIMEOUT
&& srt_getsockstate( p_sys
->sock
) == SRTS_CONNECTED
)
132 msg_Err( p_stream
, "released poll wait (reason : %s)", srt_getlasterror_str() );
137 int ret
= read( p_sys
->i_pipe_fds
[0], &cancel
, sizeof( bool ) );
138 if ( ret
> 0 && cancel
)
140 msg_Dbg( p_stream
, "Cancelled running" );
144 int stat
= srt_recvmsg( p_sys
->sock
, (char *)pkt
->p_buffer
, p_sys
->i_chunk_size
);
146 if ( stat
== SRT_ERROR
)
148 msg_Err( p_stream
, "failed to recevie SRT packet (reason: %s)", srt_getlasterror_str() );
152 pkt
->i_buffer
= stat
;
153 vlc_interrupt_unregister();
157 msg_Dbg( p_stream
, "EOS");
161 srt_clearlasterror();
162 vlc_interrupt_unregister();
167 static int Open(vlc_object_t
*p_this
)
169 stream_t
*p_stream
= (stream_t
*)p_this
;
170 stream_sys_t
*p_sys
= NULL
;
171 vlc_url_t parsed_url
= { 0 };
172 struct addrinfo hints
= {
173 .ai_socktype
= SOCK_DGRAM
,
177 char *psz_passphrase
= NULL
;
179 p_sys
= vlc_obj_malloc( p_this
, sizeof( *p_sys
) );
180 if( unlikely( p_sys
== NULL
) )
183 if ( vlc_UrlParse( &parsed_url
, p_stream
->psz_url
) == -1 )
185 msg_Err( p_stream
, "Failed to parse a given URL (%s)", p_stream
->psz_url
);
189 p_sys
->i_chunk_size
= var_InheritInteger( p_stream
, "chunk-size" );
190 p_sys
->i_poll_timeout
= var_InheritInteger( p_stream
, "poll-timeout" );
191 p_sys
->i_latency
= var_InheritInteger( p_stream
, "latency" );
192 p_sys
->i_poll_id
= -1;
193 p_stream
->p_sys
= p_sys
;
194 p_stream
->pf_block
= BlockSRT
;
195 p_stream
->pf_control
= Control
;
197 psz_passphrase
= var_InheritString( p_stream
, "passphrase" );
199 stat
= vlc_getaddrinfo( parsed_url
.psz_host
, parsed_url
.i_port
, &hints
, &res
);
202 msg_Err( p_stream
, "Cannot resolve [%s]:%d (reason: %s)",
205 gai_strerror( stat
) );
210 p_sys
->sock
= srt_socket( res
->ai_family
, SOCK_DGRAM
, 0 );
211 if ( p_sys
->sock
== SRT_ERROR
)
213 msg_Err( p_stream
, "Failed to open socket." );
217 /* Make SRT non-blocking */
218 srt_setsockopt( p_sys
->sock
, 0, SRTO_SNDSYN
, &(bool) { false }, sizeof( bool ) );
220 /* Make sure TSBPD mode is enable (SRT mode) */
221 srt_setsockopt( p_sys
->sock
, 0, SRTO_TSBPDMODE
, &(int) { 1 }, sizeof( int ) );
224 srt_setsockopt( p_sys
->sock
, 0, SRTO_TSBPDDELAY
, &p_sys
->i_latency
, sizeof( int ) );
226 if ( psz_passphrase
!= NULL
&& psz_passphrase
[0] != '\0')
228 int i_key_length
= var_InheritInteger( p_stream
, "key-length" );
230 srt_setsockopt( p_sys
->sock
, 0, SRTO_PASSPHRASE
,
231 psz_passphrase
, strlen( psz_passphrase
) );
232 srt_setsockopt( p_sys
->sock
, 0, SRTO_PBKEYLEN
,
233 &i_key_length
, sizeof( int ) );
236 p_sys
->i_poll_id
= srt_epoll_create();
237 if ( p_sys
->i_poll_id
== -1 )
239 msg_Err( p_stream
, "Failed to create poll id for SRT socket." );
243 if ( vlc_pipe( p_sys
->i_pipe_fds
) != 0 )
245 msg_Err( p_stream
, "Failed to create pipe fds." );
249 fcntl( p_sys
->i_pipe_fds
[0], F_SETFL
, O_NONBLOCK
| fcntl( p_sys
->i_pipe_fds
[0], F_GETFL
) );
251 srt_epoll_add_usock( p_sys
->i_poll_id
, p_sys
->sock
, &(int) { SRT_EPOLL_IN
} );
252 srt_epoll_add_ssock( p_sys
->i_poll_id
, p_sys
->i_pipe_fds
[0], &(int) { SRT_EPOLL_IN
} );
254 stat
= srt_connect( p_sys
->sock
, res
->ai_addr
, sizeof (struct sockaddr
) );
256 if ( stat
== SRT_ERROR
)
258 msg_Err( p_stream
, "Failed to connect to server." );
262 vlc_UrlClean( &parsed_url
);
264 free (psz_passphrase
);
270 if ( parsed_url
.psz_host
!= NULL
271 && parsed_url
.psz_buffer
!= NULL
)
273 vlc_UrlClean( &parsed_url
);
281 vlc_close( p_sys
->i_pipe_fds
[0] );
282 vlc_close( p_sys
->i_pipe_fds
[1] );
284 if ( p_sys
->i_poll_id
!= -1 )
286 srt_epoll_release( p_sys
->i_poll_id
);
287 p_sys
->i_poll_id
= -1;
289 srt_close( p_sys
->sock
);
291 free (psz_passphrase
);
296 static void Close(vlc_object_t
*p_this
)
298 stream_t
*p_stream
= (stream_t
*)p_this
;
299 stream_sys_t
*p_sys
= p_stream
->p_sys
;
301 if ( p_sys
->i_poll_id
!= -1 )
303 srt_epoll_release( p_sys
->i_poll_id
);
304 p_sys
->i_poll_id
= -1;
306 msg_Dbg( p_stream
, "closing server" );
307 srt_close( p_sys
->sock
);
309 vlc_close( p_sys
->i_pipe_fds
[0] );
310 vlc_close( p_sys
->i_pipe_fds
[1] );
313 /* Module descriptor */
315 set_shortname( N_("SRT") )
316 set_description( N_("SRT input") )
317 set_category( CAT_INPUT
)
318 set_subcategory( SUBCAT_INPUT_ACCESS
)
320 add_integer( "chunk-size", SRT_DEFAULT_CHUNK_SIZE
,
321 N_("SRT chunk size (bytes)"), NULL
, true )
322 add_integer( "poll-timeout", SRT_DEFAULT_POLL_TIMEOUT
,
323 N_("Return poll wait after timeout milliseconds (-1 = infinite)"), NULL
, true )
324 add_integer( "latency", SRT_DEFAULT_LATENCY
, N_("SRT latency (ms)"), NULL
, true )
325 add_password( "passphrase", "", N_("Password for stream encryption"), NULL
, false )
326 add_integer( "key-length", SRT_DEFAULT_KEY_LENGTH
,
327 SRT_KEY_LENGTH_TEXT
, SRT_KEY_LENGTH_TEXT
, false )
328 change_integer_list( srt_key_lengths
, srt_key_length_names
)
330 set_capability( "access", 0 )
331 add_shortcut( "srt" )
333 set_callbacks( Open
, Close
)