stream_output.c 30 KB
Newer Older
1
2
3
/*****************************************************************************
 * stream_output.c : stream output module
 *****************************************************************************
zorglub's avatar
zorglub committed
4
 * Copyright (C) 2002-2004 VideoLAN
5
 * $Id$
6
7
 *
 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8
 *          Laurent Aimar <fenrir@via.ecp.fr>
9
 *          Eric Petit <titer@videolan.org>
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 *
 * 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() */

#include <vlc/vlc.h>
#include <vlc/sout.h>
35
36
37

#include "vlc_meta.h"

38
#undef DEBUG_BUFFER
39
40
41
/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
42
43
static void sout_cfg_free( sout_cfg_t * );

44
45
46
#define sout_stream_url_to_chain( p, s ) _sout_stream_url_to_chain( VLC_OBJECT(p), s )
static char *_sout_stream_url_to_chain( vlc_object_t *, char * );

47
48
49
50
51
52
53
/*
 * Generic MRL parser
 *
 */

typedef struct
{
54
55
    char *psz_access;
    char *psz_way;
56
57
58
59
60
61
62
63
64
    char *psz_name;
} mrl_t;

/* mrl_Parse: parse psz_mrl and fill p_mrl */
static int  mrl_Parse( mrl_t *p_mrl, char *psz_mrl );
/* mrl_Clean: clean p_mrl  after a call to mrl_Parse */
static void mrl_Clean( mrl_t *p_mrl );

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

66
67
68
/*****************************************************************************
 * sout_NewInstance: creates a new stream output instance
 *****************************************************************************/
69
sout_instance_t *__sout_NewInstance( vlc_object_t *p_parent, char * psz_dest )
70
{
71
    sout_instance_t *p_sout;
72
73
74
75
76
77
78
79
80
81
    vlc_value_t keep;

    if( var_Get( p_parent, "sout-keep", &keep ) < 0 )
    {
        msg_Warn( p_parent, "cannot get sout-keep value" );
        keep.b_bool = VLC_FALSE;
    }
    else if( keep.b_bool )
    {
        msg_Warn( p_parent, "sout-keep true" );
82
83
        if( ( p_sout = vlc_object_find( p_parent, VLC_OBJECT_SOUT,
                                        FIND_ANYWHERE ) ) )
84
85
86
87
        {
            if( !strcmp( p_sout->psz_sout, psz_dest ) )
            {
                msg_Warn( p_parent, "sout keep : reusing sout" );
88
89
                msg_Warn( p_parent, "sout keep : you probably want to use "
                          "gather stream_out" );
90
91
92
93
94
95
96
97
98
99
100
101
102
103
                vlc_object_detach( p_sout );
                vlc_object_attach( p_sout, p_parent );
                vlc_object_release( p_sout );
                return p_sout;
            }
            else
            {
                msg_Warn( p_parent, "sout keep : destroying unusable sout" );
                sout_DeleteInstance( p_sout );
            }
        }
    }
    else if( !keep.b_bool )
    {
104
105
        while( ( p_sout = vlc_object_find( p_parent, VLC_OBJECT_SOUT,
                                           FIND_PARENT ) ) )
106
107
108
109
110
        {
            msg_Warn( p_parent, "sout keep : destroying old sout" );
            sout_DeleteInstance( p_sout );
        }
    }
111

112
    /* *** Allocate descriptor *** */
113
114
115
116
117
118
119
    p_sout = vlc_object_create( p_parent, VLC_OBJECT_SOUT );
    if( p_sout == NULL )
    {
        msg_Err( p_parent, "out of memory" );
        return NULL;
    }

120
121
    /* *** init descriptor *** */
    p_sout->psz_sout    = strdup( psz_dest );
122
    p_sout->p_meta      = NULL;
123
    p_sout->i_out_pace_nocontrol = 0;
124
    p_sout->p_sys       = NULL;
125
126

    vlc_mutex_init( p_sout, &p_sout->lock );
127
    if( psz_dest && psz_dest[0] == '#' )
128
    {
129
        p_sout->psz_chain = strdup( &psz_dest[1] );
130
    }
131
    else
132
    {
133
134
        p_sout->psz_chain = sout_stream_url_to_chain( p_sout, psz_dest );
        msg_Dbg( p_sout, "using sout chain=`%s'", p_sout->psz_chain );
135
    }
136
137
138
139
    p_sout->p_stream = NULL;

    /* attach it for inherit */
    vlc_object_attach( p_sout, p_parent );
140

141
    p_sout->p_stream = sout_stream_new( p_sout, p_sout->psz_chain );
142

143
    if( p_sout->p_stream == NULL )
144
    {
145
146
147
148
149
        msg_Err( p_sout, "stream chained failed for `%s'", p_sout->psz_chain );

        FREE( p_sout->psz_sout );
        FREE( p_sout->psz_chain );

150
        vlc_object_detach( p_sout );
151
        vlc_object_destroy( p_sout );
152
        return NULL;
153
154
155
156
    }

    return p_sout;
}
157
158
159
160
161
162
163
164
/*****************************************************************************
 * sout_DeleteInstance: delete a previously allocated instance
 *****************************************************************************/
