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_playlist.h"
26 #include "playlist_internal.h"
29 /*****************************************************************************
31 *****************************************************************************/
32 static int PlaylistVAControl( playlist_t * p_playlist, int i_query, va_list args );
34 static void PreparseEnqueueItemSub( playlist_t *, playlist_item_t * );
36 /*****************************************************************************
38 *****************************************************************************/
40 int playlist_Control( playlist_t * p_playlist, int i_query, vlc_bool_t b_locked, ... )
44 va_start( args, b_locked );
45 if( !b_locked ) PL_LOCK;
46 i_result = PlaylistVAControl( p_playlist, i_query, args );
48 if( !b_locked ) PL_UNLOCK;
53 static int PlaylistVAControl( playlist_t * p_playlist, int i_query, va_list args )
55 playlist_item_t *p_item, *p_node;
58 if( playlist_IsEmpty( p_playlist ) )
64 p_playlist->request.i_status = PLAYLIST_STOPPED;
65 p_playlist->request.b_request = VLC_TRUE;
66 p_playlist->request.p_item = NULL;
69 // Node can be null, it will keep the same. Use with care ...
70 // Item null = take the first child of node
71 case PLAYLIST_VIEWPLAY:
72 p_node = (playlist_item_t *)va_arg( args, playlist_item_t * );
73 p_item = (playlist_item_t *)va_arg( args, playlist_item_t * );
76 p_node = p_playlist->status.p_node;
81 p_playlist->request.i_status = PLAYLIST_RUNNING;
82 p_playlist->request.i_skip = 0;
83 p_playlist->request.b_request = VLC_TRUE;
84 p_playlist->request.p_node = p_node;
85 p_playlist->request.p_item = p_item;
86 if( p_item && var_GetBool( p_playlist, "random" ) )
87 p_playlist->b_reset_currently_playing = VLC_TRUE;
91 if( p_playlist->p_input )
93 val.i_int = PLAYING_S;
94 var_Set( p_playlist->p_input, "state", val );
99 p_playlist->request.i_status = PLAYLIST_RUNNING;
100 p_playlist->request.b_request = VLC_TRUE;
101 p_playlist->request.p_node = p_playlist->status.p_node;
102 p_playlist->request.p_item = p_playlist->status.p_item;
103 p_playlist->request.i_skip = 0;
107 case PLAYLIST_AUTOPLAY:
108 // AUTOPLAY is an ugly hack for initial status.
109 // Hopefully it will disappear
110 p_playlist->status.i_status = PLAYLIST_RUNNING;
111 p_playlist->request.p_node = p_playlist->status.p_node;
112 p_playlist->request.b_request = VLC_FALSE;
117 if( p_playlist->p_input )
118 var_Get( p_playlist->p_input, "state", &val );
120 if( val.i_int == PAUSE_S )
122 p_playlist->status.i_status = PLAYLIST_RUNNING;
123 if( p_playlist->p_input )
125 val.i_int = PLAYING_S;
126 var_Set( p_playlist->p_input, "state", val );
131 p_playlist->status.i_status = PLAYLIST_PAUSED;
132 if( p_playlist->p_input )
135 var_Set( p_playlist->p_input, "state", val );
141 p_playlist->request.p_node = p_playlist->status.p_node;
142 p_playlist->request.p_item = p_playlist->status.p_item;
143 p_playlist->request.i_skip = (int) va_arg( args, int );
144 /* if already running, keep running */
145 if( p_playlist->status.i_status != PLAYLIST_STOPPED )
146 p_playlist->request.i_status = p_playlist->status.i_status;
147 p_playlist->request.b_request = VLC_TRUE;
151 msg_Err( p_playlist, "unknown playlist query" );
155 vlc_cond_signal( &p_playlist->object_wait );
160 /*****************************************************************************
162 *****************************************************************************/
163 /** Enqueue an item for preparsing */
164 int playlist_PreparseEnqueue( playlist_t *p_playlist,
165 input_item_t *p_item )
167 vlc_mutex_lock( &p_playlist->p_preparse->object_lock );
168 vlc_gc_incref( p_item );
169 INSERT_ELEM( p_playlist->p_preparse->pp_waiting,
170 p_playlist->p_preparse->i_waiting,
171 p_playlist->p_preparse->i_waiting,
173 vlc_mutex_unlock( &p_playlist->p_preparse->object_lock );
174 vlc_cond_signal( &p_playlist->p_preparse->object_wait );
178 /** Enqueue a playlist item or a node for peparsing.
179 * This function should be entered without playlist and preparser locks */
180 int playlist_PreparseEnqueueItem( playlist_t *p_playlist,
181 playlist_item_t *p_item )
183 vlc_mutex_lock( &p_playlist->object_lock );
184 vlc_mutex_lock( &p_playlist->p_preparse->object_lock );
185 PreparseEnqueueItemSub( p_playlist, p_item );
186 vlc_mutex_unlock( &p_playlist->p_preparse->object_lock );
187 vlc_mutex_unlock( &p_playlist->object_lock );
191 int playlist_AskForArtEnqueue( playlist_t *p_playlist,
192 input_item_t *p_item )
197 p.b_fetch_art = VLC_TRUE;
199 vlc_mutex_lock( &p_playlist->p_fetcher->object_lock );
200 for( i = 0; i < p_playlist->p_fetcher->i_waiting &&
201 p_playlist->p_fetcher->p_waiting->b_fetch_art == VLC_TRUE;
203 vlc_gc_incref( p_item );
204 INSERT_ELEM( p_playlist->p_fetcher->p_waiting,
205 p_playlist->p_fetcher->i_waiting,
207 vlc_mutex_unlock( &p_playlist->p_fetcher->object_lock );
208 vlc_cond_signal( &p_playlist->p_fetcher->object_wait );
212 static void PreparseEnqueueItemSub( playlist_t *p_playlist,
213 playlist_item_t *p_item )
216 if( p_item->i_children == -1 )
218 vlc_gc_incref( p_item );
219 INSERT_ELEM( p_playlist->p_preparse->pp_waiting,
220 p_playlist->p_preparse->i_waiting,
221 p_playlist->p_preparse->i_waiting,
226 for( i = 0; i < p_item->i_children; i++)
228 PreparseEnqueueItemSub( p_playlist, p_item->pp_children[i] );
233 /*****************************************************************************
235 *****************************************************************************/
237 static void ResyncCurrentIndex(playlist_t *p_playlist, playlist_item_t *p_cur )
239 PL_DEBUG("resyncing on %s", PLI_NAME(p_cur) );
240 /* Simply resync index */
242 p_playlist->i_current_index = -1;
243 for( i = 0 ; i< p_playlist->current.i_size; i++ )
245 if( ARRAY_VAL(p_playlist->current, i) == p_cur )
247 p_playlist->i_current_index = i;
251 PL_DEBUG("%s is at %i", PLI_NAME(p_cur), p_playlist->i_current_index );
254 void ResetCurrentlyPlaying( playlist_t *p_playlist, vlc_bool_t b_random,
255 playlist_item_t *p_cur )
257 playlist_item_t *p_next = NULL;
258 stats_TimerStart( p_playlist, "Items array build",
259 STATS_TIMER_PLAYLIST_BUILD );
260 PL_DEBUG("rebuilding array of current - root %s",
261 PLI_NAME(p_playlist->status.p_node) );
262 ARRAY_RESET(p_playlist->current);
263 p_playlist->i_current_index = -1;
266 /** FIXME: this is *slow* */
267 p_next = playlist_GetNextLeaf( p_playlist,
268 p_playlist->status.p_node,
269 p_next, VLC_TRUE, VLC_FALSE );
272 if( p_next == p_cur )
273 p_playlist->i_current_index = p_playlist->current.i_size;
274 ARRAY_APPEND( p_playlist->current, p_next);
278 PL_DEBUG("rebuild done - %i items, index %i", p_playlist->current.i_size,
279 p_playlist->i_current_index);
282 /* Shuffle the array */
283 srand( (unsigned int)mdate() );
286 for( j = p_playlist->current.i_size - 1; j > 0; j-- )
289 int i = rand() % (j+1); /* between 0 and j */
290 playlist_item_t *p_tmp;
291 p_tmp = ARRAY_VAL(p_playlist->current, i);
292 ARRAY_VAL(p_playlist->current,i) = ARRAY_VAL(p_playlist->current,j);
293 ARRAY_VAL(p_playlist->current,j) = p_tmp;
296 p_playlist->b_reset_currently_playing = VLC_FALSE;
297 stats_TimerStop( p_playlist, STATS_TIMER_PLAYLIST_BUILD );
300 /** This function calculates the next playlist item, depending
301 * on the playlist course mode (forward, backward, random, view,...). */
302 playlist_item_t * playlist_NextItem( playlist_t *p_playlist )
304 playlist_item_t *p_new = NULL;
307 vlc_bool_t b_loop = var_GetBool( p_playlist, "loop" );
308 vlc_bool_t b_random = var_GetBool( p_playlist, "random" );
309 vlc_bool_t b_repeat = var_GetBool( p_playlist, "repeat" );
310 vlc_bool_t b_playstop = var_GetBool( p_playlist, "play-and-stop" );
312 /* Handle quickly a few special cases */
313 /* No items to play */
314 if( p_playlist->items.i_size == 0 )
316 msg_Info( p_playlist, "playlist is empty" );
320 /* Repeat and play/stop */
321 if( !p_playlist->request.b_request && b_repeat == VLC_TRUE &&
322 p_playlist->status.p_item )
324 msg_Dbg( p_playlist,"repeating item" );
325 return p_playlist->status.p_item;
327 if( !p_playlist->request.b_request && b_playstop == VLC_TRUE )
329 msg_Dbg( p_playlist,"stopping (play and stop)");
333 if( !p_playlist->request.b_request && p_playlist->status.p_item )
335 playlist_item_t *p_parent = p_playlist->status.p_item;
338 if( p_parent->i_flags & PLAYLIST_SKIP_FLAG )
340 msg_Dbg( p_playlist, "blocking item, stopping") ;
343 p_parent = p_parent->p_parent;
347 /* Start the real work */
348 if( p_playlist->request.b_request )
350 p_new = p_playlist->request.p_item;
351 i_skip = p_playlist->request.i_skip;
352 PL_DEBUG( "processing request item %s node %s skip %i",
353 PLI_NAME( p_playlist->request.p_item ),
354 PLI_NAME( p_playlist->request.p_node ), i_skip );
356 if( p_playlist->request.p_node &&
357 p_playlist->request.p_node != p_playlist->status.p_node )
359 p_playlist->status.p_node = p_playlist->request.p_node;
360 p_playlist->b_reset_currently_playing = VLC_TRUE;
363 /* If we are asked for a node, dont take it */
364 if( i_skip == 0 && ( p_new == NULL || p_new->i_children != -1 ) )
367 if( p_playlist->b_reset_currently_playing )
368 /* A bit too bad to reset twice ... */
369 ResetCurrentlyPlaying( p_playlist, b_random, p_new );
371 ResyncCurrentIndex( p_playlist, p_new );
373 p_playlist->i_current_index = -1;
375 if( p_playlist->current.i_size && i_skip > 0 )
377 for( i = i_skip; i > 0 ; i-- )
379 p_playlist->i_current_index++;
380 if( p_playlist->i_current_index == p_playlist->current.i_size )
382 PL_DEBUG( "looping - restarting at beginning of node" );
383 p_playlist->i_current_index = 0;
386 p_new = ARRAY_VAL( p_playlist->current,
387 p_playlist->i_current_index );
389 else if( p_playlist->current.i_size && i_skip < 0 )
391 for( i = i_skip; i < 0 ; i++ )
393 p_playlist->i_current_index--;
394 if( p_playlist->i_current_index == -1 )
396 PL_DEBUG( "looping - restarting at end of node" );
397 p_playlist->i_current_index = p_playlist->current.i_size-1;
400 p_new = ARRAY_VAL( p_playlist->current,
401 p_playlist->i_current_index );
403 /* Clear the request */
404 p_playlist->request.b_request = VLC_FALSE;
406 /* "Automatic" item change ( next ) */
409 PL_DEBUG( "changing item without a request (current %i/%i)",
410 p_playlist->i_current_index, p_playlist->current.i_size );
411 /* Cant go to next from current item */
412 if( p_playlist->status.p_item &&
413 p_playlist->status.p_item->i_flags & PLAYLIST_SKIP_FLAG )
416 if( p_playlist->b_reset_currently_playing )
417 ResetCurrentlyPlaying( p_playlist, b_random,
418 p_playlist->status.p_item );
420 p_playlist->i_current_index++;
421 if( p_playlist->i_current_index == p_playlist->current.i_size )
423 if( !b_loop || p_playlist->current.i_size == 0 ) return NULL;
424 p_playlist->i_current_index = 0;
426 PL_DEBUG( "using item %i", p_playlist->i_current_index );
427 if ( p_playlist->current.i_size == 0 ) return NULL;
429 p_new = ARRAY_VAL( p_playlist->current, p_playlist->i_current_index );
430 /* The new item can't be autoselected */
431 if( p_new != NULL && p_new->i_flags & PLAYLIST_SKIP_FLAG )
437 /** Start the input for an item */
438 int playlist_PlayItem( playlist_t *p_playlist, playlist_item_t *p_item )
441 input_item_t *p_input = p_item->p_input;
442 int i_activity = var_GetInteger( p_playlist, "activity") ;
444 msg_Dbg( p_playlist, "creating new input thread" );
446 p_input->i_nb_played++;
447 p_playlist->status.p_item = p_item;
449 p_playlist->status.i_status = PLAYLIST_RUNNING;
451 var_SetInteger( p_playlist, "activity", i_activity +
452 DEFAULT_INPUT_ACTIVITY );
453 p_playlist->p_input = input_CreateThread( p_playlist, p_input );
455 if( p_playlist->p_fetcher->i_art_policy == ALBUM_ART_WHEN_PLAYED )
457 if( p_input->p_meta && EMPTY_STR( p_input->p_meta->psz_arturl ) )
459 PL_DEBUG( "requesting art for %s", p_input->psz_name );
460 playlist_AskForArtEnqueue( p_playlist, p_input );
462 else if( !p_input->p_meta )
464 PL_DEBUG2( "unable to request art for %s, no meta", p_input->psz_name );
468 val.i_int = p_input->i_id;
469 vlc_mutex_unlock( &p_playlist->object_lock);
470 var_Set( p_playlist, "playlist-current", val);
471 vlc_mutex_lock( &p_playlist->object_lock);