]> git.sesse.net Git - vlc/blob - modules/stream_out/duplicate.c
* all: new sout scheme. Now a chain of module are created that can
[vlc] / modules / stream_out / duplicate.c
1 /*****************************************************************************
2  * duplicate.c
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id: duplicate.c,v 1.1 2003/04/13 20:00:21 fenrir 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 *, sout_buffer_t* );
42
43 /*****************************************************************************
44  * Module descriptor
45  *****************************************************************************/
46 vlc_module_begin();
47     set_description( _("Duplicate stream") );
48     set_capability( "sout stream", 50 );
49     add_shortcut( "duplicate" );
50     add_shortcut( "dup" );
51     set_callbacks( Open, Close );
52 vlc_module_end();
53
54
55 struct sout_stream_sys_t
56 {
57     int             i_nb_streams;
58     sout_stream_t   **pp_streams;
59 };
60
61 struct sout_stream_id_t
62 {
63     int                 i_nb_ids;
64     void                **pp_ids;
65 };
66
67 /*****************************************************************************
68  * Open:
69  *****************************************************************************/
70 static int Open( vlc_object_t *p_this )
71 {
72     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
73     sout_stream_sys_t *p_sys;
74     sout_cfg_t        *p_cfg;
75
76     msg_Dbg( p_stream, "creating a duplication" );
77
78     p_sys = malloc( sizeof( sout_stream_sys_t ) );
79     p_sys->i_nb_streams = 0;
80     p_sys->pp_streams   = NULL;
81
82     for( p_cfg = p_stream->p_cfg; p_cfg != NULL; p_cfg = p_cfg->p_next )
83     {
84         if( !strncmp( p_cfg->psz_name, "dst", strlen( "dst" ) ) )
85         {
86             sout_stream_t *s;
87
88             msg_Dbg( p_stream, " * adding `%s'", p_cfg->psz_value );
89             s = sout_stream_new( p_stream->p_sout, p_cfg->psz_value );
90
91             TAB_APPEND( p_sys->i_nb_streams, p_sys->pp_streams, s );
92         }
93     }
94
95     if( p_sys->i_nb_streams == 0 )
96     {
97         msg_Err( p_stream, "no destination given" );
98         free( p_sys );
99
100         return VLC_EGENERIC;
101     }
102
103     p_stream->pf_add    = Add;
104     p_stream->pf_del    = Del;
105     p_stream->pf_send   = Send;
106
107     p_stream->p_sys     = p_sys;
108
109     return VLC_SUCCESS;
110 }
111
112 /*****************************************************************************
113  * Close:
114  *****************************************************************************/
115
116 static void Close( vlc_object_t * p_this )
117 {
118     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
119     sout_stream_sys_t *p_sys = p_stream->p_sys;
120
121     int i;
122
123     msg_Dbg( p_stream, "closing a duplication" );
124     for( i = 0; i < p_sys->i_nb_streams; i++ )
125     {
126         sout_stream_delete( p_sys->pp_streams[i] );
127     }
128     free( p_sys->pp_streams );
129
130     free( p_sys );
131 }
132
133
134
135 static sout_stream_id_t * Add      ( sout_stream_t *p_stream, sout_format_t *p_fmt )
136 {
137     sout_stream_sys_t *p_sys = p_stream->p_sys;
138     sout_stream_id_t  *id;
139     int               i_stream;
140
141     id = malloc( sizeof( sout_stream_id_t ) );
142     id->i_nb_ids = 0;
143     id->pp_ids   = NULL;
144
145     for( i_stream = 0; i_stream < p_sys->i_nb_streams; i_stream++ )
146     {
147         void *id_new;
148
149         /* XXX not the same sout_stream_id_t definition ... */
150         id_new = (void*)p_sys->pp_streams[i_stream]->pf_add( p_sys->pp_streams[i_stream], p_fmt );
151         TAB_APPEND( id->i_nb_ids, id->pp_ids, id_new );
152     }
153
154     if( id->i_nb_ids <= 0 )
155     {
156         free( id );
157         return NULL;
158     }
159
160     return id;
161 }
162
163 static int     Del      ( sout_stream_t *p_stream, sout_stream_id_t *id )
164 {
165     sout_stream_sys_t *p_sys = p_stream->p_sys;
166     int               i_stream;
167
168     for( i_stream = 0; i_stream < p_sys->i_nb_streams; i_stream++ )
169     {
170         if( id->pp_ids[i_stream] )
171         {
172             p_sys->pp_streams[i_stream]->pf_del( p_sys->pp_streams[i_stream],
173                                                  id->pp_ids[i_stream] );
174         }
175     }
176
177     free( id->pp_ids );
178     free( id );
179     return VLC_SUCCESS;
180 }
181
182 static int     Send     ( sout_stream_t *p_stream, sout_stream_id_t *id, sout_buffer_t *p_buffer )
183 {
184     sout_stream_sys_t *p_sys = p_stream->p_sys;
185     int               i_stream;
186
187     for( i_stream = 0; i_stream < p_sys->i_nb_streams - 1; i_stream++ )
188     {
189         sout_buffer_t *p_dup;
190
191         if( id->pp_ids[i_stream] )
192         {
193             p_dup = sout_BufferDuplicate( p_stream->p_sout, p_buffer );
194
195             p_sys->pp_streams[i_stream]->pf_send( p_sys->pp_streams[i_stream],
196                                                   id->pp_ids[i_stream],
197                                                   p_dup );
198         }
199     }
200
201     i_stream = p_sys->i_nb_streams - 1;
202     if( id->pp_ids[i_stream] )
203     {
204         p_sys->pp_streams[i_stream]->pf_send( p_sys->pp_streams[i_stream],
205                                               id->pp_ids[i_stream],
206                                               p_buffer);
207     }
208
209     return VLC_SUCCESS;
210 }
211