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 static int Open ( vlc_object_t * );
38 static void Close ( vlc_object_t * );
40 #define SOUT_CFG_PREFIX "sout-es-"
43 set_description( _("Elementary stream output") );
44 set_capability( "sout stream", 50 );
47 add_string( SOUT_CFG_PREFIX "access", "", NULL, "access", "", VLC_TRUE );
48 add_string( SOUT_CFG_PREFIX "access-audio", "", NULL, "access audio", "", VLC_TRUE );
49 add_string( SOUT_CFG_PREFIX "access-video", "", NULL, "access video", "", VLC_TRUE );
51 add_string( SOUT_CFG_PREFIX "mux", "", NULL, "mux", "", VLC_TRUE );
52 add_string( SOUT_CFG_PREFIX "mux-audio", "", NULL, "mux audio", "", VLC_TRUE );
53 add_string( SOUT_CFG_PREFIX "mux-video", "", NULL, "mux video", "", VLC_TRUE );
55 add_string( SOUT_CFG_PREFIX "dst", "", NULL, "dst", "", VLC_TRUE );
56 add_string( SOUT_CFG_PREFIX "dst-audio", "", NULL, "dst audio", "", VLC_TRUE );
57 add_string( SOUT_CFG_PREFIX "dst-video", "", NULL, "dst video", "", VLC_TRUE );
59 set_callbacks( Open, Close );
63 #define FREE( p ) if( p ) { free( p ); (p) = NULL; }
64 /*****************************************************************************
66 *****************************************************************************/
67 static const char *ppsz_sout_options[] = {
68 "access", "access-audio", "access-video",
69 "mux", "mux-audio", "mux-video",
70 "dst", "dst-audio", "dst-video",
74 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
75 static int Del ( sout_stream_t *, sout_stream_id_t * );
76 static int Send( sout_stream_t *, sout_stream_id_t *, block_t* );
78 struct sout_stream_sys_t
89 char *psz_access_audio;
90 char *psz_access_video;
97 /*****************************************************************************
99 *****************************************************************************/
100 static int Open( vlc_object_t *p_this )
102 sout_stream_t *p_stream = (sout_stream_t*)p_this;
103 sout_stream_sys_t *p_sys;
106 sout_ParseCfg( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options, p_stream->p_cfg );
107 p_sys = malloc( sizeof( sout_stream_sys_t ) );
110 p_sys->i_count_audio = 0;
111 p_sys->i_count_video = 0;
113 var_Get( p_stream, SOUT_CFG_PREFIX "access", &val );
114 p_sys->psz_access = val.psz_string;
115 var_Get( p_stream, SOUT_CFG_PREFIX "access-audio", &val );
116 p_sys->psz_access_audio = val.psz_string;
117 var_Get( p_stream, SOUT_CFG_PREFIX "access-video", &val );
118 p_sys->psz_access_video = val.psz_string;
120 var_Get( p_stream, SOUT_CFG_PREFIX "mux", &val );
121 p_sys->psz_mux = val.psz_string;
122 var_Get( p_stream, SOUT_CFG_PREFIX "mux-audio", &val );
123 p_sys->psz_mux_audio = val.psz_string;
124 var_Get( p_stream, SOUT_CFG_PREFIX "mux-video", &val );
125 p_sys->psz_mux_video = val.psz_string;
127 var_Get( p_stream, SOUT_CFG_PREFIX "dst", &val );
128 p_sys->psz_dst = val.psz_string;
129 var_Get( p_stream, SOUT_CFG_PREFIX "dst-audio", &val );
130 p_sys->psz_dst_audio = val.psz_string;
131 var_Get( p_stream, SOUT_CFG_PREFIX "dst-video", &val );
132 p_sys->psz_dst_video = val.psz_string;
134 p_stream->pf_add = Add;
135 p_stream->pf_del = Del;
136 p_stream->pf_send = Send;
138 p_stream->p_sys = p_sys;
143 /*****************************************************************************
145 *****************************************************************************/
147 static void Close( vlc_object_t * p_this )
149 sout_stream_t *p_stream = (sout_stream_t*)p_this;
150 sout_stream_sys_t *p_sys = p_stream->p_sys;
152 free( p_sys->psz_access );
153 free( p_sys->psz_access_audio );
154 free( p_sys->psz_access_video );
156 free( p_sys->psz_mux );
157 free( p_sys->psz_mux_audio );
158 free( p_sys->psz_mux_video );
160 free( p_sys->psz_dst );
161 free( p_sys->psz_dst_audio );
162 free( p_sys->psz_dst_video );
167 struct sout_stream_id_t
169 sout_input_t *p_input;
173 static char * es_print_url( char *psz_fmt, vlc_fourcc_t i_fourcc, int i_count,
174 char *psz_access, char *psz_mux )
178 if( psz_fmt == NULL || !*psz_fmt )
180 psz_fmt = "stream-%n-%c.%m";
183 p = psz_dst = malloc( 4096 );
184 memset( p, 0, 4096 );
187 if( *psz_fmt == '\0' )
193 if( *psz_fmt != '%' )
199 if( psz_fmt[1] == 'n' )
201 p += sprintf( p, "%d", i_count );
203 else if( psz_fmt[1] == 'c' )
205 p += sprintf( p, "%4.4s", (char*)&i_fourcc );
207 else if( psz_fmt[1] == 'm' )
209 p += sprintf( p, "%s", psz_mux );
211 else if( psz_fmt[1] == 'a' )
213 p += sprintf( p, "%s", psz_access );
215 else if( psz_fmt[1] != '\0' )
217 p += sprintf( p, "%c%c", psz_fmt[0], psz_fmt[1] );
221 p += sprintf( p, "%c", psz_fmt[0] );
232 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
234 sout_stream_sys_t *p_sys = p_stream->p_sys;
235 sout_instance_t *p_sout = p_stream->p_sout;
236 sout_stream_id_t *id;
242 sout_access_out_t *p_access;
245 /* *** get access name *** */
246 if( p_fmt->i_cat == AUDIO_ES && p_sys->psz_access_audio && *p_sys->psz_access_audio )
248 psz_access = p_sys->psz_access_audio;
250 else if( p_fmt->i_cat == VIDEO_ES && p_sys->psz_access_video && *p_sys->psz_access_video )
252 psz_access = p_sys->psz_access_video;
256 psz_access = p_sys->psz_access;
259 /* *** get mux name *** */
260 if( p_fmt->i_cat == AUDIO_ES && p_sys->psz_mux_audio && *p_sys->psz_mux_audio )
262 psz_mux = p_sys->psz_mux_audio;
264 else if( p_fmt->i_cat == VIDEO_ES && p_sys->psz_mux_video && *p_sys->psz_mux_video )
266 psz_mux = p_sys->psz_mux_video;
270 psz_mux = p_sys->psz_mux;
273 /* Get url (%d expanded as a codec count, %c expanded as codec fcc ) */
274 if( p_fmt->i_cat == AUDIO_ES && p_sys->psz_dst_audio && *p_sys->psz_dst_audio )
276 psz_dst = es_print_url( p_sys->psz_dst_audio, p_fmt->i_codec,
277 p_sys->i_count_audio, psz_access, psz_mux );
279 else if( p_fmt->i_cat == VIDEO_ES && p_sys->psz_dst_video && *p_sys->psz_dst_video )
281 psz_dst = es_print_url( p_sys->psz_dst_video, p_fmt->i_codec,
282 p_sys->i_count_video, psz_access, psz_mux );
287 if( p_fmt->i_cat == VIDEO_ES )
289 i_count = p_sys->i_count_video;
291 else if( p_fmt->i_cat == AUDIO_ES )
293 i_count = p_sys->i_count_audio;
297 i_count = p_sys->i_count;
300 psz_dst = es_print_url( p_sys->psz_dst, p_fmt->i_codec,
301 i_count, psz_access, psz_mux );
305 if( p_fmt->i_cat == VIDEO_ES )
307 p_sys->i_count_video++;
309 else if( p_fmt->i_cat == AUDIO_ES )
311 p_sys->i_count_audio++;
313 msg_Dbg( p_stream, "creating `%s/%s://%s'",
314 psz_access, psz_mux, psz_dst );
316 /* *** find and open appropriate access module *** */
317 p_access = sout_AccessOutNew( p_sout, psz_access, psz_dst );
318 if( p_access == NULL )
320 msg_Err( p_stream, "no suitable sout access module for `%s/%s://%s'",
321 psz_access, psz_mux, psz_dst );
325 /* *** find and open appropriate mux module *** */
326 p_mux = sout_MuxNew( p_sout, psz_mux, p_access );
329 msg_Err( p_stream, "no suitable sout mux module for `%s/%s://%s'",
330 psz_access, psz_mux, psz_dst );
331 sout_AccessOutDelete( p_access );
335 id = malloc( sizeof( sout_stream_id_t ) );
337 id->p_input = sout_MuxAddStream( p_mux, p_fmt );
339 if( id->p_input == NULL )
343 sout_MuxDelete( p_mux );
344 sout_AccessOutDelete( p_access );
352 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
354 sout_access_out_t *p_access = id->p_mux->p_access;
356 sout_MuxDeleteStream( id->p_mux, id->p_input );
357 sout_AccessOutDelete( p_access );
363 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
366 sout_MuxSendBuffer( id->p_mux, id->p_input, p_buffer );