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-append", VLC_VAR_ADDRESS );
88 var_Create( p_playlist, "playlist-current", VLC_VAR_INTEGER );
90 var_Set( p_playlist, "playlist-current", val );
92 var_Create( p_playlist, "intf-popupmenu", VLC_VAR_BOOL );
94 var_Create( p_playlist, "intf-show", VLC_VAR_BOOL );
95 val.b_bool = VLC_TRUE;
96 var_Set( p_playlist, "intf-show", val );
99 /* Variables to control playback */
100 var_CreateGetBool( p_playlist, "play-and-stop" );
101 var_CreateGetBool( p_playlist, "random" );
102 var_CreateGetBool( p_playlist, "repeat" );
103 var_CreateGetBool( p_playlist, "loop" );
105 /* Initialise data structures */
106 p_playlist->b_go_next = VLC_TRUE;
107 p_playlist->p_input = NULL;
109 p_playlist->request_date = 0;
111 p_playlist->i_views = 0;
112 p_playlist->pp_views = NULL;
114 p_playlist->i_index = -1;
115 p_playlist->i_size = 0;
116 p_playlist->pp_items = NULL;
118 playlist_ViewInsert( p_playlist, VIEW_CATEGORY, TITLE_CATEGORY );
119 playlist_ViewInsert( p_playlist, VIEW_SIMPLE, TITLE_SIMPLE );
120 playlist_ViewInsert( p_playlist, VIEW_ALL, TITLE_ALL );
122 p_view = playlist_ViewFind( p_playlist, VIEW_CATEGORY );
124 p_playlist->p_general = playlist_NodeCreate( p_playlist, VIEW_CATEGORY,
125 _( "General" ), p_view->p_root );
127 /* Set startup status
128 * We set to simple view on startup for interfaces that don't do
130 p_view = playlist_ViewFind( p_playlist, VIEW_SIMPLE );
131 p_playlist->status.i_view = VIEW_SIMPLE;
132 p_playlist->status.p_item = NULL;
133 p_playlist->status.p_node = p_view->p_root;
134 p_playlist->request.b_request = VLC_FALSE;
135 p_playlist->status.i_status = PLAYLIST_STOPPED;
138 p_playlist->i_last_id = 0;
139 p_playlist->i_sort = SORT_ID;
140 p_playlist->i_order = ORDER_NORMAL;
142 /* Finally, launch the thread ! */
143 if( vlc_thread_create( p_playlist, "playlist", RunThread,
144 VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
146 msg_Err( p_playlist, "cannot spawn playlist thread" );
147 vlc_object_destroy( p_playlist );
151 /* Preparsing stuff */
152 p_playlist->p_preparse = vlc_object_create( p_playlist,
153 sizeof( playlist_preparse_t ) );
154 if( !p_playlist->p_preparse )
156 msg_Err( p_playlist, "unable to create preparser" );
157 vlc_object_destroy( p_playlist );
161 p_playlist->p_preparse->i_waiting = 0;
162 p_playlist->p_preparse->pp_waiting = NULL;
163 vlc_mutex_init( p_playlist->p_preparse,
164 &p_playlist->p_preparse->object_lock );
166 vlc_object_attach( p_playlist->p_preparse, p_playlist );
167 if( vlc_thread_create( p_playlist->p_preparse, "preparser",
168 RunPreparse, VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
170 msg_Err( p_playlist, "cannot spawn preparse thread" );
171 vlc_object_detach( p_playlist->p_preparse );
172 vlc_object_destroy( p_playlist->p_preparse );
176 /* The object has been initialized, now attach it */
177 vlc_object_attach( p_playlist, p_parent );
183 * Destroy the playlist.
185 * Delete all items in the playlist and free the playlist structure.
186 * \param p_playlist the playlist structure to destroy
187 * \return VLC_SUCCESS or an error
189 int playlist_Destroy( playlist_t * p_playlist )
192 p_playlist->b_die = 1;
194 for( i = 0 ; i< p_playlist->i_sds ; i++ )
196 playlist_ServicesDiscoveryRemove( p_playlist,
197 p_playlist->pp_sds[i]->psz_module );
200 vlc_thread_join( p_playlist->p_preparse );
201 vlc_thread_join( p_playlist );
203 vlc_object_detach( p_playlist->p_preparse );
205 vlc_mutex_destroy( &p_playlist->p_preparse->object_lock );
207 var_Destroy( p_playlist, "intf-change" );
208 var_Destroy( p_playlist, "item-change" );
209 var_Destroy( p_playlist, "playlist-current" );
210 var_Destroy( p_playlist, "intf-popmenu" );
211 var_Destroy( p_playlist, "intf-show" );
212 var_Destroy( p_playlist, "play-and-stop" );
213 var_Destroy( p_playlist, "random" );
214 var_Destroy( p_playlist, "repeat" );
215 var_Destroy( p_playlist, "loop" );
217 playlist_Clear( p_playlist );
219 for( i = p_playlist->i_views - 1; i >= 0 ; i-- )
221 playlist_view_t *p_view = p_playlist->pp_views[i];
222 if( p_view->psz_name )
223 free( p_view->psz_name );
224 playlist_ItemDelete( p_view->p_root );
225 REMOVE_ELEM( p_playlist->pp_views, p_playlist->i_views, i );
229 vlc_object_destroy( p_playlist->p_preparse );
230 vlc_object_destroy( p_playlist );
237 * Do a playlist action.
239 * If there is something in the playlist then you can do playlist actions.
241 * Playlist lock must not be taken when calling this function
243 * \param p_playlist the playlist to do the command on
244 * \param i_query the command to do
245 * \param variable number of arguments
246 * \return VLC_SUCCESS or an error
248 int playlist_Control( playlist_t * p_playlist, int i_query, ... )
252 va_start( args, i_query );
253 i_result = playlist_vaControl( p_playlist, i_query, args );
259 int playlist_vaControl( playlist_t * p_playlist, int i_query, va_list args )
261 playlist_view_t *p_view;
264 vlc_mutex_lock( &p_playlist->object_lock );
266 #ifdef PLAYLIST_PROFILE
267 p_playlist->request_date = mdate();
270 if( p_playlist->i_size <= 0 )
272 vlc_mutex_unlock( &p_playlist->object_lock );
279 p_playlist->status.i_status = PLAYLIST_STOPPED;
280 p_playlist->request.b_request = VLC_TRUE;
283 case PLAYLIST_ITEMPLAY:
284 p_playlist->status.i_status = PLAYLIST_RUNNING;
285 p_playlist->request.i_skip = 0;
286 p_playlist->request.b_request = VLC_TRUE;
287 p_playlist->request.p_item = (playlist_item_t *)va_arg( args,
289 p_playlist->request.i_view = p_playlist->status.i_view;
290 p_view = playlist_ViewFind( p_playlist, p_playlist->status.i_view );
293 p_playlist->request.p_node = p_view->p_root;
297 p_playlist->request.p_node = NULL;
301 case PLAYLIST_VIEWPLAY:
302 p_playlist->status.i_status = PLAYLIST_RUNNING;
303 p_playlist->request.i_skip = 0;
304 p_playlist->request.b_request = VLC_TRUE;
305 p_playlist->request.i_view = (int)va_arg( args,int );
306 p_playlist->request.p_node = (playlist_item_t *)va_arg( args,
308 p_playlist->request.p_item = (playlist_item_t *)va_arg( args,
311 /* If we select a node, play only it.
312 * If we select an item, continue */
313 if( p_playlist->request.p_item == NULL ||
314 ! p_playlist->request.p_node->i_flags & PLAYLIST_SKIP_FLAG )
316 p_playlist->b_go_next = VLC_FALSE;
320 p_playlist->b_go_next = VLC_TRUE;
325 p_playlist->status.i_status = PLAYLIST_RUNNING;
327 if( p_playlist->p_input )
329 val.i_int = PLAYING_S;
330 var_Set( p_playlist->p_input, "state", val );
334 /* FIXME : needed ? */
335 p_playlist->request.b_request = VLC_TRUE;
336 p_playlist->request.i_view = p_playlist->status.i_view;
337 p_playlist->request.p_node = p_playlist->status.p_node;
338 p_playlist->request.p_item = p_playlist->status.p_item;
339 p_playlist->request.i_skip = 0;
340 p_playlist->request.i_goto = -1;
343 case PLAYLIST_AUTOPLAY:
344 p_playlist->status.i_status = PLAYLIST_RUNNING;
346 p_playlist->request.b_request = VLC_FALSE;
351 if( p_playlist->p_input )
352 var_Get( p_playlist->p_input, "state", &val );
354 if( val.i_int == PAUSE_S )
356 p_playlist->status.i_status = PLAYLIST_RUNNING;
357 if( p_playlist->p_input )
359 val.i_int = PLAYING_S;
360 var_Set( p_playlist->p_input, "state", val );
365 p_playlist->status.i_status = PLAYLIST_PAUSED;
366 if( p_playlist->p_input )
369 var_Set( p_playlist->p_input, "state", val );
375 if( p_playlist->status.i_view > -1 )
377 p_playlist->request.p_node = p_playlist->status.p_node;
378 p_playlist->request.p_item = p_playlist->status.p_item;
380 p_playlist->request.i_skip = (int) va_arg( args, int );
381 p_playlist->request.b_request = VLC_TRUE;
385 p_playlist->status.i_status = PLAYLIST_RUNNING;
386 p_playlist->request.p_node = NULL;
387 p_playlist->request.p_item = NULL;
388 p_playlist->request.i_view = -1;
389 p_playlist->request.i_goto = (int) va_arg( args, int );
390 p_playlist->request.b_request = VLC_TRUE;
394 msg_Err( p_playlist, "unimplemented playlist query" );
399 vlc_mutex_unlock( &p_playlist->object_lock );
403 int playlist_PreparseEnqueue( playlist_t *p_playlist,
404 input_item_t *p_item )
406 vlc_mutex_lock( &p_playlist->p_preparse->object_lock );
407 INSERT_ELEM( p_playlist->p_preparse->pp_waiting,
408 p_playlist->p_preparse->i_waiting,
409 p_playlist->p_preparse->i_waiting,
411 vlc_mutex_unlock( &p_playlist->p_preparse->object_lock );
416 /* Destroy remaining objects */
417 static mtime_t ObjectGarbageCollector( playlist_t *p_playlist, int i_type,
418 mtime_t destroy_date )
422 if( destroy_date > mdate() ) return destroy_date;
424 if( destroy_date == 0 )
426 /* give a little time */
427 return mdate() + I64C(1000000);
431 while( ( p_obj = vlc_object_find( p_playlist, i_type, FIND_CHILD ) ) )
433 if( p_obj->p_parent != (vlc_object_t*)p_playlist )
435 /* only first child (ie unused) */
436 vlc_object_release( p_obj );
439 if( i_type == VLC_OBJECT_VOUT )
441 msg_Dbg( p_playlist, "garbage collector destroying 1 vout" );
442 vlc_object_detach( p_obj );
443 vlc_object_release( p_obj );
444 vout_Destroy( (vout_thread_t *)p_obj );
446 else if( i_type == VLC_OBJECT_SOUT )
448 vlc_object_release( p_obj );
449 sout_DeleteInstance( (sout_instance_t*)p_obj );
456 /*****************************************************************************
457 * RunThread: main playlist thread
458 *****************************************************************************/
459 static void RunThread ( playlist_t *p_playlist )
462 playlist_item_t *p_item;
464 mtime_t i_vout_destroyed_date = 0;
465 mtime_t i_sout_destroyed_date = 0;
467 playlist_item_t *p_autodelete_item = NULL;
469 /* Tell above that we're ready */
470 vlc_thread_ready( p_playlist );
472 while( !p_playlist->b_die )
474 vlc_mutex_lock( &p_playlist->object_lock );
476 /* First, check if we have something to do */
477 /* FIXME : this can be called several times */
478 if( p_playlist->request.b_request )
480 #ifdef PLAYLIST_PROFILE
481 msg_Dbg(p_playlist, "beginning processing of request, "
482 I64Fi" us ", mdate() - p_playlist->request_date );
484 /* Stop the existing input */
485 if( p_playlist->p_input )
487 input_StopThread( p_playlist->p_input );
489 /* The code below will start the next input for us */
490 if( p_playlist->status.i_status == PLAYLIST_STOPPED )
492 p_playlist->request.b_request = VLC_FALSE;
496 /* If there is an input, check that it doesn't need to die. */
497 if( p_playlist->p_input )
499 /* This input is dead. Remove it ! */
500 if( p_playlist->p_input->b_dead )
502 input_thread_t *p_input;
504 p_input = p_playlist->p_input;
505 p_playlist->p_input = NULL;
507 /* Release the playlist lock, because we may get stuck
508 * in input_DestroyThread() for some time. */
509 vlc_mutex_unlock( &p_playlist->object_lock );
512 input_DestroyThread( p_input );
514 /* Unlink current input
515 * (_after_ input_DestroyThread for vout garbage collector) */
516 vlc_object_detach( p_input );
519 vlc_object_destroy( p_input );
521 i_vout_destroyed_date = 0;
522 i_sout_destroyed_date = 0;
526 /* This input is dying, let him do */
527 else if( p_playlist->p_input->b_die )
531 /* This input has finished, ask him to die ! */
532 else if( p_playlist->p_input->b_error
533 || p_playlist->p_input->b_eof )
535 /* TODO FIXME XXX TODO FIXME XXX */
536 /* Check for autodeletion */
538 if( p_playlist->status.p_item->i_flags & PLAYLIST_DEL_FLAG )
540 p_autodelete_item = p_playlist->status.p_item;
542 input_StopThread( p_playlist->p_input );
543 /* Select the next playlist item */
544 vlc_mutex_unlock( &p_playlist->object_lock );
547 else if( p_playlist->p_input->i_state != INIT_S )
549 vlc_mutex_unlock( &p_playlist->object_lock );
550 i_vout_destroyed_date =
551 ObjectGarbageCollector( p_playlist, VLC_OBJECT_VOUT,
552 i_vout_destroyed_date );
553 i_sout_destroyed_date =
554 ObjectGarbageCollector( p_playlist, VLC_OBJECT_SOUT,
555 i_sout_destroyed_date );
556 vlc_mutex_lock( &p_playlist->object_lock );
559 else if( p_playlist->status.i_status != PLAYLIST_STOPPED )
561 /* Start another input.
562 * Get the next item to play */
563 p_item = NextItem( p_playlist );
569 if( p_autodelete_item )
571 vlc_mutex_unlock( &p_playlist->object_lock );
572 playlist_Delete( p_playlist,
573 p_autodelete_item->input.i_id );
574 vlc_mutex_lock( &p_playlist->object_lock );
575 p_autodelete_item = NULL;
577 p_playlist->status.i_status = PLAYLIST_STOPPED;
578 vlc_mutex_unlock( &p_playlist->object_lock );
582 PlayItem( p_playlist, p_item );
584 if( p_autodelete_item )
586 vlc_mutex_unlock( &p_playlist->object_lock );
587 playlist_Delete( p_playlist, p_autodelete_item->input.i_id );
588 vlc_mutex_lock( &p_playlist->object_lock );
589 p_autodelete_item = NULL;
592 else if( p_playlist->status.i_status == PLAYLIST_STOPPED )
594 /* Collect garbage */
595 vlc_mutex_unlock( &p_playlist->object_lock );
596 i_sout_destroyed_date =
597 ObjectGarbageCollector( p_playlist, VLC_OBJECT_SOUT, mdate() );
598 i_vout_destroyed_date =
599 ObjectGarbageCollector( p_playlist, VLC_OBJECT_VOUT, mdate() );
600 vlc_mutex_lock( &p_playlist->object_lock );
602 vlc_mutex_unlock( &p_playlist->object_lock );
604 msleep( INTF_IDLE_SLEEP / 2 );
606 /* Stop sleeping earlier if we have work */
607 /* TODO : statistics about this */
608 if ( p_playlist->request.b_request &&
609 p_playlist->status.i_status == PLAYLIST_RUNNING )
614 msleep( INTF_IDLE_SLEEP / 2 );
619 /* If there is an input, kill it */
622 vlc_mutex_lock( &p_playlist->object_lock );
624 if( p_playlist->p_input == NULL )
626 vlc_mutex_unlock( &p_playlist->object_lock );
630 if( p_playlist->p_input->b_dead )
632 input_thread_t *p_input;
634 /* Unlink current input */
635 p_input = p_playlist->p_input;
636 p_playlist->p_input = NULL;
637 vlc_mutex_unlock( &p_playlist->object_lock );
640 input_DestroyThread( p_input );
641 /* Unlink current input (_after_ input_DestroyThread for vout
642 * garbage collector)*/
643 vlc_object_detach( p_input );
646 vlc_object_destroy( p_input );
649 else if( p_playlist->p_input->b_die )
651 /* This input is dying, leave him alone */
654 else if( p_playlist->p_input->b_error || p_playlist->p_input->b_eof )
656 input_StopThread( p_playlist->p_input );
657 vlc_mutex_unlock( &p_playlist->object_lock );
662 p_playlist->p_input->b_eof = 1;
665 vlc_mutex_unlock( &p_playlist->object_lock );
667 msleep( INTF_IDLE_SLEEP );
670 /* close all remaining sout */
671 while( ( p_obj = vlc_object_find( p_playlist,
672 VLC_OBJECT_SOUT, FIND_CHILD ) ) )
674 vlc_object_release( p_obj );
675 sout_DeleteInstance( (sout_instance_t*)p_obj );
678 /* close all remaining vout */
679 while( ( p_obj = vlc_object_find( p_playlist,
680 VLC_OBJECT_VOUT, FIND_CHILD ) ) )
682 vlc_object_detach( p_obj );
683 vlc_object_release( p_obj );
684 vout_Destroy( (vout_thread_t *)p_obj );
688 /* Queue for items to preparse */
689 static void RunPreparse ( playlist_preparse_t *p_obj )
691 playlist_t *p_playlist = p_obj->p_parent;
694 /* Tell above that we're ready */
695 vlc_thread_ready( p_obj );
697 while( !p_playlist->b_die )
699 vlc_mutex_lock( &p_obj->object_lock );
701 if( p_obj->i_waiting > 0 )
703 input_Preparse( p_playlist, p_obj->pp_waiting[0] );
704 var_SetInteger( p_playlist, "item-change",
705 p_obj->pp_waiting[0]->i_id );
706 REMOVE_ELEM( p_obj->pp_waiting, p_obj->i_waiting, 0 );
708 b_sleep = ( p_obj->i_waiting == 0 );
710 vlc_mutex_unlock( &p_obj->object_lock );
712 if( p_obj->i_waiting == 0 )
714 msleep( INTF_IDLE_SLEEP );
719 /*****************************************************************************
721 *****************************************************************************
722 * This function calculates the next playlist item, depending
723 * on the playlist course mode (forward, backward, random, view,...).
724 *****************************************************************************/
725 static playlist_item_t * NextItem( playlist_t *p_playlist )
727 playlist_item_t *p_new = NULL;
728 int i_skip,i_goto,i, i_new, i_count ;
729 playlist_view_t *p_view;
731 vlc_bool_t b_loop = var_GetBool( p_playlist, "loop");
732 vlc_bool_t b_random = var_GetBool( p_playlist, "random" );
733 vlc_bool_t b_repeat = var_GetBool( p_playlist, "repeat" );
734 vlc_bool_t b_playstop = var_GetBool( p_playlist, "play-and-stop" );
736 #ifdef PLAYLIST_PROFILE
737 /* Calculate time needed */
738 int64_t start = mdate();
742 /* Handle quickly a few special cases */
744 /* No items to play */
745 if( p_playlist->i_size == 0 )
747 msg_Info( p_playlist, "playlist is empty" );
750 /* Nothing requested */
751 if( !p_playlist->request.b_request && p_playlist->status.p_item == NULL )
753 msg_Dbg( p_playlist,"nothing requested, starting" );
756 /* Repeat and play/stop */
757 if( !p_playlist->request.b_request && b_repeat == VLC_TRUE )
759 msg_Dbg( p_playlist,"repeating item" );
760 return p_playlist->status.p_item;
763 if( !p_playlist->request.b_request && b_playstop == VLC_TRUE )
765 msg_Dbg( p_playlist,"stopping (play and stop)");
769 if( !p_playlist->request.b_request && p_playlist->status.p_item &&
770 !(p_playlist->status.p_item->i_flags & PLAYLIST_SKIP_FLAG) )
772 msg_Dbg( p_playlist, "no-skip mode, stopping") ;
776 /* TODO: improve this (only use current node) */
777 /* TODO: use the "shuffled view" internally ? */
778 /* Random case. This is an exception: if request, but request is skip +- 1
779 * we don't go to next item but select a new random one. */
780 if( b_random && (!p_playlist->request.b_request ||
781 p_playlist->request.i_skip == 1 || p_playlist->request.i_skip == -1 ) )
783 srand( (unsigned int)mdate() );
785 for( i_count = 0; i_count < p_playlist->i_size - 1 ; i_count ++ )
788 (int)((float)p_playlist->i_size * rand() / (RAND_MAX+1.0));
789 /* Check if the item has not already been played */
790 if( p_playlist->pp_items[i_new]->i_nb_played == 0 )
793 if( i_count == p_playlist->i_size )
795 /* The whole playlist has been played: reset the counters */
798 p_playlist->pp_items[--i_count]->i_nb_played = 0;
805 p_playlist->request.i_skip = 0;
806 p_playlist->request.b_request = VLC_FALSE;
807 return p_playlist->pp_items[i_new];
810 /* Start the real work */
811 if( p_playlist->request.b_request )
813 msg_Dbg( p_playlist,"processing request" );
814 /* We are not playing from a view */
815 if( p_playlist->request.i_view == -1 )
817 #ifdef PLAYLIST_DEBUG
818 msg_Dbg( p_playlist, "non-view mode request");
820 /* Directly select the item, just like now */
821 i_skip = p_playlist->request.i_skip;
822 i_goto = p_playlist->request.i_goto;
824 if( p_playlist->i_index == -1 ) p_playlist->i_index = 0;
825 p_new = p_playlist->pp_items[p_playlist->i_index];
827 if( i_goto >= 0 && i_goto < p_playlist->i_size )
829 p_playlist->i_index = i_goto;
830 p_new = p_playlist->pp_items[p_playlist->i_index];
831 p_playlist->request.i_goto = -1;
836 if( p_playlist->i_index + i_skip < p_playlist->i_size &&
837 p_playlist->i_index + i_skip >= 0 )
839 p_playlist->i_index += i_skip;
840 p_new = p_playlist->pp_items[p_playlist->i_index];
846 #ifdef PLAYLIST_DEBUG
847 msg_Dbg( p_playlist, "view mode request" );
849 p_new = p_playlist->request.p_item;
850 i_skip = p_playlist->request.i_skip;
852 /* If we are asked for a node, take its first item */
853 if( p_playlist->request.p_item == NULL && i_skip == 0 )
858 p_view = playlist_ViewFind( p_playlist,p_playlist->request.i_view );
859 p_playlist->status.p_node = p_playlist->request.p_node;
860 p_playlist->status.i_view = p_playlist->request.i_view;
863 for( i = i_skip; i > 0 ; i-- )
865 p_new = playlist_FindNextFromParent( p_playlist,
866 p_playlist->request.i_view,
868 p_playlist->request.p_node,
870 if( p_new == NULL ) break;
873 else if( i_skip < 0 )
875 for( i = i_skip; i < 0 ; i++ )
877 p_new = playlist_FindPrevFromParent( p_playlist,
878 p_playlist->request.i_view,
880 p_playlist->request.p_node,
882 if( p_new == NULL ) break;
887 /* Clear the request */
888 p_playlist->request.b_request = VLC_FALSE;
890 /* "Automatic" item change ( next ) */
893 p_playlist->request_date = 0;
895 if( p_playlist->status.i_view == -1 )
897 if( p_playlist->i_index + 1 < p_playlist->i_size )
899 p_playlist->i_index++;
900 p_new = p_playlist->pp_items[p_playlist->i_index];
901 if( !(p_new->i_flags & PLAYLIST_SKIP_FLAG) )
908 msg_Dbg( p_playlist,"finished" );
912 /* We are playing with a view */
915 playlist_view_t *p_view =
916 playlist_ViewFind( p_playlist,
917 p_playlist->status.i_view );
918 fprintf(stderr,"Finding next of %s within %s, view %i\n",
919 p_playlist->status.p_item ? p_playlist->status.p_item->input.psz_name : "coincoin",
920 p_playlist->status.p_node->input.psz_name,
921 p_playlist->status.i_view) ;
922 p_new = playlist_FindNextFromParent( p_playlist,
923 p_playlist->status.i_view,
925 p_playlist->status.p_node,
926 p_playlist->status.p_item );
931 if( p_playlist->i_index >= 0 && p_new != NULL &&
932 p_playlist->pp_items[p_playlist->i_index] != p_new )
934 p_playlist->i_index = playlist_GetPositionById( p_playlist,
938 #ifdef PLAYLIST_PROFILE
939 msg_Dbg(p_playlist,"next item found in "I64Fi " us", mdate()-start );
942 if( p_new == NULL ) { msg_Info( p_playlist, "Nothing to play" ); }
951 static void SkipItem( playlist_t *p_playlist, int i_arg )
953 int i_oldindex = p_playlist->i_index;
954 vlc_bool_t b_random, b_repeat, b_loop;
961 p_playlist->i_index += i_arg;
965 if( p_playlist->i_index >= p_playlist->i_size )
967 if( p_playlist->i_status == PLAYLIST_STOPPED || b_random || b_loop )
969 p_playlist->i_index -= p_playlist->i_size
970 * ( p_playlist->i_index / p_playlist->i_size );
974 /* Don't loop by default: stop at playlist end */
975 p_playlist->i_index = i_oldindex;
976 p_playlist->i_status = PLAYLIST_STOPPED;
979 else if( p_playlist->i_index < 0 )
981 p_playlist->i_index = p_playlist->i_size - 1;
984 /* Check that the item is enabled */
985 if( p_playlist->pp_items[p_playlist->i_index]->b_enabled == VLC_FALSE &&
986 p_playlist->i_enabled != 0)
988 SkipItem( p_playlist , 1 );
994 /*****************************************************************************
995 * PlayItem: start the input thread for an item
996 ****************************************************************************/
997 static int PlayItem( playlist_t *p_playlist, playlist_item_t *p_item )
1001 msg_Dbg( p_playlist, "creating new input thread" );
1003 p_item->i_nb_played++;
1004 p_playlist->status.p_item = p_item;
1006 p_playlist->i_index = playlist_GetPositionById( p_playlist,
1007 p_item->input.i_id );
1009 #ifdef PLAYLIST_PROFILE
1010 if( p_playlist->request_date != 0 )
1012 msg_Dbg( p_playlist, "request processed after "I64Fi " us",
1013 mdate() - p_playlist->request_date );
1017 p_playlist->p_input = input_CreateThread( p_playlist, &p_item->input );
1019 var_AddCallback( p_playlist->p_input, "item-change",
1020 ItemChange, p_playlist );
1022 val.i_int = p_item->input.i_id;
1023 /* unlock the playlist to set the var...mmm */
1024 vlc_mutex_unlock( &p_playlist->object_lock);
1025 var_Set( p_playlist, "playlist-current", val);
1026 vlc_mutex_lock( &p_playlist->object_lock);
1032 /* Forward item change from input */
1033 static int ItemChange( vlc_object_t *p_obj, const char *psz_var,
1034 vlc_value_t oldval, vlc_value_t newval, void *param )
1036 playlist_t *p_playlist = (playlist_t *)param;
1039 //p_playlist->b_need_update = VLC_TRUE;
1040 var_SetInteger( p_playlist, "item-change", newval.i_int );
1043 /* FIXME: Make that automatic */
1044 playlist_ViewUpdate( p_playlist, VIEW_S_AUTHOR );