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;
58 /* Allocate structure */
59 p_playlist = vlc_object_create( p_parent, VLC_OBJECT_PLAYLIST );
62 msg_Err( p_parent, "out of memory" );
65 p_parent->p_libvlc->p_playlist = p_playlist;
67 VariablesInit( p_playlist );
69 /* Initialise data structures */
70 vlc_mutex_init( p_playlist, &p_playlist->gc_lock );
71 p_playlist->i_last_playlist_id = 0;
72 p_playlist->i_last_input_id = 0;
73 p_playlist->p_input = NULL;
75 p_playlist->gc_date = 0;
76 p_playlist->b_cant_sleep = VLC_FALSE;
78 ARRAY_INIT( p_playlist->items );
79 ARRAY_INIT( p_playlist->all_items );
80 ARRAY_INIT( p_playlist->input_items );
81 ARRAY_INIT( p_playlist->current );
83 p_playlist->i_current_index = 0;
84 p_playlist->b_reset_currently_playing = VLC_TRUE;
85 p_playlist->last_rebuild_date = 0;
87 i_tree = var_CreateGetBool( p_playlist, "playlist-tree" );
88 p_playlist->b_always_tree = (i_tree == 1);
89 p_playlist->b_never_tree = (i_tree == 2);
91 p_playlist->b_doing_ml = VLC_FALSE;
93 p_playlist->p_root_category = playlist_NodeCreate( p_playlist, NULL, NULL);
94 p_playlist->p_root_onelevel = playlist_NodeCreate( p_playlist, NULL, NULL);
96 /* Create playlist and media library */
97 p_playlist->p_local_category = playlist_NodeCreate( p_playlist,
98 _( "Playlist" ),p_playlist->p_root_category );
99 p_playlist->p_local_onelevel = playlist_NodeCreate( p_playlist,
100 _( "Playlist" ), p_playlist->p_root_onelevel );
101 p_playlist->p_local_category->i_flags |= PLAYLIST_RO_FLAG;
102 p_playlist->p_local_onelevel->i_flags |= PLAYLIST_RO_FLAG;
104 /* Link the nodes together. Todo: actually create them from the same input*/
105 p_playlist->p_local_onelevel->p_input->i_id =
106 p_playlist->p_local_category->p_input->i_id;
108 if( config_GetInt( p_playlist, "media-library") )
110 p_playlist->p_ml_category = playlist_NodeCreate( p_playlist,
111 _( "Media Library" ), p_playlist->p_root_category );
112 p_playlist->p_ml_onelevel = playlist_NodeCreate( p_playlist,
113 _( "Media Library" ), p_playlist->p_root_onelevel );
114 p_playlist->p_ml_category->i_flags |= PLAYLIST_RO_FLAG;
115 p_playlist->p_ml_onelevel->i_flags |= PLAYLIST_RO_FLAG;
116 p_playlist->p_ml_onelevel->p_input->i_id =
117 p_playlist->p_ml_category->p_input->i_id;
122 p_playlist->p_ml_category = p_playlist->p_ml_onelevel = NULL;
126 p_playlist->status.p_item = NULL;
127 p_playlist->status.p_node = p_playlist->p_local_onelevel;
128 p_playlist->request.b_request = VLC_FALSE;
129 p_playlist->status.i_status = PLAYLIST_STOPPED;
131 p_playlist->i_sort = SORT_ID;
132 p_playlist->i_order = ORDER_NORMAL;
134 vlc_object_attach( p_playlist, p_parent );
136 playlist_MLLoad( p_playlist );
140 void playlist_Destroy( playlist_t *p_playlist )
142 while( p_playlist->i_sds )
144 playlist_ServicesDiscoveryRemove( p_playlist,
145 p_playlist->pp_sds[0]->psz_module );
148 playlist_MLDump( p_playlist );
150 vlc_thread_join( p_playlist->p_preparse );
151 vlc_thread_join( p_playlist->p_fetcher );
152 vlc_thread_join( p_playlist );
154 vlc_object_detach( p_playlist->p_preparse );
155 vlc_object_detach( p_playlist->p_fetcher );
157 var_Destroy( p_playlist, "intf-change" );
158 var_Destroy( p_playlist, "item-change" );
159 var_Destroy( p_playlist, "playlist-current" );
160 var_Destroy( p_playlist, "intf-popmenu" );
161 var_Destroy( p_playlist, "intf-show" );
162 var_Destroy( p_playlist, "play-and-stop" );
163 var_Destroy( p_playlist, "play-and-exit" );
164 var_Destroy( p_playlist, "random" );
165 var_Destroy( p_playlist, "repeat" );
166 var_Destroy( p_playlist, "loop" );
167 var_Destroy( p_playlist, "activity" );
170 /* Go through all items, and simply free everything without caring
171 * about the tree structure. Do not decref, it will be done by doing
172 * the same thing on the input items array */
173 FOREACH_ARRAY( playlist_item_t *p_del, p_playlist->all_items )
174 free( p_del->pp_children );
177 ARRAY_RESET( p_playlist->all_items );
179 FOREACH_ARRAY( input_item_t *p_del, p_playlist->input_items )
180 input_ItemClean( p_del );
183 ARRAY_RESET( p_playlist->input_items );
185 ARRAY_RESET( p_playlist->items );
186 ARRAY_RESET( p_playlist->current );
190 if( p_playlist->p_stats )
191 free( p_playlist->p_stats );
193 vlc_mutex_destroy( &p_playlist->gc_lock );
194 vlc_object_destroy( p_playlist->p_preparse );
195 vlc_object_destroy( p_playlist->p_fetcher );
196 vlc_object_detach( p_playlist );
197 vlc_object_destroy( p_playlist );
201 /* Destroy remaining objects */
202 static void ObjectGarbageCollector( playlist_t *p_playlist )
206 if( mdate() - p_playlist->gc_date < 1000000 )
208 p_playlist->b_cant_sleep = VLC_TRUE;
211 else if( p_playlist->gc_date == 0 )
214 vlc_mutex_lock( &p_playlist->gc_lock );
215 while( ( p_obj = vlc_object_find( p_playlist, VLC_OBJECT_VOUT,
218 if( p_obj->p_parent != (vlc_object_t*)p_playlist )
220 vlc_object_release( p_obj );
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 while( ( p_obj = vlc_object_find( p_playlist, VLC_OBJECT_SOUT,
231 if( p_obj->p_parent != (vlc_object_t*)p_playlist )
233 vlc_object_release( p_obj );
236 vlc_object_release( p_obj );
237 sout_DeleteInstance( (sout_instance_t*)p_obj );
239 p_playlist->b_cant_sleep = VLC_FALSE;
240 vlc_mutex_unlock( &p_playlist->gc_lock );
243 /** Main loop for the playlist */
244 void playlist_MainLoop( playlist_t *p_playlist )
246 playlist_item_t *p_item = NULL;
247 vlc_bool_t b_playexit = var_GetBool( p_playlist, "play-and-exit" );
250 if( p_playlist->b_reset_currently_playing &&
251 mdate() - p_playlist->last_rebuild_date > 30000 ) // 30 ms
253 ResetCurrentlyPlaying( p_playlist, var_GetBool( p_playlist, "random"),
254 p_playlist->status.p_item );
255 p_playlist->last_rebuild_date = mdate();
259 /* If there is an input, check that it doesn't need to die. */
260 if( p_playlist->p_input )
262 if( p_playlist->request.b_request && !p_playlist->p_input->b_die )
264 PL_DEBUG( "incoming request - stopping current input" );
265 input_StopThread( p_playlist->p_input );
268 /* This input is dead. Remove it ! */
269 if( p_playlist->p_input->b_dead )
272 input_thread_t *p_input;
273 PL_DEBUG( "dead input" );
275 p_input = p_playlist->p_input;
276 p_playlist->p_input = NULL;
278 /* Release the playlist lock, because we may get stuck
279 * in input_DestroyThread() for some time. */
283 input_DestroyThread( p_input );
285 /* Unlink current input
286 * (_after_ input_DestroyThread for vout garbage collector) */
287 vlc_object_detach( p_input );
290 vlc_object_destroy( p_input );
294 p_playlist->gc_date = mdate();
295 p_playlist->b_cant_sleep = VLC_TRUE;
297 if( p_playlist->status.p_item->i_flags
298 & PLAYLIST_REMOVE_FLAG )
300 PL_DEBUG( "%s was marked for deletion, deleting",
301 PLI_NAME( p_playlist->status.p_item ) );
302 playlist_ItemDelete( p_playlist->status.p_item );
303 if( p_playlist->request.p_item == p_playlist->status.p_item )
304 p_playlist->request.p_item = NULL;
305 p_playlist->status.p_item = NULL;
308 i_activity= var_GetInteger( p_playlist, "activity") ;
309 var_SetInteger( p_playlist, "activity", i_activity -
310 DEFAULT_INPUT_ACTIVITY );
313 /* This input is dying, let it do */
314 else if( p_playlist->p_input->b_die )
316 PL_DEBUG( "dying input" );
317 msleep( 25000 ); // 25 ms
320 /* This input has finished, ask it to die ! */
321 else if( p_playlist->p_input->b_error
322 || p_playlist->p_input->b_eof )
324 PL_DEBUG( "finished input" );
325 input_StopThread( p_playlist->p_input );
326 /* No need to wait here, we'll wait in the p_input->b_die case */
329 else if( p_playlist->p_input->i_state != INIT_S )
332 ObjectGarbageCollector( p_playlist );
338 /* No input. Several cases
339 * - No request, running status -> start new item
340 * - No request, stopped status -> collect garbage
341 * - Request, running requested -> start new item
342 * - Request, stopped requested -> collect garbage
344 if( (!p_playlist->request.b_request &&
345 p_playlist->status.i_status != PLAYLIST_STOPPED) ||
346 ( p_playlist->request.b_request &&
347 p_playlist->request.i_status != PLAYLIST_STOPPED ) )
349 msg_Dbg( p_playlist, "starting new item" );
350 p_item = playlist_NextItem( p_playlist );
354 msg_Dbg( p_playlist, "nothing to play" );
355 if( b_playexit == VLC_TRUE )
357 msg_Info( p_playlist, "end of playlist, exiting" );
358 p_playlist->p_libvlc->b_die = VLC_TRUE;
360 p_playlist->status.i_status = PLAYLIST_STOPPED;
364 playlist_PlayItem( p_playlist, p_item );
368 p_playlist->status.i_status = PLAYLIST_STOPPED;
369 if( p_playlist->status.p_item &&
370 p_playlist->status.p_item->i_flags & PLAYLIST_REMOVE_FLAG )
372 PL_DEBUG( "deleting item marked for deletion" );
373 playlist_ItemDelete( p_playlist->status.p_item );
374 p_playlist->status.p_item = NULL;
377 /* Collect garbage */
379 ObjectGarbageCollector( p_playlist );
386 /** Playlist dying last loop */
387 void playlist_LastLoop( playlist_t *p_playlist )
391 /* If there is an input, kill it */
396 if( p_playlist->p_input == NULL )
402 if( p_playlist->p_input->b_dead )
404 input_thread_t *p_input;
406 /* Unlink current input */
407 p_input = p_playlist->p_input;
408 p_playlist->p_input = NULL;
412 input_DestroyThread( p_input );
413 /* Unlink current input (_after_ input_DestroyThread for vout
414 * garbage collector)*/
415 vlc_object_detach( p_input );
418 vlc_object_destroy( p_input );
421 else if( p_playlist->p_input->b_die )
423 /* This input is dying, leave it alone */
426 else if( p_playlist->p_input->b_error || p_playlist->p_input->b_eof )
428 input_StopThread( p_playlist->p_input );
434 p_playlist->p_input->b_eof = 1;
439 msleep( INTF_IDLE_SLEEP );
442 /* close all remaining sout */
443 while( ( p_obj = vlc_object_find( p_playlist,
444 VLC_OBJECT_SOUT, FIND_CHILD ) ) )
446 vlc_object_release( p_obj );
447 sout_DeleteInstance( (sout_instance_t*)p_obj );
450 /* close all remaining vout */
451 while( ( p_obj = vlc_object_find( p_playlist,
452 VLC_OBJECT_VOUT, FIND_CHILD ) ) )
454 vlc_object_detach( p_obj );
455 vlc_object_release( p_obj );
456 vout_Destroy( (vout_thread_t *)p_obj );
460 /** Main loop for preparser queue */
461 void playlist_PreparseLoop( playlist_preparse_t *p_obj )
463 playlist_t *p_playlist = (playlist_t *)p_obj->p_parent;
464 input_item_t *p_current;
468 while( !p_playlist->b_die )
470 vlc_mutex_lock( &p_obj->object_lock );
471 while( p_obj->i_waiting == 0 )
473 vlc_cond_wait( &p_obj->object_wait, &p_obj->object_lock );
474 if( p_playlist->b_die )
476 vlc_mutex_unlock( &p_obj->object_lock );
481 p_current = p_obj->pp_waiting[0];
482 REMOVE_ELEM( p_obj->pp_waiting, p_obj->i_waiting, 0 );
483 vlc_mutex_unlock( &p_obj->object_lock );
488 vlc_bool_t b_preparsed = VLC_FALSE;
489 if( strncmp( p_current->psz_uri, "http:", 5 ) &&
490 strncmp( p_current->psz_uri, "rtsp:", 5 ) &&
491 strncmp( p_current->psz_uri, "udp:", 4 ) &&
492 strncmp( p_current->psz_uri, "mms:", 4 ) &&
493 strncmp( p_current->psz_uri, "cdda:", 4 ) &&
494 strncmp( p_current->psz_uri, "dvd:", 4 ) &&
495 strncmp( p_current->psz_uri, "v4l:", 4 ) &&
496 strncmp( p_current->psz_uri, "dshow:", 6 ) )
498 b_preparsed = VLC_TRUE;
499 stats_TimerStart( p_playlist, "Preparse run",
500 STATS_TIMER_PREPARSE );
502 input_Preparse( p_playlist, p_current );
504 stats_TimerStop( p_playlist, STATS_TIMER_PREPARSE );
509 p_current->p_meta->i_status |= ITEM_PREPARSED;
510 var_SetInteger( p_playlist, "item-change", p_current->i_id );
514 /* If we haven't retrieved enough meta, add to secondary queue
515 * which will run the "meta fetchers".
516 * This only checks for meta, not for art
517 * \todo don't do this for things we won't get meta for, like vids
519 if( !input_MetaSatisfied( p_playlist, p_current, &i_m, &i_o ) )
522 PL_DEBUG("need to fetch meta for %s", p_current->psz_name );
523 p.p_item = p_current;
524 p.b_fetch_art = VLC_FALSE;
525 vlc_mutex_lock( &p_playlist->p_fetcher->object_lock );
526 INSERT_ELEM( p_playlist->p_fetcher->p_waiting,
527 p_playlist->p_fetcher->i_waiting,
528 p_playlist->p_fetcher->i_waiting,
530 vlc_mutex_unlock( &p_playlist->p_fetcher->object_lock );
531 vlc_cond_signal( &p_playlist->p_fetcher->object_wait );
533 /* We already have all needed meta, but we need art right now */
534 else if( p_playlist->p_fetcher->i_art_policy == ALBUM_ART_ALL &&
535 EMPTY_STR( p_current->p_meta->psz_arturl ) )
538 PL_DEBUG("meta ok for %s, need to fetch art",
539 p_current->psz_name );
540 p.p_item = p_current;
541 p.b_fetch_art = VLC_TRUE;
542 vlc_mutex_lock( &p_playlist->p_fetcher->object_lock );
543 INSERT_ELEM( p_playlist->p_fetcher->p_waiting,
544 p_playlist->p_fetcher->i_waiting,
545 p_playlist->p_fetcher->i_waiting,
547 vlc_mutex_unlock( &p_playlist->p_fetcher->object_lock );
548 vlc_cond_signal( &p_playlist->p_fetcher->object_wait );
552 PL_DEBUG( "no fetch required for %s (art currently %s)",
553 p_current->psz_name, p_current->p_meta->psz_arturl );
554 vlc_gc_decref( p_current );
561 vlc_mutex_lock( &p_obj->object_lock );
562 i_activity = var_GetInteger( p_playlist, "activity" );
563 if( i_activity < 0 ) i_activity = 0;
564 vlc_mutex_unlock( &p_obj->object_lock );
565 /* Sleep at least 1ms */
566 msleep( (i_activity+1) * 1000 );
570 /** Main loop for secondary preparser queue */
571 void playlist_FetcherLoop( playlist_fetcher_t *p_obj )
573 playlist_t *p_playlist = (playlist_t *)p_obj->p_parent;
574 vlc_bool_t b_fetch_art;
575 input_item_t *p_item;
578 while( !p_playlist->b_die )
580 vlc_mutex_lock( &p_obj->object_lock );
581 while( p_obj->i_waiting == 0 )
583 vlc_cond_wait( &p_obj->object_wait, &p_obj->object_lock );
584 if( p_playlist->b_die )
586 vlc_mutex_unlock( &p_obj->object_lock );
591 b_fetch_art = p_obj->p_waiting->b_fetch_art;
592 p_item = p_obj->p_waiting->p_item;
593 REMOVE_ELEM( p_obj->p_waiting, p_obj->i_waiting, 0 );
594 vlc_mutex_unlock( &p_obj->object_lock );
599 input_MetaFetch( p_playlist, p_item );
600 p_item->p_meta->i_status |= ITEM_META_FETCHED;
601 var_SetInteger( p_playlist, "item-change", p_item->i_id );
602 /* Fetch right now */
603 if( p_playlist->p_fetcher->i_art_policy == ALBUM_ART_ALL )
605 vlc_mutex_lock( &p_obj->object_lock );
608 p.b_fetch_art = VLC_TRUE;
609 INSERT_ELEM( p_playlist->p_fetcher->p_waiting,
610 p_playlist->p_fetcher->i_waiting,
612 PL_DEBUG("meta fetched for %s, get art", p_item->psz_name);
613 vlc_mutex_unlock( &p_obj->object_lock );
617 vlc_gc_decref( p_item );
621 int i_ret = input_ArtFind( p_playlist, p_item );
624 PL_DEBUG("downloading art for %s", p_item->psz_name );
625 if( !input_DownloadAndCacheArt( p_playlist, p_item ) )
626 p_item->p_meta->i_status |= ITEM_ART_NOTFOUND;
628 p_item->p_meta->i_status |= ITEM_ART_FETCHED;
630 else if( i_ret == 0 ) /* Was in cache */
632 PL_DEBUG("found art for %s in cache", p_item->psz_name );
633 p_item->p_meta->i_status |= ITEM_ART_FETCHED;
637 PL_DEBUG("art not found for %s", p_item->psz_name );
638 p_item->p_meta->i_status |= ITEM_ART_NOTFOUND;
640 vlc_gc_decref( p_item );
643 vlc_mutex_lock( &p_obj->object_lock );
644 i_activity = var_GetInteger( p_playlist, "activity" );
645 if( i_activity < 0 ) i_activity = 0;
646 vlc_mutex_unlock( &p_obj->object_lock );
647 /* Sleep at least 1ms */
648 msleep( (i_activity+1) * 1000 );
652 static void VariablesInit( playlist_t *p_playlist )
655 /* These variables control updates */
656 var_Create( p_playlist, "intf-change", VLC_VAR_BOOL );
657 val.b_bool = VLC_TRUE;
658 var_Set( p_playlist, "intf-change", val );
660 var_Create( p_playlist, "item-change", VLC_VAR_INTEGER );
662 var_Set( p_playlist, "item-change", val );
664 var_Create( p_playlist, "item-deleted", VLC_VAR_INTEGER );
666 var_Set( p_playlist, "item-deleted", val );
668 var_Create( p_playlist, "item-append", VLC_VAR_ADDRESS );
670 var_Create( p_playlist, "playlist-current", VLC_VAR_INTEGER );
672 var_Set( p_playlist, "playlist-current", val );
674 var_Create( p_playlist, "intf-popupmenu", VLC_VAR_BOOL );
676 var_Create( p_playlist, "intf-show", VLC_VAR_BOOL );
677 val.b_bool = VLC_TRUE;
678 var_Set( p_playlist, "intf-show", val );
680 var_Create( p_playlist, "activity", VLC_VAR_INTEGER );
681 var_SetInteger( p_playlist, "activity", 0 );
683 /* Variables to control playback */
684 var_CreateGetBool( p_playlist, "play-and-stop" );
685 var_CreateGetBool( p_playlist, "play-and-exit" );
686 var_CreateGetBool( p_playlist, "random" );
687 var_CreateGetBool( p_playlist, "repeat" );
688 var_CreateGetBool( p_playlist, "loop" );
690 var_AddCallback( p_playlist, "random", RandomCallback, NULL );