1 /*****************************************************************************
2 * engine.c : Run the playlist and handle its control
3 *****************************************************************************
4 * Copyright (C) 1999-2008 the VideoLAN team
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
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.
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.
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 *****************************************************************************/
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 */
37 /*****************************************************************************
39 *****************************************************************************/
40 static void VariablesInit( playlist_t *p_playlist );
41 static void playlist_Destructor( vlc_object_t * p_this );
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;
51 pl_priv(p_playlist)->b_reset_currently_playing = true;
52 vlc_object_signal_unlocked( p_playlist );
61 * Create a playlist structure.
62 * \param p_parent the vlc object that is to be the parent of this playlist
63 * \return a pointer to the created playlist, or NULL on error
65 playlist_t * playlist_Create( vlc_object_t *p_parent )
67 static const char playlist_name[] = "playlist";
68 playlist_t *p_playlist;
69 playlist_private_t *p;
71 /* Allocate structure */
72 p = vlc_custom_create( p_parent, sizeof( *p ),
73 VLC_OBJECT_GENERIC, playlist_name );
77 assert( offsetof( playlist_private_t, public_data ) == 0 );
78 p_playlist = &p->public_data;
79 TAB_INIT( pl_priv(p_playlist)->i_sds, pl_priv(p_playlist)->pp_sds );
81 libvlc_priv(p_parent->p_libvlc)->p_playlist = p_playlist;
83 VariablesInit( p_playlist );
85 /* Initialise data structures */
86 pl_priv(p_playlist)->i_last_playlist_id = 0;
87 pl_priv(p_playlist)->p_input = NULL;
89 ARRAY_INIT( p_playlist->items );
90 ARRAY_INIT( p_playlist->all_items );
91 ARRAY_INIT( pl_priv(p_playlist)->items_to_delete );
92 ARRAY_INIT( p_playlist->current );
94 p_playlist->i_current_index = 0;
95 pl_priv(p_playlist)->b_reset_currently_playing = true;
96 pl_priv(p_playlist)->last_rebuild_date = 0;
98 pl_priv(p_playlist)->b_tree = var_CreateGetBool( p_playlist, "playlist-tree" );
100 pl_priv(p_playlist)->b_doing_ml = false;
102 pl_priv(p_playlist)->b_auto_preparse =
103 var_CreateGetBool( p_playlist, "auto-preparse" ) ;
105 PL_LOCK; /* playlist_NodeCreate will check for it */
106 p_playlist->p_root_category = playlist_NodeCreate( p_playlist, NULL, NULL,
108 p_playlist->p_root_onelevel = playlist_NodeCreate( p_playlist, NULL, NULL,
109 0, p_playlist->p_root_category->p_input );
112 if( !p_playlist->p_root_category || !p_playlist->p_root_onelevel )
115 /* Create playlist and media library */
116 PL_LOCK; /* playlist_NodesPairCreate will check for it */
117 playlist_NodesPairCreate( p_playlist, _( "Playlist" ),
118 &p_playlist->p_local_category,
119 &p_playlist->p_local_onelevel, false );
122 p_playlist->p_local_category->i_flags |= PLAYLIST_RO_FLAG;
123 p_playlist->p_local_onelevel->i_flags |= PLAYLIST_RO_FLAG;
125 if( !p_playlist->p_local_category || !p_playlist->p_local_onelevel ||
126 !p_playlist->p_local_category->p_input ||
127 !p_playlist->p_local_onelevel->p_input )
130 if( config_GetInt( p_playlist, "media-library") )
132 PL_LOCK; /* playlist_NodesPairCreate will check for it */
133 playlist_NodesPairCreate( p_playlist, _( "Media Library" ),
134 &p_playlist->p_ml_category,
135 &p_playlist->p_ml_onelevel, false );
138 if(!p_playlist->p_ml_category || !p_playlist->p_ml_onelevel)
141 p_playlist->p_ml_category->i_flags |= PLAYLIST_RO_FLAG;
142 p_playlist->p_ml_onelevel->i_flags |= PLAYLIST_RO_FLAG;
146 p_playlist->p_ml_category = p_playlist->p_ml_onelevel = NULL;
150 pl_priv(p_playlist)->status.p_item = NULL;
151 pl_priv(p_playlist)->status.p_node = p_playlist->p_local_onelevel;
152 pl_priv(p_playlist)->request.b_request = false;
153 pl_priv(p_playlist)->status.i_status = PLAYLIST_STOPPED;
155 pl_priv(p_playlist)->b_auto_preparse = false;
156 playlist_MLLoad( p_playlist );
157 pl_priv(p_playlist)->b_auto_preparse = true;
159 vlc_object_set_destructor( p_playlist, playlist_Destructor );
167 * Destroy a playlist structure.
168 * \param p_playlist the playlist object
172 static void playlist_Destructor( vlc_object_t * p_this )
174 playlist_t *p_playlist = (playlist_t *)p_this;
175 playlist_private_t *p_sys = pl_priv(p_playlist);
177 assert( !p_sys->p_sout );
178 assert( !p_sys->p_preparser );
179 assert( !p_sys->p_fetcher );
181 msg_Err( p_this, "Destroyed" );
185 static int InputEvent( vlc_object_t *p_this, char const *psz_cmd,
186 vlc_value_t oldval, vlc_value_t newval, void *p_data )
188 VLC_UNUSED(p_this); VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
189 playlist_t *p_playlist = p_data;
191 if( newval.i_int != INPUT_EVENT_STATE &&
192 newval.i_int != INPUT_EVENT_ES )
197 vlc_object_signal_unlocked( p_playlist );
204 void playlist_release_current_input( playlist_t * p_playlist )
208 if( !pl_priv(p_playlist)->p_input ) return;
210 input_thread_t * p_input = pl_priv(p_playlist)->p_input;
212 var_DelCallback( p_input, "intf-event", InputEvent, p_playlist );
213 pl_priv(p_playlist)->p_input = NULL;
215 /* Release the playlist lock, because we may get stuck
216 * in vlc_object_release() for some time. */
218 vlc_thread_join( p_input );
219 vlc_object_release( p_input );
223 void playlist_set_current_input(
224 playlist_t * p_playlist, input_thread_t * p_input )
228 playlist_release_current_input( p_playlist );
232 vlc_object_hold( p_input );
233 pl_priv(p_playlist)->p_input = p_input;
235 var_AddCallback( p_input, "intf-event", InputEvent, p_playlist );
239 /** Get current playing input.
241 input_thread_t * playlist_CurrentInput( playlist_t * p_playlist )
243 input_thread_t * p_input;
245 p_input = pl_priv(p_playlist)->p_input;
246 if( p_input ) vlc_object_hold( p_input );
255 /** Accessor for status item and status nodes.
257 playlist_item_t * get_current_status_item( playlist_t * p_playlist )
261 return pl_priv(p_playlist)->status.p_item;
264 playlist_item_t * get_current_status_node( playlist_t * p_playlist )
268 return pl_priv(p_playlist)->status.p_node;
271 void set_current_status_item( playlist_t * p_playlist,
272 playlist_item_t * p_item )
276 if( pl_priv(p_playlist)->status.p_item &&
277 pl_priv(p_playlist)->status.p_item->i_flags & PLAYLIST_REMOVE_FLAG &&
278 pl_priv(p_playlist)->status.p_item != p_item )
280 /* It's unsafe given current design to delete a playlist item :(
281 playlist_ItemDelete( pl_priv(p_playlist)->status.p_item ); */
283 pl_priv(p_playlist)->status.p_item = p_item;
286 void set_current_status_node( playlist_t * p_playlist,
287 playlist_item_t * p_node )
291 if( pl_priv(p_playlist)->status.p_node &&
292 pl_priv(p_playlist)->status.p_node->i_flags & PLAYLIST_REMOVE_FLAG &&
293 pl_priv(p_playlist)->status.p_node != p_node )
295 /* It's unsafe given current design to delete a playlist item :(
296 playlist_ItemDelete( pl_priv(p_playlist)->status.p_node ); */
298 pl_priv(p_playlist)->status.p_node = p_node;
304 * Main loop for the playlist. It should be entered with the
305 * playlist lock (otherwise input event may be lost)
306 * \param p_playlist the playlist object
309 void playlist_MainLoop( playlist_t *p_playlist )
311 bool b_playexit = var_GetBool( p_playlist, "play-and-exit" );
315 if( pl_priv(p_playlist)->b_reset_currently_playing &&
316 mdate() - pl_priv(p_playlist)->last_rebuild_date > 30000 ) // 30 ms
318 ResetCurrentlyPlaying( p_playlist, var_GetBool( p_playlist, "random" ),
319 get_current_status_item( p_playlist ) );
320 pl_priv(p_playlist)->last_rebuild_date = mdate();
324 /* If there is an input, check that it doesn't need to die. */
325 if( pl_priv(p_playlist)->p_input )
327 input_thread_t *p_input = pl_priv(p_playlist)->p_input;
329 if( pl_priv(p_playlist)->request.b_request && !p_input->b_die )
331 PL_DEBUG( "incoming request - stopping current input" );
332 input_StopThread( p_input );
335 /* This input is dead. Remove it ! */
336 if( p_input->b_dead )
338 sout_instance_t **pp_sout = &pl_priv(p_playlist)->p_sout;
340 PL_DEBUG( "dead input" );
342 assert( *pp_sout == NULL );
343 if( var_CreateGetBool( p_input, "sout-keep" ) )
344 *pp_sout = input_DetachSout( p_input );
347 playlist_release_current_input( p_playlist );
349 int i_activity= var_GetInteger( p_playlist, "activity" );
350 var_SetInteger( p_playlist, "activity",
351 i_activity - DEFAULT_INPUT_ACTIVITY );
354 /* This input is dying, let it do */
355 else if( p_input->b_die )
357 PL_DEBUG( "dying input" );
359 msleep( INTF_IDLE_SLEEP );
363 /* This input has finished, ask it to die ! */
364 else if( p_input->b_error || p_input->b_eof )
366 PL_DEBUG( "finished input" );
367 input_StopThread( p_input );
368 /* No need to wait here, we'll wait in the p_input->b_die case */
374 /* No input. Several cases
375 * - No request, running status -> start new item
376 * - No request, stopped status -> collect garbage
377 * - Request, running requested -> start new item
378 * - Request, stopped requested -> collect garbage
380 int i_status = pl_priv(p_playlist)->request.b_request ?
381 pl_priv(p_playlist)->request.i_status : pl_priv(p_playlist)->status.i_status;
382 if( i_status != PLAYLIST_STOPPED )
384 msg_Dbg( p_playlist, "starting new item" );
385 playlist_item_t *p_item = playlist_NextItem( p_playlist );
389 msg_Dbg( p_playlist, "nothing to play" );
390 pl_priv(p_playlist)->status.i_status = PLAYLIST_STOPPED;
392 if( b_playexit == true )
394 msg_Info( p_playlist, "end of playlist, exiting" );
395 vlc_object_kill( p_playlist->p_libvlc );
399 playlist_PlayItem( p_playlist, p_item );
400 /* playlist_PlayItem loose input event, we need to recheck */
405 pl_priv(p_playlist)->status.i_status = PLAYLIST_STOPPED;
410 static void VariablesInit( playlist_t *p_playlist )
413 /* These variables control updates */
414 var_Create( p_playlist, "intf-change", VLC_VAR_BOOL );
416 var_Set( p_playlist, "intf-change", val );
418 var_Create( p_playlist, "item-change", VLC_VAR_INTEGER );
420 var_Set( p_playlist, "item-change", val );
422 var_Create( p_playlist, "item-deleted", VLC_VAR_INTEGER );
424 var_Set( p_playlist, "item-deleted", val );
426 var_Create( p_playlist, "item-append", VLC_VAR_ADDRESS );
428 var_Create( p_playlist, "playlist-current", VLC_VAR_INTEGER );
430 var_Set( p_playlist, "playlist-current", val );
432 var_Create( p_playlist, "activity", VLC_VAR_INTEGER );
433 var_SetInteger( p_playlist, "activity", 0 );
435 /* Variables to control playback */
436 var_Create( p_playlist, "play-and-stop", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
437 var_Create( p_playlist, "play-and-exit", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
438 var_Create( p_playlist, "random", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
439 var_Create( p_playlist, "repeat", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
440 var_Create( p_playlist, "loop", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
442 var_AddCallback( p_playlist, "random", RandomCallback, NULL );
445 var_Create( p_playlist, "album-art", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
448 int playlist_CurrentId( playlist_t * p_playlist )
450 return pl_priv(p_playlist)->status.p_item->i_id;
454 bool playlist_IsPlaying( playlist_t * p_playlist )
456 return ( pl_priv(p_playlist)->status.i_status == PLAYLIST_RUNNING &&
457 !(pl_priv(p_playlist)->request.b_request && pl_priv(p_playlist)->request.i_status == PLAYLIST_STOPPED) );
460 playlist_item_t * playlist_CurrentPlayingItem( playlist_t * p_playlist )
462 return pl_priv(p_playlist)->status.p_item;
465 int playlist_Status( playlist_t * p_playlist )
467 return pl_priv(p_playlist)->status.i_status;