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