1 /*****************************************************************************
2 * thread.c : Playlist management functions
3 *****************************************************************************
4 * Copyright © 1999-2008 VLC authors and VideoLAN
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 it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * 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>
36 #include "stream_output/stream_output.h"
37 #include "playlist_internal.h"
39 /*****************************************************************************
41 *****************************************************************************/
42 static void *Thread ( void * );
44 /*****************************************************************************
45 * Main functions for the global thread
46 *****************************************************************************/
49 * Create the main playlist threads.
50 * Additionally to the playlist, this thread controls :
54 * \return an object with a started thread
56 void playlist_Activate( playlist_t *p_playlist )
59 playlist_private_t *p_sys = pl_priv(p_playlist);
61 p_sys->p_input_resource = input_resource_New( VLC_OBJECT( p_playlist ) );
62 if( unlikely(p_sys->p_input_resource == NULL) )
65 /* Start the playlist thread */
66 if( vlc_clone( &p_sys->thread, Thread, p_playlist,
67 VLC_THREAD_PRIORITY_LOW ) )
69 msg_Err( p_playlist, "cannot spawn playlist thread" );
71 msg_Dbg( p_playlist, "playlist threads correctly activated" );
74 void playlist_Deactivate( playlist_t *p_playlist )
77 playlist_private_t *p_sys = pl_priv(p_playlist);
79 msg_Dbg( p_playlist, "deactivating the playlist" );
83 vlc_cond_signal( &p_sys->signal );
86 vlc_join( p_sys->thread, NULL );
87 assert( !p_sys->p_input );
89 /* release input resources */
90 input_resource_Release( p_sys->p_input_resource );
92 if( var_InheritBool( p_playlist, "media-library" ) )
93 playlist_MLDump( p_playlist );
97 /* Release the current node */
98 set_current_status_node( p_playlist, NULL );
100 /* Release the current item */
101 set_current_status_item( p_playlist, NULL );
105 msg_Dbg( p_playlist, "playlist correctly deactivated" );
111 static int InputEvent( vlc_object_t *p_this, char const *psz_cmd,
112 vlc_value_t oldval, vlc_value_t newval, void *p_data )
114 VLC_UNUSED(p_this); VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
115 playlist_t *p_playlist = p_data;
117 if( newval.i_int != INPUT_EVENT_STATE &&
118 newval.i_int != INPUT_EVENT_DEAD )
123 /* XXX: signaling while not changing any parameter... suspicious... */
124 vlc_cond_signal( &pl_priv(p_playlist)->signal );
131 * Synchronise the current index of the playlist
132 * to match the index of the current item.
134 * \param p_playlist the playlist structure
135 * \param p_cur the current playlist item
138 void ResyncCurrentIndex( playlist_t *p_playlist, playlist_item_t *p_cur )
142 PL_DEBUG( "resyncing on %s", PLI_NAME( p_cur ) );
143 /* Simply resync index */
145 p_playlist->i_current_index = -1;
146 for( i = 0 ; i< p_playlist->current.i_size; i++ )
148 if( ARRAY_VAL( p_playlist->current, i ) == p_cur )
150 p_playlist->i_current_index = i;
154 PL_DEBUG( "%s is at %i", PLI_NAME( p_cur ), p_playlist->i_current_index );
158 * Reset the currently playing playlist.
160 * \param p_playlist the playlist structure
161 * \param p_cur the current playlist item
164 void ResetCurrentlyPlaying( playlist_t *p_playlist,
165 playlist_item_t *p_cur )
167 playlist_private_t *p_sys = pl_priv(p_playlist);
169 PL_DEBUG( "rebuilding array of current - root %s",
170 PLI_NAME( p_sys->status.p_node ) );
171 ARRAY_RESET( p_playlist->current );
172 p_playlist->i_current_index = -1;
173 for( playlist_item_t *p_next = NULL; ; )
175 /** FIXME: this is *slow* */
176 p_next = playlist_GetNextLeaf( p_playlist,
177 p_sys->status.p_node,
178 p_next, true, false );
182 if( p_next == p_cur )
183 p_playlist->i_current_index = p_playlist->current.i_size;
184 ARRAY_APPEND( p_playlist->current, p_next);
186 PL_DEBUG("rebuild done - %i items, index %i", p_playlist->current.i_size,
187 p_playlist->i_current_index);
189 if( var_GetBool( p_playlist, "random" ) && ( p_playlist->current.i_size > 0 ) )
191 /* Shuffle the array */
192 for( unsigned j = p_playlist->current.i_size - 1; j > 0; j-- )
194 unsigned i = vlc_lrand48() % (j+1); /* between 0 and j */
195 playlist_item_t *p_tmp;
196 /* swap the two items */
197 p_tmp = ARRAY_VAL(p_playlist->current, i);
198 ARRAY_VAL(p_playlist->current,i) = ARRAY_VAL(p_playlist->current,j);
199 ARRAY_VAL(p_playlist->current,j) = p_tmp;
202 p_sys->b_reset_currently_playing = false;
207 * Start the input for an item
209 * \param p_playlist the playlist object
210 * \param p_item the item to play
213 static int PlayItem( playlist_t *p_playlist, playlist_item_t *p_item )
215 playlist_private_t *p_sys = pl_priv(p_playlist);
216 input_item_t *p_input = p_item->p_input;
220 msg_Dbg( p_playlist, "creating new input thread" );
222 p_input->i_nb_played++;
223 set_current_status_item( p_playlist, p_item );
225 p_sys->status.i_status = PLAYLIST_RUNNING;
227 var_TriggerCallback( p_playlist, "activity" );
229 assert( p_sys->p_input == NULL );
231 input_thread_t *p_input_thread = input_Create( p_playlist, p_input, NULL, p_sys->p_input_resource );
234 p_sys->p_input = p_input_thread;
235 var_AddCallback( p_input_thread, "intf-event", InputEvent, p_playlist );
237 var_SetAddress( p_playlist, "input-current", p_input_thread );
239 if( input_Start( p_sys->p_input ) )
241 vlc_object_release( p_input_thread );
242 p_sys->p_input = p_input_thread = NULL;
246 char *psz_uri = input_item_GetURI( p_item->p_input );
247 if( psz_uri && ( !strncmp( psz_uri, "directory:", 10 ) ||
248 !strncmp( psz_uri, "vlc:", 4 ) ) )
255 /* TODO store art policy in playlist private data */
256 if( var_GetInteger( p_playlist, "album-art" ) == ALBUM_ART_WHEN_PLAYED )
260 char *psz_arturl, *psz_name;
261 psz_arturl = input_item_GetArtURL( p_input );
262 psz_name = input_item_GetName( p_input );
264 /* p_input->p_meta should not be null after a successful CreateThread */
265 b_has_art = !EMPTY_STR( psz_arturl );
267 if( !b_has_art || strncmp( psz_arturl, "attachment://", 13 ) )
269 PL_DEBUG( "requesting art for %s", psz_name );
270 playlist_AskForArtEnqueue( p_playlist, p_input );
275 /* FIXME: this is not safe !!*/
277 var_SetAddress( p_playlist, "item-current", p_input );
284 * Compute the next playlist item depending on
285 * the playlist course mode (forward, backward, random, view,...).
287 * \param p_playlist the playlist object
290 static playlist_item_t *NextItem( playlist_t *p_playlist )
292 playlist_private_t *p_sys = pl_priv(p_playlist);
293 playlist_item_t *p_new = NULL;
295 /* Handle quickly a few special cases */
296 /* No items to play */
297 if( p_playlist->items.i_size == 0 )
299 msg_Info( p_playlist, "playlist is empty" );
303 /* Start the real work */
304 if( p_sys->request.b_request )
306 p_new = p_sys->request.p_item;
307 int i_skip = p_sys->request.i_skip;
308 PL_DEBUG( "processing request item: %s, node: %s, skip: %i",
309 PLI_NAME( p_sys->request.p_item ),
310 PLI_NAME( p_sys->request.p_node ), i_skip );
312 if( p_sys->request.p_node &&
313 p_sys->request.p_node != get_current_status_node( p_playlist ) )
316 set_current_status_node( p_playlist, p_sys->request.p_node );
317 p_sys->request.p_node = NULL;
318 p_sys->b_reset_currently_playing = true;
321 /* If we are asked for a node, go to it's first child */
322 if( i_skip == 0 && ( p_new == NULL || p_new->i_children != -1 ) )
327 p_new = playlist_GetNextLeaf( p_playlist, p_new, NULL, true, false );
328 for( int i = 0; i < p_playlist->current.i_size; i++ )
330 if( p_new == ARRAY_VAL( p_playlist->current, i ) )
332 p_playlist->i_current_index = i;
339 if( p_sys->b_reset_currently_playing )
340 /* A bit too bad to reset twice ... */
341 ResetCurrentlyPlaying( p_playlist, p_new );
343 ResyncCurrentIndex( p_playlist, p_new );
345 p_playlist->i_current_index = -1;
347 if( p_playlist->current.i_size && (i_skip > 0) )
349 if( p_playlist->i_current_index < -1 )
350 p_playlist->i_current_index = -1;
351 for( int i = i_skip; i > 0 ; i-- )
353 p_playlist->i_current_index++;
354 if( p_playlist->i_current_index >= p_playlist->current.i_size )
356 PL_DEBUG( "looping - restarting at beginning of node" );
357 p_playlist->i_current_index = 0;
360 p_new = ARRAY_VAL( p_playlist->current,
361 p_playlist->i_current_index );
363 else if( p_playlist->current.i_size && (i_skip < 0) )
365 for( int i = i_skip; i < 0 ; i++ )
367 p_playlist->i_current_index--;
368 if( p_playlist->i_current_index <= -1 )
370 PL_DEBUG( "looping - restarting at end of node" );
371 p_playlist->i_current_index = p_playlist->current.i_size-1;
374 p_new = ARRAY_VAL( p_playlist->current,
375 p_playlist->i_current_index );
377 /* Clear the request */
378 p_sys->request.b_request = false;
380 /* "Automatic" item change ( next ) */
383 bool b_loop = var_GetBool( p_playlist, "loop" );
384 bool b_repeat = var_GetBool( p_playlist, "repeat" );
385 bool b_playstop = var_GetBool( p_playlist, "play-and-stop" );
387 /* Repeat and play/stop */
388 if( b_repeat && get_current_status_item( p_playlist ) )
390 msg_Dbg( p_playlist,"repeating item" );
391 return get_current_status_item( p_playlist );
395 msg_Dbg( p_playlist,"stopping (play and stop)" );
400 if( get_current_status_item( p_playlist ) )
402 playlist_item_t *p_parent = get_current_status_item( p_playlist );
405 if( p_parent->i_flags & PLAYLIST_SKIP_FLAG )
407 msg_Dbg( p_playlist, "blocking item, stopping") ;
410 p_parent = p_parent->p_parent;
414 PL_DEBUG( "changing item without a request (current %i/%i)",
415 p_playlist->i_current_index, p_playlist->current.i_size );
416 /* Cant go to next from current item */
417 if( get_current_status_item( p_playlist ) &&
418 get_current_status_item( p_playlist )->i_flags & PLAYLIST_SKIP_FLAG )
421 if( p_sys->b_reset_currently_playing )
422 ResetCurrentlyPlaying( p_playlist,
423 get_current_status_item( p_playlist ) );
425 p_playlist->i_current_index++;
426 assert( p_playlist->i_current_index <= p_playlist->current.i_size );
427 if( p_playlist->i_current_index == p_playlist->current.i_size )
429 if( !b_loop || p_playlist->current.i_size == 0 )
431 p_playlist->i_current_index = 0;
433 PL_DEBUG( "using item %i", p_playlist->i_current_index );
434 if ( p_playlist->current.i_size == 0 )
437 p_new = ARRAY_VAL( p_playlist->current, p_playlist->i_current_index );
438 /* The new item can't be autoselected */
439 if( p_new != NULL && p_new->i_flags & PLAYLIST_SKIP_FLAG )
445 static void LoopInput( playlist_t *p_playlist )
447 playlist_private_t *p_sys = pl_priv(p_playlist);
448 input_thread_t *p_input = p_sys->p_input;
450 assert( p_input != NULL );
452 if( ( p_sys->request.b_request || p_sys->killed ) && vlc_object_alive(p_input) )
454 PL_DEBUG( "incoming request - stopping current input" );
455 input_Stop( p_input, true );
458 /* This input is dead. Remove it ! */
459 if( p_input->b_dead )
461 p_sys->p_input = NULL;
462 PL_DEBUG( "dead input" );
465 /* WARNING: Input resource manipulation and callback deletion are
466 * incompatible with the playlist lock. */
467 if( !var_InheritBool( p_input, "sout-keep" ) )
468 input_resource_TerminateSout( p_sys->p_input_resource );
469 var_DelCallback( p_input, "intf-event", InputEvent, p_playlist );
471 input_Close( p_input );
473 var_TriggerCallback( p_playlist, "activity" );
476 /* This input is dying, let it do */
477 else if( !vlc_object_alive(p_input) )
479 PL_DEBUG( "dying input" );
481 /* This input has finished, ask it to die ! */
482 else if( p_input->b_error || p_input->b_eof )
484 PL_DEBUG( "finished input" );
485 input_Stop( p_input, false );
488 vlc_cond_wait( &p_sys->signal, &p_sys->lock );
491 static void LoopRequest( playlist_t *p_playlist )
493 playlist_private_t *p_sys = pl_priv(p_playlist);
494 assert( !p_sys->p_input );
496 /* No input. Several cases
497 * - No request, running status -> start new item
498 * - No request, stopped status -> collect garbage
499 * - Request, running requested -> start new item
500 * - Request, stopped requested -> collect garbage
502 const int i_status = p_sys->request.b_request ?
503 p_sys->request.i_status : p_sys->status.i_status;
505 if( i_status == PLAYLIST_STOPPED )
507 p_sys->status.i_status = PLAYLIST_STOPPED;
508 vlc_cond_wait( &p_sys->signal, &p_sys->lock );
512 playlist_item_t *p_item = NextItem( p_playlist );
515 msg_Dbg( p_playlist, "starting playback of the new playlist item" );
516 ResyncCurrentIndex( p_playlist, p_item );
517 PlayItem( p_playlist, p_item );
521 msg_Dbg( p_playlist, "nothing to play" );
522 p_sys->status.i_status = PLAYLIST_STOPPED;
524 if( var_GetBool( p_playlist, "play-and-exit" ) )
526 msg_Info( p_playlist, "end of playlist, exiting" );
527 libvlc_Quit( p_playlist->p_libvlc );
532 * Run the main control thread itself
534 static void *Thread ( void *data )
536 playlist_t *p_playlist = data;
537 playlist_private_t *p_sys = pl_priv(p_playlist);
539 playlist_Lock( p_playlist );
542 while( p_sys->p_input != NULL )
543 LoopInput( p_playlist );
548 /* Destroy any video display if the playlist is stopped */
549 if( p_sys->status.i_status == PLAYLIST_STOPPED
550 && input_resource_HasVout( p_sys->p_input_resource ) )
552 PL_UNLOCK; /* Mind: NO LOCKS while manipulating input resources! */
553 input_resource_TerminateVout( p_sys->p_input_resource );
555 continue; /* lost lock = lost state */
558 LoopRequest( p_playlist );
560 p_sys->status.i_status = PLAYLIST_STOPPED;
561 playlist_Unlock( p_playlist );
563 input_resource_Terminate( p_sys->p_input_resource );