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 );
40 * Create a playlist structure.
41 * \param p_parent the vlc object that is to be the parent of this playlist
42 * \return a pointer to the created playlist, or NULL on error
44 playlist_t * playlist_Create( vlc_object_t *p_parent )
46 playlist_t *p_playlist;
49 /* Allocate structure */
50 p_playlist = vlc_object_create( p_parent, VLC_OBJECT_PLAYLIST );
53 msg_Err( p_parent, "out of memory" );
56 p_parent->p_libvlc->p_playlist = p_playlist;
58 VariablesInit( p_playlist );
60 /* Initialise data structures */
61 vlc_mutex_init( p_playlist, &p_playlist->gc_lock );
62 p_playlist->i_last_playlist_id = 0;
63 p_playlist->i_last_input_id = 0;
64 p_playlist->p_input = NULL;
66 p_playlist->i_vout_destroyed_date = 0;
67 p_playlist->i_sout_destroyed_date = 0;
69 p_playlist->i_size = 0;
70 p_playlist->pp_items = NULL;
71 p_playlist->i_all_size = 0;
72 p_playlist->pp_all_items = NULL;
74 p_playlist->i_input_items = 0;
75 p_playlist->pp_input_items = NULL;
77 p_playlist->i_random = 0;
78 p_playlist->pp_random = NULL;
79 p_playlist->i_random_index = 0;
80 p_playlist->b_reset_random = VLC_TRUE;
82 i_tree = var_CreateGetBool( p_playlist, "playlist-tree" );
83 p_playlist->b_always_tree = (i_tree == 1);
84 p_playlist->b_never_tree = (i_tree == 2);
86 p_playlist->b_doing_ml = VLC_FALSE;
88 p_playlist->p_root_category = playlist_NodeCreate( p_playlist, NULL, NULL);
89 p_playlist->p_root_onelevel = playlist_NodeCreate( p_playlist, NULL, NULL);
91 /* Create playlist and media library */
92 p_playlist->p_local_category = playlist_NodeCreate( p_playlist,
93 _( "Playlist" ),p_playlist->p_root_category );
94 p_playlist->p_local_onelevel = playlist_NodeCreate( p_playlist,
95 _( "Playlist" ), p_playlist->p_root_onelevel );
96 p_playlist->p_local_category->i_flags |= PLAYLIST_RO_FLAG;
97 p_playlist->p_local_onelevel->i_flags |= PLAYLIST_RO_FLAG;
99 /* Link the nodes together. Todo: actually create them from the same input*/
100 p_playlist->p_local_onelevel->p_input->i_id =
101 p_playlist->p_local_category->p_input->i_id;
103 if( config_GetInt( p_playlist, "media-library") )
105 p_playlist->p_ml_category = playlist_NodeCreate( p_playlist,
106 _( "Media Library" ), p_playlist->p_root_category );
107 p_playlist->p_ml_onelevel = playlist_NodeCreate( p_playlist,
108 _( "Media Library" ), p_playlist->p_root_onelevel );
109 p_playlist->p_ml_category->i_flags |= PLAYLIST_RO_FLAG;
110 p_playlist->p_ml_onelevel->i_flags |= PLAYLIST_RO_FLAG;
111 p_playlist->p_ml_onelevel->p_input->i_id =
112 p_playlist->p_ml_category->p_input->i_id;
117 p_playlist->p_ml_category = p_playlist->p_ml_onelevel = NULL;
121 p_playlist->status.p_item = NULL;
122 p_playlist->status.p_node = p_playlist->p_local_onelevel;
123 p_playlist->request.b_request = VLC_FALSE;
124 p_playlist->status.i_status = PLAYLIST_STOPPED;
126 p_playlist->i_sort = SORT_ID;
127 p_playlist->i_order = ORDER_NORMAL;
129 vlc_object_attach( p_playlist, p_parent );
131 playlist_MLLoad( p_playlist );
135 void playlist_Destroy( playlist_t *p_playlist )
137 while( p_playlist->i_sds )
139 playlist_ServicesDiscoveryRemove( p_playlist,
140 p_playlist->pp_sds[0]->psz_module );
143 playlist_MLDump( p_playlist );
145 vlc_thread_join( p_playlist->p_preparse );
146 vlc_thread_join( p_playlist->p_secondary_preparse );
147 vlc_thread_join( p_playlist );
149 vlc_object_detach( p_playlist->p_preparse );
150 vlc_object_detach( p_playlist->p_secondary_preparse );
152 var_Destroy( p_playlist, "intf-change" );
153 var_Destroy( p_playlist, "item-change" );
154 var_Destroy( p_playlist, "playlist-current" );
155 var_Destroy( p_playlist, "intf-popmenu" );
156 var_Destroy( p_playlist, "intf-show" );
157 var_Destroy( p_playlist, "play-and-stop" );
158 var_Destroy( p_playlist, "play-and-exit" );
159 var_Destroy( p_playlist, "random" );
160 var_Destroy( p_playlist, "repeat" );
161 var_Destroy( p_playlist, "loop" );
162 var_Destroy( p_playlist, "activity" );
165 playlist_NodeDelete( p_playlist, p_playlist->p_root_category, VLC_TRUE,
167 playlist_NodeDelete( p_playlist, p_playlist->p_root_onelevel, VLC_TRUE,
171 if( p_playlist->p_stats )
172 free( p_playlist->p_stats );
174 vlc_mutex_destroy( &p_playlist->gc_lock );
175 vlc_object_destroy( p_playlist->p_preparse );
176 vlc_object_destroy( p_playlist->p_secondary_preparse );
177 vlc_object_detach( p_playlist );
178 vlc_object_destroy( p_playlist );
181 /* Destroy remaining objects */
182 static mtime_t ObjectGarbageCollector( playlist_t *p_playlist, int i_type,
183 mtime_t destroy_date )
187 if( destroy_date > mdate() ) return destroy_date;
189 if( destroy_date == 0 )
191 /* give a little time */
192 return mdate() + I64C(1000000);
196 vlc_mutex_lock( &p_playlist->gc_lock );
197 while( ( p_obj = vlc_object_find( p_playlist, i_type, FIND_CHILD ) ) )
199 if( p_obj->p_parent != (vlc_object_t*)p_playlist )
201 /* only first child (ie unused) */
202 vlc_object_release( p_obj );
205 if( i_type == VLC_OBJECT_VOUT )
207 msg_Dbg( p_playlist, "garbage collector destroying 1 vout" );
208 vlc_object_detach( p_obj );
209 vlc_object_release( p_obj );
210 vout_Destroy( (vout_thread_t *)p_obj );
212 else if( i_type == VLC_OBJECT_SOUT )
214 vlc_object_release( p_obj );
215 sout_DeleteInstance( (sout_instance_t*)p_obj );
218 vlc_mutex_unlock( &p_playlist->gc_lock );
223 /** Main loop for the playlist */
224 void playlist_MainLoop( playlist_t *p_playlist )
226 playlist_item_t *p_item = NULL;
227 vlc_bool_t b_playexit = var_GetBool( p_playlist, "play-and-exit" );
230 /* First, check if we have something to do */
231 /* FIXME : this can be called several times */
232 if( p_playlist->request.b_request )
234 /* Stop the existing input */
235 if( p_playlist->p_input && !p_playlist->p_input->b_die )
237 PL_DEBUG( "incoming request - stopping current input" );
238 input_StopThread( p_playlist->p_input );
242 /* If there is an input, check that it doesn't need to die. */
243 if( p_playlist->p_input )
245 /* This input is dead. Remove it ! */
246 if( p_playlist->p_input->b_dead )
249 input_thread_t *p_input;
250 PL_DEBUG( "dead input" );
252 p_input = p_playlist->p_input;
253 p_playlist->p_input = NULL;
255 /* Release the playlist lock, because we may get stuck
256 * in input_DestroyThread() for some time. */
260 input_DestroyThread( p_input );
262 /* Unlink current input
263 * (_after_ input_DestroyThread for vout garbage collector) */
264 vlc_object_detach( p_input );
267 vlc_object_destroy( p_input );
269 p_playlist->i_vout_destroyed_date = 0;
270 p_playlist->i_sout_destroyed_date = 0;
272 if( p_playlist->status.p_item->i_flags
273 & PLAYLIST_REMOVE_FLAG )
275 PL_DEBUG( "%s was marked for deletion, deleting",
276 PLI_NAME( p_playlist->status.p_item ) );
277 playlist_ItemDelete( p_playlist->status.p_item );
278 if( p_playlist->request.p_item == p_playlist->status.p_item )
279 p_playlist->request.p_item = NULL;
280 p_playlist->status.p_item = NULL;
283 i_activity= var_GetInteger( p_playlist, "activity") ;
284 var_SetInteger( p_playlist, "activity", i_activity -
285 DEFAULT_INPUT_ACTIVITY );
289 /* This input is dying, let it do */
290 else if( p_playlist->p_input->b_die )
292 PL_DEBUG( "dying input" );
294 /* This input has finished, ask it to die ! */
295 else if( p_playlist->p_input->b_error
296 || p_playlist->p_input->b_eof )
298 PL_DEBUG( "finished input" );
299 input_StopThread( p_playlist->p_input );
300 /* Select the next playlist item */
304 else if( p_playlist->p_input->i_state != INIT_S )
307 p_playlist->i_vout_destroyed_date =
308 ObjectGarbageCollector( p_playlist, VLC_OBJECT_VOUT,
309 p_playlist->i_vout_destroyed_date );
310 p_playlist->i_sout_destroyed_date =
311 ObjectGarbageCollector( p_playlist, VLC_OBJECT_SOUT,
312 p_playlist->i_sout_destroyed_date );
318 /* No input. Several cases
319 * - No request, running status -> start new item
320 * - No request, stopped status -> collect garbage
321 * - Request, running requested -> start new item
322 * - Request, stopped requested -> collect garbage
324 if( (!p_playlist->request.b_request &&
325 p_playlist->status.i_status != PLAYLIST_STOPPED) ||
326 ( p_playlist->request.b_request &&
327 p_playlist->request.i_status != PLAYLIST_STOPPED ) )
329 msg_Dbg( p_playlist, "starting new item" );
330 stats_TimerStart( p_playlist, "Playlist walk",
331 STATS_TIMER_PLAYLIST_WALK );
332 p_item = playlist_NextItem( p_playlist );
333 stats_TimerStop( p_playlist, STATS_TIMER_PLAYLIST_WALK );
337 msg_Dbg( p_playlist, "nothing to play" );
338 if( b_playexit == VLC_TRUE )
340 msg_Info( p_playlist, "end of playlist, exiting" );
341 p_playlist->p_libvlc->b_die = VLC_TRUE;
343 p_playlist->status.i_status = PLAYLIST_STOPPED;
347 playlist_PlayItem( p_playlist, p_item );
351 if( p_playlist->status.p_item &&
352 p_playlist->status.p_item->i_flags & PLAYLIST_REMOVE_FLAG )
354 PL_DEBUG( "deleting item marked for deletion" );
355 playlist_ItemDelete( p_playlist->status.p_item );
356 p_playlist->status.p_item = NULL;
359 /* Collect garbage */
361 p_playlist->i_sout_destroyed_date =
362 ObjectGarbageCollector( p_playlist, VLC_OBJECT_SOUT, mdate() );
363 p_playlist->i_vout_destroyed_date =
364 ObjectGarbageCollector( p_playlist, VLC_OBJECT_VOUT, mdate() );
371 /** Playlist dying last loop */
372 void playlist_LastLoop( playlist_t *p_playlist )
376 /* If there is an input, kill it */
381 if( p_playlist->p_input == NULL )
387 if( p_playlist->p_input->b_dead )
389 input_thread_t *p_input;
391 /* Unlink current input */
392 p_input = p_playlist->p_input;
393 p_playlist->p_input = NULL;
397 input_DestroyThread( p_input );
398 /* Unlink current input (_after_ input_DestroyThread for vout
399 * garbage collector)*/
400 vlc_object_detach( p_input );
403 vlc_object_destroy( p_input );
406 else if( p_playlist->p_input->b_die )
408 /* This input is dying, leave it alone */
411 else if( p_playlist->p_input->b_error || p_playlist->p_input->b_eof )
413 input_StopThread( p_playlist->p_input );
419 p_playlist->p_input->b_eof = 1;
424 msleep( INTF_IDLE_SLEEP );
427 /* close all remaining sout */
428 while( ( p_obj = vlc_object_find( p_playlist,
429 VLC_OBJECT_SOUT, FIND_CHILD ) ) )
431 vlc_object_release( p_obj );
432 sout_DeleteInstance( (sout_instance_t*)p_obj );
435 /* close all remaining vout */
436 while( ( p_obj = vlc_object_find( p_playlist,
437 VLC_OBJECT_VOUT, FIND_CHILD ) ) )
439 vlc_object_detach( p_obj );
440 vlc_object_release( p_obj );
441 vout_Destroy( (vout_thread_t *)p_obj );
445 /** Main loop for preparser queue */
446 void playlist_PreparseLoop( playlist_preparse_t *p_obj )
448 playlist_t *p_playlist = (playlist_t *)p_obj->p_parent;
451 vlc_mutex_lock( &p_obj->object_lock );
453 if( p_obj->i_waiting > 0 )
455 input_item_t *p_current = p_obj->pp_waiting[0];
456 REMOVE_ELEM( p_obj->pp_waiting, p_obj->i_waiting, 0 );
457 vlc_mutex_unlock( &p_obj->object_lock );
461 vlc_bool_t b_preparsed = VLC_FALSE;
462 if( strncmp( p_current->psz_uri, "http:", 5 ) &&
463 strncmp( p_current->psz_uri, "rtsp:", 5 ) &&
464 strncmp( p_current->psz_uri, "udp:", 4 ) &&
465 strncmp( p_current->psz_uri, "mms:", 4 ) &&
466 strncmp( p_current->psz_uri, "cdda:", 4 ) &&
467 strncmp( p_current->psz_uri, "dvd:", 4 ) &&
468 strncmp( p_current->psz_uri, "v4l:", 4 ) &&
469 strncmp( p_current->psz_uri, "dshow:", 6 ) )
471 b_preparsed = VLC_TRUE;
472 stats_TimerStart( p_playlist, "Preparse run",
473 STATS_TIMER_PREPARSE );
475 input_Preparse( p_playlist, p_current );
477 stats_TimerStop( p_playlist, STATS_TIMER_PREPARSE );
482 p_current->p_meta->i_status |= ITEM_PREPARSED;
483 var_SetInteger( p_playlist, "item-change", p_current->i_id );
485 vlc_gc_decref( p_current );
486 /* Add to secondary preparse queue */
488 vlc_mutex_lock( &p_playlist->p_secondary_preparse->object_lock );
489 INSERT_ELEM( p_playlist->p_secondary_preparse->pp_waiting,
490 p_playlist->p_secondary_preparse->i_waiting,
491 p_playlist->p_secondary_preparse->i_waiting,
493 vlc_gc_incref( p_current );
494 vlc_mutex_unlock( &p_playlist->p_secondary_preparse->object_lock );
499 vlc_mutex_unlock( &p_playlist->object_lock );
501 vlc_mutex_lock( &p_obj->object_lock );
502 i_activity = var_GetInteger( p_playlist, "activity" );
503 if( i_activity < 0 ) i_activity = 0;
504 vlc_mutex_unlock( &p_obj->object_lock );
505 msleep( (i_activity+1) * 1000 );
508 vlc_mutex_unlock( &p_obj->object_lock );
511 /** Main loop for secondary preparser queue */
512 void playlist_SecondaryPreparseLoop( playlist_preparse_t *p_obj )
514 playlist_t *p_playlist = (playlist_t *)p_obj->p_parent;
516 vlc_mutex_lock( &p_obj->object_lock );
518 if( p_obj->i_waiting > 0 )
520 input_item_t *p_current = p_obj->pp_waiting[0];
521 REMOVE_ELEM( p_obj->pp_waiting, p_obj->i_waiting, 0 );
522 vlc_mutex_unlock( &p_obj->object_lock );
525 input_SecondaryPreparse( p_playlist, p_current );
526 var_SetInteger( p_playlist, "item-change",
528 vlc_gc_decref( p_current );
532 vlc_mutex_unlock( &p_playlist->object_lock );
536 vlc_mutex_unlock( &p_obj->object_lock );
539 static void VariablesInit( playlist_t *p_playlist )
542 /* These variables control updates */
543 var_Create( p_playlist, "intf-change", VLC_VAR_BOOL );
544 val.b_bool = VLC_TRUE;
545 var_Set( p_playlist, "intf-change", val );
547 var_Create( p_playlist, "item-change", VLC_VAR_INTEGER );
549 var_Set( p_playlist, "item-change", val );
551 var_Create( p_playlist, "item-deleted", VLC_VAR_INTEGER );
553 var_Set( p_playlist, "item-deleted", val );
555 var_Create( p_playlist, "item-append", VLC_VAR_ADDRESS );
557 var_Create( p_playlist, "playlist-current", VLC_VAR_INTEGER );
559 var_Set( p_playlist, "playlist-current", val );
561 var_Create( p_playlist, "intf-popupmenu", VLC_VAR_BOOL );
563 var_Create( p_playlist, "intf-show", VLC_VAR_BOOL );
564 val.b_bool = VLC_TRUE;
565 var_Set( p_playlist, "intf-show", val );
567 var_Create( p_playlist, "activity", VLC_VAR_INTEGER );
568 var_SetInteger( p_playlist, "activity", 0 );
570 /* Variables to control playback */
571 var_CreateGetBool( p_playlist, "play-and-stop" );
572 var_CreateGetBool( p_playlist, "play-and-exit" );
573 var_CreateGetBool( p_playlist, "random" );
574 var_CreateGetBool( p_playlist, "repeat" );
575 var_CreateGetBool( p_playlist, "loop" );