es.c 13.1 KB
Newer Older
1
/*****************************************************************************
gbazin's avatar
   
gbazin committed
2
 * es.c: Elementary stream output module
3
 *****************************************************************************
4
 * Copyright (C) 2003-2004 the VideoLAN team
5
 * $Id$
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 *
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
 *
 * 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
dionoea's avatar
dionoea committed
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22
23
24
25
26
27
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/

28
29
30
31
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

32
#include <vlc/vlc.h>
33
#include <vlc_plugin.h>
zorglub's avatar
zorglub committed
34
35
36
#include <vlc_input.h>
#include <vlc_sout.h>
#include <vlc_interface.h>
37
38

/*****************************************************************************
39
 * Module descriptor
40
 *****************************************************************************/
41
42
#define ACCESS_TEXT N_("Output access method")
#define ACCESS_LONGTEXT N_( \
43
44
    "This is the default output access method that will be used." )

45
46
#define ACCESSA_TEXT N_("Audio output access method")
#define ACCESSA_LONGTEXT N_( \
47
    "This is the output access method that will be used for audio." )
48
49
#define ACCESSV_TEXT N_("Video output access method")
#define ACCESSV_LONGTEXT N_( \
50
    "This is the output access method that will be used for video." )
51
52
53

#define MUX_TEXT N_("Output muxer")
#define MUX_LONGTEXT N_( \
54
    "This is the default muxer method that will be used." )
55
56
#define MUXA_TEXT N_("Audio output muxer")
#define MUXA_LONGTEXT N_( \
57
    "This is the muxer that will be used for audio." )
58
59
#define MUXV_TEXT N_("Video output muxer")
#define MUXV_LONGTEXT N_( \
60
    "This is the muxer that will be used for video." )
61
62
63

#define DEST_TEXT N_("Output URL")
#define DEST_LONGTEXT N_( \
64
    "This is the default output URI." )
65
66
#define DESTA_TEXT N_("Audio output URL")
#define DESTA_LONGTEXT N_( \
67
    "This is the output URI that will be used for audio." )
68
69
#define DESTV_TEXT N_("Video output URL")
#define DESTV_LONGTEXT N_( \
70
    "This is the output URI that will be used for video." )
71

72
73
74
static int      Open    ( vlc_object_t * );
static void     Close   ( vlc_object_t * );

75
#define SOUT_CFG_PREFIX "sout-es-"
76
77

vlc_module_begin();
78
    set_shortname( "ES" );
79
    set_description( N_("Elementary stream output") );
80
81
    set_capability( "sout stream", 50 );
    add_shortcut( "es" );
zorglub's avatar
zorglub committed
82
83
    set_category( CAT_SOUT );
    set_subcategory( SUBCAT_SOUT_STREAM );
84

85
    add_string( SOUT_CFG_PREFIX "access", "", NULL, ACCESS_TEXT,
86
                ACCESS_LONGTEXT, true );
87
    add_string( SOUT_CFG_PREFIX "access-audio", "", NULL, ACCESSA_TEXT,
88
                ACCESSA_LONGTEXT, true );
89
    add_string( SOUT_CFG_PREFIX "access-video", "", NULL, ACCESSV_TEXT,
90
                ACCESSV_LONGTEXT, true );
91

92
    add_string( SOUT_CFG_PREFIX "mux", "", NULL, MUX_TEXT,
93
                MUX_LONGTEXT, true );
94
    add_string( SOUT_CFG_PREFIX "mux-audio", "", NULL, MUXA_TEXT,
95
                MUXA_LONGTEXT, true );
96
    add_string( SOUT_CFG_PREFIX "mux-video", "", NULL, MUXV_TEXT,
97
                MUXV_LONGTEXT, true );
98

99
    add_string( SOUT_CFG_PREFIX "dst", "", NULL, DEST_TEXT,
100
                DEST_LONGTEXT, true );
101
        change_unsafe();
