]> git.sesse.net Git - vlc/blob - modules/stream_out/duplicate.c
* src/stream_output/stream_output.c, modules/stream_out/duplicate.c: bug fixes for...
[vlc] / modules / stream_out / duplicate.c
1 /*****************************************************************************
2  * duplicate.c
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id: duplicate.c,v 1.2 2003/05/19 11:38:05 gbazin Exp $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include <vlc/vlc.h>
31 #include <vlc/sout.h>
32
33 /*****************************************************************************
34  * Exported prototypes
35  *****************************************************************************/
36 static int      Open    ( vlc_object_t * );
37 static void     Close   ( vlc_object_t * );
38
39 static sout_stream_id_t *Add ( sout_stream_t *, sout_format_t * );
40 static int               Del ( sout_stream_t *, sout_stream_id_t * );
41 static int               Send( sout_stream_t *, sout_stream_id_t *,
42                                sout_buffer_t* );
43
44 /*****************************************************************************
45  * Module descriptor
46  *****************************************************************************/
47 vlc_module_begin();
48     set_description( _("Duplicate stream") );
49     set_capability( "sout stream", 50 );
50     add_shortcut( "duplicate" );
51     add_shortcut( "dup" );
52     set_callbacks( Open, Close );
53 vlc_module_end();
54
55
56 struct sout_stream_sys_t
57 {
58     int             i_nb_streams;
59     sout_stream_t   **pp_streams;
60 };
61
62 struct sout_stream_id_t
63 {
64     int                 i_nb_ids;
65     void                **pp_ids;
66 };
67
68 /*****************************************************************************
69  * Open:
70  *****************************************************************************/
71 static int Open( vlc_object_t *p_this )
72 {
73     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
74     sout_stream_sys_t *p_sys;
75     sout_cfg_t        *p_cfg;
76
77     msg_Dbg( p_stream, "creating a duplication" );
78
79     p_sys = malloc( sizeof( sout_stream_sys_t ) );
80     p_sys->i_nb_streams = 0;
81     p_sys->pp_streams   = NULL;
82
83     for( p_cfg = p_stream->p_cfg; p_cfg != NULL; p_cfg = p_cfg->p_next )
84     {
85         if( !strncmp( p_cfg->psz_name, "dst", strlen( "dst" ) ) )
86         {
87             sout_stream_t *s;
88
89             msg_Dbg( p_stream, " * adding `%s'", p_cfg->psz_value );
90             s = sout_stream_new( p_stream->p_sout, p_cfg->psz_value );
91
92             if( s )
93             {
94                 TAB_APPEND( p_sys->i_nb_streams, p_sys->pp_streams, s );
95             }
96         }
97     }
98
99     if( p_sys->i_nb_streams == 0 )
100     {
101         msg_Err( p_stream, "no destination given" );
102         free( p_sys );
103
104         return VLC_EGENERIC;
105     }
106
107     p_stream->pf_add    = Add;
108     p_stream->pf_del    = Del;
109     p_stream->pf_send   = Send;
110
111     p_stream->p_sys     = p_sys;
112
113     return VLC_SUCCESS;
114 }
115
116 /*****************************************************************************
117  * Close:
118  *****************************************************************************/
119
120 static void Close( vlc_object_t * p_this )
121 {
122     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
123     sout_stream_sys_t *p_sys = p_stream->p_sys;
124
125     int i;
126
127     msg_Dbg( p_stream, "closing a duplication");
128     for( i = 0; i < p_sys->i_nb_streams; i++ )
129     {
130         sout_stream_delete( p_sys->pp_streams[i] );
131     }
132     free( p_sys->pp_streams );
133
134     free( p_sys );
135 }
136
137 static sout_stream_id_t * Add( sout_stream_t *p_stream, sout_format_t *p_fmt )
138 {
139     sout_stream_sys_t *p_sys = p_stream->p_sys;
140     sout_stream_id_t  *id;
141     int               i_stream;
142
143     id = malloc( sizeof( sout_stream_id_t ) );
144     id->i_nb_ids = 0;
145     id->pp_ids   = NULL;
146
147     for( i_stream = 0; i_stream < p_sys->i_nb_streams; i_stream++ )
148     {
149         void *id_new;
150
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 );
154         if( id_new )
155         {
156             TAB_APPEND( id->i_nb_ids, id->pp_ids, id_new );
157         }
158     }
159
160     if( id->i_nb_ids <= 0 )
161     {
162         free( id );
163         return NULL;
164     }
165
166     return id;
167 }
168
169 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
170 {
171     sout_stream_sys_t *p_sys = p_stream->p_sys;
172     int               i_stream;
173
174     for( i_stream = 0; i_stream < p_sys->i_nb_streams; i_stream++ )
175     {
176         if( id->pp_ids[i_stream] )
177         {
178             p_sys->pp_streams[i_stream]->pf_del( p_sys->pp_streams[i_stream],
179                                                  id->pp_ids[i_stream] );
180         }
181     }
182
183     free( id->pp_ids );
184     free( id );
185     return VLC_SUCCESS;
186 }
187
188 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
189                  sout_buffer_t *p_buffer )
190 {
191     sout_stream_sys_t *p_sys = p_stream->p_sys;
192     int               i_stream;
193
194     for( i_stream = 0; i_stream < p_sys->i_nb_streams - 1; i_stream++ )
195     {
196         sout_buffer_t *p_dup;
197
198         if( id->pp_ids[i_stream] )
199         {
200             p_dup = sout_BufferDuplicate( p_stream->p_sout, p_buffer );
201
202             p_sys->pp_streams[i_stream]->pf_send( p_sys->pp_streams[i_stream],
203                                                   id->pp_ids[i_stream],
204                                                   p_dup );
205         }
206     }
207
208     i_stream = p_sys->i_nb_streams - 1;
209     if( id->pp_ids[i_stream] )
210     {
211         p_sys->pp_streams[i_stream]->pf_send( p_sys->pp_streams[i_stream],
212                                               id->pp_ids[i_stream],
213                                               p_buffer);
214     }
215
216     return VLC_SUCCESS;
217 }