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 pl_priv(p_playlist)->gc_date = 0;
90 pl_priv(p_playlist)->b_cant_sleep = false;
92 ARRAY_INIT( p_playlist->items );
93 ARRAY_INIT( p_playlist->all_items );
94 ARRAY_INIT( pl_priv(p_playlist)->items_to_delete );
95 ARRAY_INIT( p_playlist->current );
97 p_playlist->i_current_index = 0;
98 pl_priv(p_playlist)->b_reset_currently_playing = true;
99 pl_priv(p_playlist)->last_rebuild_date = 0;
101 pl_priv(p_playlist)->b_tree = var_CreateGetBool( p_playlist, "playlist-tree" );
103 pl_priv(p_playlist)->b_doing_ml = false;
105 pl_priv(p_playlist)->b_auto_preparse =
106 var_CreateGetBool( p_playlist, "auto-preparse" ) ;
108 PL_LOCK; /* playlist_NodeCreate will check for it */
109 p_playlist->p_root_category = playlist_NodeCreate( p_playlist, NULL, NULL,
111 p_playlist->p_root_onelevel = playlist_NodeCreate( p_playlist, NULL, NULL,
112 0, p_playlist->p_root_category->p_input );
115 if( !p_playlist->p_root_category || !p_playlist->p_root_onelevel )
118 /* Create playlist and media library */
119 PL_LOCK; /* playlist_NodesPairCreate will check for it */
120 playlist_NodesPairCreate( p_playlist, _( "Playlist" ),
121 &p_playlist->p_local_category,
122 &p_playlist->p_local_onelevel, false );
125 p_playlist->p_local_category->i_flags |= PLAYLIST_RO_FLAG;
126 p_playlist->p_local_onelevel->i_flags |= PLAYLIST_RO_FLAG;
128 if( !p_playlist->p_local_category || !p_playlist->p_local_onelevel ||
129 !p_playlist->p_local_category->p_input ||
130 !p_playlist->p_local_onelevel->p_input )
133 if( config_GetInt( p_playlist, "media-library") )
135 PL_LOCK; /* playlist_NodesPairCreate will check for it */
136 playlist_NodesPairCreate( p_playlist, _( "Media Library" ),
137 &p_playlist->p_ml_category,
138 &p_playlist->p_ml_onelevel, false );
141 if(!p_playlist->p_ml_category || !p_playlist->p_ml_onelevel)
144 p_playlist->p_ml_category->i_flags |= PLAYLIST_RO_FLAG;
145 p_playlist->p_ml_onelevel->i_flags |= PLAYLIST_RO_FLAG;
149 p_playlist->p_ml_category = p_playlist->p_ml_onelevel = NULL;
153 pl_priv(p_playlist)->status.p_item = NULL;
154 pl_priv(p_playlist)->status.p_node = p_playlist->p_local_onelevel;
155 pl_priv(p_playlist)->request.b_request = false;
156 pl_priv(p_playlist)->status.i_status = PLAYLIST_STOPPED;
158 pl_priv(p_playlist)->b_auto_preparse = false;
159 playlist_MLLoad( p_playlist );
160 pl_priv(p_playlist)->b_auto_preparse = true;
162 vlc_object_set_destructor( p_playlist, playlist_Destructor );
170 * Destroy a playlist structure.
171 * \param p_playlist the playlist object
175 static void playlist_Destructor( vlc_object_t * p_this )
177 playlist_t *p_playlist = (playlist_t *)p_this;
178 playlist_private_t *p_sys = pl_priv(p_playlist);
180 assert( !p_sys->p_preparser );
181 assert( !p_sys->p_fetcher );
183 msg_Err( p_this, "Destroyed" );
186 /* Destroy remaining objects */
187 static void ObjectGarbageCollector( playlist_t *p_playlist, bool b_force )
191 if( mdate() - pl_priv(p_playlist)->gc_date < 1000000 )
193 pl_priv(p_playlist)->b_cant_sleep = true;
196 else if( pl_priv(p_playlist)->gc_date == 0 )
200 pl_priv(p_playlist)->b_cant_sleep = false;
204 static int InputEvent( vlc_object_t *p_this, char const *psz_cmd,
205 vlc_value_t oldval, vlc_value_t newval, void *p_data )
207 VLC_UNUSED(p_this); VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
208 playlist_t *p_playlist = p_data;
210 if( newval.i_int != INPUT_EVENT_STATE &&
211 newval.i_int != INPUT_EVENT_ES )
216 if( newval.i_int == INPUT_EVENT_ES )
217 pl_priv(p_playlist)->gc_date = mdate();
219 vlc_object_signal_unlocked( p_playlist );
226 void playlist_release_current_input( playlist_t * p_playlist )
230 if( !pl_priv(p_playlist)->p_input ) return;
232 input_thread_t * p_input = pl_priv(p_playlist)->p_input;
234 var_DelCallback( p_input, "intf-event", InputEvent, p_playlist );
235 pl_priv(p_playlist)->p_input = NULL;
237 /* Release the playlist lock, because we may get stuck
238 * in vlc_object_release() for some time. */
240 vlc_thread_join( p_input );
241 vlc_object_release( p_input );
245 void playlist_set_current_input(
246 playlist_t * p_playlist, input_thread_t * p_input )
250 playlist_release_current_input( p_playlist );
254 vlc_object_hold( p_input );
255 pl_priv(p_playlist)->p_input = p_input;
257 var_AddCallback( p_input, "intf-event", InputEvent, p_playlist );
261 /** Get current playing input.
263 input_thread_t * playlist_CurrentInput( playlist_t * p_playlist )
265 input_thread_t * p_input;
267 p_input = pl_priv(p_playlist)->p_input;
268 if( p_input ) vlc_object_hold( p_input );
277 /** Accessor for status item and status nodes.
279 playlist_item_t * get_current_status_item( playlist_t * p_playlist )
283 return pl_priv(p_playlist)->status.p_item;
286 playlist_item_t * get_current_status_node( playlist_t * p_playlist )
290 return pl_priv(p_playlist)->status.p_node;
293 void set_current_status_item( playlist_t * p_playlist,
294 playlist_item_t * p_item )
298 if( pl_priv(p_playlist)->status.p_item &&
299 pl_priv(p_playlist)->status.p_item->i_flags & PLAYLIST_REMOVE_FLAG &&
300 pl_priv(p_playlist)->status.p_item != p_item )
302 /* It's unsafe given current design to delete a playlist item :(
303 playlist_ItemDelete( pl_priv(p_playlist)->status.p_item ); */
305 pl_priv(p_playlist)->status.p_item = p_item;
308 void set_current_status_node( playlist_t * p_playlist,
309 playlist_item_t * p_node )
313 if( pl_priv(p_playlist)->status.p_node &&
314 pl_priv(p_playlist)->status.p_node->i_flags & PLAYLIST_REMOVE_FLAG &&
315 pl_priv(p_playlist)->status.p_node != p_node )
317 /* It's unsafe given current design to delete a playlist item :(
318 playlist_ItemDelete( pl_priv(p_playlist)->status.p_node ); */
320 pl_priv(p_playlist)->status.p_node = p_node;
326 * Main loop for the playlist. It should be entered with the
327 * playlist lock (otherwise input event may be lost)
328 * \param p_playlist the playlist object
331 void playlist_MainLoop( playlist_t *p_playlist )
333 playlist_item_t *p_item = NULL;
334 bool b_playexit = var_GetBool( p_playlist, "play-and-exit" );
338 if( pl_priv(p_playlist)->b_reset_currently_playing &&
339 mdate() - pl_priv(p_playlist)->last_rebuild_date > 30000 ) // 30 ms
341 ResetCurrentlyPlaying( p_playlist, var_GetBool( p_playlist, "random" ),
342 get_current_status_item( p_playlist ) );
343 pl_priv(p_playlist)->last_rebuild_date = mdate();
347 /* If there is an input, check that it doesn't need to die. */
348 if( pl_priv(p_playlist)->p_input )
350 if( pl_priv(p_playlist)->request.b_request && !pl_priv(p_playlist)->p_input->b_die )
352 PL_DEBUG( "incoming request - stopping current input" );
353 input_StopThread( pl_priv(p_playlist)->p_input );
356 /* This input is dead. Remove it ! */
357 if( pl_priv(p_playlist)->p_input->b_dead )
360 input_thread_t *p_input;
361 sout_instance_t **pp_sout = &pl_priv(p_playlist)->p_sout;
363 PL_DEBUG( "dead input" );
365 p_input = pl_priv(p_playlist)->p_input;
367 assert( *pp_sout == NULL );
368 if( var_CreateGetBool( p_input, "sout-keep" ) )
369 *pp_sout = input_DetachSout( p_input );
372 playlist_release_current_input( p_playlist );
374 pl_priv(p_playlist)->gc_date = mdate();
375 pl_priv(p_playlist)->b_cant_sleep = true;
377 i_activity= var_GetInteger( p_playlist, "activity" );
378 var_SetInteger( p_playlist, "activity", i_activity -
379 DEFAULT_INPUT_ACTIVITY );
383 /* This input is dying, let it do */
384 else if( pl_priv(p_playlist)->p_input->b_die )
386 PL_DEBUG( "dying input" );
388 msleep( INTF_IDLE_SLEEP );
392 /* This input has finished, ask it to die ! */
393 else if( pl_priv(p_playlist)->p_input->b_error
394 || pl_priv(p_playlist)->p_input->b_eof )
396 PL_DEBUG( "finished input" );
397 input_StopThread( pl_priv(p_playlist)->p_input );
398 /* No need to wait here, we'll wait in the p_input->b_die case */
401 else if( pl_priv(p_playlist)->p_input->i_state != INIT_S )
403 ObjectGarbageCollector( p_playlist, false );
408 /* No input. Several cases
409 * - No request, running status -> start new item
410 * - No request, stopped status -> collect garbage
411 * - Request, running requested -> start new item
412 * - Request, stopped requested -> collect garbage
414 int i_status = pl_priv(p_playlist)->request.b_request ?
415 pl_priv(p_playlist)->request.i_status : pl_priv(p_playlist)->status.i_status;
416 if( i_status != PLAYLIST_STOPPED )
418 msg_Dbg( p_playlist, "starting new item" );
419 p_item = playlist_NextItem( p_playlist );
423 msg_Dbg( p_playlist, "nothing to play" );
424 pl_priv(p_playlist)->status.i_status = PLAYLIST_STOPPED;
426 if( b_playexit == true )
428 msg_Info( p_playlist, "end of playlist, exiting" );
429 vlc_object_kill( p_playlist->p_libvlc );
431 ObjectGarbageCollector( p_playlist, true );
434 playlist_PlayItem( p_playlist, p_item );
435 /* playlist_PlayItem loose input event, we need to recheck */
440 const bool b_gc_forced = pl_priv(p_playlist)->status.i_status != PLAYLIST_STOPPED;
442 pl_priv(p_playlist)->status.i_status = PLAYLIST_STOPPED;
444 /* Collect garbage */
445 ObjectGarbageCollector( p_playlist, b_gc_forced );
453 * The playlist is dying so do the last loop
454 * \param p_playlist the playlist object
457 void playlist_LastLoop( playlist_t *p_playlist )
459 /* If there is an input, kill it */
463 if( pl_priv(p_playlist)->p_input == NULL )
469 if( pl_priv(p_playlist)->p_input->b_dead )
472 playlist_release_current_input( p_playlist );
474 /* sout-keep: no need to anything here.
475 * The last input will destroy its sout, if any, by itself */
480 else if( pl_priv(p_playlist)->p_input->b_die )
482 /* This input is dying, leave it alone */
485 else if( pl_priv(p_playlist)->p_input->b_error || pl_priv(p_playlist)->p_input->b_eof )
487 input_StopThread( pl_priv(p_playlist)->p_input );
493 pl_priv(p_playlist)->p_input->b_eof = 1;
497 msleep( INTF_IDLE_SLEEP );
501 /* close the remaining sout-keep (if there was no input atm) */
502 sout_instance_t *p_sout = pl_priv(p_playlist)->p_sout;
504 sout_DeleteInstance( p_sout );
507 /* Core should have terminated all SDs before the playlist */
508 /* TODO: It fails to do so when not playing anything -- Courmisch */
509 playlist_ServicesDiscoveryKillAll( p_playlist );
510 playlist_MLDump( p_playlist );
514 /* Release the current node */
515 set_current_status_node( p_playlist, NULL );
517 /* Release the current item */
518 set_current_status_item( p_playlist, NULL );
520 FOREACH_ARRAY( playlist_item_t *p_del, p_playlist->all_items )
521 free( p_del->pp_children );
522 vlc_gc_decref( p_del->p_input );
525 ARRAY_RESET( p_playlist->all_items );
526 FOREACH_ARRAY( playlist_item_t *p_del, pl_priv(p_playlist)->items_to_delete )
527 free( p_del->pp_children );
528 vlc_gc_decref( p_del->p_input );
531 ARRAY_RESET( pl_priv(p_playlist)->items_to_delete );
533 ARRAY_RESET( p_playlist->items );
534 ARRAY_RESET( p_playlist->current );
539 static void VariablesInit( playlist_t *p_playlist )
542 /* These variables control updates */
543 var_Create( p_playlist, "intf-change", VLC_VAR_BOOL );
545 var_Set( p_playlist, "intf-change", val );
547 var_Create( p_playlist, "item-change", VLC_VAR_INTEGER );
549 var_Set( p_playlist, "item-change", val );
551 var_Create( p_playlist, "item-deleted", VLC_VAR_INTEGER );
553 var_Set( p_playlist, "item-deleted", val );
555 var_Create( p_playlist, "item-append", VLC_VAR_ADDRESS );
557 var_Create( p_playlist, "playlist-current", VLC_VAR_INTEGER );
559 var_Set( p_playlist, "playlist-current", val );
561 var_Create( p_playlist, "activity", VLC_VAR_INTEGER );
562 var_SetInteger( p_playlist, "activity", 0 );
564 /* Variables to control playback */
565 var_Create( p_playlist, "play-and-stop", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
566 var_Create( p_playlist, "play-and-exit", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
567 var_Create( p_playlist, "random", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
568 var_Create( p_playlist, "repeat", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
569 var_Create( p_playlist, "loop", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
571 var_AddCallback( p_playlist, "random", RandomCallback, NULL );
574 var_Create( p_playlist, "album-art", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
577 int playlist_CurrentId( playlist_t * p_playlist )
579 return pl_priv(p_playlist)->status.p_item->i_id;
582 bool playlist_IsPlaying( playlist_t * p_playlist )
584 return ( pl_priv(p_playlist)->status.i_status == PLAYLIST_RUNNING &&
585 !(pl_priv(p_playlist)->request.b_request && pl_priv(p_playlist)->request.i_status == PLAYLIST_STOPPED) );
588 playlist_item_t * playlist_CurrentPlayingItem( playlist_t * p_playlist )
590 return pl_priv(p_playlist)->status.p_item;
593 int playlist_Status( playlist_t * p_playlist )
595 return pl_priv(p_playlist)->status.i_status;