1 /*****************************************************************************
2 * duplicate.c: duplicate stream output module
3 *****************************************************************************
4 * Copyright (C) 2003-2004 the VideoLAN team
7 * Author: 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 *****************************************************************************/
34 #include <vlc_block.h>
36 /*****************************************************************************
38 *****************************************************************************/
39 static int Open ( vlc_object_t * );
40 static void Close ( vlc_object_t * );
43 set_description( _("Duplicate stream output") );
44 set_capability( "sout stream", 50 );
45 add_shortcut( "duplicate" );
46 add_shortcut( "dup" );
47 set_category( CAT_SOUT );
48 set_subcategory( SUBCAT_SOUT_STREAM );
49 set_callbacks( Open, Close );
53 /*****************************************************************************
55 *****************************************************************************/
56 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
57 static int Del ( sout_stream_t *, sout_stream_id_t * );
58 static int Send( sout_stream_t *, sout_stream_id_t *,
61 struct sout_stream_sys_t
64 sout_stream_t **pp_streams;
70 struct sout_stream_id_t
76 static vlc_bool_t ESSelected( es_format_t *fmt, char *psz_select );
78 /*****************************************************************************
80 *****************************************************************************/
81 static int Open( vlc_object_t *p_this )
83 sout_stream_t *p_stream = (sout_stream_t*)p_this;
84 sout_stream_sys_t *p_sys;
85 config_chain_t *p_cfg;
87 msg_Dbg( p_stream, "creating 'duplicate'" );
89 p_sys = malloc( sizeof( sout_stream_sys_t ) );
91 p_sys->i_nb_streams = 0;
92 p_sys->pp_streams = NULL;
93 p_sys->i_nb_select = 0;
94 p_sys->ppsz_select = NULL;
96 for( p_cfg = p_stream->p_cfg; p_cfg != NULL; p_cfg = p_cfg->p_next )
98 if( !strncmp( p_cfg->psz_name, "dst", strlen( "dst" ) ) )
102 msg_Dbg( p_stream, " * adding `%s'", p_cfg->psz_value );
103 s = sout_StreamNew( p_stream->p_sout, p_cfg->psz_value );
107 TAB_APPEND( p_sys->i_nb_streams, p_sys->pp_streams, s );
108 TAB_APPEND( p_sys->i_nb_select, p_sys->ppsz_select, NULL );
111 else if( !strncmp( p_cfg->psz_name, "select", strlen( "select" ) ) )
113 char *psz = p_cfg->psz_value;
114 if( p_sys->i_nb_select > 0 && psz && *psz )
116 msg_Dbg( p_stream, " * apply selection %s", psz );
117 p_sys->ppsz_select[p_sys->i_nb_select - 1] = strdup( psz );
122 if( p_sys->i_nb_streams == 0 )
124 msg_Err( p_stream, "no destination given" );
130 p_stream->pf_add = Add;
131 p_stream->pf_del = Del;
132 p_stream->pf_send = Send;
134 p_stream->p_sys = p_sys;
139 /*****************************************************************************
141 *****************************************************************************/
142 static void Close( vlc_object_t * p_this )
144 sout_stream_t *p_stream = (sout_stream_t*)p_this;
145 sout_stream_sys_t *p_sys = p_stream->p_sys;
149 msg_Dbg( p_stream, "closing a duplication" );
150 for( i = 0; i < p_sys->i_nb_streams; i++ )
152 sout_StreamDelete( p_sys->pp_streams[i] );
153 if( p_sys->ppsz_select[i] )
155 free( p_sys->ppsz_select[i] );
158 if( p_sys->pp_streams )
160 free( p_sys->pp_streams );
162 if( p_sys->ppsz_select )
164 free( p_sys->ppsz_select );
170 /*****************************************************************************
172 *****************************************************************************/
173 static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
175 sout_stream_sys_t *p_sys = p_stream->p_sys;
176 sout_stream_id_t *id;
177 int i_stream, i_valid_streams = 0;
179 id = malloc( sizeof( sout_stream_id_t ) );
183 msg_Dbg( p_stream, "duplicated a new stream codec=%4.4s (es=%d group=%d)",
184 (char*)&p_fmt->i_codec, p_fmt->i_id, p_fmt->i_group );
186 for( i_stream = 0; i_stream < p_sys->i_nb_streams; i_stream++ )
190 if( ESSelected( p_fmt, p_sys->ppsz_select[i_stream] ) )
192 sout_stream_t *out = p_sys->pp_streams[i_stream];
194 id_new = (void*)out->pf_add( out, p_fmt );
197 msg_Dbg( p_stream, " - added for output %d", i_stream );
202 msg_Dbg( p_stream, " - failed for output %d", i_stream );
207 msg_Dbg( p_stream, " - ignored for output %d", i_stream );
210 /* Append failed attempts as well to keep track of which pp_id
211 * belongs to which duplicated stream */
212 TAB_APPEND( id->i_nb_ids, id->pp_ids, id_new );
215 if( i_valid_streams <= 0 )
224 /*****************************************************************************
226 *****************************************************************************/
227 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
229 sout_stream_sys_t *p_sys = p_stream->p_sys;
232 for( i_stream = 0; i_stream < p_sys->i_nb_streams; i_stream++ )
234 if( id->pp_ids[i_stream] )
236 sout_stream_t *out = p_sys->pp_streams[i_stream];
237 out->pf_del( out, id->pp_ids[i_stream] );
246 /*****************************************************************************
248 *****************************************************************************/
249 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
252 sout_stream_sys_t *p_sys = p_stream->p_sys;
253 sout_stream_t *p_dup_stream;
256 /* Loop through the linked list of buffers */
259 block_t *p_next = p_buffer->p_next;
261 p_buffer->p_next = NULL;
263 for( i_stream = 0; i_stream < p_sys->i_nb_streams - 1; i_stream++ )
266 p_dup_stream = p_sys->pp_streams[i_stream];
268 if( id->pp_ids[i_stream] )
270 p_dup = block_Duplicate( p_buffer );
272 p_dup_stream->pf_send( p_dup_stream, id->pp_ids[i_stream],
277 if( i_stream < p_sys->i_nb_streams && id->pp_ids[i_stream] )
279 p_dup_stream = p_sys->pp_streams[i_stream];
280 p_dup_stream->pf_send( p_dup_stream, id->pp_ids[i_stream],
285 block_Release( p_buffer );
293 /*****************************************************************************
295 *****************************************************************************/
296 static vlc_bool_t NumInRange( char *psz_range, int i_num )
298 char *psz = strchr( psz_range, '-' );
304 i_start = strtol( psz_range, &end, 0 );
305 if( end == psz_range ) i_start = i_num;
307 i_stop = strtol( psz, &end, 0 );
308 if( end == psz_range ) i_stop = i_num;
312 i_start = i_stop = strtol( psz_range, NULL, 0 );
315 return i_start <= i_num && i_num <= i_stop ? VLC_TRUE : VLC_FALSE;
318 static vlc_bool_t ESSelected( es_format_t *fmt, char *psz_select )
323 /* We have tri-state variable : no tested (-1), failed(0), succeed(1) */
328 /* If empty all es are selected */
329 if( psz_select == NULL || *psz_select == '\0' )
333 psz_dup = strdup( psz_select );
336 /* If non empty, parse the selection:
337 * We have selection[,selection[,..]] where following selection are recognized:
341 * (no(-))es=[start]-[end] or es=num
342 * (no(-))prgm=[start]-[end] or prgm=num (program works too)
343 * if a negative test failed we exit directly
350 while( *psz == ' ' || *psz == '\t' ) psz++;
353 p = strchr( psz, ',' );
365 if( !strncmp( psz, "no-audio", strlen( "no-audio" ) ) ||
366 !strncmp( psz, "noaudio", strlen( "noaudio" ) ) )
370 i_cat = fmt->i_cat != AUDIO_ES ? 1 : 0;
373 else if( !strncmp( psz, "no-video", strlen( "no-video" ) ) ||
374 !strncmp( psz, "novideo", strlen( "novideo" ) ) )
378 i_cat = fmt->i_cat != VIDEO_ES ? 1 : 0;
381 else if( !strncmp( psz, "no-spu", strlen( "no-spu" ) ) ||
382 !strncmp( psz, "nospu", strlen( "nospu" ) ) )
386 i_cat = fmt->i_cat != SPU_ES ? 1 : 0;
389 else if( !strncmp( psz, "audio", strlen( "audio" ) ) )
393 i_cat = fmt->i_cat == AUDIO_ES ? 1 : 0;
396 else if( !strncmp( psz, "video", strlen( "video" ) ) )
400 i_cat = fmt->i_cat == VIDEO_ES ? 1 : 0;
403 else if( !strncmp( psz, "spu", strlen( "spu" ) ) )
407 i_cat = fmt->i_cat == SPU_ES ? 1 : 0;
410 else if( strchr( psz, '=' ) != NULL )
412 char *psz_arg = strchr( psz, '=' );
415 if( !strcmp( psz, "no-es" ) || !strcmp( psz, "noes" ) )
419 i_es = NumInRange( psz_arg, fmt->i_id ) ? 0 : -1;
422 else if( !strcmp( psz, "es" ) )
426 i_es = NumInRange( psz_arg, fmt->i_id) ? 1 : -1;
429 else if( !strcmp( psz, "no-prgm" ) || !strcmp( psz, "noprgm" ) ||
430 !strcmp( psz, "no-program" ) || !strcmp( psz, "noprogram" ) )
432 if( fmt->i_group >= 0 && i_prgm == -1 )
434 i_prgm = NumInRange( psz_arg, fmt->i_group ) ? 0 : -1;
437 else if( !strcmp( psz, "prgm" ) || !strcmp( psz, "program" ) )
439 if( fmt->i_group >= 0 && i_prgm == -1 )
441 i_prgm = NumInRange( psz_arg, fmt->i_group ) ? 1 : -1;
447 fprintf( stderr, "unknown args (%s)\n", psz );
455 if( i_cat == 1 || i_es == 1 || i_prgm == 1 )