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 sout_access_out_sys_t
*p_sys
= p_access
->p_sys
;
146 int i_ret
= VLC_SUCCESS
;
150 case ACCESS_OUT_CONTROLS_PACE
:
151 *va_arg( args
, bool * ) = false;
155 i_ret
= VLC_EGENERIC
;
162 static int Open( vlc_object_t
*p_this
)
164 sout_access_out_t
*p_access
= (sout_access_out_t
*)p_this
;
165 sout_access_out_sys_t
*p_sys
= NULL
;
167 char *psz_dst_addr
= NULL
;
171 struct addrinfo hints
= {
172 .ai_socktype
= SOCK_DGRAM
,
175 if (var_Create ( p_access
, "dst-port", VLC_VAR_INTEGER
)
176 || var_Create ( p_access
, "src-port", VLC_VAR_INTEGER
)
177 || var_Create ( p_access
, "dst-addr", VLC_VAR_STRING
)
178 || var_Create ( p_access
, "src-addr", VLC_VAR_STRING
) )
180 msg_Err( p_access
, "Valid network information is required." );
184 if( !( p_sys
= malloc ( sizeof( *p_sys
) ) ) )
187 p_sys
->i_chunk_size
= var_InheritInteger( p_access
, "chunk-size" );
188 p_sys
->i_poll_timeout
= var_InheritInteger( p_access
, "poll-timeout" );
189 p_sys
->i_latency
= var_InheritInteger( p_access
, "latency" );
190 p_sys
->i_poll_id
= -1;
191 p_sys
->i_event_fd
= -1;
193 p_access
->p_sys
= p_sys
;
195 i_dst_port
= SRT_DEFAULT_PORT
;
196 char *psz_parser
= psz_dst_addr
= strdup( p_access
->psz_path
);
203 if (psz_parser
[0] == '[')
204 psz_parser
= strchr (psz_parser
, ']');
206 psz_parser
= strchr (psz_parser
? psz_parser
: psz_dst_addr
, ':');
207 if (psz_parser
!= NULL
)
209 *psz_parser
++ = '\0';
210 i_dst_port
= atoi (psz_parser
);
213 msg_Dbg( p_access
, "Setting SRT socket (dest addresss: %s, port: %d).",
214 psz_dst_addr
, i_dst_port
);
216 stat
= vlc_getaddrinfo( psz_dst_addr
, i_dst_port
, &hints
, &res
);
219 msg_Err( p_access
, "Cannot resolve [%s]:%d (reason: %s)",
222 gai_strerror( stat
) );
227 p_sys
->sock
= srt_socket( res
->ai_family
, SOCK_DGRAM
, 0 );
228 if ( p_sys
->sock
== SRT_ERROR
)
230 msg_Err( p_access
, "Failed to open socket." );
234 /* Make SRT non-blocking */
235 srt_setsockopt( p_sys
->sock
, 0, SRTO_SNDSYN
, &(bool) { false }, sizeof( bool ) );
237 /* Make sure TSBPD mode is enable (SRT mode) */
238 srt_setsockopt( p_sys
->sock
, 0, SRTO_TSBPDMODE
, &(int) { 1 }, sizeof( int ) );
240 /* This is an access_out so it is always a sender */
241 srt_setsockopt( p_sys
->sock
, 0, SRTO_SENDER
, &(int) { 1 }, sizeof( int ) );
244 srt_setsockopt( p_sys
->sock
, 0, SRTO_TSBPDDELAY
, &p_sys
->i_latency
, sizeof( int ) );
246 p_sys
->i_poll_id
= srt_epoll_create();
247 if ( p_sys
->i_poll_id
== -1 )
249 msg_Err( p_access
, "Failed to create poll id for SRT socket (reason: %s)",
250 srt_getlasterror_str() );
255 srt_epoll_add_usock( p_sys
->i_poll_id
, p_sys
->sock
, &(int) { SRT_EPOLL_OUT
});
256 srt_setsockopt( p_sys
->sock
, 0, SRTO_SENDER
, &(int) { 1 }, sizeof(int) );
258 p_sys
->i_event_fd
= eventfd( 0, EFD_NONBLOCK
| EFD_CLOEXEC
);
259 srt_epoll_add_ssock( p_sys
->i_poll_id
, p_sys
->i_event_fd
, &(int) { EPOLLIN
} );
261 stat
= srt_connect( p_sys
->sock
, res
->ai_addr
, sizeof (struct sockaddr
));
262 if ( stat
== SRT_ERROR
)
264 msg_Err( p_access
, "Failed to connect to server (reason: %s)",
265 srt_getlasterror_str() );
269 p_access
->pf_write
= Write
;
270 p_access
->pf_control
= Control
;
272 free( psz_dst_addr
);
278 if ( psz_dst_addr
!= NULL
)
279 free( psz_dst_addr
);
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
);
288 if ( p_sys
->i_event_fd
!= -1 ) close( p_sys
->i_event_fd
);
296 static void Close( vlc_object_t
* p_this
)
298 sout_access_out_t
*p_access
= (sout_access_out_t
*)p_this
;
299 sout_access_out_sys_t
*p_sys
= p_access
->p_sys
;
301 srt_epoll_release( p_sys
->i_poll_id
);
302 srt_close( p_sys
->sock
);
304 if ( p_sys
->i_event_fd
!= -1 )
306 close( p_sys
->i_event_fd
);
307 p_sys
->i_event_fd
= -1;
313 /* Module descriptor */
315 set_shortname( N_("SRT") )
316 set_description( N_("SRT stream output") )
317 set_category( CAT_SOUT
)
318 set_subcategory( SUBCAT_SOUT_ACO
)
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 miliseconds (-1 = infinite)"), NULL
, true )
324 add_integer( "latency", SRT_DEFAULT_LATENCY
, N_("SRT latency (ms)"), NULL
, true )
326 set_capability( "sout access", 0 )
327 add_shortcut( "srt" )
329 set_callbacks( Open
, Close
)