1 /*****************************************************************************
2 * engine.c : Run the playlist and handle its control
3 *****************************************************************************
4 * Copyright (C) 1999-2008 VLC authors and VideoLAN
6 * Authors: Samuel Hocevar <sam@zoy.org>
7 * Clément Stenac <zorglub@videolan.org>
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.
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.
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 *****************************************************************************/
30 #include <vlc_common.h>
32 #include <vlc_playlist.h>
33 #include <vlc_interface.h>
34 #include "playlist_internal.h"
35 #include "stream_output/stream_output.h" /* sout_DeleteInstance */
36 #include <math.h> /* for fabs() */
38 /*****************************************************************************
40 *****************************************************************************/
41 static void VariablesInit( playlist_t *p_playlist );
43 static int RandomCallback( vlc_object_t *p_this, char const *psz_cmd,
44 vlc_value_t oldval, vlc_value_t newval, void *a )
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;
53 pl_priv(p_playlist)->b_reset_currently_playing = true;
54 vlc_cond_signal( &pl_priv(p_playlist)->signal );
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)
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 );
64 ResyncCurrentIndex( p_playlist, p_new );
71 static int RateCallback( vlc_object_t *p_this, char const *psz_cmd,
72 vlc_value_t oldval, vlc_value_t newval, void *p )
74 (void)psz_cmd; (void)oldval;(void)p;
75 playlist_t *p_playlist = (playlist_t*)p_this;
79 if( pl_priv(p_playlist)->p_input == NULL )
85 var_SetFloat( pl_priv( p_playlist )->p_input, "rate", newval.f_float );
90 static int RateOffsetCallback( vlc_object_t *obj, char const *psz_cmd,
91 vlc_value_t oldval, vlc_value_t newval, void *p_data )
93 playlist_t *p_playlist = (playlist_t *)obj;
94 VLC_UNUSED(oldval); VLC_UNUSED(p_data); VLC_UNUSED(newval);
96 static const float pf_rate[] = {
97 1.0/64, 1.0/32, 1.0/16, 1.0/8, 1.0/4, 1.0/3, 1.0/2, 2.0/3,
99 3.0/2, 2.0/1, 3.0/1, 4.0/1, 8.0/1, 16.0/1, 32.0/1, 64.0/1,
101 const size_t i_rate_count = sizeof(pf_rate)/sizeof(*pf_rate);
104 struct input_thread_t *input;
107 input = pl_priv( p_playlist )->p_input;
108 f_rate = var_GetFloat( input ? (vlc_object_t *)input : obj, "rate" );
111 if( !strcmp( psz_cmd, "rate-faster" ) )
113 /* compensate for input rounding errors */
114 float r = f_rate * 1.1;
115 for( size_t i = 0; i < i_rate_count; i++ )
124 /* compensate for input rounding errors */
125 float r = f_rate * .9;
126 for( size_t i = 1; i < i_rate_count; i++ )
127 if( r <= pf_rate[i] )
129 f_rate = pf_rate[i - 1];
134 var_SetFloat( p_playlist, "rate", f_rate );
138 static int VideoSplitterCallback( vlc_object_t *p_this, char const *psz_cmd,
139 vlc_value_t oldval, vlc_value_t newval, void *p_data )
141 playlist_t *p_playlist = (playlist_t*)p_this;
142 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(p_data); VLC_UNUSED(newval);
146 /* Force the input to restart the video ES to force a vout recreation */
147 input_thread_t *p_input = pl_priv( p_playlist )->p_input;
150 const double f_position = var_GetFloat( p_input, "position" );
151 input_Control( p_input, INPUT_RESTART_ES, -VIDEO_ES );
152 var_SetFloat( p_input, "position", f_position );
162 * Create a playlist structure.
163 * \param p_parent the vlc object that is to be the parent of this playlist
164 * \return a pointer to the created playlist, or NULL on error
166 playlist_t * playlist_Create( vlc_object_t *p_parent )
168 playlist_t *p_playlist;
169 playlist_private_t *p;
171 /* Allocate structure */
172 p = vlc_custom_create( p_parent, sizeof( *p ), "playlist" );
176 assert( offsetof( playlist_private_t, public_data ) == 0 );
177 p_playlist = &p->public_data;
178 TAB_INIT( pl_priv(p_playlist)->i_sds, pl_priv(p_playlist)->pp_sds );
180 libvlc_priv(p_parent->p_libvlc)->p_playlist = p_playlist;
182 VariablesInit( p_playlist );
183 vlc_mutex_init( &p->lock );
184 vlc_cond_init( &p->signal );
187 /* Initialise data structures */
188 pl_priv(p_playlist)->i_last_playlist_id = 0;
189 pl_priv(p_playlist)->p_input = NULL;
191 ARRAY_INIT( p_playlist->items );
192 ARRAY_INIT( p_playlist->all_items );
193 ARRAY_INIT( pl_priv(p_playlist)->items_to_delete );
194 ARRAY_INIT( p_playlist->current );
196 p_playlist->i_current_index = 0;
197 pl_priv(p_playlist)->b_reset_currently_playing = true;
198 pl_priv(p_playlist)->last_rebuild_date = 0;
200 pl_priv(p_playlist)->b_tree = var_InheritBool( p_parent, "playlist-tree" );
202 pl_priv(p_playlist)->b_doing_ml = false;
204 pl_priv(p_playlist)->b_auto_preparse =
205 var_InheritBool( p_parent, "auto-preparse" );
208 p->p_fetcher = playlist_fetcher_New( p_playlist );
209 if( unlikely(p->p_fetcher == NULL) )
211 msg_Err( p_playlist, "cannot create fetcher" );
212 p->p_preparser = NULL;
216 p->p_preparser = playlist_preparser_New( p_playlist, p->p_fetcher );
217 if( unlikely(p->p_preparser == NULL) )
218 msg_Err( p_playlist, "cannot create preparser" );
221 /* Create the root node */
223 p_playlist->p_root = playlist_NodeCreate( p_playlist, NULL, NULL,
224 PLAYLIST_END, 0, NULL );
226 if( !p_playlist->p_root ) return NULL;
228 /* Create currently playing items node */
230 p_playlist->p_playing = playlist_NodeCreate(
231 p_playlist, _( "Playlist" ), p_playlist->p_root,
232 PLAYLIST_END, PLAYLIST_RO_FLAG, NULL );
236 if( !p_playlist->p_playing ) return NULL;
238 /* Create media library node */
239 const bool b_ml = var_InheritBool( p_parent, "media-library");
243 p_playlist->p_media_library = playlist_NodeCreate(
244 p_playlist, _( "Media Library" ), p_playlist->p_root,
245 PLAYLIST_END, PLAYLIST_RO_FLAG, NULL );
248 if(!p_playlist->p_media_library ) return NULL;
252 p_playlist->p_media_library = NULL;
255 p_playlist->p_root_category = p_playlist->p_root;
256 p_playlist->p_root_onelevel = p_playlist->p_root;
257 p_playlist->p_local_category = p_playlist->p_playing;
258 p_playlist->p_local_onelevel = p_playlist->p_playing;
259 p_playlist->p_ml_category = p_playlist->p_media_library;
260 p_playlist->p_ml_onelevel = p_playlist->p_media_library;;
263 pl_priv(p_playlist)->status.p_item = NULL;
264 pl_priv(p_playlist)->status.p_node = p_playlist->p_playing;
265 pl_priv(p_playlist)->request.b_request = false;
266 pl_priv(p_playlist)->status.i_status = PLAYLIST_STOPPED;
270 const bool b_auto_preparse = pl_priv(p_playlist)->b_auto_preparse;
271 pl_priv(p_playlist)->b_auto_preparse = false;
272 playlist_MLLoad( p_playlist );
273 pl_priv(p_playlist)->b_auto_preparse = b_auto_preparse;
281 * This is not thread-safe. Any reference to the playlist is assumed gone.
282 * (In particular, all interface and services threads must have been joined).
284 * \param p_playlist the playlist object
286 void playlist_Destroy( playlist_t *p_playlist )
288 playlist_private_t *p_sys = pl_priv(p_playlist);
290 msg_Dbg( p_playlist, "destroying" );
291 if( p_sys->p_preparser )
292 playlist_preparser_Delete( p_sys->p_preparser );
293 if( p_sys->p_fetcher )
294 playlist_fetcher_Delete( p_sys->p_fetcher );
296 /* Already cleared when deactivating (if activated anyway) */
297 assert( !p_sys->p_input );
298 assert( !p_sys->p_input_resource );
300 vlc_cond_destroy( &p_sys->signal );
301 vlc_mutex_destroy( &p_sys->lock );
303 /* Remove all remaining items */
304 FOREACH_ARRAY( playlist_item_t *p_del, p_playlist->all_items )
305 free( p_del->pp_children );
306 vlc_gc_decref( p_del->p_input );
309 ARRAY_RESET( p_playlist->all_items );
310 FOREACH_ARRAY( playlist_item_t *p_del, p_sys->items_to_delete )
311 free( p_del->pp_children );
312 vlc_gc_decref( p_del->p_input );
315 ARRAY_RESET( p_sys->items_to_delete );
317 ARRAY_RESET( p_playlist->items );
318 ARRAY_RESET( p_playlist->current );
320 vlc_object_release( p_playlist );
323 /** Get current playing input.
325 input_thread_t * playlist_CurrentInput( playlist_t * p_playlist )
327 input_thread_t * p_input;
329 p_input = pl_priv(p_playlist)->p_input;
330 if( p_input ) vlc_object_hold( p_input );
339 /** Accessor for status item and status nodes.
341 playlist_item_t * get_current_status_item( playlist_t * p_playlist )
345 return pl_priv(p_playlist)->status.p_item;
348 playlist_item_t * get_current_status_node( playlist_t * p_playlist )
352 return pl_priv(p_playlist)->status.p_node;
355 void set_current_status_item( playlist_t * p_playlist,
356 playlist_item_t * p_item )
360 if( pl_priv(p_playlist)->status.p_item &&
361 pl_priv(p_playlist)->status.p_item->i_flags & PLAYLIST_REMOVE_FLAG &&
362 pl_priv(p_playlist)->status.p_item != p_item )
364 /* It's unsafe given current design to delete a playlist item :(
365 playlist_ItemDelete( pl_priv(p_playlist)->status.p_item ); */
367 pl_priv(p_playlist)->status.p_item = p_item;
370 void set_current_status_node( playlist_t * p_playlist,
371 playlist_item_t * p_node )
375 if( pl_priv(p_playlist)->status.p_node &&
376 pl_priv(p_playlist)->status.p_node->i_flags & PLAYLIST_REMOVE_FLAG &&
377 pl_priv(p_playlist)->status.p_node != p_node )
379 /* It's unsafe given current design to delete a playlist item :(
380 playlist_ItemDelete( pl_priv(p_playlist)->status.p_node ); */
382 pl_priv(p_playlist)->status.p_node = p_node;
385 static input_thread_t *playlist_FindInput( vlc_object_t *object )
387 assert( object == VLC_OBJECT(pl_Get(object)) );
388 return playlist_CurrentInput( (playlist_t *)object );
391 static void VariablesInit( playlist_t *p_playlist )
393 /* These variables control updates */
394 var_Create( p_playlist, "intf-change", VLC_VAR_BOOL );
395 var_SetBool( p_playlist, "intf-change", true );
397 var_Create( p_playlist, "item-change", VLC_VAR_ADDRESS );
398 var_Create( p_playlist, "leaf-to-parent", VLC_VAR_INTEGER );
400 var_Create( p_playlist, "playlist-item-deleted", VLC_VAR_INTEGER );
401 var_SetInteger( p_playlist, "playlist-item-deleted", -1 );
403 var_Create( p_playlist, "playlist-item-append", VLC_VAR_ADDRESS );
405 var_Create( p_playlist, "item-current", VLC_VAR_ADDRESS );
406 var_Create( p_playlist, "input-current", VLC_VAR_ADDRESS );
408 var_Create( p_playlist, "activity", VLC_VAR_INTEGER );
409 var_SetInteger( p_playlist, "activity", 0 );
411 /* Variables to control playback */
412 var_Create( p_playlist, "playlist-autostart", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
413 var_Create( p_playlist, "play-and-stop", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
414 var_Create( p_playlist, "play-and-exit", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
415 var_Create( p_playlist, "random", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
416 var_Create( p_playlist, "repeat", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
417 var_Create( p_playlist, "loop", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
419 var_Create( p_playlist, "rate", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
420 var_Create( p_playlist, "rate-slower", VLC_VAR_VOID );
421 var_Create( p_playlist, "rate-faster", VLC_VAR_VOID );
422 var_AddCallback( p_playlist, "rate", RateCallback, NULL );
423 var_AddCallback( p_playlist, "rate-slower", RateOffsetCallback, NULL );
424 var_AddCallback( p_playlist, "rate-faster", RateOffsetCallback, NULL );
426 var_Create( p_playlist, "video-splitter", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
427 var_AddCallback( p_playlist, "video-splitter", VideoSplitterCallback, NULL );
429 var_AddCallback( p_playlist, "random", RandomCallback, NULL );
432 var_Create( p_playlist, "album-art", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
434 /* Variables to preserve video output parameters */
435 var_Create( p_playlist, "fullscreen", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
436 var_Create( p_playlist, "video-on-top", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
438 /* Audio output parameters */
439 var_Create( p_playlist, "mute", VLC_VAR_BOOL );
440 var_Create( p_playlist, "volume", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
441 /* FIXME: horrible hack for audio output interface code */
442 var_Create( p_playlist, "find-input-callback", VLC_VAR_ADDRESS );
443 var_SetAddress( p_playlist, "find-input-callback", playlist_FindInput );
446 playlist_item_t * playlist_CurrentPlayingItem( playlist_t * p_playlist )
450 return pl_priv(p_playlist)->status.p_item;
453 int playlist_Status( playlist_t * p_playlist )
457 return pl_priv(p_playlist)->status.i_status;