Main Page | Modules | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

sap.c

00001 /*****************************************************************************
00002  * sap.c : SAP announce handler
00003  *****************************************************************************
00004  * Copyright (C) 2002-2005 the VideoLAN team
00005  * $Id: sap.c 12929 2005-10-23 10:06:49Z courmisch $
00006  *
00007  * Authors: Clément Stenac <[email protected]>
00008  *          Rémi Denis-Courmont <rem # videolan.org>
00009  *
00010  * This program is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version.
00014  *
00015  * This program is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License
00021  * along with this program; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
00023  *****************************************************************************/
00024 
00025 /*****************************************************************************
00026  * Preamble
00027  *****************************************************************************/
00028 #include <stdlib.h>                                                /* free() */
00029 #include <stdio.h>                                              /* sprintf() */
00030 #include <string.h>                                            /* strerror() */
00031 #include <ctype.h>                                  /* tolower(), isxdigit() */
00032 
00033 #include <vlc/vlc.h>
00034 #include <vlc/sout.h>
00035 
00036 #include "network.h"
00037 #include "charset.h"
00038 
00039 /* SAP is always on that port */
00040 #define SAP_PORT 9875
00041 
00042 #define DEFAULT_PORT "1234"
00043 
00044 #undef EXTRA_DEBUG
00045 
00046 /* SAP Specific structures */
00047 
00048 /* 100ms */
00049 #define SAP_IDLE ((mtime_t)(0.100*CLOCK_FREQ))
00050 #define SAP_MAX_BUFFER 65534
00051 #define MIN_INTERVAL 2
00052 #define MAX_INTERVAL 300
00053 
00054 /* A SAP announce address. For each of these, we run the
00055  * control flow algorithm */
00056 struct sap_address_t
00057 {
00058     char *psz_address;
00059     char psz_machine[NI_MAXNUMERICHOST];
00060     int i_port;
00061     int i_rfd; /* Read socket */
00062     int i_wfd; /* Write socket */
00063 
00064     /* Used for flow control */
00065     mtime_t t1;
00066     vlc_bool_t b_enabled;
00067     vlc_bool_t b_ready;
00068     int i_interval;
00069     int i_buff;
00070     int i_limit;
00071 };
00072 
00073 /*****************************************************************************
00074  * Local prototypes
00075  *****************************************************************************/
00076 static void RunThread( vlc_object_t *p_this);
00077 static int CalculateRate( sap_handler_t *p_sap, sap_address_t *p_address );
00078 static char *SDPGenerate( sap_handler_t *p_sap,
00079                           const session_descriptor_t *p_session,
00080                           const sap_address_t *p_addr );
00081 
00082 static int announce_SendSAPAnnounce( sap_handler_t *p_sap,
00083                                      sap_session_t *p_session );
00084 
00085 
00086 static int announce_SAPAnnounceAdd( sap_handler_t *p_sap,
00087                              session_descriptor_t *p_session );
00088 
00089 static int announce_SAPAnnounceDel( sap_handler_t *p_sap,
00090                              session_descriptor_t *p_session );
00091 
00092 #define FREE( p ) if( p ) { free( p ); (p) = NULL; }
00093 
00094 
00101 sap_handler_t *announce_SAPHandlerCreate( announce_handler_t *p_announce )
00102 {
00103     sap_handler_t *p_sap;
00104 
00105     p_sap = vlc_object_create( p_announce, sizeof( sap_handler_t ) );
00106 
00107     if( !p_sap )
00108     {
00109         msg_Err( p_announce, "out of memory" );
00110         return NULL;
00111     }
00112 
00113     vlc_mutex_init( p_sap, &p_sap->object_lock );
00114 
00115     p_sap->pf_add = announce_SAPAnnounceAdd;
00116     p_sap->pf_del = announce_SAPAnnounceDel;
00117 
00118     p_sap->i_sessions = 0;
00119     p_sap->i_addresses = 0;
00120     p_sap->i_current_session = 0;
00121 
00122     p_sap->b_control = config_GetInt( p_sap, "sap-flow-control");
00123 
00124     if( vlc_thread_create( p_sap, "sap handler", RunThread,
00125                        VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
00126     {
00127         msg_Dbg( p_announce, "Unable to spawn SAP handler thread");
00128         free( p_sap );
00129         return NULL;
00130     };
00131     msg_Dbg( p_announce, "thread created, %i sessions", p_sap->i_sessions);
00132     return p_sap;
00133 }
00134 
00140 void announce_SAPHandlerDestroy( sap_handler_t *p_sap )
00141 {
00142     int i;
00143 
00144     vlc_mutex_destroy( &p_sap->object_lock );
00145 
00146     /* Free the remaining sessions */
00147     for( i = 0 ; i< p_sap->i_sessions ; i++)
00148     {
00149         sap_session_t *p_session = p_sap->pp_sessions[i];
00150         FREE( p_session->psz_sdp );
00151         FREE( p_session->psz_data );
00152         REMOVE_ELEM( p_sap->pp_sessions, p_sap->i_sessions , i );
00153         FREE( p_session );
00154     }
00155 
00156     /* Free the remaining addresses */
00157     for( i = 0 ; i< p_sap->i_addresses ; i++)
00158     {
00159         sap_address_t *p_address = p_sap->pp_addresses[i];
00160         FREE( p_address->psz_address );
00161         if( p_address->i_rfd > -1 )
00162         {
00163             net_Close( p_address->i_rfd );
00164         }
00165         if( p_address->i_wfd > -1 && p_sap->b_control )
00166         {
00167             net_Close( p_address->i_wfd );
00168         }
00169         REMOVE_ELEM( p_sap->pp_addresses, p_sap->i_addresses, i );
00170         FREE( p_address );
00171     }
00172 
00173     /* Free the structure */
00174     vlc_object_destroy( p_sap );
00175 }
00176 
00182 static void RunThread( vlc_object_t *p_this)
00183 {
00184     sap_handler_t *p_sap = (sap_handler_t*)p_this;
00185     sap_session_t *p_session;
00186 
00187     while( !p_sap->b_die )
00188     {
00189         int i;
00190 
00191         /* If needed, get the rate info */
00192         if( p_sap->b_control == VLC_TRUE )
00193         {
00194             for( i = 0 ; i< p_sap->i_addresses ; i++)
00195             {
00196                 if( p_sap->pp_addresses[i]->b_enabled == VLC_TRUE )
00197                 {
00198                     CalculateRate( p_sap, p_sap->pp_addresses[i] );
00199                 }
00200             }
00201         }
00202 
00203         /* Find the session to announce */
00204         vlc_mutex_lock( &p_sap->object_lock );
00205         if( p_sap->i_sessions > p_sap->i_current_session + 1)
00206         {
00207             p_sap->i_current_session++;
00208         }
00209         else if( p_sap->i_sessions > 0)
00210         {
00211             p_sap->i_current_session = 0;
00212         }
00213         else
00214         {
00215             vlc_mutex_unlock( &p_sap->object_lock );
00216             msleep( SAP_IDLE );
00217             continue;
00218         }
00219         p_session = p_sap->pp_sessions[p_sap->i_current_session];
00220         vlc_mutex_unlock( &p_sap->object_lock );
00221 
00222         /* And announce it */
00223         if( p_session->p_address->b_enabled == VLC_TRUE &&
00224             p_session->p_address->b_ready == VLC_TRUE )
00225         {
00226             announce_SendSAPAnnounce( p_sap, p_session );
00227         }
00228 
00229         msleep( SAP_IDLE );
00230     }
00231 }
00232 
00233 /* Add a SAP announce */
00234 static int announce_SAPAnnounceAdd( sap_handler_t *p_sap,
00235                              session_descriptor_t *p_session )
00236 {
00237     int i_header_size, i;
00238     char *psz_head, psz_addr[NI_MAXNUMERICHOST];
00239     vlc_bool_t b_ipv6 = VLC_FALSE;
00240     sap_session_t *p_sap_session;
00241     mtime_t i_hash;
00242     struct addrinfo hints, *res;
00243     struct sockaddr_storage addr;
00244 
00245     vlc_mutex_lock( &p_sap->object_lock );
00246 
00247     if( p_session->psz_uri == NULL )
00248     {
00249         vlc_mutex_unlock( &p_sap->object_lock );
00250         msg_Err( p_sap, "*FIXME* Unexpected NULL URI for SAP announce" );
00251         msg_Err( p_sap, "This should not happen. VLC needs fixing." );
00252         return VLC_EGENERIC;
00253     }
00254 
00255     /* Determine SAP multicast address automatically */
00256     memset( &hints, 0, sizeof( hints ) );
00257     hints.ai_socktype = SOCK_DGRAM;
00258     hints.ai_flags = AI_NUMERICHOST;
00259 
00260     i = vlc_getaddrinfo( (vlc_object_t *)p_sap, p_session->psz_uri, 0,
00261                          &hints, &res );
00262     if( i )
00263     {
00264         vlc_mutex_unlock( &p_sap->object_lock );
00265         msg_Err( p_sap, "Invalid URI for SAP announce: %s: %s",
00266                  p_session->psz_uri, vlc_gai_strerror( i ) );
00267         return VLC_EGENERIC;
00268     }
00269 
00270     if( (unsigned)res->ai_addrlen > sizeof( addr ) )
00271     {
00272         vlc_mutex_unlock( &p_sap->object_lock );
00273         vlc_freeaddrinfo( res );
00274         msg_Err( p_sap, "Unsupported address family of size %d > %u",
00275                  res->ai_addrlen, (unsigned) sizeof( addr ) );
00276         return VLC_EGENERIC;
00277     }
00278 
00279     memcpy( &addr, res->ai_addr, res->ai_addrlen );
00280 
00281     switch( addr.ss_family )
00282     {
00283 #if defined (HAVE_INET_PTON) || defined (WIN32)
00284         case AF_INET6:
00285         {
00286             /* See RFC3513 for list of valid IPv6 scopes */
00287             struct in6_addr *a6 = &((struct sockaddr_in6 *)&addr)->sin6_addr;
00288 
00289             memcpy( a6->s6_addr + 2, "\x00\x00\x00\x00\x00\x00"
00290                    "\x00\x00\x00\x00\x00\x02\x7f\xfe", 14 );
00291             if( IN6_IS_ADDR_MULTICAST( a6 ) )
00292                  /* force flags to zero, preserve scope */
00293                 a6->s6_addr[1] &= 0xf;
00294             else
00295                 /* Unicast IPv6 - assume global scope */
00296                 memcpy( a6->s6_addr, "\xff\x0e", 2 );
00297 
00298             b_ipv6 = VLC_TRUE;
00299             break;
00300         }
00301 #endif
00302 
00303         case AF_INET:
00304         {
00305             /* See RFC2365 for IPv4 scopes */
00306             uint32_t ipv4;
00307 
00308             ipv4 = ntohl( ((struct sockaddr_in *)&addr)->sin_addr.s_addr );
00309             /* 224.0.0.0/24 => 224.0.0.255 */
00310             if ((ipv4 & 0xffffff00) == 0xe0000000)
00311                 ipv4 =  0xe00000ff;
00312             else
00313             /* 239.255.0.0/16 => 239.255.255.255 */
00314             if ((ipv4 & 0xffff0000) == 0xefff0000)
00315                 ipv4 =  0xefffffff;
00316             else
00317             /* 239.192.0.0/14 => 239.195.255.255 */
00318             if ((ipv4 & 0xfffc0000) == 0xefc00000)
00319                 ipv4 =  0xefc3ffff;
00320             else
00321             /* other addresses => 224.2.127.254 */
00322                 ipv4 = 0xe0027ffe;
00323 
00324             ((struct sockaddr_in *)&addr)->sin_addr.s_addr = htonl( ipv4 );
00325             break;
00326         }
00327 
00328         default:
00329             vlc_mutex_unlock( &p_sap->object_lock );
00330             vlc_freeaddrinfo( res );
00331             msg_Err( p_sap, "Address family %d not supported by SAP",
00332                      addr.ss_family );
00333             return VLC_EGENERIC;
00334     }
00335 
00336     i = vlc_getnameinfo( (struct sockaddr *)&addr, res->ai_addrlen,
00337                          psz_addr, sizeof( psz_addr ), NULL, NI_NUMERICHOST );
00338     vlc_freeaddrinfo( res );
00339 
00340     if( i )
00341     {
00342         vlc_mutex_unlock( &p_sap->object_lock );
00343         msg_Err( p_sap, "%s", vlc_gai_strerror( i ) );
00344         return VLC_EGENERIC;
00345     }
00346 
00347     msg_Dbg( p_sap, "using SAP address: %s", psz_addr);
00348 
00349     /* XXX: Check for dupes */
00350     p_sap_session = (sap_session_t*)malloc(sizeof(sap_session_t));
00351     p_sap_session->p_address = NULL;
00352 
00353     /* Add the address to the buffer */
00354     for( i = 0; i < p_sap->i_addresses; i++)
00355     {
00356         if( !strcmp( psz_addr, p_sap->pp_addresses[i]->psz_address ) )
00357         {
00358             p_sap_session->p_address = p_sap->pp_addresses[i];
00359             break;
00360         }
00361     }
00362 
00363     if( p_sap_session->p_address == NULL )
00364     {
00365         sap_address_t *p_address = (sap_address_t *)
00366                                     malloc( sizeof(sap_address_t) );
00367         if( !p_address )
00368         {
00369             msg_Err( p_sap, "out of memory" );
00370             return VLC_ENOMEM;
00371         }
00372         p_address->psz_address = strdup( psz_addr );
00373         p_address->i_port  =  9875;
00374         p_address->i_wfd = net_OpenUDP( p_sap, "", 0, psz_addr,
00375                                         p_address->i_port );
00376         if( p_address->i_wfd != -1 )
00377         {
00378             char *ptr;
00379 
00380             net_StopRecv( p_address->i_wfd );
00381             net_GetSockAddress( p_address->i_wfd, p_address->psz_machine,
00382                                 NULL );
00383 
00384             /* removes scope if present */
00385             ptr = strchr( p_address->psz_machine, '%' );
00386             if( ptr != NULL )
00387                 *ptr = '\0';
00388         }
00389 
00390         if( p_sap->b_control == VLC_TRUE )
00391         {
00392             p_address->i_rfd = net_OpenUDP( p_sap, psz_addr,
00393                                             p_address->i_port, "", 0 );
00394             if( p_address->i_rfd != -1 )
00395                 net_StopSend( p_address->i_rfd );
00396             p_address->i_buff = 0;
00397             p_address->b_enabled = VLC_TRUE;
00398             p_address->b_ready = VLC_FALSE;
00399             p_address->i_limit = 10000; /* 10000 bps */
00400             p_address->t1 = 0;
00401         }
00402         else
00403         {
00404             p_address->b_enabled = VLC_TRUE;
00405             p_address->b_ready = VLC_TRUE;
00406             p_address->i_interval = config_GetInt( p_sap,"sap-interval");
00407             p_address->i_rfd = -1;
00408         }
00409 
00410         if( p_address->i_wfd == -1 || (p_address->i_rfd == -1
00411                                         && p_sap->b_control ) )
00412         {
00413             msg_Warn( p_sap, "disabling address" );
00414             p_address->b_enabled = VLC_FALSE;
00415         }
00416 
00417         INSERT_ELEM( p_sap->pp_addresses,
00418                      p_sap->i_addresses,
00419                      p_sap->i_addresses,
00420                      p_address );
00421         p_sap_session->p_address = p_address;
00422     }
00423 
00424 
00425     /* Build the SAP Headers */
00426     i_header_size = ( b_ipv6 ? 16 : 4 ) + 20;
00427     psz_head = (char *) malloc( i_header_size * sizeof( char ) );
00428     if( psz_head == NULL )
00429     {
00430         msg_Err( p_sap, "out of memory" );
00431         return VLC_ENOMEM;
00432     }
00433 
00434     /* SAPv1, not encrypted, not compressed */
00435     psz_head[0] = b_ipv6 ? 0x30 : 0x20;
00436     psz_head[1] = 0x00; /* No authentification length */
00437 
00438     i_hash = mdate();
00439     psz_head[2] = (i_hash & 0xFF00) >> 8; /* Msg id hash */
00440     psz_head[3] = (i_hash & 0xFF);        /* Msg id hash 2 */
00441 
00442 #if defined (HAVE_INET_PTON) || defined (WIN32)
00443     if( b_ipv6 )
00444     {
00445         inet_pton( AF_INET6, /* can't fail */
00446                    p_sap_session->p_address->psz_machine,
00447                    psz_head + 4 );
00448     }
00449     else
00450 #else
00451     {
00452         inet_pton( AF_INET, /* can't fail */
00453                    p_sap_session->p_address->psz_machine,
00454                    psz_head + 4 );
00455     }
00456 #endif
00457 
00458     memcpy( psz_head + (b_ipv6 ? 20 : 8), "application/sdp", 15 );
00459 
00460     /* If needed, build the SDP */
00461     if( p_session->psz_sdp == NULL )
00462     {
00463         p_session->psz_sdp = SDPGenerate( p_sap, p_session,
00464                                           p_sap_session->p_address );
00465         if( p_session->psz_sdp == NULL )
00466         {
00467             vlc_mutex_unlock( &p_sap->object_lock );
00468             return VLC_ENOMEM;
00469         }
00470     }
00471 
00472     p_sap_session->psz_sdp = strdup( p_session->psz_sdp );
00473     p_sap_session->i_last = 0;
00474 
00475     psz_head[ i_header_size-1 ] = '\0';
00476     p_sap_session->i_length = i_header_size + strlen( p_sap_session->psz_sdp);
00477 
00478     p_sap_session->psz_data = (uint8_t *)malloc( sizeof(char)*
00479                                                  p_sap_session->i_length );
00480 
00481     /* Build the final message */
00482     memcpy( p_sap_session->psz_data, psz_head, i_header_size );
00483     memcpy( p_sap_session->psz_data+i_header_size, p_sap_session->psz_sdp,
00484             strlen( p_sap_session->psz_sdp) );
00485 
00486     free( psz_head );
00487 
00488     /* Enqueue the announce */
00489     INSERT_ELEM( p_sap->pp_sessions,
00490                  p_sap->i_sessions,
00491                  p_sap->i_sessions,
00492                  p_sap_session );
00493     msg_Dbg( p_sap,"Addresses: %i  Sessions: %i",
00494                    p_sap->i_addresses,p_sap->i_sessions);
00495 
00496     /* Remember the SAP session for later deletion */
00497     p_session->p_sap = p_sap_session;
00498 
00499     vlc_mutex_unlock( &p_sap->object_lock );
00500 
00501     return VLC_SUCCESS;
00502 }
00503 
00504 /* Remove a SAP Announce */
00505 static int announce_SAPAnnounceDel( sap_handler_t *p_sap,
00506                              session_descriptor_t *p_session )
00507 {
00508     int i;
00509     vlc_mutex_lock( &p_sap->object_lock );
00510 
00511     msg_Dbg( p_sap,"removing SAP announce %p",p_session->p_sap);
00512 
00513     /* Dequeue the announce */
00514     for( i = 0; i< p_sap->i_sessions; i++)
00515     {
00516         if( p_session->p_sap == p_sap->pp_sessions[i] )
00517         {
00518             REMOVE_ELEM( p_sap->pp_sessions,
00519                          p_sap->i_sessions,
00520                          i );
00521 
00522             FREE( p_session->p_sap->psz_sdp );
00523             FREE( p_session->p_sap->psz_data );
00524             free( p_session->p_sap );
00525             break;
00526         }
00527     }
00528 
00529     /* XXX: Dequeue the address too if it is not used anymore
00530      * TODO: - address refcount
00531              - send a SAP deletion packet */
00532 
00533     msg_Dbg( p_sap,"%i announcements remaining", p_sap->i_sessions );
00534 
00535     vlc_mutex_unlock( &p_sap->object_lock );
00536 
00537     return VLC_SUCCESS;
00538 }
00539 
00540 static int announce_SendSAPAnnounce( sap_handler_t *p_sap,
00541                                      sap_session_t *p_session )
00542 {
00543     int i_ret;
00544 
00545     /* This announce has never been sent yet */
00546     if( p_session->i_last == 0 )
00547     {
00548         p_session->i_next = mdate()+ p_session->p_address->i_interval*1000000;
00549         p_session->i_last = 1;
00550         return VLC_SUCCESS;
00551     }
00552 
00553     if( p_session->i_next < mdate() )
00554     {
00555 #ifdef EXTRA_DEBUG
00556         msg_Dbg( p_sap, "Sending announce");
00557 #endif
00558         i_ret = net_Write( p_sap, p_session->p_address->i_wfd, NULL,
00559                            p_session->psz_data,
00560                            p_session->i_length );
00561         if( i_ret != p_session->i_length )
00562         {
00563             msg_Warn( p_sap, "SAP send failed on address %s (%i %i)",
00564                       p_session->p_address->psz_address,
00565                       i_ret, p_session->i_length );
00566         }
00567         p_session->i_last = p_session->i_next;
00568         p_session->i_next = p_session->i_last
00569                             + p_session->p_address->i_interval*1000000;
00570     }
00571     else
00572     {
00573         return VLC_SUCCESS;
00574     }
00575     return VLC_SUCCESS;
00576 }
00577 
00578 static char *SDPGenerate( sap_handler_t *p_sap,
00579                           const session_descriptor_t *p_session,
00580                           const sap_address_t *p_addr )
00581 {
00582     int64_t i_sdp_id = mdate();
00583     int     i_sdp_version = 1 + p_sap->i_sessions + (rand()&0xfff);
00584     char *psz_group, *psz_name, psz_uribuf[NI_MAXNUMERICHOST], *psz_uri,
00585          *psz_sdp;
00586     char ipv;
00587 
00588     psz_group = p_session->psz_group;
00589     psz_name = p_session->psz_name;
00590 
00591     /* FIXME: really check that psz_uri is a real IP address
00592      * FIXME: make a common function to obtain a canonical IP address */
00593     ipv = ( strchr( p_session->psz_uri, ':' )  != NULL) ? '6' : '4';
00594     if( *p_session->psz_uri == '[' )
00595     {
00596         char *ptr;
00597 
00598         strncpy( psz_uribuf, p_session->psz_uri + 1, sizeof( psz_uribuf ) );
00599         psz_uribuf[sizeof( psz_uribuf ) - 1] = '\0';
00600         ptr = strchr( psz_uribuf, '%' );
00601         if( ptr != NULL)
00602             *ptr = '\0';
00603         ptr = strchr( psz_uribuf, ']' );
00604         if( ptr != NULL)
00605             *ptr = '\0';
00606         psz_uri = psz_uribuf;
00607     }
00608     else
00609         psz_uri = p_session->psz_uri;
00610 
00611     /* see the lists in modules/stream_out/rtp.c for compliance stuff */
00612     if( asprintf( &psz_sdp,
00613                             "v=0\r\n"
00614                             "o=- "I64Fd" %d IN IP%c %s\r\n"
00615                             "s=%s\r\n"
00616                             "t=0 0\r\n"
00617                             "c=IN IP%c %s/%d\r\n"
00618                             "m=video %d %s %d\r\n"
00619                             "a=tool:"PACKAGE_STRING"\r\n"
00620                             "a=type:broadcast\r\n"
00621                             "%s%s%s",
00622                             i_sdp_id, i_sdp_version,
00623                             ipv, p_addr->psz_machine,
00624                             psz_name, ipv, psz_uri, p_session->i_ttl,
00625                             p_session->i_port, 
00626                             p_session->b_rtp ? "RTP/AVP" : "udp",
00627                             p_session->i_payload,
00628                             psz_group ? "a=x-plgroup:" : "",
00629                             psz_group ? psz_group : "", psz_group ? "\r\n" : "" ) == -1 )
00630         return NULL;
00631     
00632     msg_Dbg( p_sap, "Generated SDP (%i bytes):\n%s", strlen(psz_sdp),
00633              psz_sdp );
00634     return psz_sdp;
00635 }
00636 
00637 static int CalculateRate( sap_handler_t *p_sap, sap_address_t *p_address )
00638 {
00639     int i_read;
00640     uint8_t buffer[SAP_MAX_BUFFER];
00641     int i_tot = 0;
00642     mtime_t i_temp;
00643     int i_rate;
00644 
00645     if( p_address->t1 == 0 )
00646     {
00647         p_address->t1 = mdate();
00648         return VLC_SUCCESS;
00649     }
00650     do
00651     {
00652         /* Might be too slow if we have huge data */
00653         i_read = net_ReadNonBlock( p_sap, p_address->i_rfd, NULL, buffer,
00654                                    SAP_MAX_BUFFER, 0 );
00655         i_tot += i_read;
00656     } while( i_read > 0 && i_tot < SAP_MAX_BUFFER );
00657 
00658     i_temp = mdate();
00659 
00660     /* We calculate the rate every 5 seconds */
00661     if( i_temp - p_address->t1 < 5000000 )
00662     {
00663         p_address->i_buff += i_tot;
00664         return VLC_SUCCESS;
00665     }
00666 
00667     /* Bits/second */
00668     i_rate = (int)(8*1000000*((mtime_t)p_address->i_buff + (mtime_t)i_tot ) /
00669                         (i_temp - p_address->t1 ));
00670 
00671     p_address->i_limit = 10000;
00672 
00673     p_address->i_interval = ((1000*i_rate / p_address->i_limit) *
00674                             (MAX_INTERVAL - MIN_INTERVAL))/1000 + MIN_INTERVAL;
00675 
00676     if( p_address->i_interval > MAX_INTERVAL || p_address->i_interval < 0 )
00677     {
00678         p_address->i_interval = MAX_INTERVAL;
00679     }
00680 #ifdef EXTRA_DEBUG
00681     msg_Dbg( p_sap,"%s:%i : Rate=%i, Interval = %i s",
00682                     p_address->psz_address,p_address->i_port,
00683                     i_rate,
00684                     p_address->i_interval );
00685 #endif
00686 
00687     p_address->b_ready = VLC_TRUE;
00688 
00689     p_address->t1 = i_temp;
00690     p_address->i_buff = 0;
00691 
00692     return VLC_SUCCESS;
00693 }

Generated on Tue Dec 20 10:14:49 2005 for vlc-0.8.4a by  doxygen 1.4.2