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;
41 playlist_Signal( ((playlist_t*)p_this) );
48 * Create a playlist structure.
49 * \param p_parent the vlc object that is to be the parent of this playlist
50 * \return a pointer to the created playlist, or NULL on error
52 playlist_t * playlist_Create( vlc_object_t *p_parent )
54 playlist_t *p_playlist;
57 /* Allocate structure */
58 p_playlist = vlc_object_create( p_parent, VLC_OBJECT_PLAYLIST );
61 msg_Err( p_parent, "out of memory" );
64 p_parent->p_libvlc->p_playlist = p_playlist;
66 VariablesInit( p_playlist );
68 /* Initialise data structures */
69 vlc_mutex_init( p_playlist, &p_playlist->gc_lock );
70 p_playlist->i_last_playlist_id = 0;
71 p_playlist->i_last_input_id = 0;
72 p_playlist->p_input = NULL;
74 p_playlist->gc_date = 0;
75 p_playlist->b_cant_sleep = VLC_FALSE;
77 ARRAY_INIT( p_playlist->items );
78 ARRAY_INIT( p_playlist->all_items );
79 ARRAY_INIT( p_playlist->input_items );
80 ARRAY_INIT( p_playlist->current );
82 p_playlist->i_current_index = 0;
83 p_playlist->b_reset_currently_playing = VLC_TRUE;
84 p_playlist->last_rebuild_date = 0;
86 i_tree = var_CreateGetBool( p_playlist, "playlist-tree" );
87 p_playlist->b_always_tree = (i_tree == 1);
88 p_playlist->b_never_tree = (i_tree == 2);
90 p_playlist->b_doing_ml = VLC_FALSE;
92 p_playlist->p_root_category = playlist_NodeCreate( p_playlist, NULL, NULL);
93 p_playlist->p_root_onelevel = playlist_NodeCreate( p_playlist, NULL, NULL);
95 /* Create playlist and media library */
96 p_playlist->p_local_category = playlist_NodeCreate( p_playlist,
97 _( "Playlist" ),p_playlist->p_root_category );
98 p_playlist->p_local_onelevel = playlist_NodeCreate( p_playlist,
99 _( "Playlist" ), p_playlist->p_root_onelevel );
100 p_playlist->p_local_category->i_flags |= PLAYLIST_RO_FLAG;
101 p_playlist->p_local_onelevel->i_flags |= PLAYLIST_RO_FLAG;
103 /* Link the nodes together. Todo: actually create them from the same input*/
104 p_playlist->p_local_onelevel->p_input->i_id =
105 p_playlist->p_local_category->p_input->i_id;
107 if( config_GetInt( p_playlist, "media-library") )
109 p_playlist->p_ml_category = playlist_NodeCreate( p_playlist,
110 _( "Media Library" ), p_playlist->p_root_category );
111 p_playlist->p_ml_onelevel = playlist_NodeCreate( p_playlist,
112 _( "Media Library" ), p_playlist->p_root_onelevel );
113 p_playlist->p_ml_category->i_flags |= PLAYLIST_RO_FLAG;
114 p_playlist->p_ml_onelevel->i_flags |= PLAYLIST_RO_FLAG;
115 p_playlist->p_ml_onelevel->p_input->i_id =
116 p_playlist->p_ml_category->p_input->i_id;
121 p_playlist->p_ml_category = p_playlist->p_ml_onelevel = NULL;
125 p_playlist->status.p_item = NULL;
126 p_playlist->status.p_node = p_playlist->p_local_onelevel;
127 p_playlist->request.b_request = VLC_FALSE;
128 p_playlist->status.i_status = PLAYLIST_STOPPED;
130 p_playlist->i_sort = SORT_ID;
131 p_playlist->i_order = ORDER_NORMAL;
133 vlc_object_attach( p_playlist, p_parent );
135 playlist_MLLoad( p_playlist );
139 void playlist_Destroy( playlist_t *p_playlist )
141 while( p_playlist->i_sds )
143 playlist_ServicesDiscoveryRemove( p_playlist,
144 p_playlist->pp_sds[0]->psz_module );
147 playlist_MLDump( p_playlist );
149 vlc_thread_join( p_playlist->p_preparse );
150 vlc_thread_join( p_playlist->p_secondary_preparse );
151 vlc_thread_join( p_playlist );
153 vlc_object_detach( p_playlist->p_preparse );
154 vlc_object_detach( p_playlist->p_secondary_preparse );
156 var_Destroy( p_playlist, "intf-change" );
157 var_Destroy( p_playlist, "item-change" );
158 var_Destroy( p_playlist, "playlist-current" );
159 var_Destroy( p_playlist, "intf-popmenu" );
160 var_Destroy( p_playlist, "intf-show" );
161 var_Destroy( p_playlist, "play-and-stop" );
162 var_Destroy( p_playlist, "play-and-exit" );
163 var_Destroy( p_playlist, "random" );
164 var_Destroy( p_playlist, "repeat" );
165 var_Destroy( p_playlist, "loop" );
166 var_Destroy( p_playlist, "activity" );
169 /* Go through all items, and simply free everything without caring
170 * about the tree structure. Do not decref, it will be done by doing
171 * the same thing on the input items array */
172 FOREACH_ARRAY( playlist_item_t *p_del, p_playlist->all_items )
173 free( p_del->pp_children );
176 ARRAY_RESET( p_playlist->all_items );
178 FOREACH_ARRAY( input_item_t *p_del, p_playlist->input_items )
179 input_ItemClean( p_del );
182 ARRAY_RESET( p_playlist->input_items );
184 ARRAY_RESET( p_playlist->items );
185 ARRAY_RESET( p_playlist->current );
189 if( p_playlist->p_stats )
190 free( p_playlist->p_stats );
192 vlc_mutex_destroy( &p_playlist->gc_lock );
193 vlc_object_destroy( p_playlist->p_preparse );
194 vlc_object_destroy( p_playlist->p_secondary_preparse );
195 vlc_object_detach( p_playlist );
196 vlc_object_destroy( p_playlist );
200 /* Destroy remaining objects */
201 static void ObjectGarbageCollector( playlist_t *p_playlist )
205 if( mdate() - p_playlist->gc_date < 1000000 )
207 p_playlist->b_cant_sleep = VLC_TRUE;
210 else if( p_playlist->gc_date == 0 )
213 vlc_mutex_lock( &p_playlist->gc_lock );
214 while( ( p_obj = vlc_object_find( p_playlist, VLC_OBJECT_VOUT,
217 if( p_obj->p_parent != (vlc_object_t*)p_playlist )
219 vlc_object_release( p_obj );
222 msg_Dbg( p_playlist, "garbage collector destroying 1 vout" );
223 vlc_object_detach( p_obj );
224 vlc_object_release( p_obj );
225 vout_Destroy( (vout_thread_t *)p_obj );
227 while( ( p_obj = vlc_object_find( p_playlist, VLC_OBJECT_SOUT,
230 if( p_obj->p_parent != (vlc_object_t*)p_playlist )
232 vlc_object_release( p_obj );
235 vlc_object_release( p_obj );
236 sout_DeleteInstance( (sout_instance_t*)p_obj );
238 p_playlist->b_cant_sleep = VLC_FALSE;
239 vlc_mutex_unlock( &p_playlist->gc_lock );
242 /** Main loop for the playlist */
243 void playlist_MainLoop( playlist_t *p_playlist )
245 playlist_item_t *p_item = NULL;
246 vlc_bool_t b_playexit = var_GetBool( p_playlist, "play-and-exit" );
249 if( p_playlist->b_reset_currently_playing &&
250 mdate() - p_playlist->last_rebuild_date > 30000 ) // 30 ms
252 ResetCurrentlyPlaying( p_playlist, var_GetBool( p_playlist, "random"),
253 p_playlist->status.p_item );
254 p_playlist->last_rebuild_date = mdate();
258 /* If there is an input, check that it doesn't need to die. */
259 if( p_playlist->p_input )
261 if( p_playlist->request.b_request && !p_playlist->p_input->b_die )
263 PL_DEBUG( "incoming request - stopping current input" );
264 input_StopThread( p_playlist->p_input );
267 /* This input is dead. Remove it ! */
268 if( p_playlist->p_input->b_dead )
271 input_thread_t *p_input;
272 PL_DEBUG( "dead input" );
274 p_input = p_playlist->p_input;
275 p_playlist->p_input = NULL;
277 /* Release the playlist lock, because we may get stuck
278 * in input_DestroyThread() for some time. */
282 input_DestroyThread( p_input );
284 /* Unlink current input
285 * (_after_ input_DestroyThread for vout garbage collector) */
286 vlc_object_detach( p_input );
289 vlc_object_destroy( p_input );
293 p_playlist->gc_date = mdate();
294 p_playlist->b_cant_sleep = VLC_TRUE;
296 if( p_playlist->status.p_item->i_flags
297 & PLAYLIST_REMOVE_FLAG )
299 PL_DEBUG( "%s was marked for deletion, deleting",
300 PLI_NAME( p_playlist->status.p_item ) );
301 playlist_ItemDelete( p_playlist->status.p_item );
302 if( p_playlist->request.p_item == p_playlist->status.p_item )
303 p_playlist->request.p_item = NULL;
304 p_playlist->status.p_item = NULL;
307 i_activity= var_GetInteger( p_playlist, "activity") ;
308 var_SetInteger( p_playlist, "activity", i_activity -
309 DEFAULT_INPUT_ACTIVITY );
312 /* This input is dying, let it do */
313 else if( p_playlist->p_input->b_die )
315 PL_DEBUG( "dying input" );
316 msleep( 25000 ); // 25 ms
319 /* This input has finished, ask it to die ! */
320 else if( p_playlist->p_input->b_error
321 || p_playlist->p_input->b_eof )
323 PL_DEBUG( "finished input" );
324 input_StopThread( p_playlist->p_input );
325 /* No need to wait here, we'll wait in the p_input->b_die case */
328 else if( p_playlist->p_input->i_state != INIT_S )
331 ObjectGarbageCollector( p_playlist );
337 /* No input. Several cases
338 * - No request, running status -> start new item
339 * - No request, stopped status -> collect garbage
340 * - Request, running requested -> start new item
341 * - Request, stopped requested -> collect garbage
343 if( (!p_playlist->request.b_request &&
344 p_playlist->status.i_status != PLAYLIST_STOPPED) ||
345 ( p_playlist->request.b_request &&
346 p_playlist->request.i_status != PLAYLIST_STOPPED ) )
348 msg_Dbg( p_playlist, "starting new item" );
349 p_item = playlist_NextItem( p_playlist );
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 ObjectGarbageCollector( p_playlist );
385 /** Playlist dying last loop */
386 void playlist_LastLoop( playlist_t *p_playlist )
390 /* If there is an input, kill it */
395 if( p_playlist->p_input == NULL )
401 if( p_playlist->p_input->b_dead )
403 input_thread_t *p_input;
405 /* Unlink current input */
406 p_input = p_playlist->p_input;
407 p_playlist->p_input = NULL;
411 input_DestroyThread( p_input );
412 /* Unlink current input (_after_ input_DestroyThread for vout
413 * garbage collector)*/
414 vlc_object_detach( p_input );
417 vlc_object_destroy( p_input );
420 else if( p_playlist->p_input->b_die )
422 /* This input is dying, leave it alone */
425 else if( p_playlist->p_input->b_error || p_playlist->p_input->b_eof )
427 input_StopThread( p_playlist->p_input );
433 p_playlist->p_input->b_eof = 1;
438 msleep( INTF_IDLE_SLEEP );
441 /* close all remaining sout */
442 while( ( p_obj = vlc_object_find( p_playlist,
443 VLC_OBJECT_SOUT, FIND_CHILD ) ) )
445 vlc_object_release( p_obj );
446 sout_DeleteInstance( (sout_instance_t*)p_obj );
449 /* close all remaining vout */
450 while( ( p_obj = vlc_object_find( p_playlist,
451 VLC_OBJECT_VOUT, FIND_CHILD ) ) )
453 vlc_object_detach( p_obj );
454 vlc_object_release( p_obj );
455 vout_Destroy( (vout_thread_t *)p_obj );
459 /** Main loop for preparser queue */
460 void playlist_PreparseLoop( playlist_preparse_t *p_obj )
462 playlist_t *p_playlist = (playlist_t *)p_obj->p_parent;
463 input_item_t *p_current;
467 while( !p_playlist->b_die )
469 vlc_mutex_lock( &p_obj->object_lock );
470 while( p_obj->i_waiting == 0 )
472 vlc_cond_wait( &p_obj->object_wait, &p_obj->object_lock );
473 if( p_playlist->b_die )
475 vlc_mutex_unlock( &p_obj->object_lock );
480 p_current = p_obj->pp_waiting[0];
481 REMOVE_ELEM( p_obj->pp_waiting, p_obj->i_waiting, 0 );
482 vlc_mutex_unlock( &p_obj->object_lock );
487 vlc_bool_t b_preparsed = VLC_FALSE;
488 if( strncmp( p_current->psz_uri, "http:", 5 ) &&
489 strncmp( p_current->psz_uri, "rtsp:", 5 ) &&
490 strncmp( p_current->psz_uri, "udp:", 4 ) &&
491 strncmp( p_current->psz_uri, "mms:", 4 ) &&
492 strncmp( p_current->psz_uri, "cdda:", 4 ) &&
493 strncmp( p_current->psz_uri, "dvd:", 4 ) &&
494 strncmp( p_current->psz_uri, "v4l:", 4 ) &&
495 strncmp( p_current->psz_uri, "dshow:", 6 ) )
497 b_preparsed = VLC_TRUE;
498 stats_TimerStart( p_playlist, "Preparse run",
499 STATS_TIMER_PREPARSE );
501 input_Preparse( p_playlist, p_current );
503 stats_TimerStop( p_playlist, STATS_TIMER_PREPARSE );
508 p_current->p_meta->i_status |= ITEM_PREPARSED;
509 var_SetInteger( p_playlist, "item-change", p_current->i_id );
513 /* If we haven't retrieved enough meta, add to secondary queue
514 * which will run the "meta fetchers"
516 * don't do this for things we won't get meta for, like
519 if( !input_MetaSatisfied( p_playlist, p_current, &i_m, &i_o,
523 p.p_item = p_current;
524 p.b_fetch_art = VLC_FALSE;
525 vlc_mutex_lock( &p_playlist->p_secondary_preparse->object_lock);
526 INSERT_ELEM( p_playlist->p_secondary_preparse->p_waiting,
527 p_playlist->p_secondary_preparse->i_waiting,
528 p_playlist->p_secondary_preparse->i_waiting,
531 &p_playlist->p_secondary_preparse->object_lock);
533 &p_playlist->p_secondary_preparse->object_wait );
536 vlc_gc_decref( p_current );
542 vlc_mutex_lock( &p_obj->object_lock );
543 i_activity = var_GetInteger( p_playlist, "activity" );
544 if( i_activity < 0 ) i_activity = 0;
545 vlc_mutex_unlock( &p_obj->object_lock );
546 /* Sleep at least 1ms */
547 msleep( (i_activity+1) * 1000 );
551 /** Main loop for secondary preparser queue */
552 void playlist_SecondaryPreparseLoop( playlist_secondary_preparse_t *p_obj )
554 playlist_t *p_playlist = (playlist_t *)p_obj->p_parent;
555 vlc_bool_t b_fetch_art;
556 input_item_t *p_item;
559 while( !p_playlist->b_die )
561 vlc_mutex_lock( &p_obj->object_lock );
562 while( p_obj->i_waiting == 0 )
564 vlc_cond_wait( &p_obj->object_wait, &p_obj->object_lock );
565 if( p_playlist->b_die )
567 vlc_mutex_unlock( &p_obj->object_lock );
572 b_fetch_art = p_obj->p_waiting->b_fetch_art;
573 p_item = p_obj->p_waiting->p_item;
574 REMOVE_ELEM( p_obj->p_waiting, p_obj->i_waiting, 0 );
575 vlc_mutex_unlock( &p_obj->object_lock );
580 input_MetaFetch( p_playlist, p_item );
581 p_item->p_meta->i_status |= ITEM_META_FETCHED;
582 var_SetInteger( p_playlist, "item-change", p_item->i_id );
583 /* Fetch right now */
584 if( var_GetInteger( p_playlist, "album-art" ) == ALBUM_ART_ALL )
586 vlc_mutex_lock( &p_obj->object_lock );
589 p.b_fetch_art = VLC_TRUE;
590 INSERT_ELEM( p_playlist->p_secondary_preparse->p_waiting,
591 p_playlist->p_secondary_preparse->i_waiting,
593 vlc_mutex_unlock( &p_obj->object_lock );
596 vlc_gc_decref( p_item );
600 input_ArtFetch( p_playlist, p_item );
601 p_item->p_meta->i_status |= ITEM_ART_FETCHED;
602 vlc_gc_decref( p_item );
605 vlc_mutex_lock( &p_obj->object_lock );
606 i_activity = var_GetInteger( p_playlist, "activity" );
607 if( i_activity < 0 ) i_activity = 0;
608 vlc_mutex_unlock( &p_obj->object_lock );
609 /* Sleep at least 1ms */
610 msleep( (i_activity+1) * 1000 );
614 static void VariablesInit( playlist_t *p_playlist )
617 /* These variables control updates */
618 var_Create( p_playlist, "intf-change", VLC_VAR_BOOL );
619 val.b_bool = VLC_TRUE;
620 var_Set( p_playlist, "intf-change", val );
622 var_Create( p_playlist, "item-change", VLC_VAR_INTEGER );
624 var_Set( p_playlist, "item-change", val );
626 var_Create( p_playlist, "item-deleted", VLC_VAR_INTEGER );
628 var_Set( p_playlist, "item-deleted", val );
630 var_Create( p_playlist, "item-append", VLC_VAR_ADDRESS );
632 var_Create( p_playlist, "playlist-current", VLC_VAR_INTEGER );
634 var_Set( p_playlist, "playlist-current", val );
636 var_Create( p_playlist, "intf-popupmenu", VLC_VAR_BOOL );
638 var_Create( p_playlist, "intf-show", VLC_VAR_BOOL );
639 val.b_bool = VLC_TRUE;
640 var_Set( p_playlist, "intf-show", val );
642 var_Create( p_playlist, "activity", VLC_VAR_INTEGER );
643 var_SetInteger( p_playlist, "activity", 0 );
645 /* Variables to control playback */
646 var_CreateGetBool( p_playlist, "play-and-stop" );
647 var_CreateGetBool( p_playlist, "play-and-exit" );
648 var_CreateGetBool( p_playlist, "random" );
649 var_CreateGetBool( p_playlist, "repeat" );
650 var_CreateGetBool( p_playlist, "loop" );
652 var_AddCallback( p_playlist, "random", RandomCallback, NULL );