]> git.sesse.net Git - vlc/blob - src/playlist/playlist.c
include services discovery in interface
[vlc] / src / playlist / playlist.c
1 /*****************************************************************************
2  * playlist.c : Playlist management functions
3  *****************************************************************************
4  * Copyright (C) 1999-2004 VideoLAN
5  * $Id$
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *          ClĂ©ment Stenac <zorglub@videolan.org>
9  *
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.
14  *
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.
19  *
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() */
27
28 #include <vlc/vlc.h>
29 #include <vlc/vout.h>
30 #include <vlc/sout.h>
31 #include <vlc/input.h>
32
33 #include "vlc_playlist.h"
34
35 #define TITLE_CATEGORY N_( "By category" )
36 #define TITLE_SIMPLE   N_( "Manually added" )
37 #define TITLE_ALL      N_( "All items, unsorted" )
38
39 #define PLAYLIST_PROFILE 1
40
41 /*****************************************************************************
42  * Local prototypes
43  *****************************************************************************/
44 static void RunThread ( playlist_t * );
45 static playlist_item_t * NextItem  ( playlist_t * );
46 static void PlayItem  ( playlist_t *, playlist_item_t * );
47
48 static int ItemChange( vlc_object_t *, const char *,
49                        vlc_value_t, vlc_value_t, void * );
50
51 int playlist_vaControl( playlist_t * p_playlist, int i_query, va_list args );
52
53
54 /**
55  * Create playlist
56  *
57  * Create a playlist structure.
58  * \param p_parent the vlc object that is to be the parent of this playlist
59  * \return a pointer to the created playlist, or NULL on error
60  */
61 playlist_t * __playlist_Create ( vlc_object_t *p_parent )
62 {
63     int i_index;
64     playlist_t *p_playlist;
65     playlist_view_t *p_view;
66     vlc_value_t     val;
67
68     /* Allocate structure */
69     p_playlist = vlc_object_create( p_parent, VLC_OBJECT_PLAYLIST );
70     if( !p_playlist )
71     {
72         msg_Err( p_parent, "out of memory" );
73         return NULL;
74     }
75
76     /* These variables control updates */
77     var_Create( p_playlist, "intf-change", VLC_VAR_BOOL );
78     val.b_bool = VLC_TRUE;
79     var_Set( p_playlist, "intf-change", val );
80
81     var_Create( p_playlist, "item-change", VLC_VAR_INTEGER );
82     val.i_int = -1;
83     var_Set( p_playlist, "item-change", val );
84
85     var_Create( p_playlist, "playlist-current", VLC_VAR_INTEGER );
86     val.i_int = -1;
87     var_Set( p_playlist, "playlist-current", val );
88
89     var_Create( p_playlist, "intf-popupmenu", VLC_VAR_BOOL );
90
91     var_Create( p_playlist, "intf-show", VLC_VAR_BOOL );
92     val.b_bool = VLC_TRUE;
93     var_Set( p_playlist, "intf-show", val );
94
95
96     /* Variables to control playback */
97     var_CreateGetBool( p_playlist, "play-and-stop" );
98     var_CreateGetBool( p_playlist, "random" );
99     var_CreateGetBool( p_playlist, "repeat" );
100     var_CreateGetBool( p_playlist, "loop" );
101
102     /* Initialise data structures */
103     p_playlist->b_go_next = VLC_TRUE;
104     p_playlist->p_input = NULL;
105
106     p_playlist->request_date = 0;
107
108     p_playlist->i_views = 0;
109     p_playlist->pp_views = NULL;
110
111     p_playlist->i_index = -1;
112     p_playlist->i_size = 0;
113     p_playlist->pp_items = NULL;
114
115     playlist_ViewInsert( p_playlist, VIEW_CATEGORY, TITLE_CATEGORY );
116     playlist_ViewInsert( p_playlist, VIEW_SIMPLE, TITLE_SIMPLE );
117     playlist_ViewInsert( p_playlist, VIEW_ALL, TITLE_ALL );
118
119     p_view = playlist_ViewFind( p_playlist, VIEW_CATEGORY );
120
121     p_playlist->p_general = playlist_NodeCreate( p_playlist, VIEW_CATEGORY,
122                                         _( "General" ), p_view->p_root );
123
124     /* Set startup status
125      * We set to simple view on startup for interfaces that don't do
126      * anything */
127     p_view = playlist_ViewFind( p_playlist, VIEW_SIMPLE );
128     p_playlist->status.i_view = VIEW_SIMPLE;
129     p_playlist->status.p_item = NULL;
130     p_playlist->status.p_node = p_view->p_root;
131     p_playlist->request.b_request = VLC_FALSE;
132     p_playlist->status.i_status = PLAYLIST_STOPPED;
133
134
135     p_playlist->i_last_id = 0;
136     p_playlist->i_sort = SORT_ID;
137     p_playlist->i_order = ORDER_NORMAL;
138
139     /* Finally, launch the thread ! */
140     if( vlc_thread_create( p_playlist, "playlist", RunThread,
141                            VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
142     {
143         msg_Err( p_playlist, "cannot spawn playlist thread" );
144         vlc_object_destroy( p_playlist );
145         return NULL;
146     }
147
148     /* The object has been initialized, now attach it */
149     vlc_object_attach( p_playlist, p_parent );
150
151     return p_playlist;
152 }
153
154 /**
155  * Destroy the playlist.
156  *
157  * Delete all items in the playlist and free the playlist structure.
158  * \param p_playlist the playlist structure to destroy
159  */
160 void playlist_Destroy( playlist_t * p_playlist )
161 {
162     int i;
163     p_playlist->b_die = 1;
164
165     for( i = 0 ; i< p_playlist->i_sds ; i++ )
166     {
167         playlist_ServicesDiscoveryRemove( p_playlist,
168                                           p_playlist->pp_sds[i]->psz_module );
169     }
170
171     vlc_thread_join( p_playlist );
172
173     var_Destroy( p_playlist, "intf-change" );
174     var_Destroy( p_playlist, "item-change" );
175     var_Destroy( p_playlist, "playlist-current" );
176     var_Destroy( p_playlist, "intf-popmenu" );
177     var_Destroy( p_playlist, "intf-show" );
178     var_Destroy( p_playlist, "play-and-stop" );
179     var_Destroy( p_playlist, "random" );
180     var_Destroy( p_playlist, "repeat" );
181     var_Destroy( p_playlist, "loop" );
182
183     playlist_Clear( p_playlist );
184
185     for( i = p_playlist->i_views - 1; i >= 0 ; i-- )
186     {
187         playlist_view_t *p_view = p_playlist->pp_views[i];
188         if( p_view->psz_name )
189             free( p_view->psz_name );
190         playlist_ItemDelete( p_view->p_root );
191         REMOVE_ELEM( p_playlist->pp_views, p_playlist->i_views, i );
192         free( p_view );
193     }
194
195     vlc_object_destroy( p_playlist );
196 }
197
198
199 /**
200  * Do a playlist action.
201  *
202  * If there is something in the playlist then you can do playlist actions.
203  *
204  * Playlist lock must not be taken when calling this function
205  *
206  * \param p_playlist the playlist to do the command on
207  * \param i_query the command to do
208  * \param variable number of arguments
209  * \return VLC_SUCCESS or an error
210  */
211 int playlist_Control( playlist_t * p_playlist, int i_query, ... )
212 {
213     va_list args;
214     int i_result;
215     va_start( args, i_query );
216     i_result = playlist_vaControl( p_playlist, i_query, args );
217     va_end( args );
218
219     return i_result;
220 }
221
222 int playlist_vaControl( playlist_t * p_playlist, int i_query, va_list args )
223 {
224     vlc_mutex_lock( &p_playlist->object_lock );
225
226     playlist_view_t *p_view;
227     vlc_value_t val;
228
229 #ifdef PLAYLIST_PROFILE
230     p_playlist->request_date = mdate();
231 #endif
232
233     if( p_playlist->i_size <= 0 )
234     {
235         vlc_mutex_unlock( &p_playlist->object_lock );
236         return VLC_EGENERIC;
237     }
238
239     switch( i_query )
240     {
241     case PLAYLIST_STOP:
242         p_playlist->status.i_status = PLAYLIST_STOPPED;
243         p_playlist->request.b_request = VLC_TRUE;
244         break;
245
246     case PLAYLIST_ITEMPLAY:
247         p_playlist->status.i_status = PLAYLIST_RUNNING;
248         p_playlist->request.i_skip = 0;
249         p_playlist->request.b_request = VLC_TRUE;
250         p_playlist->request.p_item = (playlist_item_t *)va_arg( args,
251                                                    playlist_item_t *);
252         p_playlist->request.i_view = p_playlist->status.i_view;
253         p_view = playlist_ViewFind( p_playlist, p_playlist->status.i_view );
254         p_playlist->request.p_node = p_view->p_root;
255         break;
256
257     case PLAYLIST_VIEWPLAY:
258         p_playlist->status.i_status = PLAYLIST_RUNNING;
259         p_playlist->request.i_skip = 0;
260         p_playlist->request.b_request = VLC_TRUE;
261         p_playlist->request.i_view = (int)va_arg( args,int );
262         p_playlist->request.p_node = (playlist_item_t *)va_arg( args,
263                                                         playlist_item_t *);
264         p_playlist->request.p_item = (playlist_item_t *)va_arg( args,
265                                                         playlist_item_t *);
266
267         /* If we select a node, play only it.
268          * If we select an item, continue */
269         if( p_playlist->request.p_item == NULL ||
270             ! p_playlist->request.p_node->i_flags & PLAYLIST_SKIP_FLAG )
271         {
272             p_playlist->b_go_next = VLC_FALSE;
273         }
274         else
275         {
276             p_playlist->b_go_next = VLC_TRUE;
277         }
278         break;
279
280     case PLAYLIST_PLAY:
281         p_playlist->status.i_status = PLAYLIST_RUNNING;
282
283         if( p_playlist->p_input )
284         {
285             val.i_int = PLAYING_S;
286             var_Set( p_playlist->p_input, "state", val );
287             break;
288         }
289
290         /* FIXME : needed ? */
291         p_playlist->request.b_request = VLC_TRUE;
292         p_playlist->request.i_view = p_playlist->status.i_view;
293         p_playlist->request.p_node = p_playlist->status.p_node;
294         p_playlist->request.p_item = p_playlist->status.p_item;
295         p_playlist->request.i_skip = 0;
296         p_playlist->request.i_goto = -1;
297         break;
298
299     case PLAYLIST_PAUSE:
300         val.i_int = 0;
301         if( p_playlist->p_input )
302             var_Get( p_playlist->p_input, "state", &val );
303
304         if( val.i_int == PAUSE_S )
305         {
306             p_playlist->status.i_status = PLAYLIST_RUNNING;
307             if( p_playlist->p_input )
308             {
309                 val.i_int = PLAYING_S;
310                 var_Set( p_playlist->p_input, "state", val );
311             }
312         }
313         else
314         {
315             p_playlist->status.i_status = PLAYLIST_PAUSED;
316             if( p_playlist->p_input )
317             {
318                 val.i_int = PAUSE_S;
319                 var_Set( p_playlist->p_input, "state", val );
320             }
321         }
322         break;
323
324     case PLAYLIST_SKIP:
325         if( p_playlist->status.i_view > -1 )
326         {
327             p_playlist->request.p_node = p_playlist->status.p_node;
328             p_playlist->request.p_item = p_playlist->status.p_item;
329         }
330         p_playlist->request.i_skip = (int) va_arg( args, int );
331         p_playlist->request.b_request = VLC_TRUE;
332         break;
333
334     case PLAYLIST_GOTO:
335         p_playlist->status.i_status = PLAYLIST_RUNNING;
336         p_playlist->request.p_node = NULL;
337         p_playlist->request.p_item = NULL;
338         p_playlist->request.i_view = -1;
339         p_playlist->request.i_goto = (int) va_arg( args, int );
340         p_playlist->request.b_request = VLC_TRUE;
341         break;
342
343     default:
344         msg_Err( p_playlist, "unimplemented playlist query" );
345         return VLC_EBADVAR;
346         break;
347     }
348
349     vlc_mutex_unlock( &p_playlist->object_lock );
350     return VLC_SUCCESS;
351 }
352
353
354 /* Destroy remaining objects */
355 static mtime_t ObjectGarbageCollector( playlist_t *p_playlist, int i_type,
356                                        mtime_t destroy_date )
357 {
358     vlc_object_t *p_obj;
359
360     if( destroy_date > mdate() ) return destroy_date;
361
362     if( destroy_date == 0 )
363     {
364         /* give a little time */
365         return mdate() + I64C(1000000);
366     }
367     else
368     {
369         while( ( p_obj = vlc_object_find( p_playlist, i_type, FIND_CHILD ) ) )
370         {
371             if( p_obj->p_parent != (vlc_object_t*)p_playlist )
372             {
373                 /* only first child (ie unused) */
374                 vlc_object_release( p_obj );
375                 break;
376             }
377             if( i_type == VLC_OBJECT_VOUT )
378             {
379                 msg_Dbg( p_playlist, "garbage collector destroying 1 vout" );
380                 vlc_object_detach( p_obj );
381                 vlc_object_release( p_obj );
382                 vout_Destroy( (vout_thread_t *)p_obj );
383             }
384             else if( i_type == VLC_OBJECT_SOUT )
385             {
386                 vlc_object_release( p_obj );
387                 sout_DeleteInstance( (sout_instance_t*)p_obj );
388             }
389         }
390         return 0;
391     }
392 }
393
394 /*****************************************************************************
395  * RunThread: main playlist thread
396  *****************************************************************************/
397 static void RunThread ( playlist_t *p_playlist )
398 {
399     vlc_object_t *p_obj;
400     playlist_item_t *p_item;
401
402     mtime_t    i_vout_destroyed_date = 0;
403     mtime_t    i_sout_destroyed_date = 0;
404
405     playlist_item_t *p_autodelete_item = NULL;
406
407     /* Tell above that we're ready */
408     vlc_thread_ready( p_playlist );
409
410     while( !p_playlist->b_die )
411     {
412         vlc_mutex_lock( &p_playlist->object_lock );
413
414         /* First, check if we have something to do */
415         /* FIXME : this can be called several times */
416         if( p_playlist->request.b_request )
417         {
418 #ifdef PLAYLIST_PROFILE
419             msg_Dbg(p_playlist, "beginning processing of request, "
420                          I64Fi" us ", mdate() - p_playlist->request_date );
421 #endif
422             /* Stop the existing input */
423             if( p_playlist->p_input )
424             {
425                 input_StopThread( p_playlist->p_input );
426             }
427             /* The code below will start the next input for us */
428             if( p_playlist->status.i_status == PLAYLIST_STOPPED )
429             {
430                 p_playlist->request.b_request = VLC_FALSE;
431             }
432         }
433
434         /* If there is an input, check that it doesn't need to die. */
435         if( p_playlist->p_input )
436         {
437             /* This input is dead. Remove it ! */
438             if( p_playlist->p_input->b_dead )
439             {
440                 input_thread_t *p_input;
441
442                 p_input = p_playlist->p_input;
443                 p_playlist->p_input = NULL;
444
445                 /* Release the playlist lock, because we may get stuck
446                  * in input_DestroyThread() for some time. */
447                 vlc_mutex_unlock( &p_playlist->object_lock );
448
449                 /* Destroy input */
450                 input_DestroyThread( p_input );
451
452                 /* Unlink current input
453                  * (_after_ input_DestroyThread for vout garbage collector) */
454                 vlc_object_detach( p_input );
455
456                 /* Destroy object */
457                 vlc_object_destroy( p_input );
458
459                 i_vout_destroyed_date = 0;
460                 i_sout_destroyed_date = 0;
461
462                 continue;
463             }
464             /* This input is dying, let him do */
465             else if( p_playlist->p_input->b_die )
466             {
467                 ;
468             }
469             /* This input has finished, ask him to die ! */
470             else if( p_playlist->p_input->b_error
471                       || p_playlist->p_input->b_eof )
472             {
473                 /* TODO FIXME XXX TODO FIXME XXX */
474                 /* Check for autodeletion */
475
476                 if( p_playlist->status.p_item->i_flags & PLAYLIST_DEL_FLAG )
477                 {
478                     p_autodelete_item = p_playlist->status.p_item;
479                 }
480                 input_StopThread( p_playlist->p_input );
481                 /* Select the next playlist item */
482                 vlc_mutex_unlock( &p_playlist->object_lock );
483                 continue;
484             }
485             else if( p_playlist->p_input->i_state != INIT_S )
486             {
487                 vlc_mutex_unlock( &p_playlist->object_lock );
488                 i_vout_destroyed_date =
489                     ObjectGarbageCollector( p_playlist, VLC_OBJECT_VOUT,
490                                             i_vout_destroyed_date );
491                 i_sout_destroyed_date =
492                     ObjectGarbageCollector( p_playlist, VLC_OBJECT_SOUT,
493                                             i_sout_destroyed_date );
494                 vlc_mutex_lock( &p_playlist->object_lock );
495             }
496         }
497         else if( p_playlist->status.i_status != PLAYLIST_STOPPED )
498         {
499             /* Start another input.
500              * Get the next item to play */
501             p_item = NextItem( p_playlist );
502
503
504             /* We must stop */
505             if( p_item == NULL )
506             {
507                 if( p_autodelete_item )
508                 {
509                     vlc_mutex_unlock( &p_playlist->object_lock );
510                     playlist_Delete( p_playlist,
511                                      p_autodelete_item->input.i_id );
512                     vlc_mutex_lock( &p_playlist->object_lock );
513                     p_autodelete_item = NULL;
514                 }
515                 p_playlist->status.i_status = PLAYLIST_STOPPED;
516                 vlc_mutex_unlock( &p_playlist->object_lock );
517                 continue;
518             }
519
520             PlayItem( p_playlist, p_item );
521
522             if( p_autodelete_item )
523             {
524                 vlc_mutex_unlock( &p_playlist->object_lock );
525                 playlist_Delete( p_playlist, p_autodelete_item->input.i_id );
526                 vlc_mutex_lock( &p_playlist->object_lock );
527                 p_autodelete_item = NULL;
528             }
529         }
530         else if( p_playlist->status.i_status == PLAYLIST_STOPPED )
531         {
532             /* Collect garbage */
533             vlc_mutex_unlock( &p_playlist->object_lock );
534             i_sout_destroyed_date =
535                 ObjectGarbageCollector( p_playlist, VLC_OBJECT_SOUT, mdate() );
536             i_vout_destroyed_date =
537                 ObjectGarbageCollector( p_playlist, VLC_OBJECT_VOUT, mdate() );
538             vlc_mutex_lock( &p_playlist->object_lock );
539         }
540         vlc_mutex_unlock( &p_playlist->object_lock );
541
542         msleep( INTF_IDLE_SLEEP / 2 );
543
544         /* Stop sleeping earlier if we have work */
545         /* TODO : statistics about this */
546         if ( p_playlist->request.b_request &&
547                         p_playlist->status.i_status == PLAYLIST_RUNNING )
548         {
549             continue;
550         }
551
552         msleep( INTF_IDLE_SLEEP / 2 );
553     }
554
555     /* Playlist dying */
556
557     /* If there is an input, kill it */
558     while( 1 )
559     {
560         vlc_mutex_lock( &p_playlist->object_lock );
561
562         if( p_playlist->p_input == NULL )
563         {
564             vlc_mutex_unlock( &p_playlist->object_lock );
565             break;
566         }
567
568         if( p_playlist->p_input->b_dead )
569         {
570             input_thread_t *p_input;
571
572             /* Unlink current input */
573             p_input = p_playlist->p_input;
574             p_playlist->p_input = NULL;
575             vlc_mutex_unlock( &p_playlist->object_lock );
576
577             /* Destroy input */
578             input_DestroyThread( p_input );
579             /* Unlink current input (_after_ input_DestroyThread for vout
580              * garbage collector)*/
581             vlc_object_detach( p_input );
582
583             /* Destroy object */
584             vlc_object_destroy( p_input );
585             continue;
586         }
587         else if( p_playlist->p_input->b_die )
588         {
589             /* This input is dying, leave him alone */
590             ;
591         }
592         else if( p_playlist->p_input->b_error || p_playlist->p_input->b_eof )
593         {
594             input_StopThread( p_playlist->p_input );
595             vlc_mutex_unlock( &p_playlist->object_lock );
596             continue;
597         }
598         else
599         {
600             p_playlist->p_input->b_eof = 1;
601         }
602
603         vlc_mutex_unlock( &p_playlist->object_lock );
604
605         msleep( INTF_IDLE_SLEEP );
606     }
607
608     /* close all remaining sout */
609     while( ( p_obj = vlc_object_find( p_playlist,
610                                       VLC_OBJECT_SOUT, FIND_CHILD ) ) )
611     {
612         vlc_object_release( p_obj );
613         sout_DeleteInstance( (sout_instance_t*)p_obj );
614     }
615
616     /* close all remaining vout */
617     while( ( p_obj = vlc_object_find( p_playlist,
618                                       VLC_OBJECT_VOUT, FIND_CHILD ) ) )
619     {
620         vlc_object_detach( p_obj );
621         vlc_object_release( p_obj );
622         vout_Destroy( (vout_thread_t *)p_obj );
623     }
624 }
625
626 /*****************************************************************************
627  * NextItem
628  *****************************************************************************
629  * This function calculates the next playlist item, depending
630  * on the playlist course mode (forward, backward, random, view,...).
631  *****************************************************************************/
632 static playlist_item_t * NextItem( playlist_t *p_playlist )
633 {
634     playlist_item_t *p_new = NULL;
635     int i_skip,i_goto,i, i_new, i_count ;
636     playlist_view_t *p_view;
637
638     vlc_bool_t b_loop = var_GetBool( p_playlist, "loop");
639     vlc_bool_t b_random = var_GetBool( p_playlist, "random" );
640     vlc_bool_t b_repeat = var_GetBool( p_playlist, "repeat" );
641     vlc_bool_t b_playstop = var_GetBool( p_playlist, "play-and-stop" );
642
643 #ifdef PLAYLIST_PROFILE
644     /* Calculate time needed */
645     int64_t start = mdate();
646 #endif
647
648
649     /* Handle quickly a few special cases */
650
651     /* No items to play */
652     if( p_playlist->i_size == 0 )
653     {
654         msg_Info( p_playlist, "playlist is empty" );
655         return NULL;
656     }
657     /* Nothing requested */
658     if( !p_playlist->request.b_request && p_playlist->status.p_item == NULL )
659     {
660         msg_Warn( p_playlist,"nothing requested" );
661         return NULL;
662     }
663
664     /* Repeat and play/stop */
665     if( !p_playlist->request.b_request && b_repeat == VLC_TRUE )
666     {
667         msg_Dbg( p_playlist,"repeating item" );
668         return p_playlist->status.p_item;
669     }
670
671     if( !p_playlist->request.b_request && b_playstop == VLC_TRUE )
672     {
673         msg_Dbg( p_playlist,"stopping (play and stop)");
674         return NULL;
675     }
676
677     if( !p_playlist->request.b_request &&
678         !(p_playlist->status.p_item->i_flags & PLAYLIST_SKIP_FLAG) )
679     {
680         msg_Dbg( p_playlist, "no-skip mode, stopping") ;
681         return NULL;
682     }
683
684     /* TODO: improve this (only use current node) */
685     /* TODO: use the "shuffled view" internally ? */
686     /* Random case. This is an exception: if request, but request is skip +- 1
687      * we don't go to next item but select a new random one. */
688     if( b_random && (!p_playlist->request.b_request ||
689         p_playlist->request.i_skip == 1 || p_playlist->request.i_skip == -1 ) )
690     {
691         srand( (unsigned int)mdate() );
692         i_new = 0;
693         for( i_count = 0; i_count < p_playlist->i_size - 1 ; i_count ++ )
694         {
695             i_new =
696                 (int)((float)p_playlist->i_size * rand() / (RAND_MAX+1.0));
697             /* Check if the item has not already been played */
698             if( p_playlist->pp_items[i_new]->i_nb_played == 0 )
699                 break;
700         }
701         if( i_count == p_playlist->i_size )
702         {
703             /* The whole playlist has been played: reset the counters */
704             while( i_count > 0 )
705             {
706                 p_playlist->pp_items[--i_count]->i_nb_played = 0;
707             }
708            if( !b_loop )
709             {
710                 return NULL;
711             }
712         }
713         p_playlist->request.i_skip = 0;
714         p_playlist->request.b_request = VLC_FALSE;
715         return p_playlist->pp_items[i_new];
716    }
717
718     /* Start the real work */
719     if( p_playlist->request.b_request )
720     {
721         msg_Dbg( p_playlist,"processing request" );
722         /* We are not playing from a view */
723         if(  p_playlist->request.i_view == -1  )
724         {
725             /* Directly select the item, just like now */
726             i_skip = p_playlist->request.i_skip;
727             i_goto = p_playlist->request.i_goto;
728
729             if( p_playlist->i_index == -1 ) p_playlist->i_index = 0;
730             p_new = p_playlist->pp_items[p_playlist->i_index];
731
732             if( i_goto >= 0  && i_goto < p_playlist->i_size )
733             {
734                 p_playlist->i_index = i_goto;
735                 p_new = p_playlist->pp_items[p_playlist->i_index];
736                 p_playlist->request.i_goto = -1;
737             }
738
739             if( i_skip != 0 )
740             {
741                 if( p_playlist->i_index + i_skip < p_playlist->i_size &&
742                     p_playlist->i_index + i_skip >=  0 )
743                 {
744                     p_playlist->i_index += i_skip;
745                     p_new = p_playlist->pp_items[p_playlist->i_index];
746                 }
747             }
748         }
749         else
750         {
751             p_new = p_playlist->request.p_item;
752             i_skip = p_playlist->request.i_skip;
753
754             /* If we are asked for a node, take its first item */
755             if( p_playlist->request.p_item == NULL && i_skip == 0 )
756             {
757                 i_skip++;
758             }
759
760             p_view = playlist_ViewFind( p_playlist,p_playlist->request.i_view );
761             p_playlist->status.p_node = p_playlist->request.p_node;
762             p_playlist->status.i_view = p_playlist->request.i_view;
763             if( i_skip > 0 )
764             {
765                 for( i = i_skip; i > 0 ; i-- )
766                 {
767                     p_new = playlist_FindNextFromParent( p_playlist,
768                                     p_playlist->request.i_view,
769                                     p_view->p_root,
770                                     p_playlist->request.p_node,
771                                     p_new );
772                     if( p_new == NULL ) break;
773                 }
774             }
775             else if( i_skip < 0 )
776             {
777                 for( i = i_skip; i < 0 ; i++ )
778                 {
779                     p_new = playlist_FindPrevFromParent( p_playlist,
780                                     p_playlist->request.i_view,
781                                     p_view->p_root,
782                                     p_playlist->request.p_node,
783                                     p_new );
784                     if( p_new == NULL ) break;
785                 }
786
787             }
788         }
789         /* Clear the request */
790         p_playlist->request.b_request = VLC_FALSE;
791     }
792     /* "Automatic" item change ( next ) */
793     else
794     {
795         p_playlist->request_date = 0;
796
797
798         if( p_playlist->status.i_view == -1 )
799         {
800             if( p_playlist->i_index + 1 < p_playlist->i_size )
801             {
802                 p_playlist->i_index++;
803                 p_new = p_playlist->pp_items[p_playlist->i_index];
804             }
805             else
806             {
807                 msg_Dbg( p_playlist,"finished" );
808                 p_new = NULL;
809             }
810         }
811         /* We are playing with a view */
812         else
813         {
814             playlist_view_t *p_view =
815                     playlist_ViewFind( p_playlist,
816                                    p_playlist->status.i_view );
817             p_new = playlist_FindNextFromParent( p_playlist,
818                             p_playlist->status.i_view,
819                             p_view->p_root,
820                             p_playlist->status.p_node,
821                             p_playlist->status.p_item );
822         }
823     }
824
825     /* Reset index */
826     if( p_playlist->i_index >= 0 && p_new != NULL &&
827             p_playlist->pp_items[p_playlist->i_index] != p_new )
828     {
829         p_playlist->i_index = playlist_GetPositionById( p_playlist,
830                                                         p_new->input.i_id );
831     }
832
833 #ifdef PLAYLIST_PROFILE
834     msg_Dbg(p_playlist,"next item found in "I64Fi " us\n",mdate()-start );
835 #endif
836
837     if( p_new == NULL ) { msg_Info( p_playlist, "Nothing to play" ); }
838
839     return p_new;
840 }
841
842
843 #if 0
844
845
846 static void SkipItem( playlist_t *p_playlist, int i_arg )
847 {
848     int i_oldindex = p_playlist->i_index;
849     vlc_bool_t b_random, b_repeat, b_loop;
850     vlc_value_t val;
851     int i_count;
852
853     /* Increment */
854     else if( !b_repeat )
855     {
856         p_playlist->i_index += i_arg;
857     }
858
859     /* Boundary check */
860     if( p_playlist->i_index >= p_playlist->i_size )
861     {
862         if( p_playlist->i_status == PLAYLIST_STOPPED || b_random || b_loop )
863         {
864             p_playlist->i_index -= p_playlist->i_size
865                          * ( p_playlist->i_index / p_playlist->i_size );
866         }
867         else
868         {
869             /* Don't loop by default: stop at playlist end */
870             p_playlist->i_index = i_oldindex;
871             p_playlist->i_status = PLAYLIST_STOPPED;
872         }
873     }
874     else if( p_playlist->i_index < 0 )
875     {
876         p_playlist->i_index = p_playlist->i_size - 1;
877     }
878
879     /* Check that the item is enabled */
880     if( p_playlist->pp_items[p_playlist->i_index]->b_enabled == VLC_FALSE &&
881         p_playlist->i_enabled != 0)
882     {
883         SkipItem( p_playlist , 1 );
884     }
885 }
886
887 #endif
888
889 /*****************************************************************************
890  * PlayItem: start the input thread for an item
891  ****************************************************************************/
892 static void PlayItem( playlist_t *p_playlist, playlist_item_t *p_item )
893 {
894     vlc_value_t val;
895     int i;
896
897     msg_Dbg( p_playlist, "creating new input thread" );
898
899     p_item->i_nb_played++;
900     p_playlist->status.p_item = p_item;
901
902 #ifdef PLAYLIST_PROFILE
903     if( p_playlist->request_date != 0 )
904     {
905         msg_Dbg( p_playlist, "request processed after "I64Fi " us",
906                   mdate() - p_playlist->request_date );
907     }
908 #endif
909
910     p_playlist->p_input = input_CreateThread( p_playlist, &p_item->input );
911
912     var_AddCallback( p_playlist->p_input, "item-change",
913                          ItemChange, p_playlist );
914
915     val.i_int = p_item->input.i_id;
916     /* unlock the playlist to set the var...mmm */
917     vlc_mutex_unlock( &p_playlist->object_lock);
918     var_Set( p_playlist, "playlist-current", val);
919     vlc_mutex_lock( &p_playlist->object_lock);
920
921 }
922
923 /* Forward item change from input */
924 static int ItemChange( vlc_object_t *p_obj, const char *psz_var,
925                        vlc_value_t oldval, vlc_value_t newval, void *param )
926 {
927     playlist_t *p_playlist = (playlist_t *)param;
928     int i_index;
929
930     //p_playlist->b_need_update = VLC_TRUE;
931     var_SetInteger( p_playlist, "item-change", newval.i_int );
932
933     /* Update view */
934     /* FIXME: Make that automatic */
935     playlist_ViewUpdate( p_playlist, VIEW_S_AUTHOR );
936
937     return VLC_SUCCESS;
938 }