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 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_input.h>
36 #include <vlc_dialog.h>
38 /*****************************************************************************
40 *****************************************************************************/
41 #define ACCESS_TEXT N_("Output access method")
42 #define ACCESS_LONGTEXT N_( \
43 "This is the default output access method that will be used." )
45 #define ACCESSA_TEXT N_("Audio output access method")
46 #define ACCESSA_LONGTEXT N_( \
47 "This is the output access method that will be used for audio." )
48 #define ACCESSV_TEXT N_("Video output access method")
49 #define ACCESSV_LONGTEXT N_( \
50 "This is the output access method that will be used for video." )
52 #define MUX_TEXT N_("Output muxer")
53 #define MUX_LONGTEXT N_( \
54 "This is the default muxer method that will be used." )
55 #define MUXA_TEXT N_("Audio output muxer")
56 #define MUXA_LONGTEXT N_( \
57 "This is the muxer that will be used for audio." )
58 #define MUXV_TEXT N_("Video output muxer")
59 #define MUXV_LONGTEXT N_( \
60 "This is the muxer that will be used for video." )
62 #define DEST_TEXT N_("Output URL")
63 #define DEST_LONGTEXT N_( \
64 "This is the default output URI." )
65 #define DESTA_TEXT N_("Audio output URL")
66 #define DESTA_LONGTEXT N_( \
67 "This is the output URI that will be used for audio." )
68 #define DESTV_TEXT N_("Video output URL")
69 #define DESTV_LONGTEXT N_( \
70 "This is the output URI that will be used for video." )
72 static int Open ( vlc_object_t
* );
73 static void Close ( vlc_object_t
* );
75 #define SOUT_CFG_PREFIX "sout-es-"
79 set_description( N_("Elementary stream output") )
80 set_capability( "sout stream", 50 )
82 set_category( CAT_SOUT
)
83 set_subcategory( SUBCAT_SOUT_STREAM
)
85 set_section( N_("Generic"), NULL
)
86 add_string( SOUT_CFG_PREFIX
"access", "", NULL
, ACCESS_TEXT
,
87 ACCESS_LONGTEXT
, true )
88 add_string( SOUT_CFG_PREFIX
"mux", "", NULL
, MUX_TEXT
,
90 add_string( SOUT_CFG_PREFIX
"dst", "", NULL
, DEST_TEXT
,
93 set_section( N_("Audio"), NULL
)
94 add_string( SOUT_CFG_PREFIX
"access-audio", "", NULL
, ACCESSA_TEXT
,
95 ACCESSA_LONGTEXT
, true )
96 add_string( SOUT_CFG_PREFIX
"mux-audio", "", NULL
, MUXA_TEXT
,
98 add_string( SOUT_CFG_PREFIX
"dst-audio", "", NULL
, DESTA_TEXT
,
99 DESTA_LONGTEXT
, true )
101 set_section( N_("Video"), NULL
)
102 add_string( SOUT_CFG_PREFIX
"access-video", "", NULL
, ACCESSV_TEXT
,
103 ACCESSV_LONGTEXT
, true )
104 add_string( SOUT_CFG_PREFIX
"mux-video", "", NULL
, MUXV_TEXT
,
105 MUXV_LONGTEXT
, true )
106 add_string( SOUT_CFG_PREFIX
"dst-video", "", NULL
, DESTV_TEXT
,
107 DESTV_LONGTEXT
, true )
109 set_callbacks( Open
, Close
)
112 /*****************************************************************************
113 * Exported prototypes
114 *****************************************************************************/
115 static const char *const ppsz_sout_options
[] = {
116 "access", "access-audio", "access-video",
117 "mux", "mux-audio", "mux-video",
118 "dst", "dst-audio", "dst-video",
122 static sout_stream_id_t
*Add ( sout_stream_t
*, es_format_t
* );
123 static int Del ( sout_stream_t
*, sout_stream_id_t
* );
124 static int Send( sout_stream_t
*, sout_stream_id_t
*, block_t
* );
126 struct sout_stream_sys_t
137 char *psz_access_audio
;
138 char *psz_access_video
;
145 /*****************************************************************************
147 *****************************************************************************/
148 static int Open( vlc_object_t
*p_this
)
150 sout_stream_t
*p_stream
= (sout_stream_t
*)p_this
;
151 sout_stream_sys_t
*p_sys
;
153 config_ChainParse( p_stream
, SOUT_CFG_PREFIX
, ppsz_sout_options
, p_stream
->p_cfg
);
154 p_sys
= malloc( sizeof( sout_stream_sys_t
) );
157 p_sys
->i_count_audio
= 0;
158 p_sys
->i_count_video
= 0;
160 p_sys
->psz_access
= var_GetString( p_stream
, SOUT_CFG_PREFIX
"access" );
161 p_sys
->psz_access_audio
= var_GetString( p_stream
, SOUT_CFG_PREFIX
"access-audio" );
162 p_sys
->psz_access_video
= var_GetString( p_stream
, SOUT_CFG_PREFIX
"access-video" );
164 p_sys
->psz_mux
= var_GetString( p_stream
, SOUT_CFG_PREFIX
"mux" );
165 p_sys
->psz_mux_audio
= var_GetString( p_stream
, SOUT_CFG_PREFIX
"mux-audio" );
166 p_sys
->psz_mux_video
= var_GetString( p_stream
, SOUT_CFG_PREFIX
"mux-video" );
168 p_sys
->psz_dst
= var_GetString( p_stream
, SOUT_CFG_PREFIX
"dst" );
169 p_sys
->psz_dst_audio
= var_GetString( p_stream
, SOUT_CFG_PREFIX
"dst-audio" );
170 p_sys
->psz_dst_video
= var_GetString( p_stream
, SOUT_CFG_PREFIX
"dst-video" );
172 p_stream
->pf_add
= Add
;
173 p_stream
->pf_del
= Del
;
174 p_stream
->pf_send
= Send
;
176 p_stream
->p_sys
= p_sys
;
181 /*****************************************************************************
183 *****************************************************************************/
185 static void Close( vlc_object_t
* p_this
)
187 sout_stream_t
*p_stream
= (sout_stream_t
*)p_this
;
188 sout_stream_sys_t
*p_sys
= p_stream
->p_sys
;
190 free( p_sys
->psz_access
);
191 free( p_sys
->psz_access_audio
);
192 free( p_sys
->psz_access_video
);
194 free( p_sys
->psz_mux
);
195 free( p_sys
->psz_mux_audio
);
196 free( p_sys
->psz_mux_video
);
198 free( p_sys
->psz_dst
);
199 free( p_sys
->psz_dst_audio
);
200 free( p_sys
->psz_dst_video
);
205 struct sout_stream_id_t
207 sout_input_t
*p_input
;
211 static char * es_print_url( const char *psz_fmt
, vlc_fourcc_t i_fourcc
, int i_count
,
212 const char *psz_access
, const char *psz_mux
)
216 if( psz_fmt
== NULL
|| !*psz_fmt
)
218 psz_fmt
= (char*)"stream-%n-%c.%m";
221 p
= psz_dst
= malloc( 4096 );
224 memset( p
, 0, 4096 );
227 if( *psz_fmt
== '\0' )
233 if( *psz_fmt
!= '%' )
239 if( psz_fmt
[1] == 'n' )
241 p
+= sprintf( p
, "%d", i_count
);
243 else if( psz_fmt
[1] == 'c' )
245 p
+= sprintf( p
, "%4.4s", (char*)&i_fourcc
);
247 else if( psz_fmt
[1] == 'm' )
249 p
+= sprintf( p
, "%s", psz_mux
);
251 else if( psz_fmt
[1] == 'a' )
253 p
+= sprintf( p
, "%s", psz_access
);
255 else if( psz_fmt
[1] != '\0' )
257 p
+= sprintf( p
, "%c%c", psz_fmt
[0], psz_fmt
[1] );
261 p
+= sprintf( p
, "%c", psz_fmt
[0] );
272 static sout_stream_id_t
*Add( sout_stream_t
*p_stream
, es_format_t
*p_fmt
)
274 sout_stream_sys_t
*p_sys
= p_stream
->p_sys
;
275 sout_instance_t
*p_sout
= p_stream
->p_sout
;
276 sout_stream_id_t
*id
;
278 const char *psz_access
;
282 sout_access_out_t
*p_access
;
285 /* *** get access name *** */
286 if( p_fmt
->i_cat
== AUDIO_ES
&& p_sys
->psz_access_audio
&& *p_sys
->psz_access_audio
)
288 psz_access
= p_sys
->psz_access_audio
;
290 else if( p_fmt
->i_cat
== VIDEO_ES
&& p_sys
->psz_access_video
&& *p_sys
->psz_access_video
)
292 psz_access
= p_sys
->psz_access_video
;
296 psz_access
= p_sys
->psz_access
;
299 /* *** get mux name *** */
300 if( p_fmt
->i_cat
== AUDIO_ES
&& p_sys
->psz_mux_audio
&& *p_sys
->psz_mux_audio
)
302 psz_mux
= p_sys
->psz_mux_audio
;
304 else if( p_fmt
->i_cat
== VIDEO_ES
&& p_sys
->psz_mux_video
&& *p_sys
->psz_mux_video
)
306 psz_mux
= p_sys
->psz_mux_video
;
310 psz_mux
= p_sys
->psz_mux
;
313 /* Get url (%d expanded as a codec count, %c expanded as codec fcc ) */
314 if( p_fmt
->i_cat
== AUDIO_ES
&& p_sys
->psz_dst_audio
&& *p_sys
->psz_dst_audio
)
316 psz_dst
= es_print_url( p_sys
->psz_dst_audio
, p_fmt
->i_codec
,
317 p_sys
->i_count_audio
, psz_access
, psz_mux
);
319 else if( p_fmt
->i_cat
== VIDEO_ES
&& p_sys
->psz_dst_video
&& *p_sys
->psz_dst_video
)
321 psz_dst
= es_print_url( p_sys
->psz_dst_video
, p_fmt
->i_codec
,
322 p_sys
->i_count_video
, psz_access
, psz_mux
);
327 if( p_fmt
->i_cat
== VIDEO_ES
)
329 i_count
= p_sys
->i_count_video
;
331 else if( p_fmt
->i_cat
== AUDIO_ES
)
333 i_count
= p_sys
->i_count_audio
;
337 i_count
= p_sys
->i_count
;
340 psz_dst
= es_print_url( p_sys
->psz_dst
, p_fmt
->i_codec
,
341 i_count
, psz_access
, psz_mux
);
345 if( p_fmt
->i_cat
== VIDEO_ES
)
347 p_sys
->i_count_video
++;
349 else if( p_fmt
->i_cat
== AUDIO_ES
)
351 p_sys
->i_count_audio
++;
353 msg_Dbg( p_stream
, "creating `%s/%s://%s'",
354 psz_access
, psz_mux
, psz_dst
);
356 /* *** find and open appropriate access module *** */
357 p_access
= sout_AccessOutNew( p_sout
, psz_access
, psz_dst
);
358 if( p_access
== NULL
)
360 msg_Err( p_stream
, "no suitable sout access module for `%s/%s://%s'",
361 psz_access
, psz_mux
, psz_dst
);
362 dialog_Fatal( p_stream
,
363 _("Streaming / Transcoding failed"),
364 _("There is no suitable stream-output access module for \"%s/%s://%s\"."),
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 dialog_Fatal( p_stream
,
378 _("Streaming / Transcoding failed"),
379 _("There is no suitable stream-output access module "\
380 "for \"%s/%s://%s\"."),
381 psz_access
, psz_mux
, psz_dst
);
382 sout_AccessOutDelete( p_access
);
388 id
= malloc( sizeof( sout_stream_id_t
) );
391 sout_MuxDelete( p_mux
);
392 sout_AccessOutDelete( p_access
);
396 id
->p_input
= sout_MuxAddStream( p_mux
, p_fmt
);
398 if( id
->p_input
== NULL
)
400 sout_MuxDelete( p_mux
);
401 sout_AccessOutDelete( p_access
);
406 if( !sout_AccessOutCanControlPace( p_access
) )
407 p_sout
->i_out_pace_nocontrol
++;
412 static int Del( sout_stream_t
*p_stream
, sout_stream_id_t
*id
)
414 VLC_UNUSED(p_stream
);
415 sout_access_out_t
*p_access
= id
->p_mux
->p_access
;
417 sout_MuxDeleteStream( id
->p_mux
, id
->p_input
);
418 sout_MuxDelete( id
->p_mux
);
419 if( !sout_AccessOutCanControlPace( p_access
) )
420 p_stream
->p_sout
->i_out_pace_nocontrol
--;
421 sout_AccessOutDelete( p_access
);
427 static int Send( sout_stream_t
*p_stream
, sout_stream_id_t
*id
,
430 VLC_UNUSED(p_stream
);
431 sout_MuxSendBuffer( id
->p_mux
, id
->p_input
, p_buffer
);