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 )
49 pl = libvlc_priv (p_this->p_libvlc)->p_playlist;
51 vlc_object_yield (pl);
55 void __pl_Release( vlc_object_t *p_this )
57 playlist_t *pl = libvlc_priv (p_this->p_libvlc)->p_playlist;
59 vlc_object_release( pl );
62 int playlist_Control( playlist_t * p_playlist, int i_query,
67 va_start( args, b_locked );
68 if( !b_locked ) PL_LOCK;
69 i_result = PlaylistVAControl( p_playlist, i_query, args );
71 if( !b_locked ) PL_UNLOCK;
76 static int PlaylistVAControl( playlist_t * p_playlist, int i_query, va_list args )
78 playlist_item_t *p_item, *p_node;
81 if( !vlc_object_alive( p_playlist ) )
84 if( playlist_IsEmpty( p_playlist ) )
90 p_playlist->request.i_status = PLAYLIST_STOPPED;
91 p_playlist->request.b_request = true;
92 p_playlist->request.p_item = NULL;
95 // Node can be null, it will keep the same. Use with care ...
96 // Item null = take the first child of node
97 case PLAYLIST_VIEWPLAY:
98 p_node = (playlist_item_t *)va_arg( args, playlist_item_t * );
99 p_item = (playlist_item_t *)va_arg( args, playlist_item_t * );
100 if ( p_node == NULL )
102 p_node = p_playlist->status.p_node;
105 p_playlist->request.i_status = PLAYLIST_RUNNING;
106 p_playlist->request.i_skip = 0;
107 p_playlist->request.b_request = true;
108 p_playlist->request.p_node = p_node;
109 p_playlist->request.p_item = p_item;
110 if( p_item && var_GetBool( p_playlist, "random" ) )
111 p_playlist->b_reset_currently_playing = true;
115 if( p_playlist->p_input )
117 val.i_int = PLAYING_S;
118 var_Set( p_playlist->p_input, "state", val );
123 p_playlist->request.i_status = PLAYLIST_RUNNING;
124 p_playlist->request.b_request = true;
125 p_playlist->request.p_node = p_playlist->status.p_node;
126 p_playlist->request.p_item = p_playlist->status.p_item;
127 p_playlist->request.i_skip = 0;
133 if( p_playlist->p_input )
134 var_Get( p_playlist->p_input, "state", &val );
136 if( val.i_int == PAUSE_S )
138 p_playlist->status.i_status = PLAYLIST_RUNNING;
139 if( p_playlist->p_input )
141 val.i_int = PLAYING_S;
142 var_Set( p_playlist->p_input, "state", val );
147 p_playlist->status.i_status = PLAYLIST_PAUSED;
148 if( p_playlist->p_input )
151 var_Set( p_playlist->p_input, "state", val );
157 p_playlist->request.p_node = p_playlist->status.p_node;
158 p_playlist->request.p_item = p_playlist->status.p_item;
159 p_playlist->request.i_skip = (int) va_arg( args, int );
160 /* if already running, keep running */
161 if( p_playlist->status.i_status != PLAYLIST_STOPPED )
162 p_playlist->request.i_status = p_playlist->status.i_status;
163 p_playlist->request.b_request = true;
167 msg_Err( p_playlist, "unknown playlist query" );
171 vlc_object_signal_maybe( VLC_OBJECT(p_playlist) );
176 /*****************************************************************************
178 *****************************************************************************/
179 /** Enqueue an item for preparsing */
180 int playlist_PreparseEnqueue( playlist_t *p_playlist,
181 input_item_t *p_item )
183 vlc_object_lock( p_playlist->p_preparse );
184 if( !vlc_object_alive( p_playlist->p_preparse ) )
186 vlc_object_unlock( p_playlist->p_preparse );
189 vlc_gc_incref( p_item );
190 INSERT_ELEM( p_playlist->p_preparse->pp_waiting,
191 p_playlist->p_preparse->i_waiting,
192 p_playlist->p_preparse->i_waiting,
194 vlc_object_signal_unlocked( p_playlist->p_preparse );
195 vlc_object_unlock( p_playlist->p_preparse );
199 /** Enqueue a playlist item or a node for peparsing.
200 * This function should be entered without playlist and preparser locks */
201 int playlist_PreparseEnqueueItem( playlist_t *p_playlist,
202 playlist_item_t *p_item )
204 vlc_object_lock( p_playlist );
205 vlc_object_lock( p_playlist->p_preparse );
206 if( !vlc_object_alive( p_playlist->p_preparse ) )
208 vlc_object_unlock( p_playlist->p_preparse );
209 vlc_object_unlock( p_playlist );
212 PreparseEnqueueItemSub( p_playlist, p_item );
213 vlc_object_unlock( p_playlist->p_preparse );
214 vlc_object_unlock( p_playlist );
218 int playlist_AskForArtEnqueue( playlist_t *p_playlist,
219 input_item_t *p_item )
223 vlc_object_lock( p_playlist->p_fetcher );
224 if( !vlc_object_alive( p_playlist->p_fetcher ) )
226 vlc_object_unlock( p_playlist->p_fetcher );
230 for( i = 0; i < p_playlist->p_fetcher->i_waiting ; i++ );
231 vlc_gc_incref( p_item );
232 INSERT_ELEM( p_playlist->p_fetcher->pp_waiting,
233 p_playlist->p_fetcher->i_waiting,
235 vlc_object_signal_unlocked( p_playlist->p_fetcher );
236 vlc_object_unlock( p_playlist->p_fetcher );
240 static void PreparseEnqueueItemSub( playlist_t *p_playlist,
241 playlist_item_t *p_item )
244 if( p_item->i_children == -1 )
246 vlc_gc_incref( p_item );
247 INSERT_ELEM( p_playlist->p_preparse->pp_waiting,
248 p_playlist->p_preparse->i_waiting,
249 p_playlist->p_preparse->i_waiting,
254 for( i = 0; i < p_item->i_children; i++)
256 PreparseEnqueueItemSub( p_playlist, p_item->pp_children[i] );
261 /*****************************************************************************
263 *****************************************************************************/
266 * Synchronise the current index of the playlist
267 * to match the index of the current item.
269 * \param p_playlist the playlist structure
270 * \param p_cur the current playlist item
273 static void ResyncCurrentIndex( playlist_t *p_playlist, playlist_item_t *p_cur )
275 PL_DEBUG( "resyncing on %s", PLI_NAME( p_cur ) );
276 /* Simply resync index */
278 p_playlist->i_current_index = -1;
279 for( i = 0 ; i< p_playlist->current.i_size; i++ )
281 if( ARRAY_VAL( p_playlist->current, i ) == p_cur )
283 p_playlist->i_current_index = i;
287 PL_DEBUG( "%s is at %i", PLI_NAME( p_cur ), p_playlist->i_current_index );
290 void ResetCurrentlyPlaying( playlist_t *p_playlist, bool b_random,
291 playlist_item_t *p_cur )
293 playlist_item_t *p_next = NULL;
294 stats_TimerStart( p_playlist, "Items array build",
295 STATS_TIMER_PLAYLIST_BUILD );
296 PL_DEBUG( "rebuilding array of current - root %s",
297 PLI_NAME( p_playlist->status.p_node ) );
298 ARRAY_RESET( p_playlist->current );
299 p_playlist->i_current_index = -1;
302 /** FIXME: this is *slow* */
303 p_next = playlist_GetNextLeaf( p_playlist,
304 p_playlist->status.p_node,
305 p_next, true, false );
308 if( p_next == p_cur )
309 p_playlist->i_current_index = p_playlist->current.i_size;
310 ARRAY_APPEND( p_playlist->current, p_next);
314 PL_DEBUG("rebuild done - %i items, index %i", p_playlist->current.i_size,
315 p_playlist->i_current_index);
318 /* Shuffle the array */
319 srand( (unsigned int)mdate() );
321 for( j = p_playlist->current.i_size - 1; j > 0; j-- )
323 int i = rand() % (j+1); /* between 0 and j */
324 playlist_item_t *p_tmp;
325 /* swap the two items */
326 p_tmp = ARRAY_VAL(p_playlist->current, i);
327 ARRAY_VAL(p_playlist->current,i) = ARRAY_VAL(p_playlist->current,j);
328 ARRAY_VAL(p_playlist->current,j) = p_tmp;
331 p_playlist->b_reset_currently_playing = false;
332 stats_TimerStop( p_playlist, STATS_TIMER_PLAYLIST_BUILD );
336 * Compute the next playlist item depending on
337 * the playlist course mode (forward, backward, random, view,...).
339 * \param p_playlist the playlist object
342 playlist_item_t * playlist_NextItem( playlist_t *p_playlist )
344 playlist_item_t *p_new = NULL;
347 bool b_loop = var_GetBool( p_playlist, "loop" );
348 bool b_random = var_GetBool( p_playlist, "random" );
349 bool b_repeat = var_GetBool( p_playlist, "repeat" );
350 bool b_playstop = var_GetBool( p_playlist, "play-and-stop" );
352 /* Handle quickly a few special cases */
353 /* No items to play */
354 if( p_playlist->items.i_size == 0 )
356 msg_Info( p_playlist, "playlist is empty" );
360 /* Repeat and play/stop */
361 if( !p_playlist->request.b_request && b_repeat == true &&
362 p_playlist->status.p_item )
364 msg_Dbg( p_playlist,"repeating item" );
365 return p_playlist->status.p_item;
367 if( !p_playlist->request.b_request && b_playstop == true )
369 msg_Dbg( p_playlist,"stopping (play and stop)" );
373 if( !p_playlist->request.b_request && p_playlist->status.p_item )
375 playlist_item_t *p_parent = p_playlist->status.p_item;
378 if( p_parent->i_flags & PLAYLIST_SKIP_FLAG )
380 msg_Dbg( p_playlist, "blocking item, stopping") ;
383 p_parent = p_parent->p_parent;
387 /* Start the real work */
388 if( p_playlist->request.b_request )
390 p_new = p_playlist->request.p_item;
391 i_skip = p_playlist->request.i_skip;
392 PL_DEBUG( "processing request item %s node %s skip %i",
393 PLI_NAME( p_playlist->request.p_item ),
394 PLI_NAME( p_playlist->request.p_node ), i_skip );
396 /* Make sure the node wasn't deleted */
397 if( p_playlist->status.p_node &&
398 p_playlist->status.p_node->i_flags & PLAYLIST_REMOVE_FLAG )
400 PL_DEBUG( "%s was marked for deletion, deleting",
401 PLI_NAME( p_playlist->status.p_node ) );
402 playlist_ItemDelete( p_playlist->status.p_node );
403 /* Don't attempt to reuse that node */
404 if( p_playlist->status.p_node == p_playlist->request.p_node )
405 p_playlist->request.p_node = NULL;
406 p_playlist->status.p_node = NULL;
409 if( p_playlist->request.p_node &&
410 p_playlist->request.p_node != p_playlist->status.p_node )
413 p_playlist->status.p_node = p_playlist->request.p_node;
414 p_playlist->b_reset_currently_playing = true;
417 /* If we are asked for a node, don't take it */
418 if( i_skip == 0 && ( p_new == NULL || p_new->i_children != -1 ) )
421 if( p_playlist->b_reset_currently_playing )
422 /* A bit too bad to reset twice ... */
423 ResetCurrentlyPlaying( p_playlist, b_random, p_new );
425 ResyncCurrentIndex( p_playlist, p_new );
427 p_playlist->i_current_index = -1;
429 if( p_playlist->current.i_size && (i_skip > 0) )
431 if( p_playlist->i_current_index < -1 )
432 p_playlist->i_current_index = -1;
433 for( i = i_skip; i > 0 ; i-- )
435 p_playlist->i_current_index++;
436 if( p_playlist->i_current_index >= p_playlist->current.i_size )
438 PL_DEBUG( "looping - restarting at beginning of node" );
439 p_playlist->i_current_index = 0;
442 p_new = ARRAY_VAL( p_playlist->current,
443 p_playlist->i_current_index );
445 else if( p_playlist->current.i_size && (i_skip < 0) )
447 for( i = i_skip; i < 0 ; i++ )
449 p_playlist->i_current_index--;
450 if( p_playlist->i_current_index <= -1 )
452 PL_DEBUG( "looping - restarting at end of node" );
453 p_playlist->i_current_index = p_playlist->current.i_size-1;
456 p_new = ARRAY_VAL( p_playlist->current,
457 p_playlist->i_current_index );
459 /* Clear the request */
460 p_playlist->request.b_request = false;
462 /* "Automatic" item change ( next ) */
465 PL_DEBUG( "changing item without a request (current %i/%i)",
466 p_playlist->i_current_index, p_playlist->current.i_size );
467 /* Cant go to next from current item */
468 if( p_playlist->status.p_item &&
469 p_playlist->status.p_item->i_flags & PLAYLIST_SKIP_FLAG )
472 if( p_playlist->b_reset_currently_playing )
473 ResetCurrentlyPlaying( p_playlist, b_random,
474 p_playlist->status.p_item );
476 p_playlist->i_current_index++;
477 assert( p_playlist->i_current_index <= p_playlist->current.i_size );
478 if( p_playlist->i_current_index == p_playlist->current.i_size )
480 if( !b_loop || p_playlist->current.i_size == 0 ) return NULL;
481 p_playlist->i_current_index = 0;
483 PL_DEBUG( "using item %i", p_playlist->i_current_index );
484 if ( p_playlist->current.i_size == 0 ) return NULL;
486 p_new = ARRAY_VAL( p_playlist->current, p_playlist->i_current_index );
487 /* The new item can't be autoselected */
488 if( p_new != NULL && p_new->i_flags & PLAYLIST_SKIP_FLAG )
495 * Start the input for an item
497 * \param p_playlist the playlist objetc
498 * \param p_item the item to play
501 int playlist_PlayItem( playlist_t *p_playlist, playlist_item_t *p_item )
503 input_item_t *p_input = p_item->p_input;
504 sout_instance_t **pp_sout = &libvlc_priv(p_playlist->p_libvlc)->p_sout;
505 int i_activity = var_GetInteger( p_playlist, "activity" ) ;
507 msg_Dbg( p_playlist, "creating new input thread" );
509 p_input->i_nb_played++;
510 p_playlist->status.p_item = p_item;
512 p_playlist->status.i_status = PLAYLIST_RUNNING;
514 var_SetInteger( p_playlist, "activity", i_activity +
515 DEFAULT_INPUT_ACTIVITY );
517 input_thread_t * p_input_thread =
518 input_CreateThreadExtended( p_playlist, p_input, NULL, *pp_sout );
519 playlist_set_current_input( p_playlist, p_input_thread );
520 vlc_object_release( p_input_thread );
524 char *psz_uri = input_item_GetURI( p_item->p_input );
525 if( psz_uri && ( !strncmp( psz_uri, "directory:", 10 ) ||
526 !strncmp( psz_uri, "vlc:", 4 ) ) )
533 if( p_playlist->p_fetcher &&
534 p_playlist->p_fetcher->i_art_policy == ALBUM_ART_WHEN_PLAYED )
538 char *psz_arturl, *psz_name;
539 psz_arturl = input_item_GetArtURL( p_input );
540 psz_name = input_item_GetName( p_input );
542 /* p_input->p_meta should not be null after a successfull CreateThread */
543 b_has_art = !EMPTY_STR( psz_arturl );
545 if( !b_has_art || strncmp( psz_arturl, "attachment://", 13 ) )
547 PL_DEBUG( "requesting art for %s", psz_name );
548 playlist_AskForArtEnqueue( p_playlist, p_input );
555 var_SetInteger( p_playlist, "playlist-current", p_input->i_id );