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 *****************************************************************************/
30 #include <vlc_common.h>
31 #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_pipe_fds
[1], &( 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
);
91 if ( srt_epoll_wait( p_sys
->i_poll_id
,
92 0, 0, ready
, &(int){ 2 }, p_sys
->i_poll_timeout
,
93 &(int) { p_sys
->i_pipe_fds
[0] }, &(int) { 1 }, NULL
, 0 ) == -1 )
95 /* Assuming that timeout error is normal when SRT socket is connected. */
96 if ( srt_getlasterror( NULL
) == SRT_ETIMEOUT
&&
97 srt_getsockstate( p_sys
->sock
) == SRTS_CONNECTED
)
103 i_len
= VLC_EGENERIC
;
108 int ret
= read( p_sys
->i_pipe_fds
[0], &cancel
, sizeof( bool ) );
109 if ( ret
> 0 && cancel
)
111 msg_Dbg( p_access
, "Cancelled running" );
116 if ( srt_sendmsg2( p_sys
->sock
, (char *)p_buffer
->p_buffer
, i_write
, 0 ) == SRT_ERROR
)
117 msg_Warn( p_access
, "send error: %s", srt_getlasterror_str() );
119 p_buffer
->p_buffer
+= i_write
;
120 p_buffer
->i_buffer
-= i_write
;
123 p_next
= p_buffer
->p_next
;
124 block_Release( p_buffer
);
129 vlc_interrupt_unregister();
130 if ( i_len
<= 0 ) block_ChainRelease( p_buffer
);
134 static int Control( sout_access_out_t
*p_access
, int i_query
, va_list args
)
136 VLC_UNUSED (p_access
);
138 int i_ret
= VLC_SUCCESS
;
142 case ACCESS_OUT_CONTROLS_PACE
:
143 *va_arg( args
, bool * ) = false;
147 i_ret
= VLC_EGENERIC
;
154 static int Open( vlc_object_t
*p_this
)
156 sout_access_out_t
*p_access
= (sout_access_out_t
*)p_this
;
157 sout_access_out_sys_t
*p_sys
= NULL
;
159 char *psz_dst_addr
= NULL
;
163 struct addrinfo hints
= {
164 .ai_socktype
= SOCK_DGRAM
,
167 if (var_Create ( p_access
, "dst-port", VLC_VAR_INTEGER
)
168 || var_Create ( p_access
, "src-port", VLC_VAR_INTEGER
)
169 || var_Create ( p_access
, "dst-addr", VLC_VAR_STRING
)
170 || var_Create ( p_access
, "src-addr", VLC_VAR_STRING
) )
172 msg_Err( p_access
, "Valid network information is required." );
176 if( !( p_sys
= malloc ( sizeof( *p_sys
) ) ) )
179 p_sys
->i_chunk_size
= var_InheritInteger( p_access
, "chunk-size" );
180 p_sys
->i_poll_timeout
= var_InheritInteger( p_access
, "poll-timeout" );
181 p_sys
->i_latency
= var_InheritInteger( p_access
, "latency" );
182 p_sys
->i_poll_id
= -1;
184 p_access
->p_sys
= p_sys
;
186 i_dst_port
= SRT_DEFAULT_PORT
;
187 char *psz_parser
= psz_dst_addr
= strdup( p_access
->psz_path
);
194 if (psz_parser
[0] == '[')
195 psz_parser
= strchr (psz_parser
, ']');
197 psz_parser
= strchr (psz_parser
? psz_parser
: psz_dst_addr
, ':');
198 if (psz_parser
!= NULL
)
200 *psz_parser
++ = '\0';
201 i_dst_port
= atoi (psz_parser
);
204 msg_Dbg( p_access
, "Setting SRT socket (dest addresss: %s, port: %d).",
205 psz_dst_addr
, i_dst_port
);
207 stat
= vlc_getaddrinfo( psz_dst_addr
, i_dst_port
, &hints
, &res
);
210 msg_Err( p_access
, "Cannot resolve [%s]:%d (reason: %s)",
213 gai_strerror( stat
) );
218 p_sys
->sock
= srt_socket( res
->ai_family
, SOCK_DGRAM
, 0 );
219 if ( p_sys
->sock
== SRT_ERROR
)
221 msg_Err( p_access
, "Failed to open socket." );
225 /* Make SRT non-blocking */
226 srt_setsockopt( p_sys
->sock
, 0, SRTO_SNDSYN
, &(bool) { false }, sizeof( bool ) );
228 /* Make sure TSBPD mode is enable (SRT mode) */
229 srt_setsockopt( p_sys
->sock
, 0, SRTO_TSBPDMODE
, &(int) { 1 }, sizeof( int ) );
231 /* This is an access_out so it is always a sender */
232 srt_setsockopt( p_sys
->sock
, 0, SRTO_SENDER
, &(int) { 1 }, sizeof( int ) );
235 srt_setsockopt( p_sys
->sock
, 0, SRTO_TSBPDDELAY
, &p_sys
->i_latency
, sizeof( int ) );
237 p_sys
->i_poll_id
= srt_epoll_create();
238 if ( p_sys
->i_poll_id
== -1 )
240 msg_Err( p_access
, "Failed to create poll id for SRT socket (reason: %s)",
241 srt_getlasterror_str() );
246 if ( vlc_pipe( p_sys
->i_pipe_fds
) != 0 )
248 msg_Err( p_access
, "Failed to create pipe fds." );
251 fcntl( p_sys
->i_pipe_fds
[0], F_SETFL
, O_NONBLOCK
| fcntl( p_sys
->i_pipe_fds
[0], F_GETFL
) );
253 srt_epoll_add_usock( p_sys
->i_poll_id
, p_sys
->sock
, &(int) { SRT_EPOLL_OUT
});
254 srt_setsockopt( p_sys
->sock
, 0, SRTO_SENDER
, &(int) { 1 }, sizeof(int) );
256 srt_epoll_add_ssock( p_sys
->i_poll_id
, p_sys
->i_pipe_fds
[0], &(int) { SRT_EPOLL_IN
} );
258 stat
= srt_connect( p_sys
->sock
, res
->ai_addr
, sizeof (struct sockaddr
));
259 if ( stat
== SRT_ERROR
)
261 msg_Err( p_access
, "Failed to connect to server (reason: %s)",
262 srt_getlasterror_str() );
266 p_access
->pf_write
= Write
;
267 p_access
->pf_control
= Control
;
269 free( psz_dst_addr
);
275 if ( psz_dst_addr
!= NULL
)
276 free( psz_dst_addr
);
281 vlc_close( p_sys
->i_pipe_fds
[0] );
282 vlc_close( p_sys
->i_pipe_fds
[1] );
286 if ( p_sys
->i_poll_id
!= -1 ) srt_epoll_release( p_sys
->i_poll_id
);
287 if ( p_sys
->sock
!= -1 ) srt_close( p_sys
->sock
);
295 static void Close( vlc_object_t
* p_this
)
297 sout_access_out_t
*p_access
= (sout_access_out_t
*)p_this
;
298 sout_access_out_sys_t
*p_sys
= p_access
->p_sys
;
300 srt_epoll_release( p_sys
->i_poll_id
);
301 srt_close( p_sys
->sock
);
303 vlc_close( p_sys
->i_pipe_fds
[0] );
304 vlc_close( p_sys
->i_pipe_fds
[1] );
309 /* Module descriptor */
311 set_shortname( N_("SRT") )
312 set_description( N_("SRT stream output") )
313 set_category( CAT_SOUT
)
314 set_subcategory( SUBCAT_SOUT_ACO
)
316 add_integer( "chunk-size", SRT_DEFAULT_CHUNK_SIZE
,
317 N_("SRT chunk size (bytes)"), NULL
, true )
318 add_integer( "poll-timeout", SRT_DEFAULT_POLL_TIMEOUT
,
319 N_("Return poll wait after timeout miliseconds (-1 = infinite)"), NULL
, true )
320 add_integer( "latency", SRT_DEFAULT_LATENCY
, N_("SRT latency (ms)"), NULL
, true )
322 set_capability( "sout access", 0 )
323 add_shortcut( "srt" )
325 set_callbacks( Open
, Close
)