Spell resource the English way
[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 <vlc_common.h>
32 #include <vlc_vout.h>
33 #include <vlc_osd.h>
34 #include <vlc_aout.h>
35 #include <vlc_sout.h>
36 #include "../libvlc.h"
37 #include "../stream_output/stream_output.h"
38 #include "../audio_output/aout_internal.h"
39 #include "../video_output/vout_control.h"
40 #include "input_interface.h"
41 #include "resource.h"
42
43 struct input_resource_t
44 {
45     /* This lock is used to serialize request and protect
46      * our variables */
47     vlc_mutex_t    lock;
48
49     /* */
50     input_thread_t *p_input;
51
52     sout_instance_t *p_sout;
53     vout_thread_t   *p_vout_free;
54     aout_instance_t *p_aout;
55
56     /* This lock is used to protect vout resources access (for hold)
57      * It is a special case because of embed video (possible deadlock
58      * between vout window request and vout holds in some(qt4) interface) */
59     vlc_mutex_t    lock_vout;
60     int             i_vout;
61     vout_thread_t   **pp_vout;
62 };
63
64 /* */
65 static void DestroySout( input_resource_t *p_resource )
66 {
67 #ifdef ENABLE_SOUT
68     if( p_resource->p_sout )
69         sout_DeleteInstance( p_resource->p_sout );
70 #endif
71     p_resource->p_sout = NULL;
72 }
73 static sout_instance_t *RequestSout( input_resource_t *p_resource,
74                                      sout_instance_t *p_sout, const char *psz_sout )
75 {
76 #ifdef ENABLE_SOUT
77     if( !p_sout && !psz_sout )
78     {
79         if( p_resource->p_sout )
80             msg_Dbg( p_resource->p_sout, "destroying useless sout" );
81         DestroySout( p_resource );
82         return NULL;
83     }
84
85     assert( p_resource->p_input );
86     assert( !p_sout || ( !p_resource->p_sout && !psz_sout ) );
87
88     /* Check the validity of the sout */
89     if( p_resource->p_sout &&
90         strcmp( p_resource->p_sout->psz_sout, psz_sout ) )
91     {
92         msg_Dbg( p_resource->p_input, "destroying unusable sout" );
93         DestroySout( p_resource );
94     }
95
96     if( psz_sout )
97     {
98         if( p_resource->p_sout )
99         {
100             /* Reuse it */
101             msg_Dbg( p_resource->p_input, "reusing sout" );
102             msg_Dbg( p_resource->p_input, "you probably want to use gather stream_out" );
103             vlc_object_attach( p_resource->p_sout, p_resource->p_input );
104         }
105         else
106         {
107             /* Create a new one */
108             p_resource->p_sout = sout_NewInstance( p_resource->p_input, psz_sout );
109         }
110
111         p_sout = p_resource->p_sout;
112         p_resource->p_sout = NULL;
113
114         return p_sout;
115     }
116     else
117     {
118         vlc_object_detach( p_sout );
119         p_resource->p_sout = p_sout;
120
121         return NULL;
122     }
123 #else
124     return NULL;
125 #endif
126 }
127
128 /* */
129 static void DestroyVout( input_resource_t *p_resource )
130 {
131     assert( p_resource->i_vout == 0 );
132
133     if( p_resource->p_vout_free )
134         vout_CloseAndRelease( p_resource->p_vout_free );
135
136     p_resource->p_vout_free = NULL;
137 }
138 static void DisplayVoutTitle( input_resource_t *p_resource,
139                               vout_thread_t *p_vout )
140 {
141     assert( p_resource->p_input );
142
143     /* TODO display the title only one time for the same input ? */
144
145     input_item_t *p_item = input_GetItem( p_resource->p_input );
146
147     char *psz_nowplaying = input_item_GetNowPlaying( p_item );
148     if( psz_nowplaying && *psz_nowplaying )
149     {
150         vout_DisplayTitle( p_vout, psz_nowplaying );
151     }
152     else
153     {
154         char *psz_artist = input_item_GetArtist( p_item );
155         char *psz_name = input_item_GetTitle( p_item );
156
157         if( !psz_name || *psz_name == '\0' )
158         {
159             free( psz_name );
160             psz_name = input_item_GetName( p_item );
161         }
162         if( psz_artist && *psz_artist )
163         {
164             char *psz_string;
165             if( asprintf( &psz_string, "%s - %s", psz_name, psz_artist ) != -1 )
166             {
167                 vout_DisplayTitle( p_vout, psz_string );
168                 free( psz_string );
169             }
170         }
171         else if( psz_name )
172         {
173             vout_DisplayTitle( p_vout, psz_name );
174         }
175         free( psz_name );
176         free( psz_artist );
177     }
178     free( psz_nowplaying );
179 }
180 static vout_thread_t *RequestVout( input_resource_t *p_resource,
181                                    vout_thread_t *p_vout, video_format_t *p_fmt,
182                                    bool b_recycle )
183 {
184     if( !p_vout && !p_fmt )
185     {
186         if( p_resource->p_vout_free )
187         {
188             msg_Dbg( p_resource->p_vout_free, "destroying useless vout" );
189             vout_CloseAndRelease( p_resource->p_vout_free );
190             p_resource->p_vout_free = NULL;
191         }
192         return NULL;
193     }
194
195     assert( p_resource->p_input );
196     if( p_fmt )
197     {
198         /* */
199         if( !p_vout && p_resource->p_vout_free )
200         {
201             msg_Dbg( p_resource->p_input, "trying to reuse free vout" );
202             p_vout = p_resource->p_vout_free;
203
204             p_resource->p_vout_free = NULL;
205         }
206         else if( p_vout )
207         {
208             assert( p_vout != p_resource->p_vout_free );
209
210             vlc_mutex_lock( &p_resource->lock_vout );
211             TAB_REMOVE( p_resource->i_vout, p_resource->pp_vout, p_vout );
212             vlc_mutex_unlock( &p_resource->lock_vout );
213         }
214
215         /* */
216         p_vout = vout_Request( p_resource->p_input, p_vout, p_fmt );
217         if( !p_vout )
218             return NULL;
219
220         DisplayVoutTitle( p_resource, p_vout );
221
222         vlc_mutex_lock( &p_resource->lock_vout );
223         TAB_APPEND( p_resource->i_vout, p_resource->pp_vout, p_vout );
224         vlc_mutex_unlock( &p_resource->lock_vout );
225
226         return p_vout;
227     }
228     else
229     {
230         assert( p_vout );
231
232         vlc_mutex_lock( &p_resource->lock_vout );
233         TAB_REMOVE( p_resource->i_vout, p_resource->pp_vout, p_vout );
234         const int i_vout_active = p_resource->i_vout;
235         vlc_mutex_unlock( &p_resource->lock_vout );
236
237         if( p_resource->p_vout_free || i_vout_active > 0 || !b_recycle )
238         {
239             if( b_recycle )
240                 msg_Dbg( p_resource->p_input, "detroying vout (already one saved or active)" );
241             vout_CloseAndRelease( p_vout );
242         }
243         else
244         {
245             msg_Dbg( p_resource->p_input, "saving a free vout" );
246             vout_Flush( p_vout, 1 );
247             spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR, -1 );
248
249             p_resource->p_vout_free = p_vout;
250         }
251         return NULL;
252     }
253 }
254 static vout_thread_t *HoldVout( input_resource_t *p_resource )
255 {
256     if( p_resource->i_vout <= 0 )
257         return NULL;
258
259     /* TODO FIXME: p_resource->pp_vout order is NOT stable */
260     vlc_mutex_lock( &p_resource->lock_vout );
261
262     vout_thread_t *p_vout = p_resource->pp_vout[0];
263
264     vlc_object_hold( p_vout );
265
266     vlc_mutex_unlock( &p_resource->lock_vout );
267
268     return p_vout;
269 }
270 static void HoldVouts( input_resource_t *p_resource, vout_thread_t ***ppp_vout, int *pi_vout )
271 {
272     vout_thread_t **pp_vout;
273
274     *pi_vout = 0;
275     *ppp_vout = NULL;
276
277     vlc_mutex_lock( &p_resource->lock_vout );
278
279     if( p_resource->i_vout <= 0 )
280         goto exit;
281
282     pp_vout = calloc( p_resource->i_vout, sizeof(*pp_vout) );
283     if( !pp_vout )
284         goto exit;
285
286     *ppp_vout = pp_vout;
287     *pi_vout = p_resource->i_vout;
288
289     for( int i = 0; i < p_resource->i_vout; i++ )
290     {
291         pp_vout[i] = p_resource->pp_vout[i];
292         vlc_object_hold( pp_vout[i] );
293     }
294
295 exit:
296     vlc_mutex_unlock( &p_resource->lock_vout );
297 }
298
299 /* */
300 static void DestroyAout( input_resource_t *p_resource )
301 {
302     if( p_resource->p_aout )
303         vlc_object_release( p_resource->p_aout );
304     p_resource->p_aout = NULL;
305 }
306 static aout_instance_t *RequestAout( input_resource_t *p_resource, aout_instance_t *p_aout )
307 {
308     assert( p_resource->p_input );
309
310     if( p_aout )
311     {
312         msg_Dbg( p_resource->p_input, "releasing aout" );
313         vlc_object_release( p_aout );
314         return NULL;
315     }
316     else
317     {
318         if( !p_resource->p_aout )
319         {
320             msg_Dbg( p_resource->p_input, "creating aout" );
321             p_resource->p_aout = aout_New( p_resource->p_input );
322         }
323         else
324         {
325             msg_Dbg( p_resource->p_input, "reusing aout" );
326         }
327
328         if( !p_resource->p_aout )
329             return NULL;
330
331         vlc_object_detach( p_resource->p_aout );
332         vlc_object_attach( p_resource->p_aout, p_resource->p_input );
333         vlc_object_hold( p_resource->p_aout );
334         return p_resource->p_aout;
335     }
336 }
337 static aout_instance_t *HoldAout( input_resource_t *p_resource )
338 {
339     if( !p_resource->p_aout )
340         return NULL;
341
342     aout_instance_t *p_aout = p_resource->p_aout;
343
344     vlc_object_hold( p_aout );
345
346     return p_aout;
347 }
348
349 /* */
350 input_resource_t *input_resource_New( void )
351 {
352     input_resource_t *p_resource = calloc( 1, sizeof(*p_resource) );
353     if( !p_resource )
354         return NULL;
355
356     vlc_mutex_init( &p_resource->lock );
357     vlc_mutex_init( &p_resource->lock_vout );
358     return p_resource;
359 }
360
361 void input_resource_Delete( input_resource_t *p_resource )
362 {
363     DestroySout( p_resource );
364     DestroyVout( p_resource );
365     DestroyAout( p_resource );
366
367     vlc_mutex_destroy( &p_resource->lock_vout );
368     vlc_mutex_destroy( &p_resource->lock );
369     free( p_resource );
370 }
371
372 void input_resource_SetInput( input_resource_t *p_resource, input_thread_t *p_input )
373 {
374     vlc_mutex_lock( &p_resource->lock );
375
376     if( p_resource->p_input && !p_input )
377     {
378         if( p_resource->p_aout )
379             vlc_object_detach( p_resource->p_aout );
380
381         assert( p_resource->i_vout == 0 );
382         if( p_resource->p_vout_free )
383             vlc_object_detach( p_resource->p_vout_free );
384
385         if( p_resource->p_sout )
386             vlc_object_detach( p_resource->p_sout );
387     }
388
389     /* */
390     p_resource->p_input = p_input;
391
392     vlc_mutex_unlock( &p_resource->lock );
393 }
394
395 vout_thread_t *input_resource_RequestVout( input_resource_t *p_resource,
396                                             vout_thread_t *p_vout, video_format_t *p_fmt, bool b_recycle )
397 {
398     vlc_mutex_lock( &p_resource->lock );
399     vout_thread_t *p_ret = RequestVout( p_resource, p_vout, p_fmt, b_recycle );
400     vlc_mutex_unlock( &p_resource->lock );
401
402     return p_ret;
403 }
404 vout_thread_t *input_resource_HoldVout( input_resource_t *p_resource )
405 {
406     return HoldVout( p_resource );
407 }
408 void input_resource_HoldVouts( input_resource_t *p_resource, vout_thread_t ***ppp_vout, int *pi_vout )
409 {
410     HoldVouts( p_resource, ppp_vout, pi_vout );
411 }
412 void input_resource_TerminateVout( input_resource_t *p_resource )
413 {
414     input_resource_RequestVout( p_resource, NULL, NULL, false );
415 }
416 bool input_resource_HasVout( input_resource_t *p_resource )
417 {
418     vlc_mutex_lock( &p_resource->lock );
419     assert( !p_resource->p_input );
420     const bool b_vout = p_resource->p_vout_free != NULL;
421     vlc_mutex_unlock( &p_resource->lock );
422
423     return b_vout;
424 }
425
426 /* */
427 aout_instance_t *input_resource_RequestAout( input_resource_t *p_resource, aout_instance_t *p_aout )
428 {
429     vlc_mutex_lock( &p_resource->lock );
430     aout_instance_t *p_ret = RequestAout( p_resource, p_aout );
431     vlc_mutex_unlock( &p_resource->lock );
432
433     return p_ret;
434 }
435 aout_instance_t *input_resource_HoldAout( input_resource_t *p_resource )
436 {
437     vlc_mutex_lock( &p_resource->lock );
438     aout_instance_t *p_ret = HoldAout( p_resource );
439     vlc_mutex_unlock( &p_resource->lock );
440
441     return p_ret;
442 }
443 /* */
444 sout_instance_t *input_resource_RequestSout( input_resource_t *p_resource, sout_instance_t *p_sout, const char *psz_sout )
445 {
446     vlc_mutex_lock( &p_resource->lock );
447     sout_instance_t *p_ret = RequestSout( p_resource, p_sout, psz_sout );
448     vlc_mutex_unlock( &p_resource->lock );
449
450     return p_ret;
451 }
452 void input_resource_TerminateSout( input_resource_t *p_resource )
453 {
454     input_resource_RequestSout( p_resource, NULL, NULL );
455 }
456