directfb: remove obvious comments
[vlc/gmpfix.git] / src / playlist / thread.c
blobed1b80866b063df9e473c5955c993ea2b8b33ebb
1 /*****************************************************************************
2 * thread.c : Playlist management functions
3 *****************************************************************************
4 * Copyright © 1999-2008 VLC authors and VideoLAN
5 * $Id$
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 *****************************************************************************/
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
28 #include <assert.h>
30 #include <vlc_common.h>
31 #include <vlc_es.h>
32 #include <vlc_input.h>
33 #include <vlc_interface.h>
34 #include <vlc_playlist.h>
35 #include <vlc_rand.h>
36 #include "stream_output/stream_output.h"
37 #include "playlist_internal.h"
39 /*****************************************************************************
40 * Local prototypes
41 *****************************************************************************/
42 static void *Thread ( void * );
44 /*****************************************************************************
45 * Main functions for the global thread
46 *****************************************************************************/
48 /**
49 * Creates the main playlist thread.
51 void playlist_Activate( playlist_t *p_playlist )
53 playlist_private_t *p_sys = pl_priv(p_playlist);
55 if( vlc_clone( &p_sys->thread, Thread, p_playlist,
56 VLC_THREAD_PRIORITY_LOW ) )
58 msg_Err( p_playlist, "cannot spawn playlist thread" );
59 abort();
63 /**
64 * Stops the playlist forever (but do not destroy it yet).
65 * Any input is stopped.
66 * \return Nothing but waits for the playlist to be deactivated.
68 void playlist_Deactivate( playlist_t *p_playlist )
70 playlist_private_t *p_sys = pl_priv(p_playlist);
72 PL_LOCK;
73 /* WARNING: There is a latent bug. It is assumed that only one thread will
74 * be waiting for playlist deactivation at a time. So far, that works
75 * as playlist_Deactivate() is only ever called while closing an
76 * interface and interfaces are shut down serially by intf_DestroyAll(). */
77 if( p_sys->killed )
79 PL_UNLOCK;
80 return;
83 msg_Dbg( p_playlist, "deactivating the playlist" );
84 p_sys->killed = true;
85 vlc_cond_signal( &p_sys->signal );
86 PL_UNLOCK;
88 vlc_join( p_sys->thread, NULL );
91 /* */
93 /* Input Callback */
94 static int InputEvent( vlc_object_t *p_this, char const *psz_cmd,
95 vlc_value_t oldval, vlc_value_t newval, void *p_data )
97 VLC_UNUSED(p_this); VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
98 playlist_t *p_playlist = p_data;
100 if( newval.i_int != INPUT_EVENT_STATE &&
101 newval.i_int != INPUT_EVENT_DEAD )
102 return VLC_SUCCESS;
104 PL_LOCK;
106 /* XXX: signaling while not changing any parameter... suspicious... */
107 vlc_cond_signal( &pl_priv(p_playlist)->signal );
109 PL_UNLOCK;
110 return VLC_SUCCESS;
114 * Synchronise the current index of the playlist
115 * to match the index of the current item.
117 * \param p_playlist the playlist structure
118 * \param p_cur the current playlist item
119 * \return nothing
121 void ResyncCurrentIndex( playlist_t *p_playlist, playlist_item_t *p_cur )
123 PL_ASSERT_LOCKED;
125 PL_DEBUG( "resyncing on %s", PLI_NAME( p_cur ) );
126 /* Simply resync index */
127 int i;
128 p_playlist->i_current_index = -1;
129 for( i = 0 ; i< p_playlist->current.i_size; i++ )
131 if( ARRAY_VAL( p_playlist->current, i ) == p_cur )
133 p_playlist->i_current_index = i;
134 break;
137 PL_DEBUG( "%s is at %i", PLI_NAME( p_cur ), p_playlist->i_current_index );
141 * Reset the currently playing playlist.
143 * \param p_playlist the playlist structure
144 * \param p_cur the current playlist item
145 * \return nothing
147 void ResetCurrentlyPlaying( playlist_t *p_playlist,
148 playlist_item_t *p_cur )
150 playlist_private_t *p_sys = pl_priv(p_playlist);
152 PL_DEBUG( "rebuilding array of current - root %s",
153 PLI_NAME( p_sys->status.p_node ) );
154 ARRAY_RESET( p_playlist->current );
155 p_playlist->i_current_index = -1;
156 for( playlist_item_t *p_next = NULL; ; )
158 /** FIXME: this is *slow* */
159 p_next = playlist_GetNextLeaf( p_playlist,
160 p_sys->status.p_node,
161 p_next, true, false );
162 if( !p_next )
163 break;
165 if( p_next == p_cur )
166 p_playlist->i_current_index = p_playlist->current.i_size;
167 ARRAY_APPEND( p_playlist->current, p_next);
169 PL_DEBUG("rebuild done - %i items, index %i", p_playlist->current.i_size,
170 p_playlist->i_current_index);
172 if( var_GetBool( p_playlist, "random" ) && ( p_playlist->current.i_size > 0 ) )
174 /* Shuffle the array */
175 for( unsigned j = p_playlist->current.i_size - 1; j > 0; j-- )
177 unsigned i = vlc_lrand48() % (j+1); /* between 0 and j */
178 playlist_item_t *p_tmp;
179 /* swap the two items */
180 p_tmp = ARRAY_VAL(p_playlist->current, i);
181 ARRAY_VAL(p_playlist->current,i) = ARRAY_VAL(p_playlist->current,j);
182 ARRAY_VAL(p_playlist->current,j) = p_tmp;
185 p_sys->b_reset_currently_playing = false;
190 * Start the input for an item
192 * \param p_playlist the playlist object
193 * \param p_item the item to play
194 * \return nothing
196 static int PlayItem( playlist_t *p_playlist, playlist_item_t *p_item )
198 playlist_private_t *p_sys = pl_priv(p_playlist);
199 input_item_t *p_input = p_item->p_input;
201 PL_ASSERT_LOCKED;
203 msg_Dbg( p_playlist, "creating new input thread" );
205 p_input->i_nb_played++;
206 set_current_status_item( p_playlist, p_item );
208 p_sys->status.i_status = PLAYLIST_RUNNING;
210 assert( p_sys->p_input == NULL );
212 input_thread_t *p_input_thread = input_Create( p_playlist, p_input, NULL, p_sys->p_input_resource );
213 if( p_input_thread )
215 p_sys->p_input = p_input_thread;
216 var_AddCallback( p_input_thread, "intf-event", InputEvent, p_playlist );
218 var_SetAddress( p_playlist, "input-current", p_input_thread );
220 if( input_Start( p_sys->p_input ) )
222 vlc_object_release( p_input_thread );
223 p_sys->p_input = p_input_thread = NULL;
227 bool b_find_art = var_GetInteger( p_playlist, "album-art" )
228 == ALBUM_ART_WHEN_PLAYED;
229 if( b_find_art )
231 char *psz_uri = input_item_GetURI( p_item->p_input );
232 if( psz_uri != NULL && (!strncmp( psz_uri, "directory:", 10 ) ||
233 !strncmp( psz_uri, "vlc:", 4 )) )
234 b_find_art = false;
235 free( psz_uri );
238 /* TODO store art policy in playlist private data */
239 if( b_find_art )
241 char *psz_arturl = input_item_GetArtURL( p_input );
242 char *psz_name = input_item_GetName( p_input );
243 /* p_input->p_meta should not be null after a successful CreateThread */
244 bool b_has_art = !EMPTY_STR( psz_arturl );
246 if( !b_has_art || strncmp( psz_arturl, "attachment://", 13 ) )
248 PL_DEBUG( "requesting art for %s", psz_name );
249 libvlc_ArtRequest( p_playlist->p_libvlc, p_input );
251 free( psz_arturl );
252 free( psz_name );
254 PL_UNLOCK;
255 var_TriggerCallback( p_playlist, "activity" );
256 PL_LOCK;
258 return VLC_SUCCESS;
262 * Compute the next playlist item depending on
263 * the playlist course mode (forward, backward, random, view,...).
265 * \param p_playlist the playlist object
266 * \return nothing
268 static playlist_item_t *NextItem( playlist_t *p_playlist )
270 playlist_private_t *p_sys = pl_priv(p_playlist);
271 playlist_item_t *p_new = NULL;
273 /* Handle quickly a few special cases */
274 /* No items to play */
275 if( p_playlist->items.i_size == 0 )
277 msg_Info( p_playlist, "playlist is empty" );
278 return NULL;
281 /* Start the real work */
282 if( p_sys->request.b_request )
284 p_new = p_sys->request.p_item;
285 int i_skip = p_sys->request.i_skip;
286 PL_DEBUG( "processing request item: %s, node: %s, skip: %i",
287 PLI_NAME( p_sys->request.p_item ),
288 PLI_NAME( p_sys->request.p_node ), i_skip );
290 if( p_sys->request.p_node &&
291 p_sys->request.p_node != get_current_status_node( p_playlist ) )
294 set_current_status_node( p_playlist, p_sys->request.p_node );
295 p_sys->request.p_node = NULL;
296 p_sys->b_reset_currently_playing = true;
299 /* If we are asked for a node, go to it's first child */
300 if( i_skip == 0 && ( p_new == NULL || p_new->i_children != -1 ) )
302 i_skip++;
303 if( p_new != NULL )
305 p_new = playlist_GetNextLeaf( p_playlist, p_new, NULL, true, false );
306 for( int i = 0; i < p_playlist->current.i_size; i++ )
308 if( p_new == ARRAY_VAL( p_playlist->current, i ) )
310 p_playlist->i_current_index = i;
311 i_skip = 0;
317 if( p_sys->b_reset_currently_playing )
318 /* A bit too bad to reset twice ... */
319 ResetCurrentlyPlaying( p_playlist, p_new );
320 else if( p_new )
321 ResyncCurrentIndex( p_playlist, p_new );
322 else
323 p_playlist->i_current_index = -1;
325 if( p_playlist->current.i_size && (i_skip > 0) )
327 if( p_playlist->i_current_index < -1 )
328 p_playlist->i_current_index = -1;
329 for( int i = i_skip; i > 0 ; i-- )
331 p_playlist->i_current_index++;
332 if( p_playlist->i_current_index >= p_playlist->current.i_size )
334 PL_DEBUG( "looping - restarting at beginning of node" );
335 /* reshuffle playlist when end is reached */
336 if( var_GetBool( p_playlist, "random" ) ) {
337 PL_DEBUG( "reshuffle playlist" );
338 ResetCurrentlyPlaying( p_playlist,
339 get_current_status_item( p_playlist ) );
341 p_playlist->i_current_index = 0;
344 p_new = ARRAY_VAL( p_playlist->current,
345 p_playlist->i_current_index );
347 else if( p_playlist->current.i_size && (i_skip < 0) )
349 for( int i = i_skip; i < 0 ; i++ )
351 p_playlist->i_current_index--;
352 if( p_playlist->i_current_index <= -1 )
354 PL_DEBUG( "looping - restarting at end of node" );
355 /* reshuffle playlist when beginning is reached */
356 if( var_GetBool( p_playlist, "random" ) ) {
357 PL_DEBUG( "reshuffle playlist" );
358 ResetCurrentlyPlaying( p_playlist,
359 get_current_status_item( p_playlist ) );
361 p_playlist->i_current_index = p_playlist->current.i_size-1;
364 p_new = ARRAY_VAL( p_playlist->current,
365 p_playlist->i_current_index );
367 /* Clear the request */
368 p_sys->request.b_request = false;
370 /* "Automatic" item change ( next ) */
371 else
373 bool b_loop = var_GetBool( p_playlist, "loop" );
374 bool b_repeat = var_GetBool( p_playlist, "repeat" );
375 bool b_playstop = var_InheritBool( p_playlist, "play-and-stop" );
377 /* Repeat and play/stop */
378 if( b_repeat && get_current_status_item( p_playlist ) )
380 msg_Dbg( p_playlist,"repeating item" );
381 return get_current_status_item( p_playlist );
383 if( b_playstop )
385 msg_Dbg( p_playlist,"stopping (play and stop)" );
386 return NULL;
389 /* */
390 if( get_current_status_item( p_playlist ) )
392 playlist_item_t *p_parent = get_current_status_item( p_playlist );
393 while( p_parent )
395 if( p_parent->i_flags & PLAYLIST_SKIP_FLAG )
397 msg_Dbg( p_playlist, "blocking item, stopping") ;
398 return NULL;
400 p_parent = p_parent->p_parent;
404 PL_DEBUG( "changing item without a request (current %i/%i)",
405 p_playlist->i_current_index, p_playlist->current.i_size );
406 /* Cant go to next from current item */
407 if( get_current_status_item( p_playlist ) &&
408 get_current_status_item( p_playlist )->i_flags & PLAYLIST_SKIP_FLAG )
409 return NULL;
411 if( p_sys->b_reset_currently_playing )
412 ResetCurrentlyPlaying( p_playlist,
413 get_current_status_item( p_playlist ) );
415 p_playlist->i_current_index++;
416 assert( p_playlist->i_current_index <= p_playlist->current.i_size );
417 if( p_playlist->i_current_index == p_playlist->current.i_size )
419 if( !b_loop || p_playlist->current.i_size == 0 )
420 return NULL;
421 /* reshuffle after last item has been played */
422 if( var_GetBool( p_playlist, "random" ) ) {
423 PL_DEBUG( "reshuffle playlist" );
424 ResetCurrentlyPlaying( p_playlist,
425 get_current_status_item( p_playlist ) );
427 p_playlist->i_current_index = 0;
429 PL_DEBUG( "using item %i", p_playlist->i_current_index );
430 if ( p_playlist->current.i_size == 0 )
431 return NULL;
433 p_new = ARRAY_VAL( p_playlist->current, p_playlist->i_current_index );
434 /* The new item can't be autoselected */
435 if( p_new != NULL && p_new->i_flags & PLAYLIST_SKIP_FLAG )
436 return NULL;
438 return p_new;
441 static void LoopInput( playlist_t *p_playlist )
443 playlist_private_t *p_sys = pl_priv(p_playlist);
444 input_thread_t *p_input = p_sys->p_input;
446 assert( p_input != NULL );
448 if( p_sys->request.b_request || p_sys->killed )
450 PL_DEBUG( "incoming request - stopping current input" );
451 input_Stop( p_input, true );
454 #warning Unsynchronized access to *p_input flags...
455 /* This input is dead. Remove it ! */
456 if( p_input->b_dead )
458 p_sys->p_input = NULL;
459 PL_DEBUG( "dead input" );
460 PL_UNLOCK;
462 /* WARNING: Input resource manipulation and callback deletion are
463 * incompatible with the playlist lock. */
464 if( !var_InheritBool( p_input, "sout-keep" ) )
465 input_resource_TerminateSout( p_sys->p_input_resource );
466 var_DelCallback( p_input, "intf-event", InputEvent, p_playlist );
468 input_Close( p_input );
469 var_TriggerCallback( p_playlist, "activity" );
470 PL_LOCK;
471 return;
473 /* This input has finished, ask it to die ! */
474 else if( p_input->b_error || p_input->b_eof )
476 PL_DEBUG( "finished input" );
477 input_Stop( p_input, false );
480 vlc_cond_wait( &p_sys->signal, &p_sys->lock );
483 static void LoopRequest( playlist_t *p_playlist, int i_status )
485 playlist_private_t *p_sys = pl_priv(p_playlist);
486 assert( !p_sys->p_input );
488 /* No input. Several cases
489 * - No request, running status -> start new item
490 * - No request, stopped status -> collect garbage
491 * - Request, running requested -> start new item
492 * - Request, stopped requested -> collect garbage
494 if( i_status == PLAYLIST_STOPPED )
496 p_sys->status.i_status = PLAYLIST_STOPPED;
497 vlc_cond_wait( &p_sys->signal, &p_sys->lock );
498 return;
501 playlist_item_t *p_item = NextItem( p_playlist );
502 if( p_item )
504 msg_Dbg( p_playlist, "starting playback of the new playlist item" );
505 ResyncCurrentIndex( p_playlist, p_item );
506 PlayItem( p_playlist, p_item );
507 return;
510 msg_Dbg( p_playlist, "nothing to play" );
511 p_sys->status.i_status = PLAYLIST_STOPPED;
513 if( var_InheritBool( p_playlist, "play-and-exit" ) )
515 msg_Info( p_playlist, "end of playlist, exiting" );
516 libvlc_Quit( p_playlist->p_libvlc );
521 * Run the main control thread itself
523 static void *Thread ( void *data )
525 playlist_t *p_playlist = data;
526 playlist_private_t *p_sys = pl_priv(p_playlist);
528 playlist_Lock( p_playlist );
529 for( ;; )
531 while( p_sys->p_input != NULL )
532 LoopInput( p_playlist );
534 if( p_sys->killed )
535 break; /* THE END */
537 const int status = p_sys->request.b_request ?
538 p_sys->request.i_status : p_sys->status.i_status;
540 /* Destroy any video display if the playlist is supposed to stop */
541 if( status == PLAYLIST_STOPPED
542 && input_resource_HasVout( p_sys->p_input_resource ) )
544 PL_UNLOCK; /* Mind: NO LOCKS while manipulating input resources! */
545 input_resource_TerminateVout( p_sys->p_input_resource );
546 PL_LOCK;
547 continue; /* lost lock = lost state */
550 LoopRequest( p_playlist, status );
552 p_sys->status.i_status = PLAYLIST_STOPPED;
553 playlist_Unlock( p_playlist );
555 input_resource_Terminate( p_sys->p_input_resource );
556 return NULL;