1 /*****************************************************************************
2 * control.c : Hanle 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"
28 /*****************************************************************************
30 *****************************************************************************/
31 int PlaylistVAControl( playlist_t * p_playlist, int i_query, va_list args );
33 void PreparseEnqueueItemSub( playlist_t *, playlist_item_t * );
35 playlist_item_t *playlist_RecursiveFindLast(playlist_t *p_playlist,
36 playlist_item_t *p_node );
38 /*****************************************************************************
40 *****************************************************************************/
43 * Do a playlist action. Should be entered without playlist lock
44 * \see playlist_Control
46 int playlist_LockControl( playlist_t * p_playlist, int i_query, ... )
50 va_start( args, i_query );
51 vlc_mutex_lock( &p_playlist->object_lock );
52 i_result = PlaylistVAControl( p_playlist, i_query, args );
54 vlc_mutex_unlock( &p_playlist->object_lock );
59 * Do a playlist action.
60 * If there is something in the playlist then you can do playlist actions.
61 * Should be entered with playlist lock. See include/vlc_playlist.h for
64 * \param p_playlist the playlist to do the command on
65 * \param i_query the command to do
66 * \param variable number of arguments
67 * \return VLC_SUCCESS or an error
69 int playlist_Control( playlist_t * p_playlist, int i_query, ... )
73 va_start( args, i_query );
74 i_result = PlaylistVAControl( p_playlist, i_query, args );
80 int PlaylistVAControl( playlist_t * p_playlist, int i_query, va_list args )
83 playlist_item_t *p_item, *p_node;
86 if( p_playlist->i_size <= 0 )
94 p_playlist->request.i_status = PLAYLIST_STOPPED;
95 p_playlist->request.b_request = VLC_TRUE;
96 p_playlist->request.p_item = NULL;
99 case PLAYLIST_ITEMPLAY:
100 p_item = (playlist_item_t *)va_arg( args, playlist_item_t * );
101 if ( p_item == NULL || p_item->p_input->psz_uri == NULL )
103 p_playlist->request.i_status = PLAYLIST_RUNNING;
104 p_playlist->request.i_skip = 0;
105 p_playlist->request.b_request = VLC_TRUE;
106 p_playlist->request.p_item = p_item;
107 p_playlist->request.p_node = p_playlist->status.p_node;
110 case PLAYLIST_VIEWPLAY:
111 i_view = (int) va_arg( args, playlist_item_t *);
112 p_node = (playlist_item_t *)va_arg( args, playlist_item_t * );
113 p_item = (playlist_item_t *)va_arg( args, playlist_item_t * );
114 if ( p_node == NULL )
116 p_node = p_playlist->status.p_node;
119 p_playlist->status.i_status = PLAYLIST_STOPPED;
120 p_playlist->request.b_request = VLC_TRUE;
121 msg_Err( p_playlist, "null node" );
125 p_playlist->request.i_status = PLAYLIST_RUNNING;
126 p_playlist->request.i_skip = 0;
127 p_playlist->request.b_request = VLC_TRUE;
128 p_playlist->request.p_node = p_node;
129 p_playlist->request.p_item = p_item;
133 p_playlist->request.i_status = PLAYLIST_RUNNING;
134 p_playlist->request.b_request = VLC_TRUE;
136 if( p_playlist->p_input )
138 val.i_int = PLAYING_S;
139 var_Set( p_playlist->p_input, "state", val );
142 p_playlist->request.p_node = p_playlist->status.p_node;
143 p_playlist->request.p_item = p_playlist->status.p_item;
144 p_playlist->request.i_skip = 0;
147 case PLAYLIST_AUTOPLAY:
148 p_playlist->status.i_status = PLAYLIST_RUNNING;
149 p_playlist->status.p_node = p_playlist->p_local_category;
150 p_playlist->request.b_request = VLC_FALSE;
155 if( p_playlist->p_input )
156 var_Get( p_playlist->p_input, "state", &val );
158 if( val.i_int == PAUSE_S )
160 p_playlist->status.i_status = PLAYLIST_RUNNING;
161 if( p_playlist->p_input )
163 val.i_int = PLAYING_S;
164 var_Set( p_playlist->p_input, "state", val );
169 p_playlist->status.i_status = PLAYLIST_PAUSED;
170 if( p_playlist->p_input )
173 var_Set( p_playlist->p_input, "state", val );
179 p_playlist->request.p_node = p_playlist->status.p_node;
180 p_playlist->request.p_item = p_playlist->status.p_item;
181 p_playlist->request.i_skip = (int) va_arg( args, int );
182 p_playlist->request.b_request = VLC_TRUE;
186 msg_Err( p_playlist, "unknown playlist query" );
194 /*****************************************************************************
196 *****************************************************************************/
197 /** Enqueue an item for preparsing */
198 int playlist_PreparseEnqueue( playlist_t *p_playlist,
199 input_item_t *p_item )
201 vlc_mutex_lock( &p_playlist->p_preparse->object_lock );
202 vlc_gc_incref( p_item );
203 INSERT_ELEM( p_playlist->p_preparse->pp_waiting,
204 p_playlist->p_preparse->i_waiting,
205 p_playlist->p_preparse->i_waiting,
207 vlc_mutex_unlock( &p_playlist->p_preparse->object_lock );
211 /** Enqueue a playlist item or a node for peparsing.
212 * This function should be entered without playlist and preparser locks */
213 int playlist_PreparseEnqueueItem( playlist_t *p_playlist,
214 playlist_item_t *p_item )
216 vlc_mutex_lock( &p_playlist->object_lock );
217 vlc_mutex_lock( &p_playlist->p_preparse->object_lock );
218 PreparseEnqueueItemSub( p_playlist, p_item );
219 vlc_mutex_unlock( &p_playlist->p_preparse->object_lock );
220 vlc_mutex_unlock( &p_playlist->object_lock );
224 void PreparseEnqueueItemSub( playlist_t *p_playlist,
225 playlist_item_t *p_item )
228 if( p_item->i_children == -1 )
230 vlc_gc_incref( p_item );
231 INSERT_ELEM( p_playlist->p_preparse->pp_waiting,
232 p_playlist->p_preparse->i_waiting,
233 p_playlist->p_preparse->i_waiting,
238 for( i = 0; i < p_item->i_children; i++)
240 PreparseEnqueueItemSub( p_playlist,
241 p_item->pp_children[i] );
246 /*****************************************************************************
248 *****************************************************************************/
250 /** This function calculates the next playlist item, depending
251 * on the playlist course mode (forward, backward, random, view,...). */
252 playlist_item_t * playlist_NextItem( playlist_t *p_playlist )
254 playlist_item_t *p_new = NULL;
257 vlc_bool_t b_loop = var_GetBool( p_playlist, "loop" );
258 vlc_bool_t b_random = var_GetBool( p_playlist, "random" );
259 vlc_bool_t b_repeat = var_GetBool( p_playlist, "repeat" );
260 vlc_bool_t b_playstop = var_GetBool( p_playlist, "play-and-stop" );
262 /* Handle quickly a few special cases */
264 /* No items to play */
265 if( p_playlist->i_size == 0 )
267 msg_Info( p_playlist, "playlist is empty" );
271 /* Repeat and play/stop */
272 if( !p_playlist->request.b_request && b_repeat == VLC_TRUE &&
273 p_playlist->status.p_item )
275 msg_Dbg( p_playlist,"repeating item" );
276 return p_playlist->status.p_item;
278 if( !p_playlist->request.b_request && b_playstop == VLC_TRUE )
280 msg_Dbg( p_playlist,"stopping (play and stop)");
284 if( !p_playlist->request.b_request && p_playlist->status.p_item &&
285 p_playlist->status.p_item->i_flags & PLAYLIST_SKIP_FLAG )
287 msg_Dbg( p_playlist, "blocking item, stopping") ;
291 /* Random case. This is an exception: if request, but request is skip +- 1
292 * we don't go to next item but select a new random one. */
294 msg_Err( p_playlist, "random unsupported" );
297 ( !p_playlist->request.b_request ||
298 ( p_playlist->request.b_request && ( p_playlist->request.p_item == NULL ||
299 p_playlist->request.i_skip == 1 || p_playlist->request.i_skip == -1 ) ) ) )
301 /* how many items to choose from ? */
303 for ( i = 0; i < p_playlist->i_size; i++ )
305 if ( p_playlist->pp_items[i]->p_input->i_nb_played == 0 )
311 /* Don't loop? Exit! */
314 /* Otherwise reset the counter */
315 for ( i = 0; i < p_playlist->i_size; i++ )
317 p_playlist->pp_items[i]->p_input->i_nb_played = 0;
319 i_count = p_playlist->i_size;
321 srand( (unsigned int)mdate() );
322 i = rand() % i_count + 1 ;
323 /* loop thru the list and count down the unplayed items to the selected one */
324 for ( i_new = 0; i_new < p_playlist->i_size && i > 0; i_new++ )
326 if ( p_playlist->pp_items[i_new]->p_input->i_nb_played == 0 )
331 p_playlist->request.i_skip = 0;
332 p_playlist->request.b_request = VLC_FALSE;
333 return p_playlist->pp_items[i_new];
337 /* Start the real work */
338 if( p_playlist->request.b_request )
340 PL_DEBUG( "processing request" );
341 p_new = p_playlist->request.p_item;
342 i_skip = p_playlist->request.i_skip;
344 p_playlist->status.p_node = p_playlist->request.p_node;
346 /* If we are asked for a node, take its first item */
348 ( p_new == NULL || p_new->i_children != -1 ) )
355 for( i = i_skip; i > 0 ; i-- )
357 p_new = playlist_GetNextEnabledLeaf( p_playlist,
358 p_playlist->request.p_node,
362 PL_DEBUG( "looping - restarting at beginning of node" );
363 p_new = playlist_GetNextLeaf( p_playlist,
364 p_playlist->request.p_node,
366 if( p_new == NULL ) break;
370 else if( i_skip < 0 )
372 for( i = i_skip; i < 0 ; i++ )
374 p_new = playlist_GetPrevLeaf( p_playlist,
375 p_playlist->request.p_node,
379 PL_DEBUG( "looping - restarting at end of node" );
380 /** \bug This is needed because GetPrevLeaf does not loop
382 p_new = playlist_GetLastLeaf( p_playlist,
383 p_playlist->request.p_node );
385 if( p_new == NULL ) break;
388 /* Clear the request */
389 p_playlist->request.b_request = VLC_FALSE;
391 /* "Automatic" item change ( next ) */
394 PL_DEBUG( "changing item without a request" );
395 /* Cant go to next from current item */
396 if( p_playlist->status.p_item &&
397 p_playlist->status.p_item->i_flags & PLAYLIST_SKIP_FLAG )
400 p_new = playlist_GetNextLeaf( p_playlist,
401 p_playlist->status.p_node,
402 p_playlist->status.p_item );
403 if( p_new == NULL && b_loop )
405 PL_DEBUG( "looping" );
406 p_new = playlist_GetNextLeaf( p_playlist,
407 p_playlist->status.p_node,
410 /* The new item can't be autoselected */
411 if( p_new != NULL && p_new->i_flags & PLAYLIST_SKIP_FLAG )
416 msg_Dbg( p_playlist, "did not find something to play" );
421 /** Start the input for an item */
422 int playlist_PlayItem( playlist_t *p_playlist, playlist_item_t *p_item )
425 int i_activity = var_GetInteger( p_playlist, "activity") ;
427 msg_Dbg( p_playlist, "creating new input thread" );
429 p_item->p_input->i_nb_played++;
430 p_playlist->status.p_item = p_item;
432 p_playlist->status.i_status = PLAYLIST_RUNNING;
434 var_SetInteger( p_playlist, "activity", i_activity +
435 DEFAULT_INPUT_ACTIVITY );
436 p_playlist->p_input = input_CreateThread( p_playlist, p_item->p_input );
438 val.i_int = p_item->p_input->i_id;
439 /* unlock the playlist to set the var...mmm */
440 vlc_mutex_unlock( &p_playlist->object_lock);
441 var_Set( p_playlist, "playlist-current", val);
442 vlc_mutex_lock( &p_playlist->object_lock);