]> git.sesse.net Git - vlc/blob - src/stream_output/sap.c
* a missing vlc_freeaddrinfo
[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     int i_port;
69     int i_rfd; /* Read socket */
70     int i_wfd; /* Write socket */
71
72     /* Used for flow control */
73     mtime_t t1;
74     vlc_bool_t b_enabled;
75     vlc_bool_t b_ready;
76     int i_interval;
77     int i_buff;
78     int i_limit;
79 };
80
81 /*****************************************************************************
82  * Local prototypes
83  *****************************************************************************/
84 static void RunThread( vlc_object_t *p_this);
85 static int CalculateRate( sap_handler_t *p_sap, sap_address_t *p_address );
86 static int SDPGenerate( sap_handler_t *p_sap, session_descriptor_t *p_session );
87
88 static int announce_SendSAPAnnounce( sap_handler_t *p_sap,
89                                      sap_session_t *p_session );
90
91
92 static int announce_SAPAnnounceAdd( sap_handler_t *p_sap,
93                              session_descriptor_t *p_session,
94                              announce_method_t *p_method );
95
96 static int announce_SAPAnnounceDel( sap_handler_t *p_sap,
97                              session_descriptor_t *p_session );
98 static char *convert_to_utf8( struct sap_handler_t *p_this, char *psz_local );
99
100 #define FREE( p ) if( p ) { free( p ); (p) = NULL; }
101
102
103 /**
104  * Create the SAP handler
105  *
106  * \param p_announce the parent announce_handler
107  * \return the newly created SAP handler or NULL on error
108  */
109 sap_handler_t *announce_SAPHandlerCreate( announce_handler_t *p_announce )
110 {
111     sap_handler_t *p_sap;
112     char *psz_charset;
113
114     p_sap = vlc_object_create( p_announce, sizeof( sap_handler_t ) );
115
116     if( !p_sap )
117     {
118         msg_Err( p_announce, "out of memory" );
119         return NULL;
120     }
121
122     vlc_mutex_init( p_sap, &p_sap->object_lock );
123
124     vlc_current_charset( &psz_charset );
125     p_sap->iconvHandle = vlc_iconv_open( "UTF-8", psz_charset );
126     free( psz_charset );
127     if( p_sap->iconvHandle == (vlc_iconv_t)(-1) )
128     {
129         msg_Warn( p_sap, "Unable to do requested conversion" );
130     }
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                              announce_method_t *p_method )
257 {
258     int i;
259     char *psz_type = "application/sdp";
260     int i_header_size;
261     char *psz_head;
262     vlc_bool_t b_found = VLC_FALSE, b_ipv6 = VLC_FALSE;
263     sap_session_t *p_sap_session;
264     mtime_t i_hash;
265
266     vlc_mutex_lock( &p_sap->object_lock );
267
268     /* If needed, build the SDP */
269     if( !p_session->psz_sdp )
270     {
271         if ( SDPGenerate( p_sap, p_session ) != VLC_SUCCESS )
272         {
273             vlc_mutex_unlock( &p_sap->object_lock );
274             return VLC_EGENERIC;
275         }
276     }
277
278     if( p_method->psz_address == NULL )
279     {
280         /* Determine SAP multicast address automatically */
281         char psz_buf[NI_MAXNUMERICHOST], *ptr;
282         const char *psz_addr;
283         struct addrinfo hints, *res;
284
285         if( p_session->psz_uri == NULL )
286         {
287             msg_Err( p_sap, "*FIXME* Unexpected NULL URI for SAP announce" );
288             msg_Err( p_sap, "This should not happen. VLC needs fixing." );
289             vlc_mutex_unlock( &p_sap->object_lock );
290             return VLC_EGENERIC;
291         }
292
293         /* Canonicalize IP address (e.g. 224.00.010.1 => 224.0.10.1) */
294         memset( &hints, 0, sizeof( hints ) );
295         hints.ai_socktype = SOCK_DGRAM;
296         hints.ai_flags = AI_NUMERICHOST;
297
298         i = vlc_getaddrinfo( (vlc_object_t *)p_sap, p_session->psz_uri, 0,
299                              &hints, &res );
300         if( i == 0 )
301         {
302             i = vlc_getnameinfo( res->ai_addr, res->ai_addrlen, psz_buf,
303                                  sizeof( psz_buf ), NULL, NI_NUMERICHOST );
304             vlc_freeaddrinfo( res );
305         }
306
307         if( i )
308         {
309             msg_Err( p_sap, "Invalid URI for SAP announce : %s : %s",
310                      p_session->psz_uri, vlc_gai_strerror( i ) );
311             vlc_mutex_unlock( &p_sap->object_lock );
312             return VLC_EGENERIC;
313         }
314
315         /* Remove interface specification if present */
316         ptr = strchr( psz_buf, '%' );
317         if( ptr != NULL )
318             *ptr = '\0';
319
320         if( strchr( psz_buf, ':' ) != NULL )
321         {
322             b_ipv6 = VLC_TRUE;
323
324             /* See RFC3513 for list of valid IPv6 scopes */
325             if( ( tolower( psz_buf[0] ) == 'f' )
326              && ( tolower( psz_buf[1] ) == 'f' )
327              && isxdigit( psz_buf[2] ) && isxdigit( psz_buf[3] ) )
328             {
329                 /* Multicast IPv6 */
330                 psz_buf[2] = '0'; /* force flags to zero */
331                 /* keep scope in psz_addr[3] */
332                 memcpy( &psz_buf[4], "::2:7ffe", sizeof( "::2:7ffe" ) );
333                 psz_addr = psz_buf;
334             }
335             else
336                 /* Unicast IPv6 - assume global scope */
337                 psz_addr = "ff0e::2:7ffe";
338         }
339         else
340         {
341             /* See RFC2365 for IPv4 scopes */
342             if( memcmp( psz_buf, "224.0.0.", 8 ) == 0 )
343                 psz_addr = "224.0.0.255";
344             else
345             if( memcmp( psz_buf, "239.255.", 8 ) == 0 )
346                 psz_addr = "239.255.255.255";
347             else
348             if( ( memcmp( psz_buf, "239.19", 6 ) == 0 )
349              && ( ( psz_buf[6] >= '2' ) && ( psz_buf[6] <= '5' ) ) )
350                 psz_addr = "239.195.255.255";
351             else
352                 /* assume global scope */
353                 psz_addr = "224.2.127.254";
354         }
355
356         p_method->psz_address = strdup( psz_addr );
357     }
358     else
359         b_ipv6 = (strchr( p_method->psz_address, ':' ) != NULL);
360
361     msg_Dbg( p_sap, "using SAP address: %s", p_method->psz_address);
362
363     /* XXX: Check for dupes */
364     p_sap_session = (sap_session_t*)malloc(sizeof(sap_session_t));
365
366     p_sap_session->psz_sdp = strdup( p_session->psz_sdp );
367     p_sap_session->i_last = 0;
368
369     /* Add the address to the buffer */
370     for( i = 0; i< p_sap->i_addresses; i++)
371     {
372         if( !strcmp( p_method->psz_address,
373              p_sap->pp_addresses[i]->psz_address ) )
374         {
375             p_sap_session->p_address = p_sap->pp_addresses[i];
376             b_found = VLC_TRUE;
377             break;
378         }
379     }
380     if( b_found == VLC_FALSE )
381     {
382         sap_address_t *p_address = (sap_address_t *)
383                                     malloc( sizeof(sap_address_t) );
384         if( !p_address )
385         {
386             msg_Err( p_sap, "out of memory" );
387             return VLC_ENOMEM;
388         }
389         p_address->psz_address = strdup( p_method->psz_address );
390         p_address->i_port  =  9875;
391         p_address->i_wfd = net_OpenUDP( p_sap, "", 0,
392                                         p_address->psz_address,
393                                         p_address->i_port );
394         if( p_address->i_wfd != -1 )
395             net_StopRecv( p_address->i_wfd );
396
397         if( p_sap->b_control == VLC_TRUE )
398         {
399             p_address->i_rfd = net_OpenUDP( p_sap, p_method->psz_address,
400                                             p_address->i_port,
401                                             "", 0 );
402             if( p_address->i_rfd != -1 )
403                 net_StopSend( p_address->i_rfd );
404             p_address->i_buff = 0;
405             p_address->b_enabled = VLC_TRUE;
406             p_address->b_ready = VLC_FALSE;
407             p_address->i_limit = 10000; /* 10000 bps */
408             p_address->t1 = 0;
409         }
410         else
411         {
412             p_address->b_enabled = VLC_TRUE;
413             p_address->b_ready = VLC_TRUE;
414             p_address->i_interval = config_GetInt( p_sap,"sap-interval");
415             p_address->i_rfd = -1;
416         }
417
418         if( p_address->i_wfd == -1 || (p_address->i_rfd == -1
419                                         && p_sap->b_control ) )
420         {
421             msg_Warn( p_sap, "disabling address" );
422             p_address->b_enabled = VLC_FALSE;
423         }
424
425         INSERT_ELEM( p_sap->pp_addresses,
426                      p_sap->i_addresses,
427                      p_sap->i_addresses,
428                      p_address );
429         p_sap_session->p_address = p_address;
430     }
431
432     /* Build the SAP Headers */
433     i_header_size = ( b_ipv6 ? 20 : 8 ) + strlen( psz_type ) + 1;
434     psz_head = (char *) malloc( i_header_size * sizeof( char ) );
435     if( ! psz_head )
436     {
437         msg_Err( p_sap, "out of memory" );
438         return VLC_ENOMEM;
439     }
440
441     psz_head[0] = 0x20; /* Means SAPv1, IPv4, not encrypted, not compressed */
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( b_ipv6 )
449     {
450         /* in_addr_t ip_server = inet_addr( ip ); */
451         psz_head[0] |= 0x10; /* Set IPv6 */
452
453         psz_head[4] = 0x01; /* Source IP  FIXME: we should get the real address */
454         psz_head[5] = 0x02; /* idem */
455         psz_head[6] = 0x03; /* idem */
456         psz_head[7] = 0x04; /* idem */
457
458         psz_head[8] = 0x01; /* Source IP  FIXME: we should get the real address */
459         psz_head[9] = 0x02; /* idem */
460         psz_head[10] = 0x03; /* idem */
461         psz_head[11] = 0x04; /* idem */
462
463         psz_head[12] = 0x01; /* Source IP  FIXME: we should get the real address */
464         psz_head[13] = 0x02; /* idem */
465         psz_head[14] = 0x03; /* idem */
466         psz_head[15] = 0x04; /* idem */
467
468         psz_head[16] = 0x01; /* Source IP  FIXME: we should get the real address */
469         psz_head[17] = 0x02; /* idem */
470         psz_head[18] = 0x03; /* idem */
471         psz_head[19] = 0x04; /* idem */
472
473         strncpy( psz_head + 20, psz_type, 15 );
474     }
475     else
476     {
477         /* in_addr_t ip_server = inet_addr( ip) */
478         /* Source IP  FIXME: we should get the real address */
479         psz_head[4] = 0x01; /* ip_server */
480         psz_head[5] = 0x02; /* ip_server>>8 */
481         psz_head[6] = 0x03; /* ip_server>>16 */
482         psz_head[7] = 0x04; /* ip_server>>24 */
483
484         strncpy( psz_head + 8, psz_type, 15 );
485     }
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     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 != 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 int SDPGenerate( sap_handler_t *p_sap, session_descriptor_t *p_session )
591 {
592     int64_t i_sdp_id = mdate();
593     int     i_sdp_version = 1 + p_sap->i_sessions + (rand()&0xfff);
594     char *psz_group, *psz_name, psz_uribuf[NI_MAXNUMERICHOST], *psz_uri;
595     char ipv;
596
597     psz_group = convert_to_utf8( p_sap, p_session->psz_group );
598     psz_name = convert_to_utf8( p_sap, p_session->psz_name );
599
600     /* FIXME: really check that psz_uri is a real IP address
601      * FIXME: make a common function to obtain a canonical IP address */
602     ipv = ( strchr( p_session->psz_uri, ':' )  != NULL) ? '6' : '4';
603     if( *p_session->psz_uri == '[' )
604     {
605         char *ptr;
606
607         strncpy( psz_uribuf, p_session->psz_uri + 1, sizeof( psz_uribuf ) );
608         psz_uribuf[sizeof( psz_uribuf ) - 1] = '\0';
609         ptr = strchr( psz_uribuf, '%' );
610         if( ptr != NULL)
611             *ptr = '\0';
612         ptr = strchr( psz_uribuf, ']' );
613         if( ptr != NULL)
614             *ptr = '\0';
615         psz_uri = psz_uribuf;
616     }
617     else
618         psz_uri = p_session->psz_uri;
619
620     /* see the lists in modules/stream_out/rtp.c for compliance stuff */
621     p_session->psz_sdp = (char *)malloc(
622                             sizeof("v=0\r\n"
623                                    "o=- 45383436098 45398  IN IP4 127.0.0.1\r\n" /* FIXME */
624                                    "s=\r\n"
625                                    "t=0 0\r\n"
626                                    "c=IN IP4 /\r\n"
627                                    "m=video  udp\r\n"
628                                    "a=tool:"PACKAGE_STRING"\r\n"
629                                    "a=type:broadcast\r\n")
630                            + strlen( psz_name )
631                            + strlen( psz_uri ) + 300
632                            + ( psz_group ? strlen( psz_group ) : 0 ) );
633
634     if( p_session->psz_sdp == NULL || psz_name == NULL )
635     {
636         msg_Err( p_sap, "out of memory" );
637         FREE( psz_name );
638         FREE( psz_group );
639         return VLC_ENOMEM;
640     }
641
642     sprintf( p_session->psz_sdp,
643                             "v=0\r\n"
644                             "o=- "I64Fd" %d IN IP4 127.0.0.1\r\n"
645                             "s=%s\r\n"
646                             "t=0 0\r\n"
647                             "c=IN IP%c %s/%d\r\n"
648                             "m=video %d udp %d\r\n"
649                             "a=tool:"PACKAGE_STRING"\r\n"
650                             "a=type:broadcast\r\n",
651                             i_sdp_id, i_sdp_version,
652                             psz_name, ipv,
653                             psz_uri, p_session->i_ttl,
654                             p_session->i_port, p_session->i_payload );
655     free( psz_name );
656
657     if( psz_group )
658     {
659         sprintf( p_session->psz_sdp, "%sa=x-plgroup:%s\r\n",
660                                      p_session->psz_sdp, psz_group );
661         free( psz_group );
662     }
663
664     msg_Dbg( p_sap, "Generated SDP (%i bytes):\n%s", strlen(p_session->psz_sdp),
665                     p_session->psz_sdp );
666     return VLC_SUCCESS;
667 }
668
669 static int CalculateRate( sap_handler_t *p_sap, sap_address_t *p_address )
670 {
671     int i_read;
672     uint8_t buffer[SAP_MAX_BUFFER];
673     int i_tot = 0;
674     mtime_t i_temp;
675     int i_rate;
676
677     if( p_address->t1 == 0 )
678     {
679         p_address->t1 = mdate();
680         return VLC_SUCCESS;
681     }
682     do
683     {
684         /* Might be too slow if we have huge data */
685         i_read = net_ReadNonBlock( p_sap, p_address->i_rfd, NULL, buffer,
686                                    SAP_MAX_BUFFER, 0 );
687         i_tot += i_read;
688     } while( i_read > 0 && i_tot < SAP_MAX_BUFFER );
689
690     i_temp = mdate();
691
692     /* We calculate the rate every 5 seconds */
693     if( i_temp - p_address->t1 < 5000000 )
694     {
695         p_address->i_buff += i_tot;
696         return VLC_SUCCESS;
697     }
698
699     /* Bits/second */
700     i_rate = (int)(8*1000000*((mtime_t)p_address->i_buff + (mtime_t)i_tot ) /
701                         (i_temp - p_address->t1 ));
702
703     p_address->i_limit = 10000;
704
705     p_address->i_interval = ((1000*i_rate / p_address->i_limit) *
706                             (MAX_INTERVAL - MIN_INTERVAL))/1000 + MIN_INTERVAL;
707
708     if( p_address->i_interval > MAX_INTERVAL || p_address->i_interval < 0 )
709     {
710         p_address->i_interval = MAX_INTERVAL;
711     }
712 #ifdef EXTRA_DEBUG
713     msg_Dbg( p_sap,"%s:%i : Rate=%i, Interval = %i s",
714                     p_address->psz_address,p_address->i_port,
715                     i_rate,
716                     p_address->i_interval );
717 #endif
718
719     p_address->b_ready = VLC_TRUE;
720
721     p_address->t1 = i_temp;
722     p_address->i_buff = 0;
723
724     return VLC_SUCCESS;
725 }
726
727
728 static char *convert_to_utf8( struct sap_handler_t *p_this, char *psz_local )
729 {
730     char *psz_unicode, *psz_in, *psz_out;
731     size_t ret, i_in, i_out;
732
733     if( psz_local == NULL )
734         return NULL;
735     if ( p_this->iconvHandle == (vlc_iconv_t)(-1) )
736         return strdup( psz_local );
737
738     psz_in = psz_local;
739     i_in = strlen( psz_local );
740
741     i_out = 6 * i_in;
742     psz_unicode = malloc( i_out + 1 );
743     if( psz_unicode == NULL )
744         return strdup( psz_local );
745     psz_out = psz_unicode;
746
747     ret = vlc_iconv( p_this->iconvHandle,
748                      &psz_in, &i_in, &psz_out, &i_out);
749     if( ret == (size_t)(-1) || i_in )
750     {
751         msg_Warn( p_this, "Failed to convert \"%s\" to UTF-8", psz_local );
752         return strdup( psz_local );
753     }
754     *psz_out = '\0';
755     return psz_unicode;
756 }