1 /*****************************************************************************
2 * es.c: Elementary stream output module
3 *****************************************************************************
4 * Copyright (C) 2003-2004 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
31 #include <vlc/input.h>
34 /*****************************************************************************
36 *****************************************************************************/
37 #define ACCESS_TEXT N_("Output access method")
38 #define ACCESS_LONGTEXT N_( \
39 "Allows you to specify the output access method used for the streaming " \
41 #define ACCESSA_TEXT N_("Audio output access method")
42 #define ACCESSA_LONGTEXT N_( \
43 "Allows you to specify the output access method used for the audio " \
45 #define ACCESSV_TEXT N_("Video output access method")
46 #define ACCESSV_LONGTEXT N_( \
47 "Allows you to specify the output access method used for the video " \
50 #define MUX_TEXT N_("Output muxer")
51 #define MUX_LONGTEXT N_( \
52 "Allows you to specify the muxer used for the streaming output." )
53 #define MUXA_TEXT N_("Audio output muxer")
54 #define MUXA_LONGTEXT N_( \
55 "Allows you to specify the muxer used for the audio streaming output." )
56 #define MUXV_TEXT N_("Video output muxer")
57 #define MUXV_LONGTEXT N_( \
58 "Allows you to specify the muxer used for the video streaming output." )
60 #define DEST_TEXT N_("Output URL")
61 #define DEST_LONGTEXT N_( \
62 "Allows you to specify the output URL used for the streaming output." )
63 #define DESTA_TEXT N_("Audio output URL")
64 #define DESTA_LONGTEXT N_( \
65 "Allows you to specify the output URL used for the audio streaming " \
67 #define DESTV_TEXT N_("Video output URL")
68 #define DESTV_LONGTEXT N_( \
69 "Allows you to specify the output URL used for the video streaming " \
72 static int Open ( vlc_object_t * );
73 static void Close ( vlc_object_t * );
75 #define SOUT_CFG_PREFIX "sout-es-"
78 set_description( _("Elementary stream output") );
79 set_capability( "sout stream", 50 );
81 set_category( CAT_SOUT );
82 set_subcategory( SUBCAT_SOUT_STREAM );
84 add_string( SOUT_CFG_PREFIX "access", "", NULL, ACCESS_TEXT,
85 ACCESS_LONGTEXT, VLC_TRUE );
86 add_string( SOUT_CFG_PREFIX "access-audio", "", NULL, ACCESSA_TEXT,
87 ACCESSA_LONGTEXT, VLC_TRUE );
88 add_string( SOUT_CFG_PREFIX "access-video", "", NULL, ACCESSV_TEXT,
89 ACCESSV_LONGTEXT, VLC_TRUE );
91 add_string( SOUT_CFG_PREFIX "mux", "", NULL, MUX_TEXT,
92 MUX_LONGTEXT, VLC_TRUE );
93 add_string( SOUT_CFG_PREFIX "mux-audio", "", NULL, MUXA_TEXT,
94 MUXA_LONGTEXT, VLC_TRUE );
95 add_string( SOUT_CFG_PREFIX "mux-video", "", NULL, MUXV_TEXT,
96 MUXV_LONGTEXT, VLC_TRUE );
98 add_string( SOUT_CFG_PREFIX "dst", "", NULL, DEST_TEXT,
99 DEST_LONGTEXT, VLC_TRUE );
100 add_string( SOUT_CFG_PREFIX "dst-audio", "", NULL, DESTA_TEXT,
101 DESTA_LONGTEXT, VLC_TRUE );
102 add_string( SOUT_CFG_PREFIX "dst-video", "", NULL, DESTV_TEXT,
103 DESTV_LONGTEXT, VLC_TRUE );
105 set_callbacks( Open, Close );
109 #define FREE( p ) if( p ) { free( p ); (p) = NULL; }
110 /*****************************************************************************
111 * Exported prototypes
112 *****************************************************************************/
113 static const char *ppsz_sout_options[] = {
114 "access", "access-audio", "access-video",
115 "mux", "mux-audio", "mux-video",
116 "dst", "dst-audio", "dst-video",
120 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
121 static int Del ( sout_stream_t *, sout_stream_id_t * );
122 static int Send( sout_stream_t *, sout_stream_id_t *, block_t* );
124 struct sout_stream_sys_t
135 char *psz_access_audio;
136 char *psz_access_video;
143 /*****************************************************************************
145 *****************************************************************************/
146 static int Open( vlc_object_t *p_this )
148 sout_stream_t *p_stream = (sout_stream_t*)p_this;
149 sout_stream_sys_t *p_sys;
152 sout_CfgParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options, p_stream->p_cfg );
153 p_sys = malloc( sizeof( sout_stream_sys_t ) );
156 p_sys->i_count_audio = 0;
157 p_sys->i_count_video = 0;
159 var_Get( p_stream, SOUT_CFG_PREFIX "access", &val );
160 p_sys->psz_access = val.psz_string;
161 var_Get( p_stream, SOUT_CFG_PREFIX "access-audio", &val );
162 p_sys->psz_access_audio = val.psz_string;
163 var_Get( p_stream, SOUT_CFG_PREFIX "access-video", &val );
164 p_sys->psz_access_video = val.psz_string;
166 var_Get( p_stream, SOUT_CFG_PREFIX "mux", &val );
167 p_sys->psz_mux = val.psz_string;
168 var_Get( p_stream, SOUT_CFG_PREFIX "mux-audio", &val );
169 p_sys->psz_mux_audio = val.psz_string;
170 var_Get( p_stream, SOUT_CFG_PREFIX "mux-video", &val );
171 p_sys->psz_mux_video = val.psz_string;
173 var_Get( p_stream, SOUT_CFG_PREFIX "dst", &val );
174 p_sys->psz_dst = val.psz_string;
175 var_Get( p_stream, SOUT_CFG_PREFIX "dst-audio", &val );
176 p_sys->psz_dst_audio = val.psz_string;
177 var_Get( p_stream, SOUT_CFG_PREFIX "dst-video", &val );
178 p_sys->psz_dst_video = val.psz_string;
180 p_stream->pf_add = Add;
181 p_stream->pf_del = Del;
182 p_stream->pf_send = Send;
184 p_stream->p_sys = p_sys;
189 /*****************************************************************************
191 *****************************************************************************/
193 static void Close( vlc_object_t * p_this )
195 sout_stream_t *p_stream = (sout_stream_t*)p_this;
196 sout_stream_sys_t *p_sys = p_stream->p_sys;
198 free( p_sys->psz_access );
199 free( p_sys->psz_access_audio );
200 free( p_sys->psz_access_video );
202 free( p_sys->psz_mux );
203 free( p_sys->psz_mux_audio );
204 free( p_sys->psz_mux_video );
206 free( p_sys->psz_dst );
207 free( p_sys->psz_dst_audio );
208 free( p_sys->psz_dst_video );
213 struct sout_stream_id_t
215 sout_input_t *p_input;
219 static char * es_print_url( char *psz_fmt, vlc_fourcc_t i_fourcc, int i_count,
220 char *psz_access, char *psz_mux )
224 if( psz_fmt == NULL || !*psz_fmt )
226 psz_fmt = "stream-%n-%c.%m";
229 p = psz_dst = malloc( 4096 );
230 memset( p, 0, 4096 );
233 if( *psz_fmt == '\0' )
239 if( *psz_fmt != '%' )
245 if( psz_fmt[1] == 'n' )
247 p += sprintf( p, "%d", i_count );
249 else if( psz_fmt[1] == 'c' )
251 p += sprintf( p, "%4.4s", (char*)&i_fourcc );
253 else if( psz_fmt[1] == 'm' )
255 p += sprintf( p, "%s", psz_mux );
257 else if( psz_fmt[1] == 'a' )
259 p += sprintf( p, "%s", psz_access );
261 else if( psz_fmt[1] != '\0' )
263 p += sprintf( p, "%c%c", psz_fmt[0], psz_fmt[1] );
267 p += sprintf( p, "%c", psz_fmt[0] );
278 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
280 sout_stream_sys_t *p_sys = p_stream->p_sys;
281 sout_instance_t *p_sout = p_stream->p_sout;
282 sout_stream_id_t *id;
288 sout_access_out_t *p_access;
291 /* *** get access name *** */
292 if( p_fmt->i_cat == AUDIO_ES && p_sys->psz_access_audio && *p_sys->psz_access_audio )
294 psz_access = p_sys->psz_access_audio;
296 else if( p_fmt->i_cat == VIDEO_ES && p_sys->psz_access_video && *p_sys->psz_access_video )
298 psz_access = p_sys->psz_access_video;
302 psz_access = p_sys->psz_access;
305 /* *** get mux name *** */
306 if( p_fmt->i_cat == AUDIO_ES && p_sys->psz_mux_audio && *p_sys->psz_mux_audio )
308 psz_mux = p_sys->psz_mux_audio;
310 else if( p_fmt->i_cat == VIDEO_ES && p_sys->psz_mux_video && *p_sys->psz_mux_video )
312 psz_mux = p_sys->psz_mux_video;
316 psz_mux = p_sys->psz_mux;
319 /* Get url (%d expanded as a codec count, %c expanded as codec fcc ) */
320 if( p_fmt->i_cat == AUDIO_ES && p_sys->psz_dst_audio && *p_sys->psz_dst_audio )
322 psz_dst = es_print_url( p_sys->psz_dst_audio, p_fmt->i_codec,
323 p_sys->i_count_audio, psz_access, psz_mux );
325 else if( p_fmt->i_cat == VIDEO_ES && p_sys->psz_dst_video && *p_sys->psz_dst_video )
327 psz_dst = es_print_url( p_sys->psz_dst_video, p_fmt->i_codec,
328 p_sys->i_count_video, psz_access, psz_mux );
333 if( p_fmt->i_cat == VIDEO_ES )
335 i_count = p_sys->i_count_video;
337 else if( p_fmt->i_cat == AUDIO_ES )
339 i_count = p_sys->i_count_audio;
343 i_count = p_sys->i_count;
346 psz_dst = es_print_url( p_sys->psz_dst, p_fmt->i_codec,
347 i_count, psz_access, psz_mux );
351 if( p_fmt->i_cat == VIDEO_ES )
353 p_sys->i_count_video++;
355 else if( p_fmt->i_cat == AUDIO_ES )
357 p_sys->i_count_audio++;
359 msg_Dbg( p_stream, "creating `%s/%s://%s'",
360 psz_access, psz_mux, psz_dst );
362 /* *** find and open appropriate access module *** */
363 p_access = sout_AccessOutNew( p_sout, psz_access, psz_dst );
364 if( p_access == NULL )
366 msg_Err( p_stream, "no suitable sout access module for `%s/%s://%s'",
367 psz_access, psz_mux, psz_dst );
371 /* *** find and open appropriate mux module *** */
372 p_mux = sout_MuxNew( p_sout, psz_mux, p_access );
375 msg_Err( p_stream, "no suitable sout mux module for `%s/%s://%s'",
376 psz_access, psz_mux, psz_dst );
377 sout_AccessOutDelete( p_access );
381 id = malloc( sizeof( sout_stream_id_t ) );
383 id->p_input = sout_MuxAddStream( p_mux, p_fmt );
385 if( id->p_input == NULL )
389 sout_MuxDelete( p_mux );
390 sout_AccessOutDelete( p_access );
398 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
400 sout_access_out_t *p_access = id->p_mux->p_access;
402 sout_MuxDeleteStream( id->p_mux, id->p_input );
403 sout_AccessOutDelete( p_access );
409 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
412 sout_MuxSendBuffer( id->p_mux, id->p_input, p_buffer );