1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2008 Laurent Aimar
7 * Authors: Laurent Aimar < fenrir _AT_ videolan _DOT_ org >
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
31 #include <vlc_common.h>
35 #include "../libvlc.h"
36 #include "../stream_output/stream_output.h"
37 #include "../audio_output/aout_internal.h"
38 #include "input_interface.h"
39 #include "ressource.h"
41 struct input_ressource_t
45 input_thread_t *p_input;
47 sout_instance_t *p_sout;
50 vout_thread_t **pp_vout;
51 vout_thread_t *p_vout_free;
53 aout_instance_t *p_aout;
57 static void DestroySout( input_ressource_t *p_ressource )
59 if( p_ressource->p_sout )
60 sout_DeleteInstance( p_ressource->p_sout );
61 p_ressource->p_sout = NULL;
63 static sout_instance_t *RequestSout( input_ressource_t *p_ressource,
64 sout_instance_t *p_sout, const char *psz_sout )
66 if( !p_sout && !psz_sout )
68 if( p_ressource->p_sout )
69 msg_Dbg( p_ressource->p_sout, "destroying useless sout" );
70 DestroySout( p_ressource );
74 assert( p_ressource->p_input );
75 assert( !p_sout || ( !p_ressource->p_sout && !psz_sout ) );
77 /* Check the validity of the sout */
78 if( p_ressource->p_sout &&
79 strcmp( p_ressource->p_sout->psz_sout, psz_sout ) )
81 msg_Dbg( p_ressource->p_input, "destroying unusable sout" );
82 DestroySout( p_ressource );
87 if( p_ressource->p_sout )
90 msg_Dbg( p_ressource->p_input, "reusing sout" );
91 msg_Dbg( p_ressource->p_input, "you probably want to use gather stream_out" );
92 vlc_object_attach( p_ressource->p_sout, p_ressource->p_input );
96 /* Create a new one */
97 p_ressource->p_sout = sout_NewInstance( p_ressource->p_input, psz_sout );
100 p_sout = p_ressource->p_sout;
101 p_ressource->p_sout = NULL;
107 vlc_object_detach( p_sout );
108 p_ressource->p_sout = p_sout;
115 static void DestroyVout( input_ressource_t *p_ressource )
117 assert( p_ressource->i_vout == 0 );
119 if( p_ressource->p_vout_free )
120 vout_CloseAndRelease( p_ressource->p_vout_free );
122 p_ressource->p_vout_free = NULL;
124 static vout_thread_t *RequestVout( input_ressource_t *p_ressource,
125 vout_thread_t *p_vout, video_format_t *p_fmt )
127 if( !p_vout && !p_fmt )
129 if( p_ressource->p_vout_free )
131 msg_Dbg( p_ressource->p_vout_free, "destroying useless vout" );
132 vout_CloseAndRelease( p_ressource->p_vout_free );
133 p_ressource->p_vout_free = NULL;
138 assert( p_ressource->p_input );
142 if( !p_vout && p_ressource->p_vout_free )
144 msg_Dbg( p_ressource->p_input, "trying to reuse free vout" );
145 p_vout = p_ressource->p_vout_free;
147 p_ressource->p_vout_free = NULL;
151 assert( p_vout != p_ressource->p_vout_free );
152 TAB_REMOVE( p_ressource->i_vout, p_ressource->pp_vout, p_vout );
156 p_vout = vout_Request( p_ressource->p_input, p_vout, p_fmt );
160 TAB_APPEND( p_ressource->i_vout, p_ressource->pp_vout, p_vout );
166 TAB_REMOVE( p_ressource->i_vout, p_ressource->pp_vout, p_vout );
167 if( p_ressource->p_vout_free )
169 msg_Dbg( p_ressource->p_input, "detroying vout (already one saved)" );
170 vout_CloseAndRelease( p_vout );
174 msg_Dbg( p_ressource->p_input, "saving a free vout" );
175 p_ressource->p_vout_free = p_vout;
180 static vout_thread_t *HoldVout( input_ressource_t *p_ressource )
182 if( p_ressource->i_vout <= 0 )
185 /* TODO FIXME: p_ressource->pp_vout order is NOT stable */
186 vout_thread_t *p_vout = p_ressource->pp_vout[0];
188 vlc_object_hold( p_vout );
194 static void DestroyAout( input_ressource_t *p_ressource )
196 if( p_ressource->p_aout )
197 vlc_object_release( p_ressource->p_aout );
198 p_ressource->p_aout = NULL;
200 static aout_instance_t *RequestAout( input_ressource_t *p_ressource, aout_instance_t *p_aout )
202 assert( p_ressource->p_input );
206 msg_Dbg( p_ressource->p_input, "releasing aout" );
207 vlc_object_release( p_aout );
212 if( !p_ressource->p_aout )
214 msg_Dbg( p_ressource->p_input, "creating aout" );
215 p_ressource->p_aout = aout_New( p_ressource->p_input );
219 msg_Dbg( p_ressource->p_input, "reusing aout" );
222 if( !p_ressource->p_aout )
225 vlc_object_detach( p_ressource->p_aout );
226 vlc_object_attach( p_ressource->p_aout, p_ressource->p_input );
227 vlc_object_hold( p_ressource->p_aout );
228 return p_ressource->p_aout;
233 input_ressource_t *input_ressource_New( void )
235 input_ressource_t *p_ressource = calloc( 1, sizeof(*p_ressource) );
239 vlc_mutex_init( &p_ressource->lock );
243 void input_ressource_Delete( input_ressource_t *p_ressource )
245 DestroySout( p_ressource );
246 DestroyVout( p_ressource );
247 DestroyAout( p_ressource );
249 vlc_mutex_destroy( &p_ressource->lock );
253 void input_ressource_SetInput( input_ressource_t *p_ressource, input_thread_t *p_input )
255 vlc_mutex_lock( &p_ressource->lock );
257 if( p_ressource->p_input && !p_input )
259 if( p_ressource->p_aout )
260 vlc_object_detach( p_ressource->p_aout );
262 assert( p_ressource->i_vout == 0 );
263 if( p_ressource->p_vout_free )
264 vlc_object_detach( p_ressource->p_vout_free );
266 if( p_ressource->p_sout )
267 vlc_object_detach( p_ressource->p_sout );
271 p_ressource->p_input = p_input;
273 vlc_mutex_unlock( &p_ressource->lock );
276 vout_thread_t *input_ressource_RequestVout( input_ressource_t *p_ressource,
277 vout_thread_t *p_vout, video_format_t *p_fmt )
279 vlc_mutex_lock( &p_ressource->lock );
280 vout_thread_t *p_ret = RequestVout( p_ressource, p_vout, p_fmt );
281 vlc_mutex_unlock( &p_ressource->lock );
285 vout_thread_t *input_ressource_HoldVout( input_ressource_t *p_ressource )
287 vlc_mutex_lock( &p_ressource->lock );
288 vout_thread_t *p_ret = HoldVout( p_ressource );
289 vlc_mutex_unlock( &p_ressource->lock );
293 void input_ressource_TerminateVout( input_ressource_t *p_ressource )
295 input_ressource_RequestVout( p_ressource, NULL, NULL );
299 aout_instance_t *input_ressource_RequestAout( input_ressource_t *p_ressource, aout_instance_t *p_aout )
301 vlc_mutex_lock( &p_ressource->lock );
302 aout_instance_t *p_ret = RequestAout( p_ressource, p_aout );
303 vlc_mutex_unlock( &p_ressource->lock );
309 sout_instance_t *input_ressource_RequestSout( input_ressource_t *p_ressource, sout_instance_t *p_sout, const char *psz_sout )
311 vlc_mutex_lock( &p_ressource->lock );
312 sout_instance_t *p_ret = RequestSout( p_ressource, p_sout, psz_sout );
313 vlc_mutex_unlock( &p_ressource->lock );
317 void input_ressource_TerminateSout( input_ressource_t *p_ressource )
319 input_ressource_RequestSout( p_ressource, NULL, NULL );