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