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 if( !p_playlist->p_root_category || !p_playlist->p_root_onelevel )
103 /* Create playlist and media library */
104 p_playlist->p_local_category = playlist_NodeCreate( p_playlist,
105 _( "Playlist" ),p_playlist->p_root_category );
106 p_playlist->p_local_onelevel = playlist_NodeCreate( p_playlist,
107 _( "Playlist" ), p_playlist->p_root_onelevel );
108 p_playlist->p_local_category->i_flags |= PLAYLIST_RO_FLAG;
109 p_playlist->p_local_onelevel->i_flags |= PLAYLIST_RO_FLAG;
111 if( !p_playlist->p_local_category || !p_playlist->p_local_onelevel ||
112 !p_playlist->p_local_category->p_input ||
113 !p_playlist->p_local_onelevel->p_input )
116 /* Link the nodes together. Todo: actually create them from the same input*/
117 p_playlist->p_local_onelevel->p_input->i_id =
118 p_playlist->p_local_category->p_input->i_id;
120 if( config_GetInt( p_playlist, "media-library") )
122 p_playlist->p_ml_category = playlist_NodeCreate( p_playlist,
123 _( "Media Library" ), p_playlist->p_root_category );
124 p_playlist->p_ml_onelevel = playlist_NodeCreate( p_playlist,
125 _( "Media Library" ), p_playlist->p_root_onelevel );
127 if(!p_playlist->p_ml_category || !p_playlist->p_ml_onelevel)
130 p_playlist->p_ml_category->i_flags |= PLAYLIST_RO_FLAG;
131 p_playlist->p_ml_onelevel->i_flags |= PLAYLIST_RO_FLAG;
132 p_playlist->p_ml_onelevel->p_input->i_id =
133 p_playlist->p_ml_category->p_input->i_id;
138 p_playlist->p_ml_category = p_playlist->p_ml_onelevel = NULL;
142 p_playlist->status.p_item = NULL;
143 p_playlist->status.p_node = p_playlist->p_local_onelevel;
144 p_playlist->request.b_request = VLC_FALSE;
145 p_playlist->status.i_status = PLAYLIST_STOPPED;
147 p_playlist->i_sort = SORT_ID;
148 p_playlist->i_order = ORDER_NORMAL;
150 vlc_object_attach( p_playlist, p_parent );
151 b_save = p_playlist->b_auto_preparse;
152 p_playlist->b_auto_preparse = VLC_FALSE;
153 playlist_MLLoad( p_playlist );
154 p_playlist->b_auto_preparse = VLC_TRUE;
158 void playlist_Destroy( playlist_t *p_playlist )
160 while( p_playlist->i_sds )
162 playlist_ServicesDiscoveryRemove( p_playlist,
163 p_playlist->pp_sds[0]->psz_module );
166 playlist_MLDump( p_playlist );
168 vlc_thread_join( p_playlist->p_preparse );
169 vlc_thread_join( p_playlist->p_fetcher );
170 vlc_thread_join( p_playlist );
172 vlc_object_detach( p_playlist->p_preparse );
173 vlc_object_detach( p_playlist->p_fetcher );
175 var_Destroy( p_playlist, "intf-change" );
176 var_Destroy( p_playlist, "item-change" );
177 var_Destroy( p_playlist, "playlist-current" );
178 var_Destroy( p_playlist, "intf-popmenu" );
179 var_Destroy( p_playlist, "intf-show" );
180 var_Destroy( p_playlist, "play-and-stop" );
181 var_Destroy( p_playlist, "play-and-exit" );
182 var_Destroy( p_playlist, "random" );
183 var_Destroy( p_playlist, "repeat" );
184 var_Destroy( p_playlist, "loop" );
185 var_Destroy( p_playlist, "activity" );
188 /* Go through all items, and simply free everything without caring
189 * about the tree structure. Do not decref, it will be done by doing
190 * the same thing on the input items array */
191 FOREACH_ARRAY( playlist_item_t *p_del, p_playlist->all_items )
192 free( p_del->pp_children );
195 ARRAY_RESET( p_playlist->all_items );
197 FOREACH_ARRAY( input_item_t *p_del, p_playlist->input_items )
198 input_ItemClean( p_del );
201 ARRAY_RESET( p_playlist->input_items );
203 ARRAY_RESET( p_playlist->items );
204 ARRAY_RESET( p_playlist->current );
208 vlc_mutex_destroy( &p_playlist->p_stats->lock );
209 if( p_playlist->p_stats )
210 free( p_playlist->p_stats );
212 vlc_mutex_destroy( &p_playlist->gc_lock );
213 vlc_object_destroy( p_playlist->p_preparse );
214 vlc_object_destroy( p_playlist->p_fetcher );
215 vlc_object_detach( p_playlist );
216 vlc_object_destroy( p_playlist );
219 /* Destroy remaining objects */
220 static void ObjectGarbageCollector( playlist_t *p_playlist )
224 if( mdate() - p_playlist->gc_date < 1000000 )
226 p_playlist->b_cant_sleep = VLC_TRUE;
229 else if( p_playlist->gc_date == 0 )
232 vlc_mutex_lock( &p_playlist->gc_lock );
233 while( ( p_obj = vlc_object_find( p_playlist, VLC_OBJECT_VOUT,
236 if( p_obj->p_parent != (vlc_object_t*)p_playlist )
238 vlc_object_release( p_obj );
241 msg_Dbg( p_playlist, "garbage collector destroying 1 vout" );
242 vlc_object_detach( p_obj );
243 vlc_object_release( p_obj );
244 vout_Destroy( (vout_thread_t *)p_obj );
246 while( ( p_obj = vlc_object_find( p_playlist, VLC_OBJECT_SOUT,
249 if( p_obj->p_parent != (vlc_object_t*)p_playlist )
251 vlc_object_release( p_obj );
254 vlc_object_release( p_obj );
255 sout_DeleteInstance( (sout_instance_t*)p_obj );
257 p_playlist->b_cant_sleep = VLC_FALSE;
258 vlc_mutex_unlock( &p_playlist->gc_lock );
261 /** Main loop for the playlist */
262 void playlist_MainLoop( playlist_t *p_playlist )
264 playlist_item_t *p_item = NULL;
265 vlc_bool_t b_playexit = var_GetBool( p_playlist, "play-and-exit" );
268 if( p_playlist->b_reset_currently_playing &&
269 mdate() - p_playlist->last_rebuild_date > 30000 ) // 30 ms
271 ResetCurrentlyPlaying( p_playlist, var_GetBool( p_playlist, "random"),
272 p_playlist->status.p_item );
273 p_playlist->last_rebuild_date = mdate();
277 /* If there is an input, check that it doesn't need to die. */
278 if( p_playlist->p_input )
280 if( p_playlist->request.b_request && !p_playlist->p_input->b_die )
282 PL_DEBUG( "incoming request - stopping current input" );
283 input_StopThread( p_playlist->p_input );
286 /* This input is dead. Remove it ! */
287 if( p_playlist->p_input->b_dead )
290 input_thread_t *p_input;
291 PL_DEBUG( "dead input" );
293 p_input = p_playlist->p_input;
294 p_playlist->p_input = NULL;
296 /* Release the playlist lock, because we may get stuck
297 * in input_DestroyThread() for some time. */
301 input_DestroyThread( p_input );
303 /* Unlink current input
304 * (_after_ input_DestroyThread for vout garbage collector) */
305 vlc_object_detach( p_input );
308 vlc_object_destroy( p_input );
312 p_playlist->gc_date = mdate();
313 p_playlist->b_cant_sleep = VLC_TRUE;
315 if( p_playlist->status.p_item->i_flags
316 & PLAYLIST_REMOVE_FLAG )
318 PL_DEBUG( "%s was marked for deletion, deleting",
319 PLI_NAME( p_playlist->status.p_item ) );
320 playlist_ItemDelete( p_playlist->status.p_item );
321 if( p_playlist->request.p_item == p_playlist->status.p_item )
322 p_playlist->request.p_item = NULL;
323 p_playlist->status.p_item = NULL;
326 i_activity= var_GetInteger( p_playlist, "activity") ;
327 var_SetInteger( p_playlist, "activity", i_activity -
328 DEFAULT_INPUT_ACTIVITY );
331 /* This input is dying, let it do */
332 else if( p_playlist->p_input->b_die )
334 PL_DEBUG( "dying input" );
335 msleep( 25000 ); // 25 ms
338 /* This input has finished, ask it to die ! */
339 else if( p_playlist->p_input->b_error
340 || p_playlist->p_input->b_eof )
342 PL_DEBUG( "finished input" );
343 input_StopThread( p_playlist->p_input );
344 /* No need to wait here, we'll wait in the p_input->b_die case */
347 else if( p_playlist->p_input->i_state != INIT_S )
350 ObjectGarbageCollector( p_playlist );
356 /* No input. Several cases
357 * - No request, running status -> start new item
358 * - No request, stopped status -> collect garbage
359 * - Request, running requested -> start new item
360 * - Request, stopped requested -> collect garbage
362 if( (!p_playlist->request.b_request &&
363 p_playlist->status.i_status != PLAYLIST_STOPPED) ||
364 ( p_playlist->request.b_request &&
365 p_playlist->request.i_status != PLAYLIST_STOPPED ) )
367 msg_Dbg( p_playlist, "starting new item" );
368 p_item = playlist_NextItem( p_playlist );
372 msg_Dbg( p_playlist, "nothing to play" );
373 if( b_playexit == VLC_TRUE )
375 msg_Info( p_playlist, "end of playlist, exiting" );
376 p_playlist->p_libvlc->b_die = VLC_TRUE;
378 p_playlist->status.i_status = PLAYLIST_STOPPED;
382 playlist_PlayItem( p_playlist, p_item );
386 p_playlist->status.i_status = PLAYLIST_STOPPED;
387 if( p_playlist->status.p_item &&
388 p_playlist->status.p_item->i_flags & PLAYLIST_REMOVE_FLAG )
390 PL_DEBUG( "deleting item marked for deletion" );
391 playlist_ItemDelete( p_playlist->status.p_item );
392 p_playlist->status.p_item = NULL;
395 /* Collect garbage */
397 ObjectGarbageCollector( p_playlist );
404 /** Playlist dying last loop */
405 void playlist_LastLoop( playlist_t *p_playlist )
409 /* If there is an input, kill it */
414 if( p_playlist->p_input == NULL )
420 if( p_playlist->p_input->b_dead )
422 input_thread_t *p_input;
424 /* Unlink current input */
425 p_input = p_playlist->p_input;
426 p_playlist->p_input = NULL;
430 input_DestroyThread( p_input );
431 /* Unlink current input (_after_ input_DestroyThread for vout
432 * garbage collector)*/
433 vlc_object_detach( p_input );
436 vlc_object_destroy( p_input );
439 else if( p_playlist->p_input->b_die )
441 /* This input is dying, leave it alone */
444 else if( p_playlist->p_input->b_error || p_playlist->p_input->b_eof )
446 input_StopThread( p_playlist->p_input );
452 p_playlist->p_input->b_eof = 1;
457 msleep( INTF_IDLE_SLEEP );
460 /* close all remaining sout */
461 while( ( p_obj = vlc_object_find( p_playlist,
462 VLC_OBJECT_SOUT, FIND_CHILD ) ) )
464 vlc_object_release( p_obj );
465 sout_DeleteInstance( (sout_instance_t*)p_obj );
468 /* close all remaining vout */
469 while( ( p_obj = vlc_object_find( p_playlist,
470 VLC_OBJECT_VOUT, FIND_CHILD ) ) )
472 vlc_object_detach( p_obj );
473 vlc_object_release( p_obj );
474 vout_Destroy( (vout_thread_t *)p_obj );
478 /** Main loop for preparser queue */
479 void playlist_PreparseLoop( playlist_preparse_t *p_obj )
481 playlist_t *p_playlist = (playlist_t *)p_obj->p_parent;
482 input_item_t *p_current;
486 while( !p_playlist->b_die )
488 vlc_mutex_lock( &p_obj->object_lock );
489 while( p_obj->i_waiting == 0 )
491 vlc_cond_wait( &p_obj->object_wait, &p_obj->object_lock );
492 if( p_playlist->b_die )
494 vlc_mutex_unlock( &p_obj->object_lock );
499 p_current = p_obj->pp_waiting[0];
500 REMOVE_ELEM( p_obj->pp_waiting, p_obj->i_waiting, 0 );
501 vlc_mutex_unlock( &p_obj->object_lock );
506 vlc_bool_t b_preparsed = VLC_FALSE;
507 if( strncmp( p_current->psz_uri, "http:", 5 ) &&
508 strncmp( p_current->psz_uri, "rtsp:", 5 ) &&
509 strncmp( p_current->psz_uri, "udp:", 4 ) &&
510 strncmp( p_current->psz_uri, "mms:", 4 ) &&
511 strncmp( p_current->psz_uri, "cdda:", 4 ) &&
512 strncmp( p_current->psz_uri, "dvd:", 4 ) &&
513 strncmp( p_current->psz_uri, "v4l:", 4 ) &&
514 strncmp( p_current->psz_uri, "dshow:", 6 ) )
516 b_preparsed = VLC_TRUE;
517 stats_TimerStart( p_playlist, "Preparse run",
518 STATS_TIMER_PREPARSE );
520 input_Preparse( p_playlist, p_current );
522 stats_TimerStop( p_playlist, STATS_TIMER_PREPARSE );
527 p_current->p_meta->i_status |= ITEM_PREPARSED;
528 var_SetInteger( p_playlist, "item-change", p_current->i_id );
532 /* If we haven't retrieved enough meta, add to secondary queue
533 * which will run the "meta fetchers".
534 * This only checks for meta, not for art
535 * \todo don't do this for things we won't get meta for, like vids
537 if( p_current->p_meta &&
538 !input_MetaSatisfied( p_playlist, p_current, &i_m, &i_o ) )
541 PL_DEBUG("need to fetch meta for %s", p_current->psz_name );
542 p.p_item = p_current;
543 p.b_fetch_art = VLC_FALSE;
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_mutex_unlock( &p_playlist->p_fetcher->object_lock );
549 vlc_cond_signal( &p_playlist->p_fetcher->object_wait );
551 /* We already have all needed meta, but we need art right now */
552 else if( p_current->p_meta &&
553 p_playlist->p_fetcher->i_art_policy == ALBUM_ART_ALL &&
554 EMPTY_STR( p_current->p_meta->psz_arturl ) )
557 PL_DEBUG("meta ok for %s, need to fetch art",
558 p_current->psz_name );
559 p.p_item = p_current;
560 p.b_fetch_art = VLC_TRUE;
561 vlc_mutex_lock( &p_playlist->p_fetcher->object_lock );
562 INSERT_ELEM( p_playlist->p_fetcher->p_waiting,
563 p_playlist->p_fetcher->i_waiting,
564 p_playlist->p_fetcher->i_waiting, p);
565 vlc_mutex_unlock( &p_playlist->p_fetcher->object_lock );
566 vlc_cond_signal( &p_playlist->p_fetcher->object_wait );
570 PL_DEBUG( "no fetch required for %s (art currently %s)",
572 p_current->p_meta ? p_current->p_meta->psz_arturl:
574 vlc_gc_decref( p_current );
581 vlc_mutex_lock( &p_obj->object_lock );
582 i_activity = var_GetInteger( p_playlist, "activity" );
583 if( i_activity < 0 ) i_activity = 0;
584 vlc_mutex_unlock( &p_obj->object_lock );
585 /* Sleep at least 1ms */
586 msleep( (i_activity+1) * 1000 );
590 /** Main loop for secondary preparser queue */
591 void playlist_FetcherLoop( playlist_fetcher_t *p_obj )
593 playlist_t *p_playlist = (playlist_t *)p_obj->p_parent;
594 vlc_bool_t b_fetch_art;
595 input_item_t *p_item;
598 while( !p_playlist->b_die )
600 vlc_mutex_lock( &p_obj->object_lock );
601 while( p_obj->i_waiting == 0 )
603 vlc_cond_wait( &p_obj->object_wait, &p_obj->object_lock );
604 if( p_playlist->b_die )
606 vlc_mutex_unlock( &p_obj->object_lock );
611 b_fetch_art = p_obj->p_waiting->b_fetch_art;
612 p_item = p_obj->p_waiting->p_item;
613 REMOVE_ELEM( p_obj->p_waiting, p_obj->i_waiting, 0 );
614 vlc_mutex_unlock( &p_obj->object_lock );
617 assert( p_item->p_meta );
620 input_MetaFetch( p_playlist, p_item );
621 p_item->p_meta->i_status |= ITEM_META_FETCHED;
622 var_SetInteger( p_playlist, "item-change", p_item->i_id );
623 /* Fetch right now */
624 if( p_playlist->p_fetcher->i_art_policy == ALBUM_ART_ALL )
626 vlc_mutex_lock( &p_obj->object_lock );
629 p.b_fetch_art = VLC_TRUE;
630 INSERT_ELEM( p_playlist->p_fetcher->p_waiting,
631 p_playlist->p_fetcher->i_waiting,
633 PL_DEBUG("meta fetched for %s, get art", p_item->psz_name);
634 vlc_mutex_unlock( &p_obj->object_lock );
638 vlc_gc_decref( p_item );
642 int i_ret = input_ArtFind( p_playlist, p_item );
645 PL_DEBUG("downloading art for %s", p_item->psz_name );
646 if( input_DownloadAndCacheArt( p_playlist, p_item ) )
647 p_item->p_meta->i_status |= ITEM_ART_NOTFOUND;
649 p_item->p_meta->i_status |= ITEM_ART_FETCHED;
650 var_SetInteger( p_playlist, "item-change",
654 else if( i_ret == 0 ) /* Was in cache */
656 PL_DEBUG("found art for %s in cache", p_item->psz_name );
657 p_item->p_meta->i_status |= ITEM_ART_FETCHED;
658 var_SetInteger( p_playlist, "item-change", p_item->i_id );
662 PL_DEBUG("art not found for %s", p_item->psz_name );
663 p_item->p_meta->i_status |= ITEM_ART_NOTFOUND;
665 vlc_gc_decref( p_item );
668 vlc_mutex_lock( &p_obj->object_lock );
669 i_activity = var_GetInteger( p_playlist, "activity" );
670 if( i_activity < 0 ) i_activity = 0;
671 vlc_mutex_unlock( &p_obj->object_lock );
672 /* Sleep at least 1ms */
673 msleep( (i_activity+1) * 1000 );
677 static void VariablesInit( playlist_t *p_playlist )
680 /* These variables control updates */
681 var_Create( p_playlist, "intf-change", VLC_VAR_BOOL );
682 val.b_bool = VLC_TRUE;
683 var_Set( p_playlist, "intf-change", val );
685 var_Create( p_playlist, "item-change", VLC_VAR_INTEGER );
687 var_Set( p_playlist, "item-change", val );
689 var_Create( p_playlist, "item-deleted", VLC_VAR_INTEGER );
691 var_Set( p_playlist, "item-deleted", val );
693 var_Create( p_playlist, "item-append", VLC_VAR_ADDRESS );
695 var_Create( p_playlist, "playlist-current", VLC_VAR_INTEGER );
697 var_Set( p_playlist, "playlist-current", val );
699 var_Create( p_playlist, "intf-popupmenu", VLC_VAR_BOOL );
701 var_Create( p_playlist, "intf-show", VLC_VAR_BOOL );
702 val.b_bool = VLC_TRUE;
703 var_Set( p_playlist, "intf-show", val );
705 var_Create( p_playlist, "activity", VLC_VAR_INTEGER );
706 var_SetInteger( p_playlist, "activity", 0 );
708 /* Variables to control playback */
709 var_CreateGetBool( p_playlist, "play-and-stop" );
710 var_CreateGetBool( p_playlist, "play-and-exit" );
711 var_CreateGetBool( p_playlist, "random" );
712 var_CreateGetBool( p_playlist, "repeat" );
713 var_CreateGetBool( p_playlist, "loop" );
715 var_AddCallback( p_playlist, "random", RandomCallback, NULL );