profiles.c 17.9 KB
Newer Older
1
2
3
/*****************************************************************************
 * profiles.c: Streaming profiles
 *****************************************************************************
Christophe Mutricy's avatar
Christophe Mutricy committed
4
5
 * Copyright (C) 2006 the VideoLAN team
 * $Id$
6
 *
7
 * Authors: Clément Stenac <zorglub@videolan.org>
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#include <vlc_streaming.h>
zorglub's avatar
zorglub committed
25
#include <assert.h>
26
27
28
29
30
31
32
33
34
35
36
37
38
39

#define MAX_CHAIN 32768
#define CHAIN_APPEND( format, args... ) { \
    memcpy( psz_temp, psz_output, MAX_CHAIN ); \
    snprintf( psz_output, MAX_CHAIN - 1, "%s"  format , psz_temp, ## args ); }

#define DUPM p_module->typed.p_duplicate
#define STDM p_module->typed.p_std
#define DISM p_module->typed.p_display
#define TRAM p_module->typed.p_transcode

/**********************************************************************
 * General chain manipulation
 **********************************************************************/
40
41
42
/** Add a new duplicate element to a streaming chain
 * \return the new element
 */
43
44
45
46
47
48
49
sout_duplicate_t *streaming_ChainAddDup( sout_chain_t *p_chain )
{
    DECMALLOC_NULL( p_module, sout_module_t );
    MALLOC_NULL( DUPM, sout_duplicate_t );
    p_module->i_type = SOUT_MOD_DUPLICATE;
    DUPM->i_children = 0;
    DUPM->pp_children = NULL;
50
51
    TAB_APPEND( p_chain->i_modules, p_chain->pp_modules, p_module );
    return DUPM;
52
53
}

54
55
56
/** Add a new standard element to a streaming chain
 * \return the new element
 */
57
58
59
60
61
62
63
64
65
sout_std_t *streaming_ChainAddStd( sout_chain_t *p_chain, char *psz_access,
                                   char *psz_mux, char *psz_url )
{
    DECMALLOC_NULL( p_module, sout_module_t );
    MALLOC_NULL( STDM, sout_std_t );
    p_module->i_type = SOUT_MOD_STD;
    STDM->psz_mux = strdup( psz_mux );
    STDM->psz_access = strdup( psz_access );
    STDM->psz_url = strdup( psz_url );
66
    TAB_APPEND( p_chain->i_modules, p_chain->pp_modules, p_module );
67
68
    return STDM;
}
69
70
71
/** Add a new display element to a streaming chain
 * \return the new element
 */
72
73
74
75
76
sout_display_t *streaming_ChainAddDisplay( sout_chain_t *p_chain )
{
    DECMALLOC_NULL( p_module, sout_module_t );
    MALLOC_NULL( DISM, sout_display_t );
    p_module->i_type = SOUT_MOD_DISPLAY;
77
    TAB_APPEND( p_chain->i_modules, p_chain->pp_modules, p_module );
78
79
80
    return DISM;
}

81
82
83
/** Add a new transcode element to a streaming chain
 * \return the new element
 */
84
85
sout_transcode_t *streaming_ChainAddTranscode( sout_chain_t *p_chain,
                        char *psz_vcodec, char * psz_acodec, char * psz_scodec,
86
                        int i_vb, float f_scale, int i_ab, int i_channels,
zorglub's avatar
zorglub committed
87
                        vlc_bool_t b_soverlay, char *psz_additional )
88
89
90
91
{
    DECMALLOC_NULL( p_module, sout_module_t );
    MALLOC_NULL( TRAM, sout_transcode_t );
    p_module->i_type = SOUT_MOD_TRANSCODE;
zorglub's avatar
zorglub committed
92
93
94
95
96

    assert( !( b_soverlay && psz_scodec ) );
    if( psz_vcodec ) TRAM->psz_vcodec = strdup( psz_vcodec );
    if( psz_acodec ) TRAM->psz_acodec = strdup( psz_acodec );
    if( psz_scodec ) TRAM->psz_scodec = strdup( psz_scodec );
97
    TRAM->i_vb = i_vb; TRAM->i_ab = i_ab; TRAM->f_scale = f_scale;
zorglub's avatar
zorglub committed
98
99
    TRAM->i_channels = i_channels; TRAM->b_soverlay = b_soverlay;
    if( TRAM->psz_additional ) TRAM->psz_additional = strdup( psz_additional );
100
    TAB_APPEND( p_chain->i_modules, p_chain->pp_modules, p_module );
101
102
    return TRAM;
}
103
104

