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