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