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_playlist->status.i_status = PLAYLIST_STOPPED;
117 p_playlist->request.b_request = VLC_TRUE;
118 msg_Err( p_playlist, "null node" );
121 p_playlist->request.i_status = PLAYLIST_RUNNING;
122 p_playlist->request.i_skip = 0;
123 p_playlist->request.b_request = VLC_TRUE;
124 p_playlist->request.p_node = p_node;
125 p_playlist->request.p_item = p_item;
129 p_playlist->request.i_status = PLAYLIST_RUNNING;
130 p_playlist->request.b_request = VLC_TRUE;
132 if( p_playlist->p_input )
134 val.i_int = PLAYING_S;
135 var_Set( p_playlist->p_input, "state", val );
138 p_playlist->request.p_node = p_playlist->status.p_node;
139 p_playlist->request.p_item = p_playlist->status.p_item;
140 p_playlist->request.i_skip = 0;
143 case PLAYLIST_AUTOPLAY:
144 p_playlist->status.i_status = PLAYLIST_RUNNING;
145 p_playlist->status.p_node = p_playlist->p_local_category;
146 p_playlist->request.b_request = VLC_FALSE;
151 if( p_playlist->p_input )
152 var_Get( p_playlist->p_input, "state", &val );
154 if( val.i_int == PAUSE_S )
156 p_playlist->status.i_status = PLAYLIST_RUNNING;
157 if( p_playlist->p_input )
159 val.i_int = PLAYING_S;
160 var_Set( p_playlist->p_input, "state", val );
165 p_playlist->status.i_status = PLAYLIST_PAUSED;
166 if( p_playlist->p_input )
169 var_Set( p_playlist->p_input, "state", val );
175 p_playlist->request.p_node = p_playlist->status.p_node;
176 p_playlist->request.p_item = p_playlist->status.p_item;
177 p_playlist->request.i_skip = (int) va_arg( args, int );
178 p_playlist->request.b_request = VLC_TRUE;
182 msg_Err( p_playlist, "unknown playlist query" );
190 /*****************************************************************************
192 *****************************************************************************/
193 /** Enqueue an item for preparsing */
194 int playlist_PreparseEnqueue( playlist_t *p_playlist,
195 input_item_t *p_item )
197 vlc_mutex_lock( &p_playlist->p_preparse->object_lock );
198 vlc_gc_incref( p_item );
199 INSERT_ELEM( p_playlist->p_preparse->pp_waiting,
200 p_playlist->p_preparse->i_waiting,
201 p_playlist->p_preparse->i_waiting,
203 vlc_mutex_unlock( &p_playlist->p_preparse->object_lock );
207 /** Enqueue a playlist item or a node for peparsing.
208 * This function should be entered without playlist and preparser locks */
209 int playlist_PreparseEnqueueItem( playlist_t *p_playlist,
210 playlist_item_t *p_item )
212 vlc_mutex_lock( &p_playlist->object_lock );
213 vlc_mutex_lock( &p_playlist->p_preparse->object_lock );
214 PreparseEnqueueItemSub( p_playlist, p_item );
215 vlc_mutex_unlock( &p_playlist->p_preparse->object_lock );
216 vlc_mutex_unlock( &p_playlist->object_lock );
220 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,
237 p_item->pp_children[i] );
242 /*****************************************************************************
244 *****************************************************************************/
246 /** This function calculates the next playlist item, depending
247 * on the playlist course mode (forward, backward, random, view,...). */
248 playlist_item_t * playlist_NextItem( playlist_t *p_playlist )
250 playlist_item_t *p_new = NULL;
253 vlc_bool_t b_loop = var_GetBool( p_playlist, "loop" );
254 vlc_bool_t b_random = var_GetBool( p_playlist, "random" );
255 vlc_bool_t b_repeat = var_GetBool( p_playlist, "repeat" );
256 vlc_bool_t b_playstop = var_GetBool( p_playlist, "play-and-stop" );
258 /* Handle quickly a few special cases */
260 /* No items to play */
261 if( p_playlist->i_size == 0 )
263 msg_Info( p_playlist, "playlist is empty" );
267 /* Repeat and play/stop */
268 if( !p_playlist->request.b_request && b_repeat == VLC_TRUE &&
269 p_playlist->status.p_item )
271 msg_Dbg( p_playlist,"repeating item" );
272 return p_playlist->status.p_item;
274 if( !p_playlist->request.b_request && b_playstop == VLC_TRUE )
276 msg_Dbg( p_playlist,"stopping (play and stop)");
280 if( !p_playlist->request.b_request && p_playlist->status.p_item &&
281 p_playlist->status.p_item->i_flags & PLAYLIST_SKIP_FLAG )
283 msg_Dbg( p_playlist, "blocking item, stopping") ;
287 /* Random case. This is an exception: if request, but request is skip +- 1
288 * we don't go to next item but select a new random one. */
290 msg_Err( p_playlist, "random unsupported" );
293 ( !p_playlist->request.b_request ||
294 ( p_playlist->request.b_request && ( p_playlist->request.p_item == NULL ||
295 p_playlist->request.i_skip == 1 || p_playlist->request.i_skip == -1 ) ) ) )
297 /* how many items to choose from ? */
299 for ( i = 0; i < p_playlist->i_size; i++ )
301 if ( p_playlist->pp_items[i]->p_input->i_nb_played == 0 )
307 /* Don't loop? Exit! */
310 /* Otherwise reset the counter */
311 for ( i = 0; i < p_playlist->i_size; i++ )
313 p_playlist->pp_items[i]->p_input->i_nb_played = 0;
315 i_count = p_playlist->i_size;
317 srand( (unsigned int)mdate() );
318 i = rand() % i_count + 1 ;
319 /* loop thru the list and count down the unplayed items to the selected one */
320 for ( i_new = 0; i_new < p_playlist->i_size && i > 0; i_new++ )
322 if ( p_playlist->pp_items[i_new]->p_input->i_nb_played == 0 )
327 p_playlist->request.i_skip = 0;
328 p_playlist->request.b_request = VLC_FALSE;
329 return p_playlist->pp_items[i_new];
333 /* Start the real work */
334 if( p_playlist->request.b_request )
336 PL_DEBUG( "processing request" );
337 p_new = p_playlist->request.p_item;
338 i_skip = p_playlist->request.i_skip;
340 p_playlist->status.p_node = p_playlist->request.p_node;
342 /* If we are asked for a node, take its first item */
344 ( p_new == NULL || p_new->i_children != -1 ) )
351 for( i = i_skip; i > 0 ; i-- )
353 p_new = playlist_GetNextEnabledLeaf( p_playlist,
354 p_playlist->request.p_node,
358 PL_DEBUG( "looping - restarting at beginning of node" );
359 p_new = playlist_GetNextLeaf( p_playlist,
360 p_playlist->request.p_node,
362 if( p_new == NULL ) break;
366 else if( i_skip < 0 )
368 for( i = i_skip; i < 0 ; i++ )
370 p_new = playlist_GetPrevLeaf( p_playlist,
371 p_playlist->request.p_node,
375 PL_DEBUG( "looping - restarting at end of node" );
376 /** \bug This is needed because GetPrevLeaf does not loop
378 p_new = playlist_GetLastLeaf( p_playlist,
379 p_playlist->request.p_node );
381 if( p_new == NULL ) break;
384 /* Clear the request */
385 p_playlist->request.b_request = VLC_FALSE;
387 /* "Automatic" item change ( next ) */
390 PL_DEBUG( "changing item without a request" );
391 /* Cant go to next from current item */
392 if( p_playlist->status.p_item &&
393 p_playlist->status.p_item->i_flags & PLAYLIST_SKIP_FLAG )
396 p_new = playlist_GetNextLeaf( p_playlist,
397 p_playlist->status.p_node,
398 p_playlist->status.p_item );
399 if( p_new == NULL && b_loop )
401 PL_DEBUG( "looping" );
402 p_new = playlist_GetNextLeaf( p_playlist,
403 p_playlist->status.p_node,
406 /* The new item can't be autoselected */
407 if( p_new != NULL && p_new->i_flags & PLAYLIST_SKIP_FLAG )
412 msg_Dbg( p_playlist, "did not find something to play" );
417 /** Start the input for an item */
418 int playlist_PlayItem( playlist_t *p_playlist, playlist_item_t *p_item )
421 int i_activity = var_GetInteger( p_playlist, "activity") ;
423 msg_Dbg( p_playlist, "creating new input thread" );
425 p_item->p_input->i_nb_played++;
426 p_playlist->status.p_item = p_item;
428 p_playlist->status.i_status = PLAYLIST_RUNNING;
430 var_SetInteger( p_playlist, "activity", i_activity +
431 DEFAULT_INPUT_ACTIVITY );
432 p_playlist->p_input = input_CreateThread( p_playlist, p_item->p_input );
434 val.i_int = p_item->p_input->i_id;
435 /* unlock the playlist to set the var...mmm */
436 vlc_mutex_unlock( &p_playlist->object_lock);
437 var_Set( p_playlist, "playlist-current", val);
438 vlc_mutex_lock( &p_playlist->object_lock);