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 *****************************************************************************/
36 #include <vlc_access.h>
37 #include <vlc_network.h>
39 /* Big pile of stuff missing form glibc 2.5 */
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 SOCK_DCCP /* provisional API */
54 # define IPPROTO_DCCP 33 /* IANA */
57 #ifndef IPPROTO_UDPLITE
58 # define IPPROTO_UDPLITE 136 /* from IANA */
61 # define SOL_UDPLITE IPPROTO_UDPLITE
65 /*****************************************************************************
67 *****************************************************************************/
68 #define CACHING_TEXT N_("Caching value in ms")
69 #define CACHING_LONGTEXT N_( \
70 "Caching value for UDP streams. This " \
71 "value should be set in milliseconds." )
73 #define AUTO_MTU_TEXT N_("Autodetection of MTU")
74 #define AUTO_MTU_LONGTEXT N_( \
75 "Automatically detect the line's MTU. This will increase the size if" \
76 " truncated packets are found" )
78 #define RTP_LATE_TEXT N_("RTP reordering timeout in ms")
79 #define RTP_LATE_LONGTEXT N_( \
80 "VLC reorders RTP packets. The input will wait for late packets at most "\
81 "the time specified here (in milliseconds)." )
83 static int Open ( vlc_object_t * );
84 static void Close( vlc_object_t * );
87 set_shortname( _("UDP/RTP" ) );
88 set_description( _("UDP/RTP input") );
89 set_category( CAT_INPUT );
90 set_subcategory( SUBCAT_INPUT_ACCESS );
92 add_integer( "udp-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT,
93 CACHING_LONGTEXT, VLC_TRUE );
94 add_integer( "rtp-late", 100, NULL, RTP_LATE_TEXT, RTP_LATE_LONGTEXT, VLC_TRUE );
96 add_bool( "udp-auto-mtu", 1, NULL,
97 AUTO_MTU_TEXT, AUTO_MTU_LONGTEXT, VLC_TRUE );
99 set_capability( "access2", 0 );
100 add_shortcut( "udp" );
101 add_shortcut( "udpstream" );
102 add_shortcut( "udp4" );
103 add_shortcut( "udp6" );
104 add_shortcut( "rtp" );
105 add_shortcut( "rtp4" );
106 add_shortcut( "rtp6" );
107 add_shortcut( "udplite" );
108 add_shortcut( "rtptcp" );
109 add_shortcut( "dccp" );
111 set_callbacks( Open, Close );
114 /*****************************************************************************
116 *****************************************************************************/
117 #define RTP_HEADER_LEN 12
119 static block_t *BlockUDP( access_t * );
120 static block_t *BlockTCP( access_t * );
121 static block_t *BlockRTP( access_t * );
122 static block_t *BlockChoose( access_t * );
123 static int Control( access_t *, int, va_list );
130 vlc_bool_t b_auto_mtu;
131 vlc_bool_t b_framed_rtp;
133 /* reorder rtp packets when out-of-sequence */
134 uint16_t i_last_seqno;
138 block_t *p_partial_frame; /* Partial Framed RTP packet */
141 /*****************************************************************************
142 * Open: open the socket
143 *****************************************************************************/
144 static int Open( vlc_object_t *p_this )
146 access_t *p_access = (access_t*)p_this;
149 char *psz_name = strdup( p_access->psz_path );
151 const char *psz_server_addr, *psz_bind_addr = "";
152 int i_bind_port, i_server_port = 0;
153 int fam = AF_UNSPEC, proto = IPPROTO_UDP;
155 if (strlen (p_access->psz_access) >= 3)
157 switch (p_access->psz_access[3])
167 if (strcmp (p_access->psz_access + 3, "lite") == 0)
168 proto = IPPROTO_UDPLITE;
170 if (strcmp (p_access->psz_access + 3, "tcp") == 0)
173 if (strcmp (p_access->psz_access, "dccp") == 0)
174 proto = IPPROTO_DCCP;
177 i_bind_port = var_CreateGetInteger( p_access, "server-port" );
179 /* Parse psz_name syntax :
180 * [serveraddr[:serverport]][@[bindaddr]:[bindport]] */
181 psz_parser = strchr( psz_name, '@' );
182 if( psz_parser != NULL )
184 /* Found bind address and/or bind port */
185 *psz_parser++ = '\0';
186 psz_bind_addr = psz_parser;
188 if( psz_bind_addr[0] == '[' )
189 /* skips bracket'd IPv6 address */
190 psz_parser = strchr( psz_parser, ']' );
192 if( psz_parser != NULL )
194 psz_parser = strchr( psz_parser, ':' );
195 if( psz_parser != NULL )
197 *psz_parser++ = '\0';
198 i_bind_port = atoi( psz_parser );
203 psz_server_addr = psz_name;
204 psz_parser = ( psz_server_addr[0] == '[' )
205 ? strchr( psz_name, ']' ) /* skips bracket'd IPv6 address */
208 if( psz_parser != NULL )
210 psz_parser = strchr( psz_parser, ':' );
211 if( psz_parser != NULL )
213 *psz_parser++ = '\0';
214 i_server_port = atoi( psz_parser );
218 msg_Dbg( p_access, "opening server=%s:%d local=%s:%d",
219 psz_server_addr, i_server_port, psz_bind_addr, i_bind_port );
221 /* Set up p_access */
222 access_InitFields( p_access );
223 ACCESS_SET_CALLBACKS( NULL, BlockChoose, Control, NULL );
224 p_access->info.b_prebuffered = VLC_FALSE;
225 MALLOC_ERR( p_access->p_sys, access_sys_t ); p_sys = p_access->p_sys;
226 memset (p_sys, 0, sizeof (*p_sys));
231 case IPPROTO_UDPLITE:
232 p_sys->fd = net_OpenDgram( p_access, psz_bind_addr, i_bind_port,
233 psz_server_addr, i_server_port, fam,
238 p_sys->fd = net_ConnectTCP( p_access, psz_server_addr, i_server_port );
239 p_sys->b_framed_rtp = VLC_TRUE;
244 p_sys->fd = net_Connect( p_access, psz_server_addr, i_server_port,
245 SOCK_DCCP, IPPROTO_DCCP );
248 msg_Err( p_access, "DCCP support not compiled-in!" );
253 if( p_sys->fd == -1 )
255 msg_Err( p_access, "cannot open socket" );
260 shutdown( p_sys->fd, SHUT_WR );
262 #ifdef UDPLITE_RECV_CSCOV
263 if (proto == IPPROTO_UDPLITE)
264 /* UDP header: 8 bytes + RTP header: 12 bytes (or more) */
265 setsockopt (p_sys->fd, SOL_UDPLITE, UDPLITE_RECV_CSCOV,
266 &(int){ 20 }, sizeof (int));
269 if (p_sys->b_framed_rtp)
271 /* We don't do autodetection and prebuffering in case of framing */
272 p_access->pf_block = BlockRTP;
273 p_sys->i_mtu = 65535;
278 p_sys->i_mtu = var_CreateGetInteger( p_access, "mtu" );
279 if( p_sys->i_mtu <= 1 )
280 p_sys->i_mtu = 1500; /* Avoid problem */
282 p_sys->b_auto_mtu = var_CreateGetBool( p_access, "udp-auto-mtu" );;
285 /* Update default_pts to a suitable value for udp access */
286 var_Create( p_access, "udp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
288 /* RTP reordering for out-of-sequence packets */
289 p_sys->i_rtp_late = var_CreateGetInteger( p_access, "rtp-late" ) * 1000;
290 p_sys->i_last_seqno = 0;
291 p_sys->p_list = NULL;
296 /*****************************************************************************
297 * Close: free unused data structures
298 *****************************************************************************/
299 static void Close( vlc_object_t *p_this )
301 access_t *p_access = (access_t*)p_this;
302 access_sys_t *p_sys = p_access->p_sys;
304 block_ChainRelease( p_sys->p_list );
305 net_Close( p_sys->fd );
309 /*****************************************************************************
311 *****************************************************************************/
312 static int Control( access_t *p_access, int i_query, va_list args )
314 access_sys_t *p_sys = p_access->p_sys;
322 case ACCESS_CAN_SEEK:
323 case ACCESS_CAN_FASTSEEK:
324 case ACCESS_CAN_PAUSE:
325 case ACCESS_CAN_CONTROL_PACE:
326 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
327 *pb_bool = VLC_FALSE;
331 pi_int = (int*)va_arg( args, int * );
332 *pi_int = p_sys->i_mtu;
335 case ACCESS_GET_PTS_DELAY:
336 pi_64 = (int64_t*)va_arg( args, int64_t * );
337 *pi_64 = var_GetInteger( p_access, "udp-caching" ) * 1000;
341 case ACCESS_SET_PAUSE_STATE:
342 case ACCESS_GET_TITLE_INFO:
343 case ACCESS_SET_TITLE:
344 case ACCESS_SET_SEEKPOINT:
345 case ACCESS_SET_PRIVATE_ID_STATE:
349 msg_Warn( p_access, "unimplemented query in control" );
356 /*****************************************************************************
358 *****************************************************************************/
359 static block_t *BlockUDP( access_t *p_access )
361 access_sys_t *p_sys = p_access->p_sys;
365 p_block = block_New( p_access, p_sys->i_mtu );
366 p_block->i_buffer = net_Read( p_access, p_sys->fd, NULL,
367 p_block->p_buffer, p_sys->i_mtu,
369 if( p_block->i_buffer < 0 )
371 block_Release( p_block );
375 if( (p_block->i_buffer >= p_sys->i_mtu) && p_sys->b_auto_mtu &&
376 p_sys->i_mtu < 32767 )
378 /* Increase by 100% */
380 msg_Dbg( p_access, "increasing MTU to %d", p_sys->i_mtu );
386 /*****************************************************************************
387 * BlockTCP: Framed RTP/AVP packet reception for COMEDIA (see RFC4571)
388 *****************************************************************************/
389 static block_t *BlockTCP( access_t *p_access )
391 access_sys_t *p_sys = p_access->p_sys;
392 block_t *p_block = p_sys->p_partial_frame;
394 if( p_access->info.b_eof )
397 if( p_block == NULL )
399 /* MTU should always be 65535 in this case */
400 p_sys->p_partial_frame = p_block = block_New( p_access, 65537 );
405 /* Read RTP framing */
406 if (p_block->i_buffer < 2)
408 /* FIXME: not very efficient */
409 int i_read = net_Read( p_access, p_sys->fd, NULL,
410 p_block->p_buffer + p_block->i_buffer,
411 2 - p_block->i_buffer, VLC_FALSE );
415 p_block->i_buffer += i_read;
416 if (p_block->i_buffer < 2)
420 uint16_t framelen = GetWLE( p_block->p_buffer );
424 int i_read = net_Read( p_access, p_sys->fd, NULL,
425 p_block->p_buffer + p_block->i_buffer,
426 2 + framelen - p_block->i_buffer, VLC_FALSE );
430 p_block->i_buffer += i_read;
433 if( p_block->i_buffer < (2 + framelen) )
434 return NULL; // incomplete frame
436 /* Hide framing from RTP layer */
437 p_block->p_buffer += 2;
438 p_block->i_buffer -= 2;
439 p_sys->p_partial_frame = NULL;
443 p_access->info.b_eof = VLC_TRUE;
444 block_Release( p_block );
445 p_sys->p_partial_frame = NULL;
451 * rtp_ChainInsert - insert a p_block in the chain and
452 * look at the sequence numbers.
454 static inline vlc_bool_t rtp_ChainInsert( access_t *p_access, block_t *p_block )
456 access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
457 block_t *p_prev = NULL;
458 block_t *p = p_sys->p_end;
459 uint16_t i_new = (uint16_t) p_block->i_dts;
464 p_sys->p_list = p_block;
465 p_sys->p_end = p_block;
468 /* walk through the queue from top down since the new packet is in
469 most cases just appended to the end */
473 i_tmp = i_new - (uint16_t) p->i_dts;
475 if( !i_tmp ) /* trash duplicate */
479 { /* insert after this block ( i_new > p->i_dts ) */
480 p_block->p_next = p->p_next;
485 p_prev->p_prev = p_block;
486 msg_Dbg(p_access, "RTP reordering: insert after %d, new %d",
487 (uint16_t) p->i_dts, i_new );
491 p_sys->p_end = p_block;
495 if( p == p_sys->p_list )
496 { /* we've reached bottom of chain */
497 i_tmp = p_sys->i_last_seqno - i_new;
498 if( !p_access->info.b_prebuffered || (i_tmp > 32767) )
500 msg_Dbg(p_access, "RTP reordering: prepend %d before %d",
501 i_new, (uint16_t) p->i_dts );
504 p_sys->p_list = p_block;
508 if( !i_tmp ) /* trash duplicate */
511 /* reordering failed - append the packet to the end of queue */
512 msg_Dbg(p_access, "RTP: sequence changed (or buffer too small) "
513 "new: %d, buffer %d...%d", i_new, (uint16_t) p->i_dts,
514 (uint16_t) p_sys->p_end->i_dts);
515 p_sys->p_end->p_next = p_block;
516 p_block->p_prev = p_sys->p_end;
517 p_sys->p_end = p_block;
523 block_Release( p_block );
527 /*****************************************************************************
528 * BlockParseRTP/BlockRTP:
529 *****************************************************************************/
530 static block_t *BlockParseRTP( access_t *p_access, block_t *p_block )
533 size_t i_skip = RTP_HEADER_LEN;
535 if( p_block == NULL )
538 if( p_block->i_buffer < RTP_HEADER_LEN )
540 msg_Dbg( p_access, "short RTP packet received" );
544 /* Parse the header and make some verifications.
547 if( ( p_block->p_buffer[0] >> 6 ) != 2)
549 msg_Dbg( p_access, "RTP version is %u instead of 2",
550 p_block->p_buffer[0] >> 6 );
554 uint8_t pad = (p_block->p_buffer[0] & 0x20)
555 ? p_block->p_buffer[p_block->i_buffer - 1] : 0;
557 i_skip += (p_block->p_buffer[0] & 0x0F) * 4;
559 if (p_block->p_buffer[0] & 0x10) /* Extension header */
562 if ((size_t)p_block->i_buffer < i_skip)
565 i_skip += 4 * GetWBE( p_block->p_buffer + i_skip - 2 );
568 i_payload_type = p_block->p_buffer[1] & 0x7F;
570 /* Remember sequence number in i_dts */
571 p_block->i_pts = mdate();
572 p_block->i_dts = (mtime_t) GetWBE( p_block->p_buffer + 2 );
574 /* FIXME: use rtpmap */
575 switch( i_payload_type )
577 case 14: // MPA: MPEG Audio (RFC2250, §3.4)
578 i_skip += 4; // 32 bits RTP/MPA header
581 case 32: // MPV: MPEG Video (RFC2250, §3.5)
582 i_skip += 4; // 32 bits RTP/MPV header
583 if( (size_t)p_block->i_buffer < i_skip )
585 if( p_block->p_buffer[i_skip - 3] & 0x4 )
587 /* MPEG2 Video extension header */
588 /* TODO: shouldn't we skip this too ? */
592 case 33: // MP2: MPEG TS (RFC2250, §2)
593 /* plain TS over RTP */
597 msg_Dbg( p_access, "unsupported RTP payload type: %u", i_payload_type );
601 if( (size_t)p_block->i_buffer < (i_skip + pad) )
604 /* Remove the RTP header */
605 p_block->i_buffer -= i_skip;
606 p_block->p_buffer += i_skip;
608 /* This is the place for deciphering and authentication */
610 /* Remove padding (at the end) */
611 p_block->i_buffer -= pad;
614 /* Emulate packet loss */
615 if ( (i_sequence_number % 4000) == 0)
617 msg_Warn( p_access, "Emulating packet drop" );
618 block_Release( p_block );
626 block_Release( p_block );
630 static block_t *BlockPrebufferRTP( access_t *p_access, block_t *p_block )
632 access_sys_t *p_sys = p_access->p_sys;
633 mtime_t i_first = mdate();
635 block_t *p = p_block;
639 mtime_t i_date = mdate();
641 if( p && rtp_ChainInsert( p_access, p ))
644 /* Require at least 2 packets in the buffer */
645 if( i_count > 2 && (i_date - i_first) > p_sys->i_rtp_late )
648 p = BlockParseRTP( p_access, BlockUDP( p_access ) );
649 if( !p && (i_date - i_first) > p_sys->i_rtp_late )
651 msg_Err( p_access, "error in RTP prebuffering!" );
656 msg_Dbg( p_access, "RTP: prebuffered %d packets", i_count - 1 );
657 p_access->info.b_prebuffered = VLC_TRUE;
659 p_sys->p_list = p_sys->p_list->p_next;
660 p_sys->i_last_seqno = (uint16_t) p->i_dts;
665 static block_t *BlockRTP( access_t *p_access )
667 access_sys_t *p_sys = p_access->p_sys;
670 while ( !p_sys->p_list ||
671 ( mdate() - p_sys->p_list->i_pts ) < p_sys->i_rtp_late )
673 p = BlockParseRTP( p_access,
674 p_sys->b_framed_rtp ? BlockTCP( p_access )
675 : BlockUDP( p_access ) );
679 rtp_ChainInsert( p_access, p );
683 p_sys->p_list = p_sys->p_list->p_next;
684 p_sys->i_last_seqno++;
685 if( p_sys->i_last_seqno != (uint16_t) p->i_dts )
687 msg_Dbg( p_access, "RTP: packet(s) lost, expected %d, got %d",
688 p_sys->i_last_seqno, (uint16_t) p->i_dts );
689 p_sys->i_last_seqno = (uint16_t) p->i_dts;
695 /*****************************************************************************
696 * BlockChoose: decide between RTP and UDP
697 *****************************************************************************/
698 static block_t *BlockChoose( access_t *p_access )
704 if( ( p_block = BlockUDP( p_access ) ) == NULL )
707 if( p_block->p_buffer[0] == 0x47 )
709 msg_Dbg( p_access, "detected TS over raw UDP" );
710 p_access->pf_block = BlockUDP;
711 p_access->info.b_prebuffered = VLC_TRUE;
715 if( p_block->i_buffer < RTP_HEADER_LEN )
718 /* Parse the header and make some verifications.
721 i_rtp_version = p_block->p_buffer[0] >> 6;
722 i_payload_type = ( p_block->p_buffer[1] & 0x7F );
724 if( i_rtp_version != 2 )
726 msg_Dbg( p_access, "no supported RTP header detected" );
727 p_access->pf_block = BlockUDP;
728 p_access->info.b_prebuffered = VLC_TRUE;
732 switch( i_payload_type )
735 msg_Dbg( p_access, "detected MPEG2 TS over RTP" );
736 p_access->psz_demux = strdup( "ts" );
740 msg_Dbg( p_access, "detected MPEG Audio over RTP" );
741 p_access->psz_demux = strdup( "mpga" );
745 msg_Dbg( p_access, "detected MPEG Video over RTP" );
746 p_access->psz_demux = strdup( "mpgv" );
750 msg_Dbg( p_access, "no RTP header detected" );
751 p_access->pf_block = BlockUDP;
752 p_access->info.b_prebuffered = VLC_TRUE;
756 if( !BlockParseRTP( p_access, p_block )) return NULL;
758 p_access->pf_block = BlockRTP;
760 return BlockPrebufferRTP( p_access, p_block );