]> git.sesse.net Git - vlc/blob - modules/stream_out/gather.c
Merge branch 'master' of git@git.videolan.org:vlc
[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 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc/vlc.h>
33 #include <vlc_input.h>
34 #include <vlc_sout.h>
35
36 /*****************************************************************************
37  * Module descriptor
38  *****************************************************************************/
39 static int      Open    ( vlc_object_t * );
40 static void     Close   ( vlc_object_t * );
41
42 vlc_module_begin();
43     set_description( _("Gathering stream output") );
44     set_capability( "sout stream", 50 );
45     add_shortcut( "gather" );
46     set_callbacks( Open, Close );
47 vlc_module_end();
48
49 /*****************************************************************************
50  * Exported prototypes
51  *****************************************************************************/
52 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
53 static int               Del ( sout_stream_t *, sout_stream_id_t * );
54 static int               Send( sout_stream_t *, sout_stream_id_t *, block_t* );
55
56 struct sout_stream_id_t
57 {
58     bool    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     if( p_sys == NULL )
82         return VLC_EGENERIC;
83
84     p_sys->p_out    = sout_StreamNew( p_stream->p_sout, p_stream->psz_next );
85     if( p_sys->p_out == NULL )
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_t *id = p_sys->id[i];
111
112         sout_StreamIdDel( p_sys->p_out, id->id );
113         es_format_Clean( &id->fmt );
114         free( id );
115     }
116     TAB_CLEAN( p_sys->i_id, p_sys->id );
117
118     sout_StreamDelete( p_sys->p_out );
119     free( p_sys );
120 }
121
122 /*****************************************************************************
123  * Add:
124  *****************************************************************************/
125 static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
126 {
127     sout_stream_sys_t *p_sys = p_stream->p_sys;
128     sout_stream_id_t  *id;
129     int i;
130
131     /* search a compatible output */
132     for( i = 0; i < p_sys->i_id; i++ )
133     {
134         id = p_sys->id[i];
135         if( id->b_used )
136             continue;
137
138         if( id->fmt.i_cat != p_fmt->i_cat || id->fmt.i_codec != p_fmt->i_codec )
139             continue;
140
141         if( id->fmt.i_cat == AUDIO_ES )
142         {
143             audio_format_t *p_a = &id->fmt.audio;
144             if( p_a->i_rate != p_fmt->audio.i_rate ||
145                 p_a->i_channels != p_fmt->audio.i_channels ||
146                 p_a->i_blockalign != p_fmt->audio.i_blockalign )
147                 continue;
148         }
149         else if( id->fmt.i_cat == VIDEO_ES )
150         {
151             video_format_t *p_v = &id->fmt.video;
152             if( p_v->i_width != p_fmt->video.i_width ||
153                 p_v->i_height != p_fmt->video.i_height )
154                 continue;
155         }
156
157         /* */
158         msg_Dbg( p_stream, "reusing already opened output" );
159         id->b_used = 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_sys->p_out, 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_t ) );
181     if( id == NULL )
182         return NULL;
183     es_format_Copy( &id->fmt, p_fmt );
184     id->b_used           = true;
185     id->id               = sout_StreamIdAdd( p_sys->p_out, &id->fmt );
186     if( id->id == NULL )
187     {
188         free( id );
189         return NULL;
190     }
191     TAB_APPEND( p_sys->i_id, p_sys->id, id );
192
193     return id;
194 }
195
196 /*****************************************************************************
197  * Del:
198  *****************************************************************************/
199 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
200 {
201     VLC_UNUSED(p_stream);
202     id->b_used = false;
203     return VLC_SUCCESS;
204 }
205
206 /*****************************************************************************
207  * Send:
208  *****************************************************************************/
209 static int Send( sout_stream_t *p_stream,
210                  sout_stream_id_t *id, block_t *p_buffer )
211 {
212     sout_stream_sys_t *p_sys = p_stream->p_sys;
213
214     return sout_StreamIdSend( p_sys->p_out, id->id, p_buffer );
215 }