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