1 /*****************************************************************************
2 * udp.c: raw UDP & RTP input module
3 *****************************************************************************
4 * Copyright (C) 2001-2005 the VideoLAN team
5 * Copyright (C) 2007 Remi Denis-Courmont
8 * Authors: Christophe Massiot <massiot@via.ecp.fr>
9 * Tristan Leteurtre <tooney@via.ecp.fr>
10 * Laurent Aimar <fenrir@via.ecp.fr>
11 * Jean-Paul Saman <jpsaman #_at_# m2x dot nl>
14 * Reviewed: 23 October 2003, Jean-Paul Saman <jpsaman _at_ videolan _dot_ org>
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
29 *****************************************************************************/
31 /*****************************************************************************
33 *****************************************************************************/
37 #include <vlc_access.h>
38 #include <vlc_network.h>
40 #if defined (HAVE_NETINET_UDPLITE_H)
41 # include <netinet/udplite.h>
42 #elif defined (__linux__)
43 # define UDPLITE_SEND_CSCOV 10
44 # define UDPLITE_RECV_CSCOV 11
47 #ifndef IPPROTO_UDPLITE
48 # define IPPROTO_UDPLITE 136 /* from IANA */
51 # define SOL_UDPLITE IPPROTO_UDPLITE
55 /*****************************************************************************
57 *****************************************************************************/
58 #define CACHING_TEXT N_("Caching value in ms")
59 #define CACHING_LONGTEXT N_( \
60 "Caching value for UDP streams. This " \
61 "value should be set in milliseconds." )
63 #define AUTO_MTU_TEXT N_("Autodetection of MTU")
64 #define AUTO_MTU_LONGTEXT N_( \
65 "Automatically detect the line's MTU. This will increase the size if" \
66 " truncated packets are found" )
68 #define RTP_LATE_TEXT N_("RTP reordering timeout in ms")
69 #define RTP_LATE_LONGTEXT N_( \
70 "VLC reorders RTP packets. The input will wait for late packets at most "\
71 "the time specified here (in milliseconds)." )
73 static int Open ( vlc_object_t * );
74 static void Close( vlc_object_t * );
77 set_shortname( _("UDP/RTP" ) );
78 set_description( _("UDP/RTP input") );
79 set_category( CAT_INPUT );
80 set_subcategory( SUBCAT_INPUT_ACCESS );
82 add_integer( "udp-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT,
83 CACHING_LONGTEXT, VLC_TRUE );
84 add_integer( "rtp-late", 100, NULL, RTP_LATE_TEXT, RTP_LATE_LONGTEXT, VLC_TRUE );
86 add_bool( "udp-auto-mtu", 1, NULL,
87 AUTO_MTU_TEXT, AUTO_MTU_LONGTEXT, VLC_TRUE );
89 set_capability( "access2", 0 );
90 add_shortcut( "udp" );
91 add_shortcut( "udpstream" );
92 add_shortcut( "udp4" );
93 add_shortcut( "udp6" );
94 add_shortcut( "rtp" );
95 add_shortcut( "rtp4" );
96 add_shortcut( "rtp6" );
97 add_shortcut( "udplite" );
98 add_shortcut( "rtptcp" );
100 set_callbacks( Open, Close );
103 /*****************************************************************************
105 *****************************************************************************/
106 #define RTP_HEADER_LEN 12
108 static block_t *BlockUDP( access_t * );
109 static block_t *BlockTCP( access_t * );
110 static block_t *BlockRTP( access_t * );
111 static block_t *BlockChoose( access_t * );
112 static int Control( access_t *, int, va_list );
119 vlc_bool_t b_auto_mtu;
120 vlc_bool_t b_framed_rtp;
122 /* reorder rtp packets when out-of-sequence */
123 uint16_t i_last_seqno;
127 block_t *p_partial_frame; /* Partial Framed RTP packet */
130 /*****************************************************************************
131 * Open: open the socket
132 *****************************************************************************/
133 static int Open( vlc_object_t *p_this )
135 access_t *p_access = (access_t*)p_this;
138 char *psz_name = strdup( p_access->psz_path );
140 const char *psz_server_addr, *psz_bind_addr = "";
141 int i_bind_port, i_server_port = 0;
142 int fam = AF_UNSPEC, proto = IPPROTO_UDP;
143 vlc_bool_t b_framed = VLC_FALSE;
145 if (strlen (p_access->psz_access) >= 3)
147 switch (p_access->psz_access[3])
157 if (strcmp (p_access->psz_access + 3, "lite") == 0)
158 proto = IPPROTO_UDPLITE;
159 if (strcmp (p_access->psz_access + 3, "tcp") == 0)
166 i_bind_port = var_CreateGetInteger( p_access, "server-port" );
168 /* Parse psz_name syntax :
169 * [serveraddr[:serverport]][@[bindaddr]:[bindport]] */
170 psz_parser = strchr( psz_name, '@' );
171 if( psz_parser != NULL )
173 /* Found bind address and/or bind port */
174 *psz_parser++ = '\0';
175 psz_bind_addr = psz_parser;
177 if( *psz_parser == '[' )
178 /* skips bracket'd IPv6 address */
179 psz_parser = strchr( psz_parser, ']' );
181 if( psz_parser != NULL )
183 psz_parser = strchr( psz_parser, ':' );
184 if( psz_parser != NULL )
186 *psz_parser++ = '\0';
187 i_bind_port = atoi( psz_parser );
192 psz_server_addr = psz_name;
193 if( *psz_server_addr == '[' )
194 /* skips bracket'd IPv6 address */
195 psz_parser = strchr( psz_name, ']' );
197 if( psz_parser != NULL )
199 psz_parser = strchr( psz_parser, ':' );
200 if( psz_parser != NULL )
202 *psz_parser++ = '\0';
203 i_server_port = atoi( psz_parser );
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 access_InitFields( p_access );
212 ACCESS_SET_CALLBACKS( NULL, BlockChoose, Control, NULL );
213 p_access->info.b_prebuffered = VLC_FALSE;
214 MALLOC_ERR( p_access->p_sys, access_sys_t ); p_sys = p_access->p_sys;
217 ? net_ConnectTCP( p_access, psz_server_addr, i_server_port )
218 : net_OpenDgram( p_access, psz_bind_addr, i_bind_port,
219 psz_server_addr, i_server_port, fam, proto );
221 if( p_sys->fd == -1 )
223 msg_Err( p_access, "cannot open socket" );
228 net_StopSend( p_sys->fd );
230 #ifdef UDPLITE_RECV_CSCOV
231 if (proto == IPPROTO_UDPLITE)
232 /* UDP header: 8 bytes + RTP header: 12 bytes (or more) */
233 setsockopt (p_sys->fd, SOL_UDPLITE, UDPLITE_RECV_CSCOV,
234 &(int){ 20 }, sizeof (int));
237 p_sys->b_framed_rtp = b_framed;
240 /* We don't do autodetection and prebuffering in case of framing */
241 p_access->pf_block = BlockRTP;
242 p_sys->i_mtu = 65535;
247 p_sys->i_mtu = var_CreateGetInteger( p_access, "mtu" );
248 if( p_sys->i_mtu <= 1 )
249 p_sys->i_mtu = 1500; /* Avoid problem */
251 p_sys->b_auto_mtu = var_CreateGetBool( p_access, "udp-auto-mtu" );;
254 /* Update default_pts to a suitable value for udp access */
255 var_Create( p_access, "udp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
257 /* RTP reordering for out-of-sequence packets */
258 p_sys->i_rtp_late = var_CreateGetInteger( p_access, "rtp-late" ) * 1000;
259 p_sys->i_last_seqno = 0;
260 p_sys->p_list = NULL;
265 /*****************************************************************************
266 * Close: free unused data structures
267 *****************************************************************************/
268 static void Close( vlc_object_t *p_this )
270 access_t *p_access = (access_t*)p_this;
271 access_sys_t *p_sys = p_access->p_sys;
273 block_ChainRelease( p_sys->p_list );
274 net_Close( p_sys->fd );
278 /*****************************************************************************
280 *****************************************************************************/
281 static int Control( access_t *p_access, int i_query, va_list args )
283 access_sys_t *p_sys = p_access->p_sys;
291 case ACCESS_CAN_SEEK:
292 case ACCESS_CAN_FASTSEEK:
293 case ACCESS_CAN_PAUSE:
294 case ACCESS_CAN_CONTROL_PACE:
295 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
296 *pb_bool = VLC_FALSE;
300 pi_int = (int*)va_arg( args, int * );
301 *pi_int = p_sys->i_mtu;
304 case ACCESS_GET_PTS_DELAY:
305 pi_64 = (int64_t*)va_arg( args, int64_t * );
306 *pi_64 = var_GetInteger( p_access, "udp-caching" ) * 1000;
310 case ACCESS_SET_PAUSE_STATE:
311 case ACCESS_GET_TITLE_INFO:
312 case ACCESS_SET_TITLE:
313 case ACCESS_SET_SEEKPOINT:
314 case ACCESS_SET_PRIVATE_ID_STATE:
318 msg_Warn( p_access, "unimplemented query in control" );
325 /*****************************************************************************
327 *****************************************************************************/
328 static block_t *BlockUDP( access_t *p_access )
330 access_sys_t *p_sys = p_access->p_sys;
334 p_block = block_New( p_access, p_sys->i_mtu );
335 p_block->i_buffer = net_Read( p_access, p_sys->fd, NULL,
336 p_block->p_buffer, p_sys->i_mtu,
338 if( p_block->i_buffer <= 0 )
340 block_Release( p_block );
344 if( (p_block->i_buffer >= p_sys->i_mtu) && p_sys->b_auto_mtu &&
345 p_sys->i_mtu < 32767 )
347 /* Increase by 100% */
349 msg_Dbg( p_access, "increasing MTU to %d", p_sys->i_mtu );
355 /*****************************************************************************
356 * BlockTCP: Framed RTP/AVP packet reception for COMEDIA
357 * Still an I-D (draft-ietf-avt-rtp-framing-contrans-06) - subject to change.
358 *****************************************************************************/
359 static block_t *BlockTCP( access_t *p_access )
361 access_sys_t *p_sys = p_access->p_sys;
362 block_t *p_block = p_sys->p_partial_frame;
365 if( p_block == NULL )
367 /* MTU should always be 65535 in this case */
368 p_sys->p_partial_frame = p_block = block_New( p_access, 65537 );
373 /* Read RTP framing */
374 if (p_block->i_buffer < 2)
376 /* FIXME: not very efficient */
377 i_read = net_Read( p_access, p_sys->fd, NULL,
378 p_block->p_buffer + p_block->i_buffer,
379 2 - p_block->i_buffer, VLC_FALSE );
383 p_block->i_buffer += i_read;
384 if (p_block->i_buffer < 2)
388 uint16_t framelen = GetWLE( p_block->p_buffer );
392 i_read = net_Read( p_access, p_sys->fd, NULL,
393 p_block->p_buffer + p_block->i_buffer,
394 2 + framelen - p_block->i_buffer, VLC_FALSE );
398 p_block->i_buffer += i_read;
401 if( p_block->i_buffer < (2 + framelen) )
402 return NULL; // incomplete frame
404 /* Hide framing from RTP layer */
405 p_block->p_buffer += 2;
406 p_block->i_buffer -= 2;
407 p_sys->p_partial_frame = NULL;
411 block_Release( p_block );
412 p_sys->p_partial_frame = NULL;
418 * rtp_ChainInsert - insert a p_block in the chain and
419 * look at the sequence numbers.
421 static inline vlc_bool_t rtp_ChainInsert( access_t *p_access, block_t *p_block )
423 access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
424 block_t *p_prev = NULL;
425 block_t *p = p_sys->p_end;
426 uint16_t i_new = (uint16_t) p_block->i_dts;
431 p_sys->p_list = p_block;
432 p_sys->p_end = p_block;
435 /* walk through the queue from top down since the new packet is in
436 most cases just appended to the end */
440 i_tmp = i_new - (uint16_t) p->i_dts;
442 if( !i_tmp ) /* trash duplicate */
446 { /* insert after this block ( i_new > p->i_dts ) */
447 p_block->p_next = p->p_next;
452 p_prev->p_prev = p_block;
453 msg_Dbg(p_access, "RTP reordering: insert after %d, new %d",
454 (uint16_t) p->i_dts, i_new );
458 p_sys->p_end = p_block;
462 if( p == p_sys->p_list )
463 { /* we've reached bottom of chain */
464 i_tmp = p_sys->i_last_seqno - i_new;
465 if( !p_access->info.b_prebuffered || (i_tmp > 32767) )
467 msg_Dbg(p_access, "RTP reordering: prepend %d before %d",
468 i_new, (uint16_t) p->i_dts );
471 p_sys->p_list = p_block;
475 if( !i_tmp ) /* trash duplicate */
478 /* reordering failed - append the packet to the end of queue */
479 msg_Dbg(p_access, "RTP: sequence changed (or buffer too small) "
480 "new: %d, buffer %d...%d", i_new, (uint16_t) p->i_dts,
481 (uint16_t) p_sys->p_end->i_dts);
482 p_sys->p_end->p_next = p_block;
483 p_block->p_prev = p_sys->p_end;
484 p_sys->p_end = p_block;
490 block_Release( p_block );
494 /*****************************************************************************
495 * BlockParseRTP/BlockRTP:
496 *****************************************************************************/
497 static block_t *BlockParseRTP( access_t *p_access, block_t *p_block )
503 int i_extension_flag = 0;
504 int i_extension_length = 0;
505 uint16_t i_sequence_number = 0;
507 if( p_block == NULL )
510 if( p_block->i_buffer < RTP_HEADER_LEN )
513 /* Parse the header and make some verifications.
515 i_rtp_version = ( p_block->p_buffer[0] & 0xC0 ) >> 6;
516 i_CSRC_count = p_block->p_buffer[0] & 0x0F;
517 i_extension_flag = p_block->p_buffer[0] & 0x10;
518 i_payload_type = p_block->p_buffer[1] & 0x7F;
519 i_sequence_number = (p_block->p_buffer[2] << 8 ) + p_block->p_buffer[3];
521 if( i_rtp_version != 2 )
522 msg_Dbg( p_access, "RTP version is %u, should be 2", i_rtp_version );
524 if( i_payload_type == 14 || i_payload_type == 32)
526 else if( i_payload_type != 33 )
527 msg_Dbg( p_access, "unsupported RTP payload type (%u)", i_payload_type );
528 if( i_extension_flag )
530 if( p_block->i_buffer < 16 )
532 i_extension_length = 4 +
533 4 * ( (p_block->p_buffer[14] << 8) + p_block->p_buffer[15] );
536 /* Skip header + CSRC extension field n*(32 bits) + extension */
537 i_skip += RTP_HEADER_LEN + 4*i_CSRC_count + i_extension_length;
539 if( i_skip >= p_block->i_buffer )
542 /* Return the packet without the RTP header, remember seqno in i_dts */
543 p_block->i_buffer -= i_skip;
544 p_block->p_buffer += i_skip;
545 p_block->i_pts = mdate();
546 p_block->i_dts = (mtime_t) i_sequence_number;
549 /* Emulate packet loss */
550 if ( (i_sequence_number % 4000) == 0)
552 msg_Warn( p_access, "Emulating packet drop" );
553 block_Release( p_block );
561 msg_Warn( p_access, "received a too short packet for RTP" );
562 block_Release( p_block );
566 static block_t *BlockPrebufferRTP( access_t *p_access, block_t *p_block )
568 access_sys_t *p_sys = p_access->p_sys;
569 mtime_t i_first = mdate();
571 block_t *p = p_block;
575 mtime_t i_date = mdate();
577 if( p && rtp_ChainInsert( p_access, p ))
580 /* Require at least 2 packets in the buffer */
581 if( i_count > 2 && (i_date - i_first) > p_sys->i_rtp_late )
584 p = BlockParseRTP( p_access, BlockUDP( p_access ) );
585 if( !p && (i_date - i_first) > p_sys->i_rtp_late )
587 msg_Err( p_access, "error in RTP prebuffering!" );
592 msg_Dbg( p_access, "RTP: prebuffered %d packets", i_count - 1 );
593 p_access->info.b_prebuffered = VLC_TRUE;
595 p_sys->p_list = p_sys->p_list->p_next;
596 p_sys->i_last_seqno = (uint16_t) p->i_dts;
601 static block_t *BlockRTP( access_t *p_access )
603 access_sys_t *p_sys = p_access->p_sys;
606 while ( !p_sys->p_list ||
607 ( mdate() - p_sys->p_list->i_pts ) < p_sys->i_rtp_late )
609 p = BlockParseRTP( p_access,
610 p_sys->b_framed_rtp ? BlockTCP( p_access )
611 : BlockUDP( p_access ) );
615 rtp_ChainInsert( p_access, p );
619 p_sys->p_list = p_sys->p_list->p_next;
620 p_sys->i_last_seqno++;
621 if( p_sys->i_last_seqno != (uint16_t) p->i_dts )
623 msg_Dbg( p_access, "RTP: packet(s) lost, expected %d, got %d",
624 p_sys->i_last_seqno, (uint16_t) p->i_dts );
625 p_sys->i_last_seqno = (uint16_t) p->i_dts;
631 /*****************************************************************************
632 * BlockChoose: decide between RTP and UDP
633 *****************************************************************************/
634 static block_t *BlockChoose( access_t *p_access )
641 if( ( p_block = BlockUDP( p_access ) ) == NULL )
644 if( p_block->p_buffer[0] == 0x47 )
646 msg_Dbg( p_access, "detected TS over raw UDP" );
647 p_access->pf_block = BlockUDP;
648 p_access->info.b_prebuffered = VLC_TRUE;
652 if( p_block->i_buffer < RTP_HEADER_LEN )
655 /* Parse the header and make some verifications.
658 i_rtp_version = ( p_block->p_buffer[0] & 0xC0 ) >> 6;
659 i_CSRC_count = ( p_block->p_buffer[0] & 0x0F );
660 i_payload_type = ( p_block->p_buffer[1] & 0x7F );
662 if( i_rtp_version != 2 )
664 msg_Dbg( p_access, "no supported RTP header detected" );
665 p_access->pf_block = BlockUDP;
666 p_access->info.b_prebuffered = VLC_TRUE;
670 switch( i_payload_type )
673 msg_Dbg( p_access, "detected TS over RTP" );
674 p_access->psz_demux = strdup( "ts" );
678 msg_Dbg( p_access, "detected MPEG audio over RTP" );
679 p_access->psz_demux = strdup( "mpga" );
683 msg_Dbg( p_access, "detected MPEG video over RTP" );
684 p_access->psz_demux = strdup( "mpgv" );
688 msg_Dbg( p_access, "no RTP header detected" );
689 p_access->pf_block = BlockUDP;
690 p_access->info.b_prebuffered = VLC_TRUE;
694 if( !BlockParseRTP( p_access, p_block )) return NULL;
696 p_access->pf_block = BlockRTP;
698 return BlockPrebufferRTP( p_access, p_block );