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 *****************************************************************************/
39 #include <vlc_common.h>
40 #include <vlc_plugin.h>
41 #include <vlc_access.h>
42 #include <vlc_network.h>
46 /*****************************************************************************
48 *****************************************************************************/
49 #define CACHING_TEXT N_("Caching value in ms")
50 #define CACHING_LONGTEXT N_( \
51 "Caching value for UDP streams. This " \
52 "value should be set in milliseconds." )
54 #define RTP_LATE_TEXT N_("RTP reordering timeout in ms")
55 #define RTP_LATE_LONGTEXT N_( \
56 "VLC reorders RTP packets. The input will wait for late packets at most "\
57 "the time specified here (in milliseconds)." )
59 static int Open ( vlc_object_t * );
60 static void Close( vlc_object_t * );
63 set_shortname( N_("UDP/RTP" ) );
64 set_description( N_("UDP/RTP input") );
65 set_category( CAT_INPUT );
66 set_subcategory( SUBCAT_INPUT_ACCESS );
68 add_integer( "udp-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT,
69 CACHING_LONGTEXT, true );
70 add_integer( "rtp-late", 100, NULL, RTP_LATE_TEXT, RTP_LATE_LONGTEXT, true );
71 add_obsolete_bool( "udp-auto-mtu" );
73 set_capability( "access", 0 );
74 add_shortcut( "udp" );
75 add_shortcut( "udpstream" );
76 add_shortcut( "udp4" );
77 add_shortcut( "udp6" );
78 add_shortcut( "rtptcp" ); /* tcp name is already taken */
80 set_callbacks( Open, Close );
83 /*****************************************************************************
85 *****************************************************************************/
86 #define RTP_HEADER_LEN 12
88 static block_t *BlockUDP( access_t * );
89 static block_t *BlockStartRTP( access_t * );
90 static block_t *BlockRTP( access_t * );
91 static int Control( access_t *, int, va_list );
97 bool b_framed_rtp, b_comedia;
99 /* reorder rtp packets when out-of-sequence */
100 uint16_t i_last_seqno;
104 block_t *p_partial_frame; /* Partial Framed RTP packet */
107 /*****************************************************************************
108 * Open: open the socket
109 *****************************************************************************/
110 static int Open( vlc_object_t *p_this )
112 access_t *p_access = (access_t*)p_this;
115 char *psz_name = strdup( p_access->psz_path );
117 const char *psz_server_addr, *psz_bind_addr = "";
118 int i_bind_port, i_server_port = 0;
119 int fam = AF_UNSPEC, proto = IPPROTO_UDP;
121 /* Set up p_access */
122 access_InitFields( p_access );
123 ACCESS_SET_CALLBACKS( NULL, BlockStartRTP, Control, NULL );
124 p_access->info.b_prebuffered = false;
125 MALLOC_ERR( p_access->p_sys, access_sys_t ); p_sys = p_access->p_sys;
126 memset (p_sys, 0, sizeof (*p_sys));
128 if (strlen (p_access->psz_access) > 0)
130 switch (p_access->psz_access[strlen (p_access->psz_access) - 1])
141 if (strncmp (p_access->psz_access, "udp", 3 ) == 0 )
142 p_access->pf_block = BlockUDP;
144 if (strcmp (p_access->psz_access, "rtptcp") == 0)
148 i_bind_port = var_CreateGetInteger( p_access, "server-port" );
150 /* Parse psz_name syntax :
151 * [serveraddr[:serverport]][@[bindaddr]:[bindport]] */
152 psz_parser = strchr( psz_name, '@' );
153 if( psz_parser != NULL )
155 /* Found bind address and/or bind port */
156 *psz_parser++ = '\0';
157 psz_bind_addr = psz_parser;
159 if( psz_bind_addr[0] == '[' )
160 /* skips bracket'd IPv6 address */
161 psz_parser = strchr( psz_parser, ']' );
163 if( psz_parser != NULL )
165 psz_parser = strchr( psz_parser, ':' );
166 if( psz_parser != NULL )
168 *psz_parser++ = '\0';
169 i_bind_port = atoi( psz_parser );
174 psz_server_addr = psz_name;
175 psz_parser = ( psz_server_addr[0] == '[' )
176 ? strchr( psz_name, ']' ) /* skips bracket'd IPv6 address */
179 if( psz_parser != NULL )
181 psz_parser = strchr( psz_parser, ':' );
182 if( psz_parser != NULL )
184 *psz_parser++ = '\0';
185 i_server_port = atoi( psz_parser );
189 msg_Dbg( p_access, "opening server=%s:%d local=%s:%d",
190 psz_server_addr, i_server_port, psz_bind_addr, i_bind_port );
192 /* Hmm, the net_* connection functions may need to be unified... */
196 p_sys->fd = net_OpenDgram( p_access, psz_bind_addr, i_bind_port,
197 psz_server_addr, i_server_port, fam,
202 p_sys->fd = net_ConnectTCP( p_access, psz_server_addr, i_server_port );
203 p_access->pf_block = BlockRTP;
204 p_sys->b_comedia = p_sys->b_framed_rtp = true;
208 if( p_sys->fd == -1 )
210 msg_Err( p_access, "cannot open socket" );
215 shutdown( p_sys->fd, SHUT_WR );
216 net_SetCSCov (p_sys->fd, -1, 12);
218 /* Update default_pts to a suitable value for udp access */
219 var_Create( p_access, "udp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
221 /* RTP reordering for out-of-sequence packets */
222 p_sys->i_rtp_late = var_CreateGetInteger( p_access, "rtp-late" ) * 1000;
223 p_sys->i_last_seqno = 0;
224 p_sys->p_list = NULL;
229 /*****************************************************************************
230 * Close: free unused data structures
231 *****************************************************************************/
232 static void Close( vlc_object_t *p_this )
234 access_t *p_access = (access_t*)p_this;
235 access_sys_t *p_sys = p_access->p_sys;
237 block_ChainRelease( p_sys->p_list );
238 net_Close( p_sys->fd );
242 /*****************************************************************************
244 *****************************************************************************/
245 static int Control( access_t *p_access, int i_query, va_list args )
254 case ACCESS_CAN_SEEK:
255 case ACCESS_CAN_FASTSEEK:
256 case ACCESS_CAN_PAUSE:
257 case ACCESS_CAN_CONTROL_PACE:
258 pb_bool = (bool*)va_arg( args, bool* );
263 pi_int = (int*)va_arg( args, int * );
267 case ACCESS_GET_PTS_DELAY:
268 pi_64 = (int64_t*)va_arg( args, int64_t * );
269 *pi_64 = var_GetInteger( p_access, "udp-caching" ) * 1000;
273 case ACCESS_SET_PAUSE_STATE:
274 case ACCESS_GET_TITLE_INFO:
275 case ACCESS_SET_TITLE:
276 case ACCESS_SET_SEEKPOINT:
277 case ACCESS_SET_PRIVATE_ID_STATE:
278 case ACCESS_GET_CONTENT_TYPE:
282 msg_Warn( p_access, "unimplemented query in control" );
289 /*****************************************************************************
291 *****************************************************************************/
292 static block_t *BlockUDP( access_t *p_access )
294 access_sys_t *p_sys = p_access->p_sys;
298 if( p_access->info.b_eof )
302 p_block = block_New( p_access, MTU );
303 len = net_Read( p_access, p_sys->fd, NULL,
304 p_block->p_buffer, MTU, false );
306 || ( p_sys->b_comedia && ( len == 0 ) ) )
308 if( p_sys->b_comedia )
310 p_access->info.b_eof = true;
311 msg_Dbg( p_access, "connection-oriented media hangup" );
313 block_Release( p_block );
317 return block_Realloc( p_block, 0, p_block->i_buffer = len );
320 /*****************************************************************************
321 * BlockTCP: Framed RTP/AVP packet reception for COMEDIA (see RFC4571)
322 *****************************************************************************/
323 static block_t *BlockTCP( access_t *p_access )
325 access_sys_t *p_sys = p_access->p_sys;
326 block_t *p_block = p_sys->p_partial_frame;
328 if( p_access->info.b_eof )
331 if( p_block == NULL )
333 /* MTU should always be 65535 in this case */
334 p_sys->p_partial_frame = p_block = block_New( p_access, 2 + MTU );
339 /* Read RTP framing */
340 if (p_block->i_buffer < 2)
342 int i_read = net_Read( p_access, p_sys->fd, NULL,
343 p_block->p_buffer + p_block->i_buffer,
344 2 - p_block->i_buffer, false );
348 p_block->i_buffer += i_read;
349 if (p_block->i_buffer < 2)
353 uint16_t framelen = GetWLE( p_block->p_buffer );
357 int i_read = net_Read( p_access, p_sys->fd, NULL,
358 p_block->p_buffer + p_block->i_buffer,
359 2 + framelen - p_block->i_buffer, false );
363 p_block->i_buffer += i_read;
366 if( p_block->i_buffer < (2u + framelen) )
367 return NULL; // incomplete frame
369 /* Hide framing from RTP layer */
370 p_block->p_buffer += 2;
371 p_block->i_buffer -= 2;
372 p_sys->p_partial_frame = NULL;
376 p_access->info.b_eof = true;
377 block_Release( p_block );
378 p_sys->p_partial_frame = NULL;
384 * rtp_ChainInsert - insert a p_block in the chain and
385 * look at the sequence numbers.
387 static inline bool rtp_ChainInsert( access_t *p_access, block_t *p_block )
389 access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
390 block_t *p_prev = NULL;
391 block_t *p = p_sys->p_end;
392 uint16_t i_new = (uint16_t) p_block->i_dts;
397 p_sys->p_list = p_block;
398 p_sys->p_end = p_block;
401 /* walk through the queue from top down since the new packet is in
402 most cases just appended to the end */
406 i_tmp = i_new - (uint16_t) p->i_dts;
408 if( !i_tmp ) /* trash duplicate */
412 { /* insert after this block ( i_new > p->i_dts ) */
413 p_block->p_next = p->p_next;
418 p_prev->p_prev = p_block;
419 msg_Dbg(p_access, "RTP reordering: insert after %d, new %d",
420 (uint16_t) p->i_dts, i_new );
424 p_sys->p_end = p_block;
428 if( p == p_sys->p_list )
429 { /* we've reached bottom of chain */
430 i_tmp = p_sys->i_last_seqno - i_new;
431 if( !p_access->info.b_prebuffered || (i_tmp > 32767) )
433 msg_Dbg(p_access, "RTP reordering: prepend %d before %d",
434 i_new, (uint16_t) p->i_dts );
437 p_sys->p_list = p_block;
441 if( !i_tmp ) /* trash duplicate */
444 /* reordering failed - append the packet to the end of queue */
445 msg_Dbg(p_access, "RTP: sequence changed (or buffer too small) "
446 "new: %d, buffer %d...%d", i_new, (uint16_t) p->i_dts,
447 (uint16_t) p_sys->p_end->i_dts);
448 p_sys->p_end->p_next = p_block;
449 p_block->p_prev = p_sys->p_end;
450 p_sys->p_end = p_block;
456 block_Release( p_block );
460 /*****************************************************************************
461 * BlockParseRTP: decapsulate the RTP packet and return it
462 *****************************************************************************/
463 static block_t *BlockParseRTP( access_t *p_access, block_t *p_block )
466 size_t i_skip = RTP_HEADER_LEN;
468 if( p_block == NULL )
471 if( p_block->i_buffer < RTP_HEADER_LEN )
473 msg_Dbg( p_access, "short RTP packet received" );
477 /* Parse the header and make some verifications.
480 if( ( p_block->p_buffer[0] >> 6 ) != 2)
482 msg_Dbg( p_access, "RTP version is %u instead of 2",
483 p_block->p_buffer[0] >> 6 );
487 uint8_t pad = (p_block->p_buffer[0] & 0x20)
488 ? p_block->p_buffer[p_block->i_buffer - 1] : 0;
490 i_skip += (p_block->p_buffer[0] & 0x0F) * 4;
492 if (p_block->p_buffer[0] & 0x10) /* Extension header */
495 if ((size_t)p_block->i_buffer < i_skip)
498 i_skip += 4 * GetWBE( p_block->p_buffer + i_skip - 2 );
501 i_payload_type = p_block->p_buffer[1] & 0x7F;
503 /* Remember sequence number in i_dts */
504 p_block->i_pts = mdate();
505 p_block->i_dts = (mtime_t) GetWBE( p_block->p_buffer + 2 );
507 /* FIXME: use rtpmap */
508 const char *psz_demux = NULL;
510 switch( i_payload_type )
512 case 14: // MPA: MPEG Audio (RFC2250, §3.4)
513 i_skip += 4; // 32 bits RTP/MPA header
517 case 32: // MPV: MPEG Video (RFC2250, §3.5)
518 i_skip += 4; // 32 bits RTP/MPV header
519 if( (size_t)p_block->i_buffer < i_skip )
521 if( p_block->p_buffer[i_skip - 3] & 0x4 )
523 /* MPEG2 Video extension header */
524 /* TODO: shouldn't we skip this too ? */
529 case 33: // MP2: MPEG TS (RFC2250, §2)
530 /* plain TS over RTP */
534 case 72: /* muxed SR */
535 case 73: /* muxed RR */
536 case 74: /* muxed SDES */
537 case 75: /* muxed BYE */
538 case 76: /* muxed APP */
539 goto trash; /* ooh! ignoring RTCP is evil! */
542 msg_Dbg( p_access, "unsupported RTP payload type: %u", i_payload_type );
546 if( (size_t)p_block->i_buffer < (i_skip + pad) )
549 /* Remove the RTP header */
550 p_block->i_buffer -= i_skip;
551 p_block->p_buffer += i_skip;
553 /* This is the place for deciphering and authentication */
555 /* Remove padding (at the end) */
556 p_block->i_buffer -= pad;
559 /* Emulate packet loss */
560 if ( (i_sequence_number % 4000) == 0)
562 msg_Warn( p_access, "Emulating packet drop" );
563 block_Release( p_block );
568 if( !p_access->psz_demux || !*p_access->psz_demux )
570 free( p_access->psz_demux );
571 p_access->psz_demux = strdup( psz_demux );
577 block_Release( p_block );
581 /*****************************************************************************
582 * BlockRTP: receives an RTP packet, parses it, queues it queue,
583 * then dequeues the oldest packet and returns it to input/demux.
584 ****************************************************************************/
585 static block_t *BlockRTP( access_t *p_access )
587 access_sys_t *p_sys = p_access->p_sys;
590 while ( !p_sys->p_list ||
591 ( mdate() - p_sys->p_list->i_pts ) < p_sys->i_rtp_late )
593 p = BlockParseRTP( p_access,
594 p_sys->b_framed_rtp ? BlockTCP( p_access )
595 : BlockUDP( p_access ) );
599 rtp_ChainInsert( p_access, p );
603 p_sys->p_list = p_sys->p_list->p_next;
604 p_sys->i_last_seqno++;
605 if( p_sys->i_last_seqno != (uint16_t) p->i_dts )
607 msg_Dbg( p_access, "RTP: packet(s) lost, expected %d, got %d",
608 p_sys->i_last_seqno, (uint16_t) p->i_dts );
609 p_sys->i_last_seqno = (uint16_t) p->i_dts;
615 /*****************************************************************************
616 * BlockPrebufferRTP: waits until we have at least two RTP datagrams,
617 * so that we can synchronize the RTP sequence number.
618 * This is only useful for non-reliable transport protocols.
619 ****************************************************************************/
620 static block_t *BlockPrebufferRTP( access_t *p_access, block_t *p_block )
622 access_sys_t *p_sys = p_access->p_sys;
623 mtime_t i_first = mdate();
625 block_t *p = p_block;
627 if( BlockParseRTP( p_access, p_block ) == NULL )
632 mtime_t i_date = mdate();
634 if( p && rtp_ChainInsert( p_access, p ))
637 /* Require at least 2 packets in the buffer */
638 if( i_count > 2 && (i_date - i_first) > p_sys->i_rtp_late )
641 p = BlockParseRTP( p_access, BlockUDP( p_access ) );
642 if( !p && (i_date - i_first) > p_sys->i_rtp_late )
644 msg_Err( p_access, "error in RTP prebuffering!" );
649 msg_Dbg( p_access, "RTP: prebuffered %d packets", i_count - 1 );
650 p_access->info.b_prebuffered = true;
652 p_sys->p_list = p_sys->p_list->p_next;
653 p_sys->i_last_seqno = (uint16_t) p->i_dts;
658 static block_t *BlockStartRTP( access_t *p_access )
660 p_access->pf_block = BlockRTP;
661 return BlockPrebufferRTP( p_access, BlockUDP( p_access ) );