]> git.sesse.net Git - vlc/blob - src/stream_output/sap.c
forwardport [17646] (SDP source-filter fix)
[vlc] / src / stream_output / sap.c
1 /*****************************************************************************
2  * sap.c : SAP announce handler
3  *****************************************************************************
4  * Copyright (C) 2002-2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Clément Stenac <zorglub@videolan.org>
8  *          Rémi Denis-Courmont <rem # videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #include <vlc/vlc.h>
30
31 #include <stdlib.h>                                                /* free() */
32 #include <stdio.h>                                              /* sprintf() */
33 #include <string.h>                                            /* strerror() */
34 #include <ctype.h>                                  /* tolower(), isxdigit() */
35
36 #include <vlc/sout.h>
37
38 #include "network.h"
39 #include "charset.h"
40
41 /* SAP is always on that port */
42 #define SAP_PORT 9875
43
44 #define DEFAULT_PORT "1234"
45
46 #undef EXTRA_DEBUG
47
48 /* SAP Specific structures */
49
50 /* 100ms */
51 #define SAP_IDLE ((mtime_t)(0.100*CLOCK_FREQ))
52 #define SAP_MAX_BUFFER 65534
53 #define MIN_INTERVAL 2
54 #define MAX_INTERVAL 300
55
56 /* A SAP announce address. For each of these, we run the
57  * control flow algorithm */
58 struct sap_address_t
59 {
60     char *psz_address;
61     char psz_machine[NI_MAXNUMERICHOST];
62     int i_rfd; /* Read socket */
63     int i_wfd; /* Write socket */
64
65     /* Used for flow control */
66     mtime_t t1;
67     vlc_bool_t b_enabled;
68     vlc_bool_t b_ready;
69     int i_interval;
70     int i_buff;
71     int i_limit;
72 };
73
74 /*****************************************************************************
75  * Local prototypes
76  *****************************************************************************/
77 static void RunThread( vlc_object_t *p_this);
78 static int CalculateRate( sap_handler_t *p_sap, sap_address_t *p_address );
79 static char *SDPGenerate( sap_handler_t *p_sap,
80                           const session_descriptor_t *p_session,
81                           const sap_address_t *p_addr, vlc_bool_t b_ssm );
82
83 static int announce_SendSAPAnnounce( sap_handler_t *p_sap,
84                                      sap_session_t *p_session );
85
86
87 static int announce_SAPAnnounceAdd( sap_handler_t *p_sap,
88                              session_descriptor_t *p_session );
89
90 static int announce_SAPAnnounceDel( sap_handler_t *p_sap,
91                              session_descriptor_t *p_session );
92
93
94 /**
95  * Create the SAP handler
96  *
97  * \param p_announce the parent announce_handler
98  * \return the newly created SAP handler or NULL on error
99  */
100 sap_handler_t *announce_SAPHandlerCreate( announce_handler_t *p_announce )
101 {
102     sap_handler_t *p_sap;
103
104     p_sap = vlc_object_create( p_announce, sizeof( sap_handler_t ) );
105
106     if( !p_sap )
107     {
108         msg_Err( p_announce, "out of memory" );
109         return NULL;
110     }
111
112     vlc_mutex_init( p_sap, &p_sap->object_lock );
113
114     p_sap->pf_add = announce_SAPAnnounceAdd;
115     p_sap->pf_del = announce_SAPAnnounceDel;
116
117     p_sap->i_sessions = 0;
118     p_sap->i_addresses = 0;
119     p_sap->i_current_session = 0;
120
121     p_sap->b_control = config_GetInt( p_sap, "sap-flow-control");
122
123     if( vlc_thread_create( p_sap, "sap handler", RunThread,
124                        VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
125     {
126         msg_Dbg( p_announce, "unable to spawn SAP handler thread");
127         free( p_sap );
128         return NULL;
129     };
130     msg_Dbg( p_announce, "thread created, %i sessions", p_sap->i_sessions);
131     return p_sap;
132 }
133
134 /**
135  *  Destroy the SAP handler
136  *  \param p_this the SAP Handler to destroy
137  *  \return nothing
138  */
139 void announce_SAPHandlerDestroy( sap_handler_t *p_sap )
140 {
141     int i;
142
143     vlc_mutex_destroy( &p_sap->object_lock );
144
145     /* Free the remaining sessions */
146     for( i = 0 ; i< p_sap->i_sessions ; i++)
147     {
148         sap_session_t *p_session = p_sap->pp_sessions[i];
149         FREENULL( p_session->psz_sdp );
150         FREENULL( p_session->psz_data );
151         REMOVE_ELEM( p_sap->pp_sessions, p_sap->i_sessions , i );
152         FREENULL( p_session );
153     }
154
155     /* Free the remaining addresses */
156     for( i = 0 ; i< p_sap->i_addresses ; i++)
157     {
158         sap_address_t *p_address = p_sap->pp_addresses[i];
159         FREENULL( p_address->psz_address );
160         if( p_address->i_rfd > -1 )
161         {
162             net_Close( p_address->i_rfd );
163         }
164         if( p_address->i_wfd > -1 && p_sap->b_control )
165         {
166             net_Close( p_address->i_wfd );
167         }
168         REMOVE_ELEM( p_sap->pp_addresses, p_sap->i_addresses, i );
169         FREENULL( p_address );
170     }
171
172     /* Free the structure */
173     vlc_object_destroy( p_sap );
174 }
175
176 /**
177  * main SAP handler thread
178  * \param p_this the SAP Handler object
179  * \return nothing
180  */
181 static void RunThread( vlc_object_t *p_this)
182 {
183     sap_handler_t *p_sap = (sap_handler_t*)p_this;
184     sap_session_t *p_session;
185
186     while( !p_sap->b_die )
187     {
188         int i;
189
190         /* If needed, get the rate info */
191         if( p_sap->b_control == VLC_TRUE )
192         {
193             for( i = 0 ; i< p_sap->i_addresses ; i++)
194             {
195                 if( p_sap->pp_addresses[i]->b_enabled == VLC_TRUE )
196                 {
197                     CalculateRate( p_sap, p_sap->pp_addresses[i] );
198                 }
199             }
200         }
201
202         /* Find the session to announce */
203         vlc_mutex_lock( &p_sap->object_lock );
204         if( p_sap->i_sessions > p_sap->i_current_session + 1)
205         {
206             p_sap->i_current_session++;
207         }
208         else if( p_sap->i_sessions > 0)
209         {
210             p_sap->i_current_session = 0;
211         }
212         else
213         {
214             vlc_mutex_unlock( &p_sap->object_lock );
215             msleep( SAP_IDLE );
216             continue;
217         }
218         p_session = p_sap->pp_sessions[p_sap->i_current_session];
219         vlc_mutex_unlock( &p_sap->object_lock );
220
221         /* And announce it */
222         if( p_session->p_address->b_enabled == VLC_TRUE &&
223             p_session->p_address->b_ready == VLC_TRUE )
224         {
225             announce_SendSAPAnnounce( p_sap, p_session );
226         }
227
228         msleep( SAP_IDLE );
229     }
230 }
231
232 /* Add a SAP announce */
233 static int announce_SAPAnnounceAdd( sap_handler_t *p_sap,
234                              session_descriptor_t *p_session )
235 {
236     int i_header_size, i;
237     char *psz_head, psz_addr[NI_MAXNUMERICHOST];
238     vlc_bool_t b_ipv6 = VLC_FALSE, b_ssm = VLC_FALSE;
239     sap_session_t *p_sap_session;
240     mtime_t i_hash;
241     struct addrinfo hints, *res;
242     struct sockaddr_storage addr;
243     socklen_t addrlen;
244
245     vlc_mutex_lock( &p_sap->object_lock );
246
247     if( p_session->psz_uri == NULL )
248     {
249         vlc_mutex_unlock( &p_sap->object_lock );
250         msg_Err( p_sap, "*FIXME* unexpected NULL URI for SAP announce" );
251         msg_Err( p_sap, "This should not happen. VLC needs fixing." );
252         return VLC_EGENERIC;
253     }
254
255     /* Determine SAP multicast address automatically */
256     memset( &hints, 0, sizeof( hints ) );
257     hints.ai_socktype = SOCK_DGRAM;
258     hints.ai_flags = AI_NUMERICHOST;
259
260     i = vlc_getaddrinfo( (vlc_object_t *)p_sap, p_session->psz_uri, 0,
261                          &hints, &res );
262     if( i )
263     {
264         vlc_mutex_unlock( &p_sap->object_lock );
265         msg_Err( p_sap, "Invalid URI for SAP announce: %s: %s",
266                  p_session->psz_uri, vlc_gai_strerror( i ) );
267         return VLC_EGENERIC;
268     }
269
270     addrlen = res->ai_addrlen;
271     if ((unsigned)addrlen > sizeof (addr))
272     {
273         vlc_mutex_unlock( &p_sap->object_lock );
274         vlc_freeaddrinfo( res );
275         msg_Err( p_sap, "Unsupported address family of size %d > %u",
276                  res->ai_addrlen, (unsigned) sizeof( addr ) );
277         return VLC_EGENERIC;
278     }
279
280     memcpy (&addr, res->ai_addr, addrlen);
281     vlc_freeaddrinfo (res);
282
283     switch( addr.ss_family )
284     {
285 #if defined (HAVE_INET_PTON) || defined (WIN32)
286         case AF_INET6:
287         {
288             /* See RFC3513 for list of valid IPv6 scopes */
289             struct in6_addr *a6 = &((struct sockaddr_in6 *)&addr)->sin6_addr;
290
291             memcpy( a6->s6_addr + 2, "\x00\x00\x00\x00\x00\x00"
292                    "\x00\x00\x00\x00\x00\x02\x7f\xfe", 14 );
293             if( IN6_IS_ADDR_MULTICAST( a6 ) )
294             {
295                 /* force reserved bits in flags to zero, preserve scope */
296                 a6->s6_addr[1] &= 0x3f;
297
298                 /* SSM <=> ff3x::/32 */
299                 b_ssm = (GetDWLE (a6->s6_addr) & 0xfff0ffff) == 0xff300000;
300             }
301             else
302                 /* Unicast IPv6 - assume global scope */
303                 memcpy( a6->s6_addr, "\xff\x0e", 2 );
304
305             b_ipv6 = VLC_TRUE;
306             break;
307         }
308 #endif
309
310         case AF_INET:
311         {
312             /* See RFC2365 for IPv4 scopes */
313             uint32_t ipv4;
314
315             ipv4 = ntohl( ((struct sockaddr_in *)&addr)->sin_addr.s_addr );
316             /* 224.0.0.0/24 => 224.0.0.255 */
317             if ((ipv4 & 0xffffff00) == 0xe0000000)
318                 ipv4 =  0xe00000ff;
319             else
320             /* 239.255.0.0/16 => 239.255.255.255 */
321             if ((ipv4 & 0xffff0000) == 0xefff0000)
322                 ipv4 =  0xefffffff;
323             else
324             /* 239.192.0.0/14 => 239.195.255.255 */
325             if ((ipv4 & 0xfffc0000) == 0xefc00000)
326                 ipv4 =  0xefc3ffff;
327             else
328             if ((ipv4 & 0xff000000) == 0xef000000)
329                 ipv4 = 0;
330             else
331             /* other addresses => 224.2.127.254 */
332             {
333                 /* SSM: 232.0.0.0/8 */
334                 b_ssm = (ipv4 >> 24) == 232;
335                 ipv4 = 0xe0027ffe;
336             }
337
338             if( ipv4 == 0 )
339             {
340                 msg_Err( p_sap, "Out-of-scope multicast address "
341                         "not supported by SAP: %s", p_session->psz_uri );
342                 vlc_mutex_unlock( &p_sap->object_lock );
343                 return VLC_EGENERIC;
344             }
345
346             ((struct sockaddr_in *)&addr)->sin_addr.s_addr = htonl( ipv4 );
347             break;
348         }
349
350         default:
351             vlc_mutex_unlock( &p_sap->object_lock );
352             msg_Err( p_sap, "Address family %d not supported by SAP",
353                      addr.ss_family );
354             return VLC_EGENERIC;
355     }
356
357     i = vlc_getnameinfo( (struct sockaddr *)&addr, addrlen,
358                          psz_addr, sizeof( psz_addr ), NULL, NI_NUMERICHOST );
359
360     if( i )
361     {
362         vlc_mutex_unlock( &p_sap->object_lock );
363         msg_Err( p_sap, "%s", vlc_gai_strerror( i ) );
364         return VLC_EGENERIC;
365     }
366
367     msg_Dbg( p_sap, "using SAP address: %s", psz_addr);
368
369     /* XXX: Check for dupes */
370     p_sap_session = (sap_session_t*)malloc(sizeof(sap_session_t));
371     p_sap_session->p_address = NULL;
372
373     /* Add the address to the buffer */
374     for( i = 0; i < p_sap->i_addresses; i++)
375     {
376         if( !strcmp( psz_addr, p_sap->pp_addresses[i]->psz_address ) )
377         {
378             p_sap_session->p_address = p_sap->pp_addresses[i];
379             break;
380         }
381     }
382
383     if( p_sap_session->p_address == NULL )
384     {
385         sap_address_t *p_address = (sap_address_t *)
386                                     malloc( sizeof(sap_address_t) );
387         if( !p_address )
388         {
389             msg_Err( p_sap, "out of memory" );
390             return VLC_ENOMEM;
391         }
392         p_address->psz_address = strdup( psz_addr );
393         p_address->i_wfd = net_ConnectUDP( p_sap, psz_addr, SAP_PORT, 255 );
394         if( p_address->i_wfd != -1 )
395         {
396             char *ptr;
397
398             net_StopRecv( p_address->i_wfd );
399             net_GetSockAddress( p_address->i_wfd, p_address->psz_machine,
400                                 NULL );
401
402             /* removes scope if present */
403             ptr = strchr( p_address->psz_machine, '%' );
404             if( ptr != NULL )
405                 *ptr = '\0';
406         }
407
408         if( p_sap->b_control == VLC_TRUE )
409         {
410             p_address->i_rfd = net_ListenUDP1( (vlc_object_t*)p_sap, psz_addr, SAP_PORT );
411             if( p_address->i_rfd != -1 )
412                 net_StopSend( p_address->i_rfd );
413             p_address->i_buff = 0;
414             p_address->b_enabled = VLC_TRUE;
415             p_address->b_ready = VLC_FALSE;
416             p_address->i_limit = 10000; /* 10000 bps */
417             p_address->t1 = 0;
418         }
419         else
420         {
421             p_address->b_enabled = VLC_TRUE;
422             p_address->b_ready = VLC_TRUE;
423             p_address->i_interval = config_GetInt( p_sap,"sap-interval");
424             p_address->i_rfd = -1;
425         }
426
427         if( p_address->i_wfd == -1 || (p_address->i_rfd == -1
428                                         && p_sap->b_control ) )
429         {
430             msg_Warn( p_sap, "disabling address" );
431             p_address->b_enabled = VLC_FALSE;
432         }
433
434         INSERT_ELEM( p_sap->pp_addresses,
435                      p_sap->i_addresses,
436                      p_sap->i_addresses,
437                      p_address );
438         p_sap_session->p_address = p_address;
439     }
440
441
442     /* Build the SAP Headers */
443     i_header_size = ( b_ipv6 ? 16 : 4 ) + 20;
444     psz_head = (char *) malloc( i_header_size * sizeof( char ) );
445     if( psz_head == NULL )
446     {
447         msg_Err( p_sap, "out of memory" );
448         return VLC_ENOMEM;
449     }
450
451     /* SAPv1, not encrypted, not compressed */
452     psz_head[0] = b_ipv6 ? 0x30 : 0x20;
453     psz_head[1] = 0x00; /* No authentification length */
454
455     i_hash = mdate();
456     psz_head[2] = (i_hash & 0xFF00) >> 8; /* Msg id hash */
457     psz_head[3] = (i_hash & 0xFF);        /* Msg id hash 2 */
458
459 #if defined (HAVE_INET_PTON) || defined (WIN32)
460     if( b_ipv6 )
461     {
462         inet_pton( AF_INET6, /* can't fail */
463                    p_sap_session->p_address->psz_machine,
464                    psz_head + 4 );
465     }
466     else
467 #endif
468     {
469         inet_pton( AF_INET, /* can't fail */
470                    p_sap_session->p_address->psz_machine,
471                    psz_head + 4 );
472     }
473
474     memcpy( psz_head + (b_ipv6 ? 20 : 8), "application/sdp", 15 );
475
476     /* If needed, build the SDP */
477     if( p_session->psz_sdp == NULL )
478     {
479         p_session->psz_sdp = SDPGenerate( p_sap, p_session,
480                                           p_sap_session->p_address, b_ssm );
481         if( p_session->psz_sdp == NULL )
482         {
483             vlc_mutex_unlock( &p_sap->object_lock );
484             return VLC_ENOMEM;
485         }
486     }
487
488     p_sap_session->psz_sdp = strdup( p_session->psz_sdp );
489     p_sap_session->i_last = 0;
490
491     psz_head[ i_header_size-1 ] = '\0';
492     p_sap_session->i_length = i_header_size + strlen( p_sap_session->psz_sdp);
493
494     p_sap_session->psz_data = (uint8_t *)malloc( sizeof(char)*
495                                                  p_sap_session->i_length );
496
497     /* Build the final message */
498     memcpy( p_sap_session->psz_data, psz_head, i_header_size );
499     memcpy( p_sap_session->psz_data+i_header_size, p_sap_session->psz_sdp,
500             strlen( p_sap_session->psz_sdp) );
501
502     free( psz_head );
503
504     /* Enqueue the announce */
505     INSERT_ELEM( p_sap->pp_sessions,
506                  p_sap->i_sessions,
507                  p_sap->i_sessions,
508                  p_sap_session );
509     msg_Dbg( p_sap,"%i addresses, %i sessions",
510                    p_sap->i_addresses,p_sap->i_sessions);
511
512     /* Remember the SAP session for later deletion */
513     p_session->p_sap = p_sap_session;
514
515     vlc_mutex_unlock( &p_sap->object_lock );
516
517     return VLC_SUCCESS;
518 }
519
520 /* Remove a SAP Announce */
521 static int announce_SAPAnnounceDel( sap_handler_t *p_sap,
522                              session_descriptor_t *p_session )
523 {
524     int i;
525     vlc_mutex_lock( &p_sap->object_lock );
526
527     msg_Dbg( p_sap,"removing SAP announce %p",p_session->p_sap);
528
529     /* Dequeue the announce */
530     for( i = 0; i< p_sap->i_sessions; i++)
531     {
532         if( p_session->p_sap == p_sap->pp_sessions[i] )
533         {
534             REMOVE_ELEM( p_sap->pp_sessions,
535                          p_sap->i_sessions,
536                          i );
537
538             FREENULL( p_session->p_sap->psz_sdp );
539             FREENULL( p_session->p_sap->psz_data );
540             free( p_session->p_sap );
541             break;
542         }
543     }
544
545     /* XXX: Dequeue the address too if it is not used anymore
546      * TODO: - address refcount
547              - send a SAP deletion packet */
548
549     msg_Dbg( p_sap,"%i announcements remaining", p_sap->i_sessions );
550
551     vlc_mutex_unlock( &p_sap->object_lock );
552
553     return VLC_SUCCESS;
554 }
555
556 static int announce_SendSAPAnnounce( sap_handler_t *p_sap,
557                                      sap_session_t *p_session )
558 {
559     int i_ret;
560
561     /* This announce has never been sent yet */
562     if( p_session->i_last == 0 )
563     {
564         p_session->i_next = mdate()+ p_session->p_address->i_interval*1000000;
565         p_session->i_last = 1;
566         return VLC_SUCCESS;
567     }
568
569     if( p_session->i_next < mdate() )
570     {
571 #ifdef EXTRA_DEBUG
572         msg_Dbg( p_sap, "sending announce");
573 #endif
574         i_ret = net_Write( p_sap, p_session->p_address->i_wfd, NULL,
575                            p_session->psz_data,
576                            p_session->i_length );
577         if( i_ret != (int)p_session->i_length )
578         {
579             msg_Warn( p_sap, "SAP send failed on address %s (%i %i)",
580                       p_session->p_address->psz_address,
581                       i_ret, p_session->i_length );
582         }
583         p_session->i_last = p_session->i_next;
584         p_session->i_next = p_session->i_last
585                             + p_session->p_address->i_interval*1000000;
586     }
587     else
588     {
589         return VLC_SUCCESS;
590     }
591     return VLC_SUCCESS;
592 }
593
594 static char *SDPGenerate( sap_handler_t *p_sap,
595                           const session_descriptor_t *p_session,
596                           const sap_address_t *p_addr, vlc_bool_t b_ssm )
597 {
598     int64_t i_sdp_id = mdate();
599     int     i_sdp_version = 1 + p_sap->i_sessions + (rand()&0xfff);
600     char *psz_group, *psz_name, psz_uribuf[NI_MAXNUMERICHOST], *psz_uri,
601          *psz_sdp;
602     char ipv;
603
604     psz_group = p_session->psz_group;
605     psz_name = p_session->psz_name;
606
607     /* FIXME: really check that psz_uri is a real IP address
608      * FIXME: make a common function to obtain a canonical IP address */
609     ipv = ( strchr( p_session->psz_uri, ':' )  != NULL) ? '6' : '4';
610     if( *p_session->psz_uri == '[' )
611     {
612         char *ptr;
613
614         strlcpy( psz_uribuf, p_session->psz_uri + 1, sizeof( psz_uribuf ) );
615         ptr = strchr( psz_uribuf, '%' );
616         if( ptr != NULL)
617             *ptr = '\0';
618         ptr = strchr( psz_uribuf, ']' );
619         if( ptr != NULL)
620             *ptr = '\0';
621         psz_uri = psz_uribuf;
622     }
623     else
624         psz_uri = p_session->psz_uri;
625
626     char *sfilter = NULL;
627     if (b_ssm)
628     {
629         if (asprintf (&sfilter, "a=source-filter: incl IN IP%c * %s\r\n",
630                       ipv, p_addr->psz_machine) == -1)
631             return NULL;
632     }
633
634     /* see the lists in modules/stream_out/rtp.c for compliance stuff */
635     int res = asprintf (&psz_sdp,
636                         "v=0\r\n"
637                         "o=- "I64Fd" %d IN IP%c %s\r\n"
638                         "s=%s\r\n"
639                         "c=IN IP%c %s/%d\r\n"
640                         "t=0 0\r\n"
641                         "a=tool:"PACKAGE_STRING"\r\n"
642                         "a=recvonly\r\n"
643                         "a=type:broadcast\n"
644                         "%s"
645                         "m=video %d %s %d\r\n"
646                         "%s%s%s",
647                         i_sdp_id, i_sdp_version,
648                         ipv, p_addr->psz_machine,
649                         psz_name, ipv, psz_uri,
650                         (p_session->i_ttl != -1) ? p_session->i_ttl : 255,
651                         (sfilter != NULL) ? sfilter : "",
652                         p_session->i_port,
653                         p_session->b_rtp ? "RTP/AVP" : "udp",
654                         p_session->i_payload,
655                         psz_group ? "a=x-plgroup:" : "",
656                         psz_group ? psz_group : "", psz_group ? "\r\n" : "");
657     if (sfilter != NULL)
658         free (sfilter);
659
660     if (res == -1)
661         return NULL;
662
663     msg_Dbg( p_sap, "Generated SDP (%i bytes):\n%s", strlen(psz_sdp),
664              psz_sdp );
665     return psz_sdp;
666 }
667
668 static int CalculateRate( sap_handler_t *p_sap, sap_address_t *p_address )
669 {
670     int i_read;
671     uint8_t buffer[SAP_MAX_BUFFER];
672     int i_tot = 0;
673     mtime_t i_temp;
674     int i_rate;
675
676     if( p_address->t1 == 0 )
677     {
678         p_address->t1 = mdate();
679         return VLC_SUCCESS;
680     }
681     do
682     {
683         /* Might be too slow if we have huge data */
684         i_read = net_ReadNonBlock( p_sap, p_address->i_rfd, NULL, buffer,
685                                    SAP_MAX_BUFFER, 0 );
686         i_tot += i_read;
687     } while( i_read > 0 );
688
689     i_temp = mdate();
690
691     /* We calculate the rate every 5 seconds */
692     if( i_temp - p_address->t1 < 5000000 )
693     {
694         p_address->i_buff += i_tot;
695         return VLC_SUCCESS;
696     }
697
698     /* Bits/second */
699     i_rate = (int)(8*1000000*((mtime_t)p_address->i_buff + (mtime_t)i_tot ) /
700                         (i_temp - p_address->t1 ));
701
702     p_address->i_limit = 10000;
703
704     p_address->i_interval = ((1000*i_rate / p_address->i_limit) *
705                             (MAX_INTERVAL - MIN_INTERVAL))/1000 + MIN_INTERVAL;
706
707     if( p_address->i_interval > MAX_INTERVAL || p_address->i_interval < 0 )
708     {
709         p_address->i_interval = MAX_INTERVAL;
710     }
711 #ifdef EXTRA_DEBUG
712     msg_Dbg( p_sap,"%s:%i: rate=%i, interval = %i s",
713              p_address->psz_address,SAP_PORT, i_rate, p_address->i_interval );
714 #endif
715
716     p_address->b_ready = VLC_TRUE;
717
718     p_address->t1 = i_temp;
719     p_address->i_buff = 0;
720
721     return VLC_SUCCESS;
722 }