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