Rename aout_instance_t to audio_output_t
[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     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         vlc_object_release( p_resource->p_aout );
332     p_resource->p_aout = NULL;
333 }
334
335 static void ReleaseAout( input_resource_t *p_resource,
336                          audio_output_t *p_aout )
337 {
338     msg_Dbg( p_resource->p_parent, "releasing audio output" );
339     if( likely(p_aout == p_resource->p_aout) )
340         p_resource->b_aout_busy = false;
341     vlc_object_release( p_aout );
342 }
343
344 static audio_output_t *AllocateAout( input_resource_t *p_resource )
345 {
346     audio_output_t *p_aout;
347
348     if( unlikely(p_resource->b_aout_busy) )
349     {
350         msg_Dbg( p_resource->p_parent, "creating audio output" );
351         return aout_New( p_resource->p_parent );
352     }
353
354     p_aout = p_resource->p_aout;
355     if( p_aout == NULL )
356     {
357         msg_Dbg( p_resource->p_parent, "creating audio output" );
358         p_aout = aout_New( p_resource->p_parent );
359         if( unlikely(p_aout == NULL) )
360             return NULL;
361
362         vlc_mutex_lock( &p_resource->lock_hold );
363         p_resource->p_aout = p_aout;
364         vlc_mutex_unlock( &p_resource->lock_hold );
365     }
366     else
367         msg_Dbg( p_resource->p_parent, "recycling audio output" );
368
369     p_resource->b_aout_busy = true;
370     vlc_object_hold( p_aout );
371     return p_aout;
372 }
373
374 static audio_output_t *RequestAout( input_resource_t *p_resource, audio_output_t *p_aout )
375 {
376     vlc_assert_locked( &p_resource->lock );
377
378     if( p_aout )
379     {
380         ReleaseAout( p_resource, p_aout );
381         return NULL;
382     }
383     return AllocateAout( p_resource );
384 }
385
386 static audio_output_t *HoldAout( input_resource_t *p_resource )
387 {
388     vlc_mutex_lock( &p_resource->lock_hold );
389
390     audio_output_t *p_aout = p_resource->p_aout;
391     if( p_aout )
392         vlc_object_hold( p_aout );
393
394     vlc_mutex_unlock( &p_resource->lock_hold );
395
396     return p_aout;
397 }
398
399 static void TerminateAout( input_resource_t *p_resource )
400 {
401     vlc_mutex_lock( &p_resource->lock_hold );
402
403     audio_output_t *p_aout = p_resource->p_aout;
404     p_resource->p_aout = NULL;
405
406     vlc_mutex_unlock( &p_resource->lock_hold );
407
408     if( p_aout )
409         vlc_object_release( p_aout );
410 }
411
412 static void Destructor( gc_object_t *p_gc )
413 {
414     input_resource_t *p_resource = vlc_priv( p_gc, input_resource_t );
415
416     DestroySout( p_resource );
417     DestroyVout( p_resource );
418     DestroyAout( p_resource );
419
420     vlc_mutex_destroy( &p_resource->lock_hold );
421     vlc_mutex_destroy( &p_resource->lock );
422     free( p_resource );
423 }
424
425 /* */
426 input_resource_t *input_resource_New( vlc_object_t *p_parent )
427 {
428     input_resource_t *p_resource = calloc( 1, sizeof(*p_resource) );
429     if( !p_resource )
430         return NULL;
431
432     vlc_gc_init( p_resource, Destructor );
433     p_resource->p_parent = p_parent;
434     vlc_mutex_init( &p_resource->lock );
435     vlc_mutex_init( &p_resource->lock_hold );
436     return p_resource;
437 }
438
439 void input_resource_Release( input_resource_t *p_resource )
440 {
441     vlc_gc_decref( p_resource );
442 }
443
444 input_resource_t *input_resource_Hold( input_resource_t *p_resource )
445 {
446     vlc_gc_incref( p_resource );
447     return p_resource;
448 }
449
450 void input_resource_SetInput( input_resource_t *p_resource, input_thread_t *p_input )
451 {
452     vlc_mutex_lock( &p_resource->lock );
453
454     if( p_resource->p_input && !p_input )
455         assert( p_resource->i_vout == 0 );
456
457     /* */
458     p_resource->p_input = p_input;
459
460     vlc_mutex_unlock( &p_resource->lock );
461 }
462
463 vout_thread_t *input_resource_RequestVout( input_resource_t *p_resource,
464                                             vout_thread_t *p_vout,
465                                             video_format_t *p_fmt, unsigned dpb_size,
466                                             bool b_recycle )
467 {
468     vlc_mutex_lock( &p_resource->lock );
469     vout_thread_t *p_ret = RequestVout( p_resource, p_vout, p_fmt, dpb_size, b_recycle );
470     vlc_mutex_unlock( &p_resource->lock );
471
472     return p_ret;
473 }
474 vout_thread_t *input_resource_HoldVout( input_resource_t *p_resource )
475 {
476     return HoldVout( p_resource );
477 }
478
479 void input_resource_HoldVouts( input_resource_t *p_resource, vout_thread_t ***ppp_vout,
480                                size_t *pi_vout )
481 {
482     HoldVouts( p_resource, ppp_vout, pi_vout );
483 }
484
485 void input_resource_TerminateVout( input_resource_t *p_resource )
486 {
487     input_resource_RequestVout( p_resource, NULL, NULL, 0, false );
488 }
489 bool input_resource_HasVout( input_resource_t *p_resource )
490 {
491     vlc_mutex_lock( &p_resource->lock );
492     assert( !p_resource->p_input );
493     const bool b_vout = p_resource->p_vout_free != NULL;
494     vlc_mutex_unlock( &p_resource->lock );
495
496     return b_vout;
497 }
498
499 /* */
500 audio_output_t *input_resource_RequestAout( input_resource_t *p_resource, audio_output_t *p_aout )
501 {
502     vlc_mutex_lock( &p_resource->lock );
503     audio_output_t *p_ret = RequestAout( p_resource, p_aout );
504     vlc_mutex_unlock( &p_resource->lock );
505
506     return p_ret;
507 }
508 audio_output_t *input_resource_HoldAout( input_resource_t *p_resource )
509 {
510     return HoldAout( p_resource );
511 }
512
513 /* */
514 sout_instance_t *input_resource_RequestSout( input_resource_t *p_resource, sout_instance_t *p_sout, const char *psz_sout )
515 {
516     vlc_mutex_lock( &p_resource->lock );
517     sout_instance_t *p_ret = RequestSout( p_resource, p_sout, psz_sout );
518     vlc_mutex_unlock( &p_resource->lock );
519
520     return p_ret;
521 }
522 void input_resource_TerminateSout( input_resource_t *p_resource )
523 {
524     input_resource_RequestSout( p_resource, NULL, NULL );
525 }
526
527 void input_resource_Terminate( input_resource_t *p_resource )
528 {
529     input_resource_TerminateSout( p_resource );
530
531     vlc_mutex_lock( &p_resource->lock );
532     TerminateAout( p_resource );
533     vlc_mutex_unlock( &p_resource->lock );
534
535     input_resource_TerminateVout( p_resource );
536 }
537