1 /*****************************************************************************
2 * playlist.c : Playlist management functions
3 *****************************************************************************
4 * Copyright (C) 1999-2004 the VideoLAN team
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 );
55 void playlist_PreparseEnqueueItemSub( playlist_t *, playlist_item_t * );
57 playlist_item_t *playlist_RecursiveFindLast(playlist_t *p_playlist,
58 playlist_item_t *p_node );
60 /*****************************************************************************
61 * Helper Function for NextItem
62 *****************************************************************************/
64 playlist_item_t *playlist_RecursiveFindLast(playlist_t *p_playlist,
65 playlist_item_t *p_node )
68 playlist_item_t *p_item;
69 for ( i = p_node->i_children - 1; i >= 0; i-- )
71 if( p_node->pp_children[i]->i_children == -1 )
72 return p_node->pp_children[i];
73 else if(p_node->pp_children[i]->i_children > 0)
75 p_item = playlist_RecursiveFindLast( p_playlist,
76 p_node->pp_children[i] );
90 * Create a playlist structure.
91 * \param p_parent the vlc object that is to be the parent of this playlist
92 * \return a pointer to the created playlist, or NULL on error
94 playlist_t * __playlist_Create ( vlc_object_t *p_parent )
96 playlist_t *p_playlist;
97 playlist_view_t *p_view;
100 /* Allocate structure */
101 p_playlist = vlc_object_create( p_parent, VLC_OBJECT_PLAYLIST );
104 msg_Err( p_parent, "out of memory" );
108 /* These variables control updates */
109 var_Create( p_playlist, "intf-change", VLC_VAR_BOOL );
110 val.b_bool = VLC_TRUE;
111 var_Set( p_playlist, "intf-change", val );
113 var_Create( p_playlist, "item-change", VLC_VAR_INTEGER );
115 var_Set( p_playlist, "item-change", val );
117 var_Create( p_playlist, "item-deleted", VLC_VAR_INTEGER );
119 var_Set( p_playlist, "item-deleted", val );
121 var_Create( p_playlist, "item-append", VLC_VAR_ADDRESS );
123 var_Create( p_playlist, "playlist-current", VLC_VAR_INTEGER );
125 var_Set( p_playlist, "playlist-current", val );
127 var_Create( p_playlist, "intf-popupmenu", VLC_VAR_BOOL );
129 var_Create( p_playlist, "intf-show", VLC_VAR_BOOL );
130 val.b_bool = VLC_TRUE;
131 var_Set( p_playlist, "intf-show", val );
134 /* Variables to control playback */
135 var_CreateGetBool( p_playlist, "play-and-stop" );
136 var_CreateGetBool( p_playlist, "random" );
137 var_CreateGetBool( p_playlist, "repeat" );
138 var_CreateGetBool( p_playlist, "loop" );
140 /* Initialise data structures */
141 vlc_mutex_init( p_playlist, &p_playlist->gc_lock );
142 p_playlist->i_last_id = 0;
143 p_playlist->b_go_next = VLC_TRUE;
144 p_playlist->p_input = NULL;
146 p_playlist->request_date = 0;
148 p_playlist->i_views = 0;
149 p_playlist->pp_views = NULL;
151 p_playlist->i_index = -1;
152 p_playlist->i_size = 0;
153 p_playlist->pp_items = NULL;
154 p_playlist->i_all_size = 0;
155 p_playlist->pp_all_items = 0;
157 playlist_ViewInsert( p_playlist, VIEW_CATEGORY, TITLE_CATEGORY );
158 playlist_ViewInsert( p_playlist, VIEW_ALL, TITLE_ALL );
160 p_view = playlist_ViewFind( p_playlist, VIEW_CATEGORY );
162 p_playlist->p_general =
163 playlist_NodeCreate( p_playlist, VIEW_CATEGORY,
164 _( "General" ), p_view->p_root );
165 p_playlist->p_general->i_flags |= PLAYLIST_RO_FLAG;
167 /* Set startup status
168 * We set to simple view on startup for interfaces that don't do
170 p_view = playlist_ViewFind( p_playlist, VIEW_CATEGORY );
171 p_playlist->status.i_view = VIEW_CATEGORY;
172 p_playlist->status.p_item = NULL;
173 p_playlist->status.p_node = p_view->p_root;
174 p_playlist->request.b_request = VLC_FALSE;
175 p_playlist->status.i_status = PLAYLIST_STOPPED;
178 p_playlist->i_sort = SORT_ID;
179 p_playlist->i_order = ORDER_NORMAL;
181 /* Finally, launch the thread ! */
182 if( vlc_thread_create( p_playlist, "playlist", RunThread,
183 VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
185 msg_Err( p_playlist, "cannot spawn playlist thread" );
186 vlc_object_destroy( p_playlist );
190 /* Preparsing stuff */
191 p_playlist->p_preparse = vlc_object_create( p_playlist,
192 sizeof( playlist_preparse_t ) );
193 if( !p_playlist->p_preparse )
195 msg_Err( p_playlist, "unable to create preparser" );
196 vlc_object_destroy( p_playlist );
200 p_playlist->p_preparse->i_waiting = 0;
201 p_playlist->p_preparse->pp_waiting = NULL;
203 vlc_object_attach( p_playlist->p_preparse, p_playlist );
204 if( vlc_thread_create( p_playlist->p_preparse, "preparser",
205 RunPreparse, VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
207 msg_Err( p_playlist, "cannot spawn preparse thread" );
208 vlc_object_detach( p_playlist->p_preparse );
209 vlc_object_destroy( p_playlist->p_preparse );
213 /* The object has been initialized, now attach it */
214 vlc_object_attach( p_playlist, p_parent );
220 * Destroy the playlist.
222 * Delete all items in the playlist and free the playlist structure.
223 * \param p_playlist the playlist structure to destroy
224 * \return VLC_SUCCESS or an error
226 int playlist_Destroy( playlist_t * p_playlist )
229 p_playlist->b_die = 1;
231 while( p_playlist->i_sds )
233 playlist_ServicesDiscoveryRemove( p_playlist,
234 p_playlist->pp_sds[0]->psz_module );
237 vlc_thread_join( p_playlist->p_preparse );
238 vlc_thread_join( p_playlist );
240 vlc_object_detach( p_playlist->p_preparse );
242 var_Destroy( p_playlist, "intf-change" );
243 var_Destroy( p_playlist, "item-change" );
244 var_Destroy( p_playlist, "playlist-current" );
245 var_Destroy( p_playlist, "intf-popmenu" );
246 var_Destroy( p_playlist, "intf-show" );
247 var_Destroy( p_playlist, "play-and-stop" );
248 var_Destroy( p_playlist, "random" );
249 var_Destroy( p_playlist, "repeat" );
250 var_Destroy( p_playlist, "loop" );
252 playlist_Clear( p_playlist );
254 for( i = p_playlist->i_views - 1; i >= 0 ; i-- )
256 playlist_view_t *p_view = p_playlist->pp_views[i];
257 if( p_view->psz_name )
258 free( p_view->psz_name );
259 playlist_ItemDelete( p_view->p_root );
260 REMOVE_ELEM( p_playlist->pp_views, p_playlist->i_views, i );
264 vlc_mutex_destroy( &p_playlist->gc_lock );
265 vlc_object_destroy( p_playlist->p_preparse );
266 vlc_object_destroy( p_playlist );
273 * Do a playlist action.
275 * If there is something in the playlist then you can do playlist actions.
277 * Playlist lock must not be taken when calling this function
279 * \param p_playlist the playlist to do the command on
280 * \param i_query the command to do
281 * \param variable number of arguments
282 * \return VLC_SUCCESS or an error
284 int playlist_LockControl( playlist_t * p_playlist, int i_query, ... )
288 va_start( args, i_query );
289 vlc_mutex_lock( &p_playlist->object_lock );
290 i_result = playlist_vaControl( p_playlist, i_query, args );
292 vlc_mutex_unlock( &p_playlist->object_lock );
297 * Do a playlist action.
299 * If there is something in the playlist then you can do playlist actions.
301 * Playlist lock must be taken when calling this function
303 * \param p_playlist the playlist to do the command on
304 * \param i_query the command to do
305 * \param variable number of arguments
306 * \return VLC_SUCCESS or an error
308 int playlist_Control( playlist_t * p_playlist, int i_query, ... )
312 va_start( args, i_query );
313 i_result = playlist_vaControl( p_playlist, i_query, args );
319 int playlist_vaControl( playlist_t * p_playlist, int i_query, va_list args )
321 playlist_view_t *p_view;
322 playlist_item_t *p_item, *p_node;
326 #ifdef PLAYLIST_PROFILE
327 p_playlist->request_date = mdate();
330 if( p_playlist->i_size <= 0 )
338 p_playlist->status.i_status = PLAYLIST_STOPPED;
339 p_playlist->request.b_request = VLC_TRUE;
340 p_playlist->request.p_item = NULL;
343 case PLAYLIST_ITEMPLAY:
344 p_item = (playlist_item_t *)va_arg( args, playlist_item_t * );
345 if ( p_item == NULL || p_item->input.psz_uri == NULL )
347 p_playlist->status.i_status = PLAYLIST_RUNNING;
348 p_playlist->request.i_skip = 0;
349 p_playlist->request.b_request = VLC_TRUE;
350 p_playlist->request.p_item = p_item;
351 p_playlist->request.i_view = p_playlist->status.i_view;
352 p_view = playlist_ViewFind( p_playlist, p_playlist->status.i_view );
355 p_playlist->request.p_node = p_view->p_root;
359 p_playlist->request.p_node = NULL;
363 case PLAYLIST_VIEWPLAY:
364 i_view = (int)va_arg( args,int );
365 p_node = (playlist_item_t *)va_arg( args, playlist_item_t * );
366 p_item = (playlist_item_t *)va_arg( args, playlist_item_t * );
367 if ( p_node == NULL || (p_item != NULL && p_item->input.psz_uri
370 p_playlist->status.i_status = PLAYLIST_STOPPED;
371 p_playlist->request.b_request = VLC_TRUE;
374 p_playlist->status.i_status = PLAYLIST_RUNNING;
375 p_playlist->request.i_skip = 0;
376 p_playlist->request.b_request = VLC_TRUE;
377 p_playlist->request.i_view = i_view;
378 p_playlist->request.p_node = p_node;
379 p_playlist->request.p_item = p_item;
381 /* If we select a node, play only it.
382 * If we select an item, continue */
383 if( p_playlist->request.p_item == NULL ||
384 ! p_playlist->request.p_node->i_flags & PLAYLIST_SKIP_FLAG )
386 p_playlist->b_go_next = VLC_FALSE;
390 p_playlist->b_go_next = VLC_TRUE;
395 p_playlist->status.i_status = PLAYLIST_RUNNING;
397 if( p_playlist->p_input )
399 val.i_int = PLAYING_S;
400 var_Set( p_playlist->p_input, "state", val );
404 /* FIXME : needed ? */
405 p_playlist->request.b_request = VLC_TRUE;
406 p_playlist->request.i_view = p_playlist->status.i_view;
407 p_playlist->request.p_node = p_playlist->status.p_node;
408 p_playlist->request.p_item = p_playlist->status.p_item = NULL;
409 p_playlist->request.i_skip = 0;
410 p_playlist->request.i_goto = -1;
413 case PLAYLIST_AUTOPLAY:
414 p_playlist->status.i_status = PLAYLIST_RUNNING;
415 p_playlist->status.p_node = p_playlist->p_general;
417 p_playlist->request.b_request = VLC_FALSE;
422 if( p_playlist->p_input )
423 var_Get( p_playlist->p_input, "state", &val );
425 if( val.i_int == PAUSE_S )
427 p_playlist->status.i_status = PLAYLIST_RUNNING;
428 if( p_playlist->p_input )
430 val.i_int = PLAYING_S;
431 var_Set( p_playlist->p_input, "state", val );
436 p_playlist->status.i_status = PLAYLIST_PAUSED;
437 if( p_playlist->p_input )
440 var_Set( p_playlist->p_input, "state", val );
446 if( p_playlist->status.i_view > -1 )
448 p_playlist->request.i_view = p_playlist->status.i_view;
449 p_playlist->request.p_node = p_playlist->status.p_node;
450 p_playlist->request.p_item = p_playlist->status.p_item;
452 p_playlist->request.i_skip = (int) va_arg( args, int );
453 p_playlist->request.b_request = VLC_TRUE;
457 p_playlist->status.i_status = PLAYLIST_RUNNING;
458 p_playlist->request.p_node = NULL;
459 p_playlist->request.p_item = NULL;
460 p_playlist->request.i_view = -1;
461 p_playlist->request.i_goto = (int) va_arg( args, int );
462 p_playlist->request.b_request = VLC_TRUE;
466 msg_Err( p_playlist, "unimplemented playlist query" );
474 int playlist_PreparseEnqueue( playlist_t *p_playlist,
475 input_item_t *p_item )
477 vlc_mutex_lock( &p_playlist->p_preparse->object_lock );
478 INSERT_ELEM( p_playlist->p_preparse->pp_waiting,
479 p_playlist->p_preparse->i_waiting,
480 p_playlist->p_preparse->i_waiting,
482 vlc_mutex_unlock( &p_playlist->p_preparse->object_lock );
486 /* Should only be called if playlist and preparser are locked */
487 void playlist_PreparseEnqueueItemSub( playlist_t *p_playlist,
488 playlist_item_t *p_item )
491 if( p_item->i_children == -1 )
493 INSERT_ELEM( p_playlist->p_preparse->pp_waiting,
494 p_playlist->p_preparse->i_waiting,
495 p_playlist->p_preparse->i_waiting,
500 for( i = 0; i < p_item->i_children; i++)
502 playlist_PreparseEnqueueItemSub( p_playlist,
503 p_item->pp_children[i] );
508 int playlist_PreparseEnqueueItem( playlist_t *p_playlist,
509 playlist_item_t *p_item )
511 vlc_mutex_lock( &p_playlist->object_lock );
512 vlc_mutex_lock( &p_playlist->p_preparse->object_lock );
513 playlist_PreparseEnqueueItemSub( p_playlist, p_item );
514 vlc_mutex_unlock( &p_playlist->p_preparse->object_lock );
515 vlc_mutex_unlock( &p_playlist->object_lock );
520 /* Destroy remaining objects */
521 static mtime_t ObjectGarbageCollector( playlist_t *p_playlist, int i_type,
522 mtime_t destroy_date )
526 if( destroy_date > mdate() ) return destroy_date;
528 if( destroy_date == 0 )
530 /* give a little time */
531 return mdate() + I64C(1000000);
535 vlc_mutex_lock( &p_playlist->gc_lock );
536 while( ( p_obj = vlc_object_find( p_playlist, i_type, FIND_CHILD ) ) )
538 if( p_obj->p_parent != (vlc_object_t*)p_playlist )
540 /* only first child (ie unused) */
541 vlc_object_release( p_obj );
544 if( i_type == VLC_OBJECT_VOUT )
546 msg_Dbg( p_playlist, "garbage collector destroying 1 vout" );
547 vlc_object_detach( p_obj );
548 vlc_object_release( p_obj );
549 vout_Destroy( (vout_thread_t *)p_obj );
551 else if( i_type == VLC_OBJECT_SOUT )
553 vlc_object_release( p_obj );
554 sout_DeleteInstance( (sout_instance_t*)p_obj );
557 vlc_mutex_unlock( &p_playlist->gc_lock );
562 /*****************************************************************************
563 * RunThread: main playlist thread
564 *****************************************************************************/
565 static void RunThread ( playlist_t *p_playlist )
568 playlist_item_t *p_item = NULL;
570 mtime_t i_vout_destroyed_date = 0;
571 mtime_t i_sout_destroyed_date = 0;
573 playlist_item_t *p_autodelete_item = NULL;
575 /* Tell above that we're ready */
576 vlc_thread_ready( p_playlist );
578 while( !p_playlist->b_die )
580 vlc_mutex_lock( &p_playlist->object_lock );
582 /* First, check if we have something to do */
583 /* FIXME : this can be called several times */
584 if( p_playlist->request.b_request )
586 #ifdef PLAYLIST_PROFILE
587 msg_Dbg(p_playlist, "beginning processing of request, "
588 I64Fi" us ", mdate() - p_playlist->request_date );
590 /* Stop the existing input */
591 if( p_playlist->p_input )
593 input_StopThread( p_playlist->p_input );
595 /* The code below will start the next input for us */
596 if( p_playlist->status.i_status == PLAYLIST_STOPPED )
598 p_playlist->request.b_request = VLC_FALSE;
602 /* If there is an input, check that it doesn't need to die. */
603 if( p_playlist->p_input )
605 /* This input is dead. Remove it ! */
606 if( p_playlist->p_input->b_dead )
608 input_thread_t *p_input;
610 p_input = p_playlist->p_input;
611 p_playlist->p_input = NULL;
613 /* Release the playlist lock, because we may get stuck
614 * in input_DestroyThread() for some time. */
615 vlc_mutex_unlock( &p_playlist->object_lock );
618 input_DestroyThread( p_input );
620 /* Unlink current input
621 * (_after_ input_DestroyThread for vout garbage collector) */
622 vlc_object_detach( p_input );
625 vlc_object_destroy( p_input );
627 i_vout_destroyed_date = 0;
628 i_sout_destroyed_date = 0;
630 if( p_playlist->status.p_item->i_flags
631 & PLAYLIST_REMOVE_FLAG )
633 playlist_ItemDelete( p_item );
634 p_playlist->status.p_item = NULL;
639 /* This input is dying, let it do */
640 else if( p_playlist->p_input->b_die )
644 /* This input has finished, ask it to die ! */
645 else if( p_playlist->p_input->b_error
646 || p_playlist->p_input->b_eof )
648 /* TODO FIXME XXX TODO FIXME XXX */
649 /* Check for autodeletion */
651 if( p_playlist->status.p_item->i_flags & PLAYLIST_DEL_FLAG )
653 p_autodelete_item = p_playlist->status.p_item;
655 input_StopThread( p_playlist->p_input );
656 /* Select the next playlist item */
657 vlc_mutex_unlock( &p_playlist->object_lock );
660 else if( p_playlist->p_input->i_state != INIT_S )
662 vlc_mutex_unlock( &p_playlist->object_lock );
663 i_vout_destroyed_date =
664 ObjectGarbageCollector( p_playlist, VLC_OBJECT_VOUT,
665 i_vout_destroyed_date );
666 i_sout_destroyed_date =
667 ObjectGarbageCollector( p_playlist, VLC_OBJECT_SOUT,
668 i_sout_destroyed_date );
669 vlc_mutex_lock( &p_playlist->object_lock );
672 else if( p_playlist->status.i_status != PLAYLIST_STOPPED )
674 /* Start another input.
675 * Get the next item to play */
676 p_item = NextItem( p_playlist );
682 if( p_autodelete_item )
684 playlist_Delete( p_playlist,
685 p_autodelete_item->input.i_id );
686 p_autodelete_item = NULL;
688 p_playlist->status.i_status = PLAYLIST_STOPPED;
689 vlc_mutex_unlock( &p_playlist->object_lock );
693 PlayItem( p_playlist, p_item );
695 if( p_autodelete_item )
697 playlist_Delete( p_playlist, p_autodelete_item->input.i_id );
698 p_autodelete_item = NULL;
701 else if( p_playlist->status.i_status == PLAYLIST_STOPPED )
703 /* Collect garbage */
704 vlc_mutex_unlock( &p_playlist->object_lock );
705 i_sout_destroyed_date =
706 ObjectGarbageCollector( p_playlist, VLC_OBJECT_SOUT, mdate() );
707 i_vout_destroyed_date =
708 ObjectGarbageCollector( p_playlist, VLC_OBJECT_VOUT, mdate() );
709 vlc_mutex_lock( &p_playlist->object_lock );
711 vlc_mutex_unlock( &p_playlist->object_lock );
713 msleep( INTF_IDLE_SLEEP / 2 );
715 /* Stop sleeping earlier if we have work */
716 /* TODO : statistics about this */
717 if ( p_playlist->request.b_request &&
718 p_playlist->status.i_status == PLAYLIST_RUNNING )
723 msleep( INTF_IDLE_SLEEP / 2 );
728 /* If there is an input, kill it */
731 vlc_mutex_lock( &p_playlist->object_lock );
733 if( p_playlist->p_input == NULL )
735 vlc_mutex_unlock( &p_playlist->object_lock );
739 if( p_playlist->p_input->b_dead )
741 input_thread_t *p_input;
743 /* Unlink current input */
744 p_input = p_playlist->p_input;
745 p_playlist->p_input = NULL;
746 vlc_mutex_unlock( &p_playlist->object_lock );
749 input_DestroyThread( p_input );
750 /* Unlink current input (_after_ input_DestroyThread for vout
751 * garbage collector)*/
752 vlc_object_detach( p_input );
755 vlc_object_destroy( p_input );
758 else if( p_playlist->p_input->b_die )
760 /* This input is dying, leave it alone */
763 else if( p_playlist->p_input->b_error || p_playlist->p_input->b_eof )
765 input_StopThread( p_playlist->p_input );
766 vlc_mutex_unlock( &p_playlist->object_lock );
771 p_playlist->p_input->b_eof = 1;
774 vlc_mutex_unlock( &p_playlist->object_lock );
776 msleep( INTF_IDLE_SLEEP );
779 /* close all remaining sout */
780 while( ( p_obj = vlc_object_find( p_playlist,
781 VLC_OBJECT_SOUT, FIND_CHILD ) ) )
783 vlc_object_release( p_obj );
784 sout_DeleteInstance( (sout_instance_t*)p_obj );
787 /* close all remaining vout */
788 while( ( p_obj = vlc_object_find( p_playlist,
789 VLC_OBJECT_VOUT, FIND_CHILD ) ) )
791 vlc_object_detach( p_obj );
792 vlc_object_release( p_obj );
793 vout_Destroy( (vout_thread_t *)p_obj );
797 /* Queue for items to preparse */
798 static void RunPreparse ( playlist_preparse_t *p_obj )
800 playlist_t *p_playlist = (playlist_t *)p_obj->p_parent;
803 /* Tell above that we're ready */
804 vlc_thread_ready( p_obj );
806 while( !p_playlist->b_die )
808 vlc_mutex_lock( &p_obj->object_lock );
810 if( p_obj->i_waiting > 0 )
812 input_item_t *p_current = p_obj->pp_waiting[0];
813 REMOVE_ELEM( p_obj->pp_waiting, p_obj->i_waiting, 0 );
814 vlc_mutex_unlock( &p_obj->object_lock );
815 input_Preparse( p_playlist, p_current );
816 var_SetInteger( p_playlist, "item-change", p_current->i_id );
817 vlc_mutex_lock( &p_obj->object_lock );
819 b_sleep = ( p_obj->i_waiting == 0 );
821 vlc_mutex_unlock( &p_obj->object_lock );
823 if( p_obj->i_waiting == 0 )
825 msleep( INTF_IDLE_SLEEP );
830 /*****************************************************************************
832 *****************************************************************************
833 * This function calculates the next playlist item, depending
834 * on the playlist course mode (forward, backward, random, view,...).
835 *****************************************************************************/
836 static playlist_item_t * NextItem( playlist_t *p_playlist )
838 playlist_item_t *p_new = NULL;
839 int i_skip,i_goto,i, i_new, i_count ;
840 playlist_view_t *p_view;
842 vlc_bool_t b_loop = var_GetBool( p_playlist, "loop" );
843 vlc_bool_t b_random = var_GetBool( p_playlist, "random" );
844 vlc_bool_t b_repeat = var_GetBool( p_playlist, "repeat" );
845 vlc_bool_t b_playstop = var_GetBool( p_playlist, "play-and-stop" );
847 #ifdef PLAYLIST_PROFILE
848 /* Calculate time needed */
849 int64_t start = mdate();
852 /* Handle quickly a few special cases */
854 /* No items to play */
855 if( p_playlist->i_size == 0 )
857 msg_Info( p_playlist, "playlist is empty" );
860 /* Nothing requested */
861 if( !p_playlist->request.b_request && p_playlist->status.p_item == NULL )
863 msg_Dbg( p_playlist,"nothing requested, starting" );
866 /* Repeat and play/stop */
867 if( !p_playlist->request.b_request && b_repeat == VLC_TRUE )
869 msg_Dbg( p_playlist,"repeating item" );
870 return p_playlist->status.p_item;
873 if( !p_playlist->request.b_request && b_playstop == VLC_TRUE )
875 msg_Dbg( p_playlist,"stopping (play and stop)");
879 if( !p_playlist->request.b_request && p_playlist->status.p_item &&
880 !( p_playlist->status.p_item->i_flags & PLAYLIST_SKIP_FLAG ) )
882 msg_Dbg( p_playlist, "no-skip mode, stopping") ;
886 /* TODO: improve this (only use current node) */
887 /* TODO: use the "shuffled view" internally ? */
888 /* Random case. This is an exception: if request, but request is skip +- 1
889 * we don't go to next item but select a new random one. */
891 ( !p_playlist->request.b_request ||
892 ( p_playlist->request.b_request && ( p_playlist->request.p_item == NULL ||
893 p_playlist->request.i_skip == 1 || p_playlist->request.i_skip == -1 ) ) ) )
895 srand( (unsigned int)mdate() );
897 for( i_count = 0; i_count < p_playlist->i_size - 1 ; i_count ++ )
900 (int)((float)p_playlist->i_size * rand() / (RAND_MAX+1.0));
901 /* Check if the item has not already been played */
902 if( p_playlist->pp_items[i_new]->i_nb_played == 0 )
905 if( i_count == p_playlist->i_size )
907 /* The whole playlist has been played: reset the counters */
910 p_playlist->pp_items[--i_count]->i_nb_played = 0;
917 p_playlist->request.i_skip = 0;
918 p_playlist->request.b_request = VLC_FALSE;
919 return p_playlist->pp_items[i_new];
922 /* Start the real work */
923 if( p_playlist->request.b_request )
925 #ifdef PLAYLIST_DEBUG
926 msg_Dbg( p_playlist,"processing request" );
928 /* We are not playing from a view */
929 if( p_playlist->request.i_view == -1 )
931 #ifdef PLAYLIST_DEBUG
932 msg_Dbg( p_playlist, "non-view mode request");
934 /* Directly select the item, just like now */
935 p_new = p_playlist->request.p_item;
936 i_skip = p_playlist->request.i_skip;
937 i_goto = p_playlist->request.i_goto;
939 if( p_playlist->i_index < 0 ) p_playlist->i_index = 0;
941 p_new = p_playlist->pp_items[p_playlist->i_index];
943 if( i_goto >= 0 && i_goto < p_playlist->i_size )
945 p_playlist->i_index = i_goto;
946 p_new = p_playlist->pp_items[p_playlist->i_index];
947 p_playlist->request.i_goto = -1;
952 if( p_playlist->i_index + i_skip < p_playlist->i_size &&
953 p_playlist->i_index + i_skip >= 0 )
955 p_playlist->i_index += i_skip;
956 p_new = p_playlist->pp_items[p_playlist->i_index];
958 p_playlist->request.i_skip = 0;
963 #ifdef PLAYLIST_DEBUG
964 msg_Dbg( p_playlist, "view mode request" );
966 p_new = p_playlist->request.p_item;
967 i_skip = p_playlist->request.i_skip;
969 /* If we are asked for a node, take its first item */
970 if( p_playlist->request.p_item == NULL && i_skip == 0 )
975 p_view = playlist_ViewFind( p_playlist,p_playlist->request.i_view );
976 p_playlist->status.p_node = p_playlist->request.p_node;
977 p_playlist->status.i_view = p_playlist->request.i_view;
980 msg_Err( p_playlist, "p_view is NULL and should not! (requested view is %i", p_playlist->request.i_view );
982 else if( i_skip > 0 )
984 for( i = i_skip; i > 0 ; i-- )
986 p_new = playlist_FindNextFromParent( p_playlist,
987 p_playlist->request.i_view,
989 p_playlist->request.p_node,
993 #ifdef PLAYLIST_DEBUG
994 msg_Dbg( p_playlist, "looping" );
996 p_new = playlist_FindNextFromParent( p_playlist,
997 p_playlist->request.i_view,
1001 if( p_new == NULL ) break;
1005 else if( i_skip < 0 )
1007 for( i = i_skip; i < 0 ; i++ )
1009 p_new = playlist_FindPrevFromParent( p_playlist,
1010 p_playlist->request.i_view,
1012 p_playlist->request.p_node,
1016 /* We reach the beginning of the playlist.
1017 Go back to the last item. */
1018 p_new = playlist_RecursiveFindLast( p_playlist,
1021 if( p_new == NULL ) break;
1026 /* Clear the request */
1027 p_playlist->request.b_request = VLC_FALSE;
1029 /* "Automatic" item change ( next ) */
1032 p_playlist->request_date = 0;
1034 if( p_playlist->status.i_view == -1 )
1036 #ifdef PLAYLIST_DEBUG
1037 msg_Dbg( p_playlist, "no request - old mode" );
1039 if( p_playlist->i_index + 1 < p_playlist->i_size )
1041 p_playlist->i_index++;
1042 p_new = p_playlist->pp_items[p_playlist->i_index];
1043 if( !( p_new->i_flags & PLAYLIST_SKIP_FLAG ) )
1050 if( b_loop && p_playlist->i_size > 0)
1052 p_playlist->i_index = 0;
1053 p_new = p_playlist->pp_items[0];
1059 /* We are playing with a view */
1062 #ifdef PLAYLIST_DEBUG
1063 msg_Dbg( p_playlist,"no request - from a view" );
1065 playlist_view_t *p_view =
1066 playlist_ViewFind( p_playlist,
1067 p_playlist->status.i_view );
1070 msg_Err( p_playlist, "p_view is NULL and should not! (FIXME)" );
1074 p_new = playlist_FindNextFromParent( p_playlist,
1075 p_playlist->status.i_view,
1077 p_playlist->status.p_node,
1078 p_playlist->status.p_item );
1079 if( p_new == NULL && b_loop )
1081 #ifdef PLAYLIST_DEBUG
1082 msg_Dbg( p_playlist, "looping" );
1084 p_new = playlist_FindNextFromParent( p_playlist,
1085 p_playlist->status.i_view,
1090 if( p_new != NULL && !(p_new->i_flags & PLAYLIST_SKIP_FLAG) )
1097 if( p_playlist->i_index >= 0 && p_new != NULL &&
1098 p_playlist->pp_items[p_playlist->i_index] != p_new )
1100 p_playlist->i_index = playlist_GetPositionById( p_playlist,
1101 p_new->input.i_id );
1104 #ifdef PLAYLIST_PROFILE
1105 msg_Dbg(p_playlist,"next item found in "I64Fi " us", mdate()-start );
1110 msg_Info( p_playlist, "nothing to play" );
1115 /*****************************************************************************
1116 * PlayItem: start the input thread for an item
1117 ****************************************************************************/
1118 static int PlayItem( playlist_t *p_playlist, playlist_item_t *p_item )
1122 msg_Dbg( p_playlist, "creating new input thread" );
1124 p_item->i_nb_played++;
1125 p_playlist->status.p_item = p_item;
1127 p_playlist->i_index = playlist_GetPositionById( p_playlist,
1128 p_item->input.i_id );
1130 #ifdef PLAYLIST_PROFILE
1131 if( p_playlist->request_date != 0 )
1133 msg_Dbg( p_playlist, "request processed after "I64Fi " us",
1134 mdate() - p_playlist->request_date );
1138 p_playlist->p_input = input_CreateThread( p_playlist, &p_item->input );
1140 var_AddCallback( p_playlist->p_input, "item-change",
1141 ItemChange, p_playlist );
1143 val.i_int = p_item->input.i_id;
1144 /* unlock the playlist to set the var...mmm */
1145 vlc_mutex_unlock( &p_playlist->object_lock);
1146 var_Set( p_playlist, "playlist-current", val);
1147 vlc_mutex_lock( &p_playlist->object_lock);
1153 /* Forward item change from input */
1154 static int ItemChange( vlc_object_t *p_obj, const char *psz_var,
1155 vlc_value_t oldval, vlc_value_t newval, void *param )
1157 playlist_t *p_playlist = (playlist_t *)param;
1159 //p_playlist->b_need_update = VLC_TRUE;
1160 var_SetInteger( p_playlist, "item-change", newval.i_int );
1163 /* FIXME: Make that automatic */
1164 // playlist_ViewUpdate( p_playlist, VIEW_S_AUTHOR );