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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, 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 #include "vlc_interaction.h"
37 #define TITLE_CATEGORY N_( "By category" )
38 #define TITLE_SIMPLE N_( "Manually added" )
39 #define TITLE_ALL N_( "All items, unsorted" )
41 #undef PLAYLIST_PROFILE
44 /*****************************************************************************
46 *****************************************************************************/
47 static void RunThread ( playlist_t * );
48 static void RunPreparse( playlist_preparse_t * );
49 static playlist_item_t * NextItem ( playlist_t * );
50 static int PlayItem ( playlist_t *, playlist_item_t * );
52 int playlist_vaControl( playlist_t * p_playlist, int i_query, va_list args );
54 void playlist_PreparseEnqueueItemSub( playlist_t *, playlist_item_t * );
56 playlist_item_t *playlist_RecursiveFindLast(playlist_t *p_playlist,
57 playlist_item_t *p_node );
59 /*****************************************************************************
60 * Helper Function for NextItem
61 *****************************************************************************/
63 playlist_item_t *playlist_RecursiveFindLast(playlist_t *p_playlist,
64 playlist_item_t *p_node )
67 playlist_item_t *p_item;
68 for ( i = p_node->i_children - 1; i >= 0; i-- )
70 if( p_node->pp_children[i]->i_children == -1 )
71 return p_node->pp_children[i];
72 else if(p_node->pp_children[i]->i_children > 0)
74 p_item = playlist_RecursiveFindLast( p_playlist,
75 p_node->pp_children[i] );
89 * Create a playlist structure.
90 * \param p_parent the vlc object that is to be the parent of this playlist
91 * \return a pointer to the created playlist, or NULL on error
93 playlist_t * __playlist_Create ( vlc_object_t *p_parent )
95 playlist_t *p_playlist;
96 playlist_view_t *p_view;
99 /* Allocate structure */
100 p_playlist = vlc_object_create( p_parent, VLC_OBJECT_PLAYLIST );
103 msg_Err( p_parent, "out of memory" );
107 /* These variables control updates */
108 var_Create( p_playlist, "intf-change", VLC_VAR_BOOL );
109 val.b_bool = VLC_TRUE;
110 var_Set( p_playlist, "intf-change", val );
112 var_Create( p_playlist, "item-change", VLC_VAR_INTEGER );
114 var_Set( p_playlist, "item-change", val );
116 var_Create( p_playlist, "item-deleted", VLC_VAR_INTEGER );
118 var_Set( p_playlist, "item-deleted", val );
120 var_Create( p_playlist, "item-append", VLC_VAR_ADDRESS );
122 var_Create( p_playlist, "playlist-current", VLC_VAR_INTEGER );
124 var_Set( p_playlist, "playlist-current", val );
126 var_Create( p_playlist, "intf-popupmenu", VLC_VAR_BOOL );
128 var_Create( p_playlist, "intf-show", VLC_VAR_BOOL );
129 val.b_bool = VLC_TRUE;
130 var_Set( p_playlist, "intf-show", val );
133 /* Variables to control playback */
134 var_CreateGetBool( p_playlist, "play-and-stop" );
135 var_CreateGetBool( p_playlist, "random" );
136 var_CreateGetBool( p_playlist, "repeat" );
137 var_CreateGetBool( p_playlist, "loop" );
139 /* Initialise data structures */
140 vlc_mutex_init( p_playlist, &p_playlist->gc_lock );
141 p_playlist->i_last_id = 0;
142 p_playlist->b_go_next = VLC_TRUE;
143 p_playlist->p_input = NULL;
145 p_playlist->request_date = 0;
147 p_playlist->i_views = 0;
148 p_playlist->pp_views = NULL;
150 p_playlist->i_index = -1;
151 p_playlist->i_size = 0;
152 p_playlist->pp_items = NULL;
153 p_playlist->i_all_size = 0;
154 p_playlist->pp_all_items = 0;
156 playlist_ViewInsert( p_playlist, VIEW_CATEGORY, TITLE_CATEGORY );
157 playlist_ViewInsert( p_playlist, VIEW_ALL, TITLE_ALL );
159 p_view = playlist_ViewFind( p_playlist, VIEW_CATEGORY );
161 p_playlist->p_general =
162 playlist_NodeCreate( p_playlist, VIEW_CATEGORY,
163 _( "General" ), p_view->p_root );
164 p_playlist->p_general->i_flags |= PLAYLIST_RO_FLAG;
166 /* Set startup status
167 * We set to simple view on startup for interfaces that don't do
169 p_view = playlist_ViewFind( p_playlist, VIEW_CATEGORY );
170 p_playlist->status.i_view = VIEW_CATEGORY;
171 p_playlist->status.p_item = NULL;
172 p_playlist->status.p_node = p_view->p_root;
173 p_playlist->request.b_request = VLC_FALSE;
174 p_playlist->status.i_status = PLAYLIST_STOPPED;
177 p_playlist->i_sort = SORT_ID;
178 p_playlist->i_order = ORDER_NORMAL;
180 /* Finally, launch the thread ! */
181 if( vlc_thread_create( p_playlist, "playlist", RunThread,
182 VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
184 msg_Err( p_playlist, "cannot spawn playlist thread" );
185 vlc_object_destroy( p_playlist );
189 /* Preparsing stuff */
190 p_playlist->p_preparse = vlc_object_create( p_playlist,
191 sizeof( playlist_preparse_t ) );
192 if( !p_playlist->p_preparse )
194 msg_Err( p_playlist, "unable to create preparser" );
195 vlc_object_destroy( p_playlist );
200 p_playlist->p_preparse->i_waiting = 0;
201 p_playlist->p_preparse->pp_waiting = NULL;
204 p_playlist->p_interaction = NULL;
206 vlc_object_attach( p_playlist->p_preparse, p_playlist );
207 if( vlc_thread_create( p_playlist->p_preparse, "preparser",
208 RunPreparse, VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
210 msg_Err( p_playlist, "cannot spawn preparse thread" );
211 vlc_object_detach( p_playlist->p_preparse );
212 vlc_object_destroy( p_playlist->p_preparse );
216 /* The object has been initialized, now attach it */
217 vlc_object_attach( p_playlist, p_parent );
223 * Destroy the playlist.
225 * Delete all items in the playlist and free the playlist structure.
226 * \param p_playlist the playlist structure to destroy
227 * \return VLC_SUCCESS or an error
229 int playlist_Destroy( playlist_t * p_playlist )
232 p_playlist->b_die = 1;
234 while( p_playlist->i_sds )
236 playlist_ServicesDiscoveryRemove( p_playlist,
237 p_playlist->pp_sds[0]->psz_module );
240 if( p_playlist->p_interaction )
242 intf_InteractionDestroy( p_playlist->p_interaction );
245 vlc_thread_join( p_playlist->p_preparse );
246 vlc_thread_join( p_playlist );
248 vlc_object_detach( p_playlist->p_preparse );
250 var_Destroy( p_playlist, "intf-change" );
251 var_Destroy( p_playlist, "item-change" );
252 var_Destroy( p_playlist, "playlist-current" );
253 var_Destroy( p_playlist, "intf-popmenu" );
254 var_Destroy( p_playlist, "intf-show" );
255 var_Destroy( p_playlist, "play-and-stop" );
256 var_Destroy( p_playlist, "random" );
257 var_Destroy( p_playlist, "repeat" );
258 var_Destroy( p_playlist, "loop" );
260 playlist_Clear( p_playlist );
262 for( i = p_playlist->i_views - 1; i >= 0 ; i-- )
264 playlist_view_t *p_view = p_playlist->pp_views[i];
265 if( p_view->psz_name )
266 free( p_view->psz_name );
267 playlist_ItemDelete( p_view->p_root );
268 REMOVE_ELEM( p_playlist->pp_views, p_playlist->i_views, i );
272 vlc_mutex_destroy( &p_playlist->gc_lock );
273 vlc_object_destroy( p_playlist->p_preparse );
274 vlc_object_destroy( p_playlist );
281 * Do a playlist action.
283 * If there is something in the playlist then you can do playlist actions.
285 * Playlist lock must not be taken when calling this function
287 * \param p_playlist the playlist to do the command on
288 * \param i_query the command to do
289 * \param variable number of arguments
290 * \return VLC_SUCCESS or an error
292 int playlist_LockControl( playlist_t * p_playlist, int i_query, ... )
296 va_start( args, i_query );
297 vlc_mutex_lock( &p_playlist->object_lock );
298 i_result = playlist_vaControl( p_playlist, i_query, args );
300 vlc_mutex_unlock( &p_playlist->object_lock );
305 * Do a playlist action.
307 * If there is something in the playlist then you can do playlist actions.
309 * Playlist lock must be taken when calling this function
311 * \param p_playlist the playlist to do the command on
312 * \param i_query the command to do
313 * \param variable number of arguments
314 * \return VLC_SUCCESS or an error
316 int playlist_Control( playlist_t * p_playlist, int i_query, ... )
320 va_start( args, i_query );
321 i_result = playlist_vaControl( p_playlist, i_query, args );
327 int playlist_vaControl( playlist_t * p_playlist, int i_query, va_list args )
329 playlist_view_t *p_view;
330 playlist_item_t *p_item, *p_node;
334 #ifdef PLAYLIST_PROFILE
335 p_playlist->request_date = mdate();
338 if( p_playlist->i_size <= 0 )
346 p_playlist->status.i_status = PLAYLIST_STOPPED;
347 p_playlist->request.b_request = VLC_TRUE;
348 p_playlist->request.p_item = NULL;
351 case PLAYLIST_ITEMPLAY:
352 p_item = (playlist_item_t *)va_arg( args, playlist_item_t * );
353 if ( p_item == NULL || p_item->input.psz_uri == NULL )
355 p_playlist->status.i_status = PLAYLIST_RUNNING;
356 p_playlist->request.i_skip = 0;
357 p_playlist->request.b_request = VLC_TRUE;
358 p_playlist->request.p_item = p_item;
359 p_playlist->request.i_view = p_playlist->status.i_view;
360 p_view = playlist_ViewFind( p_playlist, p_playlist->status.i_view );
363 p_playlist->request.p_node = p_view->p_root;
367 p_playlist->request.p_node = NULL;
371 case PLAYLIST_VIEWPLAY:
372 i_view = (int)va_arg( args,int );
373 p_node = (playlist_item_t *)va_arg( args, playlist_item_t * );
374 p_item = (playlist_item_t *)va_arg( args, playlist_item_t * );
375 if ( p_node == NULL ) //|| (p_item != NULL && p_item->input.psz_uri
378 p_playlist->status.i_status = PLAYLIST_STOPPED;
379 p_playlist->request.b_request = VLC_TRUE;
382 p_playlist->status.i_status = PLAYLIST_RUNNING;
383 p_playlist->request.i_skip = 0;
384 p_playlist->request.b_request = VLC_TRUE;
385 p_playlist->request.i_view = i_view;
386 p_playlist->request.p_node = p_node;
387 p_playlist->request.p_item = p_item;
389 /* Don't go further if the node doesn't want to */
390 if( ! p_playlist->request.p_node->i_flags & PLAYLIST_SKIP_FLAG )
392 p_playlist->b_go_next = VLC_FALSE;
396 p_playlist->b_go_next = VLC_TRUE;
401 p_playlist->status.i_status = PLAYLIST_RUNNING;
403 if( p_playlist->p_input )
405 val.i_int = PLAYING_S;
406 var_Set( p_playlist->p_input, "state", val );
410 /* FIXME : needed ? */
411 p_playlist->request.b_request = VLC_TRUE;
412 p_playlist->request.i_view = p_playlist->status.i_view;
413 p_playlist->request.p_node = p_playlist->status.p_node;
414 p_playlist->request.p_item = p_playlist->status.p_item;
415 p_playlist->request.i_skip = 0;
416 p_playlist->request.i_goto = -1;
419 case PLAYLIST_AUTOPLAY:
420 p_playlist->status.i_status = PLAYLIST_RUNNING;
421 p_playlist->status.p_node = p_playlist->p_general;
423 p_playlist->request.b_request = VLC_FALSE;
428 if( p_playlist->p_input )
429 var_Get( p_playlist->p_input, "state", &val );
431 if( val.i_int == PAUSE_S )
433 p_playlist->status.i_status = PLAYLIST_RUNNING;
434 if( p_playlist->p_input )
436 val.i_int = PLAYING_S;
437 var_Set( p_playlist->p_input, "state", val );
442 p_playlist->status.i_status = PLAYLIST_PAUSED;
443 if( p_playlist->p_input )
446 var_Set( p_playlist->p_input, "state", val );
452 p_playlist->request.i_view = p_playlist->status.i_view;
453 if( p_playlist->status.i_view > -1 )
455 p_playlist->request.p_node = p_playlist->status.p_node;
456 p_playlist->request.p_item = p_playlist->status.p_item;
460 p_playlist->request.p_node = NULL;
461 p_playlist->request.p_item = NULL;
463 p_playlist->request.i_skip = (int) va_arg( args, int );
464 p_playlist->request.b_request = VLC_TRUE;
468 p_playlist->status.i_status = PLAYLIST_RUNNING;
469 p_playlist->request.p_node = NULL;
470 p_playlist->request.p_item = NULL;
471 p_playlist->request.i_view = -1;
472 p_playlist->request.i_goto = (int) va_arg( args, int );
473 p_playlist->request.b_request = VLC_TRUE;
477 msg_Err( p_playlist, "unknown playlist query" );
485 int playlist_PreparseEnqueue( playlist_t *p_playlist,
486 input_item_t *p_item )
488 vlc_mutex_lock( &p_playlist->p_preparse->object_lock );
489 INSERT_ELEM( p_playlist->p_preparse->pp_waiting,
490 p_playlist->p_preparse->i_waiting,
491 p_playlist->p_preparse->i_waiting,
493 vlc_mutex_unlock( &p_playlist->p_preparse->object_lock );
497 /* Should only be called if playlist and preparser are locked */
498 void playlist_PreparseEnqueueItemSub( playlist_t *p_playlist,
499 playlist_item_t *p_item )
502 if( p_item->i_children == -1 )
504 INSERT_ELEM( p_playlist->p_preparse->pp_waiting,
505 p_playlist->p_preparse->i_waiting,
506 p_playlist->p_preparse->i_waiting,
511 for( i = 0; i < p_item->i_children; i++)
513 playlist_PreparseEnqueueItemSub( p_playlist,
514 p_item->pp_children[i] );
519 int playlist_PreparseEnqueueItem( playlist_t *p_playlist,
520 playlist_item_t *p_item )
522 vlc_mutex_lock( &p_playlist->object_lock );
523 vlc_mutex_lock( &p_playlist->p_preparse->object_lock );
524 playlist_PreparseEnqueueItemSub( p_playlist, p_item );
525 vlc_mutex_unlock( &p_playlist->p_preparse->object_lock );
526 vlc_mutex_unlock( &p_playlist->object_lock );
531 /* Destroy remaining objects */
532 static mtime_t ObjectGarbageCollector( playlist_t *p_playlist, int i_type,
533 mtime_t destroy_date )
537 if( destroy_date > mdate() ) return destroy_date;
539 if( destroy_date == 0 )
541 /* give a little time */
542 return mdate() + I64C(1000000);
546 vlc_mutex_lock( &p_playlist->gc_lock );
547 while( ( p_obj = vlc_object_find( p_playlist, i_type, FIND_CHILD ) ) )
549 if( p_obj->p_parent != (vlc_object_t*)p_playlist )
551 /* only first child (ie unused) */
552 vlc_object_release( p_obj );
555 if( i_type == VLC_OBJECT_VOUT )
557 msg_Dbg( p_playlist, "garbage collector destroying 1 vout" );
558 vlc_object_detach( p_obj );
559 vlc_object_release( p_obj );
560 vout_Destroy( (vout_thread_t *)p_obj );
562 else if( i_type == VLC_OBJECT_SOUT )
564 vlc_object_release( p_obj );
565 sout_DeleteInstance( (sout_instance_t*)p_obj );
568 vlc_mutex_unlock( &p_playlist->gc_lock );
573 /*****************************************************************************
574 * RunThread: main playlist thread
575 *****************************************************************************/
576 static void RunThread ( playlist_t *p_playlist )
579 playlist_item_t *p_item = NULL;
581 mtime_t i_vout_destroyed_date = 0;
582 mtime_t i_sout_destroyed_date = 0;
586 playlist_item_t *p_autodelete_item = NULL;
588 /* Tell above that we're ready */
589 vlc_thread_ready( p_playlist );
591 while( !p_playlist->b_die )
594 if( p_playlist->p_interaction )
596 intf_InteractionManage( p_playlist );
599 vlc_mutex_lock( &p_playlist->object_lock );
601 /* First, check if we have something to do */
602 /* FIXME : this can be called several times */
603 if( p_playlist->request.b_request )
605 #ifdef PLAYLIST_PROFILE
606 msg_Dbg(p_playlist, "beginning processing of request, "
607 I64Fi" us ", mdate() - p_playlist->request_date );
609 /* Stop the existing input */
610 if( p_playlist->p_input )
612 input_StopThread( p_playlist->p_input );
614 /* The code below will start the next input for us */
615 if( p_playlist->status.i_status == PLAYLIST_STOPPED )
617 p_playlist->request.b_request = VLC_FALSE;
621 /* If there is an input, check that it doesn't need to die. */
622 if( p_playlist->p_input )
624 if( i_loops % 5 == 0 )
626 stats_ComputeInputStats( p_playlist->p_input,
627 p_playlist->p_input->input.p_item->p_stats );
628 // stats_DumpInputStats(
629 // p_playlist->p_input->input.p_item->p_stats );
632 /* This input is dead. Remove it ! */
633 if( p_playlist->p_input->b_dead )
635 input_thread_t *p_input;
637 p_input = p_playlist->p_input;
638 p_playlist->p_input = NULL;
640 /* Release the playlist lock, because we may get stuck
641 * in input_DestroyThread() for some time. */
642 vlc_mutex_unlock( &p_playlist->object_lock );
645 input_DestroyThread( p_input );
647 /* Unlink current input
648 * (_after_ input_DestroyThread for vout garbage collector) */
649 vlc_object_detach( p_input );
652 vlc_object_destroy( p_input );
654 i_vout_destroyed_date = 0;
655 i_sout_destroyed_date = 0;
657 if( p_playlist->status.p_item->i_flags
658 & PLAYLIST_REMOVE_FLAG )
660 playlist_ItemDelete( p_item );
661 p_playlist->status.p_item = NULL;
666 /* This input is dying, let it do */
667 else if( p_playlist->p_input->b_die )
671 /* This input has finished, ask it to die ! */
672 else if( p_playlist->p_input->b_error
673 || p_playlist->p_input->b_eof )
675 /* TODO FIXME XXX TODO FIXME XXX */
676 /* Check for autodeletion */
678 if( p_playlist->status.p_item->i_flags & PLAYLIST_DEL_FLAG )
680 p_autodelete_item = p_playlist->status.p_item;
682 input_StopThread( p_playlist->p_input );
683 /* Select the next playlist item */
684 vlc_mutex_unlock( &p_playlist->object_lock );
687 else if( p_playlist->p_input->i_state != INIT_S )
689 vlc_mutex_unlock( &p_playlist->object_lock );
690 i_vout_destroyed_date =
691 ObjectGarbageCollector( p_playlist, VLC_OBJECT_VOUT,
692 i_vout_destroyed_date );
693 i_sout_destroyed_date =
694 ObjectGarbageCollector( p_playlist, VLC_OBJECT_SOUT,
695 i_sout_destroyed_date );
696 vlc_mutex_lock( &p_playlist->object_lock );
699 else if( p_playlist->status.i_status != PLAYLIST_STOPPED )
701 /* Start another input.
702 * Get the next item to play */
703 p_item = NextItem( p_playlist );
708 if( p_autodelete_item )
710 playlist_Delete( p_playlist,
711 p_autodelete_item->input.i_id );
712 p_autodelete_item = NULL;
714 p_playlist->status.i_status = PLAYLIST_STOPPED;
715 vlc_mutex_unlock( &p_playlist->object_lock );
719 PlayItem( p_playlist, p_item );
721 if( p_autodelete_item )
723 playlist_Delete( p_playlist, p_autodelete_item->input.i_id );
724 p_autodelete_item = NULL;
727 else if( p_playlist->status.i_status == PLAYLIST_STOPPED )
729 if( p_item && p_playlist->status.p_item &&
730 p_playlist->status.p_item->i_flags & PLAYLIST_REMOVE_FLAG )
732 playlist_ItemDelete( p_item );
733 p_playlist->status.p_item = NULL;
736 /* Collect garbage */
737 vlc_mutex_unlock( &p_playlist->object_lock );
738 i_sout_destroyed_date =
739 ObjectGarbageCollector( p_playlist, VLC_OBJECT_SOUT, mdate() );
740 i_vout_destroyed_date =
741 ObjectGarbageCollector( p_playlist, VLC_OBJECT_VOUT, mdate() );
742 vlc_mutex_lock( &p_playlist->object_lock );
744 vlc_mutex_unlock( &p_playlist->object_lock );
746 msleep( INTF_IDLE_SLEEP / 2 );
748 /* Stop sleeping earlier if we have work */
749 /* TODO : statistics about this */
750 if ( p_playlist->request.b_request &&
751 p_playlist->status.i_status == PLAYLIST_RUNNING )
756 msleep( INTF_IDLE_SLEEP / 2 );
761 /* If there is an input, kill it */
764 vlc_mutex_lock( &p_playlist->object_lock );
766 if( p_playlist->p_input == NULL )
768 vlc_mutex_unlock( &p_playlist->object_lock );
772 if( p_playlist->p_input->b_dead )
774 input_thread_t *p_input;
776 /* Unlink current input */
777 p_input = p_playlist->p_input;
778 p_playlist->p_input = NULL;
779 vlc_mutex_unlock( &p_playlist->object_lock );
782 input_DestroyThread( p_input );
783 /* Unlink current input (_after_ input_DestroyThread for vout
784 * garbage collector)*/
785 vlc_object_detach( p_input );
788 vlc_object_destroy( p_input );
791 else if( p_playlist->p_input->b_die )
793 /* This input is dying, leave it alone */
796 else if( p_playlist->p_input->b_error || p_playlist->p_input->b_eof )
798 input_StopThread( p_playlist->p_input );
799 vlc_mutex_unlock( &p_playlist->object_lock );
804 p_playlist->p_input->b_eof = 1;
807 vlc_mutex_unlock( &p_playlist->object_lock );
809 msleep( INTF_IDLE_SLEEP );
812 /* close all remaining sout */
813 while( ( p_obj = vlc_object_find( p_playlist,
814 VLC_OBJECT_SOUT, FIND_CHILD ) ) )
816 vlc_object_release( p_obj );
817 sout_DeleteInstance( (sout_instance_t*)p_obj );
820 /* close all remaining vout */
821 while( ( p_obj = vlc_object_find( p_playlist,
822 VLC_OBJECT_VOUT, FIND_CHILD ) ) )
824 vlc_object_detach( p_obj );
825 vlc_object_release( p_obj );
826 vout_Destroy( (vout_thread_t *)p_obj );
830 /* Queue for items to preparse */
831 static void RunPreparse ( playlist_preparse_t *p_obj )
833 playlist_t *p_playlist = (playlist_t *)p_obj->p_parent;
836 /* Tell above that we're ready */
837 vlc_thread_ready( p_obj );
839 while( !p_playlist->b_die )
841 vlc_mutex_lock( &p_obj->object_lock );
843 if( p_obj->i_waiting > 0 )
845 input_item_t *p_current = p_obj->pp_waiting[0];
846 REMOVE_ELEM( p_obj->pp_waiting, p_obj->i_waiting, 0 );
847 vlc_mutex_unlock( &p_obj->object_lock );
848 input_Preparse( p_playlist, p_current );
849 var_SetInteger( p_playlist, "item-change", p_current->i_id );
850 vlc_mutex_lock( &p_obj->object_lock );
852 b_sleep = ( p_obj->i_waiting == 0 );
854 vlc_mutex_unlock( &p_obj->object_lock );
856 if( p_obj->i_waiting == 0 )
858 msleep( INTF_IDLE_SLEEP );
863 /*****************************************************************************
865 *****************************************************************************
866 * This function calculates the next playlist item, depending
867 * on the playlist course mode (forward, backward, random, view,...).
868 *****************************************************************************/
869 static playlist_item_t * NextItem( playlist_t *p_playlist )
871 playlist_item_t *p_new = NULL;
872 int i_skip,i_goto,i, i_new, i_count ;
873 playlist_view_t *p_view;
875 vlc_bool_t b_loop = var_GetBool( p_playlist, "loop" );
876 vlc_bool_t b_random = var_GetBool( p_playlist, "random" );
877 vlc_bool_t b_repeat = var_GetBool( p_playlist, "repeat" );
878 vlc_bool_t b_playstop = var_GetBool( p_playlist, "play-and-stop" );
880 #ifdef PLAYLIST_PROFILE
881 /* Calculate time needed */
882 int64_t start = mdate();
885 /* Handle quickly a few special cases */
887 /* No items to play */
888 if( p_playlist->i_size == 0 )
890 msg_Info( p_playlist, "playlist is empty" );
893 /* Nothing requested */
894 if( !p_playlist->request.b_request && p_playlist->status.p_item == NULL )
896 msg_Dbg( p_playlist,"nothing requested, starting" );
899 /* Repeat and play/stop */
900 if( !p_playlist->request.b_request && b_repeat == VLC_TRUE &&
901 p_playlist->status.p_item )
903 msg_Dbg( p_playlist,"repeating item" );
904 return p_playlist->status.p_item;
907 if( !p_playlist->request.b_request && b_playstop == VLC_TRUE )
909 msg_Dbg( p_playlist,"stopping (play and stop)");
913 if( !p_playlist->request.b_request && p_playlist->status.p_item &&
914 !( p_playlist->status.p_item->i_flags & PLAYLIST_SKIP_FLAG ) )
916 msg_Dbg( p_playlist, "no-skip mode, stopping") ;
920 /* TODO: improve this (only use current node) */
921 /* TODO: use the "shuffled view" internally ? */
922 /* Random case. This is an exception: if request, but request is skip +- 1
923 * we don't go to next item but select a new random one. */
925 ( !p_playlist->request.b_request ||
926 ( p_playlist->request.b_request && ( p_playlist->request.p_item == NULL ||
927 p_playlist->request.i_skip == 1 || p_playlist->request.i_skip == -1 ) ) ) )
929 /* how many items to choose from ? */
931 for ( i = 0; i < p_playlist->i_size; i++ )
933 if ( p_playlist->pp_items[i]->i_nb_played == 0 )
939 /* Don't loop? Exit! */
942 /* Otherwise reset the counter */
943 for ( i = 0; i < p_playlist->i_size; i++ )
945 p_playlist->pp_items[i]->i_nb_played = 0;
947 i_count = p_playlist->i_size;
949 srand( (unsigned int)mdate() );
950 i = rand() % i_count + 1 ;
951 /* loop thru the list and count down the unplayed items to the selected one */
952 for ( i_new = 0; i_new < p_playlist->i_size && i > 0; i_new++ )
954 if ( p_playlist->pp_items[i_new]->i_nb_played == 0 )
959 p_playlist->request.i_skip = 0;
960 p_playlist->request.b_request = VLC_FALSE;
961 return p_playlist->pp_items[i_new];
964 /* Start the real work */
965 if( p_playlist->request.b_request )
967 #ifdef PLAYLIST_DEBUG
968 msg_Dbg( p_playlist,"processing request" );
970 /* We are not playing from a view */
971 if( p_playlist->request.i_view == -1 )
973 #ifdef PLAYLIST_DEBUG
974 msg_Dbg( p_playlist, "non-view mode request");
976 /* Directly select the item, just like now */
977 p_new = p_playlist->request.p_item;
978 i_skip = p_playlist->request.i_skip;
979 i_goto = p_playlist->request.i_goto;
981 if( p_playlist->i_index < 0 ) p_playlist->i_index = 0;
983 p_new = p_playlist->pp_items[p_playlist->i_index];
985 if( i_goto >= 0 && i_goto < p_playlist->i_size )
987 p_playlist->i_index = i_goto;
988 p_new = p_playlist->pp_items[p_playlist->i_index];
989 p_playlist->request.i_goto = -1;
994 if( p_playlist->i_index + i_skip < p_playlist->i_size &&
995 p_playlist->i_index + i_skip >= 0 )
997 p_playlist->i_index += i_skip;
998 p_new = p_playlist->pp_items[p_playlist->i_index];
1000 p_playlist->request.i_skip = 0;
1002 if( !( p_new->i_flags & PLAYLIST_SKIP_FLAG ) )
1009 #ifdef PLAYLIST_DEBUG
1010 msg_Dbg( p_playlist, "view mode request" );
1012 p_new = p_playlist->request.p_item;
1013 i_skip = p_playlist->request.i_skip;
1015 /* If we are asked for a node, take its first item */
1016 if( p_playlist->request.p_item == NULL && i_skip == 0 )
1021 p_view = playlist_ViewFind( p_playlist,p_playlist->request.i_view );
1022 p_playlist->status.p_node = p_playlist->request.p_node;
1023 p_playlist->status.i_view = p_playlist->request.i_view;
1026 msg_Err( p_playlist, "p_view is NULL and should not! (requested view is %i", p_playlist->request.i_view );
1028 else if( i_skip > 0 )
1030 for( i = i_skip; i > 0 ; i-- )
1032 p_new = playlist_FindNextFromParent( p_playlist,
1033 p_playlist->request.i_view,
1035 p_playlist->request.p_node,
1039 #ifdef PLAYLIST_DEBUG
1040 msg_Dbg( p_playlist, "looping" );
1042 p_new = playlist_FindNextFromParent( p_playlist,
1043 p_playlist->request.i_view,
1047 if( p_new == NULL ) break;
1051 else if( i_skip < 0 )
1053 for( i = i_skip; i < 0 ; i++ )
1055 p_new = playlist_FindPrevFromParent( p_playlist,
1056 p_playlist->request.i_view,
1058 p_playlist->request.p_node,
1062 /* We reach the beginning of the playlist.
1063 Go back to the last item. */
1064 p_new = playlist_RecursiveFindLast( p_playlist,
1067 if( p_new == NULL ) break;
1072 /* Clear the request */
1073 p_playlist->request.b_request = VLC_FALSE;
1075 /* "Automatic" item change ( next ) */
1078 p_playlist->request_date = 0;
1080 if( p_playlist->status.i_view == -1 )
1082 #ifdef PLAYLIST_DEBUG
1083 msg_Dbg( p_playlist, "no request - old mode" );
1085 if( p_playlist->i_index + 1 < p_playlist->i_size )
1087 p_playlist->i_index++;
1088 p_new = p_playlist->pp_items[p_playlist->i_index];
1089 if( !( p_new->i_flags & PLAYLIST_SKIP_FLAG ) )
1096 if( b_loop && p_playlist->i_size > 0)
1098 p_playlist->i_index = 0;
1099 p_new = p_playlist->pp_items[0];
1105 /* We are playing with a view */
1108 #ifdef PLAYLIST_DEBUG
1109 msg_Dbg( p_playlist,"no request - from a view" );
1111 playlist_view_t *p_view =
1112 playlist_ViewFind( p_playlist,
1113 p_playlist->status.i_view );
1116 msg_Err( p_playlist, "p_view is NULL and should not! (FIXME)" );
1120 p_new = playlist_FindNextFromParent( p_playlist,
1121 p_playlist->status.i_view,
1123 p_playlist->status.p_node,
1124 p_playlist->status.p_item );
1125 if( p_new == NULL && b_loop )
1127 #ifdef PLAYLIST_DEBUG
1128 msg_Dbg( p_playlist, "looping" );
1130 p_new = playlist_FindNextFromParent( p_playlist,
1131 p_playlist->status.i_view,
1136 if( p_new != NULL && !(p_new->i_flags & PLAYLIST_SKIP_FLAG) )
1143 if( p_playlist->i_index >= 0 && p_new != NULL &&
1144 p_playlist->pp_items[p_playlist->i_index] != p_new )
1146 p_playlist->i_index = playlist_GetPositionById( p_playlist,
1147 p_new->input.i_id );
1150 #ifdef PLAYLIST_PROFILE
1151 msg_Dbg(p_playlist,"next item found in "I64Fi " us", mdate()-start );
1156 msg_Info( p_playlist, "nothing to play" );
1161 /*****************************************************************************
1162 * PlayItem: start the input thread for an item
1163 ****************************************************************************/
1164 static int PlayItem( playlist_t *p_playlist, playlist_item_t *p_item )
1168 msg_Dbg( p_playlist, "creating new input thread" );
1170 p_item->i_nb_played++;
1171 p_playlist->status.p_item = p_item;
1173 p_playlist->i_index = playlist_GetPositionById( p_playlist,
1174 p_item->input.i_id );
1176 #ifdef PLAYLIST_PROFILE
1177 if( p_playlist->request_date != 0 )
1179 msg_Dbg( p_playlist, "request processed after "I64Fi " us",
1180 mdate() - p_playlist->request_date );
1184 p_playlist->p_input = input_CreateThread( p_playlist, &p_item->input );
1186 val.i_int = p_item->input.i_id;
1187 /* unlock the playlist to set the var...mmm */
1188 vlc_mutex_unlock( &p_playlist->object_lock);
1189 var_Set( p_playlist, "playlist-current", val);
1190 vlc_mutex_lock( &p_playlist->object_lock);