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