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/input.h>
29 #include "vlc_playlist.h"
30 #include "vlc_interaction.h"
31 #include "playlist_internal.h"
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);
98 p_playlist->p_root_onelevel = playlist_NodeCreate( p_playlist, NULL, NULL);
100 /* Create playlist and media library */
101 p_playlist->p_local_category = playlist_NodeCreate( p_playlist,
102 _( "Playlist" ),p_playlist->p_root_category );
103 p_playlist->p_local_onelevel = playlist_NodeCreate( p_playlist,
104 _( "Playlist" ), p_playlist->p_root_onelevel );
105 p_playlist->p_local_category->i_flags |= PLAYLIST_RO_FLAG;
106 p_playlist->p_local_onelevel->i_flags |= PLAYLIST_RO_FLAG;
108 /* Link the nodes together. Todo: actually create them from the same input*/
109 p_playlist->p_local_onelevel->p_input->i_id =
110 p_playlist->p_local_category->p_input->i_id;
112 if( config_GetInt( p_playlist, "media-library") )
114 p_playlist->p_ml_category = playlist_NodeCreate( p_playlist,
115 _( "Media Library" ), p_playlist->p_root_category );
116 p_playlist->p_ml_onelevel = playlist_NodeCreate( p_playlist,
117 _( "Media Library" ), p_playlist->p_root_onelevel );
118 p_playlist->p_ml_category->i_flags |= PLAYLIST_RO_FLAG;
119 p_playlist->p_ml_onelevel->i_flags |= PLAYLIST_RO_FLAG;
120 p_playlist->p_ml_onelevel->p_input->i_id =
121 p_playlist->p_ml_category->p_input->i_id;
126 p_playlist->p_ml_category = p_playlist->p_ml_onelevel = NULL;
130 p_playlist->status.p_item = NULL;
131 p_playlist->status.p_node = p_playlist->p_local_onelevel;
132 p_playlist->request.b_request = VLC_FALSE;
133 p_playlist->status.i_status = PLAYLIST_STOPPED;
135 p_playlist->i_sort = SORT_ID;
136 p_playlist->i_order = ORDER_NORMAL;
138 vlc_object_attach( p_playlist, p_parent );
139 b_save = p_playlist->b_auto_preparse;
140 p_playlist->b_auto_preparse = VLC_FALSE;
141 playlist_MLLoad( p_playlist );
142 p_playlist->b_auto_preparse = VLC_TRUE;
146 void playlist_Destroy( playlist_t *p_playlist )
148 while( p_playlist->i_sds )
150 playlist_ServicesDiscoveryRemove( p_playlist,
151 p_playlist->pp_sds[0]->psz_module );
154 playlist_MLDump( p_playlist );
156 vlc_thread_join( p_playlist->p_preparse );
157 vlc_thread_join( p_playlist->p_fetcher );
158 vlc_thread_join( p_playlist );
160 vlc_object_detach( p_playlist->p_preparse );
161 vlc_object_detach( p_playlist->p_fetcher );
163 var_Destroy( p_playlist, "intf-change" );
164 var_Destroy( p_playlist, "item-change" );
165 var_Destroy( p_playlist, "playlist-current" );
166 var_Destroy( p_playlist, "intf-popmenu" );
167 var_Destroy( p_playlist, "intf-show" );
168 var_Destroy( p_playlist, "play-and-stop" );
169 var_Destroy( p_playlist, "play-and-exit" );
170 var_Destroy( p_playlist, "random" );
171 var_Destroy( p_playlist, "repeat" );
172 var_Destroy( p_playlist, "loop" );
173 var_Destroy( p_playlist, "activity" );
176 /* Go through all items, and simply free everything without caring
177 * about the tree structure. Do not decref, it will be done by doing
178 * the same thing on the input items array */
179 FOREACH_ARRAY( playlist_item_t *p_del, p_playlist->all_items )
180 free( p_del->pp_children );
183 ARRAY_RESET( p_playlist->all_items );
185 FOREACH_ARRAY( input_item_t *p_del, p_playlist->input_items )
186 input_ItemClean( p_del );
189 ARRAY_RESET( p_playlist->input_items );
191 ARRAY_RESET( p_playlist->items );
192 ARRAY_RESET( p_playlist->current );
196 if( p_playlist->p_stats )
197 free( p_playlist->p_stats );
199 vlc_mutex_destroy( &p_playlist->gc_lock );
200 vlc_object_destroy( p_playlist->p_preparse );
201 vlc_object_destroy( p_playlist->p_fetcher );
202 vlc_object_detach( p_playlist );
203 vlc_object_destroy( p_playlist );
207 /* Destroy remaining objects */
208 static void ObjectGarbageCollector( playlist_t *p_playlist )
212 if( mdate() - p_playlist->gc_date < 1000000 )
214 p_playlist->b_cant_sleep = VLC_TRUE;
217 else if( p_playlist->gc_date == 0 )
220 vlc_mutex_lock( &p_playlist->gc_lock );
221 while( ( p_obj = vlc_object_find( p_playlist, VLC_OBJECT_VOUT,
224 if( p_obj->p_parent != (vlc_object_t*)p_playlist )
226 vlc_object_release( p_obj );
229 msg_Dbg( p_playlist, "garbage collector destroying 1 vout" );
230 vlc_object_detach( p_obj );
231 vlc_object_release( p_obj );
232 vout_Destroy( (vout_thread_t *)p_obj );
234 while( ( p_obj = vlc_object_find( p_playlist, VLC_OBJECT_SOUT,
237 if( p_obj->p_parent != (vlc_object_t*)p_playlist )
239 vlc_object_release( p_obj );
242 vlc_object_release( p_obj );
243 sout_DeleteInstance( (sout_instance_t*)p_obj );
245 p_playlist->b_cant_sleep = VLC_FALSE;
246 vlc_mutex_unlock( &p_playlist->gc_lock );
249 /** Main loop for the playlist */
250 void playlist_MainLoop( playlist_t *p_playlist )
252 playlist_item_t *p_item = NULL;
253 vlc_bool_t b_playexit = var_GetBool( p_playlist, "play-and-exit" );
256 if( p_playlist->b_reset_currently_playing &&
257 mdate() - p_playlist->last_rebuild_date > 30000 ) // 30 ms
259 ResetCurrentlyPlaying( p_playlist, var_GetBool( p_playlist, "random"),
260 p_playlist->status.p_item );
261 p_playlist->last_rebuild_date = mdate();
265 /* If there is an input, check that it doesn't need to die. */
266 if( p_playlist->p_input )
268 if( p_playlist->request.b_request && !p_playlist->p_input->b_die )
270 PL_DEBUG( "incoming request - stopping current input" );
271 input_StopThread( p_playlist->p_input );
274 /* This input is dead. Remove it ! */
275 if( p_playlist->p_input->b_dead )
278 input_thread_t *p_input;
279 PL_DEBUG( "dead input" );
281 p_input = p_playlist->p_input;
282 p_playlist->p_input = NULL;
284 /* Release the playlist lock, because we may get stuck
285 * in input_DestroyThread() for some time. */
289 input_DestroyThread( p_input );
291 /* Unlink current input
292 * (_after_ input_DestroyThread for vout garbage collector) */
293 vlc_object_detach( p_input );
296 vlc_object_destroy( p_input );
300 p_playlist->gc_date = mdate();
301 p_playlist->b_cant_sleep = VLC_TRUE;
303 if( p_playlist->status.p_item->i_flags
304 & PLAYLIST_REMOVE_FLAG )
306 PL_DEBUG( "%s was marked for deletion, deleting",
307 PLI_NAME( p_playlist->status.p_item ) );
308 playlist_ItemDelete( p_playlist->status.p_item );
309 if( p_playlist->request.p_item == p_playlist->status.p_item )
310 p_playlist->request.p_item = NULL;
311 p_playlist->status.p_item = NULL;
314 i_activity= var_GetInteger( p_playlist, "activity") ;
315 var_SetInteger( p_playlist, "activity", i_activity -
316 DEFAULT_INPUT_ACTIVITY );
319 /* This input is dying, let it do */
320 else if( p_playlist->p_input->b_die )
322 PL_DEBUG( "dying input" );
323 msleep( 25000 ); // 25 ms
326 /* This input has finished, ask it to die ! */
327 else if( p_playlist->p_input->b_error
328 || p_playlist->p_input->b_eof )
330 PL_DEBUG( "finished input" );
331 input_StopThread( p_playlist->p_input );
332 /* No need to wait here, we'll wait in the p_input->b_die case */
335 else if( p_playlist->p_input->i_state != INIT_S )
338 ObjectGarbageCollector( p_playlist );
344 /* No input. Several cases
345 * - No request, running status -> start new item
346 * - No request, stopped status -> collect garbage
347 * - Request, running requested -> start new item
348 * - Request, stopped requested -> collect garbage
350 if( (!p_playlist->request.b_request &&
351 p_playlist->status.i_status != PLAYLIST_STOPPED) ||
352 ( p_playlist->request.b_request &&
353 p_playlist->request.i_status != PLAYLIST_STOPPED ) )
355 msg_Dbg( p_playlist, "starting new item" );
356 p_item = playlist_NextItem( p_playlist );
360 msg_Dbg( p_playlist, "nothing to play" );
361 if( b_playexit == VLC_TRUE )
363 msg_Info( p_playlist, "end of playlist, exiting" );
364 p_playlist->p_libvlc->b_die = VLC_TRUE;
366 p_playlist->status.i_status = PLAYLIST_STOPPED;
370 playlist_PlayItem( p_playlist, p_item );
374 p_playlist->status.i_status = PLAYLIST_STOPPED;
375 if( p_playlist->status.p_item &&
376 p_playlist->status.p_item->i_flags & PLAYLIST_REMOVE_FLAG )
378 PL_DEBUG( "deleting item marked for deletion" );
379 playlist_ItemDelete( p_playlist->status.p_item );
380 p_playlist->status.p_item = NULL;
383 /* Collect garbage */
385 ObjectGarbageCollector( p_playlist );
392 /** Playlist dying last loop */
393 void playlist_LastLoop( playlist_t *p_playlist )
397 /* If there is an input, kill it */
402 if( p_playlist->p_input == NULL )
408 if( p_playlist->p_input->b_dead )
410 input_thread_t *p_input;
412 /* Unlink current input */
413 p_input = p_playlist->p_input;
414 p_playlist->p_input = NULL;
418 input_DestroyThread( p_input );
419 /* Unlink current input (_after_ input_DestroyThread for vout
420 * garbage collector)*/
421 vlc_object_detach( p_input );
424 vlc_object_destroy( p_input );
427 else if( p_playlist->p_input->b_die )
429 /* This input is dying, leave it alone */
432 else if( p_playlist->p_input->b_error || p_playlist->p_input->b_eof )
434 input_StopThread( p_playlist->p_input );
440 p_playlist->p_input->b_eof = 1;
445 msleep( INTF_IDLE_SLEEP );
448 /* close all remaining sout */
449 while( ( p_obj = vlc_object_find( p_playlist,
450 VLC_OBJECT_SOUT, FIND_CHILD ) ) )
452 vlc_object_release( p_obj );
453 sout_DeleteInstance( (sout_instance_t*)p_obj );
456 /* close all remaining vout */
457 while( ( p_obj = vlc_object_find( p_playlist,
458 VLC_OBJECT_VOUT, FIND_CHILD ) ) )
460 vlc_object_detach( p_obj );
461 vlc_object_release( p_obj );
462 vout_Destroy( (vout_thread_t *)p_obj );
466 /** Main loop for preparser queue */
467 void playlist_PreparseLoop( playlist_preparse_t *p_obj )
469 playlist_t *p_playlist = (playlist_t *)p_obj->p_parent;
470 input_item_t *p_current;
474 while( !p_playlist->b_die )
476 vlc_mutex_lock( &p_obj->object_lock );
477 while( p_obj->i_waiting == 0 )
479 vlc_cond_wait( &p_obj->object_wait, &p_obj->object_lock );
480 if( p_playlist->b_die )
482 vlc_mutex_unlock( &p_obj->object_lock );
487 p_current = p_obj->pp_waiting[0];
488 REMOVE_ELEM( p_obj->pp_waiting, p_obj->i_waiting, 0 );
489 vlc_mutex_unlock( &p_obj->object_lock );
494 vlc_bool_t b_preparsed = VLC_FALSE;
495 if( strncmp( p_current->psz_uri, "http:", 5 ) &&
496 strncmp( p_current->psz_uri, "rtsp:", 5 ) &&
497 strncmp( p_current->psz_uri, "udp:", 4 ) &&
498 strncmp( p_current->psz_uri, "mms:", 4 ) &&
499 strncmp( p_current->psz_uri, "cdda:", 4 ) &&
500 strncmp( p_current->psz_uri, "dvd:", 4 ) &&
501 strncmp( p_current->psz_uri, "v4l:", 4 ) &&
502 strncmp( p_current->psz_uri, "dshow:", 6 ) )
504 b_preparsed = VLC_TRUE;
505 stats_TimerStart( p_playlist, "Preparse run",
506 STATS_TIMER_PREPARSE );
508 input_Preparse( p_playlist, p_current );
510 stats_TimerStop( p_playlist, STATS_TIMER_PREPARSE );
515 p_current->p_meta->i_status |= ITEM_PREPARSED;
516 var_SetInteger( p_playlist, "item-change", p_current->i_id );
520 /* If we haven't retrieved enough meta, add to secondary queue
521 * which will run the "meta fetchers".
522 * This only checks for meta, not for art
523 * \todo don't do this for things we won't get meta for, like vids
525 if( !input_MetaSatisfied( p_playlist, p_current, &i_m, &i_o ) )
528 PL_DEBUG("need to fetch meta for %s", p_current->psz_name );
529 p.p_item = p_current;
530 p.b_fetch_art = VLC_FALSE;
531 vlc_mutex_lock( &p_playlist->p_fetcher->object_lock );
532 INSERT_ELEM( p_playlist->p_fetcher->p_waiting,
533 p_playlist->p_fetcher->i_waiting,
534 p_playlist->p_fetcher->i_waiting,
536 vlc_mutex_unlock( &p_playlist->p_fetcher->object_lock );
537 vlc_cond_signal( &p_playlist->p_fetcher->object_wait );
539 /* We already have all needed meta, but we need art right now */
540 else if( p_playlist->p_fetcher->i_art_policy == ALBUM_ART_ALL &&
541 EMPTY_STR( p_current->p_meta->psz_arturl ) )
544 PL_DEBUG("meta ok for %s, need to fetch art",
545 p_current->psz_name );
546 p.p_item = p_current;
547 p.b_fetch_art = VLC_TRUE;
548 vlc_mutex_lock( &p_playlist->p_fetcher->object_lock );
549 INSERT_ELEM( p_playlist->p_fetcher->p_waiting,
550 p_playlist->p_fetcher->i_waiting,
551 p_playlist->p_fetcher->i_waiting,
553 vlc_mutex_unlock( &p_playlist->p_fetcher->object_lock );
554 vlc_cond_signal( &p_playlist->p_fetcher->object_wait );
558 PL_DEBUG( "no fetch required for %s (art currently %s)",
559 p_current->psz_name, p_current->p_meta->psz_arturl );
560 vlc_gc_decref( p_current );
567 vlc_mutex_lock( &p_obj->object_lock );
568 i_activity = var_GetInteger( p_playlist, "activity" );
569 if( i_activity < 0 ) i_activity = 0;
570 vlc_mutex_unlock( &p_obj->object_lock );
571 /* Sleep at least 1ms */
572 msleep( (i_activity+1) * 1000 );
576 /** Main loop for secondary preparser queue */
577 void playlist_FetcherLoop( playlist_fetcher_t *p_obj )
579 playlist_t *p_playlist = (playlist_t *)p_obj->p_parent;
580 vlc_bool_t b_fetch_art;
581 input_item_t *p_item;
584 while( !p_playlist->b_die )
586 vlc_mutex_lock( &p_obj->object_lock );
587 while( p_obj->i_waiting == 0 )
589 vlc_cond_wait( &p_obj->object_wait, &p_obj->object_lock );
590 if( p_playlist->b_die )
592 vlc_mutex_unlock( &p_obj->object_lock );
597 b_fetch_art = p_obj->p_waiting->b_fetch_art;
598 p_item = p_obj->p_waiting->p_item;
599 REMOVE_ELEM( p_obj->p_waiting, p_obj->i_waiting, 0 );
600 vlc_mutex_unlock( &p_obj->object_lock );
605 input_MetaFetch( p_playlist, p_item );
606 p_item->p_meta->i_status |= ITEM_META_FETCHED;
607 var_SetInteger( p_playlist, "item-change", p_item->i_id );
608 /* Fetch right now */
609 if( p_playlist->p_fetcher->i_art_policy == ALBUM_ART_ALL )
611 vlc_mutex_lock( &p_obj->object_lock );
614 p.b_fetch_art = VLC_TRUE;
615 INSERT_ELEM( p_playlist->p_fetcher->p_waiting,
616 p_playlist->p_fetcher->i_waiting,
618 PL_DEBUG("meta fetched for %s, get art", p_item->psz_name);
619 vlc_mutex_unlock( &p_obj->object_lock );
623 vlc_gc_decref( p_item );
627 int i_ret = input_ArtFind( p_playlist, p_item );
630 PL_DEBUG("downloading art for %s", p_item->psz_name );
631 if( !input_DownloadAndCacheArt( p_playlist, p_item ) )
632 p_item->p_meta->i_status |= ITEM_ART_NOTFOUND;
634 p_item->p_meta->i_status |= ITEM_ART_FETCHED;
636 else if( i_ret == 0 ) /* Was in cache */
638 PL_DEBUG("found art for %s in cache", p_item->psz_name );
639 p_item->p_meta->i_status |= ITEM_ART_FETCHED;
643 PL_DEBUG("art not found for %s", p_item->psz_name );
644 p_item->p_meta->i_status |= ITEM_ART_NOTFOUND;
646 vlc_gc_decref( p_item );
649 vlc_mutex_lock( &p_obj->object_lock );
650 i_activity = var_GetInteger( p_playlist, "activity" );
651 if( i_activity < 0 ) i_activity = 0;
652 vlc_mutex_unlock( &p_obj->object_lock );
653 /* Sleep at least 1ms */
654 msleep( (i_activity+1) * 1000 );
658 static void VariablesInit( playlist_t *p_playlist )
661 /* These variables control updates */
662 var_Create( p_playlist, "intf-change", VLC_VAR_BOOL );
663 val.b_bool = VLC_TRUE;
664 var_Set( p_playlist, "intf-change", val );
666 var_Create( p_playlist, "item-change", VLC_VAR_INTEGER );
668 var_Set( p_playlist, "item-change", val );
670 var_Create( p_playlist, "item-deleted", VLC_VAR_INTEGER );
672 var_Set( p_playlist, "item-deleted", val );
674 var_Create( p_playlist, "item-append", VLC_VAR_ADDRESS );
676 var_Create( p_playlist, "playlist-current", VLC_VAR_INTEGER );
678 var_Set( p_playlist, "playlist-current", val );
680 var_Create( p_playlist, "intf-popupmenu", VLC_VAR_BOOL );
682 var_Create( p_playlist, "intf-show", VLC_VAR_BOOL );
683 val.b_bool = VLC_TRUE;
684 var_Set( p_playlist, "intf-show", val );
686 var_Create( p_playlist, "activity", VLC_VAR_INTEGER );
687 var_SetInteger( p_playlist, "activity", 0 );
689 /* Variables to control playback */
690 var_CreateGetBool( p_playlist, "play-and-stop" );
691 var_CreateGetBool( p_playlist, "play-and-exit" );
692 var_CreateGetBool( p_playlist, "random" );
693 var_CreateGetBool( p_playlist, "repeat" );
694 var_CreateGetBool( p_playlist, "loop" );
696 var_AddCallback( p_playlist, "random", RandomCallback, NULL );