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 );
192 static void HoldVouts( input_ressource_t *p_ressource, vout_thread_t ***ppp_vout, int *pi_vout )
194 vout_thread_t **pp_vout;
198 if( p_ressource->i_vout <= 0 )
201 pp_vout = calloc( p_ressource->i_vout, sizeof(*pp_vout) );
206 *pi_vout = p_ressource->i_vout;
208 for( int i = 0; i < p_ressource->i_vout; i++ )
210 pp_vout[i] = p_ressource->pp_vout[i];
211 vlc_object_hold( pp_vout[i] );
216 static void DestroyAout( input_ressource_t *p_ressource )
218 if( p_ressource->p_aout )
219 vlc_object_release( p_ressource->p_aout );
220 p_ressource->p_aout = NULL;
222 static aout_instance_t *RequestAout( input_ressource_t *p_ressource, aout_instance_t *p_aout )
224 assert( p_ressource->p_input );
228 msg_Dbg( p_ressource->p_input, "releasing aout" );
229 vlc_object_release( p_aout );
234 if( !p_ressource->p_aout )
236 msg_Dbg( p_ressource->p_input, "creating aout" );
237 p_ressource->p_aout = aout_New( p_ressource->p_input );
241 msg_Dbg( p_ressource->p_input, "reusing aout" );
244 if( !p_ressource->p_aout )
247 vlc_object_detach( p_ressource->p_aout );
248 vlc_object_attach( p_ressource->p_aout, p_ressource->p_input );
249 vlc_object_hold( p_ressource->p_aout );
250 return p_ressource->p_aout;
253 static aout_instance_t *HoldAout( input_ressource_t *p_ressource )
255 if( !p_ressource->p_aout )
258 /* TODO FIXME: p_ressource->pp_vout order is NOT stable */
259 aout_instance_t *p_aout = p_ressource->p_aout;
261 vlc_object_hold( p_aout );
266 input_ressource_t *input_ressource_New( void )
268 input_ressource_t *p_ressource = calloc( 1, sizeof(*p_ressource) );
272 vlc_mutex_init( &p_ressource->lock );
276 void input_ressource_Delete( input_ressource_t *p_ressource )
278 DestroySout( p_ressource );
279 DestroyVout( p_ressource );
280 DestroyAout( p_ressource );
282 vlc_mutex_destroy( &p_ressource->lock );
286 void input_ressource_SetInput( input_ressource_t *p_ressource, input_thread_t *p_input )
288 vlc_mutex_lock( &p_ressource->lock );
290 if( p_ressource->p_input && !p_input )
292 if( p_ressource->p_aout )
293 vlc_object_detach( p_ressource->p_aout );
295 assert( p_ressource->i_vout == 0 );
296 if( p_ressource->p_vout_free )
297 vlc_object_detach( p_ressource->p_vout_free );
299 if( p_ressource->p_sout )
300 vlc_object_detach( p_ressource->p_sout );
304 p_ressource->p_input = p_input;
306 vlc_mutex_unlock( &p_ressource->lock );
309 vout_thread_t *input_ressource_RequestVout( input_ressource_t *p_ressource,
310 vout_thread_t *p_vout, video_format_t *p_fmt )
312 vlc_mutex_lock( &p_ressource->lock );
313 vout_thread_t *p_ret = RequestVout( p_ressource, p_vout, p_fmt );
314 vlc_mutex_unlock( &p_ressource->lock );
318 vout_thread_t *input_ressource_HoldVout( input_ressource_t *p_ressource )
320 vlc_mutex_lock( &p_ressource->lock );
321 vout_thread_t *p_ret = HoldVout( p_ressource );
322 vlc_mutex_unlock( &p_ressource->lock );
326 void input_ressource_HoldVouts( input_ressource_t *p_ressource, vout_thread_t ***ppp_vout, int *pi_vout )
328 vlc_mutex_lock( &p_ressource->lock );
329 HoldVouts( p_ressource, ppp_vout, pi_vout );
330 vlc_mutex_unlock( &p_ressource->lock );
332 void input_ressource_TerminateVout( input_ressource_t *p_ressource )
334 input_ressource_RequestVout( p_ressource, NULL, NULL );
338 aout_instance_t *input_ressource_RequestAout( input_ressource_t *p_ressource, aout_instance_t *p_aout )
340 vlc_mutex_lock( &p_ressource->lock );
341 aout_instance_t *p_ret = RequestAout( p_ressource, p_aout );
342 vlc_mutex_unlock( &p_ressource->lock );
346 aout_instance_t *input_ressource_HoldAout( input_ressource_t *p_ressource )
348 vlc_mutex_lock( &p_ressource->lock );
349 aout_instance_t *p_ret = HoldAout( p_ressource );
350 vlc_mutex_unlock( &p_ressource->lock );
355 sout_instance_t *input_ressource_RequestSout( input_ressource_t *p_ressource, sout_instance_t *p_sout, const char *psz_sout )
357 vlc_mutex_lock( &p_ressource->lock );
358 sout_instance_t *p_ret = RequestSout( p_ressource, p_sout, psz_sout );
359 vlc_mutex_unlock( &p_ressource->lock );
363 void input_ressource_TerminateSout( input_ressource_t *p_ressource )
365 input_ressource_RequestSout( p_ressource, NULL, NULL );