void sout_DeleteInstance( sout_instance_t * p_sout )
{
    /* Unlink object */
    vlc_object_detach( p_sout );

165
166
167
    /* remove the stream out chain */
    sout_stream_delete( p_sout->p_stream );

168
169
    /* *** free all string *** */
    FREE( p_sout->psz_sout );
170
    FREE( p_sout->psz_chain );
171

172
    /* delete meta */
173
174
175
176
177
    if( p_sout->p_meta )
    {
        vlc_meta_Delete( p_sout->p_meta );
    }

178
179
    vlc_mutex_destroy( &p_sout->lock );

180
    /* *** free structure *** */
181
182
183
    vlc_object_destroy( p_sout );
}

184
/*****************************************************************************
185
 * Packetizer/Input
186
 *****************************************************************************/
187
188
sout_packetizer_input_t *sout_InputNew( sout_instance_t *p_sout,
                                        es_format_t *p_fmt )
189
{
190
    sout_packetizer_input_t *p_input;
191

192
    msg_Dbg( p_sout, "adding a new input" );
193

194
195
196
197
    /* *** create a packetizer input *** */
    p_input         = malloc( sizeof( sout_packetizer_input_t ) );
    p_input->p_sout = p_sout;
    p_input->p_fmt  = p_fmt;
198

199
    if( p_fmt->i_codec == VLC_FOURCC( 'n', 'u', 'l', 'l' ) )
200
201
202
    {
        vlc_object_release( p_sout );
        return p_input;
203
204
    }

205
    /* *** add it to the stream chain */
206
    vlc_mutex_lock( &p_sout->lock );
207
    p_input->id = p_sout->p_stream->pf_add( p_sout->p_stream, p_fmt );
208
    vlc_mutex_unlock( &p_sout->lock );
209

210
    if( p_input->id == NULL )
211
    {
212
        free( p_input );
213
        return NULL;
214
    }
215

216
    return( p_input );
217
}
218

219
220
221
/*****************************************************************************
 *
 *****************************************************************************/
222
int sout_InputDelete( sout_packetizer_input_t *p_input )
223
{
224
225
226
    sout_instance_t     *p_sout = p_input->p_sout;

    msg_Dbg( p_sout, "removing an input" );
227

228
    if( p_input->p_fmt->i_codec != VLC_FOURCC( 'n', 'u', 'l', 'l' ) )
229
    {
230
231
232
        vlc_mutex_lock( &p_sout->lock );
        p_sout->p_stream->pf_del( p_sout->p_stream, p_input->id );
        vlc_mutex_unlock( &p_sout->lock );
233
    }
234

235
    free( p_input );
236

237
238
    return( VLC_SUCCESS);
}
239

240
241
242
/*****************************************************************************
 *
 *****************************************************************************/
gbazin's avatar
   
gbazin committed
243
int sout_InputSendBuffer( sout_packetizer_input_t *p_input,
244
                          block_t *p_buffer )