/** Add a new clean child chain to an existing duplicate element */
105
106
void streaming_DupAddChild( sout_duplicate_t *p_dup )
{
107
108
109
    assert( p_dup );
    sout_chain_t * p_child = streaming_ChainNew();
    TAB_APPEND( p_dup->i_children, p_dup->pp_children, p_child );
110
111
112
113
114
115
116
117
118
119
}

#define DUP_OR_CHAIN p_dup ? p_dup->pp_children[p_dup->i_children-1] : p_chain

#define ADD_OPT( format, args... ) { \
    char *psz_opt; asprintf( &psz_opt, format, ##args ); \
    INSERT_ELEM( p_chain->ppsz_options, p_chain->i_options, p_chain->i_options,\
                 psz_opt );\
    free( psz_opt ); }

120
/** Clean up a chain (recursively if it has some children) */
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
void streaming_ChainClean( sout_chain_t *p_chain )
{
    int i,j;
    sout_module_t *p_module;
    if( p_chain->i_modules )
    {
        for( i = p_chain->i_modules -1; i >= 0 ; i-- )
        {
            p_module = p_chain->pp_modules[i];
            switch( p_module->i_type )
            {
            case SOUT_MOD_DUPLICATE:
                if( DUPM->i_children == 0 ) break;
                for( j = DUPM->i_children - 1 ; j >= 0; j-- )
                    streaming_ChainClean( DUPM->pp_children[j] );
                break;
            case SOUT_MOD_STD:
                FREENULL( STDM->psz_url );
                FREENULL( STDM->psz_name );
                FREENULL( STDM->psz_group );
                break;
            case SOUT_MOD_TRANSCODE:
                FREENULL( TRAM->psz_vcodec );
                FREENULL( TRAM->psz_acodec );
                FREENULL( TRAM->psz_scodec );
                FREENULL( TRAM->psz_venc );
                FREENULL( TRAM->psz_aenc );
zorglub's avatar
zorglub committed
148
                FREENULL( TRAM->psz_additional );
149
150
151
                break;
            }
            REMOVE_ELEM( p_chain->pp_modules, p_chain->i_modules, i );
zorglub's avatar
zorglub committed
152
            free( p_module );
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
190
191
192
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
/**********************************************************************
 * Parameters handling
 **********************************************************************/

#define APPLY_PSZ( element, field ) case element: \
streaming_ParameterApply( p_param, &p_module->field, NULL, NULL, NULL ); break;
#define APPLY_INT( element, field ) case element: \
streaming_ParameterApply( p_param, NULL, &p_module->field, NULL, NULL ); break;
#define APPLY_FLOAT( element, field ) case element: \
streaming_ParameterApply( p_param, NULL, NULL, &p_module->field, NULL ); break;
#define APPLY_BOOL( element, field ) case element: \
streaming_ParameterApply( p_param, NULL, NULL, NULL, &p_module->field ); break;

/** Apply the parameters for the Std module. It will copy the values from
 * the parameters to the fields themselves
 */
void streaming_StdParametersApply( sout_std_t *p_module )
{
    int i;
    for( i = 0 ; i< p_module->i_params; i++ )
    {
        sout_param_t *p_param = p_module->pp_params[i];
        switch( p_param->i_element )
        {
            APPLY_PSZ( PSZ_MUX, psz_mux );
            APPLY_PSZ( PSZ_ACCESS, psz_access );
            APPLY_PSZ( PSZ_URL, psz_url );
            APPLY_PSZ( PSZ_NAME, psz_name );
            APPLY_PSZ( PSZ_GROUP, psz_group );
        }
    }
}

/** Apply the parameters for the Transcode module. It will copy the values from
 * the parameters to the fields themselves
 */
void streaming_TranscodeParametersApply( sout_transcode_t *p_module )
{
    int i;
    for( i = 0 ; i< p_module->i_params; i++ )
    {
        sout_param_t *p_param = p_module->pp_params[i];
        switch( p_param->i_element )
        {
            APPLY_INT( I_VB, i_vb ); APPLY_INT( I_AB, i_ab );
            APPLY_INT( I_CHANNELS, i_channels ) ;
            APPLY_FLOAT( F_SCALE, f_scale );
            APPLY_BOOL( B_SOVERLAY, b_soverlay );
            APPLY_PSZ( PSZ_VC, psz_vcodec );
            APPLY_PSZ( PSZ_AC, psz_acodec );
            APPLY_PSZ( PSZ_SC, psz_scodec );
            APPLY_PSZ( PSZ_VE, psz_venc ); APPLY_PSZ( PSZ_AE, psz_aenc );
        }
    }
}

/** Apply a single parameter
 * \param p_param the parameter to apply
 * \param ppsz_dest target string, if param is a string
 * \param pi_dest target int, if param is an integer
 * \param pf_dest target float, if param is a float
 * \param pb_dest target bool, if param is a bool
 */
void streaming_ParameterApply( sout_param_t *p_param, char **ppsz_dest,
                             int *pi_dest, float *pf_dest, vlc_bool_t *pb_dest )
{
    /* Todo : Handle psz_string like formatting */
    if( p_param->psz_string )
    {
        assert( ppsz_dest );
        fprintf( stderr, "Unsupported !\n" );
    }
    else
    {
        switch( p_param->i_type )
        {
        case VLC_VAR_INTEGER:
            assert( pi_dest );
            *pi_dest = p_param->value.i_int;
            break;
        case VLC_VAR_FLOAT:
            assert( pf_dest );
            *pf_dest = p_param->value.f_float;
            break;
        case VLC_VAR_STRING:
            assert( ppsz_dest );
zorglub's avatar
zorglub committed
243
            free( *ppsz_dest );
244
245
246
247
248
249
250
251
252
253
254
255
            *ppsz_dest = p_param->value.psz_string ?
                                strdup( p_param->value.psz_string ) :
                                NULL;
            break;
        case VLC_VAR_BOOL:
            assert( pb_dest );
            *pb_dest = p_param->value.b_bool;
            break;
        }
    }
}

256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/**********************************************************************
 * Interaction with streaming GUI descriptors
 **********************************************************************/
#define DO_ENABLE_ACCESS \
    if( !strcmp( STDM->psz_access, "file" ) )\
    { \
        pd->b_file = VLC_TRUE; pd->psz_file = strdup( STDM->psz_url ); \
    } \
    else if(  !strcmp( STDM->psz_access, "http" ) )\
    { \
        pd->b_http = VLC_TRUE; pd->psz_http = strdup( STDM->psz_url ); \
    } \
    else if(  !strcmp( STDM->psz_access, "mms" ) )\
    { \
        pd->b_mms = VLC_TRUE; pd->psz_mms = strdup( STDM->psz_url ); \
    } \
    else if(  !strcmp( STDM->psz_access, "udp" ) )\
    { \
        pd->b_udp = VLC_TRUE; pd->psz_udp = strdup( STDM->psz_url ); \
    } \
    else \
    { \
        msg_Err( p_this, "unahandled access %s", STDM->psz_access ); \
    }


/**
283
284
 * Try to convert a chain to a gui descriptor. This is only possible for
 * "simple" chains.
285
286
287
288
289
 * \param p_this vlc object
 * \param p_chain the source streaming chain
 * \param pd the destination gui descriptor object
 * \return TRUE if the conversion succeeded, false else
 */
zorglub's avatar
zorglub committed
290
291
vlc_bool_t streaming_ChainToGuiDesc( vlc_object_t *p_this,
                                  sout_chain_t *p_chain, sout_gui_descr_t *pd )
292
293
294
295
296
297
298
299
300
301
302
{
    int j, i_last = 0;
    sout_module_t *p_module;
    if( p_chain->i_modules == 0 || p_chain->i_modules > 2 ) return VLC_FALSE;

    if( p_chain->pp_modules[0]->i_type == SOUT_MOD_TRANSCODE )
    {
        if( p_chain->i_modules == 1 ) return VLC_FALSE;
        p_module = p_chain->pp_modules[0];
        i_last++;

zorglub's avatar
zorglub committed
303
        pd->b_soverlay = TRAM->b_soverlay;
304
305
        pd->i_vb = TRAM->i_vb; pd->i_ab = TRAM->i_ab;
        pd->i_channels = TRAM->i_channels; pd->f_scale = TRAM->f_scale;
zorglub's avatar
zorglub committed
306
307
308
        if( TRAM->psz_vcodec ) pd->psz_vcodec = strdup( TRAM->psz_vcodec );
        if( TRAM->psz_acodec ) pd->psz_acodec = strdup( TRAM->psz_acodec );
        if( TRAM->psz_scodec ) pd->psz_scodec = strdup( TRAM->psz_scodec );
309
310
311
312
    }
    if( p_chain->pp_modules[i_last]->i_type == SOUT_MOD_DUPLICATE )
    {
        p_module = p_chain->pp_modules[i_last];
313

314
        // Nothing allowed after duplicate. Duplicate mustn't be empty
315
        if( p_chain->i_modules > i_last +1 || !DUPM->i_children )
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
            return VLC_FALSE;
        for( j = 0 ; j<  DUPM->i_children ; j++ )
        {
            sout_chain_t *p_child = DUPM->pp_children[j];
            if( p_child->i_modules != 1 ) return VLC_FALSE;
            p_module = p_child->pp_modules[0];
            if( p_module->i_type == SOUT_MOD_STD )
            {
                DO_ENABLE_ACCESS
            }
            else if( p_module->i_type == SOUT_MOD_DISPLAY )
                pd->b_local = VLC_TRUE;
            else if( p_module->i_type == SOUT_MOD_RTP )
            {
                msg_Err( p_this, "RTP unhandled" );
                return VLC_FALSE;
            }
        }
        i_last++;
    }
    if( p_chain->pp_modules[i_last]->i_type == SOUT_MOD_STD )
    {
        p_module = p_chain->pp_modules[i_last];
        DO_ENABLE_ACCESS;
    }
    else if( p_chain->pp_modules[i_last]->i_type == SOUT_MOD_DISPLAY )
    {
        pd->b_local = VLC_TRUE;
    }
    else if( p_chain->pp_modules[i_last]->i_type == SOUT_MOD_RTP )
    {
        msg_Err( p_this, "RTP unhandled" );
        return VLC_FALSE;

    }
    return VLC_TRUE;

}

#define HANDLE_GUI_URL( type, access ) if( pd->b_##type ) { \
        streaming_DupAddChild( p_dup ); \
        if( pd->i_##type > 0 ) \
        { \
            char *psz_url; \
            asprintf( &psz_url, "%s:%i", pd->psz_##type, pd->i_##type ); \
            streaming_ChainAddStd( DUP_OR_CHAIN, access, pd->psz_mux,\
                                   psz_url ); \
            free( psz_url ); \
        } \
        else \
        { \
            streaming_ChainAddStd( DUP_OR_CHAIN, access, pd->psz_mux,\
                                   pd->psz_##type );\
        }\
    }

void streaming_GuiDescToChain( vlc_object_t *p_obj, sout_chain_t *p_chain,
                               sout_gui_descr_t *pd )
{
    sout_duplicate_t *p_dup = NULL;
    /* Clean up the chain */
    streaming_ChainClean( p_chain );

    /* Transcode */
zorglub's avatar
zorglub committed
380
    if( pd->psz_vcodec || pd->psz_acodec || pd->psz_scodec || pd->b_soverlay )
381
382
383
    {
        streaming_ChainAddTranscode( p_chain, pd->psz_vcodec, pd->psz_acodec,
                                     pd->psz_scodec, pd->i_vb, pd->f_scale,
384
                                     pd->i_ab, pd->i_channels,
zorglub's avatar
zorglub committed
385
                                     pd->b_soverlay, NULL );
386
387
    }
    /* #std{} */
388
    if( pd->b_local + pd->b_file + pd->b_http + pd->b_mms + pd->b_rtp +
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
        pd->b_udp > 1 )
    {
        p_dup = streaming_ChainAddDup( p_chain );
    }
    if( pd->b_local )
    {
        streaming_DupAddChild( p_dup );
        streaming_ChainAddDisplay(  DUP_OR_CHAIN );
    }
    if( pd->b_file )
    {
        streaming_DupAddChild( p_dup );
        streaming_ChainAddStd( DUP_OR_CHAIN, "file", pd->psz_mux,
                               pd->psz_file );
    }
    if( pd->b_udp )
    {
        sout_std_t *p_std;
        streaming_DupAddChild( p_dup );
        if( pd->i_udp > 0 )
        {
            char *psz_url;
            asprintf( &psz_url, "%s:%i", pd->psz_udp, pd->i_udp );
            p_std = streaming_ChainAddStd( DUP_OR_CHAIN, "udp",
                                           pd->psz_mux, psz_url );
            free( psz_url );
        }
        else
        {
            p_std = streaming_ChainAddStd( DUP_OR_CHAIN, "udp",
                                           pd->psz_mux, pd->psz_udp );
        }
        if( pd->i_ttl ) ADD_OPT( "ttl=%i", pd->i_ttl );
        if( pd->b_sap )
        {
            pd->b_sap = VLC_TRUE;
425
            p_std->psz_name = strdup( pd->psz_name );
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
            p_std->psz_group = pd->psz_group ? strdup( pd->psz_group ) : NULL;
        }
    }
    HANDLE_GUI_URL( http, "http" )
    HANDLE_GUI_URL( mms, "mms" )
}
#undef HANDLE_GUI_URL

/**********************************************************************
 * Create a sout string from a chain
 **********************************************************************/
char * streaming_ChainToPsz( sout_chain_t *p_chain )
{
    int i;
    char psz_output[MAX_CHAIN];
    char psz_temp[MAX_CHAIN];
    sprintf( psz_output, "#" );
    for( i = 0 ; i< p_chain->i_modules; i++ )
    {
        sout_module_t *p_module = p_chain->pp_modules[i];
        switch( p_module->i_type )
        {
        case SOUT_MOD_TRANSCODE:
            CHAIN_APPEND( "transcode{" );
zorglub's avatar
zorglub committed
450
            if( TRAM->psz_vcodec )
451
452
453
            {
                CHAIN_APPEND( "vcodec=%s,vb=%i,scale=%f", TRAM->psz_vcodec,
                                     TRAM->i_vb, TRAM->f_scale );
zorglub's avatar
zorglub committed
454
                if( TRAM->psz_acodec || TRAM->psz_scodec || TRAM->b_soverlay )
455
456
                    CHAIN_APPEND( "," );
            }
zorglub's avatar
zorglub committed
457
            if( TRAM->psz_acodec )
458
459
460
            {
                CHAIN_APPEND( "acodec=%s,ab=%i,channels=%i", TRAM->psz_acodec,
                              TRAM->i_ab, TRAM->i_channels );
zorglub's avatar
zorglub committed
461
                if( TRAM->psz_scodec || TRAM->b_soverlay )
462
463
                    CHAIN_APPEND( "," );
            }
zorglub's avatar
zorglub committed
464
465
            assert( !(TRAM->psz_scodec && TRAM->b_soverlay) );
            if( TRAM->psz_scodec )
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
                CHAIN_APPEND( "scodec=%s", TRAM->psz_scodec) ;
            if( TRAM->b_soverlay )
                CHAIN_APPEND( "soverlay" );
            CHAIN_APPEND( "}" );
            break;

        case SOUT_MOD_DISPLAY:
            CHAIN_APPEND( "display" )
            break;
        case SOUT_MOD_STD:
            CHAIN_APPEND( "std{access=%s,url=%s,mux=%s}", STDM->psz_access,
                          STDM->psz_url, STDM->psz_mux );
        }
    }
    return strdup( psz_output );
}

/**********************************************************************
 * Handle streaming profiles
 **********************************************************************/

/**
 * List the available profiles. Fills the pp_profiles list with preinitialized
489
 * values.
490
491
492
493
 * \param p_this vlc object
 * \param pi_profiles number of listed profiles
 * \param pp_profiles array of profiles
 */
494
void streaming_ProfilesList( vlc_object_t *p_this, int *pi_profiles,
495
496
497
498
499
                             streaming_profile_t **pp_profiles )
{
}


zorglub's avatar
zorglub committed
500
501
502
/** Parse a profile */
int streaming_ProfileParse( vlc_object_t *p_this,streaming_profile_t *p_profile,
                            const char *psz_profile )
503
{
zorglub's avatar
zorglub committed
504
    module_t *p_module;
505
    DECMALLOC_ERR( p_parser, profile_parser_t );
zorglub's avatar
zorglub committed
506
    assert( p_profile ); assert( psz_profile );
507

zorglub's avatar
zorglub committed
508
509
510
511
512
513
514
515
    p_parser->psz_profile = strdup( psz_profile );
    p_parser->p_profile = p_profile;

    p_this->p_private = (void *)p_parser;

    /* And call the module ! All work is done now */
    p_module = module_Need( p_this, "profile parser", "", VLC_TRUE );
    if( !p_module )
516
    {
zorglub's avatar
zorglub committed
517
        msg_Warn( p_this, "parsing profile failed" );
518
        return VLC_EGENERIC;
519
    }
520
    return VLC_SUCCESS;
521
}