]> git.sesse.net Git - vlc/blob - modules/stream_out/gather.c
7aee39a67c2e6d1e70ac4f918e75ee575f7db8bb
[vlc] / modules / stream_out / gather.c
1 /*****************************************************************************
2  * gather.c
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id: gather.c,v 1.1 2003/09/07 20:12:44 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
29 #include <vlc/vlc.h>
30 #include <vlc/input.h>
31 #include <vlc/sout.h>
32
33 /*****************************************************************************
34  * Module descriptor
35  *****************************************************************************/
36 static int      Open    ( vlc_object_t * );
37 static void     Close   ( vlc_object_t * );
38
39 vlc_module_begin();
40     set_description( _("Gather stream") );
41     set_capability( "sout stream", 50 );
42     add_shortcut( "gather" );
43     set_callbacks( Open, Close );
44 vlc_module_end();
45
46 /*****************************************************************************
47  * Exported prototypes
48  *****************************************************************************/
49 static sout_stream_id_t *Add ( sout_stream_t *, sout_format_t * );
50 static int               Del ( sout_stream_t *, sout_stream_id_t * );
51 static int               Send( sout_stream_t *, sout_stream_id_t *, sout_buffer_t* );
52
53 struct sout_stream_id_t
54 {
55     vlc_bool_t    b_used;
56
57     sout_format_t fmt;
58     void          *id;
59 };
60
61 struct sout_stream_sys_t
62 {
63     sout_stream_t   *p_out;
64
65     int              i_id;
66     sout_stream_id_t **id;
67 };
68
69 /*****************************************************************************
70  * Open:
71  *****************************************************************************/
72 static int Open( vlc_object_t *p_this )
73 {
74     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
75     sout_stream_sys_t *p_sys;
76
77     p_stream->p_sys     = p_sys = malloc( sizeof( sout_stream_sys_t ) );
78     p_sys->p_out        = sout_stream_new( p_stream->p_sout, p_stream->psz_next );
79     if( p_sys->p_out == NULL )
80     {
81         free( p_sys );
82         return VLC_EGENERIC;
83     }
84     p_sys->i_id         = 0;
85     p_sys->id           = NULL;
86
87     p_stream->pf_add    = Add;
88     p_stream->pf_del    = Del;
89     p_stream->pf_send   = Send;
90
91     return VLC_SUCCESS;
92 }
93
94 /*****************************************************************************
95  * Close:
96  *****************************************************************************/
97 static void Close( vlc_object_t * p_this )
98 {
99     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
100     sout_stream_sys_t *p_sys = p_stream->p_sys;
101     int i;
102
103     for( i = 0; i < p_sys->i_id; i++ )
104     {
105         p_sys->p_out->pf_del( p_sys->p_out, p_sys->id[i]->id );
106         free( p_sys->id[i] );
107     }
108     free( p_sys->id );
109
110     sout_stream_delete( p_sys->p_out );
111     free( p_sys );
112 }
113
114 /*****************************************************************************
115  * Add:
116  *****************************************************************************/
117 static sout_stream_id_t * Add( sout_stream_t *p_stream, sout_format_t *p_fmt )
118 {
119     sout_stream_sys_t *p_sys = p_stream->p_sys;
120     sout_stream_id_t  *id;
121     int i;
122
123     /* search a output compatible */
124     for( i = 0; i < p_sys->i_id; i++ )
125     {
126         id = p_sys->id[i];
127         if( !id->b_used &&
128             id->fmt.i_cat == p_fmt->i_cat &&
129             id->fmt.i_fourcc == p_fmt->i_fourcc &&
130             ( ( id->fmt.i_cat == AUDIO_ES &&
131                 id->fmt.i_sample_rate == p_fmt->i_sample_rate &&
132                 id->fmt.i_channels == p_fmt->i_channels &&
133                 id->fmt.i_block_align == p_fmt->i_block_align ) ||
134               ( id->fmt.i_cat == VIDEO_ES &&
135                 id->fmt.i_width == p_fmt->i_width &&
136                 id->fmt.i_height == p_fmt->i_height ) ) )
137         {
138             msg_Dbg( p_stream, "reusing already opened output" );
139             id->b_used = VLC_TRUE;
140             return id;
141         }
142     }
143
144     /* destroy all output of the same categorie */
145     for( i = 0; i < p_sys->i_id; i++ )
146     {
147         id = p_sys->id[i];
148         if( !id->b_used && id->fmt.i_cat == p_fmt->i_cat )
149         {
150             TAB_REMOVE( p_sys->i_id, p_sys->id, id );
151             p_sys->p_out->pf_del( p_sys->p_out, id );
152             free( id );
153
154             i = 0;
155             continue;
156         }
157     }
158
159     id = malloc( sizeof( sout_stream_id_t ) );
160     msg_Dbg( p_stream, "creating new output" );
161     memcpy( &id->fmt, p_fmt, sizeof( sout_format_t ) );
162     id->fmt.i_extra_data = 0;
163     id->fmt.p_extra_data = NULL;
164     id->b_used           = VLC_TRUE;
165     id->id               = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
166     if( id->id == NULL )
167     {
168         free( id );
169         return NULL;
170     }
171     TAB_APPEND( p_sys->i_id, p_sys->id, id );
172
173     return id;
174 }
175
176 /*****************************************************************************
177  * Del:
178  *****************************************************************************/
179 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
180 {
181     id->b_used = VLC_FALSE;
182     return VLC_SUCCESS;
183 }
184
185 /*****************************************************************************
186  * Send:
187  *****************************************************************************/
188 static int Send( sout_stream_t *p_stream,
189                  sout_stream_id_t *id, sout_buffer_t *p_buffer )
190 {
191     sout_stream_sys_t *p_sys = p_stream->p_sys;
192
193     return p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
194 }
195