Made SPU commands go through vout.
[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         p_vout = vout_Request( p_resource->p_input, p_vout, p_fmt );
246         if( !p_vout )
247             return NULL;
248
249         DisplayVoutTitle( p_resource, p_vout );
250
251         vlc_mutex_lock( &p_resource->lock_hold );
252         TAB_APPEND( p_resource->i_vout, p_resource->pp_vout, p_vout );
253         vlc_mutex_unlock( &p_resource->lock_hold );
254
255         return p_vout;
256     }
257     else
258     {
259         assert( p_vout );
260
261         vlc_mutex_lock( &p_resource->lock_hold );
262         TAB_REMOVE( p_resource->i_vout, p_resource->pp_vout, p_vout );
263         const int i_vout_active = p_resource->i_vout;
264         vlc_mutex_unlock( &p_resource->lock_hold );
265
266         if( p_resource->p_vout_free || i_vout_active > 0 || !b_recycle )
267         {
268             if( b_recycle )
269                 msg_Dbg( p_resource->p_input, "detroying vout (already one saved or active)" );
270             vout_CloseAndRelease( p_vout );
271         }
272         else
273         {
274             msg_Dbg( p_resource->p_input, "saving a free vout" );
275             vout_Flush( p_vout, 1 );
276             vout_FlushSubpictureChannel( p_vout, -1 );
277
278             p_resource->p_vout_free = p_vout;
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 static aout_instance_t *DetachAout( input_resource_t *p_resource )
335 {
336     vlc_assert_locked( &p_resource->lock );
337     vlc_mutex_lock( &p_resource->lock_hold );
338
339     aout_instance_t *p_aout = p_resource->p_aout;
340     p_resource->p_aout = NULL;
341
342     vlc_mutex_unlock( &p_resource->lock_hold );
343
344     return p_aout;
345 }
346
347 static aout_instance_t *RequestAout( input_resource_t *p_resource, aout_instance_t *p_aout )
348 {
349     vlc_assert_locked( &p_resource->lock );
350     assert( p_resource->p_input );
351
352     if( p_aout )
353     {
354         msg_Dbg( p_resource->p_input, "releasing aout" );
355         vlc_object_release( p_aout );
356         return NULL;
357     }
358     else
359     {
360         p_aout = p_resource->p_aout;
361         if( !p_aout )
362         {
363             msg_Dbg( p_resource->p_input, "creating aout" );
364             p_aout = aout_New( p_resource->p_input );
365
366             vlc_mutex_lock( &p_resource->lock_hold );
367             p_resource->p_aout = p_aout;
368             vlc_mutex_unlock( &p_resource->lock_hold );
369         }
370         else
371         {
372             msg_Dbg( p_resource->p_input, "reusing aout" );
373         }
374
375         if( !p_aout )
376             return NULL;
377
378         vlc_object_detach( p_aout );
379         vlc_object_attach( p_aout, p_resource->p_input );
380         vlc_object_hold( p_aout );
381         return p_aout;
382     }
383 }
384 static aout_instance_t *HoldAout( input_resource_t *p_resource )
385 {
386     vlc_mutex_lock( &p_resource->lock_hold );
387
388     aout_instance_t *p_aout = p_resource->p_aout;
389     if( p_aout )
390         vlc_object_hold( p_aout );
391
392     vlc_mutex_unlock( &p_resource->lock_hold );
393
394     return p_aout;
395 }
396
397 /* */
398 input_resource_t *input_resource_New( void )
399 {
400     input_resource_t *p_resource = calloc( 1, sizeof(*p_resource) );
401     if( !p_resource )
402         return NULL;
403
404     vlc_mutex_init( &p_resource->lock );
405     vlc_mutex_init( &p_resource->lock_hold );
406     return p_resource;
407 }
408
409 void input_resource_Delete( input_resource_t *p_resource )
410 {
411     DestroySout( p_resource );
412     DestroyVout( p_resource );
413     DestroyAout( p_resource );
414
415     vlc_mutex_destroy( &p_resource->lock_hold );
416     vlc_mutex_destroy( &p_resource->lock );
417     free( p_resource );
418 }
419
420 void input_resource_SetInput( input_resource_t *p_resource, input_thread_t *p_input )
421 {
422     vlc_mutex_lock( &p_resource->lock );
423
424     if( p_resource->p_input && !p_input )
425     {
426         if( p_resource->p_aout )
427             vlc_object_detach( p_resource->p_aout );
428
429         assert( p_resource->i_vout == 0 );
430         if( p_resource->p_vout_free )
431             vlc_object_detach( p_resource->p_vout_free );
432
433         if( p_resource->p_sout )
434             vlc_object_detach( p_resource->p_sout );
435     }
436
437     /* */
438     p_resource->p_input = p_input;
439
440     vlc_mutex_unlock( &p_resource->lock );
441 }
442
443 input_resource_t *input_resource_Detach( input_resource_t *p_resource )
444 {
445     input_resource_t *p_ret = input_resource_New();
446     if( !p_ret )
447         return NULL;
448
449     vlc_mutex_lock( &p_resource->lock );
450     assert( !p_resource->p_input );
451     p_ret->p_sout = DetachSout( p_resource );
452     p_ret->p_vout_free = DetachVout( p_resource );
453     p_ret->p_aout = DetachAout( p_resource );
454     vlc_mutex_unlock( &p_resource->lock );
455
456     return p_ret;
457 }
458
459 vout_thread_t *input_resource_RequestVout( input_resource_t *p_resource,
460                                             vout_thread_t *p_vout, video_format_t *p_fmt, bool b_recycle )
461 {
462     vlc_mutex_lock( &p_resource->lock );
463     vout_thread_t *p_ret = RequestVout( p_resource, p_vout, p_fmt, b_recycle );
464     vlc_mutex_unlock( &p_resource->lock );
465
466     return p_ret;
467 }
468 vout_thread_t *input_resource_HoldVout( input_resource_t *p_resource )
469 {
470     return HoldVout( p_resource );
471 }
472
473 void input_resource_HoldVouts( input_resource_t *p_resource, vout_thread_t ***ppp_vout,
474                                size_t *pi_vout )
475 {
476     HoldVouts( p_resource, ppp_vout, pi_vout );
477 }
478
479 void input_resource_TerminateVout( input_resource_t *p_resource )
480 {
481     input_resource_RequestVout( p_resource, NULL, NULL, false );
482 }
483 bool input_resource_HasVout( input_resource_t *p_resource )
484 {
485     vlc_mutex_lock( &p_resource->lock );
486     assert( !p_resource->p_input );
487     const bool b_vout = p_resource->p_vout_free != NULL;
488     vlc_mutex_unlock( &p_resource->lock );
489
490     return b_vout;
491 }
492
493 /* */
494 aout_instance_t *input_resource_RequestAout( input_resource_t *p_resource, aout_instance_t *p_aout )
495 {
496     vlc_mutex_lock( &p_resource->lock );
497     aout_instance_t *p_ret = RequestAout( p_resource, p_aout );
498     vlc_mutex_unlock( &p_resource->lock );
499
500     return p_ret;
501 }
502 aout_instance_t *input_resource_HoldAout( input_resource_t *p_resource )
503 {
504     return HoldAout( p_resource );
505 }
506 /* */
507 sout_instance_t *input_resource_RequestSout( input_resource_t *p_resource, sout_instance_t *p_sout, const char *psz_sout )
508 {
509     vlc_mutex_lock( &p_resource->lock );
510     sout_instance_t *p_ret = RequestSout( p_resource, p_sout, psz_sout );
511     vlc_mutex_unlock( &p_resource->lock );
512
513     return p_ret;
514 }
515 void input_resource_TerminateSout( input_resource_t *p_resource )
516 {
517     input_resource_RequestSout( p_resource, NULL, NULL );
518 }
519