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 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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_atomic.h>
39 #include "../libvlc.h"
40 #include "../stream_output/stream_output.h"
41 #include "../audio_output/aout_internal.h"
42 #include "../video_output/vout_control.h"
43 #include "input_interface.h"
46 struct input_resource_t
50 vlc_object_t
*p_parent
;
52 /* This lock is used to serialize request and protect
57 input_thread_t
*p_input
;
59 sout_instance_t
*p_sout
;
60 vout_thread_t
*p_vout_free
;
62 /* This lock is used to protect vout resources access (for hold)
63 * It is a special case because of embed video (possible deadlock
64 * between vout window request and vout holds in some(qt4) interface)
66 vlc_mutex_t lock_hold
;
68 /* You need lock+lock_hold to write to the following variables and
69 * only lock or lock_hold to read them */
71 vout_thread_t
**pp_vout
;
75 audio_output_t
*p_aout
;
79 static void DestroySout( input_resource_t
*p_resource
)
82 if( p_resource
->p_sout
)
83 sout_DeleteInstance( p_resource
->p_sout
);
85 p_resource
->p_sout
= NULL
;
88 static sout_instance_t
*RequestSout( input_resource_t
*p_resource
,
89 sout_instance_t
*p_sout
, const char *psz_sout
)
92 if( !p_sout
&& !psz_sout
)
94 if( p_resource
->p_sout
)
95 msg_Dbg( p_resource
->p_sout
, "destroying useless sout" );
96 DestroySout( p_resource
);
100 assert( !p_sout
|| ( !p_resource
->p_sout
&& !psz_sout
) );
102 /* Check the validity of the sout */
103 if( p_resource
->p_sout
&&
104 strcmp( p_resource
->p_sout
->psz_sout
, psz_sout
) )
106 msg_Dbg( p_resource
->p_parent
, "destroying unusable sout" );
107 DestroySout( p_resource
);
112 if( p_resource
->p_sout
)
115 msg_Dbg( p_resource
->p_parent
, "reusing sout" );
116 msg_Dbg( p_resource
->p_parent
, "you probably want to use gather stream_out" );
120 /* Create a new one */
121 p_resource
->p_sout
= sout_NewInstance( p_resource
->p_parent
, psz_sout
);
124 p_sout
= p_resource
->p_sout
;
125 p_resource
->p_sout
= NULL
;
131 p_resource
->p_sout
= p_sout
;
140 static void DestroyVout( input_resource_t
*p_resource
)
142 assert( p_resource
->i_vout
== 0 );
144 if( p_resource
->p_vout_free
)
145 vout_CloseAndRelease( p_resource
->p_vout_free
);
147 p_resource
->p_vout_free
= NULL
;
150 static void DisplayVoutTitle( input_resource_t
*p_resource
,
151 vout_thread_t
*p_vout
)
153 if( p_resource
->p_input
== NULL
)
156 /* TODO display the title only one time for the same input ? */
158 input_item_t
*p_item
= input_GetItem( p_resource
->p_input
);
160 char *psz_nowplaying
= input_item_GetNowPlaying( p_item
);
161 if( psz_nowplaying
&& *psz_nowplaying
)
163 vout_DisplayTitle( p_vout
, psz_nowplaying
);
167 char *psz_artist
= input_item_GetArtist( p_item
);
168 char *psz_name
= input_item_GetTitle( p_item
);
170 if( !psz_name
|| *psz_name
== '\0' )
173 psz_name
= input_item_GetName( p_item
);
175 if( psz_artist
&& *psz_artist
)
178 if( asprintf( &psz_string
, "%s - %s", psz_name
, psz_artist
) != -1 )
180 vout_DisplayTitle( p_vout
, psz_string
);
186 vout_DisplayTitle( p_vout
, psz_name
);
191 free( psz_nowplaying
);
193 static vout_thread_t
*RequestVout( input_resource_t
*p_resource
,
194 vout_thread_t
*p_vout
,
195 video_format_t
*p_fmt
, unsigned dpb_size
,
198 vlc_assert_locked( &p_resource
->lock
);
200 if( !p_vout
&& !p_fmt
)
202 if( p_resource
->p_vout_free
)
204 msg_Dbg( p_resource
->p_vout_free
, "destroying useless vout" );
205 vout_CloseAndRelease( p_resource
->p_vout_free
);
206 p_resource
->p_vout_free
= NULL
;
214 if( !p_vout
&& p_resource
->p_vout_free
)
216 msg_Dbg( p_resource
->p_parent
, "trying to reuse free vout" );
217 p_vout
= p_resource
->p_vout_free
;
219 p_resource
->p_vout_free
= NULL
;
223 assert( p_vout
!= p_resource
->p_vout_free
);
225 vlc_mutex_lock( &p_resource
->lock_hold
);
226 TAB_REMOVE( p_resource
->i_vout
, p_resource
->pp_vout
, p_vout
);
227 vlc_mutex_unlock( &p_resource
->lock_hold
);
231 vout_configuration_t cfg
= {
233 .input
= VLC_OBJECT(p_resource
->p_input
),
236 .dpb_size
= dpb_size
,
238 p_vout
= vout_Request( p_resource
->p_parent
, &cfg
);
242 DisplayVoutTitle( p_resource
, p_vout
);
244 vlc_mutex_lock( &p_resource
->lock_hold
);
245 TAB_APPEND( p_resource
->i_vout
, p_resource
->pp_vout
, p_vout
);
246 vlc_mutex_unlock( &p_resource
->lock_hold
);
254 vlc_mutex_lock( &p_resource
->lock_hold
);
255 TAB_REMOVE( p_resource
->i_vout
, p_resource
->pp_vout
, p_vout
);
256 const int i_vout_active
= p_resource
->i_vout
;
257 vlc_mutex_unlock( &p_resource
->lock_hold
);
259 if( p_resource
->p_vout_free
|| i_vout_active
> 0 || !b_recycle
)
262 msg_Dbg( p_resource
->p_parent
, "detroying vout (already one saved or active)" );
263 vout_CloseAndRelease( p_vout
);
267 msg_Dbg( p_resource
->p_parent
, "saving a free vout" );
268 vout_Flush( p_vout
, 1 );
269 vout_FlushSubpictureChannel( p_vout
, -1 );
271 vout_configuration_t cfg
= {
278 p_resource
->p_vout_free
= vout_Request( p_resource
->p_parent
, &cfg
);
283 static vout_thread_t
*HoldVout( input_resource_t
*p_resource
)
285 /* TODO FIXME: p_resource->pp_vout order is NOT stable */
286 vlc_mutex_lock( &p_resource
->lock_hold
);
288 vout_thread_t
*p_vout
= p_resource
->i_vout
> 0 ? p_resource
->pp_vout
[0] : NULL
;
290 vlc_object_hold( p_vout
);
292 vlc_mutex_unlock( &p_resource
->lock_hold
);
297 static void HoldVouts( input_resource_t
*p_resource
, vout_thread_t
***ppp_vout
,
300 vout_thread_t
**pp_vout
;
305 vlc_mutex_lock( &p_resource
->lock_hold
);
307 if( p_resource
->i_vout
<= 0 )
310 pp_vout
= malloc( p_resource
->i_vout
* sizeof(*pp_vout
) );
315 *pi_vout
= p_resource
->i_vout
;
317 for( int i
= 0; i
< p_resource
->i_vout
; i
++ )
319 pp_vout
[i
] = p_resource
->pp_vout
[i
];
320 vlc_object_hold( pp_vout
[i
] );
324 vlc_mutex_unlock( &p_resource
->lock_hold
);
328 audio_output_t
*input_resource_GetAout( input_resource_t
*p_resource
)
330 audio_output_t
*p_aout
;
332 vlc_mutex_lock( &p_resource
->lock
);
333 if( p_resource
->b_aout_busy
)
335 vlc_mutex_unlock( &p_resource
->lock
);
336 msg_Dbg( p_resource
->p_parent
, "creating extra audio output" );
337 return aout_New( p_resource
->p_parent
);
340 p_aout
= p_resource
->p_aout
;
343 msg_Dbg( p_resource
->p_parent
, "creating audio output" );
344 p_aout
= aout_New( p_resource
->p_parent
);
347 msg_Dbg( p_resource
->p_parent
, "reusing audio output" );
351 vlc_mutex_lock( &p_resource
->lock_hold
);
352 p_resource
->p_aout
= p_aout
;
353 vlc_mutex_unlock( &p_resource
->lock_hold
);
355 p_resource
->b_aout_busy
= true;
356 vlc_object_hold( p_aout
);
358 vlc_mutex_unlock( &p_resource
->lock
);
362 void input_resource_PutAout( input_resource_t
*p_resource
,
363 audio_output_t
*p_aout
)
365 assert( p_aout
!= NULL
);
367 vlc_mutex_lock( &p_resource
->lock
);
368 if( p_aout
== p_resource
->p_aout
)
370 assert( p_resource
->b_aout_busy
);
371 p_resource
->b_aout_busy
= false;
372 msg_Dbg( p_resource
->p_parent
, "keeping audio output" );
373 vlc_object_release( p_aout
);
377 msg_Dbg( p_resource
->p_parent
, "destroying extra audio output" );
378 vlc_mutex_unlock( &p_resource
->lock
);
381 aout_Destroy( p_aout
);
384 audio_output_t
*input_resource_HoldAout( input_resource_t
*p_resource
)
386 audio_output_t
*p_aout
;
388 vlc_mutex_lock( &p_resource
->lock_hold
);
389 p_aout
= p_resource
->p_aout
;
391 vlc_object_hold( p_aout
);
392 vlc_mutex_unlock( &p_resource
->lock_hold
);
397 static void input_resource_TerminateAout( input_resource_t
*p_resource
)
399 audio_output_t
*p_aout
;
401 vlc_mutex_lock( &p_resource
->lock
);
402 vlc_mutex_lock( &p_resource
->lock_hold
);
403 p_aout
= p_resource
->p_aout
;
404 p_resource
->p_aout
= NULL
;
405 vlc_mutex_unlock( &p_resource
->lock_hold
);
406 p_resource
->b_aout_busy
= false;
407 vlc_mutex_unlock( &p_resource
->lock
);
410 aout_Destroy( p_aout
);
414 input_resource_t
*input_resource_New( vlc_object_t
*p_parent
)
416 input_resource_t
*p_resource
= calloc( 1, sizeof(*p_resource
) );
420 atomic_init( &p_resource
->refs
, 1 );
421 p_resource
->p_parent
= p_parent
;
422 vlc_mutex_init( &p_resource
->lock
);
423 vlc_mutex_init( &p_resource
->lock_hold
);
427 void input_resource_Release( input_resource_t
*p_resource
)
429 if( atomic_fetch_sub( &p_resource
->refs
, 1 ) != 1 )
432 DestroySout( p_resource
);
433 DestroyVout( p_resource
);
434 if( p_resource
->p_aout
!= NULL
)
435 aout_Destroy( p_resource
->p_aout
);
437 vlc_mutex_destroy( &p_resource
->lock_hold
);
438 vlc_mutex_destroy( &p_resource
->lock
);
442 input_resource_t
*input_resource_Hold( input_resource_t
*p_resource
)
444 atomic_fetch_add( &p_resource
->refs
, 1 );
448 void input_resource_SetInput( input_resource_t
*p_resource
, input_thread_t
*p_input
)
450 vlc_mutex_lock( &p_resource
->lock
);
452 if( p_resource
->p_input
&& !p_input
)
453 assert( p_resource
->i_vout
== 0 );
456 p_resource
->p_input
= p_input
;
458 vlc_mutex_unlock( &p_resource
->lock
);
461 vout_thread_t
*input_resource_RequestVout( input_resource_t
*p_resource
,
462 vout_thread_t
*p_vout
,
463 video_format_t
*p_fmt
, unsigned dpb_size
,
466 vlc_mutex_lock( &p_resource
->lock
);
467 vout_thread_t
*p_ret
= RequestVout( p_resource
, p_vout
, p_fmt
, dpb_size
, b_recycle
);
468 vlc_mutex_unlock( &p_resource
->lock
);
472 vout_thread_t
*input_resource_HoldVout( input_resource_t
*p_resource
)
474 return HoldVout( p_resource
);
477 void input_resource_HoldVouts( input_resource_t
*p_resource
, vout_thread_t
***ppp_vout
,
480 HoldVouts( p_resource
, ppp_vout
, pi_vout
);
483 void input_resource_TerminateVout( input_resource_t
*p_resource
)
485 input_resource_RequestVout( p_resource
, NULL
, NULL
, 0, false );
487 bool input_resource_HasVout( input_resource_t
*p_resource
)
489 vlc_mutex_lock( &p_resource
->lock
);
490 assert( !p_resource
->p_input
);
491 const bool b_vout
= p_resource
->p_vout_free
!= NULL
;
492 vlc_mutex_unlock( &p_resource
->lock
);
498 sout_instance_t
*input_resource_RequestSout( input_resource_t
*p_resource
, sout_instance_t
*p_sout
, const char *psz_sout
)
500 vlc_mutex_lock( &p_resource
->lock
);
501 sout_instance_t
*p_ret
= RequestSout( p_resource
, p_sout
, psz_sout
);
502 vlc_mutex_unlock( &p_resource
->lock
);
506 void input_resource_TerminateSout( input_resource_t
*p_resource
)
508 input_resource_RequestSout( p_resource
, NULL
, NULL
);
511 void input_resource_Terminate( input_resource_t
*p_resource
)
513 input_resource_TerminateSout( p_resource
);
514 input_resource_TerminateAout( p_resource
);
515 input_resource_TerminateVout( p_resource
);