1 /*****************************************************************************
2 * control.c : Handle control of the playlist & running through it
3 *****************************************************************************
4 * Copyright (C) 1999-2004 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>
29 #include "vlc_playlist.h"
30 #include "playlist_internal.h"
33 /*****************************************************************************
35 *****************************************************************************/
36 static int PlaylistVAControl( playlist_t * p_playlist, int i_query, va_list args );
38 static void PreparseEnqueueItemSub( playlist_t *, playlist_item_t * );
40 /*****************************************************************************
42 *****************************************************************************/
44 playlist_t *__pl_Yield( vlc_object_t *p_this )
46 playlist_t *pl = libvlc_priv (p_this->p_libvlc)->p_playlist;
48 vlc_object_yield( pl );
52 void __pl_Release( vlc_object_t *p_this )
54 playlist_t *pl = libvlc_priv (p_this->p_libvlc)->p_playlist;
56 vlc_object_release( pl );
59 int playlist_Control( playlist_t * p_playlist, int i_query,
64 va_start( args, b_locked );
65 if( !b_locked ) PL_LOCK;
66 i_result = PlaylistVAControl( p_playlist, i_query, args );
68 if( !b_locked ) PL_UNLOCK;
73 static int PlaylistVAControl( playlist_t * p_playlist, int i_query, va_list args )
75 playlist_item_t *p_item, *p_node;
78 if( playlist_IsEmpty( p_playlist ) )
84 p_playlist->request.i_status = PLAYLIST_STOPPED;
85 p_playlist->request.b_request = true;
86 p_playlist->request.p_item = NULL;
89 // Node can be null, it will keep the same. Use with care ...
90 // Item null = take the first child of node
91 case PLAYLIST_VIEWPLAY:
92 p_node = (playlist_item_t *)va_arg( args, playlist_item_t * );
93 p_item = (playlist_item_t *)va_arg( args, playlist_item_t * );
96 p_node = p_playlist->status.p_node;
99 p_playlist->request.i_status = PLAYLIST_RUNNING;
100 p_playlist->request.i_skip = 0;
101 p_playlist->request.b_request = true;
102 p_playlist->request.p_node = p_node;
103 p_playlist->request.p_item = p_item;
104 if( p_item && var_GetBool( p_playlist, "random" ) )
105 p_playlist->b_reset_currently_playing = true;
109 if( p_playlist->p_input )
111 val.i_int = PLAYING_S;
112 var_Set( p_playlist->p_input, "state", val );
117 p_playlist->request.i_status = PLAYLIST_RUNNING;
118 p_playlist->request.b_request = true;
119 p_playlist->request.p_node = p_playlist->status.p_node;
120 p_playlist->request.p_item = p_playlist->status.p_item;
121 p_playlist->request.i_skip = 0;
127 if( p_playlist->p_input )
128 var_Get( p_playlist->p_input, "state", &val );
130 if( val.i_int == PAUSE_S )
132 p_playlist->status.i_status = PLAYLIST_RUNNING;
133 if( p_playlist->p_input )
135 val.i_int = PLAYING_S;
136 var_Set( p_playlist->p_input, "state", val );
141 p_playlist->status.i_status = PLAYLIST_PAUSED;
142 if( p_playlist->p_input )
145 var_Set( p_playlist->p_input, "state", val );
151 p_playlist->request.p_node = p_playlist->status.p_node;
152 p_playlist->request.p_item = p_playlist->status.p_item;
153 p_playlist->request.i_skip = (int) va_arg( args, int );
154 /* if already running, keep running */
155 if( p_playlist->status.i_status != PLAYLIST_STOPPED )
156 p_playlist->request.i_status = p_playlist->status.i_status;
157 p_playlist->request.b_request = true;
161 msg_Err( p_playlist, "unknown playlist query" );
165 vlc_cond_signal( &p_playlist->object_wait );
170 /*****************************************************************************
172 *****************************************************************************/
173 /** Enqueue an item for preparsing */
174 int playlist_PreparseEnqueue( playlist_t *p_playlist,
175 input_item_t *p_item )
177 vlc_mutex_lock( &p_playlist->p_preparse->object_lock );
178 vlc_gc_incref( p_item );
179 INSERT_ELEM( p_playlist->p_preparse->pp_waiting,
180 p_playlist->p_preparse->i_waiting,
181 p_playlist->p_preparse->i_waiting,
183 vlc_cond_signal( &p_playlist->p_preparse->object_wait );
184 vlc_mutex_unlock( &p_playlist->p_preparse->object_lock );
188 /** Enqueue a playlist item or a node for peparsing.
189 * This function should be entered without playlist and preparser locks */
190 int playlist_PreparseEnqueueItem( playlist_t *p_playlist,
191 playlist_item_t *p_item )
193 vlc_mutex_lock( &p_playlist->object_lock );
194 vlc_mutex_lock( &p_playlist->p_preparse->object_lock );
195 PreparseEnqueueItemSub( p_playlist, p_item );
196 vlc_mutex_unlock( &p_playlist->p_preparse->object_lock );
197 vlc_mutex_unlock( &p_playlist->object_lock );
201 int playlist_AskForArtEnqueue( playlist_t *p_playlist,
202 input_item_t *p_item )
206 vlc_object_lock( p_playlist->p_fetcher );
207 for( i = 0; i < p_playlist->p_fetcher->i_waiting ; i++ );
208 vlc_gc_incref( p_item );
209 INSERT_ELEM( p_playlist->p_fetcher->pp_waiting,
210 p_playlist->p_fetcher->i_waiting,
212 vlc_object_signal_unlocked( p_playlist->p_fetcher );
213 vlc_object_unlock( p_playlist->p_fetcher );
217 static void PreparseEnqueueItemSub( playlist_t *p_playlist,
218 playlist_item_t *p_item )
221 if( p_item->i_children == -1 )
223 vlc_gc_incref( p_item );
224 INSERT_ELEM( p_playlist->p_preparse->pp_waiting,
225 p_playlist->p_preparse->i_waiting,
226 p_playlist->p_preparse->i_waiting,
231 for( i = 0; i < p_item->i_children; i++)
233 PreparseEnqueueItemSub( p_playlist, p_item->pp_children[i] );
238 /*****************************************************************************
240 *****************************************************************************/
243 * Synchronise the current index of the playlist
244 * to match the index of the current item.
246 * \param p_playlist the playlist structure
247 * \param p_cur the current playlist item
250 static void ResyncCurrentIndex( playlist_t *p_playlist, playlist_item_t *p_cur )
252 PL_DEBUG( "resyncing on %s", PLI_NAME( p_cur ) );
253 /* Simply resync index */
255 p_playlist->i_current_index = -1;
256 for( i = 0 ; i< p_playlist->current.i_size; i++ )
258 if( ARRAY_VAL( p_playlist->current, i ) == p_cur )
260 p_playlist->i_current_index = i;
264 PL_DEBUG( "%s is at %i", PLI_NAME( p_cur ), p_playlist->i_current_index );
267 void ResetCurrentlyPlaying( playlist_t *p_playlist, bool b_random,
268 playlist_item_t *p_cur )
270 playlist_item_t *p_next = NULL;
271 stats_TimerStart( p_playlist, "Items array build",
272 STATS_TIMER_PLAYLIST_BUILD );
273 PL_DEBUG( "rebuilding array of current - root %s",
274 PLI_NAME( p_playlist->status.p_node ) );
275 ARRAY_RESET( p_playlist->current );
276 p_playlist->i_current_index = -1;
279 /** FIXME: this is *slow* */
280 p_next = playlist_GetNextLeaf( p_playlist,
281 p_playlist->status.p_node,
282 p_next, true, false );
285 if( p_next == p_cur )
286 p_playlist->i_current_index = p_playlist->current.i_size;
287 ARRAY_APPEND( p_playlist->current, p_next);
291 PL_DEBUG("rebuild done - %i items, index %i", p_playlist->current.i_size,
292 p_playlist->i_current_index);
295 /* Shuffle the array */
296 srand( (unsigned int)mdate() );
298 for( j = p_playlist->current.i_size - 1; j > 0; j-- )
300 int i = rand() % (j+1); /* between 0 and j */
301 playlist_item_t *p_tmp;
302 /* swap the two items */
303 p_tmp = ARRAY_VAL(p_playlist->current, i);
304 ARRAY_VAL(p_playlist->current,i) = ARRAY_VAL(p_playlist->current,j);
305 ARRAY_VAL(p_playlist->current,j) = p_tmp;
308 p_playlist->b_reset_currently_playing = false;
309 stats_TimerStop( p_playlist, STATS_TIMER_PLAYLIST_BUILD );
313 * Compute the next playlist item depending on
314 * the playlist course mode (forward, backward, random, view,...).
316 * \param p_playlist the playlist object
319 playlist_item_t * playlist_NextItem( playlist_t *p_playlist )
321 playlist_item_t *p_new = NULL;
324 bool b_loop = var_GetBool( p_playlist, "loop" );
325 bool b_random = var_GetBool( p_playlist, "random" );
326 bool b_repeat = var_GetBool( p_playlist, "repeat" );
327 bool b_playstop = var_GetBool( p_playlist, "play-and-stop" );
329 /* Handle quickly a few special cases */
330 /* No items to play */
331 if( p_playlist->items.i_size == 0 )
333 msg_Info( p_playlist, "playlist is empty" );
337 /* Repeat and play/stop */
338 if( !p_playlist->request.b_request && b_repeat == true &&
339 p_playlist->status.p_item )
341 msg_Dbg( p_playlist,"repeating item" );
342 return p_playlist->status.p_item;
344 if( !p_playlist->request.b_request && b_playstop == true )
346 msg_Dbg( p_playlist,"stopping (play and stop)" );
350 if( !p_playlist->request.b_request && p_playlist->status.p_item )
352 playlist_item_t *p_parent = p_playlist->status.p_item;
355 if( p_parent->i_flags & PLAYLIST_SKIP_FLAG )
357 msg_Dbg( p_playlist, "blocking item, stopping") ;
360 p_parent = p_parent->p_parent;
364 /* Start the real work */
365 if( p_playlist->request.b_request )
367 p_new = p_playlist->request.p_item;
368 i_skip = p_playlist->request.i_skip;
369 PL_DEBUG( "processing request item %s node %s skip %i",
370 PLI_NAME( p_playlist->request.p_item ),
371 PLI_NAME( p_playlist->request.p_node ), i_skip );
373 if( p_playlist->request.p_node &&
374 p_playlist->request.p_node != p_playlist->status.p_node )
376 p_playlist->status.p_node = p_playlist->request.p_node;
377 p_playlist->b_reset_currently_playing = true;
380 /* If we are asked for a node, don't take it */
381 if( i_skip == 0 && ( p_new == NULL || p_new->i_children != -1 ) )
384 if( p_playlist->b_reset_currently_playing )
385 /* A bit too bad to reset twice ... */
386 ResetCurrentlyPlaying( p_playlist, b_random, p_new );
388 ResyncCurrentIndex( p_playlist, p_new );
390 p_playlist->i_current_index = -1;
392 if( p_playlist->current.i_size && (i_skip > 0) )
394 if( p_playlist->i_current_index < -1 )
395 p_playlist->i_current_index = -1;
396 for( i = i_skip; i > 0 ; i-- )
398 p_playlist->i_current_index++;
399 if( p_playlist->i_current_index >= p_playlist->current.i_size )
401 PL_DEBUG( "looping - restarting at beginning of node" );
402 p_playlist->i_current_index = 0;
405 p_new = ARRAY_VAL( p_playlist->current,
406 p_playlist->i_current_index );
408 else if( p_playlist->current.i_size && (i_skip < 0) )
410 for( i = i_skip; i < 0 ; i++ )
412 p_playlist->i_current_index--;
413 if( p_playlist->i_current_index <= -1 )
415 PL_DEBUG( "looping - restarting at end of node" );
416 p_playlist->i_current_index = p_playlist->current.i_size-1;
419 p_new = ARRAY_VAL( p_playlist->current,
420 p_playlist->i_current_index );
422 /* Clear the request */
423 p_playlist->request.b_request = false;
425 /* "Automatic" item change ( next ) */
428 PL_DEBUG( "changing item without a request (current %i/%i)",
429 p_playlist->i_current_index, p_playlist->current.i_size );
430 /* Cant go to next from current item */
431 if( p_playlist->status.p_item &&
432 p_playlist->status.p_item->i_flags & PLAYLIST_SKIP_FLAG )
435 if( p_playlist->b_reset_currently_playing )
436 ResetCurrentlyPlaying( p_playlist, b_random,
437 p_playlist->status.p_item );
439 p_playlist->i_current_index++;
440 assert( p_playlist->i_current_index <= p_playlist->current.i_size );
441 if( p_playlist->i_current_index == p_playlist->current.i_size )
443 if( !b_loop || p_playlist->current.i_size == 0 ) return NULL;
444 p_playlist->i_current_index = 0;
446 PL_DEBUG( "using item %i", p_playlist->i_current_index );
447 if ( p_playlist->current.i_size == 0 ) return NULL;
449 p_new = ARRAY_VAL( p_playlist->current, p_playlist->i_current_index );
450 /* The new item can't be autoselected */
451 if( p_new != NULL && p_new->i_flags & PLAYLIST_SKIP_FLAG )
458 * Start the input for an item
460 * \param p_playlist the playlist objetc
461 * \param p_item the item to play
464 int playlist_PlayItem( playlist_t *p_playlist, playlist_item_t *p_item )
466 input_item_t *p_input = p_item->p_input;
467 int i_activity = var_GetInteger( p_playlist, "activity" ) ;
469 msg_Dbg( p_playlist, "creating new input thread" );
471 p_input->i_nb_played++;
472 p_playlist->status.p_item = p_item;
474 p_playlist->status.i_status = PLAYLIST_RUNNING;
476 var_SetInteger( p_playlist, "activity", i_activity +
477 DEFAULT_INPUT_ACTIVITY );
478 p_playlist->p_input = input_CreateThread( p_playlist, p_input );
480 char *psz_uri = input_item_GetURI( p_item->p_input );
481 if( psz_uri && ( !strncmp( psz_uri, "directory:", 10 ) ||
482 !strncmp( psz_uri, "vlc:", 4 ) ) )
489 if( p_playlist->p_fetcher &&
490 p_playlist->p_fetcher->i_art_policy == ALBUM_ART_WHEN_PLAYED )
494 char *psz_arturl, *psz_name;
495 psz_arturl = input_item_GetArtURL( p_input );
496 psz_name = input_item_GetName( p_input );
498 /* p_input->p_meta should not be null after a successfull CreateThread */
499 b_has_art = !EMPTY_STR( psz_arturl );
501 if( !b_has_art || strncmp( psz_arturl, "attachment://", 13 ) )
503 PL_DEBUG( "requesting art for %s", psz_name );
504 playlist_AskForArtEnqueue( p_playlist, p_input );
511 var_SetInteger( p_playlist, "playlist-current", p_input->i_id );