sap.c 23.4 KB
Newer Older
zorglub's avatar
zorglub committed
1
2
3
/*****************************************************************************
 * sap.c : SAP announce handler
 *****************************************************************************
4
 * Copyright (C) 2002-2005 the VideoLAN team
5
 * $Id$
zorglub's avatar
zorglub committed
6
 *
7
8
 * Authors: Clément Stenac <zorglub@videolan.org>
 *          Rémi Denis-Courmont <rem # videolan.org>
zorglub's avatar
zorglub committed
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <stdlib.h>                                                /* free() */
#include <stdio.h>                                              /* sprintf() */
#include <string.h>                                            /* strerror() */
31
#include <ctype.h>                                  /* tolower(), isxdigit() */
zorglub's avatar
zorglub committed
32
33
34
35

#include <vlc/vlc.h>
#include <vlc/sout.h>

36
37
38
39
40
41
42
43
44
45
#include "network.h"
#if defined( WIN32 ) || defined( UNDER_CE )
#   if defined(UNDER_CE) && defined(sockaddr_storage)
#       undef sockaddr_storage
#   endif
#   include <winsock2.h>
#   include <ws2tcpip.h>
#else
#   include <netdb.h>
#endif
46
#include "charset.h"
zorglub's avatar
zorglub committed
47

48
/* SAP is always on that port */
zorglub's avatar
zorglub committed
49
50
51
52
53
54
#define SAP_PORT 9875

#define DEFAULT_PORT "1234"

#undef EXTRA_DEBUG

55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/* SAP Specific structures */

/* 100ms */
#define SAP_IDLE ((mtime_t)(0.100*CLOCK_FREQ))
#define SAP_MAX_BUFFER 65534
#define MIN_INTERVAL 2
#define MAX_INTERVAL 300

/* A SAP announce address. For each of these, we run the
 * control flow algorithm */
struct sap_address_t
{
    char *psz_address;
    int i_port;
    int i_rfd; /* Read socket */
    int i_wfd; /* Write socket */

    /* Used for flow control */
    mtime_t t1;
    vlc_bool_t b_enabled;
    vlc_bool_t b_ready;
    int i_interval;
    int i_buff;
    int i_limit;
};

zorglub's avatar
zorglub committed
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static void RunThread( vlc_object_t *p_this);
static int CalculateRate( sap_handler_t *p_sap, sap_address_t *p_address );
static int SDPGenerate( sap_handler_t *p_sap, session_descriptor_t *p_session );

static int announce_SendSAPAnnounce( sap_handler_t *p_sap,
                                     sap_session_t *p_session );


static int announce_SAPAnnounceAdd( sap_handler_t *p_sap,
                             session_descriptor_t *p_session,
                             announce_method_t *p_method );

static int announce_SAPAnnounceDel( sap_handler_t *p_sap,
                             session_descriptor_t *p_session );
98
static char *convert_to_utf8( struct sap_handler_t *p_this, char *psz_local );
zorglub's avatar
zorglub committed
99
100
101
102
103
104
105
106
107
108
109
110
111

#define FREE( p ) if( p ) { free( p ); (p) = NULL; }


/**
 * Create the SAP handler
 *
 * \param p_announce the parent announce_handler
 * \return the newly created SAP handler or NULL on error
 */
