access: http: only warn on deflate errors
[vlc.git] / src / input / resource.c
blob25f979befbbc11f182d322260f40ba27aeabacb0
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_control.h"
43 #include "input_interface.h"
44 #include "resource.h"
46 struct input_resource_t
48 atomic_uint refs;
50 vlc_object_t *p_parent;
52 /* This lock is used to serialize request and protect
53 * our variables */
54 vlc_mutex_t lock;
56 /* */
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;
72 int i_vout;
74 bool b_aout_busy;
75 audio_output_t *p_aout;
78 /* */
79 static void DestroySout( input_resource_t *p_resource )
81 #ifdef ENABLE_SOUT
82 if( p_resource->p_sout )
83 sout_DeleteInstance( p_resource->p_sout );
84 #endif
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 )
91 #ifdef ENABLE_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 );
97 return NULL;
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 );
110 if( psz_sout )
112 if( p_resource->p_sout )
114 /* Reuse it */
115 msg_Dbg( p_resource->p_parent, "reusing sout" );
116 msg_Dbg( p_resource->p_parent, "you probably want to use gather stream_out" );
118 else
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;
127 return p_sout;
129 else
131 p_resource->p_sout = p_sout;
132 return NULL;
134 #else
135 return NULL;
136 #endif
139 /* */
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 )
154 return;
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 );
165 else
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' )
172 free( psz_name );
173 psz_name = input_item_GetName( p_item );
175 if( psz_artist && *psz_artist )
177 char *psz_string;
178 if( asprintf( &psz_string, "%s - %s", psz_name, psz_artist ) != -1 )
180 vout_DisplayTitle( p_vout, psz_string );
181 free( psz_string );
184 else if( psz_name )
186 vout_DisplayTitle( p_vout, psz_name );
188 free( psz_name );
189 free( psz_artist );
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,
196 bool b_recycle )
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;
208 return NULL;
211 if( p_fmt )
213 /* */
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;
221 else if( p_vout )
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 );
230 /* */
231 vout_configuration_t cfg = {
232 .vout = p_vout,
233 .input = VLC_OBJECT(p_resource->p_input),
234 .change_fmt = true,
235 .fmt = p_fmt,
236 .dpb_size = dpb_size,
238 p_vout = vout_Request( p_resource->p_parent, &cfg );
239 if( !p_vout )
240 return NULL;
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 );
248 return p_vout;
250 else
252 assert( p_vout );
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 )
261 if( b_recycle )
262 msg_Dbg( p_resource->p_parent, "detroying vout (already one saved or active)" );
263 vout_CloseAndRelease( p_vout );
265 else
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 = {
272 .vout = p_vout,
273 .input = NULL,
274 .change_fmt = false,
275 .fmt = NULL,
276 .dpb_size = 0,
278 p_resource->p_vout_free = vout_Request( p_resource->p_parent, &cfg );
280 return NULL;
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;
289 if( p_vout )
290 vlc_object_hold( p_vout );
292 vlc_mutex_unlock( &p_resource->lock_hold );
294 return p_vout;
297 static void HoldVouts( input_resource_t *p_resource, vout_thread_t ***ppp_vout,
298 size_t *pi_vout )
300 vout_thread_t **pp_vout;
302 *pi_vout = 0;
303 *ppp_vout = NULL;
305 vlc_mutex_lock( &p_resource->lock_hold );
307 if( p_resource->i_vout <= 0 )
308 goto exit;
310 pp_vout = malloc( p_resource->i_vout * sizeof(*pp_vout) );
311 if( !pp_vout )
312 goto exit;
314 *ppp_vout = 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] );
323 exit:
324 vlc_mutex_unlock( &p_resource->lock_hold );
327 /* Audio output */
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;
341 if( p_aout == NULL )
343 msg_Dbg( p_resource->p_parent, "creating audio output" );
344 p_aout = aout_New( p_resource->p_parent );
346 else
347 msg_Dbg( p_resource->p_parent, "reusing audio output" );
349 if( p_aout != NULL )
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 );
359 return p_aout;
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 );
374 p_aout = NULL;
376 else
377 msg_Dbg( p_resource->p_parent, "destroying extra audio output" );
378 vlc_mutex_unlock( &p_resource->lock );
380 if( p_aout != NULL )
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;
390 if( p_aout )
391 vlc_object_hold( p_aout );
392 vlc_mutex_unlock( &p_resource->lock_hold );
394 return p_aout;
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 );
409 if( p_aout != NULL )
410 aout_Destroy( p_aout );
413 /* Common */
414 input_resource_t *input_resource_New( vlc_object_t *p_parent )
416 input_resource_t *p_resource = calloc( 1, sizeof(*p_resource) );
417 if( !p_resource )
418 return NULL;
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 );
424 return p_resource;
427 void input_resource_Release( input_resource_t *p_resource )
429 if( atomic_fetch_sub( &p_resource->refs, 1 ) != 1 )
430 return;
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 );
439 free( p_resource );
442 input_resource_t *input_resource_Hold( input_resource_t *p_resource )
444 atomic_fetch_add( &p_resource->refs, 1 );
445 return p_resource;
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 );
455 /* */
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,
464 bool b_recycle )
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 );
470 return p_ret;
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,
478 size_t *pi_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 );
494 return b_vout;
497 /* */
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 );
504 return p_ret;
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 );