102
    add_string( SOUT_CFG_PREFIX "dst-audio", "", NULL, DESTA_TEXT,
103
                DESTA_LONGTEXT, true );
104
        change_unsafe();
105
    add_string( SOUT_CFG_PREFIX "dst-video", "", NULL, DESTV_TEXT,
106
                DESTV_LONGTEXT, true );
107
        change_unsafe();
108

109
110
111
    set_callbacks( Open, Close );
vlc_module_end();

112
113
114
/*****************************************************************************
 * Exported prototypes
 *****************************************************************************/
115
static const char *const ppsz_sout_options[] = {
116
117
118
119
120
121
122
123
124
125
    "access", "access-audio", "access-video",
    "mux", "mux-audio", "mux-video",
    "dst", "dst-audio", "dst-video",
    NULL
};

static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
static int               Del ( sout_stream_t *, sout_stream_id_t * );
static int               Send( sout_stream_t *, sout_stream_id_t *, block_t* );

126
127
128
129
130
131
132
133
134
135
136
137
138
139
struct sout_stream_sys_t
{
    int  i_count_audio;
    int  i_count_video;
    int  i_count;

    char *psz_mux;
    char *psz_mux_audio;
    char *psz_mux_video;

    char *psz_access;
    char *psz_access_audio;
    char *psz_access_video;

140
141
142
    char *psz_dst;
    char *psz_dst_audio;
    char *psz_dst_video;
143
144
145
146
147
148
149
150
151
};

/*****************************************************************************
 * Open:
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
    sout_stream_t       *p_stream = (sout_stream_t*)p_this;
    sout_stream_sys_t   *p_sys;
152
    vlc_value_t         val;
153

154
    config_ChainParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options, p_stream->p_cfg );
155
156
157
158
159
160
    p_sys                   = malloc( sizeof( sout_stream_sys_t ) );

    p_sys->i_count          = 0;
    p_sys->i_count_audio    = 0;
    p_sys->i_count_video    = 0;

161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
    var_Get( p_stream, SOUT_CFG_PREFIX "access", &val );
    p_sys->psz_access       = val.psz_string;
    var_Get( p_stream, SOUT_CFG_PREFIX "access-audio", &val );
    p_sys->psz_access_audio = val.psz_string;
    var_Get( p_stream, SOUT_CFG_PREFIX "access-video", &val );
    p_sys->psz_access_video = val.psz_string;

    var_Get( p_stream, SOUT_CFG_PREFIX "mux", &val );
    p_sys->psz_mux       = val.psz_string;
    var_Get( p_stream, SOUT_CFG_PREFIX "mux-audio", &val );
    p_sys->psz_mux_audio = val.psz_string;
    var_Get( p_stream, SOUT_CFG_PREFIX "mux-video", &val );
    p_sys->psz_mux_video = val.psz_string;

    var_Get( p_stream, SOUT_CFG_PREFIX "dst", &val );
    p_sys->psz_dst       = val.psz_string;
    var_Get( p_stream, SOUT_CFG_PREFIX "dst-audio", &val );
    p_sys->psz_dst_audio = val.psz_string;
    var_Get( p_stream, SOUT_CFG_PREFIX "dst-video", &val );
    p_sys->psz_dst_video = val.psz_string;
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199

    p_stream->pf_add    = Add;
    p_stream->pf_del    = Del;
    p_stream->pf_send   = Send;

    p_stream->p_sys     = p_sys;

    return VLC_SUCCESS;
}

/*****************************************************************************
 * Close:
 *****************************************************************************/

static void Close( vlc_object_t * p_this )
{
    sout_stream_t     *p_stream = (sout_stream_t*)p_this;
    sout_stream_sys_t *p_sys = p_stream->p_sys;

200
201
202
203
204
205
206
207
208
209
210
211
    free( p_sys->psz_access );
    free( p_sys->psz_access_audio );
    free( p_sys->psz_access_video );

    free( p_sys->psz_mux );
    free( p_sys->psz_mux_audio );
    free( p_sys->psz_mux_video );

    free( p_sys->psz_dst );
    free( p_sys->psz_dst_audio );
    free( p_sys->psz_dst_video );

212
213
214
215
216
217
218
219
220
    free( p_sys );
}

