1 /*****************************************************************************
2 * control.c : Handle control of the playlist & running through it
3 *****************************************************************************
4 * Copyright (C) 1999-2004 the VideoLAN team
5 * $Id: /local/vlc/0.8.6-playlist-vlm/src/playlist/playlist.c 13741 2006-03-21T19:29:39.792444Z zorglub $
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 *****************************************************************************/
25 #include <vlc/input.h>
26 #include "vlc_playlist.h"
27 #include "playlist_internal.h"
30 /*****************************************************************************
32 *****************************************************************************/
33 int PlaylistVAControl( playlist_t * p_playlist, int i_query, va_list args );
35 void PreparseEnqueueItemSub( playlist_t *, playlist_item_t * );
37 playlist_item_t *playlist_RecursiveFindLast(playlist_t *p_playlist,
38 playlist_item_t *p_node );
40 /*****************************************************************************
42 *****************************************************************************/
45 * Do a playlist action. Should be entered without playlist lock
46 * \see playlist_Control
48 int playlist_LockControl( playlist_t * p_playlist, int i_query, ... )
52 va_start( args, i_query );
53 vlc_mutex_lock( &p_playlist->object_lock );
54 i_result = PlaylistVAControl( p_playlist, i_query, args );
56 vlc_mutex_unlock( &p_playlist->object_lock );
61 * Do a playlist action.
62 * If there is something in the playlist then you can do playlist actions.
63 * Should be entered with playlist lock. See include/vlc_playlist.h for
66 * \param p_playlist the playlist to do the command on
67 * \param i_query the command to do
68 * \param variable number of arguments
69 * \return VLC_SUCCESS or an error
71 int playlist_Control( playlist_t * p_playlist, int i_query, ... )
75 va_start( args, i_query );
76 i_result = PlaylistVAControl( p_playlist, i_query, args );
82 int PlaylistVAControl( playlist_t * p_playlist, int i_query, va_list args )
84 playlist_item_t *p_item, *p_node;
87 if( p_playlist->items.i_size <= 0 )
93 p_playlist->request.i_status = PLAYLIST_STOPPED;
94 p_playlist->request.b_request = VLC_TRUE;
95 p_playlist->request.p_item = NULL;
98 // Node can be null, it will keep the same. Use with care ...
99 // Item null = take the first child of node
100 case PLAYLIST_VIEWPLAY:
101 p_node = (playlist_item_t *)va_arg( args, playlist_item_t * );
102 p_item = (playlist_item_t *)va_arg( args, playlist_item_t * );
103 if ( p_node == NULL )
105 p_node = p_playlist->status.p_node;
108 p_playlist->request.i_status = PLAYLIST_RUNNING;
109 p_playlist->request.i_skip = 0;
110 p_playlist->request.b_request = VLC_TRUE;
111 p_playlist->request.p_node = p_node;
112 p_playlist->request.p_item = p_item;
113 if( p_item && var_GetBool( p_playlist, "random" ) )
114 p_playlist->b_reset_currently_playing = VLC_TRUE;
118 if( p_playlist->p_input )
120 val.i_int = PLAYING_S;
121 var_Set( p_playlist->p_input, "state", val );
126 p_playlist->request.i_status = PLAYLIST_RUNNING;
127 p_playlist->request.b_request = VLC_TRUE;
128 p_playlist->request.p_node = p_playlist->status.p_node;
129 p_playlist->request.p_item = p_playlist->status.p_item;
130 p_playlist->request.i_skip = 0;
134 case PLAYLIST_AUTOPLAY:
135 // AUTOPLAY is an ugly hack for initial status.
136 // Hopefully it will disappear
137 p_playlist->status.i_status = PLAYLIST_RUNNING;
138 p_playlist->request.p_node = p_playlist->status.p_node;
139 p_playlist->request.b_request = VLC_FALSE;
144 if( p_playlist->p_input )
145 var_Get( p_playlist->p_input, "state", &val );
147 if( val.i_int == PAUSE_S )
149 p_playlist->status.i_status = PLAYLIST_RUNNING;
150 if( p_playlist->p_input )
152 val.i_int = PLAYING_S;
153 var_Set( p_playlist->p_input, "state", val );
158 p_playlist->status.i_status = PLAYLIST_PAUSED;
159 if( p_playlist->p_input )
162 var_Set( p_playlist->p_input, "state", val );
168 p_playlist->request.p_node = p_playlist->status.p_node;
169 p_playlist->request.p_item = p_playlist->status.p_item;
170 p_playlist->request.i_skip = (int) va_arg( args, int );
171 /* if already running, keep running */
172 if( p_playlist->status.i_status != PLAYLIST_STOPPED )
173 p_playlist->request.i_status = p_playlist->status.i_status;
174 p_playlist->request.b_request = VLC_TRUE;
178 msg_Err( p_playlist, "unknown playlist query" );
182 vlc_cond_signal( &p_playlist->object_wait );
187 /*****************************************************************************
189 *****************************************************************************/
190 /** Enqueue an item for preparsing */
191 int playlist_PreparseEnqueue( playlist_t *p_playlist,
192 input_item_t *p_item )
194 vlc_mutex_lock( &p_playlist->p_preparse->object_lock );
195 vlc_gc_incref( p_item );
196 INSERT_ELEM( p_playlist->p_preparse->pp_waiting,
197 p_playlist->p_preparse->i_waiting,
198 p_playlist->p_preparse->i_waiting,
200 vlc_mutex_unlock( &p_playlist->p_preparse->object_lock );
201 vlc_cond_signal( &p_playlist->p_preparse->object_wait );
205 /** Enqueue a playlist item or a node for peparsing.
206 * This function should be entered without playlist and preparser locks */
207 int playlist_PreparseEnqueueItem( playlist_t *p_playlist,
208 playlist_item_t *p_item )
210 vlc_mutex_lock( &p_playlist->object_lock );
211 vlc_mutex_lock( &p_playlist->p_preparse->object_lock );
212 PreparseEnqueueItemSub( p_playlist, p_item );
213 vlc_mutex_unlock( &p_playlist->p_preparse->object_lock );
214 vlc_mutex_unlock( &p_playlist->object_lock );
218 int playlist_AskForArtEnqueue( playlist_t *p_playlist,
219 input_item_t *p_item )
224 p.b_fetch_art = VLC_TRUE;
226 vlc_mutex_lock( &p_playlist->p_secondary_preparse->object_lock );
227 for( i = 0; i < p_playlist->p_secondary_preparse->i_waiting &&
228 p_playlist->p_secondary_preparse->p_waiting->b_fetch_art == VLC_TRUE;
230 vlc_gc_incref( p_item );
231 INSERT_ELEM( p_playlist->p_secondary_preparse->p_waiting,
232 p_playlist->p_secondary_preparse->i_waiting,
234 vlc_mutex_unlock( &p_playlist->p_secondary_preparse->object_lock );
235 vlc_cond_signal( &p_playlist->p_secondary_preparse->object_wait );
239 void PreparseEnqueueItemSub( playlist_t *p_playlist,
240 playlist_item_t *p_item )
243 if( p_item->i_children == -1 )
245 vlc_gc_incref( p_item );
246 INSERT_ELEM( p_playlist->p_preparse->pp_waiting,
247 p_playlist->p_preparse->i_waiting,
248 p_playlist->p_preparse->i_waiting,
253 for( i = 0; i < p_item->i_children; i++)
255 PreparseEnqueueItemSub( p_playlist, p_item->pp_children[i] );
260 /*****************************************************************************
262 *****************************************************************************/
264 static void ResyncCurrentIndex(playlist_t *p_playlist, playlist_item_t *p_cur )
266 PL_DEBUG("resyncing on %s", PLI_NAME(p_cur) );
267 /* Simply resync index */
269 p_playlist->i_current_index = -1;
270 for( i = 0 ; i< p_playlist->current.i_size; i++ )
272 if( ARRAY_VAL(p_playlist->current, i) == p_cur )
274 p_playlist->i_current_index = i;
278 PL_DEBUG("%s is at %i", PLI_NAME(p_cur), p_playlist->i_current_index );
281 static void ResetCurrentlyPlaying( playlist_t *p_playlist, vlc_bool_t b_random,
282 playlist_item_t *p_cur )
284 playlist_item_t *p_next = NULL;
285 PL_DEBUG("rebuilding array of current - root %s",
286 PLI_NAME(p_playlist->status.p_node) );
287 ARRAY_RESET(p_playlist->current);
288 p_playlist->i_current_index = -1;
291 /** FIXME: this is *slow* */
292 p_next = playlist_GetNextLeaf( p_playlist,
293 p_playlist->status.p_node,
294 p_next, VLC_TRUE, VLC_FALSE );
297 if( p_next == p_cur )
298 p_playlist->i_current_index = p_playlist->current.i_size;
299 ARRAY_APPEND( p_playlist->current, p_next);
303 PL_DEBUG("rebuild done - %i items, index %i", p_playlist->current.i_size,
304 p_playlist->i_current_index);
307 /* Shuffle the array */
308 srand( (unsigned int)mdate() );
311 for( j = p_playlist->current.i_size - 1; j > 0; j-- )
314 int i = rand() % (j+1); /* between 0 and j */
315 playlist_item_t *p_tmp;
316 p_tmp = ARRAY_VAL(p_playlist->current, i);
317 ARRAY_VAL(p_playlist->current,i) = ARRAY_VAL(p_playlist->current,j);
318 ARRAY_VAL(p_playlist->current,j) = p_tmp;
321 p_playlist->b_reset_currently_playing = VLC_FALSE;
324 /** This function calculates the next playlist item, depending
325 * on the playlist course mode (forward, backward, random, view,...). */
326 playlist_item_t * playlist_NextItem( playlist_t *p_playlist )
328 playlist_item_t *p_new = NULL;
331 vlc_bool_t b_loop = var_GetBool( p_playlist, "loop" );
332 vlc_bool_t b_random = var_GetBool( p_playlist, "random" );
333 vlc_bool_t b_repeat = var_GetBool( p_playlist, "repeat" );
334 vlc_bool_t b_playstop = var_GetBool( p_playlist, "play-and-stop" );
336 /* Handle quickly a few special cases */
337 /* No items to play */
338 if( p_playlist->items.i_size == 0 )
340 msg_Info( p_playlist, "playlist is empty" );
344 /* Repeat and play/stop */
345 if( !p_playlist->request.b_request && b_repeat == VLC_TRUE &&
346 p_playlist->status.p_item )
348 msg_Dbg( p_playlist,"repeating item" );
349 return p_playlist->status.p_item;
351 if( !p_playlist->request.b_request && b_playstop == VLC_TRUE )
353 msg_Dbg( p_playlist,"stopping (play and stop)");
357 if( !p_playlist->request.b_request && p_playlist->status.p_item )
359 playlist_item_t *p_parent = p_playlist->status.p_item;
362 if( p_parent->i_flags & PLAYLIST_SKIP_FLAG )
364 msg_Dbg( p_playlist, "blocking item, stopping") ;
367 p_parent = p_parent->p_parent;
371 /* Start the real work */
372 if( p_playlist->request.b_request )
374 p_new = p_playlist->request.p_item;
375 i_skip = p_playlist->request.i_skip;
376 PL_DEBUG( "processing request item %s node %s skip %i",
377 PLI_NAME( p_playlist->request.p_item ),
378 PLI_NAME( p_playlist->request.p_node ), i_skip );
380 if( p_playlist->request.p_node &&
381 p_playlist->request.p_node != p_playlist->status.p_node )
383 p_playlist->status.p_node = p_playlist->request.p_node;
384 p_playlist->b_reset_currently_playing = VLC_TRUE;
387 /* If we are asked for a node, dont take it */
388 if( i_skip == 0 && ( p_new == NULL || p_new->i_children != -1 ) )
391 if( p_playlist->b_reset_currently_playing )
392 ResetCurrentlyPlaying( p_playlist, b_random, p_new );
394 ResyncCurrentIndex( p_playlist, p_new );
396 p_playlist->i_current_index = -1;
398 if( p_playlist->current.i_size && i_skip > 0 )
400 for( i = i_skip; i > 0 ; i-- )
402 p_playlist->i_current_index++;
403 if( p_playlist->i_current_index == p_playlist->current.i_size )
405 PL_DEBUG( "looping - restarting at beginning of node" );
406 p_playlist->i_current_index = 0;
409 p_new = ARRAY_VAL( p_playlist->current,
410 p_playlist->i_current_index );
412 else if( p_playlist->current.i_size && i_skip < 0 )
414 for( i = i_skip; i < 0 ; i++ )
416 p_playlist->i_current_index--;
417 if( p_playlist->i_current_index == -1 )
419 PL_DEBUG( "looping - restarting at end of node" );
420 p_playlist->i_current_index = p_playlist->current.i_size-1;
423 p_new = ARRAY_VAL( p_playlist->current,
424 p_playlist->i_current_index );
426 /* Clear the request */
427 p_playlist->request.b_request = VLC_FALSE;
429 /* "Automatic" item change ( next ) */
432 PL_DEBUG( "changing item without a request (current %i/%i)",
433 p_playlist->i_current_index, p_playlist->current.i_size );
434 /* Cant go to next from current item */
435 if( p_playlist->status.p_item &&
436 p_playlist->status.p_item->i_flags & PLAYLIST_SKIP_FLAG )
439 if( p_playlist->b_reset_currently_playing )
440 ResetCurrentlyPlaying( p_playlist, b_random,
441 p_playlist->status.p_item );
443 p_playlist->i_current_index++;
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 )
460 /** Start the input for an item */
461 int playlist_PlayItem( playlist_t *p_playlist, playlist_item_t *p_item )
464 int i_activity = var_GetInteger( p_playlist, "activity") ;
466 msg_Dbg( p_playlist, "creating new input thread" );
468 p_item->p_input->i_nb_played++;
469 p_playlist->status.p_item = p_item;
471 p_playlist->status.i_status = PLAYLIST_RUNNING;
473 var_SetInteger( p_playlist, "activity", i_activity +
474 DEFAULT_INPUT_ACTIVITY );
475 p_playlist->p_input = input_CreateThread( p_playlist, p_item->p_input );
477 val.i_int = p_item->p_input->i_id;
478 /* unlock the playlist to set the var...mmm */
479 vlc_mutex_unlock( &p_playlist->object_lock);
480 var_Set( p_playlist, "playlist-current", val);
481 vlc_mutex_lock( &p_playlist->object_lock);