]> git.sesse.net Git - vlc/blob - src/stream_output/sap.c
Put correct owner address in SAP-generated SDP
[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;
258     const char *psz_type = "application/sdp";
259     int i_header_size;
260     char *psz_head;
261     vlc_bool_t b_ipv6 = VLC_FALSE;
262     sap_session_t *p_sap_session;
263     mtime_t i_hash;
264     char psz_buf[NI_MAXNUMERICHOST], *ptr;
265     const char *psz_addr;
266     struct addrinfo hints, *res;
267         
268
269     vlc_mutex_lock( &p_sap->object_lock );
270
271     if( p_session->psz_uri == NULL )
272     {
273         msg_Err( p_sap, "*FIXME* Unexpected NULL URI for SAP announce" );
274         msg_Err( p_sap, "This should not happen. VLC needs fixing." );
275         vlc_mutex_unlock( &p_sap->object_lock );
276         return VLC_EGENERIC;
277     }
278
279     /* Determine SAP multicast address automatically (FIXME: gruik) */
280     /* Canonicalize IP address (e.g. 224.00.010.1 => 224.0.10.1) */
281     memset( &hints, 0, sizeof( hints ) );
282     hints.ai_socktype = SOCK_DGRAM;
283     hints.ai_flags = AI_NUMERICHOST;
284
285     i = vlc_getaddrinfo( (vlc_object_t *)p_sap, p_session->psz_uri, 0,
286                          &hints, &res );
287     if( i == 0 )
288     {
289         i = vlc_getnameinfo( res->ai_addr, res->ai_addrlen, psz_buf,
290                              sizeof( psz_buf ), NULL, NI_NUMERICHOST );
291         vlc_freeaddrinfo( res );
292     }
293
294     if( i )
295     {
296         msg_Err( p_sap, "Invalid URI for SAP announce : %s : %s",
297                  p_session->psz_uri, vlc_gai_strerror( i ) );
298         vlc_mutex_unlock( &p_sap->object_lock );
299         return VLC_EGENERIC;
300     }
301
302     /* Remove interface specification if present */
303     ptr = strchr( psz_buf, '%' );
304     if( ptr != NULL )
305         *ptr = '\0';
306
307     if( strchr( psz_buf, ':' ) != NULL )
308     {
309         b_ipv6 = VLC_TRUE;
310
311         /* See RFC3513 for list of valid IPv6 scopes */
312         if( ( tolower( psz_buf[0] ) == 'f' )
313          && ( tolower( psz_buf[1] ) == 'f' )
314          && isxdigit( psz_buf[2] ) && isxdigit( psz_buf[3] ) )
315         {
316             /* Multicast IPv6 */
317             psz_buf[2] = '0'; /* force flags to zero */
318             /* keep scope in psz_addr[3] */
319             memcpy( &psz_buf[4], "::2:7ffe", sizeof( "::2:7ffe" ) );
320             psz_addr = psz_buf;
321         }
322         else
323             /* Unicast IPv6 - assume global scope */
324             psz_addr = "ff0e::2:7ffe";
325     }
326     else
327     {
328         /* See RFC2365 for IPv4 scopes */
329         if( memcmp( psz_buf, "224.0.0.", 8 ) == 0 )
330             psz_addr = "224.0.0.255";
331         else
332         if( memcmp( psz_buf, "239.255.", 8 ) == 0 )
333             psz_addr = "239.255.255.255";
334         else
335         if( ( memcmp( psz_buf, "239.19", 6 ) == 0 )
336          && ( ( psz_buf[6] >= '2' ) && ( psz_buf[6] <= '5' ) ) )
337             psz_addr = "239.195.255.255";
338         else
339             /* assume global scope */
340             psz_addr = "224.2.127.254";
341     }
342
343     msg_Dbg( p_sap, "using SAP address: %s", psz_addr);
344
345     /* XXX: Check for dupes */
346     p_sap_session = (sap_session_t*)malloc(sizeof(sap_session_t));
347     p_sap_session->p_address = NULL;
348
349     /* Add the address to the buffer */
350     for( i = 0; i < p_sap->i_addresses; i++)
351     {
352         if( !strcmp( psz_addr, p_sap->pp_addresses[i]->psz_address ) )
353         {
354             p_sap_session->p_address = p_sap->pp_addresses[i];
355             break;
356         }
357     }
358
359     if( p_sap_session->p_address == NULL )
360     {
361         sap_address_t *p_address = (sap_address_t *)
362                                     malloc( sizeof(sap_address_t) );
363         if( !p_address )
364         {
365             msg_Err( p_sap, "out of memory" );
366             return VLC_ENOMEM;
367         }
368         p_address->psz_address = strdup( psz_addr );
369         p_address->i_port  =  9875;
370         p_address->i_wfd = net_OpenUDP( p_sap, "", 0, psz_addr,
371                                         p_address->i_port );
372         if( p_address->i_wfd != -1 )
373         {
374             net_StopRecv( p_address->i_wfd );
375             net_GetSockAddress( p_sap, p_address->i_wfd,
376                                 p_address->psz_machine, NULL );
377         }
378
379         if( p_sap->b_control == VLC_TRUE )
380         {
381             p_address->i_rfd = net_OpenUDP( p_sap, psz_addr,
382                                             p_address->i_port, "", 0 );
383             if( p_address->i_rfd != -1 )
384                 net_StopSend( p_address->i_rfd );
385             p_address->i_buff = 0;
386             p_address->b_enabled = VLC_TRUE;
387             p_address->b_ready = VLC_FALSE;
388             p_address->i_limit = 10000; /* 10000 bps */
389             p_address->t1 = 0;
390         }
391         else
392         {
393             p_address->b_enabled = VLC_TRUE;
394             p_address->b_ready = VLC_TRUE;
395             p_address->i_interval = config_GetInt( p_sap,"sap-interval");
396             p_address->i_rfd = -1;
397         }
398
399         if( p_address->i_wfd == -1 || (p_address->i_rfd == -1
400                                         && p_sap->b_control ) )
401         {
402             msg_Warn( p_sap, "disabling address" );
403             p_address->b_enabled = VLC_FALSE;
404         }
405
406         INSERT_ELEM( p_sap->pp_addresses,
407                      p_sap->i_addresses,
408                      p_sap->i_addresses,
409                      p_address );
410         p_sap_session->p_address = p_address;
411     }
412
413
414     /* Build the SAP Headers */
415     i_header_size = ( b_ipv6 ? 20 : 8 ) + strlen( psz_type ) + 1;
416     psz_head = (char *) malloc( i_header_size * sizeof( char ) );
417     if( psz_head == NULL )
418     {
419         msg_Err( p_sap, "out of memory" );
420         return VLC_ENOMEM;
421     }
422
423     psz_head[0] = 0x20; /* Means SAPv1, IPv4, not encrypted, not compressed */
424     psz_head[1] = 0x00; /* No authentification length */
425
426     i_hash = mdate();
427     psz_head[2] = (i_hash & 0xFF00) >> 8; /* Msg id hash */
428     psz_head[3] = (i_hash & 0xFF);        /* Msg id hash 2 */
429
430     if( b_ipv6 )
431     {
432         /* in_addr_t ip_server = inet_addr( ip ); */
433         psz_head[0] |= 0x10; /* Set IPv6 */
434
435         psz_head[4] = 0x01; /* Source IP  FIXME: we should get the real address */
436         psz_head[5] = 0x02; /* idem */
437         psz_head[6] = 0x03; /* idem */
438         psz_head[7] = 0x04; /* idem */
439
440         psz_head[8] = 0x01; /* Source IP  FIXME: we should get the real address */
441         psz_head[9] = 0x02; /* idem */
442         psz_head[10] = 0x03; /* idem */
443         psz_head[11] = 0x04; /* idem */
444
445         psz_head[12] = 0x01; /* Source IP  FIXME: we should get the real address */
446         psz_head[13] = 0x02; /* idem */
447         psz_head[14] = 0x03; /* idem */
448         psz_head[15] = 0x04; /* idem */
449
450         psz_head[16] = 0x01; /* Source IP  FIXME: we should get the real address */
451         psz_head[17] = 0x02; /* idem */
452         psz_head[18] = 0x03; /* idem */
453         psz_head[19] = 0x04; /* idem */
454
455         strncpy( psz_head + 20, psz_type, 15 );
456     }
457     else
458     {
459         /* in_addr_t ip_server = inet_addr( ip) */
460         /* Source IP  FIXME: we should get the real address */
461         psz_head[4] = 0x01; /* ip_server */
462         psz_head[5] = 0x02; /* ip_server>>8 */
463         psz_head[6] = 0x03; /* ip_server>>16 */
464         psz_head[7] = 0x04; /* ip_server>>24 */
465
466         strncpy( psz_head + 8, psz_type, 15 );
467     }
468
469     /* If needed, build the SDP */
470     if( p_session->psz_sdp == NULL )
471     {
472         p_session->psz_sdp = SDPGenerate( p_sap, p_session,
473                                           p_sap_session->p_address );
474         if( p_session->psz_sdp == NULL )
475         {
476             vlc_mutex_unlock( &p_sap->object_lock );
477             return VLC_ENOMEM;
478         }
479     }
480
481     p_sap_session->psz_sdp = strdup( p_session->psz_sdp );
482     p_sap_session->i_last = 0;
483
484     psz_head[ i_header_size-1 ] = '\0';
485     p_sap_session->i_length = i_header_size + strlen( p_sap_session->psz_sdp);
486
487     p_sap_session->psz_data = (uint8_t *)malloc( sizeof(char)*
488                                                  p_sap_session->i_length );
489
490     /* Build the final message */
491     memcpy( p_sap_session->psz_data, psz_head, i_header_size );
492     memcpy( p_sap_session->psz_data+i_header_size, p_sap_session->psz_sdp,
493             strlen( p_sap_session->psz_sdp) );
494
495     free( psz_head );
496
497     /* Enqueue the announce */
498     INSERT_ELEM( p_sap->pp_sessions,
499                  p_sap->i_sessions,
500                  p_sap->i_sessions,
501                  p_sap_session );
502     msg_Dbg( p_sap,"Addresses: %i  Sessions: %i",
503                    p_sap->i_addresses,p_sap->i_sessions);
504
505     /* Remember the SAP session for later deletion */
506     p_session->p_sap = p_sap_session;
507
508     vlc_mutex_unlock( &p_sap->object_lock );
509
510     return VLC_SUCCESS;
511 }
512
513 /* Remove a SAP Announce */
514 static int announce_SAPAnnounceDel( sap_handler_t *p_sap,
515                              session_descriptor_t *p_session )
516 {
517     int i;
518     vlc_mutex_lock( &p_sap->object_lock );
519
520     msg_Dbg( p_sap,"removing SAP announce %p",p_session->p_sap);
521
522     /* Dequeue the announce */
523     for( i = 0; i< p_sap->i_sessions; i++)
524     {
525         if( p_session->p_sap == p_sap->pp_sessions[i] )
526         {
527             REMOVE_ELEM( p_sap->pp_sessions,
528                          p_sap->i_sessions,
529                          i );
530
531             FREE( p_session->p_sap->psz_sdp );
532             FREE( p_session->p_sap->psz_data );
533             free( p_session->p_sap );
534             break;
535         }
536     }
537
538     /* XXX: Dequeue the address too if it is not used anymore
539      * TODO: - address refcount
540              - send a SAP deletion packet */
541
542     msg_Dbg( p_sap,"%i announces remaining", p_sap->i_sessions );
543
544     vlc_mutex_unlock( &p_sap->object_lock );
545
546     return VLC_SUCCESS;
547 }
548
549 static int announce_SendSAPAnnounce( sap_handler_t *p_sap,
550                                      sap_session_t *p_session )
551 {
552     int i_ret;
553
554     /* This announce has never been sent yet */
555     if( p_session->i_last == 0 )
556     {
557         p_session->i_next = mdate()+ p_session->p_address->i_interval*1000000;
558         p_session->i_last = 1;
559         return VLC_SUCCESS;
560     }
561
562     if( p_session->i_next < mdate() )
563     {
564 #ifdef EXTRA_DEBUG
565         msg_Dbg( p_sap, "Sending announce");
566 #endif
567         i_ret = net_Write( p_sap, p_session->p_address->i_wfd, NULL,
568                            p_session->psz_data,
569                            p_session->i_length );
570         if( i_ret != (int)p_session->i_length )
571         {
572             msg_Warn( p_sap, "SAP send failed on address %s (%i %i)",
573                    p_session->p_address->psz_address,
574                    i_ret, p_session->i_length );
575         }
576         p_session->i_last = p_session->i_next;
577         p_session->i_next = p_session->i_last
578                             + p_session->p_address->i_interval*1000000;
579     }
580     else
581     {
582         return VLC_SUCCESS;
583     }
584     return VLC_SUCCESS;
585 }
586
587 static char *SDPGenerate( sap_handler_t *p_sap,
588                           const session_descriptor_t *p_session,
589                           const sap_address_t *p_addr )
590 {
591     int64_t i_sdp_id = mdate();
592     int     i_sdp_version = 1 + p_sap->i_sessions + (rand()&0xfff);
593     char *psz_group, *psz_name, psz_uribuf[NI_MAXNUMERICHOST], *psz_uri,
594          *psz_sdp;
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     if( psz_name == NULL )
600     {
601         FREE( psz_group );
602         return NULL;
603     }
604
605     /* FIXME: really check that psz_uri is a real IP address
606      * FIXME: make a common function to obtain a canonical IP address */
607     ipv = ( strchr( p_session->psz_uri, ':' )  != NULL) ? '6' : '4';
608     if( *p_session->psz_uri == '[' )
609     {
610         char *ptr;
611
612         strncpy( psz_uribuf, p_session->psz_uri + 1, sizeof( psz_uribuf ) );
613         psz_uribuf[sizeof( psz_uribuf ) - 1] = '\0';
614         ptr = strchr( psz_uribuf, '%' );
615         if( ptr != NULL)
616             *ptr = '\0';
617         ptr = strchr( psz_uribuf, ']' );
618         if( ptr != NULL)
619             *ptr = '\0';
620         psz_uri = psz_uribuf;
621     }
622     else
623         psz_uri = p_session->psz_uri;
624
625     /* see the lists in modules/stream_out/rtp.c for compliance stuff */
626     if( asprintf( &psz_sdp,
627                             "v=0\r\n"
628                             "o=- "I64Fd" %d IN IP%c %s\r\n"
629                             "s=%s\r\n"
630                             "t=0 0\r\n"
631                             "c=IN IP%c %s/%d\r\n"
632                             "m=video %d udp %d\r\n"
633                             "a=tool:"PACKAGE_STRING"\r\n"
634                             "a=type:broadcast\r\n",
635                             i_sdp_id, i_sdp_version,
636                             ipv, p_addr->psz_machine,
637                             psz_name, ipv,
638                             psz_uri, p_session->i_ttl,
639                             p_session->i_port, p_session->i_payload ) == -1 )
640     {
641         free( psz_name );
642         FREE( psz_group );
643         return NULL;
644     }
645     
646     free( psz_name );
647
648     if( psz_group )
649     {
650         /* FIXME: this is illegal use of sprintf */
651         sprintf( psz_sdp, "%sa=x-plgroup:%s\r\n", psz_sdp, psz_group );
652         free( psz_group );
653     }
654
655     msg_Dbg( p_sap, "Generated SDP (%i bytes):\n%s", strlen(psz_sdp),
656              psz_sdp );
657     return psz_sdp;
658 }
659
660 static int CalculateRate( sap_handler_t *p_sap, sap_address_t *p_address )
661 {
662     int i_read;
663     uint8_t 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 }