struct sout_stream_id_t
{
    sout_input_t *p_input;
    sout_mux_t   *p_mux;
};

gbazin's avatar
   
gbazin committed
221
222
static char * es_print_url( char *psz_fmt, vlc_fourcc_t i_fourcc, int i_count,
                            char *psz_access, char *psz_mux )
223
{
224
    char *psz_dst, *p;
225
226
227
228
229
230

    if( psz_fmt == NULL || !*psz_fmt )
    {
        psz_fmt = "stream-%n-%c.%m";
    }

231
    p = psz_dst = malloc( 4096 );
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
    memset( p, 0, 4096 );
    for( ;; )
    {
        if( *psz_fmt == '\0' )
        {
            *p = '\0';
            break;
        }

        if( *psz_fmt != '%' )
        {
            *p++ = *psz_fmt++;
        }
        else
        {
            if( psz_fmt[1] == 'n' )
            {
                p += sprintf( p, "%d", i_count );
            }
            else if( psz_fmt[1] == 'c' )
            {
                p += sprintf( p, "%4.4s", (char*)&i_fourcc );
            }
            else if( psz_fmt[1] == 'm' )
            {
                p += sprintf( p, "%s", psz_mux );
            }
            else if( psz_fmt[1] == 'a' )
            {
                p += sprintf( p, "%s", psz_access );
            }
            else if( psz_fmt[1] != '\0' )
            {
                p += sprintf( p, "%c%c", psz_fmt[0], psz_fmt[1] );
            }
            else
            {
                p += sprintf( p, "%c", psz_fmt[0] );
                *p++ = '\0';
                break;
            }
            psz_fmt += 2;
        }
    }

277
    return( psz_dst );
278
279
}

gbazin's avatar
   
