]> git.sesse.net Git - vlc/blob - modules/stream_out/gather.c
sout: voidify sout_stream_t.pf_del
[vlc] / modules / stream_out / gather.c
1 /*****************************************************************************
2  * gather.c: gathering stream output module
3  *****************************************************************************
4  * Copyright (C) 2003-2004 VLC authors and 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 it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_input.h>
35 #include <vlc_sout.h>
36 #include <vlc_block.h>
37
38 /*****************************************************************************
39  * Module descriptor
40  *****************************************************************************/
41 static int      Open    ( vlc_object_t * );
42 static void     Close   ( vlc_object_t * );
43
44 vlc_module_begin ()
45     set_description( N_("Gathering stream output") )
46     set_capability( "sout stream", 50 )
47     add_shortcut( "gather" )
48     set_callbacks( Open, Close )
49 vlc_module_end ()
50
51 /*****************************************************************************
52  * Exported prototypes
53  *****************************************************************************/
54 static sout_stream_id_sys_t *Add ( sout_stream_t *, es_format_t * );
55 static void              Del ( sout_stream_t *, sout_stream_id_sys_t * );
56 static int               Send( sout_stream_t *, sout_stream_id_sys_t *, block_t* );
57
58 struct sout_stream_id_sys_t
59 {
60     bool    b_used;
61     bool    b_streamswap;
62
63     es_format_t fmt;
64     void          *id;
65 };
66
67 struct sout_stream_sys_t
68 {
69     int              i_id;
70     sout_stream_id_sys_t **id;
71 };
72
73 /*****************************************************************************
74  * Open:
75  *****************************************************************************/
76 static int Open( vlc_object_t *p_this )
77 {
78     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
79     sout_stream_sys_t *p_sys;
80
81     p_stream->p_sys = p_sys = malloc( sizeof( sout_stream_sys_t ) );
82     if( p_sys == NULL )
83         return VLC_EGENERIC;
84
85     if( !p_stream->p_next )
86     {
87         free( p_sys );
88         return VLC_EGENERIC;
89     }
90     p_stream->pf_add    = Add;
91     p_stream->pf_del    = Del;
92     p_stream->pf_send   = Send;
93
94     TAB_INIT( p_sys->i_id, p_sys->id );
95
96     return VLC_SUCCESS;
97 }
98
99 /*****************************************************************************
100  * Close:
101  *****************************************************************************/
102 static void Close( vlc_object_t * p_this )
103 {
104     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
105     sout_stream_sys_t *p_sys = p_stream->p_sys;
106     int i;
107
108     for( i = 0; i < p_sys->i_id; i++ )
109     {
110         sout_stream_id_sys_t *id = p_sys->id[i];
111
112         sout_StreamIdDel( p_stream->p_next, id->id );
113         es_format_Clean( &id->fmt );
114         free( id );
115     }
116     TAB_CLEAN( p_sys->i_id, p_sys->id );
117
118     free( p_sys );
119 }
120
121 /*****************************************************************************
122  * Add:
123  *****************************************************************************/
124 static sout_stream_id_sys_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
125 {
126     sout_stream_sys_t *p_sys = p_stream->p_sys;
127     sout_stream_id_sys_t  *id;
128     int i;
129
130     /* search a compatible output */
131     for( i = 0; i < p_sys->i_id; i++ )
132     {
133         id = p_sys->id[i];
134         if( id->b_used )
135             continue;
136
137         if( id->fmt.i_cat != p_fmt->i_cat || id->fmt.i_codec != p_fmt->i_codec )
138             continue;
139
140         if( id->fmt.i_cat == AUDIO_ES )
141         {
142             audio_format_t *p_a = &id->fmt.audio;
143             if( p_a->i_rate != p_fmt->audio.i_rate ||
144                 p_a->i_channels != p_fmt->audio.i_channels ||
145                 p_a->i_blockalign != p_fmt->audio.i_blockalign )
146                 continue;
147         }
148         else if( id->fmt.i_cat == VIDEO_ES )
149         {
150             video_format_t *p_v = &id->fmt.video;
151             if( p_v->i_width != p_fmt->video.i_width ||
152                 p_v->i_height != p_fmt->video.i_height )
153                 continue;
154         }
155
156         /* */
157         msg_Dbg( p_stream, "reusing already opened output" );
158         id->b_used = true;
159         id->b_streamswap = true;
160         return id;
161     }
162
163     /* destroy all outputs from the same category */
164     for( i = 0; i < p_sys->i_id; i++ )
165     {
166         id = p_sys->id[i];
167         if( !id->b_used && id->fmt.i_cat == p_fmt->i_cat )
168         {
169             TAB_REMOVE( p_sys->i_id, p_sys->id, id );
170             sout_StreamIdDel( p_stream->p_next, id->id );
171             es_format_Clean( &id->fmt );
172             free( id );
173
174             i = 0;
175             continue;
176         }
177     }
178
179     msg_Dbg( p_stream, "creating new output" );
180     id = malloc( sizeof( sout_stream_id_sys_t ) );
181     if( id == NULL )
182         return NULL;
183     es_format_Copy( &id->fmt, p_fmt );
184     id->b_streamswap     = false;
185     id->b_used           = true;
186     id->id               = sout_StreamIdAdd( p_stream->p_next, &id->fmt );
187     if( id->id == NULL )
188     {
189         free( id );
190         return NULL;
191     }
192     TAB_APPEND( p_sys->i_id, p_sys->id, id );
193
194     return id;
195 }
196
197 /*****************************************************************************
198  * Del:
199  *****************************************************************************/
200 static void Del( sout_stream_t *p_stream, sout_stream_id_sys_t *id )
201 {
202     VLC_UNUSED(p_stream);
203     id->b_used = false;
204 }
205
206 /*****************************************************************************
207  * Send:
208  *****************************************************************************/
209 static int Send( sout_stream_t *p_stream,
210                  sout_stream_id_sys_t *id, block_t *p_buffer )
211 {
212     if ( id->b_streamswap )
213     {
214         id->b_streamswap = false;
215         p_buffer->i_flags |= BLOCK_FLAG_DISCONTINUITY;
216     }
217     return sout_StreamIdSend( p_stream->p_next, id->id, p_buffer );
218 }