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( playlist_IsEmpty( p_playlist ) )
87 p_playlist->request.i_status = PLAYLIST_STOPPED;
88 p_playlist->request.b_request = true;
89 p_playlist->request.p_item = NULL;
92 // Node can be null, it will keep the same. Use with care ...
93 // Item null = take the first child of node
94 case PLAYLIST_VIEWPLAY:
95 p_node = (playlist_item_t *)va_arg( args, playlist_item_t * );
96 p_item = (playlist_item_t *)va_arg( args, playlist_item_t * );
99 p_node = p_playlist->status.p_node;
102 p_playlist->request.i_status = PLAYLIST_RUNNING;
103 p_playlist->request.i_skip = 0;
104 p_playlist->request.b_request = true;
105 p_playlist->request.p_node = p_node;
106 p_playlist->request.p_item = p_item;
107 if( p_item && var_GetBool( p_playlist, "random" ) )
108 p_playlist->b_reset_currently_playing = true;
112 if( p_playlist->p_input )
114 val.i_int = PLAYING_S;
115 var_Set( p_playlist->p_input, "state", val );
120 p_playlist->request.i_status = PLAYLIST_RUNNING;
121 p_playlist->request.b_request = true;
122 p_playlist->request.p_node = p_playlist->status.p_node;
123 p_playlist->request.p_item = p_playlist->status.p_item;
124 p_playlist->request.i_skip = 0;
130 if( p_playlist->p_input )
131 var_Get( p_playlist->p_input, "state", &val );
133 if( val.i_int == PAUSE_S )
135 p_playlist->status.i_status = PLAYLIST_RUNNING;
136 if( p_playlist->p_input )
138 val.i_int = PLAYING_S;
139 var_Set( p_playlist->p_input, "state", val );
144 p_playlist->status.i_status = PLAYLIST_PAUSED;
145 if( p_playlist->p_input )
148 var_Set( p_playlist->p_input, "state", val );
154 p_playlist->request.p_node = p_playlist->status.p_node;
155 p_playlist->request.p_item = p_playlist->status.p_item;
156 p_playlist->request.i_skip = (int) va_arg( args, int );
157 /* if already running, keep running */
158 if( p_playlist->status.i_status != PLAYLIST_STOPPED )
159 p_playlist->request.i_status = p_playlist->status.i_status;
160 p_playlist->request.b_request = true;
164 msg_Err( p_playlist, "unknown playlist query" );
168 vlc_object_signal_maybe( VLC_OBJECT(p_playlist) );
173 /*****************************************************************************
175 *****************************************************************************/
176 /** Enqueue an item for preparsing */
177 int playlist_PreparseEnqueue( playlist_t *p_playlist,
178 input_item_t *p_item )
180 vlc_object_lock( p_playlist->p_preparse );
181 vlc_gc_incref( p_item );
182 INSERT_ELEM( p_playlist->p_preparse->pp_waiting,
183 p_playlist->p_preparse->i_waiting,
184 p_playlist->p_preparse->i_waiting,
186 vlc_object_signal_unlocked( p_playlist->p_preparse );
187 vlc_object_unlock( p_playlist->p_preparse );
191 /** Enqueue a playlist item or a node for peparsing.
192 * This function should be entered without playlist and preparser locks */
193 int playlist_PreparseEnqueueItem( playlist_t *p_playlist,
194 playlist_item_t *p_item )
196 vlc_object_lock( p_playlist );
197 vlc_object_lock( p_playlist->p_preparse );
198 PreparseEnqueueItemSub( p_playlist, p_item );
199 vlc_object_unlock( p_playlist->p_preparse );
200 vlc_object_unlock( p_playlist );
204 int playlist_AskForArtEnqueue( playlist_t *p_playlist,
205 input_item_t *p_item )
209 vlc_object_lock( p_playlist->p_fetcher );
210 for( i = 0; i < p_playlist->p_fetcher->i_waiting ; i++ );
211 vlc_gc_incref( p_item );
212 INSERT_ELEM( p_playlist->p_fetcher->pp_waiting,
213 p_playlist->p_fetcher->i_waiting,
215 vlc_object_signal_unlocked( p_playlist->p_fetcher );
216 vlc_object_unlock( p_playlist->p_fetcher );
220 static void PreparseEnqueueItemSub( playlist_t *p_playlist,
221 playlist_item_t *p_item )
224 if( p_item->i_children == -1 )
226 vlc_gc_incref( p_item );
227 INSERT_ELEM( p_playlist->p_preparse->pp_waiting,
228 p_playlist->p_preparse->i_waiting,
229 p_playlist->p_preparse->i_waiting,
234 for( i = 0; i < p_item->i_children; i++)
236 PreparseEnqueueItemSub( p_playlist, p_item->pp_children[i] );
241 /*****************************************************************************
243 *****************************************************************************/
246 * Synchronise the current index of the playlist
247 * to match the index of the current item.
249 * \param p_playlist the playlist structure
250 * \param p_cur the current playlist item
253 static void ResyncCurrentIndex( playlist_t *p_playlist, playlist_item_t *p_cur )
255 PL_DEBUG( "resyncing on %s", PLI_NAME( p_cur ) );
256 /* Simply resync index */
258 p_playlist->i_current_index = -1;
259 for( i = 0 ; i< p_playlist->current.i_size; i++ )
261 if( ARRAY_VAL( p_playlist->current, i ) == p_cur )
263 p_playlist->i_current_index = i;
267 PL_DEBUG( "%s is at %i", PLI_NAME( p_cur ), p_playlist->i_current_index );
270 void ResetCurrentlyPlaying( playlist_t *p_playlist, bool b_random,
271 playlist_item_t *p_cur )
273 playlist_item_t *p_next = NULL;
274 stats_TimerStart( p_playlist, "Items array build",
275 STATS_TIMER_PLAYLIST_BUILD );
276 PL_DEBUG( "rebuilding array of current - root %s",
277 PLI_NAME( p_playlist->status.p_node ) );
278 ARRAY_RESET( p_playlist->current );
279 p_playlist->i_current_index = -1;
282 /** FIXME: this is *slow* */
283 p_next = playlist_GetNextLeaf( p_playlist,
284 p_playlist->status.p_node,
285 p_next, true, false );
288 if( p_next == p_cur )
289 p_playlist->i_current_index = p_playlist->current.i_size;
290 ARRAY_APPEND( p_playlist->current, p_next);
294 PL_DEBUG("rebuild done - %i items, index %i", p_playlist->current.i_size,
295 p_playlist->i_current_index);
298 /* Shuffle the array */
299 srand( (unsigned int)mdate() );
301 for( j = p_playlist->current.i_size - 1; j > 0; j-- )
303 int i = rand() % (j+1); /* between 0 and j */
304 playlist_item_t *p_tmp;
305 /* swap the two items */
306 p_tmp = ARRAY_VAL(p_playlist->current, i);
307 ARRAY_VAL(p_playlist->current,i) = ARRAY_VAL(p_playlist->current,j);
308 ARRAY_VAL(p_playlist->current,j) = p_tmp;
311 p_playlist->b_reset_currently_playing = false;
312 stats_TimerStop( p_playlist, STATS_TIMER_PLAYLIST_BUILD );
316 * Compute the next playlist item depending on
317 * the playlist course mode (forward, backward, random, view,...).
319 * \param p_playlist the playlist object
322 playlist_item_t * playlist_NextItem( playlist_t *p_playlist )
324 playlist_item_t *p_new = NULL;
327 bool b_loop = var_GetBool( p_playlist, "loop" );
328 bool b_random = var_GetBool( p_playlist, "random" );
329 bool b_repeat = var_GetBool( p_playlist, "repeat" );
330 bool b_playstop = var_GetBool( p_playlist, "play-and-stop" );
332 /* Handle quickly a few special cases */
333 /* No items to play */
334 if( p_playlist->items.i_size == 0 )
336 msg_Info( p_playlist, "playlist is empty" );
340 /* Repeat and play/stop */
341 if( !p_playlist->request.b_request && b_repeat == true &&
342 p_playlist->status.p_item )
344 msg_Dbg( p_playlist,"repeating item" );
345 return p_playlist->status.p_item;
347 if( !p_playlist->request.b_request && b_playstop == true )
349 msg_Dbg( p_playlist,"stopping (play and stop)" );
353 if( !p_playlist->request.b_request && p_playlist->status.p_item )
355 playlist_item_t *p_parent = p_playlist->status.p_item;
358 if( p_parent->i_flags & PLAYLIST_SKIP_FLAG )
360 msg_Dbg( p_playlist, "blocking item, stopping") ;
363 p_parent = p_parent->p_parent;
367 /* Start the real work */
368 if( p_playlist->request.b_request )
370 p_new = p_playlist->request.p_item;
371 i_skip = p_playlist->request.i_skip;
372 PL_DEBUG( "processing request item %s node %s skip %i",
373 PLI_NAME( p_playlist->request.p_item ),
374 PLI_NAME( p_playlist->request.p_node ), i_skip );
376 if( p_playlist->request.p_node &&
377 p_playlist->request.p_node != p_playlist->status.p_node )
379 p_playlist->status.p_node = p_playlist->request.p_node;
380 p_playlist->b_reset_currently_playing = true;
383 /* If we are asked for a node, don't take it */
384 if( i_skip == 0 && ( p_new == NULL || p_new->i_children != -1 ) )
387 if( p_playlist->b_reset_currently_playing )
388 /* A bit too bad to reset twice ... */
389 ResetCurrentlyPlaying( p_playlist, b_random, p_new );
391 ResyncCurrentIndex( p_playlist, p_new );
393 p_playlist->i_current_index = -1;
395 if( p_playlist->current.i_size && (i_skip > 0) )
397 if( p_playlist->i_current_index < -1 )
398 p_playlist->i_current_index = -1;
399 for( i = i_skip; i > 0 ; i-- )
401 p_playlist->i_current_index++;
402 if( p_playlist->i_current_index >= p_playlist->current.i_size )
404 PL_DEBUG( "looping - restarting at beginning of node" );
405 p_playlist->i_current_index = 0;
408 p_new = ARRAY_VAL( p_playlist->current,
409 p_playlist->i_current_index );
411 else if( p_playlist->current.i_size && (i_skip < 0) )
413 for( i = i_skip; i < 0 ; i++ )
415 p_playlist->i_current_index--;
416 if( p_playlist->i_current_index <= -1 )
418 PL_DEBUG( "looping - restarting at end of node" );
419 p_playlist->i_current_index = p_playlist->current.i_size-1;
422 p_new = ARRAY_VAL( p_playlist->current,
423 p_playlist->i_current_index );
425 /* Clear the request */
426 p_playlist->request.b_request = false;
428 /* "Automatic" item change ( next ) */
431 PL_DEBUG( "changing item without a request (current %i/%i)",
432 p_playlist->i_current_index, p_playlist->current.i_size );
433 /* Cant go to next from current item */
434 if( p_playlist->status.p_item &&
435 p_playlist->status.p_item->i_flags & PLAYLIST_SKIP_FLAG )
438 if( p_playlist->b_reset_currently_playing )
439 ResetCurrentlyPlaying( p_playlist, b_random,
440 p_playlist->status.p_item );
442 p_playlist->i_current_index++;
443 assert( p_playlist->i_current_index <= p_playlist->current.i_size );
444 if( p_playlist->i_current_index == p_playlist->current.i_size )
446 if( !b_loop || p_playlist->current.i_size == 0 ) return NULL;
447 p_playlist->i_current_index = 0;
449 PL_DEBUG( "using item %i", p_playlist->i_current_index );
450 if ( p_playlist->current.i_size == 0 ) return NULL;
452 p_new = ARRAY_VAL( p_playlist->current, p_playlist->i_current_index );
453 /* The new item can't be autoselected */
454 if( p_new != NULL && p_new->i_flags & PLAYLIST_SKIP_FLAG )
461 * Start the input for an item
463 * \param p_playlist the playlist objetc
464 * \param p_item the item to play
467 int playlist_PlayItem( playlist_t *p_playlist, playlist_item_t *p_item )
469 input_item_t *p_input = p_item->p_input;
470 sout_instance_t **pp_sout = &libvlc_priv(p_playlist->p_libvlc)->p_sout;
471 int i_activity = var_GetInteger( p_playlist, "activity" ) ;
473 msg_Dbg( p_playlist, "creating new input thread" );
475 p_input->i_nb_played++;
476 p_playlist->status.p_item = p_item;
478 p_playlist->status.i_status = PLAYLIST_RUNNING;
480 var_SetInteger( p_playlist, "activity", i_activity +
481 DEFAULT_INPUT_ACTIVITY );
483 input_thread_t * p_input_thread =
484 input_CreateThreadExtended( p_playlist, p_input, NULL, *pp_sout );
485 playlist_set_current_input( p_playlist, p_input_thread );
486 vlc_object_release( p_input_thread );
490 char *psz_uri = input_item_GetURI( p_item->p_input );
491 if( psz_uri && ( !strncmp( psz_uri, "directory:", 10 ) ||
492 !strncmp( psz_uri, "vlc:", 4 ) ) )
499 if( p_playlist->p_fetcher &&
500 p_playlist->p_fetcher->i_art_policy == ALBUM_ART_WHEN_PLAYED )
504 char *psz_arturl, *psz_name;
505 psz_arturl = input_item_GetArtURL( p_input );
506 psz_name = input_item_GetName( p_input );
508 /* p_input->p_meta should not be null after a successfull CreateThread */
509 b_has_art = !EMPTY_STR( psz_arturl );
511 if( !b_has_art || strncmp( psz_arturl, "attachment://", 13 ) )
513 PL_DEBUG( "requesting art for %s", psz_name );
514 playlist_AskForArtEnqueue( p_playlist, p_input );
521 var_SetInteger( p_playlist, "playlist-current", p_input->i_id );