gbazin committed
280
static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
281
282
283
284
285
286
287
{
    sout_stream_sys_t *p_sys = p_stream->p_sys;
    sout_instance_t   *p_sout = p_stream->p_sout;
    sout_stream_id_t  *id;

    char              *psz_access;
    char              *psz_mux;
288
    char              *psz_dst;
289
290
291
292
293

    sout_access_out_t *p_access;
    sout_mux_t        *p_mux;

    /* *** get access name *** */
294
    if( p_fmt->i_cat == AUDIO_ES && p_sys->psz_access_audio && *p_sys->psz_access_audio )
295
296
297
    {
        psz_access = p_sys->psz_access_audio;
    }
298
    else if( p_fmt->i_cat == VIDEO_ES && p_sys->psz_access_video && *p_sys->psz_access_video )
299
300
301
302
303
304
305
306
307
    {
        psz_access = p_sys->psz_access_video;
    }
    else
    {
        psz_access = p_sys->psz_access;
    }

    /* *** get mux name *** */
308
    if( p_fmt->i_cat == AUDIO_ES && p_sys->psz_mux_audio && *p_sys->psz_mux_audio )
309
310
311
    {
        psz_mux = p_sys->psz_mux_audio;
    }
312
    else if( p_fmt->i_cat == VIDEO_ES && p_sys->psz_mux_video && *p_sys->psz_mux_video )
313
314
315
316
317
318
319
320
    {
        psz_mux = p_sys->psz_mux_video;
    }
    else
    {
        psz_mux = p_sys->psz_mux;
    }

gbazin's avatar
   
gbazin committed
321
    /* Get url (%d expanded as a codec count, %c expanded as codec fcc ) */
322
    if( p_fmt->i_cat == AUDIO_ES && p_sys->psz_dst_audio && *p_sys->psz_dst_audio )
323
    {
324
        psz_dst = es_print_url( p_sys->psz_dst_audio, p_fmt->i_codec,
gbazin's avatar
   
gbazin committed
325
                                p_sys->i_count_audio, psz_access, psz_mux );
326
    }
327
    else if( p_fmt->i_cat == VIDEO_ES && p_sys->psz_dst_video && *p_sys->psz_dst_video )
328
    {
329
        psz_dst = es_print_url( p_sys->psz_dst_video, p_fmt->i_codec,
gbazin's avatar
   
gbazin committed
330
                                p_sys->i_count_video, psz_access, psz_mux );
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
    }
    else
    {
        int i_count;
        if( p_fmt->i_cat == VIDEO_ES )
        {
            i_count = p_sys->i_count_video;
        }
        else if( p_fmt->i_cat == AUDIO_ES )
        {
            i_count = p_sys->i_count_audio;
        }
        else
        {
            i_count = p_sys->i_count;
        }

348
        psz_dst = es_print_url( p_sys->psz_dst, p_fmt->i_codec,
gbazin's avatar
   
gbazin committed
349
                                i_count, psz_access, psz_mux );
350
351
352
353
354
355
356
357
358
359
360
361
    }

    p_sys->i_count++;
    if( p_fmt->i_cat == VIDEO_ES )
    {
        p_sys->i_count_video++;
    }
    else if( p_fmt->i_cat == AUDIO_ES )
    {
        p_sys->i_count_audio++;
    }
    msg_Dbg( p_stream, "creating `%s/%s://%s'",
362
             psz_access, psz_mux, psz_dst );
363
364

    /* *** find and open appropriate access module *** */
365
    p_access = sout_AccessOutNew( p_sout, psz_access, psz_dst );
366
367
368
    if( p_access == NULL )
    {
        msg_Err( p_stream, "no suitable sout access module for `%s/%s://%s'",
369
                 psz_access, psz_mux, psz_dst );
370
        intf_UserFatal( p_stream, false,
371
372
373
                    _("Streaming / Transcoding failed"),
                    _("There is no suitable stream-output access module for \"%s/%s://%s\"."),
                          psz_access,
374
                          psz_mux, psz_dst );
375
376
377
378
379
380
381
382
        return( NULL );
    }

    /* *** find and open appropriate mux module *** */
    p_mux = sout_MuxNew( p_sout, psz_mux, p_access );
    if( p_mux == NULL )
    {
        msg_Err( p_stream, "no suitable sout mux module for `%s/%s://%s'",
383
                 psz_access, psz_mux, psz_dst );
384
        intf_UserFatal( p_stream, false,
385
                        _("Streaming / Transcoding failed"),
386
                        _("There is no suitable stream-output access module "\
387
                          "for \"%s/%s://%s\"."),
388
                          psz_access, psz_mux, psz_dst );
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
        sout_AccessOutDelete( p_access );
        return( NULL );
    }

    id = malloc( sizeof( sout_stream_id_t ) );
    id->p_mux = p_mux;
    id->p_input = sout_MuxAddStream( p_mux, p_fmt );

    if( id->p_input == NULL )
    {
        sout_MuxDelete( p_mux );
        sout_AccessOutDelete( p_access );
        free( id );
        return NULL;
    }

    return id;
}

gbazin's avatar
   
gbazin committed
408
static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
409
{
Rafaël Carré's avatar
Rafaël Carré committed
410
    VLC_UNUSED(p_stream);
411
    sout_access_out_t *p_access = id->p_mux->p_access;
412
    sout_MuxDelete( id->p_mux );
413
414
415
416
417
418
419
    sout_MuxDeleteStream( id->p_mux, id->p_input );
    sout_AccessOutDelete( p_access );

    free( id );
    return VLC_SUCCESS;
}

gbazin's avatar
   
gbazin committed
420
static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
421
                 block_t *p_buffer )
422
{
Rafaël Carré's avatar
Rafaël Carré committed
423
    VLC_UNUSED(p_stream);
424
425
426
427
428
    sout_MuxSendBuffer( id->p_mux, id->p_input, p_buffer );

    return VLC_SUCCESS;
}