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