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_internal.h"
43 #include "input_interface.h"
47 struct input_resource_t
51 vlc_object_t
*p_parent
;
53 /* This lock is used to serialize request and protect
58 input_thread_t
*p_input
;
60 sout_instance_t
*p_sout
;
61 vout_thread_t
*p_vout_free
;
63 /* This lock is used to protect vout resources access (for hold)
64 * It is a special case because of embed video (possible deadlock
65 * between vout window request and vout holds in some(qt) interface)
67 vlc_mutex_t lock_hold
;
69 /* You need lock+lock_hold to write to the following variables and
70 * only lock or lock_hold to read them */
72 vout_thread_t
**pp_vout
;
76 audio_output_t
*p_aout
;
80 static void DestroySout( input_resource_t
*p_resource
)
83 if( p_resource
->p_sout
)
84 sout_DeleteInstance( p_resource
->p_sout
);
86 p_resource
->p_sout
= NULL
;
89 static sout_instance_t
*RequestSout( input_resource_t
*p_resource
,
90 sout_instance_t
*p_sout
, const char *psz_sout
)
93 if( !p_sout
&& !psz_sout
)
95 if( p_resource
->p_sout
)
97 msg_Dbg( p_resource
->p_sout
, "destroying useless sout" );
98 DestroySout( p_resource
);
103 assert( !p_sout
|| ( !p_resource
->p_sout
&& !psz_sout
) );
105 /* Check the validity of the sout */
106 if( p_resource
->p_sout
&&
107 strcmp( p_resource
->p_sout
->psz_sout
, psz_sout
) )
109 msg_Dbg( p_resource
->p_parent
, "destroying unusable sout" );
110 DestroySout( p_resource
);
115 if( p_resource
->p_sout
)
118 msg_Dbg( p_resource
->p_parent
, "reusing sout" );
119 msg_Dbg( p_resource
->p_parent
, "you probably want to use gather stream_out" );
123 /* Create a new one */
124 p_resource
->p_sout
= sout_NewInstance( p_resource
->p_parent
, psz_sout
);
127 p_sout
= p_resource
->p_sout
;
128 p_resource
->p_sout
= NULL
;
134 p_resource
->p_sout
= p_sout
;
138 VLC_UNUSED (p_resource
); VLC_UNUSED (p_sout
); VLC_UNUSED (psz_sout
);
144 static void DestroyVout( input_resource_t
*p_resource
)
146 assert( p_resource
->i_vout
== 0 );
148 if( p_resource
->p_vout_free
)
149 vout_CloseAndRelease( p_resource
->p_vout_free
);
151 p_resource
->p_vout_free
= NULL
;
154 static void DisplayVoutTitle( input_resource_t
*p_resource
,
155 vout_thread_t
*p_vout
)
157 if( p_resource
->p_input
== NULL
)
160 /* TODO display the title only one time for the same input ? */
162 input_item_t
*p_item
= input_GetItem( p_resource
->p_input
);
164 char *psz_nowplaying
= input_item_GetNowPlayingFb( p_item
);
165 if( psz_nowplaying
&& *psz_nowplaying
)
167 vout_DisplayTitle( p_vout
, psz_nowplaying
);
171 char *psz_artist
= input_item_GetArtist( p_item
);
172 char *psz_name
= input_item_GetTitle( p_item
);
174 if( !psz_name
|| *psz_name
== '\0' )
177 psz_name
= input_item_GetName( p_item
);
179 if( psz_artist
&& *psz_artist
)
182 if( asprintf( &psz_string
, "%s - %s", psz_name
, psz_artist
) != -1 )
184 vout_DisplayTitle( p_vout
, psz_string
);
190 vout_DisplayTitle( p_vout
, psz_name
);
195 free( psz_nowplaying
);
197 static vout_thread_t
*RequestVout( input_resource_t
*p_resource
,
198 const vout_configuration_t
*req_cfg
,
201 vlc_mutex_assert( &p_resource
->lock
);
205 if( p_resource
->p_vout_free
)
207 msg_Dbg( p_resource
->p_vout_free
, "destroying useless vout" );
208 vout_CloseAndRelease( p_resource
->p_vout_free
);
209 p_resource
->p_vout_free
= NULL
;
213 vout_configuration_t cfg
= *req_cfg
;
218 if( !cfg
.vout
&& p_resource
->p_vout_free
)
220 msg_Dbg( p_resource
->p_parent
, "trying to reuse free vout" );
221 cfg
.vout
= p_resource
->p_vout_free
;
223 p_resource
->p_vout_free
= NULL
;
227 assert( cfg
.vout
!= p_resource
->p_vout_free
);
229 vlc_mutex_lock( &p_resource
->lock_hold
);
230 TAB_REMOVE( p_resource
->i_vout
, p_resource
->pp_vout
, cfg
.vout
);
231 vlc_mutex_unlock( &p_resource
->lock_hold
);
235 vout_thread_t
*p_vout
= vout_Request( p_resource
->p_parent
, &cfg
,
236 p_resource
->p_input
);
240 DisplayVoutTitle( p_resource
, p_vout
);
242 /* Send original viewpoint to the input in order to update other ESes */
243 if( p_resource
->p_input
!= NULL
)
244 input_Control( p_resource
->p_input
, INPUT_SET_INITIAL_VIEWPOINT
,
247 vlc_mutex_lock( &p_resource
->lock_hold
);
248 TAB_APPEND( p_resource
->i_vout
, p_resource
->pp_vout
, p_vout
);
249 vlc_mutex_unlock( &p_resource
->lock_hold
);
251 if( p_resource
->p_input
)
252 input_SendEventVout( p_resource
->p_input
,
253 &(struct vlc_input_event_vout
) {
254 .action
= VLC_INPUT_EVENT_VOUT_ADDED
,
264 vlc_mutex_lock( &p_resource
->lock_hold
);
265 TAB_REMOVE( p_resource
->i_vout
, p_resource
->pp_vout
, cfg
.vout
);
266 const int i_vout_active
= p_resource
->i_vout
;
267 vlc_mutex_unlock( &p_resource
->lock_hold
);
269 if( p_resource
->p_input
)
270 input_SendEventVout( p_resource
->p_input
,
271 &(struct vlc_input_event_vout
) {
272 .action
= VLC_INPUT_EVENT_VOUT_DELETED
,
276 if( p_resource
->p_vout_free
|| i_vout_active
> 0 || !b_recycle
)
279 msg_Dbg( p_resource
->p_parent
, "destroying vout (already one saved or active)" );
280 vout_CloseAndRelease( cfg
.vout
);
284 msg_Dbg( p_resource
->p_parent
, "saving a free vout" );
285 vout_FlushAll( cfg
.vout
);
286 vout_FlushSubpictureChannel( cfg
.vout
, -1 );
288 p_resource
->p_vout_free
= vout_Request( p_resource
->p_parent
, &cfg
,
294 static vout_thread_t
*HoldVout( input_resource_t
*p_resource
)
296 /* TODO FIXME: p_resource->pp_vout order is NOT stable */
297 vlc_mutex_lock( &p_resource
->lock_hold
);
299 vout_thread_t
*p_vout
= p_resource
->i_vout
> 0 ? p_resource
->pp_vout
[0] : NULL
;
301 vlc_object_hold( p_vout
);
303 vlc_mutex_unlock( &p_resource
->lock_hold
);
308 static void HoldVouts( input_resource_t
*p_resource
, vout_thread_t
***ppp_vout
,
311 vout_thread_t
**pp_vout
;
316 vlc_mutex_lock( &p_resource
->lock_hold
);
318 if( p_resource
->i_vout
<= 0 )
321 pp_vout
= vlc_alloc( p_resource
->i_vout
, sizeof(*pp_vout
) );
326 *pi_vout
= p_resource
->i_vout
;
328 for( int i
= 0; i
< p_resource
->i_vout
; i
++ )
330 pp_vout
[i
] = p_resource
->pp_vout
[i
];
331 vlc_object_hold( pp_vout
[i
] );
335 vlc_mutex_unlock( &p_resource
->lock_hold
);
339 audio_output_t
*input_resource_GetAout( input_resource_t
*p_resource
)
341 audio_output_t
*p_aout
;
343 vlc_mutex_lock( &p_resource
->lock_hold
);
344 p_aout
= p_resource
->p_aout
;
346 if( p_aout
== NULL
|| p_resource
->b_aout_busy
)
348 msg_Dbg( p_resource
->p_parent
, "creating audio output" );
349 vlc_mutex_unlock( &p_resource
->lock_hold
);
351 p_aout
= aout_New( p_resource
->p_parent
);
353 return NULL
; /* failed */
355 vlc_mutex_lock( &p_resource
->lock_hold
);
356 if( p_resource
->p_aout
== NULL
)
357 p_resource
->p_aout
= p_aout
;
360 msg_Dbg( p_resource
->p_parent
, "reusing audio output" );
362 if( p_resource
->p_aout
== p_aout
)
364 assert( !p_resource
->b_aout_busy
);
365 p_resource
->b_aout_busy
= true;
367 vlc_mutex_unlock( &p_resource
->lock_hold
);
371 void input_resource_PutAout( input_resource_t
*p_resource
,
372 audio_output_t
*p_aout
)
374 assert( p_aout
!= NULL
);
376 vlc_mutex_lock( &p_resource
->lock_hold
);
377 if( p_aout
== p_resource
->p_aout
)
379 assert( p_resource
->b_aout_busy
);
380 p_resource
->b_aout_busy
= false;
381 msg_Dbg( p_resource
->p_parent
, "keeping audio output" );
385 msg_Dbg( p_resource
->p_parent
, "destroying extra audio output" );
386 vlc_mutex_unlock( &p_resource
->lock_hold
);
389 aout_Destroy( p_aout
);
392 audio_output_t
*input_resource_HoldAout( input_resource_t
*p_resource
)
394 audio_output_t
*p_aout
;
396 vlc_mutex_lock( &p_resource
->lock_hold
);
397 p_aout
= p_resource
->p_aout
;
399 vlc_object_hold( p_aout
);
400 vlc_mutex_unlock( &p_resource
->lock_hold
);
405 void input_resource_ResetAout( input_resource_t
*p_resource
)
407 audio_output_t
*p_aout
= NULL
;
409 vlc_mutex_lock( &p_resource
->lock_hold
);
410 if( !p_resource
->b_aout_busy
)
411 p_aout
= p_resource
->p_aout
;
413 p_resource
->p_aout
= NULL
;
414 p_resource
->b_aout_busy
= false;
415 vlc_mutex_unlock( &p_resource
->lock_hold
);
418 aout_Destroy( p_aout
);
422 input_resource_t
*input_resource_New( vlc_object_t
*p_parent
)
424 input_resource_t
*p_resource
= calloc( 1, sizeof(*p_resource
) );
428 vlc_atomic_rc_init( &p_resource
->rc
);
429 p_resource
->p_parent
= p_parent
;
430 vlc_mutex_init( &p_resource
->lock
);
431 vlc_mutex_init( &p_resource
->lock_hold
);
435 void input_resource_Release( input_resource_t
*p_resource
)
437 if( !vlc_atomic_rc_dec( &p_resource
->rc
) )
440 DestroySout( p_resource
);
441 DestroyVout( p_resource
);
442 if( p_resource
->p_aout
!= NULL
)
443 aout_Destroy( p_resource
->p_aout
);
445 vlc_mutex_destroy( &p_resource
->lock_hold
);
446 vlc_mutex_destroy( &p_resource
->lock
);
450 input_resource_t
*input_resource_Hold( input_resource_t
*p_resource
)
452 vlc_atomic_rc_inc( &p_resource
->rc
);
456 void input_resource_SetInput( input_resource_t
*p_resource
, input_thread_t
*p_input
)
458 vlc_mutex_lock( &p_resource
->lock
);
460 if( p_resource
->p_input
&& !p_input
)
461 assert( p_resource
->i_vout
== 0 );
464 p_resource
->p_input
= p_input
;
466 vlc_mutex_unlock( &p_resource
->lock
);
469 vout_thread_t
*input_resource_RequestVout( input_resource_t
*p_resource
,
470 const vout_configuration_t
*cfg
,
473 vlc_mutex_lock( &p_resource
->lock
);
474 vout_thread_t
*p_ret
= RequestVout( p_resource
, cfg
, b_recycle
);
475 vlc_mutex_unlock( &p_resource
->lock
);
479 vout_thread_t
*input_resource_HoldVout( input_resource_t
*p_resource
)
481 return HoldVout( p_resource
);
484 void input_resource_HoldVouts( input_resource_t
*p_resource
, vout_thread_t
***ppp_vout
,
487 HoldVouts( p_resource
, ppp_vout
, pi_vout
);
490 void input_resource_TerminateVout( input_resource_t
*p_resource
)
492 input_resource_RequestVout( p_resource
, NULL
, false );
494 bool input_resource_HasVout( input_resource_t
*p_resource
)
496 vlc_mutex_lock( &p_resource
->lock
);
497 assert( !p_resource
->p_input
);
498 const bool b_vout
= p_resource
->p_vout_free
!= NULL
;
499 vlc_mutex_unlock( &p_resource
->lock
);
505 sout_instance_t
*input_resource_RequestSout( input_resource_t
*p_resource
, sout_instance_t
*p_sout
, const char *psz_sout
)
507 vlc_mutex_lock( &p_resource
->lock
);
508 sout_instance_t
*p_ret
= RequestSout( p_resource
, p_sout
, psz_sout
);
509 vlc_mutex_unlock( &p_resource
->lock
);
513 void input_resource_TerminateSout( input_resource_t
*p_resource
)
515 input_resource_RequestSout( p_resource
, NULL
, NULL
);
518 void input_resource_Terminate( input_resource_t
*p_resource
)
520 input_resource_TerminateSout( p_resource
);
521 input_resource_ResetAout( p_resource
);
522 input_resource_TerminateVout( p_resource
);