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