1 /*****************************************************************************
2 * udp.c: raw UDP & RTP input module
3 *****************************************************************************
4 * Copyright (C) 2001-2005 the VideoLAN team
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * Tristan Leteurtre <tooney@via.ecp.fr>
9 * Laurent Aimar <fenrir@via.ecp.fr>
10 * Jean-Paul Saman <jpsaman #_at_# m2x dot nl>
12 * Reviewed: 23 October 2003, Jean-Paul Saman <jpsaman@wxs.nl>
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
27 *****************************************************************************/
29 /*****************************************************************************
31 *****************************************************************************/
35 #include <vlc/input.h>
39 /*****************************************************************************
41 *****************************************************************************/
42 #define CACHING_TEXT N_("Caching value in ms")
43 #define CACHING_LONGTEXT N_( \
44 "Allows you to modify the default caching value for UDP streams. This " \
45 "value should be set in millisecond units." )
47 #define AUTO_MTU_TEXT N_("Autodetection of MTU")
48 #define AUTO_MTU_LONGTEXT N_( \
49 "Allows growing the MTU if truncated packets are found" )
51 #define RTP_LATE_TEXT N_("Reorder timeout in ms for late RTP packets")
52 #define RTP_LATE_LONGTEXT N_( \
53 "Allows you to modify the RTP packets reorder and late behaviour. " \
54 "If enabled (value>0) then out-of-order packets will be held for the " \
55 "specified timeout in ms. " \
56 "The default behaviour is not to reorder." )
58 static int Open ( vlc_object_t * );
59 static void Close( vlc_object_t * );
62 set_shortname( _("UDP/RTP" ) );
63 set_description( _("UDP/RTP input") );
64 set_category( CAT_INPUT );
65 set_subcategory( SUBCAT_INPUT_ACCESS );
67 add_integer( "udp-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT,
68 CACHING_LONGTEXT, VLC_TRUE );
69 add_bool( "udp-auto-mtu", 1, NULL,
70 AUTO_MTU_TEXT, AUTO_MTU_LONGTEXT, VLC_TRUE );
72 add_integer( "rtp-late", 0, NULL, RTP_LATE_TEXT, RTP_LATE_LONGTEXT, VLC_TRUE );
74 set_capability( "access2", 0 );
75 add_shortcut( "udp" );
76 add_shortcut( "udpstream" );
77 add_shortcut( "udp4" );
78 add_shortcut( "udp6" );
79 add_shortcut( "rtp" );
80 add_shortcut( "rtp4" );
81 add_shortcut( "rtp6" );
82 set_callbacks( Open, Close );
85 /*****************************************************************************
87 *****************************************************************************/
88 #define RTP_HEADER_LEN 12
89 #define RTP_SEQ_NUM_SIZE 65536
91 static block_t *BlockUDP( access_t * );
92 static block_t *BlockRTP( access_t * );
93 static block_t *BlockChoose( access_t * );
94 static int Control( access_t *, int, va_list );
101 vlc_bool_t b_auto_mtu;
104 uint16_t i_sequence_number;
105 vlc_bool_t b_first_seqno;
107 /* reorder rtp packets when out-of-bounds
108 * the packets hold queue is one level deep
110 uint32_t i_rtp_late; /* number of ms an RTP packet may be too late*/
111 uint32_t i_last_pcr; /* last known good PCR */
112 block_t *p_list; /* list of packets to rearrange */
113 block_t *p_end; /* last packet in p_list */
114 block_t *p_next; /* p_next ?? */
117 /*****************************************************************************
118 * Open: open the socket
119 *****************************************************************************/
120 static int Open( vlc_object_t *p_this )
122 access_t *p_access = (access_t*)p_this;
125 char *psz_name = strdup( p_access->psz_path );
126 char *psz_parser, *psz_server_addr, *psz_bind_addr = "";
127 int i_bind_port, i_server_port = 0;
129 /* First set ipv4/ipv6 */
130 var_Create( p_access, "ipv4", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
131 var_Create( p_access, "ipv6", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
133 if( *p_access->psz_access )
136 /* Find out which shortcut was used */
137 if( !strncmp( p_access->psz_access, "udp4", 6 ) ||
138 !strncmp( p_access->psz_access, "rtp4", 6 ))
140 val.b_bool = VLC_TRUE;
141 var_Set( p_access, "ipv4", val );
143 val.b_bool = VLC_FALSE;
144 var_Set( p_access, "ipv6", val );
146 else if( !strncmp( p_access->psz_access, "udp6", 6 ) ||
147 !strncmp( p_access->psz_access, "rtp6", 6 ) )
149 val.b_bool = VLC_TRUE;
150 var_Set( p_access, "ipv6", val );
152 val.b_bool = VLC_FALSE;
153 var_Set( p_access, "ipv4", val );
157 i_bind_port = var_CreateGetInteger( p_access, "server-port" );
159 /* Parse psz_name syntax :
160 * [serveraddr[:serverport]][@[bindaddr]:[bindport]] */
161 psz_parser = strchr( psz_name, '@' );
162 if( psz_parser != NULL )
164 /* Found bind address and/or bind port */
165 *psz_parser++ = '\0';
166 psz_bind_addr = psz_parser;
168 if( *psz_parser == '[' )
169 /* skips bracket'd IPv6 address */
170 psz_parser = strchr( psz_parser, ']' );
172 if( psz_parser != NULL )
174 psz_parser = strchr( psz_parser, ':' );
175 if( psz_parser != NULL )
177 *psz_parser++ = '\0';
178 i_bind_port = atoi( psz_parser );
183 psz_server_addr = psz_name;
184 if( *psz_server_addr == '[' )
185 /* skips bracket'd IPv6 address */
186 psz_parser = strchr( psz_name, ']' );
188 if( psz_parser != NULL )
190 psz_parser = strchr( psz_parser, ':' );
191 if( psz_parser != NULL )
193 *psz_parser++ = '\0';
194 i_server_port = atoi( psz_parser );
198 msg_Dbg( p_access, "opening server=%s:%d local=%s:%d",
199 psz_server_addr, i_server_port, psz_bind_addr, i_bind_port );
201 /* Set up p_access */
202 p_access->pf_read = NULL;
203 if( !strcasecmp( p_access->psz_access, "rtp" )
204 || !strcasecmp( p_access->psz_access, "rtp4" )
205 || !strcasecmp( p_access->psz_access, "rtp6" ) )
207 p_access->pf_block = BlockRTP;
211 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" );
234 net_StopSend( p_sys->fd );
237 p_sys->i_mtu = var_CreateGetInteger( p_access, "mtu" );
238 if( p_sys->i_mtu <= 1 )
239 p_sys->i_mtu = 1500; /* Avoid problem */
241 p_sys->b_auto_mtu = var_CreateGetBool( p_access, "udp-auto-mtu" );;
243 /* Update default_pts to a suitable value for udp access */
244 var_Create( p_access, "udp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
246 /* Keep track of RTP sequence number */
247 p_sys->i_sequence_number = 0;
248 p_sys->b_first_seqno = VLC_TRUE;
250 /* RTP reordering out-of-bound packets */
251 p_sys->i_last_pcr = 0;
252 p_sys->i_rtp_late = var_CreateGetInteger( p_access, "rtp-late" );
253 p_sys->p_list = NULL;
255 p_sys->p_next = NULL;
260 /*****************************************************************************
261 * Close: free unused data structures
262 *****************************************************************************/
263 static void Close( vlc_object_t *p_this )
265 access_t *p_access = (access_t*)p_this;
266 access_sys_t *p_sys = p_access->p_sys;
268 block_ChainRelease( p_sys->p_list );
269 net_Close( p_sys->fd );
273 /*****************************************************************************
275 *****************************************************************************/
276 static int Control( access_t *p_access, int i_query, va_list args )
278 access_sys_t *p_sys = p_access->p_sys;
286 case ACCESS_CAN_SEEK:
287 case ACCESS_CAN_FASTSEEK:
288 case ACCESS_CAN_PAUSE:
289 case ACCESS_CAN_CONTROL_PACE:
290 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
291 *pb_bool = VLC_FALSE;
295 pi_int = (int*)va_arg( args, int * );
296 *pi_int = p_sys->i_mtu;
299 case ACCESS_GET_PTS_DELAY:
300 pi_64 = (int64_t*)va_arg( args, int64_t * );
301 *pi_64 = var_GetInteger( p_access, "udp-caching" ) * 1000;
305 case ACCESS_SET_PAUSE_STATE:
306 case ACCESS_GET_TITLE_INFO:
307 case ACCESS_SET_TITLE:
308 case ACCESS_SET_SEEKPOINT:
309 case ACCESS_SET_PRIVATE_ID_STATE:
313 msg_Warn( p_access, "unimplemented query in control" );
320 /*****************************************************************************
322 *****************************************************************************/
323 static block_t *BlockUDP( access_t *p_access )
325 access_sys_t *p_sys = p_access->p_sys;
329 p_block = block_New( p_access, p_sys->i_mtu );
330 p_block->i_buffer = net_Read( p_access, p_sys->fd, NULL,
331 p_block->p_buffer, p_sys->i_mtu,
333 if( p_block->i_buffer <= 0 )
335 block_Release( p_block );
339 if( p_block->i_buffer >= p_sys->i_mtu && p_sys->b_auto_mtu &&
340 p_sys->i_mtu < 32767 )
342 /* Increase by 100% */
344 msg_Dbg( p_access, "increasing MTU to %d", p_sys->i_mtu );
351 * rtp_ChainInsert - insert a p_block in the chain and
352 * look at the sequence numbers.
354 static inline void rtp_ChainInsert( access_t *p_access, block_t **pp_list, block_t **pp_end, block_t *p_block )
356 block_t *p_tmp = NULL;
360 uint16_t i_expected = 0;
361 uint32_t i_pcr_new = 0;
363 if( *pp_list == NULL )
369 /* Appending packets at the end of the chain is the normal case */
370 i_pcr_new = ( (p_block->p_buffer[4] << 24) +
371 (p_block->p_buffer[5] << 16) +
372 (p_block->p_buffer[6] << 8) +
373 p_block->p_buffer[7] );
374 i_new = ( (p_block->p_buffer[2] << 8 ) + p_block->p_buffer[3] );
377 i_cur = ( (p->p_buffer[2] << 8 ) + p->p_buffer[3] );
378 i_expected = ((i_cur+1) % RTP_SEQ_NUM_SIZE);
380 if( i_new == i_expected ) /* Append at the end? */
382 msg_Dbg( p_access, ">> append %p(%u)==%p(%u)\n", p_block, i_cur, p, i_new );
383 p->p_next = *pp_end = p_block;
386 /* Add to the fron fo the chain? */
388 i_new = ( (p_block->p_buffer[2] << 8 ) + p_block->p_buffer[3] );
389 i_cur = ( (p->p_buffer[2] << 8 ) + p->p_buffer[3] );
390 i_expected = ((i_cur+1) % RTP_SEQ_NUM_SIZE);
391 if( i_expected > i_new )
393 msg_Dbg( p_access, ">> prepend %p(%u)==%p(%u)\n", p_block, i_cur, p, i_new );
398 /* The packet can't be added to the front or the end of the chain,
399 * thus walk the chain from the start.
403 i_cur = (p->p_buffer[2] << 8 ) + p->p_buffer[3];
404 i_expected = (i_cur+1) % RTP_SEQ_NUM_SIZE;
406 msg_Dbg( p_access, "i_cur: %u, i_new: %u", i_cur, i_new);
409 uint32_t i_pcr_cur = ( (p->p_buffer[4] << 24) +
410 (p->p_buffer[5] << 16) +
411 (p->p_buffer[6] << 8) +
413 /* This packet might be a duplicate, so check PCR's */
414 if( i_pcr_cur >= i_pcr_new )
416 /* packet way too late drop it. */
417 block_Release( p_block );
420 /* Add it to list later on
421 * else if( i_pcr_cur < i_pcr_new ) */
424 else if( i_expected == i_new ) /* insert in chain */
427 msg_Dbg( p_access, ">> insert between %p(%u)==%p(%u)", p, i_cur, p_tmp, i_new );
429 p_block->p_next = p_tmp;
432 if( !p->p_next ) break;
438 * rtp_ChainSend - look which packets are ready for sending.
440 static inline block_t *rtp_ChainSend( access_t *p_access, block_t **pp_list, uint16_t i_seq )
442 access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
447 /* Parse RTP header */
449 int i_extension_bit = 0;
450 int i_extension_length = 0;
451 int i_CSRC_count = 0;
453 block_t *p_prev = NULL;
454 block_t *p_send = *pp_list;
455 block_t *p = *pp_list;
459 i_cur = ( (p->p_buffer[2] << 8 ) + p->p_buffer[3] );
460 msg_Dbg( p_access, "rtp_ChainSend: i_cur %u, i_seq %u", i_cur, i_seq );
463 /* sent all packets that are received in order */
466 i_CSRC_count = p->p_buffer[0] & 0x0F;
467 i_extension_bit = ( p->p_buffer[0] & 0x10 ) >> 4;
468 if ( i_extension_bit == 1)
469 i_extension_length = ( (p->p_buffer[14] << 8 ) +
472 /* Skip header + CSRC extension field n*(32 bits) + extention */
473 i_skip = RTP_HEADER_LEN + 4*i_CSRC_count + i_extension_length;
475 /* Return the packet without the RTP header. */
476 p->i_buffer -= i_skip;
477 p->p_buffer += i_skip;
479 else if( i_cur > i_seq )
484 p_prev->p_next = NULL;
485 p_sys->i_last_pcr = ( (p_prev->p_buffer[4] << 24) +
486 (p_prev->p_buffer[5] << 16) +
487 (p_prev->p_buffer[6] << 8) +
488 p_prev->p_buffer[7] );
489 p_sys->i_sequence_number = ( (p_prev->p_buffer[2] << 8 ) +
490 p_prev->p_buffer[3] );
493 /* FiXME: or should we return NULL here? */
497 if (!p->p_next) break;
500 /* We have walked through the complete chain and all packets are
501 * in sequence - so send the whole chain
503 i_CSRC_count = p->p_buffer[0] & 0x0F;
504 i_extension_bit = ( p->p_buffer[0] & 0x10 ) >> 4;
505 if( i_extension_bit == 1)
506 i_extension_length = ( (p->p_buffer[14] << 8 ) + p->p_buffer[15] );
508 /* Skip header + CSRC extension field n*(32 bits) + extention */
509 i_skip = RTP_HEADER_LEN + 4*i_CSRC_count + i_extension_length;
511 /* Update the list pointers */
513 p_sys->p_next = NULL;
515 p_sys->i_sequence_number = ( (p->p_buffer[2] << 8 ) +
517 p_sys->i_last_pcr = ( (p->p_buffer[4] << 24) +
518 (p->p_buffer[5] << 16) +
519 (p->p_buffer[6] << 8) +
521 /* Return the packet without the RTP header. */
522 p->i_buffer -= i_skip;
523 p->p_buffer += i_skip;
529 /*****************************************************************************
530 * BlockParseRTP/BlockRTP:
531 *****************************************************************************/
532 static block_t *BlockParseRTP( access_t *p_access, block_t *p_block )
534 access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
539 uint16_t i_sequence_number = 0;
540 uint16_t i_sequence_expected = 0;
541 int i_extension_bit = 0;
542 int i_extension_length = 0;
545 if( p_block == NULL )
548 if( p_block->i_buffer < RTP_HEADER_LEN )
550 msg_Warn( p_access, "received a too short packet for RTP" );
551 block_Release( p_block );
554 /* Parse the header and make some verifications.
556 i_rtp_version = ( p_block->p_buffer[0] & 0xC0 ) >> 6;
557 i_CSRC_count = p_block->p_buffer[0] & 0x0F;
558 i_payload_type = p_block->p_buffer[1] & 0x7F;
559 i_sequence_number = (p_block->p_buffer[2] << 8) +
560 p_block->p_buffer[3];
561 i_pcr = ( (p_block->p_buffer[4] << 24) +
562 (p_block->p_buffer[5] << 16) +
563 (p_block->p_buffer[6] << 8) +
564 p_block->p_buffer[7] );
565 i_extension_bit = ( p_block->p_buffer[0] & 0x10 ) >> 4;
567 if( i_rtp_version != 2 )
568 msg_Dbg( p_access, "RTP version is %u, should be 2", i_rtp_version );
570 if( i_payload_type == 14 )
572 else if( i_payload_type != 33 && i_payload_type != 32 )
573 msg_Dbg( p_access, "unsupported RTP payload type (%u)", i_payload_type );
575 if( i_extension_bit == 1)
576 i_extension_length = 4 +
577 4 * ( (p_block->p_buffer[14] << 8) + p_block->p_buffer[15] );
579 /* Skip header + CSRC extension field n*(32 bits) + extention */
580 i_skip += RTP_HEADER_LEN + 4*i_CSRC_count + i_extension_length;
581 if( i_skip >= p_block->i_buffer )
583 msg_Warn( p_access, "received a too short packet for RTP" );
584 block_Release( p_block );
588 /* Detect RTP packet loss through tracking sequence numbers,
589 * and take RTP PCR into account.
592 if( p_sys->b_first_seqno )
594 p_sys->i_sequence_number = i_sequence_number;
595 p_sys->i_last_pcr = i_pcr;
596 p_sys->b_first_seqno = VLC_FALSE;
599 /* Emulate packet loss */
600 if ( (i_sequence_number % 4000) == 0)
602 msg_Warn( p_access, "Emulating packet drop" );
603 block_Release( p_block );
607 i_sequence_expected = ((p_sys->i_sequence_number + 1) % RTP_SEQ_NUM_SIZE);
608 if( i_sequence_expected != i_sequence_number )
610 /* Handle out of order packets */
611 if( p_sys->i_rtp_late > 0 )
613 if( i_sequence_expected < i_sequence_number )
616 "RTP packet out of order (too early) expected %u, current %u",
617 i_sequence_expected, i_sequence_number );
618 if( (i_pcr - p_sys->i_last_pcr) > (p_sys->i_rtp_late*90) )
620 /* Gap too big, we have been holding this data for too long,
624 "Gap too big resyncing: delta %u, held for %d ms",
625 (i_pcr - p_sys->i_last_pcr), p_sys->i_rtp_late );
626 rtp_ChainInsert( p_access, &p_sys->p_list, &p_sys->p_end, p_block );
627 return rtp_ChainSend( p_access, &p_sys->p_list, i_sequence_expected );
629 /* hold packets that arrive too early. */
630 rtp_ChainInsert( p_access, &p_sys->p_list, &p_sys->p_end, p_block );
631 return rtp_ChainSend( p_access, &p_sys->p_list, p_sys->i_sequence_number );
633 else if( /* (i_sequence_expected > i_sequence_number ) && */
634 (i_pcr <= p_sys->i_last_pcr) )
637 "RTP packet out of order (duplicate or too late) expected %u, current %u .. trashing it",
638 i_sequence_expected, i_sequence_number );
639 block_Release( p_block );
640 p_sys->i_sequence_number = i_sequence_number;
641 p_sys->i_last_pcr = i_pcr;
648 block_t **p_send = &p_sys->p_list;
651 "RTP packet (unexpected condition) expected %u, current %u",
652 i_sequence_expected, i_sequence_number );
654 /* Append block to the end of chain and send whole chain */
655 block_ChainLastAppend( &p_send, p_block );
656 p_sys->p_list = p_sys->p_end = NULL;
657 p_sys->i_sequence_number = i_sequence_number;
658 p_sys->i_last_pcr = i_pcr;
660 /* Return the packet without the RTP header. */
664 p->i_buffer -= i_skip;
665 p->p_buffer += i_skip;
670 /* This code should never be reached !! */
672 "Bug in algorithme: (unexpected condition) expected %u (pcr=%u), current %u (pcr=%u)",
673 i_sequence_expected, i_sequence_number, p_sys->i_last_pcr, i_pcr );
676 "RTP packet(s) lost, expected sequence number %d got %d",
677 i_sequence_expected, i_sequence_number );
679 /* Mark transport error in the first TS packet in the RTP stream. */
680 if( (i_payload_type == 33) && (p_block->p_buffer[0] == 0x47) )
681 p_block->p_buffer[1] |= 0x80;
683 else if( (p_sys->i_rtp_late > 0) && p_sys->p_list )
685 if( i_pcr <= p_sys->i_last_pcr )
688 "RTP packet out of order (duplicate) expected %u, current %u .. trashing it",
689 i_sequence_expected, i_sequence_number );
690 block_Release( p_block );
691 p_sys->i_sequence_number = i_sequence_number;
692 p_sys->i_last_pcr = i_pcr;
695 rtp_ChainInsert( p_access, &p_sys->p_list, &p_sys->p_end, p_block );
696 return rtp_ChainSend( p_access, &p_sys->p_list, i_sequence_expected );
699 /* This is the normal case when no packet reordering is effective */
700 p_sys->i_sequence_number = i_sequence_number;
701 p_sys->i_last_pcr = i_pcr;
703 /* Return the packet without the RTP header. */
704 p_block->i_buffer -= i_skip;
705 p_block->p_buffer += i_skip;
709 static block_t *BlockRTP( access_t *p_access )
711 block_t *p_block = BlockUDP( p_access );
713 if ( p_block != NULL )
714 return BlockParseRTP( p_access, p_block );
719 /*****************************************************************************
720 * BlockChoose: decide between RTP and UDP
721 *****************************************************************************/
722 static block_t *BlockChoose( access_t *p_access )
729 if( ( p_block = BlockUDP( p_access ) ) == NULL )
732 if( p_block->p_buffer[0] == 0x47 )
734 msg_Dbg( p_access, "detected TS over raw UDP" );
735 p_access->pf_block = BlockUDP;
739 if( p_block->i_buffer < RTP_HEADER_LEN )
742 /* Parse the header and make some verifications.
745 i_rtp_version = ( p_block->p_buffer[0] & 0xC0 ) >> 6;
746 i_CSRC_count = ( p_block->p_buffer[0] & 0x0F );
747 i_payload_type = ( p_block->p_buffer[1] & 0x7F );
749 if( i_rtp_version != 2 )
751 msg_Dbg( p_access, "no supported RTP header detected" );
752 p_access->pf_block = BlockUDP;
756 switch( i_payload_type )
759 msg_Dbg( p_access, "detected TS over RTP" );
760 p_access->psz_demux = strdup( "ts" );
764 msg_Dbg( p_access, "detected MPEG audio over RTP" );
765 p_access->psz_demux = strdup( "mpga" );
769 msg_Dbg( p_access, "detected MPEG video over RTP" );
770 p_access->psz_demux = strdup( "mpgv" );
774 msg_Dbg( p_access, "no RTP header detected" );
775 p_access->pf_block = BlockUDP;
779 p_access->pf_block = BlockRTP;
781 return BlockParseRTP( p_access, p_block );