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