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;
342 case PLAYLIST_ITEMPLAY:
343 p_item = (playlist_item_t *)va_arg( args, playlist_item_t * );
344 if ( p_item == NULL || p_item->input.psz_uri == NULL )
346 p_playlist->status.i_status = PLAYLIST_RUNNING;
347 p_playlist->request.i_skip = 0;
348 p_playlist->request.b_request = VLC_TRUE;
349 p_playlist->request.p_item = p_item;
350 p_playlist->request.i_view = p_playlist->status.i_view;
351 p_view = playlist_ViewFind( p_playlist, p_playlist->status.i_view );
354 p_playlist->request.p_node = p_view->p_root;
358 p_playlist->request.p_node = NULL;
362 case PLAYLIST_VIEWPLAY:
363 i_view = (int)va_arg( args,int );
364 p_node = (playlist_item_t *)va_arg( args, playlist_item_t * );
365 p_item = (playlist_item_t *)va_arg( args, playlist_item_t * );
366 if ( p_node == NULL || (p_item != NULL && p_item->input.psz_uri
369 p_playlist->status.i_status = PLAYLIST_STOPPED;
370 p_playlist->request.b_request = VLC_TRUE;
373 p_playlist->status.i_status = PLAYLIST_RUNNING;
374 p_playlist->request.i_skip = 0;
375 p_playlist->request.b_request = VLC_TRUE;
376 p_playlist->request.i_view = i_view;
377 p_playlist->request.p_node = p_node;
378 p_playlist->request.p_item = p_item;
380 /* If we select a node, play only it.
381 * If we select an item, continue */
382 if( p_playlist->request.p_item == NULL ||
383 ! p_playlist->request.p_node->i_flags & PLAYLIST_SKIP_FLAG )
385 p_playlist->b_go_next = VLC_FALSE;
389 p_playlist->b_go_next = VLC_TRUE;
394 p_playlist->status.i_status = PLAYLIST_RUNNING;
396 if( p_playlist->p_input )
398 val.i_int = PLAYING_S;
399 var_Set( p_playlist->p_input, "state", val );
403 /* FIXME : needed ? */
404 p_playlist->request.b_request = VLC_TRUE;
405 p_playlist->request.i_view = p_playlist->status.i_view;
406 p_playlist->request.p_node = p_playlist->status.p_node;
407 p_playlist->request.p_item = p_playlist->status.p_item;
408 p_playlist->request.i_skip = 0;
409 p_playlist->request.i_goto = -1;
412 case PLAYLIST_AUTOPLAY:
413 p_playlist->status.i_status = PLAYLIST_RUNNING;
415 p_playlist->request.b_request = VLC_FALSE;
420 if( p_playlist->p_input )
421 var_Get( p_playlist->p_input, "state", &val );
423 if( val.i_int == PAUSE_S )
425 p_playlist->status.i_status = PLAYLIST_RUNNING;
426 if( p_playlist->p_input )
428 val.i_int = PLAYING_S;
429 var_Set( p_playlist->p_input, "state", val );
434 p_playlist->status.i_status = PLAYLIST_PAUSED;
435 if( p_playlist->p_input )
438 var_Set( p_playlist->p_input, "state", val );
444 if( p_playlist->status.i_view > -1 )
446 p_playlist->request.i_view = p_playlist->status.i_view;
447 p_playlist->request.p_node = p_playlist->status.p_node;
448 p_playlist->request.p_item = p_playlist->status.p_item;
450 p_playlist->request.i_skip = (int) va_arg( args, int );
451 p_playlist->request.b_request = VLC_TRUE;
455 p_playlist->status.i_status = PLAYLIST_RUNNING;
456 p_playlist->request.p_node = NULL;
457 p_playlist->request.p_item = NULL;
458 p_playlist->request.i_view = -1;
459 p_playlist->request.i_goto = (int) va_arg( args, int );
460 p_playlist->request.b_request = VLC_TRUE;
464 msg_Err( p_playlist, "unimplemented playlist query" );
472 int playlist_PreparseEnqueue( playlist_t *p_playlist,
473 input_item_t *p_item )
475 vlc_mutex_lock( &p_playlist->p_preparse->object_lock );
476 INSERT_ELEM( p_playlist->p_preparse->pp_waiting,
477 p_playlist->p_preparse->i_waiting,
478 p_playlist->p_preparse->i_waiting,
480 vlc_mutex_unlock( &p_playlist->p_preparse->object_lock );
484 /* Should only be called if playlist and preparser are locked */
485 void playlist_PreparseEnqueueItemSub( playlist_t *p_playlist,
486 playlist_item_t *p_item )
489 if( p_item->i_children == -1 )
491 INSERT_ELEM( p_playlist->p_preparse->pp_waiting,
492 p_playlist->p_preparse->i_waiting,
493 p_playlist->p_preparse->i_waiting,
498 for( i = 0; i < p_item->i_children; i++)
500 playlist_PreparseEnqueueItemSub( p_playlist,
501 p_item->pp_children[i] );
506 int playlist_PreparseEnqueueItem( playlist_t *p_playlist,
507 playlist_item_t *p_item )
509 vlc_mutex_lock( &p_playlist->object_lock );
510 vlc_mutex_lock( &p_playlist->p_preparse->object_lock );
511 playlist_PreparseEnqueueItemSub( p_playlist, p_item );
512 vlc_mutex_unlock( &p_playlist->p_preparse->object_lock );
513 vlc_mutex_unlock( &p_playlist->object_lock );
518 /* Destroy remaining objects */
519 static mtime_t ObjectGarbageCollector( playlist_t *p_playlist, int i_type,
520 mtime_t destroy_date )
524 if( destroy_date > mdate() ) return destroy_date;
526 if( destroy_date == 0 )
528 /* give a little time */
529 return mdate() + I64C(1000000);
533 vlc_mutex_lock( &p_playlist->gc_lock );
534 while( ( p_obj = vlc_object_find( p_playlist, i_type, FIND_CHILD ) ) )
536 if( p_obj->p_parent != (vlc_object_t*)p_playlist )
538 /* only first child (ie unused) */
539 vlc_object_release( p_obj );
542 if( i_type == VLC_OBJECT_VOUT )
544 msg_Dbg( p_playlist, "garbage collector destroying 1 vout" );
545 vlc_object_detach( p_obj );
546 vlc_object_release( p_obj );
547 vout_Destroy( (vout_thread_t *)p_obj );
549 else if( i_type == VLC_OBJECT_SOUT )
551 vlc_object_release( p_obj );
552 sout_DeleteInstance( (sout_instance_t*)p_obj );
555 vlc_mutex_unlock( &p_playlist->gc_lock );
560 /*****************************************************************************
561 * RunThread: main playlist thread
562 *****************************************************************************/
563 static void RunThread ( playlist_t *p_playlist )
566 playlist_item_t *p_item = NULL;
568 mtime_t i_vout_destroyed_date = 0;
569 mtime_t i_sout_destroyed_date = 0;
571 playlist_item_t *p_autodelete_item = NULL;
573 /* Tell above that we're ready */
574 vlc_thread_ready( p_playlist );
576 while( !p_playlist->b_die )
578 vlc_mutex_lock( &p_playlist->object_lock );
580 /* First, check if we have something to do */
581 /* FIXME : this can be called several times */
582 if( p_playlist->request.b_request )
584 #ifdef PLAYLIST_PROFILE
585 msg_Dbg(p_playlist, "beginning processing of request, "
586 I64Fi" us ", mdate() - p_playlist->request_date );
588 /* Stop the existing input */
589 if( p_playlist->p_input )
591 input_StopThread( p_playlist->p_input );
593 /* The code below will start the next input for us */
594 if( p_playlist->status.i_status == PLAYLIST_STOPPED )
596 p_playlist->request.b_request = VLC_FALSE;
600 /* If there is an input, check that it doesn't need to die. */
601 if( p_playlist->p_input )
603 /* This input is dead. Remove it ! */
604 if( p_playlist->p_input->b_dead )
606 input_thread_t *p_input;
608 p_input = p_playlist->p_input;
609 p_playlist->p_input = NULL;
611 /* Release the playlist lock, because we may get stuck
612 * in input_DestroyThread() for some time. */
613 vlc_mutex_unlock( &p_playlist->object_lock );
616 input_DestroyThread( p_input );
618 /* Unlink current input
619 * (_after_ input_DestroyThread for vout garbage collector) */
620 vlc_object_detach( p_input );
623 vlc_object_destroy( p_input );
625 i_vout_destroyed_date = 0;
626 i_sout_destroyed_date = 0;
628 if( p_playlist->status.p_item->i_flags
629 & PLAYLIST_REMOVE_FLAG )
631 playlist_ItemDelete( p_item );
632 p_playlist->status.p_item = NULL;
637 /* This input is dying, let it do */
638 else if( p_playlist->p_input->b_die )
642 /* This input has finished, ask it to die ! */
643 else if( p_playlist->p_input->b_error
644 || p_playlist->p_input->b_eof )
646 /* TODO FIXME XXX TODO FIXME XXX */
647 /* Check for autodeletion */
649 if( p_playlist->status.p_item->i_flags & PLAYLIST_DEL_FLAG )
651 p_autodelete_item = p_playlist->status.p_item;
653 input_StopThread( p_playlist->p_input );
654 /* Select the next playlist item */
655 vlc_mutex_unlock( &p_playlist->object_lock );
658 else if( p_playlist->p_input->i_state != INIT_S )
660 vlc_mutex_unlock( &p_playlist->object_lock );
661 i_vout_destroyed_date =
662 ObjectGarbageCollector( p_playlist, VLC_OBJECT_VOUT,
663 i_vout_destroyed_date );
664 i_sout_destroyed_date =
665 ObjectGarbageCollector( p_playlist, VLC_OBJECT_SOUT,
666 i_sout_destroyed_date );
667 vlc_mutex_lock( &p_playlist->object_lock );
670 else if( p_playlist->status.i_status != PLAYLIST_STOPPED )
672 /* Start another input.
673 * Get the next item to play */
674 p_item = NextItem( p_playlist );
680 if( p_autodelete_item )
682 playlist_Delete( p_playlist,
683 p_autodelete_item->input.i_id );
684 p_autodelete_item = NULL;
686 p_playlist->status.i_status = PLAYLIST_STOPPED;
687 vlc_mutex_unlock( &p_playlist->object_lock );
691 PlayItem( p_playlist, p_item );
693 if( p_autodelete_item )
695 playlist_Delete( p_playlist, p_autodelete_item->input.i_id );
696 p_autodelete_item = NULL;
699 else if( p_playlist->status.i_status == PLAYLIST_STOPPED )
701 /* Collect garbage */
702 vlc_mutex_unlock( &p_playlist->object_lock );
703 i_sout_destroyed_date =
704 ObjectGarbageCollector( p_playlist, VLC_OBJECT_SOUT, mdate() );
705 i_vout_destroyed_date =
706 ObjectGarbageCollector( p_playlist, VLC_OBJECT_VOUT, mdate() );
707 vlc_mutex_lock( &p_playlist->object_lock );
709 vlc_mutex_unlock( &p_playlist->object_lock );
711 msleep( INTF_IDLE_SLEEP / 2 );
713 /* Stop sleeping earlier if we have work */
714 /* TODO : statistics about this */
715 if ( p_playlist->request.b_request &&
716 p_playlist->status.i_status == PLAYLIST_RUNNING )
721 msleep( INTF_IDLE_SLEEP / 2 );
726 /* If there is an input, kill it */
729 vlc_mutex_lock( &p_playlist->object_lock );
731 if( p_playlist->p_input == NULL )
733 vlc_mutex_unlock( &p_playlist->object_lock );
737 if( p_playlist->p_input->b_dead )
739 input_thread_t *p_input;
741 /* Unlink current input */
742 p_input = p_playlist->p_input;
743 p_playlist->p_input = NULL;
744 vlc_mutex_unlock( &p_playlist->object_lock );
747 input_DestroyThread( p_input );
748 /* Unlink current input (_after_ input_DestroyThread for vout
749 * garbage collector)*/
750 vlc_object_detach( p_input );
753 vlc_object_destroy( p_input );
756 else if( p_playlist->p_input->b_die )
758 /* This input is dying, leave it alone */
761 else if( p_playlist->p_input->b_error || p_playlist->p_input->b_eof )
763 input_StopThread( p_playlist->p_input );
764 vlc_mutex_unlock( &p_playlist->object_lock );
769 p_playlist->p_input->b_eof = 1;
772 vlc_mutex_unlock( &p_playlist->object_lock );
774 msleep( INTF_IDLE_SLEEP );
777 /* close all remaining sout */
778 while( ( p_obj = vlc_object_find( p_playlist,
779 VLC_OBJECT_SOUT, FIND_CHILD ) ) )
781 vlc_object_release( p_obj );
782 sout_DeleteInstance( (sout_instance_t*)p_obj );
785 /* close all remaining vout */
786 while( ( p_obj = vlc_object_find( p_playlist,
787 VLC_OBJECT_VOUT, FIND_CHILD ) ) )
789 vlc_object_detach( p_obj );
790 vlc_object_release( p_obj );
791 vout_Destroy( (vout_thread_t *)p_obj );
795 /* Queue for items to preparse */
796 static void RunPreparse ( playlist_preparse_t *p_obj )
798 playlist_t *p_playlist = (playlist_t *)p_obj->p_parent;
801 /* Tell above that we're ready */
802 vlc_thread_ready( p_obj );
804 while( !p_playlist->b_die )
806 vlc_mutex_lock( &p_obj->object_lock );
808 if( p_obj->i_waiting > 0 )
810 input_item_t *p_current = p_obj->pp_waiting[0];
811 REMOVE_ELEM( p_obj->pp_waiting, p_obj->i_waiting, 0 );
812 vlc_mutex_unlock( &p_obj->object_lock );
813 input_Preparse( p_playlist, p_current );
814 var_SetInteger( p_playlist, "item-change", p_current->i_id );
815 vlc_mutex_lock( &p_obj->object_lock );
817 b_sleep = ( p_obj->i_waiting == 0 );
819 vlc_mutex_unlock( &p_obj->object_lock );
821 if( p_obj->i_waiting == 0 )
823 msleep( INTF_IDLE_SLEEP );
828 /*****************************************************************************
830 *****************************************************************************
831 * This function calculates the next playlist item, depending
832 * on the playlist course mode (forward, backward, random, view,...).
833 *****************************************************************************/
834 static playlist_item_t * NextItem( playlist_t *p_playlist )
836 playlist_item_t *p_new = NULL;
837 int i_skip,i_goto,i, i_new, i_count ;
838 playlist_view_t *p_view;
840 vlc_bool_t b_loop = var_GetBool( p_playlist, "loop" );
841 vlc_bool_t b_random = var_GetBool( p_playlist, "random" );
842 vlc_bool_t b_repeat = var_GetBool( p_playlist, "repeat" );
843 vlc_bool_t b_playstop = var_GetBool( p_playlist, "play-and-stop" );
845 #ifdef PLAYLIST_PROFILE
846 /* Calculate time needed */
847 int64_t start = mdate();
850 /* Handle quickly a few special cases */
852 /* No items to play */
853 if( p_playlist->i_size == 0 )
855 msg_Info( p_playlist, "playlist is empty" );
858 /* Nothing requested */
859 if( !p_playlist->request.b_request && p_playlist->status.p_item == NULL )
861 msg_Dbg( p_playlist,"nothing requested, starting" );
864 /* Repeat and play/stop */
865 if( !p_playlist->request.b_request && b_repeat == VLC_TRUE )
867 msg_Dbg( p_playlist,"repeating item" );
868 return p_playlist->status.p_item;
871 if( !p_playlist->request.b_request && b_playstop == VLC_TRUE )
873 msg_Dbg( p_playlist,"stopping (play and stop)");
877 if( !p_playlist->request.b_request && p_playlist->status.p_item &&
878 !( p_playlist->status.p_item->i_flags & PLAYLIST_SKIP_FLAG ) )
880 msg_Dbg( p_playlist, "no-skip mode, stopping") ;
884 /* TODO: improve this (only use current node) */
885 /* TODO: use the "shuffled view" internally ? */
886 /* Random case. This is an exception: if request, but request is skip +- 1
887 * we don't go to next item but select a new random one. */
888 if( b_random && (!p_playlist->request.b_request ||
889 p_playlist->request.i_skip == 1 || p_playlist->request.i_skip == -1 ) )
891 srand( (unsigned int)mdate() );
893 for( i_count = 0; i_count < p_playlist->i_size - 1 ; i_count ++ )
896 (int)((float)p_playlist->i_size * rand() / (RAND_MAX+1.0));
897 /* Check if the item has not already been played */
898 if( p_playlist->pp_items[i_new]->i_nb_played == 0 )
901 if( i_count == p_playlist->i_size )
903 /* The whole playlist has been played: reset the counters */
906 p_playlist->pp_items[--i_count]->i_nb_played = 0;
913 p_playlist->request.i_skip = 0;
914 p_playlist->request.b_request = VLC_FALSE;
915 return p_playlist->pp_items[i_new];
918 /* Start the real work */
919 if( p_playlist->request.b_request )
921 #ifdef PLAYLIST_DEBUG
922 msg_Dbg( p_playlist,"processing request" );
924 /* We are not playing from a view */
925 if( p_playlist->request.i_view == -1 )
927 #ifdef PLAYLIST_DEBUG
928 msg_Dbg( p_playlist, "non-view mode request");
930 /* Directly select the item, just like now */
931 p_new = p_playlist->request.p_item;
932 i_skip = p_playlist->request.i_skip;
933 i_goto = p_playlist->request.i_goto;
935 if( p_playlist->i_index < 0 ) p_playlist->i_index = 0;
937 p_new = p_playlist->pp_items[p_playlist->i_index];
939 if( i_goto >= 0 && i_goto < p_playlist->i_size )
941 p_playlist->i_index = i_goto;
942 p_new = p_playlist->pp_items[p_playlist->i_index];
943 p_playlist->request.i_goto = -1;
948 if( p_playlist->i_index + i_skip < p_playlist->i_size &&
949 p_playlist->i_index + i_skip >= 0 )
951 p_playlist->i_index += i_skip;
952 p_new = p_playlist->pp_items[p_playlist->i_index];
954 p_playlist->request.i_skip = 0;
959 #ifdef PLAYLIST_DEBUG
960 msg_Dbg( p_playlist, "view mode request" );
962 p_new = p_playlist->request.p_item;
963 i_skip = p_playlist->request.i_skip;
965 /* If we are asked for a node, take its first item */
966 if( p_playlist->request.p_item == NULL && i_skip == 0 )
971 p_view = playlist_ViewFind( p_playlist,p_playlist->request.i_view );
972 p_playlist->status.p_node = p_playlist->request.p_node;
973 p_playlist->status.i_view = p_playlist->request.i_view;
976 msg_Err( p_playlist, "p_view is NULL and should not! (requested view is %i", p_playlist->request.i_view );
978 else if( i_skip > 0 )
980 for( i = i_skip; i > 0 ; i-- )
982 p_new = playlist_FindNextFromParent( p_playlist,
983 p_playlist->request.i_view,
985 p_playlist->request.p_node,
989 #ifdef PLAYLIST_DEBUG
990 msg_Dbg( p_playlist, "looping" );
992 p_new = playlist_FindNextFromParent( p_playlist,
993 p_playlist->request.i_view,
995 p_playlist->request.p_node,
997 if( p_new == NULL ) break;
1001 else if( i_skip < 0 )
1003 for( i = i_skip; i < 0 ; i++ )
1005 p_new = playlist_FindPrevFromParent( p_playlist,
1006 p_playlist->request.i_view,
1008 p_playlist->request.p_node,
1012 /* We reach the beginning of the playlist.
1013 Go back to the last item. */
1014 p_new = playlist_RecursiveFindLast( p_playlist,
1017 if( p_new == NULL ) break;
1022 /* Clear the request */
1023 p_playlist->request.b_request = VLC_FALSE;
1025 /* "Automatic" item change ( next ) */
1028 p_playlist->request_date = 0;
1030 if( p_playlist->status.i_view == -1 )
1032 #ifdef PLAYLIST_DEBUG
1033 msg_Dbg( p_playlist, "no request - old mode" );
1035 if( p_playlist->i_index + 1 < p_playlist->i_size )
1037 p_playlist->i_index++;
1038 p_new = p_playlist->pp_items[p_playlist->i_index];
1039 if( !( p_new->i_flags & PLAYLIST_SKIP_FLAG ) )
1046 if( b_loop && p_playlist->i_size > 0)
1048 p_playlist->i_index = 0;
1049 p_new = p_playlist->pp_items[0];
1055 /* We are playing with a view */
1058 #ifdef PLAYLIST_DEBUG
1059 msg_Dbg( p_playlist,"no request - from a view" );
1061 playlist_view_t *p_view =
1062 playlist_ViewFind( p_playlist,
1063 p_playlist->status.i_view );
1066 msg_Err( p_playlist, "p_view is NULL and should not! (FIXME)" );
1070 p_new = playlist_FindNextFromParent( p_playlist,
1071 p_playlist->status.i_view,
1073 p_playlist->status.p_node,
1074 p_playlist->status.p_item );
1075 if( p_new == NULL && b_loop )
1077 #ifdef PLAYLIST_DEBUG
1078 msg_Dbg( p_playlist, "looping" );
1080 p_new = playlist_FindNextFromParent( p_playlist,
1081 p_playlist->status.i_view,
1083 p_playlist->status.p_node,
1086 if( p_new != NULL && !(p_new->i_flags & PLAYLIST_SKIP_FLAG) )
1093 if( p_playlist->i_index >= 0 && p_new != NULL &&
1094 p_playlist->pp_items[p_playlist->i_index] != p_new )
1096 p_playlist->i_index = playlist_GetPositionById( p_playlist,
1097 p_new->input.i_id );
1100 #ifdef PLAYLIST_PROFILE
1101 msg_Dbg(p_playlist,"next item found in "I64Fi " us", mdate()-start );
1106 msg_Info( p_playlist, "nothing to play" );
1111 /*****************************************************************************
1112 * PlayItem: start the input thread for an item
1113 ****************************************************************************/
1114 static int PlayItem( playlist_t *p_playlist, playlist_item_t *p_item )
1118 msg_Dbg( p_playlist, "creating new input thread" );
1120 p_item->i_nb_played++;
1121 p_playlist->status.p_item = p_item;
1123 p_playlist->i_index = playlist_GetPositionById( p_playlist,
1124 p_item->input.i_id );
1126 #ifdef PLAYLIST_PROFILE
1127 if( p_playlist->request_date != 0 )
1129 msg_Dbg( p_playlist, "request processed after "I64Fi " us",
1130 mdate() - p_playlist->request_date );
1134 p_playlist->p_input = input_CreateThread( p_playlist, &p_item->input );
1136 var_AddCallback( p_playlist->p_input, "item-change",
1137 ItemChange, p_playlist );
1139 val.i_int = p_item->input.i_id;
1140 /* unlock the playlist to set the var...mmm */
1141 vlc_mutex_unlock( &p_playlist->object_lock);
1142 var_Set( p_playlist, "playlist-current", val);
1143 vlc_mutex_lock( &p_playlist->object_lock);
1149 /* Forward item change from input */
1150 static int ItemChange( vlc_object_t *p_obj, const char *psz_var,
1151 vlc_value_t oldval, vlc_value_t newval, void *param )
1153 playlist_t *p_playlist = (playlist_t *)param;
1155 //p_playlist->b_need_update = VLC_TRUE;
1156 var_SetInteger( p_playlist, "item-change", newval.i_int );
1159 /* FIXME: Make that automatic */
1160 // playlist_ViewUpdate( p_playlist, VIEW_S_AUTHOR );