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 TAB_INIT( p_playlist->i_sds, p_playlist->pp_sds );
71 p_parent->p_libvlc->p_playlist = p_playlist;
73 VariablesInit( p_playlist );
75 /* Initialise data structures */
76 vlc_mutex_init( p_playlist, &p_playlist->gc_lock );
77 p_playlist->i_last_playlist_id = 0;
78 p_playlist->i_last_input_id = 0;
79 p_playlist->p_input = NULL;
81 p_playlist->gc_date = 0;
82 p_playlist->b_cant_sleep = VLC_FALSE;
84 ARRAY_INIT( p_playlist->items );
85 ARRAY_INIT( p_playlist->all_items );
86 ARRAY_INIT( p_playlist->input_items );
87 ARRAY_INIT( p_playlist->current );
89 p_playlist->i_current_index = 0;
90 p_playlist->b_reset_currently_playing = VLC_TRUE;
91 p_playlist->last_rebuild_date = 0;
93 i_tree = var_CreateGetBool( p_playlist, "playlist-tree" );
94 p_playlist->b_always_tree = (i_tree == 1);
95 p_playlist->b_never_tree = (i_tree == 2);
97 p_playlist->b_doing_ml = VLC_FALSE;
99 p_playlist->b_auto_preparse =
100 var_CreateGetBool( p_playlist, "auto-preparse") ;
102 p_playlist->p_root_category = playlist_NodeCreate( p_playlist, NULL, NULL,
104 p_playlist->p_root_onelevel = playlist_NodeCreate( p_playlist, NULL, NULL,
107 if( !p_playlist->p_root_category || !p_playlist->p_root_onelevel )
110 /* Create playlist and media library */
111 p_playlist->p_local_category = playlist_NodeCreate( p_playlist,
112 _( "Playlist" ),p_playlist->p_root_category, 0 );
113 p_playlist->p_local_onelevel = playlist_NodeCreate( p_playlist,
114 _( "Playlist" ), p_playlist->p_root_onelevel, 0 );
115 p_playlist->p_local_category->i_flags |= PLAYLIST_RO_FLAG;
116 p_playlist->p_local_onelevel->i_flags |= PLAYLIST_RO_FLAG;
118 if( !p_playlist->p_local_category || !p_playlist->p_local_onelevel ||
119 !p_playlist->p_local_category->p_input ||
120 !p_playlist->p_local_onelevel->p_input )
123 /* Link the nodes together. Todo: actually create them from the same input*/
124 p_playlist->p_local_onelevel->p_input->i_id =
125 p_playlist->p_local_category->p_input->i_id;
127 if( config_GetInt( p_playlist, "media-library") )
129 p_playlist->p_ml_category = playlist_NodeCreate( p_playlist,
130 _( "Media Library" ), p_playlist->p_root_category, 0 );
131 p_playlist->p_ml_onelevel = playlist_NodeCreate( p_playlist,
132 _( "Media Library" ), p_playlist->p_root_onelevel, 0 );
134 if(!p_playlist->p_ml_category || !p_playlist->p_ml_onelevel)
137 p_playlist->p_ml_category->i_flags |= PLAYLIST_RO_FLAG;
138 p_playlist->p_ml_onelevel->i_flags |= PLAYLIST_RO_FLAG;
139 p_playlist->p_ml_onelevel->p_input->i_id =
140 p_playlist->p_ml_category->p_input->i_id;
145 p_playlist->p_ml_category = p_playlist->p_ml_onelevel = NULL;
149 p_playlist->status.p_item = NULL;
150 p_playlist->status.p_node = p_playlist->p_local_onelevel;
151 p_playlist->request.b_request = VLC_FALSE;
152 p_playlist->status.i_status = PLAYLIST_STOPPED;
154 p_playlist->i_sort = SORT_ID;
155 p_playlist->i_order = ORDER_NORMAL;
157 b_save = p_playlist->b_auto_preparse;
158 p_playlist->b_auto_preparse = VLC_FALSE;
159 playlist_MLLoad( p_playlist );
160 p_playlist->b_auto_preparse = VLC_TRUE;
164 void playlist_Destroy( playlist_t *p_playlist )
166 var_Destroy( p_playlist, "intf-change" );
167 var_Destroy( p_playlist, "item-change" );
168 var_Destroy( p_playlist, "playlist-current" );
169 var_Destroy( p_playlist, "intf-popmenu" );
170 var_Destroy( p_playlist, "intf-show" );
171 var_Destroy( p_playlist, "play-and-stop" );
172 var_Destroy( p_playlist, "play-and-exit" );
173 var_Destroy( p_playlist, "random" );
174 var_Destroy( p_playlist, "repeat" );
175 var_Destroy( p_playlist, "loop" );
176 var_Destroy( p_playlist, "activity" );
178 vlc_mutex_destroy( &p_playlist->gc_lock );
179 vlc_object_detach( p_playlist );
180 vlc_object_destroy( p_playlist );
183 /* Destroy remaining objects */
184 static void ObjectGarbageCollector( playlist_t *p_playlist, vlc_bool_t b_force )
190 if( mdate() - p_playlist->gc_date < 1000000 )
192 p_playlist->b_cant_sleep = VLC_TRUE;
195 else if( p_playlist->gc_date == 0 )
199 vlc_mutex_lock( &p_playlist->gc_lock );
200 while( ( p_obj = vlc_object_find( p_playlist, VLC_OBJECT_VOUT,
203 if( p_obj->p_parent != VLC_OBJECT(p_playlist) )
205 vlc_object_release( p_obj );
208 msg_Dbg( p_playlist, "garbage collector destroying 1 vout" );
209 vlc_object_detach( p_obj );
210 vlc_object_release( p_obj );
211 vout_Destroy( (vout_thread_t *)p_obj );
213 while( ( p_obj = vlc_object_find( p_playlist, VLC_OBJECT_SOUT,
216 if( p_obj->p_parent != VLC_OBJECT(p_playlist) )
218 vlc_object_release( p_obj );
221 msg_Dbg( p_playlist, "garbage collector destroying 1 sout" );
222 vlc_object_detach( p_obj );
223 vlc_object_release( p_obj );
224 sout_DeleteInstance( (sout_instance_t*)p_obj );
226 p_playlist->b_cant_sleep = VLC_FALSE;
227 vlc_mutex_unlock( &p_playlist->gc_lock );
230 /** Main loop for the playlist */
231 void playlist_MainLoop( playlist_t *p_playlist )
233 playlist_item_t *p_item = NULL;
234 vlc_bool_t b_playexit = var_GetBool( p_playlist, "play-and-exit" );
237 if( p_playlist->b_reset_currently_playing &&
238 mdate() - p_playlist->last_rebuild_date > 30000 ) // 30 ms
240 ResetCurrentlyPlaying( p_playlist, var_GetBool( p_playlist, "random"),
241 p_playlist->status.p_item );
242 p_playlist->last_rebuild_date = mdate();
246 /* If there is an input, check that it doesn't need to die. */
247 if( p_playlist->p_input )
249 if( p_playlist->request.b_request && !p_playlist->p_input->b_die )
251 PL_DEBUG( "incoming request - stopping current input" );
252 input_StopThread( p_playlist->p_input );
255 /* This input is dead. Remove it ! */
256 if( p_playlist->p_input->b_dead )
259 input_thread_t *p_input;
260 PL_DEBUG( "dead input" );
262 p_input = p_playlist->p_input;
263 p_playlist->p_input = NULL;
265 /* Release the playlist lock, because we may get stuck
266 * in input_DestroyThread() for some time. */
270 input_DestroyThread( p_input );
274 p_playlist->gc_date = mdate();
275 p_playlist->b_cant_sleep = VLC_TRUE;
277 if( p_playlist->status.p_item->i_flags
278 & PLAYLIST_REMOVE_FLAG )
280 PL_DEBUG( "%s was marked for deletion, deleting",
281 PLI_NAME( p_playlist->status.p_item ) );
282 playlist_ItemDelete( p_playlist->status.p_item );
283 if( p_playlist->request.p_item == p_playlist->status.p_item )
284 p_playlist->request.p_item = NULL;
285 p_playlist->status.p_item = NULL;
288 i_activity= var_GetInteger( p_playlist, "activity") ;
289 var_SetInteger( p_playlist, "activity", i_activity -
290 DEFAULT_INPUT_ACTIVITY );
293 /* This input is dying, let it do */
294 else if( p_playlist->p_input->b_die )
296 PL_DEBUG( "dying input" );
298 msleep( 25000 ); // 25 ms
302 /* This input has finished, ask it to die ! */
303 else if( p_playlist->p_input->b_error
304 || p_playlist->p_input->b_eof )
306 PL_DEBUG( "finished input" );
307 input_StopThread( p_playlist->p_input );
308 /* No need to wait here, we'll wait in the p_input->b_die case */
311 else if( p_playlist->p_input->i_state != INIT_S )
314 ObjectGarbageCollector( p_playlist, VLC_FALSE );
320 /* No input. Several cases
321 * - No request, running status -> start new item
322 * - No request, stopped status -> collect garbage
323 * - Request, running requested -> start new item
324 * - Request, stopped requested -> collect garbage
326 if( (!p_playlist->request.b_request &&
327 p_playlist->status.i_status != PLAYLIST_STOPPED) ||
328 ( p_playlist->request.b_request &&
329 p_playlist->request.i_status != PLAYLIST_STOPPED ) )
331 msg_Dbg( p_playlist, "starting new item" );
332 p_item = playlist_NextItem( p_playlist );
336 msg_Dbg( p_playlist, "nothing to play" );
337 p_playlist->status.i_status = PLAYLIST_STOPPED;
340 if( b_playexit == VLC_TRUE )
342 msg_Info( p_playlist, "end of playlist, exiting" );
343 vlc_object_kill( p_playlist->p_libvlc );
345 ObjectGarbageCollector( p_playlist, VLC_TRUE );
348 playlist_PlayItem( p_playlist, p_item );
352 const vlc_bool_t b_gc_forced = p_playlist->status.i_status != PLAYLIST_STOPPED;
354 p_playlist->status.i_status = PLAYLIST_STOPPED;
355 if( p_playlist->status.p_item &&
356 p_playlist->status.p_item->i_flags & PLAYLIST_REMOVE_FLAG )
358 PL_DEBUG( "deleting item marked for deletion" );
359 playlist_ItemDelete( p_playlist->status.p_item );
360 p_playlist->status.p_item = NULL;
363 /* Collect garbage */
365 ObjectGarbageCollector( p_playlist, b_gc_forced );
372 /** Playlist dying last loop */
373 void playlist_LastLoop( playlist_t *p_playlist )
377 /* If there is an input, kill it */
382 if( p_playlist->p_input == NULL )
388 if( p_playlist->p_input->b_dead )
390 input_thread_t *p_input;
392 /* Unlink current input */
393 p_input = p_playlist->p_input;
394 p_playlist->p_input = NULL;
398 input_DestroyThread( p_input );
401 else if( p_playlist->p_input->b_die )
403 /* This input is dying, leave it alone */
406 else if( p_playlist->p_input->b_error || p_playlist->p_input->b_eof )
408 input_StopThread( p_playlist->p_input );
414 p_playlist->p_input->b_eof = 1;
419 msleep( INTF_IDLE_SLEEP );
422 /* close all remaining sout */
423 while( ( p_obj = vlc_object_find( p_playlist,
424 VLC_OBJECT_SOUT, FIND_CHILD ) ) )
426 vlc_object_detach( p_obj );
427 vlc_object_release( p_obj );
428 sout_DeleteInstance( (sout_instance_t*)p_obj );
431 /* close all remaining vout */
432 while( ( p_obj = vlc_object_find( p_playlist,
433 VLC_OBJECT_VOUT, FIND_CHILD ) ) )
435 vlc_object_detach( p_obj );
436 vlc_object_release( p_obj );
437 vout_Destroy( (vout_thread_t *)p_obj );
440 while( p_playlist->i_sds )
442 playlist_ServicesDiscoveryRemove( p_playlist,
443 p_playlist->pp_sds[0]->p_sd->psz_module );
446 playlist_MLDump( p_playlist );
449 /* Go through all items, and simply free everything without caring
450 * about the tree structure. Do not decref, it will be done by doing
451 * the same thing on the input items array */
452 FOREACH_ARRAY( playlist_item_t *p_del, p_playlist->all_items )
453 free( p_del->pp_children );
456 ARRAY_RESET( p_playlist->all_items );
458 FOREACH_ARRAY( input_item_t *p_del, p_playlist->input_items )
459 input_ItemClean( p_del );
462 ARRAY_RESET( p_playlist->input_items );
464 ARRAY_RESET( p_playlist->items );
465 ARRAY_RESET( p_playlist->current );
470 /** Main loop for preparser queue */
471 void playlist_PreparseLoop( playlist_preparse_t *p_obj )
473 playlist_t *p_playlist = (playlist_t *)p_obj->p_parent;
474 input_item_t *p_current;
478 while( !p_playlist->b_die )
480 vlc_mutex_lock( &p_obj->object_lock );
481 while( p_obj->i_waiting == 0 )
483 vlc_cond_wait( &p_obj->object_wait, &p_obj->object_lock );
484 if( p_playlist->b_die )
486 vlc_mutex_unlock( &p_obj->object_lock );
491 p_current = p_obj->pp_waiting[0];
492 REMOVE_ELEM( p_obj->pp_waiting, p_obj->i_waiting, 0 );
493 vlc_mutex_unlock( &p_obj->object_lock );
498 if( !strncmp( p_current->psz_uri, "file:", 5 ) )
500 stats_TimerStart( p_playlist, "Preparse run",
501 STATS_TIMER_PREPARSE );
502 /* Do not preparse if it is already done (like by playing it) */
503 if( !input_item_IsPreparsed( p_current ) )
506 input_Preparse( p_playlist, p_current );
509 stats_TimerStop( p_playlist, STATS_TIMER_PREPARSE );
511 input_item_SetPreparsed( p_current, VLC_TRUE );
512 var_SetInteger( p_playlist, "item-change", p_current->i_id );
515 /* If we haven't retrieved enough meta, add to secondary queue
516 * which will run the "meta fetchers".
517 * This only checks for meta, not for art
518 * \todo don't do this for things we won't get meta for, like vids
520 char *psz_arturl = input_item_GetArtURL( p_current );
521 char *psz_name = input_item_GetName( p_current );
522 if( !input_MetaSatisfied( p_playlist, p_current, &i_m, &i_o ) )
525 PL_DEBUG("need to fetch meta for %s", p_current->psz_name );
526 p.p_item = p_current;
527 p.b_fetch_art = VLC_FALSE;
528 vlc_mutex_lock( &p_playlist->p_fetcher->object_lock );
529 INSERT_ELEM( p_playlist->p_fetcher->p_waiting,
530 p_playlist->p_fetcher->i_waiting,
531 p_playlist->p_fetcher->i_waiting, p);
532 vlc_cond_signal( &p_playlist->p_fetcher->object_wait );
533 vlc_mutex_unlock( &p_playlist->p_fetcher->object_lock );
535 /* We already have all needed meta, but we need art right now */
536 else if( p_playlist->p_fetcher->i_art_policy == ALBUM_ART_ALL &&
538 strncmp( psz_arturl, "file://", strlen("file://") ) )
541 PL_DEBUG("meta ok for %s, need to fetch art", psz_name );
542 p.p_item = p_current;
543 p.b_fetch_art = VLC_TRUE;
544 vlc_mutex_lock( &p_playlist->p_fetcher->object_lock );
545 INSERT_ELEM( p_playlist->p_fetcher->p_waiting,
546 p_playlist->p_fetcher->i_waiting,
547 p_playlist->p_fetcher->i_waiting, p);
548 vlc_cond_signal( &p_playlist->p_fetcher->object_wait );
549 vlc_mutex_unlock( &p_playlist->p_fetcher->object_lock );
553 PL_DEBUG( "no fetch required for %s (art currently %s)",
554 psz_name, psz_arturl );
555 vlc_gc_decref( p_current );
564 vlc_mutex_lock( &p_obj->object_lock );
565 i_activity = var_GetInteger( p_playlist, "activity" );
566 if( i_activity < 0 ) i_activity = 0;
567 vlc_mutex_unlock( &p_obj->object_lock );
568 /* Sleep at least 1ms */
569 msleep( (i_activity+1) * 1000 );
573 /** Main loop for secondary preparser queue */
574 void playlist_FetcherLoop( playlist_fetcher_t *p_obj )
576 playlist_t *p_playlist = (playlist_t *)p_obj->p_parent;
577 vlc_bool_t b_fetch_art;
578 input_item_t *p_item;
581 while( !p_playlist->b_die )
583 vlc_mutex_lock( &p_obj->object_lock );
584 while( p_obj->i_waiting == 0 )
586 vlc_cond_wait( &p_obj->object_wait, &p_obj->object_lock );
587 if( p_playlist->b_die )
589 vlc_mutex_unlock( &p_obj->object_lock );
594 b_fetch_art = p_obj->p_waiting->b_fetch_art;
595 p_item = p_obj->p_waiting->p_item;
596 REMOVE_ELEM( p_obj->p_waiting, p_obj->i_waiting, 0 );
597 vlc_mutex_unlock( &p_obj->object_lock );
602 input_MetaFetch( p_playlist, p_item );
603 var_SetInteger( p_playlist, "item-change", p_item->i_id );
604 /* Fetch right now */
605 if( p_playlist->p_fetcher->i_art_policy == ALBUM_ART_ALL )
607 vlc_mutex_lock( &p_obj->object_lock );
610 p.b_fetch_art = VLC_TRUE;
611 INSERT_ELEM( p_playlist->p_fetcher->p_waiting,
612 p_playlist->p_fetcher->i_waiting,
614 PL_DEBUG("meta fetched for %s, get art", p_item->psz_name);
615 vlc_mutex_unlock( &p_obj->object_lock );
619 vlc_gc_decref( p_item );
625 /* Check if it is not yet preparsed and if so wait for it (at most 0.5s)
626 * (This can happen if we fetch art on play)
627 * FIXME this doesn't work if we need to fetch meta before art ... */
628 for( i_ret = 0; i_ret < 10 && !input_item_IsPreparsed( p_item ); i_ret++ )
632 b_break = ( !p_playlist->p_input || input_GetItem(p_playlist->p_input) != p_item ||
633 p_playlist->p_input->b_die || p_playlist->p_input->b_eof || p_playlist->p_input->b_error );
640 i_ret = input_ArtFind( p_playlist, p_item );
643 PL_DEBUG("downloading art for %s", p_item->psz_name );
644 if( input_DownloadAndCacheArt( p_playlist, p_item ) )
645 input_item_SetArtNotFound( p_item, VLC_TRUE );
647 input_item_SetArtFetched( p_item, VLC_TRUE );
648 var_SetInteger( p_playlist, "item-change",
652 else if( i_ret == 0 ) /* Was in cache */
654 PL_DEBUG("found art for %s in cache", p_item->psz_name );
655 input_item_SetArtFetched( p_item, VLC_TRUE );
656 var_SetInteger( p_playlist, "item-change", p_item->i_id );
660 PL_DEBUG("art not found for %s", p_item->psz_name );
661 input_item_SetArtNotFound( p_item, VLC_TRUE );
663 vlc_gc_decref( p_item );
666 vlc_mutex_lock( &p_obj->object_lock );
667 i_activity = var_GetInteger( p_playlist, "activity" );
668 if( i_activity < 0 ) i_activity = 0;
669 vlc_mutex_unlock( &p_obj->object_lock );
670 /* Sleep at least 1ms */
671 msleep( (i_activity+1) * 1000 );
675 static void VariablesInit( playlist_t *p_playlist )
678 /* These variables control updates */
679 var_Create( p_playlist, "intf-change", VLC_VAR_BOOL );
680 val.b_bool = VLC_TRUE;
681 var_Set( p_playlist, "intf-change", val );
683 var_Create( p_playlist, "item-change", VLC_VAR_INTEGER );
685 var_Set( p_playlist, "item-change", val );
687 var_Create( p_playlist, "item-deleted", VLC_VAR_INTEGER );
689 var_Set( p_playlist, "item-deleted", val );
691 var_Create( p_playlist, "item-append", VLC_VAR_ADDRESS );
693 var_Create( p_playlist, "playlist-current", VLC_VAR_INTEGER );
695 var_Set( p_playlist, "playlist-current", val );
697 var_Create( p_playlist, "intf-popupmenu", VLC_VAR_BOOL );
699 var_Create( p_playlist, "intf-show", VLC_VAR_BOOL );
700 val.b_bool = VLC_TRUE;
701 var_Set( p_playlist, "intf-show", val );
703 var_Create( p_playlist, "activity", VLC_VAR_INTEGER );
704 var_SetInteger( p_playlist, "activity", 0 );
706 /* Variables to control playback */
707 var_CreateGetBool( p_playlist, "play-and-stop" );
708 var_CreateGetBool( p_playlist, "play-and-exit" );
709 var_CreateGetBool( p_playlist, "random" );
710 var_CreateGetBool( p_playlist, "repeat" );
711 var_CreateGetBool( p_playlist, "loop" );
713 var_AddCallback( p_playlist, "random", RandomCallback, NULL );