1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: duplicate.c,v 1.8 2003/12/22 14:32:56 sam Exp $
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
33 /*****************************************************************************
35 *****************************************************************************/
36 static int Open ( vlc_object_t * );
37 static void Close ( vlc_object_t * );
39 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
40 static int Del ( sout_stream_t *, sout_stream_id_t * );
41 static int Send( sout_stream_t *, sout_stream_id_t *,
44 /*****************************************************************************
46 *****************************************************************************/
48 set_description( _("Duplicate stream") );
49 set_capability( "sout stream", 50 );
50 add_shortcut( "duplicate" );
51 add_shortcut( "dup" );
52 set_callbacks( Open, Close );
56 struct sout_stream_sys_t
59 sout_stream_t **pp_streams;
62 struct sout_stream_id_t
68 /*****************************************************************************
70 *****************************************************************************/
71 static int Open( vlc_object_t *p_this )
73 sout_stream_t *p_stream = (sout_stream_t*)p_this;
74 sout_stream_sys_t *p_sys;
77 msg_Dbg( p_stream, "creating a duplication" );
79 p_sys = malloc( sizeof( sout_stream_sys_t ) );
80 p_sys->i_nb_streams = 0;
81 p_sys->pp_streams = NULL;
83 for( p_cfg = p_stream->p_cfg; p_cfg != NULL; p_cfg = p_cfg->p_next )
85 if( !strncmp( p_cfg->psz_name, "dst", strlen( "dst" ) ) )
89 msg_Dbg( p_stream, " * adding `%s'", p_cfg->psz_value );
90 s = sout_stream_new( p_stream->p_sout, p_cfg->psz_value );
94 TAB_APPEND( p_sys->i_nb_streams, p_sys->pp_streams, s );
99 if( p_sys->i_nb_streams == 0 )
101 msg_Err( p_stream, "no destination given" );
107 p_stream->pf_add = Add;
108 p_stream->pf_del = Del;
109 p_stream->pf_send = Send;
111 p_stream->p_sys = p_sys;
116 /*****************************************************************************
118 *****************************************************************************/
120 static void Close( vlc_object_t * p_this )
122 sout_stream_t *p_stream = (sout_stream_t*)p_this;
123 sout_stream_sys_t *p_sys = p_stream->p_sys;
127 msg_Dbg( p_stream, "closing a duplication");
128 for( i = 0; i < p_sys->i_nb_streams; i++ )
130 sout_stream_delete( p_sys->pp_streams[i] );
132 free( p_sys->pp_streams );
137 static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
139 sout_stream_sys_t *p_sys = p_stream->p_sys;
140 sout_stream_id_t *id;
141 int i_stream, i_valid_streams = 0;
143 id = malloc( sizeof( sout_stream_id_t ) );
147 for( i_stream = 0; i_stream < p_sys->i_nb_streams; i_stream++ )
151 /* XXX not the same sout_stream_id_t definition ... */
152 id_new = (void*)p_sys->pp_streams[i_stream]->pf_add(
153 p_sys->pp_streams[i_stream], p_fmt );
155 /* Append failed attempts as well to keep track of which pp_id
156 * belongs to which duplicated stream */
157 TAB_APPEND( id->i_nb_ids, id->pp_ids, id_new );
158 if( id_new ) i_valid_streams++;
161 if( i_valid_streams <= 0 )
170 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
172 sout_stream_sys_t *p_sys = p_stream->p_sys;
175 for( i_stream = 0; i_stream < p_sys->i_nb_streams; i_stream++ )
177 if( id->pp_ids[i_stream] )
179 p_sys->pp_streams[i_stream]->pf_del( p_sys->pp_streams[i_stream],
180 id->pp_ids[i_stream] );
189 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
190 sout_buffer_t *p_buffer )
192 sout_stream_sys_t *p_sys = p_stream->p_sys;
193 sout_stream_t *p_dup_stream;
196 /* Loop through the linked list of buffers */
199 sout_buffer_t *p_next = p_buffer->p_next;
201 p_buffer->p_next = NULL;
203 for( i_stream = 0; i_stream < p_sys->i_nb_streams - 1; i_stream++ )
205 sout_buffer_t *p_dup;
206 p_dup_stream = p_sys->pp_streams[i_stream];
208 if( id->pp_ids[i_stream] )
210 p_dup = sout_BufferDuplicate( p_stream->p_sout, p_buffer );
212 p_dup_stream->pf_send( p_dup_stream, id->pp_ids[i_stream],
217 if( i_stream < p_sys->i_nb_streams && id->pp_ids[i_stream] )
219 p_dup_stream = p_sys->pp_streams[i_stream];
220 p_dup_stream->pf_send( p_dup_stream, id->pp_ids[i_stream],
225 sout_BufferDelete( p_stream->p_sout, p_buffer );