7b9b1f35b0d721c904c118310b2f7417106a448d
[vlc.git] / src / input / resource.c
1 /*****************************************************************************
2  * resource.c
3  *****************************************************************************
4  * Copyright (C) 2008 Laurent Aimar
5  * $Id$
6  *
7  * Authors: Laurent Aimar < fenrir _AT_ videolan _DOT_ org >
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32 #include <vlc_vout.h>
33 #include <vlc_osd.h>
34 #include <vlc_aout.h>
35 #include <vlc_sout.h>
36 #include "../libvlc.h"
37 #include "../stream_output/stream_output.h"
38 #include "../audio_output/aout_internal.h"
39 #include "../video_output/vout_control.h"
40 #include "input_interface.h"
41 #include "resource.h"
42
43 struct input_resource_t
44 {
45     /* This lock is used to serialize request and protect
46      * our variables */
47     vlc_mutex_t    lock;
48
49     /* */
50     input_thread_t *p_input;
51
52     sout_instance_t *p_sout;
53     vout_thread_t   *p_vout_free;
54
55     /* This lock is used to protect vout resources access (for hold)
56      * It is a special case because of embed video (possible deadlock
57      * between vout window request and vout holds in some(qt4) interface)
58      */
59     vlc_mutex_t    lock_hold;
60
61     /* You need lock+lock_hold to write to the following variables and
62      * only lock or lock_hold to read them */
63
64     int             i_vout;
65     vout_thread_t   **pp_vout;
66
67     aout_instance_t *p_aout;
68 };
69
70 /* */
71 static void DestroySout( input_resource_t *p_resource )
72 {
73 #ifdef ENABLE_SOUT
74     if( p_resource->p_sout )
75         sout_DeleteInstance( p_resource->p_sout );
76 #endif
77     p_resource->p_sout = NULL;
78 }
79
80 static sout_instance_t *DetachSout( input_resource_t *p_resource )
81 {
82     sout_instance_t *p_sout = p_resource->p_sout;
83     p_resource->p_sout = NULL;
84
85     return p_sout;
86 }
87
88 static sout_instance_t *RequestSout( input_resource_t *p_resource,
89                                      sout_instance_t *p_sout, const char *psz_sout )
90 {
91 #ifdef ENABLE_SOUT
92     if( !p_sout && !psz_sout )
93     {
94         if( p_resource->p_sout )
95             msg_Dbg( p_resource->p_sout, "destroying useless sout" );
96         DestroySout( p_resource );
97         return NULL;
98     }
99
100     assert( p_resource->p_input );
101     assert( !p_sout || ( !p_resource->p_sout && !psz_sout ) );
102
103     /* Check the validity of the sout */
104     if( p_resource->p_sout &&
105         strcmp( p_resource->p_sout->psz_sout, psz_sout ) )
106     {
107         msg_Dbg( p_resource->p_input, "destroying unusable sout" );
108         DestroySout( p_resource );
109     }
110
111     if( psz_sout )
112     {
113         if( p_resource->p_sout )
114         {
115             /* Reuse it */
116             msg_Dbg( p_resource->p_input, "reusing sout" );
117             msg_Dbg( p_resource->p_input, "you probably want to use gather stream_out" );
118             vlc_object_attach( p_resource->p_sout, p_resource->p_input );
119         }
120         else
121         {
122             /* Create a new one */
123             p_resource->p_sout = sout_NewInstance( p_resource->p_input, psz_sout );
124         }
125
126         p_sout = p_resource->p_sout;
127         p_resource->p_sout = NULL;
128
129         return p_sout;
130     }
131     else
132     {
133         vlc_object_detach( p_sout );
134         p_resource->p_sout = p_sout;
135
136         return NULL;
137     }
138 #else
139     return NULL;
140 #endif
141 }
142
143 /* */
144 static void DestroyVout( input_resource_t *p_resource )
145 {
146     assert( p_resource->i_vout == 0 );
147
148     if( p_resource->p_vout_free )
149         vout_CloseAndRelease( p_resource->p_vout_free );
150
151     p_resource->p_vout_free = NULL;
152 }
153 static vout_thread_t *DetachVout( input_resource_t *p_resource )
154 {
155     assert( p_resource->i_vout == 0 );
156     vout_thread_t *p_vout = p_resource->p_vout_free;
157     p_resource->p_vout_free = NULL;
158
159     return p_vout;
160 }
161
162 static void DisplayVoutTitle( input_resource_t *p_resource,
163                               vout_thread_t *p_vout )
164 {
165     assert( p_resource->p_input );
166
167     /* TODO display the title only one time for the same input ? */
168
169     input_item_t *p_item = input_GetItem( p_resource->p_input );
170
171     char *psz_nowplaying = input_item_GetNowPlaying( p_item );
172     if( psz_nowplaying && *psz_nowplaying )
173     {
174         vout_DisplayTitle( p_vout, psz_nowplaying );
175     }
176     else
177     {
178         char *psz_artist = input_item_GetArtist( p_item );
179         char *psz_name = input_item_GetTitle( p_item );
180
181         if( !psz_name || *psz_name == '\0' )
182         {
183             free( psz_name );
184             psz_name = input_item_GetName( p_item );
185         }
186         if( psz_artist && *psz_artist )
187         {
188             char *psz_string;
189             if( asprintf( &psz_string, "%s - %s", psz_name, psz_artist ) != -1 )
190             {
191                 vout_DisplayTitle( p_vout, psz_string );
192                 free( psz_string );
193             }
194         }
195         else if( psz_name )
196         {
197             vout_DisplayTitle( p_vout, psz_name );
198         }
199         free( psz_name );
200         free( psz_artist );
201     }
202     free( psz_nowplaying );
203 }
204 static vout_thread_t *RequestVout( input_resource_t *p_resource,
205                                    vout_thread_t *p_vout, video_format_t *p_fmt,
206                                    bool b_recycle )
207 {
208     if( !p_vout && !p_fmt )
209     {
210         if( p_resource->p_vout_free )
211         {
212             msg_Dbg( p_resource->p_vout_free, "destroying useless vout" );
213             vout_CloseAndRelease( p_resource->p_vout_free );
214             p_resource->p_vout_free = NULL;
215         }
216         return NULL;
217     }
218
219     assert( p_resource->p_input );
220     if( p_fmt )
221     {
222         /* */
223         if( !p_vout && p_resource->p_vout_free )
224         {
225             msg_Dbg( p_resource->p_input, "trying to reuse free vout" );
226             p_vout = p_resource->p_vout_free;
227
228             p_resource->p_vout_free = NULL;
229         }
230         else if( p_vout )
231         {
232             assert( p_vout != p_resource->p_vout_free );
233
234             vlc_mutex_lock( &p_resource->lock_hold );
235             TAB_REMOVE( p_resource->i_vout, p_resource->pp_vout, p_vout );
236             vlc_mutex_unlock( &p_resource->lock_hold );
237         }
238
239         /* */
240         p_vout = vout_Request( p_resource->p_input, p_vout, p_fmt );
241         if( !p_vout )
242             return NULL;
243
244         DisplayVoutTitle( p_resource, p_vout );
245
246         vlc_mutex_lock( &p_resource->lock_hold );
247         TAB_APPEND( p_resource->i_vout, p_resource->pp_vout, p_vout );
248         vlc_mutex_unlock( &p_resource->lock_hold );
249
250         return p_vout;
251     }
252     else
253     {
254         assert( p_vout );
255
256         vlc_mutex_lock( &p_resource->lock_hold );
257         TAB_REMOVE( p_resource->i_vout, p_resource->pp_vout, p_vout );
258         const int i_vout_active = p_resource->i_vout;
259         vlc_mutex_unlock( &p_resource->lock_hold );
260
261         if( p_resource->p_vout_free || i_vout_active > 0 || !b_recycle )
262         {
263             if( b_recycle )
264                 msg_Dbg( p_resource->p_input, "detroying vout (already one saved or active)" );
265             vout_CloseAndRelease( p_vout );
266         }
267         else
268         {
269             msg_Dbg( p_resource->p_input, "saving a free vout" );
270             vout_Flush( p_vout, 1 );
271             spu_Control( vout_GetSpu( p_vout ), SPU_CHANNEL_CLEAR, -1 );
272
273             p_resource->p_vout_free = p_vout;
274         }
275         return NULL;
276     }
277 }
278 static vout_thread_t *HoldVout( input_resource_t *p_resource )
279 {
280     /* TODO FIXME: p_resource->pp_vout order is NOT stable */
281     vlc_mutex_lock( &p_resource->lock_hold );
282
283     vout_thread_t *p_vout = p_resource->i_vout > 0 ? p_resource->pp_vout[0] : NULL;
284     if( p_vout )
285         vlc_object_hold( p_vout );
286
287     vlc_mutex_unlock( &p_resource->lock_hold );
288
289     return p_vout;
290 }
291
292 static void HoldVouts( input_resource_t *p_resource, vout_thread_t ***ppp_vout,
293                        size_t *pi_vout )
294 {
295     vout_thread_t **pp_vout;
296
297     *pi_vout = 0;
298     *ppp_vout = NULL;
299
300     vlc_mutex_lock( &p_resource->lock_hold );
301
302     if( p_resource->i_vout <= 0 )
303         goto exit;
304
305     pp_vout = malloc( p_resource->i_vout * sizeof(*pp_vout) );
306     if( !pp_vout )
307         goto exit;
308
309     *ppp_vout = pp_vout;
310     *pi_vout = p_resource->i_vout;
311
312     for( int i = 0; i < p_resource->i_vout; i++ )
313     {
314         pp_vout[i] = p_resource->pp_vout[i];
315         vlc_object_hold( pp_vout[i] );
316     }
317
318 exit:
319     vlc_mutex_unlock( &p_resource->lock_hold );
320 }
321
322 /* */
323 static void DestroyAout( input_resource_t *p_resource )
324 {
325     if( p_resource->p_aout )
326         vlc_object_release( p_resource->p_aout );
327     p_resource->p_aout = NULL;
328 }
329 static aout_instance_t *DetachAout( input_resource_t *p_resource )
330 {
331     vlc_mutex_lock( &p_resource->lock_hold );
332
333     aout_instance_t *p_aout = p_resource->p_aout;
334     p_resource->p_aout = NULL;
335
336     vlc_mutex_unlock( &p_resource->lock_hold );
337
338     return p_aout;
339 }
340
341 static aout_instance_t *RequestAout( input_resource_t *p_resource, aout_instance_t *p_aout )
342 {
343     assert( p_resource->p_input );
344
345     if( p_aout )
346     {
347         msg_Dbg( p_resource->p_input, "releasing aout" );
348         vlc_object_release( p_aout );
349         return NULL;
350     }
351     else
352     {
353         p_aout = p_resource->p_aout;
354         if( !p_aout )
355         {
356             msg_Dbg( p_resource->p_input, "creating aout" );
357             p_aout = aout_New( p_resource->p_input );
358
359             vlc_mutex_lock( &p_resource->lock_hold );
360             p_resource->p_aout = p_aout;
361             vlc_mutex_unlock( &p_resource->lock_hold );
362         }
363         else
364         {
365             msg_Dbg( p_resource->p_input, "reusing aout" );
366         }
367
368         if( !p_aout )
369             return NULL;
370
371         vlc_object_detach( p_aout );
372         vlc_object_attach( p_aout, p_resource->p_input );
373         vlc_object_hold( p_aout );
374         return p_aout;
375     }
376 }
377 static aout_instance_t *HoldAout( input_resource_t *p_resource )
378 {
379     vlc_mutex_lock( &p_resource->lock_hold );
380
381     aout_instance_t *p_aout = p_resource->p_aout;
382     if( p_aout )
383         vlc_object_hold( p_aout );
384
385     vlc_mutex_unlock( &p_resource->lock_hold );
386
387     return p_aout;
388 }
389
390 /* */
391 input_resource_t *input_resource_New( void )
392 {
393     input_resource_t *p_resource = calloc( 1, sizeof(*p_resource) );
394     if( !p_resource )
395         return NULL;
396
397     vlc_mutex_init( &p_resource->lock );
398     vlc_mutex_init( &p_resource->lock_hold );
399     return p_resource;
400 }
401
402 void input_resource_Delete( input_resource_t *p_resource )
403 {
404     DestroySout( p_resource );
405     DestroyVout( p_resource );
406     DestroyAout( p_resource );
407
408     vlc_mutex_destroy( &p_resource->lock_hold );
409     vlc_mutex_destroy( &p_resource->lock );
410     free( p_resource );
411 }
412
413 void input_resource_SetInput( input_resource_t *p_resource, input_thread_t *p_input )
414 {
415     vlc_mutex_lock( &p_resource->lock );
416
417     if( p_resource->p_input && !p_input )
418     {
419         if( p_resource->p_aout )
420             vlc_object_detach( p_resource->p_aout );
421
422         assert( p_resource->i_vout == 0 );
423         if( p_resource->p_vout_free )
424             vlc_object_detach( p_resource->p_vout_free );
425
426         if( p_resource->p_sout )
427             vlc_object_detach( p_resource->p_sout );
428     }
429
430     /* */
431     p_resource->p_input = p_input;
432
433     vlc_mutex_unlock( &p_resource->lock );
434 }
435
436 input_resource_t *input_resource_Detach( input_resource_t *p_resource )
437 {
438     input_resource_t *p_ret = input_resource_New();
439     if( !p_ret )
440         return NULL;
441
442     vlc_mutex_lock( &p_resource->lock );
443     assert( !p_resource->p_input );
444     p_ret->p_sout = DetachSout( p_resource );
445     p_ret->p_vout_free = DetachVout( p_resource );
446     p_ret->p_aout = DetachAout( p_resource );
447     vlc_mutex_unlock( &p_resource->lock );
448
449     return p_ret;
450 }
451
452 vout_thread_t *input_resource_RequestVout( input_resource_t *p_resource,
453                                             vout_thread_t *p_vout, video_format_t *p_fmt, bool b_recycle )
454 {
455     vlc_mutex_lock( &p_resource->lock );
456     vout_thread_t *p_ret = RequestVout( p_resource, p_vout, p_fmt, b_recycle );
457     vlc_mutex_unlock( &p_resource->lock );
458
459     return p_ret;
460 }
461 vout_thread_t *input_resource_HoldVout( input_resource_t *p_resource )
462 {
463     return HoldVout( p_resource );
464 }
465
466 void input_resource_HoldVouts( input_resource_t *p_resource, vout_thread_t ***ppp_vout,
467                                size_t *pi_vout )
468 {
469     HoldVouts( p_resource, ppp_vout, pi_vout );
470 }
471
472 void input_resource_TerminateVout( input_resource_t *p_resource )
473 {
474     input_resource_RequestVout( p_resource, NULL, NULL, false );
475 }
476 bool input_resource_HasVout( input_resource_t *p_resource )
477 {
478     vlc_mutex_lock( &p_resource->lock );
479     assert( !p_resource->p_input );
480     const bool b_vout = p_resource->p_vout_free != NULL;
481     vlc_mutex_unlock( &p_resource->lock );
482
483     return b_vout;
484 }
485
486 /* */
487 aout_instance_t *input_resource_RequestAout( input_resource_t *p_resource, aout_instance_t *p_aout )
488 {
489     vlc_mutex_lock( &p_resource->lock );
490     aout_instance_t *p_ret = RequestAout( p_resource, p_aout );
491     vlc_mutex_unlock( &p_resource->lock );
492
493     return p_ret;
494 }
495 aout_instance_t *input_resource_HoldAout( input_resource_t *p_resource )
496 {
497     return HoldAout( p_resource );
498 }
499 /* */
500 sout_instance_t *input_resource_RequestSout( input_resource_t *p_resource, sout_instance_t *p_sout, const char *psz_sout )
501 {
502     vlc_mutex_lock( &p_resource->lock );
503     sout_instance_t *p_ret = RequestSout( p_resource, p_sout, psz_sout );
504     vlc_mutex_unlock( &p_resource->lock );
505
506     return p_ret;
507 }
508 void input_resource_TerminateSout( input_resource_t *p_resource )
509 {
510     input_resource_RequestSout( p_resource, NULL, NULL );
511 }
512