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