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