srt: Fix build failure
[vlc.git] / modules / access_output / srt.c
blob7de9b9151a2705b2c54bdbda62778e978518dff6
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 *****************************************************************************/
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
27 #include <errno.h>
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>
34 #include <vlc_sout.h>
35 #include <vlc_block.h>
36 #include <vlc_network.h>
38 #include <srt/srt.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
53 SRTSOCKET sock;
54 int i_poll_id;
55 int i_poll_timeout;
56 int i_latency;
57 size_t i_chunk_size;
58 int i_event_fd;
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;
75 int i_len = 0;
77 vlc_interrupt_register( srt_wait_interrupted, p_access);
79 while( p_buffer )
81 block_t *p_next;
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 );
88 SRTSOCKET ready[2];
90 struct epoll_event event[1] = { 0 };
91 retry:
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();
101 goto retry;
104 i_len = VLC_EGENERIC;
105 goto out;
108 if ( event[0].events & EPOLLIN )
110 bool cancel = 0;
111 int ret = read( event[0].data.fd, &cancel, sizeof( bool ) );
112 if ( ret < 0 )
114 goto retry;
117 if ( cancel )
119 msg_Dbg( p_access, "Cancelled running" );
120 i_len = 0;
121 goto out;
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 );
134 p_buffer = p_next;
137 out:
138 vlc_interrupt_unregister();
139 if ( i_len <= 0 ) block_ChainRelease( p_buffer );
140 return i_len;
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;
148 switch( i_query )
150 case ACCESS_OUT_CONTROLS_PACE:
151 *va_arg( args, bool * ) = false;
152 break;
154 default:
155 i_ret = VLC_EGENERIC;
156 break;
159 return i_ret;
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;
168 int i_dst_port;
169 int stat;
171 struct addrinfo hints = {
172 .ai_socktype = SOCK_DGRAM,
173 }, *res = NULL;
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." );
181 return VLC_ENOMEM;
184 if( !( p_sys = malloc ( sizeof( *p_sys ) ) ) )
185 return VLC_ENOMEM;
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 );
197 if( !psz_dst_addr )
199 free( p_sys );
200 return VLC_ENOMEM;
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 );
217 if ( stat )
219 msg_Err( p_access, "Cannot resolve [%s]:%d (reason: %s)",
220 psz_dst_addr,
221 i_dst_port,
222 gai_strerror( stat ) );
224 goto failed;
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." );
231 goto failed;
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 ) );
243 /* Set latency */
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() );
252 goto failed;
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() );
266 goto failed;
269 p_access->pf_write = Write;
270 p_access->pf_control = Control;
272 free( psz_dst_addr );
273 freeaddrinfo( res );
275 return VLC_SUCCESS;
277 failed:
278 if ( psz_dst_addr != NULL)
279 free( psz_dst_addr );
281 if ( res != NULL )
282 freeaddrinfo( res );
284 if ( p_sys != NULL )
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 );
290 free( p_sys );
293 return VLC_EGENERIC;
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;
310 free( p_sys );
313 /* Module descriptor */
314 vlc_module_begin()
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 )
330 vlc_module_end ()