245
246
{
    sout_instance_t     *p_sout = p_input->p_sout;
247
    int                 i_ret;
248

249
    if( p_input->p_fmt->i_codec == VLC_FOURCC( 'n', 'u', 'l', 'l' ) )
250
    {
251
        block_Release( p_buffer );
252
253
        return VLC_SUCCESS;
    }
254
    if( p_buffer->i_dts <= 0 )
gbazin's avatar
   
gbazin committed
255
256
    {
        msg_Warn( p_sout, "trying to send non-dated packet to stream output!");
257
        block_Release( p_buffer );
gbazin's avatar
   
gbazin committed
258
259
260
        return VLC_SUCCESS;
    }

261
    vlc_mutex_lock( &p_sout->lock );
gbazin's avatar
   
gbazin committed
262
263
    i_ret = p_sout->p_stream->pf_send( p_sout->p_stream,
                                       p_input->id, p_buffer );
264
    vlc_mutex_unlock( &p_sout->lock );
265

266
    return i_ret;
267
}
268

269
270
271
272
273
274
275
/*****************************************************************************
 * sout_AccessOutNew: allocate a new access out
 *****************************************************************************/
sout_access_out_t *sout_AccessOutNew( sout_instance_t *p_sout,
                                      char *psz_access, char *psz_name )
{
    sout_access_out_t *p_access;
276
    char              *psz_next;
277
278
279
280
281
282
283

    if( !( p_access = vlc_object_create( p_sout,
                                         sizeof( sout_access_out_t ) ) ) )
    {
        msg_Err( p_sout, "out of memory" );
        return NULL;
    }
284

gbazin's avatar
   
gbazin committed
285
286
    psz_next = sout_cfg_parser( &p_access->psz_access, &p_access->p_cfg,
                                psz_access );
287
288
289
290
    if( psz_next )
    {
        free( psz_next );
    }
291
292
293
294
    p_access->psz_name   = strdup( psz_name ? psz_name : "" );
    p_access->p_sout     = p_sout;
    p_access->p_sys = NULL;
    p_access->pf_seek    = NULL;
gbazin's avatar
   
gbazin committed
295
    p_access->pf_read    = NULL;
296
    p_access->pf_write   = NULL;
297
298
    p_access->p_module   = NULL;
    vlc_object_attach( p_access, p_sout );
299

300
    p_access->p_module   =
gbazin's avatar
   
gbazin committed
301
        module_Need( p_access, "sout access", p_access->psz_access, VLC_TRUE );
302
303
304

    if( !p_access->p_module )
    {
305
306
        free( p_access->psz_access );
        free( p_access->psz_name );
307
        vlc_object_detach( p_access );
308
        vlc_object_destroy( p_access );
309
        return( NULL );
310
311
312
313
314
315
316
    }

    return p_access;
}
/*****************************************************************************
 * sout_AccessDelete: delete an access out
 *****************************************************************************/
317
void sout_AccessOutDelete( sout_access_out_t *p_access )
318
{
319
    vlc_object_detach( p_access );
320
321
322
323
324
    if( p_access->p_module )
    {
        module_Unneed( p_access, p_access->p_module );
    }
    free( p_access->psz_access );
325
326
327

    sout_cfg_free( p_access->p_cfg );

328
329
330
331
332
333
334
335
    free( p_access->psz_name );

    vlc_object_destroy( p_access );
}

/*****************************************************************************
 * sout_AccessSeek:
 *****************************************************************************/
gbazin's avatar
   
gbazin committed
336
int sout_AccessOutSeek( sout_access_out_t *p_access, off_t i_pos )
337
{
gbazin's avatar
   
gbazin committed
338
    return p_access->pf_seek( p_access, i_pos );
339
340
341
}

/*****************************************************************************
gbazin's avatar
   
gbazin committed
342
 * sout_AccessRead:
343
 *****************************************************************************/
344
int sout_AccessOutRead( sout_access_out_t *p_access, block_t *p_buffer )
345
{
346
347
    return( p_access->pf_read ?
            p_access->pf_read( p_access, p_buffer ) : VLC_EGENERIC );
348
349
}

gbazin's avatar
   
