]> git.sesse.net Git - vlc/blob - src/playlist/playlist.c
* Prevent the autoplay function from playing the Shoutcast url for the xmllist. The...
[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         p_playlist->request.p_item = NULL;
341         break;
342
343     case PLAYLIST_ITEMPLAY:
344         p_item = (playlist_item_t *)va_arg( args, playlist_item_t * );
345         if ( p_item == NULL || p_item->input.psz_uri == NULL )
346             return VLC_EGENERIC;
347         p_playlist->status.i_status = PLAYLIST_RUNNING;
348         p_playlist->request.i_skip = 0;
349         p_playlist->request.b_request = VLC_TRUE;
350         p_playlist->request.p_item = p_item;
351         p_playlist->request.i_view = p_playlist->status.i_view;
352         p_view = playlist_ViewFind( p_playlist, p_playlist->status.i_view );
353         if( p_view )
354         {
355             p_playlist->request.p_node = p_view->p_root;
356         }
357         else
358         {
359             p_playlist->request.p_node = NULL;
360         }
361         break;
362
363     case PLAYLIST_VIEWPLAY:
364         i_view = (int)va_arg( args,int );
365         p_node = (playlist_item_t *)va_arg( args, playlist_item_t * );
366         p_item = (playlist_item_t *)va_arg( args, playlist_item_t * );
367         if ( p_node == NULL || (p_item != NULL && p_item->input.psz_uri
368                                                          == NULL ))
369         {
370             p_playlist->status.i_status = PLAYLIST_STOPPED;
371             p_playlist->request.b_request = VLC_TRUE;
372             return VLC_SUCCESS;
373         }
374         p_playlist->status.i_status = PLAYLIST_RUNNING;
375         p_playlist->request.i_skip = 0;
376         p_playlist->request.b_request = VLC_TRUE;
377         p_playlist->request.i_view = i_view;
378         p_playlist->request.p_node = p_node;
379         p_playlist->request.p_item = p_item;
380
381         /* If we select a node, play only it.
382          * If we select an item, continue */
383         if( p_playlist->request.p_item == NULL ||
384             ! p_playlist->request.p_node->i_flags & PLAYLIST_SKIP_FLAG )
385         {
386             p_playlist->b_go_next = VLC_FALSE;
387         }
388         else
389         {
390             p_playlist->b_go_next = VLC_TRUE;
391         }
392         break;
393
394     case PLAYLIST_PLAY:
395         p_playlist->status.i_status = PLAYLIST_RUNNING;
396
397         if( p_playlist->p_input )
398         {
399             val.i_int = PLAYING_S;
400             var_Set( p_playlist->p_input, "state", val );
401             break;
402         }
403
404         /* FIXME : needed ? */
405         p_playlist->request.b_request = VLC_TRUE;
406         p_playlist->request.i_view = p_playlist->status.i_view;
407         p_playlist->request.p_node = p_playlist->status.p_node;
408         p_playlist->request.p_item = p_playlist->status.p_item = NULL;
409         p_playlist->request.i_skip = 0;
410         p_playlist->request.i_goto = -1;
411         break;
412
413     case PLAYLIST_AUTOPLAY:
414         p_playlist->status.i_status = PLAYLIST_RUNNING;
415         p_playlist->status.p_node = p_playlist->p_general;
416
417         p_playlist->request.b_request = VLC_FALSE;
418         break;
419
420     case PLAYLIST_PAUSE:
421         val.i_int = 0;
422         if( p_playlist->p_input )
423             var_Get( p_playlist->p_input, "state", &val );
424
425         if( val.i_int == PAUSE_S )
426         {
427             p_playlist->status.i_status = PLAYLIST_RUNNING;
428             if( p_playlist->p_input )
429             {
430                 val.i_int = PLAYING_S;
431                 var_Set( p_playlist->p_input, "state", val );
432             }
433         }
434         else
435         {
436             p_playlist->status.i_status = PLAYLIST_PAUSED;
437             if( p_playlist->p_input )
438             {
439                 val.i_int = PAUSE_S;
440                 var_Set( p_playlist->p_input, "state", val );
441             }
442         }
443         break;
444
445     case PLAYLIST_SKIP:
446         if( p_playlist->status.i_view > -1 )
447         {
448             p_playlist->request.i_view = p_playlist->status.i_view;
449             p_playlist->request.p_node = p_playlist->status.p_node;
450             p_playlist->request.p_item = p_playlist->status.p_item;
451         }
452         p_playlist->request.i_skip = (int) va_arg( args, int );
453         p_playlist->request.b_request = VLC_TRUE;
454         break;
455
456     case PLAYLIST_GOTO:
457         p_playlist->status.i_status = PLAYLIST_RUNNING;
458         p_playlist->request.p_node = NULL;
459         p_playlist->request.p_item = NULL;
460         p_playlist->request.i_view = -1;
461         p_playlist->request.i_goto = (int) va_arg( args, int );
462         p_playlist->request.b_request = VLC_TRUE;
463         break;
464
465     default:
466         msg_Err( p_playlist, "unimplemented playlist query" );
467         return VLC_EBADVAR;
468         break;
469     }
470
471     return VLC_SUCCESS;
472 }
473
474 int playlist_PreparseEnqueue( playlist_t *p_playlist,
475                               input_item_t *p_item )
476 {
477     vlc_mutex_lock( &p_playlist->p_preparse->object_lock );
478     INSERT_ELEM( p_playlist->p_preparse->pp_waiting,
479                  p_playlist->p_preparse->i_waiting,
480                  p_playlist->p_preparse->i_waiting,
481                  p_item );
482     vlc_mutex_unlock( &p_playlist->p_preparse->object_lock );
483     return VLC_SUCCESS;
484 }
485
486 /* Should only be called if playlist and preparser are locked */
487 void playlist_PreparseEnqueueItemSub( playlist_t *p_playlist,
488                                      playlist_item_t *p_item )
489 {
490     int i;
491     if( p_item->i_children == -1 )
492     {
493         INSERT_ELEM( p_playlist->p_preparse->pp_waiting,
494                      p_playlist->p_preparse->i_waiting,
495                      p_playlist->p_preparse->i_waiting,
496                      &(p_item->input) );
497     }
498     else
499     {
500         for( i = 0; i < p_item->i_children; i++)
501         {
502             playlist_PreparseEnqueueItemSub( p_playlist,
503                                              p_item->pp_children[i] );
504         }
505     }
506 }
507
508 int playlist_PreparseEnqueueItem( playlist_t *p_playlist,
509                                   playlist_item_t *p_item )
510 {
511     vlc_mutex_lock( &p_playlist->object_lock );
512     vlc_mutex_lock( &p_playlist->p_preparse->object_lock );
513     playlist_PreparseEnqueueItemSub( p_playlist, p_item );
514     vlc_mutex_unlock( &p_playlist->p_preparse->object_lock );
515     vlc_mutex_unlock( &p_playlist->object_lock );
516     return VLC_SUCCESS;
517 }
518
519
520 /* Destroy remaining objects */
521 static mtime_t ObjectGarbageCollector( playlist_t *p_playlist, int i_type,
522                                        mtime_t destroy_date )
523 {
524     vlc_object_t *p_obj;
525
526     if( destroy_date > mdate() ) return destroy_date;
527
528     if( destroy_date == 0 )
529     {
530         /* give a little time */
531         return mdate() + I64C(1000000);
532     }
533     else
534     {
535         vlc_mutex_lock( &p_playlist->gc_lock );
536         while( ( p_obj = vlc_object_find( p_playlist, i_type, FIND_CHILD ) ) )
537         {
538             if( p_obj->p_parent != (vlc_object_t*)p_playlist )
539             {
540                 /* only first child (ie unused) */
541                 vlc_object_release( p_obj );
542                 break;
543             }
544             if( i_type == VLC_OBJECT_VOUT )
545             {
546                 msg_Dbg( p_playlist, "garbage collector destroying 1 vout" );
547                 vlc_object_detach( p_obj );
548                 vlc_object_release( p_obj );
549                 vout_Destroy( (vout_thread_t *)p_obj );
550             }
551             else if( i_type == VLC_OBJECT_SOUT )
552             {
553                 vlc_object_release( p_obj );
554                 sout_DeleteInstance( (sout_instance_t*)p_obj );
555             }
556         }
557         vlc_mutex_unlock( &p_playlist->gc_lock );
558         return 0;
559     }
560 }
561
562 /*****************************************************************************
563  * RunThread: main playlist thread
564  *****************************************************************************/
565 static void RunThread ( playlist_t *p_playlist )
566 {
567     vlc_object_t *p_obj;
568     playlist_item_t *p_item = NULL;
569
570     mtime_t    i_vout_destroyed_date = 0;
571     mtime_t    i_sout_destroyed_date = 0;
572
573     playlist_item_t *p_autodelete_item = NULL;
574
575     /* Tell above that we're ready */
576     vlc_thread_ready( p_playlist );
577
578     while( !p_playlist->b_die )
579     {
580         vlc_mutex_lock( &p_playlist->object_lock );
581
582         /* First, check if we have something to do */
583         /* FIXME : this can be called several times */
584         if( p_playlist->request.b_request )
585         {
586 #ifdef PLAYLIST_PROFILE
587             msg_Dbg(p_playlist, "beginning processing of request, "
588                          I64Fi" us ", mdate() - p_playlist->request_date );
589 #endif
590             /* Stop the existing input */
591             if( p_playlist->p_input )
592             {
593                 input_StopThread( p_playlist->p_input );
594             }
595             /* The code below will start the next input for us */
596             if( p_playlist->status.i_status == PLAYLIST_STOPPED )
597             {
598                 p_playlist->request.b_request = VLC_FALSE;
599             }
600         }
601
602         /* If there is an input, check that it doesn't need to die. */
603         if( p_playlist->p_input )
604         {
605             /* This input is dead. Remove it ! */
606             if( p_playlist->p_input->b_dead )
607             {
608                 input_thread_t *p_input;
609
610                 p_input = p_playlist->p_input;
611                 p_playlist->p_input = NULL;
612
613                 /* Release the playlist lock, because we may get stuck
614                  * in input_DestroyThread() for some time. */
615                 vlc_mutex_unlock( &p_playlist->object_lock );
616
617                 /* Destroy input */
618                 input_DestroyThread( p_input );
619
620                 /* Unlink current input
621                  * (_after_ input_DestroyThread for vout garbage collector) */
622                 vlc_object_detach( p_input );
623
624                 /* Destroy object */
625                 vlc_object_destroy( p_input );
626
627                 i_vout_destroyed_date = 0;
628                 i_sout_destroyed_date = 0;
629
630                 if( p_playlist->status.p_item->i_flags
631                     & PLAYLIST_REMOVE_FLAG )
632                 {
633                      playlist_ItemDelete( p_item );
634                      p_playlist->status.p_item = NULL;
635                 }
636
637                 continue;
638             }
639             /* This input is dying, let it do */
640             else if( p_playlist->p_input->b_die )
641             {
642                 ;
643             }
644             /* This input has finished, ask it to die ! */
645             else if( p_playlist->p_input->b_error
646                       || p_playlist->p_input->b_eof )
647             {
648                 /* TODO FIXME XXX TODO FIXME XXX */
649                 /* Check for autodeletion */
650
651                 if( p_playlist->status.p_item->i_flags & PLAYLIST_DEL_FLAG )
652                 {
653                     p_autodelete_item = p_playlist->status.p_item;
654                 }
655                 input_StopThread( p_playlist->p_input );
656                 /* Select the next playlist item */
657                 vlc_mutex_unlock( &p_playlist->object_lock );
658                 continue;
659             }
660             else if( p_playlist->p_input->i_state != INIT_S )
661             {
662                 vlc_mutex_unlock( &p_playlist->object_lock );
663                 i_vout_destroyed_date =
664                     ObjectGarbageCollector( p_playlist, VLC_OBJECT_VOUT,
665                                             i_vout_destroyed_date );
666                 i_sout_destroyed_date =
667                     ObjectGarbageCollector( p_playlist, VLC_OBJECT_SOUT,
668                                             i_sout_destroyed_date );
669                 vlc_mutex_lock( &p_playlist->object_lock );
670             }
671         }
672         else if( p_playlist->status.i_status != PLAYLIST_STOPPED )
673         {
674             /* Start another input.
675              * Get the next item to play */
676             p_item = NextItem( p_playlist );
677
678
679             /* We must stop */
680             if( p_item == NULL )
681             {
682                 if( p_autodelete_item )
683                 {
684                     playlist_Delete( p_playlist,
685                                      p_autodelete_item->input.i_id );
686                     p_autodelete_item = NULL;
687                 }
688                 p_playlist->status.i_status = PLAYLIST_STOPPED;
689                 vlc_mutex_unlock( &p_playlist->object_lock );
690                 continue;
691             }
692
693             PlayItem( p_playlist, p_item );
694
695             if( p_autodelete_item )
696             {
697                 playlist_Delete( p_playlist, p_autodelete_item->input.i_id );
698                 p_autodelete_item = NULL;
699             }
700         }
701         else if( p_playlist->status.i_status == PLAYLIST_STOPPED )
702         {
703             /* Collect garbage */
704             vlc_mutex_unlock( &p_playlist->object_lock );
705             i_sout_destroyed_date =
706                 ObjectGarbageCollector( p_playlist, VLC_OBJECT_SOUT, mdate() );
707             i_vout_destroyed_date =
708                 ObjectGarbageCollector( p_playlist, VLC_OBJECT_VOUT, mdate() );
709             vlc_mutex_lock( &p_playlist->object_lock );
710         }
711         vlc_mutex_unlock( &p_playlist->object_lock );
712
713         msleep( INTF_IDLE_SLEEP / 2 );
714
715         /* Stop sleeping earlier if we have work */
716         /* TODO : statistics about this */
717         if ( p_playlist->request.b_request &&
718                         p_playlist->status.i_status == PLAYLIST_RUNNING )
719         {
720             continue;
721         }
722
723         msleep( INTF_IDLE_SLEEP / 2 );
724     }
725
726     /* Playlist dying */
727
728     /* If there is an input, kill it */
729     while( 1 )
730     {
731         vlc_mutex_lock( &p_playlist->object_lock );
732
733         if( p_playlist->p_input == NULL )
734         {
735             vlc_mutex_unlock( &p_playlist->object_lock );
736             break;
737         }
738
739         if( p_playlist->p_input->b_dead )
740         {
741             input_thread_t *p_input;
742
743             /* Unlink current input */
744             p_input = p_playlist->p_input;
745             p_playlist->p_input = NULL;
746             vlc_mutex_unlock( &p_playlist->object_lock );
747
748             /* Destroy input */
749             input_DestroyThread( p_input );
750             /* Unlink current input (_after_ input_DestroyThread for vout
751              * garbage collector)*/
752             vlc_object_detach( p_input );
753
754             /* Destroy object */
755             vlc_object_destroy( p_input );
756             continue;
757         }
758         else if( p_playlist->p_input->b_die )
759         {
760             /* This input is dying, leave it alone */
761             ;
762         }
763         else if( p_playlist->p_input->b_error || p_playlist->p_input->b_eof )
764         {
765             input_StopThread( p_playlist->p_input );
766             vlc_mutex_unlock( &p_playlist->object_lock );
767             continue;
768         }
769         else
770         {
771             p_playlist->p_input->b_eof = 1;
772         }
773
774         vlc_mutex_unlock( &p_playlist->object_lock );
775
776         msleep( INTF_IDLE_SLEEP );
777     }
778
779     /* close all remaining sout */
780     while( ( p_obj = vlc_object_find( p_playlist,
781                                       VLC_OBJECT_SOUT, FIND_CHILD ) ) )
782     {
783         vlc_object_release( p_obj );
784         sout_DeleteInstance( (sout_instance_t*)p_obj );
785     }
786
787     /* close all remaining vout */
788     while( ( p_obj = vlc_object_find( p_playlist,
789                                       VLC_OBJECT_VOUT, FIND_CHILD ) ) )
790     {
791         vlc_object_detach( p_obj );
792         vlc_object_release( p_obj );
793         vout_Destroy( (vout_thread_t *)p_obj );
794     }
795 }
796
797 /* Queue for items to preparse */
798 static void RunPreparse ( playlist_preparse_t *p_obj )
799 {
800     playlist_t *p_playlist = (playlist_t *)p_obj->p_parent;
801     vlc_bool_t b_sleep;
802
803     /* Tell above that we're ready */
804     vlc_thread_ready( p_obj );
805
806     while( !p_playlist->b_die )
807     {
808         vlc_mutex_lock( &p_obj->object_lock );
809
810         if( p_obj->i_waiting > 0 )
811         {
812             input_item_t *p_current = p_obj->pp_waiting[0];
813             REMOVE_ELEM( p_obj->pp_waiting, p_obj->i_waiting, 0 );
814             vlc_mutex_unlock( &p_obj->object_lock );
815             input_Preparse( p_playlist, p_current );
816             var_SetInteger( p_playlist, "item-change", p_current->i_id );
817             vlc_mutex_lock( &p_obj->object_lock );
818         }
819         b_sleep = ( p_obj->i_waiting == 0 );
820
821         vlc_mutex_unlock( &p_obj->object_lock );
822
823         if( p_obj->i_waiting == 0 )
824         {
825             msleep( INTF_IDLE_SLEEP );
826         }
827     }
828 }
829
830 /*****************************************************************************
831  * NextItem
832  *****************************************************************************
833  * This function calculates the next playlist item, depending
834  * on the playlist course mode (forward, backward, random, view,...).
835  *****************************************************************************/
836 static playlist_item_t * NextItem( playlist_t *p_playlist )
837 {
838     playlist_item_t *p_new = NULL;
839     int i_skip,i_goto,i, i_new, i_count ;
840     playlist_view_t *p_view;
841
842     vlc_bool_t b_loop = var_GetBool( p_playlist, "loop" );
843     vlc_bool_t b_random = var_GetBool( p_playlist, "random" );
844     vlc_bool_t b_repeat = var_GetBool( p_playlist, "repeat" );
845     vlc_bool_t b_playstop = var_GetBool( p_playlist, "play-and-stop" );
846
847 #ifdef PLAYLIST_PROFILE
848     /* Calculate time needed */
849     int64_t start = mdate();
850 #endif
851
852     /* Handle quickly a few special cases */
853
854     /* No items to play */
855     if( p_playlist->i_size == 0 )
856     {
857         msg_Info( p_playlist, "playlist is empty" );
858         return NULL;
859     }
860     /* Nothing requested */
861     if( !p_playlist->request.b_request && p_playlist->status.p_item == NULL )
862     {
863         msg_Dbg( p_playlist,"nothing requested, starting" );
864     }
865
866     /* Repeat and play/stop */
867     if( !p_playlist->request.b_request && b_repeat == VLC_TRUE )
868     {
869         msg_Dbg( p_playlist,"repeating item" );
870         return p_playlist->status.p_item;
871     }
872
873     if( !p_playlist->request.b_request && b_playstop == VLC_TRUE )
874     {
875         msg_Dbg( p_playlist,"stopping (play and stop)");
876         return NULL;
877     }
878
879     if( !p_playlist->request.b_request && p_playlist->status.p_item &&
880         !( p_playlist->status.p_item->i_flags & PLAYLIST_SKIP_FLAG ) )
881     {
882         msg_Dbg( p_playlist, "no-skip mode, stopping") ;
883         return NULL;
884     }
885
886     /* TODO: improve this (only use current node) */
887     /* TODO: use the "shuffled view" internally ? */
888     /* Random case. This is an exception: if request, but request is skip +- 1
889      * we don't go to next item but select a new random one. */
890     if( b_random && 
891         ( !p_playlist->request.b_request ||
892         ( p_playlist->request.b_request && ( p_playlist->request.p_item == NULL ||
893           p_playlist->request.i_skip == 1 || p_playlist->request.i_skip == -1 ) ) ) )
894     {
895         srand( (unsigned int)mdate() );
896         i_new = 0;
897         for( i_count = 0; i_count < p_playlist->i_size - 1 ; i_count ++ )
898         {
899             i_new =
900                 (int)((float)p_playlist->i_size * rand() / (RAND_MAX+1.0));
901             /* Check if the item has not already been played */
902             if( p_playlist->pp_items[i_new]->i_nb_played == 0 )
903                 break;
904         }
905         if( i_count == p_playlist->i_size )
906         {
907             /* The whole playlist has been played: reset the counters */
908             while( i_count > 0 )
909             {
910                 p_playlist->pp_items[--i_count]->i_nb_played = 0;
911             }
912             if( !b_loop )
913             {
914                 return NULL;
915             }
916         }
917         p_playlist->request.i_skip = 0;
918         p_playlist->request.b_request = VLC_FALSE;
919         return p_playlist->pp_items[i_new];
920     }
921
922     /* Start the real work */
923     if( p_playlist->request.b_request )
924     {
925 #ifdef PLAYLIST_DEBUG
926         msg_Dbg( p_playlist,"processing request" );
927 #endif
928         /* We are not playing from a view */
929         if( p_playlist->request.i_view == -1  )
930         {
931 #ifdef PLAYLIST_DEBUG
932             msg_Dbg( p_playlist, "non-view mode request");
933 #endif
934             /* Directly select the item, just like now */
935             p_new = p_playlist->request.p_item;
936             i_skip = p_playlist->request.i_skip;
937             i_goto = p_playlist->request.i_goto;
938
939             if( p_playlist->i_index < 0 ) p_playlist->i_index = 0;
940             if ( p_new == NULL )
941                 p_new = p_playlist->pp_items[p_playlist->i_index];
942
943             if( i_goto >= 0  && i_goto < p_playlist->i_size )
944             {
945                 p_playlist->i_index = i_goto;
946                 p_new = p_playlist->pp_items[p_playlist->i_index];
947                 p_playlist->request.i_goto = -1;
948             }
949
950             if( i_skip != 0 )
951             {
952                 if( p_playlist->i_index + i_skip < p_playlist->i_size &&
953                     p_playlist->i_index + i_skip >=  0 )
954                 {
955                     p_playlist->i_index += i_skip;
956                     p_new = p_playlist->pp_items[p_playlist->i_index];
957                 }
958                 p_playlist->request.i_skip = 0;
959             }
960         }
961         else
962         {
963 #ifdef PLAYLIST_DEBUG
964             msg_Dbg( p_playlist, "view mode request" );
965 #endif
966             p_new = p_playlist->request.p_item;
967             i_skip = p_playlist->request.i_skip;
968
969             /* If we are asked for a node, take its first item */
970             if( p_playlist->request.p_item == NULL && i_skip == 0 )
971             {
972                 i_skip++;
973             }
974
975             p_view = playlist_ViewFind( p_playlist,p_playlist->request.i_view );
976             p_playlist->status.p_node = p_playlist->request.p_node;
977             p_playlist->status.i_view = p_playlist->request.i_view;
978             if( !p_view )
979             {
980                 msg_Err( p_playlist, "p_view is NULL and should not! (requested view is %i", p_playlist->request.i_view );
981             }
982             else if( i_skip > 0 )
983             {
984                 for( i = i_skip; i > 0 ; i-- )
985                 {
986                     p_new = playlist_FindNextFromParent( p_playlist,
987                                     p_playlist->request.i_view,
988                                     p_view->p_root,
989                                     p_playlist->request.p_node,
990                                     p_new );
991                     if( p_new == NULL )
992                     {
993 #ifdef PLAYLIST_DEBUG
994                         msg_Dbg( p_playlist, "looping" );
995 #endif
996                         p_new = playlist_FindNextFromParent( p_playlist,
997                                   p_playlist->request.i_view,
998                                   p_view->p_root,
999                                   p_view->p_root,
1000                                   NULL );
1001                         if( p_new == NULL ) break;
1002                     }
1003                 }
1004             }
1005             else if( i_skip < 0 )
1006             {
1007                 for( i = i_skip; i < 0 ; i++ )
1008                 {
1009                     p_new = playlist_FindPrevFromParent( p_playlist,
1010                                     p_playlist->request.i_view,
1011                                     p_view->p_root,
1012                                     p_playlist->request.p_node,
1013                                     p_new );
1014                     if( p_new == NULL )
1015                     {
1016                     /* We reach the beginning of the playlist.
1017                        Go back to the last item. */
1018                         p_new = playlist_RecursiveFindLast( p_playlist,
1019                                                             p_view->p_root );
1020                     }
1021                     if( p_new == NULL ) break;
1022                 }
1023
1024             }
1025         }
1026         /* Clear the request */
1027         p_playlist->request.b_request = VLC_FALSE;
1028     }
1029     /* "Automatic" item change ( next ) */
1030     else
1031     {
1032         p_playlist->request_date = 0;
1033
1034         if( p_playlist->status.i_view == -1 )
1035         {
1036 #ifdef PLAYLIST_DEBUG
1037         msg_Dbg( p_playlist, "no request - old mode" );
1038 #endif
1039             if( p_playlist->i_index + 1 < p_playlist->i_size )
1040             {
1041                 p_playlist->i_index++;
1042                 p_new = p_playlist->pp_items[p_playlist->i_index];
1043                 if( !( p_new->i_flags & PLAYLIST_SKIP_FLAG ) )
1044                 {
1045                     return NULL;
1046                 }
1047             }
1048             else
1049             {
1050                 if( b_loop && p_playlist->i_size > 0)
1051                 {
1052                     p_playlist->i_index = 0;
1053                     p_new = p_playlist->pp_items[0];
1054                 }
1055                 else
1056                     p_new = NULL;
1057             }
1058         }
1059         /* We are playing with a view */
1060         else
1061         {
1062 #ifdef PLAYLIST_DEBUG
1063             msg_Dbg( p_playlist,"no request - from a view" );
1064 #endif
1065             playlist_view_t *p_view =
1066                     playlist_ViewFind( p_playlist,
1067                                    p_playlist->status.i_view );
1068             if( !p_view )
1069             {
1070                 msg_Err( p_playlist, "p_view is NULL and should not! (FIXME)" );
1071             }
1072             else
1073             {
1074                 p_new = playlist_FindNextFromParent( p_playlist,
1075                             p_playlist->status.i_view,
1076                             p_view->p_root,
1077                             p_playlist->status.p_node,
1078                             p_playlist->status.p_item );
1079                 if( p_new == NULL && b_loop )
1080                 {
1081 #ifdef PLAYLIST_DEBUG
1082                     msg_Dbg( p_playlist, "looping" );
1083 #endif
1084                     p_new = playlist_FindNextFromParent( p_playlist,
1085                                    p_playlist->status.i_view,
1086                                    p_view->p_root,
1087                                    p_view->p_root,
1088                                    NULL );
1089                 }
1090                 if( p_new != NULL && !(p_new->i_flags & PLAYLIST_SKIP_FLAG) )
1091                     return NULL;
1092             }
1093         }
1094     }
1095
1096     /* Reset index */
1097     if( p_playlist->i_index >= 0 && p_new != NULL &&
1098             p_playlist->pp_items[p_playlist->i_index] != p_new )
1099     {
1100         p_playlist->i_index = playlist_GetPositionById( p_playlist,
1101                                                         p_new->input.i_id );
1102     }
1103
1104 #ifdef PLAYLIST_PROFILE
1105     msg_Dbg(p_playlist,"next item found in "I64Fi " us", mdate()-start );
1106 #endif
1107
1108     if( p_new == NULL )
1109     {
1110         msg_Info( p_playlist, "nothing to play" );
1111     }
1112     return p_new;
1113 }
1114
1115 /*****************************************************************************
1116  * PlayItem: start the input thread for an item
1117  ****************************************************************************/
1118 static int PlayItem( playlist_t *p_playlist, playlist_item_t *p_item )
1119 {
1120     vlc_value_t val;
1121
1122     msg_Dbg( p_playlist, "creating new input thread" );
1123
1124     p_item->i_nb_played++;
1125     p_playlist->status.p_item = p_item;
1126
1127     p_playlist->i_index = playlist_GetPositionById( p_playlist,
1128                                                     p_item->input.i_id );
1129
1130 #ifdef PLAYLIST_PROFILE
1131     if( p_playlist->request_date != 0 )
1132     {
1133         msg_Dbg( p_playlist, "request processed after "I64Fi " us",
1134                   mdate() - p_playlist->request_date );
1135     }
1136 #endif
1137
1138     p_playlist->p_input = input_CreateThread( p_playlist, &p_item->input );
1139
1140     var_AddCallback( p_playlist->p_input, "item-change",
1141                          ItemChange, p_playlist );
1142
1143     val.i_int = p_item->input.i_id;
1144     /* unlock the playlist to set the var...mmm */
1145     vlc_mutex_unlock( &p_playlist->object_lock);
1146     var_Set( p_playlist, "playlist-current", val);
1147     vlc_mutex_lock( &p_playlist->object_lock);
1148
1149     return VLC_SUCCESS;
1150
1151 }
1152
1153 /* Forward item change from input */
1154 static int ItemChange( vlc_object_t *p_obj, const char *psz_var,
1155                        vlc_value_t oldval, vlc_value_t newval, void *param )
1156 {
1157     playlist_t *p_playlist = (playlist_t *)param;
1158
1159     //p_playlist->b_need_update = VLC_TRUE;
1160     var_SetInteger( p_playlist, "item-change", newval.i_int );
1161
1162     /* Update view */
1163     /* FIXME: Make that automatic */
1164 //    playlist_ViewUpdate( p_playlist, VIEW_S_AUTHOR );
1165
1166     return VLC_SUCCESS;
1167 }