Really used a weaker lock for input_ressource_HoldAout.
[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 static void HoldVouts( input_resource_t *p_resource, vout_thread_t ***ppp_vout, int *pi_vout )
292 {
293     vout_thread_t **pp_vout;
294
295     *pi_vout = 0;
296     *ppp_vout = NULL;
297
298     vlc_mutex_lock( &p_resource->lock_hold );
299
300     if( p_resource->i_vout <= 0 )
301         goto exit;
302
303     pp_vout = calloc( p_resource->i_vout, sizeof(*pp_vout) );
304     if( !pp_vout )
305         goto exit;
306
307     *ppp_vout = pp_vout;
308     *pi_vout = p_resource->i_vout;
309
310     for( int i = 0; i < p_resource->i_vout; i++ )
311     {
312         pp_vout[i] = p_resource->pp_vout[i];
313         vlc_object_hold( pp_vout[i] );
314     }
315
316 exit:
317     vlc_mutex_unlock( &p_resource->lock_hold );
318 }
319
320 /* */
321 static void DestroyAout( input_resource_t *p_resource )
322 {
323     if( p_resource->p_aout )
324         vlc_object_release( p_resource->p_aout );
325     p_resource->p_aout = NULL;
326 }
327 static aout_instance_t *DetachAout( input_resource_t *p_resource )
328 {
329     vlc_mutex_lock( &p_resource->lock_hold );
330
331     aout_instance_t *p_aout = p_resource->p_aout;
332     p_resource->p_aout = NULL;
333
334     vlc_mutex_unlock( &p_resource->lock_hold );
335
336     return p_aout;
337 }
338
339 static aout_instance_t *RequestAout( input_resource_t *p_resource, aout_instance_t *p_aout )
340 {
341     assert( p_resource->p_input );
342
343     if( p_aout )
344     {
345         msg_Dbg( p_resource->p_input, "releasing aout" );
346         vlc_object_release( p_aout );
347         return NULL;
348     }
349     else
350     {
351         p_aout = p_resource->p_aout;
352         if( !p_aout )
353         {
354             msg_Dbg( p_resource->p_input, "creating aout" );
355             p_aout = aout_New( p_resource->p_input );
356
357             vlc_mutex_lock( &p_resource->lock_hold );
358             p_resource->p_aout = p_aout;
359             vlc_mutex_unlock( &p_resource->lock_hold );
360         }
361         else
362         {
363             msg_Dbg( p_resource->p_input, "reusing aout" );
364         }
365
366         if( !p_aout )
367             return NULL;
368
369         vlc_object_detach( p_aout );
370         vlc_object_attach( p_aout, p_resource->p_input );
371         vlc_object_hold( p_aout );
372         return p_aout;
373     }
374 }
375 static aout_instance_t *HoldAout( input_resource_t *p_resource )
376 {
377     vlc_mutex_lock( &p_resource->lock_hold );
378
379     aout_instance_t *p_aout = p_resource->p_aout;
380     if( p_aout )
381         vlc_object_hold( p_aout );
382
383     vlc_mutex_unlock( &p_resource->lock_hold );
384
385     return p_aout;
386 }
387
388 /* */
389 input_resource_t *input_resource_New( void )
390 {
391     input_resource_t *p_resource = calloc( 1, sizeof(*p_resource) );
392     if( !p_resource )
393         return NULL;
394
395     vlc_mutex_init( &p_resource->lock );
396     vlc_mutex_init( &p_resource->lock_hold );
397     return p_resource;
398 }
399
400 void input_resource_Delete( input_resource_t *p_resource )
401 {
402     DestroySout( p_resource );
403     DestroyVout( p_resource );
404     DestroyAout( p_resource );
405
406     vlc_mutex_destroy( &p_resource->lock_hold );
407     vlc_mutex_destroy( &p_resource->lock );
408     free( p_resource );
409 }
410
411 void input_resource_SetInput( input_resource_t *p_resource, input_thread_t *p_input )
412 {
413     vlc_mutex_lock( &p_resource->lock );
414
415     if( p_resource->p_input && !p_input )
416     {
417         if( p_resource->p_aout )
418             vlc_object_detach( p_resource->p_aout );
419
420         assert( p_resource->i_vout == 0 );
421         if( p_resource->p_vout_free )
422             vlc_object_detach( p_resource->p_vout_free );
423
424         if( p_resource->p_sout )
425             vlc_object_detach( p_resource->p_sout );
426     }
427
428     /* */
429     p_resource->p_input = p_input;
430
431     vlc_mutex_unlock( &p_resource->lock );
432 }
433
434 input_resource_t *input_resource_Detach( input_resource_t *p_resource )
435 {
436     input_resource_t *p_ret = input_resource_New();
437     if( !p_ret )
438         return NULL;
439
440     vlc_mutex_lock( &p_resource->lock );
441     assert( !p_resource->p_input );
442     p_ret->p_sout = DetachSout( p_resource );
443     p_ret->p_vout_free = DetachVout( p_resource );
444     p_ret->p_aout = DetachAout( p_resource );
445     vlc_mutex_unlock( &p_resource->lock );
446
447     return p_ret;
448 }
449
450 vout_thread_t *input_resource_RequestVout( input_resource_t *p_resource,
451                                             vout_thread_t *p_vout, video_format_t *p_fmt, bool b_recycle )
452 {
453     vlc_mutex_lock( &p_resource->lock );
454     vout_thread_t *p_ret = RequestVout( p_resource, p_vout, p_fmt, b_recycle );
455     vlc_mutex_unlock( &p_resource->lock );
456
457     return p_ret;
458 }
459 vout_thread_t *input_resource_HoldVout( input_resource_t *p_resource )
460 {
461     return HoldVout( p_resource );
462 }
463 void input_resource_HoldVouts( input_resource_t *p_resource, vout_thread_t ***ppp_vout, int *pi_vout )
464 {
465     HoldVouts( p_resource, ppp_vout, pi_vout );
466 }
467 void input_resource_TerminateVout( input_resource_t *p_resource )
468 {
469     input_resource_RequestVout( p_resource, NULL, NULL, false );
470 }
471 bool input_resource_HasVout( input_resource_t *p_resource )
472 {
473     vlc_mutex_lock( &p_resource->lock );
474     assert( !p_resource->p_input );
475     const bool b_vout = p_resource->p_vout_free != NULL;
476     vlc_mutex_unlock( &p_resource->lock );
477
478     return b_vout;
479 }
480
481 /* */
482 aout_instance_t *input_resource_RequestAout( input_resource_t *p_resource, aout_instance_t *p_aout )
483 {
484     vlc_mutex_lock( &p_resource->lock );
485     aout_instance_t *p_ret = RequestAout( p_resource, p_aout );
486     vlc_mutex_unlock( &p_resource->lock );
487
488     return p_ret;
489 }
490 aout_instance_t *input_resource_HoldAout( input_resource_t *p_resource )
491 {
492     return HoldAout( p_resource );
493 }
494 /* */
495 sout_instance_t *input_resource_RequestSout( input_resource_t *p_resource, sout_instance_t *p_sout, const char *psz_sout )
496 {
497     vlc_mutex_lock( &p_resource->lock );
498     sout_instance_t *p_ret = RequestSout( p_resource, p_sout, psz_sout );
499     vlc_mutex_unlock( &p_resource->lock );
500
501     return p_ret;
502 }
503 void input_resource_TerminateSout( input_resource_t *p_resource )
504 {
505     input_resource_RequestSout( p_resource, NULL, NULL );
506 }
507