gbazin committed
350
351
352
/*****************************************************************************
 * sout_AccessWrite:
 *****************************************************************************/
353
int sout_AccessOutWrite( sout_access_out_t *p_access, block_t *p_buffer )
gbazin's avatar
   
gbazin committed
354
355
356
{
    return p_access->pf_write( p_access, p_buffer );
}
357

358
/*****************************************************************************
359
 * sout_MuxNew: create a new mux
360
 *****************************************************************************/
361
362
sout_mux_t * sout_MuxNew( sout_instance_t *p_sout, char *psz_mux,
                          sout_access_out_t *p_access )
363
364
{
    sout_mux_t *p_mux;
365
    char       *psz_next;
366
367
368
369
370
371
372
373
374
375

    p_mux = vlc_object_create( p_sout,
                               sizeof( sout_mux_t ) );
    if( p_mux == NULL )
    {
        msg_Err( p_sout, "out of memory" );
        return NULL;
    }

    p_mux->p_sout       = p_sout;
376
377
378
379
380
    psz_next = sout_cfg_parser( &p_mux->psz_mux, &p_mux->p_cfg, psz_mux );
    if( psz_next )
    {
        free( psz_next );
    }
381
382
383
384
385
386
387
388
389
    p_mux->p_access     = p_access;
    p_mux->pf_capacity  = NULL;
    p_mux->pf_addstream = NULL;
    p_mux->pf_delstream = NULL;
    p_mux->pf_mux       = NULL;
    p_mux->i_nb_inputs  = 0;
    p_mux->pp_inputs    = NULL;

    p_mux->p_sys        = NULL;
390
391
392
    p_mux->p_module = NULL;

    vlc_object_attach( p_mux, p_sout );
393

394
    p_mux->p_module     =
gbazin's avatar
   
gbazin committed
395
        module_Need( p_mux, "sout mux", p_mux->psz_mux, VLC_TRUE );
396

397
398
399
400
    if( p_mux->p_module == NULL )
    {
        FREE( p_mux->psz_mux );

401
        vlc_object_detach( p_mux );
402
403
404
405
406
407
408
409
410
        vlc_object_destroy( p_mux );
        return NULL;
    }

    /* *** probe mux capacity *** */
    if( p_mux->pf_capacity )
    {
        int b_answer;
        if( p_mux->pf_capacity( p_mux,
gbazin's avatar
   
gbazin committed
411
412
                                SOUT_MUX_CAP_GET_ADD_STREAM_ANY_TIME, NULL,
                                (void*)&b_answer ) != SOUT_MUX_CAP_ERR_OK )
413
414
415
416
417
418
419
420
        {
            b_answer = VLC_FALSE;
        }
        if( b_answer )
        {
            msg_Dbg( p_sout, "muxer support adding stream at any time" );
            p_mux->b_add_stream_any_time = VLC_TRUE;
            p_mux->b_waiting_stream = VLC_FALSE;
gbazin's avatar
   
gbazin committed
421
422
423
424
425
426
427
428
429
430
431
432
433

            if( p_mux->pf_capacity( p_mux,
                                    SOUT_MUX_CAP_GET_ADD_STREAM_WAIT, NULL,
                                    (void*)&b_answer ) != SOUT_MUX_CAP_ERR_OK )
            {
                b_answer = VLC_FALSE;
            }
            if( b_answer )
            {
                msg_Dbg( p_sout, "muxer prefers waiting for all ES before "
                         "starting muxing" );
                p_mux->b_waiting_stream = VLC_TRUE;
            }
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
        }
        else
        {
            p_mux->b_add_stream_any_time = VLC_FALSE;
            p_mux->b_waiting_stream = VLC_TRUE;
        }
    }
    else
    {
        p_mux->b_add_stream_any_time = VLC_FALSE;
        p_mux->b_waiting_stream = VLC_TRUE;
    }
    p_mux->i_add_stream_start = -1;

    return p_mux;
}

451
452
453
/*****************************************************************************
 * sout_MuxDelete:
 *****************************************************************************/
gbazin's avatar
   
