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 *****************************************************************************/
28 #include <vlc_common.h>
30 #include <vlc_input.h>
31 #include <vlc_interface.h>
32 #include <vlc_playlist.h>
33 #include "stream_output/stream_output.h"
34 #include "playlist_internal.h"
36 /*****************************************************************************
38 *****************************************************************************/
39 static void *Thread ( void * );
41 /*****************************************************************************
42 * Main functions for the global thread
43 *****************************************************************************/
46 * Create the main playlist threads.
47 * Additionally to the playlist, this thread controls :
51 * \return an object with a started thread
53 void playlist_Activate( playlist_t *p_playlist )
56 playlist_private_t *p_sys = pl_priv(p_playlist);
59 p_sys->p_fetcher = playlist_fetcher_New( p_playlist );
60 if( !p_sys->p_fetcher )
61 msg_Err( p_playlist, "cannot create playlist fetcher" );
64 p_sys->p_preparser = playlist_preparser_New( p_playlist, p_sys->p_fetcher );
65 if( !p_sys->p_preparser )
66 msg_Err( p_playlist, "cannot create playlist preparser" );
68 /* Start the playlist thread */
69 if( vlc_clone( &p_sys->thread, Thread, p_playlist,
70 VLC_THREAD_PRIORITY_LOW ) )
72 msg_Err( p_playlist, "cannot spawn playlist thread" );
74 msg_Dbg( p_playlist, "Activated" );
77 void playlist_Deactivate( playlist_t *p_playlist )
80 playlist_private_t *p_sys = pl_priv(p_playlist);
82 msg_Dbg( p_playlist, "Deactivate" );
84 vlc_object_kill( p_playlist );
86 vlc_cond_signal( &p_sys->signal );
89 vlc_join( p_sys->thread, NULL );
90 assert( !p_sys->p_input );
93 playlist_preparser_t *p_preparser = p_sys->p_preparser;
94 playlist_fetcher_t *p_fetcher = p_sys->p_fetcher;
96 p_sys->p_preparser = NULL;
97 p_sys->p_fetcher = NULL;
101 playlist_preparser_Delete( p_preparser );
103 playlist_fetcher_Delete( p_fetcher );
105 /* release input resources */
106 if( p_sys->p_input_resource )
107 input_resource_Delete( p_sys->p_input_resource );
108 p_sys->p_input_resource = NULL;
111 playlist_MLDump( p_playlist );
115 /* Release the current node */
116 set_current_status_node( p_playlist, NULL );
118 /* Release the current item */
119 set_current_status_item( p_playlist, NULL );
123 msg_Dbg( p_playlist, "Deactivated" );
129 static int InputEvent( vlc_object_t *p_this, char const *psz_cmd,
130 vlc_value_t oldval, vlc_value_t newval, void *p_data )
132 VLC_UNUSED(p_this); VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
133 playlist_t *p_playlist = p_data;
135 if( newval.i_int != INPUT_EVENT_STATE &&
136 newval.i_int != INPUT_EVENT_DEAD )
141 /* XXX: signaling while not changing any parameter... suspicious... */
142 vlc_cond_signal( &pl_priv(p_playlist)->signal );
148 static void UpdateActivity( playlist_t *p_playlist, int i_delta )
152 const int i_activity = var_GetInteger( p_playlist, "activity" ) ;
153 var_SetInteger( p_playlist, "activity", i_activity + i_delta );
157 * Synchronise the current index of the playlist
158 * to match the index of the current item.
160 * \param p_playlist the playlist structure
161 * \param p_cur the current playlist item
164 static void ResyncCurrentIndex( playlist_t *p_playlist, playlist_item_t *p_cur )
168 PL_DEBUG( "resyncing on %s", PLI_NAME( p_cur ) );
169 /* Simply resync index */
171 p_playlist->i_current_index = -1;
172 for( i = 0 ; i< p_playlist->current.i_size; i++ )
174 if( ARRAY_VAL( p_playlist->current, i ) == p_cur )
176 p_playlist->i_current_index = i;
180 PL_DEBUG( "%s is at %i", PLI_NAME( p_cur ), p_playlist->i_current_index );
183 static void ResetCurrentlyPlaying( playlist_t *p_playlist,
184 playlist_item_t *p_cur )
186 playlist_private_t *p_sys = pl_priv(p_playlist);
188 stats_TimerStart( p_playlist, "Items array build",
189 STATS_TIMER_PLAYLIST_BUILD );
190 PL_DEBUG( "rebuilding array of current - root %s",
191 PLI_NAME( p_sys->status.p_node ) );
192 ARRAY_RESET( p_playlist->current );
193 p_playlist->i_current_index = -1;
194 for( playlist_item_t *p_next = NULL; ; )
196 /** FIXME: this is *slow* */
197 p_next = playlist_GetNextLeaf( p_playlist,
198 p_sys->status.p_node,
199 p_next, true, false );
203 if( p_next == p_cur )
204 p_playlist->i_current_index = p_playlist->current.i_size;
205 ARRAY_APPEND( p_playlist->current, p_next);
207 PL_DEBUG("rebuild done - %i items, index %i", p_playlist->current.i_size,
208 p_playlist->i_current_index);
210 if( var_GetBool( p_playlist, "random" ) )
212 /* Shuffle the array */
213 srand( (unsigned int)mdate() );
214 for( int j = p_playlist->current.i_size - 1; j > 0; j-- )
216 int i = rand() % (j+1); /* between 0 and j */
217 playlist_item_t *p_tmp;
218 /* swap the two items */
219 p_tmp = ARRAY_VAL(p_playlist->current, i);
220 ARRAY_VAL(p_playlist->current,i) = ARRAY_VAL(p_playlist->current,j);
221 ARRAY_VAL(p_playlist->current,j) = p_tmp;
224 p_sys->b_reset_currently_playing = false;
225 stats_TimerStop( p_playlist, STATS_TIMER_PLAYLIST_BUILD );
230 * Start the input for an item
232 * \param p_playlist the playlist object
233 * \param p_item the item to play
236 static int PlayItem( playlist_t *p_playlist, playlist_item_t *p_item )
238 playlist_private_t *p_sys = pl_priv(p_playlist);
239 input_item_t *p_input = p_item->p_input;
243 msg_Dbg( p_playlist, "creating new input thread" );
245 p_input->i_nb_played++;
246 set_current_status_item( p_playlist, p_item );
248 p_sys->status.i_status = PLAYLIST_RUNNING;
250 UpdateActivity( p_playlist, DEFAULT_INPUT_ACTIVITY );
252 assert( p_sys->p_input == NULL );
254 input_thread_t *p_input_thread =
255 input_CreateThreadExtended( p_playlist, p_input, NULL, p_sys->p_input_resource );
259 p_sys->p_input = p_input_thread;
261 var_AddCallback( p_input_thread, "intf-event", InputEvent, p_playlist );
264 p_sys->p_input_resource = NULL;
266 char *psz_uri = input_item_GetURI( p_item->p_input );
267 if( psz_uri && ( !strncmp( psz_uri, "directory:", 10 ) ||
268 !strncmp( psz_uri, "vlc:", 4 ) ) )
275 /* TODO store art policy in playlist private data */
276 if( var_GetInteger( p_playlist, "album-art" ) == ALBUM_ART_WHEN_PLAYED )
280 char *psz_arturl, *psz_name;
281 psz_arturl = input_item_GetArtURL( p_input );
282 psz_name = input_item_GetName( p_input );
284 /* p_input->p_meta should not be null after a successfull CreateThread */
285 b_has_art = !EMPTY_STR( psz_arturl );
287 if( !b_has_art || strncmp( psz_arturl, "attachment://", 13 ) )
289 PL_DEBUG( "requesting art for %s", psz_name );
290 playlist_AskForArtEnqueue( p_playlist, p_input, pl_Locked );
297 var_SetInteger( p_playlist, "item-current", p_input->i_id );
304 * Compute the next playlist item depending on
305 * the playlist course mode (forward, backward, random, view,...).
307 * \param p_playlist the playlist object
310 static playlist_item_t *NextItem( playlist_t *p_playlist )
312 playlist_private_t *p_sys = pl_priv(p_playlist);
313 playlist_item_t *p_new = NULL;
315 /* Handle quickly a few special cases */
316 /* No items to play */
317 if( p_playlist->items.i_size == 0 )
319 msg_Info( p_playlist, "playlist is empty" );
323 /* Start the real work */
324 if( p_sys->request.b_request )
326 p_new = p_sys->request.p_item;
327 int i_skip = p_sys->request.i_skip;
328 PL_DEBUG( "processing request item %s node %s skip %i",
329 PLI_NAME( p_sys->request.p_item ),
330 PLI_NAME( p_sys->request.p_node ), i_skip );
332 if( p_sys->request.p_node &&
333 p_sys->request.p_node != get_current_status_node( p_playlist ) )
336 set_current_status_node( p_playlist, p_sys->request.p_node );
337 p_sys->request.p_node = NULL;
338 p_sys->b_reset_currently_playing = true;
341 /* If we are asked for a node, go to it's first child */
342 if( i_skip == 0 && ( p_new == NULL || p_new->i_children != -1 ) )
347 p_new = playlist_GetNextLeaf( p_playlist, p_new, NULL, true, false );
348 for( int i = 0; i < p_playlist->current.i_size; i++ )
350 if( p_new == ARRAY_VAL( p_playlist->current, i ) )
352 p_playlist->i_current_index = i;
359 if( p_sys->b_reset_currently_playing )
360 /* A bit too bad to reset twice ... */
361 ResetCurrentlyPlaying( p_playlist, p_new );
363 ResyncCurrentIndex( p_playlist, p_new );
365 p_playlist->i_current_index = -1;
367 if( p_playlist->current.i_size && (i_skip > 0) )
369 if( p_playlist->i_current_index < -1 )
370 p_playlist->i_current_index = -1;
371 for( int i = i_skip; i > 0 ; i-- )
373 p_playlist->i_current_index++;
374 if( p_playlist->i_current_index >= p_playlist->current.i_size )
376 PL_DEBUG( "looping - restarting at beginning of node" );
377 p_playlist->i_current_index = 0;
380 p_new = ARRAY_VAL( p_playlist->current,
381 p_playlist->i_current_index );
383 else if( p_playlist->current.i_size && (i_skip < 0) )
385 for( int i = i_skip; i < 0 ; i++ )
387 p_playlist->i_current_index--;
388 if( p_playlist->i_current_index <= -1 )
390 PL_DEBUG( "looping - restarting at end of node" );
391 p_playlist->i_current_index = p_playlist->current.i_size-1;
394 p_new = ARRAY_VAL( p_playlist->current,
395 p_playlist->i_current_index );
397 /* Clear the request */
398 p_sys->request.b_request = false;
400 /* "Automatic" item change ( next ) */
403 bool b_loop = var_GetBool( p_playlist, "loop" );
404 bool b_repeat = var_GetBool( p_playlist, "repeat" );
405 bool b_playstop = var_GetBool( p_playlist, "play-and-stop" );
407 /* Repeat and play/stop */
408 if( b_repeat && get_current_status_item( p_playlist ) )
410 msg_Dbg( p_playlist,"repeating item" );
411 return get_current_status_item( p_playlist );
415 msg_Dbg( p_playlist,"stopping (play and stop)" );
420 if( get_current_status_item( p_playlist ) )
422 playlist_item_t *p_parent = get_current_status_item( p_playlist );
425 if( p_parent->i_flags & PLAYLIST_SKIP_FLAG )
427 msg_Dbg( p_playlist, "blocking item, stopping") ;
430 p_parent = p_parent->p_parent;
434 PL_DEBUG( "changing item without a request (current %i/%i)",
435 p_playlist->i_current_index, p_playlist->current.i_size );
436 /* Cant go to next from current item */
437 if( get_current_status_item( p_playlist ) &&
438 get_current_status_item( p_playlist )->i_flags & PLAYLIST_SKIP_FLAG )
441 if( p_sys->b_reset_currently_playing )
442 ResetCurrentlyPlaying( p_playlist,
443 get_current_status_item( p_playlist ) );
445 p_playlist->i_current_index++;
446 assert( p_playlist->i_current_index <= p_playlist->current.i_size );
447 if( p_playlist->i_current_index == p_playlist->current.i_size )
449 if( !b_loop || p_playlist->current.i_size == 0 )
451 p_playlist->i_current_index = 0;
453 PL_DEBUG( "using item %i", p_playlist->i_current_index );
454 if ( p_playlist->current.i_size == 0 )
457 p_new = ARRAY_VAL( p_playlist->current, p_playlist->i_current_index );
458 /* The new item can't be autoselected */
459 if( p_new != NULL && p_new->i_flags & PLAYLIST_SKIP_FLAG )
465 static int LoopInput( playlist_t *p_playlist )
467 playlist_private_t *p_sys = pl_priv(p_playlist);
468 input_thread_t *p_input = p_sys->p_input;
473 if( ( p_sys->request.b_request || !vlc_object_alive( p_playlist ) ) && !p_input->b_die )
475 PL_DEBUG( "incoming request - stopping current input" );
476 input_StopThread( p_input );
479 /* This input is dead. Remove it ! */
480 if( p_input->b_dead )
482 PL_DEBUG( "dead input" );
484 assert( p_sys->p_input_resource == NULL );
486 p_sys->p_input_resource = input_DetachResource( p_input );
489 /* We can unlock as we return VLC_EGENERIC (no event will be lost) */
491 /* input_resource_t must be manipulated without playlist lock */
492 if( !var_CreateGetBool( p_input, "sout-keep" ) )
493 input_resource_TerminateSout( p_sys->p_input_resource );
495 /* The DelCallback must be issued without playlist lock */
496 var_DelCallback( p_input, "intf-event", InputEvent, p_playlist );
500 p_sys->p_input = NULL;
501 vlc_thread_join( p_input );
502 vlc_object_release( p_input );
504 UpdateActivity( p_playlist, -DEFAULT_INPUT_ACTIVITY );
508 /* This input is dying, let it do */
509 else if( p_input->b_die )
511 PL_DEBUG( "dying input" );
513 /* This input has finished, ask it to die ! */
514 else if( p_input->b_error || p_input->b_eof )
516 PL_DEBUG( "finished input" );
517 input_StopThread( p_input );
522 static void LoopRequest( playlist_t *p_playlist )
524 playlist_private_t *p_sys = pl_priv(p_playlist);
525 assert( !p_sys->p_input );
527 /* No input. Several cases
528 * - No request, running status -> start new item
529 * - No request, stopped status -> collect garbage
530 * - Request, running requested -> start new item
531 * - Request, stopped requested -> collect garbage
533 const int i_status = p_sys->request.b_request ?
534 p_sys->request.i_status : p_sys->status.i_status;
536 if( i_status == PLAYLIST_STOPPED || !vlc_object_alive( p_playlist ) )
538 p_sys->status.i_status = PLAYLIST_STOPPED;
540 if( p_sys->p_input_resource &&
541 input_resource_HasVout( p_sys->p_input_resource ) )
543 /* XXX We can unlock if we don't issue the wait as we will be
544 * call again without anything else done between the calls */
547 /* input_resource_t must be manipulated without playlist lock */
548 input_resource_TerminateVout( p_sys->p_input_resource );
554 if( vlc_object_alive( p_playlist ) )
555 vlc_cond_wait( &pl_priv(p_playlist)->signal,
556 &vlc_internals(p_playlist)->lock );
561 playlist_item_t *p_item = NextItem( p_playlist );
564 msg_Dbg( p_playlist, "starting new item" );
565 PlayItem( p_playlist, p_item );
569 msg_Dbg( p_playlist, "nothing to play" );
570 p_sys->status.i_status = PLAYLIST_STOPPED;
572 if( var_GetBool( p_playlist, "play-and-exit" ) )
574 msg_Info( p_playlist, "end of playlist, exiting" );
575 libvlc_Quit( p_playlist->p_libvlc );
580 * Run the main control thread itself
582 static void *Thread ( void *data )
584 playlist_t *p_playlist = data;
585 playlist_private_t *p_sys = pl_priv(p_playlist);
587 playlist_Lock( p_playlist );
588 while( vlc_object_alive( p_playlist ) || p_sys->p_input )
590 /* FIXME: what's that ! */
591 if( p_sys->b_reset_currently_playing &&
592 mdate() - p_sys->last_rebuild_date > 30000 ) // 30 ms
594 ResetCurrentlyPlaying( p_playlist,
595 get_current_status_item( p_playlist ) );
596 p_sys->last_rebuild_date = mdate();
599 /* If there is an input, check that it doesn't need to die. */
600 while( !LoopInput( p_playlist ) )
601 vlc_cond_wait( &pl_priv(p_playlist)->signal,
602 &vlc_internals(p_playlist)->lock );
604 LoopRequest( p_playlist );
606 playlist_Unlock( p_playlist );