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 var_SetAddress( p_playlist, "input-current", p_input_thread );
264 if( input_Start( p_sys->p_input ) )
266 vlc_object_release( p_input_thread );
267 p_sys->p_input = p_input_thread = NULL;
271 p_sys->p_input_resource = NULL;
273 char *psz_uri = input_item_GetURI( p_item->p_input );
274 if( psz_uri && ( !strncmp( psz_uri, "directory:", 10 ) ||
275 !strncmp( psz_uri, "vlc:", 4 ) ) )
282 /* TODO store art policy in playlist private data */
283 if( var_GetInteger( p_playlist, "album-art" ) == ALBUM_ART_WHEN_PLAYED )
287 char *psz_arturl, *psz_name;
288 psz_arturl = input_item_GetArtURL( p_input );
289 psz_name = input_item_GetName( p_input );
291 /* p_input->p_meta should not be null after a successfull CreateThread */
292 b_has_art = !EMPTY_STR( psz_arturl );
294 if( !b_has_art || strncmp( psz_arturl, "attachment://", 13 ) )
296 PL_DEBUG( "requesting art for %s", psz_name );
297 playlist_AskForArtEnqueue( p_playlist, p_input, pl_Locked );
304 var_SetAddress( p_playlist, "item-current", p_input );
311 * Compute the next playlist item depending on
312 * the playlist course mode (forward, backward, random, view,...).
314 * \param p_playlist the playlist object
317 static playlist_item_t *NextItem( playlist_t *p_playlist )
319 playlist_private_t *p_sys = pl_priv(p_playlist);
320 playlist_item_t *p_new = NULL;
322 /* Handle quickly a few special cases */
323 /* No items to play */
324 if( p_playlist->items.i_size == 0 )
326 msg_Info( p_playlist, "playlist is empty" );
330 /* Start the real work */
331 if( p_sys->request.b_request )
333 p_new = p_sys->request.p_item;
334 int i_skip = p_sys->request.i_skip;
335 PL_DEBUG( "processing request item %s node %s skip %i",
336 PLI_NAME( p_sys->request.p_item ),
337 PLI_NAME( p_sys->request.p_node ), i_skip );
339 if( p_sys->request.p_node &&
340 p_sys->request.p_node != get_current_status_node( p_playlist ) )
343 set_current_status_node( p_playlist, p_sys->request.p_node );
344 p_sys->request.p_node = NULL;
345 p_sys->b_reset_currently_playing = true;
348 /* If we are asked for a node, go to it's first child */
349 if( i_skip == 0 && ( p_new == NULL || p_new->i_children != -1 ) )
354 p_new = playlist_GetNextLeaf( p_playlist, p_new, NULL, true, false );
355 for( int i = 0; i < p_playlist->current.i_size; i++ )
357 if( p_new == ARRAY_VAL( p_playlist->current, i ) )
359 p_playlist->i_current_index = i;
366 if( p_sys->b_reset_currently_playing )
367 /* A bit too bad to reset twice ... */
368 ResetCurrentlyPlaying( p_playlist, p_new );
370 ResyncCurrentIndex( p_playlist, p_new );
372 p_playlist->i_current_index = -1;
374 if( p_playlist->current.i_size && (i_skip > 0) )
376 if( p_playlist->i_current_index < -1 )
377 p_playlist->i_current_index = -1;
378 for( int i = i_skip; i > 0 ; i-- )
380 p_playlist->i_current_index++;
381 if( p_playlist->i_current_index >= p_playlist->current.i_size )
383 PL_DEBUG( "looping - restarting at beginning of node" );
384 p_playlist->i_current_index = 0;
387 p_new = ARRAY_VAL( p_playlist->current,
388 p_playlist->i_current_index );
390 else if( p_playlist->current.i_size && (i_skip < 0) )
392 for( int i = i_skip; i < 0 ; i++ )
394 p_playlist->i_current_index--;
395 if( p_playlist->i_current_index <= -1 )
397 PL_DEBUG( "looping - restarting at end of node" );
398 p_playlist->i_current_index = p_playlist->current.i_size-1;
401 p_new = ARRAY_VAL( p_playlist->current,
402 p_playlist->i_current_index );
404 /* Clear the request */
405 p_sys->request.b_request = false;
407 /* "Automatic" item change ( next ) */
410 bool b_loop = var_GetBool( p_playlist, "loop" );
411 bool b_repeat = var_GetBool( p_playlist, "repeat" );
412 bool b_playstop = var_GetBool( p_playlist, "play-and-stop" );
414 /* Repeat and play/stop */
415 if( b_repeat && get_current_status_item( p_playlist ) )
417 msg_Dbg( p_playlist,"repeating item" );
418 return get_current_status_item( p_playlist );
422 msg_Dbg( p_playlist,"stopping (play and stop)" );
427 if( get_current_status_item( p_playlist ) )
429 playlist_item_t *p_parent = get_current_status_item( p_playlist );
432 if( p_parent->i_flags & PLAYLIST_SKIP_FLAG )
434 msg_Dbg( p_playlist, "blocking item, stopping") ;
437 p_parent = p_parent->p_parent;
441 PL_DEBUG( "changing item without a request (current %i/%i)",
442 p_playlist->i_current_index, p_playlist->current.i_size );
443 /* Cant go to next from current item */
444 if( get_current_status_item( p_playlist ) &&
445 get_current_status_item( p_playlist )->i_flags & PLAYLIST_SKIP_FLAG )
448 if( p_sys->b_reset_currently_playing )
449 ResetCurrentlyPlaying( p_playlist,
450 get_current_status_item( p_playlist ) );
452 p_playlist->i_current_index++;
453 assert( p_playlist->i_current_index <= p_playlist->current.i_size );
454 if( p_playlist->i_current_index == p_playlist->current.i_size )
456 if( !b_loop || p_playlist->current.i_size == 0 )
458 p_playlist->i_current_index = 0;
460 PL_DEBUG( "using item %i", p_playlist->i_current_index );
461 if ( p_playlist->current.i_size == 0 )
464 p_new = ARRAY_VAL( p_playlist->current, p_playlist->i_current_index );
465 /* The new item can't be autoselected */
466 if( p_new != NULL && p_new->i_flags & PLAYLIST_SKIP_FLAG )
472 static int LoopInput( playlist_t *p_playlist )
474 playlist_private_t *p_sys = pl_priv(p_playlist);
475 input_thread_t *p_input = p_sys->p_input;
480 if( ( p_sys->request.b_request || !vlc_object_alive( p_playlist ) ) && !p_input->b_die )
482 PL_DEBUG( "incoming request - stopping current input" );
483 input_Stop( p_input, true );
486 /* This input is dead. Remove it ! */
487 if( p_input->b_dead )
489 PL_DEBUG( "dead input" );
491 assert( p_sys->p_input_resource == NULL );
493 p_sys->p_input_resource = input_DetachResource( p_input );
496 /* We can unlock as we return VLC_EGENERIC (no event will be lost) */
498 /* input_resource_t must be manipulated without playlist lock */
499 if( !var_CreateGetBool( p_input, "sout-keep" ) )
500 input_resource_TerminateSout( p_sys->p_input_resource );
502 /* The DelCallback must be issued without playlist lock */
503 var_DelCallback( p_input, "intf-event", InputEvent, p_playlist );
507 p_sys->p_input = NULL;
508 vlc_thread_join( p_input );
509 vlc_object_release( p_input );
511 UpdateActivity( p_playlist, -DEFAULT_INPUT_ACTIVITY );
515 /* This input is dying, let it do */
516 else if( p_input->b_die )
518 PL_DEBUG( "dying input" );
520 /* This input has finished, ask it to die ! */
521 else if( p_input->b_error || p_input->b_eof )
523 PL_DEBUG( "finished input" );
524 input_Stop( p_input, false );
529 static void LoopRequest( playlist_t *p_playlist )
531 playlist_private_t *p_sys = pl_priv(p_playlist);
532 assert( !p_sys->p_input );
534 /* No input. Several cases
535 * - No request, running status -> start new item
536 * - No request, stopped status -> collect garbage
537 * - Request, running requested -> start new item
538 * - Request, stopped requested -> collect garbage
540 const int i_status = p_sys->request.b_request ?
541 p_sys->request.i_status : p_sys->status.i_status;
543 if( i_status == PLAYLIST_STOPPED || !vlc_object_alive( p_playlist ) )
545 p_sys->status.i_status = PLAYLIST_STOPPED;
547 if( p_sys->p_input_resource &&
548 input_resource_HasVout( p_sys->p_input_resource ) )
550 /* XXX We can unlock if we don't issue the wait as we will be
551 * call again without anything else done between the calls */
554 /* input_resource_t must be manipulated without playlist lock */
555 input_resource_TerminateVout( p_sys->p_input_resource );
561 if( vlc_object_alive( p_playlist ) )
562 vlc_cond_wait( &p_sys->signal, &p_sys->lock );
567 playlist_item_t *p_item = NextItem( p_playlist );
570 msg_Dbg( p_playlist, "starting new item" );
571 PlayItem( p_playlist, p_item );
575 msg_Dbg( p_playlist, "nothing to play" );
576 p_sys->status.i_status = PLAYLIST_STOPPED;
578 if( var_GetBool( p_playlist, "play-and-exit" ) )
580 msg_Info( p_playlist, "end of playlist, exiting" );
581 libvlc_Quit( p_playlist->p_libvlc );
586 * Run the main control thread itself
588 static void *Thread ( void *data )
590 playlist_t *p_playlist = data;
591 playlist_private_t *p_sys = pl_priv(p_playlist);
593 playlist_Lock( p_playlist );
594 while( vlc_object_alive( p_playlist ) || p_sys->p_input )
596 /* FIXME: what's that ! */
597 if( p_sys->b_reset_currently_playing &&
598 mdate() - p_sys->last_rebuild_date > 30000 ) // 30 ms
600 ResetCurrentlyPlaying( p_playlist,
601 get_current_status_item( p_playlist ) );
602 p_sys->last_rebuild_date = mdate();
605 /* If there is an input, check that it doesn't need to die. */
606 while( !LoopInput( p_playlist ) )
607 vlc_cond_wait( &p_sys->signal, &p_sys->lock );
609 LoopRequest( p_playlist );
611 playlist_Unlock( p_playlist );