demux: heif: refactor pic setup
[vlc.git] / src / input / resource.c
blob70cfa2dee20406b7fa8a51ea3a2fbfa9acc6540e
1 /*****************************************************************************
2 * resource.c
3 *****************************************************************************
4 * Copyright (C) 2008 Laurent Aimar
5 * $Id$
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 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #include <assert.h>
33 #include <vlc_common.h>
34 #include <vlc_atomic.h>
35 #include <vlc_vout.h>
36 #include <vlc_spu.h>
37 #include <vlc_aout.h>
38 #include <vlc_sout.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"
44 #include "event.h"
45 #include "resource.h"
47 struct input_resource_t
49 vlc_atomic_rc_t rc;
51 vlc_object_t *p_parent;
53 /* This lock is used to serialize request and protect
54 * our variables */
55 vlc_mutex_t lock;
57 /* */
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;
73 int i_vout;
75 bool b_aout_busy;
76 audio_output_t *p_aout;
79 /* */
80 static void DestroySout( input_resource_t *p_resource )
82 #ifdef ENABLE_SOUT
83 if( p_resource->p_sout )
84 sout_DeleteInstance( p_resource->p_sout );
85 #endif
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 )
92 #ifdef ENABLE_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 );
100 return NULL;
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 );
113 if( psz_sout )
115 if( p_resource->p_sout )
117 /* Reuse it */
118 msg_Dbg( p_resource->p_parent, "reusing sout" );
119 msg_Dbg( p_resource->p_parent, "you probably want to use gather stream_out" );
121 else
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;
130 return p_sout;
132 else
134 p_resource->p_sout = p_sout;
135 return NULL;
137 #else
138 VLC_UNUSED (p_resource); VLC_UNUSED (p_sout); VLC_UNUSED (psz_sout);
139 return NULL;
140 #endif
143 /* */
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 )
158 return;
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 );
169 else
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' )
176 free( psz_name );
177 psz_name = input_item_GetName( p_item );
179 if( psz_artist && *psz_artist )
181 char *psz_string;
182 if( asprintf( &psz_string, "%s - %s", psz_name, psz_artist ) != -1 )
184 vout_DisplayTitle( p_vout, psz_string );
185 free( psz_string );
188 else if( psz_name )
190 vout_DisplayTitle( p_vout, psz_name );
192 free( psz_name );
193 free( psz_artist );
195 free( psz_nowplaying );
197 static vout_thread_t *RequestVout( input_resource_t *p_resource,
198 const vout_configuration_t *req_cfg,
199 bool b_recycle )
201 vlc_mutex_assert( &p_resource->lock );
203 if( !req_cfg )
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;
211 return NULL;
213 vout_configuration_t cfg = *req_cfg;
215 if( cfg.fmt )
217 /* */
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;
225 else if( cfg.vout )
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 );
234 /* */
235 vout_thread_t *p_vout = vout_Request( p_resource->p_parent, &cfg,
236 p_resource->p_input );
237 if( !p_vout )
238 return NULL;
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,
245 &cfg.fmt->pose );
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,
255 .vout = p_vout,
258 return p_vout;
260 else
262 assert( cfg.vout );
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,
273 .vout = cfg.vout,
276 if( p_resource->p_vout_free || i_vout_active > 0 || !b_recycle )
278 if( b_recycle )
279 msg_Dbg( p_resource->p_parent, "destroying vout (already one saved or active)" );
280 vout_CloseAndRelease( cfg.vout );
282 else
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,
289 NULL );
291 return NULL;
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;
300 if( p_vout )
301 vlc_object_hold( p_vout );
303 vlc_mutex_unlock( &p_resource->lock_hold );
305 return p_vout;
308 static void HoldVouts( input_resource_t *p_resource, vout_thread_t ***ppp_vout,
309 size_t *pi_vout )
311 vout_thread_t **pp_vout;
313 *pi_vout = 0;
314 *ppp_vout = NULL;
316 vlc_mutex_lock( &p_resource->lock_hold );
318 if( p_resource->i_vout <= 0 )
319 goto exit;
321 pp_vout = vlc_alloc( p_resource->i_vout, sizeof(*pp_vout) );
322 if( !pp_vout )
323 goto exit;
325 *ppp_vout = 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] );
334 exit:
335 vlc_mutex_unlock( &p_resource->lock_hold );
338 /* Audio output */
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 );
352 if( p_aout == NULL )
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;
359 else
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 );
368 return p_aout;
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" );
382 p_aout = NULL;
384 else
385 msg_Dbg( p_resource->p_parent, "destroying extra audio output" );
386 vlc_mutex_unlock( &p_resource->lock_hold );
388 if( p_aout != NULL )
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;
398 if( p_aout != NULL )
399 vlc_object_hold( p_aout );
400 vlc_mutex_unlock( &p_resource->lock_hold );
402 return p_aout;
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 );
417 if( p_aout != NULL )
418 aout_Destroy( p_aout );
421 /* Common */
422 input_resource_t *input_resource_New( vlc_object_t *p_parent )
424 input_resource_t *p_resource = calloc( 1, sizeof(*p_resource) );
425 if( !p_resource )
426 return NULL;
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 );
432 return p_resource;
435 void input_resource_Release( input_resource_t *p_resource )
437 if( !vlc_atomic_rc_dec( &p_resource->rc ) )
438 return;
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 );
447 free( p_resource );
450 input_resource_t *input_resource_Hold( input_resource_t *p_resource )
452 vlc_atomic_rc_inc( &p_resource->rc );
453 return p_resource;
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 );
463 /* */
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,
471 bool b_recycle )
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 );
477 return p_ret;
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,
485 size_t *pi_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 );
501 return b_vout;
504 /* */
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 );
511 return p_ret;
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 );