1 /*****************************************************************************
2 * playlist.c : Playlist management functions
3 *****************************************************************************
4 * Copyright (C) 1999-2004 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
24 #include <stdlib.h> /* free(), strtol() */
25 #include <stdio.h> /* sprintf() */
26 #include <string.h> /* strerror() */
31 #include <vlc/input.h>
33 #include "vlc_playlist.h"
35 #define TITLE_CATEGORY N_( "By category" )
36 #define TITLE_SIMPLE N_( "Manually added" )
37 #define TITLE_ALL N_( "All items, unsorted" )
39 #define PLAYLIST_PROFILE 1
41 /*****************************************************************************
43 *****************************************************************************/
44 static void RunThread ( playlist_t * );
45 static playlist_item_t * NextItem ( playlist_t * );
46 static void PlayItem ( playlist_t *, playlist_item_t * );
48 static int ItemChange( vlc_object_t *, const char *,
49 vlc_value_t, vlc_value_t, void * );
51 int playlist_vaControl( playlist_t * p_playlist, int i_query, va_list args );
57 * Create a playlist structure.
58 * \param p_parent the vlc object that is to be the parent of this playlist
59 * \return a pointer to the created playlist, or NULL on error
61 playlist_t * __playlist_Create ( vlc_object_t *p_parent )
64 playlist_t *p_playlist;
65 playlist_view_t *p_view;
68 /* Allocate structure */
69 p_playlist = vlc_object_create( p_parent, VLC_OBJECT_PLAYLIST );
72 msg_Err( p_parent, "out of memory" );
76 /* These variables control updates */
77 var_Create( p_playlist, "intf-change", VLC_VAR_BOOL );
78 val.b_bool = VLC_TRUE;
79 var_Set( p_playlist, "intf-change", val );
81 var_Create( p_playlist, "item-change", VLC_VAR_INTEGER );
83 var_Set( p_playlist, "item-change", val );
85 var_Create( p_playlist, "playlist-current", VLC_VAR_INTEGER );
87 var_Set( p_playlist, "playlist-current", val );
89 var_Create( p_playlist, "intf-popupmenu", VLC_VAR_BOOL );
91 var_Create( p_playlist, "intf-show", VLC_VAR_BOOL );
92 val.b_bool = VLC_TRUE;
93 var_Set( p_playlist, "intf-show", val );
96 /* Variables to control playback */
97 var_CreateGetBool( p_playlist, "play-and-stop" );
98 var_CreateGetBool( p_playlist, "random" );
99 var_CreateGetBool( p_playlist, "repeat" );
100 var_CreateGetBool( p_playlist, "loop" );
102 /* Initialise data structures */
103 p_playlist->b_go_next = VLC_TRUE;
104 p_playlist->p_input = NULL;
106 p_playlist->request_date = 0;
108 p_playlist->i_views = 0;
109 p_playlist->pp_views = NULL;
111 p_playlist->i_index = -1;
112 p_playlist->i_size = 0;
113 p_playlist->pp_items = NULL;
115 playlist_ViewInsert( p_playlist, VIEW_CATEGORY, TITLE_CATEGORY );
116 playlist_ViewInsert( p_playlist, VIEW_SIMPLE, TITLE_SIMPLE );
117 playlist_ViewInsert( p_playlist, VIEW_ALL, TITLE_ALL );
119 p_view = playlist_ViewFind( p_playlist, VIEW_CATEGORY );
121 p_playlist->p_general = playlist_NodeCreate( p_playlist, VIEW_CATEGORY,
122 _( "General" ), p_view->p_root );
124 /* Set startup status
125 * We set to simple view on startup for interfaces that don't do
127 p_view = playlist_ViewFind( p_playlist, VIEW_SIMPLE );
128 p_playlist->status.i_view = VIEW_SIMPLE;
129 p_playlist->status.p_item = NULL;
130 p_playlist->status.p_node = p_view->p_root;
131 p_playlist->request.b_request = VLC_FALSE;
132 p_playlist->status.i_status = PLAYLIST_STOPPED;
135 p_playlist->i_last_id = 0;
136 p_playlist->i_sort = SORT_ID;
137 p_playlist->i_order = ORDER_NORMAL;
139 /* Finally, launch the thread ! */
140 if( vlc_thread_create( p_playlist, "playlist", RunThread,
141 VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
143 msg_Err( p_playlist, "cannot spawn playlist thread" );
144 vlc_object_destroy( p_playlist );
148 /* The object has been initialized, now attach it */
149 vlc_object_attach( p_playlist, p_parent );
155 * Destroy the playlist.
157 * Delete all items in the playlist and free the playlist structure.
158 * \param p_playlist the playlist structure to destroy
160 void playlist_Destroy( playlist_t * p_playlist )
163 p_playlist->b_die = 1;
165 for( i = 0 ; i< p_playlist->i_sds ; i++ )
167 playlist_ServicesDiscoveryRemove( p_playlist,
168 p_playlist->pp_sds[i]->psz_module );
171 vlc_thread_join( p_playlist );
173 var_Destroy( p_playlist, "intf-change" );
174 var_Destroy( p_playlist, "item-change" );
175 var_Destroy( p_playlist, "playlist-current" );
176 var_Destroy( p_playlist, "intf-popmenu" );
177 var_Destroy( p_playlist, "intf-show" );
178 var_Destroy( p_playlist, "play-and-stop" );
179 var_Destroy( p_playlist, "random" );
180 var_Destroy( p_playlist, "repeat" );
181 var_Destroy( p_playlist, "loop" );
183 playlist_Clear( p_playlist );
185 for( i = p_playlist->i_views - 1; i >= 0 ; i-- )
187 playlist_view_t *p_view = p_playlist->pp_views[i];
188 if( p_view->psz_name )
189 free( p_view->psz_name );
190 playlist_ItemDelete( p_view->p_root );
191 REMOVE_ELEM( p_playlist->pp_views, p_playlist->i_views, i );
195 vlc_object_destroy( p_playlist );
200 * Do a playlist action.
202 * If there is something in the playlist then you can do playlist actions.
204 * Playlist lock must not be taken when calling this function
206 * \param p_playlist the playlist to do the command on
207 * \param i_query the command to do
208 * \param variable number of arguments
209 * \return VLC_SUCCESS or an error
211 int playlist_Control( playlist_t * p_playlist, int i_query, ... )
215 va_start( args, i_query );
216 i_result = playlist_vaControl( p_playlist, i_query, args );
222 int playlist_vaControl( playlist_t * p_playlist, int i_query, va_list args )
224 vlc_mutex_lock( &p_playlist->object_lock );
226 playlist_view_t *p_view;
229 #ifdef PLAYLIST_PROFILE
230 p_playlist->request_date = mdate();
233 if( p_playlist->i_size <= 0 )
235 vlc_mutex_unlock( &p_playlist->object_lock );
242 p_playlist->status.i_status = PLAYLIST_STOPPED;
243 p_playlist->request.b_request = VLC_TRUE;
246 case PLAYLIST_ITEMPLAY:
247 p_playlist->status.i_status = PLAYLIST_RUNNING;
248 p_playlist->request.i_skip = 0;
249 p_playlist->request.b_request = VLC_TRUE;
250 p_playlist->request.p_item = (playlist_item_t *)va_arg( args,
252 p_playlist->request.i_view = p_playlist->status.i_view;
253 p_view = playlist_ViewFind( p_playlist, p_playlist->status.i_view );
254 p_playlist->request.p_node = p_view->p_root;
257 case PLAYLIST_VIEWPLAY:
258 p_playlist->status.i_status = PLAYLIST_RUNNING;
259 p_playlist->request.i_skip = 0;
260 p_playlist->request.b_request = VLC_TRUE;
261 p_playlist->request.i_view = (int)va_arg( args,int );
262 p_playlist->request.p_node = (playlist_item_t *)va_arg( args,
264 p_playlist->request.p_item = (playlist_item_t *)va_arg( args,
267 /* If we select a node, play only it.
268 * If we select an item, continue */
269 if( p_playlist->request.p_item == NULL ||
270 ! p_playlist->request.p_node->i_flags & PLAYLIST_SKIP_FLAG )
272 p_playlist->b_go_next = VLC_FALSE;
276 p_playlist->b_go_next = VLC_TRUE;
281 p_playlist->status.i_status = PLAYLIST_RUNNING;
283 if( p_playlist->p_input )
285 val.i_int = PLAYING_S;
286 var_Set( p_playlist->p_input, "state", val );
290 /* FIXME : needed ? */
291 p_playlist->request.b_request = VLC_TRUE;
292 p_playlist->request.i_view = p_playlist->status.i_view;
293 p_playlist->request.p_node = p_playlist->status.p_node;
294 p_playlist->request.p_item = p_playlist->status.p_item;
295 p_playlist->request.i_skip = 0;
296 p_playlist->request.i_goto = -1;
301 if( p_playlist->p_input )
302 var_Get( p_playlist->p_input, "state", &val );
304 if( val.i_int == PAUSE_S )
306 p_playlist->status.i_status = PLAYLIST_RUNNING;
307 if( p_playlist->p_input )
309 val.i_int = PLAYING_S;
310 var_Set( p_playlist->p_input, "state", val );
315 p_playlist->status.i_status = PLAYLIST_PAUSED;
316 if( p_playlist->p_input )
319 var_Set( p_playlist->p_input, "state", val );
325 if( p_playlist->status.i_view > -1 )
327 p_playlist->request.p_node = p_playlist->status.p_node;
328 p_playlist->request.p_item = p_playlist->status.p_item;
330 p_playlist->request.i_skip = (int) va_arg( args, int );
331 p_playlist->request.b_request = VLC_TRUE;
335 p_playlist->status.i_status = PLAYLIST_RUNNING;
336 p_playlist->request.p_node = NULL;
337 p_playlist->request.p_item = NULL;
338 p_playlist->request.i_view = -1;
339 p_playlist->request.i_goto = (int) va_arg( args, int );
340 p_playlist->request.b_request = VLC_TRUE;
344 msg_Err( p_playlist, "unimplemented playlist query" );
349 vlc_mutex_unlock( &p_playlist->object_lock );
354 /* Destroy remaining objects */
355 static mtime_t ObjectGarbageCollector( playlist_t *p_playlist, int i_type,
356 mtime_t destroy_date )
360 if( destroy_date > mdate() ) return destroy_date;
362 if( destroy_date == 0 )
364 /* give a little time */
365 return mdate() + I64C(1000000);
369 while( ( p_obj = vlc_object_find( p_playlist, i_type, FIND_CHILD ) ) )
371 if( p_obj->p_parent != (vlc_object_t*)p_playlist )
373 /* only first child (ie unused) */
374 vlc_object_release( p_obj );
377 if( i_type == VLC_OBJECT_VOUT )
379 msg_Dbg( p_playlist, "garbage collector destroying 1 vout" );
380 vlc_object_detach( p_obj );
381 vlc_object_release( p_obj );
382 vout_Destroy( (vout_thread_t *)p_obj );
384 else if( i_type == VLC_OBJECT_SOUT )
386 vlc_object_release( p_obj );
387 sout_DeleteInstance( (sout_instance_t*)p_obj );
394 /*****************************************************************************
395 * RunThread: main playlist thread
396 *****************************************************************************/
397 static void RunThread ( playlist_t *p_playlist )
400 playlist_item_t *p_item;
402 mtime_t i_vout_destroyed_date = 0;
403 mtime_t i_sout_destroyed_date = 0;
405 playlist_item_t *p_autodelete_item = NULL;
407 /* Tell above that we're ready */
408 vlc_thread_ready( p_playlist );
410 while( !p_playlist->b_die )
412 vlc_mutex_lock( &p_playlist->object_lock );
414 /* First, check if we have something to do */
415 /* FIXME : this can be called several times */
416 if( p_playlist->request.b_request )
418 #ifdef PLAYLIST_PROFILE
419 msg_Dbg(p_playlist, "beginning processing of request, "
420 I64Fi" us ", mdate() - p_playlist->request_date );
422 /* Stop the existing input */
423 if( p_playlist->p_input )
425 input_StopThread( p_playlist->p_input );
427 /* The code below will start the next input for us */
428 if( p_playlist->status.i_status == PLAYLIST_STOPPED )
430 p_playlist->request.b_request = VLC_FALSE;
434 /* If there is an input, check that it doesn't need to die. */
435 if( p_playlist->p_input )
437 /* This input is dead. Remove it ! */
438 if( p_playlist->p_input->b_dead )
440 input_thread_t *p_input;
442 p_input = p_playlist->p_input;
443 p_playlist->p_input = NULL;
445 /* Release the playlist lock, because we may get stuck
446 * in input_DestroyThread() for some time. */
447 vlc_mutex_unlock( &p_playlist->object_lock );
450 input_DestroyThread( p_input );
452 /* Unlink current input
453 * (_after_ input_DestroyThread for vout garbage collector) */
454 vlc_object_detach( p_input );
457 vlc_object_destroy( p_input );
459 i_vout_destroyed_date = 0;
460 i_sout_destroyed_date = 0;
464 /* This input is dying, let him do */
465 else if( p_playlist->p_input->b_die )
469 /* This input has finished, ask him to die ! */
470 else if( p_playlist->p_input->b_error
471 || p_playlist->p_input->b_eof )
473 /* TODO FIXME XXX TODO FIXME XXX */
474 /* Check for autodeletion */
476 if( p_playlist->status.p_item->i_flags & PLAYLIST_DEL_FLAG )
478 p_autodelete_item = p_playlist->status.p_item;
480 input_StopThread( p_playlist->p_input );
481 /* Select the next playlist item */
482 vlc_mutex_unlock( &p_playlist->object_lock );
485 else if( p_playlist->p_input->i_state != INIT_S )
487 vlc_mutex_unlock( &p_playlist->object_lock );
488 i_vout_destroyed_date =
489 ObjectGarbageCollector( p_playlist, VLC_OBJECT_VOUT,
490 i_vout_destroyed_date );
491 i_sout_destroyed_date =
492 ObjectGarbageCollector( p_playlist, VLC_OBJECT_SOUT,
493 i_sout_destroyed_date );
494 vlc_mutex_lock( &p_playlist->object_lock );
497 else if( p_playlist->status.i_status != PLAYLIST_STOPPED )
499 /* Start another input.
500 * Get the next item to play */
501 p_item = NextItem( p_playlist );
507 if( p_autodelete_item )
509 vlc_mutex_unlock( &p_playlist->object_lock );
510 playlist_Delete( p_playlist,
511 p_autodelete_item->input.i_id );
512 vlc_mutex_lock( &p_playlist->object_lock );
513 p_autodelete_item = NULL;
515 p_playlist->status.i_status = PLAYLIST_STOPPED;
516 vlc_mutex_unlock( &p_playlist->object_lock );
520 PlayItem( p_playlist, p_item );
522 if( p_autodelete_item )
524 vlc_mutex_unlock( &p_playlist->object_lock );
525 playlist_Delete( p_playlist, p_autodelete_item->input.i_id );
526 vlc_mutex_lock( &p_playlist->object_lock );
527 p_autodelete_item = NULL;
530 else if( p_playlist->status.i_status == PLAYLIST_STOPPED )
532 /* Collect garbage */
533 vlc_mutex_unlock( &p_playlist->object_lock );
534 i_sout_destroyed_date =
535 ObjectGarbageCollector( p_playlist, VLC_OBJECT_SOUT, mdate() );
536 i_vout_destroyed_date =
537 ObjectGarbageCollector( p_playlist, VLC_OBJECT_VOUT, mdate() );
538 vlc_mutex_lock( &p_playlist->object_lock );
540 vlc_mutex_unlock( &p_playlist->object_lock );
542 msleep( INTF_IDLE_SLEEP / 2 );
544 /* Stop sleeping earlier if we have work */
545 /* TODO : statistics about this */
546 if ( p_playlist->request.b_request &&
547 p_playlist->status.i_status == PLAYLIST_RUNNING )
552 msleep( INTF_IDLE_SLEEP / 2 );
557 /* If there is an input, kill it */
560 vlc_mutex_lock( &p_playlist->object_lock );
562 if( p_playlist->p_input == NULL )
564 vlc_mutex_unlock( &p_playlist->object_lock );
568 if( p_playlist->p_input->b_dead )
570 input_thread_t *p_input;
572 /* Unlink current input */
573 p_input = p_playlist->p_input;
574 p_playlist->p_input = NULL;
575 vlc_mutex_unlock( &p_playlist->object_lock );
578 input_DestroyThread( p_input );
579 /* Unlink current input (_after_ input_DestroyThread for vout
580 * garbage collector)*/
581 vlc_object_detach( p_input );
584 vlc_object_destroy( p_input );
587 else if( p_playlist->p_input->b_die )
589 /* This input is dying, leave him alone */
592 else if( p_playlist->p_input->b_error || p_playlist->p_input->b_eof )
594 input_StopThread( p_playlist->p_input );
595 vlc_mutex_unlock( &p_playlist->object_lock );
600 p_playlist->p_input->b_eof = 1;
603 vlc_mutex_unlock( &p_playlist->object_lock );
605 msleep( INTF_IDLE_SLEEP );
608 /* close all remaining sout */
609 while( ( p_obj = vlc_object_find( p_playlist,
610 VLC_OBJECT_SOUT, FIND_CHILD ) ) )
612 vlc_object_release( p_obj );
613 sout_DeleteInstance( (sout_instance_t*)p_obj );
616 /* close all remaining vout */
617 while( ( p_obj = vlc_object_find( p_playlist,
618 VLC_OBJECT_VOUT, FIND_CHILD ) ) )
620 vlc_object_detach( p_obj );
621 vlc_object_release( p_obj );
622 vout_Destroy( (vout_thread_t *)p_obj );
626 /*****************************************************************************
628 *****************************************************************************
629 * This function calculates the next playlist item, depending
630 * on the playlist course mode (forward, backward, random, view,...).
631 *****************************************************************************/
632 static playlist_item_t * NextItem( playlist_t *p_playlist )
634 playlist_item_t *p_new = NULL;
635 int i_skip,i_goto,i, i_new, i_count ;
636 playlist_view_t *p_view;
638 vlc_bool_t b_loop = var_GetBool( p_playlist, "loop");
639 vlc_bool_t b_random = var_GetBool( p_playlist, "random" );
640 vlc_bool_t b_repeat = var_GetBool( p_playlist, "repeat" );
641 vlc_bool_t b_playstop = var_GetBool( p_playlist, "play-and-stop" );
643 #ifdef PLAYLIST_PROFILE
644 /* Calculate time needed */
645 int64_t start = mdate();
649 /* Handle quickly a few special cases */
651 /* No items to play */
652 if( p_playlist->i_size == 0 )
654 msg_Info( p_playlist, "playlist is empty" );
657 /* Nothing requested */
658 if( !p_playlist->request.b_request && p_playlist->status.p_item == NULL )
660 msg_Warn( p_playlist,"nothing requested" );
664 /* Repeat and play/stop */
665 if( !p_playlist->request.b_request && b_repeat == VLC_TRUE )
667 msg_Dbg( p_playlist,"repeating item" );
668 return p_playlist->status.p_item;
671 if( !p_playlist->request.b_request && b_playstop == VLC_TRUE )
673 msg_Dbg( p_playlist,"stopping (play and stop)");
677 if( !p_playlist->request.b_request &&
678 !(p_playlist->status.p_item->i_flags & PLAYLIST_SKIP_FLAG) )
680 msg_Dbg( p_playlist, "no-skip mode, stopping") ;
684 /* TODO: improve this (only use current node) */
685 /* TODO: use the "shuffled view" internally ? */
686 /* Random case. This is an exception: if request, but request is skip +- 1
687 * we don't go to next item but select a new random one. */
688 if( b_random && (!p_playlist->request.b_request ||
689 p_playlist->request.i_skip == 1 || p_playlist->request.i_skip == -1 ) )
691 srand( (unsigned int)mdate() );
693 for( i_count = 0; i_count < p_playlist->i_size - 1 ; i_count ++ )
696 (int)((float)p_playlist->i_size * rand() / (RAND_MAX+1.0));
697 /* Check if the item has not already been played */
698 if( p_playlist->pp_items[i_new]->i_nb_played == 0 )
701 if( i_count == p_playlist->i_size )
703 /* The whole playlist has been played: reset the counters */
706 p_playlist->pp_items[--i_count]->i_nb_played = 0;
713 p_playlist->request.i_skip = 0;
714 p_playlist->request.b_request = VLC_FALSE;
715 return p_playlist->pp_items[i_new];
718 /* Start the real work */
719 if( p_playlist->request.b_request )
721 msg_Dbg( p_playlist,"processing request" );
722 /* We are not playing from a view */
723 if( p_playlist->request.i_view == -1 )
725 /* Directly select the item, just like now */
726 i_skip = p_playlist->request.i_skip;
727 i_goto = p_playlist->request.i_goto;
729 if( p_playlist->i_index == -1 ) p_playlist->i_index = 0;
730 p_new = p_playlist->pp_items[p_playlist->i_index];
732 if( i_goto >= 0 && i_goto < p_playlist->i_size )
734 p_playlist->i_index = i_goto;
735 p_new = p_playlist->pp_items[p_playlist->i_index];
736 p_playlist->request.i_goto = -1;
741 if( p_playlist->i_index + i_skip < p_playlist->i_size &&
742 p_playlist->i_index + i_skip >= 0 )
744 p_playlist->i_index += i_skip;
745 p_new = p_playlist->pp_items[p_playlist->i_index];
751 p_new = p_playlist->request.p_item;
752 i_skip = p_playlist->request.i_skip;
754 /* If we are asked for a node, take its first item */
755 if( p_playlist->request.p_item == NULL && i_skip == 0 )
760 p_view = playlist_ViewFind( p_playlist,p_playlist->request.i_view );
761 p_playlist->status.p_node = p_playlist->request.p_node;
762 p_playlist->status.i_view = p_playlist->request.i_view;
765 for( i = i_skip; i > 0 ; i-- )
767 p_new = playlist_FindNextFromParent( p_playlist,
768 p_playlist->request.i_view,
770 p_playlist->request.p_node,
772 if( p_new == NULL ) break;
775 else if( i_skip < 0 )
777 for( i = i_skip; i < 0 ; i++ )
779 p_new = playlist_FindPrevFromParent( p_playlist,
780 p_playlist->request.i_view,
782 p_playlist->request.p_node,
784 if( p_new == NULL ) break;
789 /* Clear the request */
790 p_playlist->request.b_request = VLC_FALSE;
792 /* "Automatic" item change ( next ) */
795 p_playlist->request_date = 0;
798 if( p_playlist->status.i_view == -1 )
800 if( p_playlist->i_index + 1 < p_playlist->i_size )
802 p_playlist->i_index++;
803 p_new = p_playlist->pp_items[p_playlist->i_index];
807 msg_Dbg( p_playlist,"finished" );
811 /* We are playing with a view */
814 playlist_view_t *p_view =
815 playlist_ViewFind( p_playlist,
816 p_playlist->status.i_view );
817 p_new = playlist_FindNextFromParent( p_playlist,
818 p_playlist->status.i_view,
820 p_playlist->status.p_node,
821 p_playlist->status.p_item );
826 if( p_playlist->i_index >= 0 && p_new != NULL &&
827 p_playlist->pp_items[p_playlist->i_index] != p_new )
829 p_playlist->i_index = playlist_GetPositionById( p_playlist,
833 #ifdef PLAYLIST_PROFILE
834 msg_Dbg(p_playlist,"next item found in "I64Fi " us\n",mdate()-start );
837 if( p_new == NULL ) { msg_Info( p_playlist, "Nothing to play" ); }
846 static void SkipItem( playlist_t *p_playlist, int i_arg )
848 int i_oldindex = p_playlist->i_index;
849 vlc_bool_t b_random, b_repeat, b_loop;
856 p_playlist->i_index += i_arg;
860 if( p_playlist->i_index >= p_playlist->i_size )
862 if( p_playlist->i_status == PLAYLIST_STOPPED || b_random || b_loop )
864 p_playlist->i_index -= p_playlist->i_size
865 * ( p_playlist->i_index / p_playlist->i_size );
869 /* Don't loop by default: stop at playlist end */
870 p_playlist->i_index = i_oldindex;
871 p_playlist->i_status = PLAYLIST_STOPPED;
874 else if( p_playlist->i_index < 0 )
876 p_playlist->i_index = p_playlist->i_size - 1;
879 /* Check that the item is enabled */
880 if( p_playlist->pp_items[p_playlist->i_index]->b_enabled == VLC_FALSE &&
881 p_playlist->i_enabled != 0)
883 SkipItem( p_playlist , 1 );
889 /*****************************************************************************
890 * PlayItem: start the input thread for an item
891 ****************************************************************************/
892 static void PlayItem( playlist_t *p_playlist, playlist_item_t *p_item )
897 msg_Dbg( p_playlist, "creating new input thread" );
899 p_item->i_nb_played++;
900 p_playlist->status.p_item = p_item;
902 #ifdef PLAYLIST_PROFILE
903 if( p_playlist->request_date != 0 )
905 msg_Dbg( p_playlist, "request processed after "I64Fi " us",
906 mdate() - p_playlist->request_date );
910 p_playlist->p_input = input_CreateThread( p_playlist, &p_item->input );
912 var_AddCallback( p_playlist->p_input, "item-change",
913 ItemChange, p_playlist );
915 val.i_int = p_item->input.i_id;
916 /* unlock the playlist to set the var...mmm */
917 vlc_mutex_unlock( &p_playlist->object_lock);
918 var_Set( p_playlist, "playlist-current", val);
919 vlc_mutex_lock( &p_playlist->object_lock);
923 /* Forward item change from input */
924 static int ItemChange( vlc_object_t *p_obj, const char *psz_var,
925 vlc_value_t oldval, vlc_value_t newval, void *param )
927 playlist_t *p_playlist = (playlist_t *)param;
930 //p_playlist->b_need_update = VLC_TRUE;
931 var_SetInteger( p_playlist, "item-change", newval.i_int );
934 /* FIXME: Make that automatic */
935 playlist_ViewUpdate( p_playlist, VIEW_S_AUTHOR );