]> git.sesse.net Git - vlc/blob - modules/access/udp.c
I *can* do it
[vlc] / modules / access / udp.c
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
6  * $Id$
7  *
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>
12  *          Remi Denis-Courmont
13  *
14  * Reviewed: 23 October 2003, Jean-Paul Saman <jpsaman _at_ videolan _dot_ org>
15  *
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.
20  *
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.
25  *
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  *****************************************************************************/
30
31 /*****************************************************************************
32  * Preamble
33  *****************************************************************************/
34 #include <stdlib.h>
35
36 #include <vlc/vlc.h>
37 #include <vlc_access.h>
38 #include <vlc_network.h>
39
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
45 #endif
46
47 #ifndef IPPROTO_UDPLITE
48 # define IPPROTO_UDPLITE 136 /* from IANA */
49 #endif
50 #ifndef SOL_UDPLITE
51 # define SOL_UDPLITE IPPROTO_UDPLITE
52 #endif
53
54
55 /*****************************************************************************
56  * Module descriptor
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." )
62
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" )
67
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)." )
72
73 static int  Open ( vlc_object_t * );
74 static void Close( vlc_object_t * );
75
76 vlc_module_begin();
77     set_shortname( _("UDP/RTP" ) );
78     set_description( _("UDP/RTP input") );
79     set_category( CAT_INPUT );
80     set_subcategory( SUBCAT_INPUT_ACCESS );
81
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 );
85
86     add_bool( "udp-auto-mtu", 1, NULL,
87               AUTO_MTU_TEXT, AUTO_MTU_LONGTEXT, VLC_TRUE );
88
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( "rtplite" );
99
100     set_callbacks( Open, Close );
101 vlc_module_end();
102
103 /*****************************************************************************
104  * Local prototypes
105  *****************************************************************************/
106 #define RTP_HEADER_LEN 12
107
108 static block_t *BlockUDP( access_t * );
109 static block_t *BlockRTP( access_t * );
110 static block_t *BlockChoose( access_t * );
111 static int Control( access_t *, int, va_list );
112
113 struct access_sys_t
114 {
115     int fd;
116
117     int i_mtu;
118     vlc_bool_t b_auto_mtu;
119
120     /* reorder rtp packets when out-of-sequence */
121     mtime_t i_rtp_late;
122     uint16_t i_last_seqno;
123     block_t *p_list;
124     block_t *p_end;
125 };
126
127 /*****************************************************************************
128  * Open: open the socket
129  *****************************************************************************/
130 static int Open( vlc_object_t *p_this )
131 {
132     access_t     *p_access = (access_t*)p_this;
133     access_sys_t *p_sys;
134
135     char *psz_name = strdup( p_access->psz_path );
136     char *psz_parser;
137     const char *psz_server_addr, *psz_bind_addr = "";
138     int  i_bind_port, i_server_port = 0;
139     int fam = AF_UNSPEC, proto = IPPROTO_UDP, cscov = 8;
140
141     if (strlen (p_access->psz_access) >= 3)
142     {
143         switch (p_access->psz_access[3])
144         {
145             case '4':
146                 fam = AF_INET;
147                 break;
148
149             case '6':
150                 fam = AF_INET6;
151                 break;
152         }
153         if (strcmp (p_access->psz_access + 3, "lite") == 0)
154             proto = IPPROTO_UDPLITE;
155    }
156    if (strncmp (p_access->psz_access, "rtp", 3) == 0)
157         /* Checksum coverage: RTP header is AT LEAST 12 bytes
158          * in addition to UDP header (8 bytes) */
159         cscov += 12;
160
161     i_bind_port = var_CreateGetInteger( p_access, "server-port" );
162
163     /* Parse psz_name syntax :
164      * [serveraddr[:serverport]][@[bindaddr]:[bindport]] */
165     psz_parser = strchr( psz_name, '@' );
166     if( psz_parser != NULL )
167     {
168         /* Found bind address and/or bind port */
169         *psz_parser++ = '\0';
170         psz_bind_addr = psz_parser;
171
172         if( *psz_parser == '[' )
173             /* skips bracket'd IPv6 address */
174             psz_parser = strchr( psz_parser, ']' );
175
176         if( psz_parser != NULL )
177         {
178             psz_parser = strchr( psz_parser, ':' );
179             if( psz_parser != NULL )
180             {
181                 *psz_parser++ = '\0';
182                 i_bind_port = atoi( psz_parser );
183             }
184         }
185     }
186
187     psz_server_addr = psz_name;
188     if( *psz_server_addr == '[' )
189         /* skips bracket'd IPv6 address */
190         psz_parser = strchr( psz_name, ']' );
191
192     if( psz_parser != NULL )
193     {
194         psz_parser = strchr( psz_parser, ':' );
195         if( psz_parser != NULL )
196         {
197             *psz_parser++ = '\0';
198             i_server_port = atoi( psz_parser );
199         }
200     }
201
202     msg_Dbg( p_access, "opening server=%s:%d local=%s:%d",
203              psz_server_addr, i_server_port, psz_bind_addr, i_bind_port );
204
205     /* Set up p_access */
206     access_InitFields( p_access );
207     ACCESS_SET_CALLBACKS( NULL, BlockChoose, Control, NULL );
208     p_access->info.b_prebuffered = VLC_FALSE;
209     MALLOC_ERR( p_access->p_sys, access_sys_t ); p_sys = p_access->p_sys;
210
211     p_sys->fd = net_OpenDgram( p_access, psz_bind_addr, i_bind_port,
212                                psz_server_addr, i_server_port, fam, proto );
213     free (psz_name);
214     if( p_sys->fd == -1 )
215     {
216         msg_Err( p_access, "cannot open socket" );
217         free( p_sys );
218         return VLC_EGENERIC;
219     }
220
221     net_StopSend( p_sys->fd );
222
223 #ifdef UDPLITE_RECV_CSCOV
224     if (proto == IPPROTO_UDPLITE)
225         setsockopt (p_sys->fd, SOL_UDPLITE, UDPLITE_RECV_CSCOV, &cscov, sizeof (cscov));
226 #endif
227
228     /* FIXME */
229     p_sys->i_mtu = var_CreateGetInteger( p_access, "mtu" );
230     if( p_sys->i_mtu <= 1 )
231         p_sys->i_mtu  = 1500;   /* Avoid problem */
232
233     p_sys->b_auto_mtu = var_CreateGetBool( p_access, "udp-auto-mtu" );;
234
235     /* Update default_pts to a suitable value for udp access */
236     var_Create( p_access, "udp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
237
238     /* RTP reordering for out-of-sequence packets */
239     p_sys->i_rtp_late = var_CreateGetInteger( p_access, "rtp-late" ) * 1000;
240     p_sys->i_last_seqno = 0;
241     p_sys->p_list = NULL;
242     p_sys->p_end = NULL;
243     return VLC_SUCCESS;
244 }
245
246 /*****************************************************************************
247  * Close: free unused data structures
248  *****************************************************************************/
249 static void Close( vlc_object_t *p_this )
250 {
251     access_t     *p_access = (access_t*)p_this;
252     access_sys_t *p_sys = p_access->p_sys;
253
254     block_ChainRelease( p_sys->p_list );
255     net_Close( p_sys->fd );
256     free( p_sys );
257 }
258
259 /*****************************************************************************
260  * Control:
261  *****************************************************************************/
262 static int Control( access_t *p_access, int i_query, va_list args )
263 {
264     access_sys_t *p_sys = p_access->p_sys;
265     vlc_bool_t   *pb_bool;
266     int          *pi_int;
267     int64_t      *pi_64;
268
269     switch( i_query )
270     {
271         /* */
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;
278             break;
279         /* */
280         case ACCESS_GET_MTU:
281             pi_int = (int*)va_arg( args, int * );
282             *pi_int = p_sys->i_mtu;
283             break;
284
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;
288             break;
289
290         /* */
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:
296             return VLC_EGENERIC;
297
298         default:
299             msg_Warn( p_access, "unimplemented query in control" );
300             return VLC_EGENERIC;
301
302     }
303     return VLC_SUCCESS;
304 }
305
306 /*****************************************************************************
307  * BlockUDP:
308  *****************************************************************************/
309 static block_t *BlockUDP( access_t *p_access )
310 {
311     access_sys_t *p_sys = p_access->p_sys;
312     block_t      *p_block;
313
314     /* Read data */
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,
318                                   VLC_FALSE );
319     if( p_block->i_buffer <= 0 )
320     {
321         block_Release( p_block );
322         return NULL;
323     }
324
325     if( (p_block->i_buffer >= p_sys->i_mtu) && p_sys->b_auto_mtu &&
326         p_sys->i_mtu < 32767 )
327     {
328         /* Increase by 100% */
329         p_sys->i_mtu *= 2;
330         msg_Dbg( p_access, "increasing MTU to %d", p_sys->i_mtu );
331     }
332
333     return p_block;
334 }
335
336 /*
337  * rtp_ChainInsert - insert a p_block in the chain and
338  * look at the sequence numbers.
339  */
340 static inline vlc_bool_t rtp_ChainInsert( access_t *p_access, block_t *p_block )
341 {
342     access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
343     block_t *p_prev = NULL;
344     block_t *p = p_sys->p_end;
345     uint16_t i_new = (uint16_t) p_block->i_dts;
346     uint16_t i_tmp = 0;
347
348     if( !p_sys->p_list )
349     {
350         p_sys->p_list = p_block;
351         p_sys->p_end = p_block;
352         return VLC_TRUE;
353     }
354     /* walk through the queue from top down since the new packet is in 
355     most cases just appended to the end */
356
357     for( ;; )
358     {
359         i_tmp = i_new - (uint16_t) p->i_dts;
360
361         if( !i_tmp )   /* trash duplicate */
362             break; 
363
364         if ( i_tmp < 32768 )
365         {   /* insert after this block ( i_new > p->i_dts ) */
366             p_block->p_next = p->p_next;
367             p->p_next = p_block;
368             p_block->p_prev = p;
369             if (p_prev)
370             {
371                 p_prev->p_prev = p_block;
372                 msg_Dbg(p_access, "RTP reordering: insert after %d, new %d", 
373                     (uint16_t) p->i_dts, i_new );
374             }
375             else 
376             {
377                 p_sys->p_end = p_block;
378             }
379             return VLC_TRUE;
380         }
381         if( p == p_sys->p_list )
382         {   /* we've reached bottom of chain */
383             i_tmp = p_sys->i_last_seqno - i_new;
384             if( !p_access->info.b_prebuffered || (i_tmp > 32767) )
385             {
386                 msg_Dbg(p_access, "RTP reordering: prepend %d before %d", 
387                         i_new, (uint16_t) p->i_dts );
388                 p_block->p_next = p;
389                 p->p_prev = p_block;
390                 p_sys->p_list = p_block;
391                 return VLC_TRUE;
392             }
393
394             if( !i_tmp )   /* trash duplicate */
395                 break;    
396
397             /* reordering failed - append the packet to the end of queue */
398             msg_Dbg(p_access, "RTP: sequence changed (or buffer too small) "
399                 "new: %d, buffer %d...%d", i_new, (uint16_t) p->i_dts, 
400                 (uint16_t) p_sys->p_end->i_dts);
401             p_sys->p_end->p_next = p_block;
402             p_block->p_prev = p_sys->p_end;
403             p_sys->p_end = p_block;
404             return VLC_TRUE;
405         }
406         p_prev = p;
407         p = p->p_prev;
408     }
409     block_Release( p_block );
410     return VLC_FALSE;
411 }
412
413 /*****************************************************************************
414  * BlockParseRTP/BlockRTP:
415  *****************************************************************************/
416 static block_t *BlockParseRTP( access_t *p_access, block_t *p_block )
417 {
418     int      i_rtp_version;
419     int      i_CSRC_count;
420     int      i_payload_type;
421     int      i_skip = 0;
422     int      i_extension_flag = 0;
423     int      i_extension_length = 0;
424     uint16_t i_sequence_number = 0;
425
426     if( p_block == NULL )
427         return NULL;
428
429     if( p_block->i_buffer < RTP_HEADER_LEN )
430         goto trash;
431
432     /* Parse the header and make some verifications.
433      * See RFC 3550. */
434     i_rtp_version     = ( p_block->p_buffer[0] & 0xC0 ) >> 6;
435     i_CSRC_count      = p_block->p_buffer[0] & 0x0F;
436     i_extension_flag  = p_block->p_buffer[0] & 0x10;
437     i_payload_type    = p_block->p_buffer[1] & 0x7F;
438     i_sequence_number = (p_block->p_buffer[2] << 8 ) + p_block->p_buffer[3];
439
440     if( i_rtp_version != 2 )
441         msg_Dbg( p_access, "RTP version is %u, should be 2", i_rtp_version );
442
443     if( i_payload_type == 14 || i_payload_type == 32)
444         i_skip = 4;
445     else if( i_payload_type !=  33 )
446         msg_Dbg( p_access, "unsupported RTP payload type (%u)", i_payload_type );
447     if( i_extension_flag )
448         i_extension_length = 4 +
449             4 * ( (p_block->p_buffer[14] << 8) + p_block->p_buffer[15] );
450
451     /* Skip header + CSRC extension field n*(32 bits) + extension */
452     i_skip += RTP_HEADER_LEN + 4*i_CSRC_count + i_extension_length;
453
454     if( i_skip >= p_block->i_buffer )
455         goto trash;
456
457     /* Return the packet without the RTP header, remember seqno in i_dts */
458     p_block->i_buffer -= i_skip;
459     p_block->p_buffer += i_skip;
460     p_block->i_pts = mdate();
461     p_block->i_dts = (mtime_t) i_sequence_number;
462
463 #if 0
464     /* Emulate packet loss */
465     if ( (i_sequence_number % 4000) == 0)
466     {
467         msg_Warn( p_access, "Emulating packet drop" );
468         block_Release( p_block );
469         return NULL;
470     }
471 #endif
472
473     return p_block;
474
475 trash:
476     msg_Warn( p_access, "received a too short packet for RTP" );
477     block_Release( p_block );
478     return NULL;
479 }
480
481 static block_t *BlockPrebufferRTP( access_t *p_access, block_t *p_block )
482 {
483     access_sys_t *p_sys = p_access->p_sys;
484     mtime_t   i_first = mdate();
485     int       i_count = 0;
486     block_t   *p = p_block;
487
488     for( ;; )
489     {
490         mtime_t i_date = mdate();
491
492         if( p && rtp_ChainInsert( p_access, p ))
493             i_count++;
494
495         /* Require at least 2 packets in the buffer */
496         if( i_count > 2 && (i_date - i_first) > p_sys->i_rtp_late )
497             break;
498
499         p = BlockParseRTP( p_access, BlockUDP( p_access ));
500         if( !p && (i_date - i_first) > p_sys->i_rtp_late ) 
501         {
502             msg_Err( p_access, "error in RTP prebuffering!" );
503             break;
504         }
505     }
506
507     msg_Dbg( p_access, "RTP: prebuffered %d packets", i_count - 1 );
508     p_access->info.b_prebuffered = VLC_TRUE;
509     p = p_sys->p_list;
510     p_sys->p_list = p_sys->p_list->p_next;
511     p_sys->i_last_seqno = (uint16_t) p->i_dts;
512     p->p_next = NULL;
513     return p;
514 }
515
516 static block_t *BlockRTP( access_t *p_access )
517 {
518     access_sys_t *p_sys = p_access->p_sys;
519     block_t *p;
520
521     while ( !p_sys->p_list || 
522              ( mdate() - p_sys->p_list->i_pts ) < p_sys->i_rtp_late )
523     {
524         p = BlockParseRTP( p_access, BlockUDP( p_access ));
525
526         if ( !p ) 
527             return NULL;
528
529         rtp_ChainInsert( p_access, p );
530     }
531
532     p = p_sys->p_list;
533     p_sys->p_list = p_sys->p_list->p_next;
534     p_sys->i_last_seqno++;
535     if( p_sys->i_last_seqno != (uint16_t) p->i_dts )
536     {
537         msg_Dbg( p_access, "RTP: packet(s) lost, expected %d, got %d",
538                  p_sys->i_last_seqno, (uint16_t) p->i_dts );
539         p_sys->i_last_seqno = (uint16_t) p->i_dts;
540     }
541     p->p_next = NULL;
542     return p;
543 }
544
545 /*****************************************************************************
546  * BlockChoose: decide between RTP and UDP
547  *****************************************************************************/
548 static block_t *BlockChoose( access_t *p_access )
549 {
550     block_t *p_block;
551     int     i_rtp_version;
552     int     i_CSRC_count;
553     int     i_payload_type;
554
555     if( ( p_block = BlockUDP( p_access ) ) == NULL )
556         return NULL;
557
558     if( p_block->p_buffer[0] == 0x47 )
559     {
560         msg_Dbg( p_access, "detected TS over raw UDP" );
561         p_access->pf_block = BlockUDP;
562         p_access->info.b_prebuffered = VLC_TRUE;
563         return p_block;
564     }
565
566     if( p_block->i_buffer < RTP_HEADER_LEN )
567         return p_block;
568
569     /* Parse the header and make some verifications.
570      * See RFC 3550. */
571
572     i_rtp_version  = ( p_block->p_buffer[0] & 0xC0 ) >> 6;
573     i_CSRC_count   = ( p_block->p_buffer[0] & 0x0F );
574     i_payload_type = ( p_block->p_buffer[1] & 0x7F );
575
576     if( i_rtp_version != 2 )
577     {
578         msg_Dbg( p_access, "no supported RTP header detected" );
579         p_access->pf_block = BlockUDP;
580         p_access->info.b_prebuffered = VLC_TRUE;
581         return p_block;
582     }
583
584     switch( i_payload_type )
585     {
586         case 33:
587             msg_Dbg( p_access, "detected TS over RTP" );
588             p_access->psz_demux = strdup( "ts" );
589             break;
590
591         case 14:
592             msg_Dbg( p_access, "detected MPEG audio over RTP" );
593             p_access->psz_demux = strdup( "mpga" );
594             break;
595
596         case 32:
597             msg_Dbg( p_access, "detected MPEG video over RTP" );
598             p_access->psz_demux = strdup( "mpgv" );
599             break;
600
601         default:
602             msg_Dbg( p_access, "no RTP header detected" );
603             p_access->pf_block = BlockUDP;
604             p_access->info.b_prebuffered = VLC_TRUE;
605             return p_block;
606     }
607
608     if( !BlockParseRTP( p_access, p_block )) return NULL;
609
610     p_access->pf_block = BlockRTP;
611
612     return BlockPrebufferRTP( p_access, p_block );
613 }