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 #define FREE( p ) if( p ) { free( p ); (p) = NULL; }
35 /*****************************************************************************
37 *****************************************************************************/
38 static int Open ( vlc_object_t * );
39 static void Close ( vlc_object_t * );
41 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
42 static int Del ( sout_stream_t *, sout_stream_id_t * );
43 static int Send( sout_stream_t *, sout_stream_id_t *, block_t* );
45 /*****************************************************************************
47 *****************************************************************************/
49 set_description( _("Elementary stream output") );
50 set_capability( "sout stream", 50 );
52 set_callbacks( Open, Close );
55 struct sout_stream_sys_t
66 char *psz_access_audio;
67 char *psz_access_video;
74 /*****************************************************************************
76 *****************************************************************************/
77 static int Open( vlc_object_t *p_this )
79 sout_stream_t *p_stream = (sout_stream_t*)p_this;
80 sout_stream_sys_t *p_sys;
82 /* p_sout->i_preheader = __MAX( p_sout->i_preheader, p_mux->i_preheader ); */
84 p_sys = malloc( sizeof( sout_stream_sys_t ) );
87 p_sys->i_count_audio = 0;
88 p_sys->i_count_video = 0;
90 p_sys->psz_access = sout_cfg_find_value( p_stream->p_cfg, "access" );
91 p_sys->psz_access_audio = sout_cfg_find_value( p_stream->p_cfg, "access_audio" );
92 p_sys->psz_access_video = sout_cfg_find_value( p_stream->p_cfg, "access_video" );
94 p_sys->psz_mux = sout_cfg_find_value( p_stream->p_cfg, "mux" );
95 p_sys->psz_mux_audio = sout_cfg_find_value( p_stream->p_cfg, "mux_audio" );
96 p_sys->psz_mux_video = sout_cfg_find_value( p_stream->p_cfg, "mux_video" );
98 p_sys->psz_url = sout_cfg_find_value( p_stream->p_cfg, "url" );
99 p_sys->psz_url_audio = sout_cfg_find_value( p_stream->p_cfg, "url_audio" );
100 p_sys->psz_url_video = sout_cfg_find_value( p_stream->p_cfg, "url_video" );
102 p_stream->pf_add = Add;
103 p_stream->pf_del = Del;
104 p_stream->pf_send = Send;
106 p_stream->p_sys = p_sys;
111 /*****************************************************************************
113 *****************************************************************************/
115 static void Close( vlc_object_t * p_this )
117 sout_stream_t *p_stream = (sout_stream_t*)p_this;
118 sout_stream_sys_t *p_sys = p_stream->p_sys;
123 struct sout_stream_id_t
125 sout_input_t *p_input;
129 static char * es_print_url( char *psz_fmt, vlc_fourcc_t i_fourcc, int i_count,
130 char *psz_access, char *psz_mux )
134 if( psz_fmt == NULL || !*psz_fmt )
136 psz_fmt = "stream-%n-%c.%m";
139 p = psz_url = malloc( 4096 );
140 memset( p, 0, 4096 );
143 if( *psz_fmt == '\0' )
149 if( *psz_fmt != '%' )
155 if( psz_fmt[1] == 'n' )
157 p += sprintf( p, "%d", i_count );
159 else if( psz_fmt[1] == 'c' )
161 p += sprintf( p, "%4.4s", (char*)&i_fourcc );
163 else if( psz_fmt[1] == 'm' )
165 p += sprintf( p, "%s", psz_mux );
167 else if( psz_fmt[1] == 'a' )
169 p += sprintf( p, "%s", psz_access );
171 else if( psz_fmt[1] != '\0' )
173 p += sprintf( p, "%c%c", psz_fmt[0], psz_fmt[1] );
177 p += sprintf( p, "%c", psz_fmt[0] );
188 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
190 sout_stream_sys_t *p_sys = p_stream->p_sys;
191 sout_instance_t *p_sout = p_stream->p_sout;
192 sout_stream_id_t *id;
198 sout_access_out_t *p_access;
201 /* *** get access name *** */
202 if( p_fmt->i_cat == AUDIO_ES && p_sys->psz_access_audio )
204 psz_access = p_sys->psz_access_audio;
206 else if( p_fmt->i_cat == VIDEO_ES && p_sys->psz_access_video )
208 psz_access = p_sys->psz_access_video;
212 psz_access = p_sys->psz_access;
215 /* *** get mux name *** */
216 if( p_fmt->i_cat == AUDIO_ES && p_sys->psz_mux_audio )
218 psz_mux = p_sys->psz_mux_audio;
220 else if( p_fmt->i_cat == VIDEO_ES && p_sys->psz_mux_video )
222 psz_mux = p_sys->psz_mux_video;
226 psz_mux = p_sys->psz_mux;
229 /* Get url (%d expanded as a codec count, %c expanded as codec fcc ) */
230 if( p_fmt->i_cat == AUDIO_ES && p_sys->psz_url_audio )
232 psz_url = es_print_url( p_sys->psz_url_audio, p_fmt->i_codec,
233 p_sys->i_count_audio, psz_access, psz_mux );
235 else if( p_fmt->i_cat == VIDEO_ES && p_sys->psz_url_video )
237 psz_url = es_print_url( p_sys->psz_url_video, p_fmt->i_codec,
238 p_sys->i_count_video, psz_access, psz_mux );
243 if( p_fmt->i_cat == VIDEO_ES )
245 i_count = p_sys->i_count_video;
247 else if( p_fmt->i_cat == AUDIO_ES )
249 i_count = p_sys->i_count_audio;
253 i_count = p_sys->i_count;
256 psz_url = es_print_url( p_sys->psz_url, p_fmt->i_codec,
257 i_count, psz_access, psz_mux );
261 if( p_fmt->i_cat == VIDEO_ES )
263 p_sys->i_count_video++;
265 else if( p_fmt->i_cat == AUDIO_ES )
267 p_sys->i_count_audio++;
269 msg_Dbg( p_stream, "creating `%s/%s://%s'",
270 psz_access, psz_mux, psz_url );
272 /* *** find and open appropriate access module *** */
273 p_access = sout_AccessOutNew( p_sout, psz_access, psz_url );
274 if( p_access == NULL )
276 msg_Err( p_stream, "no suitable sout access module for `%s/%s://%s'",
277 psz_access, psz_mux, psz_url );
281 /* *** find and open appropriate mux module *** */
282 p_mux = sout_MuxNew( p_sout, psz_mux, p_access );
285 msg_Err( p_stream, "no suitable sout mux module for `%s/%s://%s'",
286 psz_access, psz_mux, psz_url );
287 sout_AccessOutDelete( p_access );
291 id = malloc( sizeof( sout_stream_id_t ) );
293 id->p_input = sout_MuxAddStream( p_mux, p_fmt );
295 if( id->p_input == NULL )
299 sout_MuxDelete( p_mux );
300 sout_AccessOutDelete( p_access );
308 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
310 sout_access_out_t *p_access = id->p_mux->p_access;
312 sout_MuxDeleteStream( id->p_mux, id->p_input );
313 sout_AccessOutDelete( p_access );
319 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
322 sout_MuxSendBuffer( id->p_mux, id->p_input, p_buffer );