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 #define PLAYLIST_DEBUG 1
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 )
85 playlist_item_t *p_item, *p_node;
88 if( p_playlist->i_size <= 0 )
96 p_playlist->request.i_status = PLAYLIST_STOPPED;
97 p_playlist->request.b_request = VLC_TRUE;
98 p_playlist->request.p_item = NULL;
101 case PLAYLIST_ITEMPLAY:
102 p_item = (playlist_item_t *)va_arg( args, playlist_item_t * );
103 if ( p_item == NULL || p_item->p_input->psz_uri == NULL )
105 p_playlist->request.i_status = PLAYLIST_RUNNING;
106 p_playlist->request.i_skip = 0;
107 p_playlist->request.b_request = VLC_TRUE;
108 p_playlist->request.p_item = p_item;
109 p_playlist->request.p_node = p_playlist->status.p_node;
112 case PLAYLIST_VIEWPLAY:
113 i_view = (int) va_arg( args, playlist_item_t *);
114 p_node = (playlist_item_t *)va_arg( args, playlist_item_t * );
115 p_item = (playlist_item_t *)va_arg( args, playlist_item_t * );
116 if ( p_node == NULL )
118 p_playlist->status.i_status = PLAYLIST_STOPPED;
119 p_playlist->request.b_request = VLC_TRUE;
120 msg_Err( p_playlist, "null node" );
123 p_playlist->request.i_status = PLAYLIST_RUNNING;
124 p_playlist->request.i_skip = 0;
125 p_playlist->request.b_request = VLC_TRUE;
126 p_playlist->request.p_node = p_node;
127 p_playlist->request.p_item = p_item;
131 p_playlist->request.i_status = PLAYLIST_RUNNING;
132 p_playlist->request.b_request = VLC_TRUE;
134 if( p_playlist->p_input )
136 val.i_int = PLAYING_S;
137 var_Set( p_playlist->p_input, "state", val );
140 p_playlist->request.p_node = p_playlist->status.p_node;
141 p_playlist->request.p_item = p_playlist->status.p_item;
142 p_playlist->request.i_skip = 0;
145 case PLAYLIST_AUTOPLAY:
146 p_playlist->status.i_status = PLAYLIST_RUNNING;
147 p_playlist->status.p_node = p_playlist->p_local_category;
148 p_playlist->request.b_request = VLC_FALSE;
153 if( p_playlist->p_input )
154 var_Get( p_playlist->p_input, "state", &val );
156 if( val.i_int == PAUSE_S )
158 p_playlist->status.i_status = PLAYLIST_RUNNING;
159 if( p_playlist->p_input )
161 val.i_int = PLAYING_S;
162 var_Set( p_playlist->p_input, "state", val );
167 p_playlist->status.i_status = PLAYLIST_PAUSED;
168 if( p_playlist->p_input )
171 var_Set( p_playlist->p_input, "state", val );
177 p_playlist->request.p_node = p_playlist->status.p_node;
178 p_playlist->request.p_item = p_playlist->status.p_item;
179 p_playlist->request.i_skip = (int) va_arg( args, int );
180 p_playlist->request.b_request = VLC_TRUE;
184 msg_Err( p_playlist, "unknown playlist query" );
192 /*****************************************************************************
194 *****************************************************************************/
195 /** Enqueue an item for preparsing */
196 int playlist_PreparseEnqueue( playlist_t *p_playlist,
197 input_item_t *p_item )
199 vlc_mutex_lock( &p_playlist->p_preparse->object_lock );
200 vlc_gc_incref( p_item );
201 INSERT_ELEM( p_playlist->p_preparse->pp_waiting,
202 p_playlist->p_preparse->i_waiting,
203 p_playlist->p_preparse->i_waiting,
205 vlc_mutex_unlock( &p_playlist->p_preparse->object_lock );
209 /** Enqueue a playlist item or a node for peparsing.
210 * This function should be entered without playlist and preparser locks */
211 int playlist_PreparseEnqueueItem( playlist_t *p_playlist,
212 playlist_item_t *p_item )
214 vlc_mutex_lock( &p_playlist->object_lock );
215 vlc_mutex_lock( &p_playlist->p_preparse->object_lock );
216 PreparseEnqueueItemSub( p_playlist, p_item );
217 vlc_mutex_unlock( &p_playlist->p_preparse->object_lock );
218 vlc_mutex_unlock( &p_playlist->object_lock );
222 void PreparseEnqueueItemSub( playlist_t *p_playlist,
223 playlist_item_t *p_item )
226 if( p_item->i_children == -1 )
228 vlc_gc_incref( p_item );
229 INSERT_ELEM( p_playlist->p_preparse->pp_waiting,
230 p_playlist->p_preparse->i_waiting,
231 p_playlist->p_preparse->i_waiting,
236 for( i = 0; i < p_item->i_children; i++)
238 PreparseEnqueueItemSub( p_playlist,
239 p_item->pp_children[i] );
244 /*****************************************************************************
246 *****************************************************************************/
248 /** This function calculates the next playlist item, depending
249 * on the playlist course mode (forward, backward, random, view,...). */
250 playlist_item_t * playlist_NextItem( playlist_t *p_playlist )
252 playlist_item_t *p_new = NULL;
255 vlc_bool_t b_loop = var_GetBool( p_playlist, "loop" );
256 vlc_bool_t b_random = var_GetBool( p_playlist, "random" );
257 vlc_bool_t b_repeat = var_GetBool( p_playlist, "repeat" );
258 vlc_bool_t b_playstop = var_GetBool( p_playlist, "play-and-stop" );
260 /* Handle quickly a few special cases */
262 /* No items to play */
263 if( p_playlist->i_size == 0 )
265 msg_Info( p_playlist, "playlist is empty" );
269 /* Repeat and play/stop */
270 if( !p_playlist->request.b_request && b_repeat == VLC_TRUE &&
271 p_playlist->status.p_item )
273 msg_Dbg( p_playlist,"repeating item" );
274 return p_playlist->status.p_item;
276 if( !p_playlist->request.b_request && b_playstop == VLC_TRUE )
278 msg_Dbg( p_playlist,"stopping (play and stop)");
282 if( !p_playlist->request.b_request && p_playlist->status.p_item &&
283 p_playlist->status.p_item->i_flags & PLAYLIST_SKIP_FLAG )
285 msg_Dbg( p_playlist, "blocking item, stopping") ;
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 msg_Err( p_playlist, "random unsupported" );
295 ( !p_playlist->request.b_request ||
296 ( p_playlist->request.b_request && ( p_playlist->request.p_item == NULL ||
297 p_playlist->request.i_skip == 1 || p_playlist->request.i_skip == -1 ) ) ) )
299 /* how many items to choose from ? */
301 for ( i = 0; i < p_playlist->i_size; i++ )
303 if ( p_playlist->pp_items[i]->p_input->i_nb_played == 0 )
309 /* Don't loop? Exit! */
312 /* Otherwise reset the counter */
313 for ( i = 0; i < p_playlist->i_size; i++ )
315 p_playlist->pp_items[i]->p_input->i_nb_played = 0;
317 i_count = p_playlist->i_size;
319 srand( (unsigned int)mdate() );
320 i = rand() % i_count + 1 ;
321 /* loop thru the list and count down the unplayed items to the selected one */
322 for ( i_new = 0; i_new < p_playlist->i_size && i > 0; i_new++ )
324 if ( p_playlist->pp_items[i_new]->p_input->i_nb_played == 0 )
329 p_playlist->request.i_skip = 0;
330 p_playlist->request.b_request = VLC_FALSE;
331 return p_playlist->pp_items[i_new];
335 /* Start the real work */
336 if( p_playlist->request.b_request )
338 #ifdef PLAYLIST_DEBUG
339 msg_Dbg( p_playlist,"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 #ifdef PLAYLIST_DEBUG
363 msg_Dbg( p_playlist, "looping - restarting at beginning "
366 p_new = playlist_GetNextLeaf( p_playlist,
367 p_playlist->request.p_node,
369 if( p_new == NULL ) break;
373 else if( i_skip < 0 )
375 for( i = i_skip; i < 0 ; i++ )
377 p_new = playlist_GetPrevLeaf( p_playlist,
378 p_playlist->request.p_node,
382 #ifdef PLAYLIST_DEBUG
383 msg_Dbg( p_playlist, "looping - restarting at end "
386 /** \bug This is needed because GetPrevLeaf does not loop
388 p_new = playlist_GetLastLeaf( p_playlist,
389 p_playlist->request.p_node );
391 if( p_new == NULL ) break;
394 /* Clear the request */
395 p_playlist->request.b_request = VLC_FALSE;
397 /* "Automatic" item change ( next ) */
400 #ifdef PLAYLIST_DEBUG
401 msg_Dbg( p_playlist,"changing item without a request" );
403 /* Cant go to next from current item */
404 if( p_playlist->status.p_item &&
405 p_playlist->status.p_item->i_flags & PLAYLIST_SKIP_FLAG )
408 p_new = playlist_GetNextLeaf( p_playlist,
409 p_playlist->status.p_node,
410 p_playlist->status.p_item );
411 if( p_new == NULL && b_loop )
413 #ifdef PLAYLIST_DEBUG
414 msg_Dbg( p_playlist, "looping" );
416 p_new = playlist_GetNextLeaf( p_playlist,
417 p_playlist->status.p_node,
420 /* The new item can't be autoselected */
421 if( p_new != NULL && p_new->i_flags & PLAYLIST_SKIP_FLAG )
426 msg_Dbg( p_playlist, "did not find something to play" );
431 /** Start the input for an item */
432 int playlist_PlayItem( playlist_t *p_playlist, playlist_item_t *p_item )
435 int i_activity = var_GetInteger( p_playlist, "activity") ;
437 msg_Dbg( p_playlist, "creating new input thread" );
439 p_item->p_input->i_nb_played++;
440 p_playlist->status.p_item = p_item;
442 p_playlist->status.i_status = PLAYLIST_RUNNING;
444 var_SetInteger( p_playlist, "activity", i_activity +
445 DEFAULT_INPUT_ACTIVITY );
446 p_playlist->p_input = input_CreateThread( p_playlist, p_item->p_input );
448 val.i_int = p_item->p_input->i_id;
449 /* unlock the playlist to set the var...mmm */
450 vlc_mutex_unlock( &p_playlist->object_lock);
451 var_Set( p_playlist, "playlist-current", val);
452 vlc_mutex_lock( &p_playlist->object_lock);