1 /*****************************************************************************
2 * es.c: Elementary stream output module
3 *****************************************************************************
4 * Copyright (C) 2003-2004 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
31 #include <vlc_input.h>
33 #include <vlc_interface.h>
35 /*****************************************************************************
37 *****************************************************************************/
38 #define ACCESS_TEXT N_("Output access method")
39 #define ACCESS_LONGTEXT N_( \
40 "This is the default output access method that will be used." )
42 #define ACCESSA_TEXT N_("Audio output access method")
43 #define ACCESSA_LONGTEXT N_( \
44 "This is the output access method that will be used for audio." )
45 #define ACCESSV_TEXT N_("Video output access method")
46 #define ACCESSV_LONGTEXT N_( \
47 "This is the output access method that will be used for video." )
49 #define MUX_TEXT N_("Output muxer")
50 #define MUX_LONGTEXT N_( \
51 "This is the default muxer method that will be used." )
52 #define MUXA_TEXT N_("Audio output muxer")
53 #define MUXA_LONGTEXT N_( \
54 "This is the muxer that will be used for audio." )
55 #define MUXV_TEXT N_("Video output muxer")
56 #define MUXV_LONGTEXT N_( \
57 "This is the muxer that will be used for video." )
59 #define DEST_TEXT N_("Output URL")
60 #define DEST_LONGTEXT N_( \
61 "This is the default output URI." )
62 #define DESTA_TEXT N_("Audio output URL")
63 #define DESTA_LONGTEXT N_( \
64 "This is the output URI that will be used for audio." )
65 #define DESTV_TEXT N_("Video output URL")
66 #define DESTV_LONGTEXT N_( \
67 "This is the output URI that will be used for video." )
69 static int Open ( vlc_object_t * );
70 static void Close ( vlc_object_t * );
72 #define SOUT_CFG_PREFIX "sout-es-"
75 set_shortname( "ES" );
76 set_description( _("Elementary stream output") );
77 set_capability( "sout stream", 50 );
79 set_category( CAT_SOUT );
80 set_subcategory( SUBCAT_SOUT_STREAM );
82 add_string( SOUT_CFG_PREFIX "access", "", NULL, ACCESS_TEXT,
83 ACCESS_LONGTEXT, VLC_TRUE );
84 add_string( SOUT_CFG_PREFIX "access-audio", "", NULL, ACCESSA_TEXT,
85 ACCESSA_LONGTEXT, VLC_TRUE );
86 add_string( SOUT_CFG_PREFIX "access-video", "", NULL, ACCESSV_TEXT,
87 ACCESSV_LONGTEXT, VLC_TRUE );
89 add_string( SOUT_CFG_PREFIX "mux", "", NULL, MUX_TEXT,
90 MUX_LONGTEXT, VLC_TRUE );
91 add_string( SOUT_CFG_PREFIX "mux-audio", "", NULL, MUXA_TEXT,
92 MUXA_LONGTEXT, VLC_TRUE );
93 add_string( SOUT_CFG_PREFIX "mux-video", "", NULL, MUXV_TEXT,
94 MUXV_LONGTEXT, VLC_TRUE );
96 add_string( SOUT_CFG_PREFIX "dst", "", NULL, DEST_TEXT,
97 DEST_LONGTEXT, VLC_TRUE );
98 add_string( SOUT_CFG_PREFIX "dst-audio", "", NULL, DESTA_TEXT,
99 DESTA_LONGTEXT, VLC_TRUE );
100 add_string( SOUT_CFG_PREFIX "dst-video", "", NULL, DESTV_TEXT,
101 DESTV_LONGTEXT, VLC_TRUE );
103 set_callbacks( Open, Close );
106 /*****************************************************************************
107 * Exported prototypes
108 *****************************************************************************/
109 static const char *ppsz_sout_options[] = {
110 "access", "access-audio", "access-video",
111 "mux", "mux-audio", "mux-video",
112 "dst", "dst-audio", "dst-video",
116 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
117 static int Del ( sout_stream_t *, sout_stream_id_t * );
118 static int Send( sout_stream_t *, sout_stream_id_t *, block_t* );
120 struct sout_stream_sys_t
131 char *psz_access_audio;
132 char *psz_access_video;
139 /*****************************************************************************
141 *****************************************************************************/
142 static int Open( vlc_object_t *p_this )
144 sout_stream_t *p_stream = (sout_stream_t*)p_this;
145 sout_stream_sys_t *p_sys;
148 config_ChainParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options, p_stream->p_cfg );
149 p_sys = malloc( sizeof( sout_stream_sys_t ) );
152 p_sys->i_count_audio = 0;
153 p_sys->i_count_video = 0;
155 var_Get( p_stream, SOUT_CFG_PREFIX "access", &val );
156 p_sys->psz_access = val.psz_string;
157 var_Get( p_stream, SOUT_CFG_PREFIX "access-audio", &val );
158 p_sys->psz_access_audio = val.psz_string;
159 var_Get( p_stream, SOUT_CFG_PREFIX "access-video", &val );
160 p_sys->psz_access_video = val.psz_string;
162 var_Get( p_stream, SOUT_CFG_PREFIX "mux", &val );
163 p_sys->psz_mux = val.psz_string;
164 var_Get( p_stream, SOUT_CFG_PREFIX "mux-audio", &val );
165 p_sys->psz_mux_audio = val.psz_string;
166 var_Get( p_stream, SOUT_CFG_PREFIX "mux-video", &val );
167 p_sys->psz_mux_video = val.psz_string;
169 var_Get( p_stream, SOUT_CFG_PREFIX "dst", &val );
170 p_sys->psz_dst = val.psz_string;
171 var_Get( p_stream, SOUT_CFG_PREFIX "dst-audio", &val );
172 p_sys->psz_dst_audio = val.psz_string;
173 var_Get( p_stream, SOUT_CFG_PREFIX "dst-video", &val );
174 p_sys->psz_dst_video = val.psz_string;
176 p_stream->pf_add = Add;
177 p_stream->pf_del = Del;
178 p_stream->pf_send = Send;
180 p_stream->p_sys = p_sys;
185 /*****************************************************************************
187 *****************************************************************************/
189 static void Close( vlc_object_t * p_this )
191 sout_stream_t *p_stream = (sout_stream_t*)p_this;
192 sout_stream_sys_t *p_sys = p_stream->p_sys;
194 free( p_sys->psz_access );
195 free( p_sys->psz_access_audio );
196 free( p_sys->psz_access_video );
198 free( p_sys->psz_mux );
199 free( p_sys->psz_mux_audio );
200 free( p_sys->psz_mux_video );
202 free( p_sys->psz_dst );
203 free( p_sys->psz_dst_audio );
204 free( p_sys->psz_dst_video );
209 struct sout_stream_id_t
211 sout_input_t *p_input;
215 static char * es_print_url( char *psz_fmt, vlc_fourcc_t i_fourcc, int i_count,
216 char *psz_access, char *psz_mux )
220 if( psz_fmt == NULL || !*psz_fmt )
222 psz_fmt = "stream-%n-%c.%m";
225 p = psz_dst = malloc( 4096 );
226 memset( p, 0, 4096 );
229 if( *psz_fmt == '\0' )
235 if( *psz_fmt != '%' )
241 if( psz_fmt[1] == 'n' )
243 p += sprintf( p, "%d", i_count );
245 else if( psz_fmt[1] == 'c' )
247 p += sprintf( p, "%4.4s", (char*)&i_fourcc );
249 else if( psz_fmt[1] == 'm' )
251 p += sprintf( p, "%s", psz_mux );
253 else if( psz_fmt[1] == 'a' )
255 p += sprintf( p, "%s", psz_access );
257 else if( psz_fmt[1] != '\0' )
259 p += sprintf( p, "%c%c", psz_fmt[0], psz_fmt[1] );
263 p += sprintf( p, "%c", psz_fmt[0] );
274 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
276 sout_stream_sys_t *p_sys = p_stream->p_sys;
277 sout_instance_t *p_sout = p_stream->p_sout;
278 sout_stream_id_t *id;
284 sout_access_out_t *p_access;
287 /* *** get access name *** */
288 if( p_fmt->i_cat == AUDIO_ES && p_sys->psz_access_audio && *p_sys->psz_access_audio )
290 psz_access = p_sys->psz_access_audio;
292 else if( p_fmt->i_cat == VIDEO_ES && p_sys->psz_access_video && *p_sys->psz_access_video )
294 psz_access = p_sys->psz_access_video;
298 psz_access = p_sys->psz_access;
301 /* *** get mux name *** */
302 if( p_fmt->i_cat == AUDIO_ES && p_sys->psz_mux_audio && *p_sys->psz_mux_audio )
304 psz_mux = p_sys->psz_mux_audio;
306 else if( p_fmt->i_cat == VIDEO_ES && p_sys->psz_mux_video && *p_sys->psz_mux_video )
308 psz_mux = p_sys->psz_mux_video;
312 psz_mux = p_sys->psz_mux;
315 /* Get url (%d expanded as a codec count, %c expanded as codec fcc ) */
316 if( p_fmt->i_cat == AUDIO_ES && p_sys->psz_dst_audio && *p_sys->psz_dst_audio )
318 psz_dst = es_print_url( p_sys->psz_dst_audio, p_fmt->i_codec,
319 p_sys->i_count_audio, psz_access, psz_mux );
321 else if( p_fmt->i_cat == VIDEO_ES && p_sys->psz_dst_video && *p_sys->psz_dst_video )
323 psz_dst = es_print_url( p_sys->psz_dst_video, p_fmt->i_codec,
324 p_sys->i_count_video, psz_access, psz_mux );
329 if( p_fmt->i_cat == VIDEO_ES )
331 i_count = p_sys->i_count_video;
333 else if( p_fmt->i_cat == AUDIO_ES )
335 i_count = p_sys->i_count_audio;
339 i_count = p_sys->i_count;
342 psz_dst = es_print_url( p_sys->psz_dst, p_fmt->i_codec,
343 i_count, psz_access, psz_mux );
347 if( p_fmt->i_cat == VIDEO_ES )
349 p_sys->i_count_video++;
351 else if( p_fmt->i_cat == AUDIO_ES )
353 p_sys->i_count_audio++;
355 msg_Dbg( p_stream, "creating `%s/%s://%s'",
356 psz_access, psz_mux, psz_dst );
358 /* *** find and open appropriate access module *** */
359 p_access = sout_AccessOutNew( p_sout, psz_access, psz_dst );
360 if( p_access == NULL )
362 msg_Err( p_stream, "no suitable sout access module for `%s/%s://%s'",
363 psz_access, psz_mux, psz_dst );
364 intf_UserFatal( p_stream, VLC_FALSE,
365 _("Streaming / Transcoding failed"),
366 _("There is no suitable stream-output access module for \"%s/%s://%s\"."),
372 /* *** find and open appropriate mux module *** */
373 p_mux = sout_MuxNew( p_sout, psz_mux, p_access );
376 msg_Err( p_stream, "no suitable sout mux module for `%s/%s://%s'",
377 psz_access, psz_mux, psz_dst );
378 intf_UserFatal( p_stream, VLC_FALSE,
379 _("Streaming / Transcoding failed"),
380 _("There is no suitable stream-output access module "\
381 "for \"%s/%s://%s\"."),
382 psz_access, psz_mux, psz_dst );
383 sout_AccessOutDelete( p_access );
387 id = malloc( sizeof( sout_stream_id_t ) );
389 id->p_input = sout_MuxAddStream( p_mux, p_fmt );
391 if( id->p_input == NULL )
395 sout_MuxDelete( p_mux );
396 sout_AccessOutDelete( p_access );
404 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
406 sout_access_out_t *p_access = id->p_mux->p_access;
407 sout_MuxDelete( id->p_mux );
408 sout_MuxDeleteStream( id->p_mux, id->p_input );
409 sout_AccessOutDelete( p_access );
415 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
418 sout_MuxSendBuffer( id->p_mux, id->p_input, p_buffer );