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;
79 p_playlist->request.i_status = PLAYLIST_RUNNING;
80 p_playlist->request.i_skip = 0;
81 p_playlist->request.b_request = VLC_TRUE;
82 p_playlist->request.p_node = p_node;
83 p_playlist->request.p_item = p_item;
84 if( p_item && var_GetBool( p_playlist, "random" ) )
85 p_playlist->b_reset_currently_playing = VLC_TRUE;
89 if( p_playlist->p_input )
91 val.i_int = PLAYING_S;
92 var_Set( p_playlist->p_input, "state", val );
97 p_playlist->request.i_status = PLAYLIST_RUNNING;
98 p_playlist->request.b_request = VLC_TRUE;
99 p_playlist->request.p_node = p_playlist->status.p_node;
100 p_playlist->request.p_item = p_playlist->status.p_item;
101 p_playlist->request.i_skip = 0;
105 case PLAYLIST_AUTOPLAY:
106 // AUTOPLAY is an ugly hack for initial status.
107 // Hopefully it will disappear
108 p_playlist->status.i_status = PLAYLIST_RUNNING;
109 p_playlist->request.p_node = p_playlist->status.p_node;
110 p_playlist->request.b_request = VLC_FALSE;
115 if( p_playlist->p_input )
116 var_Get( p_playlist->p_input, "state", &val );
118 if( val.i_int == PAUSE_S )
120 p_playlist->status.i_status = PLAYLIST_RUNNING;
121 if( p_playlist->p_input )
123 val.i_int = PLAYING_S;
124 var_Set( p_playlist->p_input, "state", val );
129 p_playlist->status.i_status = PLAYLIST_PAUSED;
130 if( p_playlist->p_input )
133 var_Set( p_playlist->p_input, "state", val );
139 p_playlist->request.p_node = p_playlist->status.p_node;
140 p_playlist->request.p_item = p_playlist->status.p_item;
141 p_playlist->request.i_skip = (int) va_arg( args, int );
142 /* if already running, keep running */
143 if( p_playlist->status.i_status != PLAYLIST_STOPPED )
144 p_playlist->request.i_status = p_playlist->status.i_status;
145 p_playlist->request.b_request = VLC_TRUE;
149 msg_Err( p_playlist, "unknown playlist query" );
153 vlc_cond_signal( &p_playlist->object_wait );
158 /*****************************************************************************
160 *****************************************************************************/
161 /** Enqueue an item for preparsing */
162 int playlist_PreparseEnqueue( playlist_t *p_playlist,
163 input_item_t *p_item )
165 vlc_mutex_lock( &p_playlist->p_preparse->object_lock );
166 vlc_gc_incref( p_item );
167 INSERT_ELEM( p_playlist->p_preparse->pp_waiting,
168 p_playlist->p_preparse->i_waiting,
169 p_playlist->p_preparse->i_waiting,
171 vlc_mutex_unlock( &p_playlist->p_preparse->object_lock );
172 vlc_cond_signal( &p_playlist->p_preparse->object_wait );
176 /** Enqueue a playlist item or a node for peparsing.
177 * This function should be entered without playlist and preparser locks */
178 int playlist_PreparseEnqueueItem( playlist_t *p_playlist,
179 playlist_item_t *p_item )
181 vlc_mutex_lock( &p_playlist->object_lock );
182 vlc_mutex_lock( &p_playlist->p_preparse->object_lock );
183 PreparseEnqueueItemSub( p_playlist, p_item );
184 vlc_mutex_unlock( &p_playlist->p_preparse->object_lock );
185 vlc_mutex_unlock( &p_playlist->object_lock );
189 int playlist_AskForArtEnqueue( playlist_t *p_playlist,
190 input_item_t *p_item )
195 p.b_fetch_art = VLC_TRUE;
197 vlc_mutex_lock( &p_playlist->p_fetcher->object_lock );
198 for( i = 0; i < p_playlist->p_fetcher->i_waiting &&
199 p_playlist->p_fetcher->p_waiting->b_fetch_art == VLC_TRUE;
201 vlc_gc_incref( p_item );
202 INSERT_ELEM( p_playlist->p_fetcher->p_waiting,
203 p_playlist->p_fetcher->i_waiting,
205 vlc_mutex_unlock( &p_playlist->p_fetcher->object_lock );
206 vlc_cond_signal( &p_playlist->p_fetcher->object_wait );
210 static void PreparseEnqueueItemSub( playlist_t *p_playlist,
211 playlist_item_t *p_item )
214 if( p_item->i_children == -1 )
216 vlc_gc_incref( p_item );
217 INSERT_ELEM( p_playlist->p_preparse->pp_waiting,
218 p_playlist->p_preparse->i_waiting,
219 p_playlist->p_preparse->i_waiting,
224 for( i = 0; i < p_item->i_children; i++)
226 PreparseEnqueueItemSub( p_playlist, p_item->pp_children[i] );
231 /*****************************************************************************
233 *****************************************************************************/
235 static void ResyncCurrentIndex(playlist_t *p_playlist, playlist_item_t *p_cur )
237 PL_DEBUG("resyncing on %s", PLI_NAME(p_cur) );
238 /* Simply resync index */
240 p_playlist->i_current_index = -1;
241 for( i = 0 ; i< p_playlist->current.i_size; i++ )
243 if( ARRAY_VAL(p_playlist->current, i) == p_cur )
245 p_playlist->i_current_index = i;
249 PL_DEBUG("%s is at %i", PLI_NAME(p_cur), p_playlist->i_current_index );
252 void ResetCurrentlyPlaying( playlist_t *p_playlist, vlc_bool_t b_random,
253 playlist_item_t *p_cur )
255 playlist_item_t *p_next = NULL;
256 stats_TimerStart( p_playlist, "Items array build",
257 STATS_TIMER_PLAYLIST_BUILD );
258 PL_DEBUG("rebuilding array of current - root %s",
259 PLI_NAME(p_playlist->status.p_node) );
260 ARRAY_RESET(p_playlist->current);
261 p_playlist->i_current_index = -1;
264 /** FIXME: this is *slow* */
265 p_next = playlist_GetNextLeaf( p_playlist,
266 p_playlist->status.p_node,
267 p_next, VLC_TRUE, VLC_FALSE );
270 if( p_next == p_cur )
271 p_playlist->i_current_index = p_playlist->current.i_size;
272 ARRAY_APPEND( p_playlist->current, p_next);
276 PL_DEBUG("rebuild done - %i items, index %i", p_playlist->current.i_size,
277 p_playlist->i_current_index);
280 /* Shuffle the array */
281 srand( (unsigned int)mdate() );
284 for( j = p_playlist->current.i_size - 1; j > 0; j-- )
287 int i = rand() % (j+1); /* between 0 and j */
288 playlist_item_t *p_tmp;
289 p_tmp = ARRAY_VAL(p_playlist->current, i);
290 ARRAY_VAL(p_playlist->current,i) = ARRAY_VAL(p_playlist->current,j);
291 ARRAY_VAL(p_playlist->current,j) = p_tmp;
294 p_playlist->b_reset_currently_playing = VLC_FALSE;
295 stats_TimerStop( p_playlist, STATS_TIMER_PLAYLIST_BUILD );
298 /** This function calculates the next playlist item, depending
299 * on the playlist course mode (forward, backward, random, view,...). */
300 playlist_item_t * playlist_NextItem( playlist_t *p_playlist )
302 playlist_item_t *p_new = NULL;
305 vlc_bool_t b_loop = var_GetBool( p_playlist, "loop" );
306 vlc_bool_t b_random = var_GetBool( p_playlist, "random" );
307 vlc_bool_t b_repeat = var_GetBool( p_playlist, "repeat" );
308 vlc_bool_t b_playstop = var_GetBool( p_playlist, "play-and-stop" );
310 /* Handle quickly a few special cases */
311 /* No items to play */
312 if( p_playlist->items.i_size == 0 )
314 msg_Info( p_playlist, "playlist is empty" );
318 /* Repeat and play/stop */
319 if( !p_playlist->request.b_request && b_repeat == VLC_TRUE &&
320 p_playlist->status.p_item )
322 msg_Dbg( p_playlist,"repeating item" );
323 return p_playlist->status.p_item;
325 if( !p_playlist->request.b_request && b_playstop == VLC_TRUE )
327 msg_Dbg( p_playlist,"stopping (play and stop)");
331 if( !p_playlist->request.b_request && p_playlist->status.p_item )
333 playlist_item_t *p_parent = p_playlist->status.p_item;
336 if( p_parent->i_flags & PLAYLIST_SKIP_FLAG )
338 msg_Dbg( p_playlist, "blocking item, stopping") ;
341 p_parent = p_parent->p_parent;
345 /* Start the real work */
346 if( p_playlist->request.b_request )
348 p_new = p_playlist->request.p_item;
349 i_skip = p_playlist->request.i_skip;
350 PL_DEBUG( "processing request item %s node %s skip %i",
351 PLI_NAME( p_playlist->request.p_item ),
352 PLI_NAME( p_playlist->request.p_node ), i_skip );
354 if( p_playlist->request.p_node &&
355 p_playlist->request.p_node != p_playlist->status.p_node )
357 p_playlist->status.p_node = p_playlist->request.p_node;
358 p_playlist->b_reset_currently_playing = VLC_TRUE;
361 /* If we are asked for a node, dont take it */
362 if( i_skip == 0 && ( p_new == NULL || p_new->i_children != -1 ) )
365 if( p_playlist->b_reset_currently_playing )
366 /* A bit too bad to reset twice ... */
367 ResetCurrentlyPlaying( p_playlist, b_random, p_new );
369 ResyncCurrentIndex( p_playlist, p_new );
371 p_playlist->i_current_index = -1;
373 if( p_playlist->current.i_size && i_skip > 0 )
375 for( i = i_skip; i > 0 ; i-- )
377 p_playlist->i_current_index++;
378 if( p_playlist->i_current_index == p_playlist->current.i_size )
380 PL_DEBUG( "looping - restarting at beginning of node" );
381 p_playlist->i_current_index = 0;
384 p_new = ARRAY_VAL( p_playlist->current,
385 p_playlist->i_current_index );
387 else if( p_playlist->current.i_size && i_skip < 0 )
389 for( i = i_skip; i < 0 ; i++ )
391 p_playlist->i_current_index--;
392 if( p_playlist->i_current_index == -1 )
394 PL_DEBUG( "looping - restarting at end of node" );
395 p_playlist->i_current_index = p_playlist->current.i_size-1;
398 p_new = ARRAY_VAL( p_playlist->current,
399 p_playlist->i_current_index );
401 /* Clear the request */
402 p_playlist->request.b_request = VLC_FALSE;
404 /* "Automatic" item change ( next ) */
407 PL_DEBUG( "changing item without a request (current %i/%i)",
408 p_playlist->i_current_index, p_playlist->current.i_size );
409 /* Cant go to next from current item */
410 if( p_playlist->status.p_item &&
411 p_playlist->status.p_item->i_flags & PLAYLIST_SKIP_FLAG )
414 if( p_playlist->b_reset_currently_playing )
415 ResetCurrentlyPlaying( p_playlist, b_random,
416 p_playlist->status.p_item );
418 p_playlist->i_current_index++;
419 if( p_playlist->i_current_index == p_playlist->current.i_size )
421 if( !b_loop || p_playlist->current.i_size == 0 ) return NULL;
422 p_playlist->i_current_index = 0;
424 PL_DEBUG( "using item %i", p_playlist->i_current_index );
425 if ( p_playlist->current.i_size == 0 ) return NULL;
427 p_new = ARRAY_VAL( p_playlist->current, p_playlist->i_current_index );
428 /* The new item can't be autoselected */
429 if( p_new != NULL && p_new->i_flags & PLAYLIST_SKIP_FLAG )
435 /** Start the input for an item */
436 int playlist_PlayItem( playlist_t *p_playlist, playlist_item_t *p_item )
439 input_item_t *p_input = p_item->p_input;
440 int i_activity = var_GetInteger( p_playlist, "activity") ;
442 msg_Dbg( p_playlist, "creating new input thread" );
444 p_input->i_nb_played++;
445 p_playlist->status.p_item = p_item;
447 p_playlist->status.i_status = PLAYLIST_RUNNING;
449 var_SetInteger( p_playlist, "activity", i_activity +
450 DEFAULT_INPUT_ACTIVITY );
451 p_playlist->p_input = input_CreateThread( p_playlist, p_input );
453 if( p_playlist->p_fetcher->i_art_policy == ALBUM_ART_WHEN_PLAYED )
455 if( p_input->p_meta && EMPTY_STR( p_input->p_meta->psz_arturl ) )
457 PL_DEBUG( "requesting art for %s", p_input->psz_name );
458 playlist_AskForArtEnqueue( p_playlist, p_input );
460 else if( !p_input->p_meta )
462 PL_DEBUG2( "unable to request art for %s, no meta", p_input->psz_name );
466 val.i_int = p_input->i_id;
467 vlc_mutex_unlock( &p_playlist->object_lock);
468 var_Set( p_playlist, "playlist-current", val);
469 vlc_mutex_lock( &p_playlist->object_lock);