]> git.sesse.net Git - vlc/blob - modules/stream_out/duplicate.c
* Coding style cleanup: removed tabs and trailing spaces.
[vlc] / modules / stream_out / duplicate.c
1 /*****************************************************************************
2  * duplicate.c
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id: duplicate.c,v 1.8 2003/12/22 14:32:56 sam Exp $
6  *
7  * Author: 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 *, 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 *,
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, es_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, i_valid_streams = 0;
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
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++;
159     }
160
161     if( i_valid_streams <= 0 )
162     {
163         Del( p_stream, id );
164         return NULL;
165     }
166
167     return id;
168 }
169
170 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
171 {
172     sout_stream_sys_t *p_sys = p_stream->p_sys;
173     int               i_stream;
174
175     for( i_stream = 0; i_stream < p_sys->i_nb_streams; i_stream++ )
176     {
177         if( id->pp_ids[i_stream] )
178         {
179             p_sys->pp_streams[i_stream]->pf_del( p_sys->pp_streams[i_stream],
180                                                  id->pp_ids[i_stream] );
181         }
182     }
183
184     free( id->pp_ids );
185     free( id );
186     return VLC_SUCCESS;
187 }
188
189 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
190                  sout_buffer_t *p_buffer )
191 {
192     sout_stream_sys_t *p_sys = p_stream->p_sys;
193     sout_stream_t     *p_dup_stream;
194     int               i_stream;
195
196     /* Loop through the linked list of buffers */
197     while( p_buffer )
198     {
199         sout_buffer_t *p_next = p_buffer->p_next;
200
201         p_buffer->p_next = NULL;
202
203         for( i_stream = 0; i_stream < p_sys->i_nb_streams - 1; i_stream++ )
204         {
205             sout_buffer_t *p_dup;
206             p_dup_stream = p_sys->pp_streams[i_stream];
207
208             if( id->pp_ids[i_stream] )
209             {
210                 p_dup = sout_BufferDuplicate( p_stream->p_sout, p_buffer );
211
212                 p_dup_stream->pf_send( p_dup_stream, id->pp_ids[i_stream],
213                                        p_dup );
214             }
215         }
216
217         if( i_stream < p_sys->i_nb_streams && id->pp_ids[i_stream] )
218         {
219             p_dup_stream = p_sys->pp_streams[i_stream];
220             p_dup_stream->pf_send( p_dup_stream, id->pp_ids[i_stream],
221                                    p_buffer );
222         }
223         else
224         {
225             sout_BufferDelete( p_stream->p_sout, p_buffer );
226         }
227
228         p_buffer = p_next;
229     }
230     return VLC_SUCCESS;
231 }