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 p_playlist->i_last_id = 0;
111 p_playlist->b_go_next = VLC_TRUE;
112 p_playlist->p_input = NULL;
114 p_playlist->request_date = 0;
116 p_playlist->i_views = 0;
117 p_playlist->pp_views = NULL;
119 p_playlist->i_index = -1;
120 p_playlist->i_size = 0;
121 p_playlist->pp_items = NULL;
122 p_playlist->i_all_size = 0;
123 p_playlist->pp_all_items = 0;
125 playlist_ViewInsert( p_playlist, VIEW_CATEGORY, TITLE_CATEGORY );
126 playlist_ViewInsert( p_playlist, VIEW_ALL, TITLE_ALL );
128 p_view = playlist_ViewFind( p_playlist, VIEW_CATEGORY );
130 p_playlist->p_general = playlist_NodeCreate( p_playlist, VIEW_CATEGORY,
131 _( "General" ), p_view->p_root );
132 p_playlist->p_general->i_flags |= PLAYLIST_RO_FLAG;
134 /* Set startup status
135 * We set to simple view on startup for interfaces that don't do
137 p_view = playlist_ViewFind( p_playlist, VIEW_CATEGORY );
138 p_playlist->status.i_view = VIEW_CATEGORY;
139 p_playlist->status.p_item = NULL;
140 p_playlist->status.p_node = p_view->p_root;
141 p_playlist->request.b_request = VLC_FALSE;
142 p_playlist->status.i_status = PLAYLIST_STOPPED;
145 p_playlist->i_sort = SORT_ID;
146 p_playlist->i_order = ORDER_NORMAL;
148 /* Finally, launch the thread ! */
149 if( vlc_thread_create( p_playlist, "playlist", RunThread,
150 VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
152 msg_Err( p_playlist, "cannot spawn playlist thread" );
153 vlc_object_destroy( p_playlist );
157 /* Preparsing stuff */
158 p_playlist->p_preparse = vlc_object_create( p_playlist,
159 sizeof( playlist_preparse_t ) );
160 if( !p_playlist->p_preparse )
162 msg_Err( p_playlist, "unable to create preparser" );
163 vlc_object_destroy( p_playlist );
167 p_playlist->p_preparse->i_waiting = 0;
168 p_playlist->p_preparse->pp_waiting = NULL;
170 vlc_object_attach( p_playlist->p_preparse, p_playlist );
171 if( vlc_thread_create( p_playlist->p_preparse, "preparser",
172 RunPreparse, VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
174 msg_Err( p_playlist, "cannot spawn preparse thread" );
175 vlc_object_detach( p_playlist->p_preparse );
176 vlc_object_destroy( p_playlist->p_preparse );
180 /* The object has been initialized, now attach it */
181 vlc_object_attach( p_playlist, p_parent );
187 * Destroy the playlist.
189 * Delete all items in the playlist and free the playlist structure.
190 * \param p_playlist the playlist structure to destroy
191 * \return VLC_SUCCESS or an error
193 int playlist_Destroy( playlist_t * p_playlist )
196 p_playlist->b_die = 1;
198 for( i = 0 ; i< p_playlist->i_sds ; i++ )
200 playlist_ServicesDiscoveryRemove( p_playlist,
201 p_playlist->pp_sds[i]->psz_module );
204 vlc_thread_join( p_playlist->p_preparse );
205 vlc_thread_join( p_playlist );
207 vlc_object_detach( p_playlist->p_preparse );
209 var_Destroy( p_playlist, "intf-change" );
210 var_Destroy( p_playlist, "item-change" );
211 var_Destroy( p_playlist, "playlist-current" );
212 var_Destroy( p_playlist, "intf-popmenu" );
213 var_Destroy( p_playlist, "intf-show" );
214 var_Destroy( p_playlist, "play-and-stop" );
215 var_Destroy( p_playlist, "random" );
216 var_Destroy( p_playlist, "repeat" );
217 var_Destroy( p_playlist, "loop" );
219 playlist_Clear( p_playlist );
221 for( i = p_playlist->i_views - 1; i >= 0 ; i-- )
223 playlist_view_t *p_view = p_playlist->pp_views[i];
224 if( p_view->psz_name )
225 free( p_view->psz_name );
226 playlist_ItemDelete( p_view->p_root );
227 REMOVE_ELEM( p_playlist->pp_views, p_playlist->i_views, i );
231 vlc_object_destroy( p_playlist->p_preparse );
232 vlc_object_destroy( p_playlist );
239 * Do a playlist action.
241 * If there is something in the playlist then you can do playlist actions.
243 * Playlist lock must not be taken when calling this function
245 * \param p_playlist the playlist to do the command on
246 * \param i_query the command to do
247 * \param variable number of arguments
248 * \return VLC_SUCCESS or an error
250 int playlist_LockControl( playlist_t * p_playlist, int i_query, ... )
254 va_start( args, i_query );
255 vlc_mutex_lock( &p_playlist->object_lock );
256 i_result = playlist_vaControl( p_playlist, i_query, args );
258 vlc_mutex_unlock( &p_playlist->object_lock );
263 * Do a playlist action.
265 * If there is something in the playlist then you can do playlist actions.
267 * Playlist lock must be taken when calling this function
269 * \param p_playlist the playlist to do the command on
270 * \param i_query the command to do
271 * \param variable number of arguments
272 * \return VLC_SUCCESS or an error
274 int playlist_Control( playlist_t * p_playlist, int i_query, ... )
278 va_start( args, i_query );
279 i_result = playlist_vaControl( p_playlist, i_query, args );
285 int playlist_vaControl( playlist_t * p_playlist, int i_query, va_list args )
287 playlist_view_t *p_view;
290 #ifdef PLAYLIST_PROFILE
291 p_playlist->request_date = mdate();
294 if( p_playlist->i_size <= 0 )
302 p_playlist->status.i_status = PLAYLIST_STOPPED;
303 p_playlist->request.b_request = VLC_TRUE;
306 case PLAYLIST_ITEMPLAY:
307 p_playlist->status.i_status = PLAYLIST_RUNNING;
308 p_playlist->request.i_skip = 0;
309 p_playlist->request.b_request = VLC_TRUE;
310 p_playlist->request.p_item = (playlist_item_t *)va_arg( args,
312 p_playlist->request.i_view = p_playlist->status.i_view;
313 p_view = playlist_ViewFind( p_playlist, p_playlist->status.i_view );
316 p_playlist->request.p_node = p_view->p_root;
320 p_playlist->request.p_node = NULL;
324 case PLAYLIST_VIEWPLAY:
325 p_playlist->status.i_status = PLAYLIST_RUNNING;
326 p_playlist->request.i_skip = 0;
327 p_playlist->request.b_request = VLC_TRUE;
328 p_playlist->request.i_view = (int)va_arg( args,int );
329 p_playlist->request.p_node = (playlist_item_t *)va_arg( args,
331 p_playlist->request.p_item = (playlist_item_t *)va_arg( args,
334 /* If we select a node, play only it.
335 * If we select an item, continue */
336 if( p_playlist->request.p_item == NULL ||
337 ! p_playlist->request.p_node->i_flags & PLAYLIST_SKIP_FLAG )
339 p_playlist->b_go_next = VLC_FALSE;
343 p_playlist->b_go_next = VLC_TRUE;
348 p_playlist->status.i_status = PLAYLIST_RUNNING;
350 if( p_playlist->p_input )
352 val.i_int = PLAYING_S;
353 var_Set( p_playlist->p_input, "state", val );
357 /* FIXME : needed ? */
358 p_playlist->request.b_request = VLC_TRUE;
359 p_playlist->request.i_view = p_playlist->status.i_view;
360 p_playlist->request.p_node = p_playlist->status.p_node;
361 p_playlist->request.p_item = p_playlist->status.p_item;
362 p_playlist->request.i_skip = 0;
363 p_playlist->request.i_goto = -1;
366 case PLAYLIST_AUTOPLAY:
367 p_playlist->status.i_status = PLAYLIST_RUNNING;
369 p_playlist->request.b_request = VLC_FALSE;
374 if( p_playlist->p_input )
375 var_Get( p_playlist->p_input, "state", &val );
377 if( val.i_int == PAUSE_S )
379 p_playlist->status.i_status = PLAYLIST_RUNNING;
380 if( p_playlist->p_input )
382 val.i_int = PLAYING_S;
383 var_Set( p_playlist->p_input, "state", val );
388 p_playlist->status.i_status = PLAYLIST_PAUSED;
389 if( p_playlist->p_input )
392 var_Set( p_playlist->p_input, "state", val );
398 if( p_playlist->status.i_view > -1 )
400 p_playlist->request.p_node = p_playlist->status.p_node;
401 p_playlist->request.p_item = p_playlist->status.p_item;
403 p_playlist->request.i_skip = (int) va_arg( args, int );
404 p_playlist->request.b_request = VLC_TRUE;
408 p_playlist->status.i_status = PLAYLIST_RUNNING;
409 p_playlist->request.p_node = NULL;
410 p_playlist->request.p_item = NULL;
411 p_playlist->request.i_view = -1;
412 p_playlist->request.i_goto = (int) va_arg( args, int );
413 p_playlist->request.b_request = VLC_TRUE;
417 msg_Err( p_playlist, "unimplemented playlist query" );
425 int playlist_PreparseEnqueue( playlist_t *p_playlist,
426 input_item_t *p_item )
428 vlc_mutex_lock( &p_playlist->p_preparse->object_lock );
429 INSERT_ELEM( p_playlist->p_preparse->pp_waiting,
430 p_playlist->p_preparse->i_waiting,
431 p_playlist->p_preparse->i_waiting,
433 vlc_mutex_unlock( &p_playlist->p_preparse->object_lock );
438 /* Destroy remaining objects */
439 static mtime_t ObjectGarbageCollector( playlist_t *p_playlist, int i_type,
440 mtime_t destroy_date )
444 if( destroy_date > mdate() ) return destroy_date;
446 if( destroy_date == 0 )
448 /* give a little time */
449 return mdate() + I64C(1000000);
453 while( ( p_obj = vlc_object_find( p_playlist, i_type, FIND_CHILD ) ) )
455 if( p_obj->p_parent != (vlc_object_t*)p_playlist )
457 /* only first child (ie unused) */
458 vlc_object_release( p_obj );
461 if( i_type == VLC_OBJECT_VOUT )
463 msg_Dbg( p_playlist, "garbage collector destroying 1 vout" );
464 vlc_object_detach( p_obj );
465 vlc_object_release( p_obj );
466 vout_Destroy( (vout_thread_t *)p_obj );
468 else if( i_type == VLC_OBJECT_SOUT )
470 vlc_object_release( p_obj );
471 sout_DeleteInstance( (sout_instance_t*)p_obj );
478 /*****************************************************************************
479 * RunThread: main playlist thread
480 *****************************************************************************/
481 static void RunThread ( playlist_t *p_playlist )
484 playlist_item_t *p_item;
486 mtime_t i_vout_destroyed_date = 0;
487 mtime_t i_sout_destroyed_date = 0;
489 playlist_item_t *p_autodelete_item = NULL;
491 /* Tell above that we're ready */
492 vlc_thread_ready( p_playlist );
494 while( !p_playlist->b_die )
496 vlc_mutex_lock( &p_playlist->object_lock );
498 /* First, check if we have something to do */
499 /* FIXME : this can be called several times */
500 if( p_playlist->request.b_request )
502 #ifdef PLAYLIST_PROFILE
503 msg_Dbg(p_playlist, "beginning processing of request, "
504 I64Fi" us ", mdate() - p_playlist->request_date );
506 /* Stop the existing input */
507 if( p_playlist->p_input )
509 input_StopThread( p_playlist->p_input );
511 /* The code below will start the next input for us */
512 if( p_playlist->status.i_status == PLAYLIST_STOPPED )
514 p_playlist->request.b_request = VLC_FALSE;
518 /* If there is an input, check that it doesn't need to die. */
519 if( p_playlist->p_input )
521 /* This input is dead. Remove it ! */
522 if( p_playlist->p_input->b_dead )
524 input_thread_t *p_input;
526 p_input = p_playlist->p_input;
527 p_playlist->p_input = NULL;
529 /* Release the playlist lock, because we may get stuck
530 * in input_DestroyThread() for some time. */
531 vlc_mutex_unlock( &p_playlist->object_lock );
534 input_DestroyThread( p_input );
536 /* Unlink current input
537 * (_after_ input_DestroyThread for vout garbage collector) */
538 vlc_object_detach( p_input );
541 vlc_object_destroy( p_input );
543 i_vout_destroyed_date = 0;
544 i_sout_destroyed_date = 0;
546 if( p_playlist->status.p_item->i_flags
547 & PLAYLIST_REMOVE_FLAG )
549 playlist_ItemDelete( p_item );
550 p_playlist->status.p_item = NULL;
555 /* This input is dying, let him do */
556 else if( p_playlist->p_input->b_die )
560 /* This input has finished, ask him to die ! */
561 else if( p_playlist->p_input->b_error
562 || p_playlist->p_input->b_eof )
564 /* TODO FIXME XXX TODO FIXME XXX */
565 /* Check for autodeletion */
567 if( p_playlist->status.p_item->i_flags & PLAYLIST_DEL_FLAG )
569 p_autodelete_item = p_playlist->status.p_item;
571 input_StopThread( p_playlist->p_input );
572 /* Select the next playlist item */
573 vlc_mutex_unlock( &p_playlist->object_lock );
576 else if( p_playlist->p_input->i_state != INIT_S )
578 vlc_mutex_unlock( &p_playlist->object_lock );
579 i_vout_destroyed_date =
580 ObjectGarbageCollector( p_playlist, VLC_OBJECT_VOUT,
581 i_vout_destroyed_date );
582 i_sout_destroyed_date =
583 ObjectGarbageCollector( p_playlist, VLC_OBJECT_SOUT,
584 i_sout_destroyed_date );
585 vlc_mutex_lock( &p_playlist->object_lock );
588 else if( p_playlist->status.i_status != PLAYLIST_STOPPED )
590 /* Start another input.
591 * Get the next item to play */
592 p_item = NextItem( p_playlist );
598 if( p_autodelete_item )
600 playlist_Delete( p_playlist,
601 p_autodelete_item->input.i_id );
602 p_autodelete_item = NULL;
604 p_playlist->status.i_status = PLAYLIST_STOPPED;
605 vlc_mutex_unlock( &p_playlist->object_lock );
609 PlayItem( p_playlist, p_item );
611 if( p_autodelete_item )
613 playlist_Delete( p_playlist, p_autodelete_item->input.i_id );
614 p_autodelete_item = NULL;
617 else if( p_playlist->status.i_status == PLAYLIST_STOPPED )
619 /* Collect garbage */
620 vlc_mutex_unlock( &p_playlist->object_lock );
621 i_sout_destroyed_date =
622 ObjectGarbageCollector( p_playlist, VLC_OBJECT_SOUT, mdate() );
623 i_vout_destroyed_date =
624 ObjectGarbageCollector( p_playlist, VLC_OBJECT_VOUT, mdate() );
625 vlc_mutex_lock( &p_playlist->object_lock );
627 vlc_mutex_unlock( &p_playlist->object_lock );
629 msleep( INTF_IDLE_SLEEP / 2 );
631 /* Stop sleeping earlier if we have work */
632 /* TODO : statistics about this */
633 if ( p_playlist->request.b_request &&
634 p_playlist->status.i_status == PLAYLIST_RUNNING )
639 msleep( INTF_IDLE_SLEEP / 2 );
644 /* If there is an input, kill it */
647 vlc_mutex_lock( &p_playlist->object_lock );
649 if( p_playlist->p_input == NULL )
651 vlc_mutex_unlock( &p_playlist->object_lock );
655 if( p_playlist->p_input->b_dead )
657 input_thread_t *p_input;
659 /* Unlink current input */
660 p_input = p_playlist->p_input;
661 p_playlist->p_input = NULL;
662 vlc_mutex_unlock( &p_playlist->object_lock );
665 input_DestroyThread( p_input );
666 /* Unlink current input (_after_ input_DestroyThread for vout
667 * garbage collector)*/
668 vlc_object_detach( p_input );
671 vlc_object_destroy( p_input );
674 else if( p_playlist->p_input->b_die )
676 /* This input is dying, leave him alone */
679 else if( p_playlist->p_input->b_error || p_playlist->p_input->b_eof )
681 input_StopThread( p_playlist->p_input );
682 vlc_mutex_unlock( &p_playlist->object_lock );
687 p_playlist->p_input->b_eof = 1;
690 vlc_mutex_unlock( &p_playlist->object_lock );
692 msleep( INTF_IDLE_SLEEP );
695 /* close all remaining sout */
696 while( ( p_obj = vlc_object_find( p_playlist,
697 VLC_OBJECT_SOUT, FIND_CHILD ) ) )
699 vlc_object_release( p_obj );
700 sout_DeleteInstance( (sout_instance_t*)p_obj );
703 /* close all remaining vout */
704 while( ( p_obj = vlc_object_find( p_playlist,
705 VLC_OBJECT_VOUT, FIND_CHILD ) ) )
707 vlc_object_detach( p_obj );
708 vlc_object_release( p_obj );
709 vout_Destroy( (vout_thread_t *)p_obj );
713 /* Queue for items to preparse */
714 static void RunPreparse ( playlist_preparse_t *p_obj )
716 playlist_t *p_playlist = (playlist_t *)p_obj->p_parent;
719 /* Tell above that we're ready */
720 vlc_thread_ready( p_obj );
722 while( !p_playlist->b_die )
724 vlc_mutex_lock( &p_obj->object_lock );
726 if( p_obj->i_waiting > 0 )
728 input_item_t *p_current = p_obj->pp_waiting[0];
729 REMOVE_ELEM( p_obj->pp_waiting, p_obj->i_waiting, 0 );
730 vlc_mutex_unlock( &p_obj->object_lock );
731 input_Preparse( p_playlist, p_current );
732 var_SetInteger( p_playlist, "item-change", p_current->i_id );
733 vlc_mutex_lock( &p_obj->object_lock );
735 b_sleep = ( p_obj->i_waiting == 0 );
737 vlc_mutex_unlock( &p_obj->object_lock );
739 if( p_obj->i_waiting == 0 )
741 msleep( INTF_IDLE_SLEEP );
746 /*****************************************************************************
748 *****************************************************************************
749 * This function calculates the next playlist item, depending
750 * on the playlist course mode (forward, backward, random, view,...).
751 *****************************************************************************/
752 static playlist_item_t * NextItem( playlist_t *p_playlist )
754 playlist_item_t *p_new = NULL;
755 int i_skip,i_goto,i, i_new, i_count ;
756 playlist_view_t *p_view;
758 vlc_bool_t b_loop = var_GetBool( p_playlist, "loop");
759 vlc_bool_t b_random = var_GetBool( p_playlist, "random" );
760 vlc_bool_t b_repeat = var_GetBool( p_playlist, "repeat" );
761 vlc_bool_t b_playstop = var_GetBool( p_playlist, "play-and-stop" );
763 #ifdef PLAYLIST_PROFILE
764 /* Calculate time needed */
765 int64_t start = mdate();
769 /* Handle quickly a few special cases */
771 /* No items to play */
772 if( p_playlist->i_size == 0 )
774 msg_Info( p_playlist, "playlist is empty" );
777 /* Nothing requested */
778 if( !p_playlist->request.b_request && p_playlist->status.p_item == NULL )
780 msg_Dbg( p_playlist,"nothing requested, starting" );
783 /* Repeat and play/stop */
784 if( !p_playlist->request.b_request && b_repeat == VLC_TRUE )
786 msg_Dbg( p_playlist,"repeating item" );
787 return p_playlist->status.p_item;
790 if( !p_playlist->request.b_request && b_playstop == VLC_TRUE )
792 msg_Dbg( p_playlist,"stopping (play and stop)");
796 if( !p_playlist->request.b_request && p_playlist->status.p_item &&
797 !( p_playlist->status.p_item->i_flags & PLAYLIST_SKIP_FLAG ) )
799 msg_Dbg( p_playlist, "no-skip mode, stopping") ;
803 /* TODO: improve this (only use current node) */
804 /* TODO: use the "shuffled view" internally ? */
805 /* Random case. This is an exception: if request, but request is skip +- 1
806 * we don't go to next item but select a new random one. */
807 if( b_random && (!p_playlist->request.b_request ||
808 p_playlist->request.i_skip == 1 || p_playlist->request.i_skip == -1 ) )
810 srand( (unsigned int)mdate() );
812 for( i_count = 0; i_count < p_playlist->i_size - 1 ; i_count ++ )
815 (int)((float)p_playlist->i_size * rand() / (RAND_MAX+1.0));
816 /* Check if the item has not already been played */
817 if( p_playlist->pp_items[i_new]->i_nb_played == 0 )
820 if( i_count == p_playlist->i_size )
822 /* The whole playlist has been played: reset the counters */
825 p_playlist->pp_items[--i_count]->i_nb_played = 0;
832 p_playlist->request.i_skip = 0;
833 p_playlist->request.b_request = VLC_FALSE;
834 return p_playlist->pp_items[i_new];
837 /* Start the real work */
838 if( p_playlist->request.b_request )
840 #ifdef PLAYLIST_DEBUG
841 msg_Dbg( p_playlist,"processing request" );
843 /* We are not playing from a view */
844 if( p_playlist->request.i_view == -1 )
846 #ifdef PLAYLIST_DEBUG
847 msg_Dbg( p_playlist, "non-view mode request");
849 /* Directly select the item, just like now */
850 i_skip = p_playlist->request.i_skip;
851 i_goto = p_playlist->request.i_goto;
853 if( p_playlist->i_index == -1 ) p_playlist->i_index = 0;
854 p_new = p_playlist->pp_items[p_playlist->i_index];
856 if( i_goto >= 0 && i_goto < p_playlist->i_size )
858 p_playlist->i_index = i_goto;
859 p_new = p_playlist->pp_items[p_playlist->i_index];
860 p_playlist->request.i_goto = -1;
865 if( p_playlist->i_index + i_skip < p_playlist->i_size &&
866 p_playlist->i_index + i_skip >= 0 )
868 p_playlist->i_index += i_skip;
869 p_new = p_playlist->pp_items[p_playlist->i_index];
871 p_playlist->request.i_skip = 0;
876 #ifdef PLAYLIST_DEBUG
877 msg_Dbg( p_playlist, "view mode request" );
879 p_new = p_playlist->request.p_item;
880 i_skip = p_playlist->request.i_skip;
882 /* If we are asked for a node, take its first item */
883 if( p_playlist->request.p_item == NULL && i_skip == 0 )
888 p_view = playlist_ViewFind( p_playlist,p_playlist->request.i_view );
889 p_playlist->status.p_node = p_playlist->request.p_node;
890 p_playlist->status.i_view = p_playlist->request.i_view;
893 msg_Err( p_playlist, "p_view is NULL and should not! (FIXME)" );
895 else if( i_skip > 0 )
897 for( i = i_skip; i > 0 ; i-- )
899 p_new = playlist_FindNextFromParent( p_playlist,
900 p_playlist->request.i_view,
902 p_playlist->request.p_node,
908 p_new = playlist_FindNextFromParent( p_playlist,
909 p_playlist->request.i_view,
911 p_playlist->request.p_node,
913 if( p_new == NULL ) break;
922 else if( i_skip < 0 )
924 for( i = i_skip; i < 0 ; i++ )
926 p_new = playlist_FindPrevFromParent( p_playlist,
927 p_playlist->request.i_view,
929 p_playlist->request.p_node,
931 if( p_new == NULL ) break;
936 /* Clear the request */
937 p_playlist->request.b_request = VLC_FALSE;
939 /* "Automatic" item change ( next ) */
942 p_playlist->request_date = 0;
944 if( p_playlist->status.i_view == -1 )
946 if( p_playlist->i_index + 1 < p_playlist->i_size )
948 p_playlist->i_index++;
949 p_new = p_playlist->pp_items[p_playlist->i_index];
950 if( !( p_new->i_flags & PLAYLIST_SKIP_FLAG ) )
957 if( b_loop && p_playlist->i_size > 0)
959 p_playlist->i_index = 0;
960 p_new = p_playlist->pp_items[0];
966 /* We are playing with a view */
969 playlist_view_t *p_view =
970 playlist_ViewFind( p_playlist,
971 p_playlist->status.i_view );
974 msg_Err( p_playlist, "p_view is NULL and should not! (FIXME)" );
978 p_new = playlist_FindNextFromParent( p_playlist,
979 p_playlist->status.i_view,
981 p_playlist->status.p_node,
982 p_playlist->status.p_item );
983 if( p_new == NULL && b_loop )
985 p_new = playlist_FindNextFromParent( p_playlist,
986 p_playlist->status.i_view,
988 p_playlist->status.p_node,
991 if( p_new != NULL && !(p_new->i_flags & PLAYLIST_SKIP_FLAG) )
998 if( p_playlist->i_index >= 0 && p_new != NULL &&
999 p_playlist->pp_items[p_playlist->i_index] != p_new )
1001 p_playlist->i_index = playlist_GetPositionById( p_playlist,
1002 p_new->input.i_id );
1005 #ifdef PLAYLIST_PROFILE
1006 msg_Dbg(p_playlist,"next item found in "I64Fi " us", mdate()-start );
1011 msg_Info( p_playlist, "nothing to play" );
1016 /*****************************************************************************
1017 * PlayItem: start the input thread for an item
1018 ****************************************************************************/
1019 static int PlayItem( playlist_t *p_playlist, playlist_item_t *p_item )
1023 msg_Dbg( p_playlist, "creating new input thread" );
1025 p_item->i_nb_played++;
1026 p_playlist->status.p_item = p_item;
1028 p_playlist->i_index = playlist_GetPositionById( p_playlist,
1029 p_item->input.i_id );
1031 #ifdef PLAYLIST_PROFILE
1032 if( p_playlist->request_date != 0 )
1034 msg_Dbg( p_playlist, "request processed after "I64Fi " us",
1035 mdate() - p_playlist->request_date );
1039 p_playlist->p_input = input_CreateThread( p_playlist, &p_item->input );
1041 var_AddCallback( p_playlist->p_input, "item-change",
1042 ItemChange, p_playlist );
1044 val.i_int = p_item->input.i_id;
1045 /* unlock the playlist to set the var...mmm */
1046 vlc_mutex_unlock( &p_playlist->object_lock);
1047 var_Set( p_playlist, "playlist-current", val);
1048 vlc_mutex_lock( &p_playlist->object_lock);
1054 /* Forward item change from input */
1055 static int ItemChange( vlc_object_t *p_obj, const char *psz_var,
1056 vlc_value_t oldval, vlc_value_t newval, void *param )
1058 playlist_t *p_playlist = (playlist_t *)param;
1060 //p_playlist->b_need_update = VLC_TRUE;
1061 var_SetInteger( p_playlist, "item-change", newval.i_int );
1064 /* FIXME: Make that automatic */
1065 // playlist_ViewUpdate( p_playlist, VIEW_S_AUTHOR );