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 *****************************************************************************/
28 #include <vlc_playlist.h>
29 #include <vlc_interface.h>
30 #include "playlist_internal.h"
31 #include "stream_output/stream_output.h" /* sout_DeleteInstance */
33 /*****************************************************************************
35 *****************************************************************************/
36 static void VariablesInit( playlist_t *p_playlist );
38 static int RandomCallback( vlc_object_t *p_this, char const *psz_cmd,
39 vlc_value_t oldval, vlc_value_t newval, void *a )
41 (void)psz_cmd; (void)oldval; (void)newval; (void)a;
43 ((playlist_t*)p_this)->b_reset_currently_playing = VLC_TRUE;
44 playlist_Signal( ((playlist_t*)p_this) );
51 * Create a playlist structure.
52 * \param p_parent the vlc object that is to be the parent of this playlist
53 * \return a pointer to the created playlist, or NULL on error
55 playlist_t * playlist_Create( vlc_object_t *p_parent )
57 playlist_t *p_playlist;
61 /* Allocate structure */
62 p_playlist = vlc_object_create( p_parent, VLC_OBJECT_PLAYLIST );
65 msg_Err( p_parent, "out of memory" );
69 p_playlist->p_internal = malloc(sizeof(struct playlist_internal_t));
70 memset( p_playlist->p_internal, 0, sizeof(struct playlist_internal_t) );
72 p_parent->p_libvlc->p_playlist = p_playlist;
74 VariablesInit( p_playlist );
76 /* Initialise data structures */
77 vlc_mutex_init( p_playlist, &p_playlist->gc_lock );
78 p_playlist->i_last_playlist_id = 0;
79 p_playlist->i_last_input_id = 0;
80 p_playlist->p_input = NULL;
82 p_playlist->gc_date = 0;
83 p_playlist->b_cant_sleep = VLC_FALSE;
85 ARRAY_INIT( p_playlist->items );
86 ARRAY_INIT( p_playlist->all_items );
87 ARRAY_INIT( p_playlist->input_items );
88 ARRAY_INIT( p_playlist->current );
90 p_playlist->i_current_index = 0;
91 p_playlist->b_reset_currently_playing = VLC_TRUE;
92 p_playlist->last_rebuild_date = 0;
94 i_tree = var_CreateGetBool( p_playlist, "playlist-tree" );
95 p_playlist->b_always_tree = (i_tree == 1);
96 p_playlist->b_never_tree = (i_tree == 2);
98 p_playlist->b_doing_ml = VLC_FALSE;
100 p_playlist->b_auto_preparse =
101 var_CreateGetBool( p_playlist, "auto-preparse") ;
103 p_playlist->p_root_category = playlist_NodeCreate( p_playlist, NULL, NULL,
105 p_playlist->p_root_onelevel = playlist_NodeCreate( p_playlist, NULL, NULL,
108 if( !p_playlist->p_root_category || !p_playlist->p_root_onelevel )
111 /* Create playlist and media library */
112 p_playlist->p_local_category = playlist_NodeCreate( p_playlist,
113 _( "Playlist" ),p_playlist->p_root_category, 0 );
114 p_playlist->p_local_onelevel = playlist_NodeCreate( p_playlist,
115 _( "Playlist" ), p_playlist->p_root_onelevel, 0 );
116 p_playlist->p_local_category->i_flags |= PLAYLIST_RO_FLAG;
117 p_playlist->p_local_onelevel->i_flags |= PLAYLIST_RO_FLAG;
119 if( !p_playlist->p_local_category || !p_playlist->p_local_onelevel ||
120 !p_playlist->p_local_category->p_input ||
121 !p_playlist->p_local_onelevel->p_input )
124 /* Link the nodes together. Todo: actually create them from the same input*/
125 p_playlist->p_local_onelevel->p_input->i_id =
126 p_playlist->p_local_category->p_input->i_id;
128 if( config_GetInt( p_playlist, "media-library") )
130 p_playlist->p_ml_category = playlist_NodeCreate( p_playlist,
131 _( "Media Library" ), p_playlist->p_root_category, 0 );
132 p_playlist->p_ml_onelevel = playlist_NodeCreate( p_playlist,
133 _( "Media Library" ), p_playlist->p_root_onelevel, 0 );
135 if(!p_playlist->p_ml_category || !p_playlist->p_ml_onelevel)
138 p_playlist->p_ml_category->i_flags |= PLAYLIST_RO_FLAG;
139 p_playlist->p_ml_onelevel->i_flags |= PLAYLIST_RO_FLAG;
140 p_playlist->p_ml_onelevel->p_input->i_id =
141 p_playlist->p_ml_category->p_input->i_id;
146 p_playlist->p_ml_category = p_playlist->p_ml_onelevel = NULL;
150 p_playlist->status.p_item = NULL;
151 p_playlist->status.p_node = p_playlist->p_local_onelevel;
152 p_playlist->request.b_request = VLC_FALSE;
153 p_playlist->status.i_status = PLAYLIST_STOPPED;
155 p_playlist->i_sort = SORT_ID;
156 p_playlist->i_order = ORDER_NORMAL;
158 b_save = p_playlist->b_auto_preparse;
159 p_playlist->b_auto_preparse = VLC_FALSE;
160 playlist_MLLoad( p_playlist );
161 p_playlist->b_auto_preparse = VLC_TRUE;
165 void playlist_Destroy( playlist_t *p_playlist )
167 var_Destroy( p_playlist, "intf-change" );
168 var_Destroy( p_playlist, "item-change" );
169 var_Destroy( p_playlist, "playlist-current" );
170 var_Destroy( p_playlist, "intf-popmenu" );
171 var_Destroy( p_playlist, "intf-show" );
172 var_Destroy( p_playlist, "play-and-stop" );
173 var_Destroy( p_playlist, "play-and-exit" );
174 var_Destroy( p_playlist, "random" );
175 var_Destroy( p_playlist, "repeat" );
176 var_Destroy( p_playlist, "loop" );
177 var_Destroy( p_playlist, "activity" );
179 vlc_mutex_destroy( &p_playlist->gc_lock );
180 vlc_object_detach( p_playlist );
181 free( p_playlist->p_internal );
182 vlc_object_destroy( p_playlist );
185 /* Destroy remaining objects */
186 static void ObjectGarbageCollector( playlist_t *p_playlist, vlc_bool_t b_force )
192 if( mdate() - p_playlist->gc_date < 1000000 )
194 p_playlist->b_cant_sleep = VLC_TRUE;
197 else if( p_playlist->gc_date == 0 )
201 vlc_mutex_lock( &p_playlist->gc_lock );
202 while( ( p_obj = vlc_object_find( p_playlist, VLC_OBJECT_VOUT,
205 if( p_obj->p_parent != VLC_OBJECT(p_playlist) )
207 vlc_object_release( p_obj );
210 msg_Dbg( p_playlist, "garbage collector destroying 1 vout" );
211 vlc_object_detach( p_obj );
212 vlc_object_release( p_obj );
213 vout_Destroy( (vout_thread_t *)p_obj );
215 while( ( p_obj = vlc_object_find( p_playlist, VLC_OBJECT_SOUT,
218 if( p_obj->p_parent != VLC_OBJECT(p_playlist) )
220 vlc_object_release( p_obj );
223 msg_Dbg( p_playlist, "garbage collector destroying 1 sout" );
224 vlc_object_detach( p_obj );
225 vlc_object_release( p_obj );
226 sout_DeleteInstance( (sout_instance_t*)p_obj );
228 p_playlist->b_cant_sleep = VLC_FALSE;
229 vlc_mutex_unlock( &p_playlist->gc_lock );
232 /** Main loop for the playlist */
233 void playlist_MainLoop( playlist_t *p_playlist )
235 playlist_item_t *p_item = NULL;
236 vlc_bool_t b_playexit = var_GetBool( p_playlist, "play-and-exit" );
239 if( p_playlist->b_reset_currently_playing &&
240 mdate() - p_playlist->last_rebuild_date > 30000 ) // 30 ms
242 ResetCurrentlyPlaying( p_playlist, var_GetBool( p_playlist, "random"),
243 p_playlist->status.p_item );
244 p_playlist->last_rebuild_date = mdate();
248 /* If there is an input, check that it doesn't need to die. */
249 if( p_playlist->p_input )
251 if( p_playlist->request.b_request && !p_playlist->p_input->b_die )
253 PL_DEBUG( "incoming request - stopping current input" );
254 input_StopThread( p_playlist->p_input );
257 /* This input is dead. Remove it ! */
258 if( p_playlist->p_input->b_dead )
261 input_thread_t *p_input;
262 PL_DEBUG( "dead input" );
264 p_input = p_playlist->p_input;
265 p_playlist->p_input = NULL;
267 /* Release the playlist lock, because we may get stuck
268 * in input_DestroyThread() for some time. */
272 input_DestroyThread( p_input );
276 p_playlist->gc_date = mdate();
277 p_playlist->b_cant_sleep = VLC_TRUE;
279 if( p_playlist->status.p_item->i_flags
280 & PLAYLIST_REMOVE_FLAG )
282 PL_DEBUG( "%s was marked for deletion, deleting",
283 PLI_NAME( p_playlist->status.p_item ) );
284 playlist_ItemDelete( p_playlist->status.p_item );
285 if( p_playlist->request.p_item == p_playlist->status.p_item )
286 p_playlist->request.p_item = NULL;
287 p_playlist->status.p_item = NULL;
290 i_activity= var_GetInteger( p_playlist, "activity") ;
291 var_SetInteger( p_playlist, "activity", i_activity -
292 DEFAULT_INPUT_ACTIVITY );
295 /* This input is dying, let it do */
296 else if( p_playlist->p_input->b_die )
298 PL_DEBUG( "dying input" );
300 msleep( 25000 ); // 25 ms
304 /* This input has finished, ask it to die ! */
305 else if( p_playlist->p_input->b_error
306 || p_playlist->p_input->b_eof )
308 PL_DEBUG( "finished input" );
309 input_StopThread( p_playlist->p_input );
310 /* No need to wait here, we'll wait in the p_input->b_die case */
313 else if( p_playlist->p_input->i_state != INIT_S )
316 ObjectGarbageCollector( p_playlist, VLC_FALSE );
322 /* No input. Several cases
323 * - No request, running status -> start new item
324 * - No request, stopped status -> collect garbage
325 * - Request, running requested -> start new item
326 * - Request, stopped requested -> collect garbage
328 if( (!p_playlist->request.b_request &&
329 p_playlist->status.i_status != PLAYLIST_STOPPED) ||
330 ( p_playlist->request.b_request &&
331 p_playlist->request.i_status != PLAYLIST_STOPPED ) )
333 msg_Dbg( p_playlist, "starting new item" );
334 p_item = playlist_NextItem( p_playlist );
338 msg_Dbg( p_playlist, "nothing to play" );
339 if( b_playexit == VLC_TRUE )
341 msg_Info( p_playlist, "end of playlist, exiting" );
342 vlc_object_kill( p_playlist->p_libvlc );
344 p_playlist->status.i_status = PLAYLIST_STOPPED;
347 ObjectGarbageCollector( p_playlist, VLC_TRUE );
350 playlist_PlayItem( p_playlist, p_item );
354 const vlc_bool_t b_gc_forced = p_playlist->status.i_status != PLAYLIST_STOPPED;
356 p_playlist->status.i_status = PLAYLIST_STOPPED;
357 if( p_playlist->status.p_item &&
358 p_playlist->status.p_item->i_flags & PLAYLIST_REMOVE_FLAG )
360 PL_DEBUG( "deleting item marked for deletion" );
361 playlist_ItemDelete( p_playlist->status.p_item );
362 p_playlist->status.p_item = NULL;
365 /* Collect garbage */
367 ObjectGarbageCollector( p_playlist, b_gc_forced );
374 /** Playlist dying last loop */
375 void playlist_LastLoop( playlist_t *p_playlist )
379 /* If there is an input, kill it */
384 if( p_playlist->p_input == NULL )
390 if( p_playlist->p_input->b_dead )
392 input_thread_t *p_input;
394 /* Unlink current input */
395 p_input = p_playlist->p_input;
396 p_playlist->p_input = NULL;
400 input_DestroyThread( p_input );
403 else if( p_playlist->p_input->b_die )
405 /* This input is dying, leave it alone */
408 else if( p_playlist->p_input->b_error || p_playlist->p_input->b_eof )
410 input_StopThread( p_playlist->p_input );
416 p_playlist->p_input->b_eof = 1;
421 msleep( INTF_IDLE_SLEEP );
424 /* close all remaining sout */
425 while( ( p_obj = vlc_object_find( p_playlist,
426 VLC_OBJECT_SOUT, FIND_CHILD ) ) )
428 vlc_object_detach( p_obj );
429 vlc_object_release( p_obj );
430 sout_DeleteInstance( (sout_instance_t*)p_obj );
433 /* close all remaining vout */
434 while( ( p_obj = vlc_object_find( p_playlist,
435 VLC_OBJECT_VOUT, FIND_CHILD ) ) )
437 vlc_object_detach( p_obj );
438 vlc_object_release( p_obj );
439 vout_Destroy( (vout_thread_t *)p_obj );
442 while( p_playlist->p_internal->i_asds )
444 playlist_ServicesDiscoveryRemove( p_playlist,
445 p_playlist->p_internal->pp_asds[0]->p_sd->psz_module );
448 playlist_MLDump( p_playlist );
451 /* Go through all items, and simply free everything without caring
452 * about the tree structure. Do not decref, it will be done by doing
453 * the same thing on the input items array */
454 FOREACH_ARRAY( playlist_item_t *p_del, p_playlist->all_items )
455 free( p_del->pp_children );
458 ARRAY_RESET( p_playlist->all_items );
460 FOREACH_ARRAY( input_item_t *p_del, p_playlist->input_items )
461 input_ItemClean( p_del );
464 ARRAY_RESET( p_playlist->input_items );
466 ARRAY_RESET( p_playlist->items );
467 ARRAY_RESET( p_playlist->current );
472 /** Main loop for preparser queue */
473 void playlist_PreparseLoop( playlist_preparse_t *p_obj )
475 playlist_t *p_playlist = (playlist_t *)p_obj->p_parent;
476 input_item_t *p_current;
480 while( !p_playlist->b_die )
482 vlc_mutex_lock( &p_obj->object_lock );
483 while( p_obj->i_waiting == 0 )
485 vlc_cond_wait( &p_obj->object_wait, &p_obj->object_lock );
486 if( p_playlist->b_die )
488 vlc_mutex_unlock( &p_obj->object_lock );
493 p_current = p_obj->pp_waiting[0];
494 REMOVE_ELEM( p_obj->pp_waiting, p_obj->i_waiting, 0 );
495 vlc_mutex_unlock( &p_obj->object_lock );
500 if( !strncmp( p_current->psz_uri, "file:", 5 ) )
502 stats_TimerStart( p_playlist, "Preparse run",
503 STATS_TIMER_PREPARSE );
504 /* Do not preparse if it is already done (like by playing it) */
505 if( !input_item_IsPreparsed( p_current ) )
508 input_Preparse( p_playlist, p_current );
511 stats_TimerStop( p_playlist, STATS_TIMER_PREPARSE );
513 input_item_SetPreparsed( p_current, VLC_TRUE );
514 var_SetInteger( p_playlist, "item-change", p_current->i_id );
517 /* If we haven't retrieved enough meta, add to secondary queue
518 * which will run the "meta fetchers".
519 * This only checks for meta, not for art
520 * \todo don't do this for things we won't get meta for, like vids
522 char *psz_arturl = input_item_GetArtURL( p_current );
523 char *psz_name = input_item_GetName( p_current );
524 if( !input_MetaSatisfied( p_playlist, p_current, &i_m, &i_o ) )
527 PL_DEBUG("need to fetch meta for %s", p_current->psz_name );
528 p.p_item = p_current;
529 p.b_fetch_art = VLC_FALSE;
530 vlc_mutex_lock( &p_playlist->p_fetcher->object_lock );
531 INSERT_ELEM( p_playlist->p_fetcher->p_waiting,
532 p_playlist->p_fetcher->i_waiting,
533 p_playlist->p_fetcher->i_waiting, p);
534 vlc_cond_signal( &p_playlist->p_fetcher->object_wait );
535 vlc_mutex_unlock( &p_playlist->p_fetcher->object_lock );
537 /* We already have all needed meta, but we need art right now */
538 else if( p_playlist->p_fetcher->i_art_policy == ALBUM_ART_ALL &&
539 EMPTY_STR( psz_arturl ) )
542 PL_DEBUG("meta ok for %s, need to fetch art", psz_name );
543 p.p_item = p_current;
544 p.b_fetch_art = VLC_TRUE;
545 vlc_mutex_lock( &p_playlist->p_fetcher->object_lock );
546 INSERT_ELEM( p_playlist->p_fetcher->p_waiting,
547 p_playlist->p_fetcher->i_waiting,
548 p_playlist->p_fetcher->i_waiting, p);
549 vlc_cond_signal( &p_playlist->p_fetcher->object_wait );
550 vlc_mutex_unlock( &p_playlist->p_fetcher->object_lock );
554 PL_DEBUG( "no fetch required for %s (art currently %s)",
555 psz_name, psz_arturl );
556 vlc_gc_decref( p_current );
565 vlc_mutex_lock( &p_obj->object_lock );
566 i_activity = var_GetInteger( p_playlist, "activity" );
567 if( i_activity < 0 ) i_activity = 0;
568 vlc_mutex_unlock( &p_obj->object_lock );
569 /* Sleep at least 1ms */
570 msleep( (i_activity+1) * 1000 );
574 /** Main loop for secondary preparser queue */
575 void playlist_FetcherLoop( playlist_fetcher_t *p_obj )
577 playlist_t *p_playlist = (playlist_t *)p_obj->p_parent;
578 vlc_bool_t b_fetch_art;
579 input_item_t *p_item;
582 while( !p_playlist->b_die )
584 vlc_mutex_lock( &p_obj->object_lock );
585 while( p_obj->i_waiting == 0 )
587 vlc_cond_wait( &p_obj->object_wait, &p_obj->object_lock );
588 if( p_playlist->b_die )
590 vlc_mutex_unlock( &p_obj->object_lock );
595 b_fetch_art = p_obj->p_waiting->b_fetch_art;
596 p_item = p_obj->p_waiting->p_item;
597 REMOVE_ELEM( p_obj->p_waiting, p_obj->i_waiting, 0 );
598 vlc_mutex_unlock( &p_obj->object_lock );
603 input_MetaFetch( p_playlist, p_item );
604 var_SetInteger( p_playlist, "item-change", p_item->i_id );
605 /* Fetch right now */
606 if( p_playlist->p_fetcher->i_art_policy == ALBUM_ART_ALL )
608 vlc_mutex_lock( &p_obj->object_lock );
611 p.b_fetch_art = VLC_TRUE;
612 INSERT_ELEM( p_playlist->p_fetcher->p_waiting,
613 p_playlist->p_fetcher->i_waiting,
615 PL_DEBUG("meta fetched for %s, get art", p_item->psz_name);
616 vlc_mutex_unlock( &p_obj->object_lock );
620 vlc_gc_decref( p_item );
626 /* Check if it is not yet preparsed and if so wait for it (at most 0.5s)
627 * (This can happen if we fetch art on play)
628 * FIXME this doesn't work if we need to fetch meta before art ... */
629 for( i_ret = 0; i_ret < 10 && !input_item_IsPreparsed( p_item ); i_ret++ )
633 b_break = ( !p_playlist->p_input || input_GetItem(p_playlist->p_input) != p_item ||
634 p_playlist->p_input->b_die || p_playlist->p_input->b_eof || p_playlist->p_input->b_error );
641 i_ret = input_ArtFind( p_playlist, p_item );
644 PL_DEBUG("downloading art for %s", p_item->psz_name );
645 if( input_DownloadAndCacheArt( p_playlist, p_item ) )
646 input_item_SetArtNotFound( p_item, VLC_TRUE );
648 input_item_SetArtFetched( p_item, VLC_TRUE );
649 var_SetInteger( p_playlist, "item-change",
653 else if( i_ret == 0 ) /* Was in cache */
655 PL_DEBUG("found art for %s in cache", p_item->psz_name );
656 input_item_SetArtFetched( p_item, VLC_TRUE );
657 var_SetInteger( p_playlist, "item-change", p_item->i_id );
661 PL_DEBUG("art not found for %s", p_item->psz_name );
662 input_item_SetArtNotFound( p_item, VLC_TRUE );
664 vlc_gc_decref( p_item );
667 vlc_mutex_lock( &p_obj->object_lock );
668 i_activity = var_GetInteger( p_playlist, "activity" );
669 if( i_activity < 0 ) i_activity = 0;
670 vlc_mutex_unlock( &p_obj->object_lock );
671 /* Sleep at least 1ms */
672 msleep( (i_activity+1) * 1000 );
676 static void VariablesInit( playlist_t *p_playlist )
679 /* These variables control updates */
680 var_Create( p_playlist, "intf-change", VLC_VAR_BOOL );
681 val.b_bool = VLC_TRUE;
682 var_Set( p_playlist, "intf-change", val );
684 var_Create( p_playlist, "item-change", VLC_VAR_INTEGER );
686 var_Set( p_playlist, "item-change", val );
688 var_Create( p_playlist, "item-deleted", VLC_VAR_INTEGER );
690 var_Set( p_playlist, "item-deleted", val );
692 var_Create( p_playlist, "item-append", VLC_VAR_ADDRESS );
694 var_Create( p_playlist, "playlist-current", VLC_VAR_INTEGER );
696 var_Set( p_playlist, "playlist-current", val );
698 var_Create( p_playlist, "intf-popupmenu", VLC_VAR_BOOL );
700 var_Create( p_playlist, "intf-show", VLC_VAR_BOOL );
701 val.b_bool = VLC_TRUE;
702 var_Set( p_playlist, "intf-show", val );
704 var_Create( p_playlist, "activity", VLC_VAR_INTEGER );
705 var_SetInteger( p_playlist, "activity", 0 );
707 /* Variables to control playback */
708 var_CreateGetBool( p_playlist, "play-and-stop" );
709 var_CreateGetBool( p_playlist, "play-and-exit" );
710 var_CreateGetBool( p_playlist, "random" );
711 var_CreateGetBool( p_playlist, "repeat" );
712 var_CreateGetBool( p_playlist, "loop" );
714 var_AddCallback( p_playlist, "random", RandomCallback, NULL );