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 "playlist_internal.h"
38 /*****************************************************************************
40 *****************************************************************************/
41 static void *Thread ( void * );
43 /*****************************************************************************
44 * Main functions for the global thread
45 *****************************************************************************/
48 * Creates the main playlist thread.
50 void playlist_Activate( playlist_t
*p_playlist
)
52 playlist_private_t
*p_sys
= pl_priv(p_playlist
);
54 if( vlc_clone( &p_sys
->thread
, Thread
, p_playlist
,
55 VLC_THREAD_PRIORITY_LOW
) )
57 msg_Err( p_playlist
, "cannot spawn playlist thread" );
63 * Stops the playlist forever (but do not destroy it yet).
64 * Any input is stopped.
65 * \return Nothing but waits for the playlist to be deactivated.
67 void playlist_Deactivate( playlist_t
*p_playlist
)
69 playlist_private_t
*p_sys
= pl_priv(p_playlist
);
72 /* WARNING: There is a latent bug. It is assumed that only one thread will
73 * be waiting for playlist deactivation at a time. So far, that works
74 * as playlist_Deactivate() is only ever called while closing an
75 * interface and interfaces are shut down serially by intf_DestroyAll(). */
82 msg_Dbg( p_playlist
, "deactivating the playlist" );
84 vlc_cond_signal( &p_sys
->signal
);
87 vlc_join( p_sys
->thread
, NULL
);
93 static int InputEvent( vlc_object_t
*p_this
, char const *psz_cmd
,
94 vlc_value_t oldval
, vlc_value_t newval
, void *p_data
)
96 VLC_UNUSED(p_this
); VLC_UNUSED(psz_cmd
); VLC_UNUSED(oldval
);
97 playlist_t
*p_playlist
= p_data
;
99 if( newval
.i_int
!= INPUT_EVENT_STATE
&&
100 newval
.i_int
!= INPUT_EVENT_DEAD
)
105 /* XXX: signaling while not changing any parameter... suspicious... */
106 vlc_cond_signal( &pl_priv(p_playlist
)->signal
);
113 * Synchronise the current index of the playlist
114 * to match the index of the current item.
116 * \param p_playlist the playlist structure
117 * \param p_cur the current playlist item
120 void ResyncCurrentIndex( playlist_t
*p_playlist
, playlist_item_t
*p_cur
)
124 PL_DEBUG( "resyncing on %s", PLI_NAME( p_cur
) );
125 /* Simply resync index */
127 p_playlist
->i_current_index
= -1;
128 for( i
= 0 ; i
< p_playlist
->current
.i_size
; i
++ )
130 if( ARRAY_VAL( p_playlist
->current
, i
) == p_cur
)
132 p_playlist
->i_current_index
= i
;
136 PL_DEBUG( "%s is at %i", PLI_NAME( p_cur
), p_playlist
->i_current_index
);
140 * Reset the currently playing playlist.
142 * \param p_playlist the playlist structure
143 * \param p_cur the current playlist item
146 void ResetCurrentlyPlaying( playlist_t
*p_playlist
,
147 playlist_item_t
*p_cur
)
149 playlist_private_t
*p_sys
= pl_priv(p_playlist
);
151 PL_DEBUG( "rebuilding array of current - root %s",
152 PLI_NAME( p_sys
->status
.p_node
) );
153 ARRAY_RESET( p_playlist
->current
);
154 p_playlist
->i_current_index
= -1;
155 for( playlist_item_t
*p_next
= NULL
; ; )
157 /** FIXME: this is *slow* */
158 p_next
= playlist_GetNextLeaf( p_playlist
,
159 p_sys
->status
.p_node
,
160 p_next
, true, false );
164 if( p_next
== p_cur
)
165 p_playlist
->i_current_index
= p_playlist
->current
.i_size
;
166 ARRAY_APPEND( p_playlist
->current
, p_next
);
168 PL_DEBUG("rebuild done - %i items, index %i", p_playlist
->current
.i_size
,
169 p_playlist
->i_current_index
);
171 if( var_GetBool( p_playlist
, "random" ) && ( p_playlist
->current
.i_size
> 0 ) )
173 /* Shuffle the array */
174 for( unsigned j
= p_playlist
->current
.i_size
- 1; j
> 0; j
-- )
176 unsigned i
= vlc_lrand48() % (j
+1); /* between 0 and j */
177 playlist_item_t
*p_tmp
;
178 /* swap the two items */
179 p_tmp
= ARRAY_VAL(p_playlist
->current
, i
);
180 ARRAY_VAL(p_playlist
->current
,i
) = ARRAY_VAL(p_playlist
->current
,j
);
181 ARRAY_VAL(p_playlist
->current
,j
) = p_tmp
;
184 p_sys
->b_reset_currently_playing
= false;
189 * Start the input for an item
191 * \param p_playlist the playlist object
192 * \param p_item the item to play
194 static bool PlayItem( playlist_t
*p_playlist
, playlist_item_t
*p_item
)
196 playlist_private_t
*p_sys
= pl_priv(p_playlist
);
197 input_item_t
*p_input
= p_item
->p_input
;
201 msg_Dbg( p_playlist
, "creating new input thread" );
203 p_item
->i_nb_played
++;
204 set_current_status_item( p_playlist
, p_item
);
205 assert( p_sys
->p_input
== NULL
);
208 libvlc_MetadataCancel( p_playlist
->obj
.libvlc
, p_item
);
210 input_thread_t
*p_input_thread
= input_Create( p_playlist
, p_input
, NULL
,
211 p_sys
->p_input_resource
);
212 if( likely(p_input_thread
!= NULL
) )
214 var_AddCallback( p_input_thread
, "intf-event",
215 InputEvent
, p_playlist
);
217 if( input_Start( p_input_thread
) )
219 var_DelCallback( p_input_thread
, "intf-event",
220 InputEvent
, p_playlist
);
221 vlc_object_release( p_input_thread
);
222 p_input_thread
= NULL
;
226 /* TODO store art policy in playlist private data */
227 char *psz_arturl
= input_item_GetArtURL( p_input
);
228 /* p_input->p_meta should not be null after a successful CreateThread */
229 bool b_has_art
= !EMPTY_STR( psz_arturl
);
231 if( !b_has_art
|| strncmp( psz_arturl
, "attachment://", 13 ) )
233 PL_DEBUG( "requesting art for new input thread" );
234 libvlc_ArtRequest( p_playlist
->obj
.libvlc
, p_input
, META_REQUEST_OPTION_NONE
);
239 p_sys
->p_input
= p_input_thread
;
242 var_SetAddress( p_playlist
, "input-current", p_input_thread
);
245 return p_input_thread
!= NULL
;
249 * Compute the next playlist item depending on
250 * the playlist course mode (forward, backward, random, view,...).
252 * \param p_playlist the playlist object
255 static playlist_item_t
*NextItem( playlist_t
*p_playlist
)
257 playlist_private_t
*p_sys
= pl_priv(p_playlist
);
258 playlist_item_t
*p_new
= NULL
;
259 bool requested
= p_sys
->request
.b_request
;
261 /* Clear the request */
262 p_sys
->request
.b_request
= false;
264 /* Handle quickly a few special cases */
265 /* No items to play */
266 if( p_playlist
->items
.i_size
== 0 )
268 msg_Info( p_playlist
, "playlist is empty" );
272 /* Start the real work */
275 p_new
= p_sys
->request
.p_item
;
277 if( p_new
== NULL
&& p_sys
->request
.p_node
== NULL
)
278 return NULL
; /* Stop request! */
280 int i_skip
= p_sys
->request
.i_skip
;
281 PL_DEBUG( "processing request item: %s, node: %s, skip: %i",
282 PLI_NAME( p_sys
->request
.p_item
),
283 PLI_NAME( p_sys
->request
.p_node
), i_skip
);
285 if( p_sys
->request
.p_node
&&
286 p_sys
->request
.p_node
!= get_current_status_node( p_playlist
) )
289 set_current_status_node( p_playlist
, p_sys
->request
.p_node
);
290 p_sys
->request
.p_node
= NULL
;
291 p_sys
->b_reset_currently_playing
= true;
294 /* If we are asked for a node, go to it's first child */
295 if( i_skip
== 0 && ( p_new
== NULL
|| p_new
->i_children
!= -1 ) )
300 p_new
= playlist_GetNextLeaf( p_playlist
, p_new
, NULL
, true, false );
301 for( int i
= 0; i
< p_playlist
->current
.i_size
; i
++ )
303 if( p_new
== ARRAY_VAL( p_playlist
->current
, i
) )
305 p_playlist
->i_current_index
= i
;
312 if( p_sys
->b_reset_currently_playing
)
313 /* A bit too bad to reset twice ... */
314 ResetCurrentlyPlaying( p_playlist
, p_new
);
316 ResyncCurrentIndex( p_playlist
, p_new
);
318 p_playlist
->i_current_index
= -1;
320 if( p_playlist
->current
.i_size
&& (i_skip
> 0) )
322 if( p_playlist
->i_current_index
< -1 )
323 p_playlist
->i_current_index
= -1;
324 for( int i
= i_skip
; i
> 0 ; i
-- )
326 p_playlist
->i_current_index
++;
327 if( p_playlist
->i_current_index
>= p_playlist
->current
.i_size
)
329 PL_DEBUG( "looping - restarting at beginning of node" );
330 /* reshuffle playlist when end is reached */
331 if( var_GetBool( p_playlist
, "random" ) ) {
332 PL_DEBUG( "reshuffle playlist" );
333 ResetCurrentlyPlaying( p_playlist
,
334 get_current_status_item( p_playlist
) );
336 p_playlist
->i_current_index
= 0;
339 p_new
= ARRAY_VAL( p_playlist
->current
,
340 p_playlist
->i_current_index
);
342 else if( p_playlist
->current
.i_size
&& (i_skip
< 0) )
344 for( int i
= i_skip
; i
< 0 ; i
++ )
346 p_playlist
->i_current_index
--;
347 if( p_playlist
->i_current_index
<= -1 )
349 PL_DEBUG( "looping - restarting at end of node" );
350 /* reshuffle playlist when beginning is reached */
351 if( var_GetBool( p_playlist
, "random" ) ) {
352 PL_DEBUG( "reshuffle playlist" );
353 ResetCurrentlyPlaying( p_playlist
,
354 get_current_status_item( p_playlist
) );
356 p_playlist
->i_current_index
= p_playlist
->current
.i_size
-1;
359 p_new
= ARRAY_VAL( p_playlist
->current
,
360 p_playlist
->i_current_index
);
363 /* "Automatic" item change ( next ) */
366 bool b_loop
= var_GetBool( p_playlist
, "loop" );
367 bool b_repeat
= var_GetBool( p_playlist
, "repeat" );
368 bool b_playstop
= var_InheritBool( p_playlist
, "play-and-stop" );
370 /* Repeat and play/stop */
371 if( b_repeat
&& get_current_status_item( p_playlist
) )
373 msg_Dbg( p_playlist
,"repeating item" );
374 return get_current_status_item( p_playlist
);
376 if( b_playstop
&& get_current_status_item( p_playlist
) )
378 msg_Dbg( p_playlist
,"stopping (play and stop)" );
384 PL_DEBUG( "changing item without a request (current %i/%i)",
385 p_playlist
->i_current_index
, p_playlist
->current
.i_size
);
386 /* Can't go to next from current item */
387 if( p_sys
->b_reset_currently_playing
)
388 ResetCurrentlyPlaying( p_playlist
,
389 get_current_status_item( p_playlist
) );
391 p_playlist
->i_current_index
++;
392 assert( p_playlist
->i_current_index
<= p_playlist
->current
.i_size
);
393 if( p_playlist
->i_current_index
== p_playlist
->current
.i_size
)
395 if( !b_loop
|| p_playlist
->current
.i_size
== 0 )
397 /* reshuffle after last item has been played */
398 if( var_GetBool( p_playlist
, "random" ) ) {
399 PL_DEBUG( "reshuffle playlist" );
400 ResetCurrentlyPlaying( p_playlist
,
401 get_current_status_item( p_playlist
) );
403 p_playlist
->i_current_index
= 0;
405 PL_DEBUG( "using item %i", p_playlist
->i_current_index
);
406 if ( p_playlist
->current
.i_size
== 0 )
409 p_new
= ARRAY_VAL( p_playlist
->current
, p_playlist
->i_current_index
);
414 static void LoopInput( playlist_t
*p_playlist
)
416 playlist_private_t
*p_sys
= pl_priv(p_playlist
);
417 input_thread_t
*p_input
= p_sys
->p_input
;
419 assert( p_input
!= NULL
);
421 if( p_sys
->request
.b_request
|| p_sys
->killed
)
423 PL_DEBUG( "incoming request - stopping current input" );
424 input_Stop( p_input
);
427 switch( var_GetInteger( p_input
, "state" ) )
431 /* This input is dead. Remove it ! */
432 p_sys
->p_input
= NULL
;
433 PL_DEBUG( "dead input" );
436 var_SetAddress( p_playlist
, "input-current", NULL
);
438 /* WARNING: Input resource manipulation and callback deletion are
439 * incompatible with the playlist lock. */
440 if( !var_InheritBool( p_input
, "sout-keep" ) )
441 input_resource_TerminateSout( p_sys
->p_input_resource
);
442 var_DelCallback( p_input
, "intf-event", InputEvent
, p_playlist
);
444 input_Close( p_input
);
448 vlc_cond_wait( &p_sys
->signal
, &p_sys
->lock
);
452 static bool Next( playlist_t
*p_playlist
)
454 playlist_item_t
*p_item
= NextItem( p_playlist
);
458 msg_Dbg( p_playlist
, "starting playback of new item" );
459 ResyncCurrentIndex( p_playlist
, p_item
);
460 return PlayItem( p_playlist
, p_item
);
464 * Run the main control thread itself
466 static void *Thread ( void *data
)
468 playlist_t
*p_playlist
= data
;
469 playlist_private_t
*p_sys
= pl_priv(p_playlist
);
472 while( !p_sys
->killed
)
474 /* Playlist in stopped state */
475 assert(p_sys
->p_input
== NULL
);
477 if( !p_sys
->request
.b_request
)
479 vlc_cond_wait( &p_sys
->signal
, &p_sys
->lock
);
483 while( !p_sys
->killed
&& Next( p_playlist
) )
484 { /* Playlist in running state */
485 assert(p_sys
->p_input
!= NULL
);
488 LoopInput( p_playlist
);
489 while( p_sys
->p_input
!= NULL
);
492 msg_Dbg( p_playlist
, "nothing to play" );
493 if( var_InheritBool( p_playlist
, "play-and-exit" ) )
495 msg_Info( p_playlist
, "end of playlist, exiting" );
496 libvlc_Quit( p_playlist
->obj
.libvlc
);
499 /* Destroy any video display now (XXX: ugly hack) */
500 if( input_resource_HasVout( p_sys
->p_input_resource
) )
502 PL_UNLOCK
; /* Mind: NO LOCKS while manipulating input resources! */
503 input_resource_TerminateVout( p_sys
->p_input_resource
);
509 input_resource_Terminate( p_sys
->p_input_resource
);