1 /*****************************************************************************
2 * engine.c : Run the playlist and handle its control
3 *****************************************************************************
4 * Copyright (C) 1999-2007 the VideoLAN team
7 * Authors: Samuel Hocevar <sam@zoy.org>
8 * Clément Stenac <zorglub@videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
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;
48 ((playlist_t*)p_this)->b_reset_currently_playing = VLC_TRUE;
49 playlist_Signal( ((playlist_t*)p_this) );
56 * Create a playlist structure.
57 * \param p_parent the vlc object that is to be the parent of this playlist
58 * \return a pointer to the created playlist, or NULL on error
60 playlist_t * playlist_Create( vlc_object_t *p_parent )
62 playlist_t *p_playlist;
66 /* Allocate structure */
67 p_playlist = vlc_object_create( p_parent, VLC_OBJECT_PLAYLIST );
70 msg_Err( p_parent, "out of memory" );
74 TAB_INIT( p_playlist->i_sds, p_playlist->pp_sds );
76 p_parent->p_libvlc->p_playlist = p_playlist;
78 VariablesInit( p_playlist );
80 /* Initialise data structures */
81 vlc_mutex_init( p_playlist, &p_playlist->gc_lock );
82 p_playlist->i_last_playlist_id = 0;
83 p_playlist->p_input = NULL;
85 p_playlist->gc_date = 0;
86 p_playlist->b_cant_sleep = VLC_FALSE;
88 ARRAY_INIT( p_playlist->items );
89 ARRAY_INIT( p_playlist->all_items );
90 ARRAY_INIT( p_playlist->current );
92 p_playlist->i_current_index = 0;
93 p_playlist->b_reset_currently_playing = VLC_TRUE;
94 p_playlist->last_rebuild_date = 0;
96 i_tree = var_CreateGetBool( p_playlist, "playlist-tree" );
97 p_playlist->b_always_tree = (i_tree == 1);
98 p_playlist->b_never_tree = (i_tree == 2);
100 p_playlist->b_doing_ml = VLC_FALSE;
102 p_playlist->b_auto_preparse =
103 var_CreateGetBool( p_playlist, "auto-preparse" ) ;
105 p_playlist->p_root_category = playlist_NodeCreate( p_playlist, NULL, NULL,
107 p_playlist->p_root_onelevel = playlist_NodeCreate( p_playlist, NULL, NULL,
108 0, p_playlist->p_root_category->p_input );
110 if( !p_playlist->p_root_category || !p_playlist->p_root_onelevel )
113 /* Create playlist and media library */
114 playlist_NodesPairCreate( p_playlist, _( "Playlist" ),
115 &p_playlist->p_local_category,
116 &p_playlist->p_local_onelevel, VLC_FALSE );
118 p_playlist->p_local_category->i_flags |= PLAYLIST_RO_FLAG;
119 p_playlist->p_local_onelevel->i_flags |= PLAYLIST_RO_FLAG;
121 if( !p_playlist->p_local_category || !p_playlist->p_local_onelevel ||
122 !p_playlist->p_local_category->p_input ||
123 !p_playlist->p_local_onelevel->p_input )
126 if( config_GetInt( p_playlist, "media-library") )
128 playlist_NodesPairCreate( p_playlist, _( "Media Library" ),
129 &p_playlist->p_ml_category,
130 &p_playlist->p_ml_onelevel, VLC_FALSE );
132 if(!p_playlist->p_ml_category || !p_playlist->p_ml_onelevel)
135 p_playlist->p_ml_category->i_flags |= PLAYLIST_RO_FLAG;
136 p_playlist->p_ml_onelevel->i_flags |= PLAYLIST_RO_FLAG;
140 p_playlist->p_ml_category = p_playlist->p_ml_onelevel = NULL;
144 p_playlist->status.p_item = NULL;
145 p_playlist->status.p_node = p_playlist->p_local_onelevel;
146 p_playlist->request.b_request = VLC_FALSE;
147 p_playlist->status.i_status = PLAYLIST_STOPPED;
149 p_playlist->i_sort = SORT_ID;
150 p_playlist->i_order = ORDER_NORMAL;
153 b_save = p_playlist->b_auto_preparse;
154 p_playlist->b_auto_preparse = VLC_FALSE;
155 playlist_MLLoad( p_playlist );
156 p_playlist->b_auto_preparse = VLC_TRUE;
158 vlc_object_set_destructor( p_playlist, playlist_Destructor );
166 * Destroy a playlist structure.
167 * \param p_playlist the playlist object
170 void playlist_Destroy( playlist_t *p_playlist )
172 /* XXX: should go in the playlist destructor */
173 var_Destroy( p_playlist, "intf-change" );
174 var_Destroy( p_playlist, "item-change" );
175 var_Destroy( p_playlist, "playlist-current" );
176 var_Destroy( p_playlist, "intf-popupmenu" );
177 var_Destroy( p_playlist, "intf-show" );
178 var_Destroy( p_playlist, "play-and-stop" );
179 var_Destroy( p_playlist, "play-and-exit" );
180 var_Destroy( p_playlist, "random" );
181 var_Destroy( p_playlist, "repeat" );
182 var_Destroy( p_playlist, "loop" );
183 var_Destroy( p_playlist, "activity" );
185 vlc_object_release( p_playlist );
188 static void playlist_Destructor( vlc_object_t * p_this )
190 playlist_t * p_playlist = (playlist_t *)p_this;
193 if( p_playlist->p_preparse )
195 vlc_object_release( p_playlist->p_preparse );
199 if( p_playlist->p_fetcher )
201 vlc_object_release( p_playlist->p_fetcher );
205 vlc_mutex_destroy( &p_playlist->p_stats->lock );
206 if( p_playlist->p_stats )
207 free( p_playlist->p_stats );
210 /* Destroy remaining objects */
211 static void ObjectGarbageCollector( playlist_t *p_playlist, vlc_bool_t b_force )
217 if( mdate() - p_playlist->gc_date < 1000000 )
219 p_playlist->b_cant_sleep = VLC_TRUE;
222 else if( p_playlist->gc_date == 0 )
226 vlc_mutex_lock( &p_playlist->gc_lock );
227 while( ( p_obj = vlc_object_find( p_playlist, VLC_OBJECT_VOUT,
230 if( p_obj->p_parent != VLC_OBJECT(p_playlist) )
232 vlc_object_release( p_obj );
235 msg_Dbg( p_playlist, "garbage collector destroying 1 vout" );
236 vlc_object_detach( p_obj );
237 vlc_object_release( p_obj );
238 vout_Destroy( (vout_thread_t *)p_obj );
240 while( ( p_obj = vlc_object_find( p_playlist, VLC_OBJECT_SOUT,
243 if( p_obj->p_parent != VLC_OBJECT(p_playlist) )
245 vlc_object_release( p_obj );
248 msg_Dbg( p_playlist, "garbage collector destroying 1 sout" );
249 vlc_object_detach( p_obj );
250 vlc_object_release( p_obj );
251 sout_DeleteInstance( (sout_instance_t*)p_obj );
253 p_playlist->b_cant_sleep = VLC_FALSE;
254 vlc_mutex_unlock( &p_playlist->gc_lock );
260 * Main loop for the playlist
261 * \param p_playlist the playlist object
264 void playlist_MainLoop( playlist_t *p_playlist )
266 playlist_item_t *p_item = NULL;
267 vlc_bool_t b_playexit = var_GetBool( p_playlist, "play-and-exit" );
270 if( p_playlist->b_reset_currently_playing &&
271 mdate() - p_playlist->last_rebuild_date > 30000 ) // 30 ms
273 ResetCurrentlyPlaying( p_playlist, var_GetBool( p_playlist, "random" ),
274 p_playlist->status.p_item );
275 p_playlist->last_rebuild_date = mdate();
279 /* If there is an input, check that it doesn't need to die. */
280 if( p_playlist->p_input )
282 if( p_playlist->request.b_request && !p_playlist->p_input->b_die )
284 PL_DEBUG( "incoming request - stopping current input" );
285 input_StopThread( p_playlist->p_input );
288 /* This input is dead. Remove it ! */
289 if( p_playlist->p_input->b_dead )
292 input_thread_t *p_input;
293 PL_DEBUG( "dead input" );
295 p_input = p_playlist->p_input;
296 p_playlist->p_input = NULL;
298 /* Release the playlist lock, because we may get stuck
299 * in vlc_object_release() for some time. */
303 vlc_object_release( p_input );
307 p_playlist->gc_date = mdate();
308 p_playlist->b_cant_sleep = VLC_TRUE;
310 if( p_playlist->status.p_item->i_flags
311 & PLAYLIST_REMOVE_FLAG )
313 PL_DEBUG( "%s was marked for deletion, deleting",
314 PLI_NAME( p_playlist->status.p_item ) );
315 playlist_ItemDelete( p_playlist->status.p_item );
316 if( p_playlist->request.p_item == p_playlist->status.p_item )
317 p_playlist->request.p_item = NULL;
318 p_playlist->status.p_item = NULL;
321 i_activity= var_GetInteger( p_playlist, "activity" );
322 var_SetInteger( p_playlist, "activity", i_activity -
323 DEFAULT_INPUT_ACTIVITY );
326 /* This input is dying, let it do */
327 else if( p_playlist->p_input->b_die )
329 PL_DEBUG( "dying input" );
331 msleep( INTF_IDLE_SLEEP );
335 /* This input has finished, ask it to die ! */
336 else if( p_playlist->p_input->b_error
337 || p_playlist->p_input->b_eof )
339 PL_DEBUG( "finished input" );
340 input_StopThread( p_playlist->p_input );
341 /* No need to wait here, we'll wait in the p_input->b_die case */
344 else if( p_playlist->p_input->i_state != INIT_S )
347 ObjectGarbageCollector( p_playlist, VLC_FALSE );
353 /* No input. Several cases
354 * - No request, running status -> start new item
355 * - No request, stopped status -> collect garbage
356 * - Request, running requested -> start new item
357 * - Request, stopped requested -> collect garbage
359 if( p_playlist->request.i_status != PLAYLIST_STOPPED )
361 msg_Dbg( p_playlist, "starting new item" );
362 p_item = playlist_NextItem( p_playlist );
366 msg_Dbg( p_playlist, "nothing to play" );
367 p_playlist->status.i_status = PLAYLIST_STOPPED;
370 if( b_playexit == VLC_TRUE )
372 msg_Info( p_playlist, "end of playlist, exiting" );
373 vlc_object_kill( p_playlist->p_libvlc );
375 ObjectGarbageCollector( p_playlist, VLC_TRUE );
378 playlist_PlayItem( p_playlist, p_item );
382 const vlc_bool_t b_gc_forced = p_playlist->status.i_status != PLAYLIST_STOPPED;
384 p_playlist->status.i_status = PLAYLIST_STOPPED;
385 if( p_playlist->status.p_item &&
386 p_playlist->status.p_item->i_flags & PLAYLIST_REMOVE_FLAG )
388 PL_DEBUG( "deleting item marked for deletion" );
389 playlist_ItemDelete( p_playlist->status.p_item );
390 p_playlist->status.p_item = NULL;
393 /* Collect garbage */
395 ObjectGarbageCollector( p_playlist, b_gc_forced );
406 * The playlist is dying so do the last loop
407 * \param p_playlist the playlist object
410 void playlist_LastLoop( playlist_t *p_playlist )
414 /* If there is an input, kill it */
418 if( p_playlist->p_input == NULL )
424 if( p_playlist->p_input->b_dead )
426 input_thread_t *p_input;
428 /* Unlink current input */
429 p_input = p_playlist->p_input;
430 p_playlist->p_input = NULL;
434 vlc_object_release( p_input );
437 else if( p_playlist->p_input->b_die )
439 /* This input is dying, leave it alone */
442 else if( p_playlist->p_input->b_error || p_playlist->p_input->b_eof )
444 input_StopThread( p_playlist->p_input );
450 p_playlist->p_input->b_eof = 1;
454 msleep( INTF_IDLE_SLEEP );
457 /* close all remaining sout */
458 while( ( p_obj = vlc_object_find( p_playlist,
459 VLC_OBJECT_SOUT, FIND_CHILD ) ) )
461 vlc_object_detach( p_obj );
462 vlc_object_release( p_obj );
463 sout_DeleteInstance( (sout_instance_t*)p_obj );
466 /* close all remaining vout */
467 while( ( p_obj = vlc_object_find( p_playlist,
468 VLC_OBJECT_VOUT, FIND_CHILD ) ) )
470 vlc_object_detach( p_obj );
471 vlc_object_release( p_obj );
472 vout_Destroy( (vout_thread_t *)p_obj );
475 while( p_playlist->i_sds )
477 playlist_ServicesDiscoveryRemove( p_playlist,
478 p_playlist->pp_sds[0]->p_sd->psz_module );
481 playlist_MLDump( p_playlist );
484 /* Go through all items, and simply free everything without caring
485 * about the tree structure. Do not decref, it will be done by doing
486 * the same thing on the input items array */
487 FOREACH_ARRAY( playlist_item_t *p_del, p_playlist->all_items )
488 free( p_del->pp_children );
491 ARRAY_RESET( p_playlist->all_items );
493 ARRAY_RESET( p_playlist->items );
494 ARRAY_RESET( p_playlist->current );
502 * Main loop for preparser queue
503 * \param p_obj items to preparse
506 void playlist_PreparseLoop( playlist_preparse_t *p_obj )
508 playlist_t *p_playlist = (playlist_t *)p_obj->p_parent;
509 input_item_t *p_current;
513 while( !p_playlist->b_die )
515 vlc_object_lock( p_obj );
516 while( p_obj->i_waiting == 0 )
518 if( vlc_object_wait( p_obj ) || p_playlist->b_die )
520 vlc_object_unlock( p_obj );
525 p_current = p_obj->pp_waiting[0];
526 REMOVE_ELEM( p_obj->pp_waiting, p_obj->i_waiting, 0 );
527 vlc_object_unlock( p_obj );
532 if( p_current->i_type == ITEM_TYPE_FILE )
534 stats_TimerStart( p_playlist, "Preparse run",
535 STATS_TIMER_PREPARSE );
536 /* Do not preparse if it is already done (like by playing it) */
537 if( !input_item_IsPreparsed( p_current ) )
540 input_Preparse( p_playlist, p_current );
543 stats_TimerStop( p_playlist, STATS_TIMER_PREPARSE );
545 input_item_SetPreparsed( p_current, VLC_TRUE );
546 var_SetInteger( p_playlist, "item-change", p_current->i_id );
549 /* If we haven't retrieved enough meta, add to secondary queue
550 * which will run the "meta fetchers".
551 * This only checks for meta, not for art
552 * \todo don't do this for things we won't get meta for, like vids
554 char *psz_arturl = input_item_GetArtURL( p_current );
555 char *psz_name = input_item_GetName( p_current );
556 if( !input_MetaSatisfied( p_playlist, p_current, &i_m, &i_o ) )
559 PL_DEBUG( "need to fetch meta for %s", p_current->psz_name );
560 p.p_item = p_current;
561 p.b_fetch_art = VLC_FALSE;
562 vlc_object_lock( p_playlist->p_fetcher );
563 INSERT_ELEM( p_playlist->p_fetcher->p_waiting,
564 p_playlist->p_fetcher->i_waiting,
565 p_playlist->p_fetcher->i_waiting, p);
566 vlc_object_signal_unlocked( p_playlist->p_fetcher );
567 vlc_object_unlock( p_playlist->p_fetcher );
569 /* We already have all needed meta, but we need art right now */
570 else if( p_playlist->p_fetcher->i_art_policy == ALBUM_ART_ALL &&
571 ( !psz_arturl || strncmp( psz_arturl, "file://", 7 ) ) )
574 PL_DEBUG("meta ok for %s, need to fetch art", psz_name );
575 p.p_item = p_current;
576 p.b_fetch_art = VLC_TRUE;
577 vlc_object_lock( p_playlist->p_fetcher );
578 INSERT_ELEM( p_playlist->p_fetcher->p_waiting,
579 p_playlist->p_fetcher->i_waiting,
580 p_playlist->p_fetcher->i_waiting, p);
581 vlc_object_signal_unlocked( p_playlist->p_fetcher );
582 vlc_object_unlock( p_playlist->p_fetcher );
586 PL_DEBUG( "no fetch required for %s (art currently %s)",
587 psz_name, psz_arturl );
588 vlc_gc_decref( p_current );
597 vlc_object_lock( p_obj );
598 i_activity = var_GetInteger( p_playlist, "activity" );
599 if( i_activity < 0 ) i_activity = 0;
600 vlc_object_unlock( p_obj );
601 /* Sleep at least 1ms */
602 msleep( (i_activity+1) * 1000 );
609 * Main loop for secondary preparser queue
610 * \param p_obj items to preparse
613 void playlist_FetcherLoop( playlist_fetcher_t *p_obj )
615 playlist_t *p_playlist = (playlist_t *)p_obj->p_parent;
616 vlc_bool_t b_fetch_art;
617 input_item_t *p_item;
620 while( !p_playlist->b_die )
622 vlc_mutex_lock( &p_obj->object_lock );
623 while( p_obj->i_waiting == 0 )
625 vlc_cond_wait( &p_obj->object_wait, &p_obj->object_lock );
626 if( p_playlist->b_die )
628 vlc_mutex_unlock( &p_obj->object_lock );
633 b_fetch_art = p_obj->p_waiting->b_fetch_art;
634 p_item = p_obj->p_waiting->p_item;
635 REMOVE_ELEM( p_obj->p_waiting, p_obj->i_waiting, 0 );
636 vlc_mutex_unlock( &p_obj->object_lock );
641 /* If the user doesn't want us to fetch meta automatically
643 if( p_playlist->p_fetcher->b_fetch_meta )
645 input_MetaFetch( p_playlist, p_item );
646 var_SetInteger( p_playlist, "item-change", p_item->i_id );
649 /* Fetch right now */
650 if( p_playlist->p_fetcher->i_art_policy == ALBUM_ART_ALL )
652 vlc_mutex_lock( &p_obj->object_lock );
655 p.b_fetch_art = VLC_TRUE;
656 INSERT_ELEM( p_playlist->p_fetcher->p_waiting,
657 p_playlist->p_fetcher->i_waiting,
659 PL_DEBUG( "meta fetched for %s, get art", p_item->psz_name );
660 vlc_mutex_unlock( &p_obj->object_lock );
664 vlc_gc_decref( p_item );
670 /* Check if it is not yet preparsed and if so wait for it (at most 0.5s)
671 * (This can happen if we fetch art on play)
672 * FIXME this doesn't work if we need to fetch meta before art ... */
673 for( i_ret = 0; i_ret < 10 && !input_item_IsPreparsed( p_item ); i_ret++ )
677 b_break = ( !p_playlist->p_input || input_GetItem(p_playlist->p_input) != p_item ||
678 p_playlist->p_input->b_die || p_playlist->p_input->b_eof || p_playlist->p_input->b_error );
685 i_ret = input_ArtFind( p_playlist, p_item );
688 PL_DEBUG( "downloading art for %s", p_item->psz_name );
689 if( input_DownloadAndCacheArt( p_playlist, p_item ) )
690 input_item_SetArtNotFound( p_item, VLC_TRUE );
692 input_item_SetArtFetched( p_item, VLC_TRUE );
693 var_SetInteger( p_playlist, "item-change",
697 else if( i_ret == 0 ) /* Was in cache */
699 PL_DEBUG( "found art for %s in cache", p_item->psz_name );
700 input_item_SetArtFetched( p_item, VLC_TRUE );
701 var_SetInteger( p_playlist, "item-change", p_item->i_id );
705 PL_DEBUG( "art not found for %s", p_item->psz_name );
706 input_item_SetArtNotFound( p_item, VLC_TRUE );
708 vlc_gc_decref( p_item );
711 vlc_object_lock( p_obj );
712 i_activity = var_GetInteger( p_playlist, "activity" );
713 if( i_activity < 0 ) i_activity = 0;
714 vlc_object_unlock( p_obj );
715 /* Sleep at least 1ms */
716 msleep( (i_activity+1) * 1000 );
720 static void VariablesInit( playlist_t *p_playlist )
723 /* These variables control updates */
724 var_Create( p_playlist, "intf-change", VLC_VAR_BOOL );
725 val.b_bool = VLC_TRUE;
726 var_Set( p_playlist, "intf-change", val );
728 var_Create( p_playlist, "item-change", VLC_VAR_INTEGER );
730 var_Set( p_playlist, "item-change", val );
732 var_Create( p_playlist, "item-deleted", VLC_VAR_INTEGER );
734 var_Set( p_playlist, "item-deleted", val );
736 var_Create( p_playlist, "item-append", VLC_VAR_ADDRESS );
738 var_Create( p_playlist, "playlist-current", VLC_VAR_INTEGER );
740 var_Set( p_playlist, "playlist-current", val );
742 var_Create( p_playlist, "intf-popupmenu", VLC_VAR_BOOL );
744 var_Create( p_playlist, "intf-show", VLC_VAR_BOOL );
745 val.b_bool = VLC_TRUE;
746 var_Set( p_playlist, "intf-show", val );
748 var_Create( p_playlist, "activity", VLC_VAR_INTEGER );
749 var_SetInteger( p_playlist, "activity", 0 );
751 /* Variables to control playback */
752 var_CreateGetBool( p_playlist, "play-and-stop" );
753 var_CreateGetBool( p_playlist, "play-and-exit" );
754 var_CreateGetBool( p_playlist, "random" );
755 var_CreateGetBool( p_playlist, "repeat" );
756 var_CreateGetBool( p_playlist, "loop" );
758 var_AddCallback( p_playlist, "random", RandomCallback, NULL );