LGPL
[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 it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * 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     vout_thread_t   **pp_vout;
71     int             i_vout;
72
73     /* TODO? track more than one audio output (like video outputs) */
74     bool            b_aout_busy;
75     audio_output_t *p_aout;
76 };
77
78 /* */
79 static void DestroySout( input_resource_t *p_resource )
80 {
81 #ifdef ENABLE_SOUT
82     if( p_resource->p_sout )
83         sout_DeleteInstance( p_resource->p_sout );
84 #endif
85     p_resource->p_sout = NULL;
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_sout || ( !p_resource->p_sout && !psz_sout ) );
101
102     /* Check the validity of the sout */
103     if( p_resource->p_sout &&
104         strcmp( p_resource->p_sout->psz_sout, psz_sout ) )
105     {
106         msg_Dbg( p_resource->p_parent, "destroying unusable sout" );
107         DestroySout( p_resource );
108     }
109
110     if( psz_sout )
111     {
112         if( p_resource->p_sout )
113         {
114             /* Reuse it */
115             msg_Dbg( p_resource->p_parent, "reusing sout" );
116             msg_Dbg( p_resource->p_parent, "you probably want to use gather stream_out" );
117         }
118         else
119         {
120             /* Create a new one */
121             p_resource->p_sout = sout_NewInstance( p_resource->p_parent, 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         p_resource->p_sout = p_sout;
132         return NULL;
133     }
134 #else
135     return NULL;
136 #endif
137 }
138
139 /* */
140 static void DestroyVout( input_resource_t *p_resource )
141 {
142     assert( p_resource->i_vout == 0 );
143
144     if( p_resource->p_vout_free )
145         vout_CloseAndRelease( p_resource->p_vout_free );
146
147     p_resource->p_vout_free = NULL;
148 }
149
150 static void DisplayVoutTitle( input_resource_t *p_resource,
151                               vout_thread_t *p_vout )
152 {
153     if( p_resource->p_input == NULL )
154         return;
155
156     /* TODO display the title only one time for the same input ? */
157
158     input_item_t *p_item = input_GetItem( p_resource->p_input );
159
160     char *psz_nowplaying = input_item_GetNowPlaying( p_item );
161     if( psz_nowplaying && *psz_nowplaying )
162     {
163         vout_DisplayTitle( p_vout, psz_nowplaying );
164     }
165     else
166     {
167         char *psz_artist = input_item_GetArtist( p_item );
168         char *psz_name = input_item_GetTitle( p_item );
169
170         if( !psz_name || *psz_name == '\0' )
171         {
172             free( psz_name );
173             psz_name = input_item_GetName( p_item );
174         }
175         if( psz_artist && *psz_artist )
176         {
177             char *psz_string;
178             if( asprintf( &psz_string, "%s - %s", psz_name, psz_artist ) != -1 )
179             {
180                 vout_DisplayTitle( p_vout, psz_string );
181                 free( psz_string );
182             }
183         }
184         else if( psz_name )
185         {
186             vout_DisplayTitle( p_vout, psz_name );
187         }
188         free( psz_name );
189         free( psz_artist );
190     }
191     free( psz_nowplaying );
192 }
193 static vout_thread_t *RequestVout( input_resource_t *p_resource,
194                                    vout_thread_t *p_vout,
195                                    video_format_t *p_fmt, unsigned dpb_size,
196                                    bool b_recycle )
197 {
198     vlc_assert_locked( &p_resource->lock );
199
200     if( !p_vout && !p_fmt )
201     {
202         if( p_resource->p_vout_free )
203         {
204             msg_Dbg( p_resource->p_vout_free, "destroying useless vout" );
205             vout_CloseAndRelease( p_resource->p_vout_free );
206             p_resource->p_vout_free = NULL;
207         }
208         return NULL;
209     }
210
211     if( p_fmt )
212     {
213         /* */
214         if( !p_vout && p_resource->p_vout_free )
215         {
216             msg_Dbg( p_resource->p_parent, "trying to reuse free vout" );
217             p_vout = p_resource->p_vout_free;
218
219             p_resource->p_vout_free = NULL;
220         }
221         else if( p_vout )
222         {
223             assert( p_vout != p_resource->p_vout_free );
224
225             vlc_mutex_lock( &p_resource->lock_hold );
226             TAB_REMOVE( p_resource->i_vout, p_resource->pp_vout, p_vout );
227             vlc_mutex_unlock( &p_resource->lock_hold );
228         }
229
230         /* */
231         vout_configuration_t cfg = {
232             .vout       = p_vout,
233             .input      = VLC_OBJECT(p_resource->p_input),
234             .change_fmt = true,
235             .fmt        = p_fmt,
236             .dpb_size   = dpb_size,
237         };
238         p_vout = vout_Request( p_resource->p_parent, &cfg );
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_parent, "detroying vout (already one saved or active)" );
263             vout_CloseAndRelease( p_vout );
264         }
265         else
266         {
267             msg_Dbg( p_resource->p_parent, "saving a free vout" );
268             vout_Flush( p_vout, 1 );
269             vout_FlushSubpictureChannel( p_vout, -1 );
270
271             vout_configuration_t cfg = {
272                 .vout       = p_vout,
273                 .input      = NULL,
274                 .change_fmt = false,
275                 .fmt        = NULL,
276                 .dpb_size   = 0,
277             };
278             p_resource->p_vout_free = vout_Request( p_resource->p_parent, &cfg );
279         }
280         return NULL;
281     }
282 }
283 static vout_thread_t *HoldVout( input_resource_t *p_resource )
284 {
285     /* TODO FIXME: p_resource->pp_vout order is NOT stable */
286     vlc_mutex_lock( &p_resource->lock_hold );
287
288     vout_thread_t *p_vout = p_resource->i_vout > 0 ? p_resource->pp_vout[0] : NULL;
289     if( p_vout )
290         vlc_object_hold( p_vout );
291
292     vlc_mutex_unlock( &p_resource->lock_hold );
293
294     return p_vout;
295 }
296
297 static void HoldVouts( input_resource_t *p_resource, vout_thread_t ***ppp_vout,
298                        size_t *pi_vout )
299 {
300     vout_thread_t **pp_vout;
301
302     *pi_vout = 0;
303     *ppp_vout = NULL;
304
305     vlc_mutex_lock( &p_resource->lock_hold );
306
307     if( p_resource->i_vout <= 0 )
308         goto exit;
309
310     pp_vout = malloc( p_resource->i_vout * sizeof(*pp_vout) );
311     if( !pp_vout )
312         goto exit;
313
314     *ppp_vout = pp_vout;
315     *pi_vout = p_resource->i_vout;
316
317     for( int i = 0; i < p_resource->i_vout; i++ )
318     {
319         pp_vout[i] = p_resource->pp_vout[i];
320         vlc_object_hold( pp_vout[i] );
321     }
322
323 exit:
324     vlc_mutex_unlock( &p_resource->lock_hold );
325 }
326
327 /* */
328 static void DestroyAout( input_resource_t *p_resource )
329 {
330     if( p_resource->p_aout )
331     {
332         aout_Destroy( p_resource->p_aout );
333         p_resource->p_aout = NULL;
334     }
335 }
336
337 static void ReleaseAout( input_resource_t *p_resource,
338                          audio_output_t *p_aout )
339 {
340     if( likely(p_aout == p_resource->p_aout) )
341     {
342         assert( p_resource->b_aout_busy );
343         p_resource->b_aout_busy = false;
344         msg_Dbg( p_resource->p_parent, "releasing audio output" );
345         vlc_object_release( p_aout );
346     }
347     else
348     {
349         msg_Dbg( p_resource->p_parent, "destroying extra audio output" );
350         aout_Destroy( p_aout );
351     }
352 }
353
354 static audio_output_t *AllocateAout( input_resource_t *p_resource )
355 {
356     audio_output_t *p_aout;
357
358     if( unlikely(p_resource->b_aout_busy) )
359     {
360         msg_Dbg( p_resource->p_parent, "creating extra audio output" );
361         return aout_New( p_resource->p_parent );
362     }
363
364     p_aout = p_resource->p_aout;
365     if( p_aout == NULL )
366     {
367         msg_Dbg( p_resource->p_parent, "creating audio output" );
368         p_aout = aout_New( p_resource->p_parent );
369         if( unlikely(p_aout == NULL) )
370             return NULL;
371
372         vlc_mutex_lock( &p_resource->lock_hold );
373         assert( p_resource->p_aout == NULL );
374         p_resource->p_aout = p_aout;
375         vlc_mutex_unlock( &p_resource->lock_hold );
376     }
377     else
378         msg_Dbg( p_resource->p_parent, "recycling audio output" );
379
380     p_resource->b_aout_busy = true;
381     vlc_object_hold( p_aout );
382     return p_aout;
383 }
384
385 static audio_output_t *RequestAout( input_resource_t *p_resource, audio_output_t *p_aout )
386 {
387     vlc_assert_locked( &p_resource->lock );
388
389     if( p_aout )
390     {
391         ReleaseAout( p_resource, p_aout );
392         return NULL;
393     }
394     return AllocateAout( p_resource );
395 }
396
397 static audio_output_t *HoldAout( input_resource_t *p_resource )
398 {
399     vlc_mutex_lock( &p_resource->lock_hold );
400
401     audio_output_t *p_aout = p_resource->p_aout;
402     if( p_aout )
403         vlc_object_hold( p_aout );
404
405     vlc_mutex_unlock( &p_resource->lock_hold );
406
407     return p_aout;
408 }
409
410 static void TerminateAout( input_resource_t *p_resource )
411 {
412     vlc_mutex_lock( &p_resource->lock_hold );
413
414     audio_output_t *p_aout = p_resource->p_aout;
415     p_resource->p_aout = NULL;
416
417     vlc_mutex_unlock( &p_resource->lock_hold );
418
419     if( p_aout )
420         aout_Destroy( p_aout );
421 }
422
423 static void Destructor( gc_object_t *p_gc )
424 {
425     input_resource_t *p_resource = vlc_priv( p_gc, input_resource_t );
426
427     DestroySout( p_resource );
428     DestroyVout( p_resource );
429     DestroyAout( p_resource );
430
431     vlc_mutex_destroy( &p_resource->lock_hold );
432     vlc_mutex_destroy( &p_resource->lock );
433     free( p_resource );
434 }
435
436 /* */
437 input_resource_t *input_resource_New( vlc_object_t *p_parent )
438 {
439     input_resource_t *p_resource = calloc( 1, sizeof(*p_resource) );
440     if( !p_resource )
441         return NULL;
442
443     vlc_gc_init( p_resource, Destructor );
444     p_resource->p_parent = p_parent;
445     vlc_mutex_init( &p_resource->lock );
446     vlc_mutex_init( &p_resource->lock_hold );
447     return p_resource;
448 }
449
450 void input_resource_Release( input_resource_t *p_resource )
451 {
452     vlc_gc_decref( p_resource );
453 }
454
455 input_resource_t *input_resource_Hold( input_resource_t *p_resource )
456 {
457     vlc_gc_incref( p_resource );
458     return p_resource;
459 }
460
461 void input_resource_SetInput( input_resource_t *p_resource, input_thread_t *p_input )
462 {
463     vlc_mutex_lock( &p_resource->lock );
464
465     if( p_resource->p_input && !p_input )
466         assert( p_resource->i_vout == 0 );
467
468     /* */
469     p_resource->p_input = p_input;
470
471     vlc_mutex_unlock( &p_resource->lock );
472 }
473
474 vout_thread_t *input_resource_RequestVout( input_resource_t *p_resource,
475                                             vout_thread_t *p_vout,
476                                             video_format_t *p_fmt, unsigned dpb_size,
477                                             bool b_recycle )
478 {
479     vlc_mutex_lock( &p_resource->lock );
480     vout_thread_t *p_ret = RequestVout( p_resource, p_vout, p_fmt, dpb_size, b_recycle );
481     vlc_mutex_unlock( &p_resource->lock );
482
483     return p_ret;
484 }
485 vout_thread_t *input_resource_HoldVout( input_resource_t *p_resource )
486 {
487     return HoldVout( p_resource );
488 }
489
490 void input_resource_HoldVouts( input_resource_t *p_resource, vout_thread_t ***ppp_vout,
491                                size_t *pi_vout )
492 {
493     HoldVouts( p_resource, ppp_vout, pi_vout );
494 }
495
496 void input_resource_TerminateVout( input_resource_t *p_resource )
497 {
498     input_resource_RequestVout( p_resource, NULL, NULL, 0, false );
499 }
500 bool input_resource_HasVout( input_resource_t *p_resource )
501 {
502     vlc_mutex_lock( &p_resource->lock );
503     assert( !p_resource->p_input );
504     const bool b_vout = p_resource->p_vout_free != NULL;
505     vlc_mutex_unlock( &p_resource->lock );
506
507     return b_vout;
508 }
509
510 /* */
511 audio_output_t *input_resource_RequestAout( input_resource_t *p_resource, audio_output_t *p_aout )
512 {
513     vlc_mutex_lock( &p_resource->lock );
514     audio_output_t *p_ret = RequestAout( p_resource, p_aout );
515     vlc_mutex_unlock( &p_resource->lock );
516
517     return p_ret;
518 }
519 audio_output_t *input_resource_HoldAout( input_resource_t *p_resource )
520 {
521     return HoldAout( p_resource );
522 }
523
524 /* */
525 sout_instance_t *input_resource_RequestSout( input_resource_t *p_resource, sout_instance_t *p_sout, const char *psz_sout )
526 {
527     vlc_mutex_lock( &p_resource->lock );
528     sout_instance_t *p_ret = RequestSout( p_resource, p_sout, psz_sout );
529     vlc_mutex_unlock( &p_resource->lock );
530
531     return p_ret;
532 }
533 void input_resource_TerminateSout( input_resource_t *p_resource )
534 {
535     input_resource_RequestSout( p_resource, NULL, NULL );
536 }
537
538 void input_resource_Terminate( input_resource_t *p_resource )
539 {
540     input_resource_TerminateSout( p_resource );
541
542     vlc_mutex_lock( &p_resource->lock );
543     TerminateAout( p_resource );
544     vlc_mutex_unlock( &p_resource->lock );
545
546     input_resource_TerminateVout( p_resource );
547 }
548