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 #undef PLAYLIST_PROFILE
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 vlc_mutex_init( p_playlist, &p_playlist->gc_lock );
111 p_playlist->i_last_id = 0;
112 p_playlist->b_go_next = VLC_TRUE;
113 p_playlist->p_input = NULL;
115 p_playlist->request_date = 0;
117 p_playlist->i_views = 0;
118 p_playlist->pp_views = NULL;
120 p_playlist->i_index = -1;
121 p_playlist->i_size = 0;
122 p_playlist->pp_items = NULL;
123 p_playlist->i_all_size = 0;
124 p_playlist->pp_all_items = 0;
126 playlist_ViewInsert( p_playlist, VIEW_CATEGORY, TITLE_CATEGORY );
127 playlist_ViewInsert( p_playlist, VIEW_ALL, TITLE_ALL );
129 p_view = playlist_ViewFind( p_playlist, VIEW_CATEGORY );
131 p_playlist->p_general =
132 playlist_NodeCreate( p_playlist, VIEW_CATEGORY,
133 _( "General" ), p_view->p_root );
134 p_playlist->p_general->i_flags |= PLAYLIST_RO_FLAG;
136 /* Set startup status
137 * We set to simple view on startup for interfaces that don't do
139 p_view = playlist_ViewFind( p_playlist, VIEW_CATEGORY );
140 p_playlist->status.i_view = VIEW_CATEGORY;
141 p_playlist->status.p_item = NULL;
142 p_playlist->status.p_node = p_view->p_root;
143 p_playlist->request.b_request = VLC_FALSE;
144 p_playlist->status.i_status = PLAYLIST_STOPPED;
147 p_playlist->i_sort = SORT_ID;
148 p_playlist->i_order = ORDER_NORMAL;
150 /* Finally, launch the thread ! */
151 if( vlc_thread_create( p_playlist, "playlist", RunThread,
152 VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
154 msg_Err( p_playlist, "cannot spawn playlist thread" );
155 vlc_object_destroy( p_playlist );
159 /* Preparsing stuff */
160 p_playlist->p_preparse = vlc_object_create( p_playlist,
161 sizeof( playlist_preparse_t ) );
162 if( !p_playlist->p_preparse )
164 msg_Err( p_playlist, "unable to create preparser" );
165 vlc_object_destroy( p_playlist );
169 p_playlist->p_preparse->i_waiting = 0;
170 p_playlist->p_preparse->pp_waiting = NULL;
172 vlc_object_attach( p_playlist->p_preparse, p_playlist );
173 if( vlc_thread_create( p_playlist->p_preparse, "preparser",
174 RunPreparse, VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
176 msg_Err( p_playlist, "cannot spawn preparse thread" );
177 vlc_object_detach( p_playlist->p_preparse );
178 vlc_object_destroy( p_playlist->p_preparse );
182 /* The object has been initialized, now attach it */
183 vlc_object_attach( p_playlist, p_parent );
189 * Destroy the playlist.
191 * Delete all items in the playlist and free the playlist structure.
192 * \param p_playlist the playlist structure to destroy
193 * \return VLC_SUCCESS or an error
195 int playlist_Destroy( playlist_t * p_playlist )
198 p_playlist->b_die = 1;
200 while( p_playlist->i_sds )
202 playlist_ServicesDiscoveryRemove( p_playlist,
203 p_playlist->pp_sds[0]->psz_module );
206 vlc_thread_join( p_playlist->p_preparse );
207 vlc_thread_join( p_playlist );
209 vlc_object_detach( p_playlist->p_preparse );
211 var_Destroy( p_playlist, "intf-change" );
212 var_Destroy( p_playlist, "item-change" );
213 var_Destroy( p_playlist, "playlist-current" );
214 var_Destroy( p_playlist, "intf-popmenu" );
215 var_Destroy( p_playlist, "intf-show" );
216 var_Destroy( p_playlist, "play-and-stop" );
217 var_Destroy( p_playlist, "random" );
218 var_Destroy( p_playlist, "repeat" );
219 var_Destroy( p_playlist, "loop" );
221 playlist_Clear( p_playlist );
223 for( i = p_playlist->i_views - 1; i >= 0 ; i-- )
225 playlist_view_t *p_view = p_playlist->pp_views[i];
226 if( p_view->psz_name )
227 free( p_view->psz_name );
228 playlist_ItemDelete( p_view->p_root );
229 REMOVE_ELEM( p_playlist->pp_views, p_playlist->i_views, i );
233 vlc_mutex_destroy( &p_playlist->gc_lock );
234 vlc_object_destroy( p_playlist->p_preparse );
235 vlc_object_destroy( p_playlist );
242 * Do a playlist action.
244 * If there is something in the playlist then you can do playlist actions.
246 * Playlist lock must not be taken when calling this function
248 * \param p_playlist the playlist to do the command on
249 * \param i_query the command to do
250 * \param variable number of arguments
251 * \return VLC_SUCCESS or an error
253 int playlist_LockControl( playlist_t * p_playlist, int i_query, ... )
257 va_start( args, i_query );
258 vlc_mutex_lock( &p_playlist->object_lock );
259 i_result = playlist_vaControl( p_playlist, i_query, args );
261 vlc_mutex_unlock( &p_playlist->object_lock );
266 * Do a playlist action.
268 * If there is something in the playlist then you can do playlist actions.
270 * Playlist lock must be taken when calling this function
272 * \param p_playlist the playlist to do the command on
273 * \param i_query the command to do
274 * \param variable number of arguments
275 * \return VLC_SUCCESS or an error
277 int playlist_Control( playlist_t * p_playlist, int i_query, ... )
281 va_start( args, i_query );
282 i_result = playlist_vaControl( p_playlist, i_query, args );
288 int playlist_vaControl( playlist_t * p_playlist, int i_query, va_list args )
290 playlist_view_t *p_view;
293 #ifdef PLAYLIST_PROFILE
294 p_playlist->request_date = mdate();
297 if( p_playlist->i_size <= 0 )
305 p_playlist->status.i_status = PLAYLIST_STOPPED;
306 p_playlist->request.b_request = VLC_TRUE;
309 case PLAYLIST_ITEMPLAY:
310 p_playlist->status.i_status = PLAYLIST_RUNNING;
311 p_playlist->request.i_skip = 0;
312 p_playlist->request.b_request = VLC_TRUE;
313 p_playlist->request.p_item = (playlist_item_t *)va_arg( args,
315 p_playlist->request.i_view = p_playlist->status.i_view;
316 p_view = playlist_ViewFind( p_playlist, p_playlist->status.i_view );
319 p_playlist->request.p_node = p_view->p_root;
323 p_playlist->request.p_node = NULL;
327 case PLAYLIST_VIEWPLAY:
328 p_playlist->status.i_status = PLAYLIST_RUNNING;
329 p_playlist->request.i_skip = 0;
330 p_playlist->request.b_request = VLC_TRUE;
331 p_playlist->request.i_view = (int)va_arg( args,int );
332 p_playlist->request.p_node = (playlist_item_t *)va_arg( args,
334 p_playlist->request.p_item = (playlist_item_t *)va_arg( args,
337 /* If we select a node, play only it.
338 * If we select an item, continue */
339 if( p_playlist->request.p_item == NULL ||
340 ! p_playlist->request.p_node->i_flags & PLAYLIST_SKIP_FLAG )
342 p_playlist->b_go_next = VLC_FALSE;
346 p_playlist->b_go_next = VLC_TRUE;
351 p_playlist->status.i_status = PLAYLIST_RUNNING;
353 if( p_playlist->p_input )
355 val.i_int = PLAYING_S;
356 var_Set( p_playlist->p_input, "state", val );
360 /* FIXME : needed ? */
361 p_playlist->request.b_request = VLC_TRUE;
362 p_playlist->request.i_view = p_playlist->status.i_view;
363 p_playlist->request.p_node = p_playlist->status.p_node;
364 p_playlist->request.p_item = p_playlist->status.p_item;
365 p_playlist->request.i_skip = 0;
366 p_playlist->request.i_goto = -1;
369 case PLAYLIST_AUTOPLAY:
370 p_playlist->status.i_status = PLAYLIST_RUNNING;
372 p_playlist->request.b_request = VLC_FALSE;
377 if( p_playlist->p_input )
378 var_Get( p_playlist->p_input, "state", &val );
380 if( val.i_int == PAUSE_S )
382 p_playlist->status.i_status = PLAYLIST_RUNNING;
383 if( p_playlist->p_input )
385 val.i_int = PLAYING_S;
386 var_Set( p_playlist->p_input, "state", val );
391 p_playlist->status.i_status = PLAYLIST_PAUSED;
392 if( p_playlist->p_input )
395 var_Set( p_playlist->p_input, "state", val );
401 if( p_playlist->status.i_view > -1 )
403 p_playlist->request.i_view = p_playlist->status.i_view;
404 p_playlist->request.p_node = p_playlist->status.p_node;
405 p_playlist->request.p_item = p_playlist->status.p_item;
407 p_playlist->request.i_skip = (int) va_arg( args, int );
408 p_playlist->request.b_request = VLC_TRUE;
412 p_playlist->status.i_status = PLAYLIST_RUNNING;
413 p_playlist->request.p_node = NULL;
414 p_playlist->request.p_item = NULL;
415 p_playlist->request.i_view = -1;
416 p_playlist->request.i_goto = (int) va_arg( args, int );
417 p_playlist->request.b_request = VLC_TRUE;
421 msg_Err( p_playlist, "unimplemented playlist query" );
429 int playlist_PreparseEnqueue( playlist_t *p_playlist,
430 input_item_t *p_item )
432 vlc_mutex_lock( &p_playlist->p_preparse->object_lock );
433 INSERT_ELEM( p_playlist->p_preparse->pp_waiting,
434 p_playlist->p_preparse->i_waiting,
435 p_playlist->p_preparse->i_waiting,
437 vlc_mutex_unlock( &p_playlist->p_preparse->object_lock );
442 /* Destroy remaining objects */
443 static mtime_t ObjectGarbageCollector( playlist_t *p_playlist, int i_type,
444 mtime_t destroy_date )
448 if( destroy_date > mdate() ) return destroy_date;
450 if( destroy_date == 0 )
452 /* give a little time */
453 return mdate() + I64C(1000000);
457 vlc_mutex_lock( &p_playlist->gc_lock );
458 while( ( p_obj = vlc_object_find( p_playlist, i_type, FIND_CHILD ) ) )
460 if( p_obj->p_parent != (vlc_object_t*)p_playlist )
462 /* only first child (ie unused) */
463 vlc_object_release( p_obj );
466 if( i_type == VLC_OBJECT_VOUT )
468 msg_Dbg( p_playlist, "garbage collector destroying 1 vout" );
469 vlc_object_detach( p_obj );
470 vlc_object_release( p_obj );
471 vout_Destroy( (vout_thread_t *)p_obj );
473 else if( i_type == VLC_OBJECT_SOUT )
475 vlc_object_release( p_obj );
476 sout_DeleteInstance( (sout_instance_t*)p_obj );
479 vlc_mutex_unlock( &p_playlist->gc_lock );
484 /*****************************************************************************
485 * RunThread: main playlist thread
486 *****************************************************************************/
487 static void RunThread ( playlist_t *p_playlist )
490 playlist_item_t *p_item = NULL;
492 mtime_t i_vout_destroyed_date = 0;
493 mtime_t i_sout_destroyed_date = 0;
495 playlist_item_t *p_autodelete_item = NULL;
497 /* Tell above that we're ready */
498 vlc_thread_ready( p_playlist );
500 while( !p_playlist->b_die )
502 vlc_mutex_lock( &p_playlist->object_lock );
504 /* First, check if we have something to do */
505 /* FIXME : this can be called several times */
506 if( p_playlist->request.b_request )
508 #ifdef PLAYLIST_PROFILE
509 msg_Dbg(p_playlist, "beginning processing of request, "
510 I64Fi" us ", mdate() - p_playlist->request_date );
512 /* Stop the existing input */
513 if( p_playlist->p_input )
515 input_StopThread( p_playlist->p_input );
517 /* The code below will start the next input for us */
518 if( p_playlist->status.i_status == PLAYLIST_STOPPED )
520 p_playlist->request.b_request = VLC_FALSE;
524 /* If there is an input, check that it doesn't need to die. */
525 if( p_playlist->p_input )
527 /* This input is dead. Remove it ! */
528 if( p_playlist->p_input->b_dead )
530 input_thread_t *p_input;
532 p_input = p_playlist->p_input;
533 p_playlist->p_input = NULL;
535 /* Release the playlist lock, because we may get stuck
536 * in input_DestroyThread() for some time. */
537 vlc_mutex_unlock( &p_playlist->object_lock );
540 input_DestroyThread( p_input );
542 /* Unlink current input
543 * (_after_ input_DestroyThread for vout garbage collector) */
544 vlc_object_detach( p_input );
547 vlc_object_destroy( p_input );
549 i_vout_destroyed_date = 0;
550 i_sout_destroyed_date = 0;
552 if( p_playlist->status.p_item->i_flags
553 & PLAYLIST_REMOVE_FLAG )
555 playlist_ItemDelete( p_item );
556 p_playlist->status.p_item = NULL;
561 /* This input is dying, let him do */
562 else if( p_playlist->p_input->b_die )
566 /* This input has finished, ask him to die ! */
567 else if( p_playlist->p_input->b_error
568 || p_playlist->p_input->b_eof )
570 /* TODO FIXME XXX TODO FIXME XXX */
571 /* Check for autodeletion */
573 if( p_playlist->status.p_item->i_flags & PLAYLIST_DEL_FLAG )
575 p_autodelete_item = p_playlist->status.p_item;
577 input_StopThread( p_playlist->p_input );
578 /* Select the next playlist item */
579 vlc_mutex_unlock( &p_playlist->object_lock );
582 else if( p_playlist->p_input->i_state != INIT_S )
584 vlc_mutex_unlock( &p_playlist->object_lock );
585 i_vout_destroyed_date =
586 ObjectGarbageCollector( p_playlist, VLC_OBJECT_VOUT,
587 i_vout_destroyed_date );
588 i_sout_destroyed_date =
589 ObjectGarbageCollector( p_playlist, VLC_OBJECT_SOUT,
590 i_sout_destroyed_date );
591 vlc_mutex_lock( &p_playlist->object_lock );
594 else if( p_playlist->status.i_status != PLAYLIST_STOPPED )
596 /* Start another input.
597 * Get the next item to play */
598 p_item = NextItem( p_playlist );
604 if( p_autodelete_item )
606 playlist_Delete( p_playlist,
607 p_autodelete_item->input.i_id );
608 p_autodelete_item = NULL;
610 p_playlist->status.i_status = PLAYLIST_STOPPED;
611 vlc_mutex_unlock( &p_playlist->object_lock );
615 PlayItem( p_playlist, p_item );
617 if( p_autodelete_item )
619 playlist_Delete( p_playlist, p_autodelete_item->input.i_id );
620 p_autodelete_item = NULL;
623 else if( p_playlist->status.i_status == PLAYLIST_STOPPED )
625 /* Collect garbage */
626 vlc_mutex_unlock( &p_playlist->object_lock );
627 i_sout_destroyed_date =
628 ObjectGarbageCollector( p_playlist, VLC_OBJECT_SOUT, mdate() );
629 i_vout_destroyed_date =
630 ObjectGarbageCollector( p_playlist, VLC_OBJECT_VOUT, mdate() );
631 vlc_mutex_lock( &p_playlist->object_lock );
633 vlc_mutex_unlock( &p_playlist->object_lock );
635 msleep( INTF_IDLE_SLEEP / 2 );
637 /* Stop sleeping earlier if we have work */
638 /* TODO : statistics about this */
639 if ( p_playlist->request.b_request &&
640 p_playlist->status.i_status == PLAYLIST_RUNNING )
645 msleep( INTF_IDLE_SLEEP / 2 );
650 /* If there is an input, kill it */
653 vlc_mutex_lock( &p_playlist->object_lock );
655 if( p_playlist->p_input == NULL )
657 vlc_mutex_unlock( &p_playlist->object_lock );
661 if( p_playlist->p_input->b_dead )
663 input_thread_t *p_input;
665 /* Unlink current input */
666 p_input = p_playlist->p_input;
667 p_playlist->p_input = NULL;
668 vlc_mutex_unlock( &p_playlist->object_lock );
671 input_DestroyThread( p_input );
672 /* Unlink current input (_after_ input_DestroyThread for vout
673 * garbage collector)*/
674 vlc_object_detach( p_input );
677 vlc_object_destroy( p_input );
680 else if( p_playlist->p_input->b_die )
682 /* This input is dying, leave him alone */
685 else if( p_playlist->p_input->b_error || p_playlist->p_input->b_eof )
687 input_StopThread( p_playlist->p_input );
688 vlc_mutex_unlock( &p_playlist->object_lock );
693 p_playlist->p_input->b_eof = 1;
696 vlc_mutex_unlock( &p_playlist->object_lock );
698 msleep( INTF_IDLE_SLEEP );
701 /* close all remaining sout */
702 while( ( p_obj = vlc_object_find( p_playlist,
703 VLC_OBJECT_SOUT, FIND_CHILD ) ) )
705 vlc_object_release( p_obj );
706 sout_DeleteInstance( (sout_instance_t*)p_obj );
709 /* close all remaining vout */
710 while( ( p_obj = vlc_object_find( p_playlist,
711 VLC_OBJECT_VOUT, FIND_CHILD ) ) )
713 vlc_object_detach( p_obj );
714 vlc_object_release( p_obj );
715 vout_Destroy( (vout_thread_t *)p_obj );
719 /* Queue for items to preparse */
720 static void RunPreparse ( playlist_preparse_t *p_obj )
722 playlist_t *p_playlist = (playlist_t *)p_obj->p_parent;
725 /* Tell above that we're ready */
726 vlc_thread_ready( p_obj );
728 while( !p_playlist->b_die )
730 vlc_mutex_lock( &p_obj->object_lock );
732 if( p_obj->i_waiting > 0 )
734 input_item_t *p_current = p_obj->pp_waiting[0];
735 REMOVE_ELEM( p_obj->pp_waiting, p_obj->i_waiting, 0 );
736 vlc_mutex_unlock( &p_obj->object_lock );
737 input_Preparse( p_playlist, p_current );
738 var_SetInteger( p_playlist, "item-change", p_current->i_id );
739 vlc_mutex_lock( &p_obj->object_lock );
741 b_sleep = ( p_obj->i_waiting == 0 );
743 vlc_mutex_unlock( &p_obj->object_lock );
745 if( p_obj->i_waiting == 0 )
747 msleep( INTF_IDLE_SLEEP );
752 /*****************************************************************************
754 *****************************************************************************
755 * This function calculates the next playlist item, depending
756 * on the playlist course mode (forward, backward, random, view,...).
757 *****************************************************************************/
758 static playlist_item_t * NextItem( playlist_t *p_playlist )
760 playlist_item_t *p_new = NULL;
761 int i_skip,i_goto,i, i_new, i_count ;
762 playlist_view_t *p_view;
764 vlc_bool_t b_loop = var_GetBool( p_playlist, "loop" );
765 vlc_bool_t b_random = var_GetBool( p_playlist, "random" );
766 vlc_bool_t b_repeat = var_GetBool( p_playlist, "repeat" );
767 vlc_bool_t b_playstop = var_GetBool( p_playlist, "play-and-stop" );
769 #ifdef PLAYLIST_PROFILE
770 /* Calculate time needed */
771 int64_t start = mdate();
774 /* Handle quickly a few special cases */
776 /* No items to play */
777 if( p_playlist->i_size == 0 )
779 msg_Info( p_playlist, "playlist is empty" );
782 /* Nothing requested */
783 if( !p_playlist->request.b_request && p_playlist->status.p_item == NULL )
785 msg_Dbg( p_playlist,"nothing requested, starting" );
788 /* Repeat and play/stop */
789 if( !p_playlist->request.b_request && b_repeat == VLC_TRUE )
791 msg_Dbg( p_playlist,"repeating item" );
792 return p_playlist->status.p_item;
795 if( !p_playlist->request.b_request && b_playstop == VLC_TRUE )
797 msg_Dbg( p_playlist,"stopping (play and stop)");
801 if( !p_playlist->request.b_request && p_playlist->status.p_item &&
802 !( p_playlist->status.p_item->i_flags & PLAYLIST_SKIP_FLAG ) )
804 msg_Dbg( p_playlist, "no-skip mode, stopping") ;
808 /* TODO: improve this (only use current node) */
809 /* TODO: use the "shuffled view" internally ? */
810 /* Random case. This is an exception: if request, but request is skip +- 1
811 * we don't go to next item but select a new random one. */
812 if( b_random && (!p_playlist->request.b_request ||
813 p_playlist->request.i_skip == 1 || p_playlist->request.i_skip == -1 ) )
815 srand( (unsigned int)mdate() );
817 for( i_count = 0; i_count < p_playlist->i_size - 1 ; i_count ++ )
820 (int)((float)p_playlist->i_size * rand() / (RAND_MAX+1.0));
821 /* Check if the item has not already been played */
822 if( p_playlist->pp_items[i_new]->i_nb_played == 0 )
825 if( i_count == p_playlist->i_size )
827 /* The whole playlist has been played: reset the counters */
830 p_playlist->pp_items[--i_count]->i_nb_played = 0;
837 p_playlist->request.i_skip = 0;
838 p_playlist->request.b_request = VLC_FALSE;
839 return p_playlist->pp_items[i_new];
842 /* Start the real work */
843 if( p_playlist->request.b_request )
845 #ifdef PLAYLIST_DEBUG
846 msg_Dbg( p_playlist,"processing request" );
848 /* We are not playing from a view */
849 if( p_playlist->request.i_view == -1 )
851 #ifdef PLAYLIST_DEBUG
852 msg_Dbg( p_playlist, "non-view mode request");
854 /* Directly select the item, just like now */
855 i_skip = p_playlist->request.i_skip;
856 i_goto = p_playlist->request.i_goto;
858 if( p_playlist->i_index == -1 ) p_playlist->i_index = 0;
859 p_new = p_playlist->pp_items[p_playlist->i_index];
861 if( i_goto >= 0 && i_goto < p_playlist->i_size )
863 p_playlist->i_index = i_goto;
864 p_new = p_playlist->pp_items[p_playlist->i_index];
865 p_playlist->request.i_goto = -1;
870 if( p_playlist->i_index + i_skip < p_playlist->i_size &&
871 p_playlist->i_index + i_skip >= 0 )
873 p_playlist->i_index += i_skip;
874 p_new = p_playlist->pp_items[p_playlist->i_index];
876 p_playlist->request.i_skip = 0;
881 #ifdef PLAYLIST_DEBUG
882 msg_Dbg( p_playlist, "view mode request" );
884 p_new = p_playlist->request.p_item;
885 i_skip = p_playlist->request.i_skip;
887 /* If we are asked for a node, take its first item */
888 if( p_playlist->request.p_item == NULL && i_skip == 0 )
893 p_view = playlist_ViewFind( p_playlist,p_playlist->request.i_view );
894 p_playlist->status.p_node = p_playlist->request.p_node;
895 p_playlist->status.i_view = p_playlist->request.i_view;
898 msg_Err( p_playlist, "p_view is NULL and should not! (requested view is %i", p_playlist->request.i_view );
900 else if( i_skip > 0 )
902 for( i = i_skip; i > 0 ; i-- )
904 p_new = playlist_FindNextFromParent( p_playlist,
905 p_playlist->request.i_view,
907 p_playlist->request.p_node,
913 #ifdef PLAYLIST_DEBUG
914 msg_Dbg( p_playlist, "looping" );
916 p_new = playlist_FindNextFromParent( p_playlist,
917 p_playlist->request.i_view,
919 p_playlist->request.p_node,
921 if( p_new == NULL ) break;
930 else if( i_skip < 0 )
932 for( i = i_skip; i < 0 ; i++ )
934 p_new = playlist_FindPrevFromParent( p_playlist,
935 p_playlist->request.i_view,
937 p_playlist->request.p_node,
939 if( p_new == NULL ) break;
944 /* Clear the request */
945 p_playlist->request.b_request = VLC_FALSE;
947 /* "Automatic" item change ( next ) */
950 p_playlist->request_date = 0;
952 if( p_playlist->status.i_view == -1 )
954 #ifdef PLAYLIST_DEBUG
955 msg_Dbg( p_playlist,"no request - old mode" );
957 if( p_playlist->i_index + 1 < p_playlist->i_size )
959 p_playlist->i_index++;
960 p_new = p_playlist->pp_items[p_playlist->i_index];
961 if( !( p_new->i_flags & PLAYLIST_SKIP_FLAG ) )
968 if( b_loop && p_playlist->i_size > 0)
970 p_playlist->i_index = 0;
971 p_new = p_playlist->pp_items[0];
977 /* We are playing with a view */
980 #ifdef PLAYLIST_DEBUG
981 msg_Dbg( p_playlist,"no request - from a view" );
983 playlist_view_t *p_view =
984 playlist_ViewFind( p_playlist,
985 p_playlist->status.i_view );
988 msg_Err( p_playlist, "p_view is NULL and should not! (FIXME)" );
992 p_new = playlist_FindNextFromParent( p_playlist,
993 p_playlist->status.i_view,
995 p_playlist->status.p_node,
996 p_playlist->status.p_item );
997 if( p_new == NULL && b_loop )
999 #ifdef PLAYLIST_DEBUG
1000 msg_Dbg( p_playlist, "looping" );
1002 p_new = playlist_FindNextFromParent( p_playlist,
1003 p_playlist->status.i_view,
1005 p_playlist->status.p_node,
1008 if( p_new != NULL && !(p_new->i_flags & PLAYLIST_SKIP_FLAG) )
1015 if( p_playlist->i_index >= 0 && p_new != NULL &&
1016 p_playlist->pp_items[p_playlist->i_index] != p_new )
1018 p_playlist->i_index = playlist_GetPositionById( p_playlist,
1019 p_new->input.i_id );
1022 #ifdef PLAYLIST_PROFILE
1023 msg_Dbg(p_playlist,"next item found in "I64Fi " us", mdate()-start );
1028 msg_Info( p_playlist, "nothing to play" );
1033 /*****************************************************************************
1034 * PlayItem: start the input thread for an item
1035 ****************************************************************************/
1036 static int PlayItem( playlist_t *p_playlist, playlist_item_t *p_item )
1040 msg_Dbg( p_playlist, "creating new input thread" );
1042 p_item->i_nb_played++;
1043 p_playlist->status.p_item = p_item;
1045 p_playlist->i_index = playlist_GetPositionById( p_playlist,
1046 p_item->input.i_id );
1048 #ifdef PLAYLIST_PROFILE
1049 if( p_playlist->request_date != 0 )
1051 msg_Dbg( p_playlist, "request processed after "I64Fi " us",
1052 mdate() - p_playlist->request_date );
1056 p_playlist->p_input = input_CreateThread( p_playlist, &p_item->input );
1058 var_AddCallback( p_playlist->p_input, "item-change",
1059 ItemChange, p_playlist );
1061 val.i_int = p_item->input.i_id;
1062 /* unlock the playlist to set the var...mmm */
1063 vlc_mutex_unlock( &p_playlist->object_lock);
1064 var_Set( p_playlist, "playlist-current", val);
1065 vlc_mutex_lock( &p_playlist->object_lock);
1071 /* Forward item change from input */
1072 static int ItemChange( vlc_object_t *p_obj, const char *psz_var,
1073 vlc_value_t oldval, vlc_value_t newval, void *param )
1075 playlist_t *p_playlist = (playlist_t *)param;
1077 //p_playlist->b_need_update = VLC_TRUE;
1078 var_SetInteger( p_playlist, "item-change", newval.i_int );
1081 /* FIXME: Make that automatic */
1082 // playlist_ViewUpdate( p_playlist, VIEW_S_AUTHOR );