]> git.sesse.net Git - vlc/blob - src/playlist/playlist.c
Fix autoplayback with skins
[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_Dbg( p_playlist,"nothing requested, starting" );
679     }
680
681     /* Repeat and play/stop */
682     if( !p_playlist->request.b_request && b_repeat == VLC_TRUE )
683     {
684         msg_Dbg( p_playlist,"repeating item" );
685         return p_playlist->status.p_item;
686     }
687
688     if( !p_playlist->request.b_request && b_playstop == VLC_TRUE )
689     {
690         msg_Dbg( p_playlist,"stopping (play and stop)");
691         return NULL;
692     }
693
694     if( !p_playlist->request.b_request && p_playlist->status.p_item && 
695         !(p_playlist->status.p_item->i_flags & PLAYLIST_SKIP_FLAG) )
696     {
697         msg_Dbg( p_playlist, "no-skip mode, stopping") ;
698         return NULL;
699     }
700
701     /* TODO: improve this (only use current node) */
702     /* TODO: use the "shuffled view" internally ? */
703     /* Random case. This is an exception: if request, but request is skip +- 1
704      * we don't go to next item but select a new random one. */
705     if( b_random && (!p_playlist->request.b_request ||
706         p_playlist->request.i_skip == 1 || p_playlist->request.i_skip == -1 ) )
707     {
708         srand( (unsigned int)mdate() );
709         i_new = 0;
710         for( i_count = 0; i_count < p_playlist->i_size - 1 ; i_count ++ )
711         {
712             i_new =
713                 (int)((float)p_playlist->i_size * rand() / (RAND_MAX+1.0));
714             /* Check if the item has not already been played */
715             if( p_playlist->pp_items[i_new]->i_nb_played == 0 )
716                 break;
717         }
718         if( i_count == p_playlist->i_size )
719         {
720             /* The whole playlist has been played: reset the counters */
721             while( i_count > 0 )
722             {
723                 p_playlist->pp_items[--i_count]->i_nb_played = 0;
724             }
725            if( !b_loop )
726             {
727                 return NULL;
728             }
729         }
730         p_playlist->request.i_skip = 0;
731         p_playlist->request.b_request = VLC_FALSE;
732         return p_playlist->pp_items[i_new];
733    }
734
735     /* Start the real work */
736     if( p_playlist->request.b_request )
737     {
738         msg_Dbg( p_playlist,"processing request" );
739         /* We are not playing from a view */
740         if(  p_playlist->request.i_view == -1  )
741         {
742 #ifdef PLAYLIST_DEBUG
743             msg_Dbg( p_playlist, "non-view mode request");
744 #endif
745             /* Directly select the item, just like now */
746             i_skip = p_playlist->request.i_skip;
747             i_goto = p_playlist->request.i_goto;
748
749             if( p_playlist->i_index == -1 ) p_playlist->i_index = 0;
750             p_new = p_playlist->pp_items[p_playlist->i_index];
751
752             if( i_goto >= 0  && i_goto < p_playlist->i_size )
753             {
754                 p_playlist->i_index = i_goto;
755                 p_new = p_playlist->pp_items[p_playlist->i_index];
756                 p_playlist->request.i_goto = -1;
757             }
758
759             if( i_skip != 0 )
760             {
761                 if( p_playlist->i_index + i_skip < p_playlist->i_size &&
762                     p_playlist->i_index + i_skip >=  0 )
763                 {
764                     p_playlist->i_index += i_skip;
765                     p_new = p_playlist->pp_items[p_playlist->i_index];
766                 }
767             }
768         }
769         else
770         {
771 #ifdef PLAYLIST_DEBUG
772             msg_Dbg( p_playlist, "view mode request" );
773 #endif
774             p_new = p_playlist->request.p_item;
775             i_skip = p_playlist->request.i_skip;
776
777             /* If we are asked for a node, take its first item */
778             if( p_playlist->request.p_item == NULL && i_skip == 0 )
779             {
780                 i_skip++;
781             }
782
783             p_view = playlist_ViewFind( p_playlist,p_playlist->request.i_view );
784             p_playlist->status.p_node = p_playlist->request.p_node;
785             p_playlist->status.i_view = p_playlist->request.i_view;
786             if( i_skip > 0 )
787             {
788                 for( i = i_skip; i > 0 ; i-- )
789                 {
790                     p_new = playlist_FindNextFromParent( p_playlist,
791                                     p_playlist->request.i_view,
792                                     p_view->p_root,
793                                     p_playlist->request.p_node,
794                                     p_new );
795                     if( p_new == NULL ) break;
796                 }
797             }
798             else if( i_skip < 0 )
799             {
800                 for( i = i_skip; i < 0 ; i++ )
801                 {
802                     p_new = playlist_FindPrevFromParent( p_playlist,
803                                     p_playlist->request.i_view,
804                                     p_view->p_root,
805                                     p_playlist->request.p_node,
806                                     p_new );
807                     if( p_new == NULL ) break;
808                 }
809
810             }
811         }
812         /* Clear the request */
813         p_playlist->request.b_request = VLC_FALSE;
814     }
815     /* "Automatic" item change ( next ) */
816     else
817     {
818         p_playlist->request_date = 0;
819
820         if( p_playlist->status.i_view == -1 )
821         {
822             if( p_playlist->i_index + 1 < p_playlist->i_size )
823             {
824                 p_playlist->i_index++;
825                 p_new = p_playlist->pp_items[p_playlist->i_index];
826                 if( !(p_new->i_flags & PLAYLIST_SKIP_FLAG) )
827                 {
828                     return NULL;
829                 }
830             }
831             else
832             {
833                 msg_Dbg( p_playlist,"finished" );
834                 p_new = NULL;
835             }
836         }
837         /* We are playing with a view */
838         else
839         {
840             playlist_view_t *p_view =
841                     playlist_ViewFind( p_playlist,
842                                    p_playlist->status.i_view );
843             p_new = playlist_FindNextFromParent( p_playlist,
844                             p_playlist->status.i_view,
845                             p_view->p_root,
846                             p_playlist->status.p_node,
847                             p_playlist->status.p_item );
848         }
849     }
850
851     /* Reset index */
852     if( p_playlist->i_index >= 0 && p_new != NULL &&
853             p_playlist->pp_items[p_playlist->i_index] != p_new )
854     {
855         p_playlist->i_index = playlist_GetPositionById( p_playlist,
856                                                         p_new->input.i_id );
857     }
858
859 #ifdef PLAYLIST_PROFILE
860     msg_Dbg(p_playlist,"next item found in "I64Fi " us", mdate()-start );
861 #endif
862
863     if( p_new == NULL ) { msg_Info( p_playlist, "Nothing to play" ); }
864
865     return p_new;
866 }
867
868
869 #if 0
870
871
872 static void SkipItem( playlist_t *p_playlist, int i_arg )
873 {
874     int i_oldindex = p_playlist->i_index;
875     vlc_bool_t b_random, b_repeat, b_loop;
876     vlc_value_t val;
877     int i_count;
878
879     /* Increment */
880     else if( !b_repeat )
881     {
882         p_playlist->i_index += i_arg;
883     }
884
885     /* Boundary check */
886     if( p_playlist->i_index >= p_playlist->i_size )
887     {
888         if( p_playlist->i_status == PLAYLIST_STOPPED || b_random || b_loop )
889         {
890             p_playlist->i_index -= p_playlist->i_size
891                          * ( p_playlist->i_index / p_playlist->i_size );
892         }
893         else
894         {
895             /* Don't loop by default: stop at playlist end */
896             p_playlist->i_index = i_oldindex;
897             p_playlist->i_status = PLAYLIST_STOPPED;
898         }
899     }
900     else if( p_playlist->i_index < 0 )
901     {
902         p_playlist->i_index = p_playlist->i_size - 1;
903     }
904
905     /* Check that the item is enabled */
906     if( p_playlist->pp_items[p_playlist->i_index]->b_enabled == VLC_FALSE &&
907         p_playlist->i_enabled != 0)
908     {
909         SkipItem( p_playlist , 1 );
910     }
911 }
912
913 #endif
914
915 /*****************************************************************************
916  * PlayItem: start the input thread for an item
917  ****************************************************************************/
918 static int PlayItem( playlist_t *p_playlist, playlist_item_t *p_item )
919 {
920     vlc_value_t val;
921
922     msg_Dbg( p_playlist, "creating new input thread" );
923
924     p_item->i_nb_played++;
925     p_playlist->status.p_item = p_item;
926
927     p_playlist->i_index = playlist_GetPositionById( p_playlist,
928                                                     p_item->input.i_id );
929
930 #ifdef PLAYLIST_PROFILE
931     if( p_playlist->request_date != 0 )
932     {
933         msg_Dbg( p_playlist, "request processed after "I64Fi " us",
934                   mdate() - p_playlist->request_date );
935     }
936 #endif
937
938     p_playlist->p_input = input_CreateThread( p_playlist, &p_item->input );
939
940     var_AddCallback( p_playlist->p_input, "item-change",
941                          ItemChange, p_playlist );
942
943     val.i_int = p_item->input.i_id;
944     /* unlock the playlist to set the var...mmm */
945     vlc_mutex_unlock( &p_playlist->object_lock);
946     var_Set( p_playlist, "playlist-current", val);
947     vlc_mutex_lock( &p_playlist->object_lock);
948
949     return VLC_SUCCESS;
950
951 }
952
953 /* Forward item change from input */
954 static int ItemChange( vlc_object_t *p_obj, const char *psz_var,
955                        vlc_value_t oldval, vlc_value_t newval, void *param )
956 {
957     playlist_t *p_playlist = (playlist_t *)param;
958     int i_index;
959
960     //p_playlist->b_need_update = VLC_TRUE;
961     var_SetInteger( p_playlist, "item-change", newval.i_int );
962
963     /* Update view */
964     /* FIXME: Make that automatic */
965     playlist_ViewUpdate( p_playlist, VIEW_S_AUTHOR );
966
967     return VLC_SUCCESS;
968 }