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