src/ : missing assert.h include
[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_osd.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     assert( p_resource->i_vout == 0 );
158     vout_thread_t *p_vout = p_resource->p_vout_free;
159     p_resource->p_vout_free = NULL;
160
161     return p_vout;
162 }
163
164 static void DisplayVoutTitle( input_resource_t *p_resource,
165                               vout_thread_t *p_vout )
166 {
167     assert( p_resource->p_input );
168
169     /* TODO display the title only one time for the same input ? */
170
171     input_item_t *p_item = input_GetItem( p_resource->p_input );
172
173     char *psz_nowplaying = input_item_GetNowPlaying( p_item );
174     if( psz_nowplaying && *psz_nowplaying )
175     {
176         vout_DisplayTitle( p_vout, psz_nowplaying );
177     }
178     else
179     {
180         char *psz_artist = input_item_GetArtist( p_item );
181         char *psz_name = input_item_GetTitle( p_item );
182
183         if( !psz_name || *psz_name == '\0' )
184         {
185             free( psz_name );
186             psz_name = input_item_GetName( p_item );
187         }
188         if( psz_artist && *psz_artist )
189         {
190             char *psz_string;
191             if( asprintf( &psz_string, "%s - %s", psz_name, psz_artist ) != -1 )
192             {
193                 vout_DisplayTitle( p_vout, psz_string );
194                 free( psz_string );
195             }
196         }
197         else if( psz_name )
198         {
199             vout_DisplayTitle( p_vout, psz_name );
200         }
201         free( psz_name );
202         free( psz_artist );
203     }
204     free( psz_nowplaying );
205 }
206 static vout_thread_t *RequestVout( input_resource_t *p_resource,
207                                    vout_thread_t *p_vout, video_format_t *p_fmt,
208                                    bool b_recycle )
209 {
210     if( !p_vout && !p_fmt )
211     {
212         if( p_resource->p_vout_free )
213         {
214             msg_Dbg( p_resource->p_vout_free, "destroying useless vout" );
215             vout_CloseAndRelease( p_resource->p_vout_free );
216             p_resource->p_vout_free = NULL;
217         }
218         return NULL;
219     }
220
221     assert( p_resource->p_input );
222     if( p_fmt )
223     {
224         /* */
225         if( !p_vout && p_resource->p_vout_free )
226         {
227             msg_Dbg( p_resource->p_input, "trying to reuse free vout" );
228             p_vout = p_resource->p_vout_free;
229
230             p_resource->p_vout_free = NULL;
231         }
232         else if( p_vout )
233         {
234             assert( p_vout != p_resource->p_vout_free );
235
236             vlc_mutex_lock( &p_resource->lock_hold );
237             TAB_REMOVE( p_resource->i_vout, p_resource->pp_vout, p_vout );
238             vlc_mutex_unlock( &p_resource->lock_hold );
239         }
240
241         /* */
242         p_vout = vout_Request( p_resource->p_input, p_vout, p_fmt );
243         if( !p_vout )
244             return NULL;
245
246         DisplayVoutTitle( p_resource, p_vout );
247
248         vlc_mutex_lock( &p_resource->lock_hold );
249         TAB_APPEND( p_resource->i_vout, p_resource->pp_vout, p_vout );
250         vlc_mutex_unlock( &p_resource->lock_hold );
251
252         return p_vout;
253     }
254     else
255     {
256         assert( p_vout );
257
258         vlc_mutex_lock( &p_resource->lock_hold );
259         TAB_REMOVE( p_resource->i_vout, p_resource->pp_vout, p_vout );
260         const int i_vout_active = p_resource->i_vout;
261         vlc_mutex_unlock( &p_resource->lock_hold );
262
263         if( p_resource->p_vout_free || i_vout_active > 0 || !b_recycle )
264         {
265             if( b_recycle )
266                 msg_Dbg( p_resource->p_input, "detroying vout (already one saved or active)" );
267             vout_CloseAndRelease( p_vout );
268         }
269         else
270         {
271             msg_Dbg( p_resource->p_input, "saving a free vout" );
272             vout_Flush( p_vout, 1 );
273             spu_Control( vout_GetSpu( p_vout ), SPU_CHANNEL_CLEAR, -1 );
274
275             p_resource->p_vout_free = p_vout;
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 *DetachAout( input_resource_t *p_resource )
332 {
333     vlc_mutex_lock( &p_resource->lock_hold );
334
335     aout_instance_t *p_aout = p_resource->p_aout;
336     p_resource->p_aout = NULL;
337
338     vlc_mutex_unlock( &p_resource->lock_hold );
339
340     return p_aout;
341 }
342
343 static aout_instance_t *RequestAout( input_resource_t *p_resource, aout_instance_t *p_aout )
344 {
345     assert( p_resource->p_input );
346
347     if( p_aout )
348     {
349         msg_Dbg( p_resource->p_input, "releasing aout" );
350         vlc_object_release( p_aout );
351         return NULL;
352     }
353     else
354     {
355         p_aout = p_resource->p_aout;
356         if( !p_aout )
357         {
358             msg_Dbg( p_resource->p_input, "creating aout" );
359             p_aout = aout_New( p_resource->p_input );
360
361             vlc_mutex_lock( &p_resource->lock_hold );
362             p_resource->p_aout = p_aout;
363             vlc_mutex_unlock( &p_resource->lock_hold );
364         }
365         else
366         {
367             msg_Dbg( p_resource->p_input, "reusing aout" );
368         }
369
370         if( !p_aout )
371             return NULL;
372
373         vlc_object_detach( p_aout );
374         vlc_object_attach( p_aout, p_resource->p_input );
375         vlc_object_hold( p_aout );
376         return p_aout;
377     }
378 }
379 static aout_instance_t *HoldAout( input_resource_t *p_resource )
380 {
381     vlc_mutex_lock( &p_resource->lock_hold );
382
383     aout_instance_t *p_aout = p_resource->p_aout;
384     if( p_aout )
385         vlc_object_hold( p_aout );
386
387     vlc_mutex_unlock( &p_resource->lock_hold );
388
389     return p_aout;
390 }
391
392 /* */
393 input_resource_t *input_resource_New( void )
394 {
395     input_resource_t *p_resource = calloc( 1, sizeof(*p_resource) );
396     if( !p_resource )
397         return NULL;
398
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     {
421         if( p_resource->p_aout )
422             vlc_object_detach( p_resource->p_aout );
423
424         assert( p_resource->i_vout == 0 );
425         if( p_resource->p_vout_free )
426             vlc_object_detach( p_resource->p_vout_free );
427
428         if( p_resource->p_sout )
429             vlc_object_detach( p_resource->p_sout );
430     }
431
432     /* */
433     p_resource->p_input = p_input;
434
435     vlc_mutex_unlock( &p_resource->lock );
436 }
437
438 input_resource_t *input_resource_Detach( input_resource_t *p_resource )
439 {
440     input_resource_t *p_ret = input_resource_New();
441     if( !p_ret )
442         return NULL;
443
444     vlc_mutex_lock( &p_resource->lock );
445     assert( !p_resource->p_input );
446     p_ret->p_sout = DetachSout( p_resource );
447     p_ret->p_vout_free = DetachVout( p_resource );
448     p_ret->p_aout = DetachAout( p_resource );
449     vlc_mutex_unlock( &p_resource->lock );
450
451     return p_ret;
452 }
453
454 vout_thread_t *input_resource_RequestVout( input_resource_t *p_resource,
455                                             vout_thread_t *p_vout, video_format_t *p_fmt, bool b_recycle )
456 {
457     vlc_mutex_lock( &p_resource->lock );
458     vout_thread_t *p_ret = RequestVout( p_resource, p_vout, p_fmt, b_recycle );
459     vlc_mutex_unlock( &p_resource->lock );
460
461     return p_ret;
462 }
463 vout_thread_t *input_resource_HoldVout( input_resource_t *p_resource )
464 {
465     return HoldVout( p_resource );
466 }
467
468 void input_resource_HoldVouts( input_resource_t *p_resource, vout_thread_t ***ppp_vout,
469                                size_t *pi_vout )
470 {
471     HoldVouts( p_resource, ppp_vout, pi_vout );
472 }
473
474 void input_resource_TerminateVout( input_resource_t *p_resource )
475 {
476     input_resource_RequestVout( p_resource, NULL, NULL, false );
477 }
478 bool input_resource_HasVout( input_resource_t *p_resource )
479 {
480     vlc_mutex_lock( &p_resource->lock );
481     assert( !p_resource->p_input );
482     const bool b_vout = p_resource->p_vout_free != NULL;
483     vlc_mutex_unlock( &p_resource->lock );
484
485     return b_vout;
486 }
487
488 /* */
489 aout_instance_t *input_resource_RequestAout( input_resource_t *p_resource, aout_instance_t *p_aout )
490 {
491     vlc_mutex_lock( &p_resource->lock );
492     aout_instance_t *p_ret = RequestAout( p_resource, p_aout );
493     vlc_mutex_unlock( &p_resource->lock );
494
495     return p_ret;
496 }
497 aout_instance_t *input_resource_HoldAout( input_resource_t *p_resource )
498 {
499     return HoldAout( p_resource );
500 }
501 /* */
502 sout_instance_t *input_resource_RequestSout( input_resource_t *p_resource, sout_instance_t *p_sout, const char *psz_sout )
503 {
504     vlc_mutex_lock( &p_resource->lock );
505     sout_instance_t *p_ret = RequestSout( p_resource, p_sout, psz_sout );
506     vlc_mutex_unlock( &p_resource->lock );
507
508     return p_ret;
509 }
510 void input_resource_TerminateSout( input_resource_t *p_resource )
511 {
512     input_resource_RequestSout( p_resource, NULL, NULL );
513 }
514