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 void RunPreparse( playlist_preparse_t * );
47 static playlist_item_t * NextItem ( playlist_t * );
48 static int PlayItem ( playlist_t *, playlist_item_t * );
50 static int ItemChange( vlc_object_t *, const char *,
51 vlc_value_t, vlc_value_t, void * );
53 int playlist_vaControl( playlist_t * p_playlist, int i_query, va_list args );
59 * Create a playlist structure.
60 * \param p_parent the vlc object that is to be the parent of this playlist
61 * \return a pointer to the created playlist, or NULL on error
63 playlist_t * __playlist_Create ( vlc_object_t *p_parent )
65 playlist_t *p_playlist;
66 playlist_view_t *p_view;
69 /* Allocate structure */
70 p_playlist = vlc_object_create( p_parent, VLC_OBJECT_PLAYLIST );
73 msg_Err( p_parent, "out of memory" );
77 /* These variables control updates */
78 var_Create( p_playlist, "intf-change", VLC_VAR_BOOL );
79 val.b_bool = VLC_TRUE;
80 var_Set( p_playlist, "intf-change", val );
82 var_Create( p_playlist, "item-change", VLC_VAR_INTEGER );
84 var_Set( p_playlist, "item-change", val );
86 var_Create( p_playlist, "item-deleted", VLC_VAR_INTEGER );
88 var_Set( p_playlist, "item-deleted", val );
90 var_Create( p_playlist, "item-append", VLC_VAR_ADDRESS );
92 var_Create( p_playlist, "playlist-current", VLC_VAR_INTEGER );
94 var_Set( p_playlist, "playlist-current", val );
96 var_Create( p_playlist, "intf-popupmenu", VLC_VAR_BOOL );
98 var_Create( p_playlist, "intf-show", VLC_VAR_BOOL );
99 val.b_bool = VLC_TRUE;
100 var_Set( p_playlist, "intf-show", val );
103 /* Variables to control playback */
104 var_CreateGetBool( p_playlist, "play-and-stop" );
105 var_CreateGetBool( p_playlist, "random" );
106 var_CreateGetBool( p_playlist, "repeat" );
107 var_CreateGetBool( p_playlist, "loop" );
109 /* Initialise data structures */
110 p_playlist->b_go_next = VLC_TRUE;
111 p_playlist->p_input = NULL;
113 p_playlist->request_date = 0;
115 p_playlist->i_views = 0;
116 p_playlist->pp_views = NULL;
118 p_playlist->i_index = -1;
119 p_playlist->i_size = 0;
120 p_playlist->pp_items = NULL;
122 playlist_ViewInsert( p_playlist, VIEW_CATEGORY, TITLE_CATEGORY );
123 playlist_ViewInsert( p_playlist, VIEW_SIMPLE, TITLE_SIMPLE );
124 playlist_ViewInsert( p_playlist, VIEW_ALL, TITLE_ALL );
126 p_view = playlist_ViewFind( p_playlist, VIEW_CATEGORY );
128 p_playlist->p_general = playlist_NodeCreate( p_playlist, VIEW_CATEGORY,
129 _( "General" ), p_view->p_root );
130 p_playlist->p_general->i_flags |= PLAYLIST_RO_FLAG;
132 /* Set startup status
133 * We set to simple view on startup for interfaces that don't do
135 p_view = playlist_ViewFind( p_playlist, VIEW_SIMPLE );
136 p_playlist->status.i_view = VIEW_SIMPLE;
137 p_playlist->status.p_item = NULL;
138 p_playlist->status.p_node = p_view->p_root;
139 p_playlist->request.b_request = VLC_FALSE;
140 p_playlist->status.i_status = PLAYLIST_STOPPED;
143 p_playlist->i_last_id = 0;
144 p_playlist->i_sort = SORT_ID;
145 p_playlist->i_order = ORDER_NORMAL;
147 /* Finally, launch the thread ! */
148 if( vlc_thread_create( p_playlist, "playlist", RunThread,
149 VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
151 msg_Err( p_playlist, "cannot spawn playlist thread" );
152 vlc_object_destroy( p_playlist );
156 /* Preparsing stuff */
157 p_playlist->p_preparse = vlc_object_create( p_playlist,
158 sizeof( playlist_preparse_t ) );
159 if( !p_playlist->p_preparse )
161 msg_Err( p_playlist, "unable to create preparser" );
162 vlc_object_destroy( p_playlist );
166 p_playlist->p_preparse->i_waiting = 0;
167 p_playlist->p_preparse->pp_waiting = NULL;
169 vlc_object_attach( p_playlist->p_preparse, p_playlist );
170 if( vlc_thread_create( p_playlist->p_preparse, "preparser",
171 RunPreparse, VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
173 msg_Err( p_playlist, "cannot spawn preparse thread" );
174 vlc_object_detach( p_playlist->p_preparse );
175 vlc_object_destroy( p_playlist->p_preparse );
179 /* The object has been initialized, now attach it */
180 vlc_object_attach( p_playlist, p_parent );
186 * Destroy the playlist.
188 * Delete all items in the playlist and free the playlist structure.
189 * \param p_playlist the playlist structure to destroy
190 * \return VLC_SUCCESS or an error
192 int playlist_Destroy( playlist_t * p_playlist )
195 p_playlist->b_die = 1;
197 for( i = 0 ; i< p_playlist->i_sds ; i++ )
199 playlist_ServicesDiscoveryRemove( p_playlist,
200 p_playlist->pp_sds[i]->psz_module );
203 vlc_thread_join( p_playlist->p_preparse );
204 vlc_thread_join( p_playlist );
206 vlc_object_detach( p_playlist->p_preparse );
208 var_Destroy( p_playlist, "intf-change" );
209 var_Destroy( p_playlist, "item-change" );
210 var_Destroy( p_playlist, "playlist-current" );
211 var_Destroy( p_playlist, "intf-popmenu" );
212 var_Destroy( p_playlist, "intf-show" );
213 var_Destroy( p_playlist, "play-and-stop" );
214 var_Destroy( p_playlist, "random" );
215 var_Destroy( p_playlist, "repeat" );
216 var_Destroy( p_playlist, "loop" );
218 playlist_Clear( p_playlist );
220 for( i = p_playlist->i_views - 1; i >= 0 ; i-- )
222 playlist_view_t *p_view = p_playlist->pp_views[i];
223 if( p_view->psz_name )
224 free( p_view->psz_name );
225 playlist_ItemDelete( p_view->p_root );
226 REMOVE_ELEM( p_playlist->pp_views, p_playlist->i_views, i );
230 vlc_object_destroy( p_playlist->p_preparse );
231 vlc_object_destroy( p_playlist );
238 * Do a playlist action.
240 * If there is something in the playlist then you can do playlist actions.
242 * Playlist lock must not be taken when calling this function
244 * \param p_playlist the playlist to do the command on
245 * \param i_query the command to do
246 * \param variable number of arguments
247 * \return VLC_SUCCESS or an error
249 int playlist_Control( playlist_t * p_playlist, int i_query, ... )
253 va_start( args, i_query );
254 i_result = playlist_vaControl( p_playlist, i_query, args );
260 int playlist_vaControl( playlist_t * p_playlist, int i_query, va_list args )
262 playlist_view_t *p_view;
265 vlc_mutex_lock( &p_playlist->object_lock );
267 #ifdef PLAYLIST_PROFILE
268 p_playlist->request_date = mdate();
271 if( p_playlist->i_size <= 0 )
273 vlc_mutex_unlock( &p_playlist->object_lock );
280 p_playlist->status.i_status = PLAYLIST_STOPPED;
281 p_playlist->request.b_request = VLC_TRUE;
284 case PLAYLIST_ITEMPLAY:
285 p_playlist->status.i_status = PLAYLIST_RUNNING;
286 p_playlist->request.i_skip = 0;
287 p_playlist->request.b_request = VLC_TRUE;
288 p_playlist->request.p_item = (playlist_item_t *)va_arg( args,
290 p_playlist->request.i_view = p_playlist->status.i_view;
291 p_view = playlist_ViewFind( p_playlist, p_playlist->status.i_view );
294 p_playlist->request.p_node = p_view->p_root;
298 p_playlist->request.p_node = NULL;
302 case PLAYLIST_VIEWPLAY:
303 p_playlist->status.i_status = PLAYLIST_RUNNING;
304 p_playlist->request.i_skip = 0;
305 p_playlist->request.b_request = VLC_TRUE;
306 p_playlist->request.i_view = (int)va_arg( args,int );
307 p_playlist->request.p_node = (playlist_item_t *)va_arg( args,
309 p_playlist->request.p_item = (playlist_item_t *)va_arg( args,
312 /* If we select a node, play only it.
313 * If we select an item, continue */
314 if( p_playlist->request.p_item == NULL ||
315 ! p_playlist->request.p_node->i_flags & PLAYLIST_SKIP_FLAG )
317 p_playlist->b_go_next = VLC_FALSE;
321 p_playlist->b_go_next = VLC_TRUE;
326 p_playlist->status.i_status = PLAYLIST_RUNNING;
328 if( p_playlist->p_input )
330 val.i_int = PLAYING_S;
331 var_Set( p_playlist->p_input, "state", val );
335 /* FIXME : needed ? */
336 p_playlist->request.b_request = VLC_TRUE;
337 p_playlist->request.i_view = p_playlist->status.i_view;
338 p_playlist->request.p_node = p_playlist->status.p_node;
339 p_playlist->request.p_item = p_playlist->status.p_item;
340 p_playlist->request.i_skip = 0;
341 p_playlist->request.i_goto = -1;
344 case PLAYLIST_AUTOPLAY:
345 p_playlist->status.i_status = PLAYLIST_RUNNING;
347 p_playlist->request.b_request = VLC_FALSE;
352 if( p_playlist->p_input )
353 var_Get( p_playlist->p_input, "state", &val );
355 if( val.i_int == PAUSE_S )
357 p_playlist->status.i_status = PLAYLIST_RUNNING;
358 if( p_playlist->p_input )
360 val.i_int = PLAYING_S;
361 var_Set( p_playlist->p_input, "state", val );
366 p_playlist->status.i_status = PLAYLIST_PAUSED;
367 if( p_playlist->p_input )
370 var_Set( p_playlist->p_input, "state", val );
376 if( p_playlist->status.i_view > -1 )
378 p_playlist->request.p_node = p_playlist->status.p_node;
379 p_playlist->request.p_item = p_playlist->status.p_item;
381 p_playlist->request.i_skip = (int) va_arg( args, int );
382 p_playlist->request.b_request = VLC_TRUE;
386 p_playlist->status.i_status = PLAYLIST_RUNNING;
387 p_playlist->request.p_node = NULL;
388 p_playlist->request.p_item = NULL;
389 p_playlist->request.i_view = -1;
390 p_playlist->request.i_goto = (int) va_arg( args, int );
391 p_playlist->request.b_request = VLC_TRUE;
395 msg_Err( p_playlist, "unimplemented playlist query" );
400 vlc_mutex_unlock( &p_playlist->object_lock );
404 int playlist_PreparseEnqueue( playlist_t *p_playlist,
405 input_item_t *p_item )
407 vlc_mutex_lock( &p_playlist->p_preparse->object_lock );
408 INSERT_ELEM( p_playlist->p_preparse->pp_waiting,
409 p_playlist->p_preparse->i_waiting,
410 p_playlist->p_preparse->i_waiting,
412 vlc_mutex_unlock( &p_playlist->p_preparse->object_lock );
417 /* Destroy remaining objects */
418 static mtime_t ObjectGarbageCollector( playlist_t *p_playlist, int i_type,
419 mtime_t destroy_date )
423 if( destroy_date > mdate() ) return destroy_date;
425 if( destroy_date == 0 )
427 /* give a little time */
428 return mdate() + I64C(1000000);
432 while( ( p_obj = vlc_object_find( p_playlist, i_type, FIND_CHILD ) ) )
434 if( p_obj->p_parent != (vlc_object_t*)p_playlist )
436 /* only first child (ie unused) */
437 vlc_object_release( p_obj );
440 if( i_type == VLC_OBJECT_VOUT )
442 msg_Dbg( p_playlist, "garbage collector destroying 1 vout" );
443 vlc_object_detach( p_obj );
444 vlc_object_release( p_obj );
445 vout_Destroy( (vout_thread_t *)p_obj );
447 else if( i_type == VLC_OBJECT_SOUT )
449 vlc_object_release( p_obj );
450 sout_DeleteInstance( (sout_instance_t*)p_obj );
457 /*****************************************************************************
458 * RunThread: main playlist thread
459 *****************************************************************************/
460 static void RunThread ( playlist_t *p_playlist )
463 playlist_item_t *p_item;
465 mtime_t i_vout_destroyed_date = 0;
466 mtime_t i_sout_destroyed_date = 0;
468 playlist_item_t *p_autodelete_item = NULL;
470 /* Tell above that we're ready */
471 vlc_thread_ready( p_playlist );
473 while( !p_playlist->b_die )
475 vlc_mutex_lock( &p_playlist->object_lock );
477 /* First, check if we have something to do */
478 /* FIXME : this can be called several times */
479 if( p_playlist->request.b_request )
481 #ifdef PLAYLIST_PROFILE
482 msg_Dbg(p_playlist, "beginning processing of request, "
483 I64Fi" us ", mdate() - p_playlist->request_date );
485 /* Stop the existing input */
486 if( p_playlist->p_input )
488 input_StopThread( p_playlist->p_input );
490 /* The code below will start the next input for us */
491 if( p_playlist->status.i_status == PLAYLIST_STOPPED )
493 p_playlist->request.b_request = VLC_FALSE;
497 /* If there is an input, check that it doesn't need to die. */
498 if( p_playlist->p_input )
500 /* This input is dead. Remove it ! */
501 if( p_playlist->p_input->b_dead )
503 input_thread_t *p_input;
505 p_input = p_playlist->p_input;
506 p_playlist->p_input = NULL;
508 /* Release the playlist lock, because we may get stuck
509 * in input_DestroyThread() for some time. */
510 vlc_mutex_unlock( &p_playlist->object_lock );
513 input_DestroyThread( p_input );
515 /* Unlink current input
516 * (_after_ input_DestroyThread for vout garbage collector) */
517 vlc_object_detach( p_input );
520 vlc_object_destroy( p_input );
522 i_vout_destroyed_date = 0;
523 i_sout_destroyed_date = 0;
527 /* This input is dying, let him do */
528 else if( p_playlist->p_input->b_die )
532 /* This input has finished, ask him to die ! */
533 else if( p_playlist->p_input->b_error
534 || p_playlist->p_input->b_eof )
536 /* TODO FIXME XXX TODO FIXME XXX */
537 /* Check for autodeletion */
539 if( p_playlist->status.p_item->i_flags & PLAYLIST_DEL_FLAG )
541 p_autodelete_item = p_playlist->status.p_item;
543 input_StopThread( p_playlist->p_input );
544 /* Select the next playlist item */
545 vlc_mutex_unlock( &p_playlist->object_lock );
548 else if( p_playlist->p_input->i_state != INIT_S )
550 vlc_mutex_unlock( &p_playlist->object_lock );
551 i_vout_destroyed_date =
552 ObjectGarbageCollector( p_playlist, VLC_OBJECT_VOUT,
553 i_vout_destroyed_date );
554 i_sout_destroyed_date =
555 ObjectGarbageCollector( p_playlist, VLC_OBJECT_SOUT,
556 i_sout_destroyed_date );
557 vlc_mutex_lock( &p_playlist->object_lock );
560 else if( p_playlist->status.i_status != PLAYLIST_STOPPED )
562 /* Start another input.
563 * Get the next item to play */
564 p_item = NextItem( p_playlist );
570 if( p_autodelete_item )
572 playlist_Delete( p_playlist,
573 p_autodelete_item->input.i_id );
574 p_autodelete_item = NULL;
576 p_playlist->status.i_status = PLAYLIST_STOPPED;
577 vlc_mutex_unlock( &p_playlist->object_lock );
581 PlayItem( p_playlist, p_item );
583 if( p_autodelete_item )
585 playlist_Delete( p_playlist, p_autodelete_item->input.i_id );
586 p_autodelete_item = NULL;
589 else if( p_playlist->status.i_status == PLAYLIST_STOPPED )
591 /* Collect garbage */
592 vlc_mutex_unlock( &p_playlist->object_lock );
593 i_sout_destroyed_date =
594 ObjectGarbageCollector( p_playlist, VLC_OBJECT_SOUT, mdate() );
595 i_vout_destroyed_date =
596 ObjectGarbageCollector( p_playlist, VLC_OBJECT_VOUT, mdate() );
597 vlc_mutex_lock( &p_playlist->object_lock );
599 vlc_mutex_unlock( &p_playlist->object_lock );
601 msleep( INTF_IDLE_SLEEP / 2 );
603 /* Stop sleeping earlier if we have work */
604 /* TODO : statistics about this */
605 if ( p_playlist->request.b_request &&
606 p_playlist->status.i_status == PLAYLIST_RUNNING )
611 msleep( INTF_IDLE_SLEEP / 2 );
616 /* If there is an input, kill it */
619 vlc_mutex_lock( &p_playlist->object_lock );
621 if( p_playlist->p_input == NULL )
623 vlc_mutex_unlock( &p_playlist->object_lock );
627 if( p_playlist->p_input->b_dead )
629 input_thread_t *p_input;
631 /* Unlink current input */
632 p_input = p_playlist->p_input;
633 p_playlist->p_input = NULL;
634 vlc_mutex_unlock( &p_playlist->object_lock );
637 input_DestroyThread( p_input );
638 /* Unlink current input (_after_ input_DestroyThread for vout
639 * garbage collector)*/
640 vlc_object_detach( p_input );
643 vlc_object_destroy( p_input );
646 else if( p_playlist->p_input->b_die )
648 /* This input is dying, leave him alone */
651 else if( p_playlist->p_input->b_error || p_playlist->p_input->b_eof )
653 input_StopThread( p_playlist->p_input );
654 vlc_mutex_unlock( &p_playlist->object_lock );
659 p_playlist->p_input->b_eof = 1;
662 vlc_mutex_unlock( &p_playlist->object_lock );
664 msleep( INTF_IDLE_SLEEP );
667 /* close all remaining sout */
668 while( ( p_obj = vlc_object_find( p_playlist,
669 VLC_OBJECT_SOUT, FIND_CHILD ) ) )
671 vlc_object_release( p_obj );
672 sout_DeleteInstance( (sout_instance_t*)p_obj );
675 /* close all remaining vout */
676 while( ( p_obj = vlc_object_find( p_playlist,
677 VLC_OBJECT_VOUT, FIND_CHILD ) ) )
679 vlc_object_detach( p_obj );
680 vlc_object_release( p_obj );
681 vout_Destroy( (vout_thread_t *)p_obj );
685 /* Queue for items to preparse */
686 static void RunPreparse ( playlist_preparse_t *p_obj )
688 playlist_t *p_playlist = (playlist_t *)p_obj->p_parent;
691 /* Tell above that we're ready */
692 vlc_thread_ready( p_obj );
694 while( !p_playlist->b_die )
696 vlc_mutex_lock( &p_obj->object_lock );
698 if( p_obj->i_waiting > 0 )
700 input_item_t *p_current = p_obj->pp_waiting[0];
701 REMOVE_ELEM( p_obj->pp_waiting, p_obj->i_waiting, 0 );
702 vlc_mutex_unlock( &p_obj->object_lock );
703 input_Preparse( p_playlist, p_current );
704 var_SetInteger( p_playlist, "item-change", p_current->i_id );
705 vlc_mutex_lock( &p_obj->object_lock );
707 b_sleep = ( p_obj->i_waiting == 0 );
709 vlc_mutex_unlock( &p_obj->object_lock );
711 if( p_obj->i_waiting == 0 )
713 msleep( INTF_IDLE_SLEEP );
718 /*****************************************************************************
720 *****************************************************************************
721 * This function calculates the next playlist item, depending
722 * on the playlist course mode (forward, backward, random, view,...).
723 *****************************************************************************/
724 static playlist_item_t * NextItem( playlist_t *p_playlist )
726 playlist_item_t *p_new = NULL;
727 int i_skip,i_goto,i, i_new, i_count ;
728 playlist_view_t *p_view;
730 vlc_bool_t b_loop = var_GetBool( p_playlist, "loop");
731 vlc_bool_t b_random = var_GetBool( p_playlist, "random" );
732 vlc_bool_t b_repeat = var_GetBool( p_playlist, "repeat" );
733 vlc_bool_t b_playstop = var_GetBool( p_playlist, "play-and-stop" );
735 #ifdef PLAYLIST_PROFILE
736 /* Calculate time needed */
737 int64_t start = mdate();
741 /* Handle quickly a few special cases */
743 /* No items to play */
744 if( p_playlist->i_size == 0 )
746 msg_Info( p_playlist, "playlist is empty" );
749 /* Nothing requested */
750 if( !p_playlist->request.b_request && p_playlist->status.p_item == NULL )
752 msg_Dbg( p_playlist,"nothing requested, starting" );
755 /* Repeat and play/stop */
756 if( !p_playlist->request.b_request && b_repeat == VLC_TRUE )
758 msg_Dbg( p_playlist,"repeating item" );
759 return p_playlist->status.p_item;
762 if( !p_playlist->request.b_request && b_playstop == VLC_TRUE )
764 msg_Dbg( p_playlist,"stopping (play and stop)");
768 if( !p_playlist->request.b_request && p_playlist->status.p_item &&
769 !(p_playlist->status.p_item->i_flags & PLAYLIST_SKIP_FLAG) )
771 msg_Dbg( p_playlist, "no-skip mode, stopping") ;
775 /* TODO: improve this (only use current node) */
776 /* TODO: use the "shuffled view" internally ? */
777 /* Random case. This is an exception: if request, but request is skip +- 1
778 * we don't go to next item but select a new random one. */
779 if( b_random && (!p_playlist->request.b_request ||
780 p_playlist->request.i_skip == 1 || p_playlist->request.i_skip == -1 ) )
782 srand( (unsigned int)mdate() );
784 for( i_count = 0; i_count < p_playlist->i_size - 1 ; i_count ++ )
787 (int)((float)p_playlist->i_size * rand() / (RAND_MAX+1.0));
788 /* Check if the item has not already been played */
789 if( p_playlist->pp_items[i_new]->i_nb_played == 0 )
792 if( i_count == p_playlist->i_size )
794 /* The whole playlist has been played: reset the counters */
797 p_playlist->pp_items[--i_count]->i_nb_played = 0;
804 p_playlist->request.i_skip = 0;
805 p_playlist->request.b_request = VLC_FALSE;
806 return p_playlist->pp_items[i_new];
809 /* Start the real work */
810 if( p_playlist->request.b_request )
812 msg_Dbg( p_playlist,"processing request" );
813 /* We are not playing from a view */
814 if( p_playlist->request.i_view == -1 )
816 #ifdef PLAYLIST_DEBUG
817 msg_Dbg( p_playlist, "non-view mode request");
819 /* Directly select the item, just like now */
820 i_skip = p_playlist->request.i_skip;
821 i_goto = p_playlist->request.i_goto;
823 if( p_playlist->i_index == -1 ) p_playlist->i_index = 0;
824 p_new = p_playlist->pp_items[p_playlist->i_index];
826 if( i_goto >= 0 && i_goto < p_playlist->i_size )
828 p_playlist->i_index = i_goto;
829 p_new = p_playlist->pp_items[p_playlist->i_index];
830 p_playlist->request.i_goto = -1;
835 if( p_playlist->i_index + i_skip < p_playlist->i_size &&
836 p_playlist->i_index + i_skip >= 0 )
838 p_playlist->i_index += i_skip;
839 p_new = p_playlist->pp_items[p_playlist->i_index];
845 #ifdef PLAYLIST_DEBUG
846 msg_Dbg( p_playlist, "view mode request" );
848 p_new = p_playlist->request.p_item;
849 i_skip = p_playlist->request.i_skip;
851 /* If we are asked for a node, take its first item */
852 if( p_playlist->request.p_item == NULL && i_skip == 0 )
857 p_view = playlist_ViewFind( p_playlist,p_playlist->request.i_view );
858 p_playlist->status.p_node = p_playlist->request.p_node;
859 p_playlist->status.i_view = p_playlist->request.i_view;
862 msg_Err( p_playlist, "p_view is NULL and should not! (FIXME)" );
864 else if( i_skip > 0 )
866 for( i = i_skip; i > 0 ; i-- )
868 p_new = playlist_FindNextFromParent( p_playlist,
869 p_playlist->request.i_view,
871 p_playlist->request.p_node,
877 p_new = playlist_FindNextFromParent( p_playlist,
878 p_playlist->request.i_view,
880 p_playlist->request.p_node,
882 if( p_new == NULL ) break;
891 else if( i_skip < 0 )
893 for( i = i_skip; i < 0 ; i++ )
895 p_new = playlist_FindPrevFromParent( p_playlist,
896 p_playlist->request.i_view,
898 p_playlist->request.p_node,
900 if( p_new == NULL ) break;
905 /* Clear the request */
906 p_playlist->request.b_request = VLC_FALSE;
908 /* "Automatic" item change ( next ) */
911 p_playlist->request_date = 0;
913 if( p_playlist->status.i_view == -1 )
915 if( p_playlist->i_index + 1 < p_playlist->i_size )
917 p_playlist->i_index++;
918 p_new = p_playlist->pp_items[p_playlist->i_index];
919 if( !(p_new->i_flags & PLAYLIST_SKIP_FLAG) )
926 msg_Dbg( p_playlist,"finished" );
927 if( b_loop && p_playlist->i_size > 0)
929 p_playlist->i_index = 0;
930 p_new = p_playlist->pp_items[0];
936 /* We are playing with a view */
939 playlist_view_t *p_view =
940 playlist_ViewFind( p_playlist,
941 p_playlist->status.i_view );
944 msg_Err( p_playlist, "p_view is NULL and should not! (FIXME)" );
948 p_new = playlist_FindNextFromParent( p_playlist,
949 p_playlist->status.i_view,
951 p_playlist->status.p_node,
952 p_playlist->status.p_item );
953 if( p_new == NULL && b_loop )
955 p_new = playlist_FindNextFromParent( p_playlist,
956 p_playlist->status.i_view,
958 p_playlist->status.p_node,
966 if( p_playlist->i_index >= 0 && p_new != NULL &&
967 p_playlist->pp_items[p_playlist->i_index] != p_new )
969 p_playlist->i_index = playlist_GetPositionById( p_playlist,
973 #ifdef PLAYLIST_PROFILE
974 msg_Dbg(p_playlist,"next item found in "I64Fi " us", mdate()-start );
979 msg_Info( p_playlist, "Nothing to play" );
988 static void SkipItem( playlist_t *p_playlist, int i_arg )
990 int i_oldindex = p_playlist->i_index;
991 vlc_bool_t b_random, b_repeat, b_loop;
998 p_playlist->i_index += i_arg;
1001 /* Boundary check */
1002 if( p_playlist->i_index >= p_playlist->i_size )
1004 if( p_playlist->i_status == PLAYLIST_STOPPED || b_random || b_loop )
1006 p_playlist->i_index -= p_playlist->i_size
1007 * ( p_playlist->i_index / p_playlist->i_size );
1011 /* Don't loop by default: stop at playlist end */
1012 p_playlist->i_index = i_oldindex;
1013 p_playlist->i_status = PLAYLIST_STOPPED;
1016 else if( p_playlist->i_index < 0 )
1018 p_playlist->i_index = p_playlist->i_size - 1;
1021 /* Check that the item is enabled */
1022 if( p_playlist->pp_items[p_playlist->i_index]->b_enabled == VLC_FALSE &&
1023 p_playlist->i_enabled != 0)
1025 SkipItem( p_playlist , 1 );
1031 /*****************************************************************************
1032 * PlayItem: start the input thread for an item
1033 ****************************************************************************/
1034 static int PlayItem( playlist_t *p_playlist, playlist_item_t *p_item )
1038 msg_Dbg( p_playlist, "creating new input thread" );
1040 p_item->i_nb_played++;
1041 p_playlist->status.p_item = p_item;
1043 p_playlist->i_index = playlist_GetPositionById( p_playlist,
1044 p_item->input.i_id );
1046 #ifdef PLAYLIST_PROFILE
1047 if( p_playlist->request_date != 0 )
1049 msg_Dbg( p_playlist, "request processed after "I64Fi " us",
1050 mdate() - p_playlist->request_date );
1054 p_playlist->p_input = input_CreateThread( p_playlist, &p_item->input );
1056 var_AddCallback( p_playlist->p_input, "item-change",
1057 ItemChange, p_playlist );
1059 val.i_int = p_item->input.i_id;
1060 /* unlock the playlist to set the var...mmm */
1061 vlc_mutex_unlock( &p_playlist->object_lock);
1062 var_Set( p_playlist, "playlist-current", val);
1063 vlc_mutex_lock( &p_playlist->object_lock);
1069 /* Forward item change from input */
1070 static int ItemChange( vlc_object_t *p_obj, const char *psz_var,
1071 vlc_value_t oldval, vlc_value_t newval, void *param )
1073 playlist_t *p_playlist = (playlist_t *)param;
1075 //p_playlist->b_need_update = VLC_TRUE;
1076 var_SetInteger( p_playlist, "item-change", newval.i_int );
1079 /* FIXME: Make that automatic */
1080 // playlist_ViewUpdate( p_playlist, VIEW_S_AUTHOR );