]> git.sesse.net Git - vlc/blob - modules/stream_out/gather.c
Typo
[vlc] / modules / stream_out / gather.c
1 /*****************************************************************************
2  * gather.c: gathering stream output module
3  *****************************************************************************
4  * Copyright (C) 2003-2004 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #include <vlc/vlc.h>
29 #include <vlc_input.h>
30 #include <vlc_sout.h>
31
32 /*****************************************************************************
33  * Module descriptor
34  *****************************************************************************/
35 static int      Open    ( vlc_object_t * );
36 static void     Close   ( vlc_object_t * );
37
38 vlc_module_begin();
39     set_description( _("Gathering stream output") );
40     set_capability( "sout stream", 50 );
41     add_shortcut( "gather" );
42     set_callbacks( Open, Close );
43 vlc_module_end();
44
45 /*****************************************************************************
46  * Exported prototypes
47  *****************************************************************************/
48 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
49 static int               Del ( sout_stream_t *, sout_stream_id_t * );
50 static int               Send( sout_stream_t *, sout_stream_id_t *, block_t* );
51
52 struct sout_stream_id_t
53 {
54     vlc_bool_t    b_used;
55
56     es_format_t fmt;
57     void          *id;
58 };
59
60 struct sout_stream_sys_t
61 {
62     sout_stream_t   *p_out;
63
64     int              i_id;
65     sout_stream_id_t **id;
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
76     p_stream->p_sys = p_sys = malloc( sizeof( sout_stream_sys_t ) );
77     if( p_sys == NULL )
78         return VLC_EGENERIC;
79
80     p_sys->p_out    = sout_StreamNew( p_stream->p_sout, p_stream->psz_next );
81     if( p_sys->p_out == NULL )
82     {
83         free( p_sys );
84         return VLC_EGENERIC;
85     }
86     p_stream->pf_add    = Add;
87     p_stream->pf_del    = Del;
88     p_stream->pf_send   = Send;
89
90     TAB_INIT( p_sys->i_id, p_sys->id );
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         sout_stream_id_t *id = p_sys->id[i];
107
108         sout_StreamIdDel( p_sys->p_out, id->id );
109         es_format_Clean( &id->fmt );
110         free( id );
111     }
112     TAB_CLEAN( p_sys->i_id, p_sys->id );
113
114     sout_StreamDelete( p_sys->p_out );
115     free( p_sys );
116 }
117
118 /*****************************************************************************
119  * Add:
120  *****************************************************************************/
121 static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
122 {
123     sout_stream_sys_t *p_sys = p_stream->p_sys;
124     sout_stream_id_t  *id;
125     int i;
126
127     /* search a compatible output */
128     for( i = 0; i < p_sys->i_id; i++ )
129     {
130         id = p_sys->id[i];
131         if( id->b_used )
132             continue;
133
134         if( id->fmt.i_cat != p_fmt->i_cat || id->fmt.i_codec != p_fmt->i_codec )
135             continue;
136
137         if( id->fmt.i_cat == AUDIO_ES )
138         {
139             audio_format_t *p_a = &id->fmt.audio;
140             if( p_a->i_rate != p_fmt->audio.i_rate ||
141                 p_a->i_channels != p_fmt->audio.i_channels ||
142                 p_a->i_blockalign != p_fmt->audio.i_blockalign )
143                 continue;
144         }
145         else if( id->fmt.i_cat == VIDEO_ES )
146         {
147             video_format_t *p_v = &id->fmt.video;
148             if( p_v->i_width != p_fmt->video.i_width ||
149                 p_v->i_height != p_fmt->video.i_height )
150                 continue;
151         }
152
153         /* */
154         msg_Dbg( p_stream, "reusing already opened output" );
155         id->b_used = VLC_TRUE;
156         return id;
157     }
158
159     /* destroy all outputs from the same category */
160     for( i = 0; i < p_sys->i_id; i++ )
161     {
162         id = p_sys->id[i];
163         if( !id->b_used && id->fmt.i_cat == p_fmt->i_cat )
164         {
165             TAB_REMOVE( p_sys->i_id, p_sys->id, id );
166             sout_StreamIdDel( p_sys->p_out, id->id );
167             es_format_Clean( &id->fmt );
168             free( id );
169
170             i = 0;
171             continue;
172         }
173     }
174
175     msg_Dbg( p_stream, "creating new output" );
176     id = malloc( sizeof( sout_stream_id_t ) );
177     if( id == NULL )
178         return NULL;
179     es_format_Copy( &id->fmt, p_fmt );
180     id->b_used           = VLC_TRUE;
181     id->id               = sout_StreamIdAdd( p_sys->p_out, &id->fmt );
182     if( id->id == NULL )
183     {
184         free( id );
185         return NULL;
186     }
187     TAB_APPEND( p_sys->i_id, p_sys->id, id );
188
189     return id;
190 }
191
192 /*****************************************************************************
193  * Del:
194  *****************************************************************************/
195 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
196 {
197     id->b_used = VLC_FALSE;
198     return VLC_SUCCESS;
199 }
200
201 /*****************************************************************************
202  * Send:
203  *****************************************************************************/
204 static int Send( sout_stream_t *p_stream,
205                  sout_stream_id_t *id, block_t *p_buffer )
206 {
207     sout_stream_sys_t *p_sys = p_stream->p_sys;
208
209     return sout_StreamIdSend( p_sys->p_out, id->id, p_buffer );
210 }