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