srt: Fix build failure
[vlc.git] / modules / access / srt.c
blob4f61844a0a20f9769e7ec643eb4f286d1d6acad9
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 *****************************************************************************/
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_access.h>
36 #include <vlc_network.h>
37 #include <vlc_url.h>
39 #include <srt/srt.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
50 struct stream_sys_t
52 SRTSOCKET sock;
53 int i_poll_id;
54 int i_poll_timeout;
55 int i_latency;
56 size_t i_chunk_size;
57 int i_event_fd;
60 static void srt_wait_interrupted(void *p_data)
62 stream_t *p_stream = p_data;
63 stream_sys_t *p_sys = p_stream->p_sys;
64 msg_Dbg( p_stream, "Waking up srt_epoll_wait");
65 if ( write( p_sys->i_event_fd, &( bool ) { true }, sizeof( bool ) ) < 0 )
67 msg_Err( p_stream, "Failed to send data to event fd");
71 static int Control(stream_t *p_stream, int i_query, va_list args)
73 stream_sys_t *p_sys = p_stream->p_sys;
74 int i_ret = VLC_SUCCESS;
76 switch( i_query )
78 case STREAM_CAN_SEEK:
79 case STREAM_CAN_FASTSEEK:
80 case STREAM_CAN_PAUSE:
81 case STREAM_CAN_CONTROL_PACE:
82 *va_arg( args, bool * ) = false;
83 break;
84 case STREAM_GET_PTS_DELAY:
85 *va_arg( args, int64_t * ) = INT64_C(1000)
86 * var_InheritInteger(p_stream, "network-caching");
87 break;
88 default:
89 i_ret = VLC_EGENERIC;
90 break;
93 return i_ret;
96 static block_t *BlockSRT(stream_t *p_stream, bool *restrict eof)
98 stream_sys_t *p_sys = p_stream->p_sys;
100 block_t *pkt = block_Alloc( p_sys->i_chunk_size );
102 if ( unlikely( pkt == NULL ) )
104 return NULL;
107 vlc_interrupt_register( srt_wait_interrupted, p_stream);
109 SRTSOCKET ready[2];
110 struct epoll_event event[1] = { 0 };
112 if ( srt_epoll_wait( p_sys->i_poll_id,
113 ready, &(int) { 2 }, 0, 0, p_sys->i_poll_timeout,
114 &(int) { p_sys->i_event_fd }, &(int) { 1 }, 0, 0 ) == -1 )
116 int srt_err = srt_getlasterror( NULL );
118 /* Assuming that timeout error is normal when SRT socket is connected. */
119 if ( srt_err == SRT_ETIMEOUT && srt_getsockstate( p_sys->sock ) == SRTS_CONNECTED )
121 goto skip;
124 msg_Err( p_stream, "released poll wait (reason : %s)", srt_getlasterror_str() );
125 goto endofstream;
128 if ( event[0].events & EPOLLIN ) {
129 bool cancel = 0;
130 int ret = read( event[0].data.fd, &cancel, sizeof( bool ) );
131 if ( ret < 0 )
133 goto skip;
136 if ( cancel )
138 msg_Dbg( p_stream, "Cancelled running" );
139 goto endofstream;
143 int stat = srt_recvmsg( p_sys->sock, (char *)pkt->p_buffer, p_sys->i_chunk_size );
145 if ( stat == SRT_ERROR )
147 msg_Err( p_stream, "failed to recevie SRT packet (reason: %s)", srt_getlasterror_str() );
148 goto endofstream;
151 pkt->i_buffer = stat;
152 vlc_interrupt_unregister();
153 return pkt;
155 endofstream:
156 msg_Dbg( p_stream, "EOS");
157 *eof = true;
158 skip:
159 block_Release(pkt);
160 srt_clearlasterror();
161 vlc_interrupt_unregister();
163 return NULL;
166 static int Open(vlc_object_t *p_this)
168 stream_t *p_stream = (stream_t*)p_this;
169 stream_sys_t *p_sys = NULL;
170 vlc_url_t parsed_url = { 0 };
171 struct addrinfo hints = {
172 .ai_socktype = SOCK_DGRAM,
173 }, *res = NULL;
174 int stat;
176 p_sys = vlc_obj_alloc( p_this, 1, sizeof( *p_sys ) );
177 if( unlikely( p_sys == NULL ) )
178 return VLC_ENOMEM;
180 if ( vlc_UrlParse( &parsed_url, p_stream->psz_url ) == -1 )
182 msg_Err( p_stream, "Failed to parse a given URL (%s)", p_stream->psz_url );
183 goto failed;
186 p_sys->i_chunk_size = var_InheritInteger( p_stream, "chunk-size" );
187 p_sys->i_poll_timeout = var_InheritInteger( p_stream, "poll-timeout" );
188 p_sys->i_latency = var_InheritInteger( p_stream, "latency" );
189 p_sys->i_poll_id = -1;
190 p_sys->i_event_fd = -1;
191 p_stream->p_sys = p_sys;
192 p_stream->pf_block = BlockSRT;
193 p_stream->pf_control = Control;
195 stat = vlc_getaddrinfo( parsed_url.psz_host, parsed_url.i_port, &hints, &res );
196 if ( stat )
198 msg_Err( p_stream, "Cannot resolve [%s]:%d (reason: %s)",
199 parsed_url.psz_host,
200 parsed_url.i_port,
201 gai_strerror( stat ) );
203 goto failed;
206 p_sys->sock = srt_socket( res->ai_family, SOCK_DGRAM, 0 );
207 if ( p_sys->sock == SRT_ERROR )
209 msg_Err( p_stream, "Failed to open socket." );
210 goto failed;
213 /* Make SRT non-blocking */
214 srt_setsockopt( p_sys->sock, 0, SRTO_SNDSYN, &(bool) { false }, sizeof( bool ) );
216 /* Make sure TSBPD mode is enable (SRT mode) */
217 srt_setsockopt( p_sys->sock, 0, SRTO_TSBPDMODE, &(int) { 1 }, sizeof( int ) );
219 /* Set latency */
220 srt_setsockopt( p_sys->sock, 0, SRTO_TSBPDDELAY, &p_sys->i_latency, sizeof( int ) );
222 p_sys->i_poll_id = srt_epoll_create();
223 if ( p_sys->i_poll_id == -1 )
225 msg_Err( p_stream, "Failed to create poll id for SRT socket." );
226 goto failed;
228 srt_epoll_add_usock( p_sys->i_poll_id, p_sys->sock, &(int) { SRT_EPOLL_IN } );
230 p_sys->i_event_fd = eventfd( 0, EFD_NONBLOCK | EFD_CLOEXEC );
231 srt_epoll_add_ssock( p_sys->i_poll_id, p_sys->i_event_fd, &(int) { EPOLLIN } );
233 stat = srt_connect( p_sys->sock, res->ai_addr, sizeof (struct sockaddr) );
235 if ( stat == SRT_ERROR )
237 msg_Err( p_stream, "Failed to connect to server." );
238 goto failed;
241 vlc_UrlClean( &parsed_url );
242 freeaddrinfo( res );
244 return VLC_SUCCESS;
246 failed:
248 if ( parsed_url.psz_host != NULL
249 && parsed_url.psz_buffer != NULL)
251 vlc_UrlClean( &parsed_url );
254 if ( res != NULL )
256 freeaddrinfo( res );
259 if ( p_sys->i_event_fd != -1 )
261 close( p_sys->i_event_fd );
262 p_sys->i_event_fd = -1;
265 if ( p_sys->i_poll_id != -1 )
267 srt_epoll_release( p_sys->i_poll_id );
268 p_sys->i_poll_id = -1;
270 srt_close( p_sys->sock );
272 return VLC_EGENERIC;
275 static void Close(vlc_object_t *p_this)
277 stream_t *p_stream = (stream_t*)p_this;
278 stream_sys_t *p_sys = p_stream->p_sys;
280 if ( p_sys->i_poll_id != -1 )
282 srt_epoll_release( p_sys->i_poll_id );
283 p_sys->i_poll_id = -1;
285 msg_Dbg( p_stream, "closing server" );
286 srt_close( p_sys->sock );
288 if ( p_sys->i_event_fd != -1 )
290 close( p_sys->i_event_fd );
291 p_sys->i_event_fd = -1;
295 /* Module descriptor */
296 vlc_module_begin ()
297 set_shortname( N_("SRT") )
298 set_description( N_("SRT input") )
299 set_category( CAT_INPUT )
300 set_subcategory( SUBCAT_INPUT_ACCESS )
302 add_integer( "chunk-size", SRT_DEFAULT_CHUNK_SIZE,
303 N_("SRT chunk size (bytes)"), NULL, true )
304 add_integer( "poll-timeout", SRT_DEFAULT_POLL_TIMEOUT,
305 N_("Return poll wait after timeout miliseconds (-1 = infinite)"), NULL, true )
306 add_integer( "latency", SRT_DEFAULT_LATENCY, N_("SRT latency (ms)"), NULL, true )
308 set_capability( "access", 0 )
309 add_shortcut( "srt" )
311 set_callbacks( Open, Close )
312 vlc_module_end ()