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 if( p_sys->p_preparser )
181 playlist_preparser_Delete( p_sys->p_preparser );
183 if( p_sys->p_fetcher )
184 playlist_fetcher_Delete( p_sys->p_fetcher );
186 msg_Dbg( p_this, "Destroyed" );
189 /* Destroy remaining objects */
190 static void ObjectGarbageCollector( playlist_t *p_playlist, bool b_force )
194 if( mdate() - pl_priv(p_playlist)->gc_date < 1000000 )
196 pl_priv(p_playlist)->b_cant_sleep = true;
199 else if( pl_priv(p_playlist)->gc_date == 0 )
203 pl_priv(p_playlist)->b_cant_sleep = false;
207 static int InputEvent( vlc_object_t *p_this, char const *psz_cmd,
208 vlc_value_t oldval, vlc_value_t newval, void *p_data )
210 VLC_UNUSED(p_this); VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
211 playlist_t *p_playlist = p_data;
213 if( newval.i_int != INPUT_EVENT_STATE &&
214 newval.i_int != INPUT_EVENT_ES )
219 if( newval.i_int == INPUT_EVENT_ES )
220 pl_priv(p_playlist)->gc_date = mdate();
222 vlc_object_signal_unlocked( p_playlist );
229 void playlist_release_current_input( playlist_t * p_playlist )
233 if( !pl_priv(p_playlist)->p_input ) return;
235 input_thread_t * p_input = pl_priv(p_playlist)->p_input;
237 var_DelCallback( p_input, "intf-event", InputEvent, p_playlist );
238 pl_priv(p_playlist)->p_input = NULL;
240 /* Release the playlist lock, because we may get stuck
241 * in vlc_object_release() for some time. */
243 vlc_thread_join( p_input );
244 vlc_object_release( p_input );
248 void playlist_set_current_input(
249 playlist_t * p_playlist, input_thread_t * p_input )
253 playlist_release_current_input( p_playlist );
257 vlc_object_hold( p_input );
258 pl_priv(p_playlist)->p_input = p_input;
260 var_AddCallback( p_input, "intf-event", InputEvent, p_playlist );
264 /** Get current playing input.
266 input_thread_t * playlist_CurrentInput( playlist_t * p_playlist )
268 input_thread_t * p_input;
270 p_input = pl_priv(p_playlist)->p_input;
271 if( p_input ) vlc_object_hold( p_input );
280 /** Accessor for status item and status nodes.
282 playlist_item_t * get_current_status_item( playlist_t * p_playlist )
286 return pl_priv(p_playlist)->status.p_item;
289 playlist_item_t * get_current_status_node( playlist_t * p_playlist )
293 return pl_priv(p_playlist)->status.p_node;
296 void set_current_status_item( playlist_t * p_playlist,
297 playlist_item_t * p_item )
301 if( pl_priv(p_playlist)->status.p_item &&
302 pl_priv(p_playlist)->status.p_item->i_flags & PLAYLIST_REMOVE_FLAG &&
303 pl_priv(p_playlist)->status.p_item != p_item )
305 /* It's unsafe given current design to delete a playlist item :(
306 playlist_ItemDelete( pl_priv(p_playlist)->status.p_item ); */
308 pl_priv(p_playlist)->status.p_item = p_item;
311 void set_current_status_node( playlist_t * p_playlist,
312 playlist_item_t * p_node )
316 if( pl_priv(p_playlist)->status.p_node &&
317 pl_priv(p_playlist)->status.p_node->i_flags & PLAYLIST_REMOVE_FLAG &&
318 pl_priv(p_playlist)->status.p_node != p_node )
320 /* It's unsafe given current design to delete a playlist item :(
321 playlist_ItemDelete( pl_priv(p_playlist)->status.p_node ); */
323 pl_priv(p_playlist)->status.p_node = p_node;
329 * Main loop for the playlist. It should be entered with the
330 * playlist lock (otherwise input event may be lost)
331 * \param p_playlist the playlist object
334 void playlist_MainLoop( playlist_t *p_playlist )
336 playlist_item_t *p_item = NULL;
337 bool b_playexit = var_GetBool( p_playlist, "play-and-exit" );
341 if( pl_priv(p_playlist)->b_reset_currently_playing &&
342 mdate() - pl_priv(p_playlist)->last_rebuild_date > 30000 ) // 30 ms
344 ResetCurrentlyPlaying( p_playlist, var_GetBool( p_playlist, "random" ),
345 get_current_status_item( p_playlist ) );
346 pl_priv(p_playlist)->last_rebuild_date = mdate();
350 /* If there is an input, check that it doesn't need to die. */
351 if( pl_priv(p_playlist)->p_input )
353 if( pl_priv(p_playlist)->request.b_request && !pl_priv(p_playlist)->p_input->b_die )
355 PL_DEBUG( "incoming request - stopping current input" );
356 input_StopThread( pl_priv(p_playlist)->p_input );
359 /* This input is dead. Remove it ! */
360 if( pl_priv(p_playlist)->p_input->b_dead )
363 input_thread_t *p_input;
364 sout_instance_t **pp_sout = &pl_priv(p_playlist)->p_sout;
366 PL_DEBUG( "dead input" );
368 p_input = pl_priv(p_playlist)->p_input;
370 assert( *pp_sout == NULL );
371 if( var_CreateGetBool( p_input, "sout-keep" ) )
372 *pp_sout = input_DetachSout( p_input );
375 playlist_release_current_input( p_playlist );
377 pl_priv(p_playlist)->gc_date = mdate();
378 pl_priv(p_playlist)->b_cant_sleep = true;
380 i_activity= var_GetInteger( p_playlist, "activity" );
381 var_SetInteger( p_playlist, "activity", i_activity -
382 DEFAULT_INPUT_ACTIVITY );
386 /* This input is dying, let it do */
387 else if( pl_priv(p_playlist)->p_input->b_die )
389 PL_DEBUG( "dying input" );
391 msleep( INTF_IDLE_SLEEP );
395 /* This input has finished, ask it to die ! */
396 else if( pl_priv(p_playlist)->p_input->b_error
397 || pl_priv(p_playlist)->p_input->b_eof )
399 PL_DEBUG( "finished input" );
400 input_StopThread( pl_priv(p_playlist)->p_input );
401 /* No need to wait here, we'll wait in the p_input->b_die case */
404 else if( pl_priv(p_playlist)->p_input->i_state != INIT_S )
406 ObjectGarbageCollector( p_playlist, false );
411 /* No input. Several cases
412 * - No request, running status -> start new item
413 * - No request, stopped status -> collect garbage
414 * - Request, running requested -> start new item
415 * - Request, stopped requested -> collect garbage
417 int i_status = pl_priv(p_playlist)->request.b_request ?
418 pl_priv(p_playlist)->request.i_status : pl_priv(p_playlist)->status.i_status;
419 if( i_status != PLAYLIST_STOPPED )
421 msg_Dbg( p_playlist, "starting new item" );
422 p_item = playlist_NextItem( p_playlist );
426 msg_Dbg( p_playlist, "nothing to play" );
427 pl_priv(p_playlist)->status.i_status = PLAYLIST_STOPPED;
429 if( b_playexit == true )
431 msg_Info( p_playlist, "end of playlist, exiting" );
432 vlc_object_kill( p_playlist->p_libvlc );
434 ObjectGarbageCollector( p_playlist, true );
437 playlist_PlayItem( p_playlist, p_item );
438 /* playlist_PlayItem loose input event, we need to recheck */
443 const bool b_gc_forced = pl_priv(p_playlist)->status.i_status != PLAYLIST_STOPPED;
445 pl_priv(p_playlist)->status.i_status = PLAYLIST_STOPPED;
447 /* Collect garbage */
448 ObjectGarbageCollector( p_playlist, b_gc_forced );
456 * The playlist is dying so do the last loop
457 * \param p_playlist the playlist object
460 void playlist_LastLoop( playlist_t *p_playlist )
462 /* If there is an input, kill it */
466 if( pl_priv(p_playlist)->p_input == NULL )
472 if( pl_priv(p_playlist)->p_input->b_dead )
475 playlist_release_current_input( p_playlist );
477 /* sout-keep: no need to anything here.
478 * The last input will destroy its sout, if any, by itself */
483 else if( pl_priv(p_playlist)->p_input->b_die )
485 /* This input is dying, leave it alone */
488 else if( pl_priv(p_playlist)->p_input->b_error || pl_priv(p_playlist)->p_input->b_eof )
490 input_StopThread( pl_priv(p_playlist)->p_input );
496 pl_priv(p_playlist)->p_input->b_eof = 1;
500 msleep( INTF_IDLE_SLEEP );
504 /* close the remaining sout-keep (if there was no input atm) */
505 sout_instance_t *p_sout = pl_priv(p_playlist)->p_sout;
507 sout_DeleteInstance( p_sout );
510 /* Core should have terminated all SDs before the playlist */
511 /* TODO: It fails to do so when not playing anything -- Courmisch */
512 playlist_ServicesDiscoveryKillAll( p_playlist );
513 playlist_MLDump( p_playlist );
517 /* Release the current node */
518 set_current_status_node( p_playlist, NULL );
520 /* Release the current item */
521 set_current_status_item( p_playlist, NULL );
523 FOREACH_ARRAY( playlist_item_t *p_del, p_playlist->all_items )
524 free( p_del->pp_children );
525 vlc_gc_decref( p_del->p_input );
528 ARRAY_RESET( p_playlist->all_items );
529 FOREACH_ARRAY( playlist_item_t *p_del, pl_priv(p_playlist)->items_to_delete )
530 free( p_del->pp_children );
531 vlc_gc_decref( p_del->p_input );
534 ARRAY_RESET( pl_priv(p_playlist)->items_to_delete );
536 ARRAY_RESET( p_playlist->items );
537 ARRAY_RESET( p_playlist->current );
542 static void VariablesInit( playlist_t *p_playlist )
545 /* These variables control updates */
546 var_Create( p_playlist, "intf-change", VLC_VAR_BOOL );
548 var_Set( p_playlist, "intf-change", val );
550 var_Create( p_playlist, "item-change", VLC_VAR_INTEGER );
552 var_Set( p_playlist, "item-change", val );
554 var_Create( p_playlist, "item-deleted", VLC_VAR_INTEGER );
556 var_Set( p_playlist, "item-deleted", val );
558 var_Create( p_playlist, "item-append", VLC_VAR_ADDRESS );
560 var_Create( p_playlist, "playlist-current", VLC_VAR_INTEGER );
562 var_Set( p_playlist, "playlist-current", val );
564 var_Create( p_playlist, "activity", VLC_VAR_INTEGER );
565 var_SetInteger( p_playlist, "activity", 0 );
567 /* Variables to control playback */
568 var_Create( p_playlist, "play-and-stop", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
569 var_Create( p_playlist, "play-and-exit", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
570 var_Create( p_playlist, "random", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
571 var_Create( p_playlist, "repeat", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
572 var_Create( p_playlist, "loop", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
574 var_AddCallback( p_playlist, "random", RandomCallback, NULL );
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;