1 /*****************************************************************************
2 * engine.c : Run the playlist and handle its control
3 *****************************************************************************
4 * Copyright (C) 1999-2004 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 ((playlist_t*)p_this)->b_reset_currently_playing = VLC_TRUE;
42 playlist_Signal( ((playlist_t*)p_this) );
49 * Create a playlist structure.
50 * \param p_parent the vlc object that is to be the parent of this playlist
51 * \return a pointer to the created playlist, or NULL on error
53 playlist_t * playlist_Create( vlc_object_t *p_parent )
55 playlist_t *p_playlist;
59 /* Allocate structure */
60 p_playlist = vlc_object_create( p_parent, VLC_OBJECT_PLAYLIST );
63 msg_Err( p_parent, "out of memory" );
66 p_parent->p_libvlc->p_playlist = p_playlist;
68 VariablesInit( p_playlist );
70 /* Initialise data structures */
71 vlc_mutex_init( p_playlist, &p_playlist->gc_lock );
72 p_playlist->i_last_playlist_id = 0;
73 p_playlist->i_last_input_id = 0;
74 p_playlist->p_input = NULL;
76 p_playlist->gc_date = 0;
77 p_playlist->b_cant_sleep = VLC_FALSE;
79 ARRAY_INIT( p_playlist->items );
80 ARRAY_INIT( p_playlist->all_items );
81 ARRAY_INIT( p_playlist->input_items );
82 ARRAY_INIT( p_playlist->current );
84 p_playlist->i_current_index = 0;
85 p_playlist->b_reset_currently_playing = VLC_TRUE;
86 p_playlist->last_rebuild_date = 0;
88 i_tree = var_CreateGetBool( p_playlist, "playlist-tree" );
89 p_playlist->b_always_tree = (i_tree == 1);
90 p_playlist->b_never_tree = (i_tree == 2);
92 p_playlist->b_doing_ml = VLC_FALSE;
94 p_playlist->b_auto_preparse =
95 var_CreateGetBool( p_playlist, "auto-preparse") ;
97 p_playlist->p_root_category = playlist_NodeCreate( p_playlist, NULL, NULL,
99 p_playlist->p_root_onelevel = playlist_NodeCreate( p_playlist, NULL, NULL,
102 if( !p_playlist->p_root_category || !p_playlist->p_root_onelevel )
105 /* Create playlist and media library */
106 p_playlist->p_local_category = playlist_NodeCreate( p_playlist,
107 _( "Playlist" ),p_playlist->p_root_category, 0 );
108 p_playlist->p_local_onelevel = playlist_NodeCreate( p_playlist,
109 _( "Playlist" ), p_playlist->p_root_onelevel, 0 );
110 p_playlist->p_local_category->i_flags |= PLAYLIST_RO_FLAG;
111 p_playlist->p_local_onelevel->i_flags |= PLAYLIST_RO_FLAG;
113 if( !p_playlist->p_local_category || !p_playlist->p_local_onelevel ||
114 !p_playlist->p_local_category->p_input ||
115 !p_playlist->p_local_onelevel->p_input )
118 /* Link the nodes together. Todo: actually create them from the same input*/
119 p_playlist->p_local_onelevel->p_input->i_id =
120 p_playlist->p_local_category->p_input->i_id;
122 if( config_GetInt( p_playlist, "media-library") )
124 p_playlist->p_ml_category = playlist_NodeCreate( p_playlist,
125 _( "Media Library" ), p_playlist->p_root_category, 0 );
126 p_playlist->p_ml_onelevel = playlist_NodeCreate( p_playlist,
127 _( "Media Library" ), p_playlist->p_root_onelevel, 0 );
129 if(!p_playlist->p_ml_category || !p_playlist->p_ml_onelevel)
132 p_playlist->p_ml_category->i_flags |= PLAYLIST_RO_FLAG;
133 p_playlist->p_ml_onelevel->i_flags |= PLAYLIST_RO_FLAG;
134 p_playlist->p_ml_onelevel->p_input->i_id =
135 p_playlist->p_ml_category->p_input->i_id;
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;
152 b_save = p_playlist->b_auto_preparse;
153 p_playlist->b_auto_preparse = VLC_FALSE;
154 playlist_MLLoad( p_playlist );
155 p_playlist->b_auto_preparse = VLC_TRUE;
159 void playlist_Destroy( playlist_t *p_playlist )
161 var_Destroy( p_playlist, "intf-change" );
162 var_Destroy( p_playlist, "item-change" );
163 var_Destroy( p_playlist, "playlist-current" );
164 var_Destroy( p_playlist, "intf-popmenu" );
165 var_Destroy( p_playlist, "intf-show" );
166 var_Destroy( p_playlist, "play-and-stop" );
167 var_Destroy( p_playlist, "play-and-exit" );
168 var_Destroy( p_playlist, "random" );
169 var_Destroy( p_playlist, "repeat" );
170 var_Destroy( p_playlist, "loop" );
171 var_Destroy( p_playlist, "activity" );
173 vlc_mutex_destroy( &p_playlist->gc_lock );
174 vlc_object_detach( p_playlist );
175 vlc_object_destroy( p_playlist );
178 /* Destroy remaining objects */
179 static void ObjectGarbageCollector( playlist_t *p_playlist )
183 if( mdate() - p_playlist->gc_date < 1000000 )
185 p_playlist->b_cant_sleep = VLC_TRUE;
188 else if( p_playlist->gc_date == 0 )
191 vlc_mutex_lock( &p_playlist->gc_lock );
192 while( ( p_obj = vlc_object_find( p_playlist, VLC_OBJECT_VOUT,
195 if( p_obj->p_parent != (vlc_object_t*)p_playlist )
197 vlc_object_release( p_obj );
200 msg_Dbg( p_playlist, "garbage collector destroying 1 vout" );
201 vlc_object_detach( p_obj );
202 vlc_object_release( p_obj );
203 vout_Destroy( (vout_thread_t *)p_obj );
205 while( ( p_obj = vlc_object_find( p_playlist, VLC_OBJECT_SOUT,
208 if( p_obj->p_parent != (vlc_object_t*)p_playlist )
210 vlc_object_release( p_obj );
213 vlc_object_release( p_obj );
214 sout_DeleteInstance( (sout_instance_t*)p_obj );
216 p_playlist->b_cant_sleep = VLC_FALSE;
217 vlc_mutex_unlock( &p_playlist->gc_lock );
220 /** Main loop for the playlist */
221 void playlist_MainLoop( playlist_t *p_playlist )
223 playlist_item_t *p_item = NULL;
224 vlc_bool_t b_playexit = var_GetBool( p_playlist, "play-and-exit" );
227 if( p_playlist->b_reset_currently_playing &&
228 mdate() - p_playlist->last_rebuild_date > 30000 ) // 30 ms
230 ResetCurrentlyPlaying( p_playlist, var_GetBool( p_playlist, "random"),
231 p_playlist->status.p_item );
232 p_playlist->last_rebuild_date = mdate();
236 /* If there is an input, check that it doesn't need to die. */
237 if( p_playlist->p_input )
239 if( p_playlist->request.b_request && !p_playlist->p_input->b_die )
241 PL_DEBUG( "incoming request - stopping current input" );
242 input_StopThread( p_playlist->p_input );
245 /* This input is dead. Remove it ! */
246 if( p_playlist->p_input->b_dead )
249 input_thread_t *p_input;
250 PL_DEBUG( "dead input" );
252 p_input = p_playlist->p_input;
253 p_playlist->p_input = NULL;
255 /* Release the playlist lock, because we may get stuck
256 * in input_DestroyThread() for some time. */
260 input_DestroyThread( p_input );
264 p_playlist->gc_date = mdate();
265 p_playlist->b_cant_sleep = VLC_TRUE;
267 if( p_playlist->status.p_item->i_flags
268 & PLAYLIST_REMOVE_FLAG )
270 PL_DEBUG( "%s was marked for deletion, deleting",
271 PLI_NAME( p_playlist->status.p_item ) );
272 playlist_ItemDelete( p_playlist->status.p_item );
273 if( p_playlist->request.p_item == p_playlist->status.p_item )
274 p_playlist->request.p_item = NULL;
275 p_playlist->status.p_item = NULL;
278 i_activity= var_GetInteger( p_playlist, "activity") ;
279 var_SetInteger( p_playlist, "activity", i_activity -
280 DEFAULT_INPUT_ACTIVITY );
283 /* This input is dying, let it do */
284 else if( p_playlist->p_input->b_die )
286 PL_DEBUG( "dying input" );
288 msleep( 25000 ); // 25 ms
292 /* This input has finished, ask it to die ! */
293 else if( p_playlist->p_input->b_error
294 || p_playlist->p_input->b_eof )
296 PL_DEBUG( "finished input" );
297 input_StopThread( p_playlist->p_input );
298 /* No need to wait here, we'll wait in the p_input->b_die case */
301 else if( p_playlist->p_input->i_state != INIT_S )
304 ObjectGarbageCollector( p_playlist );
310 /* No input. Several cases
311 * - No request, running status -> start new item
312 * - No request, stopped status -> collect garbage
313 * - Request, running requested -> start new item
314 * - Request, stopped requested -> collect garbage
316 if( (!p_playlist->request.b_request &&
317 p_playlist->status.i_status != PLAYLIST_STOPPED) ||
318 ( p_playlist->request.b_request &&
319 p_playlist->request.i_status != PLAYLIST_STOPPED ) )
321 msg_Dbg( p_playlist, "starting new item" );
322 p_item = playlist_NextItem( p_playlist );
326 msg_Dbg( p_playlist, "nothing to play" );
327 if( b_playexit == VLC_TRUE )
329 msg_Info( p_playlist, "end of playlist, exiting" );
330 p_playlist->p_libvlc->b_die = VLC_TRUE;
332 p_playlist->status.i_status = PLAYLIST_STOPPED;
336 playlist_PlayItem( p_playlist, p_item );
340 p_playlist->status.i_status = PLAYLIST_STOPPED;
341 if( p_playlist->status.p_item &&
342 p_playlist->status.p_item->i_flags & PLAYLIST_REMOVE_FLAG )
344 PL_DEBUG( "deleting item marked for deletion" );
345 playlist_ItemDelete( p_playlist->status.p_item );
346 p_playlist->status.p_item = NULL;
349 /* Collect garbage */
351 ObjectGarbageCollector( p_playlist );
358 /** Playlist dying last loop */
359 void playlist_LastLoop( playlist_t *p_playlist )
363 /* If there is an input, kill it */
368 if( p_playlist->p_input == NULL )
374 if( p_playlist->p_input->b_dead )
376 input_thread_t *p_input;
378 /* Unlink current input */
379 p_input = p_playlist->p_input;
380 p_playlist->p_input = NULL;
384 input_DestroyThread( p_input );
387 else if( p_playlist->p_input->b_die )
389 /* This input is dying, leave it alone */
392 else if( p_playlist->p_input->b_error || p_playlist->p_input->b_eof )
394 input_StopThread( p_playlist->p_input );
400 p_playlist->p_input->b_eof = 1;
405 msleep( INTF_IDLE_SLEEP );
408 /* close all remaining sout */
409 while( ( p_obj = vlc_object_find( p_playlist,
410 VLC_OBJECT_SOUT, FIND_CHILD ) ) )
412 vlc_object_release( p_obj );
413 sout_DeleteInstance( (sout_instance_t*)p_obj );
416 /* close all remaining vout */
417 while( ( p_obj = vlc_object_find( p_playlist,
418 VLC_OBJECT_VOUT, FIND_CHILD ) ) )
420 vlc_object_detach( p_obj );
421 vlc_object_release( p_obj );
422 vout_Destroy( (vout_thread_t *)p_obj );
425 while( p_playlist->i_sds )
427 playlist_ServicesDiscoveryRemove( p_playlist,
428 p_playlist->pp_sds[0]->psz_module );
431 playlist_MLDump( p_playlist );
434 /* Go through all items, and simply free everything without caring
435 * about the tree structure. Do not decref, it will be done by doing
436 * the same thing on the input items array */
437 FOREACH_ARRAY( playlist_item_t *p_del, p_playlist->all_items )
438 free( p_del->pp_children );
441 ARRAY_RESET( p_playlist->all_items );
443 FOREACH_ARRAY( input_item_t *p_del, p_playlist->input_items )
444 input_ItemClean( p_del );
447 ARRAY_RESET( p_playlist->input_items );
449 ARRAY_RESET( p_playlist->items );
450 ARRAY_RESET( p_playlist->current );
455 /** Main loop for preparser queue */
456 void playlist_PreparseLoop( playlist_preparse_t *p_obj )
458 playlist_t *p_playlist = (playlist_t *)p_obj->p_parent;
459 input_item_t *p_current;
463 while( !p_playlist->b_die )
465 vlc_mutex_lock( &p_obj->object_lock );
466 while( p_obj->i_waiting == 0 )
468 vlc_cond_wait( &p_obj->object_wait, &p_obj->object_lock );
469 if( p_playlist->b_die )
471 vlc_mutex_unlock( &p_obj->object_lock );
476 p_current = p_obj->pp_waiting[0];
477 REMOVE_ELEM( p_obj->pp_waiting, p_obj->i_waiting, 0 );
478 vlc_mutex_unlock( &p_obj->object_lock );
483 vlc_bool_t b_preparsed = VLC_FALSE;
484 if( strncmp( p_current->psz_uri, "http:", 5 ) &&
485 strncmp( p_current->psz_uri, "rtsp:", 5 ) &&
486 strncmp( p_current->psz_uri, "udp:", 4 ) &&
487 strncmp( p_current->psz_uri, "mms:", 4 ) &&
488 strncmp( p_current->psz_uri, "cdda:", 4 ) &&
489 strncmp( p_current->psz_uri, "dvd:", 4 ) &&
490 strncmp( p_current->psz_uri, "v4l:", 4 ) &&
491 strncmp( p_current->psz_uri, "dshow:", 6 ) )
493 b_preparsed = VLC_TRUE;
494 stats_TimerStart( p_playlist, "Preparse run",
495 STATS_TIMER_PREPARSE );
497 input_Preparse( p_playlist, p_current );
499 stats_TimerStop( p_playlist, STATS_TIMER_PREPARSE );
504 p_current->p_meta->i_status |= ITEM_PREPARSED;
505 var_SetInteger( p_playlist, "item-change", p_current->i_id );
509 /* If we haven't retrieved enough meta, add to secondary queue
510 * which will run the "meta fetchers".
511 * This only checks for meta, not for art
512 * \todo don't do this for things we won't get meta for, like vids
514 if( p_current->p_meta &&
515 !input_MetaSatisfied( p_playlist, p_current, &i_m, &i_o ) )
518 PL_DEBUG("need to fetch meta for %s", p_current->psz_name );
519 p.p_item = p_current;
520 p.b_fetch_art = VLC_FALSE;
521 vlc_mutex_lock( &p_playlist->p_fetcher->object_lock );
522 INSERT_ELEM( p_playlist->p_fetcher->p_waiting,
523 p_playlist->p_fetcher->i_waiting,
524 p_playlist->p_fetcher->i_waiting, p);
525 vlc_mutex_unlock( &p_playlist->p_fetcher->object_lock );
526 vlc_cond_signal( &p_playlist->p_fetcher->object_wait );
528 /* We already have all needed meta, but we need art right now */
529 else if( p_current->p_meta &&
530 p_playlist->p_fetcher->i_art_policy == ALBUM_ART_ALL &&
531 EMPTY_STR( p_current->p_meta->psz_arturl ) )
534 PL_DEBUG("meta ok for %s, need to fetch art",
535 p_current->psz_name );
536 p.p_item = p_current;
537 p.b_fetch_art = VLC_TRUE;
538 vlc_mutex_lock( &p_playlist->p_fetcher->object_lock );
539 INSERT_ELEM( p_playlist->p_fetcher->p_waiting,
540 p_playlist->p_fetcher->i_waiting,
541 p_playlist->p_fetcher->i_waiting, p);
542 vlc_mutex_unlock( &p_playlist->p_fetcher->object_lock );
543 vlc_cond_signal( &p_playlist->p_fetcher->object_wait );
547 PL_DEBUG( "no fetch required for %s (art currently %s)",
549 p_current->p_meta ? p_current->p_meta->psz_arturl:
551 vlc_gc_decref( p_current );
558 vlc_mutex_lock( &p_obj->object_lock );
559 i_activity = var_GetInteger( p_playlist, "activity" );
560 if( i_activity < 0 ) i_activity = 0;
561 vlc_mutex_unlock( &p_obj->object_lock );
562 /* Sleep at least 1ms */
563 msleep( (i_activity+1) * 1000 );
567 /** Main loop for secondary preparser queue */
568 void playlist_FetcherLoop( playlist_fetcher_t *p_obj )
570 playlist_t *p_playlist = (playlist_t *)p_obj->p_parent;
571 vlc_bool_t b_fetch_art;
572 input_item_t *p_item;
575 while( !p_playlist->b_die )
577 vlc_mutex_lock( &p_obj->object_lock );
578 while( p_obj->i_waiting == 0 )
580 vlc_cond_wait( &p_obj->object_wait, &p_obj->object_lock );
581 if( p_playlist->b_die )
583 vlc_mutex_unlock( &p_obj->object_lock );
588 b_fetch_art = p_obj->p_waiting->b_fetch_art;
589 p_item = p_obj->p_waiting->p_item;
590 REMOVE_ELEM( p_obj->p_waiting, p_obj->i_waiting, 0 );
591 vlc_mutex_unlock( &p_obj->object_lock );
594 assert( p_item->p_meta );
597 input_MetaFetch( p_playlist, p_item );
598 p_item->p_meta->i_status |= ITEM_META_FETCHED;
599 var_SetInteger( p_playlist, "item-change", p_item->i_id );
600 /* Fetch right now */
601 if( p_playlist->p_fetcher->i_art_policy == ALBUM_ART_ALL )
603 vlc_mutex_lock( &p_obj->object_lock );
606 p.b_fetch_art = VLC_TRUE;
607 INSERT_ELEM( p_playlist->p_fetcher->p_waiting,
608 p_playlist->p_fetcher->i_waiting,
610 PL_DEBUG("meta fetched for %s, get art", p_item->psz_name);
611 vlc_mutex_unlock( &p_obj->object_lock );
615 vlc_gc_decref( p_item );
619 int i_ret = input_ArtFind( p_playlist, p_item );
622 PL_DEBUG("downloading art for %s", p_item->psz_name );
623 if( input_DownloadAndCacheArt( p_playlist, p_item ) )
624 p_item->p_meta->i_status |= ITEM_ART_NOTFOUND;
626 p_item->p_meta->i_status |= ITEM_ART_FETCHED;
627 var_SetInteger( p_playlist, "item-change",
631 else if( i_ret == 0 ) /* Was in cache */
633 PL_DEBUG("found art for %s in cache", p_item->psz_name );
634 p_item->p_meta->i_status |= ITEM_ART_FETCHED;
635 var_SetInteger( p_playlist, "item-change", p_item->i_id );
639 PL_DEBUG("art not found for %s", p_item->psz_name );
640 p_item->p_meta->i_status |= ITEM_ART_NOTFOUND;
642 vlc_gc_decref( p_item );
645 vlc_mutex_lock( &p_obj->object_lock );
646 i_activity = var_GetInteger( p_playlist, "activity" );
647 if( i_activity < 0 ) i_activity = 0;
648 vlc_mutex_unlock( &p_obj->object_lock );
649 /* Sleep at least 1ms */
650 msleep( (i_activity+1) * 1000 );
654 static void VariablesInit( playlist_t *p_playlist )
657 /* These variables control updates */
658 var_Create( p_playlist, "intf-change", VLC_VAR_BOOL );
659 val.b_bool = VLC_TRUE;
660 var_Set( p_playlist, "intf-change", val );
662 var_Create( p_playlist, "item-change", VLC_VAR_INTEGER );
664 var_Set( p_playlist, "item-change", val );
666 var_Create( p_playlist, "item-deleted", VLC_VAR_INTEGER );
668 var_Set( p_playlist, "item-deleted", val );
670 var_Create( p_playlist, "item-append", VLC_VAR_ADDRESS );
672 var_Create( p_playlist, "playlist-current", VLC_VAR_INTEGER );
674 var_Set( p_playlist, "playlist-current", val );
676 var_Create( p_playlist, "intf-popupmenu", VLC_VAR_BOOL );
678 var_Create( p_playlist, "intf-show", VLC_VAR_BOOL );
679 val.b_bool = VLC_TRUE;
680 var_Set( p_playlist, "intf-show", val );
682 var_Create( p_playlist, "activity", VLC_VAR_INTEGER );
683 var_SetInteger( p_playlist, "activity", 0 );
685 /* Variables to control playback */
686 var_CreateGetBool( p_playlist, "play-and-stop" );
687 var_CreateGetBool( p_playlist, "play-and-exit" );
688 var_CreateGetBool( p_playlist, "random" );
689 var_CreateGetBool( p_playlist, "repeat" );
690 var_CreateGetBool( p_playlist, "loop" );
692 var_AddCallback( p_playlist, "random", RandomCallback, NULL );