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