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