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->i_size <= 0 )
95 p_playlist->request.i_status = PLAYLIST_STOPPED;
96 p_playlist->request.b_request = VLC_TRUE;
97 p_playlist->request.p_item = NULL;
100 // Node can be null, it will keep the same. Use with care ...
101 // Item null = take the first child of node
102 case PLAYLIST_VIEWPLAY:
103 p_playlist->b_reset_random = VLC_TRUE;
104 p_node = (playlist_item_t *)va_arg( args, playlist_item_t * );
105 p_item = (playlist_item_t *)va_arg( args, playlist_item_t * );
106 if ( p_node == NULL )
108 p_node = p_playlist->status.p_node;
111 p_playlist->request.i_status = PLAYLIST_RUNNING;
112 p_playlist->request.i_skip = 0;
113 p_playlist->request.b_request = VLC_TRUE;
114 p_playlist->request.p_node = p_node;
115 p_playlist->request.p_item = p_item;
119 if( p_playlist->p_input )
121 val.i_int = PLAYING_S;
122 var_Set( p_playlist->p_input, "state", val );
127 p_playlist->request.i_status = PLAYLIST_RUNNING;
128 p_playlist->request.b_request = VLC_TRUE;
129 p_playlist->request.p_node = p_playlist->status.p_node;
130 p_playlist->request.p_item = p_playlist->status.p_item;
131 p_playlist->request.i_skip = 0;
135 case PLAYLIST_AUTOPLAY:
136 // AUTOPLAY is an ugly hack for initial status.
137 // Hopefully it will disappear
138 p_playlist->status.i_status = PLAYLIST_RUNNING;
139 p_playlist->request.p_node = p_playlist->status.p_node;
140 p_playlist->request.b_request = VLC_FALSE;
145 if( p_playlist->p_input )
146 var_Get( p_playlist->p_input, "state", &val );
148 if( val.i_int == PAUSE_S )
150 p_playlist->status.i_status = PLAYLIST_RUNNING;
151 if( p_playlist->p_input )
153 val.i_int = PLAYING_S;
154 var_Set( p_playlist->p_input, "state", val );
159 p_playlist->status.i_status = PLAYLIST_PAUSED;
160 if( p_playlist->p_input )
163 var_Set( p_playlist->p_input, "state", val );
169 p_playlist->request.p_node = p_playlist->status.p_node;
170 p_playlist->request.p_item = p_playlist->status.p_item;
171 p_playlist->request.i_skip = (int) va_arg( args, int );
172 /* if already running, keep running */
173 if( p_playlist->status.i_status != PLAYLIST_STOPPED )
174 p_playlist->request.i_status = p_playlist->status.i_status;
175 p_playlist->request.b_request = VLC_TRUE;
179 msg_Err( p_playlist, "unknown playlist query" );
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 );
204 /** Enqueue a playlist item or a node for peparsing.
205 * This function should be entered without playlist and preparser locks */
206 int playlist_PreparseEnqueueItem( playlist_t *p_playlist,
207 playlist_item_t *p_item )
209 vlc_mutex_lock( &p_playlist->object_lock );
210 vlc_mutex_lock( &p_playlist->p_preparse->object_lock );
211 PreparseEnqueueItemSub( p_playlist, p_item );
212 vlc_mutex_unlock( &p_playlist->p_preparse->object_lock );
213 vlc_mutex_unlock( &p_playlist->object_lock );
217 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 *****************************************************************************/
242 /** This function calculates the next playlist item, depending
243 * on the playlist course mode (forward, backward, random, view,...). */
244 playlist_item_t * playlist_NextItem( playlist_t *p_playlist )
246 playlist_item_t *p_new = NULL;
249 vlc_bool_t b_loop = var_GetBool( p_playlist, "loop" );
250 vlc_bool_t b_random = var_GetBool( p_playlist, "random" );
251 vlc_bool_t b_repeat = var_GetBool( p_playlist, "repeat" );
252 vlc_bool_t b_playstop = var_GetBool( p_playlist, "play-and-stop" );
254 /* Handle quickly a few special cases */
255 /* No items to play */
256 if( p_playlist->i_size == 0 )
258 msg_Info( p_playlist, "playlist is empty" );
262 /* Repeat and play/stop */
263 if( !p_playlist->request.b_request && b_repeat == VLC_TRUE &&
264 p_playlist->status.p_item )
266 msg_Dbg( p_playlist,"repeating item" );
267 return p_playlist->status.p_item;
269 if( !p_playlist->request.b_request && b_playstop == VLC_TRUE )
271 msg_Dbg( p_playlist,"stopping (play and stop)");
275 if( !p_playlist->request.b_request && p_playlist->status.p_item )
277 playlist_item_t *p_parent = p_playlist->status.p_item;
280 if( p_parent->i_flags & PLAYLIST_SKIP_FLAG )
282 msg_Dbg( p_playlist, "blocking item, stopping") ;
285 p_parent = p_parent->p_parent;
289 /* Random case. This is an exception: if request, but request is skip +- 1
290 * we don't go to next item but select a new random one. */
292 ( !p_playlist->request.b_request ||
293 ( p_playlist->request.b_request &&
294 ( p_playlist->request.p_item == NULL ||
295 p_playlist->request.i_skip == 1 ||
296 p_playlist->request.i_skip == -1 ) ) ) )
298 PL_DEBUG( "doing random, have %i items, currently at %i, reset %i\n",
299 p_playlist->i_random, p_playlist->i_random_index,
300 p_playlist->b_reset_random );
301 if( p_playlist->b_reset_random )
304 FREE( p_playlist->pp_random );
305 if( !p_playlist->b_reset_random && !b_loop ) goto end;
306 p_playlist->i_random = 0;
307 p_playlist->i_random_index = 0;
308 p_playlist->i_random = playlist_GetAllEnabledChildren(
310 p_playlist->status.p_node,
311 &p_playlist->pp_random );
312 /* Shuffle the array */
313 srand( (unsigned int)mdate() );
315 for( j = p_playlist->i_random -1; j > 0; j-- )
318 int i = rand() % (j+1); /* between 0 and j */
319 playlist_item_t *p_tmp;
320 p_tmp = p_playlist->pp_random[i];
321 p_playlist->pp_random[i] = p_playlist->pp_random[j];
322 p_playlist->pp_random[j] = p_tmp;
324 p_playlist->b_reset_random = VLC_FALSE;
325 PL_DEBUG( "random rebuilt, have %i items", p_playlist->i_random );
329 /* Go backward or forward */
330 if( !p_playlist->request.b_request || !p_playlist->request.p_item ||
331 p_playlist->request.i_skip == 1 )
332 p_playlist->i_random_index++;
334 p_playlist->i_random_index--;
335 /* Handle bounds situations */
336 if( p_playlist->i_random_index == -1 )
338 if( !b_loop || p_playlist->i_random == 0 ) goto end;
339 p_playlist->i_random_index = p_playlist->i_random - 1;
341 else if( p_playlist->i_random_index == p_playlist->i_random )
343 if( !b_loop || p_playlist->i_random == 0 ) goto end;
344 p_playlist->i_random_index = 0;
347 PL_DEBUG( "using random item %i", p_playlist->i_random_index );
348 if ( p_playlist->i_random == 0 ) goto end; /* Can this happen ?? */
349 p_new = p_playlist->pp_random[p_playlist->i_random_index];
351 if( !p_new ) p_playlist->b_reset_random = VLC_TRUE;
352 p_playlist->request.i_skip = 0;
353 p_playlist->request.b_request = VLC_FALSE;
357 /* Start the real work */
358 if( p_playlist->request.b_request )
360 p_new = p_playlist->request.p_item;
361 i_skip = p_playlist->request.i_skip;
362 PL_DEBUG( "processing request item %s node %s skip %i",
363 PLI_NAME( p_playlist->request.p_item ),
364 PLI_NAME( p_playlist->request.p_node ), i_skip );
366 if( p_playlist->request.p_node )
367 p_playlist->status.p_node = p_playlist->request.p_node;
369 /* If we are asked for a node, dont take it */
370 if( i_skip == 0 && ( p_new == NULL || p_new->i_children != -1 ) )
375 for( i = i_skip; i > 0 ; i-- )
377 p_new = playlist_GetNextLeaf( p_playlist,
378 p_playlist->request.p_node,
379 p_new, VLC_TRUE, VLC_FALSE );
382 PL_DEBUG( "looping - restarting at beginning of node" );
383 p_new = playlist_GetNextLeaf( p_playlist,
384 p_playlist->request.p_node,
385 NULL, VLC_TRUE, VLC_FALSE);
386 if( p_new == NULL ) break;
390 else if( i_skip < 0 )
392 for( i = i_skip; i < 0 ; i++ )
394 p_new = playlist_GetPrevLeaf( p_playlist,
395 p_playlist->request.p_node,
396 p_new, VLC_FALSE, VLC_FALSE );
399 PL_DEBUG( "looping - restarting at end of node" );
400 /** \bug This is needed because GetPrevLeaf does not loop
402 p_new = playlist_GetLastLeaf( p_playlist,
403 p_playlist->request.p_node );
405 if( p_new == NULL ) break;
408 /* Clear the request */
409 p_playlist->request.b_request = VLC_FALSE;
411 /* "Automatic" item change ( next ) */
414 PL_DEBUG( "changing item without a request" );
415 /* Cant go to next from current item */
416 if( p_playlist->status.p_item &&
417 p_playlist->status.p_item->i_flags & PLAYLIST_SKIP_FLAG )
420 p_new = playlist_GetNextLeaf( p_playlist,
421 p_playlist->status.p_node,
422 p_playlist->status.p_item,
423 VLC_TRUE, VLC_FALSE );
424 if( p_new == NULL && b_loop )
426 PL_DEBUG( "looping" );
427 p_new = playlist_GetNextLeaf( p_playlist,
428 p_playlist->status.p_node,
429 NULL, VLC_TRUE, VLC_FALSE );
431 /* The new item can't be autoselected */
432 if( p_new != NULL && p_new->i_flags & PLAYLIST_SKIP_FLAG )
437 msg_Dbg( p_playlist, "did not find something to play" );
442 /** Start the input for an item */
443 int playlist_PlayItem( playlist_t *p_playlist, playlist_item_t *p_item )
446 int i_activity = var_GetInteger( p_playlist, "activity") ;
448 msg_Dbg( p_playlist, "creating new input thread" );
450 p_item->p_input->i_nb_played++;
451 p_playlist->status.p_item = p_item;
453 p_playlist->status.i_status = PLAYLIST_RUNNING;
455 var_SetInteger( p_playlist, "activity", i_activity +
456 DEFAULT_INPUT_ACTIVITY );
457 p_playlist->p_input = input_CreateThread( p_playlist, p_item->p_input );
459 val.i_int = p_item->p_input->i_id;
460 /* unlock the playlist to set the var...mmm */
461 vlc_mutex_unlock( &p_playlist->object_lock);
462 var_Set( p_playlist, "playlist-current", val);
463 vlc_mutex_lock( &p_playlist->object_lock);