sap_handler_t *announce_SAPHandlerCreate( announce_handler_t *p_announce )
{
    sap_handler_t *p_sap;
112
    char *psz_charset;
zorglub's avatar
zorglub committed
113
114
115
116
117
118
119
120
121
122
123

    p_sap = vlc_object_create( p_announce, sizeof( sap_handler_t ) );

    if( !p_sap )
    {
        msg_Err( p_announce, "out of memory" );
        return NULL;
    }

    vlc_mutex_init( p_sap, &p_sap->object_lock );

124
125
126
127
128
129
130
131
    vlc_current_charset( &psz_charset );
    p_sap->iconvHandle = vlc_iconv_open( "UTF-8", psz_charset );
    free( psz_charset );
    if( p_sap->iconvHandle == (vlc_iconv_t)(-1) )
    {
        msg_Warn( p_sap, "Unable to do requested conversion" );
    }

zorglub's avatar
zorglub committed
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
    p_sap->pf_add = announce_SAPAnnounceAdd;
    p_sap->pf_del = announce_SAPAnnounceDel;

    p_sap->i_sessions = 0;
    p_sap->i_addresses = 0;
    p_sap->i_current_session = 0;

    p_sap->b_control = config_GetInt( p_sap, "sap-flow-control");

    if( vlc_thread_create( p_sap, "sap handler", RunThread,
                       VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
    {
        msg_Dbg( p_announce, "Unable to spawn SAP handler thread");
        free( p_sap );
        return NULL;
    };
    msg_Dbg( p_announce, "thread created, %i sessions", p_sap->i_sessions);
    return p_sap;
}

/**
 *  Destroy the SAP handler
 *  \param p_this the SAP Handler to destroy
 *  \return nothing
 */
void announce_SAPHandlerDestroy( sap_handler_t *p_sap )
{
    int i;

    vlc_mutex_destroy( &p_sap->object_lock );

    /* Free the remaining sessions */
    for( i = 0 ; i< p_sap->i_sessions ; i++)
    {
        sap_session_t *p_session = p_sap->pp_sessions[i];
        FREE( p_session->psz_sdp );
        FREE( p_session->psz_data );
        REMOVE_ELEM( p_sap->pp_sessions, p_sap->i_sessions , i );
        FREE( p_session );
    }

    /* Free the remaining addresses */
    for( i = 0 ; i< p_sap->i_addresses ; i++)
    {
        sap_address_t *p_address = p_sap->pp_addresses[i];
        FREE( p_address->psz_address );
        if( p_address->i_rfd > -1 )
        {
            net_Close( p_address->i_rfd );
        }
        if( p_address->i_wfd > -1 && p_sap->b_control )
        {
            net_Close( p_address->i_wfd );
        }
        REMOVE_ELEM( p_sap->pp_addresses, p_sap->i_addresses, i );
        FREE( p_address );
    }

190
191
192
    if( p_sap->iconvHandle != (vlc_iconv_t)(-1) )
        vlc_iconv_close( p_sap->iconvHandle );

zorglub's avatar
zorglub committed
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
    /* Free the structure */
    vlc_object_destroy( p_sap );
}

/**
 * main SAP handler thread
 * \param p_this the SAP Handler object
 * \return nothing
 */
static void RunThread( vlc_object_t *p_this)
{
    sap_handler_t *p_sap = (sap_handler_t*)p_this;
    sap_session_t *p_session;

    while( !p_sap->b_die )
    {
        int i;

        /* If needed, get the rate info */
        if( p_sap->b_control == VLC_TRUE )
        {
            for( i = 0 ; i< p_sap->i_addresses ; i++)
            {
                if( p_sap->pp_addresses[i]->b_enabled == VLC_TRUE )
                {
                    CalculateRate( p_sap, p_sap->pp_addresses[i] );
                }
            }
        }

        /* Find the session to announce */
        vlc_mutex_lock( &p_sap->object_lock );
        if( p_sap->i_sessions > p_sap->i_current_session + 1)
        {
            p_sap->i_current_session++;
        }
        else if( p_sap->i_sessions > 0)
        {
            p_sap->i_current_session = 0;
        }
        else
        {
            vlc_mutex_unlock( &p_sap->object_lock );
            msleep( SAP_IDLE );
            continue;
        }
        p_session = p_sap->pp_sessions[p_sap->i_current_session];
        vlc_mutex_unlock( &p_sap->object_lock );

        /* And announce it */
        if( p_session->p_address->b_enabled == VLC_TRUE &&
            p_session->p_address->b_ready == VLC_TRUE )
        {
            announce_SendSAPAnnounce( p_sap, p_session );
        }

        msleep( SAP_IDLE );
    }
}

/* Add a SAP announce */
static int announce_SAPAnnounceAdd( sap_handler_t *p_sap,
                             session_descriptor_t *p_session,
                             announce_method_t *p_method )
{
    int i;
    char *psz_type = "application/sdp";
    int i_header_size;
    char *psz_head;
262
    vlc_bool_t b_found = VLC_FALSE, b_ipv6 = VLC_FALSE;
zorglub's avatar
zorglub committed
263
    sap_session_t *p_sap_session;
264
    mtime_t i_hash;
zorglub's avatar
zorglub committed
265
266
267
268
269
270
271
272
273
274
275
276
277

    vlc_mutex_lock( &p_sap->object_lock );

    /* If needed, build the SDP */
    if( !p_session->psz_sdp )
    {
        if ( SDPGenerate( p_sap, p_session ) != VLC_SUCCESS )
        {
            vlc_mutex_unlock( &p_sap->object_lock );
            return VLC_EGENERIC;
        }
    }

278
    if( p_method->psz_address == NULL )
zorglub's avatar
zorglub committed
279
    {
280
        /* Determine SAP multicast address automatically */
281
        char psz_buf[NI_MAXNUMERICHOST], *ptr;
282
283
284
285
        const char *psz_addr;
        struct addrinfo hints, *res;

        if( p_session->psz_uri == NULL )
zorglub's avatar
zorglub committed
286
        {
287
288
289
290
291
292
293
294
295
296
297
            msg_Err( p_sap, "*FIXME* Unexpected NULL URI for SAP announce" );
            msg_Err( p_sap, "This should not happen. VLC needs fixing." );
            vlc_mutex_unlock( &p_sap->object_lock );
            return VLC_EGENERIC;
        }

        /* Canonicalize IP address (e.g. 224.00.010.1 => 224.0.10.1) */
        memset( &hints, 0, sizeof( hints ) );
        hints.ai_socktype = SOCK_DGRAM;
        hints.ai_flags = AI_NUMERICHOST;

298
        i = vlc_getaddrinfo( (vlc_object_t *)p_sap, p_session->psz_uri, 0,
299
300
                             &hints, &res );
        if( i == 0 )
301
302
            i = vlc_getnameinfo( res->ai_addr, res->ai_addrlen, psz_buf,
                                 sizeof( psz_buf ), NULL, NI_NUMERICHOST );
303
304
305
306
307
308
309
310
        if( i )
        {
            msg_Err( p_sap, "Invalid URI for SAP announce : %s : %s",
                     p_session->psz_uri, vlc_gai_strerror( i ) );
            vlc_mutex_unlock( &p_sap->object_lock );
            return VLC_EGENERIC;
        }

311
312
313
314
315
        /* Remove interface specification if present */
        ptr = strchr( psz_buf, '%' );
        if( ptr != NULL )
            *ptr = '\0';

316
317
318
319
320
321
322
323
        if( strchr( psz_buf, ':' ) != NULL )
        {
            b_ipv6 = VLC_TRUE;

            /* See RFC3513 for list of valid IPv6 scopes */
            if( ( tolower( psz_buf[0] ) == 'f' )
             && ( tolower( psz_buf[1] ) == 'f' )
             && isxdigit( psz_buf[2] ) && isxdigit( psz_buf[3] ) )
zorglub's avatar
zorglub committed
324
            {
325
326
327
328
329
                /* Multicast IPv6 */
                psz_buf[2] = '0'; /* force flags to zero */
                /* keep scope in psz_addr[3] */
                memcpy( &psz_buf[4], "::2:7ffe", sizeof( "::2:7ffe" ) );
                psz_addr = psz_buf;
zorglub's avatar
zorglub committed
330
331
            }
            else
332
333
                /* Unicast IPv6 - assume global scope */
                psz_addr = "ff0e::2:7ffe";
zorglub's avatar
zorglub committed
334
335
336
        }
        else
        {
337
338
339
340
341
342
343
344
345
346
347
348
349
            /* See RFC2365 for IPv4 scopes */
            if( memcmp( psz_buf, "224.0.0.", 8 ) == 0 )
                psz_addr = "224.0.0.255";
            else
            if( memcmp( psz_buf, "239.255.", 8 ) == 0 )
                psz_addr = "239.255.255.255";
            else
            if( ( memcmp( psz_buf, "239.19", 6 ) == 0 )
             && ( ( psz_buf[6] >= '2' ) && ( psz_buf[6] <= '5' ) ) )
                psz_addr = "239.195.255.255";
            else
                /* assume global scope */
                psz_addr = "224.2.127.254";
zorglub's avatar
zorglub committed
350
        }
351
352

        p_method->psz_address = strdup( psz_addr );
zorglub's avatar
zorglub committed
353
    }
354
    else
355
        b_ipv6 = (strchr( p_method->psz_address, ':' ) != NULL);
356
357

    msg_Dbg( p_sap, "using SAP address: %s", p_method->psz_address);
zorglub's avatar
zorglub committed
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389

    /* XXX: Check for dupes */
    p_sap_session = (sap_session_t*)malloc(sizeof(sap_session_t));

    p_sap_session->psz_sdp = strdup( p_session->psz_sdp );
    p_sap_session->i_last = 0;

    /* Add the address to the buffer */
    for( i = 0; i< p_sap->i_addresses; i++)
    {
        if( !strcmp( p_method->psz_address,
             p_sap->pp_addresses[i]->psz_address ) )
        {
            p_sap_session->p_address = p_sap->pp_addresses[i];
            b_found = VLC_TRUE;
            break;
        }
    }
    if( b_found == VLC_FALSE )
    {
        sap_address_t *p_address = (sap_address_t *)
                                    malloc( sizeof(sap_address_t) );
        if( !p_address )
        {
            msg_Err( p_sap, "out of memory" );
            return VLC_ENOMEM;
        }
        p_address->psz_address = strdup( p_method->psz_address );
        p_address->i_port  =  9875;
        p_address->i_wfd = net_OpenUDP( p_sap, "", 0,
                                        p_address->psz_address,
                                        p_address->i_port );
390
391
        if( p_address->i_wfd != -1 )
            net_StopRecv( p_address->i_wfd );
zorglub's avatar
zorglub committed
392
393
394
395
396
397

        if( p_sap->b_control == VLC_TRUE )
        {
            p_address->i_rfd = net_OpenUDP( p_sap, p_method->psz_address,
                                            p_address->i_port,
                                            "", 0 );
398
399
            if( p_address->i_rfd != -1 )
                net_StopSend( p_address->i_rfd );
zorglub's avatar
zorglub committed
400
401
402
403
404
405
406
407
408
409
410
            p_address->i_buff = 0;
            p_address->b_enabled = VLC_TRUE;
            p_address->b_ready = VLC_FALSE;
            p_address->i_limit = 10000; /* 10000 bps */
            p_address->t1 = 0;
        }
        else
        {
            p_address->b_enabled = VLC_TRUE;
            p_address->b_ready = VLC_TRUE;
            p_address->i_interval = config_GetInt( p_sap,"sap-interval");
411
            p_address->i_rfd = -1;
zorglub's avatar
zorglub committed
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
        }

        if( p_address->i_wfd == -1 || (p_address->i_rfd == -1
                                        && p_sap->b_control ) )
        {
            msg_Warn( p_sap, "disabling address" );
            p_address->b_enabled = VLC_FALSE;
        }

        INSERT_ELEM( p_sap->pp_addresses,
                     p_sap->i_addresses,
                     p_sap->i_addresses,
                     p_address );
        p_sap_session->p_address = p_address;
    }

    /* Build the SAP Headers */
429
    i_header_size = ( b_ipv6 ? 20 : 8 ) + strlen( psz_type ) + 1;
zorglub's avatar
zorglub committed
430
431
432
433
434
435
436
    psz_head = (char *) malloc( i_header_size * sizeof( char ) );
    if( ! psz_head )
    {
        msg_Err( p_sap, "out of memory" );
        return VLC_ENOMEM;
    }

437
438
    psz_head[0] = 0x20; /* Means SAPv1, IPv4, not encrypted, not compressed */
    psz_head[1] = 0x00; /* No authentification length */
zorglub's avatar
zorglub committed
439

440
441
442
443
    i_hash = mdate();
    psz_head[2] = (i_hash & 0xFF00) >> 8; /* Msg id hash */
    psz_head[3] = (i_hash & 0xFF);        /* Msg id hash 2 */

444
    if( b_ipv6 )
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
    {
        /* in_addr_t ip_server = inet_addr( ip ); */
        psz_head[0] |= 0x10; /* Set IPv6 */

        psz_head[4] = 0x01; /* Source IP  FIXME: we should get the real address */
        psz_head[5] = 0x02; /* idem */
        psz_head[6] = 0x03; /* idem */
        psz_head[7] = 0x04; /* idem */

        psz_head[8] = 0x01; /* Source IP  FIXME: we should get the real address */
        psz_head[9] = 0x02; /* idem */
        psz_head[10] = 0x03; /* idem */
        psz_head[11] = 0x04; /* idem */

        psz_head[12] = 0x01; /* Source IP  FIXME: we should get the real address */
        psz_head[13] = 0x02; /* idem */
        psz_head[14] = 0x03; /* idem */
        psz_head[15] = 0x04; /* idem */

        psz_head[16] = 0x01; /* Source IP  FIXME: we should get the real address */
        psz_head[17] = 0x02; /* idem */
        psz_head[18] = 0x03; /* idem */
        psz_head[19] = 0x04; /* idem */

        strncpy( psz_head + 20, psz_type, 15 );
    }
    else
    {
        /* in_addr_t ip_server = inet_addr( ip) */
        /* Source IP  FIXME: we should get the real address */
        psz_head[4] = 0x01; /* ip_server */
        psz_head[5] = 0x02; /* ip_server>>8 */
        psz_head[6] = 0x03; /* ip_server>>16 */
        psz_head[7] = 0x04; /* ip_server>>24 */

        strncpy( psz_head + 8, psz_type, 15 );
    }
zorglub's avatar
zorglub committed
482
483
484
485

    psz_head[ i_header_size-1 ] = '\0';
    p_sap_session->i_length = i_header_size + strlen( p_sap_session->psz_sdp);

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
486
487
    p_sap_session->psz_data = (uint8_t *)malloc( sizeof(char)*
                                                 p_sap_session->i_length );
zorglub's avatar
zorglub committed
488
489
490
491
492
493

    /* Build the final message */
    memcpy( p_sap_session->psz_data, psz_head, i_header_size );
    memcpy( p_sap_session->psz_data+i_header_size, p_sap_session->psz_sdp,
            strlen( p_sap_session->psz_sdp) );

zorglub's avatar
zorglub committed
494
495
    free( psz_head );

zorglub's avatar
zorglub committed
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
    /* Enqueue the announce */
    INSERT_ELEM( p_sap->pp_sessions,
                 p_sap->i_sessions,
                 p_sap->i_sessions,
                 p_sap_session );
    msg_Dbg( p_sap,"Addresses: %i  Sessions: %i",
                   p_sap->i_addresses,p_sap->i_sessions);

    /* Remember the SAP session for later deletion */
    p_session->p_sap = p_sap_session;

    vlc_mutex_unlock( &p_sap->object_lock );

    return VLC_SUCCESS;
}

/* Remove a SAP Announce */
static int announce_SAPAnnounceDel( sap_handler_t *p_sap,
                             session_descriptor_t *p_session )
{
    int i;
    vlc_mutex_lock( &p_sap->object_lock );

    msg_Dbg( p_sap,"removing SAP announce %p",p_session->p_sap);

    /* Dequeue the announce */
    for( i = 0; i< p_sap->i_sessions; i++)
    {
        if( p_session->p_sap == p_sap->pp_sessions[i] )
        {
            REMOVE_ELEM( p_sap->pp_sessions,
                         p_sap->i_sessions,
                         i );
529
530
531
532

            FREE( p_session->p_sap->psz_sdp );
            FREE( p_session->p_sap->psz_data );
            free( p_session->p_sap );
zorglub's avatar
zorglub committed
533
534
535
536
            break;
        }
    }

537
538
    /* XXX: Dequeue the address too if it is not used anymore
     * TODO: - address refcount
zorglub's avatar
zorglub committed
539
             - send a SAP deletion packet */
zorglub's avatar
zorglub committed
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565

    msg_Dbg( p_sap,"%i announces remaining", p_sap->i_sessions );

    vlc_mutex_unlock( &p_sap->object_lock );

    return VLC_SUCCESS;
}

static int announce_SendSAPAnnounce( sap_handler_t *p_sap,
                                     sap_session_t *p_session )
{
    int i_ret;

    /* This announce has never been sent yet */
    if( p_session->i_last == 0 )
    {
        p_session->i_next = mdate()+ p_session->p_address->i_interval*1000000;
        p_session->i_last = 1;
        return VLC_SUCCESS;
    }

    if( p_session->i_next < mdate() )
    {
#ifdef EXTRA_DEBUG
        msg_Dbg( p_sap, "Sending announce");
#endif
566
        i_ret = net_Write( p_sap, p_session->p_address->i_wfd, NULL,
zorglub's avatar
zorglub committed
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
                           p_session->psz_data,
                           p_session->i_length );
        if( i_ret  != p_session->i_length )
        {
            msg_Warn( p_sap, "SAP send failed on address %s (%i %i)",
                   p_session->p_address->psz_address,
                   i_ret, p_session->i_length );
        }
        p_session->i_last = p_session->i_next;
        p_session->i_next = p_session->i_last
                            + p_session->p_address->i_interval*1000000;
    }
    else
    {
        return VLC_SUCCESS;
    }
    return VLC_SUCCESS;
}

static int SDPGenerate( sap_handler_t *p_sap, session_descriptor_t *p_session )
{
588
589
    int64_t i_sdp_id = mdate();
    int     i_sdp_version = 1 + p_sap->i_sessions + (rand()&0xfff);
590
    char *psz_group, *psz_name, psz_uribuf[NI_MAXNUMERICHOST], *psz_uri;
591
    char ipv;
592
593
594

    psz_group = convert_to_utf8( p_sap, p_session->psz_group );
    psz_name = convert_to_utf8( p_sap, p_session->psz_name );
595

596
597
    /* FIXME: really check that psz_uri is a real IP address
     * FIXME: make a common function to obtain a canonical IP address */
598
    ipv = ( strchr( p_session->psz_uri, ':' )  != NULL) ? '6' : '4';
599
600
601
602
    if( *p_session->psz_uri == '[' )
    {
        char *ptr;

603
604
605
        strncpy( psz_uribuf, p_session->psz_uri + 1, sizeof( psz_uribuf ) );
        psz_uribuf[sizeof( psz_uribuf ) - 1] = '\0';
        ptr = strchr( psz_uribuf, '%' );
606
607
        if( ptr != NULL)
            *ptr = '\0';
608
        ptr = strchr( psz_uribuf, ']' );
609
610
        if( ptr != NULL)
            *ptr = '\0';
611
        psz_uri = psz_uribuf;
612
    }
613
614
    else
        psz_uri = p_session->psz_uri;
615

616
    /* see the lists in modules/stream_out/rtp.c for compliance stuff */
zorglub's avatar
zorglub committed
617
    p_session->psz_sdp = (char *)malloc(
618
619
620
621
622
623
624
625
                            sizeof("v=0\r\n"
                                   "o=- 45383436098 45398  IN IP4 127.0.0.1\r\n" /* FIXME */
                                   "s=\r\n"
                                   "t=0 0\r\n"
                                   "c=IN IP4 /\r\n"
                                   "m=video  udp\r\n"
                                   "a=tool:"PACKAGE_STRING"\r\n"
                                   "a=type:broadcast\r\n")
626
                           + strlen( psz_name )
627
                           + strlen( psz_uri ) + 300
628
                           + ( psz_group ? strlen( psz_group ) : 0 ) );
yoann's avatar
yoann committed
629

630
    if( p_session->psz_sdp == NULL || psz_name == NULL )
zorglub's avatar
zorglub committed
631
632
    {
        msg_Err( p_sap, "out of memory" );
633
634
        FREE( psz_name );
        FREE( psz_group );
zorglub's avatar
zorglub committed
635
636
        return VLC_ENOMEM;
    }
637

zorglub's avatar
zorglub committed
638
    sprintf( p_session->psz_sdp,
639
640
641
642
                            "v=0\r\n"
                            "o=- "I64Fd" %d IN IP4 127.0.0.1\r\n"
                            "s=%s\r\n"
                            "t=0 0\r\n"
643
                            "c=IN IP%c %s/%d\r\n"
644
645
646
647
                            "m=video %d udp %d\r\n"
                            "a=tool:"PACKAGE_STRING"\r\n"
                            "a=type:broadcast\r\n",
                            i_sdp_id, i_sdp_version,
648
                            psz_name, ipv,
649
                            psz_uri, p_session->i_ttl,
zorglub's avatar
zorglub committed
650
                            p_session->i_port, p_session->i_payload );
651
    free( psz_name );
yoann's avatar
yoann committed
652

653
    if( psz_group )
yoann's avatar
yoann committed
654
655
    {
        sprintf( p_session->psz_sdp, "%sa=x-plgroup:%s\r\n",
656
657
                                     p_session->psz_sdp, psz_group );
        free( psz_group );
yoann's avatar
yoann committed
658
659
    }

zorglub's avatar
zorglub committed
660
661
662
663
664
665
666
667
    msg_Dbg( p_sap, "Generated SDP (%i bytes):\n%s", strlen(p_session->psz_sdp),
                    p_session->psz_sdp );
    return VLC_SUCCESS;
}

static int CalculateRate( sap_handler_t *p_sap, sap_address_t *p_address )
{
    int i_read;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
668
    uint8_t buffer[SAP_MAX_BUFFER];
zorglub's avatar
zorglub committed
669
670
671
672
673
674
675
676
677
678
679
680
    int i_tot = 0;
    mtime_t i_temp;
    int i_rate;

    if( p_address->t1 == 0 )
    {
        p_address->t1 = mdate();
        return VLC_SUCCESS;
    }
    do
    {
        /* Might be too slow if we have huge data */
681
        i_read = net_ReadNonBlock( p_sap, p_address->i_rfd, NULL, buffer,
zorglub's avatar
zorglub committed
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
                                   SAP_MAX_BUFFER, 0 );
        i_tot += i_read;
    } while( i_read > 0 && i_tot < SAP_MAX_BUFFER );

    i_temp = mdate();

    /* We calculate the rate every 5 seconds */
    if( i_temp - p_address->t1 < 5000000 )
    {
        p_address->i_buff += i_tot;
        return VLC_SUCCESS;
    }

    /* Bits/second */
    i_rate = (int)(8*1000000*((mtime_t)p_address->i_buff + (mtime_t)i_tot ) /
                        (i_temp - p_address->t1 ));

    p_address->i_limit = 10000;

    p_address->i_interval = ((1000*i_rate / p_address->i_limit) *
                            (MAX_INTERVAL - MIN_INTERVAL))/1000 + MIN_INTERVAL;

    if( p_address->i_interval > MAX_INTERVAL || p_address->i_interval < 0 )
    {
        p_address->i_interval = MAX_INTERVAL;
    }
#ifdef EXTRA_DEBUG
    msg_Dbg( p_sap,"%s:%i : Rate=%i, Interval = %i s",
                    p_address->psz_address,p_address->i_port,
                    i_rate,
                    p_address->i_interval );
#endif

    p_address->b_ready = VLC_TRUE;

    p_address->t1 = i_temp;
    p_address->i_buff = 0;

    return VLC_SUCCESS;
}
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752


static char *convert_to_utf8( struct sap_handler_t *p_this, char *psz_local )
{
    char *psz_unicode, *psz_in, *psz_out;
    size_t ret, i_in, i_out;

    if( psz_local == NULL )
        return NULL;
    if ( p_this->iconvHandle == (vlc_iconv_t)(-1) )
        return strdup( psz_local );

    psz_in = psz_local;
    i_in = strlen( psz_local );

    i_out = 6 * i_in;
    psz_unicode = malloc( i_out + 1 );
    if( psz_unicode == NULL )
        return strdup( psz_local );
    psz_out = psz_unicode;

    ret = vlc_iconv( p_this->iconvHandle,
                     &psz_in, &i_in, &psz_out, &i_out);
    if( ret == (size_t)(-1) || i_in )
    {
        msg_Warn( p_this, "Failed to convert \"%s\" to UTF-8", psz_local );
        return strdup( psz_local );
    }
    *psz_out = '\0';
    return psz_unicode;
}