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