playlist: avoid implicit double upconversion
[vlc.git] / src / playlist / engine.c
1 /*****************************************************************************
2  * engine.c : Run the playlist and handle its control
3  *****************************************************************************
4  * Copyright (C) 1999-2008 VLC authors and VideoLAN
5  *
6  * Authors: Samuel Hocevar <sam@zoy.org>
7  *          ClĂ©ment Stenac <zorglub@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <stddef.h>
29 #include <assert.h>
30
31 #include <vlc_common.h>
32 #include <vlc_sout.h>
33 #include <vlc_playlist.h>
34 #include <vlc_interface.h>
35 #include "playlist_internal.h"
36 #include "input/resource.h"
37
38 /*****************************************************************************
39  * Local prototypes
40  *****************************************************************************/
41 static void VariablesInit( playlist_t *p_playlist );
42
43 static int RandomCallback( vlc_object_t *p_this, char const *psz_cmd,
44                            vlc_value_t oldval, vlc_value_t newval, void *a )
45 {
46     (void)psz_cmd; (void)oldval; (void)newval; (void)a;
47     playlist_t *p_playlist = (playlist_t*)p_this;
48     bool random = newval.b_bool;
49
50     PL_LOCK;
51
52     if( !random ) {
53         pl_priv(p_playlist)->b_reset_currently_playing = true;
54         vlc_cond_signal( &pl_priv(p_playlist)->signal );
55     } else {
56         /* Shuffle and sync the playlist on activation of random mode.
57          * This preserves the current playing item, so that the user
58          * can return to it if needed. (See #4472)
59          */
60         playlist_private_t *p_sys = pl_priv(p_playlist);
61         playlist_item_t *p_new = p_sys->status.p_item;
62         ResetCurrentlyPlaying( p_playlist, NULL );
63         if( p_new )
64             ResyncCurrentIndex( p_playlist, p_new );
65     }
66
67     PL_UNLOCK;
68     return VLC_SUCCESS;
69 }
70
71 /**
72  * When there are one or more pending corks, playback should be paused.
73  * This is used for audio policy.
74  * \warning Always add and remove a cork with var_IncInteger() and var_DecInteger().
75  * var_Get() and var_Set() are prone to race conditions.
76  */
77 static int CorksCallback( vlc_object_t *obj, char const *var,
78                           vlc_value_t old, vlc_value_t cur, void *dummy )
79 {
80     playlist_t *pl = (playlist_t *)obj;
81
82     msg_Dbg( obj, "corks count: %"PRId64" -> %"PRId64, old.i_int, cur.i_int );
83     if( !old.i_int == !cur.i_int )
84         return VLC_SUCCESS; /* nothing to do */
85
86     if( cur.i_int )
87     {
88         if( var_InheritBool( obj, "playlist-cork" ) )
89         {
90             msg_Dbg( obj, "corked" );
91             playlist_Pause( pl );
92         }
93         else
94             msg_Dbg( obj, "not corked" );
95     }
96     else
97         msg_Dbg( obj, "uncorked" );
98
99     (void) var; (void) dummy;
100     return VLC_SUCCESS;
101 }
102
103 static int RateCallback( vlc_object_t *p_this, char const *psz_cmd,
104                          vlc_value_t oldval, vlc_value_t newval, void *p )
105 {
106     (void)psz_cmd; (void)oldval;(void)p;
107     playlist_t *p_playlist = (playlist_t*)p_this;
108
109     PL_LOCK;
110
111     if( pl_priv(p_playlist)->p_input == NULL )
112     {
113         PL_UNLOCK;
114         return VLC_SUCCESS;
115     }
116
117     var_SetFloat( pl_priv( p_playlist )->p_input, "rate", newval.f_float );
118     PL_UNLOCK;
119     return VLC_SUCCESS;
120 }
121
122 static int RateOffsetCallback( vlc_object_t *obj, char const *psz_cmd,
123                                vlc_value_t oldval, vlc_value_t newval, void *p_data )
124 {
125     playlist_t *p_playlist = (playlist_t *)obj;
126     VLC_UNUSED(oldval); VLC_UNUSED(p_data); VLC_UNUSED(newval);
127
128     static const float pf_rate[] = {
129         1.0/64, 1.0/32, 1.0/16, 1.0/8, 1.0/4, 1.0/3, 1.0/2, 2.0/3,
130         1.0/1,
131         3.0/2, 2.0/1, 3.0/1, 4.0/1, 8.0/1, 16.0/1, 32.0/1, 64.0/1,
132     };
133     const size_t i_rate_count = sizeof(pf_rate)/sizeof(*pf_rate);
134
135     float f_rate;
136     struct input_thread_t *input;
137
138     PL_LOCK;
139     input = pl_priv( p_playlist )->p_input;
140     f_rate = var_GetFloat( input ? (vlc_object_t *)input : obj, "rate" );
141     PL_UNLOCK;
142
143     if( !strcmp( psz_cmd, "rate-faster" ) )
144     {
145         /* compensate for input rounding errors */
146         float r = f_rate * 1.1f;
147         for( size_t i = 0; i < i_rate_count; i++ )
148             if( r < pf_rate[i] )
149             {
150                 f_rate = pf_rate[i];
151                 break;
152             }
153     }
154     else
155     {
156         /* compensate for input rounding errors */
157         float r = f_rate * .9f;
158         for( size_t i = 1; i < i_rate_count; i++ )
159             if( r <= pf_rate[i] )
160             {
161                 f_rate = pf_rate[i - 1];
162                 break;
163             }
164     }
165
166     var_SetFloat( p_playlist, "rate", f_rate );
167     return VLC_SUCCESS;
168 }
169
170 static int VideoSplitterCallback( vlc_object_t *p_this, char const *psz_cmd,
171                                   vlc_value_t oldval, vlc_value_t newval, void *p_data )
172 {
173     playlist_t *p_playlist = (playlist_t*)p_this;
174     VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(p_data); VLC_UNUSED(newval);
175
176     PL_LOCK;
177
178     /* Force the input to restart the video ES to force a vout recreation */
179     input_thread_t *p_input = pl_priv( p_playlist )->p_input;
180     if( p_input )
181     {
182         const double f_position = var_GetFloat( p_input, "position" );
183         input_Control( p_input, INPUT_RESTART_ES, -VIDEO_ES );
184         var_SetFloat( p_input, "position", f_position );
185     }
186
187     PL_UNLOCK;
188     return VLC_SUCCESS;
189 }
190
191 /**
192  * Create playlist
193  *
194  * Create a playlist structure.
195  * \param p_parent the vlc object that is to be the parent of this playlist
196  * \return a pointer to the created playlist, or NULL on error
197  */
198 playlist_t *playlist_Create( vlc_object_t *p_parent )
199 {
200     playlist_t *p_playlist;
201     playlist_private_t *p;
202
203     /* Allocate structure */
204     p = vlc_custom_create( p_parent, sizeof( *p ), "playlist" );
205     if( !p )
206         return NULL;
207
208     assert( offsetof( playlist_private_t, public_data ) == 0 );
209     p_playlist = &p->public_data;
210     TAB_INIT( pl_priv(p_playlist)->i_sds, pl_priv(p_playlist)->pp_sds );
211
212     VariablesInit( p_playlist );
213     vlc_mutex_init( &p->lock );
214     vlc_cond_init( &p->signal );
215     p->killed = false;
216
217     /* Initialise data structures */
218     pl_priv(p_playlist)->i_last_playlist_id = 0;
219     pl_priv(p_playlist)->p_input = NULL;
220
221     ARRAY_INIT( p_playlist->items );
222     ARRAY_INIT( p_playlist->all_items );
223     ARRAY_INIT( pl_priv(p_playlist)->items_to_delete );
224     ARRAY_INIT( p_playlist->current );
225
226     p_playlist->i_current_index = 0;
227     pl_priv(p_playlist)->b_reset_currently_playing = true;
228
229     pl_priv(p_playlist)->b_tree = var_InheritBool( p_parent, "playlist-tree" );
230
231     pl_priv(p_playlist)->b_doing_ml = false;
232
233     /* Create the root node */
234     PL_LOCK;
235     p_playlist->p_root = playlist_NodeCreate( p_playlist, NULL, NULL,
236                                     PLAYLIST_END, 0, NULL );
237     PL_UNLOCK;
238     if( !p_playlist->p_root ) return NULL;
239
240     /* Create currently playing items node */
241     PL_LOCK;
242     p_playlist->p_playing = playlist_NodeCreate(
243         p_playlist, _( "Playlist" ), p_playlist->p_root,
244         PLAYLIST_END, PLAYLIST_RO_FLAG, NULL );
245
246     PL_UNLOCK;
247
248     if( !p_playlist->p_playing ) return NULL;
249
250     /* Create media library node */
251     const bool b_ml = var_InheritBool( p_parent, "media-library");
252     if( b_ml )
253     {
254         PL_LOCK;
255         p_playlist->p_media_library = playlist_NodeCreate(
256             p_playlist, _( "Media Library" ), p_playlist->p_root,
257             PLAYLIST_END, PLAYLIST_RO_FLAG, NULL );
258         PL_UNLOCK;
259     }
260     else
261     {
262         p_playlist->p_media_library = NULL;
263     }
264
265     p_playlist->p_root_category = p_playlist->p_root;
266     p_playlist->p_root_onelevel = p_playlist->p_root;
267     p_playlist->p_local_category = p_playlist->p_playing;
268     p_playlist->p_local_onelevel = p_playlist->p_playing;
269     p_playlist->p_ml_category = p_playlist->p_media_library;
270     p_playlist->p_ml_onelevel = p_playlist->p_media_library;;
271
272     /* Initial status */
273     pl_priv(p_playlist)->status.p_item = NULL;
274     pl_priv(p_playlist)->status.p_node = p_playlist->p_playing;
275     pl_priv(p_playlist)->request.b_request = false;
276     pl_priv(p_playlist)->status.i_status = PLAYLIST_STOPPED;
277
278     if(b_ml)
279         playlist_MLLoad( p_playlist );
280
281     /* Preparser (and meta retriever) _after_ the Media Library*/
282     if( var_InheritBool( p_parent, "auto-preparse" ) )
283     {
284         p->p_preparser = playlist_preparser_New( VLC_OBJECT(p_playlist) );
285         if( unlikely(p->p_preparser == NULL) )
286             msg_Err( p_playlist, "cannot create preparser" );
287     }
288
289     /* Input resources */
290     p->p_input_resource = input_resource_New( VLC_OBJECT( p_playlist ) );
291     if( unlikely(p->p_input_resource == NULL) )
292         abort();
293
294     /* Audio output (needed for volume and device controls). */
295     audio_output_t *aout = input_resource_GetAout( p->p_input_resource );
296     if( aout != NULL )
297         input_resource_PutAout( p->p_input_resource, aout );
298
299     /* Thread */
300     playlist_Activate (p_playlist);
301
302     /* Add service discovery modules */
303     char *mods = var_InheritString( p_playlist, "services-discovery" );
304     if( mods != NULL )
305     {
306         char *s = mods, *m;
307         while( (m = strsep( &s, " :," )) != NULL )
308             playlist_ServicesDiscoveryAdd( p_playlist, m );
309         free( mods );
310     }
311
312     return p_playlist;
313 }
314
315 /**
316  * Destroy playlist.
317  * This is not thread-safe. Any reference to the playlist is assumed gone.
318  * (In particular, all interface and services threads must have been joined).
319  *
320  * \param p_playlist the playlist object
321  */
322 void playlist_Destroy( playlist_t *p_playlist )
323 {
324     playlist_private_t *p_sys = pl_priv(p_playlist);
325
326     /* Remove all services discovery */
327     playlist_ServicesDiscoveryKillAll( p_playlist );
328
329     msg_Dbg( p_playlist, "destroying" );
330
331     playlist_Deactivate( p_playlist );
332     if( p_sys->p_preparser )
333         playlist_preparser_Delete( p_sys->p_preparser );
334
335     /* Release input resources */
336     assert( p_sys->p_input == NULL );
337     input_resource_Release( p_sys->p_input_resource );
338
339     if( p_playlist->p_media_library != NULL )
340         playlist_MLDump( p_playlist );
341
342     PL_LOCK;
343     /* Release the current node */
344     set_current_status_node( p_playlist, NULL );
345     /* Release the current item */
346     set_current_status_item( p_playlist, NULL );
347     PL_UNLOCK;
348
349     vlc_cond_destroy( &p_sys->signal );
350     vlc_mutex_destroy( &p_sys->lock );
351
352     /* Remove all remaining items */
353     FOREACH_ARRAY( playlist_item_t *p_del, p_playlist->all_items )
354         free( p_del->pp_children );
355         vlc_gc_decref( p_del->p_input );
356         free( p_del );
357     FOREACH_END();
358     ARRAY_RESET( p_playlist->all_items );
359     FOREACH_ARRAY( playlist_item_t *p_del, p_sys->items_to_delete )
360         free( p_del->pp_children );
361         vlc_gc_decref( p_del->p_input );
362         free( p_del );
363     FOREACH_END();
364     ARRAY_RESET( p_sys->items_to_delete );
365
366     ARRAY_RESET( p_playlist->items );
367     ARRAY_RESET( p_playlist->current );
368
369     vlc_object_release( p_playlist );
370 }
371
372 /** Get current playing input.
373  */
374 input_thread_t * playlist_CurrentInput( playlist_t * p_playlist )
375 {
376     input_thread_t * p_input;
377     PL_LOCK;
378     p_input = pl_priv(p_playlist)->p_input;
379     if( p_input ) vlc_object_hold( p_input );
380     PL_UNLOCK;
381     return p_input;
382 }
383
384 /**
385  * @}
386  */
387
388 /** Accessor for status item and status nodes.
389  */
390 playlist_item_t * get_current_status_item( playlist_t * p_playlist )
391 {
392     PL_ASSERT_LOCKED;
393
394     return pl_priv(p_playlist)->status.p_item;
395 }
396
397 playlist_item_t * get_current_status_node( playlist_t * p_playlist )
398 {
399     PL_ASSERT_LOCKED;
400
401     return pl_priv(p_playlist)->status.p_node;
402 }
403
404 void set_current_status_item( playlist_t * p_playlist,
405     playlist_item_t * p_item )
406 {
407     PL_ASSERT_LOCKED;
408
409     if( pl_priv(p_playlist)->status.p_item &&
410         pl_priv(p_playlist)->status.p_item->i_flags & PLAYLIST_REMOVE_FLAG &&
411         pl_priv(p_playlist)->status.p_item != p_item )
412     {
413         /* It's unsafe given current design to delete a playlist item :(
414         playlist_ItemDelete( pl_priv(p_playlist)->status.p_item ); */
415     }
416     pl_priv(p_playlist)->status.p_item = p_item;
417 }
418
419 void set_current_status_node( playlist_t * p_playlist,
420     playlist_item_t * p_node )
421 {
422     PL_ASSERT_LOCKED;
423
424     if( pl_priv(p_playlist)->status.p_node &&
425         pl_priv(p_playlist)->status.p_node->i_flags & PLAYLIST_REMOVE_FLAG &&
426         pl_priv(p_playlist)->status.p_node != p_node )
427     {
428         /* It's unsafe given current design to delete a playlist item :(
429         playlist_ItemDelete( pl_priv(p_playlist)->status.p_node ); */
430     }
431     pl_priv(p_playlist)->status.p_node = p_node;
432 }
433
434 static void VariablesInit( playlist_t *p_playlist )
435 {
436     /* These variables control updates */
437     var_Create( p_playlist, "intf-change", VLC_VAR_BOOL );
438     var_SetBool( p_playlist, "intf-change", true );
439
440     var_Create( p_playlist, "item-change", VLC_VAR_ADDRESS );
441     var_Create( p_playlist, "leaf-to-parent", VLC_VAR_INTEGER );
442
443     var_Create( p_playlist, "playlist-item-deleted", VLC_VAR_INTEGER );
444     var_SetInteger( p_playlist, "playlist-item-deleted", -1 );
445
446     var_Create( p_playlist, "playlist-item-append", VLC_VAR_ADDRESS );
447
448     var_Create( p_playlist, "input-current", VLC_VAR_ADDRESS );
449
450     var_Create( p_playlist, "activity", VLC_VAR_VOID );
451
452     /* Variables to control playback */
453     var_Create( p_playlist, "playlist-autostart", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
454     var_Create( p_playlist, "random", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
455     var_AddCallback( p_playlist, "random", RandomCallback, NULL );
456     var_Create( p_playlist, "repeat", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
457     var_Create( p_playlist, "loop", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
458     var_Create( p_playlist, "corks", VLC_VAR_INTEGER );
459     var_AddCallback( p_playlist, "corks", CorksCallback, NULL );
460
461     var_Create( p_playlist, "rate", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
462     var_AddCallback( p_playlist, "rate", RateCallback, NULL );
463     var_Create( p_playlist, "rate-slower", VLC_VAR_VOID );
464     var_AddCallback( p_playlist, "rate-slower", RateOffsetCallback, NULL );
465     var_Create( p_playlist, "rate-faster", VLC_VAR_VOID );
466     var_AddCallback( p_playlist, "rate-faster", RateOffsetCallback, NULL );
467
468     var_Create( p_playlist, "video-splitter", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
469     var_AddCallback( p_playlist, "video-splitter", VideoSplitterCallback, NULL );
470
471     /* */
472     var_Create( p_playlist, "album-art", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
473     var_Create( p_playlist, "metadata-network-access", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
474
475     /* Variables to preserve video output parameters */
476     var_Create( p_playlist, "fullscreen", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
477     var_Create( p_playlist, "video-on-top", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
478     var_Create( p_playlist, "video-wallpaper", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
479
480     /* Audio output parameters */
481     var_Create( p_playlist, "mute", VLC_VAR_BOOL );
482     var_Create( p_playlist, "volume", VLC_VAR_FLOAT );
483     var_SetFloat( p_playlist, "volume", -1.f );
484 }
485
486 playlist_item_t * playlist_CurrentPlayingItem( playlist_t * p_playlist )
487 {
488     PL_ASSERT_LOCKED;
489
490     return pl_priv(p_playlist)->status.p_item;
491 }
492
493 int playlist_Status( playlist_t * p_playlist )
494 {
495     PL_ASSERT_LOCKED;
496
497     return pl_priv(p_playlist)->status.i_status;
498 }
499