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