gbazin committed
454
void sout_MuxDelete( sout_mux_t *p_mux )
455
{
456
    vlc_object_detach( p_mux );
457
458
459
460
461
462
    if( p_mux->p_module )
    {
        module_Unneed( p_mux, p_mux->p_module );
    }
    free( p_mux->psz_mux );

463
464
    sout_cfg_free( p_mux->p_cfg );

465
466
467
    vlc_object_destroy( p_mux );
}

468
469
470
/*****************************************************************************
 * sout_MuxAddStream:
 *****************************************************************************/
gbazin's avatar
   
gbazin committed
471
sout_input_t *sout_MuxAddStream( sout_mux_t *p_mux, es_format_t *p_fmt )
472
473
474
{
    sout_input_t *p_input;

gbazin's avatar
   
gbazin committed
475
    if( !p_mux->b_add_stream_any_time && !p_mux->b_waiting_stream )
476
    {
gbazin's avatar
   
gbazin committed
477
478
        msg_Err( p_mux, "cannot add a new stream (unsuported while muxing "
                        "for this format)" );
479
480
481
482
483
484
485
486
487
        return NULL;
    }
    if( p_mux->i_add_stream_start < 0 )
    {
        /* we wait for one second */
        p_mux->i_add_stream_start = mdate();
    }

    msg_Dbg( p_mux, "adding a new input" );
488

489
    /* create a new sout input */
490
491
492
    p_input = malloc( sizeof( sout_input_t ) );
    p_input->p_sout = p_mux->p_sout;
    p_input->p_fmt  = p_fmt;
493
    p_input->p_fifo = block_FifoNew( p_mux->p_sout );
494
    p_input->p_sys  = NULL;
495
496
497
498
499

    TAB_APPEND( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
    if( p_mux->pf_addstream( p_mux, p_input ) < 0 )
    {
            msg_Err( p_mux, "cannot add this stream" );
gbazin's avatar
   
gbazin committed
500
            TAB_REMOVE( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );
501
            block_FifoRelease( p_input->p_fifo );
gbazin's avatar
   
gbazin committed
502
            free( p_input );
503
            return NULL;
504
505
    }

506
    return p_input;
507
508
}

509
510
511
512
/*****************************************************************************
 * sout_MuxDeleteStream:
 *****************************************************************************/
void sout_MuxDeleteStream( sout_mux_t *p_mux, sout_input_t *p_input )
513
514
515
{
    int i_index;

516
517
518
519
520
521
522
523
    if( p_mux->b_waiting_stream && p_input->p_fifo->i_depth > 0 )
    {
        /* We stop waiting, and call the muxer for taking care of the data
         * before we remove this es */
        p_mux->b_waiting_stream = VLC_FALSE;
        p_mux->pf_mux( p_mux );
    }

524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
    TAB_FIND( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input, i_index );
    if( i_index >= 0 )
    {
        if( p_mux->pf_delstream( p_mux, p_input ) < 0 )
        {
            msg_Err( p_mux, "cannot del this stream from mux" );
        }

        /* remove the entry */
        TAB_REMOVE( p_mux->i_nb_inputs, p_mux->pp_inputs, p_input );

        if( p_mux->i_nb_inputs == 0 )
        {
            msg_Warn( p_mux, "no more input stream for this mux" );
        }

540
        block_FifoRelease( p_input->p_fifo );
541
        free( p_input );
542
543
544
    }
}

545
546
547
548
549
/*****************************************************************************
 * sout_MuxSendBuffer:
 *****************************************************************************/
void sout_MuxSendBuffer( sout_mux_t *p_mux, sout_input_t *p_input,
                         block_t *p_buffer )
550
{
551
    block_FifoPut( p_input->p_fifo, p_buffer );
552
553
554
555

    if( p_mux->b_waiting_stream )
    {
        if( p_mux->i_add_stream_start > 0 &&
556
            p_mux->i_add_stream_start + (mtime_t)1500000 < mdate() )
557
        {
558
            /* more than 1.5 second, start muxing */
559
560
561
562
563
564
565
566
567
568
            p_mux->b_waiting_stream = VLC_FALSE;
        }
        else
        {
            return;
        }
    }
    p_mux->pf_mux( p_mux );
}

569
570
571
/*****************************************************************************
 *
 *****************************************************************************/
572
static int mrl_Parse( mrl_t *p_mrl, char *psz_mrl )
573
{
Laurent Aimar's avatar
Laurent Aimar committed
574
    char * psz_dup = strdup( psz_mrl );
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
    char * psz_parser = psz_dup;
    char * psz_access = "";
    char * psz_way = "";
    char * psz_name = "";

    /* *** first parse psz_dest */
    while( *psz_parser && *psz_parser != ':' )
    {
        if( *psz_parser == '{' )
        {
            while( *psz_parser && *psz_parser != '}' )
            {
                psz_parser++;
            }
            if( *psz_parser )
            {
                psz_parser++;
            }
        }
        else
        {
            psz_parser++;
        }
    }
#if defined( WIN32 ) || defined( UNDER_CE )
    if( psz_parser - psz_dup == 1 )
    {
602
603
        /* msg_Warn( p_sout, "drive letter %c: found in source string",
                          *psz_dup ) ; */
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
        psz_parser = "";
    }
#endif

    if( !*psz_parser )
    {
        psz_access = psz_way = "";
        psz_name = psz_dup;
    }
    else
    {
        *psz_parser++ = '\0';

        /* let's skip '//' */
        if( psz_parser[0] == '/' && psz_parser[1] == '/' )
        {
            psz_parser += 2 ;
        }

        psz_name = psz_parser ;

        /* Come back to parse the access and mux plug-ins */
        psz_parser = psz_dup;

        if( !*psz_parser )
        {
            /* No access */
            psz_access = "";
        }
        else if( *psz_parser == '/' )
        {
            /* No access */
            psz_access = "";
            psz_parser++;
        }
        else
        {
            psz_access = psz_parser;

            while( *psz_parser && *psz_parser != '/' )
            {
                if( *psz_parser == '{' )
                {
                    while( *psz_parser && *psz_parser != '}' )
                    {
                        psz_parser++;
                    }
                    if( *psz_parser )
                    {
                        psz_parser++;
                    }
                }
                else
                {
                    psz_parser++;
                }
            }

            if( *psz_parser == '/' )
            {
                *psz_parser++ = '\0';
            }
        }

        if( !*psz_parser )
        {
            /* No mux */
            psz_way = "";
        }
        else
        {
            psz_way = psz_parser;
        }
    }

    p_mrl->psz_access = strdup( psz_access );
    p_mrl->psz_way    = strdup( psz_way );
    p_mrl->psz_name   = strdup( psz_name );

Laurent Aimar's avatar
Laurent Aimar committed
683
    free( psz_dup );
684
685
686
687
688
689
690
691
692
693
694
695
696
    return( VLC_SUCCESS );
}


/* mrl_Clean: clean p_mrl  after a call to mrl_Parse */
static void mrl_Clean( mrl_t *p_mrl )
{
    FREE( p_mrl->psz_access );
    FREE( p_mrl->psz_way );
    FREE( p_mrl->psz_name );
}


697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
/****************************************************************************
 ****************************************************************************
 **
 **
 **
 ****************************************************************************
 ****************************************************************************/

/* create a complete chain */
/* chain format:
    module{option=*:option=*}[:module{option=*:...}]
 */

/*
 * parse module{options=str, option="str "}:
 *  return a pointer on the rest
 *  XXX: psz_chain is modified
 */
#define SKIPSPACE( p ) { while( *p && ( *p == ' ' || *p == '\t' ) ) p++; }
716
717
718
#define SKIPTRAILINGSPACE( p, e ) \
    { while( e > p && ( *(e-1) == ' ' || *(e-1) == '\t' ) ) e--; }

719
720
721
/* go accross " " and { } */
static char *_get_chain_end( char *str )
{
722
    char c, *p = str;
723
724
725
726
727

    SKIPSPACE( p );

    for( ;; )
    {
728
        if( !*p || *p == ',' || *p == '}' ) return p;
729

730
731
        if( *p != '{' && *p != '"' && *p != '\'' )
        {
732
            p++;
733
734
            continue;
        }
735

736
737
738
        if( *p == '{' ) c = '}';
        else c = *p;
        p++;
739

740
        for( ;; )
741
        {
742
743
744
745
746
            if( !*p ) return p;

            if( *p == c ) return ++p;
            else if( *p == '{' && c == '}' ) p = _get_chain_end( p );
            else p++;
747
748
749
750
        }
    }
}

751
char *sout_cfg_parser( char **ppsz_name, sout_cfg_t **pp_cfg, char *psz_chain )
752
753
754
755
756
757
758
{
    sout_cfg_t *p_cfg = NULL;
    char       *p = psz_chain;

    *ppsz_name = NULL;
    *pp_cfg    = NULL;

759
    if( !p ) return NULL;
760

761
762
    SKIPSPACE( p );

763
    while( *p && *p != '{' && *p != ':' && *p != ' ' && *p != '\t' ) p++;
764

765
    if( p == psz_chain ) return NULL;
766

767
    *ppsz_name = strndup( psz_chain, p - psz_chain );
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784

    SKIPSPACE( p );

    if( *p == '{' )
    {
        char *psz_name;

        p++;

        for( ;; )
        {
            sout_cfg_t cfg;

            SKIPSPACE( p );

            psz_name = p;

785
786
            while( *p && *p != '=' && *p != ',' && *p != '{' && *p != '}' &&
                   *p != ' ' && *p != '\t' ) p++;
787

788
            /* fprintf( stderr, "name=%s - rest=%s\n", psz_name, p ); */
789
790
791
792
793
794
            if( p == psz_name )
            {
                fprintf( stderr, "invalid options (empty)" );
                break;
            }

795
            cfg.psz_name = strndup( psz_name, p - psz_name );
796
797
798

            SKIPSPACE( p );

799
            if( *p == '=' || *p == '{' )
800
801
            {
                char *end;
802
                vlc_bool_t b_keep_brackets = (*p == '{');
803

804
                if( *p == '=' ) p++;
805
806
807
808
809
810
811
812

                end = _get_chain_end( p );
                if( end <= p )
                {
                    cfg.psz_value = NULL;
                }
                else
                {
813
814
815
816
817
818
819
820
821
822
823
824
825
                    /* Skip heading and trailing spaces.
                     * This ain't necessary but will avoid simple
                     * user mistakes. */
                    SKIPSPACE( p );
                }

                if( end <= p )
                {
                    cfg.psz_value = NULL;
                }
                else
                {
                    if( *p == '\'' || *p == '"' ||
826
                        ( !b_keep_brackets && *p == '{' ) )
827
828
                    {
                        p++;
829
830
831
832
833
834
835
836
837
838
839
840

                        if( *(end-1) != '\'' && *(end-1) == '"' )
                            SKIPTRAILINGSPACE( p, end );

                        if( end - 1 <= p ) cfg.psz_value = NULL;
                        else cfg.psz_value = strndup( p, end -1 - p );
                    }
                    else
                    {
                        SKIPTRAILINGSPACE( p, end );
                        if( end <= p ) cfg.psz_value = NULL;
                        else cfg.psz_value = strndup( p, end - p );
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
                    }
                }

                p = end;
                SKIPSPACE( p );
            }
            else
            {
                cfg.psz_value = NULL;
            }

            cfg.p_next = NULL;
            if( p_cfg )
            {
                p_cfg->p_next = malloc( sizeof( sout_cfg_t ) );
                memcpy( p_cfg->p_next, &cfg, sizeof( sout_cfg_t ) );

                p_cfg = p_cfg->p_next;
            }
            else
            {
                p_cfg = malloc( sizeof( sout_cfg_t ) );
                memcpy( p_cfg, &cfg, sizeof( sout_cfg_t ) );

                *pp_cfg = p_cfg;
            }

868
            if( *p == ',' ) p++;
869
870
871
872
873
874
875
876
877

            if( *p == '}' )
            {
                p++;
                break;
            }
        }
    }

878
    if( *p == ':' ) return( strdup( p + 1 ) );
879

880
    return NULL;
881
882
}

883
884
885
886
887
static void sout_cfg_free( sout_cfg_t *p_cfg )
{
    while( p_cfg != NULL )
    {
        sout_cfg_t *p_next;
888

889
        p_next = p_cfg->p_next;
890

891
892
893
894
895
896
897
        FREE( p_cfg->psz_name );
        FREE( p_cfg->psz_value );
        free( p_cfg );

        p_cfg = p_next;
    }
}
898

899
900
void __sout_ParseCfg( vlc_object_t *p_this, char *psz_prefix,
                      const char **ppsz_options, sout_cfg_t *cfg )
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
{
    char *psz_name;
    int  i_type;
    int  i;

    /* First, var_Create all variables */
    for( i = 0; ppsz_options[i] != NULL; i++ )
    {
        asprintf( &psz_name, "%s%s", psz_prefix, ppsz_options[i] );

        i_type = config_GetType( p_this, psz_name );

        var_Create( p_this, psz_name, i_type | VLC_VAR_DOINHERIT );
        free( psz_name );
    }

    /* Now parse options and set value */
    if( psz_prefix == NULL ) psz_prefix = "";

    while( cfg )
    {
        vlc_value_t val;
923
        vlc_bool_t b_yes = VLC_TRUE;
924
925
926
927
928
929
930
931
932
933
934
935

        if( cfg->psz_name == NULL || *cfg->psz_name == '\0' )
        {
            cfg = cfg->p_next;
            continue;
        }
        for( i = 0; ppsz_options[i] != NULL; i++ )
        {
            if( !strcmp( ppsz_options[i], cfg->psz_name ) )
            {
                break;
            }
936
937
938
939
            if( ( !strncmp( cfg->psz_name, "no-", 3 ) &&
                  !strcmp( ppsz_options[i], cfg->psz_name + 3 ) ) ||
                ( !strncmp( cfg->psz_name, "no", 2 ) &&
                  !strcmp( ppsz_options[i], cfg->psz_name + 2 ) ) )
940
941
942
943
            {
                b_yes = VLC_FALSE;
                break;
            }
944
945
946
        }
        if( ppsz_options[i] == NULL )
        {
Laurent Aimar's avatar
Laurent Aimar committed
947
            msg_Warn( p_this, "option %s is unknown", cfg->psz_name );
948
949
950
951
952
            cfg = cfg->p_next;
            continue;
        }

        /* create name */
953
        asprintf( &psz_name, "%s%s", psz_prefix, ppsz_options[i] );
954
955
956
957
958

        /* get the type of the variable */
        i_type = config_GetType( p_this, psz_name );
        if( !i_type )
        {
959
960
            msg_Warn( p_this, "unknown option %s (value=%s)",
                      cfg->psz_name, cfg->psz_value );
961
962
963
964
965
966
967
968
969
970
            goto next;
        }
        if( i_type != VLC_VAR_BOOL && cfg->psz_value == NULL )
        {
            msg_Warn( p_this, "missing value for option %s", cfg->psz_name );
            goto next;
        }

        switch( i_type )
        {
971
972
973
            case VLC_VAR_BOOL:
                val.b_bool = b_yes;
                break;
974
            case VLC_VAR_INTEGER:
975
976
                val.i_int = strtol( cfg->psz_value ? cfg->psz_value : "0",
                                    NULL, 0 );
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
                break;
            case VLC_VAR_FLOAT:
                val.f_float = atof( cfg->psz_value ? cfg->psz_value : "0" );
                break;
            case VLC_VAR_STRING:
                val.psz_string = cfg->psz_value;
                break;
            default:
                msg_Warn( p_this, "unhandled config var type" );
                memset( &val, 0, sizeof( vlc_value_t ) );
                break;
        }
        var_Set( p_this, psz_name, val );
        msg_Dbg( p_this, "set sout option: %s to %s", psz_name, cfg->psz_value );

    next:
        free( psz_name );
        cfg = cfg->p_next;
    }
}

998
999
1000

/*
 * XXX name and p_cfg are used (-> do NOT free them)
For faster browsing, not all history is shown. View entire blame