1 /*****************************************************************************
2 * thread.c : Playlist management functions
3 *****************************************************************************
4 * Copyright © 1999-2008 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 *****************************************************************************/
30 #include <vlc_common.h>
32 #include <vlc_input.h>
33 #include <vlc_interface.h>
34 #include <vlc_playlist.h>
35 #include "stream_output/stream_output.h"
36 #include "playlist_internal.h"
38 /*****************************************************************************
40 *****************************************************************************/
41 static void *Thread ( void * );
43 /*****************************************************************************
44 * Main functions for the global thread
45 *****************************************************************************/
48 * Create the main playlist threads.
49 * Additionally to the playlist, this thread controls :
53 * \return an object with a started thread
55 void playlist_Activate( playlist_t *p_playlist )
58 playlist_private_t *p_sys = pl_priv(p_playlist);
61 p_sys->p_fetcher = playlist_fetcher_New( p_playlist );
62 if( !p_sys->p_fetcher )
63 msg_Err( p_playlist, "cannot create playlist fetcher" );
66 p_sys->p_preparser = playlist_preparser_New( p_playlist, p_sys->p_fetcher );
67 if( !p_sys->p_preparser )
68 msg_Err( p_playlist, "cannot create playlist preparser" );
70 /* Start the playlist thread */
71 if( vlc_clone( &p_sys->thread, Thread, p_playlist,
72 VLC_THREAD_PRIORITY_LOW ) )
74 msg_Err( p_playlist, "cannot spawn playlist thread" );
76 msg_Dbg( p_playlist, "Activated" );
79 void playlist_Deactivate( playlist_t *p_playlist )
82 playlist_private_t *p_sys = pl_priv(p_playlist);
84 msg_Dbg( p_playlist, "Deactivate" );
86 vlc_object_kill( p_playlist );
88 vlc_cond_signal( &p_sys->signal );
91 vlc_join( p_sys->thread, NULL );
92 assert( !p_sys->p_input );
95 playlist_preparser_t *p_preparser = p_sys->p_preparser;
96 playlist_fetcher_t *p_fetcher = p_sys->p_fetcher;
98 p_sys->p_preparser = NULL;
99 p_sys->p_fetcher = NULL;
103 playlist_preparser_Delete( p_preparser );
105 playlist_fetcher_Delete( p_fetcher );
107 /* release input resources */
108 if( p_sys->p_input_resource )
109 input_resource_Delete( p_sys->p_input_resource );
110 p_sys->p_input_resource = NULL;
113 playlist_MLDump( p_playlist );
117 /* Release the current node */
118 set_current_status_node( p_playlist, NULL );
120 /* Release the current item */
121 set_current_status_item( p_playlist, NULL );
125 msg_Dbg( p_playlist, "Deactivated" );
131 static int InputEvent( vlc_object_t *p_this, char const *psz_cmd,
132 vlc_value_t oldval, vlc_value_t newval, void *p_data )
134 VLC_UNUSED(p_this); VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
135 playlist_t *p_playlist = p_data;
137 if( newval.i_int != INPUT_EVENT_STATE &&
138 newval.i_int != INPUT_EVENT_DEAD )
143 /* XXX: signaling while not changing any parameter... suspicious... */
144 vlc_cond_signal( &pl_priv(p_playlist)->signal );
150 static void UpdateActivity( playlist_t *p_playlist, int i_delta )
154 const int i_activity = var_GetInteger( p_playlist, "activity" ) ;
155 var_SetInteger( p_playlist, "activity", i_activity + i_delta );
159 * Synchronise the current index of the playlist
160 * to match the index of the current item.
162 * \param p_playlist the playlist structure
163 * \param p_cur the current playlist item
166 static void ResyncCurrentIndex( playlist_t *p_playlist, playlist_item_t *p_cur )
170 PL_DEBUG( "resyncing on %s", PLI_NAME( p_cur ) );
171 /* Simply resync index */
173 p_playlist->i_current_index = -1;
174 for( i = 0 ; i< p_playlist->current.i_size; i++ )
176 if( ARRAY_VAL( p_playlist->current, i ) == p_cur )
178 p_playlist->i_current_index = i;
182 PL_DEBUG( "%s is at %i", PLI_NAME( p_cur ), p_playlist->i_current_index );
185 static void ResetCurrentlyPlaying( playlist_t *p_playlist,
186 playlist_item_t *p_cur )
188 playlist_private_t *p_sys = pl_priv(p_playlist);
190 stats_TimerStart( p_playlist, "Items array build",
191 STATS_TIMER_PLAYLIST_BUILD );
192 PL_DEBUG( "rebuilding array of current - root %s",
193 PLI_NAME( p_sys->status.p_node ) );
194 ARRAY_RESET( p_playlist->current );
195 p_playlist->i_current_index = -1;
196 for( playlist_item_t *p_next = NULL; ; )
198 /** FIXME: this is *slow* */
199 p_next = playlist_GetNextLeaf( p_playlist,
200 p_sys->status.p_node,
201 p_next, true, false );
205 if( p_next == p_cur )
206 p_playlist->i_current_index = p_playlist->current.i_size;
207 ARRAY_APPEND( p_playlist->current, p_next);
209 PL_DEBUG("rebuild done - %i items, index %i", p_playlist->current.i_size,
210 p_playlist->i_current_index);
212 if( var_GetBool( p_playlist, "random" ) )
214 /* Shuffle the array */
215 srand( (unsigned int)mdate() );
216 for( int j = p_playlist->current.i_size - 1; j > 0; j-- )
218 int i = rand() % (j+1); /* between 0 and j */
219 playlist_item_t *p_tmp;
220 /* swap the two items */
221 p_tmp = ARRAY_VAL(p_playlist->current, i);
222 ARRAY_VAL(p_playlist->current,i) = ARRAY_VAL(p_playlist->current,j);
223 ARRAY_VAL(p_playlist->current,j) = p_tmp;
226 p_sys->b_reset_currently_playing = false;
227 stats_TimerStop( p_playlist, STATS_TIMER_PLAYLIST_BUILD );
232 * Start the input for an item
234 * \param p_playlist the playlist object
235 * \param p_item the item to play
238 static int PlayItem( playlist_t *p_playlist, playlist_item_t *p_item )
240 playlist_private_t *p_sys = pl_priv(p_playlist);
241 input_item_t *p_input = p_item->p_input;
245 msg_Dbg( p_playlist, "creating new input thread" );
247 p_input->i_nb_played++;
248 set_current_status_item( p_playlist, p_item );
250 p_sys->status.i_status = PLAYLIST_RUNNING;
252 UpdateActivity( p_playlist, DEFAULT_INPUT_ACTIVITY );
254 assert( p_sys->p_input == NULL );
256 input_thread_t *p_input_thread = input_Create( p_playlist, p_input, NULL, p_sys->p_input_resource );
259 p_sys->p_input = p_input_thread;
260 var_AddCallback( p_input_thread, "intf-event", InputEvent, p_playlist );
262 if( input_Start( p_sys->p_input ) )
264 vlc_object_release( p_input_thread );
265 p_sys->p_input = p_input_thread = NULL;
269 p_sys->p_input_resource = NULL;
271 char *psz_uri = input_item_GetURI( p_item->p_input );
272 if( psz_uri && ( !strncmp( psz_uri, "directory:", 10 ) ||
273 !strncmp( psz_uri, "vlc:", 4 ) ) )
280 /* TODO store art policy in playlist private data */
281 if( var_GetInteger( p_playlist, "album-art" ) == ALBUM_ART_WHEN_PLAYED )
285 char *psz_arturl, *psz_name;
286 psz_arturl = input_item_GetArtURL( p_input );
287 psz_name = input_item_GetName( p_input );
289 /* p_input->p_meta should not be null after a successfull CreateThread */
290 b_has_art = !EMPTY_STR( psz_arturl );
292 if( !b_has_art || strncmp( psz_arturl, "attachment://", 13 ) )
294 PL_DEBUG( "requesting art for %s", psz_name );
295 playlist_AskForArtEnqueue( p_playlist, p_input, pl_Locked );
302 var_SetAddress( p_playlist, "item-current", p_input );
309 * Compute the next playlist item depending on
310 * the playlist course mode (forward, backward, random, view,...).
312 * \param p_playlist the playlist object
315 static playlist_item_t *NextItem( playlist_t *p_playlist )
317 playlist_private_t *p_sys = pl_priv(p_playlist);
318 playlist_item_t *p_new = NULL;
320 /* Handle quickly a few special cases */
321 /* No items to play */
322 if( p_playlist->items.i_size == 0 )
324 msg_Info( p_playlist, "playlist is empty" );
328 /* Start the real work */
329 if( p_sys->request.b_request )
331 p_new = p_sys->request.p_item;
332 int i_skip = p_sys->request.i_skip;
333 PL_DEBUG( "processing request item %s node %s skip %i",
334 PLI_NAME( p_sys->request.p_item ),
335 PLI_NAME( p_sys->request.p_node ), i_skip );
337 if( p_sys->request.p_node &&
338 p_sys->request.p_node != get_current_status_node( p_playlist ) )
341 set_current_status_node( p_playlist, p_sys->request.p_node );
342 p_sys->request.p_node = NULL;
343 p_sys->b_reset_currently_playing = true;
346 /* If we are asked for a node, go to it's first child */
347 if( i_skip == 0 && ( p_new == NULL || p_new->i_children != -1 ) )
352 p_new = playlist_GetNextLeaf( p_playlist, p_new, NULL, true, false );
353 for( int i = 0; i < p_playlist->current.i_size; i++ )
355 if( p_new == ARRAY_VAL( p_playlist->current, i ) )
357 p_playlist->i_current_index = i;
364 if( p_sys->b_reset_currently_playing )
365 /* A bit too bad to reset twice ... */
366 ResetCurrentlyPlaying( p_playlist, p_new );
368 ResyncCurrentIndex( p_playlist, p_new );
370 p_playlist->i_current_index = -1;
372 if( p_playlist->current.i_size && (i_skip > 0) )
374 if( p_playlist->i_current_index < -1 )
375 p_playlist->i_current_index = -1;
376 for( int i = i_skip; i > 0 ; i-- )
378 p_playlist->i_current_index++;
379 if( p_playlist->i_current_index >= p_playlist->current.i_size )
381 PL_DEBUG( "looping - restarting at beginning of node" );
382 p_playlist->i_current_index = 0;
385 p_new = ARRAY_VAL( p_playlist->current,
386 p_playlist->i_current_index );
388 else if( p_playlist->current.i_size && (i_skip < 0) )
390 for( int i = i_skip; i < 0 ; i++ )
392 p_playlist->i_current_index--;
393 if( p_playlist->i_current_index <= -1 )
395 PL_DEBUG( "looping - restarting at end of node" );
396 p_playlist->i_current_index = p_playlist->current.i_size-1;
399 p_new = ARRAY_VAL( p_playlist->current,
400 p_playlist->i_current_index );
402 /* Clear the request */
403 p_sys->request.b_request = false;
405 /* "Automatic" item change ( next ) */
408 bool b_loop = var_GetBool( p_playlist, "loop" );
409 bool b_repeat = var_GetBool( p_playlist, "repeat" );
410 bool b_playstop = var_GetBool( p_playlist, "play-and-stop" );
412 /* Repeat and play/stop */
413 if( b_repeat && get_current_status_item( p_playlist ) )
415 msg_Dbg( p_playlist,"repeating item" );
416 return get_current_status_item( p_playlist );
420 msg_Dbg( p_playlist,"stopping (play and stop)" );
425 if( get_current_status_item( p_playlist ) )
427 playlist_item_t *p_parent = get_current_status_item( p_playlist );
430 if( p_parent->i_flags & PLAYLIST_SKIP_FLAG )
432 msg_Dbg( p_playlist, "blocking item, stopping") ;
435 p_parent = p_parent->p_parent;
439 PL_DEBUG( "changing item without a request (current %i/%i)",
440 p_playlist->i_current_index, p_playlist->current.i_size );
441 /* Cant go to next from current item */
442 if( get_current_status_item( p_playlist ) &&
443 get_current_status_item( p_playlist )->i_flags & PLAYLIST_SKIP_FLAG )
446 if( p_sys->b_reset_currently_playing )
447 ResetCurrentlyPlaying( p_playlist,
448 get_current_status_item( p_playlist ) );
450 p_playlist->i_current_index++;
451 assert( p_playlist->i_current_index <= p_playlist->current.i_size );
452 if( p_playlist->i_current_index == p_playlist->current.i_size )
454 if( !b_loop || p_playlist->current.i_size == 0 )
456 p_playlist->i_current_index = 0;
458 PL_DEBUG( "using item %i", p_playlist->i_current_index );
459 if ( p_playlist->current.i_size == 0 )
462 p_new = ARRAY_VAL( p_playlist->current, p_playlist->i_current_index );
463 /* The new item can't be autoselected */
464 if( p_new != NULL && p_new->i_flags & PLAYLIST_SKIP_FLAG )
470 static int LoopInput( playlist_t *p_playlist )
472 playlist_private_t *p_sys = pl_priv(p_playlist);
473 input_thread_t *p_input = p_sys->p_input;
478 if( ( p_sys->request.b_request || !vlc_object_alive( p_playlist ) ) && !p_input->b_die )
480 PL_DEBUG( "incoming request - stopping current input" );
481 input_Stop( p_input, true );
484 /* This input is dead. Remove it ! */
485 if( p_input->b_dead )
487 PL_DEBUG( "dead input" );
489 assert( p_sys->p_input_resource == NULL );
491 p_sys->p_input_resource = input_DetachResource( p_input );
494 /* We can unlock as we return VLC_EGENERIC (no event will be lost) */
496 /* input_resource_t must be manipulated without playlist lock */
497 if( !var_CreateGetBool( p_input, "sout-keep" ) )
498 input_resource_TerminateSout( p_sys->p_input_resource );
500 /* The DelCallback must be issued without playlist lock */
501 var_DelCallback( p_input, "intf-event", InputEvent, p_playlist );
505 p_sys->p_input = NULL;
506 vlc_thread_join( p_input );
507 vlc_object_release( p_input );
509 UpdateActivity( p_playlist, -DEFAULT_INPUT_ACTIVITY );
513 /* This input is dying, let it do */
514 else if( p_input->b_die )
516 PL_DEBUG( "dying input" );
518 /* This input has finished, ask it to die ! */
519 else if( p_input->b_error || p_input->b_eof )
521 PL_DEBUG( "finished input" );
522 input_Stop( p_input, false );
527 static void LoopRequest( playlist_t *p_playlist )
529 playlist_private_t *p_sys = pl_priv(p_playlist);
530 assert( !p_sys->p_input );
532 /* No input. Several cases
533 * - No request, running status -> start new item
534 * - No request, stopped status -> collect garbage
535 * - Request, running requested -> start new item
536 * - Request, stopped requested -> collect garbage
538 const int i_status = p_sys->request.b_request ?
539 p_sys->request.i_status : p_sys->status.i_status;
541 if( i_status == PLAYLIST_STOPPED || !vlc_object_alive( p_playlist ) )
543 p_sys->status.i_status = PLAYLIST_STOPPED;
545 if( p_sys->p_input_resource &&
546 input_resource_HasVout( p_sys->p_input_resource ) )
548 /* XXX We can unlock if we don't issue the wait as we will be
549 * call again without anything else done between the calls */
552 /* input_resource_t must be manipulated without playlist lock */
553 input_resource_TerminateVout( p_sys->p_input_resource );
559 if( vlc_object_alive( p_playlist ) )
560 vlc_cond_wait( &p_sys->signal, &p_sys->lock );
565 playlist_item_t *p_item = NextItem( p_playlist );
568 msg_Dbg( p_playlist, "starting new item" );
569 PlayItem( p_playlist, p_item );
573 msg_Dbg( p_playlist, "nothing to play" );
574 p_sys->status.i_status = PLAYLIST_STOPPED;
576 if( var_GetBool( p_playlist, "play-and-exit" ) )
578 msg_Info( p_playlist, "end of playlist, exiting" );
579 libvlc_Quit( p_playlist->p_libvlc );
584 * Run the main control thread itself
586 static void *Thread ( void *data )
588 playlist_t *p_playlist = data;
589 playlist_private_t *p_sys = pl_priv(p_playlist);
591 playlist_Lock( p_playlist );
592 while( vlc_object_alive( p_playlist ) || p_sys->p_input )
594 /* FIXME: what's that ! */
595 if( p_sys->b_reset_currently_playing &&
596 mdate() - p_sys->last_rebuild_date > 30000 ) // 30 ms
598 ResetCurrentlyPlaying( p_playlist,
599 get_current_status_item( p_playlist ) );
600 p_sys->last_rebuild_date = mdate();
603 /* If there is an input, check that it doesn't need to die. */
604 while( !LoopInput( p_playlist ) )
605 vlc_cond_wait( &p_sys->signal, &p_sys->lock );
607 LoopRequest( p_playlist );
609 playlist_Unlock( p_playlist );