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