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 *****************************************************************************/
27 #include <vlc/input.h>
28 #include "vlc_playlist.h"
29 #include "vlc_interaction.h"
30 #include "playlist_internal.h"
32 /*****************************************************************************
34 *****************************************************************************/
35 static void VariablesInit( playlist_t *p_playlist );
37 static int RandomCallback( vlc_object_t *p_this, char const *psz_cmd,
38 vlc_value_t oldval, vlc_value_t newval, void *a )
40 ((playlist_t*)p_this)->b_reset_currently_playing = VLC_TRUE;
47 * Create a playlist structure.
48 * \param p_parent the vlc object that is to be the parent of this playlist
49 * \return a pointer to the created playlist, or NULL on error
51 playlist_t * playlist_Create( vlc_object_t *p_parent )
53 playlist_t *p_playlist;
56 /* Allocate structure */
57 p_playlist = vlc_object_create( p_parent, VLC_OBJECT_PLAYLIST );
60 msg_Err( p_parent, "out of memory" );
63 p_parent->p_libvlc->p_playlist = p_playlist;
65 VariablesInit( p_playlist );
67 /* Initialise data structures */
68 vlc_mutex_init( p_playlist, &p_playlist->gc_lock );
69 p_playlist->i_last_playlist_id = 0;
70 p_playlist->i_last_input_id = 0;
71 p_playlist->p_input = NULL;
73 p_playlist->i_vout_destroyed_date = 0;
74 p_playlist->i_sout_destroyed_date = 0;
76 ARRAY_INIT( p_playlist->items );
77 ARRAY_INIT( p_playlist->all_items );
78 ARRAY_INIT( p_playlist->input_items );
79 ARRAY_INIT( p_playlist->current );
81 p_playlist->i_current_index = 0;
82 p_playlist->b_reset_currently_playing = VLC_TRUE;
84 i_tree = var_CreateGetBool( p_playlist, "playlist-tree" );
85 p_playlist->b_always_tree = (i_tree == 1);
86 p_playlist->b_never_tree = (i_tree == 2);
88 p_playlist->b_doing_ml = VLC_FALSE;
90 p_playlist->p_root_category = playlist_NodeCreate( p_playlist, NULL, NULL);
91 p_playlist->p_root_onelevel = playlist_NodeCreate( p_playlist, NULL, NULL);
93 /* Create playlist and media library */
94 p_playlist->p_local_category = playlist_NodeCreate( p_playlist,
95 _( "Playlist" ),p_playlist->p_root_category );
96 p_playlist->p_local_onelevel = playlist_NodeCreate( p_playlist,
97 _( "Playlist" ), p_playlist->p_root_onelevel );
98 p_playlist->p_local_category->i_flags |= PLAYLIST_RO_FLAG;
99 p_playlist->p_local_onelevel->i_flags |= PLAYLIST_RO_FLAG;
101 /* Link the nodes together. Todo: actually create them from the same input*/
102 p_playlist->p_local_onelevel->p_input->i_id =
103 p_playlist->p_local_category->p_input->i_id;
105 if( config_GetInt( p_playlist, "media-library") )
107 p_playlist->p_ml_category = playlist_NodeCreate( p_playlist,
108 _( "Media Library" ), p_playlist->p_root_category );
109 p_playlist->p_ml_onelevel = playlist_NodeCreate( p_playlist,
110 _( "Media Library" ), p_playlist->p_root_onelevel );
111 p_playlist->p_ml_category->i_flags |= PLAYLIST_RO_FLAG;
112 p_playlist->p_ml_onelevel->i_flags |= PLAYLIST_RO_FLAG;
113 p_playlist->p_ml_onelevel->p_input->i_id =
114 p_playlist->p_ml_category->p_input->i_id;
119 p_playlist->p_ml_category = p_playlist->p_ml_onelevel = NULL;
123 p_playlist->status.p_item = NULL;
124 p_playlist->status.p_node = p_playlist->p_local_onelevel;
125 p_playlist->request.b_request = VLC_FALSE;
126 p_playlist->status.i_status = PLAYLIST_STOPPED;
128 p_playlist->i_sort = SORT_ID;
129 p_playlist->i_order = ORDER_NORMAL;
131 vlc_object_attach( p_playlist, p_parent );
133 playlist_MLLoad( p_playlist );
137 void playlist_Destroy( playlist_t *p_playlist )
139 while( p_playlist->i_sds )
141 playlist_ServicesDiscoveryRemove( p_playlist,
142 p_playlist->pp_sds[0]->psz_module );
145 playlist_MLDump( p_playlist );
147 vlc_thread_join( p_playlist->p_preparse );
148 vlc_thread_join( p_playlist->p_secondary_preparse );
149 vlc_thread_join( p_playlist );
151 vlc_object_detach( p_playlist->p_preparse );
152 vlc_object_detach( p_playlist->p_secondary_preparse );
154 var_Destroy( p_playlist, "intf-change" );
155 var_Destroy( p_playlist, "item-change" );
156 var_Destroy( p_playlist, "playlist-current" );
157 var_Destroy( p_playlist, "intf-popmenu" );
158 var_Destroy( p_playlist, "intf-show" );
159 var_Destroy( p_playlist, "play-and-stop" );
160 var_Destroy( p_playlist, "play-and-exit" );
161 var_Destroy( p_playlist, "random" );
162 var_Destroy( p_playlist, "repeat" );
163 var_Destroy( p_playlist, "loop" );
164 var_Destroy( p_playlist, "activity" );
167 /* Go through all items, and simply free everything without caring
168 * about the tree structure. Do not decref, it will be done by doing
169 * the same thing on the input items array */
170 FOREACH_ARRAY( playlist_item_t *p_del, p_playlist->all_items )
171 free( p_del->pp_children );
174 ARRAY_RESET( p_playlist->all_items );
176 FOREACH_ARRAY( input_item_t *p_del, p_playlist->input_items )
177 input_ItemClean( p_del );
180 ARRAY_RESET( p_playlist->input_items );
182 ARRAY_RESET( p_playlist->items );
183 ARRAY_RESET( p_playlist->current );
187 if( p_playlist->p_stats )
188 free( p_playlist->p_stats );
190 vlc_mutex_destroy( &p_playlist->gc_lock );
191 vlc_object_destroy( p_playlist->p_preparse );
192 vlc_object_destroy( p_playlist->p_secondary_preparse );
193 vlc_object_detach( p_playlist );
194 vlc_object_destroy( p_playlist );
197 /* Destroy remaining objects */
198 static mtime_t ObjectGarbageCollector( playlist_t *p_playlist, int i_type,
199 mtime_t destroy_date )
203 if( destroy_date > mdate() ) return destroy_date;
205 if( destroy_date == 0 )
207 /* give a little time */
208 return mdate() + I64C(1000000);
212 vlc_mutex_lock( &p_playlist->gc_lock );
213 while( ( p_obj = vlc_object_find( p_playlist, i_type, FIND_CHILD ) ) )
215 if( p_obj->p_parent != (vlc_object_t*)p_playlist )
217 /* only first child (ie unused) */
218 vlc_object_release( p_obj );
221 if( i_type == VLC_OBJECT_VOUT )
223 msg_Dbg( p_playlist, "garbage collector destroying 1 vout" );
224 vlc_object_detach( p_obj );
225 vlc_object_release( p_obj );
226 vout_Destroy( (vout_thread_t *)p_obj );
228 else if( i_type == VLC_OBJECT_SOUT )
230 vlc_object_release( p_obj );
231 sout_DeleteInstance( (sout_instance_t*)p_obj );
234 vlc_mutex_unlock( &p_playlist->gc_lock );
239 /** Main loop for the playlist */
240 void playlist_MainLoop( playlist_t *p_playlist )
242 playlist_item_t *p_item = NULL;
243 vlc_bool_t b_playexit = var_GetBool( p_playlist, "play-and-exit" );
246 /* First, check if we have something to do */
247 /* FIXME : this can be called several times */
248 if( p_playlist->request.b_request )
250 /* Stop the existing input */
251 if( p_playlist->p_input && !p_playlist->p_input->b_die )
253 PL_DEBUG( "incoming request - stopping current input" );
254 input_StopThread( p_playlist->p_input );
258 /* If there is an input, check that it doesn't need to die. */
259 if( p_playlist->p_input )
261 /* This input is dead. Remove it ! */
262 if( p_playlist->p_input->b_dead )
265 input_thread_t *p_input;
266 PL_DEBUG( "dead input" );
268 p_input = p_playlist->p_input;
269 p_playlist->p_input = NULL;
271 /* Release the playlist lock, because we may get stuck
272 * in input_DestroyThread() for some time. */
276 input_DestroyThread( p_input );
278 /* Unlink current input
279 * (_after_ input_DestroyThread for vout garbage collector) */
280 vlc_object_detach( p_input );
283 vlc_object_destroy( p_input );
285 p_playlist->i_vout_destroyed_date = 0;
286 p_playlist->i_sout_destroyed_date = 0;
288 if( p_playlist->status.p_item->i_flags
289 & PLAYLIST_REMOVE_FLAG )
291 PL_DEBUG( "%s was marked for deletion, deleting",
292 PLI_NAME( p_playlist->status.p_item ) );
293 playlist_ItemDelete( p_playlist->status.p_item );
294 if( p_playlist->request.p_item == p_playlist->status.p_item )
295 p_playlist->request.p_item = NULL;
296 p_playlist->status.p_item = NULL;
299 i_activity= var_GetInteger( p_playlist, "activity") ;
300 var_SetInteger( p_playlist, "activity", i_activity -
301 DEFAULT_INPUT_ACTIVITY );
305 /* This input is dying, let it do */
306 else if( p_playlist->p_input->b_die )
308 PL_DEBUG( "dying input" );
310 /* This input has finished, ask it to die ! */
311 else if( p_playlist->p_input->b_error
312 || p_playlist->p_input->b_eof )
314 PL_DEBUG( "finished input" );
315 input_StopThread( p_playlist->p_input );
316 /* Select the next playlist item */
320 else if( p_playlist->p_input->i_state != INIT_S )
323 p_playlist->i_vout_destroyed_date =
324 ObjectGarbageCollector( p_playlist, VLC_OBJECT_VOUT,
325 p_playlist->i_vout_destroyed_date );
326 p_playlist->i_sout_destroyed_date =
327 ObjectGarbageCollector( p_playlist, VLC_OBJECT_SOUT,
328 p_playlist->i_sout_destroyed_date );
334 /* No input. Several cases
335 * - No request, running status -> start new item
336 * - No request, stopped status -> collect garbage
337 * - Request, running requested -> start new item
338 * - Request, stopped requested -> collect garbage
340 if( (!p_playlist->request.b_request &&
341 p_playlist->status.i_status != PLAYLIST_STOPPED) ||
342 ( p_playlist->request.b_request &&
343 p_playlist->request.i_status != PLAYLIST_STOPPED ) )
345 msg_Dbg( p_playlist, "starting new item" );
346 stats_TimerStart( p_playlist, "Playlist walk",
347 STATS_TIMER_PLAYLIST_WALK );
348 p_item = playlist_NextItem( p_playlist );
349 stats_TimerStop( p_playlist, STATS_TIMER_PLAYLIST_WALK );
353 msg_Dbg( p_playlist, "nothing to play" );
354 if( b_playexit == VLC_TRUE )
356 msg_Info( p_playlist, "end of playlist, exiting" );
357 p_playlist->p_libvlc->b_die = VLC_TRUE;
359 p_playlist->status.i_status = PLAYLIST_STOPPED;
363 playlist_PlayItem( p_playlist, p_item );
367 p_playlist->status.i_status = PLAYLIST_STOPPED;
368 if( p_playlist->status.p_item &&
369 p_playlist->status.p_item->i_flags & PLAYLIST_REMOVE_FLAG )
371 PL_DEBUG( "deleting item marked for deletion" );
372 playlist_ItemDelete( p_playlist->status.p_item );
373 p_playlist->status.p_item = NULL;
376 /* Collect garbage */
378 p_playlist->i_sout_destroyed_date =
379 ObjectGarbageCollector( p_playlist, VLC_OBJECT_SOUT, mdate() );
380 p_playlist->i_vout_destroyed_date =
381 ObjectGarbageCollector( p_playlist, VLC_OBJECT_VOUT, mdate() );
388 /** Playlist dying last loop */
389 void playlist_LastLoop( playlist_t *p_playlist )
393 /* If there is an input, kill it */
398 if( p_playlist->p_input == NULL )
404 if( p_playlist->p_input->b_dead )
406 input_thread_t *p_input;
408 /* Unlink current input */
409 p_input = p_playlist->p_input;
410 p_playlist->p_input = NULL;
414 input_DestroyThread( p_input );
415 /* Unlink current input (_after_ input_DestroyThread for vout
416 * garbage collector)*/
417 vlc_object_detach( p_input );
420 vlc_object_destroy( p_input );
423 else if( p_playlist->p_input->b_die )
425 /* This input is dying, leave it alone */
428 else if( p_playlist->p_input->b_error || p_playlist->p_input->b_eof )
430 input_StopThread( p_playlist->p_input );
436 p_playlist->p_input->b_eof = 1;
441 msleep( INTF_IDLE_SLEEP );
444 /* close all remaining sout */
445 while( ( p_obj = vlc_object_find( p_playlist,
446 VLC_OBJECT_SOUT, FIND_CHILD ) ) )
448 vlc_object_release( p_obj );
449 sout_DeleteInstance( (sout_instance_t*)p_obj );
452 /* close all remaining vout */
453 while( ( p_obj = vlc_object_find( p_playlist,
454 VLC_OBJECT_VOUT, FIND_CHILD ) ) )
456 vlc_object_detach( p_obj );
457 vlc_object_release( p_obj );
458 vout_Destroy( (vout_thread_t *)p_obj );
462 /** Main loop for preparser queue */
463 void playlist_PreparseLoop( playlist_preparse_t *p_obj )
465 playlist_t *p_playlist = (playlist_t *)p_obj->p_parent;
466 input_item_t *p_current;
470 while( !p_playlist->b_die )
472 vlc_mutex_lock( &p_obj->object_lock );
473 while( p_obj->i_waiting == 0 )
475 vlc_cond_wait( &p_obj->object_wait, &p_obj->object_lock );
476 if( p_playlist->b_die )
478 vlc_mutex_unlock( &p_obj->object_lock );
483 p_current = p_obj->pp_waiting[0];
484 REMOVE_ELEM( p_obj->pp_waiting, p_obj->i_waiting, 0 );
485 vlc_mutex_unlock( &p_obj->object_lock );
490 vlc_bool_t b_preparsed = VLC_FALSE;
491 if( strncmp( p_current->psz_uri, "http:", 5 ) &&
492 strncmp( p_current->psz_uri, "rtsp:", 5 ) &&
493 strncmp( p_current->psz_uri, "udp:", 4 ) &&
494 strncmp( p_current->psz_uri, "mms:", 4 ) &&
495 strncmp( p_current->psz_uri, "cdda:", 4 ) &&
496 strncmp( p_current->psz_uri, "dvd:", 4 ) &&
497 strncmp( p_current->psz_uri, "v4l:", 4 ) &&
498 strncmp( p_current->psz_uri, "dshow:", 6 ) )
500 b_preparsed = VLC_TRUE;
501 stats_TimerStart( p_playlist, "Preparse run",
502 STATS_TIMER_PREPARSE );
504 input_Preparse( p_playlist, p_current );
506 stats_TimerStop( p_playlist, STATS_TIMER_PREPARSE );
511 p_current->p_meta->i_status |= ITEM_PREPARSED;
512 var_SetInteger( p_playlist, "item-change", p_current->i_id );
516 /* If we haven't retrieved enough meta, add to secondary queue
517 * which will run the "meta fetchers"
519 * don't do this for things we won't get meta for, like
522 if( !input_MetaSatisfied( p_playlist, p_current, &i_m, &i_o,
526 p.p_item = p_current;
527 p.b_fetch_art = VLC_FALSE;
528 vlc_mutex_lock( &p_playlist->p_secondary_preparse->object_lock);
529 INSERT_ELEM( p_playlist->p_secondary_preparse->p_waiting,
530 p_playlist->p_secondary_preparse->i_waiting,
531 p_playlist->p_secondary_preparse->i_waiting,
534 &p_playlist->p_secondary_preparse->object_lock);
536 &p_playlist->p_secondary_preparse->object_wait );
539 vlc_gc_decref( p_current );
545 vlc_mutex_lock( &p_obj->object_lock );
546 i_activity = var_GetInteger( p_playlist, "activity" );
547 if( i_activity < 0 ) i_activity = 0;
548 vlc_mutex_unlock( &p_obj->object_lock );
549 /* Sleep at least 1ms */
550 msleep( (i_activity+1) * 1000 );
554 /** Main loop for secondary preparser queue */
555 void playlist_SecondaryPreparseLoop( playlist_secondary_preparse_t *p_obj )
557 playlist_t *p_playlist = (playlist_t *)p_obj->p_parent;
558 vlc_bool_t b_fetch_art;
559 input_item_t *p_item;
562 while( !p_playlist->b_die )
564 vlc_mutex_lock( &p_obj->object_lock );
565 while( p_obj->i_waiting == 0 )
567 vlc_cond_wait( &p_obj->object_wait, &p_obj->object_lock );
568 if( p_playlist->b_die )
570 vlc_mutex_unlock( &p_obj->object_lock );
575 b_fetch_art = p_obj->p_waiting->b_fetch_art;
576 p_item = p_obj->p_waiting->p_item;
577 REMOVE_ELEM( p_obj->p_waiting, p_obj->i_waiting, 0 );
578 vlc_mutex_unlock( &p_obj->object_lock );
583 input_MetaFetch( p_playlist, p_item );
584 p_item->p_meta->i_status |= ITEM_META_FETCHED;
585 var_SetInteger( p_playlist, "item-change", p_item->i_id );
586 /* Fetch right now */
587 if( var_GetInteger( p_playlist, "album-art" ) == ALBUM_ART_ALL )
589 vlc_mutex_lock( &p_obj->object_lock );
592 p.b_fetch_art = VLC_TRUE;
593 INSERT_ELEM( p_playlist->p_secondary_preparse->p_waiting,
594 p_playlist->p_secondary_preparse->i_waiting,
596 vlc_mutex_unlock( &p_obj->object_lock );
599 vlc_gc_decref( p_item );
603 input_ArtFetch( p_playlist, p_item );
604 p_item->p_meta->i_status |= ITEM_ART_FETCHED;
605 vlc_gc_decref( p_item );
608 vlc_mutex_lock( &p_obj->object_lock );
609 i_activity = var_GetInteger( p_playlist, "activity" );
610 if( i_activity < 0 ) i_activity = 0;
611 vlc_mutex_unlock( &p_obj->object_lock );
612 /* Sleep at least 1ms */
613 msleep( (i_activity+1) * 1000 );
617 static void VariablesInit( playlist_t *p_playlist )
620 /* These variables control updates */
621 var_Create( p_playlist, "intf-change", VLC_VAR_BOOL );
622 val.b_bool = VLC_TRUE;
623 var_Set( p_playlist, "intf-change", val );
625 var_Create( p_playlist, "item-change", VLC_VAR_INTEGER );
627 var_Set( p_playlist, "item-change", val );
629 var_Create( p_playlist, "item-deleted", VLC_VAR_INTEGER );
631 var_Set( p_playlist, "item-deleted", val );
633 var_Create( p_playlist, "item-append", VLC_VAR_ADDRESS );
635 var_Create( p_playlist, "playlist-current", VLC_VAR_INTEGER );
637 var_Set( p_playlist, "playlist-current", val );
639 var_Create( p_playlist, "intf-popupmenu", VLC_VAR_BOOL );
641 var_Create( p_playlist, "intf-show", VLC_VAR_BOOL );
642 val.b_bool = VLC_TRUE;
643 var_Set( p_playlist, "intf-show", val );
645 var_Create( p_playlist, "activity", VLC_VAR_INTEGER );
646 var_SetInteger( p_playlist, "activity", 0 );
648 /* Variables to control playback */
649 var_CreateGetBool( p_playlist, "play-and-stop" );
650 var_CreateGetBool( p_playlist, "play-and-exit" );
651 var_CreateGetBool( p_playlist, "random" );
652 var_CreateGetBool( p_playlist, "repeat" );
653 var_CreateGetBool( p_playlist, "loop" );
655 var_AddCallback( p_playlist, "random", RandomCallback, NULL );