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>
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 /* Start the playlist thread */
62 if( vlc_clone( &p_sys->thread, Thread, p_playlist,
63 VLC_THREAD_PRIORITY_LOW ) )
65 msg_Err( p_playlist, "cannot spawn playlist thread" );
67 msg_Dbg( p_playlist, "Activated" );
70 void playlist_Deactivate( playlist_t *p_playlist )
73 playlist_private_t *p_sys = pl_priv(p_playlist);
75 msg_Dbg( p_playlist, "Deactivate" );
78 vlc_object_kill( p_playlist );
79 vlc_cond_signal( &p_sys->signal );
82 vlc_join( p_sys->thread, NULL );
83 assert( !p_sys->p_input );
85 /* release input resources */
86 if( p_sys->p_input_resource )
87 input_resource_Delete( p_sys->p_input_resource );
88 p_sys->p_input_resource = NULL;
90 if( var_InheritBool( p_playlist, "media-library" ) )
91 playlist_MLDump( p_playlist );
95 /* Release the current node */
96 set_current_status_node( p_playlist, NULL );
98 /* Release the current item */
99 set_current_status_item( p_playlist, NULL );
103 msg_Dbg( p_playlist, "Deactivated" );
109 static int InputEvent( vlc_object_t *p_this, char const *psz_cmd,
110 vlc_value_t oldval, vlc_value_t newval, void *p_data )
112 VLC_UNUSED(p_this); VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
113 playlist_t *p_playlist = p_data;
115 if( newval.i_int != INPUT_EVENT_STATE &&
116 newval.i_int != INPUT_EVENT_DEAD )
121 /* XXX: signaling while not changing any parameter... suspicious... */
122 vlc_cond_signal( &pl_priv(p_playlist)->signal );
128 static void UpdateActivity( playlist_t *p_playlist, int i_delta )
132 const int i_activity = var_GetInteger( p_playlist, "activity" ) ;
133 var_SetInteger( p_playlist, "activity", i_activity + i_delta );
137 * Synchronise the current index of the playlist
138 * to match the index of the current item.
140 * \param p_playlist the playlist structure
141 * \param p_cur the current playlist item
144 static void ResyncCurrentIndex( playlist_t *p_playlist, playlist_item_t *p_cur )
148 PL_DEBUG( "resyncing on %s", PLI_NAME( p_cur ) );
149 /* Simply resync index */
151 p_playlist->i_current_index = -1;
152 for( i = 0 ; i< p_playlist->current.i_size; i++ )
154 if( ARRAY_VAL( p_playlist->current, i ) == p_cur )
156 p_playlist->i_current_index = i;
160 PL_DEBUG( "%s is at %i", PLI_NAME( p_cur ), p_playlist->i_current_index );
163 static void ResetCurrentlyPlaying( playlist_t *p_playlist,
164 playlist_item_t *p_cur )
166 playlist_private_t *p_sys = pl_priv(p_playlist);
168 stats_TimerStart( p_playlist, "Items array build",
169 STATS_TIMER_PLAYLIST_BUILD );
170 PL_DEBUG( "rebuilding array of current - root %s",
171 PLI_NAME( p_sys->status.p_node ) );
172 ARRAY_RESET( p_playlist->current );
173 p_playlist->i_current_index = -1;
174 for( playlist_item_t *p_next = NULL; ; )
176 /** FIXME: this is *slow* */
177 p_next = playlist_GetNextLeaf( p_playlist,
178 p_sys->status.p_node,
179 p_next, true, false );
183 if( p_next == p_cur )
184 p_playlist->i_current_index = p_playlist->current.i_size;
185 ARRAY_APPEND( p_playlist->current, p_next);
187 PL_DEBUG("rebuild done - %i items, index %i", p_playlist->current.i_size,
188 p_playlist->i_current_index);
190 if( var_GetBool( p_playlist, "random" ) && ( p_playlist->current.i_size > 0 ) )
192 /* Shuffle the array */
193 for( unsigned j = p_playlist->current.i_size - 1; j > 0; j-- )
195 unsigned i = ((unsigned)vlc_mrand48()) % (j+1); /* between 0 and j */
196 playlist_item_t *p_tmp;
197 /* swap the two items */
198 p_tmp = ARRAY_VAL(p_playlist->current, i);
199 ARRAY_VAL(p_playlist->current,i) = ARRAY_VAL(p_playlist->current,j);
200 ARRAY_VAL(p_playlist->current,j) = p_tmp;
203 p_sys->b_reset_currently_playing = false;
204 stats_TimerStop( p_playlist, STATS_TIMER_PLAYLIST_BUILD );
209 * Start the input for an item
211 * \param p_playlist the playlist object
212 * \param p_item the item to play
215 static int PlayItem( playlist_t *p_playlist, playlist_item_t *p_item )
217 playlist_private_t *p_sys = pl_priv(p_playlist);
218 input_item_t *p_input = p_item->p_input;
222 msg_Dbg( p_playlist, "creating new input thread" );
224 p_input->i_nb_played++;
225 set_current_status_item( p_playlist, p_item );
227 p_sys->status.i_status = PLAYLIST_RUNNING;
229 UpdateActivity( p_playlist, DEFAULT_INPUT_ACTIVITY );
231 assert( p_sys->p_input == NULL );
233 if( !p_sys->p_input_resource )
234 p_sys->p_input_resource = input_resource_New( VLC_OBJECT( p_playlist ) );
235 input_thread_t *p_input_thread = input_Create( p_playlist, p_input, NULL, p_sys->p_input_resource );
238 p_sys->p_input = p_input_thread;
239 var_AddCallback( p_input_thread, "intf-event", InputEvent, p_playlist );
241 var_SetAddress( p_playlist, "input-current", p_input_thread );
243 if( input_Start( p_sys->p_input ) )
245 vlc_object_release( p_input_thread );
246 p_sys->p_input = p_input_thread = NULL;
250 char *psz_uri = input_item_GetURI( p_item->p_input );
251 if( psz_uri && ( !strncmp( psz_uri, "directory:", 10 ) ||
252 !strncmp( psz_uri, "vlc:", 4 ) ) )
259 /* TODO store art policy in playlist private data */
260 if( var_GetInteger( p_playlist, "album-art" ) == ALBUM_ART_WHEN_PLAYED )
264 char *psz_arturl, *psz_name;
265 psz_arturl = input_item_GetArtURL( p_input );
266 psz_name = input_item_GetName( p_input );
268 /* p_input->p_meta should not be null after a successfull CreateThread */
269 b_has_art = !EMPTY_STR( psz_arturl );
271 if( !b_has_art || strncmp( psz_arturl, "attachment://", 13 ) )
273 PL_DEBUG( "requesting art for %s", psz_name );
274 playlist_AskForArtEnqueue( p_playlist, p_input );
279 /* FIXME: this is not safe !!*/
281 var_SetAddress( p_playlist, "item-current", p_input );
288 * Compute the next playlist item depending on
289 * the playlist course mode (forward, backward, random, view,...).
291 * \param p_playlist the playlist object
294 static playlist_item_t *NextItem( playlist_t *p_playlist )
296 playlist_private_t *p_sys = pl_priv(p_playlist);
297 playlist_item_t *p_new = NULL;
299 /* Handle quickly a few special cases */
300 /* No items to play */
301 if( p_playlist->items.i_size == 0 )
303 msg_Info( p_playlist, "playlist is empty" );
307 /* Start the real work */
308 if( p_sys->request.b_request )
310 p_new = p_sys->request.p_item;
311 int i_skip = p_sys->request.i_skip;
312 PL_DEBUG( "processing request item %s node %s skip %i",
313 PLI_NAME( p_sys->request.p_item ),
314 PLI_NAME( p_sys->request.p_node ), i_skip );
316 if( p_sys->request.p_node &&
317 p_sys->request.p_node != get_current_status_node( p_playlist ) )
320 set_current_status_node( p_playlist, p_sys->request.p_node );
321 p_sys->request.p_node = NULL;
322 p_sys->b_reset_currently_playing = true;
325 /* If we are asked for a node, go to it's first child */
326 if( i_skip == 0 && ( p_new == NULL || p_new->i_children != -1 ) )
331 p_new = playlist_GetNextLeaf( p_playlist, p_new, NULL, true, false );
332 for( int i = 0; i < p_playlist->current.i_size; i++ )
334 if( p_new == ARRAY_VAL( p_playlist->current, i ) )
336 p_playlist->i_current_index = i;
343 if( p_sys->b_reset_currently_playing )
344 /* A bit too bad to reset twice ... */
345 ResetCurrentlyPlaying( p_playlist, p_new );
347 ResyncCurrentIndex( p_playlist, p_new );
349 p_playlist->i_current_index = -1;
351 if( p_playlist->current.i_size && (i_skip > 0) )
353 if( p_playlist->i_current_index < -1 )
354 p_playlist->i_current_index = -1;
355 for( int i = i_skip; i > 0 ; i-- )
357 p_playlist->i_current_index++;
358 if( p_playlist->i_current_index >= p_playlist->current.i_size )
360 PL_DEBUG( "looping - restarting at beginning of node" );
361 p_playlist->i_current_index = 0;
364 p_new = ARRAY_VAL( p_playlist->current,
365 p_playlist->i_current_index );
367 else if( p_playlist->current.i_size && (i_skip < 0) )
369 for( int i = i_skip; i < 0 ; i++ )
371 p_playlist->i_current_index--;
372 if( p_playlist->i_current_index <= -1 )
374 PL_DEBUG( "looping - restarting at end of node" );
375 p_playlist->i_current_index = p_playlist->current.i_size-1;
378 p_new = ARRAY_VAL( p_playlist->current,
379 p_playlist->i_current_index );
381 /* Clear the request */
382 p_sys->request.b_request = false;
384 /* "Automatic" item change ( next ) */
387 bool b_loop = var_GetBool( p_playlist, "loop" );
388 bool b_repeat = var_GetBool( p_playlist, "repeat" );
389 bool b_playstop = var_GetBool( p_playlist, "play-and-stop" );
391 /* Repeat and play/stop */
392 if( b_repeat && get_current_status_item( p_playlist ) )
394 msg_Dbg( p_playlist,"repeating item" );
395 return get_current_status_item( p_playlist );
399 msg_Dbg( p_playlist,"stopping (play and stop)" );
404 if( get_current_status_item( p_playlist ) )
406 playlist_item_t *p_parent = get_current_status_item( p_playlist );
409 if( p_parent->i_flags & PLAYLIST_SKIP_FLAG )
411 msg_Dbg( p_playlist, "blocking item, stopping") ;
414 p_parent = p_parent->p_parent;
418 PL_DEBUG( "changing item without a request (current %i/%i)",
419 p_playlist->i_current_index, p_playlist->current.i_size );
420 /* Cant go to next from current item */
421 if( get_current_status_item( p_playlist ) &&
422 get_current_status_item( p_playlist )->i_flags & PLAYLIST_SKIP_FLAG )
425 if( p_sys->b_reset_currently_playing )
426 ResetCurrentlyPlaying( p_playlist,
427 get_current_status_item( p_playlist ) );
429 p_playlist->i_current_index++;
430 assert( p_playlist->i_current_index <= p_playlist->current.i_size );
431 if( p_playlist->i_current_index == p_playlist->current.i_size )
433 if( !b_loop || p_playlist->current.i_size == 0 )
435 p_playlist->i_current_index = 0;
437 PL_DEBUG( "using item %i", p_playlist->i_current_index );
438 if ( p_playlist->current.i_size == 0 )
441 p_new = ARRAY_VAL( p_playlist->current, p_playlist->i_current_index );
442 /* The new item can't be autoselected */
443 if( p_new != NULL && p_new->i_flags & PLAYLIST_SKIP_FLAG )
449 static int LoopInput( playlist_t *p_playlist )
451 playlist_private_t *p_sys = pl_priv(p_playlist);
452 input_thread_t *p_input = p_sys->p_input;
457 if( ( p_sys->request.b_request || !vlc_object_alive( p_playlist ) ) && !p_input->b_die )
459 PL_DEBUG( "incoming request - stopping current input" );
460 input_Stop( p_input, true );
463 /* This input is dead. Remove it ! */
464 if( p_input->b_dead )
466 PL_DEBUG( "dead input" );
469 /* We can unlock as we return VLC_EGENERIC (no event will be lost) */
471 /* input_resource_t must be manipulated without playlist lock */
472 if( !var_CreateGetBool( p_input, "sout-keep" ) )
473 input_resource_TerminateSout( p_sys->p_input_resource );
475 /* The DelCallback must be issued without playlist lock */
476 var_DelCallback( p_input, "intf-event", InputEvent, p_playlist );
480 p_sys->p_input = NULL;
481 vlc_thread_join( p_input );
482 vlc_object_release( p_input );
484 UpdateActivity( p_playlist, -DEFAULT_INPUT_ACTIVITY );
488 /* This input is dying, let it do */
489 else if( p_input->b_die )
491 PL_DEBUG( "dying input" );
493 /* This input has finished, ask it to die ! */
494 else if( p_input->b_error || p_input->b_eof )
496 PL_DEBUG( "finished input" );
497 input_Stop( p_input, false );
502 static void LoopRequest( playlist_t *p_playlist )
504 playlist_private_t *p_sys = pl_priv(p_playlist);
505 assert( !p_sys->p_input );
507 /* No input. Several cases
508 * - No request, running status -> start new item
509 * - No request, stopped status -> collect garbage
510 * - Request, running requested -> start new item
511 * - Request, stopped requested -> collect garbage
513 const int i_status = p_sys->request.b_request ?
514 p_sys->request.i_status : p_sys->status.i_status;
516 if( i_status == PLAYLIST_STOPPED || !vlc_object_alive( p_playlist ) )
518 p_sys->status.i_status = PLAYLIST_STOPPED;
520 if( p_sys->p_input_resource &&
521 input_resource_HasVout( p_sys->p_input_resource ) )
523 /* XXX We can unlock if we don't issue the wait as we will be
524 * call again without anything else done between the calls */
527 /* input_resource_t must be manipulated without playlist lock */
528 input_resource_TerminateVout( p_sys->p_input_resource );
534 if( vlc_object_alive( p_playlist ) )
535 vlc_cond_wait( &p_sys->signal, &p_sys->lock );
540 playlist_item_t *p_item = NextItem( p_playlist );
543 msg_Dbg( p_playlist, "starting new item" );
544 PlayItem( p_playlist, p_item );
548 msg_Dbg( p_playlist, "nothing to play" );
549 p_sys->status.i_status = PLAYLIST_STOPPED;
551 if( var_GetBool( p_playlist, "play-and-exit" ) )
553 msg_Info( p_playlist, "end of playlist, exiting" );
554 libvlc_Quit( p_playlist->p_libvlc );
559 * Run the main control thread itself
561 static void *Thread ( void *data )
563 playlist_t *p_playlist = data;
564 playlist_private_t *p_sys = pl_priv(p_playlist);
566 playlist_Lock( p_playlist );
567 while( vlc_object_alive( p_playlist ) || p_sys->p_input )
569 /* FIXME: what's that ! */
570 if( p_sys->b_reset_currently_playing &&
571 mdate() - p_sys->last_rebuild_date > 30000 ) // 30 ms
573 ResetCurrentlyPlaying( p_playlist,
574 get_current_status_item( p_playlist ) );
575 p_sys->last_rebuild_date = mdate();
578 /* If there is an input, check that it doesn't need to die. */
579 while( !LoopInput( p_playlist ) )
580 vlc_cond_wait( &p_sys->signal, &p_sys->lock );
582 LoopRequest( p_playlist );
584 playlist_Unlock( p_playlist );