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