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
42 /*****************************************************************************
44 *****************************************************************************/
45 static void RunThread ( playlist_t * );
46 static playlist_item_t * NextItem ( playlist_t * );
47 static int PlayItem ( playlist_t *, playlist_item_t * );
49 static int ItemChange( vlc_object_t *, const char *,
50 vlc_value_t, vlc_value_t, void * );
52 int playlist_vaControl( playlist_t * p_playlist, int i_query, va_list args );
58 * Create a playlist structure.
59 * \param p_parent the vlc object that is to be the parent of this playlist
60 * \return a pointer to the created playlist, or NULL on error
62 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, "item-append", VLC_VAR_ADDRESS );
87 var_Create( p_playlist, "playlist-current", VLC_VAR_INTEGER );
89 var_Set( p_playlist, "playlist-current", val );
91 var_Create( p_playlist, "intf-popupmenu", VLC_VAR_BOOL );
93 var_Create( p_playlist, "intf-show", VLC_VAR_BOOL );
94 val.b_bool = VLC_TRUE;
95 var_Set( p_playlist, "intf-show", val );
98 /* Variables to control playback */
99 var_CreateGetBool( p_playlist, "play-and-stop" );
100 var_CreateGetBool( p_playlist, "random" );
101 var_CreateGetBool( p_playlist, "repeat" );
102 var_CreateGetBool( p_playlist, "loop" );
104 /* Initialise data structures */
105 p_playlist->b_go_next = VLC_TRUE;
106 p_playlist->p_input = NULL;
108 p_playlist->request_date = 0;
110 p_playlist->i_views = 0;
111 p_playlist->pp_views = NULL;
113 p_playlist->i_index = -1;
114 p_playlist->i_size = 0;
115 p_playlist->pp_items = NULL;
117 playlist_ViewInsert( p_playlist, VIEW_CATEGORY, TITLE_CATEGORY );
118 playlist_ViewInsert( p_playlist, VIEW_SIMPLE, TITLE_SIMPLE );
119 playlist_ViewInsert( p_playlist, VIEW_ALL, TITLE_ALL );
121 p_view = playlist_ViewFind( p_playlist, VIEW_CATEGORY );
123 p_playlist->p_general = playlist_NodeCreate( p_playlist, VIEW_CATEGORY,
124 _( "General" ), p_view->p_root );
126 /* Set startup status
127 * We set to simple view on startup for interfaces that don't do
129 p_view = playlist_ViewFind( p_playlist, VIEW_SIMPLE );
130 p_playlist->status.i_view = VIEW_SIMPLE;
131 p_playlist->status.p_item = NULL;
132 p_playlist->status.p_node = p_view->p_root;
133 p_playlist->request.b_request = VLC_FALSE;
134 p_playlist->status.i_status = PLAYLIST_STOPPED;
137 p_playlist->i_last_id = 0;
138 p_playlist->i_sort = SORT_ID;
139 p_playlist->i_order = ORDER_NORMAL;
141 /* Finally, launch the thread ! */
142 if( vlc_thread_create( p_playlist, "playlist", RunThread,
143 VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
145 msg_Err( p_playlist, "cannot spawn playlist thread" );
146 vlc_object_destroy( p_playlist );
150 /* The object has been initialized, now attach it */
151 vlc_object_attach( p_playlist, p_parent );
157 * Destroy the playlist.
159 * Delete all items in the playlist and free the playlist structure.
160 * \param p_playlist the playlist structure to destroy
161 * \return VLC_SUCCESS or an error
163 int playlist_Destroy( playlist_t * p_playlist )
166 p_playlist->b_die = 1;
168 for( i = 0 ; i< p_playlist->i_sds ; i++ )
170 playlist_ServicesDiscoveryRemove( p_playlist,
171 p_playlist->pp_sds[i]->psz_module );
174 vlc_thread_join( p_playlist );
176 var_Destroy( p_playlist, "intf-change" );
177 var_Destroy( p_playlist, "item-change" );
178 var_Destroy( p_playlist, "playlist-current" );
179 var_Destroy( p_playlist, "intf-popmenu" );
180 var_Destroy( p_playlist, "intf-show" );
181 var_Destroy( p_playlist, "play-and-stop" );
182 var_Destroy( p_playlist, "random" );
183 var_Destroy( p_playlist, "repeat" );
184 var_Destroy( p_playlist, "loop" );
186 playlist_Clear( p_playlist );
188 for( i = p_playlist->i_views - 1; i >= 0 ; i-- )
190 playlist_view_t *p_view = p_playlist->pp_views[i];
191 if( p_view->psz_name )
192 free( p_view->psz_name );
193 playlist_ItemDelete( p_view->p_root );
194 REMOVE_ELEM( p_playlist->pp_views, p_playlist->i_views, i );
198 vlc_object_destroy( p_playlist );
205 * Do a playlist action.
207 * If there is something in the playlist then you can do playlist actions.
209 * Playlist lock must not be taken when calling this function
211 * \param p_playlist the playlist to do the command on
212 * \param i_query the command to do
213 * \param variable number of arguments
214 * \return VLC_SUCCESS or an error
216 int playlist_Control( playlist_t * p_playlist, int i_query, ... )
220 va_start( args, i_query );
221 i_result = playlist_vaControl( p_playlist, i_query, args );
227 int playlist_vaControl( playlist_t * p_playlist, int i_query, va_list args )
229 playlist_view_t *p_view;
232 vlc_mutex_lock( &p_playlist->object_lock );
234 #ifdef PLAYLIST_PROFILE
235 p_playlist->request_date = mdate();
238 if( p_playlist->i_size <= 0 )
240 vlc_mutex_unlock( &p_playlist->object_lock );
247 p_playlist->status.i_status = PLAYLIST_STOPPED;
248 p_playlist->request.b_request = VLC_TRUE;
251 case PLAYLIST_ITEMPLAY:
252 p_playlist->status.i_status = PLAYLIST_RUNNING;
253 p_playlist->request.i_skip = 0;
254 p_playlist->request.b_request = VLC_TRUE;
255 p_playlist->request.p_item = (playlist_item_t *)va_arg( args,
257 p_playlist->request.i_view = p_playlist->status.i_view;
258 p_view = playlist_ViewFind( p_playlist, p_playlist->status.i_view );
261 p_playlist->request.p_node = p_view->p_root;
265 p_playlist->request.p_node = NULL;
269 case PLAYLIST_VIEWPLAY:
270 p_playlist->status.i_status = PLAYLIST_RUNNING;
271 p_playlist->request.i_skip = 0;
272 p_playlist->request.b_request = VLC_TRUE;
273 p_playlist->request.i_view = (int)va_arg( args,int );
274 p_playlist->request.p_node = (playlist_item_t *)va_arg( args,
276 p_playlist->request.p_item = (playlist_item_t *)va_arg( args,
279 /* If we select a node, play only it.
280 * If we select an item, continue */
281 if( p_playlist->request.p_item == NULL ||
282 ! p_playlist->request.p_node->i_flags & PLAYLIST_SKIP_FLAG )
284 p_playlist->b_go_next = VLC_FALSE;
288 p_playlist->b_go_next = VLC_TRUE;
293 p_playlist->status.i_status = PLAYLIST_RUNNING;
295 if( p_playlist->p_input )
297 val.i_int = PLAYING_S;
298 var_Set( p_playlist->p_input, "state", val );
302 /* FIXME : needed ? */
303 p_playlist->request.b_request = VLC_TRUE;
304 p_playlist->request.i_view = p_playlist->status.i_view;
305 p_playlist->request.p_node = p_playlist->status.p_node;
306 p_playlist->request.p_item = p_playlist->status.p_item;
307 p_playlist->request.i_skip = 0;
308 p_playlist->request.i_goto = -1;
311 case PLAYLIST_AUTOPLAY:
312 p_playlist->status.i_status = PLAYLIST_RUNNING;
314 p_playlist->request.b_request = VLC_FALSE;
319 if( p_playlist->p_input )
320 var_Get( p_playlist->p_input, "state", &val );
322 if( val.i_int == PAUSE_S )
324 p_playlist->status.i_status = PLAYLIST_RUNNING;
325 if( p_playlist->p_input )
327 val.i_int = PLAYING_S;
328 var_Set( p_playlist->p_input, "state", val );
333 p_playlist->status.i_status = PLAYLIST_PAUSED;
334 if( p_playlist->p_input )
337 var_Set( p_playlist->p_input, "state", val );
343 if( p_playlist->status.i_view > -1 )
345 p_playlist->request.p_node = p_playlist->status.p_node;
346 p_playlist->request.p_item = p_playlist->status.p_item;
348 p_playlist->request.i_skip = (int) va_arg( args, int );
349 p_playlist->request.b_request = VLC_TRUE;
353 p_playlist->status.i_status = PLAYLIST_RUNNING;
354 p_playlist->request.p_node = NULL;
355 p_playlist->request.p_item = NULL;
356 p_playlist->request.i_view = -1;
357 p_playlist->request.i_goto = (int) va_arg( args, int );
358 p_playlist->request.b_request = VLC_TRUE;
362 msg_Err( p_playlist, "unimplemented playlist query" );
367 vlc_mutex_unlock( &p_playlist->object_lock );
372 /* Destroy remaining objects */
373 static mtime_t ObjectGarbageCollector( playlist_t *p_playlist, int i_type,
374 mtime_t destroy_date )
378 if( destroy_date > mdate() ) return destroy_date;
380 if( destroy_date == 0 )
382 /* give a little time */
383 return mdate() + I64C(1000000);
387 while( ( p_obj = vlc_object_find( p_playlist, i_type, FIND_CHILD ) ) )
389 if( p_obj->p_parent != (vlc_object_t*)p_playlist )
391 /* only first child (ie unused) */
392 vlc_object_release( p_obj );
395 if( i_type == VLC_OBJECT_VOUT )
397 msg_Dbg( p_playlist, "garbage collector destroying 1 vout" );
398 vlc_object_detach( p_obj );
399 vlc_object_release( p_obj );
400 vout_Destroy( (vout_thread_t *)p_obj );
402 else if( i_type == VLC_OBJECT_SOUT )
404 vlc_object_release( p_obj );
405 sout_DeleteInstance( (sout_instance_t*)p_obj );
412 /*****************************************************************************
413 * RunThread: main playlist thread
414 *****************************************************************************/
415 static void RunThread ( playlist_t *p_playlist )
418 playlist_item_t *p_item;
420 mtime_t i_vout_destroyed_date = 0;
421 mtime_t i_sout_destroyed_date = 0;
423 playlist_item_t *p_autodelete_item = NULL;
425 /* Tell above that we're ready */
426 vlc_thread_ready( p_playlist );
428 while( !p_playlist->b_die )
430 vlc_mutex_lock( &p_playlist->object_lock );
432 /* First, check if we have something to do */
433 /* FIXME : this can be called several times */
434 if( p_playlist->request.b_request )
436 #ifdef PLAYLIST_PROFILE
437 msg_Dbg(p_playlist, "beginning processing of request, "
438 I64Fi" us ", mdate() - p_playlist->request_date );
440 /* Stop the existing input */
441 if( p_playlist->p_input )
443 input_StopThread( p_playlist->p_input );
445 /* The code below will start the next input for us */
446 if( p_playlist->status.i_status == PLAYLIST_STOPPED )
448 p_playlist->request.b_request = VLC_FALSE;
452 /* If there is an input, check that it doesn't need to die. */
453 if( p_playlist->p_input )
455 /* This input is dead. Remove it ! */
456 if( p_playlist->p_input->b_dead )
458 input_thread_t *p_input;
460 p_input = p_playlist->p_input;
461 p_playlist->p_input = NULL;
463 /* Release the playlist lock, because we may get stuck
464 * in input_DestroyThread() for some time. */
465 vlc_mutex_unlock( &p_playlist->object_lock );
468 input_DestroyThread( p_input );
470 /* Unlink current input
471 * (_after_ input_DestroyThread for vout garbage collector) */
472 vlc_object_detach( p_input );
475 vlc_object_destroy( p_input );
477 i_vout_destroyed_date = 0;
478 i_sout_destroyed_date = 0;
482 /* This input is dying, let him do */
483 else if( p_playlist->p_input->b_die )
487 /* This input has finished, ask him to die ! */
488 else if( p_playlist->p_input->b_error
489 || p_playlist->p_input->b_eof )
491 /* TODO FIXME XXX TODO FIXME XXX */
492 /* Check for autodeletion */
494 if( p_playlist->status.p_item->i_flags & PLAYLIST_DEL_FLAG )
496 p_autodelete_item = p_playlist->status.p_item;
498 input_StopThread( p_playlist->p_input );
499 /* Select the next playlist item */
500 vlc_mutex_unlock( &p_playlist->object_lock );
503 else if( p_playlist->p_input->i_state != INIT_S )
505 vlc_mutex_unlock( &p_playlist->object_lock );
506 i_vout_destroyed_date =
507 ObjectGarbageCollector( p_playlist, VLC_OBJECT_VOUT,
508 i_vout_destroyed_date );
509 i_sout_destroyed_date =
510 ObjectGarbageCollector( p_playlist, VLC_OBJECT_SOUT,
511 i_sout_destroyed_date );
512 vlc_mutex_lock( &p_playlist->object_lock );
515 else if( p_playlist->status.i_status != PLAYLIST_STOPPED )
517 /* Start another input.
518 * Get the next item to play */
519 p_item = NextItem( p_playlist );
525 if( p_autodelete_item )
527 vlc_mutex_unlock( &p_playlist->object_lock );
528 playlist_Delete( p_playlist,
529 p_autodelete_item->input.i_id );
530 vlc_mutex_lock( &p_playlist->object_lock );
531 p_autodelete_item = NULL;
533 p_playlist->status.i_status = PLAYLIST_STOPPED;
534 vlc_mutex_unlock( &p_playlist->object_lock );
538 PlayItem( p_playlist, p_item );
540 if( p_autodelete_item )
542 vlc_mutex_unlock( &p_playlist->object_lock );
543 playlist_Delete( p_playlist, p_autodelete_item->input.i_id );
544 vlc_mutex_lock( &p_playlist->object_lock );
545 p_autodelete_item = NULL;
548 else if( p_playlist->status.i_status == PLAYLIST_STOPPED )
550 /* Collect garbage */
551 vlc_mutex_unlock( &p_playlist->object_lock );
552 i_sout_destroyed_date =
553 ObjectGarbageCollector( p_playlist, VLC_OBJECT_SOUT, mdate() );
554 i_vout_destroyed_date =
555 ObjectGarbageCollector( p_playlist, VLC_OBJECT_VOUT, mdate() );
556 vlc_mutex_lock( &p_playlist->object_lock );
558 vlc_mutex_unlock( &p_playlist->object_lock );
560 msleep( INTF_IDLE_SLEEP / 2 );
562 /* Stop sleeping earlier if we have work */
563 /* TODO : statistics about this */
564 if ( p_playlist->request.b_request &&
565 p_playlist->status.i_status == PLAYLIST_RUNNING )
570 msleep( INTF_IDLE_SLEEP / 2 );
575 /* If there is an input, kill it */
578 vlc_mutex_lock( &p_playlist->object_lock );
580 if( p_playlist->p_input == NULL )
582 vlc_mutex_unlock( &p_playlist->object_lock );
586 if( p_playlist->p_input->b_dead )
588 input_thread_t *p_input;
590 /* Unlink current input */
591 p_input = p_playlist->p_input;
592 p_playlist->p_input = NULL;
593 vlc_mutex_unlock( &p_playlist->object_lock );
596 input_DestroyThread( p_input );
597 /* Unlink current input (_after_ input_DestroyThread for vout
598 * garbage collector)*/
599 vlc_object_detach( p_input );
602 vlc_object_destroy( p_input );
605 else if( p_playlist->p_input->b_die )
607 /* This input is dying, leave him alone */
610 else if( p_playlist->p_input->b_error || p_playlist->p_input->b_eof )
612 input_StopThread( p_playlist->p_input );
613 vlc_mutex_unlock( &p_playlist->object_lock );
618 p_playlist->p_input->b_eof = 1;
621 vlc_mutex_unlock( &p_playlist->object_lock );
623 msleep( INTF_IDLE_SLEEP );
626 /* close all remaining sout */
627 while( ( p_obj = vlc_object_find( p_playlist,
628 VLC_OBJECT_SOUT, FIND_CHILD ) ) )
630 vlc_object_release( p_obj );
631 sout_DeleteInstance( (sout_instance_t*)p_obj );
634 /* close all remaining vout */
635 while( ( p_obj = vlc_object_find( p_playlist,
636 VLC_OBJECT_VOUT, FIND_CHILD ) ) )
638 vlc_object_detach( p_obj );
639 vlc_object_release( p_obj );
640 vout_Destroy( (vout_thread_t *)p_obj );
644 /*****************************************************************************
646 *****************************************************************************
647 * This function calculates the next playlist item, depending
648 * on the playlist course mode (forward, backward, random, view,...).
649 *****************************************************************************/
650 static playlist_item_t * NextItem( playlist_t *p_playlist )
652 playlist_item_t *p_new = NULL;
653 int i_skip,i_goto,i, i_new, i_count ;
654 playlist_view_t *p_view;
656 vlc_bool_t b_loop = var_GetBool( p_playlist, "loop");
657 vlc_bool_t b_random = var_GetBool( p_playlist, "random" );
658 vlc_bool_t b_repeat = var_GetBool( p_playlist, "repeat" );
659 vlc_bool_t b_playstop = var_GetBool( p_playlist, "play-and-stop" );
661 #ifdef PLAYLIST_PROFILE
662 /* Calculate time needed */
663 int64_t start = mdate();
667 /* Handle quickly a few special cases */
669 /* No items to play */
670 if( p_playlist->i_size == 0 )
672 msg_Info( p_playlist, "playlist is empty" );
675 /* Nothing requested */
676 if( !p_playlist->request.b_request && p_playlist->status.p_item == NULL )
678 msg_Warn( p_playlist,"nothing requested" );
682 /* Repeat and play/stop */
683 if( !p_playlist->request.b_request && b_repeat == VLC_TRUE )
685 msg_Dbg( p_playlist,"repeating item" );
686 return p_playlist->status.p_item;
689 if( !p_playlist->request.b_request && b_playstop == VLC_TRUE )
691 msg_Dbg( p_playlist,"stopping (play and stop)");
695 if( !p_playlist->request.b_request &&
696 !(p_playlist->status.p_item->i_flags & PLAYLIST_SKIP_FLAG) )
698 msg_Dbg( p_playlist, "no-skip mode, stopping") ;
702 /* TODO: improve this (only use current node) */
703 /* TODO: use the "shuffled view" internally ? */
704 /* Random case. This is an exception: if request, but request is skip +- 1
705 * we don't go to next item but select a new random one. */
706 if( b_random && (!p_playlist->request.b_request ||
707 p_playlist->request.i_skip == 1 || p_playlist->request.i_skip == -1 ) )
709 srand( (unsigned int)mdate() );
711 for( i_count = 0; i_count < p_playlist->i_size - 1 ; i_count ++ )
714 (int)((float)p_playlist->i_size * rand() / (RAND_MAX+1.0));
715 /* Check if the item has not already been played */
716 if( p_playlist->pp_items[i_new]->i_nb_played == 0 )
719 if( i_count == p_playlist->i_size )
721 /* The whole playlist has been played: reset the counters */
724 p_playlist->pp_items[--i_count]->i_nb_played = 0;
731 p_playlist->request.i_skip = 0;
732 p_playlist->request.b_request = VLC_FALSE;
733 return p_playlist->pp_items[i_new];
736 /* Start the real work */
737 if( p_playlist->request.b_request )
739 msg_Dbg( p_playlist,"processing request" );
740 /* We are not playing from a view */
741 if( p_playlist->request.i_view == -1 )
743 #ifdef PLAYLIST_DEBUG
744 msg_Dbg( p_playlist, "non-view mode request");
746 /* Directly select the item, just like now */
747 i_skip = p_playlist->request.i_skip;
748 i_goto = p_playlist->request.i_goto;
750 if( p_playlist->i_index == -1 ) p_playlist->i_index = 0;
751 p_new = p_playlist->pp_items[p_playlist->i_index];
753 if( i_goto >= 0 && i_goto < p_playlist->i_size )
755 p_playlist->i_index = i_goto;
756 p_new = p_playlist->pp_items[p_playlist->i_index];
757 p_playlist->request.i_goto = -1;
762 if( p_playlist->i_index + i_skip < p_playlist->i_size &&
763 p_playlist->i_index + i_skip >= 0 )
765 p_playlist->i_index += i_skip;
766 p_new = p_playlist->pp_items[p_playlist->i_index];
772 #ifdef PLAYLIST_DEBUG
773 msg_Dbg( p_playlist, "view mode request" );
775 p_new = p_playlist->request.p_item;
776 i_skip = p_playlist->request.i_skip;
778 /* If we are asked for a node, take its first item */
779 if( p_playlist->request.p_item == NULL && i_skip == 0 )
784 p_view = playlist_ViewFind( p_playlist,p_playlist->request.i_view );
785 p_playlist->status.p_node = p_playlist->request.p_node;
786 p_playlist->status.i_view = p_playlist->request.i_view;
789 for( i = i_skip; i > 0 ; i-- )
791 p_new = playlist_FindNextFromParent( p_playlist,
792 p_playlist->request.i_view,
794 p_playlist->request.p_node,
796 if( p_new == NULL ) break;
799 else if( i_skip < 0 )
801 for( i = i_skip; i < 0 ; i++ )
803 p_new = playlist_FindPrevFromParent( p_playlist,
804 p_playlist->request.i_view,
806 p_playlist->request.p_node,
808 if( p_new == NULL ) break;
813 /* Clear the request */
814 p_playlist->request.b_request = VLC_FALSE;
816 /* "Automatic" item change ( next ) */
819 p_playlist->request_date = 0;
822 if( p_playlist->status.i_view == -1 )
824 if( p_playlist->i_index + 1 < p_playlist->i_size )
826 p_playlist->i_index++;
827 p_new = p_playlist->pp_items[p_playlist->i_index];
828 if( !(p_new->i_flags & PLAYLIST_SKIP_FLAG) )
835 msg_Dbg( p_playlist,"finished" );
839 /* We are playing with a view */
842 playlist_view_t *p_view =
843 playlist_ViewFind( p_playlist,
844 p_playlist->status.i_view );
845 p_new = playlist_FindNextFromParent( p_playlist,
846 p_playlist->status.i_view,
848 p_playlist->status.p_node,
849 p_playlist->status.p_item );
854 if( p_playlist->i_index >= 0 && p_new != NULL &&
855 p_playlist->pp_items[p_playlist->i_index] != p_new )
857 p_playlist->i_index = playlist_GetPositionById( p_playlist,
861 #ifdef PLAYLIST_PROFILE
862 msg_Dbg(p_playlist,"next item found in "I64Fi " us", mdate()-start );
865 if( p_new == NULL ) { msg_Info( p_playlist, "Nothing to play" ); }
874 static void SkipItem( playlist_t *p_playlist, int i_arg )
876 int i_oldindex = p_playlist->i_index;
877 vlc_bool_t b_random, b_repeat, b_loop;
884 p_playlist->i_index += i_arg;
888 if( p_playlist->i_index >= p_playlist->i_size )
890 if( p_playlist->i_status == PLAYLIST_STOPPED || b_random || b_loop )
892 p_playlist->i_index -= p_playlist->i_size
893 * ( p_playlist->i_index / p_playlist->i_size );
897 /* Don't loop by default: stop at playlist end */
898 p_playlist->i_index = i_oldindex;
899 p_playlist->i_status = PLAYLIST_STOPPED;
902 else if( p_playlist->i_index < 0 )
904 p_playlist->i_index = p_playlist->i_size - 1;
907 /* Check that the item is enabled */
908 if( p_playlist->pp_items[p_playlist->i_index]->b_enabled == VLC_FALSE &&
909 p_playlist->i_enabled != 0)
911 SkipItem( p_playlist , 1 );
917 /*****************************************************************************
918 * PlayItem: start the input thread for an item
919 ****************************************************************************/
920 static int PlayItem( playlist_t *p_playlist, playlist_item_t *p_item )
924 msg_Dbg( p_playlist, "creating new input thread" );
926 p_item->i_nb_played++;
927 p_playlist->status.p_item = p_item;
929 p_playlist->i_index = playlist_GetPositionById( p_playlist,
930 p_item->input.i_id );
932 #ifdef PLAYLIST_PROFILE
933 if( p_playlist->request_date != 0 )
935 msg_Dbg( p_playlist, "request processed after "I64Fi " us",
936 mdate() - p_playlist->request_date );
940 p_playlist->p_input = input_CreateThread( p_playlist, &p_item->input );
942 var_AddCallback( p_playlist->p_input, "item-change",
943 ItemChange, p_playlist );
945 val.i_int = p_item->input.i_id;
946 /* unlock the playlist to set the var...mmm */
947 vlc_mutex_unlock( &p_playlist->object_lock);
948 var_Set( p_playlist, "playlist-current", val);
949 vlc_mutex_lock( &p_playlist->object_lock);
955 /* Forward item change from input */
956 static int ItemChange( vlc_object_t *p_obj, const char *psz_var,
957 vlc_value_t oldval, vlc_value_t newval, void *param )
959 playlist_t *p_playlist = (playlist_t *)param;
962 //p_playlist->b_need_update = VLC_TRUE;
963 var_SetInteger( p_playlist, "item-change", newval.i_int );
966 /* FIXME: Make that automatic */
967 playlist_ViewUpdate( p_playlist, VIEW_S_AUTHOR );