1 /*****************************************************************************
2 * udp.c: raw UDP & RTP input module
3 *****************************************************************************
4 * Copyright (C) 2001-2004 VideoLAN
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * Tristan Leteurtre <tooney@via.ecp.fr>
9 * Laurent Aimar <fenrir@via.ecp.fr>
11 * Reviewed: 23 October 2003, Jean-Paul Saman <jpsaman@wxs.nl>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
26 *****************************************************************************/
28 /*****************************************************************************
30 *****************************************************************************/
34 #include <vlc/input.h>
38 /*****************************************************************************
40 *****************************************************************************/
41 #define CACHING_TEXT N_("Caching value in ms")
42 #define CACHING_LONGTEXT N_( \
43 "Allows you to modify the default caching value for UDP streams. This " \
44 "value should be set in millisecond units." )
46 #define AUTO_MTU_TEXT N_("Autodetection of MTU")
47 #define AUTO_MTU_LONGTEXT N_( \
48 "Allows growing the MTU if truncated packets are found" )
50 static int Open ( vlc_object_t * );
51 static void Close( vlc_object_t * );
54 set_description( _("UDP/RTP input") );
56 add_integer( "udp-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT,
57 CACHING_LONGTEXT, VLC_TRUE );
58 add_bool( "udp-auto-mtu", 1, NULL,
59 AUTO_MTU_TEXT, AUTO_MTU_LONGTEXT, VLC_TRUE );
61 set_capability( "access2", 0 );
62 add_shortcut( "udp" );
63 add_shortcut( "udpstream" );
64 add_shortcut( "udp4" );
65 add_shortcut( "udp6" );
66 add_shortcut( "rtp" );
67 add_shortcut( "rtp4" );
68 add_shortcut( "rtp6" );
69 set_callbacks( Open, Close );
72 /*****************************************************************************
74 *****************************************************************************/
75 #define RTP_HEADER_LEN 12
77 static block_t *BlockUDP( access_t * );
78 static block_t *BlockRTP( access_t * );
79 static block_t *BlockChoose( access_t * );
80 static int Control( access_t *, int, va_list );
87 vlc_bool_t b_auto_mtu;
90 /*****************************************************************************
91 * Open: open the socket
92 *****************************************************************************/
93 static int Open( vlc_object_t *p_this )
95 access_t *p_access = (access_t*)p_this;
98 char *psz_name = strdup( p_access->psz_path );
99 char *psz_parser = psz_name;
100 char *psz_server_addr = "";
101 char *psz_server_port = "";
102 char *psz_bind_addr = "";
103 char *psz_bind_port = "";
105 int i_server_port = 0;
108 /* First set ipv4/ipv6 */
109 var_Create( p_access, "ipv4", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
110 var_Create( p_access, "ipv6", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
112 if( *p_access->psz_access )
115 /* Find out which shortcut was used */
116 if( !strncmp( p_access->psz_access, "udp4", 6 ) ||
117 !strncmp( p_access->psz_access, "rtp4", 6 ))
119 val.b_bool = VLC_TRUE;
120 var_Set( p_access, "ipv4", val );
122 val.b_bool = VLC_FALSE;
123 var_Set( p_access, "ipv6", val );
125 else if( !strncmp( p_access->psz_access, "udp6", 6 ) ||
126 !strncmp( p_access->psz_access, "rtp6", 6 ) )
128 val.b_bool = VLC_TRUE;
129 var_Set( p_access, "ipv6", val );
131 val.b_bool = VLC_FALSE;
132 var_Set( p_access, "ipv4", val );
136 /* Parse psz_name syntax :
137 * [serveraddr[:serverport]][@[bindaddr]:[bindport]] */
138 if( *psz_parser && *psz_parser != '@' )
141 psz_server_addr = psz_parser;
143 while( *psz_parser && *psz_parser != ':' && *psz_parser != '@' )
145 if( *psz_parser == '[' )
148 while( *psz_parser && *psz_parser != ']' )
156 if( *psz_parser == ':' )
158 /* Found server port */
159 *psz_parser++ = '\0'; /* Terminate server name */
160 psz_server_port = psz_parser;
162 while( *psz_parser && *psz_parser != '@' )
169 if( *psz_parser == '@' )
171 /* Found bind address or bind port */
172 *psz_parser++ = '\0'; /* Terminate server port or name if necessary */
174 if( *psz_parser && *psz_parser != ':' )
176 /* Found bind address */
177 psz_bind_addr = psz_parser;
179 while( *psz_parser && *psz_parser != ':' )
181 if( *psz_parser == '[' )
184 while( *psz_parser && *psz_parser != ']' )
193 if( *psz_parser == ':' )
195 /* Found bind port */
196 *psz_parser++ = '\0'; /* Terminate bind address if necessary */
197 psz_bind_port = psz_parser;
201 i_server_port = strtol( psz_server_port, NULL, 10 );
202 if( ( i_bind_port = strtol( psz_bind_port, NULL, 10 ) ) == 0 )
204 i_bind_port = var_CreateGetInteger( p_access, "server-port" );
207 msg_Dbg( p_access, "opening server=%s:%d local=%s:%d",
208 psz_server_addr, i_server_port, psz_bind_addr, i_bind_port );
210 /* Set up p_access */
211 p_access->pf_read = NULL;
212 p_access->pf_block = BlockChoose;
213 p_access->pf_control = Control;
214 p_access->pf_seek = NULL;
215 p_access->info.i_update = 0;
216 p_access->info.i_size = 0;
217 p_access->info.i_pos = 0;
218 p_access->info.b_eof = VLC_FALSE;
219 p_access->info.i_title = 0;
220 p_access->info.i_seekpoint = 0;
222 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
223 p_sys->fd = net_OpenUDP( p_access, psz_bind_addr, i_bind_port,
224 psz_server_addr, i_server_port );
227 msg_Err( p_access, "cannot open socket" );
235 p_sys->i_mtu = var_CreateGetInteger( p_access, "mtu" );
236 if( p_sys->i_mtu <= 1 )
237 p_sys->i_mtu = 1500; /* Avoid problem */
239 p_sys->b_auto_mtu = var_CreateGetBool( p_access, "udp-auto-mtu" );;
241 /* Update default_pts to a suitable value for udp access */
242 var_Create( p_access, "udp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
247 /*****************************************************************************
248 * Close: free unused data structures
249 *****************************************************************************/
250 static void Close( vlc_object_t *p_this )
252 access_t *p_access = (access_t*)p_this;
253 access_sys_t *p_sys = p_access->p_sys;
255 net_Close( p_sys->fd );
259 /*****************************************************************************
261 *****************************************************************************/
262 static int Control( access_t *p_access, int i_query, va_list args )
264 access_sys_t *p_sys = p_access->p_sys;
272 case ACCESS_CAN_SEEK:
273 case ACCESS_CAN_FASTSEEK:
274 case ACCESS_CAN_PAUSE:
275 case ACCESS_CAN_CONTROL_PACE:
276 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
277 *pb_bool = VLC_FALSE;
281 pi_int = (int*)va_arg( args, int * );
282 *pi_int = p_sys->i_mtu;
285 case ACCESS_GET_PTS_DELAY:
286 pi_64 = (int64_t*)va_arg( args, int64_t * );
287 *pi_64 = var_GetInteger( p_access, "udp-caching" ) * 1000;
291 case ACCESS_SET_PAUSE_STATE:
292 case ACCESS_GET_TITLE_INFO:
293 case ACCESS_SET_TITLE:
294 case ACCESS_SET_SEEKPOINT:
295 case ACCESS_SET_PRIVATE_ID_STATE:
299 msg_Warn( p_access, "unimplemented query in control" );
306 /*****************************************************************************
308 *****************************************************************************/
309 static block_t *BlockUDP( access_t *p_access )
311 access_sys_t *p_sys = p_access->p_sys;
315 p_block = block_New( p_access, p_sys->i_mtu );
316 p_block->i_buffer = net_Read( p_access, p_sys->fd, NULL,
317 p_block->p_buffer, p_sys->i_mtu,
319 if( p_block->i_buffer <= 0 )
321 block_Release( p_block );
325 if( p_block->i_buffer >= p_sys->i_mtu && p_sys->b_auto_mtu &&
326 p_sys->i_mtu < 32767 )
328 /* Increase by 100% */
330 msg_Dbg( p_access, "increasing MTU to %d", p_sys->i_mtu );
336 /*****************************************************************************
337 * BlockParseRTP/BlockRTP:
338 *****************************************************************************/
339 static block_t *BlockParseRTP( access_t *p_access, block_t *p_block )
346 if( p_block->i_buffer < RTP_HEADER_LEN )
349 /* Parse the header and make some verifications.
350 * See RFC 1889 & RFC 2250. */
351 i_rtp_version = ( p_block->p_buffer[0] & 0xC0 ) >> 6;
352 i_CSRC_count = ( p_block->p_buffer[0] & 0x0F );
353 i_payload_type = ( p_block->p_buffer[1] & 0x7F );
355 if ( i_rtp_version != 2 )
356 msg_Dbg( p_access, "RTP version is %u, should be 2", i_rtp_version );
358 if( i_payload_type == 14 )
360 else if( i_payload_type != 33 && i_payload_type != 32 )
361 msg_Dbg( p_access, "unsupported RTP payload type (%u)", i_payload_type );
363 i_skip += RTP_HEADER_LEN + 4*i_CSRC_count;
365 /* A CSRC extension field is 32 bits in size (4 bytes) */
366 if( i_skip >= p_block->i_buffer )
369 /* Return the packet without the RTP header. */
370 p_block->i_buffer -= i_skip;
371 p_block->p_buffer += i_skip;
376 msg_Warn( p_access, "received a too short packet for RTP" );
377 block_Release( p_block );
381 static block_t *BlockRTP( access_t *p_access )
383 return BlockParseRTP( p_access, BlockUDP( p_access ) );
386 /*****************************************************************************
387 * BlockChoose: decide between RTP and UDP
388 *****************************************************************************/
389 static block_t *BlockChoose( access_t *p_access )
396 if( ( p_block = BlockUDP( p_access ) ) == NULL )
399 if( p_block->p_buffer[0] == 0x47 )
401 msg_Dbg( p_access, "detected TS over raw UDP" );
402 p_access->pf_block = BlockUDP;
406 if( p_block->i_buffer < RTP_HEADER_LEN )
409 /* Parse the header and make some verifications.
410 * See RFC 1889 & RFC 2250. */
412 i_rtp_version = ( p_block->p_buffer[0] & 0xC0 ) >> 6;
413 i_CSRC_count = ( p_block->p_buffer[0] & 0x0F );
414 i_payload_type = ( p_block->p_buffer[1] & 0x7F );
416 if( i_rtp_version != 2 )
418 msg_Dbg( p_access, "no supported RTP header detected" );
419 p_access->pf_block = BlockUDP;
423 switch( i_payload_type )
426 msg_Dbg( p_access, "detected TS over RTP" );
427 p_access->psz_demux = strdup( "ts" );
431 msg_Dbg( p_access, "detected MPEG audio over RTP" );
432 p_access->psz_demux = strdup( "mpga" );
436 msg_Dbg( p_access, "detected MPEG video over RTP" );
437 p_access->psz_demux = strdup( "mpgv" );
441 msg_Dbg( p_access, "no RTP header detected" );
442 p_access->pf_block = BlockUDP;
446 p_access->pf_block = BlockRTP;
448 return BlockParseRTP( p_access, p_block );